Skip to content

Replace the derived type_ancestry and structural_rule tables with views over their source #4016

Closed as not planned

Description

@malibio

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 the extends edges between schema nodes. It exists because triggers can't call the type registry (ADR-086 §5) and SQLite does not allow a WITH clause inside a trigger body.
  • structural_rule is the declared rules of ADR-089 (children_none, children_except, must_be_root, parent_of), filled from each non-core schema node's $.children and $.parent properties by json_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

  1. Replace the type_ancestry table and its 7 triggers with a view of the same name and columns (node_type, ancestor, depth), built as a recursive walk over the extends edges. Callers and the structural-rule triggers keep joining against type_ancestry.
  2. Establish whether structural_rule can also be a view over schema nodes' children / parent properties. 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.
  3. Measure the write path before and after. Every has_child insert and retype runs these lookups inside the trigger. Compare insert throughput and the query plan on a database with tens of thousands of edges.
  4. 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 extends relationship, single parent, additive only; already resolves the closure with WITH RECURSIVE for 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_ancestry is 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 extends edge 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_child inserts and retypes on a large database, posted on this issue
  • ADR-086 §5 and ADR-089 updated in nodespace-docs
  • bun run test:changed passes, bun run quality:fix clean

Technical Specifications

Reference Files

  • packages/core/src/db/schema.rs - type_ancestry and structural_rule DDL and trigger builders, TYPE_ANCESTRY_TABLE, STRUCTURAL_RULE_TABLE
  • packages/core/src/db/schema.rs - is_a_sql callers and has_child_checks
  • packages/core/tests/it/schema_test.rs - schema tests that pin the DDL
  • packages/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_type was measured about 50x slower in relationship_paths.rs.
  • A view can't back a partial index. Check that no index depends on type_ancestry.

Non-Goals

Related Issues

Activity

  1. self-assigned this
    on Nov 3, 2026
  2. mstomar125 commented on Nov 3, 2026

    @mstomar125
    Contributor

    Picking this up (sweep drain).

  3. mstomar125 commented on Nov 3, 2026

    @mstomar125
    Contributor

    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

  4. malibio commented on Nov 3, 2026

    @malibio
    CollaboratorAuthor

    The design is now recorded: ADR-102 (Accepted), which supersedes ADR-086 §5 in part and amends ADR-089. It keeps structural_rule a table until the measurement in this issue meets the stated criterion. The companion docs change this issue called for is on docs main.

    🤖 Generated with Claude Code

  5. mstomar125 commented on Nov 5, 2026

    @mstomar125
    Contributor

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions