prikk 0.31.0

A standalone distributed version control system built around block-oriented patch theory.
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
#![forbid(unsafe_code)]

//! Prikk command-line entry point.
//!
//! The CLI exposes minimal repository layout commands, active WAL status, node-addressed worktree
//! commit authoring, explicit non-default branch genesis, tag publication, bundle export/import,
//! sync negotiation and tag adoption, deterministic arbitrary-span text edit generation,
//! read-only inverse planning, rollback preview, rollback draft append/verification, sealed rollback classification,
//! supported patch replay planning/materialization, explicit patch deletion planning, a local
//! no-audit seal scaffold, read-only history inspection, checkout planning, conservative snapshot
//! materialization, read-only worktree status, minimal publication trust setup, read-only merge
//! evidence and merge plan review, confluent merge execution, repository verification, doctor
//! diagnostics, and stale lock/index maintenance.
//! Multi-operation text diff minimization, full patch algebra, and audit plugins remain later increments.

use std::path::PathBuf;
use std::process::ExitCode;

// RFC 121 §2.1: shadows the prelude's `println!`/`print!` with the closed-pipe-safe versions in
// `stdout.rs` -- see that module's own doc comment.
use stdout::println;

mod arg_scan;
mod args;
mod branch;
mod bundle;
mod commands;
mod compact;
mod durable_output;
mod merge;
mod output;
mod seal;
mod stdout;
mod sync;
mod tag;
mod unlock;
mod verify_verdict;

use arg_scan::{SetOnce, flag_value, unknown_argument};
use args::{
    CheckoutMode, MergeEvidenceTargetArg, VerifyOutputFormat, current_dir, parse_checkout_args,
    parse_commit_args, parse_doctor_args, parse_inverse_plan_args, parse_log_args,
    parse_merge_evidence_args, parse_merge_plan_args, parse_rollback_draft_args,
    parse_rollback_draft_verify_args, parse_rollback_preview_args, parse_verify_args,
    parse_worktree_status_args,
};
use commands::CliError;
use output::{
    print_active_session_repairs, print_checkout_plan, print_command_help, print_doctor_report,
    print_help, print_history, print_merge_evidence, print_merge_plan, print_patch_deletion_plan,
    print_patch_inverse_plan, print_patch_materialization_report, print_patch_replay_plan,
    print_rollback_draft_report, print_rollback_draft_verification, print_rollback_preview_plan,
    print_snapshot_checkout_plan, print_snapshot_materialization_report, print_verify_report,
    print_verify_report_json, print_worktree_status,
};
use prikk_store::{
    ActiveRefMetadata, ActiveSessionRepairStatus, DEFAULT_ACTIVE_NAME, DEFAULT_ACTIVE_PATCH_LIMIT,
    DoctorRepairOptions, Ed25519AuthorSigner, Ed25519MaintainerSigner, MergeEvidenceTarget,
    RefStore, RepositoryLayout, VerifyOptions, Wal, WorktreePatchCommitOptions,
    add_trusted_maintainer, append_rollback_draft, commit_worktree_changes_signed,
    doctor_repository, list_received_pointers, load_received_ref_history, load_ref_history,
    materialize_patch_checkout, materialize_patch_checkout_with_deletions,
    materialize_snapshot_checkout, plan_patch_checkout_deletions, prepare_checkout_plan,
    prepare_merge_evidence, prepare_merge_plan, prepare_patch_inverse_plan,
    prepare_patch_replay_plan, prepare_rollback_preview, prepare_snapshot_checkout_plan,
    read_active_ref_metadata, remove_trusted_maintainer, repair_repository,
    verify_active_rollback_draft, verify_repository_with_options, worktree_status,
};

const VERSION: &str = env!("CARGO_PKG_VERSION");

pub(crate) fn open_repository(
    root: impl Into<PathBuf>,
) -> std::result::Result<RepositoryLayout, String> {
    RepositoryLayout::open(root).map_err(|err| err.to_string())
}

fn main() -> ExitCode {
    match run() {
        Ok(()) => ExitCode::SUCCESS,
        Err(err) => {
            eprintln!("error: {}", err.message());
            ExitCode::from(err.exit_code())
        }
    }
}

