Compare commits
28
Commits
settings-s5
...
refactor
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
29daa4c4bf | ||
|
|
c8db3665cf | ||
|
|
f2c262cf40 | ||
|
|
ebfbdb13a0 | ||
|
|
4d378fc38b | ||
|
|
ab8633ede8 | ||
|
|
6ad11e6993 | ||
|
|
e52a6b714f | ||
|
|
2d065b936e | ||
|
|
098b3771c0 | ||
|
|
eac42663ba | ||
|
|
b41bc00b26 | ||
|
|
749d6d98f5 | ||
|
|
fa5ce3ae65 | ||
|
|
d009ccaec4 | ||
|
|
2613fb1c4c | ||
|
|
df26085303 | ||
|
|
7c1b87d154 | ||
|
|
8908df113e | ||
|
|
b8354e4304 | ||
|
|
f62b1b9518 | ||
|
|
7355b233a8 | ||
|
|
88d08c8437 | ||
|
|
4377941bd4 | ||
|
|
de39750ebb | ||
|
|
3b17326b74 | ||
|
|
d03ae494e8 | ||
|
|
e1b56d9ea7 |
No files matched your search
+12
-4
@@ -1,6 +1,6 @@
|
||||
#!/bin/sh
|
||||
# Pi adapter: implements the Mosaic adapter contract for the pinned
|
||||
# @earendil-works/pi-coding-agent CLI.
|
||||
# Pi adapter: implements the Mosaic adapter contract for the
|
||||
# @earendil-works/pi-coding-agent CLI. Its behaviour notes are read from 0.85.1.
|
||||
#
|
||||
# Contract: see /opt/mosaic/adapters/README.md.
|
||||
# Headless (default): stdout = response only; stderr = diagnostics; exit 0.
|
||||
@@ -92,6 +92,11 @@ fi
|
||||
THINKING_FLAG=""
|
||||
[ -n "${MOSAIC_THINKING:-}" ] && THINKING_FLAG="--thinking $MOSAIC_THINKING"
|
||||
|
||||
# MCP (#1873 S3): the harness module sets MOSAIC_PI_NO_MCP=1 when the binary's --help lists
|
||||
# --no-mcp, so MCP stays off by its own flag and not only as a side effect of --no-extensions.
|
||||
NO_MCP_FLAG=""
|
||||
[ "${MOSAIC_PI_NO_MCP:-}" = "1" ] && NO_MCP_FLAG="--no-mcp"
|
||||
|
||||
# Mode (M13): interactive TUI or one-shot print.
|
||||
PRINT_MODE="-p"
|
||||
REQUEST_ARG=""
|
||||
@@ -107,6 +112,7 @@ fi
|
||||
# --system-prompt replace the default prompt with the generated one, given
|
||||
# as the file's absolute path (checked above), never its text
|
||||
# --no-* no ambient context/skills/extensions/templates/themes
|
||||
# NO_MCP_FLAG --no-mcp when the binary lists it, else nothing
|
||||
# EXT_FLAGS the explicitly provided extensions only (-e)
|
||||
# SESSION_FLAGS ephemeral | persistent | forked (per env)
|
||||
# TOOLS_FLAG per capabilities
|
||||
@@ -118,6 +124,7 @@ set -- \
|
||||
--offline \
|
||||
--no-approve \
|
||||
--no-extensions \
|
||||
$NO_MCP_FLAG \
|
||||
$EXT_FLAGS \
|
||||
$SKILLS_FLAG \
|
||||
--no-prompt-templates \
|
||||
@@ -134,6 +141,7 @@ set -- \
|
||||
[ "$INTERACTIVE" = "1" ] || set -- "$@" -p "$MOSAIC_REQUEST"
|
||||
if [ -n "${MOSAIC_MODEL_EVENT_MONITOR:-}" ] && [ "$INTERACTIVE" != "1" ]; then
|
||||
STREAM="$(dirname "$0")/../../packages/plugins/harness/pi/model-stream.mjs"
|
||||
exec node "$STREAM" pi "$MOSAIC_FIRST_OUTPUT_DIR" pi "$@" --mode json
|
||||
exec node "$STREAM" pi "$MOSAIC_FIRST_OUTPUT_DIR" "${MOSAIC_PI_BIN:-pi}" "$@" --mode json
|
||||
fi
|
||||
exec pi "$@"
|
||||
# #1873 S2: the binary the harness module found (MOSAIC_PI_BIN, its path inside the sandbox).
|
||||
exec "${MOSAIC_PI_BIN:-pi}" "$@"
|
||||
@@ -3,7 +3,7 @@ import { randomBytes } from 'node:crypto';
|
||||
import { isDeepStrictEqual } from 'node:util';
|
||||
import { sha256, refuse } from '../../model-proxy/src/common.mjs';
|
||||
import { PROXY_PAGE_CODES } from './proxy-page-vocabulary.mjs';
|
||||
import { ZAI_URL, MODEL_ID } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
import { MODEL_ID, PROVIDERS, publicProviders } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
export { PROXY_PAGE_VERBS, PROXY_PAGE_CODES } from './proxy-page-vocabulary.mjs';
|
||||
const object = x => x !== null && typeof x === 'object' && !Array.isArray(x);
|
||||
function fields(args, names) {
|
||||
@@ -13,11 +13,16 @@ const priorityOk = n => Number.isInteger(n) && n >= -1000 && n <= 1000;
|
||||
const identifier = x => typeof x === 'string' && x.length > 0 && x.length <= 512;
|
||||
const stateOk = x => typeof x === 'string' && /^[a-zA-Z0-9_-]{1,512}$/.test(x);
|
||||
const keyId = value => sha256(value).slice(0, 12);
|
||||
const providerName = provider => ({ claude: 'Claude', codex: 'Codex', 'z.ai': 'z.ai', other: 'Other' })[provider];
|
||||
// #1873 settings-backend-a3: provider names, the login allow list and the key allow list all come
|
||||
// from the plugin's PROVIDERS. An account the list does not name reads Other.
|
||||
// An entry counts only with what its door needs: a sign-in host, or a key base URL.
|
||||
const usable = p => p.signIn === 'oauth' ? typeof p.signInHost === 'string' : p.signIn === 'apikey' && typeof p.baseUrl === 'string';
|
||||
const listed = (providers, id, signIn) => providers.find(p => p.id === id && p.signIn === signIn && usable(p)) ?? null;
|
||||
const providerName = (providers, provider) => providers.find(p => p.id === provider)?.label ?? 'Other';
|
||||
// Credential labels/emails and group names are arbitrary upstream strings. A stable
|
||||
// generated display name provides identification without reflecting a misplaced token.
|
||||
function view(id, provider, status, priority) {
|
||||
return { accountId: keyId(id), provider, displayName: `${providerName(provider)} account ${keyId(id)}`,
|
||||
function view(providers, id, provider, status, priority) {
|
||||
return { accountId: keyId(id), provider, displayName: `${providerName(providers, provider)} account ${keyId(id)}`,
|
||||
status, priority: priorityOk(priority) ? priority : 0 };
|
||||
}
|
||||
// #1873 chat-fail F3: one plain line under an account the proxy reports as not working, with
|
||||
@@ -57,11 +62,12 @@ function accountReason(row, now) {
|
||||
return NO_REASON;
|
||||
}
|
||||
export class ProxyPage {
|
||||
#client; #tag; #records; #lock; #org; #dirty = false; #now; #timeout; #onChange; #pending = new Map(); #writes = Promise.resolve();
|
||||
constructor({ client, instanceTag, keyRecords = () => [], now = Date.now, loginTimeoutSeconds = 300, onChange = () => {}, lock = null, org = null }) {
|
||||
#client; #tag; #records; #lock; #org; #dirty = false; #now; #timeout; #onChange; #providers; #pending = new Map(); #writes = Promise.resolve();
|
||||
// providers: the plugin's PROVIDERS; a test passes a copy with one taken out.
|
||||
constructor({ client, instanceTag, keyRecords = () => [], now = Date.now, loginTimeoutSeconds = 300, onChange = () => {}, lock = null, org = null, providers = PROVIDERS }) {
|
||||
if (!client || !/^[a-zA-Z0-9_-]{1,64}$/.test(instanceTag) || typeof keyRecords !== 'function' || typeof now !== 'function'
|
||||
|| typeof onChange !== 'function' || !Number.isSafeInteger(loginTimeoutSeconds) || loginTimeoutSeconds <= 0) throw new TypeError('invalid proxy page settings');
|
||||
this.#lock = lock; this.#org = org;
|
||||
|| typeof onChange !== 'function' || !Number.isSafeInteger(loginTimeoutSeconds) || loginTimeoutSeconds <= 0 || !Array.isArray(providers)) throw new TypeError('invalid proxy page settings');
|
||||
this.#lock = lock; this.#org = org; this.#providers = providers;
|
||||
this.#client = client; this.#tag = instanceTag; this.#records = keyRecords; this.#now = now; this.#timeout = loginTimeoutSeconds * 1000; this.#onChange = onChange;
|
||||
}
|
||||
// Serializes read-modify-write provider config within the bus. P2 owns the
|
||||
@@ -84,10 +90,10 @@ export class ProxyPage {
|
||||
const rows = [];
|
||||
for (const row of response.files) {
|
||||
if (!identifier(row?.id) || !identifier(row?.name)) continue;
|
||||
const provider = ['claude', 'codex'].includes(row.provider) ? row.provider : 'other';
|
||||
const provider = listed(this.#providers, row.provider, 'oauth') ? row.provider : 'other';
|
||||
const status = row.disabled === true ? 'disabled' : row.unavailable === true ? 'unavailable'
|
||||
: ['ready', 'active', 'error', 'disabled', 'unavailable'].includes(row.status) ? row.status : 'unknown';
|
||||
rows.push({ ...view(row.id, provider, status, row.priority), source: 'oauth', name: row.name,
|
||||
rows.push({ ...view(this.#providers, row.id, provider, status, row.priority), source: 'oauth', name: row.name,
|
||||
...(['unavailable', 'error'].includes(status) ? { reason: accountReason(row, this.#now()) } : {}) });
|
||||
}
|
||||
for (const [groupIndex, group] of groups.entries()) {
|
||||
@@ -95,18 +101,18 @@ export class ProxyPage {
|
||||
for (const [keyIndex, key] of group.keys.entries()) {
|
||||
// auth_index is generated by the proxy. Never fall back to hashing api-key.
|
||||
if (!identifier(key?.auth_index)) continue;
|
||||
const provider = group['base-url'] === ZAI_URL ? 'z.ai' : 'other';
|
||||
const provider = this.#providers.find(p => p.signIn === 'apikey' && usable(p) && p.baseUrl === group['base-url'])?.id ?? 'other';
|
||||
const status = key.disabled === true || group.disabled === true ? 'disabled' : 'active';
|
||||
const configured = group.models ?? [];
|
||||
if (!Array.isArray(configured) || configured.length > 32 || configured.some(m => !m || typeof (m.alias || m.name) !== 'string' || !MODEL_ID.test(m.alias || m.name))) refuse('proxy-down');
|
||||
rows.push({ ...view(key.auth_index, provider, status, key.priority ?? group.priority), source: 'apikey', groupIndex, keyIndex, models: configured.map(m => m.alias || m.name) });
|
||||
rows.push({ ...view(this.#providers, key.auth_index, provider, status, key.priority ?? group.priority), source: 'apikey', groupIndex, keyIndex, models: configured.map(m => m.alias || m.name) });
|
||||
}
|
||||
}
|
||||
return { rows, groups };
|
||||
}
|
||||
#public(row) {
|
||||
const { accountId, provider, displayName, status, priority } = row;
|
||||
return { accountId, provider, displayName, status, priority, ...(row.source === 'apikey' && provider === 'z.ai' ? { models: row.models } : {}),
|
||||
return { accountId, provider, displayName, status, priority, ...(row.source === 'apikey' && listed(this.#providers, provider, 'apikey') ? { models: row.models } : {}),
|
||||
...(typeof row.reason === 'string' ? { reason: row.reason } : {}) };
|
||||
}
|
||||
#session(state, scope, { reportTimeout = false } = {}) {
|
||||
@@ -131,17 +137,18 @@ export class ProxyPage {
|
||||
async #request(verb, args, scope) {
|
||||
if (verb === 'proxy.accounts') {
|
||||
fields(args, []);
|
||||
return { accounts: (await this.#accounts()).rows.map(row => this.#public(row)) };
|
||||
// The provider list rides along for the page's select: id, label and sign-in kind only.
|
||||
return { accounts: (await this.#accounts()).rows.map(row => this.#public(row)), providers: publicProviders(this.#providers) };
|
||||
}
|
||||
if (verb === 'proxy.login.start') {
|
||||
fields(args, ['provider']);
|
||||
if (!['claude', 'codex'].includes(args.provider)) refuse('provider-not-allowed');
|
||||
const oauth = listed(this.#providers, args.provider, 'oauth');
|
||||
if (!oauth) refuse('provider-not-allowed');
|
||||
for (const [state, value] of this.#pending) if (value.expires <= this.#now()) this.#pending.delete(state);
|
||||
if (this.#pending.size >= 128) refuse('proxy-down');
|
||||
const answer = await (this.#lock ? this.#write(() => this.#client.oauthStart(args.provider)) : this.#client.oauthStart(args.provider));
|
||||
let url; try { url = new URL(answer.url); } catch { refuse('proxy-down'); }
|
||||
const host = args.provider === 'claude' ? 'claude.ai' : 'auth.openai.com';
|
||||
if (url.protocol !== 'https:' || url.hostname !== host || url.username || url.password || url.port
|
||||
if (url.protocol !== 'https:' || typeof oauth.signInHost !== 'string' || url.hostname !== oauth.signInHost || url.username || url.password || url.port
|
||||
|| url.href.length > 8192 || !stateOk(answer.state) || url.searchParams.getAll('state').length !== 1
|
||||
|| url.searchParams.get('state') !== answer.state || this.#pending.has(answer.state)) refuse('proxy-down');
|
||||
this.#pending.set(answer.state, { business: scope.business, human: scope.human, expires: this.#now() + this.#timeout });
|
||||
@@ -186,12 +193,13 @@ export class ProxyPage {
|
||||
}
|
||||
if (verb === 'proxy.apikey.add') {
|
||||
fields(args, ['provider', 'key']);
|
||||
if (args.provider !== 'z.ai') refuse('provider-not-allowed');
|
||||
const keyed = listed(this.#providers, args.provider, 'apikey');
|
||||
if (!keyed) refuse('provider-not-allowed');
|
||||
if (typeof args.key !== 'string' || !/^[\x21-\x7e]{1,4096}$/.test(args.key)) refuse('apikey-invalid');
|
||||
return this.#write(async () => {
|
||||
const groups = await this.#client.getUpstreamGroups();
|
||||
if (!Array.isArray(groups)) refuse('proxy-down');
|
||||
const group = { name: `mosaic-zai-${randomBytes(6).toString('hex')}`, 'base-url': ZAI_URL,
|
||||
const group = { name: `mosaic-${keyed.id.replace(/[^a-z0-9]/g, '')}-${randomBytes(6).toString('hex')}`, 'base-url': keyed.baseUrl,
|
||||
priority: 0, keys: [{ 'api-key': args.key }] };
|
||||
if (this.#lock && !isDeepStrictEqual(await this.#client.getUpstreamGroups(), groups)) refuse('proxy-down');
|
||||
this.#beforeMutation('proxy.apikey.add');
|
||||
|
||||
@@ -6,6 +6,7 @@ import { join } from 'node:path';
|
||||
import { sandboxProblem } from '../../seat/src/sandbox.mjs';
|
||||
import { startTimeOf } from '../../seat/src/proc.mjs';
|
||||
import { ownershipFixture, waitFor } from './model-hire-ownership-fixture.mjs';
|
||||
import * as pi from '../../plugins/harness/pi/index.mjs';
|
||||
const unavailable=sandboxProblem()??false; // Any skip means UNVERIFIED, not green acceptance.
|
||||
const done=async box=>{await box.promise;return box;};
|
||||
const refused=async(box,code)=>{await done(box);assert.equal(box.ok,false,'real operation refuses');assert.equal(box.code,code,'real operation preserves exact refusal');};
|
||||
@@ -17,7 +18,12 @@ test('655 listed duplicate with no record refuses before any model or management
|
||||
assert.deepEqual(f.snapshot(),before,'duplicate hire makes no credential or management effects');f.assertHistoryGuard();f.valueFree();
|
||||
});
|
||||
|
||||
test('655 actual launch own preflight generation and bound consumer survive concurrent duplicate hire',{skip:unavailable,timeout:30000},async t=>{
|
||||
test('655 actual launch own preflight generation and bound consumer survive concurrent duplicate hire',{skip:unavailable,timeout:120000},async t=>{
|
||||
// #1873 pi-unpin re-check 9 and 10: the launch's preflight shares pi's fit check, which is cold in
|
||||
// a fresh process. Wait for it before the fixture starts its launcher, so the launcher's warm-up
|
||||
// and the launch's preflight hit the cache, the gate's 5 s times the launch, and the launch starts
|
||||
// at the same point in the launcher's 2 s tick as it did before the fit check existed.
|
||||
await pi.ready({env:{PATH:process.env.PATH,HOME:process.env.HOME,TMPDIR:process.env.TMPDIR}});
|
||||
const f=await ownershipFixture(t),pGet=f.hold('GET'),p=f.launchAide();await pGet.entered();
|
||||
// P's own acquisition GET is held. Its PUT confirmation GET is ordinal+1;
|
||||
// only an OLD H reuse GET can be ordinal+2. The correct early refusal removes it.
|
||||
|
||||
@@ -4,6 +4,7 @@ import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { fakeManagement } from './proxy-page-fixture.mjs';
|
||||
import { PROVIDERS } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
const id = value => createHash('sha256').update(value).digest('hex').slice(0, 12);
|
||||
const oauth = extra => ({ id: 'oauth-one.json', name: 'oauth-one.json', provider: 'claude', label: 'p4-private-label',
|
||||
email: 'p4-private-email', token: 'p4-private-token', status: 'ready', priority: 3, ...extra });
|
||||
@@ -11,7 +12,9 @@ test('accounts have exactly the allowlisted public fields, id from credential id
|
||||
const s = await fakeManagement(t, { files: [oauth()] });
|
||||
const result = await s.call('proxy.accounts');
|
||||
assert.deepEqual(result, { accounts: [{ accountId: id('oauth-one.json'), provider: 'claude',
|
||||
displayName: `Claude account ${id('oauth-one.json')}`, status: 'ready', priority: 3 }] });
|
||||
displayName: `Claude account ${id('oauth-one.json')}`, status: 'ready', priority: 3 }],
|
||||
// settings-backend-a3: the provider list rides along, id, label and sign-in kind only.
|
||||
providers: PROVIDERS.map(({ id, label, signIn }) => ({ id, label, signIn })) });
|
||||
for (const value of ['p4-private-label', 'p4-private-email', 'p4-private-token']) {
|
||||
assert.equal(JSON.stringify(result).includes(value), false);
|
||||
assert.notEqual(result.accounts[0].accountId, id(value));
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
// #1873 settings-backend-a3: the proxy page's provider names, login allow list, sign-in host and key
|
||||
// allow list all come from the plugin's PROVIDERS. A provider a test copy of the list leaves out is
|
||||
// refused at each door with the same code as before; one a copy adds is let through. The accounts
|
||||
// answer carries the list as id, label and sign-in kind only.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { ProxyPage } from '../src/proxy-page.mjs';
|
||||
import { PROVIDERS } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
const owner = { business: 'demo', human: 'owner' };
|
||||
function setup(options = {}) {
|
||||
const calls = [], state = { files: [], groups: [] };
|
||||
const client = Object.fromEntries(['getCredentials', 'oauthStart', 'oauthCallback', 'oauthStatus', 'oauthCancel', 'getUpstreamGroups', 'putUpstreamGroups'].map(name => [name, async (...args) => {
|
||||
calls.push([name, ...args]);
|
||||
if (name === 'oauthStart') return { status: 'ok', state: `s-${calls.length}`, url: `https://${options.hosts?.[args[0]] ?? 'nowhere.invalid'}/oauth/authorize?state=s-${calls.length}` };
|
||||
if (name === 'getCredentials') return { files: state.files };
|
||||
if (name === 'getUpstreamGroups') return structuredClone(state.groups);
|
||||
if (name === 'putUpstreamGroups') { state.groups = args[0].map((g, i) => ({ ...g, keys: g.keys.map((k, j) => ({ ...k, auth_index: `idx-${i}-${j}` })) })); return { status: 'ok' }; }
|
||||
return { status: 'ok' };
|
||||
}]));
|
||||
const p = new ProxyPage({ client, instanceTag: 'fixture', ...(options.providers ? { providers: options.providers } : {}) });
|
||||
return { calls, state, call: (verb, args = {}) => p.request(verb, args, owner) };
|
||||
}
|
||||
const hosts = Object.fromEntries(PROVIDERS.filter(p => p.signIn === 'oauth').map(p => [p.id, p.signInHost]));
|
||||
const door = p => p.signIn === 'oauth' ? ['proxy.login.start', { provider: p.id }] : ['proxy.apikey.add', { provider: p.id, key: 'fixture-key' }];
|
||||
const otherDoor = p => p.signIn === 'oauth' ? ['proxy.apikey.add', { provider: p.id, key: 'fixture-key' }] : ['proxy.login.start', { provider: p.id }];
|
||||
|
||||
test('a3: each provider in PROVIDERS passes its own door and is refused at the other with provider-not-allowed', async () => {
|
||||
assert.ok(PROVIDERS.some(p => p.signIn === 'oauth') && PROVIDERS.some(p => p.signIn === 'apikey'));
|
||||
for (const p of PROVIDERS) {
|
||||
const s = setup({ hosts });
|
||||
const answer = await s.call(...door(p));
|
||||
if (p.signIn === 'oauth') assert.equal(new URL(answer.url).hostname, p.signInHost, p.id);
|
||||
else { assert.deepEqual(answer, { added: true }); assert.equal(s.state.groups.at(-1)['base-url'], p.baseUrl); }
|
||||
const before = s.calls.length;
|
||||
await assert.rejects(s.call(...otherDoor(p)), { code: 'provider-not-allowed' }, p.id);
|
||||
assert.equal(s.calls.length, before, `${p.id}: a refusal reaches no proxy call`);
|
||||
}
|
||||
for (const [verb, args] of [['proxy.login.start', { provider: 'other' }], ['proxy.apikey.add', { provider: 'other', key: 'fixture-key' }]]) {
|
||||
const s = setup({ hosts });
|
||||
await assert.rejects(s.call(verb, args), { code: 'provider-not-allowed' }); assert.equal(s.calls.length, 0);
|
||||
}
|
||||
});
|
||||
|
||||
test('a3: a provider taken out of a test copy of the list is refused at its door, reads Other and leaves the providers answer', async () => {
|
||||
for (const gone of PROVIDERS) {
|
||||
const s = setup({ hosts, providers: PROVIDERS.filter(p => p !== gone) });
|
||||
await assert.rejects(s.call(...door(gone)), { code: 'provider-not-allowed' }, gone.id);
|
||||
assert.equal(s.calls.length, 0, `${gone.id}: refused before any proxy call`);
|
||||
if (gone.signIn === 'oauth') s.state.files = [{ id: 'one.json', name: 'one.json', provider: gone.id, status: 'ready' }];
|
||||
else s.state.groups = [{ name: 'g', 'base-url': gone.baseUrl, keys: [{ auth_index: 'idx-0-0' }], models: [{ name: 'm1' }] }];
|
||||
const answer = await s.call('proxy.accounts');
|
||||
assert.equal(answer.accounts.length, 1);
|
||||
assert.equal(answer.accounts[0].provider, 'other', gone.id);
|
||||
assert.match(answer.accounts[0].displayName, /^Other account /);
|
||||
assert.equal(Object.hasOwn(answer.accounts[0], 'models'), false, `${gone.id}: an unlisted group shows no models`);
|
||||
assert.deepEqual(answer.providers.map(p => p.id), PROVIDERS.filter(p => p !== gone).map(p => p.id));
|
||||
}
|
||||
});
|
||||
|
||||
test('a3: a provider a test copy adds is let through: key group from its base URL, sign-in on its host, name from its label', async () => {
|
||||
const extra = [{ id: 'fixture.ai', label: 'Fixture AI', signIn: 'apikey', baseUrl: 'https://api.fixture.invalid/v1' },
|
||||
{ id: 'fixture-sso', label: 'Fixture SSO', signIn: 'oauth', signInHost: 'sso.fixture.invalid' }];
|
||||
const s = setup({ providers: [...PROVIDERS, ...extra], hosts: { ...hosts, 'fixture-sso': 'sso.fixture.invalid' } });
|
||||
assert.deepEqual(await s.call('proxy.apikey.add', { provider: 'fixture.ai', key: 'fixture-key' }), { added: true });
|
||||
assert.equal(s.state.groups[0]['base-url'], 'https://api.fixture.invalid/v1');
|
||||
assert.match(s.state.groups[0].name, /^mosaic-fixtureai-[0-9a-f]{12}$/);
|
||||
assert.equal(new URL((await s.call('proxy.login.start', { provider: 'fixture-sso' })).url).hostname, 'sso.fixture.invalid');
|
||||
s.state.files = [{ id: 'sso.json', name: 'sso.json', provider: 'fixture-sso', status: 'ready' }];
|
||||
const { accounts } = await s.call('proxy.accounts');
|
||||
assert.deepEqual(accounts.map(a => [a.provider, a.displayName.replace(/ [0-9a-f]{12}$/, '')]), [['fixture-sso', 'Fixture SSO account'], ['fixture.ai', 'Fixture AI account']]);
|
||||
assert.deepEqual(accounts[1].models, []);
|
||||
});
|
||||
|
||||
test('a3: the sign-in address must be on the host the list names for that provider', async () => {
|
||||
const codex = PROVIDERS.find(p => p.id === 'codex');
|
||||
const moved = PROVIDERS.map(p => p === codex ? { ...p, signInHost: 'elsewhere.invalid' } : p);
|
||||
const s = setup({ hosts, providers: moved });
|
||||
await assert.rejects(s.call('proxy.login.start', { provider: 'codex' }), { code: 'proxy-down' });
|
||||
const t = setup({ hosts: { ...hosts, codex: 'elsewhere.invalid' }, providers: moved });
|
||||
assert.equal(new URL((await t.call('proxy.login.start', { provider: 'codex' })).url).hostname, 'elsewhere.invalid');
|
||||
});
|
||||
|
||||
test('a3: the accounts answer carries the providers as id, label and sign-in kind only, no URL, host or key', async () => {
|
||||
const s = setup({ hosts });
|
||||
await s.call('proxy.apikey.add', { provider: 'z.ai', key: 'a3-canary-key-value' });
|
||||
const answer = await s.call('proxy.accounts');
|
||||
assert.deepEqual(answer.providers, PROVIDERS.map(({ id, label, signIn }) => ({ id, label, signIn })));
|
||||
for (const p of answer.providers) assert.deepEqual(Object.keys(p).sort(), ['id', 'label', 'signIn']);
|
||||
const text = JSON.stringify(answer.providers);
|
||||
for (const word of ['a3-canary-key-value', 'https://', 'api.z.ai', 'claude.ai', 'auth.openai.com', 'baseUrl', 'signInHost']) assert.equal(text.includes(word), false, word);
|
||||
});
|
||||
|
||||
// Review P3-1: an entry missing what its door needs is refused before any proxy call, so no sign-in
|
||||
// is opened at the proxy that the page could not cancel; a key group with no base URL is no provider's.
|
||||
test('a3: an entry with no sign-in host or no key base URL is refused before any proxy call and names no group', async () => {
|
||||
const bare = [{ id: 'nohost', label: 'No Host', signIn: 'oauth' }, { id: 'nourl', label: 'No URL', signIn: 'apikey' }];
|
||||
const s = setup({ providers: [...PROVIDERS, ...bare], hosts: { ...hosts, nohost: 'nohost.invalid' } });
|
||||
await assert.rejects(s.call('proxy.login.start', { provider: 'nohost' }), { code: 'provider-not-allowed' });
|
||||
await assert.rejects(s.call('proxy.apikey.add', { provider: 'nourl', key: 'fixture-key' }), { code: 'provider-not-allowed' });
|
||||
assert.equal(s.calls.length, 0);
|
||||
s.state.groups = [{ name: 'g', keys: [{ auth_index: 'idx-0-0' }], models: [{ name: 'm1' }] }];
|
||||
s.state.files = [{ id: 'one.json', name: 'one.json', provider: 'nohost', status: 'ready' }];
|
||||
const { accounts } = await s.call('proxy.accounts');
|
||||
assert.deepEqual(accounts.map(a => a.provider), ['other', 'other']);
|
||||
assert.equal(accounts.some(a => Object.hasOwn(a, 'models')), false);
|
||||
});
|
||||
@@ -215,12 +215,19 @@ const relayed = (failed) => failed && (typeof failed.message === "string" ? { ..
|
||||
// and names the module rather than escaping the host. What it threw is named by its system code
|
||||
// or its class, never its message, which the host didn't write (#1873).
|
||||
function call(name, what, fn) {
|
||||
try {
|
||||
return fn();
|
||||
} catch (e) {
|
||||
const failed = (e) => {
|
||||
const thrown = quietMessage(e, "a host file") ?? `threw ${/^[A-Za-z][A-Za-z0-9]{0,39}$/.test(String(e?.name)) ? e.name : "a value"} without a system code`;
|
||||
throw new Refusal("harness-failed", `harness ${name} ${what}: ${thrown}`);
|
||||
};
|
||||
let out;
|
||||
try {
|
||||
out = fn();
|
||||
} catch (e) {
|
||||
failed(e);
|
||||
}
|
||||
// #1873 pi-unpin re-check 7: a module may answer with a promise (pi's preflight runs its fit
|
||||
// check as child processes). The caller awaits it; a rejection is refused as a throw is.
|
||||
return typeof out?.then === "function" ? Promise.resolve(out).then(undefined, failed) : out;
|
||||
}
|
||||
|
||||
// host: startHost()'s handle. tracker: true when the broker has task verbs for
|
||||
@@ -454,14 +461,11 @@ export function createLauncher({
|
||||
for (const m of [primary, fallback].filter(Boolean)) {
|
||||
if (!on.includes(m.harness)) throw new Refusal("harness-off", `harness ${m.harness} isn't switched on in plugins.harness`);
|
||||
}
|
||||
// One harness through a fresh loader, so a refusal never leaves an earlier load's module in use.
|
||||
const harnessOf = async (m) => {
|
||||
const loader = makeLoader({ dataRoot:system.instanceRoot ?? dataRoot, log: (entry) => record({ ...entry, instance }) });
|
||||
const { refused } = await loader.load({ harness: [m.harness] });
|
||||
const { refused, module } = await freshHarness(m, (entry) => record({ ...entry, instance }));
|
||||
// #1873: a loader message can quote the module's path or its load error; only a
|
||||
// missing-export one, which names the export, is relayed. Any other refusal names its code.
|
||||
if (refused.length) throw new Refusal("harness-refused", `harness ${m.harness}: ${refused.map((r) => r.code === "missing-export" ? `${r.code}: ${relay(r.message)}` : String(r.code)).join("; ")}`);
|
||||
const module = loader.modules("harness")[0];
|
||||
const levels = call(m.harness, "levels", () => module.levels());
|
||||
if (!levels.includes(m.thinking)) throw new Refusal("thinking-unsupported", `harness ${m.harness} takes thinking ${levels.join(", ")}, not ${m.thinking}`);
|
||||
return module;
|
||||
@@ -556,7 +560,7 @@ export function createLauncher({
|
||||
const pickRoute = async (proxyOf) => {
|
||||
let sessionProxy = await proxyOf(primary);
|
||||
// #1873: a preflight's message is the module's own text, relayed with host paths replaced.
|
||||
const failed = relayed(call(primary.harness, "preflight", () => harness.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
const failed = relayed(await call(primary.harness, "preflight", () => harness.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
if (failed) {
|
||||
if (!fallback) throw new Refusal(failed.code, failed.message);
|
||||
// The fallback only on a failed primary preflight, and only at launch (row 65). Any refusal of
|
||||
@@ -566,7 +570,7 @@ export function createLauncher({
|
||||
throw e;
|
||||
});
|
||||
sessionProxy = await proxyOf(fallback);
|
||||
const also = relayed(call(fallback.harness, "preflight", () => second.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
const also = relayed(await call(fallback.harness, "preflight", () => second.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
if (also) throw new Refusal(also.code, `fallback ${fallback.harness}: ${also.message} (primary ${primary.harness}: ${failed.message})`);
|
||||
[model, harness, route, routeReason] = [fallback, second, "fallback", failed.code];
|
||||
}
|
||||
@@ -740,7 +744,7 @@ export function createLauncher({
|
||||
if (store) compactions(store, model.harness, harness, { instance, thread, session, run });
|
||||
const toolSocket = join(runDir, "tools.sock");
|
||||
const turnMarker = join(runDir, "turn.done");
|
||||
const version = harnessVersion !== undefined ? harnessVersion : call(model.harness, "version", () => harness.version({ env }));
|
||||
const version = harnessVersion !== undefined ? harnessVersion : await call(model.harness, "version", () => harness.version({ env }));
|
||||
// Row 137 (#1631): a role and thread holding an approved, unspent merge decision get land_work.
|
||||
// The broker checks the decision again on every land; this only decides what the bundle shows.
|
||||
// Row 156 (#1651): a non-thread instance's land and landing come from one landHolders read, so a
|
||||
@@ -1670,6 +1674,38 @@ export function createLauncher({
|
||||
clearTimeout(timer);
|
||||
}
|
||||
|
||||
// One harness ({ harness: name }) through a fresh loader, so a refusal never leaves an earlier
|
||||
// load's module in use: { refused, module }, module null when refused. A launch (harnessOf) and
|
||||
// the start's fit warm-up below (#1873 re-check 8) both load through here.
|
||||
async function freshHarness(m, log) {
|
||||
const loader = makeLoader({ dataRoot:system.instanceRoot ?? dataRoot, log });
|
||||
const { refused } = await loader.load({ harness: [m.harness] });
|
||||
return { refused, module: refused.length ? null : loader.modules("harness")[0] };
|
||||
}
|
||||
|
||||
// #1873 pi-unpin re-check 8: each switched-on harness module with ready() (pi's fit check) starts
|
||||
// it here, in the background, so the first launch joins the check in flight instead of starting
|
||||
// it. Nothing waits on it: a launch still awaits its own preflight, which shares this check or
|
||||
// reads its pass. A warm-up that fails or throws is dropped; it caches nothing, and the launch's
|
||||
// preflight runs the check again. Re-check 9: a harness plugin the warm-up can't load leaves one
|
||||
// launch log entry, by its code only (a launch that loads it records the message).
|
||||
function warmHarnesses() {
|
||||
const on = Array.isArray(b.plugins?.harness) ? b.plugins.harness : [];
|
||||
for (const name of on) {
|
||||
(async () => {
|
||||
let logged = false;
|
||||
const refusal = (entry) => {
|
||||
if (logged || entry?.event !== "plugin-refused") return;
|
||||
logged = true;
|
||||
record({ event: "plugin-refused", kind: entry.kind, name: entry.name, code: entry.code, during: "warm-up" });
|
||||
};
|
||||
const { module } = await freshHarness({ harness: name }, refusal);
|
||||
if (typeof module?.ready === "function") await module.ready({ env });
|
||||
})().catch(() => {});
|
||||
}
|
||||
}
|
||||
if (launches) warmHarnesses();
|
||||
|
||||
host.beforeClose(close);
|
||||
return { business: b, registry, listen, launchPm, fromSession, tick, close };
|
||||
}
|
||||
+2
-1
@@ -16,7 +16,7 @@ import { pushWorking, fetchReview } from '../../../harness/src/git-tools.mjs';
|
||||
import { callTool } from '../../../harness/src/tools.mjs';
|
||||
import { toolServer } from '../../../harness/src/runner.mjs';
|
||||
import { buildBundle } from '../../../harness/src/bundle.mjs';
|
||||
import { HARNESS } from '../../../harness/tests/helpers.mjs';
|
||||
import { HARNESS, harnessReady } from '../../../harness/tests/helpers.mjs';
|
||||
import { ensureCheckouts } from '../../../seat/src/checkout.mjs';
|
||||
import { newRun, Registry, readSessions, writeSessionFile, workspaceDir } from '../../../seat/src/session.mjs';
|
||||
import { startTimeOf } from '../../src/host.mjs';
|
||||
@@ -127,6 +127,7 @@ async function runtime() {
|
||||
const cap = bus.bindLaunch(launch_record).cap;
|
||||
roles[role] = { cap, run: id, workspace, runDir };
|
||||
call(role, 'role.claim');
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
const bundle = buildBundle({ dir: join(runDir, 'bundle'), resolved, contract: readFileSync(resolved.contract, 'utf8'),
|
||||
arbiter: role === 'cto', tracker: Boolean(resolved.tracker), credentials: [], skillsDir: join(dataRoot, 'skills-enabled'), harness: HARNESS.pi,
|
||||
harnessVersion: '0.85.1', session: { run: id, workspace, checkouts, toolSocket: join(runDir, 'tools.sock'), turnMarker: join(runDir, 'turn.done') } });
|
||||
|
||||
@@ -12,7 +12,8 @@ import { createHash } from "node:crypto";
|
||||
import { once } from "node:events";
|
||||
import { createInterface } from "node:readline";
|
||||
import { connect } from "node:net";
|
||||
import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, symlinkSync, watch, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { Client } from "../../bus/src/client.mjs";
|
||||
@@ -24,6 +25,8 @@ import { bootConfig, loadSystem } from "../src/config.mjs";
|
||||
import { cmdlineOf, startHost, startTimeOf } from "../src/host.mjs";
|
||||
import { createLauncher } from "../src/launcher.mjs";
|
||||
import { mockAnthropic, REPO } from "../../harness/tests/helpers.mjs";
|
||||
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
|
||||
import * as pi from "../../plugins/harness/pi/index.mjs";
|
||||
import { fakeHarnesses, fixture, HOST_CREDENTIALS, tmp, withoutHostCredentials } from "./helpers.mjs";
|
||||
|
||||
const FAKE = fileURLToPath(new URL("../../harness/tests/fixtures/fake-adapter.mjs", import.meta.url));
|
||||
@@ -266,6 +269,142 @@ test("bus start --pm: the launcher instance (the chief of staff) runs under its
|
||||
assert.equal(end.exitCode, 0);
|
||||
});
|
||||
|
||||
// #1873 pi-unpin S1/S2: the version label comes from the pi the module finds. A pi whose --version
|
||||
// fails gives a null label, and the launch still goes ahead.
|
||||
test("pi-unpin: a pi named by MOSAIC_PI_BIN that exits 1 on --version records a null harnessVersion and still launches", { skip, timeout: 30000 }, async (t) => {
|
||||
const prepared = prepare(t);
|
||||
const pkg = join(prepared.root, "pi-prefix", "lib", "node_modules", "@earendil-works", "pi-coding-agent");
|
||||
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "3.0.0", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
// #1873 S3: the stand-in passes the fit check; only its --version fails.
|
||||
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn({ versionExit: 1 }), { mode: 0o755 });
|
||||
mkdirSync(join(prepared.root, "pi-prefix", "bin"));
|
||||
const bin = join(prepared.root, "pi-prefix", "bin", "pi");
|
||||
symlinkSync("../lib/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js", bin);
|
||||
prepared.f.env.MOSAIC_PI_BIN = bin;
|
||||
// env() reads the bus's own environment, as the launcher's is process.env outside tests.
|
||||
const before = process.env.MOSAIC_PI_BIN;
|
||||
process.env.MOSAIC_PI_BIN = bin;
|
||||
t.after(() => { if (before === undefined) delete process.env.MOSAIC_PI_BIN; else process.env.MOSAIC_PI_BIN = before; });
|
||||
const { f, launcher, close } = await setup(t, (doc) => doc, prepared, { harnessVersion: undefined });
|
||||
const chief = await launcher.launchPm();
|
||||
assert.equal(chief.harness, "pi");
|
||||
const [s] = readSessions(f.dataRoot);
|
||||
const manifest = JSON.parse(readFileSync(join(s.runDir, "bundle", "manifest.json"), "utf8"));
|
||||
assert.equal(manifest.harnessVersion, null);
|
||||
assert.equal(await close(), 0);
|
||||
});
|
||||
|
||||
// #1873 re-check 8: a pi stand-in under root/pi-prefix, as an npm prefix holds it. Returns its bin.
|
||||
function piPrefix(root, knobs) {
|
||||
const pkg = join(root, "pi-prefix", "lib", "node_modules", "@earendil-works", "pi-coding-agent");
|
||||
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "3.0.0", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn(knobs), { mode: 0o755 });
|
||||
mkdirSync(join(root, "pi-prefix", "bin"));
|
||||
const bin = join(root, "pi-prefix", "bin", "pi");
|
||||
symlinkSync("../lib/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js", bin);
|
||||
return bin;
|
||||
}
|
||||
// Each fit run makes one scratch directory under tmpdir(), so a watch there counts the runs.
|
||||
// quiet() waits until no run has started for a while, so a check an earlier test left going
|
||||
// (the real pi's, warmed by its launcher) is not counted.
|
||||
function fitRuns(t) {
|
||||
const made = new Set();
|
||||
const watcher = watch(tmpdir(), (event, name) => { if (name && name.startsWith("mosaic-pi-fit-")) made.add(name); });
|
||||
t.after(() => watcher.close());
|
||||
const quiet = async () => {
|
||||
let n;
|
||||
do { n = made.size; await new Promise((r) => setTimeout(r, 700)); } while (made.size !== n);
|
||||
};
|
||||
return { made, quiet };
|
||||
}
|
||||
function busPiBin(t, prepared, bin) {
|
||||
prepared.f.env.MOSAIC_PI_BIN = bin;
|
||||
const before = process.env.MOSAIC_PI_BIN;
|
||||
process.env.MOSAIC_PI_BIN = bin;
|
||||
t.after(() => { if (before === undefined) delete process.env.MOSAIC_PI_BIN; else process.env.MOSAIC_PI_BIN = before; });
|
||||
}
|
||||
|
||||
// #1873 pi-unpin re-check 8: the launcher starts pi's fit check when it starts, before any launch,
|
||||
// and does not wait for it. The first launch joins that check: the two together make one check's
|
||||
// runs, not two.
|
||||
test("pi-unpin re-check 8: the launcher starts pi's fit check when it starts, and the first launch joins it", { skip, timeout: 60000 }, async (t) => {
|
||||
const { made, quiet } = fitRuns(t);
|
||||
// One check on its own, for another stand-in: how many runs it makes.
|
||||
const alone = piPrefix(tmp(t, "mosaic-launch-pi-"), {});
|
||||
await quiet();
|
||||
made.clear();
|
||||
assert.equal(await pi.preflight({ env: { ANTHROPIC_API_KEY: "stand-in", MOSAIC_PI_BIN: alone } }), null);
|
||||
await quiet();
|
||||
const runs = made.size;
|
||||
assert.ok(runs > 10, `one check made ${runs} runs`);
|
||||
const prepared = prepare(t);
|
||||
busPiBin(t, prepared, piPrefix(prepared.root, {}));
|
||||
made.clear();
|
||||
const { launcher, close } = await setup(t, (doc) => doc, prepared, { harnessVersion: undefined });
|
||||
// No launch yet, and the check is under way.
|
||||
await until(() => made.size > 0, 10000).catch(() => assert.fail("no fit run started before the first launch"));
|
||||
const chief = await launcher.launchPm();
|
||||
assert.equal(chief.harness, "pi");
|
||||
await quiet();
|
||||
assert.equal(made.size, runs, "the launch joined the check the launcher started");
|
||||
assert.equal(await close(), 0);
|
||||
});
|
||||
|
||||
// A warm-up that fails leaves no unhandled rejection and keeps no verdict: the next preflight runs
|
||||
// the check again and refuses on its own.
|
||||
test("pi-unpin re-check 8: a fit check the launcher starts that fails is dropped: no unhandled rejection, nothing kept", { skip, timeout: 60000 }, async (t) => {
|
||||
const { made, quiet } = fitRuns(t);
|
||||
const unhandled = [];
|
||||
const seen = (e) => unhandled.push(e);
|
||||
process.on("unhandledRejection", seen);
|
||||
t.after(() => process.off("unhandledRejection", seen));
|
||||
const prepared = prepare(t);
|
||||
const bin = piPrefix(prepared.root, { dropFlags: ["--no-themes"] });
|
||||
busPiBin(t, prepared, bin);
|
||||
await quiet();
|
||||
made.clear();
|
||||
const { close } = await setup(t, (doc) => doc, prepared, { harnessVersion: undefined });
|
||||
await until(() => made.size > 0, 10000).catch(() => assert.fail("no fit run started before the first launch"));
|
||||
// This preflight joins the failing check; then it is gone.
|
||||
const env = { ANTHROPIC_API_KEY: "stand-in", MOSAIC_PI_BIN: bin };
|
||||
const refusal = { code: "harness-feature-missing", message: "pi 9.9.9 (MOSAIC_PI_BIN) lacks --no-themes" };
|
||||
assert.deepEqual(await pi.preflight({ env }), refusal);
|
||||
await quiet();
|
||||
await new Promise((r) => setImmediate(r));
|
||||
assert.deepEqual(unhandled, []);
|
||||
made.clear();
|
||||
assert.deepEqual(await pi.preflight({ env }), refusal);
|
||||
await quiet();
|
||||
assert.ok(made.size > 0, "the failed check was kept");
|
||||
assert.equal(await close(), 0);
|
||||
});
|
||||
|
||||
// Re-check 9: a harness plugin the warm-up can't load leaves one launch log entry, by its code
|
||||
// only, so the message (which may name a host path) stays out of it.
|
||||
test("pi-unpin re-check 9: a harness plugin the launcher's warm-up can't load leaves one launch log entry, by code only", { skip, timeout: 30000 }, async (t) => {
|
||||
const given = prepare(t, (doc) => {
|
||||
doc.plugins = { ...doc.plugins, harness: [...doc.plugins.harness, "broken"] };
|
||||
return doc;
|
||||
});
|
||||
const makeLoader = await fakeHarnesses(t, { pi: given.adapter, "claude-code": given.adapter }, (write) => {
|
||||
write("harness", "broken", `throw new Error("broken at /srv/fit-secret-path");\n`);
|
||||
});
|
||||
const { f, close } = await setup(t, undefined, given, { makeLoader });
|
||||
const refused = () => existsSync(launchLogFile(f.dataRoot, "acme")) ? log(f).filter((e) => e.event === "plugin-refused") : [];
|
||||
await until(() => refused().length > 0, 10000).catch(() => assert.fail("the warm-up left no record of the refused plugin"));
|
||||
await new Promise((r) => setTimeout(r, 500));
|
||||
const entries = refused();
|
||||
assert.equal(entries.length, 1, JSON.stringify(entries));
|
||||
const [entry] = entries;
|
||||
assert.deepEqual(Object.keys(entry).sort(), ["at", "code", "during", "event", "kind", "name"]);
|
||||
assert.deepEqual([entry.kind, entry.name, entry.during], ["harness", "broken", "warm-up"]);
|
||||
assert.match(entry.code, /^[a-z][a-z-]+$/);
|
||||
assert.doesNotMatch(readFileSync(launchLogFile(f.dataRoot, "acme"), "utf8"), /fit-secret-path/);
|
||||
assert.equal(await close(), 0);
|
||||
});
|
||||
|
||||
test("the chief of staff launches a coder through its launch tool; the coder answers; refusals name their code", { skip, timeout: 30000 }, async (t) => {
|
||||
const { f, launcher, client, cto } = await setup(t);
|
||||
const sock = launchSocketPath(f.dataRoot);
|
||||
|
||||
@@ -90,6 +90,8 @@ async function routed(t, harness) {
|
||||
const makeLoader = await fakeHarnesses(t, { pi: null, 'claude-code': null }, write => {
|
||||
// fsthrows: its preflight reads a missing file, so the error's message quotes the path.
|
||||
write('harness', 'fsthrows', `export * from ${PI};\nimport { readFileSync } from "node:fs";\nexport function preflight() { readFileSync(${JSON.stringify(absent)}); }\n`);
|
||||
// fsrejects (#1873 re-check 8): an async preflight, as pi's is, whose promise rejects with that error.
|
||||
write('harness', 'fsrejects', `export * from ${PI};\nimport { readFileSync } from "node:fs";\nexport async function preflight() { readFileSync(${JSON.stringify(absent)}); }\n`);
|
||||
// plainthrows: a plain Error whose message quotes a host path, as a module's own text.
|
||||
write('harness', 'plainthrows', `export * from ${PI};\nexport function preflight() { throw new Error(${JSON.stringify(`cannot read ${absent}`)}); }\n`);
|
||||
// pathfails: a preflight that returns a failure whose message quotes a host path.
|
||||
@@ -146,6 +148,15 @@ test('a harness whose preflight throws a system error refuses harness-failed nam
|
||||
clean(JSON.stringify(s.last()), s.root);
|
||||
});
|
||||
|
||||
// #1873 re-check 8 P3-1: a preflight promise that rejects is refused as a throw is, by the call and
|
||||
// the system code, never the raw error or its path.
|
||||
test('a harness whose async preflight rejects with a system error refuses harness-failed naming the call and code, never the path', async t => {
|
||||
const s = await routed(t, 'fsrejects');
|
||||
assert.deepEqual(await s.launch(), { ok: false, error: 'harness-failed' });
|
||||
assert.equal(s.last().why, 'harness fsrejects preflight: cannot open a host file (ENOENT)');
|
||||
clean(JSON.stringify(s.last()), s.root);
|
||||
});
|
||||
|
||||
test('a harness the loader refuses names its code only, never the loader message or a path', async t => {
|
||||
const s = await routed(t, 'absent');
|
||||
assert.deepEqual(await s.launch(), { ok: false, error: 'harness-refused' });
|
||||
|
||||
@@ -18,13 +18,103 @@ Every harness layer below is a row S0 line
|
||||
(`docs/plans/2026-10-04_slice-1.md`, "What S6 can rely on"), and each
|
||||
bundle's `manifest.json` names the lines it relies on (`reliesOn`):
|
||||
|
||||
| | Pi | Claude Code |
|
||||
|---|---|---|
|
||||
| 1. block a tool | `tool_call` handler returns a block | PreToolUse command hook exits 2; `--bare` is never passed |
|
||||
| 2. gate crash | a throw in the handler blocks | the hook runs as `<gate> \|\| exit 2` |
|
||||
| 3. gate missing | a missing `-e` file refuses to start | the wrapper blocks |
|
||||
| 4. gate hang | the runner's wall clock plus the `agent_end` marker | `timeout -k 2 10 <gate> \|\| exit 2`, hook timeout 20 |
|
||||
| 5. bash | limited only by the tool limit | limited only by the tool limit |
|
||||
| | Pi | Claude Code | How pi's line is checked |
|
||||
|---|---|---|---|
|
||||
| 1. block a tool | `tool_call` handler returns a block | PreToolUse command hook exits 2; `--bare` is never passed | suites, on the repository's pi (`pi-session.test.mjs`) |
|
||||
| 2. gate crash | a throw in the handler blocks | the hook runs as `<gate> \|\| exit 2` | suites, on the repository's pi |
|
||||
| 3. gate missing | a missing `-e` file refuses to start | the wrapper blocks | at every start, by the fit check, on the pi that runs; and suites |
|
||||
| 4. gate hang | the runner's wall clock plus the `agent_end` marker | `timeout -k 2 10 <gate> \|\| exit 2`, hook timeout 20 | suites, on the repository's pi |
|
||||
| 5. bash | limited only by the tool limit | limited only by the tool limit | suites, on the repository's pi |
|
||||
|
||||
Pi is not pinned (#1873 pi-unpin). Before a session starts, `preflight`
|
||||
awaits the fit check in `src/pi-fit.mjs` on the binary the module found
|
||||
(the Triage host and route await the module's `ready` instead), and
|
||||
`env` reads its pass. It runs that binary in a sandbox with the network
|
||||
unshared and calls no model. Each run is a child process the check
|
||||
awaits, up to four at a time, so the bus's event loop keeps running
|
||||
while it does; callers that ask while a check runs share it. Each run
|
||||
gets the environment a bus session's pi gets, built by the same code
|
||||
(the launcher's `ENV_ALLOW` and `sessionEnv`, the sandbox's
|
||||
`sessionSetenv`, the runner's `adapterEnv` and the pi module's
|
||||
`harnessEnv`) from the bus process's own environment, less the provider
|
||||
keys, and starts the binary through `/bin/sh` as the adapter does. So
|
||||
`HOME` is a scratch home, `TMPDIR` is `/tmp` bound to scratch, a run has
|
||||
`TERM`, `TZ` or `LC_ALL` only when the bus has it (a bus started by its
|
||||
unit has none), and no run sets `PI_CODING_AGENT_DIR`, which no session
|
||||
has: pi's agent directory is `$HOME/.pi/agent`, where the user sources
|
||||
are planted. A test fails when the fit runs' names, or the values the
|
||||
code sets, differ from a bus session's, apart from the provider
|
||||
variables, for a login shell's host and a unit's. It needs
|
||||
`--help` to list, in its option column, every flag the adapter passes
|
||||
(with rpc mode and each thinking level), and a missing `-e` file to stop
|
||||
the start in rpc and in `-p`. The planted ambient sources must stay
|
||||
unloaded with the seal flags on (skills and prompt templates are read
|
||||
from `get_commands` in rpc and from the `-e` extension's command list in
|
||||
`-p`): extensions, MCP
|
||||
servers, skills and prompt templates (found in their directories, listed
|
||||
in a `settings.json`, or in a local package a `settings.json` lists),
|
||||
context files (`AGENTS.md`, `CLAUDE.md`, `SYSTEM.md`) and a trusted
|
||||
project's `.pi/SYSTEM.md` and `.pi/APPEND_SYSTEM.md`. The project has a
|
||||
saved trust, so only `--no-approve` keeps those two out. An `-e`
|
||||
extension writes the system prompt, command names and session id it sees
|
||||
at `session_start`; the sealed runs must show the `--system-prompt` file
|
||||
and none of the planted sources. A control run passes the seal flags less every loader flag
|
||||
(`--no-approve`, `--no-extensions`, `--no-skills`,
|
||||
`--no-prompt-templates`, `--no-context-files`, and `--no-mcp` when
|
||||
listed), so it keeps `--offline`, `--no-themes`, `--no-tools` and
|
||||
`--no-session`, and the same `-e` and `--system-prompt`. It must show
|
||||
each kind of source (an MCP server only when `--help` lists `--no-mcp`)
|
||||
and answer `get_state` and `get_commands`, and its `-e` extension must
|
||||
list the planted skill and prompt template, or the check can't tell and
|
||||
refuses. For every kind but the trusted project files, only a source
|
||||
outside the project counts there, since a project source can wait on
|
||||
trust and `--no-approve` would then hide a seal flag the binary ignores.
|
||||
Then one sealed rpc run per loader flag leaves out only that flag: its
|
||||
kind must show, and no other kind (an MCP server may show beside an
|
||||
extension when `--help` lists no `--no-mcp`, since pi then ties MCP to
|
||||
`--no-extensions`). So each loader flag is shown to keep its own kind
|
||||
out, and no other seal flag, nor their order, can stand in for it. Five
|
||||
sealed `-p` runs have a bus session's shape: the arguments the adapter
|
||||
passes for a session, in its order, from `sessionShape` in
|
||||
`src/pi-fit.mjs`, with two `-e` and `--session-dir`. The adapter's four
|
||||
session branches (a tool list or `--no-tools`, `-c` over a stored
|
||||
session or none, `--thinking` or none, `--mode json` or none) are spread
|
||||
over the five runs so every pair of them is passed together. A `-c` run
|
||||
gets a stored session and must resume it. Each must load both `-e`
|
||||
files and show no planted source. A test drives the adapter, in a bus
|
||||
session's environment, through every branch set with no tools, one or
|
||||
two, `-c` over a pi-named session file or an unrelated file, each
|
||||
thinking level and each provider a session can name. It runs over a
|
||||
login shell's host and a unit's, and fails on an argv `sessionShape`
|
||||
doesn't build, a pair no run passes, or a variable the adapter reads
|
||||
that no session sets. So a branch on `TZ` or `LC_ALL` being unset, or on
|
||||
`TERM` missing from the environment, turns it red. (Bash run as
|
||||
`/bin/sh` sets an unset `TERM` to `dumb` for the shell without exporting
|
||||
it, so `${TERM:-}` in the adapter is never empty, in a session or here.)
|
||||
It can't see a branch keyed on a
|
||||
value it doesn't try (a model, a path, another tool or provider). A last sealed `-p` run uses `--no-session`, as
|
||||
worker runs do. Each run gets a fresh scratch directory, so no run sees
|
||||
what another left. Not run: `--fork`, `--skill` and the interactive TUI
|
||||
(worker containers and `scripts/agent.sh` only), the branches three or
|
||||
four at a time beyond the five runs, `-c` over a session directory that
|
||||
holds nothing for the project (one more run costs about 340 ms cold),
|
||||
and a session's own values (tools beyond `read` and `bash`, thinking
|
||||
levels beyond `low`, provider, model, key, `-e` files, request). The
|
||||
runs are offline with no key, so nothing past the start is tried. A pi
|
||||
built to act one way in the fit runs and another way with those is
|
||||
outside what a start-time check can catch. A run killed for taking
|
||||
over 20 s or printing over 4 MB can't be checked and says which. A
|
||||
`--version` that fails or hangs past 3 s only leaves no label. Pi
|
||||
0.85.1 appends the agent directory's `APPEND_SYSTEM.md` whatever the
|
||||
flags, so the check doesn't plant one. Any failure refuses the launch with
|
||||
`harness-feature-missing` and one line that names the binary by its label,
|
||||
for example `pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-context-files`. When
|
||||
`--help` lists `--no-mcp`, the adapter passes it too. A pass is cached in
|
||||
memory by a digest of the package's files (and npm's lock files, for
|
||||
the repository's copy) and the names in the runs' environment, so a bus
|
||||
restarted under other names checks again. Lines 1, 2, 4 and 5 are not
|
||||
checked at start; a pi other than the repository's copy relies on them
|
||||
unproved until a scripted-model fit run exists (design section 5, S6).
|
||||
|
||||
Claude Code also runs with `--restricted`: no user, project or local
|
||||
settings, file tools confined to the working directory, and no `CLAUDE.md`
|
||||
|
||||
@@ -0,0 +1,778 @@
|
||||
// #1873 pi-unpin S3: the start-time fit check for a pi binary the harness module found. Nothing
|
||||
// here compares a version. The check runs the binary only inside a sandbox with the network
|
||||
// unshared, the found package mounted as a session mounts it, and a bus session's environment
|
||||
// (fitEnv below), so its agent directory is $HOME/.pi/agent under a scratch HOME, as a session's
|
||||
// is. No model is called and no provider key is passed. It asks:
|
||||
//
|
||||
// --version a label for the refusal line and the record; not a gate
|
||||
// --help lists, in its option column, every flag Mosaic passes, with rpc mode and each
|
||||
// thinking level
|
||||
// control rpc, planted ambient sources, a saved trust for the project, the seal flags less
|
||||
// every loader flag (the ones that each keep one kind of source out: --no-approve,
|
||||
// --no-extensions, --no-skills, --no-prompt-templates, --no-context-files, and
|
||||
// --no-mcp when --help lists it), the --system-prompt file the sealed runs pass, and
|
||||
// one -e extension that writes what it sees at session_start: the system prompt, the
|
||||
// command names and the session id. The binary answers get_state and get_commands,
|
||||
// its -e extension lists the planted skill and prompt template (the -p runs see
|
||||
// those kinds only that way), and it shows each kind of planted source:
|
||||
// an extension, a skill, a prompt template, a context file, a trusted project file
|
||||
// (the project's .pi/APPEND_SYSTEM.md, which only the trust --no-approve must undo
|
||||
// lets in), and an MCP server when --help lists --no-mcp. Only a source outside the
|
||||
// project counts for the other kinds, since a project source can wait on trust. A
|
||||
// kind it doesn't show could not be told apart from a sealed one.
|
||||
// sealed rpc the adapter's seal flags (and --no-mcp when --help lists it), the -e extension and
|
||||
// --system-prompt. The binary answers both, loads that extension (a session's gate
|
||||
// comes in that way), uses the --system-prompt file, and shows no planted source.
|
||||
// one out one more sealed rpc run per loader flag, with every seal flag but that one. The
|
||||
// kind it keeps out must show, and no other kind (pi without --no-mcp ties MCP to
|
||||
// --no-extensions, so an MCP server may show beside an extension). So each loader flag
|
||||
// is shown to keep its own kind out, and no other seal flag, nor the order they come
|
||||
// in, can stand in for it.
|
||||
// session five -p runs in a bus session's shape, with the arguments adapters/pi/adapter.sh
|
||||
// passes for a session, in its order (sessionShape below): two -e, --session-dir in
|
||||
// place of --no-session, a fixed --provider and --model, -p and a message. The
|
||||
// adapter's four session branches (a tool list or --no-tools, -c over a stored
|
||||
// session or none, --thinking or none, --mode json or none) are spread over the five
|
||||
// runs so each pair of them is passed together (SESSION_RUNS). A -c run gets a
|
||||
// stored session for the project and must resume it. Each run loads both -e
|
||||
// extensions, uses the --system-prompt file, and shows no
|
||||
// planted source. A test drives the adapter through every branch set, tool list
|
||||
// length and thinking level, and fails on an argv sessionShape doesn't build or a
|
||||
// pair no run passes.
|
||||
// sealed -p the seal flags in -p with --no-session, as a worker run passes them. Its -e
|
||||
// extension must reach session_start, and no planted source may show.
|
||||
// missing -e a missing -e file, in rpc and in -p, stops the start: exit non-zero, no answer,
|
||||
// and no session_start for the -e extension given after it
|
||||
//
|
||||
// Every run gets a fresh scratch directory with the sources planted again, so no run sees what
|
||||
// another one left. The directory's name carries the pid, start time and pid namespace of the
|
||||
// process that made it, and each check first stops the processes and removes the directories a
|
||||
// process no longer running left (sweepScratch below). Every run dies with the process that runs
|
||||
// the check (checkArgv in packages/seat/src/sandbox.mjs), at once when unshare runs here and at
|
||||
// the next check's sweep when it doesn't.
|
||||
// The runs share no state: up to FIT_CONCURRENCY of them run at once,
|
||||
// each a child process the check awaits, so the process that runs the check (the bus) keeps
|
||||
// answering while it runs. --version and --help go first; the rest start in the order above and
|
||||
// are judged in that order, so the first failure is the one a refusal names, and the runs still
|
||||
// going are killed. Each run passes the same -e and --system-prompt; the other runs differ only by
|
||||
// their seal flags, their mode, the -p message and the missing -e file. Each run starts the binary
|
||||
// through /bin/sh, as the adapter does, with fitEnv's variables: the environment a bus session's pi
|
||||
// gets, built by the code that builds it (the launcher's ENV_ALLOW and sessionEnv, the sandbox's
|
||||
// sessionSetenv, the runner's adapterEnv and the pi module's harnessEnv) from the calling process's
|
||||
// own environment, less the provider variables. So a fit run has a name only when a session in that
|
||||
// process has it (TERM, TZ and LC_ALL only when the bus has them), no run sets a variable a session
|
||||
// lacks, such as PI_CODING_AGENT_DIR, and a test holds the two together.
|
||||
//
|
||||
// A check that cannot tell refuses: a sandbox that fails, a run that times out or prints more
|
||||
// than 4 MB, an answer that is missing or doesn't parse, a kind of source the control doesn't
|
||||
// show, or the kind a one-out run should show. A one-out run that shows another kind, or a
|
||||
// session-shaped run that shows any, is a leak. A --version that fails or hangs past 3 s only
|
||||
// leaves no label. A pass is cached in memory by a digest of the files that run (the package's
|
||||
// package.json, every file under dist/bundle/, every file in the directory tree that holds the
|
||||
// binary's real file, and, for a copy mounted with the node_modules it sits in, npm's lock files
|
||||
// there), the mount layout and the names in the fit runs' environment, so a process with other
|
||||
// names checks again. Callers that ask while a check for the same key is running share it.
|
||||
// A refusal line names the found binary by its label (MOSAIC_PI_BIN, the pi on PATH, the
|
||||
// repository's pi), never a host path.
|
||||
//
|
||||
// Known limit: pi 0.85.1 appends the agent directory's APPEND_SYSTEM.md whatever the flags. The
|
||||
// check doesn't plant one; a session's agent directory is the run's own. Not run: --fork,
|
||||
// --skill with --no-skills dropped, and the interactive TUI, which only worker containers and
|
||||
// scripts/agent.sh pass; the session branches three or four at a time beyond the session-shaped
|
||||
// runs; -c over a session directory that holds nothing for the project (the adapter passes -c
|
||||
// whenever the directory isn't empty), since one more run costs about 340 ms cold; and a
|
||||
// session's own values: its tool names beyond read and bash, thinking levels beyond low,
|
||||
// provider, model, key, -e files and request. The runs are offline with no key, so nothing
|
||||
// past the start is tried. A pi built to act differently with those is outside what a start-time
|
||||
// check can catch.
|
||||
import { spawn } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, readlinkSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join, relative } from "node:path";
|
||||
import { BWRAP, SETPRIV, UNSHARE, checkArgv, outerArgv, sessionSetenv } from "../../seat/src/sandbox.mjs";
|
||||
import { isProviderVariable, sessionEnv } from "../../seat/src/session.mjs";
|
||||
import { adapterEnv } from "./runner.mjs";
|
||||
import { FIT_ROUTE, harnessEnv } from "../../plugins/harness/pi/session-env.mjs";
|
||||
|
||||
// The flags adapters/pi/adapter.sh and the conversation engine pass.
|
||||
export const FIT_FLAGS = Object.freeze(["--mode", "--session", "--no-session", "--session-dir", "--fork", "-c", "--no-extensions", "-e", "--no-skills", "--skill", "--no-prompt-templates", "--no-themes", "--no-context-files", "--no-approve", "--offline", "--tools", "--no-tools", "--provider", "--model", "--thinking", "--system-prompt", "-p"]);
|
||||
// The adapter's seal flags, as it passes them in an ephemeral session with no tools.
|
||||
const SEAL = ["--offline", "--no-approve", "--no-extensions", "--no-skills", "--no-prompt-templates", "--no-themes", "--no-context-files", "--no-tools", "--no-session"];
|
||||
// The loader flags: each keeps one kind of planted source out. --no-mcp counts when --help lists it.
|
||||
const LOADERS = [["--no-approve", "trusted project file"], ["--no-extensions", "extension"], ["--no-skills", "skill"], ["--no-prompt-templates", "prompt template"], ["--no-context-files", "context file"]];
|
||||
// The session-shaped runs' fixed values: a two-tool list, a thinking level, a provider and model.
|
||||
// Offline with no key, pi stops before any model call. The provider and model are the pi module's
|
||||
// (session-env.mjs FIT_ROUTE), so no core source names them.
|
||||
export const SESSION_SHAPE = Object.freeze({ tools: "read,bash", thinking: "low", provider: FIT_ROUTE.provider, model: FIT_ROUTE.model });
|
||||
// The branches adapters/pi/adapter.sh takes for a bus session, each run once or not: a tool list or
|
||||
// --no-tools, -c over a stored session or a fresh one, --thinking or none, --mode json (the model
|
||||
// event monitor) or none. The five runs pass every pair of those together; pi-fit.test.mjs drives
|
||||
// the adapter through all sixteen and fails on a pair no run passes.
|
||||
const T = SESSION_SHAPE.tools;
|
||||
const L = SESSION_SHAPE.thinking;
|
||||
export const SESSION_RUNS = Object.freeze([
|
||||
{ tools: "", cont: false, thinking: "", json: false },
|
||||
{ tools: "", cont: true, thinking: L, json: true },
|
||||
{ tools: T, cont: false, thinking: L, json: true },
|
||||
{ tools: T, cont: true, thinking: "", json: true },
|
||||
{ tools: T, cont: true, thinking: L, json: false },
|
||||
].map((r) => Object.freeze(r)));
|
||||
// The arguments adapters/pi/adapter.sh builds for a bus session: the given -e files, a tool list
|
||||
// or none, a persistent session directory with -c when it holds a session, a thinking level or
|
||||
// none, the --system-prompt file, -p and the request, and --mode json when the model event monitor
|
||||
// runs pi. No --skill and no --fork: a bus session never passes them. Keep its order:
|
||||
// pi-fit.test.mjs runs the adapter through every branch and fails when the two differ.
|
||||
export function sessionShape({ noMcp, extensions, prompt, sessionDir, tools, cont, thinking, json, message }) {
|
||||
return [
|
||||
"--offline", "--no-approve", "--no-extensions", ...(noMcp ? ["--no-mcp"] : []), ...extensions.flatMap((e) => ["-e", e]),
|
||||
"--no-skills", "--no-prompt-templates", "--no-themes", "--no-context-files",
|
||||
...(tools ? ["--tools", tools] : ["--no-tools"]), "--session-dir", sessionDir, ...(cont ? ["-c"] : []),
|
||||
"--provider", SESSION_SHAPE.provider, "--model", SESSION_SHAPE.model, ...(thinking ? ["--thinking", thinking] : []),
|
||||
"--system-prompt", prompt, "-p", message, ...(json ? ["--mode", "json"] : []),
|
||||
];
|
||||
}
|
||||
// What a session-shaped run is named by in a refusal line.
|
||||
const shapeName = ({ tools, cont, thinking, json }) => `in a session's shape with ${[tools ? "--tools" : "--no-tools", ...(cont ? ["-c"] : []), ...(thinking ? ["--thinking"] : []), ...(json ? ["--mode json"] : [])].join(", ")}`;
|
||||
const RUN_TIMEOUT = 20000;
|
||||
// --version gives only a label, so a hang costs this much and leaves no label.
|
||||
const VERSION_TIMEOUT = 3000;
|
||||
const OUTPUT_MAX = 4 * 1024 * 1024;
|
||||
// How long a run's pipes may stay open after it exits or is killed (spawned).
|
||||
const PIPE_GRACE = 1000;
|
||||
const DIGEST_FILES_MAX = 20000;
|
||||
|
||||
// At most this many fit runs at once. Each run is one sandboxed pi start in its own scratch
|
||||
// directory.
|
||||
export const FIT_CONCURRENCY = 4;
|
||||
|
||||
const passed = new Map();
|
||||
const running = new Map();
|
||||
|
||||
// #1873 re-check 9: each run's scratch directory is named for the process that makes it, by its
|
||||
// pid and its start time (field 22 of /proc/<pid>/stat), so a pid used again later is not taken
|
||||
// for it. A process killed mid-check can't remove its own directories. So a check removes, before
|
||||
// it starts, every directory here that a process no longer running made: its pid is gone, or now
|
||||
// belongs to a process that started at another time. A directory whose process can't be read, or
|
||||
// one this uid doesn't own, or a symlink, is left alone.
|
||||
//
|
||||
// #1873 re-check 10: the name also carries the maker's pid namespace (the inode of
|
||||
// /proc/<pid>/ns/pid), and a sweep leaves alone every directory made in another one, since it
|
||||
// can't read that process. And before it removes a directory, the sweep stops the processes that
|
||||
// name it on their command line: a run's every layer does (the setpriv, shell, unshare and bwrap
|
||||
// argv, bwrap's child before its exec, pi's own -e and --system-prompt), and so do the sandbox
|
||||
// probes. Without unshare's outer layer a bwrap child killed in its setup waits for good (see
|
||||
// checkArgv in sandbox.mjs), and this is what stops it. A process is stopped only when an argument
|
||||
// is that directory or a path in it, its real and effective uid are this process's, and its start
|
||||
// time is the same before and after its command line is read. Never by name, never another uid's,
|
||||
// never one whose directory's maker still runs.
|
||||
const SCRATCH = /^mosaic-pi-fit-(\d+)-(\d+)-(\d+)-/;
|
||||
const startTime = (pid) => readFileSync(`/proc/${pid}/stat`, "utf8").split(") ").pop().split(" ")[19];
|
||||
const pidNamespace = (pid) => /^pid:\[(\d+)\]$/.exec(readlinkSync(`/proc/${pid}/ns/pid`))[1];
|
||||
let ownName = null;
|
||||
const scratchPrefix = () => `mosaic-pi-fit-${ownName ??= `${process.pid}-${startTime(process.pid)}-${pidNamespace("self")}`}-`;
|
||||
function sweepScratch(dir = tmpdir()) {
|
||||
let names;
|
||||
try {
|
||||
names = readdirSync(dir);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
const uid = process.getuid();
|
||||
const ns = pidNamespace("self");
|
||||
const stale = [];
|
||||
for (const name of names) {
|
||||
const m = SCRATCH.exec(name);
|
||||
if (!m || m[3] !== ns) continue;
|
||||
try {
|
||||
let alive;
|
||||
try {
|
||||
alive = startTime(m[1]) === m[2];
|
||||
} catch (e) {
|
||||
if (e.code !== "ENOENT") continue;
|
||||
alive = false;
|
||||
}
|
||||
if (alive) continue;
|
||||
const at = join(dir, name);
|
||||
const st = lstatSync(at);
|
||||
if (st.isDirectory() && st.uid === uid) stale.push(at);
|
||||
} catch {}
|
||||
}
|
||||
if (!stale.length) return;
|
||||
stopNaming(stale, uid);
|
||||
for (const at of stale) {
|
||||
try {
|
||||
rmSync(at, { recursive: true, force: true });
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
// SIGKILL to every process of uid (real and effective) with an argument that is one of dirs or a
|
||||
// path in one, other than this one.
|
||||
function stopNaming(dirs, uid) {
|
||||
let pids;
|
||||
try {
|
||||
pids = readdirSync("/proc").filter((p) => /^\d+$/.test(p) && Number(p) !== process.pid);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
const names = (arg) => dirs.some((d) => arg === d || arg.startsWith(`${d}/`));
|
||||
for (const pid of pids) {
|
||||
try {
|
||||
const start = startTime(pid);
|
||||
const ids = /^Uid:\s+(\d+)\s+(\d+)\s/m.exec(readFileSync(`/proc/${pid}/status`, "utf8"));
|
||||
if (!ids || Number(ids[1]) !== uid || Number(ids[2]) !== uid) continue;
|
||||
if (!readFileSync(`/proc/${pid}/cmdline`, "utf8").split("\0").some(names)) continue;
|
||||
if (startTime(pid) !== start) continue;
|
||||
process.kill(Number(pid), "SIGKILL");
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
// The provider variables never reach a fit run: no key is passed, since no model is called.
|
||||
const withoutProviders = (host) => Object.fromEntries(Object.entries(host ?? {}).filter(([k, v]) => typeof v === "string" && !isProviderVariable(k)));
|
||||
// The names a fit run's host part holds: sessionEnv's answer for the process's environment.
|
||||
export const fitNames = (host = process.env) => Object.keys(sessionEnv(withoutProviders(host), "fit")).sort();
|
||||
const fitKey = (found, { levels, bwrap, unshare, node, host }) => JSON.stringify([filesDigest(found), found.root, found.dest ?? null, found.view, bwrap, unshare, node, levels, fitNames(host)]);
|
||||
|
||||
// found: binary.mjs's answer ({ bin, label, root, dest, view, pkg }). levels: the thinking levels
|
||||
// the module applies. host: the environment of the process sessions launch from (the bus's own),
|
||||
// which a fit run's environment is built from as a session's is. bwrap and unshare: where those
|
||||
// tools are (tests pass stand-ins). Returns a promise of { ok: true, version, noMcp, digest, outer }
|
||||
// or { ok: false, message }. outer: { used: true } when the runs had unshare's outer layer, else
|
||||
// { used: false, why } with why naming unshare and how it failed (sandboxLayers below). A call
|
||||
// while a check with the same key runs gets that check's promise. A check that throws (a scratch directory it can't
|
||||
// make, say) can't tell, so it gives { ok: false, message } too and keeps nothing.
|
||||
export function piFit(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, node = process.execPath, host = process.env } = {}) {
|
||||
const who = (version) => `pi${version ? ` ${version}` : ""} (${found.label})`;
|
||||
let key;
|
||||
try {
|
||||
key = fitKey(found, { levels, bwrap, unshare, node, host });
|
||||
} catch {
|
||||
return Promise.resolve({ ok: false, message: `${who(null)} can't be checked: its files can't be read` });
|
||||
}
|
||||
if (passed.has(key)) return Promise.resolve(passed.get(key));
|
||||
if (running.has(key)) return running.get(key);
|
||||
const job = (async () => {
|
||||
try {
|
||||
sweepScratch();
|
||||
// The sandbox's own error text can name a host path, so the line keeps only what failed.
|
||||
const layers = await sandboxLayers({ bwrap, unshare, node });
|
||||
if (layers.problem) return { ok: false, message: `${who(null)} can't be checked: ${layers.problem}` };
|
||||
const result = await check(found, { levels, bwrap, unshare: layers.outer, node, who, digest: JSON.parse(key)[0], host });
|
||||
if (!result.ok) return result;
|
||||
const passing = { ...result, outer: layers.outer ? { used: true } : { used: false, why: layers.why } };
|
||||
passed.set(key, passing);
|
||||
return passing;
|
||||
} catch {
|
||||
// #1873 re-check 8: refused as a fit that failed. The error's text can name a host path, so
|
||||
// the line keeps none of it.
|
||||
return { ok: false, message: `${who(null)} can't be checked: the check could not run` };
|
||||
}
|
||||
})().finally(() => { if (running.get(key) === job) running.delete(key); });
|
||||
running.set(key, job);
|
||||
return job;
|
||||
}
|
||||
|
||||
// The passed check for found as piFit would key it now, or null: none passed, or its files can't
|
||||
// be read. Nothing is run.
|
||||
export function fitVerdict(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, node = process.execPath, host = process.env } = {}) {
|
||||
try {
|
||||
return passed.get(fitKey(found, { levels, bwrap, unshare, node, host })) ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function check(found, { levels, bwrap, unshare, node, who, digest, host }) {
|
||||
// At most FIT_CONCURRENCY runs at once, started in the order they are asked for. stop kills the
|
||||
// runs still going and keeps queued ones from starting; the check waits for every run it began,
|
||||
// so no scratch directory or child outlives it.
|
||||
const stop = new AbortController();
|
||||
const begun = [];
|
||||
let active = 0;
|
||||
const waiting = [];
|
||||
const slot = () => active < FIT_CONCURRENCY ? (active++, Promise.resolve()) : new Promise((resolve) => waiting.push(resolve));
|
||||
const free = () => { const next = waiting.shift(); if (next) next(); else active--; };
|
||||
// args: the arguments, or a function of the run's scratch directory that returns them (it may
|
||||
// plant more first). The answer is read before the scratch directory goes: the rpc answers,
|
||||
// whether each -e extension loaded, what the first one saw at session_start (the system prompt,
|
||||
// the command names, the session id), and the planted sources that showed, through the rpc
|
||||
// get_commands answer or through the -e extension's command list (listed: through that alone).
|
||||
// shape: a session-shaped run's branches, which set its tools, thinking level and monitor
|
||||
// variables as a session's would. A run that stop kept from starting gives null.
|
||||
const once = async (args, input, { timeout, noMcp, shape }) => {
|
||||
await slot();
|
||||
try {
|
||||
if (stop.signal.aborted) return null;
|
||||
const scratch = mkdtempSync(join(tmpdir(), scratchPrefix()));
|
||||
let ran = null;
|
||||
try {
|
||||
plant(scratch);
|
||||
const env = fitEnv(scratch, { view: found.view, noMcp, shape, host });
|
||||
const r = ran = await runIn(found, { bwrap, unshare, node, scratch, env, args: typeof args === "function" ? args(scratch) : args, input, timeout, signal: stop.signal });
|
||||
const answer = answered(r.stdout);
|
||||
const start = started(scratch);
|
||||
const listed = start?.commands ?? [];
|
||||
return { ...r, answer, explicit: marked(scratch, EXPLICIT), second: marked(scratch, SECOND), prompt: start?.prompt ?? null, session: start?.session ?? null, listed: named(listed), seen: seen(scratch, [...(answer.commands ?? []), ...listed], start) };
|
||||
} finally {
|
||||
// #1873 re-check 10: a run this check killed (a timeout, or stop) may have had bwrap in its
|
||||
// setup, and without unshare's outer layer bwrap's child then waits for good. The run is
|
||||
// over, so whatever still names its directory is left from it.
|
||||
if (ran?.stopped) stopNaming([scratch], process.getuid());
|
||||
rmSync(scratch, { recursive: true, force: true });
|
||||
}
|
||||
} finally {
|
||||
free();
|
||||
}
|
||||
};
|
||||
const run = (args, input = "", { timeout = RUN_TIMEOUT, noMcp = false, shape = null } = {}) => {
|
||||
const p = once(args, input, { timeout, noMcp, shape });
|
||||
// Judged in order, a later run can fail before an earlier one is awaited: this keeps that
|
||||
// failure from counting as unhandled. Awaiting p still throws it.
|
||||
p.catch(() => {});
|
||||
begun.push(p);
|
||||
return p;
|
||||
};
|
||||
try {
|
||||
return await judge(found, { levels, who, digest, run });
|
||||
} finally {
|
||||
stop.abort();
|
||||
await Promise.allSettled(begun);
|
||||
}
|
||||
}
|
||||
|
||||
async function judge(found, { levels, who, digest, run }) {
|
||||
const helped = run(["--help"]);
|
||||
const versioned = run(["--version"], "", { timeout: VERSION_TIMEOUT });
|
||||
const help = await helped;
|
||||
const v = await versioned;
|
||||
const version = v.status === 0 ? versionLabel(v.stdout) : null;
|
||||
const refuse = (why) => ({ ok: false, message: `${who(version)} ${why}` });
|
||||
if (help.status !== 0) return refuse("can't be checked: --help failed");
|
||||
const lines = help.stdout.split("\n");
|
||||
const has = (flag) => lines.some((l) => optionsIn(l).includes(flag));
|
||||
const line = (flag) => lines.find((l) => optionsIn(l).includes(flag)) ?? "";
|
||||
const missing = FIT_FLAGS.filter((f) => !has(f));
|
||||
if (has("--mode") && !word(line("--mode"), "rpc")) missing.push("--mode rpc");
|
||||
if (has("--thinking")) for (const l of levels) if (!word(line("--thinking"), l)) missing.push(`--thinking ${l}`);
|
||||
if (missing.length) return refuse(`lacks ${missing.join(", ")}`);
|
||||
const noMcp = has("--no-mcp");
|
||||
|
||||
const ask = '{"id":"fit-state","type":"get_state"}\n{"id":"fit-commands","type":"get_commands"}\n';
|
||||
// Every run passes the same -e and --system-prompt, so the runs differ only by the seal flags.
|
||||
const given = (scratch) => ["-e", join(scratch, "explicit", "fit.ts"), "--system-prompt", join(scratch, "prompt.md")];
|
||||
const seal = [...SEAL, ...(noMcp ? ["--no-mcp"] : [])];
|
||||
const loaders = [...LOADERS, ...(noMcp ? [["--no-mcp", "MCP server"]] : [])];
|
||||
const without = (...flags) => seal.filter((f) => !flags.includes(f));
|
||||
const rpcRun = (flags) => run((s) => ["--mode", "rpc", ...flags, ...given(s)], ask, { noMcp });
|
||||
|
||||
// Every run is asked for now, in the order they are judged below; none depends on another.
|
||||
// The control keeps the seal flags no planted source tells apart (--offline, --no-themes,
|
||||
// --no-tools, --no-session), so one of those can't stand in for a loader flag either.
|
||||
const controlRun = rpcRun(without(...loaders.map(([f]) => f)));
|
||||
const rpcSealed = rpcRun(seal);
|
||||
const oneOut = loaders.map(([flag, kind]) => [flag, kind, rpcRun(without(flag))]);
|
||||
// A bus session's shapes, in -p as its turns run: --session-dir in place of --no-session, two -e
|
||||
// as a session passes, and each pair of the adapter's session branches in one run. A -c run gets
|
||||
// a stored session for the project first and must resume it, since -c with none starts afresh.
|
||||
const shaped = SESSION_RUNS.map((shape) => [shape, run((s) => {
|
||||
if (shape.cont) plantSession(s);
|
||||
return sessionShape({ noMcp, extensions: [join(s, "explicit", "fit.ts"), join(s, "explicit", "second.ts")], prompt: join(s, "prompt.md"), sessionDir: join(s, "sessions"), message: "fit check", ...shape });
|
||||
}, "", { noMcp, shape })]);
|
||||
// -p takes a message; offline with no key, pi stops before any model call.
|
||||
const pRun = run((s) => ["-p", ...seal, ...given(s), "fit check"], "", { noMcp });
|
||||
// A missing -e file stops the start. pi 0.85.1 loads the -e files in order and still loads the
|
||||
// one after the missing one, then exits 1 before session_start, so only session_start tells.
|
||||
const missingRuns = [["rpc", ["--mode", "rpc"], ask], ["-p", ["-p"], ""]].map(([mode, args, input]) =>
|
||||
[mode, run((s) => [...args, ...seal, "-e", join(s, "missing", "extension.ts"), ...given(s), ...(mode === "-p" ? ["fit check"] : [])], input, { noMcp })]);
|
||||
|
||||
const control = await controlRun;
|
||||
if (control.stopped) return refuse(`can't be checked: the start without the seal flags ${control.stopped}`);
|
||||
if (!control.answer.state) return refuse("can't be checked: it didn't answer get_state");
|
||||
if (!control.answer.commands) return refuse("can't be checked: it didn't answer get_commands");
|
||||
if (!control.explicit || control.prompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt without the seal flags");
|
||||
// A kind counts only through a source outside the project, except the trusted project files
|
||||
// themselves: a project source can wait on project trust, and then --no-approve alone would keep
|
||||
// it out of the sealed runs and hide a seal flag of its kind that the binary ignores.
|
||||
const shown = new Set(control.seen.filter((s) => s.kind === "trusted project file" || !s.source.startsWith("project ")).map((s) => s.kind));
|
||||
const unshown = KINDS.filter((k) => (k !== "MCP server" || noMcp) && !shown.has(k));
|
||||
if (unshown.length) return refuse(`can't be checked: it didn't load a planted ${unshown.join(", ")} without the seal flags`);
|
||||
// -p answers no get_commands, so the -p runs see skills and prompt templates only through the -e
|
||||
// extension's command list, which must show them here.
|
||||
const listed = new Set(control.listed.filter((s) => !s.source.startsWith("project ")).map((s) => s.kind));
|
||||
const unlisted = ["skill", "prompt template"].filter((k) => !listed.has(k));
|
||||
if (unlisted.length) return refuse(`can't be checked: its -e extension didn't list a planted ${unlisted.join(", ")} without the seal flags`);
|
||||
|
||||
const rpc = await rpcSealed;
|
||||
if (rpc.stopped) return refuse(`can't be checked: the start with the seal flags ${rpc.stopped}`);
|
||||
if (!rpc.answer.state) return refuse("can't be checked: it didn't answer get_state with the seal flags on");
|
||||
if (!rpc.answer.commands) return refuse("can't be checked: it didn't answer get_commands with the seal flags on");
|
||||
if (!rpc.explicit) return refuse("didn't load an -e extension with the seal flags on");
|
||||
if (rpc.prompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt with the seal flags on");
|
||||
if (!rpc.prompt.includes(GIVEN)) return refuse("didn't use the --system-prompt file with the seal flags on");
|
||||
if (rpc.seen.length) return refuse(`loaded a planted ${rpc.seen.map((s) => s.source).join(", ")} with the seal flags on`);
|
||||
|
||||
// Each loader flag on its own: with every other seal flag on, its kind must show.
|
||||
for (const [flag, kind, pending] of oneOut) {
|
||||
const r = await pending;
|
||||
const but = `with every seal flag but ${flag}`;
|
||||
if (r.stopped) return refuse(`can't be checked: the start ${but} ${r.stopped}`);
|
||||
if (!r.answer.state || !r.answer.commands) return refuse(`can't be checked: it didn't answer get_state and get_commands ${but}`);
|
||||
if (r.prompt === null) return refuse(`can't be checked: its -e extension didn't see the system prompt ${but}`);
|
||||
if (!r.seen.some((s) => s.kind === kind)) return refuse(`can't be checked: it didn't load a planted ${kind} ${but}`);
|
||||
const others = r.seen.filter((s) => s.kind !== kind && !(flag === "--no-extensions" && !noMcp && s.kind === "MCP server"));
|
||||
if (others.length) return refuse(`loaded a planted ${others.map((s) => s.source).join(", ")} ${but}`);
|
||||
}
|
||||
|
||||
for (const [shape, pending] of shaped) {
|
||||
const r = await pending;
|
||||
const name = shapeName(shape);
|
||||
if (r.stopped) return refuse(`can't be checked: the start ${name} ${r.stopped}`);
|
||||
if (r.prompt === null) return refuse(`can't be checked: its -e extension didn't see the system prompt ${name}`);
|
||||
if (!r.explicit || !r.second) return refuse(`didn't load every -e extension ${name}`);
|
||||
if (!r.prompt.includes(GIVEN)) return refuse(`didn't use the --system-prompt file ${name}`);
|
||||
if (shape.cont && r.session !== PLANTED_SESSION) return refuse(`can't be checked: it didn't resume the planted session with -c ${name}`);
|
||||
if (r.seen.length) return refuse(`loaded a planted ${r.seen.map((x) => x.source).join(", ")} with the seal flags ${name}`);
|
||||
}
|
||||
|
||||
const p = await pRun;
|
||||
if (p.stopped) return refuse(`can't be checked: a -p start with the seal flags ${p.stopped}`);
|
||||
if (p.prompt === null) return refuse("can't be checked: a -p start with the seal flags didn't run its -e extension");
|
||||
if (p.seen.length) return refuse(`loaded a planted ${p.seen.map((x) => x.source).join(", ")} with the seal flags on in -p`);
|
||||
|
||||
for (const [mode, pending] of missingRuns) {
|
||||
const r = await pending;
|
||||
if (r.stopped) return refuse(`can't be checked: a ${mode} start with a missing -e extension ${r.stopped}`);
|
||||
if (r.status === 0 || r.answer.state || r.prompt !== null) return refuse("starts with a missing -e extension");
|
||||
}
|
||||
return { ok: true, version, noMcp, digest };
|
||||
}
|
||||
|
||||
// Whether bwrap runs here, as sandbox.mjs's sandboxProblem asks, but as child processes the caller
|
||||
// awaits: the system part, the network unshared, an empty environment, then /usr/bin/true. And,
|
||||
// once per check, whether unshare's outer layer runs (checkArgv in sandbox.mjs): first on its own,
|
||||
// then with bwrap inside it. Returns { problem } when the check can't run, naming the tool that
|
||||
// failed without its own text, else { outer, why }: outer is the unshare to pass checkArgv, or null
|
||||
// when the layer can't run here, and why then names unshare and how it failed. A missing,
|
||||
// too old or refused unshare drops only that layer and is no refusal: bwrap's sandbox is the same
|
||||
// either way. The probes run in a scratch directory of their own and name it on their command
|
||||
// line, so a probe process a killed owner leaves is one the next sweep finds.
|
||||
async function sandboxLayers({ bwrap, unshare, node }) {
|
||||
if (!existsSync(bwrap)) return { problem: "the sandbox (bwrap) is missing" };
|
||||
if (!existsSync(SETPRIV)) return { problem: "the sandbox's parent-death tool (setpriv) is missing" };
|
||||
const scratch = mkdtempSync(join(tmpdir(), scratchPrefix()));
|
||||
let stopped = false;
|
||||
try {
|
||||
const probe = async (argv) => {
|
||||
const r = await spawned(argv, { input: "", timeout: 10000, signal: new AbortController().signal });
|
||||
stopped ||= Boolean(r.stopped);
|
||||
return r.status === 0;
|
||||
};
|
||||
const sandboxed = (outer) => checkArgv([{ kind: "ro", src: scratch }], ["/usr/bin/true"], { chdir: scratch, node, bwrap, unshare: outer });
|
||||
let why = "unshare is missing";
|
||||
if (existsSync(unshare)) {
|
||||
if (!await probe(outerArgv(["/usr/bin/true", scratch], { unshare }))) why = "unshare failed";
|
||||
else if (await probe(sandboxed(unshare))) return { outer: unshare, why: null };
|
||||
else why = "unshare failed with bwrap inside it";
|
||||
}
|
||||
if (!await probe(sandboxed(null))) return { problem: "the sandbox (bwrap) failed" };
|
||||
return { outer: null, why };
|
||||
} finally {
|
||||
// As for a run (once, in piFit): a probe killed at its timeout may leave what names its directory.
|
||||
if (stopped) stopNaming([scratch], process.getuid());
|
||||
rmSync(scratch, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
// The binary, inside the sandbox: the system part, the found package where a session sees it, the
|
||||
// scratch directory read-write, and its tmp/ as /tmp, as a session has it. The environment is
|
||||
// fitEnv's. The binary starts through /bin/sh, as the adapter starts it, so it also gets the
|
||||
// variables the shell sets. The run is a child process the caller awaits; nothing here blocks.
|
||||
// stopped: why the run was killed ("timed out", "printed more than 4 MB", "was stopped"), or null.
|
||||
function runIn(found, { bwrap, unshare, node, scratch, env, args, input, timeout, signal }) {
|
||||
const list = [
|
||||
{ kind: "ro", src: found.root, ...(found.dest ? { dest: found.dest } : {}) },
|
||||
{ kind: "rw", src: scratch },
|
||||
{ kind: "rw", src: join(scratch, "tmp"), dest: "/tmp" },
|
||||
];
|
||||
return spawned(checkArgv(list, ["/bin/sh", "-c", 'exec "$0" "$@"', found.view, ...args], { env, chdir: join(scratch, "project"), node, bwrap, unshare }), { input, timeout, signal });
|
||||
}
|
||||
|
||||
// One child process with an empty environment, awaited: its exit status, its stdout, and why it was
|
||||
// killed, or null. It settles PIPE_GRACE after the child exits or is killed even if its pipes stay
|
||||
// open: a process left behind can hold them (a kill of bwrap's outer process while its child still
|
||||
// sets up leaves that child waiting until the pid namespace checkArgv makes is killed, or, without
|
||||
// that layer, until the next check's sweep), and no wait here is unbounded.
|
||||
function spawned(argv, { input, timeout, signal }) {
|
||||
return new Promise((resolve) => {
|
||||
const out = [];
|
||||
const size = { stdout: 0, stderr: 0 };
|
||||
let stopped = null;
|
||||
let done = false;
|
||||
let child;
|
||||
let timer = null;
|
||||
let grace = null;
|
||||
let exited = null;
|
||||
const finish = (status, sig) => {
|
||||
if (done) return;
|
||||
done = true;
|
||||
clearTimeout(timer);
|
||||
clearTimeout(grace);
|
||||
child?.stdout?.destroy();
|
||||
child?.stderr?.destroy();
|
||||
signal.removeEventListener("abort", onAbort);
|
||||
if (!stopped && sig === "SIGKILL") stopped = "timed out";
|
||||
resolve({ status, stdout: Buffer.concat(out).toString("utf8"), stopped });
|
||||
};
|
||||
const settleSoon = () => { if (!grace) grace = setTimeout(() => finish(exited?.status ?? null, exited?.sig ?? "SIGKILL"), PIPE_GRACE); };
|
||||
const kill = (why) => {
|
||||
if (!stopped) stopped = why;
|
||||
try { child?.kill("SIGKILL"); } catch {}
|
||||
settleSoon();
|
||||
};
|
||||
const onAbort = () => kill("was stopped");
|
||||
try {
|
||||
child = spawn(argv[0], argv.slice(1), { env: {}, stdio: ["pipe", "pipe", "pipe"] });
|
||||
} catch {
|
||||
finish(null, null);
|
||||
return;
|
||||
}
|
||||
timer = setTimeout(() => kill("timed out"), timeout);
|
||||
if (signal.aborted) onAbort();
|
||||
else signal.addEventListener("abort", onAbort, { once: true });
|
||||
const count = (stream) => (chunk) => {
|
||||
size[stream] += chunk.length;
|
||||
if (size[stream] > OUTPUT_MAX) kill(`printed more than ${OUTPUT_MAX / 1024 / 1024} MB`);
|
||||
else if (stream === "stdout") out.push(chunk);
|
||||
};
|
||||
child.stdout.on("data", count("stdout"));
|
||||
child.stderr.on("data", count("stderr"));
|
||||
child.stdin.on("error", () => {});
|
||||
child.on("error", () => finish(null, null));
|
||||
child.on("exit", (status, sig) => { exited = { status, sig }; settleSoon(); });
|
||||
child.on("close", (status, sig) => finish(status, sig));
|
||||
child.stdin.end(input);
|
||||
});
|
||||
}
|
||||
|
||||
// A fit run's environment: what a bus session's pi gets, built the same way from host, the
|
||||
// environment of the process sessions launch from (the bus's own). The launcher passes host's
|
||||
// ENV_ALLOW names that are set through sessionEnv (less the provider variables here, since no key
|
||||
// is passed: no model is called), with host's PATH behind the repository's .bin; the sandbox sets
|
||||
// sessionSetenv's (HOME is the scratch home, so pi's agent directory is $HOME/.pi/agent); the
|
||||
// runner adds adapterEnv's with the pi module's harnessEnv, and MOSAIC_REQUEST for each turn. No
|
||||
// name is added that a session in that process would lack: a bus started by its unit has no TERM,
|
||||
// TZ or LC_ALL, and then no fit run has them either. shape: a session-shaped run's branches (its
|
||||
// tool list, thinking level and --mode json, which a proxy session's first turn takes with the
|
||||
// model event monitor's variables), else none: no tools, no thinking, one -e. noMcp: --help lists
|
||||
// --no-mcp. pi-fit.test.mjs fails when these names, or the values the code sets, differ from a bus
|
||||
// session's, for a login shell's host and a unit's.
|
||||
export function fitEnv(scratch, { view, noMcp = false, shape = null, host = process.env }) {
|
||||
const runner = { ...sessionEnv(withoutProviders(host), "fit"), ...sessionSetenv(scratch, { business: "fit", role: "fit" }) };
|
||||
const { tools = "", thinking = "", json = false } = shape ?? {};
|
||||
const session = { provider: SESSION_SHAPE.provider, model: SESSION_SHAPE.model, thinking };
|
||||
const extensions = [join(scratch, "explicit", "fit.ts"), ...(shape ? [join(scratch, "explicit", "second.ts")] : [])];
|
||||
const env = adapterEnv({
|
||||
...session,
|
||||
bundle: { prompt: join(scratch, "prompt.md"), policy: join(scratch, "policy.json"), tools: join(scratch, "tools.json") },
|
||||
workspace: join(scratch, "project"), sessionDir: join(scratch, "sessions"), toolSocket: join(scratch, "tools.sock"), turnMarker: join(scratch, "turn"),
|
||||
harnessEnv: harnessEnv({ bin: view, noMcp, tools: tools ? tools.split(",") : [], extensions, session }),
|
||||
runDir: scratch, proxyFirstTurn: json,
|
||||
}, runner);
|
||||
return { ...env, MOSAIC_REQUEST: "fit check" };
|
||||
}
|
||||
|
||||
// The options one help line lists in its option column: a line that starts with "-" (after its
|
||||
// indent), up to the first run of two spaces or a tab, split at commas; the first word of each
|
||||
// part. " --extension, -e <path> Load an extension" lists --extension and -e. A flag named in a
|
||||
// sentence is not listed.
|
||||
function optionsIn(line) {
|
||||
const t = line.trim();
|
||||
if (!t.startsWith("-")) return [];
|
||||
return t.split(/ {2,}|\t/)[0].split(",").map((part) => part.trim().split(/[ =<[]/)[0]).filter((o) => /^--?[A-Za-z0-9]/.test(o));
|
||||
}
|
||||
const word = (line, w) => line.split(/[^A-Za-z0-9-]+/).includes(w);
|
||||
|
||||
// The version a refusal line may carry: a version-shaped first line, else none. Anything else the
|
||||
// binary prints stays out of the line.
|
||||
function versionLabel(stdout) {
|
||||
const first = String(stdout).split("\n")[0].trim();
|
||||
return /^v?[0-9]+(\.[0-9]+){0,3}([-+][0-9A-Za-z.-]{1,32})?$/.test(first) ? first : null;
|
||||
}
|
||||
|
||||
// The rpc answers in the output: whether get_state succeeded, and the command names get_commands
|
||||
// listed, or null when it didn't answer with a { commands: [...] } list.
|
||||
function answered(stdout) {
|
||||
let state = false;
|
||||
let commands = null;
|
||||
for (const l of String(stdout).split("\n")) {
|
||||
let m;
|
||||
try { m = JSON.parse(l); } catch { continue; }
|
||||
if (!m || m.type !== "response" || m.success !== true) continue;
|
||||
if (m.id === "fit-state" && m.command === "get_state" && m.data && typeof m.data === "object") state = true;
|
||||
if (m.id === "fit-commands" && m.command === "get_commands" && Array.isArray(m.data?.commands)) {
|
||||
commands = m.data.commands.filter((c) => typeof c?.name === "string").map((c) => c.name);
|
||||
}
|
||||
}
|
||||
return { state, commands };
|
||||
}
|
||||
|
||||
// The planted sources, each with the kind the control must show. Each extension and each MCP
|
||||
// server touches its own marker; each skill and prompt template has a name get_commands lists;
|
||||
// each context and system prompt file holds a word the -e extension's view of the system prompt
|
||||
// shows. "user" sources sit in the agent directory, $HOME/.pi/agent: no fit run sets
|
||||
// PI_CODING_AGENT_DIR, as no bus session does. "settings" sources are listed by a settings.json;
|
||||
// "package" sources come from a local package a settings.json lists. "parent" is a directory above
|
||||
// the project.
|
||||
const KINDS = ["extension", "MCP server", "skill", "prompt template", "context file", "trusted project file"];
|
||||
const MARKED = {
|
||||
"user extension": "extension", "user settings extension": "extension", "user package extension": "extension",
|
||||
"project extension": "extension", "project settings extension": "extension", "project package extension": "extension",
|
||||
"user mcp.json": "MCP server", "project mcp.json": "MCP server", "project .mcp.json": "MCP server",
|
||||
};
|
||||
const NAMED = {
|
||||
"fit-user-skill": ["user skill", "skill"], "fit-user-set-skill": ["user settings skill", "skill"], "fit-user-pkg-skill": ["user package skill", "skill"],
|
||||
"fit-project-skill": ["project skill", "skill"], "fit-project-set-skill": ["project settings skill", "skill"],
|
||||
"fit-project-pkg-skill": ["project package skill", "skill"], "fit-agents-skill": ["project .agents skill", "skill"], "fit-home-skill": ["home .agents skill", "skill"],
|
||||
"fit-user-prompt": ["user prompt template", "prompt template"], "fit-user-set-prompt": ["user settings prompt template", "prompt template"],
|
||||
"fit-user-pkg-prompt": ["user package prompt template", "prompt template"],
|
||||
"fit-project-prompt": ["project prompt template", "prompt template"], "fit-project-set-prompt": ["project settings prompt template", "prompt template"],
|
||||
"fit-project-pkg-prompt": ["project package prompt template", "prompt template"],
|
||||
};
|
||||
// Planted file (under the scratch directory), the word it holds, the source, the kind. A project
|
||||
// SYSTEM.md or APPEND_SYSTEM.md loads only in a trusted project: the saved trust makes it trusted,
|
||||
// and only --no-approve undoes that with the seal flags on.
|
||||
const TEXTS = [
|
||||
["home/.pi/agent/SYSTEM.md", "FITUSERSYSTEM", "user SYSTEM.md", "context file"],
|
||||
["home/.pi/agent/AGENTS.md", "FITUSERAGENTS", "user AGENTS.md", "context file"],
|
||||
["project/.pi/SYSTEM.md", "FITPROJECTSYSTEM", "project SYSTEM.md", "trusted project file"],
|
||||
["project/.pi/APPEND_SYSTEM.md", "FITPROJECTAPPEND", "project APPEND_SYSTEM.md", "trusted project file"],
|
||||
["project/AGENTS.md", "FITPROJECTAGENTS", "project AGENTS.md", "context file"],
|
||||
["project/CLAUDE.md", "FITPROJECTCLAUDE", "project CLAUDE.md", "context file"],
|
||||
["CLAUDE.md", "FITPARENTCLAUDE", "parent CLAUDE.md", "context file"],
|
||||
];
|
||||
// The --system-prompt file's word, which the sealed runs must show.
|
||||
const GIVEN = "FITGIVENPROMPT";
|
||||
// The -e extension every run passes, which must load, and the second one the session-shaped runs
|
||||
// pass after it.
|
||||
const EXPLICIT = "explicit extension";
|
||||
const SECOND = "second explicit extension";
|
||||
// The id of the session a -c run must resume.
|
||||
const PLANTED_SESSION = "fit-planted-session";
|
||||
const markerName = (source) => source.replace(/[^a-z]/g, "-");
|
||||
const marked = (scratch, source) => { try { return readdirSync(join(scratch, "marks")).includes(markerName(source)); } catch { return false; } };
|
||||
// What the -e extension saw at session_start ({ prompt, commands, session }), or null when it wrote
|
||||
// nothing that parses into that shape.
|
||||
function started(scratch) {
|
||||
let m;
|
||||
try { m = JSON.parse(readFileSync(join(scratch, "marks", "start.json"), "utf8")); } catch { return null; }
|
||||
if (!m || typeof m.prompt !== "string" || !Array.isArray(m.commands)) return null;
|
||||
return { prompt: m.prompt, commands: m.commands.filter((c) => typeof c === "string"), session: typeof m.session === "string" ? m.session : null };
|
||||
}
|
||||
// The planted skills and prompt templates among command names, as { source, kind }.
|
||||
const named = (names) => Object.entries(NAMED).filter(([name]) => names.some((c) => c === name || c.endsWith(`:${name}`))).map(([, [source, kind]]) => ({ source, kind }));
|
||||
|
||||
function plant(scratch) {
|
||||
const marks = join(scratch, "marks");
|
||||
const put = (path, text) => { mkdirSync(dirname(path), { recursive: true }); writeFileSync(path, text); };
|
||||
const ext = (source) => `import { writeFileSync } from "node:fs";\nexport default function (): void { writeFileSync(${JSON.stringify(join(marks, markerName(source)))}, ""); }\n`;
|
||||
const mcp = (source) => JSON.stringify({ mcpServers: { fit: { command: "/bin/sh", args: ["-c", `: > ${join(marks, markerName(source))}`] } } });
|
||||
const skill = (name) => `---\nname: ${name}\ndescription: a planted skill\n---\nA planted skill.\n`;
|
||||
const prompt = "A planted prompt template.\n";
|
||||
const agent = join(scratch, "home", ".pi", "agent");
|
||||
const project = join(scratch, "project");
|
||||
mkdirSync(marks, { recursive: true });
|
||||
mkdirSync(join(scratch, "tmp"), { recursive: true });
|
||||
mkdirSync(project, { recursive: true });
|
||||
// The session-shaped runs' --session-dir, made as the adapter makes it.
|
||||
mkdirSync(join(scratch, "sessions"), { recursive: true });
|
||||
// Each settings.json lists an extension, a skill directory, a prompt template and a local
|
||||
// package holding one of each.
|
||||
const listed = (who) => {
|
||||
const dir = join(scratch, "listed", who);
|
||||
put(join(dir, "fit.ts"), ext(`${who} settings extension`));
|
||||
put(join(dir, `fit-${who}-set-skill`, "SKILL.md"), skill(`fit-${who}-set-skill`));
|
||||
put(join(dir, `fit-${who}-set-prompt.md`), prompt);
|
||||
const pkg = join(dir, "package");
|
||||
put(join(pkg, "package.json"), JSON.stringify({ name: `fit-${who}-package`, version: "1.0.0" }));
|
||||
put(join(pkg, "extensions", "fit.ts"), ext(`${who} package extension`));
|
||||
put(join(pkg, "skills", `fit-${who}-pkg-skill`, "SKILL.md"), skill(`fit-${who}-pkg-skill`));
|
||||
put(join(pkg, "prompts", `fit-${who}-pkg-prompt.md`), prompt);
|
||||
return { extensions: [join(dir, "fit.ts")], skills: [join(dir, `fit-${who}-set-skill`)], prompts: [join(dir, `fit-${who}-set-prompt.md`)], packages: [pkg] };
|
||||
};
|
||||
// A saved trust for the project, so the control loads its trust-gated files without --approve.
|
||||
put(join(agent, "settings.json"), JSON.stringify({ ...listed("user"), defaultProjectTrust: "always" }));
|
||||
put(join(agent, "trust.json"), JSON.stringify({ [realpathSync(project)]: true }));
|
||||
put(join(agent, "extensions", "fit.ts"), ext("user extension"));
|
||||
put(join(agent, "mcp.json"), mcp("user mcp.json"));
|
||||
put(join(agent, "skills", "fit-user-skill", "SKILL.md"), skill("fit-user-skill"));
|
||||
put(join(agent, "prompts", "fit-user-prompt.md"), prompt);
|
||||
put(join(project, ".pi", "settings.json"), JSON.stringify(listed("project")));
|
||||
put(join(project, ".pi", "extensions", "fit.ts"), ext("project extension"));
|
||||
put(join(project, ".pi", "mcp.json"), mcp("project mcp.json"));
|
||||
put(join(project, ".mcp.json"), mcp("project .mcp.json"));
|
||||
put(join(project, ".pi", "skills", "fit-project-skill", "SKILL.md"), skill("fit-project-skill"));
|
||||
put(join(project, ".agents", "skills", "fit-agents-skill", "SKILL.md"), skill("fit-agents-skill"));
|
||||
put(join(project, ".pi", "prompts", "fit-project-prompt.md"), prompt);
|
||||
put(join(scratch, "home", ".agents", "skills", "fit-home-skill", "SKILL.md"), skill("fit-home-skill"));
|
||||
for (const [file, text] of TEXTS) put(join(scratch, file), `${text}\n`);
|
||||
put(join(scratch, "explicit", "fit.ts"), `import { writeFileSync } from "node:fs";\nexport default function (pi: any): void {\n writeFileSync(${JSON.stringify(join(marks, markerName(EXPLICIT)))}, "");\n pi.on("session_start", (_event: any, ctx: any) => { writeFileSync(${JSON.stringify(join(marks, "start.json"))}, JSON.stringify({ prompt: ctx.getSystemPrompt(), commands: pi.getCommands().map((c: any) => c.name), session: ctx.sessionManager.getSessionId() })); });\n}\n`);
|
||||
put(join(scratch, "explicit", "second.ts"), ext(SECOND));
|
||||
put(join(scratch, "prompt.md"), `${GIVEN}\n`);
|
||||
}
|
||||
|
||||
// A stored session for the project, as pi 0.85.1 writes one: a header whose cwd is the project's
|
||||
// real path (pi's -c takes only a session for its own cwd) and one user message.
|
||||
function plantSession(scratch) {
|
||||
const at = new Date().toISOString();
|
||||
writeFileSync(join(scratch, "sessions", `${PLANTED_SESSION}.jsonl`), [
|
||||
{ type: "session", version: 3, id: PLANTED_SESSION, timestamp: at, cwd: realpathSync(join(scratch, "project")) },
|
||||
{ type: "message", id: "f17e0001", parentId: null, timestamp: at, message: { role: "user", content: [{ type: "text", text: "an earlier turn" }], timestamp: Date.now() } },
|
||||
].map((e) => `${JSON.stringify(e)}\n`).join(""));
|
||||
}
|
||||
|
||||
// The planted sources a run loaded, as { source, kind }: markers present, planted names among the
|
||||
// command names, and planted words in the system prompt the -e extension saw.
|
||||
function seen(scratch, commands, start) {
|
||||
let marks = [];
|
||||
try { marks = readdirSync(join(scratch, "marks")); } catch { marks = []; }
|
||||
const out = Object.entries(MARKED).filter(([s]) => marks.includes(markerName(s))).map(([source, kind]) => ({ source, kind }));
|
||||
out.push(...named(commands));
|
||||
const text = start?.prompt ?? "";
|
||||
for (const [, word, source, kind] of TEXTS) if (text.includes(word)) out.push({ source, kind });
|
||||
return out;
|
||||
}
|
||||
|
||||
// sha256 over the sorted (path, sha256) list of the package's package.json, every regular file
|
||||
// under dist/bundle/ (design section 4), every regular file in the directory tree that holds the
|
||||
// binary's real file, so a bin outside dist/bundle/ can't import a file the digest skips, and npm's
|
||||
// lock files for a copy mounted with its node_modules. A symlink there is read through.
|
||||
function filesDigest(found) {
|
||||
const pkg = found.pkg;
|
||||
const bin = realpathSync(found.bin);
|
||||
const files = new Set([join(pkg, "package.json"), bin]);
|
||||
const walk = (dir) => {
|
||||
let entries;
|
||||
try { entries = readdirSync(dir, { withFileTypes: true }); } catch (e) { if (e.code === "ENOENT") return; throw e; }
|
||||
for (const e of entries) {
|
||||
const p = join(dir, e.name);
|
||||
if (e.isDirectory()) walk(p);
|
||||
else if (e.isFile() || (e.isSymbolicLink() && lstatSync(realpathSync(p)).isFile())) files.add(p);
|
||||
if (files.size > DIGEST_FILES_MAX) throw new Error("too many files");
|
||||
}
|
||||
};
|
||||
walk(join(pkg, "dist", "bundle"));
|
||||
const binDir = dirname(bin);
|
||||
if (binDir !== pkg && binDir.startsWith(`${pkg}/`)) walk(binDir);
|
||||
// A copy mounted with the node_modules it sits in imports packages beside it: npm's lock files
|
||||
// there stand for them.
|
||||
if (found.root !== pkg) for (const f of [join(found.root, ".package-lock.json"), join(dirname(found.root), "package-lock.json")]) {
|
||||
try { lstatSync(f); } catch (e) { if (e.code === "ENOENT") continue; throw e; }
|
||||
files.add(f);
|
||||
}
|
||||
const h = createHash("sha256");
|
||||
for (const f of [...files].sort()) {
|
||||
h.update(relative(pkg, f)).update("\0").update(createHash("sha256").update(readFileSync(f)).digest("hex")).update("\n");
|
||||
}
|
||||
return h.digest("hex");
|
||||
}
|
||||
@@ -4,7 +4,8 @@ import { createHash } from "node:crypto";
|
||||
import { mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { BundleError, buildBundle, sessionModel, SOURCES } from "../src/bundle.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
import { decide } from "../src/gate.mjs";
|
||||
import { resolveInstance, validateBusinessDocument } from "../../organization/src/index.mjs";
|
||||
import { businessDoc, REPO_ROLES, systemFor } from "../../organization/tests/helpers.mjs";
|
||||
|
||||
@@ -12,7 +12,8 @@ import { join, relative } from "node:path";
|
||||
import { buildBundle, PERSONA_CHARS } from "../src/bundle.mjs";
|
||||
import { decide } from "../src/gate.mjs";
|
||||
import { adapterEnv } from "../src/runner.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
|
||||
const sha = (data) => createHash("sha256").update(data).digest("hex");
|
||||
const CREATIVE = ["companion", "ideas", "research", "proofreader", "fact-checker", "continuity"];
|
||||
@@ -177,7 +178,8 @@ function runAdapter(t, harness, b, { prompt = b.files.prompt, cwd } = {}) {
|
||||
writeFileSync(cli, '#!/bin/sh\nprintf "%s\\0" "$@" > "$ARGV_OUT"\necho \'{"type":"result","subtype":"success","result":"ok"}\'\n');
|
||||
chmodSync(cli, 0o755);
|
||||
const s = { harness, provider: harness === "pi" ? "probe" : null, model: "claude-sonnet-5-5", workspace: b.workspace, sessionDir: join(b.dir, "session"), toolSocket: join(b.dir, "tools.sock"), turnMarker: join(b.dir, "turn.done"), bundle: { ...b.files, prompt }, harnessEnv: b.env };
|
||||
const env = { ...adapterEnv(s, { PATH: `${bin}:/usr/bin:/bin`, HOME: join(b.dir, "home") }), ARGV_OUT: join(b.dir, "argv"), MOSAIC_REQUEST: "x" };
|
||||
// #1873 S2: the pi adapter runs MOSAIC_PI_BIN, which the bundle sets to the pi it found.
|
||||
const env = { ...adapterEnv(s, { PATH: `${bin}:/usr/bin:/bin`, HOME: join(b.dir, "home") }), ARGV_OUT: join(b.dir, "argv"), MOSAIC_REQUEST: "x", ...(harness === "pi" ? { MOSAIC_PI_BIN: cli } : {}) };
|
||||
const r = spawnSync("/bin/sh", [join(REPO, "adapters", harness === "pi" ? "pi" : "claude", "adapter.sh")], { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], env });
|
||||
const argv = existsSync(env.ARGV_OUT) ? readFileSync(env.ARGV_OUT, "utf8").split("\0") : null;
|
||||
return { ...r, argv };
|
||||
|
||||
@@ -31,6 +31,12 @@ async function harnessModule(name) {
|
||||
return loader.modules("harness")[0];
|
||||
}
|
||||
export const HARNESS = Object.freeze({ pi: await harnessModule("pi"), "claude-code": await harnessModule("claude-code") });
|
||||
// #1873 re-check 7: a module whose fit check runs async has ready(), which the launcher's preflight
|
||||
// stands for. A test that calls buildBundle directly awaits this first. It is not run on import:
|
||||
// request-agent.mjs imports this file in every tool-call process, which builds no bundle.
|
||||
export async function harnessReady() {
|
||||
for (const mod of Object.values(HARNESS)) if (typeof mod.ready === "function") await mod.ready();
|
||||
}
|
||||
|
||||
// A session's route is its own three vars (row 65); a role the test doesn't route runs pi on the
|
||||
// test system's old execution.model at thinking high, so the bundle tests keep their models.
|
||||
|
||||
@@ -6,7 +6,8 @@ import { join } from 'node:path';
|
||||
import { typedTools } from '../src/tools.mjs';
|
||||
import { buildBundle } from '../src/bundle.mjs';
|
||||
import { TYPES } from '../../library/src/mapping.mjs';
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch } from './helpers.mjs';
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch, harnessReady } from './helpers.mjs';
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
|
||||
const LOOKUPS = ['library_find', 'library_get', 'library_search', 'library_manuscript', 'library_universe'];
|
||||
const CREATIVE = ['companion', 'research', 'ideas', 'proofreader', 'fact-checker', 'continuity'];
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -10,7 +10,8 @@ import { join } from "node:path";
|
||||
import { buildBundle } from "../src/bundle.mjs";
|
||||
import { adapterEnv, unslopCounts, unslopLog, unslopNotes } from "../src/runner.mjs";
|
||||
import { REPLY_CAP } from "../src/tools.mjs";
|
||||
import { fakeToolSocket, HARNESS, mockAnthropic, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
|
||||
import { fakeToolSocket, HARNESS, mockAnthropic, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
|
||||
const ADAPTER = join(REPO, "adapters", "pi", "adapter.sh");
|
||||
|
||||
|
||||
@@ -0,0 +1,241 @@
|
||||
// #1873 pi-unpin S3: a stand-in pi that the fit check (src/pi-fit.mjs) can run. It is one sh
|
||||
// script with its knobs written into it, because the fit check runs it with an empty
|
||||
// environment. Put it at dist/bundle/cli.js in a package named @earendil-works/pi-coding-agent.
|
||||
//
|
||||
// What it does, in order:
|
||||
// --version prints `version` (default 9.9.9), or exits `versionExit` when that is set, or
|
||||
// hangs for a minute with `versionHang`
|
||||
// $ARGV_OUT set: a session. Writes its argv there, prints `sessionOut`, exits 0. This part is
|
||||
// sh, so it runs in a sandbox that holds no node.
|
||||
// otherwise runs node (the absolute path, since the fit check gives it no PATH) on the rest:
|
||||
// --help lists every flag the fit check wants, less `dropFlags`, plus `extraFlags`, then
|
||||
// the `helpProse` lines as they are
|
||||
// -e <file> a file that doesn't exist stops it with exit 1, unless `missingExtOk` is true or
|
||||
// names the mode ("rpc" or "print") it starts in anyway
|
||||
// --mode rpc loads the planted sources the way pi 0.85.1 finds them, honouring the seal flags,
|
||||
// or -p runs each -e extension's session_start (one that writes JSON.stringify(...) gets
|
||||
// the prompt this stand-in builds, the command names and the session id), then
|
||||
// answers get_state and get_commands
|
||||
// (rpc), or prints `sessionOut` and exits 0 (-p).
|
||||
// An -e file that exists loads even with --no-extensions, unless `dropExplicit`
|
||||
// (then it loads only without --no-extensions). `printNoStart`: -p runs no
|
||||
// session_start. MCP servers stay out with --no-mcp when `extraFlags` lists it, else
|
||||
// with --no-extensions.
|
||||
// `ignoreAll` loads no planted source. `sealLeak` lists kinds (extensions, mcp,
|
||||
// skills, prompts, context, approve) loaded even when their seal flag is on.
|
||||
// `ignoreSystemPrompt` drops the --system-prompt file. `ignoreAppend` drops every
|
||||
// APPEND_SYSTEM.md. `sysReplacesAll`: a --system-prompt file also drops every
|
||||
// APPEND_SYSTEM.md and context file. `trustExtOnly`: the only extensions are the
|
||||
// project's .pi/extensions, loaded in a trusted project whatever --no-extensions
|
||||
// says. `trustCtxOnly`: the only context file is the project's AGENTS.md, loaded the
|
||||
// same way whatever --no-context-files says. `flood`: prints 8 MB first.
|
||||
// `hideWhen`: a cue, a flag or "order" (--offline before --no-session). With the cue
|
||||
// it loads no planted source; without it, every kind even with its seal flag on.
|
||||
// `standIn`: [flag, other]: it ignores flag, and other keeps flag's kind out instead.
|
||||
// `remember`: the first run in a scratch directory (it leaves a marker beside the
|
||||
// -e file's directory) loads every kind whatever the seal flags; a later run loads
|
||||
// none. `needs`: a flag the loader flags work only with; without it they do
|
||||
// nothing. `leakWithout`: flags; when any of them is absent, every kind loads.
|
||||
// `leakWith`: flags; when all of them are given, every kind loads (with
|
||||
// `leakWithout` too, both must hold). `mcpAlso`: a flag that keeps MCP servers out
|
||||
// as well as their own flag does. `leakNoAgentDir`: every kind loads when
|
||||
// PI_CODING_AGENT_DIR is unset, as in a bus session. `leakNoVar`: names; every
|
||||
// kind loads when any of them is unset (TERM, TZ, LC_ALL in a unit-started bus).
|
||||
// `leakNoMatch`: with -c, every
|
||||
// kind loads when no .jsonl in --session-dir names the working directory.
|
||||
// `leakEnv`: { json, other }, the variable names a run with --mode json and any
|
||||
// other run should get; every kind loads when the environment holds a name not in
|
||||
// the list or lacks one, apart from the names /bin/sh sets (PWD, OLDPWD, SHLVL, _).
|
||||
// An -e extension that writes JSON.stringify({...}) at session_start gets the
|
||||
// prompt, the command names, the session id (with -c, the id in the header of a
|
||||
// .jsonl in --session-dir whose cwd is the working directory, else a new one) and
|
||||
// the --thinking level (else "medium"). `noResume`: -c always starts a new
|
||||
// session. `oneExt`: only the first -e loads. `extNoCommands`: the extension sees
|
||||
// no commands.
|
||||
// `commands` "fail" answers get_commands with success false; "list" answers
|
||||
// with a bare list.
|
||||
// otherwise a session with no $ARGV_OUT: prints `sessionOut` and exits 0.
|
||||
//
|
||||
// Trust, as pi 0.85.1 has it: the project is trusted with --approve, or with a saved trust and
|
||||
// no --no-approve. Only the project SYSTEM.md and APPEND_SYSTEM.md wait on it here. The user
|
||||
// APPEND_SYSTEM.md is appended whatever the flags, as 0.85.1 does when the project has none.
|
||||
import { FIT_FLAGS } from "../src/pi-fit.mjs";
|
||||
|
||||
const BODY = String.raw`
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const cp = require("node:child_process");
|
||||
const argv = process.argv.slice(1);
|
||||
const has = (f) => argv.includes(f);
|
||||
const val = (f) => { const i = argv.indexOf(f); return i === -1 ? undefined : argv[i + 1]; };
|
||||
if (argv[0] === "--help") {
|
||||
const drop = new Set(K.dropFlags || []);
|
||||
const lines = ["Usage: pi [options] [messages...]", "", "Options:"];
|
||||
for (const f of FLAGS) {
|
||||
if (drop.has(f)) continue;
|
||||
if (f === "--mode") lines.push(" --mode <mode> Output mode: " + (K.modes || ["text", "json", "rpc"]).join(", "));
|
||||
else if (f === "--thinking") lines.push(" --thinking <level> Thinking level: " + (K.levels || ["off", "minimal", "low", "medium", "high", "xhigh", "max"]).join(", "));
|
||||
else lines.push(" " + f);
|
||||
}
|
||||
for (const f of K.extraFlags || []) lines.push(" " + f);
|
||||
for (const l of K.helpProse || []) lines.push(l);
|
||||
process.stdout.write(lines.join("\n") + "\n");
|
||||
process.exit(0);
|
||||
}
|
||||
const rpc = val("--mode") === "rpc";
|
||||
const print = !rpc && has("-p");
|
||||
const startsAnyway = K.missingExtOk === true || (rpc && K.missingExtOk === "rpc") || (print && K.missingExtOk === "print");
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
if (argv[i] === "-e" && !fs.existsSync(argv[i + 1]) && !startsAnyway) {
|
||||
process.stderr.write("Failed to load extension: it does not exist\n");
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
if (rpc || print) {
|
||||
if (K.flood) {
|
||||
const chunk = Buffer.alloc(1024 * 1024, "x");
|
||||
for (let n = 0; n < 8; n++) for (let off = 0; off < chunk.length;) {
|
||||
try { off += fs.writeSync(1, chunk, off); } catch (e) { if (e.code !== "EAGAIN") throw e; }
|
||||
}
|
||||
}
|
||||
const ALL = ["extensions", "mcp", "skills", "prompts", "context", "approve"];
|
||||
// The id in the header of the last .jsonl in dir whose cwd is the working directory, or null.
|
||||
const storedFor = (dir) => {
|
||||
let id = null;
|
||||
if (dir) for (const n of (() => { try { return fs.readdirSync(dir); } catch { return []; } })()) {
|
||||
if (!n.endsWith(".jsonl")) continue;
|
||||
let h = null;
|
||||
try { h = JSON.parse(fs.readFileSync(path.join(dir, n), "utf8").split("\n")[0]); } catch { h = null; }
|
||||
if (h && h.type === "session" && h.cwd === process.cwd()) id = h.id;
|
||||
}
|
||||
return id;
|
||||
};
|
||||
let ignoreAll = K.ignoreAll;
|
||||
let leak = new Set(K.sealLeak || []);
|
||||
if (K.hideWhen) {
|
||||
const cued = K.hideWhen === "order" ? argv.indexOf("--offline") !== -1 && argv.indexOf("--offline") < argv.indexOf("--no-session") : has(K.hideWhen);
|
||||
if (cued) ignoreAll = true;
|
||||
else leak = new Set(ALL);
|
||||
}
|
||||
if (K.remember) {
|
||||
const marker = path.join(path.dirname(path.dirname(val("-e"))), "seen-before");
|
||||
if (fs.existsSync(marker)) ignoreAll = true;
|
||||
else { fs.writeFileSync(marker, ""); leak = new Set(ALL); }
|
||||
}
|
||||
if ((K.leakWith || K.leakWithout) && (K.leakWith || []).every(has) && (!K.leakWithout || K.leakWithout.some((f) => !has(f)))) leak = new Set(ALL);
|
||||
if (K.leakNoAgentDir && !process.env.PI_CODING_AGENT_DIR) leak = new Set(ALL);
|
||||
if (K.leakNoVar && K.leakNoVar.some((n) => process.env[n] === undefined)) leak = new Set(ALL);
|
||||
if (K.leakNoMatch && has("-c") && !storedFor(val("--session-dir"))) leak = new Set(ALL);
|
||||
if (K.leakEnv) {
|
||||
const want = new Set(K.leakEnv[val("--mode") === "json" ? "json" : "other"]);
|
||||
const got = Object.keys(process.env).filter((n) => !["PWD", "OLDPWD", "SHLVL", "_"].includes(n));
|
||||
if (got.length !== want.size || got.some((n) => !want.has(n))) leak = new Set(ALL);
|
||||
}
|
||||
// Whether a seal flag is on: with standIn, its flag reads as the other flag; with needs, only
|
||||
// when that flag is given too.
|
||||
const sealed = (flag) => has(K.standIn && K.standIn[0] === flag ? K.standIn[1] : flag) && (!K.needs || has(K.needs));
|
||||
const on = (kind, flag) => !ignoreAll && (!sealed(flag) || leak.has(kind));
|
||||
const agent = process.env.PI_CODING_AGENT_DIR || path.join(process.env.HOME || "/", ".pi", "agent");
|
||||
const cwd = process.cwd();
|
||||
const list = (dir, test) => { try { return fs.readdirSync(dir).filter(test).map((n) => path.join(dir, n)); } catch { return []; } };
|
||||
const json = (file) => { try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return null; } };
|
||||
const read = (file) => { try { return fs.readFileSync(file, "utf8"); } catch { return null; } };
|
||||
const settings = [json(path.join(agent, "settings.json")), json(path.join(cwd, ".pi", "settings.json"))].filter(Boolean);
|
||||
const trust = json(path.join(agent, "trust.json")) || {};
|
||||
const saved = settings.some((s) => s.defaultProjectTrust === "always") || trust[cwd] === true;
|
||||
const trusted = !ignoreAll && (has("--approve") || (saved && (!sealed("--no-approve") || leak.has("approve"))));
|
||||
const commands = [];
|
||||
// A planted extension writes one marker on load; an -e extension may also write what it saw at
|
||||
// session_start. Do what each would do.
|
||||
const starts = [];
|
||||
const load = (f) => {
|
||||
const text = read(f);
|
||||
if (text === null) return;
|
||||
for (const m of text.matchAll(/writeFileSync\(("(?:[^"\\]|\\.)*")(, JSON\.stringify\()?/g)) {
|
||||
if (m[2]) starts.push(JSON.parse(m[1]));
|
||||
else fs.writeFileSync(JSON.parse(m[1]), "");
|
||||
}
|
||||
};
|
||||
const given = argv.flatMap((a, i) => a === "-e" ? [argv[i + 1]] : []);
|
||||
if (!(K.dropExplicit && has("--no-extensions"))) for (const f of K.oneExt ? given.slice(0, 1) : given) load(f);
|
||||
const packages = settings.flatMap((s) => (s.packages || []).map((p) => typeof p === "string" ? p : p.source));
|
||||
if (K.trustExtOnly) {
|
||||
if (trusted) for (const f of list(path.join(cwd, ".pi", "extensions"), (n) => n.endsWith(".ts"))) load(f);
|
||||
} else if (on("extensions", "--no-extensions")) {
|
||||
const files = [...list(path.join(agent, "extensions"), (n) => n.endsWith(".ts")), ...list(path.join(cwd, ".pi", "extensions"), (n) => n.endsWith(".ts"))];
|
||||
for (const s of settings) files.push(...(s.extensions || []));
|
||||
for (const p of packages) files.push(...list(path.join(p, "extensions"), (n) => n.endsWith(".ts")));
|
||||
for (const f of files) load(f);
|
||||
}
|
||||
const mcpFlag = (K.extraFlags || []).includes("--no-mcp") ? "--no-mcp" : "--no-extensions";
|
||||
if (!ignoreAll && (!(sealed(mcpFlag) || (K.mcpAlso && has(K.mcpAlso))) || leak.has("mcp"))) {
|
||||
for (const f of [path.join(agent, "mcp.json"), path.join(cwd, ".pi", "mcp.json"), path.join(cwd, ".mcp.json")]) {
|
||||
for (const s of Object.values(json(f)?.mcpServers || {})) cp.spawnSync(s.command, s.args || [], { stdio: "ignore" });
|
||||
}
|
||||
}
|
||||
if (on("skills", "--no-skills")) {
|
||||
const home = process.env.HOME || "/";
|
||||
const dirs = [path.join(agent, "skills"), path.join(cwd, ".pi", "skills"), path.join(cwd, ".agents", "skills"), path.join(home, ".agents", "skills"), ...packages.map((p) => path.join(p, "skills"))];
|
||||
for (const d of dirs) for (const p of list(d, () => true)) commands.push({ name: "skill:" + path.basename(p), source: "skill" });
|
||||
for (const s of settings) for (const p of s.skills || []) commands.push({ name: "skill:" + path.basename(p), source: "skill" });
|
||||
}
|
||||
if (on("prompts", "--no-prompt-templates")) {
|
||||
const dirs = [path.join(agent, "prompts"), path.join(cwd, ".pi", "prompts"), ...packages.map((p) => path.join(p, "prompts"))];
|
||||
for (const d of dirs) for (const p of list(d, (n) => n.endsWith(".md"))) commands.push({ name: path.basename(p, ".md"), source: "prompt" });
|
||||
for (const s of settings) for (const p of s.prompts || []) commands.push({ name: path.basename(p, ".md"), source: "prompt" });
|
||||
}
|
||||
// The system prompt: the --system-prompt file, else SYSTEM.md; then APPEND_SYSTEM.md; then the
|
||||
// context files, from the agent directory and from / down to the working directory.
|
||||
const sys = K.ignoreSystemPrompt ? null : val("--system-prompt");
|
||||
const own = (name) => (trusted && read(path.join(cwd, ".pi", name))) || (!ignoreAll && read(path.join(agent, name))) || "";
|
||||
const hide = K.sysReplacesAll && sys;
|
||||
const parts = [sys ? (read(sys) || "") : own("SYSTEM.md"), K.ignoreAppend || hide ? "" : own("APPEND_SYSTEM.md")];
|
||||
if (K.trustCtxOnly) {
|
||||
if (trusted) parts.push(read(path.join(cwd, "AGENTS.md")) || "");
|
||||
} else if (!hide && on("context", "--no-context-files")) {
|
||||
const dirs = [];
|
||||
for (let d = cwd; ; d = path.dirname(d)) { dirs.unshift(d); if (path.dirname(d) === d) break; }
|
||||
parts.push(read(path.join(agent, "AGENTS.md")) || "");
|
||||
for (const d of dirs) parts.push(read(path.join(d, "AGENTS.md")) || read(path.join(d, "CLAUDE.md")) || "");
|
||||
}
|
||||
// The session -c resumes: the newest .jsonl in --session-dir whose header names this cwd.
|
||||
let session = "stand-in-" + process.pid;
|
||||
const dir = val("--session-dir");
|
||||
if (has("-c") && dir && !K.noResume) session = storedFor(dir) || session;
|
||||
const start = { prompt: parts.join("\n"), commands: K.extNoCommands ? [] : commands.map((c) => c.name), session };
|
||||
if (!(print && K.printNoStart)) for (const f of starts) fs.writeFileSync(f, JSON.stringify(start));
|
||||
if (print) {
|
||||
if (K.sessionOut) process.stdout.write(K.sessionOut);
|
||||
process.exit(0);
|
||||
}
|
||||
let input = "";
|
||||
try { input = fs.readFileSync(0, "utf8"); } catch { input = ""; }
|
||||
for (const line of input.split("\n")) {
|
||||
let m;
|
||||
try { m = JSON.parse(line); } catch { continue; }
|
||||
if (m.type === "get_state") process.stdout.write(JSON.stringify({ id: m.id, type: "response", command: "get_state", success: true, data: { isStreaming: false } }) + "\n");
|
||||
if (m.type === "get_commands") {
|
||||
const answer = K.commands === "fail" ? { success: false, error: "no" } : { success: true, data: K.commands === "list" ? commands : { commands } };
|
||||
process.stdout.write(JSON.stringify({ id: m.id, type: "response", command: "get_commands", ...answer }) + "\n");
|
||||
}
|
||||
}
|
||||
process.exit(0);
|
||||
}
|
||||
if (K.sessionOut) process.stdout.write(K.sessionOut);
|
||||
process.exit(0);
|
||||
`;
|
||||
|
||||
const quote = (text) => `'${String(text).replace(/'/g, "'\\''")}'`;
|
||||
|
||||
// The script text. node: the interpreter the fit part runs on (absolute, so it runs with no PATH).
|
||||
export function piStandIn(knobs = {}, { node = process.execPath } = {}) {
|
||||
const version = knobs.versionHang ? "sleep 60; exit 0" : knobs.versionExit != null ? `exit ${Number(knobs.versionExit)}` : `printf '%s\\n' ${quote(knobs.version ?? "9.9.9")}; exit 0`;
|
||||
const program = `"use strict";\nconst K = ${JSON.stringify(knobs)};\nconst FLAGS = ${JSON.stringify(FIT_FLAGS)};\n${BODY}`;
|
||||
return [
|
||||
"#!/bin/sh",
|
||||
`[ "$1" = --version ] && { ${version}; }`,
|
||||
`if [ -n "\${ARGV_OUT:-}" ]; then printf '%s\\n' "$@" > "$ARGV_OUT"; printf '%s' ${quote(knobs.sessionOut ?? "")}; exit 0; fi`,
|
||||
`exec ${quote(node)} -e ${quote(program)} -- "$@"`,
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
@@ -4,7 +4,8 @@ import assert from 'node:assert/strict';
|
||||
import { mkdirSync, readFileSync, statSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { buildBundle } from '../src/bundle.mjs';
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch } from './helpers.mjs';
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch, harnessReady } from './helpers.mjs';
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
|
||||
const models = {
|
||||
a: { provider: 'proxy', model: 'claude-fixture', route: 'anthropic' },
|
||||
|
||||
@@ -4,7 +4,8 @@ import { readFileSync, mkdirSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { buildBundle } from '../src/bundle.mjs';
|
||||
import { typedTools } from '../src/tools.mjs';
|
||||
import { resolvedFor, scratch, HARNESS, REPO_SKILLS } from './helpers.mjs';
|
||||
import { resolvedFor, scratch, HARNESS, REPO_SKILLS, harnessReady } from './helpers.mjs';
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
import { ownRails, railsText, decisionAutonomy } from '../../organization/src/rails.mjs';
|
||||
|
||||
const SECRET = 'sk-test-NOT-A-SECRET-1234';
|
||||
|
||||
@@ -9,7 +9,8 @@ import { createServer } from 'node:net';
|
||||
import { buildBundle, SOURCES } from '../src/bundle.mjs';
|
||||
import { callTool, typedTools } from '../src/tools.mjs';
|
||||
import * as runner from '../src/runner.mjs';
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from './helpers.mjs';
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from './helpers.mjs';
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
import { Broker } from '../../bus/src/broker.mjs';
|
||||
import { Store } from '../../bus/src/store.mjs';
|
||||
import { createRequestStore } from '../../bus/src/request-store.mjs';
|
||||
|
||||
@@ -10,7 +10,8 @@ import { chmodSync, cpSync, mkdirSync, readFileSync, rmSync, symlinkSync, writeF
|
||||
import { join } from "node:path";
|
||||
import { BundleError, buildBundle, readSkills, SKILL_BYTES } from "../src/bundle.mjs";
|
||||
import { adapterEnv } from "../src/runner.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
|
||||
const sha = (text) => createHash("sha256").update(text).digest("hex");
|
||||
const MARKED = /<skill-file skill="([^"]+)" path="([^"]+)">\n([\s\S]*?)\n<\/skill-file>/g;
|
||||
@@ -183,7 +184,8 @@ test("each adapter passes the skills only in the prompt file, with skill discove
|
||||
const b = buildBundle({ dir: join(dir, "bundle"), resolved, contract: "# Contract\n", harness: HARNESS[harness], models, skillsDir: REPO_SKILLS, session: { ...s, run: "run-1", launchedBy: "pm" } });
|
||||
s.bundle = b.files;
|
||||
s.harnessEnv = b.env;
|
||||
const env = { ...adapterEnv(s, { PATH: `${join(dir, "bin")}:/usr/bin:/bin`, HOME: join(dir, "home") }), ARGV_OUT: join(dir, "argv"), MOSAIC_REQUEST: "x" };
|
||||
// #1873 S2: the pi adapter runs MOSAIC_PI_BIN, which the bundle sets to the pi it found.
|
||||
const env = { ...adapterEnv(s, { PATH: `${join(dir, "bin")}:/usr/bin:/bin`, HOME: join(dir, "home") }), ARGV_OUT: join(dir, "argv"), MOSAIC_REQUEST: "x", ...(harness === "pi" ? { MOSAIC_PI_BIN: cli } : {}) };
|
||||
const r = spawnSync("/bin/sh", [join(REPO, "adapters", harness === "pi" ? "pi" : "claude", "adapter.sh")], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], env });
|
||||
assert.equal(r.status, 0, `${harness}: ${r.stderr}`);
|
||||
const argv = readFileSync(join(dir, "argv"), "utf8").split("\0");
|
||||
|
||||
@@ -9,7 +9,8 @@ import { join } from "node:path";
|
||||
// A namespace import, so the red-before test runs (and fails on its assertion) on a base without
|
||||
// the section's exports.
|
||||
import * as bundleModule from "../src/bundle.mjs";
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
import { REPO_ROLES } from "../../organization/tests/helpers.mjs";
|
||||
|
||||
const { buildBundle, sessionModel } = bundleModule;
|
||||
|
||||
@@ -9,10 +9,65 @@ import { PIN } from './common.mjs';
|
||||
import { localRide, riddenOrgs } from './ride.mjs';
|
||||
import { releaseState, initState, fenceState, initLockStatus, clearInitLock, appendReceipt, recentReceipts, receiptsText, createUnitControl, FILE_LABELS, LOG_LINES, receiptHistory, serviceKeyState } from './admin.mjs';
|
||||
import { secretValues } from './redact.mjs';
|
||||
import { discoverModules, hasModule } from '../../plugins/src/loader.mjs';
|
||||
import { readPluginLock } from '../../plugins/src/pinned.mjs';
|
||||
import { PROVIDERS, publicProviders } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
|
||||
export const proxyCode = (error, fallback) => (/^proxy-[a-z-]+$/.test(error?.code ?? '') ? error.code : fallback);
|
||||
const failure = code => Object.assign(new Error(code), { code });
|
||||
|
||||
// #1873 settings-backend A2: the harness programs installed on this instance, from the plugin tree
|
||||
// through the plugins loader and from the instance's plugin lock, the way the bus lists them
|
||||
// (runtime.mjs harnessLevels). No name list here. A tree that cannot be read answers null; a lock
|
||||
// that cannot be read adds nothing, so a pinned program is then not offered.
|
||||
export function installedHarnesses(dataRoot = null) {
|
||||
let names;
|
||||
try { names = discoverModules('harness').names.filter(name => hasModule('harness', name)); } catch { return null; }
|
||||
if (dataRoot !== null) {
|
||||
try { names = [...new Set([...names, ...Object.keys(readPluginLock(dataRoot).plugins).filter(name => hasModule('harness', name, dataRoot))])]; } catch { /* the tree's list */ }
|
||||
}
|
||||
return names.sort();
|
||||
}
|
||||
|
||||
// #1873 settings-backend A2: every harness an organization document names for an agent: each
|
||||
// `harness` or `fallback.harness` string anywhere in it (role vars, project agent vars, or any
|
||||
// later place), outside its plugins list. Broader than the routes view on purpose: a program is
|
||||
// never turned off where anything in the file still names it.
|
||||
export function harnessesNamed(document) {
|
||||
const named = new Set();
|
||||
const walk = (value, depth) => {
|
||||
if (depth > 32 || value === null || typeof value !== 'object') return;
|
||||
if (Array.isArray(value)) { for (const item of value) walk(item, depth + 1); return; }
|
||||
for (const [key, item] of Object.entries(value)) {
|
||||
if ((key === 'harness' || key === 'fallback.harness') && typeof item === 'string') named.add(item);
|
||||
else walk(item, depth + 1);
|
||||
}
|
||||
};
|
||||
if (document === null || typeof document !== 'object' || Array.isArray(document)) return null;
|
||||
for (const [key, value] of Object.entries(document)) if (key !== 'plugins') walk(value, 1);
|
||||
return named;
|
||||
}
|
||||
|
||||
// #1873 settings-backend A2 (review P1-1): the harnesses the root agents an organization takes name.
|
||||
// `root` is what loadRootAgents answered ({state, agents, assistant, definitions?}). An organization
|
||||
// takes every root agent except the root assistant and the ones that exclude it, the way
|
||||
// materializeCopies copies them at bus boot. Anything not in that shape answers null (unreadable).
|
||||
export function rootHarnessesNamed(root, orgId) {
|
||||
if (root === null || typeof root !== 'object' || root.agents === null || typeof root.agents !== 'object' || Array.isArray(root.agents)) return null;
|
||||
const taken = {};
|
||||
for (const [name, entry] of Object.entries(root.agents)) {
|
||||
if (entry === null || typeof entry !== 'object' || !Array.isArray(entry.excludeOrgs)) return null;
|
||||
if (entry.definition === 'assistant' || entry.excludeOrgs.includes(orgId)) continue;
|
||||
taken[name] = entry.vars ?? {};
|
||||
}
|
||||
return harnessesNamed({ agents: taken, definitions: root.definitions ?? {} });
|
||||
}
|
||||
// A key id is the first 12 hex of the key's sha256. A label is a short plain name; anything that
|
||||
// looks like a Mosaic key prefix is dropped, so an answer can never carry key text.
|
||||
const KEY_ID = /^[a-f0-9]{12}$/;
|
||||
const plainLabel = value => (typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/.test(value) && !/ms[kotp][-_]/i.test(value) ? value : null);
|
||||
const isoTime = value => (typeof value === 'string' && /^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:.]{1,18}Z$/.test(value) ? value : null);
|
||||
|
||||
// `buses()` proves the init-lock condition "bus stopped" ({stopped, running:[label]}).
|
||||
// `orgs()` lists the organizations this proxy serves: [{id, catalogue}]. `unit` and `fetch` are
|
||||
// (and `pin`) seams for tests; the defaults run the real proxy-service.sh, systemctl and pinned fetch.
|
||||
@@ -21,7 +76,11 @@ const failure = code => Object.assign(new Error(code), { code });
|
||||
// change log, `call(verb, args)` reaches its bus as the owner and `status()` is its bus status. `ownerModels()` answers
|
||||
// {proxy, riding, models} from the riding buses' catalogues. `move(id, models, principal)` moves a
|
||||
// stopped fixture organization to the owner's models through the organization service.
|
||||
export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir = null, unit = null, fetch = fetchPinned, buses, orgs = async () => [], riders = async () => [], ownerModels = async () => ({ proxy: false, riding: false, models: [] }), move = null, now = () => new Date(), env = process.env, pin = PIN }) {
|
||||
// #1873 settings-backend A2: a rider may also have `file()`, its organization file as the writer
|
||||
// reads it ({document, revision}), read only. `harnesses()` lists the installed harness programs.
|
||||
// `rootAgents()` reads the root agent file (loadRootAgents); with none given, or one that throws,
|
||||
// no organization that has the program on is written (unreadable).
|
||||
export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir = null, unit = null, fetch = fetchPinned, buses, orgs = async () => [], riders = async () => [], harnesses = () => installedHarnesses(dataRoot), rootAgents = null, ownerModels = async () => ({ proxy: false, riding: false, models: [] }), move = null, now = () => new Date(), env = process.env, pin = PIN, providers = PROVIDERS }) {
|
||||
const port = () => { try { return initState(configDir, dataRoot).settings?.port ?? null; } catch { return null; } };
|
||||
const control = unit ?? createUnitControl({ instance, unitDir, port, env });
|
||||
let job = { state: 'idle', step: null, receipt: null, error: null };
|
||||
@@ -98,6 +157,83 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
return (await riders()).map(r => { const state = decide(r, wasRidden); return state === 'fixture' && typeof r.status !== 'function' ? { id: r.id, state, running: null } : { id: r.id, state }; });
|
||||
} catch { return null; }
|
||||
}
|
||||
// #1873 settings-backend A2, item 5: which harness programs each organization has on, from its
|
||||
// bus's plugins read as the owner. null when the read fails, its answer has no harness list, or
|
||||
// any entry is not a plugin name (review P3-3: no bus text reaches the page).
|
||||
const harnessList = value => (Array.isArray(value?.plugins?.harness) && value.plugins.harness.every(n => typeof n === 'string' && /^[a-z][a-z0-9-]{0,63}$/.test(n)) ? [...value.plugins.harness] : null);
|
||||
async function programsSummary() {
|
||||
let installed = null;
|
||||
try { installed = harnesses(); if (!Array.isArray(installed)) installed = null; } catch { installed = null; }
|
||||
let list;
|
||||
try { list = await riders(); } catch { return { installed, organizations: null }; }
|
||||
const organizations = await Promise.all(list.map(async rider => {
|
||||
try { return { id: rider.id, on: harnessList(await rider.call('plugins')) }; } catch { return { id: rider.id, on: null }; }
|
||||
}));
|
||||
return { installed, organizations };
|
||||
}
|
||||
// Item 5, one organization: turn `name` off unless something still uses it or it is the only one.
|
||||
// Three reads (the plugins read, the routes view and the organization file) must show one revision;
|
||||
// the write carries it, so a change after them is refused by the writer (revision-conflict) and
|
||||
// reported, never retried. Every skip writes nothing. `root` is the root agent file read once per
|
||||
// call (undefined when it could not be read): the root agents this organization takes count too.
|
||||
async function offOne(rider, name, root) {
|
||||
const id = rider.id;
|
||||
let current;
|
||||
try { current = await rider.call('plugins'); } catch { return { id, outcome: 'bus-down' }; }
|
||||
const on = harnessList(current);
|
||||
if (on === null || typeof current?.revision !== 'string' || !/^[a-f0-9]{64}$/.test(current.revision)) return { id, outcome: 'unreadable' };
|
||||
if (!on.includes(name)) return { id, outcome: 'already-off' };
|
||||
let routes = null;
|
||||
try { routes = await rider.call('routes'); } catch (error) { if (error?.code !== 'launch-not-configured') return { id, outcome: 'unreadable' }; }
|
||||
let file;
|
||||
try { file = typeof rider.file === 'function' ? await rider.file() : null; } catch { file = null; }
|
||||
const named = harnessesNamed(file?.document);
|
||||
const fromRoot = root === undefined ? null : rootHarnessesNamed(root, id);
|
||||
if (named === null || fromRoot === null || typeof file?.revision !== 'string') return { id, outcome: 'unreadable' };
|
||||
for (const harness of fromRoot) named.add(harness);
|
||||
if (file.revision !== current.revision || (routes !== null && routes?.revision !== current.revision)) return { id, outcome: 'failed:revision-conflict' };
|
||||
// The routes view must be readable when there is one; each instance's route and fallback count.
|
||||
if (routes !== null) {
|
||||
if (!routes || typeof routes.instances !== 'object' || routes.instances === null) return { id, outcome: 'unreadable' };
|
||||
for (const route of Object.values(routes.instances)) {
|
||||
if (typeof route?.harness === 'string') named.add(route.harness);
|
||||
if (typeof route?.fallback?.harness === 'string') named.add(route.fallback.harness);
|
||||
}
|
||||
}
|
||||
// The harness rule: any route on this program, whatever account its model uses, keeps it on.
|
||||
if (named.has(name)) return { id, outcome: 'in-use' };
|
||||
if (on.length === 1) return { id, outcome: 'last-program' };
|
||||
try { await rider.call('plugins.set', { revision: current.revision, plugins: { harness: on.filter(n => n !== name) } }); }
|
||||
catch (error) { return { id, outcome: `failed:${failCode(error)}` }; }
|
||||
return { id, outcome: 'off' };
|
||||
}
|
||||
// Item 8, one organization: its proxy client keys by key id, from proxy.keys as the owner. Never
|
||||
// the usage queue. A key value never reaches here: proxy.keys answers ids, and every other field
|
||||
// is checked against a plain pattern before it is passed on.
|
||||
async function accessOne(rider) {
|
||||
const id = rider.id;
|
||||
if (typeof rider.fixture !== 'boolean' || typeof rider.cliproxy !== 'boolean') return { id, health: 'unreadable', reason: 'unreadable', keys: [] };
|
||||
if (rider.fixture || !rider.cliproxy) return { id, health: 'not-riding', keys: [] };
|
||||
let answer;
|
||||
try { answer = await rider.call('proxy.keys'); }
|
||||
catch (error) {
|
||||
const code = error?.code;
|
||||
if (code === 'proxy-uncertain') return { id, health: 'attention', reason: 'proxy-uncertain', keys: [] };
|
||||
const reason = code === 'not-configured' ? 'no-proxy-client' : code === 'outcome-unknown' || code === 'no-bus-host' ? 'bus-down' : 'unreadable';
|
||||
return { id, health: 'unreadable', reason, keys: [] };
|
||||
}
|
||||
if (!Array.isArray(answer?.keys)) return { id, health: 'unreadable', reason: 'unreadable', keys: [] };
|
||||
const keys = [];
|
||||
let other = 0, sentinel = 0;
|
||||
for (const key of answer.keys) {
|
||||
if (!key || typeof key !== 'object' || !KEY_ID.test(key.keyId ?? '')) return { id, health: 'unreadable', reason: 'unreadable', keys: [] };
|
||||
if (key.management === 'sentinel') sentinel++;
|
||||
else if (key.management === 'stack-managed') keys.push({ keyId: key.keyId, agent: plainLabel(key.agent), state: plainLabel(key.state), issuedAt: isoTime(key.issuedAt) });
|
||||
else other++;
|
||||
}
|
||||
const health = keys.length === 0 ? 'missing' : keys.every(k => k.state === 'active') ? 'ok' : 'attention';
|
||||
return { id, health, keys, other, sentinel };
|
||||
}
|
||||
function nextSteps({ release, init, unitState, fence, lock }) {
|
||||
if (!release.fetched || release.problem) return ['fetch'];
|
||||
if (lock.present) return lock.clearable ? ['init-lock-clear'] : ['init-lock-wait'];
|
||||
@@ -123,6 +259,7 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
let organizations = null;
|
||||
try { organizations = await orgs(); } catch { organizations = null; }
|
||||
const ride = await rideSummary();
|
||||
const programs = await programsSummary();
|
||||
const next = nextSteps({ release, init, unitState, fence, lock });
|
||||
// Ride all comes after the proxy runs with no fence, while an organization can still ride.
|
||||
if (next[0] === 'accounts' && ride?.some(r => r.state === 'ready' || r.state === 'fixture')) next.push('ride-all');
|
||||
@@ -137,6 +274,8 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
initLock: lock,
|
||||
fetch: fetchJob,
|
||||
restart: restarted ? { confirmed: true, observedAt: restarted.observedAt, pid: restarted.after.pid } : null,
|
||||
// #1873 settings-backend-a3: the page's provider select, from the plugin. Id, label and sign-in kind only.
|
||||
providers: publicProviders(providers),
|
||||
busy,
|
||||
receipts: recentReceipts(dataRoot),
|
||||
organizations,
|
||||
@@ -144,6 +283,7 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
// needs a config format that does not exist yet.
|
||||
ownProxy: { available: false, state: 'not built', reason: 'This instance has one shared proxy. A separate proxy per organization is not built yet.' },
|
||||
ride,
|
||||
programs,
|
||||
next,
|
||||
};
|
||||
},
|
||||
@@ -287,6 +427,33 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
if (typeof user === 'string' && user) record.user = user.slice(0, 64);
|
||||
try { receipt(record); return true; } catch { return false; }
|
||||
},
|
||||
// #1873 settings-backend A2, item 5: turns one harness program off for every current
|
||||
// organization that can do without it, through each organization's bus as the owner (the way
|
||||
// Ride all writes). Sequential, one organization at a time; one receipt names the outcome ids.
|
||||
async programsOff({ name, user = null } = {}) {
|
||||
return exclusive('programs', async () => {
|
||||
let installed;
|
||||
try { installed = harnesses(); } catch { installed = null; }
|
||||
if (typeof name !== 'string' || !Array.isArray(installed) || !installed.includes(name)) throw failure('invalid-request');
|
||||
const list = await riders();
|
||||
let root;
|
||||
try { root = typeof rootAgents === 'function' ? await rootAgents() : undefined; } catch { root = undefined; }
|
||||
const results = [];
|
||||
for (const rider of list) results.push(await offOne(rider, name, root));
|
||||
const record = { op: 'programs.off', name, results: results.map(r => ({ id: r.id, outcome: r.outcome })) };
|
||||
if (typeof user === 'string' && user) record.user = user.slice(0, 64);
|
||||
// Each write is the organization's own change log entry, so a receipt that cannot be
|
||||
// written still answers what happened, with no receipt.
|
||||
let written = null;
|
||||
try { written = receipt(record); } catch { written = null; }
|
||||
return { name, results, receipt: written };
|
||||
});
|
||||
},
|
||||
// #1873 settings-backend A2, item 8: each organization's proxy client keys and their health.
|
||||
async access() {
|
||||
const list = await riders();
|
||||
return { organizations: await Promise.all(list.map(rider => accessOne(rider).catch(() => ({ id: rider.id, health: 'unreadable', reason: 'unreadable', keys: [] })))) };
|
||||
},
|
||||
async initLockClear() {
|
||||
return exclusive('init-lock-clear', async () => ({ receipt: clearInitLock({ configDir, dataRoot, buses: await buses(), now }) }));
|
||||
},
|
||||
|
||||
@@ -3,17 +3,19 @@ import http from 'node:http';
|
||||
import { ProxyError, refuse, sha256, regularBytes, validManagementKey } from './common.mjs';
|
||||
import { lstatSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { PROVIDERS } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
const KEY_PATH = '/v8/management/config/access/api-keys';
|
||||
const validKeys = value => Array.isArray(value) && value.length <= 100000 && value.every(k => typeof k === 'string' && k.length > 0 && k.length <= 4096 && !/[\r\n\x00]/.test(k));
|
||||
export class ManagementClient {
|
||||
#base; #key; #receipt; #timeout;
|
||||
constructor({ baseUrl, key, receipt = () => {}, timeout = 5000 }) {
|
||||
#base; #key; #receipt; #timeout; #providers;
|
||||
// providers: the plugin's PROVIDERS; a test passes a copy with one taken out.
|
||||
constructor({ baseUrl, key, receipt = () => {}, timeout = 5000, providers = PROVIDERS }) {
|
||||
let u; try { u = new URL(baseUrl); } catch { refuse('proxy-client-invalid'); }
|
||||
if (u.protocol !== 'http:' || u.hostname !== '127.0.0.1' || !u.port || Number(u.port) < 1024 || u.port === '8317'
|
||||
|| u.username || u.password || u.pathname !== '/' || u.search || u.hash
|
||||
|| !validManagementKey(key)
|
||||
|| typeof receipt !== 'function' || !Number.isSafeInteger(timeout) || timeout <= 0) refuse('proxy-client-invalid');
|
||||
this.#base = u.origin; this.#key = key; this.#receipt = receipt; this.#timeout = timeout;
|
||||
|| typeof receipt !== 'function' || !Number.isSafeInteger(timeout) || timeout <= 0 || !Array.isArray(providers)) refuse('proxy-client-invalid');
|
||||
this.#base = u.origin; this.#key = key; this.#receipt = receipt; this.#timeout = timeout; this.#providers = providers;
|
||||
}
|
||||
#request(method, endpoint, payload, onStatus) {
|
||||
// node:http addresses the fixed numeric loopback directly. An ambient fetch
|
||||
@@ -107,7 +109,7 @@ export class ManagementClient {
|
||||
}
|
||||
patchCredential(name, priority) { return this.#page('PATCH', '/v8/management/credentials/fields', { name, priority }, 200); }
|
||||
oauthStart(provider) {
|
||||
if (!['claude', 'codex'].includes(provider)) refuse('proxy-client-invalid');
|
||||
if (!this.#providers.some(p => p.id === provider && p.signIn === 'oauth')) refuse('proxy-client-invalid');
|
||||
return this.#page('GET', `/v8/management/oauth/auth-url?provider=${provider}`);
|
||||
}
|
||||
oauthCallback(redirect_url) { return this.#page('POST', '/v8/management/oauth/callback', { redirect_url }); }
|
||||
|
||||
@@ -7,7 +7,10 @@ import fs from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { createProxyAdmin } from '../src/admin-control.mjs';
|
||||
import { createProxyAdmin, installedHarnesses, harnessesNamed, rootHarnessesNamed } from '../src/admin-control.mjs';
|
||||
import { pluginHash } from '../../plugins/src/pinned.mjs';
|
||||
import { installPlugin } from '../../plugins/src/install.mjs';
|
||||
import { KINDS } from '../../plugins/src/kinds.mjs';
|
||||
import { initFiles, receiptFile } from '../src/admin.mjs';
|
||||
import { PIN } from '../src/common.mjs';
|
||||
|
||||
@@ -15,7 +18,7 @@ const code = name => error => error.code === name;
|
||||
const SENTINEL = 'pa-sentinel-management-key-0123456789';
|
||||
const stopped = async () => ({ stopped: true, running: [] });
|
||||
|
||||
function fixture(t, { unitState = { installed: false, active: 'inactive', healthy: false }, restart = null, fetch = null, pin = PIN, statusHook = null } = {}) {
|
||||
function fixture(t, { unitState = { installed: false, active: 'inactive', healthy: false }, restart = null, fetch = null, pin = PIN, statusHook = null, extra = {} } = {}) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'pc-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
@@ -30,7 +33,7 @@ function fixture(t, { unitState = { installed: false, active: 'inactive', health
|
||||
stop: async () => { ops.push('stop'); return { ...unitState, active: 'inactive', pid: null }; },
|
||||
restart: async () => { ops.push('restart'); return restart ?? { state: unitState, evidence: { unit: 'mosaic-proxy-zz.service', confirmed: true, before: { pid: 10, since: 1, active: 'active' }, after: { pid: 11, since: 2, active: 'active', listening: true }, observedAt: new Date(0).toISOString() } }; },
|
||||
};
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit, buses: stopped, fetch: fetch ?? (async () => ({})), now: () => new Date(0), orgs: async () => [{ id: 'acme', catalogue: 'ready' }], pin });
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit, buses: stopped, fetch: fetch ?? (async () => ({})), now: () => new Date(0), orgs: async () => [{ id: 'acme', catalogue: 'ready' }], pin, ...extra });
|
||||
return { root, configDir, dataRoot, admin, ops, files: initFiles(configDir, dataRoot) };
|
||||
}
|
||||
const receipts = dataRoot => { try { return fs.readFileSync(receiptFile(dataRoot), 'utf8').trim().split('\n').filter(Boolean).map(JSON.parse); } catch { return []; } };
|
||||
@@ -307,3 +310,260 @@ test('recordAccount answers false, and throws nothing, when the history cannot b
|
||||
fs.mkdirSync(receiptFile(dataRoot));
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.apikey.add', args: { provider: 'openai', key: 'k' } }), false);
|
||||
});
|
||||
|
||||
// #1873 settings-backend A2 (items 5 and 8): fake organizations behind the riders. The harness names
|
||||
// are made up; every answer carries a planted key, which no result, receipt or history entry may hold.
|
||||
const REV = 'a'.repeat(64), REV2 = 'b'.repeat(64);
|
||||
const PLANT = `mso-zz-acme-key-${'Q'.repeat(43)}`;
|
||||
const KEY_TEXT = /ms[kotp][-_][A-Za-z0-9_-]{6,}/;
|
||||
const busError = name => Object.assign(new Error(`refused ${PLANT}`), { code: name, reason: PLANT });
|
||||
function fakeOrg(id, { on = ['alpha', 'beta'], revision = REV, fileRevision = null, instances = {}, routesError = null, routesRevision = null, document = null, fileError = false, down = false, setError = null, keys = [], keysError = null, fixture = false, cliproxy = true, plugins = undefined } = {}) {
|
||||
const calls = [];
|
||||
const rider = {
|
||||
id, fixture, cliproxy, changes: '',
|
||||
file: () => { if (fileError) throw new Error(PLANT); return { document: document ?? { roles: {}, plugins: { harness: on }, secretNote: PLANT }, revision: fileRevision ?? revision }; },
|
||||
call: async (verb, args) => {
|
||||
calls.push(args === undefined ? [verb] : [verb, args]);
|
||||
if (down) throw busError('outcome-unknown');
|
||||
if (verb === 'plugins') return { business: id, revision, plugins: plugins !== undefined ? plugins : { harness: on, 'model-provider': ['cliproxy'] }, available: { harness: on }, note: PLANT };
|
||||
if (verb === 'routes') { if (routesError) throw busError(routesError); return { business: id, revision: routesRevision ?? revision, harnesses: on, models: ['m1'], providerModels: {}, levels: {}, instances, note: PLANT }; }
|
||||
if (verb === 'plugins.set') { if (setError) throw busError(setError); return { business: id, revision: REV2, plugins: args.plugins }; }
|
||||
if (verb === 'proxy.keys') { if (keysError) throw busError(keysError); return { keys }; }
|
||||
throw busError('invalid-request');
|
||||
},
|
||||
};
|
||||
return { rider, calls };
|
||||
}
|
||||
const NO_ROOT = { state: 'absent', agents: {}, assistant: null };
|
||||
const programsFixture = (t, orgs, extra = {}) => fixture(t, { extra: { riders: async () => orgs.map(o => o.rider), harnesses: () => ['alpha', 'beta', 'gamma'], rootAgents: () => NO_ROOT, ...extra } });
|
||||
const rootAgent = (vars, { definition = 'coder', excludeOrgs = [] } = {}) => ({ definition, title: 'Coder', vars, excludeOrgs });
|
||||
const route = harness => ({ harness, model: 'm1', thinking: null, fallback: null });
|
||||
|
||||
test('A2 item 5: the installed list comes from the harness plugin tree, and status lists it with no organization', async t => {
|
||||
const tree = new URL('../../plugins/harness/', import.meta.url);
|
||||
const fromTree = fs.readdirSync(tree).filter(name => { try { return fs.lstatSync(new URL(`${name}/index.mjs`, tree)).isFile(); } catch { return false; } }).sort();
|
||||
assert.ok(fromTree.length >= 1);
|
||||
assert.deepEqual(installedHarnesses(), fromTree);
|
||||
const { admin } = fixture(t);
|
||||
assert.deepEqual((await admin.status()).programs, { installed: fromTree, organizations: [] });
|
||||
});
|
||||
|
||||
test('A2 item 5: status names the programs each organization has on; one bus down is null and leaves the others', async t => {
|
||||
const acme = fakeOrg('acme', { on: ['alpha'] }), bolt = fakeOrg('bolt', { down: true }), crux = fakeOrg('crux', { plugins: { harness: 'alpha' } });
|
||||
const { admin } = programsFixture(t, [acme, bolt, crux]);
|
||||
const status = await admin.status();
|
||||
assert.deepEqual(status.programs, { installed: ['alpha', 'beta', 'gamma'], organizations: [{ id: 'acme', on: ['alpha'] }, { id: 'bolt', on: null }, { id: 'crux', on: null }] });
|
||||
assert.doesNotMatch(JSON.stringify(status), KEY_TEXT);
|
||||
});
|
||||
|
||||
test('A2 item 5: off skips a program any route still uses, whatever its model, and writes nothing there', async t => {
|
||||
// A role route, a fallback route, a project agent only the file shows, and a route whose model has
|
||||
// no known account kind: each keeps alpha on (the harness rule).
|
||||
const role = fakeOrg('role', { instances: { chief: route('alpha') } });
|
||||
const fallback = fakeOrg('fallback', { instances: { chief: { ...route('beta'), fallback: { harness: 'alpha', model: 'm1', thinking: null } } } });
|
||||
const project = fakeOrg('project', { routesError: 'launch-not-configured', document: { roles: {}, plugins: { harness: ['alpha', 'beta'] }, projects: { site: { agents: { dev: { definition: 'coder', vars: { harness: 'alpha' } } } } } } });
|
||||
const unknownKind = fakeOrg('unknown-kind', { instances: { chief: { harness: 'alpha', model: 'mystery-account-model', thinking: null, fallback: null } } });
|
||||
const { admin } = programsFixture(t, [role, fallback, project, unknownKind]);
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results.map(r => [r.id, r.outcome]), [['role', 'in-use'], ['fallback', 'in-use'], ['project', 'in-use'], ['unknown-kind', 'in-use']]);
|
||||
for (const o of [role, fallback, project, unknownKind]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
test('A2 item 5: off turns the program off where nothing uses it, skips the last program, already off and unreadable, and reports bus down', async t => {
|
||||
const free = fakeOrg('free', { instances: { chief: route('beta') } });
|
||||
const last = fakeOrg('last', { on: ['alpha'] });
|
||||
const already = fakeOrg('already', { on: ['beta'] });
|
||||
const down = fakeOrg('down', { down: true });
|
||||
const unreadable = fakeOrg('unreadable', { fileError: true });
|
||||
const noFile = fakeOrg('no-file'); delete noFile.rider.file;
|
||||
const badRoutes = fakeOrg('bad-routes', { routesError: 'organization-invalid' });
|
||||
const { admin } = programsFixture(t, [free, last, already, down, unreadable, noFile, badRoutes]);
|
||||
const out = await admin.programsOff({ name: 'alpha', user: 'owner' });
|
||||
assert.equal(out.name, 'alpha');
|
||||
assert.deepEqual(out.results, [
|
||||
{ id: 'free', outcome: 'off' }, { id: 'last', outcome: 'last-program' }, { id: 'already', outcome: 'already-off' },
|
||||
{ id: 'down', outcome: 'bus-down' }, { id: 'unreadable', outcome: 'unreadable' }, { id: 'no-file', outcome: 'unreadable' }, { id: 'bad-routes', outcome: 'unreadable' },
|
||||
]);
|
||||
assert.deepEqual(free.calls.find(([verb]) => verb === 'plugins.set'), ['plugins.set', { revision: REV, plugins: { harness: ['beta'] } }]);
|
||||
for (const o of [last, already, down, unreadable, noFile, badRoutes]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
test('A2 item 5: a plugins.set refusal is reported per organization, and a revision change is reported once, never retried', async t => {
|
||||
const refused = fakeOrg('refused', { setError: 'unknown-plugin' });
|
||||
const raced = fakeOrg('raced', { setError: 'revision-conflict' });
|
||||
const moved = fakeOrg('moved', { fileRevision: REV2 });
|
||||
const ok = fakeOrg('ok');
|
||||
const { admin } = programsFixture(t, [refused, raced, moved, ok]);
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results.map(r => r.outcome), ['failed:unknown-plugin', 'failed:revision-conflict', 'failed:revision-conflict', 'off']);
|
||||
assert.equal(raced.calls.filter(([verb]) => verb === 'plugins.set').length, 1);
|
||||
assert.equal(moved.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
test('A2 item 5: off writes one receipt by name and outcome ids, and no answer, receipt or history entry holds a key', async t => {
|
||||
const a = fakeOrg('acme'), b = fakeOrg('bolt', { setError: 'invalid-plugins' });
|
||||
const { admin, dataRoot } = programsFixture(t, [a, b]);
|
||||
const out = await admin.programsOff({ name: 'beta', user: 'owner' });
|
||||
const record = { time: new Date(0).toISOString(), op: 'programs.off', name: 'beta', results: [{ id: 'acme', outcome: 'off' }, { id: 'bolt', outcome: 'failed:invalid-plugins' }], user: 'owner' };
|
||||
assert.deepEqual(out.receipt, record);
|
||||
assert.deepEqual(receipts(dataRoot), [record]);
|
||||
const history = admin.history({});
|
||||
assert.deepEqual(history.entries, [record]);
|
||||
for (const value of [out, receipts(dataRoot), history]) assert.doesNotMatch(JSON.stringify(value), KEY_TEXT);
|
||||
});
|
||||
|
||||
test('A2 item 5: an unknown name is invalid-request and calls no bus; two off calls at once give proxy-admin-busy for one', async t => {
|
||||
let release;
|
||||
const gate = new Promise(r => { release = r; });
|
||||
const slow = fakeOrg('slow');
|
||||
const original = slow.rider.call;
|
||||
// Blocks only from the busy part on, so a name check that lets a bad name through goes red, not hangs.
|
||||
let blocking = false;
|
||||
slow.rider.call = async (verb, args) => { if (verb === 'plugins' && blocking) await gate; return original(verb, args); };
|
||||
const { admin, dataRoot } = programsFixture(t, [slow]);
|
||||
await assert.rejects(admin.programsOff({ name: 'delta' }), code('invalid-request'));
|
||||
await assert.rejects(admin.programsOff({ name: 42 }), code('invalid-request'));
|
||||
assert.deepEqual(slow.calls, []);
|
||||
assert.deepEqual(receipts(dataRoot), []);
|
||||
blocking = true;
|
||||
const first = admin.programsOff({ name: 'alpha' });
|
||||
await new Promise(r => setImmediate(r));
|
||||
// Bounded: without the lock the second call would wait on the same gate, so it answers 'waited'.
|
||||
const second = admin.programsOff({ name: 'beta' }).then(() => 'answered', e => e?.code);
|
||||
assert.equal(await Promise.race([second, new Promise(r => setTimeout(() => r('waited'), 1000))]), 'proxy-admin-busy');
|
||||
release();
|
||||
await second;
|
||||
assert.deepEqual((await first).results, [{ id: 'slow', outcome: 'off' }]);
|
||||
assert.equal(receipts(dataRoot).length, 1);
|
||||
});
|
||||
|
||||
test('A2 item 5: an installed list that cannot be read refuses off as invalid-request', async t => {
|
||||
const a = fakeOrg('acme');
|
||||
const { admin } = programsFixture(t, [a], { harnesses: () => { throw new Error('unreadable tree'); } });
|
||||
await assert.rejects(admin.programsOff({ name: 'alpha' }), code('invalid-request'));
|
||||
assert.deepEqual(a.calls, []);
|
||||
assert.equal((await admin.status()).programs.installed, null);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P1-1): a root agent the organization takes keeps its program on; an excluded organization and the root assistant do not count', async t => {
|
||||
// Only the root agent file names alpha: nothing in either organization's file or routes does.
|
||||
const root = { state: 'ok', assistant: 'helper', agents: {
|
||||
writer: rootAgent({ harness: 'gamma', 'fallback.harness': 'alpha' }, { excludeOrgs: ['spare'] }),
|
||||
helper: rootAgent({ harness: 'beta' }, { definition: 'assistant' }),
|
||||
} };
|
||||
const takes = fakeOrg('takes', { on: ['alpha', 'beta', 'gamma'] });
|
||||
const spare = fakeOrg('spare', { on: ['alpha', 'beta', 'gamma'] });
|
||||
const { admin } = programsFixture(t, [takes, spare], { rootAgents: () => root });
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results, [{ id: 'takes', outcome: 'in-use' }, { id: 'spare', outcome: 'off' }]);
|
||||
assert.equal(takes.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
// The root assistant is not copied into an organization, so its harness does not keep beta on.
|
||||
const again = await admin.programsOff({ name: 'beta' });
|
||||
assert.deepEqual(again.results.map(r => r.outcome), ['off', 'off']);
|
||||
// The root file is read once per call, not once per organization.
|
||||
let reads = 0;
|
||||
const counted = programsFixture(t, [fakeOrg('a'), fakeOrg('b')], { rootAgents: () => { reads++; return NO_ROOT; } });
|
||||
await counted.admin.programsOff({ name: 'alpha' });
|
||||
assert.equal(reads, 1);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P1-1): a root agent file that cannot be read, or no reader, writes nothing where the program is on', async t => {
|
||||
const on = fakeOrg('on'), already = fakeOrg('already', { on: ['beta'] });
|
||||
const { admin } = programsFixture(t, [on, already], { rootAgents: () => { throw Object.assign(new Error(`root agent file refused ${PLANT}`), { name: 'BusinessError' }); } });
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results, [{ id: 'on', outcome: 'unreadable' }, { id: 'already', outcome: 'already-off' }]);
|
||||
assert.doesNotMatch(JSON.stringify(out), KEY_TEXT);
|
||||
const bare = fakeOrg('bare');
|
||||
const none = fixture(t, { extra: { riders: async () => [bare.rider], harnesses: () => ['alpha', 'beta'] } });
|
||||
assert.deepEqual((await none.admin.programsOff({ name: 'alpha' })).results, [{ id: 'bare', outcome: 'unreadable' }]);
|
||||
for (const o of [on, already, bare]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
// A root answer of the wrong shape is unreadable too.
|
||||
assert.equal(rootHarnessesNamed({ agents: { w: { vars: { harness: 'alpha' } } } }, 'acme'), null);
|
||||
assert.equal(rootHarnessesNamed(null, 'acme'), null);
|
||||
assert.deepEqual([...rootHarnessesNamed(NO_ROOT, 'acme')], []);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P3-1): a revision that is not 64 hex, routes from another revision, and routes with no instances write nothing; later organizations still go', async t => {
|
||||
const badRev = fakeOrg('bad-rev', { revision: 'not-a-revision' });
|
||||
const routesMoved = fakeOrg('routes-moved', { routesRevision: REV2 });
|
||||
const noInstances = fakeOrg('no-instances', { instances: null });
|
||||
const after = fakeOrg('after');
|
||||
const { admin, dataRoot } = programsFixture(t, [badRev, routesMoved, noInstances, after]);
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results, [{ id: 'bad-rev', outcome: 'unreadable' }, { id: 'routes-moved', outcome: 'failed:revision-conflict' }, { id: 'no-instances', outcome: 'unreadable' }, { id: 'after', outcome: 'off' }]);
|
||||
for (const o of [badRev, routesMoved, noInstances]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
assert.equal(receipts(dataRoot).length, 1);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P3-1): a harness program the instance plugin lock pins is listed as installed', t => {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'pl-'));
|
||||
// The install leaves read-only directories; open them up before removing the tree.
|
||||
const thaw = p => { const st = fs.lstatSync(p); if (st.isSymbolicLink()) return; if (st.isDirectory()) { fs.chmodSync(p, 0o700); for (const n of fs.readdirSync(p)) thaw(join(p, n)); } };
|
||||
t.after(() => { thaw(root); fs.rmSync(root, { recursive: true, force: true }); });
|
||||
const src = join(root, 'src'), dataRoot = join(root, 'data');
|
||||
fs.mkdirSync(join(src, 'harness'), { recursive: true }); fs.mkdirSync(dataRoot);
|
||||
fs.writeFileSync(join(src, 'harness', 'index.mjs'), KINDS.harness.exports.map(name => `export function ${name}(){return ${name === 'levels' ? "['high']" : name === 'countsAccount' ? 'true' : 'null'};}`).join('\n') + '\nexport const interfaceVersion=1;');
|
||||
const manifest = { name: 'zetaplug', version: '1.0.0', kinds: ['harness'], interfaceVersions: { harness: 1 }, sha256: '0'.repeat(64), source: { path: 'fixture', version: '1.0.0' }, signature: null, tools: [], actions: [], services: [], roleTemplates: [] };
|
||||
fs.writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
manifest.sha256 = pluginHash(src);
|
||||
fs.writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
assert.equal(installedHarnesses(dataRoot).includes('zetaplug'), false);
|
||||
installPlugin({ dataRoot, source: src, expectedHash: manifest.sha256 });
|
||||
const listed = installedHarnesses(dataRoot);
|
||||
assert.ok(listed.includes('zetaplug'));
|
||||
assert.deepEqual(listed.filter(n => n !== 'zetaplug'), installedHarnesses());
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P3-3): an organization whose harness list holds anything but plugin names reads on: null', async t => {
|
||||
const odd = fakeOrg('odd', { plugins: { harness: ['alpha', `bad ${PLANT}`] } });
|
||||
const fine = fakeOrg('fine', { on: ['alpha'] });
|
||||
const { admin } = programsFixture(t, [odd, fine]);
|
||||
const status = await admin.status();
|
||||
assert.deepEqual(status.programs.organizations, [{ id: 'odd', on: null }, { id: 'fine', on: ['alpha'] }]);
|
||||
assert.doesNotMatch(JSON.stringify(status), KEY_TEXT);
|
||||
});
|
||||
|
||||
test('A2: harnessesNamed reads every harness and fallback.harness string outside the plugins list', () => {
|
||||
assert.deepEqual([...harnessesNamed({ plugins: { harness: ['alpha'] }, roles: { a: { vars: { harness: 'beta', 'fallback.harness': 'gamma' } } }, projects: { p: { agents: { x: { vars: { harness: 'delta' } } } } } })].sort(), ['beta', 'delta', 'gamma']);
|
||||
assert.equal(harnessesNamed(null), null);
|
||||
assert.equal(harnessesNamed([]), null);
|
||||
});
|
||||
|
||||
const key = (keyId, management, state, agent = 'dev') => ({ keyId, management, agent, issuedAt: '2026-11-01T10:00:00.000Z', state });
|
||||
test('A2 item 8: access gives each organization a health and key ids, never a key, and never reads the usage queue', async t => {
|
||||
const ok = fakeOrg('ok', { keys: [key('0123456789ab', 'stack-managed', 'active'), key('ba9876543210', 'not stack-managed', null, null), key('aaaaaaaaaaaa', 'sentinel', null, null)] });
|
||||
const attention = fakeOrg('attention', { keys: [key('0123456789ab', 'stack-managed', 'active'), key('bbbbbbbbbbbb', 'stack-managed', 'revoked')] });
|
||||
const missing = fakeOrg('missing', { keys: [key('cccccccccccc', 'not stack-managed', null, null)] });
|
||||
const fixtureOrg = fakeOrg('fixture', { fixture: true });
|
||||
const ownModels = fakeOrg('own', { cliproxy: false });
|
||||
const down = fakeOrg('down', { down: true });
|
||||
const noClient = fakeOrg('no-client', { keysError: 'not-configured' });
|
||||
const uncertain = fakeOrg('uncertain', { keysError: 'proxy-uncertain' });
|
||||
const proxyDown = fakeOrg('proxy-down', { keysError: 'proxy-down' });
|
||||
const badShape = fakeOrg('bad-shape', { keys: 'none' });
|
||||
const planted = fakeOrg('planted', { keys: [key('dddddddddddd', 'stack-managed', PLANT, PLANT), { ...key('eeeeeeeeeeee', 'stack-managed', 'active'), value: PLANT, issuedAt: PLANT }] });
|
||||
const plantedId = fakeOrg('planted-id', { keys: [key(PLANT, 'stack-managed', 'active')] });
|
||||
const unread = fakeOrg('unread'); unread.rider.fixture = undefined;
|
||||
const all = [ok, attention, missing, fixtureOrg, ownModels, down, noClient, uncertain, proxyDown, badShape, planted, plantedId, unread];
|
||||
const { admin } = programsFixture(t, all);
|
||||
const out = await admin.access();
|
||||
assert.deepEqual(out.organizations, [
|
||||
{ id: 'ok', health: 'ok', keys: [{ keyId: '0123456789ab', agent: 'dev', state: 'active', issuedAt: '2026-11-01T10:00:00.000Z' }], other: 1, sentinel: 1 },
|
||||
{ id: 'attention', health: 'attention', keys: [{ keyId: '0123456789ab', agent: 'dev', state: 'active', issuedAt: '2026-11-01T10:00:00.000Z' }, { keyId: 'bbbbbbbbbbbb', agent: 'dev', state: 'revoked', issuedAt: '2026-11-01T10:00:00.000Z' }], other: 0, sentinel: 0 },
|
||||
{ id: 'missing', health: 'missing', keys: [], other: 1, sentinel: 0 },
|
||||
{ id: 'fixture', health: 'not-riding', keys: [] },
|
||||
{ id: 'own', health: 'not-riding', keys: [] },
|
||||
{ id: 'down', health: 'unreadable', reason: 'bus-down', keys: [] },
|
||||
{ id: 'no-client', health: 'unreadable', reason: 'no-proxy-client', keys: [] },
|
||||
{ id: 'uncertain', health: 'attention', reason: 'proxy-uncertain', keys: [] },
|
||||
{ id: 'proxy-down', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
{ id: 'bad-shape', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
{ id: 'planted', health: 'attention', keys: [{ keyId: 'dddddddddddd', agent: null, state: null, issuedAt: '2026-11-01T10:00:00.000Z' }, { keyId: 'eeeeeeeeeeee', agent: 'dev', state: 'active', issuedAt: null }], other: 0, sentinel: 0 },
|
||||
{ id: 'planted-id', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
{ id: 'unread', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
]);
|
||||
assert.doesNotMatch(JSON.stringify(out), KEY_TEXT);
|
||||
assert.notEqual(out.organizations.find(o => o.id === 'uncertain').health, 'ok');
|
||||
// proxy.keys is the only verb access sends; the usage queue is never read.
|
||||
for (const o of all) for (const [verb] of o.calls) assert.equal(verb, 'proxy.keys');
|
||||
});
|
||||
@@ -0,0 +1,73 @@
|
||||
// #1873 settings-backend-a3: the management client's sign-in allow list and the setup status's
|
||||
// provider field both come from the plugin's PROVIDERS. oauthStart refuses the key provider and any
|
||||
// provider a test copy of the list leaves out, before any request. The status field carries id,
|
||||
// label and sign-in kind only: no base URL, no sign-in host, no key text.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import http from 'node:http';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { ManagementClient } from '../src/client.mjs';
|
||||
import { createProxyAdmin } from '../src/admin-control.mjs';
|
||||
import { PROVIDERS } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
const code = name => error => error.code === name;
|
||||
|
||||
async function server(t) {
|
||||
const asked = [];
|
||||
const s = http.createServer((q, r) => { asked.push(new URL(q.url, 'http://x').searchParams.get('provider')); r.setHeader('content-type', 'application/json'); r.end(JSON.stringify({ status: 'ok', state: 's', url: 'https://claude.ai/x?state=s' })); });
|
||||
await new Promise(r => s.listen(0, '127.0.0.1', r)); t.after(() => new Promise(r => s.close(r)));
|
||||
return { asked, baseUrl: `http://127.0.0.1:${s.address().port}` };
|
||||
}
|
||||
|
||||
test('a3: oauthStart asks the proxy for each sign-in provider in PROVIDERS and refuses the key provider before any request', async t => {
|
||||
const { asked, baseUrl } = await server(t);
|
||||
const c = new ManagementClient({ baseUrl, key: 'a3-management' });
|
||||
for (const p of PROVIDERS.filter(p => p.signIn === 'oauth')) await c.oauthStart(p.id);
|
||||
assert.deepEqual(asked, PROVIDERS.filter(p => p.signIn === 'oauth').map(p => p.id));
|
||||
const keyed = PROVIDERS.filter(p => p.signIn === 'apikey');
|
||||
assert.ok(keyed.length > 0);
|
||||
for (const p of keyed) await assert.rejects(async () => c.oauthStart(p.id), code('proxy-client-invalid'), p.id);
|
||||
for (const id of ['other', '', 'claude&x=1', undefined]) await assert.rejects(async () => c.oauthStart(id), code('proxy-client-invalid'));
|
||||
assert.equal(asked.length, PROVIDERS.filter(p => p.signIn === 'oauth').length, 'no refused provider reached the proxy');
|
||||
});
|
||||
|
||||
test('a3: oauthStart refuses a sign-in provider a test copy of the list leaves out, and asks for one it adds', async t => {
|
||||
const { asked, baseUrl } = await server(t);
|
||||
for (const gone of PROVIDERS.filter(p => p.signIn === 'oauth')) {
|
||||
const c = new ManagementClient({ baseUrl, key: 'a3-management', providers: PROVIDERS.filter(p => p !== gone) });
|
||||
await assert.rejects(async () => c.oauthStart(gone.id), code('proxy-client-invalid'), gone.id);
|
||||
}
|
||||
assert.deepEqual(asked, []);
|
||||
const c = new ManagementClient({ baseUrl, key: 'a3-management', providers: [...PROVIDERS, { id: 'fixture-sso', label: 'Fixture SSO', signIn: 'oauth', signInHost: 'sso.fixture.invalid' }] });
|
||||
await c.oauthStart('fixture-sso');
|
||||
assert.deepEqual(asked, ['fixture-sso']);
|
||||
assert.throws(() => new ManagementClient({ baseUrl, key: 'a3-management', providers: null }), code('proxy-client-invalid'));
|
||||
});
|
||||
|
||||
function admin(t, extra = {}) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'a3-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
fs.mkdirSync(configDir, { mode: 0o700 }); fs.mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const unit = { unit: 'mosaic-proxy-a3.service', status: async () => ({ unit: 'mosaic-proxy-a3.service', installed: false, active: 'inactive', healthy: false }) };
|
||||
return createProxyAdmin({ dataRoot, configDir, unit, buses: async () => ({ stopped: true, running: [] }), fetch: async () => ({}), now: () => new Date(0),
|
||||
orgs: async () => [], harnesses: () => [], rootAgents: () => ({ state: 'absent', agents: {}, assistant: null }), ...extra });
|
||||
}
|
||||
|
||||
test('a3: the status lists the providers as id, label and sign-in kind only, in the plugin\'s order', async t => {
|
||||
const status = await admin(t).status();
|
||||
assert.deepEqual(status.providers, PROVIDERS.map(({ id, label, signIn }) => ({ id, label, signIn })));
|
||||
for (const p of status.providers) assert.deepEqual(Object.keys(p).sort(), ['id', 'label', 'signIn']);
|
||||
const text = JSON.stringify(status.providers);
|
||||
for (const word of ['https://', 'api.z.ai', 'claude.ai', 'auth.openai.com', 'baseUrl', 'signInHost']) assert.equal(text.includes(word), false, word);
|
||||
});
|
||||
|
||||
test('a3: the status field holds no key text, even from a list entry that carries a key, and a provider left out is gone', async t => {
|
||||
const canary = 'a3-status-canary-key-0123456789';
|
||||
const list = [...PROVIDERS.filter(p => p.id !== 'codex'), { id: 'fixture.ai', label: 'Fixture AI', signIn: 'apikey', baseUrl: `https://${canary}.invalid/`, key: canary, apiKey: canary }];
|
||||
const status = await admin(t, { providers: list }).status();
|
||||
assert.deepEqual(status.providers.map(p => p.id), ['claude', 'z.ai', 'fixture.ai']);
|
||||
assert.equal(JSON.stringify(status.providers).includes(canary), false);
|
||||
assert.equal(JSON.stringify(status).includes(canary), false);
|
||||
});
|
||||
@@ -246,10 +246,42 @@ export returns:
|
||||
`minimal`, `low`, `medium`, `high`, `xhigh` and `max` as `--thinking`,
|
||||
Claude Code takes `low`, `medium`, `high`, `xhigh` and `max` as
|
||||
`--effort`.
|
||||
- `preflight({env})` returns `null` or `{code, message}`: pi checks its
|
||||
binary in `node_modules` (`harness-binary-missing`), Claude Code its
|
||||
- `preflight({env})` returns `null` or `{code, message}`, or a promise of
|
||||
one, which the launcher awaits before it launches: pi checks that
|
||||
it finds a pi binary (`harness-binary-missing`), Claude Code its
|
||||
session credential (`no-session-credential`) and `MOSAIC_CLAUDE_BIN`.
|
||||
- `version({env})` is what the manifest records as `harnessVersion`.
|
||||
Pi (#1873 pi-unpin, `harness/pi/binary.mjs`) takes `MOSAIC_PI_BIN`,
|
||||
which must be an absolute path to an executable file; else the first
|
||||
`pi` on PATH, skipping the repository's own `node_modules/.bin`; else
|
||||
the repository's copy. The binary must belong to the
|
||||
`@earendil-works/pi-coding-agent` package: it is the bin that package's
|
||||
`package.json` names. With none, the message is
|
||||
"no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci". No
|
||||
version is pinned. Pi's `mounts` and `installRoots` hold the found
|
||||
package's directory (the repository's whole `node_modules` for the
|
||||
repository's copy, whose packages are hoisted beside pi), and its `env`
|
||||
sets `MOSAIC_PI_BIN` to the binary's path inside the session, which the
|
||||
adapter runs. A refusal names the variable or "the pi on PATH" or "the
|
||||
repository's pi", never a host path. Pi's `preflight` then awaits
|
||||
the fit check (#1873 S3, `packages/harness/src/pi-fit.mjs`), which
|
||||
runs its sandboxed pi starts as child processes, four at a time, so the
|
||||
bus keeps answering, and which every caller that asks while it runs
|
||||
shares. It refuses `harness-feature-missing` when the binary lacks a flag the
|
||||
adapter passes, starts with a missing `-e` file, loads a planted ambient
|
||||
source or context file with the seal flags on, or can't be checked. When its `--help`
|
||||
lists `--no-mcp`, `env` sets `MOSAIC_PI_NO_MCP=1` and the adapter
|
||||
passes `--no-mcp`; otherwise it sets `MOSAIC_PI_NO_MCP=0`. `env`'s
|
||||
variables are built in `harness/pi/session-env.mjs`, which the fit
|
||||
check also uses so its runs get a session's names, from the bus's own
|
||||
environment as a session's are. `env` runs nothing: it reads the passed
|
||||
check for the binary's files and the bus's names, and throws when there
|
||||
is none. A host that calls `env` without `preflight` awaits the
|
||||
optional `ready({env})` first (the Triage host and route do), which runs
|
||||
the check, or shares the one running, and throws its refusal line.
|
||||
- `version({env})` is what the manifest records as `harnessVersion`, or a
|
||||
promise of it, which the launcher awaits. Pi's is the first line its
|
||||
binary prints for `--version`, or null; the binary runs as a child
|
||||
process.
|
||||
- `countsAccount()` says whether its launches spend `launch.account` and get
|
||||
the row 126 account checks. The same answer decides whether an account
|
||||
quota covers authority hires (#1873); the bus reads it once at start, so
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
// #1873 pi-unpin: the pi binary a session runs, and the package it comes from. Nothing here is
|
||||
// pinned. The version label is read from the binary (or its package.json), never compared to a
|
||||
// fixed one. The launcher, the bundle and the usage reader call these on the host.
|
||||
import { accessSync, constants, readFileSync, realpathSync, statSync } from "node:fs";
|
||||
import { dirname, isAbsolute, join, relative } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { spawn } from "node:child_process";
|
||||
|
||||
const REPO = fileURLToPath(new URL("../../../../", import.meta.url));
|
||||
export const PACKAGE = "@earendil-works/pi-coding-agent";
|
||||
export const REPO_BIN = join(REPO, "node_modules", ".bin", "pi");
|
||||
const VERSION_MAX = 128;
|
||||
|
||||
export const executable = (path) => {
|
||||
try {
|
||||
accessSync(path, constants.X_OK);
|
||||
return statSync(path).isFile();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const REPO_NODE_MODULES = join(REPO, "node_modules");
|
||||
const realOrNull = (path) => { try { return realpathSync(path); } catch { return null; } };
|
||||
const inside = (path, root) => root === "/" || path === root || path.startsWith(root + "/");
|
||||
|
||||
// #1873 S2: the binary a session runs, with the name a refusal line uses for it (never a path):
|
||||
// MOSAIC_PI_BIN, which must be an absolute path to an executable file; else the first executable
|
||||
// pi on PATH, skipping the repository's own node_modules/.bin (a session PATH starts with it, so
|
||||
// the bus's own PATH and a session's give the same answer); else the repository's copy. The binary
|
||||
// must belong to the pi package: its package directory (or, for the repository's copy, the
|
||||
// repository's node_modules) is what the sandbox mounts. The answer: { bin, label, root, view,
|
||||
// dest, pkg } or { bin: null, problem }. root is the real directory mounted, dest where it is
|
||||
// mounted (null: at root itself), view the binary's path inside the session, pkg the package's
|
||||
// real directory.
|
||||
export function locate(env = {}) {
|
||||
if (env.MOSAIC_PI_BIN !== undefined && env.MOSAIC_PI_BIN !== "") {
|
||||
const bin = env.MOSAIC_PI_BIN;
|
||||
if (typeof bin !== "string" || !isAbsolute(bin) || !executable(bin)) return { bin: null, problem: "MOSAIC_PI_BIN isn't an executable file at an absolute path" };
|
||||
return placed(bin, "MOSAIC_PI_BIN", env);
|
||||
}
|
||||
const repoBin = realOrNull(join(REPO_NODE_MODULES, ".bin"));
|
||||
for (const dir of String(env.PATH ?? "").split(":")) {
|
||||
if (!isAbsolute(dir)) continue;
|
||||
const real = realOrNull(dir);
|
||||
if (real === null || (repoBin !== null && real === repoBin)) continue;
|
||||
if (executable(join(dir, "pi"))) return placed(join(dir, "pi"), "the pi on PATH", env);
|
||||
}
|
||||
if (executable(REPO_BIN)) return placed(REPO_BIN, "the repository's pi", env);
|
||||
return { bin: null, problem: "no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci" };
|
||||
}
|
||||
|
||||
function placed(bin, label, env) {
|
||||
const pkg = packageDir(bin);
|
||||
const realBin = realOrNull(bin);
|
||||
if (!pkg || !realBin) return { bin: null, problem: `${label} isn't part of the ${PACKAGE} package` };
|
||||
// The package directory is mounted whole, so the binary must be the pi bin its package.json
|
||||
// names (a string bin, or bin.pi), inside that directory. A stray executable under some wider
|
||||
// directory whose package.json carries the name doesn't count.
|
||||
if (namedBin(pkg) !== realBin) return { bin: null, problem: `${label} isn't the pi bin the ${PACKAGE} package names` };
|
||||
// The repository's copy keeps the mount every session has had: all of node_modules, at its own
|
||||
// path, since its packages are hoisted beside pi.
|
||||
const repoReal = realOrNull(REPO_NODE_MODULES);
|
||||
if (repoReal !== null && inside(pkg, repoReal)) {
|
||||
return { bin, label, root: repoReal, dest: REPO_NODE_MODULES, view: join(REPO_NODE_MODULES, relative(repoReal, realBin)), pkg };
|
||||
}
|
||||
// Any other copy: its package directory alone, at its real path. npm nests a global install's
|
||||
// dependencies inside it; a copy that needs packages beside it fails the fit check (S3).
|
||||
// A package directory that holds the home directory or the repository would mount them.
|
||||
const held = [homedir(), env.HOME, REPO].filter((p) => typeof p === "string" && isAbsolute(p)).map((p) => realOrNull(p) ?? p);
|
||||
if (held.some((p) => inside(p, pkg))) return { bin: null, problem: `${label}'s package directory holds the home directory or the repository` };
|
||||
return { bin, label, root: pkg, dest: null, view: realBin, pkg };
|
||||
}
|
||||
|
||||
// The real path of the pi bin the package.json in `pkg` names, inside `pkg`, or null.
|
||||
function namedBin(pkg) {
|
||||
let bin;
|
||||
try { bin = JSON.parse(readFileSync(join(pkg, "package.json"), "utf8")).bin; } catch { return null; }
|
||||
const rel = typeof bin === "string" ? bin : bin && typeof bin === "object" ? bin.pi : null;
|
||||
if (typeof rel !== "string" || !rel) return null;
|
||||
const real = realOrNull(join(pkg, rel));
|
||||
return real !== null && real !== pkg && inside(real, pkg) ? real : null;
|
||||
}
|
||||
|
||||
// The package directory that holds the binary's real file: the nearest directory above it whose
|
||||
// package.json has a name. That name must be PACKAGE, or there is none (null).
|
||||
export function packageDir(bin) {
|
||||
let dir;
|
||||
try { dir = dirname(realpathSync(bin)); } catch { return null; }
|
||||
for (let depth = 0; depth < 32; depth++) {
|
||||
let pkg = null;
|
||||
try { pkg = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")); } catch { pkg = null; }
|
||||
if (pkg && typeof pkg === "object" && pkg.name !== undefined) return pkg.name === PACKAGE ? dir : null;
|
||||
const up = dirname(dir);
|
||||
if (up === dir) return null;
|
||||
dir = up;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// The found package's own version, a file read with no spawn, or null.
|
||||
export function packageVersion(bin) {
|
||||
const dir = bin ? packageDir(bin) : null;
|
||||
if (!dir) return null;
|
||||
try {
|
||||
const v = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")).version;
|
||||
return typeof v === "string" && cleanLabel(v) ? v : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// The most a --version may print (spawnSync's default buffer, which the check used before).
|
||||
const VERSION_OUTPUT_MAX = 1024 * 1024;
|
||||
// One printable line, short enough to record.
|
||||
const cleanLabel = (s) => s.length > 0 && s.length <= VERSION_MAX && !/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/.test(s);
|
||||
|
||||
// The first line `<bin> --version` prints, or null when it fails, times out or prints something
|
||||
// that is not one short printable line. The environment holds only a PATH that finds node. A
|
||||
// promise: the binary runs as a child process, so the caller's event loop keeps running.
|
||||
export function printedVersion(bin) {
|
||||
if (!bin) return Promise.resolve(null);
|
||||
return new Promise((resolve) => {
|
||||
const out = [];
|
||||
let size = 0;
|
||||
let done = false;
|
||||
let child;
|
||||
let timer = null;
|
||||
const finish = (line) => {
|
||||
if (done) return;
|
||||
done = true;
|
||||
clearTimeout(timer);
|
||||
resolve(line);
|
||||
};
|
||||
try {
|
||||
child = spawn(bin, ["--version"], { env: { PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, PI_OFFLINE: "1" }, stdio: ["ignore", "pipe", "ignore"] });
|
||||
} catch {
|
||||
finish(null);
|
||||
return;
|
||||
}
|
||||
timer = setTimeout(() => { try { child.kill("SIGKILL"); } catch {} finish(null); }, 5000);
|
||||
child.stdout.on("data", (chunk) => {
|
||||
size += chunk.length;
|
||||
if (size > VERSION_OUTPUT_MAX) { try { child.kill("SIGKILL"); } catch {} finish(null); } else out.push(chunk);
|
||||
});
|
||||
child.on("error", () => finish(null));
|
||||
child.on("close", (status) => {
|
||||
const line = Buffer.concat(out).toString("utf8").split("\n")[0].trim();
|
||||
finish(status === 0 && cleanLabel(line) ? line : null);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
// `compactions` through the harness kind (packages/plugins/README.md has the contract). This file
|
||||
// reads only the session's native pi store and the Claude history beside it.
|
||||
//
|
||||
// Pi (pinned 0.85.1): a completed compaction is one `type: "compaction"` entry,
|
||||
// Pi (read from 0.85.1): a completed compaction is one `type: "compaction"` entry,
|
||||
// written by SessionManager.appendCompaction after the summary exists. A start
|
||||
// or an aborted compaction writes none. A `compactionSummary` message, a
|
||||
// message inside a compaction's retainedTail and a branch_summary are not
|
||||
@@ -19,8 +19,10 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { closeSync, constants as fsc, fstatSync, lstatSync, openSync, readdirSync, readSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { locate, packageVersion } from "./binary.mjs";
|
||||
|
||||
export const PI_VERSION = "0.85.1";
|
||||
// #1873 S1: the found package's own version (its package.json, no spawn), or null.
|
||||
const foundVersion = () => packageVersion(locate(process.env).bin);
|
||||
const TAIL = 4096;
|
||||
const CHUNK = 1 << 20;
|
||||
const STREAM = /^[A-Za-z0-9._-]{1,200}\.jsonl$/;
|
||||
@@ -158,9 +160,9 @@ export function compactions({ store, cursors, budget, streams, first }) {
|
||||
if (first && claudeHistory(store)) {
|
||||
// Claude runs before this ledger: their compactions can't be counted
|
||||
// (Vikunja 466), so the count is unknown for good, as after a Claude run.
|
||||
return { found: [], cursors: {}, known: false, reason: "harness-unsupported", version: PI_VERSION };
|
||||
return { found: [], cursors: {}, known: false, reason: "harness-unsupported", version: foundVersion() };
|
||||
}
|
||||
const scan = scanPi(store.session, cursors, budget, streams);
|
||||
const reason = REASONS.find((p) => scan.problems.includes(p)) ?? null;
|
||||
return { found: scan.found, cursors: scan.cursors, known: reason === null, reason, version: PI_VERSION };
|
||||
return { found: scan.found, cursors: scan.cursors, known: reason === null, reason, version: foundVersion() };
|
||||
}
|
||||
@@ -1,18 +1,19 @@
|
||||
// The "pi" harness plug-in (row 65, REQ-PLUG-2, Q156): a managed session on the pinned
|
||||
// @earendil-works/pi-coding-agent CLI, the repository's node_modules/.bin/pi. The tool gate is the
|
||||
// The "pi" harness plug-in (row 65, REQ-PLUG-2, Q156): a managed session on the
|
||||
// @earendil-works/pi-coding-agent CLI that binary.mjs finds (#1873: no version pin). The tool gate is the
|
||||
// Pi extension, which stays in packages/harness (a module can't change that boundary). The
|
||||
// launcher, the bundle and the usage reader call these on the host; the runner gets the answers
|
||||
// as data in session.json. packages/plugins/README.md lists the interface.
|
||||
import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
|
||||
import { lstatSync, mkdirSync, writeFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { locate, printedVersion } from "./binary.mjs";
|
||||
import { fitVerdict, piFit } from "../../../harness/src/pi-fit.mjs";
|
||||
import { envNames, harnessEnv } from "./session-env.mjs";
|
||||
|
||||
export const interfaceVersion = 1;
|
||||
|
||||
const REPO = fileURLToPath(new URL("../../../../", import.meta.url));
|
||||
const PACKAGE = "@earendil-works/pi-coding-agent";
|
||||
const BIN = join(REPO, "node_modules", ".bin", "pi");
|
||||
const EXTENSION = fileURLToPath(new URL("../../../harness/src/pi-extension.mjs", import.meta.url));
|
||||
// Row 66 (#1559): the unslop check, loaded as a second extension. It logs to MOSAIC_UNSLOP_LOG,
|
||||
// which the runner sets, and the runner tells the operator.
|
||||
@@ -31,33 +32,53 @@ export const adapter = () => join(REPO, "adapters", "pi", "adapter.sh");
|
||||
export const levels = () => LEVELS;
|
||||
export const outputPolicy = ({ proxy }) => proxy ? "non-newline-stdout" : "stdout";
|
||||
|
||||
// The session runs pi from the repository's node_modules, which the sandbox mounts.
|
||||
export function preflight({ env = {}, proxy = null } = {}) {
|
||||
// The session runs the pi binary.mjs finds, from the package directory the sandbox mounts. Async:
|
||||
// the fit check runs child processes, and the launcher awaits the answer before it launches.
|
||||
export async function preflight({ env = {}, proxy = null } = {}) {
|
||||
if (!(typeof proxy?.key === "string" && proxy.key)
|
||||
&& !["ANTHROPIC_API_KEY", "OPENAI_API_KEY", "OPENROUTER_API_KEY", "ZAI_API_KEY"].some(name => Boolean(env[name]))) {
|
||||
return { code: "no-session-credential", message: "pi needs its own provider key or a session proxy key" };
|
||||
}
|
||||
if (!existsSync(BIN)) return { code: "harness-binary-missing", message: "pi isn't installed in the repository's node_modules; run npm ci in the repository" };
|
||||
const found = locate(env);
|
||||
if (!found.bin) return { code: "harness-binary-missing", message: found.problem };
|
||||
// #1873 S3: the found binary takes every flag the adapter passes and keeps the known ambient
|
||||
// sources off with them, or the launch is refused. A check that cannot tell refuses too. The
|
||||
// check's environment comes from this process's own (the bus's), as a session's does.
|
||||
const fit = await piFit(found, { levels: LEVELS });
|
||||
if (!fit.ok) return { code: "harness-feature-missing", message: fit.message };
|
||||
return null;
|
||||
}
|
||||
|
||||
// The exact pin from the repository's package.json.
|
||||
export function version() {
|
||||
try {
|
||||
const pkg = JSON.parse(readFileSync(join(REPO, "package.json"), "utf8"));
|
||||
return pkg.dependencies?.[PACKAGE] ?? pkg.devDependencies?.[PACKAGE] ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
// #1873 S1: what the found binary prints for --version (5 s timeout), or null. A label only:
|
||||
// nothing compares it to a fixed version. Async, so the bus keeps answering while it runs.
|
||||
export async function version({ env = {} } = {}) {
|
||||
return printedVersion(locate(env).bin);
|
||||
}
|
||||
|
||||
// #1873 S3: runs the fit check, or shares the one running, and throws its refusal line when the
|
||||
// binary fails it. A host that calls env() without preflight (Triage) awaits this first.
|
||||
export async function ready({ env = process.env } = {}) {
|
||||
const fit = await piFit(found(env), { levels: LEVELS });
|
||||
if (!fit.ok) throw new Error(fit.message);
|
||||
}
|
||||
|
||||
// Pi sessions run on a provider key, never the Claude account limit.
|
||||
export const countsAccount = () => false;
|
||||
export const mounts = () => [
|
||||
{ src: join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), why: "the managed model event stream" },
|
||||
{ src: realpathSync(join(REPO, "node_modules")), dest: join(REPO, "node_modules"), why: "pi and its packages, on the session's PATH" },
|
||||
...Object.values(UNSLOP).map((src) => ({ src, why: "the unslop check the session's second extension runs" })),
|
||||
];
|
||||
// #1873 S2: the found pi's package directory (the repository's copy: all of node_modules, as before).
|
||||
// One under /usr is already in every sandbox.
|
||||
const found = (env) => {
|
||||
const at = locate(env);
|
||||
if (!at.bin) throw new Error(at.problem);
|
||||
return at;
|
||||
};
|
||||
export function mounts({ env = {} } = {}) {
|
||||
const at = found(env);
|
||||
return [
|
||||
{ src: join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), why: "the managed model event stream" },
|
||||
...(at.root.startsWith("/usr/") ? [] : [{ src: at.root, ...(at.dest ? { dest: at.dest } : {}), why: `pi and its packages (${at.label})` }]),
|
||||
...Object.values(UNSLOP).map((src) => ({ src, why: "the unslop check the session's second extension runs" })),
|
||||
];
|
||||
}
|
||||
// Pi has no web tool of its own, so a granted role under harness-builtin is refused.
|
||||
export const builtinWeb = () => false;
|
||||
export const builtins = ({ tools }) => [...tools];
|
||||
@@ -105,26 +126,33 @@ export function bundle({ dir, models, proxy = null, transports = {} } = {}) {
|
||||
return { files: { proxyModels: file }, digests: [["../home/.pi/agent/models.json", createHash("sha256").update(text).digest("hex")]], sources };
|
||||
}
|
||||
|
||||
// The tool limit is the built-ins plus the typed tools, which the extension serves.
|
||||
export function env({ policy, tools, session, proxy = null }) {
|
||||
return {
|
||||
PI_OFFLINE: "1",
|
||||
MOSAIC_TOOLS: [...policy.tools, ...tools.map((t) => t.name)].join(","),
|
||||
MOSAIC_EXTENSIONS: [EXTENSION, UNSLOP.unslopExtension].join(","),
|
||||
PI_PROVIDER: proxy ? session.proxyTransport.piProvider : session.provider ?? "",
|
||||
PI_MODEL: session.model,
|
||||
MOSAIC_THINKING: session.thinking,
|
||||
...(proxy ? { ANTHROPIC_API_KEY: proxy.key } : {}),
|
||||
};
|
||||
// The tool limit is the built-ins plus the typed tools, which the extension serves. #1873 S2:
|
||||
// MOSAIC_PI_BIN is the found binary's path inside the session; the bus's own environment names it
|
||||
// (env defaults to it, since buildBundle and the Triage host call env() without one). preflight,
|
||||
// version and mounts get the launcher's env instead. Both find the same binary: the session env
|
||||
// copies PATH and MOSAIC_PI_BIN from the bus's, and locate() skips the repository's .bin on PATH.
|
||||
// env() runs nothing: it reads the fit check that preflight or ready() awaited for what it found.
|
||||
// With none passed for those files and this environment (they differ, the files changed since, or
|
||||
// nobody awaited one), it throws, so the launch fails closed (as harness-failed, not
|
||||
// harness-feature-missing).
|
||||
export function env({ policy, tools, session, proxy = null, env: hostEnv = process.env }) {
|
||||
const at = found(hostEnv);
|
||||
// #1873 S3: a session runs only a binary that passed the fit check.
|
||||
const fit = fitVerdict(at, { levels: LEVELS });
|
||||
if (!fit) throw new Error(`pi (${at.label}) has no passed fit check for its files; preflight or ready() runs one first`);
|
||||
return harnessEnv({ bin: at.view, noMcp: fit.noMcp, tools: [...policy.tools, ...tools.map((t) => t.name)], extensions: [EXTENSION, UNSLOP.unslopExtension], session, proxy });
|
||||
}
|
||||
|
||||
// #1873 triage-fences: the names env() returns, less the provider key, and the directories its
|
||||
// adapter and mounts live in. A hosted session's host (Triage) takes harnessEnv names and mount
|
||||
// sources only from these; launches don't read them. The host refuses every MOSAIC_* name from a
|
||||
// route, so MOSAIC_TOOLS, MOSAIC_EXTENSIONS and MOSAIC_THINKING are listed only to say what env()
|
||||
// hands a session: a hosted pi needs the host to set them itself (a later slice).
|
||||
export const envNames = Object.freeze(["PI_OFFLINE", "MOSAIC_TOOLS", "MOSAIC_EXTENSIONS", "PI_PROVIDER", "PI_MODEL", "MOSAIC_THINKING"]);
|
||||
export const installRoots = () => [join(REPO, "adapters", "pi"), join(REPO, "packages", "plugins", "harness", "pi"), realpathSync(join(REPO, "node_modules")), join(REPO, "tools", "unslop-hook")];
|
||||
// hands a session: a hosted pi needs the host to set them itself (a later slice). MOSAIC_PI_BIN
|
||||
// (#1873 S2) is the binary's path inside the session, which the host keeps from its own env() call;
|
||||
// MOSAIC_PI_NO_MCP (#1873 S3) is "1" when the binary's --help lists --no-mcp, else "0".
|
||||
// session-env.mjs holds the list beside the builder.
|
||||
export { envNames };
|
||||
export const installRoots = ({ env = {} } = {}) => [join(REPO, "adapters", "pi"), join(REPO, "packages", "plugins", "harness", "pi"), found(env).root, join(REPO, "tools", "unslop-hook")];
|
||||
|
||||
// The extension writes the turn marker at agent_end; an exit without it is a failed turn.
|
||||
export const turnEnd = () => "marker";
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
// The variables the pi module hands a session (index.mjs env() returns them as harnessEnv). Built
|
||||
// here, with no fit check, so packages/harness/src/pi-fit.mjs (#1873 S3) gives its runs the same
|
||||
// names as a session gets without importing index.mjs, which imports it.
|
||||
//
|
||||
// bin: the binary's path inside the session. noMcp: the binary's --help lists --no-mcp. tools: the
|
||||
// tool names. extensions: the -e files. session: { provider, model, thinking, proxyTransport }.
|
||||
// proxy: { key } when the session runs on the proxy, else null.
|
||||
export function harnessEnv({ bin, noMcp, tools, extensions, session, proxy = null }) {
|
||||
return {
|
||||
MOSAIC_PI_BIN: bin,
|
||||
// "1" when --help lists --no-mcp: the adapter then passes it, so MCP stays off by its own
|
||||
// flag. "0" otherwise, so every name in envNames is always set.
|
||||
MOSAIC_PI_NO_MCP: noMcp ? "1" : "0",
|
||||
PI_OFFLINE: "1",
|
||||
MOSAIC_TOOLS: tools.join(","),
|
||||
MOSAIC_EXTENSIONS: extensions.join(","),
|
||||
PI_PROVIDER: proxy ? session.proxyTransport.piProvider : session.provider ?? "",
|
||||
PI_MODEL: session.model,
|
||||
MOSAIC_THINKING: session.thinking,
|
||||
...(proxy ? { ANTHROPIC_API_KEY: proxy.key } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
// #1873 triage-fences: the names harnessEnv returns, less the provider key. index.mjs exports
|
||||
// them; its comment says what a hosted session's host does with them.
|
||||
export const envNames = Object.freeze(["MOSAIC_PI_BIN", "MOSAIC_PI_NO_MCP", "PI_OFFLINE", "MOSAIC_TOOLS", "MOSAIC_EXTENSIONS", "PI_PROVIDER", "PI_MODEL", "MOSAIC_THINKING"]);
|
||||
|
||||
// #1873 pi-unpin re-check 8: the provider and model the fit check's session-shaped runs pass
|
||||
// (packages/harness/src/pi-fit.mjs SESSION_SHAPE). The runs are offline with no key, so pi stops
|
||||
// before any model call; the names only need to be ones pi takes. Re-check 9: a Claude model is
|
||||
// offered, routed and tested only on the claude-code harness, so this names no Claude model and
|
||||
// no Anthropic provider (pi-fit.test.mjs holds that); glm-5.3-flash is the model index.mjs has
|
||||
// documented limits for, and pi's own catalogue lists it under zai.
|
||||
export const FIT_ROUTE = Object.freeze({ provider: "zai", model: "glm-5.3-flash" });
|
||||
@@ -1,6 +1,18 @@
|
||||
// Provider configuration and transport declarations owned by the proxy module.
|
||||
export const interfaceVersion = 1;
|
||||
export const ZAI_URL = 'https://api.z.ai/api/anthropic';
|
||||
// #1873 settings-backend-a3: the one list of providers an account can be added for. The bus proxy
|
||||
// page, the management client and the Console's provider select all read it; a provider not here
|
||||
// is refused. `id` is the value the page sends and an account row carries. An OAuth provider names
|
||||
// the host its sign-in address must be on; the key provider names the base URL its key group uses.
|
||||
// The setup status and the accounts read pass on only id, label and sign-in kind.
|
||||
export const PROVIDERS = Object.freeze([
|
||||
Object.freeze({ id: 'claude', label: 'Claude', signIn: 'oauth', signInHost: 'claude.ai' }),
|
||||
Object.freeze({ id: 'codex', label: 'ChatGPT / Codex', signIn: 'oauth', signInHost: 'auth.openai.com' }),
|
||||
Object.freeze({ id: 'z.ai', label: 'z.ai', signIn: 'apikey', baseUrl: ZAI_URL }),
|
||||
]);
|
||||
// What a page may see of the list: no URL, no host, nothing else.
|
||||
export const publicProviders = (list = PROVIDERS) => list.map(({ id, label, signIn }) => ({ id, label, signIn }));
|
||||
export const MODEL_ID = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,199}$/;
|
||||
const MODEL = MODEL_ID;
|
||||
const MAX_MODELS = 10000;
|
||||
|
||||
@@ -54,6 +54,7 @@ export const KINDS = Object.freeze({
|
||||
// each one's contract:
|
||||
// adapter() -> the adapter script's absolute path levels() -> the thinking levels it applies
|
||||
// preflight({ env }) -> null | { code, message } version({ env }) -> string | null
|
||||
// (either may answer with a promise of that; the launcher awaits it before it launches)
|
||||
// countsAccount() -> true when its launches spend launch.account
|
||||
// mounts({ env }) -> read-only binds [{ src, dest, why }]
|
||||
// builtinWeb() -> true when it serves harness-builtin web
|
||||
@@ -62,6 +63,8 @@ export const KINDS = Object.freeze({
|
||||
// gateTimeout() -> the gate hook's timeouts | null
|
||||
// bundle({ dir, files, session, write, quote }) -> { files, digests, sources }, its own bundle files
|
||||
// env({ files, policy, tools, session }) -> its adapter variables, written into session.json
|
||||
// (optional, outside the list) ready({ env }) -> a promise a host that calls env() without
|
||||
// preflight awaits first; it rejects when env() would refuse (pi's fit check)
|
||||
// turnEnd() -> "marker" (a turn ends only with the turn marker) | "exit"
|
||||
// outputPolicy({ proxy }) -> "adapter" | "non-newline-stdout" | "stdout"
|
||||
// transcript({ runDir, store }) -> the directory its transcripts land in, for usage
|
||||
|
||||
@@ -3,13 +3,14 @@
|
||||
// harness outside the exceptions the brief lists.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, relative, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { createLoader } from "../src/loader.mjs";
|
||||
import { KINDS } from "../src/kinds.mjs";
|
||||
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
|
||||
|
||||
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
|
||||
|
||||
@@ -98,11 +99,11 @@ test("row 65 claude-code version asks MOSAIC_CLAUDE_BIN when set, else claude on
|
||||
assert.equal(claude.version({ env: { PATH: join(dir, "none") } }), null);
|
||||
});
|
||||
|
||||
test("row 65 pi preflight refuses missing credentials, and its version is the package pin", async () => {
|
||||
test("row 65 pi preflight refuses missing credentials, and its version is what the installed pi prints (#1873 S1)", async () => {
|
||||
const pi = await load("pi");
|
||||
assert.equal(pi.preflight({ env: {} }).code, "no-session-credential");
|
||||
const pkg = JSON.parse(readFileSync(join(REPO, "package.json"), "utf8"));
|
||||
assert.equal(pi.version({ env: {} }), pkg.dependencies?.["@earendil-works/pi-coding-agent"] ?? pkg.devDependencies?.["@earendil-works/pi-coding-agent"]);
|
||||
assert.equal((await pi.preflight({ env: {} })).code, "no-session-credential");
|
||||
const installed = JSON.parse(readFileSync(join(REPO, "node_modules", "@earendil-works", "pi-coding-agent", "package.json"), "utf8")).version;
|
||||
assert.equal(await pi.version({ env: {} }), installed);
|
||||
// Row 66: the unslop extension's files, bound read-only at their own paths.
|
||||
assert.deepEqual(pi.mounts({ env: {} }).map(m => m.dest ?? m.src), [join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), join(REPO, "node_modules"), ...["extension.ts", "unslop-check.js", "lists.json"].map(f => join(REPO, "tools", "unslop-hook", f))]);
|
||||
});
|
||||
@@ -110,6 +111,8 @@ test("row 65 pi preflight refuses missing credentials, and its version is the pa
|
||||
test("row 65 env passes the thinking level to both adapters, and claude-code's bundle writes its hook and MCP files", async t => {
|
||||
const pi = await load("pi"), claude = await load("claude-code");
|
||||
const session = { provider: "fixture", model: "m", thinking: "high", toolSocket: "/run/tools.sock" };
|
||||
// #1873 S3: env() reads the fit check ready() awaits for the found pi.
|
||||
await pi.ready();
|
||||
const piEnv = pi.env({ policy: { tools: ["read"] }, tools: [{ name: "list_tasks" }], session });
|
||||
assert.equal(piEnv.PI_OFFLINE, "1", "pi avoids startup network operations");
|
||||
assert.deepEqual([piEnv.MOSAIC_THINKING, piEnv.PI_MODEL, piEnv.MOSAIC_TOOLS], ["high", "m", "read,list_tasks"]);
|
||||
@@ -140,10 +143,16 @@ test("row 65 a route's thinking reaches pi as --thinking and claude as --effort,
|
||||
const pi = await load("pi"), claude = await load("claude-code");
|
||||
const dir = scratch(t);
|
||||
mkdirSync(join(dir, "bin"));
|
||||
for (const name of ["pi", "claude"]) {
|
||||
writeFileSync(join(dir, "bin", name), '#!/bin/sh\nprintf "%s\\n" "$@" > "$ARGV_OUT"\necho \'{"type":"result","subtype":"success","result":"ok"}\'\n');
|
||||
chmodSync(join(dir, "bin", name), 0o755);
|
||||
}
|
||||
// #1873 S2: pi's stand-in sits in a pi package, as the module requires, and is found on PATH.
|
||||
const piPackage = join(dir, "lib", "node_modules", "@earendil-works", "pi-coding-agent");
|
||||
mkdirSync(join(piPackage, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(piPackage, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "0.0.1", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
const result = '{"type":"result","subtype":"success","result":"ok"}\n';
|
||||
writeFileSync(join(dir, "bin", "claude"), `#!/bin/sh\nprintf "%s\\n" "$@" > "$ARGV_OUT"\necho '${result.trim()}'\n`);
|
||||
// #1873 S3: pi's stand-in also passes the fit check that ready() runs and env() reads.
|
||||
writeFileSync(join(piPackage, "dist", "bundle", "cli.js"), piStandIn({ sessionOut: result }));
|
||||
for (const file of [join(dir, "bin", "claude"), join(piPackage, "dist", "bundle", "cli.js")]) chmodSync(file, 0o755);
|
||||
symlinkSync(join(piPackage, "dist", "bundle", "cli.js"), join(dir, "bin", "pi"));
|
||||
for (const f of ["prompt.md", "settings.json", "mcp.json"]) writeFileSync(join(dir, f), "{}");
|
||||
const run = (adapter, moduleEnv) => {
|
||||
const out = join(dir, "argv");
|
||||
@@ -163,9 +172,10 @@ test("row 65 a route's thinking reaches pi as --thinking and claude as --effort,
|
||||
assert.equal(argv.filter(a => a === name).length, 1, argv.join(" "));
|
||||
return argv[argv.indexOf(name) + 1];
|
||||
};
|
||||
await pi.ready({ env: { PATH: `${join(dir, "bin")}:/usr/bin:/bin` } });
|
||||
for (const thinking of ["xhigh", "low"]) {
|
||||
const session = { provider: "fixture", model: "m", thinking, toolSocket: "/run/tools.sock" };
|
||||
assert.equal(flag(run("pi", pi.env({ policy: { tools: ["read"] }, tools: [], session })), "--thinking"), thinking);
|
||||
assert.equal(flag(run("pi", pi.env({ policy: { tools: ["read"] }, tools: [], session, env: { PATH: `${join(dir, "bin")}:/usr/bin:/bin` } })), "--thinking"), thinking);
|
||||
const files = { settings: join(dir, "settings.json"), mcp: join(dir, "mcp.json") };
|
||||
assert.equal(flag(run("claude", claude.env({ files, policy: { tools: ["read"], typed: [] }, session })), "--effort"), thinking);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,216 @@
|
||||
// #1873 pi-unpin (design section 5, S1): pi's labels come from the binary the module finds, not
|
||||
// from a pin. The version is what `<bin> --version` prints, or null when that fails; the
|
||||
// compaction count records the found package's own version.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { sandboxProblem } from "../../seat/src/sandbox.mjs";
|
||||
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
|
||||
|
||||
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
|
||||
const MODULE = join(REPO, "packages", "plugins", "harness", "pi");
|
||||
const NAME = "@earendil-works/pi-coding-agent";
|
||||
|
||||
function scratch(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), "mosaic-pi-bin-"));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
return root;
|
||||
}
|
||||
|
||||
// A pi package under `dir` (a node_modules directory), with `script` as its bin.
|
||||
function standIn(dir, script, version) {
|
||||
const pkg = join(dir, NAME);
|
||||
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: NAME, version, bin: { pi: "dist/bundle/cli.js" } }));
|
||||
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), script, { mode: 0o755 });
|
||||
return join(pkg, "dist", "bundle", "cli.js");
|
||||
}
|
||||
|
||||
// A copy of the pi module in a scratch repository whose node_modules holds a stand-in, so the
|
||||
// module's repository copy is that stand-in and not the real pi. The rest of packages/ links to
|
||||
// the real tree, for the modules the copy imports.
|
||||
async function copyRepo(t, script, version) {
|
||||
const root = scratch(t);
|
||||
const mod = join(root, "packages", "plugins", "harness", "pi");
|
||||
mkdirSync(mod, { recursive: true });
|
||||
for (const f of readdirSync(MODULE)) if (f.endsWith(".mjs")) copyFileSync(join(MODULE, f), join(mod, f));
|
||||
for (const p of readdirSync(join(REPO, "packages"))) if (p !== "plugins") symlinkSync(join(REPO, "packages", p), join(root, "packages", p));
|
||||
standIn(join(root, "node_modules"), script, version);
|
||||
mkdirSync(join(root, "node_modules", ".bin"));
|
||||
symlinkSync(`../${NAME}/dist/bundle/cli.js`, join(root, "node_modules", ".bin", "pi"));
|
||||
return { root, pi: await import(pathToFileURL(join(mod, "index.mjs")).href) };
|
||||
}
|
||||
|
||||
const emptyStore = (t) => {
|
||||
const root = scratch(t);
|
||||
mkdirSync(join(root, "session"));
|
||||
return { session: join(root, "session"), projects: join(root, "claude-projects") };
|
||||
};
|
||||
|
||||
test("pi-unpin S1: the version is what the found binary prints, and the compaction count records its package's version", async t => {
|
||||
const { pi } = await copyRepo(t, "#!/bin/sh\n[ \"$1\" = --version ] && echo 9.9.9\n", "9.9.4");
|
||||
assert.equal(await pi.version({ env: {} }), "9.9.9");
|
||||
const counted = pi.compactions({ store: emptyStore(t), cursors: {}, budget: 1 << 20, streams: 16, first: false });
|
||||
assert.equal(counted.version, "9.9.4");
|
||||
});
|
||||
|
||||
test("pi-unpin S1: a binary that exits 1 on --version gives a null version, not a guess", async t => {
|
||||
const { pi } = await copyRepo(t, "#!/bin/sh\nexit 1\n", "8.8.8");
|
||||
assert.equal(await pi.version({ env: {} }), null);
|
||||
// The package's own file still names its version for the compaction count.
|
||||
assert.equal(pi.compactions({ store: emptyStore(t), cursors: {}, budget: 1 << 20, streams: 16, first: false }).version, "8.8.8");
|
||||
});
|
||||
|
||||
test("pi-unpin S1: a version line with a control character or over 128 characters is not recorded", async t => {
|
||||
const { pi } = await copyRepo(t, "#!/bin/sh\nprintf '1.0\\033[31m\\n'\n", "1.0.0");
|
||||
assert.equal(await pi.version({ env: {} }), null);
|
||||
const long = await copyRepo(t, `#!/bin/sh\necho ${"9".repeat(129)}\n`, "1.0.0");
|
||||
assert.equal(await long.pi.version({ env: {} }), null);
|
||||
});
|
||||
|
||||
test("pi-unpin S1: the repository's real pi reports its installed package's version", async () => {
|
||||
const pi = await import(pathToFileURL(join(MODULE, "index.mjs")).href);
|
||||
const installed = JSON.parse(readFileSync(join(REPO, "node_modules", NAME, "package.json"), "utf8")).version;
|
||||
assert.equal(await pi.version({ env: {} }), installed);
|
||||
});
|
||||
|
||||
// #1873 pi-unpin (design section 5, S2): the binary comes from MOSAIC_PI_BIN, else the first pi on
|
||||
// PATH, else the repository's copy. Mounts, install roots and the session's MOSAIC_PI_BIN follow
|
||||
// the package that binary belongs to. A refusal names the variable, never a path.
|
||||
const real = await import(pathToFileURL(join(MODULE, "index.mjs")).href);
|
||||
const CREDENTIAL = { ZAI_API_KEY: "fixture-zai" };
|
||||
// #1873 S3: the stand-in passes the fit check, prints 7.7.7 for --version, and as a session
|
||||
// records its argv in $ARGV_OUT.
|
||||
const ARGV_SCRIPT = piStandIn({ version: "7.7.7" });
|
||||
|
||||
// npm's global layout: prefix/lib/node_modules/<package>, with prefix/bin/pi a link to its bin.
|
||||
function prefix(t, script = ARGV_SCRIPT, version = "7.7.0") {
|
||||
const root = join(scratch(t), "prefix");
|
||||
const cli = standIn(join(root, "lib", "node_modules"), script, version);
|
||||
mkdirSync(join(root, "bin"), { recursive: true });
|
||||
symlinkSync(`../lib/node_modules/${NAME}/dist/bundle/cli.js`, join(root, "bin", "pi"));
|
||||
return { root, bin: join(root, "bin", "pi"), cli: realpathSync(cli), pkg: realpathSync(join(root, "lib", "node_modules", NAME)) };
|
||||
}
|
||||
const session = { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" };
|
||||
// pi.env() reads the fit check that preflight awaited for the same files.
|
||||
const piEnv = (env) => real.env({ policy: { tools: ["read"] }, tools: [], session, env });
|
||||
const noPath = (problem, ...paths) => { for (const p of paths) assert.ok(!problem.message.includes(p), problem.message); };
|
||||
|
||||
test("pi-unpin S2: a stand-in pi in an npm prefix, named by MOSAIC_PI_BIN, is found, mounted alone and run from the mount", async t => {
|
||||
const p = prefix(t);
|
||||
const env = { ...CREDENTIAL, MOSAIC_PI_BIN: p.bin, PATH: "/usr/bin:/bin" };
|
||||
assert.equal(await real.preflight({ env }), null);
|
||||
assert.equal(await real.version({ env }), "7.7.7");
|
||||
const mounts = real.mounts({ env });
|
||||
assert.deepEqual(mounts.filter(m => (m.dest ?? m.src) === p.pkg || m.src === p.pkg).map(m => m.src), [p.pkg]);
|
||||
assert.ok(!mounts.some(m => (m.dest ?? m.src) === join(REPO, "node_modules")), "the repository's node_modules is not mounted");
|
||||
const roots = real.installRoots({ env });
|
||||
assert.ok(roots.includes(p.pkg));
|
||||
assert.ok(!roots.includes(realpathSync(join(REPO, "node_modules"))));
|
||||
const given = piEnv(env);
|
||||
assert.equal(given.MOSAIC_PI_BIN, p.cli, "the session runs the real file inside the mounted package");
|
||||
assert.ok(real.envNames.includes("MOSAIC_PI_BIN"));
|
||||
|
||||
// Run the real adapter in a sandbox that holds the system, the adapter and the module's mounts,
|
||||
// and not the repository's node_modules: the stand-in records the argv it was given.
|
||||
if (sandboxProblem()) return t.skip("no usable bwrap");
|
||||
const out = join(scratch(t), "out");
|
||||
mkdirSync(out);
|
||||
writeFileSync(join(out, "prompt.md"), "p");
|
||||
const argv = ["/usr/bin/bwrap", "--unshare-user", "--unshare-pid", "--unshare-net", "--die-with-parent", "--clearenv", "--ro-bind", "/usr", "/usr"];
|
||||
for (const link of ["/bin", "/sbin", "/lib", "/lib64"]) {
|
||||
if (!existsSync(link)) continue;
|
||||
if (lstatSync(link).isSymbolicLink()) argv.push("--symlink", readlinkSync(link), link);
|
||||
else argv.push("--ro-bind", link, link);
|
||||
}
|
||||
argv.push("--proc", "/proc", "--dev", "/dev", "--ro-bind", join(REPO, "adapters", "pi"), join(REPO, "adapters", "pi"));
|
||||
for (const m of mounts) argv.push("--ro-bind", m.src, m.dest ?? m.src);
|
||||
// The extensions the adapter checks for, which a launch's bundle binds.
|
||||
for (const e of given.MOSAIC_EXTENSIONS.split(",")) argv.push("--ro-bind", e, e);
|
||||
argv.push("--bind", out, out);
|
||||
for (const [k, v] of Object.entries({ ...given, PATH: "/usr/bin:/bin", ARGV_OUT: join(out, "argv"), MOSAIC_SYSTEM_PROMPT_FILE: join(out, "prompt.md"),
|
||||
MOSAIC_REQUEST: "x", MOSAIC_WORKSPACE: join(out, "ws"), MOSAIC_SESSION_DIR: join(out, "session"), MOSAIC_MODEL: "m" })) argv.push("--setenv", k, v);
|
||||
argv.push("/bin/sh", join(REPO, "adapters", "pi", "adapter.sh"));
|
||||
const r = spawnSync(argv[0], argv.slice(1), { encoding: "utf8", timeout: 20000 });
|
||||
assert.equal(r.status, 0, r.stderr);
|
||||
const ran = readFileSync(join(out, "argv"), "utf8").split("\n");
|
||||
assert.ok(ran.includes("--no-context-files") && ran.includes("--thinking"), ran.join(" "));
|
||||
});
|
||||
|
||||
test("pi-unpin S2: a relative or non-executable MOSAIC_PI_BIN is refused with harness-binary-missing, naming the variable only", async t => {
|
||||
const p = prefix(t);
|
||||
const plain = join(scratch(t), "pi");
|
||||
writeFileSync(plain, "#!/bin/sh\n", { mode: 0o644 });
|
||||
for (const bin of ["bin/pi", "pi", plain, join(p.root, "missing")]) {
|
||||
const problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: bin, PATH: `${join(p.root, "bin")}:/usr/bin:/bin` } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", "MOSAIC_PI_BIN isn't an executable file at an absolute path"], bin);
|
||||
noPath(problem, plain, p.root);
|
||||
assert.equal(await real.version({ env: { MOSAIC_PI_BIN: bin } }), null, "no fallback to another pi");
|
||||
}
|
||||
});
|
||||
|
||||
test("pi-unpin S2: with MOSAIC_PI_BIN unset the first pi on PATH is used, and the repository's own .bin on PATH does not count", async t => {
|
||||
const p = prefix(t);
|
||||
const env = { ...CREDENTIAL, PATH: `${join(REPO, "node_modules", ".bin")}:/nonexistent:relative:${join(p.root, "bin")}:/usr/bin:/bin` };
|
||||
assert.equal(await real.preflight({ env }), null);
|
||||
assert.equal(await real.version({ env }), "7.7.7");
|
||||
assert.equal(piEnv(env).MOSAIC_PI_BIN, p.cli);
|
||||
assert.ok(real.mounts({ env }).some(m => m.src === p.pkg));
|
||||
});
|
||||
|
||||
test("pi-unpin S2: with neither, the repository's copy is used and mounted as before", async () => {
|
||||
for (const PATH of ["/usr/bin:/bin", `${join(REPO, "node_modules", ".bin")}:/usr/bin:/bin`]) {
|
||||
const env = { ...CREDENTIAL, PATH };
|
||||
assert.equal(await real.preflight({ env }), null, PATH);
|
||||
const nm = realpathSync(join(REPO, "node_modules"));
|
||||
assert.deepEqual(real.mounts({ env }).filter(m => m.dest === join(REPO, "node_modules")).map(m => m.src), [nm]);
|
||||
assert.ok(real.installRoots({ env }).includes(nm));
|
||||
const bin = piEnv(env).MOSAIC_PI_BIN;
|
||||
assert.ok(bin.startsWith(join(REPO, "node_modules") + "/"), bin);
|
||||
assert.equal(realpathSync(bin), realpathSync(join(REPO, "node_modules", ".bin", "pi")));
|
||||
}
|
||||
});
|
||||
|
||||
test("pi-unpin S2: a pi that is not in a pi package is refused, and so is a pi that cannot be found at all", async t => {
|
||||
const dir = join(scratch(t), "bin");
|
||||
mkdirSync(dir);
|
||||
writeFileSync(join(dir, "pi"), "#!/bin/sh\necho 1.0.0\n");
|
||||
chmodSync(join(dir, "pi"), 0o755);
|
||||
let problem = await real.preflight({ env: { ...CREDENTIAL, PATH: `${dir}:/usr/bin:/bin` } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `the pi on PATH isn't part of the ${NAME} package`]);
|
||||
problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(dir, "pi") } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't part of the ${NAME} package`]);
|
||||
noPath(problem, dir);
|
||||
// The scratch copy has no node_modules of its own: no pi anywhere.
|
||||
const { root, pi } = await copyRepo(t, "#!/bin/sh\n", "1.0.0");
|
||||
rmSync(join(root, "node_modules"), { recursive: true });
|
||||
problem = await pi.preflight({ env: { ...CREDENTIAL, PATH: "/usr/bin:/bin" } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", "no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci"]);
|
||||
assert.throws(() => pi.env({ policy: { tools: [] }, tools: [], session, env: { PATH: "/usr/bin:/bin" } }), /no pi found/);
|
||||
});
|
||||
|
||||
// Review finding 8: the package directory is mounted whole, so the binary must be the pi bin its
|
||||
// package.json names. A stray executable under a directory whose package.json carries the name
|
||||
// (a wide tree, say) is refused, naming the variable only.
|
||||
test("pi-unpin S2 review: a binary that isn't the bin its package names is refused", async t => {
|
||||
const wide = scratch(t);
|
||||
writeFileSync(join(wide, "package.json"), JSON.stringify({ name: NAME, version: "1.0.0", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
mkdirSync(join(wide, "tools"));
|
||||
writeFileSync(join(wide, "tools", "pi"), "#!/bin/sh\necho 1.0.0\n", { mode: 0o755 });
|
||||
let problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(wide, "tools", "pi") } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't the pi bin the ${NAME} package names`]);
|
||||
noPath(problem, wide);
|
||||
// A package.json with no bin names none.
|
||||
writeFileSync(join(wide, "package.json"), JSON.stringify({ name: NAME, version: "1.0.0" }));
|
||||
problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(wide, "tools", "pi") } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't the pi bin the ${NAME} package names`]);
|
||||
// A bin given as a plain string names it too.
|
||||
const dir = join(scratch(t), "node_modules");
|
||||
const cli = standIn(dir, piStandIn({ version: "1.2.3" }), "1.2.3");
|
||||
writeFileSync(join(dir, NAME, "package.json"), JSON.stringify({ name: NAME, version: "1.2.3", bin: "dist/bundle/cli.js" }));
|
||||
assert.equal(await real.version({ env: { MOSAIC_PI_BIN: cli } }), "1.2.3");
|
||||
});
|
||||
@@ -48,20 +48,20 @@ test('P3 Claude preflight accepts a proxy key without a host provider credential
|
||||
|
||||
test('P3 pi refuses no-session-credential with no provider key', async () => {
|
||||
const pi = await harness('pi');
|
||||
assert.equal(pi.preflight({ env: {} })?.code, 'no-session-credential');
|
||||
assert.equal((await pi.preflight({ env: {} }))?.code, 'no-session-credential');
|
||||
});
|
||||
|
||||
test('P3 pi refuses no-session-credential with only a Claude OAuth token', async () => {
|
||||
const pi = await harness('pi');
|
||||
assert.equal(pi.preflight({ env: { CLAUDE_CODE_OAUTH_TOKEN: 'fx332-oauth' } })?.code, 'no-session-credential');
|
||||
assert.equal((await pi.preflight({ env: { CLAUDE_CODE_OAUTH_TOKEN: 'fx332-oauth' } }))?.code, 'no-session-credential');
|
||||
});
|
||||
|
||||
test('P3 pi accepts its own provider key or a session proxy key', async () => {
|
||||
const pi = await harness('pi');
|
||||
for (const name of ['ANTHROPIC_API_KEY', 'OPENAI_API_KEY', 'OPENROUTER_API_KEY', 'ZAI_API_KEY']) {
|
||||
assert.equal(pi.preflight({ env: { [name]: 'fx332-own' } }), null, name);
|
||||
assert.equal(await pi.preflight({ env: { [name]: 'fx332-own' } }), null, name);
|
||||
}
|
||||
assert.equal(pi.preflight({ env: {}, proxy: { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' } }), null);
|
||||
assert.equal(await pi.preflight({ env: {}, proxy: { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' } }), null);
|
||||
});
|
||||
|
||||
// Row 332 R2 (#1830): callers that only want pi's bundle sources call bundle() with no argument,
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
//
|
||||
// read-only /usr, /etc and the host's /bin, /lib, /lib64, /sbin links;
|
||||
// the repository files the runner loads; the adapter; for pi
|
||||
// the repository's node_modules, for Claude Code its pinned
|
||||
// the found package's directory (#1873), for Claude Code its
|
||||
// binary; node when it lives outside /usr; the broker socket,
|
||||
// the launch socket for the role that may launch, and the
|
||||
// host's git socket for a role holding git.push.working or
|
||||
@@ -41,7 +41,8 @@ import { ID_PATTERN } from "../../organization/src/vocabulary.mjs";
|
||||
|
||||
export const REPO = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..", "..");
|
||||
export const BWRAP = "/usr/bin/bwrap";
|
||||
const KILL_WITH_PARENT = ["/usr/bin/setpriv", "--pdeathsig", "KILL", "--"];
|
||||
export const SETPRIV = "/usr/bin/setpriv";
|
||||
const KILL_WITH_PARENT = [SETPRIV, "--pdeathsig", "KILL", "--"];
|
||||
|
||||
// What the runner imports, and the three files the harness starts from the
|
||||
// bundle's settings (claude-gate, mcp-server, pi-extension) with theirs.
|
||||
@@ -185,14 +186,61 @@ export function gitIdentity({ business, role }) {
|
||||
return { GIT_AUTHOR_NAME: role, GIT_AUTHOR_EMAIL: email, GIT_COMMITTER_NAME: role, GIT_COMMITTER_EMAIL: email };
|
||||
}
|
||||
|
||||
// The full command line for `command` inside the sandbox. HOME is the run's
|
||||
// home/, empty at the start; the harness makes what it needs there. The git
|
||||
// identity is the role's, for the business it runs in.
|
||||
export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWRAP }) {
|
||||
const identity = Object.entries(gitIdentity({ business, role })).flatMap(([k, v]) => ["--setenv", k, v]);
|
||||
return [bwrap, ...NAMESPACES, ...argsOf(list), "--remount-ro", "/", "--setenv", "HOME", join(runDir, "home"), "--setenv", "TMPDIR", "/tmp", ...identity, "--chdir", runDir, "--", ...KILL_WITH_PARENT, ...command];
|
||||
// The variables the sandbox sets for a session's runner, over the session env
|
||||
// the launcher hands it. HOME is the run's home/, empty at the start; the
|
||||
// harness makes what it needs there. TMPDIR is the private /tmp. The git
|
||||
// identity is the role's, for the business it runs in. #1873 S3: the pi fit
|
||||
// check gives its runs these too, so they get the names a session gets.
|
||||
export function sessionSetenv(runDir, { business, role }) {
|
||||
return { HOME: join(runDir, "home"), TMPDIR: "/tmp", ...gitIdentity({ business, role }) };
|
||||
}
|
||||
|
||||
// The full command line for `command` inside the sandbox, with sessionSetenv's variables.
|
||||
export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWRAP }) {
|
||||
const setenv = Object.entries(sessionSetenv(runDir, { business, role })).flatMap(([k, v]) => ["--setenv", k, v]);
|
||||
return [bwrap, ...NAMESPACES, ...argsOf(list), "--remount-ro", "/", ...setenv, "--chdir", runDir, "--", ...KILL_WITH_PARENT, ...command];
|
||||
}
|
||||
|
||||
// #1873 pi-unpin S3: the command line for a start-time check of a harness binary. It has the
|
||||
// system part every session gets, then `list`, with the network unshared, an empty environment
|
||||
// but `env`, and `chdir` as the working directory.
|
||||
//
|
||||
// #1873 re-check 9: a check dies with the process that runs it, unlike a session. It must not
|
||||
// outlive a bus that was killed. These steps make sure of that:
|
||||
// - setpriv gives the first process SIGKILL as its parent-death signal. A shell then checks
|
||||
// that its parent is still this process ($PPID is read after the signal is set). So either
|
||||
// the owner dies later and the signal kills the chain, or the owner is already gone and the
|
||||
// shell exits before anything starts.
|
||||
// - bwrap's --die-with-parent ties bwrap to the process before it.
|
||||
// - unshare, when this host lets it (the outer layer below), runs bwrap as pid 1 of a fresh pid
|
||||
// namespace, and --kill-child kills it when unshare dies. When pid 1 of a namespace dies, the
|
||||
// kernel kills every process in it. That includes bwrap's own child, wherever it is in its
|
||||
// setup. bwrap's --die-with-parent can't reach that child: bwrap sets the child's parent-death
|
||||
// signal only just before its exec, after the child waits for bwrap to write its uid map. If
|
||||
// bwrap dies before that write, the child waits for good.
|
||||
// The one step left open with the outer layer is unshare's own: if unshare dies after it forks
|
||||
// and before its child sets the signal, bwrap still runs. bwrap is alive then, so the run sets up
|
||||
// and goes on as normal, and it ends when the binary exits. It does not wait for good.
|
||||
//
|
||||
// #1873 re-check 10: the outer layer is best effort. unshare may be missing, older than
|
||||
// util-linux 2.36 (the first with --map-current-user), or refused: on Ubuntu 24.04 with
|
||||
// kernel.apparmor_restrict_unprivileged_userns=1 only bwrap has a profile that lets it make a
|
||||
// user namespace, so an unconfined unshare can't write its uid map. The fit check probes it once
|
||||
// per check (pi-fit.mjs) and passes unshare: null when it can't run, which drops the outer layer
|
||||
// and nothing else. That is not a weaker sandbox: the outer layer only bounds how long a check's
|
||||
// processes live. The sandbox a fit run gets is bwrap's, and bwrap's command line is the same
|
||||
// either way (pi-fit.test.mjs compares the two). Without the outer layer a stuck bwrap child can
|
||||
// outlive a killed owner; the next check stops it (sweepScratch in pi-fit.mjs).
|
||||
export const UNSHARE = "/usr/bin/unshare";
|
||||
const owned = () => [...KILL_WITH_PARENT, "/bin/sh", "-c", '[ "$PPID" = "$0" ] && exec "$@"', String(process.pid)];
|
||||
const bound = (unshare) => unshare ? [unshare, "--user", "--map-current-user", "--pid", "--fork", "--kill-child", "--"] : [];
|
||||
export function checkArgv(list, command, { env = {}, chdir, node = process.execPath, bwrap = BWRAP, unshare = UNSHARE }) {
|
||||
const setenv = Object.entries(env).flatMap(([k, v]) => ["--setenv", k, v]);
|
||||
return [...owned(), ...bound(unshare), bwrap, "--die-with-parent", ...NAMESPACES, "--unshare-net", ...argsOf([...systemBinds(node), ...list]), "--remount-ro", "/", "--clearenv", ...setenv, "--chdir", chdir, "--", ...KILL_WITH_PARENT, ...command];
|
||||
}
|
||||
// The outer layer alone, for its probe: the steps before bwrap in checkArgv, then command.
|
||||
export const outerArgv = (command, { unshare = UNSHARE } = {}) => [...owned(), ...bound(unshare), ...command];
|
||||
|
||||
// #1873 triage-fences: what a model route may hand a hosted session, checked before the host
|
||||
// writes anything and again in hostedBinds. The adapter and each mount's source must be a clean
|
||||
// absolute path whose real path lies under one of the harness module's install roots, named under
|
||||
|
||||
@@ -254,7 +254,7 @@ export const ENV_ALLOW = Object.freeze([
|
||||
"HOME", "USER", "LOGNAME", "LANG", "LC_ALL", "TZ", "TERM",
|
||||
"ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL", "CLAUDE_CODE_OAUTH_TOKEN",
|
||||
"OPENAI_API_KEY", "OPENROUTER_API_KEY", "ZAI_API_KEY",
|
||||
"MOSAIC_CLAUDE_BIN",
|
||||
"MOSAIC_CLAUDE_BIN", "MOSAIC_PI_BIN",
|
||||
]);
|
||||
|
||||
// The ways a Claude Code session can log in, each a set of variables that
|
||||
|
||||
@@ -129,7 +129,7 @@ test("row 65 a harness module's mounts bind read-only after the adapter, each wi
|
||||
const list = binds({ ...args, harnessMounts: pi.mounts({ env: {} }) });
|
||||
const at = (src) => list.findIndex((b) => b.src === src);
|
||||
const modules = list[at(realpathSync(join(REPO, "node_modules")))];
|
||||
assert.deepEqual(modules, { kind: "ro", src: realpathSync(join(REPO, "node_modules")), dest: join(REPO, "node_modules"), why: "pi and its packages, on the session's PATH" });
|
||||
assert.deepEqual(modules, { kind: "ro", src: realpathSync(join(REPO, "node_modules")), dest: join(REPO, "node_modules"), why: "pi and its packages (the repository's pi)" });
|
||||
assert.ok(at(join(root, "a.sh")) < at(modules.src) && at(modules.src) < at(join(root, "bus.sock")));
|
||||
const fixture = binds({ ...args, harnessMounts: [{ src: join(root, "tool"), why: "a fixture harness's tool" }] });
|
||||
assert.deepEqual(fixture.find((b) => b.src === join(root, "tool")), { kind: "ro", src: join(root, "tool"), why: "a fixture harness's tool" });
|
||||
|
||||
@@ -58,6 +58,15 @@ test("sessionEnv passes only the allowlist, the repo's bin on PATH, and the run
|
||||
assert.equal(sessionEnv({ MOSAIC_CLAUDE_BIN: "/opt/claude" }, "r0123456789ab").MOSAIC_CLAUDE_BIN, "/opt/claude");
|
||||
});
|
||||
|
||||
// #1873 pi-unpin S2: the host's MOSAIC_PI_BIN reaches the session, as MOSAIC_CLAUDE_BIN does, and
|
||||
// it is the only name the slice adds: every other MOSAIC_* and PI_* name from the host stays out.
|
||||
test("pi-unpin S2: sessionEnv passes MOSAIC_PI_BIN and nothing else new", () => {
|
||||
const env = sessionEnv({ MOSAIC_PI_BIN: "/opt/pi/bin/pi", PI_CODING_AGENT_DIR: "/h/.pi", PI_OFFLINE: "0", MOSAIC_PI_ROOT: "/x", PATH: "/usr/bin" }, "r0123456789ab");
|
||||
assert.deepEqual(Object.keys(env).sort(), ["MOSAIC_PI_BIN", "MOSAIC_RUN_ID", "PATH"]);
|
||||
assert.equal(env.MOSAIC_PI_BIN, "/opt/pi/bin/pi");
|
||||
assert.deepEqual(ENV_ALLOW.filter((k) => k.startsWith("MOSAIC_")), ["MOSAIC_CLAUDE_BIN", "MOSAIC_PI_BIN"]);
|
||||
});
|
||||
|
||||
test("ZAI_API_KEY passes the session allowlist, and ZAI_TOKEN does not", () => {
|
||||
assert.ok(ENV_ALLOW.includes("ZAI_API_KEY"));
|
||||
const env = sessionEnv({ ZAI_API_KEY: "fixture-zai", ZAI_TOKEN: "t" }, "r0123456789ab");
|
||||
|
||||
@@ -230,7 +230,9 @@ try {
|
||||
// own check (no symlink, a regular file this user owns, not group or world writable).
|
||||
const rideRiders = async ({ timeoutMs = 5000, deadline } = {}) => {
|
||||
const writer = organizationWriter({ dir: configDir(), rolesDir: rolesDir() });
|
||||
const local = id => readRider({ id, read: () => writer.read(id).document, readLog: () => ownedText(writer.logFile(id), { mask: 0o022 }), isFixture: orgUsesFixture });
|
||||
// settings-backend A2: `file()` is the organization file as the writer reads it ({document, revision}),
|
||||
// read again on each call, for turning a program off for every organization.
|
||||
const local = id => ({ ...readRider({ id, read: () => writer.read(id).document, readLog: () => ownedText(writer.logFile(id), { mask: 0o022 }), isFixture: orgUsesFixture }), file: () => writer.read(id) });
|
||||
const down = async () => { throw Object.assign(new Error('the bus is not reachable'), { code: 'no-bus-host' }); };
|
||||
// The single-business Console has no bus status here: its organization never reads as stopped.
|
||||
if (!organizations) return ownerCall && options.business ? [{ ...local(options.business), call: ownerCall }] : [];
|
||||
@@ -264,6 +266,8 @@ try {
|
||||
return out;
|
||||
},
|
||||
riders: rideRiders,
|
||||
// The root agents each organization takes count when a program is turned off (review P1-1).
|
||||
rootAgents: () => loadRootAgents(configDir(), { rolesDir: rolesDir() }),
|
||||
ownerModels,
|
||||
// A stopped fixture organization moves through the organization service's whole-file update, the
|
||||
// write `POST /api/orgs/ID/admin` makes, as the signed-in owner. The owner starts its bus after.
|
||||
|
||||
@@ -40,6 +40,10 @@ const ROUTES = Object.freeze({
|
||||
'/api/proxy/admin/log': ['GET', ['lines']],
|
||||
'/api/proxy/admin/service-key': ['GET', []],
|
||||
'/api/proxy/admin/history': ['GET', ['limit', 'before']],
|
||||
// #1873 settings-backend A2: one harness program off for every organization, and each
|
||||
// organization's proxy client key health. The program list itself is in status.
|
||||
'/api/proxy/admin/programs/off': ['POST', ['name']],
|
||||
'/api/proxy/admin/access': ['GET', []],
|
||||
});
|
||||
// The A1 reads take a query; every other route ignores one, as before.
|
||||
const QUERY_ROUTES = new Set(['/api/proxy/admin/log', '/api/proxy/admin/service-key', '/api/proxy/admin/history']);
|
||||
@@ -140,6 +144,10 @@ export async function proxyAdminHTTP(req, res, { path, admin, call, named, teste
|
||||
case '/api/proxy/admin/log': return done(200, await admin.log(Object.hasOwn(query, 'lines') ? { lines: query.lines } : {}));
|
||||
case '/api/proxy/admin/service-key': return done(200, await admin.serviceKey());
|
||||
case '/api/proxy/admin/history': return done(200, await admin.history({ ...(Object.hasOwn(query, 'limit') ? { limit: query.limit } : {}), before: query.before ?? null }));
|
||||
case '/api/proxy/admin/programs/off':
|
||||
if (typeof args.name !== 'string' || !/^[a-z][a-z0-9-]{0,31}$/.test(args.name)) return refuse('invalid-request');
|
||||
return done(200, await admin.programsOff({ name: args.name, user: typeof user === 'string' ? user : null }));
|
||||
case '/api/proxy/admin/access': return done(200, await admin.access());
|
||||
}
|
||||
} catch (error) {
|
||||
const code = ['human-required', 'not-configured', 'invalid-request'].includes(error?.code) ? error.code : proxyCode(error, 'outcome-unknown');
|
||||
|
||||
@@ -1978,6 +1978,9 @@
|
||||
if (answer && (navigated || path !== (answer.org ? '/inbox' : `/inbox/${answer.id}`))) answer = null;
|
||||
// Recheck N1: arriving at Models reads the model service again; a status read on an earlier visit may be out of date.
|
||||
if (navigated && path === '/settings/routing' && (typeof shown === 'string' ? shown.split('?')[0] : shown) !== path && !serviceLoading) serviceState = null;
|
||||
// settings-s5b: a focus request for the Models outcome line belongs to that page. Going anywhere else drops it,
|
||||
// including Assistant, Projects and the board, which return before the focus chain at the end of render.
|
||||
if (navigated && path !== '/settings/routing') modelsFocus = false;
|
||||
const g = ++gen, routeVersion = routesEditVersion;
|
||||
const current = () => g === gen && route === routeOf() && !out();
|
||||
// In organization mode #/ is the cross-organization dashboard; the processing controls stay on the board.
|
||||
|
||||
@@ -593,7 +593,8 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
const service = '<div id="proxy-setup-next" class="proxy-setup-next" role="status" aria-live="polite"></div>'
|
||||
+ '<ol id="proxy-setup-progress" class="progress" aria-label="Setup progress" hidden></ol>'
|
||||
+ '<p id="proxy-setup-message" role="status" aria-live="polite"></p>'
|
||||
+ '<details id="proxy-setup-lines" hidden><summary>The service\'s last lines</summary><div class="inside"><p class="small muted">This page can\'t read the service\'s lines yet. What the service said last, when the host reported it:</p><pre id="proxy-setup-log" class="setup-log">Nothing reported.</pre></div></details>'
|
||||
// settings-s5b: the lines come from GET /api/proxy/admin/log, read when the box opens (readLog in the controller).
|
||||
+ '<details id="proxy-setup-lines" class="proxy-setup-lines" hidden><summary>The service\'s last lines</summary><div class="inside" data-log></div></details>'
|
||||
+ '<div id="proxy-setup-fixtures" class="banner mk-note" hidden></div>';
|
||||
const accounts = '<p id="proxy-message" role="status" aria-live="polite">Reading accounts…</p>'
|
||||
+ '<p id="proxy-readonly" class="muted" hidden>This Console is read-only. Sign-in and account changes are unavailable.</p>'
|
||||
@@ -602,13 +603,13 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
+ '<details id="proxy-account-order"><summary>Account order</summary><div class="inside"><p class="small muted">Higher priority is used first. Credential values are never shown.</p><ul id="proxy-order" class="rows proxy-order"></ul></div></details>'
|
||||
+ '<details id="proxy-offered"><summary>Models offered</summary><div class="inside" id="proxy-offered-list"></div></details>'
|
||||
+ '<details id="proxy-add-box"><summary>Add an account</summary><div class="inside">'
|
||||
+ '<form id="proxy-login-start" class="settings-form"><fieldset data-write disabled><div class="field"><label for="proxy-provider">Provider</label>'
|
||||
+ '<select id="proxy-provider" name="provider"><option value="claude">Claude (subscription)</option><option value="codex">ChatGPT / Codex (subscription)</option><option value="z.ai">z.ai (API key)</option></select>'
|
||||
+ '<p class="help small muted">Claude and ChatGPT / Codex: you sign in on the provider\'s site in a new tab. Nothing you type there comes to this page. z.ai: you paste an API key from your z.ai account.</p></div>'
|
||||
+ '<div class="form-foot"><button type="submit" id="proxy-login-go">Sign in to Claude</button></div></fieldset></form>'
|
||||
+ '<form id="proxy-apikey" class="settings-form" hidden><fieldset data-write disabled><div class="field"><label for="proxy-apikey-key" id="proxy-apikey-label">z.ai API key</label>'
|
||||
+ '<form id="proxy-login-start" class="settings-form"><fieldset data-write disabled><div class="field"><label for="proxy-provider" hidden>Provider</label>'
|
||||
// settings-backend-a3: the options and this help text come from the plugin's provider list (setProviders).
|
||||
+ '<select id="proxy-provider" name="provider" hidden></select><p id="proxy-provider-help" class="help small muted">Reading providers…</p></div>'
|
||||
+ '<div class="form-foot"><button type="submit" id="proxy-login-go" hidden>Sign in</button></div></fieldset></form>'
|
||||
+ '<form id="proxy-apikey" class="settings-form" hidden><fieldset data-write disabled><div class="field"><label for="proxy-apikey-key" id="proxy-apikey-label">API key</label>'
|
||||
+ '<input id="proxy-apikey-key" name="key" type="password" autocomplete="off" spellcheck="false" maxlength="4096" required>'
|
||||
+ '<p class="help small muted">Mosaic checks it with z.ai, keeps it in your model service, and never shows it again.</p></div>'
|
||||
+ '<p class="help small muted">Mosaic checks it with <span id="proxy-apikey-provider">the provider</span>, keeps it in your model service, and never shows it again.</p></div>'
|
||||
+ '<div class="form-foot"><button type="submit">Check and add</button><button type="button" id="proxy-apikey-cancel">Cancel</button></div></fieldset><p id="proxy-apikey-message" role="status"></p></form>'
|
||||
+ '</div></details>'
|
||||
+ '<div id="proxy-signin" hidden><ol id="proxy-signin-progress" class="progress" aria-label="Sign-in progress"></ol>'
|
||||
@@ -626,8 +627,12 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
+ '<ul id="proxy-ride-skips" class="proxy-ride-list"></ul><ul id="proxy-ride-fixtures" class="proxy-ride-list"></ul></div>';
|
||||
const triage = '<p><a id="proxy-setup-triage" class="btn" href="#/settings/triage">Set up Triage</a></p>';
|
||||
const advanced = '<details id="proxy-setup-advanced" data-admin hidden><summary>Advanced: programs, port, timeouts</summary><div class="inside">'
|
||||
+ '<div class="settings-form"><p class="lbl">Programs that drive models</p><ul id="proxy-setup-programs" class="model-list"></ul>'
|
||||
+ '<p class="help small muted">Each organization switches these on or off on its own Models page. Claude subscription models run only in Claude Code. Turning a program off for every organization is not built yet.</p></div>'
|
||||
// settings-s5c: the installed programs and which organizations have each one on, from the status
|
||||
// read; one program can be turned off in every organization that can do without it.
|
||||
+ '<div class="settings-form proxy-programs"><h4 id="proxy-setup-programs-title" class="lbl">Programs that drive models</h4>'
|
||||
+ '<p class="help small muted">Each organization turns these on or off on its own Models page. Claude subscription models run only in Claude Code. Turn off for every organization skips an organization that still uses the program or has no other program on.</p>'
|
||||
+ '<ul id="proxy-setup-programs" class="proxy-programs-list" aria-labelledby="proxy-setup-programs-title"><li class="muted">Not read yet.</li></ul>'
|
||||
+ '<div id="proxy-setup-programs-result" class="proxy-programs-result" tabindex="-1" hidden></div></div>'
|
||||
+ '<form id="proxy-setup-init" class="settings-form"><fieldset data-setup-write disabled><legend>Service key</legend>'
|
||||
+ '<p class="small muted">Mosaic makes the service key on this machine, keeps it where setup keeps keys and never shows it.</p></fieldset>'
|
||||
+ '<fieldset data-setup-write disabled><legend>Settings (leave empty for the defaults)</legend>'
|
||||
@@ -637,12 +642,24 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
+ '<dl id="proxy-setup-settings" class="proxy-setup-state" hidden></dl><p id="proxy-setup-settings-note" class="small muted" hidden>Changing these after setup is not built yet.</p></div></details>';
|
||||
const tech = '<details id="proxy-setup-tech" class="proxy-setup-tech"><summary>Technical details: release, service, history</summary><div class="inside">'
|
||||
+ '<div data-admin hidden><dl id="proxy-setup-state" class="proxy-setup-state"></dl>'
|
||||
// settings-s5b: the setup history and the service's lines, read when they open (owner only). The service key is the last
|
||||
// row of the list above (paintState), and the history follows it, as models-advanced shows them.
|
||||
+ '<div class="proxy-setup-history"><h4 id="proxy-setup-history-title">History</h4><p id="proxy-setup-history-message" class="small muted" role="status">Not read yet.</p>'
|
||||
+ '<ol id="proxy-setup-history" aria-labelledby="proxy-setup-history-title"></ol>'
|
||||
+ '<p class="proxy-setup-history-foot"><button type="button" id="proxy-setup-history-all" aria-expanded="false" aria-controls="proxy-setup-history" hidden>Show all</button>'
|
||||
+ '<button type="button" id="proxy-setup-history-older" hidden>Show older</button><button type="button" id="proxy-setup-history-retry" hidden>Read again</button></p></div>'
|
||||
// settings-s5c: access health per organization (GET /api/proxy/admin/access), read when this box opens.
|
||||
+ '<div class="proxy-setup-access"><h4 id="proxy-setup-access-title">Access</h4><p class="small muted">Whether each organization\'s agents can reach your models. You never see a key.</p>'
|
||||
+ '<p id="proxy-setup-access-message" class="small muted" role="status">Not read yet.</p>'
|
||||
+ '<ul id="proxy-setup-access" class="rows proxy-access" aria-labelledby="proxy-setup-access-title"></ul>'
|
||||
+ '<p class="proxy-setup-history-foot"><button type="button" id="proxy-setup-access-retry" hidden>Read again</button></p></div>'
|
||||
+ '<details id="proxy-setup-tech-lines" class="proxy-setup-lines"><summary>The service\'s last lines</summary><div class="inside" data-log></div></details>'
|
||||
+ '<div class="proxy-setup-unit"><h4>Service controls</h4><p class="small muted">Install renders the unit the same way as <code>mosaic proxy unit install</code>.</p>'
|
||||
+ '<div class="proxy-setup-buttons"><button type="button" data-unit="install" data-setup-write disabled>Install</button><button type="button" data-unit="start" data-setup-write disabled>Start</button><button type="button" data-unit="restart" data-setup-write disabled>Restart</button><button type="button" data-unit="stop" data-setup-write disabled>Stop</button><button type="button" id="proxy-setup-render">Show unit file</button></div>'
|
||||
+ '<pre id="proxy-setup-unit-file" hidden></pre></div>'
|
||||
+ '<div class="proxy-setup-orgs"><h4>Organizations</h4><div class="table-wrap" tabindex="0" role="region" aria-label="Organizations using the model service"><table><thead><tr><th>Organization</th><th>Proxy</th><th>Catalogue</th><th>Models</th></tr></thead><tbody id="proxy-setup-orgs"></tbody></table></div>'
|
||||
+ '<p id="proxy-setup-own" class="muted"></p></div>'
|
||||
+ '<div class="proxy-setup-receipts"><h4>History</h4><ol id="proxy-setup-receipts"><li class="muted">No setup actions on this page yet.</li></ol></div></div>'
|
||||
+ '</div>'
|
||||
+ '</div></details>';
|
||||
const markup = '<div id="proxy-setup" class="model-setup">'
|
||||
+ card('service', 1, 'Model service', service, true)
|
||||
@@ -670,7 +687,8 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
const cell = value => { const td = document.createElement('td'); td.textContent = value ?? '—'; return td; };
|
||||
const el = (tag, text, className) => { const e = document.createElement(tag); if (text !== undefined) e.textContent = text; if (className) e.className = className; return e; };
|
||||
// The provider select is this page's one list of providers: rows and buttons take their names
|
||||
// and kinds (subscription or API key) from its option labels.
|
||||
// and kinds (subscription or API key) from its option labels. Its options come from the plugin's
|
||||
// list (setProviders), read with the setup status for the instance owner, with the accounts for anyone else.
|
||||
const providerSelect = $('proxy-provider');
|
||||
const providerLabel = p => [...providerSelect.options].find(o => o.value === p)?.textContent ?? String(p ?? 'Unknown');
|
||||
const providerShort = p => providerLabel(p).replace(/\s*\([^)]*\)$/, '');
|
||||
@@ -784,6 +802,7 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
}
|
||||
async function refresh() {
|
||||
const accounts = await call('accounts');
|
||||
if (!proxyAdmin) setProviders(accounts?.providers);
|
||||
accountList = Array.isArray(accounts?.accounts) ? accounts.accounts : [];
|
||||
accountsDown = null;
|
||||
paintAccounts(); paintOrder();
|
||||
@@ -830,9 +849,28 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
$('proxy-offered').addEventListener('toggle', () => { if ($('proxy-offered').open) readOffered(); });
|
||||
// Add an account: the provider picks a sign-in or an API key.
|
||||
function paintProvider() {
|
||||
const p = providerSelect.value, apikey = keyed(p);
|
||||
const go = $('proxy-login-go'); go.textContent = `Sign in to ${providerShort(p)}`; go.hidden = apikey;
|
||||
const p = providerSelect.value, apikey = Boolean(p) && keyed(p);
|
||||
const go = $('proxy-login-go'); go.textContent = `Sign in to ${providerShort(p)}`; go.hidden = apikey || !p;
|
||||
$('proxy-apikey').hidden = !apikey; $('proxy-apikey-label').textContent = `${providerShort(p)} API key`;
|
||||
$('proxy-apikey-provider').textContent = providerShort(p);
|
||||
}
|
||||
// settings-backend-a3: no list, or an empty one, leaves no select and one line saying so.
|
||||
let providerKey = null;
|
||||
function setProviders(list) {
|
||||
const items = Array.isArray(list) ? list.filter(p => typeof p?.id === 'string' && p.id && typeof p.label === 'string' && ['oauth', 'apikey'].includes(p.signIn)) : [];
|
||||
const key = JSON.stringify(items.map(p => [p.id, p.label, p.signIn]));
|
||||
if (key === providerKey) return; providerKey = key;
|
||||
const kept = providerSelect.value;
|
||||
providerSelect.replaceChildren(...items.map(p => el('option', `${p.label} (${p.signIn === 'oauth' ? 'subscription' : 'API key'})`)));
|
||||
items.forEach((p, i) => { providerSelect.options[i].value = p.id; });
|
||||
if (items.some(p => p.id === kept)) providerSelect.value = kept;
|
||||
providerSelect.hidden = !items.length; for (const label of providerSelect.labels) label.hidden = !items.length;
|
||||
const oauth = items.filter(p => p.signIn === 'oauth').map(p => p.label);
|
||||
$('proxy-provider-help').textContent = !items.length ? 'The providers could not be read, so no account can be added here.'
|
||||
: [oauth.length ? `${new Intl.ListFormat('en').format(oauth)}: you sign in on the provider's site in a new tab. Nothing you type there comes to this page.` : '',
|
||||
...items.filter(p => p.signIn === 'apikey').map(p => `${p.label}: you paste an API key from your ${p.label} account.`)].filter(Boolean).join(' ');
|
||||
paintProvider();
|
||||
if (accountList) { paintAccounts(); paintOrder(); }
|
||||
}
|
||||
providerSelect.addEventListener('change', paintProvider);
|
||||
function paintSignin() {
|
||||
@@ -945,7 +983,7 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
'not-configured': 'Model service setup is not available on this Console.',
|
||||
'outcome-unknown': 'The outcome is unknown. Check the status before retrying.',
|
||||
};
|
||||
const explain = code => `${REASONS[code] ?? 'The action was refused.'} (${code})`;
|
||||
const explain = code => `${Object.hasOwn(REASONS, code) ? REASONS[code] : 'The action was refused.'} (${code})`;
|
||||
async function admin(path, args) {
|
||||
const response = await fetch('/api/proxy/admin/' + path, { cache: 'no-store', credentials: 'same-origin', redirect: 'manual',
|
||||
method: args === undefined ? 'GET' : 'POST',
|
||||
@@ -957,19 +995,160 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
}
|
||||
const say = (text, kind) => receipt('proxy-setup-message', text, kind);
|
||||
const human = value => value === null || value === undefined ? '—' : typeof value === 'object' ? JSON.stringify(value) : String(value);
|
||||
function addReceipt(record) {
|
||||
if (!record || typeof record !== 'object') return;
|
||||
const list = $('proxy-setup-receipts'); list.querySelector('li.muted')?.remove();
|
||||
const li = document.createElement('li'), op = document.createElement('strong'); op.textContent = record.op ?? 'action';
|
||||
const details = Object.entries(record).filter(([k]) => k !== 'op' && k !== 'time').map(([k, v]) => `${k}: ${human(v)}`).join(', ');
|
||||
li.append(op, ` ${window.MosaicLocalTime(record.time)}${details ? ' — ' + details : ''}`);
|
||||
list.prepend(li);
|
||||
// settings-s5b: History, the service key and the service's lines come from their own reads
|
||||
// (GET /api/proxy/admin/history, service-key and log). Nothing reads them until their box opens,
|
||||
// and only the instance owner's page starts this controller. A write's receipt reads them again
|
||||
// when Technical details is open, so the page never shows a raw receipt.
|
||||
const techOpen = () => $('proxy-setup-tech')?.open === true;
|
||||
function addReceipt() { if (enabled && techOpen()) { readKey(); readHistory(true); } }
|
||||
const plural = (n, word) => `${n} ${word}${n === 1 ? '' : 's'}`;
|
||||
const count = n => Number.isInteger(n) && n > 0 ? n : 0;
|
||||
// A failed read says what the answer's code means here; a request that got no answer says so. A code
|
||||
// shaped like a Mosaic key is not shown, even though it passes admin()'s letters-and-hyphens check.
|
||||
const failText = (error, words) => !error?.code ? (error instanceof TypeError ? 'The Console did not answer.' : String(error?.message ?? 'The Console did not answer.'))
|
||||
: /ms[kotp][-_]/i.test(error.code) ? 'The Console refused the read.' : Object.hasOwn(words, error.code) ? `${words[error.code]} (${error.code})` : error.message;
|
||||
const retryButton = (id, action) => { const button = el('button', 'Read again'); button.type = 'button'; if (id) button.id = id; button.addEventListener('click', action); return button; };
|
||||
// The service's last lines: one read paints every box. The lines show as the answer has them:
|
||||
// the route removes key-shaped text; this page adds nothing and takes nothing away.
|
||||
const LOG_WORDS = { 'not-configured': 'The model service is not installed, so it has no lines yet.', 'outcome-unknown': 'The host could not read the service\'s lines.', 'invalid-request': 'The Console refused the request.' };
|
||||
let logRound = 0, logAnswer = null;
|
||||
function paintLog() {
|
||||
for (const box of root.querySelectorAll('[data-log]')) {
|
||||
if (!logAnswer) { box.replaceChildren(el('p', 'Reading the service\'s lines…', 'small muted')); continue; }
|
||||
// An uninstalled service has no lines: a plain line, not a fault.
|
||||
if (logAnswer.none) { box.replaceChildren(el('p', LOG_WORDS['not-configured'], 'small muted'), retryButton(null, readLog)); continue; }
|
||||
if (logAnswer.error) { box.replaceChildren(el('p', `The service's lines could not be read. ${logAnswer.error}`, 'banner err'), retryButton(null, readLog)); continue; }
|
||||
const { lines, redacted, dropped } = logAnswer, notes = ['Mosaic removes anything that looks like a key.'];
|
||||
if (redacted) notes.push(`It removed something from ${plural(redacted, 'line')}.`);
|
||||
if (dropped) notes.push(`${plural(dropped, 'line')} ${dropped === 1 ? 'was' : 'were'} left out.`);
|
||||
const parts = [el('p', notes.join(' '), 'small muted')];
|
||||
parts.push(lines.length ? el('pre', lines.join('\n'), 'setup-log') : el('p', dropped ? 'No line is left to show.' : 'The service has written nothing yet.', 'small muted'));
|
||||
parts.push(retryButton(null, readLog));
|
||||
box.replaceChildren(...parts);
|
||||
}
|
||||
}
|
||||
// #1873: receipts survive a reload: status carries the last few from proxy-admin.jsonl.
|
||||
function paintReceipts() {
|
||||
if (!Array.isArray(state.receipts) || !state.receipts.length) return;
|
||||
$('proxy-setup-receipts').replaceChildren(); state.receipts.forEach(addReceipt);
|
||||
async function readLog() {
|
||||
if (!enabled) return;
|
||||
// A Read again pressed in a box keeps focus there once the answer is painted.
|
||||
const from = [...root.querySelectorAll('[data-log]')].find(box => box.contains(document.activeElement));
|
||||
const round = ++logRound; logAnswer = null; paintLog();
|
||||
let answer;
|
||||
try {
|
||||
const value = await admin('log?lines=20');
|
||||
if (!value || !Array.isArray(value.lines)) throw Object.assign(new Error('The answer could not be read.'), { code: 'outcome-unknown' });
|
||||
answer = { lines: value.lines.filter(line => typeof line === 'string'), redacted: count(value.redacted), dropped: count(value.dropped) };
|
||||
} catch (error) { answer = error?.code === 'not-configured' ? { none: true } : { error: failText(error, LOG_WORDS) }; }
|
||||
if (round !== logRound || !enabled) return;
|
||||
logAnswer = answer; paintLog();
|
||||
from?.querySelector('button')?.focus();
|
||||
}
|
||||
// When the service key was made. Never the key: the answer has no key, and this shows only its time.
|
||||
// It is the last row of the state list; paintState redraws that list, so the row paints from keyView.
|
||||
const KEY_WORDS = { 'not-configured': 'Model service setup is not available on this Console.', 'outcome-unknown': 'The host could not check the key.' };
|
||||
let keyRound = 0, keyView = { text: 'Not read yet.', failed: false };
|
||||
function paintKey() {
|
||||
const dd = $('proxy-setup-keymade-text')?.parentElement; if (!dd) return;
|
||||
const line = el(keyView.failed ? 'p' : 'span', keyView.text, keyView.failed ? 'banner err' : keyView.reading ? 'muted' : undefined); line.id = 'proxy-setup-keymade-text';
|
||||
dd.replaceChildren(line, ...(keyView.failed ? [retryButton('proxy-setup-keymade-retry', readKey)] : []));
|
||||
}
|
||||
function keyRow(dl) {
|
||||
const dt = document.createElement('dt'), dd = document.createElement('dd'), span = el('span'); span.id = 'proxy-setup-keymade-text';
|
||||
dt.textContent = 'Service key'; dd.append(span); dl.append(dt, dd); paintKey();
|
||||
}
|
||||
async function readKey() {
|
||||
if (!enabled) return;
|
||||
const round = ++keyRound, refocus = document.activeElement?.id === 'proxy-setup-keymade-retry';
|
||||
keyView = { text: 'Reading…', failed: false, reading: true }; paintKey();
|
||||
let text, failed = false;
|
||||
try {
|
||||
const value = await admin('service-key');
|
||||
if (!value || typeof value !== 'object') throw Object.assign(new Error('The answer could not be read.'), { code: 'outcome-unknown' });
|
||||
const renewed = value.renewal !== 'not-built' && Number.isFinite(Date.parse(value.renewedAt)) ? ` Last renewed ${window.MosaicLocalTime(value.renewedAt)}.` : ' Renewing it is not built yet.';
|
||||
if (value.problem === 'missing') text = 'Not made yet. Set up model service in step 1 makes it. You never see it.';
|
||||
else if (value.problem === 'unreadable') text = 'Mosaic can\'t tell when it was made: the service\'s settings file is not one it can check. You never see the key.';
|
||||
else if (Number.isFinite(Date.parse(value.madeAt))) text = `Mosaic made it on ${window.MosaicLocalTime(value.madeAt)}. You never see it.${renewed}`;
|
||||
else text = `When it was made is not known. You never see it.${renewed}`;
|
||||
} catch (error) { failed = true; text = `When it was made could not be read. ${failText(error, KEY_WORDS)}`; }
|
||||
if (round !== keyRound || !enabled) return;
|
||||
keyView = { text, failed }; paintKey();
|
||||
if (refocus) { if (failed) $('proxy-setup-keymade-retry')?.focus(); else { const line = $('proxy-setup-keymade-text'); line.tabIndex = -1; line.focus(); } }
|
||||
}
|
||||
// Setup history, a page at a time. The first page holds the newest entries, oldest first within
|
||||
// it; the list shows newest first. A page can be empty and still name an older one.
|
||||
const HISTORY_WORDS = { 'not-configured': 'Model service setup is not available on this Console.', 'outcome-unknown': 'The host could not read the history.', 'invalid-request': 'The Console refused that page.' };
|
||||
const FEW = 3;
|
||||
const hist = { entries: [], next: null, loading: false, expanded: false, error: null, emptyPage: false, round: 0, read: false };
|
||||
const orgName = id => { const label = typeof id === 'string' ? window.MosaicOrg?.row?.(id)?.label : null; return typeof label === 'string' && label ? label : null; };
|
||||
const providerOf = p => typeof p === 'string' && [...providerSelect.options].some(o => o.value === p) ? providerShort(p) : null;
|
||||
// A name shown as written, unless it holds text shaped like a Mosaic key.
|
||||
const shownName = v => typeof v === 'string' && v && v.length <= 64 && !/ms[kotp][-_]/i.test(v) ? v : null;
|
||||
function historyText(entry) {
|
||||
if (!entry || typeof entry !== 'object' || entry.withheld === true) return null;
|
||||
const org = orgName(entry.org), provider = providerOf(entry.provider), some = org ?? 'An organization';
|
||||
const n = (v, word) => Number.isInteger(v) && v >= 0 ? plural(v, word) : null;
|
||||
const labels = {
|
||||
'account.proxy.apikey.add': provider ? `${provider} account added` : 'Account added',
|
||||
'account.proxy.login.complete': 'Signed in to an account',
|
||||
'account.proxy.priority.set': Number.isInteger(entry.priority) ? `Account order changed, priority ${entry.priority}` : 'Account order changed',
|
||||
'account.proxy.group.models.set': n(entry.models, 'model') ? `Models chosen for an account, ${n(entry.models, 'model')}` : 'Models chosen for an account',
|
||||
fetch: 'Release downloaded and checked', init: 'Set up', 'init-lock.clear': 'Stale setup lock cleared', 'fence.clear': 'Key writes resumed',
|
||||
'unit.install': 'Service installed', 'unit.start': 'Started', 'unit.stop': 'Stopped', 'unit.restart': 'Restarted',
|
||||
'ride-all': 'Organizations switched to your models', 'ride-move': `${some} switched to your models`, 'ride-intent': `Switching ${org ?? 'an organization'} to your models`,
|
||||
'ride-not-added': `${some} was not switched to your models`, 'ride-removed': `${some} stopped using your models`,
|
||||
};
|
||||
const label = typeof entry.op === 'string' && Object.hasOwn(labels, entry.op) ? labels[entry.op] : 'Other setup action';
|
||||
const inOrg = org && String(entry.op).startsWith('account.') ? ` in ${org}` : '';
|
||||
const user = entry.user === '[redacted]' ? 'someone whose name Mosaic hid' : shownName(entry.user);
|
||||
return `${label}${inOrg} · ${window.MosaicLocalTime(entry.time)}${user ? ` · by ${user}` : ''}`;
|
||||
}
|
||||
function paintHistory() {
|
||||
const list = $('proxy-setup-history'), msg = $('proxy-setup-history-message'), all = $('proxy-setup-history-all'), older = $('proxy-setup-history-older'), retry = $('proxy-setup-history-retry');
|
||||
list.replaceChildren(...hist.entries.map((entry, i) => {
|
||||
const text = historyText(entry), li = el('li', text ?? 'Withheld. The Console kept this entry back because it may hold a secret.', text ? undefined : 'muted');
|
||||
if (!text) li.dataset.withheld = '';
|
||||
li.hidden = !hist.expanded && i >= FEW; return li;
|
||||
}));
|
||||
const more = hist.next !== null;
|
||||
// Show all only when some entries are folded away; Show older whenever there is an older page and
|
||||
// nothing read is folded away, so an empty page offers it at once. After an empty page it stays while
|
||||
// the list is folded, since the line below names it.
|
||||
all.hidden = !hist.read || hist.entries.length <= FEW;
|
||||
all.textContent = hist.expanded ? 'Show fewer' : 'Show all'; all.setAttribute('aria-expanded', String(hist.expanded));
|
||||
older.hidden = !more || Boolean(hist.error) || (!hist.expanded && hist.entries.length > FEW && !hist.emptyPage);
|
||||
older.disabled = hist.loading; older.textContent = hist.loading ? 'Reading…' : 'Show older';
|
||||
retry.hidden = !hist.error;
|
||||
msg.className = hist.error ? 'banner err' : 'small muted';
|
||||
msg.textContent = hist.error ? `History could not be read. ${hist.error}`
|
||||
: hist.loading && !hist.entries.length ? 'Reading…'
|
||||
: !hist.read ? 'Not read yet.'
|
||||
: hist.emptyPage && more ? 'That page of history had no entries to show. Show older reads further back.'
|
||||
: !hist.entries.length && !more ? 'No setup history yet.' : '';
|
||||
}
|
||||
async function readHistory(reset) {
|
||||
if (!enabled || (hist.loading && !reset)) return;
|
||||
if (reset) Object.assign(hist, { entries: [], next: null, expanded: false, read: false, emptyPage: false });
|
||||
const round = ++hist.round, before = hist.next, from = ['proxy-setup-history-older', 'proxy-setup-history-retry'].find(id => document.activeElement?.id === id);
|
||||
Object.assign(hist, { loading: true, error: null }); paintHistory();
|
||||
try {
|
||||
const value = await admin(`history?limit=25${before === null ? '' : `&before=${encodeURIComponent(before)}`}`);
|
||||
const next = value?.next;
|
||||
// An older page is further back than the one asked for; one that is not would repeat entries.
|
||||
if (!value || !Array.isArray(value.entries) || !(next === null || (Number.isInteger(next) && next >= 0 && (before === null || next < before)))) throw Object.assign(new Error('The answer could not be read.'), { code: 'outcome-unknown' });
|
||||
if (round !== hist.round || !enabled) return;
|
||||
hist.entries.push(...value.entries.slice().reverse());
|
||||
Object.assign(hist, { next, read: true, emptyPage: value.entries.length === 0 });
|
||||
} catch (error) { if (round !== hist.round || !enabled) return; hist.error = failText(error, HISTORY_WORDS); }
|
||||
hist.loading = false; paintHistory();
|
||||
// The button pressed keeps focus while it stays; when it goes, focus lands on Read again, Show older,
|
||||
// Show fewer or the heading, in that order.
|
||||
if (from) { const back = [$(from), $('proxy-setup-history-retry'), $('proxy-setup-history-older'), $('proxy-setup-history-all')].find(b => !b.hidden); if (back) back.focus(); else { const h = $('proxy-setup-history-title'); h.tabIndex = -1; h.focus(); } }
|
||||
}
|
||||
$('proxy-setup-history-all').addEventListener('click', () => { hist.expanded = !hist.expanded; paintHistory(); });
|
||||
// Show older shows what it reads: the list stays open from then on.
|
||||
$('proxy-setup-history-older').addEventListener('click', () => { if (!hist.loading && hist.next !== null) { hist.expanded = true; readHistory(false); } });
|
||||
$('proxy-setup-history-retry').addEventListener('click', () => readHistory(!hist.read));
|
||||
$('proxy-setup-tech').addEventListener('toggle', () => { if (techOpen() && enabled) { readKey(); readHistory(true); readAccess(); } });
|
||||
$('proxy-setup-tech-lines').addEventListener('toggle', () => { if ($('proxy-setup-tech-lines').open && enabled) readLog(); });
|
||||
function row(dl, term, text) { const dt = document.createElement('dt'), dd = document.createElement('dd'); dt.textContent = term; dd.textContent = text; dl.append(dt, dd); }
|
||||
function paintState() {
|
||||
// #1873 settings slice 5: the timeouts init chose, from the same read. Null is not set in
|
||||
@@ -983,6 +1162,7 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
row(dl, 'Fence', !fence.set ? 'Clear' : fence.invalid ? 'Set (unreadable fence file)' : `Set after ${fence.op ?? 'a key write'}${fence.org ? ` for ${fence.org}` : ''}${fence.time ? ` at ${window.MosaicLocalTime(fence.time)}` : ''}`);
|
||||
row(dl, 'Init lock', !initLock.present ? 'None' : initLock.clearable ? 'Stale: the bus is stopped and no model service files exist' : `Held: ${initLock.blockers.join('; ')}`);
|
||||
if (state.restart?.confirmed) row(dl, 'Last restart', `Confirmed at ${window.MosaicLocalTime(state.restart.observedAt)}, new pid ${state.restart.pid}`);
|
||||
if (enabled) keyRow(dl);
|
||||
}
|
||||
// settings-p3 (re-check 3 P3-1): where the hand steps for a held init lock or a partial init are written.
|
||||
const README_STEPS = ['The hand steps for a stale init lock and a partial init are in ', 'packages/model-proxy/README.md'];
|
||||
@@ -1034,8 +1214,8 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
if (failed && (CHAIN.includes(next) || next === 'unit-restart')) {
|
||||
setCard('service', 'bad', `${failed.reason} The steps that finished are kept. No organization was touched.`, { badge: ['badge-danger', 'Setup stopped'] });
|
||||
box.replaceChildren(el('p', 'Try again starts from the step that failed.', 'small muted'), goButton('Try again', next, !sendOk || Boolean(state.busy), runChain));
|
||||
paintProgress(true); lines.hidden = false; lines.open = true;
|
||||
$('proxy-setup-log').textContent = state.unit?.error ? String(state.unit.error) : 'Nothing reported.';
|
||||
paintProgress(true);
|
||||
if (lines.hidden) { lines.hidden = false; lines.open = true; readLog(); }
|
||||
return;
|
||||
}
|
||||
lines.hidden = true;
|
||||
@@ -1218,18 +1398,206 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
if (s.rotateGraceMinutes !== undefined) row(dl, 'Old key kept after a new one', s.rotateGraceMinutes === null ? 'The default' : `${human(s.rotateGraceMinutes)} minutes`);
|
||||
}
|
||||
}
|
||||
// Programs that drive models: the harnesses this Console offers an organization, read on open.
|
||||
let programsRead = false;
|
||||
$('proxy-setup-advanced').addEventListener('toggle', async () => {
|
||||
if (!$('proxy-setup-advanced').open || programsRead) return; programsRead = true;
|
||||
const list = $('proxy-setup-programs'); list.replaceChildren(el('li', 'Reading…', 'muted'));
|
||||
// settings-s5c: Programs that drive models, from the status read's programs field (owner only: only
|
||||
// the owner's page reads the status). A list the read could not give reads as not known, never as off.
|
||||
// Program names come from the read; this page holds no list of them.
|
||||
const KEYISH = /ms[kotp][-_]/i;
|
||||
const programName = v => typeof v === 'string' && /^[a-z][a-z0-9-]{0,31}$/.test(v) && !KEYISH.test(v) ? v : null;
|
||||
const orgLabel = id => shownName(orgName(id)) ?? shownName(id) ?? 'An organization Mosaic can\'t name';
|
||||
let offAsk = null, offBusy = false, offView = null, offFocus = null;
|
||||
function programsState() {
|
||||
const p = state?.programs;
|
||||
if (!p || typeof p !== 'object') return { error: 'The programs could not be read.' };
|
||||
if (!Array.isArray(p.installed)) return { error: 'The installed programs could not be read.' };
|
||||
const names = [...new Set(p.installed.map(programName).filter(Boolean))], hidden = p.installed.some(n => !programName(n));
|
||||
const orgs = Array.isArray(p.organizations) ? p.organizations.filter(o => o && typeof o === 'object') : null;
|
||||
return { names, hidden, orgs };
|
||||
}
|
||||
function programLine(name, orgs) {
|
||||
if (!orgs) return 'Which organizations have it on could not be read.';
|
||||
if (!orgs.length) return 'No organization to check.';
|
||||
const on = [], off = [], unknown = [];
|
||||
for (const o of orgs) {
|
||||
const label = orgLabel(o.id);
|
||||
if (!Array.isArray(o.on)) unknown.push(label); else if (o.on.includes(name)) on.push(label); else off.push(label);
|
||||
}
|
||||
const parts = [];
|
||||
parts.push(on.length ? `On in ${listNames(on)}.` : 'On in no organization.');
|
||||
if (off.length) parts.push(`Off in ${listNames(off)}.`);
|
||||
if (unknown.length) parts.push(`Not known in ${listNames(unknown)}: ${unknown.length === 1 ? 'its list' : 'their lists'} could not be read.`);
|
||||
return parts.join(' ');
|
||||
}
|
||||
function paintPrograms() {
|
||||
const list = $('proxy-setup-programs'), had = document.activeElement;
|
||||
// A repaint while a control here holds focus puts focus back on the same control.
|
||||
const keep = had && list.contains(had) ? (had.id ? '#' + had.id : had.dataset.programOff ? `[data-program-off="${had.dataset.programOff}"]` : null) : null;
|
||||
const view = programsState();
|
||||
// A read that no longer lists the program being asked about drops the question.
|
||||
if (offAsk !== null && (view.error || !view.names.includes(offAsk))) offAsk = null;
|
||||
if (view.error) { list.replaceChildren(el('li', view.error, 'muted')); return; }
|
||||
const items = view.names.map(name => {
|
||||
const li = el('li'); li.dataset.program = name;
|
||||
const head = el('div', '', 'proxy-program-head');
|
||||
head.append(el('span', name, 'proxy-program-name'));
|
||||
const off = el('button', 'Turn off for every organization'); off.type = 'button'; off.dataset.programOff = name;
|
||||
off.setAttribute('aria-label', `Turn off ${name} for every organization`);
|
||||
off.disabled = !sendOk || offBusy || offAsk !== null;
|
||||
off.addEventListener('click', () => askOff(name));
|
||||
head.append(off); li.append(head, el('p', programLine(name, view.orgs), 'small proxy-program-orgs'));
|
||||
if (offAsk === name) {
|
||||
const box = el('div', '', 'proxy-program-confirm'); box.setAttribute('role', 'group'); box.setAttribute('aria-labelledby', 'proxy-setup-programs-ask');
|
||||
const ask = el('p', `Turn off ${name} in every organization? Mosaic skips an organization where something still uses ${name} or where it is the only program on. Turning it on again is done on each organization's Models page.`);
|
||||
ask.id = 'proxy-setup-programs-ask';
|
||||
const yes = el('button', `Turn off ${name}`, 'danger'); yes.type = 'button'; yes.id = 'proxy-setup-programs-yes'; yes.disabled = !sendOk || offBusy;
|
||||
yes.addEventListener('click', () => programsOff(name));
|
||||
const no = el('button', 'Cancel'); no.type = 'button'; no.id = 'proxy-setup-programs-no';
|
||||
no.addEventListener('click', () => { offAsk = null; paintPrograms(); root.querySelector(`[data-program-off="${name}"]`)?.focus(); });
|
||||
const foot = el('p', '', 'proxy-setup-history-foot'); foot.append(yes, no);
|
||||
box.append(ask, foot); li.append(box);
|
||||
}
|
||||
return li;
|
||||
});
|
||||
if (!view.names.length) items.push(el('li', 'No programs are installed.', 'muted'));
|
||||
if (view.hidden) items.push(el('li', 'One or more programs have a name this page does not show.', 'muted'));
|
||||
list.replaceChildren(...items);
|
||||
if (keep) root.querySelector(keep)?.focus();
|
||||
}
|
||||
function askOff(name) {
|
||||
if (!sendOk || offBusy) return;
|
||||
offAsk = name; paintPrograms(); $('proxy-setup-programs-yes')?.focus();
|
||||
}
|
||||
// What each organization's outcome means, in plain words (settings-backend-a2 build report).
|
||||
function offOutcome(outcome) {
|
||||
const words = {
|
||||
off: 'Turned off.',
|
||||
'already-off': 'It was already off. Nothing changed.',
|
||||
'in-use': 'Kept on. Something there still uses it: a role route, a fallback or an agent.',
|
||||
'last-program': 'Kept on. It is the only program on there.',
|
||||
'bus-down': 'Not changed. Its programs could not be read.',
|
||||
unreadable: 'Not changed. Something Mosaic needed to check could not be read.',
|
||||
'failed:revision-conflict': 'Not changed. It changed while Mosaic was checking. Try again.',
|
||||
'failed:outcome-unknown': 'May have changed. The change did not answer in time.',
|
||||
};
|
||||
if (typeof outcome !== 'string') return 'Not known. The answer did not say what happened.';
|
||||
if (Object.hasOwn(words, outcome)) return words[outcome];
|
||||
if (outcome.startsWith('failed:')) {
|
||||
const code = outcome.slice(7);
|
||||
return /^[a-z0-9][a-z0-9-]{0,63}$/.test(code) && !KEYISH.test(code) ? `Not changed. It refused the change (${code}).` : 'Not changed. It refused the change, with a code this page does not show.';
|
||||
}
|
||||
return 'Not known. The answer did not say what happened.';
|
||||
}
|
||||
const OFF_WORDS = {
|
||||
'invalid-request': 'The Console refused the request: the program is not in its installed list, or that list could not be read.',
|
||||
'human-required': 'Only the instance owner can turn a program off.',
|
||||
'proxy-admin-busy': 'Another setup action is running. Wait for it to finish, then try again.',
|
||||
};
|
||||
function paintOffResult() {
|
||||
const box = $('proxy-setup-programs-result');
|
||||
if (!offView) { box.hidden = true; box.replaceChildren(); return; }
|
||||
box.hidden = false;
|
||||
if (offView.working) { box.className = 'proxy-programs-result'; box.replaceChildren(el('p', `Turning off ${offView.name}…`, 'small muted')); return; }
|
||||
// Whether the list above was read again after the answer: it says so, and never claims a list it did not get.
|
||||
const reread = () => {
|
||||
if (offView.reread === 'ok') return [el('p', 'The list above is read again and shows what is on now.', 'small proxy-programs-reread')];
|
||||
if (offView.reread === 'failed') {
|
||||
const foot = el('p', '', 'proxy-setup-history-foot'); foot.append(retryButton('proxy-setup-programs-reread', () => rereadPrograms(true)));
|
||||
return [el('p', 'The list above could not be read again, so it may be out of date.', 'small proxy-programs-reread'), foot];
|
||||
}
|
||||
return [el('p', 'Reading the list again…', 'small muted proxy-programs-reread')];
|
||||
};
|
||||
if (offView.line) { box.className = `proxy-programs-result banner ${offView.kind === 'busy' ? 'ref' : 'err'}`; box.replaceChildren(el('p', offView.line), ...reread()); return; }
|
||||
box.className = 'proxy-programs-result banner';
|
||||
const title = el('p', `Turn off ${offView.name}: what happened in each organization.`, 'proxy-programs-result-title');
|
||||
const ul = el('ul', '', 'proxy-programs-outcomes');
|
||||
for (const r of offView.results) {
|
||||
const li = el('li'); li.dataset.outcome = typeof r?.outcome === 'string' && /^[a-z:-]{1,80}$/.test(r.outcome) && !KEYISH.test(r.outcome) ? r.outcome : 'unknown';
|
||||
li.append(el('span', orgLabel(r?.id), 'proxy-outcome-org'), document.createTextNode(': '), el('span', offOutcome(r?.outcome)));
|
||||
ul.append(li);
|
||||
}
|
||||
box.replaceChildren(title, offView.results.length ? ul : el('p', 'No organization was in the answer.', 'small muted'), ...reread());
|
||||
}
|
||||
// Read the status again after a turn-off answer, after a read already under way. load() keeps the
|
||||
// old state when its read fails, so a new state object is the sign the list was read again.
|
||||
async function rereadPrograms(fromButton) {
|
||||
const view = offView; if (!view) return;
|
||||
view.reread = 'reading'; paintOffResult();
|
||||
while (loading) await pause(50);
|
||||
const painted = await load();
|
||||
if (offView !== view) return;
|
||||
view.reread = painted ? 'ok' : 'failed'; paintOffResult();
|
||||
const box = $('proxy-setup-programs-result');
|
||||
if (fromButton || document.activeElement === document.body || !document.activeElement) (view.reread === 'failed' ? $('proxy-setup-programs-reread') : box)?.focus();
|
||||
}
|
||||
async function programsOff(name) {
|
||||
if (!sendOk || offBusy) return;
|
||||
offBusy = true; offAsk = null; offView = { name, working: true }; paintPrograms(); paintOffResult();
|
||||
const box = $('proxy-setup-programs-result'); box.focus();
|
||||
let view;
|
||||
try {
|
||||
const r = await fetch('/api/proxy/plugins', { cache: 'no-store', redirect: 'manual' }); const v = r.ok ? await r.json() : null;
|
||||
const names = Array.isArray(v?.available?.harness) ? v.available.harness.filter(n => typeof n === 'string') : null;
|
||||
if (!names) throw Error();
|
||||
list.replaceChildren(...(names.length ? names.map(n => el('li', n)) : [el('li', 'No programs are installed.', 'muted')]));
|
||||
} catch { programsRead = false; list.replaceChildren(el('li', 'The programs could not be read.', 'muted')); }
|
||||
});
|
||||
const value = await admin('programs/off', { name });
|
||||
if (!value || typeof value !== 'object' || !Array.isArray(value.results)) throw Object.assign(new Error('The answer could not be read.'), { code: 'outcome-unknown' });
|
||||
view = { name, results: value.results };
|
||||
addReceipt(value.receipt);
|
||||
} catch (error) {
|
||||
if (error?.code === 'proxy-admin-busy') view = { name, kind: 'busy', line: `${name} was not turned off. ${OFF_WORDS['proxy-admin-busy']}` };
|
||||
else if (error?.code === 'invalid-request' || error?.code === 'human-required') view = { name, line: `${name} was not turned off. ${failText(error, OFF_WORDS)}` };
|
||||
else view = { name, line: `Turning off ${name} may not have finished, so some organizations may have changed. ${failText(error, OFF_WORDS)}` };
|
||||
}
|
||||
offView = view; offBusy = false; paintOffResult(); paintPrograms();
|
||||
box.focus();
|
||||
// After any answer, read the status again.
|
||||
await rereadPrograms(false);
|
||||
}
|
||||
// settings-s5c: access health per organization, read when Technical details opens (owner only).
|
||||
// Ready only for health ok whose every stack-managed key is active; never a key, a key id or the
|
||||
// count of other keys, which holds other organizations' keys and is not a warning.
|
||||
const ACCESS_WORDS = { 'human-required': 'Only the instance owner can read access.', 'outcome-unknown': 'The host could not read access.', 'not-configured': 'Model service setup is not available on this Console.' };
|
||||
let accessRound = 0, accessView = null;
|
||||
function accessHealth(o) {
|
||||
const unknown = ['badge-muted', 'Not known', 'The answer did not say.'];
|
||||
if (!o || typeof o !== 'object') return unknown;
|
||||
const { health, reason, keys } = o;
|
||||
if (health === 'ok') return reason === undefined && Array.isArray(keys) && keys.length > 0 && keys.every(k => k && typeof k === 'object' && k.state === 'active')
|
||||
? ['badge-ok', 'Ready', null] : ['badge-muted', 'Not known', 'The answer did not agree with itself.'];
|
||||
if (health === 'attention') return reason === 'proxy-uncertain'
|
||||
? ['badge-attn', 'Not sure', 'The model service did not answer clearly, so Mosaic can\'t tell whether its access works.']
|
||||
: ['badge-attn', 'Needs attention', 'Some of its access is not active.'];
|
||||
if (health === 'missing') return ['badge-attn', 'None yet', 'No agent here has access to your models yet.'];
|
||||
if (health === 'not-riding') return ['badge-muted', 'Not used', 'It does not use your models.'];
|
||||
if (health === 'unreadable') return ['badge-danger', 'Could not be read', reason === 'bus-down' ? 'Its bus did not answer.' : reason === 'no-proxy-client' ? 'Its bus has no model service client set up.' : reason === 'unreadable' ? 'Its answer about access could not be read.' : null];
|
||||
return unknown;
|
||||
}
|
||||
function paintAccess() {
|
||||
const list = $('proxy-setup-access'), message = $('proxy-setup-access-message'), retry = $('proxy-setup-access-retry');
|
||||
retry.hidden = !accessView?.error;
|
||||
if (!accessView) { message.className = 'small muted'; message.textContent = 'Not read yet.'; list.replaceChildren(); return; }
|
||||
if (accessView.reading) { message.className = 'small muted'; message.textContent = 'Reading…'; return; }
|
||||
if (accessView.error) { message.className = 'banner err'; message.textContent = `Access could not be read. ${accessView.error}`; list.replaceChildren(); return; }
|
||||
const orgs = accessView.organizations;
|
||||
message.className = 'small muted'; message.textContent = orgs.length ? '' : 'There is no organization to check.';
|
||||
list.replaceChildren(...orgs.map(o => {
|
||||
const [badge, word, why] = accessHealth(o), li = el('li');
|
||||
li.dataset.access = word === 'Ready' ? 'ready' : 'not-ready';
|
||||
li.append(el('span', orgLabel(o?.id)), el('span', word, `badge ${badge}`));
|
||||
if (why) li.append(el('p', why, 'small muted account-reason'));
|
||||
return li;
|
||||
}));
|
||||
}
|
||||
async function readAccess() {
|
||||
if (!enabled) return;
|
||||
const round = ++accessRound, refocus = document.activeElement?.id === 'proxy-setup-access-retry';
|
||||
accessView = { ...(accessView ?? {}), reading: true, error: null }; paintAccess();
|
||||
let view;
|
||||
try {
|
||||
const value = await admin('access');
|
||||
if (!value || typeof value !== 'object' || !Array.isArray(value.organizations)) throw Object.assign(new Error('The answer could not be read.'), { code: 'outcome-unknown' });
|
||||
view = { organizations: value.organizations };
|
||||
} catch (error) { view = { error: failText(error, ACCESS_WORDS) }; }
|
||||
if (round !== accessRound || !enabled) return;
|
||||
accessView = view; paintAccess();
|
||||
if (refocus) { if (view.error) $('proxy-setup-access-retry').focus(); else { const m = $('proxy-setup-access-title'); m.tabIndex = -1; m.focus(); } }
|
||||
}
|
||||
$('proxy-setup-access-retry').addEventListener('click', readAccess);
|
||||
// #1873 ride-default: Ride all and the fixture move. The server decides each organization again;
|
||||
// this code only words the answer. Every write asks first, in plain words, what changes.
|
||||
const RIDE_REASONS = {
|
||||
@@ -1308,22 +1676,24 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
};
|
||||
$('proxy-ride-go').addEventListener('click', () => { if (!$('proxy-ride-go').disabled) rideAll(); });
|
||||
function paint() {
|
||||
paintState(); paintControls(); paintReceipts(); paintService(); paintRestart(); paintOrgsCard(); paintTriageCard(); paintAccountCard();
|
||||
paintState(); paintControls(); paintService(); paintRestart(); paintOrgsCard(); paintTriageCard(); paintAccountCard(); paintPrograms();
|
||||
const job = state.fetch;
|
||||
if (job.state === 'running') say(`Downloading: ${STEPS[job.step] ?? job.step ?? 'starting'}…`);
|
||||
else if (job.state === 'failed' && seenFetch !== 'failed') { say(`Fetch failed. ${explain(job.error)}`, 'error'); seenFetch = 'failed'; }
|
||||
else if (job.state === 'done' && job.receipt && seenFetch !== job.receipt.time) { seenFetch = job.receipt.time; if (!state.receipts?.some(r => r.op === 'fetch' && r.time === job.receipt.time)) addReceipt(job.receipt); say(`Fetched ${job.receipt.version}; archive, panel and executable hashes verified.`, 'success'); }
|
||||
else if (job.state === 'done' && job.receipt && seenFetch !== job.receipt.time) { seenFetch = job.receipt.time; addReceipt(job.receipt); say(`Fetched ${job.receipt.version}; archive, panel and executable hashes verified.`, 'success'); }
|
||||
clearTimeout(timer); timer = null;
|
||||
if (enabled && !chain?.running && (job.state === 'running' || state.busy)) timer = setTimeout(load, 1000);
|
||||
// The sidebar's model marks read the same status (MosaicModelDots).
|
||||
document.dispatchEvent(new CustomEvent('mosaic-proxy-status', { detail: state }));
|
||||
focusStep();
|
||||
}
|
||||
// True once the answer is painted, so a caller can say the list on the page is the new one.
|
||||
async function load() {
|
||||
if (!enabled || loading) return; loading = true;
|
||||
try { state = await admin('status'); causeRound++; if (state.next?.[0] === 'accounts' && triageSet === null) await readTriage(); paint(); if (state.init.initialized) await accountsReady(); }
|
||||
catch (error) { say(error.message, 'error'); }
|
||||
if (!enabled || loading) return false; loading = true; let painted = false;
|
||||
try { state = await admin('status'); setProviders(state.providers); causeRound++; if (state.next?.[0] === 'accounts' && triageSet === null) await readTriage(); paint(); painted = true; if (state.init.initialized) await accountsReady(); }
|
||||
catch (error) { say(error.message, 'error'); if (providerKey === null) setProviders(null); }
|
||||
finally { loading = false; }
|
||||
return painted;
|
||||
}
|
||||
async function act(label, action) {
|
||||
if (!sendOk || state?.busy || chain?.running) return;
|
||||
@@ -1437,7 +1807,7 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
if (proxyAdmin) { root.querySelectorAll('[data-admin]').forEach(part => { part.hidden = false; }); setup.start(config); }
|
||||
paintAccountCard();
|
||||
return refresh();
|
||||
}).then(() => message('')).catch(error => { message(error.message);
|
||||
}).then(() => message('')).catch(error => { message(error.message); if (!proxyAdmin && providerKey === null) setProviders(null);
|
||||
// #1873: on the setup page a stopped proxy hides the account parts like an unconfigured one,
|
||||
// and the setup load brings them back once the service answers.
|
||||
// Any failed read hides them, not only proxy-down: a proxy that is initialized but not yet answering
|
||||
|
||||
@@ -119,7 +119,19 @@ table.s1-table td code{font-size:12.5px;white-space:nowrap}
|
||||
.proxy-setup-next{display:flex;flex-wrap:wrap;align-items:center;gap:8px 16px;margin:0 0 12px}.proxy-setup-next p{margin:0;flex:1 1 280px}
|
||||
.proxy-setup-buttons{display:flex;flex-wrap:wrap;gap:8px}.proxy-setup-buttons button{min-height:44px}
|
||||
#proxy-setup pre{overflow-x:auto;max-width:100%;font-size:12px}#proxy-setup h3{margin:16px 0 4px}#proxy-setup details{margin:0 0 12px}#proxy-setup summary{min-height:44px;display:flex;align-items:center;cursor:pointer}
|
||||
#proxy-setup-receipts{padding-left:20px;overflow-wrap:anywhere;font-size:13px}
|
||||
/* settings-s5b: the setup history (GET /api/proxy/admin/history), the service key and the service's lines. */
|
||||
#proxy-setup-history{list-style:none;padding-left:0;overflow-wrap:anywhere;font-size:13px;margin:0 0 8px}#proxy-setup-history li{padding:2px 0}
|
||||
.proxy-setup-history-foot{display:flex;flex-wrap:wrap;gap:8px;margin:0}.proxy-setup-history-foot button,[data-log] button,#proxy-setup-keymade-retry{min-height:44px}
|
||||
/* settings-s5c: Programs that drive models under Advanced, and Access under Technical details. */
|
||||
.settings-form.proxy-programs{margin-bottom:20px;gap:8px}.proxy-programs-list{list-style:none;margin:8px 0 0;padding:0}.proxy-programs-list>li{padding:10px 0;border-top:1px solid var(--line);min-width:0}
|
||||
.proxy-program-head{display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:8px 12px}.proxy-program-name{font-weight:600;color:var(--strong)}
|
||||
.proxy-program-head button,.proxy-program-confirm button,#proxy-setup-access-retry{min-height:44px}.proxy-program-orgs{margin:4px 0 0;overflow-wrap:anywhere}
|
||||
.proxy-program-confirm{margin:8px 0 0;padding:10px 12px;border:1px solid color-mix(in srgb,var(--danger) 50%,var(--line));border-radius:var(--r);background:color-mix(in srgb,var(--danger) 6%,var(--surface))}.proxy-program-confirm>p:first-child{margin:0 0 8px;overflow-wrap:anywhere}
|
||||
.proxy-program-confirm button.danger{color:var(--danger);border-color:color-mix(in srgb,var(--danger) 60%,var(--line))}
|
||||
.proxy-programs-result{margin:12px 0 0;display:block}.proxy-programs-result[hidden]{display:none}.proxy-programs-result:focus-visible,#proxy-setup-access-title:focus-visible{outline:2px solid var(--focus);outline-offset:2px}.proxy-programs-result>p{margin:0}.proxy-programs-result>.proxy-programs-reread{margin-top:8px}.proxy-programs-result-title{font-weight:600;color:var(--strong)}
|
||||
.proxy-programs-outcomes{margin:6px 0 0;padding-left:18px;overflow-wrap:anywhere}.proxy-programs-outcomes li{padding:2px 0}.proxy-outcome-org{font-weight:600}
|
||||
.proxy-setup-access{margin:12px 0}.proxy-setup-access h4{margin:0 0 2px}.proxy-access li>.badge{justify-self:start}.proxy-setup-access .rows{margin-top:4px}#proxy-setup-access-message:empty{display:none}
|
||||
[data-log] .setup-log{margin:8px 0}[data-log] p{margin:0 0 8px}#proxy-setup-history-message:empty{display:none}
|
||||
/* #1873 settings slice 1 (D3, D4): the kept restart list and Technical details. */
|
||||
.proxy-setup-restart{margin:0 0 12px}
|
||||
.proxy-setup-restart-list{list-style:none;margin:8px 0 0;padding:0;display:grid;gap:8px}
|
||||
|
||||
@@ -127,8 +127,8 @@ const CAP = /^[0-9a-f]{64}$/;
|
||||
// The spawn's environment: these names from the Console's own environment, PATH fixed, and the
|
||||
// route's proxy variables. bwrap sets HOME and TMPDIR inside.
|
||||
export const SPAWN_ENV_ALLOW = Object.freeze(["PATH", "LANG", "LC_ALL", "TZ", "TERM"]);
|
||||
// Passed only as an absolute path: the pinned harness binary (packages/seat/src/session.mjs ENV_ALLOW).
|
||||
export const SPAWN_PATH_ALLOW = Object.freeze(["MOSAIC_CLAUDE_BIN"]);
|
||||
// Passed only as an absolute path: a harness binary the Console names (packages/seat/src/session.mjs ENV_ALLOW).
|
||||
export const SPAWN_PATH_ALLOW = Object.freeze(["MOSAIC_CLAUDE_BIN", "MOSAIC_PI_BIN"]);
|
||||
// The names the runner sets for every harness (runner.mjs adapterEnv); the host's own names never replace them.
|
||||
const RUNNER_NAMES = new Set(["MOSAIC_SYSTEM_PROMPT_FILE", "MOSAIC_WORKSPACE", "MOSAIC_SESSION_DIR", "MOSAIC_POLICY_FILE", "MOSAIC_TOOLS_FILE",
|
||||
"MOSAIC_TOOL_SOCKET", "MOSAIC_TURN_MARKER", "MOSAIC_PROVIDER", "MOSAIC_MODEL", "MOSAIC_MODEL_ACTIVITY_DIR", "MOSAIC_UNSLOP_LOG",
|
||||
@@ -826,6 +826,16 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
|
||||
const start = async ({ admitted, installRoots, transcriptRoot, mod }) => {
|
||||
ensureDirs();
|
||||
// #1873 pi-unpin re-check 7: a module whose env() reads a check it runs first (pi's fit check)
|
||||
// has it run and awaited before the bundle, which calls env(). A refusal or a throw refuses the
|
||||
// route, as a throw from env() does.
|
||||
if (typeof mod?.ready === "function") {
|
||||
try { await mod.ready(); }
|
||||
catch {
|
||||
log("triage host: route refused: harness-bundle");
|
||||
throw new TriageError("triage-route-invalid", "the model route's answer is not usable");
|
||||
}
|
||||
}
|
||||
const { run, harnessFiles } = writeBundle(admitted, mod);
|
||||
// The key this session holds, and anything shaped like a key, never reaches the transcript.
|
||||
const key = routeKey(admitted.env)?.value ?? null;
|
||||
|
||||
@@ -228,6 +228,9 @@ export function createTriageRoute({ dataRoot, configDir, proxyPort = null, catal
|
||||
// name the host's fence refuses (MOSAIC_* and ANTHROPIC_* among them: the host sets those)
|
||||
// and every name the module does not declare.
|
||||
const session = { model: offered.model, provider: offered.provider ?? "", ...(transport ? { proxyTransport: { ...transport } } : {}) };
|
||||
// #1873 pi-unpin re-check 7: a module whose env() reads a check it runs first (pi's fit
|
||||
// check) has it run here and awaited; a refusal or a throw is incompatible, as env()'s is.
|
||||
if (typeof mod.ready === "function") await mod.ready();
|
||||
const given = mod.env({ files: {}, policy: { tools: [] }, tools: [], session, models: {}, proxy: { ...PROXY_PROBE } });
|
||||
keyName = routeKeyName(given);
|
||||
if (!keyName) return refuse("triage-harness-incompatible");
|
||||
|
||||
@@ -185,6 +185,17 @@ test('dev owner resolves a decision through the real Console wrapper and bus', {
|
||||
const result=await post(`/api/orgs/acme/decisions/${decision.id}/resolve`,{choice:'yes',confirmation:confirmation.body.confirmation});
|
||||
assert.equal(result.status,200,JSON.stringify(result));
|
||||
assert.equal(result.body.choice,'yes');
|
||||
// settings-backend A2: the real Console's riders carry the organization file, so turning a program
|
||||
// off for every organization can read it; without it each organization answers unreadable.
|
||||
const get=path=>new Promise((resolve,reject)=>{const host=`127.0.0.1:${port}`;const req=request({host:'127.0.0.1',port,path,headers:{host}},res=>{let raw='';res.on('data',c=>raw+=c);res.on('end',()=>resolve({status:res.statusCode,body:JSON.parse(raw)}));});req.on('error',reject);req.end();});
|
||||
const status=await get('/api/orgs/acme/proxy/admin/status');
|
||||
assert.equal(status.status,200,JSON.stringify(status));
|
||||
const acmeOn=status.body.programs?.organizations?.find(o=>o.id==='acme')?.on;
|
||||
assert.ok(Array.isArray(acmeOn)&&acmeOn.length>0,JSON.stringify(status.body.programs));
|
||||
const off=await post('/api/orgs/acme/proxy/admin/programs/off',{name:acmeOn[0]});
|
||||
assert.equal(off.status,200,JSON.stringify(off));
|
||||
// The chief's route uses the program in both organizations, so neither file is written.
|
||||
assert.deepEqual(off.body.results,[{id:'acme',outcome:'in-use'},{id:'beta',outcome:'in-use'}],JSON.stringify(off.body));
|
||||
const stopped = new Promise(resolve => bus.once('exit', resolve));
|
||||
const stop = spawnSync(process.execPath,['packages/cli/src/cli.mjs','bus','stop','acme'],{env,encoding:'utf8'}); assert.equal(stop.status,0,stop.stderr); await stopped;
|
||||
busOut=''; bus=spawn(process.execPath,['packages/cli/src/cli.mjs','bus','start','acme','--no-launch'],{env,stdio:['ignore','pipe','pipe']});
|
||||
|
||||
@@ -22,7 +22,10 @@ const ROUTES = [
|
||||
['ride-all', 'POST', {}], ['ride-move', 'POST', { org: 'stand-in' }],
|
||||
// #1873 settings-backend A1: the model service log, the service key's made-at time, setup history.
|
||||
['log', 'GET'], ['service-key', 'GET'], ['history', 'GET'],
|
||||
// #1873 settings-backend A2: one program off for every organization, and access health.
|
||||
['programs/off', 'POST', { name: 'alpha' }], ['access', 'GET'],
|
||||
];
|
||||
const A2 = ROUTES.filter(([p]) => ['programs/off', 'access'].includes(p));
|
||||
const READS = ROUTES.filter(([p]) => ['log', 'service-key', 'history'].includes(p));
|
||||
|
||||
function fakeAdmin() {
|
||||
@@ -40,6 +43,8 @@ function fakeAdmin() {
|
||||
serviceKey: () => { calls.push(['serviceKey']); return { madeAt: '2026-11-01T08:00:00.000Z', renewedAt: null, renewal: 'not-built', problem: null }; },
|
||||
history: args => { calls.push(['history', args]); return { entries: [], next: null }; },
|
||||
recordAccount: entry => { calls.push(['recordAccount', entry]); return true; },
|
||||
programsOff: async args => { calls.push(['programsOff', args]); return { name: args.name, results: [], receipt: null }; },
|
||||
access: async () => { calls.push(['access']); return { organizations: [] }; },
|
||||
};
|
||||
}
|
||||
const send = (port, path, { method = 'GET', headers = {}, body } = {}) => new Promise((resolve, reject) => {
|
||||
@@ -61,7 +66,7 @@ async function devServer(t, options = {}) {
|
||||
}
|
||||
const hit = (f, route, extra = {}) => send(f.port, `/api/proxy/admin/${route[0]}`, { method: route[1], headers: { ...f.headers(), ...extra }, body: route[2] });
|
||||
|
||||
test('the route table is exactly the brief: status, render, fetch, init, four unit verbs, fence clear, lock clear, ride all, ride move, log, service key, history', () => {
|
||||
test('the route table is exactly the brief: status, render, fetch, init, four unit verbs, fence clear, lock clear, ride all, ride move, log, service key, history, programs off, access', () => {
|
||||
assert.deepEqual(PROXY_ADMIN_PATHS, ROUTES.map(([p]) => `/api/proxy/admin/${p}`));
|
||||
});
|
||||
|
||||
@@ -71,7 +76,8 @@ test('the dev local owner reaches every route; init and fence clear go to the bu
|
||||
const r = await hit(f, route);
|
||||
assert.equal(r.status, route[0] === 'fetch' ? 202 : 200, route[0]);
|
||||
}
|
||||
assert.deepEqual(f.admin.calls.map(c => c[0]), ['status', 'render', 'fetchStart', 'init', 'unitAction', 'unitAction', 'unitAction', 'unitAction', 'fenceClear', 'initLockClear', 'rideAll', 'rideMove', 'log', 'serviceKey', 'history']);
|
||||
assert.deepEqual(f.admin.calls.map(c => c[0]), ['status', 'render', 'fetchStart', 'init', 'unitAction', 'unitAction', 'unitAction', 'unitAction', 'fenceClear', 'initLockClear', 'rideAll', 'rideMove', 'log', 'serviceKey', 'history', 'programsOff', 'access']);
|
||||
assert.deepEqual(f.admin.calls.find(c => c[0] === 'programsOff')[1], { name: 'alpha', user: 'Local operator' });
|
||||
assert.deepEqual(f.admin.calls.filter(c => c[0] === 'unitAction').map(c => c[1]), ['install', 'start', 'stop', 'restart']);
|
||||
assert.deepEqual(f.busCalls.map(c => c[0]), ['proxy.init', 'proxy.fence.clear']);
|
||||
assert.deepEqual(f.busCalls[0][1], { generate: true });
|
||||
@@ -415,3 +421,88 @@ test('a real controller records the account write without the key, and history s
|
||||
assert.deepEqual(h.json, { entries: [{ time: new Date(0).toISOString(), op: 'account.proxy.apikey.add', provider: 'openai', user: 'Local operator' }], next: null });
|
||||
assert.equal(readFileSync(join(dataRoot, 'state', 'proxy-admin.jsonl'), 'utf8').includes(SENTINEL), false);
|
||||
});
|
||||
|
||||
// #1873 settings-backend A2: programs off and access health.
|
||||
test('A2: a member and an organization owner get human-required on programs off and access, before any controller call', async t => {
|
||||
const organizations = { list: async () => [], resolve: async id => (id === 'x' ? { busHealthy: true } : null) };
|
||||
for (const session of [{ name: 'Bob', provider: 'local-dev', iss: 'local-dev', sub: 'bob', rootOwner: false, instanceRole: 'member' },
|
||||
{ name: 'Olive', provider: 'local-dev', iss: 'local-dev', sub: 'olive', rootOwner: false, instanceRole: 'operator', orgRole: 'owner' }]) {
|
||||
const f = await devServer(t, { organizations, sso: { ...createDevOwner(), session: () => session } });
|
||||
for (const route of A2) {
|
||||
const inOrg = await send(f.port, `/api/orgs/x/proxy/admin/${route[0]}`, { method: route[1], headers: f.headers(), body: route[2] });
|
||||
assert.equal(inOrg.status, 403, `${session.sub} ${route[0]}`); assert.deepEqual(inOrg.json, { error: 'human-required' }, `${session.sub} ${route[0]}`);
|
||||
const plain = await hit(f, route);
|
||||
assert.equal(plain.status >= 400 && plain.status < 500, true, `${session.sub} ${route[0]} outside an organization is refused`);
|
||||
}
|
||||
assert.deepEqual(f.admin.calls, []);
|
||||
}
|
||||
});
|
||||
|
||||
test('A2: programs off takes one plain program name and nothing else; access takes no body', async t => {
|
||||
const f = await devServer(t);
|
||||
for (const body of [{}, { name: '' }, { name: 'Alpha' }, { name: '../x' }, { name: 7 }, { name: 'a'.repeat(33) }, { name: 'alpha', extra: 1 }, { name: ['alpha'] }])
|
||||
assert.deepEqual((await hit(f, ['programs/off', 'POST', body])).json, { error: 'invalid-request' }, JSON.stringify(body));
|
||||
assert.deepEqual(f.admin.calls, []);
|
||||
assert.equal((await send(f.port, '/api/proxy/admin/access', { method: 'POST', headers: f.headers(), body: {} })).status, 405);
|
||||
assert.equal((await send(f.port, '/api/proxy/admin/programs/off', { headers: f.headers() })).status, 405);
|
||||
f.admin.programsOff = async () => { throw Object.assign(new Error(`${SENTINEL} busy`), { code: 'proxy-admin-busy' }); };
|
||||
const busy = await hit(f, ['programs/off', 'POST', { name: 'alpha' }]);
|
||||
assert.equal(busy.status, 409); assert.deepEqual(busy.json, { error: 'proxy-admin-busy' });
|
||||
f.admin.access = async () => { throw new Error(SENTINEL); };
|
||||
assert.deepEqual((await hit(f, ['access', 'GET'])).json, { error: 'outcome-unknown' });
|
||||
assert.equal(f.logs.join('\n').includes(SENTINEL), false);
|
||||
});
|
||||
|
||||
// The real controller behind the real routes, with fake organizations whose answers carry planted keys.
|
||||
test('A2 end to end: programs off writes one receipt, skips a down bus, and no answer, receipt or history holds a key; access never says ok for proxy-uncertain', async t => {
|
||||
const { createProxyAdmin } = await import('../../model-proxy/src/admin-control.mjs');
|
||||
const { mkdirSync } = await import('node:fs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'pa-a2-'));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
mkdirSync(configDir, { mode: 0o700 }); mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const TAIL = 'Ab3_dE5-fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7dE';
|
||||
const planted = [`msk-k1a2-${TAIL}`, `mso-k1a2-acme-${TAIL}`, `mst-k1a2-triage-${TAIL}`, `msp-k1a2-${TAIL}`];
|
||||
const REV = 'c'.repeat(64);
|
||||
const sets = [];
|
||||
const org = (id, { down = false, keys = [], keysError = null } = {}) => ({
|
||||
id, fixture: false, cliproxy: true, changes: '',
|
||||
file: () => ({ document: { roles: {}, plugins: { harness: ['alpha', 'beta'] }, note: planted[1] }, revision: REV }),
|
||||
call: async (verb, args) => {
|
||||
if (down) throw Object.assign(new Error(planted[0]), { code: 'outcome-unknown' });
|
||||
if (verb === 'plugins') return { business: id, revision: REV, plugins: { harness: ['alpha', 'beta'] }, note: planted[0] };
|
||||
if (verb === 'routes') return { business: id, revision: REV, instances: {}, note: planted[2] };
|
||||
if (verb === 'plugins.set') { sets.push([id, args]); return { business: id, revision: 'd'.repeat(64) }; }
|
||||
if (verb === 'proxy.keys') { if (keysError) throw Object.assign(new Error(planted[3]), { code: keysError }); return { keys }; }
|
||||
throw Object.assign(new Error(planted[3]), { code: 'invalid-request' });
|
||||
},
|
||||
});
|
||||
const orgs = [
|
||||
org('acme', { keys: [{ keyId: '0123456789ab', management: 'stack-managed', agent: planted[1], issuedAt: planted[3], state: 'active' }, { keyId: 'abcdefabcdef', management: 'not stack-managed', agent: planted[0], issuedAt: null, state: null }] }),
|
||||
org('bolt', { down: true }),
|
||||
org('crux', { keysError: 'proxy-uncertain' }),
|
||||
];
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit: { unit: 'mosaic-proxy-zz.service', status: async () => ({ installed: false }) }, buses: async () => ({ stopped: true, running: [] }), now: () => new Date(0), env: {},
|
||||
riders: async () => orgs, harnesses: () => ['alpha', 'beta'], rootAgents: () => ({ state: 'absent', agents: {}, assistant: null }) });
|
||||
const f = await devServer(t, { proxyAdmin: admin });
|
||||
const unknown = await hit(f, ['programs/off', 'POST', { name: 'gamma' }]);
|
||||
assert.equal(unknown.status, 400); assert.deepEqual(unknown.json, { error: 'invalid-request' });
|
||||
const off = await hit(f, ['programs/off', 'POST', { name: 'alpha' }]);
|
||||
assert.equal(off.status, 200);
|
||||
assert.deepEqual(off.json.results, [{ id: 'acme', outcome: 'off' }, { id: 'bolt', outcome: 'bus-down' }, { id: 'crux', outcome: 'off' }]);
|
||||
assert.deepEqual(off.json.receipt, { time: new Date(0).toISOString(), op: 'programs.off', name: 'alpha', results: off.json.results, user: 'Local operator' });
|
||||
assert.deepEqual(sets.map(([id]) => id), ['acme', 'crux']);
|
||||
const lines = readFileSync(join(dataRoot, 'state', 'proxy-admin.jsonl'), 'utf8').trim().split('\n');
|
||||
assert.equal(lines.length, 1);
|
||||
const access = await hit(f, ['access', 'GET']);
|
||||
assert.equal(access.status, 200);
|
||||
assert.deepEqual(access.json.organizations.map(o => [o.id, o.health, o.reason ?? null]), [['acme', 'ok', null], ['bolt', 'unreadable', 'bus-down'], ['crux', 'attention', 'proxy-uncertain']]);
|
||||
assert.deepEqual(access.json.organizations[0].keys, [{ keyId: '0123456789ab', agent: null, state: 'active', issuedAt: null }]);
|
||||
const status = await hit(f, ['status', 'GET']);
|
||||
assert.deepEqual(status.json.programs, { installed: ['alpha', 'beta'], organizations: [{ id: 'acme', on: ['alpha', 'beta'] }, { id: 'bolt', on: null }, { id: 'crux', on: ['alpha', 'beta'] }] });
|
||||
const history = await hit(f, ['history', 'GET']);
|
||||
assert.deepEqual(history.json.entries, [off.json.receipt]);
|
||||
const all = [unknown.text, off.text, access.text, status.text, history.text, lines.join('\n'), f.logs.join('\n')].join('\n');
|
||||
assert.doesNotMatch(all, /ms[kotp]-[A-Za-z0-9_-]{4,}/);
|
||||
for (const bit of [TAIL, TAIL.slice(0, 10), root]) assert.equal(all.includes(bit), false, bit);
|
||||
});
|
||||
@@ -10,3 +10,21 @@ test('Console production module graph cannot import the model-proxy client', asy
|
||||
platform: 'node', format: 'esm', splitting: true, outdir: '/unused-proxy-architecture', write: false, metafile: true, logLevel: 'silent' });
|
||||
assert.equal(Object.keys(result.metafile.inputs).some(file => file.endsWith('/model-proxy/src/client.mjs')), false);
|
||||
});
|
||||
// #1873 settings-backend-a3: the provider list lives in the cliproxy plugin's PROVIDERS. The
|
||||
// management client, the bus proxy page and the settings page read it; none of them writes the
|
||||
// provider ids, sign-in hosts or key base URL out as its own list.
|
||||
test('a3: no provider id, sign-in host or key base URL is written out in client.mjs, proxy-page.mjs or display.js', async () => {
|
||||
const { readFileSync } = await import('node:fs');
|
||||
const { PROVIDERS } = await import('../../plugins/model-provider/cliproxy/index.mjs');
|
||||
const escape = text => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
const words = PROVIDERS.flatMap(p => [p.id, p.signInHost, p.baseUrl]).filter(Boolean);
|
||||
assert.ok(words.length >= PROVIDERS.length + 2);
|
||||
const quoted = new RegExp(`['"\`](${words.map(escape).join('|')})['"\`]`);
|
||||
const keyed = new RegExp(`[{,]\\s*(${PROVIDERS.map(p => escape(p.id)).join('|')})\\s*:`);
|
||||
for (const file of ['../../model-proxy/src/client.mjs', '../../bus/src/proxy-page.mjs', '../src/public/display.js']) {
|
||||
const text = readFileSync(new URL(file, import.meta.url), 'utf8');
|
||||
assert.equal(quoted.exec(text)?.[0], undefined, `${file} writes a provider value out`);
|
||||
assert.equal(keyed.exec(text)?.[0], undefined, `${file} keys an object by provider id`);
|
||||
assert.equal(/<option value=/.test(text), false, `${file} writes a provider option out`);
|
||||
}
|
||||
});
|
||||
@@ -6,8 +6,9 @@ import http from 'node:http';
|
||||
import { readFileSync, existsSync } from 'node:fs';
|
||||
import { resolve, extname } from 'node:path';
|
||||
import { browser } from './browser.mjs';
|
||||
async function setup(t, readonly = false, refusal = null) {
|
||||
const state = { routes: [], loginStatus: 'wait', receivedKey: false, release: null, priority: 0, models: ['fixture-old'] };
|
||||
import { publicProviders } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
async function setup(t, readonly = false, refusal = null, providers = publicProviders()) {
|
||||
const state = { routes: [], loginStatus: 'wait', receivedKey: false, keyProvider: null, release: null, priority: 0, models: ['fixture-old'] };
|
||||
const server = http.createServer(async (req, res) => {
|
||||
const send = value => { res.setHeader('content-type', 'application/json'); res.end(JSON.stringify(value)); };
|
||||
if (req.url === '/api/config') return send({ send: !readonly, devOwner: true });
|
||||
@@ -16,10 +17,10 @@ async function setup(t, readonly = false, refusal = null) {
|
||||
if (refusal && req.url.endsWith(refusal)) { res.statusCode = 503; return send({ error: 'not-configured' }); }
|
||||
const chunks = []; for await (const c of req) chunks.push(c);
|
||||
const args = chunks.length ? JSON.parse(Buffer.concat(chunks)) : {};
|
||||
if (req.url.endsWith('/accounts')) return send({ accounts: [{ accountId: 'a'.repeat(12), provider: 'claude', displayName: '<img src=x onerror="window.injected=true">', status: 'ready', priority: state.priority }, { accountId: 'c'.repeat(12), provider: 'z.ai', displayName: 'Fixture z.ai account', status: 'active', priority: 0, models: state.models }] });
|
||||
if (req.url.endsWith('/accounts')) return send({ accounts: [{ accountId: 'a'.repeat(12), provider: 'claude', displayName: '<img src=x onerror="window.injected=true">', status: 'ready', priority: state.priority }, { accountId: 'c'.repeat(12), provider: 'z.ai', displayName: 'Fixture z.ai account', status: 'active', priority: 0, models: state.models }], ...(providers === 'none' ? {} : { providers }) });
|
||||
if (req.url.endsWith('/keys')) return send({ keys: [{ keyId: 'b'.repeat(12), management: 'sentinel', agent: null, issuedAt: '2026-11-07T02:24:02.021Z', state: null }] });
|
||||
if (req.url.endsWith('/apikey/add')) {
|
||||
state.receivedKey = args.key === 'p4-browser-key';
|
||||
state.receivedKey = args.key === 'p4-browser-key'; state.keyProvider = args.provider;
|
||||
await new Promise(r => { state.release = r; }); return send({ added: true });
|
||||
}
|
||||
if (req.url.endsWith('/login/start')) return send({ url: 'https://claude.ai/oauth/authorize?state=p4-state', state: 'p4-state' });
|
||||
@@ -113,3 +114,35 @@ test('settings-s4: Model setup shows no client keys table and reads no keys (des
|
||||
assert.doesNotMatch(await b.evaluate('document.body.innerText'),/Client keys|Accounts and client keys/);
|
||||
assert.equal(state.routes.some(r=>r.startsWith('/api/proxy/keys')),false);
|
||||
});
|
||||
// #1873 settings-backend-a3: anyone but the instance owner reads no setup status, so the select comes
|
||||
// from the providers the accounts answer carries (the plugin's list as id, label and sign-in kind).
|
||||
const selectNow = b => b.evaluate(`(() => { const s = document.querySelector("#proxy-provider"); return { options: [...s.options].map(o => [o.value, o.textContent]),
|
||||
hidden: s.hidden, help: document.querySelector("#proxy-provider-help").textContent, go: document.querySelector("#proxy-login-go").hidden }; })()`);
|
||||
test('settings-backend-a3: the select is built from the accounts answer\'s providers, and a key goes to the provider picked', { timeout: 60000 }, async t => {
|
||||
const { b, state, wait } = await setup(t);
|
||||
assert.deepEqual((await selectNow(b)).options, publicProviders().map(p => [p.id, `${p.label} (${p.signIn === 'oauth' ? 'subscription' : 'API key'})`]));
|
||||
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("#proxy-accounts li[data-account]")].map(li => [li.children[0].textContent, li.children[1].firstChild.textContent])'),
|
||||
[['Claude', 'Subscription'], ['z.ai', 'API key']], 'a row takes its provider name and kind from the list');
|
||||
await b.evaluate('(() => { const s = document.querySelector("#proxy-provider"); s.value = "z.ai"; s.dispatchEvent(new Event("change")); document.querySelector("#proxy-apikey input").value = "p4-browser-key"; document.querySelector("#proxy-apikey").requestSubmit(); return true; })()');
|
||||
for (let n = 0; n < 100 && !state.release; n++) await new Promise(r => setTimeout(r, 20));
|
||||
assert.equal(state.keyProvider, 'z.ai'); state.release();
|
||||
await wait('document.querySelector("#proxy-message").textContent.includes("added")');
|
||||
});
|
||||
test('settings-backend-a3: a provider the accounts answer leaves out is not in the select', { timeout: 60000 }, async t => {
|
||||
const { b } = await setup(t, false, null, publicProviders().filter(p => p.id !== 'claude'));
|
||||
const now = await selectNow(b);
|
||||
assert.deepEqual(now.options, [['codex', 'ChatGPT / Codex (subscription)'], ['z.ai', 'z.ai (API key)']]);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-login-go").textContent'), 'Sign in to ChatGPT / Codex');
|
||||
});
|
||||
for (const [name, providers] of [['no providers', 'none'], ['an empty list', []]]) {
|
||||
test(`settings-backend-a3: an accounts answer with ${name} shows no select and says the providers could not be read`, { timeout: 60000 }, async t => {
|
||||
const { b } = await setup(t, false, null, providers);
|
||||
assert.deepEqual(await selectNow(b), { options: [], hidden: true, help: 'The providers could not be read, so no account can be added here.', go: true });
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-apikey").hidden'), true);
|
||||
});
|
||||
}
|
||||
test('settings-backend-a3: an accounts read that fails says the providers could not be read', { timeout: 60000 }, async t => {
|
||||
const { b, wait } = await setup(t, false, '/accounts');
|
||||
await wait('!/^Reading/.test(document.querySelector("#proxy-provider-help").textContent)');
|
||||
assert.deepEqual(await selectNow(b), { options: [], hidden: true, help: 'The providers could not be read, so no account can be added here.', go: true });
|
||||
});
|
||||
@@ -5,6 +5,7 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { w15Fixture } from './w15-fixture.mjs';
|
||||
import { publicProviders } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
|
||||
const T = '2026-11-11T12:00:00.000Z';
|
||||
const base = {
|
||||
@@ -15,7 +16,7 @@ const base = {
|
||||
fetch: { state: 'idle' }, restart: null, busy: null,
|
||||
organizations: [{ id: 'acme', proxy: 'shared', catalogue: null }],
|
||||
ownProxy: { available: false, state: 'not built', reason: 'The configuration has one proxy per instance.' },
|
||||
next: ['fetch'],
|
||||
next: ['fetch'], providers: publicProviders(),
|
||||
};
|
||||
const fetched = { ...base, release: { ...base.release, fetched: true, verified: true }, next: ['init'] };
|
||||
const fenced = { ...base, release: fetched.release, init: { initialized: true, partial: false, settings: { port: 18432 }, files: [] },
|
||||
@@ -26,9 +27,12 @@ const scenario = (status, config = {}) => ({ '/api/config': { send: true, devOwn
|
||||
const writes = b => b.evaluate('window.__writes.filter(w=>w.path.startsWith("/api/proxy/admin/")).map(w=>({path:w.path,body:JSON.parse(w.body)}))');
|
||||
// Step 3's line names the step it holds in data-next.
|
||||
const STEP3 = '#setup-step-organizations [data-step-summary]';
|
||||
// settings-s5b: Technical details reads the setup history from GET /api/proxy/admin/history, not from receipts.
|
||||
const history = entries => ({ '/api/proxy/admin/history?limit=25': { entries, next: null }, '/api/proxy/admin/service-key': { madeAt: T, renewedAt: null, renewal: 'not-built', problem: null } });
|
||||
const historyItems = b => b.evaluate('[...document.querySelectorAll("#proxy-setup-history li")].map(li=>li.textContent.split(" · ")[0])');
|
||||
|
||||
test('a fresh instance shows Set up model service as one button, the not-built own proxy, and no terminal note', { timeout: 90000 }, async t => {
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(base), 'POST /api/proxy/admin/fetch': { status: 202, body: { state: 'running' } } } });
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(base), 'POST /api/proxy/admin/fetch': { status: 202, body: { state: 'running' } }, '/api/proxy/admin/log?lines=20': { unit: 'mosaic-proxy.service', lines: [], redacted: 0, dropped: 0 } } });
|
||||
await b.navigate(f.base + '/#/settings/model-setup');
|
||||
await wait('document.querySelector("#proxy-setup-go")?.dataset.next==="fetch"');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup").hidden'), false);
|
||||
@@ -46,6 +50,8 @@ test('a fresh instance shows Set up model service as one button, the not-built o
|
||||
await wait('document.querySelector("#setup-step-service").dataset.state==="bad"');
|
||||
assert.deepEqual(await writes(b), [{ path: '/api/proxy/admin/fetch', body: {} }]);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-go").textContent'), 'Try again');
|
||||
// settings-s5b: a failed step 1 opens the service's last lines, read from the log route.
|
||||
await wait('document.querySelector("#proxy-setup-lines").open && /written nothing yet/.test(document.querySelector("#proxy-setup-lines [data-log]").textContent)');
|
||||
assert.deepEqual(await b.evaluate('window.__unexpected'), []);
|
||||
});
|
||||
|
||||
@@ -59,7 +65,7 @@ test('without send the next-step button is disabled and nothing is posted', { ti
|
||||
});
|
||||
|
||||
test('init always sends generate; there is no pasted key, and Try again sends generate again', { timeout: 90000 }, async t => {
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(fetched), 'POST /api/proxy/admin/init': { body: { settings: { port: 18432 }, receipt: { op: 'init', time: T, port: 18432, generated: true } } } } });
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(fetched), ...history([{ op: 'init', time: T }]), 'POST /api/proxy/admin/init': { body: { settings: { port: 18432 }, receipt: { op: 'init', time: T, port: 18432, generated: true } } } } });
|
||||
await b.navigate(f.base + '/#/settings/model-setup');
|
||||
await wait('document.querySelector("#proxy-setup-go")?.dataset.next==="init"');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-go").textContent'), 'Continue setup');
|
||||
@@ -73,7 +79,11 @@ test('init always sends generate; there is no pasted key, and Try again sends ge
|
||||
await b.evaluate('(()=>{const f=document.querySelector("#proxy-setup-init");f.elements.port.value="18432";document.querySelector("#proxy-setup-go").click();})()');
|
||||
await wait('window.__writes.some(w=>w.path==="/api/proxy/admin/init")');
|
||||
assert.deepEqual(await writes(b), [{ path: '/api/proxy/admin/init', body: { generate: true, port: 18432 } }]);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-setup-receipts").textContent'), /init/);
|
||||
// settings-s5b: the receipt is not shown raw; the history read under Technical details names it.
|
||||
assert.doesNotMatch(await b.evaluate('document.querySelector("#proxy-setup").textContent'), /generated/);
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = true');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 1');
|
||||
assert.deepEqual(await historyItems(b), ['Set up']);
|
||||
// The status still names init, so the run stops there; Try again starts from it.
|
||||
await wait('document.querySelector("#proxy-setup-go")?.textContent==="Try again" && !document.querySelector("#proxy-setup-init [data-setup-write]").disabled');
|
||||
await b.evaluate('(()=>{const f=document.querySelector("#proxy-setup-init");f.elements.port.value="";document.querySelector("#proxy-setup-go").click();})()');
|
||||
@@ -82,14 +92,20 @@ test('init always sends generate; there is no pasted key, and Try again sends ge
|
||||
});
|
||||
|
||||
test('after a confirmed restart the next step clears the fence and shows the receipt', { timeout: 90000 }, async t => {
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(fenced), 'POST /api/proxy/admin/fence/clear': { body: { cleared: true, receipt: { op: 'fence.clear', time: T, pid: 4242 } } } } });
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(fenced), ...history([]), 'POST /api/proxy/admin/fence/clear': { body: { cleared: true, receipt: { op: 'fence.clear', time: T, pid: 4242 } } } } });
|
||||
await b.navigate(f.base + '/#/settings/model-setup');
|
||||
await wait('document.querySelector("#proxy-setup-go")?.dataset.next==="fence-clear"');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = true');
|
||||
await wait('document.querySelector("#proxy-setup-history-message").textContent === "No setup history yet."');
|
||||
await b.evaluate(`window.__scenario['/api/proxy/admin/history?limit=25'] = { entries: [{ op: 'fence.clear', time: '${T}' }], next: null }`);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-setup-state").textContent'), /new pid 4242/);
|
||||
await b.evaluate('document.querySelector("#proxy-setup-go").click()');
|
||||
await wait('document.querySelector("#proxy-setup-message").textContent.includes("Fence cleared")');
|
||||
assert.deepEqual(await writes(b), [{ path: '/api/proxy/admin/fence/clear', body: {} }]);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-setup-receipts").textContent'), /fence\.clear.*pid: 4242/);
|
||||
// settings-s5b: with Technical details open, the write reads the history again.
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 1');
|
||||
assert.deepEqual(await historyItems(b), ['Key writes resumed']);
|
||||
assert.doesNotMatch(await b.evaluate('document.querySelector("#proxy-setup").textContent'), /pid: 4242/);
|
||||
});
|
||||
|
||||
test('a stopped proxy hides the account panels with a setup note, and starting the service brings them back', { timeout: 90000 }, async t => {
|
||||
@@ -143,13 +159,16 @@ test('a fence with no readable time names the --proxy-restarted path instead of
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-next details.proxy-setup-cli p").textContent'), 'Run this in a terminal on this machine: mosaic proxy fence clear --proxy-restarted');
|
||||
});
|
||||
|
||||
test('after a reload the receipts come from status, and an initialized proxy shows its settings read-only', { timeout: 90000 }, async t => {
|
||||
test('after a reload the history comes from the history read, not the status receipts, and an initialized proxy shows its settings read-only', { timeout: 90000 }, async t => {
|
||||
const initialized = { ...base, release: fetched.release, init: { initialized: true, partial: false, settings: { port: 18432 }, files: [] },
|
||||
receipts: [{ op: 'fetch', time: T, version: '8.0.42' }, { op: 'init', time: T, generated: true, port: 18432 }], next: ['unit-install'] };
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(initialized), 'POST /api/proxy/admin/unit/install': { body: { receipt: { op: 'unit.install', time: T } } } } });
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(initialized), ...history([{ op: 'fetch', time: T }, { op: 'init', time: T }]), 'POST /api/proxy/admin/unit/install': { body: { receipt: { op: 'unit.install', time: T } } } } });
|
||||
await b.navigate(f.base + '/#/settings/model-setup');
|
||||
await wait('document.querySelector("#proxy-setup-go")?.dataset.next==="unit-install"');
|
||||
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("#proxy-setup-receipts li strong")].map(s=>s.textContent)'), ['init', 'fetch'], 'newest first, from proxy-admin.jsonl');
|
||||
assert.equal(await b.evaluate('document.querySelectorAll("#proxy-setup-history li").length'), 0, 'nothing is read before Technical details opens');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = true');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 2');
|
||||
assert.deepEqual(await historyItems(b), ['Set up', 'Release downloaded and checked'], 'newest first, from the history read');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-init").hidden'), true, 'init is done, so its form is gone');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-settings").textContent'), 'Port18432');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-settings-note").textContent'), 'Changing these after setup is not built yet.');
|
||||
@@ -446,8 +465,8 @@ test('settings-s1 B4: only the next step has a button; done steps, Fence, Init l
|
||||
assert.deepEqual(await b.evaluate(`${visible('.settings-content .primary')}.map(e=>e.id||e.textContent)`), ['proxy-login-go'], 'one primary button on the page');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-go")'), null, 'step 1 is done, so it has no button');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-init").checkVisibility()'), false, 'init is done, so its form is gone');
|
||||
const tech = await b.evaluate('(()=>{const d=document.querySelector("#proxy-setup-tech");return {summary:d.querySelector("summary").textContent,open:d.open,terms:[...d.querySelectorAll("dt")].map(x=>x.textContent),units:d.querySelectorAll("[data-unit]").length,receipts:!!d.querySelector("#proxy-setup-receipts")}})()');
|
||||
assert.deepEqual(tech, { summary: 'Technical details: release, service, history', open: false, terms: ['Release', 'Initialized', 'Service', 'Fence', 'Init lock'], units: 4, receipts: true });
|
||||
const tech = await b.evaluate('(()=>{const d=document.querySelector("#proxy-setup-tech");return {summary:d.querySelector("summary").textContent,open:d.open,terms:[...d.querySelectorAll("dt")].map(x=>x.textContent),units:d.querySelectorAll("[data-unit]").length,history:!!d.querySelector("#proxy-setup-history")}})()');
|
||||
assert.deepEqual(tech, { summary: 'Technical details: release, service, history', open: false, terms: ['Release', 'Initialized', 'Service', 'Fence', 'Init lock', 'Service key'], units: 4, history: true });
|
||||
assert.equal(await b.evaluate(`${visible('#proxy-setup dt, #proxy-setup [data-unit]')}.length`), 0, 'nothing technical shows until it is opened');
|
||||
// Technical details opens by keyboard and shows the controls.
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech summary").focus()');
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {w15Fixture} from './w15-fixture.mjs';
|
||||
import {publicProviders} from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
|
||||
const rev='a'.repeat(64), long='a'.repeat(64);
|
||||
const instances={revision:rev,launch:true,instances:{pm:{definition:'coder',launched:true,kept:false,running:['run-1'],protected:false,endProtected:false},[long]:{definition:'coder',launched:true,kept:false,running:[long],protected:false,endProtected:false}},definitions:{coder:{accepted:true,services:[]}},noHire:[],retired:[]};
|
||||
const launch={revision:rev,models:{opus:{provider:'anthropic',model:'opus'}},limits:{},usage:{},assistants:{assistant:{definition:'assistant',vars:{},tracker:null}}};
|
||||
const routes={revision:rev,harnesses:['claude-code','codex'],models:['opus'],levels:{'claude-code':['high','xhigh'],codex:['high','xhigh']},instances:{pm:{harness:'claude-code',model:'opus',thinking:'high'}}};
|
||||
const scenario={'/api/config':{devOwner:true,send:true,decide:true,edit:true},'/api/settings':{launch,notifier:{binding:null}},'/api/bus/instances':{rows:[instances]},'/api/bus/routes':{rows:[routes]},'/api/bus/names':{rows:[{instance:'assistant',name:'Piper'}]},'/api/bus/limits':{rows:[]},'/api/bus/usage':{rows:[]},'/api/proxy/accounts':{accounts:[{accountId:'fixture',displayName:'Fixture',provider:'claude',status:'ready',priority:100}]},'/api/proxy/keys':{keys:[]}};
|
||||
const scenario={'/api/config':{devOwner:true,send:true,decide:true,edit:true},'/api/settings':{launch,notifier:{binding:null}},'/api/bus/instances':{rows:[instances]},'/api/bus/routes':{rows:[routes]},'/api/bus/names':{rows:[{instance:'assistant',name:'Piper'}]},'/api/bus/limits':{rows:[]},'/api/bus/usage':{rows:[]},'/api/proxy/accounts':{accounts:[{accountId:'fixture',displayName:'Fixture',provider:'claude',status:'ready',priority:100}],providers:publicProviders()},'/api/proxy/keys':{keys:[]}};
|
||||
async function page(t,section,width=1440,options={}) {
|
||||
const x=await w15Fixture(t,{scenario}); const {b,f,wait}=x;
|
||||
if(options.poll)await b.call('Page.addScriptToEvaluateOnNewDocument',{source:`(()=>{const original=setTimeout;window.setTimeout=(fn,ms,...args)=>{if(ms===10000)window.__pollRefresh=fn;return original(fn,ms,...args)}})()`});
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
// Part B is the single-business Console with every API answer explicit (w15-fixture.mjs).
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { publicProviders } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { startServer } from '../src/serve.mjs';
|
||||
import { createDevOwner } from '../src/dev-owner.mjs';
|
||||
@@ -57,7 +58,7 @@ async function orgFixture() {
|
||||
// The instance owner's flags on /api/config and the model service status, by path suffix (display.js
|
||||
// scopes some reads under /api/orgs/<id>/). Everything else is the real server.
|
||||
const SERVICE_UP = { next: ['accounts'], init: { initialized: true, files: [] }, release: { fetched: true, verified: true, version: '8.0.42', panelVersion: '1.26.7' },
|
||||
unit: { unit: 'mosaic-proxy.service', installed: true, active: 'active', port: 18432, listening: true, healthy: true }, fence: { set: false }, initLock: { present: false, blockers: [] }, fetch: { state: 'idle' }, organizations: [], ride: [] };
|
||||
unit: { unit: 'mosaic-proxy.service', installed: true, active: 'active', port: 18432, listening: true, healthy: true }, fence: { set: false }, initLock: { present: false, blockers: [] }, fetch: { state: 'idle' }, organizations: [], ride: [], providers: publicProviders() };
|
||||
const ownerStub = (service = SERVICE_UP, extra = {}) => `(() => {
|
||||
const real = window.fetch.bind(window); window.__s4 = ${JSON.stringify({ service, accounts: [], routes: null, posts: {}, ...extra })}; window.__writes = [];
|
||||
const json = (body, status = 200) => new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } });
|
||||
@@ -205,12 +206,53 @@ test('settings-s4 review N8: no paused mark while the service runs and only a fe
|
||||
|
||||
// ---- Part B: Model setup ---------------------------------------------------------------------
|
||||
const T = '2026-11-11T12:00:00.000Z';
|
||||
// #1873 settings-backend-a3: the provider select and its help text come from status.providers, the
|
||||
// plugin's list as id, label and sign-in kind. No list, or an empty one, leaves no select and one line.
|
||||
const selectNow = b => b.evaluate(`(() => { const s = document.querySelector("#proxy-provider"); return { options: [...s.options].map(o => [o.value, o.textContent]),
|
||||
hidden: s.hidden, labels: [...s.labels].map(l => l.hidden), help: document.querySelector("#proxy-provider-help").textContent, go: document.querySelector("#proxy-login-go").hidden }; })()`);
|
||||
const READING = '!/^Reading/.test(document.querySelector("#proxy-provider-help")?.textContent ?? "Reading")';
|
||||
test('settings-backend-a3: the provider select\'s options are status.providers, with the help text built from them', { timeout: 90000 }, async t => {
|
||||
const { b, wait } = await orgSetup(t, { path: '/#/settings/model-setup' });
|
||||
await wait(READING);
|
||||
const now = await selectNow(b);
|
||||
assert.deepEqual(now.options, SERVICE_UP.providers.map(p => [p.id, `${p.label} (${p.signIn === 'oauth' ? 'subscription' : 'API key'})`]));
|
||||
assert.deepEqual([now.hidden, now.labels, now.go], [false, [false], false]);
|
||||
assert.equal(now.help, "Claude and ChatGPT / Codex: you sign in on the provider's site in a new tab. Nothing you type there comes to this page. z.ai: you paste an API key from your z.ai account.");
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-login-go").textContent'), 'Sign in to Claude');
|
||||
await b.evaluate('(() => { const s = document.querySelector("#proxy-provider"); s.value = "z.ai"; s.dispatchEvent(new Event("change")); return true; })()');
|
||||
assert.deepEqual(await b.evaluate('[document.querySelector("#proxy-apikey").hidden, document.querySelector("#proxy-login-go").hidden, document.querySelector("#proxy-apikey-label").textContent, document.querySelector("#proxy-apikey-provider").textContent]'),
|
||||
[false, true, 'z.ai API key', 'z.ai']);
|
||||
});
|
||||
test('settings-backend-a3: a provider the status leaves out is gone from the select and its help text', { timeout: 90000 }, async t => {
|
||||
const { b, wait } = await orgSetup(t, { path: '/#/settings/model-setup', service: { ...SERVICE_UP, providers: SERVICE_UP.providers.filter(p => p.id !== 'codex') } });
|
||||
await wait(READING);
|
||||
const now = await selectNow(b);
|
||||
assert.deepEqual(now.options, [['claude', 'Claude (subscription)'], ['z.ai', 'z.ai (API key)']]);
|
||||
assert.equal(now.help, "Claude: you sign in on the provider's site in a new tab. Nothing you type there comes to this page. z.ai: you paste an API key from your z.ai account.");
|
||||
});
|
||||
for (const [name, providers] of [['no providers field', undefined], ['an empty list', []], ['a list of nothing usable', [{ id: 'x' }, null, 'claude']]]) {
|
||||
test(`settings-backend-a3: a status with ${name} shows no select and says the providers could not be read`, { timeout: 90000 }, async t => {
|
||||
const { providers: _, ...rest } = SERVICE_UP;
|
||||
const { b, wait } = await orgSetup(t, { path: '/#/settings/model-setup', service: providers === undefined ? rest : { ...rest, providers } });
|
||||
await wait(READING);
|
||||
const now = await selectNow(b);
|
||||
assert.deepEqual(now, { options: [], hidden: true, labels: [true], help: 'The providers could not be read, so no account can be added here.', go: true });
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-apikey").hidden'), true);
|
||||
});
|
||||
}
|
||||
test('settings-backend-a3: a status read that fails (an answer that is no status) says the providers could not be read', { timeout: 90000 }, async t => {
|
||||
const { b, wait } = await orgSetup(t, { path: '/#/settings/model-setup', service: null });
|
||||
await wait(READING);
|
||||
assert.deepEqual(await selectNow(b), { options: [], hidden: true, labels: [true], help: 'The providers could not be read, so no account can be added here.', go: true });
|
||||
});
|
||||
|
||||
const fresh = {
|
||||
release: { fetched: false, verified: false, version: '8.0.42', panelVersion: '1.26.7' },
|
||||
init: { initialized: false, partial: false, files: [] },
|
||||
unit: { unit: 'mosaic-proxy.service', installed: false, active: 'inactive' },
|
||||
fence: { set: false }, initLock: { present: false, clearable: false, blockers: [] }, fetch: { state: 'idle' }, restart: null, busy: null,
|
||||
organizations: [{ id: 'acme', proxy: 'shared', catalogue: null }], ownProxy: { available: false, state: 'not built', reason: 'One proxy per instance.' }, next: ['fetch'],
|
||||
providers: publicProviders(),
|
||||
};
|
||||
const fetched = { ...fresh, release: { ...fresh.release, fetched: true, verified: true }, fetch: { state: 'done', receipt: { op: 'fetch', time: T, version: '8.0.42' } }, next: ['init'] };
|
||||
const configured = { ...fetched, init: { initialized: true, partial: false, settings: { port: 18432 }, files: [] }, next: ['unit-install'] };
|
||||
|
||||
@@ -0,0 +1,516 @@
|
||||
// #1873 settings-s5b (DESIGN.md 3.1, mockups models-setup-failed and models-advanced): Model setup
|
||||
// shows the service's last lines (GET /api/proxy/admin/log) on step 1 when setup stopped and under
|
||||
// Technical details, when the service key was made (GET /api/proxy/admin/service-key), and the setup
|
||||
// history a page at a time (GET /api/proxy/admin/history). All three are the instance owner's: a page
|
||||
// without proxyAdmin shows none of them and sends no /api/proxy/admin/ request. Also: a focus request
|
||||
// for an organization's Models outcome line is dropped when the owner goes to any other page.
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { startServer } from '../src/serve.mjs';
|
||||
import { createDevOwner } from '../src/dev-owner.mjs';
|
||||
import { browser } from './browser.mjs';
|
||||
import { close } from './fixture.mjs';
|
||||
|
||||
const LIST = [
|
||||
{ id: 'acme', label: 'Acme Studio', theme: 'cobalt', busHealthy: true },
|
||||
{ id: 'gamma', label: 'Gamma Home', theme: 'rosewood', busHealthy: true },
|
||||
];
|
||||
const VIEW = {
|
||||
business: 'acme', revision: 'a'.repeat(64), harnesses: ['pi'], models: ['fixture-deterministic'],
|
||||
providerModels: { 'fixture-deterministic': { module: 'fixture', upstream: 'fixture', model: 'fixture-deterministic' } },
|
||||
levels: { pi: ['off', 'low'] }, instances: {}, catalogue: { status: 'ready', observedAt: new Date().toISOString(), models: [], notices: [] },
|
||||
};
|
||||
|
||||
async function orgFixture() {
|
||||
const { checkRootAgents } = await import('../../organization/src/index.mjs');
|
||||
const rolesDir = fileURLToPath(new URL('../../../roles', import.meta.url));
|
||||
const root = { state: 'ok', ...checkRootAgents({ agentsVersion: 1, agents: {} }, { rolesDir }) };
|
||||
const empty = () => Promise.resolve([]);
|
||||
const member = org => ({
|
||||
id: org.id, doc: { appearance: { theme: org.theme }, roles: { chief: { definition: 'chief-of-staff' } }, projects: {} },
|
||||
status: async () => ({ healthy: true, pid: 4242, boot: null }),
|
||||
options: () => ({ bus: { inbox: empty, agents: empty, 'thread.claims': empty, tasks: empty, usage: () => Promise.resolve({}), limits: () => Promise.resolve({}), decisions: empty }, chat: { threads: empty, projects: empty } }),
|
||||
});
|
||||
const organizations = {
|
||||
list: async () => LIST,
|
||||
members: async () => LIST.map(member),
|
||||
resolve: async id => {
|
||||
const org = LIST.find(o => o.id === id); if (!org) return null;
|
||||
return {
|
||||
busHealthy: true,
|
||||
reads: { business: async () => ({ business: { id, human: 'jason', projects: [], appearance: { theme: org.theme } } }), settings: async () => ({ business: id, release: 'fixture', notifier: {} }), queue: async () => ({ rows: [] }), busStatus: async () => ({}) },
|
||||
bus: { messages: async () => ({ messages: [], unread: 0, total: 0 }), agents: empty, 'thread.claims': empty, inbox: empty, tasks: empty, usage: async () => ({}), routes: async () => ({ ...VIEW, business: id }), instances: async () => ({ business: id, revision: 'a'.repeat(64), launch: true, noHire: [], retired: [], definitions: {}, instances: {} }) },
|
||||
chat: { projects: empty, threads: empty },
|
||||
};
|
||||
},
|
||||
};
|
||||
return { organizations, agentLevels: { loadRoot: () => root, rolesDir, ctx: { configDir: '/cfg', dataRoot: '/data', instance: 'test' } } };
|
||||
}
|
||||
|
||||
const T = n => `2026-11-19T10:0${n}:00.000Z`;
|
||||
const SERVICE_UP = { next: ['accounts'], init: { initialized: true, partial: false, problem: null, settings: { port: 18432, rotateGraceMinutes: 30, firstOutputSeconds: 90 }, files: [] },
|
||||
release: { fetched: true, verified: true, version: '8.0.42', panelVersion: '1.26.7' },
|
||||
unit: { unit: 'mosaic-proxy.service', installed: true, active: 'active', port: 18432, listening: true, healthy: true }, fence: { set: false }, initLock: { present: false, blockers: [] }, fetch: { state: 'idle' }, organizations: [], ride: [] };
|
||||
// Setup stopped: the download failed, so step 1 says so and opens the service's last lines.
|
||||
const SERVICE_FAILED = { ...SERVICE_UP, next: ['fetch'], release: { fetched: false, verified: false, version: '8.0.42', panelVersion: '1.26.7' },
|
||||
init: { initialized: false, partial: false, problem: null, files: [] }, unit: { unit: 'mosaic-proxy.service', installed: false, active: 'inactive' },
|
||||
fetch: { state: 'failed', error: 'proxy-install-invalid' } };
|
||||
// Setup stopped at the start step: the service is installed but did not answer, so the start was refused.
|
||||
const SERVICE_START = { ...SERVICE_UP, next: ['unit-start'], unit: { unit: 'mosaic-proxy.service', installed: true, active: 'inactive', port: 18432, listening: false, healthy: false } };
|
||||
const START_REFUSED = { 'POST unit/start': { status: 503, body: { error: 'proxy-unit-unhealthy' } } };
|
||||
const LOG_OK = { unit: 'mosaic-proxy.service', lines: ['Nov 19 10:00:01 host proxy[4242]: listening on 127.0.0.1:18432', 'Nov 19 10:00:02 host proxy[4242]: loaded key [redacted]'], redacted: 1, dropped: 2 };
|
||||
const KEY_OK = { madeAt: T(3), renewedAt: null, renewal: 'not-built', problem: null };
|
||||
// Three pages, newest first: the first holds four entries (one withheld), the second is empty but
|
||||
// names a third, and the third holds the oldest entry.
|
||||
const PAGE1 = { entries: [{ op: 'unit.start', time: T(4), user: 'jason' }, { op: 'init', time: T(5), user: '[redacted]' }, { withheld: true },
|
||||
{ op: 'account.proxy.apikey.add', time: T(7), org: 'acme', accountId: 'a1b2c3d4e5f6', user: 'jason' }], next: 3 };
|
||||
const PAGE2 = { entries: [], next: 1 };
|
||||
const PAGE3 = { entries: [{ op: 'ride-move', time: T(1), org: 'gamma', user: 'jason' }], next: null };
|
||||
const PAGES = { 'history?limit=25': PAGE1, 'history?limit=25&before=3': PAGE2, 'history?limit=25&before=1': PAGE3 };
|
||||
const LOG = 'log?lines=20';
|
||||
const OK = { [LOG]: LOG_OK, 'service-key': KEY_OK, ...PAGES };
|
||||
|
||||
// The instance owner's flags (or not), the setup status, and the three reads by name and query. An
|
||||
// answer may be a list: each read takes the next one and the last one stays. window.__admin records
|
||||
// every /api/proxy/admin/ request the page sends, window.__holds pauses a read by name, and window.__queue
|
||||
// pauses every request to a name, each until its own resolver runs. A write answers from admin['POST name'].
|
||||
const stub = ({ owner = true, service = SERVICE_UP, admin = OK } = {}) => `(() => {
|
||||
const real = window.fetch.bind(window);
|
||||
window.__s5b = ${JSON.stringify({ owner, service, admin })}; window.__admin = []; window.__writes = []; window.__holds = {}; window.__queue = {};
|
||||
const json = (body, status = 200) => new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } });
|
||||
window.fetch = async (input, init = {}) => {
|
||||
const url = new URL(typeof input === 'string' ? input : input.url, location.href), path = url.pathname, method = (init.method || 'GET').toUpperCase();
|
||||
const at = path.indexOf('/proxy/admin/');
|
||||
if (at >= 0) {
|
||||
const name = path.slice(at + 13) + url.search; window.__admin.push(method + ' ' + name);
|
||||
if (method !== 'GET') { window.__writes.push(path); const w = __s5b.admin[method + ' ' + name]; return w ? json(w.body, w.status) : json({ error: 'stub' }, 500); }
|
||||
if (name === 'status') return json(__s5b.service);
|
||||
if (window.__holds[name]) await new Promise(r => { window.__holds[name] = r; });
|
||||
if (window.__queue[name]) await new Promise(r => window.__queue[name].push(r));
|
||||
let a = __s5b.admin[name];
|
||||
if (Array.isArray(a)) a = a.length > 1 ? a.shift() : a[0];
|
||||
if (!a) return json({ error: 'fixture-unexpected-api' }, 403);
|
||||
return a.status ? json(a.body, a.status) : json(a);
|
||||
}
|
||||
if (method !== 'GET' && /\\/proxy\\//.test(path)) { window.__writes.push(path); return json({ error: 'stub' }, 500); }
|
||||
if (method !== 'GET') return real(input, init);
|
||||
if (/\\/api\\/(orgs\\/[^/]+\\/)?config$/.test(path)) { const c = await (await real(input, init)).json(); return json({ ...c, orgSetup: true, proxyAdmin: __s5b.owner, proxyAdminSend: __s5b.owner, ...(/\\/orgs\\//.test(path) ? { settingsSend: true } : {}) }); }
|
||||
if (path.endsWith('/proxy/plugins')) return json({ revision: 'b'.repeat(64), available: { harness: ['pi'], 'model-provider': ['fixture', 'cliproxy'] }, plugins: { harness: ['pi'], 'model-provider': ['fixture'] } });
|
||||
if (path.endsWith('/proxy/accounts')) return json({ accounts: [] });
|
||||
if (path.endsWith('/proxy/keys')) return json({ keys: [] });
|
||||
if (path.endsWith('/triage/route')) return json({ route: null });
|
||||
return real(input, init);
|
||||
};
|
||||
})();`;
|
||||
|
||||
async function setup(t, { path = '/#/settings/model-setup', viewport = [1440, 900], ...options } = {}) {
|
||||
const { organizations, agentLevels } = await orgFixture();
|
||||
const server = await startServer({ port: 0, sso: createDevOwner(), organizations, agentLevels, triage: null, send: async () => ({}), log: () => {} });
|
||||
t.after(() => close(server));
|
||||
const b = await browser({ machine: false }); t.after(() => b.close());
|
||||
await b.viewport(...viewport);
|
||||
await b.call('Page.addScriptToEvaluateOnNewDocument', { source: stub(options) });
|
||||
await b.navigate(`http://127.0.0.1:${server.address().port}${path}`);
|
||||
const wait = expression => b.evaluate(`(async()=>{for(let i=0;i<250;i++){if(${expression})return true;await new Promise(r=>setTimeout(r,30))}throw Error("wait failed: "+${JSON.stringify(expression)}+" HASH "+location.hash)})()`);
|
||||
const text = sel => b.evaluate(`document.querySelector(${JSON.stringify(sel)})?.textContent ?? null`);
|
||||
const click = sel => b.evaluate(`(() => { const el = document.querySelector(${JSON.stringify(sel)}); if (!el) throw Error("no ${sel.replace(/"/g, '')}"); el.click(); return true; })()`);
|
||||
const reads = () => b.evaluate('window.__admin.filter(r => r !== "GET status")');
|
||||
return { b, wait, text, click, reads };
|
||||
}
|
||||
const READY = 'document.querySelector("#proxy-setup-state dd") && !document.querySelector("#setup-step-service").hidden';
|
||||
const openTech = b => b.evaluate('(() => { const d = document.querySelector("#proxy-setup-tech"); d.open = true; return true; })()');
|
||||
const items = b => b.evaluate('[...document.querySelectorAll("#proxy-setup-history li")].map(li => ({ text: li.textContent, hidden: li.hidden, withheld: li.hasAttribute("data-withheld") }))');
|
||||
const KEY_TEXT = /ms[kotp]-[A-Za-z0-9_-]{4,}/i;
|
||||
|
||||
// ---- Each part renders from its read ---------------------------------------------------------
|
||||
test('settings-s5b: Technical details shows when the service key was made, the service\'s last lines and the newest history, and reads none of them until it opens', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, click, reads } = await setup(t);
|
||||
await wait(READY);
|
||||
await new Promise(r => setTimeout(r, 300));
|
||||
assert.deepEqual(await reads(), [], 'nothing is read before Technical details opens');
|
||||
await openTech(b);
|
||||
await wait('/Mosaic made it on/.test(document.querySelector("#proxy-setup-keymade-text")?.textContent ?? "") && document.querySelectorAll("#proxy-setup-history li").length === 4');
|
||||
// The service key: when it was made, that renewal is not built, never the key.
|
||||
// It is the last row of the state list, after Release and Service, as models-advanced shows it.
|
||||
assert.equal(await b.evaluate('[...document.querySelectorAll("#proxy-setup-state dt")].at(-1).textContent'), 'Service key');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-keymade-text").closest("dl").id'), 'proxy-setup-state');
|
||||
const key = await text('#proxy-setup-keymade-text');
|
||||
assert.match(key, /^Mosaic made it on .+\. You never see it\. Renewing it is not built yet\.$/);
|
||||
assert.equal(key.includes(await b.evaluate(`window.MosaicLocalTime(${JSON.stringify(T(3))})`)), true);
|
||||
// History, newest first, three shown with Show all; the withheld entry is a plain row.
|
||||
const list = await items(b);
|
||||
assert.deepEqual(list.map(i => i.hidden), [false, false, false, true]);
|
||||
assert.match(list[0].text, /^Account added in Acme Studio · .+ · by jason$/);
|
||||
assert.equal(list[1].text, 'Withheld. The Console kept this entry back because it may hold a secret.');
|
||||
assert.equal(list[1].withheld, true);
|
||||
assert.match(list[2].text, /^Set up · .+ · by someone whose name Mosaic hid$/);
|
||||
assert.match(list[3].text, /^Started · .+ · by jason$/);
|
||||
assert.equal(await text('#proxy-setup-history-all'), 'Show all');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").hidden'), true);
|
||||
// The lines: read when their box opens, shown as the answer has them, with what was removed.
|
||||
assert.deepEqual(await reads(), ['GET service-key', 'GET history?limit=25', 'GET access']);
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech-lines").open = true; true');
|
||||
await wait('document.querySelector("#proxy-setup-tech-lines pre.setup-log")');
|
||||
assert.equal(await text('#proxy-setup-tech-lines summary'), 'The service\'s last lines');
|
||||
assert.equal(await text('#proxy-setup-tech-lines pre.setup-log'), LOG_OK.lines.join('\n'));
|
||||
assert.equal(await text('#proxy-setup-tech-lines [data-log] p'), 'Mosaic removes anything that looks like a key. It removed something from 1 line. 2 lines were left out.');
|
||||
assert.deepEqual(await reads(), ['GET service-key', 'GET history?limit=25', 'GET access', 'GET log?lines=20']);
|
||||
// Read again reads the lines once more.
|
||||
await click('#proxy-setup-tech-lines [data-log] button');
|
||||
await wait('window.__admin.filter(r => r === "GET log?lines=20").length === 2 && document.querySelector("#proxy-setup-tech-lines pre.setup-log")');
|
||||
});
|
||||
|
||||
test('settings-s5b: when setup stops, step 1 opens the service\'s last lines from the log read', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, reads } = await setup(t, { service: SERVICE_START, admin: { ...OK, ...START_REFUSED } });
|
||||
await wait(READY + ' && document.querySelector("#proxy-setup-go") && !document.querySelector("#proxy-setup-go").hidden');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-go").click(); true');
|
||||
await wait('document.querySelector("#proxy-setup-lines pre.setup-log")');
|
||||
assert.deepEqual(await b.evaluate('window.__writes.map(p => p.slice(p.indexOf("/proxy/admin/") + 13))'), ['unit/start']);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-lines").hidden'), false);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-lines").open'), true);
|
||||
assert.equal(await text('#proxy-setup-lines pre.setup-log'), LOG_OK.lines.join('\n'));
|
||||
assert.match(await text('#proxy-setup-lines [data-log] p'), /^Mosaic removes anything that looks like a key\./);
|
||||
assert.doesNotMatch(await text('#proxy-setup-lines'), /can't read the service's lines yet|Nothing reported/);
|
||||
// The status read polls; the lines are read once, not on every paint.
|
||||
await new Promise(r => setTimeout(r, 1500));
|
||||
assert.deepEqual(await reads(), ['POST unit/start', 'GET log?lines=20']);
|
||||
});
|
||||
|
||||
test('settings-s5b: a service that is not installed has no lines, and step 1 says so plainly, not as a failed read', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text } = await setup(t, { service: SERVICE_FAILED, admin: { ...OK, [LOG]: { status: 503, body: { error: 'not-configured' } } } });
|
||||
await wait('document.querySelector("#proxy-setup-lines [data-log] p")');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-lines").open'), true);
|
||||
assert.equal(await text('#proxy-setup-lines [data-log] p'), 'The model service is not installed, so it has no lines yet.');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-lines [data-log] p").className'), 'small muted');
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#proxy-setup-lines .banner.err")'), false);
|
||||
assert.doesNotMatch(await text('#proxy-setup-lines'), /could not be read/);
|
||||
});
|
||||
|
||||
test('settings-s5b: a service key that is not made yet, or that cannot be checked, says so', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text } = await setup(t, { admin: { ...OK, 'service-key': [{ madeAt: null, renewedAt: null, renewal: 'not-built', problem: 'missing' }, { madeAt: null, renewedAt: null, renewal: 'not-built', problem: 'unreadable' }] } });
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('/Not made yet/.test(document.querySelector("#proxy-setup-keymade-text")?.textContent ?? "")');
|
||||
assert.equal(await text('#proxy-setup-keymade-text'), 'Not made yet. Set up model service in step 1 makes it. You never see it.');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = false; true');
|
||||
await new Promise(r => setTimeout(r, 100));
|
||||
await openTech(b);
|
||||
await wait('/can.t tell/.test(document.querySelector("#proxy-setup-keymade-text")?.textContent ?? "")');
|
||||
assert.match(await text('#proxy-setup-keymade-text'), /^Mosaic can't tell when it was made/);
|
||||
});
|
||||
|
||||
// ---- Failed reads -----------------------------------------------------------------------------
|
||||
test('settings-s5b: each failed read shows its line with Read again, and Read again works', { timeout: 90000 }, async t => {
|
||||
const fail = code => ({ status: 503, body: { error: code } });
|
||||
const { b, wait, text, click, reads } = await setup(t, { admin: { ...OK, [LOG]: [fail('outcome-unknown'), LOG_OK], 'service-key': [fail('not-configured'), KEY_OK], 'history?limit=25': [fail('outcome-unknown'), PAGE1] } });
|
||||
await wait(READY); await openTech(b);
|
||||
// The service key.
|
||||
await wait('document.querySelector("#proxy-setup-keymade-retry")');
|
||||
assert.equal(await text('#proxy-setup-keymade-text'), 'When it was made could not be read. Model service setup is not available on this Console. (not-configured)');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-keymade-text").className'), 'banner err', 'the same failure style as the history and the lines');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-keymade-retry").focus(); document.querySelector("#proxy-setup-keymade-retry").click(); true');
|
||||
await wait('/Mosaic made it on/.test(document.querySelector("#proxy-setup-keymade-text")?.textContent ?? "")');
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#proxy-setup-keymade-retry")'), false);
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-keymade-text', 'focus stays on the answer, not the page');
|
||||
// History.
|
||||
await wait('!document.querySelector("#proxy-setup-history-retry").hidden');
|
||||
assert.equal(await text('#proxy-setup-history-message'), 'History could not be read. The host could not read the history. (outcome-unknown)');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-message").className'), 'banner err');
|
||||
assert.equal(await b.evaluate('document.querySelectorAll("#proxy-setup-history li").length'), 0);
|
||||
await click('#proxy-setup-history-retry');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-retry").hidden'), true);
|
||||
assert.equal(await text('#proxy-setup-history-message'), '');
|
||||
// The lines.
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech-lines").open = true; true');
|
||||
await wait('document.querySelector("#proxy-setup-tech-lines [data-log] .banner.err")');
|
||||
assert.equal(await text('#proxy-setup-tech-lines [data-log] .banner.err'), 'The service\'s lines could not be read. The host could not read the service\'s lines. (outcome-unknown)');
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#proxy-setup-tech-lines pre")'), false);
|
||||
await b.evaluate('(() => { const r = document.querySelector("#proxy-setup-tech-lines [data-log] button"); r.focus(); r.click(); return true; })()');
|
||||
await wait('document.querySelector("#proxy-setup-tech-lines pre.setup-log")');
|
||||
assert.equal(await b.evaluate('document.activeElement?.closest("[data-log]") !== null && document.activeElement.textContent === "Read again"'), true);
|
||||
assert.deepEqual(await reads(), ['GET service-key', 'GET history?limit=25', 'GET access', 'GET service-key', 'GET history?limit=25', 'GET log?lines=20', 'GET log?lines=20']);
|
||||
});
|
||||
|
||||
// ---- Owner only -------------------------------------------------------------------------------
|
||||
test('settings-s5b: a page that is not the instance owner\'s shows none of the three and sends no /api/proxy/admin/ request', { timeout: 90000 }, async t => {
|
||||
// The owner's page, as the control: the three parts are there and read.
|
||||
{
|
||||
const { b, wait } = await setup(t, { service: SERVICE_FAILED });
|
||||
await wait('document.querySelector("#proxy-setup-lines pre.setup-log")');
|
||||
await openTech(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4 && /Mosaic made it/.test(document.querySelector("#proxy-setup-keymade-text").textContent)');
|
||||
}
|
||||
const { b, wait } = await setup(t, { owner: false, service: SERVICE_FAILED });
|
||||
await wait('window.MosaicProxy && document.querySelector("#proxy-setup") && document.querySelector("#settings-page-title")');
|
||||
await new Promise(r => setTimeout(r, 800));
|
||||
// Opening every box on the page, even the hidden ones, still reads nothing.
|
||||
await b.evaluate('document.querySelectorAll("#proxy-setup details").forEach(d => { d.open = true; }); true');
|
||||
await new Promise(r => setTimeout(r, 800));
|
||||
assert.deepEqual(await b.evaluate('window.__admin'), []);
|
||||
const shown = sel => b.evaluate(`(() => { const e = document.querySelector(${JSON.stringify(sel)}); return !!e && e.getClientRects().length > 0; })()`);
|
||||
assert.equal(await b.evaluate('[...document.querySelectorAll("#proxy-setup dt")].some(d => d.textContent === "Service key")'), false);
|
||||
for (const sel of ['#proxy-setup-keymade-text', '#proxy-setup-keymade-retry', '#proxy-setup-tech-lines', '#proxy-setup-lines', '#proxy-setup-history', '#proxy-setup-history-message', '#proxy-setup-history-all'])
|
||||
assert.equal(await shown(sel), false, `${sel} is not shown`);
|
||||
assert.equal(await b.evaluate('document.querySelectorAll("[data-log] pre, [data-log] .banner, #proxy-setup-history li").length'), 0);
|
||||
});
|
||||
|
||||
// ---- History paging ---------------------------------------------------------------------------
|
||||
test('settings-s5b: history pages back one page at a time, shows each entry once, and an empty page with an older one still offers it', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, click, reads } = await setup(t);
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4');
|
||||
await click('#proxy-setup-history-all');
|
||||
assert.deepEqual((await items(b)).map(i => i.hidden), [false, false, false, false]);
|
||||
assert.equal(await text('#proxy-setup-history-all'), 'Show fewer');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-all").getAttribute("aria-expanded")'), 'true');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").hidden'), false);
|
||||
// Two quick presses send one read.
|
||||
await b.evaluate('window.__holds["history?limit=25&before=3"] = true; true');
|
||||
await b.evaluate('(() => { const o = document.querySelector("#proxy-setup-history-older"); o.focus(); o.click(); o.click(); return true; })()');
|
||||
await wait('typeof window.__holds["history?limit=25&before=3"] === "function"');
|
||||
assert.equal(await text('#proxy-setup-history-older'), 'Reading…');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").disabled'), true);
|
||||
await b.evaluate('window.__holds["history?limit=25&before=3"](); true');
|
||||
// The empty page: no new entry, a line that says so, and Show older still there.
|
||||
await wait('/no entries to show/.test(document.querySelector("#proxy-setup-history-message").textContent)');
|
||||
assert.equal(await text('#proxy-setup-history-message'), 'That page of history had no entries to show. Show older reads further back.');
|
||||
assert.equal(await b.evaluate('document.querySelectorAll("#proxy-setup-history li").length'), 4);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").hidden'), false);
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-history-older');
|
||||
// Folding the list keeps Show older, because the line above still names it.
|
||||
await click('#proxy-setup-history-all');
|
||||
assert.equal(await text('#proxy-setup-history-all'), 'Show all');
|
||||
assert.equal(await text('#proxy-setup-history-message'), 'That page of history had no entries to show. Show older reads further back.');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").hidden'), false);
|
||||
await click('#proxy-setup-history-all');
|
||||
await click('#proxy-setup-history-older');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 5');
|
||||
const list = await items(b);
|
||||
assert.match(list[4].text, /^Gamma Home switched to your models · .+ · by jason$/);
|
||||
assert.equal(new Set(list.map(i => i.text)).size, list.length, 'each entry once');
|
||||
// The last page: no Show older, focus goes to Show fewer, no empty-page line.
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").hidden'), true);
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-history-all');
|
||||
assert.equal(await text('#proxy-setup-history-message'), '');
|
||||
assert.deepEqual(await reads(), ['GET service-key', 'GET history?limit=25', 'GET access', 'GET history?limit=25&before=3', 'GET history?limit=25&before=1']);
|
||||
// Closing and opening Technical details starts from the newest page again.
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = false; true');
|
||||
await new Promise(r => setTimeout(r, 100));
|
||||
await openTech(b);
|
||||
await wait('window.__admin.filter(r => r === "GET history?limit=25").length === 2 && document.querySelectorAll("#proxy-setup-history li").length === 4');
|
||||
assert.equal(await text('#proxy-setup-history-all'), 'Show all');
|
||||
});
|
||||
|
||||
test('settings-s5b: a history with no entries says so, and a first page that is empty but names an older one offers it', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, click } = await setup(t, { admin: { ...OK, 'history?limit=25': [{ entries: [], next: null }, { entries: [], next: 1 }] } });
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('document.querySelector("#proxy-setup-history-message").textContent === "No setup history yet."');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-all").hidden'), true);
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = false; true');
|
||||
await new Promise(r => setTimeout(r, 100));
|
||||
await openTech(b);
|
||||
await wait('/no entries to show/.test(document.querySelector("#proxy-setup-history-message").textContent)');
|
||||
// Nothing is folded away, so there is no Show all, and Show older is offered at once.
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-all").hidden'), true);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").hidden'), false);
|
||||
await click('#proxy-setup-history-older');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 1');
|
||||
assert.match((await items(b))[0].text, /^Gamma Home switched to your models/);
|
||||
assert.equal(await text('#proxy-setup-history-message'), '');
|
||||
});
|
||||
|
||||
test('settings-s5b: a short first page offers Show older at once, and what it reads stays open', { timeout: 90000 }, async t => {
|
||||
const e = (op, n) => ({ op, time: T(n), user: 'jason' });
|
||||
const { b, wait, click } = await setup(t, { admin: { ...OK, 'history?limit=25': { entries: [e('unit.start', 8), e('unit.stop', 9)], next: 7 }, 'history?limit=25&before=7': { entries: [e('init', 4), e('unit.install', 5), e('unit.restart', 6)], next: null } } });
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 2');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-all").hidden'), true);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-older").hidden'), false);
|
||||
await click('#proxy-setup-history-older');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 5');
|
||||
assert.deepEqual((await items(b)).map(i => i.hidden), [false, false, false, false, false], 'the older entries it read are shown');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-history-all").textContent'), 'Show fewer');
|
||||
});
|
||||
|
||||
test('settings-s5b: an older page that is not further back than the one asked for is not shown', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, click } = await setup(t, { admin: { ...OK, 'history?limit=25&before=3': [{ entries: [{ op: 'init', time: T(2), user: 'jason' }], next: 3 }, PAGE2] } });
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4');
|
||||
await click('#proxy-setup-history-all');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-history-older").focus(); document.querySelector("#proxy-setup-history-older").click(); true');
|
||||
await wait('!document.querySelector("#proxy-setup-history-retry").hidden');
|
||||
assert.match(await text('#proxy-setup-history-message'), /^History could not be read\./);
|
||||
assert.equal(await b.evaluate('document.querySelectorAll("#proxy-setup-history li").length'), 4, 'the repeated page adds nothing');
|
||||
// The pressed button went away, so focus lands on Read again.
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-history-retry');
|
||||
// Read again answers; it goes away, and focus lands on Show older, not Show fewer.
|
||||
await b.evaluate('document.querySelector("#proxy-setup-history-retry").click(); true');
|
||||
await wait('document.querySelector("#proxy-setup-history-retry").hidden && /no entries to show/.test(document.querySelector("#proxy-setup-history-message").textContent)');
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-history-older');
|
||||
});
|
||||
|
||||
test('settings-s5b: a history answer from before Technical details was closed and opened again is dropped', { timeout: 90000 }, async t => {
|
||||
const STALE = { entries: [{ op: 'init', time: T(1), user: 'stale' }], next: null };
|
||||
const { b, wait } = await setup(t, { admin: { ...OK, 'history?limit=25': [PAGE1, STALE] } });
|
||||
await wait(READY);
|
||||
await b.evaluate('window.__queue["history?limit=25"] = []; true');
|
||||
await openTech(b);
|
||||
await wait('window.__queue["history?limit=25"].length === 1');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = false; true');
|
||||
await new Promise(r => setTimeout(r, 100));
|
||||
await openTech(b);
|
||||
await wait('window.__queue["history?limit=25"].length === 2');
|
||||
// The newer read answers first, then the older one.
|
||||
await b.evaluate('window.__queue["history?limit=25"][1](); true');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4');
|
||||
await b.evaluate('window.__queue["history?limit=25"][0](); true');
|
||||
await new Promise(r => setTimeout(r, 500));
|
||||
const list = await items(b);
|
||||
assert.equal(list.length, 4);
|
||||
assert.equal(list.some(i => /stale/.test(i.text)), false, 'the older answer is not painted');
|
||||
assert.equal(new Set(list.map(i => i.text)).size, 4, 'one copy of each entry');
|
||||
});
|
||||
|
||||
test('settings-s5b: a history entry whose action is not a known one reads Other setup action', { timeout: 90000 }, async t => {
|
||||
const { b, wait } = await setup(t, { admin: { ...OK, 'history?limit=25': { entries: [{ op: 'toString', time: T(1), user: 'jason' }, { op: 'constructor', time: T(2), user: 'jason' }, { op: 'something.new', time: T(3), user: 'jason' }], next: null } } });
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 3');
|
||||
for (const item of await items(b)) assert.match(item.text, /^Other setup action · .+ · by jason$/);
|
||||
});
|
||||
|
||||
test('settings-s5b: a withheld entry shows as withheld and nothing else from it', { timeout: 90000 }, async t => {
|
||||
const { b, wait } = await setup(t, { admin: { ...OK, 'history?limit=25': { entries: [{ withheld: true, op: 'init', time: T(2), user: 'jason', org: 'acme' }, { withheld: true }], next: null } } });
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 2');
|
||||
for (const item of await items(b)) {
|
||||
assert.equal(item.text, 'Withheld. The Console kept this entry back because it may hold a secret.');
|
||||
assert.equal(item.withheld, true);
|
||||
}
|
||||
assert.equal(await b.evaluate('[...document.querySelectorAll("#proxy-setup-history li")].every(li => li.classList.contains("muted"))'), true);
|
||||
});
|
||||
|
||||
// ---- No key text ------------------------------------------------------------------------------
|
||||
test('settings-s5b: key-shaped text in an unexpected field of any answer never reaches the page', { timeout: 90000 }, async t => {
|
||||
const k = p => `${p}-AAAAbbbbCCCCdddd0123456789`;
|
||||
const admin = {
|
||||
[LOG]: { unit: k('msk'), lines: ['plain line'], redacted: 0, dropped: 0, key: k('mso'), note: k('mst') },
|
||||
'service-key': { madeAt: k('msk'), renewedAt: k('mso'), renewal: k('mst'), problem: k('msp'), key: k('msk') },
|
||||
'history?limit=25': { entries: [
|
||||
{ op: k('msk'), time: k('mso'), user: k('mst'), org: k('msp'), provider: k('msk'), accountId: k('mso'), models: k('mst'), priority: k('msp'), note: k('msk') },
|
||||
{ op: 'account.proxy.apikey.add', time: T(2), user: `x${k('msk')}`, org: k('mso'), provider: k('mst') },
|
||||
{ op: 'ride-move', time: T(3), org: `${k('msp')}`, user: 'jason' },
|
||||
{ withheld: true, note: k('msk') },
|
||||
], next: null },
|
||||
};
|
||||
{
|
||||
const { b, wait } = await setup(t, { admin });
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4 && !/Reading/.test(document.querySelector("#proxy-setup-keymade-text").textContent)');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-history-all").click(); document.querySelector("#proxy-setup-tech-lines").open = true; true');
|
||||
await wait('document.querySelector("#proxy-setup-tech-lines pre.setup-log")');
|
||||
const page = await b.evaluate('document.querySelector("#proxy-setup").textContent + "\\n" + document.body.innerText');
|
||||
assert.doesNotMatch(page, KEY_TEXT);
|
||||
const list = await items(b);
|
||||
assert.match(list[3].text, /^Other setup action · unknown$/);
|
||||
assert.match(list[2].text, /^Account added · /);
|
||||
assert.match(list[1].text, /^An organization switched to your models · /);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-setup-keymade-text").textContent'), /^When it was made is not known\./);
|
||||
}
|
||||
// A refusal whose code is shaped like a key.
|
||||
const refused = { status: 400, body: { error: 'msk-abcdefghijklmnop' } };
|
||||
const { b, wait } = await setup(t, { admin: { [LOG]: refused, 'service-key': refused, 'history?limit=25': refused } });
|
||||
await wait(READY); await openTech(b);
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech-lines").open = true; true');
|
||||
await wait('document.querySelector("#proxy-setup-keymade-retry") && !document.querySelector("#proxy-setup-history-retry").hidden && document.querySelector("#proxy-setup-tech-lines .banner.err")');
|
||||
const page = await b.evaluate('document.querySelector("#proxy-setup").textContent + "\\n" + document.body.innerText');
|
||||
assert.doesNotMatch(page, KEY_TEXT);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-setup-history-message").textContent'), /^History could not be read\. The Console refused the read\.$/);
|
||||
});
|
||||
|
||||
// ---- Models focus follow-up -------------------------------------------------------------------
|
||||
// The settings-s5 re-check left one window: a switch whose follow-up read is still out when the owner
|
||||
// goes to a page that returns before render's focus chain (Assistant, Projects, the board) kept its focus request.
|
||||
for (const [name, away] of [['Assistant', '#/assistant'], ['the board', '#/board']])
|
||||
test(`settings-s5b: leaving Models for ${name} while a switch's follow-up read is out, the next visit does not focus the outcome line`, { timeout: 90000 }, async t => {
|
||||
const { b, wait, click } = await setup(t, { path: '/#/settings/routing?org=acme' });
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.includes("does not use")');
|
||||
// plugins.set succeeds; the routes read that the render after it awaits waits until released.
|
||||
await b.evaluate(`(() => { const f = window.fetch; window.__sets = 0;
|
||||
window.fetch = async (input, init = {}) => { const u = new URL(String(input?.url ?? input), location.href).pathname, m = (init.method || 'GET').toUpperCase();
|
||||
if (m !== 'GET' && u.endsWith('/proxy/plugins/set')) { window.__sets++; return new Response(JSON.stringify({ revision: 'c'.repeat(64) }), { status: 200, headers: { 'content-type': 'application/json' } }); }
|
||||
if (m === 'GET' && u.endsWith('/proxy/plugins') && window.__sets) return new Response(JSON.stringify({ revision: 'c'.repeat(64), available: { harness: ['pi'], 'model-provider': ['fixture', 'cliproxy'] }, plugins: { harness: ['pi'], 'model-provider': ['fixture', 'cliproxy'] } }), { status: 200, headers: { 'content-type': 'application/json' } });
|
||||
if (m === 'GET' && u.endsWith('/routes') && window.__sets && !window.__released) { await new Promise(r => { window.__release = r; }); window.__released = true; }
|
||||
return f(input, init); }; return true; })()`);
|
||||
await click('#models-yours');
|
||||
await wait('typeof window.__release === "function"');
|
||||
await b.evaluate(`location.hash = ${JSON.stringify(away)}; true`);
|
||||
await wait('!location.hash.startsWith("#/settings/") && !document.querySelector("#models-in-force")');
|
||||
await new Promise(r => setTimeout(r, 300));
|
||||
await b.evaluate('window.__release(); true');
|
||||
await new Promise(r => setTimeout(r, 400));
|
||||
await b.evaluate(`location.hash = '#/settings/routing?org=acme'; true`);
|
||||
await wait('/^Saved\\./.test(document.querySelector("#models-receipt")?.textContent ?? "") && document.querySelector("#models-in-force")?.textContent.includes("uses your models")');
|
||||
await new Promise(r => setTimeout(r, 300));
|
||||
assert.notEqual(await b.evaluate('document.activeElement?.id'), 'models-receipt');
|
||||
});
|
||||
|
||||
// ---- Shots ------------------------------------------------------------------------------------
|
||||
test('shots', { timeout: 900000, skip: !process.env.MOSAIC_UX_SHOTS }, async t => {
|
||||
const { mkdir, writeFile } = await import('node:fs/promises');
|
||||
const { join } = await import('node:path');
|
||||
const dir = process.env.MOSAIC_UX_SHOTS; await mkdir(dir, { recursive: true });
|
||||
const settle = () => new Promise(r => setTimeout(r, 1000));
|
||||
const fail = { status: 503, body: { error: 'outcome-unknown' } };
|
||||
const SHOTS = [
|
||||
{ name: 'setup-start-failed', options: { service: SERVICE_START, admin: { ...OK, ...START_REFUSED } }, ready: READY + ' && document.querySelector("#proxy-setup-go") && !document.querySelector("#proxy-setup-go").hidden',
|
||||
after: async (b, wait) => { await b.evaluate('document.querySelector("#proxy-setup-go").click(); true'); await wait('document.querySelector("#proxy-setup-lines pre.setup-log")'); } },
|
||||
{ name: 'setup-failed-no-lines', options: { service: SERVICE_FAILED, admin: { ...OK, [LOG]: { status: 503, body: { error: 'not-configured' } } } }, ready: 'document.querySelector("#proxy-setup-lines [data-log] p")' },
|
||||
{ name: 'technical-details', ready: READY,
|
||||
after: async (b, wait) => { await b.evaluate('document.querySelector("#proxy-setup-tech").open = true; true');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4 && /Mosaic made it/.test(document.querySelector("#proxy-setup-keymade-text").textContent)'); } },
|
||||
{ name: 'technical-details-all', ready: READY,
|
||||
after: async (b, wait) => { await b.evaluate('document.querySelector("#proxy-setup-tech").open = true; true');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 4');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-history-all").click(); document.querySelector("#proxy-setup-tech-lines").open = true; true');
|
||||
await wait('document.querySelector("#proxy-setup-tech-lines pre.setup-log")'); } },
|
||||
{ name: 'technical-details-failed', options: { admin: { [LOG]: fail, 'service-key': fail, 'history?limit=25': fail } }, ready: READY,
|
||||
after: async (b, wait) => { await b.evaluate('document.querySelector("#proxy-setup-tech").open = true; document.querySelector("#proxy-setup-tech-lines").open = true; true');
|
||||
await wait('document.querySelector("#proxy-setup-keymade-retry") && !document.querySelector("#proxy-setup-history-retry").hidden && document.querySelector("#proxy-setup-tech-lines .banner.err")'); } },
|
||||
];
|
||||
const report = [];
|
||||
for (const shot of SHOTS) for (const scheme of ['light', 'dark']) for (const width of [390, 1440]) {
|
||||
const { b, wait } = await setup(t, { ...(shot.options ?? {}), viewport: [width, width === 390 ? 844 : 900] });
|
||||
await b.call('Emulation.setScrollbarsHidden', { hidden: true });
|
||||
await b.call('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: scheme }] });
|
||||
await wait(`(${shot.ready}) && !!window.MosaicAppearance`);
|
||||
await b.evaluate(`window.MosaicAppearance.ready.then(() => window.MosaicAppearance.set({ mode: ${JSON.stringify(scheme)} }))`);
|
||||
if (shot.after) await shot.after(b, wait);
|
||||
await settle();
|
||||
const sideways = await b.evaluate('document.documentElement.scrollWidth > document.documentElement.clientWidth');
|
||||
const noRing = [], seen = new Set();
|
||||
for (let i = 0; i < 160; i++) {
|
||||
await b.call('Input.dispatchKeyEvent', { type: 'keyDown', key: 'Tab', code: 'Tab', windowsVirtualKeyCode: 9 });
|
||||
await b.call('Input.dispatchKeyEvent', { type: 'keyUp', key: 'Tab', code: 'Tab', windowsVirtualKeyCode: 9 });
|
||||
const r = await b.evaluate(`(() => { const e = document.activeElement; if (!e || e === document.body) return null; const cs = getComputedStyle(e);
|
||||
const ring = (cs.outlineStyle !== 'none' && parseFloat(cs.outlineWidth) > 0) || (cs.boxShadow && cs.boxShadow !== 'none');
|
||||
return { key: e.id || e.getAttribute('href') || e.tagName + ':' + (e.textContent || '').trim().slice(0, 30), ring }; })()`);
|
||||
if (!r || seen.has(r.key)) break; seen.add(r.key);
|
||||
if (!r.ring) noRing.push(r.key);
|
||||
}
|
||||
await b.evaluate('document.activeElement?.blur(); window.scrollTo(0, 0); true'); await settle();
|
||||
const file = `${shot.name}-${width}-${scheme}.png`;
|
||||
let metrics = await b.call('Page.getLayoutMetrics'), size = metrics.cssContentSize || metrics.contentSize;
|
||||
await b.viewport(width, Math.ceil(size.height)); await settle();
|
||||
metrics = await b.call('Page.getLayoutMetrics'); size = metrics.cssContentSize || metrics.contentSize;
|
||||
const { data } = await b.call('Page.captureScreenshot', { format: 'png', captureBeyondViewport: true, clip: { x: 0, y: 0, width: Math.ceil(size.width), height: Math.ceil(size.height), scale: 1 } });
|
||||
await writeFile(join(dir, file), Buffer.from(data, 'base64'));
|
||||
report.push({ page: shot.name, width, scheme, shot: file, sideways, noRing });
|
||||
await b.close();
|
||||
}
|
||||
await writeFile(join(dir, 'measure.json'), JSON.stringify(report, null, 1));
|
||||
for (const r of report) {
|
||||
assert.equal(r.sideways, false, `${r.shot} scrolls sideways`);
|
||||
assert.deepEqual(r.noRing, [], `${r.shot}: controls with no visible focus ring`);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,515 @@
|
||||
// #1873 settings-s5c (DESIGN.md, mockup models-advanced): Model setup, Advanced, lists the installed
|
||||
// programs from the status read's programs field and which organizations have each one on; one
|
||||
// program can be turned off in every organization (POST /api/proxy/admin/programs/off), and the page
|
||||
// words each organization's outcome. Technical details reads access health per organization
|
||||
// (GET /api/proxy/admin/access) when it opens. All of it is the instance owner's: a page without
|
||||
// proxyAdmin shows none of it and sends no /api/proxy/admin/ request.
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { startServer } from '../src/serve.mjs';
|
||||
import { createDevOwner } from '../src/dev-owner.mjs';
|
||||
import { browser } from './browser.mjs';
|
||||
import { close } from './fixture.mjs';
|
||||
|
||||
const LIST = [
|
||||
{ id: 'acme', label: 'Acme Studio', theme: 'cobalt', busHealthy: true },
|
||||
{ id: 'gamma', label: 'Gamma Home', theme: 'rosewood', busHealthy: true },
|
||||
{ id: 'lab', label: 'Lab Bench', theme: 'cobalt', busHealthy: true },
|
||||
];
|
||||
const VIEW = {
|
||||
business: 'acme', revision: 'a'.repeat(64), harnesses: ['pi'], models: ['fixture-deterministic'],
|
||||
providerModels: { 'fixture-deterministic': { module: 'fixture', upstream: 'fixture', model: 'fixture-deterministic' } },
|
||||
levels: { pi: ['off', 'low'] }, instances: {}, catalogue: { status: 'ready', observedAt: new Date().toISOString(), models: [], notices: [] },
|
||||
};
|
||||
|
||||
async function orgFixture() {
|
||||
const { checkRootAgents } = await import('../../organization/src/index.mjs');
|
||||
const rolesDir = fileURLToPath(new URL('../../../roles', import.meta.url));
|
||||
const root = { state: 'ok', ...checkRootAgents({ agentsVersion: 1, agents: {} }, { rolesDir }) };
|
||||
const empty = () => Promise.resolve([]);
|
||||
const member = org => ({
|
||||
id: org.id, doc: { appearance: { theme: org.theme }, roles: { chief: { definition: 'chief-of-staff' } }, projects: {} },
|
||||
status: async () => ({ healthy: true, pid: 4242, boot: null }),
|
||||
options: () => ({ bus: { inbox: empty, agents: empty, 'thread.claims': empty, tasks: empty, usage: () => Promise.resolve({}), limits: () => Promise.resolve({}), decisions: empty }, chat: { threads: empty, projects: empty } }),
|
||||
});
|
||||
const organizations = {
|
||||
list: async () => LIST,
|
||||
members: async () => LIST.map(member),
|
||||
resolve: async id => {
|
||||
const org = LIST.find(o => o.id === id); if (!org) return null;
|
||||
return {
|
||||
busHealthy: true,
|
||||
reads: { business: async () => ({ business: { id, human: 'jason', projects: [], appearance: { theme: org.theme } } }), settings: async () => ({ business: id, release: 'fixture', notifier: {} }), queue: async () => ({ rows: [] }), busStatus: async () => ({}) },
|
||||
bus: { messages: async () => ({ messages: [], unread: 0, total: 0 }), agents: empty, 'thread.claims': empty, inbox: empty, tasks: empty, usage: async () => ({}), routes: async () => ({ ...VIEW, business: id }), instances: async () => ({ business: id, revision: 'a'.repeat(64), launch: true, noHire: [], retired: [], definitions: {}, instances: {} }) },
|
||||
chat: { projects: empty, threads: empty },
|
||||
};
|
||||
},
|
||||
};
|
||||
return { organizations, agentLevels: { loadRoot: () => root, rolesDir, ctx: { configDir: '/cfg', dataRoot: '/data', instance: 'test' } } };
|
||||
}
|
||||
|
||||
// Program names are the read's, not a list the page holds: one of them is made up.
|
||||
const PROGRAMS = { installed: ['pi', 'zeta-runner'], organizations: [{ id: 'acme', on: ['pi', 'zeta-runner'] }, { id: 'gamma', on: null }, { id: 'lab', on: ['pi'] }] };
|
||||
const SERVICE_UP = { next: ['accounts'], init: { initialized: true, partial: false, problem: null, settings: { port: 18432, rotateGraceMinutes: 30, firstOutputSeconds: 90 }, files: [] },
|
||||
release: { fetched: true, verified: true, version: '8.0.42', panelVersion: '1.26.7' },
|
||||
unit: { unit: 'mosaic-proxy.service', installed: true, active: 'active', port: 18432, listening: true, healthy: true }, fence: { set: false }, initLock: { present: false, blockers: [] }, fetch: { state: 'idle' },
|
||||
organizations: [], ride: [], programs: PROGRAMS };
|
||||
const AFTER = { ...SERVICE_UP, programs: { ...PROGRAMS, organizations: [{ id: 'acme', on: ['pi'] }, { id: 'gamma', on: null }, { id: 'lab', on: ['pi'] }] } };
|
||||
const ACCESS = { organizations: [
|
||||
{ id: 'acme', health: 'ok', keys: [{ keyId: 'kid-AAAA1111', agent: 'chief', state: 'active', issuedAt: null }], other: 4242, sentinel: 1 },
|
||||
{ id: 'gamma', health: 'attention', reason: 'proxy-uncertain', keys: [] },
|
||||
{ id: 'lab', health: 'attention', keys: [{ keyId: 'kid-BBBB2222', agent: 'chief', state: 'active', issuedAt: null }, { keyId: 'kid-CCCC3333', agent: 'writer', state: 'expired', issuedAt: null }], other: 4242 },
|
||||
{ id: 'north', health: 'missing', keys: [], other: 4242 },
|
||||
{ id: 'south', health: 'not-riding', keys: [] },
|
||||
{ id: 'east', health: 'unreadable', reason: 'bus-down', keys: [] },
|
||||
{ id: 'west', health: 'unreadable', reason: 'no-proxy-client', keys: [] },
|
||||
{ id: 'up', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
{ id: 'down', keys: [{ keyId: 'kid-DDDD4444', agent: 'chief', state: 'active', issuedAt: null }] },
|
||||
{ id: 'left', health: 'ok', reason: 'proxy-uncertain', keys: [{ keyId: 'kid-EEEE5555', agent: 'chief', state: 'active', issuedAt: null }] },
|
||||
{ id: 'right', health: 'ok', keys: [] },
|
||||
{ id: 'top', health: 'great', keys: [] },
|
||||
] };
|
||||
|
||||
// The owner's flags (or not), the status (a list: each read takes the next one and the last one stays),
|
||||
// and the admin reads and writes by name. window.__admin records every /api/proxy/admin/ request,
|
||||
// window.__writes every write anywhere under /proxy/.
|
||||
const stub = ({ owner = true, status = SERVICE_UP, admin = {} } = {}) => `(() => {
|
||||
const real = window.fetch.bind(window);
|
||||
window.__s5c = ${JSON.stringify({ owner, status: Array.isArray(status) ? status : [status], admin })}; window.__admin = []; window.__writes = [];
|
||||
const json = (body, status = 200) => new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } });
|
||||
const next = list => Array.isArray(list) ? (list.length > 1 ? list.shift() : list[0]) : list;
|
||||
window.fetch = async (input, init = {}) => {
|
||||
const url = new URL(typeof input === 'string' ? input : input.url, location.href), path = url.pathname, method = (init.method || 'GET').toUpperCase();
|
||||
const at = path.indexOf('/proxy/admin/');
|
||||
if (at >= 0) {
|
||||
const name = path.slice(at + 13) + url.search; window.__admin.push(method + ' ' + name);
|
||||
if (method !== 'GET') window.__writes.push({ path, body: init.body ?? null });
|
||||
if (name === 'status') { const s = next(__s5c.status); if (s === null || typeof s !== 'object') return json(s); if (typeof s.raw === 'string') return new Response(s.raw, { status: 200, headers: { 'content-type': 'text/html' } }); return s.status ? json(s.body, s.status) : json(s); }
|
||||
const a = next(__s5c.admin[method === 'GET' ? name : method + ' ' + name]);
|
||||
if (!a) return json({ error: 'fixture-unexpected-api' }, 403);
|
||||
if (a.throws) throw new TypeError('Failed to fetch');
|
||||
return a.status ? json(a.body, a.status) : json(a);
|
||||
}
|
||||
if (method !== 'GET' && /\\/proxy\\//.test(path)) { window.__writes.push({ path, body: init.body ?? null }); return json({ error: 'stub' }, 500); }
|
||||
if (method !== 'GET') return real(input, init);
|
||||
if (/\\/api\\/(orgs\\/[^/]+\\/)?config$/.test(path)) { const c = await (await real(input, init)).json(); return json({ ...c, orgSetup: true, proxyAdmin: __s5c.owner, proxyAdminSend: __s5c.owner }); }
|
||||
if (path.endsWith('/proxy/plugins')) return json({ revision: 'b'.repeat(64), available: { harness: ['pi'], 'model-provider': ['fixture', 'cliproxy'] }, plugins: { harness: ['pi'], 'model-provider': ['fixture'] } });
|
||||
if (path.endsWith('/proxy/accounts')) return json({ accounts: [] });
|
||||
if (path.endsWith('/proxy/keys')) return json({ keys: [] });
|
||||
if (path.endsWith('/triage/route')) return json({ route: null });
|
||||
return real(input, init);
|
||||
};
|
||||
})();`;
|
||||
|
||||
async function setup(t, { path = '/#/settings/model-setup', viewport = [1440, 900], ...options } = {}) {
|
||||
const { organizations, agentLevels } = await orgFixture();
|
||||
const server = await startServer({ port: 0, sso: createDevOwner(), organizations, agentLevels, triage: null, send: async () => ({}), log: () => {} });
|
||||
t.after(() => close(server));
|
||||
const b = await browser({ machine: false }); t.after(() => b.close());
|
||||
await b.viewport(...viewport);
|
||||
await b.call('Page.addScriptToEvaluateOnNewDocument', { source: stub(options) });
|
||||
await b.navigate(`http://127.0.0.1:${server.address().port}${path}`);
|
||||
const wait = expression => b.evaluate(`(async()=>{for(let i=0;i<250;i++){if(${expression})return true;await new Promise(r=>setTimeout(r,30))}throw Error("wait failed: "+${JSON.stringify(expression)}+" HASH "+location.hash)})()`);
|
||||
const text = sel => b.evaluate(`document.querySelector(${JSON.stringify(sel)})?.textContent ?? null`);
|
||||
const click = sel => b.evaluate(`(() => { const el = document.querySelector(${JSON.stringify(sel)}); if (!el) throw Error("no ${sel.replace(/"/g, '')}"); el.click(); return true; })()`);
|
||||
const admin = () => b.evaluate('window.__admin');
|
||||
return { b, wait, text, click, admin };
|
||||
}
|
||||
const READY = 'document.querySelector("#proxy-setup-state dd") && !document.querySelector("#setup-step-service").hidden && !document.querySelector("#proxy-setup-advanced").hidden';
|
||||
const openAdvanced = b => b.evaluate('(() => { document.querySelector("#proxy-setup-advanced").open = true; return true; })()');
|
||||
const openTech = b => b.evaluate('(() => { document.querySelector("#proxy-setup-tech").open = true; return true; })()');
|
||||
const programs = b => b.evaluate('[...document.querySelectorAll("#proxy-setup-programs > li")].map(li => ({ name: li.dataset.program ?? null, text: li.querySelector(".proxy-program-orgs")?.textContent ?? li.textContent }))');
|
||||
const outcomes = b => b.evaluate('[...document.querySelectorAll("#proxy-setup-programs-result li")].map(li => li.textContent)');
|
||||
const access = b => b.evaluate('[...document.querySelectorAll("#proxy-setup-access li")].map(li => ({ text: li.textContent, ready: li.dataset.access }))');
|
||||
const KEY_TEXT = /ms[kotp]-[A-Za-z0-9_-]{4,}/i;
|
||||
// Technical details' other reads, so a shot shows them as they read.
|
||||
const T = n => new Date(Date.UTC(2026, 10, 19, 9, n)).toISOString();
|
||||
const TECH = { 'service-key': { madeAt: T(3), renewedAt: null, renewal: 'not-built', problem: null }, 'history?limit=25': { entries: [{ op: 'unit.start', time: T(4), user: 'jason' }, { op: 'init', time: T(2), user: 'jason' }], next: null } };
|
||||
const OFF_PI = { name: 'pi', results: [{ id: 'acme', outcome: 'in-use' }, { id: 'gamma', outcome: 'bus-down' }, { id: 'lab', outcome: 'last-program' }], receipt: null };
|
||||
|
||||
// ---- The list -------------------------------------------------------------------------------
|
||||
test('settings-s5c: Advanced lists the installed programs from the status read and which organizations have each one on, by display name', { timeout: 90000 }, async t => {
|
||||
const { b, wait, admin } = await setup(t);
|
||||
await wait(READY); await openAdvanced(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-programs > li[data-program]").length === 2');
|
||||
assert.deepEqual(await programs(b), [
|
||||
{ name: 'pi', text: 'On in Acme Studio and Lab Bench. Not known in Gamma Home: its list could not be read.' },
|
||||
{ name: 'zeta-runner', text: 'On in Acme Studio. Off in Lab Bench. Not known in Gamma Home: its list could not be read.' },
|
||||
]);
|
||||
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("#proxy-setup-programs .proxy-program-name")].map(n => n.textContent)'), ['pi', 'zeta-runner']);
|
||||
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("[data-program-off]")].map(n => n.getAttribute("aria-label"))'), ['Turn off pi for every organization', 'Turn off zeta-runner for every organization']);
|
||||
assert.doesNotMatch(await b.evaluate('document.querySelector("#proxy-setup-advanced").textContent'), /Turning a program off for every organization is not built/);
|
||||
// Opening Advanced reads nothing more: the list is the status read's.
|
||||
assert.deepEqual((await admin()).filter(r => r !== 'GET status'), []);
|
||||
});
|
||||
|
||||
test('settings-s5c: a null list reads as not known, never off; a list the read could not give says so', { timeout: 90000 }, async t => {
|
||||
{
|
||||
const status = { ...SERVICE_UP, programs: { installed: ['pi'], organizations: [{ id: 'gamma', on: null }, { id: 'acme', on: ['pi'] }] } };
|
||||
const { b, wait } = await setup(t, { status });
|
||||
await wait(READY); await openAdvanced(b);
|
||||
await wait('document.querySelector("#proxy-setup-programs > li[data-program]")');
|
||||
const [pi] = await programs(b);
|
||||
assert.equal(pi.text, 'On in Acme Studio. Not known in Gamma Home: its list could not be read.');
|
||||
assert.doesNotMatch(pi.text, /Off in/);
|
||||
}
|
||||
for (const [programsField, line] of [
|
||||
[{ installed: ['pi'], organizations: null }, 'Which organizations have it on could not be read.'],
|
||||
[{ installed: null, organizations: [] }, 'The installed programs could not be read.'],
|
||||
[undefined, 'The programs could not be read.'],
|
||||
[{ installed: [], organizations: [] }, 'No programs are installed.'],
|
||||
]) {
|
||||
const { b, wait } = await setup(t, { status: { ...SERVICE_UP, programs: programsField } });
|
||||
await wait(READY); await openAdvanced(b);
|
||||
await wait(`document.querySelector("#proxy-setup-programs")?.textContent.includes(${JSON.stringify(line)})`);
|
||||
assert.doesNotMatch(await b.evaluate('document.querySelector("#proxy-setup-programs").textContent'), /Off in|On in no/);
|
||||
}
|
||||
});
|
||||
|
||||
// ---- Turn off for every organization ----------------------------------------------------------
|
||||
test('settings-s5c: the confirm names the program, and Cancel sends nothing', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, click, admin } = await setup(t, { admin: { 'POST programs/off': OFF_PI } });
|
||||
await wait(READY); await openAdvanced(b);
|
||||
await wait('document.querySelector("[data-program-off=pi]")');
|
||||
await click('[data-program-off=pi]');
|
||||
await wait('document.querySelector("#proxy-setup-programs-yes")');
|
||||
assert.match(await text('#proxy-setup-programs-ask'), /^Turn off pi in every organization\?/);
|
||||
assert.equal(await text('#proxy-setup-programs-yes'), 'Turn off pi');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-programs-yes").closest("li").dataset.program'), 'pi');
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-programs-yes');
|
||||
await click('#proxy-setup-programs-no');
|
||||
await wait('!document.querySelector("#proxy-setup-programs-yes")');
|
||||
await new Promise(r => setTimeout(r, 300));
|
||||
assert.deepEqual(await b.evaluate('window.__writes'), []);
|
||||
assert.deepEqual((await admin()).filter(r => r !== 'GET status'), []);
|
||||
assert.equal(await b.evaluate('document.activeElement?.dataset.programOff'), 'pi', 'focus goes back to the button');
|
||||
// Confirming sends the one name.
|
||||
await click('[data-program-off=pi]'); await wait('document.querySelector("#proxy-setup-programs-yes")');
|
||||
await click('#proxy-setup-programs-yes');
|
||||
await wait('document.querySelectorAll("#proxy-setup-programs-result li").length === 3');
|
||||
assert.deepEqual(await b.evaluate('window.__writes.map(w => [w.path.replace(/^.*(\\/proxy\\/admin\\/)/, "$1"), JSON.parse(w.body)])'), [['/proxy/admin/programs/off', { name: 'pi' }]]);
|
||||
});
|
||||
|
||||
test('settings-s5c: each outcome id renders its line, by display name', { timeout: 90000 }, async t => {
|
||||
const results = [
|
||||
{ id: 'acme', outcome: 'off' }, { id: 'gamma', outcome: 'already-off' }, { id: 'lab', outcome: 'in-use' },
|
||||
{ id: 'o4', outcome: 'last-program' }, { id: 'o5', outcome: 'bus-down' }, { id: 'o6', outcome: 'unreadable' },
|
||||
{ id: 'o7', outcome: 'failed:revision-conflict' }, { id: 'o8', outcome: 'failed:outcome-unknown' }, { id: 'o9', outcome: 'failed:plugins-refused' },
|
||||
{ id: 'o10', outcome: 'sideways' }, { id: 'o11' },
|
||||
];
|
||||
const { b, wait, click } = await setup(t, { admin: { 'POST programs/off': { name: 'zeta-runner', results, receipt: null } } });
|
||||
await wait(READY); await openAdvanced(b);
|
||||
await wait('document.querySelector("[data-program-off=zeta-runner]")');
|
||||
await click('[data-program-off=zeta-runner]'); await wait('document.querySelector("#proxy-setup-programs-yes")'); await click('#proxy-setup-programs-yes');
|
||||
await wait('document.querySelectorAll("#proxy-setup-programs-result li").length === 11');
|
||||
assert.equal(await b.evaluate('document.querySelector(".proxy-programs-result-title").textContent'), 'Turn off zeta-runner: what happened in each organization.');
|
||||
const lines = await outcomes(b);
|
||||
await wait('/The list above is read again and shows what is on now\./.test(document.querySelector("#proxy-setup-programs-result").textContent)');
|
||||
assert.deepEqual(lines, [
|
||||
'Acme Studio: Turned off.',
|
||||
'Gamma Home: It was already off. Nothing changed.',
|
||||
'Lab Bench: Kept on. Something there still uses it: a role route, a fallback or an agent.',
|
||||
'o4: Kept on. It is the only program on there.',
|
||||
'o5: Not changed. Its programs could not be read.',
|
||||
'o6: Not changed. Something Mosaic needed to check could not be read.',
|
||||
'o7: Not changed. It changed while Mosaic was checking. Try again.',
|
||||
'o8: May have changed. The change did not answer in time.',
|
||||
'o9: Not changed. It refused the change (plugins-refused).',
|
||||
'o10: Not known. The answer did not say what happened.',
|
||||
'o11: Not known. The answer did not say what happened.',
|
||||
]);
|
||||
// bus-down does not say the organization is stopped.
|
||||
assert.doesNotMatch(lines[4], /stop|not running|down/i);
|
||||
});
|
||||
|
||||
test('settings-s5c: busy and a failed request each render a plain line, and the status is read again after each', { timeout: 90000 }, async t => {
|
||||
const answers = [
|
||||
{ status: 409, body: { error: 'proxy-admin-busy' } },
|
||||
{ status: 500, body: { error: 'outcome-unknown' } },
|
||||
{ status: 400, body: { error: 'invalid-request' } },
|
||||
{ throws: true },
|
||||
];
|
||||
const { b, wait, click, admin } = await setup(t, { admin: { 'POST programs/off': answers } });
|
||||
await wait(READY); await openAdvanced(b);
|
||||
const lines = [];
|
||||
for (let i = 0; i < answers.length; i++) {
|
||||
await wait('document.querySelector("[data-program-off=pi]") && !document.querySelector("[data-program-off=pi]").disabled');
|
||||
const before = (await admin()).filter(r => r === 'GET status').length;
|
||||
await click('[data-program-off=pi]'); await wait('document.querySelector("#proxy-setup-programs-yes")'); await click('#proxy-setup-programs-yes');
|
||||
await wait(`window.__admin.filter(r => r === "POST programs/off").length === ${i + 1} && window.__admin.lastIndexOf("GET status") > window.__admin.lastIndexOf("POST programs/off") && /The list above is read again/.test(document.querySelector("#proxy-setup-programs-result").textContent)`);
|
||||
assert.equal((await admin()).filter(r => r === 'GET status').length, before + 1);
|
||||
lines.push([await b.evaluate('document.querySelector("#proxy-setup-programs-result").className'), await b.evaluate('document.querySelector("#proxy-setup-programs-result").textContent')]);
|
||||
}
|
||||
assert.deepEqual(lines, [
|
||||
['proxy-programs-result banner ref', 'pi was not turned off. Another setup action is running. Wait for it to finish, then try again.The list above is read again and shows what is on now.'],
|
||||
['proxy-programs-result banner err', 'Turning off pi may not have finished, so some organizations may have changed. The outcome is unknown. Check the status before retrying. (outcome-unknown)The list above is read again and shows what is on now.'],
|
||||
['proxy-programs-result banner err', 'pi was not turned off. The Console refused the request: the program is not in its installed list, or that list could not be read. (invalid-request)The list above is read again and shows what is on now.'],
|
||||
['proxy-programs-result banner err', 'Turning off pi may not have finished, so some organizations may have changed. The Console did not answer.The list above is read again and shows what is on now.'],
|
||||
]);
|
||||
});
|
||||
|
||||
test('settings-s5c: after an off the page reads the status again and shows the new list; focus goes to the result', { timeout: 90000 }, async t => {
|
||||
const { b, wait, click, admin } = await setup(t, { status: [SERVICE_UP, SERVICE_UP, AFTER], admin: { 'POST programs/off': { name: 'zeta-runner', results: [{ id: 'acme', outcome: 'off' }, { id: 'gamma', outcome: 'bus-down' }, { id: 'lab', outcome: 'already-off' }], receipt: null } } });
|
||||
await wait(READY); await openAdvanced(b);
|
||||
await wait('document.querySelector("[data-program-off=zeta-runner]")');
|
||||
const before = (await admin()).filter(r => r === 'GET status').length;
|
||||
await click('[data-program-off=zeta-runner]'); await wait('document.querySelector("#proxy-setup-programs-yes")');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-programs-yes").focus(); document.querySelector("#proxy-setup-programs-yes").click(); true');
|
||||
await wait('document.querySelectorAll("#proxy-setup-programs-result li").length === 3');
|
||||
await wait(`window.__admin.filter(r => r === "GET status").length > ${before}`);
|
||||
const order = await admin();
|
||||
assert.ok(order.lastIndexOf('GET status') > order.indexOf('POST programs/off'), JSON.stringify(order));
|
||||
await wait('document.querySelector("li[data-program=zeta-runner] .proxy-program-orgs")?.textContent.startsWith("On in no organization.")');
|
||||
assert.equal((await programs(b))[1].text, 'On in no organization. Off in Acme Studio and Lab Bench. Not known in Gamma Home: its list could not be read.');
|
||||
await new Promise(r => setTimeout(r, 300));
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-programs-result');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-programs-result").tabIndex'), -1);
|
||||
});
|
||||
|
||||
test('settings-s5c: focus goes to the result after a busy answer too', { timeout: 90000 }, async t => {
|
||||
const { b, wait, click } = await setup(t, { admin: { 'POST programs/off': { status: 409, body: { error: 'proxy-admin-busy' } } } });
|
||||
await wait(READY); await openAdvanced(b);
|
||||
await wait('document.querySelector("[data-program-off=pi]")');
|
||||
await click('[data-program-off=pi]'); await wait('document.querySelector("#proxy-setup-programs-yes")'); await click('#proxy-setup-programs-yes');
|
||||
await wait('/was not turned off/.test(document.querySelector("#proxy-setup-programs-result").textContent)');
|
||||
await new Promise(r => setTimeout(r, 300));
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-programs-result');
|
||||
});
|
||||
|
||||
test('settings-s5c: when the status read after an off fails, or answers with no status, the result says the list may be out of date, and Read again reads it', { timeout: 180000 }, async t => {
|
||||
// A refusal, and three answers that are 200 but not a status: null, an empty object, a page that is not JSON.
|
||||
for (const down of [{ status: 503, body: { error: 'outcome-unknown' } }, null, {}, { raw: '<html><body>Gateway</body></html>' }]) {
|
||||
const { b, wait, click, admin } = await setup(t, { admin: { 'POST programs/off': { name: 'zeta-runner', results: [{ id: 'acme', outcome: 'failed:outcome-unknown' }], receipt: null } } });
|
||||
await wait(READY); await openAdvanced(b);
|
||||
await wait('document.querySelector("[data-program-off=zeta-runner]")');
|
||||
const before = (await admin()).filter(r => r === 'GET status').length;
|
||||
await b.evaluate(`window.__s5c.status = ${JSON.stringify([down, AFTER])}; true`);
|
||||
await click('[data-program-off=zeta-runner]'); await wait('document.querySelector("#proxy-setup-programs-yes")'); await click('#proxy-setup-programs-yes');
|
||||
await wait('document.querySelector("#proxy-setup-programs-reread")');
|
||||
const shown = await b.evaluate('document.querySelector("#proxy-setup-programs-result").textContent');
|
||||
assert.match(shown, /The list above could not be read again, so it may be out of date\./);
|
||||
assert.doesNotMatch(shown, /shows what is on now/);
|
||||
// The old list still shows, and still says so only as what was last read.
|
||||
assert.match((await programs(b))[1].text, /^On in Acme Studio\./);
|
||||
await b.evaluate('document.querySelector("#proxy-setup-programs-reread").focus(); document.querySelector("#proxy-setup-programs-reread").click(); true');
|
||||
await wait('/The list above is read again and shows what is on now\./.test(document.querySelector("#proxy-setup-programs-result").textContent)');
|
||||
assert.equal((await admin()).filter(r => r === 'GET status').length, before + 2);
|
||||
assert.equal((await programs(b))[1].text, 'On in no organization. Off in Acme Studio and Lab Bench. Not known in Gamma Home: its list could not be read.');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-programs-reread")'), null);
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-programs-result');
|
||||
}
|
||||
});
|
||||
|
||||
test('settings-s5c: a status read that drops the program being asked about, or cannot list programs, drops the question and frees the buttons', { timeout: 90000 }, async t => {
|
||||
const RUNNING = { ...SERVICE_UP, fetch: { state: 'running', step: 'download' } };
|
||||
for (const dropped of [{ ...SERVICE_UP, programs: { ...PROGRAMS, installed: ['pi'] } }, { ...SERVICE_UP, programs: null }, { ...SERVICE_UP, programs: { ...PROGRAMS, installed: ['pi'] } }]) {
|
||||
const { b, wait, click } = await setup(t, { status: [RUNNING, RUNNING, RUNNING, RUNNING, dropped] });
|
||||
await wait(READY); await openAdvanced(b);
|
||||
await wait('document.querySelector("[data-program-off=zeta-runner]")');
|
||||
await click('[data-program-off=zeta-runner]'); await wait('document.querySelector("#proxy-setup-programs-yes")');
|
||||
assert.equal(await b.evaluate('document.querySelector("[data-program-off=pi]").disabled'), true);
|
||||
await wait('!document.querySelector("li[data-program=zeta-runner]")');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-programs-yes")'), null);
|
||||
if (dropped.programs) {
|
||||
assert.equal(await b.evaluate('document.querySelector("[data-program-off=pi]").disabled'), false);
|
||||
await click('[data-program-off=pi]'); await wait('document.querySelector("#proxy-setup-programs-yes")');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-programs-yes").closest("li").dataset.program'), 'pi');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('settings-s5c: a refusal code named like an object property shows as its code, never as page source', { timeout: 90000 }, async t => {
|
||||
const refused = { status: 400, body: { error: 'constructor' } };
|
||||
const { b, wait, click, text } = await setup(t, { admin: { access: refused, 'POST programs/off': refused } });
|
||||
await wait(READY); await openAdvanced(b); await openTech(b);
|
||||
await wait('!document.querySelector("#proxy-setup-access-retry").hidden && document.querySelector("[data-program-off=pi]")');
|
||||
await click('[data-program-off=pi]'); await wait('document.querySelector("#proxy-setup-programs-yes")'); await click('#proxy-setup-programs-yes');
|
||||
await wait('/may not have finished/.test(document.querySelector("#proxy-setup-programs-result").textContent)');
|
||||
const page = await b.evaluate('document.querySelector("#proxy-setup").textContent');
|
||||
assert.doesNotMatch(page, /function|native code|\[object/);
|
||||
assert.match(await text('#proxy-setup-access-message'), /^Access could not be read\. /);
|
||||
});
|
||||
|
||||
// ---- Access ----------------------------------------------------------------------------------
|
||||
test('settings-s5c: Technical details reads access when it opens and words each health; only ok with every key active reads Ready', { timeout: 90000 }, async t => {
|
||||
const { b, wait, admin } = await setup(t, { admin: { access: ACCESS } });
|
||||
await wait(READY);
|
||||
await new Promise(r => setTimeout(r, 300));
|
||||
assert.equal((await admin()).includes('GET access'), false, 'access is not read before Technical details opens');
|
||||
await openTech(b);
|
||||
await wait(`document.querySelectorAll("#proxy-setup-access li").length === ${ACCESS.organizations.length}`);
|
||||
assert.equal((await admin()).filter(r => r === 'GET access').length, 1);
|
||||
assert.deepEqual(await access(b), [
|
||||
{ text: 'Acme StudioReady', ready: 'ready' },
|
||||
{ text: 'Gamma HomeNot sureThe model service did not answer clearly, so Mosaic can\'t tell whether its access works.', ready: 'not-ready' },
|
||||
{ text: 'Lab BenchNeeds attentionSome of its access is not active.', ready: 'not-ready' },
|
||||
{ text: 'northNone yetNo agent here has access to your models yet.', ready: 'not-ready' },
|
||||
{ text: 'southNot usedIt does not use your models.', ready: 'not-ready' },
|
||||
{ text: 'eastCould not be readIts bus did not answer.', ready: 'not-ready' },
|
||||
{ text: 'westCould not be readIts bus has no model service client set up.', ready: 'not-ready' },
|
||||
{ text: 'upCould not be readIts answer about access could not be read.', ready: 'not-ready' },
|
||||
{ text: 'downNot knownThe answer did not say.', ready: 'not-ready' },
|
||||
{ text: 'leftNot knownThe answer did not agree with itself.', ready: 'not-ready' },
|
||||
{ text: 'rightNot knownThe answer did not agree with itself.', ready: 'not-ready' },
|
||||
{ text: 'topNot knownThe answer did not say.', ready: 'not-ready' },
|
||||
]);
|
||||
const shown = await b.evaluate('document.querySelector(".proxy-setup-access").textContent');
|
||||
assert.equal((shown.match(/Ready/g) ?? []).length, 1);
|
||||
assert.doesNotMatch(shown, /kid-|4242|sentinel|chief|writer|expired/);
|
||||
assert.doesNotMatch(await b.evaluate('document.querySelector(".proxy-setup-access").textContent'), /bus is (stopped|down)|stopped/i);
|
||||
});
|
||||
|
||||
test('settings-s5c: a failed access read shows a plain line with Read again, and Read again reads it', { timeout: 90000 }, async t => {
|
||||
const { b, wait, text, admin } = await setup(t, { admin: { access: [{ status: 503, body: { error: 'outcome-unknown' } }, { status: 200, body: { organizations: 'nope' } }, ACCESS] } });
|
||||
await wait(READY); await openTech(b);
|
||||
await wait('!document.querySelector("#proxy-setup-access-retry").hidden');
|
||||
assert.equal(await text('#proxy-setup-access-message'), 'Access could not be read. The host could not read access. (outcome-unknown)');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-access-message").className'), 'banner err');
|
||||
assert.equal(await b.evaluate('document.querySelectorAll("#proxy-setup-access li").length'), 0);
|
||||
await b.evaluate('document.querySelector("#proxy-setup-access-retry").focus(); document.querySelector("#proxy-setup-access-retry").click(); true');
|
||||
await wait('window.__admin.filter(r => r === "GET access").length === 2'); await new Promise(r => setTimeout(r, 400));
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-access-retry").hidden'), false);
|
||||
assert.equal(await text('#proxy-setup-access-message'), 'Access could not be read. The host could not read access. (outcome-unknown)');
|
||||
assert.equal(await b.evaluate('document.querySelectorAll("#proxy-setup-access li").length'), 0, 'a shape the page does not know never reads as Ready');
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-access-retry');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-access-retry").focus(); document.querySelector("#proxy-setup-access-retry").click(); true');
|
||||
await wait(`document.querySelectorAll("#proxy-setup-access li").length === ${ACCESS.organizations.length}`);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-access-retry").hidden'), true);
|
||||
assert.equal(await b.evaluate('document.activeElement?.id'), 'proxy-setup-access-title', 'focus lands on the answer');
|
||||
assert.equal((await admin()).filter(r => r === 'GET access').length, 3);
|
||||
});
|
||||
|
||||
// ---- Owner only ------------------------------------------------------------------------------
|
||||
test('settings-s5c: a page that is not the instance owner\'s shows none of it and sends no request to programs or access', { timeout: 90000 }, async t => {
|
||||
// The owner's page, as the control.
|
||||
{
|
||||
const { b, wait } = await setup(t, { admin: { access: ACCESS } });
|
||||
await wait(READY); await openAdvanced(b); await openTech(b);
|
||||
await wait(`document.querySelectorAll("[data-program-off]").length === 2 && document.querySelectorAll("#proxy-setup-access li").length === ${ACCESS.organizations.length}`);
|
||||
}
|
||||
const { b, wait } = await setup(t, { owner: false, admin: { access: ACCESS, 'POST programs/off': OFF_PI } });
|
||||
await wait('window.MosaicProxy && document.querySelector("#proxy-setup") && document.querySelector("#settings-page-title")');
|
||||
await new Promise(r => setTimeout(r, 800));
|
||||
await b.evaluate('document.querySelectorAll("#proxy-setup details").forEach(d => { d.open = true; }); true');
|
||||
await new Promise(r => setTimeout(r, 800));
|
||||
const sent = await b.evaluate('window.__admin');
|
||||
assert.deepEqual(sent, []);
|
||||
assert.equal(sent.some(r => /programs|access/.test(r)), false);
|
||||
const shown = sel => b.evaluate(`(() => { const e = document.querySelector(${JSON.stringify(sel)}); return !!e && e.getClientRects().length > 0; })()`);
|
||||
for (const sel of ['#proxy-setup-programs', '#proxy-setup-programs-result', '#proxy-setup-access', '#proxy-setup-access-message', '#proxy-setup-access-retry', '.proxy-setup-access'])
|
||||
assert.equal(await shown(sel), false, `${sel} is not shown`);
|
||||
assert.equal(await b.evaluate('document.querySelectorAll("[data-program-off], #proxy-setup-access li").length'), 0);
|
||||
assert.doesNotMatch(await b.evaluate('document.body.innerText'), /Turn off for every organization|Programs that drive models|Access/);
|
||||
});
|
||||
|
||||
// ---- No key text -----------------------------------------------------------------------------
|
||||
test('settings-s5c: no key text reaches the page, even from a fixture answer that holds it in an unexpected field', { timeout: 90000 }, async t => {
|
||||
const K = s => `${s}-abcdefghijklmnop`;
|
||||
const status = { ...SERVICE_UP, programs: { installed: ['pi', K('msk')], organizations: [{ id: 'acme', on: ['pi', K('msk')] }, { id: K('mso'), on: ['pi'] }] } };
|
||||
const off = { name: 'pi', receipt: { time: 'x', op: 'programs.off', name: K('mst'), results: [] }, results: [{ id: K('msp'), outcome: 'off' }, { id: 'acme', outcome: `failed:${K('msk')}` }, { id: 'gamma', outcome: K('mso') }] };
|
||||
const accessAnswer = { organizations: [
|
||||
{ id: K('msk'), health: 'ok', keys: [{ keyId: K('msk'), agent: K('msp'), state: 'active', issuedAt: null }] },
|
||||
{ id: 'acme', health: K('mso'), reason: K('mst'), keys: [], other: 3 },
|
||||
{ id: 'gamma', health: 'unreadable', reason: K('msk'), keys: [{ keyId: K('mst') }] },
|
||||
] };
|
||||
const { b, wait, click } = await setup(t, { status, admin: { access: accessAnswer, 'POST programs/off': off } });
|
||||
await wait(READY); await openAdvanced(b); await openTech(b);
|
||||
await wait('document.querySelectorAll("#proxy-setup-access li").length === 3 && document.querySelector("[data-program-off=pi]")');
|
||||
await click('[data-program-off=pi]'); await wait('document.querySelector("#proxy-setup-programs-yes")'); await click('#proxy-setup-programs-yes');
|
||||
await wait('document.querySelectorAll("#proxy-setup-programs-result li").length === 3');
|
||||
const page = await b.evaluate('document.querySelector("#proxy-setup").textContent + "\\n" + document.body.innerText + "\\n" + [...document.querySelectorAll("#proxy-setup [aria-label], #proxy-setup [data-program], #proxy-setup [data-program-off], #proxy-setup [data-outcome]")].map(e => [e.getAttribute("aria-label"), e.dataset.program, e.dataset.programOff, e.dataset.outcome].join(" ")).join("\\n")');
|
||||
assert.doesNotMatch(page, KEY_TEXT);
|
||||
// What the page shows in their place.
|
||||
assert.equal(await b.evaluate('document.querySelectorAll("#proxy-setup-programs > li[data-program]").length'), 1);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-setup-programs").textContent'), /One or more programs have a name this page does not show\./);
|
||||
assert.match((await programs(b))[0].text, /^On in Acme Studio and An organization Mosaic can't name\.$/);
|
||||
assert.deepEqual(await outcomes(b), [
|
||||
'An organization Mosaic can\'t name: Turned off.',
|
||||
'Acme Studio: Not changed. It refused the change, with a code this page does not show.',
|
||||
'Gamma Home: Not known. The answer did not say what happened.',
|
||||
]);
|
||||
assert.deepEqual(await access(b), [
|
||||
{ text: 'An organization Mosaic can\'t nameReady', ready: 'ready' },
|
||||
{ text: 'Acme StudioNot knownThe answer did not say.', ready: 'not-ready' },
|
||||
{ text: 'Gamma HomeCould not be read', ready: 'not-ready' },
|
||||
]);
|
||||
});
|
||||
|
||||
test('settings-s5c: a refusal whose code is shaped like a key is not shown', { timeout: 90000 }, async t => {
|
||||
const refused = { status: 400, body: { error: 'msk-abcdefghijklmnop' } };
|
||||
const { b, wait, click } = await setup(t, { admin: { access: refused, 'POST programs/off': refused } });
|
||||
await wait(READY); await openAdvanced(b); await openTech(b);
|
||||
await wait('!document.querySelector("#proxy-setup-access-retry").hidden && document.querySelector("[data-program-off=pi]")');
|
||||
await click('[data-program-off=pi]'); await wait('document.querySelector("#proxy-setup-programs-yes")'); await click('#proxy-setup-programs-yes');
|
||||
await wait('/turned off|may not have finished/.test(document.querySelector("#proxy-setup-programs-result").textContent)');
|
||||
const page = await b.evaluate('document.querySelector("#proxy-setup").textContent + "\\n" + document.body.innerText');
|
||||
assert.doesNotMatch(page, KEY_TEXT);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-access-message").textContent'), 'Access could not be read. The Console refused the read.');
|
||||
});
|
||||
|
||||
// ---- Shots ------------------------------------------------------------------------------------
|
||||
test('shots', { timeout: 900000, skip: !process.env.MOSAIC_UX_SHOTS }, async t => {
|
||||
const { mkdir, writeFile } = await import('node:fs/promises');
|
||||
const { join } = await import('node:path');
|
||||
const dir = process.env.MOSAIC_UX_SHOTS; await mkdir(dir, { recursive: true });
|
||||
const settle = () => new Promise(r => setTimeout(r, 1000));
|
||||
const mixed = { name: 'zeta-runner', results: [{ id: 'acme', outcome: 'off' }, { id: 'gamma', outcome: 'bus-down' }, { id: 'lab', outcome: 'failed:outcome-unknown' }], receipt: null };
|
||||
const off = async (b, wait, name) => {
|
||||
await wait(`document.querySelector("[data-program-off=${name}]")`);
|
||||
await b.evaluate(`document.querySelector("[data-program-off=${name}]").click(); true`);
|
||||
await wait('document.querySelector("#proxy-setup-programs-yes")');
|
||||
};
|
||||
const SHOTS = [
|
||||
{ name: 'advanced-programs', after: async b => openAdvanced(b), ready: 'document.querySelector("[data-program-off]")' },
|
||||
{ name: 'advanced-confirm', after: async (b, wait) => { await openAdvanced(b); await off(b, wait, 'zeta-runner'); } },
|
||||
{ name: 'advanced-result', options: { status: [SERVICE_UP, SERVICE_UP, AFTER], admin: { 'POST programs/off': mixed } },
|
||||
after: async (b, wait) => { await openAdvanced(b); await off(b, wait, 'zeta-runner'); await b.evaluate('document.querySelector("#proxy-setup-programs-yes").click(); true'); await wait('document.querySelectorAll("#proxy-setup-programs-result li").length === 3'); } },
|
||||
{ name: 'advanced-busy', options: { admin: { 'POST programs/off': { status: 409, body: { error: 'proxy-admin-busy' } } } },
|
||||
after: async (b, wait) => { await openAdvanced(b); await off(b, wait, 'pi'); await b.evaluate('document.querySelector("#proxy-setup-programs-yes").click(); true'); await wait('/was not turned off/.test(document.querySelector("#proxy-setup-programs-result").textContent)'); } },
|
||||
{ name: 'technical-access', options: { admin: { ...TECH, access: ACCESS } },
|
||||
after: async (b, wait) => { await openTech(b); await wait(`document.querySelectorAll("#proxy-setup-access li").length === ${ACCESS.organizations.length}`); } },
|
||||
{ name: 'technical-access-failed', options: { admin: { ...TECH, access: { status: 503, body: { error: 'outcome-unknown' } } } },
|
||||
after: async (b, wait) => { await openTech(b); await wait('!document.querySelector("#proxy-setup-access-retry").hidden'); } },
|
||||
];
|
||||
const report = [];
|
||||
for (const shot of SHOTS) for (const scheme of ['light', 'dark']) for (const width of [390, 1440]) {
|
||||
const { b, wait } = await setup(t, { ...(shot.options ?? {}), viewport: [width, width === 390 ? 844 : 900] });
|
||||
await b.call('Emulation.setScrollbarsHidden', { hidden: true });
|
||||
await b.call('Emulation.setEmulatedMedia', { features: [{ name: 'prefers-color-scheme', value: scheme }] });
|
||||
await wait(`(${READY}) && !!window.MosaicAppearance`);
|
||||
await b.evaluate(`window.MosaicAppearance.ready.then(() => window.MosaicAppearance.set({ mode: ${JSON.stringify(scheme)} }))`);
|
||||
await shot.after(b, wait);
|
||||
if (shot.ready) await wait(shot.ready);
|
||||
await settle();
|
||||
const sideways = await b.evaluate('document.documentElement.scrollWidth > document.documentElement.clientWidth');
|
||||
const noRing = [], seen = new Set();
|
||||
for (let i = 0; i < 200; i++) {
|
||||
await b.call('Input.dispatchKeyEvent', { type: 'keyDown', key: 'Tab', code: 'Tab', windowsVirtualKeyCode: 9 });
|
||||
await b.call('Input.dispatchKeyEvent', { type: 'keyUp', key: 'Tab', code: 'Tab', windowsVirtualKeyCode: 9 });
|
||||
const r = await b.evaluate(`(() => { const e = document.activeElement; if (!e || e === document.body) return null; const cs = getComputedStyle(e);
|
||||
const ring = (cs.outlineStyle !== 'none' && parseFloat(cs.outlineWidth) > 0) || (cs.boxShadow && cs.boxShadow !== 'none');
|
||||
return { key: e.id || e.dataset.programOff || e.getAttribute('href') || e.tagName + ':' + (e.textContent || '').trim().slice(0, 30), ring }; })()`);
|
||||
if (!r || seen.has(r.key)) break; seen.add(r.key);
|
||||
if (!r.ring) noRing.push(r.key);
|
||||
}
|
||||
// The result box takes focus from code, not Tab: check its ring too.
|
||||
const resultRing = await b.evaluate(`(() => { const e = document.querySelector("#proxy-setup-programs-result"); if (e.hidden) return null; e.focus(); const cs = getComputedStyle(e);
|
||||
return (cs.outlineStyle !== 'none' && parseFloat(cs.outlineWidth) > 0) || (cs.boxShadow && cs.boxShadow !== 'none'); })()`);
|
||||
if (resultRing === false) noRing.push('proxy-setup-programs-result');
|
||||
await b.evaluate('document.activeElement?.blur(); window.scrollTo(0, 0); true'); await settle();
|
||||
const file = `${shot.name}-${width}-${scheme}.png`;
|
||||
let metrics = await b.call('Page.getLayoutMetrics'), size = metrics.cssContentSize || metrics.contentSize;
|
||||
await b.viewport(width, Math.ceil(size.height)); await settle();
|
||||
metrics = await b.call('Page.getLayoutMetrics'); size = metrics.cssContentSize || metrics.contentSize;
|
||||
const { data } = await b.call('Page.captureScreenshot', { format: 'png', captureBeyondViewport: true, clip: { x: 0, y: 0, width: Math.ceil(size.width), height: Math.ceil(size.height), scale: 1 } });
|
||||
await writeFile(join(dir, file), Buffer.from(data, 'base64'));
|
||||
report.push({ page: shot.name, width, scheme, shot: file, sideways, noRing });
|
||||
await b.close();
|
||||
}
|
||||
await writeFile(join(dir, 'measure.json'), JSON.stringify(report, null, 1));
|
||||
for (const r of report) {
|
||||
assert.equal(r.sideways, false, `${r.shot} scrolls sideways`);
|
||||
assert.deepEqual(r.noRing, [], `${r.shot}: controls with no visible focus ring`);
|
||||
}
|
||||
});
|
||||
@@ -58,9 +58,13 @@ test("triageEnv keeps the fixed names and the route's proxy variables, and nothi
|
||||
assert.equal(triageEnv(CONSOLE_ENV, { ANTHROPIC_API_KEY: "from-the-route" }).ANTHROPIC_API_KEY, "from-the-route", "the route's key under the name its harness takes");
|
||||
assert.equal(triageEnv(CONSOLE_ENV).ANTHROPIC_API_KEY, undefined, "a key in the Console's environment is not the route's");
|
||||
// #1873 triage-wiring: the pinned harness binary passes as an absolute path, as a bus session's does.
|
||||
assert.deepEqual(SPAWN_PATH_ALLOW, ["MOSAIC_CLAUDE_BIN"]);
|
||||
// #1873 pi-unpin S2: and so does the pi binary the Console names, under the same checks.
|
||||
assert.deepEqual(SPAWN_PATH_ALLOW, ["MOSAIC_CLAUDE_BIN", "MOSAIC_PI_BIN"]);
|
||||
assert.equal(triageEnv({ ...CONSOLE_ENV, MOSAIC_CLAUDE_BIN: "/opt/claude/bin/claude" }).MOSAIC_CLAUDE_BIN, "/opt/claude/bin/claude");
|
||||
for (const bad of ["claude", "bin/claude", "/opt/a\nb", "/" + "x".repeat(300)]) assert.equal(triageEnv({ ...CONSOLE_ENV, MOSAIC_CLAUDE_BIN: bad }).MOSAIC_CLAUDE_BIN, undefined, bad);
|
||||
assert.equal(triageEnv({ ...CONSOLE_ENV, MOSAIC_PI_BIN: "/opt/pi/bin/pi" }).MOSAIC_PI_BIN, "/opt/pi/bin/pi");
|
||||
for (const name of SPAWN_PATH_ALLOW) {
|
||||
for (const bad of ["claude", "bin/claude", "/opt/a\nb", "/" + "x".repeat(300)]) assert.equal(triageEnv({ ...CONSOLE_ENV, [name]: bad })[name], undefined, `${name} ${bad}`);
|
||||
}
|
||||
});
|
||||
|
||||
test("the relay refuses every verb but a message to the person, and any call without the capability", async (t) => {
|
||||
@@ -275,6 +279,26 @@ test("fence 1: a name the harness module does not declare is refused; a declared
|
||||
assert.deepEqual(s.harnessEnv, { FAKE_OK: "yes" });
|
||||
});
|
||||
|
||||
// #1873 pi-unpin re-check 8 P3-2: the host awaits a module's ready() (pi's fit check) before the
|
||||
// bundle. This module's env() works only once its ready() has finished, as pi's reads the check's
|
||||
// pass; a ready() that rejects refuses the route though its env() would answer.
|
||||
test("a module's ready() is awaited before the bundle: env() after it works, and a ready() that rejects refuses the route", async (t) => {
|
||||
const gated = (roots, fail) => async (name) => {
|
||||
if (name !== "fake") return null;
|
||||
let done = false;
|
||||
return {
|
||||
envNames: DECLARED, installRoots: () => roots, transcript: ({ runDir }) => join(runDir, "session"),
|
||||
ready: async () => { await new Promise((r) => setTimeout(r, 20)); if (fail) throw new Error("fit refused"); done = true; },
|
||||
bundle: () => ({ files: {} }),
|
||||
// The host wants the route's key under its own name, as the key itself, and the proxy URL.
|
||||
env: ({ proxy }) => { if (!done && !fail) throw new Error("no passed check"); return { ANTHROPIC_AUTH_TOKEN: proxy.key, ANTHROPIC_BASE_URL: proxy.baseUrl }; },
|
||||
};
|
||||
};
|
||||
await admittedRoute(t, "ready first", () => ({}), (root) => ({ harnessModule: gated([join(root, "install")], false) }));
|
||||
const logs = await refusedRoute(t, "ready rejects", () => ({}), (root) => ({ harnessModule: gated([join(root, "install")], true) }));
|
||||
assert.ok(logs.includes("triage host: route refused: harness-bundle"), logs.join(" | "));
|
||||
});
|
||||
|
||||
test("fence 1: the bundle holds the copy the host checked; no MOSAIC_ name reaches session.json, even from a getter", async (t) => {
|
||||
let reads = 0;
|
||||
const s = await admittedRoute(t, "getter", (root) => () => {
|
||||
@@ -409,6 +433,8 @@ test("the shipped harness modules declare what their own env() and mounts() hand
|
||||
for (const name of ["pi", "claude-code"]) {
|
||||
const mod = await import(new URL(`../../plugins/harness/${name}/index.mjs`, import.meta.url).href);
|
||||
assert.ok(Array.isArray(mod.envNames) && Object.isFrozen(mod.envNames), name);
|
||||
// #1873 S3: pi's env() reads the fit check its ready() awaits.
|
||||
if (typeof mod.ready === "function") await mod.ready();
|
||||
const keys = Object.keys(mod.env({
|
||||
files: { settings: "/s", mcp: "/m" }, policy: { tools: ["read"] }, tools: [], session: { model: "m", thinking: "off", provider: "p" },
|
||||
}));
|
||||
@@ -438,7 +464,11 @@ test("a host whose HOME holds the repository starts the shipped pi harness's own
|
||||
// triage-wiring); the module's PI_PROVIDER is not the host's to set.
|
||||
const own = Object.fromEntries(Object.entries(s.harnessEnv).filter(([k]) => !k.startsWith("MOSAIC_")));
|
||||
assert.deepEqual(own, { PI_MODEL: "m", PI_OFFLINE: "1" });
|
||||
assert.deepEqual(Object.keys(s.harnessEnv).filter((k) => k.startsWith("MOSAIC_")).sort(), ["MOSAIC_EXTENSIONS", "MOSAIC_THINKING", "MOSAIC_TOOLS"]);
|
||||
assert.deepEqual(Object.keys(s.harnessEnv).filter((k) => k.startsWith("MOSAIC_")).sort(), ["MOSAIC_EXTENSIONS", "MOSAIC_PI_BIN", "MOSAIC_PI_NO_MCP", "MOSAIC_THINKING", "MOSAIC_TOOLS"]);
|
||||
// #1873 pi-unpin S3: the repository's pi has no --no-mcp, so the module sets it to "0".
|
||||
assert.equal(s.harnessEnv.MOSAIC_PI_NO_MCP, "0");
|
||||
// #1873 pi-unpin S2: the session runs the pi the module found, here the repository's copy.
|
||||
assert.ok(s.harnessEnv.MOSAIC_PI_BIN.startsWith(join(REPO, "node_modules") + "/"), s.harnessEnv.MOSAIC_PI_BIN);
|
||||
// The same host and module, with HOME's own directory (which holds the repository) as an extra
|
||||
// install root and mount: it is under HOME and not in the code, so it is refused.
|
||||
const wide = { ...pi, installRoots: (o) => [...pi.installRoots(o), home] };
|
||||
|
||||
Reference in new issue
Block a user