/{cmd,internal}: read optimizations

This commit is contained in:
coffeegoddd☕️✨ 2026-05-22 12:39:46 -07:00
commit ff8a2e8cdc
48 changed files with 2724 additions and 1790 deletions

View file

@ -308,6 +308,60 @@ func sortIssues(issues []*types.Issue, sortBy string, reverse bool) {
})
}
// sortIssuesWithCounts sorts an IssueWithCounts slice by the same field set
// as sortIssues. Mirrors that function exactly for the embedded *Issue.
func sortIssuesWithCounts(items []*types.IssueWithCounts, sortBy string, reverse bool) {
if sortBy == "" {
return
}
slices.SortFunc(items, func(a, b *types.IssueWithCounts) int {
if a == nil || a.Issue == nil {
if b == nil || b.Issue == nil {
return 0
}
return 1
}
if b == nil || b.Issue == nil {
return -1
}
var result int
switch sortBy {
case "priority":
result = cmp.Compare(a.Priority, b.Priority)
case "created":
result = b.CreatedAt.Compare(a.CreatedAt)
case "updated":
result = b.UpdatedAt.Compare(a.UpdatedAt)
case "closed":
if a.ClosedAt == nil && b.ClosedAt == nil {
result = 0
} else if a.ClosedAt == nil {
result = 1
} else if b.ClosedAt == nil {
result = -1
} else {
result = b.ClosedAt.Compare(*a.ClosedAt)
}
case "status":
result = cmp.Compare(a.Status, b.Status)
case "id":
result = utils.NaturalCompareIDs(a.ID, b.ID)
case "title":
result = cmp.Compare(strings.ToLower(a.Title), strings.ToLower(b.Title))
case "type":
result = cmp.Compare(a.IssueType, b.IssueType)
case "assignee":
result = cmp.Compare(a.Assignee, b.Assignee)
default:
result = 0
}
if reverse {
return -result
}
return result
})
}
// knownListFlags maps bare words that users might pass as positional args
// but are actually flag names. Each maps to a hint for the error message.
var knownListFlags = map[string]string{
@ -889,6 +943,42 @@ var listCmd = &cobra.Command{
}
// Direct mode
// Watch mode is handled entirely by watchIssues which manages its
// own fetch loop; skip the up-front SearchIssues to avoid a wasted
// call. Stays the highest-precedence path (GH#654).
if watchMode {
watchIssues(ctx, activeStore, filter, readyFlag, parentID, sortBy, reverse, effectiveLimit)
return
}
// JSON branch: single mega-query path. Bypasses the legacy
// SearchIssues + per-call hydration and returns IssueWithCounts
// directly.
if jsonOutput {
var iwc []*types.IssueWithCounts
var err error
if readyFlag {
iwc, err = activeStore.GetReadyWorkWithCounts(ctx, readyWorkFilterFromIssueFilter(filter))
} else {
iwc, err = activeStore.SearchIssuesWithCounts(ctx, "", filter)
}
if err != nil {
FatalError("%v", err)
}
sortIssuesWithCounts(iwc, sortBy, reverse)
truncated := effectiveLimit > 0 && len(iwc) > effectiveLimit
if truncated {
iwc = iwc[:effectiveLimit]
}
if iwc == nil {
iwc = []*types.IssueWithCounts{}
}
outputJSON(iwc)
printTruncationHint(truncated, effectiveLimit)
return
}
var issues []*types.Issue
if readyFlag {
// Use blocker-aware GetReadyWork semantics (GH#3478).
@ -918,12 +1008,6 @@ var listCmd = &cobra.Command{
issues = issues[:effectiveLimit]
}
// Handle watch mode (GH#654) - must be before other output modes
if watchMode {
watchIssues(ctx, activeStore, filter, readyFlag, parentID, sortBy, reverse, effectiveLimit)
return
}
// Handle pretty format (GH#654)
// JSON output takes priority over pretty/tree format (bd-list-json-fix, bd-03r)
if prettyFormat && !jsonOutput {
@ -964,62 +1048,20 @@ var listCmd = &cobra.Command{
return
}
if jsonOutput {
// Get labels and dependency counts in bulk (single query instead of N queries)
issueIDs := make([]string, len(issues))
for i, issue := range issues {
issueIDs[i] = issue.ID
}
// Best effort: display gracefully degrades with empty data
labelsMap, _ := activeStore.GetLabelsForIssues(ctx, issueIDs)
depCounts, _ := activeStore.GetDependencyCounts(ctx, issueIDs)
allDeps, _ := activeStore.GetDependencyRecordsForIssues(ctx, issueIDs)
commentCounts, _ := activeStore.GetCommentCounts(ctx, issueIDs)
// Populate labels and dependencies for JSON output
for _, issue := range issues {
issue.Labels = labelsMap[issue.ID]
issue.Dependencies = allDeps[issue.ID]
}
// Build response with counts + computed parent (bd-ym8c)
issuesWithCounts := make([]*types.IssueWithCounts, len(issues))
for i, issue := range issues {
counts := depCounts[issue.ID]
if counts == nil {
counts = &types.DependencyCounts{DependencyCount: 0, DependentCount: 0}
}
// Compute parent from dependency records
var parent *string
for _, dep := range allDeps[issue.ID] {
if dep.Type == types.DepParentChild {
parent = &dep.DependsOnID
break
}
}
issuesWithCounts[i] = &types.IssueWithCounts{
Issue: issue,
DependencyCount: counts.DependencyCount,
DependentCount: counts.DependentCount,
CommentCount: commentCounts[issue.ID],
Parent: parent,
}
}
outputJSON(issuesWithCounts)
printTruncationHint(truncated, effectiveLimit)
return
}
// Show upgrade notification if needed
maybeShowUpgradeNotification()
// Load labels in bulk for display
// Labels were already hydrated by SearchIssues / GetReadyWork; reuse
// them via a map keyed by ID so the existing format helpers do not
// need to change shape.
issueIDs := make([]string, len(issues))
labelsMap := make(map[string][]string, len(issues))
for i, issue := range issues {
issueIDs[i] = issue.ID
if len(issue.Labels) > 0 {
labelsMap[issue.ID] = issue.Labels
}
}
// Best effort: display gracefully degrades with empty data
labelsMap, _ := activeStore.GetLabelsForIssues(ctx, issueIDs)
// Load blocking info for displayed issues only (bd-7di).
// Previously loaded ALL dependency records which was O(total_issues) and took 2-4s.

View file

@ -129,36 +129,57 @@ Examples:
FatalError("no storage available")
}
// If we need predicate filtering, we may need to fetch more results
// to ensure we get enough after filtering
// If predicate filtering is needed (OR queries that can't be
// expressed in pure SQL), over-fetch so we still have enough rows
// after the Go-side predicate culls non-matches.
searchFilter := result.Filter
if result.RequiresPredicate && limit > 0 {
// Fetch more to account for predicate filtering
searchFilter.Limit = limit * 3
if searchFilter.Limit < 100 {
searchFilter.Limit = 100
}
}
// JSON branch: single mega-query returns IssueWithCounts directly,
// bypassing the legacy SearchIssues + GetDependencyCounts +
// GetCommentCounts sequence (Stage 2).
if jsonOutput {
iwc, err := store.SearchIssuesWithCounts(ctx, "", searchFilter)
if err != nil {
FatalError("%v", err)
}
if result.RequiresPredicate && result.Predicate != nil {
filtered := make([]*types.IssueWithCounts, 0, len(iwc))
for _, item := range iwc {
if item == nil || item.Issue == nil {
continue
}
if result.Predicate(item.Issue) {
filtered = append(filtered, item)
}
}
iwc = filtered
if limit > 0 && len(iwc) > limit {
iwc = iwc[:limit]
}
}
sortIssuesWithCounts(iwc, sortBy, reverse)
if iwc == nil {
iwc = []*types.IssueWithCounts{}
}
outputJSON(iwc)
return
}
// Non-JSON path: legacy SearchIssues + Go-side sort + display.
// Labels are hydrated on each *types.Issue by SearchIssues, so
// outputQueryResults can read issue.Labels directly.
issues, err := store.SearchIssues(ctx, "", searchFilter)
if err != nil {
FatalError("%v", err)
}
// Apply predicate filter if needed (for OR queries)
if result.RequiresPredicate && result.Predicate != nil {
// For predicate filtering, we need labels populated on issues
if store != nil {
issueIDs := make([]string, len(issues))
for i, issue := range issues {
issueIDs[i] = issue.ID
}
labelsMap, _ := store.GetLabelsForIssues(ctx, issueIDs) // Best effort: display gracefully degrades with empty data
for _, issue := range issues {
issue.Labels = labelsMap[issue.ID]
}
}
filtered := make([]*types.Issue, 0, len(issues))
for _, issue := range issues {
if result.Predicate(issue) {
@ -166,64 +187,13 @@ Examples:
}
}
issues = filtered
// Apply limit after predicate filtering
if limit > 0 && len(issues) > limit {
issues = issues[:limit]
}
}
// Apply sorting
sortIssues(issues, sortBy, reverse)
// Output results
if jsonOutput {
// Get labels and dependency counts
if store != nil {
issueIDs := make([]string, len(issues))
for i, issue := range issues {
issueIDs[i] = issue.ID
}
labelsMap, _ := store.GetLabelsForIssues(ctx, issueIDs) // Best effort: display gracefully degrades with empty data
depCounts, _ := store.GetDependencyCounts(ctx, issueIDs) // Best effort: display gracefully degrades with empty data
commentCounts, _ := store.GetCommentCounts(ctx, issueIDs) // Best effort: display gracefully degrades with empty data
for _, issue := range issues {
issue.Labels = labelsMap[issue.ID]
}
issuesWithCounts := make([]*types.IssueWithCounts, len(issues))
for i, issue := range issues {
counts := depCounts[issue.ID]
if counts == nil {
counts = &types.DependencyCounts{DependencyCount: 0, DependentCount: 0}
}
issuesWithCounts[i] = &types.IssueWithCounts{
Issue: issue,
DependencyCount: counts.DependencyCount,
DependentCount: counts.DependentCount,
CommentCount: commentCounts[issue.ID],
}
}
outputJSON(issuesWithCounts)
} else {
outputJSON(issues)
}
return
}
// Load labels for display
if store != nil && !result.RequiresPredicate {
issueIDs := make([]string, len(issues))
for i, issue := range issues {
issueIDs[i] = issue.ID
}
labelsMap, _ := store.GetLabelsForIssues(ctx, issueIDs) // Best effort: display gracefully degrades with empty data
for _, issue := range issues {
issue.Labels = labelsMap[issue.ID]
}
}
outputQueryResults(issues, queryStr, longFormat)
},
}

View file

@ -218,6 +218,32 @@ This is useful for agents executing molecules to see which steps can run next.`,
return
}
if jsonOutput {
results, err := activeStore.GetReadyWorkWithCounts(ctx, filter)
if err != nil {
FatalError("%v", err)
}
totalReady := len(results)
truncated := false
if filter.Limit > 0 && len(results) == filter.Limit {
countFilter := filter
countFilter.Limit = 0
all, countErr := activeStore.GetReadyWorkWithCounts(ctx, countFilter)
if countErr == nil && len(all) > len(results) {
totalReady = len(all)
truncated = true
}
}
if results == nil {
results = []*types.IssueWithCounts{}
}
outputJSON(results)
if truncated {
fmt.Fprintf(os.Stderr, "Showing %d of %d ready issues. Use --limit 0 for all, or --limit N to raise the cap.\n", len(results), totalReady)
}
return
}
issues, err := activeStore.GetReadyWork(ctx, filter)
if err != nil {
FatalError("%v", err)
@ -234,14 +260,6 @@ This is useful for agents executing molecules to see which steps can run next.`,
truncated = true
}
}
if jsonOutput {
outputJSON(buildReadyIssueOutput(ctx, activeStore, issues))
if truncated {
fmt.Fprintf(os.Stderr, "Showing %d of %d ready issues. Use --limit 0 for all, or --limit N to raise the cap.\n", len(issues), totalReady)
}
return
}
// Show upgrade notification if needed
maybeShowUpgradeNotification()
@ -419,13 +437,13 @@ func buildReadyIssueOutput(ctx context.Context, s storage.DoltStorage, issues []
}
// Best effort: display gracefully degrades with empty data.
labelsMap, _ := s.GetLabelsForIssues(ctx, issueIDs)
// Labels were already hydrated by GetReadyWork / ClaimReadyIssue
// (issueops.GetIssuesByIDsInTx) so we do not re-fetch them here.
depCounts, _ := s.GetDependencyCounts(ctx, issueIDs)
allDeps, _ := s.GetDependencyRecordsForIssues(ctx, issueIDs)
commentCounts, _ := s.GetCommentCounts(ctx, issueIDs)
for _, issue := range issues {
issue.Labels = labelsMap[issue.ID]
issue.Dependencies = allDeps[issue.ID]
}

View file

@ -12,22 +12,42 @@ import (
"github.com/steveyegge/beads/internal/storage"
)
// getRoutingConfigValue resolves routing config from YAML/env first, then DB config.
// Only uses the YAML value if it was explicitly set (not a Viper default), so that
// DB-stored values aren't shadowed by defaults like "~/.beads-planning".
func getRoutingConfigValue(ctx context.Context, store storage.DoltStorage, key string) string {
// routingConfigKeys lists every config key consulted by routing resolution.
// Used to batch-load DB values in one query (GH read-path optimization).
var routingConfigKeys = []string{
"routing.mode",
"routing.contributor",
"routing.default",
"routing.maintainer",
"contributor.auto_route",
"contributor.planning_repo",
}
// resolveRoutingConfigValue applies the standard YAML/env-first, then-DB
// precedence using a preloaded DB-config map. dbValues may be nil (treated as
// empty) when the store lookup failed; resolution still works from YAML/env.
func resolveRoutingConfigValue(key string, dbValues map[string]string) string {
// Only trust YAML/env values that were explicitly set, not Viper defaults.
if src := config.GetValueSource(key); src != config.SourceDefault {
value := strings.TrimSpace(config.GetString(key))
if value != "" {
if value := strings.TrimSpace(config.GetString(key)); value != "" {
return value
}
}
return strings.TrimSpace(dbValues[key])
}
// getRoutingConfigValue is the single-key compatibility shim retained for
// callers (e.g. write paths in bd create) that do not yet batch their reads.
// New code should prefer resolveRoutingConfigValue with a preloaded map.
func getRoutingConfigValue(ctx context.Context, store storage.DoltStorage, key string) string {
if src := config.GetValueSource(key); src != config.SourceDefault {
if value := strings.TrimSpace(config.GetString(key)); value != "" {
return value
}
}
if store == nil {
return ""
}
dbValue, err := store.GetConfig(ctx, key)
if err != nil {
debug.Logf("DEBUG: failed to read config %q from store: %v\n", key, err)
@ -44,24 +64,42 @@ func determineAutoRoutedRepoPath(ctx context.Context, store storage.DoltStorage)
debug.Logf("Warning: failed to detect user role: %v\n", err)
}
// Load all routing-related config keys in a single DB transaction rather
// than issuing one tx per key. The full config table is small in
// practice (well under a thousand rows on real projects).
var dbValues map[string]string
if store != nil {
all, allErr := store.GetAllConfig(ctx)
if allErr != nil {
debug.Logf("DEBUG: failed to read config from store: %v\n", allErr)
} else {
dbValues = make(map[string]string, len(routingConfigKeys))
for _, key := range routingConfigKeys {
if v, ok := all[key]; ok {
dbValues[key] = v
}
}
}
}
// Build routing config with backward compatibility for legacy contributor.* keys.
routingMode := getRoutingConfigValue(ctx, store, "routing.mode")
contributorRepo := getRoutingConfigValue(ctx, store, "routing.contributor")
routingMode := resolveRoutingConfigValue("routing.mode", dbValues)
contributorRepo := resolveRoutingConfigValue("routing.contributor", dbValues)
// Backward compatibility - fall back to legacy contributor.* keys
if routingMode == "" {
if getRoutingConfigValue(ctx, store, "contributor.auto_route") == "true" {
if resolveRoutingConfigValue("contributor.auto_route", dbValues) == "true" {
routingMode = "auto"
}
}
if contributorRepo == "" {
contributorRepo = getRoutingConfigValue(ctx, store, "contributor.planning_repo")
contributorRepo = resolveRoutingConfigValue("contributor.planning_repo", dbValues)
}
routingConfig := &routing.RoutingConfig{
Mode: routingMode,
DefaultRepo: getRoutingConfigValue(ctx, store, "routing.default"),
MaintainerRepo: getRoutingConfigValue(ctx, store, "routing.maintainer"),
DefaultRepo: resolveRoutingConfigValue("routing.default", dbValues),
MaintainerRepo: resolveRoutingConfigValue("routing.maintainer", dbValues),
ContributorRepo: contributorRepo,
ExplicitOverride: "",
}

View file

@ -548,6 +548,9 @@ func (s *configStore) ReopenIssue(_ context.Context, _, _, _ string) error {
func (s *configStore) UpdateIssueType(_ context.Context, _, _, _ string) error { return nil }
func (s *configStore) CloseIssue(_ context.Context, _, _, _, _ string) error { return nil }
func (s *configStore) DeleteIssue(_ context.Context, _ string) error { return nil }
func (s *configStore) SearchIssuesWithCounts(_ context.Context, _ string, _ types.IssueFilter) ([]*types.IssueWithCounts, error) {
return nil, nil
}
func (s *configStore) SearchIssues(_ context.Context, _ string, _ types.IssueFilter) ([]*types.Issue, error) {
return nil, nil
}
@ -581,6 +584,9 @@ func (s *configStore) GetIssuesByLabel(_ context.Context, _ string) ([]*types.Is
func (s *configStore) GetReadyWork(_ context.Context, _ types.WorkFilter) ([]*types.Issue, error) {
return nil, nil
}
func (s *configStore) GetReadyWorkWithCounts(_ context.Context, _ types.WorkFilter) ([]*types.IssueWithCounts, error) {
return nil, nil
}
func (s *configStore) GetBlockedIssues(_ context.Context, _ types.WorkFilter) ([]*types.BlockedIssue, error) {
return nil, nil
}

View file

@ -120,101 +120,82 @@ func (s *DoltStore) GetLocalMetadata(ctx context.Context, key string) (string, e
return value, err
}
// GetCustomStatuses returns custom status name strings from config (backward-compatible API).
// Callers that need category information should use GetCustomStatusesDetailed instead.
func (s *DoltStore) GetCustomStatuses(ctx context.Context) ([]string, error) {
// loadCustomConfigCache lazily populates the customStatus* and customType*
// caches together via a single tx. The combined resolver issues at most one
// batched config-table read (for both status.custom and types.custom) when
// either normalized table is empty, replacing what used to be two separate
// single-key SELECTs across two separate transactions.
func (s *DoltStore) loadCustomConfigCache(ctx context.Context) {
s.cacheMu.Lock()
if s.customStatusCached {
result := s.customStatusCache
if s.customStatusCached && s.customTypeCached {
s.cacheMu.Unlock()
return result, nil
return
}
s.cacheMu.Unlock()
// Populate via detailed method which handles parsing and fallback
detailed, err := s.GetCustomStatusesDetailed(ctx)
if err != nil {
return nil, err
}
return types.CustomStatusNames(detailed), nil
}
// GetCustomStatusesDetailed returns typed custom statuses with category information.
// Falls back to config.yaml if DB config is unavailable.
// On parse errors (malformed config), logs a warning and returns nil (degraded mode).
// Results are cached per DoltStore lifetime and invalidated when SetConfig
// updates the "status.custom" key.
func (s *DoltStore) GetCustomStatusesDetailed(ctx context.Context) ([]types.CustomStatus, error) {
s.cacheMu.Lock()
if s.customStatusCached {
result := s.customStatusDetailedCache
s.cacheMu.Unlock()
return result, nil
}
s.cacheMu.Unlock()
var detailed []types.CustomStatus
var statuses []types.CustomStatus
var customTypes []string
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var txErr error
detailed, txErr = issueops.ResolveCustomStatusesDetailedInTx(ctx, tx)
return txErr
var resolveErr error
statuses, customTypes, resolveErr = issueops.ResolveCustomConfigInTx(ctx, tx)
return resolveErr
})
if err != nil {
// DB unavailable — fall back to config.yaml before giving up.
log.Printf("warning: failed to resolve custom statuses: %v. Custom statuses disabled. Fix with: bd config set status.custom \"valid,values\"", err)
// DB unavailable — fall back to config.yaml for both sides before
// giving up. We still mark the caches populated so a transient
// failure doesn't cause a re-probe on every call.
log.Printf("warning: failed to resolve custom config: %v", err)
if yamlStatuses := config.GetCustomStatusesFromYAML(); len(yamlStatuses) > 0 {
return issueops.ParseStatusFallback(yamlStatuses), nil
statuses = issueops.ParseStatusFallback(yamlStatuses)
}
if yamlTypes := config.GetCustomTypesFromYAML(); len(yamlTypes) > 0 {
customTypes = yamlTypes
}
return nil, nil
}
s.cacheMu.Lock()
if !s.customStatusCached {
s.customStatusDetailedCache = detailed
s.customStatusCache = types.CustomStatusNames(detailed)
s.customStatusDetailedCache = statuses
s.customStatusCache = types.CustomStatusNames(statuses)
s.customStatusCached = true
}
if !s.customTypeCached {
s.customTypeCache = customTypes
s.customTypeCached = true
}
s.cacheMu.Unlock()
}
return detailed, nil
// GetCustomStatuses returns custom status name strings from config (backward-compatible API).
// Callers that need category information should use GetCustomStatusesDetailed instead.
func (s *DoltStore) GetCustomStatuses(ctx context.Context) ([]string, error) {
s.loadCustomConfigCache(ctx)
s.cacheMu.Lock()
defer s.cacheMu.Unlock()
return s.customStatusCache, nil
}
// GetCustomStatusesDetailed returns typed custom statuses with category information.
// Falls back to config.yaml if DB config is unavailable.
// Results are cached per DoltStore lifetime and invalidated when SetConfig
// updates the "status.custom" key.
func (s *DoltStore) GetCustomStatusesDetailed(ctx context.Context) ([]types.CustomStatus, error) {
s.loadCustomConfigCache(ctx)
s.cacheMu.Lock()
defer s.cacheMu.Unlock()
return s.customStatusDetailedCache, nil
}
// GetCustomTypes returns custom issue type values from config.
// If the database doesn't have custom types configured, falls back to config.yaml.
// This fallback is essential during operations when the database connection is
// temporarily unavailable or when types.custom hasn't been configured yet.
// Returns an empty slice if no custom types are configured.
// Results are cached per DoltStore lifetime and invalidated when SetConfig
// updates the "types.custom" key.
func (s *DoltStore) GetCustomTypes(ctx context.Context) ([]string, error) {
s.loadCustomConfigCache(ctx)
s.cacheMu.Lock()
if s.customTypeCached {
result := s.customTypeCache
s.cacheMu.Unlock()
return result, nil
}
s.cacheMu.Unlock()
var result []string
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var txErr error
result, txErr = issueops.ResolveCustomTypesInTx(ctx, tx)
return txErr
})
if err != nil {
// DB unavailable — fall back to config.yaml.
if yamlTypes := config.GetCustomTypesFromYAML(); len(yamlTypes) > 0 {
return yamlTypes, nil
}
return nil, err
}
s.cacheMu.Lock()
s.customTypeCache = result
s.customTypeCached = true
s.cacheMu.Unlock()
return result, nil
defer s.cacheMu.Unlock()
return s.customTypeCache, nil
}
// GetInfraTypes returns infrastructure type names from config.

View file

@ -1,78 +0,0 @@
package dolt
import (
"testing"
"github.com/steveyegge/beads/internal/types"
)
// TestDeleteIssue_InvalidatesBlockedCache verifies that DeleteIssue clears the
// blocked-IDs cache so that subsequent bd-ready/bd-blocked calls recompute.
func TestDeleteIssue_InvalidatesBlockedCache(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
// Create two issues with a dependency edge
a := &types.Issue{Title: "parent", Status: types.StatusOpen, Priority: 3, IssueType: types.TypeTask}
b := &types.Issue{Title: "child", Status: types.StatusOpen, Priority: 3, IssueType: types.TypeTask}
if err := store.CreateIssue(ctx, a, "test"); err != nil {
t.Fatalf("CreateIssue(a): %v", err)
}
if err := store.CreateIssue(ctx, b, "test"); err != nil {
t.Fatalf("CreateIssue(b): %v", err)
}
dep := &types.Dependency{IssueID: b.ID, DependsOnID: a.ID, Type: "blocks"}
if err := store.AddDependency(ctx, dep, "test"); err != nil {
t.Fatalf("AddDependency: %v", err)
}
// Prime the cache by computing blocked IDs
if _, err := store.computeBlockedIDs(ctx, true); err != nil {
t.Fatalf("computeBlockedIDs: %v", err)
}
if !store.blockedIDsCached {
t.Fatal("expected blockedIDsCached=true after computeBlockedIDs")
}
// Delete the parent — should invalidate cache
if err := store.DeleteIssue(ctx, a.ID); err != nil {
t.Fatalf("DeleteIssue: %v", err)
}
if store.blockedIDsCached {
t.Error("expected blockedIDsCached=false after DeleteIssue")
}
}
// TestDeleteIssues_InvalidatesBlockedCache verifies that batch delete clears
// the blocked-IDs cache.
func TestDeleteIssues_InvalidatesBlockedCache(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
a := &types.Issue{Title: "issue-a", Status: types.StatusOpen, Priority: 3, IssueType: types.TypeTask}
if err := store.CreateIssue(ctx, a, "test"); err != nil {
t.Fatalf("CreateIssue: %v", err)
}
// Prime the cache
if _, err := store.computeBlockedIDs(ctx, true); err != nil {
t.Fatalf("computeBlockedIDs: %v", err)
}
if !store.blockedIDsCached {
t.Fatal("expected blockedIDsCached=true after computeBlockedIDs")
}
// Batch delete
if _, err := store.DeleteIssues(ctx, []string{a.ID}, false, true, false); err != nil {
t.Fatalf("DeleteIssues: %v", err)
}
if store.blockedIDsCached {
t.Error("expected blockedIDsCached=false after DeleteIssues")
}
}

View file

@ -47,11 +47,7 @@ func (s *DoltStore) AddDependency(ctx context.Context, dep *types.Dependency, ac
IsCrossPrefix: isCrossPrefix,
TargetKind: &kind,
}
if err := issueops.AddDependencyInTx(ctx, tx, dep, actor, opts); err != nil {
return err
}
s.invalidateBlockedIDsCache()
return nil
return issueops.AddDependencyInTx(ctx, tx, dep, actor, opts)
}); err != nil {
return err
}
@ -72,7 +68,6 @@ func (s *DoltStore) RemoveDependency(ctx context.Context, issueID, dependsOnID s
if err := issueops.RemoveDependencyInTx(ctx, tx, issueID, dependsOnID); err != nil {
return err
}
s.invalidateBlockedIDsCache()
return wrapTransactionError("commit remove wisp dependency", tx.Commit())
}
@ -86,7 +81,6 @@ func (s *DoltStore) RemoveDependency(ctx context.Context, issueID, dependsOnID s
return err
}
s.invalidateBlockedIDsCache()
if err := tx.Commit(); err != nil {
return fmt.Errorf("sql commit: %w", err)
}
@ -296,48 +290,24 @@ func (s *DoltStore) DetectCycles(ctx context.Context) ([][]*types.Issue, error)
return result, err
}
// IsBlocked checks if an issue has open blockers.
// Uses computeBlockedIDs for authoritative blocked status, consistent with
// GetReadyWork. This covers all blocking dependency types (blocks, waits-for)
// with full gate evaluation semantics. (GH#1524)
// IsBlocked checks if an issue has open blockers, reading the authoritative
// stored is_blocked column on issues/wisps and (when true) collecting the
// active blocker list for display.
func (s *DoltStore) IsBlocked(ctx context.Context, issueID string) (bool, []string, error) {
// Use computeBlockedIDs as the single source of truth for blocked status.
// This ensures the close guard is consistent with ready work calculation.
_, err := s.computeBlockedIDs(ctx, true)
if err != nil {
return false, nil, fmt.Errorf("failed to compute blocked IDs: %w", err)
}
s.cacheMu.Lock()
isBlocked := s.blockedIDsCacheMap[issueID]
s.cacheMu.Unlock()
if !isBlocked {
return false, nil, nil
}
var blocked bool
var blockers []string
if err := s.withReadTx(ctx, func(tx *sql.Tx) error {
blocked, got, err := issueops.IsBlockedInTx(ctx, tx, issueID)
if err != nil {
return err
}
if blocked {
blockers = got
}
return nil
}); err != nil {
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
blocked, blockers, err = issueops.IsBlockedInTx(ctx, tx, issueID)
return err
})
if err != nil {
return false, nil, fmt.Errorf("failed to check blockers: %w", err)
}
return true, blockers, nil
return blocked, blockers, nil
}
// GetNewlyUnblockedByClose finds issues that become unblocked when an issue is closed.
//
// Rewritten from a single query with nested JOIN + correlated NOT EXISTS to two
// sequential queries to avoid Dolt query-planner issues with nested JOIN subqueries.
// See bd-o23 / hq-g4nxe for the SQL audit that identified this pattern.
func (s *DoltStore) GetNewlyUnblockedByClose(ctx context.Context, closedIssueID string) ([]*types.Issue, error) {
var result []*types.Issue
err := s.withReadTx(ctx, func(tx *sql.Tx) error {

View file

@ -191,7 +191,6 @@ func (s *DoltStore) PromoteFromEphemeral(ctx context.Context, id string, actor s
}); err != nil {
return err
}
s.invalidateBlockedIDsCache()
return nil
}
@ -275,11 +274,21 @@ func (s *DoltStore) DemoteToWisp(ctx context.Context, id string, updates map[str
return fmt.Errorf("failed to delete issue from issues: %w", err)
}
// id now lives in `wisps` and any inbound refs have been retargeted
// to depends_on_wisp_id. Refresh is_blocked for id plus its
// dependers and parent-child descendants.
affectedIssues, affectedWisps, aerr := issueops.AffectedByStatusChangeForWispInTx(ctx, tx, id)
if aerr != nil {
return fmt.Errorf("affected by demote for %s: %w", id, aerr)
}
if err := issueops.RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
return fmt.Errorf("recompute is_blocked after demote for %s: %w", id, err)
}
return s.doltAddAndCommitInTx(ctx, tx, permanentIssueAuxTables, fmt.Sprintf("bd: demote %s to wisp", id))
}); err != nil {
return err
}
s.invalidateBlockedIDsCache()
return nil
}

