| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "math" |
| 5 | "sync" |
| 6 | "sync/atomic" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestLiveCompactRatioUsesOneSnapshotPerDecision(t *testing.T) { |
| 11 | var bits atomic.Uint64 |
| 12 | bits.Store(math.Float64bits(.85)) |
| 13 | reads := 0 |
| 14 | a := &Agent{agentConfig: agentConfig{contextWindow: 100_000, compactRatio: .85, compactRatioSource: func() float64 { reads++; return math.Float64frombits(bits.Load()) }}} |
| 15 | if got := a.compactTrigger(); got != 85_000 || reads != 1 { |
| 16 | t.Fatalf("trigger=%d reads=%d", got, reads) |
| 17 | } |
| 18 | bits.Store(math.Float64bits(.80)) |
| 19 | if got := a.compactTrigger(); got != 80_000 || reads != 2 { |
| 20 | t.Fatalf("updated trigger=%d reads=%d", got, reads) |
| 21 | } |
| 22 | if a.CompactRatio() != .80 { |
| 23 | t.Fatal("status threshold disagrees") |
| 24 | } |
| 25 | bits.Store(math.Float64bits(0)) |
| 26 | if a.CompactRatio() != defaultCompactRatio || a.compactTrigger() != 80_000 { |
| 27 | t.Fatal("unset live ratio lost default behavior") |
| 28 | } |
| 29 | fixed := &Agent{agentConfig: agentConfig{compactRatio: .7}} |
| 30 | if fixed.CompactRatio() != .7 { |
| 31 | t.Fatal("standalone construction value changed") |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestTaskCompactRatioSnapshotsNewChildren(t *testing.T) { |
| 36 | var bits atomic.Uint64 |
| 37 | bits.Store(math.Float64bits(.85)) |
| 38 | source := func() float64 { return math.Float64frombits(bits.Load()) } |
| 39 | task := NewTaskToolWithOptions(TaskToolOptions{CompactRatio: .85, CompactRatioSource: source}) |
| 40 | before := task.subagentOptions(t.Context(), 0, nil, 100_000, 1, "", nil) |
| 41 | bits.Store(math.Float64bits(.8)) |
| 42 | after := task.subagentOptions(t.Context(), 0, nil, 100_000, 1, "", nil) |
| 43 | if before.CompactRatio != .85 || after.CompactRatio != .8 || before.CompactRatioSource != nil || after.CompactRatioSource != nil { |
| 44 | t.Fatal("children did not freeze their creation-time threshold") |
| 45 | } |
| 46 | var wg sync.WaitGroup |
| 47 | wg.Go(func() { |
| 48 | for range 1000 { |
| 49 | bits.Store(math.Float64bits(.7)) |
| 50 | bits.Store(math.Float64bits(.8)) |
| 51 | } |
| 52 | }) |
| 53 | for range 1000 { |
| 54 | opts := task.subagentOptions(t.Context(), 0, nil, 100_000, 1, "", nil) |
| 55 | if opts.CompactRatio != .7 && opts.CompactRatio != .8 { |
| 56 | t.Fatalf("torn child threshold %v", opts.CompactRatio) |
| 57 | } |
| 58 | } |
| 59 | wg.Wait() |
| 60 | } |
| 61 |