doctrine 0.5.1

Project tooling CLI
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
// SPDX-License-Identifier: GPL-3.0-only
//! The status-class partition (SL-047 design §5.3, OQ-8) — the pure policy data
//! that classifies a node's authored status into [`StatusClass::Workable`]
//! (eligible for default-active work) / [`StatusClass::Terminal`]
//! (default-excluded) / [`StatusClass::Unrecognised`] (the D12 conservative
//! default: non-eligible **and** a diagnostic).
//!
//! Pure: no clock, RNG, or disk — a `(kind, status) -> StatusClass` lookup over the
//! static [`PARTITION`] table. Kind identity is the `&'static str` prefix (the same
//! identity `EntityKey` carries); `entity::Kind` is data, not `Eq`, so the table
//! keys on `kind.prefix`.
//!
//! **Drift canary (VT-1).** Each partitioned closed-enum kind asserts
//! `workable ∪ terminal == <kind>'s status vocabulary`, reading the REAL authoritative
//! const (`*_STATUSES` in each kind module) — so the canary FAILS at test time if a
//! kind adds a status the table forgot. REC is status-less (no const-compare); slice
//! binds against the ADR-009/`SLICE_STATUSES` lifecycle vocabulary (its stringly status
//! has no closed enum), and a slice status outside the table rides `Unrecognised`.
//!
//! Consumed by the priority CLI surface (SL-047 PHASE-03 — `channels`/`surface` call
//! [`status_class`]), so the PHASE-02 self-clearing `not(test)` `dead_code`
//! suppression has retired itself, as designed (`mem.pattern.lint.
//! dead-code-expect-vs-cfg-test`).

use crate::entity;

/// How a node's authored status classifies for default-active work selection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum StatusClass {
    /// Eligible — the status is in the kind's workable set.
    Workable,
    /// Default-excluded — the status is in the kind's terminal set (or the
    /// status-less REC kind, whose every node is context-only).
    Terminal,
    /// Not in the kind's vocabulary — the D12 conservative default: non-eligible
    /// AND a diagnostic (the table forgot a status, or the status drifted).
    Unrecognised,
}

/// One kind's status partition: the workable (eligible) and terminal
/// (default-excluded) status sets, keyed by the kind's canonical-id `prefix`.
/// A status absent from BOTH sets is [`StatusClass::Unrecognised`].
struct KindPartition {
    /// The kind's canonical-id prefix (`SL`, `ADR`, `ISS`, …) — kind identity.
    prefix: &'static str,
    workable: &'static [&'static str],
    terminal: &'static [&'static str],
}