View file

@ -0,0 +1,380 @@
package dolt
import (
"context"
"testing"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/types"
)
// getIsBlocked reads the stored is_blocked column for the given id in the
// given table ("issues" or "wisps"). Returns false if the row is missing.
func getIsBlocked(t *testing.T, ctx context.Context, store *DoltStore, table, id string) bool {
t.Helper()
var b int
//nolint:gosec // G201: table is a hardcoded "issues" or "wisps" from callers.
err := store.db.QueryRowContext(ctx, "SELECT is_blocked FROM "+table+" WHERE id = ?", id).Scan(&b)
if err != nil {
t.Fatalf("read is_blocked from %s for %s: %v", table, id, err)
}
return b != 0
}
// TestIsBlocked_FreshIssueIsNotBlocked covers spec case (1): a newly created
// issue with no dependencies should have is_blocked = 0.
func TestIsBlocked_FreshIssueIsNotBlocked(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
createPerm(t, ctx, store, "isb-fresh-1")
if getIsBlocked(t, ctx, store, "issues", "isb-fresh-1") {
t.Fatal("fresh issue should have is_blocked = 0")
}
}
// TestIsBlocked_AddRemoveBlocksDepFlips covers spec case (2).
func TestIsBlocked_AddRemoveBlocksDepFlips(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
createPerm(t, ctx, store, "isb-blocker")
createPerm(t, ctx, store, "isb-blocked")
if err := store.AddDependency(ctx, &types.Dependency{
IssueID: "isb-blocked", DependsOnID: "isb-blocker", Type: types.DepBlocks,
}, "tester"); err != nil {
t.Fatalf("AddDependency: %v", err)
}
if !getIsBlocked(t, ctx, store, "issues", "isb-blocked") {
t.Fatal("expected is_blocked = 1 after adding blocks dep")
}
if getIsBlocked(t, ctx, store, "issues", "isb-blocker") {
t.Fatal("blocker itself should not be is_blocked")
}
if err := store.RemoveDependency(ctx, "isb-blocked", "isb-blocker", "tester"); err != nil {
t.Fatalf("RemoveDependency: %v", err)
}
if getIsBlocked(t, ctx, store, "issues", "isb-blocked") {
t.Fatal("expected is_blocked = 0 after removing blocks dep")
}
}
// TestIsBlocked_CloseReopenBlockerFlipsDepender covers spec case (3).
func TestIsBlocked_CloseReopenBlockerFlipsDepender(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
createPerm(t, ctx, store, "isb-cr-blocker")
createPerm(t, ctx, store, "isb-cr-blocked")
if err := store.AddDependency(ctx, &types.Dependency{
IssueID: "isb-cr-blocked", DependsOnID: "isb-cr-blocker", Type: types.DepBlocks,
}, "tester"); err != nil {
t.Fatalf("AddDependency: %v", err)
}
if !getIsBlocked(t, ctx, store, "issues", "isb-cr-blocked") {
t.Fatal("expected blocked after dep add")
}
if err := store.CloseIssue(ctx, "isb-cr-blocker", "done", "tester", ""); err != nil {
t.Fatalf("CloseIssue: %v", err)
}
if getIsBlocked(t, ctx, store, "issues", "isb-cr-blocked") {
t.Fatal("expected unblocked after blocker closes")
}
if err := store.ReopenIssue(ctx, "isb-cr-blocker", "", "tester"); err != nil {
t.Fatalf("ReopenIssue: %v", err)
}
if !getIsBlocked(t, ctx, store, "issues", "isb-cr-blocked") {
t.Fatal("expected blocked again after blocker reopens")
}
}
// TestIsBlocked_PinStatusBehavesLikeClose covers spec case (4): setting status
// to 'pinned' on the blocker makes it inactive for blocking purposes, just
// like 'closed'.
func TestIsBlocked_PinStatusBehavesLikeClose(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
createPerm(t, ctx, store, "isb-pin-blocker")
createPerm(t, ctx, store, "isb-pin-blocked")
if err := store.AddDependency(ctx, &types.Dependency{
IssueID: "isb-pin-blocked", DependsOnID: "isb-pin-blocker", Type: types.DepBlocks,
}, "tester"); err != nil {
t.Fatalf("AddDependency: %v", err)
}
if !getIsBlocked(t, ctx, store, "issues", "isb-pin-blocked") {
t.Fatal("expected blocked after dep add")
}
if err := store.UpdateIssue(ctx, "isb-pin-blocker", map[string]interface{}{
"status": string(types.StatusPinned),
}, "tester"); err != nil {
t.Fatalf("pin blocker: %v", err)
}
if getIsBlocked(t, ctx, store, "issues", "isb-pin-blocked") {
t.Fatal("expected unblocked when blocker status is pinned")
}
if err := store.UpdateIssue(ctx, "isb-pin-blocker", map[string]interface{}{
"status": string(types.StatusOpen),
}, "tester"); err != nil {
t.Fatalf("unpin blocker: %v", err)
}
if !getIsBlocked(t, ctx, store, "issues", "isb-pin-blocked") {
t.Fatal("expected blocked again after blocker unpinned to open")
}
}
// TestIsBlocked_ParentChildTransitivePropagation covers spec case (5):
// an active grandchild of a blocked grandparent is also is_blocked = 1.
func TestIsBlocked_ParentChildTransitivePropagation(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
createPerm(t, ctx, store, "isb-gp-blocker")
createPerm(t, ctx, store, "isb-gp")
createPerm(t, ctx, store, "isb-parent")
createPerm(t, ctx, store, "isb-child")
if err := store.AddDependency(ctx, &types.Dependency{
IssueID: "isb-parent", DependsOnID: "isb-gp", Type: types.DepParentChild,
}, "tester"); err != nil {
t.Fatalf("parent-child gp->parent: %v", err)
}
if err := store.AddDependency(ctx, &types.Dependency{
IssueID: "isb-child", DependsOnID: "isb-parent", Type: types.DepParentChild,
}, "tester"); err != nil {
t.Fatalf("parent-child parent->child: %v", err)
}
if err := store.AddDependency(ctx, &types.Dependency{
IssueID: "isb-gp", DependsOnID: "isb-gp-blocker", Type: types.DepBlocks,
}, "tester"); err != nil {
t.Fatalf("blocks gp: %v", err)
}
for _, id := range []string{"isb-gp", "isb-parent", "isb-child"} {
if !getIsBlocked(t, ctx, store, "issues", id) {
t.Fatalf("%s should be is_blocked = 1 (transitive)", id)
}
}
if err := store.CloseIssue(ctx, "isb-gp-blocker", "done", "tester", ""); err != nil {
t.Fatalf("CloseIssue gp-blocker: %v", err)
}
for _, id := range []string{"isb-gp", "isb-parent", "isb-child"} {
if getIsBlocked(t, ctx, store, "issues", id) {
t.Fatalf("%s should be is_blocked = 0 after gp-blocker closes", id)
}
}
}
// TestIsBlocked_CascadeDeleteClearsDepender covers spec case (6): deleting the
// blocker (which cascades the dep row) leaves the former depender unblocked.
func TestIsBlocked_CascadeDeleteClearsDepender(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
createPerm(t, ctx, store, "isb-del-blocker")
createPerm(t, ctx, store, "isb-del-blocked")
if err := store.AddDependency(ctx, &types.Dependency{
IssueID: "isb-del-blocked", DependsOnID: "isb-del-blocker", Type: types.DepBlocks,
}, "tester"); err != nil {
t.Fatalf("AddDependency: %v", err)
}
if !getIsBlocked(t, ctx, store, "issues", "isb-del-blocked") {
t.Fatal("expected blocked before delete")
}
if err := store.DeleteIssue(ctx, "isb-del-blocker"); err != nil {
t.Fatalf("DeleteIssue blocker: %v", err)
}
if getIsBlocked(t, ctx, store, "issues", "isb-del-blocked") {
t.Fatal("expected unblocked after blocker delete")
}
}
// TestIsBlocked_BatchedCreateWithDepsInOneTxn covers spec case (7): when
// CreateIssues batches both issues and a parent-child dep between them,
// is_blocked propagates correctly within the single transaction.
func TestIsBlocked_BatchedCreateWithDepsInOneTxn(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
// Seed a pre-existing blocker so the new parent will become blocked.
createPerm(t, ctx, store, "isb-batch-blocker")
parent := &types.Issue{
ID: "isb-batch-parent", Title: "p", Status: types.StatusOpen,
Priority: 2, IssueType: types.TypeTask,
Dependencies: []*types.Dependency{
{DependsOnID: "isb-batch-blocker", Type: types.DepBlocks},
},
}
child := &types.Issue{
ID: "isb-batch-child", Title: "c", Status: types.StatusOpen,
Priority: 2, IssueType: types.TypeTask,
Dependencies: []*types.Dependency{
{DependsOnID: "isb-batch-parent", Type: types.DepParentChild},
},
}
if err := store.CreateIssuesWithFullOptions(ctx, []*types.Issue{parent, child}, "tester",
storage.BatchCreateOptions{
OrphanHandling: storage.OrphanAllow,
SkipPrefixValidation: true,
}); err != nil {
t.Fatalf("CreateIssuesWithFullOptions: %v", err)
}
if !getIsBlocked(t, ctx, store, "issues", "isb-batch-parent") {
t.Fatal("parent should be is_blocked = 1 (direct blocker)")
}
if !getIsBlocked(t, ctx, store, "issues", "isb-batch-child") {
t.Fatal("child should be is_blocked = 1 (inherits from parent)")
}
}
// TestIsBlocked_ConditionalBlocksAndWaitsFor verifies that the non-'blocks'
// blocking-type deps (conditional-blocks, waits-for) also flip is_blocked
// correctly. waits-for with default gate semantics is blocked while the
// spawner is active.
func TestIsBlocked_ConditionalBlocksAndWaitsFor(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
createPerm(t, ctx, store, "isb-cb-target")
createPerm(t, ctx, store, "isb-cb-source")
if err := store.AddDependency(ctx, &types.Dependency{
IssueID: "isb-cb-source", DependsOnID: "isb-cb-target", Type: types.DepConditionalBlocks,
}, "tester"); err != nil {
t.Fatalf("AddDependency conditional-blocks: %v", err)
}
if !getIsBlocked(t, ctx, store, "issues", "isb-cb-source") {
t.Fatal("expected is_blocked = 1 via conditional-blocks")
}
}
// TestIsBlocked_WaitsForDefaultGate verifies the default ("all-children")
// gate: waiter is blocked iff at least one child of the spawner is active.
func TestIsBlocked_WaitsForDefaultGate(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
createPerm(t, ctx, store, "isb-wf-default-waiter")
createPerm(t, ctx, store, "isb-wf-default-spawner")
createPerm(t, ctx, store, "isb-wf-default-child")
// child <- spawner (parent-child), then waiter --waits-for--> spawner.
if err := store.AddDependency(ctx, &types.Dependency{
IssueID: "isb-wf-default-child", DependsOnID: "isb-wf-default-spawner", Type: types.DepParentChild,
}, "tester"); err != nil {
t.Fatalf("parent-child: %v", err)
}
if err := store.AddDependency(ctx, &types.Dependency{
IssueID: "isb-wf-default-waiter", DependsOnID: "isb-wf-default-spawner", Type: types.DepWaitsFor,
}, "tester"); err != nil {
t.Fatalf("waits-for: %v", err)
}
if !getIsBlocked(t, ctx, store, "issues", "isb-wf-default-waiter") {
t.Fatal("expected waiter blocked: active child exists under default gate")
}
if err := store.CloseIssue(ctx, "isb-wf-default-child", "done", "tester", ""); err != nil {
t.Fatalf("CloseIssue child: %v", err)
}
if getIsBlocked(t, ctx, store, "issues", "isb-wf-default-waiter") {
t.Fatal("expected waiter unblocked: all children closed under default gate")
}
}
// TestIsBlocked_WaitsForAnyChildrenGate verifies the "any-children" gate:
// waiter is unblocked once ANY child closes, even if others are still active.
func TestIsBlocked_WaitsForAnyChildrenGate(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
createPerm(t, ctx, store, "isb-wf-any-waiter")
createPerm(t, ctx, store, "isb-wf-any-spawner")
createPerm(t, ctx, store, "isb-wf-any-child-1")
createPerm(t, ctx, store, "isb-wf-any-child-2")
for _, child := range []string{"isb-wf-any-child-1", "isb-wf-any-child-2"} {
if err := store.AddDependency(ctx, &types.Dependency{
IssueID: child, DependsOnID: "isb-wf-any-spawner", Type: types.DepParentChild,
}, "tester"); err != nil {
t.Fatalf("parent-child %s: %v", child, err)
}
}
if err := store.AddDependency(ctx, &types.Dependency{
IssueID: "isb-wf-any-waiter", DependsOnID: "isb-wf-any-spawner",
Type: types.DepWaitsFor, Metadata: `{"gate":"any-children"}`,
}, "tester"); err != nil {
t.Fatalf("waits-for any-children: %v", err)
}
if !getIsBlocked(t, ctx, store, "issues", "isb-wf-any-waiter") {
t.Fatal("expected waiter blocked: no children closed yet under any-children gate")
}
// Close just one child — should unblock the waiter.
if err := store.CloseIssue(ctx, "isb-wf-any-child-1", "done", "tester", ""); err != nil {
t.Fatalf("CloseIssue first child: %v", err)
}
if getIsBlocked(t, ctx, store, "issues", "isb-wf-any-waiter") {
t.Fatal("expected waiter unblocked: any-children gate satisfied by one closed child")
}
// Reopen the closed child — waiter should be blocked again.
if err := store.ReopenIssue(ctx, "isb-wf-any-child-1", "", "tester"); err != nil {
t.Fatalf("ReopenIssue child: %v", err)
}
if !getIsBlocked(t, ctx, store, "issues", "isb-wf-any-waiter") {
t.Fatal("expected waiter re-blocked: any-children gate no longer satisfied after reopen")
}
}
// TestIsBlocked_ClosedDependerNotRemarkedActive verifies that an issue whose
// status is closed stays is_blocked = 0 even if its blocker is active.
func TestIsBlocked_ClosedDependerNotRemarkedActive(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
createPerm(t, ctx, store, "isb-cd-blocker")
createPerm(t, ctx, store, "isb-cd-depender")
if err := store.AddDependency(ctx, &types.Dependency{
IssueID: "isb-cd-depender", DependsOnID: "isb-cd-blocker", Type: types.DepBlocks,
}, "tester"); err != nil {
t.Fatalf("AddDependency: %v", err)
}
if err := store.CloseIssue(ctx, "isb-cd-depender", "done", "tester", ""); err != nil {
t.Fatalf("CloseIssue depender: %v", err)
}
if getIsBlocked(t, ctx, store, "issues", "isb-cd-depender") {
t.Fatal("closed depender should be is_blocked = 0")
}
}

View file

@ -180,11 +180,7 @@ func (s *DoltStore) UpdateIssue(ctx context.Context, id string, updates map[stri
if err := tx.Commit(); err != nil {
return wrapTransactionError("commit update issue", err)
}
// Status changes affect the active set used by blocked ID computation
if _, hasStatus := updates["status"]; hasStatus {
s.invalidateBlockedIDsCache()
}
_ = result // OldIssue available if needed for future cache invalidation
_ = result // OldIssue available if needed for future call sites
return nil
}
@ -224,8 +220,6 @@ func (s *DoltStore) ClaimIssue(ctx context.Context, id string, actor string) err
if err := tx.Commit(); err != nil {
return wrapTransactionError("commit claim issue", err)
}
// Claiming changes status to in_progress, affecting blocked ID computation
s.invalidateBlockedIDsCache()
return nil
}
@ -237,7 +231,7 @@ func (s *DoltStore) ClaimReadyIssue(ctx context.Context, filter types.WorkFilter
}
defer func() { _ = tx.Rollback() }()
claimed, err := issueops.ClaimReadyIssueInTx(ctx, tx, filter, actor, s.computeBlockedIDsForReadyWork)
claimed, err := issueops.ClaimReadyIssueInTx(ctx, tx, filter, actor)
if err != nil {
return nil, err
}
@ -257,7 +251,6 @@ func (s *DoltStore) ClaimReadyIssue(ctx context.Context, filter types.WorkFilter
if err := tx.Commit(); err != nil {
return nil, wrapTransactionError("commit claim ready issue", err)
}
s.invalidateBlockedIDsCache()
return claimed, nil
}
@ -320,8 +313,6 @@ func (s *DoltStore) CloseIssue(ctx context.Context, id string, reason string, ac
if err := tx.Commit(); err != nil {
return wrapTransactionError("commit close issue", err)
}
// Closing changes the active set, which affects blocked ID computation (GH#1495)
s.invalidateBlockedIDsCache()
return nil
}
@ -349,7 +340,6 @@ func (s *DoltStore) DeleteIssue(ctx context.Context, id string) error {
}); err != nil {
return err
}
s.invalidateBlockedIDsCache()
return nil
}
@ -433,11 +423,6 @@ func (s *DoltStore) DeleteIssues(ctx context.Context, ids []string, cascade bool
}
result.DeletedCount += wispDeleteCount
if dryRun {
return result, nil
}
s.invalidateBlockedIDsCache()
return result, nil
}
@ -629,9 +614,6 @@ func (s *DoltStore) DeleteIssuesBySourceRepo(ctx context.Context, sourceRepo str
count, err = issueops.DeleteIssuesBySourceRepoInTx(ctx, tx, sourceRepo)
return err
})
if err == nil {
s.invalidateBlockedIDsCache()
}
return count, err
}

View file

