Skip to content

Commit be1d2f6

Browse files
committed
feat(contract): D-GV2-2 — per-family Codebook registry (gated guid-v2-tail)
Continues the v2 arc after #560 merged. The type + in-memory registry tier of the family→Codebook scoping (the Lance-backed/OntologyRegistry tier is deferred). Zero-dep, feature-gated (default OFF). contract::codebook: - Codebook — insertion-ordered index↔label interning, 1-byte index, CODEBOOK_CAP=256; intern() returns None on overflow (the split-the-family signal, never widen the byte). - FamilyCodebookRegistry — family(u16) → Codebook; intern(family,label), resolve(family,index) for cross-family decode. Per-family scoping: the SAME label gets INDEPENDENT indices in different families (no global codebook contamination — this is what dissolves the aiwar "60 noisy families"). The finer sibling of classid→ClassView; the family node's episodic-basin content (E-MIXIN-IS-AN-ADDRESS-REFERENCE-NOT-A-COPY); the 256×256 Morton tile (≤256 leaves for the 1-byte in-family index, E-UNIFORM-MORTON-TILE-PYRAMID). 3 tests (dedup/sequential, overflow-split, per-family scoping). --features guid-v2-tail green; default build clean (codebook absent); clippy -D warnings clean both. Plan D-GV2-2 marked PARTIAL; AGENT_LOG updated. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CcpLeEC3XK8Eye53GKBVvi
1 parent c05394f commit be1d2f6

4 files changed

Lines changed: 182 additions & 1 deletion

File tree

.claude/board/AGENT_LOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2026-06-20 (cont.¹⁰) — D-GV2-2 partial: per-family Codebook (contract::codebook, gated)
2+
3+
**Main thread (Opus), autoattended.** #560 merged (synced main c05394f4; #558 also merged). Continued the greenlit v2 arc with **D-GV2-2** (type + in-memory registry tier): NEW `contract::codebook` (feature `guid-v2-tail`, zero-dep, default OFF) — `Codebook` (insertion-ordered index↔label, 1-byte index, `CODEBOOK_CAP=256`, overflow→None = split-the-family signal) + `FamilyCodebookRegistry` (`family→Codebook`, per-family scoping so the SAME label gets independent indices per family, `resolve(family,index)` for cross-family decode). The finer sibling of `classid→ClassView`; the family node's episodic-basin content; the 256×256 Morton tile (≤256 leaves for the 1-byte in-family index). Dissolves the aiwar "60 noisy families" at the root (per-family vocabularies are small + clean). 3 tests; `--features guid-v2-tail` green, default build clean (codebook absent), clippy clean both. **DEFERRED:** Lance-backed persistence + OntologyRegistry integration in lance-graph-ontology. Next: D-GV2-3 (soa_graph per-family edges under v2) + D-GV2-4 (aiwar re-key). New PR off main. **558/559 (OpenProject/Redmine bridges, other arc) still have open comments — left for that arc.**
4+
15
## 2026-06-20 (cont.⁹) — PR #560 codex P2 review fixes (gremlin bag semantics + aiwar cross-family edges)
26

37
**Main thread (Opus), autoattended.** Two unresolved P2 codex threads on PR #560, both fixed: (1) `graph_gremlin.rs` `step()` silently deduped targets via a `seen` set — broke Gremlin bag/multiset semantics (`v(["A","C"]).out().count()` = 1 not 2 when both reach B). Rewrote to per-traverser emission (duplicates preserved); added explicit `dedup()` step + `out_preserves_bag_multiplicity` test. (2) `aiwar.rs` `aiwar_node_rows` put cross-category adapter bytes into the first 12 `in_family` slots (labeled `linked`), so `references` queries missed them and the label flipped with fan-out count — aiwar edges are ALL cross-family, so they now go to the 4 `out_family` slots (`references`), cap 4; test asserts `references` present + no `linked`. contract aiwar 3/3, callcenter gremlin 8/8 (+1 bag test), clippy clean (my files; pre-existing TD-CALLCENTER-QUERY-CLIPPY untouched). Pushed to #560; both review threads resolved. **558/559 (NOT mine — OpenProject/Redmine ontology bridges) checked: NOT all resolved** — #558 2 open (codex P2 seed-context-id + CodeRabbit unit-tests), #559 1 open P1 (Redmine/OpenProject entity_type_id convergence). Surfaced to operator (different arc); not auto-fixed.

