With the interactive mode on, Claude Code in WSL was refused with a 502
in two cases: the distro user's own folder unreadable from Windows
(\\wsl$ needs the 9P redirector, which a session over ssh hasn't;
"wslread") and a distro whose home the probe didn't find ("wsl-home").
Both now run interactively: the helper reads the transcript inside the
distro.
- claudetui/wslread.go: before Claude Code starts, a shell run through
wsl.exe notes how much of $HOME/.claude/projects/*/<sid>.jsonl is
there (a resumed session's earlier turns), says "ready", then tail -F's
the rest; the helper copies it to a file of its own and tails that as
any transcript. When Claude Code ends it waits for the copy to stop
growing (settle), then closes the shell's stdin, which ends it.
- claudetui.WSL gets Distro (Projects omitempty); the gateway's tuiWSL
sets it when Windows can't read the folder or the home is unknown.
- the wslread and wsl-home refusals are gone (claudeTUI, claudeWhy,
claudeNotTUIWhy); the GUI's "wsl-home" reason and its 4 strings too.
- docs/subsystems/claude-subscription-bridge.md says how it is read.
Run, at origin/main ff65ebd14 plus this change (the reader through real
wsl.exe and the break-by-hand at 546cdbde6):
- WSL (magpie-desktop, go1.26.8): go vet -tags nogui, GOOS=windows vet,
GOOS=darwin CGO_ENABLED=0 vet of claudetui ok; go test ./internal/claudetui
ok; go test -run 'Claude|WSL' ./internal/gateway ok (51s);
claude-mode.test.cjs chromium + webkit 44/44; i18n-keys 4/4.
- Windows 11 (xiaomi-gpu): go vet ok; go test ./internal/claudetui ok
but TestShimFirstMessageWaitsForTools, red the same at main 2739626c3
(bee3ea675's test, #4810);
go test -run 'Claude|WSL' ./internal/gateway ok with
TestClaudeAsRootEnv skipped: it is red the same at main 546cdbde6 on
Windows (#2425, a root test where magpie is never root), at 546cdbde6.
- the reader through the real wsl.exe into magpie-desktop (a temporary
test, not committed): ready in 93ms, the line already in the file left
out, two lines written after copied, stopped in 9ms, no tail left in
the distro.
Broken by hand: tuiWSL not setting Distro: TestClaudeTUIWSL red ("its
own folder, unread": Distro empty).
Not tried: a real Claude Code turn in WSL over ssh (real runs stay on
this Mac); mote (its disk is full); the full go test ./... .
126 lines
3.9 KiB
Go
126 lines
3.9 KiB
Go
package claudetui
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// wslHere runs the in-distro reader's script with this machine's sh, its
|
|
// $HOME the test's.
|
|
func wslHere(t *testing.T) string {
|
|
t.Helper()
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("the script is the distro's sh's")
|
|
}
|
|
home := t.TempDir()
|
|
old := wslReadCommand
|
|
wslReadCommand = func(distro, sid string) *exec.Cmd {
|
|
if distro != "Ubuntu" {
|
|
t.Errorf("distro %q", distro)
|
|
}
|
|
cmd := exec.Command("/bin/sh", "-c", wslReadScript, "sh", sid)
|
|
cmd.Env = append(os.Environ(), "HOME="+home)
|
|
return cmd
|
|
}
|
|
t.Cleanup(func() { wslReadCommand = old })
|
|
return filepath.Join(home, ".claude", "projects", "-work")
|
|
}
|
|
|
|
// waitFor waits until file path holds want.
|
|
func waitFor(t *testing.T, path, want string) {
|
|
t.Helper()
|
|
var got []byte
|
|
for until := time.Now().Add(10 * time.Second); time.Now().Before(until); time.Sleep(20 * time.Millisecond) {
|
|
if got, _ = os.ReadFile(path); string(got) == want {
|
|
return
|
|
}
|
|
}
|
|
t.Fatalf("%s holds %q, want %q", path, got, want)
|
|
}
|
|
|
|
func appendTo(t *testing.T, path, s string) {
|
|
t.Helper()
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer f.Close()
|
|
if _, err := f.WriteString(s); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// A transcript Windows can't read is read inside the distro: a resumed
|
|
// session's earlier turns are left out, what is written after is copied
|
|
// as it is written, and the shell ends with the run.
|
|
func TestWSLReadResumed(t *testing.T) {
|
|
dir := wslHere(t)
|
|
if err := os.MkdirAll(dir, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sid := "0f3c-1"
|
|
file := filepath.Join(dir, sid+".jsonl")
|
|
appendTo(t, file, `{"earlier":1}`+"\n")
|
|
r, err := startWSLRead("Ubuntu", sid, t.TempDir())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if want := filepath.Join(filepath.Dir(filepath.Dir(r.path)), "wsl", sid+".jsonl"); r.path != want {
|
|
t.Fatalf("copied to %s, not as a projects folder has it", r.path)
|
|
}
|
|
appendTo(t, file, `{"later":1}`+"\n")
|
|
waitFor(t, r.path, `{"later":1}`+"\n")
|
|
appendTo(t, file, `{"later":2}`+"\n")
|
|
waitFor(t, r.path, `{"later":1}`+"\n"+`{"later":2}`+"\n")
|
|
done := make(chan struct{})
|
|
go func() { r.stop(); close(done) }()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("the shell didn't end with the run")
|
|
}
|
|
if r.cmd.ProcessState == nil || !r.cmd.ProcessState.Exited() {
|
|
t.Fatalf("the shell: %v", r.cmd.ProcessState)
|
|
}
|
|
}
|
|
|
|
// A new session's file, written after Claude Code starts, is read whole.
|
|
func TestWSLReadNewSession(t *testing.T) {
|
|
dir := wslHere(t)
|
|
r, err := startWSLRead("Ubuntu", "abc", t.TempDir())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(r.stop)
|
|
if err := os.MkdirAll(dir, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
appendTo(t, filepath.Join(dir, "abc.jsonl"), "one\ntwo\n")
|
|
waitFor(t, r.path, "one\ntwo\n")
|
|
}
|
|
|
|
// A session id is the bridge's, but never reaches the shell as anything
|
|
// but a word; a shell that never says it is ready fails the run.
|
|
func TestWSLReadRefuses(t *testing.T) {
|
|
wslHere(t)
|
|
if _, err := startWSLRead("Ubuntu", "x; rm -rf ~", t.TempDir()); err == nil {
|
|
t.Fatal("an id with a shell's words in it")
|
|
}
|
|
old, oldWait := wslReadCommand, wslReadWait
|
|
wslReadWait = 200 * time.Millisecond
|
|
wslReadCommand = func(string, string) *exec.Cmd { return exec.Command("/bin/sh", "-c", "echo nope; cat >/dev/null") }
|
|
t.Cleanup(func() { wslReadCommand, wslReadWait = old, oldWait })
|
|
if _, err := startWSLRead("Ubuntu", "abc", t.TempDir()); err == nil || !strings.Contains(err.Error(), "can't be read in WSL") {
|
|
t.Fatalf("a shell that said nope: %v", err)
|
|
}
|
|
wslReadCommand = func(string, string) *exec.Cmd { return exec.Command("/bin/sh", "-c", "cat >/dev/null") }
|
|
if _, err := startWSLRead("Ubuntu", "abc", t.TempDir()); err == nil || !strings.Contains(err.Error(), "didn't start") {
|
|
t.Fatalf("a shell that said nothing: %v", err)
|
|
}
|
|
}
|