Skip to content

Store relationship_type's reverse name as a queryable column on the relationship edge table #2638

Closed

Description

@malibio

Overview

SchemaRelationship's name/reverse_name pair is the schema-definition-level naming for both ends of a relationship (e.g. tasks/project, extends/extended_by — see packages/nodespace-types/src/schema.rs). But the relationship edge table (packages/core/src/db/schema.rs) has only a single relationship_type TEXT column — it stores name (the declaring side's label) directly as relationship_type, and reverse_name only inside the row's serialized properties JSON blob (set_schema_declarations, packages/core/src/db/sqlite_store/relationships.rs, serializes the full SchemaRelationship struct into properties). There is no column a query can use to find or filter edges by their reverse label.

This is worse than just "one extra JSON-decode step" for two of NodeSpace's four built-in structural relationships (has_child, member_of, mentions, has_role — packages/core/src/models/schema.rs's BUILTIN_RELATIONSHIP_NAMES): those were never migrated onto the SchemaRelationship naming convention at all. They predate it, are declared as bare reserved strings, and never get a properties blob written with a reverse_name populated — so for a built-in relationship, the reverse name isn't just hard to query, it doesn't exist anywhere as data at all. has_child's reverse direction (child → parent) is reachable today only through hardcoded, relationship-specific Rust methods (NodeService::hierarchy's get_parent/get_children), not through any generic, named, relationship-table-level mechanism.

Ownership principle this issue extends, not introduces: per ../nodespace-docs/decisions/070-schema-relationship-declarations-are-edges.md (ADR-070), relationship data belongs to the relationship table, never to either endpoint's schema — SchemaNode.relationships is a read-time hydration/projection over that table's rows, never storage itself. This issue is squarely a fix to that shared table's column shape (adding reverse_relationship_type alongside relationship_type), consistent with ADR-070's ownership model, not a change to it or a departure from it.

Problem Statement

Anything that wants to do generic, named relationship traversal — not hardcoded per-relationship Rust methods, but "resolve the edge with reverse name X" — has no column to query against. Concretely, this blocks the playbook engine's Play-condition path resolver (graph_resolver.rs) from ever supporting "find my parent via has_child" as a named path segment the way it already supports forward traversal of any SchemaRelationship-declared relationship by name.

Proposed Solution

Add a reverse_relationship_type TEXT column to the relationship table, in packages/core/src/db/schema.rs's SCHEMA_SQL:

Updated by #2688. This issue originally specified a v006_... migration following v004_schema_relationship_edges.rs. That ladder no longer exists — #2688 collapsed it into a single current-shape bootstrap, and CLAUDE.md now states there is no migration ladder and no schema version. Add the column to the bootstrap DDL and reset the database.

  • For SchemaRelationship-declared edges (tasks/project, extends/extended_by, and any user-declared relationship), set_schema_declarations (and its transactional counterpart set_schema_declarations_in_tx) writes rel.reverse_name into this column at the same time it writes rel.name into relationship_type — no more relying on a properties JSON decode to learn the reverse label.
  • For the four built-in structural relationships, the reverse names are decided (see the spec-update comment for rationale): has_child → child_of, member_of → has_member, mentions → mentioned_by, has_role → role_of. Each is wired at the write sites that create those edges: has_child → child_of is the immediately-needed case (motivated by a Play-condition use case in a separate issue); member_of, mentions, has_role should each get a reverse name decided during this issue's implementation, for consistency, even if no consumer needs them immediately.
  • Per this repo's pre-release policy (see CLAUDE.md — no backward-compatibility code, no data migrations/backfills), this issue adds the column and populates it going forward from write paths only. It does not backfill reverse_relationship_type for existing rows from their properties JSON — NodeSpace has no production data to preserve; reset the database to pick up the new column being populated correctly from write time onward.

Architecture Impact

No existing ADR documents the relationship table's column shape as fixed or asserts properties-JSON-only storage for reverse_name is intentional — this is closing an oversight from when SchemaRelationship's name/reverse_name pair was introduced, not a reversal of a documented decision.

Companion nodespace-docs update required: ../nodespace-docs/components/typed-relationships.md (lines 19-21, its "One stored edge, two readings" section at lines 56-73) currently documents reverseName as real and load-bearing ("makes an edge read naturally from the side that did not declare it") but describes it only as a properties-JSON field, matching today's actual (gap-having) implementation — it never states the reverse name is recoverable only via a JSON decode, not a queryable column, and it says nothing about built-in structural relationships (has_child etc.) having no reverse name as data at all, since that document is scoped to schema-declared relationships. Once this issue lands, that section must be updated to: (1) state reverse_relationship_type is a first-class column, not something requiring a properties decode, and (2) document the decided reverse names for all four built-in structural relationships. ../nodespace-docs/architecture/data-layer.md (lines 14, 38, 65, 79) should also gain the new column in its relationship table description.

