okami.mom/strata

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

Niko1221merge f-split-agent4 4b5cc08d1 (skills end to end, New skill button, agent skills folder, goal-status race fix) into f-split-int19ea30f6a

main
6.6 KiB90 linesraw
1/**
2 * Skills, end to end: a skill written in the app is saved as a folder, written out for the agent, listed in the agent's skill catalog,
3 * loadable by name through the `skill` tool, and a skill added or switched off later reaches the NEXT message without a restart.
4 */
5import test from 'node:test';
6import assert from 'node:assert/strict';
7import { existsSync, mkdtempSync, mkdirSync, readFileSync, 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 { buildProfile } from '../server/agent/profile.mjs';
14import { materializeSkills, parseSkillFile, skillFileText, skillSlug, writeCreatedSkill } from '../server/skill-files.mjs';
15
16const skip = !process.env.STRATA_HARNESS_DIR || !resolveHarness({ env: process.env }).ok ? 'STRATA_HARNESS_DIR is not set to a built Harness' : false;
17const cleanup = dir => { try { rmSync(dir, { recursive: true, force: true, maxRetries: 3 }); } catch { /* best effort */ } };
18const INSTRUCTIONS = 'Open the Downloads folder, then open the Documents folder, using the command line. Say which folders you opened.';
19
20test('a skill name becomes the kebab-case name the agent requires, and its file carries what the agent reads', () => {
21  assert.equal(skillSlug('Directory opener'), 'directory-opener');
22  assert.equal(skillSlug('  Écrire des notes!! '), 'ecrire-des-notes');
23  assert.equal(skillSlug('???'), '');
24  const text = skillFileText({ name: 'directory-opener', title: 'Directory opener', description: 'Opens "folders"\nfor you', body: INSTRUCTIONS });
25  const parsed = parseSkillFile(text);
26  assert.deepEqual([parsed.name, parsed.title, parsed.description, parsed.body], ['directory-opener', 'Directory opener', 'Opens "folders" for you', INSTRUCTIONS]);
27  assert.match(skillFileText({ name: 'x', description: 'd', body: 'b', hidden: true }), /disable-model-invocation: true/);
28  assert.deepEqual(parseSkillFile('just text'), { name: null, title: null, description: null, body: 'just text' });
29});
30
31test('writing a skill refuses a duplicate and an empty name, and materializing makes the agent folder match the list exactly', () => {
32  const dir = mkdtempSync(join(tmpdir(), 'strata-skill-files-'));
33  try {
34    const mine = join(dir, 'mine');
35    assert.equal(writeCreatedSkill(mine, { name: 'Directory opener', description: 'Opens folders', body: INSTRUCTIONS }), 'directory-opener');
36    assert.throws(() => writeCreatedSkill(mine, { name: 'directory opener', body: 'x' }), /already have a skill/);
37    assert.throws(() => writeCreatedSkill(mine, { name: '!!!', body: 'x' }), /name with letters/);
38    const live = join(dir, 'live');
39    const list = [{ id: 'my-directory-opener', name: 'Directory opener', description: 'Opens folders', body: INSTRUCTIONS, enabled: true },
40      { id: 'commit-messages', name: 'Commit messages', description: 'Writes commits', body: 'Write them well.', enabled: false },
41      { id: 'ws-empty', name: 'Empty', body: '', enabled: true }];
42    assert.deepEqual(materializeSkills(live, list).sort(), ['commit-messages', 'directory-opener']);
43    assert.match(readFileSync(join(live, 'commit-messages', 'SKILL.md'), 'utf8'), /disable-model-invocation: true/);
44    assert.ok(!existsSync(join(live, 'empty')), 'a skill with no instructions is not offered');
45    mkdirSync(join(live, 'old-one'));
46    materializeSkills(live, list.slice(0, 1));
47    assert.ok(!existsSync(join(live, 'old-one')) && !existsSync(join(live, 'commit-messages')), 'removed skills disappear from the agent folder');
48  } finally { cleanup(dir); }
49});
50
51test('the agent profile tells the skills plugin to scan the app skill folder', () => {
52  const dir = '/data/agent/skills-live';
53  const entries = JSON.parse(buildProfile({ local: [], connections: [], skillsDir: dir }).patch.split('\n').slice(1).join('\n'));
54  const plugins = entries.find(e => e.id === 'preset-standard').config.plugins;
55  assert.deepEqual(plugins.find(p => p.id === 'skill-filesystem').config, { customSkillDirs: [dir] });
56  assert.ok(plugins.some(p => p.id === 'tool-skill'), 'the skill loader tool is in the preset');
57});
58
59test('real Harness: a skill saved in the app is in the catalog, loads by name, and one added later is seen without a restart', { skip }, async (t) => {
60  const worker = await startScriptedWorker({});
61  const dir = mkdtempSync(join(tmpdir(), 'strata-skills-'));
62  const ws = join(dir, 'ws'); mkdirSync(ws, { recursive: true });
63  const live = join(dir, 'skills-live');
64  const list = [{ id: 'my-directory-opener', name: 'Directory opener', description: 'Opens the Downloads and Documents folders', body: INSTRUCTIONS, enabled: true }];
65  materializeSkills(live, list);
66  const local = [{ id: 'inst-1', label: 'Scripted', baseUrl: worker.url, modelId: 'scripted-model', context: 32768, slots: 2 }];
67  const bridge = createAgentBridge({ dataDir: dir, localInstances: () => local, connections: () => [], skillsDir: () => live, env: process.env, startTimeoutMs: 90_000 });
68  t.after(async () => { await bridge.close(); await worker.close(); cleanup(dir); });
69  const run = async (chatId, message) => {
70    const frames = [];
71    await bridge.runTurn({ chatId, message, workspacePath: ws, mode: 'bypass', onFrame: f => frames.push(f) });
72    return frames.filter(f => f.type === 'agent-tool').map(f => f.tool);
73  };
74  await run('s1', 'SCRIPT:say hello');
75  const first = worker.requests.at(-1);
76  assert.ok(first.toolNames.includes('skill'), 'the agent has the skill loader');
77  assert.match(first.raw, /directory-opener/, 'the skill is in what the model is told');
78  const pid = bridge.status().runtime.pid;
79  const cards = await run('s1', `SCRIPT:call skill ${JSON.stringify({ name: 'directory-opener' })}`);
80  assert.equal(cards.at(-1).state, 'done');
81  assert.equal(cards.at(-1).verb, 'Loaded your skill', 'the card says it in the person own words');
82  assert.match(cards.at(-1).result, /Open the Downloads folder, then open the Documents folder/, 'loading it returns the instructions the person wrote');
83  // saved later, while the agent runs
84  list.push({ id: 'my-release-notes', name: 'Release notes', description: 'Writes release notes', body: 'Start with one line.', enabled: true });
85  materializeSkills(live, list);
86  let seen = false;
87  for (let i = 0; i < 20 && !seen; i++) { await new Promise(r => setTimeout(r, 500)); await run('s2', 'SCRIPT:say hi'); seen = /release-notes/.test(worker.requests.at(-1).raw); }
88  assert.ok(seen, 'a skill saved while the agent runs reaches a later message');
89  assert.equal(bridge.status().runtime.pid, pid, 'without restarting the agent');
90});