Skip to content

fix(sidebar): stack same-branch hosts at every level of a workspace hierarchy - #456

Merged
nplez1 merged 6 commits into
mainfrom
nplez1/Bug-Worktree-Stacking
Nov 23, 2026
Merged

nplez1 merged 6 commits into
mainfrom
nplez1/Bug-Worktree-Stacking

Conversation

@nplez1

@nplez1 nplez1 commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Reported: a branch checked out on two hosts — M5 Studio and Win — stops being one stacked card
with the host selector the moment one of the two checkouts gains a child workspace. With no child the
rows merge as intended; add one and the branch splits back into one row per host. Asked for on top of
that: when the same hierarchy exists on both hosts, every workspace in it should carry the
dropdown and only one hierarchy should be drawn, and changing a dropdown must switch that card's
agents and terminals but not the child workspace list.

Two defects produced the first symptom. The merge's candidate filter rejected any row with
lineageChildCount > 0, an exclusion written before lineage rows existed; and the merge only ever
looked at depth-0 rows, so a hierarchy could only stack on its top level. The tab strip's
getBranchGroupMembersForWorktree never had the exclusion, which is why the strip and the sidebar
disagreed.

What changed

  • The merge is recursive (grouping/same-branch-row-merge.ts, same-branch-merged-row.ts,
    sidebar-row-forest.ts). The row list is read as a depth-keyed tree, siblings are grouped by branch
    at every level, and the result is flattened back. The same three-level hierarchy on two hosts is now
    three stacked cards, each with the host dropdown, not two trees.
  • A card owns its members' whole subtree. Child count, child ids and descendant identities are
    unions, and every member's children are drawn under the card in host order — so the child list is
    the same whichever host the card shows. One chip click folds the union: the card writes and clears
    every member's lineage group key at once and reads folded if any is folded, so it can never sit half
    collapsed and strand rows.
  • Host picks resolve by row key (use-branch-group-host-selection.ts), so a nested card on the
    same branch as its parent activates its own member instead of the parent's.
  • Fold keys are published per workspace (indexLineageFoldKeys + a setVisibleLineageFoldKeys
    channel, visible-worktrees.ts). It keys the hosts a card absorbed, which render no row of their
    own, so the child-workspaces shortcut works when the active workspace is a non-primary parent or a
    relocated child — without touching the Cmd+1-9 order, which names rows.
  • Reveal unfolds whole cards (worktree-reveal-group-keys.ts). Members are resolved the way the
    merge resolves them — the same-branch workspaces among the previous level's members' children — so a
    leaf under a folded nested card clears every key above it.

Decisions

  • One card per level, not one card for the whole hierarchy. Every workspace keeping its own
    dropdown is the requirement, and it is what makes the child list host-independent: each level folds
    only its own members.
  • Union children, always. A card that showed only its own host's children would hide the others;
    the strip already lists every host's sibling checkout, so the sidebar now agrees with it.
  • The chip counts cards, not workspaces. Two children on two hosts are one nested card, so the
    count is the rows the next level renders. A folded card still counts the rows it hides.
  • Duplicates are accepted when parentage differs. If one workspace nests under a parent on each
    host, it draws twice rather than guessing which host's parentage wins.
  • The merge is skipped where it cannot be sound: pinned rows, an explicit host scope or filter
    (rows become host sections), and two checkouts of one branch on the same host (ambiguous — one
    would hide behind the dropdown).

Verification

Check Result
orca-queue node local/gate-unit.mjs PASS — 27672 passed, 0 failed, 0 unexpected, 248 s @c44c31aafb
pnpm test (merge, reveal, host-pick, collapse-toggle) 43 passed
pnpm tc:web clean
pnpm check:changed 37 of 40 green
pnpm run audit:code-quality:native clean
node local/check-upstream-file-comments.mjs 5113/5113 (frozen baseline)

The three failing check:changed ratchets (direct-link-open, main-blocking-api,
react-compiler-compat) are pre-existing baseline drift in files this branch does not touch.

Tests added: a three-level hierarchy on two hosts collapses to three cards, each with both hosts in
its dropdown; child counts follow the cards drawn; a nested card folds without folding its parent;
children on their own branches still relocate under the merged parent; deep chains (to depth 3) move
intact; a child never merges with its parent; the child list is the same whichever host a card shows;
the collapse-key toggle unfolds a half-folded set rather than re-folding it; the reveal clears both
keys of a folded nested card.

