import test from 'node:test'; import assert from 'node:assert/strict'; import { createRequire } from 'node:module'; import { VOICE_SYSTEM_PROMPT, voiceSystemPrompt } from '../skills/voice-prompt.js'; const require = createRequire(import.meta.url); const compaction = require('../vendor/agent-harness/agent/compaction.js'); const prompts = require('../vendor/agent-harness/agent/prompts.js'); function hugeTools() { return Array.from({ length: 24 }, (_, i) => ({ name: 'tool_' + i, description: 'schema '.repeat(80), parameters: { type: 'object', properties: { q: { type: 'string' } } }, })); } test('short voice chats do not compact just because tool schemas are large', () => { const tools = hugeTools(); const hist = [ { role: 'system', content: VOICE_SYSTEM_PROMPT }, { role: 'assistant', content: 'Hello! How can I help you today?' }, { role: 'user', content: 'Hi, please tell me about my computer.' }, ]; assert.equal(compaction.shouldCompact(hist, tools, 8192), false); const out = compaction.compact(hist, { budgetTokens: compaction.historyBudget(8192, tools, 0), tools, voice: true, }); assert.equal(out.length, hist.length); assert.equal(out[2].content, hist[2].content); assert.equal(JSON.stringify(out).includes('Earlier turns were compacted'), false); }); test('heuristic compact does not double-count tools against the history budget', () => { const tools = hugeTools(); const hist = [ { role: 'system', content: 'You are Jarvis' }, { role: 'assistant', content: 'Hello! How can I help you today?' }, { role: 'user', content: 'Hi, please tell me about my computer.' }, { role: 'assistant', content: 'Let me check that.' }, { role: 'user', content: 'Please continue.' }, ]; const budget = compaction.historyBudget(8192, tools, 0); assert.ok(budget <= 240 || compaction.toolTokens(tools) > 1000); const out = compaction.heuristicCompact(hist, { budgetTokens: budget, tools }); assert.ok(out.some((m) => String(m.content).includes('tell me about my computer'))); assert.equal(JSON.stringify(out).includes('Earlier turns were compacted'), false); }); test('voice compaction does not auto-continue a new greeting', () => { const cont = compaction.autoContinue([{ role: 'assistant', content: 'Hello!' }], { voice: true }); assert.equal(cont, null); assert.match(compaction.compactReminder({ voice: true }), /Do not greet again/); }); test('voice assemble inlines workspace identity files and skips a completed bootstrap', () => { const sys = prompts.assemble({ personality: 'voice', extra: VOICE_SYSTEM_PROMPT, cwd: '/home/raven/.local/share/jarvis/workspace', hostWorkspace: true, fsRead: (_c, n) => { if (n === 'SOUL.md') return '# SOUL.md: Jarvis\nYou are Jarvis on this desktop.'; if (n === 'AGENTS.md') return '# AGENTS.md\nFollow the workspace ritual.'; if (n === 'PERSONA.md') return '# PERSONA.md\nBe a roast comedian.'; if (n === 'BOOTSTRAP.md') return '# completed'; return ''; }, }); assert.match(sys, /You are Jarvis/); assert.match(sys, /SOUL\.md/); assert.match(sys, /AGENTS\.md/); assert.doesNotMatch(sys, /## PERSONA\.md/); assert.doesNotMatch(sys, /Be a roast comedian/); assert.doesNotMatch(sys, /You are a local coding agent/); assert.doesNotMatch(sys, /## BOOTSTRAP\.md/); }); test('voice assemble keeps acting notes in the system prompt and skips SOUL identity files', () => { const extra = voiceSystemPrompt('Tony', 'Jarvis, you are acting as Tony Hinchcliffe. Never call yourself Jarvis.'); const sys = prompts.assemble({ personality: 'voice', extra, cwd: '/home/raven/.local/share/jarvis/workspace', hostWorkspace: true, fsRead: (_c, n) => { if (n === 'SOUL.md') return '# SOUL.md: Jarvis\nYou are **Jarvis**.'; if (n === 'IDENTITY.md') return '# IDENTITY.md\nJarvis lives here.'; if (n === 'PERSONA.md') return '# PERSONA.md\nDuplicate roast notes.'; if (n === 'AGENTS.md') return '# AGENTS.md\nFollow the workspace ritual.'; return ''; }, }); assert.match(sys, /Your name is Tony/); assert.match(sys, /## Acting/); assert.match(sys, /Tony Hinchcliffe/); assert.match(sys, /AGENTS\.md/); assert.doesNotMatch(sys, /## SOUL\.md/); assert.doesNotMatch(sys, /## IDENTITY\.md/); assert.doesNotMatch(sys, /## PERSONA\.md/); assert.doesNotMatch(sys, /Duplicate roast notes/); assert.doesNotMatch(sys, /You are \*\*Jarvis\*\*/); }); test('LLM compact skips a two-turn voice chat', async () => { let called = false; const hist = [ { role: 'system', content: 'You are Jarvis' }, { role: 'assistant', content: 'Hello! How can I help you today?' }, { role: 'user', content: 'Hi, please tell me about my computer.' }, ]; const out = await compaction.compactWithLlm(hist, { voice: true, complete: async () => { called = true; return { text: '1. Latest user request\n2. Facts\n3. Answered\n4. Follow-ups\n' }; }, }); assert.equal(called, false); 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.'); }); }