|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { resolveSessionContextBudgetPolicy } from "./context-budget-policy.js"; |
| 3 | +import type { SessionContextBudgetStatus } from "./types.js"; |
| 4 | + |
| 5 | +function makeStatus(patch: Partial<SessionContextBudgetStatus> = {}): SessionContextBudgetStatus { |
| 6 | + return { |
| 7 | + schemaVersion: 1, |
| 8 | + source: "pre-prompt-estimate", |
| 9 | + updatedAt: 1, |
| 10 | + provider: "anthropic", |
| 11 | + model: "claude-opus-4-6", |
| 12 | + route: "fits", |
| 13 | + shouldCompact: false, |
| 14 | + estimatedPromptTokens: 100, |
| 15 | + contextTokenBudget: 1_000, |
| 16 | + promptBudgetBeforeReserve: 900, |
| 17 | + reserveTokens: 100, |
| 18 | + effectiveReserveTokens: 100, |
| 19 | + remainingPromptBudgetTokens: 800, |
| 20 | + overflowTokens: 0, |
| 21 | + toolResultReducibleChars: 0, |
| 22 | + messageCount: 1, |
| 23 | + unwindowedMessageCount: 1, |
| 24 | + ...patch, |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +describe("resolveSessionContextBudgetPolicy", () => { |
| 29 | + it("classifies low estimated prompt usage as safe", () => { |
| 30 | + expect( |
| 31 | + resolveSessionContextBudgetPolicy( |
| 32 | + makeStatus({ |
| 33 | + estimatedPromptTokens: 125_000, |
| 34 | + contextTokenBudget: 1_000_000, |
| 35 | + promptBudgetBeforeReserve: 900_000, |
| 36 | + remainingPromptBudgetTokens: 775_000, |
| 37 | + }), |
| 38 | + ), |
| 39 | + ).toMatchObject({ |
| 40 | + pressure: "safe", |
| 41 | + contextBudgetPct: 13, |
| 42 | + promptBudgetPct: 14, |
| 43 | + remainingPromptBudgetTokens: 775_000, |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it("classifies reserve-budget pressure before overflow", () => { |
| 48 | + expect( |
| 49 | + resolveSessionContextBudgetPolicy( |
| 50 | + makeStatus({ |
| 51 | + estimatedPromptTokens: 640_000, |
| 52 | + contextTokenBudget: 1_000_000, |
| 53 | + promptBudgetBeforeReserve: 900_000, |
| 54 | + remainingPromptBudgetTokens: 260_000, |
| 55 | + }), |
| 56 | + ), |
| 57 | + ).toMatchObject({ |
| 58 | + pressure: "watch", |
| 59 | + contextBudgetPct: 64, |
| 60 | + promptBudgetPct: 71, |
| 61 | + }); |
| 62 | + |
| 63 | + expect( |
| 64 | + resolveSessionContextBudgetPolicy( |
| 65 | + makeStatus({ |
| 66 | + estimatedPromptTokens: 780_000, |
| 67 | + contextTokenBudget: 1_000_000, |
| 68 | + promptBudgetBeforeReserve: 900_000, |
| 69 | + remainingPromptBudgetTokens: 120_000, |
| 70 | + }), |
| 71 | + )?.pressure, |
| 72 | + ).toBe("pressure"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("classifies non-fitting precheck routes as overflow risk", () => { |
| 76 | + expect( |
| 77 | + resolveSessionContextBudgetPolicy( |
| 78 | + makeStatus({ |
| 79 | + route: "compact_then_truncate", |
| 80 | + shouldCompact: true, |
| 81 | + estimatedPromptTokens: 920_000, |
| 82 | + contextTokenBudget: 1_000_000, |
| 83 | + promptBudgetBeforeReserve: 900_000, |
| 84 | + remainingPromptBudgetTokens: 0, |
| 85 | + overflowTokens: 20_000, |
| 86 | + }), |
| 87 | + )?.pressure, |
| 88 | + ).toBe("overflow-risk"); |
| 89 | + }); |
| 90 | +}); |
0 commit comments