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
//! The Handle Kind Decider (Rust binding).
//!
//! `INV-TAPE-01a` — TYPE DETECTION IS THE KERNEL'S.
//!
//! `card` has no type column. What a card *is* is named by its **handle** (`INV-TAPE-03`),
//! and decided by the kernel's `kindOf`. This module is the **Rust binding** of that
//! decision, not a second implementation: it carries the same grammar and the same declared
//! sector set as `kernel/clm_js_core/src/shared/handleKind.ts` and
//! `kernel/clm_py_core/clm/cross_cutting/handle_kind.py`, and
//! `scripts/ci/check_handle_kind.py` runs the TypeScript and Python bindings over one shared
//! fixture set and asserts they agree. A runtime that derives a kind independently has
//! reintroduced the removed `card_type` column as code.
//! ```text
//! urn:mcard:pcard:sprint:orc-02 -> sector "pcard", kind "sprint"
//! urn:mcard:vcard:seal:<hash> -> sector "vcard", kind "seal"
//! urn:mcard:telemetry:event:n:7 -> sector "telemetry", kind "event"
//! ```
//! THE GRAMMAR IS DECLARED BY THE HUB, IMPLEMENTED BY THE KERNEL. The hub's
//! `registry/handle_taxonomy.yaml` is the declaration; this embedded set is the
//! implementation, and the drift check asserts they are equal.
/// The decided kind of a handle.
///
/// `declared` distinguishes "an undeclared kind" from "not a handle at all" — which is what
/// makes an undeclared prefix a *finding* rather than a silent miss.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HandleKind {
pub sector: String,
pub kind: String,
pub handle: String,
pub declared: bool,
pub subject: Option<String>,
}
/// The declared sectors and their kinds. Must equal the hub's `handle_taxonomy.yaml`.
pub const KIND_SECTORS: &[(&str, &[&str])] = &[
// Content Memory (content_memory.db)
(
"mcard",
&[
"content", "knowledge", "schema", "contract", "registry", "record", "projection",
"derived", "lifecycle",
// ORC-24: the nine archetypes (`clm/schema/archetypes/`) as kinds — an
// archetype is `<kind>_<sector>`, so the kind is derived, not hand-mapped.
"artifact", "commit", "design_decision", "feedback", "specification",
"sprint_status", "telemetry", "test_result",
],
),
("doc", &["vault", "document"]),
("pcard", &["program", "sprint"]),
("vcard", &["seal", "execution", "contract"]),
// Agent Identities (agent_identities.db)
("agent", &["root", "actor"]),
("node", &["did", "peer"]),
("key", &["pub", "rotate"]),
// Execution History (execution_log.db)
("execution", &["record", "trace", "migration"]),
("telemetry", &["event"]),
];
/// The closed set of transition kinds (`INV-TAPE-04`). The KIND of a change is card content
/// in the lifecycle sidecar, never a column.
pub const CHANGE_CALCULUS: &[&str] = &[
"CREATE", "UPDATE", "SEAL", "RELOCATE", "ROTATE", "TOMBSTONE",
];
/// Split `urn:mcard:<sector>:<kind>[:<subject>...]` into its parts.
///
/// Hand-rolled rather than a regex so the crate takes no new dependency; the grammar is
/// small and the split is the whole of it.
fn split_handle(handle: &str) -> Option<(&str, &str, Option<&str>)> {
// NAMESPACE-01: the window is CLOSED. The four tapes hold `urn:mcard:` and zero
// `urn:clm:` rows, so the old prefix fails — which is the point.
let rest = handle.strip_prefix("urn:mcard:")?;
let (sector, rest) = rest.split_once(':')?;
let (kind, subject) = match rest.split_once(':') {
Some((k, s)) => (k, Some(s)),
None => (rest, None),
};
let lower = |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_lowercase() || b == b'_');
if !lower(sector) || !lower(kind) {
return None;
}
Some((sector, kind, subject))
}
/// Decide the kind of a handle. Pure: no I/O, no state, no cache.
pub fn kind_of(handle: &str) -> Option<HandleKind> {
let (sector, kind, subject) = split_handle(handle)?;
let declared = KIND_SECTORS
.iter()
.find(|(s, _)| *s == sector)
.map(|(_, kinds)| kinds.contains(&kind))
.unwrap_or(false);
Some(HandleKind {
sector: sector.to_string(),
kind: kind.to_string(),
handle: handle.to_string(),
declared,
subject: subject.map(str::to_string),
})
}
/// The sector of a handle, or `None`.
pub fn sector_of(handle: &str) -> Option<&str> {
split_handle(handle).map(|(s, _, _)| s)
}
/// The kind within the sector, or `None`.
pub fn kind_name_of(handle: &str) -> Option<&str> {
split_handle(handle).map(|(_, k, _)| k)
}
/// Is this handle in the given sector?
pub fn is_sector(handle: &str, sector: &str) -> bool {
sector_of(handle) == Some(sector)
}
/// Is this handle's kind declared? An undeclared kind is a finding, not a `false`.
pub fn is_declared(handle: &str) -> bool {
kind_of(handle).map(|k| k.declared).unwrap_or(false)
}
/// Every declared `sector:kind` pair, sorted — the drift check compares these to the hub.
pub fn declared_kinds() -> Vec<String> {
let mut out: Vec<String> = KIND_SECTORS
.iter()
.flat_map(|(s, kinds)| kinds.iter().map(move |k| format!("{s}:{k}")))
.collect();
out.sort();
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_the_declared_sectors() {
let k = kind_of("urn:mcard:pcard:sprint:orc-02").expect("a handle");
assert_eq!(k.sector, "pcard");
assert_eq!(k.kind, "sprint");
assert_eq!(k.subject.as_deref(), Some("orc-02"));
assert!(k.declared);
}
#[test]
fn a_subject_may_contain_colons() {
let k = kind_of("urn:mcard:node:did:key:z6Mk").expect("a handle");
assert_eq!(k.sector, "node");
assert_eq!(k.kind, "did");
assert_eq!(k.subject.as_deref(), Some("key:z6Mk"));
}
#[test]
fn an_undeclared_kind_is_a_finding_not_a_null() {
let k = kind_of("urn:mcard:bogus:thing").expect("well formed");
assert!(!k.declared, "an undeclared kind must report declared:false");
}
#[test]
fn a_non_handle_is_none() {
assert!(kind_of("not-a-handle").is_none());
assert!(kind_of("urn:mcard:Pcard:Sprint").is_none(), "the grammar is lowercase");
assert!(kind_of("urn:mcard:pcard").is_none(), "no kind");
assert!(kind_of("").is_none());
}
#[test]
fn declared_kinds_matches_the_other_bindings() {
// 34 pairs — the same count the TypeScript and Python bindings report.
// ORC-21 added doc (+2), and ORC-24 added the 9 archetypes (+9), reaching 34.
assert_eq!(declared_kinds().len(), 34);
}
}