fn run() -> std::result::Result<(), CliError> {
    let mut args = std::env::args().skip(1);
    match args.next().as_deref() {
        None | Some("--help") | Some("-h") => {
            print_help(VERSION);
            Ok(())
        }
        Some("--version") | Some("-V") => {
            println!("prikk {VERSION}");
            Ok(())
        }
        Some(name) => match commands::find(name) {
            Some(command) => {
                let args: Vec<String> = args.collect();
                // RFC 121 §2.5 v2 amendment §2: round 3's argument hygiene made every parser refuse
                // an unrecognized flag, which now includes `--help`/`-h` -- so this must be
                // recognized *before* the command's own parser ever sees it, not added as a 27th
                // arm to those parsers (the same "one absence, not many places" reasoning round 3
                // itself was built on). Recognized anywhere in the argument list, not only as the
                // first token (matching `git`/`cargo`'s own convention) -- `COMMANDS` is a flat
                // table of top-level names, so this does not distinguish `bundle --help` from
                // `bundle export --help`; both print `bundle`'s full help text (`print_command_help`'s
                // own doc comment).
                if args.iter().any(|arg| arg == "--help" || arg == "-h") {
                    print_command_help(command);
                    Ok(())
                } else {
                    (command.run)(args)
                }
            }
            // RFC 121 §6a: an unrecognized command name is detected before any repository work
            // begins, the exact shape §1's `Usage` variant exists for -- not `.into()`'s default
            // `Failure`, which every other bare error in this file still gets until the argument-
            // hygiene increment (`command-discovery-handoff-v1.md`'s sibling) adds the rest.
            None => Err(CliError::Usage(format!("unknown command: {name}"))),
        },
    }
}

fn run_init(path: Option<String>) -> std::result::Result<(), CliError> {
    let root = match path {
        Some(path) => PathBuf::from(path),
        None => current_dir()?,
    };
    RepositoryLayout::init(root.clone()).map_err(|err| err.to_string())?;
    println!(
        "initialized Prikk repository at {}",
        root.join(".prikk").display()
    );
    Ok(())
}

fn run_commit(args: Vec<String>) -> std::result::Result<(), CliError> {
    let args = parse_commit_args(args)?;
    let root = current_dir()?;
    let layout = open_repository(root)?;
    layout
        .require_current_format()
        .map_err(|err| err.to_string())?;
    let thresholds = ActivePatchThresholds::from_env()?;
    let options = if args.text_edits {
        WorktreePatchCommitOptions::prefer_text_edits()
    } else {
        WorktreePatchCommitOptions::file_level()
    }
    .with_active_patch_limit(thresholds.limit);
    let signer = author_signer_from_env()?;
    let report =
        commit_worktree_changes_signed(&layout, &args.ref_name, &args.message, options, &signer)
            .map_err(|err| err.to_string())?;
    println!("recorded worktree patch in active WAL");
    println!("baseline ref: {}", report.ref_name);
    println!("patch id: {}", report.patch_id);
    println!("WAL sequence: {}", report.wal_sequence);
    println!("operations: {}", report.operation_count);
    println!("referenced blobs: {}", report.referenced_blob_count);
    println!("text edits: {}", report.text_edit_count);
    for change in &report.changes {
        println!("  {} {}", change.operation.as_str(), change.path);
    }
    println!(
        "note: multi-operation text diff minimization, patch algebra, rename detection, and audit \
         plugins remain later increments"
    );
    // RFC 123 §4 Option C-revised: `-m`'s message is validated (required, non-empty) and then
    // discarded -- `node_authoring.rs` binds it as `_message` and goes no further. Silently
    // discarding required user input is worse than saying so; this is the interim mitigation while
    // Option A (a schema-3 `message` field) is built. Printed every commit, not once, matching the
    // existing note above.
    println!(
        "note: the message is validated but not stored -- it will not appear in `prikk log`; \
         persisting it is a later increment"
    );
    Ok(())
}

