diff --git a/internal/claudetui/child_windows.go b/internal/claudetui/child_windows.go index 216036d58..55a94773d 100644 --- a/internal/claudetui/child_windows.go +++ b/internal/claudetui/child_windows.go @@ -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) }) }