@@ -122,3 +122,189 @@ test('LLM compact skips a two-turn voice chat', async () => {
|
||||
assert.equal(out.length, hist.length);
|
||||
assert.equal(out[2].content, hist[2].content);
|
||||
});
|
||||
|
||||
function exchange(id, size = 5000) {
|
||||
return [
|
||||
{ role: 'assistant', content: 'Checking the device.', tool_calls: [{ id, type: 'function', function: { name: 'lookup', arguments: '{"device":"nest"}' } }] },
|
||||
{ role: 'tool', tool_call_id: id, name: 'lookup', content: 'Hostname: nest. ' + 'details '.repeat(size) },
|
||||
];
|
||||
}
|
||||
|
||||
function assertValidTools(history) {
|
||||
let calls = new Set();
|
||||
for (const message of history) {
|
||||
if (message.role === 'tool') assert.ok(calls.has(message.tool_call_id), 'tool result must follow its original call');
|
||||
else calls = new Set((message.tool_calls || []).map((call) => call.id));
|
||||
}
|
||||
}
|
||||
|
||||
test('summary rebuild keeps the latest request in order and preserves native tool metadata', () => {
|
||||
const latest = { role: 'user', content: 'What about its memory?' };
|
||||
const tool = exchange('memory', 1);
|
||||
const rebuilt = compaction.rebuildHistory([
|
||||
{ role: 'system', content: 'You are Jarvis.' },
|
||||
{ role: 'user', content: 'Check my computer.' },
|
||||
{ role: 'assistant', content: 'It is named nest.' },
|
||||
latest, ...tool,
|
||||
], 'The user is asking about their computer named nest.');
|
||||
assert.deepEqual(rebuilt.slice(2), [latest, ...tool]);
|
||||
assertValidTools(rebuilt);
|
||||
});
|
||||
|
||||
test('large tool results trigger compaction even in a short conversation', () => {
|
||||
const history = [{ role: 'system', content: 'Jarvis' }, { role: 'user', content: 'Check nest.' }, ...exchange('large')];
|
||||
assert.equal(compaction.shouldCompact(history, [], 8192), true);
|
||||
const compacted = compaction.compact(history, { budgetTokens: 800 });
|
||||
assert.ok(compaction.conversationTokens(compacted) <= 800);
|
||||
assert.ok(compacted.some((m) => m.content === 'Check nest.'));
|
||||
assert.match(JSON.stringify(compacted), /Hostname: nest/);
|
||||
assertValidTools(compacted);
|
||||
});
|
||||
|
||||
test('heuristic fallback retains useful conversational facts and fits its budget', () => {
|
||||
const request = { role: 'user', content: 'And what should I do next?' };
|
||||
const history = [
|
||||
{ role: 'system', content: 'Jarvis' },
|
||||
{ role: 'user', content: 'My computer is nest. Keep answers brief.' },
|
||||
{ role: 'assistant', content: 'We are investigating memory usage. ' + 'details '.repeat(3000) },
|
||||
request,
|
||||
];
|
||||
const compacted = compaction.compact(history, { budgetTokens: 500, aggressive: true });
|
||||
assert.ok(compaction.conversationTokens(compacted) <= 500);
|
||||
assert.match(compacted[1].content, /nest.*Keep answers brief/);
|
||||
assert.equal(compacted.at(-1), request);
|
||||
assert.doesNotMatch(compacted[1].content, /Earlier turns were compacted to fit/);
|
||||
});
|
||||
|
||||
test('LLM summaries are bounded and repeated compaction retains preferences and the latest request', async () => {
|
||||
const request = { role: 'user', content: 'What is the next step?' };
|
||||
let history = [
|
||||
{ role: 'system', content: 'Jarvis' },
|
||||
{ role: 'user', content: 'My computer is nest. Keep answers brief.' },
|
||||
{ role: 'assistant', content: 'We are checking memory.' },
|
||||
request, ...exchange('memory'),
|
||||
];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
history = await compaction.compactWithLlm(history, {
|
||||
voice: true, aggressive: true, budgetTokens: 700,
|
||||
complete: async ({ history: prompt }) => {
|
||||
assert.match(prompt[1].content, /nest/);
|
||||
assert.match(prompt[1].content, /Tool calls:/);
|
||||
return { text: 'The user owns nest and wants brief answers. We are investigating memory usage. ' + 'Additional findings. '.repeat(1000) };
|
||||
},
|
||||
});
|
||||
assert.ok(compaction.conversationTokens(history) <= 700);
|
||||
assert.match(history[1].content, /nest.*brief answers/);
|
||||
assert.ok(history.includes(request));
|
||||
assertValidTools(history);
|
||||
history.push(...exchange('next-' + i));
|
||||
}
|
||||
});
|
||||
|
||||
test('failed summarization falls back without erasing the latest request or tool identities', async () => {
|
||||
const request = { role: 'user', content: 'Please check the computer named nest.' };
|
||||
const history = [{ role: 'system', content: 'Jarvis' }, request, ...exchange('failure')];
|
||||
const compacted = await compaction.compactWithLlm(history, {
|
||||
budgetTokens: 600, complete: async () => { throw new Error('summary failed'); },
|
||||
});
|
||||
assert.ok(compaction.conversationTokens(compacted) <= 600);
|
||||
assert.ok(compacted.includes(request));
|
||||
assertValidTools(compacted);
|
||||
});
|
||||
|
||||
test('compaction never silently clips an oversized latest user request', () => {
|
||||
const request = { role: 'user', content: 'Keep this exact constraint. '.repeat(1000) };
|
||||
const compacted = compaction.compact([{ role: 'system', content: 'Jarvis' }, request], { budgetTokens: 300, aggressive: true });
|
||||
assert.equal(compacted.at(-1), request);
|
||||
});
|
||||
|
||||
test('sidecar context is reserved in the history budget', () => {
|
||||
assert.equal(compaction.historyBudget(8192, [], 0) - compaction.historyBudget(8192, [], 0, 512), 512);
|
||||
});
|
||||
|
||||
for (const overflow of [false, true]) {
|
||||
test(`turn loop persists compaction and sends one-shot reminders through retries (overflow=${overflow})`, async (t) => {
|
||||
const { mkdtempSync, rmSync } = await import('node:fs');
|
||||
const { tmpdir } = await import('node:os');
|
||||
const path = await import('node:path');
|
||||
const engine = require('../vendor/agent-harness/lib/qvac.js');
|
||||
const sessions = require('../vendor/agent-harness/agent/sessions.js');
|
||||
const mcp = require('../vendor/agent-harness/agent/mcp.js');
|
||||
const loop = require('../vendor/agent-harness/agent/loop.js');
|
||||
const root = mkdtempSync(path.join(tmpdir(), 'jarvis-compaction-'));
|
||||
const previousRoot = process.env.AGENT_HARNESS_HOME;
|
||||
process.env.AGENT_HARNESS_HOME = root;
|
||||
const oldComplete = engine.complete;
|
||||
const oldLoaded = engine.getLoaded;
|
||||
const oldNotes = mcp.handshakeReminders;
|
||||
t.after(() => {
|
||||
engine.complete = oldComplete;
|
||||
engine.getLoaded = oldLoaded;
|
||||
mcp.handshakeReminders = oldNotes;
|
||||
if (previousRoot === undefined) delete process.env.AGENT_HARNESS_HOME;
|
||||
else process.env.AGENT_HARNESS_HOME = previousRoot;
|
||||
rmSync(root, { recursive: true, force: true });
|
||||
});
|
||||
const model = 'test-model';
|
||||
engine.getLoaded = () => ({ modelId: model, friendlyId: model, ctxSize: 8192 });
|
||||
let noteSent = false;
|
||||
mcp.handshakeReminders = () => {
|
||||
if (noteSent) return [];
|
||||
noteSent = true;
|
||||
return ['MCP test handshake failed: unavailable'];
|
||||
};
|
||||
const meta = sessions.create({ origin: 'jarvis-qvac', cwd: root, hostWorkspace: false, builtinTools: false, model });
|
||||
sessions.replaceHistory(meta.id, [
|
||||
{ role: 'user', content: 'My computer is nest. Please keep answers brief.' },
|
||||
{ role: 'assistant', content: 'Checking its memory. ' + 'history '.repeat(5000) },
|
||||
{ role: 'user', content: 'Remember the hostname.' },
|
||||
{ role: 'assistant', content: 'I will remember nest.' },
|
||||
]);
|
||||
let summaries = 0;
|
||||
let completions = 0;
|
||||
const promptSizes = [];
|
||||
engine.complete = async ({ history }) => {
|
||||
if (history[0].content.startsWith('Reply with the four summary sections')) {
|
||||
summaries++;
|
||||
return { text: 'The computer is named nest. The user wants brief answers. We have checked memory and should continue answering follow-up questions about this computer.' };
|
||||
}
|
||||
completions++;
|
||||
promptSizes.push(compaction.conversationTokens(history));
|
||||
assert.equal(history.filter((m) => /Context was compacted/.test(m.content)).length, 1);
|
||||
assert.equal(history.filter((m) => /MCP test handshake/.test(m.content)).length, 1);
|
||||
assert.ok(history.some((m) => m.content === 'What is its hostname?'));
|
||||
assert.match(JSON.stringify(history), /nest/);
|
||||
assert.ok(compaction.estimateTokens(history, []) < 8192);
|
||||
if (overflow && completions === 1) throw new Error('prompt too long for context window');
|
||||
return { text: 'Its hostname is nest.', toolCalls: [] };
|
||||
};
|
||||
const updates = [];
|
||||
const result = await loop.runTurn({
|
||||
session: sessions.load(meta.id), userText: 'What is its hostname?', jobId: 'context-test',
|
||||
payload: { voice: true, system: 'Jarvis', maxTurns: 1 },
|
||||
emit: (_kind, event) => updates.push(event),
|
||||
});
|
||||
assert.equal(result.text, 'Its hostname is nest.');
|
||||
assert.equal(summaries, 1);
|
||||
assert.equal(completions, overflow ? 2 : 1);
|
||||
if (overflow) assert.ok(promptSizes[1] < promptSizes[0], 'overflow recovery must actually shrink the prompt');
|
||||
assert.ok(updates.some((ev) => ev.type === 'compaction' && ev.status === 'done'));
|
||||
const saved = sessions.load(meta.id).history;
|
||||
assert.match(saved[1].content, /nest/);
|
||||
assert.equal(saved.at(-1).content, 'Its hostname is nest.');
|
||||
assert.ok(compaction.conversationTokens(saved) < 2000);
|
||||
|
||||
// Follow-up requests reload the persisted summary, not the original large history.
|
||||
engine.complete = async ({ history }) => {
|
||||
assert.match(history[1].content, /nest/);
|
||||
assert.equal(history.at(-1).role, 'user');
|
||||
assert.ok(history.some((m) => m.content === 'And what were we checking?'));
|
||||
return { text: 'We were checking memory.', toolCalls: [] };
|
||||
};
|
||||
const followup = await loop.runTurn({
|
||||
session: sessions.load(meta.id), userText: 'And what were we checking?', jobId: 'followup-test',
|
||||
payload: { voice: true, system: 'Jarvis', maxTurns: 1 }, emit() {},
|
||||
});
|
||||
assert.equal(followup.text, 'We were checking memory.');
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user