fn run_seal(args: Vec<String>) -> std::result::Result<(), CliError> {
    // AUD-10: parse before building the signer, so a bad argument is refused before an unrelated
    // missing-signer environment is ever consulted.
    let ref_name = seal::parse_seal_args(args)?;
    let root = current_dir()?;
    let signer = maintainer_signer_from_env()?;
    let result = seal::run_seal(root, ref_name, &signer)?;
    println!("sealed active WAL into block");
    println!("patches: {}", result.patch_count);
    println!("block id: {}", result.block_id);
    println!("{} RefState: {}", result.ref_name, result.ref_state_id);
    println!("note: audit plugins remain later PRs");
    Ok(())
}

fn run_merge(args: Vec<String>) -> std::result::Result<(), CliError> {
    // AUD-10: parse before building the signer, so a bad argument is refused before an unrelated
    // missing-signer environment is ever consulted.
    let args = crate::args::parse_merge_execute_args(args)?;
    let signer = maintainer_signer_from_env()?;
    let report = merge::run_merge(args, &signer)?;
    println!("merged {} into {}", report.from_ref, report.into_ref);
    println!("baseline block: {}", report.baseline_block_id);
    println!("parent block: {}", report.parent_block_id);
    println!("adopted target block: {}", report.adopted_target_block_id);
    println!("adopted patches: {}", report.adopted_patch_ids.len());
    for patch_id in &report.adopted_patch_ids {
        println!("  {patch_id}");
    }
    println!("block id: {}", report.block_id);
    println!("{} RefState: {}", report.into_ref, report.ref_state_id);
    Ok(())
}

fn run_branch(args: Vec<String>) -> std::result::Result<(), CliError> {
    let root = current_dir()?;
    branch::run_branch(root, args)?;
    Ok(())
}

fn run_tag(args: Vec<String>) -> std::result::Result<(), CliError> {
    let root = current_dir()?;
    tag::run_tag(root, args)?;
    Ok(())
}

fn run_bundle(args: Vec<String>) -> std::result::Result<(), CliError> {
    let root = current_dir()?;
    bundle::run_bundle(root, args)?;
    Ok(())
}

fn run_sync(args: Vec<String>) -> std::result::Result<(), CliError> {
    let root = current_dir()?;
    sync::run_sync(root, args)?;
    Ok(())
}

fn run_unlock(args: Vec<String>) -> std::result::Result<(), CliError> {
    let root = current_dir()?;
    unlock::run_unlock(root, args)?;
    Ok(())
}

fn run_compact(args: Vec<String>) -> std::result::Result<(), CliError> {
    let root = current_dir()?;
    compact::run_compact(root, args)?;
    Ok(())
}

fn run_trust(args: Vec<String>) -> std::result::Result<(), CliError> {
    let mut args = args.into_iter();
    match (args.next().as_deref(), args.next().as_deref()) {
        (Some("maintainer"), Some("add")) => {
            let mut key_id = None;
            let mut public_key = None;
            while let Some(arg) = args.next() {
                match arg.as_str() {
                    "--key-id" => {
                        let value = flag_value(&mut args, "--key-id")?;
                        key_id.set_once("--key-id", value)?;
                    }
                    "--public-key" => {
                        let value = flag_value(&mut args, "--public-key")?;
                        public_key.set_once("--public-key", value)?;
                    }
                    other => return Err(unknown_argument("trust maintainer add", other)),
                }
            }
            let key_id = key_id.ok_or_else(|| {
                CliError::Usage("trust maintainer add requires --key-id".to_string())
            })?;
            let public_key = public_key.ok_or_else(|| {
                CliError::Usage("trust maintainer add requires --public-key".to_string())
            })?;
            let root = current_dir()?;
            let layout = open_repository(root)?;
            let (adopted, newly_added) = add_trusted_maintainer(&layout, &key_id, &public_key)
                .map_err(|err| err.to_string())?;
            if newly_added {
                println!("trusted maintainer key: {}", adopted.key_id);
            } else {
                println!("maintainer key already trusted: {}", adopted.key_id);
            }
            println!("policy: required=1");
            Ok(())
        }
        // RFC 102 Stage 5, design-v1.md §14.9: a supported interface for the revocation capability
        // that already existed as an undocumented hand-edit of the retired `policy.toml`. Not a new
        // capability -- see `trust_index.rs`'s own module doc for why the snapshot-container shape
        // makes this representable without a tombstone record.
        (Some("maintainer"), Some("remove")) => {
            let mut key_id = None;
            while let Some(arg) = args.next() {
                match arg.as_str() {
                    "--key-id" => {
                        let value = flag_value(&mut args, "--key-id")?;
                        key_id.set_once("--key-id", value)?;
                    }
                    other => return Err(unknown_argument("trust maintainer remove", other)),
                }
            }
            let key_id = key_id.ok_or_else(|| {
                CliError::Usage("trust maintainer remove requires --key-id".to_string())
            })?;
            let root = current_dir()?;
            let layout = open_repository(root)?;
            let removed =
                remove_trusted_maintainer(&layout, &key_id).map_err(|err| err.to_string())?;
            if removed {
                println!("revoked maintainer key: {key_id}");
            } else {
                println!("maintainer key was not trusted: {key_id}");
            }
            Ok(())
        }
        _ => Err(CliError::Usage(
            "usage: prikk trust maintainer add --key-id <key-id> --public-key <64-hex>\n       \
             prikk trust maintainer remove --key-id <key-id>"
                .to_string(),
        )),
    }
}

