Compare commits
57
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0f07301607 | ||
|
|
a4d06b95c6 | ||
|
|
f2463ab36d | ||
|
|
7afe892e94 | ||
|
|
4a3f501e13 | ||
|
|
c065034a6e | ||
|
|
83250ef36b | ||
|
|
b432f9cd6f | ||
|
|
e0065b6a44 | ||
|
|
3f39ed3f11 | ||
|
|
28a5d38349 | ||
|
|
824b1213c1 | ||
|
|
ca7388743b | ||
|
|
59af86a5ee | ||
|
|
d6d3271808 | ||
|
|
4a7815d29e | ||
|
|
af8c2f9777 | ||
|
|
b156727c00 | ||
|
|
29daa4c4bf | ||
|
|
c8db3665cf | ||
|
|
f2c262cf40 | ||
|
|
ebfbdb13a0 | ||
|
|
4d378fc38b | ||
|
|
ab8633ede8 | ||
|
|
6ad11e6993 | ||
|
|
e52a6b714f | ||
|
|
2d065b936e | ||
|
|
098b3771c0 | ||
|
|
eac42663ba | ||
|
|
b41bc00b26 | ||
|
|
749d6d98f5 | ||
|
|
fa5ce3ae65 | ||
|
|
d009ccaec4 | ||
|
|
2613fb1c4c | ||
|
|
df26085303 | ||
|
|
7c1b87d154 | ||
|
|
8908df113e | ||
|
|
b8354e4304 | ||
|
|
f62b1b9518 | ||
|
|
7355b233a8 | ||
|
|
88d08c8437 | ||
|
|
4377941bd4 | ||
|
|
de39750ebb | ||
|
|
3b17326b74 | ||
|
|
d03ae494e8 | ||
|
|
e1b56d9ea7 | ||
|
|
e6f6f5445b | ||
|
|
55f9652f6a | ||
|
|
9e434c0e9c | ||
|
|
e732ae7450 | ||
|
|
66255c9bf2 | ||
|
|
cecc901854 | ||
|
|
81ca8399a2 | ||
|
|
37c3854849 | ||
|
|
db2dc96aa2 | ||
|
|
f666590d54 | ||
|
|
2f20346719 |
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
|
||||
|
||||
+21
-8
@@ -1,6 +1,6 @@
|
||||
#!/bin/sh
|
||||
# Pi adapter: implements the Mosaic adapter contract for the pinned
|
||||
# @earendil-works/pi-coding-agent CLI.
|
||||
# Pi adapter: implements the Mosaic adapter contract for the
|
||||
# @earendil-works/pi-coding-agent CLI. Its behaviour notes are read from 0.85.1.
|
||||
#
|
||||
# Contract: see /opt/mosaic/adapters/README.md.
|
||||
# Headless (default): stdout = response only; stderr = diagnostics; exit 0.
|
||||
@@ -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; }
|
||||
@@ -92,6 +96,11 @@ fi
|
||||
THINKING_FLAG=""
|
||||
[ -n "${MOSAIC_THINKING:-}" ] && THINKING_FLAG="--thinking $MOSAIC_THINKING"
|
||||
|
||||
# MCP (#1873 S3): the harness module sets MOSAIC_PI_NO_MCP=1 when the binary's --help lists
|
||||
# --no-mcp, so MCP stays off by its own flag and not only as a side effect of --no-extensions.
|
||||
NO_MCP_FLAG=""
|
||||
[ "${MOSAIC_PI_NO_MCP:-}" = "1" ] && NO_MCP_FLAG="--no-mcp"
|
||||
|
||||
# Mode (M13): interactive TUI or one-shot print.
|
||||
PRINT_MODE="-p"
|
||||
REQUEST_ARG=""
|
||||
@@ -107,6 +116,8 @@ 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)
|
||||
# TOOLS_FLAG per capabilities
|
||||
@@ -118,6 +129,7 @@ set -- \
|
||||
--offline \
|
||||
--no-approve \
|
||||
--no-extensions \
|
||||
$NO_MCP_FLAG \
|
||||
$EXT_FLAGS \
|
||||
$SKILLS_FLAG \
|
||||
--no-prompt-templates \
|
||||
@@ -134,6 +146,7 @@ set -- \
|
||||
[ "$INTERACTIVE" = "1" ] || set -- "$@" -p "$MOSAIC_REQUEST"
|
||||
if [ -n "${MOSAIC_MODEL_EVENT_MONITOR:-}" ] && [ "$INTERACTIVE" != "1" ]; then
|
||||
STREAM="$(dirname "$0")/../../packages/plugins/harness/pi/model-stream.mjs"
|
||||
exec node "$STREAM" pi "$MOSAIC_FIRST_OUTPUT_DIR" pi "$@" --mode json
|
||||
exec node "$STREAM" pi "$MOSAIC_FIRST_OUTPUT_DIR" "${MOSAIC_PI_BIN:-pi}" "$@" --mode json
|
||||
fi
|
||||
exec pi "$@"
|
||||
# #1873 S2: the binary the harness module found (MOSAIC_PI_BIN, its path inside the sandbox).
|
||||
exec "${MOSAIC_PI_BIN:-pi}" "$@"
|
||||
@@ -708,6 +708,7 @@ export class Broker {
|
||||
#credentialVerbs;
|
||||
#proxyInit;
|
||||
#proxyFenceClear;
|
||||
#proxyTriageKey;
|
||||
#proxyPage;
|
||||
#project;
|
||||
#library;
|
||||
@@ -772,7 +773,7 @@ export class Broker {
|
||||
#processing;
|
||||
#taskTracker;
|
||||
// credentialStatus is the runtime's metadata-only Credentials.status(); the broker never gets use().
|
||||
constructor({ store, businesses, pluginService = null, secretCheck = () => {}, credentialStatus = () => [], organization = null, hires = null, autonomy = null, project = null, library = null, usage = null, limits = null, harnessOn = null, credentialVerbs = null, proxyInit = null, proxyFenceClear = null, proxyPage = null, hostGrant = null, hostKeys = null, processing = null, testerOn = null, grantScope = null, roleFile = null, giteaGrant = null, copyGrant = null, copyLaunch = null, copyRevoker = null, giteaKeys = null, access = null, accessStamp = null, compactions = null, requestLifecycle = null, requirementsHost = null, workspaceRead = null, taskTracker = null }) {
|
||||
constructor({ store, businesses, pluginService = null, secretCheck = () => {}, credentialStatus = () => [], organization = null, hires = null, autonomy = null, project = null, library = null, usage = null, limits = null, harnessOn = null, credentialVerbs = null, proxyInit = null, proxyFenceClear = null, proxyTriageKey = null, proxyPage = null, hostGrant = null, hostKeys = null, processing = null, testerOn = null, grantScope = null, roleFile = null, giteaGrant = null, copyGrant = null, copyLaunch = null, copyRevoker = null, giteaKeys = null, access = null, accessStamp = null, compactions = null, requestLifecycle = null, requirementsHost = null, workspaceRead = null, taskTracker = null }) {
|
||||
if (!object(businesses) || !Object.keys(businesses).length) fail('invalid-business');
|
||||
// Row 194 b15 (sage-2501): the function-argument checks come before anything reads the store.
|
||||
if (harnessOn !== null && typeof harnessOn !== 'function') throw new TypeError('harnessOn must be a function');
|
||||
@@ -802,6 +803,7 @@ export class Broker {
|
||||
this.#credentialVerbs = credentialVerbs;
|
||||
this.#proxyInit = proxyInit;
|
||||
this.#proxyFenceClear = proxyFenceClear;
|
||||
this.#proxyTriageKey = proxyTriageKey;
|
||||
this.#proxyPage = proxyPage;
|
||||
this.#hostGrant = hostGrant;
|
||||
this.#hostKeys = hostKeys;
|
||||
@@ -6220,6 +6222,27 @@ export class Broker {
|
||||
return result;
|
||||
}).catch(error => { throw this.#refused(s, new BusError(/^proxy-[a-z-]+$/.test(error.code ?? '') ? error.code : 'proxy-down'), { verb: request.verb }); });
|
||||
}
|
||||
// proxy-scope slice 4 (Jason 2026-11-19): ensure or revoke the instance's Triage key through the
|
||||
// proxy this bus rides. Owner only, before every other gate, like proxy.fence.clear. The
|
||||
// answer, the event and the refusal carry key ids and codes only, never a key.
|
||||
if (request.verb === 'proxy.triage.key') {
|
||||
this.#human(s);
|
||||
if (!this.#proxyTriageKey) fail('not-configured');
|
||||
const args = request.args ?? {};
|
||||
keys(args, ['op'], ['op']);
|
||||
if (args.op !== 'ensure' && args.op !== 'revoke') fail('invalid-request');
|
||||
const ID = /^[0-9a-f]{12}$/;
|
||||
const idList = v => Array.isArray(v) && v.every(id => typeof id === 'string' && ID.test(id));
|
||||
return Promise.resolve().then(() => this.#proxyTriageKey(args.op)).then(result => {
|
||||
const ACTIONS = args.op === 'ensure' ? ['issued', 'held', 'replaced'] : ['revoked', 'none'];
|
||||
if (!object(result) || !ACTIONS.includes(result.action) || !idList(result.addedKeyIds) || !idList(result.removedKeyIds)
|
||||
|| (args.op === 'ensure' && !(typeof result.keyId === 'string' && ID.test(result.keyId)))) fail('proxy-client-response');
|
||||
const answer = { op: args.op, action: result.action, addedKeyIds: [...result.addedKeyIds], removedKeyIds: [...result.removedKeyIds],
|
||||
...(args.op === 'ensure' ? { keyId: result.keyId, rotateBy: /^\d{4}-\d{2}-\d{2}$/.test(result.rotateBy ?? '') ? result.rotateBy : null } : {}) };
|
||||
this.#store.transaction(() => this.#event(s, 'action.allowed', { call: 'proxy.triage.key', ...answer }, null));
|
||||
return answer;
|
||||
}).catch(error => { throw this.#refused(s, error instanceof BusError ? error : new BusError('storage-unavailable', 'triage-key-failed'), { verb: request.verb }); });
|
||||
}
|
||||
// FED R3 (design 3.5, 3.7 trigger 1): a user reaches the collaborator list only, and each
|
||||
// request reads the access file again, so a removed or expired grant refuses the next one.
|
||||
let grants = null;
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -87,6 +87,10 @@ group("proxy-key-mismatch", "R1: a pasted management key is confirmed before ini
|
||||
group("proxy-lock-busy proxy-lock-invalid", "R4: one proxy key write at a time", "Wait for the running proxy write to finish and retry. An invalid lock file needs the owner to inspect the proxy state directory.");
|
||||
group("proxy-not-configured proxy-unit-usage proxy-unit-exists proxy-unit-dropin proxy-unit-copy proxy-unit-path proxy-unit-missing proxy-unit-unhealthy proxy-unit-failed", "R1/R4: proxy user unit is explicit creation", "Initialize the proxy first, then install the unit from the Proxy page or with mosaic proxy unit install. An existing unit or drop-in is never replaced; remove it by hand. An unhealthy unit: read its journal by unit name and restart it.");
|
||||
group("proxy-client-invalid proxy-client-refused proxy-client-response", "R1: proxy management answers", "Check the proxy unit is running and its port answers on the Proxy page, then retry. Upstream bodies are never shown.");
|
||||
// proxy-scope slice 4 (Jason 2026-11-19): the Triage key lives with a bus that rides the owner's
|
||||
// proxy, and a Triage session on a key past its retireAt ends before the key leaves the proxy.
|
||||
group("triage-no-riding-bus", "R1/R4: the Triage key is issued and rotated by a bus that rides the owner's proxy", "Triage needs one organization that uses the owner's models. Start an organization that rides the owner's model proxy, then repair Triage access from Settings, Triage.");
|
||||
group("triage-key-rotated", "R1/R6: a session ends when its Triage key's grace ends", "The Triage key was rotated and the old key's grace has ended. Send the next message; Triage starts again on the current key.");
|
||||
group(
|
||||
"block-recipient block-reply-required block-sender message-recipient-refused message-sender-refused tester-reply-required tester-thread-human user-reply-required user-thread-human",
|
||||
"Role messaging: permitted sender, recipient and reply context",
|
||||
|
||||
@@ -34,6 +34,7 @@ import { createModelCatalogue } from './model-catalogue.mjs';
|
||||
import { sha256 } from '../../model-proxy/src/common.mjs';
|
||||
import { initializeProxy, loadProxy } from '../../model-proxy/src/settings.mjs';
|
||||
import { createProxyLock } from './proxy-lock.mjs';
|
||||
import { ensureKey, readKey as readTriageKey, revokeKeys, sweepDue, sweepKeys } from './triage-key.mjs';
|
||||
import { verifyRestart } from '../../model-proxy/src/admin.mjs';
|
||||
import { ModelKeys, modelOrganization, stopModelRuns, MODEL_STOP_TIMEOUT_MS } from './model-keys.mjs';
|
||||
import { instanceSettings } from '../../organization/src/instance-settings.mjs';
|
||||
@@ -622,6 +623,10 @@ export async function startBroker({
|
||||
// P1 validated sidecar from the trusted host; never a socket request setting.
|
||||
proxy = null,
|
||||
org = null, instanceDataRoot = null, instanceConfigDir = null,
|
||||
// proxy-scope slice 4: the Triage key sweep on a riding org bus. intervalMs (default 30 s) and
|
||||
// marginMs (default 30 s past a retiring key's retireAt, so the Console stops a session on it
|
||||
// first) are in-process seams, as are now and hook (triage-key.mjs's clock and stage hook).
|
||||
triageKeys = {},
|
||||
// Host-only preparation/admission capabilities. The host builds its own from the organization writer and `trackers`
|
||||
// (request-host.mjs); anything given here replaces the host's, and a capability nothing supplies leaves a setup wait.
|
||||
requestServices = {},
|
||||
@@ -657,6 +662,8 @@ export async function startBroker({
|
||||
throw new BusError('invalid-host-request');
|
||||
let modelProxyClient = null, proxyPage = null, proxyUsage = null, usageTimer = null, proxyLock = null, proxyPort = null;
|
||||
let modelKeys = null, modelTimer = null, modelSweep = null, modelCatalogues = null;
|
||||
// proxy-scope slice 4: the settings of the proxy this bus rides (boot, picked up or initialized).
|
||||
let proxySettings = null, triageTimer = null, triageSweepRun = null;
|
||||
const modelRuns = new Map();
|
||||
let store, credentials, server, adapter, processing, syncTimer = null, syncPass = null, accessTimer = null, requestTimer = null, copyLaunch = null, copyRevoker = null, copyTimer = null;
|
||||
// #1873 CB4: a revoke's stop waits for the broker; a start that fails before it exists stops nothing.
|
||||
@@ -707,7 +714,7 @@ export async function startBroker({
|
||||
proxyLock = createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${proxy.port}` });
|
||||
}
|
||||
modelProxyClient = await loadManagementClient({ configDir: org ? instanceConfigDir : organization.dir, proxy, receipt: proxyReceipt });
|
||||
proxyPort = proxy.port;
|
||||
proxyPort = proxy.port; proxySettings = proxy;
|
||||
}
|
||||
|
||||
// Row 234 (#1740, r6 X4 third bullet): each host grant record gives its agent a store reference to
|
||||
@@ -845,7 +852,7 @@ export async function startBroker({
|
||||
// is no client: static text and a refusal code, never a path or a key.
|
||||
let lateProxy = false, proxyCause = null, proxySync = Promise.resolve();
|
||||
const dropLateProxy = cause => {
|
||||
if (lateProxy) { modelProxyClient = null; proxyPage = null; proxyPort = null; proxyLock = null; lateProxy = false; modelCatalogues?.invalidate?.(); }
|
||||
if (lateProxy) { modelProxyClient = null; proxyPage = null; proxyPort = null; proxyLock = null; proxySettings = null; lateProxy = false; modelCatalogues?.invalidate?.(); }
|
||||
proxyCause = cause;
|
||||
};
|
||||
// The host refuses fixture-proxy-configured at boot when a dev fixture is recorded for the
|
||||
@@ -879,11 +886,65 @@ export async function startBroker({
|
||||
catch { return dropLateProxy('The shared proxy lock does not load (proxy-lock-invalid). Check the proxy state for this instance, then restart this bus.'); }
|
||||
}
|
||||
proxyCause = null;
|
||||
proxySettings = found;
|
||||
// Same port, key and lock still load: keep the client and its page, which holds login states.
|
||||
if (modelProxyClient) return;
|
||||
modelProxyClient = client; proxyPort = found.port; proxyLock = lock; lateProxy = true;
|
||||
configureProxyPage(); modelCatalogues?.invalidate?.();
|
||||
}
|
||||
// proxy-scope slice 4: what triage-key.mjs needs from a riding org bus. A key error becomes a bus
|
||||
// refusal the table already holds: proxy codes it knows pass through, the rest name the key
|
||||
// store's code in the reason only. Neither carries a key, a path or upstream text.
|
||||
// The verb checks the proxy first, as proxy.fence.clear does; the sweep uses what this bus holds
|
||||
// and never picks a proxy up on its own timer.
|
||||
const triageOptions = async ({ sync = true } = {}) => {
|
||||
if (sync) await syncProxy();
|
||||
if (org === null || !organization || !proxyLock || !modelProxyClient || !proxySettings || typeof instanceConfigDir !== 'string' || typeof instanceDataRoot !== 'string')
|
||||
throw new BusError('triage-no-riding-bus');
|
||||
return { configDir: instanceConfigDir, system: { dataRoot: instanceDataRoot, proxy: proxySettings }, client: modelProxyClient, lock: proxyLock,
|
||||
...(triageKeys.now ? { now: triageKeys.now } : {}), ...(triageKeys.hook ? { hook: triageKeys.hook } : {}) };
|
||||
};
|
||||
const TRIAGE_PASS = new Set(['proxy-lock-busy', 'proxy-lock-invalid', 'proxy-lock-uncertain', 'proxy-client-invalid', 'proxy-client-refused', 'proxy-client-response', 'proxy-management-key-invalid']);
|
||||
const TRIAGE_MAP = { 'triage-key-no-proxy': 'triage-no-riding-bus', 'triage-key-raced': 'proxy-lock-busy', 'triage-key-unconfirmed': 'proxy-lock-uncertain',
|
||||
'proxy-uncertain': 'proxy-lock-uncertain', 'proxy-client-down': 'proxy-client-response', 'proxy-client-redirect': 'proxy-client-response', 'triage-key-proxy-open': 'proxy-not-configured' };
|
||||
const triageRefusal = error => {
|
||||
if (error instanceof BusError) return error;
|
||||
const code = typeof error?.code === 'string' ? error.code : '';
|
||||
if (TRIAGE_PASS.has(code)) return new BusError(code);
|
||||
if (Object.hasOwn(TRIAGE_MAP, code)) return new BusError(TRIAGE_MAP[code], code);
|
||||
return new BusError('storage-unavailable', /^triage-key-[a-z-]+$/.test(code) ? code : 'triage-key-failed');
|
||||
};
|
||||
// The sweep: a cheap read of the file first, so the proxy is asked only when something is due;
|
||||
// sweepKeys decides again under the lock. It never issues: no file, nothing.
|
||||
const triageMarginMs = Number.isSafeInteger(triageKeys.marginMs) && triageKeys.marginMs >= 0 ? triageKeys.marginMs : 30_000;
|
||||
// A refusal that repeats on every tick is logged when it first appears or changes, and then once
|
||||
// an hour while it stays the same; any other outcome clears it.
|
||||
let triageLastRefusal = null;
|
||||
const sweepRefused = (now, entry) => {
|
||||
const sig = `${entry.code}|${entry.reason ?? ''}`;
|
||||
if (triageLastRefusal?.sig === sig && now - triageLastRefusal.at < 3_600_000) return;
|
||||
triageLastRefusal = { sig, at: now };
|
||||
proxyReceipt({ action: 'triage.key.sweep-refused', ...entry });
|
||||
};
|
||||
const sweepTriage = async () => {
|
||||
let opts;
|
||||
try { opts = await triageOptions({ sync: false }); } catch { return { action: 'none' }; }
|
||||
const now = (opts.now ?? Date.now)();
|
||||
let record;
|
||||
try { record = readTriageKey(opts.configDir); } catch { sweepRefused(now, { code: 'triage-key-unreadable' }); return { action: 'none' }; }
|
||||
if (!sweepDue(record, now, triageMarginMs)) { triageLastRefusal = null; return { action: 'none' }; }
|
||||
try {
|
||||
const result = await sweepKeys(opts, { marginMs: triageMarginMs });
|
||||
triageLastRefusal = null;
|
||||
if (result.action !== 'none') proxyReceipt({ action: `triage.key.${result.action}`, keyId: result.keyId ?? null, addedKeyIds: result.addedKeyIds, removedKeyIds: result.removedKeyIds });
|
||||
return result;
|
||||
} catch (error) {
|
||||
const refusal = triageRefusal(error);
|
||||
sweepRefused(now, { code: refusal.code, ...(typeof refusal.reason === 'string' ? { reason: refusal.reason } : {}) });
|
||||
return { action: 'refused', code: refusal.code };
|
||||
}
|
||||
};
|
||||
const triageSweepOnce = () => (triageSweepRun ??= sweepTriage().finally(() => { triageSweepRun = null; }));
|
||||
// One check at a time; an unexpected failure drops a late client, never keeps it.
|
||||
const syncProxy = () => (proxySync = proxySync.then(pickProxy).catch(() => dropLateProxy('The model proxy could not be checked (proxy-down). Try again shortly.')));
|
||||
// Unavailable with no client: the catalogue names the cause instead of a bare unavailable.
|
||||
@@ -1419,7 +1480,7 @@ export async function startBroker({
|
||||
const lock = org && !proxyLock ? createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${settings.port}` }) : proxyLock;
|
||||
// proxy-pickup: a client this bus did not boot with is checked again like a picked-up one.
|
||||
if (!proxy) { lateProxy = true; proxyCause = null; }
|
||||
modelProxyClient = client; proxyPort = settings.port; proxyLock = lock;
|
||||
modelProxyClient = client; proxyPort = settings.port; proxyLock = lock; proxySettings = settings;
|
||||
configureProxyPage(); modelCatalogues?.invalidate?.();
|
||||
return settings;
|
||||
} : null,
|
||||
@@ -1430,6 +1491,15 @@ export async function startBroker({
|
||||
if (!proxyLock || !modelProxyClient) throw new BusError('not-configured', proxyCause ?? undefined);
|
||||
return proxyLock.clear({ restarted: true, read: () => modelProxyClient.getKeys(), verify: previous => verifyRestart(evidence, { fence: previous, port: proxyPort }) });
|
||||
} : null,
|
||||
// proxy-scope slice 4 (Jason 2026-11-19): ensure or revoke the instance's Triage key through the
|
||||
// proxy this org bus rides, under the bus's own client and instance proxy lock. The answer is
|
||||
// ids only. A no-org bus, or an org bus with no proxy client and lock, refuses
|
||||
// triage-no-riding-bus.
|
||||
proxyTriageKey: async (op) => {
|
||||
const opts = await triageOptions();
|
||||
try { return op === 'ensure' ? await ensureKey(opts) : await revokeKeys(opts); }
|
||||
catch (error) { throw triageRefusal(error); }
|
||||
},
|
||||
credentialVerbs: organization
|
||||
? credentialVerbs({ credentials, writer: organizationWriter({ dir: organization.dir, rolesDir: organization.rolesDir, dataRoot:instanceRoot ?? dataRoot }),
|
||||
organizations: () => Object.keys(businesses), boot: copies?.definitions ? { org: copies.org, definitions: copies.definitions } : null })
|
||||
@@ -1737,6 +1807,12 @@ export async function startBroker({
|
||||
}, access.intervalMs ?? ACCESS_SWEEP_MS);
|
||||
accessTimer.unref();
|
||||
}
|
||||
// proxy-scope slice 4: only an org bus sweeps the Triage key; each tick checks it still rides.
|
||||
if (org !== null && organization) {
|
||||
triageTimer = setInterval(() => { triageSweepOnce(); },
|
||||
Number.isSafeInteger(triageKeys.intervalMs) && triageKeys.intervalMs > 0 ? triageKeys.intervalMs : 30_000);
|
||||
triageTimer.unref();
|
||||
}
|
||||
if (modelKeys) {
|
||||
modelTimer = setInterval(() => {
|
||||
if (modelSweep) return;
|
||||
@@ -1905,6 +1981,8 @@ export async function startBroker({
|
||||
definitionScan,
|
||||
launches: bound,
|
||||
readers: readCaps,
|
||||
// proxy-scope slice 4: one Triage key sweep now, as the timer runs it (tests).
|
||||
triageKeySweep: () => triageSweepOnce(),
|
||||
async close() {
|
||||
clearInterval(copyTimer);
|
||||
brokerUp(null);
|
||||
@@ -1913,6 +1991,8 @@ export async function startBroker({
|
||||
await proxyUsage.close();
|
||||
clearInterval(modelTimer);
|
||||
await modelSweep;
|
||||
clearInterval(triageTimer);
|
||||
await triageSweepRun;
|
||||
clearInterval(accessTimer);
|
||||
await stopSync();
|
||||
await server.close();
|
||||
@@ -1930,6 +2010,7 @@ export async function startBroker({
|
||||
clearInterval(usageTimer);
|
||||
await proxyUsage?.close();
|
||||
clearInterval(modelTimer);
|
||||
clearInterval(triageTimer);
|
||||
clearInterval(accessTimer);
|
||||
await server?.close();
|
||||
await adapter?.close?.();
|
||||
|
||||
@@ -155,6 +155,8 @@ export async function serve({ broker, path, authenticateHuman = null, authentica
|
||||
// Row 236 (#1742): the grants verbs may call Gitea several times, 15 s each at most.
|
||||
GRANTS_VERBS.includes(r.verb) ||
|
||||
r.verb === 'proxy.init' ||
|
||||
// proxy-scope slice 4: the Triage key verb waits on the proxy lock and the proxy.
|
||||
r.verb === 'proxy.triage.key' ||
|
||||
(r.verb === 'decision.raise' && (Object.hasOwn(r.args ?? {}, 'spec_path') || Object.hasOwn(r.args ?? {}, 'spec_commit') ||
|
||||
Object.hasOwn(r.args ?? {}, 'grant')))) {
|
||||
const own = transient;
|
||||
|
||||
File renamed without changes.
@@ -1,5 +1,9 @@
|
||||
// #1873 triage-key: Triage's own model proxy client key, issued from the owner's terminal
|
||||
// (design report reports/triage-route-design.md slice 3, Jason's ruling 2026-11-18).
|
||||
// (design report reports/triage-route-design.md slice 3, Jason's ruling 2026-11-18), and since
|
||||
// proxy-scope slice 4 (Jason's ruling 2026-11-19) by the bus too: the verb proxy.triage.key
|
||||
// (ensure, revoke) and a riding org bus's sweep call this module with the bus's own management
|
||||
// client and instance proxy lock. It moved here from packages/cli/src so the bus can import it; the
|
||||
// terminal verbs (packages/cli/src/triage.mjs) call it unchanged.
|
||||
//
|
||||
// <configDir>/secrets/triage/model (directory 0700, file 0600, the running user's)
|
||||
// { version, key, keyId, issuedAt, rotateBy, retiring: [{ key, keyId, issuedAt, retireAt }] }
|
||||
@@ -16,9 +20,11 @@
|
||||
// uncertain fence, which every proxy writer on the instance refuses until a human clears it
|
||||
// (`mosaic proxy fence clear --proxy-restarted`).
|
||||
//
|
||||
// Nothing runs on a timer. rotate records when the old key's grace ends; the key stays in the proxy
|
||||
// until a later retire, rotate or issue removes it, and status says when that is due. The same goes
|
||||
// for rotateBy: status marks it due; only rotate replaces the key.
|
||||
// rotate records when the old key's grace ends; the key stays in the proxy until a later retire,
|
||||
// rotate, issue or sweep removes it, and status says when that is due. A riding org bus runs
|
||||
// sweepKeys on a timer: under the lock it reads the file again and rotates when rotateBy is due, or
|
||||
// retires keys whose grace (plus a margin, so the Console stops a session on the key first) has
|
||||
// passed. The sweep only replaces a key that exists; it never issues the first one.
|
||||
//
|
||||
// Nothing here prints, logs or records a key. Receipts and output carry key ids only (the first 12
|
||||
// hex of sha256(key), the id the bus uses). Messages name files and options, never values.
|
||||
@@ -27,7 +33,7 @@ import { closeSync, constants, fchmodSync, fsyncSync, lstatSync, openSync, readd
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { join } from "node:path";
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
import { createProxyLock } from "../../bus/src/proxy-lock.mjs";
|
||||
import { createProxyLock } from "./proxy-lock.mjs";
|
||||
import { loadManagementClient } from "../../model-proxy/src/client.mjs";
|
||||
|
||||
// #1873 triage-wiring: the key file's reader lives in triage-key-file.mjs (node built-ins only), so
|
||||
@@ -136,20 +142,24 @@ const proxyFailure = (code) => {
|
||||
};
|
||||
|
||||
// opts.uid, opts.now and opts.hook(stage) are test seams; a stage hook may throw or kill the process.
|
||||
function context({ configDir, system, uid, fileUid, now, hook }) {
|
||||
// opts.client and opts.lock: the bus passes its own management client and instance proxy lock, so a
|
||||
// bus change runs under the lock object its model key sweep uses and reads no key file of its own.
|
||||
function context({ configDir, system, uid, fileUid, now, hook, client, lock }) {
|
||||
if (!system?.proxy || !Number.isInteger(system.proxy.port)) throw new TriageKeyError("triage-key-no-proxy", "the instance has no model proxy set up (proxy.json)");
|
||||
return {
|
||||
configDir, dataRoot: system.dataRoot, proxy: system.proxy, stateDir: join(system.dataRoot, "proxy"),
|
||||
uid: uid ?? process.getuid(), fileUid, now: now ?? Date.now, hook: hook ?? (() => {}),
|
||||
uid: uid ?? process.getuid(), fileUid, now: now ?? Date.now, hook: hook ?? (() => {}), client, lock,
|
||||
};
|
||||
}
|
||||
|
||||
function lockFor(c) {
|
||||
if (c.lock) return c.lock;
|
||||
try { return createProxyLock({ stateDir: c.stateDir, identity: `${c.configDir}:${c.proxy.port}` }); }
|
||||
catch (e) { throw proxyFailure(e.code ?? "proxy-lock-invalid"); }
|
||||
}
|
||||
|
||||
async function clientFor(c) {
|
||||
if (c.client) return c.client;
|
||||
try { return await loadManagementClient({ configDir: c.configDir, proxy: c.proxy }); }
|
||||
catch (e) { throw proxyFailure(e.code ?? "proxy-management-key-invalid"); }
|
||||
}
|
||||
@@ -162,9 +172,10 @@ function checkSentinel(c, keys) {
|
||||
}
|
||||
|
||||
// Under the instance proxy lock: read the list, check the sentinel, run `plan`, write the result.
|
||||
// `plan(keys, record)` returns { after, record, removed, added, done }; `record` null leaves the
|
||||
// file alone. The proxy changes first, then the file, then one receipt line.
|
||||
async function change(c, op, plan) {
|
||||
// `plan(keys, record)` returns { after, record, remove, op, done }; `record` null leaves the file
|
||||
// alone, `remove` true unlinks it (revoke), `op` names the change when the plan picks it (sweep).
|
||||
// The proxy changes first, then the file, then one receipt line.
|
||||
async function change(c, opName, plan) {
|
||||
const lock = lockFor(c);
|
||||
const client = await clientFor(c);
|
||||
try {
|
||||
@@ -173,6 +184,8 @@ async function change(c, op, plan) {
|
||||
checkSentinel(c, before);
|
||||
const current = readKey(c.configDir, { uid: c.uid, fileUid: c.fileUid });
|
||||
const result = plan(before, current);
|
||||
const op = result.op ?? opName;
|
||||
const remove = result.remove === true && current !== null;
|
||||
if (result.record) prepareDirs(c.configDir, c.uid);
|
||||
removeTemps(c.configDir);
|
||||
receiptsReady(c.stateDir, c.uid);
|
||||
@@ -195,8 +208,8 @@ async function change(c, op, plan) {
|
||||
}
|
||||
c.hook("write");
|
||||
let unsynced = false;
|
||||
if (result.record) {
|
||||
try { writeKey(c.configDir, result.record, c.uid, c.hook); }
|
||||
if (result.record || remove) {
|
||||
try { if (remove) unlinkKey(c.configDir, c.hook); else writeKey(c.configDir, result.record, c.uid, c.hook); }
|
||||
catch (e) {
|
||||
if (e instanceof NotSynced) unsynced = true;
|
||||
else throw new TriageKeyError("triage-key-unwritten", `the proxy list changed but ${keyPaths(c.configDir).file} was not written (${e.message}); fix the file's directory, then run mosaic triage key ${op} again, which clears what this run left`);
|
||||
@@ -204,7 +217,7 @@ async function change(c, op, plan) {
|
||||
}
|
||||
c.hook("receipt");
|
||||
// A run that changed neither the proxy nor the file leaves no receipt.
|
||||
if (added.length || removed.length || result.record) receipt(c.stateDir, c.uid, { op: `triage.key.${op}`, time: new Date(c.now()).toISOString(), addedKeyIds: added.map(keyIdOf), removedKeyIds: removed.map(keyIdOf) });
|
||||
if (added.length || removed.length || result.record || remove) receipt(c.stateDir, c.uid, { op: `triage.key.${op}`, time: new Date(c.now()).toISOString(), addedKeyIds: added.map(keyIdOf), removedKeyIds: removed.map(keyIdOf) });
|
||||
// The file is in place and the receipt written; only its durability is in doubt.
|
||||
if (unsynced) throw new TriageKeyError("triage-key-unsynced", `${keyPaths(c.configDir).file} was written but its directory could not be synced, so it may not survive a power loss; run mosaic triage key status`);
|
||||
return { ...result.done, addedKeyIds: added.map(keyIdOf), removedKeyIds: removed.map(keyIdOf) };
|
||||
@@ -215,6 +228,18 @@ async function change(c, op, plan) {
|
||||
}
|
||||
}
|
||||
|
||||
// Revoke's file step: unlink, then sync the directory, with writeKey's stages and failures.
|
||||
function unlinkKey(configDir, hook = () => {}) {
|
||||
const { dir, file } = keyPaths(configDir);
|
||||
try { hook("rename"); unlinkSync(file); }
|
||||
catch (e) { if (e?.code !== "ENOENT") throw unreadable("the key file cannot be removed"); }
|
||||
try {
|
||||
hook("sync");
|
||||
const dfd = openSync(dir, constants.O_RDONLY | constants.O_DIRECTORY);
|
||||
try { fsyncSync(dfd); } finally { closeSync(dfd); }
|
||||
} catch { throw new NotSynced(); }
|
||||
}
|
||||
|
||||
const newKey = (configDir) => keyPrefix(configDir) + randomBytes(32).toString("base64url");
|
||||
|
||||
// Keys in the Triage namespace that the file does not hold: left by a crash between the PUT and the
|
||||
@@ -235,39 +260,117 @@ export async function issueKey(opts) {
|
||||
});
|
||||
}
|
||||
|
||||
// Retiring keys whose grace has passed, or that the proxy no longer holds.
|
||||
const dueOf = (record, keys, nowMs) => record.retiring.filter((r) => Date.parse(r.retireAt) <= nowMs || !keys.includes(r.key));
|
||||
// Retiring keys whose grace (plus marginMs, 0 for the terminal verbs) has passed, or that the proxy
|
||||
// no longer holds.
|
||||
const dueOf = (record, keys, nowMs, marginMs = 0) => record.retiring.filter((r) => Date.parse(r.retireAt) + marginMs <= nowMs || !keys.includes(r.key));
|
||||
const rotateDueAt = (record) => Date.parse(`${record.rotateBy}T00:00:00Z`);
|
||||
|
||||
// The rotate plan, shared by the terminal verb and the sweep.
|
||||
function rotatePlan(c, before, current, nowMs, marginMs = 0) {
|
||||
const orphans = orphansOf(c.configDir, before, current);
|
||||
const due = dueOf(current, before, nowMs, marginMs);
|
||||
const keep = before.includes(current.key);
|
||||
const retiring = [...current.retiring.filter((r) => !due.includes(r)),
|
||||
...(keep ? [{ key: current.key, keyId: current.keyId, issuedAt: current.issuedAt, retireAt: retireAtFor(nowMs, c.proxy) }] : [])];
|
||||
if (retiring.length > MAX_RETIRING) throw new TriageKeyError("triage-key-failed", "too many keys are retiring; run mosaic triage key retire once their grace has passed");
|
||||
const key = newKey(c.configDir);
|
||||
const record = { version: KEY_VERSION, key, keyId: keyIdOf(key), issuedAt: new Date(nowMs).toISOString(), rotateBy: rotateByFor(nowMs), retiring };
|
||||
const drop = [...orphans, ...due.map((r) => r.key)];
|
||||
return {
|
||||
after: [...before.filter((k) => !drop.includes(k)), key], record,
|
||||
done: { keyId: record.keyId, rotateBy: record.rotateBy, old: keep ? { keyId: current.keyId, retireAt: retiring.at(-1).retireAt } : { keyId: current.keyId, lost: true } },
|
||||
};
|
||||
}
|
||||
|
||||
export async function rotateKey(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "rotate", (before, current) => {
|
||||
if (!current) throw new TriageKeyError("triage-key-not-issued", "no Triage key is issued; use mosaic triage key issue");
|
||||
const nowMs = c.now();
|
||||
const orphans = orphansOf(c.configDir, before, current);
|
||||
const due = dueOf(current, before, nowMs);
|
||||
const keep = before.includes(current.key);
|
||||
const retiring = [...current.retiring.filter((r) => !due.includes(r)),
|
||||
...(keep ? [{ key: current.key, keyId: current.keyId, issuedAt: current.issuedAt, retireAt: retireAtFor(nowMs, c.proxy) }] : [])];
|
||||
if (retiring.length > MAX_RETIRING) throw new TriageKeyError("triage-key-failed", "too many keys are retiring; run mosaic triage key retire once their grace has passed");
|
||||
const key = newKey(c.configDir);
|
||||
const record = { version: KEY_VERSION, key, keyId: keyIdOf(key), issuedAt: new Date(nowMs).toISOString(), rotateBy: rotateByFor(nowMs), retiring };
|
||||
const drop = [...orphans, ...due.map((r) => r.key)];
|
||||
return {
|
||||
after: [...before.filter((k) => !drop.includes(k)), key], record,
|
||||
done: { keyId: record.keyId, rotateBy: record.rotateBy, old: keep ? { keyId: current.keyId, retireAt: retiring.at(-1).retireAt } : { keyId: current.keyId, lost: true } },
|
||||
};
|
||||
return rotatePlan(c, before, current, c.now());
|
||||
});
|
||||
}
|
||||
function retirePlan(c, before, current, nowMs, marginMs = 0) {
|
||||
const orphans = orphansOf(c.configDir, before, current);
|
||||
const due = current ? dueOf(current, before, nowMs, marginMs) : [];
|
||||
const record = current && due.length ? { ...current, retiring: current.retiring.filter((r) => !due.includes(r)) } : null;
|
||||
const drop = [...orphans, ...due.map((r) => r.key)];
|
||||
return { after: before.filter((k) => !drop.includes(k)), record, done: { droppedKeyIds: due.map((r) => r.keyId) } };
|
||||
}
|
||||
|
||||
export async function retireKeys(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "retire", (before, current) => {
|
||||
return change(c, "retire", (before, current) => retirePlan(c, before, current, c.now()));
|
||||
}
|
||||
|
||||
// proxy-scope slice 4: the bus verb proxy.triage.key op ensure. Leaves a held key alone, issues one
|
||||
// when none exists, replaces one the proxy lost. Never prints the key; the answer is ids only.
|
||||
// issued no file: a new key (keys in the namespace the file does not hold are removed)
|
||||
// held the proxy holds the file's key: only orphans and retiring keys the proxy lost go
|
||||
// replaced the proxy does not hold the file's key: a new key, the old one marked lost
|
||||
export async function ensureKey(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "ensure", (before, current) => {
|
||||
const nowMs = c.now();
|
||||
const orphans = orphansOf(c.configDir, before, current);
|
||||
const due = current ? dueOf(current, before, nowMs) : [];
|
||||
const record = current && due.length ? { ...current, retiring: current.retiring.filter((r) => !due.includes(r)) } : null;
|
||||
const drop = [...orphans, ...due.map((r) => r.key)];
|
||||
return { after: before.filter((k) => !drop.includes(k)), record, done: { droppedKeyIds: due.map((r) => r.keyId) } };
|
||||
const fresh = (retiring) => {
|
||||
const key = newKey(c.configDir);
|
||||
return { version: KEY_VERSION, key, keyId: keyIdOf(key), issuedAt: new Date(nowMs).toISOString(), rotateBy: rotateByFor(nowMs), retiring };
|
||||
};
|
||||
if (!current) {
|
||||
const record = fresh([]);
|
||||
return { after: [...before.filter((k) => !orphans.includes(k)), record.key], record, done: { action: "issued", keyId: record.keyId, rotateBy: record.rotateBy } };
|
||||
}
|
||||
const lost = current.retiring.filter((r) => !before.includes(r.key));
|
||||
const retiring = current.retiring.filter((r) => !lost.includes(r));
|
||||
if (before.includes(current.key)) {
|
||||
return {
|
||||
after: before.filter((k) => !orphans.includes(k)), record: lost.length ? { ...current, retiring } : null,
|
||||
done: { action: "held", keyId: current.keyId, rotateBy: current.rotateBy },
|
||||
};
|
||||
}
|
||||
const record = fresh(retiring);
|
||||
return {
|
||||
after: [...before.filter((k) => !orphans.includes(k)), record.key], record,
|
||||
done: { action: "replaced", keyId: record.keyId, rotateBy: record.rotateBy, old: { keyId: current.keyId, lost: true } },
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// proxy-scope slice 4: op revoke, when the owner turns Triage off. Every name in this instance's
|
||||
// Triage namespace leaves the proxy list, then the key file goes. No file and no names: nothing.
|
||||
export async function revokeKeys(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "revoke", (before, current) => {
|
||||
const mine = before.filter(inNamespace(c.configDir));
|
||||
return { after: before.filter((k) => !mine.includes(k)), record: null, remove: true, done: { action: mine.length || current ? "revoked" : "none" } };
|
||||
});
|
||||
}
|
||||
|
||||
// What a sweep would do from the file alone: "rotate" when rotateBy has come, "retire" when a
|
||||
// retiring key's grace plus marginMs has passed, else null. A cheap check without the lock or the
|
||||
// proxy; sweepKeys decides again under the lock.
|
||||
export function sweepDue(record, nowMs, marginMs = 0) {
|
||||
if (!record) return null;
|
||||
if (rotateDueAt(record) <= nowMs) return "rotate";
|
||||
return record.retiring.some((r) => Date.parse(r.retireAt) + marginMs <= nowMs) ? "retire" : null;
|
||||
}
|
||||
|
||||
// The bus sweep. Under the instance proxy lock it reads the file again and acts only if the action
|
||||
// is still due, so two buses that wake together change the proxy once. No file: nothing, ever.
|
||||
export async function sweepKeys(opts, { marginMs = 0 } = {}) {
|
||||
const c = context(opts);
|
||||
return change(c, "sweep", (before, current) => {
|
||||
const nowMs = c.now();
|
||||
const due = sweepDue(current, nowMs, marginMs);
|
||||
if (due === "rotate") {
|
||||
const plan = rotatePlan(c, before, current, nowMs, marginMs);
|
||||
return { ...plan, op: "rotate", done: { action: "rotated", ...plan.done } };
|
||||
}
|
||||
if (due === "retire") {
|
||||
const plan = retirePlan(c, before, current, nowMs, marginMs);
|
||||
return { ...plan, op: "retire", done: { action: "retired", ...plan.done } };
|
||||
}
|
||||
return { after: before, record: null, done: { action: "none" } };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import { join } from 'node:path';
|
||||
import { sandboxProblem } from '../../seat/src/sandbox.mjs';
|
||||
import { startTimeOf } from '../../seat/src/proc.mjs';
|
||||
import { ownershipFixture, waitFor } from './model-hire-ownership-fixture.mjs';
|
||||
import * as pi from '../../plugins/harness/pi/index.mjs';
|
||||
const unavailable=sandboxProblem()??false; // Any skip means UNVERIFIED, not green acceptance.
|
||||
const done=async box=>{await box.promise;return box;};
|
||||
const refused=async(box,code)=>{await done(box);assert.equal(box.ok,false,'real operation refuses');assert.equal(box.code,code,'real operation preserves exact refusal');};
|
||||
@@ -17,7 +18,12 @@ test('655 listed duplicate with no record refuses before any model or management
|
||||
assert.deepEqual(f.snapshot(),before,'duplicate hire makes no credential or management effects');f.assertHistoryGuard();f.valueFree();
|
||||
});
|
||||
|
||||
test('655 actual launch own preflight generation and bound consumer survive concurrent duplicate hire',{skip:unavailable,timeout:30000},async t=>{
|
||||
test('655 actual launch own preflight generation and bound consumer survive concurrent duplicate hire',{skip:unavailable,timeout:120000},async t=>{
|
||||
// #1873 pi-unpin re-check 9 and 10: the launch's preflight shares pi's fit check, which is cold in
|
||||
// a fresh process. Wait for it before the fixture starts its launcher, so the launcher's warm-up
|
||||
// and the launch's preflight hit the cache, the gate's 5 s times the launch, and the launch starts
|
||||
// at the same point in the launcher's 2 s tick as it did before the fit check existed.
|
||||
await pi.ready({env:{PATH:process.env.PATH,HOME:process.env.HOME,TMPDIR:process.env.TMPDIR}});
|
||||
const f=await ownershipFixture(t),pGet=f.hold('GET'),p=f.launchAide();await pGet.entered();
|
||||
// P's own acquisition GET is held. Its PUT confirmation GET is ordinal+1;
|
||||
// only an OLD H reuse GET can be ordinal+2. The correct early refusal removes it.
|
||||
|
||||
@@ -4,6 +4,7 @@ import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { fakeManagement } from './proxy-page-fixture.mjs';
|
||||
import { PROVIDERS } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
const id = value => createHash('sha256').update(value).digest('hex').slice(0, 12);
|
||||
const oauth = extra => ({ id: 'oauth-one.json', name: 'oauth-one.json', provider: 'claude', label: 'p4-private-label',
|
||||
email: 'p4-private-email', token: 'p4-private-token', status: 'ready', priority: 3, ...extra });
|
||||
@@ -11,7 +12,9 @@ test('accounts have exactly the allowlisted public fields, id from credential id
|
||||
const s = await fakeManagement(t, { files: [oauth()] });
|
||||
const result = await s.call('proxy.accounts');
|
||||
assert.deepEqual(result, { accounts: [{ accountId: id('oauth-one.json'), provider: 'claude',
|
||||
displayName: `Claude account ${id('oauth-one.json')}`, status: 'ready', priority: 3 }] });
|
||||
displayName: `Claude account ${id('oauth-one.json')}`, status: 'ready', priority: 3 }],
|
||||
// settings-backend-a3: the provider list rides along, id, label and sign-in kind only.
|
||||
providers: PROVIDERS.map(({ id, label, signIn }) => ({ id, label, signIn })) });
|
||||
for (const value of ['p4-private-label', 'p4-private-email', 'p4-private-token']) {
|
||||
assert.equal(JSON.stringify(result).includes(value), false);
|
||||
assert.notEqual(result.accounts[0].accountId, id(value));
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
// #1873 settings-backend-a3: the proxy page's provider names, login allow list, sign-in host and key
|
||||
// allow list all come from the plugin's PROVIDERS. A provider a test copy of the list leaves out is
|
||||
// refused at each door with the same code as before; one a copy adds is let through. The accounts
|
||||
// answer carries the list as id, label and sign-in kind only.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { ProxyPage } from '../src/proxy-page.mjs';
|
||||
import { PROVIDERS } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
const owner = { business: 'demo', human: 'owner' };
|
||||
function setup(options = {}) {
|
||||
const calls = [], state = { files: [], groups: [] };
|
||||
const client = Object.fromEntries(['getCredentials', 'oauthStart', 'oauthCallback', 'oauthStatus', 'oauthCancel', 'getUpstreamGroups', 'putUpstreamGroups'].map(name => [name, async (...args) => {
|
||||
calls.push([name, ...args]);
|
||||
if (name === 'oauthStart') return { status: 'ok', state: `s-${calls.length}`, url: `https://${options.hosts?.[args[0]] ?? 'nowhere.invalid'}/oauth/authorize?state=s-${calls.length}` };
|
||||
if (name === 'getCredentials') return { files: state.files };
|
||||
if (name === 'getUpstreamGroups') return structuredClone(state.groups);
|
||||
if (name === 'putUpstreamGroups') { state.groups = args[0].map((g, i) => ({ ...g, keys: g.keys.map((k, j) => ({ ...k, auth_index: `idx-${i}-${j}` })) })); return { status: 'ok' }; }
|
||||
return { status: 'ok' };
|
||||
}]));
|
||||
const p = new ProxyPage({ client, instanceTag: 'fixture', ...(options.providers ? { providers: options.providers } : {}) });
|
||||
return { calls, state, call: (verb, args = {}) => p.request(verb, args, owner) };
|
||||
}
|
||||
const hosts = Object.fromEntries(PROVIDERS.filter(p => p.signIn === 'oauth').map(p => [p.id, p.signInHost]));
|
||||
const door = p => p.signIn === 'oauth' ? ['proxy.login.start', { provider: p.id }] : ['proxy.apikey.add', { provider: p.id, key: 'fixture-key' }];
|
||||
const otherDoor = p => p.signIn === 'oauth' ? ['proxy.apikey.add', { provider: p.id, key: 'fixture-key' }] : ['proxy.login.start', { provider: p.id }];
|
||||
|
||||
test('a3: each provider in PROVIDERS passes its own door and is refused at the other with provider-not-allowed', async () => {
|
||||
assert.ok(PROVIDERS.some(p => p.signIn === 'oauth') && PROVIDERS.some(p => p.signIn === 'apikey'));
|
||||
for (const p of PROVIDERS) {
|
||||
const s = setup({ hosts });
|
||||
const answer = await s.call(...door(p));
|
||||
if (p.signIn === 'oauth') assert.equal(new URL(answer.url).hostname, p.signInHost, p.id);
|
||||
else { assert.deepEqual(answer, { added: true }); assert.equal(s.state.groups.at(-1)['base-url'], p.baseUrl); }
|
||||
const before = s.calls.length;
|
||||
await assert.rejects(s.call(...otherDoor(p)), { code: 'provider-not-allowed' }, p.id);
|
||||
assert.equal(s.calls.length, before, `${p.id}: a refusal reaches no proxy call`);
|
||||
}
|
||||
for (const [verb, args] of [['proxy.login.start', { provider: 'other' }], ['proxy.apikey.add', { provider: 'other', key: 'fixture-key' }]]) {
|
||||
const s = setup({ hosts });
|
||||
await assert.rejects(s.call(verb, args), { code: 'provider-not-allowed' }); assert.equal(s.calls.length, 0);
|
||||
}
|
||||
});
|
||||
|
||||
test('a3: a provider taken out of a test copy of the list is refused at its door, reads Other and leaves the providers answer', async () => {
|
||||
for (const gone of PROVIDERS) {
|
||||
const s = setup({ hosts, providers: PROVIDERS.filter(p => p !== gone) });
|
||||
await assert.rejects(s.call(...door(gone)), { code: 'provider-not-allowed' }, gone.id);
|
||||
assert.equal(s.calls.length, 0, `${gone.id}: refused before any proxy call`);
|
||||
if (gone.signIn === 'oauth') s.state.files = [{ id: 'one.json', name: 'one.json', provider: gone.id, status: 'ready' }];
|
||||
else s.state.groups = [{ name: 'g', 'base-url': gone.baseUrl, keys: [{ auth_index: 'idx-0-0' }], models: [{ name: 'm1' }] }];
|
||||
const answer = await s.call('proxy.accounts');
|
||||
assert.equal(answer.accounts.length, 1);
|
||||
assert.equal(answer.accounts[0].provider, 'other', gone.id);
|
||||
assert.match(answer.accounts[0].displayName, /^Other account /);
|
||||
assert.equal(Object.hasOwn(answer.accounts[0], 'models'), false, `${gone.id}: an unlisted group shows no models`);
|
||||
assert.deepEqual(answer.providers.map(p => p.id), PROVIDERS.filter(p => p !== gone).map(p => p.id));
|
||||
}
|
||||
});
|
||||
|
||||
test('a3: a provider a test copy adds is let through: key group from its base URL, sign-in on its host, name from its label', async () => {
|
||||
const extra = [{ id: 'fixture.ai', label: 'Fixture AI', signIn: 'apikey', baseUrl: 'https://api.fixture.invalid/v1' },
|
||||
{ id: 'fixture-sso', label: 'Fixture SSO', signIn: 'oauth', signInHost: 'sso.fixture.invalid' }];
|
||||
const s = setup({ providers: [...PROVIDERS, ...extra], hosts: { ...hosts, 'fixture-sso': 'sso.fixture.invalid' } });
|
||||
assert.deepEqual(await s.call('proxy.apikey.add', { provider: 'fixture.ai', key: 'fixture-key' }), { added: true });
|
||||
assert.equal(s.state.groups[0]['base-url'], 'https://api.fixture.invalid/v1');
|
||||
assert.match(s.state.groups[0].name, /^mosaic-fixtureai-[0-9a-f]{12}$/);
|
||||
assert.equal(new URL((await s.call('proxy.login.start', { provider: 'fixture-sso' })).url).hostname, 'sso.fixture.invalid');
|
||||
s.state.files = [{ id: 'sso.json', name: 'sso.json', provider: 'fixture-sso', status: 'ready' }];
|
||||
const { accounts } = await s.call('proxy.accounts');
|
||||
assert.deepEqual(accounts.map(a => [a.provider, a.displayName.replace(/ [0-9a-f]{12}$/, '')]), [['fixture-sso', 'Fixture SSO account'], ['fixture.ai', 'Fixture AI account']]);
|
||||
assert.deepEqual(accounts[1].models, []);
|
||||
});
|
||||
|
||||
test('a3: the sign-in address must be on the host the list names for that provider', async () => {
|
||||
const codex = PROVIDERS.find(p => p.id === 'codex');
|
||||
const moved = PROVIDERS.map(p => p === codex ? { ...p, signInHost: 'elsewhere.invalid' } : p);
|
||||
const s = setup({ hosts, providers: moved });
|
||||
await assert.rejects(s.call('proxy.login.start', { provider: 'codex' }), { code: 'proxy-down' });
|
||||
const t = setup({ hosts: { ...hosts, codex: 'elsewhere.invalid' }, providers: moved });
|
||||
assert.equal(new URL((await t.call('proxy.login.start', { provider: 'codex' })).url).hostname, 'elsewhere.invalid');
|
||||
});
|
||||
|
||||
test('a3: the accounts answer carries the providers as id, label and sign-in kind only, no URL, host or key', async () => {
|
||||
const s = setup({ hosts });
|
||||
await s.call('proxy.apikey.add', { provider: 'z.ai', key: 'a3-canary-key-value' });
|
||||
const answer = await s.call('proxy.accounts');
|
||||
assert.deepEqual(answer.providers, PROVIDERS.map(({ id, label, signIn }) => ({ id, label, signIn })));
|
||||
for (const p of answer.providers) assert.deepEqual(Object.keys(p).sort(), ['id', 'label', 'signIn']);
|
||||
const text = JSON.stringify(answer.providers);
|
||||
for (const word of ['a3-canary-key-value', 'https://', 'api.z.ai', 'claude.ai', 'auth.openai.com', 'baseUrl', 'signInHost']) assert.equal(text.includes(word), false, word);
|
||||
});
|
||||
|
||||
// Review P3-1: an entry missing what its door needs is refused before any proxy call, so no sign-in
|
||||
// is opened at the proxy that the page could not cancel; a key group with no base URL is no provider's.
|
||||
test('a3: an entry with no sign-in host or no key base URL is refused before any proxy call and names no group', async () => {
|
||||
const bare = [{ id: 'nohost', label: 'No Host', signIn: 'oauth' }, { id: 'nourl', label: 'No URL', signIn: 'apikey' }];
|
||||
const s = setup({ providers: [...PROVIDERS, ...bare], hosts: { ...hosts, nohost: 'nohost.invalid' } });
|
||||
await assert.rejects(s.call('proxy.login.start', { provider: 'nohost' }), { code: 'provider-not-allowed' });
|
||||
await assert.rejects(s.call('proxy.apikey.add', { provider: 'nourl', key: 'fixture-key' }), { code: 'provider-not-allowed' });
|
||||
assert.equal(s.calls.length, 0);
|
||||
s.state.groups = [{ name: 'g', keys: [{ auth_index: 'idx-0-0' }], models: [{ name: 'm1' }] }];
|
||||
s.state.files = [{ id: 'one.json', name: 'one.json', provider: 'nohost', status: 'ready' }];
|
||||
const { accounts } = await s.call('proxy.accounts');
|
||||
assert.deepEqual(accounts.map(a => a.provider), ['other', 'other']);
|
||||
assert.equal(accounts.some(a => Object.hasOwn(a, 'models')), false);
|
||||
});
|
||||
@@ -0,0 +1,305 @@
|
||||
// proxy-scope slice 4 (#1873): the Triage key in the bus. Real organization buses on one instance,
|
||||
// a stand-in proxy management server on loopback and a temporary config directory; nothing here
|
||||
// reads a real config, proxy or secret. Each test scans every answer, event, receipt line and
|
||||
// stderr line for every Triage key the stand-in ever held.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import http from 'node:http';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
import { startBroker } from '../src/runtime.mjs';
|
||||
import { busOrganization } from '../src/organization-context.mjs';
|
||||
import { keyPaths, readKey } from '../src/triage-key.mjs';
|
||||
import { Registry } from '../../seat/src/session.mjs';
|
||||
import { loadBusiness } from '../../organization/src/organization-config.mjs';
|
||||
import { resolveInstance } from '../../organization/src/resolve.mjs';
|
||||
import { businessDoc, rolesCopy, systemFor, writeJson } from '../../organization/tests/helpers.mjs';
|
||||
|
||||
const NOW = Date.parse('2026-11-18T12:00:00.000Z');
|
||||
const MIN = 60_000, HOUR = 60 * MIN, DAY = 24 * HOUR;
|
||||
const MANAGE = 'fx1873-s4-manage';
|
||||
const KEY_PATH = '/v8/management/config/access/api-keys';
|
||||
const sha = s => createHash('sha256').update(s).digest('hex');
|
||||
const tag = dir => sha(dir).slice(0, 12);
|
||||
const id = key => sha(key).slice(0, 12);
|
||||
const b64 = () => randomBytes(32).toString('base64url');
|
||||
const code = name => e => e.code === name;
|
||||
|
||||
// Every Triage key the stand-in ever held, and every text a bus produced, across the file.
|
||||
const SEEN = new Set();
|
||||
const TEXTS = [];
|
||||
function leaks(text) {
|
||||
for (const key of SEEN) {
|
||||
const secret = key.slice(-43);
|
||||
if (text.includes(key) || text.includes(secret)) return key.slice(0, 8);
|
||||
for (let i = 0; i + 8 <= secret.length; i++) if (text.includes(secret.slice(i, i + 8))) return key.slice(0, 8);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Every stderr line while the test runs: the bus's model-proxy receipt lines go there.
|
||||
function captureStderr(t) {
|
||||
const write = process.stderr.write, lines = [];
|
||||
process.stderr.write = (chunk, ...rest) => { lines.push(String(chunk)); return write.call(process.stderr, chunk, ...rest); };
|
||||
t.after(() => { process.stderr.write = write; TEXTS.push(lines.join('')); });
|
||||
return lines;
|
||||
}
|
||||
|
||||
async function instance(t) {
|
||||
const root = fs.realpathSync(fs.mkdtempSync(join(tmpdir(), 'tkb-')));
|
||||
const configDir = join(root, 'config');
|
||||
fs.mkdirSync(join(configDir, 'secrets'), { recursive: true, mode: 0o700 });
|
||||
fs.chmodSync(configDir, 0o700); fs.chmodSync(join(configDir, 'secrets'), 0o700);
|
||||
fs.writeFileSync(join(configDir, 'secrets', 'proxy-management.key'), MANAGE, { mode: 0o600 });
|
||||
fs.mkdirSync(join(root, 'proxy'), { mode: 0o700 });
|
||||
const sentinel = `msk-${tag(configDir)}-sentinel-${b64()}`, orgKey = `mso-${tag(configDir)}-other-key-${b64()}`;
|
||||
// triagePuts counts the writes that changed a Triage name; the organization buses' own key
|
||||
// reconcile writes the list too, and those do not count.
|
||||
const state = { keys: [sentinel, 'foreign-client-key', orgKey], puts: [], triagePuts: 0 };
|
||||
const triageNames = list => JSON.stringify(list.filter(k => k.startsWith('mst-')).sort());
|
||||
const server = http.createServer(async (req, res) => {
|
||||
if (req.url !== KEY_PATH) {
|
||||
const key = req.headers.authorization?.slice(7);
|
||||
res.writeHead(state.keys.includes(key) ? 200 : 401); res.end('{}'); return;
|
||||
}
|
||||
if (req.headers.authorization !== `Bearer ${MANAGE}`) { res.writeHead(401); res.end('{}'); return; }
|
||||
if (req.method === 'GET') { res.end(JSON.stringify(state.keys)); return; }
|
||||
const chunks = []; for await (const c of req) chunks.push(c);
|
||||
const next = JSON.parse(Buffer.concat(chunks));
|
||||
for (const k of next) if (/^mst-.*-[A-Za-z0-9_-]{43}$/.test(k)) SEEN.add(k);
|
||||
state.puts.push(next);
|
||||
if (triageNames(next) !== triageNames(state.keys)) state.triagePuts++;
|
||||
state.keys = next;
|
||||
res.end('{"status":"ok"}');
|
||||
});
|
||||
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||
const port = server.address().port;
|
||||
const closers = [];
|
||||
t.after(async () => {
|
||||
for (const close of closers.reverse()) await close().catch(() => {});
|
||||
server.closeAllConnections(); await new Promise(resolve => server.close(resolve));
|
||||
const receipts = join(root, 'proxy', 'mosaic-keys.receipts.jsonl');
|
||||
if (fs.existsSync(receipts)) TEXTS.push(fs.readFileSync(receipts, 'utf8'));
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
});
|
||||
const rolesDir = rolesCopy(root), system = systemFor(root);
|
||||
const f = {
|
||||
root, configDir, port, state, closers, rolesDir, system, sentinel, orgKey, clock: { now: NOW },
|
||||
prefix: `mst-${tag(configDir)}-triage-`,
|
||||
file: keyPaths(configDir).file,
|
||||
record: () => readKey(configDir),
|
||||
triageKeys: () => f.state.keys.filter(k => k.startsWith(f.prefix)),
|
||||
receipts: () => {
|
||||
const p = join(root, 'proxy', 'mosaic-keys.receipts.jsonl');
|
||||
return fs.existsSync(p) ? fs.readFileSync(p, 'utf8').trim().split('\n').filter(Boolean).map(JSON.parse) : [];
|
||||
},
|
||||
// A real organization bus on this instance. `proxy: false` starts it with no proxy settings, as
|
||||
// a bus on an instance with no proxy.json does.
|
||||
async bus(orgId, { proxy = true, triageKeys = {} } = {}) {
|
||||
const doc = businessDoc(root, orgId);
|
||||
doc.plugins = { ...doc.plugins, harness: ['pi'] };
|
||||
writeJson(join(configDir, 'businesses', `${orgId}.json`), doc);
|
||||
const business = loadBusiness(orgId, { dir: configDir, rolesDir }), resolved = {};
|
||||
for (const name of Object.keys(business.roles)) resolved[name] = resolveInstance({ system, business, instance: name });
|
||||
const dataRoot = join(root, `${orgId}-bus`);
|
||||
fs.mkdirSync(dataRoot, { mode: 0o700 }); new Registry(dataRoot);
|
||||
const bus = await startBroker({ dataRoot, businesses: { [orgId]: busOrganization(business, resolved) },
|
||||
organization: { dir: configDir, rolesDir, system }, ...(proxy ? { proxy: { port, binary: '/fixture-unused' } } : {}),
|
||||
org: { id: orgId }, instanceDataRoot: root, instanceConfigDir: configDir,
|
||||
triageKeys: { intervalMs: 3_600_000, now: () => f.clock.now, ...triageKeys } });
|
||||
closers.push(() => bus.close());
|
||||
const cap = bus.broker.bindHuman({ business: orgId, human: business.human ?? doc.human, via: 'cli', outsideAgent: true });
|
||||
const ask = async (verb, args) => {
|
||||
try { const answer = await bus.broker.request(cap, { verb, args }); TEXTS.push(JSON.stringify(answer)); return answer; }
|
||||
catch (error) { TEXTS.push(JSON.stringify({ code: error.code, reason: error.reason, message: error.message, nextStep: error.nextStep })); throw error; }
|
||||
};
|
||||
const sweep = async () => { const r = await bus.triageKeySweep(); TEXTS.push(JSON.stringify(r)); return r; };
|
||||
return { bus, ask, sweep, dataRoot };
|
||||
},
|
||||
};
|
||||
return f;
|
||||
}
|
||||
|
||||
// Every byte under a bus's data root (its store, with events and refusals, and its logs), as text.
|
||||
function eventsText(b, called = true) {
|
||||
const out = [];
|
||||
const walk = dir => {
|
||||
for (const name of fs.readdirSync(dir)) {
|
||||
const p = join(dir, name), st = fs.lstatSync(p);
|
||||
if (st.isDirectory()) walk(p);
|
||||
else if (st.isFile()) out.push(fs.readFileSync(p).toString('latin1'));
|
||||
}
|
||||
};
|
||||
walk(b.dataRoot);
|
||||
if (called) assert.ok(out.join('').includes('proxy.triage.key'), 'the store holds the verb\'s events');
|
||||
return out.join('\n');
|
||||
}
|
||||
|
||||
test('ensure issues one key, a second ensure keeps it, and both answers carry ids only', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha');
|
||||
const first = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.equal(first.action, 'issued');
|
||||
assert.equal(f.triageKeys().length, 1);
|
||||
const record = f.record();
|
||||
assert.equal(first.keyId, record.keyId);
|
||||
assert.equal(first.keyId, id(f.triageKeys()[0]));
|
||||
assert.deepEqual(first.addedKeyIds, [record.keyId]);
|
||||
assert.equal(fs.statSync(f.file).mode & 0o777, 0o600);
|
||||
const second = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.deepEqual(second, { op: 'ensure', action: 'held', addedKeyIds: [], removedKeyIds: [], keyId: record.keyId, rotateBy: record.rotateBy });
|
||||
assert.equal(f.triageKeys().length, 1, 'ensure twice gives one key');
|
||||
assert.equal(f.record().key, record.key);
|
||||
// Through a second riding bus on the same instance: still the one key.
|
||||
const b = await f.bus('beta');
|
||||
assert.equal((await b.ask('proxy.triage.key', { op: 'ensure' })).action, 'held');
|
||||
assert.equal(f.triageKeys().length, 1);
|
||||
assert.equal(f.state.triagePuts, 1, 'only the first ensure wrote the proxy list');
|
||||
// The receipt names the change by op and ids.
|
||||
const receipts = f.receipts().filter(r => r.op?.startsWith('triage.'));
|
||||
assert.deepEqual(receipts.map(r => [r.op, r.addedKeyIds, r.removedKeyIds]), [['triage.key.ensure', [record.keyId], []]]);
|
||||
for (const text of [eventsText(a), eventsText(b)]) assert.equal(leaks(text), null);
|
||||
});
|
||||
|
||||
test('ensure replaces a key the proxy lost, and revoke removes every Triage name and the file', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha');
|
||||
const issued = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
// The proxy loses the key (removed by hand): ensure issues a new one and names the old as lost.
|
||||
f.state.keys = f.state.keys.filter(k => !k.startsWith(f.prefix));
|
||||
const replaced = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.equal(replaced.action, 'replaced');
|
||||
assert.notEqual(replaced.keyId, issued.keyId);
|
||||
assert.equal(f.triageKeys().length, 1);
|
||||
// An orphan in the namespace (a crash left it) goes with revoke too.
|
||||
const orphan = `${f.prefix}${b64()}`; SEEN.add(orphan);
|
||||
f.state.keys.push(orphan);
|
||||
const revoked = await a.ask('proxy.triage.key', { op: 'revoke' });
|
||||
assert.equal(revoked.action, 'revoked');
|
||||
assert.deepEqual(revoked.removedKeyIds.sort(), [replaced.keyId, id(orphan)].sort());
|
||||
assert.equal(f.triageKeys().length, 0);
|
||||
assert.equal(fs.existsSync(f.file), false);
|
||||
assert.equal(f.record(), null);
|
||||
// Revoke again: nothing to do, nothing written.
|
||||
const puts = f.state.triagePuts;
|
||||
assert.deepEqual(await a.ask('proxy.triage.key', { op: 'revoke' }), { op: 'revoke', action: 'none', addedKeyIds: [], removedKeyIds: [] });
|
||||
assert.equal(f.state.triagePuts, puts);
|
||||
// Namespace guard: the sentinel, the organization key and the foreign key are still there.
|
||||
for (const k of [f.sentinel, f.orgKey, 'foreign-client-key']) assert.ok(f.state.keys.includes(k), k.slice(0, 4));
|
||||
assert.equal(leaks(eventsText(a)), null);
|
||||
});
|
||||
|
||||
test('a bus that does not ride the owner\'s proxy refuses triage-no-riding-bus, and its sweep does nothing', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const loose = await f.bus('loose', { proxy: false });
|
||||
await assert.rejects(loose.ask('proxy.triage.key', { op: 'ensure' }), e => e.code === 'triage-no-riding-bus'
|
||||
&& /^Triage needs one organization that uses the owner's models\./.test(e.nextStep));
|
||||
await assert.rejects(loose.ask('proxy.triage.key', { op: 'revoke' }), code('triage-no-riding-bus'));
|
||||
assert.equal(f.state.triagePuts, 0);
|
||||
// With a key issued through a riding bus and its rotation due, the loose bus still does nothing.
|
||||
const a = await f.bus('alpha');
|
||||
await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
f.clock.now += 31 * DAY;
|
||||
assert.deepEqual(await loose.sweep(), { action: 'none' });
|
||||
assert.equal(f.state.triagePuts, 1);
|
||||
});
|
||||
|
||||
test('the sweep rotates when rotateBy comes, once across two buses that wake together, and again a rotation later', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha'), b = await f.bus('beta');
|
||||
const issued = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
// Not due yet: neither bus touches the proxy.
|
||||
assert.deepEqual([await a.sweep(), await b.sweep()], [{ action: 'none' }, { action: 'none' }]);
|
||||
assert.equal(f.state.triagePuts, 1);
|
||||
f.clock.now = Date.parse(`${issued.rotateBy}T00:00:00Z`);
|
||||
const both = await Promise.all([a.sweep(), b.sweep()]);
|
||||
assert.deepEqual(both.map(r => r.action).sort(), ['none', 'rotated']);
|
||||
assert.equal(f.state.triagePuts, 2, 'two buses rotated once');
|
||||
const rotated = both.find(r => r.action === 'rotated');
|
||||
const record = f.record();
|
||||
assert.equal(rotated.keyId, record.keyId);
|
||||
assert.deepEqual(record.retiring.map(r => r.keyId), [issued.keyId]);
|
||||
assert.equal(f.triageKeys().length, 2, 'the old key stays live through its grace');
|
||||
// Inside the grace both buses leave it; at retireAt plus the margin one of them retires it.
|
||||
const retireAt = Date.parse(record.retiring[0].retireAt);
|
||||
f.clock.now = retireAt + 10_000;
|
||||
assert.deepEqual((await Promise.all([a.sweep(), b.sweep()])).map(r => r.action), ['none', 'none']);
|
||||
f.clock.now = retireAt + 30_000;
|
||||
const retired = await Promise.all([a.sweep(), b.sweep()]);
|
||||
assert.deepEqual(retired.map(r => r.action).sort(), ['none', 'retired']);
|
||||
assert.equal(f.state.triagePuts, 3);
|
||||
assert.deepEqual(f.triageKeys().map(id), [record.keyId]);
|
||||
assert.deepEqual(f.record().retiring, []);
|
||||
// A second rotation 30 days on: again once.
|
||||
f.clock.now = Date.parse(`${record.rotateBy}T00:00:00Z`) + HOUR;
|
||||
const again = await Promise.all([b.sweep(), a.sweep()]);
|
||||
assert.deepEqual(again.map(r => r.action).sort(), ['none', 'rotated']);
|
||||
assert.equal(f.state.triagePuts, 4);
|
||||
assert.equal(f.triageKeys().length, 2);
|
||||
// Namespace guard across every sweep.
|
||||
for (const k of [f.sentinel, f.orgKey, 'foreign-client-key']) assert.ok(f.state.keys.includes(k), k.slice(0, 4));
|
||||
const ops = f.receipts().filter(r => r.op?.startsWith('triage.')).map(r => r.op);
|
||||
assert.deepEqual(ops, ['triage.key.ensure', 'triage.key.rotate', 'triage.key.retire', 'triage.key.rotate']);
|
||||
for (const bus of [a, b]) assert.equal(leaks(eventsText(bus, bus === a)), null);
|
||||
});
|
||||
|
||||
test('the sweep never issues the first key, and a sweep refusal is a receipt line with a code only', async t => {
|
||||
const lines = captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha');
|
||||
f.clock.now += 400 * DAY;
|
||||
assert.deepEqual(await a.sweep(), { action: 'none' });
|
||||
assert.equal(f.state.triagePuts, 0);
|
||||
assert.equal(f.record(), null);
|
||||
// A due rotation on a proxy with no sentinel (it may be another proxy): refused, nothing written.
|
||||
await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
f.state.keys = f.state.keys.filter(k => k !== f.sentinel);
|
||||
f.clock.now += 31 * DAY;
|
||||
const r = await a.sweep();
|
||||
assert.deepEqual(r, { action: 'refused', code: 'proxy-not-configured' });
|
||||
assert.equal(f.state.triagePuts, 1);
|
||||
const refusal = lines.join('').split('\n').filter(l => l.includes('triage.key.sweep-refused'));
|
||||
assert.equal(refusal.length, 1);
|
||||
assert.deepEqual(JSON.parse(refusal[0].replace(/^model-proxy: /, '')), { action: 'triage.key.sweep-refused', code: 'proxy-not-configured', reason: 'triage-key-proxy-open' });
|
||||
// The same refusal on the next ticks is not logged again within the hour; after it, once more.
|
||||
const refusals = () => lines.join('').split('\n').filter(l => l.includes('triage.key.sweep-refused')).length;
|
||||
for (let i = 0; i < 3; i++) { f.clock.now += 30_000; assert.equal((await a.sweep()).code, 'proxy-not-configured'); }
|
||||
assert.equal(refusals(), 1);
|
||||
f.clock.now += HOUR;
|
||||
await a.sweep();
|
||||
assert.equal(refusals(), 2);
|
||||
});
|
||||
|
||||
test('a revoke stopped between the proxy write and the file is finished by the next revoke; ensure after a stopped revoke replaces the key', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
let stage = null;
|
||||
const a = await f.bus('alpha', { triageKeys: { hook: s => { if (s === stage) throw new Error(`stop at ${s}`); } } });
|
||||
await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
stage = 'write';
|
||||
await assert.rejects(a.ask('proxy.triage.key', { op: 'revoke' }), e => e.code === 'storage-unavailable' && !/stop at/.test(e.message));
|
||||
assert.equal(f.triageKeys().length, 0, 'the proxy write happened');
|
||||
assert.ok(f.record(), 'the file stayed');
|
||||
stage = null;
|
||||
// ensure now sees a file whose key the proxy lacks: a new key, the old named lost.
|
||||
const replaced = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.equal(replaced.action, 'replaced');
|
||||
stage = 'rename';
|
||||
await assert.rejects(a.ask('proxy.triage.key', { op: 'revoke' }), code('storage-unavailable'));
|
||||
stage = null;
|
||||
assert.equal((await a.ask('proxy.triage.key', { op: 'revoke' })).action, 'revoked');
|
||||
assert.equal(f.record(), null);
|
||||
assert.equal(f.triageKeys().length, 0);
|
||||
});
|
||||
|
||||
test('no answer, event, receipt line or stderr line in this file holds any Triage key the stand-in saw', () => {
|
||||
assert.ok(SEEN.size >= 8, `only ${SEEN.size} keys were seen`);
|
||||
assert.ok(TEXTS.length >= 30, `only ${TEXTS.length} texts`);
|
||||
for (const text of TEXTS) assert.equal(leaks(text), null);
|
||||
});
|
||||
@@ -0,0 +1,125 @@
|
||||
// proxy-scope slice 4 (#1873): the bus verb proxy.triage.key at the broker. Only the instance
|
||||
// owner's path-bound proof (a human cap from the cli transport, outside any agent) reaches the
|
||||
// runtime's hook; an agent, a tester, a Console proof, a grant holder and an organization owner get
|
||||
// human-required, a reader read-only. The answer and the event carry key ids only, and a hook
|
||||
// failure that is not a bus refusal becomes storage-unavailable with no text of its own.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { Broker, BusError } from '../src/broker.mjs';
|
||||
import { Store } from '../src/store.mjs';
|
||||
import { REFUSALS } from '../src/refusals.mjs';
|
||||
|
||||
const role = { authority: { withinRole: ['message.send'], crossRole: [] } };
|
||||
const code = name => error => error.code === name;
|
||||
const SUBJECT = { provider: 'fixture', iss: 'https://sso.invalid', sub: 'owner-1' };
|
||||
// A Triage key as triage-key.mjs makes them, so a leak check has a real-looking value to look for.
|
||||
const KEY = `mst-0123456789ab-triage-${'Q'.repeat(20)}xY_-${'z'.repeat(19)}`;
|
||||
|
||||
function fixture(t, { proxyTriageKey = null } = {}) {
|
||||
const store = new Store(fs.mkdtempSync(join(tmpdir(), 'tkv-bus-')));
|
||||
t.after(() => store.close());
|
||||
const b = new Broker({ store, proxyTriageKey, businesses: { demo: { id: 'demo', human: 'owner', arbiters: { technical: 'chief', delivery: 'chief' }, vars: { 'bus.tester': true }, roles: { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery'], crossRole: [] } }, coder: role } } } });
|
||||
return {
|
||||
store, b,
|
||||
human: b.bindHuman({ business: 'demo', human: 'owner', via: 'cli', outsideAgent: true }),
|
||||
tester: b.bindTester({ business: 'demo' }), reader: b.bindReader({ business: 'demo' }),
|
||||
agent: b.bindLaunch({ business: 'demo', role: 'coder', run: 'tkv-run', harness: 'pi' }),
|
||||
// The owner's Console proof, a granted account that holds org.hire, and an organization owner
|
||||
// who is not the instance owner: none is the path-bound cli proof.
|
||||
console: b.bindConsole({ business: 'demo', human: 'owner', subject: SUBJECT }),
|
||||
grant: b.bindHireConsole({ business: 'demo', actor: 'grantee', subject: { ...SUBJECT, sub: 'grantee-1' }, rights: ['org.hire', 'decide'] }),
|
||||
orgOwner: b.bindHireConsole({ business: 'demo', actor: 'orgowner', subject: { ...SUBJECT, sub: 'orgowner-1' }, rights: ['org.hire'], owner: true }),
|
||||
};
|
||||
}
|
||||
const events = store => JSON.stringify(store.all('SELECT body FROM events'));
|
||||
const bodies = store => store.all('SELECT body FROM events').map(r => JSON.parse(r.body));
|
||||
|
||||
test('proxy.triage.key refuses an agent, a tester, a Console proof, a grant holder and an organization owner with human-required, and a reader with read-only', async t => {
|
||||
let calls = 0;
|
||||
const f = fixture(t, { proxyTriageKey: async () => { calls++; return { action: 'issued', keyId: '0123456789ab', rotateBy: '2026-12-19', addedKeyIds: ['0123456789ab'], removedKeyIds: [] }; } });
|
||||
for (const op of ['ensure', 'revoke']) {
|
||||
for (const [name, cap] of [['agent', f.agent], ['tester', f.tester], ['console', f.console], ['grant', f.grant], ['orgOwner', f.orgOwner]])
|
||||
assert.throws(() => f.b.request(cap, { verb: 'proxy.triage.key', args: { op } }), code('human-required'), `${name} ${op}`);
|
||||
assert.throws(() => f.b.request(f.reader, { verb: 'proxy.triage.key', args: { op } }), code('read-only'));
|
||||
}
|
||||
assert.equal(calls, 0);
|
||||
// The same verb runs for the owner's cli proof.
|
||||
const answer = await f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } });
|
||||
assert.equal(answer.action, 'issued');
|
||||
assert.equal(calls, 1);
|
||||
});
|
||||
|
||||
test('proxy.triage.key takes op ensure or revoke and nothing else, and a bus with no hook is not-configured', async t => {
|
||||
let calls = 0;
|
||||
const f = fixture(t, { proxyTriageKey: async () => { calls++; return { action: 'none', addedKeyIds: [], removedKeyIds: [] }; } });
|
||||
for (const args of [{}, { op: 'rotate' }, { op: 'issue' }, { op: 'ensure', key: KEY }, { op: 'revoke', force: true }, { op: ['ensure'] }])
|
||||
assert.throws(() => f.b.request(f.human, { verb: 'proxy.triage.key', args }), code('invalid-request'), JSON.stringify(args));
|
||||
assert.throws(() => f.b.request(f.human, { verb: 'proxy.triage.key' }), code('invalid-request'));
|
||||
assert.equal(calls, 0);
|
||||
assert.equal(events(f.store).includes(KEY), false);
|
||||
assert.equal(events(f.store).includes(KEY.slice(-43)), false);
|
||||
const g = fixture(t);
|
||||
assert.throws(() => g.b.request(g.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } }), code('not-configured'));
|
||||
});
|
||||
|
||||
test('the answer and the event of each op carry ids only, even when the hook returns more', async t => {
|
||||
const seen = [];
|
||||
const f = fixture(t, { proxyTriageKey: async op => {
|
||||
seen.push(op);
|
||||
// A hook that returns the key beside the ids: the broker copies the named fields only.
|
||||
return op === 'ensure'
|
||||
? { action: 'issued', keyId: '0123456789ab', rotateBy: '2026-12-19', addedKeyIds: ['0123456789ab'], removedKeyIds: ['ba9876543210'], key: KEY, old: { key: KEY } }
|
||||
: { action: 'revoked', addedKeyIds: [], removedKeyIds: ['0123456789ab'], key: KEY };
|
||||
} });
|
||||
const ensure = await f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } });
|
||||
assert.deepEqual(ensure, { op: 'ensure', action: 'issued', addedKeyIds: ['0123456789ab'], removedKeyIds: ['ba9876543210'], keyId: '0123456789ab', rotateBy: '2026-12-19' });
|
||||
const revoke = await f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'revoke' } });
|
||||
assert.deepEqual(revoke, { op: 'revoke', action: 'revoked', addedKeyIds: [], removedKeyIds: ['0123456789ab'] });
|
||||
assert.deepEqual(seen, ['ensure', 'revoke']);
|
||||
assert.deepEqual(bodies(f.store).filter(b => b.call === 'proxy.triage.key'), [{ call: 'proxy.triage.key', ...ensure }, { call: 'proxy.triage.key', ...revoke }]);
|
||||
for (const text of [JSON.stringify(ensure), JSON.stringify(revoke), events(f.store)]) {
|
||||
assert.equal(text.includes(KEY), false);
|
||||
assert.equal(text.includes(KEY.slice(-43)), false);
|
||||
}
|
||||
});
|
||||
|
||||
test('an answer the broker cannot vouch for refuses proxy-client-response and records no allowed event', async t => {
|
||||
const answers = [
|
||||
null, { action: 'issued' },
|
||||
{ action: 'deleted', keyId: '0123456789ab', addedKeyIds: [], removedKeyIds: [] },
|
||||
{ action: 'issued', keyId: KEY, addedKeyIds: [], removedKeyIds: [] },
|
||||
{ action: 'issued', keyId: '0123456789ab', addedKeyIds: [KEY], removedKeyIds: [] },
|
||||
{ action: 'held', keyId: '0123456789ab', addedKeyIds: [], removedKeyIds: 'none' },
|
||||
];
|
||||
let next = 0;
|
||||
const f = fixture(t, { proxyTriageKey: async () => answers[next++] });
|
||||
for (const answer of answers)
|
||||
await assert.rejects(f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } }), code('proxy-client-response'), JSON.stringify(answer));
|
||||
await assert.rejects(Promise.resolve().then(() => fixture(t, { proxyTriageKey: async () => ({ action: 'issued', addedKeyIds: [], removedKeyIds: [] }) }))
|
||||
.then(g => g.b.request(g.human, { verb: 'proxy.triage.key', args: { op: 'revoke' } })), code('proxy-client-response'));
|
||||
assert.equal(bodies(f.store).some(b => b.call === 'proxy.triage.key'), false);
|
||||
assert.equal(events(f.store).includes(KEY.slice(-43)), false);
|
||||
});
|
||||
|
||||
test('a bus refusal from the hook passes through with its guidance; any other failure is storage-unavailable and names nothing from the error', async t => {
|
||||
const f = fixture(t, { proxyTriageKey: async op => { throw op === 'ensure' ? new BusError('triage-no-riding-bus') : Object.assign(new Error(`boom ${KEY} /home/x`), { code: 'EACCES' }); } });
|
||||
await assert.rejects(f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } }), e => e.code === 'triage-no-riding-bus'
|
||||
&& /Triage needs one organization that uses the owner's models/.test(e.nextStep ?? REFUSALS['triage-no-riding-bus'].nextStep));
|
||||
await assert.rejects(f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'revoke' } }), e => e.code === 'storage-unavailable'
|
||||
&& !JSON.stringify({ ...e, message: e.message }).includes(KEY.slice(-43)) && !String(e.message).includes('/home/x'));
|
||||
assert.equal(events(f.store).includes(KEY.slice(-43)), false);
|
||||
});
|
||||
|
||||
test('the two slice 4 codes have refusal-table entries with a next step and no absolute path', () => {
|
||||
for (const c of ['triage-no-riding-bus', 'triage-key-rotated']) {
|
||||
const entry = REFUSALS[c];
|
||||
assert.ok(entry, c);
|
||||
assert.equal(typeof entry.nextStep, 'string', c);
|
||||
assert.ok(entry.nextStep.length > 20, c);
|
||||
assert.equal(/\/(home|mnt|srv|tmp)\//.test(entry.nextStep), false, c);
|
||||
}
|
||||
assert.match(REFUSALS['triage-no-riding-bus'].nextStep, /^Triage needs one organization that uses the owner's models\./);
|
||||
});
|
||||
@@ -677,15 +677,18 @@ async function bus(rest, io, deps) {
|
||||
const priorBoot = readBootStatus(org.root);
|
||||
try { recordBootOk(org.root, { business: businessId, file }); } catch { /* status is advisory */ }
|
||||
const binding = readNotifyConfig(org.root, businessId);
|
||||
const host = await startHost({ boot, business: businessId, notifier: binding ? { binding } : null, refuseStale: true, options: { devLocalOwner: Boolean(instance), pm: parsed.flags.has("--pm"), noLaunch: !launches } }).catch((e) => {
|
||||
// A dev fixture serves only the organizations that use it; their launches carry no provider credential.
|
||||
const orgDoc = instance ? JSON.parse(readFileSync(file, "utf8")) : undefined;
|
||||
const launchEnv = instance ? fixtureLaunchEnv(orgDoc, io.env) : io.env;
|
||||
// #1873 W2: the harness watch runs in the host with the env sessions launch from, unless the
|
||||
// host launches nothing (--no-launch). deps.harnessWatch replaces its options in tests.
|
||||
const harnessWatch = launches ? (deps.harnessWatch === undefined ? {} : deps.harnessWatch) : null;
|
||||
const host = await startHost({ boot, business: businessId, notifier: binding ? { binding } : null, refuseStale: true, options: { devLocalOwner: Boolean(instance), pm: parsed.flags.has("--pm"), noLaunch: !launches }, harnessWatch: harnessWatch ? { env: launchEnv, ...harnessWatch } : null }).catch((e) => {
|
||||
// org-defscan: the broker refused a definition that holds a credential it holds; the load was not good after all.
|
||||
if (e.code === "definition-holds-credential") try { recordBootRefused(org.root, { business: businessId, file, reason: e.message, prior: priorBoot }); } catch { /* the refusal still throws */ }
|
||||
throw e;
|
||||
});
|
||||
try {
|
||||
// A dev fixture serves only the organizations that use it; their launches carry no provider credential.
|
||||
const orgDoc = instance ? JSON.parse(readFileSync(file, "utf8")) : undefined;
|
||||
const launchEnv = instance ? fixtureLaunchEnv(orgDoc, io.env) : io.env;
|
||||
// U4a (#1873): agents get the task tools when the bus serves task verbs for the organization: a
|
||||
// Vikunja tracker in the boot config, or the built-in store, which an organization with no
|
||||
// tracker gets (the bus runtime makes the same choice, runtime.mjs trackerOf).
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
// #1873 harness watch, slice W2 (reports/harness-watch-design.md section 1, "How often" and "Where
|
||||
// results are kept"): the bus host's timer over the W1 watch (packages/plugins/src/harness-watch.mjs).
|
||||
// It runs in the host process with the env sessions launch from.
|
||||
//
|
||||
// at start one full pass
|
||||
// every statMs (5 minutes) a stat pass: the stat key of every harness module the loader gives
|
||||
// (its optional watchKey({ env }), else its install roots), and a full pass when
|
||||
// the key is not the last full pass's, or when dayMs (a day) has gone by since it
|
||||
// halted/invalid while processing is halted or invalid (processingStatus), no pass at all; the
|
||||
// first tick after it clears runs a full pass
|
||||
//
|
||||
// A harness module may ask for a stat pass now (packages/plugins/src/watch-request.mjs; claude-code's
|
||||
// preflight asks when it has no verdict for its binary): it runs at once, or right after the pass
|
||||
// running, and runs a full pass only when the key changed, as a tick does.
|
||||
//
|
||||
// One pass at a time: a tick that comes while a pass runs does nothing. A pass that throws is
|
||||
// logged, with no path in the line, and the timer goes on; its key is not kept, so the next tick
|
||||
// runs a full pass again. stop() clears the timer, aborts the pass running (the watch aborts the
|
||||
// inspect it is in, which stops what it started) and waits for it; nothing is written after.
|
||||
//
|
||||
// The host is the one writer of two files under <org dataRoot>/state/:
|
||||
// harness-watch.json { version: 1, checkedAt, harnesses: [record], refused, gone }: the current
|
||||
// record per harness, as the watch returns it but without surfaces (their
|
||||
// store is slice W6). Written whole to a temp file in the same directory, then
|
||||
// renamed over the last one, 0600.
|
||||
// harness-watch.jsonl one line per transition the watch reports, appended, never rewritten.
|
||||
// The watch redacts its records (no host path, no env value). Both files are checked again before
|
||||
// they are written: a line that would hold the scratch directory, the home directory, the data
|
||||
// root or an env value of six characters or more is not written, and the pass is logged as failed.
|
||||
// The timer adds no model call and no network call: the stat pass reads file metadata only, and a
|
||||
// full pass is the watch's (its modules run their binaries with no network where the host allows).
|
||||
import { appendFileSync, chmodSync, closeSync, fsyncSync, mkdirSync, mkdtempSync, openSync, readFileSync, renameSync, rmSync, writeSync } from "node:fs";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { loadInstalled } from "../../plugins/src/loader.mjs";
|
||||
import { watchHarnesses } from "../../plugins/src/harness-watch.mjs";
|
||||
import { statKey } from "../../plugins/src/stat-key.mjs";
|
||||
import { onWatchRequest } from "../../plugins/src/watch-request.mjs";
|
||||
import { processingStatus } from "../../processing-control/src/state.mjs";
|
||||
|
||||
export const STAT_MS = 5 * 60 * 1000;
|
||||
export const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
export const WATCH_VERSION = 1;
|
||||
export const watchFile = (dataRoot) => join(dataRoot, "state", "harness-watch.json");
|
||||
export const watchLog = (dataRoot) => join(dataRoot, "state", "harness-watch.jsonl");
|
||||
const VALUE_MIN = 6;
|
||||
|
||||
const realClock = Object.freeze({
|
||||
now: () => Date.now(),
|
||||
setInterval: (fn, ms) => setInterval(fn, ms),
|
||||
clearInterval: (t) => clearInterval(t),
|
||||
});
|
||||
|
||||
// The stat key of every harness the loader gives: per module, [name, its key]. A module's key is
|
||||
// its watchKey({ env }) when it has one, else the stat key of its installRoots({ env }), else null.
|
||||
// A module that comes or goes changes the key too. The key holds paths; it is compared in memory
|
||||
// only.
|
||||
export async function harnessKeys({ dataRoot, env }) {
|
||||
const loaded = await loadInstalled("harness", { dataRoot });
|
||||
const out = [];
|
||||
for (const { name, module } of loaded.modules) {
|
||||
let key = null;
|
||||
try {
|
||||
if (typeof module.watchKey === "function") key = module.watchKey({ env });
|
||||
else if (typeof module.installRoots === "function") key = statKey(module.installRoots({ env }));
|
||||
} catch (e) {
|
||||
key = ["error", typeof e?.code === "string" ? e.code : "threw"];
|
||||
}
|
||||
out.push([name, typeof key === "string" || Array.isArray(key) ? key : null]);
|
||||
}
|
||||
out.push(["refused", loaded.refused.map((r) => r?.name ?? null)]);
|
||||
return JSON.stringify(out);
|
||||
}
|
||||
|
||||
// Starts the timer and its first pass. Returns { stop, settled }: stop() ends it (a promise), and
|
||||
// settled() is the pass running, or a resolved promise, for tests and for stop.
|
||||
export function startHarnessWatch({
|
||||
dataRoot,
|
||||
env = process.env,
|
||||
log = () => {},
|
||||
clock = realClock,
|
||||
processing = () => processingStatus(dataRoot),
|
||||
watch = watchHarnesses,
|
||||
keys = () => harnessKeys({ dataRoot, env }),
|
||||
statMs = STAT_MS,
|
||||
dayMs = DAY_MS,
|
||||
timeoutMs = undefined,
|
||||
scratchRoot = () => tmpdir(),
|
||||
requests = onWatchRequest,
|
||||
} = {}) {
|
||||
if (typeof dataRoot !== "string" || !dataRoot) throw new TypeError("startHarnessWatch needs the organization's dataRoot");
|
||||
const control = new AbortController();
|
||||
let stopped = false;
|
||||
let running = null;
|
||||
let held = false;
|
||||
let again = false;
|
||||
let needFull = true;
|
||||
let lastKey = null;
|
||||
let lastFull = null;
|
||||
let last = readRecords(dataRoot);
|
||||
|
||||
async function pass() {
|
||||
let status;
|
||||
try { status = processing(); } catch { status = { invalid: true }; }
|
||||
if (status?.halted || status?.invalid) {
|
||||
if (!held) log("harness watch: processing is halted or invalid, so passes wait for it");
|
||||
held = true;
|
||||
return;
|
||||
}
|
||||
if (held) { held = false; needFull = true; }
|
||||
const key = await keys();
|
||||
if (stopped) return;
|
||||
const now = clock.now();
|
||||
if (!needFull && key === lastKey && lastFull !== null && now - lastFull < dayMs) return;
|
||||
const scratch = mkdtempSync(join(scratchRoot(), "mosaic-watch-"));
|
||||
try {
|
||||
const result = await watch({ last, env, scratch, dataRoot, now: () => new Date(clock.now()), signal: control.signal, ...(timeoutMs ? { timeoutMs } : {}) });
|
||||
if (stopped || result?.stopped) return;
|
||||
store(dataRoot, result, { env, scratch, at: new Date(now).toISOString() });
|
||||
last = result.records;
|
||||
lastKey = key;
|
||||
lastFull = now;
|
||||
needFull = false;
|
||||
} finally {
|
||||
rmSync(scratch, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
function tick() {
|
||||
if (stopped || running) return;
|
||||
running = pass()
|
||||
.catch((e) => { if (!stopped) log(`harness watch: a pass failed (${failure(e)}); the timer goes on`); })
|
||||
.finally(() => {
|
||||
running = null;
|
||||
if (again && !stopped) { again = false; tick(); }
|
||||
});
|
||||
}
|
||||
|
||||
// A request runs a tick now, or once right after the pass running.
|
||||
function request() {
|
||||
if (stopped) return;
|
||||
if (running) again = true;
|
||||
else tick();
|
||||
}
|
||||
|
||||
const timer = clock.setInterval(tick, statMs);
|
||||
timer?.unref?.();
|
||||
const unlisten = typeof requests === "function" ? requests(request) : null;
|
||||
tick();
|
||||
|
||||
return Object.freeze({
|
||||
settled: () => running ?? Promise.resolve(),
|
||||
async stop() {
|
||||
if (!stopped) {
|
||||
stopped = true;
|
||||
clock.clearInterval(timer);
|
||||
if (typeof unlisten === "function") unlisten();
|
||||
control.abort();
|
||||
}
|
||||
await (running ?? Promise.resolve());
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// What failed, without the error's text, which can name a host path.
|
||||
const failure = (e) => (typeof e?.code === "string" && /^[A-Za-z0-9_-]{1,40}$/.test(e.code) ? e.code : e?.name === "LeakError" ? "a state line held a host path or an env value" : "an error");
|
||||
|
||||
// The records of the last harness-watch.json, or none when it is missing or not this shape.
|
||||
function readRecords(dataRoot) {
|
||||
try {
|
||||
const doc = JSON.parse(readFileSync(watchFile(dataRoot), "utf8"));
|
||||
return doc?.version === WATCH_VERSION && Array.isArray(doc.harnesses) ? doc.harnesses.filter((r) => r && typeof r === "object" && typeof r.harness === "string") : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function store(dataRoot, result, { env, scratch, at }) {
|
||||
const records = (Array.isArray(result?.records) ? result.records : []).map(({ surfaces, ...rest }) => rest);
|
||||
const doc = { version: WATCH_VERSION, checkedAt: at, harnesses: records, refused: Array.isArray(result?.refused) ? result.refused : [], gone: Array.isArray(result?.gone) ? result.gone : [] };
|
||||
const transitions = Array.isArray(result?.transitions) ? result.transitions : [];
|
||||
const leaks = leakChecker({ env, scratch, dataRoot });
|
||||
if (leaks(doc) || transitions.some(leaks)) throw Object.assign(new Error("leak"), { name: "LeakError" });
|
||||
const text = `${JSON.stringify(doc, null, 2)}\n`;
|
||||
const lines = transitions.map((t) => `${JSON.stringify(t)}\n`).join("");
|
||||
const dir = join(dataRoot, "state");
|
||||
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
||||
const file = watchFile(dataRoot);
|
||||
const tmp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
||||
try {
|
||||
const fd = openSync(tmp, "wx", 0o600);
|
||||
try { writeSync(fd, text); fsyncSync(fd); } finally { closeSync(fd); }
|
||||
renameSync(tmp, file);
|
||||
} catch (e) {
|
||||
rmSync(tmp, { force: true });
|
||||
throw e;
|
||||
}
|
||||
chmodSync(file, 0o600);
|
||||
if (lines) appendFileSync(watchLog(dataRoot), lines, { mode: 0o600 });
|
||||
}
|
||||
|
||||
// Whether a value holds a path the host knows (the scratch directory, the home directory, the data
|
||||
// root) or an env value of VALUE_MIN characters or more, the host's or the launch env's, in any of
|
||||
// its strings. Names the watch sets itself are not text and are skipped: a harness or module name
|
||||
// (an env value may name the harness), a lookup step, a state, a fit, a via, a time or a digest.
|
||||
const NOT_TEXT = new Set(["harness", "gone", "name", "kept", "source", "code", "state", "fit", "via", "at", "checkedAt", "changedAt", "digest", "lastDigest"]);
|
||||
export function leakChecker({ env, scratch, dataRoot }) {
|
||||
const needles = new Set();
|
||||
const add = (v) => { if (typeof v === "string" && v.length >= VALUE_MIN) needles.add(v); };
|
||||
for (const p of [scratch, dataRoot, homedir()]) add(p);
|
||||
for (const source of [env ?? {}, process.env]) for (const v of Object.values(source)) add(v);
|
||||
const holds = (text) => { for (const n of needles) if (text.includes(n)) return true; return false; };
|
||||
const walk = (v, key) => {
|
||||
if (typeof v === "string") return !NOT_TEXT.has(key) && holds(v);
|
||||
if (Array.isArray(v)) return v.some((x) => walk(x, key));
|
||||
if (v && typeof v === "object") return Object.entries(v).some(([k, x]) => walk(x, k));
|
||||
return false;
|
||||
};
|
||||
return (value) => walk(value, null);
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { CliError } from "./errors.mjs";
|
||||
import { cmdlineOf, startTimeOf } from "../../seat/src/proc.mjs";
|
||||
import { startHarnessWatch } from "./harness-watch-host.mjs";
|
||||
|
||||
const BROKER = fileURLToPath(new URL("../../bus/src/process.mjs", import.meta.url));
|
||||
const NOTIFIER = fileURLToPath(new URL("./notifier-process.mjs", import.meta.url));
|
||||
@@ -175,7 +176,10 @@ export function watchChildren(children, onDeath) {
|
||||
// {binding, base?, pollMs?}; base and pollMs exist for the tests, and the
|
||||
// command line never sets them, nor requestTimeoutMs. Resolves once both
|
||||
// children are up.
|
||||
export async function startHost({ boot, business, notifier = null, options = null, refuseStale = false, bootTimeoutMs = BOOT_TIMEOUT_MS, requestTimeoutMs = REQUEST_TIMEOUT_MS, log = (l) => process.stderr.write(`mosaic-bus: ${l}\n`) }) {
|
||||
// harnessWatch (#1873 W2): { env, ...options } starts the harness watch's timer in this process
|
||||
// once the host is up (harness-watch-host.mjs), with env the environment sessions launch from; the
|
||||
// host's close stops it first. null (the default) starts none.
|
||||
export async function startHost({ boot, business, notifier = null, options = null, refuseStale = false, bootTimeoutMs = BOOT_TIMEOUT_MS, requestTimeoutMs = REQUEST_TIMEOUT_MS, log = (l) => process.stderr.write(`mosaic-bus: ${l}\n`), harnessWatch = null }) {
|
||||
const dataRoot = boot.dataRoot;
|
||||
// A host started before host.lock existed holds only host.json.
|
||||
const prior = readHostState(dataRoot);
|
||||
@@ -372,6 +376,11 @@ export async function startHost({ boot, business, notifier = null, options = nul
|
||||
log(`${name} exited (${signal ?? code}); stopping the host`);
|
||||
close(1);
|
||||
});
|
||||
// #1873 W2: the harness watch's timer, stopped before anything else when the host closes.
|
||||
if (harnessWatch) {
|
||||
const watch = startHarnessWatch({ log, ...harnessWatch, dataRoot });
|
||||
hooks.unshift(() => watch.stop());
|
||||
}
|
||||
|
||||
// #1873 CB3: the copies the broker's boot proved live, names only; anything malformed counts as none.
|
||||
const copyBound = {};
|
||||
|
||||
@@ -215,12 +215,19 @@ const relayed = (failed) => failed && (typeof failed.message === "string" ? { ..
|
||||
// and names the module rather than escaping the host. What it threw is named by its system code
|
||||
// or its class, never its message, which the host didn't write (#1873).
|
||||
function call(name, what, fn) {
|
||||
try {
|
||||
return fn();
|
||||
} catch (e) {
|
||||
const failed = (e) => {
|
||||
const thrown = quietMessage(e, "a host file") ?? `threw ${/^[A-Za-z][A-Za-z0-9]{0,39}$/.test(String(e?.name)) ? e.name : "a value"} without a system code`;
|
||||
throw new Refusal("harness-failed", `harness ${name} ${what}: ${thrown}`);
|
||||
};
|
||||
let out;
|
||||
try {
|
||||
out = fn();
|
||||
} catch (e) {
|
||||
failed(e);
|
||||
}
|
||||
// #1873 pi-unpin re-check 7: a module may answer with a promise (pi's preflight runs its fit
|
||||
// check as child processes). The caller awaits it; a rejection is refused as a throw is.
|
||||
return typeof out?.then === "function" ? Promise.resolve(out).then(undefined, failed) : out;
|
||||
}
|
||||
|
||||
// host: startHost()'s handle. tracker: true when the broker has task verbs for
|
||||
@@ -454,14 +461,11 @@ export function createLauncher({
|
||||
for (const m of [primary, fallback].filter(Boolean)) {
|
||||
if (!on.includes(m.harness)) throw new Refusal("harness-off", `harness ${m.harness} isn't switched on in plugins.harness`);
|
||||
}
|
||||
// One harness through a fresh loader, so a refusal never leaves an earlier load's module in use.
|
||||
const harnessOf = async (m) => {
|
||||
const loader = makeLoader({ dataRoot:system.instanceRoot ?? dataRoot, log: (entry) => record({ ...entry, instance }) });
|
||||
const { refused } = await loader.load({ harness: [m.harness] });
|
||||
const { refused, module } = await freshHarness(m, (entry) => record({ ...entry, instance }));
|
||||
// #1873: a loader message can quote the module's path or its load error; only a
|
||||
// missing-export one, which names the export, is relayed. Any other refusal names its code.
|
||||
if (refused.length) throw new Refusal("harness-refused", `harness ${m.harness}: ${refused.map((r) => r.code === "missing-export" ? `${r.code}: ${relay(r.message)}` : String(r.code)).join("; ")}`);
|
||||
const module = loader.modules("harness")[0];
|
||||
const levels = call(m.harness, "levels", () => module.levels());
|
||||
if (!levels.includes(m.thinking)) throw new Refusal("thinking-unsupported", `harness ${m.harness} takes thinking ${levels.join(", ")}, not ${m.thinking}`);
|
||||
return module;
|
||||
@@ -556,7 +560,7 @@ export function createLauncher({
|
||||
const pickRoute = async (proxyOf) => {
|
||||
let sessionProxy = await proxyOf(primary);
|
||||
// #1873: a preflight's message is the module's own text, relayed with host paths replaced.
|
||||
const failed = relayed(call(primary.harness, "preflight", () => harness.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
const failed = relayed(await call(primary.harness, "preflight", () => harness.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
if (failed) {
|
||||
if (!fallback) throw new Refusal(failed.code, failed.message);
|
||||
// The fallback only on a failed primary preflight, and only at launch (row 65). Any refusal of
|
||||
@@ -566,7 +570,7 @@ export function createLauncher({
|
||||
throw e;
|
||||
});
|
||||
sessionProxy = await proxyOf(fallback);
|
||||
const also = relayed(call(fallback.harness, "preflight", () => second.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
const also = relayed(await call(fallback.harness, "preflight", () => second.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
if (also) throw new Refusal(also.code, `fallback ${fallback.harness}: ${also.message} (primary ${primary.harness}: ${failed.message})`);
|
||||
[model, harness, route, routeReason] = [fallback, second, "fallback", failed.code];
|
||||
}
|
||||
@@ -740,7 +744,7 @@ export function createLauncher({
|
||||
if (store) compactions(store, model.harness, harness, { instance, thread, session, run });
|
||||
const toolSocket = join(runDir, "tools.sock");
|
||||
const turnMarker = join(runDir, "turn.done");
|
||||
const version = harnessVersion !== undefined ? harnessVersion : call(model.harness, "version", () => harness.version({ env }));
|
||||
const version = harnessVersion !== undefined ? harnessVersion : await call(model.harness, "version", () => harness.version({ env }));
|
||||
// Row 137 (#1631): a role and thread holding an approved, unspent merge decision get land_work.
|
||||
// The broker checks the decision again on every land; this only decides what the bundle shows.
|
||||
// Row 156 (#1651): a non-thread instance's land and landing come from one landHolders read, so a
|
||||
@@ -1670,6 +1674,38 @@ export function createLauncher({
|
||||
clearTimeout(timer);
|
||||
}
|
||||
|
||||
// One harness ({ harness: name }) through a fresh loader, so a refusal never leaves an earlier
|
||||
// load's module in use: { refused, module }, module null when refused. A launch (harnessOf) and
|
||||
// the start's fit warm-up below (#1873 re-check 8) both load through here.
|
||||
async function freshHarness(m, log) {
|
||||
const loader = makeLoader({ dataRoot:system.instanceRoot ?? dataRoot, log });
|
||||
const { refused } = await loader.load({ harness: [m.harness] });
|
||||
return { refused, module: refused.length ? null : loader.modules("harness")[0] };
|
||||
}
|
||||
|
||||
// #1873 pi-unpin re-check 8: each switched-on harness module with ready() (pi's fit check) starts
|
||||
// it here, in the background, so the first launch joins the check in flight instead of starting
|
||||
// it. Nothing waits on it: a launch still awaits its own preflight, which shares this check or
|
||||
// reads its pass. A warm-up that fails or throws is dropped; it caches nothing, and the launch's
|
||||
// preflight runs the check again. Re-check 9: a harness plugin the warm-up can't load leaves one
|
||||
// launch log entry, by its code only (a launch that loads it records the message).
|
||||
function warmHarnesses() {
|
||||
const on = Array.isArray(b.plugins?.harness) ? b.plugins.harness : [];
|
||||
for (const name of on) {
|
||||
(async () => {
|
||||
let logged = false;
|
||||
const refusal = (entry) => {
|
||||
if (logged || entry?.event !== "plugin-refused") return;
|
||||
logged = true;
|
||||
record({ event: "plugin-refused", kind: entry.kind, name: entry.name, code: entry.code, during: "warm-up" });
|
||||
};
|
||||
const { module } = await freshHarness({ harness: name }, refusal);
|
||||
if (typeof module?.ready === "function") await module.ready({ env });
|
||||
})().catch(() => {});
|
||||
}
|
||||
}
|
||||
if (launches) warmHarnesses();
|
||||
|
||||
host.beforeClose(close);
|
||||
return { business: b, registry, listen, launchPm, fromSession, tick, close };
|
||||
}
|
||||
@@ -16,7 +16,7 @@
|
||||
//
|
||||
// mosaic triage key issue | rotate | retire | status
|
||||
//
|
||||
// Triage's own model proxy client key (packages/cli/src/triage-key.mjs). Human verbs as well,
|
||||
// Triage's own model proxy client key (packages/bus/src/triage-key.mjs, shared with the bus). Human verbs as well,
|
||||
// refused in an agent run before anything is read. No output names a key, only its id.
|
||||
import { lstatSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
@@ -24,7 +24,7 @@ import { fileURLToPath } from "node:url";
|
||||
import { readRoute, RouteStoreError, writeRoute } from "../../webui/src/triage-route-store.mjs";
|
||||
import { loadSystem } from "./config.mjs";
|
||||
import { configDir } from "../../organization/src/organization-config.mjs";
|
||||
import { issueKey, keyPaths, keyStatus, retireKeys, rotateKey, TriageKeyError } from "./triage-key.mjs";
|
||||
import { issueKey, keyPaths, keyStatus, retireKeys, rotateKey, TriageKeyError } from "../../bus/src/triage-key.mjs";
|
||||
import { CliError } from "./errors.mjs";
|
||||
import { refuseInsideAgent } from "./transport.mjs";
|
||||
|
||||
|
||||
+2
-1
@@ -16,7 +16,7 @@ import { pushWorking, fetchReview } from '../../../harness/src/git-tools.mjs';
|
||||
import { callTool } from '../../../harness/src/tools.mjs';
|
||||
import { toolServer } from '../../../harness/src/runner.mjs';
|
||||
import { buildBundle } from '../../../harness/src/bundle.mjs';
|
||||
import { HARNESS } from '../../../harness/tests/helpers.mjs';
|
||||
import { HARNESS, harnessReady } from '../../../harness/tests/helpers.mjs';
|
||||
import { ensureCheckouts } from '../../../seat/src/checkout.mjs';
|
||||
import { newRun, Registry, readSessions, writeSessionFile, workspaceDir } from '../../../seat/src/session.mjs';
|
||||
import { startTimeOf } from '../../src/host.mjs';
|
||||
@@ -127,6 +127,7 @@ async function runtime() {
|
||||
const cap = bus.bindLaunch(launch_record).cap;
|
||||
roles[role] = { cap, run: id, workspace, runDir };
|
||||
call(role, 'role.claim');
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
const bundle = buildBundle({ dir: join(runDir, 'bundle'), resolved, contract: readFileSync(resolved.contract, 'utf8'),
|
||||
arbiter: role === 'cto', tracker: Boolean(resolved.tracker), credentials: [], skillsDir: join(dataRoot, 'skills-enabled'), harness: HARNESS.pi,
|
||||
harnessVersion: '0.85.1', session: { run: id, workspace, checkouts, toolSocket: join(runDir, 'tools.sock'), turnMarker: join(runDir, 'turn.done') } });
|
||||
|
||||
@@ -0,0 +1,428 @@
|
||||
// #1873 harness watch, slice W2: the bus host's timer over the watch (src/harness-watch-host.mjs)
|
||||
// and its two state files. A fake clock and stand-in watches; one run goes through the real watch
|
||||
// with stand-in harnesses, and one through startHost.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, utimesSync, writeFileSync } from "node:fs";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { DAY_MS, STAT_MS, leakChecker, startHarnessWatch, watchFile, watchLog } from "../src/harness-watch-host.mjs";
|
||||
import { bootConfig, loadSystem } from "../src/config.mjs";
|
||||
import { startHost } from "../src/host.mjs";
|
||||
import { makeDeployment } from "../../discord/tests/helpers.mjs";
|
||||
import { fixture, tmp } from "./helpers.mjs";
|
||||
import { requestWatch } from "../../plugins/src/watch-request.mjs";
|
||||
import * as claude from "../../plugins/harness/claude-code/index.mjs";
|
||||
|
||||
function scratch(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), "mosaic-hw-"));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
return root;
|
||||
}
|
||||
|
||||
// A clock the test moves: fire() runs the interval's callback, as the timer would every statMs.
|
||||
function fakeClock(start = Date.parse("2026-11-20T00:00:00Z")) {
|
||||
const c = { at: start, fn: null, ms: null, cleared: [], handle: { fake: true } };
|
||||
return Object.assign(c, {
|
||||
now: () => c.at,
|
||||
setInterval: (fn, ms) => { c.fn = fn; c.ms = ms; return c.handle; },
|
||||
clearInterval: (h) => c.cleared.push(h),
|
||||
fire: () => c.fn(),
|
||||
});
|
||||
}
|
||||
|
||||
const record = (over = {}) => ({ harness: "claude-code", state: "fit", via: "inspect", found: true, source: "MOSAIC_CLAUDE_BIN", version: "1.0.0", digest: "a".repeat(64), lastDigest: "a".repeat(64), fit: "pass", definite: true, failed: [], line: "ok", surfaces: { help: "usage" }, checkedAt: "2026-11-20T00:00:00.000Z", changedAt: "2026-11-20T00:00:00.000Z", ...over });
|
||||
const transition = (over = {}) => ({ harness: "claude-code", at: "2026-11-20T00:00:00.000Z", from: null, to: { state: "fit", version: "1.0.0", digest: "a".repeat(64) }, failed: [], line: "ok", ...over });
|
||||
|
||||
// A stand-in watch that counts its calls and answers what the test sets next.
|
||||
function stubWatch(answer = () => ({ records: [record()], transitions: [], refused: [], gone: [] })) {
|
||||
const calls = [];
|
||||
const watch = async (opts) => { calls.push(opts); return answer(opts, calls.length); };
|
||||
return { calls, watch };
|
||||
}
|
||||
|
||||
function start(t, over = {}) {
|
||||
const dataRoot = over.dataRoot ?? scratch(t);
|
||||
const clock = over.clock ?? fakeClock();
|
||||
const logs = [];
|
||||
let key = "k1";
|
||||
let status = { halted: false };
|
||||
const keyCalls = [];
|
||||
const w = over.stub ?? stubWatch();
|
||||
const handle = startHarnessWatch({
|
||||
dataRoot, clock, log: (l) => logs.push(l), env: over.env ?? {}, watch: w.watch,
|
||||
keys: async () => { keyCalls.push(key); return key; },
|
||||
processing: () => status,
|
||||
scratchRoot: () => over.scratchRoot ?? scratch(t),
|
||||
});
|
||||
t.after(() => handle.stop());
|
||||
return { dataRoot, clock, logs, handle, w, keyCalls, setKey: (k) => { key = k; }, setStatus: (s) => { status = s; } };
|
||||
}
|
||||
|
||||
async function tick(s) {
|
||||
s.clock.fire();
|
||||
await s.handle.settled();
|
||||
}
|
||||
|
||||
test("W2 host watch: one full pass at start; a stat pass with no change runs none; a changed key runs one; a day runs one", async (t) => {
|
||||
const s = start(t);
|
||||
await s.handle.settled();
|
||||
assert.equal(s.clock.ms, STAT_MS);
|
||||
assert.equal(s.w.calls.length, 1, "the start pass");
|
||||
assert.ok(existsSync(watchFile(s.dataRoot)));
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.deepEqual([s.w.calls.length, s.keyCalls.length], [1, 2], "a stat pass reads the key and runs no full pass");
|
||||
s.setKey("k2");
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "a changed key runs one full pass");
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "and only one");
|
||||
s.clock.at += DAY_MS - 2 * STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "less than a day since the last full pass");
|
||||
s.clock.at += 2 * STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 3, "the daily pass");
|
||||
// The watch gets the last records, the clock, a scratch directory and the stop signal.
|
||||
const opts = s.w.calls[2];
|
||||
assert.equal(opts.last[0].harness, "claude-code");
|
||||
assert.equal(opts.now().getTime(), s.clock.at);
|
||||
assert.equal(typeof opts.scratch, "string");
|
||||
assert.equal(existsSync(opts.scratch), false, "its scratch directory is removed after the pass");
|
||||
assert.equal(opts.signal.aborted, false);
|
||||
});
|
||||
|
||||
test("W2 host watch: while processing is halted or invalid no pass runs; the first tick after resume runs one full pass", async (t) => {
|
||||
const s = start(t);
|
||||
await s.handle.settled();
|
||||
s.setStatus({ halted: true });
|
||||
for (let i = 0; i < 3; i++) { s.clock.at += STAT_MS; await tick(s); }
|
||||
assert.deepEqual([s.w.calls.length, s.keyCalls.length], [1, 1], "halted: no key read, no pass");
|
||||
s.setStatus({ halted: null, invalid: true, code: "processing-state-invalid" });
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 1, "invalid: no pass");
|
||||
assert.equal(s.logs.filter((l) => /halted or invalid/.test(l)).length, 1, "logged once while held");
|
||||
s.setStatus({ halted: false });
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "resume runs a full pass though the key did not change");
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "once");
|
||||
});
|
||||
|
||||
test("W2 host watch: a processing state that can't be read holds the start pass", async (t) => {
|
||||
const dataRoot = scratch(t);
|
||||
const clock = fakeClock();
|
||||
const w = stubWatch();
|
||||
const h = startHarnessWatch({ dataRoot, clock, watch: w.watch, keys: async () => "k", processing: () => { throw new Error("unreadable"); }, scratchRoot: () => scratch(t) });
|
||||
t.after(() => h.stop());
|
||||
await h.settled();
|
||||
assert.equal(w.calls.length, 0);
|
||||
});
|
||||
|
||||
test("W2 host watch: passes never overlap; a tick while one runs does nothing", async (t) => {
|
||||
let release;
|
||||
const stub = stubWatch((opts, n) => n === 1 ? new Promise((done) => { release = () => done({ records: [record()], transitions: [], refused: [], gone: [] }); }) : { records: [record()], transitions: [], refused: [], gone: [] });
|
||||
const s = start(t, { stub });
|
||||
await new Promise((done) => setImmediate(done));
|
||||
assert.equal(s.w.calls.length, 1);
|
||||
const running = s.handle.settled();
|
||||
for (let i = 0; i < 5; i++) { s.setKey(`k${i + 2}`); s.clock.at += STAT_MS; s.clock.fire(); }
|
||||
await new Promise((done) => setImmediate(done));
|
||||
assert.deepEqual([s.w.calls.length, s.keyCalls.length], [1, 1], "no second pass and no key read while the first runs");
|
||||
release();
|
||||
await running;
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "the next tick after it runs normally");
|
||||
});
|
||||
|
||||
test("W2 host watch: a pass that throws is logged with no path and the timer goes on", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const stub = stubWatch((opts, n) => { if (n === 1) throw Object.assign(new Error(`boom at ${dir}/secret`), {}); if (n === 2) throw Object.assign(new Error(`eacces ${dir}`), { code: "EACCES" }); return { records: [record()], transitions: [], refused: [], gone: [] }; });
|
||||
const s = start(t, { stub });
|
||||
await s.handle.settled();
|
||||
assert.equal(s.logs.length, 1);
|
||||
assert.match(s.logs[0], /harness watch: a pass failed \(an error\); the timer goes on/);
|
||||
assert.ok(!s.logs[0].includes(dir));
|
||||
assert.equal(existsSync(watchFile(s.dataRoot)), false);
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "the key was not kept, so the next tick runs a full pass again");
|
||||
assert.match(s.logs[1], /\(EACCES\)/);
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 3);
|
||||
assert.ok(existsSync(watchFile(s.dataRoot)));
|
||||
assert.deepEqual(s.clock.cleared, [], "the timer was never cleared");
|
||||
// A keys read that throws is a failed pass too.
|
||||
const k = startHarnessWatch({ dataRoot: scratch(t), clock: fakeClock(), log: (l) => s.logs.push(l), watch: stubWatch().watch, keys: async () => { throw new Error(dir); }, processing: () => ({ halted: false }), scratchRoot: () => scratch(t) });
|
||||
t.after(() => k.stop());
|
||||
await k.settled();
|
||||
assert.match(s.logs.at(-1), /a pass failed \(an error\)/);
|
||||
});
|
||||
|
||||
test("W2 host watch: stop clears the timer, aborts the pass running, waits for it, and writes nothing after", async (t) => {
|
||||
let seen = null;
|
||||
let ended = false;
|
||||
const stub = stubWatch((opts) => new Promise((done) => {
|
||||
seen = opts.signal;
|
||||
opts.signal.addEventListener("abort", () => setTimeout(() => { ended = true; done({ records: [record()], transitions: [transition()], refused: [], gone: [], stopped: true }); }, 50));
|
||||
}));
|
||||
const s = start(t, { stub });
|
||||
await new Promise((done) => setImmediate(done));
|
||||
assert.equal(seen.aborted, false);
|
||||
await s.handle.stop();
|
||||
assert.equal(seen.aborted, true);
|
||||
assert.equal(ended, true, "stop waited for the pass to end");
|
||||
assert.deepEqual(s.clock.cleared, [s.clock.handle]);
|
||||
assert.equal(existsSync(watchFile(s.dataRoot)), false);
|
||||
assert.equal(existsSync(watchLog(s.dataRoot)), false);
|
||||
s.clock.fire();
|
||||
await s.handle.settled();
|
||||
assert.equal(s.w.calls.length, 1, "a tick after stop does nothing");
|
||||
assert.deepEqual(readdirSync(stub.calls[0].scratch.replace(/\/[^/]+$/, "")), [], "the pass's scratch directory is gone");
|
||||
// A watch that ignores the signal and answers normally still writes nothing once stopped.
|
||||
const late = start(t, { stub: stubWatch((opts) => new Promise((done) => opts.signal.addEventListener("abort", () => done({ records: [record()], transitions: [transition()], refused: [], gone: [] })))) });
|
||||
await new Promise((done) => setImmediate(done));
|
||||
await late.handle.stop();
|
||||
assert.equal(existsSync(watchFile(late.dataRoot)), false);
|
||||
assert.deepEqual(late.logs, []);
|
||||
});
|
||||
|
||||
test("W2 host watch: the json is replaced whole and holds no surfaces; the jsonl only grows, one line per transition", async (t) => {
|
||||
let answer = { records: [record()], transitions: [transition()], refused: [{ event: "plugin-refused", kind: "harness", name: "odd", code: "plugin-interface", message: "x" }], gone: [] };
|
||||
const s = start(t, { stub: stubWatch(() => answer) });
|
||||
await s.handle.settled();
|
||||
const file = watchFile(s.dataRoot);
|
||||
const first = JSON.parse(readFileSync(file, "utf8"));
|
||||
assert.deepEqual(Object.keys(first).sort(), ["checkedAt", "gone", "harnesses", "refused", "version"]);
|
||||
assert.equal(first.version, 1);
|
||||
assert.equal(first.checkedAt, new Date(s.clock.at).toISOString());
|
||||
assert.equal(first.harnesses.length, 1);
|
||||
assert.equal("surfaces" in first.harnesses[0], false, "surfaces are slice W6's");
|
||||
assert.equal(first.refused[0].name, "odd");
|
||||
assert.equal(statSync(file).mode & 0o777, 0o600);
|
||||
assert.equal(statSync(join(s.dataRoot, "state")).mode & 0o777, 0o700);
|
||||
const log = watchLog(s.dataRoot);
|
||||
assert.equal(readFileSync(log, "utf8").split("\n").filter(Boolean).length, 1);
|
||||
assert.equal(statSync(log).mode & 0o777, 0o600);
|
||||
const ino = statSync(file).ino;
|
||||
const before = readFileSync(log, "utf8");
|
||||
// A second pass with two transitions and one harness gone.
|
||||
answer = { records: [record({ state: "red", fit: "fail", failed: ["--strict-mcp-config"] })], transitions: [transition({ to: { state: "red", version: "1.0.0", digest: "a".repeat(64) } }), transition({ harness: "pi" })], refused: [], gone: ["pi"] };
|
||||
s.setKey("k2");
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
const second = JSON.parse(readFileSync(file, "utf8"));
|
||||
assert.notEqual(statSync(file).ino, ino, "renamed over, not written in place");
|
||||
assert.deepEqual([second.harnesses[0].state, second.refused, second.gone], ["red", [], ["pi"]]);
|
||||
const after = readFileSync(log, "utf8");
|
||||
assert.ok(after.startsWith(before), "the jsonl keeps every line it had");
|
||||
assert.equal(after.split("\n").filter(Boolean).length, 3);
|
||||
assert.deepEqual(after.split("\n").filter(Boolean).map((l) => JSON.parse(l).harness), ["claude-code", "claude-code", "pi"]);
|
||||
// A pass with no transition appends nothing.
|
||||
answer = { records: [record()], transitions: [], refused: [], gone: [] };
|
||||
s.setKey("k3");
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(readFileSync(log, "utf8"), after);
|
||||
assert.deepEqual(readdirSync(join(s.dataRoot, "state")).sort(), ["harness-watch.json", "harness-watch.jsonl"], "no temp file is left");
|
||||
// A new host reads the records back as the last ones.
|
||||
const next = start(t, { dataRoot: s.dataRoot });
|
||||
await next.handle.settled();
|
||||
assert.equal(next.w.calls[0].last[0].harness, "claude-code");
|
||||
});
|
||||
|
||||
test("W2 host watch: a record or transition holding a host path or an env value is not written, and the log names no path", async (t) => {
|
||||
const secret = "fixture-secret-value-71";
|
||||
const cases = [
|
||||
(o) => ({ records: [record({ line: `found at ${o.scratch}/claude` })], transitions: [], refused: [], gone: [] }),
|
||||
() => ({ records: [record({ version: `1.0 ${homedir()}` })], transitions: [], refused: [], gone: [] }),
|
||||
() => ({ records: [record()], transitions: [transition({ line: `token ${secret}` })], refused: [], gone: [] }),
|
||||
(o) => ({ records: [record({ failed: [o.dataRoot] })], transitions: [], refused: [], gone: [] }),
|
||||
];
|
||||
for (const answer of cases) {
|
||||
const s = start(t, { env: { FIXTURE_TOKEN: secret }, stub: stubWatch(answer) });
|
||||
await s.handle.settled();
|
||||
assert.equal(existsSync(watchFile(s.dataRoot)), false);
|
||||
assert.equal(existsSync(watchLog(s.dataRoot)), false);
|
||||
assert.match(s.logs.join("\n"), /a state line held a host path or an env value/);
|
||||
for (const l of s.logs) assert.ok(!l.includes(s.dataRoot) && !l.includes(secret));
|
||||
}
|
||||
// An env value that is also a harness name or a state word does not block the write.
|
||||
const named = start(t, { env: { MOSAIC_HARNESS: "claude-code", OTHER: "unknown-ish" } });
|
||||
await named.handle.settled();
|
||||
assert.ok(existsSync(watchFile(named.dataRoot)));
|
||||
});
|
||||
|
||||
test("W2 host watch through the real watch: stand-in harnesses that print a scratch path and an env value leave neither in either file", async (t) => {
|
||||
const root = scratch(t);
|
||||
const dataRoot = join(root, "org");
|
||||
mkdirSync(dataRoot);
|
||||
const secret = "fixture-secret-value-93";
|
||||
const bin = join(root, "bin", "claude");
|
||||
mkdirSync(join(root, "bin"));
|
||||
// Its --version prints its own path and the secret; its --help lacks every adapter flag.
|
||||
writeFileSync(bin, `#!/bin/sh\ncase "$1" in\n--version) echo "1.0.0 $0 ${secret} $HOME";;\n--help) echo "usage: claude [--version] at $0";;\n*) exit 3;;\nesac\n`, { mode: 0o755 });
|
||||
const env = { MOSAIC_CLAUDE_BIN: bin, MOSAIC_PI_BIN: join(root, "no-pi"), PATH: "/usr/bin:/bin", FIXTURE_TOKEN: secret, HOME: join(root, "home") };
|
||||
const clock = fakeClock();
|
||||
const logs = [];
|
||||
const h = startHarnessWatch({ dataRoot, env, clock, log: (l) => logs.push(l), processing: () => ({ halted: false }), scratchRoot: () => root });
|
||||
t.after(() => h.stop());
|
||||
await h.settled();
|
||||
assert.deepEqual(logs, []);
|
||||
const json = readFileSync(watchFile(dataRoot), "utf8");
|
||||
const jsonl = readFileSync(watchLog(dataRoot), "utf8");
|
||||
const doc = JSON.parse(json);
|
||||
const rec = doc.harnesses.find((r) => r.source === "MOSAIC_CLAUDE_BIN");
|
||||
assert.equal(rec.state, "red");
|
||||
assert.ok(rec.failed.includes("--strict-mcp-config"));
|
||||
assert.ok(doc.harnesses.some((r) => r.state === "missing" && r.source === "MOSAIC_PI_BIN"));
|
||||
assert.equal(jsonl.split("\n").filter(Boolean).length, doc.harnesses.length, "the first pass is a transition per harness");
|
||||
for (const text of [json, jsonl]) {
|
||||
for (const needle of [root, secret, homedir(), dataRoot, bin, tmpdir()]) assert.ok(!text.includes(needle), `no ${needle === secret ? "env value" : "host path"} in a state file`);
|
||||
}
|
||||
// Its scratch directories are gone.
|
||||
assert.deepEqual(readdirSync(root).sort(), ["bin", "org"]);
|
||||
// A stat pass with nothing changed runs no full pass: the jsonl and json stay as they are.
|
||||
clock.at += STAT_MS;
|
||||
clock.fire();
|
||||
await h.settled();
|
||||
assert.equal(readFileSync(watchFile(dataRoot), "utf8"), json);
|
||||
// The binary replaced: its key changed, so the next tick runs a full pass.
|
||||
writeFileSync(bin, `#!/bin/sh\ncase "$1" in\n--version) echo "1.0.1";;\n--help) echo "usage";;\n*) exit 3;;\nesac\n`, { mode: 0o755 });
|
||||
clock.at += STAT_MS;
|
||||
clock.fire();
|
||||
await h.settled();
|
||||
assert.notEqual(readFileSync(watchFile(dataRoot), "utf8"), json, "a changed key ran a full pass");
|
||||
assert.equal(JSON.parse(readFileSync(watchFile(dataRoot), "utf8")).harnesses.find((r) => r.source === "MOSAIC_CLAUDE_BIN").version, "1.0.1");
|
||||
});
|
||||
|
||||
// Review P3-6: each needle of the backstop on its own: the data root, the home directory (with HOME
|
||||
// unset, so no env value covers it) and a value only the host process's env holds.
|
||||
test("W2 host watch: the backstop knows the data root, the home directory and the host's own env values", () => {
|
||||
const leaks = leakChecker({ env: {}, scratch: "/fixture-scratch-x", dataRoot: "/fixture-data-root/org" });
|
||||
assert.equal(leaks({ line: "at /fixture-data-root/org/state" }), true);
|
||||
assert.equal(leaks({ line: "at /fixture-scratch-x/home" }), true);
|
||||
assert.equal(leaks({ line: "nothing here" }), false);
|
||||
assert.equal(leaks({ harness: "/fixture-data-root/org" }), false, "a name field is not text");
|
||||
const saved = process.env.HOME;
|
||||
process.env.FIXTURE_HW_ONLY = "fixture-process-only-77";
|
||||
try {
|
||||
delete process.env.HOME;
|
||||
const home = homedir();
|
||||
const bare = leakChecker({ env: {}, scratch: "/fixture-scratch-x", dataRoot: "/fixture-data-root/org" });
|
||||
assert.equal(bare({ version: `1.0 ${home}` }), true, "the home directory");
|
||||
assert.equal(bare({ line: "token fixture-process-only-77" }), true, "a value of the host process's env");
|
||||
} finally {
|
||||
process.env.HOME = saved;
|
||||
delete process.env.FIXTURE_HW_ONLY;
|
||||
}
|
||||
});
|
||||
|
||||
// Review P3-5: a file: URL, a host:/path and a ~/ path a binary prints are paths too, and reach
|
||||
// neither file.
|
||||
test("W2 host watch through the real watch: a file: URL, a host:/path and a ~/ path in a version reach neither file", async (t) => {
|
||||
const root = scratch(t);
|
||||
const dataRoot = join(root, "org");
|
||||
mkdirSync(dataRoot);
|
||||
const bin = join(root, "bin", "claude");
|
||||
mkdirSync(join(root, "bin"));
|
||||
writeFileSync(bin, `#!/bin/sh\ncase "$1" in\n--version) echo "1.0 file:///opt/fixture-tree/bin/claude host:/srv/fixture-a/b ~/.local/share/fixture/claude";;\n--help) echo "usage";;\n*) exit 3;;\nesac\n`, { mode: 0o755 });
|
||||
const env = { MOSAIC_CLAUDE_BIN: bin, MOSAIC_PI_BIN: join(root, "no-pi"), PATH: "/usr/bin:/bin", HOME: join(root, "home") };
|
||||
const h = startHarnessWatch({ dataRoot, env, clock: fakeClock(), log: () => {}, processing: () => ({ halted: false }), scratchRoot: () => root });
|
||||
t.after(() => h.stop());
|
||||
await h.settled();
|
||||
const json = readFileSync(watchFile(dataRoot), "utf8");
|
||||
const jsonl = readFileSync(watchLog(dataRoot), "utf8");
|
||||
assert.equal(JSON.parse(json).harnesses.find((r) => r.source === "MOSAIC_CLAUDE_BIN").version, "1.0 <path> host:<path> <path>");
|
||||
for (const text of [json, jsonl]) for (const needle of ["fixture-tree", "/srv/", "fixture-a", "~/", "/fixture/claude"]) assert.ok(!text.includes(needle), `no ${needle} in a state file`);
|
||||
});
|
||||
|
||||
// Review P2-4: a module asks for a stat pass now (claude-code's preflight, with no verdict). It runs
|
||||
// at once, or once after the pass running, and a full pass only when the key changed.
|
||||
test("W2 host watch: a request runs a stat pass now, a full pass only on a changed key, once after a running pass, none after stop", async (t) => {
|
||||
let release = null;
|
||||
let hold = false;
|
||||
const stub = stubWatch(() => (hold ? new Promise((done) => { release = () => done({ records: [record()], transitions: [], refused: [], gone: [] }); }) : { records: [record()], transitions: [], refused: [], gone: [] }));
|
||||
const s = start(t, { stub });
|
||||
await s.handle.settled();
|
||||
assert.deepEqual([stub.calls.length, s.keyCalls.length], [1, 1]);
|
||||
requestWatch();
|
||||
await s.handle.settled();
|
||||
assert.deepEqual([stub.calls.length, s.keyCalls.length], [1, 2], "a request with the same key runs a stat pass and no full pass");
|
||||
s.setKey("k2");
|
||||
requestWatch();
|
||||
await s.handle.settled();
|
||||
assert.equal(stub.calls.length, 2, "a request after the key changed runs a full pass");
|
||||
// Requests while a pass runs: one more stat pass right after it, however many came.
|
||||
hold = true;
|
||||
s.setKey("k3");
|
||||
requestWatch();
|
||||
await new Promise((done) => setImmediate(done));
|
||||
assert.equal(stub.calls.length, 3);
|
||||
hold = false;
|
||||
s.setKey("k4");
|
||||
requestWatch();
|
||||
requestWatch();
|
||||
assert.equal(s.keyCalls.length, 4, "no stat pass while a pass runs");
|
||||
release();
|
||||
await new Promise((done) => setTimeout(done, 20));
|
||||
await s.handle.settled();
|
||||
assert.deepEqual([stub.calls.length, s.keyCalls.length], [4, 5], "one stat pass after it, which saw k4 and ran a full pass");
|
||||
await s.handle.stop();
|
||||
requestWatch();
|
||||
await s.handle.settled();
|
||||
assert.deepEqual([stub.calls.length, s.keyCalls.length], [4, 5], "after stop a request does nothing");
|
||||
});
|
||||
|
||||
// Review P2-4, through the real watch: a red claude touched after its check has a new stat key, so
|
||||
// preflight has no verdict for a moment; it asks the watch, whose pass checks it again, and preflight
|
||||
// refuses again within that pass, not at the next tick.
|
||||
test("W2 host watch through the real watch: preflight's request re-checks a red claude touched after its check", async (t) => {
|
||||
const root = scratch(t);
|
||||
const dataRoot = join(root, "org");
|
||||
mkdirSync(dataRoot);
|
||||
const bin = join(root, "bin", "claude");
|
||||
mkdirSync(join(root, "bin"));
|
||||
writeFileSync(bin, `#!/bin/sh\ncase "$1" in\n--version) echo "1.0.0";;\n--help) echo "usage: claude [--version]";;\n*) exit 3;;\nesac\n`, { mode: 0o755 });
|
||||
const env = { MOSAIC_CLAUDE_BIN: bin, MOSAIC_PI_BIN: join(root, "no-pi"), PATH: "/usr/bin:/bin", HOME: join(root, "home") };
|
||||
const launch = { ...env, ANTHROPIC_API_KEY: "fixture-api-key" };
|
||||
const clock = fakeClock();
|
||||
const h = startHarnessWatch({ dataRoot, env, clock, log: () => {}, processing: () => ({ halted: false }), scratchRoot: () => root });
|
||||
t.after(() => h.stop());
|
||||
await h.settled();
|
||||
assert.equal(claude.preflight({ env: launch })?.code, "harness-feature-missing");
|
||||
chmodSync(bin, 0o700);
|
||||
utimesSync(bin, new Date(), new Date(Date.now() + 60000));
|
||||
assert.equal(claude.preflight({ env: launch }), null, "no verdict for the new stat key: this launch is not held");
|
||||
await h.settled();
|
||||
assert.equal(claude.preflight({ env: launch })?.code, "harness-feature-missing", "the requested pass checked it again, with no tick");
|
||||
});
|
||||
|
||||
test("W2 startHost with harnessWatch starts the watch in the host, and close stops it", async (t) => {
|
||||
const root = tmp(t);
|
||||
const f = fixture(root);
|
||||
makeDeployment(root);
|
||||
const boot = bootConfig({ system: loadSystem({ env: f.env }), businessId: "acme", env: f.env });
|
||||
const clock = fakeClock();
|
||||
let signal = null;
|
||||
const stub = stubWatch((opts) => new Promise((done) => { signal = opts.signal; opts.signal.addEventListener("abort", () => done({ records: [], transitions: [], refused: [], gone: [], stopped: true })); }));
|
||||
const host = await startHost({ boot, business: "acme", log: () => {}, harnessWatch: { env: {}, clock, watch: stub.watch, keys: async () => "k", processing: () => ({ halted: false }), scratchRoot: () => scratch(t) } });
|
||||
let closed = false;
|
||||
t.after(() => (closed ? null : host.close(0)));
|
||||
await new Promise((done) => setTimeout(done, 50));
|
||||
assert.equal(stub.calls.length, 1, "the start pass runs in the host");
|
||||
assert.equal(stub.calls[0].dataRoot, boot.dataRoot);
|
||||
assert.equal(signal.aborted, false);
|
||||
assert.equal(await host.close(0), 0);
|
||||
closed = true;
|
||||
assert.equal(signal.aborted, true, "close aborted the pass");
|
||||
assert.deepEqual(clock.cleared, [clock.handle], "and cleared the timer");
|
||||
});
|
||||
@@ -12,7 +12,8 @@ import { createHash } from "node:crypto";
|
||||
import { once } from "node:events";
|
||||
import { createInterface } from "node:readline";
|
||||
import { connect } from "node:net";
|
||||
import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, symlinkSync, watch, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { Client } from "../../bus/src/client.mjs";
|
||||
@@ -24,6 +25,8 @@ import { bootConfig, loadSystem } from "../src/config.mjs";
|
||||
import { cmdlineOf, startHost, startTimeOf } from "../src/host.mjs";
|
||||
import { createLauncher } from "../src/launcher.mjs";
|
||||
import { mockAnthropic, REPO } from "../../harness/tests/helpers.mjs";
|
||||
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
|
||||
import * as pi from "../../plugins/harness/pi/index.mjs";
|
||||
import { fakeHarnesses, fixture, HOST_CREDENTIALS, tmp, withoutHostCredentials } from "./helpers.mjs";
|
||||
|
||||
const FAKE = fileURLToPath(new URL("../../harness/tests/fixtures/fake-adapter.mjs", import.meta.url));
|
||||
@@ -266,6 +269,142 @@ test("bus start --pm: the launcher instance (the chief of staff) runs under its
|
||||
assert.equal(end.exitCode, 0);
|
||||
});
|
||||
|
||||
// #1873 pi-unpin S1/S2: the version label comes from the pi the module finds. A pi whose --version
|
||||
// fails gives a null label, and the launch still goes ahead.
|
||||
test("pi-unpin: a pi named by MOSAIC_PI_BIN that exits 1 on --version records a null harnessVersion and still launches", { skip, timeout: 30000 }, async (t) => {
|
||||
const prepared = prepare(t);
|
||||
const pkg = join(prepared.root, "pi-prefix", "lib", "node_modules", "@earendil-works", "pi-coding-agent");
|
||||
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "3.0.0", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
// #1873 S3: the stand-in passes the fit check; only its --version fails.
|
||||
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn({ versionExit: 1 }), { mode: 0o755 });
|
||||
mkdirSync(join(prepared.root, "pi-prefix", "bin"));
|
||||
const bin = join(prepared.root, "pi-prefix", "bin", "pi");
|
||||
symlinkSync("../lib/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js", bin);
|
||||
prepared.f.env.MOSAIC_PI_BIN = bin;
|
||||
// env() reads the bus's own environment, as the launcher's is process.env outside tests.
|
||||
const before = process.env.MOSAIC_PI_BIN;
|
||||
process.env.MOSAIC_PI_BIN = bin;
|
||||
t.after(() => { if (before === undefined) delete process.env.MOSAIC_PI_BIN; else process.env.MOSAIC_PI_BIN = before; });
|
||||
const { f, launcher, close } = await setup(t, (doc) => doc, prepared, { harnessVersion: undefined });
|
||||
const chief = await launcher.launchPm();
|
||||
assert.equal(chief.harness, "pi");
|
||||
const [s] = readSessions(f.dataRoot);
|
||||
const manifest = JSON.parse(readFileSync(join(s.runDir, "bundle", "manifest.json"), "utf8"));
|
||||
assert.equal(manifest.harnessVersion, null);
|
||||
assert.equal(await close(), 0);
|
||||
});
|
||||
|
||||
// #1873 re-check 8: a pi stand-in under root/pi-prefix, as an npm prefix holds it. Returns its bin.
|
||||
function piPrefix(root, knobs) {
|
||||
const pkg = join(root, "pi-prefix", "lib", "node_modules", "@earendil-works", "pi-coding-agent");
|
||||
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "3.0.0", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn(knobs), { mode: 0o755 });
|
||||
mkdirSync(join(root, "pi-prefix", "bin"));
|
||||
const bin = join(root, "pi-prefix", "bin", "pi");
|
||||
symlinkSync("../lib/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js", bin);
|
||||
return bin;
|
||||
}
|
||||
// Each fit run makes one scratch directory under tmpdir(), so a watch there counts the runs.
|
||||
// quiet() waits until no run has started for a while, so a check an earlier test left going
|
||||
// (the real pi's, warmed by its launcher) is not counted.
|
||||
function fitRuns(t) {
|
||||
const made = new Set();
|
||||
const watcher = watch(tmpdir(), (event, name) => { if (name && name.startsWith("mosaic-pi-fit-")) made.add(name); });
|
||||
t.after(() => watcher.close());
|
||||
const quiet = async () => {
|
||||
let n;
|
||||
do { n = made.size; await new Promise((r) => setTimeout(r, 700)); } while (made.size !== n);
|
||||
};
|
||||
return { made, quiet };
|
||||
}
|
||||
function busPiBin(t, prepared, bin) {
|
||||
prepared.f.env.MOSAIC_PI_BIN = bin;
|
||||
const before = process.env.MOSAIC_PI_BIN;
|
||||
process.env.MOSAIC_PI_BIN = bin;
|
||||
t.after(() => { if (before === undefined) delete process.env.MOSAIC_PI_BIN; else process.env.MOSAIC_PI_BIN = before; });
|
||||
}
|
||||
|
||||
// #1873 pi-unpin re-check 8: the launcher starts pi's fit check when it starts, before any launch,
|
||||
// and does not wait for it. The first launch joins that check: the two together make one check's
|
||||
// runs, not two.
|
||||
test("pi-unpin re-check 8: the launcher starts pi's fit check when it starts, and the first launch joins it", { skip, timeout: 60000 }, async (t) => {
|
||||
const { made, quiet } = fitRuns(t);
|
||||
// One check on its own, for another stand-in: how many runs it makes.
|
||||
const alone = piPrefix(tmp(t, "mosaic-launch-pi-"), {});
|
||||
await quiet();
|
||||
made.clear();
|
||||
assert.equal(await pi.preflight({ env: { ANTHROPIC_API_KEY: "stand-in", MOSAIC_PI_BIN: alone } }), null);
|
||||
await quiet();
|
||||
const runs = made.size;
|
||||
assert.ok(runs > 10, `one check made ${runs} runs`);
|
||||
const prepared = prepare(t);
|
||||
busPiBin(t, prepared, piPrefix(prepared.root, {}));
|
||||
made.clear();
|
||||
const { launcher, close } = await setup(t, (doc) => doc, prepared, { harnessVersion: undefined });
|
||||
// No launch yet, and the check is under way.
|
||||
await until(() => made.size > 0, 10000).catch(() => assert.fail("no fit run started before the first launch"));
|
||||
const chief = await launcher.launchPm();
|
||||
assert.equal(chief.harness, "pi");
|
||||
await quiet();
|
||||
assert.equal(made.size, runs, "the launch joined the check the launcher started");
|
||||
assert.equal(await close(), 0);
|
||||
});
|
||||
|
||||
// A warm-up that fails leaves no unhandled rejection and keeps no verdict: the next preflight runs
|
||||
// the check again and refuses on its own.
|
||||
test("pi-unpin re-check 8: a fit check the launcher starts that fails is dropped: no unhandled rejection, nothing kept", { skip, timeout: 60000 }, async (t) => {
|
||||
const { made, quiet } = fitRuns(t);
|
||||
const unhandled = [];
|
||||
const seen = (e) => unhandled.push(e);
|
||||
process.on("unhandledRejection", seen);
|
||||
t.after(() => process.off("unhandledRejection", seen));
|
||||
const prepared = prepare(t);
|
||||
const bin = piPrefix(prepared.root, { dropFlags: ["--no-themes"] });
|
||||
busPiBin(t, prepared, bin);
|
||||
await quiet();
|
||||
made.clear();
|
||||
const { close } = await setup(t, (doc) => doc, prepared, { harnessVersion: undefined });
|
||||
await until(() => made.size > 0, 10000).catch(() => assert.fail("no fit run started before the first launch"));
|
||||
// This preflight joins the failing check; then it is gone.
|
||||
const env = { ANTHROPIC_API_KEY: "stand-in", MOSAIC_PI_BIN: bin };
|
||||
const refusal = { code: "harness-feature-missing", message: "pi 9.9.9 (MOSAIC_PI_BIN) lacks --no-themes" };
|
||||
assert.deepEqual(await pi.preflight({ env }), refusal);
|
||||
await quiet();
|
||||
await new Promise((r) => setImmediate(r));
|
||||
assert.deepEqual(unhandled, []);
|
||||
made.clear();
|
||||
assert.deepEqual(await pi.preflight({ env }), refusal);
|
||||
await quiet();
|
||||
assert.ok(made.size > 0, "the failed check was kept");
|
||||
assert.equal(await close(), 0);
|
||||
});
|
||||
|
||||
// Re-check 9: a harness plugin the warm-up can't load leaves one launch log entry, by its code
|
||||
// only, so the message (which may name a host path) stays out of it.
|
||||
test("pi-unpin re-check 9: a harness plugin the launcher's warm-up can't load leaves one launch log entry, by code only", { skip, timeout: 30000 }, async (t) => {
|
||||
const given = prepare(t, (doc) => {
|
||||
doc.plugins = { ...doc.plugins, harness: [...doc.plugins.harness, "broken"] };
|
||||
return doc;
|
||||
});
|
||||
const makeLoader = await fakeHarnesses(t, { pi: given.adapter, "claude-code": given.adapter }, (write) => {
|
||||
write("harness", "broken", `throw new Error("broken at /srv/fit-secret-path");\n`);
|
||||
});
|
||||
const { f, close } = await setup(t, undefined, given, { makeLoader });
|
||||
const refused = () => existsSync(launchLogFile(f.dataRoot, "acme")) ? log(f).filter((e) => e.event === "plugin-refused") : [];
|
||||
await until(() => refused().length > 0, 10000).catch(() => assert.fail("the warm-up left no record of the refused plugin"));
|
||||
await new Promise((r) => setTimeout(r, 500));
|
||||
const entries = refused();
|
||||
assert.equal(entries.length, 1, JSON.stringify(entries));
|
||||
const [entry] = entries;
|
||||
assert.deepEqual(Object.keys(entry).sort(), ["at", "code", "during", "event", "kind", "name"]);
|
||||
assert.deepEqual([entry.kind, entry.name, entry.during], ["harness", "broken", "warm-up"]);
|
||||
assert.match(entry.code, /^[a-z][a-z-]+$/);
|
||||
assert.doesNotMatch(readFileSync(launchLogFile(f.dataRoot, "acme"), "utf8"), /fit-secret-path/);
|
||||
assert.equal(await close(), 0);
|
||||
});
|
||||
|
||||
test("the chief of staff launches a coder through its launch tool; the coder answers; refusals name their code", { skip, timeout: 30000 }, async (t) => {
|
||||
const { f, launcher, client, cto } = await setup(t);
|
||||
const sock = launchSocketPath(f.dataRoot);
|
||||
|
||||
@@ -90,6 +90,8 @@ async function routed(t, harness) {
|
||||
const makeLoader = await fakeHarnesses(t, { pi: null, 'claude-code': null }, write => {
|
||||
// fsthrows: its preflight reads a missing file, so the error's message quotes the path.
|
||||
write('harness', 'fsthrows', `export * from ${PI};\nimport { readFileSync } from "node:fs";\nexport function preflight() { readFileSync(${JSON.stringify(absent)}); }\n`);
|
||||
// fsrejects (#1873 re-check 8): an async preflight, as pi's is, whose promise rejects with that error.
|
||||
write('harness', 'fsrejects', `export * from ${PI};\nimport { readFileSync } from "node:fs";\nexport async function preflight() { readFileSync(${JSON.stringify(absent)}); }\n`);
|
||||
// plainthrows: a plain Error whose message quotes a host path, as a module's own text.
|
||||
write('harness', 'plainthrows', `export * from ${PI};\nexport function preflight() { throw new Error(${JSON.stringify(`cannot read ${absent}`)}); }\n`);
|
||||
// pathfails: a preflight that returns a failure whose message quotes a host path.
|
||||
@@ -146,6 +148,15 @@ test('a harness whose preflight throws a system error refuses harness-failed nam
|
||||
clean(JSON.stringify(s.last()), s.root);
|
||||
});
|
||||
|
||||
// #1873 re-check 8 P3-1: a preflight promise that rejects is refused as a throw is, by the call and
|
||||
// the system code, never the raw error or its path.
|
||||
test('a harness whose async preflight rejects with a system error refuses harness-failed naming the call and code, never the path', async t => {
|
||||
const s = await routed(t, 'fsrejects');
|
||||
assert.deepEqual(await s.launch(), { ok: false, error: 'harness-failed' });
|
||||
assert.equal(s.last().why, 'harness fsrejects preflight: cannot open a host file (ENOENT)');
|
||||
clean(JSON.stringify(s.last()), s.root);
|
||||
});
|
||||
|
||||
test('a harness the loader refuses names its code only, never the loader message or a path', async t => {
|
||||
const s = await routed(t, 'absent');
|
||||
assert.deepEqual(await s.launch(), { ok: false, error: 'harness-refused' });
|
||||
|
||||
@@ -146,7 +146,8 @@ test('real pinned install loads bus and Console; the Console starts only with --
|
||||
const s=setup(t,true);
|
||||
const copied=(await s.run('stack-copy.sh',['copy',s.id,'--copies',s.copies]));assert.equal(copied.status,0,copied.stderr);
|
||||
const root=join(s.copies,s.id),url=f=>pathToFileURL(join(root,f)).href;
|
||||
const {checkEnginePin}=await import(url('packages/conversation/src/pi-pin.mjs'));assert.equal(checkEnginePin(root).version,'0.85.1');
|
||||
await import(url('packages/conversation/src/pi-seal.mjs'));const {locate}=await import(url('packages/plugins/harness/pi/binary.mjs')),{levels}=await import(url('packages/plugins/harness/pi/index.mjs')),{piFit}=await import(url('packages/harness/src/pi-fit.mjs'));
|
||||
const copyPi=locate({});assert.equal(copyPi.label,"the repository's pi");assert.equal(copyPi.bin,join(root,'node_modules','.bin','pi'));const fit=await piFit(copyPi,{levels:levels()});assert.equal(fit.ok,true,fit.message);assert.equal(fit.version,'0.85.1');
|
||||
const home=join(s.root,'home'),f=fixture(home),configDir=join(home,'.config','mosaic-pin');
|
||||
mkdirSync(join(home,'.config'),{recursive:true});renameSync(join(home,'config'),configDir);chmodSync(configDir,0o700);
|
||||
writeFileSync(join(f.dataRoot,'instance.json'),JSON.stringify({instanceVersion:1,name:'pin'}));
|
||||
|
||||
@@ -12,7 +12,7 @@ import { join } from "node:path";
|
||||
import { createHash, randomBytes } from "node:crypto";
|
||||
import { run } from "../src/cli.mjs";
|
||||
import { AGENT_MARKERS } from "../src/transport.mjs";
|
||||
import { DEFAULT_ROTATE_GRACE_MINUTES, keyPaths, retireAtFor, rotateByFor } from "../src/triage-key.mjs";
|
||||
import { DEFAULT_ROTATE_GRACE_MINUTES, keyPaths, retireAtFor, rotateByFor } from "../../bus/src/triage-key.mjs";
|
||||
import { ModelKeys } from "../../bus/src/model-keys.mjs";
|
||||
import { ManagementClient } from "../../model-proxy/src/client.mjs";
|
||||
import { startBroker } from "../../bus/src/runtime.mjs";
|
||||
|
||||
@@ -234,7 +234,7 @@ live session, and the controller never writes a session file (W11).
|
||||
| `src/cohort.mjs` | `ScopeLauncher` (systemd user scope plus `shim.mjs`), `PgroupLauncher`, force stop, cohort and boot proofs |
|
||||
| `src/shim.mjs` | the scope's first process, outside the `engine` cgroup; ops `hello`, `events`, `members`, `term`, `freeze`, `kill`, `release`; ignores SIGTERM and exits on `release` once `engine` is empty |
|
||||
| `src/guard.mjs` | `LiveSessionGuard`: refuses live sessions and paths under live roots |
|
||||
| `src/pi-pin.mjs` | the Pi pin (0.85.1 and its integrity) and the seal |
|
||||
| `src/pi-seal.mjs` | the seal (with `--no-mcp` when the pi's help lists it) and the engine refusal codes |
|
||||
| `src/engine.mjs`, `src/framing.mjs` | the engine link and LF framing |
|
||||
| `src/turns.mjs` | `Tracker`: runs, the dispatch slot, overlap signals O1–O6 |
|
||||
| `src/events.mjs` | native event to CHAT-01 event mapping |
|
||||
@@ -289,16 +289,71 @@ can disagree with it.
|
||||
(`--no-session`, `--fork`, `--export`, `--print`, `--continue`, ...) or a
|
||||
bare word, which Pi reads as a prompt (lead decision 31; N24, including the missing flag
|
||||
through `checkSeal`).
|
||||
- **Engine command and environment (slice 1 S5, #1522).** The controller
|
||||
always launches `node <pinRoot>/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js`
|
||||
and checks that at construction and again at bind. `engine` takes only
|
||||
- **Engine command and environment (slice 1 S5, #1522; #1873 S4).** The
|
||||
controller launches `node <the real file of the pi the lookup finds>`.
|
||||
The lookup is the pi harness module's (`packages/plugins/harness/pi/binary.mjs`):
|
||||
`MOSAIC_PI_BIN`, else the first `pi` on `PATH`, else the repository's
|
||||
copy. The `pi` option takes a lookup answer and defaults to
|
||||
`locate(process.env)`. The controller checks the command at construction
|
||||
and again at bind, resolving the bin's real path each time, so a bin link
|
||||
moved after construction refuses `unsealed-engine`. `engine` takes only
|
||||
`extraArgs`, `cwd` and `envKeys`; any other key (`command`, `preArgs`,
|
||||
`env`), or an `engine` that isn't an object, refuses `unsealed-engine`.
|
||||
The fake engine comes in through `opts[TEST_ENGINE]`, a symbol key that
|
||||
JSON config can't carry, so no config file reaches an unsealed command.
|
||||
`preArgs` carrying `--extension` still refuses there too. The binding's
|
||||
`argvDigest` records the full command line.
|
||||
- **Engine environment.** The engine gets `ENGINE_ENV` (`pi-pin.mjs`):
|
||||
- **Engine fit and pins (#1873 S4).** No pi is refused for its version.
|
||||
Before a claim, a resume or a launch, the controller runs the fit check
|
||||
(`packages/harness/src/pi-fit.mjs`) on the pi the lookup finds. A pi that
|
||||
lacks a feature the seal needs, or that the check can't read, refuses
|
||||
`engine-feature-missing` with the fit check's line (for example
|
||||
`pi 9.9.9 (MOSAIC_PI_BIN) lacks --mode rpc`). The claim's pins are
|
||||
`engineVersion` (what `--version` printed, or null), `enginePin` (the fit
|
||||
check's digest of the pi's files) and `argvDigest`. A resume or recovery
|
||||
whose pins differ refuses `engine-pin-mismatch`, so changed bytes under a
|
||||
stopped claim can't resume it. A claim made before #1873 holds npm's
|
||||
integrity in `enginePin`; it refuses `engine-pin-mismatch` with "the claim
|
||||
was made under the old pin record; start a new claim". Like any pin
|
||||
mismatch, that refusal holds at every start of the session, since a resume
|
||||
is checked against the session's last proven stop. A new session starts a
|
||||
new claim.
|
||||
- **Same file, same interpreter.** The fit check runs the bin as a program,
|
||||
so its first line picks what runs it, while the launch runs the bin's real
|
||||
file with this Node. A pi whose first line isn't `#!/usr/bin/env node` or
|
||||
`#!` and an absolute path that resolves to this Node refuses
|
||||
`engine-feature-missing`. So does a first line that doesn't end within the
|
||||
256 bytes the kernel reads, or one with a CR before its newline. Under
|
||||
`#!/usr/bin/env node`, env takes node from the fit check's PATH (the
|
||||
repository's `.bin`, then this process's PATH, as `sessionEnv` builds it),
|
||||
and it looks inside the fit check's sandbox, which shows only `/usr`,
|
||||
`/etc`, the host's `/bin`, `/lib`, `/lib64` and `/sbin`, and this Node's
|
||||
own file at its path unless that path is under `/tmp` (the sandbox's fresh
|
||||
`/tmp` covers it). Up to the first `node`, every entry must read the
|
||||
same in the sandbox as on the host: absolute (env reads an empty or
|
||||
relative one against the fit check's directory), already in plain form (a
|
||||
`..` after a link goes where the link points), and not in `/proc` or
|
||||
`/dev`, which the sandbox makes its own, by its spelling or by the real
|
||||
path of the nearest part of it that resolves. The sandbox's `/tmp` is its
|
||||
own too, but starts empty, so a `/tmp` entry is judged like any other
|
||||
directory the sandbox doesn't show. An entry or a `node` link there that
|
||||
can't be checked refuses too. No `node` the sandbox can't show
|
||||
(or reaches through a link it can't show) may come before the first one it
|
||||
can, and that first one must be this Node. No `node` anywhere on the PATH
|
||||
may lie inside the package the fit check mounts. Otherwise the start
|
||||
refuses `engine-feature-missing`. A refusal line, the fit check's
|
||||
included, names the pi by the lookup's label, which is words such as
|
||||
`MOSAIC_PI_BIN`, or "the pi" for any other label. A lookup that found no
|
||||
pi refuses with its own line, or "the pi lookup gave no binary" when that
|
||||
line holds a path or a control character.
|
||||
The controller keeps a frozen plain copy of the lookup's answer, its own
|
||||
string data fields only, so an answer that changes later changes nothing.
|
||||
That copy must still be the lookup's at each check: the lookup run again on
|
||||
its bin gives the same `root`, `dest`, `view` and `pkg`, so the fit check
|
||||
mounts, runs and digests the package the launch runs. A package link moved
|
||||
since the lookup, an answer changed by hand, or a bin the lookup no longer
|
||||
takes refuses `unsealed-engine`.
|
||||
- **Engine environment.** The engine gets `ENGINE_ENV` (`pi-seal.mjs`):
|
||||
`PATH`, `HOME`, `USER`, `LOGNAME`, `SHELL`, `LANG`, `LC_ALL`,
|
||||
`LC_CTYPE`, `TZ`, `TERM`, `TMPDIR`, `PI_CODING_AGENT_DIR`, `PI_OFFLINE`,
|
||||
`PI_SKIP_VERSION_CHECK` and `PI_TELEMETRY`, plus the names in
|
||||
@@ -472,7 +527,7 @@ different IDs, so overlap at the seam can't be deduplicated (CHAT-01 lines
|
||||
## Slash text
|
||||
|
||||
`textPolicy` refuses any prompt whose text, after leading whitespace, starts
|
||||
with `/`: pinned Pi runs extension commands, skills (`/skill:`) and prompt
|
||||
with `/`: Pi runs extension commands, skills (`/skill:`) and prompt
|
||||
templates from index 0 (S1, S2). `!`, `!!` and `@` are interpreted only by
|
||||
Pi's interactive mode and CLI, not on the RPC prompt path, so they are
|
||||
admitted and sent as text. A `/` on a later line is not interpreted
|
||||
@@ -547,13 +602,14 @@ runs in CHAT-01 `check.mjs` order, then the CHAT-03 narrowings.
|
||||
| `eligibility` | launch with a used or unknown eligibility record, one from another incarnation, or a changed reservation (K17) |
|
||||
| `already-active`, `unsafe-replacement`, `foreign-host` | the writer claim (W2, W3, W17) |
|
||||
| `live-session-refused` | the live-session guard, at construction, bind and launch (G1–G3) |
|
||||
| `unsealed-engine`, `engine-pin-mismatch` | the seal or the Pi pin (N24) |
|
||||
| `unsealed-engine`, `engine-pin-mismatch` | the seal, or the engine's version, digest or argv changed under a claim (N24, W9, S4c) |
|
||||
| `engine-feature-missing` | the fit check found the pi lacks a feature, or couldn't tell (S4b) |
|
||||
| `configuration` | a required constructor path is missing or misplaced, the session file is unreadable, or the session header ID isn't a CHAT-01 ID |
|
||||
|
||||
`malformed`, `eligibility` and `preflight` are names this build adds; the
|
||||
brief describes those refusals without naming them.
|
||||
|
||||
The controller, claim store, live-session guard and engine pin throw
|
||||
The controller, claim store, live-session guard and engine checks throw
|
||||
`ControlRefusal` (`safe-fs.mjs`), a subclass of the reader's `Refusal`. These
|
||||
codes reach a socket client, never the reader's HTTP routes, so the control
|
||||
board's status map (`REFUSAL_STATUS`, which its tests check against every
|
||||
@@ -563,15 +619,15 @@ board ever serves the controller, these codes need statuses there first.
|
||||
Receipt reason codes, not refusals: `transport-unknown`,
|
||||
`handled-without-run`, `ack-without-start`, `interrupted`, `run-overlap`.
|
||||
|
||||
## Findings against pinned Pi
|
||||
## Findings against Pi 0.85.1
|
||||
|
||||
From `tests/smoke.test.mjs`, which starts the pinned binary sealed, in a
|
||||
From `tests/smoke.test.mjs`, which starts the repository's pi (0.85.1) sealed, in a
|
||||
scratch home with an empty agent dir and no inherited environment, and sends
|
||||
no prompt:
|
||||
|
||||
- It starts with no credentials and answers `get_state`, `get_commands`,
|
||||
`clear_queue`, `abort` and `get_tree`. Every field the fake engine sends
|
||||
for those commands, pinned Pi sends with the same type. `clear_queue` emits
|
||||
for those commands, Pi 0.85.1 sends with the same type. `clear_queue` emits
|
||||
`queue_update` with empty queues before its response, as the fake does.
|
||||
Pi writes `auth.json` (`{}`) and `models-store.json` into the agent dir.
|
||||
- **Startup append.** Pi appends to the session at startup in two cases
|
||||
@@ -593,13 +649,13 @@ no prompt:
|
||||
|
||||
| Suite | Covers |
|
||||
|---|---|
|
||||
| `claim.test.mjs` | W1–W17, W20, G1–G3: the writer claim, crash barriers, the guard |
|
||||
| `claim.test.mjs` | W1–W17, W20, G1–G3, S4a–S4h: the writer claim, crash barriers, the guard, the engine fit, pins and seal, the node-script check and the lookup answer check |
|
||||
| `races.test.mjs` | H1–H4, H9–H23: takeover, Interrupt and force stop, retries, incarnations |
|
||||
| `turns.test.mjs` | N1–N25, N24b: the turn tracker against the fake engine's Pi behaviors; the engine seal and environment |
|
||||
| `turns.test.mjs` | N1–N25, N24b, N24c: the turn tracker against the fake engine's Pi behaviors; the engine seal, `--no-mcp` and environment |
|
||||
| `cohort.test.mjs` | K1–K19, R1–R3, R5–R13, E1–E6: scopes, force stop, proofs, recovery, eligibility, the scope's environment, scope release and its retry after a crash, the engine-exit stop (needs a systemd user manager) |
|
||||
| `flows.test.mjs` | S1–S7, P3, E1–E7, the terminal, and a CHAT-01 schema check of every record produced |
|
||||
| `smoke.test.mjs` | the pinned Pi binary, as above |
|
||||
| `smoke.test.mjs` | the repository's Pi binary, as above, and its fit check |
|
||||
|
||||
`fake-pi.mjs` models pinned Pi's RPC mode, including the startup append, and
|
||||
`fake-pi.mjs` models Pi 0.85.1's RPC mode, including the startup append, and
|
||||
`ctrl-child.mjs` runs a controller in a child process for the crash tests.
|
||||
Fixtures live in temporary directories and are removed after each file.
|
||||
@@ -16,7 +16,7 @@
|
||||
// as in CHAT-01. With no verifier nothing verifies, and every stop ends
|
||||
// `uncertain`.
|
||||
|
||||
import { lstatSync, mkdirSync, readFileSync, unlinkSync } from "node:fs";
|
||||
import { closeSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, readlinkSync, realpathSync, statSync, unlinkSync } from "node:fs";
|
||||
import { createServer } from "node:net";
|
||||
import { basename, dirname, join, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
@@ -30,7 +30,11 @@ import { LineSplitter, encodeLine, parseLine } from "./framing.mjs";
|
||||
import { LiveSessionGuard, realPath } from "./guard.mjs";
|
||||
import { ID, fragments, safeId } from "./parts.mjs";
|
||||
import { parseSnapshot } from "./pi.mjs";
|
||||
import { ENGINE_PIN_MISMATCH, PI_BIN, UNSEALED_ENGINE, argvDigest, buildPiArgs, checkEnginePin, checkSeal, engineEnv } from "./pi-pin.mjs";
|
||||
import { ENGINE_FEATURE_MISSING, ENGINE_PIN_MISMATCH, OLD_PIN_LINE, UNSEALED_ENGINE, argvDigest, buildPiArgs, checkSeal, engineEnv, oldPinRecord } from "./pi-seal.mjs";
|
||||
import { PACKAGE, executable, locate } from "../../plugins/harness/pi/binary.mjs";
|
||||
import { sessionEnv } from "../../seat/src/session.mjs";
|
||||
import { levels } from "../../plugins/harness/pi/index.mjs";
|
||||
import { piFit } from "../../harness/src/pi-fit.mjs";
|
||||
import { ACTOR, conversationId, createReader, rootsFromSpecs } from "./reader.mjs";
|
||||
import { clone, equal, hash, newId, receiptAllows, record, scopeMatch, sealProof, sha256, targetOf } from "./records.mjs";
|
||||
import { ControlRefusal, Refusal } from "./safe-fs.mjs";
|
||||
@@ -79,7 +83,7 @@ const STOP_IN_PROGRESS = new Set(["fenced", "cancelling", "stopping"]);
|
||||
const STOP_NEXT = { fenced: ["cancelling", "stopping", "uncertain"], cancelling: ["stopping", "uncertain"], stopping: ["uncertain"], uncertain: [] };
|
||||
// check.mjs `ending`: the binding follows only stops that end the engine.
|
||||
const ending = (mode) => mode === "force-stop" || mode === "engine-exit";
|
||||
const SEAL_BASIS = "seal: --no-extensions and no --extension argument (pi-pin.mjs SEAL_FLAGS)";
|
||||
const SEAL_BASIS = "seal: --no-extensions and no --extension argument (pi-seal.mjs SEAL_FLAGS)";
|
||||
const DIALOG_REASON = "Pi dialogs are not answered in CHAT-03 (lead decision 30); shown disabled";
|
||||
|
||||
const refused = (code, extra = {}) => ({ outcome: `refused:${code}`, refusal: code, ...extra });
|
||||
@@ -152,13 +156,72 @@ export function sessionTools(parsed) {
|
||||
return tools;
|
||||
}
|
||||
|
||||
// A first line that runs node: #!/usr/bin/env node, or #! and an absolute path
|
||||
// whose last part is node, with nothing after it.
|
||||
const NODE_SHEBANG = /^#![ \t]*(?:\/usr\/bin\/env[ \t]+node|(\/[^\s]*\/node))[ \t]*$/;
|
||||
const ANSWER_KEYS = ["root", "dest", "view", "pkg"];
|
||||
const ANSWER_FIELDS = ["bin", "label", "problem", ...ANSWER_KEYS];
|
||||
|
||||
// #1873 S4: what the fit check's sandbox (systemBinds in seat/src/sandbox.mjs)
|
||||
// shows as the host has it: /usr, /etc and the host's /bin, /lib, /lib64 and
|
||||
// /sbin, and this node's own file at process.execPath. shownInFit follows p
|
||||
// one part at a time as the sandbox would and gives the file it reaches, or
|
||||
// null when a link or the file lies anywhere else. Null may be too strict (the
|
||||
// package's own mount is left out on purpose: no node may come from it), never
|
||||
// too loose.
|
||||
const SHOWN_DIRS = ["/usr", "/etc", "/bin", "/lib", "/lib64", "/sbin"];
|
||||
const shownDir = (p) => SHOWN_DIRS.some((d) => p === d || p.startsWith(d + "/"));
|
||||
// The sandbox makes its own /proc and /dev, so a path there names something
|
||||
// else inside it than on the host. Its /tmp is another directory too, but one
|
||||
// that starts empty (pi-fit.mjs makes it fresh for each run), so an entry
|
||||
// there holds no node in the sandbox, and a node the host finds there is one
|
||||
// the sandbox can't show. That holds for this Node's own file too: pi-fit.mjs
|
||||
// puts the fresh /tmp in place after it mounts that file, so an engine whose
|
||||
// node sits under /tmp is hidden in the sandbox.
|
||||
const OWN_COPY_DIRS = ["/proc", "/dev"];
|
||||
const underTmp = (p) => p === "/tmp" || p.startsWith("/tmp/");
|
||||
function shownInFit(p, hops = 0) {
|
||||
if (hops > 40) return null;
|
||||
const parts = p.split("/").filter(Boolean);
|
||||
let at = "/";
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
const next = join(at, parts[i]);
|
||||
let st;
|
||||
try { st = lstatSync(next); } catch { return null; }
|
||||
if (st.isSymbolicLink()) {
|
||||
if (!shownDir(next)) return null;
|
||||
let target;
|
||||
try { target = readlinkSync(next); } catch { return null; }
|
||||
return shownInFit(resolve(at, target, ...parts.slice(i + 1)), hops + 1);
|
||||
}
|
||||
at = next;
|
||||
}
|
||||
return (at === process.execPath && !underTmp(at)) || shownDir(at) ? at : null;
|
||||
}
|
||||
|
||||
// #1873 S4: the lookup's answer, copied once into a frozen plain object of
|
||||
// its own data fields, so what the seal checks and what the fit check reads
|
||||
// are the same values for the controller's life. A getter, a Proxy that
|
||||
// answers differently later, or an inherited field changes nothing after
|
||||
// this copy.
|
||||
function answerCopy(pi) {
|
||||
if (!pi || typeof pi !== "object") return pi;
|
||||
const out = {};
|
||||
for (const k of ANSWER_FIELDS) {
|
||||
let d;
|
||||
try { d = Object.getOwnPropertyDescriptor(pi, k); } catch { d = undefined; }
|
||||
if (d && "value" in d && (typeof d.value === "string" || d.value === null)) out[k] = d.value;
|
||||
}
|
||||
return Object.freeze(out);
|
||||
}
|
||||
|
||||
export class Controller {
|
||||
constructor(opts = {}) {
|
||||
const {
|
||||
fixtureRoot, claimRoot, socketDir, sessionFile, seat, workspace = "default",
|
||||
engine = {}, launcher = new PgroupLauncher(), verifier = null, host, identity, barrier = null, units = systemdUnits,
|
||||
grants = null, guardOptions = {}, timeouts = {}, now = () => new Date(),
|
||||
policyRevision = "policy-fixture-1", sourceRootRef = "source-fixture-1", approvedMappings = null, pinRoot = REPO_ROOT, auditWritable = true,
|
||||
policyRevision = "policy-fixture-1", sourceRootRef = "source-fixture-1", approvedMappings = null, pi = locate(process.env), auditWritable = true,
|
||||
hostId = null,
|
||||
} = opts;
|
||||
for (const [k, v] of Object.entries({ claimRoot, socketDir, sessionFile, seat })) {
|
||||
@@ -180,12 +243,15 @@ export class Controller {
|
||||
this.conversation = conversationId(this.root, basename(this.paths.sessionFile));
|
||||
if (!engine || typeof engine !== "object" || Array.isArray(engine)) throw new ControlRefusal(UNSEALED_ENGINE, "engine is not an object");
|
||||
const extra = Object.keys(engine).find((k) => !ENGINE_KEYS.has(k));
|
||||
if (extra !== undefined) throw new ControlRefusal(UNSEALED_ENGINE, `engine.${extra.slice(0, 40)} can't be configured; the controller launches the pinned Pi with its own environment`);
|
||||
if (extra !== undefined) throw new ControlRefusal(UNSEALED_ENGINE, `engine.${extra.slice(0, 40)} can't be configured; the controller launches the pi the lookup finds, with its own environment`);
|
||||
const test = opts[TEST_ENGINE] ?? null;
|
||||
// #1873 S4: the pi is binary.mjs's answer ({ bin, label, ... } or
|
||||
// { bin: null, problem }). A sealed launch runs its bin's real file.
|
||||
this.pi = answerCopy(pi);
|
||||
this.engine = {
|
||||
sealed: test === null,
|
||||
command: test ? test.command : process.execPath,
|
||||
preArgs: test ? test.preArgs : [join(pinRoot, PI_BIN)],
|
||||
preArgs: test ? test.preArgs : [this.#piFile()],
|
||||
extraArgs: engine.extraArgs ?? [],
|
||||
cwd: engine.cwd ?? projectRoot,
|
||||
env: test ? test.env : engineEnv(engine.envKeys),
|
||||
@@ -194,8 +260,8 @@ export class Controller {
|
||||
if (!Array.isArray(this.engine[k])) throw new ControlRefusal(UNSEALED_ENGINE, `engine.${k} is not a list`);
|
||||
}
|
||||
if (typeof this.engine.command !== "string" || !this.engine.command) throw new ControlRefusal(UNSEALED_ENGINE, "the engine command is not a path");
|
||||
this.noMcp = false;
|
||||
this.piArgs = buildPiArgs({ sessionFile: this.paths.sessionFile, extraArgs: this.engine.extraArgs });
|
||||
this.pinRoot = pinRoot;
|
||||
this.#checkSeal();
|
||||
this.launcher = launcher;
|
||||
this.verifier = verifier;
|
||||
@@ -257,20 +323,168 @@ export class Controller {
|
||||
if (this.barrier) await this.barrier(name, detail);
|
||||
}
|
||||
|
||||
// The real file of the pi's bin, read now. No answer from the lookup, or a
|
||||
// bin that no longer resolves, refuses: the lookup's own line names the
|
||||
// step, never a path. The answer must still be the lookup's: the lookup run
|
||||
// again on its bin gives the same root, dest, view and pkg. A stale
|
||||
// answer (a link moved since the lookup) or one changed by hand refuses
|
||||
// unsealed-engine.
|
||||
#piFile() {
|
||||
const pi = this.pi;
|
||||
if (!pi || typeof pi !== "object" || typeof pi.bin !== "string" || !pi.bin) {
|
||||
// The lookup's problem lines are words and its package's name; a
|
||||
// hand-built one with a path or a control character in it isn't shown.
|
||||
const problem = pi?.problem;
|
||||
const plain = typeof problem === "string" && problem && !/[\x00-\x1f\x7f-\x9f\u2028\u2029]/.test(problem) && !problem.replaceAll(PACKAGE, "").includes("/");
|
||||
const why = plain ? problem : "the pi lookup gave no binary";
|
||||
throw new ControlRefusal(ENGINE_FEATURE_MISSING, why);
|
||||
}
|
||||
const label = this.#label();
|
||||
let real;
|
||||
try {
|
||||
real = realpathSync(pi.bin);
|
||||
} catch {
|
||||
throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label} no longer resolves to a file`);
|
||||
}
|
||||
// The lookup run again on the same bin must give the same answer: what the
|
||||
// fit check mounts (root at dest), runs (view) and digests (pkg) is then
|
||||
// the package the launch runs.
|
||||
const again = locate({ MOSAIC_PI_BIN: pi.bin, ...(typeof process.env.HOME === "string" ? { HOME: process.env.HOME } : {}) });
|
||||
if (again.bin === null || ANSWER_KEYS.some((k) => (again[k] ?? null) !== (pi[k] ?? null))) {
|
||||
throw new ControlRefusal(UNSEALED_ENGINE, `${label} no longer matches the lookup's answer; look the pi up again`);
|
||||
}
|
||||
return real;
|
||||
}
|
||||
|
||||
// #1873 S4: the launch is this Node running the bin's real file, while the
|
||||
// fit check runs the bin as a program, so its first line picks what runs it.
|
||||
// Only a file whose first line runs node (#!/usr/bin/env node, or an
|
||||
// absolute path to this same node) runs the same way in both.
|
||||
#nodeScript(file) {
|
||||
let head = "";
|
||||
let fd = null;
|
||||
try {
|
||||
fd = openSync(file, "r");
|
||||
const buf = Buffer.alloc(256);
|
||||
head = buf.subarray(0, readSync(fd, buf, 0, buf.length, 0)).toString("latin1");
|
||||
} catch {
|
||||
head = "";
|
||||
} finally {
|
||||
if (fd !== null) try { closeSync(fd); } catch {}
|
||||
}
|
||||
const label = this.#label();
|
||||
// The kernel reads at most 256 bytes for the first line; one that doesn't
|
||||
// end inside them isn't run as a #! line at all.
|
||||
const nl = head.indexOf("\n");
|
||||
const m = nl < 0 ? null : NODE_SHEBANG.exec(head.slice(0, nl));
|
||||
if (!m) throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label} isn't a node script, and the engine runs pi with node`);
|
||||
// An absolute node must be this one: a file named node that is something
|
||||
// else would let the fit check run another program than the launch does.
|
||||
if (m[1] !== undefined) {
|
||||
let interp = null;
|
||||
try { interp = realpathSync(m[1]); } catch {}
|
||||
if (interp === null || interp !== realpathSync(process.execPath)) {
|
||||
throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs another node than the engine's`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
// env takes node from the fit check's PATH, the one a bus session gets
|
||||
// (sessionEnv: the repository's .bin, then this process's PATH), and it
|
||||
// looks inside the sandbox, which shows only some of the host. Up to the
|
||||
// first node, every entry must read the same in both: absolute, already
|
||||
// in plain form (a ".." after a link goes where the link points, not
|
||||
// where the text does), and nowhere the sandbox gives its own copy of
|
||||
// (/proc, /dev), in its spelling or its real path. An entry or a
|
||||
// node there that can't be checked refuses. No node the sandbox can't show
|
||||
// may come first, and the first one must be this node. No node on the
|
||||
// PATH may come from the package the fit check mounts.
|
||||
const engineNode = realpathSync(process.execPath);
|
||||
const realOrNull = (p) => { try { return realpathSync(p); } catch { return null; } };
|
||||
const root = realOrNull(this.pi.root) ?? this.pi.root;
|
||||
const refuse = (why) => { throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs node from the fit check's PATH, ${why}`); };
|
||||
const missing = (e) => e?.code === "ENOENT" || e?.code === "ENOTDIR";
|
||||
const ownCopy = (p) => OWN_COPY_DIRS.some((d) => p === d || p.startsWith(d + "/"));
|
||||
let first = null;
|
||||
for (const dir of String(sessionEnv(process.env, "fit").PATH ?? "").split(":")) {
|
||||
if (first === null) {
|
||||
if (!dir.startsWith("/")) refuse("which has an empty or relative entry");
|
||||
const plain = dir.length > 1 ? dir.replace(/\/+$/, "") : dir;
|
||||
if (resolve(plain) !== plain) refuse("which has an entry that isn't in plain form");
|
||||
const own = "which has an entry the fit check's sandbox has its own copy of";
|
||||
if (ownCopy(plain)) refuse(own);
|
||||
// An entry that doesn't exist is judged by the real path of the
|
||||
// nearest part of it that does.
|
||||
let realDir = null;
|
||||
for (let at = plain; realDir === null; at = dirname(at)) {
|
||||
try { realDir = realpathSync(at); } catch (e) { if (!missing(e) || at === "/") refuse("where an entry can't be checked"); }
|
||||
}
|
||||
if (ownCopy(realDir)) refuse(own);
|
||||
const candidate = join(plain, "node");
|
||||
try { lstatSync(candidate); } catch (e) { if (missing(e)) continue; refuse("where an entry can't be checked"); }
|
||||
let held = null;
|
||||
try { held = statSync(candidate); } catch { refuse("where a node can't be checked"); }
|
||||
if (!held.isFile() || !executable(candidate)) continue;
|
||||
} else if (!dir.startsWith("/") || !executable(resolve(dir, "node"))) {
|
||||
continue;
|
||||
}
|
||||
const candidate = resolve(dir, "node");
|
||||
const real = realOrNull(candidate);
|
||||
if (real === null) {
|
||||
if (first === null) refuse("where a node can't be checked");
|
||||
continue;
|
||||
}
|
||||
if (real === root || real.startsWith(root + "/")) {
|
||||
throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s package puts a node on the fit check's PATH`);
|
||||
}
|
||||
if (first !== null) continue;
|
||||
first = shownInFit(candidate);
|
||||
if (first === null) refuse("where a node the fit check can't see comes first");
|
||||
}
|
||||
if (first === null || realOrNull(first) !== engineNode) {
|
||||
throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs the first node on the fit check's PATH, which isn't the engine's`);
|
||||
}
|
||||
}
|
||||
|
||||
// The name a refusal line uses for the pi: the lookup's label, which names
|
||||
// a step in words ("MOSAIC_PI_BIN", "the pi on PATH", "the repository's
|
||||
// pi"), never a path. Anything else, such as a hand-built answer's label
|
||||
// with a / or a newline in it, is "the pi".
|
||||
#label() {
|
||||
const label = this.pi?.label;
|
||||
return typeof label === "string" && /^[A-Za-z_' ]{1,64}$/.test(label) ? label : "the pi";
|
||||
}
|
||||
|
||||
// The seal covers the command: unless a test engine was given, the launch
|
||||
// is this Node running the pinned Pi's bin, and nothing else.
|
||||
// is this Node running the real file of the bin the lookup found, read
|
||||
// again at each check, and nothing else.
|
||||
#checkSeal() {
|
||||
if (this.engine.sealed && (this.engine.command !== process.execPath || this.engine.preArgs.length !== 1 || this.engine.preArgs[0] !== join(this.pinRoot, PI_BIN))) {
|
||||
throw new ControlRefusal(UNSEALED_ENGINE, "the engine command is not the pinned Pi");
|
||||
if (this.engine.sealed && (this.engine.command !== process.execPath || this.engine.preArgs.length !== 1 || this.engine.preArgs[0] !== this.#piFile())) {
|
||||
throw new ControlRefusal(UNSEALED_ENGINE, `the engine command is not ${this.#label()} the lookup found`);
|
||||
}
|
||||
const bad = this.engine.preArgs.find((a) => typeof a !== "string" || a === "-e" || a === "--extension" || a.startsWith("--extension="));
|
||||
if (bad !== undefined) throw new ControlRefusal(UNSEALED_ENGINE, `engine pre-arguments carry ${bad}`);
|
||||
checkSeal(this.piArgs);
|
||||
checkSeal(this.piArgs, { noMcp: this.noMcp });
|
||||
}
|
||||
|
||||
#pins() {
|
||||
const pin = checkEnginePin(this.pinRoot);
|
||||
return { engineVersion: pin.version, enginePin: pin.pin, argvDigest: argvDigest(this.engine.command, [...this.engine.preArgs, ...this.piArgs]) };
|
||||
// #1873 S4: the fit check (pi-fit.mjs, cached by the digest of the files
|
||||
// that run) stands where the version pin stood. A pi that fails it, or
|
||||
// can't be checked, refuses engine-feature-missing with the check's line.
|
||||
// engineVersion is the --version text (null when pi printed none) and
|
||||
// enginePin the digest. --no-mcp joins the seal when the help lists it.
|
||||
async #pins() {
|
||||
this.#nodeScript(this.#piFile());
|
||||
const fit = await piFit({ ...this.pi, label: this.#label() }, { levels: levels() });
|
||||
if (!fit?.ok) throw new ControlRefusal(ENGINE_FEATURE_MISSING, typeof fit?.message === "string" ? fit.message : "the pi could not be checked");
|
||||
if (typeof fit.digest !== "string" || !/^[0-9a-f]{64}$/.test(fit.digest)) throw new ControlRefusal(ENGINE_FEATURE_MISSING, "the fit check gave no digest");
|
||||
this.noMcp = fit.noMcp === true;
|
||||
this.piArgs = buildPiArgs({ sessionFile: this.paths.sessionFile, extraArgs: this.engine.extraArgs, noMcp: this.noMcp });
|
||||
this.#checkSeal();
|
||||
return { engineVersion: typeof fit.version === "string" ? fit.version : null, enginePin: fit.digest, argvDigest: argvDigest(this.engine.command, [...this.engine.preArgs, ...this.piArgs]) };
|
||||
}
|
||||
|
||||
// The line for a pins mismatch against a claim's recorded pins.
|
||||
static pinLine(recorded, since) {
|
||||
return oldPinRecord(recorded) ? OLD_PIN_LINE : `the engine pin or launch argv changed since ${since}`;
|
||||
}
|
||||
|
||||
// Every read after construction refuses a header ID other than the one the
|
||||
@@ -341,7 +555,7 @@ export class Controller {
|
||||
async start() {
|
||||
this.#guardCheck("bind");
|
||||
this.#checkSeal();
|
||||
const pins = this.#pins();
|
||||
const pins = await this.#pins();
|
||||
const session = this.#readSession();
|
||||
const classified = await this.store.classify(this.seatK, this.sessionK, {
|
||||
units: this.units,
|
||||
@@ -373,7 +587,7 @@ export class Controller {
|
||||
let priorRef = null;
|
||||
if (prior.n > 0 && !prior.damaged && prior.record.state === "stopped") {
|
||||
const p = prior.record;
|
||||
if (!equal(p.pins, pins)) throw new ControlRefusal(ENGINE_PIN_MISMATCH, "the engine pin or launch argv changed since the proven stop");
|
||||
if (!equal(p.pins, pins)) throw new ControlRefusal(ENGINE_PIN_MISMATCH, Controller.pinLine(p.pins, "the proven stop"));
|
||||
const leaf = p.leafAtProof !== undefined ? p.leafAtProof : p.leaf;
|
||||
const branch = p.branchAtProof ?? p.branch;
|
||||
if (leaf !== session.leaf || branch !== session.branch) throw new ControlRefusal("target", "the session branch or leaf changed since the proven stop");
|
||||
@@ -1679,11 +1893,11 @@ export class Controller {
|
||||
if (b.state !== "stopped" || b.admission !== "closed" || !this.#stopped(s)) return refused("stop-proof");
|
||||
let pins;
|
||||
try {
|
||||
pins = this.#pins();
|
||||
pins = await this.#pins();
|
||||
} catch (err) {
|
||||
return refused(err.code ?? ENGINE_PIN_MISMATCH);
|
||||
return refused(err.code ?? ENGINE_PIN_MISMATCH, typeof err?.message === "string" ? { detail: err.message } : {});
|
||||
}
|
||||
if (!equal(pins, this.claim.record.pins)) return refused(ENGINE_PIN_MISMATCH);
|
||||
if (!equal(pins, this.claim.record.pins)) return refused(ENGINE_PIN_MISMATCH, { detail: Controller.pinLine(this.claim.record.pins, "the proven stop") });
|
||||
const session = this.#readSession();
|
||||
if (session.leaf !== this.claim.record.leafAtProof || session.branch !== this.claim.record.branchAtProof) return refused("target");
|
||||
if (!this.#checkConfirmation(r, c, "recover")) return refused("confirmation");
|
||||
@@ -1715,7 +1929,7 @@ export class Controller {
|
||||
throw new ControlRefusal(ELIGIBILITY, "the reserved claim changed since eligibility");
|
||||
}
|
||||
}
|
||||
const pins = this.#pins();
|
||||
const pins = await this.#pins();
|
||||
if (!equal(pins, e.pins)) throw new ControlRefusal(ENGINE_PIN_MISMATCH, "the engine pin or launch argv changed since eligibility");
|
||||
const session = this.#readSession();
|
||||
if (session.leaf !== e.leaf || session.branch !== e.branch) throw new ControlRefusal("target", "the session branch or leaf changed since eligibility (K18)");
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
// The Pi pin and the engine seal (#1507, CHAT-03 §3, lead decisions 31–32).
|
||||
// The engine seal and the engine's pins (#1507, CHAT-03 §3, lead decisions
|
||||
// 31–32; #1873 pi-unpin S4).
|
||||
//
|
||||
// Pin: package-lock.json and npm's installed record
|
||||
// (node_modules/.package-lock.json) must both name the pinned version with the
|
||||
// pinned integrity. That ties the install to the package through npm's record;
|
||||
// it is not a hash of the files on disk. `pi` runs dist/bundle/cli.js, the
|
||||
// package's bin, and the built-in llama.cpp extension ships inside it.
|
||||
// Pins: no pi version is pinned (Jason, 2026-11-19). The controller runs the
|
||||
// pi binary.mjs's lookup finds (MOSAIC_PI_BIN, else the first pi on PATH, else
|
||||
// the repository's) once the fit check (packages/harness/src/pi-fit.mjs)
|
||||
// passes it. The claim's pins are what that check reports: `engineVersion`,
|
||||
// the `--version` text (null when pi printed none), and `enginePin`, the
|
||||
// sha256 digest of the files that run. A pi that fails the check, or can't be
|
||||
// checked, is refused `engine-feature-missing`; a digest or argv that differs
|
||||
// from a claim's is refused `engine-pin-mismatch`. A claim made before #1873
|
||||
// holds npm's sha512 integrity in `enginePin`, which no digest equals, so it
|
||||
// refuses once with OLD_PIN_LINE.
|
||||
//
|
||||
// Seal: the controller builds the launch argv. It always carries
|
||||
// --no-extensions, --no-prompt-templates, --no-themes and --no-approve, and
|
||||
// never an --extension argument (cli/args.js; usage.md 224 and 233–236).
|
||||
// --no-extensions, --no-prompt-templates, --no-themes and --no-approve, plus
|
||||
// --no-mcp when the pi's --help lists it, and never an --extension argument
|
||||
// (cli/args.js; usage.md 224 and 233–236).
|
||||
// --no-approve sets the project trust override to false, so a project's
|
||||
// .pi/settings.json, SYSTEM.md, APPEND_SYSTEM.md and skills don't load even
|
||||
// when trust.json under the agent dir trusts it (main.js 574–581;
|
||||
@@ -23,60 +30,45 @@
|
||||
// file, so the argv must be exactly the controller's prefix followed by
|
||||
// ENGINE_OPTIONS pairs, each at most once with one plain value.
|
||||
|
||||
import { readFileSync } from "node:fs";
|
||||
import { isAbsolute, join } from "node:path";
|
||||
import { isAbsolute } from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import { ControlRefusal } from "./safe-fs.mjs";
|
||||
|
||||
export const PI_PACKAGE = "@earendil-works/pi-coding-agent";
|
||||
export const PI_VERSION = "0.85.1";
|
||||
export const PI_INTEGRITY = "sha512-FGRN+OHbWaefBPGaTggAdLjrIHW+s2PzLyglz/5dfLzb9of7uuXMXYC0fJIeZTw+shS32o2cuQ9jF7YSDuL/oQ==";
|
||||
export const PI_BIN = join("node_modules", PI_PACKAGE, "dist", "bundle", "cli.js");
|
||||
export const SEAL_FLAGS = Object.freeze(["--no-extensions", "--no-prompt-templates", "--no-themes", "--no-approve"]);
|
||||
export const ENGINE_OPTIONS = Object.freeze(["--model", "--provider", "--thinking"]);
|
||||
|
||||
export const ENGINE_PIN_MISMATCH = "engine-pin-mismatch";
|
||||
export const UNSEALED_ENGINE = "unsealed-engine";
|
||||
export const ENGINE_FEATURE_MISSING = "engine-feature-missing";
|
||||
|
||||
function lockEntry(path) {
|
||||
let lock;
|
||||
try {
|
||||
lock = JSON.parse(readFileSync(path, "utf8"));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
const entry = lock?.packages?.[`node_modules/${PI_PACKAGE}`];
|
||||
return entry && typeof entry === "object" ? entry : null;
|
||||
// The seal flags for a pi whose --help does (noMcp) or doesn't list --no-mcp.
|
||||
export const sealFlags = (noMcp = false) => (noMcp ? [...SEAL_FLAGS, "--no-mcp"] : [...SEAL_FLAGS]);
|
||||
|
||||
// An `enginePin` the fit check gives is a sha256 hex digest.
|
||||
export const PIN_DIGEST = /^[0-9a-f]{64}$/;
|
||||
export const OLD_PIN_LINE = "the claim was made under the old pin record; start a new claim";
|
||||
// True when a claim's pins came from the old npm pin record: an `enginePin`
|
||||
// string that isn't a digest (npm's integrity is `sha512-...`).
|
||||
export const oldPinRecord = (pins) => typeof pins?.enginePin === "string" && !PIN_DIGEST.test(pins.enginePin);
|
||||
|
||||
export function buildPiArgs({ sessionFile, extraArgs = [], noMcp = false }) {
|
||||
return ["--mode", "rpc", ...sealFlags(noMcp), "--session", sessionFile, ...extraArgs];
|
||||
}
|
||||
|
||||
// `root` holds package-lock.json and node_modules/.package-lock.json.
|
||||
export function checkEnginePin(root) {
|
||||
for (const path of [join(root, "package-lock.json"), join(root, "node_modules", ".package-lock.json")]) {
|
||||
const entry = lockEntry(path);
|
||||
if (!entry || entry.version !== PI_VERSION || entry.integrity !== PI_INTEGRITY) {
|
||||
throw new ControlRefusal(ENGINE_PIN_MISMATCH, `${path} does not pin ${PI_PACKAGE} ${PI_VERSION} with the pinned integrity`);
|
||||
}
|
||||
}
|
||||
return { version: PI_VERSION, pin: PI_INTEGRITY };
|
||||
}
|
||||
|
||||
export function buildPiArgs({ sessionFile, extraArgs = [] }) {
|
||||
return ["--mode", "rpc", ...SEAL_FLAGS, "--session", sessionFile, ...extraArgs];
|
||||
}
|
||||
|
||||
// Refuses any argv that is not `--mode rpc`, the four --no-* flags and
|
||||
// `--session <absolute path>`, in that order, followed by ENGINE_OPTIONS
|
||||
// pairs. That covers --extension in either spelling, a second --mode or
|
||||
// Refuses any argv that is not `--mode rpc`, the four --no-* flags (and
|
||||
// --no-mcp when noMcp), and `--session <absolute path>`, in that order,
|
||||
// followed by ENGINE_OPTIONS pairs. That covers --extension in either spelling, a second --mode or
|
||||
// --session, session and output flags (--no-session, --fork, --export, ...)
|
||||
// and stray prompt words.
|
||||
export function checkSeal(args) {
|
||||
export function checkSeal(args, { noMcp = false } = {}) {
|
||||
if (!Array.isArray(args) || args.some((a) => typeof a !== "string")) throw new ControlRefusal(UNSEALED_ENGINE, "launch argv is not a list of strings");
|
||||
const extension = args.find((a) => a === "-e" || a === "--extension" || a.startsWith("--extension="));
|
||||
if (extension !== undefined) throw new ControlRefusal(UNSEALED_ENGINE, `launch argv carries ${extension}`);
|
||||
for (const flag of SEAL_FLAGS) {
|
||||
const flags = sealFlags(noMcp);
|
||||
for (const flag of flags) {
|
||||
if (!args.includes(flag)) throw new ControlRefusal(UNSEALED_ENGINE, `launch argv lacks ${flag}`);
|
||||
}
|
||||
const prefix = ["--mode", "rpc", ...SEAL_FLAGS, "--session"];
|
||||
const prefix = ["--mode", "rpc", ...flags, "--session"];
|
||||
if (prefix.some((a, i) => args[i] !== a)) throw new ControlRefusal(UNSEALED_ENGINE, `launch argv does not start with ${prefix.join(" ")}`);
|
||||
const file = args[prefix.length];
|
||||
if (typeof file !== "string" || !isAbsolute(file)) throw new ControlRefusal(UNSEALED_ENGINE, "the --session value is not an absolute path");
|
||||
@@ -1,6 +1,6 @@
|
||||
// Run tracking, overlap signals and receipt settlement (#1507, CHAT-03 §3).
|
||||
//
|
||||
// Pinned Pi ties no event to a prompt. Under the seal (pi-pin.mjs) the Mosaic
|
||||
// Pi ties no event to a prompt. Under the seal (pi-seal.mjs) the Mosaic
|
||||
// prompt in the slot is the only thing that can start a run, so a run that
|
||||
// follows the slot's ack is attributed to it by order. The tracker watches
|
||||
// for signs that the seal failed (O1–O6) and for lead decision 34's
|
||||
|
||||
@@ -5,23 +5,27 @@
|
||||
|
||||
import { test, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { appendFileSync, copyFileSync, linkSync, mkdirSync, statSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, unlinkSync, writeFileSync, lstatSync } from "node:fs";
|
||||
import { appendFileSync, chmodSync, copyFileSync, linkSync, mkdirSync, statSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, unlinkSync, writeFileSync, lstatSync } from "node:fs";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { dirname, join } from "node:path";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { types as utilTypes } from "node:util";
|
||||
import { ALREADY_ACTIVE, ClaimStore, FOREIGN_HOST, UNSAFE_REPLACEMENT, held, machineId } from "../src/claim.mjs";
|
||||
import { Controller } from "../src/controller.mjs";
|
||||
import { Controller, REPO_ROOT } from "../src/controller.mjs";
|
||||
import { ConversationClient } from "../src/client.mjs";
|
||||
import { AUTHORITY, scopeAvailable } from "../src/cohort.mjs";
|
||||
import { FixtureVerifier } from "../src/records.mjs";
|
||||
import { LIVE_SESSION_REFUSED } from "../src/guard.mjs";
|
||||
import { ENGINE_PIN_MISMATCH, PI_PACKAGE } from "../src/pi-pin.mjs";
|
||||
import { ENGINE_FEATURE_MISSING, ENGINE_PIN_MISMATCH, OLD_PIN_LINE, UNSEALED_ENGINE } from "../src/pi-seal.mjs";
|
||||
import { piFit } from "../../harness/src/pi-fit.mjs";
|
||||
import { levels } from "../../plugins/harness/pi/index.mjs";
|
||||
import { bootId } from "../../discord/src/journal.mjs";
|
||||
import { FakeLauncher } from "./fake-pi.mjs";
|
||||
import {
|
||||
REPO, FAST, fixture, controllerFor, started, spawnController, killChildren, cleanupAll, reap, claimRecords,
|
||||
userEntry, assistantEntry, thinkingEntry, receiptState, noUnits, tick, killShims, shimsGone,
|
||||
PI_PACKAGE, standInPi, piAt,
|
||||
} from "./harness.mjs";
|
||||
|
||||
const reaped = [];
|
||||
@@ -579,14 +583,8 @@ test("W8: resume after a proven stop with the same pins: new claim ID, generatio
|
||||
|
||||
test("W9: resume with a changed binary, argv digest, branch or leaf is refused and the claim is unchanged", async () => {
|
||||
const cases = {
|
||||
binary: (fx) => {
|
||||
const root = mkdtempSync(join(fx.base, "pin-"));
|
||||
const lock = { packages: { [`node_modules/${PI_PACKAGE}`]: { version: "0.85.2", integrity: "sha512-other" } } };
|
||||
mkdirSync(join(root, "node_modules"));
|
||||
writeFileSync(join(root, "package-lock.json"), JSON.stringify(lock));
|
||||
writeFileSync(join(root, "node_modules", ".package-lock.json"), JSON.stringify(lock));
|
||||
return [{ pinRoot: root }, ENGINE_PIN_MISMATCH];
|
||||
},
|
||||
// Another pi, which the fit check passes, with other files.
|
||||
binary: (fx) => [{ pi: standInPi(join(fx.base, "other-pi"), { version: "0.85.2" }).found }, ENGINE_PIN_MISMATCH],
|
||||
argv: () => [{ engine: { extraArgs: ["--model", "other"] } }, ENGINE_PIN_MISMATCH],
|
||||
leaf: (fx) => {
|
||||
appendFileSync(fx.sessionFile, JSON.stringify(userEntry("d4e5f6a7", "b2c3d4e5", "outside", 9)) + "\n");
|
||||
@@ -610,6 +608,410 @@ test("W9: resume with a changed binary, argv digest, branch or leaf is refused a
|
||||
}
|
||||
});
|
||||
|
||||
// ---- #1873 pi-unpin S4: the engine is the pi the lookup finds --------------
|
||||
|
||||
test("S4a: a stand-in pi with another version and every feature launches, and the claim records its version and digest", async () => {
|
||||
const fx = fixture();
|
||||
const pi = standInPi(join(fx.base, "pi"), { version: "7.4.2" });
|
||||
const h = await started({ fx, pi: pi.found });
|
||||
try {
|
||||
assert.equal(h.launcher.launches.length, 1);
|
||||
const fit = await piFit(pi.found, { levels: levels() });
|
||||
assert.equal(fit.ok, true, fit.message);
|
||||
const recorded = h.ctrl.claim.record.pins;
|
||||
assert.equal(recorded.engineVersion, "7.4.2");
|
||||
assert.equal(recorded.enginePin, fit.digest);
|
||||
assert.match(recorded.enginePin, /^[0-9a-f]{64}$/);
|
||||
} finally {
|
||||
await h.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("S4b: a stand-in pi whose --help lacks --mode rpc is refused engine-feature-missing with a plain line; nothing launches or is claimed", async () => {
|
||||
const fx = fixture();
|
||||
const pi = standInPi(join(fx.base, "pi"), { modes: ["text", "json"] });
|
||||
const k = keysFor(fx, { pi: pi.found });
|
||||
const { ctrl, launcher } = controllerFor(fx, { pi: pi.found });
|
||||
try {
|
||||
await assert.rejects(ctrl.start(), (e) => {
|
||||
assert.equal(e.code, ENGINE_FEATURE_MISSING);
|
||||
assert.equal(e.message, "pi 9.9.9 (MOSAIC_PI_BIN) lacks --mode rpc");
|
||||
return true;
|
||||
});
|
||||
assert.equal(launcher.launches.length, 0);
|
||||
assert.deepEqual([revisionTexts(k.store, k.seatK), revisionTexts(k.store, k.sessionK)], [[], []]);
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
});
|
||||
|
||||
test("S4c: changed bytes in the pi between launch and resume refuse engine-pin-mismatch; the claim is unchanged", async () => {
|
||||
const fx = fixture();
|
||||
const pi = standInPi(join(fx.base, "pi"));
|
||||
const first = await provenStop(fx, { pi: pi.found });
|
||||
appendFileSync(pi.cli, "// one more line\n");
|
||||
const k = keysFor(fx, { pi: pi.found });
|
||||
const before = [revisionTexts(k.store, k.seatK), revisionTexts(k.store, k.sessionK)];
|
||||
const { ctrl, launcher } = controllerFor(fx, { pi: piAt(pi.bin) });
|
||||
try {
|
||||
await assert.rejects(ctrl.start(), (e) => {
|
||||
assert.equal(e.code, ENGINE_PIN_MISMATCH);
|
||||
assert.equal(e.message, "the engine pin or launch argv changed since the proven stop");
|
||||
return true;
|
||||
});
|
||||
assert.equal(launcher.launches.length, 0);
|
||||
assert.deepEqual([revisionTexts(k.store, k.seatK), revisionTexts(k.store, k.sessionK)], before);
|
||||
assert.equal(first.pins.engineVersion, "9.9.9");
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
});
|
||||
|
||||
test("S4d: a pre-argument that is not the resolved bin's real file refuses unsealed-engine at bind, as does a bin link moved after construction", async () => {
|
||||
const fx = fixture();
|
||||
const pi = standInPi(join(fx.base, "pi"));
|
||||
const make = () => new Controller({ fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat, launcher: new FakeLauncher(), units: noUnits, timeouts: FAST, pi: pi.found });
|
||||
{
|
||||
const ctrl = make();
|
||||
try {
|
||||
assert.deepEqual(ctrl.engine.preArgs, [realpathSync(pi.cli)]);
|
||||
// The repository's own pi, which the old pin accepted.
|
||||
ctrl.engine.preArgs = [join(REPO_ROOT, "node_modules", PI_PACKAGE, "dist", "bundle", "cli.js")];
|
||||
await assert.rejects(ctrl.start(), (e) => e.code === UNSEALED_ENGINE);
|
||||
assert.equal(ctrl.launcher.launches.length, 0);
|
||||
// The bin link itself, not its real file.
|
||||
ctrl.engine.preArgs = [pi.bin];
|
||||
await assert.rejects(ctrl.start(), (e) => e.code === UNSEALED_ENGINE);
|
||||
assert.equal(ctrl.launcher.launches.length, 0);
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
}
|
||||
{
|
||||
const ctrl = make();
|
||||
const other = standInPi(join(fx.base, "pi-other"), { version: "1.0.0" });
|
||||
try {
|
||||
unlinkSync(pi.bin);
|
||||
symlinkSync(other.cli, pi.bin);
|
||||
await assert.rejects(ctrl.start(), (e) => e.code === UNSEALED_ENGINE);
|
||||
assert.equal(ctrl.launcher.launches.length, 0);
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("S4e: a claim made under the old pin record (npm's integrity in enginePin) refuses engine-pin-mismatch with the old-record line at every start of that session", async () => {
|
||||
const fx = fixture();
|
||||
const first = await provenStop(fx);
|
||||
// Rewrite the stopped heads as a claim made before #1873 would hold them.
|
||||
for (const { path, record } of claimRecords(fx)) {
|
||||
if (record?.claimId !== first.claimId || record.state !== "stopped") continue;
|
||||
const old = { ...record, pins: { ...record.pins, engineVersion: "0.85.1", enginePin: "sha512-FGRN+OHbWaefBPGaTggAdLjrIHW+s2PzLyglz/5dfLzb9of7uuXMXYC0fJIeZTw+shS32o2cuQ9jF7YSDuL/oQ==" } };
|
||||
unlinkSync(path);
|
||||
writeFileSync(path, JSON.stringify(old), { mode: 0o400 });
|
||||
}
|
||||
// Like any pin mismatch, the refusal holds at each start: the session's last
|
||||
// proven stop is what a resume is checked against.
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const { ctrl, launcher } = controllerFor(fx);
|
||||
try {
|
||||
await assert.rejects(ctrl.start(), (e) => {
|
||||
assert.equal(e.code, ENGINE_PIN_MISMATCH);
|
||||
assert.equal(e.message, OLD_PIN_LINE);
|
||||
assert.match(e.message, /old pin record; start a new claim/);
|
||||
return true;
|
||||
});
|
||||
assert.equal(launcher.launches.length, 0);
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("S4f: a pi whose first line doesn't run node is refused engine-feature-missing, though the fit check passes it; #!/usr/bin/env node launches", async () => {
|
||||
const fx = fixture();
|
||||
// The sh form of the stand-in passes the fit check, which runs the bin as a
|
||||
// program, but the engine runs the bin's real file with node.
|
||||
const sh = standInPi(join(fx.base, "pi-sh"), { form: "sh" });
|
||||
const fit = await piFit(sh.found, { levels: levels() });
|
||||
assert.equal(fit.ok, true, fit.message);
|
||||
const k = keysFor(fx, { pi: sh.found });
|
||||
{
|
||||
const { ctrl, launcher } = controllerFor(fx, { pi: sh.found });
|
||||
try {
|
||||
await assert.rejects(ctrl.start(), (e) => {
|
||||
assert.equal(e.code, ENGINE_FEATURE_MISSING);
|
||||
assert.equal(e.message, "MOSAIC_PI_BIN isn't a node script, and the engine runs pi with node");
|
||||
return true;
|
||||
});
|
||||
assert.equal(launcher.launches.length, 0);
|
||||
assert.deepEqual([revisionTexts(k.store, k.seatK), revisionTexts(k.store, k.sessionK)], [[], []]);
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
}
|
||||
// The node form with the shebang npm writes for pi.
|
||||
const env = standInPi(join(fx.base, "pi-env"));
|
||||
const text = readFileSync(env.cli, "utf8");
|
||||
writeFileSync(env.cli, "#!/usr/bin/env node" + text.slice(text.indexOf("\n")));
|
||||
const h = await started({ fx, pi: env.found });
|
||||
try {
|
||||
assert.equal(h.launcher.launches.length, 1);
|
||||
assert.equal(h.launcher.launches[0].args[0], realpathSync(env.cli));
|
||||
} finally {
|
||||
await h.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("S4g: a lookup answer that is no longer the lookup's refuses unsealed-engine: a package link moved since the lookup, a hand-built answer with another bin, one field changed by hand, a bin no package holds, or a getter; an answer that changes after construction changes nothing", async () => {
|
||||
const fx = fixture();
|
||||
const good = standInPi(join(fx.base, "pi-good"));
|
||||
const lacking = standInPi(join(fx.base, "pi-lacking"), { modes: ["text", "json"] });
|
||||
// A global prefix whose package directory is a link, as a version manager
|
||||
// keeps it.
|
||||
const prefix = join(fx.base, "prefix");
|
||||
mkdirSync(join(prefix, "lib", "node_modules", "@earendil-works"), { recursive: true });
|
||||
mkdirSync(join(prefix, "bin"));
|
||||
const link = join(prefix, "lib", "node_modules", PI_PACKAGE);
|
||||
symlinkSync(good.pkg, link);
|
||||
const bin = join(prefix, "bin", "pi");
|
||||
symlinkSync(`../lib/node_modules/${PI_PACKAGE}/dist/bundle/cli.js`, bin);
|
||||
const found = piAt(bin);
|
||||
assert.equal(found.bin, bin);
|
||||
const make = (pi) => new Controller({ fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat, launcher: new FakeLauncher(), units: noUnits, timeouts: FAST, pi });
|
||||
const ctrl = make(found);
|
||||
try {
|
||||
unlinkSync(link);
|
||||
symlinkSync(lacking.pkg, link);
|
||||
// Built before the move: the start refuses.
|
||||
await assert.rejects(ctrl.start(), (e) => {
|
||||
assert.equal(e.code, UNSEALED_ENGINE);
|
||||
assert.equal(e.message, "MOSAIC_PI_BIN no longer matches the lookup's answer; look the pi up again");
|
||||
return true;
|
||||
});
|
||||
assert.equal(ctrl.launcher.launches.length, 0);
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
// Built after the move, with the stale answer.
|
||||
assert.throws(() => make(found), (e) => e.code === UNSEALED_ENGINE);
|
||||
// An answer with a bin swapped in by hand.
|
||||
assert.throws(() => make({ ...good.found, bin: lacking.bin }), (e) => e.code === UNSEALED_ENGINE);
|
||||
assert.throws(() => make({ bin: good.bin, label: "MOSAIC_PI_BIN" }), (e) => e.code === UNSEALED_ENGINE);
|
||||
// One field changed by hand at a time: the fit check would mount, run or
|
||||
// digest another package than the launch runs.
|
||||
for (const k of ["root", "dest", "view", "pkg"]) {
|
||||
const value = k === "dest" ? lacking.found.root : lacking.found[k];
|
||||
assert.throws(() => make({ ...good.found, [k]: value }), (e) => e.code === UNSEALED_ENGINE, k);
|
||||
}
|
||||
// The lacking pi's answer with the good package mounted over its path.
|
||||
assert.throws(() => make({ ...lacking.found, root: good.found.root, dest: lacking.found.root }), (e) => e.code === UNSEALED_ENGINE);
|
||||
// A bin the lookup no longer takes (no package holds it) refuses when the
|
||||
// controller is built.
|
||||
const loose = join(fx.base, "loose-pi");
|
||||
writeFileSync(loose, "#!/usr/bin/env node\n", { mode: 0o755 });
|
||||
assert.throws(() => make({ bin: loose, label: "MOSAIC_PI_BIN" }), (e) => e.code === UNSEALED_ENGINE);
|
||||
// A getter is not a data field: the copy the controller keeps lacks it.
|
||||
assert.throws(() => make({ ...good.found, get view() { return good.found.view; } }), (e) => e.code === UNSEALED_ENGINE);
|
||||
// An answer that changes after the controller is built changes nothing: it
|
||||
// keeps a frozen plain copy, and the start checks and launches the good pi.
|
||||
let flipped = false;
|
||||
const live = new Proxy({ ...good.found }, {
|
||||
get: (t, k) => (flipped && k in lacking.found ? lacking.found[k] : t[k]),
|
||||
getOwnPropertyDescriptor: (t, k) => {
|
||||
const d = Reflect.getOwnPropertyDescriptor(t, k);
|
||||
return d && flipped && k in lacking.found ? { ...d, value: lacking.found[k] } : d;
|
||||
},
|
||||
});
|
||||
const { ctrl: kept, launcher: keptLauncher } = controllerFor(fx, { pi: live });
|
||||
try {
|
||||
flipped = true;
|
||||
assert.ok(Object.isFrozen(kept.pi));
|
||||
assert.equal(utilTypes.isProxy(kept.pi), false);
|
||||
assert.deepEqual(kept.pi, good.found);
|
||||
await kept.start();
|
||||
assert.equal(keptLauncher.launches.length, 1);
|
||||
assert.equal(keptLauncher.launches[0].args[0], realpathSync(good.cli));
|
||||
} finally {
|
||||
await kept.close({ killEngine: true }).catch(() => {});
|
||||
}
|
||||
// The answer as the lookup gives it still builds.
|
||||
await make(piAt(good.bin)).close();
|
||||
});
|
||||
|
||||
test("S4h: a first line the kernel and node read differently refuses engine-feature-missing: an absolute node that isn't the engine's, a first line past 256 bytes, a CR before the newline, under env a node from the package, a node the fit check's sandbox can't see ahead of the first it can (even a link to the engine's node), an empty, relative or unplain entry before the first node, one in /proc or /dev, one that can't be checked, or a first shown node that isn't the engine's; the engine's own directory first launches, and a label that isn't the lookup's kind reads \"the pi\", in the fit check's line too; a lookup's problem line shows, a hand-built one with a path or a newline doesn't", async () => {
|
||||
const fx = fixture();
|
||||
const refused = async (pi, message, f = fx) => {
|
||||
const { ctrl, launcher } = controllerFor(f, { pi });
|
||||
try {
|
||||
await assert.rejects(ctrl.start(), (e) => {
|
||||
assert.equal(e.code, ENGINE_FEATURE_MISSING);
|
||||
assert.equal(e.message, message);
|
||||
return true;
|
||||
});
|
||||
assert.equal(launcher.launches.length, 0);
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
};
|
||||
const withFirstLine = (name, line) => {
|
||||
const pi = standInPi(join(fx.base, name));
|
||||
const text = readFileSync(pi.cli, "utf8");
|
||||
writeFileSync(pi.cli, line + text.slice(text.indexOf("\n")));
|
||||
return pi;
|
||||
};
|
||||
// A program named node in the package that runs another program: here the
|
||||
// real node, so the fit check passes it.
|
||||
const shim = standInPi(join(fx.base, "pi-shim"));
|
||||
const fakeNode = join(dirname(shim.cli), "node");
|
||||
writeFileSync(fakeNode, `#!/bin/sh\nexec ${process.execPath} "$@"\n`, { mode: 0o755 });
|
||||
const shimText = readFileSync(shim.cli, "utf8");
|
||||
writeFileSync(shim.cli, `#!${fakeNode}` + shimText.slice(shimText.indexOf("\n")));
|
||||
const fit = await piFit(shim.found, { levels: levels() });
|
||||
assert.equal(fit.ok, true, fit.message);
|
||||
await refused(shim.found, "MOSAIC_PI_BIN's first line runs another node than the engine's");
|
||||
// The kernel reads 256 bytes for the first line; this one ends past them.
|
||||
const long = withFirstLine("pi-long", "#!/usr/bin/env node" + " ".repeat(260));
|
||||
await refused(long.found, "MOSAIC_PI_BIN isn't a node script, and the engine runs pi with node");
|
||||
// The kernel looks for a program named with the CR.
|
||||
const cr = withFirstLine("pi-cr", "#!/usr/bin/env node\r");
|
||||
await refused(cr.found, "MOSAIC_PI_BIN isn't a node script, and the engine runs pi with node");
|
||||
// A link to the engine's own node is the same file.
|
||||
const linked = standInPi(join(fx.base, "pi-linked"));
|
||||
const nodeLink = join(dirname(linked.cli), "node");
|
||||
symlinkSync(process.execPath, nodeLink);
|
||||
const linkedText = readFileSync(linked.cli, "utf8");
|
||||
writeFileSync(linked.cli, `#!${nodeLink}` + linkedText.slice(linkedText.indexOf("\n")));
|
||||
const h = await started({ fx, pi: linked.found });
|
||||
try {
|
||||
assert.equal(h.launcher.launches.length, 1);
|
||||
assert.equal(h.launcher.launches[0].args[0], realpathSync(linked.cli));
|
||||
} finally {
|
||||
await h.close();
|
||||
}
|
||||
// #!/usr/bin/env node takes node from the fit check's PATH, which is this
|
||||
// process's PATH behind the repository's .bin.
|
||||
const envPi = withFirstLine("pi-env-path", "#!/usr/bin/env node");
|
||||
const fx2 = fixture();
|
||||
const onPath = async (dir, fn) => {
|
||||
const saved = process.env.PATH;
|
||||
process.env.PATH = `${dir}:${saved}`;
|
||||
try {
|
||||
await fn();
|
||||
} finally {
|
||||
process.env.PATH = saved;
|
||||
}
|
||||
};
|
||||
// A node in the package itself, put on PATH.
|
||||
const pkgBin = join(envPi.pkg, "bin");
|
||||
mkdirSync(pkgBin);
|
||||
writeFileSync(join(pkgBin, "node"), `#!/bin/sh\nexec ${process.execPath} "$@"\n`, { mode: 0o755 });
|
||||
await onPath(pkgBin, () => refused(envPi.found, "MOSAIC_PI_BIN's package puts a node on the fit check's PATH", fx2));
|
||||
// The fit check's sandbox shows only /usr, /etc, the host's /bin, /lib,
|
||||
// /lib64 and /sbin, and the engine's own node file. A node in any other
|
||||
// directory ahead of the engine's: env in the sandbox would pass it by,
|
||||
// while the host finds it first. Even a link to the engine's node.
|
||||
const otherBin = join(fx.base, "other-bin");
|
||||
mkdirSync(otherBin);
|
||||
writeFileSync(join(otherBin, "node"), `#!/bin/sh\nexec ${process.execPath} "$@"\n`, { mode: 0o755 });
|
||||
const hidden = "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, where a node the fit check can't see comes first";
|
||||
await onPath(otherBin, () => refused(envPi.found, hidden, fx2));
|
||||
const linkBin = join(fx.base, "link-bin");
|
||||
mkdirSync(linkBin);
|
||||
symlinkSync(process.execPath, join(linkBin, "node"));
|
||||
await onPath(linkBin, () => refused(envPi.found, hidden, fx2));
|
||||
// env reads an empty or relative entry against the fit check's directory.
|
||||
const relative = "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, which has an empty or relative entry";
|
||||
await onPath("", () => refused(envPi.found, relative, fx2));
|
||||
await onPath("some/bin", () => refused(envPi.found, relative, fx2));
|
||||
// Up to the first node, an entry must read the same in the sandbox as on
|
||||
// the host. A ".." after a link goes where the link points, not where the
|
||||
// text does.
|
||||
symlinkSync("/usr/lib", join(fx.base, "sys"));
|
||||
await onPath(`${fx.base}/sys/../bin`, () => refused(envPi.found, "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, which has an entry that isn't in plain form", fx2));
|
||||
// The sandbox makes its own /proc and /dev: pid 1 there is its own, and a
|
||||
// link into /proc counts too, even past a pid the host lacks.
|
||||
const ownCopy = "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, which has an entry the fit check's sandbox has its own copy of";
|
||||
await onPath("/proc/1/root/usr/bin", () => refused(envPi.found, ownCopy, fx2));
|
||||
await onPath("/dev/mosaic-no-such-dir", () => refused(envPi.found, ownCopy, fx2));
|
||||
symlinkSync("/proc", join(fx.base, "p"));
|
||||
await onPath(`${fx.base}/p/999999999/root/usr/bin`, () => refused(envPi.found, ownCopy, fx2));
|
||||
// A node link that leads nowhere on the host can't be checked: what the
|
||||
// sandbox finds at its target need not be what the host lacks there.
|
||||
const dangling = join(fx.base, "dangling-bin");
|
||||
mkdirSync(dangling);
|
||||
symlinkSync("/tmp/mosaic-no-such-dir/node", join(dangling, "node"));
|
||||
await onPath(dangling, () => refused(envPi.found, "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, where a node can't be checked", fx2));
|
||||
// An entry the host can't read can't be checked.
|
||||
const locked = join(fx.base, "locked");
|
||||
mkdirSync(join(locked, "bin"), { recursive: true });
|
||||
chmodSync(locked, 0o000);
|
||||
try {
|
||||
await onPath(join(locked, "bin"), () => refused(envPi.found, "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, where an entry can't be checked", fx2));
|
||||
} finally {
|
||||
chmodSync(locked, 0o755);
|
||||
}
|
||||
// The first node the sandbox shows must be the engine's: no node at all
|
||||
// isn't, and neither is a system node that is another file.
|
||||
const notEngine = "MOSAIC_PI_BIN's first line runs the first node on the fit check's PATH, which isn't the engine's";
|
||||
const withPath = async (path, fn) => {
|
||||
const saved = process.env.PATH;
|
||||
process.env.PATH = path;
|
||||
try {
|
||||
await fn();
|
||||
} finally {
|
||||
process.env.PATH = saved;
|
||||
}
|
||||
};
|
||||
await withPath("/usr/share", () => refused(envPi.found, notEngine, fx2));
|
||||
const systemNode = ["/usr/bin/node", "/usr/local/bin/node"].find((n) => {
|
||||
try {
|
||||
return realpathSync(n) !== realpathSync(process.execPath);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
if (systemNode) await withPath(`${dirname(systemNode)}:${dirname(process.execPath)}`, () => refused(envPi.found, notEngine, fx2));
|
||||
// The engine's own directory first: the sandbox shows that file at the same
|
||||
// path, so env runs the engine's node in both.
|
||||
// Entries after it are never read, so a trailing slash on it and an empty
|
||||
// entry at the end change nothing. The sandbox's /tmp starts empty, so a
|
||||
// /tmp entry with no node on the host holds none there either.
|
||||
await withPath(`/tmp/mosaic-no-such-dir:${dirname(process.execPath)}/:/usr/bin:/bin:`, async () => {
|
||||
const h2 = await started({ fx: fx2, pi: envPi.found });
|
||||
try {
|
||||
assert.equal(h2.launcher.launches.length, 1);
|
||||
assert.equal(h2.launcher.launches[0].args[0], realpathSync(envPi.cli));
|
||||
} finally {
|
||||
await h2.close();
|
||||
}
|
||||
});
|
||||
// A hand-built label that is a path never reaches a refusal line.
|
||||
await refused({ ...shim.found, label: join(fx.base, "named") }, "the pi's first line runs another node than the engine's");
|
||||
await refused({ ...shim.found, label: "MOSAIC_PI_BIN\nrefused: forged" }, "the pi's first line runs another node than the engine's");
|
||||
// Nor the fit check's own line.
|
||||
const lacking = standInPi(join(fx.base, "pi-lacking"), { modes: ["text", "json"] });
|
||||
await refused({ ...lacking.found, label: join(fx.base, "named") }, "pi 9.9.9 (the pi) lacks --mode rpc");
|
||||
// A lookup that found no pi refuses when the controller is built: its own
|
||||
// line, which names the step and the package, never a path; a hand-built
|
||||
// line with a path or a newline isn't shown.
|
||||
const unbuilt = (pi, message) =>
|
||||
assert.throws(() => controllerFor(fx, { pi }), (e) => {
|
||||
assert.equal(e.code, ENGINE_FEATURE_MISSING);
|
||||
assert.equal(e.message, message);
|
||||
return true;
|
||||
});
|
||||
unbuilt(piAt("relative/pi"), "MOSAIC_PI_BIN isn't an executable file at an absolute path");
|
||||
const loose = join(fx.base, "loose-pi");
|
||||
writeFileSync(loose, "#!/usr/bin/env node\n", { mode: 0o755 });
|
||||
unbuilt(piAt(loose), `MOSAIC_PI_BIN isn't part of the ${PI_PACKAGE} package`);
|
||||
unbuilt({ bin: null, problem: `${fx.base} has no pi` }, "the pi lookup gave no binary");
|
||||
unbuilt({ bin: null, problem: "no pi\nrefused: forged" }, "the pi lookup gave no binary");
|
||||
unbuilt({ bin: null, problem: "no pi\u2028refused: forged" }, "the pi lookup gave no binary");
|
||||
unbuilt({ bin: null, problem: "no pi\u0085refused: forged" }, "the pi lookup gave no binary");
|
||||
});
|
||||
|
||||
// ---- W11, W16, W17 ----------------------------------------------------------
|
||||
|
||||
test("W11: the controller writes no session file; only the fake engine's own appends appear", async () => {
|
||||
|
||||
@@ -9,17 +9,17 @@
|
||||
|
||||
import { test, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { appendFileSync, chmodSync, copyFileSync, mkdirSync, readFileSync, renameSync, rmdirSync, writeFileSync } from "node:fs";
|
||||
import { appendFileSync, chmodSync, mkdirSync, readFileSync, renameSync, rmdirSync, writeFileSync } from "node:fs";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { dirname, join } from "node:path";
|
||||
import { ClaimStore, FOREIGN_HOST, defaultHost, newClaimId, unitNameFor } from "../src/claim.mjs";
|
||||
import { ConversationClient } from "../src/client.mjs";
|
||||
import { AUTHORITY, PgroupLauncher, ScopeLauncher, engineExitCohort, forceStopCohort, releaseCohort, scopeAvailable, shimRequest, systemctlShow, systemdUnits } from "../src/cohort.mjs";
|
||||
import { Controller, ELIGIBILITY, TEST_ENGINE } from "../src/controller.mjs";
|
||||
import { ENGINE_ENV, ENGINE_PIN_MISMATCH, engineEnv } from "../src/pi-pin.mjs";
|
||||
import { ENGINE_ENV, ENGINE_PIN_MISMATCH, engineEnv } from "../src/pi-seal.mjs";
|
||||
import { FixtureVerifier, newId } from "../src/records.mjs";
|
||||
import { ControlClient, FakeLauncher } from "./fake-pi.mjs";
|
||||
import { FAST, REPO, assistantEntry, claimRecords, cleanupAll, controllerFor, fixture, killChildren, killShims, liveShims, noUnits, reap, receiptState, shimsGone, spawnController, started, tick } from "./harness.mjs";
|
||||
import { FAST, PI, assistantEntry, claimRecords, cleanupAll, controllerFor, fixture, killChildren, killShims, liveShims, noUnits, reap, receiptState, shimsGone, spawnController, standInPi, started, tick } from "./harness.mjs";
|
||||
|
||||
const reaped = [];
|
||||
const strays = new Set();
|
||||
@@ -111,7 +111,7 @@ async function live({ kind = "scope", barrier = null, verifier = new FixtureVeri
|
||||
fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat,
|
||||
launcher: launcher ?? (kind === "scope" ? new ScopeLauncher() : new PgroupLauncher()),
|
||||
engine: { cwd: fx.proj }, [TEST_ENGINE]: { command: process.execPath, preArgs: [FAKE_PI], env: { ...process.env, FAKE_PI_CONTROL: control, FAKE_PI_LOG: join(fx.base, "fake.log") } },
|
||||
verifier, units: kind === "scope" ? systemdUnits : noUnits, timeouts, barrier,
|
||||
verifier, units: kind === "scope" ? systemdUnits : noUnits, timeouts, barrier, pi: PI.found,
|
||||
});
|
||||
await ctrl.start();
|
||||
assert.equal(ctrl.binding.state, "active", JSON.stringify(ctrl.evidence.uncertain));
|
||||
@@ -532,14 +532,6 @@ async function provenStop(h, c = h.client) {
|
||||
return fs.stop.id;
|
||||
}
|
||||
|
||||
function pinRootCopy(fx) {
|
||||
const root = join(fx.base, "pins");
|
||||
mkdirSync(join(root, "node_modules"), { recursive: true });
|
||||
copyFileSync(join(REPO, "package-lock.json"), join(root, "package-lock.json"));
|
||||
copyFileSync(join(REPO, "node_modules", ".package-lock.json"), join(root, "node_modules", ".package-lock.json"));
|
||||
return root;
|
||||
}
|
||||
|
||||
test("K6: recover without proof, without confirmation, or with changed pins is refused", async () => {
|
||||
{
|
||||
const h = await started();
|
||||
@@ -562,8 +554,8 @@ test("K6: recover without proof, without confirmation, or with changed pins is r
|
||||
}
|
||||
{
|
||||
const fx = track(fixture());
|
||||
const pinRoot = pinRootCopy(fx);
|
||||
const h = await started({ fx, pinRoot });
|
||||
const pi = standInPi(join(fx.base, "pi"));
|
||||
const h = await started({ fx, pi: pi.found });
|
||||
try {
|
||||
const stop = await provenStop(h);
|
||||
const missing = await h.client.request("recover", { stop });
|
||||
@@ -574,13 +566,13 @@ test("K6: recover without proof, without confirmation, or with changed pins is r
|
||||
const id = issued.data.confirmation.id;
|
||||
await h.client.request("answer-confirmation", { confirmation: id, answer: "confirm" });
|
||||
assert.equal((await h.client.request("recover", { stop, confirmation: id })).refusal, "confirmation", "a confirmation for another operation");
|
||||
const lock = join(pinRoot, "package-lock.json");
|
||||
const original = readFileSync(lock, "utf8");
|
||||
const changed = JSON.parse(original);
|
||||
changed.packages["node_modules/@earendil-works/pi-coding-agent"].version = "0.0.0";
|
||||
writeFileSync(lock, JSON.stringify(changed));
|
||||
assert.equal((await h.client.confirmed("recover", { stop })).refusal, ENGINE_PIN_MISMATCH);
|
||||
writeFileSync(lock, original);
|
||||
// Other bytes in the pi the lookup finds, then the same bytes again.
|
||||
const original = readFileSync(pi.cli, "utf8");
|
||||
writeFileSync(pi.cli, `${original}// other bytes\n`);
|
||||
const changed = await h.client.confirmed("recover", { stop });
|
||||
assert.equal(changed.refusal, ENGINE_PIN_MISMATCH);
|
||||
assert.equal(changed.detail, "the engine pin or launch argv changed since the proven stop");
|
||||
writeFileSync(pi.cli, original);
|
||||
const again = await h.client.request("issue-confirmation", { operationToConfirm: "recover" });
|
||||
const once = again.data.confirmation.id;
|
||||
await h.client.request("answer-confirmation", { confirmation: once, answer: "confirm" });
|
||||
@@ -1177,7 +1169,7 @@ function scopeController(fx, { host, verifier = new FixtureVerifier({ authoritie
|
||||
fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat,
|
||||
launcher, engine: { cwd: fx.proj },
|
||||
[TEST_ENGINE]: { command: process.execPath, preArgs: [FAKE_PI], env: { ...process.env, FAKE_PI_CONTROL: join(fx.base, "fake-b.sock"), FAKE_PI_LOG: join(fx.base, "fake-b.log") } },
|
||||
verifier, units: systemdUnits, timeouts: FAST, barrier, host,
|
||||
verifier, units: systemdUnits, timeouts: FAST, barrier, host, pi: PI.found,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import { Controller, TEST_ENGINE } from "../src/controller.mjs";
|
||||
import { AUTHORITY, PgroupLauncher, ScopeLauncher, systemdUnits } from "../src/cohort.mjs";
|
||||
import { FixtureVerifier } from "../src/records.mjs";
|
||||
import { defaultHost } from "../src/claim.mjs";
|
||||
import { locate } from "../../plugins/harness/pi/binary.mjs";
|
||||
|
||||
const cfg = JSON.parse(process.argv[2]);
|
||||
const out = (v) => process.stdout.write(JSON.stringify(v) + "\n");
|
||||
@@ -79,7 +80,7 @@ try {
|
||||
engine: { cwd: fx.proj }, [TEST_ENGINE]: { command: process.execPath, preArgs: [fakePi], env: { ...process.env, ...(cfg.fakeEnv ?? {}) } },
|
||||
verifier: cfg.verifier === false ? null : new FixtureVerifier({ authorities: [AUTHORITY] }),
|
||||
units: cfg.units === "absent" ? { lookup: async () => ({ state: "absent" }) } : systemdUnits,
|
||||
host, barrier, timeouts: cfg.timeouts,
|
||||
host, barrier, timeouts: cfg.timeouts, pi: locate({ MOSAIC_PI_BIN: cfg.piBin, PATH: "/usr/bin:/bin" }),
|
||||
});
|
||||
const started = await ctrl.start();
|
||||
out({ ready: true, started, socketPath: ctrl.socketPath, incarnation: ctrl.incarnation, pid: process.pid, binding: ctrl.binding, claim: ctrl.claim ? { claimId: ctrl.claim.claimId, record: ctrl.claim.record } : null });
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// directory: fixture root, project, session file, claim root and socket
|
||||
// directory. Nothing here touches a live session.
|
||||
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync } from "node:fs";
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync, symlinkSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
import { spawnSync } from "node:child_process";
|
||||
@@ -11,6 +11,8 @@ import { ConversationClient } from "../src/client.mjs";
|
||||
import { AUTHORITY } from "../src/cohort.mjs";
|
||||
import { FixtureVerifier } from "../src/records.mjs";
|
||||
import { FakeLauncher } from "./fake-pi.mjs";
|
||||
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
|
||||
import { locate } from "../../plugins/harness/pi/binary.mjs";
|
||||
|
||||
export const REPO = resolve(import.meta.dirname, "..", "..", "..");
|
||||
export const SESSION_ID = "0f5e1c2a-1111-4222-8333-944455556666";
|
||||
@@ -20,6 +22,27 @@ export const time = (i) => new Date(Date.UTC(2026, 9, 4, 12, 0, 0) + i * 1000).t
|
||||
const tmp = mkdtempSync(join(tmpdir(), "chat03-"));
|
||||
let serial = 0;
|
||||
|
||||
// #1873 S4: a stand-in pi package (packages/harness/tests/pi-stand-in.mjs,
|
||||
// which the fit check passes unless its knobs say otherwise) under `dir`,
|
||||
// with an npm-style bin link. `found` is the lookup's answer for it, through
|
||||
// MOSAIC_PI_BIN. The test engine still launches fake-pi.mjs; the stand-in is
|
||||
// what the controller's fit check and pins read.
|
||||
export const PI_PACKAGE = "@earendil-works/pi-coding-agent";
|
||||
export function standInPi(dir, knobs = {}) {
|
||||
const pkg = join(dir, "prefix", "lib", "node_modules", PI_PACKAGE);
|
||||
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: PI_PACKAGE, version: knobs.version ?? "9.9.9", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
const cli = join(pkg, "dist", "bundle", "cli.js");
|
||||
writeFileSync(cli, piStandIn(knobs, { form: knobs.form ?? "node" }), { mode: 0o755 });
|
||||
mkdirSync(join(dir, "prefix", "bin"));
|
||||
const bin = join(dir, "prefix", "bin", "pi");
|
||||
symlinkSync(`../lib/node_modules/${PI_PACKAGE}/dist/bundle/cli.js`, bin);
|
||||
return { bin, cli, pkg, found: piAt(bin) };
|
||||
}
|
||||
export const piAt = (bin) => locate({ MOSAIC_PI_BIN: bin, PATH: "/usr/bin:/bin" });
|
||||
// The pi every fixture controller checks unless a test passes its own.
|
||||
export const PI = standInPi(join(tmp, "pi"));
|
||||
|
||||
export function cleanupAll() {
|
||||
spawnSync("chmod", ["-R", "u+rwx", tmp]);
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
@@ -51,7 +74,7 @@ export const noUnits = Object.freeze({ lookup: async () => ({ state: "absent" })
|
||||
export function controllerFor(fx, { launcher = new FakeLauncher(), verifier = new FixtureVerifier({ authorities: [AUTHORITY] }), timeouts = FAST, ...opts } = {}) {
|
||||
const ctrl = new Controller({
|
||||
fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat,
|
||||
launcher, verifier, units: noUnits, timeouts, ...opts,
|
||||
launcher, verifier, units: noUnits, timeouts, pi: PI.found, ...opts,
|
||||
});
|
||||
return { ctrl, launcher, verifier };
|
||||
}
|
||||
@@ -118,7 +141,7 @@ export function killChildren() {
|
||||
// Spawns ctrl-child.mjs. `msgs` collects its JSON lines; `next(pred)` waits
|
||||
// for one.
|
||||
export function spawnController(cfg) {
|
||||
const proc = spawn(process.execPath, [CHILD, JSON.stringify({ timeouts: FAST, ...cfg })], { stdio: ["pipe", "pipe", "pipe"] });
|
||||
const proc = spawn(process.execPath, [CHILD, JSON.stringify({ timeouts: FAST, piBin: PI.bin, ...cfg })], { stdio: ["pipe", "pipe", "pipe"] });
|
||||
childPids.add(proc.pid);
|
||||
const msgs = [];
|
||||
const waiters = new Set();
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// CHAT-03 (#1507): the pinned Pi binary, started sealed in a scratch home with
|
||||
// CHAT-03 (#1507): the repository's Pi binary, started sealed in a scratch home with
|
||||
// no credentials, answers the RPC commands the controller sends with the
|
||||
// shapes the fake engine models. No prompt is sent, so no model is called.
|
||||
//
|
||||
// It also records pinned Pi's startup append (sdk.js 240–252): a session
|
||||
// It also records Pi 0.85.1's startup append (sdk.js 240–252): a session
|
||||
// whose branch has no thinking_level_change entry gains one at every start,
|
||||
// so the leaf moves after launch and the K8 load check fails closed on it.
|
||||
//
|
||||
@@ -16,9 +16,15 @@ import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { PassThrough } from "node:stream";
|
||||
import { LineSplitter, encodeLine } from "../src/framing.mjs";
|
||||
import { PI_BIN, PI_VERSION, buildPiArgs, checkEnginePin, checkSeal } from "../src/pi-pin.mjs";
|
||||
import { buildPiArgs, checkSeal } from "../src/pi-seal.mjs";
|
||||
import { piFit } from "../../harness/src/pi-fit.mjs";
|
||||
import { locate } from "../../plugins/harness/pi/binary.mjs";
|
||||
import { levels } from "../../plugins/harness/pi/index.mjs";
|
||||
import { FakePi } from "./fake-pi.mjs";
|
||||
import { REPO, assistantEntry, header, thinkingEntry, userEntry } from "./harness.mjs";
|
||||
import { assistantEntry, header, thinkingEntry, userEntry } from "./harness.mjs";
|
||||
|
||||
// The repository's pi: with no MOSAIC_PI_BIN and no PATH the lookup ends there.
|
||||
const REPO_PI = locate({});
|
||||
|
||||
const scratch = mkdtempSync(join(tmpdir(), "chat03-smoke-"));
|
||||
after(() => rmSync(scratch, { recursive: true, force: true }));
|
||||
@@ -90,9 +96,11 @@ async function realPi(tag, sessionFile, { project = "proj", trusted = null, afte
|
||||
assert.equal(existsSync(join(home, ".pi", "agent", "auth.json")), false);
|
||||
assert.deepEqual(readdirSync(agent), [], "the agent dir starts empty");
|
||||
if (trusted) writeFileSync(join(agent, "trust.json"), JSON.stringify({ [realpathSync(join(scratch, trusted))]: true }));
|
||||
const args = buildPiArgs({ sessionFile });
|
||||
checkSeal(args);
|
||||
const pi = spawn(process.execPath, [join(REPO, PI_BIN), ...args, ...after], { cwd: join(scratch, project), env, stdio: ["pipe", "pipe", "pipe"] });
|
||||
const fit = await piFit(REPO_PI, { levels: levels() });
|
||||
assert.equal(fit.ok, true, fit.message);
|
||||
const args = buildPiArgs({ sessionFile, noMcp: fit.noMcp });
|
||||
checkSeal(args, { noMcp: fit.noMcp });
|
||||
const pi = spawn(process.execPath, [realpathSync(REPO_PI.bin), ...args, ...after], { cwd: join(scratch, project), env, stdio: ["pipe", "pipe", "pipe"] });
|
||||
let stderr = "";
|
||||
pi.stderr.on("data", (c) => (stderr += c));
|
||||
const exited = new Promise((r) => pi.on("exit", (code, signal) => r({ code, signal })));
|
||||
@@ -118,11 +126,15 @@ function fakePi(sessionFile) {
|
||||
const lastId = (file) => JSON.parse(readFileSync(file, "utf8").trim().split("\n").at(-1)).id;
|
||||
const typesOf = (data) => Object.fromEntries(Object.entries(data ?? {}).map(([k, v]) => [k, v === null ? "null" : Array.isArray(v) ? "array" : typeof v]));
|
||||
|
||||
test("the engine pin holds for the installed package", () => {
|
||||
assert.deepEqual(checkEnginePin(REPO).version, PI_VERSION);
|
||||
test("the repository's pi passes the fit check and gives a version and a digest", async () => {
|
||||
assert.equal(REPO_PI.label, "the repository's pi");
|
||||
const fit = await piFit(REPO_PI, { levels: levels() });
|
||||
assert.equal(fit.ok, true, fit.message);
|
||||
assert.equal(fit.version, "0.85.1");
|
||||
assert.match(fit.digest, /^[0-9a-f]{64}$/);
|
||||
});
|
||||
|
||||
test("pinned Pi, sealed and without credentials, answers the controller's commands with the shapes the fake models", async () => {
|
||||
test("the repository's Pi, sealed and without credentials, answers the controller's commands with the shapes the fake models", async () => {
|
||||
const entries = [thinkingEntry(), userEntry("a1b2c3d4", "f0e1d2c3", "hello"), assistantEntry("b2c3d4e5", "a1b2c3d4", "hi")];
|
||||
const realFile = session("real.jsonl", entries);
|
||||
const fakeFile = session("fake.jsonl", entries);
|
||||
@@ -155,7 +167,7 @@ test("pinned Pi, sealed and without credentials, answers the controller's comman
|
||||
assert.equal(r.command, f.command, type);
|
||||
assert.equal(r.success, f.success, type);
|
||||
const rt = typesOf(r.data), ft = typesOf(f.data);
|
||||
for (const [k, t] of Object.entries(ft)) assert.equal(rt[k], t, `${type}.${k}: the fake models a field pinned Pi doesn't send that way`);
|
||||
for (const [k, t] of Object.entries(ft)) assert.equal(rt[k], t, `${type}.${k}: the fake models a field the repository's Pi doesn't send that way`);
|
||||
}
|
||||
// clear_queue: one queue_update with both queues empty precedes the response, in both.
|
||||
for (const side of [real, fake]) {
|
||||
@@ -165,7 +177,7 @@ test("pinned Pi, sealed and without credentials, answers the controller's comman
|
||||
}
|
||||
});
|
||||
|
||||
test("sealed, pinned Pi ignores a trusted project's .pi resources; --approve past the seal would load them, and checkSeal refuses it (Filbert F2 on #1522)", async () => {
|
||||
test("sealed Pi ignores a trusted project's .pi resources; --approve past the seal would load them, and checkSeal refuses it (Filbert F2 on #1522)", async () => {
|
||||
const skill = join(scratch, "trusted", ".pi", "skills", "probe");
|
||||
mkdirSync(skill, { recursive: true });
|
||||
writeFileSync(join(skill, "SKILL.md"), "---\nname: probe\ndescription: A project skill that only a trusted load offers.\n---\nprobe\n");
|
||||
@@ -183,7 +195,7 @@ test("sealed, pinned Pi ignores a trusted project's .pi resources; --approve pas
|
||||
}
|
||||
});
|
||||
|
||||
test("pinned Pi appends thinking_level_change at start when the branch lacks one, so the leaf moves (K8 then fails closed)", async () => {
|
||||
test("the repository's Pi appends thinking_level_change at start when the branch lacks one, so the leaf moves (K8 then fails closed)", async () => {
|
||||
const entries = [userEntry("a1b2c3d4", null, "hello"), assistantEntry("b2c3d4e5", "a1b2c3d4", "hi")];
|
||||
const realFile = session("bare-real.jsonl", entries);
|
||||
const fakeFile = session("bare-fake.jsonl", entries);
|
||||
@@ -197,5 +209,5 @@ test("pinned Pi appends thinking_level_change at start when the branch lacks one
|
||||
assert.notEqual(side.responses.get_tree.data.leafId, "b2c3d4e5", "the leaf moved off the loaded leaf");
|
||||
assert.equal(side.responses.get_tree.data.leafId, added[0].id);
|
||||
}
|
||||
if (process.env.CHAT03_SMOKE_OUT) writeFileSync(process.env.CHAT03_SMOKE_OUT, JSON.stringify({ pi: PI_VERSION, commands: COMMANDS, exchanges }, null, 2) + "\n");
|
||||
if (process.env.CHAT03_SMOKE_OUT) writeFileSync(process.env.CHAT03_SMOKE_OUT, JSON.stringify({ pi: (await piFit(REPO_PI, { levels: levels() })).version, commands: COMMANDS, exchanges }, null, 2) + "\n");
|
||||
});
|
||||
@@ -5,14 +5,16 @@
|
||||
import { test, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { PassThrough } from "node:stream";
|
||||
import { realpathSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { Controller, TEST_ENGINE, REPO_ROOT, HANDLED_WITHOUT_RUN, ACK_WITHOUT_START, NO_TURN, RUN_OVERLAP, TRANSPORT_UNKNOWN } from "../src/controller.mjs";
|
||||
import { AUTHORITY } from "../src/cohort.mjs";
|
||||
import { FixtureVerifier, sha256 } from "../src/records.mjs";
|
||||
import { ENGINE_ENV, PI_BIN, SEAL_FLAGS, UNSEALED_ENGINE, checkSeal } from "../src/pi-pin.mjs";
|
||||
import { ENGINE_ENV, SEAL_FLAGS, UNSEALED_ENGINE, checkSeal } from "../src/pi-seal.mjs";
|
||||
import { locate } from "../../plugins/harness/pi/binary.mjs";
|
||||
import { LineSplitter, encodeLine, parseLine } from "../src/framing.mjs";
|
||||
import { BUSY_ERROR, FakeLauncher, FakePi } from "./fake-pi.mjs";
|
||||
import { fixture, started, receiptState, outcomeUnknownPush, sessionText, cleanupAll, tick, noUnits, FAST } from "./harness.mjs";
|
||||
import { fixture, started, receiptState, outcomeUnknownPush, sessionText, cleanupAll, tick, noUnits, FAST, PI, PI_PACKAGE, standInPi } from "./harness.mjs";
|
||||
|
||||
after(() => cleanupAll());
|
||||
|
||||
@@ -982,12 +984,38 @@ test("N24: the seal is an allow-list: --extension, a missing --no-* flag, a seco
|
||||
}
|
||||
});
|
||||
|
||||
test("N24c: when --help lists --no-mcp the launch passes it after the seal flags, and the seal then requires it (#1873 S4)", async () => {
|
||||
const fx = fixture();
|
||||
const pi = standInPi(join(fx.base, "pi"), { extraFlags: ["--no-mcp"] });
|
||||
const h = await started({ fx, pi: pi.found });
|
||||
try {
|
||||
const args = h.launcher.launches[0].args.slice(1);
|
||||
assert.deepEqual(args.slice(0, SEAL_FLAGS.length + 5), ["--mode", "rpc", ...SEAL_FLAGS, "--no-mcp", "--session", fx.sessionFile]);
|
||||
assert.equal(h.ctrl.noMcp, true);
|
||||
assert.equal(checkSeal(args, { noMcp: true }), true);
|
||||
const without = args.filter((a) => a !== "--no-mcp");
|
||||
assert.throws(() => checkSeal(without, { noMcp: true }), (e) => e.code === UNSEALED_ENGINE);
|
||||
assert.throws(() => checkSeal(args), (e) => e.code === UNSEALED_ENGINE, "--no-mcp where help lacks it");
|
||||
} finally {
|
||||
await h.close();
|
||||
}
|
||||
// The shared stand-in's help lacks --no-mcp, so its launch has none.
|
||||
const plain = await started({ fx: fixture() });
|
||||
try {
|
||||
assert.equal(plain.launcher.launches[0].args.includes("--no-mcp"), false);
|
||||
assert.equal(plain.ctrl.noMcp, false);
|
||||
} finally {
|
||||
await plain.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("N24b: the seal covers the engine command and environment: config can't name either, the env is built from names, and a mutated command is refused at bind", async () => {
|
||||
const fx = fixture();
|
||||
const make = (extra) => new Controller({ fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat, launcher: new FakeLauncher(), units: noUnits, timeouts: FAST, ...extra });
|
||||
// The plain engine option takes extraArgs, cwd and envKeys; the command,
|
||||
// pre-arguments and environment are the controller's (I3, #1507).
|
||||
for (const engine of [{ command: "/bin/sh" }, { preArgs: [join(REPO_ROOT, PI_BIN)] }, { command: process.execPath, preArgs: [join(REPO_ROOT, PI_BIN), "--extension", "x"] }, { env: {} }, { env: process.env }, null, [], "pi"]) {
|
||||
const repoCli = join(REPO_ROOT, "node_modules", PI_PACKAGE, "dist", "bundle", "cli.js");
|
||||
for (const engine of [{ command: "/bin/sh" }, { preArgs: [repoCli] }, { command: process.execPath, preArgs: [repoCli, "--extension", "x"] }, { env: {} }, { env: process.env }, null, [], "pi"]) {
|
||||
assert.throws(() => make({ engine }), (e) => e.code === UNSEALED_ENGINE, JSON.stringify(engine));
|
||||
}
|
||||
// A configuration is JSON, which can't carry the symbol-keyed test engine.
|
||||
@@ -1001,7 +1029,7 @@ test("N24b: the seal covers the engine command and environment: config can't nam
|
||||
assert.throws(() => make({ [TEST_ENGINE]: { command: process.execPath, preArgs: ["fake.mjs", "-e", "x"], env: {} } }), (e) => e.code === UNSEALED_ENGINE);
|
||||
assert.throws(() => make({ [TEST_ENGINE]: { command: "", preArgs: [], env: {} } }), (e) => e.code === UNSEALED_ENGINE);
|
||||
|
||||
// The launch: this Node, the pinned bin, and an environment of named keys
|
||||
// The launch: this Node, the real file of the pi the lookup found, and an environment of named keys
|
||||
// only, whatever the controller's own environment holds.
|
||||
const planted = { NODE_OPTIONS: "--require /tmp/x.cjs", LD_PRELOAD: "/tmp/x.so", PI_PACKAGE_DIR: "/tmp/pkg", ZAI_API_KEY: "zai-test-value", OTHER_API_KEY: "other-test-value" };
|
||||
const saved = Object.fromEntries(Object.keys(planted).map((k) => [k, process.env[k]]));
|
||||
@@ -1015,7 +1043,8 @@ test("N24b: the seal covers the engine command and environment: config can't nam
|
||||
try {
|
||||
const l = h.launcher.launches[0];
|
||||
assert.equal(l.command, process.execPath);
|
||||
assert.equal(l.args[0], join(REPO_ROOT, PI_BIN));
|
||||
assert.equal(l.args[0], realpathSync(PI.bin));
|
||||
assert.equal(l.args[0], h.ctrl.engine.preArgs[0]);
|
||||
assert.equal(l.env.ZAI_API_KEY, "zai-test-value");
|
||||
for (const k of Object.keys(l.env)) assert.ok(ENGINE_ENV.includes(k) || k === "ZAI_API_KEY", k);
|
||||
for (const k of ["NODE_OPTIONS", "LD_PRELOAD", "PI_PACKAGE_DIR", "OTHER_API_KEY"]) assert.equal(k in l.env, false, k);
|
||||
@@ -1027,7 +1056,21 @@ test("N24b: the seal covers the engine command and environment: config can't nam
|
||||
// A command changed after construction is refused at bind; nothing launches.
|
||||
const fx2 = fixture();
|
||||
const launcher = new FakeLauncher();
|
||||
const ctrl = new Controller({ fixtureRoot: fx2.base, claimRoot: fx2.claimRoot, socketDir: fx2.socketDir, sessionFile: fx2.sessionFile, seat: fx2.seat, launcher, units: noUnits, timeouts: FAST });
|
||||
// With no pi option the controller takes the one the lookup finds in its own
|
||||
// environment, read when it is built (#1873 S4): here MOSAIC_PI_BIN names a
|
||||
// stand-in, which no other step of the lookup would find.
|
||||
const own = standInPi(join(fx2.base, "env-pi"));
|
||||
const savedBin = process.env.MOSAIC_PI_BIN;
|
||||
process.env.MOSAIC_PI_BIN = own.bin;
|
||||
let ctrl;
|
||||
try {
|
||||
ctrl = new Controller({ fixtureRoot: fx2.base, claimRoot: fx2.claimRoot, socketDir: fx2.socketDir, sessionFile: fx2.sessionFile, seat: fx2.seat, launcher, units: noUnits, timeouts: FAST });
|
||||
assert.deepEqual(ctrl.pi, locate(process.env));
|
||||
} finally {
|
||||
if (savedBin === undefined) delete process.env.MOSAIC_PI_BIN; else process.env.MOSAIC_PI_BIN = savedBin;
|
||||
}
|
||||
assert.equal(ctrl.pi.label, "MOSAIC_PI_BIN");
|
||||
assert.deepEqual(ctrl.engine.preArgs, [realpathSync(own.cli)]);
|
||||
// Closed in finally: a start that wrongly succeeds holds the socket open.
|
||||
try {
|
||||
ctrl.engine.command = "/bin/sh";
|
||||
|
||||
@@ -30,7 +30,12 @@ scripts/discord-service.sh render | install | uninstall | status <binding>
|
||||
`--repo` for you; the CLI also takes `--config PATH`.
|
||||
|
||||
- `check` validates the binding, the token file (0600, regular, not a
|
||||
symlink), the context files and the pi install; reads the bot, the guild and
|
||||
symlink), the context files and the pi the harness lookup finds
|
||||
(`MOSAIC_PI_BIN`, else the first `pi` on `PATH`, else the repository's;
|
||||
#1873 S4). `--repo` no longer picks the pi, and the CLI runs no fit check
|
||||
on it yet, so a pi on `PATH` is used as it is. The service wrapper still
|
||||
checks that the copy has its own `node_modules/.bin/pi`, but the CLI may run
|
||||
another pi that the lookup finds first. It then reads the bot, the guild and
|
||||
every listed channel over REST; opens one gateway connection, waits for
|
||||
READY, closes it. Nothing is sent to a channel. Close code 4014 is reported
|
||||
as the message-content intent not being granted in the developer portal.
|
||||
|
||||
@@ -46,7 +46,7 @@
|
||||
// 3 refused by a brake (STOP present or the binding held; a supervisor must
|
||||
// not retry); 4 usage.
|
||||
|
||||
import { existsSync, mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
||||
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import { DiscordError, printable } from "./errors.mjs";
|
||||
@@ -57,6 +57,7 @@ import { defaultConfigPath, loadDataRoot, bindingPath, bindingDataDir, loadBindi
|
||||
import { createRest } from "./rest.mjs";
|
||||
import { createGateway, CONNECTOR_INTENTS } from "./gateway.mjs";
|
||||
import { createEngine, buildPiArgs, sessionContinues } from "./engine-pi.mjs";
|
||||
import { locate } from "../../plugins/harness/pi/binary.mjs";
|
||||
import { TOOLS_ENV, enabledToolNames } from "./tools.mjs";
|
||||
import { createSetsparkApi } from "./setspark.mjs";
|
||||
import { assembleContext } from "./context.mjs";
|
||||
@@ -134,8 +135,11 @@ function prepare(opts) {
|
||||
checkLibraryEngine(dirname(opts.config), binding);
|
||||
const contextFiles = resolveContextFiles(binding, opts.repo);
|
||||
const toolRoots = resolveToolRoots(binding, { dataRoot });
|
||||
const pi = join(opts.repo, "node_modules", ".bin", "pi");
|
||||
if (!existsSync(pi)) throw new DiscordError("pi not found at node_modules/.bin/pi in the repository; run npm ci in the repository");
|
||||
// #1873 S4: the pi the harness lookup finds (MOSAIC_PI_BIN, the first pi on PATH, the
|
||||
// repository's), not only the repository's copy.
|
||||
const found = locate(process.env);
|
||||
if (!found.bin) throw new DiscordError(found.problem);
|
||||
const pi = found.bin;
|
||||
const journalDir = bindingDataDir(dataRoot, binding.name);
|
||||
const sessionDir = join(dataRoot, "sessions", `discord-${binding.name}`);
|
||||
return { dataRoot, bindingFile, binding, contextFiles, toolRoots, pi, journalDir, sessionDir };
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// Shared fakes for the offline suite. No network, no token, no model.
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, rmSync, chmodSync } from "node:fs";
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, rmSync, chmodSync, symlinkSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { after } from "node:test";
|
||||
@@ -53,12 +53,19 @@ export function binding(overrides = {}) {
|
||||
return validateBinding(rawBinding(overrides));
|
||||
}
|
||||
|
||||
// A fake repository with the files prepare() looks for.
|
||||
// A fake repository with the files prepare() looks for. Its pi is a package the
|
||||
// harness lookup accepts, and MOSAIC_PI_BIN names it for every CLI this test
|
||||
// process starts (#1873 S4), so no test reads the host's own pi.
|
||||
export function makeRepo(root) {
|
||||
const repo = join(root, "repo");
|
||||
const pkg = join(repo, "node_modules", "@earendil-works", "pi-coding-agent");
|
||||
mkdirSync(join(repo, "node_modules", ".bin"), { recursive: true });
|
||||
mkdirSync(join(pkg, "dist"), { recursive: true });
|
||||
mkdirSync(join(repo, "contracts"), { recursive: true });
|
||||
writeFileSync(join(repo, "node_modules", ".bin", "pi"), "#!/bin/sh\nexit 0\n", { mode: 0o755 });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "0.0.0-test", bin: { pi: "dist/cli.js" } }));
|
||||
writeFileSync(join(pkg, "dist", "cli.js"), "#!/bin/sh\nexit 0\n", { mode: 0o755 });
|
||||
symlinkSync("../@earendil-works/pi-coding-agent/dist/cli.js", join(repo, "node_modules", ".bin", "pi"));
|
||||
process.env.MOSAIC_PI_BIN = join(repo, "node_modules", ".bin", "pi");
|
||||
writeFileSync(join(repo, "contracts", "CONSTITUTION.md"), "# constitution\nbe good\n");
|
||||
return repo;
|
||||
}
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {spawnSync} from 'node:child_process';
|
||||
import {writeFileSync,chmodSync,unlinkSync,existsSync} from 'node:fs';
|
||||
import {join} from 'node:path';
|
||||
import {writeFileSync,chmodSync,unlinkSync,existsSync,mkdirSync,symlinkSync} from 'node:fs';
|
||||
import {dirname,join} from 'node:path';
|
||||
import {fileURLToPath} from 'node:url';
|
||||
import {makeRoot,makeRepo,makeDeployment} from './helpers.mjs';
|
||||
const CLI=fileURLToPath(new URL('../src/cli.mjs',import.meta.url));
|
||||
function setup(){
|
||||
const root=makeRoot(),repo=makeRepo(root),d=makeDeployment(root,{}, {tokenContent:'MTAw.abcdefghijklmnopqrstuvwxyz0123456789'}),calls=join(root,'network-called'),preload=join(root,'no-network.mjs');
|
||||
writeFileSync(preload,`import {writeFileSync} from 'node:fs';const refuse=()=>{writeFileSync(${JSON.stringify(calls)},'called');throw Error('fixture refuses network')};globalThis.fetch=refuse;globalThis.WebSocket=class{constructor(){refuse()}};`);
|
||||
const run=(command='check',args=[])=>spawnSync(process.execPath,['--import',preload,CLI,command,d.raw.name,'--config',d.config,'--repo',repo,...args],{encoding:'utf8',timeout:5000});
|
||||
const run=(command='check',args=[],env=process.env)=>spawnSync(process.execPath,['--import',preload,CLI,command,d.raw.name,'--config',d.config,'--repo',repo,...args],{encoding:'utf8',timeout:5000,env});
|
||||
return {root,repo,d,calls,run};
|
||||
}
|
||||
test('check --offline validates local fixture without REST or gateway calls',()=>{
|
||||
const s=setup(),r=s.run('check',['--offline']);assert.equal(r.status,0,r.stderr);assert.match(r.stdout,/token file mode 0600 ok; STOP absent/);assert.match(r.stdout,/offline check passed; no REST or gateway calls were made\n$/);assert.ok(!existsSync(s.calls));
|
||||
// #1873 S4: the fixture's own pi, which the helpers name in MOSAIC_PI_BIN, never the host's.
|
||||
assert.ok(r.stdout.includes(`pi ${join(s.repo,'node_modules','.bin','pi')};`),r.stdout);
|
||||
// Online check still reaches REST: the trap fails it immediately.
|
||||
const online=s.run();assert.equal(online.status,1);assert.ok(existsSync(s.calls));assert.match(online.stderr,/fixture refuses network/);
|
||||
});
|
||||
@@ -24,3 +26,12 @@ test('check --offline refuses token mode and missing context under the selected
|
||||
test('run --offline is a usage refusal before reading config or networking',()=>{
|
||||
const s=setup();unlinkSync(s.d.config);const r=s.run('run',['--offline']);assert.equal(r.status,4,r.stderr);assert.match(r.stderr,/--offline/);assert.ok(!existsSync(s.calls));
|
||||
});
|
||||
test('#1873 S4: check takes the pi the harness lookup finds; MOSAIC_PI_BIN names it, and a bad one refuses with the lookup line, no path',()=>{
|
||||
// A pi package the way npm installs one under a prefix: its bin is linked from prefix/bin.
|
||||
const s=setup(),pkg=join(s.root,'prefix','lib','node_modules','@earendil-works','pi-coding-agent'),bin=join(s.root,'prefix','bin','pi');
|
||||
mkdirSync(join(pkg,'dist'),{recursive:true});mkdirSync(dirname(bin),{recursive:true});
|
||||
writeFileSync(join(pkg,'package.json'),JSON.stringify({name:'@earendil-works/pi-coding-agent',version:'9.9.9',bin:{pi:'dist/cli.js'}}));
|
||||
writeFileSync(join(pkg,'dist','cli.js'),'#!/bin/sh\nexit 0\n',{mode:0o755});symlinkSync(join(pkg,'dist','cli.js'),bin);
|
||||
const named=s.run('check',['--offline'],{...process.env,MOSAIC_PI_BIN:bin});assert.equal(named.status,0,named.stderr);assert.ok(named.stdout.includes(`pi ${bin};`),named.stdout);
|
||||
const bad=s.run('check',['--offline'],{...process.env,MOSAIC_PI_BIN:'relative/pi'});assert.equal(bad.status,2,bad.stderr);assert.match(bad.stderr,/MOSAIC_PI_BIN isn't an executable file at an absolute path/);assert.ok(!bad.stderr.includes(s.root));assert.ok(!existsSync(s.calls));
|
||||
});
|
||||
+116
-7
@@ -18,13 +18,122 @@ Every harness layer below is a row S0 line
|
||||
(`docs/plans/2026-10-04_slice-1.md`, "What S6 can rely on"), and each
|
||||
bundle's `manifest.json` names the lines it relies on (`reliesOn`):
|
||||
|
||||
| | Pi | Claude Code |
|
||||
|---|---|---|
|
||||
| 1. block a tool | `tool_call` handler returns a block | PreToolUse command hook exits 2; `--bare` is never passed |
|
||||
| 2. gate crash | a throw in the handler blocks | the hook runs as `<gate> \|\| exit 2` |
|
||||
| 3. gate missing | a missing `-e` file refuses to start | the wrapper blocks |
|
||||
| 4. gate hang | the runner's wall clock plus the `agent_end` marker | `timeout -k 2 10 <gate> \|\| exit 2`, hook timeout 20 |
|
||||
| 5. bash | limited only by the tool limit | limited only by the tool limit |
|
||||
| | Pi | Claude Code | How pi's line is checked |
|
||||
|---|---|---|---|
|
||||
| 1. block a tool | `tool_call` handler returns a block | PreToolUse command hook exits 2; `--bare` is never passed | suites, on the repository's pi (`pi-session.test.mjs`) |
|
||||
| 2. gate crash | a throw in the handler blocks | the hook runs as `<gate> \|\| exit 2` | suites, on the repository's pi |
|
||||
| 3. gate missing | a missing `-e` file refuses to start | the wrapper blocks | at every start, by the fit check, on the pi that runs; and suites |
|
||||
| 4. gate hang | the runner's wall clock plus the `agent_end` marker | `timeout -k 2 10 <gate> \|\| exit 2`, hook timeout 20 | suites, on the repository's pi |
|
||||
| 5. bash | limited only by the tool limit | limited only by the tool limit | suites, on the repository's pi |
|
||||
|
||||
Pi is not pinned (#1873 pi-unpin). Before a session starts, `preflight`
|
||||
awaits the fit check in `src/pi-fit.mjs` on the binary the module found
|
||||
(the Triage host and route await the module's `ready` instead), and
|
||||
`env` reads its pass. It runs that binary in a sandbox with the network
|
||||
unshared and calls no model. Each run is a child process the check
|
||||
awaits, up to four at a time, so the bus's event loop keeps running
|
||||
while it does; callers that ask while a check runs share it. Each run
|
||||
gets the environment a bus session's pi gets, built by the same code
|
||||
(the launcher's `ENV_ALLOW` and `sessionEnv`, the sandbox's
|
||||
`sessionSetenv`, the runner's `adapterEnv` and the pi module's
|
||||
`harnessEnv`) from the bus process's own environment, less the provider
|
||||
keys, and starts the binary through `/bin/sh` as the adapter does. So
|
||||
`HOME` is a scratch home, `TMPDIR` is `/tmp` bound to scratch, a run has
|
||||
`TERM`, `TZ` or `LC_ALL` only when the bus has it (a bus started by its
|
||||
unit has none), and no run sets `PI_CODING_AGENT_DIR`, which no session
|
||||
has: pi's agent directory is `$HOME/.pi/agent`, where the user sources
|
||||
are planted. A test fails when the fit runs' names, or the values the
|
||||
code sets, differ from a bus session's, apart from the provider
|
||||
variables, for a login shell's host and a unit's. It needs
|
||||
`--help` to list, in its option column, every flag the adapter passes
|
||||
(with rpc mode and each thinking level), and a missing `-e` file to stop
|
||||
the start in rpc and in `-p`. The planted ambient sources must stay
|
||||
unloaded with the seal flags on (skills and prompt templates are read
|
||||
from `get_commands` in rpc and from the `-e` extension's command list in
|
||||
`-p`): extensions, MCP
|
||||
servers, skills and prompt templates (found in their directories, listed
|
||||
in a `settings.json`, or in a local package a `settings.json` lists),
|
||||
context files (`AGENTS.md`, `CLAUDE.md`, `SYSTEM.md`) and a trusted
|
||||
project's `.pi/SYSTEM.md` and `.pi/APPEND_SYSTEM.md`. The project has a
|
||||
saved trust, so only `--no-approve` keeps those two out. An `-e`
|
||||
extension writes the system prompt, command names and session id it sees
|
||||
at `session_start`; the sealed runs must show the `--system-prompt` file
|
||||
and none of the planted sources. A control run passes the seal flags less every loader flag
|
||||
(`--no-approve`, `--no-extensions`, `--no-skills`,
|
||||
`--no-prompt-templates`, `--no-context-files`, and `--no-mcp` when
|
||||
listed), so it keeps `--offline`, `--no-themes`, `--no-tools` and
|
||||
`--no-session`, and the same `-e` and `--system-prompt`. It must show
|
||||
each kind of source (an MCP server only when `--help` lists `--no-mcp`)
|
||||
and answer `get_state` and `get_commands`, and its `-e` extension must
|
||||
list the planted skill and prompt template, or the check can't tell and
|
||||
refuses. For every kind but the trusted project files, only a source
|
||||
outside the project counts there, since a project source can wait on
|
||||
trust and `--no-approve` would then hide a seal flag the binary ignores.
|
||||
Then one sealed rpc run per loader flag leaves out only that flag: its
|
||||
kind must show, and no other kind (an MCP server may show beside an
|
||||
extension when `--help` lists no `--no-mcp`, since pi then ties MCP to
|
||||
`--no-extensions`). So each loader flag is shown to keep its own kind
|
||||
out, and no other seal flag, nor their order, can stand in for it. Five
|
||||
sealed `-p` runs have a bus session's shape: the arguments the adapter
|
||||
passes for a session, in its order, from `sessionShape` in
|
||||
`src/pi-fit.mjs`, with two `-e` and `--session-dir`. The adapter's four
|
||||
session branches (a tool list or `--no-tools`, `-c` over a stored
|
||||
session or none, `--thinking` or none, `--mode json` or none) are spread
|
||||
over the five runs so every pair of them is passed together. A `-c` run
|
||||
gets a stored session and must resume it. Each must load both `-e`
|
||||
files and show no planted source. A test drives the adapter, in a bus
|
||||
session's environment, through every branch set with no tools, one or
|
||||
two, `-c` over a pi-named session file or an unrelated file, each
|
||||
thinking level and each provider a session can name. It runs over a
|
||||
login shell's host and a unit's, and fails on an argv `sessionShape`
|
||||
doesn't build, a pair no run passes, or a variable the adapter reads
|
||||
that no session sets. So a branch on `TZ` or `LC_ALL` being unset, or on
|
||||
`TERM` missing from the environment, turns it red. (Bash run as
|
||||
`/bin/sh` sets an unset `TERM` to `dumb` for the shell without exporting
|
||||
it, so `${TERM:-}` in the adapter is never empty, in a session or here.)
|
||||
It can't see a branch keyed on a
|
||||
value it doesn't try (a model, a path, another tool or provider). A last `-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
|
||||
levels beyond `low`, provider, model, key, `-e` files, request). The
|
||||
runs are offline with no key, so nothing past the start is tried. A pi
|
||||
built to act one way in the fit runs and another way with those is
|
||||
outside what a start-time check can catch. A run killed for taking
|
||||
over 20 s or printing over 4 MB can't be checked and says which. A
|
||||
`--version` that fails or hangs past 3 s only leaves no label. Pi
|
||||
0.85.1 appends the agent directory's `APPEND_SYSTEM.md` whatever the
|
||||
flags, so the check doesn't plant one. Any failure refuses the launch with
|
||||
`harness-feature-missing` and one line that names the binary by its label,
|
||||
for example `pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-context-files`. When
|
||||
`--help` lists `--no-mcp`, the adapter passes it too. A pass is cached in
|
||||
memory by a digest of the package's files (and npm's lock files, for
|
||||
the repository's copy) and the names in the runs' environment, so a bus
|
||||
restarted under other names checks again. Lines 1, 2, 4 and 5 are not
|
||||
checked at start; a pi other than the repository's copy relies on them
|
||||
unproved until a scripted-model fit run exists (design section 5, S6).
|
||||
|
||||
Claude Code also runs with `--restricted`: no user, project or local
|
||||
settings, file tools confined to the working directory, and no `CLAUDE.md`
|
||||
|
||||
@@ -0,0 +1,863 @@
|
||||
// #1873 pi-unpin S3: the start-time fit check for a pi binary the harness module found. Nothing
|
||||
// here compares a version. The check runs the binary only inside a sandbox with the network
|
||||
// unshared, the found package mounted as a session mounts it, and a bus session's environment
|
||||
// (fitEnv below), so its agent directory is $HOME/.pi/agent under a scratch HOME, as a session's
|
||||
// is. No model is called and no provider key is passed. It asks:
|
||||
//
|
||||
// --version a label for the refusal line and the record; not a gate
|
||||
// --help lists, in its option column, every flag Mosaic passes, with rpc mode and each
|
||||
// thinking level
|
||||
// control rpc, planted ambient sources, a saved trust for the project, the seal flags less
|
||||
// every loader flag (the ones that each keep one kind of source out: --no-approve,
|
||||
// --no-extensions, --no-skills, --no-prompt-templates, --no-context-files, and
|
||||
// --no-mcp when --help lists it), the --system-prompt file the sealed runs pass, and
|
||||
// one -e extension that writes what it sees at session_start: the system prompt, the
|
||||
// command names and the session id. The binary answers get_state and get_commands,
|
||||
// its -e extension lists the planted skill and prompt template (the -p runs see
|
||||
// those kinds only that way), and it shows each kind of planted source:
|
||||
// an extension, a skill, a prompt template, a context file, a trusted project file
|
||||
// (the project's .pi/APPEND_SYSTEM.md, which only the trust --no-approve must undo
|
||||
// lets in), and an MCP server when --help lists --no-mcp. Only a source outside the
|
||||
// project counts for the other kinds, since a project source can wait on trust. A
|
||||
// kind it doesn't show could not be told apart from a sealed one.
|
||||
// sealed rpc the adapter's seal flags (and --no-mcp when --help lists it), the -e extension and
|
||||
// --system-prompt. The binary answers both, loads that extension (a session's gate
|
||||
// comes in that way), uses the --system-prompt file, and shows no planted source.
|
||||
// one out one more sealed rpc run per loader flag, with every seal flag but that one. The
|
||||
// kind it keeps out must show, and no other kind (pi without --no-mcp ties MCP to
|
||||
// --no-extensions, so an MCP server may show beside an extension). So each loader flag
|
||||
// is shown to keep its own kind out, and no other seal flag, nor the order they come
|
||||
// in, can stand in for it.
|
||||
// session five -p runs in a bus session's shape, with the arguments adapters/pi/adapter.sh
|
||||
// passes for a session, in its order (sessionShape below): two -e, --session-dir in
|
||||
// place of --no-session, a fixed --provider and --model, -p and a message. The
|
||||
// adapter's four session branches (a tool list or --no-tools, -c over a stored
|
||||
// session or none, --thinking or none, --mode json or none) are spread over the five
|
||||
// runs so each pair of them is passed together (SESSION_RUNS). A -c run gets a
|
||||
// stored session for the project and must resume it. Each run loads both -e
|
||||
// extensions, uses the --system-prompt file, and shows no
|
||||
// planted source. A test drives the adapter through every branch set, tool list
|
||||
// length and thinking level, and fails on an argv sessionShape doesn't build or a
|
||||
// pair no run passes.
|
||||
// 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
|
||||
//
|
||||
// Every run gets a fresh scratch directory with the sources planted again, so no run sees what
|
||||
// another one left. The directory's name carries the pid, start time and pid namespace of the
|
||||
// process that made it, and each check first stops the processes and removes the directories a
|
||||
// process no longer running left (sweepScratch below). Every run dies with the process that runs
|
||||
// the check (checkArgv in packages/seat/src/sandbox.mjs), at once when unshare runs here and at
|
||||
// the next check's sweep when it doesn't.
|
||||
// The runs share no state: up to FIT_CONCURRENCY of them run at once,
|
||||
// each a child process the check awaits, so the process that runs the check (the bus) keeps
|
||||
// answering while it runs. --version and --help go first; the rest start in the order above and
|
||||
// are judged in that order, so the first failure is the one a refusal names, and the runs still
|
||||
// going are killed. Each run passes the same -e and --system-prompt; the other runs differ only by
|
||||
// their seal flags, their mode, the -p message and the missing -e file. Each run starts the binary
|
||||
// through /bin/sh, as the adapter does, with fitEnv's variables: the environment a bus session's pi
|
||||
// gets, built by the code that builds it (the launcher's ENV_ALLOW and sessionEnv, the sandbox's
|
||||
// sessionSetenv, the runner's adapterEnv and the pi module's harnessEnv) from the calling process's
|
||||
// own environment, less the provider variables. So a fit run has a name only when a session in that
|
||||
// process has it (TERM, TZ and LC_ALL only when the bus has them), no run sets a variable a session
|
||||
// lacks, such as PI_CODING_AGENT_DIR, and a test holds the two together.
|
||||
//
|
||||
// A check that cannot tell refuses: a sandbox that fails, a run that times out or prints more
|
||||
// than 4 MB, an answer that is missing or doesn't parse, a kind of source the control doesn't
|
||||
// show, or the kind a one-out run should show. A one-out run that shows another kind, or a
|
||||
// session-shaped run that shows any, is a leak. A --version that fails or hangs past 3 s only
|
||||
// leaves no label. A pass is cached in memory by a digest of the files that run (the package's
|
||||
// package.json, every file under dist/bundle/, every file in the directory tree that holds the
|
||||
// binary's real file, and, for a copy mounted with the node_modules it sits in, npm's lock files
|
||||
// there), the mount layout and the names in the fit runs' environment, so a process with other
|
||||
// names checks again. Callers that ask while a check for the same key is running share it.
|
||||
// A refusal line names the found binary by its label (MOSAIC_PI_BIN, the pi on PATH, the
|
||||
// repository's pi), never a host path.
|
||||
//
|
||||
// Known limit: pi 0.85.1 appends the agent directory's APPEND_SYSTEM.md whatever the flags. The
|
||||
// check doesn't plant one; a session's agent directory is the run's own. Not run: --fork 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,
|
||||
// provider, model, key, -e files and request. The runs are offline with no key, so nothing
|
||||
// past the start is tried. A pi built to act differently with those is outside what a start-time
|
||||
// check can catch.
|
||||
import { spawn } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, readlinkSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join, relative } from "node:path";
|
||||
import { BWRAP, SETPRIV, UNSHARE, checkArgv, outerArgv, sessionSetenv } from "../../seat/src/sandbox.mjs";
|
||||
import { isProviderVariable, sessionEnv } from "../../seat/src/session.mjs";
|
||||
import { adapterEnv } from "./runner.mjs";
|
||||
import { FIT_ROUTE, harnessEnv } from "../../plugins/harness/pi/session-env.mjs";
|
||||
|
||||
// The flags adapters/pi/adapter.sh and the conversation engine pass.
|
||||
export const FIT_FLAGS = Object.freeze(["--mode", "--session", "--no-session", "--session-dir", "--fork", "-c", "--no-extensions", "-e", "--no-skills", "--skill", "--no-prompt-templates", "--no-themes", "--no-context-files", "--no-approve", "--offline", "--tools", "--no-tools", "--provider", "--model", "--thinking", "--system-prompt", "-p"]);
|
||||
// The adapter's seal flags, as it passes them in an ephemeral session with no tools.
|
||||
const SEAL = ["--offline", "--no-approve", "--no-extensions", "--no-skills", "--no-prompt-templates", "--no-themes", "--no-context-files", "--no-tools", "--no-session"];
|
||||
// The loader flags: each keeps one kind of planted source out. --no-mcp counts when --help lists it.
|
||||
const LOADERS = [["--no-approve", "trusted project file"], ["--no-extensions", "extension"], ["--no-skills", "skill"], ["--no-prompt-templates", "prompt template"], ["--no-context-files", "context file"]];
|
||||
// The session-shaped runs' fixed values: a two-tool list, a thinking level, a provider and model.
|
||||
// Offline with no key, pi stops before any model call. The provider and model are the pi module's
|
||||
// (session-env.mjs FIT_ROUTE), so no core source names them.
|
||||
export const SESSION_SHAPE = Object.freeze({ tools: "read,bash", thinking: "low", provider: FIT_ROUTE.provider, model: FIT_ROUTE.model });
|
||||
// The branches adapters/pi/adapter.sh takes for a bus session, each run once or not: a tool list or
|
||||
// --no-tools, -c over a stored session or a fresh one, --thinking or none, --mode json (the model
|
||||
// event monitor) or none. The five runs pass every pair of those together; pi-fit.test.mjs drives
|
||||
// the adapter through all sixteen and fails on a pair no run passes.
|
||||
const T = SESSION_SHAPE.tools;
|
||||
const L = SESSION_SHAPE.thinking;
|
||||
export const SESSION_RUNS = Object.freeze([
|
||||
{ tools: "", cont: false, thinking: "", json: false },
|
||||
{ tools: "", cont: true, thinking: L, json: true },
|
||||
{ tools: T, cont: false, thinking: L, json: true },
|
||||
{ tools: T, cont: true, thinking: "", json: true },
|
||||
{ tools: T, cont: true, thinking: L, json: false },
|
||||
].map((r) => Object.freeze(r)));
|
||||
// The arguments adapters/pi/adapter.sh builds for a bus session: the given -e files, a tool list
|
||||
// or none, a persistent session directory with -c when it holds a session, a thinking level or
|
||||
// none, the --system-prompt file, -p and the request, and --mode json when the model event monitor
|
||||
// runs pi. No --skill and no --fork: a bus session never passes them. Keep its order:
|
||||
// pi-fit.test.mjs runs the adapter through every branch and fails when the two differ.
|
||||
export function sessionShape({ noMcp, extensions, prompt, sessionDir, tools, cont, thinking, json, message }) {
|
||||
return [
|
||||
"--offline", "--no-approve", "--no-extensions", ...(noMcp ? ["--no-mcp"] : []), ...extensions.flatMap((e) => ["-e", e]),
|
||||
"--no-skills", "--no-prompt-templates", "--no-themes", "--no-context-files",
|
||||
...(tools ? ["--tools", tools] : ["--no-tools"]), "--session-dir", sessionDir, ...(cont ? ["-c"] : []),
|
||||
"--provider", SESSION_SHAPE.provider, "--model", SESSION_SHAPE.model, ...(thinking ? ["--thinking", thinking] : []),
|
||||
"--system-prompt", prompt, "-p", message, ...(json ? ["--mode", "json"] : []),
|
||||
];
|
||||
}
|
||||
// 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;
|
||||
// --version gives only a label, so a hang costs this much and leaves no label.
|
||||
const VERSION_TIMEOUT = 3000;
|
||||
const OUTPUT_MAX = 4 * 1024 * 1024;
|
||||
// How long a run's pipes may stay open after it exits or is killed (spawned).
|
||||
const PIPE_GRACE = 1000;
|
||||
const DIGEST_FILES_MAX = 20000;
|
||||
|
||||
// At most this many fit runs at once. Each run is one sandboxed pi start in its own scratch
|
||||
// directory.
|
||||
export const FIT_CONCURRENCY = 4;
|
||||
|
||||
const passed = new Map();
|
||||
const running = new Map();
|
||||
|
||||
// #1873 re-check 9: each run's scratch directory is named for the process that makes it, by its
|
||||
// pid and its start time (field 22 of /proc/<pid>/stat), so a pid used again later is not taken
|
||||
// for it. A process killed mid-check can't remove its own directories. So a check removes, before
|
||||
// it starts, every directory here that a process no longer running made: its pid is gone, or now
|
||||
// belongs to a process that started at another time. A directory whose process can't be read, or
|
||||
// one this uid doesn't own, or a symlink, is left alone.
|
||||
//
|
||||
// #1873 re-check 10: the name also carries the maker's pid namespace (the inode of
|
||||
// /proc/<pid>/ns/pid), and a sweep leaves alone every directory made in another one, since it
|
||||
// can't read that process. And before it removes a directory, the sweep stops the processes that
|
||||
// name it on their command line: a run's every layer does (the setpriv, shell, unshare and bwrap
|
||||
// argv, bwrap's child before its exec, pi's own -e and --system-prompt), and so do the sandbox
|
||||
// probes. Without unshare's outer layer a bwrap child killed in its setup waits for good (see
|
||||
// checkArgv in sandbox.mjs), and this is what stops it. A process is stopped only when an argument
|
||||
// is that directory or a path in it, its real and effective uid are this process's, and its start
|
||||
// time is the same before and after its command line is read. Never by name, never another uid's,
|
||||
// never one whose directory's maker still runs.
|
||||
const SCRATCH = /^mosaic-pi-fit-(\d+)-(\d+)-(\d+)-/;
|
||||
const startTime = (pid) => readFileSync(`/proc/${pid}/stat`, "utf8").split(") ").pop().split(" ")[19];
|
||||
const pidNamespace = (pid) => /^pid:\[(\d+)\]$/.exec(readlinkSync(`/proc/${pid}/ns/pid`))[1];
|
||||
let ownName = null;
|
||||
const scratchPrefix = () => `mosaic-pi-fit-${ownName ??= `${process.pid}-${startTime(process.pid)}-${pidNamespace("self")}`}-`;
|
||||
function sweepScratch(dir = tmpdir()) {
|
||||
let names;
|
||||
try {
|
||||
names = readdirSync(dir);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
const uid = process.getuid();
|
||||
const ns = pidNamespace("self");
|
||||
const stale = [];
|
||||
for (const name of names) {
|
||||
const m = SCRATCH.exec(name);
|
||||
if (!m || m[3] !== ns) continue;
|
||||
try {
|
||||
let alive;
|
||||
try {
|
||||
alive = startTime(m[1]) === m[2];
|
||||
} catch (e) {
|
||||
if (e.code !== "ENOENT") continue;
|
||||
alive = false;
|
||||
}
|
||||
if (alive) continue;
|
||||
const at = join(dir, name);
|
||||
const st = lstatSync(at);
|
||||
if (st.isDirectory() && st.uid === uid) stale.push(at);
|
||||
} catch {}
|
||||
}
|
||||
if (!stale.length) return;
|
||||
stopNaming(stale, uid);
|
||||
for (const at of stale) {
|
||||
try {
|
||||
rmSync(at, { recursive: true, force: true });
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
// SIGKILL to every process of uid (real and effective) with an argument that is one of dirs or a
|
||||
// path in one, other than this one.
|
||||
function stopNaming(dirs, uid) {
|
||||
let pids;
|
||||
try {
|
||||
pids = readdirSync("/proc").filter((p) => /^\d+$/.test(p) && Number(p) !== process.pid);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
const names = (arg) => dirs.some((d) => arg === d || arg.startsWith(`${d}/`));
|
||||
for (const pid of pids) {
|
||||
try {
|
||||
const start = startTime(pid);
|
||||
const ids = /^Uid:\s+(\d+)\s+(\d+)\s/m.exec(readFileSync(`/proc/${pid}/status`, "utf8"));
|
||||
if (!ids || Number(ids[1]) !== uid || Number(ids[2]) !== uid) continue;
|
||||
if (!readFileSync(`/proc/${pid}/cmdline`, "utf8").split("\0").some(names)) continue;
|
||||
if (startTime(pid) !== start) continue;
|
||||
process.kill(Number(pid), "SIGKILL");
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
// The provider variables never reach a fit run: no key is passed, since no model is called.
|
||||
const withoutProviders = (host) => Object.fromEntries(Object.entries(host ?? {}).filter(([k, v]) => typeof v === "string" && !isProviderVariable(k)));
|
||||
// The names a fit run's host part holds: sessionEnv's answer for the process's environment.
|
||||
export const fitNames = (host = process.env) => Object.keys(sessionEnv(withoutProviders(host), "fit")).sort();
|
||||
const fitKey = (found, { levels, bwrap, unshare, node, host }) => JSON.stringify([filesDigest(found), found.root, found.dest ?? null, found.view, bwrap, unshare, node, levels, fitNames(host)]);
|
||||
|
||||
// found: binary.mjs's answer ({ bin, label, root, dest, view, pkg }). levels: the thinking levels
|
||||
// the module applies. host: the environment of the process sessions launch from (the bus's own),
|
||||
// which a fit run's environment is built from as a session's is. bwrap and unshare: where those
|
||||
// tools are (tests pass stand-ins). Returns a promise of { ok: true, version, noMcp, digest, outer }
|
||||
// or { ok: false, message, digest, undecided }. outer: { used: true } when the runs had unshare's outer layer, else
|
||||
// { used: false, why } with why naming unshare and how it failed (sandboxLayers below). A call
|
||||
// while a check with the same key runs gets that check's promise. A check that throws (a scratch directory it can't
|
||||
// make, say) can't tell, so it gives { ok: false, message } too and keeps nothing.
|
||||
// #1873 harness watch W2: a failure also carries digest, the digest of the files the check ran
|
||||
// (null only when they can't be read), and undecided: true when the check could not tell (its
|
||||
// message then says "can't be checked"), false when the binary definitely failed. A reader takes
|
||||
// these fields and never parses the message.
|
||||
// signal (W2): the caller stops waiting when it aborts. A check is stopped (its runs killed, its
|
||||
// scratch removed) only when every caller that shares it passed a signal and every one of those
|
||||
// aborted: preflight passes none, so a check a launch waits on is never stopped by the watch. A
|
||||
// caller whose signal aborts gets its answer once the check has ended, or at once when a caller
|
||||
// without a signal keeps the check going; that answer can't tell (undecided) unless the check
|
||||
// had already finished.
|
||||
export function piFit(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, node = process.execPath, host = process.env, signal = null } = {}) {
|
||||
const who = (version) => `pi${version ? ` ${version}` : ""} (${found.label})`;
|
||||
let key;
|
||||
try {
|
||||
key = fitKey(found, { levels, bwrap, unshare, node, host });
|
||||
} catch {
|
||||
return Promise.resolve({ ok: false, message: `${who(null)} can't be checked: its files can't be read`, digest: null, undecided: true });
|
||||
}
|
||||
if (passed.has(key)) return Promise.resolve(passed.get(key));
|
||||
if (running.has(key)) return share(running.get(key), signal, who, key);
|
||||
const cancel = new AbortController();
|
||||
const job = (async () => {
|
||||
try {
|
||||
sweepScratch();
|
||||
// The sandbox's own error text can name a host path, so the line keeps only what failed.
|
||||
const layers = await sandboxLayers({ bwrap, unshare, node });
|
||||
if (layers.problem) return { ok: false, message: `${who(null)} can't be checked: ${layers.problem}`, digest: JSON.parse(key)[0], undecided: true };
|
||||
if (cancel.signal.aborted) return { ok: false, message: `${who(null)} can't be checked: the check was stopped`, digest: JSON.parse(key)[0], undecided: true };
|
||||
const result = await check(found, { levels, bwrap, unshare: layers.outer, node, who, digest: JSON.parse(key)[0], host, cancel: cancel.signal });
|
||||
if (!result.ok) return result;
|
||||
const passing = { ...result, outer: layers.outer ? { used: true } : { used: false, why: layers.why } };
|
||||
passed.set(key, passing);
|
||||
return passing;
|
||||
} catch {
|
||||
// #1873 re-check 8: refused as a fit that failed. The error's text can name a host path, so
|
||||
// the line keeps none of it.
|
||||
return { ok: false, message: `${who(null)} can't be checked: the check could not run`, digest: JSON.parse(key)[0], undecided: true };
|
||||
}
|
||||
})().finally(() => { if (running.get(key)?.job === job) running.delete(key); });
|
||||
const entry = { job, cancel, holds: 0, firm: false };
|
||||
running.set(key, entry);
|
||||
return share(entry, signal, who, key);
|
||||
}
|
||||
|
||||
// One caller's view of a running check: see signal above. A stopped check leaves running at once,
|
||||
// so a caller that comes after (a launch's preflight) starts its own check and never gets the
|
||||
// stopped one's answer.
|
||||
function share(entry, signal, who, key) {
|
||||
if (!signal) {
|
||||
entry.firm = true;
|
||||
return entry.job;
|
||||
}
|
||||
entry.holds++;
|
||||
return new Promise((resolve) => {
|
||||
let left = false;
|
||||
const leave = () => {
|
||||
if (left) return;
|
||||
left = true;
|
||||
entry.holds--;
|
||||
if (!entry.firm && entry.holds === 0) {
|
||||
entry.cancel.abort();
|
||||
if (running.get(key) === entry) running.delete(key);
|
||||
}
|
||||
if (entry.firm) resolve({ ok: false, message: `${who(null)} can't be checked: the watch stopped waiting`, digest: null, undecided: true });
|
||||
else entry.job.then(resolve);
|
||||
};
|
||||
signal.addEventListener("abort", leave, { once: true });
|
||||
entry.job.then((answer) => { signal.removeEventListener("abort", leave); if (!left) { left = true; entry.holds--; resolve(answer); } });
|
||||
if (signal.aborted) leave();
|
||||
});
|
||||
}
|
||||
|
||||
// The passed check for found as piFit would key it now, or null: none passed, or its files can't
|
||||
// be read. Nothing is run.
|
||||
export function fitVerdict(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, node = process.execPath, host = process.env } = {}) {
|
||||
try {
|
||||
return passed.get(fitKey(found, { levels, bwrap, unshare, node, host })) ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function check(found, { levels, bwrap, unshare, node, who, digest, host, cancel = null }) {
|
||||
// At most FIT_CONCURRENCY runs at once, started in the order they are asked for. stop kills the
|
||||
// runs still going and keeps queued ones from starting; the check waits for every run it began,
|
||||
// so no scratch directory or child outlives it.
|
||||
const stop = new AbortController();
|
||||
const cancelled = () => stop.abort();
|
||||
cancel?.addEventListener("abort", cancelled, { once: true });
|
||||
if (cancel?.aborted) stop.abort();
|
||||
const begun = [];
|
||||
let active = 0;
|
||||
const waiting = [];
|
||||
const slot = () => active < FIT_CONCURRENCY ? (active++, Promise.resolve()) : new Promise((resolve) => waiting.push(resolve));
|
||||
const free = () => { const next = waiting.shift(); if (next) next(); else active--; };
|
||||
// args: the arguments, or a function of the run's scratch directory that returns them (it may
|
||||
// plant more first). The answer is read before the scratch directory goes: the rpc answers,
|
||||
// whether each -e extension loaded, what the first one saw at session_start (the system prompt,
|
||||
// the command names, the session id), and the planted sources that showed, through the rpc
|
||||
// get_commands answer or through the -e extension's command list (listed: through that alone).
|
||||
// shape: a session-shaped run's branches, which set its tools, thinking level and monitor
|
||||
// variables as a session's would. A run that stop kept from starting gives null.
|
||||
const once = async (args, input, { timeout, noMcp, shape }) => {
|
||||
await slot();
|
||||
try {
|
||||
if (stop.signal.aborted) return null;
|
||||
const scratch = mkdtempSync(join(tmpdir(), scratchPrefix()));
|
||||
let ran = null;
|
||||
try {
|
||||
plant(scratch);
|
||||
const env = fitEnv(scratch, { view: found.view, noMcp, shape, host });
|
||||
const r = ran = await runIn(found, { bwrap, unshare, node, scratch, env, args: typeof args === "function" ? args(scratch) : args, input, timeout, signal: stop.signal });
|
||||
const answer = answered(r.stdout);
|
||||
const start = started(scratch);
|
||||
const listed = start?.commands ?? [];
|
||||
return { ...r, answer, explicit: marked(scratch, EXPLICIT), second: marked(scratch, SECOND), prompt: start?.prompt ?? null, session: start?.session ?? null, listed: named(listed), 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
|
||||
// over, so whatever still names its directory is left from it.
|
||||
if (ran?.stopped) stopNaming([scratch], process.getuid());
|
||||
rmSync(scratch, { recursive: true, force: true });
|
||||
}
|
||||
} finally {
|
||||
free();
|
||||
}
|
||||
};
|
||||
const run = (args, input = "", { timeout = RUN_TIMEOUT, noMcp = false, shape = null } = {}) => {
|
||||
const p = once(args, input, { timeout, noMcp, shape });
|
||||
// Judged in order, a later run can fail before an earlier one is awaited: this keeps that
|
||||
// failure from counting as unhandled. Awaiting p still throws it.
|
||||
p.catch(() => {});
|
||||
begun.push(p);
|
||||
return p;
|
||||
};
|
||||
try {
|
||||
return await judge(found, { levels, who, digest, run });
|
||||
} finally {
|
||||
cancel?.removeEventListener("abort", cancelled);
|
||||
stop.abort();
|
||||
await Promise.allSettled(begun);
|
||||
}
|
||||
}
|
||||
|
||||
async function judge(found, { levels, who, digest, run }) {
|
||||
const helped = run(["--help"]);
|
||||
const versioned = run(["--version"], "", { timeout: VERSION_TIMEOUT });
|
||||
const help = await helped;
|
||||
const v = await versioned;
|
||||
const version = v.status === 0 ? versionLabel(v.stdout) : null;
|
||||
// refuse: a definite failure. unsure: the check could not tell.
|
||||
const refuse = (why) => ({ ok: false, message: `${who(version)} ${why}`, digest, undecided: false });
|
||||
const unsure = (why) => ({ ok: false, message: `${who(version)} can't be checked: ${why}`, digest, undecided: true });
|
||||
if (help.status !== 0) return unsure("--help failed");
|
||||
const lines = help.stdout.split("\n");
|
||||
const has = (flag) => lines.some((l) => optionsIn(l).includes(flag));
|
||||
const line = (flag) => lines.find((l) => optionsIn(l).includes(flag)) ?? "";
|
||||
const missing = FIT_FLAGS.filter((f) => !has(f));
|
||||
if (has("--mode") && !word(line("--mode"), "rpc")) missing.push("--mode rpc");
|
||||
if (has("--thinking")) for (const l of levels) if (!word(line("--thinking"), l)) missing.push(`--thinking ${l}`);
|
||||
if (missing.length) return refuse(`lacks ${missing.join(", ")}`);
|
||||
const noMcp = has("--no-mcp");
|
||||
|
||||
const ask = '{"id":"fit-state","type":"get_state"}\n{"id":"fit-commands","type":"get_commands"}\n';
|
||||
// Every run passes the same -e and --system-prompt, so the runs differ only by the seal flags.
|
||||
const given = (scratch) => ["-e", join(scratch, "explicit", "fit.ts"), "--system-prompt", join(scratch, "prompt.md")];
|
||||
const seal = [...SEAL, ...(noMcp ? ["--no-mcp"] : [])];
|
||||
const loaders = [...LOADERS, ...(noMcp ? [["--no-mcp", "MCP server"]] : [])];
|
||||
const without = (...flags) => seal.filter((f) => !flags.includes(f));
|
||||
const rpcRun = (flags) => run((s) => ["--mode", "rpc", ...flags, ...given(s)], ask, { noMcp });
|
||||
|
||||
// Every run is asked for now, in the order they are judged below; none depends on another.
|
||||
// The control keeps the seal flags no planted source tells apart (--offline, --no-themes,
|
||||
// --no-tools, --no-session), so one of those can't stand in for a loader flag either.
|
||||
const controlRun = rpcRun(without(...loaders.map(([f]) => f)));
|
||||
const rpcSealed = rpcRun(seal);
|
||||
const oneOut = loaders.map(([flag, kind]) => [flag, kind, rpcRun(without(flag))]);
|
||||
// A bus session's shapes, in -p as its turns run: --session-dir in place of --no-session, two -e
|
||||
// as a session passes, and each pair of the adapter's session branches in one run. A -c run gets
|
||||
// a stored session for the project first and must resume it, since -c with none starts afresh.
|
||||
const shaped = SESSION_RUNS.map((shape) => [shape, run((s) => {
|
||||
if (shape.cont) plantSession(s);
|
||||
return sessionShape({ noMcp, extensions: [join(s, "explicit", "fit.ts"), join(s, "explicit", "second.ts")], prompt: join(s, "prompt.md"), sessionDir: join(s, "sessions"), message: "fit check", ...shape });
|
||||
}, "", { noMcp, shape })]);
|
||||
// -p takes a message; offline with no key, pi stops before any model call. 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]) =>
|
||||
[mode, run((s) => [...args, ...seal, "-e", join(s, "missing", "extension.ts"), ...given(s), ...(mode === "-p" ? ["fit check"] : [])], input, { noMcp })]);
|
||||
|
||||
const control = await controlRun;
|
||||
if (control.stopped) return unsure(`the start without the seal flags ${control.stopped}`);
|
||||
if (!control.answer.state) return unsure("it didn't answer get_state");
|
||||
if (!control.answer.commands) return unsure("it didn't answer get_commands");
|
||||
if (!control.explicit || control.prompt === null) return unsure("its -e extension didn't see the system prompt without the seal flags");
|
||||
// A kind counts only through a source outside the project, except the trusted project files
|
||||
// themselves: a project source can wait on project trust, and then --no-approve alone would keep
|
||||
// it out of the sealed runs and hide a seal flag of its kind that the binary ignores.
|
||||
const shown = new Set(control.seen.filter((s) => s.kind === "trusted project file" || !s.source.startsWith("project ")).map((s) => s.kind));
|
||||
const unshown = KINDS.filter((k) => (k !== "MCP server" || noMcp) && !shown.has(k));
|
||||
if (unshown.length) return unsure(`it didn't load a planted ${unshown.join(", ")} without the seal flags`);
|
||||
// -p answers no get_commands, so the -p runs see skills and prompt templates only through the -e
|
||||
// extension's command list, which must show them here.
|
||||
const listed = new Set(control.listed.filter((s) => !s.source.startsWith("project ")).map((s) => s.kind));
|
||||
const unlisted = ["skill", "prompt template"].filter((k) => !listed.has(k));
|
||||
if (unlisted.length) return unsure(`its -e extension didn't list a planted ${unlisted.join(", ")} without the seal flags`);
|
||||
|
||||
const rpc = await rpcSealed;
|
||||
if (rpc.stopped) return unsure(`the start with the seal flags ${rpc.stopped}`);
|
||||
if (!rpc.answer.state) return unsure("it didn't answer get_state with the seal flags on");
|
||||
if (!rpc.answer.commands) return unsure("it didn't answer get_commands with the seal flags on");
|
||||
if (!rpc.explicit) return refuse("didn't load an -e extension with the seal flags on");
|
||||
if (rpc.prompt === null) return unsure("its -e extension didn't see the system prompt with the seal flags on");
|
||||
if (!rpc.prompt.includes(GIVEN)) return refuse("didn't use the --system-prompt file with the seal flags on");
|
||||
if (rpc.seen.length) return refuse(`loaded a planted ${rpc.seen.map((s) => s.source).join(", ")} with the seal flags on`);
|
||||
|
||||
// Each loader flag on its own: with every other seal flag on, its kind must show.
|
||||
for (const [flag, kind, pending] of oneOut) {
|
||||
const r = await pending;
|
||||
const but = `with every seal flag but ${flag}`;
|
||||
if (r.stopped) return unsure(`the start ${but} ${r.stopped}`);
|
||||
if (!r.answer.state || !r.answer.commands) return unsure(`it didn't answer get_state and get_commands ${but}`);
|
||||
if (r.prompt === null) return unsure(`its -e extension didn't see the system prompt ${but}`);
|
||||
if (!r.seen.some((s) => s.kind === kind)) return unsure(`it didn't load a planted ${kind} ${but}`);
|
||||
const others = r.seen.filter((s) => s.kind !== kind && !(flag === "--no-extensions" && !noMcp && s.kind === "MCP server"));
|
||||
if (others.length) return refuse(`loaded a planted ${others.map((s) => s.source).join(", ")} ${but}`);
|
||||
}
|
||||
|
||||
for (const [shape, pending] of shaped) {
|
||||
const r = await pending;
|
||||
const name = shapeName(shape);
|
||||
if (r.stopped) return unsure(`the start ${name} ${r.stopped}`);
|
||||
if (r.prompt === null) return unsure(`its -e extension didn't see the system prompt ${name}`);
|
||||
if (!r.explicit || !r.second) return refuse(`didn't load every -e extension ${name}`);
|
||||
if (!r.prompt.includes(GIVEN)) return refuse(`didn't use the --system-prompt file ${name}`);
|
||||
if (shape.cont && r.session !== PLANTED_SESSION) return unsure(`it didn't resume the planted session with -c ${name}`);
|
||||
if (r.seen.length) return refuse(`loaded a planted ${r.seen.map((x) => x.source).join(", ")} with the seal flags ${name}`);
|
||||
}
|
||||
|
||||
const p = await pRun;
|
||||
if (p.stopped) return unsure(`the start in a worker's shape ${p.stopped}`);
|
||||
if (p.prompt === null) return unsure("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;
|
||||
if (r.stopped) return unsure(`a ${mode} start with a missing -e extension ${r.stopped}`);
|
||||
if (r.status === 0 || r.answer.state || r.prompt !== null) return refuse("starts with a missing -e extension");
|
||||
}
|
||||
return { ok: true, version, noMcp, digest };
|
||||
}
|
||||
|
||||
// Whether bwrap runs here, as sandbox.mjs's sandboxProblem asks, but as child processes the caller
|
||||
// awaits: the system part, the network unshared, an empty environment, then /usr/bin/true. And,
|
||||
// once per check, whether unshare's outer layer runs (checkArgv in sandbox.mjs): first on its own,
|
||||
// then with bwrap inside it. Returns { problem } when the check can't run, naming the tool that
|
||||
// failed without its own text, else { outer, why }: outer is the unshare to pass checkArgv, or null
|
||||
// when the layer can't run here, and why then names unshare and how it failed. A missing,
|
||||
// too old or refused unshare drops only that layer and is no refusal: bwrap's sandbox is the same
|
||||
// either way. The probes run in a scratch directory of their own and name it on their command
|
||||
// line, so a probe process a killed owner leaves is one the next sweep finds.
|
||||
async function sandboxLayers({ bwrap, unshare, node }) {
|
||||
if (!existsSync(bwrap)) return { problem: "the sandbox (bwrap) is missing" };
|
||||
if (!existsSync(SETPRIV)) return { problem: "the sandbox's parent-death tool (setpriv) is missing" };
|
||||
const scratch = mkdtempSync(join(tmpdir(), scratchPrefix()));
|
||||
let stopped = false;
|
||||
try {
|
||||
const probe = async (argv) => {
|
||||
const r = await spawned(argv, { input: "", timeout: 10000, signal: new AbortController().signal });
|
||||
stopped ||= Boolean(r.stopped);
|
||||
return r.status === 0;
|
||||
};
|
||||
const sandboxed = (outer) => checkArgv([{ kind: "ro", src: scratch }], ["/usr/bin/true"], { chdir: scratch, node, bwrap, unshare: outer });
|
||||
let why = "unshare is missing";
|
||||
if (existsSync(unshare)) {
|
||||
if (!await probe(outerArgv(["/usr/bin/true", scratch], { unshare }))) why = "unshare failed";
|
||||
else if (await probe(sandboxed(unshare))) return { outer: unshare, why: null };
|
||||
else why = "unshare failed with bwrap inside it";
|
||||
}
|
||||
if (!await probe(sandboxed(null))) return { problem: "the sandbox (bwrap) failed" };
|
||||
return { outer: null, why };
|
||||
} finally {
|
||||
// As for a run (once, in piFit): a probe killed at its timeout may leave what names its directory.
|
||||
if (stopped) stopNaming([scratch], process.getuid());
|
||||
rmSync(scratch, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
// The binary, inside the sandbox: the system part, the found package where a session sees it, the
|
||||
// scratch directory read-write, and its tmp/ as /tmp, as a session has it. The environment is
|
||||
// fitEnv's. The binary starts through /bin/sh, as the adapter starts it, so it also gets the
|
||||
// variables the shell sets. The run is a child process the caller awaits; nothing here blocks.
|
||||
// stopped: why the run was killed ("timed out", "printed more than 4 MB", "was stopped"), or null.
|
||||
function runIn(found, { bwrap, unshare, node, scratch, env, args, input, timeout, signal }) {
|
||||
const list = [
|
||||
{ kind: "ro", src: found.root, ...(found.dest ? { dest: found.dest } : {}) },
|
||||
{ kind: "rw", src: scratch },
|
||||
{ kind: "rw", src: join(scratch, "tmp"), dest: "/tmp" },
|
||||
];
|
||||
return spawned(checkArgv(list, ["/bin/sh", "-c", 'exec "$0" "$@"', found.view, ...args], { env, chdir: join(scratch, "project"), node, bwrap, unshare }), { input, timeout, signal });
|
||||
}
|
||||
|
||||
// One child process with an empty environment, awaited: its exit status, its stdout, and why it was
|
||||
// killed, or null. It settles PIPE_GRACE after the child exits or is killed even if its pipes stay
|
||||
// open: a process left behind can hold them (a kill of bwrap's outer process while its child still
|
||||
// sets up leaves that child waiting until the pid namespace checkArgv makes is killed, or, without
|
||||
// that layer, until the next check's sweep), and no wait here is unbounded.
|
||||
function spawned(argv, { input, timeout, signal }) {
|
||||
return new Promise((resolve) => {
|
||||
const out = [];
|
||||
const size = { stdout: 0, stderr: 0 };
|
||||
let stopped = null;
|
||||
let done = false;
|
||||
let child;
|
||||
let timer = null;
|
||||
let grace = null;
|
||||
let exited = null;
|
||||
const finish = (status, sig) => {
|
||||
if (done) return;
|
||||
done = true;
|
||||
clearTimeout(timer);
|
||||
clearTimeout(grace);
|
||||
child?.stdout?.destroy();
|
||||
child?.stderr?.destroy();
|
||||
signal.removeEventListener("abort", onAbort);
|
||||
if (!stopped && sig === "SIGKILL") stopped = "timed out";
|
||||
resolve({ status, stdout: Buffer.concat(out).toString("utf8"), stopped });
|
||||
};
|
||||
const settleSoon = () => { if (!grace) grace = setTimeout(() => finish(exited?.status ?? null, exited?.sig ?? "SIGKILL"), PIPE_GRACE); };
|
||||
const kill = (why) => {
|
||||
if (!stopped) stopped = why;
|
||||
try { child?.kill("SIGKILL"); } catch {}
|
||||
settleSoon();
|
||||
};
|
||||
const onAbort = () => kill("was stopped");
|
||||
try {
|
||||
child = spawn(argv[0], argv.slice(1), { env: {}, stdio: ["pipe", "pipe", "pipe"] });
|
||||
} catch {
|
||||
finish(null, null);
|
||||
return;
|
||||
}
|
||||
timer = setTimeout(() => kill("timed out"), timeout);
|
||||
if (signal.aborted) onAbort();
|
||||
else signal.addEventListener("abort", onAbort, { once: true });
|
||||
const count = (stream) => (chunk) => {
|
||||
size[stream] += chunk.length;
|
||||
if (size[stream] > OUTPUT_MAX) kill(`printed more than ${OUTPUT_MAX / 1024 / 1024} MB`);
|
||||
else if (stream === "stdout") out.push(chunk);
|
||||
};
|
||||
child.stdout.on("data", count("stdout"));
|
||||
child.stderr.on("data", count("stderr"));
|
||||
child.stdin.on("error", () => {});
|
||||
child.on("error", () => finish(null, null));
|
||||
child.on("exit", (status, sig) => { exited = { status, sig }; settleSoon(); });
|
||||
child.on("close", (status, sig) => finish(status, sig));
|
||||
child.stdin.end(input);
|
||||
});
|
||||
}
|
||||
|
||||
// A fit run's environment: what a bus session's pi gets, built the same way from host, the
|
||||
// environment of the process sessions launch from (the bus's own). The launcher passes host's
|
||||
// ENV_ALLOW names that are set through sessionEnv (less the provider variables here, since no key
|
||||
// is passed: no model is called), with host's PATH behind the repository's .bin; the sandbox sets
|
||||
// sessionSetenv's (HOME is the scratch home, so pi's agent directory is $HOME/.pi/agent); the
|
||||
// runner adds adapterEnv's with the pi module's harnessEnv, and MOSAIC_REQUEST for each turn. No
|
||||
// name is added that a session in that process would lack: a bus started by its unit has no TERM,
|
||||
// TZ or LC_ALL, and then no fit run has them either. shape: a session-shaped run's branches (its
|
||||
// tool list, thinking level and --mode json, which a proxy session's first turn takes with the
|
||||
// model event monitor's variables), else none: no tools, no thinking, one -e. noMcp: --help lists
|
||||
// --no-mcp. pi-fit.test.mjs fails when these names, or the values the code sets, differ from a bus
|
||||
// session's, for a login shell's host and a unit's.
|
||||
export function fitEnv(scratch, { view, noMcp = false, shape = null, host = process.env }) {
|
||||
const runner = { ...sessionEnv(withoutProviders(host), "fit"), ...sessionSetenv(scratch, { business: "fit", role: "fit" }) };
|
||||
const { tools = "", thinking = "", json = false } = shape ?? {};
|
||||
const session = { provider: SESSION_SHAPE.provider, model: SESSION_SHAPE.model, thinking };
|
||||
const extensions = [join(scratch, "explicit", "fit.ts"), ...(shape ? [join(scratch, "explicit", "second.ts")] : [])];
|
||||
const env = adapterEnv({
|
||||
...session,
|
||||
bundle: { prompt: join(scratch, "prompt.md"), policy: join(scratch, "policy.json"), tools: join(scratch, "tools.json") },
|
||||
workspace: join(scratch, "project"), sessionDir: join(scratch, "sessions"), toolSocket: join(scratch, "tools.sock"), turnMarker: join(scratch, "turn"),
|
||||
harnessEnv: harnessEnv({ bin: view, noMcp, tools: tools ? tools.split(",") : [], extensions, session }),
|
||||
runDir: scratch, proxyFirstTurn: json,
|
||||
}, runner);
|
||||
return { ...env, MOSAIC_REQUEST: "fit check" };
|
||||
}
|
||||
|
||||
// The options one help line lists in its option column: a line that starts with "-" (after its
|
||||
// indent), up to the first run of two spaces or a tab, split at commas; the first word of each
|
||||
// part. " --extension, -e <path> Load an extension" lists --extension and -e. A flag named in a
|
||||
// sentence is not listed.
|
||||
function optionsIn(line) {
|
||||
const t = line.trim();
|
||||
if (!t.startsWith("-")) return [];
|
||||
return t.split(/ {2,}|\t/)[0].split(",").map((part) => part.trim().split(/[ =<[]/)[0]).filter((o) => /^--?[A-Za-z0-9]/.test(o));
|
||||
}
|
||||
const word = (line, w) => line.split(/[^A-Za-z0-9-]+/).includes(w);
|
||||
|
||||
// The version a refusal line may carry: a version-shaped first line, else none. Anything else the
|
||||
// binary prints stays out of the line.
|
||||
function versionLabel(stdout) {
|
||||
const first = String(stdout).split("\n")[0].trim();
|
||||
return /^v?[0-9]+(\.[0-9]+){0,3}([-+][0-9A-Za-z.-]{1,32})?$/.test(first) ? first : null;
|
||||
}
|
||||
|
||||
// The rpc answers in the output: whether get_state succeeded, and the command names get_commands
|
||||
// listed, or null when it didn't answer with a { commands: [...] } list.
|
||||
function answered(stdout) {
|
||||
let state = false;
|
||||
let commands = null;
|
||||
for (const l of String(stdout).split("\n")) {
|
||||
let m;
|
||||
try { m = JSON.parse(l); } catch { continue; }
|
||||
if (!m || m.type !== "response" || m.success !== true) continue;
|
||||
if (m.id === "fit-state" && m.command === "get_state" && m.data && typeof m.data === "object") state = true;
|
||||
if (m.id === "fit-commands" && m.command === "get_commands" && Array.isArray(m.data?.commands)) {
|
||||
commands = m.data.commands.filter((c) => typeof c?.name === "string").map((c) => c.name);
|
||||
}
|
||||
}
|
||||
return { state, commands };
|
||||
}
|
||||
|
||||
// The planted sources, each with the kind the control must show. Each extension and each MCP
|
||||
// server touches its own marker; each skill and prompt template has a name get_commands lists;
|
||||
// each context and system prompt file holds a word the -e extension's view of the system prompt
|
||||
// shows. "user" sources sit in the agent directory, $HOME/.pi/agent: no fit run sets
|
||||
// PI_CODING_AGENT_DIR, as no bus session does. "settings" sources are listed by a settings.json;
|
||||
// "package" sources come from a local package a settings.json lists. "parent" is a directory above
|
||||
// the project.
|
||||
const KINDS = ["extension", "MCP server", "skill", "prompt template", "context file", "trusted project file"];
|
||||
const MARKED = {
|
||||
"user extension": "extension", "user settings extension": "extension", "user package extension": "extension",
|
||||
"project extension": "extension", "project settings extension": "extension", "project package extension": "extension",
|
||||
"user mcp.json": "MCP server", "project mcp.json": "MCP server", "project .mcp.json": "MCP server",
|
||||
};
|
||||
const NAMED = {
|
||||
"fit-user-skill": ["user skill", "skill"], "fit-user-set-skill": ["user settings skill", "skill"], "fit-user-pkg-skill": ["user package skill", "skill"],
|
||||
"fit-project-skill": ["project skill", "skill"], "fit-project-set-skill": ["project settings skill", "skill"],
|
||||
"fit-project-pkg-skill": ["project package skill", "skill"], "fit-agents-skill": ["project .agents skill", "skill"], "fit-home-skill": ["home .agents skill", "skill"],
|
||||
"fit-user-prompt": ["user prompt template", "prompt template"], "fit-user-set-prompt": ["user settings prompt template", "prompt template"],
|
||||
"fit-user-pkg-prompt": ["user package prompt template", "prompt template"],
|
||||
"fit-project-prompt": ["project prompt template", "prompt template"], "fit-project-set-prompt": ["project settings prompt template", "prompt template"],
|
||||
"fit-project-pkg-prompt": ["project package prompt template", "prompt template"],
|
||||
};
|
||||
// Planted file (under the scratch directory), the word it holds, the source, the kind. A project
|
||||
// SYSTEM.md or APPEND_SYSTEM.md loads only in a trusted project: the saved trust makes it trusted,
|
||||
// and only --no-approve undoes that with the seal flags on.
|
||||
const TEXTS = [
|
||||
["home/.pi/agent/SYSTEM.md", "FITUSERSYSTEM", "user SYSTEM.md", "context file"],
|
||||
["home/.pi/agent/AGENTS.md", "FITUSERAGENTS", "user AGENTS.md", "context file"],
|
||||
["project/.pi/SYSTEM.md", "FITPROJECTSYSTEM", "project SYSTEM.md", "trusted project file"],
|
||||
["project/.pi/APPEND_SYSTEM.md", "FITPROJECTAPPEND", "project APPEND_SYSTEM.md", "trusted project file"],
|
||||
["project/AGENTS.md", "FITPROJECTAGENTS", "project AGENTS.md", "context file"],
|
||||
["project/CLAUDE.md", "FITPROJECTCLAUDE", "project CLAUDE.md", "context file"],
|
||||
["CLAUDE.md", "FITPARENTCLAUDE", "parent CLAUDE.md", "context file"],
|
||||
];
|
||||
// The --system-prompt file's word, which the sealed runs must show.
|
||||
const GIVEN = "FITGIVENPROMPT";
|
||||
// The -e extension every run passes, which must load, and the second one the session-shaped runs
|
||||
// pass after it.
|
||||
const EXPLICIT = "explicit extension";
|
||||
const SECOND = "second explicit extension";
|
||||
// The 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, "-");
|
||||
const marked = (scratch, source) => { try { return readdirSync(join(scratch, "marks")).includes(markerName(source)); } catch { return false; } };
|
||||
// What the -e extension saw at session_start ({ prompt, commands, session }), or null when it wrote
|
||||
// nothing that parses into that shape.
|
||||
function started(scratch) {
|
||||
let m;
|
||||
try { m = JSON.parse(readFileSync(join(scratch, "marks", "start.json"), "utf8")); } catch { return null; }
|
||||
if (!m || typeof m.prompt !== "string" || !Array.isArray(m.commands)) return null;
|
||||
return { prompt: m.prompt, commands: m.commands.filter((c) => typeof c === "string"), session: typeof m.session === "string" ? m.session : null };
|
||||
}
|
||||
// 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 = (commands) => Object.entries(NAMED).filter(([name]) => names(commands, name)).map(([, [source, kind]]) => ({ source, kind }));
|
||||
|
||||
function plant(scratch) {
|
||||
const marks = join(scratch, "marks");
|
||||
const put = (path, text) => { mkdirSync(dirname(path), { recursive: true }); writeFileSync(path, text); };
|
||||
const ext = (source) => `import { writeFileSync } from "node:fs";\nexport default function (): void { writeFileSync(${JSON.stringify(join(marks, markerName(source)))}, ""); }\n`;
|
||||
const mcp = (source) => JSON.stringify({ mcpServers: { fit: { command: "/bin/sh", args: ["-c", `: > ${join(marks, markerName(source))}`] } } });
|
||||
const skill = (name) => `---\nname: ${name}\ndescription: a planted skill\n---\nA planted skill.\n`;
|
||||
const prompt = "A planted prompt template.\n";
|
||||
const agent = join(scratch, "home", ".pi", "agent");
|
||||
const project = join(scratch, "project");
|
||||
mkdirSync(marks, { recursive: true });
|
||||
mkdirSync(join(scratch, "tmp"), { recursive: true });
|
||||
mkdirSync(project, { recursive: true });
|
||||
// The session-shaped runs' --session-dir, made as the adapter makes it.
|
||||
mkdirSync(join(scratch, "sessions"), { recursive: true });
|
||||
// Each settings.json lists an extension, a skill directory, a prompt template and a local
|
||||
// package holding one of each.
|
||||
const listed = (who) => {
|
||||
const dir = join(scratch, "listed", who);
|
||||
put(join(dir, "fit.ts"), ext(`${who} settings extension`));
|
||||
put(join(dir, `fit-${who}-set-skill`, "SKILL.md"), skill(`fit-${who}-set-skill`));
|
||||
put(join(dir, `fit-${who}-set-prompt.md`), prompt);
|
||||
const pkg = join(dir, "package");
|
||||
put(join(pkg, "package.json"), JSON.stringify({ name: `fit-${who}-package`, version: "1.0.0" }));
|
||||
put(join(pkg, "extensions", "fit.ts"), ext(`${who} package extension`));
|
||||
put(join(pkg, "skills", `fit-${who}-pkg-skill`, "SKILL.md"), skill(`fit-${who}-pkg-skill`));
|
||||
put(join(pkg, "prompts", `fit-${who}-pkg-prompt.md`), prompt);
|
||||
return { extensions: [join(dir, "fit.ts")], skills: [join(dir, `fit-${who}-set-skill`)], prompts: [join(dir, `fit-${who}-set-prompt.md`)], packages: [pkg] };
|
||||
};
|
||||
// A saved trust for the project, so the control loads its trust-gated files without --approve.
|
||||
put(join(agent, "settings.json"), JSON.stringify({ ...listed("user"), defaultProjectTrust: "always" }));
|
||||
put(join(agent, "trust.json"), JSON.stringify({ [realpathSync(project)]: true }));
|
||||
put(join(agent, "extensions", "fit.ts"), ext("user extension"));
|
||||
put(join(agent, "mcp.json"), mcp("user mcp.json"));
|
||||
put(join(agent, "skills", "fit-user-skill", "SKILL.md"), skill("fit-user-skill"));
|
||||
put(join(agent, "prompts", "fit-user-prompt.md"), prompt);
|
||||
put(join(project, ".pi", "settings.json"), JSON.stringify(listed("project")));
|
||||
put(join(project, ".pi", "extensions", "fit.ts"), ext("project extension"));
|
||||
put(join(project, ".pi", "mcp.json"), mcp("project mcp.json"));
|
||||
put(join(project, ".mcp.json"), mcp("project .mcp.json"));
|
||||
put(join(project, ".pi", "skills", "fit-project-skill", "SKILL.md"), skill("fit-project-skill"));
|
||||
put(join(project, ".agents", "skills", "fit-agents-skill", "SKILL.md"), skill("fit-agents-skill"));
|
||||
put(join(project, ".pi", "prompts", "fit-project-prompt.md"), prompt);
|
||||
put(join(scratch, "home", ".agents", "skills", "fit-home-skill", "SKILL.md"), skill("fit-home-skill"));
|
||||
for (const 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));
|
||||
put(join(scratch, "prompt.md"), `${GIVEN}\n`);
|
||||
}
|
||||
|
||||
// A stored session for the project, as pi 0.85.1 writes one: a header whose cwd is the project's
|
||||
// real path (pi's -c takes only a session for its own cwd) and one user message.
|
||||
function plantSession(scratch) {
|
||||
const at = new Date().toISOString();
|
||||
writeFileSync(join(scratch, "sessions", `${PLANTED_SESSION}.jsonl`), [
|
||||
{ type: "session", version: 3, id: PLANTED_SESSION, timestamp: at, cwd: realpathSync(join(scratch, "project")) },
|
||||
{ type: "message", id: "f17e0001", parentId: null, timestamp: at, message: { role: "user", content: [{ type: "text", text: "an earlier turn" }], timestamp: Date.now() } },
|
||||
].map((e) => `${JSON.stringify(e)}\n`).join(""));
|
||||
}
|
||||
|
||||
// The planted sources a run loaded, as { source, kind }: markers present, planted names among the
|
||||
// command names, and planted words in the system prompt the -e extension saw.
|
||||
function seen(scratch, commands, start) {
|
||||
let marks = [];
|
||||
try { marks = readdirSync(join(scratch, "marks")); } catch { marks = []; }
|
||||
const out = Object.entries(MARKED).filter(([s]) => marks.includes(markerName(s))).map(([source, kind]) => ({ source, kind }));
|
||||
out.push(...named(commands));
|
||||
const text = start?.prompt ?? "";
|
||||
for (const [, word, source, kind] of TEXTS) if (text.includes(word)) out.push({ source, kind });
|
||||
return out;
|
||||
}
|
||||
|
||||
// sha256 over the sorted (path, sha256) list of the package's package.json, every regular file
|
||||
// under dist/bundle/ (design section 4), every regular file in the directory tree that holds the
|
||||
// binary's real file, so a bin outside dist/bundle/ can't import a file the digest skips, and npm's
|
||||
// lock files for a copy mounted with its node_modules. A symlink there is read through.
|
||||
function filesDigest(found) {
|
||||
const pkg = found.pkg;
|
||||
const bin = realpathSync(found.bin);
|
||||
const files = new Set([join(pkg, "package.json"), bin]);
|
||||
const walk = (dir) => {
|
||||
let entries;
|
||||
try { entries = readdirSync(dir, { withFileTypes: true }); } catch (e) { if (e.code === "ENOENT") return; throw e; }
|
||||
for (const e of entries) {
|
||||
const p = join(dir, e.name);
|
||||
if (e.isDirectory()) walk(p);
|
||||
else if (e.isFile() || (e.isSymbolicLink() && lstatSync(realpathSync(p)).isFile())) files.add(p);
|
||||
if (files.size > DIGEST_FILES_MAX) throw new Error("too many files");
|
||||
}
|
||||
};
|
||||
walk(join(pkg, "dist", "bundle"));
|
||||
const binDir = dirname(bin);
|
||||
if (binDir !== pkg && binDir.startsWith(`${pkg}/`)) walk(binDir);
|
||||
// A copy mounted with the node_modules it sits in imports packages beside it: npm's lock files
|
||||
// there stand for them.
|
||||
if (found.root !== pkg) for (const f of [join(found.root, ".package-lock.json"), join(dirname(found.root), "package-lock.json")]) {
|
||||
try { lstatSync(f); } catch (e) { if (e.code === "ENOENT") continue; throw e; }
|
||||
files.add(f);
|
||||
}
|
||||
const h = createHash("sha256");
|
||||
for (const f of [...files].sort()) {
|
||||
h.update(relative(pkg, f)).update("\0").update(createHash("sha256").update(readFileSync(f)).digest("hex")).update("\n");
|
||||
}
|
||||
return h.digest("hex");
|
||||
}
|
||||
@@ -4,7 +4,8 @@ import { createHash } from "node:crypto";
|
||||
import { mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { BundleError, buildBundle, sessionModel, SOURCES } from "../src/bundle.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
import { decide } from "../src/gate.mjs";
|
||||
import { resolveInstance, validateBusinessDocument } from "../../organization/src/index.mjs";
|
||||
import { businessDoc, REPO_ROLES, systemFor } from "../../organization/tests/helpers.mjs";
|
||||
|
||||
@@ -12,7 +12,8 @@ import { join, relative } from "node:path";
|
||||
import { buildBundle, PERSONA_CHARS } from "../src/bundle.mjs";
|
||||
import { decide } from "../src/gate.mjs";
|
||||
import { adapterEnv } from "../src/runner.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
|
||||
const sha = (data) => createHash("sha256").update(data).digest("hex");
|
||||
const CREATIVE = ["companion", "ideas", "research", "proofreader", "fact-checker", "continuity"];
|
||||
@@ -177,7 +178,8 @@ function runAdapter(t, harness, b, { prompt = b.files.prompt, cwd } = {}) {
|
||||
writeFileSync(cli, '#!/bin/sh\nprintf "%s\\0" "$@" > "$ARGV_OUT"\necho \'{"type":"result","subtype":"success","result":"ok"}\'\n');
|
||||
chmodSync(cli, 0o755);
|
||||
const s = { harness, provider: harness === "pi" ? "probe" : null, model: "claude-sonnet-5-5", workspace: b.workspace, sessionDir: join(b.dir, "session"), toolSocket: join(b.dir, "tools.sock"), turnMarker: join(b.dir, "turn.done"), bundle: { ...b.files, prompt }, harnessEnv: b.env };
|
||||
const env = { ...adapterEnv(s, { PATH: `${bin}:/usr/bin:/bin`, HOME: join(b.dir, "home") }), ARGV_OUT: join(b.dir, "argv"), MOSAIC_REQUEST: "x" };
|
||||
// #1873 S2: the pi adapter runs MOSAIC_PI_BIN, which the bundle sets to the pi it found.
|
||||
const env = { ...adapterEnv(s, { PATH: `${bin}:/usr/bin:/bin`, HOME: join(b.dir, "home") }), ARGV_OUT: join(b.dir, "argv"), MOSAIC_REQUEST: "x", ...(harness === "pi" ? { MOSAIC_PI_BIN: cli } : {}) };
|
||||
const r = spawnSync("/bin/sh", [join(REPO, "adapters", harness === "pi" ? "pi" : "claude", "adapter.sh")], { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], env });
|
||||
const argv = existsSync(env.ARGV_OUT) ? readFileSync(env.ARGV_OUT, "utf8").split("\0") : null;
|
||||
return { ...r, argv };
|
||||
|
||||
@@ -31,6 +31,12 @@ async function harnessModule(name) {
|
||||
return loader.modules("harness")[0];
|
||||
}
|
||||
export const HARNESS = Object.freeze({ pi: await harnessModule("pi"), "claude-code": await harnessModule("claude-code") });
|
||||
// #1873 re-check 7: a module whose fit check runs async has ready(), which the launcher's preflight
|
||||
// stands for. A test that calls buildBundle directly awaits this first. It is not run on import:
|
||||
// request-agent.mjs imports this file in every tool-call process, which builds no bundle.
|
||||
export async function harnessReady() {
|
||||
for (const mod of Object.values(HARNESS)) if (typeof mod.ready === "function") await mod.ready();
|
||||
}
|
||||
|
||||
// A session's route is its own three vars (row 65); a role the test doesn't route runs pi on the
|
||||
// test system's old execution.model at thinking high, so the bundle tests keep their models.
|
||||
|
||||
@@ -6,7 +6,8 @@ import { join } from 'node:path';
|
||||
import { typedTools } from '../src/tools.mjs';
|
||||
import { buildBundle } from '../src/bundle.mjs';
|
||||
import { TYPES } from '../../library/src/mapping.mjs';
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch } from './helpers.mjs';
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch, harnessReady } from './helpers.mjs';
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
|
||||
const LOOKUPS = ['library_find', 'library_get', 'library_search', 'library_manuscript', 'library_universe'];
|
||||
const CREATIVE = ['companion', 'research', 'ideas', 'proofreader', 'fact-checker', 'continuity'];
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -10,7 +10,8 @@ import { join } from "node:path";
|
||||
import { buildBundle } from "../src/bundle.mjs";
|
||||
import { adapterEnv, unslopCounts, unslopLog, unslopNotes } from "../src/runner.mjs";
|
||||
import { REPLY_CAP } from "../src/tools.mjs";
|
||||
import { fakeToolSocket, HARNESS, mockAnthropic, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
|
||||
import { fakeToolSocket, HARNESS, mockAnthropic, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
|
||||
const ADAPTER = join(REPO, "adapters", "pi", "adapter.sh");
|
||||
|
||||
|
||||
@@ -0,0 +1,265 @@
|
||||
// #1873 pi-unpin S3: a stand-in pi that the fit check (src/pi-fit.mjs) can run. It is one sh
|
||||
// script with its knobs written into it, because the fit check runs it with an empty
|
||||
// environment. Put it at dist/bundle/cli.js in a package named @earendil-works/pi-coding-agent.
|
||||
//
|
||||
// What it does, in order:
|
||||
// --version prints `version` (default 9.9.9), or exits `versionExit` when that is set, or
|
||||
// hangs for a minute with `versionHang`
|
||||
// $ARGV_OUT set: a session. Writes its argv there, prints `sessionOut`, exits 0. This part is
|
||||
// sh, so it runs in a sandbox that holds no node.
|
||||
// otherwise runs node (the absolute path, since the fit check gives it no PATH) on the rest:
|
||||
// --help lists every flag the fit check wants, less `dropFlags`, plus `extraFlags`, then
|
||||
// the `helpProse` lines as they are
|
||||
// -e <file> a file that doesn't exist stops it with exit 1, unless `missingExtOk` is true or
|
||||
// names the mode ("rpc" or "print") it starts in anyway
|
||||
// --mode rpc loads the planted sources the way pi 0.85.1 finds them, honouring the seal flags,
|
||||
// or -p runs each -e extension's session_start (one that writes JSON.stringify(...) gets
|
||||
// the prompt this stand-in builds, the command names and the session id), then
|
||||
// answers get_state and get_commands
|
||||
// (rpc), or prints `sessionOut` and exits 0 (-p).
|
||||
// An -e file that exists loads even with --no-extensions, unless `dropExplicit`
|
||||
// (then it loads only without --no-extensions). `printNoStart`: -p runs no
|
||||
// session_start. MCP servers stay out with --no-mcp when `extraFlags` lists it, else
|
||||
// with --no-extensions.
|
||||
// `ignoreAll` loads no planted source. `sealLeak` lists kinds (extensions, mcp,
|
||||
// skills, prompts, context, approve) loaded even when their seal flag is on.
|
||||
// `ignoreSystemPrompt` drops the --system-prompt file. `ignoreAppend` drops every
|
||||
// APPEND_SYSTEM.md. `sysReplacesAll`: a --system-prompt file also drops every
|
||||
// APPEND_SYSTEM.md and context file. `trustExtOnly`: the only extensions are the
|
||||
// project's .pi/extensions, loaded in a trusted project whatever --no-extensions
|
||||
// says. `trustCtxOnly`: the only context file is the project's AGENTS.md, loaded the
|
||||
// same way whatever --no-context-files says. `flood`: prints 8 MB first.
|
||||
// `hideWhen`: a cue, a flag or "order" (--offline before --no-session). With the cue
|
||||
// it loads no planted source; without it, every kind even with its seal flag on.
|
||||
// `standIn`: [flag, other]: it ignores flag, and other keeps flag's kind out instead.
|
||||
// `remember`: the first run in a scratch directory (it leaves a marker beside the
|
||||
// -e file's directory) loads every kind whatever the seal flags; a later run loads
|
||||
// none. `needs`: a flag the loader flags work only with; without it they do
|
||||
// nothing. `leakWithout`: flags; when any of them is absent, every kind loads.
|
||||
// `leakWith`: flags; when all of them are given, every kind loads (with
|
||||
// `leakWithout` too, both must hold). `mcpAlso`: a flag that keeps MCP servers out
|
||||
// as well as their own flag does. `leakNoAgentDir`: every kind loads when
|
||||
// PI_CODING_AGENT_DIR is unset, as in a bus session. `leakNoVar`: names; every
|
||||
// kind loads when any of them is unset (TERM, TZ, LC_ALL in a unit-started bus).
|
||||
// `leakNoMatch`: with -c, every
|
||||
// kind loads when no .jsonl in --session-dir names the working directory.
|
||||
// `leakEnv`: { json, other }, the variable names a run with --mode json and any
|
||||
// other run should get; every kind loads when the environment holds a name not in
|
||||
// the list or lacks one, apart from the names /bin/sh sets (PWD, OLDPWD, SHLVL, _).
|
||||
// An -e extension that writes JSON.stringify({...}) at session_start gets the
|
||||
// prompt, the command names, the session id (with -c, the id in the header of a
|
||||
// .jsonl in --session-dir whose cwd is the working directory, else a new one) and
|
||||
// the --thinking level (else "medium"). `noResume`: -c always starts a new
|
||||
// session. `oneExt`: only the first -e loads. `extNoCommands`: the extension sees
|
||||
// no commands.
|
||||
// `commands` "fail" answers get_commands with success false; "list" answers
|
||||
// with a bare list.
|
||||
// --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
|
||||
// no --no-approve. Only the project SYSTEM.md and APPEND_SYSTEM.md wait on it here. The user
|
||||
// APPEND_SYSTEM.md is appended whatever the flags, as 0.85.1 does when the project has none.
|
||||
import { FIT_FLAGS } from "../src/pi-fit.mjs";
|
||||
|
||||
const BODY = String.raw`
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const cp = require("node:child_process");
|
||||
const argv = process.argv.slice(1);
|
||||
const has = (f) => argv.includes(f);
|
||||
const val = (f) => { const i = argv.indexOf(f); return i === -1 ? undefined : argv[i + 1]; };
|
||||
if (argv[0] === "--help") {
|
||||
const drop = new Set(K.dropFlags || []);
|
||||
const lines = ["Usage: pi [options] [messages...]", "", "Options:"];
|
||||
for (const f of FLAGS) {
|
||||
if (drop.has(f)) continue;
|
||||
if (f === "--mode") lines.push(" --mode <mode> Output mode: " + (K.modes || ["text", "json", "rpc"]).join(", "));
|
||||
else if (f === "--thinking") lines.push(" --thinking <level> Thinking level: " + (K.levels || ["off", "minimal", "low", "medium", "high", "xhigh", "max"]).join(", "));
|
||||
else lines.push(" " + f);
|
||||
}
|
||||
for (const f of K.extraFlags || []) lines.push(" " + f);
|
||||
for (const l of K.helpProse || []) lines.push(l);
|
||||
process.stdout.write(lines.join("\n") + "\n");
|
||||
process.exit(0);
|
||||
}
|
||||
const rpc = val("--mode") === "rpc";
|
||||
const print = !rpc && has("-p");
|
||||
const startsAnyway = K.missingExtOk === true || (rpc && K.missingExtOk === "rpc") || (print && K.missingExtOk === "print");
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
if (argv[i] === "-e" && !fs.existsSync(argv[i + 1]) && !startsAnyway) {
|
||||
process.stderr.write("Failed to load extension: it does not exist\n");
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
if (rpc || print) {
|
||||
if (K.flood) {
|
||||
const chunk = Buffer.alloc(1024 * 1024, "x");
|
||||
for (let n = 0; n < 8; n++) for (let off = 0; off < chunk.length;) {
|
||||
try { off += fs.writeSync(1, chunk, off); } catch (e) { if (e.code !== "EAGAIN") throw e; }
|
||||
}
|
||||
}
|
||||
const ALL = ["extensions", "mcp", "skills", "prompts", "context", "approve"];
|
||||
// The id in the header of the last .jsonl in dir whose cwd is the working directory, or null.
|
||||
const storedFor = (dir) => {
|
||||
let id = null;
|
||||
if (dir) for (const n of (() => { try { return fs.readdirSync(dir); } catch { return []; } })()) {
|
||||
if (!n.endsWith(".jsonl")) continue;
|
||||
let h = null;
|
||||
try { h = JSON.parse(fs.readFileSync(path.join(dir, n), "utf8").split("\n")[0]); } catch { h = null; }
|
||||
if (h && h.type === "session" && h.cwd === process.cwd()) id = h.id;
|
||||
}
|
||||
return id;
|
||||
};
|
||||
let ignoreAll = K.ignoreAll;
|
||||
let leak = new Set(K.sealLeak || []);
|
||||
if (K.hideWhen) {
|
||||
const cued = K.hideWhen === "order" ? argv.indexOf("--offline") !== -1 && argv.indexOf("--offline") < argv.indexOf("--no-session") : has(K.hideWhen);
|
||||
if (cued) ignoreAll = true;
|
||||
else leak = new Set(ALL);
|
||||
}
|
||||
if (K.remember) {
|
||||
const marker = path.join(path.dirname(path.dirname(val("-e"))), "seen-before");
|
||||
if (fs.existsSync(marker)) ignoreAll = true;
|
||||
else { fs.writeFileSync(marker, ""); leak = new Set(ALL); }
|
||||
}
|
||||
if ((K.leakWith || K.leakWithout) && (K.leakWith || []).every(has) && (!K.leakWithout || K.leakWithout.some((f) => !has(f)))) leak = new Set(ALL);
|
||||
if (K.leakNoAgentDir && !process.env.PI_CODING_AGENT_DIR) leak = new Set(ALL);
|
||||
if (K.leakNoVar && K.leakNoVar.some((n) => process.env[n] === undefined)) leak = new Set(ALL);
|
||||
if (K.leakNoMatch && has("-c") && !storedFor(val("--session-dir"))) leak = new Set(ALL);
|
||||
if (K.leakEnv) {
|
||||
const want = new Set(K.leakEnv[val("--mode") === "json" ? "json" : "other"]);
|
||||
const got = Object.keys(process.env).filter((n) => !["PWD", "OLDPWD", "SHLVL", "_"].includes(n));
|
||||
if (got.length !== want.size || got.some((n) => !want.has(n))) leak = new Set(ALL);
|
||||
}
|
||||
// Whether a seal flag is on: with standIn, its flag reads as the other flag; with needs, only
|
||||
// when that flag is given too.
|
||||
const sealed = (flag) => has(K.standIn && K.standIn[0] === flag ? K.standIn[1] : flag) && (!K.needs || has(K.needs));
|
||||
const on = (kind, flag) => !ignoreAll && (!sealed(flag) || leak.has(kind));
|
||||
const agent = process.env.PI_CODING_AGENT_DIR || path.join(process.env.HOME || "/", ".pi", "agent");
|
||||
const cwd = process.cwd();
|
||||
const list = (dir, test) => { try { return fs.readdirSync(dir).filter(test).map((n) => path.join(dir, n)); } catch { return []; } };
|
||||
const json = (file) => { try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return null; } };
|
||||
const read = (file) => { try { return fs.readFileSync(file, "utf8"); } catch { return null; } };
|
||||
const settings = [json(path.join(agent, "settings.json")), json(path.join(cwd, ".pi", "settings.json"))].filter(Boolean);
|
||||
const trust = json(path.join(agent, "trust.json")) || {};
|
||||
const saved = settings.some((s) => s.defaultProjectTrust === "always") || trust[cwd] === true;
|
||||
const trusted = !ignoreAll && (has("--approve") || (saved && (!sealed("--no-approve") || leak.has("approve"))));
|
||||
const commands = [];
|
||||
// A planted extension writes one marker on load; an -e extension may also write what it saw at
|
||||
// session_start. Do what each would do.
|
||||
const starts = [];
|
||||
const load = (f) => {
|
||||
const text = read(f);
|
||||
if (text === null) return;
|
||||
for (const m of text.matchAll(/writeFileSync\(("(?:[^"\\]|\\.)*")(, JSON\.stringify\()?/g)) {
|
||||
if (m[2]) starts.push(JSON.parse(m[1]));
|
||||
else fs.writeFileSync(JSON.parse(m[1]), "");
|
||||
}
|
||||
};
|
||||
const given = argv.flatMap((a, i) => a === "-e" ? [argv[i + 1]] : []);
|
||||
if (!(K.dropExplicit && has("--no-extensions"))) for (const f of K.oneExt ? given.slice(0, 1) : given) load(f);
|
||||
const packages = settings.flatMap((s) => (s.packages || []).map((p) => typeof p === "string" ? p : p.source));
|
||||
if (K.trustExtOnly) {
|
||||
if (trusted) for (const f of list(path.join(cwd, ".pi", "extensions"), (n) => n.endsWith(".ts"))) load(f);
|
||||
} else if (on("extensions", "--no-extensions")) {
|
||||
const files = [...list(path.join(agent, "extensions"), (n) => n.endsWith(".ts")), ...list(path.join(cwd, ".pi", "extensions"), (n) => n.endsWith(".ts"))];
|
||||
for (const s of settings) files.push(...(s.extensions || []));
|
||||
for (const p of packages) files.push(...list(path.join(p, "extensions"), (n) => n.endsWith(".ts")));
|
||||
for (const f of files) load(f);
|
||||
}
|
||||
const mcpFlag = (K.extraFlags || []).includes("--no-mcp") ? "--no-mcp" : "--no-extensions";
|
||||
if (!ignoreAll && (!(sealed(mcpFlag) || (K.mcpAlso && has(K.mcpAlso))) || leak.has("mcp"))) {
|
||||
for (const f of [path.join(agent, "mcp.json"), path.join(cwd, ".pi", "mcp.json"), path.join(cwd, ".mcp.json")]) {
|
||||
for (const s of Object.values(json(f)?.mcpServers || {})) cp.spawnSync(s.command, s.args || [], { stdio: "ignore" });
|
||||
}
|
||||
}
|
||||
if (on("skills", "--no-skills") || (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" });
|
||||
for (const s of settings) for (const p of s.prompts || []) commands.push({ name: path.basename(p, ".md"), source: "prompt" });
|
||||
}
|
||||
// The system prompt: the --system-prompt file, else SYSTEM.md; then APPEND_SYSTEM.md; then the
|
||||
// context files, from the agent directory and from / down to the working directory.
|
||||
const sys = K.ignoreSystemPrompt ? null : val("--system-prompt");
|
||||
const own = (name) => (trusted && read(path.join(cwd, ".pi", name))) || (!ignoreAll && read(path.join(agent, name))) || "";
|
||||
const hide = K.sysReplacesAll && sys;
|
||||
const parts = [sys ? (read(sys) || "") : own("SYSTEM.md"), K.ignoreAppend || hide ? "" : own("APPEND_SYSTEM.md")];
|
||||
if (K.trustCtxOnly) {
|
||||
if (trusted) parts.push(read(path.join(cwd, "AGENTS.md")) || "");
|
||||
} else if (!hide && on("context", "--no-context-files")) {
|
||||
const dirs = [];
|
||||
for (let d = cwd; ; d = path.dirname(d)) { dirs.unshift(d); if (path.dirname(d) === d) break; }
|
||||
parts.push(read(path.join(agent, "AGENTS.md")) || "");
|
||||
for (const d of dirs) parts.push(read(path.join(d, "AGENTS.md")) || read(path.join(d, "CLAUDE.md")) || "");
|
||||
}
|
||||
// The session -c resumes: the newest .jsonl in --session-dir whose header names this cwd.
|
||||
let session = "stand-in-" + process.pid;
|
||||
const dir = val("--session-dir");
|
||||
if (has("-c") && dir && !K.noResume) session = storedFor(dir) || session;
|
||||
const start = { prompt: parts.join("\n"), commands: K.extNoCommands ? [] : commands.map((c) => c.name), session };
|
||||
if (!(print && K.printNoStart)) for (const f of starts) fs.writeFileSync(f, JSON.stringify(start));
|
||||
if (print) {
|
||||
if (K.sessionOut) process.stdout.write(K.sessionOut);
|
||||
process.exit(0);
|
||||
}
|
||||
let input = "";
|
||||
try { input = fs.readFileSync(0, "utf8"); } catch { input = ""; }
|
||||
for (const line of input.split("\n")) {
|
||||
let m;
|
||||
try { m = JSON.parse(line); } catch { continue; }
|
||||
if (m.type === "get_state") process.stdout.write(JSON.stringify({ id: m.id, type: "response", command: "get_state", success: true, data: { isStreaming: false } }) + "\n");
|
||||
if (m.type === "get_commands") {
|
||||
const answer = K.commands === "fail" ? { success: false, error: "no" } : { success: true, data: K.commands === "list" ? commands : { commands } };
|
||||
process.stdout.write(JSON.stringify({ id: m.id, type: "response", command: "get_commands", ...answer }) + "\n");
|
||||
}
|
||||
}
|
||||
process.exit(0);
|
||||
}
|
||||
if (K.sessionOut) process.stdout.write(K.sessionOut);
|
||||
process.exit(0);
|
||||
`;
|
||||
|
||||
const quote = (text) => `'${String(text).replace(/'/g, "'\\''")}'`;
|
||||
|
||||
// The script text. node: the interpreter the fit part runs on (absolute, so it runs with no PATH).
|
||||
// form "node" (#1873 S4): the same stand-in as one node script with node in its shebang, the kind
|
||||
// of file the conversation controller launches with node; its --version and $ARGV_OUT parts are
|
||||
// JavaScript too, so it needs node wherever it runs.
|
||||
export function piStandIn(knobs = {}, { node = process.execPath, form = "sh" } = {}) {
|
||||
if (form === "node") {
|
||||
const head = knobs.versionHang ? "setTimeout(() => process.exit(0), 60000); return;" : knobs.versionExit != null ? `process.exit(${Number(knobs.versionExit)});` : `process.stdout.write(${JSON.stringify(String(knobs.version ?? "9.9.9"))} + "\\n"); process.exit(0);`;
|
||||
return [
|
||||
`#!${node}`,
|
||||
`"use strict";`,
|
||||
`process.argv.splice(1, 1);`,
|
||||
`(() => {`,
|
||||
`if (process.argv[1] === "--version") { ${head} }`,
|
||||
`if (process.env.ARGV_OUT) { require("node:fs").writeFileSync(process.env.ARGV_OUT, process.argv.slice(1).map((a) => a + "\\n").join("")); process.stdout.write(${JSON.stringify(String(knobs.sessionOut ?? ""))}); process.exit(0); }`,
|
||||
`const K = ${JSON.stringify(knobs)};`,
|
||||
`const FLAGS = ${JSON.stringify(FIT_FLAGS)};`,
|
||||
BODY,
|
||||
`})();`,
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
const version = knobs.versionHang ? "sleep 60; exit 0" : knobs.versionExit != null ? `exit ${Number(knobs.versionExit)}` : `printf '%s\\n' ${quote(knobs.version ?? "9.9.9")}; exit 0`;
|
||||
const program = `"use strict";\nconst K = ${JSON.stringify(knobs)};\nconst FLAGS = ${JSON.stringify(FIT_FLAGS)};\n${BODY}`;
|
||||
return [
|
||||
"#!/bin/sh",
|
||||
`[ "$1" = --version ] && { ${version}; }`,
|
||||
`if [ -n "\${ARGV_OUT:-}" ]; then printf '%s\\n' "$@" > "$ARGV_OUT"; printf '%s' ${quote(knobs.sessionOut ?? "")}; exit 0; fi`,
|
||||
`exec ${quote(node)} -e ${quote(program)} -- "$@"`,
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
@@ -4,7 +4,8 @@ import assert from 'node:assert/strict';
|
||||
import { mkdirSync, readFileSync, statSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { buildBundle } from '../src/bundle.mjs';
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch } from './helpers.mjs';
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch, harnessReady } from './helpers.mjs';
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
|
||||
const models = {
|
||||
a: { provider: 'proxy', model: 'claude-fixture', route: 'anthropic' },
|
||||
|
||||
@@ -4,7 +4,8 @@ import { readFileSync, mkdirSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { buildBundle } from '../src/bundle.mjs';
|
||||
import { typedTools } from '../src/tools.mjs';
|
||||
import { resolvedFor, scratch, HARNESS, REPO_SKILLS } from './helpers.mjs';
|
||||
import { resolvedFor, scratch, HARNESS, REPO_SKILLS, harnessReady } from './helpers.mjs';
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
import { ownRails, railsText, decisionAutonomy } from '../../organization/src/rails.mjs';
|
||||
|
||||
const SECRET = 'sk-test-NOT-A-SECRET-1234';
|
||||
|
||||
@@ -9,7 +9,8 @@ import { createServer } from 'node:net';
|
||||
import { buildBundle, SOURCES } from '../src/bundle.mjs';
|
||||
import { callTool, typedTools } from '../src/tools.mjs';
|
||||
import * as runner from '../src/runner.mjs';
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from './helpers.mjs';
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from './helpers.mjs';
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
import { Broker } from '../../bus/src/broker.mjs';
|
||||
import { Store } from '../../bus/src/store.mjs';
|
||||
import { createRequestStore } from '../../bus/src/request-store.mjs';
|
||||
|
||||
@@ -10,7 +10,8 @@ import { chmodSync, cpSync, mkdirSync, readFileSync, rmSync, symlinkSync, writeF
|
||||
import { join } from "node:path";
|
||||
import { BundleError, buildBundle, readSkills, SKILL_BYTES } from "../src/bundle.mjs";
|
||||
import { adapterEnv } from "../src/runner.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
|
||||
const sha = (text) => createHash("sha256").update(text).digest("hex");
|
||||
const MARKED = /<skill-file skill="([^"]+)" path="([^"]+)">\n([\s\S]*?)\n<\/skill-file>/g;
|
||||
@@ -183,7 +184,8 @@ test("each adapter passes the skills only in the prompt file, with skill discove
|
||||
const b = buildBundle({ dir: join(dir, "bundle"), resolved, contract: "# Contract\n", harness: HARNESS[harness], models, skillsDir: REPO_SKILLS, session: { ...s, run: "run-1", launchedBy: "pm" } });
|
||||
s.bundle = b.files;
|
||||
s.harnessEnv = b.env;
|
||||
const env = { ...adapterEnv(s, { PATH: `${join(dir, "bin")}:/usr/bin:/bin`, HOME: join(dir, "home") }), ARGV_OUT: join(dir, "argv"), MOSAIC_REQUEST: "x" };
|
||||
// #1873 S2: the pi adapter runs MOSAIC_PI_BIN, which the bundle sets to the pi it found.
|
||||
const env = { ...adapterEnv(s, { PATH: `${join(dir, "bin")}:/usr/bin:/bin`, HOME: join(dir, "home") }), ARGV_OUT: join(dir, "argv"), MOSAIC_REQUEST: "x", ...(harness === "pi" ? { MOSAIC_PI_BIN: cli } : {}) };
|
||||
const r = spawnSync("/bin/sh", [join(REPO, "adapters", harness === "pi" ? "pi" : "claude", "adapter.sh")], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], env });
|
||||
assert.equal(r.status, 0, `${harness}: ${r.stderr}`);
|
||||
const argv = readFileSync(join(dir, "argv"), "utf8").split("\0");
|
||||
|
||||
@@ -9,7 +9,8 @@ import { join } from "node:path";
|
||||
// A namespace import, so the red-before test runs (and fails on its assertion) on a base without
|
||||
// the section's exports.
|
||||
import * as bundleModule from "../src/bundle.mjs";
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
import { REPO_ROLES } from "../../organization/tests/helpers.mjs";
|
||||
|
||||
const { buildBundle, sessionModel } = bundleModule;
|
||||
|
||||
@@ -7,11 +7,67 @@ import { homedir } from 'node:os';
|
||||
import { fetchPinned } from './fetch.mjs';
|
||||
import { PIN } from './common.mjs';
|
||||
import { localRide, riddenOrgs } from './ride.mjs';
|
||||
import { releaseState, initState, fenceState, initLockStatus, clearInitLock, appendReceipt, recentReceipts, receiptsText, createUnitControl, FILE_LABELS } from './admin.mjs';
|
||||
import { releaseState, initState, fenceState, initLockStatus, clearInitLock, appendReceipt, recentReceipts, receiptsText, createUnitControl, FILE_LABELS, LOG_LINES, receiptHistory, serviceKeyState } from './admin.mjs';
|
||||
import { secretValues } from './redact.mjs';
|
||||
import { discoverModules, hasModule } from '../../plugins/src/loader.mjs';
|
||||
import { readPluginLock } from '../../plugins/src/pinned.mjs';
|
||||
import { PROVIDERS, publicProviders } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
|
||||
export const proxyCode = (error, fallback) => (/^proxy-[a-z-]+$/.test(error?.code ?? '') ? error.code : fallback);
|
||||
const failure = code => Object.assign(new Error(code), { code });
|
||||
|
||||
// #1873 settings-backend A2: the harness programs installed on this instance, from the plugin tree
|
||||
// through the plugins loader and from the instance's plugin lock, the way the bus lists them
|
||||
// (runtime.mjs harnessLevels). No name list here. A tree that cannot be read answers null; a lock
|
||||
// that cannot be read adds nothing, so a pinned program is then not offered.
|
||||
export function installedHarnesses(dataRoot = null) {
|
||||
let names;
|
||||
try { names = discoverModules('harness').names.filter(name => hasModule('harness', name)); } catch { return null; }
|
||||
if (dataRoot !== null) {
|
||||
try { names = [...new Set([...names, ...Object.keys(readPluginLock(dataRoot).plugins).filter(name => hasModule('harness', name, dataRoot))])]; } catch { /* the tree's list */ }
|
||||
}
|
||||
return names.sort();
|
||||
}
|
||||
|
||||
// #1873 settings-backend A2: every harness an organization document names for an agent: each
|
||||
// `harness` or `fallback.harness` string anywhere in it (role vars, project agent vars, or any
|
||||
// later place), outside its plugins list. Broader than the routes view on purpose: a program is
|
||||
// never turned off where anything in the file still names it.
|
||||
export function harnessesNamed(document) {
|
||||
const named = new Set();
|
||||
const walk = (value, depth) => {
|
||||
if (depth > 32 || value === null || typeof value !== 'object') return;
|
||||
if (Array.isArray(value)) { for (const item of value) walk(item, depth + 1); return; }
|
||||
for (const [key, item] of Object.entries(value)) {
|
||||
if ((key === 'harness' || key === 'fallback.harness') && typeof item === 'string') named.add(item);
|
||||
else walk(item, depth + 1);
|
||||
}
|
||||
};
|
||||
if (document === null || typeof document !== 'object' || Array.isArray(document)) return null;
|
||||
for (const [key, value] of Object.entries(document)) if (key !== 'plugins') walk(value, 1);
|
||||
return named;
|
||||
}
|
||||
|
||||
// #1873 settings-backend A2 (review P1-1): the harnesses the root agents an organization takes name.
|
||||
// `root` is what loadRootAgents answered ({state, agents, assistant, definitions?}). An organization
|
||||
// takes every root agent except the root assistant and the ones that exclude it, the way
|
||||
// materializeCopies copies them at bus boot. Anything not in that shape answers null (unreadable).
|
||||
export function rootHarnessesNamed(root, orgId) {
|
||||
if (root === null || typeof root !== 'object' || root.agents === null || typeof root.agents !== 'object' || Array.isArray(root.agents)) return null;
|
||||
const taken = {};
|
||||
for (const [name, entry] of Object.entries(root.agents)) {
|
||||
if (entry === null || typeof entry !== 'object' || !Array.isArray(entry.excludeOrgs)) return null;
|
||||
if (entry.definition === 'assistant' || entry.excludeOrgs.includes(orgId)) continue;
|
||||
taken[name] = entry.vars ?? {};
|
||||
}
|
||||
return harnessesNamed({ agents: taken, definitions: root.definitions ?? {} });
|
||||
}
|
||||
// A key id is the first 12 hex of the key's sha256. A label is a short plain name; anything that
|
||||
// looks like a Mosaic key prefix is dropped, so an answer can never carry key text.
|
||||
const KEY_ID = /^[a-f0-9]{12}$/;
|
||||
const plainLabel = value => (typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/.test(value) && !/ms[kotp][-_]/i.test(value) ? value : null);
|
||||
const isoTime = value => (typeof value === 'string' && /^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:.]{1,18}Z$/.test(value) ? value : null);
|
||||
|
||||
// `buses()` proves the init-lock condition "bus stopped" ({stopped, running:[label]}).
|
||||
// `orgs()` lists the organizations this proxy serves: [{id, catalogue}]. `unit` and `fetch` are
|
||||
// (and `pin`) seams for tests; the defaults run the real proxy-service.sh, systemctl and pinned fetch.
|
||||
@@ -20,7 +76,11 @@ const failure = code => Object.assign(new Error(code), { code });
|
||||
// change log, `call(verb, args)` reaches its bus as the owner and `status()` is its bus status. `ownerModels()` answers
|
||||
// {proxy, riding, models} from the riding buses' catalogues. `move(id, models, principal)` moves a
|
||||
// stopped fixture organization to the owner's models through the organization service.
|
||||
export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir = null, unit = null, fetch = fetchPinned, buses, orgs = async () => [], riders = async () => [], ownerModels = async () => ({ proxy: false, riding: false, models: [] }), move = null, now = () => new Date(), env = process.env, pin = PIN }) {
|
||||
// #1873 settings-backend A2: a rider may also have `file()`, its organization file as the writer
|
||||
// reads it ({document, revision}), read only. `harnesses()` lists the installed harness programs.
|
||||
// `rootAgents()` reads the root agent file (loadRootAgents); with none given, or one that throws,
|
||||
// no organization that has the program on is written (unreadable).
|
||||
export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir = null, unit = null, fetch = fetchPinned, buses, orgs = async () => [], riders = async () => [], harnesses = () => installedHarnesses(dataRoot), rootAgents = null, ownerModels = async () => ({ proxy: false, riding: false, models: [] }), move = null, now = () => new Date(), env = process.env, pin = PIN, providers = PROVIDERS }) {
|
||||
const port = () => { try { return initState(configDir, dataRoot).settings?.port ?? null; } catch { return null; } };
|
||||
const control = unit ?? createUnitControl({ instance, unitDir, port, env });
|
||||
let job = { state: 'idle', step: null, receipt: null, error: null };
|
||||
@@ -97,6 +157,83 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
return (await riders()).map(r => { const state = decide(r, wasRidden); return state === 'fixture' && typeof r.status !== 'function' ? { id: r.id, state, running: null } : { id: r.id, state }; });
|
||||
} catch { return null; }
|
||||
}
|
||||
// #1873 settings-backend A2, item 5: which harness programs each organization has on, from its
|
||||
// bus's plugins read as the owner. null when the read fails, its answer has no harness list, or
|
||||
// any entry is not a plugin name (review P3-3: no bus text reaches the page).
|
||||
const harnessList = value => (Array.isArray(value?.plugins?.harness) && value.plugins.harness.every(n => typeof n === 'string' && /^[a-z][a-z0-9-]{0,63}$/.test(n)) ? [...value.plugins.harness] : null);
|
||||
async function programsSummary() {
|
||||
let installed = null;
|
||||
try { installed = harnesses(); if (!Array.isArray(installed)) installed = null; } catch { installed = null; }
|
||||
let list;
|
||||
try { list = await riders(); } catch { return { installed, organizations: null }; }
|
||||
const organizations = await Promise.all(list.map(async rider => {
|
||||
try { return { id: rider.id, on: harnessList(await rider.call('plugins')) }; } catch { return { id: rider.id, on: null }; }
|
||||
}));
|
||||
return { installed, organizations };
|
||||
}
|
||||
// Item 5, one organization: turn `name` off unless something still uses it or it is the only one.
|
||||
// Three reads (the plugins read, the routes view and the organization file) must show one revision;
|
||||
// the write carries it, so a change after them is refused by the writer (revision-conflict) and
|
||||
// reported, never retried. Every skip writes nothing. `root` is the root agent file read once per
|
||||
// call (undefined when it could not be read): the root agents this organization takes count too.
|
||||
async function offOne(rider, name, root) {
|
||||
const id = rider.id;
|
||||
let current;
|
||||
try { current = await rider.call('plugins'); } catch { return { id, outcome: 'bus-down' }; }
|
||||
const on = harnessList(current);
|
||||
if (on === null || typeof current?.revision !== 'string' || !/^[a-f0-9]{64}$/.test(current.revision)) return { id, outcome: 'unreadable' };
|
||||
if (!on.includes(name)) return { id, outcome: 'already-off' };
|
||||
let routes = null;
|
||||
try { routes = await rider.call('routes'); } catch (error) { if (error?.code !== 'launch-not-configured') return { id, outcome: 'unreadable' }; }
|
||||
let file;
|
||||
try { file = typeof rider.file === 'function' ? await rider.file() : null; } catch { file = null; }
|
||||
const named = harnessesNamed(file?.document);
|
||||
const fromRoot = root === undefined ? null : rootHarnessesNamed(root, id);
|
||||
if (named === null || fromRoot === null || typeof file?.revision !== 'string') return { id, outcome: 'unreadable' };
|
||||
for (const harness of fromRoot) named.add(harness);
|
||||
if (file.revision !== current.revision || (routes !== null && routes?.revision !== current.revision)) return { id, outcome: 'failed:revision-conflict' };
|
||||
// The routes view must be readable when there is one; each instance's route and fallback count.
|
||||
if (routes !== null) {
|
||||
if (!routes || typeof routes.instances !== 'object' || routes.instances === null) return { id, outcome: 'unreadable' };
|
||||
for (const route of Object.values(routes.instances)) {
|
||||
if (typeof route?.harness === 'string') named.add(route.harness);
|
||||
if (typeof route?.fallback?.harness === 'string') named.add(route.fallback.harness);
|
||||
}
|
||||
}
|
||||
// The harness rule: any route on this program, whatever account its model uses, keeps it on.
|
||||
if (named.has(name)) return { id, outcome: 'in-use' };
|
||||
if (on.length === 1) return { id, outcome: 'last-program' };
|
||||
try { await rider.call('plugins.set', { revision: current.revision, plugins: { harness: on.filter(n => n !== name) } }); }
|
||||
catch (error) { return { id, outcome: `failed:${failCode(error)}` }; }
|
||||
return { id, outcome: 'off' };
|
||||
}
|
||||
// Item 8, one organization: its proxy client keys by key id, from proxy.keys as the owner. Never
|
||||
// the usage queue. A key value never reaches here: proxy.keys answers ids, and every other field
|
||||
// is checked against a plain pattern before it is passed on.
|
||||
async function accessOne(rider) {
|
||||
const id = rider.id;
|
||||
if (typeof rider.fixture !== 'boolean' || typeof rider.cliproxy !== 'boolean') return { id, health: 'unreadable', reason: 'unreadable', keys: [] };
|
||||
if (rider.fixture || !rider.cliproxy) return { id, health: 'not-riding', keys: [] };
|
||||
let answer;
|
||||
try { answer = await rider.call('proxy.keys'); }
|
||||
catch (error) {
|
||||
const code = error?.code;
|
||||
if (code === 'proxy-uncertain') return { id, health: 'attention', reason: 'proxy-uncertain', keys: [] };
|
||||
const reason = code === 'not-configured' ? 'no-proxy-client' : code === 'outcome-unknown' || code === 'no-bus-host' ? 'bus-down' : 'unreadable';
|
||||
return { id, health: 'unreadable', reason, keys: [] };
|
||||
}
|
||||
if (!Array.isArray(answer?.keys)) return { id, health: 'unreadable', reason: 'unreadable', keys: [] };
|
||||
const keys = [];
|
||||
let other = 0, sentinel = 0;
|
||||
for (const key of answer.keys) {
|
||||
if (!key || typeof key !== 'object' || !KEY_ID.test(key.keyId ?? '')) return { id, health: 'unreadable', reason: 'unreadable', keys: [] };
|
||||
if (key.management === 'sentinel') sentinel++;
|
||||
else if (key.management === 'stack-managed') keys.push({ keyId: key.keyId, agent: plainLabel(key.agent), state: plainLabel(key.state), issuedAt: isoTime(key.issuedAt) });
|
||||
else other++;
|
||||
}
|
||||
const health = keys.length === 0 ? 'missing' : keys.every(k => k.state === 'active') ? 'ok' : 'attention';
|
||||
return { id, health, keys, other, sentinel };
|
||||
}
|
||||
function nextSteps({ release, init, unitState, fence, lock }) {
|
||||
if (!release.fetched || release.problem) return ['fetch'];
|
||||
if (lock.present) return lock.clearable ? ['init-lock-clear'] : ['init-lock-wait'];
|
||||
@@ -122,6 +259,7 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
let organizations = null;
|
||||
try { organizations = await orgs(); } catch { organizations = null; }
|
||||
const ride = await rideSummary();
|
||||
const programs = await programsSummary();
|
||||
const next = nextSteps({ release, init, unitState, fence, lock });
|
||||
// Ride all comes after the proxy runs with no fence, while an organization can still ride.
|
||||
if (next[0] === 'accounts' && ride?.some(r => r.state === 'ready' || r.state === 'fixture')) next.push('ride-all');
|
||||
@@ -136,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,
|
||||
@@ -143,6 +283,7 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
// needs a config format that does not exist yet.
|
||||
ownProxy: { available: false, state: 'not built', reason: 'This instance has one shared proxy. A separate proxy per organization is not built yet.' },
|
||||
ride,
|
||||
programs,
|
||||
next,
|
||||
};
|
||||
},
|
||||
@@ -254,6 +395,65 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
if (typeof org !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(org)) throw failure('proxy-org-invalid');
|
||||
return receipt({ op: 'ride-removed', org });
|
||||
},
|
||||
// #1873 settings-backend A1: three reads for the instance owner's setup page.
|
||||
// The proxy unit's last journal lines, redacted. A unit that is not installed is not-configured;
|
||||
// a unit state that cannot be read is outcome-unknown, as is a journal that cannot be read.
|
||||
async log({ lines = LOG_LINES.default } = {}) {
|
||||
if (typeof control.log !== 'function') throw failure('not-configured');
|
||||
const state = await unitStatus();
|
||||
if (state.error) throw failure('outcome-unknown');
|
||||
if (!state.installed) throw failure('not-configured');
|
||||
return control.log(lines, { secrets: secretValues(env) });
|
||||
},
|
||||
// When the service key was made, from proxy.json's mtime; never the key or any part of it.
|
||||
serviceKey() { return serviceKeyState(configDir); },
|
||||
// The setup history, a page at a time (receiptHistory).
|
||||
history({ limit, before = null } = {}) { return receiptHistory(dataRoot, { limit, before, secrets: secretValues(env) }); },
|
||||
// An account change made through /api/proxy/* or an organization's /api/orgs/<id>/proxy/*,
|
||||
// recorded after its bus call answered. Labels only: the provider id, a 12-hex account id, a
|
||||
// priority or a model count, the organization whose route carried it, and the signed-in name.
|
||||
// Never a key, a login state, a callback URL, a model list or a body. Answers null when the verb
|
||||
// is not an account change, true when recorded and false when the append failed.
|
||||
recordAccount({ verb, args = {}, result = null, user = null, org = null } = {}) {
|
||||
const label = value => (typeof value === 'string' && /^[a-z0-9][a-z0-9._-]{0,31}$/.test(value) ? value : null);
|
||||
const account = value => (typeof value === 'string' && /^[a-f0-9]{12}$/.test(value) ? value : null);
|
||||
let record;
|
||||
if (verb === 'proxy.apikey.add') record = { op: 'account.proxy.apikey.add', provider: label(args.provider) };
|
||||
else if (verb === 'proxy.login.status' && result?.status === 'ok') record = { op: 'account.proxy.login.complete' };
|
||||
else if (verb === 'proxy.priority.set') record = { op: 'account.proxy.priority.set', accountId: account(args.accountId), priority: Number.isSafeInteger(args.priority) ? args.priority : null };
|
||||
else if (verb === 'proxy.group.models.set') record = { op: 'account.proxy.group.models.set', accountId: account(args.accountId), models: Array.isArray(args.models) ? args.models.length : null };
|
||||
else return null;
|
||||
if (typeof org === 'string' && /^[a-z0-9][a-z0-9._-]{0,63}$/.test(org)) record.org = org;
|
||||
if (typeof user === 'string' && user) record.user = user.slice(0, 64);
|
||||
try { receipt(record); return true; } catch { return false; }
|
||||
},
|
||||
// #1873 settings-backend A2, item 5: turns one harness program off for every current
|
||||
// organization that can do without it, through each organization's bus as the owner (the way
|
||||
// Ride all writes). Sequential, one organization at a time; one receipt names the outcome ids.
|
||||
async programsOff({ name, user = null } = {}) {
|
||||
return exclusive('programs', async () => {
|
||||
let installed;
|
||||
try { installed = harnesses(); } catch { installed = null; }
|
||||
if (typeof name !== 'string' || !Array.isArray(installed) || !installed.includes(name)) throw failure('invalid-request');
|
||||
const list = await riders();
|
||||
let root;
|
||||
try { root = typeof rootAgents === 'function' ? await rootAgents() : undefined; } catch { root = undefined; }
|
||||
const results = [];
|
||||
for (const rider of list) results.push(await offOne(rider, name, root));
|
||||
const record = { op: 'programs.off', name, results: results.map(r => ({ id: r.id, outcome: r.outcome })) };
|
||||
if (typeof user === 'string' && user) record.user = user.slice(0, 64);
|
||||
// Each write is the organization's own change log entry, so a receipt that cannot be
|
||||
// written still answers what happened, with no receipt.
|
||||
let written = null;
|
||||
try { written = receipt(record); } catch { written = null; }
|
||||
return { name, results, receipt: written };
|
||||
});
|
||||
},
|
||||
// #1873 settings-backend A2, item 8: each organization's proxy client keys and their health.
|
||||
async access() {
|
||||
const list = await riders();
|
||||
return { organizations: await Promise.all(list.map(rider => accessOne(rider).catch(() => ({ id: rider.id, health: 'unreadable', reason: 'unreadable', keys: [] })))) };
|
||||
},
|
||||
async initLockClear() {
|
||||
return exclusive('init-lock-clear', async () => ({ receipt: clearInitLock({ configDir, dataRoot, buses: await buses(), now }) }));
|
||||
},
|
||||
|
||||
@@ -9,6 +9,7 @@ import { execFile } from 'node:child_process';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { PIN, refuse } from './common.mjs';
|
||||
import { installPaths, loadProxy, verifyInstall } from './settings.mjs';
|
||||
import { holdsSecret, redactLine, redactValue } from './redact.mjs';
|
||||
|
||||
const REPO = fileURLToPath(new URL('../../../', import.meta.url)).replace(/\/$/, '');
|
||||
export const INSTANCE_NAME = /^[a-z][a-z0-9]{1,15}$/;
|
||||
@@ -142,6 +143,135 @@ export function recentReceipts(dataRoot, limit = 10) {
|
||||
} finally { fs.closeSync(fd); }
|
||||
}
|
||||
|
||||
// #1873 settings-backend A1 (item 9): the setup history, one page at a time, newest page first and
|
||||
// oldest first within a page. `before` is a byte offset the previous page answered as `next`: the
|
||||
// start of a line. The file is read the way ownedText reads (no symlink, no FIFO, owned by the
|
||||
// Console's user, no group or other bits) and backward from `before` in 64 KB chunks. A partial
|
||||
// last line (a write in progress) waits for a later read; a line that does not parse, or is longer
|
||||
// than 64 KB, is skipped. Each entry is redacted (redact.mjs) before it is answered, so a receipt
|
||||
// that ever held key text shows none. An entry whose redacted values read together, or whose keys
|
||||
// and values read together, still hold an environment secret split across them is answered as
|
||||
// { withheld: true }; pieces kept apart by another whole field are not seen (known limit). A missing
|
||||
// file is an empty history; any other failure to read refuses outcome-unknown. `uid` is a test seam.
|
||||
export const HISTORY_LIMIT = Object.freeze({ min: 1, max: 100, default: 25 });
|
||||
const HISTORY_CHUNK = 65536, HISTORY_LINE_MAX = 65536, HISTORY_SCAN_MAX = 2000;
|
||||
// A redacted entry's values joined in order, with each object key before its value when `keys`.
|
||||
const entryText = (value, keys) => (value !== null && typeof value === 'object'
|
||||
? Object.entries(value).map(([key, item]) => (keys && !Array.isArray(value) ? key : '') + entryText(item, keys)).join('')
|
||||
: String(value));
|
||||
export function receiptHistory(dataRoot, { limit = HISTORY_LIMIT.default, before = null, uid = process.getuid(), secrets = [] } = {}) {
|
||||
if (!Number.isSafeInteger(limit)) refuse('invalid-request');
|
||||
const n = Math.min(HISTORY_LIMIT.max, Math.max(HISTORY_LIMIT.min, limit));
|
||||
if (before !== null && !(Number.isSafeInteger(before) && before >= 0)) refuse('invalid-request');
|
||||
let fd;
|
||||
try { fd = fs.openSync(receiptFile(dataRoot), fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK); }
|
||||
catch (error) {
|
||||
if (error.code !== 'ENOENT') refuse('outcome-unknown');
|
||||
if (before !== null && before !== 0) refuse('invalid-request');
|
||||
return { entries: [], next: null };
|
||||
}
|
||||
try {
|
||||
let stat;
|
||||
try { stat = fs.fstatSync(fd); } catch { refuse('outcome-unknown'); }
|
||||
if (!stat.isFile() || stat.uid !== uid || (stat.mode & 0o077)) refuse('outcome-unknown');
|
||||
const size = stat.size;
|
||||
if (before !== null && before > size) refuse('invalid-request');
|
||||
// A window over the file: `buf` holds bytes [start, start + buf.length).
|
||||
let start = 0, buf = Buffer.alloc(0);
|
||||
const readAt = (offset, length) => {
|
||||
const out = Buffer.alloc(length);
|
||||
let got;
|
||||
try { got = fs.readSync(fd, out, 0, length, offset); } catch { refuse('outcome-unknown'); }
|
||||
if (got !== length) refuse('outcome-unknown');
|
||||
return out;
|
||||
};
|
||||
// The offset of the last newline in [0, from), or -1.
|
||||
const lastNewline = from => {
|
||||
let pos = from;
|
||||
while (pos > 0) {
|
||||
if (!(pos > start && pos <= start + buf.length)) { start = Math.max(0, pos - HISTORY_CHUNK); buf = readAt(start, pos - start); }
|
||||
const i = buf.lastIndexOf(0x0a, pos - start - 1);
|
||||
if (i >= 0) return start + i;
|
||||
pos = start;
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
const bytes = (from, to) => (from >= start && to <= start + buf.length ? buf.subarray(from - start, to - start) : readAt(from, to - from));
|
||||
// `cursor` is always 0 or the offset just after a newline: the end of the next line to read.
|
||||
let cursor;
|
||||
if (before === null) cursor = lastNewline(size) + 1;
|
||||
else if (before > 0 && bytes(before - 1, before)[0] !== 0x0a) refuse('invalid-request');
|
||||
else cursor = before;
|
||||
const entries = [];
|
||||
for (let scanned = 0; cursor > 0 && entries.length < n && scanned < HISTORY_SCAN_MAX; scanned++) {
|
||||
const lineStart = lastNewline(cursor - 1) + 1, lineEnd = cursor - 1;
|
||||
if (lineEnd - lineStart <= HISTORY_LINE_MAX) {
|
||||
let value = null;
|
||||
try { value = JSON.parse(bytes(lineStart, lineEnd).toString('utf8')); } catch { /* torn line */ }
|
||||
if (value && typeof value === 'object' && !Array.isArray(value) && typeof value.op === 'string') {
|
||||
const safe = redactValue(value, secrets);
|
||||
const split = holdsSecret(entryText(safe, false), secrets) || holdsSecret(entryText(safe, true), secrets);
|
||||
entries.push(split ? { withheld: true } : safe);
|
||||
}
|
||||
}
|
||||
cursor = lineStart;
|
||||
}
|
||||
entries.reverse();
|
||||
return { entries, next: cursor > 0 ? cursor : null };
|
||||
} finally { fs.closeSync(fd); }
|
||||
}
|
||||
|
||||
// #1873 settings-backend A1 (item 7): when the service key was made. proxy.json is created in the
|
||||
// same init step as the management key, with exclusive create, and is never rewritten, so its
|
||||
// mtime is the key's creation time. Only proxy.json is lstat'ed: the key file is never opened or
|
||||
// stat'ed, and nothing is read. A missing proxy.json is `missing`; a symlink, a non-file, one owned
|
||||
// by another user or one that cannot be examined is `unreadable`, with no date. There is no
|
||||
// renewal yet (proxy-scope: no rotation in the MVP), so renewedAt is null and renewal 'not-built'.
|
||||
export function serviceKeyState(configDir, { uid = process.getuid() } = {}) {
|
||||
const out = { madeAt: null, renewedAt: null, renewal: 'not-built', problem: null };
|
||||
let stat;
|
||||
try { stat = fs.lstatSync(path.join(configDir, 'proxy.json')); }
|
||||
catch (error) { out.problem = error.code === 'ENOENT' ? 'missing' : 'unreadable'; return out; }
|
||||
if (!stat.isFile() || stat.uid !== uid || !Number.isFinite(stat.mtimeMs) || stat.mtimeMs <= 0) { out.problem = 'unreadable'; return out; }
|
||||
out.madeAt = new Date(stat.mtimeMs).toISOString();
|
||||
return out;
|
||||
}
|
||||
|
||||
// #1873 settings-backend A1 (item 3): the proxy unit's last journal lines, made safe to show. The
|
||||
// unit name is unitName()'s, never the caller's; `lines` is clamped to 1..200. journalctl runs
|
||||
// through `run` (defaultRun: its timeout and 1 MB cap). A missing journalctl, a nonzero exit or a
|
||||
// timeout refuses outcome-unknown, with no lines and no other source tried. Each line is redacted
|
||||
// (redact.mjs) and cut to 300 characters; a line that cannot be shown is dropped, never shown raw.
|
||||
// A secret from `secrets` found across the shown lines, though in no one line, drops every line.
|
||||
export const LOG_LINES = Object.freeze({ min: 1, max: 200, default: 50 });
|
||||
export async function unitLog({ run = defaultRun, unit, lines = LOG_LINES.default, env, secrets = [] } = {}) {
|
||||
if (typeof unit !== 'string' || !/^mosaic-proxy(-[a-z][a-z0-9]{1,15})?\.service$/.test(unit)) refuse('proxy-unit-usage');
|
||||
if (!Number.isSafeInteger(lines)) refuse('invalid-request');
|
||||
const n = Math.min(LOG_LINES.max, Math.max(LOG_LINES.min, lines));
|
||||
let result;
|
||||
try { result = await run('journalctl', ['--user', '-u', unit, '-n', String(n), '-o', 'cat', '--no-pager', '-q'], { env, timeout: 15000 }); }
|
||||
catch { refuse('outcome-unknown'); }
|
||||
if (!result || result.code !== 0 || typeof result.stdout !== 'string') refuse('outcome-unknown');
|
||||
const raw = result.stdout.split('\n');
|
||||
if (raw.at(-1) === '') raw.pop();
|
||||
const kept = raw.slice(-n);
|
||||
const out = [];
|
||||
let redacted = 0, dropped = 0;
|
||||
try {
|
||||
for (const line of kept) {
|
||||
const safe = redactLine(line, secrets);
|
||||
if (safe.text === null) { if (line.trim()) dropped++; continue; }
|
||||
if (safe.redacted) redacted++;
|
||||
out.push(safe.text);
|
||||
}
|
||||
// The lines that will be shown, read together: a secret split across them drops them all.
|
||||
// Only environment secrets are checked this way. A key pattern broken across two journal
|
||||
// lines, each part too short for the long-run rules, is a known limit shared with chat-fail.
|
||||
if (holdsSecret(out.join(''), secrets)) { dropped += out.length; out.length = 0; }
|
||||
} catch { refuse('outcome-unknown'); }
|
||||
return { unit, lines: out, redacted, dropped };
|
||||
}
|
||||
|
||||
export function unitName(instance) {
|
||||
if (instance !== null && instance !== undefined && instance !== 'dev' && !INSTANCE_NAME.test(instance)) refuse('proxy-unit-usage');
|
||||
return instance && instance !== 'dev' ? `mosaic-proxy-${instance}.service` : 'mosaic-proxy.service';
|
||||
@@ -215,6 +345,8 @@ export function createUnitControl({ instance = null, port = () => null, unitDir
|
||||
return {
|
||||
unit,
|
||||
status,
|
||||
// settings-backend A1: the unit's last journal lines, redacted (unitLog).
|
||||
log(lines, { secrets = [] } = {}) { return unitLog({ run, unit, lines, env, secrets }); },
|
||||
async render() { return (await service('render')).stdout; },
|
||||
async install() { await service('install'); return status(); },
|
||||
async start() {
|
||||
|
||||
@@ -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,92 @@
|
||||
// #1873 settings-backend A1: redaction for the model service's log lines and the setup history,
|
||||
// read by the instance owner on the setup page.
|
||||
//
|
||||
// It is the redaction chat-fail landed for a failed turn's cause (packages/harness/src/failure-notice.mjs,
|
||||
// its REDACT list), with Mosaic's own key prefixes added to the key-prefix line: mso- (an organization's
|
||||
// model key), mst- (the Triage key) and msp-, beside msk- (the owner's). One line is added after it:
|
||||
// those four Mosaic prefixes with no word boundary, so a key glued to a word (xmso-...) is caught
|
||||
// too. Over-redaction there is the safe side. failure-notice.mjs does not
|
||||
// export its list, and packages/harness/ belongs to another lane now, so the list is copied here.
|
||||
// tests/redact.test.mjs fails when a copied line no longer matches failure-notice.mjs.
|
||||
//
|
||||
// A text is decoded (%XX, HTML references, NFKC) and cleaned (ANSI, controls, whitespace) with
|
||||
// failure-notice's own exported helpers before it is redacted. A text that holds one of the
|
||||
// Console's environment secrets (secretValues), even split by spaces or controls, is withheld
|
||||
// whole. A text that redacts to nothing but [redacted] says nothing and is dropped.
|
||||
import { cleanText, decodeText } from '../../harness/src/failure-notice.mjs';
|
||||
|
||||
export { secretValues } from '../../harness/src/failure-notice.mjs';
|
||||
|
||||
export const LINE_MAX = 300;
|
||||
export const REDACTED = '[redacted]';
|
||||
|
||||
// Copied from failure-notice.mjs. The key-prefix line adds mso, mst and msp, and the line after
|
||||
// it is Mosaic's own; every other line is failure-notice's.
|
||||
const KEY_WORD = "(?:\\w*[_-])?(?:api[_-]?key|key|token|secret|password|passwd|authorization|cookie|session)";
|
||||
const REDACT = [
|
||||
/(?<![a-z0-9+.-])[a-z0-9+.-]+:\/\/\S*/gi, // any link, its userinfo and query included, from the run's start
|
||||
/\bwww\.\S+/gi,
|
||||
/(?<!\S)(?=\S*\S@\S)\S+/g, // email addresses and user@host: the whole run holding the @
|
||||
/(?:^|(?<=[\s("'=:]))(?:~|\.{1,2})?\/\S+/g, // absolute and home paths
|
||||
/\b[A-Za-z]:\\\S*/g, // Windows paths
|
||||
/\\\\\S+/g, // UNC paths
|
||||
/\b(?:bearer|basic)\s+\S+/gi,
|
||||
new RegExp(`\\b${KEY_WORD}["']?\\s*[:=]\\s*(?:"[^"]*"?|'[^']*'?|[^\\s,;}]+)`, "gi"),
|
||||
new RegExp(`\\b${KEY_WORD}["']?\\s+(?:"[^"]*"?|'[^']*'?|(?=[^\\s"',;}]*\\d)[^\\s"',;}]+|[^\\s"',;}]{8,})`, "gi"),
|
||||
/\b(?:sk|msk|mso|mst|msp|rk|pk|ghp|gho|ghs|glpat|xox[a-z])[-_]\S+/gi,
|
||||
/ms[kotp][-_]\S+/gi, // Mosaic's prefixes glued to a word: added here, not in failure-notice.mjs
|
||||
/\beyJ\S+/g, // JWT
|
||||
/[A-Za-z0-9_\-+/=.]{32,}/g,
|
||||
/(?<![A-Za-z0-9_\-+/=.])(?=[A-Za-z0-9_\-+/=.]*\d)(?=[A-Za-z0-9_\-+/=.]*[A-Za-z])[A-Za-z0-9_\-+/=.]{16,}/g,
|
||||
];
|
||||
|
||||
// The text with every space, mark and separator gone, after cleanText, for the secret check.
|
||||
// cleanText has already removed ANSI, format, surrogate and private-use characters and turned
|
||||
// controls into spaces, so this removes the same set failure-notice's compactText does.
|
||||
const compact = clean => clean.replace(/[\p{M}\p{Z}\s]/gu, '');
|
||||
|
||||
function cap(text, n = LINE_MAX) {
|
||||
const chars = Array.from(text);
|
||||
return chars.length <= n ? text : chars.slice(0, n - 3).join('') + '...';
|
||||
}
|
||||
|
||||
// One text made safe to show. Answers { text, redacted } where text is null when nothing can be
|
||||
// shown: an empty text, one holding a secret (withheld: true), or one that redacts to only
|
||||
// [redacted]. Redaction runs over the whole text before the cap, so the cut never parts a key word
|
||||
// from its value. `secrets` is secretValues(env).
|
||||
export function redactLine(line, secrets = []) {
|
||||
const clean = cleanText(decodeText(String(line)));
|
||||
if (!clean) return { text: null, redacted: false };
|
||||
if (secrets.length) {
|
||||
const flat = compact(clean);
|
||||
if (secrets.some(value => flat.includes(value))) return { text: null, redacted: true, withheld: true };
|
||||
}
|
||||
let shown = clean;
|
||||
for (const re of REDACT) shown = shown.replace(re, REDACTED);
|
||||
shown = shown.replace(/(?:\[redacted\]\s*){2,}/g, `${REDACTED} `).trim();
|
||||
if (!shown || shown === REDACTED) return { text: null, redacted: true };
|
||||
return { text: cap(shown), redacted: shown !== clean };
|
||||
}
|
||||
|
||||
// True when the whole text, joined with no spaces, holds a secret: one split across lines is seen.
|
||||
export function holdsSecret(text, secrets = []) {
|
||||
if (!secrets.length) return false;
|
||||
const flat = compact(cleanText(decodeText(String(text))));
|
||||
return secrets.some(value => flat.includes(value));
|
||||
}
|
||||
|
||||
// A stored value made safe to show: strings are redacted, and a string that cannot be shown
|
||||
// becomes [redacted]. Keys that redaction would change are dropped with their value. Depth and
|
||||
// size are bounded; anything past the bound becomes null.
|
||||
export function redactValue(value, secrets = [], depth = 0) {
|
||||
if (value === null || typeof value === 'number' || typeof value === 'boolean') return value;
|
||||
if (typeof value === 'string') return value === '' ? '' : redactLine(value, secrets).text ?? REDACTED;
|
||||
if (depth >= 4 || typeof value !== 'object') return null;
|
||||
if (Array.isArray(value)) return value.slice(0, 64).map(item => redactValue(item, secrets, depth + 1));
|
||||
const out = {};
|
||||
for (const [key, item] of Object.entries(value).slice(0, 64)) {
|
||||
if (!/^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(key) || redactLine(key, secrets).text !== key) continue;
|
||||
out[key] = redactValue(item, secrets, depth + 1);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -7,7 +7,10 @@ import fs from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { createProxyAdmin } from '../src/admin-control.mjs';
|
||||
import { createProxyAdmin, installedHarnesses, harnessesNamed, rootHarnessesNamed } from '../src/admin-control.mjs';
|
||||
import { pluginHash } from '../../plugins/src/pinned.mjs';
|
||||
import { installPlugin } from '../../plugins/src/install.mjs';
|
||||
import { KINDS } from '../../plugins/src/kinds.mjs';
|
||||
import { initFiles, receiptFile } from '../src/admin.mjs';
|
||||
import { PIN } from '../src/common.mjs';
|
||||
|
||||
@@ -15,7 +18,7 @@ const code = name => error => error.code === name;
|
||||
const SENTINEL = 'pa-sentinel-management-key-0123456789';
|
||||
const stopped = async () => ({ stopped: true, running: [] });
|
||||
|
||||
function fixture(t, { unitState = { installed: false, active: 'inactive', healthy: false }, restart = null, fetch = null, pin = PIN, statusHook = null } = {}) {
|
||||
function fixture(t, { unitState = { installed: false, active: 'inactive', healthy: false }, restart = null, fetch = null, pin = PIN, statusHook = null, extra = {} } = {}) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'pc-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
@@ -30,7 +33,7 @@ function fixture(t, { unitState = { installed: false, active: 'inactive', health
|
||||
stop: async () => { ops.push('stop'); return { ...unitState, active: 'inactive', pid: null }; },
|
||||
restart: async () => { ops.push('restart'); return restart ?? { state: unitState, evidence: { unit: 'mosaic-proxy-zz.service', confirmed: true, before: { pid: 10, since: 1, active: 'active' }, after: { pid: 11, since: 2, active: 'active', listening: true }, observedAt: new Date(0).toISOString() } }; },
|
||||
};
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit, buses: stopped, fetch: fetch ?? (async () => ({})), now: () => new Date(0), orgs: async () => [{ id: 'acme', catalogue: 'ready' }], pin });
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit, buses: stopped, fetch: fetch ?? (async () => ({})), now: () => new Date(0), orgs: async () => [{ id: 'acme', catalogue: 'ready' }], pin, ...extra });
|
||||
return { root, configDir, dataRoot, admin, ops, files: initFiles(configDir, dataRoot) };
|
||||
}
|
||||
const receipts = dataRoot => { try { return fs.readFileSync(receiptFile(dataRoot), 'utf8').trim().split('\n').filter(Boolean).map(JSON.parse); } catch { return []; } };
|
||||
@@ -227,3 +230,340 @@ test('status carries the last receipts from proxy-admin.jsonl, so the page shows
|
||||
assert.equal(receipts.length, 10);
|
||||
assert.ok(receipts.every(r => r.op === 'unit.stop'));
|
||||
});
|
||||
|
||||
// #1873 settings-backend A1: the controller's three reads and the account-change record.
|
||||
function a1Fixture(t, { unitState = { installed: true, active: 'active', healthy: true }, log = undefined, statusError = false, env = {} } = {}) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'pc-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
fs.mkdirSync(configDir, { mode: 0o700 }); fs.mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const calls = [];
|
||||
const unit = {
|
||||
unit: 'mosaic-proxy-zz.service',
|
||||
status: async () => { if (statusError) throw Object.assign(new Error('systemctl gone'), { code: 'proxy-unit-failed' }); return { unit: 'mosaic-proxy-zz.service', ...unitState }; },
|
||||
...(log === undefined ? { log: async (lines, options) => { calls.push({ lines, options }); return { unit: 'mosaic-proxy-zz.service', lines: ['ok'], redacted: 0, dropped: 0 }; } } : log === null ? {} : { log }),
|
||||
};
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit, buses: stopped, now: () => new Date(0), env });
|
||||
return { admin, calls, configDir, dataRoot };
|
||||
}
|
||||
|
||||
test('log reads the unit journal with the env secrets; a missing unit or log reader is not-configured, an unreadable state outcome-unknown', async t => {
|
||||
const { admin, calls } = a1Fixture(t, { env: { MOSAIC_PROXY_KEY: 'plainwordsonly', PATH: '/usr/bin' } });
|
||||
assert.deepEqual(await admin.log(), { unit: 'mosaic-proxy-zz.service', lines: ['ok'], redacted: 0, dropped: 0 });
|
||||
assert.deepEqual(calls, [{ lines: 50, options: { secrets: ['plainwordsonly'] } }]);
|
||||
await admin.log({ lines: 7 });
|
||||
assert.equal(calls[1].lines, 7);
|
||||
await assert.rejects(a1Fixture(t, { unitState: { installed: false, active: 'inactive', healthy: false } }).admin.log(), code('not-configured'));
|
||||
await assert.rejects(a1Fixture(t, { log: null }).admin.log(), code('not-configured'));
|
||||
await assert.rejects(a1Fixture(t, { statusError: true }).admin.log(), code('outcome-unknown'));
|
||||
const failing = a1Fixture(t, { log: async () => { throw Object.assign(new Error('journal'), { code: 'outcome-unknown' }); } });
|
||||
await assert.rejects(failing.admin.log(), code('outcome-unknown'));
|
||||
});
|
||||
|
||||
test('serviceKey and history read the configured directories', async t => {
|
||||
const { admin, configDir, dataRoot } = a1Fixture(t);
|
||||
assert.equal(admin.serviceKey().problem, 'missing');
|
||||
fs.writeFileSync(join(configDir, 'proxy.json'), '{}', { mode: 0o600 });
|
||||
assert.equal(admin.serviceKey().problem, null);
|
||||
assert.match(admin.serviceKey().madeAt, /^\d{4}-\d\d-\d\dT/);
|
||||
assert.deepEqual(admin.history(), { entries: [], next: null });
|
||||
admin.recordAccount({ verb: 'proxy.login.status', result: { status: 'ok' } });
|
||||
assert.deepEqual(admin.history({ limit: 5 }), { entries: [{ time: new Date(0).toISOString(), op: 'account.proxy.login.complete' }], next: null });
|
||||
assert.equal(receipts(dataRoot).length, 1);
|
||||
});
|
||||
|
||||
test('history withholds an env secret a receipt holds', async t => {
|
||||
const { admin, dataRoot } = a1Fixture(t, { env: { PROXY_TOKEN: 'plainwordsonly' } });
|
||||
fs.mkdirSync(join(dataRoot, 'state'), { recursive: true, mode: 0o700 });
|
||||
fs.writeFileSync(receiptFile(dataRoot), JSON.stringify({ op: 'x', note: 'plain words only' }) + '\n', { mode: 0o600 });
|
||||
assert.deepEqual(admin.history().entries, [{ op: 'x', note: '[redacted]' }]);
|
||||
});
|
||||
|
||||
test('recordAccount records account changes by label only, never a key, login state, callback or model name', async t => {
|
||||
const { admin, dataRoot } = a1Fixture(t);
|
||||
const KEY = 'sk-proj-Ab3dE5fG7hJ9kL1mN3pQ5rS7tU9vW1xY';
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.apikey.add', args: { provider: 'openai', key: KEY }, result: { added: true }, user: 'jason', org: 'acme' }), true);
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.apikey.add', args: { provider: KEY, key: KEY } }), true);
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.login.status', args: { state: 'state-secret-123' }, result: { status: 'ok' } }), true);
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.login.status', args: { state: 'state-secret-123' }, result: { status: 'wait' } }), null);
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.priority.set', args: { accountId: '0123456789ab', priority: 2 } }), true);
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.group.models.set', args: { accountId: 'not-hex', models: ['secret-model-name', 'b'] }, org: '../x' }), true);
|
||||
for (const verb of ['proxy.login.start', 'proxy.login.submit', 'proxy.login.cancel', 'plugins.set', 'proxy.accounts', undefined]) {
|
||||
assert.equal(admin.recordAccount({ verb, args: { redirect_url: 'https://cb.example/?code=abc', provider: 'x' } }), null, String(verb));
|
||||
}
|
||||
const all = receipts(dataRoot);
|
||||
const at = new Date(0).toISOString();
|
||||
assert.deepEqual(all, [
|
||||
{ time: at, op: 'account.proxy.apikey.add', provider: 'openai', org: 'acme', user: 'jason' },
|
||||
{ time: at, op: 'account.proxy.apikey.add', provider: null },
|
||||
{ time: at, op: 'account.proxy.login.complete' },
|
||||
{ time: at, op: 'account.proxy.priority.set', accountId: '0123456789ab', priority: 2 },
|
||||
{ time: at, op: 'account.proxy.group.models.set', accountId: null, models: 2 },
|
||||
]);
|
||||
const text = fs.readFileSync(receiptFile(dataRoot), 'utf8');
|
||||
for (const bit of ['Ab3dE5', 'state-secret', 'secret-model', 'cb.example', 'code=abc']) assert.equal(text.includes(bit), false, bit);
|
||||
});
|
||||
|
||||
test('recordAccount answers false, and throws nothing, when the history cannot be written', async t => {
|
||||
const { admin, dataRoot } = a1Fixture(t);
|
||||
fs.mkdirSync(join(dataRoot, 'state'), { mode: 0o700 });
|
||||
fs.mkdirSync(receiptFile(dataRoot));
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.apikey.add', args: { provider: 'openai', key: 'k' } }), false);
|
||||
});
|
||||
|
||||
// #1873 settings-backend A2 (items 5 and 8): fake organizations behind the riders. The harness names
|
||||
// are made up; every answer carries a planted key, which no result, receipt or history entry may hold.
|
||||
const REV = 'a'.repeat(64), REV2 = 'b'.repeat(64);
|
||||
const PLANT = `mso-zz-acme-key-${'Q'.repeat(43)}`;
|
||||
const KEY_TEXT = /ms[kotp][-_][A-Za-z0-9_-]{6,}/;
|
||||
const busError = name => Object.assign(new Error(`refused ${PLANT}`), { code: name, reason: PLANT });
|
||||
function fakeOrg(id, { on = ['alpha', 'beta'], revision = REV, fileRevision = null, instances = {}, routesError = null, routesRevision = null, document = null, fileError = false, down = false, setError = null, keys = [], keysError = null, fixture = false, cliproxy = true, plugins = undefined } = {}) {
|
||||
const calls = [];
|
||||
const rider = {
|
||||
id, fixture, cliproxy, changes: '',
|
||||
file: () => { if (fileError) throw new Error(PLANT); return { document: document ?? { roles: {}, plugins: { harness: on }, secretNote: PLANT }, revision: fileRevision ?? revision }; },
|
||||
call: async (verb, args) => {
|
||||
calls.push(args === undefined ? [verb] : [verb, args]);
|
||||
if (down) throw busError('outcome-unknown');
|
||||
if (verb === 'plugins') return { business: id, revision, plugins: plugins !== undefined ? plugins : { harness: on, 'model-provider': ['cliproxy'] }, available: { harness: on }, note: PLANT };
|
||||
if (verb === 'routes') { if (routesError) throw busError(routesError); return { business: id, revision: routesRevision ?? revision, harnesses: on, models: ['m1'], providerModels: {}, levels: {}, instances, note: PLANT }; }
|
||||
if (verb === 'plugins.set') { if (setError) throw busError(setError); return { business: id, revision: REV2, plugins: args.plugins }; }
|
||||
if (verb === 'proxy.keys') { if (keysError) throw busError(keysError); return { keys }; }
|
||||
throw busError('invalid-request');
|
||||
},
|
||||
};
|
||||
return { rider, calls };
|
||||
}
|
||||
const NO_ROOT = { state: 'absent', agents: {}, assistant: null };
|
||||
const programsFixture = (t, orgs, extra = {}) => fixture(t, { extra: { riders: async () => orgs.map(o => o.rider), harnesses: () => ['alpha', 'beta', 'gamma'], rootAgents: () => NO_ROOT, ...extra } });
|
||||
const rootAgent = (vars, { definition = 'coder', excludeOrgs = [] } = {}) => ({ definition, title: 'Coder', vars, excludeOrgs });
|
||||
const route = harness => ({ harness, model: 'm1', thinking: null, fallback: null });
|
||||
|
||||
test('A2 item 5: the installed list comes from the harness plugin tree, and status lists it with no organization', async t => {
|
||||
const tree = new URL('../../plugins/harness/', import.meta.url);
|
||||
const fromTree = fs.readdirSync(tree).filter(name => { try { return fs.lstatSync(new URL(`${name}/index.mjs`, tree)).isFile(); } catch { return false; } }).sort();
|
||||
assert.ok(fromTree.length >= 1);
|
||||
assert.deepEqual(installedHarnesses(), fromTree);
|
||||
const { admin } = fixture(t);
|
||||
assert.deepEqual((await admin.status()).programs, { installed: fromTree, organizations: [] });
|
||||
});
|
||||
|
||||
test('A2 item 5: status names the programs each organization has on; one bus down is null and leaves the others', async t => {
|
||||
const acme = fakeOrg('acme', { on: ['alpha'] }), bolt = fakeOrg('bolt', { down: true }), crux = fakeOrg('crux', { plugins: { harness: 'alpha' } });
|
||||
const { admin } = programsFixture(t, [acme, bolt, crux]);
|
||||
const status = await admin.status();
|
||||
assert.deepEqual(status.programs, { installed: ['alpha', 'beta', 'gamma'], organizations: [{ id: 'acme', on: ['alpha'] }, { id: 'bolt', on: null }, { id: 'crux', on: null }] });
|
||||
assert.doesNotMatch(JSON.stringify(status), KEY_TEXT);
|
||||
});
|
||||
|
||||
test('A2 item 5: off skips a program any route still uses, whatever its model, and writes nothing there', async t => {
|
||||
// A role route, a fallback route, a project agent only the file shows, and a route whose model has
|
||||
// no known account kind: each keeps alpha on (the harness rule).
|
||||
const role = fakeOrg('role', { instances: { chief: route('alpha') } });
|
||||
const fallback = fakeOrg('fallback', { instances: { chief: { ...route('beta'), fallback: { harness: 'alpha', model: 'm1', thinking: null } } } });
|
||||
const project = fakeOrg('project', { routesError: 'launch-not-configured', document: { roles: {}, plugins: { harness: ['alpha', 'beta'] }, projects: { site: { agents: { dev: { definition: 'coder', vars: { harness: 'alpha' } } } } } } });
|
||||
const unknownKind = fakeOrg('unknown-kind', { instances: { chief: { harness: 'alpha', model: 'mystery-account-model', thinking: null, fallback: null } } });
|
||||
const { admin } = programsFixture(t, [role, fallback, project, unknownKind]);
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results.map(r => [r.id, r.outcome]), [['role', 'in-use'], ['fallback', 'in-use'], ['project', 'in-use'], ['unknown-kind', 'in-use']]);
|
||||
for (const o of [role, fallback, project, unknownKind]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
test('A2 item 5: off turns the program off where nothing uses it, skips the last program, already off and unreadable, and reports bus down', async t => {
|
||||
const free = fakeOrg('free', { instances: { chief: route('beta') } });
|
||||
const last = fakeOrg('last', { on: ['alpha'] });
|
||||
const already = fakeOrg('already', { on: ['beta'] });
|
||||
const down = fakeOrg('down', { down: true });
|
||||
const unreadable = fakeOrg('unreadable', { fileError: true });
|
||||
const noFile = fakeOrg('no-file'); delete noFile.rider.file;
|
||||
const badRoutes = fakeOrg('bad-routes', { routesError: 'organization-invalid' });
|
||||
const { admin } = programsFixture(t, [free, last, already, down, unreadable, noFile, badRoutes]);
|
||||
const out = await admin.programsOff({ name: 'alpha', user: 'owner' });
|
||||
assert.equal(out.name, 'alpha');
|
||||
assert.deepEqual(out.results, [
|
||||
{ id: 'free', outcome: 'off' }, { id: 'last', outcome: 'last-program' }, { id: 'already', outcome: 'already-off' },
|
||||
{ id: 'down', outcome: 'bus-down' }, { id: 'unreadable', outcome: 'unreadable' }, { id: 'no-file', outcome: 'unreadable' }, { id: 'bad-routes', outcome: 'unreadable' },
|
||||
]);
|
||||
assert.deepEqual(free.calls.find(([verb]) => verb === 'plugins.set'), ['plugins.set', { revision: REV, plugins: { harness: ['beta'] } }]);
|
||||
for (const o of [last, already, down, unreadable, noFile, badRoutes]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
test('A2 item 5: a plugins.set refusal is reported per organization, and a revision change is reported once, never retried', async t => {
|
||||
const refused = fakeOrg('refused', { setError: 'unknown-plugin' });
|
||||
const raced = fakeOrg('raced', { setError: 'revision-conflict' });
|
||||
const moved = fakeOrg('moved', { fileRevision: REV2 });
|
||||
const ok = fakeOrg('ok');
|
||||
const { admin } = programsFixture(t, [refused, raced, moved, ok]);
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results.map(r => r.outcome), ['failed:unknown-plugin', 'failed:revision-conflict', 'failed:revision-conflict', 'off']);
|
||||
assert.equal(raced.calls.filter(([verb]) => verb === 'plugins.set').length, 1);
|
||||
assert.equal(moved.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
test('A2 item 5: off writes one receipt by name and outcome ids, and no answer, receipt or history entry holds a key', async t => {
|
||||
const a = fakeOrg('acme'), b = fakeOrg('bolt', { setError: 'invalid-plugins' });
|
||||
const { admin, dataRoot } = programsFixture(t, [a, b]);
|
||||
const out = await admin.programsOff({ name: 'beta', user: 'owner' });
|
||||
const record = { time: new Date(0).toISOString(), op: 'programs.off', name: 'beta', results: [{ id: 'acme', outcome: 'off' }, { id: 'bolt', outcome: 'failed:invalid-plugins' }], user: 'owner' };
|
||||
assert.deepEqual(out.receipt, record);
|
||||
assert.deepEqual(receipts(dataRoot), [record]);
|
||||
const history = admin.history({});
|
||||
assert.deepEqual(history.entries, [record]);
|
||||
for (const value of [out, receipts(dataRoot), history]) assert.doesNotMatch(JSON.stringify(value), KEY_TEXT);
|
||||
});
|
||||
|
||||
test('A2 item 5: an unknown name is invalid-request and calls no bus; two off calls at once give proxy-admin-busy for one', async t => {
|
||||
let release;
|
||||
const gate = new Promise(r => { release = r; });
|
||||
const slow = fakeOrg('slow');
|
||||
const original = slow.rider.call;
|
||||
// Blocks only from the busy part on, so a name check that lets a bad name through goes red, not hangs.
|
||||
let blocking = false;
|
||||
slow.rider.call = async (verb, args) => { if (verb === 'plugins' && blocking) await gate; return original(verb, args); };
|
||||
const { admin, dataRoot } = programsFixture(t, [slow]);
|
||||
await assert.rejects(admin.programsOff({ name: 'delta' }), code('invalid-request'));
|
||||
await assert.rejects(admin.programsOff({ name: 42 }), code('invalid-request'));
|
||||
assert.deepEqual(slow.calls, []);
|
||||
assert.deepEqual(receipts(dataRoot), []);
|
||||
blocking = true;
|
||||
const first = admin.programsOff({ name: 'alpha' });
|
||||
await new Promise(r => setImmediate(r));
|
||||
// Bounded: without the lock the second call would wait on the same gate, so it answers 'waited'.
|
||||
const second = admin.programsOff({ name: 'beta' }).then(() => 'answered', e => e?.code);
|
||||
assert.equal(await Promise.race([second, new Promise(r => setTimeout(() => r('waited'), 1000))]), 'proxy-admin-busy');
|
||||
release();
|
||||
await second;
|
||||
assert.deepEqual((await first).results, [{ id: 'slow', outcome: 'off' }]);
|
||||
assert.equal(receipts(dataRoot).length, 1);
|
||||
});
|
||||
|
||||
test('A2 item 5: an installed list that cannot be read refuses off as invalid-request', async t => {
|
||||
const a = fakeOrg('acme');
|
||||
const { admin } = programsFixture(t, [a], { harnesses: () => { throw new Error('unreadable tree'); } });
|
||||
await assert.rejects(admin.programsOff({ name: 'alpha' }), code('invalid-request'));
|
||||
assert.deepEqual(a.calls, []);
|
||||
assert.equal((await admin.status()).programs.installed, null);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P1-1): a root agent the organization takes keeps its program on; an excluded organization and the root assistant do not count', async t => {
|
||||
// Only the root agent file names alpha: nothing in either organization's file or routes does.
|
||||
const root = { state: 'ok', assistant: 'helper', agents: {
|
||||
writer: rootAgent({ harness: 'gamma', 'fallback.harness': 'alpha' }, { excludeOrgs: ['spare'] }),
|
||||
helper: rootAgent({ harness: 'beta' }, { definition: 'assistant' }),
|
||||
} };
|
||||
const takes = fakeOrg('takes', { on: ['alpha', 'beta', 'gamma'] });
|
||||
const spare = fakeOrg('spare', { on: ['alpha', 'beta', 'gamma'] });
|
||||
const { admin } = programsFixture(t, [takes, spare], { rootAgents: () => root });
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results, [{ id: 'takes', outcome: 'in-use' }, { id: 'spare', outcome: 'off' }]);
|
||||
assert.equal(takes.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
// The root assistant is not copied into an organization, so its harness does not keep beta on.
|
||||
const again = await admin.programsOff({ name: 'beta' });
|
||||
assert.deepEqual(again.results.map(r => r.outcome), ['off', 'off']);
|
||||
// The root file is read once per call, not once per organization.
|
||||
let reads = 0;
|
||||
const counted = programsFixture(t, [fakeOrg('a'), fakeOrg('b')], { rootAgents: () => { reads++; return NO_ROOT; } });
|
||||
await counted.admin.programsOff({ name: 'alpha' });
|
||||
assert.equal(reads, 1);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P1-1): a root agent file that cannot be read, or no reader, writes nothing where the program is on', async t => {
|
||||
const on = fakeOrg('on'), already = fakeOrg('already', { on: ['beta'] });
|
||||
const { admin } = programsFixture(t, [on, already], { rootAgents: () => { throw Object.assign(new Error(`root agent file refused ${PLANT}`), { name: 'BusinessError' }); } });
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results, [{ id: 'on', outcome: 'unreadable' }, { id: 'already', outcome: 'already-off' }]);
|
||||
assert.doesNotMatch(JSON.stringify(out), KEY_TEXT);
|
||||
const bare = fakeOrg('bare');
|
||||
const none = fixture(t, { extra: { riders: async () => [bare.rider], harnesses: () => ['alpha', 'beta'] } });
|
||||
assert.deepEqual((await none.admin.programsOff({ name: 'alpha' })).results, [{ id: 'bare', outcome: 'unreadable' }]);
|
||||
for (const o of [on, already, bare]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
// A root answer of the wrong shape is unreadable too.
|
||||
assert.equal(rootHarnessesNamed({ agents: { w: { vars: { harness: 'alpha' } } } }, 'acme'), null);
|
||||
assert.equal(rootHarnessesNamed(null, 'acme'), null);
|
||||
assert.deepEqual([...rootHarnessesNamed(NO_ROOT, 'acme')], []);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P3-1): a revision that is not 64 hex, routes from another revision, and routes with no instances write nothing; later organizations still go', async t => {
|
||||
const badRev = fakeOrg('bad-rev', { revision: 'not-a-revision' });
|
||||
const routesMoved = fakeOrg('routes-moved', { routesRevision: REV2 });
|
||||
const noInstances = fakeOrg('no-instances', { instances: null });
|
||||
const after = fakeOrg('after');
|
||||
const { admin, dataRoot } = programsFixture(t, [badRev, routesMoved, noInstances, after]);
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results, [{ id: 'bad-rev', outcome: 'unreadable' }, { id: 'routes-moved', outcome: 'failed:revision-conflict' }, { id: 'no-instances', outcome: 'unreadable' }, { id: 'after', outcome: 'off' }]);
|
||||
for (const o of [badRev, routesMoved, noInstances]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
assert.equal(receipts(dataRoot).length, 1);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P3-1): a harness program the instance plugin lock pins is listed as installed', t => {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'pl-'));
|
||||
// The install leaves read-only directories; open them up before removing the tree.
|
||||
const thaw = p => { const st = fs.lstatSync(p); if (st.isSymbolicLink()) return; if (st.isDirectory()) { fs.chmodSync(p, 0o700); for (const n of fs.readdirSync(p)) thaw(join(p, n)); } };
|
||||
t.after(() => { thaw(root); fs.rmSync(root, { recursive: true, force: true }); });
|
||||
const src = join(root, 'src'), dataRoot = join(root, 'data');
|
||||
fs.mkdirSync(join(src, 'harness'), { recursive: true }); fs.mkdirSync(dataRoot);
|
||||
fs.writeFileSync(join(src, 'harness', 'index.mjs'), KINDS.harness.exports.map(name => `export function ${name}(){return ${name === 'levels' ? "['high']" : name === 'countsAccount' ? 'true' : 'null'};}`).join('\n') + '\nexport const interfaceVersion=1;');
|
||||
const manifest = { name: 'zetaplug', version: '1.0.0', kinds: ['harness'], interfaceVersions: { harness: 1 }, sha256: '0'.repeat(64), source: { path: 'fixture', version: '1.0.0' }, signature: null, tools: [], actions: [], services: [], roleTemplates: [] };
|
||||
fs.writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
manifest.sha256 = pluginHash(src);
|
||||
fs.writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
assert.equal(installedHarnesses(dataRoot).includes('zetaplug'), false);
|
||||
installPlugin({ dataRoot, source: src, expectedHash: manifest.sha256 });
|
||||
const listed = installedHarnesses(dataRoot);
|
||||
assert.ok(listed.includes('zetaplug'));
|
||||
assert.deepEqual(listed.filter(n => n !== 'zetaplug'), installedHarnesses());
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P3-3): an organization whose harness list holds anything but plugin names reads on: null', async t => {
|
||||
const odd = fakeOrg('odd', { plugins: { harness: ['alpha', `bad ${PLANT}`] } });
|
||||
const fine = fakeOrg('fine', { on: ['alpha'] });
|
||||
const { admin } = programsFixture(t, [odd, fine]);
|
||||
const status = await admin.status();
|
||||
assert.deepEqual(status.programs.organizations, [{ id: 'odd', on: null }, { id: 'fine', on: ['alpha'] }]);
|
||||
assert.doesNotMatch(JSON.stringify(status), KEY_TEXT);
|
||||
});
|
||||
|
||||
test('A2: harnessesNamed reads every harness and fallback.harness string outside the plugins list', () => {
|
||||
assert.deepEqual([...harnessesNamed({ plugins: { harness: ['alpha'] }, roles: { a: { vars: { harness: 'beta', 'fallback.harness': 'gamma' } } }, projects: { p: { agents: { x: { vars: { harness: 'delta' } } } } } })].sort(), ['beta', 'delta', 'gamma']);
|
||||
assert.equal(harnessesNamed(null), null);
|
||||
assert.equal(harnessesNamed([]), null);
|
||||
});
|
||||
|
||||
const key = (keyId, management, state, agent = 'dev') => ({ keyId, management, agent, issuedAt: '2026-11-01T10:00:00.000Z', state });
|
||||
test('A2 item 8: access gives each organization a health and key ids, never a key, and never reads the usage queue', async t => {
|
||||
const ok = fakeOrg('ok', { keys: [key('0123456789ab', 'stack-managed', 'active'), key('ba9876543210', 'not stack-managed', null, null), key('aaaaaaaaaaaa', 'sentinel', null, null)] });
|
||||
const attention = fakeOrg('attention', { keys: [key('0123456789ab', 'stack-managed', 'active'), key('bbbbbbbbbbbb', 'stack-managed', 'revoked')] });
|
||||
const missing = fakeOrg('missing', { keys: [key('cccccccccccc', 'not stack-managed', null, null)] });
|
||||
const fixtureOrg = fakeOrg('fixture', { fixture: true });
|
||||
const ownModels = fakeOrg('own', { cliproxy: false });
|
||||
const down = fakeOrg('down', { down: true });
|
||||
const noClient = fakeOrg('no-client', { keysError: 'not-configured' });
|
||||
const uncertain = fakeOrg('uncertain', { keysError: 'proxy-uncertain' });
|
||||
const proxyDown = fakeOrg('proxy-down', { keysError: 'proxy-down' });
|
||||
const badShape = fakeOrg('bad-shape', { keys: 'none' });
|
||||
const planted = fakeOrg('planted', { keys: [key('dddddddddddd', 'stack-managed', PLANT, PLANT), { ...key('eeeeeeeeeeee', 'stack-managed', 'active'), value: PLANT, issuedAt: PLANT }] });
|
||||
const plantedId = fakeOrg('planted-id', { keys: [key(PLANT, 'stack-managed', 'active')] });
|
||||
const unread = fakeOrg('unread'); unread.rider.fixture = undefined;
|
||||
const all = [ok, attention, missing, fixtureOrg, ownModels, down, noClient, uncertain, proxyDown, badShape, planted, plantedId, unread];
|
||||
const { admin } = programsFixture(t, all);
|
||||
const out = await admin.access();
|
||||
assert.deepEqual(out.organizations, [
|
||||
{ id: 'ok', health: 'ok', keys: [{ keyId: '0123456789ab', agent: 'dev', state: 'active', issuedAt: '2026-11-01T10:00:00.000Z' }], other: 1, sentinel: 1 },
|
||||
{ id: 'attention', health: 'attention', keys: [{ keyId: '0123456789ab', agent: 'dev', state: 'active', issuedAt: '2026-11-01T10:00:00.000Z' }, { keyId: 'bbbbbbbbbbbb', agent: 'dev', state: 'revoked', issuedAt: '2026-11-01T10:00:00.000Z' }], other: 0, sentinel: 0 },
|
||||
{ id: 'missing', health: 'missing', keys: [], other: 1, sentinel: 0 },
|
||||
{ id: 'fixture', health: 'not-riding', keys: [] },
|
||||
{ id: 'own', health: 'not-riding', keys: [] },
|
||||
{ id: 'down', health: 'unreadable', reason: 'bus-down', keys: [] },
|
||||
{ id: 'no-client', health: 'unreadable', reason: 'no-proxy-client', keys: [] },
|
||||
{ id: 'uncertain', health: 'attention', reason: 'proxy-uncertain', keys: [] },
|
||||
{ id: 'proxy-down', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
{ id: 'bad-shape', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
{ id: 'planted', health: 'attention', keys: [{ keyId: 'dddddddddddd', agent: null, state: null, issuedAt: '2026-11-01T10:00:00.000Z' }, { keyId: 'eeeeeeeeeeee', agent: 'dev', state: 'active', issuedAt: null }], other: 0, sentinel: 0 },
|
||||
{ id: 'planted-id', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
{ id: 'unread', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
]);
|
||||
assert.doesNotMatch(JSON.stringify(out), KEY_TEXT);
|
||||
assert.notEqual(out.organizations.find(o => o.id === 'uncertain').health, 'ok');
|
||||
// proxy.keys is the only verb access sends; the usage queue is never read.
|
||||
for (const o of all) for (const [verb] of o.calls) assert.equal(verb, 'proxy.keys');
|
||||
});
|
||||
@@ -6,12 +6,15 @@ import fs from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { PIN } from '../src/common.mjs';
|
||||
import { installPaths } from '../src/settings.mjs';
|
||||
import {
|
||||
processTree, pidListensOn, processStartMs, verifyRestart, initLockStatus, clearInitLock, initFiles, initState,
|
||||
fenceState, createUnitControl, releaseState, receiptFile, unitName,
|
||||
unitLog, serviceKeyState, receiptHistory, appendReceipt, LOG_LINES, HISTORY_LIMIT,
|
||||
} from '../src/admin.mjs';
|
||||
import { secretValues } from '../src/redact.mjs';
|
||||
|
||||
const code = name => error => error.code === name;
|
||||
const scratch = t => { const dir = fs.mkdtempSync(join(tmpdir(), 'pa-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true })); return dir; };
|
||||
@@ -193,3 +196,198 @@ test('unit actions map proxy-service refusals by code and refuse a missing unit'
|
||||
assert.equal(started.healthy, true); assert.deepEqual(verbs, ['start']);
|
||||
assert.throws(() => createUnitControl({ unitDir: 'relative' }), code('proxy-unit-path'));
|
||||
});
|
||||
|
||||
// #1873 settings-backend A1: the reads behind the setup page's log, service key and history.
|
||||
// Planted keys, one of each Mosaic prefix and short enough that only the prefix rule finds them,
|
||||
// plus long ones of the real shapes.
|
||||
const TAIL = 'Ab3_dE5-fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7dE';
|
||||
const PLANTED = ['msk-tag-qwertyu', 'mso-tag-acme-qwerty', 'mst-tag-triage-zxcvb', 'msp-tag-plmokn',
|
||||
`msk-k1a2-${TAIL}`, `mso-k1a2-acme-${TAIL}`, `mst-k1a2-triage-${TAIL}`, `msp-k1a2-${TAIL}`];
|
||||
const ENV_SECRET = 'plainwordsonly';
|
||||
const leaks = text => [...PLANTED, ...PLANTED.map(k => k.slice(4)), TAIL.slice(0, 10), ENV_SECRET].filter(bit => text.includes(bit));
|
||||
|
||||
function journal(stdout, { code: exit = 0, calls = [] } = {}) {
|
||||
return async (command, args, options) => { calls.push({ command, args, options }); if (stdout instanceof Error) throw stdout; return { code: exit, stdout, stderr: 'raw stderr msk-tag-qwertyu' }; };
|
||||
}
|
||||
|
||||
test('unitLog asks journalctl for the named unit only, clamps the line count, and shows no planted key', async () => {
|
||||
const calls = [];
|
||||
const lines = ['proxy listening on 127.0.0.1:18432', ...PLANTED.map(k => `upstream refused ${k} for model x`), `auth with ${ENV_SECRET} ok`, 'msk-tag-qwertyu', '', 'last line'];
|
||||
const result = await unitLog({ run: journal(lines.join('\n') + '\n', { calls }), unit: 'mosaic-proxy-zz.service', lines: 10000, env: { X: '1' }, secrets: secretValues({ PROXY_KEY: ENV_SECRET }) });
|
||||
assert.deepEqual(calls[0].args, ['--user', '-u', 'mosaic-proxy-zz.service', '-n', String(LOG_LINES.max), '-o', 'cat', '--no-pager', '-q']);
|
||||
assert.equal(calls[0].command, 'journalctl');
|
||||
assert.deepEqual(calls[0].options.env, { X: '1' });
|
||||
assert.equal(result.unit, 'mosaic-proxy-zz.service');
|
||||
assert.equal(result.lines[0], 'proxy listening on 127.0.0.1:18432', 'a line with nothing secret shows as it is');
|
||||
assert.equal(result.lines.at(-1), 'last line');
|
||||
assert.equal(result.lines.filter(l => l === 'upstream refused [redacted] for model x').length, PLANTED.length);
|
||||
assert.equal(result.redacted, PLANTED.length, 'shown lines with a redaction; dropped lines count in dropped');
|
||||
assert.equal(result.dropped, 2, 'the env-secret line and the key-only line are dropped, the blank one is not counted');
|
||||
assert.deepEqual(leaks(JSON.stringify(result)), []);
|
||||
// At least one line, and a line count that is not an integer is refused.
|
||||
await unitLog({ run: journal('x\n', { calls }), unit: 'mosaic-proxy.service', lines: 0 });
|
||||
assert.equal(calls.at(-1).args[4], '1');
|
||||
await assert.rejects(unitLog({ run: journal('x\n'), unit: 'mosaic-proxy.service', lines: 2.5 }), code('invalid-request'));
|
||||
await assert.rejects(unitLog({ run: journal('x\n'), unit: 'mosaic-proxy.service', lines: '5' }), code('invalid-request'));
|
||||
});
|
||||
|
||||
test('unitLog keeps only the last lines asked for and never takes a unit name it was not given by unitName', async () => {
|
||||
const many = Array.from({ length: 30 }, (_, i) => `line ${i}`).join('\n');
|
||||
assert.deepEqual((await unitLog({ run: journal(many), unit: 'mosaic-proxy.service', lines: 3 })).lines, ['line 27', 'line 28', 'line 29']);
|
||||
for (const unit of ['sshd.service', 'mosaic-proxy.service; rm', '../mosaic-proxy.service', 'mosaic-proxy-A.service', undefined]) {
|
||||
await assert.rejects(unitLog({ run: journal('x'), unit, lines: 5 }), code('proxy-unit-usage'), String(unit));
|
||||
}
|
||||
});
|
||||
|
||||
test('a journal that cannot be read refuses outcome-unknown with no lines and no other source', async () => {
|
||||
for (const run of [journal(Object.assign(new Error('spawn journalctl ENOENT'), { code: 'ENOENT' })), journal('partial msk-tag-qwertyu', { code: 1 }), async () => ({ code: 0, stdout: null }), async () => null]) {
|
||||
await assert.rejects(unitLog({ run, unit: 'mosaic-proxy.service', lines: 5 }), error => error.code === 'outcome-unknown' && !leaks(String(error.message) + JSON.stringify(error)).length);
|
||||
}
|
||||
});
|
||||
|
||||
test('a secret split across journal lines drops every line', async () => {
|
||||
const result = await unitLog({ run: journal('ok one\nplain\nwordsonly\nok two\n'), unit: 'mosaic-proxy.service', lines: 10, secrets: [ENV_SECRET] });
|
||||
assert.deepEqual(result.lines, []);
|
||||
assert.equal(result.dropped, 4);
|
||||
});
|
||||
|
||||
test('createUnitControl.log runs unitLog for its own unit', async () => {
|
||||
const calls = [];
|
||||
const control = createUnitControl({ instance: 'zz', port: () => 1, run: journal('hello msk-tag-qwertyu\n', { calls }), probe: async () => true, waitMs: 0, pollMs: 0 });
|
||||
assert.deepEqual(await control.log(5), { unit: 'mosaic-proxy-zz.service', lines: ['hello [redacted]'], redacted: 1, dropped: 0 });
|
||||
assert.equal(calls[0].args[2], 'mosaic-proxy-zz.service');
|
||||
});
|
||||
|
||||
test('serviceKeyState answers proxy.json\'s mtime and never opens or stats the key file', t => {
|
||||
const dir = scratch(t);
|
||||
assert.deepEqual(serviceKeyState(dir), { madeAt: null, renewedAt: null, renewal: 'not-built', problem: 'missing' });
|
||||
const files = initFiles(dir, join(dir, 'data'));
|
||||
fs.writeFileSync(files.settings, '{}', { mode: 0o600 });
|
||||
fs.mkdirSync(join(files.key, '..'), { recursive: true, mode: 0o700 });
|
||||
fs.writeFileSync(files.key, 'msk-k1a2-' + TAIL, { mode: 0o600 });
|
||||
fs.utimesSync(join(dir, 'proxy.json'), new Date(Date.UTC(2026, 10, 1, 8)), new Date(Date.UTC(2026, 10, 1, 8)));
|
||||
const spies = ['openSync', 'readFileSync', 'statSync', 'lstatSync', 'accessSync', 'existsSync', 'realpathSync', 'readdirSync'].map(name => t.mock.method(fs, name));
|
||||
const state = serviceKeyState(dir);
|
||||
const seen = spies.flatMap(spy => spy.mock.calls.map(call => String(call.arguments[0])));
|
||||
spies.forEach(spy => spy.mock.restore());
|
||||
assert.deepEqual(state, { madeAt: '2026-11-01T08:00:00.000Z', renewedAt: null, renewal: 'not-built', problem: null });
|
||||
assert.deepEqual(seen, [join(dir, 'proxy.json')]);
|
||||
assert.deepEqual(leaks(JSON.stringify(state)), []);
|
||||
});
|
||||
|
||||
test('serviceKeyState gives no date for a proxy.json that is a symlink, a directory or another user\'s', t => {
|
||||
const dir = scratch(t), other = scratch(t);
|
||||
fs.writeFileSync(join(other, 'real.json'), '{}');
|
||||
fs.symlinkSync(join(other, 'real.json'), join(dir, 'proxy.json'));
|
||||
assert.deepEqual(serviceKeyState(dir), { madeAt: null, renewedAt: null, renewal: 'not-built', problem: 'unreadable' });
|
||||
fs.rmSync(join(dir, 'proxy.json')); fs.mkdirSync(join(dir, 'proxy.json'));
|
||||
assert.equal(serviceKeyState(dir).problem, 'unreadable');
|
||||
fs.rmSync(join(dir, 'proxy.json'), { recursive: true }); fs.writeFileSync(join(dir, 'proxy.json'), '{}');
|
||||
assert.equal(serviceKeyState(dir, { uid: process.getuid() + 1 }).problem, 'unreadable');
|
||||
assert.equal(serviceKeyState(dir).problem, null);
|
||||
});
|
||||
|
||||
// A receipt file with `count` entries; some lines padded so the file crosses several 64 KB chunks.
|
||||
function history(t, count, { pad = 0 } = {}) {
|
||||
const dataRoot = scratch(t);
|
||||
for (let i = 0; i < count; i++) appendReceipt(dataRoot, { time: new Date(i * 1000).toISOString(), op: 'fetch', n: i, ...(pad ? { note: 'word '.repeat(pad) } : {}) });
|
||||
return dataRoot;
|
||||
}
|
||||
const pages = (dataRoot, limit, extra = {}) => {
|
||||
const seen = []; let before = null, guard = 0;
|
||||
do {
|
||||
const page = receiptHistory(dataRoot, { limit, before, ...extra });
|
||||
seen.push(page.entries.map(e => e.n));
|
||||
before = page.next;
|
||||
} while (before !== null && ++guard < 1000);
|
||||
return seen;
|
||||
};
|
||||
|
||||
test('receiptHistory pages newest first, oldest first within a page, each entry exactly once across chunks', t => {
|
||||
const dataRoot = history(t, 300, { pad: 150 });
|
||||
assert.ok(fs.statSync(receiptFile(dataRoot)).size > 3 * 65536, 'the file spans several chunks');
|
||||
const seen = pages(dataRoot, 7);
|
||||
assert.deepEqual(seen[0], [293, 294, 295, 296, 297, 298, 299]);
|
||||
assert.deepEqual(seen.flat().sort((a, b) => a - b), Array.from({ length: 300 }, (_, i) => i));
|
||||
assert.equal(new Set(seen.flat()).size, 300);
|
||||
assert.deepEqual(seen.reverse().flat(), Array.from({ length: 300 }, (_, i) => i), 'pages read back to front are the whole history in order');
|
||||
const first = receiptHistory(dataRoot);
|
||||
assert.equal(first.entries.length, HISTORY_LIMIT.default);
|
||||
assert.equal(receiptHistory(dataRoot, { limit: 1000 }).entries.length, HISTORY_LIMIT.max, 'limit is clamped');
|
||||
assert.equal(receiptHistory(dataRoot, { limit: 0 }).entries.length, 1);
|
||||
assert.deepEqual(Object.keys(first.entries[0]).sort(), ['n', 'note', 'op', 'time']);
|
||||
});
|
||||
|
||||
test('receiptHistory skips a torn line and a partial last line, and a write after a page does not repeat or lose entries', t => {
|
||||
const dataRoot = history(t, 5);
|
||||
const file = receiptFile(dataRoot);
|
||||
const text = fs.readFileSync(file, 'utf8').split('\n');
|
||||
text.splice(2, 0, '{"op":"torn', '[1,2]', '{"no":"op"}', 'x'.repeat(70000));
|
||||
// The last line is a whole object whose newline is not written yet: it waits for the newline.
|
||||
fs.writeFileSync(file, text.join('\n') + '{"op":"partial","n":99}');
|
||||
const first = receiptHistory(dataRoot, { limit: 2 });
|
||||
assert.deepEqual(first.entries.map(e => e.n), [3, 4]);
|
||||
assert.deepEqual(receiptHistory(dataRoot, { limit: 1 }).entries.map(e => e.n), [4], 'not shown before its newline');
|
||||
fs.appendFileSync(file, '\n'); appendReceipt(dataRoot, { op: 'fetch', n: 100 });
|
||||
const rest = receiptHistory(dataRoot, { limit: 10, before: first.next });
|
||||
assert.deepEqual(rest.entries.map(e => e.n), [0, 1, 2]);
|
||||
assert.equal(rest.next, null);
|
||||
assert.deepEqual(receiptHistory(dataRoot, { limit: 3 }).entries.map(e => e.n), [4, 99, 100], 'the finished line shows on the next newest read');
|
||||
});
|
||||
|
||||
test('receiptHistory refuses a cursor that is not a line start, past the end, negative or not an integer', t => {
|
||||
const dataRoot = history(t, 3);
|
||||
const size = fs.statSync(receiptFile(dataRoot)).size;
|
||||
for (const before of [1, size + 1, -1, 1.5, '10', Number.MAX_SAFE_INTEGER + 2]) {
|
||||
assert.throws(() => receiptHistory(dataRoot, { before }), code('invalid-request'), String(before));
|
||||
}
|
||||
assert.throws(() => receiptHistory(dataRoot, { limit: 'all' }), code('invalid-request'));
|
||||
assert.deepEqual(receiptHistory(dataRoot, { before: 0 }), { entries: [], next: null });
|
||||
assert.equal(receiptHistory(dataRoot, { before: size }).entries.length, 3);
|
||||
});
|
||||
|
||||
test('receiptHistory withholds an entry whose fields hold an environment secret split across them', t => {
|
||||
const dataRoot = scratch(t);
|
||||
appendReceipt(dataRoot, { op: 'fetch', n: 1 });
|
||||
appendReceipt(dataRoot, { op: 'x', a: 'plainword', b: 'sonly' });
|
||||
appendReceipt(dataRoot, { op: 'x', plainw: 'ordsonly' });
|
||||
appendReceipt(dataRoot, { op: 'x', list: ['plainwo', 'rdsonly'] });
|
||||
appendReceipt(dataRoot, { op: 'fetch', n: 2 });
|
||||
const all = receiptHistory(dataRoot, { limit: 10, secrets: secretValues({ PROXY_TOKEN: ENV_SECRET }) });
|
||||
assert.deepEqual(all.entries, [{ op: 'fetch', n: 1 }, { withheld: true }, { withheld: true }, { withheld: true }, { op: 'fetch', n: 2 }]);
|
||||
assert.equal(receiptHistory(dataRoot, { limit: 10 }).entries[1].a, 'plainword', 'with no secrets nothing is withheld');
|
||||
});
|
||||
|
||||
test('receiptHistory: a missing file is an empty history; a symlink, FIFO, directory, group-readable or foreign file refuses', t => {
|
||||
const dataRoot = scratch(t);
|
||||
assert.deepEqual(receiptHistory(dataRoot), { entries: [], next: null });
|
||||
assert.throws(() => receiptHistory(dataRoot, { before: 5 }), code('invalid-request'));
|
||||
appendReceipt(dataRoot, { op: 'fetch' });
|
||||
const file = receiptFile(dataRoot);
|
||||
fs.chmodSync(file, 0o640);
|
||||
assert.throws(() => receiptHistory(dataRoot), code('outcome-unknown'));
|
||||
fs.chmodSync(file, 0o600);
|
||||
assert.throws(() => receiptHistory(dataRoot, { uid: process.getuid() + 1 }), code('outcome-unknown'));
|
||||
assert.equal(receiptHistory(dataRoot).entries.length, 1);
|
||||
const elsewhere = join(scratch(t), 'x.jsonl'); fs.writeFileSync(elsewhere, '{"op":"planted"}\n', { mode: 0o600 });
|
||||
fs.rmSync(file); fs.symlinkSync(elsewhere, file);
|
||||
assert.throws(() => receiptHistory(dataRoot), code('outcome-unknown'));
|
||||
fs.rmSync(file); fs.mkdirSync(file);
|
||||
assert.throws(() => receiptHistory(dataRoot), code('outcome-unknown'));
|
||||
fs.rmSync(file, { recursive: true });
|
||||
execFileSync('mkfifo', ['-m', '600', file]);
|
||||
assert.throws(() => receiptHistory(dataRoot), code('outcome-unknown'), 'a FIFO is refused without blocking');
|
||||
});
|
||||
|
||||
test('receiptHistory shows no planted key from any receipt field, nested value or key name', t => {
|
||||
const dataRoot = scratch(t);
|
||||
for (const key of PLANTED) appendReceipt(dataRoot, { time: new Date(0).toISOString(), op: 'init', note: `made ${key}`, nested: { list: [key, { deeper: key }] }, [key.replace(/-/g, '_')]: 1 });
|
||||
appendReceipt(dataRoot, { op: 'init', env: ENV_SECRET, [`x${ENV_SECRET}`]: 1 });
|
||||
appendReceipt(dataRoot, { op: PLANTED[1] });
|
||||
const all = receiptHistory(dataRoot, { limit: 100, secrets: secretValues({ PROXY_TOKEN: ENV_SECRET }) });
|
||||
assert.equal(all.entries.length, PLANTED.length + 2);
|
||||
assert.deepEqual(leaks(JSON.stringify(all)), []);
|
||||
assert.deepEqual(all.entries[0], { time: new Date(0).toISOString(), op: 'init', note: 'made [redacted]', nested: { list: ['[redacted]', { deeper: '[redacted]' }] } });
|
||||
assert.deepEqual(all.entries.at(-2), { op: 'init', env: '[redacted]' });
|
||||
assert.deepEqual(all.entries.at(-1), { op: '[redacted]' });
|
||||
});
|
||||
@@ -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);
|
||||
});
|
||||
@@ -0,0 +1,122 @@
|
||||
// #1873 settings-backend A1: the redaction the setup page's log and history reads use. It is
|
||||
// chat-fail's list (packages/harness/src/failure-notice.mjs) with Mosaic's key prefixes added.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { redactLine, redactValue, holdsSecret, secretValues, REDACTED, LINE_MAX } from '../src/redact.mjs';
|
||||
|
||||
// Short keys of each Mosaic prefix, letters only, so no long-run or mixed-run rule catches them:
|
||||
// only the key-prefix line does.
|
||||
const SHORT = { msk: 'msk-tag-qwertyu', mso: 'mso-tag-acme-qwerty', mst: 'mst-tag-triage-zxcvb', msp: 'msp-tag-plmokn' };
|
||||
|
||||
for (const [prefix, key] of Object.entries(SHORT)) {
|
||||
test(`a short ${prefix}- key is redacted from a line, and the rest of the line stays`, () => {
|
||||
const safe = redactLine(`proxy said ${key} was refused`);
|
||||
assert.equal(safe.text, `proxy said ${REDACTED} was refused`);
|
||||
assert.equal(safe.redacted, true);
|
||||
assert.equal(safe.text.includes(key), false);
|
||||
// Upper case, and joined by _ instead of -, too.
|
||||
assert.equal(redactLine(`proxy said ${key.toUpperCase()} x`).text, `proxy said ${REDACTED} x`);
|
||||
assert.equal(redactLine(`proxy said ${key.replace('-', '_')} x`).text, `proxy said ${REDACTED} x`);
|
||||
});
|
||||
}
|
||||
|
||||
test('a short Mosaic key glued to a word or an underscore is redacted', () => {
|
||||
for (const key of Object.values(SHORT)) {
|
||||
for (const glued of [`x${key}`, `_${key}`, `id=x${key}`, `9${key.toUpperCase()}`]) {
|
||||
const safe = redactLine(`proxy said ${glued} was refused`);
|
||||
assert.equal(safe.redacted, true, glued);
|
||||
assert.equal(safe.text.toLowerCase().includes(key.slice(4)), false, glued);
|
||||
assert.match(safe.text, / was refused$/);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('the real key shapes of each prefix are redacted', () => {
|
||||
const tail = 'Ab3_dE5-fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7dE';
|
||||
for (const key of [`msk-k1a2-${tail}`, `mso-k1a2-acme-${tail}`, `mst-k1a2-triage-${tail}`, `msp-k1a2-${tail}`, `sk-ant-${tail}`]) {
|
||||
const safe = redactLine(`key ${key} rejected by upstream`);
|
||||
assert.equal(safe.text.includes(key.slice(0, 12)), false, key.slice(0, 4));
|
||||
assert.equal(safe.text.includes(tail.slice(0, 8)), false, key.slice(0, 4));
|
||||
}
|
||||
});
|
||||
|
||||
test('bearer and basic tokens, key=value pairs, links, emails and paths never show', () => {
|
||||
const line = 'POST https://u:[email protected]/v1?key=abc Authorization: Bearer abcdefgh api_key=hunter2 token "quoted value" '
|
||||
+ 'by [email protected] from /home/jane/.config/mosaic-dev/proxy.json and ~/.mosaic/x C:\\Users\\j \\\\host\\share Basic dXNlcjpwYXNz eyJhbGciOi.x.y';
|
||||
const safe = redactLine(line);
|
||||
for (const bit of ['u:p@', 'api.example', 'abcdefgh', 'hunter2', 'quoted value', 'jane@', '/home/jane', '.config', '.mosaic', 'Users', 'share', 'dXNlcjpwYXNz', 'eyJhbGci']) {
|
||||
assert.equal(safe.text.includes(bit), false, bit);
|
||||
}
|
||||
assert.match(safe.text, /^POST /);
|
||||
});
|
||||
|
||||
test('a line holding an environment secret is withheld whole, even split by spaces, controls or encoding', () => {
|
||||
const secrets = secretValues({ MOSAIC_PROXY_KEY: 'plainwordsonly', HOME: '/home/x', SHORT_TOKEN: 'abc' });
|
||||
assert.deepEqual(secrets, ['plainwordsonly'], 'only secret-named values of 8 or more characters');
|
||||
for (const line of ['started with plainwordsonly', 'started with plain words only', 'started p\u0000lainwords\u200bonly', 'started %70lainwordsonly', 'started plainwordsonly']) {
|
||||
assert.deepEqual(redactLine(line, secrets), { text: null, redacted: true, withheld: true }, JSON.stringify(line));
|
||||
}
|
||||
assert.equal(holdsSecret('plain\nwords\nonly', secrets), true, 'split across lines');
|
||||
assert.equal(holdsSecret('nothing here', secrets), false);
|
||||
assert.equal(holdsSecret('plainwordsonly', []), false);
|
||||
});
|
||||
|
||||
test('a line that is empty, blank or only redactions is dropped; a long one is cut to the line cap', () => {
|
||||
assert.deepEqual(redactLine(''), { text: null, redacted: false });
|
||||
assert.deepEqual(redactLine(' \u0000\t '), { text: null, redacted: false });
|
||||
assert.deepEqual(redactLine(`${SHORT.mso} ${SHORT.mst}`), { text: null, redacted: true });
|
||||
assert.equal(redactLine(`a ${SHORT.mso} ${SHORT.mst} b`).text, `a ${REDACTED} b`, 'repeated redactions collapse');
|
||||
const long = redactLine('word '.repeat(200));
|
||||
assert.equal(Array.from(long.text).length, LINE_MAX);
|
||||
assert.match(long.text, /\.\.\.$/);
|
||||
assert.equal(redactLine('\u001b[31mred\u001b[0m text').text, 'red text', 'ANSI goes');
|
||||
});
|
||||
|
||||
test('a stored value is redacted all through: strings, nested strings, and keys that read as secrets', () => {
|
||||
const value = { op: 'account.proxy.apikey.add', time: '2026-11-19T12:34:56.789Z', provider: 'openai', note: `got ${SHORT.mso}`, deep: { list: [SHORT.msk, 3, true, null] }, [SHORT.mst.replace(/-/g, '_')]: 'x', 'bad key': 1 };
|
||||
const out = redactValue(value);
|
||||
assert.deepEqual(out, { op: 'account.proxy.apikey.add', time: '2026-11-19T12:34:56.789Z', provider: 'openai', note: `got ${REDACTED}`, deep: { list: [REDACTED, 3, true, null] } });
|
||||
assert.equal(JSON.stringify(out).includes('qwerty'), false);
|
||||
// Depth and size are bounded.
|
||||
assert.deepEqual(redactValue({ a: { b: { c: { d: { e: 1 } } } } }), { a: { b: { c: { d: null } } } });
|
||||
assert.equal(redactValue(Array.from({ length: 100 }, (_, i) => i)).length, 64);
|
||||
assert.equal(redactValue('plainwordsonly', ['plainwordsonly']), REDACTED);
|
||||
assert.equal(redactValue(undefined), null);
|
||||
});
|
||||
|
||||
// chat-fail's shape: redaction runs over a whole line before the cut, so a long run with no spaces
|
||||
// must stay linear.
|
||||
test('a 128000-character run with no spaces is redacted in well under a second', () => {
|
||||
const N = 128000;
|
||||
const secrets = secretValues({ API_KEY: 'plainwordsonly' });
|
||||
for (const run of ['a'.repeat(N), 'a.'.repeat(N / 2), 'a-'.repeat(N / 2), 'a/'.repeat(N / 2), 'a:'.repeat(N / 2), 'a"'.repeat(N / 2), 'mso-'.repeat(N / 4), 'a@'.repeat(N / 2)]) {
|
||||
const started = performance.now();
|
||||
const safe = redactLine(`${run} then the unit stopped`, secrets);
|
||||
const took = performance.now() - started;
|
||||
assert.ok(took < 1000, `${run.slice(0, 4)}... took ${Math.round(took)} ms`);
|
||||
assert.ok(safe.text === null || Array.from(safe.text).length <= LINE_MAX);
|
||||
}
|
||||
// A run that reads as a key is redacted whole, so the words after it show.
|
||||
assert.equal(redactLine(`${'a'.repeat(N)} then the unit stopped`).text, `${REDACTED} then the unit stopped`);
|
||||
});
|
||||
|
||||
// The list is a copy of failure-notice.mjs's. Every line must still match it, except the key-prefix
|
||||
// line, which is the original with mso, mst and msp added after msk, and Mosaic's glued-prefix line
|
||||
// right after it.
|
||||
test('the redaction list matches failure-notice.mjs line for line, with only the prefixes and one line added', () => {
|
||||
const source = file => fs.readFileSync(fileURLToPath(new URL(file, import.meta.url)), 'utf8');
|
||||
const block = text => {
|
||||
const body = /const REDACT = \[\n([\s\S]*?)\n\];/.exec(text)[1];
|
||||
return body.split('\n').map(line => line.trim()).filter(line => line && !line.startsWith('//')).map(line => line.replace(/,\s*\/\/ .*$/, ',').replace(/,$/, ''));
|
||||
};
|
||||
const keyWord = text => /const KEY_WORD = (.*);/.exec(text)[1];
|
||||
const theirs = source('../../harness/src/failure-notice.mjs'), ours = source('../src/redact.mjs');
|
||||
assert.equal(keyWord(ours), keyWord(theirs));
|
||||
const expected = block(theirs).map(line => line.replace('(?:sk|msk|rk|', '(?:sk|msk|mso|mst|msp|rk|'));
|
||||
assert.equal(expected.filter((line, i) => line !== block(theirs)[i]).length, 1, 'the original has one key-prefix line');
|
||||
const at = expected.findIndex(line => line.includes('(?:sk|msk|mso|mst|msp|rk|'));
|
||||
expected.splice(at + 1, 0, '/ms[kotp][-_]\\S+/gi');
|
||||
assert.deepEqual(block(ours), expected);
|
||||
});
|
||||
+196
-3
@@ -246,10 +246,42 @@ export returns:
|
||||
`minimal`, `low`, `medium`, `high`, `xhigh` and `max` as `--thinking`,
|
||||
Claude Code takes `low`, `medium`, `high`, `xhigh` and `max` as
|
||||
`--effort`.
|
||||
- `preflight({env})` returns `null` or `{code, message}`: pi checks its
|
||||
binary in `node_modules` (`harness-binary-missing`), Claude Code its
|
||||
- `preflight({env})` returns `null` or `{code, message}`, or a promise of
|
||||
one, which the launcher awaits before it launches: pi checks that
|
||||
it finds a pi binary (`harness-binary-missing`), Claude Code its
|
||||
session credential (`no-session-credential`) and `MOSAIC_CLAUDE_BIN`.
|
||||
- `version({env})` is what the manifest records as `harnessVersion`.
|
||||
Pi (#1873 pi-unpin, `harness/pi/binary.mjs`) takes `MOSAIC_PI_BIN`,
|
||||
which must be an absolute path to an executable file; else the first
|
||||
`pi` on PATH, skipping the repository's own `node_modules/.bin`; else
|
||||
the repository's copy. The binary must belong to the
|
||||
`@earendil-works/pi-coding-agent` package: it is the bin that package's
|
||||
`package.json` names. With none, the message is
|
||||
"no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci". No
|
||||
version is pinned. Pi's `mounts` and `installRoots` hold the found
|
||||
package's directory (the repository's whole `node_modules` for the
|
||||
repository's copy, whose packages are hoisted beside pi), and its `env`
|
||||
sets `MOSAIC_PI_BIN` to the binary's path inside the session, which the
|
||||
adapter runs. A refusal names the variable or "the pi on PATH" or "the
|
||||
repository's pi", never a host path. Pi's `preflight` then awaits
|
||||
the fit check (#1873 S3, `packages/harness/src/pi-fit.mjs`), which
|
||||
runs its sandboxed pi starts as child processes, four at a time, so the
|
||||
bus keeps answering, and which every caller that asks while it runs
|
||||
shares. It refuses `harness-feature-missing` when the binary lacks a flag the
|
||||
adapter passes, starts with a missing `-e` file, loads a planted ambient
|
||||
source or context file with the seal flags on, or can't be checked. When its `--help`
|
||||
lists `--no-mcp`, `env` sets `MOSAIC_PI_NO_MCP=1` and the adapter
|
||||
passes `--no-mcp`; otherwise it sets `MOSAIC_PI_NO_MCP=0`. `env`'s
|
||||
variables are built in `harness/pi/session-env.mjs`, which the fit
|
||||
check also uses so its runs get a session's names, from the bus's own
|
||||
environment as a session's are. `env` runs nothing: it reads the passed
|
||||
check for the binary's files and the bus's names, and throws when there
|
||||
is none. A host that calls `env` without `preflight` awaits the
|
||||
optional `ready({env})` first (the Triage host and route do), which runs
|
||||
the check, or shares the one running, and throws its refusal line.
|
||||
- `version({env})` is what the manifest records as `harnessVersion`, or a
|
||||
promise of it, which the launcher awaits. Pi's is the first line its
|
||||
binary prints for `--version`, or null; the binary runs as a child
|
||||
process.
|
||||
- `countsAccount()` says whether its launches spend `launch.account` and get
|
||||
the row 126 account checks. The same answer decides whether an account
|
||||
quota covers authority hires (#1873); the bus reads it once at start, so
|
||||
@@ -335,6 +367,167 @@ 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, signal })` 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 (a test may pass
|
||||
its own check as `check`). Its digest is
|
||||
that check's digest of the binary's files, on a pass and (W2) on a failure
|
||||
too. W2: a failed check carries `undecided`, true when the check could not
|
||||
tell and false when the binary definitely failed. `inspect` reads that
|
||||
field: `undecided: false` is `fit: "fail"`, definite; anything else is
|
||||
`fit: "unknown"`. It never parses the message. The check's runs, and the
|
||||
`--version` capture after a failure, stop when `signal` aborts and only the
|
||||
watch waits on them; a check `preflight` also waits on keeps running.
|
||||
`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. Each capture runs in its
|
||||
own process group: a stop, a timeout or the capture's end kills the whole
|
||||
group, so nothing the binary left in the background outlives it, and a signal
|
||||
already aborted runs nothing.
|
||||
|
||||
W2, the claude fit rule: every flag the claude adapter passes appears in
|
||||
`--help`, and `--version` answers. The flags come from the adapter itself:
|
||||
`inspect` runs `adapters/claude/adapter.sh` twice against a recorder that
|
||||
writes its argv (a first turn, which passes `--session-id`, then a second,
|
||||
which passes `--resume`), so the module holds no list of them. The rule
|
||||
reads the text the binary printed. A flag counts only from an option line
|
||||
(one to four spaces, then `-`), read up to the description; a flag a
|
||||
description only mentions does not count. `--x[-y]` anywhere in the text
|
||||
gives `--x-y` when `--x` has its own option line (claude 2.1.322 names
|
||||
`--system-prompt-file` only as `--system-prompt[-file]` in the `--bare`
|
||||
description, beside a `--system-prompt` option line). A missing flag is `fit: "fail"`, definite, with
|
||||
the flag as the rule id; so is `--version` or `--help` exiting non-zero
|
||||
(rule id `--version` or `--help`). A capture that timed out, was stopped,
|
||||
could not start or printed too much, or an adapter run that failed, is
|
||||
`unknown`. The captures run with no network: `unshare -r -n` when a probe
|
||||
of it runs, else without it, as the pi fit check drops unshare's layer when
|
||||
it is missing or refused, and the line then ends "run with the network on:
|
||||
unshare is missing" (or "unshare failed"). A definite verdict is kept in
|
||||
memory by the stat key (`src/stat-key.mjs`) of the binary's real file and
|
||||
the adapter; `preflight` reads it and refuses `harness-feature-missing` on a
|
||||
fail. A replaced binary or adapter has another key, so an old verdict no
|
||||
longer applies. With no verdict, or an undecided one, `preflight` does not
|
||||
refuse in W2. With no verdict it asks for a watch pass at once
|
||||
(`src/watch-request.mjs`); in the bus host that runs a stat pass now, and a
|
||||
full pass when the key changed, so a binary or adapter touched after its
|
||||
check is checked again within one pass. `preflight` stays synchronous and
|
||||
does not wait for that pass: the launch that missed, any launch before the
|
||||
first full pass at host start ends, and any launch while the pass runs go
|
||||
ahead. With no listener (no bus host) the request does nothing.
|
||||
|
||||
`watchKey({ env })` is a second optional export: a string the bus host's
|
||||
timer compares every five minutes (W2). It changes when anything the module
|
||||
would check changes; `claude-code` and `pi` give the stat key of their
|
||||
binary, adapter and module files. A module without it is keyed by the stat
|
||||
key of its `installRoots({ env })`. The key holds paths and is compared in
|
||||
memory only.
|
||||
|
||||
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>`, including one after a `word:` (as in
|
||||
`host:/srv/a`), a `file://` URL and a `~/` or `~name/` 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 (a module with
|
||||
no digest, or pi's files that can't be read), 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`.
|
||||
|
||||
W2: rule ids may start with `-` or `--`, so a flag can be one. The watch
|
||||
takes an optional `signal`; when it aborts, the watch aborts the `inspect`
|
||||
it is in (waiting up to 5 s for it to end), asks no further module and
|
||||
returns `stopped: true` with no records.
|
||||
|
||||
### The bus host's timer (#1873, slice W2)
|
||||
|
||||
`packages/cli/src/harness-watch-host.mjs` runs the watch in the bus host
|
||||
process, with the environment sessions launch from. `mosaic bus start`
|
||||
starts it unless `--no-launch` is given; `startHost({ harnessWatch })` is the
|
||||
switch. It runs one full pass at start, then every five minutes a stat pass
|
||||
that reads each module's `watchKey` (or install roots), and a full pass when
|
||||
that key changed or a day has gone by. While processing is halted or
|
||||
invalid it runs nothing; the first tick after resume runs a full pass. A
|
||||
tick while a pass runs does nothing. A pass that throws is logged with no
|
||||
path and the timer goes on; its key is not kept, so the next tick tries
|
||||
again. Stopping the host clears the timer and aborts the pass running. A
|
||||
request from `src/watch-request.mjs` (preflight's, on a miss) runs a tick at
|
||||
once; a request while a pass runs runs one tick after it, and a request
|
||||
after stop does nothing.
|
||||
|
||||
The host is the one writer of two files under `<org dataRoot>/state/`:
|
||||
|
||||
- `harness-watch.json`: `{ version: 1, checkedAt, harnesses, refused, gone }`,
|
||||
the records of the last full pass without `surfaces`, written to a temp
|
||||
file and renamed over the last one, mode 0600;
|
||||
- `harness-watch.jsonl`: one line per transition, appended only.
|
||||
|
||||
Before either is written, every string in them is checked again for the
|
||||
scratch directory, the home directory, the data root and any env value of
|
||||
six characters or more, from the launch environment and the host's own; a
|
||||
hit writes nothing and logs the pass as failed. Name, code, state, digest
|
||||
and time fields (`harness`, `gone`, `name`, `kept`, `source`, `code`,
|
||||
`state`, `fit`, `via`, the digests and the times) are not checked as text.
|
||||
|
||||
## Transcript usage source (row 124)
|
||||
|
||||
`usage-source/transcript` now ships interface 1's `readTurn({harness, root})`.
|
||||
|
||||
@@ -4,12 +4,16 @@
|
||||
// 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, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, statSync, writeFileSync } 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";
|
||||
import { hasSessionCredential, SESSION_CREDENTIALS } from "../../../seat/src/session.mjs";
|
||||
import { UNSHARE } from "../../../seat/src/sandbox.mjs";
|
||||
import { statKey } from "../../src/stat-key.mjs";
|
||||
import { requestWatch } from "../../src/watch-request.mjs";
|
||||
|
||||
export const interfaceVersion = 1;
|
||||
|
||||
@@ -60,13 +64,25 @@ export function preflight({ env, proxy = null }) {
|
||||
const names = SESSION_CREDENTIALS.map((set) => set.join(" with ")).join(", ");
|
||||
return { code: "no-session-credential", message: `the bus host has none of ${names}; the bus unit's EnvironmentFile sets one when the bus starts` };
|
||||
}
|
||||
if (!binary(env)) {
|
||||
const bin = binary(env);
|
||||
if (!bin) {
|
||||
return {
|
||||
code: "harness-binary-missing",
|
||||
// Names the variable, never its value, which is a host path (#1873).
|
||||
message: env.MOSAIC_CLAUDE_BIN ? "MOSAIC_CLAUDE_BIN isn't an executable file at an absolute path" : "no claude on the bus host's PATH",
|
||||
};
|
||||
}
|
||||
// #1873 harness watch W2: the fit verdict the last check of this very file gave (inspect below),
|
||||
// found by the stat key of its real file and the adapter's. Only a definite failure refuses. No
|
||||
// verdict, or a check that could not tell, does not refuse in this slice. With no verdict, the
|
||||
// bus host's watch is asked for a stat pass now (src/watch-request.mjs), so a binary replaced,
|
||||
// touched or moved since the last check is checked within one pass, not at the next tick; this
|
||||
// launch is not held for it.
|
||||
const verdict = verdictFor(bin);
|
||||
if (!verdict) requestWatch();
|
||||
if (verdict?.fit === "fail") {
|
||||
return { code: "harness-feature-missing", message: `the claude from ${env.MOSAIC_CLAUDE_BIN ? "MOSAIC_CLAUDE_BIN" : "PATH"} failed the fit check: ${verdict.why}` };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -89,6 +105,196 @@ export function version({ env }) {
|
||||
return r.status === 0 ? r.stdout.trim().split("\n")[0] || null : null;
|
||||
}
|
||||
|
||||
// #1873 harness watch: 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.
|
||||
//
|
||||
// W2, the fit rule: every flag the adapter passes is in --help, and --version answers. The flags
|
||||
// come from the adapter itself: adapter.sh runs twice against a stand-in claude that writes its
|
||||
// argv (once fresh, which passes --session-id, and once with a session id kept, which passes
|
||||
// --resume), so there is no second list of them here. --help is read on the text the binary
|
||||
// printed, not on the redacted copy the watch stores. A flag counts only from an option line (one
|
||||
// to four spaces, then -), where it is named before the description: a flag a description only
|
||||
// mentions does not count. --x[-y] anywhere in the text gives --x-y when --x has its own option
|
||||
// line (claude 2.1.322 names --system-prompt-file only as --system-prompt[-file] in the --bare
|
||||
// description, beside a --system-prompt option line). A flag missing from it, or --version or
|
||||
// --help exiting non-zero, is fit "fail", definite, with the flag as the rule id. A capture that
|
||||
// timed out, was stopped, could not start or printed too much, or an adapter run that failed, is
|
||||
// "unknown": the check could not tell. The captures run with no network: unshare -r -n when it
|
||||
// runs here (probed per check), else without it, as the pi fit check drops unshare's layer when it
|
||||
// is missing or refused; the line says so. preflight reads the definite verdicts (verdictFor).
|
||||
const CAPTURE_TIMEOUT = 10000;
|
||||
const CAPTURE_MAX = 1024 * 1024;
|
||||
export async function inspect({ env = {}, scratch = null, signal, unshare = UNSHARE } = {}) {
|
||||
const bin = lookup(env);
|
||||
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 key = verdictKey(real);
|
||||
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 fence = await networkFence(unshare, runEnv, signal);
|
||||
const wrap = (args) => (fence.argv ? [...fence.argv, real, ...args] : [real, ...args]);
|
||||
const v = await capture(wrap(["--version"]), runEnv, signal);
|
||||
const help = await capture(wrap(["--help"]), runEnv, signal);
|
||||
const flags = await adapterFlags(scratch, signal);
|
||||
const first = v.why === null ? v.out.split("\n")[0].trim() : "";
|
||||
const version = first && first.length <= 128 ? first : null;
|
||||
const verdict = judge({ v, first, help, flags });
|
||||
// Kept when it is definite, by the key taken before the check: if the binary or the adapter
|
||||
// changed during it, preflight looks up the new key and does not find this one.
|
||||
if (verdict.definite && key) remember(key, { fit: verdict.fit, failed: verdict.failed, why: verdict.why });
|
||||
const line = fence.argv ? verdict.why : `${verdict.why}; run with the network on: ${fence.why}`;
|
||||
return { ...base, found: true, version, digest, fit: verdict.fit, definite: verdict.definite, failed: verdict.failed, line, surfaces: help.why === null ? { help: help.out } : {} };
|
||||
} finally {
|
||||
rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
// The watch's lookup: MOSAIC_CLAUDE_BIN, else the first claude in an absolute PATH entry.
|
||||
const lookup = (env) => binary({ ...env, PATH: (env.PATH ?? "").split(delimiter).filter((dir) => isAbsolute(dir)).join(delimiter) });
|
||||
|
||||
function judge({ v, first, help, flags }) {
|
||||
const unsure = (why) => ({ fit: "unknown", definite: false, failed: [], why: `${why}; fit not checked` });
|
||||
const fail = (failed, why) => ({ fit: "fail", definite: true, failed, why });
|
||||
if (v.why === "exit" || (v.why === null && !first)) return fail(["--version"], "--version gave no answer");
|
||||
if (v.why !== null) return unsure(`--version ${v.why}`);
|
||||
if (help.why === "exit") return fail(["--help"], "--help exited with an error");
|
||||
if (help.why !== null) return unsure(`--help ${help.why}`);
|
||||
if (!flags) return unsure("the adapter's flags could not be read");
|
||||
const have = helpFlags(help.out);
|
||||
const missing = flags.filter((flag) => !have.has(flag));
|
||||
if (missing.length) return fail(missing, failMessage(missing));
|
||||
return { fit: "pass", definite: true, failed: [], why: "every flag the adapter passes is in --help" };
|
||||
}
|
||||
|
||||
const failMessage = (failed) => (failed.length === 1 && (failed[0] === "--version" || failed[0] === "--help") ? `${failed[0]} gave no answer` : `--help lacks ${failed.join(" ")}`);
|
||||
|
||||
// The flags --help names in its option lines: a line that starts with one to four spaces then -,
|
||||
// read up to the first run of two spaces (the option and its aliases, before the description).
|
||||
// Then --x[-y] anywhere in the text gives --x-y when --x is one of them.
|
||||
const OPTION_LINE = /^ {1,4}-/;
|
||||
const HEAD_FLAG = /(?:^|[\s,])(--?[A-Za-z][A-Za-z0-9-]*)(?=$|[\s,=<[])/g;
|
||||
const BRACKET = /(?<![A-Za-z0-9_-])(--?[A-Za-z][A-Za-z0-9-]*)\[(-[A-Za-z0-9-]+)\]/g;
|
||||
export function helpFlags(text) {
|
||||
const all = String(text ?? "");
|
||||
const out = new Set();
|
||||
for (const line of all.split("\n")) {
|
||||
if (!OPTION_LINE.test(line)) continue;
|
||||
const head = line.trim().split(/\s{2,}/)[0];
|
||||
for (const m of head.matchAll(HEAD_FLAG)) out.add(m[1]);
|
||||
}
|
||||
for (const m of all.matchAll(BRACKET)) if (out.has(m[1])) out.add(m[1] + m[2]);
|
||||
return out;
|
||||
}
|
||||
|
||||
// unshare -r -n, when it runs here: { argv } to put before the binary, else { argv: null, why }.
|
||||
async function networkFence(unshare, runEnv, signal) {
|
||||
if (!unshare || !existsSync(unshare)) return { argv: null, why: "unshare is missing" };
|
||||
const argv = [unshare, "-r", "-n", "--"];
|
||||
const r = await capture([...argv, "/usr/bin/true"], runEnv, signal);
|
||||
return r.why === null ? { argv } : { argv: null, why: "unshare failed" };
|
||||
}
|
||||
|
||||
// The flags adapter.sh passes claude, from its own runs against a stand-in that writes its argv.
|
||||
// Kept per stat key of the adapter and its stream reader; null when a run failed.
|
||||
const FLAG = /^--?[A-Za-z]/;
|
||||
const RECORDER = "#!/bin/sh\nfor a in \"$@\"; do printf '%s\\n' \"$a\"; done > \"$FIT_ARGV_OUT\"\necho '{\"type\":\"result\",\"subtype\":\"success\",\"result\":\"ok\"}'\n";
|
||||
let flagsMemo = null;
|
||||
async function adapterFlags(scratch, signal) {
|
||||
const key = statKey([adapter(), STREAM]);
|
||||
if (flagsMemo?.key === key) return flagsMemo.flags;
|
||||
const dir = mkdtempSync(join(scratch, "claude-flags-"));
|
||||
try {
|
||||
const recorder = join(dir, "claude");
|
||||
const file = join(dir, "input.json");
|
||||
const argvFile = join(dir, "argv");
|
||||
writeFileSync(recorder, RECORDER, { mode: 0o700 });
|
||||
writeFileSync(file, "{}\n");
|
||||
mkdirSync(join(dir, "home"));
|
||||
const runEnv = {
|
||||
PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, HOME: join(dir, "home"), MOSAIC_CLAUDE_BIN: recorder, FIT_ARGV_OUT: argvFile,
|
||||
MOSAIC_SYSTEM_PROMPT_FILE: file, MOSAIC_CLAUDE_SETTINGS: file, MOSAIC_CLAUDE_MCP_CONFIG: file, MOSAIC_REQUEST: "fit check",
|
||||
MOSAIC_WORKSPACE: join(dir, "work"), MOSAIC_SESSION_DIR: join(dir, "session"), MOSAIC_MODEL: "fit-model", MOSAIC_THINKING: "low", MOSAIC_TOOLS: "Read",
|
||||
};
|
||||
const flags = new Set();
|
||||
// The first run keeps the session id it named, so the second passes --resume.
|
||||
for (let round = 0; round < 2; round++) {
|
||||
rmSync(argvFile, { force: true });
|
||||
const r = await capture(["/bin/sh", adapter()], runEnv, signal, dir);
|
||||
if (r.why !== null || !existsSync(argvFile)) return null;
|
||||
const argv = readFileSync(argvFile, "utf8").split("\n").filter(Boolean);
|
||||
if (!argv.length) return null;
|
||||
for (const arg of argv) if (FLAG.test(arg)) flags.add(arg);
|
||||
}
|
||||
const list = [...flags];
|
||||
if (key === statKey([adapter(), STREAM])) flagsMemo = { key, flags: list };
|
||||
return list;
|
||||
} catch {
|
||||
return null;
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
// The verdicts of the last checks, by the stat key of the binary's real file and the adapter's
|
||||
// (src/stat-key.mjs), as the watch compares them. Definite ones only, at most VERDICTS_MAX.
|
||||
const VERDICTS_MAX = 16;
|
||||
const verdicts = new Map();
|
||||
const verdictKey = (real) => statKey([real, adapter(), STREAM]);
|
||||
function remember(key, verdict) {
|
||||
verdicts.delete(key);
|
||||
verdicts.set(key, Object.freeze({ ...verdict, failed: Object.freeze([...verdict.failed]) }));
|
||||
while (verdicts.size > VERDICTS_MAX) verdicts.delete(verdicts.keys().next().value);
|
||||
}
|
||||
function verdictFor(bin) {
|
||||
try { return verdicts.get(verdictKey(realpathSync(bin))) ?? null; } catch { return null; }
|
||||
}
|
||||
|
||||
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")));
|
||||
});
|
||||
}
|
||||
|
||||
// Runs argv in its own process group and returns { out, why }: why is null when it exited 0 within
|
||||
// CAPTURE_TIMEOUT and printed at most CAPTURE_MAX, else "exit" (it exited non-zero: an answer),
|
||||
// "timed out", "was stopped", "could not start" or "printed too much" (no answer). A timeout, a
|
||||
// stop or too much output kills the whole group, and so does its end: nothing it left in the
|
||||
// background outlives the capture. An already aborted signal starts nothing.
|
||||
function capture(argv, runEnv, signal, cwd = runEnv.HOME) {
|
||||
return new Promise((resolve) => {
|
||||
if (signal?.aborted) { resolve({ out: "", why: "was stopped" }); return; }
|
||||
let child;
|
||||
try { child = spawn(argv[0], argv.slice(1), { env: runEnv, cwd, stdio: ["ignore", "pipe", "ignore"], detached: true }); } catch { resolve({ out: "", why: "could not start" }); return; }
|
||||
const out = [];
|
||||
let size = 0, done = false;
|
||||
const finish = (why) => { if (done) return; done = true; clearTimeout(timer); signal?.removeEventListener?.("abort", stopped); resolve({ out: why === null ? Buffer.concat(out).toString("utf8") : "", why }); };
|
||||
const kill = (why) => {
|
||||
try { process.kill(-child.pid, "SIGKILL"); } catch { try { child.kill("SIGKILL"); } catch {} }
|
||||
finish(why);
|
||||
};
|
||||
const stopped = () => kill("was stopped");
|
||||
const timer = setTimeout(() => kill("timed out"), CAPTURE_TIMEOUT);
|
||||
signal?.addEventListener?.("abort", stopped, { once: true });
|
||||
child.stdout.on("data", (c) => { size += c.length; if (size > CAPTURE_MAX) kill("printed too much"); else out.push(c); });
|
||||
child.on("error", () => finish("could not start"));
|
||||
child.on("close", (status) => {
|
||||
try { process.kill(-child.pid, "SIGKILL"); } catch {}
|
||||
finish(status === 0 ? null : "exit");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Its launches spend the business's launch.account (row 126).
|
||||
export const countsAccount = () => true;
|
||||
|
||||
@@ -155,6 +361,15 @@ export function installRoots({ env = {} } = {}) {
|
||||
return roots;
|
||||
}
|
||||
|
||||
// #1873 harness watch W2: the stat key the host's 5-minute pass compares (src/stat-key.mjs): the
|
||||
// binary the watch's lookup finds and its directory, the adapter and its stream reader (the fit
|
||||
// rule reads their flags) and the unslop hook. No binary found is a key too. The key holds paths
|
||||
// and stays in memory.
|
||||
export function watchKey({ env = {} } = {}) {
|
||||
const bin = lookup(env);
|
||||
return statKey([bin, bin ? dirname(bin) : null, adapter(), STREAM, join(REPO, "adapters", "claude"), join(REPO, "tools", "unslop-hook")]);
|
||||
}
|
||||
|
||||
// A turn ends when claude exits; there is no marker.
|
||||
export const turnEnd = () => "exit";
|
||||
export const transcript = ({ runDir, store = null }) => (store ? store.projects : join(runDir, "home", ".claude", "projects"));
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
// #1873 pi-unpin: the pi binary a session runs, and the package it comes from. Nothing here is
|
||||
// pinned. The version label is read from the binary (or its package.json), never compared to a
|
||||
// fixed one. The launcher, the bundle and the usage reader call these on the host.
|
||||
import { accessSync, constants, readFileSync, realpathSync, statSync } from "node:fs";
|
||||
import { dirname, isAbsolute, join, relative } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { spawn } from "node:child_process";
|
||||
|
||||
const REPO = fileURLToPath(new URL("../../../../", import.meta.url));
|
||||
export const PACKAGE = "@earendil-works/pi-coding-agent";
|
||||
export const REPO_BIN = join(REPO, "node_modules", ".bin", "pi");
|
||||
const VERSION_MAX = 128;
|
||||
|
||||
export const executable = (path) => {
|
||||
try {
|
||||
accessSync(path, constants.X_OK);
|
||||
return statSync(path).isFile();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const REPO_NODE_MODULES = join(REPO, "node_modules");
|
||||
const realOrNull = (path) => { try { return realpathSync(path); } catch { return null; } };
|
||||
const inside = (path, root) => root === "/" || path === root || path.startsWith(root + "/");
|
||||
|
||||
// #1873 S2: the binary a session runs, with the name a refusal line uses for it (never a path):
|
||||
// MOSAIC_PI_BIN, which must be an absolute path to an executable file; else the first executable
|
||||
// pi on PATH, skipping the repository's own node_modules/.bin (a session PATH starts with it, so
|
||||
// the bus's own PATH and a session's give the same answer); else the repository's copy. The binary
|
||||
// must belong to the pi package: its package directory (or, for the repository's copy, the
|
||||
// repository's node_modules) is what the sandbox mounts. The answer: { bin, label, root, view,
|
||||
// dest, pkg } or { bin: null, problem }. root is the real directory mounted, dest where it is
|
||||
// mounted (null: at root itself), view the binary's path inside the session, pkg the package's
|
||||
// real directory.
|
||||
export function locate(env = {}) {
|
||||
if (env.MOSAIC_PI_BIN !== undefined && env.MOSAIC_PI_BIN !== "") {
|
||||
const bin = env.MOSAIC_PI_BIN;
|
||||
if (typeof bin !== "string" || !isAbsolute(bin) || !executable(bin)) return { bin: null, problem: "MOSAIC_PI_BIN isn't an executable file at an absolute path" };
|
||||
return placed(bin, "MOSAIC_PI_BIN", env);
|
||||
}
|
||||
const repoBin = realOrNull(join(REPO_NODE_MODULES, ".bin"));
|
||||
for (const dir of String(env.PATH ?? "").split(":")) {
|
||||
if (!isAbsolute(dir)) continue;
|
||||
const real = realOrNull(dir);
|
||||
if (real === null || (repoBin !== null && real === repoBin)) continue;
|
||||
if (executable(join(dir, "pi"))) return placed(join(dir, "pi"), "the pi on PATH", env);
|
||||
}
|
||||
if (executable(REPO_BIN)) return placed(REPO_BIN, "the repository's pi", env);
|
||||
return { bin: null, problem: "no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci" };
|
||||
}
|
||||
|
||||
function placed(bin, label, env) {
|
||||
const pkg = packageDir(bin);
|
||||
const realBin = realOrNull(bin);
|
||||
if (!pkg || !realBin) return { bin: null, problem: `${label} isn't part of the ${PACKAGE} package` };
|
||||
// The package directory is mounted whole, so the binary must be the pi bin its package.json
|
||||
// names (a string bin, or bin.pi), inside that directory. A stray executable under some wider
|
||||
// directory whose package.json carries the name doesn't count.
|
||||
if (namedBin(pkg) !== realBin) return { bin: null, problem: `${label} isn't the pi bin the ${PACKAGE} package names` };
|
||||
// The repository's copy keeps the mount every session has had: all of node_modules, at its own
|
||||
// path, since its packages are hoisted beside pi.
|
||||
const repoReal = realOrNull(REPO_NODE_MODULES);
|
||||
if (repoReal !== null && inside(pkg, repoReal)) {
|
||||
return { bin, label, root: repoReal, dest: REPO_NODE_MODULES, view: join(REPO_NODE_MODULES, relative(repoReal, realBin)), pkg };
|
||||
}
|
||||
// Any other copy: its package directory alone, at its real path. npm nests a global install's
|
||||
// dependencies inside it; a copy that needs packages beside it fails the fit check (S3).
|
||||
// A package directory that holds the home directory or the repository would mount them.
|
||||
const held = [homedir(), env.HOME, REPO].filter((p) => typeof p === "string" && isAbsolute(p)).map((p) => realOrNull(p) ?? p);
|
||||
if (held.some((p) => inside(p, pkg))) return { bin: null, problem: `${label}'s package directory holds the home directory or the repository` };
|
||||
return { bin, label, root: pkg, dest: null, view: realBin, pkg };
|
||||
}
|
||||
|
||||
// The real path of the pi bin the package.json in `pkg` names, inside `pkg`, or null.
|
||||
function namedBin(pkg) {
|
||||
let bin;
|
||||
try { bin = JSON.parse(readFileSync(join(pkg, "package.json"), "utf8")).bin; } catch { return null; }
|
||||
const rel = typeof bin === "string" ? bin : bin && typeof bin === "object" ? bin.pi : null;
|
||||
if (typeof rel !== "string" || !rel) return null;
|
||||
const real = realOrNull(join(pkg, rel));
|
||||
return real !== null && real !== pkg && inside(real, pkg) ? real : null;
|
||||
}
|
||||
|
||||
// The package directory that holds the binary's real file: the nearest directory above it whose
|
||||
// package.json has a name. That name must be PACKAGE, or there is none (null).
|
||||
export function packageDir(bin) {
|
||||
let dir;
|
||||
try { dir = dirname(realpathSync(bin)); } catch { return null; }
|
||||
for (let depth = 0; depth < 32; depth++) {
|
||||
let pkg = null;
|
||||
try { pkg = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")); } catch { pkg = null; }
|
||||
if (pkg && typeof pkg === "object" && pkg.name !== undefined) return pkg.name === PACKAGE ? dir : null;
|
||||
const up = dirname(dir);
|
||||
if (up === dir) return null;
|
||||
dir = up;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// The found package's own version, a file read with no spawn, or null.
|
||||
export function packageVersion(bin) {
|
||||
const dir = bin ? packageDir(bin) : null;
|
||||
if (!dir) return null;
|
||||
try {
|
||||
const v = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")).version;
|
||||
return typeof v === "string" && cleanLabel(v) ? v : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// The most a --version may print (spawnSync's default buffer, which the check used before).
|
||||
const VERSION_OUTPUT_MAX = 1024 * 1024;
|
||||
// One printable line, short enough to record.
|
||||
const cleanLabel = (s) => s.length > 0 && s.length <= VERSION_MAX && !/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/.test(s);
|
||||
|
||||
// The first line `<bin> --version` prints, or null when it fails, times out or prints something
|
||||
// that is not one short printable line. The environment holds only a PATH that finds node. A
|
||||
// promise: the binary runs as a child process, so the caller's event loop keeps running.
|
||||
// #1873 harness watch W2: it runs in its own process group, and a timeout, too much output or the
|
||||
// optional signal aborting kills the whole group, so no child it started outlives it.
|
||||
export function printedVersion(bin, { signal = null } = {}) {
|
||||
if (!bin || signal?.aborted) return Promise.resolve(null);
|
||||
return new Promise((resolve) => {
|
||||
const out = [];
|
||||
let size = 0;
|
||||
let done = false;
|
||||
let child;
|
||||
let timer = null;
|
||||
const finish = (line) => {
|
||||
if (done) return;
|
||||
done = true;
|
||||
clearTimeout(timer);
|
||||
signal?.removeEventListener?.("abort", stop);
|
||||
resolve(line);
|
||||
};
|
||||
const kill = () => {
|
||||
try { process.kill(-child.pid, "SIGKILL"); } catch { try { child.kill("SIGKILL"); } catch {} }
|
||||
finish(null);
|
||||
};
|
||||
const stop = () => kill();
|
||||
try {
|
||||
child = spawn(bin, ["--version"], { env: { PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, PI_OFFLINE: "1" }, stdio: ["ignore", "pipe", "ignore"], detached: true });
|
||||
} catch {
|
||||
finish(null);
|
||||
return;
|
||||
}
|
||||
timer = setTimeout(kill, 5000);
|
||||
signal?.addEventListener?.("abort", stop, { once: true });
|
||||
child.stdout.on("data", (chunk) => {
|
||||
size += chunk.length;
|
||||
if (size > VERSION_OUTPUT_MAX) kill(); else out.push(chunk);
|
||||
});
|
||||
child.on("error", () => finish(null));
|
||||
child.on("close", (status) => {
|
||||
const line = Buffer.concat(out).toString("utf8").split("\n")[0].trim();
|
||||
finish(status === 0 && cleanLabel(line) ? line : null);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
// `compactions` through the harness kind (packages/plugins/README.md has the contract). This file
|
||||
// reads only the session's native pi store and the Claude history beside it.
|
||||
//
|
||||
// Pi (pinned 0.85.1): a completed compaction is one `type: "compaction"` entry,
|
||||
// Pi (read from 0.85.1): a completed compaction is one `type: "compaction"` entry,
|
||||
// written by SessionManager.appendCompaction after the summary exists. A start
|
||||
// or an aborted compaction writes none. A `compactionSummary` message, a
|
||||
// message inside a compaction's retainedTail and a branch_summary are not
|
||||
@@ -19,8 +19,10 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { closeSync, constants as fsc, fstatSync, lstatSync, openSync, readdirSync, readSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { locate, packageVersion } from "./binary.mjs";
|
||||
|
||||
export const PI_VERSION = "0.85.1";
|
||||
// #1873 S1: the found package's own version (its package.json, no spawn), or null.
|
||||
const foundVersion = () => packageVersion(locate(process.env).bin);
|
||||
const TAIL = 4096;
|
||||
const CHUNK = 1 << 20;
|
||||
const STREAM = /^[A-Za-z0-9._-]{1,200}\.jsonl$/;
|
||||
@@ -158,9 +160,9 @@ export function compactions({ store, cursors, budget, streams, first }) {
|
||||
if (first && claudeHistory(store)) {
|
||||
// Claude runs before this ledger: their compactions can't be counted
|
||||
// (Vikunja 466), so the count is unknown for good, as after a Claude run.
|
||||
return { found: [], cursors: {}, known: false, reason: "harness-unsupported", version: PI_VERSION };
|
||||
return { found: [], cursors: {}, known: false, reason: "harness-unsupported", version: foundVersion() };
|
||||
}
|
||||
const scan = scanPi(store.session, cursors, budget, streams);
|
||||
const reason = REASONS.find((p) => scan.problems.includes(p)) ?? null;
|
||||
return { found: scan.found, cursors: scan.cursors, known: reason === null, reason, version: PI_VERSION };
|
||||
return { found: scan.found, cursors: scan.cursors, known: reason === null, reason, version: foundVersion() };
|
||||
}
|
||||
@@ -1,18 +1,20 @@
|
||||
// The "pi" harness plug-in (row 65, REQ-PLUG-2, Q156): a managed session on the pinned
|
||||
// @earendil-works/pi-coding-agent CLI, the repository's node_modules/.bin/pi. The tool gate is the
|
||||
// The "pi" harness plug-in (row 65, REQ-PLUG-2, Q156): a managed session on the
|
||||
// @earendil-works/pi-coding-agent CLI that binary.mjs finds (#1873: no version pin). The tool gate is the
|
||||
// Pi extension, which stays in packages/harness (a module can't change that boundary). The
|
||||
// launcher, the bundle and the usage reader call these on the host; the runner gets the answers
|
||||
// as data in session.json. packages/plugins/README.md lists the interface.
|
||||
import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
|
||||
import { lstatSync, mkdirSync, writeFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { locate, printedVersion } from "./binary.mjs";
|
||||
import { statKey } from "../../src/stat-key.mjs";
|
||||
import { fitVerdict, piFit } from "../../../harness/src/pi-fit.mjs";
|
||||
import { envNames, harnessEnv } from "./session-env.mjs";
|
||||
|
||||
export const interfaceVersion = 1;
|
||||
|
||||
const REPO = fileURLToPath(new URL("../../../../", import.meta.url));
|
||||
const PACKAGE = "@earendil-works/pi-coding-agent";
|
||||
const BIN = join(REPO, "node_modules", ".bin", "pi");
|
||||
const EXTENSION = fileURLToPath(new URL("../../../harness/src/pi-extension.mjs", import.meta.url));
|
||||
// Row 66 (#1559): the unslop check, loaded as a second extension. It logs to MOSAIC_UNSLOP_LOG,
|
||||
// which the runner sets, and the runner tells the operator.
|
||||
@@ -31,33 +33,74 @@ export const adapter = () => join(REPO, "adapters", "pi", "adapter.sh");
|
||||
export const levels = () => LEVELS;
|
||||
export const outputPolicy = ({ proxy }) => proxy ? "non-newline-stdout" : "stdout";
|
||||
|
||||
// The session runs pi from the repository's node_modules, which the sandbox mounts.
|
||||
export function preflight({ env = {}, proxy = null } = {}) {
|
||||
// The session runs the pi binary.mjs finds, from the package directory the sandbox mounts. Async:
|
||||
// the fit check runs child processes, and the launcher awaits the answer before it launches.
|
||||
export async function preflight({ env = {}, proxy = null } = {}) {
|
||||
if (!(typeof proxy?.key === "string" && proxy.key)
|
||||
&& !["ANTHROPIC_API_KEY", "OPENAI_API_KEY", "OPENROUTER_API_KEY", "ZAI_API_KEY"].some(name => Boolean(env[name]))) {
|
||||
return { code: "no-session-credential", message: "pi needs its own provider key or a session proxy key" };
|
||||
}
|
||||
if (!existsSync(BIN)) return { code: "harness-binary-missing", message: "pi isn't installed in the repository's node_modules; run npm ci in the repository" };
|
||||
const found = locate(env);
|
||||
if (!found.bin) return { code: "harness-binary-missing", message: found.problem };
|
||||
// #1873 S3: the found binary takes every flag the adapter passes and keeps the known ambient
|
||||
// sources off with them, or the launch is refused. A check that cannot tell refuses too. The
|
||||
// check's environment comes from this process's own (the bus's), as a session's does.
|
||||
const fit = await piFit(found, { levels: LEVELS });
|
||||
if (!fit.ok) return { code: "harness-feature-missing", message: fit.message };
|
||||
return null;
|
||||
}
|
||||
|
||||
// The exact pin from the repository's package.json.
|
||||
export function version() {
|
||||
try {
|
||||
const pkg = JSON.parse(readFileSync(join(REPO, "package.json"), "utf8"));
|
||||
return pkg.dependencies?.[PACKAGE] ?? pkg.devDependencies?.[PACKAGE] ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
// #1873 S1: what the found binary prints for --version (5 s timeout), or null. A label only:
|
||||
// nothing compares it to a fixed version. Async, so the bus keeps answering while it runs.
|
||||
export async function version({ env = {} } = {}) {
|
||||
return printedVersion(locate(env).bin);
|
||||
}
|
||||
|
||||
// #1873 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, on a pass and (W2) on a failure too, null
|
||||
// only when the files can't be read. A failure pi-fit marks undecided could not decide, so its fit
|
||||
// is "unknown"; one marked undecided: false is definite. A failure without the field reads as
|
||||
// undecided, so it can never read as definite. The message is shown, never parsed. source names
|
||||
// the lookup step, never a path. scratch is not used: the fit check makes its own scratch directories.
|
||||
// signal goes to the fit check (W2): when the watch stops, a check only it waits on is stopped.
|
||||
const SOURCES = Object.freeze({ MOSAIC_PI_BIN: "MOSAIC_PI_BIN", "the pi on PATH": "PATH", "the repository's pi": "repo" });
|
||||
// check: the fit check to run, piFit unless a test gives a stand-in for it.
|
||||
export async function inspect({ env = {}, signal = null, check = piFit } = {}) {
|
||||
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 check(at, { levels: LEVELS, signal });
|
||||
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: {} };
|
||||
const undecided = fit.undecided !== false;
|
||||
return { found: true, source, version: await printedVersion(at.bin, { signal }), digest: typeof fit.digest === "string" ? fit.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 } = {}) {
|
||||
const fit = await piFit(found(env), { levels: LEVELS });
|
||||
if (!fit.ok) throw new Error(fit.message);
|
||||
}
|
||||
|
||||
// Pi sessions run on a provider key, never the Claude account limit.
|
||||
export const countsAccount = () => false;
|
||||
export const mounts = () => [
|
||||
{ src: join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), why: "the managed model event stream" },
|
||||
{ src: realpathSync(join(REPO, "node_modules")), dest: join(REPO, "node_modules"), why: "pi and its packages, on the session's PATH" },
|
||||
...Object.values(UNSLOP).map((src) => ({ src, why: "the unslop check the session's second extension runs" })),
|
||||
];
|
||||
// #1873 S2: the found pi's package directory (the repository's copy: all of node_modules, as before).
|
||||
// One under /usr is already in every sandbox.
|
||||
const found = (env) => {
|
||||
const at = locate(env);
|
||||
if (!at.bin) throw new Error(at.problem);
|
||||
return at;
|
||||
};
|
||||
export function mounts({ env = {} } = {}) {
|
||||
const at = found(env);
|
||||
return [
|
||||
{ src: join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), why: "the managed model event stream" },
|
||||
...(at.root.startsWith("/usr/") ? [] : [{ src: at.root, ...(at.dest ? { dest: at.dest } : {}), why: `pi and its packages (${at.label})` }]),
|
||||
...Object.values(UNSLOP).map((src) => ({ src, why: "the unslop check the session's second extension runs" })),
|
||||
];
|
||||
}
|
||||
// Pi has no web tool of its own, so a granted role under harness-builtin is refused.
|
||||
export const builtinWeb = () => false;
|
||||
export const builtins = ({ tools }) => [...tools];
|
||||
@@ -105,26 +148,41 @@ export function bundle({ dir, models, proxy = null, transports = {} } = {}) {
|
||||
return { files: { proxyModels: file }, digests: [["../home/.pi/agent/models.json", createHash("sha256").update(text).digest("hex")]], sources };
|
||||
}
|
||||
|
||||
// The tool limit is the built-ins plus the typed tools, which the extension serves.
|
||||
export function env({ policy, tools, session, proxy = null }) {
|
||||
return {
|
||||
PI_OFFLINE: "1",
|
||||
MOSAIC_TOOLS: [...policy.tools, ...tools.map((t) => t.name)].join(","),
|
||||
MOSAIC_EXTENSIONS: [EXTENSION, UNSLOP.unslopExtension].join(","),
|
||||
PI_PROVIDER: proxy ? session.proxyTransport.piProvider : session.provider ?? "",
|
||||
PI_MODEL: session.model,
|
||||
MOSAIC_THINKING: session.thinking,
|
||||
...(proxy ? { ANTHROPIC_API_KEY: proxy.key } : {}),
|
||||
};
|
||||
// The tool limit is the built-ins plus the typed tools, which the extension serves. #1873 S2:
|
||||
// MOSAIC_PI_BIN is the found binary's path inside the session; the bus's own environment names it
|
||||
// (env defaults to it, since buildBundle and the Triage host call env() without one). preflight,
|
||||
// version and mounts get the launcher's env instead. Both find the same binary: the session env
|
||||
// copies PATH and MOSAIC_PI_BIN from the bus's, and locate() skips the repository's .bin on PATH.
|
||||
// env() runs nothing: it reads the fit check that preflight or ready() awaited for what it found.
|
||||
// With none passed for those files and this environment (they differ, the files changed since, or
|
||||
// nobody awaited one), it throws, so the launch fails closed (as harness-failed, not
|
||||
// harness-feature-missing).
|
||||
export function env({ policy, tools, session, proxy = null, env: hostEnv = process.env }) {
|
||||
const at = found(hostEnv);
|
||||
// #1873 S3: a session runs only a binary that passed the fit check.
|
||||
const fit = fitVerdict(at, { levels: LEVELS });
|
||||
if (!fit) throw new Error(`pi (${at.label}) has no passed fit check for its files; preflight or ready() runs one first`);
|
||||
return harnessEnv({ bin: at.view, noMcp: fit.noMcp, tools: [...policy.tools, ...tools.map((t) => t.name)], extensions: [EXTENSION, UNSLOP.unslopExtension], session, proxy });
|
||||
}
|
||||
|
||||
// #1873 triage-fences: the names env() returns, less the provider key, and the directories its
|
||||
// adapter and mounts live in. A hosted session's host (Triage) takes harnessEnv names and mount
|
||||
// sources only from these; launches don't read them. The host refuses every MOSAIC_* name from a
|
||||
// route, so MOSAIC_TOOLS, MOSAIC_EXTENSIONS and MOSAIC_THINKING are listed only to say what env()
|
||||
// hands a session: a hosted pi needs the host to set them itself (a later slice).
|
||||
export const envNames = Object.freeze(["PI_OFFLINE", "MOSAIC_TOOLS", "MOSAIC_EXTENSIONS", "PI_PROVIDER", "PI_MODEL", "MOSAIC_THINKING"]);
|
||||
export const installRoots = () => [join(REPO, "adapters", "pi"), join(REPO, "packages", "plugins", "harness", "pi"), realpathSync(join(REPO, "node_modules")), join(REPO, "tools", "unslop-hook")];
|
||||
// hands a session: a hosted pi needs the host to set them itself (a later slice). MOSAIC_PI_BIN
|
||||
// (#1873 S2) is the binary's path inside the session, which the host keeps from its own env() call;
|
||||
// MOSAIC_PI_NO_MCP (#1873 S3) is "1" when the binary's --help lists --no-mcp, else "0".
|
||||
// session-env.mjs holds the list beside the builder.
|
||||
export { envNames };
|
||||
export const installRoots = ({ env = {} } = {}) => [join(REPO, "adapters", "pi"), join(REPO, "packages", "plugins", "harness", "pi"), found(env).root, join(REPO, "tools", "unslop-hook")];
|
||||
|
||||
// #1873 harness watch W2: the stat key the host's 5-minute pass compares (src/stat-key.mjs): the
|
||||
// binary the lookup finds and the install roots. No binary found is a key too, so finding one
|
||||
// later changes it. The key holds paths and stays in memory.
|
||||
export function watchKey({ env = {} } = {}) {
|
||||
const at = locate(env);
|
||||
return statKey([at.bin ?? null, join(REPO, "adapters", "pi"), join(REPO, "packages", "plugins", "harness", "pi"), at.root ?? null, join(REPO, "tools", "unslop-hook")]);
|
||||
}
|
||||
|
||||
// The extension writes the turn marker at agent_end; an exit without it is a failed turn.
|
||||
export const turnEnd = () => "marker";
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
// The variables the pi module hands a session (index.mjs env() returns them as harnessEnv). Built
|
||||
// here, with no fit check, so packages/harness/src/pi-fit.mjs (#1873 S3) gives its runs the same
|
||||
// names as a session gets without importing index.mjs, which imports it.
|
||||
//
|
||||
// bin: the binary's path inside the session. noMcp: the binary's --help lists --no-mcp. tools: the
|
||||
// tool names. extensions: the -e files. session: { provider, model, thinking, proxyTransport }.
|
||||
// proxy: { key } when the session runs on the proxy, else null.
|
||||
export function harnessEnv({ bin, noMcp, tools, extensions, session, proxy = null }) {
|
||||
return {
|
||||
MOSAIC_PI_BIN: bin,
|
||||
// "1" when --help lists --no-mcp: the adapter then passes it, so MCP stays off by its own
|
||||
// flag. "0" otherwise, so every name in envNames is always set.
|
||||
MOSAIC_PI_NO_MCP: noMcp ? "1" : "0",
|
||||
PI_OFFLINE: "1",
|
||||
MOSAIC_TOOLS: tools.join(","),
|
||||
MOSAIC_EXTENSIONS: extensions.join(","),
|
||||
PI_PROVIDER: proxy ? session.proxyTransport.piProvider : session.provider ?? "",
|
||||
PI_MODEL: session.model,
|
||||
MOSAIC_THINKING: session.thinking,
|
||||
...(proxy ? { ANTHROPIC_API_KEY: proxy.key } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
// #1873 triage-fences: the names harnessEnv returns, less the provider key. index.mjs exports
|
||||
// them; its comment says what a hosted session's host does with them.
|
||||
export const envNames = Object.freeze(["MOSAIC_PI_BIN", "MOSAIC_PI_NO_MCP", "PI_OFFLINE", "MOSAIC_TOOLS", "MOSAIC_EXTENSIONS", "PI_PROVIDER", "PI_MODEL", "MOSAIC_THINKING"]);
|
||||
|
||||
// #1873 pi-unpin re-check 8: the provider and model the fit check's session-shaped runs pass
|
||||
// (packages/harness/src/pi-fit.mjs SESSION_SHAPE). The runs are offline with no key, so pi stops
|
||||
// before any model call; the names only need to be ones pi takes. Re-check 9: a Claude model is
|
||||
// offered, routed and tested only on the claude-code harness, so this names no Claude model and
|
||||
// no Anthropic provider (pi-fit.test.mjs holds that); glm-5.3-flash is the model index.mjs has
|
||||
// documented limits for, and pi's own catalogue lists it under zai.
|
||||
export const FIT_ROUTE = Object.freeze({ provider: "zai", model: "glm-5.3-flash" });
|
||||
@@ -1,6 +1,18 @@
|
||||
// Provider configuration and transport declarations owned by the proxy module.
|
||||
export const interfaceVersion = 1;
|
||||
export const ZAI_URL = 'https://api.z.ai/api/anthropic';
|
||||
// #1873 settings-backend-a3: the one list of providers an account can be added for. The bus proxy
|
||||
// page, the management client and the Console's provider select all read it; a provider not here
|
||||
// is refused. `id` is the value the page sends and an account row carries. An OAuth provider names
|
||||
// the host its sign-in address must be on; the key provider names the base URL its key group uses.
|
||||
// The setup status and the accounts read pass on only id, label and sign-in kind.
|
||||
export const PROVIDERS = Object.freeze([
|
||||
Object.freeze({ id: 'claude', label: 'Claude', signIn: 'oauth', signInHost: 'claude.ai' }),
|
||||
Object.freeze({ id: 'codex', label: 'ChatGPT / Codex', signIn: 'oauth', signInHost: 'auth.openai.com' }),
|
||||
Object.freeze({ id: 'z.ai', label: 'z.ai', signIn: 'apikey', baseUrl: ZAI_URL }),
|
||||
]);
|
||||
// What a page may see of the list: no URL, no host, nothing else.
|
||||
export const publicProviders = (list = PROVIDERS) => list.map(({ id, label, signIn }) => ({ id, label, signIn }));
|
||||
export const MODEL_ID = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,199}$/;
|
||||
const MODEL = MODEL_ID;
|
||||
const MAX_MODELS = 10000;
|
||||
|
||||
@@ -0,0 +1,277 @@
|
||||
// #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}$/;
|
||||
// A rule id may be a flag (W2: claude-code's fit rule names the flag --help lacks).
|
||||
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 slash, a dot, a dash, a tilde or a name's closing ">" (https://x/y, ~/x, <home>/x, a/b/c); one
|
||||
// after a colon is a path (host:/srv/a/b). W2 review P3-5: a file: URL and a ~/ path are paths too.
|
||||
const LOOSE_PATH = /(?<![\w/.~>-])\/[^\s/'"`<>()[\]{},;]+(?:\/[^\s'"`<>()[\]{},;]*)+/g;
|
||||
const FILE_URL = /(?<![A-Za-z0-9_])file:\/\/[^\s'"`<>()[\]{},;]*/gi;
|
||||
const TILDE_PATH = /(?<![\w/.~-])~[A-Za-z0-9._-]*\/[^\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.
|
||||
// signal (W2): when it aborts, the inspect running is aborted too (its own signal) and no further
|
||||
// module is asked; the result then carries stopped: true and holds only the records made before.
|
||||
export async function watchHarnesses({ last = [], env = process.env, scratch = null, dataRoot = null, timeoutMs = INSPECT_TIMEOUT_MS, now = () => new Date(), signal = null } = {}) {
|
||||
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) {
|
||||
if (signal?.aborted) break;
|
||||
const answer = await ask(module, { env, scratch, timeoutMs, signal });
|
||||
if (signal?.aborted) break;
|
||||
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));
|
||||
const stopped = Boolean(signal?.aborted);
|
||||
return { records, transitions, refused, gone: stopped ? [] : [...before.keys()].filter((n) => !seen.has(n)).sort(), ...(stopped ? { stopped } : {}) };
|
||||
}
|
||||
|
||||
// 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, signal = null }) {
|
||||
const via = typeof module.inspect === "function" ? "inspect" : "version";
|
||||
const stop = new AbortController();
|
||||
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" };
|
||||
}
|
||||
})();
|
||||
let timer = null, grace = null, cut = null;
|
||||
const late = new Promise((resolve) => {
|
||||
timer = setTimeout(() => { stop.abort(); resolve({ via, error: "timeout" }); }, timeoutMs);
|
||||
// A stop from the caller aborts the inspect, then waits for it to end (what it started is
|
||||
// stopped by then), up to STOP_GRACE_MS for one that ignores its signal.
|
||||
cut = () => {
|
||||
stop.abort();
|
||||
clearTimeout(timer);
|
||||
Promise.race([asked, new Promise((r) => { grace = setTimeout(r, STOP_GRACE_MS); })]).then(() => resolve({ via, error: "stopped" }));
|
||||
};
|
||||
if (signal?.aborted) cut();
|
||||
else signal?.addEventListener?.("abort", cut, { once: true });
|
||||
});
|
||||
try {
|
||||
return signal?.aborted ? await late : await Promise.race([asked, late]);
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
clearTimeout(grace);
|
||||
signal?.removeEventListener?.("abort", cut);
|
||||
}
|
||||
}
|
||||
const STOP_GRACE_MS = 5000;
|
||||
|
||||
// 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(FILE_URL, "<path>").replace(TILDE_PATH, "<path>").replace(LOOSE_PATH, "<path>");
|
||||
};
|
||||
}
|
||||
@@ -54,6 +54,7 @@ export const KINDS = Object.freeze({
|
||||
// each one's contract:
|
||||
// adapter() -> the adapter script's absolute path levels() -> the thinking levels it applies
|
||||
// preflight({ env }) -> null | { code, message } version({ env }) -> string | null
|
||||
// (either may answer with a promise of that; the launcher awaits it before it launches)
|
||||
// countsAccount() -> true when its launches spend launch.account
|
||||
// mounts({ env }) -> read-only binds [{ src, dest, why }]
|
||||
// builtinWeb() -> true when it serves harness-builtin web
|
||||
@@ -62,6 +63,8 @@ export const KINDS = Object.freeze({
|
||||
// gateTimeout() -> the gate hook's timeouts | null
|
||||
// bundle({ dir, files, session, write, quote }) -> { files, digests, sources }, its own bundle files
|
||||
// env({ files, policy, tools, session }) -> its adapter variables, written into session.json
|
||||
// (optional, outside the list) ready({ env }) -> a promise a host that calls env() without
|
||||
// preflight awaits first; it rejects when env() would refuse (pi's fit check)
|
||||
// turnEnd() -> "marker" (a turn ends only with the turn marker) | "exit"
|
||||
// outputPolicy({ proxy }) -> "adapter" | "non-newline-stdout" | "stdout"
|
||||
// transcript({ runDir, store }) -> the directory its transcripts land in, for usage
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// #1873 harness watch W2 (reports/harness-watch-design.md section 1, "How often"): the stat key of
|
||||
// a list of files and directories. Per path: its real path, dev, ino, size, mtime and ctime. The
|
||||
// real path catches a symlink swap, ctime a copy that keeps mtime. A path that can't be resolved or
|
||||
// stat'ed gives its error code in place, so a file that goes or comes back changes the key too.
|
||||
// The key is compared in memory only. It holds host paths, so it is never written to a file or a log.
|
||||
import { realpathSync, statSync } from "node:fs";
|
||||
|
||||
export function statKey(paths) {
|
||||
return JSON.stringify((Array.isArray(paths) ? paths : []).map((p) => {
|
||||
if (typeof p !== "string" || !p) return null;
|
||||
try {
|
||||
const real = realpathSync(p);
|
||||
const s = statSync(real);
|
||||
return [real, s.dev, s.ino, s.size, s.mtimeMs, s.ctimeMs];
|
||||
} catch (e) {
|
||||
return [p, typeof e?.code === "string" ? e.code : "error"];
|
||||
}
|
||||
}));
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// #1873 harness watch W2: a harness module asks the bus host's watch for a stat pass now, instead
|
||||
// of at the next five-minute tick. claude-code's preflight asks when it has no verdict for the
|
||||
// binary it found (a binary or adapter replaced, touched or moved since the last check, or a host
|
||||
// that just started). The host's watch (packages/cli/src/harness-watch-host.mjs) listens in the
|
||||
// same process; with no listener (a Console, a test, a launch outside a bus host) the request does
|
||||
// nothing. A request carries nothing: the watch compares its own stat key and runs a full pass
|
||||
// only when it changed, so a flood of requests costs one stat pass each and no extra full pass.
|
||||
const listeners = new Set();
|
||||
|
||||
// Calls fn on each request until the returned function is called.
|
||||
export function onWatchRequest(fn) {
|
||||
listeners.add(fn);
|
||||
return () => { listeners.delete(fn); };
|
||||
}
|
||||
|
||||
export function requestWatch() {
|
||||
for (const fn of [...listeners]) {
|
||||
try { fn(); } catch {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,398 @@
|
||||
// #1873 harness watch, slice W2 (reports/harness-watch-design.md sections 1 and 5): the claude-code
|
||||
// fit rule (every flag the adapter passes is in --help, and --version answers), the verdict
|
||||
// preflight reads, the no-network captures, the watch's outer signal and flag rule ids, and pi's
|
||||
// digest and undecided fields.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
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 { piFit } from "../../harness/src/pi-fit.mjs";
|
||||
import { sandboxProblem, UNSHARE } from "../../seat/src/sandbox.mjs";
|
||||
import * as pi from "../harness/pi/index.mjs";
|
||||
import { locate, printedVersion } from "../harness/pi/binary.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 CLAUDE = process.env.MOSAIC_CLAUDE_BIN;
|
||||
const CREDENTIAL = { ANTHROPIC_API_KEY: "fixture-api-key" };
|
||||
const DIGEST = /^[a-f0-9]{64}$/;
|
||||
|
||||
function scratch(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), "mosaic-watch-"));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
return root;
|
||||
}
|
||||
|
||||
// The help a claude that has every flag the adapter passes would print (a test fixture; the module
|
||||
// itself reads the flags from adapter.sh). --system-prompt-file shows only as --system-prompt[-file],
|
||||
// as on claude 2.1.322. Descriptions also mention flags (--settings, --mcp-config, --model), as
|
||||
// claude's do; only the option lines count.
|
||||
const HELP_LINES = [
|
||||
"Usage: claude [options] [command] [prompt]", "", "Options:",
|
||||
" -p, --print Print response and exit",
|
||||
" --output-format <format> text, json or stream-json",
|
||||
" --verbose Verbose output",
|
||||
" --include-partial-messages Partial message chunks",
|
||||
" --bare Minimal mode: no --settings files, no --mcp-config servers,",
|
||||
" --settings and --model still apply",
|
||||
" --system-prompt <prompt> System prompt; also --system-prompt[-file]",
|
||||
" --model <model> Model",
|
||||
" --effort <level> Effort",
|
||||
" --restricted Restricted mode",
|
||||
" --tools <tools...> Built-in tools",
|
||||
" --allowedTools, --allowed-tools <tools...> Allowed tools",
|
||||
" --permission-mode <mode> Permission mode",
|
||||
" --settings <file-or-json> Settings",
|
||||
" --strict-mcp-config Only --mcp-config servers",
|
||||
" --mcp-config <configs...> MCP servers",
|
||||
" --disable-slash-commands No skills",
|
||||
" -r, --resume [value] Resume a session",
|
||||
" --session-id <uuid> Session id",
|
||||
" -v, --version Version",
|
||||
" -h, --help Help",
|
||||
];
|
||||
// drop removes the option line that names a flag (its head, not a mention in a description).
|
||||
const help = ({ drop = [], extra = [] } = {}) => [...HELP_LINES.filter((l) => !drop.some((d) => new RegExp(`^ (?:-[A-Za-z], )?${d}(?=[ ,<\\[]|$)`).test(l))), ...extra].join("\n");
|
||||
|
||||
// A stand-in claude: --version prints version (or exits versionExit), --help prints text (or exits
|
||||
// helpExit, or sleeps with helpHang), anything else exits 3. With nsFile it writes the network
|
||||
// namespace it runs in there first.
|
||||
function standIn(dir, { name = "claude", version = "9.9.9 (stand-in)", versionExit = null, helpText = help(), helpExit = null, helpHang = false, nsFile = null } = {}) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
const bin = join(dir, name);
|
||||
writeFileSync(bin, [
|
||||
"#!/bin/sh",
|
||||
nsFile ? `readlink /proc/self/ns/net >> '${nsFile}'` : "",
|
||||
'case "$1" in',
|
||||
`--version) ${versionExit !== null ? `exit ${versionExit}` : `echo '${version}'`};;`,
|
||||
`--help) ${helpExit !== null ? `exit ${helpExit}` : helpHang ? "exec sleep 30" : `cat <<'EOF'\n${helpText}\nEOF\n`};;`,
|
||||
"*) exit 3;;",
|
||||
"esac", "",
|
||||
].join("\n"), { mode: 0o755 });
|
||||
return bin;
|
||||
}
|
||||
|
||||
test("W2 claude-code: a stand-in whose --help lacks an adapter flag is red, and preflight refuses harness-feature-missing", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const bin = standIn(join(dir, "bin"), { helpText: help({ drop: ["--strict-mcp-config"] }) });
|
||||
const env = { MOSAIC_CLAUDE_BIN: bin, PATH: "/usr/bin:/bin" };
|
||||
// Before any check, preflight has no verdict and does not refuse.
|
||||
assert.equal(claude.preflight({ env: { ...env, ...CREDENTIAL } }), null);
|
||||
const r = await claude.inspect({ env, scratch: scratch(t) });
|
||||
assert.deepEqual([r.found, r.fit, r.definite, r.failed, r.version], [true, "fail", true, ["--strict-mcp-config"], "9.9.9 (stand-in)"]);
|
||||
assert.match(r.line, /--help lacks --strict-mcp-config/);
|
||||
const refusal = claude.preflight({ env: { ...env, ...CREDENTIAL } });
|
||||
assert.equal(refusal?.code, "harness-feature-missing");
|
||||
assert.match(refusal.message, /MOSAIC_CLAUDE_BIN/);
|
||||
assert.match(refusal.message, /--strict-mcp-config/);
|
||||
assert.ok(!refusal.message.includes(dir), "the refusal names the variable, not the path");
|
||||
// The same file through PATH is the same verdict, named by its lookup step.
|
||||
const viaPath = claude.preflight({ env: { PATH: join(dir, "bin"), ...CREDENTIAL } });
|
||||
assert.equal(viaPath?.code, "harness-feature-missing");
|
||||
assert.match(viaPath.message, /the claude from PATH/);
|
||||
// The watch keeps the flag as the rule id (W1's rule ids now take a leading dash or two).
|
||||
const { watchHarnesses } = await import(pathToFileURL(WATCH_SRC).href);
|
||||
const out = await watchHarnesses({ env: { ...env, MOSAIC_PI_BIN: join(dir, "no-pi") }, scratch: scratch(t) });
|
||||
const rec = out.records.find((x) => x.source === "MOSAIC_CLAUDE_BIN");
|
||||
assert.deepEqual([rec.state, rec.failed], ["red", ["--strict-mcp-config"]]);
|
||||
// The binary replaced in place by one with every flag: its stat key changed, so the old verdict
|
||||
// no longer refuses, before any new check.
|
||||
standIn(join(dir, "bin"), { helpText: help() });
|
||||
assert.equal(claude.preflight({ env: { ...env, ...CREDENTIAL } }), null);
|
||||
const again = await claude.inspect({ env, scratch: scratch(t) });
|
||||
assert.deepEqual([again.fit, again.definite, again.failed], ["pass", true, []], again.line);
|
||||
assert.equal(claude.preflight({ env: { ...env, ...CREDENTIAL } }), null);
|
||||
});
|
||||
|
||||
test("W2 claude-code: --resume, which only a later turn passes, is checked too, and --x[-y] in help counts as --x-y", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(dir, { helpText: help({ drop: ["--resume"] }) }) }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.fit, r.failed], ["fail", ["--resume"]]);
|
||||
const prose = help({ drop: ["--system-prompt"], extra: [" --append-system-prompt <prompt> Append; see --system-prompt-files"] });
|
||||
const s = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(join(dir, "b"), { helpText: prose }) }, scratch: scratch(t) });
|
||||
assert.deepEqual([s.fit, s.failed], ["fail", ["--system-prompt-file"]], "a longer token or a token inside a word is not the flag");
|
||||
// Only option lines count; --x[-y] in prose gives --x-y only beside an --x option line.
|
||||
assert.deepEqual([...claude.helpFlags("use -p/--print or --system-prompt[-file]; non-interactive")], []);
|
||||
assert.deepEqual([...claude.helpFlags(" -c, --continue Continue\n --system-prompt <p> Prompt\n see --system-prompt[-file], --zz[-q]\n --settings in prose")].sort(), ["--continue", "--system-prompt", "--system-prompt-file", "-c"]);
|
||||
});
|
||||
|
||||
// Review P2-3: a flag a description mentions is not an option. A claude whose help lost the
|
||||
// --settings and --mcp-config option lines, though other descriptions still name both, is red.
|
||||
test("W2 claude-code: a flag only a description mentions does not count", async (t) => {
|
||||
const text = help({ drop: ["--settings", "--mcp-config"] });
|
||||
assert.match(text, /--settings/);
|
||||
assert.match(text, /--mcp-config/);
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(scratch(t), { helpText: text }) }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.fit, r.definite, [...r.failed].sort()], ["fail", true, ["--mcp-config", "--settings"]], r.line);
|
||||
});
|
||||
|
||||
test("W2 claude-code: the flags come from adapter.sh, not a list in the module", async (t) => {
|
||||
// A copy of the module beside an adapter that passes one more flag; the rest of the repository
|
||||
// is linked. The same stand-in passes with the real adapter and fails with the copy.
|
||||
const root = scratch(t);
|
||||
for (const p of ["packages/harness", "packages/seat", "packages/plugins/src", "tools", "node_modules"]) {
|
||||
mkdirSync(join(root, p, ".."), { recursive: true });
|
||||
symlinkSync(join(REPO, p), join(root, p));
|
||||
}
|
||||
mkdirSync(join(root, "packages", "plugins", "harness", "claude-code"), { recursive: true });
|
||||
cpSync(join(REPO, "packages", "plugins", "harness", "claude-code", "index.mjs"), join(root, "packages", "plugins", "harness", "claude-code", "index.mjs"));
|
||||
mkdirSync(join(root, "adapters", "claude"), { recursive: true });
|
||||
cpSync(join(REPO, "adapters", "claude", "stream.mjs"), join(root, "adapters", "claude", "stream.mjs"));
|
||||
const adapter = readFileSync(join(REPO, "adapters", "claude", "adapter.sh"), "utf8");
|
||||
assert.ok(adapter.includes(" --disable-slash-commands \\\n"));
|
||||
writeFileSync(join(root, "adapters", "claude", "adapter.sh"), adapter.replace(" --disable-slash-commands \\\n", " --disable-slash-commands \\\n --fit-probe-flag \\\n"), { mode: 0o755 });
|
||||
const copy = await import(pathToFileURL(join(root, "packages", "plugins", "harness", "claude-code", "index.mjs")).href);
|
||||
const bin = standIn(join(root, "bin"));
|
||||
const real = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t) });
|
||||
assert.deepEqual([real.fit, real.failed], ["pass", []], real.line);
|
||||
const changed = await copy.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t) });
|
||||
assert.deepEqual([changed.fit, changed.definite, changed.failed], ["fail", true, ["--fit-probe-flag"]], changed.line);
|
||||
});
|
||||
|
||||
test("W2 claude-code: --version or --help exiting non-zero is red; a check that is stopped is unknown and preflight does not refuse", async (t) => {
|
||||
const dir = scratch(t);
|
||||
let r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(join(dir, "v"), { versionExit: 2 }) }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.fit, r.definite, r.failed, r.version], ["fail", true, ["--version"], null]);
|
||||
assert.equal(claude.preflight({ env: { MOSAIC_CLAUDE_BIN: join(dir, "v", "claude"), ...CREDENTIAL } })?.code, "harness-feature-missing");
|
||||
r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(join(dir, "h"), { helpExit: 1 }) }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.fit, r.definite, r.failed], ["fail", true, ["--help"]]);
|
||||
const hang = standIn(join(dir, "s"), { helpHang: true });
|
||||
const stop = new AbortController();
|
||||
setTimeout(() => stop.abort(), 500);
|
||||
r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: hang }, scratch: scratch(t), signal: stop.signal });
|
||||
assert.deepEqual([r.fit, r.definite, r.failed], ["unknown", false, []]);
|
||||
assert.match(r.line, /--help was stopped; fit not checked/);
|
||||
assert.equal(claude.preflight({ env: { MOSAIC_CLAUDE_BIN: hang, ...CREDENTIAL } }), null, "an undecided check does not refuse in this slice");
|
||||
});
|
||||
|
||||
const unshareRuns = existsSync(UNSHARE) && spawnSync(UNSHARE, ["-r", "-n", "--", "/usr/bin/true"]).status === 0;
|
||||
test("W2 claude-code: --version and --help run in their own network namespace when unshare runs; without unshare they run and the line says so", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const host = readlinkSync("/proc/self/ns/net");
|
||||
const nsFile = join(dir, "ns");
|
||||
const bin = standIn(join(dir, "bin"), { nsFile });
|
||||
if (unshareRuns) {
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t) });
|
||||
assert.equal(r.fit, "pass", r.line);
|
||||
const seen = readFileSync(nsFile, "utf8").trim().split("\n");
|
||||
assert.equal(seen.length, 2);
|
||||
for (const ns of seen) assert.notEqual(ns, host, "no network: not the host's namespace");
|
||||
assert.doesNotMatch(r.line, /network/);
|
||||
} else t.diagnostic("unshare -r -n does not run here; only the fallback is checked");
|
||||
rmSync(nsFile, { force: true });
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t), unshare: join(dir, "no-unshare") });
|
||||
assert.equal(r.fit, "pass");
|
||||
assert.match(r.line, /run with the network on: unshare is missing$/);
|
||||
assert.deepEqual(readFileSync(nsFile, "utf8").trim().split("\n"), [host, host]);
|
||||
});
|
||||
|
||||
test("W2 the real claude named by MOSAIC_CLAUDE_BIN is fit, and preflight lets it through", CLAUDE ? {} : { skip: "MOSAIC_CLAUDE_BIN is not set" }, async (t) => {
|
||||
const env = { MOSAIC_CLAUDE_BIN: CLAUDE, PATH: "/usr/bin:/bin" };
|
||||
const r = await claude.inspect({ env, scratch: scratch(t) });
|
||||
assert.deepEqual([r.found, r.fit, r.definite, r.failed], [true, "pass", true, []], r.line);
|
||||
assert.equal(claude.preflight({ env: { ...env, ...CREDENTIAL } }), null);
|
||||
const { watchHarnesses } = await import(pathToFileURL(WATCH_SRC).href);
|
||||
const out = await watchHarnesses({ env: { ...env, MOSAIC_PI_BIN: "/nonexistent/pi" }, scratch: scratch(t) });
|
||||
assert.equal(out.records.find((x) => x.source === "MOSAIC_CLAUDE_BIN").state, "fit");
|
||||
});
|
||||
|
||||
test("W2 claude-code watchKey changes when the binary is replaced, and holds no answer of its own", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const bin = standIn(dir);
|
||||
const env = { MOSAIC_CLAUDE_BIN: bin };
|
||||
const a = claude.watchKey({ env });
|
||||
assert.equal(claude.watchKey({ env }), a);
|
||||
standIn(dir, { version: "9.9.10 (stand-in)" });
|
||||
assert.notEqual(claude.watchKey({ env }), a);
|
||||
assert.notEqual(claude.watchKey({ env: {} }), a);
|
||||
});
|
||||
|
||||
// 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("W2 a failed pi check carries the digest of the files it ran, and says it was definite", NO_BWRAP, async (t) => {
|
||||
const lacking = piPackage(t, { dropFlags: ["--no-skills"] });
|
||||
const found = locate(lacking.env);
|
||||
const fit = await piFit(found, { levels: pi.levels() });
|
||||
assert.deepEqual([fit.ok, fit.undecided], [false, false], fit.message);
|
||||
assert.match(fit.digest, DIGEST);
|
||||
const r = await pi.inspect({ env: lacking.env });
|
||||
assert.deepEqual([r.fit, r.definite, r.digest], ["fail", true, fit.digest]);
|
||||
// Another file under the package is another digest.
|
||||
writeFileSync(join(lacking.root, "prefix", "lib", "node_modules", PI_NAME, "dist", "bundle", "extra.js"), "// changed\n");
|
||||
const later = await piFit(found, { levels: pi.levels() });
|
||||
assert.match(later.digest, DIGEST);
|
||||
assert.notEqual(later.digest, fit.digest);
|
||||
});
|
||||
|
||||
test("W2 an undecided pi check is unknown from its undecided field; pi's inspect matches no message text", NO_BWRAP, async (t) => {
|
||||
const flood = piPackage(t, { flood: true });
|
||||
const fit = await piFit(locate(flood.env), { levels: pi.levels() });
|
||||
assert.deepEqual([fit.ok, fit.undecided], [false, true], fit.message);
|
||||
assert.match(fit.digest, DIGEST);
|
||||
const r = await pi.inspect({ env: flood.env });
|
||||
assert.deepEqual([r.fit, r.definite, r.digest], ["unknown", false, fit.digest]);
|
||||
const src = readFileSync(join(REPO, "packages", "plugins", "harness", "pi", "index.mjs"), "utf8");
|
||||
assert.doesNotMatch(src, /can't be checked/, "inspect reads fit.undecided, not the message");
|
||||
// A stopped check is undecided too, and its processes are gone with it.
|
||||
const slow = piPackage(t, { versionHang: true });
|
||||
const stop = new AbortController();
|
||||
setTimeout(() => stop.abort(), 300);
|
||||
const began = Date.now();
|
||||
const stopped = await pi.inspect({ env: slow.env, signal: stop.signal });
|
||||
assert.ok(Date.now() - began < 4000, "the stop ended the check, and its --version capture with it");
|
||||
assert.deepEqual([stopped.fit, stopped.definite], ["unknown", false], stopped.line);
|
||||
});
|
||||
|
||||
// A scratch plugin tree of stand-in harnesses: each inspect writes <MARKS>/<name> when it is
|
||||
// asked, then answers its entry of the JSON file WATCH_ANSWER names; "hang-signal" waits for its
|
||||
// signal and writes <MARKS>/<name>.aborted when it aborts; "slow-stop" ends 300 ms after its signal
|
||||
// aborts and writes <MARKS>/<name>.ended then.
|
||||
const marker = (name) => `import { readFileSync, writeFileSync } from "node:fs";
|
||||
export const interfaceVersion = ${KINDS.harness.interfaceVersion};
|
||||
${KINDS.harness.exports.map((e) => `export function ${e}() { return null; }`).join("\n")}
|
||||
export async function inspect({ env, signal }) {
|
||||
writeFileSync(env.MARKS + "/${name}", "1");
|
||||
const a = JSON.parse(readFileSync(env.WATCH_ANSWER, "utf8"))[${JSON.stringify(name)}];
|
||||
if (a === "hang-signal") return new Promise(() => { signal.addEventListener("abort", () => writeFileSync(env.MARKS + "/${name}.aborted", "1")); });
|
||||
if (a === "slow-stop") return new Promise((done) => { signal.addEventListener("abort", () => setTimeout(() => { writeFileSync(env.MARKS + "/${name}.ended", "1"); done({ found: true }); }, 300)); });
|
||||
return a;
|
||||
}
|
||||
`;
|
||||
async function markerTree(t, names) {
|
||||
const tr = tree(t);
|
||||
for (const name of names) tr.write("harness", name, marker(name));
|
||||
const { watchHarnesses } = await import(pathToFileURL(join(tr.pkg, "src", "harness-watch.mjs")).href);
|
||||
const dir = scratch(t);
|
||||
mkdirSync(join(dir, "marks"));
|
||||
return { watchHarnesses, dir, env: { WATCH_ANSWER: join(dir, "answer.json"), MARKS: join(dir, "marks") } };
|
||||
}
|
||||
|
||||
test("W2 the watch's signal aborts the inspect it is in and asks no further module", async (t) => {
|
||||
const w = await markerTree(t, ["a-first", "b-second"]);
|
||||
writeFileSync(w.env.WATCH_ANSWER, JSON.stringify({ "a-first": "hang-signal", "b-second": { found: true, fit: "pass", definite: true } }));
|
||||
const stop = new AbortController();
|
||||
setTimeout(() => stop.abort(), 200);
|
||||
const began = Date.now();
|
||||
const out = await w.watchHarnesses({ env: w.env, scratch: w.dir, signal: stop.signal, timeoutMs: 60000 });
|
||||
assert.ok(Date.now() - began < 10000);
|
||||
assert.equal(out.stopped, true);
|
||||
assert.deepEqual(out.records, []);
|
||||
assert.deepEqual(readdirSync(w.env.MARKS).sort(), ["a-first", "a-first.aborted"], "the running inspect saw the abort; the next was not asked");
|
||||
// An already aborted signal asks none.
|
||||
rmSync(w.env.MARKS, { recursive: true });
|
||||
mkdirSync(w.env.MARKS);
|
||||
const none = await w.watchHarnesses({ env: w.env, scratch: w.dir, signal: AbortSignal.abort() });
|
||||
assert.deepEqual([none.stopped, readdirSync(w.env.MARKS)], [true, []]);
|
||||
});
|
||||
|
||||
test("W2 the watch keeps flag rule ids and still drops ones that are not names", async (t) => {
|
||||
const w = await markerTree(t, ["flags"]);
|
||||
writeFileSync(w.env.WATCH_ANSWER, JSON.stringify({ flags: { found: true, fit: "fail", definite: true, failed: ["--strict-mcp-config", "-p", "C1", "---x", "--", "-", "--a b", "/tmp/x"] } }));
|
||||
const out = await w.watchHarnesses({ env: w.env, scratch: w.dir });
|
||||
assert.deepEqual(out.records[0].failed, ["--strict-mcp-config", "-p", "C1"]);
|
||||
assert.equal(out.records[0].state, "red");
|
||||
});
|
||||
|
||||
// Review P3-6: a stop waits for the inspect it aborted to end (up to the grace), so what it started
|
||||
// is gone when the watch returns.
|
||||
test("W2 a stopped watch returns after the inspect it aborted has ended", async (t) => {
|
||||
const w = await markerTree(t, ["slow"]);
|
||||
writeFileSync(w.env.WATCH_ANSWER, JSON.stringify({ slow: "slow-stop" }));
|
||||
const stop = new AbortController();
|
||||
setTimeout(() => stop.abort(), 100);
|
||||
const out = await w.watchHarnesses({ env: w.env, scratch: w.dir, signal: stop.signal, timeoutMs: 60000 });
|
||||
assert.equal(out.stopped, true);
|
||||
assert.ok(existsSync(join(w.env.MARKS, "slow.ended")), "the watch waited for the inspect to end");
|
||||
});
|
||||
|
||||
const alive = (pid) => { try { process.kill(pid, 0); return true; } catch { return false; } };
|
||||
const until = async (fn, ms = 3000) => { const end = Date.now() + ms; while (!fn() && Date.now() < end) await new Promise((r) => setTimeout(r, 25)); return fn(); };
|
||||
|
||||
// Review P3-6: a capture's whole process group goes, on a stop and when it ends: a stand-in that
|
||||
// leaves a child in the background leaves nothing.
|
||||
test("W2 claude-code: a capture stops what the binary left in the background, on a stop and when it ends", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const bin = join(dir, "claude");
|
||||
const left = join(dir, "left"), held = join(dir, "held");
|
||||
writeFileSync(bin, [
|
||||
"#!/bin/sh", 'case "$1" in',
|
||||
`--version) sleep 30 >/dev/null 2>&1 & echo $! > '${left}'; echo '9.9.9';;`,
|
||||
`--help) sleep 30 2>/dev/null & echo $! > '${held}' 2>/dev/null; wait;;`,
|
||||
"*) exit 3;;", "esac", "",
|
||||
].join("\n"), { mode: 0o755 });
|
||||
const stop = new AbortController();
|
||||
const r = claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t), signal: stop.signal });
|
||||
assert.ok(await until(() => existsSync(held) && readFileSync(held, "utf8").trim() !== "", 15000), "--help started");
|
||||
stop.abort();
|
||||
const out = await r;
|
||||
assert.match(out.line, /--help was stopped/);
|
||||
const pids = [left, held].map((f) => Number(readFileSync(f, "utf8").trim()));
|
||||
for (const pid of pids) assert.ok(await until(() => !alive(pid)), `process ${pid} left by a capture is gone`);
|
||||
});
|
||||
|
||||
// Re-check P3-3: pi's version capture kills its group on a stop too. The background child keeps
|
||||
// the stdout pipe open, so killing the parent alone would leave it running.
|
||||
test("W2 pi: a stopped version capture stops what the binary left in the background", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const bin = join(dir, "pi"), held = join(dir, "held");
|
||||
writeFileSync(bin, `#!/bin/sh\nsleep 30 2>/dev/null & echo $! > '${held}' 2>/dev/null\nwait\n`, { mode: 0o755 });
|
||||
const stop = new AbortController();
|
||||
const r = printedVersion(bin, { signal: stop.signal });
|
||||
assert.ok(await until(() => existsSync(held) && readFileSync(held, "utf8").trim() !== "", 15000), "--version started");
|
||||
stop.abort();
|
||||
assert.equal(await r, null);
|
||||
const pid = Number(readFileSync(held, "utf8").trim());
|
||||
assert.ok(await until(() => !alive(pid)), `process ${pid} left by the capture is gone`);
|
||||
});
|
||||
|
||||
// Review P3-6: an already aborted signal starts nothing and decides nothing.
|
||||
test("W2 claude-code: an inspect with an already aborted signal runs nothing and is unknown", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const nsFile = join(dir, "ran");
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(join(dir, "bin"), { nsFile }) }, scratch: scratch(t), signal: AbortSignal.abort() });
|
||||
assert.deepEqual([r.fit, r.definite], ["unknown", false], r.line);
|
||||
assert.equal(existsSync(nsFile), false, "the binary was not run");
|
||||
});
|
||||
|
||||
// Review P3-6: at most 16 verdicts are kept; the oldest goes first.
|
||||
test("W2 claude-code: preflight's verdicts are capped, oldest first", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const bins = [];
|
||||
for (let i = 0; i < 17; i++) bins.push(standIn(join(dir, `b${i}`), { helpText: help({ drop: ["--restricted"] }) }));
|
||||
for (const bin of bins) assert.equal((await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t) })).fit, "fail");
|
||||
const refused = (bin) => claude.preflight({ env: { MOSAIC_CLAUDE_BIN: bin, ...CREDENTIAL } })?.code ?? null;
|
||||
assert.equal(refused(bins[0]), null, "the first verdict was dropped");
|
||||
for (const bin of bins.slice(1)) assert.equal(refused(bin), "harness-feature-missing");
|
||||
});
|
||||
|
||||
// Review P3-6: pi's inspect decides from undecided alone, whatever the message says.
|
||||
test("W2 pi's inspect reads undecided, not the message", async (t) => {
|
||||
const stand = piPackage(t, {});
|
||||
const digest = "b".repeat(64);
|
||||
const run = (fit) => pi.inspect({ env: stand.env, check: async () => fit });
|
||||
let r = await run({ ok: false, message: "pi (MOSAIC_PI_BIN) can't be checked: words", digest, undecided: false });
|
||||
assert.deepEqual([r.fit, r.definite, r.digest], ["fail", true, digest]);
|
||||
r = await run({ ok: false, message: "pi (MOSAIC_PI_BIN) lacks --no-skills", digest, undecided: true });
|
||||
assert.deepEqual([r.fit, r.definite], ["unknown", false]);
|
||||
r = await run({ ok: false, message: "pi (MOSAIC_PI_BIN) lacks --no-skills" });
|
||||
assert.deepEqual([r.fit, r.definite, r.digest], ["unknown", false, null], "a check that gives no undecided field is undecided");
|
||||
});
|
||||
@@ -0,0 +1,420 @@
|
||||
// #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],
|
||||
// W2 review P3-5: a ~/ path, a file: URL and a path after a colon are paths now; a URL with a
|
||||
// host and a one-segment path stay.
|
||||
kept: ["see https://example.com/a/b, ~/x/y and /model", "see https://example.com/a/b, <path> and /model", dir],
|
||||
forms: ["file:///opt/fixture/bin host:/srv/fixture/b ~me/.local/x", "<path> host:<path> <path>", 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.version, r.line], [true, "fail", true, "2.0.0", "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-skills"]);
|
||||
// W2: a failure carries the digest of the files the check ran.
|
||||
assert.match(r.digest, /^[a-f0-9]{64}$/);
|
||||
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 });
|
||||
// W2: the fit rule runs, and the real claude passes it.
|
||||
assert.deepEqual([r.found, r.source, r.fit, r.definite], [true, "MOSAIC_CLAUDE_BIN", "pass", true], r.line);
|
||||
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], ["fit", 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();
|
||||
}
|
||||
@@ -3,13 +3,14 @@
|
||||
// harness outside the exceptions the brief lists.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, relative, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { createLoader } from "../src/loader.mjs";
|
||||
import { KINDS } from "../src/kinds.mjs";
|
||||
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
|
||||
|
||||
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
|
||||
|
||||
@@ -98,11 +99,11 @@ test("row 65 claude-code version asks MOSAIC_CLAUDE_BIN when set, else claude on
|
||||
assert.equal(claude.version({ env: { PATH: join(dir, "none") } }), null);
|
||||
});
|
||||
|
||||
test("row 65 pi preflight refuses missing credentials, and its version is the package pin", async () => {
|
||||
test("row 65 pi preflight refuses missing credentials, and its version is what the installed pi prints (#1873 S1)", async () => {
|
||||
const pi = await load("pi");
|
||||
assert.equal(pi.preflight({ env: {} }).code, "no-session-credential");
|
||||
const pkg = JSON.parse(readFileSync(join(REPO, "package.json"), "utf8"));
|
||||
assert.equal(pi.version({ env: {} }), pkg.dependencies?.["@earendil-works/pi-coding-agent"] ?? pkg.devDependencies?.["@earendil-works/pi-coding-agent"]);
|
||||
assert.equal((await pi.preflight({ env: {} })).code, "no-session-credential");
|
||||
const installed = JSON.parse(readFileSync(join(REPO, "node_modules", "@earendil-works", "pi-coding-agent", "package.json"), "utf8")).version;
|
||||
assert.equal(await pi.version({ env: {} }), installed);
|
||||
// Row 66: the unslop extension's files, bound read-only at their own paths.
|
||||
assert.deepEqual(pi.mounts({ env: {} }).map(m => m.dest ?? m.src), [join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), join(REPO, "node_modules"), ...["extension.ts", "unslop-check.js", "lists.json"].map(f => join(REPO, "tools", "unslop-hook", f))]);
|
||||
});
|
||||
@@ -110,6 +111,8 @@ test("row 65 pi preflight refuses missing credentials, and its version is the pa
|
||||
test("row 65 env passes the thinking level to both adapters, and claude-code's bundle writes its hook and MCP files", async t => {
|
||||
const pi = await load("pi"), claude = await load("claude-code");
|
||||
const session = { provider: "fixture", model: "m", thinking: "high", toolSocket: "/run/tools.sock" };
|
||||
// #1873 S3: env() reads the fit check ready() awaits for the found pi.
|
||||
await pi.ready();
|
||||
const piEnv = pi.env({ policy: { tools: ["read"] }, tools: [{ name: "list_tasks" }], session });
|
||||
assert.equal(piEnv.PI_OFFLINE, "1", "pi avoids startup network operations");
|
||||
assert.deepEqual([piEnv.MOSAIC_THINKING, piEnv.PI_MODEL, piEnv.MOSAIC_TOOLS], ["high", "m", "read,list_tasks"]);
|
||||
@@ -140,10 +143,16 @@ test("row 65 a route's thinking reaches pi as --thinking and claude as --effort,
|
||||
const pi = await load("pi"), claude = await load("claude-code");
|
||||
const dir = scratch(t);
|
||||
mkdirSync(join(dir, "bin"));
|
||||
for (const name of ["pi", "claude"]) {
|
||||
writeFileSync(join(dir, "bin", name), '#!/bin/sh\nprintf "%s\\n" "$@" > "$ARGV_OUT"\necho \'{"type":"result","subtype":"success","result":"ok"}\'\n');
|
||||
chmodSync(join(dir, "bin", name), 0o755);
|
||||
}
|
||||
// #1873 S2: pi's stand-in sits in a pi package, as the module requires, and is found on PATH.
|
||||
const piPackage = join(dir, "lib", "node_modules", "@earendil-works", "pi-coding-agent");
|
||||
mkdirSync(join(piPackage, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(piPackage, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "0.0.1", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
const result = '{"type":"result","subtype":"success","result":"ok"}\n';
|
||||
writeFileSync(join(dir, "bin", "claude"), `#!/bin/sh\nprintf "%s\\n" "$@" > "$ARGV_OUT"\necho '${result.trim()}'\n`);
|
||||
// #1873 S3: pi's stand-in also passes the fit check that ready() runs and env() reads.
|
||||
writeFileSync(join(piPackage, "dist", "bundle", "cli.js"), piStandIn({ sessionOut: result }));
|
||||
for (const file of [join(dir, "bin", "claude"), join(piPackage, "dist", "bundle", "cli.js")]) chmodSync(file, 0o755);
|
||||
symlinkSync(join(piPackage, "dist", "bundle", "cli.js"), join(dir, "bin", "pi"));
|
||||
for (const f of ["prompt.md", "settings.json", "mcp.json"]) writeFileSync(join(dir, f), "{}");
|
||||
const run = (adapter, moduleEnv) => {
|
||||
const out = join(dir, "argv");
|
||||
@@ -163,9 +172,10 @@ test("row 65 a route's thinking reaches pi as --thinking and claude as --effort,
|
||||
assert.equal(argv.filter(a => a === name).length, 1, argv.join(" "));
|
||||
return argv[argv.indexOf(name) + 1];
|
||||
};
|
||||
await pi.ready({ env: { PATH: `${join(dir, "bin")}:/usr/bin:/bin` } });
|
||||
for (const thinking of ["xhigh", "low"]) {
|
||||
const session = { provider: "fixture", model: "m", thinking, toolSocket: "/run/tools.sock" };
|
||||
assert.equal(flag(run("pi", pi.env({ policy: { tools: ["read"] }, tools: [], session })), "--thinking"), thinking);
|
||||
assert.equal(flag(run("pi", pi.env({ policy: { tools: ["read"] }, tools: [], session, env: { PATH: `${join(dir, "bin")}:/usr/bin:/bin` } })), "--thinking"), thinking);
|
||||
const files = { settings: join(dir, "settings.json"), mcp: join(dir, "mcp.json") };
|
||||
assert.equal(flag(run("claude", claude.env({ files, policy: { tools: ["read"], typed: [] }, session })), "--effort"), thinking);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
// #1873 pi-unpin (design section 5, S1): pi's labels come from the binary the module finds, not
|
||||
// from a pin. The version is what `<bin> --version` prints, or null when that fails; the
|
||||
// compaction count records the found package's own version.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { sandboxProblem } from "../../seat/src/sandbox.mjs";
|
||||
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
|
||||
|
||||
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
|
||||
const MODULE = join(REPO, "packages", "plugins", "harness", "pi");
|
||||
const NAME = "@earendil-works/pi-coding-agent";
|
||||
|
||||
function scratch(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), "mosaic-pi-bin-"));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
return root;
|
||||
}
|
||||
|
||||
// A pi package under `dir` (a node_modules directory), with `script` as its bin.
|
||||
function standIn(dir, script, version) {
|
||||
const pkg = join(dir, NAME);
|
||||
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: NAME, version, bin: { pi: "dist/bundle/cli.js" } }));
|
||||
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), script, { mode: 0o755 });
|
||||
return join(pkg, "dist", "bundle", "cli.js");
|
||||
}
|
||||
|
||||
// A copy of the pi module in a scratch repository whose node_modules holds a stand-in, so the
|
||||
// module's repository copy is that stand-in and not the real pi. The rest of packages/ links to
|
||||
// the real tree, for the modules the copy imports.
|
||||
async function copyRepo(t, script, version) {
|
||||
const root = scratch(t);
|
||||
const mod = join(root, "packages", "plugins", "harness", "pi");
|
||||
mkdirSync(mod, { recursive: true });
|
||||
for (const f of readdirSync(MODULE)) if (f.endsWith(".mjs")) copyFileSync(join(MODULE, f), join(mod, f));
|
||||
for (const p of readdirSync(join(REPO, "packages"))) if (p !== "plugins") symlinkSync(join(REPO, "packages", p), join(root, "packages", p));
|
||||
// The module imports packages/plugins/src (the stat key), so that links to the real tree too.
|
||||
symlinkSync(join(REPO, "packages", "plugins", "src"), join(root, "packages", "plugins", "src"));
|
||||
standIn(join(root, "node_modules"), script, version);
|
||||
mkdirSync(join(root, "node_modules", ".bin"));
|
||||
symlinkSync(`../${NAME}/dist/bundle/cli.js`, join(root, "node_modules", ".bin", "pi"));
|
||||
return { root, pi: await import(pathToFileURL(join(mod, "index.mjs")).href) };
|
||||
}
|
||||
|
||||
const emptyStore = (t) => {
|
||||
const root = scratch(t);
|
||||
mkdirSync(join(root, "session"));
|
||||
return { session: join(root, "session"), projects: join(root, "claude-projects") };
|
||||
};
|
||||
|
||||
test("pi-unpin S1: the version is what the found binary prints, and the compaction count records its package's version", async t => {
|
||||
const { pi } = await copyRepo(t, "#!/bin/sh\n[ \"$1\" = --version ] && echo 9.9.9\n", "9.9.4");
|
||||
assert.equal(await pi.version({ env: {} }), "9.9.9");
|
||||
const counted = pi.compactions({ store: emptyStore(t), cursors: {}, budget: 1 << 20, streams: 16, first: false });
|
||||
assert.equal(counted.version, "9.9.4");
|
||||
});
|
||||
|
||||
test("pi-unpin S1: a binary that exits 1 on --version gives a null version, not a guess", async t => {
|
||||
const { pi } = await copyRepo(t, "#!/bin/sh\nexit 1\n", "8.8.8");
|
||||
assert.equal(await pi.version({ env: {} }), null);
|
||||
// The package's own file still names its version for the compaction count.
|
||||
assert.equal(pi.compactions({ store: emptyStore(t), cursors: {}, budget: 1 << 20, streams: 16, first: false }).version, "8.8.8");
|
||||
});
|
||||
|
||||
test("pi-unpin S1: a version line with a control character or over 128 characters is not recorded", async t => {
|
||||
const { pi } = await copyRepo(t, "#!/bin/sh\nprintf '1.0\\033[31m\\n'\n", "1.0.0");
|
||||
assert.equal(await pi.version({ env: {} }), null);
|
||||
const long = await copyRepo(t, `#!/bin/sh\necho ${"9".repeat(129)}\n`, "1.0.0");
|
||||
assert.equal(await long.pi.version({ env: {} }), null);
|
||||
});
|
||||
|
||||
test("pi-unpin S1: the repository's real pi reports its installed package's version", async () => {
|
||||
const pi = await import(pathToFileURL(join(MODULE, "index.mjs")).href);
|
||||
const installed = JSON.parse(readFileSync(join(REPO, "node_modules", NAME, "package.json"), "utf8")).version;
|
||||
assert.equal(await pi.version({ env: {} }), installed);
|
||||
});
|
||||
|
||||
// #1873 pi-unpin (design section 5, S2): the binary comes from MOSAIC_PI_BIN, else the first pi on
|
||||
// PATH, else the repository's copy. Mounts, install roots and the session's MOSAIC_PI_BIN follow
|
||||
// the package that binary belongs to. A refusal names the variable, never a path.
|
||||
const real = await import(pathToFileURL(join(MODULE, "index.mjs")).href);
|
||||
const CREDENTIAL = { ZAI_API_KEY: "fixture-zai" };
|
||||
// #1873 S3: the stand-in passes the fit check, prints 7.7.7 for --version, and as a session
|
||||
// records its argv in $ARGV_OUT.
|
||||
const ARGV_SCRIPT = piStandIn({ version: "7.7.7" });
|
||||
|
||||
// npm's global layout: prefix/lib/node_modules/<package>, with prefix/bin/pi a link to its bin.
|
||||
function prefix(t, script = ARGV_SCRIPT, version = "7.7.0") {
|
||||
const root = join(scratch(t), "prefix");
|
||||
const cli = standIn(join(root, "lib", "node_modules"), script, version);
|
||||
mkdirSync(join(root, "bin"), { recursive: true });
|
||||
symlinkSync(`../lib/node_modules/${NAME}/dist/bundle/cli.js`, join(root, "bin", "pi"));
|
||||
return { root, bin: join(root, "bin", "pi"), cli: realpathSync(cli), pkg: realpathSync(join(root, "lib", "node_modules", NAME)) };
|
||||
}
|
||||
const session = { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" };
|
||||
// pi.env() reads the fit check that preflight awaited for the same files.
|
||||
const piEnv = (env) => real.env({ policy: { tools: ["read"] }, tools: [], session, env });
|
||||
const noPath = (problem, ...paths) => { for (const p of paths) assert.ok(!problem.message.includes(p), problem.message); };
|
||||
|
||||
test("pi-unpin S2: a stand-in pi in an npm prefix, named by MOSAIC_PI_BIN, is found, mounted alone and run from the mount", async t => {
|
||||
const p = prefix(t);
|
||||
const env = { ...CREDENTIAL, MOSAIC_PI_BIN: p.bin, PATH: "/usr/bin:/bin" };
|
||||
assert.equal(await real.preflight({ env }), null);
|
||||
assert.equal(await real.version({ env }), "7.7.7");
|
||||
const mounts = real.mounts({ env });
|
||||
assert.deepEqual(mounts.filter(m => (m.dest ?? m.src) === p.pkg || m.src === p.pkg).map(m => m.src), [p.pkg]);
|
||||
assert.ok(!mounts.some(m => (m.dest ?? m.src) === join(REPO, "node_modules")), "the repository's node_modules is not mounted");
|
||||
const roots = real.installRoots({ env });
|
||||
assert.ok(roots.includes(p.pkg));
|
||||
assert.ok(!roots.includes(realpathSync(join(REPO, "node_modules"))));
|
||||
const given = piEnv(env);
|
||||
assert.equal(given.MOSAIC_PI_BIN, p.cli, "the session runs the real file inside the mounted package");
|
||||
assert.ok(real.envNames.includes("MOSAIC_PI_BIN"));
|
||||
|
||||
// Run the real adapter in a sandbox that holds the system, the adapter and the module's mounts,
|
||||
// and not the repository's node_modules: the stand-in records the argv it was given.
|
||||
if (sandboxProblem()) return t.skip("no usable bwrap");
|
||||
const out = join(scratch(t), "out");
|
||||
mkdirSync(out);
|
||||
writeFileSync(join(out, "prompt.md"), "p");
|
||||
const argv = ["/usr/bin/bwrap", "--unshare-user", "--unshare-pid", "--unshare-net", "--die-with-parent", "--clearenv", "--ro-bind", "/usr", "/usr"];
|
||||
for (const link of ["/bin", "/sbin", "/lib", "/lib64"]) {
|
||||
if (!existsSync(link)) continue;
|
||||
if (lstatSync(link).isSymbolicLink()) argv.push("--symlink", readlinkSync(link), link);
|
||||
else argv.push("--ro-bind", link, link);
|
||||
}
|
||||
argv.push("--proc", "/proc", "--dev", "/dev", "--ro-bind", join(REPO, "adapters", "pi"), join(REPO, "adapters", "pi"));
|
||||
for (const m of mounts) argv.push("--ro-bind", m.src, m.dest ?? m.src);
|
||||
// The extensions the adapter checks for, which a launch's bundle binds.
|
||||
for (const e of given.MOSAIC_EXTENSIONS.split(",")) argv.push("--ro-bind", e, e);
|
||||
argv.push("--bind", out, out);
|
||||
for (const [k, v] of Object.entries({ ...given, PATH: "/usr/bin:/bin", ARGV_OUT: join(out, "argv"), MOSAIC_SYSTEM_PROMPT_FILE: join(out, "prompt.md"),
|
||||
MOSAIC_REQUEST: "x", MOSAIC_WORKSPACE: join(out, "ws"), MOSAIC_SESSION_DIR: join(out, "session"), MOSAIC_MODEL: "m" })) argv.push("--setenv", k, v);
|
||||
argv.push("/bin/sh", join(REPO, "adapters", "pi", "adapter.sh"));
|
||||
const r = spawnSync(argv[0], argv.slice(1), { encoding: "utf8", timeout: 20000 });
|
||||
assert.equal(r.status, 0, r.stderr);
|
||||
const ran = readFileSync(join(out, "argv"), "utf8").split("\n");
|
||||
assert.ok(ran.includes("--no-context-files") && ran.includes("--thinking"), ran.join(" "));
|
||||
});
|
||||
|
||||
test("pi-unpin S2: a relative or non-executable MOSAIC_PI_BIN is refused with harness-binary-missing, naming the variable only", async t => {
|
||||
const p = prefix(t);
|
||||
const plain = join(scratch(t), "pi");
|
||||
writeFileSync(plain, "#!/bin/sh\n", { mode: 0o644 });
|
||||
for (const bin of ["bin/pi", "pi", plain, join(p.root, "missing")]) {
|
||||
const problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: bin, PATH: `${join(p.root, "bin")}:/usr/bin:/bin` } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", "MOSAIC_PI_BIN isn't an executable file at an absolute path"], bin);
|
||||
noPath(problem, plain, p.root);
|
||||
assert.equal(await real.version({ env: { MOSAIC_PI_BIN: bin } }), null, "no fallback to another pi");
|
||||
}
|
||||
});
|
||||
|
||||
test("pi-unpin S2: with MOSAIC_PI_BIN unset the first pi on PATH is used, and the repository's own .bin on PATH does not count", async t => {
|
||||
const p = prefix(t);
|
||||
const env = { ...CREDENTIAL, PATH: `${join(REPO, "node_modules", ".bin")}:/nonexistent:relative:${join(p.root, "bin")}:/usr/bin:/bin` };
|
||||
assert.equal(await real.preflight({ env }), null);
|
||||
assert.equal(await real.version({ env }), "7.7.7");
|
||||
assert.equal(piEnv(env).MOSAIC_PI_BIN, p.cli);
|
||||
assert.ok(real.mounts({ env }).some(m => m.src === p.pkg));
|
||||
});
|
||||
|
||||
test("pi-unpin S2: with neither, the repository's copy is used and mounted as before", async () => {
|
||||
for (const PATH of ["/usr/bin:/bin", `${join(REPO, "node_modules", ".bin")}:/usr/bin:/bin`]) {
|
||||
const env = { ...CREDENTIAL, PATH };
|
||||
assert.equal(await real.preflight({ env }), null, PATH);
|
||||
const nm = realpathSync(join(REPO, "node_modules"));
|
||||
assert.deepEqual(real.mounts({ env }).filter(m => m.dest === join(REPO, "node_modules")).map(m => m.src), [nm]);
|
||||
assert.ok(real.installRoots({ env }).includes(nm));
|
||||
const bin = piEnv(env).MOSAIC_PI_BIN;
|
||||
assert.ok(bin.startsWith(join(REPO, "node_modules") + "/"), bin);
|
||||
assert.equal(realpathSync(bin), realpathSync(join(REPO, "node_modules", ".bin", "pi")));
|
||||
}
|
||||
});
|
||||
|
||||
test("pi-unpin S2: a pi that is not in a pi package is refused, and so is a pi that cannot be found at all", async t => {
|
||||
const dir = join(scratch(t), "bin");
|
||||
mkdirSync(dir);
|
||||
writeFileSync(join(dir, "pi"), "#!/bin/sh\necho 1.0.0\n");
|
||||
chmodSync(join(dir, "pi"), 0o755);
|
||||
let problem = await real.preflight({ env: { ...CREDENTIAL, PATH: `${dir}:/usr/bin:/bin` } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `the pi on PATH isn't part of the ${NAME} package`]);
|
||||
problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(dir, "pi") } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't part of the ${NAME} package`]);
|
||||
noPath(problem, dir);
|
||||
// The scratch copy has no node_modules of its own: no pi anywhere.
|
||||
const { root, pi } = await copyRepo(t, "#!/bin/sh\n", "1.0.0");
|
||||
rmSync(join(root, "node_modules"), { recursive: true });
|
||||
problem = await pi.preflight({ env: { ...CREDENTIAL, PATH: "/usr/bin:/bin" } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", "no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci"]);
|
||||
assert.throws(() => pi.env({ policy: { tools: [] }, tools: [], session, env: { PATH: "/usr/bin:/bin" } }), /no pi found/);
|
||||
});
|
||||
|
||||
// Review finding 8: the package directory is mounted whole, so the binary must be the pi bin its
|
||||
// package.json names. A stray executable under a directory whose package.json carries the name
|
||||
// (a wide tree, say) is refused, naming the variable only.
|
||||
test("pi-unpin S2 review: a binary that isn't the bin its package names is refused", async t => {
|
||||
const wide = scratch(t);
|
||||
writeFileSync(join(wide, "package.json"), JSON.stringify({ name: NAME, version: "1.0.0", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
mkdirSync(join(wide, "tools"));
|
||||
writeFileSync(join(wide, "tools", "pi"), "#!/bin/sh\necho 1.0.0\n", { mode: 0o755 });
|
||||
let problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(wide, "tools", "pi") } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't the pi bin the ${NAME} package names`]);
|
||||
noPath(problem, wide);
|
||||
// A package.json with no bin names none.
|
||||
writeFileSync(join(wide, "package.json"), JSON.stringify({ name: NAME, version: "1.0.0" }));
|
||||
problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(wide, "tools", "pi") } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't the pi bin the ${NAME} package names`]);
|
||||
// A bin given as a plain string names it too.
|
||||
const dir = join(scratch(t), "node_modules");
|
||||
const cli = standIn(dir, piStandIn({ version: "1.2.3" }), "1.2.3");
|
||||
writeFileSync(join(dir, NAME, "package.json"), JSON.stringify({ name: NAME, version: "1.2.3", bin: "dist/bundle/cli.js" }));
|
||||
assert.equal(await real.version({ env: { MOSAIC_PI_BIN: cli } }), "1.2.3");
|
||||
});
|
||||
@@ -48,20 +48,20 @@ test('P3 Claude preflight accepts a proxy key without a host provider credential
|
||||
|
||||
test('P3 pi refuses no-session-credential with no provider key', async () => {
|
||||
const pi = await harness('pi');
|
||||
assert.equal(pi.preflight({ env: {} })?.code, 'no-session-credential');
|
||||
assert.equal((await pi.preflight({ env: {} }))?.code, 'no-session-credential');
|
||||
});
|
||||
|
||||
test('P3 pi refuses no-session-credential with only a Claude OAuth token', async () => {
|
||||
const pi = await harness('pi');
|
||||
assert.equal(pi.preflight({ env: { CLAUDE_CODE_OAUTH_TOKEN: 'fx332-oauth' } })?.code, 'no-session-credential');
|
||||
assert.equal((await pi.preflight({ env: { CLAUDE_CODE_OAUTH_TOKEN: 'fx332-oauth' } }))?.code, 'no-session-credential');
|
||||
});
|
||||
|
||||
test('P3 pi accepts its own provider key or a session proxy key', async () => {
|
||||
const pi = await harness('pi');
|
||||
for (const name of ['ANTHROPIC_API_KEY', 'OPENAI_API_KEY', 'OPENROUTER_API_KEY', 'ZAI_API_KEY']) {
|
||||
assert.equal(pi.preflight({ env: { [name]: 'fx332-own' } }), null, name);
|
||||
assert.equal(await pi.preflight({ env: { [name]: 'fx332-own' } }), null, name);
|
||||
}
|
||||
assert.equal(pi.preflight({ env: {}, proxy: { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' } }), null);
|
||||
assert.equal(await pi.preflight({ env: {}, proxy: { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' } }), null);
|
||||
});
|
||||
|
||||
// Row 332 R2 (#1830): callers that only want pi's bundle sources call bundle() with no argument,
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
//
|
||||
// read-only /usr, /etc and the host's /bin, /lib, /lib64, /sbin links;
|
||||
// the repository files the runner loads; the adapter; for pi
|
||||
// the repository's node_modules, for Claude Code its pinned
|
||||
// the found package's directory (#1873), for Claude Code its
|
||||
// binary; node when it lives outside /usr; the broker socket,
|
||||
// the launch socket for the role that may launch, and the
|
||||
// host's git socket for a role holding git.push.working or
|
||||
@@ -41,7 +41,8 @@ import { ID_PATTERN } from "../../organization/src/vocabulary.mjs";
|
||||
|
||||
export const REPO = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..", "..");
|
||||
export const BWRAP = "/usr/bin/bwrap";
|
||||
const KILL_WITH_PARENT = ["/usr/bin/setpriv", "--pdeathsig", "KILL", "--"];
|
||||
export const SETPRIV = "/usr/bin/setpriv";
|
||||
const KILL_WITH_PARENT = [SETPRIV, "--pdeathsig", "KILL", "--"];
|
||||
|
||||
// What the runner imports, and the three files the harness starts from the
|
||||
// bundle's settings (claude-gate, mcp-server, pi-extension) with theirs.
|
||||
@@ -185,14 +186,61 @@ export function gitIdentity({ business, role }) {
|
||||
return { GIT_AUTHOR_NAME: role, GIT_AUTHOR_EMAIL: email, GIT_COMMITTER_NAME: role, GIT_COMMITTER_EMAIL: email };
|
||||
}
|
||||
|
||||
// The full command line for `command` inside the sandbox. HOME is the run's
|
||||
// home/, empty at the start; the harness makes what it needs there. The git
|
||||
// identity is the role's, for the business it runs in.
|
||||
export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWRAP }) {
|
||||
const identity = Object.entries(gitIdentity({ business, role })).flatMap(([k, v]) => ["--setenv", k, v]);
|
||||
return [bwrap, ...NAMESPACES, ...argsOf(list), "--remount-ro", "/", "--setenv", "HOME", join(runDir, "home"), "--setenv", "TMPDIR", "/tmp", ...identity, "--chdir", runDir, "--", ...KILL_WITH_PARENT, ...command];
|
||||
// The variables the sandbox sets for a session's runner, over the session env
|
||||
// the launcher hands it. HOME is the run's home/, empty at the start; the
|
||||
// harness makes what it needs there. TMPDIR is the private /tmp. The git
|
||||
// identity is the role's, for the business it runs in. #1873 S3: the pi fit
|
||||
// check gives its runs these too, so they get the names a session gets.
|
||||
export function sessionSetenv(runDir, { business, role }) {
|
||||
return { HOME: join(runDir, "home"), TMPDIR: "/tmp", ...gitIdentity({ business, role }) };
|
||||
}
|
||||
|
||||
// The full command line for `command` inside the sandbox, with sessionSetenv's variables.
|
||||
export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWRAP }) {
|
||||
const setenv = Object.entries(sessionSetenv(runDir, { business, role })).flatMap(([k, v]) => ["--setenv", k, v]);
|
||||
return [bwrap, ...NAMESPACES, ...argsOf(list), "--remount-ro", "/", ...setenv, "--chdir", runDir, "--", ...KILL_WITH_PARENT, ...command];
|
||||
}
|
||||
|
||||
// #1873 pi-unpin S3: the command line for a start-time check of a harness binary. It has the
|
||||
// system part every session gets, then `list`, with the network unshared, an empty environment
|
||||
// but `env`, and `chdir` as the working directory.
|
||||
//
|
||||
// #1873 re-check 9: a check dies with the process that runs it, unlike a session. It must not
|
||||
// outlive a bus that was killed. These steps make sure of that:
|
||||
// - setpriv gives the first process SIGKILL as its parent-death signal. A shell then checks
|
||||
// that its parent is still this process ($PPID is read after the signal is set). So either
|
||||
// the owner dies later and the signal kills the chain, or the owner is already gone and the
|
||||
// shell exits before anything starts.
|
||||
// - bwrap's --die-with-parent ties bwrap to the process before it.
|
||||
// - unshare, when this host lets it (the outer layer below), runs bwrap as pid 1 of a fresh pid
|
||||
// namespace, and --kill-child kills it when unshare dies. When pid 1 of a namespace dies, the
|
||||
// kernel kills every process in it. That includes bwrap's own child, wherever it is in its
|
||||
// setup. bwrap's --die-with-parent can't reach that child: bwrap sets the child's parent-death
|
||||
// signal only just before its exec, after the child waits for bwrap to write its uid map. If
|
||||
// bwrap dies before that write, the child waits for good.
|
||||
// The one step left open with the outer layer is unshare's own: if unshare dies after it forks
|
||||
// and before its child sets the signal, bwrap still runs. bwrap is alive then, so the run sets up
|
||||
// and goes on as normal, and it ends when the binary exits. It does not wait for good.
|
||||
//
|
||||
// #1873 re-check 10: the outer layer is best effort. unshare may be missing, older than
|
||||
// util-linux 2.36 (the first with --map-current-user), or refused: on Ubuntu 24.04 with
|
||||
// kernel.apparmor_restrict_unprivileged_userns=1 only bwrap has a profile that lets it make a
|
||||
// user namespace, so an unconfined unshare can't write its uid map. The fit check probes it once
|
||||
// per check (pi-fit.mjs) and passes unshare: null when it can't run, which drops the outer layer
|
||||
// and nothing else. That is not a weaker sandbox: the outer layer only bounds how long a check's
|
||||
// processes live. The sandbox a fit run gets is bwrap's, and bwrap's command line is the same
|
||||
// either way (pi-fit.test.mjs compares the two). Without the outer layer a stuck bwrap child can
|
||||
// outlive a killed owner; the next check stops it (sweepScratch in pi-fit.mjs).
|
||||
export const UNSHARE = "/usr/bin/unshare";
|
||||
const owned = () => [...KILL_WITH_PARENT, "/bin/sh", "-c", '[ "$PPID" = "$0" ] && exec "$@"', String(process.pid)];
|
||||
const bound = (unshare) => unshare ? [unshare, "--user", "--map-current-user", "--pid", "--fork", "--kill-child", "--"] : [];
|
||||
export function checkArgv(list, command, { env = {}, chdir, node = process.execPath, bwrap = BWRAP, unshare = UNSHARE }) {
|
||||
const setenv = Object.entries(env).flatMap(([k, v]) => ["--setenv", k, v]);
|
||||
return [...owned(), ...bound(unshare), bwrap, "--die-with-parent", ...NAMESPACES, "--unshare-net", ...argsOf([...systemBinds(node), ...list]), "--remount-ro", "/", "--clearenv", ...setenv, "--chdir", chdir, "--", ...KILL_WITH_PARENT, ...command];
|
||||
}
|
||||
// The outer layer alone, for its probe: the steps before bwrap in checkArgv, then command.
|
||||
export const outerArgv = (command, { unshare = UNSHARE } = {}) => [...owned(), ...bound(unshare), ...command];
|
||||
|
||||
// #1873 triage-fences: what a model route may hand a hosted session, checked before the host
|
||||
// writes anything and again in hostedBinds. The adapter and each mount's source must be a clean
|
||||
// absolute path whose real path lies under one of the harness module's install roots, named under
|
||||
|
||||
@@ -254,7 +254,7 @@ export const ENV_ALLOW = Object.freeze([
|
||||
"HOME", "USER", "LOGNAME", "LANG", "LC_ALL", "TZ", "TERM",
|
||||
"ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL", "CLAUDE_CODE_OAUTH_TOKEN",
|
||||
"OPENAI_API_KEY", "OPENROUTER_API_KEY", "ZAI_API_KEY",
|
||||
"MOSAIC_CLAUDE_BIN",
|
||||
"MOSAIC_CLAUDE_BIN", "MOSAIC_PI_BIN",
|
||||
]);
|
||||
|
||||
// The ways a Claude Code session can log in, each a set of variables that
|
||||
|
||||
@@ -129,7 +129,7 @@ test("row 65 a harness module's mounts bind read-only after the adapter, each wi
|
||||
const list = binds({ ...args, harnessMounts: pi.mounts({ env: {} }) });
|
||||
const at = (src) => list.findIndex((b) => b.src === src);
|
||||
const modules = list[at(realpathSync(join(REPO, "node_modules")))];
|
||||
assert.deepEqual(modules, { kind: "ro", src: realpathSync(join(REPO, "node_modules")), dest: join(REPO, "node_modules"), why: "pi and its packages, on the session's PATH" });
|
||||
assert.deepEqual(modules, { kind: "ro", src: realpathSync(join(REPO, "node_modules")), dest: join(REPO, "node_modules"), why: "pi and its packages (the repository's pi)" });
|
||||
assert.ok(at(join(root, "a.sh")) < at(modules.src) && at(modules.src) < at(join(root, "bus.sock")));
|
||||
const fixture = binds({ ...args, harnessMounts: [{ src: join(root, "tool"), why: "a fixture harness's tool" }] });
|
||||
assert.deepEqual(fixture.find((b) => b.src === join(root, "tool")), { kind: "ro", src: join(root, "tool"), why: "a fixture harness's tool" });
|
||||
|
||||
@@ -58,6 +58,15 @@ test("sessionEnv passes only the allowlist, the repo's bin on PATH, and the run
|
||||
assert.equal(sessionEnv({ MOSAIC_CLAUDE_BIN: "/opt/claude" }, "r0123456789ab").MOSAIC_CLAUDE_BIN, "/opt/claude");
|
||||
});
|
||||
|
||||
// #1873 pi-unpin S2: the host's MOSAIC_PI_BIN reaches the session, as MOSAIC_CLAUDE_BIN does, and
|
||||
// it is the only name the slice adds: every other MOSAIC_* and PI_* name from the host stays out.
|
||||
test("pi-unpin S2: sessionEnv passes MOSAIC_PI_BIN and nothing else new", () => {
|
||||
const env = sessionEnv({ MOSAIC_PI_BIN: "/opt/pi/bin/pi", PI_CODING_AGENT_DIR: "/h/.pi", PI_OFFLINE: "0", MOSAIC_PI_ROOT: "/x", PATH: "/usr/bin" }, "r0123456789ab");
|
||||
assert.deepEqual(Object.keys(env).sort(), ["MOSAIC_PI_BIN", "MOSAIC_RUN_ID", "PATH"]);
|
||||
assert.equal(env.MOSAIC_PI_BIN, "/opt/pi/bin/pi");
|
||||
assert.deepEqual(ENV_ALLOW.filter((k) => k.startsWith("MOSAIC_")), ["MOSAIC_CLAUDE_BIN", "MOSAIC_PI_BIN"]);
|
||||
});
|
||||
|
||||
test("ZAI_API_KEY passes the session allowlist, and ZAI_TOKEN does not", () => {
|
||||
assert.ok(ENV_ALLOW.includes("ZAI_API_KEY"));
|
||||
const env = sessionEnv({ ZAI_API_KEY: "fixture-zai", ZAI_TOKEN: "t" }, "r0123456789ab");
|
||||
|
||||
@@ -45,6 +45,8 @@ import { composeSnapshot, readSystemConfig, readOrgConfig } from './triage-snaps
|
||||
import { busesStopped } from '../../cli/src/proxy-admin-host.mjs';
|
||||
import { agentLevelsWriteRoutes } from './agent-levels-write.mjs';
|
||||
import { triageRouteRoutes } from './triage-route-http.mjs';
|
||||
import { keyState, ridingOrgs, triageKeyCall } from './triage-access.mjs';
|
||||
import { readKey as readTriageKey } from '../../bus/src/triage-key-file.mjs';
|
||||
import { RESTART_EXIT } from '../../cli/src/org-console.mjs';
|
||||
import { ID_PATTERN, systemOnly } from '../../organization/src/vocabulary.mjs';
|
||||
import { selectorScope } from '../../organization/src/selector.mjs';
|
||||
@@ -226,16 +228,18 @@ try {
|
||||
// (read only) and, through `call`, from its bus as the owner. A file that cannot be read gives
|
||||
// undefined fields, which Ride all names as unreadable. The change log is read with the writer's
|
||||
// own check (no symlink, a regular file this user owns, not group or world writable).
|
||||
const rideRiders = async () => {
|
||||
const rideRiders = async ({ timeoutMs = 5000, deadline } = {}) => {
|
||||
const writer = organizationWriter({ dir: configDir(), rolesDir: rolesDir() });
|
||||
const local = id => readRider({ id, read: () => writer.read(id).document, readLog: () => ownedText(writer.logFile(id), { mask: 0o022 }), isFixture: orgUsesFixture });
|
||||
// settings-backend A2: `file()` is the organization file as the writer reads it ({document, revision}),
|
||||
// read again on each call, for turning a program off for every organization.
|
||||
const local = id => ({ ...readRider({ id, read: () => writer.read(id).document, readLog: () => ownedText(writer.logFile(id), { mask: 0o022 }), isFixture: orgUsesFixture }), file: () => writer.read(id) });
|
||||
const down = async () => { throw Object.assign(new Error('the bus is not reachable'), { code: 'no-bus-host' }); };
|
||||
// The single-business Console has no bus status here: its organization never reads as stopped.
|
||||
if (!organizations) return ownerCall && options.business ? [{ ...local(options.business), call: ownerCall }] : [];
|
||||
const out = [];
|
||||
for (const org of await listOrgs({ configDir: configDir(), dataRoot: system.dataRoot, instance })) {
|
||||
if (org.state !== 'active') continue;
|
||||
let call = down; try { const paths = orgPaths(org.root); call = busCaller({ dataRoot: org.root, socket: paths.socket, business: org.id, timeoutMs: 5000 }); } catch {}
|
||||
let call = down; try { const paths = orgPaths(org.root); call = busCaller({ dataRoot: org.root, socket: paths.socket, business: org.id, timeoutMs, ...(deadline ? { deadline } : {}) }); } catch {}
|
||||
// The organization admin page's stopped test reads this bus status (its process id).
|
||||
out.push({ ...local(org.id), call, status: () => orgBusStatus({ configDir: configDir(), dataRoot: system.dataRoot, instance }, org.id) });
|
||||
}
|
||||
@@ -262,6 +266,8 @@ try {
|
||||
return out;
|
||||
},
|
||||
riders: rideRiders,
|
||||
// The root agents each organization takes count when a program is turned off (review P1-1).
|
||||
rootAgents: () => loadRootAgents(configDir(), { rolesDir: rolesDir() }),
|
||||
ownerModels,
|
||||
// A stopped fixture organization moves through the organization service's whole-file update, the
|
||||
// write `POST /api/orgs/ID/admin` makes, as the signed-in owner. The owner starts its bus after.
|
||||
@@ -292,6 +298,8 @@ try {
|
||||
const triage = triageRoute ? createTriageHost({
|
||||
dataRoot: system.dataRoot, route: triageRoute, proxyPort: triageProxyPort, tokenPattern: triageTokenPattern(configDir()),
|
||||
devOwner: Boolean(devOwner), log: line => console.log(line),
|
||||
// proxy-scope slice 4: a session on a key past its retireAt stops with triage-key-rotated.
|
||||
keyRecord: () => readTriageKey(configDir()),
|
||||
readSnapshot: () => composeSnapshot({
|
||||
orgs: () => listOrgs({ configDir: configDir(), dataRoot: system.dataRoot, instance }),
|
||||
busStatus: id => orgBusStatus({ configDir: configDir(), dataRoot: system.dataRoot, instance }, id),
|
||||
@@ -325,6 +333,22 @@ try {
|
||||
// next admit.
|
||||
const triageRoutes = system && (devOwner || ssoOn) ? [triageRouteRoutes({
|
||||
dataRoot: system.dataRoot, log: line => console.log(line),
|
||||
// proxy-scope slice 4: the Triage key through one riding organization's bus (proxy.triage.key),
|
||||
// as Proxy setup's init asks one bus. The Console reads the key file's state only; it never
|
||||
// loads the proxy management client or the key verbs' module.
|
||||
access: {
|
||||
state: () => keyState(configDir()),
|
||||
// The same list the key walk asks (cliproxy, not a fixture), read from local files only.
|
||||
// The owner's proxy must be set up (proxy.json names a port), as ownerModels reads it.
|
||||
riding: async () => {
|
||||
let proxy = false;
|
||||
try { proxy = Number.isInteger(loadProxy(configDir(), system.dataRoot)?.port); } catch {}
|
||||
return proxy && ridingOrgs(await rideRiders()).length > 0;
|
||||
},
|
||||
// One 30 s deadline over the whole walk, not 30 s for each bus.
|
||||
key: op => { const deadline = Date.now() + 30000; return triageKeyCall({ riders: () => rideRiders({ timeoutMs: 30000, deadline }), op }); },
|
||||
stop: () => (triage ? triage.stop() : Promise.resolve()),
|
||||
},
|
||||
catalogues: async () => {
|
||||
const read = async call => { try { return (await call('routes'))?.catalogue ?? { status: 'unknown' }; } catch { return { status: 'bus-unavailable' }; } };
|
||||
if (!organizations) return ownerCall ? [await read(ownerCall)] : [];
|
||||
|
||||
@@ -36,7 +36,28 @@ const ROUTES = Object.freeze({
|
||||
// #1873 ride-default: Ride all, and the confirmed move of one stopped fixture organization.
|
||||
'/api/proxy/admin/ride-all': ['POST', []],
|
||||
'/api/proxy/admin/ride-move': ['POST', ['org']],
|
||||
// #1873 settings-backend A1: reads for Model setup. The list names each read's query keys.
|
||||
'/api/proxy/admin/log': ['GET', ['lines']],
|
||||
'/api/proxy/admin/service-key': ['GET', []],
|
||||
'/api/proxy/admin/history': ['GET', ['limit', 'before']],
|
||||
// #1873 settings-backend A2: one harness program off for every organization, and each
|
||||
// organization's proxy client key health. The program list itself is in status.
|
||||
'/api/proxy/admin/programs/off': ['POST', ['name']],
|
||||
'/api/proxy/admin/access': ['GET', []],
|
||||
});
|
||||
// The A1 reads take a query; every other route ignores one, as before.
|
||||
const QUERY_ROUTES = new Set(['/api/proxy/admin/log', '/api/proxy/admin/service-key', '/api/proxy/admin/history']);
|
||||
// Each query key at most once, only the route's own keys, each a plain decimal number.
|
||||
function readQuery(req, allowed) {
|
||||
let params;
|
||||
try { params = new URL(req.url, 'http://localhost').searchParams; } catch { return null; }
|
||||
const out = {};
|
||||
for (const [key, value] of params) {
|
||||
if (!allowed.includes(key) || Object.hasOwn(out, key) || !/^[0-9]{1,15}$/.test(value)) return null;
|
||||
out[key] = Number(value);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
export const PROXY_ADMIN_PATHS = Object.freeze(Object.keys(ROUTES));
|
||||
const INIT_KEYS = ['generate', 'key', 'confirm', 'port', 'rotateGraceMinutes', 'firstOutputSeconds'];
|
||||
const json = (res, status, value) => {
|
||||
@@ -71,6 +92,8 @@ export async function proxyAdminHTTP(req, res, { path, admin, call, named, teste
|
||||
if (req.headersDistinct.origin?.length !== 1 || req.headers.origin !== origin) return done(403, { error: 'cross-origin request refused' });
|
||||
if (req.headersDistinct['sec-fetch-site']?.length !== 1 || req.headers['sec-fetch-site'] !== 'same-origin') return done(403, { error: 'cross-site request refused' });
|
||||
}
|
||||
let query = {};
|
||||
if (QUERY_ROUTES.has(path)) { query = readQuery(req, route[1]); if (!query) return refuse('invalid-request'); }
|
||||
if (!admin) return refuse('not-configured');
|
||||
let args = {};
|
||||
if (req.method === 'POST') {
|
||||
@@ -118,6 +141,13 @@ export async function proxyAdminHTTP(req, res, { path, admin, call, named, teste
|
||||
case '/api/proxy/admin/ride-move':
|
||||
if (typeof args.org !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(args.org)) return refuse('invalid-request');
|
||||
return done(200, await admin.rideMove({ org: args.org, principal }));
|
||||
case '/api/proxy/admin/log': return done(200, await admin.log(Object.hasOwn(query, 'lines') ? { lines: query.lines } : {}));
|
||||
case '/api/proxy/admin/service-key': return done(200, await admin.serviceKey());
|
||||
case '/api/proxy/admin/history': return done(200, await admin.history({ ...(Object.hasOwn(query, 'limit') ? { limit: query.limit } : {}), before: query.before ?? null }));
|
||||
case '/api/proxy/admin/programs/off':
|
||||
if (typeof args.name !== 'string' || !/^[a-z][a-z0-9-]{0,31}$/.test(args.name)) return refuse('invalid-request');
|
||||
return done(200, await admin.programsOff({ name: args.name, user: typeof user === 'string' ? user : null }));
|
||||
case '/api/proxy/admin/access': return done(200, await admin.access());
|
||||
}
|
||||
} catch (error) {
|
||||
const code = ['human-required', 'not-configured', 'invalid-request'].includes(error?.code) ? error.code : proxyCode(error, 'outcome-unknown');
|
||||
|
||||
@@ -18,7 +18,10 @@ const codes = ['not-configured', 'human-required', 'provider-not-allowed', 'logi
|
||||
const json = (res, status, value) => {
|
||||
res.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' }); res.end(JSON.stringify(value));
|
||||
};
|
||||
export async function proxyHTTP(req, res, { path, call, named, tester, granted, send, origin, user, log }) {
|
||||
// `record` is the proxy setup controller's recordAccount (settings-backend A1), or null where this
|
||||
// Console has no proxy setup: after a write's bus call answers, an account change is added to the
|
||||
// setup history. A record that fails leaves the bus answer as it is and adds recorded: false.
|
||||
export async function proxyHTTP(req, res, { path, call, named, tester, granted, send, origin, user, log, record = null }) {
|
||||
const route = routes[path];
|
||||
const done = (status, value) => {
|
||||
// No request body, query, login state, authorization URL or exception text.
|
||||
@@ -48,11 +51,19 @@ export async function proxyHTTP(req, res, { path, call, named, tester, granted,
|
||||
} catch { return done(400, { error: 'invalid-request' }); }
|
||||
}
|
||||
if (path === '/api/proxy/plugins/set' && (!args.plugins || typeof args.plugins !== 'object' || Array.isArray(args.plugins) || Object.keys(args.plugins).length !== 1 || !['harness', 'model-provider'].includes(Object.keys(args.plugins)[0]) || !Array.isArray(Object.values(args.plugins)[0]) || !Object.values(args.plugins)[0].every(name => typeof name === 'string'))) return done(400, { error: 'invalid-request' });
|
||||
try { return done(200, await call(route[1], args)); }
|
||||
let value;
|
||||
try { value = await call(route[1], args); }
|
||||
catch (error) {
|
||||
const code = codes.includes(error.code) || (path === '/api/proxy/plugins/set' && typeof error.code === 'string' && /^[a-z-]{1,64}$/.test(error.code)) ? error.code : 'outcome-unknown';
|
||||
// proxy-pickup (#1873): a bus with no proxy client names the cause in one plain line.
|
||||
const reason = code === 'not-configured' && typeof error.reason === 'string' ? { reason: error.reason.slice(0, 300) } : {};
|
||||
return done(code === 'human-required' ? 403 : ['not-configured', 'proxy-down', 'outcome-unknown'].includes(code) ? 503 : 400, { error: code, ...reason });
|
||||
}
|
||||
if (req.method === 'POST' && typeof record === 'function') {
|
||||
let recorded;
|
||||
try { recorded = record({ verb: route[1], args, result: value, user: user ?? null }); } catch { recorded = false; }
|
||||
// All four recorded verbs answer a plain object; any other answer is passed on unmarked.
|
||||
if (recorded === false && value && typeof value === 'object' && !Array.isArray(value)) value = { ...value, recorded: false };
|
||||
}
|
||||
return done(200, value);
|
||||
}
|
||||
@@ -703,7 +703,14 @@
|
||||
<h2 id="b-for-you">Open decisions <span class="count">${rows.length}</span></h2>${body}
|
||||
<p class="small muted">Open a decision for its seen, DM and closing history.</p>`;
|
||||
}
|
||||
const messageText = m => m.from_role !== 'human' && typeof m.body === 'string' && m.body.startsWith('The turn failed: ') ? `The assistant couldn't reply. Try again, or check Agents.<span class="source">Details: ${txt(m.body.slice('The turn failed: '.length))}</span>` : txt(m.body);
|
||||
// #1873 settings slice 5: a failed turn whose body carries its own "\nDetails: " part (chat.js
|
||||
// messageBody) shows its cause and that part once.
|
||||
const messageText = m => {
|
||||
if (m.from_role === 'human' || typeof m.body !== 'string' || !m.body.startsWith('The turn failed: ')) return txt(m.body);
|
||||
const rest = m.body.slice('The turn failed: '.length), at = rest.indexOf('\nDetails: ');
|
||||
if (at > 0) return `The assistant couldn't reply: ${txt(rest.slice(0, at))}<span class="source">Details: ${txt(rest.slice(at + '\nDetails: '.length))}</span>`;
|
||||
return `The assistant couldn't reply. Try again, or check Agents.<span class="source">Details: ${txt(rest)}</span>`;
|
||||
};
|
||||
async function messagesView(get, q) {
|
||||
const after = q.get('after');
|
||||
if (after !== null && !SAFE.test(after)) return notFound('The message cursor is not a message id.');
|
||||
@@ -1299,6 +1306,9 @@
|
||||
}
|
||||
const routesApplyMarkup=()=>routesApply?.length?`<ul class="routes-apply" aria-label="How the change reaches each agent">${routesApply.map(a=>`<li><b>${txt(a.name)}</b>: ${txt(a.text)}${a.restart&&canSend&&!isTester?` ${window.MosaicAgents?.restartMarkup(a.name,a.thread)??''}`:''}</li>`).join('')}</ul>`:'';
|
||||
let harnessSettings=null, harnessDraft=null, harnessLoading=false, harnessReceipt='', harnessBusy=false;
|
||||
// #1873 settings slice 5: the organization's one choice, Use your models (the cliproxy module in
|
||||
// its model-provider list, from the plugins read above) or its own model service (Coming later).
|
||||
let modelsReceipt='', modelsReceiptKind='', modelsBusy=false, modelsFocus=false;
|
||||
function readHarnesses(){
|
||||
if(harnessLoading || harnessSettings !== null || !canSend || isTester)return; harnessLoading=true;const epoch=orgEpoch;
|
||||
fetch('/api/proxy/plugins',{cache:'no-store',redirect:'manual'}).then(async r=>{const v=await r.json();if(epoch!==orgEpoch)return;if(!r.ok || !/^[a-f0-9]{64}$/.test(v.revision??'') || !Array.isArray(v.available?.harness) || !v.available.harness.every(n=>typeof n==='string') || !Array.isArray(v.plugins?.harness??[]))throw Error();harnessSettings=v;}).catch(()=>{if(epoch!==orgEpoch)return;harnessSettings=false;harnessReceipt='Harnesses could not be read.';}).finally(()=>{if(epoch!==orgEpoch)return;harnessLoading=false;if(routeOf().split('?')[0]==='/settings/routing')render(false);});
|
||||
@@ -1345,11 +1355,48 @@
|
||||
if(owner&&serviceState==='not-running')return `<p class="small warn" id="routes-catalogue">${lead} The model service is not running. ${setupLink('Open Model setup')}. ${thinking}</p>${words}`;
|
||||
if(owner&&(serviceLoading||(serviceState===null&&triageOwner)))return `<p class="small warn" id="routes-catalogue">${lead} Checking the model service. ${thinking}</p>`;
|
||||
if(owner&&serviceState===false)return `<div class="banner routes-catalogue" id="routes-catalogue" role="status"><p>${lead}${plain} The model service's status could not be read. ${thinking}</p>${words}<p class="actions">${recheck}</p></div>`;
|
||||
return `<p class="small warn" id="routes-catalogue">${lead}${plain} ${thinking}${owner?' A new model needs the model service: ask the instance owner to set it up, or press Stop using the model service when this organization does not need it.':''}</p>${words}`;
|
||||
return `<p class="small warn" id="routes-catalogue">${lead}${plain} ${thinking}${owner?' A new model needs the model service: ask the instance owner to set it up, or press Stop using your models when this organization does not need it.':''}</p>${words}`;
|
||||
}
|
||||
// Check again rereads Routing; the bus looks for the model service at that read. A draft is kept.
|
||||
document.addEventListener('click',e=>{if(e.target.id!=='routes-recheck')return;e.target.disabled=true;serviceState=null;routesView=null;last.delete('routes');render(true);});
|
||||
document.addEventListener('mosaic-org-restarted',e=>{if(e.detail?.id!==window.MosaicOrg?.id)return;serviceState=null;routesView=null;last.delete('routes');const path=routeOf().split('?')[0];if(path==='/settings/routing'||document.querySelector('[data-org-down]'))render(true);});
|
||||
// The choice reads only the plugins read: before it answers the radios wait, and a failed read
|
||||
// says so with Read again. Use your models writes at once; Stop using your models is the way back.
|
||||
// While the organization needs a restart, its banner's Restart is the one primary and the choice waits.
|
||||
// A Console with no organizations says "this business": lowercase inside a sentence, capitalized
|
||||
// at its start (cap).
|
||||
const modelsWho=cap=>window.MosaicOrg?(window.MosaicOrg.label||window.MosaicOrg.id):cap?'This business':'this business';
|
||||
const modelsWhose=cap=>`${modelsWho(cap)}'s`;
|
||||
// settings-s5 review P2-3: a choice that is not in force says when the model service behind it is
|
||||
// not set up or not answering (the owner's status read, as on the catalogue line). The choice
|
||||
// still writes: riding before setup is the design's "Nothing set up" state.
|
||||
function modelsService(){
|
||||
const link=text=>`<a href="#/settings/model-setup">${text}</a>`;
|
||||
return serviceState==='not-set-up'?`<p class="small warn" id="models-service">Your model service isn't set up. ${link('Set it up in Model setup')}.</p>`
|
||||
:serviceState==='not-running'?`<p class="small warn" id="models-service">Your model service isn't answering. ${link('Restart it in Model setup')}.</p>`
|
||||
:serviceState==='fenced'?`<p class="small warn" id="models-service">Your model service is waiting for a restart to finish. ${link('Open Model setup to finish it')}.</p>`:'';
|
||||
}
|
||||
function modelsChoice(restartFirst){
|
||||
const who=txt(modelsWho()), Who=txt(modelsWho(true)), whose=txt(modelsWhose()), Whose=txt(modelsWhose(true));
|
||||
const names=harnessSettings&&Array.isArray(harnessSettings.plugins?.['model-provider'])?harnessSettings.plugins['model-provider']:[];
|
||||
const read=!!harnessSettings, on=read&&names.includes('cliproxy'), wait=!read||modelsBusy||restartFirst||out();
|
||||
if(triageOwner)readService();
|
||||
const now=harnessSettings===false?`Which models ${who} uses could not be read.`:!read?`Reading ${whose} models…`:on?`Now: ${who} uses your models.`:`Now: ${who} does not use your models. Its agents use only the models in its own organization file.`;
|
||||
const change=!read?'':restartFirst?`<p class="small" id="models-wait">You can change ${whose} models after it restarts.</p>`
|
||||
:on?`<p class="small muted" id="models-change">Its agents can use the models your accounts offer. To switch back, press Stop using your models. A route on one of your models then stops working until you change it.</p>`
|
||||
:`<p class="small muted" id="models-change">Choose Use your models to switch. ${Whose} agents can then use the models your accounts offer. While your model service is down, a launch that needs it is refused. A running agent keeps its model until it starts again.</p>${modelsService()}`;
|
||||
// P2-1: while the organization needs a restart every control here waits. Stop is left out, as in
|
||||
// the mockup, since a disabled button here looks no different from an enabled one.
|
||||
const stop=on&&!restartFirst?`<button type="button" id="models-stop"${modelsBusy||out()?' disabled':''}>${modelsBusy?'<span class="settings-spinner" aria-hidden="true"></span> Saving…':'Stop using your models'}</button>`:'';
|
||||
const again=harnessSettings===false?'<button type="button" id="models-read">Read again</button>':'';
|
||||
// P3-1: Use your models has no button of its own, so its write shows Saving in the status line.
|
||||
const status=modelsBusy&&!on?'<span class="settings-spinner" aria-hidden="true"></span> Saving…':txt(modelsReceipt);
|
||||
return `<fieldset class="models-source" id="models-source"><legend>Where ${who} gets its models</legend>`
|
||||
+`<label class="choice"><input type="radio" name="models-source" id="models-yours" value="yours"${on?' checked':''}${wait?' disabled':''}><strong>Use your models</strong><span class="badge badge-ok">Recommended</span><span class="small muted">${Who} uses the model service and accounts in Model setup. Nothing to sign in to.</span></label>`
|
||||
+`<label class="choice off"><input type="radio" name="models-source" id="models-own" value="own" disabled><strong>Use its own model service</strong><span class="badge badge-muted">Coming later</span><span class="small muted">${Who} would run a second model service with its own accounts. Planned, not built yet, so you can't choose it today.</span></label>`
|
||||
+`<p id="models-in-force">${now}</p>${change}<p class="actions">${stop}${again}</p>`
|
||||
+`<p role="status" id="models-receipt" class="${modelsBusy&&!on?'':modelsReceiptKind}" tabindex="-1" data-focus-key="models-receipt">${status}</p></fieldset>`;
|
||||
}
|
||||
function routesSection(view,instanceInfo){
|
||||
const title='<h2 id="routes-title">Routing</h2>';
|
||||
readHarnesses();
|
||||
@@ -1381,7 +1428,7 @@
|
||||
// settings-p3 (re-check 3 P3-5): a ready catalogue the bus picked up late says agents start on these
|
||||
// models after a restart; that notice gets the organization's Restart (not primary: Save routes is).
|
||||
const catalogueInfo=v.catalogue?.status==='ready'?`<p class="small muted">Model catalogue, observed ${txt(window.MosaicLocalTime(v.catalogue.observedAt))}.</p>${(v.catalogue.notices??[]).map(n=>`<p class="small warn">${txt(n)}${n===window.MosaicConsole?.RESTART_TO_LAUNCH&&window.MosaicOrg?.id?` ${window.MosaicConsole.orgRestartButton(window.MosaicOrg.id,{name:window.MosaicOrg.label||window.MosaicOrg.id})}`:''}</p>`).join('')}`:v.catalogue?.status==='unavailable'?catalogueLine(proxyOn,v.catalogue):'<p class="small muted">Model catalogue not enabled.</p>';
|
||||
const providerControl=canSend&&!isTester?`<button type="button" id="provider-switch" data-enabled="${proxyOn}">${proxyOn?'Stop using the model service':'Use your models'}</button><p role="status" id="provider-switch-status"></p>`:'';
|
||||
const providerControl=canSend&&!isTester?modelsChoice(/ data-primary[ >]/.test(catalogueInfo)):'';
|
||||
const harnessControl=canSend&&!isTester?`<fieldset class="harness-control"><legend>Harnesses</legend>${harnessSettings ? [...new Set([...harnessSettings.available.harness,...(harnessSettings.plugins.harness??[])])].map(name=>`<label><input type="checkbox" data-harness="${esc(name)}"${(harnessDraft??harnessSettings.plugins.harness??[]).includes(name)?' checked':''}${harnessBusy?' disabled':''}> ${txt(name)}</label>`).join('')+` <button type="button" id="harness-save"${harnessBusy?' disabled':''}>Save harnesses</button>` : '<p>Harnesses unavailable.</p><button type="button" id="harness-read">Read harnesses again</button>'}<p role="status" id="harness-status">${txt(harnessReceipt)}</p></fieldset>`:'';
|
||||
const off=v.harnesses.length?'':'<p class="small warn">No harness is switched on for this organization, so every launch is refused, and so is a save that changes a harness. Switch one on under Harnesses.</p>';
|
||||
return `<section class="panel routing-panel" aria-labelledby="routes-title"><span class="sr-only">${title}</span>${catalogueInfo}${providerControl}${harnessControl}${off}<form id="routes-form"><div class="table-wrap" tabindex="0" role="region" aria-label="Routes, scroll for all columns"><table id="routes-table"><thead><tr><th scope="col" rowspan="2">Agent</th><th scope="colgroup" colspan="3">Primary</th><th scope="colgroup" colspan="3">Fallback</th></tr><tr>${ROUTE_CELLS.map(([,l])=>`<th scope="col">${l.replace('Fallback ','')}</th>`).join('')}</tr></thead><tbody>${rows}</tbody></table></div><p class="small muted">${canSend || isTester?'Changes are checked and logged with your identity.':'Saving requires signed-in send mode.'}</p><div class="settings-actions route-actions${routesDraft?' has-draft':''}"><button type="submit"${/ data-primary[ >]/.test(catalogueInfo)?' class="route-save-quiet"':''}${disabled}>${routesBusy?'<span class="settings-spinner" aria-hidden="true"></span> Saving…':'Save routes'}</button> <button type="button" id="routes-reload"${routesDraft?'':' hidden'}${routesBusy||!routesDraft?' disabled':''}>Discard draft</button><p role="status" id="routes-receipt" class="${routesReceipt.startsWith('Outcome unknown')?'banner ref':routesReceipt.includes('Draft kept')?'banner err':routesReceipt.includes('saved')?'settings-success':''}">${txt(routesReceipt)}</p></div>${routesApplyMarkup()}</form></section>`;
|
||||
@@ -1409,7 +1456,7 @@
|
||||
const moved=Object.entries(routes).flatMap(([name,r])=>{const was=routesView?.instances?.[name]??{};
|
||||
return [[r.harness,r.model,was.harness,was.model],[r.fallback?.harness,r.fallback?.model,was.fallback?.harness,was.fallback?.model]].filter(([h,m,wh,wm])=>m&&(h!==wh||m!==wm)).map(([,m])=>m);});
|
||||
const module=key=>routesView?.providerModels?.[key]?.module;
|
||||
return moved.length&&moved.every(key=>module(key)&&module(key)!=='cliproxy'&&names.includes(module(key)))?' This change does not need the model service: press Stop using the model service, then save again.':'';
|
||||
return moved.length&&moved.every(key=>module(key)&&module(key)!=='cliproxy'&&names.includes(module(key)))?' This change does not need your models: press Stop using your models, then save again.':'';
|
||||
}
|
||||
const routesAnswer=r=>/^[a-f0-9]{64}$/.test(r?.revision??'')&&Array.isArray(r.harnesses)&&Array.isArray(r.models)&&[r.levels,r.instances].every(o=>o&&typeof o==='object'&&!Array.isArray(o));
|
||||
for(const kind of ['input','change'])document.addEventListener(kind,e=>{const form=e.target?.closest?.('#routes-form');if(!form||!routesView)return;routesDraft=routesFields(form);routesEditVersion++;markSettingsDraft('routing',true);form.querySelector('.settings-actions')?.classList.add('has-draft');const discard=$('routes-reload');if(discard){discard.hidden=false;discard.disabled=false;}
|
||||
@@ -1429,17 +1476,32 @@
|
||||
catch{if(epoch!==orgEpoch)return;harnessReceipt='Outcome unknown. Read the harnesses again before trying.';harnessSettings=false;}
|
||||
finally{if(epoch===orgEpoch){harnessBusy=false;if(!out())render(false);}}
|
||||
});
|
||||
document.addEventListener('click',async e=>{if(e.target.id!=='provider-switch'||!canSend||isTester)return;
|
||||
const button=e.target,status=$('provider-switch-status');if(routesDraft){status.textContent='Save or discard the route draft first.';return;}button.disabled=true;const epoch=orgEpoch;
|
||||
try {const read=await fetch('/api/proxy/plugins',{cache:'no-store'});const settings=await read.json();if(epoch!==orgEpoch)return;if(!read.ok||!/^[a-f0-9]{64}$/.test(settings.revision??''))throw Error('Could not read provider settings.');
|
||||
const names=Array.isArray(settings.plugins?.['model-provider'])?settings.plugins['model-provider']:[];
|
||||
// The fresh read decides: a stale label never turns the proxy module on when it meant off.
|
||||
const on=names.includes('cliproxy');if(String(on)!==button.dataset.enabled){button.dataset.enabled=String(on);button.textContent=on?'Stop using the model service':'Use your models';button.disabled=false;status.textContent='The switch now shows the current setting. Press it again to change it.';return;}
|
||||
const next=on?names.filter(n=>n!=='cliproxy'):[...names,'cliproxy'];
|
||||
const response=await fetch('/api/proxy/plugins/set',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({revision:settings.revision,plugins:{'model-provider':next}}),cache:'no-store',redirect:'manual'});
|
||||
if(epoch!==orgEpoch)return;if(!response.ok)throw Error('Provider setting was refused.');harnessSettings=null;routesView=null;last.delete('routes');render(true);
|
||||
}catch(error){if(epoch!==orgEpoch)return;status.textContent=error.message;button.disabled=false;}
|
||||
});
|
||||
// The fresh read decides: a page that shows a stale choice is redrawn from it and nothing is written.
|
||||
async function setModels(want){
|
||||
if(!canSend||isTester||modelsBusy||!harnessSettings)return;
|
||||
const who=modelsWho(true), shown=(harnessSettings.plugins?.['model-provider']??[]).includes('cliproxy');
|
||||
let sent=false;
|
||||
if(routesDraft){modelsReceipt='Save or discard the route draft first.';modelsReceiptKind='banner err';modelsFocus=true;render(false);return;}
|
||||
modelsBusy=true;modelsReceipt='';modelsReceiptKind='';render(false);const epoch=orgEpoch;
|
||||
try{const read=await fetch('/api/proxy/plugins',{cache:'no-store',redirect:'manual'});const settings=await read.json();if(epoch!==orgEpoch)return;
|
||||
if(!read.ok||!/^[a-f0-9]{64}$/.test(settings.revision??'')||!Array.isArray(settings.available?.harness))throw Error('read');
|
||||
const names=Array.isArray(settings.plugins?.['model-provider'])?settings.plugins['model-provider']:[], on=names.includes('cliproxy');
|
||||
if(on!==shown){harnessSettings=settings;modelsReceipt=`${modelsWhose(true)} models changed since this page read it. The page now shows the current choice; choose again to change it.`;modelsReceiptKind='banner ref';return;}
|
||||
if(on===want)return;
|
||||
const next=want?[...names,'cliproxy']:names.filter(n=>n!=='cliproxy');
|
||||
sent=true;const response=await fetch('/api/proxy/plugins/set',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({revision:settings.revision,plugins:{'model-provider':next}}),cache:'no-store',redirect:'manual'});
|
||||
const result=await response.json().catch(()=>({}));if(epoch!==orgEpoch)return;
|
||||
if(!response.ok){modelsReceipt=`Not changed. ${result.message||result.error||'The change was refused.'}`;modelsReceiptKind='banner err';return;}
|
||||
modelsReceipt=want?`Saved. ${who} uses your models. An agent picks them up the next time it starts.`:`Saved. ${who} no longer uses your models. An agent on one of them keeps it until it starts again.`;modelsReceiptKind='settings-success';
|
||||
harnessSettings=null;routesView=null;last.delete('routes');window.MosaicModelDots?.read?.();
|
||||
// P3-2: a failed read before the write changed nothing and says so, with Read again.
|
||||
}catch{if(epoch!==orgEpoch)return;if(sent){modelsReceipt='Outcome unknown. Read the page again before trying.';modelsReceiptKind='banner ref';harnessSettings=null;}else{modelsReceipt='Not changed. Nothing was written.';modelsReceiptKind='banner err';harnessSettings=false;}}
|
||||
// P2-4: the render that shows the outcome moves focus to it (render() below).
|
||||
// Recheck N2: only while the page is still shown; a switch left mid-write does not take focus later.
|
||||
finally{if(epoch===orgEpoch){modelsBusy=false;modelsFocus=!!modelsReceipt&&routeOf().split('?')[0]==='/settings/routing';if(!out())render(harnessSettings===null);}}
|
||||
}
|
||||
document.addEventListener('change',e=>{if(e.target.id==='models-yours'&&e.target.checked)setModels(true);});
|
||||
document.addEventListener('click',e=>{if(e.target.closest?.('#models-stop'))setModels(false);else if(e.target.id==='models-read'){harnessSettings=null;harnessReceipt='';modelsReceipt='';modelsReceiptKind='';render(false);}});
|
||||
document.addEventListener('click',e=>{if(e.target.matches?.('.fallback-toggle'))e.target.closest('tr')?.classList.add('fallback-open');});
|
||||
document.addEventListener('click',e=>{if(e.target.id!=='routes-reload')return;routesDraft=null;routesView=null;routesReceipt='';routesApply=null;last.delete('routes');render(true);});
|
||||
document.addEventListener('submit',async e=>{
|
||||
@@ -1525,6 +1587,9 @@
|
||||
const notify = typeof n.binding==='string' ? `<code>${txt(n.binding)}</code>` : n.refused ? `Could not be read: <code>${txt(n.refused)}</code>` : '<span class="muted">None set</span>';
|
||||
const contents={limits:()=>launchForm(s.launch),routing:()=>routesSection(routes,instances),agents:()=>agentsSection(instances)+agentLevels,assistant:()=>assistantSection(s.launch,names),tokens:()=>tokenLimits(limitRows,limitsRefused)+tokenUsage(usage),names:()=>testerNames(names,instances),about:()=>`<section class="panel"><dl class="kv"><dt>Address</dt><dd><code>${txt(s.address)||'unknown'}</code></dd><dt>Release</dt><dd>${txt(s.release)||'unknown'}</dd><dt>Business</dt><dd>${txt(s.business)||none}</dd><dt>Board</dt><dd><code>${txt(s.board)||'unknown'}</code></dd><dt>Notifier binding</dt><dd>${notify}</dd></dl></section>`};
|
||||
const helpers={limits:'Leave a limit blank for no limit. Zero stops new launches. Running sessions keep running when limits or models change.',routing:"Each agent’s harness, model and thinking, and the fallback a launch takes when the primary harness fails its preflight. A fallback sets all three or none. Running sessions keep their route. The next launch reads the change. A harness is the program that runs the model. Preflight is the check a launch makes before it starts.",agents:'Each agent with its definition, launch membership and runs. A launch refuses until the agent has a route in Routing.',assistant:'The definition stays an assistant one. Holder and credentials stay hand edits.',tokens:'Token limits the bus checks, and token use read from session transcripts.'};
|
||||
// settings-s5 review P2-2: the header says where the models come from only from the plugins read.
|
||||
const riding=harnessSettings?(harnessSettings.plugins?.['model-provider']??[]).includes('cliproxy'):null;
|
||||
if(window.MosaicOrg&&!isTester)helpers.routing=`The models ${modelsWhose()} agents use.${riding===true?' They come from your accounts in Model setup.':riding===false?' They come from its own organization file.':''} ${helpers.routing}`;
|
||||
return settingsShell(page,contents[page](),`${get.oldest()?`Read ${when(get.oldest())}.`: 'Reading…'} ${helpers[page]||''}`);
|
||||
}
|
||||
let settingsFocusDisclosure=false;
|
||||
@@ -1911,6 +1976,11 @@
|
||||
if ((navigated || path !== '/settings/organizations') && orgCleanup) {orgCleanup();orgCleanup=null;}
|
||||
// Leaving a decision discards its confirmation immediately, including a preview in flight.
|
||||
if (answer && (navigated || path !== (answer.org ? '/inbox' : `/inbox/${answer.id}`))) answer = null;
|
||||
// Recheck N1: arriving at Models reads the model service again; a status read on an earlier visit may be out of date.
|
||||
if (navigated && path === '/settings/routing' && (typeof shown === 'string' ? shown.split('?')[0] : shown) !== path && !serviceLoading) serviceState = null;
|
||||
// settings-s5b: a focus request for the Models outcome line belongs to that page. Going anywhere else drops it,
|
||||
// including Assistant, Projects and the board, which return before the focus chain at the end of render.
|
||||
if (navigated && path !== '/settings/routing') modelsFocus = false;
|
||||
const g = ++gen, routeVersion = routesEditVersion;
|
||||
const current = () => g === gen && route === routeOf() && !out();
|
||||
// In organization mode #/ is the cross-organization dashboard; the processing controls stay on the board.
|
||||
@@ -1923,6 +1993,8 @@
|
||||
window.MosaicChat?.leave(chatPath);
|
||||
if (path === '/business') { location.replace('#/settings/business'); return; }
|
||||
if (path === '/settings/display') { location.replace('#/settings/appearance'); return; }
|
||||
// #1873 settings slice 5: Members is gone from the nav; its old addresses land on the organization's Agents page.
|
||||
if (path === '/settings/members') { location.replace(`#/settings/agents${window.MosaicOrg ? `?org=${enc(window.MosaicOrg.id)}` : ''}`); return; }
|
||||
// #1873 settings slice 4: the owner's page is Model setup; its old address lands there, query kept.
|
||||
if (path === '/settings/model-proxy') { location.replace(`#/settings/model-setup${query ? `?${query}` : ''}`); return; }
|
||||
// #1873 SP6: the Assistant page draws its own thread panes (assistant.js); every other page
|
||||
@@ -2046,7 +2118,11 @@
|
||||
sections(path);
|
||||
const h1 = $('bus-view').querySelector('h1');
|
||||
document.title = path === '/settings/business' && $('settings-page-title') ? `Settings · ${window.MosaicOrg ? 'Organization' : 'Business'} · Mosaic Console` : `${h1 ? h1.textContent : 'Console'} · Mosaic Console`;
|
||||
if (k) restore(k);
|
||||
// settings-s5 review P2-4: after a switch on Organization > Models, focus goes to its outcome line.
|
||||
// Recheck N2: a render that paints no outcome line drops the request instead of keeping it.
|
||||
const focusModels = modelsFocus && $('models-receipt'); modelsFocus = false;
|
||||
if (focusModels) { if (k) restore(k); focusModels.focus({ preventScroll: true }); }
|
||||
else if (k) restore(k);
|
||||
// Message on the Board (row 95) lands in the form it opened, addressed to the role.
|
||||
else if (focusSend && $('send-body')) { $('send-body').focus(); announce(`Message to ${draft.to}`); }
|
||||
// SP17: a step link on an organization's Workflows card lands in that step's title field.
|
||||
@@ -2261,6 +2337,7 @@
|
||||
instanceView = null; instanceDrafts = { add: null, change: null }; instanceReceipt = ''; instanceReceiptOp = null; instanceReceipts = Object.create(null);
|
||||
routesView = null; routesDraft = null; routesReceipt = ''; routesEditVersion++;
|
||||
harnessSettings = null; harnessDraft = null; harnessReceipt = '';
|
||||
modelsReceipt = ''; modelsReceiptKind = ''; modelsBusy = false; modelsFocus = false;
|
||||
serviceState = null; serviceLoading = false;
|
||||
// A file draft is keyed by project and path only; the page reload that used to drop it is gone.
|
||||
docKey = null; docStates.clear(); pkAll = []; pkShown = []; pkAt = 0;
|
||||
|
||||
@@ -593,7 +593,8 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
const service = '<div id="proxy-setup-next" class="proxy-setup-next" role="status" aria-live="polite"></div>'
|
||||
+ '<ol id="proxy-setup-progress" class="progress" aria-label="Setup progress" hidden></ol>'
|
||||
+ '<p id="proxy-setup-message" role="status" aria-live="polite"></p>'
|
||||
+ '<details id="proxy-setup-lines" hidden><summary>The service\'s last lines</summary><div class="inside"><p class="small muted">This page can\'t read the service\'s lines yet. What the service said last, when the host reported it:</p><pre id="proxy-setup-log" class="setup-log">Nothing reported.</pre></div></details>'
|
||||
// settings-s5b: the lines come from GET /api/proxy/admin/log, read when the box opens (readLog in the controller).
|
||||
+ '<details id="proxy-setup-lines" class="proxy-setup-lines" hidden><summary>The service\'s last lines</summary><div class="inside" data-log></div></details>'
|
||||
+ '<div id="proxy-setup-fixtures" class="banner mk-note" hidden></div>';
|
||||
const accounts = '<p id="proxy-message" role="status" aria-live="polite">Reading accounts…</p>'
|
||||
+ '<p id="proxy-readonly" class="muted" hidden>This Console is read-only. Sign-in and account changes are unavailable.</p>'
|
||||
@@ -602,13 +603,13 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
+ '<details id="proxy-account-order"><summary>Account order</summary><div class="inside"><p class="small muted">Higher priority is used first. Credential values are never shown.</p><ul id="proxy-order" class="rows proxy-order"></ul></div></details>'
|
||||
+ '<details id="proxy-offered"><summary>Models offered</summary><div class="inside" id="proxy-offered-list"></div></details>'
|
||||
+ '<details id="proxy-add-box"><summary>Add an account</summary><div class="inside">'
|
||||
+ '<form id="proxy-login-start" class="settings-form"><fieldset data-write disabled><div class="field"><label for="proxy-provider">Provider</label>'
|
||||
+ '<select id="proxy-provider" name="provider"><option value="claude">Claude (subscription)</option><option value="codex">ChatGPT / Codex (subscription)</option><option value="z.ai">z.ai (API key)</option></select>'
|
||||
+ '<p class="help small muted">Claude and ChatGPT / Codex: you sign in on the provider\'s site in a new tab. Nothing you type there comes to this page. z.ai: you paste an API key from your z.ai account.</p></div>'
|
||||
+ '<div class="form-foot"><button type="submit" id="proxy-login-go">Sign in to Claude</button></div></fieldset></form>'
|
||||
+ '<form id="proxy-apikey" class="settings-form" hidden><fieldset data-write disabled><div class="field"><label for="proxy-apikey-key" id="proxy-apikey-label">z.ai API key</label>'
|
||||
+ '<form id="proxy-login-start" class="settings-form"><fieldset data-write disabled><div class="field"><label for="proxy-provider" hidden>Provider</label>'
|
||||
// settings-backend-a3: the options and this help text come from the plugin's provider list (setProviders).
|
||||
+ '<select id="proxy-provider" name="provider" hidden></select><p id="proxy-provider-help" class="help small muted">Reading providers…</p></div>'
|
||||
+ '<div class="form-foot"><button type="submit" id="proxy-login-go" hidden>Sign in</button></div></fieldset></form>'
|
||||
+ '<form id="proxy-apikey" class="settings-form" hidden><fieldset data-write disabled><div class="field"><label for="proxy-apikey-key" id="proxy-apikey-label">API key</label>'
|
||||
+ '<input id="proxy-apikey-key" name="key" type="password" autocomplete="off" spellcheck="false" maxlength="4096" required>'
|
||||
+ '<p class="help small muted">Mosaic checks it with z.ai, keeps it in your model service, and never shows it again.</p></div>'
|
||||
+ '<p class="help small muted">Mosaic checks it with <span id="proxy-apikey-provider">the provider</span>, keeps it in your model service, and never shows it again.</p></div>'
|
||||
+ '<div class="form-foot"><button type="submit">Check and add</button><button type="button" id="proxy-apikey-cancel">Cancel</button></div></fieldset><p id="proxy-apikey-message" role="status"></p></form>'
|
||||
+ '</div></details>'
|
||||
+ '<div id="proxy-signin" hidden><ol id="proxy-signin-progress" class="progress" aria-label="Sign-in progress"></ol>'
|
||||
@@ -626,8 +627,12 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
+ '<ul id="proxy-ride-skips" class="proxy-ride-list"></ul><ul id="proxy-ride-fixtures" class="proxy-ride-list"></ul></div>';
|
||||
const triage = '<p><a id="proxy-setup-triage" class="btn" href="#/settings/triage">Set up Triage</a></p>';
|
||||
const advanced = '<details id="proxy-setup-advanced" data-admin hidden><summary>Advanced: programs, port, timeouts</summary><div class="inside">'
|
||||
+ '<div class="settings-form"><p class="lbl">Programs that drive models</p><ul id="proxy-setup-programs" class="model-list"></ul>'
|
||||
+ '<p class="help small muted">Each organization switches these on or off on its own Models page. Claude subscription models run only in Claude Code. Turning a program off for every organization is not built yet.</p></div>'
|
||||
// settings-s5c: the installed programs and which organizations have each one on, from the status
|
||||
// read; one program can be turned off in every organization that can do without it.
|
||||
+ '<div class="settings-form proxy-programs"><h4 id="proxy-setup-programs-title" class="lbl">Programs that drive models</h4>'
|
||||
+ '<p class="help small muted">Each organization turns these on or off on its own Models page. Claude subscription models run only in Claude Code. Turn off for every organization skips an organization that still uses the program or has no other program on.</p>'
|
||||
+ '<ul id="proxy-setup-programs" class="proxy-programs-list" aria-labelledby="proxy-setup-programs-title"><li class="muted">Not read yet.</li></ul>'
|
||||
+ '<div id="proxy-setup-programs-result" class="proxy-programs-result" tabindex="-1" hidden></div></div>'
|
||||
+ '<form id="proxy-setup-init" class="settings-form"><fieldset data-setup-write disabled><legend>Service key</legend>'
|
||||
+ '<p class="small muted">Mosaic makes the service key on this machine, keeps it where setup keeps keys and never shows it.</p></fieldset>'
|
||||
+ '<fieldset data-setup-write disabled><legend>Settings (leave empty for the defaults)</legend>'
|
||||
@@ -637,12 +642,24 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
+ '<dl id="proxy-setup-settings" class="proxy-setup-state" hidden></dl><p id="proxy-setup-settings-note" class="small muted" hidden>Changing these after setup is not built yet.</p></div></details>';
|
||||
const tech = '<details id="proxy-setup-tech" class="proxy-setup-tech"><summary>Technical details: release, service, history</summary><div class="inside">'
|
||||
+ '<div data-admin hidden><dl id="proxy-setup-state" class="proxy-setup-state"></dl>'
|
||||
// settings-s5b: the setup history and the service's lines, read when they open (owner only). The service key is the last
|
||||
// row of the list above (paintState), and the history follows it, as models-advanced shows them.
|
||||
+ '<div class="proxy-setup-history"><h4 id="proxy-setup-history-title">History</h4><p id="proxy-setup-history-message" class="small muted" role="status">Not read yet.</p>'
|
||||
+ '<ol id="proxy-setup-history" aria-labelledby="proxy-setup-history-title"></ol>'
|
||||
+ '<p class="proxy-setup-history-foot"><button type="button" id="proxy-setup-history-all" aria-expanded="false" aria-controls="proxy-setup-history" hidden>Show all</button>'
|
||||
+ '<button type="button" id="proxy-setup-history-older" hidden>Show older</button><button type="button" id="proxy-setup-history-retry" hidden>Read again</button></p></div>'
|
||||
// settings-s5c: access health per organization (GET /api/proxy/admin/access), read when this box opens.
|
||||
+ '<div class="proxy-setup-access"><h4 id="proxy-setup-access-title">Access</h4><p class="small muted">Whether each organization\'s agents can reach your models. You never see a key.</p>'
|
||||
+ '<p id="proxy-setup-access-message" class="small muted" role="status">Not read yet.</p>'
|
||||
+ '<ul id="proxy-setup-access" class="rows proxy-access" aria-labelledby="proxy-setup-access-title"></ul>'
|
||||
+ '<p class="proxy-setup-history-foot"><button type="button" id="proxy-setup-access-retry" hidden>Read again</button></p></div>'
|
||||
+ '<details id="proxy-setup-tech-lines" class="proxy-setup-lines"><summary>The service\'s last lines</summary><div class="inside" data-log></div></details>'
|
||||
+ '<div class="proxy-setup-unit"><h4>Service controls</h4><p class="small muted">Install renders the unit the same way as <code>mosaic proxy unit install</code>.</p>'
|
||||
+ '<div class="proxy-setup-buttons"><button type="button" data-unit="install" data-setup-write disabled>Install</button><button type="button" data-unit="start" data-setup-write disabled>Start</button><button type="button" data-unit="restart" data-setup-write disabled>Restart</button><button type="button" data-unit="stop" data-setup-write disabled>Stop</button><button type="button" id="proxy-setup-render">Show unit file</button></div>'
|
||||
+ '<pre id="proxy-setup-unit-file" hidden></pre></div>'
|
||||
+ '<div class="proxy-setup-orgs"><h4>Organizations</h4><div class="table-wrap" tabindex="0" role="region" aria-label="Organizations using the model service"><table><thead><tr><th>Organization</th><th>Proxy</th><th>Catalogue</th><th>Models</th></tr></thead><tbody id="proxy-setup-orgs"></tbody></table></div>'
|
||||
+ '<p id="proxy-setup-own" class="muted"></p></div>'
|
||||
+ '<div class="proxy-setup-receipts"><h4>History</h4><ol id="proxy-setup-receipts"><li class="muted">No setup actions on this page yet.</li></ol></div></div>'
|
||||
+ '</div>'
|
||||
+ '</div></details>';
|
||||
const markup = '<div id="proxy-setup" class="model-setup">'
|
||||
+ card('service', 1, 'Model service', service, true)
|
||||
@@ -670,7 +687,8 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
const cell = value => { const td = document.createElement('td'); td.textContent = value ?? '—'; return td; };
|
||||
const el = (tag, text, className) => { const e = document.createElement(tag); if (text !== undefined) e.textContent = text; if (className) e.className = className; return e; };
|
||||
// The provider select is this page's one list of providers: rows and buttons take their names
|
||||
// and kinds (subscription or API key) from its option labels.
|
||||
// and kinds (subscription or API key) from its option labels. Its options come from the plugin's
|
||||
// list (setProviders), read with the setup status for the instance owner, with the accounts for anyone else.
|
||||
const providerSelect = $('proxy-provider');
|
||||
const providerLabel = p => [...providerSelect.options].find(o => o.value === p)?.textContent ?? String(p ?? 'Unknown');
|
||||
const providerShort = p => providerLabel(p).replace(/\s*\([^)]*\)$/, '');
|
||||
@@ -784,6 +802,7 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
}
|
||||
async function refresh() {
|
||||
const accounts = await call('accounts');
|
||||
if (!proxyAdmin) setProviders(accounts?.providers);
|
||||
accountList = Array.isArray(accounts?.accounts) ? accounts.accounts : [];
|
||||
accountsDown = null;
|
||||
paintAccounts(); paintOrder();
|
||||
@@ -830,9 +849,28 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
$('proxy-offered').addEventListener('toggle', () => { if ($('proxy-offered').open) readOffered(); });
|
||||
// Add an account: the provider picks a sign-in or an API key.
|
||||
function paintProvider() {
|
||||
const p = providerSelect.value, apikey = keyed(p);
|
||||
const go = $('proxy-login-go'); go.textContent = `Sign in to ${providerShort(p)}`; go.hidden = apikey;
|
||||
const p = providerSelect.value, apikey = Boolean(p) && keyed(p);
|
||||
const go = $('proxy-login-go'); go.textContent = `Sign in to ${providerShort(p)}`; go.hidden = apikey || !p;
|
||||
$('proxy-apikey').hidden = !apikey; $('proxy-apikey-label').textContent = `${providerShort(p)} API key`;
|
||||
$('proxy-apikey-provider').textContent = providerShort(p);
|
||||
}
|
||||
// settings-backend-a3: no list, or an empty one, leaves no select and one line saying so.
|
||||
let providerKey = null;
|
||||
function setProviders(list) {
|
||||
const items = Array.isArray(list) ? list.filter(p => typeof p?.id === 'string' && p.id && typeof p.label === 'string' && ['oauth', 'apikey'].includes(p.signIn)) : [];
|
||||
const key = JSON.stringify(items.map(p => [p.id, p.label, p.signIn]));
|
||||
if (key === providerKey) return; providerKey = key;
|
||||
const kept = providerSelect.value;
|
||||
providerSelect.replaceChildren(...items.map(p => el('option', `${p.label} (${p.signIn === 'oauth' ? 'subscription' : 'API key'})`)));
|
||||
items.forEach((p, i) => { providerSelect.options[i].value = p.id; });
|
||||
if (items.some(p => p.id === kept)) providerSelect.value = kept;
|
||||
providerSelect.hidden = !items.length; for (const label of providerSelect.labels) label.hidden = !items.length;
|
||||
const oauth = items.filter(p => p.signIn === 'oauth').map(p => p.label);
|
||||
$('proxy-provider-help').textContent = !items.length ? 'The providers could not be read, so no account can be added here.'
|
||||
: [oauth.length ? `${new Intl.ListFormat('en').format(oauth)}: you sign in on the provider's site in a new tab. Nothing you type there comes to this page.` : '',
|
||||
...items.filter(p => p.signIn === 'apikey').map(p => `${p.label}: you paste an API key from your ${p.label} account.`)].filter(Boolean).join(' ');
|
||||
paintProvider();
|
||||
if (accountList) { paintAccounts(); paintOrder(); }
|
||||
}
|
||||
providerSelect.addEventListener('change', paintProvider);
|
||||
function paintSignin() {
|
||||
@@ -945,7 +983,7 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
'not-configured': 'Model service setup is not available on this Console.',
|
||||
'outcome-unknown': 'The outcome is unknown. Check the status before retrying.',
|
||||
};
|
||||
const explain = code => `${REASONS[code] ?? 'The action was refused.'} (${code})`;
|
||||
const explain = code => `${Object.hasOwn(REASONS, code) ? REASONS[code] : 'The action was refused.'} (${code})`;
|
||||
async function admin(path, args) {
|
||||
const response = await fetch('/api/proxy/admin/' + path, { cache: 'no-store', credentials: 'same-origin', redirect: 'manual',
|
||||
method: args === undefined ? 'GET' : 'POST',
|
||||
@@ -957,29 +995,174 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
}
|
||||
const say = (text, kind) => receipt('proxy-setup-message', text, kind);
|
||||
const human = value => value === null || value === undefined ? '—' : typeof value === 'object' ? JSON.stringify(value) : String(value);
|
||||
function addReceipt(record) {
|
||||
if (!record || typeof record !== 'object') return;
|
||||
const list = $('proxy-setup-receipts'); list.querySelector('li.muted')?.remove();
|
||||
const li = document.createElement('li'), op = document.createElement('strong'); op.textContent = record.op ?? 'action';
|
||||
const details = Object.entries(record).filter(([k]) => k !== 'op' && k !== 'time').map(([k, v]) => `${k}: ${human(v)}`).join(', ');
|
||||
li.append(op, ` ${window.MosaicLocalTime(record.time)}${details ? ' — ' + details : ''}`);
|
||||
list.prepend(li);
|
||||
// settings-s5b: History, the service key and the service's lines come from their own reads
|
||||
// (GET /api/proxy/admin/history, service-key and log). Nothing reads them until their box opens,
|
||||
// and only the instance owner's page starts this controller. A write's receipt reads them again
|
||||
// when Technical details is open, so the page never shows a raw receipt.
|
||||
const techOpen = () => $('proxy-setup-tech')?.open === true;
|
||||
function addReceipt() { if (enabled && techOpen()) { readKey(); readHistory(true); } }
|
||||
const plural = (n, word) => `${n} ${word}${n === 1 ? '' : 's'}`;
|
||||
const count = n => Number.isInteger(n) && n > 0 ? n : 0;
|
||||
// A failed read says what the answer's code means here; a request that got no answer says so. A code
|
||||
// shaped like a Mosaic key is not shown, even though it passes admin()'s letters-and-hyphens check.
|
||||
const failText = (error, words) => !error?.code ? (error instanceof TypeError ? 'The Console did not answer.' : String(error?.message ?? 'The Console did not answer.'))
|
||||
: /ms[kotp][-_]/i.test(error.code) ? 'The Console refused the read.' : Object.hasOwn(words, error.code) ? `${words[error.code]} (${error.code})` : error.message;
|
||||
const retryButton = (id, action) => { const button = el('button', 'Read again'); button.type = 'button'; if (id) button.id = id; button.addEventListener('click', action); return button; };
|
||||
// The service's last lines: one read paints every box. The lines show as the answer has them:
|
||||
// the route removes key-shaped text; this page adds nothing and takes nothing away.
|
||||
const LOG_WORDS = { 'not-configured': 'The model service is not installed, so it has no lines yet.', 'outcome-unknown': 'The host could not read the service\'s lines.', 'invalid-request': 'The Console refused the request.' };
|
||||
let logRound = 0, logAnswer = null;
|
||||
function paintLog() {
|
||||
for (const box of root.querySelectorAll('[data-log]')) {
|
||||
if (!logAnswer) { box.replaceChildren(el('p', 'Reading the service\'s lines…', 'small muted')); continue; }
|
||||
// An uninstalled service has no lines: a plain line, not a fault.
|
||||
if (logAnswer.none) { box.replaceChildren(el('p', LOG_WORDS['not-configured'], 'small muted'), retryButton(null, readLog)); continue; }
|
||||
if (logAnswer.error) { box.replaceChildren(el('p', `The service's lines could not be read. ${logAnswer.error}`, 'banner err'), retryButton(null, readLog)); continue; }
|
||||
const { lines, redacted, dropped } = logAnswer, notes = ['Mosaic removes anything that looks like a key.'];
|
||||
if (redacted) notes.push(`It removed something from ${plural(redacted, 'line')}.`);
|
||||
if (dropped) notes.push(`${plural(dropped, 'line')} ${dropped === 1 ? 'was' : 'were'} left out.`);
|
||||
const parts = [el('p', notes.join(' '), 'small muted')];
|
||||
parts.push(lines.length ? el('pre', lines.join('\n'), 'setup-log') : el('p', dropped ? 'No line is left to show.' : 'The service has written nothing yet.', 'small muted'));
|
||||
parts.push(retryButton(null, readLog));
|
||||
box.replaceChildren(...parts);
|
||||
}
|
||||
}
|
||||
// #1873: receipts survive a reload: status carries the last few from proxy-admin.jsonl.
|
||||
function paintReceipts() {
|
||||
if (!Array.isArray(state.receipts) || !state.receipts.length) return;
|
||||
$('proxy-setup-receipts').replaceChildren(); state.receipts.forEach(addReceipt);
|
||||
async function readLog() {
|
||||
if (!enabled) return;
|
||||
// A Read again pressed in a box keeps focus there once the answer is painted.
|
||||
const from = [...root.querySelectorAll('[data-log]')].find(box => box.contains(document.activeElement));
|
||||
const round = ++logRound; logAnswer = null; paintLog();
|
||||
let answer;
|
||||
try {
|
||||
const value = await admin('log?lines=20');
|
||||
if (!value || !Array.isArray(value.lines)) throw Object.assign(new Error('The answer could not be read.'), { code: 'outcome-unknown' });
|
||||
answer = { lines: value.lines.filter(line => typeof line === 'string'), redacted: count(value.redacted), dropped: count(value.dropped) };
|
||||
} catch (error) { answer = error?.code === 'not-configured' ? { none: true } : { error: failText(error, LOG_WORDS) }; }
|
||||
if (round !== logRound || !enabled) return;
|
||||
logAnswer = answer; paintLog();
|
||||
from?.querySelector('button')?.focus();
|
||||
}
|
||||
// When the service key was made. Never the key: the answer has no key, and this shows only its time.
|
||||
// It is the last row of the state list; paintState redraws that list, so the row paints from keyView.
|
||||
const KEY_WORDS = { 'not-configured': 'Model service setup is not available on this Console.', 'outcome-unknown': 'The host could not check the key.' };
|
||||
let keyRound = 0, keyView = { text: 'Not read yet.', failed: false };
|
||||
function paintKey() {
|
||||
const dd = $('proxy-setup-keymade-text')?.parentElement; if (!dd) return;
|
||||
const line = el(keyView.failed ? 'p' : 'span', keyView.text, keyView.failed ? 'banner err' : keyView.reading ? 'muted' : undefined); line.id = 'proxy-setup-keymade-text';
|
||||
dd.replaceChildren(line, ...(keyView.failed ? [retryButton('proxy-setup-keymade-retry', readKey)] : []));
|
||||
}
|
||||
function keyRow(dl) {
|
||||
const dt = document.createElement('dt'), dd = document.createElement('dd'), span = el('span'); span.id = 'proxy-setup-keymade-text';
|
||||
dt.textContent = 'Service key'; dd.append(span); dl.append(dt, dd); paintKey();
|
||||
}
|
||||
async function readKey() {
|
||||
if (!enabled) return;
|
||||
const round = ++keyRound, refocus = document.activeElement?.id === 'proxy-setup-keymade-retry';
|
||||
keyView = { text: 'Reading…', failed: false, reading: true }; paintKey();
|
||||
let text, failed = false;
|
||||
try {
|
||||
const value = await admin('service-key');
|
||||
if (!value || typeof value !== 'object') throw Object.assign(new Error('The answer could not be read.'), { code: 'outcome-unknown' });
|
||||
const renewed = value.renewal !== 'not-built' && Number.isFinite(Date.parse(value.renewedAt)) ? ` Last renewed ${window.MosaicLocalTime(value.renewedAt)}.` : ' Renewing it is not built yet.';
|
||||
if (value.problem === 'missing') text = 'Not made yet. Set up model service in step 1 makes it. You never see it.';
|
||||
else if (value.problem === 'unreadable') text = 'Mosaic can\'t tell when it was made: the service\'s settings file is not one it can check. You never see the key.';
|
||||
else if (Number.isFinite(Date.parse(value.madeAt))) text = `Mosaic made it on ${window.MosaicLocalTime(value.madeAt)}. You never see it.${renewed}`;
|
||||
else text = `When it was made is not known. You never see it.${renewed}`;
|
||||
} catch (error) { failed = true; text = `When it was made could not be read. ${failText(error, KEY_WORDS)}`; }
|
||||
if (round !== keyRound || !enabled) return;
|
||||
keyView = { text, failed }; paintKey();
|
||||
if (refocus) { if (failed) $('proxy-setup-keymade-retry')?.focus(); else { const line = $('proxy-setup-keymade-text'); line.tabIndex = -1; line.focus(); } }
|
||||
}
|
||||
// Setup history, a page at a time. The first page holds the newest entries, oldest first within
|
||||
// it; the list shows newest first. A page can be empty and still name an older one.
|
||||
const HISTORY_WORDS = { 'not-configured': 'Model service setup is not available on this Console.', 'outcome-unknown': 'The host could not read the history.', 'invalid-request': 'The Console refused that page.' };
|
||||
const FEW = 3;
|
||||
const hist = { entries: [], next: null, loading: false, expanded: false, error: null, emptyPage: false, round: 0, read: false };
|
||||
const orgName = id => { const label = typeof id === 'string' ? window.MosaicOrg?.row?.(id)?.label : null; return typeof label === 'string' && label ? label : null; };
|
||||
const providerOf = p => typeof p === 'string' && [...providerSelect.options].some(o => o.value === p) ? providerShort(p) : null;
|
||||
// A name shown as written, unless it holds text shaped like a Mosaic key.
|
||||
const shownName = v => typeof v === 'string' && v && v.length <= 64 && !/ms[kotp][-_]/i.test(v) ? v : null;
|
||||
function historyText(entry) {
|
||||
if (!entry || typeof entry !== 'object' || entry.withheld === true) return null;
|
||||
const org = orgName(entry.org), provider = providerOf(entry.provider), some = org ?? 'An organization';
|
||||
const n = (v, word) => Number.isInteger(v) && v >= 0 ? plural(v, word) : null;
|
||||
const labels = {
|
||||
'account.proxy.apikey.add': provider ? `${provider} account added` : 'Account added',
|
||||
'account.proxy.login.complete': 'Signed in to an account',
|
||||
'account.proxy.priority.set': Number.isInteger(entry.priority) ? `Account order changed, priority ${entry.priority}` : 'Account order changed',
|
||||
'account.proxy.group.models.set': n(entry.models, 'model') ? `Models chosen for an account, ${n(entry.models, 'model')}` : 'Models chosen for an account',
|
||||
fetch: 'Release downloaded and checked', init: 'Set up', 'init-lock.clear': 'Stale setup lock cleared', 'fence.clear': 'Key writes resumed',
|
||||
'unit.install': 'Service installed', 'unit.start': 'Started', 'unit.stop': 'Stopped', 'unit.restart': 'Restarted',
|
||||
'ride-all': 'Organizations switched to your models', 'ride-move': `${some} switched to your models`, 'ride-intent': `Switching ${org ?? 'an organization'} to your models`,
|
||||
'ride-not-added': `${some} was not switched to your models`, 'ride-removed': `${some} stopped using your models`,
|
||||
};
|
||||
const label = typeof entry.op === 'string' && Object.hasOwn(labels, entry.op) ? labels[entry.op] : 'Other setup action';
|
||||
const inOrg = org && String(entry.op).startsWith('account.') ? ` in ${org}` : '';
|
||||
const user = entry.user === '[redacted]' ? 'someone whose name Mosaic hid' : shownName(entry.user);
|
||||
return `${label}${inOrg} · ${window.MosaicLocalTime(entry.time)}${user ? ` · by ${user}` : ''}`;
|
||||
}
|
||||
function paintHistory() {
|
||||
const list = $('proxy-setup-history'), msg = $('proxy-setup-history-message'), all = $('proxy-setup-history-all'), older = $('proxy-setup-history-older'), retry = $('proxy-setup-history-retry');
|
||||
list.replaceChildren(...hist.entries.map((entry, i) => {
|
||||
const text = historyText(entry), li = el('li', text ?? 'Withheld. The Console kept this entry back because it may hold a secret.', text ? undefined : 'muted');
|
||||
if (!text) li.dataset.withheld = '';
|
||||
li.hidden = !hist.expanded && i >= FEW; return li;
|
||||
}));
|
||||
const more = hist.next !== null;
|
||||
// Show all only when some entries are folded away; Show older whenever there is an older page and
|
||||
// nothing read is folded away, so an empty page offers it at once. After an empty page it stays while
|
||||
// the list is folded, since the line below names it.
|
||||
all.hidden = !hist.read || hist.entries.length <= FEW;
|
||||
all.textContent = hist.expanded ? 'Show fewer' : 'Show all'; all.setAttribute('aria-expanded', String(hist.expanded));
|
||||
older.hidden = !more || Boolean(hist.error) || (!hist.expanded && hist.entries.length > FEW && !hist.emptyPage);
|
||||
older.disabled = hist.loading; older.textContent = hist.loading ? 'Reading…' : 'Show older';
|
||||
retry.hidden = !hist.error;
|
||||
msg.className = hist.error ? 'banner err' : 'small muted';
|
||||
msg.textContent = hist.error ? `History could not be read. ${hist.error}`
|
||||
: hist.loading && !hist.entries.length ? 'Reading…'
|
||||
: !hist.read ? 'Not read yet.'
|
||||
: hist.emptyPage && more ? 'That page of history had no entries to show. Show older reads further back.'
|
||||
: !hist.entries.length && !more ? 'No setup history yet.' : '';
|
||||
}
|
||||
async function readHistory(reset) {
|
||||
if (!enabled || (hist.loading && !reset)) return;
|
||||
if (reset) Object.assign(hist, { entries: [], next: null, expanded: false, read: false, emptyPage: false });
|
||||
const round = ++hist.round, before = hist.next, from = ['proxy-setup-history-older', 'proxy-setup-history-retry'].find(id => document.activeElement?.id === id);
|
||||
Object.assign(hist, { loading: true, error: null }); paintHistory();
|
||||
try {
|
||||
const value = await admin(`history?limit=25${before === null ? '' : `&before=${encodeURIComponent(before)}`}`);
|
||||
const next = value?.next;
|
||||
// An older page is further back than the one asked for; one that is not would repeat entries.
|
||||
if (!value || !Array.isArray(value.entries) || !(next === null || (Number.isInteger(next) && next >= 0 && (before === null || next < before)))) throw Object.assign(new Error('The answer could not be read.'), { code: 'outcome-unknown' });
|
||||
if (round !== hist.round || !enabled) return;
|
||||
hist.entries.push(...value.entries.slice().reverse());
|
||||
Object.assign(hist, { next, read: true, emptyPage: value.entries.length === 0 });
|
||||
} catch (error) { if (round !== hist.round || !enabled) return; hist.error = failText(error, HISTORY_WORDS); }
|
||||
hist.loading = false; paintHistory();
|
||||
// The button pressed keeps focus while it stays; when it goes, focus lands on Read again, Show older,
|
||||
// Show fewer or the heading, in that order.
|
||||
if (from) { const back = [$(from), $('proxy-setup-history-retry'), $('proxy-setup-history-older'), $('proxy-setup-history-all')].find(b => !b.hidden); if (back) back.focus(); else { const h = $('proxy-setup-history-title'); h.tabIndex = -1; h.focus(); } }
|
||||
}
|
||||
$('proxy-setup-history-all').addEventListener('click', () => { hist.expanded = !hist.expanded; paintHistory(); });
|
||||
// Show older shows what it reads: the list stays open from then on.
|
||||
$('proxy-setup-history-older').addEventListener('click', () => { if (!hist.loading && hist.next !== null) { hist.expanded = true; readHistory(false); } });
|
||||
$('proxy-setup-history-retry').addEventListener('click', () => readHistory(!hist.read));
|
||||
$('proxy-setup-tech').addEventListener('toggle', () => { if (techOpen() && enabled) { readKey(); readHistory(true); readAccess(); } });
|
||||
$('proxy-setup-tech-lines').addEventListener('toggle', () => { if ($('proxy-setup-tech-lines').open && enabled) readLog(); });
|
||||
function row(dl, term, text) { const dt = document.createElement('dt'), dd = document.createElement('dd'); dt.textContent = term; dd.textContent = text; dl.append(dt, dd); }
|
||||
function paintState() {
|
||||
// #1873 settings slice 5: the timeouts init chose, from the same read. Null is not set in
|
||||
// proxy.json, so the model service uses its default.
|
||||
const timeouts = s => !s || typeof s !== 'object' ? '' : `, wait for a first answer${s.firstOutputSeconds == null ? ': the default' : ` ${human(s.firstOutputSeconds)} seconds`}, old key kept${s.rotateGraceMinutes == null ? ' after a new one: the default' : ` ${human(s.rotateGraceMinutes)} minutes after a new one`}`;
|
||||
const { release, init, unit, fence, initLock } = state, dl = $('proxy-setup-state'); dl.replaceChildren();
|
||||
row(dl, 'Release', !release.fetched ? `Not fetched (pinned ${release.version}, panel ${release.panelVersion})` : release.verified ? `Fetched ${release.version}, panel ${release.panelVersion}, hashes verified` : `Fetched ${release.version}, hashes not verified (${release.problem})`);
|
||||
const files = init.files.filter(f => f.present).map(f => f.label).join(', ') || 'none';
|
||||
row(dl, 'Initialized', init.initialized ? `Yes, port ${init.settings?.port ?? '—'}` : init.partial ? `Partly (${init.problem ?? 'incomplete'}). Present: ${files}` : init.problem ? `No (${init.problem})` : 'No');
|
||||
row(dl, 'Initialized', init.initialized ? `Yes, port ${init.settings?.port ?? '—'}${timeouts(init.settings)}` : init.partial ? `Partly (${init.problem ?? 'incomplete'}). Present: ${files}` : init.problem ? `No (${init.problem})` : 'No');
|
||||
row(dl, 'Service', `${unit.unit}: ${unit.installed ? 'installed' : 'not installed'}, ${unit.active}${unit.pid ? `, pid ${unit.pid}` : ''}${unit.port ? `, port ${unit.port} ${unit.listening ? 'answering' : 'not answering'}` : ''}${unit.error ? ` (${unit.error})` : ''}`);
|
||||
row(dl, 'Fence', !fence.set ? 'Clear' : fence.invalid ? 'Set (unreadable fence file)' : `Set after ${fence.op ?? 'a key write'}${fence.org ? ` for ${fence.org}` : ''}${fence.time ? ` at ${window.MosaicLocalTime(fence.time)}` : ''}`);
|
||||
row(dl, 'Init lock', !initLock.present ? 'None' : initLock.clearable ? 'Stale: the bus is stopped and no model service files exist' : `Held: ${initLock.blockers.join('; ')}`);
|
||||
if (state.restart?.confirmed) row(dl, 'Last restart', `Confirmed at ${window.MosaicLocalTime(state.restart.observedAt)}, new pid ${state.restart.pid}`);
|
||||
if (enabled) keyRow(dl);
|
||||
}
|
||||
// settings-p3 (re-check 3 P3-1): where the hand steps for a held init lock or a partial init are written.
|
||||
const README_STEPS = ['The hand steps for a stale init lock and a partial init are in ', 'packages/model-proxy/README.md'];
|
||||
@@ -1031,8 +1214,8 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
if (failed && (CHAIN.includes(next) || next === 'unit-restart')) {
|
||||
setCard('service', 'bad', `${failed.reason} The steps that finished are kept. No organization was touched.`, { badge: ['badge-danger', 'Setup stopped'] });
|
||||
box.replaceChildren(el('p', 'Try again starts from the step that failed.', 'small muted'), goButton('Try again', next, !sendOk || Boolean(state.busy), runChain));
|
||||
paintProgress(true); lines.hidden = false; lines.open = true;
|
||||
$('proxy-setup-log').textContent = state.unit?.error ? String(state.unit.error) : 'Nothing reported.';
|
||||
paintProgress(true);
|
||||
if (lines.hidden) { lines.hidden = false; lines.open = true; readLog(); }
|
||||
return;
|
||||
}
|
||||
lines.hidden = true;
|
||||
@@ -1118,7 +1301,16 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
function paintOrgsCard() {
|
||||
const line = $('setup-step-organizations').querySelector('[data-step-summary]'), go = $('proxy-ride-go');
|
||||
go.className = 'btn'; delete line.dataset.next;
|
||||
if (!running() || !accountCount) { setCard('organizations', 'wait', 'Opens after your first account.'); return; }
|
||||
// settings-s5: before the first account, an organization the sidebar already marks "can't start" is
|
||||
// named here, so the card and the mark agree.
|
||||
if (!running() || !accountCount) {
|
||||
const blocked = running() ? cantStart().map(f => nameOf(f.id)) : [];
|
||||
if (!blocked.length) { setCard('organizations', 'wait', 'Opens after your first account.'); return; }
|
||||
const one = blocked.length === 1;
|
||||
// The card stays closed (Move needs a model from an account), with the mark's word on its badge.
|
||||
setCard('organizations', 'wait', `Opens after your first account. ${listNames(blocked)} can't start now: ${one ? 'it uses' : 'they use'} stand-in models and ${one ? 'its bus is' : 'their buses are'} stopped. After your first account, move ${one ? 'it' : 'them'} to your models under Use your models in every organization, then start ${one ? 'its bus' : 'their buses'}.`, { badge: ['badge-danger', "Can't start"] });
|
||||
return;
|
||||
}
|
||||
const step = orgStep(), names = needRestart().filter(restartHelps).map(o => nameOf(o.id));
|
||||
// settings-p3 (rebase re-check P3-1, settings-s4 P3): the Ride all box still lists an organization,
|
||||
// so not "every". One that turned the model service off is not waiting on anything: it is named.
|
||||
@@ -1200,20 +1392,212 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
// After init, Advanced shows the settings init wrote, read-only.
|
||||
const dl = $('proxy-setup-settings'), s = init.initialized && init.settings && typeof init.settings === 'object' ? init.settings : null;
|
||||
dl.hidden = !s; $('proxy-setup-settings-note').hidden = !s; dl.replaceChildren();
|
||||
if (s) { row(dl, 'Port', human(s.port)); if (s.firstOutputSeconds !== undefined) row(dl, 'Wait for a first answer', `${human(s.firstOutputSeconds)} seconds`); }
|
||||
if (s) {
|
||||
row(dl, 'Port', human(s.port));
|
||||
if (s.firstOutputSeconds !== undefined) row(dl, 'Wait for a first answer', s.firstOutputSeconds === null ? 'The default' : `${human(s.firstOutputSeconds)} seconds`);
|
||||
if (s.rotateGraceMinutes !== undefined) row(dl, 'Old key kept after a new one', s.rotateGraceMinutes === null ? 'The default' : `${human(s.rotateGraceMinutes)} minutes`);
|
||||
}
|
||||
}
|
||||
// Programs that drive models: the harnesses this Console offers an organization, read on open.
|
||||
let programsRead = false;
|
||||
$('proxy-setup-advanced').addEventListener('toggle', async () => {
|
||||
if (!$('proxy-setup-advanced').open || programsRead) return; programsRead = true;
|
||||
const list = $('proxy-setup-programs'); list.replaceChildren(el('li', 'Reading…', 'muted'));
|
||||
// settings-s5c: Programs that drive models, from the status read's programs field (owner only: only
|
||||
// the owner's page reads the status). A list the read could not give reads as not known, never as off.
|
||||
// Program names come from the read; this page holds no list of them.
|
||||
const KEYISH = /ms[kotp][-_]/i;
|
||||
const programName = v => typeof v === 'string' && /^[a-z][a-z0-9-]{0,31}$/.test(v) && !KEYISH.test(v) ? v : null;
|
||||
const orgLabel = id => shownName(orgName(id)) ?? shownName(id) ?? 'An organization Mosaic can\'t name';
|
||||
let offAsk = null, offBusy = false, offView = null, offFocus = null;
|
||||
function programsState() {
|
||||
const p = state?.programs;
|
||||
if (!p || typeof p !== 'object') return { error: 'The programs could not be read.' };
|
||||
if (!Array.isArray(p.installed)) return { error: 'The installed programs could not be read.' };
|
||||
const names = [...new Set(p.installed.map(programName).filter(Boolean))], hidden = p.installed.some(n => !programName(n));
|
||||
const orgs = Array.isArray(p.organizations) ? p.organizations.filter(o => o && typeof o === 'object') : null;
|
||||
return { names, hidden, orgs };
|
||||
}
|
||||
function programLine(name, orgs) {
|
||||
if (!orgs) return 'Which organizations have it on could not be read.';
|
||||
if (!orgs.length) return 'No organization to check.';
|
||||
const on = [], off = [], unknown = [];
|
||||
for (const o of orgs) {
|
||||
const label = orgLabel(o.id);
|
||||
if (!Array.isArray(o.on)) unknown.push(label); else if (o.on.includes(name)) on.push(label); else off.push(label);
|
||||
}
|
||||
const parts = [];
|
||||
parts.push(on.length ? `On in ${listNames(on)}.` : 'On in no organization.');
|
||||
if (off.length) parts.push(`Off in ${listNames(off)}.`);
|
||||
if (unknown.length) parts.push(`Not known in ${listNames(unknown)}: ${unknown.length === 1 ? 'its list' : 'their lists'} could not be read.`);
|
||||
return parts.join(' ');
|
||||
}
|
||||
function paintPrograms() {
|
||||
const list = $('proxy-setup-programs'), had = document.activeElement;
|
||||
// A repaint while a control here holds focus puts focus back on the same control.
|
||||
const keep = had && list.contains(had) ? (had.id ? '#' + had.id : had.dataset.programOff ? `[data-program-off="${had.dataset.programOff}"]` : null) : null;
|
||||
const view = programsState();
|
||||
// A read that no longer lists the program being asked about drops the question.
|
||||
if (offAsk !== null && (view.error || !view.names.includes(offAsk))) offAsk = null;
|
||||
if (view.error) { list.replaceChildren(el('li', view.error, 'muted')); return; }
|
||||
const items = view.names.map(name => {
|
||||
const li = el('li'); li.dataset.program = name;
|
||||
const head = el('div', '', 'proxy-program-head');
|
||||
head.append(el('span', name, 'proxy-program-name'));
|
||||
const off = el('button', 'Turn off for every organization'); off.type = 'button'; off.dataset.programOff = name;
|
||||
off.setAttribute('aria-label', `Turn off ${name} for every organization`);
|
||||
off.disabled = !sendOk || offBusy || offAsk !== null;
|
||||
off.addEventListener('click', () => askOff(name));
|
||||
head.append(off); li.append(head, el('p', programLine(name, view.orgs), 'small proxy-program-orgs'));
|
||||
if (offAsk === name) {
|
||||
const box = el('div', '', 'proxy-program-confirm'); box.setAttribute('role', 'group'); box.setAttribute('aria-labelledby', 'proxy-setup-programs-ask');
|
||||
const ask = el('p', `Turn off ${name} in every organization? Mosaic skips an organization where something still uses ${name} or where it is the only program on. Turning it on again is done on each organization's Models page.`);
|
||||
ask.id = 'proxy-setup-programs-ask';
|
||||
const yes = el('button', `Turn off ${name}`, 'danger'); yes.type = 'button'; yes.id = 'proxy-setup-programs-yes'; yes.disabled = !sendOk || offBusy;
|
||||
yes.addEventListener('click', () => programsOff(name));
|
||||
const no = el('button', 'Cancel'); no.type = 'button'; no.id = 'proxy-setup-programs-no';
|
||||
no.addEventListener('click', () => { offAsk = null; paintPrograms(); root.querySelector(`[data-program-off="${name}"]`)?.focus(); });
|
||||
const foot = el('p', '', 'proxy-setup-history-foot'); foot.append(yes, no);
|
||||
box.append(ask, foot); li.append(box);
|
||||
}
|
||||
return li;
|
||||
});
|
||||
if (!view.names.length) items.push(el('li', 'No programs are installed.', 'muted'));
|
||||
if (view.hidden) items.push(el('li', 'One or more programs have a name this page does not show.', 'muted'));
|
||||
list.replaceChildren(...items);
|
||||
if (keep) root.querySelector(keep)?.focus();
|
||||
}
|
||||
function askOff(name) {
|
||||
if (!sendOk || offBusy) return;
|
||||
offAsk = name; paintPrograms(); $('proxy-setup-programs-yes')?.focus();
|
||||
}
|
||||
// What each organization's outcome means, in plain words (settings-backend-a2 build report).
|
||||
function offOutcome(outcome) {
|
||||
const words = {
|
||||
off: 'Turned off.',
|
||||
'already-off': 'It was already off. Nothing changed.',
|
||||
'in-use': 'Kept on. Something there still uses it: a role route, a fallback or an agent.',
|
||||
'last-program': 'Kept on. It is the only program on there.',
|
||||
'bus-down': 'Not changed. Its programs could not be read.',
|
||||
unreadable: 'Not changed. Something Mosaic needed to check could not be read.',
|
||||
'failed:revision-conflict': 'Not changed. It changed while Mosaic was checking. Try again.',
|
||||
'failed:outcome-unknown': 'May have changed. The change did not answer in time.',
|
||||
};
|
||||
if (typeof outcome !== 'string') return 'Not known. The answer did not say what happened.';
|
||||
if (Object.hasOwn(words, outcome)) return words[outcome];
|
||||
if (outcome.startsWith('failed:')) {
|
||||
const code = outcome.slice(7);
|
||||
return /^[a-z0-9][a-z0-9-]{0,63}$/.test(code) && !KEYISH.test(code) ? `Not changed. It refused the change (${code}).` : 'Not changed. It refused the change, with a code this page does not show.';
|
||||
}
|
||||
return 'Not known. The answer did not say what happened.';
|
||||
}
|
||||
const OFF_WORDS = {
|
||||
'invalid-request': 'The Console refused the request: the program is not in its installed list, or that list could not be read.',
|
||||
'human-required': 'Only the instance owner can turn a program off.',
|
||||
'proxy-admin-busy': 'Another setup action is running. Wait for it to finish, then try again.',
|
||||
};
|
||||
function paintOffResult() {
|
||||
const box = $('proxy-setup-programs-result');
|
||||
if (!offView) { box.hidden = true; box.replaceChildren(); return; }
|
||||
box.hidden = false;
|
||||
if (offView.working) { box.className = 'proxy-programs-result'; box.replaceChildren(el('p', `Turning off ${offView.name}…`, 'small muted')); return; }
|
||||
// Whether the list above was read again after the answer: it says so, and never claims a list it did not get.
|
||||
const reread = () => {
|
||||
if (offView.reread === 'ok') return [el('p', 'The list above is read again and shows what is on now.', 'small proxy-programs-reread')];
|
||||
if (offView.reread === 'failed') {
|
||||
const foot = el('p', '', 'proxy-setup-history-foot'); foot.append(retryButton('proxy-setup-programs-reread', () => rereadPrograms(true)));
|
||||
return [el('p', 'The list above could not be read again, so it may be out of date.', 'small proxy-programs-reread'), foot];
|
||||
}
|
||||
return [el('p', 'Reading the list again…', 'small muted proxy-programs-reread')];
|
||||
};
|
||||
if (offView.line) { box.className = `proxy-programs-result banner ${offView.kind === 'busy' ? 'ref' : 'err'}`; box.replaceChildren(el('p', offView.line), ...reread()); return; }
|
||||
box.className = 'proxy-programs-result banner';
|
||||
const title = el('p', `Turn off ${offView.name}: what happened in each organization.`, 'proxy-programs-result-title');
|
||||
const ul = el('ul', '', 'proxy-programs-outcomes');
|
||||
for (const r of offView.results) {
|
||||
const li = el('li'); li.dataset.outcome = typeof r?.outcome === 'string' && /^[a-z:-]{1,80}$/.test(r.outcome) && !KEYISH.test(r.outcome) ? r.outcome : 'unknown';
|
||||
li.append(el('span', orgLabel(r?.id), 'proxy-outcome-org'), document.createTextNode(': '), el('span', offOutcome(r?.outcome)));
|
||||
ul.append(li);
|
||||
}
|
||||
box.replaceChildren(title, offView.results.length ? ul : el('p', 'No organization was in the answer.', 'small muted'), ...reread());
|
||||
}
|
||||
// Read the status again after a turn-off answer, after a read already under way. load() keeps the
|
||||
// old state when its read fails, so a new state object is the sign the list was read again.
|
||||
async function rereadPrograms(fromButton) {
|
||||
const view = offView; if (!view) return;
|
||||
view.reread = 'reading'; paintOffResult();
|
||||
while (loading) await pause(50);
|
||||
const painted = await load();
|
||||
if (offView !== view) return;
|
||||
view.reread = painted ? 'ok' : 'failed'; paintOffResult();
|
||||
const box = $('proxy-setup-programs-result');
|
||||
if (fromButton || document.activeElement === document.body || !document.activeElement) (view.reread === 'failed' ? $('proxy-setup-programs-reread') : box)?.focus();
|
||||
}
|
||||
async function programsOff(name) {
|
||||
if (!sendOk || offBusy) return;
|
||||
offBusy = true; offAsk = null; offView = { name, working: true }; paintPrograms(); paintOffResult();
|
||||
const box = $('proxy-setup-programs-result'); box.focus();
|
||||
let view;
|
||||
try {
|
||||
const r = await fetch('/api/proxy/plugins', { cache: 'no-store', redirect: 'manual' }); const v = r.ok ? await r.json() : null;
|
||||
const names = Array.isArray(v?.available?.harness) ? v.available.harness.filter(n => typeof n === 'string') : null;
|
||||
if (!names) throw Error();
|
||||
list.replaceChildren(...(names.length ? names.map(n => el('li', n)) : [el('li', 'No programs are installed.', 'muted')]));
|
||||
} catch { programsRead = false; list.replaceChildren(el('li', 'The programs could not be read.', 'muted')); }
|
||||
});
|
||||
const value = await admin('programs/off', { name });
|
||||
if (!value || typeof value !== 'object' || !Array.isArray(value.results)) throw Object.assign(new Error('The answer could not be read.'), { code: 'outcome-unknown' });
|
||||
view = { name, results: value.results };
|
||||
addReceipt(value.receipt);
|
||||
} catch (error) {
|
||||
if (error?.code === 'proxy-admin-busy') view = { name, kind: 'busy', line: `${name} was not turned off. ${OFF_WORDS['proxy-admin-busy']}` };
|
||||
else if (error?.code === 'invalid-request' || error?.code === 'human-required') view = { name, line: `${name} was not turned off. ${failText(error, OFF_WORDS)}` };
|
||||
else view = { name, line: `Turning off ${name} may not have finished, so some organizations may have changed. ${failText(error, OFF_WORDS)}` };
|
||||
}
|
||||
offView = view; offBusy = false; paintOffResult(); paintPrograms();
|
||||
box.focus();
|
||||
// After any answer, read the status again.
|
||||
await rereadPrograms(false);
|
||||
}
|
||||
// settings-s5c: access health per organization, read when Technical details opens (owner only).
|
||||
// Ready only for health ok whose every stack-managed key is active; never a key, a key id or the
|
||||
// count of other keys, which holds other organizations' keys and is not a warning.
|
||||
const ACCESS_WORDS = { 'human-required': 'Only the instance owner can read access.', 'outcome-unknown': 'The host could not read access.', 'not-configured': 'Model service setup is not available on this Console.' };
|
||||
let accessRound = 0, accessView = null;
|
||||
function accessHealth(o) {
|
||||
const unknown = ['badge-muted', 'Not known', 'The answer did not say.'];
|
||||
if (!o || typeof o !== 'object') return unknown;
|
||||
const { health, reason, keys } = o;
|
||||
if (health === 'ok') return reason === undefined && Array.isArray(keys) && keys.length > 0 && keys.every(k => k && typeof k === 'object' && k.state === 'active')
|
||||
? ['badge-ok', 'Ready', null] : ['badge-muted', 'Not known', 'The answer did not agree with itself.'];
|
||||
if (health === 'attention') return reason === 'proxy-uncertain'
|
||||
? ['badge-attn', 'Not sure', 'The model service did not answer clearly, so Mosaic can\'t tell whether its access works.']
|
||||
: ['badge-attn', 'Needs attention', 'Some of its access is not active.'];
|
||||
if (health === 'missing') return ['badge-attn', 'None yet', 'No agent here has access to your models yet.'];
|
||||
if (health === 'not-riding') return ['badge-muted', 'Not used', 'It does not use your models.'];
|
||||
if (health === 'unreadable') return ['badge-danger', 'Could not be read', reason === 'bus-down' ? 'Its bus did not answer.' : reason === 'no-proxy-client' ? 'Its bus has no model service client set up.' : reason === 'unreadable' ? 'Its answer about access could not be read.' : null];
|
||||
return unknown;
|
||||
}
|
||||
function paintAccess() {
|
||||
const list = $('proxy-setup-access'), message = $('proxy-setup-access-message'), retry = $('proxy-setup-access-retry');
|
||||
retry.hidden = !accessView?.error;
|
||||
if (!accessView) { message.className = 'small muted'; message.textContent = 'Not read yet.'; list.replaceChildren(); return; }
|
||||
if (accessView.reading) { message.className = 'small muted'; message.textContent = 'Reading…'; return; }
|
||||
if (accessView.error) { message.className = 'banner err'; message.textContent = `Access could not be read. ${accessView.error}`; list.replaceChildren(); return; }
|
||||
const orgs = accessView.organizations;
|
||||
message.className = 'small muted'; message.textContent = orgs.length ? '' : 'There is no organization to check.';
|
||||
list.replaceChildren(...orgs.map(o => {
|
||||
const [badge, word, why] = accessHealth(o), li = el('li');
|
||||
li.dataset.access = word === 'Ready' ? 'ready' : 'not-ready';
|
||||
li.append(el('span', orgLabel(o?.id)), el('span', word, `badge ${badge}`));
|
||||
if (why) li.append(el('p', why, 'small muted account-reason'));
|
||||
return li;
|
||||
}));
|
||||
}
|
||||
async function readAccess() {
|
||||
if (!enabled) return;
|
||||
const round = ++accessRound, refocus = document.activeElement?.id === 'proxy-setup-access-retry';
|
||||
accessView = { ...(accessView ?? {}), reading: true, error: null }; paintAccess();
|
||||
let view;
|
||||
try {
|
||||
const value = await admin('access');
|
||||
if (!value || typeof value !== 'object' || !Array.isArray(value.organizations)) throw Object.assign(new Error('The answer could not be read.'), { code: 'outcome-unknown' });
|
||||
view = { organizations: value.organizations };
|
||||
} catch (error) { view = { error: failText(error, ACCESS_WORDS) }; }
|
||||
if (round !== accessRound || !enabled) return;
|
||||
accessView = view; paintAccess();
|
||||
if (refocus) { if (view.error) $('proxy-setup-access-retry').focus(); else { const m = $('proxy-setup-access-title'); m.tabIndex = -1; m.focus(); } }
|
||||
}
|
||||
$('proxy-setup-access-retry').addEventListener('click', readAccess);
|
||||
// #1873 ride-default: Ride all and the fixture move. The server decides each organization again;
|
||||
// this code only words the answer. Every write asks first, in plain words, what changes.
|
||||
const RIDE_REASONS = {
|
||||
@@ -1255,15 +1639,15 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
+ 'Admission gets stricter there: every launch route is checked, direct Anthropic routes too, and while the model service is down or fenced every launch in that organization is refused. '
|
||||
+ (ready ? `${ready} ${ready === 1 ? 'organization can' : 'organizations can'} ride now.` : 'No organization is waiting to ride.');
|
||||
const go = $('proxy-ride-go'); go.disabled = !sendOk || Boolean(state.busy) || !ready;
|
||||
$('proxy-ride-skips').replaceChildren(...rideSkips().map(s => li(`Skipped ${s.id}: ${rideReason(s.reason)}`)));
|
||||
$('proxy-ride-skips').replaceChildren(...rideSkips().map(s => li(`Skipped ${nameOf(s.id)}: ${rideReason(s.reason)}`)));
|
||||
const listedFixtures = rideResult?.fixtures ?? fixtures.map(f => ({ id: f.id, running: f.running }));
|
||||
$('proxy-ride-fixtures').replaceChildren(...listedFixtures.map(f => {
|
||||
// running null: no bus status here (the single-business Console), so moving is not available.
|
||||
const name = nameOf(f.id);
|
||||
if (f.running === null) return li(`${name} uses stand-in models. Moving it is not available on this Console.`);
|
||||
// settings-s4 review F2: running false is what the sidebar marks "can't start"; say so, and the fix.
|
||||
const item = li(f.running === false ? `${name} can't start: it uses stand-in models and its bus is stopped. Move it to the owner's models, then start its bus. `
|
||||
: `${name} uses stand-in models, can be moved to the owner's models.${f.running === true ? ' Its bus is running: stop it first.' : ''} `);
|
||||
const item = li(f.running === false ? `${name} can't start: it uses stand-in models and its bus is stopped. Move it to your models under Use your models in every organization, then start its bus. `
|
||||
: `${name} uses stand-in models, can be moved to your models.${f.running === true ? ' Its bus is running: stop it first.' : ''} `);
|
||||
const move = document.createElement('button'); move.type = 'button'; move.className = 'btn'; move.textContent = `Move ${name}`;
|
||||
move.dataset.rideMove = f.id; move.disabled = !sendOk || Boolean(state.busy);
|
||||
move.addEventListener('click', () => { if (!move.disabled) rideMove(f.id); });
|
||||
@@ -1281,33 +1665,35 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
});
|
||||
};
|
||||
const rideMove = id => {
|
||||
if (!window.confirm(`Move ${id} to your models? Its stand-in models are replaced by a model your model service lists. After this, its agents spend real tokens on your account. Its bus stays stopped: start it when you are ready.`)) return;
|
||||
if (!window.confirm(`Move ${nameOf(id)} to your models? Its stand-in models are replaced by a model your model service lists. After this, its agents spend real tokens on your account. Its bus stays stopped: start it when you are ready.`)) return;
|
||||
return act('ride-move', async () => {
|
||||
const result = await admin('ride-move', { org: id });
|
||||
if (!result.moved) { rideSay(`${id} was not moved. ${rideReason(result.reason)}`, 'error'); return; }
|
||||
if (!result.moved) { rideSay(`${nameOf(id)} was not moved. ${rideReason(result.reason)}`, 'error'); return; }
|
||||
addReceipt(result.receipt);
|
||||
if (rideResult) rideResult = { ...rideResult, fixtures: rideResult.fixtures.filter(f => f.id !== id) };
|
||||
rideSay(`${id} now uses ${result.model}. Start its bus when you are ready. It spends real tokens from now on.`, 'success');
|
||||
rideSay(`${nameOf(id)} now uses ${result.model}. Start its bus when you are ready. It spends real tokens from now on.`, 'success');
|
||||
});
|
||||
};
|
||||
$('proxy-ride-go').addEventListener('click', () => { if (!$('proxy-ride-go').disabled) rideAll(); });
|
||||
function paint() {
|
||||
paintState(); paintControls(); paintReceipts(); paintService(); paintRestart(); paintOrgsCard(); paintTriageCard(); paintAccountCard();
|
||||
paintState(); paintControls(); paintService(); paintRestart(); paintOrgsCard(); paintTriageCard(); paintAccountCard(); paintPrograms();
|
||||
const job = state.fetch;
|
||||
if (job.state === 'running') say(`Downloading: ${STEPS[job.step] ?? job.step ?? 'starting'}…`);
|
||||
else if (job.state === 'failed' && seenFetch !== 'failed') { say(`Fetch failed. ${explain(job.error)}`, 'error'); seenFetch = 'failed'; }
|
||||
else if (job.state === 'done' && job.receipt && seenFetch !== job.receipt.time) { seenFetch = job.receipt.time; if (!state.receipts?.some(r => r.op === 'fetch' && r.time === job.receipt.time)) addReceipt(job.receipt); say(`Fetched ${job.receipt.version}; archive, panel and executable hashes verified.`, 'success'); }
|
||||
else if (job.state === 'done' && job.receipt && seenFetch !== job.receipt.time) { seenFetch = job.receipt.time; addReceipt(job.receipt); say(`Fetched ${job.receipt.version}; archive, panel and executable hashes verified.`, 'success'); }
|
||||
clearTimeout(timer); timer = null;
|
||||
if (enabled && !chain?.running && (job.state === 'running' || state.busy)) timer = setTimeout(load, 1000);
|
||||
// The sidebar's model marks read the same status (MosaicModelDots).
|
||||
document.dispatchEvent(new CustomEvent('mosaic-proxy-status', { detail: state }));
|
||||
focusStep();
|
||||
}
|
||||
// True once the answer is painted, so a caller can say the list on the page is the new one.
|
||||
async function load() {
|
||||
if (!enabled || loading) return; loading = true;
|
||||
try { state = await admin('status'); causeRound++; if (state.next?.[0] === 'accounts' && triageSet === null) await readTriage(); paint(); if (state.init.initialized) await accountsReady(); }
|
||||
catch (error) { say(error.message, 'error'); }
|
||||
if (!enabled || loading) return false; loading = true; let painted = false;
|
||||
try { state = await admin('status'); setProviders(state.providers); causeRound++; if (state.next?.[0] === 'accounts' && triageSet === null) await readTriage(); paint(); painted = true; if (state.init.initialized) await accountsReady(); }
|
||||
catch (error) { say(error.message, 'error'); if (providerKey === null) setProviders(null); }
|
||||
finally { loading = false; }
|
||||
return painted;
|
||||
}
|
||||
async function act(label, action) {
|
||||
if (!sendOk || state?.busy || chain?.running) return;
|
||||
@@ -1421,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
|
||||
@@ -1494,5 +1880,6 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
|
||||
document.addEventListener('mosaic-org-restarted', () => { read(); });
|
||||
const begin = () => { (window.MosaicOrg?.ready ?? Promise.resolve()).then(read, () => {}); };
|
||||
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', begin, { once: true }); else begin();
|
||||
window.MosaicModelDots = { get: id => marks.get(id) ?? null };
|
||||
// settings-s5: a page that changes what the status says (an organization's Models choice) asks for a fresh read.
|
||||
window.MosaicModelDots = { get: id => marks.get(id) ?? null, read };
|
||||
})();
|
||||
@@ -119,7 +119,19 @@ table.s1-table td code{font-size:12.5px;white-space:nowrap}
|
||||
.proxy-setup-next{display:flex;flex-wrap:wrap;align-items:center;gap:8px 16px;margin:0 0 12px}.proxy-setup-next p{margin:0;flex:1 1 280px}
|
||||
.proxy-setup-buttons{display:flex;flex-wrap:wrap;gap:8px}.proxy-setup-buttons button{min-height:44px}
|
||||
#proxy-setup pre{overflow-x:auto;max-width:100%;font-size:12px}#proxy-setup h3{margin:16px 0 4px}#proxy-setup details{margin:0 0 12px}#proxy-setup summary{min-height:44px;display:flex;align-items:center;cursor:pointer}
|
||||
#proxy-setup-receipts{padding-left:20px;overflow-wrap:anywhere;font-size:13px}
|
||||
/* settings-s5b: the setup history (GET /api/proxy/admin/history), the service key and the service's lines. */
|
||||
#proxy-setup-history{list-style:none;padding-left:0;overflow-wrap:anywhere;font-size:13px;margin:0 0 8px}#proxy-setup-history li{padding:2px 0}
|
||||
.proxy-setup-history-foot{display:flex;flex-wrap:wrap;gap:8px;margin:0}.proxy-setup-history-foot button,[data-log] button,#proxy-setup-keymade-retry{min-height:44px}
|
||||
/* settings-s5c: Programs that drive models under Advanced, and Access under Technical details. */
|
||||
.settings-form.proxy-programs{margin-bottom:20px;gap:8px}.proxy-programs-list{list-style:none;margin:8px 0 0;padding:0}.proxy-programs-list>li{padding:10px 0;border-top:1px solid var(--line);min-width:0}
|
||||
.proxy-program-head{display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:8px 12px}.proxy-program-name{font-weight:600;color:var(--strong)}
|
||||
.proxy-program-head button,.proxy-program-confirm button,#proxy-setup-access-retry{min-height:44px}.proxy-program-orgs{margin:4px 0 0;overflow-wrap:anywhere}
|
||||
.proxy-program-confirm{margin:8px 0 0;padding:10px 12px;border:1px solid color-mix(in srgb,var(--danger) 50%,var(--line));border-radius:var(--r);background:color-mix(in srgb,var(--danger) 6%,var(--surface))}.proxy-program-confirm>p:first-child{margin:0 0 8px;overflow-wrap:anywhere}
|
||||
.proxy-program-confirm button.danger{color:var(--danger);border-color:color-mix(in srgb,var(--danger) 60%,var(--line))}
|
||||
.proxy-programs-result{margin:12px 0 0;display:block}.proxy-programs-result[hidden]{display:none}.proxy-programs-result:focus-visible,#proxy-setup-access-title:focus-visible{outline:2px solid var(--focus);outline-offset:2px}.proxy-programs-result>p{margin:0}.proxy-programs-result>.proxy-programs-reread{margin-top:8px}.proxy-programs-result-title{font-weight:600;color:var(--strong)}
|
||||
.proxy-programs-outcomes{margin:6px 0 0;padding-left:18px;overflow-wrap:anywhere}.proxy-programs-outcomes li{padding:2px 0}.proxy-outcome-org{font-weight:600}
|
||||
.proxy-setup-access{margin:12px 0}.proxy-setup-access h4{margin:0 0 2px}.proxy-access li>.badge{justify-self:start}.proxy-setup-access .rows{margin-top:4px}#proxy-setup-access-message:empty{display:none}
|
||||
[data-log] .setup-log{margin:8px 0}[data-log] p{margin:0 0 8px}#proxy-setup-history-message:empty{display:none}
|
||||
/* #1873 settings slice 1 (D3, D4): the kept restart list and Technical details. */
|
||||
.proxy-setup-restart{margin:0 0 12px}
|
||||
.proxy-setup-restart-list{list-style:none;margin:8px 0 0;padding:0;display:grid;gap:8px}
|
||||
@@ -339,6 +351,7 @@ button:disabled,[aria-disabled=true]{cursor:not-allowed}
|
||||
.rows li{display:grid;grid-template-columns:7rem minmax(0,1fr) auto;gap:6px var(--gap,12px);align-items:center;padding:10px 0;border-top:1px solid var(--line)}
|
||||
.rows li>span:first-child{font-weight:600;color:var(--strong)}
|
||||
.rows li>.account-reason{grid-column:2/-1;margin:0}
|
||||
.rows li.muted{display:block}
|
||||
.progress{list-style:none;margin:var(--gap,12px) 0 0;padding:0;display:grid;gap:6px}
|
||||
.progress li::before{display:inline-block;width:1.4em;color:var(--muted)}
|
||||
.progress .ok::before{content:"✓";color:var(--success)}.progress .run::before{content:"●";color:var(--action)}.progress .todo::before{content:"○"}.progress .bad::before{content:"✕";color:var(--danger)}
|
||||
@@ -375,6 +388,19 @@ button:disabled,[aria-disabled=true]{cursor:not-allowed}
|
||||
.settings-content fieldset.harness-control{display:flex;flex-wrap:wrap;align-items:center;gap:6px 12px}
|
||||
.settings-content fieldset.harness-control>legend{flex-basis:100%}
|
||||
.settings-content fieldset.harness-control>:is(button,p){flex-basis:100%}
|
||||
/* #1873 settings slice 5: an organization's one choice (mockup org-models). A choice not built yet is dashed and disabled. */
|
||||
/* settings-s5 review P3-4: the choice cards sit in the form column, as .settings-form does. */
|
||||
.settings-content fieldset.models-source{display:grid;gap:8px;max-width:28rem}
|
||||
.settings-content fieldset.models-source>legend{font-size:14px;color:var(--strong);margin-bottom:8px}
|
||||
.settings-content fieldset.models-source>p{margin:0}
|
||||
.choice{display:grid;grid-template-columns:auto 1fr auto;gap:4px 10px;align-items:start;padding:12px;border:1px solid var(--border);border-radius:var(--r);cursor:pointer}
|
||||
.choice input{margin-top:4px}
|
||||
.choice:has(input:checked){border-color:var(--action);background:var(--raised)}
|
||||
.choice:has(input:disabled){cursor:default}
|
||||
.choice.off{color:var(--muted);cursor:not-allowed;border-style:dashed;background:transparent}
|
||||
.choice strong{color:var(--strong)}.choice.off strong{color:var(--muted)}
|
||||
.choice .small{grid-column:2/4;margin:0}
|
||||
.choice:has(input:focus-visible){outline:2px solid var(--focus);outline-offset:2px}
|
||||
.settings-content :is(.instance-form,.level-form) textarea,.settings-content .assistant-form textarea{display:block;width:100%}
|
||||
.route-definition{display:none}.settings-spinner{display:inline-block;width:16px;height:16px;border:2px solid currentColor;border-right-color:transparent;border-radius:50%;vertical-align:middle;animation:settings-spin .8s linear infinite}@keyframes settings-spin{to{transform:rotate(360deg)}}@media(prefers-reduced-motion:reduce){.settings-spinner{animation:none}}
|
||||
@media(max-width:759px){#routes-table .route-definition{display:inline;float:right;color:var(--muted);font-size:12px;font-weight:400}}
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
const $ = id => document.getElementById(id);
|
||||
const esc = s => String(s).replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]);
|
||||
const PATH = '/api/triage/route';
|
||||
// proxy-scope slice 4 (#1873): Repair access and Turn Triage off (triage-route-http.mjs).
|
||||
const ACCESS_PATH = '/api/triage/access';
|
||||
// The time zones this browser knows, offered as suggestions only; the server decides.
|
||||
const zones = (() => { try { return Intl.supportedValuesOf('timeZone'); } catch { return []; } })();
|
||||
// draft holds the typed fields; base is the revision they were read at, which a save sends, so a
|
||||
@@ -68,10 +70,47 @@
|
||||
+ `</form>${send ? '' : '<p class="small muted" id="triage-route-readonly">This Console is read-only, so the route cannot be saved here.</p>'}`;
|
||||
}
|
||||
|
||||
// proxy-scope slice 4: the key's state, ids and days only, and the two access buttons.
|
||||
let accessLine = '';
|
||||
function access() {
|
||||
const a = data?.access;
|
||||
if (!a || typeof a !== 'object') return '';
|
||||
const state = a.key === 'issued' ? `Triage key ${esc(a.keyId)} is issued. It rotates on ${esc(a.rotateBy)}.`
|
||||
: a.key === 'missing' ? 'No Triage key is issued, so Triage will not start.'
|
||||
: 'The Triage key file is not safe to use, so Triage will not start.';
|
||||
const warn = [a.rotation === 'rotation overdue' ? 'Key rotation overdue.' : '', a.code === 'triage-no-riding-bus' ? `${a.message}.` : ''].filter(Boolean).join(' ');
|
||||
const off = busy || data?.send !== true ? ' disabled' : '';
|
||||
return `<div id="triage-access" data-key="${esc(a.key)}" data-code="${esc(a.code ?? '')}" data-rotation="${esc(a.rotation ?? '')}"><p class="small">${state}</p>`
|
||||
+ (warn ? `<p class="small warn" id="triage-access-warn">${esc(warn)}</p>` : '')
|
||||
+ `<div class="settings-actions"><button type="button" class="btn" id="triage-access-repair"${off}>Repair access</button> <button type="button" class="btn" id="triage-access-off"${off}>Turn Triage off</button> <span role="status" id="triage-access-line" tabindex="-1">${esc(accessLine)}</span></div></div>`;
|
||||
}
|
||||
|
||||
// Each key action in its own plain sentence (proxy.triage.key answers).
|
||||
const ACTION_LINE = {
|
||||
issued: 'A Triage key was issued.', replaced: 'A new Triage key replaced one the proxy no longer held.',
|
||||
held: 'The Triage key is in place.', revoked: 'Triage is off. Its key was removed.', none: 'Triage is off. No key was held.',
|
||||
};
|
||||
async function accessOp(op) {
|
||||
if (busy || data?.send !== true) return;
|
||||
if (op === 'off' && typeof window.confirm === 'function' && !window.confirm('Turn Triage off? This stops any Triage session and removes its key. Repair access issues a new one.')) return;
|
||||
busy = true; accessLine = op === 'off' ? 'Turning Triage off…' : 'Repairing access…'; repaint();
|
||||
let r, a = null;
|
||||
try { r = await window.fetch(ACCESS_PATH, { cache: 'no-store', redirect: 'manual', method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ op }) }); }
|
||||
catch { busy = false; accessLine = 'Outcome unknown. Reload the page to see the key state.'; repaint(); return; }
|
||||
try { a = await r.json(); } catch { /* refused below */ }
|
||||
busy = false;
|
||||
if (a?.access && data) data = { ...data, access: a.access };
|
||||
if (r.type === 'opaqueredirect') accessLine = 'Sign in again, then retry.';
|
||||
else if (r.ok) accessLine = ACTION_LINE[a?.key?.action] ?? (op === 'off' ? 'Triage is off.' : 'Access was repaired.');
|
||||
else accessLine = typeof a?.message === 'string' ? a.message : 'The Triage key was not changed.';
|
||||
repaint();
|
||||
$('triage-access-line')?.focus?.();
|
||||
}
|
||||
|
||||
function markup() {
|
||||
const body = data === null
|
||||
? `<p class="small warn">${esc(readLine || 'The Triage route could not be read.')}</p><p role="status" id="triage-route-line" tabindex="-1">${esc(line)}</p>`
|
||||
: `${stored()}${form()}<p class="small muted" id="triage-route-note">${esc(data.note ?? 'Saving a route does not start Triage. Triage also needs its key.')}</p>`;
|
||||
: `${stored()}${access()}${form()}<p class="small muted" id="triage-route-note">${esc(data.note ?? 'Saving a route does not start Triage. Triage also needs its key.')}</p>`;
|
||||
return `<section class="panel" id="triage-route" aria-labelledby="triage-route-title"><h3 id="triage-route-title">Triage model route</h3>${body}</section>`;
|
||||
}
|
||||
const repaint = () => { const panel = $('triage-route'); if (panel) panel.outerHTML = markup(); };
|
||||
@@ -93,7 +132,7 @@
|
||||
if (r.ok && a?.route) {
|
||||
data = { ...data, route: a.route, revision: a.revision, routeMessage: undefined };
|
||||
draft = fromRoute(a.route); base = a.revision;
|
||||
line = `Saved ${savedAt(a.route.updatedAt)}.`;
|
||||
line = `Saved ${savedAt(a.route.updatedAt)}.` + (a.key?.error ? ` ${a.key.message}` : ACTION_LINE[a.key?.action] ? ` ${ACTION_LINE[a.key.action]}` : '');
|
||||
repaint();
|
||||
$('triage-route-line')?.focus?.();
|
||||
return;
|
||||
@@ -127,7 +166,11 @@
|
||||
repaint(); $('triage-route-model')?.focus();
|
||||
});
|
||||
document.addEventListener('submit', e => { if (e.target?.id !== 'triage-route-form') return; e.preventDefault(); save(); });
|
||||
document.addEventListener('click', e => { if (e.target?.id === 'triage-route-reload') reload(); });
|
||||
document.addEventListener('click', e => {
|
||||
if (e.target?.id === 'triage-route-reload') reload();
|
||||
if (e.target?.id === 'triage-access-repair') accessOp('repair');
|
||||
if (e.target?.id === 'triage-access-off') accessOp('off');
|
||||
});
|
||||
|
||||
const hasDraft = () => !!(draft && data && JSON.stringify(draft) !== JSON.stringify(fromRoute(data.route)));
|
||||
window.MosaicTriageRoute = { section, hasDraft };
|
||||
|
||||
@@ -1051,7 +1051,8 @@ export async function startServer({ host = '127.0.0.1', port = 7330, board = DEF
|
||||
origin: sso ? (localOrigin ?? sso.publicOrigin) : `https://${preview.host}`, user: signer?.user,
|
||||
principal: currentSession ? { provider: currentSession.provider, iss: currentSession.iss, sub: currentSession.sub } : null, log });
|
||||
if (path.startsWith('/api/proxy/')) return proxyHTTP(req, res, { path, call: proxyCall, named, tester, granted,
|
||||
send, origin: sso ? (localOrigin ?? sso.publicOrigin) : `https://${preview.host}`, user: signer?.user, log });
|
||||
send, origin: sso ? (localOrigin ?? sso.publicOrigin) : `https://${preview.host}`, user: signer?.user, log,
|
||||
record: proxyAdmin && typeof proxyAdmin.recordAccount === 'function' ? entry => proxyAdmin.recordAccount({ ...entry, org: orgId }) : null });
|
||||
}
|
||||
// #1873 triage-host: the Triage session's two routes answer only the instance owner's own session on
|
||||
// the public name, with no organization selected. Anyone else falls through to the answer an
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
// proxy-scope slice 4 (#1873): the Console's side of the Triage key. The Console never holds the
|
||||
// proxy management key, so it never issues or removes a Triage key itself: it asks one riding
|
||||
// organization's bus to, through the bus verb `proxy.triage.key` (op ensure or revoke), as the owner,
|
||||
// the way Proxy setup's init asks one bus for `proxy.init` (packages/model-proxy/src/admin-control.mjs).
|
||||
// A riding organization lists cliproxy and is not a fixture organization; its bus decides for itself
|
||||
// whether it holds the owner's proxy client, and refuses triage-no-riding-bus when it does not.
|
||||
//
|
||||
// A bus that is down or not riding did nothing, so it is passed over and the next one is asked. A bus
|
||||
// whose outcome is unknown (a timeout, or an answer that could not be read) may have done the work, so
|
||||
// the walk stops there and the caller shows the key file's state. Any other refusal is the answer.
|
||||
// With no bus left the answer is triage-no-riding-bus.
|
||||
//
|
||||
// The key file is read only through packages/bus/src/triage-key-file.mjs, the reader with no proxy
|
||||
// client and no lock. No answer here holds a key: only ids, days and codes.
|
||||
import { readKey } from '../../bus/src/triage-key-file.mjs';
|
||||
|
||||
export const NO_RIDING_MESSAGE = "Triage needs one organization that uses the owner's models";
|
||||
// A bus sweep runs every 30 s; a rotation still not done this long after its day began means no
|
||||
// riding bus could act.
|
||||
export const OVERDUE_ALLOWANCE_MS = 10 * 60 * 1000;
|
||||
// A refusal that says only "not this bus": ask the next one.
|
||||
const NEXT = new Set(['triage-no-riding-bus', 'not-configured', 'no-bus-host', 'unknown-verb', 'bus-unavailable']);
|
||||
// An outcome the Console cannot know (webui bus.mjs OPEN): stop, and answer outcome-unknown.
|
||||
const OPEN = new Set(['outcome-unknown', 'invalid-response', 'response-too-large']);
|
||||
const ACTIONS = new Set(['issued', 'held', 'replaced', 'revoked', 'none']);
|
||||
const KEY_ID = /^[0-9a-f]{12}$/;
|
||||
const DAY = /^\d{4}-\d{2}-\d{2}$/;
|
||||
|
||||
const ids = v => (Array.isArray(v) ? v.filter(x => typeof x === 'string' && KEY_ID.test(x)) : []);
|
||||
// The fields the Console keeps from a bus answer, re-checked here: ids and a day, never a key.
|
||||
const clean = (op, a) => ({
|
||||
ok: true, op,
|
||||
action: ACTIONS.has(a?.action) ? a.action : 'unknown',
|
||||
addedKeyIds: ids(a?.addedKeyIds), removedKeyIds: ids(a?.removedKeyIds),
|
||||
...(op === 'ensure' && typeof a?.keyId === 'string' && KEY_ID.test(a.keyId) ? { keyId: a.keyId } : {}),
|
||||
...(op === 'ensure' && typeof a?.rotateBy === 'string' && DAY.test(a.rotateBy) ? { rotateBy: a.rotateBy } : {}),
|
||||
});
|
||||
|
||||
// triageKeyCall({ riders, op }) -> { ok: true, op, action, addedKeyIds, removedKeyIds, keyId?, rotateBy?, org }
|
||||
// | { ok: false, code, org? }
|
||||
// riders async () -> [{ id, fixture, cliproxy, call }] (webui cli.mjs rideRiders)
|
||||
// The organizations whose bus may hold the Triage key: they list cliproxy and are not fixtures. The
|
||||
// status reads the same list, so it says no riding organization exactly when the walk has none to ask.
|
||||
export const ridingOrgs = list => (Array.isArray(list) ? list : []).filter(r => r?.cliproxy === true && r.fixture !== true && typeof r.call === 'function');
|
||||
|
||||
export async function triageKeyCall({ riders, op }) {
|
||||
if (op !== 'ensure' && op !== 'revoke') return { ok: false, code: 'invalid-request' };
|
||||
let list = [];
|
||||
try { list = await riders(); } catch { list = []; }
|
||||
for (const r of ridingOrgs(list)) {
|
||||
let answer;
|
||||
try { answer = await r.call('proxy.triage.key', { op }); }
|
||||
catch (e) {
|
||||
const code = typeof e?.code === 'string' && /^[a-z][a-z0-9-]{0,63}$/.test(e.code) ? e.code : 'outcome-unknown';
|
||||
if (NEXT.has(code)) continue;
|
||||
return { ok: false, code: OPEN.has(code) ? 'outcome-unknown' : code, org: r.id };
|
||||
}
|
||||
return { ...clean(op, answer), org: r.id };
|
||||
}
|
||||
return { ok: false, code: 'triage-no-riding-bus' };
|
||||
}
|
||||
|
||||
// The key's state from the file alone, for Settings > Triage: never the key, only whether one is
|
||||
// held, its id, its rotation day, how many old keys are in grace, and whether the rotation is overdue.
|
||||
// { key: 'missing' } | { key: 'unsafe' } | { key: 'issued', keyId, rotateBy, retiring, overdue }
|
||||
export function keyState(configDir, { now = () => Date.now(), keyOptions = {} } = {}) {
|
||||
let record;
|
||||
try { record = readKey(configDir, keyOptions); } catch { return { key: 'unsafe' }; }
|
||||
if (!record) return { key: 'missing' };
|
||||
const due = Date.parse(`${record.rotateBy}T00:00:00Z`);
|
||||
return {
|
||||
key: 'issued', keyId: record.keyId, rotateBy: record.rotateBy, retiring: record.retiring.length,
|
||||
due: Number.isFinite(due) && now() >= due,
|
||||
overdue: Number.isFinite(due) && now() > due + OVERDUE_ALLOWANCE_MS,
|
||||
};
|
||||
}
|
||||
|
||||
// The status Settings > Triage shows: the key state, and the named code when no organization rides
|
||||
// the owner's models (so no bus can issue, rotate or retire the key). The rotation is overdue when
|
||||
// its day has begun and no riding bus can act, or when it is still not done well after (a riding
|
||||
// organization whose bus did not rotate it).
|
||||
export function accessStatus(state, { riding }) {
|
||||
const out = { ...state, riding: riding === true };
|
||||
if (riding !== true) Object.assign(out, { code: 'triage-no-riding-bus', message: NO_RIDING_MESSAGE });
|
||||
if (state.key === 'issued' && (state.overdue || (state.due && riding !== true))) out.rotation = 'rotation overdue';
|
||||
return out;
|
||||
}
|
||||
@@ -91,6 +91,12 @@ export const USAGE_NOT_READ = "Triage could not read its token usage or its cap,
|
||||
// #1873 triage-wiring: two turns in a row whose usage reads unknown end the session (cap fail closed).
|
||||
export const UNKNOWN_TURNS_MAX = 2;
|
||||
export const USAGE_UNKNOWN_ENDED = "Triage ended this session: the token usage of two turns in a row could not be read, so the daily cap could not be kept. The next message starts a new session.";
|
||||
// proxy-scope slice 4 (#1873): a session still running on a key whose grace has ended (its retireAt
|
||||
// in the key file has passed, or the key is gone from the file) stops with this code, before the bus
|
||||
// sweep removes the key from the proxy (the sweep waits 30 s past retireAt).
|
||||
export const KEY_ROTATED_CODE = "triage-key-rotated";
|
||||
export const KEY_ROTATED = "Triage stopped this session: its key was rotated and the old key's grace ended. The next message starts a new session on the current key.";
|
||||
export const KEY_POLL_MS = 5000;
|
||||
// #1873 triage-wiring: the route's refusals (triage-route.mjs), each with the one plain line the
|
||||
// drawer shows. None carries a path, a key or an env value.
|
||||
export const ROUTE_REFUSALS = Object.freeze({
|
||||
@@ -121,8 +127,8 @@ const CAP = /^[0-9a-f]{64}$/;
|
||||
// The spawn's environment: these names from the Console's own environment, PATH fixed, and the
|
||||
// route's proxy variables. bwrap sets HOME and TMPDIR inside.
|
||||
export const SPAWN_ENV_ALLOW = Object.freeze(["PATH", "LANG", "LC_ALL", "TZ", "TERM"]);
|
||||
// Passed only as an absolute path: the pinned harness binary (packages/seat/src/session.mjs ENV_ALLOW).
|
||||
export const SPAWN_PATH_ALLOW = Object.freeze(["MOSAIC_CLAUDE_BIN"]);
|
||||
// Passed only as an absolute path: a harness binary the Console names (packages/seat/src/session.mjs ENV_ALLOW).
|
||||
export const SPAWN_PATH_ALLOW = Object.freeze(["MOSAIC_CLAUDE_BIN", "MOSAIC_PI_BIN"]);
|
||||
// The names the runner sets for every harness (runner.mjs adapterEnv); the host's own names never replace them.
|
||||
const RUNNER_NAMES = new Set(["MOSAIC_SYSTEM_PROMPT_FILE", "MOSAIC_WORKSPACE", "MOSAIC_SESSION_DIR", "MOSAIC_POLICY_FILE", "MOSAIC_TOOLS_FILE",
|
||||
"MOSAIC_TOOL_SOCKET", "MOSAIC_TURN_MARKER", "MOSAIC_PROVIDER", "MOSAIC_MODEL", "MOSAIC_MODEL_ACTIVITY_DIR", "MOSAIC_UNSLOP_LOG",
|
||||
@@ -323,13 +329,18 @@ export function createRelay({ socket, cap, take, read, onSend, usage = null, sto
|
||||
// repository's packages/plugins/harness/<name> by default
|
||||
// home HOME, which no mount may reach outside the stack's own code; the Console user's by default
|
||||
// usageSource { readTurn({ harness, root }) } the transcript usage-source; the repository's own by default
|
||||
export function createTriageHost({ dataRoot, route = null, readSnapshot = async () => null, env = process.env, node = process.execPath, bwrap = BWRAP, now = () => new Date(), devOwner = false, log = () => {}, roleDir = join(REPO, "roles"), readOnly = [], pollInterval = null, proxyPort = null, tokenPattern = null, harnessModule = repoHarnessModule, home = homedir(), usageSource = { readTurn } }) {
|
||||
// keyRecord () -> the Triage key file's record (packages/bus/src/triage-key-file.mjs readKey) or
|
||||
// null; throws when unsafe. With it, a session on a key past its retireAt, or a key
|
||||
// gone from the file, stops with triage-key-rotated. Without it, nothing is checked.
|
||||
// keyPollMs how often a running session's key is checked
|
||||
export function createTriageHost({ dataRoot, route = null, readSnapshot = async () => null, env = process.env, node = process.execPath, bwrap = BWRAP, now = () => new Date(), devOwner = false, log = () => {}, roleDir = join(REPO, "roles"), readOnly = [], pollInterval = null, proxyPort = null, tokenPattern = null, harnessModule = repoHarnessModule, home = homedir(), usageSource = { readTurn }, keyRecord = null, keyPollMs = KEY_POLL_MS }) {
|
||||
if (typeof dataRoot !== "string" || !dataRoot.startsWith("/")) throw new TriageError("triage-no-data-root", "no data root");
|
||||
const validPort = (p) => Number.isInteger(p) && p > 0 && p <= 65535;
|
||||
if (proxyPort !== null && typeof proxyPort !== "function" && !validPort(proxyPort)) throw new TypeError("proxyPort must be a port number, null or a function");
|
||||
if (tokenPattern !== null && !(tokenPattern instanceof RegExp)) throw new TypeError("tokenPattern must be a RegExp or null");
|
||||
if (typeof harnessModule !== "function") throw new TypeError("harnessModule must be a function");
|
||||
if (typeof usageSource?.readTurn !== "function") throw new TypeError("usageSource must have readTurn");
|
||||
if (keyRecord !== null && typeof keyRecord !== "function") throw new TypeError("keyRecord must be a function or null");
|
||||
const tokenShape = tokenPattern && new RegExp(`^(?:${tokenPattern.source})$`, tokenPattern.flags.replace(/[gmy]/g, ""));
|
||||
const tokenAnywhere = tokenPattern && new RegExp(tokenPattern.source, tokenPattern.flags.replace(/[gmy]/g, "") + "g");
|
||||
// What no mount source may lie in or around, besides the run directory and the fixed list. HOME
|
||||
@@ -355,6 +366,11 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
// host/ too, so a Console restart between the end and the next message keeps it.
|
||||
let freshSession = false;
|
||||
const badSeen = new Set();
|
||||
// proxy-scope slice 4: the key the running session holds (memory only, never written or logged),
|
||||
// the timer that checks it, and the last stop the host made for a rotated key.
|
||||
let heldKey = null;
|
||||
let keyTimer = null;
|
||||
let lastStop = null;
|
||||
|
||||
const ensureDirs = () => {
|
||||
mkdirSync(dataRoot, { recursive: true, mode: 0o700 });
|
||||
@@ -765,8 +781,61 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
return true;
|
||||
};
|
||||
|
||||
// proxy-scope slice 4. Where the held key stands in the key file: "current", the retireAt (ms) of
|
||||
// the retiring entry that holds it, "gone", or "unknown" (no reader, no key, or an unsafe file:
|
||||
// nothing is decided on a file that cannot be read; the next admit refuses it).
|
||||
const keyStanding = (held) => {
|
||||
if (!keyRecord || typeof held !== "string" || !held) return "unknown";
|
||||
let r;
|
||||
try { r = keyRecord(); } catch { return "unknown"; }
|
||||
if (!r) return "gone";
|
||||
if (r.key === held) return "current";
|
||||
const old = Array.isArray(r.retiring) ? r.retiring.find((x) => x?.key === held) : null;
|
||||
if (!old) return "gone";
|
||||
const at = Date.parse(old.retireAt);
|
||||
return Number.isFinite(at) ? at : "gone";
|
||||
};
|
||||
const keyDue = (held) => {
|
||||
const s = keyStanding(held);
|
||||
return s === "gone" || (typeof s === "number" && s <= now().getTime());
|
||||
};
|
||||
// SIGTERM, then SIGKILL after the grace; resolves when the process has exited (or 2 s after SIGKILL).
|
||||
const endChild = (c, graceMs) => new Promise((resolve) => {
|
||||
if (!c || c.exitCode !== null || c.signalCode !== null) return resolve();
|
||||
let kill = null, last = null;
|
||||
c.once("exit", () => { clearTimeout(kill); clearTimeout(last); resolve(); });
|
||||
try { process.kill(-c.pid, "SIGTERM"); } catch {}
|
||||
kill = setTimeout(() => {
|
||||
try { process.kill(-c.pid, "SIGKILL"); } catch {}
|
||||
last = setTimeout(resolve, 2000);
|
||||
}, graceMs);
|
||||
});
|
||||
// The session on a rotated key stops: a host line says why, the runner is told its session is gone
|
||||
// at its next state or receive, and the process group ends. Queued messages get their lines at exit.
|
||||
let rotating = null;
|
||||
const stopRotated = (c, { graceMs = 5000 } = {}) => rotating ??= (async () => {
|
||||
if (!c || child !== c) return;
|
||||
if (usage) usage.halted = true;
|
||||
lastStop = { code: KEY_ROTATED_CODE, at: now().toISOString() };
|
||||
log(`triage host: stopping the session: ${KEY_ROTATED_CODE}`);
|
||||
try { append({ at: now().toISOString(), from: "host", text: KEY_ROTATED }); }
|
||||
catch { log("triage host: transcript write failed"); }
|
||||
await endChild(c, graceMs);
|
||||
})().finally(() => { rotating = null; });
|
||||
const clearKeyTimer = () => { if (keyTimer) clearInterval(keyTimer); keyTimer = null; };
|
||||
|
||||
const start = async ({ admitted, installRoots, transcriptRoot, mod }) => {
|
||||
ensureDirs();
|
||||
// #1873 pi-unpin re-check 7: a module whose env() reads a check it runs first (pi's fit check)
|
||||
// has it run and awaited before the bundle, which calls env(). A refusal or a throw refuses the
|
||||
// route, as a throw from env() does.
|
||||
if (typeof mod?.ready === "function") {
|
||||
try { await mod.ready(); }
|
||||
catch {
|
||||
log("triage host: route refused: harness-bundle");
|
||||
throw new TriageError("triage-route-invalid", "the model route's answer is not usable");
|
||||
}
|
||||
}
|
||||
const { run, harnessFiles } = writeBundle(admitted, mod);
|
||||
// The key this session holds, and anything shaped like a key, never reaches the transcript.
|
||||
const key = routeKey(admitted.env)?.value ?? null;
|
||||
@@ -837,6 +906,8 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
if (child !== mine) return;
|
||||
lastExit = { code: Number.isInteger(code) ? code : null, signal: signal ?? null, at: now().toISOString() };
|
||||
child = null;
|
||||
heldKey = null;
|
||||
clearKeyTimer();
|
||||
// A turn the session began and never ended (killed, crashed) still spent tokens: it gets its line.
|
||||
// The open turns are read when this runs, after any usage.end already in the chain.
|
||||
chainUsage(async () => { for (const k of [...u.baselines.keys()]) await finishTurn(u, k); });
|
||||
@@ -857,6 +928,13 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
log(`triage host: launcher did not run: ${e.code ?? "error"}`);
|
||||
throw new TriageError("triage-start-failed", "the Triage session did not start");
|
||||
}
|
||||
// proxy-scope slice 4: the key this session holds is checked while it runs.
|
||||
heldKey = key;
|
||||
clearKeyTimer();
|
||||
if (keyRecord && key) {
|
||||
keyTimer = setInterval(() => { if (child === mine && keyDue(key)) stopRotated(mine).catch(() => {}); }, keyPollMs);
|
||||
keyTimer.unref?.();
|
||||
}
|
||||
};
|
||||
|
||||
// The cap is the route's to count. Once it answers over the cap with a time in the next two days,
|
||||
@@ -908,6 +986,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
resetsAt: unset ? "not set" : c.resetsAt ?? "not read",
|
||||
running: child !== null,
|
||||
lastExit,
|
||||
lastStop,
|
||||
snapshot: last,
|
||||
transcript: transcript(),
|
||||
};
|
||||
@@ -935,6 +1014,9 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
try { append({ at: now().toISOString(), from: "host", in_reply_to: id, text: e?.code === "triage-route-invalid" ? ROUTE_REFUSED : NOT_STARTED }); } catch {}
|
||||
throw e instanceof TriageError ? e : new TriageError("triage-route-invalid", "the model route's answer is not usable");
|
||||
}
|
||||
// proxy-scope slice 4: a session on a key past its grace stops before this message is queued,
|
||||
// so the message starts a new session on the key the route answered with.
|
||||
if (child && keyDue(heldKey)) await stopRotated(child);
|
||||
queue.push({ id, body: text, at, delivered: false });
|
||||
if (!child) {
|
||||
starting ??= start(checked).finally(() => { starting = null; });
|
||||
@@ -952,6 +1034,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
return { id, at };
|
||||
},
|
||||
async stop({ graceMs = 5000 } = {}) {
|
||||
clearKeyTimer();
|
||||
const c = child;
|
||||
if (c) {
|
||||
try { process.kill(-c.pid, "SIGTERM"); } catch {}
|
||||
|
||||
@@ -19,8 +19,12 @@ import { fileURLToPath } from 'node:url';
|
||||
import { checkRoutes } from '../../organization/src/launch-settings.mjs';
|
||||
import { isDailyModelQuota } from '../../plugins/src/model-quota.mjs';
|
||||
import { readRoute, writeRoute } from './triage-route-store.mjs';
|
||||
import { NO_RIDING_MESSAGE, accessStatus } from './triage-access.mjs';
|
||||
|
||||
export const TRIAGE_ROUTE_PATH = '/api/triage/route';
|
||||
// proxy-scope slice 4 (#1873): Repair access (ensure) and Turn Triage off (stop, then revoke).
|
||||
export const TRIAGE_ACCESS_PATH = '/api/triage/access';
|
||||
export const ACCESS_OPS = Object.freeze(['repair', 'off']);
|
||||
export const BODY_MAX = 16384;
|
||||
export const POST_KEYS = Object.freeze(['model', 'harness', 'cap', 'timeZone', 'revision']);
|
||||
export const NOT_SET_MESSAGE = 'No Triage route is set.';
|
||||
@@ -63,6 +67,7 @@ const STATUS = Object.freeze({
|
||||
'triage-route-unreadable': 503,
|
||||
'triage-route-unrecorded': 500,
|
||||
'outcome-unknown': 503,
|
||||
'triage-no-riding-bus': 503,
|
||||
});
|
||||
// Fixed words for each code. The store's own messages name lock files, so none is forwarded.
|
||||
const MESSAGES = Object.freeze({
|
||||
@@ -85,7 +90,16 @@ const MESSAGES = Object.freeze({
|
||||
'triage-route-unreadable': 'The stored route could not be read.',
|
||||
'triage-route-unrecorded': 'The route was saved, but its receipt line was not written.',
|
||||
'outcome-unknown': 'The route service did not answer.',
|
||||
'triage-no-riding-bus': `${NO_RIDING_MESSAGE}. Start an organization that uses them, then choose Repair access.`,
|
||||
});
|
||||
// proxy-scope slice 4: a bus refusal of proxy.triage.key, in fixed words. The code is a bus code
|
||||
// (lowercase and dashes); nothing from the bus's text is shown.
|
||||
const keyFailed = code => code === 'triage-no-riding-bus' ? MESSAGES['triage-no-riding-bus']
|
||||
: `The Triage key was not changed. The organization's bus refused with ${/^[a-z][a-z0-9-]{0,63}$/.test(code ?? '') ? code : 'an unknown code'}.`;
|
||||
// What a key step answers in a response: ids, action and day, or the code and its fixed words.
|
||||
const keyView = r => r?.ok === true
|
||||
? { action: r.action, ...(r.keyId ? { keyId: r.keyId } : {}), ...(r.rotateBy ? { rotateBy: r.rotateBy } : {}) }
|
||||
: { error: r?.code ?? 'outcome-unknown', message: keyFailed(r?.code) };
|
||||
|
||||
const json = (res, status, value) => {
|
||||
res.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' });
|
||||
@@ -191,7 +205,25 @@ async function readBody(req) {
|
||||
|
||||
// Registers both routes. `catalogues` returns each active organization's `routes` catalogue (or a
|
||||
// status when its bus did not answer); `harnesses` lists the installed harness modules.
|
||||
export function triageRouteRoutes({ dataRoot, catalogues, harnesses = installedHarnesses, log = () => {} }) {
|
||||
// proxy-scope slice 4: `access`, when given, is the Triage key's side:
|
||||
// state() the key file's state (triage-access.mjs keyState), never the key
|
||||
// riding() async -> true when one organization rides the owner's models
|
||||
// key(op) async -> triageKeyCall's answer for op ensure or revoke, through one riding bus
|
||||
// stop() async -> stops a running Triage session (the host's stop)
|
||||
// A route save with no key held runs ensure once; POST /api/triage/access runs Repair access
|
||||
// (ensure) or Turn Triage off (stop, then revoke). With no access, neither happens.
|
||||
export function triageRouteRoutes({ dataRoot, catalogues, harnesses = installedHarnesses, log = () => {}, access = null }) {
|
||||
const accessNow = async () => {
|
||||
if (!access) return undefined;
|
||||
let state;
|
||||
try { state = access.state(); } catch { state = { key: 'unsafe' }; }
|
||||
let riding = false;
|
||||
try { riding = (await access.riding()) === true; } catch { riding = false; }
|
||||
return accessStatus(state, { riding });
|
||||
};
|
||||
const keyStep = async op => {
|
||||
try { return await access.key(op); } catch { return { ok: false, code: 'outcome-unknown' }; }
|
||||
};
|
||||
async function catalogue() {
|
||||
let list;
|
||||
try { list = await catalogues(); } catch { list = []; }
|
||||
@@ -222,9 +254,11 @@ export function triageRouteRoutes({ dataRoot, catalogues, harnesses = installedH
|
||||
let doc;
|
||||
try { doc = readRoute(dataRoot); } catch (error) { return refuse(error?.code === 'triage-route-unreadable' ? 'triage-route-unreadable' : 'outcome-unknown'); }
|
||||
const route = view(doc);
|
||||
const status = await accessNow();
|
||||
return done(200, {
|
||||
route, ...(route ? {} : { routeMessage: NOT_SET_MESSAGE }), revision: route ? route.revision : 0,
|
||||
choices: choices(await catalogue()), send: context.send === true, note: NOT_STARTED_NOTE,
|
||||
...(status ? { access: status } : {}),
|
||||
});
|
||||
}
|
||||
if (context.send !== true) return refuse('read-only');
|
||||
@@ -247,7 +281,17 @@ export function triageRouteRoutes({ dataRoot, catalogues, harnesses = installedH
|
||||
if (by === null) return refuse('human-required');
|
||||
try {
|
||||
const doc = writeRoute(dataRoot, { model, harness, limit: cap, timeZone: zone, by, revision });
|
||||
return done(200, { route: view(doc), revision: doc.revision, note: NOT_STARTED_NOTE });
|
||||
// proxy-scope slice 4: the owner's first save (the one that creates the route, at revision 0),
|
||||
// with no key held, asks a riding bus for one. A later save never does: after Turn Triage off
|
||||
// only Repair access brings a key back. A key that is not issued leaves the route saved; the
|
||||
// answer says why.
|
||||
let key;
|
||||
if (access && revision === 0) {
|
||||
let state;
|
||||
try { state = access.state(); } catch { state = { key: 'unsafe' }; }
|
||||
if (state.key === 'missing') key = keyView(await keyStep('ensure'));
|
||||
}
|
||||
return done(200, { route: view(doc), revision: doc.revision, note: NOT_STARTED_NOTE, ...(key ? { key } : {}) });
|
||||
} catch (error) {
|
||||
const code = typeof error?.code === 'string' && Object.hasOwn(MESSAGES, error.code) && error.code.startsWith('triage-route-') ? error.code : 'outcome-unknown';
|
||||
if (code !== 'triage-route-stale') return refuse(code);
|
||||
@@ -256,7 +300,40 @@ export function triageRouteRoutes({ dataRoot, catalogues, harnesses = installedH
|
||||
return refuse(code, { revision: stored ? stored.revision : 0 });
|
||||
}
|
||||
}
|
||||
// POST /api/triage/access { op: 'repair' | 'off' }: the owner's Repair access and Turn Triage off.
|
||||
async function handleAccess(req, res, context) {
|
||||
const done = (status, value) => {
|
||||
log(`${new Date().toISOString()} ${req.method} ${TRIAGE_ACCESS_PATH} ${status}`);
|
||||
return json(res, status, value);
|
||||
};
|
||||
const refuse = code => done(STATUS[code] ?? 503, { error: code, message: MESSAGES[code] ?? MESSAGES['outcome-unknown'] });
|
||||
if (req.method !== 'POST') return refuse('method-not-allowed');
|
||||
if (context.owner !== true || !context.session) return refuse('human-required');
|
||||
if (one(req, 'origin') !== context.origin) return refuse('cross-origin');
|
||||
if (one(req, 'sec-fetch-site') !== 'same-origin') return refuse('cross-site');
|
||||
if (context.send !== true) return refuse('read-only');
|
||||
if (!access) return refuse('outcome-unknown');
|
||||
let op;
|
||||
try {
|
||||
if ((req.headers['content-type'] ?? '').split(';')[0].trim().toLowerCase() !== 'application/json') throw Error();
|
||||
const chunks = []; let size = 0;
|
||||
for await (const chunk of req) { size += chunk.length; if (size > BODY_MAX) throw Error(); chunks.push(chunk); }
|
||||
const body = JSON.parse(Buffer.concat(chunks));
|
||||
if (!plain(body) || Object.keys(body).length !== 1 || !ACCESS_OPS.includes(body.op)) throw Error();
|
||||
op = body.op;
|
||||
} catch { return done(400, { error: 'invalid-request', message: 'The request takes op, repair or off.' }); }
|
||||
if (op === 'off') {
|
||||
// The session stops first, so nothing runs on a key while it is removed.
|
||||
try { await access.stop(); } catch { log('triage access: the session stop failed'); }
|
||||
}
|
||||
const result = await keyStep(op === 'repair' ? 'ensure' : 'revoke');
|
||||
const key = keyView(result);
|
||||
const status = await accessNow();
|
||||
if (result?.ok === true) return done(200, { op, key, access: status });
|
||||
return done(STATUS[result?.code] ?? 502, { op, ...key, key, access: status });
|
||||
}
|
||||
return registered => {
|
||||
registered.add('POST', TRIAGE_ACCESS_PATH, handleAccess);
|
||||
registered.add('GET', TRIAGE_ROUTE_PATH, handle);
|
||||
registered.add('POST', TRIAGE_ROUTE_PATH, handle);
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// #1873 triage-wiring: the Triage host's model route, resolved again at each admit (design report
|
||||
// reports/triage-route-design.md slice 5). Four sources, each read fresh:
|
||||
// the route store harness name, model key and cap (triage-route-store.mjs)
|
||||
// the key file <configDir>/secrets/triage/model (packages/cli/src/triage-key-file.mjs, the
|
||||
// the key file <configDir>/secrets/triage/model (packages/bus/src/triage-key-file.mjs, the
|
||||
// key verbs' own reader and checks; never the module that holds the proxy client)
|
||||
// the model catalogue model and provider for the key, read from each active organization's bus
|
||||
// `routes` verb and merged (mergeCatalogues), then held to checkRoutes, the
|
||||
@@ -32,7 +32,7 @@ import { lstatSync, readdirSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { checkRoutes } from "../../organization/src/launch-settings.mjs";
|
||||
import { SECRET, keyPrefix, readKey } from "../../cli/src/triage-key-file.mjs";
|
||||
import { SECRET, keyPrefix, readKey } from "../../bus/src/triage-key-file.mjs";
|
||||
import { readRoute } from "./triage-route-store.mjs";
|
||||
import { ROUTE_KEY_NAMES, ROUTE_REFUSALS, refusedHarnessName } from "./triage-host.mjs";
|
||||
import { checkedTransport } from "../../harness/src/bundle.mjs";
|
||||
@@ -228,6 +228,9 @@ export function createTriageRoute({ dataRoot, configDir, proxyPort = null, catal
|
||||
// name the host's fence refuses (MOSAIC_* and ANTHROPIC_* among them: the host sets those)
|
||||
// and every name the module does not declare.
|
||||
const session = { model: offered.model, provider: offered.provider ?? "", ...(transport ? { proxyTransport: { ...transport } } : {}) };
|
||||
// #1873 pi-unpin re-check 7: a module whose env() reads a check it runs first (pi's fit
|
||||
// check) has it run here and awaited; a refusal or a throw is incompatible, as env()'s is.
|
||||
if (typeof mod.ready === "function") await mod.ready();
|
||||
const given = mod.env({ files: {}, policy: { tools: [] }, tools: [], session, models: {}, proxy: { ...PROXY_PROBE } });
|
||||
keyName = routeKeyName(given);
|
||||
if (!keyName) return refuse("triage-harness-incompatible");
|
||||
|
||||
@@ -185,6 +185,17 @@ test('dev owner resolves a decision through the real Console wrapper and bus', {
|
||||
const result=await post(`/api/orgs/acme/decisions/${decision.id}/resolve`,{choice:'yes',confirmation:confirmation.body.confirmation});
|
||||
assert.equal(result.status,200,JSON.stringify(result));
|
||||
assert.equal(result.body.choice,'yes');
|
||||
// settings-backend A2: the real Console's riders carry the organization file, so turning a program
|
||||
// off for every organization can read it; without it each organization answers unreadable.
|
||||
const get=path=>new Promise((resolve,reject)=>{const host=`127.0.0.1:${port}`;const req=request({host:'127.0.0.1',port,path,headers:{host}},res=>{let raw='';res.on('data',c=>raw+=c);res.on('end',()=>resolve({status:res.statusCode,body:JSON.parse(raw)}));});req.on('error',reject);req.end();});
|
||||
const status=await get('/api/orgs/acme/proxy/admin/status');
|
||||
assert.equal(status.status,200,JSON.stringify(status));
|
||||
const acmeOn=status.body.programs?.organizations?.find(o=>o.id==='acme')?.on;
|
||||
assert.ok(Array.isArray(acmeOn)&&acmeOn.length>0,JSON.stringify(status.body.programs));
|
||||
const off=await post('/api/orgs/acme/proxy/admin/programs/off',{name:acmeOn[0]});
|
||||
assert.equal(off.status,200,JSON.stringify(off));
|
||||
// The chief's route uses the program in both organizations, so neither file is written.
|
||||
assert.deepEqual(off.body.results,[{id:'acme',outcome:'in-use'},{id:'beta',outcome:'in-use'}],JSON.stringify(off.body));
|
||||
const stopped = new Promise(resolve => bus.once('exit', resolve));
|
||||
const stop = spawnSync(process.execPath,['packages/cli/src/cli.mjs','bus','stop','acme'],{env,encoding:'utf8'}); assert.equal(stop.status,0,stop.stderr); await stopped;
|
||||
busOut=''; bus=spawn(process.execPath,['packages/cli/src/cli.mjs','bus','start','acme','--no-launch'],{env,stdio:['ignore','pipe','pipe']});
|
||||
|
||||
@@ -20,7 +20,13 @@ const ROUTES = [
|
||||
['fence/clear', 'POST', {}], ['init-lock/clear', 'POST', {}],
|
||||
// #1873 ride-default: Ride all, and the move of one stopped fixture organization.
|
||||
['ride-all', 'POST', {}], ['ride-move', 'POST', { org: 'stand-in' }],
|
||||
// #1873 settings-backend A1: the model service log, the service key's made-at time, setup history.
|
||||
['log', 'GET'], ['service-key', 'GET'], ['history', 'GET'],
|
||||
// #1873 settings-backend A2: one program off for every organization, and access health.
|
||||
['programs/off', 'POST', { name: 'alpha' }], ['access', 'GET'],
|
||||
];
|
||||
const A2 = ROUTES.filter(([p]) => ['programs/off', 'access'].includes(p));
|
||||
const READS = ROUTES.filter(([p]) => ['log', 'service-key', 'history'].includes(p));
|
||||
|
||||
function fakeAdmin() {
|
||||
const calls = [];
|
||||
@@ -33,6 +39,12 @@ function fakeAdmin() {
|
||||
fetchStart: () => { calls.push(['fetchStart']); return { state: 'running' }; },
|
||||
init: async (call, args) => { calls.push(['init', args]); await call('proxy.init', args); return { settings: { port: 18432 }, receipt: { op: 'init' } }; },
|
||||
fenceClear: async call => { calls.push(['fenceClear']); return call('proxy.fence.clear', { evidence: {} }); },
|
||||
log: async args => { calls.push(['log', args]); return { unit: 'mosaic-proxy.service', lines: ['ok'], redacted: 0, dropped: 0 }; },
|
||||
serviceKey: () => { calls.push(['serviceKey']); return { madeAt: '2026-11-01T08:00:00.000Z', renewedAt: null, renewal: 'not-built', problem: null }; },
|
||||
history: args => { calls.push(['history', args]); return { entries: [], next: null }; },
|
||||
recordAccount: entry => { calls.push(['recordAccount', entry]); return true; },
|
||||
programsOff: async args => { calls.push(['programsOff', args]); return { name: args.name, results: [], receipt: null }; },
|
||||
access: async () => { calls.push(['access']); return { organizations: [] }; },
|
||||
};
|
||||
}
|
||||
const send = (port, path, { method = 'GET', headers = {}, body } = {}) => new Promise((resolve, reject) => {
|
||||
@@ -54,7 +66,7 @@ async function devServer(t, options = {}) {
|
||||
}
|
||||
const hit = (f, route, extra = {}) => send(f.port, `/api/proxy/admin/${route[0]}`, { method: route[1], headers: { ...f.headers(), ...extra }, body: route[2] });
|
||||
|
||||
test('the route table is exactly the brief: status, render, fetch, init, four unit verbs, fence clear, lock clear, ride all, ride move', () => {
|
||||
test('the route table is exactly the brief: status, render, fetch, init, four unit verbs, fence clear, lock clear, ride all, ride move, log, service key, history, programs off, access', () => {
|
||||
assert.deepEqual(PROXY_ADMIN_PATHS, ROUTES.map(([p]) => `/api/proxy/admin/${p}`));
|
||||
});
|
||||
|
||||
@@ -64,7 +76,8 @@ test('the dev local owner reaches every route; init and fence clear go to the bu
|
||||
const r = await hit(f, route);
|
||||
assert.equal(r.status, route[0] === 'fetch' ? 202 : 200, route[0]);
|
||||
}
|
||||
assert.deepEqual(f.admin.calls.map(c => c[0]), ['status', 'render', 'fetchStart', 'init', 'unitAction', 'unitAction', 'unitAction', 'unitAction', 'fenceClear', 'initLockClear', 'rideAll', 'rideMove']);
|
||||
assert.deepEqual(f.admin.calls.map(c => c[0]), ['status', 'render', 'fetchStart', 'init', 'unitAction', 'unitAction', 'unitAction', 'unitAction', 'fenceClear', 'initLockClear', 'rideAll', 'rideMove', 'log', 'serviceKey', 'history', 'programsOff', 'access']);
|
||||
assert.deepEqual(f.admin.calls.find(c => c[0] === 'programsOff')[1], { name: 'alpha', user: 'Local operator' });
|
||||
assert.deepEqual(f.admin.calls.filter(c => c[0] === 'unitAction').map(c => c[1]), ['install', 'start', 'stop', 'restart']);
|
||||
assert.deepEqual(f.busCalls.map(c => c[0]), ['proxy.init', 'proxy.fence.clear']);
|
||||
assert.deepEqual(f.busCalls[0][1], { generate: true });
|
||||
@@ -245,3 +258,251 @@ test('a signed-in member who is not the instance owner gets human-required for r
|
||||
}
|
||||
assert.deepEqual(member.admin.calls, []);
|
||||
});
|
||||
|
||||
// #1873 settings-backend A1: the three reads. Query keys are each read's own, once each, plain
|
||||
// decimal numbers; the controller clamps the values.
|
||||
test('the reads pass their query to the controller and refuse an unknown, repeated or non-number key', async t => {
|
||||
const f = await devServer(t);
|
||||
const get = path => send(f.port, path, { headers: f.headers() });
|
||||
assert.equal((await get('/api/proxy/admin/log')).status, 200);
|
||||
assert.equal((await get('/api/proxy/admin/log?lines=1000')).status, 200);
|
||||
assert.equal((await get('/api/proxy/admin/history')).status, 200);
|
||||
assert.equal((await get('/api/proxy/admin/history?limit=1000&before=4096')).status, 200);
|
||||
assert.equal((await get('/api/proxy/admin/service-key')).status, 200);
|
||||
assert.deepEqual(f.admin.calls, [['log', {}], ['log', { lines: 1000 }], ['history', { before: null }], ['history', { limit: 1000, before: 4096 }], ['serviceKey']]);
|
||||
f.admin.calls.length = 0;
|
||||
for (const path of ['/api/proxy/admin/log?lines=-1', '/api/proxy/admin/log?lines=1.5', '/api/proxy/admin/log?lines=', '/api/proxy/admin/log?lines=1e3', '/api/proxy/admin/log?lines=5&lines=6',
|
||||
'/api/proxy/admin/log?limit=5', '/api/proxy/admin/log?unit=sshd.service', '/api/proxy/admin/log?lines=0x10', '/api/proxy/admin/log?lines=1234567890123456',
|
||||
'/api/proxy/admin/history?before=abc', '/api/proxy/admin/history?lines=5', '/api/proxy/admin/history?limit=5&limit=6', '/api/proxy/admin/service-key?x=1', '/api/proxy/admin/service-key?key']) {
|
||||
const r = await get(path);
|
||||
assert.equal(r.status, 400, path); assert.deepEqual(r.json, { error: 'invalid-request' }, path);
|
||||
}
|
||||
assert.deepEqual(f.admin.calls, []);
|
||||
for (const route of READS) assert.equal((await send(f.port, `/api/proxy/admin/${route[0]}`, { method: 'POST', headers: f.headers(), body: {} })).status, 405, route[0]);
|
||||
});
|
||||
|
||||
test('a read the controller cannot answer reaches the page as its code only', async t => {
|
||||
const f = await devServer(t);
|
||||
f.admin.log = async () => { throw Object.assign(new Error(`${SENTINEL} journal said /home/owner/x`), { code: 'outcome-unknown' }); };
|
||||
f.admin.history = () => { throw Object.assign(new Error(SENTINEL), { code: 'invalid-request' }); };
|
||||
f.admin.serviceKey = () => { throw new Error(SENTINEL); };
|
||||
assert.deepEqual((await hit(f, ['log', 'GET'])).json, { error: 'outcome-unknown' });
|
||||
assert.equal((await hit(f, ['log', 'GET'])).status, 503);
|
||||
assert.deepEqual((await hit(f, ['history', 'GET'])).json, { error: 'invalid-request' });
|
||||
assert.deepEqual((await hit(f, ['service-key', 'GET'])).json, { error: 'outcome-unknown' });
|
||||
f.admin.log = async () => { throw Object.assign(new Error('x'), { code: 'not-configured' }); };
|
||||
assert.deepEqual((await hit(f, ['log', 'GET'])).json, { error: 'not-configured' });
|
||||
assert.equal(f.logs.join('\n').includes(SENTINEL), false);
|
||||
});
|
||||
|
||||
test('a member, an organization owner and a request on a name that is not the Console\'s get refused on every read', async t => {
|
||||
const organizations = { list: async () => [], resolve: async id => (id === 'x' ? { busHealthy: true } : null) };
|
||||
for (const session of [{ name: 'Bob', provider: 'local-dev', iss: 'local-dev', sub: 'bob', rootOwner: false, instanceRole: 'member' },
|
||||
{ name: 'Olive', provider: 'local-dev', iss: 'local-dev', sub: 'olive', rootOwner: false, instanceRole: 'operator', orgRole: 'owner' }]) {
|
||||
const f = await devServer(t, { organizations, sso: { ...createDevOwner(), session: () => session } });
|
||||
for (const route of READS) {
|
||||
const inOrg = await send(f.port, `/api/orgs/x/proxy/admin/${route[0]}?lines=5`.replace(/\?lines=5$/, ''), { headers: f.headers() });
|
||||
assert.equal(inOrg.status, 403, `${session.sub} ${route[0]}`); assert.deepEqual(inOrg.json, { error: 'human-required' }, `${session.sub} ${route[0]}`);
|
||||
const bad = await send(f.port, `/api/orgs/x/proxy/admin/${route[0]}?nope=1`, { headers: f.headers() });
|
||||
assert.deepEqual(bad.json, { error: 'human-required' }, 'the caller is checked before the query');
|
||||
const plain = await hit(f, route);
|
||||
assert.equal(plain.status >= 400 && plain.status < 500, true, `${route[0]} outside an organization is refused`);
|
||||
}
|
||||
assert.deepEqual(f.admin.calls, []);
|
||||
}
|
||||
const f = await devServer(t);
|
||||
for (const route of READS) {
|
||||
const r = await send(f.port, `/api/proxy/admin/${route[0]}`, { headers: { host: 'console.evil.example' } });
|
||||
assert.equal(r.status, 403, route[0]);
|
||||
}
|
||||
assert.deepEqual(f.admin.calls, []);
|
||||
});
|
||||
|
||||
// The real controller over a scratch config and data root with keys planted in proxy.json, the
|
||||
// management key file, the receipt history and the journal: none reaches an answer.
|
||||
test('end to end with the real controller, no planted key reaches any read', async t => {
|
||||
const { createProxyAdmin } = await import('../../model-proxy/src/admin-control.mjs');
|
||||
const { initFiles, appendReceipt } = await import('../../model-proxy/src/admin.mjs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'pa-a1-'));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
const files = initFiles(configDir, dataRoot);
|
||||
const TAIL = 'Ab3_dE5-fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7dE';
|
||||
const planted = ['msk-tag-qwertyu', 'mso-tag-acme-qwerty', 'mst-tag-triage-zxcvb', 'msp-tag-plmokn', `msk-k1a2-${TAIL}`, `mso-k1a2-acme-${TAIL}`, `mst-k1a2-triage-${TAIL}`, `msp-k1a2-${TAIL}`];
|
||||
const { mkdirSync, writeFileSync } = await import('node:fs');
|
||||
mkdirSync(join(configDir, 'secrets'), { recursive: true, mode: 0o700 }); mkdirSync(dataRoot, { mode: 0o700 });
|
||||
writeFileSync(files.settings, JSON.stringify({ port: 18432, note: planted[0] }), { mode: 0o600 });
|
||||
writeFileSync(files.key, planted[4], { mode: 0o600 });
|
||||
for (const key of planted) appendReceipt(dataRoot, { time: new Date(0).toISOString(), op: 'init', note: `key ${key}`, [key.replace(/-/g, '_')]: key });
|
||||
const journalCalls = [];
|
||||
const unit = {
|
||||
unit: 'mosaic-proxy-zz.service',
|
||||
status: async () => ({ unit: 'mosaic-proxy-zz.service', installed: true, active: 'active', healthy: true }),
|
||||
log: async (lines, options) => {
|
||||
const { unitLog } = await import('../../model-proxy/src/admin.mjs');
|
||||
return unitLog({ run: async (command, args) => { journalCalls.push([command, ...args]); return { code: 0, stdout: [...planted.map(k => `refused ${k}`), 'auth plainwordsonly', 'served 3 requests'].join('\n') + '\n' }; }, unit: 'mosaic-proxy-zz.service', lines, secrets: options.secrets });
|
||||
},
|
||||
};
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit, buses: async () => ({ stopped: true, running: [] }), env: { MOSAIC_PROXY_TOKEN: 'plainwordsonly' } });
|
||||
const f = await devServer(t, { proxyAdmin: admin });
|
||||
const answers = [];
|
||||
for (const path of ['log', 'log?lines=1000', 'service-key', 'history', 'history?limit=2']) {
|
||||
const r = await send(f.port, `/api/proxy/admin/${path}`, { headers: f.headers() });
|
||||
assert.equal(r.status, 200, path);
|
||||
answers.push(r.text);
|
||||
}
|
||||
const all = answers.join('\n') + f.logs.join('\n');
|
||||
for (const bit of [...planted, ...planted.map(k => k.slice(4)), TAIL.slice(0, 10), 'plainwordsonly', root]) assert.equal(all.includes(bit), false, bit);
|
||||
const log = JSON.parse(answers[0]);
|
||||
assert.deepEqual(log.lines.slice(-1), ['served 3 requests']);
|
||||
assert.equal(log.lines.length, planted.length + 1);
|
||||
assert.equal(log.dropped, 1);
|
||||
assert.deepEqual(journalCalls[1].slice(4, 6), ['-n', '200']);
|
||||
const key = JSON.parse(answers[2]);
|
||||
assert.deepEqual(Object.keys(key), ['madeAt', 'renewedAt', 'renewal', 'problem']);
|
||||
assert.equal(key.problem, null); assert.equal(key.renewal, 'not-built');
|
||||
const first = JSON.parse(answers[4]);
|
||||
assert.equal(first.entries.length, 2);
|
||||
const rest = await send(f.port, `/api/proxy/admin/history?limit=100&before=${first.next}`, { headers: f.headers() });
|
||||
assert.equal(rest.json.entries.length, planted.length - 2);
|
||||
assert.equal(rest.json.next, null);
|
||||
assert.equal(rest.text.includes('qwerty'), false);
|
||||
});
|
||||
|
||||
// Account changes made outside the setup page join its history. The bus call's answer is unchanged;
|
||||
// a record that fails adds recorded: false and still answers the bus's result.
|
||||
test('an account write through /api/proxy/* records a labelled entry; reads and login steps record nothing', async t => {
|
||||
const f = await devServer(t);
|
||||
const post = (path, body) => send(f.port, path, { method: 'POST', headers: f.headers(), body });
|
||||
f.busCalls.length = 0;
|
||||
const added = await post('/api/proxy/apikey/add', { provider: 'openai', key: SENTINEL });
|
||||
assert.equal(added.status, 200);
|
||||
assert.deepEqual(added.json, { cleared: true }, 'the bus answer is passed through as it was');
|
||||
await post('/api/proxy/login/start', { provider: 'openai' });
|
||||
await send(f.port, '/api/proxy/accounts', { headers: f.headers() });
|
||||
const records = f.admin.calls.filter(c => c[0] === 'recordAccount').map(c => c[1]);
|
||||
assert.deepEqual(records.map(r => r.verb), ['proxy.apikey.add', 'proxy.login.start']);
|
||||
assert.deepEqual(records[0], { verb: 'proxy.apikey.add', args: { provider: 'openai', key: SENTINEL }, result: { cleared: true }, user: 'Local operator', org: null });
|
||||
f.admin.recordAccount = () => false;
|
||||
const unrecorded = await post('/api/proxy/apikey/add', { provider: 'openai', key: SENTINEL });
|
||||
assert.deepEqual(unrecorded.json, { cleared: true, recorded: false });
|
||||
f.admin.recordAccount = () => { throw new Error(SENTINEL); };
|
||||
const thrown = await post('/api/proxy/apikey/add', { provider: 'openai', key: SENTINEL });
|
||||
assert.deepEqual(thrown.json, { cleared: true, recorded: false });
|
||||
f.admin.recordAccount = () => null;
|
||||
assert.deepEqual((await post('/api/proxy/login/cancel', { state: 's' })).json, { cleared: true }, 'not an account change: nothing added');
|
||||
assert.equal(f.logs.join('\n').includes(SENTINEL), false);
|
||||
});
|
||||
|
||||
test('a refused or failed account write records nothing, and an organization route names its organization', async t => {
|
||||
const f = await devServer(t, { proxyCall: async () => { throw Object.assign(new Error('x'), { code: 'provider-not-allowed' }); } });
|
||||
const r = await send(f.port, '/api/proxy/apikey/add', { method: 'POST', headers: f.headers(), body: { provider: 'openai', key: SENTINEL } });
|
||||
assert.equal(r.status, 400);
|
||||
assert.deepEqual(f.admin.calls, []);
|
||||
const organizations = { list: async () => [], resolve: async id => (id === 'acme' ? { busHealthy: true, send: async () => ({}), proxyCall: async () => ({ added: true }) } : null) };
|
||||
const o = await devServer(t, { organizations });
|
||||
const inOrg = await send(o.port, '/api/orgs/acme/proxy/apikey/add', { method: 'POST', headers: o.headers(), body: { provider: 'openai', key: SENTINEL } });
|
||||
assert.deepEqual(inOrg.json, { added: true });
|
||||
assert.deepEqual(o.admin.calls, [['recordAccount', { verb: 'proxy.apikey.add', args: { provider: 'openai', key: SENTINEL }, result: { added: true }, user: 'Local operator', org: 'acme' }]]);
|
||||
});
|
||||
|
||||
test('a real controller records the account write without the key, and history shows it', async t => {
|
||||
const { createProxyAdmin } = await import('../../model-proxy/src/admin-control.mjs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'pa-a1r-'));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const { mkdirSync } = await import('node:fs');
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
mkdirSync(configDir, { mode: 0o700 }); mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit: { unit: 'mosaic-proxy-zz.service', status: async () => ({ installed: false }) }, buses: async () => ({ stopped: true, running: [] }), now: () => new Date(0), env: {} });
|
||||
const f = await devServer(t, { proxyAdmin: admin, proxyCall: async () => ({ added: true }) });
|
||||
const r = await send(f.port, '/api/proxy/apikey/add', { method: 'POST', headers: f.headers(), body: { provider: 'openai', key: SENTINEL } });
|
||||
assert.deepEqual(r.json, { added: true });
|
||||
const h = await send(f.port, '/api/proxy/admin/history', { headers: f.headers() });
|
||||
assert.deepEqual(h.json, { entries: [{ time: new Date(0).toISOString(), op: 'account.proxy.apikey.add', provider: 'openai', user: 'Local operator' }], next: null });
|
||||
assert.equal(readFileSync(join(dataRoot, 'state', 'proxy-admin.jsonl'), 'utf8').includes(SENTINEL), false);
|
||||
});
|
||||
|
||||
// #1873 settings-backend A2: programs off and access health.
|
||||
test('A2: a member and an organization owner get human-required on programs off and access, before any controller call', async t => {
|
||||
const organizations = { list: async () => [], resolve: async id => (id === 'x' ? { busHealthy: true } : null) };
|
||||
for (const session of [{ name: 'Bob', provider: 'local-dev', iss: 'local-dev', sub: 'bob', rootOwner: false, instanceRole: 'member' },
|
||||
{ name: 'Olive', provider: 'local-dev', iss: 'local-dev', sub: 'olive', rootOwner: false, instanceRole: 'operator', orgRole: 'owner' }]) {
|
||||
const f = await devServer(t, { organizations, sso: { ...createDevOwner(), session: () => session } });
|
||||
for (const route of A2) {
|
||||
const inOrg = await send(f.port, `/api/orgs/x/proxy/admin/${route[0]}`, { method: route[1], headers: f.headers(), body: route[2] });
|
||||
assert.equal(inOrg.status, 403, `${session.sub} ${route[0]}`); assert.deepEqual(inOrg.json, { error: 'human-required' }, `${session.sub} ${route[0]}`);
|
||||
const plain = await hit(f, route);
|
||||
assert.equal(plain.status >= 400 && plain.status < 500, true, `${session.sub} ${route[0]} outside an organization is refused`);
|
||||
}
|
||||
assert.deepEqual(f.admin.calls, []);
|
||||
}
|
||||
});
|
||||
|
||||
test('A2: programs off takes one plain program name and nothing else; access takes no body', async t => {
|
||||
const f = await devServer(t);
|
||||
for (const body of [{}, { name: '' }, { name: 'Alpha' }, { name: '../x' }, { name: 7 }, { name: 'a'.repeat(33) }, { name: 'alpha', extra: 1 }, { name: ['alpha'] }])
|
||||
assert.deepEqual((await hit(f, ['programs/off', 'POST', body])).json, { error: 'invalid-request' }, JSON.stringify(body));
|
||||
assert.deepEqual(f.admin.calls, []);
|
||||
assert.equal((await send(f.port, '/api/proxy/admin/access', { method: 'POST', headers: f.headers(), body: {} })).status, 405);
|
||||
assert.equal((await send(f.port, '/api/proxy/admin/programs/off', { headers: f.headers() })).status, 405);
|
||||
f.admin.programsOff = async () => { throw Object.assign(new Error(`${SENTINEL} busy`), { code: 'proxy-admin-busy' }); };
|
||||
const busy = await hit(f, ['programs/off', 'POST', { name: 'alpha' }]);
|
||||
assert.equal(busy.status, 409); assert.deepEqual(busy.json, { error: 'proxy-admin-busy' });
|
||||
f.admin.access = async () => { throw new Error(SENTINEL); };
|
||||
assert.deepEqual((await hit(f, ['access', 'GET'])).json, { error: 'outcome-unknown' });
|
||||
assert.equal(f.logs.join('\n').includes(SENTINEL), false);
|
||||
});
|
||||
|
||||
// The real controller behind the real routes, with fake organizations whose answers carry planted keys.
|
||||
test('A2 end to end: programs off writes one receipt, skips a down bus, and no answer, receipt or history holds a key; access never says ok for proxy-uncertain', async t => {
|
||||
const { createProxyAdmin } = await import('../../model-proxy/src/admin-control.mjs');
|
||||
const { mkdirSync } = await import('node:fs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'pa-a2-'));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
mkdirSync(configDir, { mode: 0o700 }); mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const TAIL = 'Ab3_dE5-fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7dE';
|
||||
const planted = [`msk-k1a2-${TAIL}`, `mso-k1a2-acme-${TAIL}`, `mst-k1a2-triage-${TAIL}`, `msp-k1a2-${TAIL}`];
|
||||
const REV = 'c'.repeat(64);
|
||||
const sets = [];
|
||||
const org = (id, { down = false, keys = [], keysError = null } = {}) => ({
|
||||
id, fixture: false, cliproxy: true, changes: '',
|
||||
file: () => ({ document: { roles: {}, plugins: { harness: ['alpha', 'beta'] }, note: planted[1] }, revision: REV }),
|
||||
call: async (verb, args) => {
|
||||
if (down) throw Object.assign(new Error(planted[0]), { code: 'outcome-unknown' });
|
||||
if (verb === 'plugins') return { business: id, revision: REV, plugins: { harness: ['alpha', 'beta'] }, note: planted[0] };
|
||||
if (verb === 'routes') return { business: id, revision: REV, instances: {}, note: planted[2] };
|
||||
if (verb === 'plugins.set') { sets.push([id, args]); return { business: id, revision: 'd'.repeat(64) }; }
|
||||
if (verb === 'proxy.keys') { if (keysError) throw Object.assign(new Error(planted[3]), { code: keysError }); return { keys }; }
|
||||
throw Object.assign(new Error(planted[3]), { code: 'invalid-request' });
|
||||
},
|
||||
});
|
||||
const orgs = [
|
||||
org('acme', { keys: [{ keyId: '0123456789ab', management: 'stack-managed', agent: planted[1], issuedAt: planted[3], state: 'active' }, { keyId: 'abcdefabcdef', management: 'not stack-managed', agent: planted[0], issuedAt: null, state: null }] }),
|
||||
org('bolt', { down: true }),
|
||||
org('crux', { keysError: 'proxy-uncertain' }),
|
||||
];
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit: { unit: 'mosaic-proxy-zz.service', status: async () => ({ installed: false }) }, buses: async () => ({ stopped: true, running: [] }), now: () => new Date(0), env: {},
|
||||
riders: async () => orgs, harnesses: () => ['alpha', 'beta'], rootAgents: () => ({ state: 'absent', agents: {}, assistant: null }) });
|
||||
const f = await devServer(t, { proxyAdmin: admin });
|
||||
const unknown = await hit(f, ['programs/off', 'POST', { name: 'gamma' }]);
|
||||
assert.equal(unknown.status, 400); assert.deepEqual(unknown.json, { error: 'invalid-request' });
|
||||
const off = await hit(f, ['programs/off', 'POST', { name: 'alpha' }]);
|
||||
assert.equal(off.status, 200);
|
||||
assert.deepEqual(off.json.results, [{ id: 'acme', outcome: 'off' }, { id: 'bolt', outcome: 'bus-down' }, { id: 'crux', outcome: 'off' }]);
|
||||
assert.deepEqual(off.json.receipt, { time: new Date(0).toISOString(), op: 'programs.off', name: 'alpha', results: off.json.results, user: 'Local operator' });
|
||||
assert.deepEqual(sets.map(([id]) => id), ['acme', 'crux']);
|
||||
const lines = readFileSync(join(dataRoot, 'state', 'proxy-admin.jsonl'), 'utf8').trim().split('\n');
|
||||
assert.equal(lines.length, 1);
|
||||
const access = await hit(f, ['access', 'GET']);
|
||||
assert.equal(access.status, 200);
|
||||
assert.deepEqual(access.json.organizations.map(o => [o.id, o.health, o.reason ?? null]), [['acme', 'ok', null], ['bolt', 'unreadable', 'bus-down'], ['crux', 'attention', 'proxy-uncertain']]);
|
||||
assert.deepEqual(access.json.organizations[0].keys, [{ keyId: '0123456789ab', agent: null, state: 'active', issuedAt: null }]);
|
||||
const status = await hit(f, ['status', 'GET']);
|
||||
assert.deepEqual(status.json.programs, { installed: ['alpha', 'beta'], organizations: [{ id: 'acme', on: ['alpha', 'beta'] }, { id: 'bolt', on: null }, { id: 'crux', on: ['alpha', 'beta'] }] });
|
||||
const history = await hit(f, ['history', 'GET']);
|
||||
assert.deepEqual(history.json.entries, [off.json.receipt]);
|
||||
const all = [unknown.text, off.text, access.text, status.text, history.text, lines.join('\n'), f.logs.join('\n')].join('\n');
|
||||
assert.doesNotMatch(all, /ms[kotp]-[A-Za-z0-9_-]{4,}/);
|
||||
for (const bit of [TAIL, TAIL.slice(0, 10), root]) assert.equal(all.includes(bit), false, bit);
|
||||
});
|
||||
@@ -10,3 +10,21 @@ test('Console production module graph cannot import the model-proxy client', asy
|
||||
platform: 'node', format: 'esm', splitting: true, outdir: '/unused-proxy-architecture', write: false, metafile: true, logLevel: 'silent' });
|
||||
assert.equal(Object.keys(result.metafile.inputs).some(file => file.endsWith('/model-proxy/src/client.mjs')), false);
|
||||
});
|
||||
// #1873 settings-backend-a3: the provider list lives in the cliproxy plugin's PROVIDERS. The
|
||||
// management client, the bus proxy page and the settings page read it; none of them writes the
|
||||
// provider ids, sign-in hosts or key base URL out as its own list.
|
||||
test('a3: no provider id, sign-in host or key base URL is written out in client.mjs, proxy-page.mjs or display.js', async () => {
|
||||
const { readFileSync } = await import('node:fs');
|
||||
const { PROVIDERS } = await import('../../plugins/model-provider/cliproxy/index.mjs');
|
||||
const escape = text => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
const words = PROVIDERS.flatMap(p => [p.id, p.signInHost, p.baseUrl]).filter(Boolean);
|
||||
assert.ok(words.length >= PROVIDERS.length + 2);
|
||||
const quoted = new RegExp(`['"\`](${words.map(escape).join('|')})['"\`]`);
|
||||
const keyed = new RegExp(`[{,]\\s*(${PROVIDERS.map(p => escape(p.id)).join('|')})\\s*:`);
|
||||
for (const file of ['../../model-proxy/src/client.mjs', '../../bus/src/proxy-page.mjs', '../src/public/display.js']) {
|
||||
const text = readFileSync(new URL(file, import.meta.url), 'utf8');
|
||||
assert.equal(quoted.exec(text)?.[0], undefined, `${file} writes a provider value out`);
|
||||
assert.equal(keyed.exec(text)?.[0], undefined, `${file} keys an object by provider id`);
|
||||
assert.equal(/<option value=/.test(text), false, `${file} writes a provider option out`);
|
||||
}
|
||||
});
|
||||
@@ -6,8 +6,9 @@ import http from 'node:http';
|
||||
import { readFileSync, existsSync } from 'node:fs';
|
||||
import { resolve, extname } from 'node:path';
|
||||
import { browser } from './browser.mjs';
|
||||
async function setup(t, readonly = false, refusal = null) {
|
||||
const state = { routes: [], loginStatus: 'wait', receivedKey: false, release: null, priority: 0, models: ['fixture-old'] };
|
||||
import { publicProviders } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
async function setup(t, readonly = false, refusal = null, providers = publicProviders()) {
|
||||
const state = { routes: [], loginStatus: 'wait', receivedKey: false, keyProvider: null, release: null, priority: 0, models: ['fixture-old'] };
|
||||
const server = http.createServer(async (req, res) => {
|
||||
const send = value => { res.setHeader('content-type', 'application/json'); res.end(JSON.stringify(value)); };
|
||||
if (req.url === '/api/config') return send({ send: !readonly, devOwner: true });
|
||||
@@ -16,10 +17,10 @@ async function setup(t, readonly = false, refusal = null) {
|
||||
if (refusal && req.url.endsWith(refusal)) { res.statusCode = 503; return send({ error: 'not-configured' }); }
|
||||
const chunks = []; for await (const c of req) chunks.push(c);
|
||||
const args = chunks.length ? JSON.parse(Buffer.concat(chunks)) : {};
|
||||
if (req.url.endsWith('/accounts')) return send({ accounts: [{ accountId: 'a'.repeat(12), provider: 'claude', displayName: '<img src=x onerror="window.injected=true">', status: 'ready', priority: state.priority }, { accountId: 'c'.repeat(12), provider: 'z.ai', displayName: 'Fixture z.ai account', status: 'active', priority: 0, models: state.models }] });
|
||||
if (req.url.endsWith('/accounts')) return send({ accounts: [{ accountId: 'a'.repeat(12), provider: 'claude', displayName: '<img src=x onerror="window.injected=true">', status: 'ready', priority: state.priority }, { accountId: 'c'.repeat(12), provider: 'z.ai', displayName: 'Fixture z.ai account', status: 'active', priority: 0, models: state.models }], ...(providers === 'none' ? {} : { providers }) });
|
||||
if (req.url.endsWith('/keys')) return send({ keys: [{ keyId: 'b'.repeat(12), management: 'sentinel', agent: null, issuedAt: '2026-11-07T02:24:02.021Z', state: null }] });
|
||||
if (req.url.endsWith('/apikey/add')) {
|
||||
state.receivedKey = args.key === 'p4-browser-key';
|
||||
state.receivedKey = args.key === 'p4-browser-key'; state.keyProvider = args.provider;
|
||||
await new Promise(r => { state.release = r; }); return send({ added: true });
|
||||
}
|
||||
if (req.url.endsWith('/login/start')) return send({ url: 'https://claude.ai/oauth/authorize?state=p4-state', state: 'p4-state' });
|
||||
@@ -113,3 +114,35 @@ test('settings-s4: Model setup shows no client keys table and reads no keys (des
|
||||
assert.doesNotMatch(await b.evaluate('document.body.innerText'),/Client keys|Accounts and client keys/);
|
||||
assert.equal(state.routes.some(r=>r.startsWith('/api/proxy/keys')),false);
|
||||
});
|
||||
// #1873 settings-backend-a3: anyone but the instance owner reads no setup status, so the select comes
|
||||
// from the providers the accounts answer carries (the plugin's list as id, label and sign-in kind).
|
||||
const selectNow = b => b.evaluate(`(() => { const s = document.querySelector("#proxy-provider"); return { options: [...s.options].map(o => [o.value, o.textContent]),
|
||||
hidden: s.hidden, help: document.querySelector("#proxy-provider-help").textContent, go: document.querySelector("#proxy-login-go").hidden }; })()`);
|
||||
test('settings-backend-a3: the select is built from the accounts answer\'s providers, and a key goes to the provider picked', { timeout: 60000 }, async t => {
|
||||
const { b, state, wait } = await setup(t);
|
||||
assert.deepEqual((await selectNow(b)).options, publicProviders().map(p => [p.id, `${p.label} (${p.signIn === 'oauth' ? 'subscription' : 'API key'})`]));
|
||||
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("#proxy-accounts li[data-account]")].map(li => [li.children[0].textContent, li.children[1].firstChild.textContent])'),
|
||||
[['Claude', 'Subscription'], ['z.ai', 'API key']], 'a row takes its provider name and kind from the list');
|
||||
await b.evaluate('(() => { const s = document.querySelector("#proxy-provider"); s.value = "z.ai"; s.dispatchEvent(new Event("change")); document.querySelector("#proxy-apikey input").value = "p4-browser-key"; document.querySelector("#proxy-apikey").requestSubmit(); return true; })()');
|
||||
for (let n = 0; n < 100 && !state.release; n++) await new Promise(r => setTimeout(r, 20));
|
||||
assert.equal(state.keyProvider, 'z.ai'); state.release();
|
||||
await wait('document.querySelector("#proxy-message").textContent.includes("added")');
|
||||
});
|
||||
test('settings-backend-a3: a provider the accounts answer leaves out is not in the select', { timeout: 60000 }, async t => {
|
||||
const { b } = await setup(t, false, null, publicProviders().filter(p => p.id !== 'claude'));
|
||||
const now = await selectNow(b);
|
||||
assert.deepEqual(now.options, [['codex', 'ChatGPT / Codex (subscription)'], ['z.ai', 'z.ai (API key)']]);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-login-go").textContent'), 'Sign in to ChatGPT / Codex');
|
||||
});
|
||||
for (const [name, providers] of [['no providers', 'none'], ['an empty list', []]]) {
|
||||
test(`settings-backend-a3: an accounts answer with ${name} shows no select and says the providers could not be read`, { timeout: 60000 }, async t => {
|
||||
const { b } = await setup(t, false, null, providers);
|
||||
assert.deepEqual(await selectNow(b), { options: [], hidden: true, help: 'The providers could not be read, so no account can be added here.', go: true });
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-apikey").hidden'), true);
|
||||
});
|
||||
}
|
||||
test('settings-backend-a3: an accounts read that fails says the providers could not be read', { timeout: 60000 }, async t => {
|
||||
const { b, wait } = await setup(t, false, '/accounts');
|
||||
await wait('!/^Reading/.test(document.querySelector("#proxy-provider-help").textContent)');
|
||||
assert.deepEqual(await selectNow(b), { options: [], hidden: true, help: 'The providers could not be read, so no account can be added here.', go: true });
|
||||
});
|
||||
@@ -5,6 +5,7 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { w15Fixture } from './w15-fixture.mjs';
|
||||
import { publicProviders } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
|
||||
const T = '2026-11-11T12:00:00.000Z';
|
||||
const base = {
|
||||
@@ -15,7 +16,7 @@ const base = {
|
||||
fetch: { state: 'idle' }, restart: null, busy: null,
|
||||
organizations: [{ id: 'acme', proxy: 'shared', catalogue: null }],
|
||||
ownProxy: { available: false, state: 'not built', reason: 'The configuration has one proxy per instance.' },
|
||||
next: ['fetch'],
|
||||
next: ['fetch'], providers: publicProviders(),
|
||||
};
|
||||
const fetched = { ...base, release: { ...base.release, fetched: true, verified: true }, next: ['init'] };
|
||||
const fenced = { ...base, release: fetched.release, init: { initialized: true, partial: false, settings: { port: 18432 }, files: [] },
|
||||
@@ -26,9 +27,12 @@ const scenario = (status, config = {}) => ({ '/api/config': { send: true, devOwn
|
||||
const writes = b => b.evaluate('window.__writes.filter(w=>w.path.startsWith("/api/proxy/admin/")).map(w=>({path:w.path,body:JSON.parse(w.body)}))');
|
||||
// Step 3's line names the step it holds in data-next.
|
||||
const STEP3 = '#setup-step-organizations [data-step-summary]';
|
||||
// settings-s5b: Technical details reads the setup history from GET /api/proxy/admin/history, not from receipts.
|
||||
const history = entries => ({ '/api/proxy/admin/history?limit=25': { entries, next: null }, '/api/proxy/admin/service-key': { madeAt: T, renewedAt: null, renewal: 'not-built', problem: null } });
|
||||
const historyItems = b => b.evaluate('[...document.querySelectorAll("#proxy-setup-history li")].map(li=>li.textContent.split(" · ")[0])');
|
||||
|
||||
test('a fresh instance shows Set up model service as one button, the not-built own proxy, and no terminal note', { timeout: 90000 }, async t => {
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(base), 'POST /api/proxy/admin/fetch': { status: 202, body: { state: 'running' } } } });
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(base), 'POST /api/proxy/admin/fetch': { status: 202, body: { state: 'running' } }, '/api/proxy/admin/log?lines=20': { unit: 'mosaic-proxy.service', lines: [], redacted: 0, dropped: 0 } } });
|
||||
await b.navigate(f.base + '/#/settings/model-setup');
|
||||
await wait('document.querySelector("#proxy-setup-go")?.dataset.next==="fetch"');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup").hidden'), false);
|
||||
@@ -46,6 +50,8 @@ test('a fresh instance shows Set up model service as one button, the not-built o
|
||||
await wait('document.querySelector("#setup-step-service").dataset.state==="bad"');
|
||||
assert.deepEqual(await writes(b), [{ path: '/api/proxy/admin/fetch', body: {} }]);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-go").textContent'), 'Try again');
|
||||
// settings-s5b: a failed step 1 opens the service's last lines, read from the log route.
|
||||
await wait('document.querySelector("#proxy-setup-lines").open && /written nothing yet/.test(document.querySelector("#proxy-setup-lines [data-log]").textContent)');
|
||||
assert.deepEqual(await b.evaluate('window.__unexpected'), []);
|
||||
});
|
||||
|
||||
@@ -59,7 +65,7 @@ test('without send the next-step button is disabled and nothing is posted', { ti
|
||||
});
|
||||
|
||||
test('init always sends generate; there is no pasted key, and Try again sends generate again', { timeout: 90000 }, async t => {
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(fetched), 'POST /api/proxy/admin/init': { body: { settings: { port: 18432 }, receipt: { op: 'init', time: T, port: 18432, generated: true } } } } });
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(fetched), ...history([{ op: 'init', time: T }]), 'POST /api/proxy/admin/init': { body: { settings: { port: 18432 }, receipt: { op: 'init', time: T, port: 18432, generated: true } } } } });
|
||||
await b.navigate(f.base + '/#/settings/model-setup');
|
||||
await wait('document.querySelector("#proxy-setup-go")?.dataset.next==="init"');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-go").textContent'), 'Continue setup');
|
||||
@@ -73,7 +79,11 @@ test('init always sends generate; there is no pasted key, and Try again sends ge
|
||||
await b.evaluate('(()=>{const f=document.querySelector("#proxy-setup-init");f.elements.port.value="18432";document.querySelector("#proxy-setup-go").click();})()');
|
||||
await wait('window.__writes.some(w=>w.path==="/api/proxy/admin/init")');
|
||||
assert.deepEqual(await writes(b), [{ path: '/api/proxy/admin/init', body: { generate: true, port: 18432 } }]);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-setup-receipts").textContent'), /init/);
|
||||
// settings-s5b: the receipt is not shown raw; the history read under Technical details names it.
|
||||
assert.doesNotMatch(await b.evaluate('document.querySelector("#proxy-setup").textContent'), /generated/);
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = true');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 1');
|
||||
assert.deepEqual(await historyItems(b), ['Set up']);
|
||||
// The status still names init, so the run stops there; Try again starts from it.
|
||||
await wait('document.querySelector("#proxy-setup-go")?.textContent==="Try again" && !document.querySelector("#proxy-setup-init [data-setup-write]").disabled');
|
||||
await b.evaluate('(()=>{const f=document.querySelector("#proxy-setup-init");f.elements.port.value="";document.querySelector("#proxy-setup-go").click();})()');
|
||||
@@ -82,14 +92,20 @@ test('init always sends generate; there is no pasted key, and Try again sends ge
|
||||
});
|
||||
|
||||
test('after a confirmed restart the next step clears the fence and shows the receipt', { timeout: 90000 }, async t => {
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(fenced), 'POST /api/proxy/admin/fence/clear': { body: { cleared: true, receipt: { op: 'fence.clear', time: T, pid: 4242 } } } } });
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(fenced), ...history([]), 'POST /api/proxy/admin/fence/clear': { body: { cleared: true, receipt: { op: 'fence.clear', time: T, pid: 4242 } } } } });
|
||||
await b.navigate(f.base + '/#/settings/model-setup');
|
||||
await wait('document.querySelector("#proxy-setup-go")?.dataset.next==="fence-clear"');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = true');
|
||||
await wait('document.querySelector("#proxy-setup-history-message").textContent === "No setup history yet."');
|
||||
await b.evaluate(`window.__scenario['/api/proxy/admin/history?limit=25'] = { entries: [{ op: 'fence.clear', time: '${T}' }], next: null }`);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-setup-state").textContent'), /new pid 4242/);
|
||||
await b.evaluate('document.querySelector("#proxy-setup-go").click()');
|
||||
await wait('document.querySelector("#proxy-setup-message").textContent.includes("Fence cleared")');
|
||||
assert.deepEqual(await writes(b), [{ path: '/api/proxy/admin/fence/clear', body: {} }]);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-setup-receipts").textContent'), /fence\.clear.*pid: 4242/);
|
||||
// settings-s5b: with Technical details open, the write reads the history again.
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 1');
|
||||
assert.deepEqual(await historyItems(b), ['Key writes resumed']);
|
||||
assert.doesNotMatch(await b.evaluate('document.querySelector("#proxy-setup").textContent'), /pid: 4242/);
|
||||
});
|
||||
|
||||
test('a stopped proxy hides the account panels with a setup note, and starting the service brings them back', { timeout: 90000 }, async t => {
|
||||
@@ -143,13 +159,16 @@ test('a fence with no readable time names the --proxy-restarted path instead of
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-next details.proxy-setup-cli p").textContent'), 'Run this in a terminal on this machine: mosaic proxy fence clear --proxy-restarted');
|
||||
});
|
||||
|
||||
test('after a reload the receipts come from status, and an initialized proxy shows its settings read-only', { timeout: 90000 }, async t => {
|
||||
test('after a reload the history comes from the history read, not the status receipts, and an initialized proxy shows its settings read-only', { timeout: 90000 }, async t => {
|
||||
const initialized = { ...base, release: fetched.release, init: { initialized: true, partial: false, settings: { port: 18432 }, files: [] },
|
||||
receipts: [{ op: 'fetch', time: T, version: '8.0.42' }, { op: 'init', time: T, generated: true, port: 18432 }], next: ['unit-install'] };
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(initialized), 'POST /api/proxy/admin/unit/install': { body: { receipt: { op: 'unit.install', time: T } } } } });
|
||||
const { f, b, wait } = await w15Fixture(t, { scenario: { ...scenario(initialized), ...history([{ op: 'fetch', time: T }, { op: 'init', time: T }]), 'POST /api/proxy/admin/unit/install': { body: { receipt: { op: 'unit.install', time: T } } } } });
|
||||
await b.navigate(f.base + '/#/settings/model-setup');
|
||||
await wait('document.querySelector("#proxy-setup-go")?.dataset.next==="unit-install"');
|
||||
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("#proxy-setup-receipts li strong")].map(s=>s.textContent)'), ['init', 'fetch'], 'newest first, from proxy-admin.jsonl');
|
||||
assert.equal(await b.evaluate('document.querySelectorAll("#proxy-setup-history li").length'), 0, 'nothing is read before Technical details opens');
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech").open = true');
|
||||
await wait('document.querySelectorAll("#proxy-setup-history li").length === 2');
|
||||
assert.deepEqual(await historyItems(b), ['Set up', 'Release downloaded and checked'], 'newest first, from the history read');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-init").hidden'), true, 'init is done, so its form is gone');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-settings").textContent'), 'Port18432');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-settings-note").textContent'), 'Changing these after setup is not built yet.');
|
||||
@@ -177,7 +196,7 @@ test('Ride all confirms the stricter admission, paints skips and stand-in organi
|
||||
await wait(`document.querySelector("#proxy-ride")?.checkVisibility() && document.querySelector(${JSON.stringify(STEP3)})?.dataset.next==="ride-all"`);
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-ride-go").className'), 'btn primary');
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-ride-text").textContent'), /every launch route is checked, direct Anthropic routes too.*2 organizations can ride now/);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-ride-fixtures li").textContent'), /^west uses stand-in models, can be moved to the owner's models\./);
|
||||
assert.match(await b.evaluate('document.querySelector("#proxy-ride-fixtures li").textContent'), /^west uses stand-in models, can be moved to your models\./);
|
||||
// The first Ride all's own reload paints, then waits on the accounts read for good.
|
||||
await b.evaluate('document.querySelector("#proxy-ride-go").click()');
|
||||
await wait('window.__writes.some(w=>w.path==="/api/proxy/admin/ride-all")');
|
||||
@@ -399,7 +418,7 @@ test('settings-p3: a stand-in organization with no bus status is not offered a M
|
||||
await b.navigate(f.base + '/#/settings/model-setup');
|
||||
await wait('document.querySelector("#proxy-ride-fixtures li")');
|
||||
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("#proxy-ride-fixtures li")].map(li => li.firstChild.textContent.trim())'),
|
||||
['west uses stand-in models. Moving it is not available on this Console.', "east uses stand-in models, can be moved to the owner's models."]);
|
||||
['west uses stand-in models. Moving it is not available on this Console.', "east uses stand-in models, can be moved to your models."]);
|
||||
assert.equal(await b.evaluate('document.querySelector("[data-ride-move=west]")'), null);
|
||||
assert.equal(await b.evaluate('document.querySelector("[data-ride-move=east]").disabled'), false);
|
||||
assert.deepEqual(await b.evaluate('window.__unexpected'), []);
|
||||
@@ -446,8 +465,8 @@ test('settings-s1 B4: only the next step has a button; done steps, Fence, Init l
|
||||
assert.deepEqual(await b.evaluate(`${visible('.settings-content .primary')}.map(e=>e.id||e.textContent)`), ['proxy-login-go'], 'one primary button on the page');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-go")'), null, 'step 1 is done, so it has no button');
|
||||
assert.equal(await b.evaluate('document.querySelector("#proxy-setup-init").checkVisibility()'), false, 'init is done, so its form is gone');
|
||||
const tech = await b.evaluate('(()=>{const d=document.querySelector("#proxy-setup-tech");return {summary:d.querySelector("summary").textContent,open:d.open,terms:[...d.querySelectorAll("dt")].map(x=>x.textContent),units:d.querySelectorAll("[data-unit]").length,receipts:!!d.querySelector("#proxy-setup-receipts")}})()');
|
||||
assert.deepEqual(tech, { summary: 'Technical details: release, service, history', open: false, terms: ['Release', 'Initialized', 'Service', 'Fence', 'Init lock'], units: 4, receipts: true });
|
||||
const tech = await b.evaluate('(()=>{const d=document.querySelector("#proxy-setup-tech");return {summary:d.querySelector("summary").textContent,open:d.open,terms:[...d.querySelectorAll("dt")].map(x=>x.textContent),units:d.querySelectorAll("[data-unit]").length,history:!!d.querySelector("#proxy-setup-history")}})()');
|
||||
assert.deepEqual(tech, { summary: 'Technical details: release, service, history', open: false, terms: ['Release', 'Initialized', 'Service', 'Fence', 'Init lock', 'Service key'], units: 4, history: true });
|
||||
assert.equal(await b.evaluate(`${visible('#proxy-setup dt, #proxy-setup [data-unit]')}.length`), 0, 'nothing technical shows until it is opened');
|
||||
// Technical details opens by keyboard and shows the controls.
|
||||
await b.evaluate('document.querySelector("#proxy-setup-tech summary").focus()');
|
||||
|
||||
@@ -197,8 +197,9 @@ test('Routing switch writes the model-provider setting and keeps other modules',
|
||||
await b.call('Page.addScriptToEvaluateOnNewDocument',{source:`const original=window.fetch;window.posts=[];window.fetch=async(url,options={})=>{if(url==='/api/config')return new Response(JSON.stringify({send:true}));if(url==='/api/proxy/plugins')return new Response(JSON.stringify(${JSON.stringify(settings)}));if(url==='/api/proxy/plugins/set'){posts.push(JSON.parse(options.body));return new Promise(r=>window.answer=(status,body)=>r(new Response(JSON.stringify(body),{status})));}return original(url,options);};`});
|
||||
await b.navigate(`http://127.0.0.1:${web.address().port}/#/settings/routing`);
|
||||
const wait=expr=>b.evaluate(`(async()=>{for(let n=0;n<200;n++){if(${expr})return true;await new Promise(r=>setTimeout(r,30));}throw Error('wait failed');})()`);
|
||||
await wait('document.querySelector("#provider-switch")?.textContent.startsWith("Use your models")');
|
||||
await b.evaluate('document.querySelector("#provider-switch").click();true');
|
||||
// settings-s5: the switch is the Use your models choice; choosing it writes at once.
|
||||
await wait('document.querySelector("#models-in-force")?.textContent.includes("does not use your models")&&!document.querySelector("#models-yours").disabled');
|
||||
await b.evaluate('document.querySelector("#models-yours").click();true');
|
||||
await wait('posts.length===1');const payload=await b.evaluate('posts[0]');
|
||||
assert.deepEqual(payload.plugins,{'model-provider':['cliproxy']});
|
||||
const before=JSON.parse(readFileSync(file,'utf8')).plugins;
|
||||
@@ -271,7 +272,7 @@ test('quick fallback edits survive the harness redraw', {timeout:60000}, async t
|
||||
// organization's catalogue is ready with no proxy, and its switch reads Enable.
|
||||
test('D3 Routing: with cliproxy listed and no model proxy the switch reads Disable and removes cliproxy only; a fixture-only org reads Enable', {timeout:90000}, async t => {
|
||||
for (const [providers, label, line, next] of [
|
||||
[['fixture','cliproxy'], 'Stop using the model service', /can't see your models yet\. Your model service isn't set up yet\. A change to thinking alone still saves\. A new model needs the model service: ask the instance owner to set it up, or press Stop using the model service when this organization does not need it\./, ['fixture']],
|
||||
[['fixture','cliproxy'], 'Stop using your models', /can't see your models yet\. Your model service isn't set up yet\. A change to thinking alone still saves\. A new model needs the model service: ask the instance owner to set it up, or press Stop using your models when this organization does not need it\./, ['fixture']],
|
||||
[['fixture'], 'Use your models', /^Model catalogue, observed /, ['fixture','cliproxy']],
|
||||
]) {
|
||||
const root=tmp(t,'d3-switch-'), f=fixture(root,'acme',doc=>{doc.plugins['model-provider']=providers;return doc;});
|
||||
@@ -287,13 +288,17 @@ test('D3 Routing: with cliproxy listed and no model proxy the switch reads Disab
|
||||
await b.call('Page.addScriptToEvaluateOnNewDocument',{source:`const original=window.fetch;window.posts=[];window.fetch=async(url,options={})=>{if(url==='/api/config')return new Response(JSON.stringify({send:true}));if(url==='/api/proxy/plugins')return new Response(JSON.stringify(${JSON.stringify(settings)}));if(url==='/api/proxy/plugins/set'){posts.push(JSON.parse(options.body));return new Promise(r=>window.answer=(status,body)=>r(new Response(JSON.stringify(body),{status})));}return original(url,options);};`});
|
||||
await b.navigate(`http://127.0.0.1:${web.address().port}/#/settings/routing`);
|
||||
const wait=expr=>b.evaluate(`(async()=>{for(let n=0;n<200;n++){if(${expr})return true;await new Promise(r=>setTimeout(r,30));}throw Error('wait failed');})()`);
|
||||
await wait(`document.querySelector("#provider-switch")?.textContent.startsWith(${JSON.stringify(label)})&&!!document.querySelector('[data-harness]')`);
|
||||
// settings-s5: in force, the way back is Stop using your models; not in force, the Use your models choice.
|
||||
const control=label==='Stop using your models'?'#models-stop':'#models-yours';
|
||||
await wait(`!!document.querySelector(${JSON.stringify(control)})&&!document.querySelector(${JSON.stringify(control)}).disabled&&!!document.querySelector('[data-harness]')`);
|
||||
// settings-s5 review P3-3: a Console with no organizations says "this business" mid-sentence.
|
||||
assert.equal(await b.evaluate('document.querySelector("#models-source legend").textContent'),'Where this business gets its models');
|
||||
const [info,...notices]=await b.evaluate('[document.querySelector(".routing-panel > p.small")?.textContent,...[...document.querySelectorAll(".routing-panel > p.small.warn")].slice(1).map(p=>p.textContent)]');
|
||||
assert.match(info,line,providers.join(','));
|
||||
// proxy-pickup (#1873): the bus's cause reads as a plain line; its own words sit under Technical details.
|
||||
assert.deepEqual(notices,[],providers.join(','));
|
||||
assert.deepEqual(await b.evaluate('[...document.querySelectorAll(".routing-panel > details.routes-cause-detail p")].map(p=>p.textContent)'),label==='Stop using the model service'?['The model proxy has not been set up for this instance yet. Set it up on the Model proxy page.']:[],providers.join(','));
|
||||
await b.evaluate('document.querySelector("#provider-switch").click();true');
|
||||
assert.deepEqual(await b.evaluate('[...document.querySelectorAll(".routing-panel > details.routes-cause-detail p")].map(p=>p.textContent)'),label==='Stop using your models'?['The model proxy has not been set up for this instance yet. Set it up on the Model proxy page.']:[],providers.join(','));
|
||||
await b.evaluate(`document.querySelector(${JSON.stringify(control)}).click();true`);
|
||||
await wait('posts.length===1');const payload=await b.evaluate('posts[0]');
|
||||
assert.deepEqual(payload.plugins,{'model-provider':next},providers.join(','));
|
||||
const result=await call('plugins.set',payload);await b.evaluate(`window.answer(200,${JSON.stringify(result)});true`);
|
||||
@@ -309,7 +314,7 @@ test('D3 Routing: with cliproxy listed and no model proxy the switch reads Disab
|
||||
test('D3 Routing: a catalogue refusal names Disable only when it can make the save apply, and sends a tester to the owner', {timeout:90000}, async t => {
|
||||
const refusal=code=>({error:code,message:`${code}: the reason. Nothing changed.`,sent:false});
|
||||
for (const [providers, tester, instance, code, expect] of [
|
||||
[['fixture','cliproxy'], false, 'coder', 'model-catalogue-unavailable', /Nothing changed\. This change does not need the model service: press Stop using the model service, then save again\. Draft kept\.$/],
|
||||
[['fixture','cliproxy'], false, 'coder', 'model-catalogue-unavailable', /Nothing changed\. This change does not need your models: press Stop using your models, then save again\. Draft kept\.$/],
|
||||
[['fixture','cliproxy'], false, 'reviewer', 'model-catalogue-unavailable', /Nothing changed\. Draft kept\.$/],
|
||||
[['fixture'], false, 'reviewer', 'model-provider-unavailable', /Nothing changed\. Draft kept\.$/],
|
||||
[['fixture','cliproxy'], true, 'coder', 'model-catalogue-unavailable', /Nothing changed\. Ask the organization's owner to set up the model service, or to stop using it when the change does not need it\. Draft kept\.$/],
|
||||
|
||||
@@ -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)}})()`});
|
||||
|
||||
Loaded 100 of 107 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user