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
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
// A separate bin crate from the lib needs its own crate-root attr for
// `#[coverage(off)]` under nightly llvm-cov (see src/lib.rs for the lib's).
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

mod common;
mod deck;
mod doctor;
mod generate;
mod launch;
mod profile;
mod progress;
mod share;

use std::path::PathBuf;

use alix::config::{self, Config};
use anyhow::{Context, Result, bail};
use clap::{Args, Parser, Subcommand};
use launch::launch;

/// A learning tool built for understanding, not just remembering.
///
/// Without a subcommand, alix serves its web app: the in-browser deck
/// picker over your decks directory, or over the folder you name.
/// Manual: https://alix.study/book
#[derive(Parser)]
#[command(version, about)]
struct Cli {
    #[command(subcommand)]
    command: Option<Command>,

    #[command(flatten)]
    launch: LaunchArgs,
}

/// The bare `alix [dir]` launcher: everything is picked in the browser, so the
/// top level carries only what it takes to spin up the server itself.
#[derive(Args)]
struct LaunchArgs {
    /// A decks folder or a workspace to serve as this instance's own root:
    /// scoped to that folder, with its own progress and recent state inside it.
    /// Default: the configured decks directory with the global state.
    dir: Option<PathBuf>,

    /// Port to listen on (default: the `[serve]` config port, 7777).
    #[arg(long)]
    port: Option<u16>,

    /// Listen on all network interfaces so phones and tablets on the same
    /// network can reach it; generates and prints a pairing token (and QR).
    #[arg(long)]
    lan: bool,

    /// Pairing token required on `/api/*`. Defaults to a value auto-generated
    /// (and printed) for `--lan`.
    #[arg(long)]
    token: Option<String>,

    /// Cards a single sitting serves (default: the `[review] max_session`
    /// config key, else 10). Its new-card share is `[review]
    /// new_cards_percent`.
    #[arg(long)]
    session: Option<usize>,

    /// Path of the config file (default: platform config dir).
    #[arg(long)]
    config: Option<PathBuf>,

    /// Launch every profile at once, each on its own port and bound to the LAN.
    /// Runs in the foreground; Ctrl-C (or closing the window) stops them all.
    #[arg(long, conflicts_with_all = ["dir", "config"])]
    launch_all: bool,
}

#[derive(Subcommand)]
enum Command {
    /// Define and launch named per-person instances (their decks, port, adult/kids flavor).
    #[command(subcommand)]
    Profile(ProfileCommand),
    /// Check this setup's health, with a one-line fix per problem.
    ///
    /// Covers the config, the progress store, the decks folder, and the
    /// optional external CLIs. Add `--backends` to also probe the configured
    /// AI backend end to end (one real, tiny request).
    Doctor(DoctorArgs),
    /// Generate learning material with AI: a deck, a trace, or a workspace.
    ///
    /// A web page or file source becomes one deck. A directory source is
    /// explored first: a one-item plan becomes a deck, a bigger plan becomes
    /// a workspace (shown and confirmed before building). `--plan` previews,
    /// `--trace` authors a trace, and naming a deck that declares `trace:` in
    /// its frontmatter builds its checkpoints in place.
    Generate(GenerateArgs),
    /// Show progress statistics for a deck, a folder, or a workspace.
    ///
    /// The target is a path: a single deck file reports that deck; a folder
    /// or workspace reports every deck inside it, each against the store it
    /// actually uses. E.g. `alix stats spanish.md` or `alix stats
    /// ~/decks/flutter`.
    Stats(DeckArgs),
    /// List all cards with their state and due time (deck, folder, or workspace).
    ///
    /// The target is a path: a single deck file lists its cards; a folder or
    /// workspace lists every member deck's, grouped per deck.
    List(DeckArgs),
    /// Clear stored progress for a deck, a folder/workspace, a card, or everything.
    ///
    /// The target is a path: a single deck file clears that deck; a folder
    /// or workspace clears every deck inside it (cards, remediation cards,
    /// and mastered flags) after one confirmation. `--card` narrows to one
    /// card; `--all` wipes the whole store instead of a path.
    Reset(ResetArgs),
    /// Initialize, augment, or import decks.
    #[command(subcommand)]
    Deck(DeckAction),
    /// Create and grow workspaces.
    #[command(subcommand)]
    Workspace(WorkspaceAction),
    /// Share a deck, folder, or workspace: over magic-wormhole, or as a .zip.
    ///
    /// Either way, what travels is a staged copy without your personal state
    /// (progress, recent list, local pacing). The default sends through the
    /// `wormhole` binary. Tell the receiver the code it prints; `--zip`
    /// writes an archive to pass along however you like instead.
    Share(ShareArgs),
    /// Receive a shared deck or folder: by wormhole code, or from a .zip.
    ///
    /// A received deck lands in the decks directory (or `--workspace <dir>`);
    /// a received folder lands beside your other decks under its own name.
    /// Leaked personal files are stripped either way.
    Receive(ReceiveArgs),
    /// Show the configuration (key bindings) or create the config file.
    Config {
        /// Write a config file with the default bindings to edit.
        #[arg(long)]
        init: bool,
    },
}

