spec-spine-cli 0.24.0

The `spec-spine` command-line tool: compile a markdown spec corpus into a deterministic authority registry and query it. A thin wrapper over spec-spine-core.
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
//! `spec-spine registry …`: typed, read-only queries over the compiled
//! registry. Assembles the registry from its committed per-spec shards via the
//! library (never ad-hoc parsing, per spec 000 §1; the shard tree replaces the
//! monolithic `registry.json` since spec 022).

use std::path::Path;

use clap::Subcommand;
use spec_spine_core::{
    ListFilter, MoveLookup, Plan, Versioning, flattened_moves, list, list_ids,
    load_committed_registry, lookup_move, plan, read_document, relationships, shard_content_hash,
    show, status_report,
};
use spec_spine_types::{Error, Status};

use crate::load_repo_config;

#[derive(Subcommand)]
pub enum RegistryQuery {
    /// List specs (optionally filtered by status).
    List {
        #[arg(long, value_name = "STATUS")]
        status: Option<String>,
        #[arg(long)]
        json: bool,
        /// Print bare spec ids, one per line (a JSON string array with --json).
        #[arg(long)]
        ids_only: bool,
    },
    /// Show one spec by id.
    Show {
        /// Spec id, full (`015-short-id-resolution`) or short (`016`).
        id: String,
        #[arg(long)]
        json: bool,
    },
    /// Counts of specs by status.
    StatusReport {
        #[arg(long)]
        json: bool,
        /// Omit statuses whose count is zero (the total still covers the corpus).
        #[arg(long)]
        nonzero_only: bool,
    },
    /// Show a spec's relationship neighborhood.
    Relationships {
        /// Spec id, full (`015-short-id-resolution`) or short (`016`).
        id: String,
        #[arg(long)]
        json: bool,
    },
    /// Resolve a qualified obligation reference, `<spec-id>#<obligation-id>`
    /// (spec 106). An unqualified id is refused, never resolved locally.
    Obligation {
        /// `<spec-id>#<obligation-id>`; the spec half may be short (`106`).
        reference: String,
        #[arg(long)]
        json: bool,
    },
    /// Resolve a context closure against the committed ledger (spec 107): every
    /// member's identity and one order-independent digest. Refuses a stale
    /// registry (exit 2).
    Closure {
        /// A closure request document, or `-` for stdin.
        #[arg(long, value_name = "FILE")]
        request: String,
        #[arg(long)]
        json: bool,
    },
    /// Every declared impact and conflict (spec 109), inverted so the target
    /// side can see what was declared about it. `--target` is a spec id
    /// (every obligation it declares) or a qualified `<spec-id>#<obligation-id>`
    /// reference; `--declared-by` is a spec id; both compose by intersection.
    Impacts {
        #[arg(long, value_name = "REF")]
        target: Option<String>,
        #[arg(long, value_name = "SPEC")]
        declared_by: Option<String>,
        #[arg(long)]
        json: bool,
    },
    /// Look up a path against every declared move (spec 111 §3.4). Without a
    /// path, lists every declaration, flattened and sorted: the derived path
    /// map. Answers from the committed registry, like `show`; refuses
    /// nothing about the working tree. Exits 1 on `ambiguous` or `cycle`.
    Moves {
        /// The path to look up. Omit to list every declared move.
        path: Option<String>,
        #[arg(long)]
        json: bool,
    },
    /// Which specs can be worked on now, and what blocks the rest (spec 035).
    Plan {
        #[arg(long)]
        json: bool,
        /// Print only the single pick: the first ready spec (spec 053). An
        /// empty ready set is `(nothing ready)` at exit 0, not a failure.
        #[arg(long)]
        next: bool,
    },
}

