Merge pull request #549 from steveyegge/fix/nodb-mode-547
fix(memory): implement GetReadyWork/GetBlockedIssues + child counters
This commit is contained in:
1630
.beads/issues.jsonl
1630
.beads/issues.jsonl
File diff suppressed because one or more lines are too long
@@ -19,6 +19,7 @@ import (
|
||||
"github.com/steveyegge/beads/internal/config"
|
||||
"github.com/steveyegge/beads/internal/debug"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
"github.com/steveyegge/beads/internal/utils"
|
||||
)
|
||||
|
||||
// outputJSON outputs data as pretty-printed JSON
|
||||
@@ -42,12 +43,26 @@ func outputJSON(v interface{}) {
|
||||
//
|
||||
// Thread-safe: No shared state access.
|
||||
func findJSONLPath() string {
|
||||
// Allow explicit override (useful in no-db mode or non-standard layouts)
|
||||
if jsonlEnv := os.Getenv("BEADS_JSONL"); jsonlEnv != "" {
|
||||
return utils.CanonicalizePath(jsonlEnv)
|
||||
}
|
||||
|
||||
// Use public API for path discovery
|
||||
jsonlPath := beads.FindJSONLPath(dbPath)
|
||||
|
||||
// In --no-db mode, dbPath may be empty. Fall back to locating the .beads directory.
|
||||
if jsonlPath == "" {
|
||||
beadsDir := beads.FindBeadsDir()
|
||||
if beadsDir == "" {
|
||||
return ""
|
||||
}
|
||||
jsonlPath = utils.FindJSONLInDir(beadsDir)
|
||||
}
|
||||
|
||||
// Ensure the directory exists (important for new databases)
|
||||
// This is the only difference from the public API - we create the directory
|
||||
dbDir := filepath.Dir(dbPath)
|
||||
dbDir := filepath.Dir(jsonlPath)
|
||||
if err := os.MkdirAll(dbDir, 0750); err != nil {
|
||||
// If we can't create the directory, return discovered path anyway
|
||||
// (the subsequent write will fail with a clearer error)
|
||||
|
||||
@@ -98,6 +98,14 @@ func (m *MemoryStorage) LoadFromIssues(issues []*types.Issue) error {
|
||||
m.counters[prefix] = num
|
||||
}
|
||||
}
|
||||
|
||||
// Update hierarchical child counters based on issue ID
|
||||
// e.g. "bd-a3f8e9.2" -> parent "bd-a3f8e9" counter 2
|
||||
if parentID, childNum, ok := extractParentAndChildNumber(issue.ID); ok {
|
||||
if m.counters[parentID] < childNum {
|
||||
m.counters[parentID] = childNum
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -157,6 +165,25 @@ func extractPrefixAndNumber(id string) (string, int) {
|
||||
return prefix, num
|
||||
}
|
||||
|
||||
// extractParentAndChildNumber extracts the parent ID and numeric child counter from an issue ID like
|
||||
// "bd-a3f8e9.2" -> ("bd-a3f8e9", 2, true).
|
||||
func extractParentAndChildNumber(id string) (string, int, bool) {
|
||||
lastDot := strings.LastIndex(id, ".")
|
||||
if lastDot == -1 {
|
||||
return "", 0, false
|
||||
}
|
||||
|
||||
parentID := id[:lastDot]
|
||||
suffix := id[lastDot+1:]
|
||||
|
||||
var num int
|
||||
if _, err := fmt.Sscanf(suffix, "%d", &num); err != nil {
|
||||
return "", 0, false
|
||||
}
|
||||
|
||||
return parentID, num, true
|
||||
}
|
||||
|
||||
// CreateIssue creates a new issue
|
||||
func (m *MemoryStorage) CreateIssue(ctx context.Context, issue *types.Issue, actor string) error {
|
||||
m.mu.Lock()
|
||||
@@ -867,16 +894,219 @@ func (m *MemoryStorage) GetIssuesByLabel(ctx context.Context, label string) ([]*
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// Stub implementations for other required methods
|
||||
// GetReadyWork returns issues that are ready to work on (no open blockers)
|
||||
func (m *MemoryStorage) GetReadyWork(ctx context.Context, filter types.WorkFilter) ([]*types.Issue, error) {
|
||||
// Simplified: return open issues with no blocking dependencies
|
||||
return m.SearchIssues(ctx, "", types.IssueFilter{
|
||||
Status: func() *types.Status { s := types.StatusOpen; return &s }(),
|
||||
})
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
var results []*types.Issue
|
||||
|
||||
for _, issue := range m.issues {
|
||||
// Status filtering: default to open OR in_progress if not specified
|
||||
if filter.Status == "" {
|
||||
if issue.Status != types.StatusOpen && issue.Status != types.StatusInProgress {
|
||||
continue
|
||||
}
|
||||
} else if issue.Status != filter.Status {
|
||||
continue
|
||||
}
|
||||
|
||||
// Priority filtering
|
||||
if filter.Priority != nil && issue.Priority != *filter.Priority {
|
||||
continue
|
||||
}
|
||||
|
||||
// Unassigned takes precedence over Assignee filter
|
||||
if filter.Unassigned {
|
||||
if issue.Assignee != "" {
|
||||
continue
|
||||
}
|
||||
} else if filter.Assignee != nil {
|
||||
if issue.Assignee != *filter.Assignee {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Label filtering (AND semantics)
|
||||
if len(filter.Labels) > 0 {
|
||||
issueLabels := m.labels[issue.ID]
|
||||
hasAllLabels := true
|
||||
for _, reqLabel := range filter.Labels {
|
||||
found := false
|
||||
for _, label := range issueLabels {
|
||||
if label == reqLabel {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
hasAllLabels = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasAllLabels {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Label filtering (OR semantics)
|
||||
if len(filter.LabelsAny) > 0 {
|
||||
issueLabels := m.labels[issue.ID]
|
||||
hasAnyLabel := false
|
||||
for _, reqLabel := range filter.LabelsAny {
|
||||
for _, label := range issueLabels {
|
||||
if label == reqLabel {
|
||||
hasAnyLabel = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if hasAnyLabel {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasAnyLabel {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// Skip issues with any open 'blocks' dependencies
|
||||
if len(m.getOpenBlockers(issue.ID)) > 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
issueCopy := *issue
|
||||
if deps, ok := m.dependencies[issue.ID]; ok {
|
||||
issueCopy.Dependencies = deps
|
||||
}
|
||||
if labels, ok := m.labels[issue.ID]; ok {
|
||||
issueCopy.Labels = labels
|
||||
}
|
||||
if comments, ok := m.comments[issue.ID]; ok {
|
||||
issueCopy.Comments = comments
|
||||
}
|
||||
|
||||
results = append(results, &issueCopy)
|
||||
}
|
||||
|
||||
// Default to hybrid sort for backwards compatibility
|
||||
sortPolicy := filter.SortPolicy
|
||||
if sortPolicy == "" {
|
||||
sortPolicy = types.SortPolicyHybrid
|
||||
}
|
||||
|
||||
switch sortPolicy {
|
||||
case types.SortPolicyOldest:
|
||||
sort.Slice(results, func(i, j int) bool {
|
||||
return results[i].CreatedAt.Before(results[j].CreatedAt)
|
||||
})
|
||||
case types.SortPolicyPriority:
|
||||
sort.Slice(results, func(i, j int) bool {
|
||||
if results[i].Priority != results[j].Priority {
|
||||
return results[i].Priority < results[j].Priority
|
||||
}
|
||||
return results[i].CreatedAt.Before(results[j].CreatedAt)
|
||||
})
|
||||
case types.SortPolicyHybrid:
|
||||
fallthrough
|
||||
default:
|
||||
cutoff := time.Now().Add(-48 * time.Hour)
|
||||
sort.Slice(results, func(i, j int) bool {
|
||||
iRecent := results[i].CreatedAt.After(cutoff)
|
||||
jRecent := results[j].CreatedAt.After(cutoff)
|
||||
if iRecent != jRecent {
|
||||
return iRecent // recent first
|
||||
}
|
||||
if iRecent {
|
||||
if results[i].Priority != results[j].Priority {
|
||||
return results[i].Priority < results[j].Priority
|
||||
}
|
||||
}
|
||||
return results[i].CreatedAt.Before(results[j].CreatedAt)
|
||||
})
|
||||
}
|
||||
|
||||
// Apply limit
|
||||
if filter.Limit > 0 && len(results) > filter.Limit {
|
||||
results = results[:filter.Limit]
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// getOpenBlockers returns the IDs of blockers that are currently open/in_progress/blocked.
|
||||
// The caller must hold at least a read lock.
|
||||
func (m *MemoryStorage) getOpenBlockers(issueID string) []string {
|
||||
deps := m.dependencies[issueID]
|
||||
if len(deps) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
blockers := make([]string, 0)
|
||||
for _, dep := range deps {
|
||||
if dep.Type != types.DepBlocks {
|
||||
continue
|
||||
}
|
||||
blocker, ok := m.issues[dep.DependsOnID]
|
||||
if !ok {
|
||||
// If the blocker is missing, treat it as still blocking (data is incomplete)
|
||||
blockers = append(blockers, dep.DependsOnID)
|
||||
continue
|
||||
}
|
||||
switch blocker.Status {
|
||||
case types.StatusOpen, types.StatusInProgress, types.StatusBlocked:
|
||||
blockers = append(blockers, blocker.ID)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Strings(blockers)
|
||||
return blockers
|
||||
}
|
||||
|
||||
// GetBlockedIssues returns issues that are blocked by other issues
|
||||
func (m *MemoryStorage) GetBlockedIssues(ctx context.Context) ([]*types.BlockedIssue, error) {
|
||||
return nil, nil
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
var results []*types.BlockedIssue
|
||||
|
||||
for _, issue := range m.issues {
|
||||
// Only consider non-closed, non-tombstone issues
|
||||
if issue.Status == types.StatusClosed || issue.Status == types.StatusTombstone {
|
||||
continue
|
||||
}
|
||||
|
||||
blockers := m.getOpenBlockers(issue.ID)
|
||||
if issue.Status != types.StatusBlocked && len(blockers) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
issueCopy := *issue
|
||||
if deps, ok := m.dependencies[issue.ID]; ok {
|
||||
issueCopy.Dependencies = deps
|
||||
}
|
||||
if labels, ok := m.labels[issue.ID]; ok {
|
||||
issueCopy.Labels = labels
|
||||
}
|
||||
if comments, ok := m.comments[issue.ID]; ok {
|
||||
issueCopy.Comments = comments
|
||||
}
|
||||
|
||||
results = append(results, &types.BlockedIssue{
|
||||
Issue: issueCopy,
|
||||
BlockedByCount: len(blockers),
|
||||
BlockedBy: blockers,
|
||||
})
|
||||
}
|
||||
|
||||
// Match SQLite behavior: order by priority ascending
|
||||
sort.Slice(results, func(i, j int) bool {
|
||||
if results[i].Priority != results[j].Priority {
|
||||
return results[i].Priority < results[j].Priority
|
||||
}
|
||||
return results[i].CreatedAt.Before(results[j].CreatedAt)
|
||||
})
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func (m *MemoryStorage) GetEpicsEligibleForClosure(ctx context.Context) ([]*types.EpicStatus, error) {
|
||||
|
||||
154
internal/storage/memory/ready_blocked_nodb_test.go
Normal file
154
internal/storage/memory/ready_blocked_nodb_test.go
Normal file
@@ -0,0 +1,154 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
func TestLoadFromIssues_InitializesChildCounters(t *testing.T) {
|
||||
store := New("")
|
||||
ctx := context.Background()
|
||||
|
||||
issues := []*types.Issue{
|
||||
{ID: "bd-parent", Title: "Parent", Status: types.StatusOpen, Priority: 1, IssueType: types.TypeEpic},
|
||||
{ID: "bd-parent.1", Title: "Child 1", Status: types.StatusOpen, Priority: 1, IssueType: types.TypeTask},
|
||||
{ID: "bd-parent.3", Title: "Child 3", Status: types.StatusOpen, Priority: 1, IssueType: types.TypeTask},
|
||||
{ID: "bd-parent.1.2", Title: "Nested Child 2", Status: types.StatusOpen, Priority: 1, IssueType: types.TypeTask},
|
||||
}
|
||||
|
||||
if err := store.LoadFromIssues(issues); err != nil {
|
||||
t.Fatalf("LoadFromIssues failed: %v", err)
|
||||
}
|
||||
|
||||
next, err := store.GetNextChildID(ctx, "bd-parent")
|
||||
if err != nil {
|
||||
t.Fatalf("GetNextChildID failed: %v", err)
|
||||
}
|
||||
if next != "bd-parent.4" {
|
||||
t.Fatalf("GetNextChildID = %q, want %q", next, "bd-parent.4")
|
||||
}
|
||||
|
||||
nextNested, err := store.GetNextChildID(ctx, "bd-parent.1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetNextChildID (nested) failed: %v", err)
|
||||
}
|
||||
if nextNested != "bd-parent.1.3" {
|
||||
t.Fatalf("GetNextChildID (nested) = %q, want %q", nextNested, "bd-parent.1.3")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetReadyWork_ExcludesIssuesWithOpenBlocksDependencies(t *testing.T) {
|
||||
store := setupTestMemory(t)
|
||||
defer store.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
closedAt := time.Now()
|
||||
blocker := &types.Issue{ID: "bd-1", Title: "Blocker", Status: types.StatusOpen, Priority: 1, IssueType: types.TypeTask}
|
||||
blocked := &types.Issue{ID: "bd-2", Title: "Blocked", Status: types.StatusOpen, Priority: 1, IssueType: types.TypeTask}
|
||||
closedBlocker := &types.Issue{ID: "bd-3", Title: "Closed blocker", Status: types.StatusClosed, Priority: 1, IssueType: types.TypeTask, ClosedAt: &closedAt}
|
||||
unblocked := &types.Issue{ID: "bd-4", Title: "Unblocked", Status: types.StatusOpen, Priority: 1, IssueType: types.TypeTask}
|
||||
|
||||
for _, issue := range []*types.Issue{blocker, blocked, closedBlocker, unblocked} {
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// bd-2 is blocked by an open issue
|
||||
if err := store.AddDependency(ctx, &types.Dependency{
|
||||
IssueID: blocked.ID,
|
||||
DependsOnID: blocker.ID,
|
||||
Type: types.DepBlocks,
|
||||
CreatedAt: time.Now(),
|
||||
CreatedBy: "test",
|
||||
}, "test"); err != nil {
|
||||
t.Fatalf("AddDependency failed: %v", err)
|
||||
}
|
||||
|
||||
// bd-4 is "blocked" by a closed issue, which should not block ready work
|
||||
if err := store.AddDependency(ctx, &types.Dependency{
|
||||
IssueID: unblocked.ID,
|
||||
DependsOnID: closedBlocker.ID,
|
||||
Type: types.DepBlocks,
|
||||
CreatedAt: time.Now(),
|
||||
CreatedBy: "test",
|
||||
}, "test"); err != nil {
|
||||
t.Fatalf("AddDependency failed: %v", err)
|
||||
}
|
||||
|
||||
ready, err := store.GetReadyWork(ctx, types.WorkFilter{})
|
||||
if err != nil {
|
||||
t.Fatalf("GetReadyWork failed: %v", err)
|
||||
}
|
||||
|
||||
got := map[string]bool{}
|
||||
for _, issue := range ready {
|
||||
got[issue.ID] = true
|
||||
}
|
||||
|
||||
if got[blocked.ID] {
|
||||
t.Fatalf("GetReadyWork should not include blocked issue %s", blocked.ID)
|
||||
}
|
||||
if !got[unblocked.ID] {
|
||||
t.Fatalf("GetReadyWork should include unblocked issue %s", unblocked.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetBlockedIssues_IncludesExplicitlyBlockedStatus(t *testing.T) {
|
||||
store := setupTestMemory(t)
|
||||
defer store.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
explicit := &types.Issue{ID: "bd-1", Title: "Explicitly blocked", Status: types.StatusBlocked, Priority: 1, IssueType: types.TypeTask}
|
||||
blocker := &types.Issue{ID: "bd-2", Title: "Blocker", Status: types.StatusOpen, Priority: 1, IssueType: types.TypeTask}
|
||||
implicitlyBlocked := &types.Issue{ID: "bd-3", Title: "Implicitly blocked", Status: types.StatusOpen, Priority: 1, IssueType: types.TypeTask}
|
||||
|
||||
for _, issue := range []*types.Issue{explicit, blocker, implicitlyBlocked} {
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("CreateIssue failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := store.AddDependency(ctx, &types.Dependency{
|
||||
IssueID: implicitlyBlocked.ID,
|
||||
DependsOnID: blocker.ID,
|
||||
Type: types.DepBlocks,
|
||||
CreatedAt: time.Now(),
|
||||
CreatedBy: "test",
|
||||
}, "test"); err != nil {
|
||||
t.Fatalf("AddDependency failed: %v", err)
|
||||
}
|
||||
|
||||
blocked, err := store.GetBlockedIssues(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("GetBlockedIssues failed: %v", err)
|
||||
}
|
||||
|
||||
var foundExplicit, foundImplicit bool
|
||||
for _, bi := range blocked {
|
||||
switch bi.ID {
|
||||
case explicit.ID:
|
||||
foundExplicit = true
|
||||
if bi.BlockedByCount != 0 {
|
||||
t.Fatalf("explicit blocked issue should have BlockedByCount=0, got %d", bi.BlockedByCount)
|
||||
}
|
||||
case implicitlyBlocked.ID:
|
||||
foundImplicit = true
|
||||
if bi.BlockedByCount != 1 || len(bi.BlockedBy) != 1 || bi.BlockedBy[0] != blocker.ID {
|
||||
t.Fatalf("implicit blocked issue blockers mismatch: count=%d blockers=%v", bi.BlockedByCount, bi.BlockedBy)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !foundExplicit {
|
||||
t.Fatalf("expected explicit blocked issue %s", explicit.ID)
|
||||
}
|
||||
if !foundImplicit {
|
||||
t.Fatalf("expected implicitly blocked issue %s", implicitlyBlocked.ID)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user