confluence_web/agent/tests/limits.test.ts
Artur Mukhamadiev 1d7b867986 agent: enforce the configured call budgets instead of hardcoded 100/50
The bridge counted tool and model calls against fixed 100 and 50 caps, so
CONFLUENCE_WEB_MAX_CONFLUENCE_CALLS / CONFLUENCE_WEB_MAX_MODEL_CALLS above the
defaults were cut short inside the container, and the model cap failed the run
outright. The backend now passes both values into the container at start, the
supervisor forwards them to the bridge, and LIMITS reads them with the same
1-1000 validation as the backend, falling back to 100/50 when absent or
invalid. Add a test that loads LIMITS in fresh processes with good and bad
values for both budgets and the deadline.
2026-09-15 14:39:24 +03:00

36 lines
1.8 KiB
TypeScript

import { test, describe } from 'node:test';
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import path from 'node:path';
// LIMITS is computed at module load, so each case imports types.ts in a fresh process.
function limitsWith(env: Record<string, string>): { model: number; tool: number; deadline: number } {
const script = "import('./types.ts').then(m => console.log(JSON.stringify({ model: m.LIMITS.MAX_MODEL_CALLS, tool: m.LIMITS.MAX_TOOL_CALLS, deadline: m.LIMITS.MAX_DEADLINE_MS })))";
const res = spawnSync(process.execPath, ['--import', 'tsx', '-e', script], {
cwd: path.resolve(import.meta.dirname, '..'), env: { PATH: process.env.PATH, ...env }, encoding: 'utf8',
});
assert.equal(res.status, 0, res.stderr);
return JSON.parse(res.stdout.trim());
}
describe('LIMITS from the container environment', () => {
test('defaults without environment', () => {
assert.deepEqual(limitsWith({}), { model: 50, tool: 100, deadline: 900_000 });
});
test('accepts in-range integers set by the backend', () => {
const l = limitsWith({ CONFLUENCE_WEB_MAX_MODEL_CALLS: '120', CONFLUENCE_WEB_MAX_CONFLUENCE_CALLS: '1000', CONFLUENCE_WEB_MAX_DEADLINE_SECONDS: '1200' });
assert.deepEqual(l, { model: 120, tool: 1000, deadline: 1_200_000 });
});
test('falls back to defaults on out-of-range or malformed values', () => {
for (const bad of ['0', '1001', '-5', '1.5', 'many', '']) {
const l = limitsWith({ CONFLUENCE_WEB_MAX_MODEL_CALLS: bad, CONFLUENCE_WEB_MAX_CONFLUENCE_CALLS: bad });
assert.deepEqual(l, { model: 50, tool: 100, deadline: 900_000 }, `budget ${JSON.stringify(bad)}`);
}
for (const bad of ['59', '3601', '-5', '1.5', 'many', '']) {
assert.equal(limitsWith({ CONFLUENCE_WEB_MAX_DEADLINE_SECONDS: bad }).deadline, 900_000, `deadline ${JSON.stringify(bad)}`);
}
});
});