/// Returns `0` on success; `NotFound`/parse/schema errors propagate to the
/// caller's exit-code mapping.
pub fn run(repo: &Path, query: &RegistryQuery) -> Result<u8, Error> {
    let cfg = load_repo_config(repo)?;
    let registry = load_committed_registry(&cfg, repo)?;

    match query {
        RegistryQuery::List {
            status,
            json,
            ids_only,
        } => {
            let filter = ListFilter {
                status: status.as_deref().map(parse_status).transpose()?,
            };
            if *ids_only {
                // Spec 009 §3.1: ids and nothing else; an empty corpus prints
                // nothing (no "(no specs)" placeholder) and still exits 0.
                let ids = list_ids(&registry, &filter);
                if *json {
                    print_json(&ids)?;
                } else {
                    for id in ids {
                        outln!("{id}");
                    }
                }
            } else {
                let specs = list(&registry, &filter);
                if *json {
                    print_json(&specs)?;
                } else if specs.is_empty() {
                    outln!("(no specs)");
                } else {
                    for s in specs {
                        outln!("{}  {:<11}  {}", s.id, status_label(s.status), s.title);
                    }
                }
            }
        }
        RegistryQuery::Show { id, json } => {
            let spec = show(&registry, id)?;
            // Spec 048 §3.3: the hash the committed shard records, read and
            // never recomputed. Added at the output boundary and nowhere else:
            // putting it on `SpecRecord` would write it into every shard, whose
            // schema is `additionalProperties: false`, for a value the shard
            // already carries one line above the record.
            let content_hash = shard_content_hash(&cfg, repo, &spec.id)?;
            if *json {
                let mut value =
                    serde_json::to_value(spec).map_err(|e| Error::Schema(e.to_string()))?;
                if let (Some(obj), Some(h)) = (value.as_object_mut(), content_hash.as_ref()) {
                    obj.insert("contentHash".to_string(), serde_json::json!(h));
                }
                print_json(&value)?;
            } else {
                outln!("id:      {}", spec.id);
                outln!("title:   {}", spec.title);
                outln!("status:  {}", status_label(spec.status));
                outln!("created: {}", spec.created);
                outln!("path:    {}", spec.spec_path);
                outln!("summary: {}", spec.summary.trim());
                // Spec 114 §3.6: the declared intent is printed beside the
                // summary, so a reader sees both statements of purpose
                // together. Neither is checked against the other; where they
                // disagree the prose governs and the intent is corrected.
                if let Some(intent) = &spec.intent {
                    outln!("intent:  {}", intent.goal.trim());
                    for ng in &intent.non_goals {
                        outln!("  not:   {}", ng.trim());
                    }
                }
                if let Some(h) = &content_hash {
                    // 048 §3.4: say which hash this is in the same breath as
                    // reporting it. The registry's and the index's per-spec
                    // hashes are the same shape, and a consumer that confuses
                    // them gets a pin that fires on unrelated edits. Spec 077:
                    // and name the construction, since it is framed by the path
                    // and so is not the digest of the file's bytes, which a
                    // consumer reproducing it would otherwise compute.
                    outln!(
                        "contentHash: {h}  (sha256 over path + NUL + normalized spec.md bytes; not the bare file digest)"
                    );
                }
            }
        }
        RegistryQuery::StatusReport { json, nonzero_only } => {
            let report = status_report(&registry);
            if *nonzero_only {
                let projected = report.nonzero_only();
                if *json {
                    print_json(&projected)?;
                } else {
                    outln!("total:      {}", projected.total);
                    print_count("draft:     ", projected.draft);
                    print_count("approved:  ", projected.approved);
                    print_count("superseded:", projected.superseded);
                    print_count("retired:   ", projected.retired);
                }
            } else if *json {
                print_json(&report)?;
            } else {
                outln!("total:      {}", report.total);
                outln!("draft:      {}", report.draft);
                outln!("approved:   {}", report.approved);
                outln!("superseded: {}", report.superseded);
                outln!("retired:    {}", report.retired);
            }
        }
        RegistryQuery::Plan { json, next } => {
            let plan = plan(&registry)?;
            if *next {
                // Spec 053 §3.2: a projection of `plan`, never a second
                // selection. An empty ready set exits 0: "nothing to do" is a
                // true answer to "what should I work on", and a driven session
                // that treats it as an error stops for the wrong reason.
                //
                // Spec 074 §3.3 (amending 060 §3.2): with `--json` the pick sits
                // under a named `next` member, built here for the populated
                // answer and the empty one alike, so both are one shape and
                // "nothing is ready" is a present `null` rather than a missing
                // key. Still the object itself, not a one-element array.
                match plan.next() {
                    pick if *json => {
                        print_json(&serde_json::json!({ "next": pick }))?;
                    }
                    Some(pick) => outln!("{}  {}", pick.id, pick.title),
                    None => outln!("(nothing ready)"),
                }
            } else if *json {
                print_json(&plan)?;
            } else {
                print_plan(&plan);
            }
        }
        RegistryQuery::Obligation { reference, json } => {
            let view = spec_spine_core::obligation(&registry, reference)?;
            // Spec 106 §3.6: the spec's full identity beside the section's,
            // read from the committed shard and never recomputed (048 §3.3).
            let content_hash = shard_content_hash(&cfg, repo, view.spec)?;
            if *json {
                let mut value =
                    serde_json::to_value(&view).map_err(|e| Error::Schema(e.to_string()))?;
                if let (Some(obj), Some(h)) = (value.as_object_mut(), content_hash.as_ref()) {
                    obj.insert("contentHash".to_string(), serde_json::json!(h));
                }
                print_json(&value)?;
            } else {
                let ob = view.obligation;
                let kind = serde_json::to_value(ob.kind)
                    .ok()
                    .and_then(|v| v.as_str().map(str::to_string))
                    .unwrap_or_default();
                outln!(
                    "{}#{}{}",
                    view.spec,
                    ob.id,
                    if ob.withdrawn { "  (withdrawn)" } else { "" }
                );
                outln!("kind:    {kind}");
                outln!("text:    {}", ob.text);
                outln!("anchor:  {}#{}", view.spec_path, ob.anchor);
                if let Some(d) = view.section_digest {
                    outln!("sectionDigest: {d}");
                }
                if let Some(h) = &content_hash {
                    outln!("contentHash:   {h}");
                }
                for input in &ob.inputs {
                    outln!("input:   {input}");
                }
            }
        }
        RegistryQuery::Impacts {
            target,
            declared_by,
            json,
        } => {
            let set =
                spec_spine_core::impacts(&registry, target.as_deref(), declared_by.as_deref())?;
            if *json {
                print_json(&set)?;
            } else if set.impacts.is_empty() && set.conflicts.is_empty() {
                outln!("(no declarations)");
            } else {
                for imp in &set.impacts {
                    outln!(
                        "impact    {} -> {}  {}{}{}",
                        imp.declared_by,
                        imp.target,
                        label(imp.nature),
                        imp.successor
                            .as_deref()
                            .map(|s| format!("  successor={s}"))
                            .unwrap_or_default(),
                        if imp.target_withdrawn {
                            "  (target withdrawn)"
                        } else {
                            ""
                        }
                    );
                }
                for c in &set.conflicts {
                    outln!(
                        "conflict  {} -> {}  {}{}{}",
                        c.declared_by,
                        c.target,
                        label(c.resolution),
                        c.settled_by
                            .as_deref()
                            .map(|s| format!("  settled_by={s}"))
                            .unwrap_or_default(),
                        if c.target_withdrawn {
                            "  (target withdrawn)"
                        } else {
                            ""
                        }
                    );
                }
            }
        }
        RegistryQuery::Moves { path, json } => {
            return Ok(match path {
                Some(p) => {
                    let outcome = lookup_move(&registry, p);
                    // Spec 111 §3.4: `unmapped`/`resolved` exit 0; the lookup
                    // refusing to answer (`ambiguous`/`cycle`) exits 1, the
                    // way `couple` exits 1 on an open violation without that
                    // being an `Err` (spec 034): a full report is still
                    // printed, only the process's exit signals the refusal.
                    let code = match &outcome {
                        MoveLookup::Unmapped { .. } | MoveLookup::Resolved { .. } => 0,
                        MoveLookup::Ambiguous { .. } | MoveLookup::Cycle { .. } => 1,
                    };
                    if *json {
                        print_json(&outcome)?;
                    } else {
                        print_move_lookup(&outcome);
                    }
                    code
                }
                None => {
                    let entries = flattened_moves(&registry);
                    if *json {
                        print_json(&entries)?;
                    } else if entries.is_empty() {
                        outln!("(no declared moves)");
                    } else {
                        for e in &entries {
                            outln!(
                                "{}  {} -> {}  ({}){}",
                                e.declared_by,
                                e.from,
                                e.to.as_deref().unwrap_or("(removed)"),
                                e.kind.label(),
                                e.answered_by
                                    .as_deref()
                                    .map(|s| format!("  answered_by={s}"))
                                    .unwrap_or_default()
                            );
                        }
                    }
                    0
                }
            });
        }
        RegistryQuery::Closure { request, json } => {
            let text = if request == "-" {
                let mut buf = String::new();
                std::io::Read::read_to_string(&mut std::io::stdin(), &mut buf)
                    .map_err(|e| Error::Io(format!("read closure request from stdin: {e}")))?;
                buf
            } else {
                std::fs::read_to_string(request)
                    .map_err(|e| Error::Io(format!("read closure request {request}: {e}")))?
            };
            let req: spec_spine_core::ClosureRequest = serde_json::from_str(&text)
                .map_err(|e| Error::Parse(format!("invalid closure request: {e}")))?;
            let resolved = spec_spine_core::closure(&cfg, repo, &req)?;
            if *json {
                print_json(&resolved)?;
            } else {
                for m in &resolved.members {
                    match m {
                        spec_spine_core::ClosureMember::Spec { spec, content_hash } => {
                            outln!("spec        {spec}  {content_hash}")
                        }
                        spec_spine_core::ClosureMember::Section {
                            spec,
                            anchor,
                            digest,
                        } => {
                            outln!("section     {spec}#{anchor}  {digest}")
                        }
                        spec_spine_core::ClosureMember::Obligation {
                            spec,
                            id,
                            section_digest,
                            withdrawn,
                            ..
                        } => outln!(
                            "obligation  {spec}#{id}  {section_digest}{}",
                            if *withdrawn { "  (withdrawn)" } else { "" }
                        ),
                    }
                }
                outln!("digest: {}", resolved.digest);
            }
        }
        RegistryQuery::Relationships { id, json } => {
            let view = relationships(&registry, id)?;
            if *json {
                print_json(&view)?;
            } else {
                outln!("{}", view.id);
                print_ids("depends_on", &view.depends_on);
                print_ids("supersedes", &view.supersedes);
                print_ids("amends", &view.amends);
                print_ids("superseded_by (incoming)", &view.superseded_by);
                print_ids("amended_by (incoming)", &view.amended_by);
                print_ids("depended_on_by (incoming)", &view.depended_on_by);
            }
        }
    }
    Ok(0)
}