#[derive(Subcommand)]
enum ProfileCommand {
    /// Create a profile: a named config for one person's instance.
    Add(ProfileAddArgs),
    /// List profiles (name, flavor, port, decks).
    List,
    /// Delete a profile's config.
    Remove(ProfileNameArgs),
    /// Set, show (no name), or clear (--clear) the default profile that bare `alix` launches.
    Default(ProfileDefaultArgs),
    /// Launch the named profile (bound to the LAN with its stored token).
    #[command(external_subcommand)]
    Launch(Vec<String>),
}

#[derive(Args)]
struct ProfileAddArgs {
    /// The profile name (also the config filename). Not add/list/remove/default.
    name: String,
    /// The decks folder this profile serves. Default: the configured decks dir.
    #[arg(long)]
    decks: Option<PathBuf>,
    /// The port this profile's server listens on. Default: the `[serve]` default (7777).
    #[arg(long)]
    port: Option<u16>,
    /// Serve the kids frontend for this profile (default: the adult frontend).
    #[arg(long, conflicts_with = "adult")]
    kids: bool,
    /// Serve the adult frontend for this profile (the default; explicit form).
    #[arg(long)]
    adult: bool,
}

#[derive(Args)]
struct ProfileNameArgs {
    name: String,
    /// Skip the confirmation prompt.
    #[arg(short = 'y', long)]
    yes: bool,
}

#[derive(Args)]
struct ProfileDefaultArgs {
    /// The profile to make default. Omit to print the current default.
    name: Option<String>,
    /// Clear the default (bare `alix` reverts to the global config).
    #[arg(long, conflicts_with = "name")]
    clear: bool,
}

#[derive(Args)]
struct DoctorArgs {
    /// What to check instead of the configured setup: a decks folder or
    /// workspace root (with its own store, like `alix <dir>` serves it), or a
    /// single deck file to lint in depth.
    dir: Option<PathBuf>,

    /// Also probe the configured AI backend end to end. This sends one real
    /// (tiny) request, the only reliable way to confirm login + reachability.
    #[arg(long)]
    backends: bool,

    /// Probe all four supported backends (one real request each).
    #[arg(long, conflicts_with = "backends")]
    all_backends: bool,

    /// Spot-check the configured model's exam grading against the hand-labeled
    /// calibration probes (a few real, costed calls, batched by strictness):
    /// does a wrong answer fail, does a correct one pass? A spot check, not a
    /// certification.
    #[arg(long)]
    grading: bool,

    /// Stamp reviewed source excerpts and rebase exact excerpts that moved
    /// uniquely within their current file.
    #[arg(long)]
    repair_source_locators: bool,

