alix 0.7.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
use std::sync::Arc;

use alix::{
    assemble::{self, VIRTUAL_LINE_BASE, synthesize_virtual},
    augment::{self, AugmentCache},
    augment_ai,
    card::Card,
    config::Config,
    generate, import, library, parser, workspace,
};
use anyhow::{Context, Result, bail};
use chrono::NaiveDate;

use crate::{
    AugmentArgs, AugmentTarget, DeckInitArgs, DeckMoveArgs, DeckRemoveArgs, DeckRestoreArgs,
    DeckTransferArgs, ImportArgs, WorkspaceDeadlineArgs, WorkspaceInitArgs, WorkspaceUpdateArgs,
    common::{confirm, deck_out_dir, one_line, store_for, truncate},
};

pub(crate) fn remove_cmd(args: DeckRemoveArgs) -> Result<()> {
    let deck = &args.deck;
    if deck.is_dir() {
        bail!(
            "{} is a folder; deck remove takes a single deck file",
            deck.display()
        );
    }
    if !deck.is_file() {
        bail!("no deck at {}", deck.display());
    }
    let root = removal_store_root(deck, args.store.as_deref())?;
    let store = alix::state::open_store(deck, &root)?;
    let preview = library::removal_preview(deck, &store);

    println!("Removing {}:", deck.display());
    let since = preview
        .earliest_review_ms
        .and_then(|ms| chrono::DateTime::from_timestamp_millis(ms as i64))
        .map(|t| format!(", reviewed since {}", t.format("%Y-%m-%d")))
        .unwrap_or_default();
    println!(
        "  {} card(s) with recorded progress{since}",
        preview.cards_with_progress
    );
    for file in &preview.files {
        println!("  {}", file.display());
    }
    for dir in &preview.directories {
        println!("  {}{}", dir.display(), std::path::MAIN_SEPARATOR);
    }
    if !preview.dependents.is_empty() {
        println!(
            "warning: required by {}; they will unlock, not break",
            preview.dependents.join(", ")
        );
    }
    if !confirm("Remove permanently? This cannot be undone.", args.yes)? {
        println!("Removal cancelled.");
        return Ok(());
    }
    let report = library::remove_deck(deck, &store)?;
    println!("Removed {} file(s); nothing was backed up.", {
        report.removed.len()
    });
    Ok(())
}

pub(crate) fn restore_cmd(args: DeckRestoreArgs) -> Result<()> {
    let deck = &args.deck;
    let root = removal_store_root(deck, args.store.as_deref())?;
    let report = library::restore_deck(deck, &root)?;
    let describe = |swapped: bool| {
        if swapped {
            "swapped"
        } else {
            "no backup found"
        }
    };
    println!(
        "Restored {} (review history: {}; augmentations: {}).",
        deck.display(),
        describe(report.progress),
        describe(report.augment)
    );
    println!("The previous state is the new backup; restore again to swap back.");
    Ok(())
}

fn removal_store_root(
    deck: &std::path::Path,
    cli_override: Option<&std::path::Path>,
) -> Result<std::path::PathBuf> {
    assemble::store_path_for(std::slice::from_ref(&deck.to_path_buf()), cli_override)
        .or_else(|| {
            Config::load(None)
                .ok()
                .and_then(|config| config.decks_dir())
                .map(|dir| workspace::root_store_path(&dir))
        })
        .or_else(alix::store::default_store_path)
        .context("cannot determine the progress store for this deck")
}

pub(crate) fn workspace_update_cmd(args: WorkspaceUpdateArgs) -> Result<()> {
    if args.discard {
        if alix::workspace_update::discard(&args.dir)? {
            println!(
                "Discarded {}.",
                alix::workspace_update::staging_path(
                    &args.dir.canonicalize().unwrap_or_else(|_| args.dir.clone())
                )
                .display()
            );
        } else {
            println!("No staged workspace update exists.");
        }
        return Ok(());
    }
    if args.apply {
        let report = alix::workspace_update::apply(&args.dir)?;
        print_workspace_update_report("Applied", &report);
        return Ok(());
    }

    let config = Config::load(args.config.as_deref())?;
    eprintln!(
        "Reconciling {} against its live sources (one AI call per source-backed deck)…",
        args.dir.display()
    );
    let report = alix::workspace_update::stage(&args.dir, &config.generate, &config.ask)?;
    print_workspace_update_report("Staged", &report);
    println!(
        "Review the exact proposal at {}. Apply it with `alix workspace update {} --apply`, or discard it with `--discard`.",
        report.staging.display(),
        args.dir.display()
    );
    Ok(())
}

