okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-split-fb3 into f-split-int (locale keys re-added after the card-list strings)7b06ea51b

main
9.0 KiB128 linesraw
1/**
2 * Helper agents on a model the person names (server/agent/model-routes.mjs and plugins/strata-models.mjs).
3 * Pure tests always run; the real-Harness tests need STRATA_HARNESS_DIR and skip loudly without it.
4 */
5import test from 'node:test';
6import assert from 'node:assert/strict';
7import { mkdtempSync, mkdirSync, rmSync } from 'node:fs';
8import { tmpdir } from 'node:os';
9import { join } from 'node:path';
10import { startScriptedWorker } from './helpers/scripted-worker.mjs';
11import { createAgentBridge } from '../server/agent/agent-bridge.mjs';
12import { resolveHarness } from '../server/agent/harness-runtime.mjs';
13import { resolveModel, modelProblem, routeList } from '../server/agent/model-routes.mjs';
14import { AGENT_PROMPT } from '../server/agent/preset.mjs';
15
16const HARNESS = process.env.STRATA_HARNESS_DIR;
17const skip = !HARNESS || !resolveHarness({ env: process.env }).ok ? 'STRATA_HARNESS_DIR is not set to a built Harness' : false;
18
19const routes = [
20  { provider: 'strata-local', model: 'gyro-gyro-s', name: 'qwen3.8-flash-next-gyro-gyro-s', connection: 'this computer', local: true },
21  { provider: 'api-aaaa', model: 'deepseek/deepseek-v3.2', name: 'DeepSeek: DeepSeek V3.2', connection: 'OpenRouter', local: false },
22  { provider: 'api-aaaa', model: 'anthropic/claude-sonnet-4.5', name: 'Anthropic: Claude Sonnet 4.5', connection: 'OpenRouter', local: false },
23  { provider: 'api-bbbb', model: 'deepseek-chat', name: 'DeepSeek Chat', connection: 'DeepSeek', local: false },
24];
25
26test('nothing named: the helper uses the chat model', () => {
27  assert.equal(resolveModel(routes, {}).status, 'inherit');
28  assert.equal(modelProblem(routes, {}), null);
29});
30
31test('exact route, exact id, provider/id, connection and name, in any letters', () => {
32  const want = (request, id) => { const r = resolveModel(routes, request); assert.equal(r.status, 'exact', JSON.stringify(request)); assert.equal(r.route.model, id); };
33  want({ provider: 'api-aaaa', model: 'deepseek/deepseek-v3.2' }, 'deepseek/deepseek-v3.2');
34  want({ model: 'deepseek/deepseek-v3.2' }, 'deepseek/deepseek-v3.2');
35  want({ model: 'DeepSeek/DeepSeek-V3.2' }, 'deepseek/deepseek-v3.2');
36  want({ provider: 'openrouter', model: 'deepseek/deepseek-v3.2' }, 'deepseek/deepseek-v3.2');
37  want({ model: 'openrouter/deepseek/deepseek-v3.2' }, 'deepseek/deepseek-v3.2');
38  want({ model: 'DeepSeek: DeepSeek V3.2' }, 'deepseek/deepseek-v3.2');
39  want({ model: 'deepseek chat' }, 'deepseek-chat');
40  want({ model: 'Local' }, 'gyro-gyro-s');
41  want({ provider: 'strata-local', model: 'gyro-gyro-s' }, 'gyro-gyro-s');
42});
43
44test('a close guess is checked with the person, several fits are listed, nothing fitting lists the models', () => {
45  const guess = resolveModel(routes, { model: 'claude sonnet' });
46  assert.equal(guess.status, 'confirm'); assert.equal(guess.route.model, 'anthropic/claude-sonnet-4.5');
47  assert.match(modelProblem(routes, { model: 'claude sonnet' }), /Did you mean Anthropic: Claude Sonnet 4\.5/);
48  const several = resolveModel(routes, { model: 'deepseek' });
49  assert.equal(several.status, 'ambiguous'); assert.ok(several.options.length >= 2);
50  const none = modelProblem(routes, { model: 'llama banana' });
51  assert.match(none, /No model is called "llama banana"/); assert.match(none, /DeepSeek Chat/); assert.match(none, /Nothing was started/);
52  assert.match(modelProblem([], { model: 'x' }), /No other model is set up/);
53  assert.match(routeList(routes), /running on this computer/);
54});
55
56test('the assistant is told to use the subagent tool when asked for a subagent or a model', () => {
57  assert.match(AGENT_PROMPT, /ask a subagent/); assert.match(AGENT_PROMPT, /Never look for a model with shell commands/);
58});
59
60async function rig(t) {
61  const local = await startScriptedWorker();
62  const remote = await startScriptedWorker({ model: 'deepseek/deepseek-v3.2' });
63  const dir = mkdtempSync(join(tmpdir(), 'strata-sub-'));
64  const ws = join(dir, 'ws'); mkdirSync(ws, { recursive: true });
65  const bridge = createAgentBridge({
66    dataDir: dir, env: process.env, startTimeoutMs: 90_000,
67    localInstances: () => [{ id: 'inst-1', label: 'Local Scripted', baseUrl: local.url, modelId: 'scripted-model', context: 32768, slots: 2 }],
68    connections: () => [{ id: 'conn-1', name: 'OpenRouter', baseUrl: remote.url, protocol: 'openai-chat', apiKey: 'k', models: [{ id: 'deepseek/deepseek-v3.2', name: 'DeepSeek V3.2', contextMax: 32768 }] }],
69  });
70  t.after(async () => { await bridge.close(); await local.close(); await remote.close(); try { rmSync(dir, { recursive: true, force: true, maxRetries: 3 }); } catch { /* best effort */ } });
71  const call = async (chatId, args) => {
72    const frames = [];
73    await bridge.runTurn({ chatId, message: `SCRIPT:call subagent ${JSON.stringify(args)}`, workspacePath: ws, mode: 'bypass', onFrame: f => frames.push(f) });
74    return { frames, text: frames.filter(f => f.type === 'delta').map(f => f.text).join('') };
75  };
76  return { local, remote, bridge, call, ws };
77}
78
79test('the real Harness: the model sees guidance and the discovery list, and a name starts the helper on that model', { skip }, async (t) => {
80  const r = await rig(t);
81  const first = await r.call('m1', { description: 'Weather', prompt: 'SCRIPT:echo grey', model: 'DeepSeek V3.2' });
82  const tools = JSON.parse([...r.local.requests].reverse().find(q => q.toolNames.length).raw).tools;
83  const sub = tools.find(x => x.function.name === 'subagent').function;
84  assert.match(sub.description, /ask a subagent/); assert.match(sub.description, /do not look for the model yourself/);
85  assert.match(sub.parameters.properties.model.description, /"local"/);
86  assert.ok(tools.some(x => x.function.name === 'list_subagent_models'));
87  // the helper ran on the remote route, not on the chat's model
88  for (let i = 0; i < 60 && !r.remote.requests.length; i++) await new Promise(res => setTimeout(res, 250));
89  assert.ok(r.remote.requests.length >= 1, `the helper reached the OpenRouter mock (${first.text})`);
90});
91
92test('the real Harness: a close guess and an unknown model are answered in plain words and start nothing', { skip }, async (t) => {
93  const r = await rig(t);
94  const guess = await r.call('m2', { description: 'Weather', prompt: 'x', model: 'deepseek v3' });
95  assert.match(guess.text, /Did you mean DeepSeek V3\.2/);
96  const none = await r.call('m3', { description: 'Weather', prompt: 'x', model: 'llama banana' });
97  assert.match(none.text, /No model is called "llama banana"/);
98  const seen = [...r.local.requests].reverse().find(q => q.lastRole === 'tool').raw;
99  assert.match(seen, /DeepSeek V3.2/, 'the list the assistant read names the models');
100  await new Promise(res => setTimeout(res, 1500));
101  assert.equal(r.remote.requests.length, 0, 'nothing was started');
102});
103
104test('the real Harness: naming the local model, and naming no model, both work', { skip }, async (t) => {
105  const r = await rig(t);
106  const before = r.local.requests.length;
107  await r.call('m4', { description: 'Local', prompt: 'SCRIPT:echo hi', model: 'the local model' });
108  await r.call('m5', { description: 'Plain', prompt: 'SCRIPT:echo hi' });
109  for (let i = 0; i < 40 && r.local.requests.filter(q => q.directive === 'echo').length < 2; i++) await new Promise(res => setTimeout(res, 250));
110  assert.ok(r.local.requests.length > before);
111  assert.equal(r.remote.requests.length, 0);
112});
113
114test('the real Harness: a helper that finishes while the chat model is still answering does not end the run early ("interrupted before it finished")', { skip }, async (t) => {
115  // The chat model answers slowly, the helper's model (a second server) answers at once: the helper reports back while the parent is mid-answer.
116  const slow = await startScriptedWorker({ chunkDelayMs: 900 });
117  const fast = await startScriptedWorker({ model: 'deepseek/deepseek-v3.2', chunkDelayMs: 5 });
118  const dir = mkdtempSync(join(tmpdir(), 'strata-race-')); const ws = join(dir, 'ws'); mkdirSync(ws, { recursive: true });
119  const bridge = createAgentBridge({ dataDir: dir, env: process.env, startTimeoutMs: 90_000,
120    localInstances: () => [{ id: 'inst-1', label: 'Slow chat model', baseUrl: slow.url, modelId: 'scripted-model', context: 32768, slots: 2 }],
121    connections: () => [{ id: 'conn-1', name: 'OpenRouter', baseUrl: fast.url, protocol: 'openai-chat', apiKey: 'k', models: [{ id: 'deepseek/deepseek-v3.2', name: 'DeepSeek V3.2', contextMax: 32768 }] }] });
122  t.after(async () => { await bridge.close(); await slow.close(); await fast.close(); try { rmSync(dir, { recursive: true, force: true, maxRetries: 3 }); } catch { /* best effort */ } });
123  const frames = [];
124  await bridge.runTurn({ chatId: 'race', message: `SCRIPT:call subagent ${JSON.stringify({ description: 'Quick', prompt: 'SCRIPT:echo hi', model: 'DeepSeek V3.2' })}`, workspacePath: ws, mode: 'bypass', onFrame: f => frames.push(f) });
125  assert.ok(!frames.some(f => f.type === 'error'), `no error: ${JSON.stringify(frames.filter(f => f.type === 'error'))}`);
126  assert.equal(frames.at(-1).type, 'done');
127  assert.match(frames.filter(f => f.type === 'delta').map(f => f.text).join(''), /Tool finished/, 'the chat model got to answer');
128});