fix(terminal): restore copying from Claude Code’s fullscreen TUI #9949
Closedcallmemorgan(GitHub profile, opens in a new tab)wants to merge 16 commits into
main fromrefs/pull/9949/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/web/src/components/ThreadTerminalDrawer.tsx line 476OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -472,3 +473,4 @@472473 onData: (data) => handleData(data),473474 onResize: (cols, rows) => void resizeTerminal(cols, rows),474475 onSelectionChange: () => handleSelectionChange(),476+ onClipboardWrite: (text) => void writeTerminalClipboard(text),apps/web/src/terminal/ghostty/clipboard.ts lines 3 to 3OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,3 @@1+// Bound retained OSC text independently of terminal scrollback.2+const MAX_OSC_LENGTH = 1024 * 1024;3+const decoder = new TextDecoder("utf-8", { fatal: true });
- apps/web/src/terminal/ghostty/clipboard.ts line 10OutdatedResolvedby callmemorgan(GitHub profile, opens in a new tab)@@ -0,0 +1,10 @@1+// Bound retained OSC text independently of terminal scrollback.2+const MAX_OSC_LENGTH = 1024 * 1024;3+const decoder = new TextDecoder("utf-8", { fatal: true });4+// oxlint-disable-next-line no-control-regex -- C0 bytes are handled by the outer VT parser.5+const oscControl = /[\x00-\x1f]/g;6+7+/** Application output cannot use the focus-stealing, user-gesture copy fallback. */8+export async function writeTerminalClipboard(text: string): Promise<void> {9+ try {10+ await navigator.clipboard?.writeText(text);apps/web/src/components/ThreadTerminalDrawer.tsx line 476OutdatedResolvedby callmemorgan(GitHub profile, opens in a new tab)@@ -472,3 +473,4 @@472473 onData: (data) => handleData(data),473474 onResize: (cols, rows) => void resizeTerminal(cols, rows),474475 onSelectionChange: () => handleSelectionChange(),476+ onClipboardWrite: (text) => void writeTerminalClipboard(text),apps/web/src/terminal/ghostty/surface.ts lines 951 to 954Resolvedby callmemorgan(GitHub profile, opens in a new tab)@@ -912,3 +934,8 @@912934 this.input.focus({ preventScroll: true });913935 }914936937+ /** Sends committed user input; terminal-generated replies bypass selection cleanup. */938+ sendUserInput(data: string): void {939+ if (this.disposed || data.length === 0) return;940+ if (!this.selectionDrag && this.selectionAnchorScreen) this.clearSelection();941+ this.options.onData(data);
- apps/web/src/terminal/ghostty/clipboard.ts lines 52 to 54OutdatedResolvedby callmemorgan(GitHub profile, opens in a new tab)@@ -0,0 +34,21 @@34+ if (request.canWrite()) await navigator.clipboard?.writeText(request.text);35+ } catch {36+ // Browser-denied application copies must not write errors into the TUI.37+ }38+ request.resolve();39+ }40+ writingClipboard = false;41+}42+43+function decodeClipboardPayload(osc: string): string | null {44+ if (!osc.startsWith("52;")) return null;45+ const separator = osc.indexOf(";", 3);46+ if (separator === -1) return null;47+ const target = osc.slice(3, separator);48+ // Empty targets use the system clipboard. Queries never read or send the49+ // client's clipboard to a PTY; application selection buffers stay local.50+ if (target !== "" && (!/^[cps0-7]+$/.test(target) || !target.includes("c"))) return null;51+ const encoded = osc.slice(separator + 1);52+ if (encoded === "" || !/^[A-Za-z0-9+/]+={0,2}$/.test(encoded)) return null;53+ try {54+ return decoder.decode(Uint8Array.from(atob(encoded), (char) => char.charCodeAt(0))) || null;
- packages/client-runtime/src/terminalClipboard.ts lines 55 to 55OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +35,21 @@35+ pendingClipboardWrite = null;36+ try {37+ if (request.canWrite()) await writeText(request.text);38+ } catch {39+ // Clipboard failures must not write errors into the TUI or block later copies.40+ }41+ request.resolve();42+ }43+ writingClipboard = false;44+ }45+ return writeTerminalClipboard;46+}47+48+function decodeClipboardPayload(osc: string): string | null {49+ if (!osc.startsWith("52;")) return null;50+ const separator = osc.indexOf(";", 3);51+ if (separator === -1) return null;52+ const target = osc.slice(3, separator);53+ // Empty targets use the system clipboard. Queries never read or send the54+ // client's clipboard to a PTY; application selection buffers stay local.55+ if (target !== "" && (!/^[cps0-7]+$/.test(target) || !target.includes("c"))) return null;apps/mobile/src/features/terminal/terminalClipboard.ts line 29OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +9,21 @@9+export function createTerminalClipboardSession(10+ write: (text: string, canWrite: () => boolean) => Promise<void>,11+) {12+ let active = false;13+ let generation = 0;14+ let cursor = INITIAL_TERMINAL_OUTPUT_CURSOR;15+ const invalidate = () => {16+ generation += 1;17+ parser.invalidatePendingCopy();18+ };19+ const parser = new TerminalClipboardParser((text) => {20+ const requestedGeneration = generation;21+ void write(text, () => active && generation === requestedGeneration);22+ });23+ return {24+ setActive(next: boolean) {25+ active = next;26+ if (!active) invalidate();27+ },28+ update(output: TerminalOutputState) {29+ const update = readTerminalOutputUpdate(output, cursor);
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/web/src/components/ThreadTerminalDrawer.tsx line 533Resolvedby callmemorgan(GitHub profile, opens in a new tab)@@ -474 +481,21 @@474481 onSelectionChange: () => handleSelectionChange(),482+ onClipboardWrite: (text, canWrite) => {483+ if (typeof navigator.clipboard?.writeText === "function") {484+ void writeTerminalClipboard(text, canWrite);485+ } else if (canWrite()) {486+ toastManager.add({487+ id: clipboardToastId,488+ type: "info",489+ title: text === "" ? "Clear clipboard?" : "Terminal text ready to copy",490+ description: "Your browser needs a click to allow this clipboard write.",491+ timeout: 10_000,492+ actionProps: {493+ children: text === "" ? "Clear" : "Copy",494+ onClick: () => {495+ if (copyTerminalClipboardFromGesture(text)) toastManager.close(clipboardToastId);496+ else497+ toastManager.update(clipboardToastId, {498+ type: "error",499+ title: "Clipboard write was blocked",500+ });501+ },
- apps/web/src/components/ThreadTerminalDrawer.tsx lines 483 to 484OutdatedResolvedby callmemorgan(GitHub profile, opens in a new tab)@@ -472,3 +479,6 @@472479 onData: (data) => handleData(data),473480 onResize: (cols, rows) => void resizeTerminal(cols, rows),474481 onSelectionChange: () => handleSelectionChange(),482+ onClipboardWrite: (text, canWrite) => {483+ if (typeof navigator.clipboard?.writeText === "function") {484+ void writeTerminalClipboard(text, canWrite);
- apps/web/src/terminal/ghostty/surface.ts line 798OutdatedResolvedby callmemorgan(GitHub profile, opens in a new tab)@@ -761,5 +790,9 @@761790762791 resetAndWrite(data: string): void {763792 if (this.disposed) return;793+ this.clearSelection();764794 this.lastMouseMotionData = "";765795 this.core.resetAndWrite(data);796+ this.invalidateClipboardCopies();797+ this.clipboardParser.reset();798+ this.clipboardParser.write(data, false);
- apps/mobile/src/features/terminal/useTerminalClipboard.ts line 34OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +14,21 @@14+ environmentId: EnvironmentId | null,15+ input: TerminalAttachInput | null,16+) {17+ useFocusEffect(18+ useCallback(() => {19+ if (environmentId === null || input === null) return;20+ const session = createTerminalClipboardSession(writeClipboard);21+ const stop = terminalEnvironment.observeAttach({ environmentId, input }, session.update);22+ const activate = () => session.setActive(AppState.currentState === "active");23+ activate();24+ const change = AppState.addEventListener("change", activate);25+ const blur = AppState.addEventListener("blur", () => session.setActive(false));26+ const focus = AppState.addEventListener("focus", activate);27+ return () => {28+ session.setActive(false);29+ stop();30+ change.remove();31+ blur.remove();32+ focus.remove();33+ };34+ }, [environmentId, input]),
- apps/web/src/components/ThreadTerminalDrawer.tsx lines 591 to 595OutdatedResolvedby callmemorgan(GitHub profile, opens in a new tab)@@ -548,3 +582,14 @@548582 terminal.focus();549583 }550584585+ setupCleanups.push(586+ terminalEnvironment.observeAttach(587+ {588+ environmentId,589+ input: {590+ threadId,591+ terminalId,592+ cwd,593+ ...(worktreePath !== undefined ? { worktreePath } : {}),594+ ...(runtimeEnv ? { env: runtimeEnv } : {}),595+ },
- apps/web/src/components/ThreadTerminalDrawer.tsx line 588Resolvedby callmemorgan(GitHub profile, opens in a new tab)@@ -548,3 +581,4 @@548581 terminal.focus();549582 }550583584+ setupCleanups.push(
- apps/web/src/terminal/ghostty/surface.ts lines 1480 to 1483Resolvedby callmemorgan(GitHub profile, opens in a new tab)@@ -1389,4 +1483,20 @@1389- if (pointer) this.extendSelectionTo(pointer.x, pointer.y);1483+ const pointer = this.selectionDrag?.pointer;1484+ if (pointer) this.extendSelectionTo(this.cellAt(pointer.x, pointer.y));13901485 }, 80);13911486 }139214871488+ private endSelectionDrag(): void {1489+ const drag = this.selectionDrag;1490+ this.selectionDrag = null;1491+ this.setSelectionAutoscroll(0);1492+ if (drag && this.canvas.hasPointerCapture(drag.pointerId)) {1493+ this.canvas.releasePointerCapture(drag.pointerId);1494+ }1495+ }1496+1497+ private readonly onLostPointerCapture = (event: PointerEvent) => {1498+ if (this.canvas.hasPointerCapture(event.pointerId)) return;1499+ if (this.selectionDrag?.pointerId === event.pointerId) this.endSelectionDrag();1500+ if (this.mouseReportingPointerId === event.pointerId) {1501+ this.mouseReportingPointerId = null;1502+ this.mouseReportingButton = null;
callmemorgan(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


Codex Review Summary#
This comment shows the latest Codex review activity on this pull request.
8339d2eℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.