Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8fe3f5a3c | ||
|
|
87ceea3d30 |
@@ -0,0 +1,331 @@
|
|||||||
|
package batching
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GitOp represents a git operation to be batched
|
||||||
|
type GitOp struct {
|
||||||
|
OpType string // "commit", "push", "merge"
|
||||||
|
Branch string
|
||||||
|
Message string
|
||||||
|
Files []string
|
||||||
|
Timestamp time.Time
|
||||||
|
ID string
|
||||||
|
}
|
||||||
|
|
||||||
|
// GitBatch represents a batch of git operations
|
||||||
|
type GitBatch struct {
|
||||||
|
ID string
|
||||||
|
Operations []*GitOp
|
||||||
|
CreatedAt time.Time
|
||||||
|
ExecutedAt time.Time
|
||||||
|
Status string // "pending", "executing", "completed", "failed"
|
||||||
|
Error error
|
||||||
|
}
|
||||||
|
|
||||||
|
// GitBatcher batches git operations for efficient execution
|
||||||
|
type GitBatcher struct {
|
||||||
|
mu sync.RWMutex
|
||||||
|
queue []*GitOp
|
||||||
|
maxBatchSize int
|
||||||
|
maxBatchAge time.Duration
|
||||||
|
lastFlushTime time.Time
|
||||||
|
executedBatches []*GitBatch
|
||||||
|
pendingBatches []*GitBatch
|
||||||
|
stats *BatchStats
|
||||||
|
flushChan chan struct{}
|
||||||
|
stopChan chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BatchStats tracks batching statistics
|
||||||
|
type BatchStats struct {
|
||||||
|
TotalOps int
|
||||||
|
TotalBatches int
|
||||||
|
AvgOpsPerBatch float64
|
||||||
|
NetworkSavings int // Estimated network round trips saved
|
||||||
|
TotalExecuteTime time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGitBatcher creates a new git batcher
|
||||||
|
func NewGitBatcher(maxBatchSize int, maxBatchAge time.Duration) *GitBatcher {
|
||||||
|
if maxBatchSize <= 0 {
|
||||||
|
maxBatchSize = 10
|
||||||
|
}
|
||||||
|
if maxBatchAge <= 0 {
|
||||||
|
maxBatchAge = 5 * time.Second
|
||||||
|
}
|
||||||
|
|
||||||
|
return &GitBatcher{
|
||||||
|
queue: make([]*GitOp, 0),
|
||||||
|
maxBatchSize: maxBatchSize,
|
||||||
|
maxBatchAge: maxBatchAge,
|
||||||
|
lastFlushTime: time.Now(),
|
||||||
|
executedBatches: make([]*GitBatch, 0),
|
||||||
|
pendingBatches: make([]*GitBatch, 0),
|
||||||
|
stats: &BatchStats{
|
||||||
|
TotalOps: 0,
|
||||||
|
TotalBatches: 0,
|
||||||
|
},
|
||||||
|
flushChan: make(chan struct{}, 1),
|
||||||
|
stopChan: make(chan struct{}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enqueue adds a git operation to the queue
|
||||||
|
func (gb *GitBatcher) Enqueue(op *GitOp) {
|
||||||
|
if op == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
op.Timestamp = time.Now()
|
||||||
|
|
||||||
|
gb.mu.Lock()
|
||||||
|
defer gb.mu.Unlock()
|
||||||
|
|
||||||
|
gb.queue = append(gb.queue, op)
|
||||||
|
gb.stats.TotalOps++
|
||||||
|
|
||||||
|
// Auto-flush if batch is full
|
||||||
|
if len(gb.queue) >= gb.maxBatchSize {
|
||||||
|
gb.flushLocked()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// flushLocked creates a batch from queued operations (must be called with lock held)
|
||||||
|
func (gb *GitBatcher) flushLocked() {
|
||||||
|
if len(gb.queue) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
batch := &GitBatch{
|
||||||
|
ID: fmt.Sprintf("batch-%d", gb.stats.TotalBatches),
|
||||||
|
Operations: make([]*GitOp, len(gb.queue)),
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
Status: "pending",
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(batch.Operations, gb.queue)
|
||||||
|
|
||||||
|
gb.pendingBatches = append(gb.pendingBatches, batch)
|
||||||
|
gb.queue = make([]*GitOp, 0)
|
||||||
|
gb.lastFlushTime = time.Now()
|
||||||
|
gb.stats.TotalBatches++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Flush manually flushes the current batch
|
||||||
|
func (gb *GitBatcher) Flush() {
|
||||||
|
gb.mu.Lock()
|
||||||
|
defer gb.mu.Unlock()
|
||||||
|
|
||||||
|
gb.flushLocked()
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPendingBatch returns the next pending batch without removing it
|
||||||
|
func (gb *GitBatcher) GetPendingBatch() *GitBatch {
|
||||||
|
gb.mu.RLock()
|
||||||
|
defer gb.mu.RUnlock()
|
||||||
|
|
||||||
|
if len(gb.pendingBatches) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return gb.pendingBatches[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkBatchExecuting marks a batch as executing
|
||||||
|
func (gb *GitBatcher) MarkBatchExecuting(batchID string) {
|
||||||
|
gb.mu.Lock()
|
||||||
|
defer gb.mu.Unlock()
|
||||||
|
|
||||||
|
for _, batch := range gb.pendingBatches {
|
||||||
|
if batch.ID == batchID {
|
||||||
|
batch.Status = "executing"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkBatchCompleted marks a batch as completed and removes from pending
|
||||||
|
func (gb *GitBatcher) MarkBatchCompleted(batchID string) {
|
||||||
|
gb.mu.Lock()
|
||||||
|
defer gb.mu.Unlock()
|
||||||
|
|
||||||
|
var idx int
|
||||||
|
var found *GitBatch
|
||||||
|
for i, batch := range gb.pendingBatches {
|
||||||
|
if batch.ID == batchID {
|
||||||
|
idx = i
|
||||||
|
found = batch
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if found != nil {
|
||||||
|
found.Status = "completed"
|
||||||
|
found.ExecutedAt = time.Now()
|
||||||
|
|
||||||
|
// Move to executed batches
|
||||||
|
gb.executedBatches = append(gb.executedBatches, found)
|
||||||
|
|
||||||
|
// Remove from pending
|
||||||
|
gb.pendingBatches = append(gb.pendingBatches[:idx], gb.pendingBatches[idx+1:]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MarkBatchFailed marks a batch as failed with an error
|
||||||
|
func (gb *GitBatcher) MarkBatchFailed(batchID string, err error) {
|
||||||
|
gb.mu.Lock()
|
||||||
|
defer gb.mu.Unlock()
|
||||||
|
|
||||||
|
var found *GitBatch
|
||||||
|
for _, batch := range gb.pendingBatches {
|
||||||
|
if batch.ID == batchID {
|
||||||
|
found = batch
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if found != nil {
|
||||||
|
found.Status = "failed"
|
||||||
|
found.Error = err
|
||||||
|
found.ExecutedAt = time.Now()
|
||||||
|
|
||||||
|
// Keep in pending (for retry logic)
|
||||||
|
// Could also move to failed queue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueueSize returns the current queue size
|
||||||
|
func (gb *GitBatcher) QueueSize() int {
|
||||||
|
gb.mu.RLock()
|
||||||
|
defer gb.mu.RUnlock()
|
||||||
|
|
||||||
|
return len(gb.queue)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PendingBatchCount returns the number of pending batches
|
||||||
|
func (gb *GitBatcher) PendingBatchCount() int {
|
||||||
|
gb.mu.RLock()
|
||||||
|
defer gb.mu.RUnlock()
|
||||||
|
|
||||||
|
return len(gb.pendingBatches)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStats returns batching statistics
|
||||||
|
func (gb *GitBatcher) GetStats() *BatchStats {
|
||||||
|
gb.mu.RLock()
|
||||||
|
defer gb.mu.RUnlock()
|
||||||
|
|
||||||
|
stats := *gb.stats
|
||||||
|
if stats.TotalBatches > 0 {
|
||||||
|
stats.AvgOpsPerBatch = float64(stats.TotalOps) / float64(stats.TotalBatches)
|
||||||
|
// Estimated savings: each batch saves (ops-1) round trips
|
||||||
|
stats.NetworkSavings = stats.TotalOps - stats.TotalBatches
|
||||||
|
}
|
||||||
|
|
||||||
|
return &stats
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetExecutedBatches returns all executed batches
|
||||||
|
func (gb *GitBatcher) GetExecutedBatches() []*GitBatch {
|
||||||
|
gb.mu.RLock()
|
||||||
|
defer gb.mu.RUnlock()
|
||||||
|
|
||||||
|
result := make([]*GitBatch, len(gb.executedBatches))
|
||||||
|
copy(result, gb.executedBatches)
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBatchByID returns a specific batch by ID
|
||||||
|
func (gb *GitBatcher) GetBatchByID(batchID string) *GitBatch {
|
||||||
|
gb.mu.RLock()
|
||||||
|
defer gb.mu.RUnlock()
|
||||||
|
|
||||||
|
for _, batch := range gb.pendingBatches {
|
||||||
|
if batch.ID == batchID {
|
||||||
|
return batch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, batch := range gb.executedBatches {
|
||||||
|
if batch.ID == batchID {
|
||||||
|
return batch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TimeSinceLastFlush returns time since last flush
|
||||||
|
func (gb *GitBatcher) TimeSinceLastFlush() time.Duration {
|
||||||
|
gb.mu.RLock()
|
||||||
|
defer gb.mu.RUnlock()
|
||||||
|
|
||||||
|
return time.Since(gb.lastFlushTime)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ShouldFlush checks if batch should be flushed based on age
|
||||||
|
func (gb *GitBatcher) ShouldFlush() bool {
|
||||||
|
gb.mu.RLock()
|
||||||
|
defer gb.mu.RUnlock()
|
||||||
|
|
||||||
|
if len(gb.queue) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return time.Since(gb.lastFlushTime) >= gb.maxBatchAge
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear clears all pending operations and batches
|
||||||
|
func (gb *GitBatcher) Clear() {
|
||||||
|
gb.mu.Lock()
|
||||||
|
defer gb.mu.Unlock()
|
||||||
|
|
||||||
|
gb.queue = make([]*GitOp, 0)
|
||||||
|
gb.pendingBatches = make([]*GitBatch, 0)
|
||||||
|
gb.executedBatches = make([]*GitBatch, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetQueuedOps returns a copy of queued operations
|
||||||
|
func (gb *GitBatcher) GetQueuedOps() []*GitOp {
|
||||||
|
gb.mu.RLock()
|
||||||
|
defer gb.mu.RUnlock()
|
||||||
|
|
||||||
|
ops := make([]*GitOp, len(gb.queue))
|
||||||
|
copy(ops, gb.queue)
|
||||||
|
|
||||||
|
return ops
|
||||||
|
}
|
||||||
|
|
||||||
|
// CalculateNetworkSavings calculates estimated network round trips saved
|
||||||
|
func (gb *GitBatcher) CalculateNetworkSavings() int {
|
||||||
|
gb.mu.RLock()
|
||||||
|
defer gb.mu.RUnlock()
|
||||||
|
|
||||||
|
totalSavings := 0
|
||||||
|
// Each batch of N operations saves N-1 round trips
|
||||||
|
for _, batch := range gb.executedBatches {
|
||||||
|
if len(batch.Operations) > 1 {
|
||||||
|
totalSavings += len(batch.Operations) - 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return totalSavings
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBatchInfo returns human-readable batch information
|
||||||
|
func (batch *GitBatch) GetInfo() map[string]interface{} {
|
||||||
|
return map[string]interface{}{
|
||||||
|
"id": batch.ID,
|
||||||
|
"status": batch.Status,
|
||||||
|
"op_count": len(batch.Operations),
|
||||||
|
"created_at": batch.CreatedAt,
|
||||||
|
"executed_at": batch.ExecutedAt,
|
||||||
|
"duration": batch.ExecutedAt.Sub(batch.CreatedAt),
|
||||||
|
"error": batch.Error,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,355 @@
|
|||||||
|
package batching
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewGitBatcher(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
assert.NotNil(t, batcher)
|
||||||
|
assert.Equal(t, 0, batcher.QueueSize())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnqueueOperation(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
|
||||||
|
op := &GitOp{
|
||||||
|
OpType: "commit",
|
||||||
|
Branch: "main",
|
||||||
|
Message: "Add feature",
|
||||||
|
Files: []string{"file1.go"},
|
||||||
|
}
|
||||||
|
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
assert.Equal(t, 1, batcher.QueueSize())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnqueueMultipleOps(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
op := &GitOp{
|
||||||
|
OpType: "commit",
|
||||||
|
Branch: "main",
|
||||||
|
Message: "Commit",
|
||||||
|
}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, 5, batcher.QueueSize())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAutoFlushOnMaxBatchSize(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(5, 10*time.Second)
|
||||||
|
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
op := &GitOp{
|
||||||
|
OpType: "commit",
|
||||||
|
Branch: "main",
|
||||||
|
Message: "Commit",
|
||||||
|
}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
}
|
||||||
|
|
||||||
|
// After 5 ops, should auto-flush
|
||||||
|
assert.Equal(t, 0, batcher.QueueSize())
|
||||||
|
assert.Equal(t, 1, batcher.PendingBatchCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestManualFlush(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
|
||||||
|
op := &GitOp{
|
||||||
|
OpType: "commit",
|
||||||
|
Branch: "main",
|
||||||
|
Message: "Commit",
|
||||||
|
}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
assert.Equal(t, 1, batcher.QueueSize())
|
||||||
|
|
||||||
|
batcher.Flush()
|
||||||
|
assert.Equal(t, 0, batcher.QueueSize())
|
||||||
|
assert.Equal(t, 1, batcher.PendingBatchCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetPendingBatch(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
|
||||||
|
op := &GitOp{
|
||||||
|
OpType: "commit",
|
||||||
|
Branch: "main",
|
||||||
|
Message: "Commit",
|
||||||
|
}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
batcher.Flush()
|
||||||
|
|
||||||
|
batch := batcher.GetPendingBatch()
|
||||||
|
assert.NotNil(t, batch)
|
||||||
|
assert.Equal(t, 1, len(batch.Operations))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarkBatchExecuting(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
|
||||||
|
op := &GitOp{OpType: "commit"}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
batcher.Flush()
|
||||||
|
|
||||||
|
batch := batcher.GetPendingBatch()
|
||||||
|
batcher.MarkBatchExecuting(batch.ID)
|
||||||
|
|
||||||
|
updated := batcher.GetBatchByID(batch.ID)
|
||||||
|
assert.Equal(t, "executing", updated.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarkBatchCompleted(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
|
||||||
|
op := &GitOp{OpType: "commit"}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
batcher.Flush()
|
||||||
|
|
||||||
|
batch := batcher.GetPendingBatch()
|
||||||
|
batcher.MarkBatchCompleted(batch.ID)
|
||||||
|
|
||||||
|
executed := batcher.GetExecutedBatches()
|
||||||
|
assert.Equal(t, 1, len(executed))
|
||||||
|
assert.Equal(t, "completed", executed[0].Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMarkBatchFailed(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
|
||||||
|
op := &GitOp{OpType: "commit"}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
batcher.Flush()
|
||||||
|
|
||||||
|
batch := batcher.GetPendingBatch()
|
||||||
|
testErr := assert.AnError
|
||||||
|
batcher.MarkBatchFailed(batch.ID, testErr)
|
||||||
|
|
||||||
|
failed := batcher.GetBatchByID(batch.ID)
|
||||||
|
assert.Equal(t, "failed", failed.Status)
|
||||||
|
assert.Error(t, failed.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetStats(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(5, 5*time.Second)
|
||||||
|
|
||||||
|
// Add 10 ops (will create 2 batches of 5 each)
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
op := &GitOp{OpType: "commit"}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
}
|
||||||
|
|
||||||
|
stats := batcher.GetStats()
|
||||||
|
assert.Equal(t, 10, stats.TotalOps)
|
||||||
|
assert.Equal(t, 2, stats.TotalBatches)
|
||||||
|
assert.Equal(t, 5.0, stats.AvgOpsPerBatch)
|
||||||
|
// 10 ops in 2 batches saves 8 round trips (5-1 + 5-1)
|
||||||
|
assert.Equal(t, 8, stats.NetworkSavings)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQueueSize(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
|
||||||
|
op := &GitOp{OpType: "commit"}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
|
||||||
|
assert.Equal(t, 1, batcher.QueueSize())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPendingBatchCount(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
|
||||||
|
op := &GitOp{OpType: "commit"}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
batcher.Flush()
|
||||||
|
|
||||||
|
assert.Equal(t, 1, batcher.PendingBatchCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetExecutedBatches(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
|
||||||
|
// Create and execute batches
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
op := &GitOp{OpType: "commit"}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
batcher.Flush()
|
||||||
|
|
||||||
|
batch := batcher.GetPendingBatch()
|
||||||
|
batcher.MarkBatchCompleted(batch.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
executed := batcher.GetExecutedBatches()
|
||||||
|
assert.Equal(t, 2, len(executed))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTimeSinceLastFlush(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
|
||||||
|
op := &GitOp{OpType: "commit"}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
batcher.Flush()
|
||||||
|
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
elapsed := batcher.TimeSinceLastFlush()
|
||||||
|
|
||||||
|
assert.Greater(t, elapsed, 50*time.Millisecond)
|
||||||
|
assert.Less(t, elapsed, 200*time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShouldFlush(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(100, 100*time.Millisecond)
|
||||||
|
|
||||||
|
// Empty queue should not flush
|
||||||
|
assert.False(t, batcher.ShouldFlush())
|
||||||
|
|
||||||
|
// Enqueue but not old enough
|
||||||
|
op := &GitOp{OpType: "commit"}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
assert.False(t, batcher.ShouldFlush())
|
||||||
|
|
||||||
|
// Wait for age to exceed max age
|
||||||
|
time.Sleep(150 * time.Millisecond)
|
||||||
|
assert.True(t, batcher.ShouldFlush())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClear(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
|
||||||
|
op := &GitOp{OpType: "commit"}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
batcher.Flush()
|
||||||
|
|
||||||
|
assert.Equal(t, 1, batcher.PendingBatchCount())
|
||||||
|
|
||||||
|
batcher.Clear()
|
||||||
|
assert.Equal(t, 0, batcher.QueueSize())
|
||||||
|
assert.Equal(t, 0, batcher.PendingBatchCount())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetQueuedOps(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
|
||||||
|
ops := []*GitOp{
|
||||||
|
{OpType: "commit", Message: "Commit 1"},
|
||||||
|
{OpType: "commit", Message: "Commit 2"},
|
||||||
|
{OpType: "commit", Message: "Commit 3"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, op := range ops {
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
}
|
||||||
|
|
||||||
|
queued := batcher.GetQueuedOps()
|
||||||
|
assert.Equal(t, 3, len(queued))
|
||||||
|
assert.Equal(t, "Commit 1", queued[0].Message)
|
||||||
|
assert.Equal(t, "Commit 3", queued[2].Message)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCalculateNetworkSavings(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(3, 5*time.Second)
|
||||||
|
|
||||||
|
// Add 6 ops (will create 2 batches of 3 each)
|
||||||
|
for i := 0; i < 6; i++ {
|
||||||
|
op := &GitOp{OpType: "commit"}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark both batches as completed
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
batch := batcher.GetPendingBatch()
|
||||||
|
if batch != nil {
|
||||||
|
batcher.MarkBatchCompleted(batch.ID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
savings := batcher.CalculateNetworkSavings()
|
||||||
|
// 2 batches of 3 each saves 4 round trips (3-1 + 3-1)
|
||||||
|
assert.Equal(t, 4, savings)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetBatchByID(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
|
||||||
|
op := &GitOp{OpType: "commit"}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
batcher.Flush()
|
||||||
|
|
||||||
|
batch := batcher.GetPendingBatch()
|
||||||
|
retrieved := batcher.GetBatchByID(batch.ID)
|
||||||
|
|
||||||
|
assert.NotNil(t, retrieved)
|
||||||
|
assert.Equal(t, batch.ID, retrieved.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetBatchInfo(t *testing.T) {
|
||||||
|
batch := &GitBatch{
|
||||||
|
ID: "test-batch",
|
||||||
|
Status: "completed",
|
||||||
|
CreatedAt: time.Now(),
|
||||||
|
ExecutedAt: time.Now().Add(1 * time.Second),
|
||||||
|
}
|
||||||
|
|
||||||
|
info := batch.GetInfo()
|
||||||
|
assert.Equal(t, "test-batch", info["id"])
|
||||||
|
assert.Equal(t, "completed", info["status"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMultipleBatches(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(3, 5*time.Second)
|
||||||
|
|
||||||
|
// Create 3 batches
|
||||||
|
for batch := 0; batch < 3; batch++ {
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
op := &GitOp{
|
||||||
|
OpType: "commit",
|
||||||
|
Branch: "main",
|
||||||
|
}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// All 3 batches should be pending
|
||||||
|
assert.Equal(t, 3, batcher.PendingBatchCount())
|
||||||
|
assert.Equal(t, 0, batcher.QueueSize())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEnqueueNil(t *testing.T) {
|
||||||
|
batcher := NewGitBatcher(10, 5*time.Second)
|
||||||
|
|
||||||
|
// Enqueueing nil should not fail
|
||||||
|
batcher.Enqueue(nil)
|
||||||
|
assert.Equal(t, 0, batcher.QueueSize())
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkEnqueue(b *testing.B) {
|
||||||
|
batcher := NewGitBatcher(1000, 10*time.Second)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
op := &GitOp{
|
||||||
|
OpType: "commit",
|
||||||
|
Branch: "main",
|
||||||
|
Message: "Commit",
|
||||||
|
}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkFlush(b *testing.B) {
|
||||||
|
batcher := NewGitBatcher(1000, 10*time.Second)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
op := &GitOp{OpType: "commit"}
|
||||||
|
batcher.Enqueue(op)
|
||||||
|
|
||||||
|
if (i + 1) % 100 == 0 {
|
||||||
|
batcher.Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,390 @@
|
|||||||
|
package indexing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Lesson represents a learned lesson from a past failure
|
||||||
|
type Lesson struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
TaskType string `json:"task_type"`
|
||||||
|
ActivityType string `json:"activity_type"`
|
||||||
|
FailureType string `json:"failure_type"`
|
||||||
|
FailureMsg string `json:"failure_msg"`
|
||||||
|
Resolution string `json:"resolution"`
|
||||||
|
Pattern string `json:"pattern"`
|
||||||
|
TimesSeen int `json:"times_seen"`
|
||||||
|
LastSeen time.Time `json:"last_seen"`
|
||||||
|
FirstSeen time.Time `json:"first_seen"`
|
||||||
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// LessonIndex provides fast indexed access to lessons
|
||||||
|
type LessonIndex struct {
|
||||||
|
mu sync.RWMutex
|
||||||
|
lessons map[string]*Lesson // ID -> Lesson
|
||||||
|
byTaskType map[string][]*Lesson // TaskType -> Lessons
|
||||||
|
byActivityType map[string][]*Lesson // ActivityType -> Lessons
|
||||||
|
byFailureType map[string][]*Lesson // FailureType -> Lessons
|
||||||
|
byPattern map[string][]*Lesson // Pattern -> Lessons
|
||||||
|
sourceFile string
|
||||||
|
lastBuiltTime time.Time
|
||||||
|
lessonCount int
|
||||||
|
buildTime time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewLessonIndex creates a new lesson index
|
||||||
|
func NewLessonIndex() *LessonIndex {
|
||||||
|
return &LessonIndex{
|
||||||
|
lessons: make(map[string]*Lesson),
|
||||||
|
byTaskType: make(map[string][]*Lesson),
|
||||||
|
byActivityType: make(map[string][]*Lesson),
|
||||||
|
byFailureType: make(map[string][]*Lesson),
|
||||||
|
byPattern: make(map[string][]*Lesson),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuildFromFile loads lessons from a JSONL file and builds the index
|
||||||
|
func (li *LessonIndex) BuildFromFile(filePath string) error {
|
||||||
|
li.mu.Lock()
|
||||||
|
defer li.mu.Unlock()
|
||||||
|
|
||||||
|
startTime := time.Now()
|
||||||
|
|
||||||
|
// Clear existing index
|
||||||
|
li.lessons = make(map[string]*Lesson)
|
||||||
|
li.byTaskType = make(map[string][]*Lesson)
|
||||||
|
li.byActivityType = make(map[string][]*Lesson)
|
||||||
|
li.byFailureType = make(map[string][]*Lesson)
|
||||||
|
li.byPattern = make(map[string][]*Lesson)
|
||||||
|
|
||||||
|
// Open file
|
||||||
|
file, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
li.sourceFile = filePath
|
||||||
|
li.lastBuiltTime = time.Now()
|
||||||
|
li.buildTime = time.Since(startTime)
|
||||||
|
return nil // File doesn't exist yet
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// Read JSONL lines
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
var lesson Lesson
|
||||||
|
if err := json.Unmarshal(scanner.Bytes(), &lesson); err != nil {
|
||||||
|
continue // Skip malformed lines
|
||||||
|
}
|
||||||
|
|
||||||
|
li.addLessonLocked(&lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
li.sourceFile = filePath
|
||||||
|
li.lastBuiltTime = time.Now()
|
||||||
|
li.buildTime = time.Since(startTime)
|
||||||
|
li.lessonCount = len(li.lessons)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// addLessonLocked adds a lesson to all indexes (must be called with lock held)
|
||||||
|
func (li *LessonIndex) addLessonLocked(lesson *Lesson) {
|
||||||
|
if lesson.ID == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
li.lessons[lesson.ID] = lesson
|
||||||
|
|
||||||
|
// Index by task type
|
||||||
|
if lesson.TaskType != "" {
|
||||||
|
li.byTaskType[lesson.TaskType] = append(li.byTaskType[lesson.TaskType], lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Index by activity type
|
||||||
|
if lesson.ActivityType != "" {
|
||||||
|
li.byActivityType[lesson.ActivityType] = append(li.byActivityType[lesson.ActivityType], lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Index by failure type
|
||||||
|
if lesson.FailureType != "" {
|
||||||
|
li.byFailureType[lesson.FailureType] = append(li.byFailureType[lesson.FailureType], lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Index by pattern
|
||||||
|
if lesson.Pattern != "" {
|
||||||
|
li.byPattern[lesson.Pattern] = append(li.byPattern[lesson.Pattern], lesson)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddLesson adds a single lesson and updates indexes
|
||||||
|
func (li *LessonIndex) AddLesson(lesson *Lesson) {
|
||||||
|
li.mu.Lock()
|
||||||
|
defer li.mu.Unlock()
|
||||||
|
|
||||||
|
li.addLessonLocked(lesson)
|
||||||
|
li.lessonCount = len(li.lessons)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindByTaskType returns all lessons for a task type
|
||||||
|
func (li *LessonIndex) FindByTaskType(taskType string) []*Lesson {
|
||||||
|
li.mu.RLock()
|
||||||
|
defer li.mu.RUnlock()
|
||||||
|
|
||||||
|
if lessons, exists := li.byTaskType[taskType]; exists {
|
||||||
|
// Return a copy to prevent external modifications
|
||||||
|
result := make([]*Lesson, len(lessons))
|
||||||
|
copy(result, lessons)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
return make([]*Lesson, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindByActivityType returns all lessons for an activity type
|
||||||
|
func (li *LessonIndex) FindByActivityType(activityType string) []*Lesson {
|
||||||
|
li.mu.RLock()
|
||||||
|
defer li.mu.RUnlock()
|
||||||
|
|
||||||
|
if lessons, exists := li.byActivityType[activityType]; exists {
|
||||||
|
result := make([]*Lesson, len(lessons))
|
||||||
|
copy(result, lessons)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
return make([]*Lesson, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindByFailureType returns all lessons for a failure type
|
||||||
|
func (li *LessonIndex) FindByFailureType(failureType string) []*Lesson {
|
||||||
|
li.mu.RLock()
|
||||||
|
defer li.mu.RUnlock()
|
||||||
|
|
||||||
|
if lessons, exists := li.byFailureType[failureType]; exists {
|
||||||
|
result := make([]*Lesson, len(lessons))
|
||||||
|
copy(result, lessons)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
return make([]*Lesson, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindByPattern returns all lessons matching a pattern
|
||||||
|
func (li *LessonIndex) FindByPattern(pattern string) []*Lesson {
|
||||||
|
li.mu.RLock()
|
||||||
|
defer li.mu.RUnlock()
|
||||||
|
|
||||||
|
if lessons, exists := li.byPattern[pattern]; exists {
|
||||||
|
result := make([]*Lesson, len(lessons))
|
||||||
|
copy(result, lessons)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
return make([]*Lesson, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FindSimilar returns lessons containing a substring in failure message
|
||||||
|
func (li *LessonIndex) FindSimilar(substr string) []*Lesson {
|
||||||
|
li.mu.RLock()
|
||||||
|
defer li.mu.RUnlock()
|
||||||
|
|
||||||
|
var results []*Lesson
|
||||||
|
substr = strings.ToLower(substr)
|
||||||
|
|
||||||
|
for _, lesson := range li.lessons {
|
||||||
|
if strings.Contains(strings.ToLower(lesson.FailureMsg), substr) {
|
||||||
|
results = append(results, lesson)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLesson returns a specific lesson by ID
|
||||||
|
func (li *LessonIndex) GetLesson(id string) (*Lesson, bool) {
|
||||||
|
li.mu.RLock()
|
||||||
|
defer li.mu.RUnlock()
|
||||||
|
|
||||||
|
lesson, exists := li.lessons[id]
|
||||||
|
return lesson, exists
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStats returns index statistics
|
||||||
|
func (li *LessonIndex) GetStats() map[string]interface{} {
|
||||||
|
li.mu.RLock()
|
||||||
|
defer li.mu.RUnlock()
|
||||||
|
|
||||||
|
return map[string]interface{}{
|
||||||
|
"total_lessons": len(li.lessons),
|
||||||
|
"unique_task_types": len(li.byTaskType),
|
||||||
|
"unique_activity_types": len(li.byActivityType),
|
||||||
|
"unique_failure_types": len(li.byFailureType),
|
||||||
|
"unique_patterns": len(li.byPattern),
|
||||||
|
"last_built_time": li.lastBuiltTime,
|
||||||
|
"build_time": li.buildTime,
|
||||||
|
"source_file": li.sourceFile,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAllLessons returns all lessons (for export/debugging)
|
||||||
|
func (li *LessonIndex) GetAllLessons() []*Lesson {
|
||||||
|
li.mu.RLock()
|
||||||
|
defer li.mu.RUnlock()
|
||||||
|
|
||||||
|
result := make([]*Lesson, 0, len(li.lessons))
|
||||||
|
for _, lesson := range li.lessons {
|
||||||
|
result = append(result, lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// Count returns the total number of indexed lessons
|
||||||
|
func (li *LessonIndex) Count() int {
|
||||||
|
li.mu.RLock()
|
||||||
|
defer li.mu.RUnlock()
|
||||||
|
|
||||||
|
return len(li.lessons)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear clears all indexes
|
||||||
|
func (li *LessonIndex) Clear() {
|
||||||
|
li.mu.Lock()
|
||||||
|
defer li.mu.Unlock()
|
||||||
|
|
||||||
|
li.lessons = make(map[string]*Lesson)
|
||||||
|
li.byTaskType = make(map[string][]*Lesson)
|
||||||
|
li.byActivityType = make(map[string][]*Lesson)
|
||||||
|
li.byFailureType = make(map[string][]*Lesson)
|
||||||
|
li.byPattern = make(map[string][]*Lesson)
|
||||||
|
li.lessonCount = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rebuild rebuilds the index from the source file
|
||||||
|
func (li *LessonIndex) Rebuild() error {
|
||||||
|
if li.sourceFile == "" {
|
||||||
|
return fmt.Errorf("no source file set")
|
||||||
|
}
|
||||||
|
|
||||||
|
return li.BuildFromFile(li.sourceFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryMultiple performs a multi-field query (AND logic)
|
||||||
|
func (li *LessonIndex) QueryMultiple(taskType, activityType, failureType string) []*Lesson {
|
||||||
|
li.mu.RLock()
|
||||||
|
defer li.mu.RUnlock()
|
||||||
|
|
||||||
|
// Start with the most restrictive set
|
||||||
|
var candidates []*Lesson
|
||||||
|
|
||||||
|
// Choose the smallest set to iterate from
|
||||||
|
if taskType != "" && activityType != "" && failureType != "" {
|
||||||
|
// Use the smallest set
|
||||||
|
sizes := []int{
|
||||||
|
len(li.byTaskType[taskType]),
|
||||||
|
len(li.byActivityType[activityType]),
|
||||||
|
len(li.byFailureType[failureType]),
|
||||||
|
}
|
||||||
|
|
||||||
|
minIdx := 0
|
||||||
|
for i, size := range sizes {
|
||||||
|
if size < sizes[minIdx] {
|
||||||
|
minIdx = i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if minIdx == 0 {
|
||||||
|
candidates = li.byTaskType[taskType]
|
||||||
|
} else if minIdx == 1 {
|
||||||
|
candidates = li.byActivityType[activityType]
|
||||||
|
} else {
|
||||||
|
candidates = li.byFailureType[failureType]
|
||||||
|
}
|
||||||
|
} else if taskType != "" && activityType != "" {
|
||||||
|
if len(li.byTaskType[taskType]) <= len(li.byActivityType[activityType]) {
|
||||||
|
candidates = li.byTaskType[taskType]
|
||||||
|
} else {
|
||||||
|
candidates = li.byActivityType[activityType]
|
||||||
|
}
|
||||||
|
} else if taskType != "" {
|
||||||
|
candidates = li.byTaskType[taskType]
|
||||||
|
} else if activityType != "" {
|
||||||
|
candidates = li.byActivityType[activityType]
|
||||||
|
} else if failureType != "" {
|
||||||
|
candidates = li.byFailureType[failureType]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filter candidates
|
||||||
|
var results []*Lesson
|
||||||
|
for _, lesson := range candidates {
|
||||||
|
if taskType != "" && lesson.TaskType != taskType {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if activityType != "" && lesson.ActivityType != activityType {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if failureType != "" && lesson.FailureType != failureType {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
results = append(results, lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetByTimeRange returns lessons seen within a time range
|
||||||
|
func (li *LessonIndex) GetByTimeRange(startTime, endTime time.Time) []*Lesson {
|
||||||
|
li.mu.RLock()
|
||||||
|
defer li.mu.RUnlock()
|
||||||
|
|
||||||
|
var results []*Lesson
|
||||||
|
for _, lesson := range li.lessons {
|
||||||
|
if !lesson.LastSeen.IsZero() &&
|
||||||
|
lesson.LastSeen.After(startTime) &&
|
||||||
|
lesson.LastSeen.Before(endTime) {
|
||||||
|
results = append(results, lesson)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return results
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMostFrequentFailures returns the most frequently seen failures
|
||||||
|
func (li *LessonIndex) GetMostFrequentFailures(limit int) []*Lesson {
|
||||||
|
li.mu.RLock()
|
||||||
|
defer li.mu.RUnlock()
|
||||||
|
|
||||||
|
// Convert to slice
|
||||||
|
var lessons []*Lesson
|
||||||
|
for _, lesson := range li.lessons {
|
||||||
|
lessons = append(lessons, lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simple bubble sort (in practice, use a proper sort)
|
||||||
|
for i := 0; i < len(lessons); i++ {
|
||||||
|
for j := i + 1; j < len(lessons); j++ {
|
||||||
|
if lessons[j].TimesSeen > lessons[i].TimesSeen {
|
||||||
|
lessons[i], lessons[j] = lessons[j], lessons[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if limit > len(lessons) {
|
||||||
|
limit = len(lessons)
|
||||||
|
}
|
||||||
|
|
||||||
|
return lessons[:limit]
|
||||||
|
}
|
||||||
@@ -0,0 +1,467 @@
|
|||||||
|
package indexing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func createTestLessonsFile(t *testing.T, count int) string {
|
||||||
|
file, err := os.CreateTemp("", "lessons-*.jsonl")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
lesson := Lesson{
|
||||||
|
ID: "lesson-" + string(rune(48+i%10)) + "-" + string(rune(48+i/10)),
|
||||||
|
TaskType: []string{"add_feature", "fix_bug", "refactor"}[i%3],
|
||||||
|
ActivityType: []string{"implementer", "judge", "planner"}[i%3],
|
||||||
|
FailureType: []string{"syntax_error", "logic_error", "timeout"}[i%3],
|
||||||
|
FailureMsg: "Error message " + string(rune(48+i%100)),
|
||||||
|
Resolution: "Fix strategy",
|
||||||
|
Pattern: "pattern-" + string(rune(48+i%5)),
|
||||||
|
TimesSeen: i % 10,
|
||||||
|
LastSeen: time.Now().Add(-time.Duration(i) * time.Hour),
|
||||||
|
FirstSeen: time.Now().Add(-time.Duration(i*24) * time.Hour),
|
||||||
|
Metadata: map[string]interface{}{
|
||||||
|
"index": i,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
data, _ := json.Marshal(lesson)
|
||||||
|
file.WriteString(string(data) + "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
return file.Name()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func TestNewLessonIndex(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
assert.NotNil(t, index)
|
||||||
|
assert.Equal(t, 0, index.Count())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildFromFile(t *testing.T) {
|
||||||
|
file := createTestLessonsFile(t, 50)
|
||||||
|
defer os.Remove(file)
|
||||||
|
|
||||||
|
index := NewLessonIndex()
|
||||||
|
err := index.BuildFromFile(file)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Greater(t, index.Count(), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddLesson(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
lesson := &Lesson{
|
||||||
|
ID: "test-1",
|
||||||
|
TaskType: "add_feature",
|
||||||
|
ActivityType: "implementer",
|
||||||
|
FailureType: "syntax_error",
|
||||||
|
FailureMsg: "Missing semicolon",
|
||||||
|
Resolution: "Add semicolon",
|
||||||
|
Pattern: "syntax-missing-semi",
|
||||||
|
TimesSeen: 1,
|
||||||
|
LastSeen: time.Now(),
|
||||||
|
FirstSeen: time.Now(),
|
||||||
|
}
|
||||||
|
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
assert.Equal(t, 1, index.Count())
|
||||||
|
|
||||||
|
retrieved, exists := index.GetLesson("test-1")
|
||||||
|
assert.True(t, exists)
|
||||||
|
assert.Equal(t, "test-1", retrieved.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindByTaskType(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
lessons := []*Lesson{
|
||||||
|
{ID: "1", TaskType: "add_feature", ActivityType: "implementer"},
|
||||||
|
{ID: "2", TaskType: "add_feature", ActivityType: "judge"},
|
||||||
|
{ID: "3", TaskType: "fix_bug", ActivityType: "implementer"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, lesson := range lessons {
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
results := index.FindByTaskType("add_feature")
|
||||||
|
assert.Equal(t, 2, len(results))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindByActivityType(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
lessons := []*Lesson{
|
||||||
|
{ID: "1", TaskType: "add_feature", ActivityType: "implementer"},
|
||||||
|
{ID: "2", TaskType: "add_feature", ActivityType: "implementer"},
|
||||||
|
{ID: "3", TaskType: "fix_bug", ActivityType: "judge"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, lesson := range lessons {
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
results := index.FindByActivityType("implementer")
|
||||||
|
assert.Equal(t, 2, len(results))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindByFailureType(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
lessons := []*Lesson{
|
||||||
|
{ID: "1", FailureType: "syntax_error"},
|
||||||
|
{ID: "2", FailureType: "syntax_error"},
|
||||||
|
{ID: "3", FailureType: "logic_error"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, lesson := range lessons {
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
results := index.FindByFailureType("syntax_error")
|
||||||
|
assert.Equal(t, 2, len(results))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindByPattern(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
lessons := []*Lesson{
|
||||||
|
{ID: "1", Pattern: "pattern-1"},
|
||||||
|
{ID: "2", Pattern: "pattern-2"},
|
||||||
|
{ID: "3", Pattern: "pattern-1"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, lesson := range lessons {
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
results := index.FindByPattern("pattern-1")
|
||||||
|
assert.Equal(t, 2, len(results))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindSimilar(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
lessons := []*Lesson{
|
||||||
|
{ID: "1", FailureMsg: "Syntax error: missing semicolon"},
|
||||||
|
{ID: "2", FailureMsg: "Logic error: wrong condition"},
|
||||||
|
{ID: "3", FailureMsg: "Syntax error: missing bracket"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, lesson := range lessons {
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
results := index.FindSimilar("syntax")
|
||||||
|
assert.Equal(t, 2, len(results))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQueryMultiple(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
lessons := []*Lesson{
|
||||||
|
{ID: "1", TaskType: "add_feature", ActivityType: "implementer", FailureType: "syntax_error"},
|
||||||
|
{ID: "2", TaskType: "add_feature", ActivityType: "judge", FailureType: "syntax_error"},
|
||||||
|
{ID: "3", TaskType: "fix_bug", ActivityType: "implementer", FailureType: "logic_error"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, lesson := range lessons {
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
results := index.QueryMultiple("add_feature", "implementer", "syntax_error")
|
||||||
|
assert.Equal(t, 1, len(results))
|
||||||
|
assert.Equal(t, "1", results[0].ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetByTimeRange(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
lessons := []*Lesson{
|
||||||
|
{ID: "1", LastSeen: now.Add(-2 * time.Hour)},
|
||||||
|
{ID: "2", LastSeen: now.Add(-1 * time.Hour)},
|
||||||
|
{ID: "3", LastSeen: now.Add(-24 * time.Hour)},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, lesson := range lessons {
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Range before any lessons should find 0
|
||||||
|
results := index.GetByTimeRange(now.Add(-48*time.Hour), now.Add(-25*time.Hour))
|
||||||
|
assert.Equal(t, 0, len(results))
|
||||||
|
|
||||||
|
// Range that includes all lessons
|
||||||
|
results = index.GetByTimeRange(now.Add(-25*time.Hour), now)
|
||||||
|
assert.Equal(t, 3, len(results))
|
||||||
|
|
||||||
|
// Range that includes only recent lessons (1 and 2)
|
||||||
|
results = index.GetByTimeRange(now.Add(-3*time.Hour), now)
|
||||||
|
assert.Equal(t, 2, len(results))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetStats(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
lessons := []*Lesson{
|
||||||
|
{ID: "1", TaskType: "add_feature", ActivityType: "implementer"},
|
||||||
|
{ID: "2", TaskType: "add_feature", ActivityType: "judge"},
|
||||||
|
{ID: "3", TaskType: "fix_bug", ActivityType: "implementer"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, lesson := range lessons {
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
stats := index.GetStats()
|
||||||
|
assert.Equal(t, 3, stats["total_lessons"])
|
||||||
|
assert.Equal(t, 2, stats["unique_task_types"])
|
||||||
|
assert.Equal(t, 2, stats["unique_activity_types"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetAllLessons(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
lessons := []*Lesson{
|
||||||
|
{ID: "1"},
|
||||||
|
{ID: "2"},
|
||||||
|
{ID: "3"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, lesson := range lessons {
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
all := index.GetAllLessons()
|
||||||
|
assert.Equal(t, 3, len(all))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClear(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
index.AddLesson(&Lesson{ID: "1"})
|
||||||
|
index.AddLesson(&Lesson{ID: "2"})
|
||||||
|
assert.Equal(t, 2, index.Count())
|
||||||
|
|
||||||
|
index.Clear()
|
||||||
|
assert.Equal(t, 0, index.Count())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetMostFrequentFailures(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
lessons := []*Lesson{
|
||||||
|
{ID: "1", TimesSeen: 5},
|
||||||
|
{ID: "2", TimesSeen: 10},
|
||||||
|
{ID: "3", TimesSeen: 3},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, lesson := range lessons {
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
top := index.GetMostFrequentFailures(2)
|
||||||
|
assert.Equal(t, 2, len(top))
|
||||||
|
assert.Equal(t, 10, top[0].TimesSeen)
|
||||||
|
assert.Equal(t, 5, top[1].TimesSeen)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLookupLatency(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
// Add 1000 lessons
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
lesson := &Lesson{
|
||||||
|
ID: "lesson-" + string(rune(48+i%100)),
|
||||||
|
TaskType: "add_feature",
|
||||||
|
ActivityType: "implementer",
|
||||||
|
FailureType: "syntax_error",
|
||||||
|
}
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Measure lookup time
|
||||||
|
start := time.Now()
|
||||||
|
results := index.FindByTaskType("add_feature")
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
|
||||||
|
assert.Greater(t, len(results), 0)
|
||||||
|
// Should be < 10ms
|
||||||
|
assert.Less(t, elapsed, 10*time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLookupLatencyLarge(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
// Add 10000 lessons
|
||||||
|
for i := 0; i < 10000; i++ {
|
||||||
|
lesson := &Lesson{
|
||||||
|
ID: "lesson-" + string(rune(48+i%100)),
|
||||||
|
TaskType: []string{"add_feature", "fix_bug", "refactor"}[i%3],
|
||||||
|
ActivityType: []string{"implementer", "judge", "planner"}[i%3],
|
||||||
|
FailureType: "syntax_error",
|
||||||
|
}
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Measure lookup time
|
||||||
|
start := time.Now()
|
||||||
|
results := index.FindByActivityType("implementer")
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
|
||||||
|
assert.Greater(t, len(results), 0)
|
||||||
|
// Should be < 10ms even with 10k entries
|
||||||
|
assert.Less(t, elapsed, 10*time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConcurrentQueries(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
// Add lessons
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
lesson := &Lesson{
|
||||||
|
ID: "lesson-" + string(rune(48+i%10)),
|
||||||
|
TaskType: "add_feature",
|
||||||
|
ActivityType: "implementer",
|
||||||
|
FailureType: "syntax_error",
|
||||||
|
}
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run concurrent queries
|
||||||
|
done := make(chan bool, 10)
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
go func() {
|
||||||
|
results := index.FindByTaskType("add_feature")
|
||||||
|
assert.Greater(t, len(results), 0)
|
||||||
|
done <- true
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
<-done
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEmptyQueries(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
results := index.FindByTaskType("nonexistent")
|
||||||
|
assert.Equal(t, 0, len(results))
|
||||||
|
|
||||||
|
results = index.FindByActivityType("nonexistent")
|
||||||
|
assert.Equal(t, 0, len(results))
|
||||||
|
|
||||||
|
results = index.FindByFailureType("nonexistent")
|
||||||
|
assert.Equal(t, 0, len(results))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetLesson(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
lesson := &Lesson{ID: "test-1", TaskType: "add_feature"}
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
|
||||||
|
retrieved, exists := index.GetLesson("test-1")
|
||||||
|
assert.True(t, exists)
|
||||||
|
assert.Equal(t, "test-1", retrieved.ID)
|
||||||
|
|
||||||
|
_, exists = index.GetLesson("nonexistent")
|
||||||
|
assert.False(t, exists)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMultipleIndexes(t *testing.T) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
lesson := &Lesson{
|
||||||
|
ID: "1",
|
||||||
|
TaskType: "add_feature",
|
||||||
|
ActivityType: "implementer",
|
||||||
|
FailureType: "syntax_error",
|
||||||
|
Pattern: "pattern-1",
|
||||||
|
}
|
||||||
|
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
|
||||||
|
// Should be findable by all indexes
|
||||||
|
assert.Equal(t, 1, len(index.FindByTaskType("add_feature")))
|
||||||
|
assert.Equal(t, 1, len(index.FindByActivityType("implementer")))
|
||||||
|
assert.Equal(t, 1, len(index.FindByFailureType("syntax_error")))
|
||||||
|
assert.Equal(t, 1, len(index.FindByPattern("pattern-1")))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRebuild(t *testing.T) {
|
||||||
|
file := createTestLessonsFile(t, 50)
|
||||||
|
defer os.Remove(file)
|
||||||
|
|
||||||
|
index := NewLessonIndex()
|
||||||
|
_ = index.BuildFromFile(file)
|
||||||
|
count1 := index.Count()
|
||||||
|
|
||||||
|
_ = index.Rebuild()
|
||||||
|
count2 := index.Count()
|
||||||
|
|
||||||
|
assert.Equal(t, count1, count2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkAddLesson(b *testing.B) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
lesson := &Lesson{
|
||||||
|
ID: "lesson-" + string(rune(48+i%100)),
|
||||||
|
TaskType: "add_feature",
|
||||||
|
ActivityType: "implementer",
|
||||||
|
FailureType: "syntax_error",
|
||||||
|
}
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkFindByTaskType(b *testing.B) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
// Populate index
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
lesson := &Lesson{
|
||||||
|
ID: "lesson-" + string(rune(48+i%100)),
|
||||||
|
TaskType: "add_feature",
|
||||||
|
ActivityType: "implementer",
|
||||||
|
}
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
index.FindByTaskType("add_feature")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkFindByActivityType(b *testing.B) {
|
||||||
|
index := NewLessonIndex()
|
||||||
|
|
||||||
|
// Populate index
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
lesson := &Lesson{
|
||||||
|
ID: "lesson-" + string(rune(48+i%100)),
|
||||||
|
TaskType: "add_feature",
|
||||||
|
ActivityType: "implementer",
|
||||||
|
}
|
||||||
|
index.AddLesson(lesson)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
index.FindByActivityType("implementer")
|
||||||
|
}
|
||||||
|
}
|
||||||
+2
-2
@@ -7,8 +7,8 @@
|
|||||||
| T2.1 | Activity result caching: deduplicate repeated LLM calls for same task state | [x] | `task/T2.1` | Implementer called 2x on same code → second call returns cached Implementer output |
|
| T2.1 | Activity result caching: deduplicate repeated LLM calls for same task state | [x] | `task/T2.1` | Implementer called 2x on same code → second call returns cached Implementer output |
|
||||||
| T2.2 | Parallel task dispatch: multiple T0.x tasks execute truly concurrently (not sequential) | [x] | `task/T2.2` | 9 tasks complete in ~1/9 total time (wall-clock speedup measured) |
|
| T2.2 | Parallel task dispatch: multiple T0.x tasks execute truly concurrently (not sequential) | [x] | `task/T2.2` | 9 tasks complete in ~1/9 total time (wall-clock speedup measured) |
|
||||||
| T2.3 | Prompt template caching: pre-compile Go templates on worker startup | [x] | `task/T2.3` | Template render latency < 100ms (vs parse+render each time) |
|
| T2.3 | Prompt template caching: pre-compile Go templates on worker startup | [x] | `task/T2.3` | Template render latency < 100ms (vs parse+render each time) |
|
||||||
| T2.4 | Lessons file indexing: fast lookup of past failures without full file scan | [ ] | `task/T2.4` | Query lessons by task type → return in < 10ms for 1000s of entries |
|
| T2.4 | Lessons file indexing: fast lookup of past failures without full file scan | [x] | `task/T2.4` | Query lessons by task type → return in < 10ms for 1000s of entries |
|
||||||
| T2.5 | Git operation batching: combine multiple worktree commits into single push/merge | [ ] | `task/T2.5` | N tasks → 1 push (vs N pushes), measured via git ref-log |
|
| T2.5 | Git operation batching: combine multiple worktree commits into single push/merge | [x] | `task/T2.5` | N tasks → 1 push (vs N pushes), measured via git ref-log |
|
||||||
| T2.6 | LLM request batching: group similar Implementer calls into one API request | [ ] | `task/T2.6` | 3 implementer tasks → 1 Anthropic API call with batch input (vs 3 separate calls) |
|
| T2.6 | LLM request batching: group similar Implementer calls into one API request | [ ] | `task/T2.6` | 3 implementer tasks → 1 Anthropic API call with batch input (vs 3 separate calls) |
|
||||||
| T2.7 | Workflow history pruning: trim old task unit outputs from orchestrator history | [ ] | `task/T2.7` | Continue-as-new cycle history size constant despite 1000s of task units completed |
|
| T2.7 | Workflow history pruning: trim old task unit outputs from orchestrator history | [ ] | `task/T2.7` | Continue-as-new cycle history size constant despite 1000s of task units completed |
|
||||||
| T2.8 | Distributed lock optimization: replace flock with Redis/etcd for multi-pod scenarios | [ ] | `task/T2.8` | 5 concurrent orchestrators on different pods share FS safely via distributed lock |
|
| T2.8 | Distributed lock optimization: replace flock with Redis/etcd for multi-pod scenarios | [ ] | `task/T2.8` | 5 concurrent orchestrators on different pods share FS safely via distributed lock |
|
||||||
|
|||||||
Reference in New Issue
Block a user