diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index c757d152a..0adadb156 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -1353,6 +1353,9 @@ func (b *subscriptionBridge) startRun(ctx context.Context, req *Request, model, if tui && !client { // a saved account's run in magpie's folder is never asked (#4830) trustWork(binary, configDir, work) + } else if tui { + // nor is one in the caller's folder (@ahz2ygnhjmIJ0E0 on X) + trustCaller(binary, configDir, work) } cmd.Env = binary.env(binary.asRoot(env), configDir) if tui { diff --git a/internal/gateway/claude_trust_test.go b/internal/gateway/claude_trust_test.go index 14efcb7a6..6a2a93e3b 100644 --- a/internal/gateway/claude_trust_test.go +++ b/internal/gateway/claude_trust_test.go @@ -217,3 +217,65 @@ func TestClaudeBridgeTrustsMagpiesFolderForSavedAccount(t *testing.T) { t.Fatalf("the run started with %v trusted, not magpie's folder %s", got, claudeKey(work)) } } + +// @ahz2ygnhjmIJ0E0 on X (Claude Code 2.1.329): the folders they work in +// were trusted in their own ~/.claude.json, and in the saved accounts' +// files only here and there, so a run of an account without the entry was +// asked about the caller's folder, and each account in turn waited out +// the question. A saved account's run in the caller's folder starts with +// the folder trusted in that account's file, under the path Claude Code +// runs in; the account Claude Code is signed in to keeps its own file +// unwritten. +func TestClaudeBridgeTrustsCallersFolderForSavedAccount(t *testing.T) { + h := tuiScriptHarness(t, `cat "$CLAUDE_CONFIG_DIR/.claude.json" > $log.seen; pwd > $log.pwd`) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(os.Getenv("HOME"), ".config")) + cfg := filepath.Join(filepath.Dir(provider.Path()), "claude-accounts", "0123456789abcdef") + if err := os.MkdirAll(cfg, 0o700); err != nil { + t.Fatal(err) + } + // a saved account's file that trusts some other folder only + if err := os.WriteFile(filepath.Join(cfg, ".claude.json"), []byte(`{"hasCompletedOnboarding": true, "projects": {"/elsewhere": {"hasTrustDialogAccepted": true}}}`), 0o600); err != nil { + t.Fatal(err) + } + caller := t.TempDir() + req := &Request{ + System: "Primary working directory: " + caller, + Messages: []Message{{Role: "user", Parts: []Part{{Kind: Text, Text: "hello"}}}}, + } + run, _, err := h.s.subscription.start(context.Background(), req, "claude-sonnet-5", cfg, "", nil) + if err != nil { + t.Fatal(err) + } + if !run.tui { + t.Fatal("the run wasn't interactive") + } + read := func(suffix string) []byte { + var b []byte + eventually(t, "the run wrote its "+suffix, func() bool { + entries, _ := os.ReadDir(h.logs) + for _, e := range entries { + if strings.HasSuffix(e.Name(), suffix) { + b, _ = os.ReadFile(filepath.Join(h.logs, e.Name())) + } + } + return len(b) > 0 + }) + return b + } + if got := strings.TrimSpace(string(read(".pwd"))); evalSymlinks(got) != evalSymlinks(caller) { + t.Fatalf("the run went in %s, not the caller's %s", got, caller) + } + got := trustedIn(t, read(".seen")) + if !slices.Contains(got, claudeKey(caller)) || !slices.Contains(got, "/elsewhere") { + t.Fatalf("the run started with %v trusted, want the caller's %s and /elsewhere", got, claudeKey(caller)) + } + + // the signed-in account's own file is never written + home, _ := os.UserHomeDir() + trustCaller(claudeCLI{}, "", caller) + for _, f := range []string{filepath.Join(home, ".claude.json"), filepath.Join(home, ".claude", ".claude.json")} { + if _, err := os.Stat(f); !os.IsNotExist(err) { + t.Errorf("%s was written", f) + } + } +} diff --git a/internal/gateway/claude_warmup.go b/internal/gateway/claude_warmup.go index 7c67f1dc3..bb90ee560 100644 --- a/internal/gateway/claude_warmup.go +++ b/internal/gateway/claude_warmup.go @@ -405,6 +405,26 @@ func trustWork(binary claudeCLI, configDir, work string) { } } +// trustCaller records, in a saved account's config directory (configDir, +// "" for the account Claude Code is signed in to, whose ~/.claude.json +// magpie never writes), that Claude Code may work in the caller's folder +// work, before a run there. The shim answers the trust question yes in a +// saved account all the same (claudetui.TrustEnv), and Claude Code then +// records just this, so the entry is the one the answer leaves: recorded +// first, the run isn't asked at all. @ahz2ygnhjmIJ0E0 (X, Claude Code +// 2.1.329) had folders their own ~/.claude.json trusts, while the saved +// accounts' files had them only here and there; each run of an account +// that didn't was asked, and a question the shim didn't read (2.1.329's +// "Quick safety check") waited out 45s on every account in turn. +func trustCaller(binary claudeCLI, configDir, work string) { + if configDir == "" || work == "" { + return + } + if err := provider.ClaudeTrustFolder(configDir, claudeTrustKeys(binary, work)...); err != nil { + log.Printf("claude: the caller's folder not recorded as trusted for %s: %v", configDir, err) + } +} + // trustGOOS is the system claudeTrustKeys keys for; a var for tests. var trustGOOS = runtime.GOOS diff --git a/internal/provider/claude_dirs.go b/internal/provider/claude_dirs.go index 652b787ec..2ca919ab1 100644 --- a/internal/provider/claude_dirs.go +++ b/internal/provider/claude_dirs.go @@ -376,11 +376,11 @@ func claudeOnboarded(dir string, acct map[string]any) { var claudeTrustMu sync.Mutex // ClaudeTrustFolder records, in saved account config directory dir, that -// Claude Code may work in magpie's own folder, which keys names as Claude -// Code keys its projects (2.1.316: the folder's path, on Windows with -// forward slashes, "C:/Users//AppData/Local/Temp/magpie-claude-work"), -// as Claude Code itself records it once the trust question is answered -// yes. On mydosu's Windows (#4830) a saved account's directory had no +// Claude Code may work in a folder (magpie's own, or a caller's), which +// keys names as Claude Code keys its projects (2.1.316: the folder's +// path, on Windows with forward slashes, +// "C:/Users//AppData/Local/Temp/magpie-claude-work"), as Claude Code +// itself records it once the trust question is answered yes. On mydosu's Windows (#4830) a saved account's directory had no // such entry, and every /usage read of that account waited out its time // on the question. // Only a directory of magpie's saved accounts is written, never the