Skip to content

Commit a7e840b

Browse files
committed
fix: opencode handshake crash in herdr panes — upstream anomalyco/opencode#41483 (herdr answers capability handshake except OSC 10/11 → deterministic EXC_BREAKPOINT in bufferDrawTextBufferView); bin/answer-handshake.py pty wrapper answers all queries xterm-style and strips them from upstream; opencode pane launches go through it
1 parent 2d37bdb commit a7e840b

3 files changed

Lines changed: 189 additions & 5 deletions

File tree

‎bin/answer-handshake.py‎

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
#!/usr/bin/env python3
2+
"""Handshake-answer pty wrapper for opencode inside herdr panes.
3+
4+
opencode/OpenTUI crashes at startup (EXC_BREAKPOINT in bufferDrawTextBufferView)
5+
when the surrounding terminal answers its capability handshake only partially —
6+
herdr panes reply to most queries but not OSC 10/11 (upstream: sst/opencode#41483).
7+
The crash is deterministic: "herdr | everything except OSC 10/11 | CRASH".
8+
9+
This wrapper runs opencode inside a private pty, answers every capability
10+
query xterm-style (foreground/background colors, cursor position, device
11+
attributes, kitty keyboard/graphics), strips the queries from the outward
12+
stream, and forwards everything else verbatim in both directions.
13+
14+
Usage: answer-handshake.py <command> [args...]
15+
"""
16+
import os
17+
import pty
18+
import re
19+
import select
20+
import struct
21+
import subprocess
22+
import sys
23+
import termios
24+
import tty
25+
import fcntl
26+
27+
# Queries opencode sends at startup (captured in sst/opencode#41483):
28+
# ESC[?2031h color scheme change notifications (no reply expected)
29+
# ESC]10;? BEL/ST default foreground color query
30+
# ESC]11;? BEL/ST default background color query
31+
# ESC[>0q XTVERSION (terminal version) query
32+
# ESC[6n cursor position report query
33+
# ESC P + q 4d73 ST XTGETTCAP (kitty terminfo) query for "4d73" ("ms")
34+
# ESC[?1016$p DECRQM: mouse SGR-pixels mode
35+
# ESC[?2027$p DECRQM: rectangular editing mode
36+
# ESC[?2026$p DECRQM: synchronized output mode
37+
38+
OSC_FG_REPLY = b"\x1b]10;rgb:eeeeee/eeeeee/eeeeee\x1b\\"
39+
OSC_BG_REPLY = b"\x1b]11;rgb:0a0a0a/0a0a0a/0a0a0a\x1b\\"
40+
XTVERSION_REPLY = b"\x1bP>|xterm(370)\x1b\\"
41+
CPR_REPLY = b"\x1b[1;1R" # cursor at row 1 col 1
42+
XTGETTCAP_REPLY = b"\x1bP1+r4d73=01\x1b\\" # "ms" capability = 0x01 (yes)
43+
DECRQM_REPLY = {
44+
b"?1016": b"\x1b[?1016;2$y", # mode set
45+
b"?2027": b"\x1b[?2027;2$y",
46+
b"?2026": b"\x1b[?2026;2$y",
47+
}
48+
49+
QUERY_PATTERNS = [
50+
(re.compile(rb"\x1b\]10;\?(\x07|\x1b\\)"), b""),
51+
(re.compile(rb"\x1b\]11;\?(\x07|\x1b\\)"), b""),
52+
(re.compile(rb"\x1b\[>0q"), b""),
53+
(re.compile(rb"\x1b\[6n"), b""),
54+
(re.compile(rb"\x1bP\+q[0-9a-fA-F]+(\x1b\\|\x07)"), b""),
55+
(re.compile(rb"\x1b\[\?\d+\$p"), b""),
56+
]
57+
58+
59+
def strip_and_collect(data: bytes):
60+
"""Remove query sequences from outward stream; return (clean, answers)."""
61+
answers = []
62+
if b"\x1b]10;?" in data:
63+
answers.append(OSC_FG_REPLY)
64+
if b"\x1b]11;?" in data:
65+
answers.append(OSC_BG_REPLY)
66+
if b"\x1b[>0q" in data:
67+
answers.append(XTVERSION_REPLY)
68+
if b"\x1b[6n" in data:
69+
answers.append(CPR_REPLY)
70+
if b"\x1bP+q" in data:
71+
answers.append(XTGETTCAP_REPLY)
72+
for pat, dec in DECRQM_REPLY.items():
73+
if b"\x1b[?" + pat[1:] + b"$p" in data:
74+
answers.append(dec)
75+
clean = data
76+
for pat, _ in QUERY_PATTERNS:
77+
clean = pat.sub(b"", clean)
78+
return clean, answers
79+
80+
81+
def main():
82+
if len(sys.argv) < 2:
83+
print("usage: answer-handshake.py <command> [args...]", file=sys.stderr)
84+
sys.exit(1)
85+
86+
slave_fd, master_fd = pty.openpty()
87+
88+
# propagate the current pane size
89+
try:
90+
sz = fcntl.ioctl(0, termios.TIOCGWINSZ, struct.pack("HHHH", 0, 0, 0, 0))
91+
fcntl.ioctl(master_fd, termios.TIOCSWINSZ, sz)
92+
except OSError:
93+
pass
94+
95+
proc = subprocess.Popen(
96+
sys.argv[1:],
97+
stdin=slave_fd,
98+
stdout=slave_fd,
99+
stderr=slave_fd,
100+
close_fds=True,
101+
)
102+
os.close(slave_fd)
103+
104+
old_term = None
105+
try:
106+
old_term = termios.tcgetattr(0)
107+
tty.setraw(0)
108+
except termios.error:
109+
old_term = None # no controlling tty (piped) — run forward-only
110+
111+
try:
112+
while True:
113+
rlist = []
114+
if proc.poll() is None:
115+
rlist.append(master_fd)
116+
if old_term is not None:
117+
rlist.append(0)
118+
if not rlist:
119+
# piped mode: no stdin; wait for the child
120+
import time
121+
time.sleep(0.2)
122+
if proc.poll() is not None:
123+
break
124+
continue
125+
ready, _, _ = select.select(rlist, [], [], 1.0)
126+
127+
for fd in ready:
128+
try:
129+
data = os.read(fd, 65536)
130+
except OSError:
131+
data = b""
132+
if not data:
133+
continue
134+
if fd == master_fd:
135+
# agent -> terminal: strip queries, inject full answers
136+
clean, answers = strip_and_collect(data)
137+
if clean:
138+
os.write(1, clean)
139+
for a in answers:
140+
os.write(master_fd, a)
141+
else:
142+
os.write(master_fd, data)
143+
144+
if proc.poll() is not None:
145+
# drain remaining output
146+
try:
147+
while True:
148+
r, _, _ = select.select([master_fd], [], [], 0.05)
149+
if not r:
150+
break
151+
data = os.read(master_fd, 65536)
152+
if not data:
153+
break
154+
clean, answers = strip_and_collect(data)
155+
if clean:
156+
os.write(1, clean)
157+
for a in answers:
158+
os.write(master_fd, a)
159+
except OSError:
160+
pass
161+
break
162+
finally:
163+
if old_term is not None:
164+
termios.tcsetattr(0, termios.TCSADRAIN, old_term)
165+
try:
166+
proc.wait(timeout=2)
167+
except Exception:
168+
proc.kill()
169+
170+
sys.exit(proc.returncode or 0)
171+
172+
173+
if __name__ == "__main__":
174+
main()

