diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 9b20507d3..760c7dd2a 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -246,8 +246,15 @@ Code asks whether to trust a folder it has not run in; the helper answers 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 fails that run, saying so. The question opens on "No, exit", and a key +signed in to, the question is the user's: the helper ends there, saying +so (`claudetui.Untrusted`), and the turn goes in magpie's own folder, +told the caller's as any run whose folder isn't here is (`envElsewhere`; +`startTrusted`). That folder is passed over for 10 minutes +(`passedOver`, `untrustedFor`), then tried again, the user having maybe +trusted it since. Until 11-01 such a run became `-p`; from then until +this change it was a 502, which a Claude Code client (paseo) asked again +with backoff, so the chat hung (lwjef on X). +`TestClaudeTUIUntrustedFolderAnswered` holds it. 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 diff --git a/internal/claudetui/command.go b/internal/claudetui/command.go index 80c400c86..04a053c7d 100644 --- a/internal/claudetui/command.go +++ b/internal/claudetui/command.go @@ -10,6 +10,11 @@ const Command = "claude-tui-helper" // answer in their own Claude Code, and the run ends, saying so. const TrustEnv = "MAGPIE_CLAUDE_TUI_TRUST" +// Untrusted begins what the helper says, on its stderr, as it ends over a +// folder it wasn't told to trust: the bridge then runs the turn in a +// folder of magpie's. +const Untrusted = "Claude Code asks whether to trust the folder" + // WSLEnv holds, as JSON (WSL), how to run a Claude Code that is in a WSL // distro: the helper, on Windows, runs wsl.exe in its pseudo console. const WSLEnv = "MAGPIE_CLAUDE_TUI_WSL" diff --git a/internal/claudetui/tui.go b/internal/claudetui/tui.go index ccf9c42d6..4709c39ea 100644 --- a/internal/claudetui/tui.go +++ b/internal/claudetui/tui.go @@ -311,7 +311,7 @@ func lastAt(re *regexp.Regexp, p string) int { return m[len(m)-1][0] } -var errUntrusted = errors.New("Claude Code asks whether to trust the folder, which only its user answers") +var errUntrusted = errors.New(Untrusted + ", which only its user answers") // signIn is what Claude Code's interactive mode shows where it is not // signed in, as -p may be (CLAUDE_CODE_OAUTH_TOKEN, a container whose diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index d084342a9..6d3dbefb7 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -85,6 +85,35 @@ type subscriptionBridge struct { // finds no run lost map[string]lostRun lostCalls map[string]lostRun + // untrusted are callers' folders, by account folder, whose + // interactive Claude Code asked whether to trust them, and when: a + // run for one goes in magpie's own folder until untrustedFor has + // passed (start) + untrusted sync.Map +} + +// untrustedFor is how long a caller's folder Claude Code asked to trust +// is passed over before it is tried again, the user having maybe trusted +// it in their own Claude Code since. +const untrustedFor = 10 * time.Minute + +// tuiUntrusted is a run whose interactive Claude Code, in the caller's +// folder under the user's own account, asked whether to trust it, which +// the helper leaves to the user (claudetui.TrustEnv). +type tuiUntrusted struct { + key, dir string + err error +} + +func (e *tuiUntrusted) Error() string { return e.err.Error() } +func (e *tuiUntrusted) Unwrap() error { return e.err } + +// passedOver is whether a run in dir under configDir's account goes in +// magpie's own folder instead: its Claude Code asked lately whether to +// trust it. +func (b *subscriptionBridge) passedOver(configDir, dir string) bool { + at, ok := b.untrusted.Load(configDir + "\x00" + dir) + return ok && time.Since(at.(time.Time)) < untrustedFor } // A run left for its conversation's next turn waits idleLongest at most, @@ -744,7 +773,7 @@ const claudeMCPPath = "/_magpie/claude-mcp/" // reads its input, and the turn was answered 502 "Claude Code ended" // (FrierenF on Discord). func (b *subscriptionBridge) start(ctx context.Context, req *Request, model, configDir, owner string, from *savedSession) (*subscriptionRun, <-chan Event, error) { - run, events, err := b.startRun(ctx, req, model, configDir, owner, from) + run, events, err := b.startTrusted(ctx, req, model, configDir, owner, from) var noStart *tuiNoStart if errors.As(err, &noStart) { // never tried again as -p (claudeTUIErr) @@ -756,11 +785,30 @@ func (b *subscriptionBridge) start(ctx context.Context, req *Request, model, con if err != nil && (from != nil || req.replayed) && !errors.As(err, &full) && !errors.As(err, &refused) && !errors.As(err, &tui) && ctx.Err() == nil { log.Printf("claude: Claude Code could not go on from the conversation's saved session (%v); a new one is told the whole conversation", err) req.branch, req.told = nil, true - return b.startRun(ctx, req, model, configDir, owner, nil) + return b.startTrusted(ctx, req, model, configDir, owner, nil) } return run, events, err } +// startTrusted is startRun, run again in magpie's own folder when the +// caller's is one the user hasn't trusted in their own Claude Code, which +// only they answer: told the caller's folder (envElsewhere), the turn is +// answered. It was a 502, which a Claude Code client asks again and +// again, so the turn hung (lwjef on X). +func (b *subscriptionBridge) startTrusted(ctx context.Context, req *Request, model, configDir, owner string, from *savedSession) (*subscriptionRun, <-chan Event, error) { + run, events, err := b.startRun(ctx, req, model, configDir, owner, from) + var untrusted *tuiUntrusted + if !errors.As(err, &untrusted) || ctx.Err() != nil { + return run, events, err + } + // passed over from now on (passedOver): this run, and the next, + // aren't in the caller's folder + b.untrusted.Store(untrusted.key, time.Now()) + log.Printf("claude: Claude Code asks whether to trust %s, which its user hasn't; the run goes in magpie's own folder, told the caller's", untrusted.dir) + req.branch = nil + return b.startRun(ctx, req, model, configDir, owner, nil) +} + // startRun is start's Claude Code, from the saved session from when set. func (b *subscriptionBridge) startRun(ctx context.Context, req *Request, model, configDir, owner string, from *savedSession) (*subscriptionRun, <-chan Event, error) { binary, err := claudeBinary() @@ -836,6 +884,9 @@ func (b *subscriptionBridge) startRun(ctx context.Context, req *Request, model, work := "" if binary.wsl == nil { work = clientWorkDir(req) + if work != "" && b.passedOver(configDir, work) { + work = "" + } } client := work != "" if !client { @@ -997,7 +1048,8 @@ func (b *subscriptionBridge) startRun(ctx context.Context, req *Request, model, // a folder of magpie's, or an account folder of magpie's whose // Claude Code no one else runs: asked whether to trust it, the // shim says yes; in the user's own account a caller's folder is - // theirs to answer for, and the run is -p + // theirs to answer for, and the run goes in magpie's + // (startTrusted) env = append(env, claudetui.TrustEnv+"=1") } cmd.Env = binary.env(binary.asRoot(env), configDir) @@ -1090,6 +1142,9 @@ func (b *subscriptionBridge) startRun(ctx context.Context, req *Request, model, } if err := run.cliControlWithin(ctx, map[string]any{"subtype": "initialize"}, wait); err != nil { run.abort() + if tui && client && configDir == "" && run.saidOnEnd(claudetui.Untrusted) { + return nil, nil, &tuiUntrusted{key: configDir + "\x00" + work, dir: work, err: err} + } var unanswered *controlUnanswered if tui && errors.As(err, &unanswered) { // the helper hung: the turn fails (start) @@ -4997,6 +5052,23 @@ func (r *subscriptionRun) whyEnded() error { return r.endedBefore("read its input") } +// saidOnEnd is whether the run, ended, wrote words on its stderr, read to +// its end: its output may end before its stderr is read. +func (r *subscriptionRun) saidOnEnd(words string) bool { + read := make(chan struct{}) + go func() { + r.reading.Wait() + close(read) + }() + select { + case <-read: + case <-time.After(2 * time.Second): + } + r.mu.Lock() + defer r.mu.Unlock() + return strings.Contains(r.stderr.String(), words) +} + // endedBefore is whyEnded for a run that ended before it did what: its // exit status (once Wait has it) and its stderr's tail. func (r *subscriptionRun) endedBefore(what string) error { diff --git a/internal/gateway/claude_tui_start_test.go b/internal/gateway/claude_tui_start_test.go index 88fc7f897..8ef296247 100644 --- a/internal/gateway/claude_tui_start_test.go +++ b/internal/gateway/claude_tui_start_test.go @@ -78,3 +78,32 @@ func TestClaudeTUIHungHelperNeverPrint(t *testing.T) { t.Fatalf("the helper started %d times, want twice: the next turn didn't try the interactive mode", n) } } + +// A caller's folder the user hasn't trusted in their own Claude Code is +// one only they answer for: the interactive mode asked and ended there, +// and the turn was a 502, which a Claude Code client (paseo) asked again +// and again, so the turn hung (lwjef on X). The turn goes in magpie's own +// folder, told the caller's, and that folder is passed over for the +// conversation's next turns rather than asked about again. +func TestClaudeTUIUntrustedFolderAnswered(t *testing.T) { + caller := t.TempDir() + cwds := filepath.Join(t.TempDir(), "cwds") + h, started := tuiHelperHarness(t, `if [ "$`+claudetui.TrustEnv+`" != 1 ]; then + echo "claude-tui: `+claudetui.Untrusted+`, which only its user answers" >&2 + exit 1 +fi +pwd >> '`+cwds+`'`) + h.ask("Primary working directory: "+caller, "hi") // fails the test on anything but 200 + b, _ := os.ReadFile(cwds) + ran := strings.Fields(string(b)) + if len(ran) != 1 || evalSymlinks(ran[0]) == evalSymlinks(caller) { + t.Fatalf("the answered run ran in %q, the caller's folder %s", ran, caller) + } + if n := helperStarts(started); n != 2 { + t.Fatalf("the helper started %d times, want twice: in the caller's folder, then magpie's", n) + } + h.ask("Primary working directory: "+caller, "another") + if n := helperStarts(started); n != 3 { + t.Fatalf("the helper started %d times, want three: the caller's folder was asked about again", n) + } +}