// Layer 4 apart from the tool: what a `limit` argument is allowed to do, and what happens when // a read is bigger than the answer is allowed to be. Truncation that is not reported is the // same bug as no cap at all — the agent reasons on a slice it believes is the whole table. import { describe, expect, test } from 'bun:test'; import type { QueryRows } from './query-limits'; import { capQueryRows, DEFAULT_QUERY_ROWS, QUERY_LIMITS, resolveQueryLimits } from './query-limits'; const rowsOf = (count: number, cell: unknown = 1): QueryRows => ({ columns: ['n'], rows: Array.from({ length: count }, () => [cell]), guards: ['txn:read-only'], }); describe('resolveQueryLimits treats `limit` as a request, never a permission', () => { test('no argument means the default, well under the ceiling', () => { expect(resolveQueryLimits(undefined).maxRows).toBe(DEFAULT_QUERY_ROWS); expect(DEFAULT_QUERY_ROWS).toBeLessThan(QUERY_LIMITS.maxRows); }); test('a bigger ask is clamped to the ceiling, not honoured', () => { expect(resolveQueryLimits(10_000).maxRows).toBe(QUERY_LIMITS.maxRows); expect(resolveQueryLimits(Number.MAX_SAFE_INTEGER).maxRows).toBe(QUERY_LIMITS.maxRows); }); test('zero, negative, fractional and non-numeric asks never widen anything', () => { expect(resolveQueryLimits(0).maxRows).toBe(1); expect(resolveQueryLimits(-5).maxRows).toBe(1); expect(resolveQueryLimits(7.9).maxRows).toBe(7); expect(resolveQueryLimits('1000000').maxRows).toBe(DEFAULT_QUERY_ROWS); expect(resolveQueryLimits(Number.POSITIVE_INFINITY).maxRows).toBe(DEFAULT_QUERY_ROWS); }); test('the byte and timeout ceilings are not negotiable from an argument', () => { const limits = resolveQueryLimits(1); expect(limits.maxBytes).toBe(QUERY_LIMITS.maxBytes); expect(limits.timeoutMs).toBe(QUERY_LIMITS.timeoutMs); }); }); describe('capQueryRows reports every cut it makes', () => { const limits = resolveQueryLimits(10); test('a result inside both ceilings is untruncated and keeps the host guards', () => { const result = capQueryRows(rowsOf(3), limits); expect(result.rowCount).toBe(3); expect(result.truncated).toBe(false); expect(result.truncatedBy).toBeNull(); expect(result.guards).toEqual(['txn:read-only', 'cap:10 rows', 'cap:262144 bytes']); }); test('the n+1 row the host fetched is dropped and reported, never returned', () => { const result = capQueryRows(rowsOf(11), limits); expect(result.rowCount).toBe(10); expect(result.truncated).toBe(true); expect(result.truncatedBy).toBe('rows'); }); test('the byte ceiling bites before the row ceiling and says so', () => { const fat = { ...rowsOf(10, 'x'.repeat(200)), guards: [] }; const tight = { ...limits, maxBytes: 700 }; const result = capQueryRows(fat, tight); expect(result.rowCount).toBeGreaterThan(0); expect(result.rowCount).toBeLessThan(10); expect(result.truncatedBy).toBe('bytes'); expect(result.bytes).toBeLessThanOrEqual(700); }); test('one row larger than the whole ceiling yields no rows rather than blowing the cap', () => { const huge = { ...rowsOf(1, 'x'.repeat(5_000)), guards: [] }; const result = capQueryRows(huge, { ...limits, maxBytes: 512 }); expect(result.rowCount).toBe(0); expect(result.truncated).toBe(true); expect(result.truncatedBy).toBe('bytes'); expect(result.bytes).toBeLessThanOrEqual(512); }); test('`bytes` counts what is returned and never under-counts it', () => { const result = capQueryRows(rowsOf(4), limits); const actual = new TextEncoder().encode(JSON.stringify(result.rows)).length; // Every row is charged its own separator, so the tally is conservative by exactly the row // count. A cap that rounds the other way is a cap that can be exceeded. expect(result.bytes).toBeGreaterThanOrEqual(actual); expect(result.bytes).toBeLessThanOrEqual(actual + result.rowCount); }); test('columns survive an empty result, so the shape is readable with no rows', () => { const result = capQueryRows({ columns: ['id', 'title'], rows: [], guards: [] }, limits); expect(result.columns).toEqual(['id', 'title']); expect(result.truncated).toBe(false); }); }); // The rows are a DRIVER's: `db.query` runs an agent-authored statement and the host hands back // whatever the driver decoded. `JSON.stringify` throws on a bigint — an `int8` column on a driver // that maps it faithfully — and on a cycle, so the cap that exists to protect the model's context // was the thing that raised, two frames from the tool that owes the agent an answer. describe('a row the driver decoded into something JSON cannot hold', () => { const limits = resolveQueryLimits(10); test('is dropped and reported as the byte ceiling, never raised', () => { const source: QueryRows = { columns: ['n'], rows: [[1], [10n], [2]], guards: [], }; const result = capQueryRows(source, limits); // The rows before it survive; the unserialisable one and everything after it do not, which is // exactly what the byte ceiling already promises — and the answer still serialises. expect(result.rowCount).toBe(1); expect(result.truncated).toBe(true); expect(result.truncatedBy).toBe('bytes'); expect(() => JSON.stringify(result)).not.toThrow(); }); }); /** * `capQueryRows` takes its ceilings from the caller, and `QueryLimits` is a PUBLIC type — so the * one path `resolveQueryLimits` cannot defend is a limits object built by hand. `slice(0, NaN)` is * `[]` and `overRows` is `length > NaN`, which is false: the agent is handed zero rows with * `truncated: false`, an empty table reported as the complete answer to its question. The byte * ceiling fails the same way — `bytes + size > NaN` is false for every row, so the 256 KiB that * exists to protect the model's context stops applying at all. */ describe('capQueryRows: a ceiling that is not a ceiling', () => { const limits = resolveQueryLimits(10); for (const maxRows of [Number.NaN, Number.POSITIVE_INFINITY, 2.5, 0, -1]) { test(`maxRows: ${String(maxRows)} is refused, never zero rows called complete`, () => { expect(() => capQueryRows(rowsOf(3), { ...limits, maxRows })).toThrow(/X_INVARIANT/); }); } for (const maxBytes of [Number.NaN, Number.POSITIVE_INFINITY, 2.5, -1]) { test(`maxBytes: ${String(maxBytes)} is refused, never an uncapped answer`, () => { expect(() => capQueryRows(rowsOf(3), { ...limits, maxBytes })).toThrow(/X_INVARIANT/); }); } test('the ceilings resolveQueryLimits builds are always usable ones', () => { expect(() => capQueryRows(rowsOf(3), resolveQueryLimits(Number.NaN))).not.toThrow(); expect(() => capQueryRows(rowsOf(3), resolveQueryLimits('nonsense'))).not.toThrow(); }); });