Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 3 additions & 22 deletions pkg/stats/statvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

// StatVar accumulates a stream of float64 observations and computes descriptive
// statistics: count, sum, min, max, mean, variance, standard deviation, and
// median. Mean and variance are maintained incrementally using Welford's online
// statistics: count, min, max, mean, sample variance, sample standard deviation,
// and median. Mean and variance are maintained incrementally using Welford's online
// algorithm, which avoids catastrophic cancellation when values are large or
// tightly clustered. All observed values are also stored so that an exact
// median can be returned on demand.
Expand All @@ -21,14 +21,13 @@ type StatVar struct {
count int
mean float64 // running mean (Welford)
m2 float64 // running sum of squared deviations from the mean (Welford)
sum float64
min float64
max float64
values []float64 // stored for median computation
}

// Add records a new observation. Finite values are recommended; NaN and ±Inf
// will propagate through sum, mean, and variance, but Min and Max may not update
// will propagate through mean and variance, but Min and Max may not update
// correctly for NaN inputs because IEEE 754 comparisons with NaN always return
// false. In practice, observations come from time.Duration, token counts, and
// cost values, which are always finite.
Expand All @@ -45,7 +44,6 @@ func (s *StatVar) Add(v float64) {
}
}
s.count++
s.sum += v
s.values = append(s.values, v)
// Welford's one-pass algorithm: numerically stable incremental mean and M2.
delta := v - s.mean
Expand All @@ -57,9 +55,6 @@ func (s *StatVar) Add(v float64) {
// Count returns the number of observations added so far.
func (s *StatVar) Count() int { return s.count }

// Sum returns the arithmetic sum of all observations, or 0 if none.
func (s *StatVar) Sum() float64 { return s.sum }

// Min returns the smallest observed value, or 0 if no observations have been added.
func (s *StatVar) Min() float64 {
if s.count == 0 {
Expand All @@ -84,15 +79,6 @@ func (s *StatVar) Mean() float64 {
return s.mean
}

// Variance returns the population variance (divides by N).
// Returns 0 when fewer than two observations are present.
func (s *StatVar) Variance() float64 {
if s.count < 2 {
return 0
}
return s.m2 / float64(s.count)
}

// SampleVariance returns the sample variance with Bessel's correction (divides
// by N−1), giving an unbiased estimator when the observations are a random
// sample from a larger population. Returns 0 when fewer than two observations
Expand All @@ -104,11 +90,6 @@ func (s *StatVar) SampleVariance() float64 {
return s.m2 / float64(s.count-1)
}

// StdDev returns the population standard deviation (sqrt of Variance).
func (s *StatVar) StdDev() float64 {
return math.Sqrt(s.Variance())
}

// SampleStdDev returns the sample standard deviation with Bessel's correction
// (sqrt of SampleVariance).
func (s *StatVar) SampleStdDev() float64 {
Expand Down
19 changes: 0 additions & 19 deletions pkg/stats/statvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ import (
func TestStatVar_Empty(t *testing.T) {
var s StatVar
assert.Equal(t, 0, s.Count(), "empty: count should be 0")
assert.InDelta(t, 0.0, s.Sum(), 1e-9, "empty: sum should be 0")
assert.InDelta(t, 0.0, s.Min(), 1e-9, "empty: min should be 0")
assert.InDelta(t, 0.0, s.Max(), 1e-9, "empty: max should be 0")
assert.InDelta(t, 0.0, s.Mean(), 1e-9, "empty: mean should be 0")
assert.InDelta(t, 0.0, s.Variance(), 1e-9, "empty: variance should be 0")
assert.InDelta(t, 0.0, s.SampleVariance(), 1e-9, "empty: sample variance should be 0")
assert.InDelta(t, 0.0, s.StdDev(), 1e-9, "empty: stddev should be 0")
assert.InDelta(t, 0.0, s.SampleStdDev(), 1e-9, "empty: sample stddev should be 0")
assert.InDelta(t, 0.0, s.Median(), 1e-9, "empty: median should be 0")
}
Expand All @@ -29,13 +26,10 @@ func TestStatVar_SingleObservation(t *testing.T) {
s.Add(42.0)

assert.Equal(t, 1, s.Count(), "single: count should be 1")
assert.InDelta(t, 42.0, s.Sum(), 1e-9, "single: sum should be 42")
assert.InDelta(t, 42.0, s.Min(), 1e-9, "single: min should be 42")
assert.InDelta(t, 42.0, s.Max(), 1e-9, "single: max should be 42")
assert.InDelta(t, 42.0, s.Mean(), 1e-9, "single: mean should be 42")
assert.InDelta(t, 0.0, s.Variance(), 1e-9, "single: variance should be 0 (needs >=2)")
assert.InDelta(t, 0.0, s.SampleVariance(), 1e-9, "single: sample variance should be 0 (needs >=2)")
assert.InDelta(t, 0.0, s.StdDev(), 1e-9, "single: stddev should be 0")
assert.InDelta(t, 42.0, s.Median(), 1e-9, "single: median should be 42")
}

Expand All @@ -45,15 +39,11 @@ func TestStatVar_TwoObservations(t *testing.T) {
s.Add(20.0)

assert.Equal(t, 2, s.Count(), "two: count should be 2")
assert.InDelta(t, 30.0, s.Sum(), 1e-9, "two: sum should be 30")
assert.InDelta(t, 10.0, s.Min(), 1e-9, "two: min should be 10")
assert.InDelta(t, 20.0, s.Max(), 1e-9, "two: max should be 20")
assert.InDelta(t, 15.0, s.Mean(), 1e-9, "two: mean should be 15")
// Population variance: ((10-15)² + (20-15)²) / 2 = 50/2 = 25
assert.InDelta(t, 25.0, s.Variance(), 1e-9, "two: population variance should be 25")
// Sample variance: 50 / (2-1) = 50
assert.InDelta(t, 50.0, s.SampleVariance(), 1e-9, "two: sample variance should be 50")
assert.InDelta(t, 5.0, s.StdDev(), 1e-9, "two: population stddev should be 5")
assert.InDelta(t, math.Sqrt(50), s.SampleStdDev(), 1e-9, "two: sample stddev should be sqrt(50)")
// Median of [10, 20] → (10+20)/2 = 15
assert.InDelta(t, 15.0, s.Median(), 1e-9, "two: median should be 15")
Expand All @@ -67,7 +57,6 @@ func TestStatVar_OddCount(t *testing.T) {
}

assert.Equal(t, 3, s.Count(), "odd: count should be 3")
assert.InDelta(t, 9.0, s.Sum(), 1e-9, "odd: sum should be 9")
assert.InDelta(t, 1.0, s.Min(), 1e-9, "odd: min should be 1")
assert.InDelta(t, 5.0, s.Max(), 1e-9, "odd: max should be 5")
assert.InDelta(t, 3.0, s.Mean(), 1e-9, "odd: mean should be 3")
Expand All @@ -82,7 +71,6 @@ func TestStatVar_EvenCount(t *testing.T) {
}

assert.Equal(t, 4, s.Count(), "even: count should be 4")
assert.InDelta(t, 20.0, s.Sum(), 1e-9, "even: sum should be 20")
assert.InDelta(t, 2.0, s.Min(), 1e-9, "even: min should be 2")
assert.InDelta(t, 8.0, s.Max(), 1e-9, "even: max should be 8")
assert.InDelta(t, 5.0, s.Mean(), 1e-9, "even: mean should be 5")
Expand All @@ -102,8 +90,6 @@ func TestStatVar_KnownVariance(t *testing.T) {

require.Equal(t, 8, s.Count(), "known: count should be 8")
assert.InDelta(t, 5.0, s.Mean(), 1e-9, "known: mean should be 5")
assert.InDelta(t, 4.0, s.Variance(), 1e-9, "known: population variance should be 4")
assert.InDelta(t, 2.0, s.StdDev(), 1e-9, "known: population stddev should be 2")
assert.InDelta(t, 32.0/7.0, s.SampleVariance(), 1e-9, "known: sample variance should be 32/7")
assert.InDelta(t, math.Sqrt(32.0/7.0), s.SampleStdDev(), 1e-9, "known: sample stddev should be sqrt(32/7)")
// Median of [2,4,4,4,5,5,7,9] → (4+5)/2 = 4.5
Expand All @@ -120,9 +106,6 @@ func TestStatVar_NumericalStability(t *testing.T) {
}
// 500 observations at 1e9 and 500 at 1e9+1 → mean = 1e9 + 0.5
assert.InDelta(t, 1e9+0.5, s.Mean(), 1e-6, "stability: mean should be ~1e9+0.5")
// Population variance = 0.25
assert.InDelta(t, 0.25, s.Variance(), 1e-6, "stability: population variance should be ~0.25")
assert.InDelta(t, 0.5, s.StdDev(), 1e-6, "stability: population stddev should be ~0.5")
}

func TestStatVar_MedianDoesNotMutateState(t *testing.T) {
Expand All @@ -148,7 +131,5 @@ func TestStatVar_AllIdentical(t *testing.T) {
s.Add(7.0)
}
assert.InDelta(t, 7.0, s.Mean(), 1e-9, "identical: mean should be 7")
assert.InDelta(t, 0.0, s.Variance(), 1e-9, "identical: variance should be 0")
assert.InDelta(t, 0.0, s.StdDev(), 1e-9, "identical: stddev should be 0")
assert.InDelta(t, 7.0, s.Median(), 1e-9, "identical: median should be 7")
}
6 changes: 0 additions & 6 deletions pkg/workflow/action_pins.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ func getActionPin(repo string) string {
return actionpins.FormatReference(repo, pins[0].SHA, pins[0].Version)
}

// getActionPins returns all loaded action pins (sorted by version descending).
// Package-private wrapper used by tests in this package.
func getActionPins() []ActionPin {
return actionpins.GetActionPins()
}

// getCachedActionPinFromResolver returns the pinned action reference for repo,
// preferring dynamic resolution via resolver over the embedded pins.
// For use within pkg/workflow when only a resolver is available (no WorkflowData).
Expand Down
13 changes: 7 additions & 6 deletions pkg/workflow/action_pins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (
"strings"
"testing"

actionpins "github.com/github/gh-aw/pkg/actionpins"
"github.com/github/gh-aw/pkg/testutil"
)

// TestActionPinsExist verifies that all action pinning entries exist
func TestActionPinsExist(t *testing.T) {
// Read action pins from JSON file instead of hardcoded list
actionPins := getActionPins()
actionPins := actionpins.GetActionPins()

// Verify we have at least some pins loaded
if len(actionPins) == 0 {
Expand Down Expand Up @@ -46,7 +47,7 @@ func TestActionPinsExist(t *testing.T) {
// TestGetActionPinReturnsValidSHA tests that getActionPin returns valid SHA references
func TestGetActionPinReturnsValidSHA(t *testing.T) {
// Generate test cases dynamically from action pins JSON
actionPins := getActionPins()
actionPins := actionpins.GetActionPins()

if len(actionPins) == 0 {
t.Fatal("No action pins loaded from JSON file")
Expand Down Expand Up @@ -296,7 +297,7 @@ func TestApplyActionPinToStep(t *testing.T) {

// TestGetActionPinsSorting tests that getActionPins returns sorted action pins
func TestGetActionPinsSorting(t *testing.T) {
pins := getActionPins()
pins := actionpins.GetActionPins()

// Dynamically derive the expected count from the JSON file to avoid
// hardcoding a number that breaks when new pins are added or when
Expand All @@ -313,7 +314,7 @@ func TestGetActionPinsSorting(t *testing.T) {

// Verify we got all the pins from the JSON (catches parsing bugs)
if len(pins) != expectedCount {
t.Errorf("getActionPins() returned %d pins, expected %d (from action_pins.json)", len(pins), expectedCount)
t.Errorf("actionpins.GetActionPins() returned %d pins, expected %d (from action_pins.json)", len(pins), expectedCount)
}

// Verify they are sorted by version (descending) then by repository name (ascending)
Expand Down Expand Up @@ -861,13 +862,13 @@ func TestActionPinsCaching(t *testing.T) {
// Note: In production, this is handled automatically by sync.Once

// First call - should load and cache
pins1 := getActionPins()
pins1 := actionpins.GetActionPins()
if len(pins1) == 0 {
t.Fatal("No action pins loaded on first call")
}

// Second call - should return cached data (same slice reference)
pins2 := getActionPins()
pins2 := actionpins.GetActionPins()
if len(pins2) == 0 {
t.Fatal("No action pins loaded on second call")
}
Expand Down