Skip to content

fix: subtree expand only first level + depth limit#8525

Open
Nriver wants to merge 1 commit intomainfrom
fix/subtree-expand-only-first-level
Open

fix: subtree expand only first level + depth limit#8525
Nriver wants to merge 1 commit intomainfrom
fix/subtree-expand-only-first-level

Conversation

@Nriver
Copy link
Collaborator

@Nriver Nriver commented Jan 27, 2026

  • Correctly recurses through multiple levels when expanding (expanded=1)
  • Only updates branches that are actually collapsed (isExpanded=0) when expanding
  • Only updates branches that are actually expanded (isExpanded=1) when collapsing
  • Adds depth limit of 4 levels when expanding to prevent performance issues in deep trees
  • Collapsing remains unlimited (full subtree)

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Jan 27, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Nriver, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the subtree expansion and collapse functionality within the API. The changes focus on improving the correctness and performance of these operations by introducing a depth limit for expansion, ensuring that only branches requiring a state change are updated, and refining the recursive SQL query for more precise traversal.

Highlights

  • Subtree Expansion Logic Refinement: The setExpandedForSubtree function has been significantly refactored to correctly handle recursive expansion and collapse operations, ensuring accurate traversal through multiple levels.
  • Conditional State Updates: The system now intelligently updates only those branches whose isExpanded state differs from the target state (e.g., only collapsing already expanded branches, and expanding already collapsed branches), preventing unnecessary database operations.
  • Depth Limiting for Expansion: A depth limit of 4 levels has been introduced for subtree expansion (expanded = 1) to prevent potential performance issues when dealing with very deep tree structures. Collapse operations (expanded = 0) remain unlimited in depth.
  • Input Validation: Added input validation for the expanded parameter in setExpandedForSubtree to ensure it is strictly 0 or 1, improving API robustness.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the subtree expansion logic to correctly handle recursion, apply depth limits, and only update branches that need to change state. The changes are a significant improvement over the previous implementation. My review focuses on a potential logic bug in the recursive SQL query that could lead to incomplete updates, some code that could be simplified for better readability, and an inconsistency in the function's return types.

Comment on lines +169 to 197
const branchIds = sql.getColumn<string>(
`
WITH RECURSIVE
tree(branchId, noteId) AS (
SELECT branchId, noteId FROM branches WHERE branchId = ?
UNION
SELECT branches.branchId, branches.noteId FROM branches
JOIN tree ON branches.parentNoteId = tree.noteId
WHERE branches.isDeleted = 0
AND branches.isExpanded = 1
tree(branchId, noteId, depth) AS (
-- Anchor: starting node (depth 0)
SELECT branchId, noteId, 0
FROM branches
WHERE branchId = ?

UNION ALL

-- Recursive part
SELECT
b.branchId,
b.noteId,
t.depth + 1
FROM branches b
JOIN tree t ON b.parentNoteId = t.noteId
WHERE b.isDeleted = 0
AND b.isExpanded = ?
AND t.depth < ?
)
SELECT branchId FROM tree`,
[branchId]
SELECT branchId
FROM tree
WHERE depth <= ?
AND branchId != 'none_root'
`,
[branchId, currentExpandedWeWantToChange, maxDepth, maxDepth]
);
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The current recursive query logic seems incorrect for finding all branches to update. By filtering on b.isExpanded within the recursive step, you stop traversing parts of the tree. For example, when expanding, if you encounter an already expanded branch, you won't look for its collapsed children. To correctly update all nodes in the subtree that are in the opposite state, you should traverse the whole subtree first (up to maxDepth) and then filter the results. This ensures all branches that need to change state are identified, regardless of their parents' state.

    const branchIds = sql.getColumn<string>(
        `
        WITH RECURSIVE
        tree(branchId, noteId, depth) AS (
            -- Anchor: starting node (depth 0)
            SELECT branchId, noteId, 0
            FROM branches
            WHERE branchId = ?

            UNION ALL

            -- Recursive part
            SELECT 
                b.branchId, 
                b.noteId, 
                t.depth + 1
            FROM branches b
            JOIN tree t ON b.parentNoteId = t.noteId
            WHERE b.isDeleted = 0
              AND t.depth < ?
        )
        SELECT t.branchId
        FROM tree t JOIN branches b ON t.branchId = b.branchId
        WHERE t.depth <= ?
          AND t.branchId != 'none_root'
          AND b.isExpanded = ?
        `,
        [branchId, maxDepth, maxDepth, currentExpandedWeWantToChange]
    );

Comment on lines +158 to +161
if (![0, 1].includes(expanded)) {
// You can change to throw new Error() if preferred
return { branchIds: [], updatedCount: 0, error: "expanded must be 0 or 1" };
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This function now has multiple return paths with inconsistent object structures. This early return provides an object with an error property, while another early return provides maxDepthApplied, and the main success path (outside this diff) returns a different structure. This makes the function's return value unpredictable for consumers. It would be best to standardize the return object across all paths, for example by having a common shape like { branchIds, updatedCount, error, ... }.

Comment on lines +200 to 210
const filteredBranchIds = branchIds.filter((id) => id !== "none_root");

if (filteredBranchIds.length === 0) {
return {
branchIds: [],
updatedCount: 0,
maxDepthApplied: maxDepth
};
}

sql.executeMany(/*sql*/`UPDATE branches SET isExpanded = ${expanded} WHERE branchId IN (???)`, branchIds);
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The filteredBranchIds variable seems redundant since the SQL query is already expected to filter out 'none_root'. Using this variable for the length check but then using the original branchIds for the sql.executeMany call is confusing and could be a source of bugs if the filter wasn't redundant. For clarity and safety, it would be better to remove the redundant client-side filter and consistently use branchIds.

Suggested change
const filteredBranchIds = branchIds.filter((id) => id !== "none_root");
if (filteredBranchIds.length === 0) {
return {
branchIds: [],
updatedCount: 0,
maxDepthApplied: maxDepth
};
}
sql.executeMany(/*sql*/`UPDATE branches SET isExpanded = ${expanded} WHERE branchId IN (???)`, branchIds);
if (branchIds.length === 0) {
return {
branchIds: [],
updatedCount: 0,
maxDepthApplied: maxDepth
};
}
sql.executeMany(/*sql*/`UPDATE branches SET isExpanded = ${expanded} WHERE branchId IN (???)`, branchIds);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant