diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 594727e7d..fec67305f 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -121,7 +121,11 @@ yes only when the bridge sets `MAGPIE_CLAUDE_TUI_TRUST=1`, which it does for magpie's own working folder or a saved account's config directory, both magpie's. In the caller's folder under the account Claude Code is signed in to, the question is the user's: a folder the user has not -trusted makes that run `-p`. +trusted makes that run `-p`. The question opens on "No, exit", and a key +sent as it is first drawn can be lost to its being drawn again, so the +helper presses ↓ until "Yes" is the last choice drawn and Enter only once +it has stayed so for 300ms (`ready`, `trustSettle`). Pressing Enter blind +chose "No, exit" in the real gateway, and every run went `-p`. A run is `-p`, as before, when (`claudeTUI`): diff --git a/internal/claudetui/tui.go b/internal/claudetui/tui.go index a9d5cde2c..1b3791637 100644 --- a/internal/claudetui/tui.go +++ b/internal/claudetui/tui.go @@ -280,8 +280,19 @@ func (s *shim) lastScreen() string { var ( trustNo = regexp.MustCompile(`❯\s*No,\s*exit`) trustYes = regexp.MustCompile(`❯\s*Yes,\s*I trust`) + // how long a Yes stays drawn before Enter is pressed on it + trustSettle = 300 * time.Millisecond ) +// lastAt is where re's last match in p starts, -1 with none. +func lastAt(re *regexp.Regexp, p string) int { + m := re.FindAllStringIndex(p, -1) + if len(m) == 0 { + return -1 + } + return m[len(m)-1][0] +} + var errUntrusted = errors.New("Claude Code asks whether to trust the folder, which only its user answers") // ready waits for Claude Code's prompt. Asked whether to trust the folder @@ -289,27 +300,37 @@ var errUntrusted = errors.New("Claude Code asks whether to trust the folder, whi func (s *shim) ready() error { deadline := time.After(readyWait) answered := 0 + var yesAt, downAt time.Time for { p := s.plain() switch { case strings.Contains(p, "trust this folder") && os.Getenv(TrustEnv) != "1": return errUntrusted case strings.Contains(p, "trust this folder") && answered < 3: - keys := "" + // the choice is the last one drawn after ❯, "No, exit" at + // first. A key sent as the question is first drawn can be lost + // to its being drawn again on No, so Enter goes only to a Yes + // drawn trustSettle ago and not drawn over since. + no, yes := lastAt(trustNo, p), lastAt(trustYes, p) switch { - case trustNo.MatchString(p): - keys = "\x1b[B" - case trustYes.MatchString(p): - default: - goto wait + case yes > no: + if yesAt.IsZero() { + yesAt = time.Now() + } + if time.Since(yesAt) < trustSettle { + break + } + answered++ + yesAt = time.Time{} + s.clearScreen() + _, _ = s.pty.WriteString("\r") + case no >= 0: + yesAt = time.Time{} + if time.Since(downAt) >= time.Second { + downAt = time.Now() + _, _ = s.pty.WriteString("\x1b[B") + } } - answered++ - s.clearScreen() - if keys != "" { - _, _ = s.pty.WriteString(keys) - time.Sleep(300 * time.Millisecond) - } - _, _ = s.pty.WriteString("\r") case strings.Contains(p, "keep browser tools off"): // Claude in Chrome found: kept off, as the bridge runs it // --no-chrome @@ -323,7 +344,6 @@ func (s *shim) ready() error { time.Sleep(300 * time.Millisecond) return nil } - wait: select { case <-s.changed: case <-time.After(200 * time.Millisecond): diff --git a/internal/claudetui/tui_test.go b/internal/claudetui/tui_test.go index 60bb7e96a..b11973f76 100644 --- a/internal/claudetui/tui_test.go +++ b/internal/claudetui/tui_test.go @@ -12,6 +12,7 @@ import ( "path/filepath" "slices" "strings" + "sync" "testing" "time" @@ -37,6 +38,51 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } +// fakeTrust asks whether to trust the folder as Claude Code 2.1.314 does: +// its words placed by cursor moves, "No, exit" chosen, and drawn again on +// No once it listens, the keys before that lost. Enter on No exits. +func fakeTrust(r *bufio.Reader) { + var mu sync.Mutex + yes, deaf := false, true + draw := func() { + marks := map[bool]string{true: "\x1b[38;2;177;185;249m❯\x1b[39m", false: " "} + fmt.Printf("\x1b[2GDo\x1b[5Gyou\x1b[9Gtrust\x1b[15Gthis\x1b[20Gfolder?\r\n\x1b[2G%s\x1b[4GNo,\x1b[8Gexit\r\n\x1b[2G%s\x1b[4GYes,\x1b[9GI\x1b[11Gtrust\x1b[17Gthis\x1b[22Gfolder\r\n", marks[!yes], marks[yes]) + } + draw() + time.AfterFunc(150*time.Millisecond, func() { + mu.Lock() + defer mu.Unlock() + deaf, yes = false, false + draw() + }) + for { + c, err := r.ReadByte() + if err != nil { + os.Exit(0) + } + if c == 0x1b { + seq := make([]byte, 2) + if _, err := io.ReadFull(r, seq); err != nil { + os.Exit(0) + } + c = seq[1] + } + mu.Lock() + switch { + case deaf: + case c == 'B' || c == 'A': + yes = c == 'B' + draw() + case c == '\r' && !yes: + os.Exit(1) + case c == '\r': + mu.Unlock() + return + } + mu.Unlock() + } +} + func fakeClaude(args []string) { if slices.Contains(args, "-p") { sc := bufio.NewScanner(os.Stdin) @@ -55,15 +101,6 @@ func fakeClaude(args []string) { } dir := filepath.Join(os.Getenv("CLAUDE_CONFIG_DIR"), "projects", "work") _ = os.MkdirAll(dir, 0o700) - if os.Getenv("FAKE_TRUST") == "1" { - fmt.Print("Do you trust this folder?\r\n ❯ Yes, I trust this folder\r\n No, exit\r\n") - r := bufio.NewReader(os.Stdin) - if _, err := r.ReadString('\n'); err != nil { - return - } - } - // what it was started with, for the test to read - _ = os.WriteFile(filepath.Join(os.Getenv("CLAUDE_CONFIG_DIR"), "args"), []byte(strings.Join(args, "\n")), 0o600) // raw, as Claude Code's terminal is: a paste is taken whole, and Enter // outside one sends it if tio, err := unix.IoctlGetTermios(0, ioctlGetTermios); err == nil { @@ -71,8 +108,13 @@ func fakeClaude(args []string) { tio.Iflag &^= unix.ICRNL _ = unix.IoctlSetTermios(0, ioctlSetTermios, tio) } - fmt.Print("\r\n❯ ") r := bufio.NewReader(os.Stdin) + if os.Getenv("FAKE_TRUST") == "1" { + fakeTrust(r) + } + // what it was started with, for the test to read + _ = os.WriteFile(filepath.Join(os.Getenv("CLAUDE_CONFIG_DIR"), "args"), []byte(strings.Join(args, "\n")), 0o600) + fmt.Print("\r\n❯ ") var typed strings.Builder pasting := false for {