Repository navigation
C2b: Remove Node.parentId field — structureTree is single source of truth (closes #1248) - #1252
Conversation
…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
left a comment
There was a problem hiding this comment.
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
persistedNodeIdsis out of sync (e.g., after a page reload or database reset). The fallbackcreateNode(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' || !isNewNodebranch). Wait — reviewing context again: lines 1180–1186 appear to be a separate, non-deduplication persistence closure (the older path forsource.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 theviewer-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.finalNodeis typed asNode. 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
structureTreeavailable, and the function has no use for it), or - Be used as a fallback inside the function when
structureTree.getParentreturnsnull, with alog.warnif 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.svelteline 521: The JSDoc still says@param parentNodeId - The parent node IDbut the parameter is now_parentNodeIdand is unused. The JSDoc should be removed or updated to note it is no longer used. -
Nit:
reactive-node-service.svelte.tsline 1081 and 1289: Comments say "no mirror write toNode.parentIdneeded" — 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.tslines 598–612: The test "should handle indent followed by immediate outdent" was previously assertingchildAfterIndent?.parentId === 'parent'and is now only checkingexpect(childAfterIndent).toBeDefined(). The test no longer validates the indent behavior at all — it just checks the node exists. Consider replacing with astructureTree.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.
…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]>
Review AddressedAll findings from the review have been resolved across 2 commits: Commit
|
…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]>
Summary
Follow-up to #1247 (C2). Removes the
parentIdfield from theNodeinterface entirely. All hierarchy reads now go throughstructureTree.getParent()/getParentId(), and backend CREATE/MOVE payloads derive their parent fromstructureTreeat call time via an explicitCreateNodeInputstruct.Changes
node.ts:parentIdfield removed fromNodeinterface; staleNodeUpdatedoc examples referencingparentIdare in inline comments only (doc cleanup candidate)shared-node-store.svelte.ts: Primary CREATE path builds explicitCreateNodeInputwithparentId: this.getParentId(nodeId)reactive-node-service.svelte.ts: Allnode.parentIdreads replaced withstructureTree.getParent(); mirrorupdateNode({parentId})calls removed; outdent unpersisted-CREATE path updated to update structureTree before re-triggeringsetNodebase-node-viewer.svelte:promotePlaceholderToNodereturn type cleaned up;_parentNodeIdparameter retained (still called by six sites) but unused internallyparentIdremoved from allNodeliterals;parentMappingoption removed; dead assertions replaced or removedTest plan
bun run test:allpasses with no new failuresbun run checkcleangrep -rn "\.parentId" packages/desktop-app/srcreturns only structureTree-internal uses🤖 Generated with Claude Code