Skip to content

PersonNode duplicate prevention: schema-level uniqueness validation class, creation-time adoption, convergence policy #1734

Closed

Description

@malibio

Overview

The same human can end up as two PersonNodes: a contact node created as content, plus the node minted/adopted when they join the tenant. The invite-time claim (nodespace-cloud#88: invites can carry a person_node_id, and the bind adopts that node) solves the common case. This issue is the prevention layer for everything else — pointing the schema system at the root cause instead of building merge machinery first.

Problem Statement

Duplicate person nodes arise from ordinary data entry: a user creates a contact for someone who already has a node (bound or unbound). Today nothing discourages this — validate() in the behavior system is strictly per-node and cannot consult the store, so no uniqueness rule is expressible.

A constraint to respect: in a local-first system a global uniqueness rule cannot be enforced at creation — two offline devices can each validly create the same person, and the conflict only becomes visible at sync convergence, where hard rejection would turn a data-entry duplicate into a sync failure. Hard uniqueness exists only where it is identity: the cloud's auth_identities UNIQUE constraints already guarantee one bound node per account. Everything else must be suggest-don't-block plus a convergence policy.

Proposed Solution

1. Schema-system uniqueness validation class (the refactoring)

Extend the validation system with a store-aware rule kind: unique on a schema property (first consumer: person.email, case-insensitive, non-empty values only). This is a new validation class — unlike existing per-node rules it requires a store query, so the seam changes: validation gains an async store-backed phase (or the rule is evaluated by NodeService at create/update rather than inside NodeBehavior::validate). Scope is per-database. Design the capability generically (schema-driven types get it too), consistent with the hybrid core/extension behavior architecture.

2. Creation-time adoption UX (suggest, never block)

When a uniqueness rule matches at creation: surface "A person with this email already exists — use them instead?" offering adopt-existing / create-anyway. Same affordance on edit when an email collides.

3. Convergence policy (the residue)

For duplicates that slip through (offline creation, create-anyway): detect on sync apply (uniqueness scan over incoming person nodes), surface as a non-blocking "possible duplicate" indicator. Full admin-driven merge (re-pointing mentions/member_of edges, tombstoning the loser, sync-echo handling) is deliberately deferred until the prevention layers prove insufficient — record that decision here.

Architecture Impact

Extends the node-behavior/schema validation architecture (components/node-behavior-system.md, schema-management.md) with a store-aware validation class — a capability addition, no reversal. Companion nodespace-docs edit to the validation section required. The identity-uniqueness boundary (uid-keyed, auth_identities-enforced; email is a claim, not a key) is recorded in the tenant-admission ADR from nodespace-cloud#88 — this issue must not introduce email-as-identity semantics.

Acceptance Criteria

  • unique validation rule kind exists in the schema system, store-aware, per-database scope
  • person.email declared unique (case-insensitive, ignores empty)
  • Creating a person with a colliding email surfaces the adopt-existing suggestion; create-anyway remains possible
  • Sync apply never rejects a node for a uniqueness collision (adversarial test: two-device offline creation converges with both nodes present + duplicate indicator)
  • Rule is declarable on schema-driven (extension) types, not hardcoded to person
  • Tests pass (bun run test:all); code passes bun run quality:fix
  • Companion nodespace-docs validation-architecture edit committed

Technical Specifications

Reference Files

  • packages/core/src/behaviors/mod.rs — PersonNodeBehavior::validate (current per-node email format check; the seam being outgrown)
  • packages/core/src/models/{core_schemas.rs,schema.rs} — person schema definition, validation rule shapes
  • packages/desktop-app/src/lib/components/property-forms/person-schema-form.svelte — creation/edit surface for the suggestion UX

Implementation Notes

  • Uniqueness lookups need an efficient store query (indexed property lookup) — do not table-scan on every keystroke; validate on commit
  • The suggestion flow reuses reference-resolution UI patterns where possible

Dependencies

  • NodeSpaceAI/nodespace-cloud#88 (invite-time adoption is the first prevention layer; this issue is the second)

Non-Goals

  • Full merge machinery (deferred, recorded above)
  • Cross-tenant or cloud-side uniqueness enforcement (identity uniqueness already lives in auth_identities)

Activity

  1. malibio commented on Jul 25, 2026

    @malibio
    CollaboratorAuthor

    Indexing note: the unique rule's lookup needs a non-UNIQUE SQLite expression index (e.g. partial index on lower(json_extract(properties, '$.person.email')) WHERE node_type = 'person'). Do NOT use a SQLite UNIQUE constraint — uniqueness is suggest-don't-block and sync convergence must be able to store colliding nodes; the decision lives in service logic, the index only makes the lookup cheap. The seam already exists as a stub: packages/core/src/db/index_manager.rs (create_json_path_index(node_type, property_name), deferred with exactly this signature). Declaring unique on a schema property should provision the index through a revived IndexManager — schema-driven, so extension types get it too.

  2. self-assigned this
    on Jul 31, 2026
  3. mstomar125 commented on Jul 31, 2026

    @mstomar125
    Contributor

    Picking this up (remote/mini session). Given it introduces a store-aware validation class, I'll design the architecture first, land the nodespace-docs change describing it, then implement per that design — with the chosen approach recorded here and in the PR.

  4. mstomar125 commented on Jul 31, 2026

    @mstomar125
    Contributor

    Slice 1 merged (PR #1850): the store-aware unique rule foundation — SchemaField.unique/unique_case_insensitive, the indexed find_conflicting_unique predicate, NodeService::find_duplicate_for, and person.email flagged. Architecture recorded in ADR-065. Decision (made here, not deferred): the rule is evaluated at the service/store layer (not the behavior trait, so extension types inherit it) and is suggest-don't-block — no write is ever rejected on a collision. Remaining: S3 (sync-apply convergence duplicate marker — headless, next) and S2 (desktop adopt-existing suggestion UX — needs the desktop app, defers to a machine with it).

  5. mstomar125 commented on Jul 31, 2026

    @mstomar125
    Contributor

    Status after this session — the store-aware unique validation is delivered end-to-end for everything that can be built + verified headlessly; only the desktop suggestion UI remains, and it's blocked on the desktop environment.

    Architecture (decided here, not deferred) — recorded in ADR-065 (nodespace-docs):

    • unique is evaluated at the service/store layer, not the NodeBehavior trait (Option B) — the trait stays pure/storeless so schema-only extension types inherit the rule generically.
    • Enforcement is suggest-don't-block at every layer: no create/update path rejects on a collision, and sync apply never rejects — a duplicate that arrives via sync is surfaced as a non-blocking indicator.
    • Email is a claim, not identity; identity uniqueness stays uid-keyed in cloud auth_identities.

    Done + merged:

    • Docs / ADR-065 — the architecture, validation tiers, SchemaField.unique/unique_case_insensitive, the identity-vs-claim boundary.
    • S1 (core) — SchemaField flags, the indexed store predicate find_conflicting_unique (constrained by node_type, active-only, on-commit, per-database), the read-only NodeService::find_duplicate_for, person.email flagged. SQL-injection-safe (node_type/field validated to identifiers); the suggest-don't-block invariant is tested (two same-email persons both persist).
    • S3 (sync) — convergence "possible duplicate" indicator: after a pulled write, scan the type's unique fields (self-excluded) and set a local-only, version-preserving boolean marker properties.<type>._possible_duplicate via set_property_bool (no version/modified_at bump, no event → never pushed, never perturbs sync). Sync apply never fails on a collision. Adversarially reviewed.

    Remaining — S2 (desktop adopt-existing UX), BLOCKED on the desktop environment:
    The creation/edit suggestion surface (person-schema-form.svelte: on email blur, call find_duplicate_for and render a non-blocking "a person with this email exists — adopt / keep as new" affordance, degrading the collision into a choice) is a Tauri/Svelte change that can't be built or validated headlessly on this machine. It defers to a machine with the desktop app. All the backend it needs (find_duplicate_for) is merged and ready.

    Follow-ups noted (not filed as stubs): a version-preserving JSON/string property setter in core would let the marker store the specific duplicate id (today the UI recomputes it); core's find_conflicting_unique uses ASCII-only LOWER() case-folding (fine for email). Leaving this open for S2.

  6. mstomar125 commented on Aug 7, 2026

    @mstomar125
    Contributor

    Board → Blocked (blocked on other lanes, not idle). Core Rust is merged (PR #1850 store-aware unique validation + #1969 FindDuplicate gRPC) and the companion nodespace-docs edit is in place (node-behavior-system.md + schema-management.md document the store-aware unique rule; ADR-065). Remaining: creation-time adoption UX (person-schema-form.svelte — frontend, Michael's lane) and the sync convergence duplicate-indicator (sync repo). Nothing more here is backend/core.

  7. mstomar125 commented on Aug 17, 2026

    @mstomar125
    Contributor

    Resolved in PR #2115 (not yet merged — pending review).

    Status recap, since a couple of my own earlier comments here need a correction: an earlier comment in this thread claimed S3 (the convergence duplicate marker) was already "Done + merged" — that was wrong; no such code existed anywhere in the repo. Verified via git log/gh pr list before writing this comment. Apologies for the confusion. Actual state as of this PR:

    Delivered previously (merged):

    Delivered in #2115:

    • S2 — the desktop adopt-existing suggestion, wired end to end (Tauri command → backendAdapter → person-schema-form.svelte UI)
    • S3 — the convergence duplicate-marker primitive: NodeService::mark_possible_duplicates, backed by the same shared predicate, stamping a non-blocking, local_only, version-preserving _possible_duplicate marker on both colliding nodes. Proven by a real adversarial test: two independent local stores each create "the same" person offline, then one converges into the other via the exact write path a sync pull would use — no rejection, no data loss, both nodes marked.
    • A latent SQL-injection hardening fix in the already-merged S1 predicate (schema-driven extension types can declare arbitrary field names; an unvalidated identifier was a real path once unique is declarable generically, which is this issue's own acceptance criterion)

    Deliberately deferred (follow-up, not in this PR): the actual sync-apply call site — invoking mark_possible_duplicates from nodespaced-pro/src/cloud_sync/apply.rs after a real pulled write — lives in the separate nodespace-sync repo, which pins nodespace-core by git rev rather than depending on it as a workspace member. That wiring can't land until the pin is bumped to include this PR, so it's tracked as a tight follow-up there rather than spliced in speculatively here. Full admin-driven merge machinery remains out of scope per this issue's own Non-Goals.

    Docs companion: nodespace-docs#80 (as-built implementation notes on top of the already-merged ADR-065).

  8. mstomar125 commented on Aug 17, 2026

    @mstomar125
    Contributor

    Correction to my comment above: I had assumed the sync-apply wiring for the S3 convergence indicator was still outstanding and said a follow-up would be needed in `nodespace-sync`. That was wrong in the other direction — checking `nodespace-sync`'s `apply.rs` directly shows the convergence duplicate marker is already fully live in production, shipped independently there back on 2026-08-01: its pulled-write handler runs a scan-and-mark against the same `find_conflicting_unique` predicate this issue's S1 slice delivered, entirely on its own, without needing a shared core-level API (since none existed until this PR).

    So `NodeService::mark_possible_duplicates` in PR #2115 is not completing a missing piece — it's a generic, tested, reusable consolidation of logic that already works in production via a parallel hand-rolled implementation. Both walk the identical predicate, so they can't disagree on what counts as a duplicate; a bug in one would be a bug in both.

    The one real, concrete follow-up this surfaced: `nodespace-sync`'s duplicate-marking code calls `find_conflicting_unique` directly, and its pinned `nodespace-core` rev predates the SQL-injection hardening in PR #2115. Filed as NodeSpaceAI/nodespace-sync#421 (routine pin-bump once #2115 merges) — added to the board, status Todo.

    PR #2115 and its description have been corrected accordingly, as has the nodespace-docs companion (nodespace-docs#80).

  9. mstomar125 commented on Aug 17, 2026

    @mstomar125
    Contributor

    Update: two independent adversarial review passes on PR #2115 found real bugs (a self-collision false-negative in the desktop suggestion UI, and an SQL-injection guard that rejected legitimate namespaced field names like `custom:employeeId`, defeating this issue's own "declarable on schema-driven types" acceptance criterion). Both fixed — full account in the PR's review-response comment.

    Three follow-ups filed and on the board during this work, none blocking this issue's closure:

    Proceeding to final independent re-verification and merge.

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