/// The §5.3 status-class table, verbatim. Backlog's five prefixes
/// (`ISS`/`IMP`/`CHR`/`RSK`/`IDE`) share ONE partition (one generic backlog
/// vocabulary, PRD-009) — listed per-prefix so the lookup is a flat prefix match.
/// `promoted` is NOT modelled here (a node-attr concern surfaced in channels, F1).
/// REC is status-less and absent from this table — [`status_class`] maps its `None`
/// status to [`StatusClass::Terminal`] directly (DD-4 context-only, no diagnostic).
const PARTITION: &[KindPartition] = &[
    // slice — ADR-009 lifecycle vocabulary (stringly status, no closed enum).
    KindPartition {
        prefix: "SL",
        workable: &[
            "proposed",
            "design",
            "plan",
            "ready",
            "started",
            "audit",
            "reconcile",
        ],
        terminal: &["done", "abandoned"],
    },
    // ADR
    KindPartition {
        prefix: "ADR",
        workable: &["proposed"],
        terminal: &["accepted", "rejected", "superseded", "deprecated"],
    },
    // policy
    KindPartition {
        prefix: "POL",
        workable: &["draft"],
        terminal: &["required", "superseded", "deprecated", "retired"],
    },
    // standard
    KindPartition {
        prefix: "STD",
        workable: &["draft"],
        terminal: &["default", "required", "superseded", "deprecated", "retired"],
    },
    // PRD (product spec)
    KindPartition {
        prefix: "PRD",
        workable: &["draft"],
        terminal: &["active", "deprecated", "superseded"],
    },
    // tech spec
    KindPartition {
        prefix: "SPEC",
        workable: &["draft"],
        terminal: &["active", "deprecated", "superseded"],
    },
    // requirement
    KindPartition {
        prefix: "REQ",
        workable: &["pending", "in-progress"],
        terminal: &["active", "deprecated", "retired", "superseded"],
    },
    // backlog ×5 — one generic vocabulary; `promoted` resolution handled in channels.
    KindPartition {
        prefix: "ISS",
        workable: BACKLOG_WORKABLE,
        terminal: BACKLOG_TERMINAL,
    },
    KindPartition {
        prefix: "IMP",
        workable: BACKLOG_WORKABLE,
        terminal: BACKLOG_TERMINAL,
    },
    KindPartition {
        prefix: "CHR",
        workable: BACKLOG_WORKABLE,
        terminal: BACKLOG_TERMINAL,
    },
    KindPartition {
        prefix: "RSK",
        workable: BACKLOG_WORKABLE,
        terminal: BACKLOG_TERMINAL,
    },
    KindPartition {
        prefix: "IDE",
        workable: BACKLOG_WORKABLE,
        terminal: BACKLOG_TERMINAL,
    },
    // RV (review) — DERIVED active/done (NodeAttr already carries the derived string).
    KindPartition {
        prefix: "RV",
        workable: &["active"],
        terminal: &["done"],
    },
    // REV (revision, SL-066/ADR-013) — its OWN row: REV vocab ≠ backlog's, so it
    // cannot ride the backlog arm. Without this row a `done`/`abandoned` REV
    // classifies `Unrecognised != Terminal` and `blocked_by` (channels.rs:67, which
    // excuses only `class == Terminal`) blocks its dependent FOREVER — the inverse of
    // the IDE-010 payoff. The terminal set reads REV's real vocab via the
    // `crate::revision::REV_STATUSES`-bound canary (`revision_partition_covers_the_real_vocabulary`).
    KindPartition {
        prefix: "REV",
        workable: &["proposed", "started"],
        terminal: &["done", "abandoned"],
    },
    // Knowledge records (SL-059, NF-003 / D7) — NEVER `Workable`: each kind's entry
    // is `workable: &[]`, `terminal: <KIND>_STATUSES` (the FULL vocab), so every
    // record status classifies `Terminal` (the positive all-`Terminal` declaration,
    // distinct from REC's status-less `None → Terminal` path). The terminal set
    // reads the REAL `knowledge::*_STATUSES` const, so the VT-1 canary fails if a
    // kind adds a status the table forgot. Direct gating is IMP-047 (out of scope).
    KindPartition {
        prefix: "ASM",
        workable: &[],
        terminal: crate::knowledge::ASSUMPTION_STATUSES,
    },
    KindPartition {
        prefix: "DEC",
        workable: &[],
        terminal: crate::knowledge::DECISION_STATUSES,
    },
    KindPartition {
        prefix: "QUE",
        workable: &[],
        terminal: crate::knowledge::QUESTION_STATUSES,
    },
    KindPartition {
        prefix: "CON",
        workable: &[],
        terminal: crate::knowledge::CONSTRAINT_STATUSES,
    },
];

/// The shared backlog workable set (the five backlog prefixes partition identically).
const BACKLOG_WORKABLE: &[&str] = &["open", "triaged", "started"];
/// The shared backlog terminal set (a promoted resolution is a SEPARATE channel
/// reason, F1 — not a status class).
const BACKLOG_TERMINAL: &[&str] = &["resolved", "closed"];

