alix 0.8.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
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
use std::{borrow::Cow, path::PathBuf, sync::mpsc::Receiver};

use anyhow::{Result, bail};

use crate::{
    ask,
    backend::{ensure_source_reachable, supports_structured_progress},
    choice,
    config::{AskConfig, GenerateCardStyle, GenerateDeckConfig},
    deck::is_url,
    parser,
    source::resolve_source,
};

pub const DEFAULT_GOAL: &str = "understand the whole source";
const CARD_SHAPE_FILE: &str = include_str!("../docs/include/card-shapes.md");

// The file opens with a human-facing preamble that must reach neither the
// prompt nor the book; both consumers take only the anchored span.
fn card_shape_guide() -> &'static str {
    CARD_SHAPE_FILE
        .split_once("<!-- ANCHOR: guide -->\n")
        .and_then(|(_, rest)| rest.split_once("<!-- ANCHOR_END: guide -->"))
        .map_or(CARD_SHAPE_FILE, |(guide, _)| guide)
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GenerationSpec {
    pub goal: String,
    pub language: Option<String>,
    pub audience: Option<String>,
    pub card_style: GenerateCardStyle,
}

impl GenerationSpec {
    pub fn from_config(goal: impl Into<String>, config: &GenerateDeckConfig) -> Self {
        Self {
            goal: goal.into(),
            language: config.language.clone(),
            audience: config.audience.clone(),
            card_style: config.card_style,
        }
    }

    pub(crate) fn requirements(&self) -> String {
        self.build_requirements(true)
    }

    pub(crate) fn learner_requirements(&self) -> String {
        self.build_requirements(false)
    }

    fn build_requirements(&self, include_card_style: bool) -> String {
        let mut requirements = format!("GENERATION REQUIREMENTS:\n- Learning goal: {}", self.goal);
        if include_card_style {
            requirements.push_str(&format!("\n- Card style: {}", self.card_style.as_str()));
        }
        if let Some(language) = &self.language {
            requirements.push_str(&format!(
                "\n- Output language: {language}. Write every learner-facing word in this language, including fronts, answers, choices, and notes."
            ));
        }
        if let Some(audience) = &self.audience {
            requirements.push_str(&format!(
                "\n- Audience: {audience}. Match vocabulary, assumed knowledge, examples, and difficulty to this audience."
            ));
        }
        requirements.push_str("\nThese requirements are binding.");
        requirements
    }

    fn is_default(&self) -> bool {
        self.goal == DEFAULT_GOAL
            && self.language.is_none()
            && self.audience.is_none()
            && self.card_style == GenerateCardStyle::Mixed
    }
}

pub(crate) fn card_format(style: GenerateCardStyle) -> Cow<'static, str> {
    let guide = card_shape_guide();
    match style {
        GenerateCardStyle::Mixed => Cow::Owned(format!(
            "- Choose each card's shape from the shared guide according to the material. Do \
             not default every card to one shape.\n\n\
             CARD SHAPE GUIDE:\n{guide}\n\
             CARD SHAPE SYNTAX:\n\
             - A plain card puts short answer lines below its `## ` front. Do not prefix \
             answers with bullets or dashes.\n\
             - A cloze card writes its answer line as plain prose and hides each span with \
             a `<!-- blank: span hidden=\"...\" -->` comment on its own line directly below \
             the answer. The hidden text must appear verbatim in the answer line; if it \
             repeats there, add `occurrence=N` (1-based) to pick which one. Blanks belong \
             below answer lines, NEVER on the front. Example: `When the owner leaves scope, \
             the value is dropped.` then `<!-- blank: span hidden=\"dropped\" -->`.\n\
             - An authored-choice card puts 3-5 GitHub task-list options directly below its \
             front: exactly one checked `- [x]` answer and at least two unchecked `- [ ]` \
             distractors, then `<!-- choices: single -->` on the line directly below the last \
             option. Without that line the options stay a literal checklist instead of \
             becoming a card. Add a `> [!NOTE]` note explaining their mistaken premises.\n\
             - A card table starts with `| front | back | note |`, then \
             `| --- | --- | --- |`, then one row per pair, then `<!-- cards -->` on the \
             line DIRECTLY below the last row, with no blank line between. Omit the note \
             column when it is unused. A card table's note is that column: a \
             blockquote below a table does not parse.\n\
             - For ordered steps, write one step per answer line and put `<!-- reveal: line -->` below the \
             last answer line.\n\
             - For a card needed in both directions, put `<!-- direction: both -->` after \
             its answer.\n\
             - For an answer the learner must sketch, put `<!-- input: draw -->` after its \
             answer."
        )),
        GenerateCardStyle::Plain => Cow::Borrowed(
            "- Write every card as a plain question and answer. The plain unindented lines \
             BELOW the front are the answer/back. EVERY card MUST have at least one short \
             answer line. Do not prefix answers with bullets or dashes. Do not use \
             `blank:` span comments or task-list choices. Split mappings into one card \
             per pair.",
        ),
        GenerateCardStyle::Cloze => Cow::Borrowed(
            "- Write EVERY card as cloze. Each answer line is full plain prose, and every \
             hidden span gets a `<!-- blank: span hidden=\"...\" -->` comment on its own \
             line directly below the answer lines. The hidden text must appear verbatim in \
             an answer line; if it repeats, add `occurrence=N` (1-based). Blanks belong \
             below answer lines, NEVER on the front. Do not prefix answers with bullets or \
             dashes. Do not write plain answers or task-list choices. Put a mapping in one \
             card with one pair per line and a blank span hiding each recalled half.",
        ),
        GenerateCardStyle::AuthoredChoices => Cow::Borrowed(
            "- Write EVERY card as authored multiple-choice. Directly below the `## ` front, \
             write 3-5 GitHub task-list options: exactly one checked `- [x]` correct answer \
             and at least two unchecked `- [ ]` plausible distractors. On the line directly \
             below the last option, write `<!-- choices: single -->`; without it the options \
             stay a literal checklist instead of becoming a card. Do not add a separate \
             plain answer or `blank:` span comments. Keep options parallel in form and \
             length. \
             Add a short `> [!NOTE]` note explaining the mistaken premise behind the distractors. \
             For a mapping, make one authored-choice card per pair and use plausible values \
             from the same domain as distractors.",
        ),
    }
}

const DEFAULT_PROMPT: &str = "\
You are an expert at creating spaced-repetition flashcards. Read the web page \
at {url} — use the WebFetch tool to fetch it (once) — and turn its content \
into a flashcard deck.

OUTPUT FORMAT — a Markdown deck, one card after another:
- A block card starts with `## ` at column 0, followed by the question/front on the \
same line. Never indent a card front.
{card_format}
- A `> [!NOTE]` line opens a note shown AFTER answering, and each note line \
goes on its own `> ` line below it. Add a note to most cards: a \
brief elaboration, a concrete example, a mnemonic, or why it matters, one or \
two short lines, never just restating the answer. The note goes last, below \
the answer lines. A blockquote whose first line is NOT one of `[!NOTE]`, \
`[!TIP]`, `[!IMPORTANT]`, `[!WARNING]`, `[!CAUTION]` is a QUOTATION and \
belongs to the answer, so never write a bare `> ` note.
- Every `<!-- ... -->` line TRAILS its card: put it below the card's LAST \
content line, with NO blank line between them, and never above an answer line, \
an option list, or a table.
- An invocation binds the block directly above it, so it sits on the line \
immediately below the last option or row. \
`<!-- cards -->` takes card directives after it, an `at:` locator or a reveal \
mode, but never a blockquote: a card table's note is its note column. \
`<!-- choices: single -->` takes a `> [!NOTE]` note after it.
- To start an answer line with a literal `## `, `> `, `---`, `<!--`, or a \
code-fence marker, escape it with a leading backslash (e.g. `\\## `).

