Skip to content
TypeScript

perf(mobile): append terminal output without replaying history (#24261)

Julius Marminge1eb4071a497c
diff --git a/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalModule.kt b/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalModule.kt
index cf99f02c028..b8c92f2d933 100644
--- a/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalModule.kt
+++ b/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalModule.kt
@@ -11,9 +11,14 @@ class T3TerminalModule : Module() {
// logs so a stale native binary is distinguishable from a broken key pipeline.
Constants(
"hardwareKeyRevision" to 2,
+ "outputRevision" to 1,
)
View(T3TerminalView::class) {
+ AsyncFunction("writeOutput") { view: T3TerminalView, data: String, reset: Boolean ->
+ view.writeOutput(data, reset)
+ }
+
Prop("terminalKey") { view: T3TerminalView, terminalKey: String ->
view.terminalKey = terminalKey
}
diff --git a/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt b/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt
index ab7d306e596..f4de25460d5 100644
--- a/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt
+++ b/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt
@@ -55,6 +55,7 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex
}
private var terminalHandle = 0L
private var fedBuffer = ""
+ private var streamedOutput: TerminalOutputBuffer? = null
private var cols = 0
private var rows = 0
private var clearingInput = false
@@ -361,6 +362,7 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex
paletteColors,
)
fedBuffer = ""
+ streamedOutput?.let { output -> feedOutput(output.data) }
}
private fun recreateTerminal() {
@@ -380,7 +382,7 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex
}
private fun feedPendingBuffer() {
- if (terminalHandle == 0L || initialBuffer == fedBuffer) return
+ if (streamedOutput != null || terminalHandle == 0L || initialBuffer == fedBuffer) return
if (!initialBuffer.startsWith(fedBuffer)) {
recreateTerminal()
if (terminalHandle == 0L) return
@@ -399,6 +401,29 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex
renderSnapshot()
}
+ fun writeOutput(data: String, reset: Boolean) {
+ if (isCleanedUp) return
+ val output = streamedOutput ?: TerminalOutputBuffer()
+ val bytes = data.toByteArray(Charsets.UTF_8)
+ output.write(bytes, reset)
+ streamedOutput = output
+ if (reset) {
+ recreateTerminal()
+ } else if (terminalHandle != 0L) {
+ feedOutput(bytes)
+ renderSnapshot()
+ }
+ }
+
+ private fun feedOutput(data: ByteArray) {
+ if (terminalHandle == 0L || data.isEmpty()) return
+ emitResponse(GhosttyBridge.nativeFeed(terminalHandle, data))
+ if (terminalCanvas.hasActiveSelection()) {
+ GhosttyBridge.nativeClearSelection(terminalHandle)
+ terminalCanvas.resetSelectionState()
+ }
+ }
+
private fun renderSnapshot() {
if (terminalHandle == 0L) return
TerminalFrame.decode(
diff --git a/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/TerminalOutputBuffer.kt b/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/TerminalOutputBuffer.kt
new file mode 100644
index 00000000000..fde3393b911
--- /dev/null
+++ b/apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/TerminalOutputBuffer.kt
@@ -0,0 +1,71 @@
+package expo.modules.t3terminal
+
+import java.util.ArrayDeque
+
+private const val REPLAY_LIMIT = 512 * 1024
+private const val REPLAY_CHUNK_SIZE = 16 * 1024
+
+// Keep only replay history for intentional terminal recreation.
+internal class TerminalOutputBuffer {
+ private val chunks = ArrayDeque<ByteArray>()
+ private var firstOffset = 0
+ private var tailSize = 0
+ private var size = 0
+
+ // Only terminal recreation needs contiguous history; appends reuse the retained chunks.
+ val data: ByteArray
+ get() {
+ val result = ByteArray(size)
+ var destination = 0
+ var start = firstOffset
+ for (chunk in chunks) {
+ val end = if (chunk === chunks.last) tailSize else chunk.size
+ chunk.copyInto(result, destination, start, end)
+ destination += end - start
+ start = 0
+ }
+ return result
+ }
+
+ fun write(incoming: ByteArray, reset: Boolean) {
+ if (reset || incoming.size >= REPLAY_LIMIT) clear()
+ var start = maxOf(0, incoming.size - REPLAY_LIMIT)
+ while (start < incoming.size && incoming[start].toInt() and 0xC0 == 0x80) start += 1
+ while (start < incoming.size) {
+ if (chunks.isEmpty() || tailSize == REPLAY_CHUNK_SIZE) {
+ chunks.addLast(ByteArray(REPLAY_CHUNK_SIZE))
+ tailSize = 0
+ }
+ val count = minOf(incoming.size - start, REPLAY_CHUNK_SIZE - tailSize)
+ incoming.copyInto(chunks.last, tailSize, start, start + count)
+ start += count
+ tailSize += count
+ size += count
+ }
+ discardPrefix(maxOf(0, size - REPLAY_LIMIT))
+ while (size > 0 && chunks.first[firstOffset].toInt() and 0xC0 == 0x80) discardPrefix(1)
+ }
+
+ private fun discardPrefix(count: Int) {
+ var remaining = count
+ while (remaining > 0) {
+ val end = if (chunks.size == 1) tailSize else chunks.first.size
+ val discarded = minOf(remaining, end - firstOffset)
+ firstOffset += discarded
+ remaining -= discarded
+ size -= discarded
+ if (firstOffset == end) {
+ chunks.removeFirst()
+ firstOffset = 0
+ if (chunks.isEmpty()) tailSize = 0
+ }
+ }
+ }
+
+ private fun clear() {
+ chunks.clear()
+ firstOffset = 0
+ tailSize = 0
+ size = 0
+ }
+}
diff --git a/apps/mobile/modules/t3-terminal/ios/T3TerminalModule.swift b/apps/mobile/modules/t3-terminal/ios/T3TerminalModule.swift
index a3ac8f622c5..2f744526588 100644
--- a/apps/mobile/modules/t3-terminal/ios/T3TerminalModule.swift
+++ b/apps/mobile/modules/t3-terminal/ios/T3TerminalModule.swift
@@ -8,9 +8,14 @@ public class T3TerminalModule: Module {
// logs so a stale native binary is distinguishable from a broken key pipeline.
Constants([
"hardwareKeyRevision": 3,
+ "outputRevision": 1,
])
View(T3TerminalView.self) {
+ AsyncFunction("writeOutput") { (view: T3TerminalView, data: String, reset: Bool) in
+ view.writeOutput(data, reset: reset)
+ }
+
Prop("terminalKey") { (view: T3TerminalView, terminalKey: String) in
view.terminalKey = terminalKey
}
diff --git a/apps/mobile/modules/t3-terminal/ios/T3TerminalView.swift b/apps/mobile/modules/t3-terminal/ios/T3TerminalView.swift
index f407080371e..364880aaeb3 100644
--- a/apps/mobile/modules/t3-terminal/ios/T3TerminalView.swift
+++ b/apps/mobile/modules/t3-terminal/ios/T3TerminalView.swift
@@ -196,6 +196,7 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
private var lastContentScale: CGFloat = 0
private var lastReportedGrid: (cols: Int, rows: Int)?
private var lastAppliedBuffer = ""
+ private var streamedOutput: TerminalOutputBuffer?
private var pendingVerticalScrollPoints: CGFloat = 0
private var app: ghostty_app_t?
private var surface: ghostty_surface_t?
@@ -526,7 +527,11 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
ghostty_surface_set_color_scheme(createdSurface, appearance.ghosttyColorScheme)
setupWriteCallback()
resizeSurface()
- feedBuffer(initialBuffer)
+ if let streamedOutput {
+ feedData(streamedOutput.data)
+ } else {
+ feedBuffer(initialBuffer)
+ }
}
private func resetSurface() {
@@ -557,6 +562,7 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
}
private func applyRemoteBuffer(_ buffer: String) {
+ guard streamedOutput == nil else { return }
guard surface != nil else {
createSurfaceIfPossible()
return
@@ -579,6 +585,20 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
createSurfaceIfPossible()
}
+ func writeOutput(_ data: String, reset: Bool) {
+ let output = streamedOutput ?? TerminalOutputBuffer()
+ let bytes = Data(data.utf8)
+ output.write(bytes, reset: reset)
+ streamedOutput = output
+ if reset {
+ refreshSurface()
+ } else if surface == nil {
+ createSurfaceIfPossible()
+ } else {
+ feedData(bytes)
+ }
+ }
+
private func feedBuffer(_ buffer: String) {
guard !buffer.isEmpty else { return }
feedData(Data(buffer.utf8))
diff --git a/apps/mobile/modules/t3-terminal/ios/TerminalOutputBuffer.swift b/apps/mobile/modules/t3-terminal/ios/TerminalOutputBuffer.swift
new file mode 100644
index 00000000000..b25c18cd0b6
--- /dev/null
+++ b/apps/mobile/modules/t3-terminal/ios/TerminalOutputBuffer.swift
@@ -0,0 +1,28 @@
+import Foundation
+
+// Keep only replay history for intentional surface recreation (font/theme changes).
+final class TerminalOutputBuffer {
+ private var storage = Data()
+ private var retainedStart = 0
+ private let limit = 512 * 1024
+
+ var data: Data { storage.subdata(in: retainedStart..<storage.count) }
+
+ func write(_ incoming: Data, reset: Bool) {
+ if reset || incoming.count >= limit {
+ storage = incoming.subdata(in: max(0, incoming.count - limit)..<incoming.count)
+ retainedStart = 0
+ } else {
+ storage.append(incoming)
+ retainedStart = max(retainedStart, storage.count - limit)
+ }
+ while retainedStart < storage.count && storage[retainedStart] & 0xC0 == 0x80 {
+ retainedStart += 1
+ }
+ // Compact once per history window, rather than copying 512 KiB on every append.
+ if retainedStart >= limit {
+ storage = storage.subdata(in: retainedStart..<storage.count)
+ retainedStart = 0
+ }
+ }
+}
diff --git a/apps/mobile/src/features/terminal/NativeTerminalSurface.test.tsx b/apps/mobile/src/features/terminal/NativeTerminalSurface.test.tsx
new file mode 100644
index 00000000000..bfaada02942
--- /dev/null
+++ b/apps/mobile/src/features/terminal/NativeTerminalSurface.test.tsx
@@ -0,0 +1,153 @@
+import {
+ applyTerminalAttachStreamEvent,
+ EMPTY_TERMINAL_BUFFER_STATE,
+ terminalOutputText,
+} from "@t3tools/client-runtime/state/terminal";
+import { act, useImperativeHandle, useLayoutEffect, type ReactNode } from "react";
+import { createRoot } from "react-dom/client";
+import type { NativeSyntheticEvent } from "react-native";
+import { expect, it, vi } from "vite-plus/test";
+
+import type { NativeTerminalSurfaceProps } from "./nativeTerminalModule";
+
+const mocks = vi.hoisted(() => ({
+ revision: null as number | null,
+ initialBuffers: [] as string[],
+ mounts: vi.fn(),
+ unmounts: vi.fn(),
+ write: vi.fn(async (_data: string, _reset: boolean) => {}),
+}));
+
+function RawView({ ref: nativeRef, initialBuffer, onResize }: NativeTerminalSurfaceProps) {
+ useImperativeHandle(nativeRef, () => ({ writeOutput: mocks.write }));
+ useLayoutEffect(() => {
+ mocks.mounts();
+ return () => {
+ mocks.unmounts();
+ };
+ }, []);
+ useLayoutEffect(() => {
+ mocks.initialBuffers.push(initialBuffer);
+ }, [initialBuffer]);
+ useLayoutEffect(() => {
+ onResize?.({ nativeEvent: { cols: 80, rows: 24 } } as NativeSyntheticEvent<{
+ cols: number;
+ rows: number;
+ }>);
+ }, [onResize]);
+ return null;
+}
+
+vi.mock("expo", () => ({
+ requireNativeView: () => RawView,
+ requireOptionalNativeModule: () =>
+ mocks.revision === null ? {} : { outputRevision: mocks.revision },
+}));
+vi.mock("react-native", () => ({ View: ({ children }: { children: ReactNode }) => children }));
+vi.mock("../../components/ui/AppText", () => ({ AppText: () => null }));
+vi.mock("../../lib/typography", () => ({ MOBILE_TYPOGRAPHY: { label: { fontSize: 10 } } }));
+vi.mock("../settings/appearance/AppearancePreferencesProvider", () => ({
+ useAppearancePreferences: () => ({ themeAppearance: "dark", themeId: "default" }),
+}));
+vi.mock("./terminalTheme", () => ({
+ buildGhosttyThemeConfig: () => "theme",
+ getMobileTerminalTheme: () => ({}),
+}));
+
+import { TerminalSurface } from "./NativeTerminalSurface";
+
+function setup(revision: number | null) {
+ mocks.revision = revision;
+ mocks.initialBuffers = [];
+ mocks.write.mockReset();
+ mocks.write.mockResolvedValue(undefined);
+ mocks.mounts.mockClear();
+ mocks.unmounts.mockClear();
+ const document = { nodeType: 9, addEventListener() {}, removeEventListener() {} };
+ const container = {
+ nodeType: 1,
+ tagName: "DIV",
+ namespaceURI: "http://www.w3.org/1999/xhtml",
+ ownerDocument: document,
+ addEventListener() {},
+ removeEventListener() {},
+ };
+ vi.stubGlobal("document", document);
+ vi.stubGlobal("window", { document, HTMLIFrameElement: EventTarget });
+ vi.stubGlobal("expo", { getViewConfig: () => ({}) });
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ const root = createRoot(container as unknown as HTMLElement);
+ return {
+ async render(
+ state: typeof EMPTY_TERMINAL_BUFFER_STATE,
+ terminalKey = "environment:thread:term-1",
+ ) {
+ await act(async () => {
+ root.render(
+ <TerminalSurface
+ terminalKey={terminalKey}
+ output={state.version === 0 ? null : state.output}
+ buffer={
+ state.version === 0
+ ? null
+ : (revision ?? 0) >= 1
+ ? ""
+ : terminalOutputText(state.output)
+ }
+ isRunning
+ onInput={() => {}}
+ onResize={() => {}}
+ />,
+ );
+ });
+ },
+ async [Symbol.asyncDispose]() {
+ await act(async () => root.unmount());
+ vi.unstubAllGlobals();
+ },
+ };
+}
+
+it.each([null, 1])(
+ "preserves the native view across rollover and uses legacy history on older binaries (revision %s)",
+ async (revision) => {
+ await using harness = setup(revision);
+ let state = applyTerminalAttachStreamEvent(
+ EMPTY_TERMINAL_BUFFER_STATE,
+ {
+ type: "output",
+ threadId: "thread",
+ terminalId: "term-1",
+ data: "retained",
+ },
+ 8,
+ );
+ await harness.render(state);
+ state = applyTerminalAttachStreamEvent(
+ state,
+ {
+ type: "output",
+ threadId: "thread",
+ terminalId: "term-1",
+ data: "-new",
+ },
+ 8,
+ );
+ await harness.render(state);
+ await harness.render(EMPTY_TERMINAL_BUFFER_STATE);
+ expect(mocks.mounts).toHaveBeenCalledTimes(1);
+ expect(mocks.unmounts).not.toHaveBeenCalled();
+ if (revision === 1) {
+ expect(mocks.initialBuffers).toEqual([""]);
+ expect(mocks.write.mock.calls).toEqual([
+ ["retained", true],
+ ["-new", false],
+ ]);
+ await harness.render(state, "another-environment:thread:term-1");
+ expect(mocks.write).toHaveBeenLastCalledWith("ined-new", true);
+ } else {
+ expect(mocks.write).not.toHaveBeenCalled();
+ expect(mocks.initialBuffers).toEqual(["retained", "ined-new"]);
+ }
+ },
+);
diff --git a/apps/mobile/src/features/terminal/NativeTerminalSurface.tsx b/apps/mobile/src/features/terminal/NativeTerminalSurface.tsx
index ea354ea14f9..bfd89bfb683 100644
--- a/apps/mobile/src/features/terminal/NativeTerminalSurface.tsx
+++ b/apps/mobile/src/features/terminal/NativeTerminalSurface.tsx
@@ -1,4 +1,5 @@
-import { memo, useCallback, useEffect, useRef } from "react";
+import type { TerminalOutputState } from "@t3tools/client-runtime/state/terminal";
+import { memo, useCallback, useEffect, useRef, useState } from "react";
import {
Pressable,
ScrollView,
@@ -16,6 +17,8 @@ import { useAppearancePreferences } from "../settings/appearance/AppearancePrefe
import {
getNativeTerminalHardwareKeyRevision,
resolveNativeTerminalSurfaceView,
+ supportsNativeTerminalOutput,
+ type NativeTerminalSurfaceHandle,
} from "./nativeTerminalModule";
import {
buildGhosttyThemeConfig,
@@ -24,6 +27,7 @@ import {
} from "./terminalTheme";
import { terminalDebugLog } from "./terminalDebugLog";
import { useTerminalSurfaceBuffer } from "./useTerminalSurfaceBuffer";
+import { createTerminalOutputWriter } from "./terminalOutputWriter";
interface TerminalInputEvent {
readonly data: string;
@@ -37,6 +41,7 @@ interface TerminalResizeEvent {
interface TerminalSurfaceProps extends ViewProps {
readonly terminalKey: string;
readonly buffer: string | null;
+ readonly output: TerminalOutputState | null;
readonly fontSize?: number;
readonly isRunning: boolean;
readonly readOnly?: boolean;
@@ -186,7 +191,7 @@ const FallbackTerminalSurface = memo(function FallbackTerminalSurface(
export const TerminalSurface = memo(function TerminalSurface(props: TerminalSurfaceProps) {
const buffer = useTerminalSurfaceBuffer(props);
- return <ReadyTerminalSurface {...props} buffer={buffer} />;
+ return <ReadyTerminalSurface key={props.terminalKey} {...props} buffer={buffer} />;
});
const ReadyTerminalSurface = memo(function ReadyTerminalSurface(props: ReadyTerminalSurfaceProps) {
@@ -196,6 +201,28 @@ const ReadyTerminalSurface = memo(function ReadyTerminalSurface(props: ReadyTerm
const { onInput, onResize } = props;
const NativeTerminalSurfaceView = resolveNativeTerminalSurfaceView();
const hasNativeSurface = Boolean(NativeTerminalSurfaceView);
+ const streaming = supportsNativeTerminalOutput();
+ const nativeRef = useRef<NativeTerminalSurfaceHandle>(null);
+ const writerRef = useRef<ReturnType<typeof createTerminalOutputWriter> | null>(null);
+ const [nativeReady, setNativeReady] = useState(false);
+
+ useEffect(() => {
+ const view = nativeRef.current;
+ if (!streaming || !nativeReady || view === null) return;
+ const writer = createTerminalOutputWriter((data, reset) => view.writeOutput(data, reset));
+ writerRef.current = writer;
+ return () => {
+ writerRef.current = null;
+ writer.dispose();
+ };
+ }, [nativeReady, streaming]);
+
+ useEffect(() => {
+ if (!streaming || !nativeReady || props.output === null) return;
+ void writerRef.current?.write(props.output).catch((error: unknown) => {
+ console.error("Terminal output delivery failed", error);
+ });
+ }, [nativeReady, props.output, streaming]);
useEffect(() => {
terminalDebugLog("native:surface", {
@@ -220,6 +247,8 @@ const ReadyTerminalSurface = memo(function ReadyTerminalSurface(props: ReadyTerm
);
const handleNativeResize = useCallback(
(event: NativeSyntheticEvent<TerminalResizeEvent>) => {
+ // The first grid event comes from the mounted native surface, after registration.
+ setNativeReady(true);
onResize({
cols: event.nativeEvent.cols,
rows: event.nativeEvent.rows,
@@ -232,6 +261,7 @@ const ReadyTerminalSurface = memo(function ReadyTerminalSurface(props: ReadyTerm
return (
<View style={props.style}>
<NativeTerminalSurfaceView
+ ref={nativeRef}
appearanceScheme={themeAppearance}
autoFocus={!props.readOnly && (props.autoFocus ?? true)}
readOnly={props.readOnly ?? false}
@@ -240,7 +270,7 @@ const ReadyTerminalSurface = memo(function ReadyTerminalSurface(props: ReadyTerm
foregroundColor={theme.foreground}
mutedForegroundColor={theme.mutedForeground}
terminalKey={props.terminalKey}
- initialBuffer={props.buffer}
+ initialBuffer={streaming ? "" : props.buffer}
fontSize={fontSize}
style={{ flex: 1 }}
themeConfig={buildGhosttyThemeConfig(theme)}
diff --git a/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx b/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx
index f933619c67d..bffcb221772 100644
--- a/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx
+++ b/apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx
@@ -17,7 +17,7 @@ import { Platform, Pressable, View } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { AppText as Text } from "../../components/ui/AppText";
import { TerminalContextSheet } from "./TerminalContextSheet";
-import { hasNativeTerminalSurface } from "./nativeTerminalModule";
+import { hasNativeTerminalSurface, supportsNativeTerminalOutput } from "./nativeTerminalModule";
import * as Clipboard from "expo-clipboard";
import * as Schema from "effect/Schema";
import {
@@ -449,7 +449,9 @@ export function ThreadTerminalRouteScreen(props: ThreadTerminalRouteScreenProps)
);
const observingTerminal =
!canOperateTerminal && canReadTerminal && selectedThread !== null && hasTerminalTarget;
+ const streamingOutput = supportsNativeTerminalOutput();
const terminal = useAttachedTerminalSession({
+ materializeBuffer: !streamingOutput,
environmentId: selectedThread?.environmentId ?? null,
terminal: canOperateTerminal
? terminalAttachInput
@@ -1245,6 +1247,7 @@ export function ThreadTerminalRouteScreen(props: ThreadTerminalRouteScreenProps)
autoFocus={canOperateTerminal && terminalAutoFocus}
readOnly={!canOperateTerminal}
buffer={terminal.version === 0 ? null : terminalSurfaceBuffer}
+ output={terminal.version === 0 ? null : terminal.output}
fontSize={fontSize}
isRunning={isRunning}
keyboardFocusRequest={keyboardFocusRequest}
diff --git a/apps/mobile/src/features/terminal/nativeTerminalModule.ts b/apps/mobile/src/features/terminal/nativeTerminalModule.ts
index b7c8bba084c..ba5b6e4f5d3 100644
--- a/apps/mobile/src/features/terminal/nativeTerminalModule.ts
+++ b/apps/mobile/src/features/terminal/nativeTerminalModule.ts
@@ -1,4 +1,4 @@
-import type { ComponentType } from "react";
+import type { ComponentType, Ref } from "react";
import type { NativeSyntheticEvent, ViewProps } from "react-native";
import { requireNativeView, requireOptionalNativeModule } from "expo";
@@ -22,6 +22,7 @@ interface TerminalResizeEvent {
}
export interface NativeTerminalSurfaceProps extends ViewProps {
+ readonly ref?: Ref<NativeTerminalSurfaceHandle>;
readonly appearanceScheme?: "light" | "dark";
readonly autoFocus?: boolean;
readonly readOnly?: boolean;
@@ -39,6 +40,10 @@ export interface NativeTerminalSurfaceProps extends ViewProps {
readonly onResize?: (event: NativeSyntheticEvent<TerminalResizeEvent>) => void;
}
+export interface NativeTerminalSurfaceHandle {
+ readonly writeOutput: (data: string, reset: boolean) => Promise<void>;
+}
+
let cachedNativeTerminalSurfaceView: ComponentType<NativeTerminalSurfaceProps> | undefined;
let nativeTerminalSurfaceViewResolutionFailed = false;
@@ -96,3 +101,14 @@ export function getNativeTerminalHardwareKeyRevision(): number | null {
export function hasNativeTerminalSurface() {
return resolveNativeTerminalSurfaceView() !== null;
}
+
+export function supportsNativeTerminalOutput(): boolean {
+ try {
+ const module = requireOptionalNativeModule<{ readonly outputRevision?: number }>(
+ NATIVE_TERMINAL_MODULE_NAME,
+ );
+ return (module?.outputRevision ?? 0) >= 1 && hasNativeTerminalSurface();
+ } catch {
+ return false;
+ }
+}
diff --git a/apps/mobile/src/features/terminal/terminalOutputWriter.test.ts b/apps/mobile/src/features/terminal/terminalOutputWriter.test.ts
new file mode 100644
index 00000000000..877346e58aa
--- /dev/null
+++ b/apps/mobile/src/features/terminal/terminalOutputWriter.test.ts
@@ -0,0 +1,73 @@
+import {
+ applyTerminalAttachStreamEvent,
+ DEFAULT_MAX_TERMINAL_BUFFER_BYTES,
+ EMPTY_TERMINAL_BUFFER_STATE,
+ terminalOutputText,
+} from "@t3tools/client-runtime/state/terminal";
+import { expect, it, vi } from "vite-plus/test";
+
+import { createTerminalOutputWriter } from "./terminalOutputWriter";
+
+function append(
+ state: typeof EMPTY_TERMINAL_BUFFER_STATE,
+ data: string,
+ limit = DEFAULT_MAX_TERMINAL_BUFFER_BYTES,
+) {
+ return applyTerminalAttachStreamEvent(
+ state,
+ { type: "output", data, threadId: "thread", terminalId: "term-1" },
+ limit,
+ );
+}
+
+it("appends across rollover without replay and seeds fresh writers with only bounded history", async () => {
+ const write = vi.fn(async (_data: string, _reset: boolean) => {});
+ const writer = createTerminalOutputWriter(write);
+ let state = EMPTY_TERMINAL_BUFFER_STATE;
+ const chunks: string[] = [];
+ await writer.write(state.output);
+ for (let index = 0; index < 128; index += 1) {
+ const chunk = `${index}\r\n`.padEnd(16 * 1024, "x");
+ chunks.push(chunk);
+ state = append(state, chunk);
+ await writer.write(state.output);
+ expect(write).toHaveBeenLastCalledWith(chunk, false);
+ }
+ expect(state.output.retainedBytes).toBe(DEFAULT_MAX_TERMINAL_BUFFER_BYTES);
+ const retained = chunks.slice(-32).join("");
+ expect(terminalOutputText(state.output)).toBe(retained);
+ expect(write.mock.calls.filter(([, reset]) => reset)).toEqual([["", true]]);
+ expect(write.mock.calls.reduce((total, [data]) => total + data.length, 0)).toBe(2 * 1024 * 1024);
+
+ const remountedWrite = vi.fn(async () => {});
+ await createTerminalOutputWriter(remountedWrite).write(state.output);
+ expect(remountedWrite.mock.calls).toEqual([[retained, true]]);
+});
+
+it("recovers the newer snapshot queued during a rejected append without a third write", async () => {
+ const started = Promise.withResolvers<void>();
+ const release = Promise.withResolvers<void>();
+ const write = vi.fn(async () => {});
+ const writer = createTerminalOutputWriter(write);
+ const initial = append(EMPTY_TERMINAL_BUFFER_STATE, "initial");
+ await writer.write(initial.output);
+ write.mockImplementationOnce(async () => {
+ started.resolve();
+ await release.promise;
+ throw new Error("partially applied append");
+ });
+ const first = append(initial, "-first");
+ const draining = writer.write(first.output);
+ await started.promise;
+ const latest = append(first, "-latest");
+ void writer.write(latest.output);
+ release.resolve();
+ await draining;
+ expect(write.mock.calls).toEqual([
+ ["initial", true],
+ ["-first", false],
+ ["initial-first-latest", true],
+ ]);
+ await writer.write(append(latest, "-next").output);
+ expect(write).toHaveBeenLastCalledWith("-next", false);
+});
diff --git a/apps/mobile/src/features/terminal/terminalOutputWriter.ts b/apps/mobile/src/features/terminal/terminalOutputWriter.ts
new file mode 100644
index 00000000000..74c1225b76f
--- /dev/null
+++ b/apps/mobile/src/features/terminal/terminalOutputWriter.ts
@@ -0,0 +1,56 @@
+import {
+ INITIAL_TERMINAL_OUTPUT_CURSOR,
+ readTerminalOutputUpdate,
+ type TerminalOutputState,
+} from "@t3tools/client-runtime/state/terminal";
+
+/** One writer per mounted native view; intermediate snapshots can be coalesced safely. */
+export function createTerminalOutputWriter(
+ writeOutput: (data: string, reset: boolean) => Promise<void>,
+) {
+ let cursor = INITIAL_TERMINAL_OUTPUT_CURSOR;
+ let pending: TerminalOutputState | null = null;
+ let draining: Promise<void> | null = null;
+ let disposed = false;
+
+ async function drain() {
+ let recovering = false;
+ try {
+ while (pending !== null) {
+ if (disposed) return;
+ const output = pending;
+ pending = null;
+ const update = readTerminalOutputUpdate(output, cursor);
+ if (update.type === "none") continue;
+ try {
+ await writeOutput(update.data, update.type === "reset");
+ } catch (error) {
+ if (disposed) return;
+ // A rejected command may have partially applied. Replay the latest retained history
+ // once per drain; a persistent native failure must settle instead of spinning.
+ cursor = INITIAL_TERMINAL_OUTPUT_CURSOR;
+ pending ??= output;
+ if (recovering) throw error;
+ recovering = true;
+ continue;
+ }
+ cursor = update.cursor;
+ }
+ } finally {
+ draining = null;
+ }
+ }
+
+ return {
+ write(output: TerminalOutputState): Promise<void> {
+ if (disposed) return Promise.resolve();
+ pending = output;
+ draining ??= Promise.resolve().then(drain);
+ return draining;
+ },
+ dispose() {
+ disposed = true;
+ pending = null;
+ },
+ };
+}
diff --git a/apps/mobile/src/state/use-terminal-session.ts b/apps/mobile/src/state/use-terminal-session.ts
index 14624e78ebd..e4171b94c22 100644
--- a/apps/mobile/src/state/use-terminal-session.ts
+++ b/apps/mobile/src/state/use-terminal-session.ts
@@ -28,6 +28,7 @@ const EMPTY_LEGACY_TERMINAL_SESSION_STATE: LegacyTerminalSessionState = {
export function useAttachedTerminalSession(input: {
readonly environmentId: EnvironmentId | null;
readonly terminal: TerminalAttachInput | null;
+ readonly materializeBuffer?: boolean;
}): LegacyTerminalSessionState {
const canRead = useEnvironmentScope(input.environmentId, AuthTerminalReadScope);
const canOperate = useEnvironmentScope(input.environmentId, AuthTerminalOperateScope);
@@ -55,8 +56,12 @@ export function useAttachedTerminalSession(input: {
}),
);
const output = attach.data?.output ?? EMPTY_TERMINAL_BUFFER_STATE.output;
- // Keep initialBuffer materialization at the mobile boundary until native streaming ships.
- const buffer = useMemo(() => terminalOutputText(output), [output]);
+ const materializeBuffer = input.materializeBuffer ?? true;
+ // Older installed binaries and the text fallback still need initialBuffer.
+ const buffer = useMemo(
+ () => (materializeBuffer ? terminalOutputText(output) : ""),
+ [materializeBuffer, output],
+ );
return useMemo(() => {
if (input.environmentId === null || input.terminal === null) {