.claude/plans/guid-v2-tail-per-family-codebook-v1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ coarse→fine left-to-right: `…·TWIG·leaf` (routing) then `family·identity`
108108
## Deliverables (when greenlit — feature `guid-v2-tail`, default OFF)
109109

110110
- **D-GV2-1****SHIPPED (this PR, feature `guid-v2-tail` default OFF, additive & non-breaking).** `canonical_node`: `new_v2` (+`leaf`), `leaf()` `10..12`, `family_v2()` `12..14`, `identity_v2()` `14..16` (all `u16`), `local_key_v2()` (4 bytes), `decode_v2()`/`GuidPartsV2`, `to_hex_v2()` (uniform 4-hex), `GUID_TAIL_LAYOUT_VERSION_V2 = 2` (version gate). `hhtl::from_guid_prefix_v2` = `HEEL·HIP·TWIG·leaf` (16 nibbles; classid is the separate codebook prefix; leaf in path, family/identity NOT). v1 `new`/`family()`/`identity()` UNTOUCHED (distinct v2 names → no silent semantic swap, `I-LEGACY-API-FEATURE-GATED`). **Field-isolation matrix test** + v1/v2 coexistence + leaf-in-path tests. default 703 / `--features guid-v2-tail` 706, clippy clean both. **Cutover (rename v2→canonical, deprecate v1, bump `ENVELOPE_LAYOUT_VERSION`) = D-GV2-5.**
111-
- **D-GV2-2** `family → Codebook` registry = **episodic basin** (the codebook + the basin's accumulated supporting edges), sibling of `classid → ClassView` in `lance-graph-ontology`: `LazyLock`/Lance-backed, masked-load lookup, head-only. 256-entry cap + split-on-overflow guard. Mixin = O(1) reference to this basin (`E-MIXIN-IS-AN-ADDRESS-REFERENCE-NOT-A-COPY`).
111+
- **D-GV2-2** **PARTIAL — type + in-memory registry SHIPPED** (`contract::codebook`, feature `guid-v2-tail`): `Codebook` (insertion-ordered `index ↔ label`, 1-byte index, `CODEBOOK_CAP=256` with overflow→`None` split-signal) + `FamilyCodebookRegistry` (`family → Codebook`, `intern`/`resolve(family,index)`/per-family scoping). The finer sibling of `classid → ClassView`; the family node's episodic-basin content (`E-MIXIN-IS-AN-ADDRESS-REFERENCE-NOT-A-COPY`); the 256×256 Morton tile, ≤256 leaves for the 1-byte index. 3 tests (dedup/sequential, overflow-split, per-family scoping). **DEFERRED:** the `LazyLock`/Lance-backed persistence + `OntologyRegistry` integration in `lance-graph-ontology` (the heavyweight runtime tier).
112112
- **D-GV2-3** `soa_graph` per-family edge resolution: 12 in-family = 1-byte own-codebook index, 4 out-of-family = `(family,index)`; retire `family & 0xFF` collision-skip under v2.
113113
- **D-GV2-4** `aiwar` re-keyed on `leaf` (coarse node-type, 5 hubs) + per-family codebook (System/Stakeholder/… vocabularies) → resolves the "60 noisy families" on real data.
114114
- **D-GV2-5** cutover: flip default after the gating numbers + downstream (none today) confirmed; v1 → `#[deprecated]` no-op path with migration pointer.
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
//! `codebook` — per-family codebook (D-GV2-2, feature `guid-v2-tail`).
2+
//!
3+
//! The finer sibling of [`class_view`](crate::class_view) (`classid → ClassView`):
4+
//! here **`family → Codebook`**. Each family owns a ≤256-entry vocabulary that its
5+
//! nodes index by a **1-byte in-family adapter** — the 256×256 Morton centroid
6+
//! tile of `E-UNIFORM-MORTON-TILE-PYRAMID`, with ≤256 leaves for the 1-byte index.
7+
//!
8+
//! Why per-family (not one global codebook): it dissolves the aiwar "60 noisy
9+
//! families" at the root — each family's vocabulary is small and clean, and a
10+
//! within-family reference is an **exact** index into that family's codebook (no
11+
//! `& 0xFF` low-byte aliasing). The `family` tier (u16) selects the codebook
12+
//! (head-only routing); the 1-byte index resolves within it. Cross-family edges
13+
//! carry `(family, index)` and decode via [`FamilyCodebookRegistry::resolve`].
14+
//!
15+
//! A family that outgrows 256 entries **splits** (mint a sub-family — cheap with
16+
//! the v2 16-bit family tier), never widens the byte ([`Codebook::intern`]
17+
//! returns `None` on overflow as the split signal). The codebook is the family
18+
//! node's **episodic basin** content (`E-MIXIN-IS-AN-ADDRESS-REFERENCE-NOT-A-COPY`):
19+
//! members reference it by the 1-byte index, the shared vocabulary lives once.
20+
//!
21+
//! This module is the TYPE + in-memory registry (the `LazyLock` tier). The
22+
//! Lance-backed persistence + `OntologyRegistry` integration are deferred to the
23+
//! ontology-crate wiring step (see plan `guid-v2-tail-per-family-codebook-v1.md`).
24+
25+
use std::collections::HashMap;
26+
27+
/// Max entries per family codebook — the 1-byte in-family index cap. A family
28+
/// that needs more SPLITS (mint a sub-family), never widens the byte.
29+
pub const CODEBOOK_CAP: usize = 256;
30+
31+
/// A per-family codebook: insertion-ordered label interning, `index ↔ label`.
32+
/// `index` is the 1-byte in-family adapter value (`0..len`). ≤[`CODEBOOK_CAP`].
33+
#[derive(Debug, Clone, Default, PartialEq, Eq)]
34+
pub struct Codebook {
35+
entries: Vec<String>, // index → label (insertion order)
36+
by_label: HashMap<String, u8>, // label → index
37+
}
38+
39+
impl Codebook {
40+
/// An empty codebook.
41+
pub fn new() -> Self {
42+
Self::default()
43+
}
44+
45+
/// Intern `label` → its 1-byte index (insertion order, deduped). Returns
46+
/// `None` if the codebook is full (256) and `label` is new — the caller must
47+
/// SPLIT the family (the `CODEBOOK_CAP` overflow signal). An already-present
48+
/// label always resolves (even at capacity).
49+
pub fn intern(&mut self, label: &str) -> Option<u8> {
50+
if let Some(&i) = self.by_label.get(label) {
51+
return Some(i);
52+
}
53+
if self.entries.len() >= CODEBOOK_CAP {
54+
return None;
55+
}
56+
let i = self.entries.len() as u8;
57+
self.entries.push(label.to_string());
58+
self.by_label.insert(label.to_string(), i);
59+
Some(i)
60+
}
61+
62+
/// The 1-byte index of `label`, if interned.
63+
pub fn index_of(&self, label: &str) -> Option<u8> {
64+
self.by_label.get(label).copied()
65+
}
66+
67+
/// The label at `index`, if present.
68+
pub fn label(&self, index: u8) -> Option<&str> {
69+
self.entries.get(index as usize).map(String::as_str)
70+
}
71+
72+
/// Number of interned entries.
73+
pub fn len(&self) -> usize {
74+
self.entries.len()
75+
}
76+
77+
/// Whether the codebook holds no entries.
78+
pub fn is_empty(&self) -> bool {
79+
self.entries.is_empty()
80+
}
81+
82+
/// Whether the codebook is at [`CODEBOOK_CAP`] (a new label would overflow →
83+
/// split the family).
84+
pub fn is_full(&self) -> bool {
85+
self.entries.len() >= CODEBOOK_CAP
86+
}
87+
}
88+
89+
/// `family → Codebook` — the per-family codebook registry, the finer sibling of
90+
/// `classid → ClassView`. In-memory (the `LazyLock` tier); a Lance-backed,
91+
/// `OntologyRegistry`-integrated variant is deferred.
92+
#[derive(Debug, Clone, Default)]
93+
pub struct FamilyCodebookRegistry {
94+
books: HashMap<u16, Codebook>,
95+
}
96+
97+
impl FamilyCodebookRegistry {
98+
/// An empty registry.
99+
pub fn new() -> Self {
100+
Self::default()
101+
}
102+
103+
/// The codebook for `family`, creating an empty one if absent.
104+
pub fn entry(&mut self, family: u16) -> &mut Codebook {
105+
self.books.entry(family).or_default()
106+
}
107+
108+
/// The codebook for `family`, if it exists (read-only).
109+
pub fn get(&self, family: u16) -> Option<&Codebook> {
110+
self.books.get(&family)
111+
}
112+
113+
/// Intern `label` into `family`'s codebook → its 1-byte index. `None` on
114+
/// codebook overflow (split the family).
115+
pub fn intern(&mut self, family: u16, label: &str) -> Option<u8> {
116+
self.entry(family).intern(label)
117+
}
118+
119+
/// Resolve a cross-family reference `(family, index)` → label — the decode of
120+
/// an out-of-family adapter / `references` edge.
121+
pub fn resolve(&self, family: u16, index: u8) -> Option<&str> {
122+
self.books.get(&family).and_then(|cb| cb.label(index))
123+
}
124+
125+
/// Number of families with a codebook.
126+
pub fn families(&self) -> usize {
127+
self.books.len()
128+
}
129+
}
130+
131+
#[cfg(test)]
132+
mod tests {
133+
use super::*;
134+
135+
#[test]
136+
fn intern_dedups_and_assigns_sequential_indices() {
137+
let mut cb = Codebook::new();
138+
assert_eq!(cb.intern("Nation"), Some(0));
139+
assert_eq!(cb.intern("TechCompany"), Some(1));
140+
assert_eq!(cb.intern("Nation"), Some(0)); // dedup
141+
assert_eq!(cb.len(), 2);
142+
assert_eq!(cb.index_of("TechCompany"), Some(1));
143+
assert_eq!(cb.label(0), Some("Nation"));
144+
assert_eq!(cb.label(9), None);
145+
}
146+
147+
#[test]
148+
fn codebook_overflow_signals_split() {
149+
let mut cb = Codebook::new();
150+
for i in 0..CODEBOOK_CAP {
151+
assert!(cb.intern(&format!("e{i}")).is_some());
152+
}
153+
assert!(cb.is_full());
154+
// a NEW label overflows → None (split the family)…
155+
assert_eq!(cb.intern("one_too_many"), None);
156+
// …but an already-interned label still resolves at capacity.
157+
assert_eq!(cb.intern("e0"), Some(0));
158+
}
159+
160+
#[test]
161+
fn registry_scopes_codebooks_per_family() {
162+
// The SAME label gets INDEPENDENT indices in different families — the
163+
// whole point of per-family scoping (no global contamination).
164+
let mut reg = FamilyCodebookRegistry::new();
165+
assert_eq!(reg.intern(0x0001, "Issue"), Some(0));
166+
assert_eq!(reg.intern(0x0001, "Bug"), Some(1));
167+
assert_eq!(reg.intern(0x0002, "Issue"), Some(0)); // family 2's own index 0
168+
assert_eq!(reg.families(), 2);
169+
// cross-family resolve (family, index) → label
170+
assert_eq!(reg.resolve(0x0001, 1), Some("Bug"));
171+
assert_eq!(reg.resolve(0x0002, 0), Some("Issue"));
172+
assert_eq!(reg.resolve(0x0099, 0), None); // unknown family
173+
}
174+
}

crates/lance-graph-contract/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ pub mod callcenter;
4646
pub mod cam;
4747
pub mod canonical_node;
4848
pub mod class_view;
49+
/// D-GV2-2 — per-family codebook (`family → Codebook`), gated on the v2 tail.
50+
#[cfg(feature = "guid-v2-tail")]
51+
pub mod codebook;
4952
pub mod codegen_manifest;
5053
pub mod codegen_spine;
5154
pub mod cognitive_shader;

0 commit comments

Comments
 (0)