Files
bare-operating-system/packages/bare-os-coreutils/test/agent-trim-ctx.test.mjs
T
Raven Scott f919d0b085
Release rolling / release (push) Successful in 8m34s
Context L
2026-07-31 19:17:19 -04:00

36 lines
1.1 KiB
JavaScript

import test from 'brittle'
import { readFileSync } from 'node:fs'
import vm from 'node:vm'
const STATE_SRC = readFileSync(new URL('../lib/agent-state.js', import.meta.url), 'utf8')
function loadTrimHelpers() {
const sandbox = { console, TextEncoder, TextDecoder }
vm.createContext(sandbox)
vm.runInContext(
STATE_SRC +
'\n;this.__exports = { bareAgentTrimMessages, bareAgentTrimMessagesForCtx, bareAgentEstimateTokens }',
sandbox
)
return sandbox.__exports
}
test('trim for ctx shrinks system when tools consume budget', async (t) => {
const { bareAgentTrimMessagesForCtx, bareAgentEstimateTokens } = loadTrimHelpers()
const tools = [{ name: 'x', description: 'y'.repeat(8000), parameters: {} }]
const msgs = [
{ role: 'system', content: 'S'.repeat(20_000) },
{ role: 'user', content: 'hello' }
]
const out = bareAgentTrimMessagesForCtx(msgs, 4096, {
tools,
reserveCompletion: 512
})
t.is(out.length, 2)
t.ok(String(/** @type {any} */ (out[0]).content).length < 20_000)
t.ok(
bareAgentEstimateTokens(out) + bareAgentEstimateTokens(tools) + 512 <=
4096 + 200
)
})