Add configurable sort policy for GetReadyWork (bd-147)
- Add SortPolicy type with hybrid, priority, oldest constants - Add SortPolicy field to WorkFilter - Implement buildOrderByClause() for SQL generation - Add --sort flag to bd ready command - Add comprehensive tests for all 3 sort policies - Update RPC protocol to support sort policy - Update documentation with sort policy examples Enables autonomous systems like VC to use strict priority ordering while preserving hybrid behavior for interactive use. Amp-Thread-ID: https://ampcode.com/threads/T-9d7ea9db-8d6d-4498-9daa-48a7e104ce1f Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
@@ -44,6 +44,13 @@ func (s *SQLiteStorage) GetReadyWork(ctx context.Context, filter types.WorkFilte
|
||||
args = append(args, filter.Limit)
|
||||
}
|
||||
|
||||
// Default to hybrid sort for backwards compatibility
|
||||
sortPolicy := filter.SortPolicy
|
||||
if sortPolicy == "" {
|
||||
sortPolicy = types.SortPolicyHybrid
|
||||
}
|
||||
orderBySQL := buildOrderByClause(sortPolicy)
|
||||
|
||||
// Query with recursive CTE to propagate blocking through parent-child hierarchy
|
||||
// Algorithm:
|
||||
// 1. Find issues directly blocked by 'blocks' dependencies
|
||||
@@ -85,23 +92,9 @@ func (s *SQLiteStorage) GetReadyWork(ctx context.Context, filter types.WorkFilte
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM blocked_transitively WHERE issue_id = i.id
|
||||
)
|
||||
ORDER BY
|
||||
-- Hybrid sort: recent issues (48 hours) by priority, then oldest-first
|
||||
CASE
|
||||
WHEN datetime(i.created_at) >= datetime('now', '-48 hours') THEN 0
|
||||
ELSE 1
|
||||
END ASC,
|
||||
CASE
|
||||
WHEN datetime(i.created_at) >= datetime('now', '-48 hours') THEN i.priority
|
||||
ELSE NULL
|
||||
END ASC,
|
||||
CASE
|
||||
WHEN datetime(i.created_at) < datetime('now', '-48 hours') THEN i.created_at
|
||||
ELSE NULL
|
||||
END ASC,
|
||||
i.created_at ASC
|
||||
%s
|
||||
`, whereSQL, limitSQL)
|
||||
%s
|
||||
%s
|
||||
`, whereSQL, orderBySQL, limitSQL)
|
||||
|
||||
rows, err := s.db.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
@@ -180,3 +173,32 @@ func (s *SQLiteStorage) GetBlockedIssues(ctx context.Context) ([]*types.BlockedI
|
||||
|
||||
return blocked, nil
|
||||
}
|
||||
|
||||
// buildOrderByClause generates the ORDER BY clause based on sort policy
|
||||
func buildOrderByClause(policy types.SortPolicy) string {
|
||||
switch policy {
|
||||
case types.SortPolicyPriority:
|
||||
return `ORDER BY i.priority ASC, i.created_at ASC`
|
||||
|
||||
case types.SortPolicyOldest:
|
||||
return `ORDER BY i.created_at ASC`
|
||||
|
||||
case types.SortPolicyHybrid:
|
||||
fallthrough
|
||||
default:
|
||||
return `ORDER BY
|
||||
CASE
|
||||
WHEN datetime(i.created_at) >= datetime('now', '-48 hours') THEN 0
|
||||
ELSE 1
|
||||
END ASC,
|
||||
CASE
|
||||
WHEN datetime(i.created_at) >= datetime('now', '-48 hours') THEN i.priority
|
||||
ELSE NULL
|
||||
END ASC,
|
||||
CASE
|
||||
WHEN datetime(i.created_at) < datetime('now', '-48 hours') THEN i.created_at
|
||||
ELSE NULL
|
||||
END ASC,
|
||||
i.created_at ASC`
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user