perf(rpc): use bd daemon protocol to reduce subprocess overhead

Replace bd subprocess calls in gt commands with daemon RPC when available.
Each subprocess call has ~40ms overhead for Go binary startup, so using
the daemon's Unix socket protocol significantly reduces latency.

Changes:
- Add RPC client to beads package (beads_rpc.go)
- Modify List/Show/Update/Close methods to try RPC first, fall back to subprocess
- Replace runBdPrime() with direct content output (avoids bd subprocess)
- Replace checkPendingEscalations() to use beads.List() with RPC
- Replace hook.go bd subprocess calls with beads package methods

The RPC client:
- Connects to daemon via Unix socket at .beads/bd.sock
- Uses JSON-based request/response protocol (same as bd daemon)
- Falls back gracefully to subprocess if daemon unavailable
- Lazy-initializes connection on first use

Performance improvement targets (from bd-2zd.2):
- gt prime < 100ms (was 5.8s with subprocess chain)
- gt hook < 100ms (was ~323ms)

Closes: bd-2zd.2
This commit is contained in:
obsidian
2026-01-24 17:13:07 -08:00
committed by John Ogle
parent bd0f30cfdd
commit 6f0282f1c6
4 changed files with 440 additions and 66 deletions

View File

@@ -11,7 +11,6 @@ import (
"github.com/spf13/cobra"
"github.com/steveyegge/gastown/internal/beads"
"github.com/steveyegge/gastown/internal/events"
"github.com/steveyegge/gastown/internal/runtime"
"github.com/steveyegge/gastown/internal/style"
"github.com/steveyegge/gastown/internal/workspace"
)
@@ -185,15 +184,8 @@ func runHook(_ *cobra.Command, args []string) error {
fmt.Printf("%s Replacing completed bead %s...\n", style.Dim.Render(""), existing.ID)
if !hookDryRun {
if hasAttachment {
// Close completed molecule bead (use bd close --force for pinned)
closeArgs := []string{"close", existing.ID, "--force",
"--reason=Auto-replaced by gt hook (molecule complete)"}
if sessionID := runtime.SessionIDFromEnv(); sessionID != "" {
closeArgs = append(closeArgs, "--session="+sessionID)
}
closeCmd := exec.Command("bd", closeArgs...)
closeCmd.Stderr = os.Stderr
if err := closeCmd.Run(); err != nil {
// Close completed molecule bead (use force for pinned)
if err := b.CloseForced(existing.ID, "Auto-replaced by gt hook (molecule complete)"); err != nil {
return fmt.Errorf("closing completed bead %s: %w", existing.ID, err)
}
} else {
@@ -234,15 +226,9 @@ func runHook(_ *cobra.Command, args []string) error {
return nil
}
// Hook the bead using bd update (discovery-based approach)
// Run from town root so bd can find routes.jsonl for prefix-based routing.
// This is essential for hooking convoys (hq-* prefix) stored in town beads.
hookCmd := exec.Command("bd", "update", beadID, "--status=hooked", "--assignee="+agentID)
if townRoot, err := workspace.FindFromCwd(); err == nil {
hookCmd.Dir = townRoot
}
hookCmd.Stderr = os.Stderr
if err := hookCmd.Run(); err != nil {
// Hook the bead using beads package (uses RPC when daemon available)
status := beads.StatusHooked
if err := b.Update(beadID, beads.UpdateOptions{Status: &status, Assignee: &agentID}); err != nil {
return fmt.Errorf("hooking bead: %w", err)
}