/// Classify a node's `(kind, status)` into a [`StatusClass`] (design §5.3).
///
/// - `Some(s)` → table lookup on `kind.prefix`: `s ∈ workable → Workable`,
///   `s ∈ terminal → Terminal`, **else `Unrecognised`** (the D12 conservative
///   default — non-eligible plus a diagnostic).
/// - `None` (the status-less REC kind) → `Terminal`, NO diagnostic (DD-4
///   context-only, expected — never surfaced as drift).
/// - RV resolves through the table via its DERIVED `active`/`done` (already held in
///   `NodeAttr.status` from PHASE-01), exactly like any other kind.
///
/// `promoted` is intentionally NOT consulted here — a promoted backlog node is
/// excluded by a SEPARATE channel reason (F1 / REQ-075 AC2), surfaced where the
/// node-attr lives, not folded into the status class.
pub(crate) fn status_class(kind: &entity::Kind, status: Option<&str>) -> StatusClass {
    let Some(status) = status else {
        // The only status-less kind is REC: context-only, default-excluded, expected.
        return StatusClass::Terminal;
    };
    let Some(part) = PARTITION.iter().find(|p| p.prefix == kind.prefix) else {
        // A kind with no partition entry (e.g. a future kind) — conservative default.
        return StatusClass::Unrecognised;
    };
    if part.workable.contains(&status) {
        StatusClass::Workable
    } else if part.terminal.contains(&status) {
        StatusClass::Terminal
    } else {
        StatusClass::Unrecognised
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        adr, backlog, knowledge, policy, requirement, review, revision, slice, spec, standard,
    };
    use std::collections::BTreeSet;

    /// Look up a partition entry by prefix (test helper).
    fn part(prefix: &str) -> &'static KindPartition {
        PARTITION
            .iter()
            .find(|p| p.prefix == prefix)
            .expect("prefix in PARTITION")
    }

    /// `workable ∪ terminal` for a prefix, as a set.
    fn vocab(prefix: &str) -> BTreeSet<&'static str> {
        let p = part(prefix);
        p.workable
            .iter()
            .chain(p.terminal.iter())
            .copied()
            .collect()
    }

    fn set(items: &[&'static str]) -> BTreeSet<&'static str> {
        items.iter().copied().collect()
    }

    // -- VT-1: partition drift canary — table vocabulary == real const vocabulary --

    #[test]
    fn slice_partition_binds_adr009_lifecycle_vocabulary() {
        // slice has a STRINGLY status (no closed enum) — its canary binds to the
        // ADR-009 / SLICE_STATUSES lifecycle set, the transition authority.
        assert_eq!(vocab("SL"), set(slice::SLICE_STATUSES));
    }

    #[test]
    fn adr_partition_covers_the_real_vocabulary() {
        assert_eq!(vocab("ADR"), set(adr::ADR_STATUSES));
    }

    #[test]
    fn policy_partition_covers_the_real_vocabulary() {
        assert_eq!(vocab("POL"), set(policy::POLICY_STATUSES));
    }

    #[test]
    fn standard_partition_covers_the_real_vocabulary() {
        assert_eq!(vocab("STD"), set(standard::STANDARD_STATUSES));
    }

    #[test]
    fn prd_and_tech_spec_partitions_cover_the_real_vocabulary() {
        // SPEC_STATUSES covers BOTH the PRD and tech-spec rows.
        assert_eq!(vocab("PRD"), set(spec::SPEC_STATUSES));
        assert_eq!(vocab("SPEC"), set(spec::SPEC_STATUSES));
    }

    #[test]
    fn requirement_partition_covers_the_real_vocabulary() {
        assert_eq!(vocab("REQ"), set(requirement::REQ_STATUSES));
    }

    #[test]
    fn backlog_partition_covers_the_real_vocabulary() {
        // All five backlog prefixes share the one backlog vocabulary.
        for prefix in ["ISS", "IMP", "CHR", "RSK", "IDE"] {
            assert_eq!(
                vocab(prefix),
                set(backlog::BACKLOG_STATUSES),
                "{prefix} partition matches BACKLOG_STATUSES"
            );
        }
    }

    #[test]
    fn review_partition_covers_the_real_vocabulary() {
        assert_eq!(vocab("RV"), set(review::REVIEW_STATUSES));
    }

    // -- SL-066 VT-2: the REV partition's G1 canary (its own vocab, not backlog's) --

    #[test]
    fn revision_partition_covers_the_real_vocabulary() {
        // REV gets its OWN row + own const — the table vocab must equal the real
        // `revision::REV_STATUSES` (NOT backlog's), or a status drifts unnoticed.
        assert_eq!(vocab("REV"), set(revision::REV_STATUSES));
    }

    #[test]
    fn revision_done_and_abandoned_classify_terminal() {
        // The precondition for `needs REV-N` to unblock its dependent: a terminal
        // REV must classify `Terminal` (not `Unrecognised`), so `blocked_by` excuses
        // it (channels.rs:67). proposed/started stay `Workable`.
        assert_eq!(
            status_class(&revision::REV_KIND, Some("done")),
            StatusClass::Terminal
        );
        assert_eq!(
            status_class(&revision::REV_KIND, Some("abandoned")),
            StatusClass::Terminal
        );
        assert_eq!(
            status_class(&revision::REV_KIND, Some("proposed")),
            StatusClass::Workable
        );
        assert_eq!(
            status_class(&revision::REV_KIND, Some("started")),
            StatusClass::Workable
        );
    }

    // -- SL-059 VT-3: the four knowledge partitions cover their real vocabulary --

    #[test]
    fn knowledge_partitions_cover_the_real_vocabularies() {
        // workable ∪ terminal == statuses(kind) per kind (∅ ∪ full = full); the
        // canary reads the REAL `knowledge::*_STATUSES` const.
        for kind in knowledge::RecordKind::ALL {
            let prefix = kind.prefix();
            assert_eq!(
                vocab(prefix),
                set(knowledge::statuses(kind)),
                "{prefix} partition matches statuses({kind:?})"
            );
        }
    }

    #[test]
    fn every_knowledge_status_classifies_terminal_never_workable() {
        // NF-003: records are NEVER `Workable` — every status in every kind's full
        // vocab classifies `Terminal` (workable: &[] makes the workable arm dead).
        for kind in knowledge::RecordKind::ALL {
            for status in knowledge::statuses(kind) {
                assert_eq!(
                    status_class(kind.kind(), Some(status)),
                    StatusClass::Terminal,
                    "{:?}/{status} must be Terminal, never Workable",
                    kind
                );
            }
        }
    }

    #[test]
    fn decision_accepted_diverges_hidden_from_status_class() {
        // F-A5 (VT-4): the two notions deliberately disagree on `accepted` — it is
        // LIST-VISIBLE (`is_hidden == false`, a live decision is not settled-away)
        // yet never workable (`status_class == Terminal`, NF-003).
        assert!(!knowledge::is_hidden(
            knowledge::RecordKind::Decision,
            "accepted"
        ));
        assert_eq!(
            status_class(&knowledge::DECISION_KIND, Some("accepted")),
            StatusClass::Terminal
        );
    }

    // -- VT-3: conservative / status-less classification ----------------------

    #[test]
    fn rec_status_less_is_terminal_no_diagnostic() {
        // REC is the known status-less kind: None → Terminal, NOT Unrecognised
        // (so it raises no drift diagnostic — DD-4 context-only).
        assert_eq!(
            status_class(&crate::rec::REC_KIND, None),
            StatusClass::Terminal
        );
    }

    #[test]
    fn unrecognised_status_is_its_own_class() {
        // A status outside the kind's vocabulary → Unrecognised (the diagnostic
        // default), distinct from Terminal.
        assert_eq!(
            status_class(&slice::SLICE_KIND, Some("not-a-real-status")),
            StatusClass::Unrecognised
        );
    }

    #[test]
    fn workable_and_terminal_lookups() {
        // A workable slice status.
        assert_eq!(
            status_class(&slice::SLICE_KIND, Some("design")),
            StatusClass::Workable
        );
        // audit / reconcile are WORKABLE (VT-2 boundary — not yet terminal).
        assert_eq!(
            status_class(&slice::SLICE_KIND, Some("audit")),
            StatusClass::Workable
        );
        assert_eq!(
            status_class(&slice::SLICE_KIND, Some("reconcile")),
            StatusClass::Workable
        );
        // A terminal slice status.
        assert_eq!(
            status_class(&slice::SLICE_KIND, Some("done")),
            StatusClass::Terminal
        );
    }

    #[test]
    fn rv_derived_status_resolves_through_the_table() {
        // RV carries a DERIVED active/done — classified like any kind (Charge I).
        assert_eq!(
            status_class(&review::REVIEW_KIND, Some("active")),
            StatusClass::Workable
        );
        assert_eq!(
            status_class(&review::REVIEW_KIND, Some("done")),
            StatusClass::Terminal
        );
    }
}