Repository navigation
Description
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_completenesshas to check both shapes for a requiredinrelationship. 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_cardinalityare enforced against the forward name. An edge stored under theinname skips theonelimits 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 ascreate_relationship(b, <forward name>, a) - Cardinality and
reverse_cardinalityare enforced for writes through aninname - The update and delete paths resolve an
inname the same way, so an edge written through aninname can be removed through it - The in-name lookup is removed from
check_node_completeness;required_inbound_relationship_written_through_in_name_reports_completestill 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
malibio commentedon Sep 25, 2026
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.
malibio commentedon Sep 25, 2026
Frontend baseline: 264 files / 5286 tests passed