Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
297 changes: 297 additions & 0 deletions src/__tests__/context-hardening.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import {
buildContextMessages,
estimateTokens,
truncateToolResult,
EXACT_TOKENIZE_MAX_CHARS,
MASK_WORK_BUDGET,
MASK_CHUNK_CHARS,
MASK_WS_RUN_MAX,
MAX_ENCODE_CHUNK_CHARS,
MAX_TOOL_RESULT_SIZE,
summarizeTurns,
} from "../agent/context.js";
Expand All @@ -26,6 +31,27 @@ import {
noToolResponse,
} from "./mocks.js";

// Delegating spy on the tokenizer: records the exact text of every encode() call; behaviour is unchanged.
const encodeCalls = vi.hoisted(() => [] as string[]);
vi.mock("js-tiktoken", async (importOriginal) => {
const actual = await importOriginal<typeof import("js-tiktoken")>();
return {
...actual,
getEncoding: (name: Parameters<typeof actual.getEncoding>[0]) => {
const inner = actual.getEncoding(name);
const spy = Object.create(inner);
spy.encode = (text: string, allowed?: string[] | "all", disallowed?: string[] | "all") => {
encodeCalls.push(text);
return inner.encode(text, allowed, disallowed);
};
return spy;
},
};
});
// Unspied tokenizer used as the full-string reference in tests.
const referenceTokenizer = (await vi.importActual<typeof import("js-tiktoken")>("js-tiktoken")).getEncoding("cl100k_base");
const fullCount = (text: string) => referenceTokenizer.encode(text, [], []).length;

// ─── Helper: Create a mock AgentTurn ───────────────────────────

function makeTurn(overrides?: Partial<AgentTurn>): AgentTurn {
Expand Down Expand Up @@ -69,6 +95,277 @@ describe("estimateTokens", () => {
});
});

