Skip to content

The agent that grows with you

refactor(desktop): move preview tab ownership into store/preview-ownership.ts

teknium1248091cd8321
diff --git a/apps/desktop/src/app/chat/right-rail/preview-act.ts b/apps/desktop/src/app/chat/right-rail/preview-act.ts
index 01022ebedbc..c45e4219de9 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-act.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-act.ts
@@ -29,7 +29,7 @@
import { actEngineSource, type PreviewActAction, type PreviewActResult } from '@/lib/preview-act/act-in-page'
import { watchInPage } from '@/lib/preview-act/watch-in-page'
-import type { PreviewOwner } from '@/store/preview'
+import type { PreviewOwner } from '@/store/preview-ownership'
import {
clearCharsBack,
diff --git a/apps/desktop/src/app/chat/right-rail/preview-active-tab.ts b/apps/desktop/src/app/chat/right-rail/preview-active-tab.ts
index 40d2421317e..79f64abb230 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-active-tab.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-active-tab.ts
@@ -12,8 +12,9 @@
import { findGroup } from '@/components/pane-shell/tree/model'
import { $activeTreeGroup, $hoveredTreeGroup, $layoutTree } from '@/components/pane-shell/tree/store'
import { $rightRailActiveTabId } from '@/store/layout'
-import { type PreviewOwner, type PreviewTab, previewTabsFor } from '@/store/preview'
+import { type PreviewTab, previewTabsFor } from '@/store/preview'
import { explicitOpenBlocksZone, PREVIEW_TILE_PREFIX } from '@/store/preview-explicit'
+import type { PreviewOwner } from '@/store/preview-ownership'
function tabIdFromPreviewPane(paneId: string | undefined): null | string {
if (!paneId?.startsWith(`${PREVIEW_TILE_PREFIX}:`)) {
diff --git a/apps/desktop/src/app/chat/right-rail/preview-input.ts b/apps/desktop/src/app/chat/right-rail/preview-input.ts
index c5043fab2ab..eca5b4e0619 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-input.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-input.ts
@@ -19,7 +19,7 @@
* must be scaled by the guest's zoom factor on the way back out (#116281).
*/
-import type { PreviewOwner } from '@/store/preview'
+import type { PreviewOwner } from '@/store/preview-ownership'
import { activePreviewTabFor } from './preview-active-tab'
diff --git a/apps/desktop/src/app/chat/right-rail/preview-nav.ts b/apps/desktop/src/app/chat/right-rail/preview-nav.ts
index 10abf7b5d11..ef311637fae 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-nav.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-nav.ts
@@ -10,7 +10,7 @@
*/
import { isElementInHiddenPane } from '@/components/pane-shell/pane-visibility'
-import type { PreviewOwner } from '@/store/preview'
+import type { PreviewOwner } from '@/store/preview-ownership'
import { activePreviewTabFor } from './preview-active-tab'
diff --git a/apps/desktop/src/app/chat/right-rail/preview-nudge.ts b/apps/desktop/src/app/chat/right-rail/preview-nudge.ts
index a2f3f360e56..043ea5685fc 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-nudge.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-nudge.ts
@@ -15,7 +15,7 @@
*/
import type { WatchStage } from '@/lib/preview-act/watch-in-page'
-import type { PreviewOwner } from '@/store/preview'
+import type { PreviewOwner } from '@/store/preview-ownership'
import { activePreviewScriptRunner } from './preview-script-runner'
diff --git a/apps/desktop/src/app/chat/right-rail/preview-popout-bridge.ts b/apps/desktop/src/app/chat/right-rail/preview-popout-bridge.ts
index 24a9c7e3370..33cfaf84fda 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-popout-bridge.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-popout-bridge.ts
@@ -16,7 +16,8 @@
*/
import type { PreviewActAction, PreviewActResult } from '@/lib/preview-act/act-in-page'
-import { type PreviewOwner, previewTabIdsVisibleTo } from '@/store/preview'
+import { previewTabIdsVisibleTo } from '@/store/preview'
+import type { PreviewOwner } from '@/store/preview-ownership'
import { isBrowserWindow, windowBrowserTabId } from '@/store/windows'
import { actOnActivePreview } from './preview-act'
diff --git a/apps/desktop/src/app/chat/right-rail/preview-reader.ts b/apps/desktop/src/app/chat/right-rail/preview-reader.ts
index d2a16ee0944..ef867d69c20 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-reader.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-reader.ts
@@ -13,7 +13,8 @@
* directly (read_file / the conversation's artifact).
*/
-import { type PreviewOwner, type PreviewTab, previewTabsFor } from '@/store/preview'
+import { type PreviewTab, previewTabsFor } from '@/store/preview'
+import type { PreviewOwner } from '@/store/preview-ownership'
import { resolveActivePreviewTab } from './preview-active-tab'
import { nudgeOverlay } from './preview-nudge'
diff --git a/apps/desktop/src/app/chat/right-rail/preview-script-runner.ts b/apps/desktop/src/app/chat/right-rail/preview-script-runner.ts
index 20a044112e9..b2b52f782eb 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-script-runner.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-script-runner.ts
@@ -10,7 +10,7 @@
* of the pane component's static import graph and only load when used.
*/
-import type { PreviewOwner } from '@/store/preview'
+import type { PreviewOwner } from '@/store/preview-ownership'
import { activePreviewTabFor } from './preview-active-tab'
diff --git a/apps/desktop/src/app/chat/right-rail/preview-tour.ts b/apps/desktop/src/app/chat/right-rail/preview-tour.ts
index 6249a437cd9..a18d76b9428 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-tour.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-tour.ts
@@ -20,7 +20,7 @@ import driverIife from 'driver.js/dist/driver.js.iife.js?raw'
import { collectTourTargets } from '@/lib/tour/collect-targets'
import { runTourEngine, type TourAction, type TourResult } from '@/lib/tour/engine'
-import type { PreviewOwner } from '@/store/preview'
+import type { PreviewOwner } from '@/store/preview-ownership'
import { activePreviewScriptRunner } from './preview-script-runner'
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.ts
index 65e74bb1828..a596acac024 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/server-requests.ts
@@ -20,7 +20,7 @@ import type { TourAction, TourStep } from '@/lib/tour'
import { normalizeQuestions, setClarifyRequest } from '@/store/clarify'
import type { ScopedServerRequest } from '@/store/gateway'
import { dispatchNativeNotification } from '@/store/native-notifications'
-import type { PreviewOwner } from '@/store/preview'
+import type { PreviewOwner } from '@/store/preview-ownership'
import {
receiveApprovalRequest,
setSecretRequest,
diff --git a/apps/desktop/src/app/session/hooks/use-preview-routing.ts b/apps/desktop/src/app/session/hooks/use-preview-routing.ts
index 61e44cee3bb..a7939dcce1e 100644
--- a/apps/desktop/src/app/session/hooks/use-preview-routing.ts
+++ b/apps/desktop/src/app/session/hooks/use-preview-routing.ts
@@ -10,11 +10,11 @@ import {
closeAgentPreview,
completePreviewServerRestart,
openPreview,
- type PreviewOwner,
progressPreviewServerRestart,
renderedHtmlTarget,
requestPreviewReload
} from '@/store/preview'
+import type { PreviewOwner } from '@/store/preview-ownership'
import { $activeSessionId, $currentCwd } from '@/store/session'
import { $focusedStoredSessionId } from '@/store/session-focus'
import {
diff --git a/apps/desktop/src/lib/tour/run-tour.ts b/apps/desktop/src/lib/tour/run-tour.ts
index 42c428b46d3..cac18dc6007 100644
--- a/apps/desktop/src/lib/tour/run-tour.ts
+++ b/apps/desktop/src/lib/tour/run-tour.ts
@@ -18,7 +18,7 @@ import { driver as driverFactory } from 'driver.js'
import { runPreviewTour } from '@/app/chat/right-rail/preview-tour'
import { revealDesktopPane } from '@/store/pane-focus'
-import type { PreviewOwner } from '@/store/preview'
+import type { PreviewOwner } from '@/store/preview-ownership'
import { collectTourTargets } from './collect-targets'
import {
diff --git a/apps/desktop/src/store/preview-ownership.ts b/apps/desktop/src/store/preview-ownership.ts
new file mode 100644
index 00000000000..6d5dd418440
--- /dev/null
+++ b/apps/desktop/src/store/preview-ownership.ts
@@ -0,0 +1,200 @@
+/**
+ * PREVIEW TAB OWNERSHIP — who a preview tab belongs to.
+ *
+ * A tab is owned by the stored session that opened it (resolved through every
+ * compression rotation), or — before that session's stored id exists — by the
+ * runtime that opened it (a pending tab) or by the draft on screen (a true
+ * draft tab). The drawer, agent tools and pop-out scoping all answer "whose is
+ * this tab?" here; `preview.ts` owns the tab list and its storage.
+ */
+
+import { atom, computed } from 'nanostores'
+
+import type { PreviewTab } from './preview'
+import { normalizeProfileKey } from './profile'
+import { $activeSessionId, $sessions, sessionMatchesStoredId } from './session'
+import { $focusedStoredSessionId } from './session-focus'
+
+// An ownerless tab opened while a live chat was on screen whose stored id had
+// not arrived yet (the agent's open_preview right after the first send): the
+// runtime it was opened under. Memory-only — runtime ids do not survive a
+// relaunch. A draft opened before any send has no runtime, so its tabs carry
+// no stamp and only adoptDraftPreviewTabs can take them.
+export const $pendingRuntimeByTab = atom<ReadonlyMap<string, string>>(new Map())
+
+// Auto-compression rotates a conversation's stored id (tip → next tip). The
+// tabs are rekeyed onto the new tip, and the old id stays an alias so the
+// window between the rotation and the focus following it cannot hide them —
+// a hidden Browser's pane leaves the tree and would lose its page.
+export const $rotatedSessionIds = atom<ReadonlyMap<string, string>>(new Map())
+
+export function latestSessionId(
+ sessionId: null | string | undefined,
+ rotated: ReadonlyMap<string, string>
+): null | string {
+ let id = sessionId ?? null
+
+ // Acyclic by construction (`rekeyPreviewTabsSession` only links one tip to
+ // a different tip), so no chain is longer than the map.
+ for (let hops = 0; id && rotated.has(id) && hops < rotated.size; hops++) {
+ id = rotated.get(id)!
+ }
+
+ return id
+}
+
+/** `sessionId` resolved through every compression rotation seen so far. */
+export function currentSessionId(sessionId: null | string | undefined): null | string {
+ return latestSessionId(sessionId, $rotatedSessionIds.get())
+}
+
+/** True when `sessionId`'s drawer shows `tab`: its own tabs plus every pin.
+ * With no session (a fresh draft) the true draft tabs are its own — never a
+ * runtime's pending tab, which only that runtime's drawer shows. */
+function tabVisibleTo(
+ tab: PreviewTab,
+ sessionId: null | string,
+ rotated: ReadonlyMap<string, string>,
+ pending: ReadonlyMap<string, string>
+): boolean {
+ if (tab.pinned) {
+ return true
+ }
+
+ if (tab.sessionId == null && pending.has(tab.id)) {
+ return false
+ }
+
+ return latestSessionId(tab.sessionId, rotated) === latestSessionId(sessionId, rotated)
+}
+
+/** Who an agent tool acts for: a stored id (null = none bound yet), or the
+ * full identity of the runtime asking — its stored id, the runtime itself
+ * (the tabs it opened before that id bound are its own) and the profile it
+ * belongs to, whose bucket is the only one whose pins it may use. A bare id
+ * (no profile) is a request from the chat on screen: the viewed bucket's
+ * pins. */
+export type PreviewOwner =
+ null | string | { profile?: null | string; runtimeId: null | string; sessionId: null | string }
+
+export interface OwnerIdentity {
+ /** Normalized profile whose pins the owner shares; null = the view's. */
+ profile: null | string
+ runtimeId: null | string
+ sessionId: null | string
+}
+
+export function ownerIdentity(owner: PreviewOwner): OwnerIdentity {
+ if (owner === null || typeof owner !== 'object') {
+ return { profile: null, runtimeId: null, sessionId: owner }
+ }
+
+ return {
+ profile: owner.profile ? normalizeProfileKey(owner.profile) || 'default' : null,
+ runtimeId: owner.runtimeId,
+ sessionId: owner.sessionId
+ }
+}
+
+/** True when `tab` is `who`'s own — pins aside. A stored owner matches by id
+ * through every compression rotation. An ownerless tab is one of two things,
+ * never a shared pool: a RUNTIME's pending tab (opened before its stored id
+ * bound), which only that runtime owns; or a true DRAFT tab (opened in a
+ * fresh draft before any runtime existed), which only the draft on screen
+ * owns — the primary's runtime while it has no stored id, or a request with
+ * no runtime at all. `inView`: a draft tab outside the viewed bucket belongs
+ * to no draft on screen. */
+function ownsTab(tab: PreviewTab, who: OwnerIdentity, inView: boolean): boolean {
+ if (tab.sessionId != null) {
+ const rotated = $rotatedSessionIds.get()
+
+ return who.sessionId != null && latestSessionId(tab.sessionId, rotated) === latestSessionId(who.sessionId, rotated)
+ }
+
+ const draftRuntime = $activeSessionId.get()
+ // A request naming no runtime and no session speaks for the draft on screen.
+ const runtime = who.runtimeId ?? (who.sessionId == null ? draftRuntime : null)
+ const pendingRuntime = $pendingRuntimeByTab.get().get(tab.id)
+
+ if (pendingRuntime !== undefined) {
+ return runtime === pendingRuntime
+ }
+
+ return inView && who.sessionId == null && runtime === draftRuntime
+}
+
+/** `who`'s tabs in bucket `key`: its own, plus the bucket's pins when it is
+ * `who`'s profile. `viewKey` is the bucket on screen. */
+export function bucketTabsFor(
+ tabs: readonly PreviewTab[],
+ who: OwnerIdentity,
+ key: string,
+ viewKey: string
+): PreviewTab[] {
+ const sharesPins = key === (who.profile ?? viewKey)
+
+ return tabs.filter(tab => (tab.pinned && sharesPins) || ownsTab(tab, who, key === viewKey))
+}
+
+/** The primary's selection names a session the list already holds — one being
+ * resumed — rather than a stored id no row carries yet, which can only be the
+ * active runtime's own id arriving. */
+export const $selectionIsListed = computed([$focusedStoredSessionId, $sessions], (sessionId, sessions) =>
+ Boolean(sessionId && sessions.some(session => sessionMatchesStoredId(session, sessionId)))
+)
+
+/** What the drawer is showing for: the focused session and the runtime on
+ * screen (see `$visiblePreviewTabs`). */
+export interface DrawerView {
+ activeRuntime: null | string
+ focusedIsTile: boolean
+ pending: ReadonlyMap<string, string>
+ rotated: ReadonlyMap<string, string>
+ selectionIsListed: boolean
+ sessionId: null | string
+}
+
+/** True when the focused session's drawer shows `tab`: its own tabs and pins,
+ * plus — for the primary, never under a listed session — the tabs its runtime
+ * opened before its stored id arrived. */
+export function drawerShowsTab(tab: PreviewTab, view: DrawerView): boolean {
+ return (
+ tabVisibleTo(tab, view.sessionId, view.rotated, view.pending) ||
+ (!view.focusedIsTile &&
+ !view.selectionIsListed &&
+ tab.sessionId == null &&
+ view.activeRuntime !== null &&
+ view.pending.get(tab.id) === view.activeRuntime)
+ )
+}
+
+export function setPendingRuntime(tabId: string, runtimeId: null | string): void {
+ const current = $pendingRuntimeByTab.get()
+
+ if ((current.get(tabId) ?? null) === runtimeId) {
+ return
+ }
+
+ const next = new Map(current)
+
+ if (runtimeId) {
+ next.set(tabId, runtimeId)
+ } else {
+ next.delete(tabId)
+ }
+
+ $pendingRuntimeByTab.set(next)
+}
+
+/** `runtimeId`'s session state was dropped before its stored id bound: its
+ * tabs are plain ownerless tabs again, which the next draft's first send
+ * adopts. Omitted = every runtime (all session states were dropped). */
+export function forgetPendingRuntimeTabs(runtimeId?: string): void {
+ const pending = $pendingRuntimeByTab.get()
+
+ if ([...pending.values()].some(runtime => runtimeId === undefined || runtime === runtimeId)) {
+ $pendingRuntimeByTab.set(
+ new Map(runtimeId === undefined ? [] : [...pending].filter(([, runtime]) => runtime !== runtimeId))
+ )
+ }
+}
diff --git a/apps/desktop/src/store/preview.ts b/apps/desktop/src/store/preview.ts
index aa564cda84c..22f8f53d804 100644
--- a/apps/desktop/src/store/preview.ts
+++ b/apps/desktop/src/store/preview.ts
@@ -12,8 +12,19 @@ import { normalize } from '@/lib/text'
import { recordFeatureUse } from './desktop-metrics'
import { $rightRailActiveTabId, type RightRailTabId, selectRightRailTab } from './layout'
import { clearExplicitPreviewOpen, noteExplicitPreviewOpen, PREVIEW_TILE_PREFIX } from './preview-explicit'
+import {
+ $pendingRuntimeByTab,
+ $rotatedSessionIds,
+ $selectionIsListed,
+ bucketTabsFor,
+ currentSessionId,
+ drawerShowsTab,
+ ownerIdentity,
+ type PreviewOwner,
+ setPendingRuntime
+} from './preview-ownership'
import { normalizeProfileKey } from './profile'
-import { $activeSessionId, $sessions, sessionMatchesStoredId } from './session'
+import { $activeSessionId } from './session'
import { $focusedSessionIsTile, $focusedStoredSessionId } from './session-focus'
import { canOpenBrowserWindow, isBrowserWindow, openBrowserInNewWindow, windowBrowserTabId } from './windows'
@@ -256,13 +267,6 @@ export const $previewTabs = atom<PreviewTab[]>([])
// its `default` bucket the same way.
let adoptingStoredTabs = true
-// An ownerless tab opened while a live chat was on screen whose stored id had
-// not arrived yet (the agent's open_preview right after the first send): the
-// runtime it was opened under. Memory-only — runtime ids do not survive a
-// relaunch. A draft opened before any send has no runtime, so its tabs carry
-// no stamp and only adoptDraftPreviewTabs can take them.
-const $pendingRuntimeByTab = atom<ReadonlyMap<string, string>>(new Map())
-
$previewTabs.subscribe(tabs => {
if (adoptingStoredTabs) {
return
@@ -368,112 +372,6 @@ if (typeof window !== 'undefined') {
}
}
-// Auto-compression rotates a conversation's stored id (tip → next tip). The
-// tabs are rekeyed onto the new tip, and the old id stays an alias so the
-// window between the rotation and the focus following it cannot hide them —
-// a hidden Browser's pane leaves the tree and would lose its page.
-const $rotatedSessionIds = atom<ReadonlyMap<string, string>>(new Map())
-
-function latestSessionId(sessionId: null | string | undefined, rotated: ReadonlyMap<string, string>): null | string {
- let id = sessionId ?? null
-
- // Acyclic by construction (`rekeyPreviewTabsSession` only links one tip to
- // a different tip), so no chain is longer than the map.
- for (let hops = 0; id && rotated.has(id) && hops < rotated.size; hops++) {
- id = rotated.get(id)!
- }
-
- return id
-}
-
-/** `sessionId` resolved through every compression rotation seen so far. */
-function currentSessionId(sessionId: null | string | undefined): null | string {
- return latestSessionId(sessionId, $rotatedSessionIds.get())
-}
-
-/** True when `sessionId`'s drawer shows `tab`: its own tabs plus every pin.
- * With no session (a fresh draft) the true draft tabs are its own — never a
- * runtime's pending tab, which only that runtime's drawer shows. */
-function tabVisibleTo(
- tab: PreviewTab,
- sessionId: null | string,
- rotated: ReadonlyMap<string, string>,
- pending: ReadonlyMap<string, string>
-): boolean {
- if (tab.pinned) {
- return true
- }
-
- if (tab.sessionId == null && pending.has(tab.id)) {
- return false
- }
-
- return latestSessionId(tab.sessionId, rotated) === latestSessionId(sessionId, rotated)
-}
-
-/** Who an agent tool acts for: a stored id (null = none bound yet), or the
- * full identity of the runtime asking — its stored id, the runtime itself
- * (the tabs it opened before that id bound are its own) and the profile it
- * belongs to, whose bucket is the only one whose pins it may use. A bare id
- * (no profile) is a request from the chat on screen: the viewed bucket's
- * pins. */
-export type PreviewOwner =
- null | string | { profile?: null | string; runtimeId: null | string; sessionId: null | string }
-
-interface OwnerIdentity {
- /** Normalized profile whose pins the owner shares; null = the view's. */
- profile: null | string
- runtimeId: null | string
- sessionId: null | string
-}
-
-function ownerIdentity(owner: PreviewOwner): OwnerIdentity {
- if (owner === null || typeof owner !== 'object') {
- return { profile: null, runtimeId: null, sessionId: owner }
- }
-
- return {
- profile: owner.profile ? normalizeProfileKey(owner.profile) || 'default' : null,
- runtimeId: owner.runtimeId,
- sessionId: owner.sessionId
- }
-}
-
-/** True when `tab` is `who`'s own — pins aside. A stored owner matches by id
- * through every compression rotation. An ownerless tab is one of two things,
- * never a shared pool: a RUNTIME's pending tab (opened before its stored id
- * bound), which only that runtime owns; or a true DRAFT tab (opened in a
- * fresh draft before any runtime existed), which only the draft on screen
- * owns — the primary's runtime while it has no stored id, or a request with
- * no runtime at all. `inView`: a draft tab outside the viewed bucket belongs
- * to no draft on screen. */
-function ownsTab(tab: PreviewTab, who: OwnerIdentity, inView: boolean): boolean {
- if (tab.sessionId != null) {
- const rotated = $rotatedSessionIds.get()
-
- return who.sessionId != null && latestSessionId(tab.sessionId, rotated) === latestSessionId(who.sessionId, rotated)
- }
-
- const draftRuntime = $activeSessionId.get()
- // A request naming no runtime and no session speaks for the draft on screen.
- const runtime = who.runtimeId ?? (who.sessionId == null ? draftRuntime : null)
- const pendingRuntime = $pendingRuntimeByTab.get().get(tab.id)
-
- if (pendingRuntime !== undefined) {
- return runtime === pendingRuntime
- }
-
- return inView && who.sessionId == null && runtime === draftRuntime
-}
-
-/** `who`'s tabs in one bucket: its own, plus the bucket's pins when it is
- * `who`'s profile. */
-function bucketTabsFor(tabs: readonly PreviewTab[], who: OwnerIdentity, key: string): PreviewTab[] {
- const sharesPins = key === (who.profile ?? viewKey)
-
- return tabs.filter(tab => (tab.pinned && sharesPins) || ownsTab(tab, who, key === viewKey))
-}
-
/** The tabs an agent tool acting for `owner` (default: the focused session)
* may read or drive: never another session's hidden tab, and never another
* profile's pin. A popped-out Browser renderer answers only for the one tab
@@ -486,7 +384,7 @@ export function previewTabsFor(owner: PreviewOwner = $focusedStoredSessionId.get
return $previewTabs.get().filter(tab => tab.id === own)
}
- return bucketTabsFor($previewTabs.get(), ownerIdentity(owner), viewKey)
+ return bucketTabsFor($previewTabs.get(), ownerIdentity(owner), viewKey, viewKey)
}
/** Ids of the tabs `owner` may see in ANY profile's rail, for scoping a
@@ -501,16 +399,9 @@ export function previewTabIdsVisibleTo(owner: PreviewOwner): string[] {
...Object.entries(tabsByProfile).filter(([key]) => key !== viewKey)
]
- return buckets.flatMap(([key, tabs]) => bucketTabsFor(tabs, who, key)).map(tab => tab.id)
+ return buckets.flatMap(([key, tabs]) => bucketTabsFor(tabs, who, key, viewKey)).map(tab => tab.id)
}
-/** The primary's selection names a session the list already holds — one being
- * resumed — rather than a stored id no row carries yet, which can only be the
- * active runtime's own id arriving. */
-const $selectionIsListed = computed([$focusedStoredSessionId, $sessions], (sessionId, sessions) =>
- Boolean(sessionId && sessions.some(session => sessionMatchesStoredId(session, sessionId)))
-)
-
/** Tabs the FOCUSED session sees. The layout-tree mirror renders only these,
* so a session switch swaps the drawer; hidden tabs stay in `$previewTabs`.
* The primary also shows the tabs its runtime opened before its stored id
@@ -528,14 +419,8 @@ export const $visiblePreviewTabs = computed(
$selectionIsListed
],
(tabs, sessionId, rotated, pending, activeRuntime, focusedIsTile, selectionIsListed) =>
- tabs.filter(
- tab =>
- tabVisibleTo(tab, sessionId, rotated, pending) ||
- (!focusedIsTile &&
- !selectionIsListed &&
- tab.sessionId == null &&
- activeRuntime !== null &&
- pending.get(tab.id) === activeRuntime)
+ tabs.filter(tab =>
+ drawerShowsTab(tab, { activeRuntime, focusedIsTile, pending, rotated, selectionIsListed, sessionId })
)
)
@@ -643,24 +528,6 @@ export function adoptDraftPreviewTabs(storedSessionId: string): void {
}
}
-function setPendingRuntime(tabId: string, runtimeId: null | string): void {
- const current = $pendingRuntimeByTab.get()
-
- if ((current.get(tabId) ?? null) === runtimeId) {
- return
- }
-
- const next = new Map(current)
-
- if (runtimeId) {
- next.set(tabId, runtimeId)
- } else {
- next.delete(tabId)
- }
-
- $pendingRuntimeByTab.set(next)
-}
-
/** Drop the runtime notes of tabs no profile bucket holds any more (closed,
* pruned, or their profile dropped). */
function forgetGonePendingTabs(): void {
@@ -677,19 +544,6 @@ function forgetGonePendingTabs(): void {
}
}
-/** `runtimeId`'s session state was dropped before its stored id bound: its
- * tabs are plain ownerless tabs again, which the next draft's first send
- * adopts. Omitted = every runtime (all session states were dropped). */
-export function forgetPendingRuntimeTabs(runtimeId?: string): void {
- const pending = $pendingRuntimeByTab.get()
-
- if ([...pending.values()].some(runtime => runtimeId === undefined || runtime === runtimeId)) {
- $pendingRuntimeByTab.set(
- new Map(runtimeId === undefined ? [] : [...pending].filter(([, runtime]) => runtime !== runtimeId))
- )
- }
-}
-
/** `runtimeId`'s stored id was just bound (its session state went from no
* stored id to `storedSessionId`): the tabs it opened before then are that
* session's. Called by the session-state layer at that transition — never on
@@ -1085,6 +939,7 @@ export function openPreview(
const visible = bucketTabsFor(
current,
ownerIdentity({ profile, runtimeId: owner === null ? runtimeId : null, sessionId: owner }),
+ viewKey,
viewKey
)
@@ -1381,7 +1236,7 @@ function dockedTabs(tabs: readonly PreviewTab[]): PreviewTab[] {
* ones included), never another profile's pin, never a pin the agent did not
* name. */
export function closeAgentPreview(owner: PreviewOwner, candidates: string[]): void {
- const visible = bucketTabsFor($previewTabs.get(), ownerIdentity(owner), viewKey)
+ const visible = bucketTabsFor($previewTabs.get(), ownerIdentity(owner), viewKey, viewKey)
if (candidates.length > 0) {
if (!closeBrowserMatchingLiveUrlIn(visible, candidates)) {
diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.ts
index 5cd0b63bda8..1c5b76d3525 100644
--- a/apps/desktop/src/store/session-states.ts
+++ b/apps/desktop/src/store/session-states.ts
@@ -47,11 +47,11 @@ import { dialedGatewayModeFor } from './gateway'
import {
adoptPendingRuntimeTabs,
dropPreviewTabsForProfile,
- forgetPendingRuntimeTabs,
migratePreviewTabsForProfile,
rekeyPreviewTabsSession,
setPreviewScope
} from './preview'
+import { forgetPendingRuntimeTabs } from './preview-ownership'
import { dropPreviewArtifactsForProfile, migratePreviewArtifactsForProfile } from './preview-status'
import { $activeGatewayProfile, normalizeProfileKey } from './profile'
import { clearAllProviderWaits, clearSessionProviderWait } from './provider-wait'