Skip to content

fix: compress linear tree chains instead of removing tree field to prevent JSON.stringify stack overflow - #48

Closed
fallleave001 wants to merge 1 commit into
agegr:mainfrom
fallleave001:fix/session-tree-overflow
Closed

fallleave001 wants to merge 1 commit into
agegr:mainfrom
fallleave001:fix/session-tree-overflow

Conversation

@fallleave001

@fallleave001 fallleave001 commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Title:

fix: compress linear tree chains instead of removing tree field to prevent JSON.stringify stack overflow

Description:

Previous PR (#38) removed the tree field entirely, which broke BranchNavigator — branched sessions lost in-session branch navigation.

This PR takes a different approach: instead of removing tree, it applies a linear chain compression before serialization.

How it works

  • A recursive walk function traverses the tree
  • Single-child chains (e.g., A → B → C → D where each node has exactly one child) are collapsed — each node adopts its grandchild's children, effectively flattening the chain
  • Branching points (nodes with >1 children) are preserved as-is

Result

  • A 2000+ message linear session: depth goes from 2000+ → 1 (root node only, no overhead)
  • A branched session root → A → [branch1, branch2]: preserved as-is, only the linear prefix before the first branch is collapsed
  • JSON.stringify stays well within V8's ~3000 recursion limit
  • BranchNavigator receives a valid (compressed) tree and renders correctly

Testing

  • Verified with a 3400-message linear session: opens without error, ChatMinimap and branch navigation working
  • Branched session creation and navigation tested and working

…event JSON.stringify stack overflow

Previous attempt removed the `tree` field entirely, which broke BranchNavigator.
This new approach keeps `tree` but compresses linear chains (single-child nodes
are collapsed into their parent) before serialization, reducing depth from 2000+
to the number of branching points — well within V8's ~3000 recursion limit.

Branching points (nodes with >1 children) are preserved as-is, so users with
branched sessions retain full in-session branch navigation.
@agegr

agegr commented Jun 8, 2026

Copy link
Copy Markdown
Owner

Thanks, this is definitely the right direction compared with removing tree.
One issue: the current compression only works when branch children are also leaves.
For a common shape like root -> A -> [branch1 -> leaf1, branch2 -> leaf2],
leaf1 and leaf2 disappear from the returned tree, while activeLeafId
still points at those leaf IDs. That means BranchNavigator can no longer build
the active path or navigate to the actual branch tail.

I think the compressed tree needs to preserve a navigable target leaf per branch
(or keep the leaf node itself), ideally with an iterative implementation so the
compression step itself cannot overflow on very long chains.

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.

3 participants