Skip to content

Refactor UpdateSource type system for clarity and correctness - #413

Merged
malibio merged 2 commits into
mainfrom
feature/issue-393-refactor-update-source
Nov 5, 2025
Merged

malibio merged 2 commits into
mainfrom
feature/issue-393-refactor-update-source

Conversation

@malibio

@malibio malibio commented Nov 5, 2025

Copy link
Copy Markdown
Collaborator

Closes #393

malibio and others added 2 commits November 5, 2025 12:00
…393)

**Problem:**
The UpdateSource type conflated two orthogonal concepts:
1. Data origin (where did the update come from?)
2. Persistence state (should this be persisted to database?)

This led to:
- Confusing dual meaning of 'database' type
- Misuse of 'external' type as workaround for persistence control
- Navigation service bug using 'external' instead of 'database'
- No explicit API for persistence control

**Solution: Explicit Persistence API (Option C)**

**Phase 1: Add Explicit Persistence Control**
- Added `persist` option to UpdateOptions: false | true | 'debounced' | 'immediate'
- Added `markAsPersistedOnly` option to explicitly track backend-loaded nodes
- Maintains backward compatibility with legacy source.type checks

**Phase 2: Migrate SharedNodeStore**
- Created `determinePersistenceBehavior()` helper method
- Migrated all 3 locations checking `source.type === 'database'`
- updateNode(), setNode(), deleteNode() now use explicit API

**Phase 3: Clarify UpdateSource Semantics**
- Added comprehensive JSDoc explaining data provenance vs persistence
- Documented that UpdateOptions.persist controls persistence, not source.type

**Phase 4: Remove 'external' Type**
- Replaced 'external' workaround in shared-node-store.ts reconciliation
- Replaced 'external' workaround in reactive-node-service.svelte.ts combineNodes
- Updated all test files to use 'database' or 'mcp-server' appropriately
- Removed 'external' from UpdateSource union type

**Phase 5: Add Tests and Documentation**
- Added 3 comprehensive tests for new explicit persistence API
- Tests verify persist: false, markAsPersistedOnly, and legacy behavior
- All existing 1486 tests still pass, now 1490 total (+4 new tests)

