claudetui: use a conpty folder beside the executable before Windows' own ConPTY

Windows 10's conhost (10.0.19041) drops bracketed paste marks, so Claude Code
takes a long message as many pastes and submits only part of it (#5205).
Windows Terminal's conpty.dll with its OpenConsole.exe keeps them. When
<exe dir>/conpty holds both, CreatePseudoConsole and ClosePseudoConsole come
from it; otherwise from kernel32 as before, so nothing changes for anyone who
does not add the folder.

Landed from @ggbdpq's verification on #5205. It's opt-in: without
<exe dir>\conpty\{conpty.dll,OpenConsole.exe}, both calls still come
from kernel32. The red/green evidence is the thread's: two Windows 10
machines (19044, 19045) with Terminal's ConPTY 1.24/1.25 passed 1 to
217 KB pastes intact, and lost them with conhost 10.0.19041.

Run on xiaomi-gpu (Windows 11, no conpty folder, so the kernel32 path):
go vet ./internal/claudetui ok, go build -tags nogui ok, go test
./internal/claudetui: every test passes except
TestShimFirstMessageWaitsForTools, which fails the same way at
origin/main d987cc7d5 (-count=2, "the shim wrote 0 lines of 3 in
30s") and is #4810.
Not tried: Windows 10, and the conpty folder on this machine. Shipping
the pair with magpie so Windows 10 users need no setup is left for a
later change.
This commit is contained in:
SkyJiashu
2026-11-14 19:46:12 +08:00
committed by yetone
parent 8769a5819a
commit a49f3745eb
+41 -7
View File
@@ -3,6 +3,7 @@ package claudetui
import (
"fmt"
"os"
"path/filepath"
"sync"
"syscall"
"time"
@@ -27,9 +28,42 @@ type child struct {
// remove (dropLeft).
var endSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}
// pseudoConsole is where CreatePseudoConsole and ClosePseudoConsole come
// from. Windows 10's own (conhost 10.0.19041) drops a paste's bracketed
// paste marks, so Claude Code takes a long message as many pastes and
// submits a part of it (yetone/magpie#5205); the ConPTY Windows Terminal
// ships (conpty.dll, which runs the OpenConsole.exe beside it) keeps them.
// dsh-cross: a conpty folder beside magpie's executable holding both is
// used first; without it, the system's.
type pseudoConsole struct{ create, close *windows.LazyProc }
var console = sync.OnceValue(func() pseudoConsole {
if exe, err := os.Executable(); err == nil {
dir := filepath.Join(filepath.Dir(exe), "conpty")
if _, err := os.Stat(filepath.Join(dir, "OpenConsole.exe")); err == nil {
dll := windows.NewLazyDLL(filepath.Join(dir, "conpty.dll"))
if dll.NewProc("CreatePseudoConsole").Find() == nil && dll.NewProc("ClosePseudoConsole").Find() == nil {
return pseudoConsole{dll.NewProc("CreatePseudoConsole"), dll.NewProc("ClosePseudoConsole")}
}
}
}
k := windows.NewLazySystemDLL("kernel32.dll")
return pseudoConsole{k.NewProc("CreatePseudoConsole"), k.NewProc("ClosePseudoConsole")}
})
func createPseudoConsole(size windows.Coord, in, out windows.Handle, h *windows.Handle) error {
r, _, _ := console().create.Call(uintptr(*(*uint32)(unsafe.Pointer(&size))), uintptr(in), uintptr(out), 0, uintptr(unsafe.Pointer(h)))
if r != 0 {
return syscall.Errno(r)
}
return nil
}
func closePseudoConsole(h windows.Handle) { _, _, _ = console().close.Call(uintptr(h)) }
func (s *shim) start(path string, args []string) error {
// Windows 10 before 1809 has none, and x/sys panics calling it there
if err := windows.NewLazySystemDLL("kernel32.dll").NewProc("CreatePseudoConsole").Find(); err != nil {
if err := console().create.Find(); err != nil {
return fmt.Errorf("no pseudo console on this Windows: %w", err)
}
// a .cmd (npm's shim) is run by cmd.exe, its arguments quoted as
@@ -53,9 +87,9 @@ func (s *shim) start(path string, args []string) error {
windows.CloseHandle(inW)
return err
}
var console windows.Handle
var pc windows.Handle
// wide, so a line of Claude Code's screen is a line
err := windows.CreatePseudoConsole(windows.Coord{X: screenCols, Y: screenRows}, inR, outW, 0, &console)
err := createPseudoConsole(windows.Coord{X: screenCols, Y: screenRows}, inR, outW, &pc)
// the console has its own of these
windows.CloseHandle(inR)
windows.CloseHandle(outW)
@@ -65,15 +99,15 @@ func (s *shim) start(path string, args []string) error {
out.Close()
return fmt.Errorf("a pseudo console: %w", err)
}
h, err := startIn(console, cmd.Path, line, childEnv(s.env...))
h, err := startIn(pc, cmd.Path, line, childEnv(s.env...))
if err != nil {
windows.ClosePseudoConsole(console)
closePseudoConsole(pc)
in.Close()
out.Close()
return err
}
s.pty = conpty{in: in, out: out}
s.proc, s.console = h, console
s.proc, s.console = h, pc
go func() {
_, _ = windows.WaitForSingleObject(h, windows.INFINITE)
var code uint32
@@ -153,7 +187,7 @@ func (c conpty) Write(p []byte) (int, error) { return c.in.Write(p) }
// Windows 11 24H2, ClosePseudoConsole waits for its output to be read.
func (s *shim) closeConsole() {
s.closeOnce.Do(func() {
go windows.ClosePseudoConsole(s.console)
go closePseudoConsole(s.console)
})
}