    /// Delete every backup (`*.bak`) file under the checked folder, after one
    /// confirmation. The backups are what `alix deck restore` swaps in.
    #[arg(long)]
    remove_backup_files: bool,

    /// Skip the backup-removal confirmation.
    #[arg(long, requires = "remove_backup_files")]
    yes: bool,

    /// Path of the config file (default: platform config dir).
    #[arg(long)]
    config: Option<PathBuf>,
}

/// The `alix workspace` subcommands: create and grow workspaces.
#[derive(Subcommand)]
enum WorkspaceAction {
    /// Initialize an empty workspace: a folder with an `alix.toml` and an
    /// `assets/` dir, no decks yet. Grow it with `alix generate … --workspace
    /// <dir>` or `alix deck import … --workspace <dir>`.
    Init(WorkspaceInitArgs),
    /// Reconcile frozen source-backed decks with their live sources. The first
    /// run stages an exact proposal; inspect it, then rerun with `--apply`.
    Update(WorkspaceUpdateArgs),
    /// Show, set, or clear this workspace's personal "ready by" deadline.
    Deadline(WorkspaceDeadlineArgs),
}

#[derive(Args)]
struct WorkspaceInitArgs {
    /// The folder to create (or to convert, when it exists without an alix.toml).
    dir: PathBuf,

    /// The workspace's display title (default: the folder name).
    #[arg(long)]
    title: Option<String>,
}

#[derive(Args)]
struct WorkspaceUpdateArgs {
    /// The workspace directory.
    dir: PathBuf,

    /// Publish the exact existing staged proposal without another AI call.
    #[arg(long, conflicts_with = "discard")]
    apply: bool,

    /// Delete the existing staged proposal without changing the workspace.
    #[arg(long, conflicts_with = "apply")]
    discard: bool,

    /// Path of the config file (default: platform config dir).
    #[arg(long)]
    config: Option<PathBuf>,
}

#[derive(Args)]
struct WorkspaceDeadlineArgs {
    /// The workspace directory.
    dir: PathBuf,
    /// A date (YYYY-MM-DD) to set, `clear` to remove; omit to show.
    date: Option<String>,
    /// Path of the config file (default: platform config dir).
    #[arg(long)]
    config: Option<PathBuf>,
}

#[derive(Args)]
struct ShareArgs {
    /// What to send: a deck file, a plain decks folder, or a workspace.
    path: PathBuf,

    /// Write a .zip archive instead of sending over wormhole: the offline
    /// fallback (mail it, put it on a stick).
    #[arg(long)]
    zip: bool,

    /// With `--zip`, where to write the archive: a file name, or a directory
    /// to put `<name>.zip` in (default: the current directory).
    #[arg(long, requires = "zip")]
    output: Option<PathBuf>,
}

#[derive(Args)]
struct ReceiveArgs {
    /// A wormhole code the sender read to you (e.g. `7-crossover-clockwork`),
    /// or a path to a `.zip` made by `alix share --zip`.
    #[arg(value_name = "CODE|ZIP")]
    code: String,

    /// Put a received DECK into this workspace instead of the decks directory.
    #[arg(long)]
    workspace: Option<PathBuf>,

    /// Overwrite an existing deck file of the same name (folders never
    /// overwrite; move the old one aside first).
    #[arg(long)]
    force: bool,
}

#[derive(Args)]
struct GenerateArgs {
    /// What to generate from: a web page URL, a local file, or a directory,
    /// or a deck that declares `trace:` in its frontmatter, whose checkpoints
    /// are then built in place.
    source: String,

    /// Public URL recorded as an additional `source:` (the workspace `source`
    /// for a generated workspace) for tutor context and exam grounding.
    #[arg(long, value_name = "URL")]
    source_url: Option<String>,

    /// The learning goal that scopes what is generated (default: understand
    /// the whole source).
    #[arg(long)]
    goal: Option<String>,