**Files Changed:**
- types/update-protocol.ts: New persistence options, removed 'external' type
- services/shared-node-store.ts: Explicit persistence helper + usage
- services/reactive-node-service.svelte.ts: Fixed 'external' workaround
- tests/*: Updated all test files, added comprehensive test coverage

**Test Results:**
- Frontend: ✅ 1490 passed | 48 skipped (1538 total)
- All quality checks pass

**Migration Path:**
- Backward compatible: Legacy `source.type === 'database'` still works
- New code should use explicit `options.persist` instead
- Clear migration path for future MCP integration (Phase 3)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Implemented 2 of 3 review recommendations from PR #413:

1. ✅ API Naming Clarity (🟢 Suggestion):
   - Enhanced JSDoc for persist option to explicitly clarify persist: true vs undefined
   - Added explanation of difference between auto-determined modes
   - Included new example showing persist: true usage for structural changes

2. ✅ Test Coverage Gap (🟢 Suggestion):
   - Added explicit test for persist: true behavior with structural changes
   - Test verifies immediate persistence triggering (not debounced)
   - Documents expected behavior with clear comments
   - Increased test count from 1490 to 1491 (all passing)

3. ⏭️ Migration Documentation (🟢 Suggestion - SKIPPED):
   - Rationale: Inline JSDoc already provides comprehensive migration examples
   - Commit message documents rationale extensively
   - Separate migration guide would duplicate existing documentation
   - Decision: Skip for now, revisit if multiple related migrations accumulate

All quality checks pass, no lint/format issues.

Test Results: ✅ 1491 passed | 48 skipped (1539 total)

Addresses reviewer recommendations from pragmatic-code-review.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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

malibio commented Nov 5, 2025

Copy link
Copy Markdown
Collaborator Author

Review Feedback Addressed

Successfully implemented 2 of 3 review recommendations:

✅ Implemented Changes

1. API Naming Clarity (🟢 Suggestion)

  • Enhanced JSDoc for persist option to explicitly clarify persist: true vs persist: undefined
  • Added detailed explanation of auto-determined mode behavior
  • Included new example showing persist: true usage for structural changes
  • File: src/lib/types/update-protocol.ts:169-196

2. Test Coverage Gap (🟢 Suggestion)

  • Added explicit test for persist: true behavior with structural changes
  • Test verifies immediate persistence triggering (not debounced)
  • Documents expected behavior with clear inline comments
  • Increased test count from 1490 to 1491 (all passing)
  • File: src/tests/services/shared-node-store.test.ts:1314-1334

⏭️ Skipped Recommendation

3. Migration Documentation (🟢 Suggestion - SKIPPED)

  • Rationale: The inline JSDoc in update-protocol.ts already provides comprehensive migration examples with clear before/after patterns. The commit message also documents the refactor rationale extensively. Creating a separate migration guide document would duplicate this information. For a single-refactor change, inline documentation is sufficient and follows DRY principles.
  • Decision: Skip for now, revisit if we accumulate multiple related migrations that would benefit from a unified guide.

Test Results

✅ 1491 passed | 48 skipped (1539 total)
✅ All quality checks pass (no lint/format issues)

Commit

  • SHA: 0be7838
  • Message: "Address review: Improve persist option documentation and test coverage"

Ready for final review. All non-blocking suggestions addressed with thoughtful engineering judgment applied to the skipped recommendation.

@malibio
malibio merged commit 71c4e47 into main Nov 5, 2025
@malibio
malibio deleted the feature/issue-393-refactor-update-source branch November 5, 2025 12:15
malibio added a commit that referenced this pull request Feb 4, 2026
* Refactor UpdateSource type system for clarity and correctness (Issue #393)

**Problem:**
The UpdateSource type conflated two orthogonal concepts:
1. Data origin (where did the update come from?)
2. Persistence state (should this be persisted to database?)

This led to:
- Confusing dual meaning of 'database' type
- Misuse of 'external' type as workaround for persistence control
- Navigation service bug using 'external' instead of 'database'
- No explicit API for persistence control

**Solution: Explicit Persistence API (Option C)**

**Phase 1: Add Explicit Persistence Control**
- Added `persist` option to UpdateOptions: false | true | 'debounced' | 'immediate'
- Added `markAsPersistedOnly` option to explicitly track backend-loaded nodes
- Maintains backward compatibility with legacy source.type checks

**Phase 2: Migrate SharedNodeStore**
- Created `determinePersistenceBehavior()` helper method
- Migrated all 3 locations checking `source.type === 'database'`
- updateNode(), setNode(), deleteNode() now use explicit API

**Phase 3: Clarify UpdateSource Semantics**
- Added comprehensive JSDoc explaining data provenance vs persistence
- Documented that UpdateOptions.persist controls persistence, not source.type

**Phase 4: Remove 'external' Type**
- Replaced 'external' workaround in shared-node-store.ts reconciliation
- Replaced 'external' workaround in reactive-node-service.svelte.ts combineNodes
- Updated all test files to use 'database' or 'mcp-server' appropriately
- Removed 'external' from UpdateSource union type

**Phase 5: Add Tests and Documentation**
- Added 3 comprehensive tests for new explicit persistence API
- Tests verify persist: false, markAsPersistedOnly, and legacy behavior
- All existing 1486 tests still pass, now 1490 total (+4 new tests)

**Files Changed:**
- types/update-protocol.ts: New persistence options, removed 'external' type
- services/shared-node-store.ts: Explicit persistence helper + usage
- services/reactive-node-service.svelte.ts: Fixed 'external' workaround
- tests/*: Updated all test files, added comprehensive test coverage

**Test Results:**
- Frontend: ✅ 1490 passed | 48 skipped (1538 total)
- All quality checks pass

**Migration Path:**
- Backward compatible: Legacy `source.type === 'database'` still works
- New code should use explicit `options.persist` instead
- Clear migration path for future MCP integration (Phase 3)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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

* Address review: Improve persist option documentation and test coverage

Implemented 2 of 3 review recommendations from PR #413:

1. ✅ API Naming Clarity (🟢 Suggestion):
   - Enhanced JSDoc for persist option to explicitly clarify persist: true vs undefined
   - Added explanation of difference between auto-determined modes
   - Included new example showing persist: true usage for structural changes

2. ✅ Test Coverage Gap (🟢 Suggestion):
   - Added explicit test for persist: true behavior with structural changes
   - Test verifies immediate persistence triggering (not debounced)
   - Documents expected behavior with clear comments
   - Increased test count from 1490 to 1491 (all passing)

3. ⏭️ Migration Documentation (🟢 Suggestion - SKIPPED):
   - Rationale: Inline JSDoc already provides comprehensive migration examples
   - Commit message documents rationale extensively
   - Separate migration guide would duplicate existing documentation
   - Decision: Skip for now, revisit if multiple related migrations accumulate

All quality checks pass, no lint/format issues.

Test Results: ✅ 1491 passed | 48 skipped (1539 total)

Addresses reviewer recommendations from pragmatic-code-review.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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

---------

Co-authored-by: Claude <[email protected]>
malibio added a commit that referenced this pull request Feb 5, 2026
* Refactor UpdateSource type system for clarity and correctness (Issue #393)

**Problem:**
The UpdateSource type conflated two orthogonal concepts:
1. Data origin (where did the update come from?)
2. Persistence state (should this be persisted to database?)

This led to:
- Confusing dual meaning of 'database' type
- Misuse of 'external' type as workaround for persistence control
- Navigation service bug using 'external' instead of 'database'
- No explicit API for persistence control

**Solution: Explicit Persistence API (Option C)**

**Phase 1: Add Explicit Persistence Control**
- Added `persist` option to UpdateOptions: false | true | 'debounced' | 'immediate'
- Added `markAsPersistedOnly` option to explicitly track backend-loaded nodes
- Maintains backward compatibility with legacy source.type checks

**Phase 2: Migrate SharedNodeStore**
- Created `determinePersistenceBehavior()` helper method
- Migrated all 3 locations checking `source.type === 'database'`
- updateNode(), setNode(), deleteNode() now use explicit API

**Phase 3: Clarify UpdateSource Semantics**
- Added comprehensive JSDoc explaining data provenance vs persistence
- Documented that UpdateOptions.persist controls persistence, not source.type

**Phase 4: Remove 'external' Type**
- Replaced 'external' workaround in shared-node-store.ts reconciliation
- Replaced 'external' workaround in reactive-node-service.svelte.ts combineNodes
- Updated all test files to use 'database' or 'mcp-server' appropriately
- Removed 'external' from UpdateSource union type

**Phase 5: Add Tests and Documentation**
- Added 3 comprehensive tests for new explicit persistence API
- Tests verify persist: false, markAsPersistedOnly, and legacy behavior
- All existing 1486 tests still pass, now 1490 total (+4 new tests)

**Files Changed:**
- types/update-protocol.ts: New persistence options, removed 'external' type
- services/shared-node-store.ts: Explicit persistence helper + usage
- services/reactive-node-service.svelte.ts: Fixed 'external' workaround
- tests/*: Updated all test files, added comprehensive test coverage

**Test Results:**
- Frontend: ✅ 1490 passed | 48 skipped (1538 total)
- All quality checks pass

**Migration Path:**
- Backward compatible: Legacy `source.type === 'database'` still works
- New code should use explicit `options.persist` instead
- Clear migration path for future MCP integration (Phase 3)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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

* Address review: Improve persist option documentation and test coverage

Implemented 2 of 3 review recommendations from PR #413:

1. ✅ API Naming Clarity (🟢 Suggestion):
   - Enhanced JSDoc for persist option to explicitly clarify persist: true vs undefined
   - Added explanation of difference between auto-determined modes
   - Included new example showing persist: true usage for structural changes

2. ✅ Test Coverage Gap (🟢 Suggestion):
   - Added explicit test for persist: true behavior with structural changes
   - Test verifies immediate persistence triggering (not debounced)
   - Documents expected behavior with clear comments
   - Increased test count from 1490 to 1491 (all passing)

3. ⏭️ Migration Documentation (🟢 Suggestion - SKIPPED):
   - Rationale: Inline JSDoc already provides comprehensive migration examples
   - Commit message documents rationale extensively
   - Separate migration guide would duplicate existing documentation
   - Decision: Skip for now, revisit if multiple related migrations accumulate

All quality checks pass, no lint/format issues.

Test Results: ✅ 1491 passed | 48 skipped (1539 total)

Addresses reviewer recommendations from pragmatic-code-review.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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

---------

Co-authored-by: Claude <[email protected]>
malibio added a commit that referenced this pull request Feb 26, 2026
* Refactor UpdateSource type system for clarity and correctness (Issue #393)

**Problem:**
The UpdateSource type conflated two orthogonal concepts:
1. Data origin (where did the update come from?)
2. Persistence state (should this be persisted to database?)

This led to:
- Confusing dual meaning of 'database' type
- Misuse of 'external' type as workaround for persistence control
- Navigation service bug using 'external' instead of 'database'
- No explicit API for persistence control

**Solution: Explicit Persistence API (Option C)**

**Phase 1: Add Explicit Persistence Control**
- Added `persist` option to UpdateOptions: false | true | 'debounced' | 'immediate'
- Added `markAsPersistedOnly` option to explicitly track backend-loaded nodes
- Maintains backward compatibility with legacy source.type checks

**Phase 2: Migrate SharedNodeStore**
- Created `determinePersistenceBehavior()` helper method
- Migrated all 3 locations checking `source.type === 'database'`
- updateNode(), setNode(), deleteNode() now use explicit API

**Phase 3: Clarify UpdateSource Semantics**
- Added comprehensive JSDoc explaining data provenance vs persistence
- Documented that UpdateOptions.persist controls persistence, not source.type

**Phase 4: Remove 'external' Type**
- Replaced 'external' workaround in shared-node-store.ts reconciliation
- Replaced 'external' workaround in reactive-node-service.svelte.ts combineNodes
- Updated all test files to use 'database' or 'mcp-server' appropriately
- Removed 'external' from UpdateSource union type

**Phase 5: Add Tests and Documentation**
- Added 3 comprehensive tests for new explicit persistence API
- Tests verify persist: false, markAsPersistedOnly, and legacy behavior
- All existing 1486 tests still pass, now 1490 total (+4 new tests)

**Files Changed:**
- types/update-protocol.ts: New persistence options, removed 'external' type
- services/shared-node-store.ts: Explicit persistence helper + usage
- services/reactive-node-service.svelte.ts: Fixed 'external' workaround
- tests/*: Updated all test files, added comprehensive test coverage

**Test Results:**
- Frontend: ✅ 1490 passed | 48 skipped (1538 total)
- All quality checks pass

**Migration Path:**
- Backward compatible: Legacy `source.type === 'database'` still works
- New code should use explicit `options.persist` instead
- Clear migration path for future MCP integration (Phase 3)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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

* Address review: Improve persist option documentation and test coverage

Implemented 2 of 3 review recommendations from PR #413:

1. ✅ API Naming Clarity (🟢 Suggestion):
   - Enhanced JSDoc for persist option to explicitly clarify persist: true vs undefined
   - Added explanation of difference between auto-determined modes
   - Included new example showing persist: true usage for structural changes

2. ✅ Test Coverage Gap (🟢 Suggestion):
   - Added explicit test for persist: true behavior with structural changes
   - Test verifies immediate persistence triggering (not debounced)
   - Documents expected behavior with clear comments
   - Increased test count from 1490 to 1491 (all passing)

3. ⏭️ Migration Documentation (🟢 Suggestion - SKIPPED):
   - Rationale: Inline JSDoc already provides comprehensive migration examples
   - Commit message documents rationale extensively
   - Separate migration guide would duplicate existing documentation
   - Decision: Skip for now, revisit if multiple related migrations accumulate

All quality checks pass, no lint/format issues.

Test Results: ✅ 1491 passed | 48 skipped (1539 total)

Addresses reviewer recommendations from pragmatic-code-review.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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

---------

Co-authored-by: Claude <[email protected]>
mstomar125 added a commit that referenced this pull request Aug 11, 2026
…(nodespace-sync#413 core half) (#2029)

* Surface access-gated cascade-delete refusal as a distinct typed error + modal

The SubtreeAccessGate seam (ADR-041) already refused a cascade delete whose
subtree contains nodes the caller can't read, but mapped it to a generic
HierarchyViolation → INVALID_ARGUMENT, indistinguishable from ordinary
validation — so the frontend couldn't tell a refusal apart. Make it a
first-class, user-visible outcome:

- New NodeServiceError::SubtreeAccessDenied { inaccessible_count } (not the
  shared bare-string HierarchyViolation); the gate's Denied branch returns it.
- New OpsError::SubtreeAccessDenied → maps to gRPC Status::failed_precondition
  with the count in x-subtree-inaccessible-count metadata (mirroring how a
  version conflict carries x-version-conflict), so the status is distinct from
  validation and from OCC.
- Frontend: status_to_command_error → a SUBTREE_ACCESS_DENIED command code
  carrying { inaccessibleCount }; an isSubtreeAccessDenied guard; a global
  refusal modal ("N item(s) not visible — nothing was deleted").
- The delete is optimistic (the node is removed locally before the backend
  call), so on a refusal the store restores exactly what the optimistic removal
  stripped (node, version, persisted-set membership, subscriber notify) and
  shows the modal. Descendants are only removed via post-commit events, which
  never fire on a refusal, so restoring the target node is sufficient.

Community/no-Pro path is unchanged (AlwaysAllowGate never denies). This is the
core half of sync#413; the daemon-side gate that actually calls the cloud
subtree_inaccessible_count, and live validation, follow separately.

Part of #413.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

* Address review: restore the view layer on refusal; scope the refusal code

Two real defects from review:

- The optimistic restore lived only at the store layer, but the reactive view
  service owns `_rootNodeIds` (the top-level view's source of truth) and strips
  the node from it synchronously — so a refused top-level delete restored the
  store yet the node stayed gone from the UI. The store's deleteNode now takes an
  `onRefused` callback it invokes (after restoring its own state) so the view
  service restores `_rootNodeIds`/`_uiState` too. The store delete is
  fire-and-forget, so this callback is how the refusal (which lands async)
  reaches the view layer. Covered by store-level onRefused tests (fires on a
  refusal, not on success or a non-refusal error).

- status_to_command_error branded EVERY FailedPrecondition as
  SUBTREE_ACCESS_DENIED, but the daemon returns FailedPrecondition from unrelated
  paths (node-create / schema failures). Now the code is assigned only when the
  x-subtree-inaccessible-count metadata is actually present; otherwise it falls
  through to GRPC_ERROR.

Also (minor): renamed the version-conflict precedence test to say what it
verifies (conflict reported when the gate allows) and added a genuine both-true
test asserting the access refusal PRECEDES the version conflict — the gate runs
before the OCC check, so we never leak version state on an access denial.

Co-Authored-By: Claude Opus 4.8 <[email protected]>

---------

Co-authored-by: Claude Opus 4.8 <[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.

Refactor UpdateSource type system for clarity and correctness

1 participant