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

@@ -6,9 +6,11 @@ import (
"fmt"
"net"
"os"
"path/filepath"
"time"
"github.com/steveyegge/beads/internal/debug"
"github.com/steveyegge/beads/internal/lockfile"
)
// ClientVersion is the version of this RPC client
@@ -33,7 +35,20 @@ func TryConnect(socketPath string) (*Client, error) {
// TryConnectWithTimeout attempts to connect to the daemon socket using the provided dial timeout.
// Returns nil if no daemon is running or unhealthy.
func TryConnectWithTimeout(socketPath string, dialTimeout time.Duration) (*Client, error) {
if !endpointExists(socketPath) {
// Fast probe: check daemon lock before attempting RPC connection if socket doesn't exist
// This eliminates unnecessary connection attempts when no daemon is running
// If socket exists, we skip lock check for backwards compatibility and test scenarios
socketExists := endpointExists(socketPath)
if !socketExists {
beadsDir := filepath.Dir(socketPath)
running, _ := lockfile.TryDaemonLock(beadsDir)
if !running {
debug.Logf("daemon lock not held and socket missing (no daemon running)")
return nil, nil
}
}
if !socketExists {
debug.Logf("RPC endpoint does not exist: %s", socketPath)
return nil, nil
}