    /// Language for generated fronts, answers, choices, and notes.
    #[arg(long, value_name = "LANGUAGE")]
    language: Option<String>,

    /// Intended learner, used to set vocabulary, assumed knowledge, examples,
    /// and difficulty.
    #[arg(long, value_name = "AUDIENCE")]
    audience: Option<String>,

    /// Card shape for generated facts decks. Workspace trace items retain
    /// their predict-and-verify checkpoint shape.
    #[arg(long, value_enum)]
    card_style: Option<config::GenerateCardStyle>,

    /// Print the plan (directory source) or the trace suggestions (--trace)
    /// and stop: generate nothing.
    #[arg(long)]
    plan: bool,

    /// Author a trace over the source instead of facts decks: a short
    /// predict-and-verify walk over its shape, written as a trace deck.
    #[arg(long)]
    trace: bool,

    /// Force a single deck from a directory source (skip the plan pass).
    #[arg(long, conflicts_with = "trace")]
    deck: bool,

    /// The workspace this lands in: the build destination for a directory
    /// source (default: a folder under the decks dir), or the folder a single
    /// generated deck is written into.
    #[arg(long)]
    workspace: Option<PathBuf>,

    /// Single deck: output name (default: derived from the source). A `.md`
    /// extension is added if missing.
    #[arg(short, long)]
    output: Option<String>,

    /// Single deck: maximum number of cards (overrides the configured default).
    #[arg(long)]
    cards: Option<usize>,

    /// Single deck: run a second AI pass that reviews the draft and removes
    /// redundant cards (an extra call; also `generate.review` in the config).
    #[arg(long)]
    review: bool,

    /// Single deck: print it to stdout instead of writing a file.
    #[arg(long)]
    print: bool,

    /// Overwrite existing output (a deck file, or a non-empty workspace dir).
    #[arg(long)]
    force: bool,

    /// Workspace build: its display title (default: the folder name).
    #[arg(long)]
    title: Option<String>,

    /// Workspace build: use this image as the workspace icon instead of
    /// letting the model draw one. Copied into `assets/`.
    #[arg(long)]
    icon: Option<PathBuf>,

    /// Skip confirmations: the large-source pre-flight, and the
    /// workspace-build go-ahead.
    #[arg(short, long)]
    yes: bool,

    /// Path of the config file (default: platform config dir).
    #[arg(long)]
    config: Option<PathBuf>,
}

#[derive(Args)]
struct GenerateDeckArgs {
    /// The source to turn into a facts deck: a web page URL, or a local file or
    /// directory path.
    source: String,

    /// Output deck name (default: a slug derived from the URL). Written into
    /// the decks directory; a `.md` extension is added if missing.
    #[arg(short, long)]
    output: Option<String>,

    /// Maximum number of cards (overrides the configured default).
    #[arg(long)]
    cards: Option<usize>,

    /// Run a second Claude pass that reviews the draft and removes redundant
    /// cards (an extra call; can also be enabled with `generate.review`).
    #[arg(long)]
    review: bool,

    /// Print the generated deck to stdout instead of writing a file.
    #[arg(long)]
    print: bool,

    /// Overwrite the output file if it already exists.
    #[arg(long)]
    force: bool,

    /// Skip the pre-flight size confirmation for a large local source tree.
    #[arg(short, long)]
    yes: bool,

    /// Path of the config file (default: platform config dir).
    #[arg(long)]
    config: Option<PathBuf>,
}

