claudetui: the folder-trust question is answered Yes only once Yes is drawn
The real-account end-to-end through a running gateway found every interactive run going to -p. The gateway's working folder (magpie-claude-work-<uid>) wasn't yet trusted under the account's config dir, so Claude Code 2.1.314 asked whether to trust it, with "No, exit" chosen. The helper pressed down and then, 300ms later, Enter without looking. Its down was lost when sent as the question was first drawn (the question is drawn again on No), Enter chose "No, exit", and Claude Code ended with status 1 (screen: "B"). The start failure marked the interactive mode broken for 30 minutes and every run went to -p, so v0.1.2266's default did nothing on a fresh account. ready() now reads the choice as the last of "❯ No, exit" / "❯ Yes, I trust" drawn, presses down (at most once a second) while No is chosen, and Enter only on a Yes that stayed drawn for 300ms (trustSettle). The test's fake asks as the real one does: words placed by cursor moves, No chosen first, keys in its first 150ms lost, and drawn again on No; Enter on No exits 1. Run: - By hand: real Claude Code 2.1.314 in a PTY in the gateway's folder, sandbox HOME with a real account's config dir: the question opens on No; a down sent at once is undone by the redraw, one sent 0.3s or 1.5s in moves to Yes. - mote: go test -count=3 -run TestShim ./internal/claudetui ok; go test ./internal/claudetui ok; go vet ./internal/claudetui on darwin, linux, windows; gofmt -l clean. - Real account, sandbox HOME, a gateway running this build (and the next commit's), haiku: a tool call, two calls in one message, words then a call: all answered, every transcript row "entrypoint":"cli", no broken mark. Broken by hand: the old ready() with the new fake: TestShimTrustsWhenTold red, "Claude Code ended before it showed its prompt (exit status 1)", the real failure. Not tried: Linux with a real Claude Code; a Claude Code version whose question words differ (it then waits out 45s and goes -p, marked).
This commit is contained in:
@@ -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`):
|
||||
|
||||
|
||||
+34
-14
@@ -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):
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user