fn print_workspace_update_report(action: &str, report: &alix::workspace_update::UpdateReport) {
    let retained = report.decks.iter().map(|deck| deck.retained).sum::<usize>();
    let retired = report.decks.iter().map(|deck| deck.retired).sum::<usize>();
    let added = report.decks.iter().map(|deck| deck.added).sum::<usize>();
    println!(
        "{action} {} deck(s): {retained} retained, {retired} retired, {added} new.",
        report.decks.len()
    );
    for deck in &report.decks {
        println!(
            "  {}: {} retained, {} retired, {} new",
            deck.path.display(),
            deck.retained,
            deck.retired,
            deck.added
        );
    }
    for path in &report.live_only {
        println!(
            "  {}: no source citations, nothing frozen to reconcile; left as is",
            path.display()
        );
    }
}

pub(crate) fn init_cmd(args: DeckInitArgs) -> Result<()> {
    let outcome = alix::assets::initialize(&args.deck)?;
    println!(
        "Initialized {} ({} card ids assigned)",
        args.deck.display(),
        outcome.stamp.minted_cards.len()
    );
    Ok(())
}

pub(crate) fn copy_cmd(args: DeckTransferArgs) -> Result<()> {
    let report = alix::deck_transfer::transfer(
        &args.deck,
        &args.workspace,
        alix::deck_transfer::TransferMode::Copy,
    )?;
    print_transfer_report("Copied", &report);
    Ok(())
}

pub(crate) fn move_cmd(args: DeckMoveArgs) -> Result<()> {
    if !confirm(
        &format!(
            "Move {} to {} and remove the source?",
            args.deck.display(),
            args.workspace.display()
        ),
        args.yes,
    )? {
        println!("Move cancelled.");
        return Ok(());
    }
    let report = alix::deck_transfer::transfer(
        &args.deck,
        &args.workspace,
        alix::deck_transfer::TransferMode::Move,
    )?;
    print_transfer_report("Moved", &report);
    Ok(())
}

fn print_transfer_report(action: &str, report: &alix::deck_transfer::TransferReport) {
    println!(
        "{action} {} to {}.",
        report.source.display(),
        report.destination.display()
    );
    println!(
        "  {} asset file(s), augmentation: {}, progress moved: {}",
        report.assets,
        if report.augmentation { "yes" } else { "no" },
        if report.progress { "yes" } else { "no" }
    );
    for path in &report.leftovers {
        eprintln!("warning: source cleanup left {}", path.display());
    }
}