/// The human form: the ready set in order, then one line of what is blocked.
///
/// The prose form counts rather than enumerating the blocked set, because the
/// question a person asks at a terminal is "what can I do now"; `--json` carries
/// every blocker and its state for the consumer that asks "why not that one".
fn print_plan(plan: &Plan) {
    // Spec 053 §3.1: render what the structure already holds. Titles come from
    // the registry the plan was computed from, and each blocked spec's reasons
    // are printed rather than counted: `blocked_by` carries the state of every
    // blocker, and printing the count while discarding the states throws away
    // the part a reader needs.
    if plan.ready.is_empty() {
        // The line a finished corpus prints, unchanged: it already carries the
        // count it would otherwise repeat as "ready: 0".
        outln!("(nothing ready), blocked: {}", plan.blocked.len());
    } else {
        outln!("ready ({}):", plan.ready.len());
        let w = id_width(plan.ready.iter().map(|r| r.id.as_str()));
        for r in &plan.ready {
            outln!("  {:<w$}  {}", r.id, r.title, w = w);
        }
    }
    if !plan.blocked.is_empty() {
        outln!();
        outln!("blocked ({}):", plan.blocked.len());
        let w = id_width(plan.blocked.iter().map(|b| b.id.as_str()));
        for b in &plan.blocked {
            outln!("  {:<w$}  {}", b.id, b.title, w = w);
            let reasons: Vec<String> = b
                .blocked_by
                .iter()
                .map(|k| format!("{} ({})", k.id, k.state))
                .collect();
            outln!("       blocked by {}", reasons.join(", "));
        }
    }
    // Spec 063 §3.6: what the corpus has said it will own and has not written
    // yet. Reported below both sets rather than folded into either, because a
    // blocked spec's planned territory is exactly what a reader wants when
    // weighing what unblocking it would cost.
    if !plan.planned.is_empty() {
        outln!();
        let units: usize = plan.planned.iter().map(|p| p.units.len()).sum();
        outln!(
            "planned territory ({units} unit(s) across {} spec(s)):",
            plan.planned.len()
        );
        let w = id_width(plan.planned.iter().map(|p| p.id.as_str()));
        for entry in &plan.planned {
            outln!("  {:<w$}  {}", entry.id, entry.title, w = w);
            for unit in &entry.units {
                outln!("       {unit}");
            }
        }
    }
    // Spec 072 §3.3: printed after both sets and after planned territory,
    // because it is a fact *about* the ready set rather than a third set. The
    // caveat rides on the heading line and not in a footnote: a reader who
    // skims the count and stops must not come away with a clearance.
    if !plan.overlaps.is_empty() {
        outln!();
        outln!(
            "overlapping territory ({} pair(s), a lower bound on what the corpus declares; an absent pair is not a safety verdict):",
            plan.overlaps.len()
        );
        for o in &plan.overlaps {
            outln!("  {} + {}", o.specs[0], o.specs[1]);
            for unit in &o.units {
                outln!("       {unit}");
            }
        }
    }
    outln!();
    outln!(
        "{} specs: {} ready, {} blocked, {} not schedulable",
        plan.ready.len() + plan.blocked.len() + plan.not_schedulable,
        plan.ready.len(),
        plan.blocked.len(),
        plan.not_schedulable
    );
}

