|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/voocel/agentcore" |
| 7 | +) |
| 8 | + |
| 9 | +func TestHashSystemBlocksStableAcrossCallOrder(t *testing.T) { |
| 10 | + t.Parallel() |
| 11 | + |
| 12 | + a := []agentcore.SystemBlock{{Text: "identity"}, {Text: "instructions"}} |
| 13 | + b := []agentcore.SystemBlock{{Text: "identity"}, {Text: "instructions"}} |
| 14 | + if hashSystemBlocks(a) != hashSystemBlocks(b) { |
| 15 | + t.Fatalf("identical blocks must hash to the same value") |
| 16 | + } |
| 17 | + |
| 18 | + c := []agentcore.SystemBlock{{Text: "identity"}, {Text: "instructions v2"}} |
| 19 | + if hashSystemBlocks(a) == hashSystemBlocks(c) { |
| 20 | + t.Fatalf("differing text must change the hash") |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func TestHashSystemBlocksIgnoresCacheControl(t *testing.T) { |
| 25 | + t.Parallel() |
| 26 | + |
| 27 | + a := []agentcore.SystemBlock{{Text: "identity", CacheControl: "ephemeral"}} |
| 28 | + b := []agentcore.SystemBlock{{Text: "identity"}} |
| 29 | + if hashSystemBlocks(a) != hashSystemBlocks(b) { |
| 30 | + t.Fatalf("cache_control metadata must not influence the fingerprint") |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestHashToolsSortsByName(t *testing.T) { |
| 35 | + t.Parallel() |
| 36 | + |
| 37 | + forward := []agentcore.Tool{&stubTool{name: "read", desc: "read files"}, &stubTool{name: "bash", desc: "run shell"}} |
| 38 | + reverse := []agentcore.Tool{&stubTool{name: "bash", desc: "run shell"}, &stubTool{name: "read", desc: "read files"}} |
| 39 | + if hashTools(forward) != hashTools(reverse) { |
| 40 | + t.Fatalf("tool ordering must not affect the fingerprint") |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestDetectCacheBreakNilWhenNoBaseline(t *testing.T) { |
| 45 | + t.Parallel() |
| 46 | + |
| 47 | + info := detectCacheBreak(cacheSnapshot{Valid: false}, cacheSnapshot{Valid: true, CacheReadTokens: 5000}) |
| 48 | + if info != nil { |
| 49 | + t.Fatalf("no baseline should suppress detection, got %+v", info) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestDetectCacheBreakIgnoresSmallDrops(t *testing.T) { |
| 54 | + t.Parallel() |
| 55 | + |
| 56 | + // 4% drop, well below breakDropFraction. |
| 57 | + prev := cacheSnapshot{Valid: true, CacheReadTokens: 100000} |
| 58 | + curr := cacheSnapshot{Valid: true, CacheReadTokens: 96000, SystemHash: prev.SystemHash} |
| 59 | + if info := detectCacheBreak(prev, curr); info != nil { |
| 60 | + t.Fatalf("small drop should not trigger, got %+v", info) |
| 61 | + } |
| 62 | + |
| 63 | + // 10% drop but absolute delta below 2000. |
| 64 | + prev = cacheSnapshot{Valid: true, CacheReadTokens: 10000} |
| 65 | + curr = cacheSnapshot{Valid: true, CacheReadTokens: 9000} |
| 66 | + if info := detectCacheBreak(prev, curr); info != nil { |
| 67 | + t.Fatalf("absolute drop below threshold should not trigger, got %+v", info) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestDetectCacheBreakReportsSystemChange(t *testing.T) { |
| 72 | + t.Parallel() |
| 73 | + |
| 74 | + prev := cacheSnapshot{Valid: true, SystemHash: 1, ToolsHash: 2, CacheReadTokens: 50000} |
| 75 | + curr := cacheSnapshot{Valid: true, SystemHash: 9, ToolsHash: 2, CacheReadTokens: 0} |
| 76 | + info := detectCacheBreak(prev, curr) |
| 77 | + if info == nil { |
| 78 | + t.Fatal("expected a cache break to be reported") |
| 79 | + } |
| 80 | + if !info.SystemChanged || info.ToolsChanged { |
| 81 | + t.Fatalf("expected system-only change, got %+v", info) |
| 82 | + } |
| 83 | + if info.DropAbsolute != 50000 { |
| 84 | + t.Fatalf("drop_absolute = %d, want 50000", info.DropAbsolute) |
| 85 | + } |
| 86 | + if info.Note == "" { |
| 87 | + t.Fatal("note should be populated so logs are self-describing") |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestDetectCacheBreakReportsUnknownWhenHashesMatch(t *testing.T) { |
| 92 | + t.Parallel() |
| 93 | + |
| 94 | + prev := cacheSnapshot{Valid: true, SystemHash: 1, ToolsHash: 2, CacheReadTokens: 80000} |
| 95 | + curr := cacheSnapshot{Valid: true, SystemHash: 1, ToolsHash: 2, CacheReadTokens: 0} |
| 96 | + info := detectCacheBreak(prev, curr) |
| 97 | + if info == nil { |
| 98 | + t.Fatal("expected a break to be reported even without hash diffs") |
| 99 | + } |
| 100 | + if info.SystemChanged || info.ToolsChanged { |
| 101 | + t.Fatalf("expected neither hash to flip, got %+v", info) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestCompactionEventInvalidatesCacheBaseline(t *testing.T) { |
| 106 | + t.Parallel() |
| 107 | + |
| 108 | + s := &Session{cacheSnap: cacheSnapshot{Valid: true, CacheReadTokens: 50000, SystemHash: 1}} |
| 109 | + |
| 110 | + // An "unchanged" compaction must not reset the baseline — no rewrite happened. |
| 111 | + s.emit(SessionEvent{Type: SEAutoCompactionEnd, CompactionChanged: false}) |
| 112 | + if !s.cacheSnap.Valid || s.cacheSnap.CacheReadTokens != 50000 { |
| 113 | + t.Fatalf("unchanged compaction should preserve baseline, got %+v", s.cacheSnap) |
| 114 | + } |
| 115 | + |
| 116 | + // A real compaction rewrites the prefix; the baseline must be dropped so |
| 117 | + // the next turn's expected cache_read drop is not misreported. |
| 118 | + s.emit(SessionEvent{Type: SEAutoCompactionEnd, CompactionChanged: true}) |
| 119 | + if s.cacheSnap.Valid || s.cacheSnap.CacheReadTokens != 0 { |
| 120 | + t.Fatalf("changed compaction should invalidate baseline, got %+v", s.cacheSnap) |
| 121 | + } |
| 122 | +} |
0 commit comments