Skip to content

Mark rename-path collection-name collisions and scope lookup to active collections - #2139

Merged
mstomar125 merged 2 commits into
mainfrom
issue-2127-collection-dedup-gaps
Aug 18, 2026
Merged

mstomar125 merged 2 commits into
mainfrom
issue-2127-collection-dedup-gaps

Conversation

@mstomar125

Copy link
Copy Markdown
Contributor

Summary

Closes #2127.

Follow-up to #2126 (create-path suggest-don't-block marking for collection-name collisions). #2126's adversarial review deferred two related gaps to keep that PR scoped to the wedge fix; this PR closes both.

Gap 1 — rename path never marks a collision

SqliteStore::create_node detects a collection-name collision and marks both sides with the non-blocking _possible_duplicate marker. SqliteStore::update_node and update_node_with_version_check had no equivalent check at all — this predates #2117/#2126, but #2126 newly made the asymmetry visible: create-time collisions are surfaced, rename-time ones silently weren't.

Both update_node and update_node_with_version_check now run the same detect-before-write / mark-after-write logic create_node uses, triggered when the write changes content on an (effectively) collection-typed node:

  • Detection happens before the write (same TOCTOU-accepting, best-effort posture as create_node — see mark_collection_name_collision's doc comment).
  • The node's own id is excluded from the pre-write match, so a no-op rename or a case-only rename ("Foo" → "foo", which folds to the same LOWER(title)) never matches and marks itself.
  • Marking only happens after the write actually lands — update_node_with_version_check skips it entirely on a version conflict (rows_affected == 0).
  • Covers both entry points nodespace-sync's apply_node_upsert and other callers reach: the version-checked path (NodeService::update_node, used by the sync-apply "node already exists locally" branch) and the plain path (NodeService::update_node_unchecked).

Gap 2 — get_collection_by_name didn't filter by lifecycle_status

get_collection_by_name matched LOWER(title) with no lifecycle_status filter, unlike find_conflicting_unique (the ADR-065 person.email mechanism this marker is modeled on), which restricts to lifecycle_status = 'active'. Practical consequence: archiving a collection to free up its name didn't actually free it up — a new collection (or a rename) reusing that name would be found colliding against the archived one and get permanently flagged, with no way to distinguish "colliding with a live collection" from "colliding with an archived one."

Added AND lifecycle_status = 'active' to get_collection_by_name's query, matching find_conflicting_unique's pattern. This method has several other callers (collection_ops::create_collection/rename_collection's interactive guards, CollectionService::resolve_path/find_collection_by_path) — all of them only ever look up active collections in existing tests and callers, so this is a behavior-narrowing fix with no observed fallout, not a widening one.

Also updated

Doc comments that described both gaps as known/deferred limitations: mark_collection_name_collision's doc comment (now describes all three call sites), CollectionNodeBehavior's doc comment, and the _possible_duplicate schema field's description in core_schemas.rs.

Tests

In packages/core/tests/collection_name_convergence_test.rs:

  • Flipped the existing gap-documenting test (update_path_introducing_a_name_collision_is_currently_unmarked → update_path_introducing_a_name_collision_is_now_marked) to assert the fixed behavior via the version-checked path.
  • Added unchecked_update_path_introducing_a_name_collision_is_marked — same scenario through NodeService::update_node_unchecked (the plain SqliteStore::update_node path).
  • Added case_only_rename_never_self_marks — regression for the self-exclusion fix.
  • Added archived_collection_does_not_block_or_mark_name_reuse — regression for gap 2, covering both the create path and the rename path reusing an archived collection's name.

All 9 tests in that file pass, along with the full collection_membership_test.rs suite (32 tests, unaffected by the lifecycle_status filter since all existing lookups are against active collections) and the full cargo test -p nodespace-core suite (1154 unit tests + all integration binaries + doc-tests, 0 failures). cargo clippy -p nodespace-core --all-targets is clean. cargo fmt clean.

Test plan

  • cargo test -p nodespace-core --test collection_name_convergence_test — 9/9 pass
  • cargo test -p nodespace-core --test collection_membership_test — 32/32 pass
  • cargo test -p nodespace-core (full suite) — 0 failures
  • cargo clippy -p nodespace-core --all-targets — clean
  • cargo fmt -p nodespace-core -- --check — clean
  • Local pre-push gate (test:all + cargo build --bin nodespaced + test:e2e + Tauri-seam integration suite) — passed

🤖 Generated with Claude Code

…e collections (closes #2127)

Follow-up to #2126 (create-path suggest-don't-block marking). Closes two
deferred gaps its adversarial review surfaced:

- SqliteStore::update_node and update_node_with_version_check now detect a
  collection-name collision the same way create_node does — before the
  write, excluding the node's own id so a no-op or case-only rename never
  self-marks — and mark both sides with the existing _possible_duplicate
  marker after a successful write. Covers both the version-checked path
  (NodeService::update_node, used by nodespace-sync's apply_node_upsert
  "node already exists locally" branch) and the plain update_node path
  (NodeService::update_node_unchecked).

- get_collection_by_name now filters to lifecycle_status = 'active',
  matching the pattern find_conflicting_unique already established for the
  ADR-065 unique-field mechanism. An archived collection no longer holds
  its name, so archiving one and creating (or renaming) another collection
  onto that name is no longer detected as a collision.

Updates the doc comments this touches (mark_collection_name_collision,
CollectionNodeBehavior, the _possible_duplicate schema field description)
that previously described both gaps as known limitations. Flips the test
that documented the rename-path gap to assert the fixed behavior, and adds
regression tests for the unchecked update path, case-only-rename
self-exclusion, and archived-collection name reuse on both the create and
rename paths.

Co-Authored-By: Claude <[email protected]>
…hten tests

- collection_ops::create_collection's AlreadyExists guard previously checked
  get_collection_by_name (now active-only per Gap 2), but the id it inserts
  under is deterministic_collection_id(name) — a pure hash of the normalized
  name, independent of lifecycle_status. Recreating a collection with an
  archived collection's exact name therefore passed the guard and then hit a
  raw primary-key-constraint violation on insert (NodeService::create_node has
  no get-or-create semantics for the collection type), surfacing as an opaque
  OpsError::Internal instead of a clean rejection. Fixed by checking node
  existence by the deterministic id directly (any node at that id, active or
  archived, already occupies it) rather than by name.
- update_node's unwrap_or(current.content.clone()) now uses unwrap_or_else to
  avoid an unconditional clone on the common Some(content) path.
- Documented the invariant the self-exclusion filter's correctness relies on
  (a self-match via LIMIT 1's unordered result can only occur when a genuine
  third-party collision would already have been caught earlier).
- Added regression tests: create_collection_op_rejects_reusing_an_archived_collections_name
  (the PK-collision fix), and
  case_only_rename_never_self_marks_via_unchecked_path (closes a coverage gap
  where the two update entry points implement self-exclusion independently).

Co-Authored-By: Claude <[email protected]>
@mstomar125

Copy link
Copy Markdown
Contributor Author

Adversarial review complete — REQUEST CHANGES, addressed in the follow-up commit.

Blocker found and fixed: collection_ops::create_collection's AlreadyExists guard used get_collection_by_name (now active-only per gap 2), but the id it inserts under (deterministic_collection_id(name)) is a pure hash of the normalized name, independent of lifecycle_status. Recreating a collection with an archived collection's exact name therefore passed the (now-narrower) guard and then hit a raw primary-key-constraint violation on insert — NodeService::create_node has no get-or-create semantics for the collection node type — surfacing as an opaque OpsError::Internal instead of a clean rejection. Fixed by checking node existence by the deterministic id directly (any node at that id, active or archived, already occupies it) instead of by name. Added create_collection_op_rejects_reusing_an_archived_collections_name, which fails against the pre-fix code with the PK-violation error and passes after.

Also addressed the review's minor findings:

  • update_node's unwrap_or(current.content.clone()) now uses unwrap_or_else to avoid an unconditional clone on the common Some(content) path.
  • Documented the invariant the self-exclusion filter's correctness relies on (a self-match via LIMIT 1's unordered result can only occur when a genuine third-party collision would already have been caught earlier — reviewed and confirmed by the adversarial pass).
  • Added case_only_rename_never_self_marks_via_unchecked_path, closing a coverage gap where the two update entry points implement self-exclusion independently (not shared code).

Confirmed correct by the review and unchanged: rename_collection's guard (updates by existing id, no PK-collision risk), CollectionService::resolve_path (uses the separate, untouched get_collections_by_names), the partial-move fixes, write-ordering, and no marker-clearing logic introduced.

Full suite (cargo test -p nodespace-core: 1154 unit + all integration + doc-tests) and cargo clippy --all-targets clean after the fix. Pushed as 3b2dd4a3.

@mstomar125
mstomar125 merged commit 679e6ac into main Aug 18, 2026
@mstomar125
mstomar125 deleted the issue-2127-collection-dedup-gaps branch August 18, 2026 08:53
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.

Collection-name possible-duplicate marking: rename path unmarked + lifecycle_status not filtered

1 participant