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
6.6 KiB85 linesraw
1/** "Used 2 skills: Weekly report, Brand voice" under an answer: the skills a turn loaded, with the name the person gave and a one-line description. */
2import test from 'node:test';
3import assert from 'node:assert/strict';
4import { mkdtempSync, mkdirSync, rmSync } from 'node:fs';
5import { tmpdir } from 'node:os';
6import { join } from 'node:path';
7import { readFile } from 'node:fs/promises';
8import { transform } from 'esbuild';
9import { startScriptedWorker } from './helpers/scripted-worker.mjs';
10import { createAgentBridge } from '../server/agent/agent-bridge.mjs';
11import { resolveHarness } from '../server/agent/harness-runtime.mjs';
12import { materializeSkills } from '../server/skill-files.mjs';
13import { SkillsUsed, skillInfoFrom, skillLabel } from '../server/agent/skills-used.mjs';
14
15const skip = !process.env.STRATA_HARNESS_DIR || !resolveHarness({ env: process.env }).ok ? 'STRATA_HARNESS_DIR is not set to a built Harness' : false;
16const cleanup = dir => { try { rmSync(dir, { recursive: true, force: true, maxRetries: 3 }); } catch { /* best effort */ } };
17
18test('a skill is reported once, when it has worked, with the whole list so far', () => {
19  const frames = [];
20  const used = new SkillsUsed({ emit: f => frames.push(f) });
21  used.card({ name: 'skill', state: 'running', args: { name: 'weekly-report' } });
22  used.card({ name: 'read', state: 'done', args: { name: 'x' } });
23  assert.equal(frames.length, 0, 'not while running, not for another tool');
24  used.card({ name: 'skill', state: 'done', args: { name: 'weekly-report' } });
25  used.card({ name: 'skill', state: 'done', args: { name: 'weekly-report' } });
26  used.card({ name: 'skill', state: 'error', args: { name: 'brand-voice' } });
27  used.card({ name: 'skill', state: 'done', args: { name: 'brand-voice' } });
28  assert.equal(frames.length, 2);
29  assert.deepEqual(frames[1], { type: 'skills-used', skills: [{ name: 'Weekly report', description: '' }, { name: 'Brand voice', description: '' }] });
30});
31
32test('the name and description come from the skill file the app wrote; a folder name is made readable', () => {
33  const dir = mkdtempSync(join(tmpdir(), 'strata-su-'));
34  try {
35    materializeSkills(dir, [{ id: 'my-weekly-report', name: 'Weekly report', description: 'Turns notes into a short status update', body: 'Be short.', enabled: true }]);
36    const info = skillInfoFrom(dir);
37    assert.deepEqual(info('weekly-report'), { name: 'Weekly report', description: 'Turns notes into a short status update' });
38    assert.deepEqual(info('brand-voice'), { name: 'Brand voice', description: '' });
39    assert.deepEqual(info('../../etc/passwd'), { name: skillLabel('../../etc/passwd'), description: '' }, 'a path is never read');
40  } finally { cleanup(dir); }
41});
42
43test('the page keeps the list on the reply (readSkills) and tolerates a bad frame', async () => {
44  const source = await readFile(new URL('../src/lib/deliverables.ts', import.meta.url), 'utf8');
45  const { code } = await transform(source.replace(/^import .*$/gm, ''), { loader: 'ts', format: 'esm' });
46  const { readSkills } = await import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`);
47  assert.deepEqual(readSkills({ skills: [{ name: 'A', description: 'd' }, { name: '' }, null, { name: 'B' }] }), [{ name: 'A', description: 'd' }, { name: 'B', description: '' }]);
48  assert.deepEqual(readSkills({}), []); assert.deepEqual(readSkills(null), []);
49});
50
51test('real Harness: loading two saved skills in a turn sends one skills-used frame per skill, then both are listed', { skip }, async (t) => {
52  const worker = await startScriptedWorker({});
53  const dir = mkdtempSync(join(tmpdir(), 'strata-su-run-')); const ws = join(dir, 'ws'); mkdirSync(ws, { recursive: true });
54  const live = join(dir, 'skills-live');
55  materializeSkills(live, [
56    { id: 'my-weekly-report', name: 'Weekly report', description: 'Turns notes into a short status update', body: 'Be short.', enabled: true },
57    { id: 'my-brand-voice', name: 'Brand voice', description: 'Plain, friendly, no jargon', body: 'Write plainly.', enabled: true },
58  ]);
59  const bridge = createAgentBridge({ dataDir: dir, localInstances: () => [{ id: 'i', label: 'Scripted', baseUrl: worker.url, modelId: 'scripted-model', context: 32768, slots: 2 }], connections: () => [], skillsDir: () => live, env: process.env, startTimeoutMs: 90_000 });
60  t.after(async () => { await bridge.close(); await worker.close(); cleanup(dir); });
61  const turn = async (chatId, args) => { const frames = []; await bridge.runTurn({ chatId, message: `SCRIPT:call skill ${JSON.stringify(args)}`, workspacePath: ws, mode: 'bypass', onFrame: f => frames.push(f) }); return frames.filter(f => f.type === 'skills-used'); };
62  const first = await turn('su', { name: 'weekly-report' });
63  assert.equal(first.length, 1);
64  assert.deepEqual(first[0].skills, [{ name: 'Weekly report', description: 'Turns notes into a short status update' }]);
65  const none = await turn('su2', { name: 'does-not-exist' });
66  assert.equal(none.length, 0, 'a skill that could not be loaded is not listed');
67});
68
69test('real Harness: two skills in one answer are listed together, in the order they were loaded', { skip }, async (t) => {
70  const worker = await startScriptedWorker({});
71  const dir = mkdtempSync(join(tmpdir(), 'strata-su-two-')); const ws = join(dir, 'ws'); mkdirSync(ws, { recursive: true });
72  const live = join(dir, 'skills-live');
73  materializeSkills(live, [
74    { id: 'my-weekly-report', name: 'Weekly report', description: 'Turns notes into a short status update', body: 'Be short.', enabled: true },
75    { id: 'my-brand-voice', name: 'Brand voice', description: 'Plain, friendly, no jargon', body: 'Write plainly.', enabled: true },
76  ]);
77  const bridge = createAgentBridge({ dataDir: dir, localInstances: () => [{ id: 'i', label: 'Scripted', baseUrl: worker.url, modelId: 'scripted-model', context: 32768, slots: 2 }], connections: () => [], skillsDir: () => live, env: process.env, startTimeoutMs: 90_000 });
78  t.after(async () => { await bridge.close(); await worker.close(); cleanup(dir); });
79  const frames = [];
80  await bridge.runTurn({ chatId: 'two', message: `SCRIPT:multi ${JSON.stringify({ calls: [['skill', { name: 'weekly-report' }], ['skill', { name: 'brand-voice' }]], say: 'Here is the note.' })}`, workspacePath: ws, mode: 'bypass', onFrame: f => frames.push(f) });
81  const last = frames.filter(f => f.type === 'skills-used').at(-1);
82  assert.deepEqual(last.skills.map(s => s.name).sort(), ['Brand voice', 'Weekly report']);
83  assert.match(frames.filter(f => f.type === 'delta').map(f => f.text).join(''), /Here is the note\./);
84  assert.ok(frames.findIndex(f => f.type === 'skills-used') < frames.findIndex(f => f.type === 'done'), 'the list arrives before the answer is done');
85});