Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
99 lines
3.1 KiB
TypeScript
99 lines
3.1 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import type { ModelMessage } from 'ai';
|
|
import { pruneFirstModelMessage, pruneLastModelMessage, pruneUntilFitsCompaction } from './compactionPruner.ts';
|
|
|
|
test('pruneLastModelMessage removes trailing user and assistant pair', () => {
|
|
const messages: ModelMessage[] = [
|
|
{ role: 'user', content: 'old' },
|
|
{
|
|
role: 'assistant',
|
|
content: [{
|
|
type: 'tool-call',
|
|
toolCallId: 'call-1',
|
|
toolName: 'terminal_execute',
|
|
input: { command: 'pwd' },
|
|
}],
|
|
},
|
|
{
|
|
role: 'tool',
|
|
content: [{
|
|
type: 'tool-result',
|
|
toolCallId: 'call-1',
|
|
toolName: 'terminal_execute',
|
|
output: { type: 'text', value: '/tmp' },
|
|
}],
|
|
},
|
|
{ role: 'user', content: 'recent' },
|
|
{ role: 'assistant', content: 'acknowledged' },
|
|
];
|
|
const pruned = pruneLastModelMessage(messages);
|
|
assert.equal(pruned.length, 3);
|
|
assert.equal(pruned[0]?.content, 'old');
|
|
assert.equal(pruned.at(-1)?.role, 'tool');
|
|
});
|
|
|
|
test('message pruning removes complete parallel tool-result batches', () => {
|
|
const calls = ['a', 'b', 'c'].map((toolCallId) => ({
|
|
type: 'tool-call' as const,
|
|
toolCallId,
|
|
toolName: 'terminal_poll',
|
|
input: { jobId: toolCallId },
|
|
}));
|
|
const results = calls.map((call) => ({
|
|
role: 'tool' as const,
|
|
content: [{
|
|
type: 'tool-result' as const,
|
|
toolCallId: call.toolCallId,
|
|
toolName: call.toolName,
|
|
output: { type: 'text' as const, value: `result ${call.toolCallId}` },
|
|
}],
|
|
}));
|
|
const batch: ModelMessage[] = [
|
|
{ role: 'assistant', content: calls },
|
|
...results,
|
|
];
|
|
|
|
assert.deepEqual(pruneFirstModelMessage([...batch, { role: 'user', content: 'next' }]), [
|
|
{ role: 'user', content: 'next' },
|
|
]);
|
|
assert.deepEqual(pruneLastModelMessage([{ role: 'user', content: 'before' }, ...batch]), [
|
|
{ role: 'user', content: 'before' },
|
|
]);
|
|
});
|
|
|
|
test('pruneUntilFitsCompaction shrinks history to fit budget', () => {
|
|
const messages: ModelMessage[] = Array.from({ length: 20 }, (_, index) => ({
|
|
role: index % 2 === 0 ? 'user' : 'assistant',
|
|
content: 'word '.repeat(500),
|
|
})) as ModelMessage[];
|
|
|
|
const pruned = pruneUntilFitsCompaction({
|
|
messages,
|
|
availableForInput: 2_000,
|
|
providerId: 'openai',
|
|
});
|
|
assert.ok(pruned.length < messages.length);
|
|
});
|
|
|
|
test('pruneUntilFitsCompaction drops oldest messages first', () => {
|
|
const messages: ModelMessage[] = [
|
|
{ role: 'user', content: `oldest ${'x'.repeat(5_000)}` },
|
|
...Array.from({ length: 16 }, (_, index) => ({
|
|
role: index % 2 === 0 ? 'assistant' : 'user',
|
|
content: 'middle context',
|
|
})) as ModelMessage[],
|
|
{ role: 'user', content: 'newest goal for current task' },
|
|
{ role: 'assistant', content: 'latest reply before tail split' },
|
|
];
|
|
|
|
const pruned = pruneUntilFitsCompaction({
|
|
messages,
|
|
availableForInput: 800,
|
|
providerId: 'openai',
|
|
});
|
|
const serialized = JSON.stringify(pruned);
|
|
assert.match(serialized, /newest goal for current task/);
|
|
assert.doesNotMatch(serialized, /oldest/);
|
|
});
|