diff --git a/src/client/TerminalView.tsx b/src/client/TerminalView.tsx index 10d6e666..dee9aff9 100644 --- a/src/client/TerminalView.tsx +++ b/src/client/TerminalView.tsx @@ -50,7 +50,7 @@ import type { SidebarTab } from './state.ts' import { t } from './locales.ts' import { isDarkScheme, subscribeColorScheme } from './theme.ts' import { - bottomTerminalContentId, bottomTerminalKey, nextTerminalMeta, terminalRunOf, webTerminals, + bottomTerminalContentId, bottomTerminalKey, nextTerminalMeta, terminalInstanceOf, terminalRunOf, webTerminals, type HostTerminalEnvironment, type HostTerminalView, type HostTerminalViewState, } from './terminal-client.ts' import css from './terminal.module.css' @@ -74,11 +74,12 @@ export function TerminalBottomView({ ctx, scope, tab, visible }: TerminalBottomP // it keeps the tray row disabled and renders the explanation below. const service = useMemo(() => webTerminals(ctx), [ctx]) const run = terminalRunOf(tab) + const instance = terminalInstanceOf(tab) // `view()` allocates the terminal eagerly and memoizes one model per // (session, key) on the host side, so this memo must be the ONLY creation // path: a repeat `view()` for the same key is free, a new key is a new PTY. const model = useMemo( - () => service?.view(sessionId, bottomTerminalKey(run), bottomTerminalContentId(run)), + () => service?.view(sessionId, bottomTerminalKey(run, instance), bottomTerminalContentId(run, instance)), [service, sessionId, run], ) const state = useSyncExternalStore( diff --git a/src/client/builtins/tabs.tsx b/src/client/builtins/tabs.tsx index 179c3edc..b83804ae 100644 --- a/src/client/builtins/tabs.tsx +++ b/src/client/builtins/tabs.tsx @@ -32,7 +32,7 @@ import { DiffTab } from '../DiffTab.tsx' import { SubagentView } from '../SubagentView.tsx' import { consumeSidechatSeed, SideChatView, sidechatThreadIdOf } from '../SideChatView.tsx' import { LazyTerminalBottom } from '../terminal-lazy.tsx' -import { closeBottomTerminal, webTerminals } from '../terminal-client.ts' +import { INSTANCE_FIELD, closeBottomTerminal, webTerminals } from '../terminal-client.ts' import { api } from '../api.ts' import type { Context } from '../../context-types.ts' import type { TabDescriptor } from '../service.ts' @@ -255,8 +255,20 @@ export function builtinTabs(ctx: Context): readonly TabDescriptor[] { // tab: the terminal is a secondary tool, but a visible menu row. order: 40, bottomOnly: true, - // One terminal per session workbench: reopening focuses the live tab. - single: true, + // Multiple bottom terminals per session workbench (#774 follow-up). + // Dropping the old `single: true` is the whole point: each open now + // MINTS its own tab (unique id + `meta.terminalInstance`), so the + + // menu stacks as many live shells as the host grants slots for, and + // two tabs never share one host process (terminal-client.ts keys the + // host binding by instance+run). + createTab: () => ({ + tab: { + id: `terminal-bottom:${crypto.randomUUID()}`, + type: 'terminal-bottom', + title: t('terminal'), + meta: { [INSTANCE_FIELD]: crypto.randomUUID() }, + }, + }), // The service is probed, never injected (it is absent on deployments // without the host terminal controller): a missing service shows a // disabled menu row instead of a terminal that cannot start. diff --git a/src/client/terminal-client.ts b/src/client/terminal-client.ts index 8a52b882..1d6cab1b 100644 --- a/src/client/terminal-client.ts +++ b/src/client/terminal-client.ts @@ -159,6 +159,9 @@ export function webTerminals(ctx: Context): WebTerminalsFace | undefined { /** Namespace for the plugin's own occurrence key and content identity. */ const TERMINAL_NS = 'dsh-better-sidebar:terminal-bottom' +/** The persisted `tab.meta` field identifying ONE terminal tab instance. */ +export const INSTANCE_FIELD = 'terminalInstance' + /** The persisted `tab.meta` field holding the content-identity generation. */ const RUN_FIELD = 'terminalRun' @@ -173,8 +176,9 @@ const RUN_FIELD = 'terminalRun' * `missingTerminal` forever. * @param run - the content-identity generation (0 for a tab's first terminal). */ -export function bottomTerminalContentId(run: number): string { - return run === 0 ? TERMINAL_NS : `${TERMINAL_NS}#${run}` +export function bottomTerminalContentId(run: number, instance?: string): string { + const base = instance === undefined || instance === '' ? TERMINAL_NS : `${TERMINAL_NS}:${instance}` + return run === 0 ? base : `${base}#${run}` } /** @@ -184,8 +188,17 @@ export function bottomTerminalContentId(run: number): string { * with a host sidebar occurrence key (those are layout tab ids such as `tab1`). * @param run - the content-identity generation. */ -export function bottomTerminalKey(run: number): string { - return bottomTerminalContentId(run) +export function bottomTerminalKey(run: number, instance?: string): string { + return bottomTerminalContentId(run, instance) +} + +/** The per-tab instance id a tab carries (absent/legacy tabs read as `undefined`). */ +export function terminalInstanceOf(tab: { meta?: unknown }): string | undefined { + const meta = tab.meta !== null && typeof tab.meta === 'object' && !Array.isArray(tab.meta) + ? tab.meta as Record + : {} + const instance = meta[INSTANCE_FIELD] + return typeof instance === 'string' && instance !== '' ? instance : undefined } /** The content-identity generation a tab carries (absent/corrupt reads as 0). */ @@ -215,5 +228,6 @@ export function closeBottomTerminal(ctx: Context, tab: { meta?: unknown }, sessi const service = webTerminals(ctx) if (service === undefined) return const run = terminalRunOf(tab) - service.close(sessionId, bottomTerminalKey(run), bottomTerminalContentId(run)) + const instance = terminalInstanceOf(tab) + service.close(sessionId, bottomTerminalKey(run, instance), bottomTerminalContentId(run, instance)) } diff --git a/tests/terminal-tab.spec.tsx b/tests/terminal-tab.spec.tsx index 8d27b0aa..e0428ee3 100644 --- a/tests/terminal-tab.spec.tsx +++ b/tests/terminal-tab.spec.tsx @@ -267,15 +267,19 @@ afterEach(() => { }) describe('terminal-bottom: carrier surface', () => { - it('registers as a bottom-only, single-instance, service-gated built-in tab', () => { + it('registers as a bottom-only, multi-instance, service-gated built-in tab', () => { const tabs = builtinTabs(ctxWith({ webTerminals: new FakeTerminals() })) const terminal = tabs.find(tab => tab.id === 'terminal-bottom') expect(terminal).toBeDefined() expect(terminal?.bottomOnly).toBe(true) - expect(terminal?.single).toBe(true) + expect(terminal?.single).not.toBe(true) expect(terminal?.hidden).not.toBe(true) - // No createTab: the id safety net keeps ONE bottom terminal per session. - expect(terminal?.createTab).toBeUndefined() + // createTab mints a FRESH tab per open: the old id safety net used to + // collapse them into one, which is exactly what this follow-up removes. + expect(typeof terminal?.createTab).toBe('function') + const first = terminal?.createTab?.({} as never) + const second = terminal?.createTab?.({} as never) + expect(first?.tab.id).not.toBe(second?.tab.id) }) it('the + menu row is disabled when the host provides no terminal service', () => {