Files
magpie/internal/claudetui/wslread.go
T
yetone 14e3513780 claude: Claude Code in WSL runs interactively when Windows can't read its transcripts
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 ./... .
2026-11-02 00:12:40 +08:00

147 lines
3.9 KiB
Go

package claudetui
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/yetone/magpie/internal/proc"
)
// A Claude Code in WSL whose transcripts Windows can't read (the distro
// user's own folder, through \\wsl$, which a session with no 9P
// redirector, one over ssh, has none of), or whose home the probe didn't
// find, has its transcript read inside the distro: a shell there, run
// through wsl.exe, waits for the session's file and tails it, and the
// helper copies what it prints to a file of its own, which it reads as
// it reads any transcript. The shell finds the folder as Claude Code
// does, under its own $HOME.
// wslReadScript is the shell's script: $1 is the session id. It prints
// "ready" once it knows how much of the file is there before Claude Code
// starts (a resumed session's earlier turns), then whatever is written
// after, until its standard input ends.
const wslReadScript = `d="$HOME/.claude/projects"; s="$1"; n=0
for f in "$d"/*/"$s".jsonl; do [ -f "$f" ] && n=$(wc -c <"$f"); done
echo ready
( while :; do for f in "$d"/*/"$s".jsonl; do [ -f "$f" ] && exec tail -c +$((n+1)) -F "$f" 2>/dev/null; done; sleep 0.1; done ) &
cat >/dev/null; kill $! 2>/dev/null`
// wslReadCommand is the shell's command; a var for tests, which run the
// script here.
var wslReadCommand = func(distro, sid string) *exec.Cmd {
return proc.Command("wsl.exe", "-d", distro, "--exec", "/bin/sh", "-c", wslReadScript, "sh", sid)
}
// how long the shell has to say it is ready
var wslReadWait = 30 * time.Second
var sidRe = regexp.MustCompile(`^[0-9A-Za-z-]+$`)
// wslRead is a transcript read inside the distro, copied to path.
type wslRead struct {
path string
cmd *exec.Cmd
in io.WriteCloser
done chan struct{}
}
// startWSLRead starts reading session sid's transcript inside distro,
// before Claude Code starts, into a file under folder projects as a
// projects folder has it (<projects>/<folder>/<sid>.jsonl).
func startWSLRead(distro, sid, projects string) (*wslRead, error) {
if !sidRe.MatchString(sid) {
return nil, fmt.Errorf("a session id %q", sid)
}
if err := os.MkdirAll(filepath.Join(projects, "wsl"), 0o700); err != nil {
return nil, err
}
f, err := os.Create(filepath.Join(projects, "wsl", sid+".jsonl"))
if err != nil {
return nil, err
}
cmd := wslReadCommand(distro, sid)
in, err := cmd.StdinPipe()
if err != nil {
f.Close()
return nil, err
}
out, err := cmd.StdoutPipe()
if err != nil {
f.Close()
return nil, err
}
if err := cmd.Start(); err != nil {
f.Close()
return nil, err
}
r := &wslRead{path: f.Name(), cmd: cmd, in: in, done: make(chan struct{})}
rd := bufio.NewReader(out)
ready := make(chan error, 1)
go func() {
line, err := rd.ReadString('\n')
if err == nil && strings.TrimSpace(line) != "ready" {
err = fmt.Errorf("it said %q", line)
}
ready <- err
if err == nil {
_, _ = io.Copy(f, rd)
}
f.Close()
close(r.done)
}()
select {
case err = <-ready:
case <-time.After(wslReadWait):
err = errors.New("it didn't start")
}
if err != nil {
r.stop()
return nil, fmt.Errorf("the transcript can't be read in WSL: %v", err)
}
return r, nil
}
// settle waits, once Claude Code has ended, for the last of what it
// wrote: until the copy hasn't grown for a moment.
func (r *wslRead) settle() {
if r == nil {
return
}
var size int64 = -1
for until := time.Now().Add(3 * time.Second); time.Now().Before(until); {
fi, err := os.Stat(r.path)
if err != nil || fi.Size() == size {
return
}
size = fi.Size()
select {
case <-r.done:
return
case <-time.After(300 * time.Millisecond):
}
}
}
// stop ends the shell: its standard input ends, and it ends its tail.
func (r *wslRead) stop() {
if r == nil {
return
}
_ = r.in.Close()
select {
case <-r.done:
case <-time.After(5 * time.Second):
_ = r.cmd.Process.Kill()
<-r.done
}
_ = r.cmd.Wait()
}