Files
magpie/internal/provider/claude_trust_test.go
T
yetone aab48dea94 claude: a saved account's .claude.json says magpie's work folder is trusted (#4830)
mydosu (#4830, Windows, v0.1.2679, Claude Code 2.1.316): since
d39358f1b the /usage reads and the "hi" test of a saved account run in
<tmp>/magpie-claude-work, and the account's config directory never got
a trusted entry for it. Every read waited out its 16.8s on the trust
question.

magpie now writes the entry itself, before each run there, into a saved
account's .claude.json (provider.ClaudeTrustFolder, gateway trustWork).
This covers every place Claude Code runs in that folder with a
non-default config dir: askClaudeTUI, claudeUsageTUI, and the bridge's
startRun when the caller's folder isn't used. The key is the one Claude
Code 2.1.316 looks a folder up by (its trust check, read from the
binary): the path in NFC, with forward slashes on Windows
("C:/Users/<me>/AppData/Local/Temp/magpie-claude-work"), and also with
its links resolved (/private/var/... on a Mac). For a Claude Code in WSL
it is the mount path. Only a direct child of claude-accounts is written,
never ~/.claude.json or the account Claude Code is signed in to. Other
keys and the project's own fields are kept, numbers go back as written
(UseNumber), a file that can't be parsed is left alone, and nothing is
written once the entry is there. A run's own fallback temp folder isn't
recorded. The helper's yes answer stays as it was.

I could not reproduce the missing entry itself. On xiaomi-gpu, Claude
Code 2.1.316 with a fake sign-in and the helper's yes recorded the key.
This fix does not depend on why it wasn't recorded on mydosu's machine.

Run at this head:
- mote: gofmt -l internal (empty); GOOS=linux and GOOS=windows go vet
  ./internal/gateway ./internal/provider; go test ./internal/provider
  ./internal/claudetui ok; go test -run
  'Claude|TUI|Usage|Warm|Ask|Trust|Subscription|Bridge'
  ./internal/gateway ok. The full gateway package was not run: two
  other sessions' full gateway suites were running on mote.
- xiaomi-gpu (win-run): go test -run
  'TestClaudeTrust|TestClaudeOneOffTrusts|TestClaudeBridgeTrusts'
  ./internal/provider ./internal/gateway ok (the shell-script ones
  skip there).
- xiaomi-gpu, real Claude Code 2.1.316 in a sandbox HOME/TEMP, helper
  told not to answer the question (MAGPIE_CLAUDE_TUI_TRUST=0). A saved
  account dir under a sandbox claude-accounts, with the entry written by
  trustWork (key "C:/Users/yeton/trust4830-sb/tmp/magpie-claude-work"):
  /usage drew its panel in 5.6s, exit 0. The same dir without the entry:
  "Claude Code asks whether to trust the folder" in 0.9s.

Broken by hand, each red:
- trustWork returning at once: TestClaudeOneOffTrustsMagpiesFolder,
  TestClaudeTrustWorkOnlyMagpiesFolderInSavedAccounts,
  TestClaudeBridgeTrustsMagpiesFolderForSavedAccount.
- the bridge's call dropped: TestClaudeBridgeTrustsMagpiesFolderForSavedAccount.
- no "/" for "\" on Windows: TestClaudeTrustKeysAsClaudeCodeKeys on
  mote, plus TestClaudeTrustWorkOnlyMagpiesFolderInSavedAccounts on
  Windows.
- no claude-accounts guard: TestClaudeTrustFolderOnlyMagpiesAccounts.

Not tried: a real signed-in saved account (the reporter's), and Claude
Code in WSL (the key is the mount path, by unit test only).
2026-11-02 14:51:16 +08:00

100 lines
3.5 KiB
Go

package provider
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"testing"
)
// mydosu's saved account (#4830, Windows, Claude Code 2.1.316) had a
// .claude.json with no projects at all, so Claude Code asked whether to
// trust magpie's work folder on every /usage read. ClaudeTrustFolder puts
// the entry there as Claude Code keeps it, every other key and the
// project's own fields kept, and writes nothing once it is there.
func TestClaudeTrustFolderRecordsMagpiesFolder(t *testing.T) {
claudeHome(t)
dir := claudeAccountDir("someone@example.com")
if err := os.MkdirAll(dir, 0o700); err != nil {
t.Fatal(err)
}
profile := filepath.Join(dir, ".claude.json")
key := "C:/Users/me/AppData/Local/Temp/magpie-claude-work"
// onboarding done, magpie's folder there untrusted; a big number as
// Claude Code writes them
stale := `{"hasCompletedOnboarding": true, "numStartups": 3, "big": 12345678901234567890,
"projects": {"C:/elsewhere": {"allowedTools": [], "hasTrustDialogAccepted": false},
"` + key + `": {"lastCost": 0.5, "hasTrustDialogAccepted": false}}}`
if err := os.WriteFile(profile, []byte(stale), 0o600); err != nil {
t.Fatal(err)
}
if err := ClaudeTrustFolder(dir, key, "/private/"+key); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(profile)
var m map[string]any
d := json.NewDecoder(bytes.NewReader(b))
d.UseNumber()
if err := d.Decode(&m); err != nil {
t.Fatalf("%v: %s", err, b)
}
if m["hasCompletedOnboarding"] != true || m["numStartups"] != json.Number("3") || m["big"] != json.Number("12345678901234567890") {
t.Fatalf("other keys changed: %s", b)
}
ps := m["projects"].(map[string]any)
for _, k := range []string{key, "/private/" + key} {
if p, _ := ps[k].(map[string]any); p["hasTrustDialogAccepted"] != true {
t.Errorf("%s is %v, want trusted: %s", k, ps[k], b)
}
}
if p := ps[key].(map[string]any); p["lastCost"] != json.Number("0.5") {
t.Errorf("the project's own fields went: %v", p)
}
if p := ps["C:/elsewhere"].(map[string]any); p["hasTrustDialogAccepted"] != false {
t.Errorf("another folder was trusted: %v", p)
}
// there already: nothing is written
if err := os.WriteFile(profile, append(b, ' '), 0o600); err != nil {
t.Fatal(err)
}
if err := ClaudeTrustFolder(dir, key); err != nil {
t.Fatal(err)
}
if again, _ := os.ReadFile(profile); !bytes.Equal(again, append(b, ' ')) {
t.Errorf("a trusted folder was written again")
}
}
// Only a saved account's directory is written: never the user's own
// ~/.claude.json, nor a file Claude Code may be half way through writing.
func TestClaudeTrustFolderOnlyMagpiesAccounts(t *testing.T) {
home := claudeHome(t)
key := "/tmp/magpie-claude-work-501"
for _, dir := range []string{home, filepath.Join(home, ".claude"), claudeDirsRoot(), filepath.Join(claudeAccountDir("a"), "x")} {
if err := os.MkdirAll(dir, 0o700); err != nil {
t.Fatal(err)
}
if err := ClaudeTrustFolder(dir, key); err == nil {
t.Errorf("%s was written", dir)
}
if _, err := os.Stat(filepath.Join(dir, ".claude.json")); !os.IsNotExist(err) {
t.Errorf("%s has a .claude.json", dir)
}
}
dir := claudeAccountDir("b")
if err := os.MkdirAll(dir, 0o700); err != nil {
t.Fatal(err)
}
half := []byte(`{"numStartups": 3, "proj`)
if err := os.WriteFile(filepath.Join(dir, ".claude.json"), half, 0o600); err != nil {
t.Fatal(err)
}
if err := ClaudeTrustFolder(dir, key); err == nil {
t.Errorf("a half-written file was taken")
}
if b, _ := os.ReadFile(filepath.Join(dir, ".claude.json")); !bytes.Equal(b, half) {
t.Errorf("a half-written file was changed: %s", b)
}
}