/// Column width for an id list, so titles line up. Per group, because the two
/// groups are read separately and a shared width would pad one of them for the
/// other's benefit.
fn id_width<'a>(ids: impl Iterator<Item = &'a str>) -> usize {
    ids.map(|id| id.chars().count()).max().unwrap_or(0)
}

fn parse_status(s: &str) -> Result<Status, Error> {
    match s {
        "draft" => Ok(Status::Draft),
        "approved" => Ok(Status::Approved),
        "superseded" => Ok(Status::Superseded),
        "retired" => Ok(Status::Retired),
        other => Err(Error::NotFound(format!(
            "unknown status '{other}' (expected draft|approved|superseded|retired)"
        ))),
    }
}

/// The lowercase wire spelling of a `lowercase`-serde enum (spec 109's
/// `ImpactNature` / `ConflictResolution`), for the text rendering: `{:?}`
/// would print `Refines`, and the wire form a reader of the frontmatter wrote
/// is `refines`.
fn label(v: impl serde::Serialize) -> String {
    serde_json::to_value(v)
        .ok()
        .and_then(|v| v.as_str().map(str::to_string))
        .unwrap_or_default()
}

fn status_label(s: Status) -> &'static str {
    match s {
        Status::Draft => "draft",
        Status::Approved => "approved",
        Status::Superseded => "superseded",
        Status::Retired => "retired",
    }
}

