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.