'use strict'; const assert = require('assert'); const path = require('path'); const os = require('os'); const fs = require('fs'); const catalog = require('../lib/catalog.js'); const compaction = require('../agent/compaction.js'); const sr = require('../agent/search-replace.js'); const toolSet = require('../agent/tool-set.js'); const prompts = require('../agent/prompts.js'); const net = require('../lib/net.js'); const customTools = require('../agent/custom-tools.js'); const planMode = require('../agent/plan-mode.js'); const todos = require('../agent/todos.js'); const stationarity = require('../agent/stationarity.js'); const truncate = require('../agent/truncate.js'); const permRules = require('../agent/perm-rules.js'); const policy = require('../agent/policy.js'); const paths = require('../lib/paths.js'); const device = require('../lib/device.js'); function testCatalog() { assert.strictEqual(catalog.resolveModelConstant('qwen3.5-4b'), 'QWEN3_5_4B_MULTIMODAL_Q4_K_M'); assert.strictEqual(catalog.resolveModelConstant('gemma4-4b'), 'GEMMA4_4B_MULTIMODAL_Q4_K_M'); assert.strictEqual(catalog.resolveModelConstant('qwen3-8b'), 'QWEN3_8B_INST_Q4_K_M'); assert.strictEqual(catalog.resolveModelConstant('qwen3vl-2b'), 'QWEN3_VL_2B_INSTRUCT_Q4_K_M'); assert.strictEqual(catalog.findCatalogEntry('gemma4-4b').vision, true); assert.strictEqual(catalog.findCatalogEntry('qwen3-8b').vision, false); assert.strictEqual(catalog.toolDialectFor('gemma4-4b'), 'gemma4'); assert.strictEqual(catalog.toolDialectFor('qwen3-8b'), 'hermes'); assert.ok(/~5 GB/.test(catalog.catalogLabel(catalog.findCatalogEntry('qwen3-8b')))); assert.ok(catalog.FALLBACK_LLM_IDS.indexOf('gemma4-4b') >= 0); const listed = catalog.listCatalog().find((m) => m.id === 'gemma4-2b'); assert.ok(listed && listed.label.indexOf('~3.5 GB') >= 0); } function testCompaction() { const sys = { role: 'system', content: 'sys' }; const user = { role: 'user', content: 'hello' }; const hist = [sys, user]; for (let i = 0; i < 40; i++) { hist.push({ role: 'assistant', content: 'x'.repeat(200) }); hist.push({ role: 'tool', name: 'read_file', content: 'y'.repeat(200) }); } assert.ok(compaction.shouldCompact(hist, [], 1024)); const out = compaction.compact(hist, { budgetTokens: 400, tools: [] }); assert.ok(out.length < hist.length); assert.strictEqual(out[0].role, 'system'); assert.ok(compaction.isOverflowError(new Error('prompt too long for context window'))); } function testSearchReplace() { const applied = sr.applySearchReplace('aaa bbb aaa', 'bbb', 'ccc', false); assert.strictEqual(applied.text, 'aaa ccc aaa'); assert.strictEqual(applied.replacements, 1); } function testToolSet() { assert.ok(toolSet.isHostWorkspaceTool('read_file')); assert.ok(!toolSet.parseHostWorkspace({ hostWorkspace: false })); const all = [ { name: 'read_file' }, { name: 'todo_write' }, { name: 'web_fetch' }, ]; const page = toolSet.filterBuiltinSchemas(all, { hostWorkspace: false, builtinTools: ['todo_write'] }); assert.ok(!page.find((t) => t.name === 'read_file')); assert.ok(page.find((t) => t.name === 'todo_write')); } function testPrompts() { assert.ok(prompts.DEFAULT_SYSTEM.indexOf('QVAC') >= 0); assert.ok(prompts.DEFAULT_SYSTEM.indexOf('BridgeSwarm') < 0); const page = prompts.assemble({ cwd: 'container', hostWorkspace: false }); assert.ok(page.indexOf('host filesystem') >= 0); } function testNet() { assert.throws(() => net.assertPublicHttpUrl('http://127.0.0.1/x')); assert.throws(() => net.assertPublicHttpUrl('http://192.168.1.1/x')); const u = net.assertPublicHttpUrl('https://example.com/a'); assert.strictEqual(u.hostname, 'example.com'); } function testCustomTools() { customTools.clear('s1'); customTools.setSession('s1', { hostWorkspace: false }); customTools.register('s1', { name: 'ping', description: 'ping', parameters: { type: 'object', properties: {} }, execute: () => ({ ok: true }), }); assert.ok(customTools.has('s1', 'ping')); assert.strictEqual(typeof customTools.getHandler('s1', 'ping'), 'function'); customTools.clear('s1'); } function testPlanTodosStationarity() { const pm = planMode.create(); planMode.activate(pm); assert.ok(planMode.isActive(pm)); const blocked = planMode.gateWrite(pm, 'write_file', { path: 'foo.txt' }); assert.ok(blocked); const ok = planMode.gateWrite(pm, 'write_file', { path: 'plan.md' }); assert.ok(!ok); const list = todos.merge([], [{ id: '1', content: 'a', status: 'pending' }], 'replace'); assert.ok(todos.hasOpen(list)); const st = stationarity.create(); stationarity.observe(st, 'read_file', { path: 'a' }); stationarity.observe(st, 'read_file', { path: 'a' }); stationarity.observe(st, 'read_file', { path: 'a' }); assert.ok(stationarity.shouldNudge(st) || !stationarity.shouldStop(st)); } function testTruncateAndPerm() { const t = truncate.truncateWithMarker('x'.repeat(5000), 400); assert.ok(t.length < 5000); assert.ok(t.indexOf('truncated') >= 0); const pat = permRules.patternFromArgs('run_terminal_cmd', { command: 'git status -sb' }); assert.strictEqual(pat, 'git status'); assert.ok(policy.shellSafe('git status')); assert.ok(!policy.shellSafe('rm -rf /')); } function testPaths() { const dir = paths.ensureDir(path.join(os.tmpdir(), 'agent-harness-test')); assert.ok(fs.existsSync(dir)); assert.ok(paths.isPathInside(dir, path.join(dir, 'a.txt'))); assert.ok(!paths.isPathInside(dir, path.join(dir, '..', 'escape'))); } function testQvacWorkerDeps() { assert.doesNotThrow(() => require('hyperdispatch/runtime')); assert.doesNotThrow(() => require('@qvac/registry-schema')); } function testDevicePrefersGpu() { const empty = { gpus: [], drivers: {}, vramBytes: 0 }; assert.deepStrictEqual(device.pickDevice('auto', empty), { device: 'gpu', gpu_layers: 99 }); assert.deepStrictEqual(device.pickDevice(undefined, empty), { device: 'gpu', gpu_layers: 99 }); assert.deepStrictEqual(device.pickDevice('gpu', empty), { device: 'gpu', gpu_layers: 99 }); assert.deepStrictEqual(device.pickDevice('cpu', { gpus: [{ name: 'NVIDIA' }], drivers: { vulkan: true } }), { device: 'cpu', gpu_layers: 0, }); assert.strictEqual(device.mmprojOnGpu({}, empty, true), true); assert.strictEqual(device.mmprojOnGpu({ mmprojUseGpu: false }, empty, true), false); assert.strictEqual(device.mmprojOnGpu({}, empty, false), false); assert.strictEqual(device.gpuLayers({}, { device: 'gpu', gpu_layers: 99 }), 99); const sdkShape = { capabilities: { memory: { totalBytes: { status: 'supported', value: 32e9, provenance: { source: 'test' } } }, gpus: { status: 'supported', value: [ { id: '0', name: { status: 'supported', value: 'GeForce', provenance: { source: 'test' } }, type: { status: 'supported', value: 2, provenance: { source: 'test' } }, memoryTotalBytes: { status: 'supported', value: 24e9, provenance: { source: 'test' } }, drivers: { vulkan: { status: 'supported', value: true, provenance: { source: 'test' } } }, }, ], provenance: { source: 'test' }, }, }, }; const norm = device.normalizeResources(sdkShape); assert.ok(device.hasGpu(norm)); assert.ok(norm.vramBytes > 1e9); assert.strictEqual(device.backendLabel(norm).backend, 'vulkan'); } testCatalog(); testCompaction(); testSearchReplace(); testToolSet(); testPrompts(); testNet(); testCustomTools(); testPlanTodosStationarity(); testTruncateAndPerm(); testPaths(); testQvacWorkerDeps(); testDevicePrefersGpu(); console.log('ok');