/// The `alix deck` subcommands: create, augment, or validate a deck.
#[derive(Subcommand)]
enum DeckAction {
    /// Initialize a hand-authored Markdown file as an Alix deck.
    Init(DeckInitArgs),
    /// Copy a workspace deck and its shareable files into another workspace.
    Copy(DeckTransferArgs),
    /// Move a workspace deck, its shareable files, and its progress.
    Move(DeckMoveArgs),
    /// Augment an existing deck with Claude: multiple-choice distractors, or
    /// trivia notes. Augmentations are deliberate and persisted, so review stays
    /// instant and fully offline.
    Augment(AugmentArgs),
    /// Import an Anki TSV export into an alix deck.
    ///
    /// Expects tab-separated `front<TAB>back` lines.
    Import(ImportArgs),
    /// Remove a deck and everything that is its alone: the file, its review
    /// history, its frozen assets, its augmentations, and any backups.
    ///
    /// Total by design: nothing is backed up and this cannot be undone.
    Remove(DeckRemoveArgs),
    /// Swap a deck with its `.bak` backups (file, review history,
    /// augmentations), undoing the last overwrite.
    ///
    /// Nothing is destroyed: the swapped-away state becomes the new backup,
    /// so running it again swaps back.
    Restore(DeckRestoreArgs),
}

#[derive(Args)]
struct DeckRemoveArgs {
    /// The deck file to remove.
    deck: PathBuf,

    /// Skip the confirmation prompt.
    #[arg(short, long)]
    yes: bool,

    /// Progress store path override (default: resolved per deck).
    #[arg(long)]
    store: Option<PathBuf>,
}

#[derive(Args)]
struct DeckRestoreArgs {
    /// The deck file (or its former path) whose backups to swap in.
    deck: PathBuf,

    /// Progress store path override (default: resolved per deck).
    #[arg(long)]
    store: Option<PathBuf>,
}

#[derive(Args)]
struct DeckInitArgs {
    /// The Markdown file to initialize with stable deck and card IDs.
    deck: PathBuf,
}

#[derive(Args)]
struct DeckTransferArgs {
    /// An initialized direct member of an Alix workspace.
    deck: PathBuf,
    /// The destination Alix workspace.
    workspace: PathBuf,
}

#[derive(Args)]
struct DeckMoveArgs {
    /// An initialized direct member of an Alix workspace.
    deck: PathBuf,
    /// The destination Alix workspace.
    workspace: PathBuf,
    /// Skip the confirmation prompt.
    #[arg(short = 'y', long)]
    yes: bool,
}

#[derive(Args)]
struct AugmentArgs {
    /// The deck file to augment.
    deck: PathBuf,

    /// What to augment, mirroring the review concepts: `choices` (distractors),
    /// `notes` (trivia / mnemonics), `questions` (reworded phrasings rotated at
    /// review), or `order` (a graph of how the cards relate plus a suggested
    /// walk that puts foundations first). All are cached in the workspace's
    /// augmentation document, never written into the deck; review reads them.
    #[arg(long, value_enum)]
    target: AugmentTarget,

    /// Free-text guidance for *how* to augment, woven into the prompt (e.g.
    /// "use common misconceptions", "add a surprising historical fact").
    #[arg(long)]
    with: Option<String>,

    /// User-files root used to include virtual cards while augmenting. It does
    /// not change where workspace augmentation is written.
    #[arg(long)]
    store: Option<PathBuf>,

    /// Path of the config file (default: platform config dir).
    #[arg(long)]
    config: Option<PathBuf>,
}

/// What `alix deck augment` generates, named after the review concept it feeds.
#[derive(Clone, Copy, Debug, clap::ValueEnum)]
enum AugmentTarget {
    /// Multiple-choice distractors.
    Choices,
    /// Trivia / mnemonic notes, shown with the card's deck note on reveal.
    Notes,
    /// Reworded question variants, rotated at review time so the card can't be
    /// answered by recognizing one fixed wording. Plain (non-cloze) cards only.
    Questions,
    /// Key points: the load-bearing claims a card's answer makes, so Explain mode
    /// can check a reconstruction against them and derive the grade. Atomic
    /// answers (nothing to decompose) are skipped.
    Keypoints,
    /// A deck-level review order: a graph of how the cards relate plus a
    /// suggested walk, so review presents the cards foundations-first.
    /// `--with` steers the organizing principle (e.g. "by module and type
    /// dependency").
    #[value(name = "order")]
    Topology,
    /// A display-only reshape of a badly-shaped card: restructured front/answer/
    /// note and a suggested mode, applied at review without touching the deck.
    /// Plain (non-cloze) cards only.
    Format,
}