@ -126,49 +126,6 @@ func TestDemoteToWispRollsBackWhenAuxiliaryCopyFails(t *testing.T) {
}
}
func TestDemoteToWispInvalidatesBlockedCache(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
createPerm(t, ctx, store, "mixed-demote-cache-blocker")
createPerm(t, ctx, store, "mixed-demote-cache-blocked")
if err := store.AddDependency(ctx, &types.Dependency{
IssueID: "mixed-demote-cache-blocked",
DependsOnID: "mixed-demote-cache-blocker",
Type: types.DepBlocks,
}, "tester"); err != nil {
t.Fatalf("AddDependency before demote: %v", err)
}
if _, err := store.computeBlockedIDs(ctx, true); err != nil {
t.Fatalf("computeBlockedIDs: %v", err)
}
if !store.blockedIDsCached {
t.Fatal("expected blocked-ID cache to be populated before demotion")
}
if err := store.UpdateIssue(ctx, "mixed-demote-cache-blocker", map[string]interface{}{
"no_history": true,
"status": string(types.StatusClosed),
}, "tester"); err != nil {
t.Fatalf("demote closed blocker: %v", err)
}
if store.blockedIDsCached {
t.Fatal("expected demotion to invalidate blocked-ID cache")
}
blocked, _, err := store.IsBlocked(ctx, "mixed-demote-cache-blocked")
if err != nil {
t.Fatalf("IsBlocked after demotion: %v", err)
}
if blocked {
t.Fatal("issue remained blocked after its demoted blocker was closed")
}
}
func TestDemoteToWispLeavesIgnoredWispTablesUnstaged(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()

View file

@ -21,24 +21,50 @@ func (s *DoltStore) SearchIssues(ctx context.Context, query string, filter types
return result, err
}
// SearchIssuesWithCounts returns matching issues hydrated with labels,
// dependency records, dep/dependent/comment counts and parent ID in one
// SQL statement (plus a wisp-empty probe). Used by bd list --json to
// avoid the SearchIssues + 3-call hydration sequence.
func (s *DoltStore) SearchIssuesWithCounts(ctx context.Context, query string, filter types.IssueFilter) ([]*types.IssueWithCounts, error) {
var result []*types.IssueWithCounts
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
result, err = issueops.SearchIssuesWithCountsInTx(ctx, tx, query, filter)
return err
})
return result, err
}
// GetReadyWork returns issues that are ready to work on (not blocked).
//
// Blocking semantics are unified through issueops.GetReadyWorkInTx.
// Blocking semantics are unified through issueops.GetReadyWorkInTx, which
// consults the stored is_blocked column maintained by write-side
// instrumentation.
func (s *DoltStore) GetReadyWork(ctx context.Context, filter types.WorkFilter) ([]*types.Issue, error) {
var result []*types.Issue
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
result, err = issueops.GetReadyWorkInTx(ctx, tx, filter, s.computeBlockedIDsForReadyWork)
result, err = issueops.GetReadyWorkInTx(ctx, tx, filter)
return err
})
return result, err
}
// GetReadyWorkWithCounts returns ready issues with labels, dependency records,
// dep/dependent/comment counts and parent ID hydrated in one SQL statement
// (plus the wisp-empty probe). Used by bd ready --json to avoid the 5-call
// per-issue hydration the legacy path would otherwise issue.
func (s *DoltStore) GetReadyWorkWithCounts(ctx context.Context, filter types.WorkFilter) ([]*types.IssueWithCounts, error) {
var result []*types.IssueWithCounts
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
result, err = issueops.GetReadyWorkWithCountsInTx(ctx, tx, filter)
return err
})
return result, err
}
// GetBlockedIssues returns issues that are blocked by other issues.
// Uses separate single-table queries with Go-level filtering to avoid
// correlated EXISTS subqueries that trigger Dolt's joinIter panic
// (slice bounds out of range at join_iters.go:192).
// Same fix pattern as GetStatistics blocked count (fc16065c, a4a21958).
func (s *DoltStore) GetBlockedIssues(ctx context.Context, filter types.WorkFilter) ([]*types.BlockedIssue, error) {
var result []*types.BlockedIssue
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
@ -82,18 +108,19 @@ func (s *DoltStore) GetStatistics(ctx context.Context) (*types.Statistics, error
return nil, fmt.Errorf("failed to get statistics: %w", err)
}
// Blocked count: reuse computeBlockedIDs which caches the result across
// GetReadyWork and GetStatistics calls within the same CLI invocation.
// Blocked count: read directly from the stored is_blocked column.
var blockedCount int
blockedIDs, err := s.computeBlockedIDs(ctx, true)
if err == nil {
blockedCount = len(blockedIDs)
if err := s.withReadTx(ctx, func(tx *sql.Tx) error {
return tx.QueryRowContext(ctx, `
SELECT COUNT(*) FROM issues
WHERE is_blocked = 1 AND status <> 'closed' AND status <> 'pinned'
`).Scan(&blockedCount)
}); err != nil {
return nil, fmt.Errorf("failed to count blocked issues: %w", err)
}
stats.BlockedIssues = blockedCount
// Ready count: compute without using the ready_issues view to avoid
// recursive CTE join that triggers the same Dolt panic.
// Ready = open, non-ephemeral, not blocked (directly or transitively).
// Ready count: derive from open count minus blocked count.
stats.ReadyIssues = stats.OpenIssues - blockedCount
if stats.ReadyIssues < 0 {
stats.ReadyIssues = 0
@ -102,95 +129,6 @@ func (s *DoltStore) GetStatistics(ctx context.Context) (*types.Statistics, error
return stats, nil
}
// computeBlockedIDs returns the set of issue IDs that are blocked by active issues.
// Uses separate single-table queries with Go-level filtering to avoid Dolt's
// joinIter panic (slice bounds out of range at join_iters.go:192).
// Results are cached per DoltStore lifetime and invalidated when dependencies
// change (AddDependency, RemoveDependency).
//
// When includeWisps is false, only the issues/dependencies tables are scanned,
// skipping the wisps/wisp_dependencies tables. This is safe when the caller only
// needs blocked status for non-ephemeral issues (no cross-table blocking deps exist).
// A cached result from includeWisps=true satisfies includeWisps=false requests.
//
// Caller must hold s.mu (at least RLock).
func (s *DoltStore) computeBlockedIDs(ctx context.Context, includeWisps bool) ([]string, error) {
var result []string
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
result, err = s.computeBlockedIDsForReadyWork(ctx, tx, includeWisps)
return err
})
if err != nil {
return nil, err
}
return result, nil
}
func (s *DoltStore) computeBlockedIDsForReadyWork(ctx context.Context, tx *sql.Tx, includeWisps bool) ([]string, error) {
s.cacheMu.Lock()
if s.blockedIDsCached && (s.blockedIDsCacheIncludesWisps || !includeWisps) {
result := s.blockedIDsCache
s.cacheMu.Unlock()
return result, nil
}
s.cacheMu.Unlock()
result, _, err := issueops.ComputeBlockedIDsInTx(ctx, tx, includeWisps)
if err != nil {
return nil, err
}
blockedSet := make(map[string]bool, len(result))
for _, id := range result {
blockedSet[id] = true
}
s.cacheMu.Lock()
defer s.cacheMu.Unlock()
if s.blockedIDsCached && (s.blockedIDsCacheIncludesWisps || !includeWisps) {
return s.blockedIDsCache, nil
}
s.blockedIDsCache = result
s.blockedIDsCacheMap = blockedSet
s.blockedIDsCached = true
s.blockedIDsCacheIncludesWisps = includeWisps
return result, nil
}
// invalidateBlockedIDsCache clears the blocked IDs cache so the next call
// to computeBlockedIDs will recompute from the database.
func (s *DoltStore) invalidateBlockedIDsCache() {
s.cacheMu.Lock()
s.blockedIDsCached = false
s.blockedIDsCache = nil
s.blockedIDsCacheMap = nil
s.blockedIDsCacheIncludesWisps = false
s.cacheMu.Unlock()
}
// getChildrenWithParents returns a map of childID -> parentID for direct children
// (parent-child deps) of the given parent IDs.
func (s *DoltStore) getChildrenWithParents(ctx context.Context, parentIDs []string) (map[string]string, error) {
var result map[string]string
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
result, err = issueops.GetChildrenWithParentsInTx(ctx, tx, parentIDs)
return err
})
return result, err
}
func (s *DoltStore) getDescendantIDs(ctx context.Context, rootID string) ([]string, error) {
var result []string
err := s.withReadTx(ctx, func(tx *sql.Tx) error {
var err error
result, err = issueops.GetDescendantIDsInTx(ctx, tx, rootID, 0)
return err
})
return result, err
}
// GetMoleculeProgress returns progress stats for a molecule
func (s *DoltStore) GetMoleculeProgress(ctx context.Context, moleculeID string) (*types.MoleculeProgressStats, error) {
stats := &types.MoleculeProgressStats{
@ -215,9 +153,6 @@ func (s *DoltStore) GetMoleculeProgress(ctx context.Context, moleculeID string)
stats.MoleculeTitle = title.String
}
// Use separate single-table queries to avoid Dolt's joinIter panic
// (join_iters.go:192) which triggers on JOIN between issues and dependencies.
// Step 1: Get child issue IDs from dependencies table (single-table scan)
//nolint:gosec // G201: depTable and parentCol are hardcoded
depRows, err := s.queryContext(ctx, fmt.Sprintf(`

View file

@ -701,6 +701,19 @@ func TestGetReadyWork_IncludeEphemeralPropagatesWispSearchError(t *testing.T) {
}, "tester"); err != nil {
t.Fatalf("create ready issue: %v", err)
}
// Insert a wisp first so the wisp-empty short-circuit in
// getReadyWispsInTx does not skip the search. We damage the table
// after the row exists so the probe still finds it.
if err := store.CreateIssue(ctx, &types.Issue{
ID: "rw-wisp-error-wisp",
Title: "Wisp control",
Status: types.StatusOpen,
Priority: 1,
IssueType: types.TypeTask,
Ephemeral: true,
}, "tester"); err != nil {
t.Fatalf("create wisp: %v", err)
}
if _, err := store.db.ExecContext(ctx, "ALTER TABLE wisps DROP COLUMN title"); err != nil {
t.Fatalf("damage wisps table for regression test: %v", err)
}
@ -788,92 +801,6 @@ func TestGetReadyWork_TypePriorityLimitUsesDoltSafeTypePredicate(t *testing.T) {
}
}
func TestGetReadyWork_UnboundedPopulatesBlockedIDsCache(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
blocker := &types.Issue{ID: "test-rw-cache-blocker", Title: "Blocker", Status: types.StatusOpen, Priority: 1, IssueType: types.TypeTask}
blocked := &types.Issue{ID: "test-rw-cache-blocked", Title: "Blocked", Status: types.StatusOpen, Priority: 1, IssueType: types.TypeTask}
if err := store.CreateIssues(ctx, []*types.Issue{blocker, blocked}, "tester"); err != nil {
t.Fatalf("create cache issues: %v", err)
}
if err := store.AddDependency(ctx, &types.Dependency{IssueID: blocked.ID, DependsOnID: blocker.ID, Type: types.DepBlocks}, "tester"); err != nil {
t.Fatalf("add cache dependency: %v", err)
}
if _, err := store.GetReadyWork(ctx, types.WorkFilter{}); err != nil {
t.Fatalf("first ready work: %v", err)
}
if !store.blockedIDsCached {
t.Fatal("expected unbounded ready work to populate blocked IDs cache")
}
firstCache := store.blockedIDsCache
if len(firstCache) == 0 {
t.Fatal("expected blocked IDs cache to contain blocked issue")
}
firstCacheEntry := &firstCache[0]
if _, err := store.GetReadyWork(ctx, types.WorkFilter{}); err != nil {
t.Fatalf("second ready work: %v", err)
}
if !store.blockedIDsCached || len(store.blockedIDsCache) == 0 {
t.Fatal("expected blocked IDs cache to remain populated")
}
if &store.blockedIDsCache[0] != firstCacheEntry {
t.Fatal("expected consecutive unbounded ready-work calls to reuse the blocked IDs cache")
}
}
func TestClaimReadyIssue_UnboundedPopulatesAndReusesBlockedIDsCache(t *testing.T) {
store, cleanup := setupTestStore(t)
defer cleanup()
ctx, cancel := testContext(t)
defer cancel()
blocker := &types.Issue{ID: "test-claim-cache-blocker", Title: "Blocker", Status: types.StatusOpen, Priority: 1, IssueType: types.TypeTask}
blocked := &types.Issue{ID: "test-claim-cache-blocked", Title: "Blocked", Status: types.StatusOpen, Priority: 1, IssueType: types.TypeBug}
if err := store.CreateIssues(ctx, []*types.Issue{blocker, blocked}, "tester"); err != nil {
t.Fatalf("create claim cache issues: %v", err)
}
if err := store.AddDependency(ctx, &types.Dependency{IssueID: blocked.ID, DependsOnID: blocker.ID, Type: types.DepBlocks}, "tester"); err != nil {
t.Fatalf("add claim cache dependency: %v", err)
}
filter := types.WorkFilter{Type: string(types.TypeBug)}
claimed, err := store.ClaimReadyIssue(ctx, filter, "tester")
if err != nil {
t.Fatalf("first claim ready issue: %v", err)
}
if claimed != nil {
t.Fatalf("first claim = %s, want nil because bug is blocked", claimed.ID)
}
if !store.blockedIDsCached {
t.Fatal("expected claim path to populate blocked IDs cache")
}
firstCache := store.blockedIDsCache
if len(firstCache) == 0 {
t.Fatal("expected blocked IDs cache to contain blocked issue")
}
firstCacheEntry := &firstCache[0]
claimed, err = store.ClaimReadyIssue(ctx, filter, "tester")
if err != nil {
t.Fatalf("second claim ready issue: %v", err)
}
if claimed != nil {
t.Fatalf("second claim = %s, want nil because bug is blocked", claimed.ID)
}
if !store.blockedIDsCached || len(store.blockedIDsCache) == 0 {
t.Fatal("expected blocked IDs cache to remain populated")
}
if &store.blockedIDsCache[0] != firstCacheEntry {
t.Fatal("expected consecutive claim attempts to reuse the blocked IDs cache")
}
}
func readyIDSet(ids []string) map[string]bool {
result := make(map[string]bool, len(ids))
for _, id := range ids {

View file

@ -163,18 +163,14 @@ type DoltStore struct {
credentialKey []byte // Random encryption key for federation credentials
// Per-invocation caches (lifetime = DoltStore lifetime)
customStatusDetailedCache []types.CustomStatus // cached result of GetCustomStatusesDetailed
customStatusCache []string // cached name-only result (derived from detailed)
customStatusCached bool // true once cache has been populated
customTypeCache []string // cached result of GetCustomTypes
customTypeCached bool // true once customTypeCache has been populated
infraTypeCache map[string]bool // cached result of GetInfraTypes
infraTypeCached bool // true once infraTypeCache has been populated
blockedIDsCache []string // cached result of computeBlockedIDs
blockedIDsCacheMap map[string]bool
blockedIDsCached bool // true once blockedIDsCache has been populated
blockedIDsCacheIncludesWisps bool // true if cache was computed with wisps
cacheMu sync.Mutex
customStatusDetailedCache []types.CustomStatus // cached result of GetCustomStatusesDetailed
customStatusCache []string // cached name-only result (derived from detailed)
customStatusCached bool // true once cache has been populated
customTypeCache []string // cached result of GetCustomTypes
customTypeCached bool // true once customTypeCache has been populated
infraTypeCache map[string]bool // cached result of GetInfraTypes
infraTypeCached bool // true once infraTypeCache has been populated
cacheMu sync.Mutex
// OTel span attribute cache (avoids per-call allocation)
spanAttrsOnce sync.Once

View file

@ -329,7 +329,6 @@ func (t *doltTransaction) SearchIssues(ctx context.Context, query string, filter
whereClauses = append(whereClauses, fmt.Sprintf("status NOT IN (%s)", strings.Join(placeholders, ",")))
}
// Type filtering — use subquery to prevent Dolt mergeJoinIter panic
if len(filter.ExcludeTypes) > 0 {
placeholders := make([]string, len(filter.ExcludeTypes))
for i, tp := range filter.ExcludeTypes {
@ -354,7 +353,6 @@ func (t *doltTransaction) SearchIssues(ctx context.Context, query string, filter
args = append(args, *filter.PriorityMax)
}
// Issue type — use subquery to prevent Dolt mergeJoinIter panic
if filter.IssueType != nil {
//nolint:gosec // G201: table is hardcoded to "issues" or "wisps"
whereClauses = append(whereClauses, fmt.Sprintf("id IN (SELECT id FROM %s WHERE issue_type = ?)", table))
@ -598,91 +596,60 @@ func (t *doltTransaction) SearchIssues(ctx context.Context, query string, filter
return issues, nil
}
// UpdateIssue updates an issue within the transaction
// UpdateIssue updates an issue within the transaction. Delegates to the
// issueops layer so is_blocked is maintained transactionally.
func (t *doltTransaction) UpdateIssue(ctx context.Context, id string, updates map[string]interface{}, actor string) error {
table := "issues"
if t.isActiveWisp(ctx, id) {
table = "wisps"
}
setClauses := []string{"updated_at = ?"}
args := []interface{}{time.Now().UTC()}
for key, value := range updates {
if !isAllowedUpdateField(key) {
return fmt.Errorf("invalid field for update: %s", key)
// Validate metadata up front (kept inline since callers expect this
// pre-validation error path before any UPDATE runs).
if rawMeta, ok := updates["metadata"]; ok {
metadataStr, err := storage.NormalizeMetadataValue(rawMeta)
if err != nil {
return fmt.Errorf("invalid metadata: %w", err)
}
columnName := key
if key == "wisp" {
columnName = "ephemeral"
}
setClauses = append(setClauses, fmt.Sprintf("`%s` = ?", columnName))
// Handle JSON serialization for array fields stored as TEXT
if key == "waiters" {
waitersJSON, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("invalid waiters: %w", err)
}
args = append(args, string(waitersJSON))
} else if key == "metadata" {
// GH#1417: Normalize metadata to string, accepting string/[]byte/json.RawMessage
metadataStr, err := storage.NormalizeMetadataValue(value)
if err != nil {
return fmt.Errorf("invalid metadata: %w", err)
}
// Validate against schema if configured (GH#1416 Phase 2)
if err := validateMetadataIfConfigured(json.RawMessage(metadataStr)); err != nil {
return err
}
args = append(args, metadataStr)
} else {
args = append(args, value)
if err := validateMetadataIfConfigured(json.RawMessage(metadataStr)); err != nil {
return err
}
}
args = append(args, id)
//nolint:gosec // G201: table is hardcoded, setClauses contains only column names
querySQL := fmt.Sprintf("UPDATE %s SET %s WHERE id = ?", table, strings.Join(setClauses, ", "))
_, err := t.txFor(table).ExecContext(ctx, querySQL, args...)
if err == nil {
t.dirty.MarkDirty(table)
if _, err := issueops.UpdateIssueWithoutEventInTx(ctx, t.txFor(table), id, updates, actor); err != nil {
return wrapExecError("update issue in tx", err)
}
return wrapExecError("update issue in tx", err)
t.dirty.MarkDirty(table)
return nil
}
// CloseIssue closes an issue within the transaction
// CloseIssue closes an issue within the transaction. Delegates to the
// issueops layer (event-free variant) so is_blocked is maintained.
func (t *doltTransaction) CloseIssue(ctx context.Context, id string, reason string, actor string, session string) error {
table := "issues"
if t.isActiveWisp(ctx, id) {
table = "wisps"
}
now := time.Now().UTC()
//nolint:gosec // G201: table is hardcoded
_, err := t.txFor(table).ExecContext(ctx, fmt.Sprintf(`
UPDATE %s SET status = ?, closed_at = ?, updated_at = ?, close_reason = ?, closed_by_session = ?
WHERE id = ?
`, table), types.StatusClosed, now, now, reason, session, id)
if err == nil {
t.dirty.MarkDirty(table)
if _, err := issueops.CloseIssueWithoutEventInTx(ctx, t.txFor(table), id, reason, actor, session); err != nil {
return wrapExecError("close issue in tx", err)
}
return wrapExecError("close issue in tx", err)
t.dirty.MarkDirty(table)
return nil
}
// DeleteIssue deletes an issue within the transaction
// DeleteIssue deletes an issue within the transaction. Delegates to the
// issueops layer so is_blocked is maintained for survivors of the cascade.
func (t *doltTransaction) DeleteIssue(ctx context.Context, id string) error {
table := "issues"
if t.isActiveWisp(ctx, id) {
table = "wisps"
}
//nolint:gosec // G201: table is hardcoded
_, err := t.txFor(table).ExecContext(ctx, fmt.Sprintf("DELETE FROM %s WHERE id = ?", table), id)
if err == nil {
t.dirty.MarkDirty(table)
if err := issueops.DeleteIssueInTx(ctx, t.txFor(table), id); err != nil {
return wrapExecError("delete issue in tx", err)
}
return wrapExecError("delete issue in tx", err)
t.dirty.MarkDirty(table)
return nil
}
// AddDependency adds a dependency within the transaction.
@ -724,7 +691,6 @@ func (t *doltTransaction) AddDependencyWithOptions(ctx context.Context, dep *typ
return err
}
t.dirty.MarkDirty(table)
t.store.invalidateBlockedIDsCache()
return nil
}
@ -764,21 +730,18 @@ func (t *doltTransaction) GetDependencyRecords(ctx context.Context, issueID stri
return deps, rows.Err()
}
// RemoveDependency removes a dependency within the transaction
// RemoveDependency removes a dependency within the transaction. Delegates
// to the issueops layer so is_blocked is maintained.
func (t *doltTransaction) RemoveDependency(ctx context.Context, issueID, dependsOnID string, actor string) error {
table := "dependencies"
if t.isActiveWisp(ctx, issueID) {
table = "wisp_dependencies"
}
//nolint:gosec // G201: table is hardcoded
_, err := t.txFor(table).ExecContext(ctx, fmt.Sprintf(`
DELETE FROM %s WHERE issue_id = ? AND %s = ?
`, table, issueops.DepTargetExpr), issueID, dependsOnID)
if err == nil {
t.dirty.MarkDirty(table)
if err := issueops.RemoveDependencyInTx(ctx, t.txFor(table), issueID, dependsOnID); err != nil {
return wrapExecError("remove dependency in tx", err)
}
return wrapExecError("remove dependency in tx", err)
t.dirty.MarkDirty(table)
return nil
}
// AddLabel adds a label within the transaction

View file

@ -224,6 +224,11 @@ func (s *DoltStore) deleteWisp(ctx context.Context, id string) error {
}
defer func() { _ = tx.Rollback() }()
affectedIssues, affectedWisps, aerr := issueops.AffectedByDeletionInTx(ctx, tx, nil, []string{id})
if aerr != nil {
return fmt.Errorf("affected by wisp delete for %s: %w", id, aerr)
}
result, err := tx.ExecContext(ctx, "DELETE FROM wisps WHERE id = ?", id)
if err != nil {
return fmt.Errorf("failed to delete wisp: %w", err)
@ -241,6 +246,10 @@ func (s *DoltStore) deleteWisp(ctx context.Context, id string) error {
return err
}
if err := issueops.RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
return fmt.Errorf("recompute is_blocked after wisp delete for %s: %w", id, err)
}
return wrapTransactionError("commit delete wisp", tx.Commit())
}
@ -288,6 +297,11 @@ func (s *DoltStore) deleteWispBatchTx(ctx context.Context, ids []string) (int, e
}
defer func() { _ = tx.Rollback() }()
affectedIssues, affectedWisps, aerr := issueops.AffectedByDeletionInTx(ctx, tx, nil, ids)
if aerr != nil {
return 0, fmt.Errorf("affected by batched wisp delete: %w", aerr)
}
inClause, args := doltBuildSQLInClause(ids)
//nolint:gosec // G201: inClause contains only ? markers
@ -303,6 +317,10 @@ func (s *DoltStore) deleteWispBatchTx(ctx context.Context, ids []string) (int, e
return 0, err
}
if err := issueops.RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
return 0, fmt.Errorf("recompute is_blocked after batched wisp delete: %w", err)
}
if err := tx.Commit(); err != nil {
return 0, fmt.Errorf("failed to commit batch wisp delete: %w", err)
}
@ -550,6 +568,14 @@ func (s *DoltStore) addWispDependency(ctx context.Context, dep *types.Dependency
return fmt.Errorf("failed to add wisp dependency: %w", err)
}
affectedIssues, affectedWisps, aerr := issueops.AffectedByDepChangeForWispInTx(ctx, tx, dep.IssueID, dep.DependsOnID, dep.Type)
if aerr != nil {
return fmt.Errorf("affected by add wisp dependency %s -> %s: %w", dep.IssueID, dep.DependsOnID, aerr)
}
if err := issueops.RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
return fmt.Errorf("recompute is_blocked after add wisp dependency %s -> %s: %w", dep.IssueID, dep.DependsOnID, err)
}
return wrapTransactionError("commit add wisp dependency", tx.Commit())
}

View file

@ -27,7 +27,7 @@ func (s *EmbeddedDoltStore) ClaimReadyIssue(ctx context.Context, filter types.Wo
var claimed *types.Issue
err := s.withConn(ctx, true, func(tx *sql.Tx) error {
var err error
claimed, err = issueops.ClaimReadyIssueInTx(ctx, tx, filter, actor, computeBlockedIDsWrapper)
claimed, err = issueops.ClaimReadyIssueInTx(ctx, tx, filter, actor)
return err
})
return claimed, err

View file

@ -20,6 +20,19 @@ func (s *EmbeddedDoltStore) SearchIssues(ctx context.Context, query string, filt
return result, err
}
// SearchIssuesWithCounts returns matching issues hydrated with labels,
// dependency records, dep/dependent/comment counts and parent ID in one SQL
// statement. See DoltStore.SearchIssuesWithCounts for details.
func (s *EmbeddedDoltStore) SearchIssuesWithCounts(ctx context.Context, query string, filter types.IssueFilter) ([]*types.IssueWithCounts, error) {
var result []*types.IssueWithCounts
err := s.withConn(ctx, false, func(tx *sql.Tx) error {
var err error
result, err = issueops.SearchIssuesWithCountsInTx(ctx, tx, query, filter)
return err
})
return result, err
}
func (s *EmbeddedDoltStore) ListWisps(ctx context.Context, filter types.WispFilter) ([]*types.Issue, error) {
issueFilter := issueops.WispFilterToIssueFilter(filter)
var result []*types.Issue

View file

@ -11,22 +11,29 @@ import (
)
// GetReadyWork returns issues that are ready to work on (not blocked).
// Delegates to issueops.GetReadyWorkInTx with the shared blocked-ID computation.
// Delegates to issueops.GetReadyWorkInTx, which consults the stored
// is_blocked column maintained by write-side instrumentation.
func (s *EmbeddedDoltStore) GetReadyWork(ctx context.Context, filter types.WorkFilter) ([]*types.Issue, error) {
var result []*types.Issue
err := s.withConn(ctx, false, func(tx *sql.Tx) error {
var err error
result, err = issueops.GetReadyWorkInTx(ctx, tx, filter, computeBlockedIDsWrapper)
result, err = issueops.GetReadyWorkInTx(ctx, tx, filter)
return err
})
return result, err
}
// computeBlockedIDsWrapper adapts ComputeBlockedIDsInTx to the callback
// signature expected by GetReadyWorkInTx.
func computeBlockedIDsWrapper(ctx context.Context, tx *sql.Tx, includeWisps bool) ([]string, error) {
ids, _, err := issueops.ComputeBlockedIDsInTx(ctx, tx, includeWisps)
return ids, err
// GetReadyWorkWithCounts returns ready issues with labels, dependency
// records, dep/dependent/comment counts and parent ID hydrated in one SQL
// statement. See DoltStore.GetReadyWorkWithCounts for details.
func (s *EmbeddedDoltStore) GetReadyWorkWithCounts(ctx context.Context, filter types.WorkFilter) ([]*types.IssueWithCounts, error) {
var result []*types.IssueWithCounts
err := s.withConn(ctx, false, func(tx *sql.Tx) error {
var err error
result, err = issueops.GetReadyWorkWithCountsInTx(ctx, tx, filter)
return err
})
return result, err
}
// GetMoleculeProgress returns progress stats for a molecule.

View file

@ -18,11 +18,14 @@ func (s *EmbeddedDoltStore) GetStatistics(ctx context.Context) (*types.Statistic
return err
}
blockedIDs, _, err := issueops.ComputeBlockedIDsInTx(ctx, tx, true)
if err != nil {
var blockedCount int
if err := tx.QueryRowContext(ctx, `
SELECT COUNT(*) FROM issues
WHERE is_blocked = 1 AND status <> 'closed' AND status <> 'pinned'
`).Scan(&blockedCount); err != nil {
return err
}
stats.BlockedIssues = len(blockedIDs)
stats.BlockedIssues = blockedCount
stats.ReadyIssues = stats.OpenIssues - stats.BlockedIssues
if stats.ReadyIssues < 0 {
stats.ReadyIssues = 0

View file

@ -2,9 +2,9 @@ package issueops
import "github.com/steveyegge/beads/internal/storage/dberrors"
// queryBatchSize limits the number of IDs in a single IN (...) clause.
// Large IN clauses cause Dolt query-planner spikes and MySQL packet-size
// issues. 200 matches the value used in the dolt package historically.
// queryBatchSize limits the number of IDs in a single IN (...) clause to
// stay under MySQL's max_allowed_packet. 200 matches the value used in the
// dolt package historically.
const queryBatchSize = 200
// isTableNotExistError returns true if the error indicates a missing table

View file

@ -19,373 +19,17 @@ func optionalBlockedTable(table string) bool {
return table == "wisps" || table == "wisp_dependencies"
}
// ComputeBlockedIDsInTx returns the set of issue IDs that are blocked by active issues.
// This is the core computation without caching — callers manage their own cache.
// The returned active ID set is owned by the caller, but waits-for evaluation
// may expand it with active children discovered while resolving spawner deps.
//
//nolint:gosec // G201: tables are hardcoded
func ComputeBlockedIDsInTx(ctx context.Context, tx *sql.Tx, includeWisps bool) ([]string, map[string]bool, error) {
issueTables := []string{"issues"}
depTables := []string{"dependencies"}
if includeWisps {
issueTables = append(issueTables, "wisps")
depTables = append(depTables, "wisp_dependencies")
}
// Step 1: Get all active issue IDs
activeIDs := make(map[string]bool)
for _, table := range issueTables {
activeRows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT id FROM %s
WHERE status NOT IN ('closed', 'pinned')
`, table))
if err != nil {
if optionalBlockedTable(table) && isTableNotExistError(err) {
continue
}
return nil, nil, fmt.Errorf("compute blocked IDs: active issues from %s: %w", table, err)
}
for activeRows.Next() {
var id string
if err := activeRows.Scan(&id); err != nil {
_ = activeRows.Close()
return nil, nil, fmt.Errorf("compute blocked IDs: scan active issue: %w", err)
}
activeIDs[id] = true
}
_ = activeRows.Close()
if err := activeRows.Err(); err != nil {
return nil, nil, fmt.Errorf("compute blocked IDs: active rows from %s: %w", table, err)
}
}
activeIDList := make([]string, 0, len(activeIDs))
for id := range activeIDs {
activeIDList = append(activeIDList, id)
}
// This only narrows the expensive scan when inactive or closed rows dominate.
// Active-heavy repositories still pay one indexed lookup per queryBatchSize
// active IDs, so callers should not treat this as a universal speedup.
allDeps, err := loadBlockingDepsForIssueIDsInTx(ctx, tx, depTables, activeIDList)
if err != nil {
return nil, nil, err
}
blockedSet := make(map[string]bool)
if err := markBlockedFromDepsInTx(ctx, tx, issueTables, depTables, allDeps, activeIDs, blockedSet); err != nil {
return nil, nil, err
}
result := make([]string, 0, len(blockedSet))
for id := range blockedSet {
result = append(result, id)
}
return result, activeIDs, nil
}
// ComputeBlockedCandidateIDsInTx returns the subset of candidate IDs that are
// blocked. It is the limited-query counterpart to ComputeBlockedIDsInTx: callers
// that only need a small page can avoid scanning the full dependency graph.
func ComputeBlockedCandidateIDsInTx(ctx context.Context, tx *sql.Tx, candidateIDs []string, includeWisps bool) ([]string, error) {
if len(candidateIDs) == 0 {
return nil, nil
}
issueTables := []string{"issues"}
depTables := []string{"dependencies"}
if includeWisps {
issueTables = append(issueTables, "wisps")
depTables = append(depTables, "wisp_dependencies")
}
candidateSet, err := loadActiveIDSetInTx(ctx, tx, issueTables, candidateIDs)
if err != nil {
return nil, fmt.Errorf("compute blocked candidates: active candidates: %w", err)
}
if len(candidateSet) == 0 {
return nil, nil
}
activeCandidateIDs := make([]string, 0, len(candidateSet))
for id := range candidateSet {
activeCandidateIDs = append(activeCandidateIDs, id)
}
deps, err := loadBlockingDepsForIssueIDsInTx(ctx, tx, depTables, activeCandidateIDs)
if err != nil {
return nil, err
}
blockerIDs := make([]string, 0, len(deps))
for _, dep := range deps {
blockerIDs = append(blockerIDs, dep.dependsOnID)
}
activeBlockers, err := loadActiveIDSetInTx(ctx, tx, issueTables, blockerIDs)
if err != nil {
return nil, fmt.Errorf("compute blocked candidates: active blockers: %w", err)
}
blockedSet := make(map[string]bool)
for id := range candidateSet {
activeBlockers[id] = true
}
if err := markBlockedFromDepsInTx(ctx, tx, issueTables, depTables, deps, activeBlockers, blockedSet); err != nil {
return nil, err
}
childParents, err := loadParentIDsForChildrenInTx(ctx, tx, depTables, activeCandidateIDs)
if err != nil {
return nil, err
}
if len(childParents) > 0 {
parentIDs := make([]string, 0, len(childParents))
for _, parentID := range childParents {
parentIDs = append(parentIDs, parentID)
}
parentSet, err := loadActiveIDSetInTx(ctx, tx, issueTables, parentIDs)
if err != nil {
return nil, fmt.Errorf("compute blocked candidates: active parents: %w", err)
}
if len(parentSet) == 0 {
return resultFromBlockedSet(blockedSet), nil
}
activeParentIDs := make([]string, 0, len(parentSet))
for parentID := range parentSet {
activeParentIDs = append(activeParentIDs, parentID)
}
parentDeps, err := loadBlockingDepsForIssueIDsInTx(ctx, tx, depTables, activeParentIDs)
if err != nil {
return nil, err
}
parentBlockerIDs := make([]string, 0, len(parentDeps))
for _, dep := range parentDeps {
parentBlockerIDs = append(parentBlockerIDs, dep.dependsOnID)
}
activeParentBlockers, err := loadActiveIDSetInTx(ctx, tx, issueTables, parentBlockerIDs)
if err != nil {
return nil, fmt.Errorf("compute blocked candidates: active parent blockers: %w", err)
}
for parentID := range parentSet {
activeParentBlockers[parentID] = true
}
parentBlockedSet := make(map[string]bool)
if err := markBlockedFromDepsInTx(ctx, tx, issueTables, depTables, parentDeps, activeParentBlockers, parentBlockedSet); err != nil {
return nil, err
}
for childID, parentID := range childParents {
if parentBlockedSet[parentID] && parentSet[parentID] {
blockedSet[childID] = true
}
}
}
return resultFromBlockedSet(blockedSet), nil
}
func resultFromBlockedSet(blockedSet map[string]bool) []string {
result := make([]string, 0, len(blockedSet))
for id := range blockedSet {
result = append(result, id)
}
return result
}
type candidateWaitsForDep struct {
issueID string
spawnerID string
gate string
}
// markBlockedFromDepsInTx evaluates preloaded blocking dependency rows.
// activeIDs must be caller-owned: waits-for handling expands it with active
// children discovered from spawner dependencies so downstream callers can reuse
// the same active lookup for transitively blocked children.
func markBlockedFromDepsInTx(
ctx context.Context,
tx *sql.Tx,
issueTables []string,
depTables []string,
deps []blockingDepRecord,
activeIDs map[string]bool,
blockedSet map[string]bool,
) error {
var waitsForDeps []candidateWaitsForDep
needsClosedChildren := false
for _, rec := range deps {
switch rec.depType {
case string(types.DepBlocks), string(types.DepConditionalBlocks):
if activeIDs[rec.issueID] && activeIDs[rec.dependsOnID] {
blockedSet[rec.issueID] = true
}
case string(types.DepWaitsFor):
if !activeIDs[rec.issueID] {
continue
}
gate := types.ParseWaitsForGateMetadata(rec.metadata.String)
if gate == types.WaitsForAnyChildren {
needsClosedChildren = true
}
waitsForDeps = append(waitsForDeps, candidateWaitsForDep{
issueID: rec.issueID,
spawnerID: rec.dependsOnID,
gate: gate,
})
}
}
if len(waitsForDeps) == 0 {
return nil
}
spawnerIDs := make(map[string]struct{})
for _, dep := range waitsForDeps {
spawnerIDs[dep.spawnerID] = struct{}{}
}
allSpawnerIDs := make([]string, 0, len(spawnerIDs))
for spawnerID := range spawnerIDs {
allSpawnerIDs = append(allSpawnerIDs, spawnerID)
}
spawnerChildren := make(map[string][]string)
childIDs := make(map[string]struct{})
for _, depTbl := range depTables {
for start := 0; start < len(allSpawnerIDs); start += queryBatchSize {
end := start + queryBatchSize
if end > len(allSpawnerIDs) {
end = len(allSpawnerIDs)
}
placeholders, args := buildSQLInClause(allSpawnerIDs[start:end])
childRows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id, %s AS depends_on_id FROM %s
WHERE type = 'parent-child' AND %s IN (%s)
`, DepTargetExpr, depTbl, DepTargetExpr, placeholders), args...)
if err != nil {
if optionalBlockedTable(depTbl) && isTableNotExistError(err) {
continue
}
return fmt.Errorf("compute blocked IDs: children from %s: %w", depTbl, err)
}
for childRows.Next() {
var childID, parentID string
if err := childRows.Scan(&childID, &parentID); err != nil {
_ = childRows.Close()
return fmt.Errorf("compute blocked IDs: scan child: %w", err)
}
spawnerChildren[parentID] = append(spawnerChildren[parentID], childID)
childIDs[childID] = struct{}{}
}
_ = childRows.Close()
if err := childRows.Err(); err != nil {
return fmt.Errorf("compute blocked IDs: child rows from %s: %w", depTbl, err)
}
}
}
closedChildren := make(map[string]bool)
if len(childIDs) > 0 {
allChildIDs := make([]string, 0, len(childIDs))
for childID := range childIDs {
allChildIDs = append(allChildIDs, childID)
}
activeChildren, err := loadActiveIDSetInTx(ctx, tx, issueTables, allChildIDs)
if err != nil {
return fmt.Errorf("compute blocked IDs: active children: %w", err)
}
for childID := range activeChildren {
activeIDs[childID] = true
}
if needsClosedChildren {
for _, issueTbl := range issueTables {
for start := 0; start < len(allChildIDs); start += queryBatchSize {
end := start + queryBatchSize
if end > len(allChildIDs) {
end = len(allChildIDs)
}
placeholders, args := buildSQLInClause(allChildIDs[start:end])
closedRows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT id FROM %s
WHERE status = 'closed' AND id IN (%s)
`, issueTbl, placeholders), args...)
if err != nil {
if optionalBlockedTable(issueTbl) && isTableNotExistError(err) {
continue
}
return fmt.Errorf("compute blocked IDs: closed children from %s: %w", issueTbl, err)
}
for closedRows.Next() {
var childID string
if err := closedRows.Scan(&childID); err != nil {
_ = closedRows.Close()
return fmt.Errorf("compute blocked IDs: scan closed child: %w", err)
}
closedChildren[childID] = true
}
_ = closedRows.Close()
if err := closedRows.Err(); err != nil {
return fmt.Errorf("compute blocked IDs: closed child rows from %s: %w", issueTbl, err)
}
}
}
}
}
for _, dep := range waitsForDeps {
children := spawnerChildren[dep.spawnerID]
switch dep.gate {
case types.WaitsForAnyChildren:
if len(children) == 0 {
continue
}
hasClosedChild := false
hasActiveChild := false
for _, childID := range children {
if closedChildren[childID] {
hasClosedChild = true
break
}
if activeIDs[childID] {
hasActiveChild = true
}
}
if !hasClosedChild && hasActiveChild {
blockedSet[dep.issueID] = true
}
default:
for _, childID := range children {
if activeIDs[childID] {
blockedSet[dep.issueID] = true
break
}
}
}
}
return nil
}
func loadBlockingDepsForIssueIDsInTx(ctx context.Context, tx *sql.Tx, depTables []string, issueIDs []string) ([]blockingDepRecord, error) {
var deps []blockingDepRecord
for _, depTable := range depTables {
for start := 0; start < len(issueIDs); start += queryBatchSize {
end := start + queryBatchSize
if end > len(issueIDs) {
end = len(issueIDs)
}
placeholders, args := buildSQLInClause(issueIDs[start:end])
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id, %s AS depends_on_id, type, metadata FROM %s
WHERE issue_id IN (%s)
AND type IN ('blocks', 'waits-for', 'conditional-blocks')
`, DepTargetExpr, depTable, placeholders), args...)
//nolint:gosec // G201: depTable is a hardcoded constant.
query := fmt.Sprintf(`
SELECT issue_id, %s AS depends_on_id, type, metadata FROM %s
WHERE issue_id = ?
AND (type = 'blocks' OR type = 'waits-for' OR type = 'conditional-blocks')
`, DepTargetExpr, depTable)
for _, id := range issueIDs {
rows, err := tx.QueryContext(ctx, query, id)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
break
@ -409,60 +53,17 @@ func loadBlockingDepsForIssueIDsInTx(ctx context.Context, tx *sql.Tx, depTables
return deps, nil
}
func loadActiveIDSetInTx(ctx context.Context, tx *sql.Tx, issueTables []string, ids []string) (map[string]bool, error) {
activeIDs := make(map[string]bool)
if len(ids) == 0 {
return activeIDs, nil
}
for _, issueTable := range issueTables {
for start := 0; start < len(ids); start += queryBatchSize {
end := start + queryBatchSize
if end > len(ids) {
end = len(ids)
}
placeholders, args := buildSQLInClause(ids[start:end])
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT id FROM %s
WHERE id IN (%s)
AND status NOT IN ('closed', 'pinned')
`, issueTable, placeholders), args...)
if err != nil {
if optionalBlockedTable(issueTable) && isTableNotExistError(err) {
break
}
return nil, fmt.Errorf("active IDs from %s: %w", issueTable, err)
}
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
_ = rows.Close()
return nil, fmt.Errorf("scan active issue: %w", err)
}
activeIDs[id] = true
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("active rows from %s: %w", issueTable, err)
}
}
}
return activeIDs, nil
}
func loadParentIDsForChildrenInTx(ctx context.Context, tx *sql.Tx, depTables []string, childIDs []string) (map[string]string, error) {
childParents := make(map[string]string)
for _, depTable := range depTables {
for start := 0; start < len(childIDs); start += queryBatchSize {
end := start + queryBatchSize
if end > len(childIDs) {
end = len(childIDs)
}
placeholders, args := buildSQLInClause(childIDs[start:end])
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT issue_id, %s AS depends_on_id FROM %s
WHERE issue_id IN (%s)
AND type = 'parent-child'
`, DepTargetExpr, depTable, placeholders), args...)
//nolint:gosec // G201: depTable is a hardcoded constant.
query := fmt.Sprintf(`
SELECT issue_id, %s AS depends_on_id FROM %s
WHERE issue_id = ?
AND type = 'parent-child'
`, DepTargetExpr, depTable)
for _, id := range childIDs {
rows, err := tx.QueryContext(ctx, query, id)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
break
@ -486,9 +87,6 @@ func loadParentIDsForChildrenInTx(ctx context.Context, tx *sql.Tx, depTables []s
return childParents, nil
}
// GetChildrenWithParentsInTx returns a map of childID -> parentID for direct children
// (parent-child deps) of the given parent IDs.
//
//nolint:gosec // G201: tables are hardcoded
func GetChildrenWithParentsInTx(ctx context.Context, tx *sql.Tx, parentIDs []string) (map[string]string, error) {
if len(parentIDs) == 0 {
@ -496,18 +94,12 @@ func GetChildrenWithParentsInTx(ctx context.Context, tx *sql.Tx, parentIDs []str
}
result := make(map[string]string)
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
for start := 0; start < len(parentIDs); start += queryBatchSize {
end := start + queryBatchSize
if end > len(parentIDs) {
end = len(parentIDs)
}
placeholders, args := buildSQLInClause(parentIDs[start:end])
query := fmt.Sprintf(`
SELECT issue_id, %s AS depends_on_id FROM %s
WHERE type = 'parent-child' AND %s IN (%s)
`, DepTargetExpr, depTable, DepTargetExpr, placeholders)
rows, err := tx.QueryContext(ctx, query, args...)
query := fmt.Sprintf(`
SELECT issue_id, %s AS depends_on_id FROM %s
WHERE type = 'parent-child' AND %s = ?
`, DepTargetExpr, depTable, DepTargetExpr)
for _, parentID := range parentIDs {
rows, err := tx.QueryContext(ctx, query, parentID)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
break
@ -531,8 +123,6 @@ func GetChildrenWithParentsInTx(ctx context.Context, tx *sql.Tx, parentIDs []str
return result, nil
}
// GetChildrenOfIssuesInTx returns IDs of direct children (parent-child deps) of the given issue IDs.
//
//nolint:gosec // G201: tables are hardcoded
func GetChildrenOfIssuesInTx(ctx context.Context, tx *sql.Tx, parentIDs []string) ([]string, error) {
if len(parentIDs) == 0 {
@ -540,18 +130,12 @@ func GetChildrenOfIssuesInTx(ctx context.Context, tx *sql.Tx, parentIDs []string
}
var children []string
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
for start := 0; start < len(parentIDs); start += queryBatchSize {
end := start + queryBatchSize
if end > len(parentIDs) {
end = len(parentIDs)
}
placeholders, args := buildSQLInClause(parentIDs[start:end])
query := fmt.Sprintf(`
SELECT issue_id FROM %s
WHERE type = 'parent-child' AND %s IN (%s)
`, depTable, DepTargetExpr, placeholders)
rows, err := tx.QueryContext(ctx, query, args...)
query := fmt.Sprintf(`
SELECT issue_id FROM %s
WHERE type = 'parent-child' AND %s = ?
`, depTable, DepTargetExpr)
for _, parentID := range parentIDs {
rows, err := tx.QueryContext(ctx, query, parentID)
if err != nil {
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
break
@ -575,11 +159,6 @@ func GetChildrenOfIssuesInTx(ctx context.Context, tx *sql.Tx, parentIDs []string
return children, nil
}
// GetDescendantIDsInTx returns IDs of all transitive parent-child descendants
// of rootID, traversing parent-child edges in both the dependencies and
// wisp_dependencies tables. rootID itself is NOT included. Cycles are broken
// inside the recursive CTE. maxDepth caps traversal depth only when positive;
// reaching that cap returns an explicit error rather than silently truncating.
func GetDescendantIDsInTx(ctx context.Context, tx *sql.Tx, rootID string, maxDepth int) ([]string, error) {
if rootID == "" {
return nil, nil
@ -657,62 +236,91 @@ func GetDescendantIDsInTx(ctx context.Context, tx *sql.Tx, rootID string, maxDep
return result, nil
}
// GetBlockedIssuesInTx returns issues that are blocked by other issues.
// This is the full implementation including transitive child blocking and parent filtering.
//
//nolint:gosec // G201: tables are hardcoded
func GetBlockedIssuesInTx(ctx context.Context, tx *sql.Tx, filter types.WorkFilter) ([]*types.BlockedIssue, error) {
// Step 1: Compute blocked IDs (also returns activeIDs for later use)
blockedIDList, activeIDs, err := ComputeBlockedIDsInTx(ctx, tx, true)
if err != nil {
return nil, fmt.Errorf("compute blocked IDs: %w", err)
}
blockedSet := make(map[string]bool, len(blockedIDList))
for _, id := range blockedIDList {
blockedSet[id] = true
}
// Step 2: Include children of blocked parents (GH#1495).
childToParent, childErr := GetChildrenWithParentsInTx(ctx, tx, blockedIDList)
if childErr == nil {
for childID := range childToParent {
if activeIDs[childID] && !blockedSet[childID] {
blockedSet[childID] = true
blockedIDList = append(blockedIDList, childID)
var blockedIDList []string
blockedSet := make(map[string]bool)
for _, table := range []string{"issues", "wisps"} {
//nolint:gosec // G201: table is one of two hardcoded values.
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT id FROM %s
WHERE is_blocked = 1 AND status <> 'closed' AND status <> 'pinned'
`, table))
if err != nil {
if optionalBlockedTable(table) && isTableNotExistError(err) {
continue
}
return nil, fmt.Errorf("read blocked ids from %s: %w", table, err)
}
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
_ = rows.Close()
return nil, fmt.Errorf("scan blocked id from %s: %w", table, err)
}
if !blockedSet[id] {
blockedSet[id] = true
blockedIDList = append(blockedIDList, id)
}
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("blocked id rows from %s: %w", table, err)
}
}
if len(blockedIDList) == 0 {
return nil, nil
}
// Step 3: Get blocking deps to build BlockedBy lists
blockerMap := make(map[string][]string)
blockingDeps, err := loadBlockingDepsForIssueIDsInTx(ctx, tx, []string{"dependencies", "wisp_dependencies"}, blockedIDList)
if err != nil {
return nil, fmt.Errorf("get blocking deps: %w", err)
}
for _, rec := range blockingDeps {
if blockedSet[rec.issueID] && activeIDs[rec.dependsOnID] {
if len(blockingDeps) > 0 {
targetIDs := make([]string, 0, len(blockingDeps))
seenTargets := make(map[string]bool, len(blockingDeps))
for _, rec := range blockingDeps {
if !seenTargets[rec.dependsOnID] {
seenTargets[rec.dependsOnID] = true
targetIDs = append(targetIDs, rec.dependsOnID)
}
}
activeTargets, err := loadStatusByIDInTx(ctx, tx, targetIDs)
if err != nil {
return nil, fmt.Errorf("blocker target status: %w", err)
}
for _, rec := range blockingDeps {
status, ok := activeTargets[rec.dependsOnID]
if !ok || status == types.StatusClosed || status == types.StatusPinned {
continue
}
blockerMap[rec.issueID] = append(blockerMap[rec.issueID], rec.dependsOnID)
}
}
// Step 3b: Add transitively blocked children to blockerMap (GH#1495).
if childErr == nil {
for childID, parentID := range childToParent {
if activeIDs[childID] && blockedSet[childID] {
if _, hasDirectBlocker := blockerMap[childID]; !hasDirectBlocker {
var inheritedIDs []string
for _, id := range blockedIDList {
if _, ok := blockerMap[id]; !ok {
inheritedIDs = append(inheritedIDs, id)
}
}
if len(inheritedIDs) > 0 {
parentMap, err := loadParentIDsForChildrenInTx(ctx, tx, []string{"dependencies", "wisp_dependencies"}, inheritedIDs)
if err == nil {
for childID, parentID := range parentMap {
if _, alreadyHas := blockerMap[childID]; !alreadyHas {
blockerMap[childID] = []string{parentID}
}
}
}
}
// Step 4: Batch-fetch all blocked issues
blockedIDs := make([]string, 0, len(blockerMap))
displayIDs := make([]string, 0, len(blockerMap))
for id := range blockerMap {
blockedIDs = append(blockedIDs, id)
displayIDs = append(displayIDs, id)
}
issues, err := GetIssuesByIDsInTx(ctx, tx, blockedIDs, nil)
issues, err := GetIssuesByIDsInTx(ctx, tx, displayIDs, nil)
if err != nil {
return nil, fmt.Errorf("batch-fetch blocked issues: %w", err)
}
@ -721,7 +329,6 @@ func GetBlockedIssuesInTx(ctx context.Context, tx *sql.Tx, filter types.WorkFilt
issueMap[issue.ID] = issue
}
// Parent filtering: restrict to children of specified parent (GH#2009)
var parentChildSet map[string]bool
if filter.ParentID != nil {
parentChildSet = make(map[string]bool)
@ -732,7 +339,6 @@ func GetBlockedIssuesInTx(ctx context.Context, tx *sql.Tx, filter types.WorkFilt
parentChildSet[childID] = true
}
}
// Also include dotted-ID children (e.g., "parent.1.2")
for id := range blockerMap {
if strings.HasPrefix(id, parentID+".") {
parentChildSet[id] = true
@ -745,12 +351,10 @@ func GetBlockedIssuesInTx(ctx context.Context, tx *sql.Tx, filter types.WorkFilt
if parentChildSet != nil && !parentChildSet[id] {
continue
}
issue, ok := issueMap[id]
if !ok || issue == nil {
continue
}
results = append(results, &types.BlockedIssue{
Issue: *issue,
BlockedByCount: len(blockerIDs),
@ -758,7 +362,6 @@ func GetBlockedIssuesInTx(ctx context.Context, tx *sql.Tx, filter types.WorkFilt
})
}
// Sort by priority ASC, then created_at DESC
sort.Slice(results, func(i, j int) bool {
if results[i].Issue.Priority != results[j].Issue.Priority {
return results[i].Issue.Priority < results[j].Issue.Priority

View file

@ -0,0 +1,664 @@
package issueops
import (
"context"
"database/sql"
"fmt"
"github.com/steveyegge/beads/internal/types"
)
const waitsForGateBlockedSQL = `
(
EXISTS (
SELECT 1 FROM dependencies cd JOIN issues child ON child.id = cd.issue_id
WHERE cd.type = 'parent-child'
AND ((d.depends_on_issue_id IS NOT NULL AND cd.depends_on_issue_id = d.depends_on_issue_id)
OR (d.depends_on_wisp_id IS NOT NULL AND cd.depends_on_wisp_id = d.depends_on_wisp_id))
AND child.status <> 'closed' AND child.status <> 'pinned'
)
OR EXISTS (
SELECT 1 FROM wisp_dependencies cd JOIN wisps child ON child.id = cd.issue_id
WHERE cd.type = 'parent-child'
AND ((d.depends_on_issue_id IS NOT NULL AND cd.depends_on_issue_id = d.depends_on_issue_id)
OR (d.depends_on_wisp_id IS NOT NULL AND cd.depends_on_wisp_id = d.depends_on_wisp_id))
AND child.status <> 'closed' AND child.status <> 'pinned'
)
)
AND NOT (
JSON_UNQUOTE(JSON_EXTRACT(d.metadata, '$.gate')) = 'any-children'
AND (
EXISTS (
SELECT 1 FROM dependencies cd JOIN issues child ON child.id = cd.issue_id
WHERE cd.type = 'parent-child'
AND ((d.depends_on_issue_id IS NOT NULL AND cd.depends_on_issue_id = d.depends_on_issue_id)
OR (d.depends_on_wisp_id IS NOT NULL AND cd.depends_on_wisp_id = d.depends_on_wisp_id))
AND child.status = 'closed'
)
OR EXISTS (
SELECT 1 FROM wisp_dependencies cd JOIN wisps child ON child.id = cd.issue_id
WHERE cd.type = 'parent-child'
AND ((d.depends_on_issue_id IS NOT NULL AND cd.depends_on_issue_id = d.depends_on_issue_id)
OR (d.depends_on_wisp_id IS NOT NULL AND cd.depends_on_wisp_id = d.depends_on_wisp_id))
AND child.status = 'closed'
)
)
)
`
func RecomputeIsBlockedInTx(ctx context.Context, tx *sql.Tx, issueIDs, wispIDs []string) error {
if len(issueIDs) == 0 && len(wispIDs) == 0 {
return nil
}
for {
var changed int64
n, err := recomputeIsBlockedPassForIssuesInTx(ctx, tx, issueIDs)
if err != nil {
return err
}
changed += n
n, err = recomputeIsBlockedPassForWispsInTx(ctx, tx, wispIDs)
if err != nil {
return err
}
changed += n
if changed == 0 {
return nil
}
}
}
func RecomputeIsBlockedForIDsInTx(ctx context.Context, tx *sql.Tx, ids []string) error {
return RecomputeIsBlockedInTx(ctx, tx, ids, nil)
}
func RecomputeIsBlockedForWispIDsInTx(ctx context.Context, tx *sql.Tx, ids []string) error {
return RecomputeIsBlockedInTx(ctx, tx, nil, ids)
}
//nolint:gosec // G201: SQL templates are constant; only IN-clause placeholders are formatted in.
func recomputeIsBlockedPassForIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string) (int64, error) {
if len(ids) == 0 {
return 0, nil
}
markBlockedTmpl := fmt.Sprintf(`
UPDATE issues i SET i.is_blocked = 1
WHERE i.id IN (%%s)
AND i.is_blocked = 0
AND i.status <> 'closed' AND i.status <> 'pinned'
AND (
EXISTS (
SELECT 1 FROM dependencies d
JOIN issues t ON t.id = d.depends_on_issue_id
WHERE d.issue_id = i.id
AND (d.type = 'blocks' OR d.type = 'conditional-blocks')
AND t.status <> 'closed' AND t.status <> 'pinned'
)
OR EXISTS (
SELECT 1 FROM dependencies d
JOIN wisps t ON t.id = d.depends_on_wisp_id
WHERE d.issue_id = i.id
AND (d.type = 'blocks' OR d.type = 'conditional-blocks')
AND t.status <> 'closed' AND t.status <> 'pinned'
)
OR EXISTS (
SELECT 1 FROM dependencies d
JOIN issues p ON p.id = d.depends_on_issue_id
WHERE d.issue_id = i.id
AND d.type = 'parent-child'
AND p.is_blocked = 1
)
OR EXISTS (
SELECT 1 FROM dependencies d
JOIN wisps p ON p.id = d.depends_on_wisp_id
WHERE d.issue_id = i.id
AND d.type = 'parent-child'
AND p.is_blocked = 1
)
OR EXISTS (
SELECT 1 FROM dependencies d
WHERE d.issue_id = i.id AND d.type = 'waits-for'
AND (%s)
)
)
`, waitsForGateBlockedSQL)
markUnblockedTmpl := fmt.Sprintf(`
UPDATE issues i SET i.is_blocked = 0
WHERE i.id IN (%%s)
AND i.is_blocked = 1
AND (
i.status = 'closed' OR i.status = 'pinned'
OR (
NOT EXISTS (
SELECT 1 FROM dependencies d
JOIN issues t ON t.id = d.depends_on_issue_id
WHERE d.issue_id = i.id
AND (d.type = 'blocks' OR d.type = 'conditional-blocks')
AND t.status <> 'closed' AND t.status <> 'pinned'
)
AND NOT EXISTS (
SELECT 1 FROM dependencies d
JOIN wisps t ON t.id = d.depends_on_wisp_id
WHERE d.issue_id = i.id
AND (d.type = 'blocks' OR d.type = 'conditional-blocks')
AND t.status <> 'closed' AND t.status <> 'pinned'
)
AND NOT EXISTS (
SELECT 1 FROM dependencies d
JOIN issues p ON p.id = d.depends_on_issue_id
WHERE d.issue_id = i.id
AND d.type = 'parent-child'
AND p.is_blocked = 1
)
AND NOT EXISTS (
SELECT 1 FROM dependencies d
JOIN wisps p ON p.id = d.depends_on_wisp_id
WHERE d.issue_id = i.id
AND d.type = 'parent-child'
AND p.is_blocked = 1
)
AND NOT EXISTS (
SELECT 1 FROM dependencies d
WHERE d.issue_id = i.id AND d.type = 'waits-for'
AND (%s)
)
)
)
`, waitsForGateBlockedSQL)
return runMarkUnmarkBatchedInTx(ctx, tx, markBlockedTmpl, markUnblockedTmpl, ids)
}
//nolint:gosec // G201: SQL templates are constant; only IN-clause placeholders are formatted in.
func recomputeIsBlockedPassForWispsInTx(ctx context.Context, tx *sql.Tx, ids []string) (int64, error) {
if len(ids) == 0 {
return 0, nil
}
markBlockedTmpl := fmt.Sprintf(`
UPDATE wisps w SET w.is_blocked = 1
WHERE w.id IN (%%s)
AND w.is_blocked = 0
AND w.status <> 'closed' AND w.status <> 'pinned'
AND (
EXISTS (
SELECT 1 FROM wisp_dependencies d
JOIN issues t ON t.id = d.depends_on_issue_id
WHERE d.issue_id = w.id
AND (d.type = 'blocks' OR d.type = 'conditional-blocks')
AND t.status <> 'closed' AND t.status <> 'pinned'
)
OR EXISTS (
SELECT 1 FROM wisp_dependencies d
JOIN wisps t ON t.id = d.depends_on_wisp_id
WHERE d.issue_id = w.id
AND (d.type = 'blocks' OR d.type = 'conditional-blocks')
AND t.status <> 'closed' AND t.status <> 'pinned'
)
OR EXISTS (
SELECT 1 FROM wisp_dependencies d
JOIN issues p ON p.id = d.depends_on_issue_id
WHERE d.issue_id = w.id
AND d.type = 'parent-child'
AND p.is_blocked = 1
)
OR EXISTS (
SELECT 1 FROM wisp_dependencies d
JOIN wisps p ON p.id = d.depends_on_wisp_id
WHERE d.issue_id = w.id
AND d.type = 'parent-child'
AND p.is_blocked = 1
)
OR EXISTS (
SELECT 1 FROM wisp_dependencies d
WHERE d.issue_id = w.id AND d.type = 'waits-for'
AND (%s)
)
)
`, waitsForGateBlockedSQL)
markUnblockedTmpl := fmt.Sprintf(`
UPDATE wisps w SET w.is_blocked = 0
WHERE w.id IN (%%s)
AND w.is_blocked = 1
AND (
w.status = 'closed' OR w.status = 'pinned'
OR (
NOT EXISTS (
SELECT 1 FROM wisp_dependencies d
JOIN issues t ON t.id = d.depends_on_issue_id
WHERE d.issue_id = w.id
AND (d.type = 'blocks' OR d.type = 'conditional-blocks')
AND t.status <> 'closed' AND t.status <> 'pinned'
)
AND NOT EXISTS (
SELECT 1 FROM wisp_dependencies d
JOIN wisps t ON t.id = d.depends_on_wisp_id
WHERE d.issue_id = w.id
AND (d.type = 'blocks' OR d.type = 'conditional-blocks')
AND t.status <> 'closed' AND t.status <> 'pinned'
)
AND NOT EXISTS (
SELECT 1 FROM wisp_dependencies d
JOIN issues p ON p.id = d.depends_on_issue_id
WHERE d.issue_id = w.id
AND d.type = 'parent-child'
AND p.is_blocked = 1
)
AND NOT EXISTS (
SELECT 1 FROM wisp_dependencies d
JOIN wisps p ON p.id = d.depends_on_wisp_id
WHERE d.issue_id = w.id
AND d.type = 'parent-child'
AND p.is_blocked = 1
)
AND NOT EXISTS (
SELECT 1 FROM wisp_dependencies d
WHERE d.issue_id = w.id AND d.type = 'waits-for'
AND (%s)
)
)
)
`, waitsForGateBlockedSQL)
return runMarkUnmarkBatchedInTx(ctx, tx, markBlockedTmpl, markUnblockedTmpl, ids)
}
//nolint:gosec // G201: callers pass constant templates; only IN-clause placeholders are formatted in.
func runMarkUnmarkBatchedInTx(ctx context.Context, tx *sql.Tx, markTmpl, unmarkTmpl string, ids []string) (int64, error) {
var changed int64
for start := 0; start < len(ids); start += queryBatchSize {
end := start + queryBatchSize
if end > len(ids) {
end = len(ids)
}
placeholders, args := buildSQLInClause(ids[start:end])
res, err := tx.ExecContext(ctx, fmt.Sprintf(markTmpl, placeholders), args...)
if err != nil {
return changed, fmt.Errorf("recompute is_blocked (mark): %w", err)
}
n, _ := res.RowsAffected()
changed += n
res, err = tx.ExecContext(ctx, fmt.Sprintf(unmarkTmpl, placeholders), args...)
if err != nil {
return changed, fmt.Errorf("recompute is_blocked (unmark): %w", err)
}
n, _ = res.RowsAffected()
changed += n
}
return changed, nil
}
func AffectedByStatusChangeInTx(ctx context.Context, tx *sql.Tx, id string) ([]string, []string, error) {
issueSeed := []string{id}
issueSeen := map[string]bool{id: true}
var wispSeed []string
wispSeen := make(map[string]bool)
if err := loadBlockingDependersInTx(ctx, tx, "depends_on_issue_id", id, &issueSeed, issueSeen, &wispSeed, wispSeen); err != nil {
return nil, nil, err
}
if err := loadWaitersWhoseSpawnerIsParentOfInTx(ctx, tx, id, false, &issueSeed, issueSeen, &wispSeed, wispSeen); err != nil {
return nil, nil, err
}
return expandByParentChildDescendantsInTx(ctx, tx, issueSeed, wispSeed, issueSeen, wispSeen)
}
func AffectedByStatusChangeForWispInTx(ctx context.Context, tx *sql.Tx, id string) ([]string, []string, error) {
var issueSeed []string
issueSeen := make(map[string]bool)
wispSeed := []string{id}
wispSeen := map[string]bool{id: true}
if err := loadBlockingDependersInTx(ctx, tx, "depends_on_wisp_id", id, &issueSeed, issueSeen, &wispSeed, wispSeen); err != nil {
return nil, nil, err
}
if err := loadWaitersWhoseSpawnerIsParentOfInTx(ctx, tx, id, true, &issueSeed, issueSeen, &wispSeed, wispSeen); err != nil {
return nil, nil, err
}
return expandByParentChildDescendantsInTx(ctx, tx, issueSeed, wispSeed, issueSeen, wispSeen)
}
func AffectedByDepChangeInTx(ctx context.Context, tx *sql.Tx, source, target string, depType types.DependencyType) ([]string, []string, error) {
switch depType {
case types.DepBlocks, types.DepConditionalBlocks, types.DepWaitsFor, types.DepParentChild:
issueSeed := []string{source}
issueSeen := map[string]bool{source: true}
var wispSeed []string
wispSeen := map[string]bool{}
if depType == types.DepParentChild && target != "" {
if err := loadWaitersOnSpawnerIDsInTx(ctx, tx, []string{target}, &issueSeed, issueSeen, &wispSeed, wispSeen); err != nil {
return nil, nil, err
}
}
return expandByParentChildDescendantsInTx(ctx, tx, issueSeed, wispSeed, issueSeen, wispSeen)
default:
return nil, nil, nil
}
}
func AffectedByDepChangeForWispInTx(ctx context.Context, tx *sql.Tx, source, target string, depType types.DependencyType) ([]string, []string, error) {
switch depType {
case types.DepBlocks, types.DepConditionalBlocks, types.DepWaitsFor, types.DepParentChild:
var issueSeed []string
issueSeen := map[string]bool{}
wispSeed := []string{source}
wispSeen := map[string]bool{source: true}
if depType == types.DepParentChild && target != "" {
if err := loadWaitersOnSpawnerIDsInTx(ctx, tx, []string{target}, &issueSeed, issueSeen, &wispSeed, wispSeen); err != nil {
return nil, nil, err
}
}
return expandByParentChildDescendantsInTx(ctx, tx, issueSeed, wispSeed, issueSeen, wispSeen)
default:
return nil, nil, nil
}
}
func loadBlockingDependersInTx(
ctx context.Context, tx *sql.Tx,
targetCol, id string,
issueSeed *[]string, issueSeen map[string]bool,
wispSeed *[]string, wispSeen map[string]bool,
) error {
return loadBlockingDependersForIDsInTx(ctx, tx, targetCol, []string{id}, issueSeed, issueSeen, wispSeed, wispSeen)
}
//nolint:gosec // G201: targetCol is one of two constant column names.
func loadBlockingDependersForIDsInTx(
ctx context.Context, tx *sql.Tx,
targetCol string, ids []string,
issueSeed *[]string, issueSeen map[string]bool,
wispSeed *[]string, wispSeen map[string]bool,
) error {
if len(ids) == 0 {
return nil
}
tables := []struct {
table string
seed *[]string
seen map[string]bool
errCtx string
}{
{"dependencies", issueSeed, issueSeen, "load issue dependers"},
{"wisp_dependencies", wispSeed, wispSeen, "load wisp dependers"},
}
for _, id := range ids {
for _, t := range tables {
query := fmt.Sprintf(`
SELECT issue_id FROM %s
WHERE %s = ?
AND (type = 'blocks' OR type = 'conditional-blocks')
`, t.table, targetCol)
rows, err := tx.QueryContext(ctx, query, id)
if err != nil {
return fmt.Errorf("%s: query: %w", t.errCtx, err)
}
for rows.Next() {
var dependerID string
if err := rows.Scan(&dependerID); err != nil {
_ = rows.Close()
return fmt.Errorf("%s: scan: %w", t.errCtx, err)
}
if !t.seen[dependerID] {
t.seen[dependerID] = true
*t.seed = append(*t.seed, dependerID)
}
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return fmt.Errorf("%s: rows: %w", t.errCtx, err)
}
}
}
return nil
}
func AffectedByDeletionInTx(
ctx context.Context, tx *sql.Tx,
deletedIssues, deletedWisps []string,
) ([]string, []string, error) {
if len(deletedIssues) == 0 && len(deletedWisps) == 0 {
return nil, nil, nil
}
issueSeen := make(map[string]bool, len(deletedIssues))
wispSeen := make(map[string]bool, len(deletedWisps))
for _, id := range deletedIssues {
issueSeen[id] = true
}
for _, id := range deletedWisps {
wispSeen[id] = true
}
var issueSeed, wispSeed []string
if err := loadBlockingDependersForIDsInTx(ctx, tx, "depends_on_issue_id", deletedIssues, &issueSeed, issueSeen, &wispSeed, wispSeen); err != nil {
return nil, nil, err
}
if err := loadBlockingDependersForIDsInTx(ctx, tx, "depends_on_wisp_id", deletedWisps, &issueSeed, issueSeen, &wispSeed, wispSeen); err != nil {
return nil, nil, err
}
for _, w := range []struct {
depTable, parentCol string
parentIDs []string
seed *[]string
seen map[string]bool
}{
{"dependencies", "depends_on_issue_id", deletedIssues, &issueSeed, issueSeen},
{"wisp_dependencies", "depends_on_issue_id", deletedIssues, &wispSeed, wispSeen},
{"dependencies", "depends_on_wisp_id", deletedWisps, &issueSeed, issueSeen},
{"wisp_dependencies", "depends_on_wisp_id", deletedWisps, &wispSeed, wispSeen},
} {
if err := appendChildrenInTx(ctx, tx, w.depTable, w.parentCol, w.parentIDs, w.seen, w.seed); err != nil {
return nil, nil, err
}
}
return expandByParentChildDescendantsInTx(ctx, tx, issueSeed, wispSeed, issueSeen, wispSeen)
}
func expandByParentChildDescendantsInTx(
ctx context.Context, tx *sql.Tx,
issueSeed, wispSeed []string,
issueSeen, wispSeen map[string]bool,
) ([]string, []string, error) {
issueQueue := issueSeed
wispQueue := wispSeed
issueHead, wispHead := 0, 0
for issueHead < len(issueQueue) || wispHead < len(wispQueue) {
if issueHead < len(issueQueue) {
end := issueHead + queryBatchSize
if end > len(issueQueue) {
end = len(issueQueue)
}
batch := issueQueue[issueHead:end]
issueHead = end
if err := appendChildrenInTx(ctx, tx, "dependencies", "depends_on_issue_id", batch, issueSeen, &issueQueue); err != nil {
return nil, nil, err
}
if err := appendChildrenInTx(ctx, tx, "wisp_dependencies", "depends_on_issue_id", batch, wispSeen, &wispQueue); err != nil {
return nil, nil, err
}
}
if wispHead < len(wispQueue) {
end := wispHead + queryBatchSize
if end > len(wispQueue) {
end = len(wispQueue)
}
batch := wispQueue[wispHead:end]
wispHead = end
if err := appendChildrenInTx(ctx, tx, "dependencies", "depends_on_wisp_id", batch, issueSeen, &issueQueue); err != nil {
return nil, nil, err
}
if err := appendChildrenInTx(ctx, tx, "wisp_dependencies", "depends_on_wisp_id", batch, wispSeen, &wispQueue); err != nil {
return nil, nil, err
}
}
}
return issueQueue, wispQueue, nil
}
//nolint:gosec // G201: depTable and parentCol come from constant call sites.
func appendChildrenInTx(
ctx context.Context, tx *sql.Tx,
depTable, parentCol string,
parentIDs []string,
seen map[string]bool, queue *[]string,
) error {
if len(parentIDs) == 0 {
return nil
}
query := fmt.Sprintf(`
SELECT issue_id FROM %s
WHERE type = 'parent-child'
AND %s = ?
`, depTable, parentCol)
for _, parentID := range parentIDs {
rows, err := tx.QueryContext(ctx, query, parentID)
if err != nil {
return fmt.Errorf("expand children from %s on %s: %w", depTable, parentCol, err)
}
for rows.Next() {
var childID string
if err := rows.Scan(&childID); err != nil {
_ = rows.Close()
return fmt.Errorf("expand children: scan: %w", err)
}
if !seen[childID] {
seen[childID] = true
*queue = append(*queue, childID)
}
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return fmt.Errorf("expand children: rows: %w", err)
}
}
return nil
}
func loadWaitersWhoseSpawnerIsParentOfInTx(
ctx context.Context, tx *sql.Tx,
childID string, childIsWisp bool,
issueSeed *[]string, issueSeen map[string]bool,
wispSeed *[]string, wispSeen map[string]bool,
) error {
depTable := "dependencies"
if childIsWisp {
depTable = "wisp_dependencies"
}
//nolint:gosec // G201: depTable is one of two constant values.
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT depends_on_issue_id, depends_on_wisp_id
FROM %s
WHERE issue_id = ? AND type = 'parent-child'
`, depTable), childID)
if err != nil {
return fmt.Errorf("waiters on parent of %s: load parents: %w", childID, err)
}
var issueParentIDs, wispParentIDs []string
for rows.Next() {
var ip, wp sql.NullString
if err := rows.Scan(&ip, &wp); err != nil {
_ = rows.Close()
return fmt.Errorf("waiters on parent of %s: scan: %w", childID, err)
}
if ip.Valid {
issueParentIDs = append(issueParentIDs, ip.String)
}
if wp.Valid {
wispParentIDs = append(wispParentIDs, wp.String)
}
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return fmt.Errorf("waiters on parent of %s: rows: %w", childID, err)
}
if len(issueParentIDs) > 0 {
if err := loadWaitersOnSpawnerIDsByColInTx(ctx, tx, "depends_on_issue_id", issueParentIDs, issueSeed, issueSeen, wispSeed, wispSeen); err != nil {
return err
}
}
if len(wispParentIDs) > 0 {
if err := loadWaitersOnSpawnerIDsByColInTx(ctx, tx, "depends_on_wisp_id", wispParentIDs, issueSeed, issueSeen, wispSeed, wispSeen); err != nil {
return err
}
}
return nil
}
func loadWaitersOnSpawnerIDsInTx(
ctx context.Context, tx *sql.Tx,
spawnerIDs []string,
issueSeed *[]string, issueSeen map[string]bool,
wispSeed *[]string, wispSeen map[string]bool,
) error {
if err := loadWaitersOnSpawnerIDsByColInTx(ctx, tx, "depends_on_issue_id", spawnerIDs, issueSeed, issueSeen, wispSeed, wispSeen); err != nil {
return err
}
return loadWaitersOnSpawnerIDsByColInTx(ctx, tx, "depends_on_wisp_id", spawnerIDs, issueSeed, issueSeen, wispSeed, wispSeen)
}
//nolint:gosec // G201: targetCol is one of two constant column names.
func loadWaitersOnSpawnerIDsByColInTx(
ctx context.Context, tx *sql.Tx,
targetCol string, spawnerIDs []string,
issueSeed *[]string, issueSeen map[string]bool,
wispSeed *[]string, wispSeen map[string]bool,
) error {
if len(spawnerIDs) == 0 {
return nil
}
tables := []struct {
table string
seed *[]string
seen map[string]bool
errCtx string
}{
{"dependencies", issueSeed, issueSeen, "load issue waiters"},
{"wisp_dependencies", wispSeed, wispSeen, "load wisp waiters"},
}
for _, spawnerID := range spawnerIDs {
for _, t := range tables {
query := fmt.Sprintf(`
SELECT issue_id FROM %s
WHERE type = 'waits-for' AND %s = ?
`, t.table, targetCol)
rows, err := tx.QueryContext(ctx, query, spawnerID)
if err != nil {
if optionalBlockedTable(t.table) && isTableNotExistError(err) {
continue
}
return fmt.Errorf("%s: query: %w", t.errCtx, err)
}
for rows.Next() {
var waiterID string
if err := rows.Scan(&waiterID); err != nil {
_ = rows.Close()
return fmt.Errorf("%s: scan: %w", t.errCtx, err)
}
if !t.seen[waiterID] {
t.seen[waiterID] = true
*t.seed = append(*t.seed, waiterID)
}
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return fmt.Errorf("%s: rows: %w", t.errCtx, err)
}
}
}
return nil
}

View file

@ -1,187 +0,0 @@
package issueops
import (
"context"
"database/sql"
"fmt"
"sort"
"testing"
sqlmock "github.com/DATA-DOG/go-sqlmock"
)
const (
activeIssuesQuery = `(?s)SELECT id FROM issues\s+WHERE status NOT IN \('closed', 'pinned'\)`
blockingDepsQuery = `(?s)SELECT issue_id, COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) AS depends_on_id, type, metadata FROM dependencies\s+WHERE issue_id IN \(.+\)\s+AND type IN \('blocks', 'waits-for', 'conditional-blocks'\)`
childrenQuery = `(?s)SELECT issue_id, COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) AS depends_on_id FROM dependencies\s+WHERE type = 'parent-child' AND COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) IN \(.+\)`
activeChildQuery = `(?s)SELECT id FROM issues\s+WHERE id IN \(.+\)\s+AND status NOT IN \('closed', 'pinned'\)`
closedChildQuery = `(?s)SELECT id FROM issues\s+WHERE status = 'closed' AND id IN \(.+\)`
)
func TestComputeBlockedIDsInTxDependencySemantics(t *testing.T) {
ctx := context.Background()
db, mock, tx := newBlockedMockTx(t, ctx)
defer closeBlockedMockTx(t, db, mock, tx)
mock.ExpectQuery(activeIssuesQuery).WillReturnRows(sqlmock.NewRows([]string{"id"}).
AddRow("issue-blocked").
AddRow("blocker-active").
AddRow("issue-closed-blocker").
AddRow("issue-conditional").
AddRow("conditional-blocker").
AddRow("wait-default-active-child").
AddRow("spawner-default").
AddRow("child-default-active").
AddRow("wait-default-closed-child").
AddRow("spawner-default-closed").
AddRow("wait-any-active-only").
AddRow("spawner-any-active").
AddRow("child-any-active").
AddRow("wait-any-with-closed").
AddRow("spawner-any-closed").
AddRow("child-any-active2"))
mock.ExpectQuery(blockingDepsQuery).WillReturnRows(sqlmock.NewRows([]string{"issue_id", "depends_on_id", "type", "metadata"}).
AddRow("issue-blocked", "blocker-active", "blocks", nil).
AddRow("issue-closed-blocker", "blocker-closed", "blocks", nil).
AddRow("issue-conditional", "conditional-blocker", "conditional-blocks", nil).
AddRow("wait-default-active-child", "spawner-default", "waits-for", nil).
AddRow("wait-default-closed-child", "spawner-default-closed", "waits-for", nil).
AddRow("wait-any-active-only", "spawner-any-active", "waits-for", `{"gate":"any-children"}`).
AddRow("wait-any-with-closed", "spawner-any-closed", "waits-for", `{"gate":"any-children"}`))
mock.ExpectQuery(childrenQuery).WillReturnRows(sqlmock.NewRows([]string{"issue_id", "depends_on_id"}).
AddRow("child-default-active", "spawner-default").
AddRow("child-default-closed", "spawner-default-closed").
AddRow("child-any-active", "spawner-any-active").
AddRow("child-any-active2", "spawner-any-closed").
AddRow("child-any-closed", "spawner-any-closed"))
mock.ExpectQuery(activeChildQuery).WillReturnRows(sqlmock.NewRows([]string{"id"}).
AddRow("child-default-active").
AddRow("child-any-active").
AddRow("child-any-active2"))
mock.ExpectQuery(closedChildQuery).WillReturnRows(sqlmock.NewRows([]string{"id"}).
AddRow("child-default-closed").
AddRow("child-any-closed"))
got, activeIDs, err := ComputeBlockedIDsInTx(ctx, tx, false)
if err != nil {
t.Fatalf("ComputeBlockedIDsInTx: %v", err)
}
assertStringSet(t, got, []string{
"issue-blocked",
"issue-conditional",
"wait-default-active-child",
"wait-any-active-only",
})
for _, id := range []string{
"issue-closed-blocker",
"wait-default-closed-child",
"wait-any-with-closed",
} {
if containsString(got, id) {
t.Fatalf("expected %s not to be blocked; got %v", id, got)
}
}
if !activeIDs["child-default-active"] || activeIDs["child-default-closed"] {
t.Fatalf("active child lookup = %v, want active child only", activeIDs)
}
}
func TestComputeBlockedIDsInTxEmptyActiveSetSkipsDependencyLoad(t *testing.T) {
ctx := context.Background()
db, mock, tx := newBlockedMockTx(t, ctx)
defer closeBlockedMockTx(t, db, mock, tx)
mock.ExpectQuery(activeIssuesQuery).WillReturnRows(sqlmock.NewRows([]string{"id"}))
got, activeIDs, err := ComputeBlockedIDsInTx(ctx, tx, false)
if err != nil {
t.Fatalf("ComputeBlockedIDsInTx: %v", err)
}
if len(got) != 0 {
t.Fatalf("blocked IDs = %v, want empty", got)
}
if len(activeIDs) != 0 {
t.Fatalf("active IDs = %v, want empty", activeIDs)
}
}
func TestComputeBlockedIDsInTxBatchesActiveDependencyScan(t *testing.T) {
ctx := context.Background()
db, mock, tx := newBlockedMockTx(t, ctx)
defer closeBlockedMockTx(t, db, mock, tx)
activeRows := sqlmock.NewRows([]string{"id"}).
AddRow("issue-batch").
AddRow("blocker-batch")
for i := 0; i < queryBatchSize-1; i++ {
activeRows.AddRow(fmt.Sprintf("filler-%03d", i))
}
mock.ExpectQuery(activeIssuesQuery).WillReturnRows(activeRows)
mock.ExpectQuery(blockingDepsQuery).WillReturnRows(sqlmock.NewRows([]string{"issue_id", "depends_on_id", "type", "metadata"}))
mock.ExpectQuery(blockingDepsQuery).WillReturnRows(sqlmock.NewRows([]string{"issue_id", "depends_on_id", "type", "metadata"}).
AddRow("issue-batch", "blocker-batch", "blocks", nil))
got, _, err := ComputeBlockedIDsInTx(ctx, tx, false)
if err != nil {
t.Fatalf("ComputeBlockedIDsInTx: %v", err)
}
assertStringSet(t, got, []string{"issue-batch"})
}
func newBlockedMockTx(t *testing.T, ctx context.Context) (*sql.DB, sqlmock.Sqlmock, *sql.Tx) {
t.Helper()
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
mock.ExpectBegin()
tx, err := db.BeginTx(ctx, nil)
if err != nil {
t.Fatalf("BeginTx: %v", err)
}
return db, mock, tx
}
func closeBlockedMockTx(t *testing.T, db *sql.DB, mock sqlmock.Sqlmock, tx *sql.Tx) {
t.Helper()
mock.ExpectRollback()
if err := tx.Rollback(); err != nil {
t.Fatalf("Rollback: %v", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet SQL expectations: %v", err)
}
mock.ExpectClose()
if err := db.Close(); err != nil {
t.Fatalf("Close: %v", err)
}
}
func assertStringSet(t *testing.T, got []string, want []string) {
t.Helper()
sort.Strings(got)
sort.Strings(want)
if fmt.Sprint(got) != fmt.Sprint(want) {
t.Fatalf("IDs = %v, want %v", got, want)
}
}
func containsString(values []string, want string) bool {
for _, value := range values {
if value == want {
return true
}
}
return false
}

View file

@ -192,6 +192,11 @@ func DeleteIssuesBySourceRepoInTx(ctx context.Context, tx *sql.Tx, sourceRepo st
return 0, nil
}
affectedIssues, affectedWisps, aerr := AffectedByDeletionInTx(ctx, tx, issueIDs, nil)
if aerr != nil {
return 0, fmt.Errorf("affected by source-repo delete: %w", aerr)
}
result, err := tx.ExecContext(ctx, `DELETE FROM issues WHERE source_repo = ?`, sourceRepo)
if err != nil {
return 0, fmt.Errorf("delete issues: %w", err)
@ -201,6 +206,11 @@ func DeleteIssuesBySourceRepoInTx(ctx context.Context, tx *sql.Tx, sourceRepo st
if err != nil {
return 0, fmt.Errorf("rows affected: %w", err)
}
if err := RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
return int(rowsAffected), fmt.Errorf("recompute is_blocked after source-repo delete: %w", err)
}
return int(rowsAffected), nil
}

View file

@ -116,7 +116,6 @@ func ClaimReadyIssueInTx(
tx *sql.Tx,
filter types.WorkFilter,
actor string,
computeBlockedFn func(ctx context.Context, tx *sql.Tx, includeWisps bool) ([]string, error),
) (*types.Issue, error) {
claimFilter := filter
claimFilter.Status = types.StatusOpen
@ -124,7 +123,7 @@ func ClaimReadyIssueInTx(
claimFilter.Assignee = nil
claimFilter.Limit = 0
readyIssues, err := GetReadyWorkInTx(ctx, tx, claimFilter, computeBlockedFn)
readyIssues, err := GetReadyWorkInTx(ctx, tx, claimFilter)
if err != nil {
return nil, err
}

View file

@ -17,12 +17,30 @@ type CloseResult struct {
// CloseIssueInTx closes an issue within a transaction, setting status to closed
// and recording the close event. Routes to the correct table (issues/wisps)
// automatically. The caller is responsible for Dolt versioning if needed.
//
//nolint:gosec // G201: table names come from WispTableRouting (hardcoded constants)
func CloseIssueInTx(ctx context.Context, tx *sql.Tx, id string, reason, actor, session string) (*CloseResult, error) {
return closeIssueInTx(ctx, tx, id, reason, actor, session, true)
}
func CloseIssueWithoutEventInTx(ctx context.Context, tx *sql.Tx, id string, reason, actor, session string) (*CloseResult, error) {
return closeIssueInTx(ctx, tx, id, reason, actor, session, false)
}
//nolint:gosec // G201: table names come from WispTableRouting (hardcoded constants)
func closeIssueInTx(ctx context.Context, tx *sql.Tx, id string, reason, actor, session string, recordEvent bool) (*CloseResult, error) {
isWisp := IsActiveWispInTx(ctx, tx, id)
issueTable, _, eventTable, _ := WispTableRouting(isWisp)
var affectedIssues, affectedWisps []string
var aerr error
if isWisp {
affectedIssues, affectedWisps, aerr = AffectedByStatusChangeForWispInTx(ctx, tx, id)
} else {
affectedIssues, affectedWisps, aerr = AffectedByStatusChangeInTx(ctx, tx, id)
}
if aerr != nil {
return nil, fmt.Errorf("affected by close for %s: %w", id, aerr)
}
now := time.Now().UTC()
result, err := tx.ExecContext(ctx, fmt.Sprintf(`
@ -41,8 +59,14 @@ func CloseIssueInTx(ctx context.Context, tx *sql.Tx, id string, reason, actor, s
return nil, fmt.Errorf("issue not found: %s", id)
}
if err := RecordEventInTable(ctx, tx, eventTable, id, types.EventClosed, actor, reason); err != nil {
return nil, fmt.Errorf("failed to record event: %w", err)
if recordEvent {
if err := RecordEventInTable(ctx, tx, eventTable, id, types.EventClosed, actor, reason); err != nil {
return nil, fmt.Errorf("failed to record event: %w", err)
}
}
if err := RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
return nil, fmt.Errorf("recompute is_blocked after close for %s: %w", id, err)
}
return &CloseResult{IsWisp: isWisp}, nil

View file

@ -47,6 +47,154 @@ func ParseCommaSeparatedList(value string) []string {
return result
}
// ResolveCustomConfigInTx reads custom statuses and custom types from their
// normalized tables in a single transaction, falling back to a single
// batched config-table read when either table is empty or missing.
// Replaces two separate Resolve*-style call sequences (each of which would
// issue its own per-key SELECT value FROM config WHERE key=? fallback) with
// at most 3 queries: two table scans plus one batched config lookup keyed
// on ('status.custom', 'types.custom').
//
// Read-path callers (DoltStore / EmbeddedDoltStore cache loaders) should
// route through this helper so the two caches are populated by one tx.
// Write-path callers (create/update) keep using the per-key resolvers when
// they only need one side.
func ResolveCustomConfigInTx(ctx context.Context, tx *sql.Tx) (statuses []types.CustomStatus, customTypes []string, err error) {
statuses, statusesFromTable, err := resolveCustomStatusesFromTableInTx(ctx, tx)
if err != nil {
return nil, nil, err
}
customTypes, typesFromTable, err := resolveCustomTypesFromTableInTx(ctx, tx)
if err != nil {
return nil, nil, err
}
if statusesFromTable && typesFromTable {
return statuses, customTypes, nil
}
// One batched config read covers both fallback keys.
cfg, err := getConfigKeysInTx(ctx, tx, "status.custom", "types.custom")
if err != nil {
// On config-table failure, give each side a chance to fall back to
// YAML before giving up entirely.
if !statusesFromTable {
if yamlStatuses := config.GetCustomStatusesFromYAML(); len(yamlStatuses) > 0 {
statuses = ParseStatusFallback(yamlStatuses)
}
}
if !typesFromTable {
if yamlTypes := config.GetCustomTypesFromYAML(); len(yamlTypes) > 0 {
customTypes = yamlTypes
}
}
return statuses, customTypes, nil
}
if !statusesFromTable {
if v := cfg["status.custom"]; v != "" {
if parsed, parseErr := types.ParseCustomStatusConfig(v); parseErr == nil {
statuses = parsed
}
// parse error: leave statuses nil (degraded mode, same as
// ResolveCustomStatusesDetailedInTx behavior).
} else if yamlStatuses := config.GetCustomStatusesFromYAML(); len(yamlStatuses) > 0 {
statuses = ParseStatusFallback(yamlStatuses)
}
}
if !typesFromTable {
if v := cfg["types.custom"]; v != "" {
var jsonTypes []string
if jsonErr := json.Unmarshal([]byte(v), &jsonTypes); jsonErr == nil {
customTypes = jsonTypes
} else {
customTypes = ParseCommaSeparatedList(v)
}
} else if yamlTypes := config.GetCustomTypesFromYAML(); len(yamlTypes) > 0 {
customTypes = yamlTypes
}
}
return statuses, customTypes, nil
}
// resolveCustomStatusesFromTableInTx reads the normalized custom_statuses
// table. The second return value reports whether the table yielded rows
// (true = trust this result, false = caller must apply fallbacks).
func resolveCustomStatusesFromTableInTx(ctx context.Context, tx *sql.Tx) ([]types.CustomStatus, bool, error) {
rows, err := tx.QueryContext(ctx, "SELECT name, category FROM custom_statuses ORDER BY name")
if err != nil {
// Table missing on pre-migration schemas: caller falls back.
return nil, false, nil
}
defer rows.Close()
var result []types.CustomStatus
for rows.Next() {
var name, category string
if err := rows.Scan(&name, &category); err != nil {
continue
}
result = append(result, types.CustomStatus{
Name: name,
Category: types.StatusCategory(category),
})
}
if err := rows.Err(); err != nil {
return nil, false, fmt.Errorf("reading custom_statuses: %w", err)
}
return result, len(result) > 0, nil
}
// resolveCustomTypesFromTableInTx reads the normalized custom_types table.
// Same shape as resolveCustomStatusesFromTableInTx.
func resolveCustomTypesFromTableInTx(ctx context.Context, tx *sql.Tx) ([]string, bool, error) {
rows, err := tx.QueryContext(ctx, "SELECT name FROM custom_types ORDER BY name")
if err != nil {
return nil, false, nil
}
defer rows.Close()
var result []string
for rows.Next() {
var name string
if err := rows.Scan(&name); err != nil {
continue
}
result = append(result, name)
}
if err := rows.Err(); err != nil {
return nil, false, fmt.Errorf("reading custom_types: %w", err)
}
return result, len(result) > 0, nil
}
// getConfigKeysInTx fetches multiple config keys in one SELECT … WHERE key IN
// (…) statement. Missing keys are omitted from the result map.
func getConfigKeysInTx(ctx context.Context, tx *sql.Tx, keys ...string) (map[string]string, error) {
if len(keys) == 0 {
return map[string]string{}, nil
}
placeholders := make([]string, len(keys))
args := make([]interface{}, len(keys))
for i, k := range keys {
placeholders[i] = "?"
args[i] = k
}
//nolint:gosec // G201: only ? placeholders are formatted in.
q := fmt.Sprintf("SELECT `key`, value FROM config WHERE `key` IN (%s)", strings.Join(placeholders, ","))
rows, err := tx.QueryContext(ctx, q, args...)
if err != nil {
return nil, fmt.Errorf("get config keys: %w", err)
}
defer rows.Close()
result := make(map[string]string, len(keys))
for rows.Next() {
var k, v string
if err := rows.Scan(&k, &v); err != nil {
return nil, fmt.Errorf("get config keys: scan: %w", err)
}
result[k] = v
}
return result, rows.Err()
}
// ResolveCustomStatusesDetailedInTx reads custom statuses from the custom_statuses
// table, falling back to the config string and then config.yaml if the table
// doesn't exist (pre-migration databases).

View file

@ -113,7 +113,45 @@ func CreateIssuesInTx(ctx context.Context, tx *sql.Tx, issues []*types.Issue, ac
return err
}
return ReconcileChildCounters(ctx, tx, issues)
if err := ReconcileChildCounters(ctx, tx, issues); err != nil {
return err
}
issueSeen := make(map[string]bool, len(issues))
wispSeen := make(map[string]bool, len(issues))
issueIDs := make([]string, 0, len(issues))
wispIDs := make([]string, 0, len(issues))
add := func(id string, isWisp bool) {
if id == "" {
return
}
if isWisp {
if !wispSeen[id] {
wispSeen[id] = true
wispIDs = append(wispIDs, id)
}
return
}
if !issueSeen[id] {
issueSeen[id] = true
issueIDs = append(issueIDs, id)
}
}
for _, issue := range issues {
if issue == nil {
continue
}
isWisp := IsWisp(issue)
add(issue.ID, isWisp)
for _, dep := range issue.Dependencies {
src := dep.IssueID
if src == "" {
src = issue.ID
}
add(src, isWisp)
}
}
return RecomputeIsBlockedInTx(ctx, tx, issueIDs, wispIDs)
}
// PrepareIssueForInsert normalizes timestamps, validates, and computes the content hash.

View file

@ -19,13 +19,36 @@ const maxRecursiveResults = 10000
//nolint:gosec // G201: table names come from WispTableRouting (hardcoded constants)
func DeleteIssueInTx(ctx context.Context, tx *sql.Tx, id string) error {
isWisp := IsActiveWispInTx(ctx, tx, id)
issueTable, _, _, _ := WispTableRouting(isWisp)
var deletedIssues, deletedWisps []string
if isWisp {
deletedWisps = []string{id}
} else {
deletedIssues = []string{id}
}
affectedIssues, affectedWisps, aerr := AffectedByDeletionInTx(ctx, tx, deletedIssues, deletedWisps)
if aerr != nil {
return fmt.Errorf("affected by delete for %s: %w", id, aerr)
}
if err := deleteIssueRowInTx(ctx, tx, id, isWisp); err != nil {
return err
}
if err := RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
return fmt.Errorf("recompute is_blocked after delete for %s: %w", id, err)
}
return nil
}
//nolint:gosec // G201: table names come from WispTableRouting (hardcoded constants)
func deleteIssueRowInTx(ctx context.Context, tx *sql.Tx, id string, isWisp bool) error {
issueTable, _, _, _ := WispTableRouting(isWisp)
result, err := tx.ExecContext(ctx, fmt.Sprintf("DELETE FROM %s WHERE id = ?", issueTable), id)
if err != nil {
return fmt.Errorf("delete issue from %s: %w", issueTable, err)
}
rows, err := result.RowsAffected()
if err != nil {
return fmt.Errorf("get rows affected: %w", err)
@ -33,13 +56,11 @@ func DeleteIssueInTx(ctx context.Context, tx *sql.Tx, id string) error {
if rows == 0 {
return fmt.Errorf("issue not found: %s", id)
}
if isWisp {
if err := DeleteWispFromDependenciesInTx(ctx, tx, id); err != nil {
return err
}
}
return nil
}
@ -49,55 +70,35 @@ func DeleteIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string, cascade boo
return &types.DeleteIssuesResult{}, nil
}
wispIDs, regularIDs, err := PartitionWispIDsInTx(ctx, tx, ids)
initialWispIDs, regularIDs, err := PartitionWispIDsInTx(ctx, tx, ids)
if err != nil {
return nil, err
}
wispDeleteCount := 0
if len(wispIDs) > 0 && !dryRun {
for _, id := range wispIDs {
if err := DeleteIssueInTx(ctx, tx, id); err != nil {
return nil, fmt.Errorf("delete wisp %s: %w", id, err)
}
wispDeleteCount++
}
} else {
wispDeleteCount = len(wispIDs)
}
ids = regularIDs
if len(ids) == 0 {
return &types.DeleteIssuesResult{DeletedCount: wispDeleteCount}, nil
}
idSet := make(map[string]bool, len(ids)+len(wispIDs))
idSet := make(map[string]bool, len(ids))
for _, id := range ids {
idSet[id] = true
}
for _, id := range wispIDs {
idSet[id] = true
}
result := &types.DeleteIssuesResult{}
expandedIDs := ids
expandedRegularIDs := regularIDs
if cascade {
allToDelete, err := findAllDependentsRecursiveInTx(ctx, tx, ids)
allToDelete, err := findAllDependentsRecursiveInTx(ctx, tx, regularIDs)
if err != nil {
return nil, fmt.Errorf("find dependents: %w", err)
}
expandedIDs = make([]string, 0, len(allToDelete))
expandedRegularIDs = make([]string, 0, len(allToDelete))
for id := range allToDelete {
expandedIDs = append(expandedIDs, id)
expandedRegularIDs = append(expandedRegularIDs, id)
}
} else if !force {
for i := 0; i < len(ids); i += deleteBatchSize {
for i := 0; i < len(regularIDs); i += deleteBatchSize {
end := i + deleteBatchSize
if end > len(ids) {
end = len(ids)
if end > len(regularIDs) {
end = len(regularIDs)
}
batch := ids[i:end]
batch := regularIDs[i:end]
inClause, args := buildSQLInClause(batch)
externalBySource := make(map[string][]string)
@ -136,59 +137,61 @@ func DeleteIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string, cascade boo
}
}
} else {
orphans, err := findExternalDependentsBatchedInTx(ctx, tx, ids, idSet)
orphans, err := findExternalDependentsBatchedInTx(ctx, tx, regularIDs, idSet)
if err != nil {
return nil, fmt.Errorf("get dependents: %w", err)
}
result.OrphanedIssues = orphans
}
allExpandedIDs := expandedIDs
expandedWispIDs, expandedRegularIDs, err := PartitionWispIDsInTx(ctx, tx, allExpandedIDs)
cascadeWispIDs, finalRegularIDs, err := PartitionWispIDsInTx(ctx, tx, expandedRegularIDs)
if err != nil {
return nil, fmt.Errorf("partition expanded delete IDs: %w", err)
}
expandedIDSet := make(map[string]bool, len(allExpandedIDs))
for _, id := range allExpandedIDs {
expandedIDSet[id] = true
allWispIDs := append(append([]string{}, initialWispIDs...), cascadeWispIDs...)
allDeletedSet := make(map[string]bool, len(finalRegularIDs)+len(allWispIDs))
for _, id := range finalRegularIDs {
allDeletedSet[id] = true
}
for _, id := range allWispIDs {
allDeletedSet[id] = true
}
expandedIDs = expandedRegularIDs
var depsCount, labelsCount, eventsCount int
if depsCount, err = countRowsForIssueIDsInTx(ctx, tx, "dependencies", expandedIDs); err != nil {
if depsCount, err = countRowsForIssueIDsInTx(ctx, tx, "dependencies", finalRegularIDs); err != nil {
return nil, fmt.Errorf("count dependencies: %w", err)
}
wispDepsCount, err := countRowsForIssueIDsInTx(ctx, tx, "wisp_dependencies", expandedWispIDs)
wispDepsCount, err := countRowsForIssueIDsInTx(ctx, tx, "wisp_dependencies", cascadeWispIDs)
if err != nil {
return nil, fmt.Errorf("count wisp dependencies: %w", err)
}
depsCount += wispDepsCount
if labelsCount, err = countRowsForIssueIDsInTx(ctx, tx, "labels", expandedIDs); err != nil {
if labelsCount, err = countRowsForIssueIDsInTx(ctx, tx, "labels", finalRegularIDs); err != nil {
return nil, fmt.Errorf("count labels: %w", err)
}
wispLabelsCount, err := countRowsForIssueIDsInTx(ctx, tx, "wisp_labels", expandedWispIDs)
wispLabelsCount, err := countRowsForIssueIDsInTx(ctx, tx, "wisp_labels", cascadeWispIDs)
if err != nil {
return nil, fmt.Errorf("count wisp labels: %w", err)
}
labelsCount += wispLabelsCount
if eventsCount, err = countRowsForIssueIDsInTx(ctx, tx, "events", expandedIDs); err != nil {
if eventsCount, err = countRowsForIssueIDsInTx(ctx, tx, "events", finalRegularIDs); err != nil {
return nil, fmt.Errorf("count events: %w", err)
}
wispEventsCount, err := countRowsForIssueIDsInTx(ctx, tx, "wisp_events", expandedWispIDs)
wispEventsCount, err := countRowsForIssueIDsInTx(ctx, tx, "wisp_events", cascadeWispIDs)
if err != nil {
return nil, fmt.Errorf("count wisp events: %w", err)
}
eventsCount += wispEventsCount
// Pass 2: inbound deps from outside the deletion set.
for i := 0; i < len(allExpandedIDs); i += deleteBatchSize {
for i := 0; i < len(expandedRegularIDs); i += deleteBatchSize {
end := i + deleteBatchSize
if end > len(allExpandedIDs) {
end = len(allExpandedIDs)
if end > len(expandedRegularIDs) {
end = len(expandedRegularIDs)
}
batch := allExpandedIDs[i:end]
batch := expandedRegularIDs[i:end]
batchInClause, batchArgs := buildSQLInClause(batch)
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
@ -207,7 +210,7 @@ func DeleteIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string, cascade boo
_ = rows.Close()
return nil, fmt.Errorf("scan inbound dependency: %w", err)
}
if !expandedIDSet[issID] {
if !allDeletedSet[issID] {
depsCount++
}
}
@ -221,25 +224,30 @@ func DeleteIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string, cascade boo
result.DependenciesCount = depsCount
result.LabelsCount = labelsCount
result.EventsCount = eventsCount
result.DeletedCount = len(expandedIDs) + len(expandedWispIDs) + wispDeleteCount
result.DeletedCount = len(finalRegularIDs) + len(allWispIDs)
if dryRun {
return result, nil
}
for _, id := range expandedWispIDs {
if err := DeleteIssueInTx(ctx, tx, id); err != nil {
return nil, fmt.Errorf("delete cascaded wisp %s: %w", id, err)
affectedIssues, affectedWisps, aerr := AffectedByDeletionInTx(ctx, tx, finalRegularIDs, allWispIDs)
if aerr != nil {
return nil, fmt.Errorf("affected by batch delete: %w", aerr)
}
for _, id := range allWispIDs {
if err := deleteIssueRowInTx(ctx, tx, id, true); err != nil {
return nil, fmt.Errorf("delete wisp %s: %w", id, err)
}
}
totalDeleted := 0
for i := 0; i < len(expandedIDs); i += deleteBatchSize {
totalRegularsDeleted := 0
for i := 0; i < len(finalRegularIDs); i += deleteBatchSize {
end := i + deleteBatchSize
if end > len(expandedIDs) {
end = len(expandedIDs)
if end > len(finalRegularIDs) {
end = len(finalRegularIDs)
}
batch := expandedIDs[i:end]
batch := finalRegularIDs[i:end]
batchInClause, batchArgs := buildSQLInClause(batch)
deleteResult, err := tx.ExecContext(ctx,
@ -249,9 +257,13 @@ func DeleteIssuesInTx(ctx context.Context, tx *sql.Tx, ids []string, cascade boo
return nil, fmt.Errorf("delete issues: %w", err)
}
rowsAffected, _ := deleteResult.RowsAffected()
totalDeleted += int(rowsAffected)
totalRegularsDeleted += int(rowsAffected)
}
result.DeletedCount = totalRegularsDeleted + len(allWispIDs)
if err := RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
return nil, fmt.Errorf("recompute is_blocked after batch delete: %w", err)
}
result.DeletedCount = totalDeleted + len(expandedWispIDs) + wispDeleteCount
return result, nil
}

View file

@ -224,6 +224,21 @@ func AddDependencyInTx(ctx context.Context, tx *sql.Tx, dep *types.Dependency, a
`, writeTable, targetCol), dep.IssueID, dep.DependsOnID, dep.Type, actor, metadata, dep.ThreadID); err != nil {
return fmt.Errorf("failed to add dependency: %w", err)
}
srcIsWisp := writeTable == "wisp_dependencies"
var affectedIssues, affectedWisps []string
var aerr error
if srcIsWisp {
affectedIssues, affectedWisps, aerr = AffectedByDepChangeForWispInTx(ctx, tx, dep.IssueID, dep.DependsOnID, dep.Type)
} else {
affectedIssues, affectedWisps, aerr = AffectedByDepChangeInTx(ctx, tx, dep.IssueID, dep.DependsOnID, dep.Type)
}
if aerr != nil {
return fmt.Errorf("affected by add dependency %s -> %s: %w", dep.IssueID, dep.DependsOnID, aerr)
}
if err := RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
return fmt.Errorf("recompute is_blocked after add dependency %s -> %s: %w", dep.IssueID, dep.DependsOnID, err)
}
return nil
}
@ -486,11 +501,38 @@ func RemoveDependencyInTx(ctx context.Context, tx *sql.Tx, issueID, dependsOnID
isWisp := IsActiveWispInTx(ctx, tx, issueID)
_, _, _, depTable := WispTableRouting(isWisp)
// Capture the row's type before deleting so we can dispatch the right
// affected-set helper. If no row matches, treat as a no-op.
var depType string
row := tx.QueryRowContext(ctx, fmt.Sprintf(
`SELECT type FROM %s WHERE issue_id = ? AND %s = ?`, depTable, DepTargetExpr),
issueID, dependsOnID)
if err := row.Scan(&depType); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil
}
return fmt.Errorf("lookup dependency type for %s -> %s: %w", issueID, dependsOnID, err)
}
if _, err := tx.ExecContext(ctx, fmt.Sprintf(
`DELETE FROM %s WHERE issue_id = ? AND %s = ?`, depTable, DepTargetExpr),
issueID, dependsOnID); err != nil {
return fmt.Errorf("remove dependency: %w", err)
}
var affectedIssues, affectedWisps []string
var aerr error
if isWisp {
affectedIssues, affectedWisps, aerr = AffectedByDepChangeForWispInTx(ctx, tx, issueID, dependsOnID, types.DependencyType(depType))
} else {
affectedIssues, affectedWisps, aerr = AffectedByDepChangeInTx(ctx, tx, issueID, dependsOnID, types.DependencyType(depType))
}
if aerr != nil {
return fmt.Errorf("affected by remove dependency %s -> %s: %w", issueID, dependsOnID, aerr)
}
if err := RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
return fmt.Errorf("recompute is_blocked after remove dependency %s -> %s: %w", issueID, dependsOnID, err)
}
return nil
}

View file

@ -3,6 +3,7 @@ package issueops
import (
"context"
"database/sql"
"errors"
"fmt"
"sort"
"strings"
@ -142,6 +143,17 @@ func GetDependencyCountsInTx(ctx context.Context, tx *sql.Tx, issueIDs []string)
result[id] = &types.DependencyCounts{}
}
// Skip the wisp_dependencies pair on projects without wisps. wisp_dependencies
// requires at least one wisp on either side, so an empty wisps table implies
// an empty wisp_dependencies table. One cheap probe replaces two queries per
// batch (blockers + dependents).
depTables := []string{"dependencies", "wisp_dependencies"}
if empty, probeErr := wispsTableEmptyOrMissingInTx(ctx, tx); probeErr != nil {
return nil, fmt.Errorf("get dependency counts: probe: %w", probeErr)
} else if empty {
depTables = []string{"dependencies"}
}
for start := 0; start < len(issueIDs); start += queryBatchSize {
end := start + queryBatchSize
if end > len(issueIDs) {
@ -157,7 +169,7 @@ func GetDependencyCountsInTx(ctx context.Context, tx *sql.Tx, issueIDs []string)
}
inClause := strings.Join(placeholders, ",")
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
for _, depTable := range depTables {
// Blockers: issues that block the given IDs.
//nolint:gosec // G201: depTable is hardcoded and inClause contains only ? placeholders.
depRows, err := tx.QueryContext(ctx, fmt.Sprintf(`
@ -467,13 +479,9 @@ func loadStatusByIDInTx(ctx context.Context, tx *sql.Tx, ids []string) (map[stri
// GetNewlyUnblockedByCloseInTx finds issues that become unblocked when the
// given issue is closed. Works within an existing transaction.
// Returns full issue objects for the newly-unblocked issues.
// Uses separate single-table queries (no JOINs) to avoid Dolt's mergeJoinKvIter
// panic when joining across tables with different tuple formats.
//
//nolint:gosec // G201: table names come from hardcoded constants
func GetNewlyUnblockedByCloseInTx(ctx context.Context, tx *sql.Tx, closedIssueID string) ([]*types.Issue, error) {
// Step 1: Find issue IDs that depend on the closed issue via "blocks" deps.
// Query both dep tables to cover cross-table dependencies.
candidateSet := make(map[string]bool)
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
@ -499,12 +507,10 @@ func GetNewlyUnblockedByCloseInTx(ctx context.Context, tx *sql.Tx, closedIssueID
return nil, fmt.Errorf("candidate rows from %s: %w", depTable, err)
}
}
if len(candidateSet) == 0 {
return nil, nil
}
// Filter to only open/active candidates (check both tables, no JOINs).
candidateIDs := make([]string, 0, len(candidateSet))
for id := range candidateSet {
candidateIDs = append(candidateIDs, id)
@ -524,14 +530,10 @@ func GetNewlyUnblockedByCloseInTx(ctx context.Context, tx *sql.Tx, closedIssueID
activeCandidateIDs = append(activeCandidateIDs, id)
}
candidateIDs = activeCandidateIDs
if len(candidateIDs) == 0 {
return nil, nil
}
// Step 2: Filter out candidates that still have other open blockers.
// Batch by candidate to avoid one dependency/status round-trip per
// candidate on remote Dolt backends.
stillBlocked := make(map[string]bool)
for start := 0; start < len(candidateIDs); start += queryBatchSize {
end := start + queryBatchSize
@ -590,7 +592,6 @@ func GetNewlyUnblockedByCloseInTx(ctx context.Context, tx *sql.Tx, closedIssueID
}
}
// Step 3: Collect unblocked issues.
var unblocked []*types.Issue
for _, id := range candidateIDs {
if stillBlocked[id] {
@ -602,19 +603,37 @@ func GetNewlyUnblockedByCloseInTx(ctx context.Context, tx *sql.Tx, closedIssueID
}
unblocked = append(unblocked, issue)
}
return unblocked, nil
}
// IsBlockedInTx checks if an issue is blocked by active dependencies within
// an existing transaction. Returns whether the issue is blocked and, if so,
// a list of blocker descriptions for display.
// Uses separate single-table queries (no JOINs) to avoid Dolt's mergeJoinKvIter
// panic when joining across tables with different tuple formats.
//
//nolint:gosec // G201: table names are hardcoded constants.
func IsBlockedInTx(ctx context.Context, tx *sql.Tx, issueID string) (bool, []string, error) {
// Step 1: Get all blocking dependency targets from both dep tables.
var blocked bool
found := false
for _, table := range []string{"issues", "wisps"} {
var b int
//nolint:gosec // G201: table is a hardcoded "issues" or "wisps".
err := tx.QueryRowContext(ctx, "SELECT is_blocked FROM "+table+" WHERE id = ?", issueID).Scan(&b)
if err == nil {
blocked = b != 0
found = true
break
}
if !errors.Is(err, sql.ErrNoRows) {
if optionalBlockedTable(table) && isTableNotExistError(err) {
continue
}
return false, nil, fmt.Errorf("read is_blocked from %s: %w", table, err)
}
}
if !found || !blocked {
return false, nil, nil
}
type depEdge struct {
dependsOnID, depType string
}
@ -645,10 +664,9 @@ func IsBlockedInTx(ctx context.Context, tx *sql.Tx, issueID string) (bool, []str
}
if len(edges) == 0 {
return false, nil, nil
return true, nil, nil
}
// Step 2: Check each blocker's status in both issues and wisps tables.
blockerIDs := make([]string, 0, len(edges))
for _, e := range edges {
blockerIDs = append(blockerIDs, e.dependsOnID)
@ -659,12 +677,12 @@ func IsBlockedInTx(ctx context.Context, tx *sql.Tx, issueID string) (bool, []str
}
var blockers []string
for _, e := range edges {
status, found := statusByID[e.dependsOnID]
if !found {
continue // Blocker not found in either table
status, ok := statusByID[e.dependsOnID]
if !ok {
continue
}
if status == types.StatusClosed || status == types.StatusPinned {
continue // Not an active blocker
continue
}
if e.depType != "blocks" {
blockers = append(blockers, e.dependsOnID+" ("+e.depType+")")
@ -673,7 +691,7 @@ func IsBlockedInTx(ctx context.Context, tx *sql.Tx, issueID string) (bool, []str
}
}
return len(blockers) > 0, blockers, nil
return true, blockers, nil
}
// scanDependencyRow scans a single dependency row from a *sql.Rows.

View file

@ -9,8 +9,7 @@ import (
)
// GetEpicsEligibleForClosureInTx returns epics whose children are all closed.
// Uses separate single-table queries to avoid Dolt's joinIter panic on
// multi-table JOINs, and batches IN clauses for performance.
// Batches IN clauses for performance on large epic sets.
//
// nolint:gosec // G201: table names are hardcoded, placeholders contain only ? markers
func GetEpicsEligibleForClosureInTx(ctx context.Context, tx *sql.Tx) ([]*types.EpicStatus, error) {

View file

@ -16,11 +16,12 @@ type FilterTables struct {
Main string // "issues" or "wisps"
Labels string // "labels" or "wisp_labels"
Dependencies string // "dependencies" or "wisp_dependencies"
Comments string // "comments" or "wisp_comments"
}
var (
IssuesFilterTables = FilterTables{Main: "issues", Labels: "labels", Dependencies: "dependencies"}
WispsFilterTables = FilterTables{Main: "wisps", Labels: "wisp_labels", Dependencies: "wisp_dependencies"}
IssuesFilterTables = FilterTables{Main: "issues", Labels: "labels", Dependencies: "dependencies", Comments: "comments"}
WispsFilterTables = FilterTables{Main: "wisps", Labels: "wisp_labels", Dependencies: "wisp_dependencies", Comments: "wisp_comments"}
)
// BuildIssueFilterClauses builds WHERE clause fragments and args from a query

View file

@ -92,5 +92,16 @@ func PromoteFromEphemeralInTx(ctx context.Context, tx *sql.Tx, id string, actor
if rows == 0 {
return fmt.Errorf("wisp %s not found", id)
}
// id now lives in `issues` and any inbound refs have been retargeted to
// depends_on_issue_id. Refresh is_blocked for id plus its dependers and
// parent-child descendants.
affectedIssues, affectedWisps, aerr := AffectedByStatusChangeInTx(ctx, tx, id)
if aerr != nil {
return fmt.Errorf("affected by promote for %s: %w", id, aerr)
}
if err := RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
return fmt.Errorf("recompute is_blocked after promote for %s: %w", id, err)
}
return nil
}

View file

@ -13,18 +13,23 @@ import (
"github.com/steveyegge/beads/internal/types"
)
// GetReadyWorkInTx returns issues that are ready to work on (not blocked).
// computeBlockedFn is the caller's function for computing blocked IDs (since
// the DoltStore and EmbeddedDoltStore have different caching strategies).
//
//nolint:gosec // G201: whereSQL/orderBySQL built from hardcoded strings and ? placeholders
func GetReadyWorkInTx(
ctx context.Context,
tx *sql.Tx,
filter types.WorkFilter,
computeBlockedFn func(ctx context.Context, tx *sql.Tx, includeWisps bool) ([]string, error),
) ([]*types.Issue, error) {
// Status filtering: default to open OR in_progress.
// readyWorkPredicates holds the SQL fragments derived from a WorkFilter that
// are shared between the legacy id-only GetReadyWorkInTx path and the
// single-statement GetReadyWorkWithCountsInTx path.
type readyWorkPredicates struct {
whereSQL string
orderBySQL string
limitSQL string
args []interface{}
deferredChildIDs []string
}
// buildReadyWorkPredicates assembles the WHERE/ORDER BY/LIMIT fragments and
// the deferred-parent-children list once so both ready-work entry points stay
// in sync. The tables parameter controls which dep/label table names are
// substituted into the subquery clauses so the same builder produces a WHERE
// suitable for either issues or wisps.
func buildReadyWorkPredicates(ctx context.Context, tx *sql.Tx, filter types.WorkFilter, tables FilterTables) (*readyWorkPredicates, error) {
var statusClause string
if filter.Status != "" {
statusClause = "status = ?"
@ -34,6 +39,7 @@ func GetReadyWorkInTx(
whereClauses := []string{
statusClause,
"(pinned = 0 OR pinned IS NULL)",
"is_blocked = 0",
}
if !filter.IncludeEphemeral {
whereClauses = append(whereClauses, "(ephemeral = 0 OR ephemeral IS NULL)")
@ -48,9 +54,7 @@ func GetReadyWorkInTx(
args = append(args, *filter.Priority)
}
if filter.Type != "" {
// Keep the type predicate isolated from other indexed predicates to
// avoid Dolt's mergeJoinIter panic on type+status+priority queries.
whereClauses = append(whereClauses, "id IN (SELECT id FROM issues WHERE issue_type = ?)")
whereClauses = append(whereClauses, "issue_type = ?")
args = append(args, filter.Type)
} else {
excludeTypes := readyWorkExcludeTypes(filter.ExcludeTypes)
@ -59,22 +63,20 @@ func GetReadyWorkInTx(
placeholders[i] = "?"
args = append(args, string(t))
}
whereClauses = append(whereClauses, fmt.Sprintf("id IN (SELECT id FROM issues WHERE issue_type NOT IN (%s))", strings.Join(placeholders, ",")))
whereClauses = append(whereClauses, fmt.Sprintf("issue_type NOT IN (%s)", strings.Join(placeholders, ",")))
}
// Unassigned takes precedence over Assignee filter.
if filter.Unassigned {
whereClauses = append(whereClauses, "(assignee IS NULL OR assignee = '')")
} else if filter.Assignee != nil {
whereClauses = append(whereClauses, "assignee = ?")
args = append(args, *filter.Assignee)
}
// Exclude future-deferred issues unless IncludeDeferred is set.
var deferredChildIDs []string
if !filter.IncludeDeferred {
whereClauses = append(whereClauses, "(defer_until IS NULL OR defer_until <= UTC_TIMESTAMP())")
}
// Exclude children of future-deferred parents.
if !filter.IncludeDeferred {
deferredChildIDs, dcErr := getChildrenOfDeferredParentsInTx(ctx, tx)
var dcErr error
deferredChildIDs, dcErr = getChildrenOfDeferredParentsInTx(ctx, tx)
if dcErr != nil {
return nil, fmt.Errorf("get ready work: compute deferred parent children: %w", dcErr)
}
@ -90,9 +92,10 @@ func GetReadyWorkInTx(
}
}
}
if len(filter.Labels) > 0 {
for _, label := range filter.Labels {
whereClauses = append(whereClauses, "id IN (SELECT issue_id FROM labels WHERE label = ?)")
whereClauses = append(whereClauses, fmt.Sprintf("id IN (SELECT issue_id FROM %s WHERE label = ?)", tables.Labels))
args = append(args, label)
}
}
@ -102,8 +105,9 @@ func GetReadyWorkInTx(
placeholders[i] = "?"
args = append(args, label)
}
whereClauses = append(whereClauses, fmt.Sprintf("id NOT IN (SELECT issue_id FROM labels WHERE label IN (%s))", strings.Join(placeholders, ", ")))
whereClauses = append(whereClauses, fmt.Sprintf("id NOT IN (SELECT issue_id FROM %s WHERE label IN (%s))", tables.Labels, strings.Join(placeholders, ", ")))
}
// Parent filtering: return all transitive descendants of parentID.
// GH#3396: previously was a one-hop subquery against dependencies, so
// grandchildren were silently dropped despite the help text and
@ -114,7 +118,7 @@ func GetReadyWorkInTx(
if descErr != nil {
return nil, fmt.Errorf("get parent descendants: %w", descErr)
}
parentClauses := []string{"(id LIKE CONCAT(?, '.%') AND id NOT IN (SELECT issue_id FROM dependencies WHERE type = 'parent-child'))"}
parentClauses := []string{fmt.Sprintf("(id LIKE CONCAT(?, '.%%') AND id NOT IN (SELECT issue_id FROM %s WHERE type = 'parent-child'))", tables.Dependencies)}
args = append(args, parentID)
for start := 0; start < len(descendantIDs); start += queryBatchSize {
end := start + queryBatchSize
@ -128,13 +132,11 @@ func GetReadyWorkInTx(
whereClauses = append(whereClauses, "("+strings.Join(parentClauses, " OR ")+")")
}
// Molecule filtering: filter to direct children of the specified molecule.
if filter.MoleculeID != "" {
whereClauses = append(whereClauses, fmt.Sprintf("(id IN (SELECT issue_id FROM dependencies WHERE type = 'parent-child' AND %s = ?) OR (id LIKE CONCAT(?, '.%%') AND id NOT IN (SELECT issue_id FROM dependencies WHERE type = 'parent-child')))", DepTargetExpr))
whereClauses = append(whereClauses, fmt.Sprintf("(id IN (SELECT issue_id FROM %s WHERE type = 'parent-child' AND %s = ?) OR (id LIKE CONCAT(?, '.%%') AND id NOT IN (SELECT issue_id FROM %s WHERE type = 'parent-child')))", tables.Dependencies, DepTargetExpr, tables.Dependencies))
args = append(args, filter.MoleculeID, filter.MoleculeID)
}
// Metadata existence check.
if filter.HasMetadataKey != "" {
if err := storage.ValidateMetadataKey(filter.HasMetadataKey); err != nil {
return nil, err
@ -143,7 +145,6 @@ func GetReadyWorkInTx(
args = append(args, storage.JSONMetadataPath(filter.HasMetadataKey))
}
// Metadata field equality filters.
if len(filter.MetadataFields) > 0 {
metaKeys := make([]string, 0, len(filter.MetadataFields))
for k := range filter.MetadataFields {
@ -159,37 +160,8 @@ func GetReadyWorkInTx(
}
}
// Exclude blocked issues eagerly for unbounded queries. Limited queries page
// candidate IDs first and filter blockers per page below, avoiding a full
// dependency graph scan when the caller only needs a small ready set.
if filter.Limit == 0 {
blockedIDs, err := computeBlockedFn(ctx, tx, true)
if err != nil {
return nil, fmt.Errorf("compute blocked IDs: %w", err)
}
if len(blockedIDs) > 0 {
// Also exclude children of blocked parents.
childrenOfBlocked, childErr := getChildrenOfIssuesInTx(ctx, tx, blockedIDs)
if childErr != nil {
return nil, fmt.Errorf("compute blocked children: %w", childErr)
}
blockedIDs = append(blockedIDs, childrenOfBlocked...)
for start := 0; start < len(blockedIDs); start += queryBatchSize {
end := start + queryBatchSize
if end > len(blockedIDs) {
end = len(blockedIDs)
}
placeholders, batchArgs := buildSQLInClause(blockedIDs[start:end])
args = append(args, batchArgs...)
whereClauses = append(whereClauses, fmt.Sprintf("id NOT IN (%s)", placeholders))
}
}
}
whereSQL := "WHERE " + strings.Join(whereClauses, " AND ")
// Build ORDER BY clause based on SortPolicy.
var orderBySQL string
switch filter.SortPolicy {
case types.SortPolicyOldest:
@ -207,61 +179,44 @@ func GetReadyWorkInTx(
orderBySQL = "ORDER BY priority ASC, created_at DESC, id ASC"
}
var issueIDs []string
var limitSQL string
if filter.Limit > 0 {
pageSize := readyWorkPageSize(filter.Limit)
for offset := 0; len(issueIDs) < filter.Limit; offset += pageSize {
//nolint:gosec // G201: whereSQL/orderBySQL are hardcoded, pageSize/offset are integers
query := fmt.Sprintf(`
SELECT id FROM issues
%s
%s
LIMIT %d OFFSET %d
`, whereSQL, orderBySQL, pageSize, offset)
limitSQL = fmt.Sprintf("LIMIT %d", filter.Limit)
}
pageIDs, err := queryReadyIssueIDPage(ctx, tx, query, args)
if err != nil {
return nil, err
}
if len(pageIDs) == 0 {
break
}
return &readyWorkPredicates{
whereSQL: whereSQL,
orderBySQL: orderBySQL,
limitSQL: limitSQL,
args: args,
deferredChildIDs: deferredChildIDs,
}, nil
}
blockedPageIDs, err := ComputeBlockedCandidateIDsInTx(ctx, tx, pageIDs, true)
if err != nil {
return nil, fmt.Errorf("get ready work: filter blocked candidates: %w", err)
}
blockedPageSet := make(map[string]struct{}, len(blockedPageIDs))
for _, id := range blockedPageIDs {
blockedPageSet[id] = struct{}{}
}
// GetReadyWorkInTx returns issues that are ready to work on (not blocked).
//
//nolint:gosec // G201: whereSQL/orderBySQL built from hardcoded strings and ? placeholders
func GetReadyWorkInTx(
ctx context.Context,
tx *sql.Tx,
filter types.WorkFilter,
) ([]*types.Issue, error) {
preds, err := buildReadyWorkPredicates(ctx, tx, filter, IssuesFilterTables)
if err != nil {
return nil, err
}
for _, id := range pageIDs {
if _, blocked := blockedPageSet[id]; blocked {
continue
}
issueIDs = append(issueIDs, id)
if len(issueIDs) >= filter.Limit {
break
}
}
if len(pageIDs) < pageSize {
break
}
}
} else {
//nolint:gosec // G201: whereSQL/orderBySQL are hardcoded strings and ? placeholders
query := fmt.Sprintf(`
SELECT id FROM issues
%s
%s
`, whereSQL, orderBySQL)
//nolint:gosec // G201: fragments are hardcoded; only ? placeholders carry user input.
query := fmt.Sprintf(`
SELECT id FROM issues
%s
%s
%s
`, preds.whereSQL, preds.orderBySQL, preds.limitSQL)
var err error
issueIDs, err = queryReadyIssueIDPage(ctx, tx, query, args)
if err != nil {
return nil, err
}
issueIDs, err := queryReadyIssueIDPage(ctx, tx, query, preds.args)
if err != nil {
return nil, err
}
// Batch-fetch full issues preserving order.
@ -280,7 +235,7 @@ func GetReadyWorkInTx(
}
}
wisps, wErr := getReadyWispsInTx(ctx, tx, filter)
wisps, wErr := getReadyWispsInTx(ctx, tx, filter, preds.deferredChildIDs)
if wErr != nil {
return nil, wErr
}
@ -312,7 +267,21 @@ func mergeReadyWisps(ordered []*types.Issue, wisps []*types.Issue, filter types.
return ordered, nil
}
func getReadyWispsInTx(ctx context.Context, tx *sql.Tx, filter types.WorkFilter) ([]*types.Issue, error) {
// deferredChildIDs is precomputed by GetReadyWorkInTx so this helper does not
// re-issue the deferred-parent probes that the issues path already ran.
// Pass nil when filter.IncludeDeferred is true.
func getReadyWispsInTx(ctx context.Context, tx *sql.Tx, filter types.WorkFilter, deferredChildIDs []string) ([]*types.Issue, error) {
// Skip the wisp candidate scan entirely when the wisps table is missing
// or empty — projects that never use wisps would otherwise pay one full
// SELECT against wisps per bd ready invocation.
empty, err := wispsTableEmptyOrMissingInTx(ctx, tx)
if err != nil {
return nil, fmt.Errorf("search wisps (ready work): probe: %w", err)
}
if empty {
return nil, nil
}
wispFilter := readyWorkWispIssueFilter(filter)
// Ready-only wisp predicates are applied after search, so avoid limiting
// before that filtering can drop non-ready candidates.
@ -324,7 +293,7 @@ func getReadyWispsInTx(ctx context.Context, tx *sql.Tx, filter types.WorkFilter)
}
return nil, fmt.Errorf("search wisps (ready work): %w", err)
}
return filterReadyWispsInTx(ctx, tx, filter, wisps)
return filterReadyWispsInTx(ctx, tx, filter, wisps, deferredChildIDs)
}
func readyWorkExcludeTypes(extra []types.IssueType) []types.IssueType {
@ -393,7 +362,7 @@ func readyWorkWispIssueFilter(filter types.WorkFilter) types.IssueFilter {
return wispFilter
}
func filterReadyWispsInTx(ctx context.Context, tx *sql.Tx, filter types.WorkFilter, wisps []*types.Issue) ([]*types.Issue, error) {
func filterReadyWispsInTx(ctx context.Context, tx *sql.Tx, filter types.WorkFilter, wisps []*types.Issue, deferredChildIDs []string) ([]*types.Issue, error) {
if len(wisps) == 0 {
return wisps, nil
}
@ -438,21 +407,39 @@ func filterReadyWispsInTx(ctx context.Context, tx *sql.Tx, filter types.WorkFilt
excluded[wisp.ID] = struct{}{}
}
}
deferredChildIDs, err := getChildrenOfDeferredParentsInTx(ctx, tx)
if err != nil {
return nil, err
}
// deferredChildIDs is precomputed by GetReadyWorkInTx; do not re-probe.
for _, id := range deferredChildIDs {
excluded[id] = struct{}{}
}
}
blockedIDs, err := ComputeBlockedCandidateIDsInTx(ctx, tx, wispIDs, true)
if err != nil {
return nil, fmt.Errorf("get ready work: filter blocked wisps: %w", err)
}
for _, id := range blockedIDs {
excluded[id] = struct{}{}
// Filter wisps that are currently is_blocked = 1 (maintained by write-side
// instrumentation).
for start := 0; start < len(wispIDs); start += queryBatchSize {
end := start + queryBatchSize
if end > len(wispIDs) {
end = len(wispIDs)
}
placeholders, args := buildSQLInClause(wispIDs[start:end])
//nolint:gosec // G201: only IN-clause placeholders are formatted in.
rows, err := tx.QueryContext(ctx, fmt.Sprintf(`
SELECT id FROM wisps WHERE id IN (%s) AND is_blocked = 1
`, placeholders), args...)
if err != nil {
return nil, fmt.Errorf("get ready work: filter blocked wisps: %w", err)
}
for rows.Next() {
var id string
if err := rows.Scan(&id); err != nil {
_ = rows.Close()
return nil, fmt.Errorf("scan blocked wisp: %w", err)
}
excluded[id] = struct{}{}
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("blocked wisp rows: %w", err)
}
}
ready := wisps[:0]
@ -510,17 +497,6 @@ func issueCreatedBefore(a, b *types.Issue) bool {
return a.ID < b.ID
}
func readyWorkPageSize(limit int) int {
pageSize := limit * 4
if pageSize < 100 {
return 100
}
if pageSize > 1000 {
return 1000
}
return pageSize
}
func queryReadyIssueIDPage(ctx context.Context, tx *sql.Tx, query string, args []interface{}) ([]string, error) {
rows, err := tx.QueryContext(ctx, query, args...)
if err != nil {
@ -656,52 +632,6 @@ func getParentedIDSetInTx(ctx context.Context, tx *sql.Tx, issueIDs []string) (m
return parented, nil
}
// getChildrenOfIssuesInTx returns IDs of direct children (parent-child deps)
// of the given issue IDs. Scans both dependencies and wisp_dependencies tables.
//
//nolint:gosec // G201: depTable is hardcoded to "dependencies" or "wisp_dependencies"
func getChildrenOfIssuesInTx(ctx context.Context, tx *sql.Tx, parentIDs []string) ([]string, error) {
if len(parentIDs) == 0 {
return nil, nil
}
var children []string
for _, depTable := range []string{"dependencies", "wisp_dependencies"} {
for start := 0; start < len(parentIDs); start += queryBatchSize {
end := start + queryBatchSize
if end > len(parentIDs) {
end = len(parentIDs)
}
placeholders, args := buildSQLInClause(parentIDs[start:end])
query := fmt.Sprintf(`
SELECT issue_id FROM %s
WHERE type = 'parent-child' AND %s IN (%s)
`, depTable, DepTargetExpr, placeholders)
rows, err := tx.QueryContext(ctx, query, args...)
if err != nil {
// wisp_dependencies table may not exist on pre-migration databases.
if optionalBlockedTable(depTable) && isTableNotExistError(err) {
break
}
return nil, fmt.Errorf("get children of issues from %s: %w", depTable, err)
}
for rows.Next() {
var childID string
if err := rows.Scan(&childID); err != nil {
_ = rows.Close()
return nil, fmt.Errorf("get children of issues: scan: %w", err)
}
children = append(children, childID)
}
_ = rows.Close()
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("get children of issues: rows from %s: %w", depTable, err)
}
}
}
return children, nil
}
// buildSQLInClause builds a parameterized IN clause from a slice of IDs.
func buildSQLInClause(ids []string) (string, []interface{}) {
placeholders := make([]string, len(ids))

View file

@ -0,0 +1,217 @@
package issueops
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/steveyegge/beads/internal/types"
)
// GetReadyWorkWithCountsInTx returns ready-work issues with all per-issue
// hydration (labels, dependency records, dep/dependent/comment counts,
// parent ID) attached as a single SQL statement per side (issues + wisps).
// Wisps are merged in via a second mega-query whenever the wisps table is
// non-empty; the wisp branch is skipped entirely after one cheap probe on
// projects that never use wisps.
//
// No per-call hydration fallbacks: both the issues and wisps sides use the
// same six-LEFT-JOIN shape.
func GetReadyWorkWithCountsInTx(ctx context.Context, tx *sql.Tx, filter types.WorkFilter) ([]*types.IssueWithCounts, error) {
issuePreds, err := buildReadyWorkPredicates(ctx, tx, filter, IssuesFilterTables)
if err != nil {
return nil, err
}
out, err := runMegaQueryInTx(ctx, tx, IssuesFilterTables, issuePreds.whereSQL, issuePreds.orderBySQL, issuePreds.limitSQL, issuePreds.args)
if err != nil {
return nil, err
}
empty, probeErr := wispsTableEmptyOrMissingInTx(ctx, tx)
if probeErr != nil {
return nil, fmt.Errorf("get ready work with counts: wisp probe: %w", probeErr)
}
if empty {
return out, nil
}
wispPreds, err := buildReadyWorkPredicates(ctx, tx, filter, WispsFilterTables)
if err != nil {
return nil, err
}
wisps, err := runMegaQueryInTx(ctx, tx, WispsFilterTables, wispPreds.whereSQL, wispPreds.orderBySQL, wispPreds.limitSQL, wispPreds.args)
if err != nil {
if isTableNotExistError(err) {
return out, nil
}
return nil, err
}
if len(wisps) == 0 {
return out, nil
}
// Merge wisp results in, dedup, re-sort by SortPolicy, then trim.
seen := make(map[string]struct{}, len(out))
for _, iwc := range out {
if iwc != nil && iwc.Issue != nil {
seen[iwc.Issue.ID] = struct{}{}
}
}
for _, w := range wisps {
if w == nil || w.Issue == nil {
continue
}
if _, dup := seen[w.Issue.ID]; dup {
return nil, fmt.Errorf("get ready work with counts: id %q exists in both issues and wisps", w.Issue.ID)
}
out = append(out, w)
}
sortIssuesWithCountsByPolicy(out, filter.SortPolicy)
if filter.Limit > 0 && len(out) > filter.Limit {
out = out[:filter.Limit]
}
return out, nil
}
// sortIssuesWithCountsByPolicy applies the same WorkFilter.SortPolicy
// ordering used by sortReadyIssues, but operating on []*IssueWithCounts so
// the merged issues+wisps slice stays consistent with the SQL ORDER BY each
// side produced.
func sortIssuesWithCountsByPolicy(items []*types.IssueWithCounts, policy types.SortPolicy) {
if len(items) <= 1 {
return
}
issues := make([]*types.Issue, 0, len(items))
for _, item := range items {
if item == nil || item.Issue == nil {
continue
}
issues = append(issues, item.Issue)
}
if len(issues) != len(items) {
return
}
sortReadyIssues(issues, policy)
byID := make(map[string]int, len(issues))
for i, iss := range issues {
byID[iss.ID] = i
}
sorted := make([]*types.IssueWithCounts, len(items))
for _, item := range items {
sorted[byID[item.Issue.ID]] = item
}
copy(items, sorted)
}
// readyWorkIssueColumns is IssueSelectColumns rewritten with the "i." alias
// used by the mega-query. Kept in sync with IssueSelectColumns by deriving it
// at init time from the canonical constant.
var readyWorkIssueColumns = func() string {
raw := strings.ReplaceAll(IssueSelectColumns, "\n", " ")
raw = strings.ReplaceAll(raw, "\t", " ")
parts := strings.Split(raw, ",")
for i, p := range parts {
parts[i] = "i." + strings.TrimSpace(p)
}
return strings.Join(parts, ", ")
}()
// readyWorkDepJSONObject is the JSON_OBJECT(...) expression embedded inside
// JSON_ARRAYAGG to serialize a Dependency row in the mega-query. The field
// names mirror types.Dependency json tags so the result can be Unmarshal'd
// directly into []*types.Dependency.
//
// - created_at is DATETIME; Dolt's default JSON serialization renders it
// as "2006-01-02 15:04:05.000000" which Go's time.Time RFC3339 unmarshal
// cannot parse, so DATE_FORMAT it to RFC3339.
// - metadata is JSON; Dolt would embed the parsed JSON value, but
// types.Dependency.Metadata is a string. CAST AS CHAR converts the JSON
// value back into its string form so the outer JSON_OBJECT serializes
// it as a quoted string.
const readyWorkDepJSONObject = `JSON_OBJECT(
'issue_id', issue_id,
'depends_on_id', COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external),
'type', type,
'created_at', DATE_FORMAT(created_at, '%Y-%m-%dT%H:%i:%sZ'),
'created_by', created_by,
'metadata', CAST(metadata AS CHAR),
'thread_id', thread_id
)`
// scanReadyWorkRowWithCounts scans a single mega-query row, hydrating Labels
// and Dependencies on the embedded Issue from the JSON aggregates and
// populating the per-issue count fields.
func scanReadyWorkRowWithCounts(rows *sql.Rows) (*types.IssueWithCounts, error) {
// Build a scanner that drains IssueSelectColumns first via ScanIssueFrom,
// then consumes the appended aggregate columns.
var labelsJSON, depsJSON sql.NullString
var parentID sql.NullString
var depCount, rdepCount, commentCount sql.NullInt64
composite := &compositeReadyRow{
row: rows,
extra: []any{
&labelsJSON,
&depCount,
&rdepCount,
&commentCount,
&parentID,
&depsJSON,
},
}
issue, err := ScanIssueFrom(composite)
if err != nil {
return nil, fmt.Errorf("scan issue with counts: %w", err)
}
if labelsJSON.Valid && labelsJSON.String != "" {
var labels []string
if err := json.Unmarshal([]byte(labelsJSON.String), &labels); err != nil {
return nil, fmt.Errorf("scan issue with counts: parse labels_json: %w", err)
}
// The pre-aggregated JSON_ARRAYAGG does not preserve input order;
// alphabetize so the JSON output is stable and matches the legacy
// per-batch label SELECT which used ORDER BY issue_id, label.
sort.Strings(labels)
issue.Labels = labels
}
if depsJSON.Valid && depsJSON.String != "" {
var deps []*types.Dependency
if err := json.Unmarshal([]byte(depsJSON.String), &deps); err != nil {
return nil, fmt.Errorf("scan issue with counts: parse deps_json: %w", err)
}
issue.Dependencies = deps
}
iwc := &types.IssueWithCounts{
Issue: issue,
DependencyCount: int(depCount.Int64),
DependentCount: int(rdepCount.Int64),
CommentCount: int(commentCount.Int64),
}
if parentID.Valid {
s := parentID.String
iwc.Parent = &s
}
return iwc, nil
}
// compositeReadyRow adapts *sql.Rows + a tail of extra destination pointers
// to the IssueScanner interface expected by ScanIssueFrom. ScanIssueFrom
// calls Scan with the IssueSelectColumns destinations; we append the
// aggregate-column destinations transparently.
type compositeReadyRow struct {
row *sql.Rows
extra []any
}
func (c *compositeReadyRow) Scan(dest ...any) error {
combined := make([]any, 0, len(dest)+len(c.extra))
combined = append(combined, dest...)
combined = append(combined, c.extra...)
return c.row.Scan(combined...)
}

View file

@ -24,10 +24,6 @@ func deferredChildrenQueryRegex(depTable, issueTable string) string {
return `SELECT dep\.issue_id\s+FROM ` + depTable + ` dep\s+JOIN ` + issueTable + ` parent ON parent\.id = dep\.` + targetCol + `\s+WHERE dep\.type = 'parent-child'\s+AND parent\.defer_until IS NOT NULL\s+AND parent\.defer_until > UTC_TIMESTAMP\(\)`
}
func childrenOfIssuesQueryRegex(depTable string) string {
return `SELECT issue_id FROM ` + depTable + `\s+WHERE type = 'parent-child' AND COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) IN \(\?\)`
}
func beginMockTx(t *testing.T) (*sql.DB, sqlmock.Sqlmock, *sql.Tx) {
t.Helper()
@ -100,51 +96,6 @@ func TestBuildSQLInClause(t *testing.T) {
}
}
func TestGetReadyWorkInTx_UnboundedPropagatesBlockedComputationError(t *testing.T) {
t.Parallel()
blockedErr := errors.New("blocked graph unavailable")
_, err := GetReadyWorkInTx(
context.Background(),
nil,
types.WorkFilter{IncludeDeferred: true},
func(context.Context, *sql.Tx, bool) ([]string, error) {
return nil, blockedErr
},
)
if err == nil {
t.Fatal("expected blocked computation error")
}
if !errors.Is(err, blockedErr) {
t.Fatalf("expected wrapped blocked computation error, got %v", err)
}
if !strings.Contains(err.Error(), "compute blocked IDs") {
t.Fatalf("expected compute blocked IDs context, got %v", err)
}
}
func TestGetReadyWorkInTx_UnboundedComputesBlockersAcrossWispsByDefault(t *testing.T) {
t.Parallel()
stopErr := errors.New("stop after blocker computation")
var gotIncludeWisps bool
_, err := GetReadyWorkInTx(
context.Background(),
nil,
types.WorkFilter{IncludeDeferred: true},
func(_ context.Context, _ *sql.Tx, includeWisps bool) ([]string, error) {
gotIncludeWisps = includeWisps
return nil, stopErr
},
)
if !errors.Is(err, stopErr) {
t.Fatalf("error = %v, want stop error", err)
}
if !gotIncludeWisps {
t.Fatal("computeBlockedFn includeWisps = false, want true")
}
}
func TestGetReadyWorkInTx_PropagatesDeferredParentChildError(t *testing.T) {
t.Parallel()
@ -156,10 +107,6 @@ func TestGetReadyWorkInTx_PropagatesDeferredParentChildError(t *testing.T) {
context.Background(),
tx,
types.WorkFilter{},
func(context.Context, *sql.Tx, bool) ([]string, error) {
t.Fatal("blocked computation should not run after deferred parent child failure")
return nil, nil
},
)
if err == nil {
t.Fatal("expected deferred parent child error")
@ -175,82 +122,6 @@ func TestGetReadyWorkInTx_PropagatesDeferredParentChildError(t *testing.T) {
}
}
func TestIsBlockedInTxErrorsOnIssueWispCollision(t *testing.T) {
t.Parallel()
_, mock, tx := beginMockTx(t)
mock.ExpectQuery(`(?s)SELECT COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) AS depends_on_id, type FROM dependencies\s+WHERE issue_id = \? AND type IN`).
WithArgs("blocked-id").
WillReturnRows(sqlmock.NewRows([]string{"depends_on_id", "type"}).AddRow("dup-id", "blocks"))
mock.ExpectQuery(`(?s)SELECT COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) AS depends_on_id, type FROM wisp_dependencies\s+WHERE issue_id = \? AND type IN`).
WithArgs("blocked-id").
WillReturnRows(sqlmock.NewRows([]string{"depends_on_id", "type"}))
mock.ExpectQuery("SELECT id, status FROM issues").
WithArgs("dup-id").
WillReturnRows(sqlmock.NewRows([]string{"id", "status"}).AddRow("dup-id", types.StatusOpen))
mock.ExpectQuery("SELECT id, status FROM wisps").
WithArgs("dup-id").
WillReturnRows(sqlmock.NewRows([]string{"id", "status"}).AddRow("dup-id", types.StatusOpen))
_, _, err := IsBlockedInTx(context.Background(), tx, "blocked-id")
if err == nil {
t.Fatal("expected duplicate issue/wisp status error")
}
if !strings.Contains(err.Error(), `id "dup-id" exists in both issues and wisps`) {
t.Fatalf("error = %v, want duplicate issue/wisp context", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet SQL expectations: %v", err)
}
}
func TestGetNewlyUnblockedByCloseInTxChecksRemainingBlockersInBatches(t *testing.T) {
t.Parallel()
_, mock, tx := beginMockTx(t)
mock.ExpectQuery(`(?s)SELECT issue_id FROM dependencies\s+WHERE COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) = \? AND type = 'blocks'`).
WithArgs("closed-blocker").
WillReturnRows(sqlmock.NewRows([]string{"issue_id"}).
AddRow("candidate-a").
AddRow("candidate-b"))
mock.ExpectQuery(`(?s)SELECT issue_id FROM wisp_dependencies\s+WHERE COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) = \? AND type = 'blocks'`).
WithArgs("closed-blocker").
WillReturnRows(sqlmock.NewRows([]string{"issue_id"}))
mock.ExpectQuery(`SELECT id, status FROM issues WHERE id IN \(\?,\?\)`).
WithArgs("candidate-a", "candidate-b").
WillReturnRows(sqlmock.NewRows([]string{"id", "status"}).
AddRow("candidate-a", types.StatusOpen).
AddRow("candidate-b", types.StatusOpen))
mock.ExpectQuery(`SELECT id, status FROM wisps WHERE id IN \(\?,\?\)`).
WithArgs("candidate-a", "candidate-b").
WillReturnRows(sqlmock.NewRows([]string{"id", "status"}))
mock.ExpectQuery(`(?s)SELECT issue_id, COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) AS depends_on_id FROM dependencies\s+WHERE issue_id IN \(\?,\?\) AND type = 'blocks' AND COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) != \?`).
WithArgs("candidate-a", "candidate-b", "closed-blocker").
WillReturnRows(sqlmock.NewRows([]string{"issue_id", "depends_on_id"}).
AddRow("candidate-a", "other-blocker").
AddRow("candidate-b", "other-blocker"))
mock.ExpectQuery(`(?s)SELECT issue_id, COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) AS depends_on_id FROM wisp_dependencies\s+WHERE issue_id IN \(\?,\?\) AND type = 'blocks' AND COALESCE\(depends_on_issue_id, depends_on_wisp_id, depends_on_external\) != \?`).
WithArgs("candidate-a", "candidate-b", "closed-blocker").
WillReturnRows(sqlmock.NewRows([]string{"issue_id", "depends_on_id"}))
mock.ExpectQuery(`SELECT id, status FROM issues WHERE id IN \(\?\)`).
WithArgs("other-blocker").
WillReturnRows(sqlmock.NewRows([]string{"id", "status"}).AddRow("other-blocker", types.StatusOpen))
mock.ExpectQuery(`SELECT id, status FROM wisps WHERE id IN \(\?\)`).
WithArgs("other-blocker").
WillReturnRows(sqlmock.NewRows([]string{"id", "status"}))
got, err := GetNewlyUnblockedByCloseInTx(context.Background(), tx, "closed-blocker")
if err != nil {
t.Fatalf("GetNewlyUnblockedByCloseInTx: %v", err)
}
if len(got) != 0 {
t.Fatalf("unblocked = %v, want none because both candidates still have an open blocker", got)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet SQL expectations: %v", err)
}
}
func TestLoadStatusByIDInTxErrorsOnIssueWispCollision(t *testing.T) {
t.Parallel()
@ -369,27 +240,3 @@ func TestGetChildrenOfDeferredParentsInTx_IgnoresMissingWispDependenciesTable(t
t.Fatalf("unmet SQL expectations: %v", err)
}
}
func TestGetChildrenOfIssuesInTxPropagatesWispDependencyReadError(t *testing.T) {
t.Parallel()
_, mock, tx := beginMockTx(t)
readErr := errors.New("permission denied reading wisp_dependencies")
mock.ExpectQuery(childrenOfIssuesQueryRegex("dependencies")).
WithArgs("parent-id").
WillReturnRows(sqlmock.NewRows([]string{"issue_id"}))
mock.ExpectQuery(childrenOfIssuesQueryRegex("wisp_dependencies")).
WithArgs("parent-id").
WillReturnError(readErr)
_, err := getChildrenOfIssuesInTx(context.Background(), tx, []string{"parent-id"})
if err == nil {
t.Fatal("expected wisp_dependencies read error")
}
if !errors.Is(err, readErr) {
t.Fatalf("error = %v, want wrapped read error", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet SQL expectations: %v", err)
}
}

View file

@ -38,6 +38,15 @@ func SearchIssuesInTx(ctx context.Context, tx *sql.Tx, query string, filter type
// querying wisps here with Ephemeral=&false returns only NoHistory beads
// while correctly excluding true ephemeral wisps. (GH#3659)
if filter.Ephemeral == nil || !*filter.Ephemeral {
// Short-circuit on projects with no wisps: skip the SELECT against
// wisps entirely. One cheap probe replaces one full filtered scan.
empty, probeErr := wispsTableEmptyOrMissingInTx(ctx, tx)
if probeErr != nil {
return nil, fmt.Errorf("search wisps (merge): probe: %w", probeErr)
}
if empty {
return results, nil
}
wispResults, wispErr := searchTableInTx(ctx, tx, query, filter, WispsFilterTables)
if wispErr != nil && !isTableNotExistError(wispErr) {
return nil, fmt.Errorf("search wisps (merge): %w", wispErr)

View file

@ -0,0 +1,230 @@
package issueops
import (
"context"
"database/sql"
"fmt"
"github.com/steveyegge/beads/internal/types"
)
// SearchIssuesWithCountsInTx is the single-statement equivalent of
// SearchIssuesInTx + per-issue hydration of labels / dependency records /
// dep/dependent/comment counts / parent ID, intended for the bd list --json
// path.
//
// Issues and wisps are each fetched via one mega-query against their own
// table set; results are merged Go-side. Cross-table reverse-dependency
// edges are reflected in rdep_count via a UNION ALL of both dep tables.
//
// When the wisps table is missing or empty (the common case on projects
// that never use wisps) one cheap probe replaces the entire wisp branch.
//
// No per-call hydration fallbacks: both sides use the same mega-query
// shape.
func SearchIssuesWithCountsInTx(ctx context.Context, tx *sql.Tx, query string, filter types.IssueFilter) ([]*types.IssueWithCounts, error) {
// Ephemeral-only: only the wisps table should be scanned.
if filter.Ephemeral != nil && *filter.Ephemeral {
empty, probeErr := wispsTableEmptyOrMissingInTx(ctx, tx)
if probeErr != nil {
return nil, fmt.Errorf("search issues with counts: ephemeral wisp probe: %w", probeErr)
}
if empty {
return nil, nil
}
return runFilterMegaQueryInTx(ctx, tx, query, filter, WispsFilterTables)
}
out, err := runFilterMegaQueryInTx(ctx, tx, query, filter, IssuesFilterTables)
if err != nil {
return nil, err
}
// Merge in wisps when not restricted to permanents-only-via-ephemeral=true.
// Empty wisps table short-circuits the entire wisp branch (one probe).
empty, probeErr := wispsTableEmptyOrMissingInTx(ctx, tx)
if probeErr != nil {
return nil, fmt.Errorf("search issues with counts: wisp probe: %w", probeErr)
}
if empty {
return out, nil
}
wisps, err := runFilterMegaQueryInTx(ctx, tx, query, filter, WispsFilterTables)
if err != nil {
if isTableNotExistError(err) {
return out, nil
}
return nil, err
}
if len(wisps) == 0 {
return out, nil
}
seen := make(map[string]struct{}, len(out))
for _, iwc := range out {
if iwc != nil && iwc.Issue != nil {
seen[iwc.Issue.ID] = struct{}{}
}
}
for _, w := range wisps {
if w == nil || w.Issue == nil {
continue
}
if _, dup := seen[w.Issue.ID]; dup {
return nil, fmt.Errorf("search issues with counts: id %q exists in both issues and wisps", w.Issue.ID)
}
out = append(out, w)
}
return out, nil
}
// runFilterMegaQueryInTx is the IssueFilter-driven entry point: it routes
// BuildIssueFilterClauses through the shared LEFT-JOIN mega-query against
// the specified table set.
func runFilterMegaQueryInTx(ctx context.Context, tx *sql.Tx, query string, filter types.IssueFilter, tables FilterTables) ([]*types.IssueWithCounts, error) {
whereClauses, args, err := BuildIssueFilterClauses(query, filter, tables)
if err != nil {
return nil, err
}
whereSQL := ""
if len(whereClauses) > 0 {
whereSQL = "WHERE " + joinAnd(whereClauses)
}
limitSQL := ""
if filter.Limit > 0 {
limitSQL = fmt.Sprintf("LIMIT %d", filter.Limit)
}
const orderBy = "ORDER BY i.priority ASC, i.created_at DESC, i.id ASC"
return runMegaQueryInTx(ctx, tx, tables, whereSQL, orderBy, limitSQL, args)
}
// runMegaQueryInTx is the shared single-statement entry point used by both
// the ready-work and the bd list --json paths. It builds the six LEFT-JOIN
// shape against the supplied table set, executes, and scans rows into
// []*types.IssueWithCounts.
//
// orderBySQL must already reference the "i." alias the mega-query uses for
// the outer table (see readyWorkIssueColumns). whereSQL must include the
// leading "WHERE " keyword when non-empty.
//
// + ? placeholders + hardcoded table names from FilterTables; the integer
// LIMIT is the only non-? interpolation.
//
//nolint:gosec // G201: SQL fragments are caller-built from hardcoded shapes
func runMegaQueryInTx(ctx context.Context, tx *sql.Tx, tables FilterTables, whereSQL, orderBySQL, limitSQL string, args []interface{}) ([]*types.IssueWithCounts, error) {
megaSQL := fmt.Sprintf(`
SELECT %s,
l.labels_json AS labels_json,
COALESCE(dc.cnt, 0) AS dep_count,
COALESCE(rc.cnt, 0) AS rdep_count,
COALESCE(cc.cnt, 0) AS comment_count,
pc.parent_id AS parent_id,
d.deps_json AS deps_json
FROM %s i
LEFT JOIN (
SELECT issue_id, JSON_ARRAYAGG(label) AS labels_json
FROM %s
GROUP BY issue_id
) l ON l.issue_id = i.id
LEFT JOIN (
SELECT issue_id, COUNT(*) AS cnt
FROM %s
WHERE type = 'blocks'
GROUP BY issue_id
) dc ON dc.issue_id = i.id
LEFT JOIN (
SELECT dep_id, COUNT(*) AS cnt FROM (
SELECT COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) AS dep_id
FROM dependencies WHERE type = 'blocks'
UNION ALL
SELECT COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external) AS dep_id
FROM wisp_dependencies WHERE type = 'blocks'
) all_blockers GROUP BY dep_id
) rc ON rc.dep_id = i.id
LEFT JOIN (
SELECT issue_id, COUNT(*) AS cnt
FROM %s
GROUP BY issue_id
) cc ON cc.issue_id = i.id
LEFT JOIN (
SELECT issue_id,
MIN(COALESCE(depends_on_issue_id, depends_on_wisp_id, depends_on_external)) AS parent_id
FROM %s
WHERE type = 'parent-child'
GROUP BY issue_id
) pc ON pc.issue_id = i.id
LEFT JOIN (
SELECT issue_id, JSON_ARRAYAGG(%s) AS deps_json
FROM %s
GROUP BY issue_id
) d ON d.issue_id = i.id
%s
%s
%s
`,
readyWorkIssueColumns,
tables.Main,
tables.Labels,
tables.Dependencies,
tables.Comments,
tables.Dependencies,
readyWorkDepJSONObject,
tables.Dependencies,
whereSQL,
orderBySQL,
limitSQL,
)
rows, err := tx.QueryContext(ctx, megaSQL, args...)
if err != nil {
return nil, fmt.Errorf("mega-query %s: %w", tables.Main, err)
}
defer func() { _ = rows.Close() }()
var out []*types.IssueWithCounts
seen := make(map[string]bool)
for rows.Next() {
iwc, scanErr := scanReadyWorkRowWithCounts(rows)
if scanErr != nil {
return nil, scanErr
}
if iwc == nil || iwc.Issue == nil {
continue
}
// Defensive dedup in case any subquery shape introduces row duplication.
if seen[iwc.Issue.ID] {
continue
}
seen[iwc.Issue.ID] = true
out = append(out, iwc)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("mega-query %s: rows: %w", tables.Main, err)
}
return out, nil
}
// joinAnd mirrors strings.Join(..., " AND ") without pulling in strings
// just for this single site.
func joinAnd(clauses []string) string {
switch len(clauses) {
case 0:
return ""
case 1:
return clauses[0]
}
total := 0
for _, c := range clauses {
total += len(c)
}
total += 5 * (len(clauses) - 1)
buf := make([]byte, 0, total)
for i, c := range clauses {
if i > 0 {
buf = append(buf, " AND "...)
}
buf = append(buf, c...)
}
return string(buf)
}

View file

@ -234,6 +234,38 @@ func updateIssueInTx(ctx context.Context, tx *sql.Tx, id string, updates map[str
}
}
// Refresh is_blocked if status flipped between active and inactive.
// "Active" for blocking purposes is status NOT IN ('closed','pinned');
// other status transitions (e.g., open -> in_progress) do not change
// blocker activity, and pure pinned-flag flips don't either, so we only
// fan out work on the active/inactive boundary.
if rawStatus, hasStatus := updates["status"]; hasStatus {
var newStatus string
switch v := rawStatus.(type) {
case string:
newStatus = v
case types.Status:
newStatus = string(v)
}
oldActive := oldIssue.Status != types.StatusClosed && oldIssue.Status != types.StatusPinned
newActive := newStatus != string(types.StatusClosed) && newStatus != string(types.StatusPinned)
if oldActive != newActive {
var affectedIssues, affectedWisps []string
var aerr error
if isWisp {
affectedIssues, affectedWisps, aerr = AffectedByStatusChangeForWispInTx(ctx, tx, id)
} else {
affectedIssues, affectedWisps, aerr = AffectedByStatusChangeInTx(ctx, tx, id)
}
if aerr != nil {
return nil, fmt.Errorf("affected by status change for %s: %w", id, aerr)
}
if err := RecomputeIsBlockedInTx(ctx, tx, affectedIssues, affectedWisps); err != nil {
return nil, fmt.Errorf("recompute is_blocked after status change for %s: %w", id, err)
}
}
}
return &UpdateResult{OldIssue: oldIssue, IsWisp: isWisp}, nil
}

View file

@ -3,6 +3,7 @@ package issueops
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
)
@ -21,6 +22,25 @@ func IsActiveWispInTx(ctx context.Context, tx *sql.Tx, id string) bool {
return err == nil
}
// wispsTableEmptyOrMissingInTx returns true when the wisps table either does
// not exist or contains zero rows. Used by the wisp-partition helpers below
// to short-circuit the per-batch IN-list probes on projects that never use
// wisps — one row probe replaces N batched lookups.
func wispsTableEmptyOrMissingInTx(ctx context.Context, tx *sql.Tx) (bool, error) {
var probe int
err := tx.QueryRowContext(ctx, "SELECT 1 FROM wisps LIMIT 1").Scan(&probe)
switch {
case err == nil:
return false, nil
case errors.Is(err, sql.ErrNoRows):
return true, nil
case isTableNotExistError(err):
return true, nil
default:
return false, err
}
}
// WispIDSetInTx returns the subset of ids that are currently-active wisps
// within the tx. The set is consistent for the tx's lifetime (Dolt MVCC).
// Intended for hot-path partitioning where a batch of IDs must be split
@ -37,6 +57,11 @@ func WispIDSetInTx(ctx context.Context, tx *sql.Tx, ids []string) (map[string]st
if len(ids) == 0 {
return set, nil
}
if empty, err := wispsTableEmptyOrMissingInTx(ctx, tx); err != nil {
return nil, fmt.Errorf("wisp id set: probe: %w", err)
} else if empty {
return set, nil
}
for start := 0; start < len(ids); start += queryBatchSize {
end := start + queryBatchSize
if end > len(ids) {
@ -96,6 +121,12 @@ func PartitionWispIDsInTx(ctx context.Context, tx *sql.Tx, ids []string) (wispID
return nil, nil, nil
}
if empty, probeErr := wispsTableEmptyOrMissingInTx(ctx, tx); probeErr != nil {
return nil, nil, fmt.Errorf("partition wisp ids: probe: %w", probeErr)
} else if empty {
return nil, append([]string(nil), ids...), nil
}
wispSet := make(map[string]struct{}, len(ids))
for start := 0; start < len(ids); start += queryBatchSize {
end := start + queryBatchSize

View file

@ -0,0 +1,2 @@
DROP INDEX idx_issues_is_blocked ON issues;
ALTER TABLE issues DROP COLUMN is_blocked;

View file

@ -0,0 +1,49 @@
SET @needs_add = (
SELECT IF(COUNT(*) = 0, 1, 0)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'issues'
AND COLUMN_NAME = 'is_blocked'
);
SET @sql = IF(@needs_add = 1,
'ALTER TABLE issues ADD COLUMN is_blocked TINYINT(1) NOT NULL DEFAULT 0',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @needs_index = (
SELECT IF(COUNT(*) = 0, 1, 0)
FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'issues'
AND INDEX_NAME = 'idx_issues_is_blocked'
);
SET @sql = IF(@needs_index = 1,
'CREATE INDEX idx_issues_is_blocked ON issues(is_blocked, status)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
UPDATE issues SET is_blocked = 0;
WITH RECURSIVE
directly_blocked(id) AS (
SELECT DISTINCT i.id
FROM issues i
JOIN dependencies d ON d.issue_id = i.id
JOIN issues t ON t.id = d.depends_on_issue_id
WHERE i.status NOT IN ('closed', 'pinned')
AND d.type IN ('blocks', 'conditional-blocks', 'waits-for')
AND t.status NOT IN ('closed', 'pinned')
),
reachable(id) AS (
SELECT id FROM directly_blocked
UNION
SELECT d.issue_id
FROM reachable r
JOIN dependencies d
ON d.depends_on_issue_id = r.id
AND d.type = 'parent-child'
)
UPDATE issues
SET is_blocked = 1
WHERE id IN (SELECT id FROM reachable)
AND status NOT IN ('closed', 'pinned');

View file

@ -0,0 +1,29 @@
SET @needs_add = IF(
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wisps') > 0
AND
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'wisps'
AND COLUMN_NAME = 'is_blocked') = 0,
1, 0
);
SET @sql = IF(@needs_add = 1,
'ALTER TABLE wisps ADD COLUMN is_blocked TINYINT(1) NOT NULL DEFAULT 0',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET @needs_index = IF(
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'wisps') > 0
AND
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'wisps'
AND INDEX_NAME = 'idx_wisps_is_blocked') = 0,
1, 0
);
SET @sql = IF(@needs_index = 1,
'CREATE INDEX idx_wisps_is_blocked ON wisps(is_blocked, status)',
'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

View file

@ -49,6 +49,11 @@ type Storage interface {
CloseIssue(ctx context.Context, id string, reason string, actor string, session string) error
DeleteIssue(ctx context.Context, id string) error
SearchIssues(ctx context.Context, query string, filter types.IssueFilter) ([]*types.Issue, error)
// SearchIssuesWithCounts returns matching issues hydrated with labels,
// dependency records, dep/dependent/comment counts and parent ID in one
// SQL statement. JSON-path callers (bd list --json) should use this in
// place of SearchIssues + 3 follow-up hydration calls.
SearchIssuesWithCounts(ctx context.Context, query string, filter types.IssueFilter) ([]*types.IssueWithCounts, error)
// Dependencies
AddDependency(ctx context.Context, dep *types.Dependency, actor string) error
@ -67,6 +72,11 @@ type Storage interface {
// Work queries
GetReadyWork(ctx context.Context, filter types.WorkFilter) ([]*types.Issue, error)
// GetReadyWorkWithCounts returns ready issues with labels, dependency
// records, dep/dependent/comment counts and parent ID hydrated in a
// single SQL statement. Use for bd ready --json. Wisps (if any) are
// merged in via the slower per-call hydration path.
GetReadyWorkWithCounts(ctx context.Context, filter types.WorkFilter) ([]*types.IssueWithCounts, error)
GetBlockedIssues(ctx context.Context, filter types.WorkFilter) ([]*types.BlockedIssue, error)
GetEpicsEligibleForClosure(ctx context.Context) ([]*types.EpicStatus, error)

View file

@ -193,6 +193,17 @@ func (s *InstrumentedStorage) SearchIssues(ctx context.Context, query string, fi
return issues, err
}
func (s *InstrumentedStorage) SearchIssuesWithCounts(ctx context.Context, query string, filter types.IssueFilter) ([]*types.IssueWithCounts, error) {
attrs := []attribute.KeyValue{attribute.String("bd.query", query)}
ctx, span, t := s.op(ctx, "SearchIssuesWithCounts", attrs...)
v, err := s.inner.SearchIssuesWithCounts(ctx, query, filter)
if err == nil {
span.SetAttributes(attribute.Int("bd.result.count", len(v)))
}
s.done(ctx, span, t, err, attrs...)
return v, err
}
// ── Dependencies ────────────────────────────────────────────────────────────
func (s *InstrumentedStorage) AddDependency(ctx context.Context, dep *types.Dependency, actor string) error {
@ -313,6 +324,16 @@ func (s *InstrumentedStorage) GetReadyWork(ctx context.Context, filter types.Wor
return v, err
}
func (s *InstrumentedStorage) GetReadyWorkWithCounts(ctx context.Context, filter types.WorkFilter) ([]*types.IssueWithCounts, error) {
ctx, span, t := s.op(ctx, "GetReadyWorkWithCounts")
v, err := s.inner.GetReadyWorkWithCounts(ctx, filter)
if err == nil {
span.SetAttributes(attribute.Int("bd.result.count", len(v)))
}
s.done(ctx, span, t, err)
return v, err
}
func (s *InstrumentedStorage) GetBlockedIssues(ctx context.Context, filter types.WorkFilter) ([]*types.BlockedIssue, error) {
ctx, span, t := s.op(ctx, "GetBlockedIssues")
v, err := s.inner.GetBlockedIssues(ctx, filter)