/// Foreground: any Claude error surfaces here, not mid-review.
pub(crate) fn augment_cmd(args: AugmentArgs) -> Result<()> {
    let config = Config::load(args.config.as_deref())?;
    // Must stamp before the cache is keyed by `Card::id`: unstamped cards all
    // hash to id 0, collapsing the cache and orphaning the spend.
    let deck = assemble::stamp_and_load_deck(&args.deck)?;
    let ask_cfg = augment_ai::run_config(&config.ai, &config.ask);
    let guidance = args.with.as_deref();

    let mut cache = AugmentCache::open_for_deck(&deck)?;
    let cache_path = cache.path().to_path_buf();
    let fp_by_id: std::collections::HashMap<String, u64> = deck
        .cards
        .iter()
        .filter_map(|card| card.id().map(|id| (id, card.content_fingerprint)))
        .collect();

    let what = match args.target {
        AugmentTarget::Choices => "multiple-choice distractors",
        AugmentTarget::Notes => "trivia / mnemonic notes",
        AugmentTarget::Questions => "reworded question variants",
        AugmentTarget::Keypoints => "answer key points",
        AugmentTarget::Topology => "a review order",
        AugmentTarget::Format => "card formatting",
    };
    let model = config
        .ai
        .model
        .as_deref()
        .or(config.ask.model.as_deref())
        .unwrap_or("the default model");
    eprintln!(
        "Generating {what} for \"{}\" with Claude ({model}) — one batched call, \
         this can take a moment…",
        deck.subject
    );

    let (made, total, kind) = match args.target {
        AugmentTarget::Choices => {
            let items = warm_items(&deck.cards);
            if items.is_empty() {
                bail!("the deck has no cards to augment");
            }
            let total = items.len();
            let map =
                augment_ai::generate(&items, config.ai.distractor_count, guidance, &ask_cfg, None)?;
            for (id, distractors) in &map {
                if let Some(&fingerprint) = fp_by_id.get(id) {
                    cache.set_distractors(id, distractors.clone(), fingerprint);
                }
            }
            (map.len(), total, "distractors")
        }
        AugmentTarget::Notes => {
            let items = warm_items(&deck.cards);
            if items.is_empty() {
                bail!("the deck has no cards to augment");
            }
            let total = items.len();
            let map = augment_ai::generate_notes(&items, guidance, &ask_cfg, None)?;
            for (id, note) in &map {
                if let Some(&fingerprint) = fp_by_id.get(id) {
                    cache.set_note(id, note.clone(), fingerprint);
                }
            }
            (map.len(), total, "notes")
        }
        AugmentTarget::Questions => {
            // Cloze cards are excluded: their front is the title, not a
            // question to reword.
            let plain: Vec<Card> = deck
                .cards
                .iter()
                .filter(|c| c.hash_lines.is_none())
                .cloned()
                .collect();
            let items = warm_items(&plain);
            if items.is_empty() {
                bail!("the deck has no plain (non-cloze) cards to add question variants to");
            }
            let total = items.len();
            let map = augment_ai::generate_variants(
                &items,
                config.ai.variant_count,
                guidance,
                &ask_cfg,
                None,
            )?;
            for (id, variants) in &map {
                if let Some(&fingerprint) = fp_by_id.get(id) {
                    cache.set_variants(id, variants.clone(), fingerprint);
                }
            }
            (map.len(), total, "question variants")
        }
        AugmentTarget::Keypoints => {
            let items = warm_items(&deck.cards);
            if items.is_empty() {
                bail!("the deck has no cards to break into key points");
            }
            let total = items.len();
            let map = augment_ai::generate_keypoints(
                &items,
                config.ai.keypoint_count,
                guidance,
                &ask_cfg,
                None,
            )?;
            for (id, keypoints) in &map {
                if let Some(&fingerprint) = fp_by_id.get(id) {
                    cache.set_keypoints(id, keypoints.clone(), fingerprint);
                }
            }
            (map.len(), total, "key points")
        }
        AugmentTarget::Topology => {
            let items = warm_items(&deck.cards);
            if items.is_empty() {
                bail!("the deck has no cards to build an order over");
            }
            let total = items.len();
            let deck_token = deck.deck_token.clone().unwrap_or_default();
            let topo =
                augment_ai::generate_topology(&items, guidance, &deck_token, &ask_cfg, None)?;
            print_topology(&topo, &deck.cards);
            let walked = topo.walk.len();
            cache.add_topology(topo);
            // Scoped to this deck: the cache may be shared with other decks.
            let deck_tokens: std::collections::HashSet<String> =
                deck.deck_token.iter().cloned().collect();
            let n = cache.topologies_for(&deck_tokens).len();
            println!(
                "({n} order{} stored for this deck)",
                if n == 1 { "" } else { "s" }
            );
            (walked, total, "a review order")
        }
        AugmentTarget::Format => {
            let store = store_for(
                std::slice::from_ref(&args.deck),
                args.store.clone(),
                &config,
            )?;
            // Cloze, promoted, and retired cards are excluded (mirrors the
            // review's injection filters), so a card is never formatted twice
            // or after resting.
            let subject: Arc<str> = Arc::from(deck.subject.as_str());
            let deck_ids: std::collections::HashSet<String> =
                deck.cards.iter().filter_map(Card::id).collect();
            let retire_after_days = config
                .review
                .for_workspace(&alix::workspace::content_root(&deck.path))
                .retire_after_days;
            let mut plain: Vec<Card> = deck
                .cards
                .iter()
                .filter(|c| c.hash_lines.is_none())
                .cloned()
                .collect();
            for (k, vc) in store
                .virtual_cards_for(deck.deck_token.as_deref().unwrap_or_default())
                .into_iter()
                .filter(|v| !deck_ids.contains(&v.id))
                .filter(|v| !alix::session::is_retired_id(&v.id, &store, retire_after_days))
                .enumerate()
            {
                if let Some(card) = synthesize_virtual(vc, &subject, VIRTUAL_LINE_BASE + k)
                    && card.hash_lines.is_none()
                {
                    plain.push(card);
                }
            }
            let items = warm_items(&plain);
            if items.is_empty() {
                bail!("the deck has no plain (non-cloze) cards to format");
            }
            let total = items.len();
            let map = augment_ai::generate_format(&items, guidance, &ask_cfg, None)?;
            let format_fp_by_id: std::collections::HashMap<String, u64> = plain
                .iter()
                .filter_map(|card| card.id().map(|id| (id, card.content_fingerprint)))
                .collect();
            for (id, fmt) in &map {
                if let Some(&fingerprint) = format_fp_by_id.get(id) {
                    cache.set_format(id, fmt.clone(), fingerprint);
                }
            }
            (map.len(), total, "card formats")
        }
    };
    cache.save()?;

    println!(
        "augmented {made} of {total} cards with {kind} → {}",
        cache_path.display()
    );
    Ok(())
}

