Skip to content

Generalize get_multi_membership_edges into member_of_edges with caller-supplied filters - #3383

Merged
mstomar125 merged 2 commits into
mainfrom
issue-3343-member-of-edges
Sep 30, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-3343-member-of-edges

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Closes #3343

Part of the Pro/core boundary epic (#3298). Implements ADR-083 §4 decision 5-B (proposed).

What changed

SqliteStore::get_multi_membership_edges() becomes SqliteStore::member_of_edges(min_memberships, exclude_member_types).

  • The query now describes only the graph: every member_of edge, as (member_id, collection_id), whose member has at least min_memberships member_of edges and whose node_type is not in exclude_member_types.
  • The policy the old query baked in moves to the caller: the threshold of 2 and the exclusion of person members. A caller reproduces the old result with member_of_edges(2, &["person"]).
  • The threshold binds as ?1 and is clamped to at least 1. The excluded types bind as ?2.., one placeholder each, and the NOT IN clause is only added when the slice is non-empty.
  • The doc names no caller, and the error context is neutral (Failed to read member_of edges).
  • The only non-core caller was the Pro daemon's membership sweep in nodespace-sync. Breaking it is accepted per the standing decision. When sync next bumps its pin it switches its one call site to member_of_edges(2, &["person"]).

Tests (collection_service.rs)

All three compare edges as sets, since the order is unspecified.

  • member_of_edges_with_threshold_two_skips_single_memberships is the old test, renamed and rewritten in graph terms.
  • member_of_edges_excludes_listed_member_types adds the local person and a content node to two collections. With (2, &[]) both nodes' edges appear. With (2, &["person"]) exactly the person's edges disappear. An unknown type excludes nothing.
  • member_of_edges_threshold_below_one_behaves_as_one: (0, &[]) equals (1, &[]), and both include a single-membership node's edge.

Deviation from the issue text

The exclusion test also asserts &["person", "text"] excludes both nodes' edges. The issue's stated risk is a placeholder numbering error in the dynamic NOT IN list, and a single-type test cannot see one, so this case is what covers ?3 and beyond.

Mutation checks

Each mutation was applied to the production SQL/code alone, the three tests were run, and the change was reverted.

Mutation Result
Drop the exclusion clause (and its params) member_of_edges_excludes_listed_member_types fails on "the person's edges are absent"; the other two pass
Hard-code the threshold to > 1 member_of_edges_threshold_below_one_behaves_as_one fails on "a threshold of 1 includes a single-membership node's edge"; the other two pass
Hard-code the threshold to >= 1 member_of_edges_with_threshold_two_skips_single_memberships fails on the edge-set equality; the other two pass
Honour only the first excluded type member_of_edges_excludes_listed_member_types fails on "every listed type is excluded"; the other two pass

The .max(1) clamp on the threshold is not distinguishable by any test: every GROUP BY group has at least one row, so >= 0 and >= 1 return the same rows. It is kept because the documented contract says a threshold below 1 behaves as 1, and that holds either way.

Verification

  • nextest run -p nodespace-core --lib member_of_edges: 3 passed.
  • cargo fmt -p nodespace-core, and cargo clippy -p nodespace-core --all-targets -- -D warnings -D clippy::unused-async: clean.
  • bun run test:changed: passed (test:scripts and the Rust workspace nextest run). Frontend and skill tiers were not reached by this diff.
  • git grep -n get_multi_membership_edges prints nothing across the repository.
  • No added line mentions cloud, push, a sweep or RBAC, and none names an issue number.

Pro-boundary ratchet

scripts/check-pro-boundary.ts is not on main yet (its PR is still open), so there is no BASELINES to edit here. I measured the effect with the ratchet's own MARKERS from that PR's branch, counting the two changed files at origin/main against this branch:

  • cloudSyncHooks: 4 -> 0 (the method, the old test's doc, its name, and its call).
  • No other marker changes, and none rises.

Whoever lands second should tighten cloudSyncHooks by 4 in BASELINES. The embedding-seam removal touches the same marker, so both PRs would edit the same baseline line and the merge queue would eject the second one on conflict. The check is one-way, so leaving the ceiling where it is fails nothing.

Risks and rollback

A placeholder numbering error would fail loudly in the tests. Rollback is a revert of this PR.

🤖 Generated with Claude Code

@mstomar125

Copy link
Copy Markdown
Contributor Author

Pragmatic code review (initial review, medium depth)

Review Type: Initial Review

Requirements Check

  • Step 4 grep prints nothing: met. git grep -n get_multi_membership_edges returns no output over the whole repository, and finds no multi_membership identifier and no other caller of member_of_edges.
  • No core doc or identifier mentions cloud, push, a sweep or RBAC: met. All 177 added lines were checked; the only hits were async (contains "sync"). The doc names no caller, and the error context is "Failed to read member_of edges". Cold-sweep and cloud-sync wording remains in relationships.rs on other methods; those lines are untouched and out of scope.
  • All three tests pass: met (3 of 3, re-run by the reviewer).
  • member_of_edges(2, &["person"]) returns the same result as the old method: met (see equivalence check below).
  • Ratchet criterion (check-pro-boundary.ts exits 0, BASELINES equals printed counts): deviation documented. The script is not on main, so it cannot be done here. The reviewer re-measured with the ratchet's own cloudSyncHooks pattern on the two files: 4 matching lines on origin/main, 0 on HEAD, and no other marker moves.
  • Core builds and the free app's collections are unchanged: met as far as review can see; the diff touches only this query and its tests. The reviewer did not re-run test:changed; that result rests on the author's run.

Code Review Summary

The change is small, correct and idiomatic for this codebase. No bugs found.

SQL equivalence (checked against origin/main)

  • The JOIN node n ON n.id = r.in_node and the relationship_type = 'member_of' filters (outer query and subquery) are unchanged.
  • HAVING COUNT(*) >= ?1 with ?1 = 2 is identical to the old > 1. idx_rel_unique on (in_node, out_node, relationship_type) means COUNT(*) counts distinct collections.
  • NOT IN ('person') is identical to != 'person'; node.node_type is TEXT NOT NULL, so NULL semantics cannot diverge.
  • Orphan-edge behaviour is unchanged: the inner JOIN drops orphans and the subquery still counts them.

Placeholders and binding

  • ?1 is the threshold and the excluded types are ?2..=?(n+1); SQLite numbered parameters bind by index.
  • A Vec<libsql::Value> matches the existing pattern in conflicts.rs.
  • usize to i64 via try_from(..).unwrap_or(i64::MAX) saturates safely (a saturated threshold matches nothing) and is safer than the as i64 casts used elsewhere.
  • An empty slice omits the clause and adds no params. Duplicate and empty-string types are valid SQL.
  • More than about 32k types would fail loudly with "too many SQL variables"; no caller passes more than a handful.
  • No injection surface: the only interpolated text is ?N built from integer indices; every type goes through Value::Text.

Tests

  • All three are non-tautological and order-independent (they compare HashSets). The ["person","text"] case covers ?3.
  • The .max(1) clamp is confirmed unobservable: every GROUP BY in_node group has at least one row, so >= 0 and >= 1 return the same rows. This is disclosed in the PR body.

Findings

Critical Issues

  • None

Suggested Improvements

  • None

Nitpicks

  • Nit: collection_service.rs, member_of_edges_threshold_below_one_behaves_as_one: the name and doc claim to cover "below 1 behaves as 1", but .max(1) cannot be observed; the test really guards the >= ?1 threshold at 1. No change needed since the issue mandates this test and the PR discloses it. A one-line comment on the clamp in relationships.rs saying it only states the documented contract would stop a future reader "fixing" the test.
  • Nit: collection_service.rs, member_of_edges_excludes_listed_member_types: the derived expected set holds only while the local person is the sole person node with at least two memberships. If seeding ever adds a second person, the test fails loudly rather than passing silently, so it is acceptable as is.

Recommendation: APPROVE

@mstomar125 mstomar125 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated pragmatic-code-review: APPROVE (posted as a comment, since GitHub blocks self-approval). Full findings in the review comment above.

@mstomar125

Copy link
Copy Markdown
Contributor Author

Address-review summary

Review verdict was APPROVE with no critical or improvement findings and two nits.

  • Addressed: 1 of 2.
    • Nit on the .max(1) clamp: added a two-line comment in relationships.rs saying every member has at least one edge, so the clamp only states the documented contract. Comment-only, commit 566b0de.
  • Skipped: 1 of 2.
    • Nit on the derived expected set in member_of_edges_excludes_listed_member_types: it holds only while the local person is the sole person node with two or more memberships. If seeding ever adds a second person node the test fails loudly rather than passing silently, and the reviewer judged it acceptable as is. Revisit if seeding changes.
  • The below-one test keeps its name and doc: the issue mandates that test, and the PR body already discloses that the clamp itself is not observable.

Checks after the change: cargo fmt -p nodespace-core --check clean, and member_of_edges nextest 3 of 3 passing. No code path changed, so test:changed and clippy from the earlier run still stand.

Re-Review Decision

Decision: NO RE-REVIEW NEEDED

Rationale: the only change since the review is a two-line comment with no logic change. Nothing in the diff the reviewer approved was altered.

mstomar125 and others added 2 commits September 30, 2026 12:44
…shold and excluded member types

The query now describes only the graph: every member_of edge whose member has
at least min_memberships member_of edges and whose node_type is not in
exclude_member_types. The threshold of two and the exclusion of person members
move to the caller. Adds tests for the type exclusion, including several types
and an unknown one, and for a threshold below one.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…the documented contract

Comment-only. Every member has at least one edge, so a threshold of 0 already
matches 1; the clamp is kept because the doc promises it.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generalize get_multi_membership_edges into member_of_edges with caller-supplied filters

1 participant