Standardize daemon detection with tryDaemonLock probe (bd-wgu4)

- Extract lock checking to internal/lockfile package
- Add lock probe in RPC client before connection attempts
- Update daemon discovery to use lock probe
- Eliminates unnecessary connection attempts when socket missing

Closes bd-wgu4

Amp-Thread-ID: https://ampcode.com/threads/T-3b863f21-3af4-49d3-9214-477d904b80fe
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-11-07 21:02:38 -08:00
parent eeef37f37b
commit ba1b856acb
10 changed files with 280 additions and 5 deletions

View File

@@ -7,6 +7,7 @@ import (
"strings"
"time"
"github.com/steveyegge/beads/internal/lockfile"
"github.com/steveyegge/beads/internal/rpc"
)
@@ -145,6 +146,25 @@ func discoverDaemon(socketPath string) DaemonInfo {
Alive: false,
}
// Fast probe: check daemon lock before attempting RPC if socket doesn't exist
// This eliminates unnecessary connection attempts when no daemon is running
// If socket exists, we proceed with RPC for backwards compatibility
_, err := os.Stat(socketPath)
socketExists := err == nil
if !socketExists {
beadsDir := filepath.Dir(socketPath)
running, _ := lockfile.TryDaemonLock(beadsDir)
if !running {
daemon.Error = "daemon lock not held and socket missing"
// Check for daemon-error file
if errMsg := checkDaemonErrorFile(socketPath); errMsg != "" {
daemon.Error = errMsg
}
return daemon
}
}
// Try to connect with short timeout
client, err := rpc.TryConnectWithTimeout(socketPath, 500*time.Millisecond)
if err != nil {