Acceptance Criteria

  • relationship table gains a reverse_relationship_type TEXT column in packages/core/src/db/schema.rs's SCHEMA_SQL
  • set_schema_declarations/set_schema_declarations_in_tx write reverse_name into the new column for every SchemaRelationship-declared edge
  • has_child edges are written with reverse_relationship_type = 'child_of' at every site that creates a has_child edge (confirm all call sites — likely hierarchy.rs's child-creation/move paths)
  • member_of, mentions, has_role each get a decided, documented reverse name, written at their respective edge-creation sites
  • New column is queryable/indexed appropriately for lookup by reverse name (mirror the existing idx_rel_type/idx_rel_in/idx_rel_out index pattern if a new index is warranted)
  • No backfill of existing rows — confirmed via test that a fresh database populates the column correctly from write time, not via any migration-time backfill logic
  • ../nodespace-docs/components/typed-relationships.md and ../nodespace-docs/architecture/data-layer.md updated per Architecture Impact above
  • Tests pass (bun run test:all)
  • Code passes bun run quality:fix

Technical Specifications

Reference Files

  • packages/core/src/db/schema.rs — the single current-shape schema bootstrap; holds the relationship table DDL and is where the new column goes
  • packages/core/src/db/sqlite_store/relationships.rs — set_schema_declarations (~line 1547), set_schema_declarations_in_tx (~line 1664) — write relationship_type from rel.name today; must also write the new column from rel.reverse_name
  • packages/nodespace-types/src/schema.rs (~line 238-260) — SchemaRelationship.name/reverse_name — the existing schema-definition-level pair this column now mirrors at the storage level
  • packages/core/src/models/schema.rs (~line 56) — BUILTIN_RELATIONSHIP_NAMES — the four structural relationships needing decided reverse names
  • packages/core/src/services/node_service/hierarchy.rs — get_parent/get_children, create_parent_edge/create_parent_edge_in_tx — has_child edge-creation sites needing the new column populated
  • ../nodespace-docs/decisions/070-schema-relationship-declarations-are-edges.md (ADR-070) — the ownership principle (relationship table, not either schema, owns relationship data) this issue's fix is consistent with
  • ../nodespace-docs/components/typed-relationships.md (lines 19-21, 56-73) — needs updating per Architecture Impact above
  • ../nodespace-docs/architecture/data-layer.md (lines 14, 38, 65, 79) — needs updating per Architecture Impact above

Dependencies

None — this can land independently.

Related Issues

Non-Goals

  • Backfilling reverse_relationship_type for existing rows — explicitly out of scope per this repo's no-data-migration policy
  • Adding reverse-traversal support to graph_resolver.rs's Play-condition path resolver itself — that consumer-side work belongs to Seed a core Play that completes a parent task when all its children are done #2636, not this issue, which only makes the reverse name exist as queryable data

Activity

  1. malibio commented on Sep 17, 2026

    @malibio
    CollaboratorAuthor

    Baseline before implementation — Frontend: 247 test files passed, 5085 tests passed (117.70s).

  2. self-assigned this
    on Sep 17, 2026
  3. malibio commented on Sep 17, 2026

    @malibio
    CollaboratorAuthor

    Spec update following #2688

    The v006 migration this issue calls for no longer exists as a concept. #2688 collapsed the five-step migration ladder into a single current-shape bootstrap (packages/core/src/db/schema.rs), deleting v001–v005, the user_version ratchet, and the backup system. CLAUDE.md now states explicitly that there is no migration ladder and no schema version.

    This issue's Technical Specifications cited v004_schema_relationship_edges.rs as the precedent migration to follow. That file was a data migration — exactly what the no-data-migration policy bans — and following it faithfully here would have meant writing a backfill of reverse_relationship_type from existing properties JSON. That was the trigger for filing #2688.

    Restated acceptance criteria

    The first criterion:

    • relationship table gains a reverse_relationship_type TEXT column via a new migration (v006_...), following v004_schema_relationship_edges.rs's structure

    becomes:

    • relationship table gains a reverse_relationship_type TEXT column in packages/core/src/db/schema.rs's SCHEMA_SQL, alongside an index if lookup by reverse name warrants one

    The "no backfill of existing rows" criterion stands unchanged and is now structurally enforced — there is no migration mechanism left that could backfill. Reset the database to pick up the new column.

    All other criteria are unaffected.

    Reverse names for the four built-in structural relationships

    The issue leaves member_of, mentions, and has_role to be decided during implementation. Recording the decision here rather than only in a commit, since these become user-visible query keys once #2641 lands and renaming one later is a breaking change to every call site:

    forward reverse
    has_child child_of (fixed by this issue)
    member_of has_member
    mentions mentioned_by
    has_role role_of

    Rule: has_X → X_of, verbs take the passive. This makes has_child/child_of and has_role/role_of invert identically. member_of → has_member necessarily breaks that pattern (it is already named from the member's side); contains was considered and rejected as too easily confused with has_child, which is the actual containment/outline edge.

    These are only the built-ins. Schema-declared relationships already carry an author-supplied SchemaRelationship.reverse_name, which is required rather than derived precisely because naming an inverse is a modeling decision — so set_schema_declarations just writes that value into the new column. No derivation, no lookup table for user relationships.

    Scope note

    17 production INSERT sites write relationship_type today (13 hardcode a built-in as a SQL literal, 2 are generic paths taking rel_type as a parameter, 2 are set_schema_declarations), plus 2 test-fixture sites. The issue's "likely hierarchy.rs's child-creation/move paths" understates this — the sites are spread across sqlite_store/nodes.rs, sqlite_store/relationships.rs, and services/node_service/hierarchy.rs.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions