Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
fix(open-knowledge): collapse hidden bottom dock so it never strands the editor (#2972)
Andrew Mikofalvy17b14817154d
diff --git a/.changeset/editor-bottom-dock-empty-gap.md b/.changeset/editor-bottom-dock-empty-gap.mdnew file mode 100644index 000000000..9daebc45f--- /dev/null+++ b/.changeset/editor-bottom-dock-empty-gap.md@@ -0,0 +1,5 @@+---+"@inkeep/open-knowledge": patch+---++Fix the editor's main content being squeezed into the top of the pane with a large empty band below it. The bottom sessions dock could be left holding space while hidden and showing nothing, and its drag handle is disabled while hidden, so there was no way to reclaim the space. The dock now collapses whenever it reports a size while hidden, so that state can no longer persist, and it records a diagnostic when it has to repair itself so a recurrence leaves a trace. Separately, the dock's height is re-clamped to at most half the window whenever the window is resized — previously a height chosen on one display kept its old ceiling after a move to a smaller one.diff --git a/packages/app/src/components/TerminalDock.dom.test.tsx b/packages/app/src/components/TerminalDock.dom.test.tsxindex 5f651abbf..eea595a3f 100644--- a/packages/app/src/components/TerminalDock.dom.test.tsx+++ b/packages/app/src/components/TerminalDock.dom.test.tsx@@ -163,9 +163,13 @@ vi.doMock('./TerminalGate', () => ({vi.doMock('@/lib/terminal-height-store', () => ({getInitialTerminalHeight: () => 240,writeTerminalHeight: () => {},+ // The dock's viewport re-clamp listener calls this on every window resize. The+ // clamp is not wrapped by that listener's try/catch, so omitting it here would+ // throw for any test that dispatches a resize event.+ clampTerminalHeight: (px: number) => px,}));-const { TerminalDock } = await import('./TerminalDock');+const { TerminalDock, MAX_STRANDED_REPORTS } = await import('./TerminalDock');const { TerminalSessionsHost } = await import('./TerminalSessionsHost');// After the vi.doMock block (a static import would load the real xterm).const { STAGE_PASTE_SETTLE_MS } = await import('./TerminalPanel');@@ -1398,3 +1402,110 @@ describe('TerminalDock extraction pins', () => {await waitFor(() => expect(document.activeElement).toBe(focusSink));});});++/**+ * The bottom panel must occupy zero height whenever the dock is not open. The+ * controlled effect asserts that only when `bottomOpen` changes, so a panel left+ * expanded by anything else — a library re-layout, or a collapse issued while the+ * group was unmeasurable and therefore discarded — strands the editor behind an+ * empty band with no dock chrome and no drag handle to recover it.+ *+ * These drive the panel's own `onResize`, which is the signal the guard hangs off+ * and the one rung where the illegal state can be produced on demand: the real+ * library will not un-collapse a collapsed panel, so a browser-level test cannot+ * reach this state and would pass with or without the guard.+ */+describe('TerminalDock hidden-dock invariant', () => {+ beforeEach(() => {+ localStorage.clear();+ terminalPanelProps = null;+ panelHandle.collapse.mockClear();+ panelHandle.resize.mockClear();+ panelHandle.expand.mockClear();+ sharedPanelRef.current = panelHandle;+ __resetLocalMenuActionBusForTests();+ });+ afterEach(() => {+ cleanup();+ __resetLocalMenuActionBusForTests();+ });++ function reportSize(inPixels: number, asPercentage: number) {+ act(() => {+ terminalPanelProps?.onResize?.({ asPercentage, inPixels });+ });+ }++ function strandedLogs(warn: ReturnType<typeof vi.spyOn>): string[] {+ return warn.mock.calls+ .map((call) => String(call[0]))+ .filter((line) => line.includes('ok-terminal-dock-stranded-while-hidden'));+ }++ test('a hidden dock that reports a non-zero height is snapped shut', () => {+ renderDock(false);+ panelHandle.collapse.mockClear();++ reportSize(588, 42.5);++ expect(panelHandle.collapse).toHaveBeenCalled();+ });++ test('an unmeasurable group (NaN percentage, zero pixels) is left alone', () => {+ // A zero-height group makes the library's percentage NaN, which compares+ // false against 0 — gating on that instead of pixels would fire the guard on+ // a panel that has no size at all.+ renderDock(false);+ panelHandle.collapse.mockClear();++ reportSize(0, Number.NaN);++ expect(panelHandle.collapse).not.toHaveBeenCalled();+ });++ test('an open dock keeps the height it reports', () => {+ renderDock(true);+ panelHandle.collapse.mockClear();++ reportSize(240, 30);++ expect(panelHandle.collapse).not.toHaveBeenCalled();+ });++ test('the repair is reported with the geometry needed to diagnose it', () => {+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});+ try {+ renderDock(false);+ reportSize(588, 42.5);++ const [line] = strandedLogs(warn);+ expect(line).toBeDefined();+ expect(JSON.parse(line as string)).toMatchObject({+ event: 'ok-terminal-dock-stranded-while-hidden',+ panelPx: 588,+ panelPct: 42.5,+ dockPosition: 'bottom',+ visible: false,+ });+ // Viewport geometry is the half that distinguishes a height clamped for the+ // current window from one carried over from a differently-sized display.+ const payload = JSON.parse(line as string);+ expect(typeof payload.innerHeight).toBe('number');+ expect(typeof payload.dockHeightPx).toBe('number');+ } finally {+ warn.mockRestore();+ }+ });++ test('the report is capped so a fight with another layout writer cannot flood the log', () => {+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});+ try {+ renderDock(false);+ for (let i = 0; i < MAX_STRANDED_REPORTS + 4; i++) reportSize(588, 42.5);++ expect(strandedLogs(warn)).toHaveLength(MAX_STRANDED_REPORTS);+ } finally {+ warn.mockRestore();+ }+ });+});diff --git a/packages/app/src/components/TerminalDock.tsx b/packages/app/src/components/TerminalDock.tsxindex 1c40d57b6..29352744a 100644--- a/packages/app/src/components/TerminalDock.tsx+++ b/packages/app/src/components/TerminalDock.tsx@@ -3,13 +3,25 @@ import { type ReactNode, useEffect, useRef, useState } from 'react';import { usePanelRef } from 'react-resizable-panels';import { ResizableHandle, ResizablePanel, ResizablePanelGroup } from '@/components/ui/resizable';import type { TerminalDockPosition } from '@/lib/terminal-dock-store';-import { getInitialTerminalHeight, writeTerminalHeight } from '@/lib/terminal-height-store';+import {+ clampTerminalHeight,+ getInitialTerminalHeight,+ writeTerminalHeight,+} from '@/lib/terminal-height-store';import { cn } from '@/lib/utils';import { TerminalRevealTab } from './TerminalRevealTab';import { useLiveXtermTheme } from './use-live-xterm-theme';const TERMINAL_PANEL_ID = 'terminal-dock-panel';+/**+ * Per-mount cap on the stranded-dock diagnostic. A healthy fire is one line — the+ * guard shuts the panel immediately — so a cap only bites when something fights+ * the collapse, exactly the case where an unbounded log would bury the signal it+ * exists to carry.+ */+export const MAX_STRANDED_REPORTS = 5;+interface TerminalDockProps {/** The editor chrome (header + area) the terminal docks beneath. */readonly children: ReactNode;@@ -101,6 +113,35 @@ export function TerminalDock({writeTimerRef.current = null;}, 100);}+ // Diagnostic for the illegal state the guard below repairs. The repair is+ // silent by construction — the panel snaps shut and the user sees nothing — so+ // without this line a recurrence leaves no trace and the next report is as+ // undiagnosable as the first. The renderer console is captured to a persisted+ // log on both hosts (the Electron main capture; `/api/client-logs` on web),+ // though only the web path reaches a submitted diagnostics bundle today. Every+ // field is a number or a fixed enum — no paths, no document content.+ const strandedReportsRef = useRef(0);+ function reportStrandedDock(panelPx: number, panelPct: number) {+ if (strandedReportsRef.current >= MAX_STRANDED_REPORTS) return;+ strandedReportsRef.current += 1;+ console.warn(+ JSON.stringify({+ event: 'ok-terminal-dock-stranded-while-hidden',+ panelPx: Math.round(panelPx),+ panelPct: Number.isFinite(panelPct) ? Math.round(panelPct * 10) / 10 : null,+ dockPosition,+ visible,+ // The height this shell would reopen at. Reading it against `innerHeight`+ // is what distinguishes a value clamped for the current viewport from one+ // carried over from a differently-sized display.+ dockHeightPx: heightPxRef.current,+ innerWidth: window.innerWidth,+ innerHeight: window.innerHeight,+ devicePixelRatio: window.devicePixelRatio,+ }),+ );+ }+// The drag-end listener is added to `window` on pointerdown and normally removes// itself on pointerup. If the shell unmounts mid-drag the closure would leak —// track it so unmount can detach it.@@ -122,13 +163,44 @@ export function TerminalDock({useEffect(() => {const panel = panelRef.current;if (panel == null) return;- if (bottomOpen) {- panel.resize(`${heightPxRef.current}px`);- } else {- panel.collapse();+ try {+ if (bottomOpen) {+ panel.resize(`${heightPxRef.current}px`);+ } else {+ panel.collapse();+ }+ } catch {+ // The imperative panel handles throw once their group has unregistered+ // (same reason EditorArea's assertRightRailLayout wraps its calls). A throw+ // means the panel never resized, so no onResize fires and the invariant+ // guard cannot see it: recovery is the next `bottomOpen` transition+ // re-running this effect, or any later change to the panel's own box.}}, [bottomOpen, panelRef]);+ // The persisted height is viewport-relative: `readTerminalHeight` caps it at+ // 50vh, but only at read time, and this shell snapshots it once at mount. A+ // window that changes size afterwards — moving to another display being the+ // common case — otherwise keeps a ceiling computed for a viewport that no+ // longer exists, letting the dock occupy more than half the editor. Re-clamp+ // against the live viewport, and re-apply while open so an open dock shrinks+ // with the window rather than waiting for the next remount.+ useEffect(() => {+ const reclampToViewport = () => {+ const next = clampTerminalHeight(heightPxRef.current);+ if (next === heightPxRef.current) return;+ heightPxRef.current = next;+ if (!bottomOpen) return;+ try {+ panelRef.current?.resize(`${next}px`);+ } catch {+ // Panel unregistered mid-flight — nothing to re-clamp against.+ }+ };+ window.addEventListener('resize', reclampToViewport);+ return () => window.removeEventListener('resize', reclampToViewport);+ }, [bottomOpen, panelRef]);+return (<ResizablePanelGrouporientation="vertical"@@ -197,6 +269,30 @@ export function TerminalDock({onResize={(size) => {const collapsed = size.asPercentage === 0;setIsCollapsed(collapsed);+ // Invariant: the bottom panel occupies ZERO height whenever the dock is+ // not open (hidden, or right-docked). The `bottomOpen` effect asserts+ // that only on a TRANSITION, so any path leaving the panel expanded+ // without flipping `bottomOpen` — a library re-layout, or a collapse+ // issued while the group was unmeasurable and therefore discarded —+ // strands the editor behind an empty band with no dock chrome and+ // nothing left to re-assert it. This is the panel's own resize signal,+ // so it catches that state whatever produced it. The handle is disabled+ // while hidden, so an expanded-hidden panel is never a user width worth+ // preserving.+ //+ // Gate on pixels rather than `collapsed`: an unmeasurable group makes+ // `asPercentage` NaN, which compares false against 0 and would read as+ // "expanded", firing this guard spuriously on a panel that has no size+ // at all.+ if (!bottomOpen && size.inPixels > 0 && !isDraggingRef.current) {+ reportStrandedDock(size.inPixels, size.asPercentage);+ try {+ panelRef.current?.collapse();+ } catch {+ // Panel unregistered mid-flight — the next resize re-asserts.+ }+ return;+ }// Persist + reflect to controlled visibility only on a user drag;// imperative replays from the `visible` effect also fire onResize and must// not overwrite the persisted value or loop onVisibleChange.diff --git a/packages/app/src/lib/terminal-height-store.test.ts b/packages/app/src/lib/terminal-height-store.test.tsindex a6dfccddb..5b4c99cca 100644--- a/packages/app/src/lib/terminal-height-store.test.ts+++ b/packages/app/src/lib/terminal-height-store.test.ts@@ -1,5 +1,6 @@import { describe, expect, test } from 'vitest';import {+ clampTerminalHeight,DEFAULT_TERMINAL_HEIGHT,type HeightStorage,MIN_TERMINAL_HEIGHT,@@ -124,3 +125,28 @@ describe('writeTerminalHeight', () => {expect(() => writeTerminalHeight(240, throwing, TALL)).not.toThrow();});});++/**+ * The 50vh ceiling is viewport-relative, but `readTerminalHeight` applies it only+ * at read time and callers snapshot the result at mount. Moving the window to a+ * shorter display therefore leaves a height that outlives the viewport it was+ * sized for, so the live value needs re-clamping independently of a read.+ */+describe('clampTerminalHeight', () => {+ test('caps a height carried over from a taller viewport', () => {+ expect(clampTerminalHeight(588, 1000)).toBe(500);+ });++ test('leaves a height that already fits', () => {+ expect(clampTerminalHeight(240, TALL)).toBe(240);+ });++ test('raises a height below the floor', () => {+ expect(clampTerminalHeight(10, TALL)).toBe(MIN_TERMINAL_HEIGHT);+ });++ test('keeps the floor reachable on a viewport too short for it', () => {+ // 50vh of 100px is below MIN; the clamp window must not invert.+ expect(clampTerminalHeight(400, 100)).toBe(MIN_TERMINAL_HEIGHT);+ });+});diff --git a/packages/app/src/lib/terminal-height-store.ts b/packages/app/src/lib/terminal-height-store.tsindex 632421cba..2c398689a 100644--- a/packages/app/src/lib/terminal-height-store.ts+++ b/packages/app/src/lib/terminal-height-store.ts@@ -47,6 +47,22 @@ function currentViewportHeight(): number {return window.innerHeight;}+/**+ * Re-clamp a live height against the CURRENT viewport's 50vh ceiling.+ *+ * `readTerminalHeight` applies that ceiling only at read time, and callers+ * snapshot the result once at mount. A window that changes size afterwards —+ * moving to another display is the common case — therefore carries a ceiling+ * computed for a viewport that no longer exists, and the dock can occupy more+ * than half the editor.+ *+ * This registers no listener of its own: a caller holding a live height is+ * responsible for subscribing to `window` resize and re-clamping there.+ */+export function clampTerminalHeight(px: number, viewportHeight?: number): number {+ return clamp(px, viewportHeight ?? currentViewportHeight());+}+export function readTerminalHeight(storage?: HeightStorage, viewportHeight?: number): number {try {const s = storage ?? localStorage;