Merge origin/main (v0.24.1, DSH 0.2.0-rc.1) into fix/native-tab-state-retention

Two conflicts, both in the native tab adapter's per-session work — main's
`5c95002` moved the expansion set into the plugin store's session state while
this branch moved the RECORD identity to `sessionId::tabId`:

- `src/client/native/tab-adapter.tsx`: keep main's store-backed expansion as
  the authority (`attachStore` / `syncExpanded` / `getSessionStates().expanded`)
  and this branch's seat-scoped identity (`views` keyed `sessionId::tabId`,
  `get/has/update/drop/versionOf/retain` take the seat session). `toggleExpanded`
  keeps main's active-`reduce` / background-`reduceFor` split, but locates the
  record by its seat and writes the SCOPE session's set — the set every view of
  that session mirrors.
- `tests/native-surface.spec.ts`: main's five expansion cases adapted to the
  session-scoped API (seat session on `ensure`, two-arg `get`/`drop`, seat on
  `toggleExpanded`); the branch's cases now bind a store before toggling, since
  expansion no longer lives in the record itself.

The `files` takeover guard and its registration invariants come from
main; this merge drops the branch's duplicate.
This commit is contained in:
ymac committed 2026-10-01 16:37:35 +08:00
commit d1c8208d56
109 files changed
+14220 -3020

No files matched your search

+9
View File
@@ -208,6 +208,15 @@ export interface NativeSurfaceDeps {
*/
export function registerNativeSurface(deps: NativeSurfaceDeps): () => void {
const { ctx, store, service, records, reportFailure } = deps
// Bind the store BEFORE any body can render: the per-session state is the
// authority for the explorer's expansion set, so the native surface and the
// workbench share one set per session and a closed tab cannot drop it.
// (Restored after the v0.22.1 merge: main's branch rewrote this function
// without the call, and taking that file wholesale silently disabled every
// folder toggle — the records then have no store to read from or write to,
// so `toggleExpanded` returns without touching anything. Only the real-host
// lane could see it; the jsdom specs bind the records themselves.)
records.attachStore(store)
// Wait for the tab-type REGISTRY (a service), not for the slot declaration:
// the native seat declares `sidebar.right.pane.tab` BEFORE it provides
// `sidebarRightTabs`, so a declaration-triggered registration reads the
+16 -11
View File
@@ -26,8 +26,8 @@ import type { NativeTabRecords } from './tab-adapter.tsx'
/** One open the surface could not place yet. */
type Pending =
| { kind: 'tab'; sessionId: string; tabKind: string; params: NativeTabParams; revealIfOpened: boolean }
| { kind: 'resource'; sessionId: string; address: string; line: number | undefined; revealIfOpened: boolean }
| { kind: 'tab'; sessionId: string; tabKind: string; params: NativeTabParams; revealIfOpened: boolean; preferNewPane: boolean }
| { kind: 'resource'; sessionId: string; address: string; line: number | undefined; revealIfOpened: boolean; preferNewPane: boolean }
/**
* The observation "which session's seat is on screen": DSH 0.1.7 publishes it
@@ -42,14 +42,14 @@ export interface MountedSessions {
/** The controller face this module uses (a structural slice of `ISidebarRight`). */
interface NativeController {
openTab(kind: string, options?: { params?: unknown; revealIfOpened?: boolean }): void
openResource(address: string, options?: { params?: unknown; revealIfOpened?: boolean }): void
openTab(kind: string, options?: { params?: unknown; revealIfOpened?: boolean; preferNewPane?: boolean }): void
openResource(address: string, options?: { params?: unknown; revealIfOpened?: boolean; preferNewPane?: boolean }): void
close(tabId: string): void
/** The mounted-seat observation (0.1.7 `ISidebarRight.mounted`). */
mounted?: MountedSessions
/** Not part of `ISidebarRight`: the concrete controller's per-session writes. */
openTabIn?(sessionId: string, kind: string, options?: { params?: unknown; revealIfOpened?: boolean }): void
openResourceIn?(sessionId: string, address: string, options?: { params?: unknown; revealIfOpened?: boolean }): void
openTabIn?(sessionId: string, kind: string, options?: { params?: unknown; revealIfOpened?: boolean; preferNewPane?: boolean }): void
openResourceIn?(sessionId: string, address: string, options?: { params?: unknown; revealIfOpened?: boolean; preferNewPane?: boolean }): void
closeIn?(sessionId: string, tabId: string): void
}
@@ -134,7 +134,11 @@ export function createNativeSurface(ctx: Context, records: NativeTabRecords): Na
if (api === undefined) return false
const onScreen = mountedSessionId(ctx) === entry.sessionId
if (entry.kind === 'tab') {
const options = { params: entry.params, revealIfOpened: entry.revealIfOpened }
const options = {
params: entry.params,
revealIfOpened: entry.revealIfOpened,
...(entry.preferNewPane ? { preferNewPane: true } : {}),
}
if (onScreen) {
api.openTab(entry.tabKind, options)
return true
@@ -148,6 +152,7 @@ export function createNativeSurface(ctx: Context, records: NativeTabRecords): Na
const options = {
...(entry.line === undefined ? {} : { params: { line: entry.line } }),
revealIfOpened: entry.revealIfOpened,
...(entry.preferNewPane ? { preferNewPane: true } : {}),
}
if (onScreen) {
api.openResource(entry.address, options)
@@ -235,11 +240,11 @@ export function createNativeSurface(ctx: Context, records: NativeTabRecords): Na
const unsubscribeList = ctx.sessions.list.subscribe(onListChange)
evictGoneSessions()
return {
openTab({ sessionId, kind, params, revealIfOpened }) {
enqueue({ kind: 'tab', sessionId, tabKind: kind, params, revealIfOpened })
openTab({ sessionId, kind, params, revealIfOpened, preferNewPane }) {
enqueue({ kind: 'tab', sessionId, tabKind: kind, params, revealIfOpened, preferNewPane: preferNewPane === true })
},
openResource({ sessionId, address, line, revealIfOpened }) {
enqueue({ kind: 'resource', sessionId, address, line, revealIfOpened })
openResource({ sessionId, address, line, revealIfOpened, preferNewPane }) {
enqueue({ kind: 'resource', sessionId, address, line, revealIfOpened, preferNewPane: preferNewPane === true })
},
fileAddress(sessionId, cwd, path) {
return fileAddressFor(sessionId, cwd, path)
+81 -10
View File
@@ -14,7 +14,10 @@
* record + `navigation.params` and kept live across navigations, so a
* component that retitles itself (`updateTab`) or rewrites its path (the
* editor's in-place file switch) keeps working;
* - the explorer's per-tab expansion/reveal sets;
* - the explorer's expansion/reveal sets: EXPANSION lives in the per-session
* state the workbench already uses (one set per session, shared by every
* native tab of that session — a closed tab cannot drop it), while the
* transient "show in folder" reveal stays per tab;
* - the per-kind instance counter behind titles like "Terminal 2".
*
* Nothing here is a singleton: the registry is created once per client
@@ -28,6 +31,7 @@ import { RenderBoundary } from '../RenderBoundary.tsx'
import { OrphanedTab } from '../OrphanedTab.tsx'
import { referenceInChat } from '../reference-in-chat.ts'
import type { BetterSidebarService } from '../service.ts'
import { toggleExpanded } from '../state.ts'
import type { SidebarStore, SidebarTab, TabType } from '../state.ts'
import css from '../sidebar.module.css'
@@ -77,7 +81,15 @@ export interface NativeTabInfo {
interface View {
tab: SidebarTab
scope: SessionScope
/**
* A PROJECTION of the session's expansion set (never an authoritative copy):
* refreshed from the store on every `ensure` and on every store change, so
* every native tab of one session shows the same set and reopening a tab
* cannot reset it. Cached only for referential stability — a fresh array on
* each render would re-run the tree's load effect for nothing.
*/
expanded: string[]
/** The transient "show in folder" highlight: per tab by design. */
revealed: string[]
/** Bumped on every mutation; the components subscribe to it. */
version: number
@@ -137,7 +149,17 @@ export interface NativeTabRecords {
* state must survive.
*/
drop(sessionId: string, id: string): void
/** Toggle one directory in a record's expansion set. */
/**
* Bind the plugin store — the per-session state is the AUTHORITY for every
* view's expansion set. Idempotent; the first call also subscribes to store
* changes so a toggle made anywhere (the workbench, another native tab, a
* persisted restore) refreshes every native view.
*/
attachStore(store: SidebarStore): void
/**
* Toggle one directory in the SCOPE session's expansion set (the set every
* view of that session mirrors); `sessionId` names the record's SEAT.
*/
toggleExpanded(sessionId: string, id: string, path: string): void
/**
* Keep only records of these sessions; the rest are forgotten. A session the
@@ -153,6 +175,11 @@ export interface NativeTabRecords {
subscribe(listener: () => void): () => void
}
/** Whether two expansion sets hold the same paths in the same order. */
function samePaths(left: readonly string[], right: readonly string[]): boolean {
return left.length === right.length && left.every((path, index) => path === right[index])
}
/** Create the record registry for one client activation. */
export function createNativeTabRecords(): NativeTabRecords {
const views = new Map<string, View>()
@@ -163,7 +190,39 @@ export function createNativeTabRecords(): NativeTabRecords {
views.set(key, { ...view, version: view.version + 1 })
notify()
}
/** The session-state authority (bound through {@link NativeTabRecords.attachStore}). */
let store: SidebarStore | undefined
/** The session's expansion set (absent session = nothing expanded yet). */
const expandedOf = (sessionId: string): string[] =>
store?.getSessionStates().get(sessionId)?.expanded ?? []
/**
* Re-read every view's expansion projection from the session state. Views
* that changed get a version bump (which is what re-renders their bodies);
* identical sets keep their array identity so the tree's `expanded` prop —
* and therefore its load effect — stays stable.
*/
const syncExpanded = (): void => {
if (store === undefined) return
// One map copy for the whole pass (getSessionStates copies by contract).
const states = store.getSessionStates()
let changed = false
for (const [id, entry] of views) {
const next = states.get(entry.scope.sessionId)?.expanded ?? []
if (samePaths(entry.expanded, next)) continue
views.set(id, { ...entry, expanded: next, version: entry.version + 1 })
changed = true
}
if (changed) notify()
}
return {
attachStore(next) {
if (store === next) return
store = next
// One listener for the whole registry: the store lives as long as this
// activation, so the returned disposer is not kept.
next.subscribe(syncExpanded)
syncExpanded()
},
ensure({ sessionId, id, kind, title, params, scope, mint }) {
const key = viewKey(sessionId, id)
const existing = views.get(key)
@@ -180,7 +239,7 @@ export function createNativeTabRecords(): NativeTabRecords {
...(meta === undefined ? {} : { meta }),
},
scope,
expanded: [],
expanded: expandedOf(scope.sessionId),
revealed: [],
version: 0,
}
@@ -199,6 +258,13 @@ export function createNativeTabRecords(): NativeTabRecords {
: {}
patch.meta = { ...meta, url: params.url }
}
// The expansion set always mirrors the CURRENT session state (a record
// reused for another session must not keep the previous one's set).
const expanded = expandedOf(scope.sessionId)
if (existing.scope.sessionId !== scope.sessionId || !samePaths(existing.expanded, expanded)) {
views.set(key, { ...existing, scope, expanded, tab: { ...existing.tab, ...patch } })
return views.get(key)!
}
if (existing.scope.cwd !== scope.cwd) {
views.set(key, { ...existing, scope, tab: { ...existing.tab, ...patch } })
return views.get(key)!
@@ -220,13 +286,18 @@ export function createNativeTabRecords(): NativeTabRecords {
if (views.delete(viewKey(sessionId, id))) notify()
},
toggleExpanded(sessionId, id, path) {
const key = viewKey(sessionId, id)
const entry = views.get(key)
if (entry === undefined) return
const expanded = entry.expanded.includes(path)
? entry.expanded.filter(candidate => candidate !== path)
: [...entry.expanded, path]
put(key, { ...entry, expanded })
const entry = views.get(viewKey(sessionId, id))
if (entry === undefined || store === undefined) return
const scopeSessionId = entry.scope.sessionId
// The ACTIVE session goes through `reduce` (it notifies, so the native
// views refresh through the subscription); a background session goes
// through `reduceFor`, which deliberately stays silent — refresh here.
if (store.getSnapshot().sessionId === scopeSessionId) {
store.reduce(state => toggleExpanded(state, path))
return
}
store.reduceFor(scopeSessionId, state => toggleExpanded(state, path))
syncExpanded()
},
retain(sessions) {
let dropped = false