‎src/cli.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,12 +485,18 @@ const COMMANDS: Command[] = [
485485
const scratch = join(process.cwd(), ".bandit", "tmp");
486486
mkdirSync(scratch, { recursive: true });
487487
const venvPrefix = cfg.venvPrefix ?? "uv venv if missing; never install globally; use project venv/bin + local package managers (uv/bun)";
488-
// Guard against the fresh-split pane race: opencode/OpenTUI crashes
489-
// (EXC_BREAKPOINT in bufferDrawTextBufferView) when the TUI draws
490-
// before the pty has real dimensions. Wait for a nonzero size via a
491-
// pty-size poll before exec'ing the agent, then set LINES/COLUMNS.
488+
// Guard against two boot killers: (1) the fresh-split pane race
489+
// (TUI draws before the pty has dimensions) and (2) opencode's
490+
// capability-handshake crash in herdr panes (sst/opencode#41483 —
491+
// herdr answers everything except OSC 10/11 → deterministic
492+
// EXC_BREAKPOINT in bufferDrawTextBufferView). Fix (1) with a stty
493+
// size poll; fix (2) by launching through the handshake-answer pty
494+
// wrapper (bin/answer-handshake.py) for opencode.
492495
const sizeGuard = `while true; do C=$(stty size 2>/dev/null | cut -d" " -f2); L=$(stty size 2>/dev/null | cut -d" " -f1); [ -n "$C" ] && [ "$C" -gt 2 ] && break; sleep 0.3; done; export LINES=$L COLUMNS=$C`;
493-
const launch = `cd ${JSON.stringify(process.cwd())} && mkdir -p .bandit/tmp && export TMPDIR=${JSON.stringify(scratch)} && ${sizeGuard} && ${tuiCommand} ${argStr}`.trim();
496+
const wrapper = tuiCommand === "opencode"
497+
? "python3 " + JSON.stringify(join(import.meta.dir, "answer-handshake.py"))
498+
: "";
499+
const launch = `cd ${JSON.stringify(process.cwd())} && mkdir -p .bandit/tmp && export TMPDIR=${JSON.stringify(scratch)} && ${sizeGuard} && ${wrapper ? wrapper + " " : ""}${tuiCommand} ${argStr}`.trim();
494500
// Boot with one retry: a flaky boot (TUI crash on fresh pane) gets a
495501
// second chance before we give up — the pane shell survives the crash.
496502
let alive = false;

‎typescript‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Script started on Sun Sep 20 21:21:12 2026
2+
^D% wrill@Thomass-MacBook-Air bandit % [?2004h[?2004l
3+
4+
Script done on Sun Sep 20 21:21:12 2026

0 commit comments

Comments
 (0)