|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { |
| 3 | + beginRequestAttempt, |
| 4 | + finishRequestAttempt, |
| 5 | + noteAttemptSend, |
| 6 | +} from "../src/server/request-log"; |
| 7 | + |
| 8 | +/** |
| 9 | + * The token-estimate context-window cap (codex-router PR #140) in the request log: |
| 10 | + * the ESTIMATE field is bounded by the routed model's context window, while the combined |
| 11 | + * inputTokens field keeps its max(reported, estimate) behavior. Provider-reported positive |
| 12 | + * counts are never reduced by the cap. |
| 13 | + */ |
| 14 | +describe("request log token-estimate context-window cap (codex-router PR #140)", () => { |
| 15 | + const DEEPSEEK_WINDOW = 128_000; // KIRO_MODEL_CONTEXT_WINDOWS["deepseek-3.2"] |
| 16 | + const CURSOR_CLAUDE_WINDOW = 200_000; // inferCursorContextWindow("claude-4.6-opus-high") |
| 17 | + |
| 18 | + test("the estimate is capped at the kiro model's context window", () => { |
| 19 | + const attempt = beginRequestAttempt(1, "kiro", "deepseek-3.2", "kiro"); |
| 20 | + noteAttemptSend(attempt, 200_000); |
| 21 | + finishRequestAttempt(attempt, 200, 10); |
| 22 | + expect(attempt.inputTokenEstimate).toBe(DEEPSEEK_WINDOW); |
| 23 | + expect(attempt.usage).toEqual({ inputTokens: DEEPSEEK_WINDOW, outputTokens: 0, estimated: true }); |
| 24 | + }); |
| 25 | + |
| 26 | + test("below-window estimates pass through unchanged", () => { |
| 27 | + const attempt = beginRequestAttempt(1, "kiro", "deepseek-3.2", "kiro"); |
| 28 | + noteAttemptSend(attempt, 100_000); |
| 29 | + finishRequestAttempt(attempt, 200, 10); |
| 30 | + expect(attempt.inputTokenEstimate).toBe(100_000); |
| 31 | + expect(attempt.usage?.inputTokens).toBe(100_000); |
| 32 | + }); |
| 33 | + |
| 34 | + test("a positive provider-reported input count is never reduced by the cap", () => { |
| 35 | + const attempt = beginRequestAttempt(1, "kiro", "deepseek-3.2", "kiro"); |
| 36 | + noteAttemptSend(attempt, 200_000); // estimate would exceed the window |
| 37 | + finishRequestAttempt(attempt, 200, 10, { inputTokens: 150_000, outputTokens: 500 }); |
| 38 | + // combined = max(reported 150k, capped estimate 128k) = 150k: the reported count wins. |
| 39 | + expect(attempt.usage?.inputTokens).toBe(150_000); |
| 40 | + }); |
| 41 | + |
| 42 | + test("a missing report substitutes the capped estimate (the ESTIMATE field)", () => { |
| 43 | + const attempt = beginRequestAttempt(1, "kiro", "deepseek-3.2", "kiro"); |
| 44 | + noteAttemptSend(attempt, 200_000); |
| 45 | + finishRequestAttempt(attempt, 200, 10); |
| 46 | + // No usage at all: the fallback inputTokens IS the capped estimate — never above the window. |
| 47 | + expect(attempt.usage?.inputTokens).toBe(DEEPSEEK_WINDOW); |
| 48 | + expect(attempt.usage?.estimated).toBe(true); |
| 49 | + expect(attempt.usageStatus).toBe("estimated"); |
| 50 | + }); |
| 51 | + |
| 52 | + test("the ESTIMATE field vs the combined inputTokens field distinction is preserved", () => { |
| 53 | + // Reported zero: combined max(0, capped estimate) equals the capped estimate. |
| 54 | + const zero = beginRequestAttempt(1, "kiro", "deepseek-3.2", "kiro"); |
| 55 | + noteAttemptSend(zero, 200_000); |
| 56 | + finishRequestAttempt(zero, 200, 10, { inputTokens: 0, outputTokens: 5 }); |
| 57 | + expect(zero.usage?.inputTokens).toBe(DEEPSEEK_WINDOW); |
| 58 | + |
| 59 | + // Reported positive above the capped estimate: combined keeps the reported count. |
| 60 | + const positive = beginRequestAttempt(1, "kiro", "deepseek-3.2", "kiro"); |
| 61 | + noteAttemptSend(positive, 200_000); |
| 62 | + finishRequestAttempt(positive, 200, 10, { inputTokens: 200_000, outputTokens: 5 }); |
| 63 | + expect(positive.usage?.inputTokens).toBe(200_000); |
| 64 | + |
| 65 | + // Reported positive below the capped estimate: combined takes the capped estimate. |
| 66 | + const middle = beginRequestAttempt(1, "kiro", "deepseek-3.2", "kiro"); |
| 67 | + noteAttemptSend(middle, 100_000); |
| 68 | + finishRequestAttempt(middle, 200, 10, { inputTokens: 50_000, outputTokens: 5 }); |
| 69 | + expect(middle.usage?.inputTokens).toBe(100_000); |
| 70 | + }); |
| 71 | + |
| 72 | + test("cursor adapter uses the cursor window inference", () => { |
| 73 | + const attempt = beginRequestAttempt(1, "cursor", "claude-4.6-opus-high", "cursor"); |
| 74 | + noteAttemptSend(attempt, 500_000); |
| 75 | + finishRequestAttempt(attempt, 200, 10); |
| 76 | + expect(attempt.inputTokenEstimate).toBe(CURSOR_CLAUDE_WINDOW); |
| 77 | + }); |
| 78 | + |
| 79 | + test("unknown adapters stay uncapped (a window is never invented)", () => { |
| 80 | + const attempt = beginRequestAttempt(1, "anthropic", "claude-sonnet-4-6", "anthropic"); |
| 81 | + noteAttemptSend(attempt, 500_000); |
| 82 | + finishRequestAttempt(attempt, 200, 10); |
| 83 | + expect(attempt.inputTokenEstimate).toBe(500_000); |
| 84 | + }); |
| 85 | + |
| 86 | + test("without a window the existing max(reported, estimate) behavior is unchanged", () => { |
| 87 | + const attempt = beginRequestAttempt(1, "anthropic", "claude-sonnet-4-6", "anthropic"); |
| 88 | + noteAttemptSend(attempt, 500_000); |
| 89 | + finishRequestAttempt(attempt, 200, 10, { inputTokens: 100_000, outputTokens: 5 }); |
| 90 | + expect(attempt.usage?.inputTokens).toBe(500_000); |
| 91 | + }); |
| 92 | + |
| 93 | + test("kiro auto has no fixed window and is never guessed", () => { |
| 94 | + const attempt = beginRequestAttempt(1, "kiro", "kiro-auto", "kiro"); |
| 95 | + noteAttemptSend(attempt, 500_000); |
| 96 | + finishRequestAttempt(attempt, 200, 10); |
| 97 | + expect(attempt.inputTokenEstimate).toBe(500_000); |
| 98 | + }); |
| 99 | +}); |
0 commit comments