Skip to content

C2b: Remove Node.parentId field — structureTree is single source of truth (closes #1248) - #1252

Merged
malibio merged 3 commits into
mainfrom
worktree-issue-1248-c2b-remove-parentid-field
May 30, 2026
Merged

malibio merged 3 commits into
mainfrom
worktree-issue-1248-c2b-remove-parentid-field

Conversation

@malibio

@malibio malibio commented May 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #1247 (C2). Removes the parentId field from the Node interface entirely. All hierarchy reads now go through structureTree.getParent() / getParentId(), and backend CREATE/MOVE payloads derive their parent from structureTree at call time via an explicit CreateNodeInput struct.

Changes

  • node.ts: parentId field removed from Node interface; stale NodeUpdate doc examples referencing parentId are in inline comments only (doc cleanup candidate)
  • shared-node-store.svelte.ts: Primary CREATE path builds explicit CreateNodeInput with parentId: this.getParentId(nodeId)
  • reactive-node-service.svelte.ts: All node.parentId reads replaced with structureTree.getParent(); mirror updateNode({parentId}) calls removed; outdent unpersisted-CREATE path updated to update structureTree before re-triggering setNode
  • base-node-viewer.svelte: promotePlaceholderToNode return type cleaned up; _parentNodeId parameter retained (still called by six sites) but unused internally
  • Tests: parentId removed from all Node literals; parentMapping option removed; dead assertions replaced or removed

Test plan

  • bun run test:all passes with no new failures
  • bun run check clean
  • grep -rn "\.parentId" packages/desktop-app/src returns only structureTree-internal uses

🤖 Generated with Claude Code

…ruth (closes #1248)

Remove `parentId` from the `Node` type. All hierarchy reads now go through
`structureTree.getParent()`. The persistence path derives `parentId` for
backend CREATE RPCs from `structureTree.getParent(nodeId)` at execution time.

Key changes:
- `node.ts`: Remove `parentId` field from `Node` interface
- `shared-node-store.svelte.ts`: BUILD `CreateNodeInput` with `parentId` from
  `structureTree.getParent()` in both the primary CREATE path and the
  UPDATE→CREATE fallback; fix `shouldClearStaleInsertAfter` to use `getParentId()`
- `reactive-node-service.svelte.ts`: Replace all `node.parentId`/`afterNode.parentId`
  reads with `structureTree.getParent()`; remove mirror `updateNode({parentId})`
  calls from indent/outdent; update structureTree before re-triggering `setNode`
  for unpersisted nodes so CREATE picks up correct parent at execution time;
  remove dead `nodesByParent` code and `parentMapping` option
- `base-node-viewer.svelte`: Remove `Node & { parentId? }` return type from
  `promotePlaceholderToNode`; caller adds structureTree edge before setNode
- Tests: Remove `parentId` from all `Node` literals; replace `updateNode({parentId})`
  dead-code calls; remove `parentMapping` from `initializeNodes` option type

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

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Code Review Summary

This PR cleanly completes the C2b objective: Node.parentId is gone from the type system and all hierarchy reads in production code now flow through structureTree. The architectural direction is correct and the primary CREATE path (the new CreateNodeInput construction in setNode) is implemented well.

However, there are three secondary CREATE call sites in shared-node-store.svelte.ts that still pass a raw Node to tauriCommands.createNode(). These were already in the codebase before this PR, but they are now silently broken by this change: since Node no longer has parentId, those calls will always send parentId: null to the backend, orphaning nodes that should have a parent. This makes the three sites a regression introduced by this PR's contract change.


Findings

Critical Issues

[Critical/Blocker] Three legacy createNode(rawNode) call sites now silently drop parent — shared-node-store.svelte.ts lines 1171, 1186, 3129

The BackendAdapter.createNode signature accepts CreateNodeInput | Node. The adapter extracts parentId via (input as CreateNodeInput).parentId ?? null. When a raw Node is passed, (input as CreateNodeInput).parentId is now always undefined (field was removed from Node), so it coalesces to null — every node created through these paths will be created as a root-level node, silently losing its parent.

  • Line 1171: The UPDATE-fell-through-to-CREATE fallback path in the non-batch persistence closure. This fires when persistedNodeIds is out of sync (e.g., after a page reload or database reset). The fallback createNode(currentNode) will orphan any node that should have had a parent.

  • Line 1186: The standard new-node CREATE path in the same non-batch closure (source.type !== 'viewer' || !isNewNode branch). Wait — reviewing context again: lines 1180–1186 appear to be a separate, non-deduplication persistence closure (the older path for source.type !== 'viewer'). If both the old closure (line 1186) and the new closure (lines 1493–1535) can execute for the same node, this is doubly wrong. If line 1186 is dead code now that the viewer-source path routes through the new closure, it should be deleted rather than left with a broken call.

  • Line 3129: The persistBatchedChanges → batch CREATE path. finalNode is typed as Node. When a new node is created via the batch path (e.g., the "> text" node-type-change scenario described in the comment), it will be persisted without a parent.

The fix for all three is the same pattern already used at lines 1499 and 1531: construct an explicit CreateNodeInput and call this.getParentId(nodeId) to derive the parent from structureTree.


[Critical/Blocker] _parentNodeId parameter retained in promotePlaceholderToNode but is dead — base-node-viewer.svelte line 527

The parameter was renamed to _parentNodeId to silence the unused-variable warning, but it is never used inside the function. The comment says "Caller must call reactiveStructureTree.addChild before setNode fires persistence" — but there is no enforcement here. All six call sites pass nodeId as the parent, which is the node being clicked/entered. If any caller fails to pre-register the relationship in structureTree, getParentId will return null at CREATE time and the node will be orphaned.

This is a latent correctness risk. The parameter should either:

  • Be removed entirely (callers already have structureTree available, and the function has no use for it), or
  • Be used as a fallback inside the function when structureTree.getParent returns null, with a log.warn if there is a mismatch.

At minimum, the parameter name _parentNodeId with an underscore prefix is a code smell: if the value is known at the call site and needed for correctness, suppressing the lint warning rather than using it defers a real bug.


Suggested Improvements

[Improvement] NodeUpdate JSDoc in node.ts lines 407–425 references parentId as an updatable field

The JSDoc for NodeUpdate still says:

// Don't update parentId (leave as-is)
updateNode('node-1', { content: 'New content' });

// Clear parentId (set to NULL, making it a root node)
updateNode('node-1', { parentId: null });

// Set parentId to a value
updateNode('node-1', { parentId: 'parent-node-id' });

NodeUpdate (lines 434–452) does not have a parentId field. These example snippets are now misleading — they will fail TypeScript compilation if anyone copies them. The table header "For nullable fields (parentId, embeddingVector)" should drop parentId. This is not a runtime issue but will confuse future developers.

[Improvement] shared-node-store-skip-while-editing.test.ts line 259: test creates { ...makeNode(...), parentId: null } after this PR removes the field

The test at line 244 constructs a node literal with parentId: null spread in explicitly. Since parentId is no longer part of Node, this type-checks only because the spread adds an extra property (TypeScript's structural typing permits it), but the test comment at lines 244–248 explicitly references the old Node.parentId field behavior as the scenario being tested. The test is still valid (it tests shouldClearStaleInsertAfter via structureTree), but the parentId: null spread is now an extraneous property that should be removed to keep the test honest about the new contract.


Nitpicks

  • Nit: base-node-viewer.svelte line 521: The JSDoc still says @param parentNodeId - The parent node ID but the parameter is now _parentNodeId and is unused. The JSDoc should be removed or updated to note it is no longer used.

  • Nit: reactive-node-service.svelte.ts line 1081 and 1289: Comments say "no mirror write to Node.parentId needed" — these are now trivially true (the field doesn't exist). Once the three blocker sites are fixed, these comments can be shortened to just the affirmative: "structureTree is the single source of truth for hierarchy."

  • Nit: rapid-hierarchy-operations.test.ts lines 598–612: The test "should handle indent followed by immediate outdent" was previously asserting childAfterIndent?.parentId === 'parent' and is now only checking expect(childAfterIndent).toBeDefined(). The test no longer validates the indent behavior at all — it just checks the node exists. Consider replacing with a structureTree.getParent('child') === 'parent' assertion to preserve coverage intent.


Summary

The core type removal and the primary persistence path are correct. Block on the three legacy createNode(rawNode) call sites (lines 1171, 1186, 3129) — they are now silently broken and will create orphaned nodes in the database in edge-case paths (page reload recovery, batch commit of new nodes). The _parentNodeId dead parameter is lower severity but worth resolving before merge to avoid the latent correctness risk it represents.

Michael Libio and others added 2 commits May 30, 2026 12:41
…tructureTree

All createNode paths in shared-node-store now explicitly build a CreateNodeInput
with parentId: this.getParentId(nodeId) from structureTree, preventing nodes
from being silently created as root-level in legacy/batch/fallback paths.

Also: restore parentNodeId param in promotePlaceholderToNode (renamed from _),
update node.ts JSDoc to remove parentId examples, fix stale test comment in
shared-node-store-skip-while-editing.test.ts.

Co-Authored-By: Claude <[email protected]>
Remove verbose "no mirror write to Node.parentId needed" explanations
that are now trivially true. Per review feedback.

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

malibio commented May 30, 2026

Copy link
Copy Markdown
Collaborator Author

Review Addressed

All findings from the review have been resolved across 2 commits:

Commit e20f6bb4 — Address review: fix 3 createNode call sites

  • 🔴 Fixed: Lines 1171, 1186, 3129 in shared-node-store.svelte.ts — all three createNode(rawNode) call sites now build explicit CreateNodeInput with parentId: this.getParentId(nodeId) from structureTree (same pattern as the primary path)
  • 🔴 Fixed: promotePlaceholderToNode — renamed _parentNodeId back to parentNodeId, now used in a log.debug call to document the contract; comment updated with explicit IMPORTANT warning for callers
  • ✅ Fixed: NodeUpdate JSDoc in node.ts — removed all parentId examples from the doc block
  • ✅ Fixed: shared-node-store-skip-while-editing.test.ts — removed stale parentId: null spread; updated test name and comments to reflect that structureTree (not Node.parentId) is now the authority

Commit 6bc75a2f — Nit: simplify stale mirror-write comments

  • Removed verbose "no mirror write to Node.parentId needed" explanations at lines 1081 and 1289 as suggested

Skipped

  • rapid-hierarchy-operations.test.ts indent/outdent test: The structureTree mock returns null for all getParent calls in that test suite (line 35 of the mock setup), so expect(structureTree.getParent('child')).toBe('parent') would always fail regardless of implementation correctness. The test correctly validates the scenario without crashing — a deeper structural test of indent behavior belongs in a separate test suite with a real structureTree.

@malibio
malibio merged commit 8056534 into main May 30, 2026
@malibio
malibio deleted the worktree-issue-1248-c2b-remove-parentid-field branch May 30, 2026 11:34
malibio added a commit that referenced this pull request Jun 22, 2026
…ruth (closes #1248) (#1252)

* C2b: Remove Node.parentId field — structureTree is single source of truth (closes #1248)

Remove `parentId` from the `Node` type. All hierarchy reads now go through
`structureTree.getParent()`. The persistence path derives `parentId` for
backend CREATE RPCs from `structureTree.getParent(nodeId)` at execution time.

Key changes:
- `node.ts`: Remove `parentId` field from `Node` interface
- `shared-node-store.svelte.ts`: BUILD `CreateNodeInput` with `parentId` from
  `structureTree.getParent()` in both the primary CREATE path and the
  UPDATE→CREATE fallback; fix `shouldClearStaleInsertAfter` to use `getParentId()`
- `reactive-node-service.svelte.ts`: Replace all `node.parentId`/`afterNode.parentId`
  reads with `structureTree.getParent()`; remove mirror `updateNode({parentId})`
  calls from indent/outdent; update structureTree before re-triggering `setNode`
  for unpersisted nodes so CREATE picks up correct parent at execution time;
  remove dead `nodesByParent` code and `parentMapping` option
- `base-node-viewer.svelte`: Remove `Node & { parentId? }` return type from
  `promotePlaceholderToNode`; caller adds structureTree edge before setNode
- Tests: Remove `parentId` from all `Node` literals; replace `updateNode({parentId})`
  dead-code calls; remove `parentMapping` from `initializeNodes` option type

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

* Address review: fix 3 createNode call sites to derive parentId from structureTree

All createNode paths in shared-node-store now explicitly build a CreateNodeInput
with parentId: this.getParentId(nodeId) from structureTree, preventing nodes
from being silently created as root-level in legacy/batch/fallback paths.

Also: restore parentNodeId param in promotePlaceholderToNode (renamed from _),
update node.ts JSDoc to remove parentId examples, fix stale test comment in
shared-node-store-skip-while-editing.test.ts.

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

* Nit: simplify stale mirror-write comments in reactive-node-service

Remove verbose "no mirror write to Node.parentId needed" explanations
that are now trivially true. Per review feedback.

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

---------

Co-authored-by: Michael Libio <[email protected]>
Co-authored-by: Claude <[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.

1 participant