Skip to content

The agent that grows with you

fix(desktop): a session's previews come back beside the chat, not stacked over it

Brooklyn Nicholsondaa3f0e1d4d3
diff --git a/apps/desktop/src/app/chat/preview-tile.test.ts b/apps/desktop/src/app/chat/preview-tile.test.ts
index ac4fca45dfb..bd393b0a0e6 100644
--- a/apps/desktop/src/app/chat/preview-tile.test.ts
+++ b/apps/desktop/src/app/chat/preview-tile.test.ts
@@ -275,4 +275,23 @@ describe('preview tiles mirror the visible session tabs', () => {
session.$selectedStoredSessionId.set('sess-1')
expect(tree.treePanesWithPrefix('preview-tile:')).toHaveLength(1)
})
+
+ it('brings a session back with its previews beside the chat, not stacked over it', async () => {
+ const { model, preview, session, tree } = await setup()
+
+ session.$selectedStoredSessionId.set('sess-1')
+ preview.openPreview(browserTarget('https://a.example'))
+ preview.openPreview(htmlTarget('/work/a.html'))
+
+ session.$selectedStoredSessionId.set('sess-2')
+ session.$selectedStoredSessionId.set('sess-1')
+
+ const layout = tree.$layoutTree.get()!
+ const chat = model.findGroupOfPane(layout, 'workspace')
+ const previews = tree.treePanesWithPrefix('preview-tile:')
+
+ expect(previews).toHaveLength(2)
+ expect(chat?.panes).toEqual(['workspace'])
+ expect(new Set(previews.map(id => model.findGroupOfPane(layout, id)?.id)).size).toBe(1)
+ })
})
diff --git a/apps/desktop/src/app/chat/preview-tile.tsx b/apps/desktop/src/app/chat/preview-tile.tsx
index a8c4fd264e1..ed48c250351 100644
--- a/apps/desktop/src/app/chat/preview-tile.tsx
+++ b/apps/desktop/src/app/chat/preview-tile.tsx
@@ -212,8 +212,10 @@ function PreviewTabLead({ tabId }: { tabId: string }) {
const previewPaneId = (tabId: string) => `${PREVIEW_TILE_PREFIX}:${tabId}`
/** The pane a NEW preview tile should stack into: another preview tile already
- * in the tree, else another open tab adopted earlier in the same pass (a
- * reload restores every tab at once, before any of them is in the tree).
+ * in the tree, else — when several tabs arrive in one pass (a reload, or a
+ * session switch bringing its tabs back) — the FIRST of them, which itself
+ * gets no anchor and opens the zone. One shared head keeps the anchors
+ * acyclic; adoption places a pane after an anchor arriving with it (store.ts).
* `undefined` means this is the first preview — it opens its own zone. */
function existingPreviewAnchor(tabId: string): string | undefined {
const own = previewPaneId(tabId)
@@ -223,9 +225,9 @@ function existingPreviewAnchor(tabId: string): string | undefined {
return inTree
}
- const other = $dockedVisiblePreviewTabs.get().find(tab => tab.id !== tabId)
+ const head = $dockedVisiblePreviewTabs.get()[0]
- return other ? previewPaneId(other.id) : undefined
+ return head && head.id !== tabId ? previewPaneId(head.id) : undefined
}
/** Keep pane contributions mirroring `$previewTabs`, keep the store's selection
diff --git a/apps/desktop/src/components/pane-shell/tree/model.ts b/apps/desktop/src/components/pane-shell/tree/model.ts
index 5fce015b6c1..6e19a6edd93 100644
--- a/apps/desktop/src/components/pane-shell/tree/model.ts
+++ b/apps/desktop/src/components/pane-shell/tree/model.ts
@@ -240,6 +240,19 @@ export function removePane(node: LayoutNode, paneId: string): LayoutNode | null
return normalize(walk(node))
}
+/**
+ * Order panes for one adoption pass so a pane docked against another pane in
+ * the SAME pass comes after it. Adopted first, its anchor isn't in the tree yet
+ * and it would fall back into main's zone (a session's preview tabs returning
+ * stacked behind the chat). Stable: everything else keeps its order.
+ */
+export function anchorsFirst<T extends { id: string }>(panes: T[], anchorOf: (id: string) => string | undefined): T[] {
+ const ids = new Set(panes.map(pane => pane.id))
+ const waits = (pane: T) => Number(ids.has(anchorOf(pane.id) ?? ''))
+
+ return [...panes].sort((a, b) => waits(a) - waits(b))
+}
+
/**
* Insert `paneId` at `target` group: `center` joins the stack (as a tab);
* an edge splits the group in that direction. If the neighboring split
diff --git a/apps/desktop/src/components/pane-shell/tree/store.ts b/apps/desktop/src/components/pane-shell/tree/store.ts
index 0b1a1e8f9a7..d838c96cb81 100644
--- a/apps/desktop/src/components/pane-shell/tree/store.ts
+++ b/apps/desktop/src/components/pane-shell/tree/store.ts
@@ -20,6 +20,7 @@ import { isBrowserWindow, isSecondaryWindow } from '@/store/windows'
import {
allPaneIds,
+ anchorsFirst,
type DropPosition,
findGroup,
findGroupOfPane,
@@ -1542,7 +1543,6 @@ export function adoptContributedPanes(): void {
const placementOf = (paneId: string) => dataOf(paneId)?.placement
const mainId = panes.find(c => placementOf(c.id) === 'main')?.id
const inTree = new Set(allPaneIds(tree))
-
const dismissed = $dismissedPanes.get()
// Enforced dock invariants run FIRST: they re-home panes that are ALREADY
@@ -1568,7 +1568,7 @@ export function adoptContributedPanes(): void {
let next = healed
- for (const pane of missing) {
+ for (const pane of anchorsFirst(missing, id => dataOf(id)?.dock?.pane)) {
const dock = dataOf(pane.id)?.dock
const placement = placementOf(pane.id) ?? 'right'