#[derive(Args)]
struct ImportArgs {
    /// The Anki TSV file to import (tab-separated `front<TAB>back` lines).
    file: PathBuf,

    /// Output deck name (default: a slug from the file name). Written into the
    /// decks directory; a `.md` extension is added if missing.
    #[arg(short, long)]
    output: Option<String>,

    /// The workspace folder to import the deck into (default: the decks dir).
    #[arg(long)]
    workspace: Option<PathBuf>,

    /// Print the deck to stdout instead of writing a file.
    #[arg(long)]
    print: bool,

    /// Overwrite the output file if it already exists.
    #[arg(long)]
    force: bool,

    /// Path of the config file (default: platform config dir).
    #[arg(long)]
    config: Option<PathBuf>,
}

#[derive(Args)]
struct DeckArgs {
    /// A path: one deck file (just that deck), or a folder/workspace
    /// (every deck inside it), e.g. `spanish.md` or `~/decks/flutter`.
    #[arg(value_name = "DECK|FOLDER|WORKSPACE")]
    target: PathBuf,

    /// State-root directory (default: resolved from the target).
    #[arg(long)]
    store: Option<PathBuf>,

    /// Path of the config file (default: platform config dir).
    #[arg(long)]
    config: Option<PathBuf>,
}

#[derive(Args)]
struct ResetArgs {
    /// What to clear, as a path: one deck file, or a folder/workspace
    /// (every deck inside it), e.g. `spanish.md` or `~/decks/flutter`.
    #[arg(value_name = "DECK|FOLDER|WORKSPACE")]
    target: Option<PathBuf>,

    /// Reset one card: its numeric id, or text matching its front (searched
    /// within the target's decks).
    #[arg(long)]
    card: Option<String>,

    /// Clear progress for every card in the store.
    #[arg(long, conflicts_with_all = ["target", "card"])]
    all: bool,

    /// Clear only ORPHANED progress: store keys matching no card or deck in the
    /// scanned decks (a stripped `<!-- id: -->` comment, a hand-deleted deck, a
    /// double-mint). Orphans are never auto-pruned (they are evidence), so this
    /// is the explicit opt-in. Scopes to a named folder/workspace, else the
    /// decks-dir root store.
    #[arg(long, conflicts_with_all = ["card", "all"])]
    orphans: bool,

    /// Skip the confirmation prompt (for scripts / test loops).
    #[arg(short = 'y', long)]
    yes: bool,

    /// User-files directory (default: resolved from the target, or the
    /// decks-dir user root for `--all`/`--card` with no target).
    #[arg(long)]
    store: Option<PathBuf>,

    /// Path of the config file (default: platform config dir).
    #[arg(long)]
    config: Option<PathBuf>,
}

