Repository navigation
Description
Overview
The schema keeps two tables that hold nothing of their own: type_ancestry and structural_rule. Each is a copy of facts that already live elsewhere (the extends edges in relationship, and the children / parent properties on schema nodes). Seven triggers keep type_ancestry in step and three keep structural_rule in step. Any write path those triggers miss, and any change to how a rule is declared, can leave the copy disagreeing with its source. This issue removes the copies by computing them from the source on read, so there is nothing to keep in step.
Problem Statement
type_ancestry(packages/core/src/db/schema.rs) is the transitive closure of theextendsedges between schema nodes. It exists because triggers can't call the type registry (ADR-086 §5) and SQLite does not allow aWITHclause inside a trigger body.structural_ruleis the declared rules of ADR-089 (children_none,children_except,must_be_root,parent_of), filled from each non-core schema node's$.childrenand$.parentproperties byjson_each, and from the registry for core types.- Both are derived data. ADR-086 lists "one more thing kept in step with the schema graph" as a known cost.
A spike on both libsql and the turso crate showed a trigger can read a view containing WITH RECURSIVE over relationship and see the full chain (bug → issue → task), so the closure needs no stored copy. The existing idx_rel_in / idx_rel_out indexes serve the walk.
Proposed Solution
- Replace the
type_ancestrytable and its 7 triggers with a view of the same name and columns (node_type,ancestor,depth), built as a recursive walk over theextendsedges. Callers and the structural-rule triggers keep joining againsttype_ancestry. - Establish whether
structural_rulecan also be a view over schema nodes'children/parentproperties. The open point is the core types: their rows come from the registry today. Either the seeded core schema nodes carry the same properties and the view covers them, or core types need another source. Decide by looking at how the rows are seeded; if the view cannot cover core types, say so on the issue and leave the table. - Measure the write path before and after. Every
has_childinsert and retype runs these lookups inside the trigger. Compare insert throughput and the query plan on a database with tens of thousands of edges. - Update
node-types.md, ADR-086 §5 and ADR-089 so they describe a view, not a maintained table.
Documentation
- ADR-086 §5 - decides the type-ancestry table; this issue supersedes that part
- ADR-089 - structural rules and their SQL enforcement through the ancestry table
- ADR-078 - the
extendsrelationship, single parent, additive only; already resolves the closure withWITH RECURSIVEfor queries - Updates: node-types.md, if its per-type reference mentions either table
Architecture Impact
This changes a decision. ADR-086 §5 says: "a type-ancestry table maintained from the schema extends edges in the same transaction". This issue replaces the maintained table with a view. A companion nodespace-docs change is required: an edit to ADR-086 §5 and ADR-089 (or a new ADR superseding them), committed to docs main before the code merges.
Acceptance Criteria
-
type_ancestryis a view; its 7 maintenance triggers are removed - Every existing structural-rule and subtype test passes unchanged, including a subtype that inherits a base type's rule and a two-level chain
- An
extendsedge that would create a cycle is refused or shown to be refused elsewhere; the view must not loop - A written decision on
structural_rule(view, or table kept with the reason), with the check of how core types' rows are seeded - Before/after numbers for
has_childinserts and retypes on a large database, posted on this issue - ADR-086 §5 and ADR-089 updated in nodespace-docs
-
bun run test:changedpasses,bun run quality:fixclean
Technical Specifications
Reference Files
packages/core/src/db/schema.rs-type_ancestryandstructural_ruleDDL and trigger builders,TYPE_ANCESTRY_TABLE,STRUCTURAL_RULE_TABLEpackages/core/src/db/schema.rs-is_a_sqlcallers andhas_child_checkspackages/core/tests/it/schema_test.rs- schema tests that pin the DDLpackages/core/src/db/sqlite_store/relationship_paths.rs- the existing recursive-walk idiom and its query-plan tests
Notes
- Create-schema is the only thing that builds a database and nothing carries an older shape forward: change the DDL and reset the database. No migration step.
- Keep the walk's access path on the endpoint indexes. A JOIN that lets SQLite drive the walk from
idx_rel_typewas measured about 50x slower inrelationship_paths.rs. - A view can't back a partial index. Check that no index depends on
type_ancestry.
Non-Goals
- Moving structural checks out of SQL triggers into Rust
- The
node_title_ftsandvec_embeddingstables - Switching database engine (see Spike: can Turso Database (the Rust SQLite rewrite) replace libsql in the local store #4015)
Related Issues
- Spike: can Turso Database (the Rust SQLite rewrite) replace libsql in the local store #4015 (the engine spike that surfaced this)
Activity
mstomar125 commentedon Nov 3, 2026
Picking this up (sweep drain).
mstomar125 commentedon Nov 3, 2026
Measured on PR branch batch-c23 (debug build, 10k has_child inserts then 1k retypes). 24 types: 1.43s/0.38s before, 9.0s/2.0s with the view (~6x). 200 types: ~1.5s/0.4s before, 86s/17s with the view (~58x). SQLite cannot push the node_type filter into a recursive CTE, so each trigger reference computes the whole closure. Per ADR-102 this needs a decision before merge. structural_rule stays a table.
🤖 Generated with Claude Code
malibio commentedon Nov 3, 2026
The design is now recorded: ADR-102 (Accepted), which supersedes ADR-086 §5 in part and amends ADR-089. It keeps
structural_rulea table until the measurement in this issue meets the stated criterion. The companion docs change this issue called for is on docsmain.🤖 Generated with Claude Code
mstomar125 commentedon Nov 5, 2026
Decision: keep type_ancestry (and structural_rule) as maintained tables. The view cost too much on the hot write path: has_child inserts and retypes were about 6x slower with 24 types and about 58x slower with 200 types, because SQLite cannot push the node_type filter into the recursive walk. This is the revisit that ADR-102 asked for before the table is brought back. The draft PR is closed; the ADR amendment records the decision.
🤖 Generated with Claude Code