perf(goals): optimize gt goals from 6s to <50ms via direct SQLite (gt-aps.3)
Some checks failed
CI / Check for .beads changes (push) Has been skipped
CI / Check embedded formulas (push) Failing after 18s
CI / Test (push) Failing after 1m17s
CI / Lint (push) Failing after 21s
CI / Integration Tests (push) Successful in 1m9s
CI / Coverage Report (push) Has been skipped
Windows CI / Windows Build and Unit Tests (push) Has been cancelled

Replace bd subprocess spawns with direct SQLite queries:
- queryEpicsInDir: direct sqlite3 query vs bd list subprocess
- getLinkedConvoys: direct JOIN query vs bd dep list + getIssueDetails loop
- computeGoalLastMovement: reuse epic.UpdatedAt vs separate bd show call

Also includes mailbox optimization from earlier session:
- Consolidated multiple parallel queries into single bd list --all query
- Filters in Go instead of spawning O(identities × statuses) bd processes

177x improvement (6.2s → 35ms) by eliminating subprocess overhead.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
diesel
2026-01-24 17:18:43 -08:00
committed by John Ogle
parent 2f893f0ad3
commit 4fc536c538
2 changed files with 163 additions and 135 deletions

View File

@@ -13,6 +13,7 @@ import (
"time"
"github.com/spf13/cobra"
"github.com/steveyegge/gastown/internal/beads"
"github.com/steveyegge/gastown/internal/style"
"github.com/steveyegge/gastown/internal/workspace"
)
@@ -119,11 +120,11 @@ func showGoal(goalID string) error {
return fmt.Errorf("'%s' is not a goal/epic (type: %s)", goalID, goal.IssueType)
}
// Get linked convoys
convoys := getLinkedConvoys(goalID)
// Get linked convoys (no dbPath available for single goal lookup, use fallback)
convoys := getLinkedConvoys(goalID, "")
// Compute staleness
lastMovement := computeGoalLastMovement(goalID, convoys)
lastMovement := computeGoalLastMovement(goal.UpdatedAt, convoys)
stalenessHrs := time.Since(lastMovement).Hours()
icon := stalenessIcon(stalenessHrs)
@@ -210,8 +211,8 @@ func listGoals() error {
// Build goal info with staleness computation
var goals []goalInfo
for _, epic := range epics {
convoys := getLinkedConvoys(epic.ID)
lastMovement := computeGoalLastMovement(epic.ID, convoys)
convoys := getLinkedConvoys(epic.ID, epic.dbPath)
lastMovement := computeGoalLastMovement(epic.UpdatedAt, convoys)
stalenessHrs := time.Since(lastMovement).Hours()
icon := stalenessIcon(stalenessHrs)
@@ -291,11 +292,61 @@ type convoyInfo struct {
}
// getLinkedConvoys finds convoys linked to a goal (via parent-child relation).
func getLinkedConvoys(goalID string) []convoyInfo {
// dbPath is the path to beads.db containing the goal for direct SQLite queries.
func getLinkedConvoys(goalID, dbPath string) []convoyInfo {
var convoys []convoyInfo
// If no dbPath provided, fall back to bd subprocess (shouldn't happen normally)
if dbPath == "" {
return getLinkedConvoysFallback(goalID)
}
// Query dependencies directly from SQLite
// Children are stored as: depends_on_id = goalID (parent) with type 'blocks'
safeGoalID := strings.ReplaceAll(goalID, "'", "''")
query := fmt.Sprintf(`
SELECT i.id, i.title, i.status
FROM dependencies d
JOIN issues i ON d.issue_id = i.id
WHERE d.depends_on_id = '%s' AND d.type = 'blocks' AND i.issue_type = 'convoy'
`, safeGoalID)
queryCmd := exec.Command("sqlite3", "-json", dbPath, query)
var stdout bytes.Buffer
queryCmd.Stdout = &stdout
if err := queryCmd.Run(); err != nil {
return convoys
}
if stdout.Len() == 0 {
return convoys
}
var results []struct {
ID string `json:"id"`
Title string `json:"title"`
Status string `json:"status"`
}
if err := json.Unmarshal(stdout.Bytes(), &results); err != nil {
return convoys
}
for _, r := range results {
convoys = append(convoys, convoyInfo{
ID: r.ID,
Title: r.Title,
Status: r.Status,
})
}
return convoys
}
// getLinkedConvoysFallback uses bd subprocess (for when dbPath is unknown).
func getLinkedConvoysFallback(goalID string) []convoyInfo {
var convoys []convoyInfo
// Query dependencies where this goal is the parent
// The child issues (convoys) will have depends_on_id = goalID with type = 'parent-child'
depArgs := []string{"dep", "list", goalID, "--json"}
depCmd := exec.Command("bd", depArgs...)
var stdout bytes.Buffer
@@ -315,7 +366,6 @@ func getLinkedConvoys(goalID string) []convoyInfo {
return convoys
}
// Get details for each child that is a convoy
for _, child := range deps.Children {
details := getIssueDetails(child.ID)
if details != nil && details.IssueType == "convoy" {
@@ -332,27 +382,22 @@ func getLinkedConvoys(goalID string) []convoyInfo {
// computeGoalLastMovement computes when the goal last had activity.
// It looks at:
// 1. The goal's own updated_at
// 1. The goal's own updated_at (passed directly to avoid re-querying)
// 2. The last activity of any linked convoy's tracked issues
func computeGoalLastMovement(goalID string, convoys []convoyInfo) time.Time {
func computeGoalLastMovement(goalUpdatedAt string, convoys []convoyInfo) time.Time {
// Start with the goal's own updated_at
showCmd := exec.Command("bd", "show", goalID, "--json")
var stdout bytes.Buffer
showCmd.Stdout = &stdout
showCmd.Run()
var goals []struct {
UpdatedAt string `json:"updated_at"`
}
json.Unmarshal(stdout.Bytes(), &goals)
lastMovement := time.Now().Add(-24 * time.Hour) // Default to 24 hours ago
if len(goals) > 0 && goals[0].UpdatedAt != "" {
if t, err := time.Parse(time.RFC3339, goals[0].UpdatedAt); err == nil {
if goalUpdatedAt != "" {
if t, err := time.Parse(time.RFC3339, goalUpdatedAt); err == nil {
lastMovement = t
}
}
// If no convoys, return early (common case - avoids unnecessary work)
if len(convoys) == 0 {
return lastMovement
}
// Check convoy activity
townBeads, err := getTownBeadsDir()
if err != nil {
@@ -470,6 +515,8 @@ type epicRecord struct {
Priority int `json:"priority"`
UpdatedAt string `json:"updated_at"`
Assignee string `json:"assignee"`
// dbPath is the path to beads.db containing this epic (for direct queries)
dbPath string
}
// collectEpicsFromAllRigs queries all rigs for epics and aggregates them.
@@ -541,27 +588,53 @@ func collectEpicsFromAllRigs() ([]epicRecord, error) {
return allEpics, nil
}
// queryEpicsInDir runs bd list --type=epic in the specified directory.
// queryEpicsInDir queries epics directly from SQLite in the specified directory.
// If dir is empty, uses current working directory.
func queryEpicsInDir(dir string) ([]epicRecord, error) {
listArgs := []string{"list", "--type=epic", "--json"}
if goalsStatus != "" && goalsStatus != "open" {
if goalsStatus == "all" {
listArgs = append(listArgs, "--all")
} else {
listArgs = append(listArgs, "--status="+goalsStatus)
beadsDir := dir
if beadsDir == "" {
var err error
beadsDir, err = os.Getwd()
if err != nil {
return nil, fmt.Errorf("getting working directory: %w", err)
}
}
listCmd := exec.Command("bd", listArgs...)
if dir != "" {
listCmd.Dir = dir
}
var stdout bytes.Buffer
listCmd.Stdout = &stdout
// Resolve redirects to find actual beads.db
resolvedBeads := beads.ResolveBeadsDir(beadsDir)
dbPath := filepath.Join(resolvedBeads, "beads.db")
if err := listCmd.Run(); err != nil {
return nil, fmt.Errorf("listing epics: %w", err)
// Check if database exists
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
return nil, nil // No database, no epics
}
// Build SQL query for epics
query := `SELECT id, title, status, priority, updated_at, assignee
FROM issues
WHERE issue_type = 'epic'`
if goalsStatus == "" || goalsStatus == "open" {
query += ` AND status <> 'closed' AND status <> 'tombstone'`
} else if goalsStatus != "all" {
query += fmt.Sprintf(` AND status = '%s'`, strings.ReplaceAll(goalsStatus, "'", "''"))
} else {
// --all: exclude tombstones but include everything else
query += ` AND status <> 'tombstone'`
}
queryCmd := exec.Command("sqlite3", "-json", dbPath, query)
var stdout bytes.Buffer
queryCmd.Stdout = &stdout
if err := queryCmd.Run(); err != nil {
// Database might be empty or have no epics - not an error
return nil, nil
}
// Handle empty result (sqlite3 -json returns nothing for empty sets)
if stdout.Len() == 0 {
return nil, nil
}
var epics []epicRecord
@@ -569,5 +642,10 @@ func queryEpicsInDir(dir string) ([]epicRecord, error) {
return nil, fmt.Errorf("parsing epics: %w", err)
}
// Set dbPath on each epic for direct queries later
for i := range epics {
epics[i].dbPath = dbPath
}
return epics, nil
}