fn print_count(label: &str, count: Option<usize>) {
    if let Some(n) = count {
        outln!("{label} {n}");
    }
}

fn print_ids(label: &str, ids: &[String]) {
    if !ids.is_empty() {
        outln!("  {label}: {}", ids.join(", "));
    }
}

/// The prose form of a `registry moves <path>` lookup (spec 111 §3.4).
fn print_move_lookup(outcome: &MoveLookup) {
    match outcome {
        MoveLookup::Unmapped { path } => outln!("{path}  unmapped"),
        MoveLookup::Resolved {
            path,
            hops,
            terminals,
        } => {
            outln!("{path}  resolved");
            for h in hops {
                outln!(
                    "  {} -> {}  ({}, declared by {}){}",
                    h.from,
                    h.to.as_deref().unwrap_or("(removed)"),
                    h.kind.label(),
                    h.declared_by.join(", "),
                    h.answered_by
                        .as_deref()
                        .map(|s| format!("  answered_by={s}"))
                        .unwrap_or_default()
                );
            }
            for t in terminals {
                match (&t.path, &t.answered_by) {
                    (Some(p), _) => outln!("  terminal: {p}"),
                    (None, Some(by)) => outln!("  terminal: removed, answered_by={by}"),
                    (None, None) => outln!("  terminal: removed"),
                }
            }
        }
        MoveLookup::Ambiguous {
            path,
            at,
            candidates,
        } => {
            outln!("{path}  ambiguous at '{at}'");
            for c in candidates {
                outln!(
                    "  {} declares {}  ({}){}",
                    c.declared_by,
                    c.to.as_deref().unwrap_or("(removed)"),
                    c.kind.label(),
                    c.answered_by
                        .as_deref()
                        .map(|s| format!("  answered_by={s}"))
                        .unwrap_or_default()
                );
            }
        }
        MoveLookup::Cycle { path, chain } => {
            outln!("{path}  cycle: {}", chain.join(" -> "));
        }
    }
}

/// Emit a read document (spec 074): sorted keys, object form, `schemaVersion`.
/// Every `--json` arm of this verb comes through here, which is what makes a
/// projection flag a versioned document rather than a call site that forgot.
fn print_json<T: serde::Serialize>(value: &T) -> Result<(), Error> {
    out!("{}", read_document(value, Versioning::Stamp)?);
    Ok(())
}