gateway: a saved Claude account's run in the caller's folder starts with it trusted in that account's file (@ahz2ygnhjmIJ0E0 on X)

The reporter's own ~/.claude.json trusts the folders they work in, while
magpie's saved accounts' .claude.json files had them only here and there.
A run of an account without the entry was asked about the caller's folder
in the interactive mode, and 2.1.329's question went unread (70a881071),
so each account in turn waited out 45s. claude -p asks nothing, which is
why claudePrintMode answered 200.

Before an interactive run of a saved account in the caller's folder,
trustCaller now records the folder in that account's .claude.json, under
the keys Claude Code looks it up by (claudeTrustKeys: the path and its
resolved one, /private/tmp/... on a Mac), through the same
provider.ClaudeTrustFolder that trusts magpie's own folder (#4830). Every
caller's folder is recorded, not only those the user's own file trusts:
the shim answers yes for a saved account all the same (TrustEnv), and
Claude Code then writes just this entry, so the file ends as it would,
without the question. The signed-in account's own ~/.claude.json is
never written (configDir ""), and its untrusted folders still go to
magpie's folder (startTrusted).

Run:
- mote: go test -tags nogui -count=1 -run 'TestClaudeBridgeTrusts|TestClaudeTrust|TestClaudeOneOff|TestClaudeTUI' ./internal/gateway ok (67s).
- Broken by hand: with claude_subscription.go from 70a881071,
  TestClaudeBridgeTrustsCallersFolderForSavedAccount fails ("the run
  started with [/elsewhere] trusted, want the caller's ..."); green with
  the change.
- Real Claude Code 2.1.329 on mote, sandbox HOME and CLAUDE_CONFIG_DIR,
  the account file holding the folder under /tmp/... and /private/tmp/...
  as trustCaller writes it, and MAGPIE_CLAUDE_TUI_TRUST not set: the
  helper wrote init and the message was sent (authentication_failed,
  no sign-in in the sandbox); nothing asked about trust.
- mote: go vet (darwin, GOOS=windows, GOOS=linux) of internal/gateway and
  internal/provider clean; gofmt clean.

Not tried: a signed-in saved account sending a real message; Windows and
WSL runs (WSL runs never use the caller's folder, binary.wsl).
This commit is contained in:
yetone
2026-11-20 16:42:52 +08:00
parent fafa1978c5
commit 5754fea435
4 changed files with 90 additions and 5 deletions
+3
View File
@@ -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 {
+62
View File
@@ -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)
}
}
}
+20
View File
@@ -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
+5 -5
View File
@@ -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/<me>/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/<me>/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