Disclosed, not fixed in this PR

  • Unfolding a merged card can change its chip number ("Show 2 child workspaces" → "Hide 1 child
    workspace") because a folded member's child rows were never planned, so the count can only be
    recomputed once they exist.
  • Two cards on the same branch at different levels share one host-choice key, so picking a host on the
    child also switches the parent's card (only the child is activated).
  • A merged level can carry two rows flagged isLastLineageChild — each host's own last child. Only the
    parity/golden serializer reads that flag.
  • The closed-sidebar fold-key fallback passes LOCAL_EXECUTION_HOST_ID as its reference host; a client
    whose default host is remote could merge differently there than the mounted list does.

Reviewed by Opus 5.5, high: R1 (3 blocking fixed), R2 delta (1 blocking fixed), R3 delta (1 blocking fixed), delta clean on R4 (1 MINOR fixed as prescribed)

The review covers the code as of 3893e557ce. The follow-up commits since then only satisfy the fork lint gate: they remove the fork comment lines this branch had put above upstream-owned files (local/check-upstream-file-comments.mjs, back to its frozen 5113) and merge one duplicated import that audit:code-quality:native refuses. No behaviour changes.

A root workspace with lineage children was excluded from the Group by branch
merge, so as soon as one host's checkout gained a child, that branch split back
into one row per host and lost the host selector.

Roots with children now merge like any other root. The merged card owns the
union of every member's children: the chip counts and summarises all of them,
their rows are drawn beneath the card even when they belong to another host, and
one click folds the whole union — the card writes and clears every member's
lineage group key at once, so it can never sit half collapsed. Revealing a child
unfolds every key of the card it hangs under.

Verification: branch-groups merge tests (stacks a parent with children, unions
the counts, moves the other host's subtree up, folds all members, collapse-key
toggle tests, tc:web, check:changed (only the three pre-existing baseline-drift
ratchets fail: direct-link-open, main-blocking-api, react-compiler-compat).
Not verified: rendered UI with two live hosts.
EOF
)
…ierarchy

A root workspace with lineage children was excluded from the Group by branch
merge, so as soon as one host's checkout gained a child, that branch split back
into one row per host and lost the host selector.

The merge is now a recursive pass over the row tree: siblings are grouped by
branch at every depth, not only at the root. The same hierarchy on two hosts
therefore collapses into one tree in which every workspace — parents and
children alike — carries the host dropdown, and each level's card owns the union
of its members' subtree.

The dropdown changes only what the card represents (its agents and terminals):
children are always the union across every member, so the child list under a
card is identical whichever host it shows. Host picks are resolved by row key,
so a nested card on the same branch as its parent activates its own member.

Review round 1 (claude/opus, high) found three SHOULD-FIX:
- the half-collapsed union showed folded but the first click folded again, so
  nothing changed on screen; the key set now folds only when nothing is folded,
  and the shortcut's expand detection matches it;
- a non-primary member's workspace could not reach the child-workspaces
  shortcut; the fold keys are published per workspace, including the hosts a
  card absorbed, on their own channel so the Cmd+1-9 order is untouched;
- the sidebar-closed fallback derived keys without the merge; it now applies the
  same gated merge. Reveal also stops unfolding other hosts' groups when no
  merge happened, and a merged card's child count is the sum of its members'
  counts (ids can repeat across hosts).
- A depth-less row (section header, imported-worktrees card) attached to the open
  workspace above it, so a merged last row of a section could swallow the next
  section's header and reorder or hide the rows under it. It now closes the
  subtree stack and lands at the root, as the pre-recursion walk did.
- Reveal matched members only for root ancestors, leaving a nested card folded
  and its revealed child hidden. Members are now the ancestor's same-branch
  siblings at its own level, which is what the merge grouped.
- A card counted workspaces while it drew cards: two children on two hosts are one
  nested card, so the chip now counts the rows the next level renders — except
  when folded, where the hidden rows are still counted.
…vel by level

Reveal still left a nested card folded: it matched members among rows sharing an
occurrence parent, but a merged card's members sit under different parents (each
host's own. Members are now resolved the way the merge resolves them — the
same-branch workspaces among the previous level's members' children — so
revealing a leaf clears every key of every card above it, at any depth.

Covered by a new test on the reveal itself (no test reached it before): a folded
mid card on both hosts, reveal a leaf under one of them, expect both keys.
EOF
)
A pinned root is never one of the merge's candidates, so a three-host hierarchy
with the local root pinned resolved level 0 to the other two hosts and dropped
the pinned ancestor from the walk, leaving it folded when a child was revealed.
A level now falls back to the ancestor alone when the resolved member set does
not contain it.
…de-dupe an import

The fork's upstream-file-comments ratchet refuses added comment lines above
upstream's code: they are conflict adjacency, and the sync that measured it paid
for them hunk by hunk. The rationale lives in the fork-owned merge modules, so
the comments come off the six upstream files this branch touched — 5113/5113 on
the frozen baseline.
@nplez1
nplez1 merged commit c318577 into main Nov 23, 2026
13 checks passed
@nplez1
nplez1 deleted the nplez1/Bug-Worktree-Stacking branch November 23, 2026 05:04
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.

1 participant