Skip to content

fix(tree): make findNode/getParentNode null-tolerant - #2425

Merged
openai0229 merged 3 commits into
OtterMind:mainfrom
Aias00:fix/tree-findnode-null-tolerant-2424
Aug 3, 2026
Merged

openai0229 merged 3 commits into
OtterMind:mainfrom
Aias00:fix/tree-findnode-null-tolerant-2424

Conversation

@Aias00

@Aias00 Aias00 commented Aug 3, 2026 •

Copy link
Copy Markdown
Contributor

What

Tree store actions can run while treeData is legitimately null during initialization, refresh, or after clearing the store. The previous lookup helpers dereferenced the tree and also claimed a non-null return type even when a node was not found.

Fix

  • Move the recursive lookup logic into treeNodeLookup.ts.
  • Accept nullable tree input and return TreeNodeData | undefined from findNode and getParentNode.
  • Guard all affected rename, delete, close-connection, and refresh call sites with explicit fallback behavior.
  • Refresh the full tree when a parent node cannot be located.
  • Avoid splice(-1, 1) when the target datasource is absent.
  • Add focused lookup tests for null trees, missing keys, nested matches, and parent lookup.

Verification

  • yarn test:tree-node-lookup
  • yarn test:tree-data-update
  • ESLint on all changed TypeScript files with zero warnings
  • yarn build:web:community

Fixes #2424

Tree store actions pass get().treeData! into findNode/getParentNode, but
treeData is legitimately null (initial, getTreeData, clearTreeStore). The
functions dereferenced tree.length with no guard -> null.length NPE on
rename/delete/close-connection right after login or during a refresh. Guard
the loop with 'tree &&' so a null tree yields undefined instead of throwing.
Signatures unchanged to avoid call-site type ripple.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: liuhy <liuhongyu@apache.org>
Copilot AI review requested due to automatic review settings August 3, 2026 12:30
@Aias00
Aias00 requested a review from openai0229 as a code owner August 3, 2026 12:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes a frontend tree-store crash by making tree traversal helpers resilient to treeData === null during initial load / refresh windows, preventing null.length runtime errors.

Changes:

  • Add null guards to getParentNode’s iteration to avoid dereferencing tree.length when treeData is null.
  • Add null guards to findNode’s iteration for the same null-tree scenario.
Suppressed comments (1)

chat2db-community-client/src/utils/index.ts:396

  • The recursive branch calls getParentNode(key, node.children) twice, which doubles the traversal work for every branch and makes the logic harder to read. Cache the recursive result in a local variable and reuse it.
      if (node.children.some((item) => item.key === key)) {
        finalNode = node;
      } else if (getParentNode(key, node.children)) {
        finalNode = getParentNode(key, node.children);
      }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread chat2db-community-client/src/utils/index.ts Outdated
Comment thread chat2db-community-client/src/utils/index.ts Outdated

@openai0229 openai0229 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed after the nullable-contract and call-site fixes. The focused lookup tests, affected-file ESLint, and full Community frontend build pass locally.

@openai0229
openai0229 merged commit 0182738 into OtterMind:main Aug 3, 2026
22 of 23 checks passed
@openai0229 openai0229 moved this from In Review to Done in Chat2DB Community Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bug(tree): findNode/getParentNode throw on null treeData

3 participants