Skip to content

create_relationship stores writes through an in-direction name with reversed endpoints, giving one edge two storage shapes #2941

Closed

Description

@malibio

Overview

Found while fixing core#2921 (PR #2935). create_relationship accepts a write through an in-direction declaration's own name and stores the edge with its endpoints the wrong way round. The same logical edge then has two storage shapes, and every reader has to handle both.

Problem Statement

Edges are stored once: relationship_type is the forward (out) name, in_node is the source, and out_node is the target. An in-direction declaration is the target's view of that edge. Its reverseName is the forward name. For example, adr declares supersedes (out) and superseded_by (in, reverseName supersedes).

create_relationship_in_tx resolves the name through resolve_declared_relationship (packages/core/src/services/node_service/relationship.rs:488). That lookup uses SchemaNode::get_relationship(name) and ignores direction. So create_relationship("old", "superseded_by", "new") succeeds and stores in_node=old, out_node=new, relationship_type="superseded_by". Nothing records it as the reverse spelling of new --supersedes--> old. The CLI and the agent tools can reach this: rel_ops::create_relationship passes the name through unchanged.

Consequences:

  • check_node_completeness has to check both shapes for a required in relationship. PR Make check_node_completeness direction-aware for required in-relationships #2935 added the check for the in-name shape for exactly this reason.
  • Forward reads miss it: get_related_nodes(new, "supersedes", "in") does not see an edge written this way, and neither does any other query keyed on the forward name.
  • Cardinality: cardinality / reverse_cardinality are enforced against the forward name. An edge stored under the in name skips the one limits on both ends.
  • Deletion guard: the last-edge removal guard (see the linked follow-up) would also have to handle two shapes.

Proposed Solution

Normalize on write. In the create path, when the name resolves to a direction: in declaration, swap source and target and store the edge under the declaration's reverseName (the forward name). Then validate it exactly like a forward write: target type, cardinality on both ends, and reverse_cardinality. Callers can still write whichever spelling reads naturally, but only one storage shape exists.

(Rejecting in-name writes with a helpful error naming the forward spelling is the alternative. Normalizing is preferred because agents and CLI users naturally phrase edges from either end.)

Once only one shape exists, delete the in-name lookup that PR #2935 added to check_node_completeness (packages/core/src/services/node_service/schema.rs). Per CLAUDE.md, write no backfill for existing in-name edges: reset the database.

Acceptance Criteria

  • create_relationship(a, <in name>, b) stores the same row as create_relationship(b, <forward name>, a)
  • Cardinality and reverse_cardinality are enforced for writes through an in name
  • The update and delete paths resolve an in name the same way, so an edge written through an in name can be removed through it
  • The in-name lookup is removed from check_node_completeness; required_inbound_relationship_written_through_in_name_reports_complete still passes (it now exercises the normalized shape)
  • Code passes bun run quality:fix
  • Tests pass (bun run test:all)

Non-Goals

Last-edge protection for required in relationships is tracked separately.

Activity

  1. malibio commented on Sep 25, 2026

    @malibio
    CollaboratorAuthor

    Follow-up that depends on this: #2942 (last-edge protection for required in-direction relationships). Doing this one first means that guard has only one edge shape to check.

  2. malibio commented on Sep 25, 2026

    @malibio
    CollaboratorAuthor

    Frontend baseline: 264 files / 5286 tests passed

  3. self-assigned this
    on Sep 25, 2026
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