fn run_status() -> std::result::Result<(), CliError> {
    let root = current_dir()?;
    let layout = open_repository(root)?;
    let wal = Wal::for_layout(&layout, DEFAULT_ACTIVE_NAME);
    let replay = wal.replay().map_err(|err| err.to_string())?;
    println!("prikk repository: {}", layout.prikk_dir().display());
    let ref_store = RefStore::new(layout.clone());
    let main_ref = ref_store
        .read_current_ref_state_id("heads/main")
        .map_err(|err| err.to_string())?;
    println!("active WAL records: {}", replay.records.len());
    println!(
        "trailing partial WAL bytes: {}",
        replay.trailing_partial_bytes
    );
    match main_ref {
        Some(id) => println!("heads/main RefState: {id}"),
        None => println!("heads/main RefState: <not published>"),
    }
    // DC-66 criterion 7: report the queued patch count and the ref the queue targets, distinct from
    // `replay.records.len()` (a raw count with no ownership) and `heads/main RefState` (the last
    // *sealed* state, not what an active queue is targeting).
    if replay.records.is_empty() {
        println!("queued patches: 0");
    } else {
        let target = match read_active_ref_metadata(&layout).map_err(|err| err.to_string())? {
            ActiveRefMetadata::Valid(ref_name) => ref_name,
            ActiveRefMetadata::Missing => "<missing metadata>".to_string(),
            ActiveRefMetadata::Invalid(_) => "<malformed metadata>".to_string(),
        };
        println!(
            "queued patches: {} targeting {target}",
            replay.records.len()
        );
        // DC-57 (NFR-PERF-02): extends DC-66's existing queue report rather than inventing a second
        // reporting path, per app-requirements §6.3 ("status must recommend sealing").
        let thresholds = ActivePatchThresholds::from_env()?;
        if replay.records.len() >= thresholds.limit {
            println!(
                "warning: active patches ({}) at or above the configured hard limit ({}); \
                 commit is blocked until you run `prikk seal`",
                replay.records.len(),
                thresholds.limit
            );
        } else if replay.records.len() >= thresholds.warn {
            println!(
                "warning: active patches ({}) at or above the recommended threshold ({}); \
                 consider running `prikk seal`",
                replay.records.len(),
                thresholds.warn
            );
        }
    }
    println!("status: multi-operation text diff minimization and plugins not yet implemented");
    Ok(())
}

/// DC-57 (NFR-PERF-02): active-patch warn/hard-block thresholds, read once from the environment and
/// validated together — a warn threshold above the hard limit, a non-numeric value, or zero for
/// either is rejected rather than silently kept at the default (the same fail-closed precedent as
/// `PRIKK_AUTHOR_KEY_ID`/`PRIKK_AUTHOR_SEED`). Per-invocation only; never persisted in the
/// repository — a durable policy belongs to a future general configuration increment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct ActivePatchThresholds {
    warn: usize,
    limit: usize,
}

/// NFR-PERF-02's default warn threshold. The hard-block default,
/// [`prikk_store::DEFAULT_ACTIVE_PATCH_LIMIT`], is owned by `prikk-store` since it is also the
/// fallback baked into `WorktreePatchCommitOptions::file_level`/`prefer_text_edits`; this constant
/// has no store-side counterpart to share, since only the CLI's `status` output ever consults it.
const DEFAULT_ACTIVE_PATCH_WARN: usize = 800;

impl ActivePatchThresholds {
    fn from_env() -> std::result::Result<Self, String> {
        let warn =
            parse_active_patch_threshold_env("PRIKK_ACTIVE_PATCH_WARN", DEFAULT_ACTIVE_PATCH_WARN)?;
        let limit = parse_active_patch_threshold_env(
            "PRIKK_ACTIVE_PATCH_LIMIT",
            DEFAULT_ACTIVE_PATCH_LIMIT,
        )?;
        if warn > limit {
            return Err(format!(
                "PRIKK_ACTIVE_PATCH_WARN ({warn}) must not exceed PRIKK_ACTIVE_PATCH_LIMIT ({limit})"
            ));
        }
        Ok(Self { warn, limit })
    }
}

/// Parse one active-patch threshold environment variable, failing closed (never silently defaulting)
/// on anything present but malformed: non-numeric, or zero.
fn parse_active_patch_threshold_env(
    name: &str,
    default: usize,
) -> std::result::Result<usize, String> {
    let Ok(raw) = std::env::var(name) else {
        return Ok(default);
    };
    let trimmed = raw.trim();
    let value: usize = trimmed
        .parse()
        .map_err(|_| format!("{name} must be a positive integer, got {raw:?}"))?;
    if value == 0 {
        return Err(format!("{name} must be greater than zero, got 0"));
    }
    Ok(value)
}

fn run_log(args: Vec<String>) -> std::result::Result<(), CliError> {
    let args = parse_log_args(args)?;
    let layout = open_repository(args.root)?;
    // Received refs (DC-78 ruling 4) live in their own container (RFC 102 Stage 5:
    // received_index.rs, formerly refs/received/), not the local-ref pointer index, and their
    // RefState objects carry the origin's own name rather than the local "remotes/"-prefixed one
    // — load_ref_history's pointer/name-match check can't resolve them, so route separately.
    let history = if args.ref_name.starts_with("remotes/") {
        load_received_ref_history(&layout, &args.ref_name, args.limit)
    } else {
        load_ref_history(&layout, &args.ref_name, args.limit)
    }
    .map_err(|err| err.to_string())?;
    print_history(&layout, &history);
    Ok(())
}

fn run_checkout(args: Vec<String>) -> std::result::Result<(), CliError> {
    let args = parse_checkout_args(args)?;
    let layout = open_repository(args.root)?;
    match args.mode {
        CheckoutMode::PlanOnly => {
            let plan =
                prepare_checkout_plan(&layout, &args.ref_name).map_err(|err| err.to_string())?;
            print_checkout_plan(&layout, &plan);
        }
        CheckoutMode::SnapshotPlan => {
            let plan = prepare_snapshot_checkout_plan(&layout, &args.ref_name)
                .map_err(|err| err.to_string())?;
            print_snapshot_checkout_plan(&layout, &plan);
        }
        CheckoutMode::SnapshotMaterialize => {
            let report = materialize_snapshot_checkout(&layout, &args.ref_name)
                .map_err(|err| err.to_string())?;
            print_snapshot_materialization_report(&layout, &report);
        }
        CheckoutMode::PatchPlan => {
            let plan = prepare_patch_replay_plan(&layout, &args.ref_name)
                .map_err(|err| err.to_string())?;
            print_patch_replay_plan(&layout, &plan);
        }
        CheckoutMode::PatchMaterialize => {
            let report = materialize_patch_checkout(&layout, &args.ref_name)
                .map_err(|err| err.to_string())?;
            print_patch_materialization_report(&layout, &report);
        }
        CheckoutMode::PatchDeletePlan => {
            let plan = plan_patch_checkout_deletions(&layout, &args.ref_name)
                .map_err(|err| err.to_string())?;
            print_patch_deletion_plan(&layout, &plan);
            if !plan.is_safe_to_apply() {
                return Err("patch deletion plan has unsafe candidates"
                    .to_string()
                    .into());
            }
        }
        CheckoutMode::PatchMaterializeDelete => {
            let report = materialize_patch_checkout_with_deletions(&layout, &args.ref_name)
                .map_err(|err| err.to_string())?;
            print_patch_materialization_report(&layout, &report);
        }
    }
    Ok(())
}

fn run_merge_evidence(args: Vec<String>) -> std::result::Result<(), CliError> {
    let args = parse_merge_evidence_args(args)?;
    let layout = open_repository(args.root)?;
    let report = prepare_merge_evidence(
        &layout,
        args.baseline_block_id,
        merge_target_from_arg(args.left_target),
        merge_target_from_arg(args.right_target),
    )
    .map_err(|err| err.to_string())?;
    print_merge_evidence(&report);
    Ok(())
}

fn run_merge_plan(args: Vec<String>) -> std::result::Result<(), CliError> {
    let args = parse_merge_plan_args(args)?;
    let layout = open_repository(args.root)?;
    let plan = prepare_merge_plan(
        &layout,
        args.baseline_block_id,
        merge_target_from_arg(args.left_target),
        merge_target_from_arg(args.right_target),
    )
    .map_err(|err| err.to_string())?;
    print_merge_plan(&plan);
    Ok(())
}

fn merge_target_from_arg(target: MergeEvidenceTargetArg) -> MergeEvidenceTarget {
    match target {
        MergeEvidenceTargetArg::Block(block_id) => MergeEvidenceTarget::Block(block_id),
        // DC-85: a `--left-ref`/`--right-ref` value can name a received ref too, so evidence/plan
        // previews reach the same source a `merge` invocation would — same prefix-routing shape as
        // `run_log`'s `remotes/` dispatch, not `run_verify`/`branch list`'s list-both shape, since
        // this resolves one specific input rather than showing everything.
        MergeEvidenceTargetArg::Ref(ref_name) if ref_name.starts_with("remotes/") => {
            MergeEvidenceTarget::ReceivedRef(ref_name)
        }
        MergeEvidenceTargetArg::Ref(ref_name) => MergeEvidenceTarget::Ref(ref_name),
    }
}

fn run_inverse_plan(args: Vec<String>) -> std::result::Result<(), CliError> {
    let args = parse_inverse_plan_args(args)?;
    let layout = open_repository(args.root)?;
    let plan =
        prepare_patch_inverse_plan(&layout, &args.ref_name).map_err(|err| err.to_string())?;
    print_patch_inverse_plan(&layout, &plan);
    Ok(())
}

fn run_rollback_preview(args: Vec<String>) -> std::result::Result<(), CliError> {
    let args = parse_rollback_preview_args(args)?;
    let layout = open_repository(args.root)?;
    let plan = prepare_rollback_preview(&layout, &args.ref_name).map_err(|err| err.to_string())?;
    print_rollback_preview_plan(&layout, &plan);
    Ok(())
}

fn run_rollback_draft(args: Vec<String>) -> std::result::Result<(), CliError> {
    let args = parse_rollback_draft_args(args)?;
    let layout = open_repository(args.root)?;
    layout
        .require_current_format()
        .map_err(|err| err.to_string())?;
    let signer = author_signer_from_env()?;
    let report = append_rollback_draft(&layout, &args.ref_name, &args.message, &signer)
        .map_err(|err| err.to_string())?;
    print_rollback_draft_report(&layout, &report);
    Ok(())
}

fn run_rollback_draft_verify(args: Vec<String>) -> std::result::Result<(), CliError> {
    let args = parse_rollback_draft_verify_args(args)?;
    let layout = open_repository(args.root)?;
    let report =
        verify_active_rollback_draft(&layout, &args.ref_name).map_err(|err| err.to_string())?;
    print_rollback_draft_verification(&layout, &report);
    Ok(())
}

fn run_worktree_status(args: Vec<String>) -> std::result::Result<(), CliError> {
    let args = parse_worktree_status_args(args)?;
    let layout = open_repository(args.root)?;
    let report = worktree_status(&layout, &args.ref_name).map_err(|err| err.to_string())?;
    print_worktree_status(&layout, &report);
    if report.is_clean() {
        Ok(())
    } else {
        Err("worktree has changes against the baseline"
            .to_string()
            .into())
    }
}

