Skip to content

reverse_cardinality is declared and surfaced but never enforced, so a 'one' target end accepts unlimited edges #2804

Closed

Description

@malibio

create_relationship enforces cardinality on the declaring (forward) side but never enforces reverse_cardinality on the target side, so a relationship declared as one-per-target accepts unlimited edges into that target.

The gap

packages/core/src/services/node_service/relationship.rs:625:

if relationship.cardinality == RelationshipCardinality::One {
    let existing_count = self.store
        .check_relationship_exists(source_id, relationship_name).await?;
    if existing_count > 0 {
        return Err(NodeServiceError::invalid_update(format!(
            "Relationship '{}' has cardinality 'one' but an edge already exists",
            relationship_name
        )));
    }
}

It reads relationship.cardinality and counts edges out of source_id. There is no counterpart that reads reverse_cardinality and counts edges into target_id. A second, identical check exists at line 905 with the same one-sidedness.

Concrete violation

person declares (core_schemas.rs:1301):

name: "tasks", target_type: Some("task"), direction: Out,
cardinality: Many,               // a person has many tasks
reverse_name: "assignee",
reverse_cardinality: One,        // a task has ONE assignee — never enforced

Because the forward cardinality is Many, the One branch never fires. So both of these succeed:

create_relationship(alice, "tasks", task1, json!({}))  // ok
create_relationship(bob,   "tasks", task1, json!({}))  // ALSO ok — task1 now has two assignees

task1 now violates its own declared reverse_cardinality: One. The same applies to reported_tasks/creator (reverse_cardinality: One) and to project↔task.

Why it matters

reverse_cardinality is not merely decorative — it is surfaced to consumers (rel_ops.rs:629 hands it out as the inbound group's cardinality) and is documented on the type as a load-bearing invariant:

Carrying both halves in the type — not merely in a validator — is what makes "every stored edge is named from both ends" an invariant every reader can rely on instead of a convention that holds only on the paths which happen to route through validation.

Anything reasoning over the inbound cardinality (UI affordances, agent tools, Plays, CEL paths like node.assignee.email) is trusting a constraint the store does not guarantee. A node.assignee path resolving against two edges has no defined answer.

This is a prerequisite for cardinality-driven UI affordances (see companion issue): a single-select "assign" control is a fiction if any other writer can create a second edge.

Decision needed

When a One end already has an edge, should a new one be rejected (matching the current forward-side behavior) or replace the existing edge (what "assign" implies in a UI)? The answer should be the same on both sides — if reverse is replace, forward arguably should be too. Recorded here rather than decided unilaterally because it changes the semantics of an existing, shipped check.

Acceptance criteria

  • create_relationship enforces reverse_cardinality == One by counting existing edges into the target for that relationship type
  • Both call sites (relationship.rs:625 and :905) are covered
  • The chosen semantics (reject vs. replace) are applied consistently to forward and reverse checks
  • Error message names the target end and the reverse relationship name, so it reads correctly from the side the caller is on
  • Test: two create_relationship(person_n, "tasks", same_task) calls — second is rejected (or replaces, per the decision)
  • Test: the forward One case still behaves as before
  • Existing person_task_assignee_relationship_assigns_and_clears_end_to_end (node_service/mod.rs:8207) still passes

Activity

  1. self-assigned this
    on Sep 23, 2026
  2. mstomar125 commented on Sep 23, 2026

    @mstomar125
    Contributor

    Frontend baseline: 5263 passed, 1 failed (pre-existing flaky perf test: bulk structural operations 100 indent/outdent, unrelated to this issue).

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