/// Builds the per-card generation input from `cards`.
fn warm_items(cards: &[Card]) -> Vec<augment::WarmItem> {
    cards.iter().map(augment::WarmItem::from_card).collect()
}

fn print_topology(topo: &augment::Topology, cards: &[Card]) {
    let fronts: std::collections::HashMap<String, String> = cards
        .iter()
        .filter_map(|c| Some((c.id()?, truncate(&one_line(&c.front), 72))))
        .collect();
    let unknown = "<card not in deck>".to_string();

    println!(
        "\norder '{}': {}\n({} cards walked, {} edges)\n",
        topo.name,
        topo.principle,
        topo.walk.len(),
        topo.edges.len()
    );
    let mut prev: Option<&str> = None;
    for (i, id) in topo.walk.iter().enumerate() {
        let front = fronts.get(id).unwrap_or(&unknown);
        match prev {
            None => println!("{:>3}. {front}", i + 1),
            Some(p) => {
                let why = topo
                    .edges
                    .iter()
                    .find(|e| e.from == p && e.to.as_str() == id.as_str())
                    .map(|e| e.label.as_str())
                    .unwrap_or("-");
                println!("{:>3}. ↳ [{why}]  {front}", i + 1);
            }
        }
        prev = Some(id.as_str());
    }
    println!();
}

pub(crate) fn workspace_init_cmd(args: WorkspaceInitArgs) -> Result<()> {
    if workspace::has_manifest(&args.dir) {
        bail!("{} is already a workspace", args.dir.display());
    }
    std::fs::create_dir_all(args.dir.join(workspace::DECKS))
        .and_then(|()| std::fs::create_dir_all(args.dir.join("assets")))
        .with_context(|| format!("cannot create {}", args.dir.display()))?;
    let title = match &args.title {
        Some(t) => t.clone(),
        None => args
            .dir
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("workspace")
            .to_string(),
    };
    // Section headers stay uncommented: a key uncommented outside its table
    // would silently be ignored by the lenient parser.
    let manifest = format!(
        "# This workspace's shared manifest — it travels when the folder is shared.\n\
         \n\
         title = {title:?}\n\
         \n\
         # description = \"one line shown under the title in the picker\"\n\
         # icon = \"assets/icon.svg\"     # picker emblem (svg/png/jpg/webp); default: assets/icon.*\n\
         \n\
         # Deck directives every member deck inherits (a deck's own line wins):\n\
         \n\
         [defaults]\n\
         \n\
         # reveal = \"flip\"              # flip | cloze | line\n\
         # order = \"scheduled\"          # scheduled | sequential\n"
    );
    let workspace_files = alix::workspace::WorkspaceFiles::new(&args.dir);
    std::fs::write(workspace_files.manifest(), manifest)
        .with_context(|| format!("cannot write {}/alix.toml", args.dir.display()))?;
    let local = "# Personal pacing for THIS workspace — never shared (`alix share` leaves it\n\
         # home). Uncomment a key to override your global [review] config here.\n\
         \n\
         [review]\n\
         \n\
         # retention = 0.9              # FSRS target recall probability (0.70–0.99)\n\
         # retire_after = \"1y\"          # a card rests at this interval (\"never\" disables)\n\
         # acquire_cooldown = \"5m\"      # settle gap before a new card's first quiz (\"90s\", \"0\" = none)\n\
         # max_session = 10             # cards a single sitting serves\n\
         # new_cards_percent = 30       # new-card share of max_session (the rest are due cards)\n\
         # deadline = \"2026-09-01\"     # make me ready by this date (picker readout + drilling ramp)\n\
         # deadline_ramp = \"14d\"       # how early the pre-deadline retention ramp starts (\"2w\"; \"0\" = cap only)\n";
    std::fs::write(
        alix::state::UserFiles::new(&args.dir).local_manifest(),
        local,
    )
    .with_context(|| format!("cannot write {}/alix.local.toml", args.dir.display()))?;
    println!(
        "Initialized {} — alix.toml (shared manifest) and alix.local.toml (your\n\
         personal pacing, never shared) document their keys inline. Add decks:\n\
         alix generate <source> --workspace {}   or   alix deck import <file.tsv> --workspace {}",
        args.dir.display(),
        args.dir.display(),
        args.dir.display(),
    );
    Ok(())
}

pub(crate) fn import_cmd(args: ImportArgs) -> Result<()> {
    let config = Config::load(args.config.as_deref())?;
    let tsv = std::fs::read_to_string(&args.file)
        .with_context(|| format!("cannot read {}", args.file.display()))?;
    let text = import::tsv_to_deck(&tsv)?;

    // The file name is part of every card's identity hash, so parse against the
    // final name.
    let name = match &args.output {
        Some(name) => name.clone(),
        None => generate::deck_name(&args.file.to_string_lossy()),
    };
    let name = if name.ends_with(".md") {
        name
    } else {
        format!("{name}.md")
    };

    if args.print {
        print!("{text}");
        if !text.ends_with('\n') {
            println!();
        }
        match parser::parse_str(&name, &text) {
            Ok(cards) => eprintln!("({} cards, not written; --print)", cards.len()),
            Err(e) => eprintln!("(warning: does not parse yet: {e})"),
        }
        return Ok(());
    }

    let dir = deck_out_dir(args.workspace.as_deref(), &config)?;
    let target = dir.join(&name);
    if target.exists() {
        if !args.force {
            bail!(
                "{} already exists; pass --force to overwrite",
                target.display()
            );
        }
        let mut store = store_for(std::slice::from_ref(&target), None, &config)?;
        let report = library::replace_deck(&dir, &name, &text, &mut store)?;
        println!(
            "Replaced {}: {} cards, wiped progress for {} card(s).",
            target.display(),
            report.minted,
            report.wiped_cards
        );
        return Ok(());
    }
    let placed = library::place_deck(&dir, &name, &text)?;
    match placed.parse_error {
        None => {
            println!(
                "Imported {} cards into {}",
                placed.cards,
                placed.path.display()
            );
            Ok(())
        }
        // Saved, but not yet valid: tell the user exactly what to fix.
        Some(e) => bail!(
            "Saved the deck to {}, but it does not parse yet:\n  {e}\n\
             Fix that line and run `alix doctor {}`.",
            placed.path.display(),
            placed.path.display()
        ),
    }
}

pub(crate) fn workspace_deadline_cmd(args: WorkspaceDeadlineArgs) -> Result<()> {
    let dir = &args.dir;
    // A deadline only has a product surface inside a real workspace, so a
    // plain folder errors here rather than silently accepting a setting it'd
    // ignore.
    if !workspace::is_workspace(dir) {
        bail!(
            "{} is not a workspace; make it one first: alix workspace init {}",
            dir.display(),
            dir.display()
        );
    }
    match args.date.as_deref() {
        None => {
            let review = Config::load(args.config.as_deref())?
                .review
                .for_workspace(dir);
            match review.deadline {
                Some(d) => {
                    let days = (d - alix::time::local_date(alix::time::now_ms())).num_days();
                    if days < 0 {
                        let past = -days;
                        let unit = if past == 1 { "day" } else { "days" };
                        println!("{d} (was due {past} {unit} ago)");
                    } else {
                        let unit = if days == 1 { "day" } else { "days" };
                        println!("{d} ({days} {unit} left)");
                    }
                }
                None => println!(
                    "no deadline set (set one: alix workspace deadline {} 2026-09-01)",
                    dir.display()
                ),
            }
        }
        Some("clear") => workspace::set_deadline(dir, None)?,
        Some(s) => {
            let date = NaiveDate::parse_from_str(s, "%Y-%m-%d").map_err(|_| {
                anyhow::anyhow!("invalid date {s:?}: expected YYYY-MM-DD (or \"clear\")")
            })?;
            workspace::set_deadline(dir, Some(date))?;
        }
    }
    Ok(())
}