Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ca7388743b | ||
|
|
59af86a5ee | ||
|
|
d6d3271808 | ||
|
|
4a7815d29e | ||
|
|
af8c2f9777 | ||
|
|
b156727c00 | ||
|
|
29daa4c4bf | ||
|
|
c8db3665cf | ||
|
|
f2c262cf40 |
No files matched your search
@@ -106,6 +106,23 @@ keeps its existing channel; the CLI does not inherit the completion pipe. No ext
|
||||
writable mount or completion file is needed. Pi retains print mode and signals
|
||||
activity through its existing extension. Mock remains a test-only echo.
|
||||
|
||||
### A worker's skills (M17, #1873)
|
||||
|
||||
`MOSAIC_SKILLS` is a comma list of skill directories. Today only the
|
||||
interactive container launch (`scripts/agent.sh`) sets it; task launches
|
||||
refuse skill directories and set it empty
|
||||
(`packages/processing-control/src/containers.mjs`). The pi adapter passes `--no-skills` and then
|
||||
`--skill <dir>` for each one. On pi 0.85.1 `--skill` adds to discovery and
|
||||
does not replace it. With `--skill` alone, a probe with a scratch HOME also
|
||||
loaded the user's skills, the skills a `settings.json` or a listed package
|
||||
names, and `~/.agents/skills` (and the project's, without `--no-approve`).
|
||||
With `--no-skills` first, only the given directories load. A directory that
|
||||
doesn't exist stops the adapter with exit 2. The fit check runs the
|
||||
headless form of this argv, with two skill directories, one `-e` file, no
|
||||
tools and no session, as its last `-p` run (`workerShape`,
|
||||
`packages/harness/README.md`); the TUI shape the launch uses is not run. The fit check runs
|
||||
before bus sessions only; worker containers run the pi pinned in the image.
|
||||
|
||||
### A role's skills (row 64)
|
||||
|
||||
A managed session's skills reach both adapters as text in the bundle's
|
||||
|
||||
@@ -57,14 +57,18 @@ fi
|
||||
TOOLS_FLAG="--no-tools"
|
||||
[ -n "${MOSAIC_TOOLS:-}" ] && TOOLS_FLAG="--tools $MOSAIC_TOOLS"
|
||||
|
||||
# Skills (M17): explicitly provided skill dirs replace discovery. When none
|
||||
# are provided the agent runs with --no-skills (nothing ambient to find).
|
||||
# Skills (M17, #1873 pi-skills): --no-skills always, then one --skill per
|
||||
# given dir. On pi 0.85.1 --skill adds to discovery and does not replace it:
|
||||
# with --skill alone pi also loads the user's skills, settings and package
|
||||
# skills and ~/.agents/skills (a planted-skill probe showed it, offline, with a
|
||||
# scratch HOME). With --no-skills first only the given dirs load. The fit check
|
||||
# (packages/harness/src/pi-fit.mjs) runs its -p form as its last run (workerShape).
|
||||
# Only the interactive launch (scripts/agent.sh) sets MOSAIC_SKILLS today.
|
||||
# Role sessions never get MOSAIC_SKILLS (ENV_ALLOW in packages/seat/src/
|
||||
# session.mjs leaves it out), so they run with --no-skills; their skills
|
||||
# session.mjs leaves it out), so they run with --no-skills alone; their skills
|
||||
# are text in the system prompt (row 64).
|
||||
SKILLS_FLAG="--no-skills"
|
||||
if [ -n "${MOSAIC_SKILLS:-}" ]; then
|
||||
SKILLS_FLAG=""
|
||||
OLDIFS=$IFS; IFS=','
|
||||
for s in $MOSAIC_SKILLS; do
|
||||
[ -d "$s" ] || { echo "pi adapter: skill dir missing: $s" >&2; exit 2; }
|
||||
@@ -112,6 +116,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
|
||||
# SKILLS_FLAG --no-skills, then --skill <dir> for each MOSAIC_SKILLS dir
|
||||
# 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)
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
@@ -92,10 +92,29 @@ that no session sets. So a branch on `TZ` or `LC_ALL` being unset, or on
|
||||
`/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
|
||||
value it doesn't try (a model, a path, another tool or provider). A last `-p` run is the
|
||||
argv the adapter builds in `-p` with `MOSAIC_SKILLS`, one `-e` file, no
|
||||
tools and no session (`workerShape`), so `--no-skills` and then two
|
||||
`--skill` directories, `--no-tools`, `--no-session`, `--provider` and
|
||||
`--model`. A test runs the adapter and holds its argv to `workerShape`.
|
||||
No caller makes this exact run today: task launches refuse skill
|
||||
directories, and only the interactive launch (`scripts/agent.sh`) sets
|
||||
`MOSAIC_SKILLS`, in the TUI, which no fit run can drive. It is the
|
||||
nearest headless form of the adapter's `--skill` argv.
|
||||
The run's `-e` extension must list both skills and no planted one. pi
|
||||
0.85.1 adds a `--skill` directory to discovery, so a pi that let
|
||||
`--skill` bring the planted skills back is refused there, and so is one
|
||||
that drops a `--skill` directory once `--no-skills` is on. This run
|
||||
replaces the plain sealed `-p` run with `--no-session` and no `--skill`,
|
||||
so it adds no run. Each run gets a fresh scratch directory, so no run sees
|
||||
what another left. Not run: `--fork` and the interactive TUI
|
||||
(worker containers and `scripts/agent.sh` only), `-p` with
|
||||
`--no-session` and no `--skill` (a task launch's shape; every bus session
|
||||
passes `--session-dir`), `--skill` beside `--tools`, `--session-dir`,
|
||||
`-c`, `--thinking` or `--mode json`, or in rpc (the TUI launch passes the
|
||||
first three; no bus session passes `--skill`), `--skill` with no `-e`
|
||||
(every fit run needs its `-e` extension to see what loaded), one or three
|
||||
or more `--skill` directories, 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
|
||||
|
||||
@@ -39,8 +39,21 @@
|
||||
// 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.
|
||||
// worker -p the arguments adapters/pi/adapter.sh builds in -p with MOSAIC_SKILLS, one -e
|
||||
// file, no tools and no session, in its order (workerShape below): the -e,
|
||||
// --no-skills then two --skill directories, --no-tools, --no-session, the fixed
|
||||
// --provider and --model, --system-prompt, -p and a message. Its -e extension must
|
||||
// reach session_start and list both given skills, and no planted source may show.
|
||||
// pi 0.85.1 adds a --skill directory to discovery, so a pi that let --skill turn
|
||||
// discovery back on would show the planted skills here, and one that drops a --skill
|
||||
// directory once --no-skills is on would not list it. No caller makes this exact
|
||||
// run today: task launches refuse skill directories (packages/processing-control
|
||||
// containers.mjs), and only the interactive container launch (scripts/agent.sh)
|
||||
// sets MOSAIC_SKILLS, in the TUI with --tools and --session-dir, which no fit run
|
||||
// can drive. This is the nearest headless form of the adapter's --skill argv. It
|
||||
// replaces the sealed -p run the check made before (the seal flags in -p with
|
||||
// --no-session and no --skill), so it adds no run; that plain shape is not run on
|
||||
// its own any more.
|
||||
// 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
|
||||
//
|
||||
@@ -76,9 +89,13 @@
|
||||
// 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
|
||||
// check doesn't plant one; a session's agent directory is the run's own. Not run: --fork and
|
||||
// the interactive TUI, which only worker containers and scripts/agent.sh pass; -p with
|
||||
// --no-session and no --skill (a task launch's shape; every bus session passes --session-dir);
|
||||
// --skill beside --tools, --session-dir, -c, --thinking or --mode json, or in rpc (the TUI
|
||||
// launch passes the first three; no bus session passes --skill); --skill with no -e (every fit
|
||||
// run needs its -e extension to see what loaded); one or three or more --skill directories; 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,
|
||||
@@ -132,6 +149,19 @@ export function sessionShape({ noMcp, extensions, prompt, sessionDir, tools, con
|
||||
"--system-prompt", prompt, "-p", message, ...(json ? ["--mode", "json"] : []),
|
||||
];
|
||||
}
|
||||
// pi-skills: the arguments adapters/pi/adapter.sh builds in -p with MOSAIC_SKILLS, no tools and no
|
||||
// session: the given -e files, --no-skills and then a --skill per directory, --no-tools,
|
||||
// --no-session, the --provider and --model, the --system-prompt file, -p and the request. Only
|
||||
// the interactive launch sets MOSAIC_SKILLS today, in the TUI; this is the nearest headless form. Keep its order: pi-fit.test.mjs runs the adapter with MOSAIC_SKILLS and
|
||||
// fails when the two differ.
|
||||
export function workerShape({ noMcp, extensions, skills, prompt, message }) {
|
||||
return [
|
||||
"--offline", "--no-approve", "--no-extensions", ...(noMcp ? ["--no-mcp"] : []), ...extensions.flatMap((e) => ["-e", e]),
|
||||
"--no-skills", ...skills.flatMap((s) => ["--skill", s]), "--no-prompt-templates", "--no-themes", "--no-context-files",
|
||||
"--no-tools", "--no-session", "--provider", SESSION_SHAPE.provider, "--model", SESSION_SHAPE.model,
|
||||
"--system-prompt", prompt, "-p", message,
|
||||
];
|
||||
}
|
||||
// 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;
|
||||
@@ -313,7 +343,7 @@ async function check(found, { levels, bwrap, unshare, node, who, digest, host })
|
||||
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) };
|
||||
return { ...r, answer, explicit: marked(scratch, EXPLICIT), second: marked(scratch, SECOND), prompt: start?.prompt ?? null, session: start?.session ?? null, listed: named(listed), skills: GIVEN_SKILLS.every((n) => names(listed, n)), 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
|
||||
@@ -379,8 +409,9 @@ async function judge(found, { levels, who, digest, run }) {
|
||||
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 });
|
||||
// -p takes a message; offline with no key, pi stops before any model call. The adapter's -p argv
|
||||
// with MOSAIC_SKILLS: two --skill directories right after --no-skills, as the adapter puts them.
|
||||
const pRun = run((s) => workerShape({ noMcp, extensions: [join(s, "explicit", "fit.ts")], skills: GIVEN_SKILLS.map((n) => join(s, "given", n)), prompt: join(s, "prompt.md"), message: "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]) =>
|
||||
@@ -436,9 +467,10 @@ async function judge(found, { levels, who, digest, run }) {
|
||||
}
|
||||
|
||||
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`);
|
||||
if (p.stopped) return refuse(`can't be checked: the start in a worker's shape ${p.stopped}`);
|
||||
if (p.prompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt in a worker's shape");
|
||||
if (p.seen.length) return refuse(`loaded a planted ${p.seen.map((x) => x.source).join(", ")} in a worker's shape with --skill`);
|
||||
if (!p.skills) return refuse("didn't load every --skill directory with --no-skills on in a worker's shape");
|
||||
|
||||
for (const [mode, pending] of missingRuns) {
|
||||
const r = await pending;
|
||||
@@ -657,6 +689,9 @@ const GIVEN = "FITGIVENPROMPT";
|
||||
// pass after it.
|
||||
const EXPLICIT = "explicit extension";
|
||||
const SECOND = "second explicit extension";
|
||||
// The skills the worker-shaped run passes with --skill (scratch/given/<name>), outside every place
|
||||
// pi looks for skills, so they show only through --skill. Not planted ambient: not in NAMED.
|
||||
const GIVEN_SKILLS = ["fit-given-skill", "fit-second-given-skill"];
|
||||
// The id of the session a -c run must resume.
|
||||
const PLANTED_SESSION = "fit-planted-session";
|
||||
const markerName = (source) => source.replace(/[^a-z]/g, "-");
|
||||
@@ -669,8 +704,10 @@ function started(scratch) {
|
||||
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 };
|
||||
}
|
||||
// Whether command names hold name, bare or after a prefix such as "skill:".
|
||||
const names = (commands, name) => commands.some((c) => c === name || c.endsWith(`:${name}`));
|
||||
// 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 }));
|
||||
const named = (commands) => Object.entries(NAMED).filter(([name]) => names(commands, name)).map(([, [source, kind]]) => ({ source, kind }));
|
||||
|
||||
function plant(scratch) {
|
||||
const marks = join(scratch, "marks");
|
||||
@@ -715,6 +752,7 @@ function plant(scratch) {
|
||||
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 n of GIVEN_SKILLS) put(join(scratch, "given", n, "SKILL.md"), skill(n));
|
||||
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));
|
||||
|
||||
@@ -123,6 +123,33 @@ test("pi-unpin S3: a stand-in that touches the planted MCP marker under the seal
|
||||
for (const [kind, line] of Object.entries(lines)) assert.equal((await fit(standIn(t, { sealLeak: [kind] }).env)).message, `pi 2.0.0 (MOSAIC_PI_BIN) ${line}`, kind);
|
||||
});
|
||||
|
||||
// pi-skills: the worker's shape. pi 0.85.1 adds a --skill directory to discovery, so the adapter
|
||||
// keeps --no-skills in front of each --skill; the fit check's last -p run passes the adapter's -p
|
||||
// argv with MOSAIC_SKILLS (workerShape) and two --skill directories.
|
||||
test("pi-skills: a stand-in that loads the planted skills once a --skill directory is given is refused", SKIP, async t => {
|
||||
const s = standIn(t, { skillLeak: true });
|
||||
const problem = await pi.preflight({ env: { ...CREDENTIAL, ...s.env } });
|
||||
assert.deepEqual(problem, { code: "harness-feature-missing", message: "pi 2.0.0 (MOSAIC_PI_BIN) loaded a planted user skill, user settings skill, user package skill, project skill, project settings skill, project package skill, project .agents skill, home .agents skill in a worker's shape with --skill" });
|
||||
noPath(problem.message, s.paths);
|
||||
});
|
||||
|
||||
test("pi-skills: a stand-in that drops a --skill directory once --no-skills is on is refused", SKIP, async t => {
|
||||
const s = standIn(t, { ignoreSkill: true });
|
||||
const problem = await pi.preflight({ env: { ...CREDENTIAL, ...s.env } });
|
||||
assert.deepEqual(problem, { code: "harness-feature-missing", message: "pi 2.0.0 (MOSAIC_PI_BIN) didn't load every --skill directory with --no-skills on in a worker's shape" });
|
||||
noPath(problem.message, s.paths);
|
||||
});
|
||||
|
||||
// Review P3-1: the run is the adapter's -p argv, --provider and --model included, so a pi that
|
||||
// leaks only once --skill and --provider are both given is seen.
|
||||
test("pi-skills review: a stand-in that loads the planted sources only with --skill and --provider is refused", SKIP, async t => {
|
||||
const s = standIn(t, { leakWith: ["--skill", "--provider"] });
|
||||
const problem = await pi.preflight({ env: { ...CREDENTIAL, ...s.env } });
|
||||
assert.equal(problem.code, "harness-feature-missing");
|
||||
assert.match(problem.message, /^pi 2\.0\.0 \(MOSAIC_PI_BIN\) loaded a planted .* in a worker's shape with --skill$/);
|
||||
noPath(problem.message, s.paths);
|
||||
});
|
||||
|
||||
test("pi-unpin S3: a stand-in that ignores the planted sources even without the seal flags fails the positive control and is refused as can't tell", SKIP, async t => {
|
||||
const s = standIn(t, { ignoreAll: true });
|
||||
const problem = await pi.preflight({ env: { ...CREDENTIAL, ...s.env } });
|
||||
@@ -188,7 +215,12 @@ test("pi-unpin S3 re-check 3: every run has its own scratch directory, and the r
|
||||
const shaped = (args) => args.includes("--session-dir");
|
||||
assert.deepEqual(runs.filter((x) => shaped(x.args)).map((x) => JSON.stringify(x.args)).sort(), shapes.map((shape) => JSON.stringify(fitModule.sessionShape({ noMcp: extraFlags.length > 0, extensions: ["<s>/explicit/fit.ts", "<s>/explicit/second.ts"], prompt: "<s>/prompt.md", sessionDir: "<s>/sessions", message: "fit check", ...shape }))).sort());
|
||||
const flagLists = [];
|
||||
for (const { args } of runs.filter((x) => x.args.length > 1 && !shaped(x.args))) {
|
||||
// pi-skills: the last -p run is the adapter's -p argv with two --skill directories, built by
|
||||
// workerShape, which the adapter test below holds to the adapter. No other run passes --skill.
|
||||
const skilled = runs.filter((x) => x.args.includes("--skill"));
|
||||
assert.equal(skilled.length, 1);
|
||||
assert.deepEqual(skilled[0].args, fitModule.workerShape({ noMcp: extraFlags.length > 0, extensions: ["<s>/explicit/fit.ts"], skills: ["<s>/given/fit-given-skill", "<s>/given/fit-second-given-skill"], prompt: "<s>/prompt.md", message: "fit check" }));
|
||||
for (const { args } of runs.filter((x) => x.args.length > 1 && !shaped(x.args) && !x.args.includes("--skill"))) {
|
||||
const flags = args.filter((a) => seal.includes(a));
|
||||
assert.deepEqual(flags, seal.filter((f) => flags.includes(f)), "the seal flags keep their order");
|
||||
const rest = [];
|
||||
@@ -863,6 +895,26 @@ test("pi-unpin S3: real pi 0.85.1 from node_modules is accepted, and the event l
|
||||
assert.ok(max <= GAP_MAX, `the event loop waited ${max.toFixed(0)} ms during the cold check`);
|
||||
});
|
||||
|
||||
// pi-skills: real pi passes the fit check's worker-shaped -p run, --no-skills and two --skill
|
||||
// directories: it lists both given skills and no planted one. A logging bwrap shows the run.
|
||||
test("pi-skills: real pi 0.85.1 passes the fit check's worker-shaped run with --no-skills and two --skill directories", SKIP, async t => {
|
||||
const found = locate({ PATH: "/usr/bin:/bin" });
|
||||
const root = mkdtempSync(join(tmpdir(), "mosaic-pi-skills-fit-"));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const log = join(root, "bwrap.log");
|
||||
const bwrap = join(root, "bwrap");
|
||||
writeFileSync(bwrap, `#!/bin/sh\nf=$(/usr/bin/mktemp '${log}.XXXXXX')\nfor a in "$@"; do printf '%s\\037' "$a"; done > "$f"\nexec '${BWRAP}' "$@"\n`, { mode: 0o755 });
|
||||
const r = await piFit(found, { levels: LEVELS, bwrap });
|
||||
assert.equal(r.ok, true, r.message);
|
||||
const skilled = readdirSync(root).filter((n) => n.startsWith("bwrap.log.")).map((n) => readFileSync(join(root, n), "utf8").split("\x1f")).filter((a) => a.includes("--skill"));
|
||||
assert.equal(skilled.length, 1);
|
||||
const a = skilled[0];
|
||||
assert.equal(a[a.indexOf("--no-skills") + 1], "--skill");
|
||||
assert.match(a[a.indexOf("--skill") + 1], /\/given\/fit-given-skill$/);
|
||||
assert.match(a[a.indexOf("--skill") + 3], /\/given\/fit-second-given-skill$/);
|
||||
for (const f of ["-p", "--no-session", "--provider", "--model"]) assert.ok(a.includes(f), f);
|
||||
});
|
||||
|
||||
test("pi-unpin S3 re-check 7: the event loop stays live while a stand-in's cold check runs", SKIP, async t => {
|
||||
const { result, max } = await gapDuring(() => fit(standIn(t, {}).env));
|
||||
assert.equal(result.ok, true, result.message);
|
||||
@@ -957,6 +1009,95 @@ test("pi-unpin S3: the adapter passes --no-mcp only when the module asks for it"
|
||||
assert.equal((await run(standIn(t, {}))).includes("--no-mcp"), false);
|
||||
});
|
||||
|
||||
// pi-skills: a worker's MOSAIC_SKILLS. pi 0.85.1 adds a --skill directory to discovery, so the
|
||||
// adapter keeps --no-skills and puts each --skill after it. A stub records the argv.
|
||||
const workerAdapter = (t, extra = {}) => {
|
||||
const root = mkdtempSync(join(tmpdir(), "mosaic-pi-skills-t-"));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const stub = join(root, "pi");
|
||||
writeFileSync(stub, '#!/bin/sh\nprintf "%s\\0" "$@" > "$ARGV_OUT"\n', { mode: 0o755 });
|
||||
writeFileSync(join(root, "prompt.md"), "p\n");
|
||||
const env = { PATH: "/usr/bin:/bin", HOME: join(root, "home"), ARGV_OUT: join(root, "argv"), MOSAIC_PI_BIN: stub, MOSAIC_SYSTEM_PROMPT_FILE: join(root, "prompt.md"), PI_PROVIDER: "fixture", PI_MODEL: "m", MOSAIC_REQUEST: "x", ...extra(root) };
|
||||
const r = spawnSync("/bin/sh", [join(REPO, "adapters", "pi", "adapter.sh")], { encoding: "utf8", env });
|
||||
return { root, r, argv: () => readFileSync(join(root, "argv"), "utf8").split("\0").slice(0, -1) };
|
||||
};
|
||||
|
||||
test("pi-skills: with MOSAIC_SKILLS the adapter passes --no-skills, then --skill for each directory", t => {
|
||||
const w = workerAdapter(t, (root) => {
|
||||
for (const n of ["one", "two"]) mkdirSync(join(root, "skills", n), { recursive: true });
|
||||
return { MOSAIC_SKILLS: `${join(root, "skills", "one")},${join(root, "skills", "two")}` };
|
||||
});
|
||||
assert.equal(w.r.status, 0, w.r.stderr);
|
||||
const argv = w.argv();
|
||||
const i = argv.indexOf("--no-skills");
|
||||
assert.ok(i >= 0, argv.join(" "));
|
||||
assert.equal(argv.filter((a) => a === "--no-skills").length, 1, argv.join(" "));
|
||||
assert.deepEqual(argv.slice(i, i + 5), ["--no-skills", "--skill", join(w.root, "skills", "one"), "--skill", join(w.root, "skills", "two")]);
|
||||
assert.equal(argv.filter((a) => a === "--skill").length, 2, argv.join(" "));
|
||||
// Without MOSAIC_SKILLS: --no-skills alone, as before.
|
||||
const bare = workerAdapter(t, () => ({}));
|
||||
assert.equal(bare.r.status, 0, bare.r.stderr);
|
||||
assert.ok(bare.argv().includes("--no-skills"));
|
||||
assert.ok(!bare.argv().includes("--skill"));
|
||||
});
|
||||
|
||||
// Review P3-1: the fit check's worker-shaped run is the argv the adapter builds in -p with
|
||||
// MOSAIC_SKILLS, an -e file, no tools and no session, flag for flag and in order.
|
||||
test("pi-skills review: the adapter's -p argv with MOSAIC_SKILLS is workerShape's", t => {
|
||||
const w = workerAdapter(t, (root) => {
|
||||
for (const n of ["one", "two"]) mkdirSync(join(root, "skills", n), { recursive: true });
|
||||
writeFileSync(join(root, "fit.ts"), "");
|
||||
return { MOSAIC_SKILLS: `${join(root, "skills", "one")},${join(root, "skills", "two")}`, MOSAIC_EXTENSIONS: join(root, "fit.ts"), PI_PROVIDER: fitModule.SESSION_SHAPE.provider, PI_MODEL: fitModule.SESSION_SHAPE.model };
|
||||
});
|
||||
assert.equal(w.r.status, 0, w.r.stderr);
|
||||
assert.deepEqual(w.argv(), fitModule.workerShape({ noMcp: false, extensions: [join(w.root, "fit.ts")], skills: [join(w.root, "skills", "one"), join(w.root, "skills", "two")], prompt: join(w.root, "prompt.md"), message: "x" }));
|
||||
});
|
||||
|
||||
test("pi-skills: a missing MOSAIC_SKILLS directory still exits 2 before pi runs", t => {
|
||||
const w = workerAdapter(t, (root) => {
|
||||
mkdirSync(join(root, "skills", "one"), { recursive: true });
|
||||
return { MOSAIC_SKILLS: `${join(root, "skills", "one")},${join(root, "skills", "gone")}` };
|
||||
});
|
||||
assert.equal(w.r.status, 2);
|
||||
assert.match(w.r.stderr, /^pi adapter: skill dir missing: /);
|
||||
assert.equal(existsSync(join(w.root, "argv")), false, "pi did not run");
|
||||
});
|
||||
|
||||
// pi-skills: the adapter's -p shape end to end. It runs real pi 0.85.1 with MOSAIC_SKILLS, an
|
||||
// -e extension that writes the command names at session_start, a HOME and workspace holding planted
|
||||
// skills in every place pi 0.85.1 looks, the network unshared and no key: pi stops before any model
|
||||
// call. Only the given skills may load. On the adapter that dropped --no-skills, the user, settings
|
||||
// and ~/.agents skills loaded too.
|
||||
const UNSHARE_NET = spawnSync("unshare", ["-rn", "true"]).status === 0 ? {} : { skip: "no unshare -rn here" };
|
||||
test("pi-skills: real pi 0.85.1 through the adapter with MOSAIC_SKILLS loads the given skills and no planted one", UNSHARE_NET, async t => {
|
||||
const found = locate({ PATH: "/usr/bin:/bin" });
|
||||
assert.equal(found.label, "the repository's pi");
|
||||
const root = mkdtempSync(join(tmpdir(), "mosaic-pi-skills-real-"));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const put = (p, text) => { mkdirSync(dirname(p), { recursive: true }); writeFileSync(p, text); };
|
||||
const skill = (name) => put(join(root, ...name.split("/"), "SKILL.md"), `---\nname: ${name.split("/").pop()}\ndescription: a planted skill\n---\nA planted skill.\n`);
|
||||
const home = join(root, "home");
|
||||
const ws = join(root, "ws");
|
||||
for (const s of ["home/.pi/agent/skills/amb-user-skill", "home/.agents/skills/amb-home-skill", "listed/amb-set-skill", "listed/pkg/skills/amb-pkg-skill", "ws/.pi/skills/amb-project-skill", "ws/.agents/skills/amb-agents-skill", "given/one-skill", "given/two-skill"]) skill(s);
|
||||
put(join(root, "listed", "pkg", "package.json"), JSON.stringify({ name: "amb-package", version: "1.0.0" }));
|
||||
put(join(home, ".pi", "agent", "settings.json"), JSON.stringify({ skills: [join(root, "listed", "amb-set-skill")], packages: [join(root, "listed", "pkg")], defaultProjectTrust: "always" }));
|
||||
put(join(ws, ".pi", "settings.json"), JSON.stringify({ skills: [join(root, "listed", "amb-set-skill")] }));
|
||||
const marks = join(root, "start.json");
|
||||
put(join(root, "ext", "list.ts"), `import { writeFileSync } from "node:fs";\nexport default function (pi: any): void {\n pi.on("session_start", () => { writeFileSync(${JSON.stringify(marks)}, JSON.stringify(pi.getCommands().map((c: any) => c.name))); });\n}\n`);
|
||||
put(join(root, "prompt.md"), "p\n");
|
||||
mkdirSync(join(root, "tmp"));
|
||||
const env = {
|
||||
PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, HOME: home, TMPDIR: join(root, "tmp"), LANG: "C.UTF-8",
|
||||
MOSAIC_PI_BIN: found.bin, MOSAIC_SYSTEM_PROMPT_FILE: join(root, "prompt.md"), PI_PROVIDER: fitModule.SESSION_SHAPE.provider, PI_MODEL: fitModule.SESSION_SHAPE.model,
|
||||
MOSAIC_REQUEST: "fit check", MOSAIC_WORKSPACE: ws, MOSAIC_EXTENSIONS: join(root, "ext", "list.ts"),
|
||||
MOSAIC_SKILLS: `${join(root, "given", "one-skill")},${join(root, "given", "two-skill")}`,
|
||||
};
|
||||
spawnSync("unshare", ["-rn", "/bin/sh", join(REPO, "adapters", "pi", "adapter.sh")], { encoding: "utf8", env, timeout: 60000 });
|
||||
const commands = JSON.parse(readFileSync(marks, "utf8"));
|
||||
const skills = commands.filter((c) => c.startsWith("skill:")).map((c) => c.slice(6)).sort();
|
||||
assert.deepEqual(skills, ["one-skill", "two-skill"]);
|
||||
});
|
||||
|
||||
// Re-check 5 item 1, narrowed by re-check 6 P3-1. The adapter runs with the environment a bus
|
||||
// session's pi gets (busEnv: sessionEnv over a host, sessionSetenv, adapterEnv, pi.env()), so
|
||||
// HOME and the rest are set as a session sets them, not by the shell. Re-check 7 P2-1: every run is
|
||||
|
||||
@@ -54,6 +54,10 @@
|
||||
// no commands.
|
||||
// `commands` "fail" answers get_commands with success false; "list" answers
|
||||
// with a bare list.
|
||||
// --skill <dir> loads that skill (its directory's name) whatever --no-skills says,
|
||||
// as pi 0.85.1 does. `skillLeak`: with a --skill given, the planted skills load too
|
||||
// whatever --no-skills says (discovery comes back with --skill). `ignoreSkill`: a
|
||||
// --skill directory loads only without --no-skills.
|
||||
// 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
|
||||
@@ -173,12 +177,13 @@ if (rpc || print) {
|
||||
for (const s of Object.values(json(f)?.mcpServers || {})) cp.spawnSync(s.command, s.args || [], { stdio: "ignore" });
|
||||
}
|
||||
}
|
||||
if (on("skills", "--no-skills")) {
|
||||
if (on("skills", "--no-skills") || (K.skillLeak && has("--skill") && !ignoreAll)) {
|
||||
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 (!(K.ignoreSkill && has("--no-skills"))) for (const p of argv.flatMap((a, i) => a === "--skill" ? [argv[i + 1]] : [])) 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" });
|
||||
|
||||
@@ -11,6 +11,7 @@ import { releaseState, initState, fenceState, initLockStatus, clearInitLock, app
|
||||
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 });
|
||||
@@ -79,7 +80,7 @@ const isoTime = value => (typeof value === 'string' && /^[0-9]{4}-[0-9]{2}-[0-9]
|
||||
// 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 }) {
|
||||
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 };
|
||||
@@ -273,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,
|
||||
|
||||
@@ -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 }); }
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
@@ -367,6 +367,87 @@ the kind's whole setting, so it names every harness that stays on:
|
||||
{ "revision": "<from mosaic plugins>", "plugins": { "harness": ["pi", "claude-code"] } }
|
||||
```
|
||||
|
||||
### Harness watch and `inspect` (#1873, slice W1)
|
||||
|
||||
Jason's ruling (2026-11-19): no pins on any harness, a periodic check
|
||||
instead. `src/harness-watch.mjs` is the function that check calls.
|
||||
`watchHarnesses({ last, env, scratch, dataRoot, timeoutMs })` makes one
|
||||
pass over every harness `loadInstalled('harness', { dataRoot })` gives,
|
||||
tree or installed. It holds no name list, so a harness added later is
|
||||
watched at once. It writes nothing: it takes the records the last pass
|
||||
returned and gives back `{ records, transitions, refused, gone }`.
|
||||
|
||||
`inspect({ env, scratch, signal })` is an optional harness export, outside
|
||||
`kinds.mjs`. It returns a promise of:
|
||||
|
||||
| Field | Meaning |
|
||||
|---|---|
|
||||
| `found` | `true` when the binary sessions run was found, `false` when not |
|
||||
| `source` | the lookup step that won, such as `MOSAIC_PI_BIN`, `PATH` or `repo`; never a path |
|
||||
| `version` | the binary's version label, or null |
|
||||
| `digest` | a sha256 hex digest of what runs, or null |
|
||||
| `fit` | `"pass"`, `"fail"` or `"unknown"`: the module's own fit check |
|
||||
| `definite` | for a `"fail"`, true when the check decided; a failure that could not tell is false |
|
||||
| `failed` | rule ids the check names, when it names any |
|
||||
| `line` | one plain line for the owner; it names a variable or a lookup step, never a path |
|
||||
| `surfaces` | name to text, such as `help` |
|
||||
|
||||
`env` is the environment of the process sessions launch from. `scratch` is
|
||||
a directory the module may use for a scratch HOME; it removes what it makes
|
||||
there. `signal` aborts when the watch stops waiting. `pi` returns its fit
|
||||
check (`packages/harness/src/pi-fit.mjs`), called exactly as `preflight`
|
||||
calls it, so the two share one cache and one running check. Its digest is
|
||||
that check's digest of the binary's files; a failed check gives none. A
|
||||
failure whose line, after pi's lookup label, starts with "can't be checked"
|
||||
is `fit: "unknown"`.
|
||||
`claude-code` returns the sha256 of the binary's real file and the first
|
||||
line of `--version`, and captures `--help` as the `help` surface, both run
|
||||
from that same real file with only PATH and a scratch HOME. Its lookup for
|
||||
the watch skips a relative PATH entry, as pi's does. Its fit stays
|
||||
`"unknown"` until slice W2 adds the claude fit rule (and runs these
|
||||
captures with no network).
|
||||
|
||||
A module with no `inspect` is watched through `version({ env })`, with fit
|
||||
`"unknown"`. A record's `state` is:
|
||||
|
||||
- `fit`: found, the fit check passed, the last known digest (or none known);
|
||||
- `changed`: found, the fit check passed, a digest that is not the last known
|
||||
one. The last known digest is the latest any earlier record saw, kept in
|
||||
each record's `lastDigest` across unknown, red and missing records, so a
|
||||
new binary first seen through a timeout still reads `changed`;
|
||||
- `red`: found, and the fit check failed with `definite: true`;
|
||||
- `unknown`: anything else, including no `inspect`, an `inspect` that throws
|
||||
or runs past `timeoutMs` (default 300 s), and an answer that isn't this
|
||||
shape;
|
||||
- `missing`: `inspect` says `found: false`.
|
||||
|
||||
Only `found: true` with `fit: "pass"` reads as `fit` or `changed`. The watch
|
||||
copies the answer's own data fields only: a field that is a getter, or an
|
||||
answer that throws when read (a Proxy), is a failed `inspect`, and an
|
||||
inherited field is not read. It checks each field's shape and drops what
|
||||
doesn't fit (a `source` that isn't a plain name, a digest that isn't 64 hex
|
||||
characters, a `source` or rule id that is an env value). In every text
|
||||
field it replaces the scratch directory, the home directory, every env value
|
||||
of six characters or more and every PATH entry, each also as its real path
|
||||
and without a trailing slash, with a name for it, the user name (USER,
|
||||
LOGNAME, the process's account) with `<user>`, then any other absolute path
|
||||
of two segments or more with `<path>`. The loader's refusals in `refused`
|
||||
get the same treatment. A record also carries `via` (`inspect` or
|
||||
`version`), `lastDigest`, `checkedAt` and `changedAt`. A transition
|
||||
(`{ harness, at, from, to, failed, line }`, with `from` null on a first
|
||||
sight) is given only when the state or the binary is not the last record's.
|
||||
The digest names the binary; when neither record has one (pi gives none for
|
||||
a failed check), the version label stands in for it, and only when both
|
||||
records have a version: a timeout or a throw knows none, and is not a new
|
||||
binary. So unknown 2.0, a timeout, then unknown 2.1, all with no digest,
|
||||
gives no transition for the version; W1 keeps no last known version.
|
||||
|
||||
The redaction applies to the stored surfaces too, so a surface is for
|
||||
display and comparison, not for fit rules: with USER set to `model`, the
|
||||
stored `help` reads `--<user>` where claude printed `--model`. A fit rule
|
||||
(slice W2's flag check, for one) runs inside the module on the text the
|
||||
binary printed and puts its answer in `fit`, `failed` and `line`.
|
||||
|
||||
## Transcript usage source (row 124)
|
||||
|
||||
`usage-source/transcript` now ships interface 1's `readTurn({harness, root})`.
|
||||
|
||||
@@ -4,8 +4,9 @@
|
||||
// both stay 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 { spawnSync } from "node:child_process";
|
||||
import { accessSync, constants, realpathSync, statSync } from "node:fs";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { accessSync, constants, createReadStream, mkdtempSync, realpathSync, rmSync, statSync } from "node:fs";
|
||||
import { delimiter, dirname, isAbsolute, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { claudeBuiltins, MCP_PREFIX } from "../../../harness/src/gate.mjs";
|
||||
@@ -89,6 +90,61 @@ export function version({ env }) {
|
||||
return r.status === 0 ? r.stdout.trim().split("\n")[0] || null : null;
|
||||
}
|
||||
|
||||
// #1873 harness watch W1: what the watch records for the binary sessions run: the lookup step that
|
||||
// found it (MOSAIC_CLAUDE_BIN or PATH, never a path), the sha256 of its real file, the first line
|
||||
// of --version and the whole --help as a surface. Both run with an empty environment but PATH and
|
||||
// a fresh HOME under scratch, which is removed after. Without scratch they don't run. The real path
|
||||
// is resolved once, then hashed and run, so the digest and the captures are of one file; a relative
|
||||
// PATH entry is skipped, as pi's lookup skips it. Fit stays "unknown": the claude fit rule (every
|
||||
// flag the adapter passes is in --help) is slice W2, with the no-network wrapper for these runs.
|
||||
const CAPTURE_TIMEOUT = 10000;
|
||||
const CAPTURE_MAX = 1024 * 1024;
|
||||
export async function inspect({ env = {}, scratch = null, signal } = {}) {
|
||||
const bin = binary({ ...env, PATH: (env.PATH ?? "").split(delimiter).filter((dir) => isAbsolute(dir)).join(delimiter) });
|
||||
const source = env.MOSAIC_CLAUDE_BIN ? "MOSAIC_CLAUDE_BIN" : bin ? "PATH" : null;
|
||||
const base = { source, fit: "unknown", definite: false, failed: [], surfaces: {} };
|
||||
if (!bin) return { ...base, found: false, version: null, digest: null, line: env.MOSAIC_CLAUDE_BIN ? "MOSAIC_CLAUDE_BIN isn't an executable file at an absolute path" : "no claude on PATH" };
|
||||
let real;
|
||||
try { real = realpathSync(bin); } catch { return { ...base, found: false, version: null, digest: null, line: `the claude from ${source} can't be resolved` }; }
|
||||
const digest = await sha256(real);
|
||||
if (typeof scratch !== "string" || !isAbsolute(scratch)) return { ...base, found: true, version: null, digest, line: "no scratch directory, so --version and --help were not run; fit not checked" };
|
||||
const home = mkdtempSync(join(scratch, "claude-home-"));
|
||||
try {
|
||||
const runEnv = { PATH: "/usr/bin:/bin", HOME: home };
|
||||
const [v, help] = [await capture(real, ["--version"], runEnv, signal), await capture(real, ["--help"], runEnv, signal)];
|
||||
const line = v?.split("\n")[0].trim() ?? "";
|
||||
const version = line && line.length <= 128 ? line : null;
|
||||
return { ...base, found: true, version, digest, line: help === null ? "--help gave no answer; fit not checked" : "fit not checked", surfaces: help === null ? {} : { help } };
|
||||
} finally {
|
||||
rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
function sha256(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const h = createHash("sha256");
|
||||
createReadStream(file).on("error", reject).on("data", (c) => h.update(c)).on("end", () => resolve(h.digest("hex")));
|
||||
});
|
||||
}
|
||||
|
||||
// stdout of bin args when it exits 0 within CAPTURE_TIMEOUT and prints at most CAPTURE_MAX, else null.
|
||||
function capture(bin, args, runEnv, signal) {
|
||||
return new Promise((resolve) => {
|
||||
let child;
|
||||
try { child = spawn(bin, args, { env: runEnv, cwd: runEnv.HOME, stdio: ["ignore", "pipe", "ignore"] }); } catch { resolve(null); return; }
|
||||
const out = [];
|
||||
let size = 0, done = false;
|
||||
const finish = (v) => { if (done) return; done = true; clearTimeout(timer); signal?.removeEventListener?.("abort", kill); resolve(v); };
|
||||
const kill = () => { try { child.kill("SIGKILL"); } catch {} finish(null); };
|
||||
const timer = setTimeout(kill, CAPTURE_TIMEOUT);
|
||||
signal?.addEventListener?.("abort", kill, { once: true });
|
||||
if (signal?.aborted) kill();
|
||||
child.stdout.on("data", (c) => { size += c.length; if (size > CAPTURE_MAX) kill(); else out.push(c); });
|
||||
child.on("error", () => finish(null));
|
||||
child.on("close", (status) => finish(status === 0 ? Buffer.concat(out).toString("utf8") : null));
|
||||
});
|
||||
}
|
||||
|
||||
// Its launches spend the business's launch.account (row 126).
|
||||
export const countsAccount = () => true;
|
||||
|
||||
|
||||
@@ -55,6 +55,28 @@ export async function version({ env = {} } = {}) {
|
||||
return printedVersion(locate(env).bin);
|
||||
}
|
||||
|
||||
// #1873 harness watch W1: what the watch records for the pi binary sessions run. It calls the fit
|
||||
// check exactly as preflight does (same found binary, same levels, this process's environment), so
|
||||
// it shares preflight's cache and its running check: a binary that passed is not checked again.
|
||||
// digest is the fit check's digest of the binary's files; a check that fails gives none, since
|
||||
// pi-fit.mjs exports no digest of its own. A failure whose reason starts "can't be checked" could
|
||||
// not decide, so its fit is "unknown"; any other failure is definite. source names the lookup step,
|
||||
// never a path. scratch is not used: the fit check makes its own scratch directories.
|
||||
const SOURCES = Object.freeze({ MOSAIC_PI_BIN: "MOSAIC_PI_BIN", "the pi on PATH": "PATH", "the repository's pi": "repo" });
|
||||
export async function inspect({ env = {} } = {}) {
|
||||
const at = locate(env);
|
||||
if (!at.bin) return { found: false, source: env.MOSAIC_PI_BIN ? "MOSAIC_PI_BIN" : null, version: null, digest: null, fit: "unknown", definite: false, failed: [], line: at.problem, surfaces: {} };
|
||||
const source = SOURCES[at.label] ?? null;
|
||||
const fit = await piFit(at, { levels: LEVELS });
|
||||
if (fit.ok) return { found: true, source, version: fit.version ?? null, digest: fit.digest, fit: "pass", definite: true, failed: [], line: `pi${fit.version ? ` ${fit.version}` : ""} (${at.label}) passed the fit check`, surfaces: {} };
|
||||
// Read after pi-fit's "pi <version> (<label>) " prefix, so a version label cannot decide it. A line
|
||||
// without that prefix keeps the whole-line match: an unknown line must not read as definite.
|
||||
const mark = `(${at.label}) `;
|
||||
const cut = fit.message.indexOf(mark);
|
||||
const undecided = cut === -1 ? /can't be checked/.test(fit.message) : fit.message.slice(cut + mark.length).startsWith("can't be checked");
|
||||
return { found: true, source, version: await printedVersion(at.bin), digest: null, fit: undecided ? "unknown" : "fail", definite: !undecided, failed: [], line: fit.message, surfaces: {} };
|
||||
}
|
||||
|
||||
// #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 } = {}) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1,254 @@
|
||||
// #1873 harness watch, slice W1 (reports/harness-watch-design.md sections 1 and 5): one pass over
|
||||
// every harness the loader gives, tree or installed. No name list: a harness added later is
|
||||
// watched at once. For each module it calls the optional inspect({ env, scratch, signal }), or
|
||||
// falls back to version({ env }) with fit "unknown". It compares the answer with the last record
|
||||
// the caller passes in and returns the new records and the transitions. It writes nothing: no
|
||||
// state file, no log, no timer. The caller (W2's host timer) keeps the records.
|
||||
//
|
||||
// A record's state:
|
||||
// fit found, the module's fit check passed, and the digest is the last known one (or there
|
||||
// is no known digest to compare with)
|
||||
// changed found, the fit check passed, and the digest differs from the last known one: the
|
||||
// latest digest any earlier record saw, kept in lastDigest across unknown, red and
|
||||
// missing records, so a new binary first seen through a timeout still reads changed
|
||||
// red found, and the fit check failed with a definite answer
|
||||
// unknown anything else: the check could not decide, the module has no inspect, inspect threw,
|
||||
// timed out or gave an answer that isn't the contract's
|
||||
// missing inspect says the binary is not found
|
||||
// Only fit === "pass" with found === true reads as fit or changed. Nothing else can.
|
||||
//
|
||||
// No record field holds a host path or an env value: source must be a lookup step's name
|
||||
// (MOSAIC_PI_BIN, PATH, repo), never a path, and every text field has the scratch directory,
|
||||
// the home directory, each env value and each PATH entry (and their real paths) replaced by a
|
||||
// name for it, then every other absolute path replaced by <path>.
|
||||
import { realpathSync } from "node:fs";
|
||||
import { homedir, userInfo } from "node:os";
|
||||
import { isAbsolute } from "node:path";
|
||||
import { loadInstalled } from "./loader.mjs";
|
||||
|
||||
export const STATES = Object.freeze(["fit", "changed", "red", "unknown", "missing"]);
|
||||
const FITS = new Set(["pass", "fail", "unknown"]);
|
||||
// An inspect that takes longer than this gives unknown. pi's fit check runs many sandboxed starts.
|
||||
export const INSPECT_TIMEOUT_MS = 300000;
|
||||
const SOURCE = /^[A-Za-z][A-Za-z0-9_]{0,63}$/;
|
||||
const RULE = /^[A-Za-z0-9][A-Za-z0-9_.:-]{0,63}$/;
|
||||
const SURFACE = /^[a-z][a-z0-9-]{0,31}$/;
|
||||
const DIGEST = /^[a-f0-9]{64}$/;
|
||||
const LABEL_MAX = 128;
|
||||
const LINE_MAX = 500;
|
||||
const SURFACE_MAX = 256 * 1024;
|
||||
const SURFACES_MAX = 16;
|
||||
// An env value shorter than this is too common a word to mean anything ("1", "true", "C").
|
||||
const VALUE_MIN = 6;
|
||||
const CONTROL = /[\u0000-\u0008\u000b-\u001f\u007f-\u009f\u2028\u2029]/g;
|
||||
|
||||
// An absolute path of two segments or more that no name replaced. Not one that follows a word,
|
||||
// a colon, a slash, a dot, a dash, a tilde or a name's closing ">" (URLs, ~/x, <home>/x, a/b/c).
|
||||
const LOOSE_PATH = /(?<![\w:/.~>-])\/[^\s/'"`<>()[\]{},;]+(?:\/[^\s'"`<>()[\]{},;]*)+/g;
|
||||
|
||||
// One pass. last: the records a previous pass returned (an array; others are ignored). env: the
|
||||
// environment of the process sessions launch from, which the modules look up their binary in.
|
||||
// scratch: a directory the modules may use for a scratch HOME. Returns { records, transitions,
|
||||
// refused, gone }: one record per harness the loader gives, a transition for each harness whose
|
||||
// state or digest is not the last record's (or that has no last record), the loader's refusals
|
||||
// as it reports them, and the names of last records whose harness the loader no longer gives.
|
||||
export async function watchHarnesses({ last = [], env = process.env, scratch = null, dataRoot = null, timeoutMs = INSPECT_TIMEOUT_MS, now = () => new Date() } = {}) {
|
||||
const before = new Map();
|
||||
for (const r of Array.isArray(last) ? last : []) if (r && typeof r.harness === "string") before.set(r.harness, r);
|
||||
const loaded = await loadInstalled("harness", { dataRoot });
|
||||
const redact = redactor(env, scratch);
|
||||
// The loader's refusals name the module's directory, a host path, so their text is redacted too.
|
||||
const refused = loaded.refused.map((r) => ({ ...r, message: typeof r.message === "string" ? redact(r.message) : null }));
|
||||
const modules = loaded.modules;
|
||||
const records = [];
|
||||
const transitions = [];
|
||||
for (const { name, module } of modules) {
|
||||
const answer = await ask(module, { env, scratch, timeoutMs });
|
||||
const at = now().toISOString();
|
||||
const prior = before.get(name) ?? null;
|
||||
const record = { harness: name, ...settle(clean(answer, redact, env), prior), checkedAt: at };
|
||||
record.lastDigest = record.digest ?? known(prior);
|
||||
const moved = !prior || prior.state !== record.state || !sameBinary(prior, record);
|
||||
record.changedAt = moved ? at : (typeof prior.changedAt === "string" ? prior.changedAt : at);
|
||||
records.push(record);
|
||||
if (moved) transitions.push({ harness: name, at, from: prior ? brief(prior) : null, to: brief(record), failed: record.failed, line: record.line });
|
||||
}
|
||||
const seen = new Set(records.map((r) => r.harness));
|
||||
return { records, transitions, refused, gone: [...before.keys()].filter((n) => !seen.has(n)).sort() };
|
||||
}
|
||||
|
||||
// The latest digest the last record knew: its own, or the one it carried forward.
|
||||
const known = (prior) => [prior?.digest, prior?.lastDigest].find((d) => typeof d === "string" && DIGEST.test(d)) ?? null;
|
||||
// Whether two records are of the same binary. The digest decides; when neither record has one
|
||||
// (pi gives none for a failed check), the version label stands in for it, but only when both
|
||||
// records have one: a pass that timed out or threw knows no version, which is not a new binary.
|
||||
function sameBinary(prior, record) {
|
||||
const was = typeof prior.digest === "string" ? prior.digest : null;
|
||||
if (was !== null || record.digest !== null) return was === record.digest;
|
||||
const before = typeof prior.version === "string" ? prior.version : null;
|
||||
return before === null || record.version === null || before === record.version;
|
||||
}
|
||||
|
||||
const brief = (r) => ({ state: STATES.includes(r.state) ? r.state : "unknown", version: typeof r.version === "string" ? r.version : null, digest: typeof r.digest === "string" ? r.digest : null });
|
||||
|
||||
// The module's answer, or { error } when it threw or ran past timeoutMs. via says which export.
|
||||
async function ask(module, { env, scratch, timeoutMs }) {
|
||||
const via = typeof module.inspect === "function" ? "inspect" : "version";
|
||||
const stop = new AbortController();
|
||||
let timer = null;
|
||||
const late = new Promise((resolve) => { timer = setTimeout(() => { stop.abort(); resolve({ via, error: "timeout" }); }, timeoutMs); });
|
||||
const asked = (async () => {
|
||||
try {
|
||||
// The answer is copied here, inside the try, to plain own data: a getter, a Proxy or an
|
||||
// inherited field that throws or lies is a failed inspect, not a failed pass.
|
||||
if (via === "inspect") return { via, answer: plain(await module.inspect({ env, scratch, signal: stop.signal })) };
|
||||
const version = await module.version({ env });
|
||||
return { via, version: typeof version === "string" ? version : null };
|
||||
} catch {
|
||||
// The error's text can name a host path, so the record keeps none of it.
|
||||
return { via, error: "threw" };
|
||||
}
|
||||
})();
|
||||
try {
|
||||
return await Promise.race([asked, late]);
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
// The contract's fields of an answer as own data properties, copied to a plain object whose
|
||||
// values are strings, booleans, null, an array of strings or a map of strings. A getter is not
|
||||
// called (a field that is one makes the answer a failed inspect), an inherited field is not read,
|
||||
// and failed and surfaces are cut before they are read.
|
||||
const FIELDS = ["found", "source", "version", "digest", "fit", "definite", "line"];
|
||||
function plain(answer) {
|
||||
if (!answer || typeof answer !== "object") return null;
|
||||
const own = (o, k) => {
|
||||
const d = Object.getOwnPropertyDescriptor(o, k);
|
||||
if (d && !("value" in d)) throw new Error("an accessor field");
|
||||
return d ? d.value : undefined;
|
||||
};
|
||||
const out = {};
|
||||
for (const k of FIELDS) {
|
||||
const v = own(answer, k);
|
||||
if (typeof v === "string" || typeof v === "boolean") out[k] = v;
|
||||
}
|
||||
const failed = own(answer, "failed");
|
||||
if (Array.isArray(failed)) {
|
||||
out.failed = [];
|
||||
for (let i = 0; i < Math.min(failed.length, 32); i++) {
|
||||
const id = own(failed, String(i));
|
||||
if (typeof id === "string") out.failed.push(id);
|
||||
}
|
||||
}
|
||||
const surfaces = own(answer, "surfaces");
|
||||
if (surfaces && typeof surfaces === "object" && !Array.isArray(surfaces)) {
|
||||
out.surfaces = {};
|
||||
for (const k of Object.keys(surfaces).slice(0, SURFACES_MAX)) {
|
||||
const v = own(surfaces, k);
|
||||
if (typeof v === "string") out.surfaces[k] = v.slice(0, SURFACE_MAX);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// The contract's fields from an answer, each checked and redacted. Anything else is dropped. A
|
||||
// source or rule id that is an env value is dropped too: they are not text, so not redacted.
|
||||
function clean({ via, answer, version, error }, redact, env) {
|
||||
const base = { via, found: null, source: null, version: null, digest: null, fit: "unknown", definite: false, failed: [], line: null, surfaces: {} };
|
||||
if (error === "timeout") return { ...base, line: `${via} did not answer in time` };
|
||||
if (error) return { ...base, line: `${via} failed` };
|
||||
if (via === "version") {
|
||||
const label = text(version, LABEL_MAX, redact);
|
||||
return { ...base, version: label, line: label ? "no inspect; fit not checked" : "no inspect, and version gave no answer" };
|
||||
}
|
||||
if (!answer || typeof answer !== "object") return { ...base, line: "inspect gave no answer" };
|
||||
const values = envValues(env);
|
||||
const found = answer.found === true ? true : answer.found === false ? false : null;
|
||||
const fit = FITS.has(answer.fit) ? answer.fit : "unknown";
|
||||
const surfaces = {};
|
||||
if (answer.surfaces && typeof answer.surfaces === "object") {
|
||||
for (const [k, v] of Object.entries(answer.surfaces)) if (SURFACE.test(k)) surfaces[k] = redact(v);
|
||||
}
|
||||
return {
|
||||
...base,
|
||||
found,
|
||||
source: typeof answer.source === "string" && SOURCE.test(answer.source) && !values.has(answer.source) ? answer.source : null,
|
||||
version: text(answer.version, LABEL_MAX, redact),
|
||||
digest: typeof answer.digest === "string" && DIGEST.test(answer.digest) ? answer.digest : null,
|
||||
fit,
|
||||
// A pass is an answer, so it is definite. A failure is definite only when the module says so
|
||||
// (otherwise the check could not decide), and an unknown never is.
|
||||
definite: fit === "pass" || (fit === "fail" && answer.definite === true),
|
||||
failed: Array.isArray(answer.failed) ? [...new Set(answer.failed.filter((id) => RULE.test(id) && !values.has(id)))] : [],
|
||||
line: text(answer.line, LINE_MAX, redact),
|
||||
surfaces,
|
||||
};
|
||||
}
|
||||
|
||||
// The state for a cleaned answer and the last record.
|
||||
function settle(r, prior) {
|
||||
let state = "unknown";
|
||||
if (r.via === "inspect" && r.found === false) state = "missing";
|
||||
else if (r.via === "inspect" && r.found === true && r.fit === "pass") {
|
||||
const was = known(prior);
|
||||
state = was !== null && r.digest !== null && was !== r.digest ? "changed" : "fit";
|
||||
} else if (r.via === "inspect" && r.found === true && r.fit === "fail" && r.definite) state = "red";
|
||||
return { state, ...r };
|
||||
}
|
||||
|
||||
// One printable line, cut to max, redacted; or null.
|
||||
function text(v, max, redact) {
|
||||
if (typeof v !== "string") return null;
|
||||
const line = redact(v.split("\n")[0].replace(CONTROL, " ").trim()).slice(0, max);
|
||||
return line || null;
|
||||
}
|
||||
|
||||
// Every nonempty string value in env and in this process's environment.
|
||||
function envValues(env) {
|
||||
const out = new Set();
|
||||
for (const source of [env ?? {}, process.env]) for (const v of Object.values(source)) if (typeof v === "string" && v) out.add(v);
|
||||
return out;
|
||||
}
|
||||
|
||||
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
|
||||
// Replaces every host path and env value a record could pick up with a name for it: the scratch
|
||||
// directory, the home directory, each env value of VALUE_MIN characters or more from env and this
|
||||
// process's own environment, and each absolute PATH entry, each also as its real path and without
|
||||
// a trailing slash. Longest first, so a path inside another is not left half replaced. The user
|
||||
// name (this process's account, USER and LOGNAME), short as it may be, is replaced as a whole
|
||||
// word. Last, any absolute path left is <path>.
|
||||
function redactor(env, scratch) {
|
||||
const pairs = new Map();
|
||||
const put = (value, name) => { if (value.length >= VALUE_MIN && !pairs.has(value)) pairs.set(value, name); };
|
||||
const add = (value, name) => {
|
||||
if (typeof value !== "string") return;
|
||||
put(value, name);
|
||||
if (!value.startsWith("/")) return;
|
||||
const bare = value.replace(/\/+$/, "");
|
||||
if (bare) put(bare, name);
|
||||
try { put(realpathSync(bare || "/"), name); } catch {}
|
||||
};
|
||||
if (typeof scratch === "string" && scratch) add(scratch, "<scratch>");
|
||||
for (const source of [env ?? {}, process.env]) {
|
||||
for (const [k, v] of Object.entries(source)) {
|
||||
if (typeof v !== "string") continue;
|
||||
add(v, `<${k}>`);
|
||||
if (k === "PATH") for (const dir of v.split(":")) if (isAbsolute(dir) && dir !== "/") add(dir, "<PATH entry>");
|
||||
}
|
||||
}
|
||||
try { add(homedir(), "<home>"); } catch {}
|
||||
const users = new Set();
|
||||
try { users.add(userInfo().username); } catch {}
|
||||
for (const source of [env ?? {}, process.env]) for (const k of ["USER", "LOGNAME"]) if (typeof source[k] === "string") users.add(source[k]);
|
||||
const words = [...users].filter((u) => typeof u === "string" && u.length >= 2 && /^[A-Za-z0-9._-]+$/.test(u));
|
||||
const word = words.length ? new RegExp(`(?<![A-Za-z0-9_])(?:${words.sort((a, b) => b.length - a.length).map(escape).join("|")})(?![A-Za-z0-9_])`, "g") : null;
|
||||
const order = [...pairs.keys()].sort((a, b) => b.length - a.length);
|
||||
return (s) => {
|
||||
let out = s;
|
||||
for (const value of order) if (out.includes(value)) out = out.split(value).join(pairs.get(value));
|
||||
if (word) out = out.replace(word, "<user>");
|
||||
return out.replace(LOOSE_PATH, "<path>");
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,414 @@
|
||||
// #1873 harness watch, slice W1 (reports/harness-watch-design.md sections 1 and 5): one pass over
|
||||
// every harness the loader gives, an optional inspect per module or the version fallback, a
|
||||
// state from the answer and the last record, and transitions only on a state or digest change.
|
||||
// No record holds a host path or an env value.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createReadStream, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, watch, writeFileSync } from "node:fs";
|
||||
import { createHash } from "node:crypto";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { tree } from "./helpers.mjs";
|
||||
import { KINDS } from "../src/kinds.mjs";
|
||||
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
|
||||
import { sandboxProblem } from "../../seat/src/sandbox.mjs";
|
||||
import * as pi from "../harness/pi/index.mjs";
|
||||
import * as claude from "../harness/claude-code/index.mjs";
|
||||
|
||||
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
|
||||
const WATCH_SRC = join(REPO, "packages", "plugins", "src", "harness-watch.mjs");
|
||||
const PI_NAME = "@earendil-works/pi-coding-agent";
|
||||
const NO_BWRAP = sandboxProblem() ? { skip: "no working bwrap here" } : {};
|
||||
const DIGEST_A = "a".repeat(64);
|
||||
const DIGEST_B = "b".repeat(64);
|
||||
|
||||
function scratch(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), "mosaic-watch-"));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
return root;
|
||||
}
|
||||
|
||||
// A stand-in harness module: every export the kind needs, a version() that answers
|
||||
// env.WATCH_VERSION, and (with inspect) an inspect() that answers its own entry of the JSON file
|
||||
// env.WATCH_ANSWER names, read at each call: "throw" throws, "hang" never answers, "hang-signal"
|
||||
// never answers but writes <answer file>.aborted when its signal aborts, and the rest build an
|
||||
// answer JSON cannot carry (a throwing getter, a Proxy, inherited fields, a huge sparse array).
|
||||
const standIn = (name, { inspect = true } = {}) => `import { readFileSync, writeFileSync } from "node:fs";
|
||||
export const interfaceVersion = ${KINDS.harness.interfaceVersion};
|
||||
${KINDS.harness.exports.filter((e) => e !== "version").map((e) => `export function ${e}() { return null; }`).join("\n")}
|
||||
export async function version({ env }) {
|
||||
if (env.WATCH_VERSION === "throw") throw new Error("version failed at " + env.WATCH_SECRET);
|
||||
return env.WATCH_VERSION ?? null;
|
||||
}
|
||||
${inspect ? `export async function inspect({ env, signal }) {
|
||||
const a = JSON.parse(readFileSync(env.WATCH_ANSWER, "utf8"))[${JSON.stringify(name)}];
|
||||
const boom = () => { throw new Error("inspect failed at " + env.WATCH_SECRET); };
|
||||
if (a === "throw") boom();
|
||||
if (a === "hang") return new Promise(() => {});
|
||||
if (a === "hang-signal") return new Promise(() => { signal.addEventListener("abort", () => writeFileSync(env.WATCH_ANSWER + ".aborted", "1")); });
|
||||
if (a === "getter") return { found: true, fit: "pass", definite: true, get digest() { return boom(); } };
|
||||
if (a === "proxy") return new Proxy({}, { get: boom, getOwnPropertyDescriptor: boom, ownKeys: boom });
|
||||
if (a === "surfaces-proxy") return { found: true, fit: "pass", definite: true, digest: "${DIGEST_A}", surfaces: new Proxy({}, { ownKeys: boom }) };
|
||||
if (a === "inherited") return Object.create({ found: true, fit: "pass", definite: true, digest: "${DIGEST_A}" });
|
||||
if (a === "sparse") { const failed = []; failed.length = 2 ** 32 - 1; failed[0] = "C1"; return { found: true, fit: "fail", definite: true, failed, line: "sparse" }; }
|
||||
return a;
|
||||
}` : ""}
|
||||
`;
|
||||
|
||||
// A scratch plugin tree whose harness directory holds only the named stand-ins, and its watch.
|
||||
async function watchTree(t, harnesses) {
|
||||
const tr = tree(t);
|
||||
for (const [name, opts] of Object.entries(harnesses)) tr.write("harness", name, standIn(name, opts));
|
||||
const { watchHarnesses } = await import(pathToFileURL(join(tr.pkg, "src", "harness-watch.mjs")).href);
|
||||
const dir = scratch(t);
|
||||
const file = join(dir, "answer.json");
|
||||
const pass = async (answers, { last = [], env = {}, timeoutMs = 2000 } = {}) => {
|
||||
writeFileSync(file, JSON.stringify(answers));
|
||||
return watchHarnesses({ last, env: { WATCH_ANSWER: file, ...env }, scratch: dir, timeoutMs });
|
||||
};
|
||||
return { pass, dir, tr, file };
|
||||
}
|
||||
|
||||
const ok = (digest, extra = {}) => ({ found: true, source: "PATH", version: "1.0.0", digest, fit: "pass", definite: true, failed: [], line: "passed", surfaces: {}, ...extra });
|
||||
const byName = (out) => Object.fromEntries(out.records.map((r) => [r.harness, r]));
|
||||
|
||||
test("W1 discovery: every harness the loader gives is watched, with no name list in the watch", async (t) => {
|
||||
// Names made for this run, so nothing in the watch could have listed them.
|
||||
const names = [`w${process.pid}-one`, `w${process.pid}-two`, `w${process.pid}-three`].sort();
|
||||
const { pass } = await watchTree(t, { [names[0]]: {}, [names[1]]: { inspect: false }, [names[2]]: {} });
|
||||
const out = await pass({ [names[0]]: ok(DIGEST_A), [names[2]]: ok(DIGEST_B) }, { env: { WATCH_VERSION: "3.1.4" } });
|
||||
assert.deepEqual(out.records.map((r) => r.harness).sort(), names);
|
||||
assert.deepEqual(out.refused, []);
|
||||
const r = byName(out);
|
||||
assert.equal(r[names[0]].via, "inspect");
|
||||
assert.equal(r[names[1]].via, "version");
|
||||
assert.equal(r[names[2]].digest, DIGEST_B);
|
||||
// The watch's source names no harness: no quoted name and no bare word for a shipped one.
|
||||
const src = readFileSync(WATCH_SRC, "utf8").replace(/^\s*\/\/.*$/gm, "");
|
||||
assert.doesNotMatch(src, /["'`](?:pi|claude|claude-code|codex)["'`]/);
|
||||
assert.doesNotMatch(src, /\b(?:claude|codex)\b|\bpi\b/i);
|
||||
});
|
||||
|
||||
test("W1 the version fallback: a harness with no inspect is watched through version() and is unknown", async (t) => {
|
||||
const { pass } = await watchTree(t, { plain: { inspect: false } });
|
||||
let rec = byName(await pass({}, { env: { WATCH_VERSION: "2.0.0" } })).plain;
|
||||
assert.deepEqual([rec.state, rec.via, rec.version, rec.fit, rec.found, rec.digest], ["unknown", "version", "2.0.0", "unknown", null, null]);
|
||||
rec = byName(await pass({}, {})).plain;
|
||||
assert.deepEqual([rec.state, rec.version], ["unknown", null]);
|
||||
rec = byName(await pass({}, { env: { WATCH_VERSION: "throw", WATCH_SECRET: "/srv/hidden-value" } })).plain;
|
||||
assert.deepEqual([rec.state, rec.line], ["unknown", "version failed"]);
|
||||
});
|
||||
|
||||
test("W1 each state from a pair of records: same digest, new digest, fail, undecided, throw, timeout, not found", async (t) => {
|
||||
const { pass } = await watchTree(t, { h: {} });
|
||||
const first = await pass({ h: ok(DIGEST_A) });
|
||||
const prior = first.records;
|
||||
assert.equal(byName(first).h.state, "fit");
|
||||
const cases = [
|
||||
["same digest, fit pass", ok(DIGEST_A), "fit"],
|
||||
["new digest, fit pass", ok(DIGEST_B), "changed"],
|
||||
["fit fail, definite", ok(DIGEST_B, { fit: "fail", definite: true, failed: ["C5"], line: "lacks --no-mcp" }), "red"],
|
||||
["fit fail, not definite", ok(DIGEST_B, { fit: "fail", definite: false }), "unknown"],
|
||||
["fit unknown", ok(DIGEST_B, { fit: "unknown" }), "unknown"],
|
||||
["inspect throws", "throw", "unknown"],
|
||||
["inspect times out", "hang", "unknown"],
|
||||
["binary not found", { found: false, source: null, version: null, digest: null, fit: "unknown", definite: false, failed: [], line: "no binary", surfaces: {} }, "missing"],
|
||||
// Answers that are not the contract's never read as fit.
|
||||
["fit spelled otherwise", ok(DIGEST_A, { fit: "PASS" }), "unknown"],
|
||||
["found not a boolean", ok(DIGEST_A, { found: "yes" }), "unknown"],
|
||||
["no answer", null, "unknown"],
|
||||
["a pass with found false", ok(DIGEST_A, { found: false }), "missing"],
|
||||
];
|
||||
for (const [what, answer, state] of cases) {
|
||||
const out = await pass({ h: answer }, { last: prior, timeoutMs: 300 });
|
||||
assert.equal(byName(out).h.state, state, what);
|
||||
}
|
||||
const red = byName(await pass({ h: ok(DIGEST_B, { fit: "fail", definite: true, failed: ["C5", "bad id with spaces"], line: "lacks --no-mcp" }) }, { last: prior })).h;
|
||||
assert.deepEqual([red.failed, red.line, red.definite], [["C5"], "lacks --no-mcp", true]);
|
||||
const late = byName(await pass({ h: "hang" }, { last: prior, timeoutMs: 300 })).h;
|
||||
assert.equal(late.line, "inspect did not answer in time");
|
||||
});
|
||||
|
||||
test("W1 a transition is reported only when the state or the digest changes", async (t) => {
|
||||
const { pass } = await watchTree(t, { h: {}, k: {} });
|
||||
const one = await pass({ h: ok(DIGEST_A), k: ok(DIGEST_A) });
|
||||
assert.deepEqual(one.transitions.map((x) => [x.harness, x.from, x.to.state]), [["h", null, "fit"], ["k", null, "fit"]]);
|
||||
// Same answers: no transition, and changedAt stays the first pass's.
|
||||
const two = await pass({ h: ok(DIGEST_A), k: ok(DIGEST_A, { version: "1.0.1", line: "another line" }) }, { last: one.records });
|
||||
assert.deepEqual(two.transitions, []);
|
||||
assert.equal(byName(two).h.changedAt, byName(one).h.changedAt);
|
||||
// A state change is a transition, with or without a new digest.
|
||||
const three = await pass({ h: ok(DIGEST_B), k: ok(DIGEST_A, { fit: "fail", definite: true }) }, { last: two.records });
|
||||
assert.deepEqual(three.transitions.map((x) => [x.harness, x.from.state, x.to.state, x.from.digest, x.to.digest]),
|
||||
[["h", "fit", "changed", DIGEST_A, DIGEST_B], ["k", "fit", "red", DIGEST_A, DIGEST_A]]);
|
||||
const four = await pass({ h: ok(DIGEST_B, { fit: "unknown" }), k: ok(DIGEST_A, { fit: "fail", definite: true }) }, { last: three.records });
|
||||
// A new digest with the state unchanged (unknown to unknown) is a transition too.
|
||||
const five = await pass({ h: ok(DIGEST_A, { fit: "unknown" }), k: ok(DIGEST_A, { fit: "fail", definite: true }) }, { last: four.records });
|
||||
assert.deepEqual(five.transitions.map((x) => [x.harness, x.from.state, x.to.state]), [["h", "unknown", "unknown"]]);
|
||||
// A harness the loader no longer gives is named in gone, and has no record.
|
||||
const six = await pass({ h: ok(DIGEST_A) }, { last: [...five.records, { harness: "removed", state: "fit", digest: DIGEST_A }] });
|
||||
assert.deepEqual(six.gone, ["removed"]);
|
||||
});
|
||||
|
||||
test("W1 a new binary first seen through an unknown, red or missing record still reads changed", async (t) => {
|
||||
const { pass } = await watchTree(t, { h: {} });
|
||||
const none = { found: true, source: "PATH", version: "2.0.0", digest: null, fit: "unknown", definite: false, failed: [], line: "timed out", surfaces: {} };
|
||||
for (const [what, between] of [["unknown", none], ["red", { ...none, fit: "fail", definite: true }], ["missing", { ...none, found: false }], ["timeout", "hang"], ["throw", "throw"]]) {
|
||||
const one = await pass({ h: ok(DIGEST_A) });
|
||||
const two = await pass({ h: between }, { last: one.records, timeoutMs: 200 });
|
||||
assert.equal(byName(two).h.lastDigest, DIGEST_A, what);
|
||||
const three = await pass({ h: ok(DIGEST_B) }, { last: two.records });
|
||||
assert.equal(byName(three).h.state, "changed", what);
|
||||
const four = await pass({ h: ok(DIGEST_B) }, { last: three.records });
|
||||
assert.equal(byName(four).h.state, "fit", what);
|
||||
}
|
||||
// With no digest on either record, a new version label is a new binary: red 2.0 to red 2.1 is a
|
||||
// transition, red 2.1 again is not.
|
||||
const red = (version, line) => ({ ...none, version, line, fit: "fail", definite: true });
|
||||
const a = await pass({ h: red("2.0.0", "lacks --no-skills") });
|
||||
const b = await pass({ h: red("2.1.0", "lacks --no-mcp") }, { last: a.records });
|
||||
assert.deepEqual(b.transitions.map((x) => [x.from.version, x.to.version, x.line]), [["2.0.0", "2.1.0", "lacks --no-mcp"]]);
|
||||
const c = await pass({ h: red("2.1.0", "lacks --no-mcp") }, { last: b.records });
|
||||
assert.deepEqual(c.transitions, []);
|
||||
// A pass that knows no version (a timeout or a throw) is not a new binary: unknown with 2.0,
|
||||
// then a timeout, then unknown with 2.0 again, then a throw gives no transition after the first.
|
||||
const d = await pass({ h: none }, { last: [] });
|
||||
const e = await pass({ h: "hang" }, { last: d.records, timeoutMs: 200 });
|
||||
const f = await pass({ h: { ...none, version: "2.0.0" } }, { last: e.records });
|
||||
const g = await pass({ h: "throw" }, { last: f.records });
|
||||
assert.deepEqual([d, e, f, g].map((x) => x.transitions.length), [1, 0, 0, 0]);
|
||||
assert.deepEqual([e, g].map((x) => byName(x).h.version), [null, null]);
|
||||
});
|
||||
|
||||
test("W1 an answer that throws when read, a Proxy or inherited fields is unknown, and the other harnesses still get records", async (t) => {
|
||||
const { pass, file } = await watchTree(t, { bad: {}, good: {} });
|
||||
for (const shape of ["getter", "proxy", "surfaces-proxy"]) {
|
||||
const out = await pass({ bad: shape, good: ok(DIGEST_A) }, { env: { WATCH_SECRET: "/srv/fixture-secret-place" } });
|
||||
const r = byName(out);
|
||||
assert.deepEqual([r.bad.state, r.bad.line, r.good.state], ["unknown", "inspect failed", "fit"], shape);
|
||||
assert.ok(!JSON.stringify(out).includes("fixture-secret"), shape);
|
||||
}
|
||||
// An inherited fit is not the module's answer.
|
||||
assert.equal(byName(await pass({ bad: "inherited", good: ok(DIGEST_A) })).bad.state, "unknown");
|
||||
// A huge sparse failed list is cut before it is read, so the pass ends at once.
|
||||
const began = Date.now();
|
||||
const sparse = byName(await pass({ bad: "sparse", good: ok(DIGEST_A) })).bad;
|
||||
assert.deepEqual([sparse.state, sparse.failed], ["red", ["C1"]]);
|
||||
assert.ok(Date.now() - began < 2000);
|
||||
// On a timeout the watch aborts the signal it passed.
|
||||
const late = byName(await pass({ bad: "hang-signal", good: ok(DIGEST_A) }, { timeoutMs: 200 })).bad;
|
||||
await new Promise((done) => setTimeout(done, 50));
|
||||
assert.equal(late.state, "unknown");
|
||||
assert.equal(readFileSync(file + ".aborted", "utf8"), "1");
|
||||
});
|
||||
|
||||
test("W1 fields are checked: a digest that is a path is dropped, version and line keep one printable line", async (t) => {
|
||||
const { pass, dir } = await watchTree(t, { h: {} });
|
||||
const r = byName(await pass({ h: ok(dir, { version: "1.0\u0007beta\nsecond line", line: "first\r\nsecond", surfaces: { help: "a\nb" } }) })).h;
|
||||
assert.deepEqual([r.digest, r.version, r.line, r.surfaces.help], [null, "1.0 beta", "first", "a\nb"]);
|
||||
});
|
||||
|
||||
test("W1 redaction covers real paths, trailing slashes, the user name, rule ids, sources, loose paths and the loader's refusals", async (t) => {
|
||||
const { pass, dir, tr } = await watchTree(t, { h: {} });
|
||||
mkdirSync(join(dir, "real", "home"), { recursive: true });
|
||||
mkdirSync(join(dir, "real", "bin"), { recursive: true });
|
||||
symlinkSync(join(dir, "real", "home"), join(dir, "linkhome"));
|
||||
symlinkSync(join(dir, "real", "bin"), join(dir, "linkbin"));
|
||||
const realHome = realpathSync(join(dir, "linkhome"));
|
||||
const realBin = realpathSync(join(dir, "linkbin"));
|
||||
// A symlinked scratch: the answer names its real path.
|
||||
const linkScratch = join(dir, "linkscratch");
|
||||
symlinkSync(dir, linkScratch);
|
||||
const env = { HOME: join(dir, "linkhome") + "/", PATH: `${join(dir, "linkbin")}:/usr/bin:/bin`, USER: "jq", WATCH_TOKEN: "sk-AbCdEf123456", WATCH_NAME: "fixturesource" };
|
||||
// Each rule on its own line, with what the line must read after. scratch: the one passed.
|
||||
const lines = {
|
||||
scratch: [`in ${dir}/x`, "in <scratch>/x", linkScratch],
|
||||
home: [`home ${realHome}`, "home <HOME>", dir],
|
||||
slash: [`home ${join(dir, "linkhome")}`, "home <HOME>", dir],
|
||||
bin: [`bin ${realBin}/claude`, "bin <PATH entry>/claude", dir],
|
||||
user: ["built by jq on host", "built by <user> on host", dir],
|
||||
loose: ["at /opt/notinenv/bin/tool and /etc/passwd", "at <path> and <path>", dir],
|
||||
kept: ["see https://example.com/a/b, ~/x/y and /model", "see https://example.com/a/b, ~/x/y and /model", dir],
|
||||
};
|
||||
const { watchHarnesses } = await import(pathToFileURL(join(tr.pkg, "src", "harness-watch.mjs")).href);
|
||||
for (const [what, [line, want, at]] of Object.entries(lines)) {
|
||||
const file = join(dir, `answer-${what}.json`);
|
||||
writeFileSync(file, JSON.stringify({ h: ok(DIGEST_A, { line }) }));
|
||||
const out = await watchHarnesses({ env: { ...env, WATCH_ANSWER: file }, scratch: at });
|
||||
assert.equal(byName(out).h.line, want, what);
|
||||
}
|
||||
const rec = byName(await pass({ h: ok(DIGEST_A, { source: "fixturesource", failed: ["sk-AbCdEf123456", "C5"], line: `in ${realpathSync(dir)}` }) }, { env })).h;
|
||||
assert.deepEqual([rec.source, rec.failed], [null, ["C5"]]);
|
||||
assert.equal(rec.line, "in <scratch>");
|
||||
// The loader's refusal of a harness directory with no index.mjs names its path; the watch's copy doesn't.
|
||||
mkdirSync(join(tr.pkg, "harness", "noindex"));
|
||||
const out = await pass({ h: ok(DIGEST_A) });
|
||||
const refusal = out.refused.find((r) => r.name === "noindex");
|
||||
assert.ok(refusal && typeof refusal.message === "string", JSON.stringify(out.refused));
|
||||
assert.ok(!refusal.message.includes(tr.pkg), refusal.message);
|
||||
});
|
||||
|
||||
test("W1 no record holds a path under the scratch directory, the home directory or an env value", async (t) => {
|
||||
const { pass, dir } = await watchTree(t, { h: {} });
|
||||
const secret = "/srv/fixture-secret-place";
|
||||
const env = { WATCH_SECRET: secret, HOME: join(dir, "home"), MOSAIC_FIXTURE_BIN: "/opt/fixture/bin/tool", PATH: "/opt/fixture-tools/bin:/usr/bin:/bin" };
|
||||
const answer = ok(DIGEST_A, {
|
||||
source: dir, version: `9.9 at ${dir}/bin`,
|
||||
line: `found at ${env.MOSAIC_FIXTURE_BIN} in ${secret}, home ${env.HOME}, ${realpathSync(dir)}`,
|
||||
surfaces: { help: `usage: ${dir}/tool\nsee /opt/fixture-tools/bin/tool and ${tmpdir()}`, "Bad Name": dir },
|
||||
});
|
||||
const out = await pass({ h: answer }, { env });
|
||||
const text = JSON.stringify(out);
|
||||
for (const p of [dir, realpathSync(dir), secret, env.MOSAIC_FIXTURE_BIN, "/opt/fixture-tools/bin", env.HOME, tmpdir()]) assert.ok(!text.includes(p), p);
|
||||
const rec = byName(out).h;
|
||||
assert.equal(rec.source, null, "a source that is a path is dropped");
|
||||
assert.deepEqual(Object.keys(rec.surfaces), ["help"]);
|
||||
assert.match(rec.line, /<MOSAIC_FIXTURE_BIN>/);
|
||||
assert.match(rec.surfaces.help, /<scratch>\/tool/);
|
||||
});
|
||||
|
||||
test("W1 real pi from node_modules gives a version, a digest and fit pass", NO_BWRAP, async (t) => {
|
||||
const dir = scratch(t);
|
||||
const env = { PATH: "/usr/bin:/bin" };
|
||||
const r = await pi.inspect({ env, scratch: dir });
|
||||
const pkg = JSON.parse(readFileSync(join(REPO, "node_modules", PI_NAME, "package.json"), "utf8"));
|
||||
assert.equal(r.fit, "pass", r.line);
|
||||
assert.deepEqual([r.found, r.source, r.version, r.definite], [true, "repo", pkg.version, true]);
|
||||
assert.match(r.digest, /^[a-f0-9]{64}$/);
|
||||
const { watchHarnesses } = await import(pathToFileURL(WATCH_SRC).href);
|
||||
const out = await watchHarnesses({ env, scratch: dir });
|
||||
const rec = out.records.find((x) => x.via === "inspect" && x.source === "repo");
|
||||
assert.deepEqual([rec.state, rec.digest, rec.version], ["fit", r.digest, pkg.version]);
|
||||
const text = JSON.stringify(out);
|
||||
for (const p of [dir, REPO, realpathSync(join(REPO, "node_modules"))]) assert.ok(!text.includes(p), p);
|
||||
});
|
||||
|
||||
// A stand-in pi package named by MOSAIC_PI_BIN, as pi-fit.test.mjs makes one.
|
||||
function piPackage(t, knobs) {
|
||||
const root = scratch(t);
|
||||
const pkg = join(root, "prefix", "lib", "node_modules", PI_NAME);
|
||||
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: PI_NAME, version: "2.0.0", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn({ version: "2.0.0", ...knobs }), { mode: 0o755 });
|
||||
mkdirSync(join(root, "prefix", "bin"));
|
||||
symlinkSync(`../lib/node_modules/${PI_NAME}/dist/bundle/cli.js`, join(root, "prefix", "bin", "pi"));
|
||||
return { root, env: { MOSAIC_PI_BIN: join(root, "prefix", "bin", "pi"), PATH: "/usr/bin:/bin" } };
|
||||
}
|
||||
|
||||
test("W1 pi inspect shares preflight's fit check: a binary that passed is not checked again", NO_BWRAP, async (t) => {
|
||||
const s = piPackage(t, {});
|
||||
// A fit check makes its scratch directories in TMPDIR. Control: preflight's check is seen making
|
||||
// them. Then inspect, on the binary that passed, must make none.
|
||||
const made = [];
|
||||
const watcher = watch(tmpdir(), (_, name) => { if (String(name).startsWith("mosaic-pi-fit-")) made.push(name); });
|
||||
t.after(() => watcher.close());
|
||||
const settle = () => new Promise((done) => setTimeout(done, 200));
|
||||
assert.equal(await pi.preflight({ env: { ...s.env, ZAI_API_KEY: "fixture-zai" } }), null);
|
||||
await settle();
|
||||
assert.ok(made.length > 0, "the watcher sees a fit check run");
|
||||
made.length = 0;
|
||||
const r = await pi.inspect({ env: s.env });
|
||||
await settle();
|
||||
assert.deepEqual(made, []);
|
||||
assert.deepEqual([r.fit, r.source, r.version], ["pass", "MOSAIC_PI_BIN", "2.0.0"]);
|
||||
assert.ok(!JSON.stringify(r).includes(s.root));
|
||||
});
|
||||
|
||||
test("W1 pi inspect: a definite failure is fail, one that can't be checked is unknown, no binary is not found", NO_BWRAP, async (t) => {
|
||||
const lacking = piPackage(t, { dropFlags: ["--no-skills"] });
|
||||
let r = await pi.inspect({ env: lacking.env });
|
||||
assert.deepEqual([r.found, r.fit, r.definite, r.digest, r.version, r.line], [true, "fail", true, null, "2.0.0", "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-skills"]);
|
||||
const flood = piPackage(t, { flood: true });
|
||||
r = await pi.inspect({ env: flood.env });
|
||||
assert.deepEqual([r.fit, r.definite], ["unknown", false], r.line);
|
||||
assert.match(r.line, /can't be checked/);
|
||||
r = await pi.inspect({ env: { MOSAIC_PI_BIN: "relative/pi", PATH: "/usr/bin:/bin" } });
|
||||
assert.deepEqual([r.found, r.source, r.line], [false, "MOSAIC_PI_BIN", "MOSAIC_PI_BIN isn't an executable file at an absolute path"]);
|
||||
});
|
||||
|
||||
const sha256 = (file) => new Promise((done, fail) => {
|
||||
const h = createHash("sha256");
|
||||
createReadStream(file).on("error", fail).on("data", (c) => h.update(c)).on("end", () => done(h.digest("hex")));
|
||||
});
|
||||
|
||||
const CLAUDE = process.env.MOSAIC_CLAUDE_BIN;
|
||||
test("W1 the real claude binary named by MOSAIC_CLAUDE_BIN gives a version, a sha256 and its help", CLAUDE ? {} : { skip: "MOSAIC_CLAUDE_BIN is not set" }, async (t) => {
|
||||
const dir = scratch(t);
|
||||
const env = { MOSAIC_CLAUDE_BIN: CLAUDE, PATH: "/usr/bin:/bin" };
|
||||
const r = await claude.inspect({ env, scratch: dir });
|
||||
assert.deepEqual([r.found, r.source, r.fit, r.definite], [true, "MOSAIC_CLAUDE_BIN", "unknown", false]);
|
||||
assert.match(r.version, /\d+\.\d+\.\d+/);
|
||||
assert.equal(r.digest, await sha256(realpathSync(CLAUDE)));
|
||||
assert.match(r.surfaces.help, /--version/);
|
||||
const { watchHarnesses } = await import(pathToFileURL(WATCH_SRC).href);
|
||||
const out = await watchHarnesses({ env, scratch: dir });
|
||||
const rec = out.records.find((x) => x.source === "MOSAIC_CLAUDE_BIN");
|
||||
assert.deepEqual([rec.state, rec.digest, rec.version], ["unknown", r.digest, r.version]);
|
||||
assert.ok(!JSON.stringify(out).includes(CLAUDE));
|
||||
// The scratch HOME it ran in is gone.
|
||||
assert.deepEqual(readdirNames(dir), []);
|
||||
});
|
||||
|
||||
test("W1 claude-code inspect: no binary is not found, naming the variable; no scratch runs nothing", async (t) => {
|
||||
let r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: "relative/claude" }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.found, r.source, r.line], [false, "MOSAIC_CLAUDE_BIN", "MOSAIC_CLAUDE_BIN isn't an executable file at an absolute path"]);
|
||||
r = await claude.inspect({ env: { PATH: scratch(t) }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.found, r.source], [false, null]);
|
||||
// A relative PATH entry is skipped, as pi's lookup skips it, though "bin" from this cwd holds a claude.
|
||||
const rel = scratch(t);
|
||||
mkdirSync(join(rel, "bin"));
|
||||
writeFileSync(join(rel, "bin", "claude"), "#!/bin/sh\necho 1.0.0\n", { mode: 0o755 });
|
||||
const cwd = process.cwd();
|
||||
process.chdir(rel);
|
||||
try {
|
||||
r = await claude.inspect({ env: { PATH: "bin" }, scratch: scratch(t) });
|
||||
} finally {
|
||||
process.chdir(cwd);
|
||||
}
|
||||
assert.deepEqual([r.found, r.source], [false, null]);
|
||||
const dir = scratch(t);
|
||||
mkdirSync(join(dir, "bin"));
|
||||
writeFileSync(join(dir, "bin", "claude"), "#!/bin/sh\necho ran >> \"$0.log\"\necho 1.2.3\n", { mode: 0o755 });
|
||||
r = await claude.inspect({ env: { PATH: join(dir, "bin") } });
|
||||
assert.deepEqual([r.found, r.source, r.version, r.surfaces], [true, "PATH", null, {}]);
|
||||
assert.equal(r.digest, await sha256(join(dir, "bin", "claude")));
|
||||
assert.deepEqual(readdirNames(join(dir, "bin")), ["claude"], "nothing ran without scratch");
|
||||
});
|
||||
|
||||
test("W1 claude-code inspect runs --version and --help with PATH and a scratch HOME only, then removes it", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const work = scratch(t);
|
||||
mkdirSync(join(dir, "bin"));
|
||||
const bin = join(dir, "bin", "claude");
|
||||
writeFileSync(bin, "#!/bin/sh\nenv > \"$0.env$1\"\n[ \"$1\" = --version ] && echo '1.2.3 (stand-in)'\n[ \"$1\" = --help ] && echo 'usage: claude --version'\nexit 0\n", { mode: 0o755 });
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin, PATH: "/usr/bin:/bin", FIXTURE_TOKEN: "fixture-token-value" }, scratch: work });
|
||||
assert.deepEqual([r.found, r.source, r.version, r.surfaces.help], [true, "MOSAIC_CLAUDE_BIN", "1.2.3 (stand-in)", "usage: claude --version\n"]);
|
||||
for (const arg of ["--version", "--help"]) {
|
||||
const vars = Object.fromEntries(readFileSync(`${bin}.env${arg}`, "utf8").trim().split("\n").map((l) => [l.slice(0, l.indexOf("=")), l.slice(l.indexOf("=") + 1)]));
|
||||
assert.deepEqual(Object.keys(vars).filter((k) => !["PWD", "SHLVL", "_", "OLDPWD"].includes(k)).sort(), ["HOME", "PATH"], arg);
|
||||
assert.equal(vars.PATH, "/usr/bin:/bin");
|
||||
assert.ok(vars.HOME.startsWith(join(realpathSync(work), "claude-home-")), arg);
|
||||
}
|
||||
assert.deepEqual(readdirNames(work), [], "the scratch HOME is removed");
|
||||
});
|
||||
|
||||
test("W1 claude-code inspect: an aborted signal kills the capture it is running", async (t) => {
|
||||
const dir = scratch(t);
|
||||
mkdirSync(join(dir, "bin"));
|
||||
const bin = join(dir, "bin", "claude");
|
||||
writeFileSync(bin, "#!/bin/sh\necho $$ > \"$0.pid$1\"\nexec sleep 30\n", { mode: 0o755 });
|
||||
const stop = new AbortController();
|
||||
setTimeout(() => stop.abort(), 300);
|
||||
const began = Date.now();
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t), signal: stop.signal });
|
||||
assert.ok(Date.now() - began < 5000);
|
||||
assert.deepEqual([r.found, r.version, r.surfaces], [true, null, {}]);
|
||||
const pid = Number(readFileSync(`${bin}.pid--version`, "utf8"));
|
||||
await new Promise((done) => setTimeout(done, 200));
|
||||
assert.throws(() => process.kill(pid, 0), { code: "ESRCH" });
|
||||
});
|
||||
|
||||
function readdirNames(dir) {
|
||||
return readdirSync(dir).sort();
|
||||
}
|
||||
@@ -603,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>'
|
||||
@@ -687,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*\([^)]*\)$/, '');
|
||||
@@ -801,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();
|
||||
@@ -847,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() {
|
||||
@@ -1669,8 +1690,8 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
// 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 false; loading = true; let painted = false;
|
||||
try { state = await admin('status'); 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'); }
|
||||
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;
|
||||
}
|
||||
@@ -1786,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
|
||||
|
||||
@@ -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: [] },
|
||||
|
||||
@@ -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'] };
|
||||
|
||||
Reference in new issue
Block a user