Files
stack/packages/webui/tests/assistant.test.mjs
T
jason.woltjeandClaude Opus 5.5 b2f57ca70d bus, business, harness, webui: the human and the chief of staff configure the assistant (row 155, Q169, #1649)
Jason's Q169: only the chief of staff removes the assistant; the chief
and the human configure it and its seat name.

Configure. A new organization key, assistants, replaces the vars,
tracker or definition of an instance whose definition is already the
assistant. holder and credentials are refused, and the whole document
still goes through validateBusinessDocument. set() refuses
organization-definition-refused if any other instance's definition
changed. THREAD_DEFINITION and END_PROTECTED move to
packages/business/src/vocabulary.mjs, so the broker and
launch-settings.mjs read one list. The human writes it with
mosaic limits set --file or the Console Settings view's Assistant
section; the chief writes it with change_organization. Both carry the
file's revision.

Seat name. The chief gets a typed set_name tool (name.set,
instance.name). The Console gets POST /api/name on the settings
boundary, through human-cli.

Remove. role.end and role.revoke of a threaded instance revoke every
thread of that instance whose latest claim is a claim, and report
threads: N. The raise and the check both record a missing holder run as
null. A new human-only verb, assistant.stop, revokes the human's own
threads of an assistant instance; the Console calls it from POST
/api/assistant-stop. The bundle, the end_run description and
roles/chief-of-staff.md now say the chief ends and configures the
assistant. No schema step.

Owner Darkwing, brief docs/plans/2026-10-16_q169-assistant-config.md.
Reviewers: Dewey (security) r1 approve 29012, r2 29014, r3 29018; Filbert r1 29010,
r2 29015, r3 29017, on candidate manifest 96cae28e...9016. Round 2 is
confirm-only after rows 162 and 125 moved the base under docs/TOOLS.md
and the cli README. Round 3 bumps row 164's webui/src/bus.mjs byte
baseline in packages/bus/tests/channel-proof.test.mjs for the eight
STATUS entries; Sage's gate on r2 failed only there. Mos's decision 123 go for the public/bus.js diff
(mos-0816) covers 9f9ce31b, unchanged here. Sage gate on 197722caa:
GATE_PASSED 25/25 (node 2370, selftests 422) 21:11:38Z, a rerun on the same tree after a first run stopped at pkg-webui on a board-held.test.mjs timeout that passes singly.

Nothing live. Follow-up brief in the packet: the names read rethrow, one
shared settings snapshot, null tests for the two new routes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-18 16:18:47 -05:00

94 lines
5.9 KiB
JavaScript

// Row 155 (#1649, Q169): the Console's seat name and assistant stop routes. Each has the Settings
// boundary and makes one human-transport call; the broker's own checks are in the bus tests.
import test from 'node:test';
import assert from 'node:assert/strict';
import { request } from 'node:http';
import { startServer } from '../src/serve.mjs';
import { BusReadError } from '../src/bus.mjs';
const preview = { host: 'console.example', header: 'x-user', user: 'jason' };
const headers = { host: 'console.example', 'x-user': 'jason', origin: 'https://console.example', 'sec-fetch-site': 'same-origin', 'content-type': 'application/json' };
async function setup(t, { on = true, name = async args => ({ ...args }), stop = async () => ({ stopped: [{ thread: 't-1', run: 'a-run' }] }), bus = null } = {}) {
const calls = [], lines = [];
const server = await startServer({
port: 0, preview, bus, log: line => lines.push(line), send: on ? async () => ({}) : null,
nameWrite: on ? async args => { calls.push(['name.set', args]); return name(args); } : null,
assistantStop: on ? async args => { calls.push(['assistant.stop', args]); return stop(args); } : null,
});
t.after(() => new Promise(r => server.close(r)));
return { calls, lines, port: server.address().port };
}
const send = (s, path, data, hs = headers, method = 'POST') => new Promise((resolve, reject) => {
const req = request({ hostname: '127.0.0.1', port: s.port, path, method, headers: hs }, r => {
let text = ''; r.on('data', c => text += c); r.on('end', () => resolve({ status: r.statusCode, body: text ? JSON.parse(text) : null }));
});
req.on('error', reject);
req.end(data === undefined ? undefined : typeof data === 'string' ? data : JSON.stringify(data));
});
test('Q169 the signed-in human names a seat and stops the assistant, one bus call each, logged with the user', async t => {
const s = await setup(t);
const named = await send(s, '/api/name', { instance: 'assistant', name: 'piper' });
assert.equal(named.status, 200);
assert.equal(named.body.name, 'piper');
const stopped = await send(s, '/api/assistant-stop', { instance: 'assistant' });
assert.equal(stopped.status, 200);
assert.deepEqual(stopped.body.stopped, [{ thread: 't-1', run: 'a-run' }]);
assert.deepEqual(s.calls, [['name.set', { instance: 'assistant', name: 'piper' }], ['assistant.stop', { instance: 'assistant' }]]);
assert.match(s.lines[0], / name user="jason" instance="assistant" done$/);
assert.match(s.lines[1], / assistant-stop user="jason" instance="assistant" done$/);
});
test('Q169 the name and stop routes need send on, the public name, the user, the exact Origin and same-origin Fetch Metadata', async t => {
const off = await setup(t, { on: false });
for (const path of ['/api/name', '/api/assistant-stop']) assert.equal((await send(off, path, { instance: 'assistant' })).status, 403);
const s = await setup(t);
for (const path of ['/api/name', '/api/assistant-stop']) {
const data = path === '/api/name' ? { instance: 'assistant', name: 'piper' } : { instance: 'assistant' };
for (const [key, value, status] of [['host', 'wrong.example', 403], ['x-user', 'other', 401], ['x-user', ['jason', 'jason'], 401], ['origin', 'https://else.example', 403], ['origin', undefined, 403], ['sec-fetch-site', 'cross-site', 403], ['sec-fetch-site', undefined, 403]]) {
const hs = { ...headers };
if (value === undefined) delete hs[key]; else hs[key] = value;
assert.equal((await send(s, path, data, hs)).status, status, `${path} ${key}`);
}
assert.equal((await send(s, path, undefined, headers, 'GET')).status, 405);
}
assert.equal(s.calls.length, 0);
});
test('Q169 the name and stop routes take JSON within 4 KiB and only their own keys, all text', async t => {
const s = await setup(t);
for (const value of [{ instance: 'assistant' }, { instance: 'assistant', name: 'piper', by: 'jason' }, { instance: 'assistant', name: 7 }, [], 'bad', { instance: 'assistant', name: 'x'.repeat(5000) }])
assert.equal((await send(s, '/api/name', value)).status, 400, JSON.stringify(value).slice(0, 40));
for (const value of [{}, { instance: 'assistant', thread: 't-1' }, { instance: null }])
assert.equal((await send(s, '/api/assistant-stop', value)).status, 400, JSON.stringify(value));
assert.equal((await send(s, '/api/name', { instance: 'assistant', name: 'piper' }, { ...headers, 'content-type': 'text/plain' })).status, 400);
assert.equal(s.calls.length, 0);
});
test('Q169 a bus refusal says nothing changed; a lost or odd answer says the change may have been made', async t => {
const fail = code => async () => { throw new BusReadError(code); };
for (const [stop, status, error, sent] of [
[fail('not-held'), 409, 'not-held', false], [fail('human-required'), 403, 'human-required', false],
[fail('not-assistant'), 400, 'not-assistant', false], [fail('outcome-unknown'), 503, 'outcome-unknown', 'unknown'],
[async () => ({ stopped: 'all' }), 503, 'outcome-unknown', 'unknown'],
]) {
const s = await setup(t, { stop });
const r = await send(s, '/api/assistant-stop', { instance: 'assistant' });
assert.deepEqual([r.status, r.body.error, r.body.sent], [status, error, sent]);
assert.equal(s.calls.length, 1);
assert.match(s.lines[0], new RegExp(` error=${error}$`));
}
const s = await setup(t, { name: fail('name-taken') });
const r = await send(s, '/api/name', { instance: 'assistant', name: 'piper' });
assert.deepEqual([r.status, r.body.error, r.body.sent], [409, 'name-taken', false]);
});
test('Q169 the names read is a bus read like the others', async t => {
const rows = [{ instance: 'assistant', name: 'piper', at: '2026-10-18T00:00:00.000Z' }];
const s = await setup(t, { bus: { names: async () => rows } });
const r = await send(s, '/api/bus/names', undefined, { host: 'console.example', 'x-user': 'jason' }, 'GET');
assert.equal(r.status, 200);
assert.deepEqual(r.body.rows, rows);
});