describe("estimateTokens large-input safety (budgeted mask-and-encode)", () => {
// Deterministic PRNG so adversarial inputs are reproducible.
let seed = 4242;
const rnd = () => {
seed = (seed + 0x6d2b79f5) | 0;
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
t ^= t + Math.imul(t ^ (t >>> 7), 61 | t);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
const R = (n: number) => Math.floor(rnd() * n);
const pick = (alphabet: string, n: number) => {
let s = "";
for (let i = 0; i < n; i++) s += alphabet[R(alphabet.length)];
return s;
};
const words = (alphabet: string, w: number, sep: string, n: number) => {
let s = "";
while (s.length < n) s += pick(alphabet, w) + sep;
return s.slice(0, n);
};
const LOWER = "abcdefghijklmnopqrstuvwxyz";
const UPPER = LOWER.toUpperCase();
const DIGITS = "0123456789";
const HEX = DIGITS + "abcdef";
const ALNUM = LOWER + UPPER + DIGITS;
const B64 = ALNUM + "+/";
const PRINTABLE = Array.from({ length: 94 }, (_, i) => String.fromCharCode(33 + i)).join("");
const SYMBOLS = "!@#$%^&*()_+-=[]{}|;:,.<>?/~`";
const PROSE = "The quick brown fox jumps over the lazy dog. Pack my box with five dozen liquor jugs! ";
const prose = (n: number) => PROSE.repeat(Math.ceil(n / PROSE.length)).slice(0, n);
const utf8 = (s: string) => Buffer.byteLength(s, "utf8");
const N = 8_000; // big enough for the budgeted path, small enough for a full-string reference

const run = (text: string) => {
encodeCalls.length = 0;
const estimate = estimateTokens(text);
const calls = encodeCalls.slice();
encodeCalls.length = 0;
return { estimate, calls };
};

describe("small inputs", () => {
it("handles empty and short inputs exactly", () => {
expect(estimateTokens("")).toBe(0);
expect(estimateTokens("hi")).toBe(1);
expect(estimateTokens("hello world")).toBe(3);
});

it("tokenizes exactly up to the threshold (with the len/4 floor)", () => {
const text = "word ".repeat(EXACT_TOKENIZE_MAX_CHARS / 5);
expect(estimateTokens(text)).toBe(Math.max(fullCount(text), Math.ceil(text.length / 4)));
});

it("treats special-token strings as ordinary text instead of falling back to len/3.5", () => {
const text = pick(B64, 900) + " <|endoftext|>";
const { estimate } = run(text);
expect(estimate).toBeGreaterThanOrEqual(fullCount(text));
expect(estimate).toBeGreaterThan(Math.ceil(text.length / 3.5)); // old fallback undercounted this
});

it("special-token strings in prose are tokenized as text (no throw, no byte-length fallback)", () => {
const small = prose(700) + " <|endoftext|>";
const reference = fullCount(small);
const { estimate } = run(small);
expect(estimate).toBeGreaterThanOrEqual(reference);
expect(estimate).toBeLessThanOrEqual(Math.ceil(reference * 1.5)); // a byte fallback would be ~4x
});
});

describe("bounded work for inputs up to 500KB (deterministic: inspects tokenizer calls, not wall-clock)", () => {
const BIG = 500 * 1024;
const inputs: Array<[string, () => string]> = [
["unbroken 'x'", () => "x".repeat(BIG)],
["prose", () => prose(BIG)],
["lowercase gibberish words", () => words(LOWER, 5, " ", BIG)],
["24-char alnum words", () => words(ALNUM, 24, " ", BIG)],
["base64 unbroken", () => pick(B64, BIG)],
["CJK words (8 chars)", () => words("日本語漢字テスト", 8, " ", BIG)],
["emoji words", () => words("😀🎉🔥", 3, " ", BIG)],
["single digits '1 2 3'", () => words(DIGITS, 1, " ", BIG)],
["128-char tab/space runs", () => ("a" + " \t".repeat(MASK_WS_RUN_MAX / 2)).repeat(Math.ceil(BIG / 129))],
["oversized (129-char) whitespace runs", () => ("a" + " ".repeat(MASK_WS_RUN_MAX + 1)).repeat(Math.ceil(BIG / 130))],
["long words + oversized runs (placeholder-heavy)", () => (pick(ALNUM, 30) + " ".repeat(200)).repeat(Math.ceil(BIG / 230))],
];
it.each(inputs)("%s", (_name, make) => {
const text = make();
const { estimate, calls } = run(text);
const total = calls.reduce((sum, c) => sum + c.length, 0);
expect(total).toBeLessThanOrEqual(MASK_WORK_BUDGET);
expect(Math.max(0, ...calls.map((c) => c.length))).toBeLessThanOrEqual(MAX_ENCODE_CHUNK_CHARS);
expect(calls.length).toBeLessThanOrEqual(Math.ceil(MASK_WORK_BUDGET / MASK_CHUNK_CHARS) + 2);
expect(estimate).toBeGreaterThanOrEqual(Math.ceil(text.length / 4));
});

it("charges UTF-8 bytes for everything past the work budget", () => {
const text = prose(BIG);
const { estimate } = run(text);
expect(estimate).toBeGreaterThanOrEqual(text.length - MASK_WORK_BUDGET);
});

it("charges UTF-8 bytes for an unbroken run without tokenizing it", () => {
const text = "x".repeat(BIG);
const { estimate, calls } = run(text);
expect(estimate).toBeGreaterThanOrEqual(utf8(text));
expect(calls.join("")).not.toContain("xxxx");
});
});

describe("never underestimates adversarial inputs (reference: full-string tokenization)", () => {
const check = (text: string, maxFactor = 3) => {
expect(text.length).toBeGreaterThan(EXACT_TOKENIZE_MAX_CHARS);
const reference = fullCount(text);
const { estimate } = run(text);
expect(estimate).toBeGreaterThanOrEqual(reference);
// The len/4 floor can legitimately exceed the factor for whitespace-heavy text (few real tokens).
expect(estimate).toBeLessThanOrEqual(Math.max(Math.ceil(reference * maxFactor), Math.ceil(text.length / 4)));
};

it("the old sample-window bypass: gibberish with a non-plain '1.' every 500 chars", () => {
const chars = words(LOWER, 5, " ", N).split("");
for (let at = 100; at < N - 4; at += 500) for (let k = 0; k < 4; k++) chars[at + k] = " 1. "[k];
check(chars.join(""));
});

it("lowercase gibberish", () => check(words(LOWER, 5, " ", N)));
it("uppercase gibberish", () => check(words(UPPER, 4, " ", N)));
it("mixed-case gibberish", () => check(words(LOWER + UPPER, 6, " ", N)));
it("base64 words", () => check(words(B64, 20, " ", N)));
it("hex words", () => check(words(HEX, 8, " ", N)));
it("symbol words", () => check(words(SYMBOLS, 3, " ", N)));
it("single digits '1 2 3'", () => check(words(DIGITS, 1, " ", N)));
it("single letters separated by newlines", () => check(words(LOWER, 1, "\n", N)));

it.each([
["space+tab alternating", " \t \t \t \t"],
["newline/space alternating", "\n \n \n \n"],
["double space", " "],
["CRLF", "\r\n"],
["tab/space mix", "\t \t \t "],
["repeated newlines", "\n\n\n\n\n\n"],
["60 spaces", " ".repeat(60)],
["128-char alternating run (max exact)", " \t".repeat(MASK_WS_RUN_MAX / 2)],
])("whitespace separator: %s", (_name, sep) => check(words(LOWER, 3, sep, N), 4));

it("oversized whitespace runs are charged bytes (sound, deliberately conservative)", () => {
for (const sep of [" ".repeat(MASK_WS_RUN_MAX + 1), " \t".repeat(150), "\n".repeat(400)]) {
const text = words(LOWER, 3, sep, N);
const { estimate } = run(text);
expect(estimate).toBeGreaterThanOrEqual(fullCount(text));
expect(estimate).toBeLessThanOrEqual(utf8(text) + 64); // never more than the byte bound
}
});

it.each([["unbroken printable ASCII", PRINTABLE], ["unbroken base64", B64], ["unbroken hex", HEX], ["unbroken symbols", SYMBOLS]])(
"%s",
(_name, alphabet) => check(pick(alphabet, 3_000)),
);

it("long words (30-200 chars)", () => {
let text = "";
while (text.length < N) text += pick(ALNUM, 30 + R(170)) + " ";
check(text);
});

it("dense Unicode: rare CJK words, mixed CJK unbroken, emoji", () => {
check(words("漢", 6, " ", N));
check(pick("日本語漢字テスト中文한국어", 1_200));
check(words("😀🎉🔥", 3, " ", N));
check("😀".repeat(800));
check(words("é ü ñ ☃ ✓ ½", 4, " ", N));
});

it("special-token strings embedded in dense text", () => {
let text = "";
while (text.length < N) text += pick(ALNUM, 20) + " <|endoftext|> " + pick(PRINTABLE, 10) + " <|fim_prefix|>\n";
check(text);
});

it("special-token strings in large prose stay accurate (no byte-length fallback)", () => {
let text = "";
while (text.length < N) text += prose(180) + " <|endoftext|> ";
const reference = fullCount(text);
const { estimate } = run(text);
expect(estimate).toBeGreaterThanOrEqual(reference);
expect(estimate).toBeLessThanOrEqual(Math.ceil(reference * 1.3));
});

it("random mixed documents (seeded property test)", () => {
const frag = (): string => {
switch (R(10)) {
case 0: return pick(LOWER, 1 + R(10));
case 1: return pick(ALNUM, 1 + R(40));
case 2: return pick(PRINTABLE, 1 + R(14));
case 3: return pick("日本語漢字テスト中文한국어", 1 + R(10));
case 4: return "😀🎉🔥".slice(0, 2 * (1 + R(3)));
case 5: return pick(PRINTABLE, 30 + R(200));
case 6: return "<|endoftext|>";
case 7: return ["The", "quick", "don't", "Hello,", "world.", "(see", "foo)"][R(7)];
case 8: return "漢".repeat(1 + R(20));
default: return pick("𐀀éü☃́", 1 + R(6));
}
};
const ws = [" ", " ", "\n", "\t", " \t ", "\r\n", "\n\n", "\n ", " ", " ".repeat(130)];
for (let d = 0; d < 60; d++) {
let text = "";
const target = 1_100 + R(2_400);
while (text.length < target) text += frag() + ws[R(ws.length)];
expect(run(text).estimate).toBeGreaterThanOrEqual(fullCount(text));
}
});
});

describe("chunk boundaries", () => {
const patterns = ["foo!\nbar ", "a b\t", "x1,\n\n y ", "word\r\n", "(see foo) ", "$ \t$\n"];
it.each(patterns)("pattern %j never undercounts at any chunk cut", (pattern) => {
for (const offset of [0, 1, 2, 3, 5, 7, 11]) {
const text = "z".repeat(offset) + " " + pattern.repeat(Math.ceil((3.5 * MASK_CHUNK_CHARS) / pattern.length));
const { estimate, calls } = run(text);
expect(calls.length).toBeGreaterThanOrEqual(3); // several cuts were actually exercised
expect(estimate).toBeGreaterThanOrEqual(fullCount(text));
}
});

it("cuts only before whitespace, so no word is split between chunks", () => {
const text = words(ALNUM, 7, " ", 5 * MASK_CHUNK_CHARS);
const { calls } = run(text);
expect(calls.length).toBeGreaterThanOrEqual(5);
for (const chunk of calls.slice(1)) expect(chunk[0]).toBe(" ");
for (const chunk of calls.slice(0, -1)) expect(chunk.length).toBeGreaterThanOrEqual(MASK_CHUNK_CHARS);
});
});

describe("ordinary text stays accurate", () => {
it("prose is within 1.15x of the full-string count", () => {
const text = prose(60_000);
const reference = fullCount(text);
const { estimate } = run(text);
expect(estimate).toBeGreaterThanOrEqual(reference);
expect(estimate).toBeLessThanOrEqual(Math.ceil(reference * 1.15));
});

it("TypeScript source is within 1.8x of the full-string count", () => {
const snippet = [
'export function buildContextMessages(systemPrompt: string, recentTurns: AgentTurn[]): ChatMessage[] {',
' const budget = options?.budget ?? DEFAULT_TOKEN_BUDGET;',
' const messages: ChatMessage[] = [{ role: "system", content: systemPrompt }];',
' for (const turn of recentTurns) {',
' if (turn.input) messages.push({ role: "user", content: `[${turn.inputSource}] ${turn.input}` });',
' }',
' return messages;',
'}',
'',
].join("\n");
const text = snippet.repeat(Math.ceil(30_000 / snippet.length));
const reference = fullCount(text);
const { estimate } = run(text);
expect(estimate).toBeGreaterThanOrEqual(reference);
expect(estimate).toBeLessThanOrEqual(Math.ceil(reference * 1.8));
});

it("markdown with lists, code fences and links is within 1.8x", () => {
const snippet = "## Setup\n\n- Install with `pnpm install --frozen-lockfile`\n- See [the docs](https://example.com/docs/setup) for details.\n\n```ts\nconst x = await run({ retries: 3 });\n```\n\n";
const text = snippet.repeat(Math.ceil(30_000 / snippet.length));
const reference = fullCount(text);
const { estimate } = run(text);
expect(estimate).toBeGreaterThanOrEqual(reference);
expect(estimate).toBeLessThanOrEqual(Math.ceil(reference * 1.8));
});
});
});

// ─── truncateToolResult ────────────────────────────────────────

describe("truncateToolResult", () => {
Expand Down
Loading