fn run_verify(args: Vec<String>) -> std::result::Result<(), CliError> {
    let verify_args = parse_verify_args(args)?;
    let layout = open_repository(verify_args.root)?;
    let options = VerifyOptions {
        stop_on_first_error: verify_args.stop_on_first_error,
    };
    let report = verify_repository_with_options(&layout, options).map_err(|err| err.to_string())?;
    // RFC 118 stage 5: `--format json` emits exactly one JSON document and nothing else -- the
    // default prose path's received-refs lines below are additive presentation, not part of the
    // schema, and would corrupt a JSON stream if interleaved with it, so this branch replaces the
    // whole default body rather than adding to it.
    if verify_args.format == VerifyOutputFormat::Json {
        print_verify_report_json(&report)?;
    } else {
        print_verify_report(&layout, &report);
        // Received refs (DC-78 ruling 4) are never read by verify_repository itself — every object
        // they point at is already checked by the ordinary type-based object scan regardless of
        // which ref (if any) points to it, so this is purely additive presentation, not a new check.
        let received = list_received_pointers(&layout).map_err(|err| err.to_string())?;
        println!("received refs: {}", received.len());
        for pointer in &received {
            println!(
                "received-ref {}: {}",
                pointer.ref_name, pointer.ref_state_id
            );
        }
    }
    // This used to be a nine-arm hand-written else-if chain. It is now one lookup into
    // `verify_verdict::VERDICT_CONDITIONS` -- the same declaration `--format json` above reads --
    // so the exit code and the JSON verdict cannot silently diverge. First-match-wins order and
    // every message are unchanged from the prior chain.
    match verify_verdict::first_true_condition(&report) {
        Some(condition) => Err(condition.message.to_string().into()),
        None => Ok(()),
    }
}

fn run_doctor(args: Vec<String>) -> std::result::Result<(), CliError> {
    let doctor_args = parse_doctor_args(args)?;
    let layout = open_repository(doctor_args.root)?;
    if doctor_args.repair_wal_tail || doctor_args.repair_main_ref {
        let options = DoctorRepairOptions {
            truncate_wal_tail: doctor_args.repair_wal_tail,
            reconstruct_main_ref: doctor_args.repair_main_ref,
        };
        let repair = repair_repository(&layout, options).map_err(|err| err.to_string())?;
        println!("doctor repository: {}", layout.prikk_dir().display());
        // RFC 108 increment 3d review v1 §1's condition: `repair.wal_repair`'s own two lines
        // (still present in the struct, unchanged, for `default`-only callers) used to be the only
        // thing printed here -- silently omitting every other active session's own outcome, and
        // omitting even `default`'s own reason when it was `Skipped` rather than repaired.
        // `print_active_session_repairs` replaces them, covering every active session
        // (`default` included) from the one field that already carries the full, honest answer.
        print_active_session_repairs(&repair.active_repairs);
        print_doctor_report(&layout, &repair.after);
        // Exit-rule adjudication (review v1 §1): "every skip is a failure" is the correct rule
        // *here*, not as a general principle, but because this branch is only reached when
        // `doctor_args.repair_wal_tail` requested exactly this repair -- `repair_main_ref` always
        // refuses inside `repair_repository` itself, before any active session is even attempted, so
        // reaching this line means `truncate_wal_tail: true` was requested unconditionally. A
        // `Skipped` entry in `active_repairs` is therefore never vacuous here: it always means a WAL
        // tail truncation the operator asked for did not happen. (A hypothetical caller of
        // `repair_repository` that requests no repair at all, `DoctorRepairOptions::none()`, would
        // need a different rule -- skips would be expected, not a failure -- but no CLI path reaches
        // this line with that combination, so that rule is not built here.)
        let any_active_session_skipped = repair
            .active_repairs
            .iter()
            .any(|outcome| matches!(outcome.status, ActiveSessionRepairStatus::Skipped { .. }));
        if any_active_session_skipped {
            Err(
                "doctor repair skipped one or more active sessions; see the per-active outcomes \
                 above for which and why"
                    .to_string()
                    .into(),
            )
        } else if repair.after.is_healthy() {
            Ok(())
        } else {
            Err("doctor repair finished but repository health errors remain"
                .to_string()
                .into())
        }
    } else {
        let report = doctor_repository(&layout);
        println!("doctor repository: {}", layout.prikk_dir().display());
        print_doctor_report(&layout, &report);
        if report.is_healthy() {
            Ok(())
        } else {
            Err("doctor found repository health errors".to_string().into())
        }
    }
}

