I am adding here a bit of research about the issue discovered with the agent:
Root Cause Analysis:
After investigating the source code (packages/opencode/src/cli/cmd/tui/util/clipboard.ts), I found that:
- OpenCode correctly implements OSC 52 in the
writeOsc52() function (lines 13-21)
- The function writes the escape sequence via
process.stdout.write(sequence)
- However, when the TUI is running,
process.stdout appears to be intercepted by the opentui rendering framework
- The OSC 52 escape sequence gets captured by the TUI layer instead of being passed through to the terminal emulator
- The native fallback (
xclip) also fails over SSH because there's no X11 display ($DISPLAY is empty) [this assumes my setup, opencode over simple ssh]
Proof that OSC 52 works in my terminal:
Running this directly in Ghostty successfully copies to clipboard:
printf '\e]52;c;%s\a' "$(echo -n 'test' | base64)"
Suggested fix:
What claude suggest for a fix it to write directly to /dev/tty instead of process.stdout. I don't have much expertise in these systems, so I cannot judge, however it feels there might be lots of edge cases to consider.
Yes, there's a simple fix. Looking at the code:
function writeOsc52(text: string): void {
if (!process.stdout.isTTY) return
const base64 = Buffer.from(text).toString("base64")
const osc52 = \x1b]52;c;${base64}\x07
const passthrough = process.env["TMUX"] || process.env["STY"]
const sequence = passthrough ? \x1bPtmux;\x1b${osc52}\x1b\\ : osc52
process.stdout.write(sequence) // <-- This gets intercepted by TUI
}
The fix: Write directly to /dev/tty instead of process.stdout:
import { openSync, writeSync, closeSync } from "fs"
function writeOsc52(text: string): void {
if (!process.stdout.isTTY) return
const base64 = Buffer.from(text).toString("base64")
const osc52 = \x1b]52;c;${base64}\x07
const passthrough = process.env["TMUX"] || process.env["STY"]
const sequence = passthrough ? \x1bPtmux;\x1b${osc52}\x1b\\ : osc52
try {
const fd = openSync("/dev/tty", "w")
writeSync(fd, sequence)
closeSync(fd)
} catch {
// Fallback to stdout if /dev/tty isn't available
process.stdout.write(sequence)
}
}
Why this works: /dev/tty is a direct handle to the controlling terminal, bypassing any stdout redirection or interception by the TUI framework.