From 4778cd021c9e47ba575157a77b909a8a8ffeb07c Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 28 Sep 2026 02:25:19 +0800 Subject: [PATCH] =?UTF-8?q?claude=20subscription:=20the=20bridge's=20runs?= =?UTF-8?q?=20keep=20no=20sessions=20on=20disk=20=E2=80=94=20Claude=20Code?= =?UTF-8?q?=20is=20started=20with=20--no-session-persistence,=20as=20each?= =?UTF-8?q?=20run's=20conversation=20lives=20in=20its=20process=20and=20is?= =?UTF-8?q?=20never=20resumed=20from=20a=20file,=20so=20its=20throwaway=20?= =?UTF-8?q?magpie-claude-=20folders=20no=20longer=20pile=20up=20among=20th?= =?UTF-8?q?e=20user's=20projects=20(or=20get=20imported=20from=20there=20b?= =?UTF-8?q?y=20other=20agents);=20the=20ones=20left=20before=20are=20swept?= =?UTF-8?q?=20once=20from=20Claude=20Code's=20projects=20folder,=20only=20?= =?UTF-8?q?those=20named=20after=20a=20magpie-claude-=20folder=20i?= =?UTF-8?q?n=20the=20temp=20directory=20(#121)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gateway/claude_subscription.go | 56 ++++++++++++++++++++ internal/gateway/claude_subscription_test.go | 49 +++++++++++++++++ 2 files changed, 105 insertions(+) diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index 1c57c50cc..a588c99f6 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -50,6 +50,7 @@ type subscriptionBridge struct { calls map[string]*subscriptionRun // tool_use id → run idle map[string]*subscriptionRun // conversation so far (turnKey) → run baseURL string + sweep sync.Once } // A run left for its conversation's next turn waits idleLongest at most, @@ -132,6 +133,57 @@ type bridgeTool struct { InputSchema json.RawMessage `json:"inputSchema"` } +func claudeConfigDir() string { + if d := os.Getenv("CLAUDE_CONFIG_DIR"); d != "" { + return d + } + home, _ := os.UserHomeDir() + return filepath.Join(home, ".claude") +} + +// claudeProjectName is the folder Claude Code keeps a working directory's +// sessions under: the path with everything but letters and digits made "-". +func claudeProjectName(path string) string { + return strings.Map(func(r rune) rune { + if r < 128 && (r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9') { + return r + } + return '-' + }, path) +} + +// sweepBridgeProjects removes the projects that runs from before +// --no-session-persistence left in Claude Code's folder: only those named +// after a magpie-claude- folder in the temp directory, which no one but the +// bridge works in. +func sweepBridgeProjects(claudeDir, tempDir string) { + var prefixes []string + for _, d := range []string{tempDir, evalSymlinks(tempDir)} { + prefix := claudeProjectName(filepath.Join(d, "magpie-claude-")) + if !slices.Contains(prefixes, prefix) { + prefixes = append(prefixes, prefix) + } + } + root := filepath.Join(claudeDir, "projects") + entries, _ := os.ReadDir(root) + for _, e := range entries { + for _, prefix := range prefixes { + rest, ok := strings.CutPrefix(e.Name(), prefix) + if ok && e.IsDir() && rest != "" && strings.Trim(rest, "0123456789") == "" { + _ = os.RemoveAll(filepath.Join(root, e.Name())) + break + } + } + } +} + +func evalSymlinks(path string) string { + if p, err := filepath.EvalSymlinks(path); err == nil { + return p + } + return path +} + func newSubscriptionBridge() *subscriptionBridge { return &subscriptionBridge{runs: map[string]*subscriptionRun{}, calls: map[string]*subscriptionRun{}, idle: map[string]*subscriptionRun{}} } @@ -178,6 +230,7 @@ func (b *subscriptionBridge) start(ctx context.Context, req *Request, model, oau if err != nil { return nil, nil, err } + b.sweep.Do(func() { go sweepBridgeProjects(claudeConfigDir(), os.TempDir()) }) tmp, err := os.MkdirTemp("", "magpie-claude-") if err != nil { return nil, nil, err @@ -451,6 +504,9 @@ func claudeCLIArgs(model, mcpConfig, effort string, web bool) []string { "--include-partial-messages", "--verbose", "--model", model, "--tools", own, "--strict-mcp-config", "--mcp-config", mcpConfig, "--setting-sources", "", "--dangerously-skip-permissions", + // the run's history lives in the process; on disk it would only list a + // throwaway folder among the user's projects + "--no-session-persistence", } if effort != "" { if effort == "xhigh" { diff --git a/internal/gateway/claude_subscription_test.go b/internal/gateway/claude_subscription_test.go index 029657168..19b8d33a4 100644 --- a/internal/gateway/claude_subscription_test.go +++ b/internal/gateway/claude_subscription_test.go @@ -348,3 +348,52 @@ func TestClaudeRunLetGoWhenTheConversationMovedOn(t *testing.T) { t.Fatalf("two conversations: %v waiting", w) } } + +func TestClaudeBridgeKeepsNoSessionsOnDisk(t *testing.T) { + args := claudeCLIArgs("m", `{}`, "", false) + if !slices.Contains(args, "--no-session-persistence") || !slices.Contains(args, "-p") { + t.Fatalf("bridge runs must not persist sessions: %q", args) + } +} + +func TestSweepBridgeProjectsTakesOnlyTheBridgesFolders(t *testing.T) { + base := t.TempDir() + real := filepath.Join(base, "real", "T") + if err := os.MkdirAll(real, 0o755); err != nil { + t.Fatal(err) + } + real = evalSymlinks(real) // what Claude Code names a folder after + link := filepath.Join(base, "link") + if err := os.Symlink(filepath.Join(base, "real"), link); err != nil { + t.Fatal(err) + } + claudeDir := filepath.Join(base, "claude") + projects := filepath.Join(claudeDir, "projects") + gone := []string{ + claudeProjectName(filepath.Join(real, "magpie-claude-1097091858")), + claudeProjectName(filepath.Join(link, "T", "magpie-claude-42")), + } + kept := []string{ + claudeProjectName(filepath.Join(real, "magpie-claude-")), + claudeProjectName(filepath.Join(real, "magpie-claude-12-x")), + claudeProjectName(filepath.Join(real, "other")), + claudeProjectName("/Users/me/code/magpie-claude-7"), + } + for _, d := range append(append([]string{}, gone...), kept...) { + if err := os.MkdirAll(filepath.Join(projects, d), 0o755); err != nil { + t.Fatal(err) + } + } + sweepBridgeProjects(claudeDir, filepath.Join(link, "T")) + for _, d := range gone { + if _, err := os.Stat(filepath.Join(projects, d)); !os.IsNotExist(err) { + t.Fatalf("%s should be swept", d) + } + } + for _, d := range kept { + if _, err := os.Stat(filepath.Join(projects, d)); err != nil { + t.Fatalf("%s should be kept: %v", d, err) + } + } + sweepBridgeProjects(filepath.Join(base, "missing"), real) // no projects folder: nothing to do +}