Begin the file with exactly this frontmatter block:
---
link: {url}

---
The `link:` key lets the learner ask follow-up questions against the source.

PEDAGOGY — produce a balanced deck aiming for at most {max_cards} cards, spread across \
four layers of understanding:
  1. Facts & terminology: definitions and key terms.
  2. Concepts & mechanisms: \"why\" and \"how\" questions.
  3. Application: \"given X, what happens / what would you do?\"
  4. Connections — how ideas relate, contrast, or build on each other.
Apply the required card-style instructions throughout all four layers.

CARD QUALITY:
- One idea per card (minimum-information principle); split compound facts.
- The answer must cover exactly what the front asks, no more. If it includes a \
fact the question did not ask for, either narrow the answer to the question, \
widen the question to cover the whole answer, or split into separate cards. \
Extra context goes in the `> [!NOTE]` note, not the answer.
- 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 instead — one card per item \
or group. Only when the ordered list ITSELF is the thing to learn (steps, a \
sequence) keep it as one card with one item per answer line and a \
`<!-- reveal: line -->` line below the last item.
- Give non-choice answers and notes clean structure when the content has it \
(short lines, one point per line); keep an atomic answer atomic; \
never pad a one-word answer into a list.
- Format the question for readability, but never let its layout leak the answer \
(don't hint how many items the answer has).
- NO TWO CARDS MAY TEST THE SAME FACT. If a point is already covered, do not \
add another card for it — vary what each card asks rather than rephrasing the \
same question.
- Fronts must be unambiguous and answerable from memory; avoid yes/no questions.
- Write original questions and answers in your own words; do not copy long \
passages verbatim.
- Give most cards a `> [!NOTE]` note that adds something beyond the answer \
(context, an example, a caveat, or a memory hook).
- Order cards from foundational to advanced.

REVISE before finishing: re-read the entire draft as a set and merge or delete \
any cards that overlap or test the same idea, so every remaining card is \
distinct. A shorter, non-repetitive deck is better than a long one with \
duplicates.

Output ONLY the final, deduplicated deck text — no markdown code fences, no \
preamble, no closing remarks.";

const DEFAULT_SOURCE_PROMPT: &str = "\
You are an expert at creating spaced-repetition flashcards. Explore the source at \
{source} — your working directory is its root; use the Read, Glob and Grep tools \
(read-only, no write or shell access) — and turn its key facts into a flashcard \
deck.

OUTPUT FORMAT — a Markdown deck, one card after another:
- A block card starts with `## ` at column 0, followed by the question/front on the \
same line. Never indent a card front.
{card_format}
- A `> [!NOTE]` line opens a note shown AFTER answering, and each note line \
goes on its own `> ` line below it. Add a note to most cards: a \
brief elaboration, a concrete example, a mnemonic, or why it matters, one or \
two short lines, never just restating the answer. A blockquote whose first \
line is NOT one of `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]`, \
`[!CAUTION]` is a QUOTATION and belongs to the answer, so never write a bare \
`> ` note.
- A `<!-- at: file:start-end -->` line under a card cites where its answer \
lives in the source (e.g. `<!-- at: src/string.rs:120-128 -->`; the path is \
relative to the source root — your working directory). Add one to every card \
whose answer maps to a specific, contiguous range of lines — read the real \
lines, never guess the numbers — so the learner can flip the card to its \
source on reveal. Omit it for a card that synthesizes across several places.
- Every `<!-- ... -->` line TRAILS its card: put it below the card's LAST \
content line, with NO blank line between them, and never above an answer line, \
an option list, or a table.
- An invocation binds the block directly above it, so it sits on the line \
immediately below the last option or row. \
`<!-- cards -->` takes card directives after it, an `at:` locator or a reveal \
mode, but never a blockquote: a card table's note is its note column. \
`<!-- choices: single -->` takes a `> [!NOTE]` note after it.
- To start an answer line with a literal `## `, `> `, `---`, `<!--`, or a \
code-fence marker, escape it with a leading backslash (e.g. `\\## `).

Begin the file with exactly this frontmatter block:
---
source: {source}
choices: single

---
The `source:` key ties the deck to its source, so `alix exam` can later grade \
your understanding against it.

PEDAGOGY — produce a balanced deck aiming for at most {max_cards} cards, spread across \
four layers of understanding:
  1. Facts & terminology: definitions and key terms.
  2. Concepts & mechanisms: \"why\" and \"how\" questions.
  3. Application: \"given X, what happens / what would you do?\"
  4. Connections — how the pieces relate, contrast, or build on each other.
Apply the required card-style instructions throughout all four layers.

CARD QUALITY:
- One idea per card (minimum-information principle); split compound facts.
- The answer must cover exactly what the front asks, no more. If it includes a \
fact the question did not ask for, either narrow the answer to the question, \
widen the question to cover the whole answer, or split into separate cards. \
Extra context goes in the `> [!NOTE]` note, not the answer.
- 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 instead — one card per item \
or group. Only when the ordered list ITSELF is the thing to learn (steps, a \
sequence) keep it as one card with one item per answer line and a \
`<!-- reveal: line -->` line below the last item.
- Give non-choice answers and notes clean structure when the content has it \
(short lines, one point per line); keep an atomic answer atomic; \
never pad a one-word answer into a list.
- Format the question for readability, but never let its layout leak the answer \
(don't hint how many items the answer has).
- NO TWO CARDS MAY TEST THE SAME FACT — vary what each card asks rather than \
rephrasing the same question.
- Ground every card in what the source actually shows; do not invent details \
it doesn't contain. Fronts must be answerable from memory; avoid yes/no questions.
- Give most cards a `> [!NOTE]` note that adds something beyond the answer.
- Order cards from foundational to advanced.

REVISE before finishing: re-read the whole draft and merge or delete any cards \
that overlap or test the same idea, so every remaining card is distinct.

Output ONLY the final, deduplicated deck text — no markdown code fences, no \
preamble, no closing remarks.";

const REVIEW_PROMPT: &str = "\
You are reviewing a spaced-repetition flashcard deck for quality, then \
returning the improved deck.

Apply these edits:
- Remove or MERGE cards that test the same fact or overlap heavily — every \
card must test something distinct. This is the most important fix.
- Drop cards that are ambiguous or trivial, or whose `> [!NOTE]` note merely \
restates the answer.
- Tighten any card whose answer covers more than its front asks: narrow the \
answer to the question, move the extra fact to the `> [!NOTE]` note, or split it into \
distinct cards. A front and its answer must ask and tell the same thing.
{mapping_review}
- Keep the EXACT same file format: the leading `---` frontmatter block, cards \
written as `## ` blocks or card-table rows, plain or task-list answers below \
block fronts, `> [!NOTE]` notes, and any `<!-- key: value -->` directive lines. A \
cloze card keeps its `<!-- blank: span hidden=\"...\" -->` comments below its \
answer lines.
- Preserve the good cards and their order; do not invent filler to hit a count.

Output ONLY the improved deck — no commentary, no markdown code fences.
";

pub fn generate_deck(
    source: &str,
    cfg: &GenerateDeckConfig,
    ask_cfg: &AskConfig,
    spec: &GenerationSpec,
) -> Result<String> {
    let url = is_url(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 = build_prompt(source, url, cfg, spec);
    let raw = ask::run(&run_config(cfg, ask_cfg, url, cwd), &prompt, &[])?;
    let deck = clean_output(&raw);
    if deck.trim().is_empty() {
        bail!("the model returned no deck content");
    }
    validate_card_style(&deck, spec)?;
    Ok(deck)
}

pub fn spawn(
    source: String,
    cfg: GenerateDeckConfig,
    ask: AskConfig,
) -> Receiver<Result<String, String>> {
    let spec = GenerationSpec::from_config(DEFAULT_GOAL, &cfg);
    let (tx, rx) = std::sync::mpsc::channel();
    std::thread::spawn(move || {
        let _ = tx.send(generate_deck(&source, &cfg, &ask, &spec).map_err(|e| format!("{e:#}")));
    });
    rx
}

pub fn review_deck(
    deck: &str,
    cfg: &GenerateDeckConfig,
    ask_cfg: &AskConfig,
    spec: &GenerationSpec,
) -> Result<String> {
    let prompt = build_review_prompt(deck, spec);
    // The reviewer only rewrites the supplied text; no source access needed.
    let raw = ask::run(&run_config(cfg, ask_cfg, true, None), &prompt, &[])?;
    let reviewed = clean_output(&raw);
    if reviewed.trim().is_empty() {
        bail!("the review pass returned no deck content");
    }
    validate_card_style(&reviewed, spec)?;
    Ok(reviewed)
}

fn run_config(
    cfg: &GenerateDeckConfig,
    ask_cfg: &AskConfig,
    url: bool,
    cwd: Option<PathBuf>,
) -> AskConfig {
    let allowed_tools = if url {
        ask_cfg.allowed_tools.clone()
    } else {
        vec!["Read".to_string(), "Glob".to_string(), "Grep".to_string()]
    };
    AskConfig {
        allowed_tools,
        model: cfg.model.clone().or_else(|| ask_cfg.model.clone()),
        timeout_secs: cfg.timeout_for(supports_structured_progress(ask_cfg)),
        progress: true,
        idle_timeout_secs: cfg.idle_timeout(),
        cwd,
        source_access: false,
        ..ask_cfg.clone()
    }
}

fn build_review_prompt(deck: &str, spec: &GenerationSpec) -> String {
    let review = REVIEW_PROMPT.replace("{mapping_review}", review_mapping(spec.card_style));
    let card_format = card_format(spec.card_style);
    format!(
        "{review}{}\n\nNOTE SAFETY:\n- {}\n\n{}\n\nThe deck to review:\n\n{deck}",
        spec.requirements(),
        choice::NOTE_POSITION_INSTRUCTION,
        card_format
    )
}

fn review_mapping(style: GenerateCardStyle) -> &'static str {
    match style {
        GenerateCardStyle::Mixed => {
            "- Rewrite any card that recalls a whole mapping of pairs at once \
             (\"match each X to its Y\") as a card table with one row per pair. Ordered \
             steps may stay a `<!-- reveal: line -->` card."
        }
        GenerateCardStyle::Cloze => {
            "- Rewrite any card that recalls a whole mapping or table of pairs at once \
             (\"match each X to its Y\") as one cloze card: one line per pair, a blank span \
             comment hiding each recalled half. Ordered steps may stay a \
             `<!-- reveal: line -->` card; unordered pairs never."
        }
        GenerateCardStyle::Plain => {
            "- Rewrite any card that recalls a whole mapping or table of pairs at once \
             (\"match each X to its Y\") as distinct plain cards, one pair per card. \
             Ordered steps may stay a `<!-- reveal: line -->` card; unordered pairs never."
        }
        GenerateCardStyle::AuthoredChoices => {
            "- Rewrite any card that recalls a whole mapping or table of pairs at once \
             (\"match each X to its Y\") as distinct authored-choice cards, one pair per \
             card, with plausible values from the same domain as distractors. Ordered \
             steps may stay a `<!-- reveal: line -->` card; unordered pairs never."
        }
    }
}

fn build_prompt(
    source: &str,
    url: bool,
    cfg: &GenerateDeckConfig,
    spec: &GenerationSpec,
) -> String {
    let template = cfg.prompt.as_deref().unwrap_or(if url {
        DEFAULT_PROMPT
    } else {
        DEFAULT_SOURCE_PROMPT
    });
    let card_format = card_format(spec.card_style);
    let has_card_format = template.contains("{card_format}");
    let mut prompt = template
        .replace("{url}", source)
        .replace("{source}", source)
        .replace("{max_cards}", &cfg.max_cards.to_string())
        .replace("{card_format}", card_format.as_ref());
    if let Some(extra) = cfg
        .extra
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty())
    {
        prompt.push_str("\n\nAdditional instructions:\n");
        prompt.push_str(extra);
    }
    if cfg.prompt.is_none() || !spec.is_default() {
        prompt.push_str("\n\n");
        prompt.push_str(&spec.requirements());
        if !has_card_format {
            prompt.push_str("\n\n");
            prompt.push_str(card_format.as_ref());
        }
    }
    prompt.push_str("\n\nNOTE SAFETY:\n- ");
    prompt.push_str(choice::NOTE_POSITION_INSTRUCTION);
    prompt
}

pub(crate) fn validate_card_style(deck: &str, spec: &GenerationSpec) -> Result<()> {
    let parsed = match parser::parse("generated.md", deck) {
        Ok(parsed) => parsed,
        Err(_) if spec.card_style == GenerateCardStyle::Mixed => return Ok(()),
        Err(error) => {
            return Err(anyhow::anyhow!(
                "cannot verify generated card style: {error}"
            ));
        }
    };
    if parsed
        .lints
        .iter()
        .any(|lint| lint.kind == parser::LintKind::ChoiceNoteNamesPosition)
    {
        bail!(
            "the model returned an authored-choice note that names an option by position; \
             options are shuffled, so name the claim or mistaken premise instead"
        );
    }
    if spec.card_style == GenerateCardStyle::Mixed {
        return Ok(());
    }
    let cards = parsed.cards;
    if cards.is_empty() {
        bail!("the model returned no cards");
    }
    let invalid = cards
        .iter()
        .filter(|card| match spec.card_style {
            GenerateCardStyle::Mixed => false,
            GenerateCardStyle::Plain => {
                card.is_blank_card() || !card.authored_distractors.is_empty()
            }
            GenerateCardStyle::Cloze => !card.is_blank_card(),
            GenerateCardStyle::AuthoredChoices => {
                !(2..=4).contains(&card.authored_distractors.len())
            }
        })
        .count();
    if invalid > 0 {
        bail!(
            "the model returned {invalid} card(s) that do not match the requested {} style",
            match spec.card_style {
                GenerateCardStyle::AuthoredChoices => "authored multiple-choice",
                style => style.as_str(),
            }
        );
    }
    Ok(())
}

/// Trailing prose isn't stripped: it can't be told apart from a card's
/// answer line.
fn clean_output(raw: &str) -> String {
    let lines: Vec<&str> = raw.lines().collect();
    let Some(start) = lines.iter().position(|l| {
        crate::parser::is_frontmatter_fence(l) || l.starts_with("# ") || l.starts_with("## ")
    }) else {
        return raw.trim().to_string();
    };
    let mut end = lines.len();
    while end > start + 1 {
        let t = lines[end - 1].trim();
        if t.is_empty() || t.starts_with("```") {
            end -= 1;
        } else {
            break;
        }
    }
    space_cards(&lines[start..end])
}

fn space_cards(lines: &[&str]) -> String {
    let mut out: Vec<&str> = Vec::with_capacity(lines.len());
    let mut seen_card = false;
    let mut fence: Option<(char, usize)> = None;
    for &line in lines {
        match fence {
            Some((ch, open)) => {
                if crate::parser::closes_fence(line, ch, open) {
                    fence = None;
                }
            }
            None => {
                if let Some(opened) = crate::parser::fence_opener(line) {
                    fence = Some(opened);
                } else if line.starts_with("## ") {
                    if seen_card && out.last().is_some_and(|prev| !prev.trim().is_empty()) {
                        out.push("");
                    }
                    seen_card = true;
                }
            }
        }
        out.push(line);
    }
    out.join("\n")
}

pub fn slug_from_url(url: &str) -> String {
    let without_scheme = url.split_once("://").map(|(_, r)| r).unwrap_or(url);
    let (host, path) = match without_scheme.split_once('/') {
        Some((h, p)) => (h, p),
        None => (without_scheme, ""),
    };
    let last_segment = path
        .split(['?', '#'])
        .next()
        .unwrap_or("")
        .trim_end_matches('/')
        .rsplit('/')
        .find(|s| !s.is_empty());
    // Only a real path segment loses its extension; the host fallback keeps
    // its dot (it's part of the domain, not an extension).
    let base = match last_segment {
        Some(seg) => seg.rsplit_once('.').map(|(b, _)| b).unwrap_or(seg),
        None => host,
    };

    slugify(base)
}

pub fn deck_name(source: &str) -> String {
    if is_url(source) {
        slug_from_url(source)
    } else {
        slug_from_path(source)
    }
}

pub fn slug_from_path(source: &str) -> String {
    let p = std::path::Path::new(source);
    let base = p
        .file_stem()
        .or_else(|| p.file_name())
        .and_then(|s| s.to_str())
        .unwrap_or("deck");
    slugify(base)
}

fn slugify(base: &str) -> String {
    let mut slug = String::new();
    let mut pending_dash = false;
    for c in base.chars() {
        if c.is_ascii_alphanumeric() {
            if pending_dash && !slug.is_empty() {
                slug.push('-');
            }
            slug.push(c.to_ascii_lowercase());
            pending_dash = false;
        } else {
            pending_dash = true;
        }
    }
    if slug.is_empty() {
        "deck".to_string()
    } else {
        slug
    }
}

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

    fn cfg(max_cards: usize) -> GenerateDeckConfig {
        GenerateDeckConfig {
            max_cards,
            ..GenerateDeckConfig::default()
        }
    }

    fn spec() -> GenerationSpec {
        GenerationSpec::from_config(DEFAULT_GOAL, &GenerateDeckConfig::default())
    }

    #[test]
    fn run_config_prefers_the_generate_model_and_falls_back_to_the_ask_model() {
        let ask = AskConfig {
            model: Some("ask-model".to_string()),
            ..AskConfig::default()
        };

        let mut generate = cfg(10);
        generate.model = Some("deck-model".to_string());
        assert_eq!(
            Some("deck-model".to_string()),
            run_config(&generate, &ask, true, None).model,
            "the generate-scoped model must win over the ask model"
        );

        generate.model = None;
        assert_eq!(
            Some("ask-model".to_string()),
            run_config(&generate, &ask, true, None).model,
            "without a generate model the ask model applies"
        );
    }

    #[test]
    fn only_the_exact_default_generation_spec_is_default() {
        let cases = [
            ("the exact default", spec(), true),
            (
                "a different goal",
                GenerationSpec {
                    goal: "learn one topic".to_string(),
                    ..spec()
                },
                false,
            ),
            (
                "an output language",
                GenerationSpec {
                    language: Some("German".to_string()),
                    ..spec()
                },
                false,
            ),
            (
                "an audience",
                GenerationSpec {
                    audience: Some("new programmers".to_string()),
                    ..spec()
                },
                false,
            ),
            (
                "a fixed card style",
                GenerationSpec {
                    card_style: GenerateCardStyle::Plain,
                    ..spec()
                },
                false,
            ),
        ];

        for (name, candidate, expected) in cases {
            assert_eq!(expected, candidate.is_default(), "{name}");
        }
    }

    #[test]
    fn run_config_derives_the_exact_tool_root_and_source_access_policy() {
        let ask = AskConfig {
            allowed_tools: vec!["ParentTool".to_string()],
            cwd: Some(PathBuf::from("/parent")),
            source_access: true,
            ..AskConfig::default()
        };
        let cases = [
            (
                "a URL keeps the parent's network tools but takes the requested root",
                true,
                Some(PathBuf::from("/url-root")),
                vec!["ParentTool".to_string()],
                Some(PathBuf::from("/url-root")),
            ),
            (
                "a local source gets read-only local tools and clears the parent root",
                false,
                None,
                vec!["Read".to_string(), "Glob".to_string(), "Grep".to_string()],
                None,
            ),
        ];

        for (name, url, cwd, expected_tools, expected_cwd) in cases {
            let derived = run_config(&cfg(10), &ask, url, cwd);
            assert_eq!(
                expected_tools, derived.allowed_tools,
                "{name}: allowed tools"
            );
            assert_eq!(expected_cwd, derived.cwd, "{name}: working directory");
            assert!(
                !derived.source_access,
                "{name}: source access is always off"
            );
        }
    }

    #[test]
    fn prompt_substitutes_url_and_card_count() {
        let p = build_prompt("https://example.org/page", true, &cfg(12), &spec());
        assert!(p.contains("https://example.org/page"));
        assert!(p.contains("aiming for at most 12 cards"));
        assert!(p.contains("link: https://example.org/page"));
        assert!(p.contains("## "));
        assert!(p.contains("four layers"));
        assert!(!p.contains("{url}"));
        assert!(!p.contains("{max_cards}"));
        assert!(p.contains("blank: span hidden="));
        assert!(p.contains("hidden=\"dropped\""));
        assert!(p.contains("NEVER on the front"));
        assert!(p.contains("Every card needs at least one answer line"));
        assert!(p.contains("Add a note to most cards"));
        assert!(p.contains("Give most cards a `> [!NOTE]` note"));
        assert!(p.contains("NO TWO CARDS MAY TEST THE SAME FACT"));
        assert!(p.contains("REVISE before finishing"));
        assert!(p.contains("cover exactly what the front asks"));
        assert!(p.contains("one row per pair"));
        assert!(!p.contains("indented answer"));
    }

    #[test]
    fn the_card_shape_guide_is_the_anchored_span_without_the_preamble() {
        let guide = card_shape_guide();
        assert_ne!(
            CARD_SHAPE_FILE, guide,
            "the strip did nothing; the anchors are gone from docs/include/card-shapes.md"
        );
        assert!(
            guide.starts_with("Which card shape suits which material."),
            "the guide must open with the first rule, not the preamble"
        );
        assert!(
            !guide.contains("ANCHOR") && !guide.contains("consumed twice"),
            "no anchor marker or preamble text may reach the prompt"
        );
    }

    #[test]
    fn the_card_shape_guide_teaches_the_current_review_key() {
        let guide = card_shape_guide();
        assert!(
            !guide.contains("`order:`"),
            "the production generator guide teaches an `order:` key the format does not define: {guide}"
        );
        assert!(
            guide.contains("`review:`"),
            "the production generator guide omits the replacement `review:` key: {guide}"
        );
    }

    #[test]
    fn mixed_prompt_embeds_the_shared_card_shape_guide_and_its_syntax() {
        let prompt = build_prompt("https://example.org/page", true, &cfg(12), &spec());
        let guide = card_shape_guide();

        assert!(
            prompt.contains(guide),
            "the mixed prompt must carry the shared authoring guide"
        );
        for syntax in [
            "| front | back | note |",
            "<!-- reveal: line -->",
            "<!-- input: draw -->",
            "<!-- direction: both -->",
        ] {
            assert!(
                prompt.contains(syntax),
                "the mixed prompt must teach `{syntax}`"
            );
        }
    }

    #[test]
    fn each_explicit_card_style_keeps_its_override_contract() {
        let guide = card_shape_guide();
        let cases = [
            (
                GenerateCardStyle::Plain,
                "Write every card as a plain question and answer",
            ),
            (GenerateCardStyle::Cloze, "Write EVERY card as cloze"),
            (
                GenerateCardStyle::AuthoredChoices,
                "Write EVERY card as authored multiple-choice",
            ),
        ];

        for (card_style, contract) in cases {
            let prompt = build_prompt(
                "https://example.org/page",
                true,
                &cfg(12),
                &GenerationSpec {
                    card_style,
                    ..spec()
                },
            );
            assert!(
                prompt.contains(contract),
                "{} must keep its override contract",
                card_style.as_str()
            );
            assert!(
                !prompt.contains(guide),
                "{} must not receive conflicting mixed-shape guidance",
                card_style.as_str()
            );
        }
    }

    #[test]
    fn authored_choice_prompt_allows_its_badged_note_after_the_invocation() {
        let choices = GenerationSpec {
            card_style: GenerateCardStyle::AuthoredChoices,
            ..spec()
        };
        for (source, url) in [("https://example.org/page", true), ("src/lib.rs", false)] {
            let prompt = build_prompt(source, url, &cfg(12), &choices);

            assert!(
                !prompt.contains("Nothing belonging to the card may follow a directive"),
                "that absolute rule forbids the note the authored-choice contract requires ({source}): {prompt}"
            );
            assert!(
                prompt.contains("binds the block directly above it"),
                "and the satisfiable order replaces it rather than leaving the question open ({source}): {prompt}"
            );
        }
    }

    #[test]
    fn the_authored_choice_shape_the_prompt_teaches_parses_and_binds() {
        let taught = "## Pick one\n- [x] right\n- [ ] wrong\n- [ ] also wrong\n\
                      <!-- choices: single -->\n> [!NOTE]\n> the mistaken premise\n";
        let parsed = parser::parse("deck.md", taught).expect("the taught shape parses");
        let card = &parsed.cards[0];
        assert_eq!(
            vec!["wrong".to_string(), "also wrong".to_string()],
            card.authored_distractors,
            "the invocation binds the option list it sits under: {card:?}"
        );
        assert_eq!(
            Some("the mistaken premise"),
            card.only_note(),
            "and the note after it is still a note: {card:?}"
        );

        let inverted = "## Pick one\n- [x] right\n- [ ] wrong\n- [ ] also wrong\n\
                        > [!NOTE]\n> the mistaken premise\n<!-- choices: single -->\n";
        assert!(
            parser::parse("deck.md", inverted).is_err(),
            "and the other order does not parse, which is why the order is taught"
        );
    }

    /// The same question the authored-choice contract failed: can a card obey
    /// every rule the prompts state about it at once? A card table cannot
    /// carry a blockquote note in EITHER position, so the note rule has to
    /// name its exception or a generated deck fails to open.
    #[test]
    fn a_card_table_carries_its_note_in_a_column_and_nowhere_else() {
        let column = "## T\n| front | back | note |\n| --- | --- | --- |\n| a | b | n |\n\
                      <!-- cards -->\n";
        let parsed = parser::parse("deck.md", column).expect("the note column parses");
        assert_eq!(
            Some("n"),
            parsed.cards[0].only_note(),
            "a card table's note is its column: {parsed:?}"
        );

        for (position, text) in [
            (
                "below the invocation",
                "## T\n| front | back |\n| --- | --- |\n| a | b |\n<!-- cards -->\n\
                 > [!NOTE]\n> why\n",
            ),
            (
                "between the table and its invocation",
                "## T\n| front | back |\n| --- | --- |\n| a | b |\n> [!NOTE]\n> why\n\
                 <!-- cards -->\n",
            ),
        ] {
            assert!(
                parser::parse("deck.md", text).is_err(),
                "a blockquote note {position} does not parse, which is why the \
                 prompts name the exception"
            );
        }

        for prompt in [
            build_prompt("https://example.org/page", true, &cfg(12), &spec()),
            build_prompt("src/lib.rs", false, &cfg(12), &spec()),
        ] {
            assert!(
                prompt.contains("A card table's note is that column"),
                "and the prompt teaching card tables states it: {prompt}"
            );
        }
    }

    /// The narrow half of the same rule: a table invocation must keep the
    /// trailing directives the prompts separately REQUIRE, or the repair for
    /// the note trades a broken deck for a deck missing its provenance.
    #[test]
    fn a_card_table_invocation_keeps_the_directives_the_prompts_require() {
        let cited = parser::parse(
            "deck.md",
            "| front | back |\n| --- | --- |\n| a | b |\n<!-- cards -->\n\
             <!-- at: src/lib.rs:1-2 -->\n",
        )
        .expect("a table invocation carries the locator the prompt requires");
        assert_eq!(
            "src/lib.rs:1-2", cited.cards[0].citations[0].locator,
            "the citation survives on a row card: {cited:?}"
        );

        for (surface, prompt) in [
            (
                "the local-source generation prompt",
                build_prompt("src/lib.rs", false, &cfg(12), &spec()),
            ),
            (
                "the URL generation prompt",
                build_prompt("https://example.org/page", true, &cfg(12), &spec()),
            ),
        ] {
            assert!(
                !prompt.contains("takes nothing after it"),
                "{surface} must not forbid the trailing directives it asks for \
                 elsewhere: {prompt}"
            );
        }
    }

    /// The two combinations the earlier probes never put together: a card
    /// table plus the reveal directive the prompts offer it, and a choice card
    /// carrying THREE trailing things at once. Both parse, so the prompts may
    /// keep teaching them.
    #[test]
    fn the_remaining_joint_trailing_shapes_parse_in_prompt_order() {
        let table = parser::parse(
            "deck.md",
            "| front | back |\n| --- | --- |\n| a | b |\n<!-- cards -->\n\
             <!-- reveal: line -->\n",
        )
        .expect("a card-table invocation carries the taught reveal directive");
        assert!(
            table
                .cards
                .iter()
                .all(|card| card.reveal == Some(crate::depth::Reveal::Line)),
            "and the mode reaches every row card: {table:?}"
        );

        let choice = parser::parse(
            "deck.md",
            "## Pick one\n- [x] right\n- [ ] wrong\n- [ ] also wrong\n\
             <!-- choices: single -->\n<!-- at: src/lib.rs:1-2 -->\n\
             > [!NOTE]\n> why the alternatives fail\n",
        )
        .expect("a choice invocation carries a locator and then its taught note");
        let card = &choice.cards[0];
        assert_eq!(
            Some("why the alternatives fail"),
            card.only_note(),
            "the note survives two directives above it: {card:?}"
        );
        assert_eq!(
            "src/lib.rs:1-2", card.citations[0].locator,
            "so does the locator: {card:?}"
        );
        assert_eq!(
            vec!["wrong", "also wrong"],
            card.authored_distractors,
            "and the invocation still binds its list: {card:?}"
        );
    }

    #[test]
    fn readme_quickstart_teaches_the_admonition_note_shape() {
        let readme = include_str!("../README.md");
        let explanation = readme
            .split_once("The front is the `## ` line")
            .map(|(_, tail)| tail.lines().take(5).collect::<Vec<_>>().join("\n"))
            .expect("the quickstart explanation remains present");

        assert!(
            explanation.contains("[!NOTE]"),
            "the prose below the badged example must name the badge: {explanation}"
        );
        assert!(
            !explanation.contains("A `> ` line\nis a note"),
            "a bare blockquote is answer quotation now, not a note: {explanation}"
        );
    }

    #[test]
    fn mixed_review_preserves_pair_mappings_as_card_tables() {
        let prompt = build_review_prompt("## Draft\nAnswer\n", &spec());

        assert!(prompt.contains("card table"), "prompt: {prompt}");
        assert!(!prompt.contains("as one cloze card"), "prompt: {prompt}");
    }

    #[test]
    fn review_prompt_embeds_the_deck_and_asks_to_dedupe() {
        let p = build_review_prompt("---\nlink: u\n---\n\n## Q\nA\n", &spec());
        assert!(p.contains("## Q"));
        assert!(p.contains("MERGE cards that test the same fact"));
        assert!(p.contains("Output ONLY the improved deck"));
        assert!(p.contains("must ask and tell the same thing"));
        assert!(p.contains("one row per pair"));
        assert!(p.ends_with("---\nlink: u\n---\n\n## Q\nA\n"));
        assert!(p.contains("`---` frontmatter block"));
    }

    #[test]
    fn extra_guidance_is_appended() {
        let mut g = cfg(10);
        g.extra = Some("Focus on the public API.".to_string());
        let p = build_prompt("u", true, &g, &spec());
        assert!(p.contains("Additional instructions:"));
        assert!(p.contains("Focus on the public API."));
    }

    #[test]
    fn prompt_carries_goal_language_and_authored_choice_contract() {
        let spec = GenerationSpec {
            goal: "recognize the constitutional institutions".to_string(),
            language: Some("German".to_string()),
            audience: Some("German high-school students".to_string()),
            card_style: GenerateCardStyle::AuthoredChoices,
        };
        let p = build_prompt("u", true, &cfg(10), &spec);

        assert!(p.contains("recognize the constitutional institutions"));
        assert!(p.contains("German"));
        assert!(p.contains("German high-school students"));
        assert!(p.contains("- [x]"));
        assert!(p.contains("- [ ]"));
        assert!(p.contains("exactly one checked"));
        assert!(p.contains(choice::NOTE_POSITION_INSTRUCTION));
        assert!(!p.contains("no bullet"));
    }

    #[test]
    fn authored_choice_prompt_contract_url_teaches_the_required_trailing_invocation() {
        let spec = GenerationSpec {
            goal: "learn it".to_string(),
            language: None,
            audience: None,
            card_style: GenerateCardStyle::AuthoredChoices,
        };

        let prompt = build_prompt("https://example.org", true, &cfg(10), &spec);

        assert!(
            prompt.contains("<!-- choices: single -->"),
            "the URL template has no deck-wide choices default, so the explicit style must teach the trailing invocation: {prompt}"
        );
    }

    /// Every prompt that teaches the task-list shape must also teach the
    /// invocation that turns it into a card: the URL template carries no
    /// `choices:` default, so a taught shape without it is a checklist.
    #[test]
    fn every_card_style_teaching_task_list_options_also_teaches_the_invocation() {
        for style in [GenerateCardStyle::Mixed, GenerateCardStyle::AuthoredChoices] {
            let format = card_format(style);
            if !format.contains("- [x]") {
                continue;
            }
            assert!(
                format.contains("<!-- choices: single -->"),
                "{style:?} teaches task-list options without the trailing invocation, \
                 so its output parses as a literal checklist: {format}"
            );
        }
    }

    #[test]
    fn authored_choice_prompt_contract_url_shape_survives_the_validator() {
        let spec = GenerationSpec {
            goal: "learn it".to_string(),
            language: None,
            audience: None,
            card_style: GenerateCardStyle::AuthoredChoices,
        };
        let following_the_prompt = "---\nlink: https://example.org\n\n---\n## Pick one\n- [x] Right\n- [ ] Wrong A\n- [ ] Wrong B\n<!-- choices: single -->\n";
        let without_the_invocation = "---\nlink: https://example.org\n\n---\n## Pick one\n- [x] Right\n- [ ] Wrong A\n- [ ] Wrong B\n";

        assert!(
            validate_card_style(following_the_prompt, &spec).is_ok(),
            "the explicit URL prompt's prescribed task-list shape must pass its own validator"
        );
        assert!(
            validate_card_style(without_the_invocation, &spec).is_err(),
            "the shape the prompt used to prescribe is exactly what the validator rejects, \
             which is why the instruction has to name the invocation"
        );
    }

    #[test]
    fn authored_choice_prompt_contract_local_source_has_a_deck_default() {
        let spec = GenerationSpec {
            goal: "learn it".to_string(),
            language: None,
            audience: None,
            card_style: GenerateCardStyle::AuthoredChoices,
        };

        let prompt = build_prompt(".", false, &cfg(10), &spec);

        assert!(prompt.contains("choices: single"));
        assert!(
            validate_card_style(
                "---\nchoices: single\n---\n## Pick one\n- [x] Right\n- [ ] Wrong A\n- [ ] Wrong B\n",
                &spec,
            )
            .is_ok(),
            "the local-source template's deck default keeps the same bare task list valid"
        );
    }

    #[test]
    fn generation_spec_reads_configured_defaults() {
        let config = GenerateDeckConfig {
            language: Some("German".to_string()),
            audience: Some("new voters".to_string()),
            card_style: GenerateCardStyle::AuthoredChoices,
            ..GenerateDeckConfig::default()
        };

        let spec = GenerationSpec::from_config("recognize institutions", &config);

        assert_eq!("recognize institutions", spec.goal);
        assert_eq!(Some("German".to_string()), spec.language);
        assert_eq!(Some("new voters".to_string()), spec.audience);
        assert_eq!(GenerateCardStyle::AuthoredChoices, spec.card_style);
    }

    #[test]
    fn explicit_controls_are_appended_to_a_custom_prompt() {
        let mut config = cfg(5);
        config.prompt = Some("Custom prompt for {source}.".to_string());
        let cases = [
            GenerationSpec {
                goal: "recognize institutions".to_string(),
                ..spec()
            },
            GenerationSpec {
                language: Some("German".to_string()),
                ..spec()
            },
            GenerationSpec {
                audience: Some("new voters".to_string()),
                ..spec()
            },
            GenerationSpec {
                card_style: GenerateCardStyle::AuthoredChoices,
                ..spec()
            },
        ];

        for spec in cases {
            let prompt = build_prompt("notes.md", false, &config, &spec);
            assert!(prompt.starts_with("Custom prompt for notes.md."));
            assert!(prompt.contains("GENERATION REQUIREMENTS"));
            assert!(prompt.contains(&spec.goal));
            if let Some(language) = spec.language {
                assert!(prompt.contains(&language));
            }
            if let Some(audience) = spec.audience {
                assert!(prompt.contains(&audience));
            }
            if spec.card_style == GenerateCardStyle::AuthoredChoices {
                assert!(prompt.contains("exactly one checked"));
            }
        }
    }

    #[test]
    fn review_prompt_preserves_language_and_authored_choices() {
        let spec = GenerationSpec {
            goal: "learn the topic".to_string(),
            language: Some("de".to_string()),
            audience: Some("beginners".to_string()),
            card_style: GenerateCardStyle::AuthoredChoices,
        };
        let p = build_review_prompt("## Frage\n- [x] Ja\n- [ ] Nein\n", &spec);

        assert!(p.contains("de"));
        assert!(p.contains("beginners"));
        assert!(p.contains("authored multiple-choice"));
        assert!(p.contains("exactly one checked"));
        assert!(p.contains("one pair per"));
        assert!(p.contains(choice::NOTE_POSITION_INSTRUCTION));
        assert!(!p.contains("as one cloze card"));
    }

    #[test]
    fn custom_generation_prompts_still_forbid_position_dependent_choice_notes() {
        let mut config = cfg(5);
        config.prompt = Some("Custom prompt for {source}.".to_string());

        let prompt = build_prompt("notes.md", false, &config, &spec());

        assert!(prompt.contains(choice::NOTE_POSITION_INSTRUCTION));
    }

    #[test]
    fn review_deck_runs_the_backend_and_returns_the_checked_style() {
        use crate::testutil::{ask_config, exec_lock, fake_reply};

        let _guard = exec_lock();
        let dir = tempfile::tempdir().unwrap();
        let cli = fake_reply(
            dir.path(),
            "---\nchoices: single\n---\n## Pick one\n- [ ] Wrong A\n- [x] Correct\n- [ ] Wrong B\n",
        );
        let spec = GenerationSpec {
            goal: "learn it".to_string(),
            language: None,
            audience: None,
            card_style: GenerateCardStyle::AuthoredChoices,
        };

        let reviewed = review_deck(
            "## Draft\n- [ ] A\n- [x] B\n- [ ] C\n",
            &cfg(10),
            &ask_config(&cli),
            &spec,
        )
        .unwrap();

        assert!(reviewed.contains("- [x] Correct"));
    }

    #[test]
    fn authored_choice_generation_rejects_plain_cards() {
        use crate::testutil::{ask_config, exec_lock, fake_reply};

        let _guard = exec_lock();
        let dir = tempfile::tempdir().unwrap();
        let cli = fake_reply(dir.path(), "## Plain question\nPlain answer\n");
        let spec = GenerationSpec {
            goal: "learn the topic".to_string(),
            language: None,
            audience: None,
            card_style: GenerateCardStyle::AuthoredChoices,
        };

        let err =
            generate_deck("https://example.org", &cfg(10), &ask_config(&cli), &spec).unwrap_err();

        assert!(format!("{err:#}").contains("authored multiple-choice"));
    }

    #[test]
    fn malformed_output_is_tolerated_only_when_no_explicit_card_style_was_requested() {
        let malformed = "---\nformat-version: 1\nbroken: [\n---\n## Question\nAnswer\n";

        for card_style in [
            GenerateCardStyle::Plain,
            GenerateCardStyle::Cloze,
            GenerateCardStyle::AuthoredChoices,
        ] {
            let spec = GenerationSpec {
                goal: "learn it".to_string(),
                language: None,
                audience: None,
                card_style,
            };
            let error = validate_card_style(malformed, &spec).unwrap_err();
            assert!(
                format!("{error:#}").contains("cannot verify generated card style"),
                "{card_style:?}: {error:#}"
            );
        }

        validate_card_style(malformed, &spec()).unwrap();
    }

    #[test]
    fn authored_choices_require_three_to_five_options() {
        let spec = GenerationSpec {
            goal: "learn it".to_string(),
            language: None,
            audience: None,
            card_style: GenerateCardStyle::AuthoredChoices,
        };

        let too_few = validate_card_style("## Pick one\n- [x] Right\n- [ ] Wrong\n", &spec);
        let too_many = validate_card_style(
            "## Pick one\n- [x] Right\n- [ ] A\n- [ ] B\n- [ ] C\n- [ ] D\n- [ ] E\n",
            &spec,
        );

        assert!(too_few.is_err());
        assert!(too_many.is_err());
    }

    #[test]
    fn a_generated_choice_note_cannot_name_a_position_that_shuffle_changes() {
        let deck = "---\nchoices: single\n---\n## Pick one\n- [x] Correct claim\n- [ ] First misconception\n- [ ] Second misconception\n> [!NOTE]\n> Option 2 reverses the relation.\n";

        for card_style in [GenerateCardStyle::Mixed, GenerateCardStyle::AuthoredChoices] {
            let spec = GenerationSpec {
                goal: "learn it".to_string(),
                language: None,
                audience: None,
                card_style,
            };
            let error = validate_card_style(deck, &spec).unwrap_err();

            assert!(
                format!("{error:#}").contains("shuffled"),
                "{card_style:?}: {error:#}"
            );
        }
    }

    #[test]
    fn each_explicit_card_style_accepts_its_canonical_shape() {
        let cases = [
            (GenerateCardStyle::Plain, "## Question\nAnswer\n"),
            (
                GenerateCardStyle::Cloze,
                "## Complete it\nThe value is dropped.\n<!-- blank: span hidden=\"dropped\" -->\n",
            ),
            (
                GenerateCardStyle::AuthoredChoices,
                "---\nchoices: single\n---\n## Pick one\n- [ ] Wrong A\n- [x] Correct\n- [ ] Wrong B\n",
            ),
        ];

        for (card_style, deck) in cases {
            let spec = GenerationSpec {
                goal: "learn it".to_string(),
                language: None,
                audience: None,
                card_style,
            };
            validate_card_style(deck, &spec).unwrap();
        }
    }

    #[test]
    fn span_blank_cards_satisfy_cloze_style_and_not_plain_style() {
        let deck =
            "## Complete it\nThe value is dropped.\n<!-- blank: span hidden=\"dropped\" -->\n";
        let spec = |card_style| GenerationSpec {
            goal: "learn it".to_string(),
            language: None,
            audience: None,
            card_style,
        };
        validate_card_style(deck, &spec(GenerateCardStyle::Cloze))
            .expect("a span blank IS the requested cloze shape");
        assert!(
            validate_card_style(deck, &spec(GenerateCardStyle::Plain)).is_err(),
            "a span blank is not a plain card"
        );
    }

    #[test]
    fn cloze_style_rejects_a_plain_card() {
        let spec = GenerationSpec {
            goal: "learn it".to_string(),
            language: None,
            audience: None,
            card_style: GenerateCardStyle::Cloze,
        };

        let error = validate_card_style("## Question\nAnswer\n", &spec).unwrap_err();

        assert!(format!("{error:#}").contains("requested cloze style"));
    }

    #[test]
    fn plain_style_rejects_cloze_and_authored_choices() {
        let spec = GenerationSpec {
            goal: "learn it".to_string(),
            language: None,
            audience: None,
            card_style: GenerateCardStyle::Plain,
        };

        let cloze = validate_card_style(
            "## Complete it\nIt is done.\n<!-- blank: span hidden=\"done\" -->\n",
            &spec,
        );
        let choices = validate_card_style(
            "---\nchoices: single\n---\n## Pick one\n- [ ] Wrong A\n- [x] Correct\n- [ ] Wrong B\n",
            &spec,
        );

        assert!(cloze.is_err());
        assert!(choices.is_err());
    }

    #[test]
    fn full_prompt_override_replaces_template_but_keeps_note_safety() {
        let mut g = cfg(5);
        g.prompt = Some("Make {max_cards} cards from {url}.".to_string());
        let p = build_prompt("U", true, &g, &spec());
        assert!(p.starts_with("Make 5 cards from U."));
        assert!(p.contains(choice::NOTE_POSITION_INSTRUCTION));
        assert!(!p.contains("four layers"));
    }

    #[test]
    fn source_prompt_explores_locally_and_ties_to_source() {
        let p = build_prompt("src/scheduler.rs", false, &cfg(8), &spec());
        assert!(p.contains("src/scheduler.rs"));
        assert!(p.contains("Read, Glob and Grep"));
        assert!(p.contains("source: src/scheduler.rs"));
        assert!(p.contains("aiming for at most 8 cards"));
        assert!(!p.contains("WebFetch"));
        assert!(!p.contains("{source}"));
        assert!(p.contains("<!-- at: file:start-end -->"));
        assert!(p.contains("never guess"));
        assert!(p.contains("one row per pair"));
        assert!(!p.contains("indented answer"));
    }

    #[test]
    fn url_prompt_does_not_ask_for_line_citations() {
        let p = build_prompt("https://example.org/page", true, &cfg(8), &spec());
        assert!(!p.contains("<!-- at:"));
    }

    #[test]
    fn slug_from_paths() {
        assert_eq!("scheduler", slug_from_path("src/scheduler.rs"));
        assert_eq!("my-crate", slug_from_path("/home/me/My_Crate"));
    }

    #[test]
    fn clean_strips_code_fence() {
        let raw = "```text\n---\nlink: u\n---\n## Q\nA\n```";
        assert_eq!("---\nlink: u\n---\n## Q\nA", clean_output(raw));
    }

    #[test]
    fn clean_strips_leading_commentary() {
        let raw = "Here is your deck:\n\n---\nlink: u\n---\n## Q\nA\n";
        assert_eq!("---\nlink: u\n---\n## Q\nA", clean_output(raw));
    }

    #[test]
    fn clean_preserves_the_frontmatter_fence_spellings_the_parser_accepts() {
        let raw = "Here is your deck:\n\n--- \nlink: u\n---\t\n## Q\nA\n";
        assert_eq!("--- \nlink: u\n---\t\n## Q\nA", clean_output(raw));
    }

    #[test]
    fn clean_strips_commentary_and_fence_together() {
        let raw = "Here is your deck:\n```text\n---\nlink: u\n---\n## Q\nA\n```";
        assert_eq!("---\nlink: u\n---\n## Q\nA", clean_output(raw));
    }

    #[test]
    fn clean_keeps_a_clean_deck_unchanged() {
        let raw = "## Q\nA";
        assert_eq!("## Q\nA", clean_output(raw));
    }

    #[test]
    fn clean_puts_a_blank_line_between_cards() {
        let raw = "## Q1\nA1\n## Q2\nA2";
        assert_eq!("## Q1\nA1\n\n## Q2\nA2", clean_output(raw));
    }

    #[test]
    fn clean_does_not_double_the_blank_between_cards() {
        let raw = "## Q1\nA1\n\n## Q2\nA2";
        assert_eq!("## Q1\nA1\n\n## Q2\nA2", clean_output(raw));
    }

    #[test]
    fn clean_keeps_the_header_attached_to_the_first_card() {
        let raw = "---\nlink: u\n---\n## Q1\nA1\n## Q2\nA2";
        assert_eq!(
            "---\nlink: u\n---\n## Q1\nA1\n\n## Q2\nA2",
            clean_output(raw)
        );
    }

    #[test]
    fn clean_never_splits_a_fenced_h2_out_of_its_card() {
        let raw = "## Q1\n```\n## not a card\n```\n## Q2\nA2";
        assert_eq!(
            "## Q1\n```\n## not a card\n```\n\n## Q2\nA2",
            clean_output(raw)
        );
    }

    #[test]
    fn slug_from_typical_urls() {
        assert_eq!(
            "ch04-01-what-is-ownership",
            slug_from_url("https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html")
        );
        assert_eq!(
            "rust-programming-language",
            slug_from_url("https://en.wikipedia.org/wiki/Rust_(programming_language)")
        );
        assert_eq!("example-org", slug_from_url("https://example.org"));
        assert_eq!("page", slug_from_url("https://example.org/page?x=1#frag"));
    }

    #[test]
    fn spawn_delivers_generated_deck_text_on_the_channel() {
        use crate::testutil::{ask_config, exec_lock, fake_reply};

        let _g = exec_lock();
        let dir = tempfile::tempdir().unwrap();
        let cli = fake_reply(dir.path(), "---\nlink: https://example.org\n---\n## Q\nA\n");
        let rx = spawn("https://example.org".to_string(), cfg(10), ask_config(&cli));
        match rx.recv().unwrap() {
            Ok(text) => assert!(text.contains("## Q")),
            Err(e) => panic!("generate failed: {e}"),
        }
    }
}