Skip to content

C5: Replace the client-side 5s conflict heuristic with the daemon's authoritative OCC version conflict (amends ADR-026) #1268

Closed

Description

@malibio

Spun off from: architectural-audit tracking issue #1228, finding C5.
Priority: backend + ui (cross-layer). Failure mode is silent data loss — high-priority within those.

Finding (from #1228)

Last-Write-Wins conflict resolution is incomplete. shared-node-store.svelte.ts always discards local; hardcoded 5s detection window; nodeType-conversion special-case bypasses detection. Concurrent edits outside the window are silently lost.
Action: move conflict adjudication to the daemon (return version + conflict marker in RPC error); revisit OT/CRDT (already flagged "Phase 7" in code).

ADR grounding (read first)

  • ADR-026: Write Flow & Conflict Resolution — OCC + Last-Write-Wins — the existing decision. C5 amends it. §"Conflict Detection: Version-Based OCC" (the OCC contract C5 honors) and §"Conflict Resolution: Last-Write-Wins" (the Recovered-Items / diff-merge flow that was never built and that C5 explicitly defers). ADR-026's "client detects empty SurrealDB result" detection model predates the daemon/gRPC architecture and is obsolete.
  • ADR-027: Local-First Sync Service — confirms ADR-026's OCC+LWW mechanics "remain valid and unchanged"; the sync service is another writer through the same daemon OCC, so fixing the local path covers sync-originated conflicts. No nodespace-sync changes here.
  • ADR-031: Daemon-Client Architecture — the daemon owns OCC adjudication (it already does). ADR-026's client-side detection model is obsolete under this.
  • Context: ADR-025, ADR-017.

Verified current state (the actual gap)

The daemon already detects version conflicts authoritatively (B6/#1257). The problem is the structured conflict is thrown away at two boundaries before it can drive client behavior, and a weaker client-only heuristic is layered on top.

  1. Ops layer populates current_node correctly: packages/core/src/ops/node_ops.rs:272-289 — on VersionConflict, re-fetches and embeds OpsError::VersionConflict { node_id, expected, actual, current_node } (ops/mod.rs:29-33).
  2. Daemon discards current_node at the gRPC boundary: packages/daemon/src/services/node_service.rs:1365-1373 uses { node_id, expected, actual, .. } — the .. drops current_node; everything collapses to a flat Status::aborted string. Test error_mapping_version_conflict_returns_aborted (:1668) only asserts the code, not the payload.
  3. Tauri command carries no structured data: packages/desktop-app/src-tauri/src/commands/nodes.rs:108-122 → CommandError { code: "VERSION_CONFLICT", message, details: "Aborted" }. No expected/actual/current_node.
  4. Frontend OCC branch is DEAD CODE: shared-node-store.svelte.ts:1237,1858 call isVersionConflict(dbError) to trigger rollback + resyncNodeFromServer. But isVersionConflict (types/errors.ts:124-134) checks for the MCP/JSON-RPC shape code === -32005 + data.type === 'VersionConflict' — the removed in-process MCP layer. The gRPC daemon produces { code: "VERSION_CONFLICT" } (string), which never matches, so the rollback/resync path never fires; the conflict falls through to the generic catch.
  5. The 5s heuristic can't detect cross-client conflicts at all: detectConflict (:2301-2353) compares against the client's own pendingUpdates within conflictWindowMs = 5000 (:514, :2325-2326). pendingUpdates only holds this client's updates → structurally blind to other writers. The window is also a guess: two real edits 6s apart are declared "not a conflict" and LWW silently drops the earlier (the exact Architectural audit (post-daemon + post-SQLite): structural findings & improvement plan #1228 failure mode).
  6. nodeType-conversion + computed-field bypasses (:2335-2339, :864-869, :2290) exist only to suppress false positives from the broken time-window heuristic.
  7. UI surface already exists (Implement conflict resolution UI for concurrent edits #1231): conflictNotifications + CONFLICT_MESSAGE (:51-55, stores/conflict-notifications.svelte.ts) with 'version-mismatch'. C5 feeds it real conflicts.

Target architecture

Daemon is the single adjudicator (ADR-031); client consumes the verdict, doesn't guess.

  1. Daemon carries the structured conflict across gRPC — stop flattening VersionConflict to a string; surface node_id/expected/actual/current_node (add a proto VersionConflict message — preferred — or encode in Status details).
  2. Tauri command preserves the structured payload in CommandError, keeping code: "VERSION_CONFLICT".
  3. Frontend consumes it and surfaces via the Implement conflict resolution UI for concurrent edits #1231 UI — on conflict: rollback optimistic update, hydrate from returned current_node (fallback resyncNodeFromServer), push conflictNotifications ('version-mismatch'). LWW stays the policy, but the losing edit is now visible, not silently dropped.
  4. Delete the 5s heuristic — conflictWindowMs, setConflictWindow, the timeDiff < conflictWindowMs / 'concurrent-edit' branch.
  5. Remove the now-unnecessary bypasses — with detection version-based, pattern conversions / computed-field writes no longer false-positive; delete the skipConflictDetection auto-sets and nodeType special-case (keep skipPersistence for computed fields — separate concern). Verify no caller depends on skipConflictDetection for a non-conflict reason first.

OT/CRDT — explicitly DEFERRED

The "Phase 7: OT/CRDT" comment (:22) stays a comment. ADR-026 §"Why OCC + LWW Over CRDTs" already argues node-level (not character-level) editing makes CRDTs over-engineered for v1 — unchanged, no real-time co-editing driver pre-release. Correct target: honest LWW with visible version-conflict surfacing. No OT/CRDT/merge engine. Record the deferral in the ADR amendment.

Exact changes

Daemon (packages/core + packages/daemon): define a structured conflict carrier across gRPC; update the VersionConflict arm in daemon/.../node_service.rs:1365-1373 to stop dropping current_node. Confirm current_node is populated on all conflict paths (node update + task-node update).
Tauri (src-tauri/.../nodes.rs): extend status_to_command_error so Aborted preserves {expected, actual, current_node}; keep code: "VERSION_CONFLICT".
Frontend (packages/desktop-app/src/lib):

  • types/errors.ts: delete the MCP -32005/VersionConflictData/isVersionConflict machinery (80-134); replace with a gRPC-shaped guard matching CommandError { code: "VERSION_CONFLICT", … } carrying the structured fields. Update tests/types/errors.test.ts.
  • services/shared-node-store.svelte.ts: rewire the two OCC sites (:1237, :1858) to the new guard (rollback + hydrate from current_node + conflictNotifications.add); delete conflictWindowMs/setConflictWindow/time-window branch; delete the bypasses (:864-869, :2290, :2335-2339) after confirming no non-conflict caller relies on them.
  • Verify conflict-notifications.svelte.ts 'version-mismatch' rendering is reached. createLogger not console.

Acceptance criteria

  • Two writers edit the same node concurrently; the second receives VERSION_CONFLICT, the client rolls back, hydrates from authoritative current_node, and surfaces a conflictNotifications entry. No edit silently lost.
  • A concurrent edit outside the old 5s window (6s+ apart) is now correctly detected (version-based, not time-based).
  • conflictWindowMs/setConflictWindow/'concurrent-edit' branch removed; no references remain.
  • skipConflictDetection bypasses + nodeType special-case removed; pattern conversions + computed-field writes still work and persist.
  • Daemon VersionConflict→Status no longer discards current_node; Tauri command preserves the payload; frontend guard matches the real gRPC shape (old MCP -32005 machinery deleted).
  • LWW remains the policy; the losing edit is surfaced via the Implement conflict resolution UI for concurrent edits #1231 UI, not dropped.
  • ADR-026 amendment authored in nodespace-docs (doc/ branch): daemon-as-adjudicator, deletion of the unbuilt Recovered-Items/diff-merge flow, OT/CRDT deferred, consistency with ADR-027/031.

Testing

  • Rust: daemon integration test — update_node with stale version returns Aborted AND the payload carries current_node/expected/actual. Strengthen error_mapping_version_conflict_returns_aborted to assert the payload. cargo test -p nodespace-core -p nodespaced.
  • Frontend (bun run test, never bun test): concurrent-edit test — mock the adapter to reject update_node with the new VERSION_CONFLICT shape; assert rollback, hydration from current_node, conflictNotifications entry. Regression: two edits >5s apart overlapping on a field now reconcile through the daemon verdict, not silent LWW. Update tests/types/errors.test.ts. Mock Tauri with vi.mock('@tauri-apps/api/core').
  • bun run test:all before PR.

Risks

  • Live-editing write path; failure mode is data loss — the concurrent-edit tests are mandatory gates.
  • Hydrating from current_node must not clobber newer local state — hydrate only after rollback of the conflicting optimistic update; re-apply queued non-conflicting edits via the existing resync flow.
  • Bypass-removal regressions — confirm pattern conversions don't reintroduce false conflicts now that detection is version-based.
  • Two-repo: code in nodespace-core; ADR-026 amendment in nodespace-docs (doc/ branch). Cross-link PRs.

Standard NodeSpace startup / PR sequence

git status + git pull origin main, EnterWorktree({name: "issue-<N>-occ-conflict-adjudication"}), bun install, bun run test baseline, gh:comment/gh:assign/gh:status. createLogger not console; bun run test not bun test. Before PR: bun run test:all, bun run quality:fix + commit (revert unrelated reformats), bun run gh:pr <N>, /pragmatic-code-review, then ExitWorktree + gh pr merge --squash --delete-branch. ADR amendment is a separate small PR in nodespace-docs on a doc/ branch.


Spun off from #1228 (architectural audit), finding C5. Amends ADR-026.

Activity

  1. malibio commented on May 30, 2026

    @malibio
    CollaboratorAuthor

    Frontend baseline: 133 test files, 3866 tests passed

  2. self-assigned this
    on May 30, 2026
  3. added 2 commits that reference this issue on May 30, 2026
    cd1452e
    0af7ec3
  4. added a commit that references this issue on Jun 22, 2026
    6f8eebe
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