Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
feat(agentic-ide): a pure split tree that keeps every split local
rubenluetke10-beep7ab7d53a1944
diff --git a/jarvis/agentic_ide/layout_tree.py b/jarvis/agentic_ide/layout_tree.pynew file mode 100644index 0000000000..962d978817--- /dev/null+++ b/jarvis/agentic_ide/layout_tree.py@@ -0,0 +1,487 @@+"""The workspace's split tree — where every pane sits and how much room it has.++The workspace used to be a flat "columns × slots" grid: every pane carried two+integers, a column was full-height by definition, and so "split right" could+only mean "open a new full-height column". Splitting the top pane of a stack+therefore restructured the WHOLE workspace — the new pane arrived beside+everything instead of beside the pane that was clicked (reported with a+drawing on 2026-08-12).++A split tree is the model that makes splits local, and it is the one every+tiling surface converges on (tmux, VS Code): a workspace is a nesting of+containers, a ``row`` lays its children side by side, a ``column`` stacks+them, and splitting a pane replaces its leaf with a two-child container. The+operation touches one branch and nothing else, so the pane that was clicked is+the only pane that changes size — in BOTH directions, at any depth.++This module is deliberately pure: nodes in, nodes out, no session, no I/O.+The session owns one tree per workspace and calls these functions; keeping+them free of session state is what makes the tricky part — the structural+edits — exhaustively testable on plain values.++## Invariants++Every function returns a tree in canonical form:++* A ``Split`` holds at least two children — a container with one child is+ spliced out and its child takes its place.+* A child never repeats its parent's direction — a row inside a row is merged+ into the parent (weights scaled so nothing moves on screen). Without this,+ repeated split/close cycles grow chains of degenerate containers that render+ identically but make every later edit harder to reason about.+* ``weights`` has exactly one positive finite entry per child. A weight is a+ plain multiplier against its siblings (two children at 1 and 1 are equal),+ never pixels — pixels do not survive a window resize, a weight does.++## Room accounting++* **A split halves its anchor.** The new pane takes half of the clicked+ pane's room and every other pane keeps exactly what it had. That promise+ already existed for the flat grid (`paneLayout.ts`, `weightsAfterSplit`)+ and the tree keeps it by construction.+* **A close gives the freed room to the siblings, proportionally.** The+ closed pane's weight is simply dropped; the remaining weights renormalise+ against each other, so a 2:1:1 stack losing its tail becomes 2:1.+"""++from __future__ import annotations++from collections.abc import Iterable+from dataclasses import dataclass, field+from typing import Any, Literal++Direction = Literal["row", "column"]++#: Where ``move_pane`` may put a pane, relative to its drop target. "swap" keeps+#: the tree's shape and exchanges the two panes; the four sides carve the+#: TARGET's own rectangle, exactly as the split buttons would.+MOVE_POSITIONS = ("swap", "left", "right", "above", "below")+++@dataclass(slots=True)+class Leaf:+ """One pane, referenced by its terminal KEY (``"t1"``), never its name.++ The key is the pane's stable identity for its whole life; the visible+ call-sign can be renamed without the tree noticing.+ """++ pane: str+++@dataclass(slots=True)+class Split:+ """A container: children side by side (``row``) or stacked (``column``)."""++ direction: Direction+ children: list[LayoutNode] = field(default_factory=list)+ weights: list[float] = field(default_factory=list)+++LayoutNode = Leaf | Split+++def _clean_weight(value: Any) -> float:+ """A usable sibling multiplier, or the default share for anything else."""+ try:+ number = float(value)+ except (TypeError, ValueError):+ return 1.0+ if not (number > 0) or number == float("inf"):+ return 1.0+ return number+++def normalize(node: LayoutNode) -> LayoutNode:+ """Return ``node`` in canonical form (see the module header).++ Every structural edit in this module funnels through here, so the+ invariants hold after ANY sequence of splits, closes and moves — not just+ the sequences someone thought to handle inline.+ """+ if isinstance(node, Leaf):+ return node++ children: list[LayoutNode] = []+ weights: list[float] = []+ raw_weights = list(node.weights)+ for index, child in enumerate(node.children):+ cleaned = normalize(child)+ weight = _clean_weight(raw_weights[index] if index < len(raw_weights) else 1.0)+ if isinstance(cleaned, Split) and cleaned.direction == node.direction:+ # A row inside a row: inline the grandchildren, scaling their+ # weights so together they still occupy exactly the room the+ # child had. Nothing moves on screen — only the bookkeeping+ # flattens.+ total = sum(cleaned.weights) or 1.0+ for grandchild, sub in zip(cleaned.children, cleaned.weights, strict=True):+ children.append(grandchild)+ weights.append(weight * sub / total)+ else:+ children.append(cleaned)+ weights.append(weight)++ if len(children) == 1:+ return children[0]+ return Split(direction=node.direction, children=children, weights=weights)+++def leaves(node: LayoutNode | None) -> list[str]:+ """Every pane key in READING order — the order `_renumber` sorts by.++ Depth-first, children left to right / top to bottom. For the plain+ "columns of stacks" shape this is exactly the old ``(column, slot)`` sort,+ so call-sign numbering keeps meaning what it always meant.+ """+ if node is None:+ return []+ if isinstance(node, Leaf):+ return [node.pane]+ found: list[str] = []+ for child in node.children:+ found.extend(leaves(child))+ return found+++def contains(node: LayoutNode | None, pane: str) -> bool:+ return pane in leaves(node)+++def wizard_tree(panes: Iterable[str], depth: int) -> LayoutNode | None:+ """The tree a wizard-opened workspace starts with.++ Columns of ``depth``, each filled top to bottom before the next is opened+ — the same arithmetic the wizard preview draws with (frontend+ ``layout.ts``), so the workspace that appears is the one that was shown.+ """+ keys = list(panes)+ if not keys:+ return None+ step = max(1, int(depth))+ columns: list[LayoutNode] = []+ for start in range(0, len(keys), step):+ chunk = [Leaf(pane=key) for key in keys[start : start + step]]+ if len(chunk) == 1:+ columns.append(chunk[0])+ else:+ columns.append(+ Split(direction="column", children=list(chunk), weights=[1.0] * len(chunk))+ )+ if len(columns) == 1:+ return columns[0]+ return Split(direction="row", children=columns, weights=[1.0] * len(columns))+++def from_grid(entries: Iterable[tuple[str, int, int]]) -> LayoutNode | None:+ """A tree for panes that only know their legacy ``(column, slot)`` place.++ This is the migration path: resume snapshots written before the tree+ existed carry the two integers per pane, and the columns-of-stacks shape+ they describe is exactly representable — a row of column stacks.+ """+ by_column: dict[int, list[tuple[int, str]]] = {}+ for key, column, slot in entries:+ by_column.setdefault(column, []).append((slot, key))+ if not by_column:+ return None+ columns: list[LayoutNode] = []+ for column in sorted(by_column):+ stack = [key for _, key in sorted(by_column[column])]+ if len(stack) == 1:+ columns.append(Leaf(pane=stack[0]))+ else:+ columns.append(+ Split(+ direction="column",+ children=[Leaf(pane=key) for key in stack],+ weights=[1.0] * len(stack),+ )+ )+ if len(columns) == 1:+ return columns[0]+ return Split(direction="row", children=columns, weights=[1.0] * len(columns))+++def _insert_beside(+ node: LayoutNode,+ anchor: str,+ added: str,+ direction: Direction,+ after: bool,+) -> tuple[LayoutNode, bool]:+ """Place ``added`` on one side of ``anchor``, halving the anchor's room.++ Returns the rewritten node and whether the anchor was found beneath it.+ The two cases are the whole trick of a split tree:++ * The anchor's parent already runs in ``direction`` — the new leaf joins+ that container right beside the anchor, taking half the anchor's weight.+ * It does not — the anchor's leaf alone is replaced by a two-child+ container. The anchor keeps its weight in the parent, so the pair+ together occupy exactly the room the anchor had.+ """+ if isinstance(node, Leaf):+ if node.pane != anchor:+ return node, False+ pair = [node, Leaf(pane=added)] if after else [Leaf(pane=added), node]+ return Split(direction=direction, children=pair, weights=[1.0, 1.0]), True++ for index, child in enumerate(node.children):+ if isinstance(child, Leaf) and child.pane == anchor:+ if node.direction == direction:+ share = _clean_weight(node.weights[index] if index < len(node.weights) else 1.0)+ node.weights[index] = share / 2+ at = index + 1 if after else index+ node.children.insert(at, Leaf(pane=added))+ node.weights.insert(at, share / 2)+ else:+ pair = [child, Leaf(pane=added)] if after else [Leaf(pane=added), child]+ node.children[index] = Split(direction=direction, children=pair, weights=[1.0, 1.0])+ return node, True+ if isinstance(child, Split):+ rewritten, found = _insert_beside(child, anchor, added, direction, after)+ if found:+ node.children[index] = rewritten+ return node, True+ return node, False+++def split_pane(+ root: LayoutNode | None,+ anchor: str | None,+ added: str,+ direction: Literal["right", "down"],+) -> LayoutNode:+ """The tree after ``added`` was split off ``anchor``.++ ``"right"`` puts the new pane beside the anchor, ``"down"`` beneath it —+ and in both cases the pair shares the room the anchor had, because that is+ what splitting A pane means. Nothing outside the anchor's rectangle moves.++ Without an anchor (an empty tree, or a caller that named none) the pane+ joins the ROOT as a new full-height column on the far right — the shape+ "open one more terminal" has always produced, and the only honest reading+ of a request that named no pane to split.+ """+ grown: Direction = "row" if direction == "right" else "column"+ if root is None:+ return Leaf(pane=added)+ if anchor is not None:+ rewritten, found = _insert_beside(root, anchor, added, grown, after=True)+ if found:+ return normalize(rewritten)+ return append_pane(root, added)+++def append_pane(root: LayoutNode | None, added: str) -> LayoutNode:+ """``added`` as a new full-height column at the workspace's right edge.++ The anchor-less add — the batch behind "open five more", the empty grid's+ button, a voice request that names nothing. The new column takes an even+ share: appending at the MEAN of the existing weights is what "an even+ share" means when the neighbours have been dragged to 2:1.+ """+ if root is None:+ return Leaf(pane=added)+ if isinstance(root, Split) and root.direction == "row":+ share = sum(root.weights) / len(root.weights) if root.weights else 1.0+ root.children.append(Leaf(pane=added))+ root.weights.append(_clean_weight(share))+ return normalize(root)+ return Split(direction="row", children=[root, Leaf(pane=added)], weights=[1.0, 1.0])+++def remove_pane(root: LayoutNode | None, pane: str) -> LayoutNode | None:+ """The tree without ``pane``; ``None`` when it held nothing else.++ The freed room goes to the closed pane's SIBLINGS, proportionally — its+ weight is dropped and the rest renormalise. A container left with one+ child dissolves, which is how a workspace that was split apart folds back+ to simple shapes instead of accumulating scar tissue.+ """+ if root is None:+ return None+ if isinstance(root, Leaf):+ return None if root.pane == pane else root++ children: list[LayoutNode] = []+ weights: list[float] = []+ for index, child in enumerate(root.children):+ weight = _clean_weight(root.weights[index] if index < len(root.weights) else 1.0)+ if isinstance(child, Leaf) and child.pane == pane:+ continue+ if isinstance(child, Split):+ slimmed = remove_pane(child, pane)+ if slimmed is None:+ continue+ children.append(slimmed)+ weights.append(weight)+ continue+ children.append(child)+ weights.append(weight)++ if not children:+ return None+ if len(children) == 1:+ return normalize(children[0])+ return normalize(Split(direction=root.direction, children=children, weights=weights))+++def swap_panes(root: LayoutNode | None, first: str, second: str) -> LayoutNode | None:+ """Exchange two panes' places; the tree's shape and every weight stay put."""+ if root is None:+ return None++ def walk(node: LayoutNode) -> LayoutNode:+ if isinstance(node, Leaf):+ if node.pane == first:+ return Leaf(pane=second)+ if node.pane == second:+ return Leaf(pane=first)+ return node+ node.children = [walk(child) for child in node.children]+ return node++ return walk(root)+++def move_pane(+ root: LayoutNode | None,+ pane: str,+ target: str,+ position: str,+) -> LayoutNode | None:+ """The tree after ``pane`` was dropped on ``target``.++ ``"swap"`` exchanges the two and keeps the shape. The four sides carve the+ TARGET's own rectangle — dropping left of a pane means "take that pane's+ left half", exactly the local meaning the split buttons have. The moved+ pane's old room dissolves to its former siblings on the way out.+ """+ if root is None or pane == target:+ return root+ if position == "swap":+ return swap_panes(root, pane, target)++ direction: Direction = "row" if position in ("left", "right") else "column"+ after = position in ("right", "below")+ slimmed = remove_pane(root, pane)+ if slimmed is None:+ return Leaf(pane=pane) if contains(root, pane) else root+ rewritten, found = _insert_beside(slimmed, target, pane, direction, after)+ if not found:+ # The target vanished mid-flight (a concurrent close). Losing the+ # DROP is recoverable; losing the PANE is not — put it back at the+ # edge rather than dropping it from the tree.+ return append_pane(slimmed, pane)+ return normalize(rewritten)+++# ------------------------------------------------------------- serialization+++def to_dict(node: LayoutNode) -> dict[str, Any]:+ """The wire/persistence form: plain dicts, safe to JSON-encode."""+ if isinstance(node, Leaf):+ return {"pane": node.pane}+ return {+ "direction": node.direction,+ "children": [to_dict(child) for child in node.children],+ "weights": [_clean_weight(weight) for weight in node.weights],+ }+++def from_dict(data: Any) -> LayoutNode:+ """Parse a stored tree, refusing anything malformed.++ Raises ``ValueError`` rather than guessing: a resume snapshot is a file on+ disk and files get truncated and hand-edited. The caller's fallback — the+ legacy ``(column, slot)`` migration — is honest; a half-parsed tree that+ silently drops panes is not.+ """+ if not isinstance(data, dict):+ raise ValueError("A layout node must be an object.")+ if "pane" in data:+ pane = data["pane"]+ if not isinstance(pane, str) or not pane:+ raise ValueError("A pane leaf needs a non-empty key.")+ return Leaf(pane=pane)+ direction = data.get("direction")+ if direction not in ("row", "column"):+ raise ValueError(f"Unknown layout direction: {direction!r}")+ children = data.get("children")+ if not isinstance(children, list) or len(children) < 2:+ raise ValueError("A split needs at least two children.")+ parsed = [from_dict(child) for child in children]+ raw_weights = data.get("weights")+ weights = [+ _clean_weight(raw_weights[index])+ if isinstance(raw_weights, list) and index < len(raw_weights)+ else 1.0+ for index in range(len(parsed))+ ]+ node = Split(direction=direction, children=parsed, weights=weights)+ seen: set[str] = set()+ for pane in leaves(node):+ if pane in seen:+ raise ValueError(f"Pane {pane!r} appears twice in the layout.")+ seen.add(pane)+ return normalize(node)+++def same_shape(a: LayoutNode | None, b: LayoutNode | None) -> bool:+ """Same structure, same panes in the same places — weights ignored.++ This is the guard for adopting a client's dragged weights: a drag is only+ meaningful against the tree the client was LOOKING at, and a workspace+ reshaped in between (a voice-opened pane, a second client) makes the drag+ stale, not wrong enough to corrupt anything.+ """+ if a is None or b is None:+ return a is None and b is None+ if isinstance(a, Leaf) or isinstance(b, Leaf):+ return isinstance(a, Leaf) and isinstance(b, Leaf) and a.pane == b.pane+ if a.direction != b.direction or len(a.children) != len(b.children):+ return False+ return all(same_shape(x, y) for x, y in zip(a.children, b.children, strict=True))+++def adopt_weights(current: LayoutNode, proposed: LayoutNode) -> LayoutNode:+ """``current``'s structure carrying ``proposed``'s weights.++ Callers check :func:`same_shape` first; this keeps the session's own tree+ authoritative for STRUCTURE while letting a client persist the one thing a+ seam drag changes.+ """+ if isinstance(current, Leaf) or isinstance(proposed, Leaf):+ return current+ return Split(+ direction=current.direction,+ children=[+ adopt_weights(mine, theirs)+ for mine, theirs in zip(current.children, proposed.children, strict=True)+ ],+ weights=[_clean_weight(weight) for weight in proposed.weights[: len(current.children)]]+ + [1.0] * max(0, len(current.children) - len(proposed.weights)),+ )+++def grid_hints(root: LayoutNode | None) -> dict[str, tuple[int, int]]:+ """Coarse legacy ``(column, slot)`` for every pane, from the tree.++ Consumers that describe the workspace in words ("the top-left terminal")+ still think in columns, and for the common shapes the mapping is exact.+ For deeper nesting it is deliberately coarse: the column is the pane's+ top-level branch, the slot its reading position within that branch —+ stable, ordered, and never claiming precision the flat model cannot hold.+ """+ if root is None:+ return {}+ if isinstance(root, Split) and root.direction == "row":+ return {+ pane: (column, slot)+ for column, child in enumerate(root.children)+ for slot, pane in enumerate(leaves(child))+ }+ # A single stack (or a lone pane) is one column of many slots.+ return {pane: (0, slot) for slot, pane in enumerate(leaves(root))}diff --git a/tests/unit/web/test_agentic_ide_layout_tree.py b/tests/unit/web/test_agentic_ide_layout_tree.pynew file mode 100644index 0000000000..b1a17e4cdf--- /dev/null+++ b/tests/unit/web/test_agentic_ide_layout_tree.py@@ -0,0 +1,354 @@+"""The split tree behind the Agentic-IDE workspace: splits are LOCAL.++The flat "columns × slots" grid could not say "beside the top pane only" —+"split right" was a full-height column by construction, so splitting the top+pane of a stack restructured the whole workspace (reported with a drawing,+2026-08-12). These tests pin the tree's answer: every split and every drop+carves the clicked pane's own rectangle and leaves every cousin alone.+"""++from __future__ import annotations++import pytest++from jarvis.agentic_ide import layout_tree as lt+from jarvis.agentic_ide.layout_tree import Leaf, Split+++def check_canonical(node: lt.LayoutNode | None) -> None:+ """Assert the module's structural invariants, recursively."""+ if node is None or isinstance(node, Leaf):+ return+ assert len(node.children) >= 2, "a container must hold at least two children"+ assert len(node.weights) == len(node.children), "one weight per child"+ assert all(w > 0 for w in node.weights), "weights are positive"+ for child in node.children:+ if isinstance(child, Split):+ assert child.direction != node.direction, "no same-direction nesting"+ check_canonical(child)+++# ------------------------------------------------------------ opening shapes+++def test_wizard_tree_of_one_is_a_bare_leaf() -> None:+ assert lt.wizard_tree(["t1"], 2) == Leaf(pane="t1")+++def test_wizard_tree_stacks_two_deep_before_opening_a_column() -> None:+ tree = lt.wizard_tree(["t1", "t2", "t3", "t4"], 2)+ assert isinstance(tree, Split) and tree.direction == "row"+ assert [lt.leaves(child) for child in tree.children] == [["t1", "t2"], ["t3", "t4"]]+ check_canonical(tree)+++def test_wizard_tree_stands_an_odd_pane_in_a_column_of_its_own() -> None:+ tree = lt.wizard_tree(["t1", "t2", "t3"], 2)+ assert isinstance(tree, Split) and tree.direction == "row"+ assert [lt.leaves(child) for child in tree.children] == [["t1", "t2"], ["t3"]]+++def test_leaves_read_left_to_right_top_to_bottom() -> None:+ tree = lt.wizard_tree(["t1", "t2", "t3", "t4", "t5"], 2)+ assert lt.leaves(tree) == ["t1", "t2", "t3", "t4", "t5"]+++# ------------------------------------------------------- splits stay local+++def test_split_right_on_top_of_a_stack_leaves_the_bottom_full_width() -> None:+ """THE reported bug, as a structure: T2 must keep the whole bottom row."""+ stack = lt.wizard_tree(["t1", "t2"], 2)+ tree = lt.split_pane(stack, "t1", "t3", "right")++ assert isinstance(tree, Split) and tree.direction == "column"+ top, bottom = tree.children+ assert isinstance(top, Split) and top.direction == "row"+ assert lt.leaves(top) == ["t1", "t3"]+ # The bottom pane is a DIRECT child of the stack — full width, untouched.+ assert bottom == Leaf(pane="t2")+ check_canonical(tree)+++def test_split_right_on_the_bottom_of_a_stack_leaves_the_top_full_width() -> None:+ stack = lt.wizard_tree(["t1", "t2"], 2)+ tree = lt.split_pane(stack, "t2", "t3", "right")++ assert isinstance(tree, Split) and tree.direction == "column"+ top, bottom = tree.children+ assert top == Leaf(pane="t1")+ assert isinstance(bottom, Split) and lt.leaves(bottom) == ["t2", "t3"]+++def test_split_right_in_a_row_joins_the_row_and_halves_the_anchor() -> None:+ row = Split(+ direction="row",+ children=[Leaf(pane="t1"), Leaf(pane="t2")],+ weights=[2.0, 1.0],+ )+ tree = lt.split_pane(row, "t1", "t3", "right")++ assert isinstance(tree, Split) and tree.direction == "row"+ assert lt.leaves(tree) == ["t1", "t3", "t2"]+ # The pair shares the anchor's former room; the neighbour keeps its own.+ assert tree.weights == [1.0, 1.0, 1.0]+++def test_split_down_in_a_stack_halves_the_anchor_only() -> None:+ stack = Split(+ direction="column",+ children=[Leaf(pane="t1"), Leaf(pane="t2")],+ weights=[2.0, 2.0],+ )+ tree = lt.split_pane(stack, "t1", "t3", "down")++ assert isinstance(tree, Split) and tree.direction == "column"+ assert lt.leaves(tree) == ["t1", "t3", "t2"]+ assert tree.weights == [1.0, 1.0, 2.0]+++def test_split_down_beside_a_neighbour_leaves_the_neighbour_whole() -> None:+ row = lt.wizard_tree(["t1", "t2"], 1) # two panes side by side+ tree = lt.split_pane(row, "t2", "t3", "down")++ assert isinstance(tree, Split) and tree.direction == "row"+ left, right = tree.children+ assert left == Leaf(pane="t1")+ assert isinstance(right, Split) and right.direction == "column"+ assert lt.leaves(right) == ["t2", "t3"]+++def test_deep_splits_stay_local_at_any_depth() -> None:+ tree: lt.LayoutNode | None = lt.wizard_tree(["t1", "t2"], 2)+ tree = lt.split_pane(tree, "t1", "t3", "right")+ tree = lt.split_pane(tree, "t3", "t4", "down")+ tree = lt.split_pane(tree, "t4", "t5", "right")++ # However deep it went, T2 is still a direct child of the root stack.+ assert isinstance(tree, Split) and tree.direction == "column"+ assert tree.children[1] == Leaf(pane="t2")+ check_canonical(tree)+++def test_anchorless_split_appends_a_full_height_column() -> None:+ tree = lt.split_pane(lt.wizard_tree(["t1", "t2"], 2), None, "t3", "right")+ assert isinstance(tree, Split) and tree.direction == "row"+ assert lt.leaves(tree) == ["t1", "t2", "t3"]+ assert tree.children[1] == Leaf(pane="t3")+++def test_split_on_a_vanished_anchor_falls_back_to_appending() -> None:+ tree = lt.split_pane(Leaf(pane="t1"), "ghost", "t2", "right")+ assert lt.leaves(tree) == ["t1", "t2"]+++def test_split_into_an_empty_workspace_is_a_bare_leaf() -> None:+ assert lt.split_pane(None, None, "t1", "right") == Leaf(pane="t1")+++def test_appending_takes_an_even_share_of_a_dragged_row() -> None:+ row = Split(+ direction="row",+ children=[Leaf(pane="t1"), Leaf(pane="t2")],+ weights=[3.0, 1.0],+ )+ tree = lt.append_pane(row, "t3")+ assert isinstance(tree, Split)+ # The mean of the neighbours, not 1.0 — "an even share" must mean the same+ # thing whatever scale the user's drags left the weights at.+ assert tree.weights == [3.0, 1.0, 2.0]+++# ---------------------------------------------------------------- closing+++def test_removing_a_split_half_folds_the_pair_back_to_one_pane() -> None:+ tree = lt.split_pane(lt.wizard_tree(["t1", "t2"], 2), "t1", "t3", "right")+ slimmed = lt.remove_pane(tree, "t3")++ # The workspace is exactly the stack it was before the split.+ assert slimmed == lt.wizard_tree(["t1", "t2"], 2)+++def test_removing_gives_the_room_to_the_siblings() -> None:+ row = Split(+ direction="row",+ children=[Leaf(pane="t1"), Leaf(pane="t2"), Leaf(pane="t3")],+ weights=[2.0, 1.0, 1.0],+ )+ slimmed = lt.remove_pane(row, "t3")+ assert isinstance(slimmed, Split)+ assert slimmed.weights == [2.0, 1.0] # 2:1 survives, the tail's share dissolves+++def test_removing_the_last_pane_empties_the_tree() -> None:+ assert lt.remove_pane(Leaf(pane="t1"), "t1") is None+++def test_removing_an_unknown_pane_changes_nothing() -> None:+ tree = lt.wizard_tree(["t1", "t2"], 2)+ assert lt.remove_pane(tree, "ghost") == tree+++def test_a_collapse_that_meets_its_grandparent_flattens() -> None:+ # row[ column[row[t1,t2], t3] , t4 ] — closing t3 leaves column holding one+ # row child, which must splice into the outer row, not nest row-in-row.+ tree: lt.LayoutNode | None = Split(+ direction="row",+ children=[+ Split(+ direction="column",+ children=[+ Split(+ direction="row",+ children=[Leaf(pane="t1"), Leaf(pane="t2")],+ weights=[1.0, 1.0],+ ),+ Leaf(pane="t3"),+ ],+ weights=[1.0, 1.0],+ ),+ Leaf(pane="t4"),+ ],+ weights=[1.0, 1.0],+ )+ slimmed = lt.remove_pane(tree, "t3")+ assert isinstance(slimmed, Split) and slimmed.direction == "row"+ assert lt.leaves(slimmed) == ["t1", "t2", "t4"]+ check_canonical(slimmed)+++# ------------------------------------------------------------------ moving+++def test_swap_exchanges_panes_and_keeps_every_weight() -> None:+ tree = Split(+ direction="row",+ children=[Leaf(pane="t1"), Leaf(pane="t2")],+ weights=[3.0, 1.0],+ )+ swapped = lt.move_pane(tree, "t1", "t2", "swap")+ assert isinstance(swapped, Split)+ assert lt.leaves(swapped) == ["t2", "t1"]+ assert swapped.weights == [3.0, 1.0]+++def test_dropping_left_of_a_pane_takes_that_panes_left_half() -> None:+ tree = lt.wizard_tree(["t1", "t2", "t3", "t4"], 2)+ moved = lt.move_pane(tree, "t1", "t4", "left")++ assert isinstance(moved, Split) and moved.direction == "row"+ first, second = moved.children+ # The vacated half folds away: t2 stands alone, full height.+ assert first == Leaf(pane="t2")+ assert isinstance(second, Split) and second.direction == "column"+ assert second.children[0] == Leaf(pane="t3")+ cell = second.children[1]+ assert isinstance(cell, Split) and cell.direction == "row"+ assert lt.leaves(cell) == ["t1", "t4"]+ check_canonical(moved)+++def test_dropping_below_a_pane_takes_that_panes_bottom_half() -> None:+ row = lt.wizard_tree(["t1", "t2"], 1)+ moved = lt.move_pane(row, "t1", "t2", "below")+ assert isinstance(moved, Split) and moved.direction == "column"+ assert lt.leaves(moved) == ["t2", "t1"]+++def test_dropping_on_itself_is_a_no_op() -> None:+ tree = lt.wizard_tree(["t1", "t2"], 2)+ assert lt.move_pane(tree, "t1", "t1", "left") == tree+++def test_a_drop_whose_target_vanished_keeps_the_pane_at_the_edge() -> None:+ tree = lt.wizard_tree(["t1", "t2", "t3"], 2)+ moved = lt.move_pane(tree, "t1", "ghost", "left")+ assert moved is not None+ assert sorted(lt.leaves(moved)) == ["t1", "t2", "t3"]+ check_canonical(moved)+++# ------------------------------------------------- serialization & weights+++def test_round_trip_survives_dict_form() -> None:+ tree = lt.split_pane(lt.wizard_tree(["t1", "t2", "t3"], 2), "t2", "t4", "right")+ assert tree is not None+ assert lt.from_dict(lt.to_dict(tree)) == tree+++ "junk",+ [+ "not a dict",+ {"direction": "diagonal", "children": [{"pane": "a"}, {"pane": "b"}]},+ {"direction": "row", "children": [{"pane": "a"}]},+ {"direction": "row", "children": [{"pane": "a"}, {"pane": "a"}]},+ {"pane": ""},+ ],+)+def test_from_dict_refuses_malformed_trees(junk: object) -> None:+ with pytest.raises(ValueError):+ lt.from_dict(junk)+++def test_from_dict_normalizes_stored_degenerates() -> None:+ # A same-direction nesting written by a buggy or older client flattens on+ # read, with weights scaled so nothing moves on screen.+ stored = {+ "direction": "row",+ "children": [+ {"pane": "t1"},+ {+ "direction": "row",+ "children": [{"pane": "t2"}, {"pane": "t3"}],+ "weights": [1.0, 3.0],+ },+ ],+ "weights": [1.0, 1.0],+ }+ tree = lt.from_dict(stored)+ assert isinstance(tree, Split) and tree.direction == "row"+ assert lt.leaves(tree) == ["t1", "t2", "t3"]+ assert tree.weights == [1.0, 0.25, 0.75]+ check_canonical(tree)+++def test_adopting_weights_needs_the_same_shape() -> None:+ mine = lt.wizard_tree(["t1", "t2", "t3"], 2)+ dragged = lt.wizard_tree(["t1", "t2", "t3"], 2)+ assert isinstance(mine, Split) and isinstance(dragged, Split)+ dragged.weights = [3.0, 1.0]++ assert lt.same_shape(mine, dragged)+ adopted = lt.adopt_weights(mine, dragged)+ assert isinstance(adopted, Split)+ assert adopted.weights == [3.0, 1.0]++ reshaped = lt.split_pane(dragged, "t3", "t9", "down")+ assert not lt.same_shape(mine, reshaped)+++# ------------------------------------------------------- legacy migration+++def test_from_grid_rebuilds_the_columns_of_stacks_shape() -> None:+ legacy = [("t1", 0, 0), ("t2", 0, 1), ("t3", 1, 0)]+ assert lt.from_grid(legacy) == lt.wizard_tree(["t1", "t2", "t3"], 2)+++def test_from_grid_of_nothing_is_an_empty_tree() -> None:+ assert lt.from_grid([]) is None+++def test_grid_hints_are_exact_for_flat_shapes() -> None:+ tree = lt.wizard_tree(["t1", "t2", "t3"], 2)+ assert lt.grid_hints(tree) == {"t1": (0, 0), "t2": (0, 1), "t3": (1, 0)}+++def test_grid_hints_stay_coarse_but_ordered_for_nested_shapes() -> None:+ stack = lt.wizard_tree(["t1", "t2"], 2)+ tree = lt.split_pane(stack, "t1", "t3", "right")+ # One top-level stack → one column; slots follow reading order.+ assert lt.grid_hints(tree) == {"t1": (0, 0), "t3": (0, 1), "t2": (0, 2)}