The shim closed `exited` as soon as cmd.Wait returned, which can be
before the PTY reader has read what Claude Code wrote just before it
ended. Every "Claude Code ended ..." error then carried an empty screen
in place of the reason it gave, which is what TestShimNeverPrints/
failed_start saw on a loaded ubuntu CI runner (run 41899504898):
"ended before it showed its prompt (exit status 3): " with no
"something went wrong". The shim's own stderr line is meant; the test
checks the reason is in it.
Now the exit goes through shim.ended: on Unix it waits for read to end
(the master reads EIO once the last slave writer is gone), on Windows,
whose pseudo console stays open, until reading has gone quiet for 250ms
after its first bytes; two seconds at most either way, for a terminal a
child of Claude Code keeps open.
Run:
- Red before: with a 200ms sleep injected at the top of read(), the old
code failed failed_start 5 of 5 on Linux (wsl-run, magpie-desktop)
with CI's exact message; with the fix and the same injection, 50 of 50
green. Without injection the old code passed 300 of 300 there (an
idle machine never loses the race).
- New TestShimEndedAfterLastWordsRead (a reader given the last words
300ms late, terminal ending and staying open). Broken by hand (ended
records at once): red on both subtests; restored: green 20 of 20 with
TestShimNeverPrints on Linux.
- internal/claudetui: Linux (wsl-run) -count=3 ok; macOS (mote-run)
-race -count=2 ok; Windows (win-run) -count=2: only
TestShimFirstMessageWaitsForTools red, the same at origin/main
49e382735 without this change (#4810, commented).
- go vet ./internal/claudetui with GOOS darwin, linux, windows (mote).
- gofmt -l clean.
Not tried: a real Claude Code ending before its prompt (only the fake);
the Windows quiet rule on a really slow ConPTY reader.
172 lines
5.1 KiB
Go
172 lines
5.1 KiB
Go
package claudetui
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"sync"
|
|
"syscall"
|
|
"time"
|
|
"unsafe"
|
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
"github.com/yetone/magpie/internal/proc"
|
|
)
|
|
|
|
// child is Claude Code, started in a pseudo console (ConPTY): Windows' own
|
|
// terminal for a program that draws, as Windows Terminal runs one in.
|
|
type child struct {
|
|
proc windows.Handle
|
|
console windows.Handle
|
|
closeOnce sync.Once
|
|
}
|
|
|
|
// endSignals are what the helper ends on, its Claude Code with it: Ctrl+C,
|
|
// and its console closed. The gateway ends a run by killing its job, which
|
|
// no handler hears: the images folder is then left for another helper to
|
|
// remove (dropLeft).
|
|
var endSignals = []os.Signal{os.Interrupt, syscall.SIGTERM}
|
|
|
|
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 {
|
|
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
|
|
// Claude Code is run with elsewhere (proc)
|
|
cmd := proc.Command(path, args...)
|
|
if cmd.Err != nil {
|
|
return cmd.Err
|
|
}
|
|
line := windows.ComposeCommandLine(cmd.Args)
|
|
if cmd.SysProcAttr != nil && cmd.SysProcAttr.CmdLine != "" {
|
|
line = cmd.SysProcAttr.CmdLine
|
|
}
|
|
// what is typed goes in at inW, what Claude Code draws comes out at
|
|
// outR; the console holds the other ends
|
|
var inR, inW, outR, outW windows.Handle
|
|
if err := windows.CreatePipe(&inR, &inW, nil, 0); err != nil {
|
|
return err
|
|
}
|
|
if err := windows.CreatePipe(&outR, &outW, nil, 0); err != nil {
|
|
windows.CloseHandle(inR)
|
|
windows.CloseHandle(inW)
|
|
return err
|
|
}
|
|
var console 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)
|
|
// the console has its own of these
|
|
windows.CloseHandle(inR)
|
|
windows.CloseHandle(outW)
|
|
in, out := os.NewFile(uintptr(inW), "conpty-in"), os.NewFile(uintptr(outR), "conpty-out")
|
|
if err != nil {
|
|
in.Close()
|
|
out.Close()
|
|
return fmt.Errorf("a pseudo console: %w", err)
|
|
}
|
|
h, err := startIn(console, cmd.Path, line, childEnv(s.env...))
|
|
if err != nil {
|
|
windows.ClosePseudoConsole(console)
|
|
in.Close()
|
|
out.Close()
|
|
return err
|
|
}
|
|
s.pty = conpty{in: in, out: out}
|
|
s.proc, s.console = h, console
|
|
go func() {
|
|
_, _ = windows.WaitForSingleObject(h, windows.INFINITE)
|
|
var code uint32
|
|
var exit error
|
|
if err := windows.GetExitCodeProcess(h, &code); err != nil {
|
|
exit = err
|
|
} else if code != 0 {
|
|
exit = fmt.Errorf("exit status %d", code)
|
|
}
|
|
s.ended(exit, true)
|
|
}()
|
|
go s.read()
|
|
return nil
|
|
}
|
|
|
|
// startIn starts the program at app, with the command line line and the
|
|
// environment env, attached to console.
|
|
func startIn(console windows.Handle, app, line string, env []string) (windows.Handle, error) {
|
|
attrs, err := windows.NewProcThreadAttributeList(1)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer attrs.Delete()
|
|
// the attribute's value is the console's handle itself
|
|
if err := attrs.Update(windows.PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, *(*unsafe.Pointer)(unsafe.Pointer(&console)), unsafe.Sizeof(console)); err != nil {
|
|
return 0, err
|
|
}
|
|
si := &windows.StartupInfoEx{ProcThreadAttributeList: attrs.List()}
|
|
si.Cb = uint32(unsafe.Sizeof(*si))
|
|
// standard handles of none: without this, the helper's own (the
|
|
// bridge's pipes) are Claude Code's, and it draws into them rather
|
|
// than the console (microsoft/terminal#11276)
|
|
si.Flags = windows.STARTF_USESTDHANDLES
|
|
appW, err := windows.UTF16PtrFromString(app)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
lineW, err := windows.UTF16PtrFromString(line)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
block, err := envBlock(env)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
var pi windows.ProcessInformation
|
|
if err := windows.CreateProcess(appW, lineW, nil, nil, false,
|
|
windows.EXTENDED_STARTUPINFO_PRESENT|windows.CREATE_UNICODE_ENVIRONMENT, block, nil, &si.StartupInfo, &pi); err != nil {
|
|
return 0, err
|
|
}
|
|
windows.CloseHandle(pi.Thread)
|
|
return pi.Process, nil
|
|
}
|
|
|
|
// envBlock is env as CreateProcess takes it: each NUL-ended, then a NUL.
|
|
func envBlock(env []string) (*uint16, error) {
|
|
var b []uint16
|
|
for _, e := range env {
|
|
u, err := windows.UTF16FromString(e)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
b = append(b, u...)
|
|
}
|
|
b = append(b, 0)
|
|
return &b[0], nil
|
|
}
|
|
|
|
// conpty is a pseudo console's two ends as one terminal.
|
|
type conpty struct{ in, out *os.File }
|
|
|
|
func (c conpty) Read(p []byte) (int, error) { return c.out.Read(p) }
|
|
func (c conpty) Write(p []byte) (int, error) { return c.in.Write(p) }
|
|
|
|
// closeConsole closes Claude Code's console, which tells it so, as closing
|
|
// a terminal window does (CTRL_CLOSE_EVENT). It returns at once: before
|
|
// Windows 11 24H2, ClosePseudoConsole waits for its output to be read.
|
|
func (s *shim) closeConsole() {
|
|
s.closeOnce.Do(func() {
|
|
go windows.ClosePseudoConsole(s.console)
|
|
})
|
|
}
|
|
|
|
// stop ends Claude Code: its console closed, then killed.
|
|
func (s *shim) stop() {
|
|
if s.proc == 0 {
|
|
return
|
|
}
|
|
s.closeConsole()
|
|
select {
|
|
case <-s.exited:
|
|
case <-time.After(3 * time.Second):
|
|
_ = windows.TerminateProcess(s.proc, 1)
|
|
}
|
|
}
|