/// Build the AUTHOR signer from caller-supplied key material in the environment, failing closed if
/// none is configured. This is deliberately minimal key *input* — no trust store, key file, rotation,
/// or persistence (those are later phases). Real authoring requires:
///   - `PRIKK_AUTHOR_KEY_ID`: non-empty key identifier recorded in the signature;
///   - `PRIKK_AUTHOR_SEED`: 64 hex characters (a 32-byte Ed25519 secret seed).
fn author_signer_from_env() -> Result<Ed25519AuthorSigner, String> {
    let key_id = std::env::var("PRIKK_AUTHOR_KEY_ID").map_err(|_| {
        "author signing is required: set PRIKK_AUTHOR_KEY_ID (no signing key configured)"
            .to_string()
    })?;
    if key_id.trim().is_empty() {
        return Err("PRIKK_AUTHOR_KEY_ID must not be empty".to_string());
    }
    let seed_hex = std::env::var("PRIKK_AUTHOR_SEED").map_err(|_| {
        "author signing is required: set PRIKK_AUTHOR_SEED (64 hex chars; no signing key configured)"
            .to_string()
    })?;
    let seed = decode_seed_hex(&seed_hex, "PRIKK_AUTHOR_SEED")?;
    Ed25519AuthorSigner::from_seed(key_id, &seed).map_err(|err| err.to_string())
}

/// Build the MAINTAINER signer from caller-supplied key material in the environment, failing closed
/// if none is configured.
pub(crate) fn maintainer_signer_from_env() -> Result<Ed25519MaintainerSigner, String> {
    let key_id = std::env::var("PRIKK_MAINTAINER_KEY_ID").map_err(|_| {
        "maintainer signing is required: set PRIKK_MAINTAINER_KEY_ID (no signing key configured)"
            .to_string()
    })?;
    if key_id.trim().is_empty() {
        return Err("PRIKK_MAINTAINER_KEY_ID must not be empty".to_string());
    }
    let seed_hex = std::env::var("PRIKK_MAINTAINER_SEED").map_err(|_| {
        "maintainer signing is required: set PRIKK_MAINTAINER_SEED (64 hex chars; no signing key configured)"
            .to_string()
    })?;
    let seed = decode_seed_hex(&seed_hex, "PRIKK_MAINTAINER_SEED")?;
    Ed25519MaintainerSigner::from_seed(key_id, &seed).map_err(|err| err.to_string())
}

/// Decode exactly 64 hex characters into a 32-byte Ed25519 secret seed.
fn decode_seed_hex(hex: &str, env_name: &str) -> Result<[u8; 32], String> {
    let hex = hex.trim();
    if hex.len() != 64 {
        return Err(format!(
            "{env_name} must be 64 hex characters, got {}",
            hex.len()
        ));
    }
    let mut seed = [0_u8; 32];
    for (slot, pair) in seed.iter_mut().zip(hex.as_bytes().chunks_exact(2)) {
        let hi = pair
            .first()
            .copied()
            .ok_or_else(|| format!("{env_name} truncated"))?;
        let lo = pair
            .get(1)
            .copied()
            .ok_or_else(|| format!("{env_name} truncated"))?;
        *slot = (hex_nibble(hi, env_name)? << 4) | hex_nibble(lo, env_name)?;
    }
    Ok(seed)
}

/// Convert one ASCII hex character to its 4-bit value.
fn hex_nibble(c: u8, env_name: &str) -> Result<u8, String> {
    match c {
        b'0'..=b'9' => Ok(c - b'0'),
        b'a'..=b'f' => Ok(c - b'a' + 10),
        b'A'..=b'F' => Ok(c - b'A' + 10),
        _ => Err(format!("{env_name} contains a non-hex character")),
    }
}