Add bd compact CLI command (bd-259)
Implements the compact command with all required features: - --dry-run: Preview compaction with size estimates - --all: Process all eligible candidates - --id: Compact specific issue - --force: Bypass eligibility checks (requires --id) - --stats: Show compaction statistics - --tier: Select compaction tier (1 or 2) - --workers: Configure parallel workers - --batch-size: Configure batch processing - Progress bar with visual feedback - JSON output support - Proper exit codes and error handling - Summary reporting (count, bytes saved, reduction %, time) Includes additional test coverage for compaction and snapshot operations. Amp-Thread-ID: https://ampcode.com/threads/T-ffcaf749-f79c-4b03-91dd-42136b2744b1 Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
199
internal/storage/sqlite/compact_bench_test.go
Normal file
199
internal/storage/sqlite/compact_bench_test.go
Normal file
@@ -0,0 +1,199 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
func BenchmarkGetTier1Candidates(b *testing.B) {
|
||||
store, cleanup := setupBenchDB(b)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
issue := &types.Issue{
|
||||
ID: generateID(b, "bd-", i),
|
||||
Title: "Benchmark issue",
|
||||
Description: "Test description for benchmarking",
|
||||
Status: "closed",
|
||||
Priority: 2,
|
||||
IssueType: "task",
|
||||
ClosedAt: timePtr(time.Now().Add(-40 * 24 * time.Hour)),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
b.Fatalf("Failed to create issue: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := store.GetTier1Candidates(ctx)
|
||||
if err != nil {
|
||||
b.Fatalf("GetTier1Candidates failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetTier2Candidates(b *testing.B) {
|
||||
store, cleanup := setupBenchDB(b)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
|
||||
for i := 0; i < 50; i++ {
|
||||
issue := &types.Issue{
|
||||
ID: generateID(b, "bd-", i),
|
||||
Title: "Benchmark issue",
|
||||
Description: "Test",
|
||||
Status: "closed",
|
||||
Priority: 2,
|
||||
IssueType: "task",
|
||||
ClosedAt: timePtr(time.Now().Add(-100 * 24 * time.Hour)),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
b.Fatalf("Failed to create issue: %v", err)
|
||||
}
|
||||
|
||||
_, err := store.db.ExecContext(ctx, `
|
||||
UPDATE issues
|
||||
SET compaction_level = 1,
|
||||
compacted_at = datetime('now', '-95 days'),
|
||||
original_size = 1000
|
||||
WHERE id = ?
|
||||
`, issue.ID)
|
||||
if err != nil {
|
||||
b.Fatalf("Failed to set compaction level: %v", err)
|
||||
}
|
||||
|
||||
for j := 0; j < 120; j++ {
|
||||
if err := store.AddComment(ctx, issue.ID, "test", "comment"); err != nil {
|
||||
b.Fatalf("Failed to add event: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := store.GetTier2Candidates(ctx)
|
||||
if err != nil {
|
||||
b.Fatalf("GetTier2Candidates failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCheckEligibility(b *testing.B) {
|
||||
store, cleanup := setupBenchDB(b)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
|
||||
issue := &types.Issue{
|
||||
ID: "bd-1",
|
||||
Title: "Eligible",
|
||||
Description: "Test",
|
||||
Status: "closed",
|
||||
Priority: 2,
|
||||
IssueType: "task",
|
||||
ClosedAt: timePtr(time.Now().Add(-40 * 24 * time.Hour)),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
b.Fatalf("Failed to create issue: %v", err)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, _, err := store.CheckEligibility(ctx, issue.ID, 1)
|
||||
if err != nil {
|
||||
b.Fatalf("CheckEligibility failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCreateSnapshot(b *testing.B) {
|
||||
store, cleanup := setupBenchDB(b)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
|
||||
issue := &types.Issue{
|
||||
ID: "bd-1",
|
||||
Title: "Test Issue",
|
||||
Description: "Original description with substantial content",
|
||||
Design: "Design notes with additional context",
|
||||
Notes: "Additional notes for the issue",
|
||||
AcceptanceCriteria: "Must meet all requirements",
|
||||
Status: "closed",
|
||||
Priority: 2,
|
||||
IssueType: "task",
|
||||
ClosedAt: timePtr(time.Now()),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
b.Fatalf("Failed to create issue: %v", err)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
if err := store.CreateSnapshot(ctx, issue, i%5+1); err != nil {
|
||||
b.Fatalf("CreateSnapshot failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetSnapshots(b *testing.B) {
|
||||
store, cleanup := setupBenchDB(b)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
|
||||
issue := &types.Issue{
|
||||
ID: "bd-1",
|
||||
Title: "Test",
|
||||
Description: "Test description",
|
||||
Status: "closed",
|
||||
Priority: 2,
|
||||
IssueType: "task",
|
||||
ClosedAt: timePtr(time.Now()),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
b.Fatalf("Failed to create issue: %v", err)
|
||||
}
|
||||
|
||||
for i := 1; i <= 5; i++ {
|
||||
if err := store.CreateSnapshot(ctx, issue, i); err != nil {
|
||||
b.Fatalf("CreateSnapshot failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_, err := store.GetSnapshots(ctx, issue.ID)
|
||||
if err != nil {
|
||||
b.Fatalf("GetSnapshots failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func generateID(b testing.TB, prefix string, n int) string {
|
||||
b.Helper()
|
||||
return prefix + string(rune('0'+n/10)) + string(rune('0'+n%10))
|
||||
}
|
||||
|
||||
func setupBenchDB(tb testing.TB) (*SQLiteStorage, func()) {
|
||||
tb.Helper()
|
||||
tmpDB := tb.TempDir() + "/test.db"
|
||||
store, err := New(tmpDB)
|
||||
if err != nil {
|
||||
tb.Fatalf("Failed to create storage: %v", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
if err := store.SetConfig(ctx, "compact_tier1_days", "30"); err != nil {
|
||||
tb.Fatalf("Failed to set config: %v", err)
|
||||
}
|
||||
if err := store.SetConfig(ctx, "compact_tier1_dep_levels", "2"); err != nil {
|
||||
tb.Fatalf("Failed to set config: %v", err)
|
||||
}
|
||||
|
||||
return store, func() {
|
||||
store.Close()
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -313,6 +315,282 @@ func TestTier1NoCircularDeps(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSnapshot(t *testing.T) {
|
||||
store, cleanup := setupTestDB(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
|
||||
issue := &types.Issue{
|
||||
ID: "bd-1",
|
||||
Title: "Test Issue",
|
||||
Description: "Original description",
|
||||
Design: "Design notes",
|
||||
Notes: "Additional notes",
|
||||
AcceptanceCriteria: "Must work",
|
||||
Status: "closed",
|
||||
Priority: 2,
|
||||
IssueType: "task",
|
||||
ClosedAt: timePtr(time.Now()),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("Failed to create issue: %v", err)
|
||||
}
|
||||
|
||||
err := store.CreateSnapshot(ctx, issue, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateSnapshot failed: %v", err)
|
||||
}
|
||||
|
||||
snapshots, err := store.GetSnapshots(ctx, issue.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetSnapshots failed: %v", err)
|
||||
}
|
||||
|
||||
if len(snapshots) != 1 {
|
||||
t.Fatalf("Expected 1 snapshot, got %d", len(snapshots))
|
||||
}
|
||||
|
||||
snapshot := snapshots[0]
|
||||
if snapshot.Description != issue.Description {
|
||||
t.Errorf("Expected description %q, got %q", issue.Description, snapshot.Description)
|
||||
}
|
||||
if snapshot.Design != issue.Design {
|
||||
t.Errorf("Expected design %q, got %q", issue.Design, snapshot.Design)
|
||||
}
|
||||
if snapshot.Notes != issue.Notes {
|
||||
t.Errorf("Expected notes %q, got %q", issue.Notes, snapshot.Notes)
|
||||
}
|
||||
if snapshot.AcceptanceCriteria != issue.AcceptanceCriteria {
|
||||
t.Errorf("Expected criteria %q, got %q", issue.AcceptanceCriteria, snapshot.AcceptanceCriteria)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateSnapshotUTF8(t *testing.T) {
|
||||
store, cleanup := setupTestDB(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
|
||||
issue := &types.Issue{
|
||||
ID: "bd-1",
|
||||
Title: "UTF-8 Test 🎉",
|
||||
Description: "Café, résumé, 日本語, emoji 🚀",
|
||||
Design: "Design with 中文 and émojis 🔥",
|
||||
Notes: "Notes: ñ, ü, é, à",
|
||||
AcceptanceCriteria: "Must handle UTF-8 correctly ✅",
|
||||
Status: "closed",
|
||||
Priority: 2,
|
||||
IssueType: "task",
|
||||
ClosedAt: timePtr(time.Now()),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("Failed to create issue: %v", err)
|
||||
}
|
||||
|
||||
err := store.CreateSnapshot(ctx, issue, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateSnapshot failed: %v", err)
|
||||
}
|
||||
|
||||
snapshots, err := store.GetSnapshots(ctx, issue.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetSnapshots failed: %v", err)
|
||||
}
|
||||
|
||||
if len(snapshots) != 1 {
|
||||
t.Fatalf("Expected 1 snapshot, got %d", len(snapshots))
|
||||
}
|
||||
|
||||
snapshot := snapshots[0]
|
||||
if snapshot.Title != issue.Title {
|
||||
t.Errorf("UTF-8 title not preserved: expected %q, got %q", issue.Title, snapshot.Title)
|
||||
}
|
||||
if snapshot.Description != issue.Description {
|
||||
t.Errorf("UTF-8 description not preserved: expected %q, got %q", issue.Description, snapshot.Description)
|
||||
}
|
||||
if snapshot.Design != issue.Design {
|
||||
t.Errorf("UTF-8 design not preserved: expected %q, got %q", issue.Design, snapshot.Design)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateMultipleSnapshots(t *testing.T) {
|
||||
store, cleanup := setupTestDB(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
|
||||
issue := &types.Issue{
|
||||
ID: "bd-1",
|
||||
Title: "Test Issue",
|
||||
Description: "Original",
|
||||
Status: "closed",
|
||||
Priority: 2,
|
||||
IssueType: "task",
|
||||
ClosedAt: timePtr(time.Now()),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("Failed to create issue: %v", err)
|
||||
}
|
||||
|
||||
if err := store.CreateSnapshot(ctx, issue, 1); err != nil {
|
||||
t.Fatalf("CreateSnapshot level 1 failed: %v", err)
|
||||
}
|
||||
|
||||
issue.Description = "Compacted once"
|
||||
if err := store.CreateSnapshot(ctx, issue, 2); err != nil {
|
||||
t.Fatalf("CreateSnapshot level 2 failed: %v", err)
|
||||
}
|
||||
|
||||
snapshots, err := store.GetSnapshots(ctx, issue.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetSnapshots failed: %v", err)
|
||||
}
|
||||
|
||||
if len(snapshots) != 2 {
|
||||
t.Fatalf("Expected 2 snapshots, got %d", len(snapshots))
|
||||
}
|
||||
|
||||
if snapshots[0].CompactionLevel != 1 {
|
||||
t.Errorf("Expected first snapshot level 1, got %d", snapshots[0].CompactionLevel)
|
||||
}
|
||||
if snapshots[1].CompactionLevel != 2 {
|
||||
t.Errorf("Expected second snapshot level 2, got %d", snapshots[1].CompactionLevel)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreFromSnapshot(t *testing.T) {
|
||||
store, cleanup := setupTestDB(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
|
||||
issue := &types.Issue{
|
||||
ID: "bd-1",
|
||||
Title: "Original Title",
|
||||
Description: "Original description",
|
||||
Design: "Original design",
|
||||
Notes: "Original notes",
|
||||
AcceptanceCriteria: "Original criteria",
|
||||
Status: "closed",
|
||||
Priority: 2,
|
||||
IssueType: "task",
|
||||
ClosedAt: timePtr(time.Now()),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("Failed to create issue: %v", err)
|
||||
}
|
||||
|
||||
if err := store.CreateSnapshot(ctx, issue, 1); err != nil {
|
||||
t.Fatalf("CreateSnapshot failed: %v", err)
|
||||
}
|
||||
|
||||
_, err := store.db.ExecContext(ctx, `
|
||||
UPDATE issues
|
||||
SET description = 'Compacted',
|
||||
design = '',
|
||||
notes = '',
|
||||
acceptance_criteria = '',
|
||||
compaction_level = 1
|
||||
WHERE id = ?
|
||||
`, issue.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to update issue: %v", err)
|
||||
}
|
||||
|
||||
err = store.RestoreFromSnapshot(ctx, issue.ID, 1)
|
||||
if err != nil {
|
||||
t.Fatalf("RestoreFromSnapshot failed: %v", err)
|
||||
}
|
||||
|
||||
restored, err := store.GetIssue(ctx, issue.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetIssue failed: %v", err)
|
||||
}
|
||||
|
||||
if restored.Description != issue.Description {
|
||||
t.Errorf("Description not restored: expected %q, got %q", issue.Description, restored.Description)
|
||||
}
|
||||
if restored.Design != issue.Design {
|
||||
t.Errorf("Design not restored: expected %q, got %q", issue.Design, restored.Design)
|
||||
}
|
||||
if restored.Notes != issue.Notes {
|
||||
t.Errorf("Notes not restored: expected %q, got %q", issue.Notes, restored.Notes)
|
||||
}
|
||||
if restored.AcceptanceCriteria != issue.AcceptanceCriteria {
|
||||
t.Errorf("Criteria not restored: expected %q, got %q", issue.AcceptanceCriteria, restored.AcceptanceCriteria)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreSnapshotNoSnapshot(t *testing.T) {
|
||||
store, cleanup := setupTestDB(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
|
||||
issue := &types.Issue{
|
||||
ID: "bd-1",
|
||||
Title: "Test",
|
||||
Description: "Test",
|
||||
Status: "closed",
|
||||
Priority: 2,
|
||||
IssueType: "task",
|
||||
ClosedAt: timePtr(time.Now()),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("Failed to create issue: %v", err)
|
||||
}
|
||||
|
||||
err := store.RestoreFromSnapshot(ctx, issue.ID, 1)
|
||||
if err == nil {
|
||||
t.Fatal("Expected error when no snapshot exists")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "no snapshot found") {
|
||||
t.Errorf("Expected 'no snapshot found' error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyCompaction(t *testing.T) {
|
||||
store, cleanup := setupTestDB(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
|
||||
issue := &types.Issue{
|
||||
ID: "bd-1",
|
||||
Title: "Test",
|
||||
Description: "Original description that is quite long",
|
||||
Status: "closed",
|
||||
Priority: 2,
|
||||
IssueType: "task",
|
||||
ClosedAt: timePtr(time.Now()),
|
||||
}
|
||||
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
|
||||
t.Fatalf("Failed to create issue: %v", err)
|
||||
}
|
||||
|
||||
originalSize := len(issue.Description)
|
||||
err := store.ApplyCompaction(ctx, issue.ID, 1, originalSize)
|
||||
if err != nil {
|
||||
t.Fatalf("ApplyCompaction failed: %v", err)
|
||||
}
|
||||
|
||||
var compactionLevel int
|
||||
var compactedAt sql.NullTime
|
||||
var storedSize int
|
||||
err = store.db.QueryRowContext(ctx, `
|
||||
SELECT COALESCE(compaction_level, 0), compacted_at, COALESCE(original_size, 0)
|
||||
FROM issues WHERE id = ?
|
||||
`, issue.ID).Scan(&compactionLevel, &compactedAt, &storedSize)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to query issue: %v", err)
|
||||
}
|
||||
|
||||
if compactionLevel != 1 {
|
||||
t.Errorf("Expected compaction_level 1, got %d", compactionLevel)
|
||||
}
|
||||
if !compactedAt.Valid {
|
||||
t.Error("Expected compacted_at to be set")
|
||||
}
|
||||
if storedSize != originalSize {
|
||||
t.Errorf("Expected original_size %d, got %d", originalSize, storedSize)
|
||||
}
|
||||
}
|
||||
|
||||
func timePtr(t time.Time) *time.Time {
|
||||
return &t
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user