@@ -27,7 +27,7 @@ import type { Theme } from "./theme.ts";
2727 import { createUiAgentController } from "./ui/agent.ts";
2828 import { createCommandController } from "./ui/commands.ts";
2929 import {
30- buildConversationLogNodes,
30+ buildConversationLayoutSnapshot,
3131 CONVERSATION_GAP,
3232 resetConversationRenderCache,
3333 } from "./ui/conversation.ts";
@@ -74,8 +74,8 @@ const TERMINAL_TITLE_FRAMES = [
7474 "[o=---]",
7575 ] as const;
7676
77-/** Maximum number of committed messages rendered before older history is chunked. */
78-const CONVERSATION_CHUNK_MESSAGES = 50;
77+/** Overscan applied above and below the viewport for virtualized log rendering. */
78+const CONVERSATION_OVERSCAN_ROWS = 6;
7979
8080 /** Minimum delay between coalesced streaming renders. */
8181 const STREAM_RENDER_MIN_INTERVAL_MS = 33;
@@ -115,9 +115,6 @@ let scrollOffset = 0;
115115 /** Whether the log auto-scrolls to the bottom. */
116116 let stickToBottom = true;
117117
118-/** First visible committed message when older history is chunked. */
119-let visibleConversationStart = 0;
120-
121118 /** Current text in the input area. */
122119 let inputValue = "";
123120
@@ -177,7 +174,6 @@ export function isQuitKey(key: string, input: string): boolean {
177174 export function resetUiState(): void {
178175 scrollOffset = 0;
179176 stickToBottom = true;
180- visibleConversationStart = 0;
181177 inputValue = "";
182178 inputReadOnly = false;
183179 inputFocused = true;
@@ -528,62 +524,151 @@ function renderDivider(state: AppState, width: number): Node {
528524 // Conversation log
529525 // ---------------------------------------------------------------------------
530526
531-function getLatestConversationChunkStart(messageCount: number): number {
532- return Math.max(0, messageCount - CONVERSATION_CHUNK_MESSAGES);
527+function measureConversationSliceHeight(
528+ prefixHeights: readonly number[],
529+ startIndex: number,
530+ endIndex: number,
531+): number {
532+ const count = endIndex - startIndex;
533+ if (count <= 0) {
534+ return 0;
535+ }
536+ return (
537+ prefixHeights[endIndex]! -
538+ prefixHeights[startIndex]! +
539+ CONVERSATION_GAP * Math.max(0, count - 1)
540+ );
541+}
542+
543+function getConversationItemTop(
544+ prefixHeights: readonly number[],
545+ index: number,
546+): number {
547+ return prefixHeights[index]! + CONVERSATION_GAP * index;
533548 }
534549
535-function getVisibleConversationStart(messageCount: number): number {
536- if (stickToBottom) {
537- return getLatestConversationChunkStart(messageCount);
550+function findConversationStartIndex(
551+ itemHeights: readonly { height: number }[],
552+ prefixHeights: readonly number[],
553+ offset: number,
554+): number {
555+ let low = 0;
556+ let high = itemHeights.length;
557+
558+ while (low < high) {
559+ const mid = Math.floor((low + high) / 2);
560+ const bottom =
561+ getConversationItemTop(prefixHeights, mid) + itemHeights[mid]!.height;
562+ if (bottom > offset) {
563+ high = mid;
564+ } else {
565+ low = mid + 1;
566+ }
538567 }
539568
540- visibleConversationStart = Math.min(
541- visibleConversationStart,
542- getLatestConversationChunkStart(messageCount),
543- );
544- return visibleConversationStart;
569+ return Math.min(low, Math.max(0, itemHeights.length - 1));
570+}
571+
572+function findConversationEndIndex(
573+ items: readonly { height: number }[],
574+ prefixHeights: readonly number[],
575+ offset: number,
576+): number {
577+ let low = 0;
578+ let high = items.length;
579+
580+ while (low < high) {
581+ const mid = Math.floor((low + high) / 2);
582+ if (getConversationItemTop(prefixHeights, mid) < offset) {
583+ low = mid + 1;
584+ } else {
585+ high = mid;
586+ }
587+ }
588+
589+ return low;
590+}
591+
592+function renderConversationSpacer(height: number): Node | null {
593+ const rows = Math.max(0, Math.floor(height));
594+ if (rows === 0) {
595+ return null;
596+ }
597+
598+ const paddingY = Math.floor(rows / 2);
599+ if (rows % 2 === 0) {
600+ return VStack({ padding: { y: paddingY } }, []);
601+ }
602+
603+ return VStack({ padding: { y: paddingY } }, [Text("")]);
545604 }
546605
547606 /** Build the full conversation log as an array of nodes. */
548607 export function buildConversationLog(
549608 state: AppState,
550609 width = Number.POSITIVE_INFINITY,
610+ viewportHeight = Number.POSITIVE_INFINITY,
551611 ): Node[] {
552- return buildConversationLogNodes(
612+ const layout = buildConversationLayoutSnapshot(
553613 state,
554614 agentController.getStreamingConversationState(),
555- getVisibleConversationStart(state.messages.length),
556615 width,
557616 );
558-}
617+ const { items, prefixHeights } = layout;
618+ if (!Number.isFinite(viewportHeight) || items.length <= 1) {
619+ return items.map((item) => item.node);
620+ }
559621
560-function measureConversationHeight(
561- state: AppState,
562- width: number,
563- startIndex: number,
564-): number {
565- return measureContentHeight(
566- VStack(
567- { gap: CONVERSATION_GAP },
568- buildConversationLogNodes(
569- state,
570- agentController.getStreamingConversationState(),
571- startIndex,
572- width,
573- ),
574- ),
575- { width: Math.max(1, width) },
622+ const totalHeight = measureConversationSliceHeight(
623+ prefixHeights,
624+ 0,
625+ items.length,
576626 );
577-}
627+ const visibleHeight = Math.max(1, Math.floor(viewportHeight));
628+ const maxOffset = Math.max(0, totalHeight - visibleHeight);
629+ const effectiveScrollOffset = stickToBottom
630+ ? maxOffset
631+ : Math.max(0, Math.min(scrollOffset, maxOffset));
632+ const paddedTop = Math.max(
633+ 0,
634+ effectiveScrollOffset - CONVERSATION_OVERSCAN_ROWS,
635+ );
636+ const paddedBottom = Math.min(
637+ totalHeight,
638+ effectiveScrollOffset + visibleHeight + CONVERSATION_OVERSCAN_ROWS,
639+ );
640+
641+ let startIndex = findConversationStartIndex(items, prefixHeights, paddedTop);
642+ let endIndex = findConversationEndIndex(items, prefixHeights, paddedBottom);
643+ if (endIndex <= startIndex) {
644+ endIndex = Math.min(items.length, startIndex + 1);
645+ }
646+ startIndex = Math.min(startIndex, Math.max(0, endIndex - 1));
578647
579-function prependConversationChunk(state: AppState, width: number): void {
580- const currentStart = visibleConversationStart;
581- const nextStart = Math.max(0, currentStart - CONVERSATION_CHUNK_MESSAGES);
582- const currentHeight = measureConversationHeight(state, width, currentStart);
583- const nextHeight = measureConversationHeight(state, width, nextStart);
648+ const topSpacerHeight = measureConversationSliceHeight(
649+ prefixHeights,
650+ 0,
651+ startIndex,
652+ );
653+ const bottomSpacerHeight = measureConversationSliceHeight(
654+ prefixHeights,
655+ endIndex,
656+ items.length,
657+ );
584658
585- visibleConversationStart = nextStart;
586- scrollOffset += Math.max(0, nextHeight - currentHeight);
659+ const nodes: Node[] = [];
660+ const topSpacer = renderConversationSpacer(topSpacerHeight);
661+ if (topSpacer) {
662+ nodes.push(topSpacer);
663+ }
664+ for (let index = startIndex; index < endIndex; index++) {
665+ nodes.push(items[index]!.node);
666+ }
667+ const bottomSpacer = renderConversationSpacer(bottomSpacerHeight);
668+ if (bottomSpacer) {
669+ nodes.push(bottomSpacer);
670+ }
671+ return nodes;
587672 }
588673
589674 // ---------------------------------------------------------------------------
@@ -930,7 +1015,30 @@ export function suspendToBackground(
9301015 // Main
9311016 // ---------------------------------------------------------------------------
9321017
933-function renderConversationLog(state: AppState, width: number): Node {
1018+function getConversationViewportHeight(
1019+ cols: number,
1020+ rows: number,
1021+ divider: Node,
1022+ input: Node,
1023+ statusBar: Node,
1024+): number {
1025+ if (!Number.isFinite(rows)) {
1026+ return Number.POSITIVE_INFINITY;
1027+ }
1028+
1029+ const width = Math.max(1, Math.floor(cols));
1030+ const reservedHeight =
1031+ measureContentHeight(divider, { width }) +
1032+ measureContentHeight(input, { width }) +
1033+ measureContentHeight(statusBar, { width });
1034+ return Math.max(1, Math.floor(rows) - reservedHeight);
1035+}
1036+
1037+function renderConversationLog(
1038+ state: AppState,
1039+ width: number,
1040+ viewportHeight: number,
1041+): Node {
9341042 return VStack(
9351043 {
9361044 flex: 1,
@@ -939,23 +1047,11 @@ function renderConversationLog(state: AppState, width: number): Node {
9391047 justifyContent: state.messages.length === 0 ? "center" : undefined,
9401048 scrollOffset: stickToBottom ? Infinity : scrollOffset,
9411049 onScroll: (offset, maxOffset) => {
942- const wasStickToBottom = stickToBottom;
9431050 scrollOffset = offset;
944-
945- if (wasStickToBottom && offset < maxOffset) {
946- visibleConversationStart = getLatestConversationChunkStart(
947- state.messages.length,
948- );
949- }
950-
9511051 stickToBottom = offset >= maxOffset;
952-
953- if (!stickToBottom && offset === 0 && visibleConversationStart > 0) {
954- prependConversationChunk(state, width);
955- }
9561052 },
9571053 },
958- buildConversationLog(state, width),
1054+ buildConversationLog(state, width, viewportHeight),
9591055 );
9601056 }
9611057
@@ -968,6 +1064,8 @@ function renderConversationLog(state: AppState, width: number): Node {
9681064 * @param state - Application state.
9691065 * @param cols - Current terminal width in columns.
9701066 * @param inputController - Stable callbacks for the controlled TextInput.
1067+ * @param onSuspend - Optional suspend handler for Ctrl+Z.
1068+ * @param rows - Current terminal height in rows.
9711069 * @returns The base layout node.
9721070 */
9731071 export function renderBaseLayout(
@@ -975,6 +1073,7 @@ export function renderBaseLayout(
9751073 cols: number,
9761074 inputController: InputController,
9771075 onSuspend?: () => void,
1076+ rows = Number.POSITIVE_INFINITY,
9781077 ): Node {
9791078 titleState = state;
9801079 titleViewportActive = true;
@@ -982,6 +1081,17 @@ export function renderBaseLayout(
9821081 syncTerminalTitle(state);
9831082 }
9841083
1084+ const divider = renderDivider(state, cols);
1085+ const input = renderInputArea(state.theme, inputController);
1086+ const statusBar = renderStatusBar(state, cols);
1087+ const conversationViewportHeight = getConversationViewportHeight(
1088+ cols,
1089+ rows,
1090+ divider,
1091+ input,
1092+ statusBar,
1093+ );
1094+
9851095 return VStack(
9861096 {
9871097 height: "100%",
@@ -1012,16 +1122,16 @@ export function renderBaseLayout(
10121122 },
10131123 [
10141124 // ── Conversation log ──
1015- renderConversationLog(state, cols),
1125+ renderConversationLog(state, cols, conversationViewportHeight),
10161126
10171127 // ── Animated divider (pulse when agent is working) ──
1018- renderDivider(state, cols),
1128+ divider,
10191129
10201130 // ── Input area ──
1021- renderInputArea(state.theme, inputController),
1131+ input,
10221132
10231133 // ── Status bar (1 line) ──
1024- renderStatusBar(state, cols),
1134+ statusBar,
10251135 ],
10261136 );
10271137 }
@@ -1044,16 +1154,22 @@ export function startUI(state: AppState): void {
10441154
10451155 cel.viewport(() => {
10461156 const cols = terminal.columns;
1047- const base = renderBaseLayout(state, cols, inputController, () => {
1048- suspendToBackground(() => {
1049- resumeTerminalUi();
1050- cel._getBuffer()?.clear();
1051- if (state.running) {
1052- startDividerAnimation();
1053- }
1054- requestRender("immediate");
1055- });
1056- });
1157+ const base = renderBaseLayout(
1158+ state,
1159+ cols,
1160+ inputController,
1161+ () => {
1162+ suspendToBackground(() => {
1163+ resumeTerminalUi();
1164+ cel._getBuffer()?.clear();
1165+ if (state.running) {
1166+ startDividerAnimation();
1167+ }
1168+ requestRender("immediate");
1169+ });
1170+ },
1171+ terminal.rows,
1172+ );
10571173 const overlay = renderActiveOverlay(state);
10581174
10591175 if (overlay) {