|
1 | 1 | import type { AcpMessage } from "@posthog/shared"; |
2 | 2 | import { describe, expect, it } from "vitest"; |
3 | | -import { createContextUsageTracker, extractContextUsage } from "./contextUsage"; |
| 3 | +import { |
| 4 | + createContextUsageTracker, |
| 5 | + DEFAULT_STALE_COSTLY_THRESHOLD, |
| 6 | + extractContextUsage, |
| 7 | + extractLastActivityAt, |
| 8 | + shouldWarnStaleCostlyConversation, |
| 9 | +} from "./contextUsage"; |
4 | 10 |
|
5 | 11 | function usageUpdateEvent(used: number, size: number): AcpMessage { |
6 | 12 | return { |
@@ -132,3 +138,116 @@ describe("createContextUsageTracker", () => { |
132 | 138 | expect(tracker.update(events)).toEqual(extractContextUsage(events)); |
133 | 139 | }); |
134 | 140 | }); |
| 141 | + |
| 142 | +describe("shouldWarnStaleCostlyConversation", () => { |
| 143 | + const now = 1_000_000_000; |
| 144 | + const threshold = { tokens: 40_000, staleMs: 5 * 60 * 1000 }; |
| 145 | + |
| 146 | + it.each([ |
| 147 | + { |
| 148 | + name: "large + stale → warn", |
| 149 | + usedTokens: 50_000, |
| 150 | + idleMs: 10 * 60 * 1000, |
| 151 | + expected: true, |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "large + fresh → no warn", |
| 155 | + usedTokens: 50_000, |
| 156 | + idleMs: 60 * 1000, |
| 157 | + expected: false, |
| 158 | + }, |
| 159 | + { |
| 160 | + name: "small + stale → no warn", |
| 161 | + usedTokens: 10_000, |
| 162 | + idleMs: 10 * 60 * 1000, |
| 163 | + expected: false, |
| 164 | + }, |
| 165 | + { |
| 166 | + name: "small + fresh → no warn", |
| 167 | + usedTokens: 10_000, |
| 168 | + idleMs: 60 * 1000, |
| 169 | + expected: false, |
| 170 | + }, |
| 171 | + { |
| 172 | + name: "exactly at both thresholds → warn", |
| 173 | + usedTokens: 40_000, |
| 174 | + idleMs: 5 * 60 * 1000, |
| 175 | + expected: true, |
| 176 | + }, |
| 177 | + { |
| 178 | + name: "one token below the size threshold → no warn", |
| 179 | + usedTokens: 39_999, |
| 180 | + idleMs: 10 * 60 * 1000, |
| 181 | + expected: false, |
| 182 | + }, |
| 183 | + { |
| 184 | + name: "one ms below the stale threshold → no warn", |
| 185 | + usedTokens: 50_000, |
| 186 | + idleMs: 5 * 60 * 1000 - 1, |
| 187 | + expected: false, |
| 188 | + }, |
| 189 | + ])("$name", ({ usedTokens, idleMs, expected }) => { |
| 190 | + expect( |
| 191 | + shouldWarnStaleCostlyConversation({ |
| 192 | + usedTokens, |
| 193 | + lastActivityAt: now - idleMs, |
| 194 | + now, |
| 195 | + threshold, |
| 196 | + }), |
| 197 | + ).toBe(expected); |
| 198 | + }); |
| 199 | + |
| 200 | + it("never warns without a last-activity timestamp", () => { |
| 201 | + expect( |
| 202 | + shouldWarnStaleCostlyConversation({ |
| 203 | + usedTokens: 1_000_000, |
| 204 | + lastActivityAt: null, |
| 205 | + now, |
| 206 | + threshold, |
| 207 | + }), |
| 208 | + ).toBe(false); |
| 209 | + }); |
| 210 | + |
| 211 | + it("treats a future timestamp (clock skew) as fresh", () => { |
| 212 | + expect( |
| 213 | + shouldWarnStaleCostlyConversation({ |
| 214 | + usedTokens: 50_000, |
| 215 | + lastActivityAt: now + 60_000, |
| 216 | + now, |
| 217 | + threshold, |
| 218 | + }), |
| 219 | + ).toBe(false); |
| 220 | + }); |
| 221 | + |
| 222 | + it("falls back to DEFAULT_STALE_COSTLY_THRESHOLD when none is given", () => { |
| 223 | + expect( |
| 224 | + shouldWarnStaleCostlyConversation({ |
| 225 | + usedTokens: DEFAULT_STALE_COSTLY_THRESHOLD.tokens, |
| 226 | + lastActivityAt: now - DEFAULT_STALE_COSTLY_THRESHOLD.staleMs, |
| 227 | + now, |
| 228 | + }), |
| 229 | + ).toBe(true); |
| 230 | + }); |
| 231 | +}); |
| 232 | + |
| 233 | +describe("extractLastActivityAt", () => { |
| 234 | + it("returns null for an empty event list", () => { |
| 235 | + expect(extractLastActivityAt([])).toBeNull(); |
| 236 | + }); |
| 237 | + |
| 238 | + it("returns the ts of the most recent event", () => { |
| 239 | + const events: AcpMessage[] = [ |
| 240 | + { ...agentChunkEvent(), ts: 10 }, |
| 241 | + { ...usageUpdateEvent(50_000, 200_000), ts: 20 }, |
| 242 | + ]; |
| 243 | + expect(extractLastActivityAt(events)).toBe(20); |
| 244 | + }); |
| 245 | + |
| 246 | + it("returns the maximum ts even when events are out of order", () => { |
| 247 | + const events: AcpMessage[] = [ |
| 248 | + { ...usageUpdateEvent(50_000, 200_000), ts: 30 }, |
| 249 | + { ...agentChunkEvent(), ts: 10 }, |
| 250 | + ]; |
| 251 | + expect(extractLastActivityAt(events)).toBe(30); |
| 252 | + }); |
| 253 | +}); |
0 commit comments