fn main() -> Result<()> {
    let cli = Cli::parse();
    match cli.command {
        None => {
            if cli.launch.launch_all {
                profile::launch_all()
            } else if cli.launch.dir.is_none() && cli.launch.config.is_none() {
                match profile::resolve_default()? {
                    Some(name) => profile::launch_profile(&name),
                    None => launch(cli.launch),
                }
            } else {
                launch(cli.launch)
            }
        }
        Some(Command::Profile(cmd)) => profile::run(cmd),
        Some(Command::Stats(args)) => progress::stats(args),
        Some(Command::List(args)) => progress::list(args),
        Some(Command::Reset(args)) => progress::reset(args),
        Some(Command::Generate(args)) => generate::generate_cmd(args),
        Some(Command::Deck(action)) => match action {
            DeckAction::Init(args) => deck::init_cmd(args),
            DeckAction::Copy(args) => deck::copy_cmd(args),
            DeckAction::Move(args) => deck::move_cmd(args),
            DeckAction::Augment(args) => deck::augment_cmd(args),
            DeckAction::Import(args) => deck::import_cmd(args),
            DeckAction::Remove(args) => deck::remove_cmd(args),
            DeckAction::Restore(args) => deck::restore_cmd(args),
        },
        Some(Command::Workspace(action)) => match action {
            WorkspaceAction::Init(args) => deck::workspace_init_cmd(args),
            WorkspaceAction::Update(args) => deck::workspace_update_cmd(args),
            WorkspaceAction::Deadline(args) => deck::workspace_deadline_cmd(args),
        },
        Some(Command::Share(args)) => share::share_cmd(args),
        Some(Command::Receive(args)) => share::receive_cmd(args),
        Some(Command::Config { init }) => config_cmd(init),
        Some(Command::Doctor(args)) => doctor::doctor_cmd(args),
    }
}

fn config_cmd(init: bool) -> Result<()> {
    let path = config::default_config_path().context("cannot determine the config directory")?;

    if init {
        if path.exists() {
            bail!("{} already exists; edit it directly", path.display());
        }
        if let Some(dir) = path.parent() {
            std::fs::create_dir_all(dir)
                .with_context(|| format!("cannot create {}", dir.display()))?;
        }
        std::fs::write(&path, config::default_config_toml())
            .with_context(|| format!("cannot write {}", path.display()))?;
        println!("wrote {}", path.display());
        return Ok(());
    }

    if path.exists() {
        println!("config file: {}", path.display());
    } else {
        println!(
            "no config file at {}, using defaults; create one with \
             `alix config --init`",
            path.display()
        );
    }
    let config = Config::load(None)?;
    let keys = &config.keys;
    let show = |action: &str, list: &[config::KeyPattern]| {
        let keys: Vec<String> = list.iter().map(|p| p.to_string()).collect();
        println!("  {action:<9} {}", keys.join(", "));
    };
    println!("key bindings:");
    show("failed", &keys.failed);
    show("partly", &keys.partly);
    show("passed", &keys.passed);
    show("reveal", &keys.reveal);
    show("hint", &keys.hint);
    show("submit", &keys.submit);
    show("skip", &keys.skip);
    show("remove", &keys.remove);
    show("continue", &keys.cont);
    show("restart", &keys.restart);
    show("ask", &keys.ask);
    show("make_note", &keys.make_note);
    show("make_card", &keys.make_card);
    show("quit", &keys.quit);
    println!("browse bindings (first/last fixed: g/G/Home/End):");
    show("next", &config.browse.next);
    show("prev", &config.browse.prev);
    show("remove", &config.browse.remove);
    show("quit", &config.browse.quit);
    println!("ask:");
    println!("  command     {}", config.ask.command);
    println!(
        "  model       {}",
        config.ask.model.as_deref().unwrap_or("(CLI default)")
    );
    println!("  timeout     {}s", config.ask.timeout_secs);
    println!("  permission  {}", config.ask.permission_mode);
    println!("  tools       {}", config.ask.allowed_tools.join(", "));
    println!("generate:");
    println!(
        "  model       {}",
        config
            .generate
            .model
            .as_deref()
            .unwrap_or("(ask / CLI default)")
    );
    println!("  timeout     {}s", config.generate.timeout_secs);
    println!("  idle        {}s", config.generate.idle_timeout_secs);
    println!("  max_cards   {}", config.generate.max_cards);
    println!(
        "  language    {}",
        config.generate.language.as_deref().unwrap_or("(source)")
    );
    println!(
        "  audience    {}",
        config.generate.audience.as_deref().unwrap_or("(general)")
    );
    println!("  card_style  {}", config.generate.card_style.as_str());
    println!("  review      {}", config.generate.review);
    Ok(())
}