From 24b5bfd77e14c59559810703e06a3c2c2937a992 Mon Sep 17 00:00:00 2001 From: canvrno <46584286+canvrno@users.noreply.github.com> Date: Sun, 12 Oct 2025 06:06:17 +0000 Subject: [PATCH] auto-cleanup stale default instance config (#6693) --- .changeset/three-humans-type.md | 5 +++++ cli/pkg/cli/global/registry.go | 37 +++++++++++++++++++++++++++++++++ cli/pkg/cli/sqlite/locks.go | 2 +- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 .changeset/three-humans-type.md diff --git a/.changeset/three-humans-type.md b/.changeset/three-humans-type.md new file mode 100644 index 00000000..42338fec --- /dev/null +++ b/.changeset/three-humans-type.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +auto-cleanup stale default instance config diff --git a/cli/pkg/cli/global/registry.go b/cli/pkg/cli/global/registry.go index d42eeb75..380d0c51 100644 --- a/cli/pkg/cli/global/registry.go +++ b/cli/pkg/cli/global/registry.go @@ -110,6 +110,43 @@ func (r *ClientRegistry) GetDefaultClient(ctx context.Context) (*client.ClineCli return nil, fmt.Errorf("no default instance configured") } + // Check if the default instance actually exists in the database + if r.lockManager != nil { + exists, err := r.lockManager.HasInstanceAtAddress(defaultAddr) + if err != nil { + // Database is unavailable - Return error instead of attempting cleanup + return nil, fmt.Errorf("cannot verify default instance: database unavailable: %w", err) + } + + if !exists { + // Instance doesn't exist in database but config file references it + // This is a stale config - remove it and try to find another instance + settingsPath := filepath.Join(r.configPath, common.SETTINGS_SUBFOLDER, "settings", "cli-default-instance.json") + if removeErr := os.Remove(settingsPath); removeErr != nil && !os.IsNotExist(removeErr) { + fmt.Printf("Warning: Failed to remove stale default instance config: %v\n", removeErr) + } else { + fmt.Printf("Removed stale default instance config (instance %s not found in database)\n", defaultAddr) + } + + // Try to find and set a new default instance + instances := r.ListInstances() + if len(instances) > 0 { + if err := r.EnsureDefaultInstance(instances); err != nil { + return nil, fmt.Errorf("failed to set new default instance: %w", err) + } + + // Retry with the new default + newDefaultAddr := r.GetDefaultInstance() + if newDefaultAddr != "" { + fmt.Printf("Set new default instance: %s\n", newDefaultAddr) + return r.GetClient(ctx, newDefaultAddr) + } + } + + return nil, fmt.Errorf("no default instance configured") + } + } + return r.GetClient(ctx, defaultAddr) } diff --git a/cli/pkg/cli/sqlite/locks.go b/cli/pkg/cli/sqlite/locks.go index 1755e87d..2b65af01 100644 --- a/cli/pkg/cli/sqlite/locks.go +++ b/cli/pkg/cli/sqlite/locks.go @@ -160,7 +160,7 @@ func (lm *LockManager) RemoveInstanceLock(address string) error { // HasInstanceAtAddress checks if an instance exists at the given address func (lm *LockManager) HasInstanceAtAddress(address string) (bool, error) { if err := lm.ensureConnection(); err != nil { - return false, nil + return false, err } query := common.CountInstanceLockSQL