claude subscription: the bridge's runs keep no sessions on disk — Claude Code is started with --no-session-persistence, as each run's conversation lives in its process and is never resumed from a file, so its throwaway magpie-claude- folders no longer pile up among the user's projects (or get imported from there by other agents); the ones left before are swept once from Claude Code's projects folder, only those named after a magpie-claude-<digits> folder in the temp directory (#121)
This commit is contained in:
@@ -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" {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user