// internal/worker/worker.go
package worker

import "sync"

// Aggregate launches one goroutine per item. Every goroutine mutates the
// same captured local `total` with no synchronization, and the early return
// abandons goroutines that were already started.
func Aggregate(items []int, sink chan<- int) int {
	var wg sync.WaitGroup
	total := 0
	for _, n := range items {
		wg.Add(1)
		go func() {
			defer wg.Done()
			total += n // concurrent mutation of a captured local
			sink <- n  // blocks forever if nobody drains sink
		}()
		if n < 0 {
			return total // abandons started goroutines; wg never waited
		}
	}
	wg.Wait()
	return total
}
