@@ -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.');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ function stripModules(source) {
|
||||
function harness() {
|
||||
const timers = new Map();
|
||||
const chrome = [];
|
||||
let nextTimer = 1;
|
||||
class Actor {
|
||||
constructor(props = {}) {
|
||||
if ('hexpand' in props) throw new Error('No property hexpand on StWidget');
|
||||
@@ -27,7 +28,11 @@ function harness() {
|
||||
this.visible = props.visible !== false;
|
||||
this.text = props.text || props.hint_text || '';
|
||||
this.clutter_text = { connect() {}, ellipsize: null };
|
||||
this.vadjustment = { value: 0, upper: 240, page_size: 80 };
|
||||
this.vadjustment = {
|
||||
value: 0, upper: 240, page_size: 80, handlers: {},
|
||||
connect(name, handler) { this.handlers[name] = handler; },
|
||||
emit(name) { this.handlers[name]?.(); },
|
||||
};
|
||||
}
|
||||
get_vadjustment() { return this.vadjustment; }
|
||||
add_child(child) { this.children.push(child); child.get_parent = () => this; }
|
||||
@@ -105,7 +110,7 @@ function harness() {
|
||||
GLib: {
|
||||
PRIORITY_DEFAULT: 0, PRIORITY_DEFAULT_IDLE: 200, SOURCE_REMOVE: false,
|
||||
idle_add(_priority, callback) { callback(); return 1; },
|
||||
timeout_add(_priority, _delay, callback) { const id = timers.size + 1; timers.set(id, callback); return id; },
|
||||
timeout_add(_priority, _delay, callback) { const id = nextTimer++; timers.set(id, callback); return id; },
|
||||
Source: { remove(id) { timers.delete(id); } },
|
||||
},
|
||||
log() {},
|
||||
@@ -492,8 +497,8 @@ test('Chat and Thinking tabs exist and thinking auto-follows', () => {
|
||||
assert.equal(view.scroll.vadjustment.value, 160);
|
||||
view.showTab('chat', { user: true });
|
||||
view.updateThinking('still thinking');
|
||||
assert.equal(view._tab, 'chat');
|
||||
assert.equal(view.thinkingScroll.visible, false);
|
||||
assert.equal(view._tab, 'thinking');
|
||||
assert.equal(view.thinkingScroll.visible, true);
|
||||
assert.match(view.thinking.text, /still thinking/);
|
||||
view.thinkingScroll.vadjustment.value = 0;
|
||||
view.showTab('thinking', { user: true });
|
||||
@@ -541,3 +546,61 @@ test('the Shell helper can capture a screenshot from inside GNOME', () => {
|
||||
assert.match(source, /new Shell\.Screenshot/);
|
||||
assert.match(source, /screenshot\(false, stream\)/);
|
||||
});
|
||||
|
||||
for (const compact of [true, false]) {
|
||||
test(`inference follows alternating content despite polling and manual navigation (${compact})`, () => {
|
||||
const { ConversationView } = harness();
|
||||
const view = new ConversationView({ compact });
|
||||
view.setState('THINKING');
|
||||
for (let i = 0; i < 12; i++) {
|
||||
view.updateThinking(`reason ${i}. `);
|
||||
assert.equal(view._tab, 'thinking');
|
||||
view.token(`answer ${i}. `);
|
||||
assert.equal(view._tab, 'chat');
|
||||
view.setState('THINKING');
|
||||
assert.equal(view._tab, 'chat');
|
||||
view.showTab('thinking', { user: true });
|
||||
view.addToolCall('{"name":"lookup"}');
|
||||
assert.equal(view._tab, 'chat');
|
||||
view.updateThinking('checking. ');
|
||||
view.addToolResult('{"name":"lookup","result":"done"}');
|
||||
assert.equal(view._tab, 'chat');
|
||||
}
|
||||
assert.match(view.thinking.text, /reason 0.*reason 11/);
|
||||
view.updateThinking('finished');
|
||||
view.finalizeReply('Final answer');
|
||||
assert.equal(view._tab, 'chat');
|
||||
assert.match(view.transcript.get_last_child().text, /Final answer/);
|
||||
});
|
||||
|
||||
test(`follow survives delayed layout, scrolling, remapping and teardown (${compact})`, () => {
|
||||
const { ConversationView, timers } = harness();
|
||||
const view = new ConversationView({ compact });
|
||||
view.setState('THINKING');
|
||||
for (const pane of [view.scroll, view.thinkingScroll]) {
|
||||
const adjustment = pane.vadjustment;
|
||||
adjustment.value = 0;
|
||||
adjustment.emit('notify::value');
|
||||
view.token('reply');
|
||||
view.updateThinking('reason');
|
||||
adjustment.upper = 1000;
|
||||
adjustment.emit('notify::upper');
|
||||
assert.equal(adjustment.value, 920);
|
||||
adjustment.page_size = 200;
|
||||
adjustment.emit('notify::page-size');
|
||||
assert.equal(adjustment.value, 800);
|
||||
adjustment.value = 0;
|
||||
pane.mapped = true;
|
||||
pane.handlers['notify::mapped']();
|
||||
assert.equal(adjustment.value, 800);
|
||||
}
|
||||
for (let i = 0; i < 100; i++) view.token(' more');
|
||||
assert.equal(timers.size, 2);
|
||||
for (const [id, callback] of [...timers]) { timers.delete(id); callback(); }
|
||||
assert.equal(timers.size, 0);
|
||||
view.updateThinking('again');
|
||||
view.token('done');
|
||||
view.destroy();
|
||||
assert.equal(timers.size, 0);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user