Compare commits

...
14 Commits
Author SHA1 Message Date
jason.woltje 8042aa13ed test(cli): exercise plugin backstop with TMPDIR unset 2026-11-20 16:23:18 -06:00
jason.woltje 82aa0c0bda test(plugins): use a multi-segment temp path for redaction 2026-11-20 16:13:28 -06:00
jason.woltje 0430c7dc32 fix(release,task,auth): health-reason review round: the worker credential check reads the auth file path as compose does (a leading ~ is $HOME, a relative path is from the checkout, a named volume is undecided), treats COMPOSE_ENV_FILES and docker-compose.override.yaml|yml as undecided like .env, and leaves the credential not checked when this user can't read the auth file (EACCES, EPERM) instead of refusing; a key variable that is only spaces is no credential, as in pi; a compose.yaml that can't be read or parsed gives fixed words, never its path or text; release.sh's reason with no run id comes from the new scripts/health-reason.mjs (the last refusal line, prefix dropped, redacted, withheld when it holds an environment value) and says "gave no run id"; the fault-injection and recursion-guard comments are back; tests cover the tilde path, COMPOSE_ENV_FILES, an unreadable auth file, a blank key, a compose warning line in a failed run's stderr, and the redacted and withheld reason; docs/TOOLS.md names each undecided case (#1873, lane health-reason) 2026-11-20 15:31:59 -06:00
jason.woltje 1433686d50 feat(release,task,auth,runs,plugins): health-reason: a failed release health check prints its run id and one plain reason before release: refusing activate: health check failed: the cause line mosaic-task show now prints for a run that did not succeed (a runner reason in words, else one redacted line from the run's stderr.txt read through the new readRunText), or, when the runner refused before a run record, its own refusal line, redacted. A pi run checks before its run directory exists that the worker container compose starts holds a credential for config execution.provider: names and types from the PI_AUTH_FILE compose mounts and the key variables compose passes with a value, judged by pi-ai's own checkAuth on placeholders (packages/plugins/harness/pi/worker-credential.mjs), so no provider list is kept; with none it refuses exit 3 naming the provider, what the container has and what to do; when it can't decide (COMPOSE_FILE, an override or .env, an env_file, an unknown provider) it says so and the run goes ahead. auth.sh status adds one line for the default run's provider and credential. Tests in test-task, test-release, test-auth and runs.test; docs/TOOLS.md (#1873, lane health-reason) 2026-11-20 15:14:07 -06:00
jason.woltje d752e75359 test(model-proxy): models-offered re-check round 3: a model kept on while its account is signed out counts against the 256 limit, so a save past it is models-invalid before any in-use ask and writes nothing; a save keeps every model seen before, so one away at the save and back later is not New (#1873, lane models-offered) 2026-11-20 14:34:28 -06:00
jason.woltje dde3ee4140 fix(model-proxy,webui): models-offered re-check round: in use now counts every model or fallback.model key the organization file names (project agents, definitions; not launch.models' own upstream ids) and the root agents the organization takes (not the root assistant, not one that excludes it), resolved through the organization's model table or legacy mapping; an organization file or root agent file that cannot be read leaves that usage unknown (unanswered); the seed waits while any usage is unknown and keeps a model in use whose account is signed out on; a provider with no entry counts such a model as on, and a save keeps a model that is on but not present (not on the page) on; receipt lists (on, off, inUse, unanswered) stop at 32 with a count of the rest; the ask reads a user with no instance as an agent in its organization; history reads models.seed and models.set by name; tests cover each, plus the seed's revision recheck under the lock (#1873, lane models-offered) 2026-11-20 14:34:28 -06:00
jason.woltje acb228d56f fix(model-proxy,webui,bus,plugins): models-offered review round: an organization that rides the proxy but does not answer (bus down, org file unreadable, catalogue not ready) is listed as unanswered, and switching any model off then refuses models-in-use until confirmed, so a down organization is never treated as using nothing; a provider with no entry reads all present models on and a save above 256 that drops a model in use asks first; the read seeds the file only when a sending Console gets a same-origin request (seed:true), never on a cross-site, same-site or metadata-less read or on a read-only Console; the in-use body always carries usedBy and unanswered arrays and the ask names unanswered organizations; the receipt caps off, inUse and unanswered at 32 with a count of the rest; tests add a hard-linked and an extra-key file (damaged), Claude accounts of unknown, error, disabled and unavailable state keeping a key model claude-code only, the bus validator refusing an isNew other than absent or true, and a browser case for the unanswered ask (#1873, lane models-offered) 2026-11-20 14:34:28 -06:00
jason.woltje d01701a9ce feat(model-proxy,bus,plugins,webui): models-offered (A9): the instance owner switches proxy models on or off per provider; one state file <dataRoot>/state/models-offered.json ({version:1, providers:{<id>:{on,seen}}}, no harness, no key, written whole under the admin lock with a receipt); catalogue() takes offered: an off or unseen model is unavailable with model-off, an unseen one is New, a damaged file turns every model off and never falls back to all on, and harnesses never change; the bus reads the file at every catalogue read; Console GET /api/proxy/admin/models and POST /api/proxy/admin/models/set (instance owner only, revision, confirmInUse) refuse models-offered-conflict and models-in-use, the second naming each organization and instance that uses the model; the plugin has no rule for current models, so the first read seeds every present model on and seen; the Settings accounts part shows one list per provider with an in-place ask before a model in use is switched off (#1873, lane models-offered) 2026-11-20 14:34:27 -06:00
jason.woltje ccd85604c6 feat(bus,model-proxy,webui): account-remove: the instance owner removes one model proxy account. Bus verb proxy.account.remove {accountId, confirm:true} under the write lock and fence: a signed-in account's credential file is deleted with ManagementClient.deleteCredential (DELETE /v8/management/credentials {names:[name]}, as the panel PIN pins calls CLIProxyAPI 8.0.79's DeleteAuthFileV8), an API key leaves its key group and a group left with no key leaves whole, then the accounts are read again; a refused write is remove-write-refused, an account still listed is remove-unconfirmed; two listed accounts on one credential name or one id listed twice refuse before any write. The Console's owner-only GET /api/proxy/admin/accounts/impact names which models leave and which organization agents route to them (account-impact.mjs, unknown where the reads cannot decide); POST /api/proxy/admin/accounts/remove writes receipt account.remove with provider and account id only. Settings Model setup gets Remove with an in-place ask; a failed remove reads the list again and never says nothing was removed. providerUpstream in the cliproxy plugin maps a provider to its upstream (#1873, lane account-remove) 2026-11-20 13:27:54 -06:00
jason.woltje e035b6dea9 test(harness,conversation): pi-fit-tmp re-check 2 round: smoke.test removes the fixture directory conversation/tests/harness.mjs makes when it loads (after(cleanupAll)); with TMPDIR unset that directory now lands in /var/tmp, which a boot doesn't clear, and smoke was the one importer that never removed it; fixtureDir throws a plain line when the temp directory is under /tmp and /var/tmp is missing or under /tmp too, so a test fails on its fixture, not with the fit check's guard line 2026-11-20 12:51:40 -06:00
jason.woltje 735e828aa2 test(harness,plugins,conversation,cli): pi-fit-tmp re-check round: stand-in pis are built outside /tmp whatever the temp directory is, since the fit check now stays undecided for a pi under /tmp; the new fixtureDir(prefix) in pi-stand-in.mjs makes the directory under tmpdir() unless its real path is under /tmp, else under /var/tmp; every stand-in root in the harness, plugins, conversation and cli tests goes through it (harness-watch's piPackage gets its own root, its watch scratch stays under tmpdir() for the W1 redaction test); with TMPDIR unset pi-fit went 13/75 to 88/0 and claim 9/24 to 33/0; the guard's test gains a pi with only a root under /tmp; sessionCovers' comment sits above it, not above ended's 2026-11-20 12:37:08 -06:00
jason.woltje e698b7d785 fix(harness,seat,conversation): pi-fit-tmp review round: the fit check stays undecided for a pi mounted under /tmp or a node there ("can't be checked: the pi's package sits under the system temp directory, which a session's sandbox covers", or "node sits ..."), since a session's sandbox mounts its private /tmp last and covers it, and no run starts; mountOrder compares targets in plain form (a trailing slash or "." names the same directory), leaves an entry with no absolute target where it is, and its comment states the limit: no bind stays hidden under a later one, so only checkArgv uses it; the conversation controller's comment and README say why a node under /tmp still can't run; tests: the guard's three cases and /tmpx, mountOrder's plain-form, no-target and hidden-bind cases 2026-11-20 12:05:26 -06:00
jason.woltje 8ed3e251be test(harness): pi-fit-tmp: the /tmp test's link to its temp directory sits in the pi's package, which the sandbox mounts, so bwrap follows it; without the scratch under the real path that case fails 2026-11-20 11:44:14 -06:00
jason.woltje 784748347b fix(harness,seat): pi-fit-tmp: the fit check gives one verdict whatever the temp directory is; with the scratch under /tmp, the bind of the scratch's tmp/ over /tmp hid the scratch's own bind and every run failed before pi started ("can't be checked: --help failed"); checkArgv now orders its binds with the new mountOrder (a bind goes before any bind whose target lies under its own, nothing else moves), the scratch goes under the temp directory's real path, and a failed --help line says how the run ended (exit status N, ended by SIGNAL, timed out) with no path; tests: a check with its temp directory in one mkdtemp directory under /tmp, and through a link to it, gives the verdict a temp directory outside /tmp gives and leaves nothing; mountOrder's order; the --help line for an exit status and a signal 2026-11-20 11:42:54 -06:00
58 changed files with 3231 additions and 94 deletions

No files matched your search

+76
View File
@@ -39,6 +39,47 @@ Task fields: `prompt` (required), `mission` (path), `expectExact`,
`directives[]`, optional governing `capabilities.tools`. Policy: a task may
narrow a mission's tools, never widen; empty intersection = tool-free run.
Worker credential check (#1873, lane health-reason). A run's provider and
model come only from the instance config, `execution.provider` and
`execution.model`; task and mission files have no provider field. Before a
pi run makes its run directory, `mosaic-task run` (and `retry`) asks whether
the worker container compose starts will hold a credential for that provider:
an entry in the auth file compose mounts (`PI_AUTH_FILE`), or a key variable
compose passes with a value that isn't blank (pi treats a blank one as
unset). The auth file path is read as compose reads it: a leading `~` is
`$HOME`, and a relative path is from the checkout. Only names and types are
read. pi
decides which credential counts: the check runs pi's own `checkAuth` from the
pi-ai the lock installs, on placeholders
(`packages/plugins/harness/pi/worker-credential.mjs`), so no provider list is
kept here. With none, the run refuses with exit 3 and no run record:
```
mosaic-task: the worker container has no credential for provider zai (config execution.provider); it has credentials for: anthropic. Change the instance's execution.provider to one of those, or add a credential for zai (a PI_AUTH_FILE entry, or an environment key compose passes to it).
```
When the check can't decide, the run goes ahead and one line says why:
`mosaic-task: worker credential not checked: <why>`. That happens when
`COMPOSE_FILE` or `COMPOSE_ENV_FILES` is set, a `compose.override.yaml`,
`compose.override.yml`, `docker-compose.override.yaml`,
`docker-compose.override.yml` or `.env` sits beside `compose.yaml`, the
service has an `env_file`, the auth file is a named volume or one this user
can't read (the container may still read it), `compose.yaml` can't be read
or parsed, pi or yaml isn't installed, or pi has no provider by that name.
The check can't see inside an entry: an `api_key` entry whose key is empty,
a `$VAR` or a `!command` still counts. The mock adapter needs no credential
and is never checked.
`mosaic-task show <runId>` on a run that did not succeed adds one line after
`exit:`, `cause: <reason>`. A reason the runner decided is said in words
(`the run passed its time limit and was stopped`, `the response did not match
the expected text`, `processing was halted during the run`). Otherwise it is
`worker said: <line>`, one line from the run's `stderr.txt` with compose's
progress and warning lines passed over, redacted (paths, links, emails,
key-shaped strings) and withheld when it holds a value from the environment.
With no such line it says the worker container could not be started or the
worker exited with its code and wrote nothing that can be shown.
Legacy worker containers use a typed per-launch mount contract. Task, mission,
user and session-fork snapshots are read-only. A fork source is opened without
following its final symlink, limited to 16 MiB, checked for owner/type and
@@ -132,6 +173,19 @@ listings and refused by `--auth`).
launch's `PI_AUTH_FILE`; missing/symlinked/non-0600 accounts refuse.
Headless task runs keep the default credential.
`auth.sh status` also prints one line for the default run: the provider the
config asks for and whether the worker container will hold a credential for
it, by the same check `mosaic-task run` makes. The auth file it checks is the
one compose mounts, which is `PI_AUTH_FILE` when set (compose's own default
otherwise, not the `$HOME` default the report above reads).
```
default run: provider zai (config execution.provider); worker container credential: yes
default run: provider openai (config execution.provider); worker container credential: none (it has: anthropic, zai)
default run: provider zai (config execution.provider); the adapter needs no credential
default run: provider zai (config execution.provider); worker container credential: not checked (<why>)
```
## Release
| Command | Purpose | Notes |
@@ -142,6 +196,28 @@ Headless task runs keep the default credential.
| `scripts/release.sh status` | Release, tag, active pointer, log | Safe on empty state |
| `scripts/release.sh ensure` | Self-determination: align active pointer to `RELEASE` | Fast path restores a missing/mismatched pointer without a gate; slow path packages + health-gates first. Invoked automatically at launch |
A failed health check says which run failed and why before it refuses. With
a run record, the reason is the `cause:` line `mosaic-task show` prints:
```
release: health check run: r-20261120T120000Z-a1b2c3
release: reason: the response did not match the expected text
release: refusing activate: health check failed
```
When the runner gave no run id (it refused before the run started: a missing
worker credential, a config problem), the reason is the runner's last refusal
line, picked and redacted by `scripts/health-reason.mjs`:
```
release: health check gave no run id
release: reason: the worker container has no credential for provider zai (config execution.provider); it has credentials for: none. ...
release: refusing activate: health check failed
```
Neither prints a key, an environment value or a host path; the run id is the
only identifier.
## Conductor (worker patches)
```bash
+12 -6
View File
@@ -1,10 +1,15 @@
// In-memory only; a new loader is made for every refresh so disabled or refused
// modules can never inherit a loader's last-good authority.
import { createLoader } from '../../plugins/src/loader.mjs';
import { readOffered } from '../../model-proxy/src/models-offered.mjs';
const validId = s => typeof s === 'string' && /^[a-z0-9][a-z0-9._-]{0,63}$/.test(s);
const validModel = s => typeof s === 'string' && /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,199}$/.test(s);
const disabled = () => ({ status: 'disabled', observedAt: null, models: [], notices: [] });
export function createModelCatalogue({ client, settings, pluginDataRoot = null, now = Date.now, makeLoader = createLoader, proxyLock = null }) {
// #1873 models-offered: the owner's on/off file, read on every catalogue read so a Console write
// counts at the next read. No data root (tests, a fixture bus) means no file: every model stays on.
const offeredNow = () => pluginDataRoot ? readOffered(pluginDataRoot) : { state: 'absent', offered: null, revision: null };
const offeredMark = o => o.state === 'damaged' ? 'damaged' : o.revision;
const cache = new Map(), flights = new Map(); let generation = 0, writing = 0;
function invalidate() { generation++; cache.clear(); }
function begin() { writing++; invalidate(); }
@@ -15,7 +20,7 @@ export function createModelCatalogue({ client, settings, pluginDataRoot = null,
if (!Array.isArray(names) || names.length > 32 || names.some(n => !validId(n)) || new Set(names).size !== names.length) throw Error();
return false;
}
async function refresh(id, previous) {
async function refresh(id, previous, offered) {
try {
if (off(id)) return disabled();
const names = [...settings(id)['model-provider']];
@@ -27,11 +32,11 @@ export function createModelCatalogue({ client, settings, pluginDataRoot = null,
const c = client();
const proxy = method => async (...args) => { if (!c) throw Error('model proxy not configured'); return c[method](...args); };
const answers = await Promise.all(loader.snapshot('model-provider').map(async ({ name, module }) => {
const answer = await module.catalogue({ listAccounts: proxy('getCredentials'), listModels: proxy('getCredentialModels'), listGroups: proxy('getUpstreamGroups'), now: () => new Date(now()).toISOString() });
const answer = await module.catalogue({ listAccounts: proxy('getCredentials'), listModels: proxy('getCredentialModels'), listGroups: proxy('getUpstreamGroups'), now: () => new Date(now()).toISOString(), offered });
if (answer?.status === 'unavailable' && answer.reason === 'model-key-collision') throw Object.assign(Error(), { code: 'model-key-collision' });
if (answer?.status !== 'ready' || !Array.isArray(answer.models) || answer.models.length > 10000 || !Array.isArray(answer.notices) || answer.notices.length > 100) throw Error();
for (const m of answer.models) {
if (!validId(m.key) || m.module !== name || !validId(m.upstream) || !validId(m.provider) || !validModel(m.model) || typeof m.label !== 'string' || m.label.length > 300 || /[\x00-\x1f\x7f]/.test(m.label) || typeof m.available !== 'boolean' || ![null, 'unverified'].includes(m.reason) || !Array.isArray(m.harnesses) || m.harnesses.length > 32 || m.harnesses.some(h => !validId(h))) throw Error();
if (!validId(m.key) || m.module !== name || !validId(m.upstream) || !validId(m.provider) || !validModel(m.model) || typeof m.label !== 'string' || m.label.length > 300 || /[\x00-\x1f\x7f]/.test(m.label) || typeof m.available !== 'boolean' || ![null, 'unverified', 'model-off'].includes(m.reason) || (m.isNew !== undefined && m.isNew !== true) || !Array.isArray(m.harnesses) || m.harnesses.length > 32 || m.harnesses.some(h => !validId(h))) throw Error();
}
if (answer.notices.some(n => typeof n !== 'string' || n.length > 300 || /[\x00-\x1f\x7f]/.test(n))) throw Error();
return answer;
@@ -50,14 +55,15 @@ export function createModelCatalogue({ client, settings, pluginDataRoot = null,
if (writing) return unavailable();
try { if (off(id)) { cache.delete(id); return disabled(); } } catch { return { status: 'unavailable', observedAt: null, models: [], notices: [] }; }
const existing = cache.get(id);
const fingerprint = JSON.stringify([settings(id)['model-provider'], sharedGeneration]);
const offered = offeredNow();
const fingerprint = JSON.stringify([settings(id)['model-provider'], sharedGeneration, offeredMark(offered)]);
if (!fresh && existing && existing.fingerprint === fingerprint && now() - existing.time < 30000) return structuredClone(existing.value);
const flightKey = `${id}:${fingerprint}:${generation}`;
let flight = flights.get(flightKey);
if (!flight || fresh) {
const at = generation;
flight = refresh(id, existing?.value).then(value => {
if (generation !== at || JSON.stringify([settings(id)?.['model-provider'], proxyLock?.generation()]) !== fingerprint) return null;
flight = refresh(id, existing?.value, offered.offered).then(value => {
if (generation !== at || JSON.stringify([settings(id)?.['model-provider'], proxyLock?.generation(), offeredMark(offeredNow())]) !== fingerprint) return null;
if (cache.size >= 128 && !cache.has(id)) cache.delete(cache.keys().next().value);
cache.set(id, { time: now(), fingerprint, value }); return value;
}).catch(() => null);
+4 -2
View File
@@ -1,6 +1,8 @@
// Pure broker/runner parsing vocabulary. No host management or state imports.
export const PROXY_PAGE_VERBS = Object.freeze(['proxy.accounts', 'proxy.login.start', 'proxy.login.submit',
'proxy.login.status', 'proxy.login.cancel', 'proxy.apikey.add', 'proxy.priority.set', 'proxy.group.models.set', 'proxy.keys']);
'proxy.login.status', 'proxy.login.cancel', 'proxy.apikey.add', 'proxy.priority.set', 'proxy.group.models.set', 'proxy.keys',
// #1873 account-remove (Jason 2026-11-20): one account out of the model service, confirmed.
'proxy.account.remove']);
export const PROXY_PAGE_CODES = Object.freeze(['not-configured', 'human-required', 'provider-not-allowed',
'login-state-unknown', 'callback-url-invalid', 'apikey-invalid', 'priority-invalid', 'priority-unconfirmed',
'priority-write-refused', 'models-invalid', 'models-ambiguous', 'models-write-refused', 'models-unconfirmed', 'proxy-uncertain', 'proxy-down', 'invalid-request']);
'priority-write-refused', 'models-invalid', 'models-ambiguous', 'models-write-refused', 'models-unconfirmed', 'remove-write-refused', 'remove-unconfirmed', 'proxy-uncertain', 'proxy-down', 'invalid-request']);
+33
View File
@@ -263,6 +263,39 @@ export class ProxyPage {
return { accountId: row.accountId, priority: args.priority };
});
}
// #1873 account-remove (Jason 2026-11-20): one account out of the model service. A signed-in
// account's credential is deleted; an API key comes out of its key group, and a group left with
// no key comes out whole. Then the accounts are read again: a write the proxy refused is
// remove-write-refused, an account still listed after it is remove-unconfirmed.
if (verb === 'proxy.account.remove') {
fields(args, ['accountId', 'confirm']);
if (typeof args.accountId !== 'string' || !/^[a-f0-9]{12}$/.test(args.accountId) || args.confirm !== true) refuse('invalid-request');
return this.#write(async () => {
const { rows, groups } = await this.#accounts();
const original = this.#lock ? structuredClone(groups) : null;
const matches = rows.filter(row => row.accountId === args.accountId);
if (matches.length !== 1) refuse('invalid-request');
const row = matches[0];
// The DELETE names a credential file; another listed account on the same name would go too.
if (row.source === 'oauth' && rows.some(r => r !== row && r.source === 'oauth' && r.name === row.name)) refuse('invalid-request');
if (this.#lock && !isDeepStrictEqual(await this.#client.getUpstreamGroups(), original)) refuse('proxy-down');
this.#beforeMutation('proxy.account.remove');
try {
if (row.source === 'oauth') await this.#client.deleteCredential(row.name);
else {
const group = groups[row.groupIndex];
const keys = group.keys.filter((_, index) => index !== row.keyIndex);
await this.#client.putUpstreamGroups(keys.length > 0 ? groups.map((g, index) => index === row.groupIndex ? { ...g, keys } : g)
: groups.filter((_, index) => index !== row.groupIndex));
}
} catch (error) {
refuse(error.code === 'proxy-client-refused' || error.code === 'proxy-client-redirect' ? 'remove-write-refused' : 'proxy-down');
}
if ((await this.#accounts()).rows.some(r => r.accountId === args.accountId)) refuse('remove-unconfirmed');
this.#onChange('invalidate');
return { accountId: row.accountId, provider: row.provider, removed: true };
});
}
if (verb === 'proxy.keys') {
fields(args, []);
const values = await this.#client.getKeys();
+3 -1
View File
@@ -71,7 +71,7 @@ group(
"Read current session/status and ask the launcher or owner to finish the supported lifecycle transition. A stale or ended run must not reclaim another holder's identity."
);
group(
"apikey-invalid callback-url-invalid harness-off harness-refused key-quota-no-model-key login-state-unknown model-catalogue-unavailable model-harness-incompatible model-key-collision model-provider-unavailable models-ambiguous models-invalid models-unconfirmed models-write-refused priority-invalid priority-unconfirmed priority-write-refused provider-not-allowed proxy-config-invalid proxy-down proxy-first-output-invalid proxy-init-failed proxy-key-invalid proxy-key-issue-failed proxy-key-refused proxy-key-revoking proxy-open proxy-revoke-revision-conflict proxy-rotate-grace-invalid proxy-uncertain thinking-unsupported",
"apikey-invalid callback-url-invalid harness-off harness-refused key-quota-no-model-key login-state-unknown model-catalogue-unavailable model-harness-incompatible model-key-collision model-provider-unavailable models-ambiguous models-invalid models-unconfirmed models-write-refused priority-invalid priority-unconfirmed priority-write-refused provider-not-allowed proxy-config-invalid proxy-down proxy-first-output-invalid proxy-init-failed proxy-key-invalid proxy-key-issue-failed proxy-key-refused proxy-key-revoking proxy-open proxy-revoke-revision-conflict proxy-rotate-grace-invalid proxy-uncertain remove-unconfirmed remove-write-refused thinking-unsupported",
"R1/R3: owner-managed model and provider configuration",
"Ask the owner to inspect the provider configuration through its supported controls; never put keys or callback contents in messages."
);
@@ -83,6 +83,8 @@ group("proxy-init-busy proxy-init-lock-held proxy-init-lock-absent", "R4: init l
group("proxy-key-transport", "R1/R6: a pasted key travels only over loopback or HTTPS", "Use the generated key, which never leaves the host, or open the Console over HTTPS or on this machine to paste one.");
group("proxy-restart-required proxy-restart-unconfirmed proxy-lock-uncertain", "R4/R6: fence clears only after a confirmed proxy restart", "Restart the proxy from the Proxy page or with mosaic proxy fence clear --restart, so the new process is confirmed on the proxy port, then clear the fence. A fence file with no readable time cannot be proven against: restart the proxy service yourself and run mosaic proxy fence clear --proxy-restarted.");
group("proxy-admin-busy", "R4: one proxy setup action at a time", "Wait for the running fetch, init or unit action to finish (the Proxy page status shows it), then retry.");
// #1873 models-offered (A9, Jason 2026-11-20): the owner's per-provider model lists on the Model setup page.
group("models-offered-conflict models-in-use", "R1/R4: the owner's offered models change one saved list at a time, and never silently under a running organization", "Reload the model list and make the change again. To switch off a model in use, confirm it in place, then give each organization and agent named another model on its Models page.");
group("proxy-key-mismatch", "R1: a pasted management key is confirmed before init", "Type the same key in both fields, or leave them empty to have the bus generate the key.");
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.");
@@ -95,3 +95,45 @@ test('shared generation invalidates cached catalogues and an uncertain fence mak
version++; assert.equal((await service.read('one')).status, 'ready'); assert.equal(calls, 2);
uncertain = true; assert.equal((await service.read('one')).status, 'unavailable');
});
// #1873 models-offered (A9): the owner's on/off file under the instance data root.
test('the catalogue reads the offered file from the plugin data root at every read: a write counts at once, a new model is New and off, a damaged file turns every model off, harnesses never change', async t => {
const { mkdtempSync, rmSync, writeFileSync, mkdirSync, chmodSync } = await import('node:fs');
const { tmpdir } = await import('node:os'); const { join } = await import('node:path');
const { writeOffered } = await import('../../model-proxy/src/models-offered.mjs');
const root = mkdtempSync(join(tmpdir(), 'mo-cat-')); t.after(() => rmSync(root, { recursive: true, force: true }));
let calls = 0;
const names = [{ name: 'glm-5' }];
const client = { getCredentials: async () => ({ files: [] }), getCredentialModels: async () => ({ models: [] }),
getUpstreamGroups: async () => { calls++; return [{ 'base-url': 'https://api.z.ai/api/anthropic', keys: [{}], models: [...names] }]; } };
const service = createModelCatalogue({ client: () => client, settings: () => ({ 'model-provider': ['cliproxy'] }), pluginDataRoot: root, now: () => 1000 });
const view = c => c.models.map(m => [m.model, m.available, m.reason, m.isNew ?? false, m.harnesses.join(',')]);
assert.deepEqual(view(await service.read('one')), [['glm-5', true, null, false, 'claude-code,pi']], 'no file: every model as listed');
writeOffered(root, { version: 1, providers: { 'z.ai': { on: [], seen: ['glm-5'] } } });
assert.deepEqual(view(await service.read('one')), [['glm-5', false, 'model-off', false, 'claude-code,pi']], 'a write counts at the next read, inside the cache window');
assert.equal(calls, 2);
writeOffered(root, { version: 1, providers: { 'z.ai': { on: ['glm-5'], seen: ['glm-5'] } } });
names.push({ name: 'glm-6' });
assert.deepEqual(view(await service.read('one')), [['glm-5', true, null, false, 'claude-code,pi'], ['glm-6', false, 'model-off', true, 'claude-code,pi']]);
const before = calls;
await service.read('one'); assert.equal(calls, before, 'an unchanged file keeps the cache');
const file = join(root, 'state', 'models-offered.json');
for (const [text, mode] of [['{"version":1,"providers":{"z.ai":{"on":["glm-5"],"seen":["glm-5"],"harness":"pi"}}}', 0o600], ['not json', 0o600], ['{"version":1,"providers":{"z.ai":{"on":["glm-5"],"seen":["glm-5"]}}}', 0o644]]) {
writeFileSync(file, text); chmodSync(file, mode);
const out = await service.read('one');
assert.equal(out.status, 'ready', text);
assert.deepEqual(out.models.map(m => [m.available, m.reason]), [[false, 'model-off'], [false, 'model-off']], `damaged (${text}, ${mode.toString(8)}): every model off, never all on`);
}
rmSync(file); mkdirSync(file);
assert.ok((await service.read('one')).models.every(m => m.available === false && m.reason === 'model-off'), 'a directory in place of the file is damaged');
});
test('a provider answer whose isNew is anything but absent or true makes the catalogue unavailable', async () => {
let isNew;
const model = () => ({ key: 'fixture-model', module: 'fixture-check', upstream: 'fixture', provider: 'fixture', model: 'fixture-model', label: 'Fixture', available: true, reason: null, harnesses: ['pi'], ...(isNew === undefined ? {} : { isNew }) });
const module = { catalogue: async () => ({ status: 'ready', models: [model()], notices: [] }) };
const makeLoader = () => ({ load: async () => ({ refused: [] }), snapshot: () => [{ name: 'fixture-check', module }] });
const service = createModelCatalogue({ client: () => null, settings: () => ({ 'model-provider': ['fixture-check'] }), makeLoader });
for (const good of [undefined, true]) { isNew = good; assert.equal((await service.read('one', { fresh: true })).status, 'ready', String(good)); }
for (const bad of [false, 'yes', 1, null, {}]) { isNew = bad; assert.equal((await service.read('one', { fresh: true })).status, 'unavailable', JSON.stringify(bad)); }
});
@@ -143,3 +143,96 @@ test('OAuth callback changes the shared generation for other organization catalo
await s.call('proxy.login.submit', { redirect_url: `http://localhost/?state=${login.state}&code=fixture` });
assert.equal(lock.generation(), 1);
});
// #1873 account-remove (Jason 2026-11-20): proxy.account.remove { accountId, confirm: true }.
const ZAI = 'https://api.z.ai/api/anthropic';
const zaiGroup = (keys, extra = {}) => ({ name: 'fixture-zai', 'base-url': ZAI, models: [{ name: 'glm-fixture' }], ...extra,
keys: keys.map(k => ({ 'api-key': `${k}-secret`, auth_index: k })) });
test('remove: an OAuth account is deleted by its credential name, read back gone, and the catalogue told', async t => {
const changes = [];
const s = await fakeManagement(t, { files: [oauth(), oauth({ id: 'oauth-two.json', name: 'oauth-two.json' })], onChange: c => changes.push(c) });
const result = await s.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true });
assert.deepEqual(result, { accountId: id('oauth-one.json'), provider: 'claude', removed: true });
assert.deepEqual(s.state.deleted, ['oauth-one.json']);
assert.deepEqual((await s.call('proxy.accounts')).accounts.map(a => a.accountId), [id('oauth-two.json')]);
assert.ok(changes.includes('invalidate'));
for (const value of ['p4-private-label', 'p4-private-email', 'p4-private-token', 'oauth-one.json']) {
assert.equal(JSON.stringify(result).includes(value), false);
assert.equal(JSON.stringify(s.state.receipts).includes(value), false);
}
});
test('remove: a z.ai group with one key goes; with two keys only that key goes and the rest of the group stays', async t => {
const s = await fakeManagement(t, { groups: [zaiGroup(['zai-one']), zaiGroup(['zai-two', 'zai-three'], { name: 'fixture-zai-2' })] });
assert.deepEqual(await s.call('proxy.account.remove', { accountId: id('zai-one'), confirm: true }), { accountId: id('zai-one'), provider: 'z.ai', removed: true });
assert.deepEqual(s.state.groups.map(g => g.name), ['fixture-zai-2']);
await s.call('proxy.account.remove', { accountId: id('zai-two'), confirm: true });
assert.deepEqual(s.state.groups.map(g => [g.name, g.keys.map(k => k['api-key']), g.models]), [['fixture-zai-2', ['zai-three-secret'], [{ name: 'glm-fixture' }]]]);
assert.equal(s.state.calls.some(c => c.startsWith('DELETE ')), false);
for (const value of ['zai-one-secret', 'zai-two-secret']) assert.equal(JSON.stringify(s.state.receipts).includes(value), false);
});
test('remove-unconfirmed: the proxy answers ok but the account is still listed', async t => {
const s = await fakeManagement(t, { files: [oauth()], deleteNoop: true });
await assert.rejects(s.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'remove-unconfirmed' });
const g = await fakeManagement(t, { groups: [zaiGroup(['zai-one'])], groupsNoop: true });
await assert.rejects(g.call('proxy.account.remove', { accountId: id('zai-one'), confirm: true }), { code: 'remove-unconfirmed' });
assert.equal(g.state.receives, 1);
});
test('remove-write-refused: a 500 from the proxy, a partial delete, or a refused group write', async t => {
const a = await fakeManagement(t, { files: [oauth()], deleteStatus: 500 });
await assert.rejects(a.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'remove-write-refused' });
const b = await fakeManagement(t, { files: [oauth()], deleteBody: { status: 'partial', deleted: 0, failed: [{ name: 'oauth-one.json' }] }, deleteNoop: true });
await assert.rejects(b.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'remove-write-refused' });
const c = await fakeManagement(t, { groups: [zaiGroup(['zai-one'])], groupsPutStatus: 500 });
await assert.rejects(c.call('proxy.account.remove', { accountId: id('zai-one'), confirm: true }), { code: 'remove-write-refused' });
assert.equal(c.state.groups.length, 1);
});
test('remove: a missing or false confirm, an unknown or malformed id, or an extra field refuses before any write', async t => {
const s = await fakeManagement(t, { files: [oauth()] });
for (const args of [{ accountId: id('oauth-one.json') }, { accountId: id('oauth-one.json'), confirm: false }, { accountId: id('oauth-one.json'), confirm: 'true' },
{ accountId: 'a'.repeat(12), confirm: true }, { accountId: 'oauth-one.json', confirm: true }, { accountId: id('oauth-one.json').toUpperCase(), confirm: true },
{ accountId: id('oauth-one.json'), confirm: true, name: 'oauth-one.json' }])
await assert.rejects(s.call('proxy.account.remove', args), { code: 'invalid-request' });
assert.equal(s.state.calls.some(c => !c.startsWith('GET ')), false);
});
test('remove: a fenced shared proxy refuses with proxy-uncertain and writes nothing', async t => {
const fs = await import('node:fs'); const { join } = await import('node:path'); const { tmpdir } = await import('node:os');
const { createProxyLock } = await import('../src/proxy-lock.mjs');
const dir = fs.mkdtempSync(join(tmpdir(), 'p4-remove-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
const lock = createProxyLock({ stateDir: dir, identity: 'fixture' });
const s = await fakeManagement(t, { lock, org: 'alpha', files: [oauth()], groups: [zaiGroup(['zai-one'])] });
await lock.run(async () => lock.fence({ org: 'beta', op: 'keys.put' }));
await assert.rejects(s.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'proxy-uncertain' });
await assert.rejects(s.call('proxy.account.remove', { accountId: id('zai-one'), confirm: true }), { code: 'proxy-uncertain' });
assert.equal(s.state.calls.some(c => !c.startsWith('GET ')), false);
assert.equal(s.state.files.length, 1);
});
test('remove: under the lock, a delete that fails after the fence leaves the proxy uncertain', async t => {
const fs = await import('node:fs'); const { join } = await import('node:path'); const { tmpdir } = await import('node:os');
const { createProxyLock } = await import('../src/proxy-lock.mjs');
const dir = fs.mkdtempSync(join(tmpdir(), 'p4-remove-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
const lock = createProxyLock({ stateDir: dir, identity: 'fixture' });
const s = await fakeManagement(t, { lock, org: 'alpha', files: [oauth()], deleteStatus: 500 });
await assert.rejects(s.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'proxy-uncertain' });
});
test('remove: two listed accounts on one credential name, or one account id listed twice, refuse before any write', async t => {
// The DELETE names the file, so it would take the second account too while reporting one removed.
const s = await fakeManagement(t, { files: [oauth(), oauth({ id: 'oauth-other-id', name: 'oauth-one.json' })] });
await assert.rejects(s.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'invalid-request' });
await assert.rejects(s.call('proxy.account.remove', { accountId: id('oauth-other-id'), confirm: true }), { code: 'invalid-request' });
const d = await fakeManagement(t, { files: [oauth(), oauth({ name: 'oauth-one-copy.json' })] });
await assert.rejects(d.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'invalid-request' });
for (const x of [s, d]) { assert.equal(x.state.calls.some(c => !c.startsWith('GET ')), false); assert.equal(x.state.files.length, 2); }
});
test('remove: under the lock, key groups that change between the read and the write refuse with proxy-down before the fence', async t => {
const fs = await import('node:fs'); const { join } = await import('node:path'); const { tmpdir } = await import('node:os');
const { createProxyLock } = await import('../src/proxy-lock.mjs');
const dir = fs.mkdtempSync(join(tmpdir(), 'p4-remove-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
const lock = createProxyLock({ stateDir: dir, identity: 'fixture' });
const s = await fakeManagement(t, { lock, org: 'alpha', files: [oauth()], groups: [zaiGroup(['zai-one'])] });
// Another writer adds a key group after the page's first read.
const read = s.client.getUpstreamGroups.bind(s.client); let reads = 0;
s.client.getUpstreamGroups = async () => { const groups = await read(); return ++reads === 2 ? [...groups, zaiGroup(['zai-late'], { name: 'fixture-late' })] : groups; };
await assert.rejects(s.call('proxy.account.remove', { accountId: id('oauth-one.json'), confirm: true }), { code: 'proxy-down' });
assert.equal(s.state.calls.some(c => !c.startsWith('GET ')), false);
assert.equal(lock.generation(), 0, 'no fence was set');
});
+11 -2
View File
@@ -14,6 +14,14 @@ export async function fakeManagement(t, options = {}) {
state.calls.push(req.method + ' ' + u.pathname);
const reply = (status, body) => { res.writeHead(status, { 'content-type': 'application/json' }); res.end(JSON.stringify(body)); };
if (req.headers.authorization !== 'Bearer p4-management') return reply(401, { error: 'refused' });
// #1873 account-remove: DELETE takes {names:[...]} (DeleteAuthFileV8). deleteNoop answers ok and
// keeps the file; deleteStatus and deleteBody shape a refusal.
if (u.pathname === '/v8/management/credentials' && req.method === 'DELETE') {
state.deleted = [...(state.deleted ?? []), ...(value?.names ?? [])];
if ((state.deleteStatus ?? 200) !== 200) return reply(state.deleteStatus, { error: 'p4-private-detail' });
if (!state.deleteNoop) state.files = state.files.filter(f => !(value?.names ?? []).includes(f.name));
return reply(200, state.deleteBody ?? { status: 'ok', deleted: (value?.names ?? []).length, failed: [] });
}
if (u.pathname === '/v8/management/credentials') return reply(200, { files: state.files, observed_at: '2026-10-24T00:00:00Z' });
if (u.pathname === '/v8/management/config/api-keys/claude') {
if (req.method === 'GET') {
@@ -24,7 +32,8 @@ export async function fakeManagement(t, options = {}) {
// Stand-in treats auth_index as derived/read-only metadata.
if (value.some(g => (g.keys ?? []).some(k => Object.hasOwn(k, 'auth_index')))) return reply(422, { error: 'derived field' });
state.receives++;
if (!state.priorityNoop) state.groups = value.map(g => ({ ...g, keys: g.keys.map(k => ({ ...k, auth_index: state.groups.flatMap(old => old.keys ?? []).find(old => old['api-key'] === k['api-key'])?.auth_index ?? `upstream-${nextId++}` })) }));
if (state.groupsPutStatus) return reply(state.groupsPutStatus, { error: 'p4-private-detail' });
if (!state.priorityNoop && !state.groupsNoop) state.groups = value.map(g => ({ ...g, keys: g.keys.map(k => ({ ...k, auth_index: state.groups.flatMap(old => old.keys ?? []).find(old => old['api-key'] === k['api-key'])?.auth_index ?? `upstream-${nextId++}` })) }));
return reply(200, { status: 'ok' });
}
if (u.pathname === '/v8/management/credentials/fields') {
@@ -43,7 +52,7 @@ export async function fakeManagement(t, options = {}) {
await new Promise(r => server.listen(0, '127.0.0.1', r));
t.after(() => new Promise(r => server.close(r)));
const client = new ManagementClient({ baseUrl: `http://127.0.0.1:${server.address().port}`, key: 'p4-management', receipt: entry => state.receipts.push(entry) });
const page = new ProxyPage({ client, instanceTag: 'fixture', lock: options.lock ?? null, org: options.org ?? null, keyRecords: () => options.records ?? [] });
const page = new ProxyPage({ client, instanceTag: 'fixture', lock: options.lock ?? null, org: options.org ?? null, keyRecords: () => options.records ?? [], onChange: options.onChange ?? (() => {}) });
const call = (verb, args = {}) => page.request(verb, args, { business: 'demo', human: 'owner' });
return { state, page, call, client, server };
}
+3 -1
View File
@@ -8,7 +8,9 @@ import { Broker } from '../src/broker.mjs';
import { Store } from '../src/store.mjs';
const verbs = ['proxy.accounts', 'proxy.login.start', 'proxy.login.submit', 'proxy.login.status',
'proxy.login.cancel', 'proxy.apikey.add', 'proxy.priority.set', 'proxy.group.models.set', 'proxy.keys'];
'proxy.login.cancel', 'proxy.apikey.add', 'proxy.priority.set', 'proxy.group.models.set', 'proxy.keys',
// #1873 account-remove: a tester, agent or user is refused like every other proxy verb.
'proxy.account.remove'];
function setup(t, proxyPage = async () => ({ ok: true })) {
const root = mkdtempSync(join(tmpdir(), 'p4-bus-'));
const store = new Store(root);
@@ -468,10 +468,10 @@ test("W3 host watch: the default stat key reads the plugin root's plugins, and a
const dataRoot = scratch(t);
// The plugin root sits beside TMPDIR, not in it: TMPDIR is an env value the backstop already
// knows, so a root under it would be caught without the plugin root as a needle of its own.
// When TMPDIR's parent can't take it, the test skips: a root under TMPDIR would pass with the
// plugin root left out of the backstop.
// With TMPDIR unset, its value cannot mask the check, so a separate scratch directory works.
// Otherwise skip if the parent cannot take a sibling: a child would give a false pass.
let pluginRoot;
try { pluginRoot = mkdtempSync(join(dirname(tmpdir()), "mosaic-hw-plugins-")); }
try { pluginRoot = process.env.TMPDIR ? mkdtempSync(join(dirname(tmpdir()), "mosaic-hw-plugins-")) : scratch(t); }
catch { t.skip("the plugin root must sit beside TMPDIR, and TMPDIR's parent is not writable"); return; }
t.after(() => rmSync(pluginRoot, { recursive: true, force: true }));
const clock = fakeClock();
@@ -487,6 +487,7 @@ test("W3 host watch: the default stat key reads the plugin root's plugins, and a
// A plugin lock that can't be read changes the plugin root's key (the loader refuses it): a full pass.
writeFileSync(pluginPaths(pluginRoot).lock, "{", { mode: 0o600 });
line = `${pluginRoot}/plugins/pi`;
assert.equal(leakChecker({ env: {}, scratch: null, dataRoot })({ line }), false, "without the plugin root the backstop does not catch this path");
clock.fire(); await handle.settled();
assert.equal(stub.calls.length, 2, "the key read the plugin root, so its change ran a full pass");
assert.match(logs.join("\n"), /a state line held a host path or an env value/, "and the record that names the plugin root was not written");
+7 -3
View File
@@ -25,7 +25,7 @@ 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 { fixtureDir, 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";
@@ -82,7 +82,9 @@ function commit(dir, text) {
// Every credential variable of the shell running the tests is dropped, pi's own provider keys
// included (row 332 R2); a pi chief gets one made-up pi key instead.
function prepare(t, more = (doc) => doc) {
const root = tmp(t, "mosaic-launch-");
// pi-fit-tmp re-check: its pi stand-ins sit under root, which must not be under /tmp.
const root = fixtureDir("mosaic-launch-");
t.after(() => rmSync(root, { recursive: true, force: true }));
const f = fixture(root, "acme", (doc) => more(edit(doc)));
withoutHostCredentials(f.env, { piKey: f.doc.roles.chief.vars.harness === "pi" || f.doc.roles.chief.vars["fallback.harness"] === "pi" });
f.env.CLAUDE_CODE_OAUTH_TOKEN = "stand-in";
@@ -332,7 +334,9 @@ function busPiBin(t, prepared, bin) {
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-"), {});
const aloneRoot = fixtureDir("mosaic-launch-pi-");
t.after(() => rmSync(aloneRoot, { recursive: true, force: true }));
const alone = piPrefix(aloneRoot, {});
await quiet();
made.clear();
assert.equal(await pi.preflight({ env: { ANTHROPIC_API_KEY: "stand-in", MOSAIC_PI_BIN: alone } }), null);
+3 -3
View File
@@ -12,7 +12,7 @@ import { tmpdir } from "node:os";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import { sandboxProblem } from "../../seat/src/sandbox.mjs";
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
import { fixtureDir, piStandIn } from "../../harness/tests/pi-stand-in.mjs";
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
const missing = (e) => { if (e.code !== "ERR_MODULE_NOT_FOUND") throw e; return {}; };
@@ -31,7 +31,7 @@ const lockOf = (version) => ({ name: "fixture", lockfileVersion: 3, requires: tr
// A checkout with package.json's range, a lock that resolves `version`, and RELEASE.
function checkout(t, { version = "9.9.9", release = "0.0.13" } = {}) {
const root = mkdtempSync(join(tmpdir(), "s5-"));
const root = fixtureDir("s5-");
t.after(() => rmSync(root, { recursive: true, force: true }));
writeFileSync(join(root, "package.json"), JSON.stringify({ name: "fixture", dependencies: { [NAME]: RANGE } }));
writeFileSync(join(root, "package-lock.json"), JSON.stringify(lockOf(version)));
@@ -267,7 +267,7 @@ test("pi-unpin S5: the image fit step can't tell, and refuses, when node_modules
// scripts/agent-host-dev.sh --check in a checkout with the range in package.json and a stand-in pi.
function hostCheckout(t, piScript) {
const root = mkdtempSync(join(tmpdir(), "s5h-"));
const root = fixtureDir("s5h-");
t.after(() => rmSync(root, { recursive: true, force: true }));
for (const d of ["scripts", "agents/fixture", "contracts", "user", "node_modules/.bin", "extensions/goal"]) mkdirSync(join(root, d), { recursive: true });
cpSync(join(REPO, "scripts/agent-host-dev.sh"), join(root, "scripts/agent-host-dev.sh"));
+3 -2
View File
@@ -328,8 +328,9 @@ can disagree with it.
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
own file at its path unless that path is under `/tmp` (a session's sandbox
mounts its private `/tmp` over it, and the fit check stays undecided for a
node there). 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
+6 -4
View File
@@ -174,10 +174,12 @@ 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.
// there holds no node in the sandbox, and a node the host finds there counts
// as one the sandbox can't show. That holds for this Node's own file too. The
// fit sandbox now mounts that file over its fresh /tmp (pi-fit-tmp's
// mountOrder), but a session's sandbox mounts its private /tmp last and
// covers it, and the fit check stays undecided for a node under /tmp, so an
// engine whose node sits under /tmp still can't run.
const OWN_COPY_DIRS = ["/proc", "/dev"];
const underTmp = (p) => p === "/tmp" || p.startsWith("/tmp/");
function shownInFit(p, hops = 0) {
+2 -2
View File
@@ -11,7 +11,7 @@ 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 { fixtureDir, piStandIn } from "../../harness/tests/pi-stand-in.mjs";
import { locate } from "../../plugins/harness/pi/binary.mjs";
export const REPO = resolve(import.meta.dirname, "..", "..", "..");
@@ -19,7 +19,7 @@ export const SESSION_ID = "0f5e1c2a-1111-4222-8333-944455556666";
export const FAST = Object.freeze({ ack: 1500, state: 1500, start: 1500, clear: 1500, abort: 3000, settle: 3000, grace: 50, write: 1500, maxRounds: 3 });
export const time = (i) => new Date(Date.UTC(2026, 9, 4, 12, 0, 0) + i * 1000).toISOString();
const tmp = mkdtempSync(join(tmpdir(), "chat03-"));
const tmp = fixtureDir("chat03-");
let serial = 0;
// #1873 S4: a stand-in pi package (packages/harness/tests/pi-stand-in.mjs,
+3 -1
View File
@@ -21,13 +21,15 @@ 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 { assistantEntry, header, thinkingEntry, userEntry } from "./harness.mjs";
import { assistantEntry, cleanupAll, 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 }));
// harness.mjs makes its fixture directory when it loads; remove it too.
after(cleanupAll);
const COMMANDS = ["get_state", "get_commands", "clear_queue", "abort", "get_tree"];
const exchanges = {};
+32 -8
View File
@@ -125,7 +125,7 @@ import { spawn } from "node:child_process";
import { createHash } from "node:crypto";
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, relative } from "node:path";
import { dirname, join, posix, relative } from "node:path";
import { fileURLToPath } from "node:url";
import { BWRAP, RUNNER_FILES, SETPRIV, UNSHARE, checkArgv, outerArgv, sessionSetenv } from "../../seat/src/sandbox.mjs";
import { isProviderVariable, sessionEnv } from "../../seat/src/session.mjs";
@@ -258,7 +258,11 @@ const startTime = (pid) => readFileSync(`/proc/${pid}/stat`, "utf8").split(") ")
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()) {
// pi-fit-tmp: where the scratch directories go: the temp directory's real path, so the paths a
// run is given and the paths bwrap mounts are the same spelling when TMPDIR is a link, and the
// sweep matches a left process's command line. tmpdir() itself when that can't be resolved.
const scratchBase = () => { try { return realpathSync(tmpdir()); } catch { return tmpdir(); } };
function sweepScratch(dir = scratchBase()) {
let names;
try {
names = readdirSync(dir);
@@ -347,6 +351,12 @@ export function piFit(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, no
} catch {
return Promise.resolve({ ok: false, message: `${who(null)} can't be checked: its files can't be read`, digest: null, undecided: true });
}
// pi-fit-tmp review P3-1: the fit sandbox orders its binds so nothing under /tmp is hidden, but
// a session's sandbox (binds()) mounts its private /tmp last and covers whatever lies under it.
// A pi mounted there, or a node there, would pass the check and then fail every session, so the
// check stays undecided for it, as it was before the fit sandbox's binds were ordered.
const covered = sessionCovers(found.dest ?? found.root) ? "the pi's package" : sessionCovers(node) ? "node" : null;
if (covered) return Promise.resolve({ ok: false, message: `${who(null)} can't be checked: ${covered} sits under the system temp directory, which a session's sandbox covers`, digest: JSON.parse(key)[0], 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();
@@ -437,7 +447,7 @@ async function check(found, { levels, bwrap, unshare, node, who, digest, host, c
await slot();
try {
if (stop.signal.aborted) return null;
const scratch = mkdtempSync(join(tmpdir(), `${scratchPrefix()}${model ? "model-" : ""}`));
const scratch = mkdtempSync(join(scratchBase(), `${scratchPrefix()}${model ? "model-" : ""}`));
let ran = null;
try {
if (model) {
@@ -490,7 +500,7 @@ async function judge(found, { levels, who, digest, run }) {
// 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");
if (help.status !== 0) return unsure(`--help failed (${ended(help)})`);
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)) ?? "";
@@ -647,7 +657,7 @@ function judgeModel(m) {
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()));
const scratch = mkdtempSync(join(scratchBase(), scratchPrefix()));
let stopped = false;
try {
const probe = async (argv) => {
@@ -688,8 +698,8 @@ function runIn(found, { bwrap, unshare, node, scratch, env, args, input, timeout
return spawned(checkArgv(list, [...prefix, "/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
// One child process with an empty environment, awaited: its exit status, the signal that ended it
// (pi-fit-tmp), 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.
@@ -712,7 +722,7 @@ function spawned(argv, { input, timeout, signal }) {
child?.stderr?.destroy();
signal.removeEventListener("abort", onAbort);
if (!stopped && sig === "SIGKILL") stopped = "timed out";
resolve({ status, stdout: Buffer.concat(out).toString("utf8"), stopped });
resolve({ status, signal: sig ?? null, 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) => {
@@ -745,6 +755,20 @@ function spawned(argv, { input, timeout, signal }) {
});
}
// A path a session's private /tmp covers: /tmp itself or under it, by its spelling (what the
// session's bind names as its target).
const sessionCovers = (p) => typeof p === "string" && /^\/+tmp(\/|$)/.test(posix.normalize(p));
// pi-fit-tmp: how a run ended, for a line that must say enough to diagnose: why the check killed
// it, else the signal that ended it, else its exit status. Never its output, which can name a host
// path.
function ended(r) {
if (r.stopped) return r.stopped;
if (typeof r.signal === "string") return `ended by ${r.signal}`;
if (Number.isInteger(r.status)) return `exit status ${r.status}`;
return "it didn't start";
}
// 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
+129 -10
View File
@@ -10,10 +10,10 @@ import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import * as fitModule from "../src/pi-fit.mjs";
import { piStandIn } from "./pi-stand-in.mjs";
import { fixtureDir, piStandIn } from "./pi-stand-in.mjs";
import { locate } from "../../plugins/harness/pi/binary.mjs";
import * as pi from "../../plugins/harness/pi/index.mjs";
import { BWRAP, UNSHARE, checkArgv, sandboxProblem, sessionSetenv } from "../../seat/src/sandbox.mjs";
import { BWRAP, UNSHARE, checkArgv, mountOrder, sandboxProblem, sessionSetenv } from "../../seat/src/sandbox.mjs";
import { ENV_ALLOW, isProviderVariable, sessionEnv } from "../../seat/src/session.mjs";
import { adapterEnv } from "../src/runner.mjs";
import { FIT_MODEL_ROUTE } from "../../plugins/harness/pi/session-env.mjs";
@@ -29,7 +29,7 @@ const SKIP = sandboxProblem() ? { skip: "no working bwrap here" } : {};
// bin), named by MOSAIC_PI_BIN. Returns the env that names it and the paths a refusal must not
// carry.
function standIn(t, knobs, version = "2.0.0") {
const root = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-t-"));
const root = fixtureDir("mosaic-pi-fit-t-");
t.after(() => rmSync(root, { recursive: true, force: true }));
const pkg = join(root, "prefix", "lib", "node_modules", NAME);
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
@@ -604,7 +604,7 @@ const procsNaming = (text) => readdirSync("/proc").filter((p) => /^\d+$/.test(p)
try { return readFileSync(`/proc/${p}/cmdline`, "utf8").includes(text); } catch { return false; }
});
test("pi-unpin S3 re-check 9: a fit check killed with its process leaves no process, and the next check removes its scratch", SKIP, async t => {
const dir = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-t-"));
const dir = fixtureDir("mosaic-pi-fit-t-");
t.after(() => {
for (const p of procsNaming(dir)) try { process.kill(Number(p), "SIGKILL"); } catch {}
rmSync(dir, { recursive: true, force: true });
@@ -711,7 +711,7 @@ test("pi-unpin S3 re-check 10: bwrap's command line is the same with and without
// nothing. The child sees the host through an outer bwrap; for the missing case /usr/bin is a tmpfs
// with only the tools a check runs bound back.
test("pi-unpin S3 re-check 10: with an unshare that is refused or missing, real pi passes the fit check and no launch is refused", SKIP, async t => {
const dir = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-t-"));
const dir = fixtureDir("mosaic-pi-fit-t-");
t.after(() => rmSync(dir, { recursive: true, force: true }));
const refusing = join(dir, "refusing-unshare");
writeFileSync(refusing, `#!/bin/sh\necho "unshare: write failed /proc/self/uid_map: Operation not permitted" >&2\nexit 1\n`, { mode: 0o755 });
@@ -766,7 +766,7 @@ test("pi-unpin S3 re-check 10: a failed probe names the tool that failed, and on
// is killed while runs are under way. The next check, in another process, must stop what the
// killed one left and remove its scratch, checked by command line.
test("pi-unpin S3 re-check 10: without unshare, a check killed with its process leaves nothing once the next check has run", SKIP, async t => {
const dir = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-t-"));
const dir = fixtureDir("mosaic-pi-fit-t-");
t.after(() => {
for (const p of procsNaming(dir)) try { process.kill(Number(p), "SIGKILL"); } catch {}
rmSync(dir, { recursive: true, force: true });
@@ -847,7 +847,7 @@ exit $st
// stale directory's name, and one naming a directory from another pid namespace are left alone, and
// so are those directories.
test("pi-unpin S3 re-check 10: the sweep leaves a live owner's processes and another directory's processes alone", SKIP, async t => {
const dir = mkdtempSync(join(tmpdir(), "mosaic-pi-fit-t-"));
const dir = fixtureDir("mosaic-pi-fit-t-");
const kids = [];
t.after(() => {
for (const k of kids) try { k.kill("SIGKILL"); } catch {}
@@ -913,7 +913,7 @@ test("pi-unpin S3: real pi 0.85.1 from node_modules is accepted, and the event l
// directories: it lists both given skills and no planted one. A logging bwrap shows the run.
test("pi-skills: real pi 0.85.1 passes the fit check's worker-shaped run with --no-skills and two --skill directories", SKIP, async t => {
const found = locate({ PATH: "/usr/bin:/bin" });
const root = mkdtempSync(join(tmpdir(), "mosaic-pi-skills-fit-"));
const root = fixtureDir("mosaic-pi-skills-fit-");
t.after(() => rmSync(root, { recursive: true, force: true }));
const log = join(root, "bwrap.log");
const bwrap = join(root, "bwrap");
@@ -1005,6 +1005,125 @@ test("pi-unpin S3 re-check 8: a fit check that throws refuses as harness-feature
assert.equal(await pi.preflight({ env: { ...CREDENTIAL, ...s.env } }), null);
});
// pi-fit-tmp: a fit run binds its scratch at its own path and the scratch's tmp/ over /tmp. bwrap
// mounts in the order given, so with the scratch under /tmp the /tmp bind hid the scratch and every
// run failed before pi started: "can't be checked: --help failed". This is the one test that writes
// under /tmp (Jason keeps agents out of it): one mkdtemp directory, removed in a finally. The
// stand-in lacks --no-themes, so the check stops after --version and --help: three scratch
// directories of a few kilobytes, not a whole check's runs. The verdict must be the one a TMPDIR
// outside /tmp gives, also when TMPDIR is a link to it, and the check must leave nothing there.
test("pi-fit-tmp: with its temp directory under /tmp the fit check gives the verdict it gives elsewhere, and leaves nothing there", SKIP, async t => {
const s = standIn(t, { dropFlags: ["--no-themes"] });
const fitUrl = new URL("../src/pi-fit.mjs", import.meta.url).href;
const binUrl = new URL("../../plugins/harness/pi/binary.mjs", import.meta.url).href;
const script = `const { piFit } = await import(${JSON.stringify(fitUrl)}); const { locate } = await import(${JSON.stringify(binUrl)});
const r = await piFit(locate(${JSON.stringify(s.env)}), { levels: ${JSON.stringify(LEVELS)} }); console.log(JSON.stringify(r));`;
// Each check in its own process, so the in-process pass cache can't answer for the other.
const checkWith = (tmp) => {
const r = spawnSync(process.execPath, ["--input-type=module", "-e", script], { env: { PATH: NODE_PATH, HOME: s.root, LANG: "C.UTF-8", TMPDIR: tmp }, encoding: "utf8", timeout: 60000 });
assert.equal(r.status, 0, r.stderr);
return JSON.parse(r.stdout);
};
const elsewhere = checkWith(s.root);
assert.deepEqual([elsewhere.ok, elsewhere.undecided, elsewhere.message], [false, false, "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-themes"]);
const dir = mkdtempSync("/tmp/mosaic-fit-tmp-");
try {
const under = checkWith(dir);
assert.deepEqual([under.ok, under.undecided, under.message], [elsewhere.ok, elsewhere.undecided, elsewhere.message]);
assert.deepEqual(readdirSync(dir), [], "the check left something in its temp directory under /tmp");
// TMPDIR a link to the directory under /tmp, in a directory the sandbox mounts (the pi's own
// package), so bwrap follows the link: the scratch goes under the real path, so the binds'
// order sees it under /tmp.
const link = join(dirname(dirname(dirname(s.cli))), "tmp-link");
symlinkSync(dir, link);
const linked = checkWith(link);
assert.deepEqual([linked.ok, linked.undecided, linked.message], [elsewhere.ok, elsewhere.undecided, elsewhere.message]);
assert.deepEqual(readdirSync(dir), [], "the check left something in its temp directory under /tmp");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
// pi-fit-tmp: a bind moves ahead of the first bind whose target lies under its own; nothing else
// moves, and binds with the same target keep their order.
test("pi-fit-tmp: mountOrder puts a bind before every bind whose target lies under its own, and moves nothing else", () => {
const ro = (src, dest) => ({ kind: "ro", src, ...(dest ? { dest } : {}) });
const rw = (src, dest) => ({ kind: "rw", src, ...(dest ? { dest } : {}) });
const scratch = "/tmp/mosaic-pi-fit-1-2-3-x";
assert.deepEqual(mountOrder([ro("/repo"), rw(scratch), rw(`${scratch}/tmp`, "/tmp"), ro("/opt/pi")]),
[ro("/repo"), rw(`${scratch}/tmp`, "/tmp"), rw(scratch), ro("/opt/pi")]);
// A scratch outside /tmp: nothing lies under another, so the order is the caller's.
const outside = [ro("/repo"), rw("/srv/s"), rw("/srv/s/tmp", "/tmp"), ro("/opt/pi")];
assert.deepEqual(mountOrder(outside), outside);
// A name that only starts with the other's is not under it.
assert.deepEqual(mountOrder([ro("/tmpx/a"), rw("/x", "/tmp")]), [ro("/tmpx/a"), rw("/x", "/tmp")]);
// The same target twice: the later still comes later and covers the earlier.
assert.deepEqual(mountOrder([ro("/a", "/t"), ro("/b", "/t")]), [ro("/a", "/t"), ro("/b", "/t")]);
// Nested three deep, given innermost first.
assert.deepEqual(mountOrder([ro("/a/b/c"), ro("/a/b"), ro("/a")]), [ro("/a"), ro("/a/b"), ro("/a/b/c")]);
// A bind at / goes before everything under it.
assert.deepEqual(mountOrder([ro("/usr"), ro("/r", "/")]), [ro("/r", "/"), ro("/usr")]);
// Targets compare in plain form: a trailing slash or a "." still names the same directory.
assert.deepEqual(mountOrder([ro("/a/x"), ro("/b", "/a/")]), [ro("/b", "/a/"), ro("/a/x")]);
assert.deepEqual(mountOrder([ro("/a/x"), ro("/b", "/a/./")]), [ro("/b", "/a/./"), ro("/a/x")]);
assert.deepEqual(mountOrder([ro("/t/a"), rw("/x", "//t")]), [rw("/x", "//t"), ro("/t/a")]);
// An entry with no target, or one that isn't absolute, stays where it is and moves nothing.
const odd = [ro("/a/x"), { kind: "none" }, ro("rel"), ro("/c")];
assert.deepEqual(mountOrder(odd), odd);
// The limit, by design: no bind stays hidden under a later one. /c at /a no longer covers /a/x.
assert.deepEqual(mountOrder([ro("/b", "/a"), ro("/a/x"), ro("/c", "/a")]), [ro("/b", "/a"), ro("/c", "/a"), ro("/a/x")]);
// In the fit check's own argv the /tmp bind comes before the scratch's.
const argv = checkArgv([rw(scratch), rw(`${scratch}/tmp`, "/tmp")], ["/usr/bin/true"], { chdir: scratch });
const at = (src, dest) => argv.findIndex((a, i) => a === "--bind" && argv[i + 1] === src && argv[i + 2] === dest);
assert.ok(at(`${scratch}/tmp`, "/tmp") >= 0 && at(scratch, scratch) > at(`${scratch}/tmp`, "/tmp"), "the scratch bind is not after the /tmp bind");
});
// pi-fit-tmp: when --help fails the line says how it ended, an exit status or a signal, and names no
// path. A bwrap stand-in fails only the --help run and passes the sandbox probe through.
test("pi-fit-tmp: a --help run that fails is undecided and its line gives the exit status or the signal, with no path", SKIP, async t => {
for (const [end, ending] of [["exit 3", "exit status 3"], ["kill -TERM $$", "ended by SIGTERM"]]) {
const s = standIn(t, {});
const bwrap = join(s.root, "help-bwrap");
writeFileSync(bwrap, `#!/bin/sh
case " $* " in *" --help "*) ${end};; esac
exec '${BWRAP}' "$@"
`, { mode: 0o755 });
const r = await piFit(locate(s.env), { levels: LEVELS, bwrap, unshare: join(s.root, "no-unshare") });
assert.deepEqual([r.ok, r.undecided], [false, true], r.message);
assert.equal(r.message, `pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: --help failed (${ending})`);
noPath(r.message, [...s.paths, bwrap]);
}
});
// pi-fit-tmp review P3-1: the fit sandbox now shows what lies under /tmp, but a session's sandbox
// mounts its private /tmp last and covers it. A pi mounted under /tmp, or a node there, stays
// undecided, with a line that names no path, and no run starts (the bwrap stand-in would record one).
test("pi-fit-tmp: a pi mounted under /tmp, or a node there, can't be checked, since a session's sandbox covers it", SKIP, async t => {
const s = standIn(t, {});
const bwrap = join(s.root, "record-bwrap");
const ran = join(s.root, "ran");
writeFileSync(bwrap, `#!/bin/sh\necho x >> '${ran}'\nexec '${BWRAP}' "$@"\n`, { mode: 0o755 });
const found = locate(s.env);
const cases = [
[{ ...found, dest: null, root: "/tmp/mosaic-fit-root" }, {}, "the pi's package"],
[{ ...found, dest: "/tmp/mosaic-fit-dest" }, {}, "the pi's package"],
[{ ...found, dest: "//tmp" }, {}, "the pi's package"],
[found, { node: "/tmp/mosaic-fit-node" }, "node"],
];
for (const [f, extra, what] of cases) {
const r = await piFit(f, { levels: LEVELS, bwrap, ...extra });
assert.deepEqual([r.ok, r.undecided], [false, true], r.message);
assert.equal(r.message, `pi (MOSAIC_PI_BIN) can't be checked: ${what} sits under the system temp directory, which a session's sandbox covers`);
assert.match(r.digest, /^[0-9a-f]{64}$/);
noPath(r.message, [...s.paths, bwrap, "/tmp"]);
}
assert.equal(existsSync(ran), false, "a run started");
// A name that only starts with tmp is not under it, and the check runs as usual.
const r = await piFit({ ...found, dest: "/tmpx/mosaic-fit-dest" }, { levels: LEVELS, bwrap });
assert.doesNotMatch(r.message ?? "", /system temp directory/);
assert.equal(existsSync(ran), true, "no run started for /tmpx");
});
test("pi-unpin S3: the adapter passes --no-mcp only when the module asks for it", SKIP, async t => {
const session = { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" };
const run = async (s) => {
@@ -1026,7 +1145,7 @@ test("pi-unpin S3: the adapter passes --no-mcp only when the module asks for it"
// pi-skills: a worker's MOSAIC_SKILLS. pi 0.85.1 adds a --skill directory to discovery, so the
// adapter keeps --no-skills and puts each --skill after it. A stub records the argv.
const workerAdapter = (t, extra = {}) => {
const root = mkdtempSync(join(tmpdir(), "mosaic-pi-skills-t-"));
const root = fixtureDir("mosaic-pi-skills-t-");
t.after(() => rmSync(root, { recursive: true, force: true }));
const stub = join(root, "pi");
writeFileSync(stub, '#!/bin/sh\nprintf "%s\\0" "$@" > "$ARGV_OUT"\n', { mode: 0o755 });
@@ -1086,7 +1205,7 @@ const UNSHARE_NET = spawnSync("unshare", ["-rn", "true"]).status === 0 ? {} : {
test("pi-skills: real pi 0.85.1 through the adapter with MOSAIC_SKILLS loads the given skills and no planted one", UNSHARE_NET, async t => {
const found = locate({ PATH: "/usr/bin:/bin" });
assert.equal(found.label, "the repository's pi");
const root = mkdtempSync(join(tmpdir(), "mosaic-pi-skills-real-"));
const root = fixtureDir("mosaic-pi-skills-real-");
t.after(() => rmSync(root, { recursive: true, force: true }));
const put = (p, text) => { mkdirSync(dirname(p), { recursive: true }); writeFileSync(p, text); };
const skill = (name) => put(join(root, ...name.split("/"), "SKILL.md"), `---\nname: ${name.split("/").pop()}\ndescription: a planted skill\n---\nA planted skill.\n`);
+22
View File
@@ -84,6 +84,28 @@
// 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";
import { FIT_MODEL_ROUTE } from "../../plugins/harness/pi/session-env.mjs";
import { mkdtempSync, realpathSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
// pi-fit-tmp re-check: a new directory for a test's stand-in pi, made like
// mkdtempSync(join(tmpdir(), prefix)). The fit check is undecided for a pi under /tmp, since a
// session's sandbox mounts its private /tmp over it, so a stand-in must not sit there. When the
// temp directory's real path is under /tmp (TMPDIR unset, say), the directory goes under /var/tmp
// instead; otherwise it goes under tmpdir() as spelled, so a run with TMPDIR elsewhere is unchanged.
// With /var/tmp missing or under /tmp too, it throws, so a test fails on its fixture and not with
// the guard's line, which would point at the code under test.
const underTmp = (p) => /^\/tmp(\/|$)/.test(p);
const realOr = (p) => { try { return realpathSync(p); } catch { return null; } };
export function fixtureDir(prefix) {
let base = tmpdir();
if (underTmp(realOr(base) ?? base)) {
const alt = realOr("/var/tmp");
if (alt === null || underTmp(alt)) throw new Error("fixtureDir: the temp directory sits under /tmp, where the fit check won't run a pi, and /var/tmp is missing or under /tmp too; set TMPDIR to a directory outside /tmp");
base = "/var/tmp";
}
return mkdtempSync(join(base, prefix));
}
const BODY = String.raw`
const fs = require("node:fs");
@@ -0,0 +1,75 @@
// #1873 account-remove: what removing one model proxy account takes away, for the Settings ask.
// Pure over two reads the caller makes: the bus's accounts read (proxy.accounts) and each
// organization's routes read. It names only facts those reads hold. A model "moves" when no
// account left would list it in the catalogue (cliproxy catalogue()): an OAuth account lists its
// provider's models only while ready or active, a key group lists its configured models while it
// has an enabled key. When another usable account of the same provider stays and the reads cannot
// say which models it serves (an OAuth account's list is not in the accounts read), the answer is
// unknown rather than a guess. Nothing here says where an instance goes next: the routes read has
// no default to fall back to, so the page names the instance and points at the Models page.
import { MODEL_ID, providerUpstream, PROVIDERS } from '../../plugins/model-provider/cliproxy/index.mjs';
const USABLE = new Set(['ready', 'active']);
const ACCOUNT = /^[a-f0-9]{12}$/;
const ORG = /^[a-z0-9][a-z0-9._-]{0,63}$/;
const INSTANCE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
const UPSTREAM = /^[a-z0-9][a-z0-9._-]{0,31}$/;
const strings = list => Array.isArray(list) && list.every(m => typeof m === 'string' && MODEL_ID.test(m));
// accounts: proxy.accounts' accounts list. Answers what moves, or null when the account is not listed.
export function accountMoves(accounts, accountId, providers = PROVIDERS) {
if (!Array.isArray(accounts) || typeof accountId !== 'string' || !ACCOUNT.test(accountId)) return null;
const target = accounts.find(a => a?.accountId === accountId);
if (!target) return null;
const upstream = providerUpstream(target.provider, providers);
const base = { accountId, provider: typeof target.provider === 'string' && UPSTREAM.test(target.provider) ? target.provider : 'other' };
if (!USABLE.has(target.status)) return { ...base, upstream, moves: 'none', models: [], others: 0 };
if (!upstream || upstream === 'other') return { ...base, upstream: null, moves: 'unknown', models: null, others: 0 };
const others = accounts.filter(a => a !== target && a?.provider === target.provider && USABLE.has(a?.status));
if (!others.length && !strings(target.models)) return { ...base, upstream, moves: 'all', models: null, others: 0 };
// A key account names its models; so do the others of its provider. Then the difference is exact.
if (strings(target.models) && others.every(a => strings(a.models))) {
const kept = new Set(others.flatMap(a => a.models));
const models = [...new Set(target.models.filter(m => !kept.has(m)))];
return { ...base, upstream, moves: models.length ? 'some' : 'none', models, others: others.length };
}
return { ...base, upstream, moves: 'unknown', models: null, others: others.length };
}
// view: one organization's routes read. Answers the instances whose route or fallback names a
// model that moves, as "upstream / model".
export function routeUses(view, { upstream, moves, models }) {
if (!['all', 'some'].includes(moves)) return [];
if (!view || typeof view !== 'object' || !view.instances || typeof view.instances !== 'object') throw new Error('routes');
const leaving = new Set(models ?? []), table = view.providerModels && typeof view.providerModels === 'object' ? view.providerModels : {};
const hit = key => {
const entry = typeof key === 'string' && Object.hasOwn(table, key) ? table[key] : null;
if (!entry || entry.module !== 'cliproxy' || entry.upstream !== upstream || typeof entry.model !== 'string' || !MODEL_ID.test(entry.model)) return null;
return moves === 'all' || leaving.has(entry.model) ? `${upstream} / ${entry.model}` : null;
};
const uses = [];
for (const [instance, route] of Object.entries(view.instances)) {
if (!INSTANCE.test(instance) || !route || typeof route !== 'object') continue;
const main = hit(route.model);
if (main) uses.push({ instance, model: main, fallback: false });
const spare = route.fallback && typeof route.fallback === 'object' ? hit(route.fallback.model) : null;
if (spare) uses.push({ instance, model: spare, fallback: true });
}
return uses;
}
// riders: [{ id, cliproxy, call }] (webui rideRiders), or null when the list could not be read,
// which answers organizations: null rather than none. Only an organization that lists cliproxy can
// route to these models. Every bus is asked at once, so a hung one costs one timeout.
export async function accountImpact({ accounts, accountId, riders, providers = PROVIDERS }) {
const moved = accountMoves(accounts, accountId, providers);
if (!moved) return null;
if (!['all', 'some'].includes(moved.moves)) return { ...moved, organizations: [] };
if (!Array.isArray(riders)) return { ...moved, organizations: null };
const list = riders.filter(r => r?.cliproxy === true && typeof r.id === 'string' && ORG.test(r.id) && typeof r.call === 'function');
const organizations = await Promise.all(list.map(async rider => {
try { return { id: rider.id, readable: true, uses: routeUses(await rider.call('routes'), moved) }; }
catch (error) { return error?.code === 'launch-not-configured' ? { id: rider.id, readable: true, uses: [] } : { id: rider.id, readable: false }; }
}));
return { ...moved, organizations };
}
@@ -9,9 +9,11 @@ import { PIN } from './common.mjs';
import { localRide, riddenOrgs } from './ride.mjs';
import { releaseState, initState, fenceState, initLockStatus, clearInitLock, appendReceipt, recentReceipts, receiptsText, createUnitControl, FILE_LABELS, LOG_LINES, receiptHistory, serviceKeyState } from './admin.mjs';
import { secretValues } from './redact.mjs';
import { accountImpact } from './account-impact.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';
import { offeredControl } from './models-offered.mjs';
export const proxyCode = (error, fallback) => (/^proxy-[a-z-]+$/.test(error?.code ?? '') ? error.code : fallback);
const failure = code => Object.assign(new Error(code), { code });
@@ -246,6 +248,8 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
return ['accounts'];
}
return {
// #1873 models-offered (A9): models() and modelsSet(), the owner's per-provider on/off lists.
...offeredControl({ dataRoot, riders, receipt, exclusive, rootAgents }),
unit: control.unit,
async status() {
// The job is read before the release: a fetch that ends during the awaits below then reads
@@ -427,6 +431,38 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
if (typeof user === 'string' && user) record.user = user.slice(0, 64);
try { receipt(record); return true; } catch { return false; }
},
// #1873 account-remove (Jason 2026-11-20): the instance owner removes one account through the
// proxy bus's proxy.account.remove, which deletes it, reads back and refuses if it is still
// listed. The receipt is written after the bus answers: the op, the provider label and the
// 12-hex account id, never a key, a credential name or a model list. A receipt that cannot be
// written still answers what happened, with no receipt.
async accountRemove(call, { accountId, user = null } = {}) {
return exclusive('account-remove', async () => {
if (typeof call !== 'function') throw failure('not-configured');
if (typeof accountId !== 'string' || !/^[a-f0-9]{12}$/.test(accountId)) throw failure('invalid-request');
const result = await call('proxy.account.remove', { accountId, confirm: true });
// Only an answer that says this account was removed is written as a removal.
if (result?.removed !== true || result.accountId !== accountId) throw failure('remove-unconfirmed');
const provider = typeof result?.provider === 'string' && /^[a-z0-9][a-z0-9._-]{0,31}$/.test(result.provider) ? result.provider : null;
const record = { op: 'account.remove', provider, accountId };
if (typeof user === 'string' && user) record.user = user.slice(0, 64);
let written = null;
try { written = receipt(record); } catch { written = null; }
return { accountId, provider, removed: true, receipt: written };
});
},
// What removing that account takes away (account-impact.mjs): the accounts read from the proxy
// bus, then each organization's routes. An account the read does not list is invalid-request.
async accountImpact(call, { accountId } = {}) {
if (typeof call !== 'function') throw failure('not-configured');
if (typeof accountId !== 'string' || !/^[a-f0-9]{12}$/.test(accountId)) throw failure('invalid-request');
const read = await call('proxy.accounts', {});
let list;
try { list = await riders(); } catch { list = null; }
const answer = await accountImpact({ accounts: read?.accounts, accountId, riders: list, providers });
if (!answer) throw failure('invalid-request');
return answer;
},
// #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.
+17 -2
View File
@@ -20,11 +20,14 @@ export class ManagementClient {
#request(method, endpoint, payload, onStatus) {
// node:http addresses the fixed numeric loopback directly. An ambient fetch
// dispatcher or HTTP_PROXY cannot send the Authorization header elsewhere.
// #1873 account-remove: Content-Length is set here because node:http sends a DELETE body with
// neither it nor chunked framing, and the server then reads no body at all.
let deadline;
const body = payload === undefined ? undefined : JSON.stringify(payload);
return new Promise((resolve, reject) => {
let received = false;
const req = http.request(this.#base + endpoint, {
method, headers: { Authorization: `Bearer ${this.#key}`, ...(payload !== undefined ? { 'Content-Type': 'application/json' } : {}) },
method, headers: { Authorization: `Bearer ${this.#key}`, ...(body !== undefined ? { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) } : {}) },
}, response => {
received = true; onStatus(response.statusCode);
const error = response.statusCode >= 300 && response.statusCode < 400 ? 'proxy-client-redirect'
@@ -45,7 +48,7 @@ export class ManagementClient {
});
req.on('error', () => reject(new ProxyError(received ? 'proxy-client-response' : 'proxy-client-down')));
deadline = setTimeout(() => { req.destroy(); reject(new ProxyError('proxy-client-down')); }, this.#timeout);
req.end(payload === undefined ? undefined : JSON.stringify(payload));
req.end(body);
}).finally(() => clearTimeout(deadline));
}
async #call(method, keys) {
@@ -108,6 +111,18 @@ export class ManagementClient {
return this.#page('PUT', '/v8/management/config/api-keys/claude', payload, 200);
}
patchCredential(name, priority) { return this.#page('PATCH', '/v8/management/credentials/fields', { name, priority }, 200); }
// #1873 account-remove: CLIProxyAPI 8.0.79 deletes credential files by name (DeleteAuthFileV8;
// the panel PIN pins, management.html 1.26.7 by panel.sha256, deletes with
// W.delete(`/credentials`,{data:{names:t}}) under base `/v8/management`). The answer may
// carry status ('ok' or 'partial') and a failed list; the panel reads no status as ok when nothing
// failed, and so does this. A partial answer or any failed entry is a refused write.
async deleteCredential(name) {
if (typeof name !== 'string' || !name || name.length > 512 || /[\r\n\x00]/.test(name)) refuse('proxy-client-invalid');
const answer = await this.#page('DELETE', '/v8/management/credentials', { names: [name] }, 200);
if (!answer || typeof answer !== 'object' || Array.isArray(answer) || (answer.status !== undefined && answer.status !== 'ok')
|| (answer.failed !== undefined && answer.failed !== null && (!Array.isArray(answer.failed) || answer.failed.length > 0))) refuse('proxy-client-refused');
return { deleted: true };
}
oauthStart(provider) {
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}`);
+299
View File
@@ -0,0 +1,299 @@
// #1873 models-offered (A9, Jason 2026-11-20): which proxy models the instance owner offers, per
// provider. One state file, <dataRoot>/state/models-offered.json:
//
// { "version": 1, "providers": { "<provider id>": { "on": [modelId...], "seen": [modelId...] } } }
//
// It holds model ids and provider ids only: no harness, no key, no label. Where a model may run
// always comes from the plugin's catalogue(), so this file can switch a model off but can never
// widen where it runs.
//
// The bus reads it (model-catalogue.mjs passes it to catalogue() as `offered`); only the Console
// writes it (admin-control.mjs, under the admin lock, with a receipt). A file that is not a regular
// file owned by this user with no group or other bits, or that does not parse to the shape above,
// reads as damaged, and the catalogue then offers nothing. It never falls back to all on.
import fs from 'node:fs';
import path from 'node:path';
import { createHash, randomBytes } from 'node:crypto';
import { MODEL_ID, offeredLabel, offeredProvider } from '../../plugins/model-provider/cliproxy/index.mjs';
export const OFFERED_VERSION = 1;
export const MAX_ON = 256;
// A provider's seen list keeps every id it has ever listed, so it may grow past MAX_ON; this bounds
// the file instead.
const MAX_SEEN = 10000;
const MAX_PROVIDERS = 32;
const MAX_BYTES = 4 * 1024 * 1024;
const PROVIDER_ID = /^[a-z0-9][a-z0-9._-]{0,63}$/;
export const offeredFile = dataRoot => path.join(dataRoot, 'state', 'models-offered.json');
// The revision a write must name: a hash of the file's text, or of nothing when there is no file.
export const offeredRevision = text => createHash('sha256').update(text ?? '').digest('hex').slice(0, 16);
const plain = v => !!v && typeof v === 'object' && !Array.isArray(v) && Object.getPrototypeOf(v) === Object.prototype;
const idList = (list, max) => Array.isArray(list) && list.length <= max && list.every(id => typeof id === 'string' && MODEL_ID.test(id)) && new Set(list).size === list.length;
// The parsed shape, or null when anything is off. Unknown fields refuse, so a harness or a key can
// never ride along in the file.
export function parseOffered(doc) {
if (!plain(doc) || Object.keys(doc).some(k => k !== 'version' && k !== 'providers') || doc.version !== OFFERED_VERSION || !plain(doc.providers)) return null;
const ids = Object.keys(doc.providers);
if (ids.length > MAX_PROVIDERS) return null;
const providers = {};
for (const id of ids) {
const entry = doc.providers[id];
if (!PROVIDER_ID.test(id) || !plain(entry) || Object.keys(entry).some(k => k !== 'on' && k !== 'seen')) return null;
if (!idList(entry.on, MAX_ON) || !idList(entry.seen, MAX_SEEN)) return null;
const seen = new Set(entry.seen);
if (entry.on.some(m => !seen.has(m))) return null;
providers[id] = { on: [...entry.on], seen: [...entry.seen] };
}
return { version: OFFERED_VERSION, providers };
}
// { state: 'absent' | 'ok' | 'damaged', offered, revision }. `offered` is null when absent and
// 'damaged' when damaged, which catalogue() reads as all off. `uid` is a test seam.
export function readOffered(dataRoot, { uid = process.getuid() } = {}) {
let fd;
try { fd = fs.openSync(offeredFile(dataRoot), fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK); }
catch (error) {
if (error?.code === 'ENOENT') return { state: 'absent', offered: null, revision: offeredRevision(null) };
return { state: 'damaged', offered: 'damaged', revision: null };
}
try {
const stat = fs.fstatSync(fd);
if (!stat.isFile() || stat.uid !== uid || (stat.mode & 0o077) || stat.nlink !== 1 || stat.size > MAX_BYTES) return { state: 'damaged', offered: 'damaged', revision: null };
const text = fs.readFileSync(fd, 'utf8');
let offered = null;
try { offered = parseOffered(JSON.parse(text)); } catch { offered = null; }
if (!offered) return { state: 'damaged', offered: 'damaged', revision: null };
return { state: 'ok', offered, revision: offeredRevision(text) };
} catch {
return { state: 'damaged', offered: 'damaged', revision: null };
} finally { fs.closeSync(fd); }
}
// The whole file, written by a 0600 temp file in the same directory, fsync, rename, fsync of the
// directory. The caller holds the admin lock and has checked the revision. Answers the new revision.
export function writeOffered(dataRoot, offered) {
const doc = parseOffered(offered);
if (!doc) throw Object.assign(new Error('models-invalid'), { code: 'models-invalid' });
const dir = path.join(dataRoot, 'state');
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
const text = JSON.stringify(doc, null, 2) + '\n';
const temp = path.join(dir, `.models-offered.${randomBytes(6).toString('hex')}.tmp`);
const fd = fs.openSync(temp, fs.constants.O_WRONLY | fs.constants.O_CREAT | fs.constants.O_EXCL | fs.constants.O_NOFOLLOW, 0o600);
try {
fs.fchmodSync(fd, 0o600);
fs.writeFileSync(fd, text);
fs.fsyncSync(fd);
} catch (error) { fs.closeSync(fd); try { fs.unlinkSync(temp); } catch { /* gone */ } throw error; }
fs.closeSync(fd);
try { fs.renameSync(temp, offeredFile(dataRoot)); } catch (error) { try { fs.unlinkSync(temp); } catch { /* gone */ } throw error; }
const dfd = fs.openSync(dir, fs.constants.O_RDONLY);
try { fs.fsyncSync(dfd); } finally { fs.closeSync(dfd); }
return offeredRevision(text);
}
// ---------------------------------------------------------------------------------------------
// The Console's read and write for the instance owner (admin-control.mjs spreads this in).
// `riders()` lists the active organizations as Ride all reads them; each that lists cliproxy is
// asked for its routes, which carry its catalogue (every cliproxy model, on or off) and which
// model each instance and fallback uses. `receipt` and `exclusive` are the admin's own.
const failure = (code, extra = {}) => Object.assign(new Error(code), { code, ...extra });
const ORG_ID = /^[a-z0-9][a-z0-9._-]{0,63}$/;
const REVISION = /^[a-f0-9]{16}$/;
const RECEIPT_LIST = 32;
const AGENT_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/;
// Every launch.models key a document names for an agent, with the agent it sits under: each `model`
// or `fallback.model` string anywhere in it outside `plugins` and `launch` (a launch.models entry's
// own `model` is the upstream id, not a key). Broader than the routes view on purpose, the way
// admin-control.mjs harnessesNamed is: role vars, project agents, definitions and any later place
// count, so a switch-off asks wherever the file still names the model. Null when not a document.
export function modelsNamed(document) {
if (document === null || typeof document !== 'object' || Array.isArray(document)) return null;
const found = [];
const walk = (value, depth, owner) => {
if (depth > 32 || value === null || typeof value !== 'object') return;
if (Array.isArray(value)) { for (const item of value) walk(item, depth + 1, owner); return; }
for (const [key, item] of Object.entries(value)) {
if ((key === 'model' || key === 'fallback.model') && typeof item === 'string') found.push({ instance: owner, key: item, fallback: key === 'fallback.model' });
else walk(item, depth + 1, key === 'vars' || key === 'define' || key === 'narrow' ? owner : (AGENT_NAME.test(key) ? key : null));
}
};
for (const [key, value] of Object.entries(document)) if (key !== 'plugins' && key !== 'launch') walk(value, 1, null);
return found;
}
// The same for the root agents an organization takes (every one but the root assistant and those
// that exclude it, as admin-control.mjs rootHarnessesNamed reads them). `root` is what
// loadRootAgents answered; anything not in that shape answers null (unreadable).
export function rootModelsNamed(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 modelsNamed({ agents: taken, definitions: root.definitions ?? {} });
}
export function offeredControl({ dataRoot, riders, receipt, exclusive, rootAgents = null, uid = process.getuid() }) {
const readFile = () => {
const file = readOffered(dataRoot, { uid });
if (file.state === 'damaged') throw failure('outcome-unknown');
return file;
};
// Every provider's present models, in catalogue order, and who uses which. A model is in use when
// a model or fallback model key resolves (the organization's model table, or the bus's legacy
// mapping) to that cliproxy upstream and model id. The keys come from the routes view's instances,
// from every place the organization file names one (project agents too) and from the root agents
// the organization takes. `unanswered` names each organization whose usage is unknown: its file
// could not be read, its bus did not answer, its catalogue (and so its legacy mapping) was not
// ready, or the root agent file could not be read. A write that switches a model off treats each
// one as a possible user.
async function survey() {
let list;
try { list = await riders(); } catch { throw failure('outcome-unknown'); }
const named = (Array.isArray(list) ? list : []).filter(r => typeof r?.id === 'string' && ORG_ID.test(r.id));
const riding = named.filter(r => r.cliproxy === true);
if (!riding.length) throw failure('not-configured');
const unanswered = new Set(named.filter(r => typeof r.cliproxy !== 'boolean').map(r => r.id));
let root;
try { root = typeof rootAgents === 'function' ? await rootAgents() : undefined; } catch { root = undefined; }
const answers = await Promise.all(riding.map(async r => {
let routes = null, named = null;
try { routes = await r.call('routes'); } catch { routes = null; }
try { named = typeof r.file === 'function' ? modelsNamed((await r.file())?.document) : null; } catch { named = null; }
const fromRoot = root === undefined ? null : rootModelsNamed(root, r.id);
return { id: r.id, routes, named: named && fromRoot ? [...named, ...fromRoot] : null };
}));
const providers = new Map(), usedBy = new Map();
let ready = false;
for (const { id, routes } of answers) {
const c = routes?.catalogue;
if (c?.status !== 'ready' || !Array.isArray(c.models)) { unanswered.add(id); continue; }
ready = true;
for (const m of c.models) {
if (m?.module !== 'cliproxy' || typeof m.upstream !== 'string' || typeof m.model !== 'string' || !MODEL_ID.test(m.model)) continue;
const provider = offeredProvider(m.upstream);
if (!providers.has(provider)) providers.set(provider, []);
if (!providers.get(provider).includes(m.model)) providers.get(provider).push(m.model);
}
}
if (!ready) throw failure('proxy-down');
for (const { id, routes, named } of answers) {
if (!routes || typeof routes.instances !== 'object' || routes.instances === null || named === null) { unanswered.add(id); continue; }
const table = routes.providerModels && typeof routes.providerModels === 'object' ? routes.providerModels : {};
const legacy = routes.catalogue?.legacy && typeof routes.catalogue.legacy === 'object' ? routes.catalogue.legacy : {};
const resolve = key => (typeof key === 'string' && (Object.hasOwn(table, key) ? table[key] : Object.hasOwn(legacy, key) ? legacy[key] : null)) || null;
const uses = [];
for (const [instance, route] of Object.entries(routes.instances)) {
if (!ORG_ID.test(instance)) continue;
uses.push({ instance, key: route?.model, fallback: false }, { instance, key: route?.fallback?.model, fallback: true });
}
const once = new Set();
for (const { instance, key, fallback } of [...uses, ...named]) {
const entry = resolve(key);
if (entry?.module !== 'cliproxy' || typeof entry.upstream !== 'string' || typeof entry.model !== 'string') continue;
const at = `${offeredProvider(entry.upstream)}\n${entry.model}`;
if (once.has(`${at}\n${instance}\n${fallback}`)) continue;
once.add(`${at}\n${instance}\n${fallback}`);
if (!usedBy.has(at)) usedBy.set(at, []);
usedBy.get(at).push({ org: id, instance, fallback });
}
}
return { providers, usedBy, unanswered: [...unanswered].sort() };
}
const entryOf = (file, p) => (file.offered && Object.hasOwn(file.offered.providers, p) ? file.offered.providers[p] : undefined);
const usersOf = (s, provider, id) => s.usedBy.get(`${provider}\n${id}`) ?? [];
// The models of `provider` something uses, present or not (an account may be signed out).
const usedOf = (s, provider) => [...s.usedBy.keys()].filter(at => at.startsWith(`${provider}\n`)).map(at => at.slice(provider.length + 1)).filter(id => MODEL_ID.test(id));
// A provider with no entry yet has every model on (catalogue() reads it so): the present ones and
// any one in use whose account is signed out now.
const onFor = (entry, present, s, provider) => entry ? entry.on : [...new Set([...present, ...usedOf(s, provider)])];
const view = (file, s) => ({
revision: file.revision,
unanswered: s.unanswered,
providers: [...s.providers].map(([provider, present]) => {
const entry = entryOf(file, provider);
const on = new Set(onFor(entry, present, s, provider).filter(id => present.includes(id)));
const seen = new Set(entry ? entry.seen : present);
return { provider, label: offeredLabel(provider), on: on.size, total: present.length,
models: present.map(id => ({ id, label: id, on: on.has(id), isNew: !seen.has(id), usedBy: usersOf(s, provider, id) })) };
}),
});
return {
// GET: the lists. A provider seen for the first time is written as all its present models on
// and seen, with any model in use whose account is signed out (the plugin has no rule for
// "current"), so a model it adds later starts off as New. The seed must change nothing anyone
// uses: a provider with no entry is all on already. So it never seeds a provider with more than
// MAX_ON such models, nor anything while an organization's usage is unknown (the owner's first
// save does it then, with the in-use check), and it seeds only when `seed` is true, which the
// Console sets only for a request that passes its write checks. The seed waits when another
// admin action holds the lock; the view reads the same either way.
async models({ seed = false } = {}) {
let file = readFile();
const s = await survey();
const fresh = seed === true && !s.unanswered.length ? [...s.providers.keys()].filter(p => !entryOf(file, p) && onFor(undefined, s.providers.get(p), s, p).length <= MAX_ON) : [];
if (fresh.length) {
try {
await exclusive('models', async () => {
const now = readFile();
if (now.revision !== file.revision) return;
const providers = { ...(now.offered?.providers ?? {}) };
for (const p of fresh) { const all = onFor(undefined, s.providers.get(p), s, p); providers[p] = { on: [...all], seen: [...all] }; }
writeOffered(dataRoot, { version: OFFERED_VERSION, providers });
receipt({ op: 'models.seed', providers: fresh.map(p => ({ provider: p, on: providers[p].on.length })) });
});
} catch (error) { if (error?.code !== 'proxy-admin-busy') throw failure('outcome-unknown'); }
file = readFile();
}
return view(file, s);
},
// POST: one provider's on list for its present models. `revision` is the one the read gave; a
// model in use that this switches off refuses unless `confirmInUse` is true, naming who uses
// it. When it switches any model off and an organization's usage is unknown, it refuses the same
// way and names that organization. A model that is on but not present now (its account signed
// out) is not on the page, so it keeps its state: only a present model, or one named in `on`,
// changes.
async modelsSet({ provider, on, revision, confirmInUse = false, user = null } = {}) {
if (typeof provider !== 'string' || !PROVIDER_ID.test(provider) || typeof revision !== 'string' || !REVISION.test(revision) || typeof confirmInUse !== 'boolean') throw failure('invalid-request');
if (!idList(on, MAX_ON)) throw failure('models-invalid');
return exclusive('models', async () => {
const file = readFile();
if (file.revision !== revision) throw failure('models-offered-conflict');
const s = await survey();
const present = s.providers.get(provider);
if (!present) throw failure('models-invalid');
const entry = entryOf(file, provider);
const before = onFor(entry, present, s, provider);
// A model may stay on while its account is signed out; a model never listed may not be switched on.
if (on.some(id => !present.includes(id) && !before.includes(id))) throw failure('models-invalid');
const kept = before.filter(id => !present.includes(id) && !on.includes(id));
if (on.length + kept.length > MAX_ON) throw failure('models-invalid');
const off = before.filter(id => present.includes(id) && !on.includes(id));
const users = off.flatMap(id => usersOf(s, provider, id).map(u => ({ model: id, ...u })));
const unanswered = off.length ? s.unanswered : [];
if ((users.length || unanswered.length) && confirmInUse !== true) throw failure('models-in-use', { usedBy: users, unanswered });
const seen = [...new Set([...(entry?.seen ?? []), ...present, ...on, ...kept])];
const providers = { ...(file.offered?.providers ?? {}), [provider]: { on: [...on, ...kept], seen } };
if (seen.length > MAX_SEEN) throw failure('models-invalid');
const next = writeOffered(dataRoot, { version: OFFERED_VERSION, providers });
// Every list is bounded, with a count of the rest, so the receipt line stays well inside what
// receiptHistory reads.
const record = { op: 'models.set', provider };
const list = (name, items) => { record[name] = items.slice(0, RECEIPT_LIST); if (items.length > RECEIPT_LIST) record[`${name}More`] = items.length - RECEIPT_LIST; };
list('on', on.filter(id => !before.includes(id)));
list('off', off);
if (users.length) list('inUse', users.map(u => ({ model: u.model, org: u.org, instance: u.instance })));
if (unanswered.length) list('unanswered', unanswered);
if (typeof user === 'string' && user) record.user = user.slice(0, 64);
let written = null;
try { written = receipt(record); } catch { written = null; }
return { provider, revision: next, on: on.length, total: present.length, receipt: written };
});
},
};
}
@@ -0,0 +1,74 @@
// #1873 account-remove: what removing one account takes away. Pure over the bus's accounts read
// and each organization's routes read (launch-settings routes()). Mutants: a model another account
// still serves reported as leaving; a guess where the reads cannot decide; an unreadable
// organization reported as using nothing.
import test from 'node:test';
import assert from 'node:assert/strict';
import { accountMoves, routeUses, accountImpact } from '../src/account-impact.mjs';
const A = 'aaaaaaaaaaaa', B = 'bbbbbbbbbbbb', C = 'cccccccccccc', D = 'dddddddddddd';
const claude = (accountId, status = 'ready') => ({ accountId, provider: 'claude', displayName: `Claude account ${accountId}`, status, priority: 0 });
const zai = (accountId, models, status = 'active') => ({ accountId, provider: 'z.ai', displayName: `z.ai account ${accountId}`, status, priority: 0, models });
const view = (instances, providerModels) => ({ business: 'acme', revision: 1, harnesses: ['pi'], models: Object.keys(providerModels), providerModels, levels: {}, instances });
const route = (model, fallback = null) => ({ harness: 'pi', model, thinking: null, fallback: fallback && { harness: 'pi', model: fallback, thinking: null } });
test('the only usable account of a provider takes every model of it', () => {
assert.deepEqual(accountMoves([claude(A), claude(B, 'error')], A), { accountId: A, provider: 'claude', upstream: 'claude', moves: 'all', models: null, others: 0 });
});
test('another usable OAuth account of the provider leaves the answer unknown, never a guess', () => {
assert.deepEqual(accountMoves([claude(A), claude(B, 'active')], A), { accountId: A, provider: 'claude', upstream: 'claude', moves: 'unknown', models: null, others: 1 });
});
test('an account that serves nothing now moves nothing', () => {
for (const status of ['disabled', 'unavailable', 'error', 'unknown'])
assert.deepEqual(accountMoves([claude(A, status)], A), { accountId: A, provider: 'claude', upstream: 'claude', moves: 'none', models: [], others: 0 }, status);
});
test('key accounts name their models, so the difference is exact', () => {
const accounts = [zai(A, ['glm-a', 'glm-b']), zai(B, ['glm-b']), zai(C, ['glm-c'], 'disabled')];
assert.deepEqual(accountMoves(accounts, A), { accountId: A, provider: 'z.ai', upstream: 'zai', moves: 'some', models: ['glm-a'], others: 1 });
assert.deepEqual(accountMoves([zai(A, ['glm-b']), zai(B, ['glm-b'])], A).moves, 'none');
// Alone, a key account takes exactly its own list.
assert.deepEqual(accountMoves([zai(A, ['glm-a', 'glm-a'])], A), { accountId: A, provider: 'z.ai', upstream: 'zai', moves: 'some', models: ['glm-a'], others: 0 });
assert.equal(accountMoves([zai(A, [])], A).moves, 'none');
});
test('a provider the plugin cannot place, or an account not listed, is unknown or null', () => {
assert.equal(accountMoves([{ accountId: A, provider: 'other', status: 'active' }], A).moves, 'unknown');
const providers = [{ id: 'z.ai', label: 'z.ai', signIn: 'apikey', baseUrl: 'https://example.invalid/anthropic' }];
assert.equal(accountMoves([zai(A, ['glm-a'])], A, providers).moves, 'unknown');
assert.equal(accountMoves([claude(A)], B), null);
assert.equal(accountMoves([claude(A)], 'not-an-id'), null);
assert.equal(accountMoves(null, A), null);
});
test('routeUses names each instance whose route or fallback uses a model that leaves', () => {
const v = view({ coder: route('k-claude', 'k-glm-b'), tester: route('k-glm-a'), chief: route('k-local'), odd: route('k-missing') },
{ 'k-claude': { module: 'cliproxy', upstream: 'claude', model: 'claude-fixture' }, 'k-glm-a': { module: 'cliproxy', upstream: 'zai', model: 'glm-a' },
'k-glm-b': { module: 'cliproxy', upstream: 'zai', model: 'glm-b' }, 'k-local': { module: 'other-module', upstream: 'zai', model: 'glm-a' } });
assert.deepEqual(routeUses(v, { upstream: 'claude', moves: 'all', models: null }), [{ instance: 'coder', model: 'claude / claude-fixture', fallback: false }]);
assert.deepEqual(routeUses(v, { upstream: 'zai', moves: 'some', models: ['glm-b'] }), [{ instance: 'coder', model: 'zai / glm-b', fallback: true }]);
assert.deepEqual(routeUses(v, { upstream: 'zai', moves: 'all', models: null }), [{ instance: 'coder', model: 'zai / glm-b', fallback: true }, { instance: 'tester', model: 'zai / glm-a', fallback: false }]);
assert.deepEqual(routeUses(v, { upstream: 'zai', moves: 'none', models: [] }), []);
assert.throws(() => routeUses({}, { upstream: 'zai', moves: 'all', models: null }));
});
test('accountImpact asks only cliproxy organizations; an unreadable one is named, not counted as clean', async () => {
const asked = [];
const rider = (id, answer, cliproxy = true) => ({ id, cliproxy, call: async verb => { asked.push([id, verb]); if (answer instanceof Error) throw answer; return answer; } });
const notConfigured = Object.assign(new Error('x'), { code: 'launch-not-configured' });
const down = Object.assign(new Error('x'), { code: 'org-bus-down' });
const v = view({ coder: route('k') }, { k: { module: 'cliproxy', upstream: 'claude', model: 'claude-fixture' } });
const result = await accountImpact({ accounts: [claude(A)], accountId: A,
riders: [rider('acme', v), rider('beta', notConfigured), rider('gamma', down), rider('delta', v, false), { id: '../x', cliproxy: true, call: async () => v }] });
assert.deepEqual(result, { accountId: A, provider: 'claude', upstream: 'claude', moves: 'all', models: null, others: 0, organizations: [
{ id: 'acme', readable: true, uses: [{ instance: 'coder', model: 'claude / claude-fixture', fallback: false }] },
{ id: 'beta', readable: true, uses: [] },
{ id: 'gamma', readable: false },
] });
assert.deepEqual(asked, [['acme', 'routes'], ['beta', 'routes'], ['gamma', 'routes']]);
asked.length = 0;
// Nothing moves, or the reads cannot say: no organization is asked.
assert.deepEqual((await accountImpact({ accounts: [claude(A), claude(B)], accountId: A, riders: [rider('acme', v)] })).organizations, []);
assert.deepEqual((await accountImpact({ accounts: [claude(D, 'disabled')], accountId: D, riders: [rider('acme', v)] })).organizations, []);
assert.deepEqual(asked, []);
assert.equal(await accountImpact({ accounts: [claude(A)], accountId: B, riders: [] }), null);
// No riders list at all is not the same as no organization.
assert.equal((await accountImpact({ accounts: [claude(A)], accountId: A, riders: null })).organizations, null);
assert.deepEqual((await accountImpact({ accounts: [claude(A)], accountId: A, riders: [] })).organizations, []);
});
@@ -567,3 +567,64 @@ test('A2 item 8: access gives each organization a health and key ids, never a ke
// 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');
});
// #1873 account-remove (Jason 2026-11-20): the controller sends proxy.account.remove with
// confirm:true and writes one receipt after the bus answers, labels only.
test('accountRemove sends the confirmed verb and its receipt holds the op, provider label and account id only', async t => {
const { admin, dataRoot } = fixture(t);
const sent = [];
const call = async (verb, args) => { sent.push([verb, args]); return { accountId: 'a1b2c3d4e5f6', provider: 'z.ai', removed: true, key: 'zai-secret-key', name: 'cred.json' }; };
const result = await admin.accountRemove(call, { accountId: 'a1b2c3d4e5f6', user: 'owner' });
assert.deepEqual(sent, [['proxy.account.remove', { accountId: 'a1b2c3d4e5f6', confirm: true }]]);
const at = new Date(0).toISOString();
assert.deepEqual(result, { accountId: 'a1b2c3d4e5f6', provider: 'z.ai', removed: true, receipt: { time: at, op: 'account.remove', provider: 'z.ai', accountId: 'a1b2c3d4e5f6', user: 'owner' } });
assert.deepEqual(receipts(dataRoot), [{ time: at, op: 'account.remove', provider: 'z.ai', accountId: 'a1b2c3d4e5f6', user: 'owner' }]);
for (const value of ['zai-secret-key', 'cred.json']) assert.equal(fs.readFileSync(receiptFile(dataRoot), 'utf8').includes(value), false);
// A provider the label check refuses is written as null.
await admin.accountRemove(async () => ({ accountId: 'a1b2c3d4e5f6', removed: true, provider: 'Bad Label/../x' }), { accountId: 'a1b2c3d4e5f6' });
assert.equal(receipts(dataRoot)[1].provider, null);
assert.equal(Object.hasOwn(receipts(dataRoot)[1], 'user'), false);
});
test('accountRemove passes a bus refusal through and writes no receipt; a bad id or no bus refuses before the call', async t => {
const { admin, dataRoot } = fixture(t);
for (const name of ['remove-write-refused', 'remove-unconfirmed', 'proxy-uncertain', 'human-required'])
await assert.rejects(admin.accountRemove(async () => { throw Object.assign(new Error(name), { code: name }); }, { accountId: 'a1b2c3d4e5f6' }), code(name));
let called = 0;
const call = async () => { called++; return {}; };
for (const accountId of [undefined, 'A1B2C3D4E5F6', 'a1b2c3d4e5f', '../x'])
await assert.rejects(admin.accountRemove(call, { accountId }), code('invalid-request'));
await assert.rejects(admin.accountRemove(null, { accountId: 'a1b2c3d4e5f6' }), code('not-configured'));
assert.equal(called, 0);
assert.deepEqual(receipts(dataRoot), []);
});
test('accountRemove writes no receipt unless the bus answer says this account was removed', async t => {
const { admin, dataRoot } = fixture(t);
for (const answer of [null, {}, { accountId: 'a1b2c3d4e5f6', removed: 'true' }, { accountId: 'a1b2c3d4e5f6' }, { accountId: 'b1b2c3d4e5f6', removed: true }, { removed: true }])
await assert.rejects(admin.accountRemove(async () => answer, { accountId: 'a1b2c3d4e5f6' }), code('remove-unconfirmed'), JSON.stringify(answer));
assert.deepEqual(receipts(dataRoot), []);
});
test('accountRemove runs one at a time', async t => {
const { admin } = fixture(t);
let release; const gate = new Promise(r => { release = r; });
const first = admin.accountRemove(async () => { await gate; return { accountId: 'a1b2c3d4e5f6', removed: true, provider: 'claude' }; }, { accountId: 'a1b2c3d4e5f6' });
await new Promise(r => setImmediate(r));
await assert.rejects(admin.accountRemove(async () => ({}), { accountId: 'a1b2c3d4e5f6' }), code('proxy-admin-busy'));
release();
assert.equal((await first).removed, true);
});
test('accountImpact reads the accounts through the bus and each cliproxy organization\'s routes', async t => {
const v = { business: 'acme', revision: 1, harnesses: ['pi'], models: ['k'], providerModels: { k: { module: 'cliproxy', upstream: 'claude', model: 'claude-fixture' } }, levels: {},
instances: { coder: { harness: 'pi', model: 'k', thinking: null, fallback: null } } };
const { admin } = fixture(t, { extra: { riders: async () => [{ id: 'acme', cliproxy: true, call: async verb => { assert.equal(verb, 'routes'); return v; } }] } });
const sent = [];
const call = async (verb, args) => { sent.push([verb, args]); return { accounts: [{ accountId: 'a1b2c3d4e5f6', provider: 'claude', displayName: 'x', status: 'ready', priority: 0 }] }; };
assert.deepEqual(await admin.accountImpact(call, { accountId: 'a1b2c3d4e5f6' }), { accountId: 'a1b2c3d4e5f6', provider: 'claude', upstream: 'claude', moves: 'all', models: null, others: 0,
organizations: [{ id: 'acme', readable: true, uses: [{ instance: 'coder', model: 'claude / claude-fixture', fallback: false }] }] });
assert.deepEqual(sent, [['proxy.accounts', {}]]);
await assert.rejects(admin.accountImpact(call, { accountId: 'ffffffffffff' }), code('invalid-request'));
await assert.rejects(admin.accountImpact(call, { accountId: 'nope' }), code('invalid-request'));
await assert.rejects(admin.accountImpact(null, { accountId: 'a1b2c3d4e5f6' }), code('not-configured'));
// A riders list that cannot be read is no answer about organizations, not none.
const { admin: lost } = fixture(t, { extra: { riders: async () => { throw new Error('gone'); } } });
assert.equal((await lost.accountImpact(call, { accountId: 'a1b2c3d4e5f6' })).organizations, null);
});
@@ -0,0 +1,264 @@
// #1873 models-offered (A9): the owner's per-provider on/off file and the Console's read and write.
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, rmSync, writeFileSync, readFileSync, statSync, symlinkSync, linkSync, mkdirSync, chmodSync, existsSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { readOffered, writeOffered, offeredControl, offeredFile, offeredRevision, MAX_ON } from '../src/models-offered.mjs';
const tempRoot = t => { const root = mkdtempSync(join(tmpdir(), 'mo-')); t.after(() => rmSync(root, { recursive: true, force: true })); return root; };
const entry = (upstream, model) => ({ module: 'cliproxy', upstream, model });
const rider = (id, models, extra = {}, document = { roles: {} }) => ({ id, cliproxy: true, file: async () => ({ document, revision: 'r' }), call: async verb => { if (verb !== 'routes') throw new Error('no'); return { instances: {}, catalogue: { status: 'ready', models }, ...extra }; } });
const NO_ROOT = async () => ({ state: 'absent', agents: {}, assistant: null });
function control(t, { riders, busy = false, rootAgents = NO_ROOT } = {}) {
const dataRoot = tempRoot(t), receipts = [];
const exclusive = async (name, action) => { if (busy) throw Object.assign(new Error('busy'), { code: 'proxy-admin-busy' }); return action(); };
return { dataRoot, receipts, ...offeredControl({ dataRoot, riders: async () => riders, receipt: r => { receipts.push(r); return r; }, exclusive, rootAgents }) };
}
test('readOffered: no file is absent; a written file reads ok with its revision; anything else reads damaged', t => {
const root = tempRoot(t);
assert.deepEqual(readOffered(root), { state: 'absent', offered: null, revision: offeredRevision(null) });
const doc = { version: 1, providers: { 'z.ai': { on: ['glm-5'], seen: ['glm-5', 'glm-4.6'] } } };
const revision = writeOffered(root, doc);
assert.deepEqual(readOffered(root), { state: 'ok', offered: doc, revision });
assert.equal(statSync(offeredFile(root)).mode & 0o777, 0o600);
assert.equal(revision, offeredRevision(readFileSync(offeredFile(root), 'utf8')));
chmodSync(offeredFile(root), 0o640);
assert.equal(readOffered(root).state, 'damaged', 'group bits');
chmodSync(offeredFile(root), 0o600);
assert.equal(readOffered(root, { uid: process.getuid() + 1 }).state, 'damaged', 'another owner');
const other = join(root, 'other.json');
writeFileSync(other, JSON.stringify(doc), { mode: 0o600 });
rmSync(offeredFile(root)); symlinkSync(other, offeredFile(root));
assert.equal(readOffered(root).state, 'damaged', 'a symlink');
rmSync(offeredFile(root)); linkSync(other, offeredFile(root));
assert.equal(readOffered(root).state, 'damaged', 'a second hard link');
rmSync(offeredFile(root)); writeFileSync(offeredFile(root), '{"version":1,"providers":{"z.ai":{"on":["glm-5"],"seen":[]}}}', { mode: 0o600 });
assert.equal(readOffered(root).state, 'damaged', 'on but never seen');
rmSync(offeredFile(root)); writeFileSync(offeredFile(root), '{"version":1,"providers":{"z.ai":{"on":[],"seen":[],"harness":"pi"}}}', { mode: 0o600 });
assert.equal(readOffered(root).state, 'damaged', 'an unknown key in an entry');
assert.throws(() => writeOffered(root, { ...doc, harness: 'pi' }), { code: 'models-invalid' });
});
test('the first read asked to seed writes every present model on and seen, once; a read not asked to, or a busy lock, leaves the file and still answers', async t => {
const models = [entry('zai', 'glm-5'), entry('zai', 'glm-4.6'), entry('codex', 'gpt-5'), entry('zai', 'glm-5'), { module: 'other', upstream: 'zai', model: 'x' }];
const quiet = control(t, { riders: [rider('acme', models)] });
for (const args of [undefined, {}, { seed: 'yes' }, { seed: 1 }]) assert.deepEqual((await quiet.models(args)).providers.map(p => [p.provider, p.on, p.total]), [['z.ai', 2, 2], ['codex', 1, 1]]);
assert.equal(existsSync(offeredFile(quiet.dataRoot)), false, 'no seed unless asked');
assert.deepEqual(quiet.receipts, []);
const busy = control(t, { riders: [rider('acme', models)], busy: true });
const view = await busy.models({ seed: true });
assert.deepEqual(view.providers.map(p => [p.provider, p.label, p.on, p.total]), [['z.ai', 'z.ai', 2, 2], ['codex', 'ChatGPT / Codex', 1, 1]]);
assert.equal(existsSync(offeredFile(busy.dataRoot)), false);
assert.deepEqual(busy.receipts, []);
const c = control(t, { riders: [rider('acme', models)] });
const first = await c.models({ seed: true });
assert.deepEqual(readOffered(c.dataRoot).offered, { version: 1, providers: { 'z.ai': { on: ['glm-5', 'glm-4.6'], seen: ['glm-5', 'glm-4.6'] }, codex: { on: ['gpt-5'], seen: ['gpt-5'] } } });
assert.deepEqual(c.receipts, [{ op: 'models.seed', providers: [{ provider: 'z.ai', on: 2 }, { provider: 'codex', on: 1 }] }]);
await c.models({ seed: true });
assert.equal(c.receipts.length, 1);
assert.equal(first.revision, readOffered(c.dataRoot).revision);
});
test(`a provider with more than ${MAX_ON} models is never seeded; it reads all on, and the first save checks every model it switches off`, async t => {
const many = Array.from({ length: MAX_ON + 4 }, (_, i) => entry('zai', `glm-${i}`));
const last = `glm-${MAX_ON + 3}`;
const routes = { instances: { chief: { model: 'mine' } }, providerModels: { mine: entry('zai', last) } };
const c = control(t, { riders: [rider('acme', many, routes), rider('bolt', [entry('codex', 'gpt-5')])] });
const view = await c.models({ seed: true });
assert.deepEqual([view.providers[0].on, view.providers[0].total], [MAX_ON + 4, MAX_ON + 4]);
assert.equal(view.providers[0].models.at(-1).on, true, 'the model in use reads on, as the catalogue offers it');
assert.deepEqual(Object.keys(readOffered(c.dataRoot).offered.providers), ['codex'], 'only the small provider is seeded');
const first = many.slice(0, MAX_ON).map(m => m.model);
await assert.rejects(c.modelsSet({ provider: 'z.ai', on: first, revision: view.revision }), error => {
assert.equal(error.code, 'models-in-use');
assert.deepEqual(error.usedBy, [{ model: last, org: 'acme', instance: 'chief', fallback: false }]);
return true;
});
const saved = await c.modelsSet({ provider: 'z.ai', on: first, revision: view.revision, confirmInUse: true });
assert.equal(saved.on, MAX_ON);
const record = c.receipts.at(-1);
assert.deepEqual(record.off, many.slice(MAX_ON).map(m => m.model), 'the receipt names every model switched off');
assert.equal(readOffered(c.dataRoot).offered.providers['z.ai'].seen.length, MAX_ON + 4);
});
test('an organization whose usage is unknown (its file unreadable, its bus down, its catalogue not ready) counts as a possible user of any model switched off', async t => {
const models = [entry('zai', 'glm-5'), entry('zai', 'glm-4.6')];
const down = { id: 'bolt', cliproxy: true, call: async () => { throw new Error('down'); } };
const unreadable = { id: 'crux', cliproxy: null, call: async () => { throw new Error('not asked'); } };
const notReady = rider('dune', [], { instances: { coder: { model: 'old-glm' } }, catalogue: { status: 'unavailable', models: [], legacy: {} } });
const notRiding = { id: 'east', cliproxy: false, call: async () => { throw new Error('not asked'); } };
const noFile = { ...rider('fern', models), file: async () => { throw new Error('unreadable'); } };
const noFileReader = { ...rider('gale', models), file: undefined };
const c = control(t, { riders: [rider('acme', models), down, unreadable, notReady, notRiding, noFile, noFileReader] });
const view = await c.models({ seed: true });
assert.deepEqual(view.unanswered, ['bolt', 'crux', 'dune', 'fern', 'gale']);
assert.equal(existsSync(offeredFile(c.dataRoot)), false, 'no seed while any usage is unknown');
await assert.rejects(c.modelsSet({ provider: 'z.ai', on: ['glm-5'], revision: view.revision }), error => {
assert.equal(error.code, 'models-in-use');
assert.deepEqual([error.usedBy, error.unanswered], [[], ['bolt', 'crux', 'dune', 'fern', 'gale']]);
return true;
});
assert.equal(readOffered(c.dataRoot).revision, view.revision, 'nothing written');
// Switching nothing off needs no ask.
const same = await c.modelsSet({ provider: 'z.ai', on: ['glm-5', 'glm-4.6'], revision: view.revision });
const saved = await c.modelsSet({ provider: 'z.ai', on: ['glm-5'], revision: same.revision, confirmInUse: true });
assert.equal(saved.on, 1);
assert.deepEqual(c.receipts.at(-1), { op: 'models.set', provider: 'z.ai', on: [], off: ['glm-4.6'], unanswered: ['bolt', 'crux', 'dune', 'fern', 'gale'] });
// The root agent file unreadable (a throw, or not the loader's shape) leaves every organization's usage unknown.
for (const rootAgents of [async () => { throw new Error('unreadable'); }, async () => ({ state: 'ok', agents: { a: { vars: {} } } }), async () => null, null]) {
const r = control(t, { riders: [rider('acme', models)], rootAgents });
assert.deepEqual((await r.models({ seed: true })).unanswered, ['acme'], String(rootAgents));
assert.equal(existsSync(offeredFile(r.dataRoot)), false);
}
});
test('in use counts every place the organization file names a model and the root agents it takes: project agents, root copies; not an excluded or assistant root agent, not launch.models itself', async t => {
const models = [entry('zai', 'glm-5'), entry('zai', 'glm-4.6'), entry('codex', 'gpt-5'), entry('codex', 'gpt-5-mini')];
const routes = { instances: { chief: { model: 'g5' } }, providerModels: { g5: entry('zai', 'glm-5'), g46: entry('zai', 'glm-4.6'), gpt: entry('codex', 'gpt-5'), mini: entry('codex', 'gpt-5-mini') } };
const document = { roles: { chief: { vars: { model: 'g5' } } }, launch: { models: { g5: { module: 'cliproxy', upstream: 'zai', model: 'g46' } } },
projects: { site: { agents: { coder: { define: { definition: 'coder', vars: { model: 'g46', 'fallback.model': 'gpt' } } } } } } };
const root = { state: 'ok', assistant: null, agents: {
helper: { definition: 'coder', excludeOrgs: [], vars: { model: 'mini' } },
away: { definition: 'coder', excludeOrgs: ['acme'], vars: { model: 'gpt' } },
front: { definition: 'assistant', excludeOrgs: [], vars: { model: 'gpt' } } } };
const c = control(t, { riders: [rider('acme', models, routes, document)], rootAgents: async () => root });
const view = await c.models({ seed: true });
assert.deepEqual(view.providers.map(p => p.models.map(m => [m.id, m.usedBy])), [
[['glm-5', [{ org: 'acme', instance: 'chief', fallback: false }]], ['glm-4.6', [{ org: 'acme', instance: 'coder', fallback: false }]]],
[['gpt-5', [{ org: 'acme', instance: 'coder', fallback: true }]], ['gpt-5-mini', [{ org: 'acme', instance: 'helper', fallback: false }]]]]);
await assert.rejects(c.modelsSet({ provider: 'codex', on: [], revision: view.revision }), error => {
assert.deepEqual(error.usedBy.map(u => [u.model, u.instance, u.fallback]), [['gpt-5', 'coder', true], ['gpt-5-mini', 'helper', false]]);
return true;
});
});
test('a model in use whose account is signed out: the seed keeps it on, the first save with no entry keeps it on, and a save that leaves it off the page keeps it on', async t => {
let models = [entry('codex', 'gpt-5')];
const routes = { instances: { chief: { model: 'pro' } }, providerModels: { pro: entry('codex', 'gpt-5-pro') } };
const acme = { ...rider('acme', []), call: async () => ({ ...routes, catalogue: { status: 'ready', models } }) };
const c = control(t, { riders: [acme] });
const view = await c.models({ seed: true });
assert.deepEqual(readOffered(c.dataRoot).offered.providers.codex, { on: ['gpt-5', 'gpt-5-pro'], seen: ['gpt-5', 'gpt-5-pro'] }, 'the seed keeps the model in use on');
const saved = await c.modelsSet({ provider: 'codex', on: [], revision: view.revision });
assert.deepEqual(readOffered(c.dataRoot).offered.providers.codex.on, ['gpt-5-pro'], 'a model not on the page keeps its state');
assert.deepEqual(c.receipts.at(-1), { op: 'models.set', provider: 'codex', on: [], off: ['gpt-5'] });
models = [entry('codex', 'gpt-5'), entry('codex', 'gpt-5-pro')];
await assert.rejects(c.modelsSet({ provider: 'codex', on: [], revision: saved.revision }), { code: 'models-in-use' }, 'back again, it is on the page and asks');
// With no entry at all, the first save keeps an absent model in use on too.
models = [entry('codex', 'gpt-5')];
const fresh = control(t, { riders: [acme] });
const read = await fresh.models();
assert.equal(existsSync(offeredFile(fresh.dataRoot)), false);
await fresh.modelsSet({ provider: 'codex', on: ['gpt-5'], revision: read.revision });
assert.deepEqual(readOffered(fresh.dataRoot).offered.providers.codex, { on: ['gpt-5', 'gpt-5-pro'], seen: ['gpt-5', 'gpt-5-pro'] });
});
test(`a model kept on while its account is signed out counts against the ${MAX_ON} limit; a save past it is models-invalid before any ask, and writes nothing`, async t => {
const present = Array.from({ length: MAX_ON - 3 }, (_, i) => entry('zai', `glm-${i}`));
const away = Array.from({ length: 10 }, (_, i) => `gone-${i}`);
const routes = { instances: { chief: { model: 'first' } }, providerModels: { first: entry('zai', 'glm-0') } };
const c = control(t, { riders: [rider('acme', present, routes)] });
const all = present.map(m => m.model);
writeOffered(c.dataRoot, { version: 1, providers: { 'z.ai': { on: [...away, ...all.slice(0, MAX_ON - 10)], seen: [...away, ...all] } } });
const before = readOffered(c.dataRoot);
// glm-0 is in use and this switches it off, but the list is too long first: no ask for a save that cannot be written.
await assert.rejects(c.modelsSet({ provider: 'z.ai', on: all.slice(1), revision: before.revision }), { code: 'models-invalid' }, `${all.length - 1} on plus 10 kept is past ${MAX_ON}`);
await assert.rejects(c.modelsSet({ provider: 'z.ai', on: all.slice(1), revision: before.revision, confirmInUse: true }), { code: 'models-invalid' });
assert.equal(readOffered(c.dataRoot).revision, before.revision);
assert.deepEqual(c.receipts, []);
const saved = await c.modelsSet({ provider: 'z.ai', on: all.slice(7), revision: before.revision, confirmInUse: true });
assert.equal(saved.on, MAX_ON - 10);
const after = readOffered(c.dataRoot);
assert.equal(after.state, 'ok', 'the file it writes still reads');
assert.equal(after.offered.providers['z.ai'].on.length, MAX_ON);
assert.deepEqual(after.offered.providers['z.ai'].on.slice(-10), away);
});
test('a save keeps every model seen before, so one away at the save and back later is not New', async t => {
let models = [entry('codex', 'gpt-5')];
const acme = { ...rider('acme', []), call: async () => ({ instances: {}, catalogue: { status: 'ready', models } }) };
const c = control(t, { riders: [acme] });
writeOffered(c.dataRoot, { version: 1, providers: { codex: { on: ['gpt-5'], seen: ['gpt-5', 'gpt-old'] } } });
await c.modelsSet({ provider: 'codex', on: ['gpt-5'], revision: readOffered(c.dataRoot).revision });
assert.deepEqual(readOffered(c.dataRoot).offered.providers.codex.seen, ['gpt-5', 'gpt-old']);
models = [entry('codex', 'gpt-5'), entry('codex', 'gpt-old')];
const view = await c.models();
const old = view.providers.find(p => p.provider === 'codex').models.find(m => m.id === 'gpt-old');
assert.deepEqual([old.on, old.isNew], [false, false]);
});
test('the receipt bounds every list at 32 with a count of the rest', async t => {
const many = Array.from({ length: 40 }, (_, i) => entry('zai', `glm-${i}`));
const instances = Object.fromEntries(many.map((m, i) => [`agent-${i}`, { model: `k${i}` }]));
const providerModels = Object.fromEntries(many.map((m, i) => [`k${i}`, m]));
const down = { id: 'zz-down', cliproxy: null, call: async () => { throw new Error('down'); } };
const quiet = Array.from({ length: 34 }, (_, i) => ({ ...down, id: `q-${String(i).padStart(2, '0')}` }));
const c = control(t, { riders: [rider('acme', many, { instances, providerModels }), ...quiet] });
const view = await c.models();
await c.modelsSet({ provider: 'z.ai', on: [], revision: view.revision, confirmInUse: true });
const r = c.receipts.at(-1);
assert.deepEqual([r.off.length, r.offMore, r.inUse.length, r.inUseMore, r.unanswered.length, r.unansweredMore], [32, 8, 32, 8, 32, 2]);
const back = await c.modelsSet({ provider: 'z.ai', on: many.map(m => m.model), revision: readOffered(c.dataRoot).revision });
assert.equal(back.on, 40);
assert.deepEqual([c.receipts.at(-1).on.length, c.receipts.at(-1).onMore], [32, 8]);
});
test('the seed rechecks the revision under the lock: a write between the read and the lock leaves the seed out', async t => {
const box = {};
const sneaky = { ...rider('acme', [entry('zai', 'glm-5')]), call: async () => { writeOffered(box.root, { version: 1, providers: { codex: { on: [], seen: [] } } }); return { instances: {}, catalogue: { status: 'ready', models: [entry('zai', 'glm-5')] } }; } };
const c = control(t, { riders: [sneaky] });
box.root = c.dataRoot;
await c.models({ seed: true });
assert.deepEqual(readOffered(c.dataRoot).offered.providers, { codex: { on: [], seen: [] } });
assert.deepEqual(c.receipts, []);
});
test('a damaged file: the read and the write answer outcome-unknown and write nothing', async t => {
const c = control(t, { riders: [rider('acme', [entry('zai', 'glm-5')])] });
mkdirSync(join(c.dataRoot, 'state'), { mode: 0o700 });
writeFileSync(offeredFile(c.dataRoot), 'not json', { mode: 0o600 });
await assert.rejects(c.models(), { code: 'outcome-unknown' });
await assert.rejects(c.modelsSet({ provider: 'z.ai', on: [], revision: 'a'.repeat(16) }), { code: 'outcome-unknown' });
assert.equal(readFileSync(offeredFile(c.dataRoot), 'utf8'), 'not json');
assert.deepEqual(c.receipts, []);
});
test('no organization riding is not-configured; none answering a ready catalogue is proxy-down; a busy lock refuses the write', async t => {
await assert.rejects(control(t, { riders: [] }).models(), { code: 'not-configured' });
await assert.rejects(control(t, { riders: [{ ...rider('acme', []), cliproxy: false }] }).models(), { code: 'not-configured' });
const down = { id: 'acme', cliproxy: true, call: async () => { throw new Error('down'); } };
await assert.rejects(control(t, { riders: [down, rider('bolt', [], { catalogue: { status: 'unavailable', models: [] } })] }).models(), { code: 'proxy-down' });
await assert.rejects(control(t, { riders: [rider('acme', [entry('zai', 'glm-5')])], busy: true }).modelsSet({ provider: 'z.ai', on: [], revision: 'a'.repeat(16) }), { code: 'proxy-admin-busy' });
});
test('the write checks its body first: a bad provider, revision or confirm is invalid-request; a bad or too long on list is models-invalid', async t => {
const c = control(t, { riders: [rider('acme', [entry('zai', 'glm-5')])] });
const r = 'a'.repeat(16);
for (const args of [{ provider: 'Z AI', on: [], revision: r }, { provider: 'z.ai', on: [], revision: 'short' }, { provider: 'z.ai', on: [], revision: r, confirmInUse: 'yes' }, {}])
await assert.rejects(c.modelsSet(args), { code: 'invalid-request' }, JSON.stringify(args));
for (const on of ['glm-5', ['glm-5', 'glm-5'], ['bad id'], Array.from({ length: MAX_ON + 1 }, (_, i) => `m-${i}`)])
await assert.rejects(c.modelsSet({ provider: 'z.ai', on, revision: r }), { code: 'models-invalid' }, JSON.stringify(on).slice(0, 40));
});
test('in use counts the organization model table and the bus legacy mapping, primary and fallback; a model still on stays on while its account is signed out', async t => {
const legacy = { instances: { coder: { model: 'old-glm' } }, catalogue: { status: 'ready', models: [entry('zai', 'glm-5'), entry('zai', 'glm-4.6')], legacy: { 'old-glm': entry('zai', 'glm-4.6') } } };
const table = { instances: { assistant: { model: 'mine', fallback: { model: 'mine' } }, '../x': { model: 'mine' } }, providerModels: { mine: entry('zai', 'glm-5') }, catalogue: { status: 'ready', models: [] } };
const c = control(t, { riders: [rider('acme', [], legacy), rider('bolt', [], table)] });
const view = await c.models({ seed: true });
assert.deepEqual(view.providers[0].models.map(m => [m.id, m.usedBy]), [
['glm-5', [{ org: 'bolt', instance: 'assistant', fallback: false }, { org: 'bolt', instance: 'assistant', fallback: true }]],
['glm-4.6', [{ org: 'acme', instance: 'coder', fallback: false }]]]);
await assert.rejects(c.modelsSet({ provider: 'z.ai', on: [], revision: view.revision }), error => {
assert.equal(error.code, 'models-in-use');
assert.deepEqual(error.usedBy.map(u => [u.model, u.org, u.instance, u.fallback]), [['glm-5', 'bolt', 'assistant', false], ['glm-5', 'bolt', 'assistant', true], ['glm-4.6', 'acme', 'coder', false]]);
return true;
});
// glm-4.6 leaves the catalogue (its account signed out): it may stay on, but one never listed may not be switched on.
legacy.catalogue.models = [entry('zai', 'glm-5')];
const saved = await c.modelsSet({ provider: 'z.ai', on: ['glm-5', 'glm-4.6'], revision: view.revision });
assert.equal(saved.on, 2);
assert.deepEqual(readOffered(c.dataRoot).offered.providers['z.ai'], { on: ['glm-5', 'glm-4.6'], seen: ['glm-5', 'glm-4.6'] });
await assert.rejects(c.modelsSet({ provider: 'z.ai', on: ['glm-5', 'glm-4.6', 'glm-9'], revision: saved.revision }), { code: 'models-invalid' });
});
@@ -54,3 +54,40 @@ test('credential model read encodes the name and keeps it out of receipts', asyn
assert.equal(JSON.stringify(s.receipts).includes('fixture name.json'), false);
for (const name of ['', 'x\n', 'x'.repeat(513)]) await assert.rejects(async () => s.client.getCredentialModels(name), { code: 'proxy-client-invalid' });
});
// #1873 account-remove: CLIProxyAPI 8.0.79's DeleteAuthFileV8, as its pinned panel calls it.
test('deleteCredential sends DELETE with only the name list to the loopback base; the name stays out of the receipt', async t => {
const s = await setup(t, () => ({ status: 200, body: { status: 'ok', deleted: 1, failed: [] } }));
assert.deepEqual(await s.client.deleteCredential('fixture-cred.json'), { deleted: true });
assert.deepEqual(s.calls.map(({ method, path, body, auth }) => [method, path, JSON.parse(body), auth]),
[['DELETE', '/v8/management/credentials', { names: ['fixture-cred.json'] }, 'Bearer p4-management']]);
assert.deepEqual(s.receipts, [{ call: 'DELETE /v8/management/credentials', status: 200, keyIds: [] }]);
assert.equal(JSON.stringify(s.receipts).includes('fixture-cred'), false);
});
test('deleteCredential reads a missing status as ok only when nothing failed', async t => {
const s = await setup(t, () => ({ status: 200, body: { deleted: 1 } }));
assert.deepEqual(await s.client.deleteCredential('fixture-cred.json'), { deleted: true });
});
for (const [label, answer] of [
['a partial status', { status: 200, body: { status: 'partial', deleted: 0, failed: [{ name: 'fixture-cred.json', error: 'p4-private' }] } }],
['a partial status with an empty failed list', { status: 200, body: { status: 'partial', deleted: 0, failed: [] } }],
['a failed entry with no status', { status: 200, body: { deleted: 0, failed: [{ name: 'fixture-cred.json' }] } }],
['a failed field that is not a list', { status: 200, body: { status: 'ok', failed: 'p4-private' } }],
['a list answer', { status: 200, body: [] }],
['a 500', { status: 500, body: { error: 'p4-private' } }],
['a 204-style 201', { status: 201, body: { status: 'ok' } }],
]) test(`deleteCredential refuses ${label} with proxy-client-refused and no upstream text`, async t => {
const s = await setup(t, () => answer);
await assert.rejects(s.client.deleteCredential('fixture-cred.json'), e => e.code === 'proxy-client-refused' && !e.message.includes('p4-private'));
assert.equal(s.calls.length, 1);
});
test('deleteCredential refuses a bad name before any request', async t => {
const s = await setup(t);
for (const name of ['', 'a\nb', 'x'.repeat(513), 7, null]) await assert.rejects(s.client.deleteCredential(name), { code: 'proxy-client-invalid' });
assert.equal(s.calls.length, 0);
});
test('deleteCredential follows no redirect', async t => {
const s = await setup(t, () => ({ status: 307, body: {}, headers: { location: 'http://127.0.0.2:1/v8/management/credentials' } }));
await assert.rejects(s.client.deleteCredential('fixture-cred.json'), { code: 'proxy-client-redirect' });
assert.equal(s.calls.length, 1);
});
@@ -0,0 +1,102 @@
// lane health-reason: does the pi worker container hold a credential for the provider a run asks
// for? pi answers, not a list kept here. Before it sends a prompt, pi checks
// `models.checkAuth(provider)` from @earendil-works/pi-ai (pi-coding-agent's agent-session.js). The
// worker image has no models.json and pi runs with --no-extensions, so that call is the whole
// gate. This runs the same call, from the pi-ai the lock installs, on stand-ins: each auth.json
// entry becomes a placeholder of its type, and each environment name the container gets with a
// value answers a placeholder. No key, token or value is read, passed or kept.
//
// credentialCheck({ provider, entries, envNames })
// entries: [{ name, type }] from the auth file the container mounts (names and types only)
// envNames: names the container's environment carries with a nonempty value
// answers { decided: true, known, has, providers } or { decided: false, why }
// known: pi has a provider by this id
// has: the container will hold a credential pi accepts for it
// providers: every provider pi would accept a credential for in that container, sorted
import { existsSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { pathToFileURL, fileURLToPath } from "node:url";
const REPO = fileURLToPath(new URL("../../../../", import.meta.url));
const SCOPE = "@earendil-works";
const PLACEHOLDER = "mosaic-credential-check-placeholder";
// Where pi reads auth.json inside the worker image: its agent directory under the image user's
// home (HOME=/home/node, pi's default ~/.pi/agent). compose.yaml mounts the host file there.
export const authFileInContainer = "/home/node/.pi/agent/auth.json";
// The pi-ai pi itself loads: the copy nested under pi-coding-agent first, else the hoisted one.
// Both come from package-lock.json, the lock the image's npm ci installs.
function piAiDir(root) {
for (const dir of [join(root, "node_modules", SCOPE, "pi-coding-agent", "node_modules", SCOPE, "pi-ai"),
join(root, "node_modules", SCOPE, "pi-ai")]) {
if (existsSync(join(dir, "package.json"))) return dir;
}
return null;
}
function exportPath(pkg, key) {
const entry = pkg?.exports?.[key];
const target = typeof entry === "string" ? entry : entry?.import ?? entry?.default;
return typeof target === "string" ? target : null;
}
async function loadPiAi(root) {
const dir = piAiDir(root);
if (!dir) return { why: "pi isn't installed in this checkout (run npm ci)" };
let pkg;
try { pkg = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")); } catch { return { why: "pi's package can't be read" }; }
const main = exportPath(pkg, ".");
const providers = exportPath(pkg, "./providers/*");
if (!main || !providers) return { why: "pi's package doesn't export what the check needs" };
try {
const ai = await import(pathToFileURL(join(dir, main)).href);
const all = await import(pathToFileURL(join(dir, providers.replace("*", "all"))).href);
if (typeof ai.createModels !== "function" || typeof all.builtinProviders !== "function") {
return { why: "pi's package doesn't export what the check needs" };
}
return { createModels: ai.createModels, builtinProviders: all.builtinProviders };
} catch {
return { why: "pi's package can't be loaded" };
}
}
// A stand-in for an entry: the type pi reads, with placeholder fields where pi wants them. A type
// pi doesn't know stays bare, and pi treats it as no credential, as it treats the real entry.
function standIn(type) {
if (type === "api_key") return { type, key: PLACEHOLDER };
if (type === "oauth") return { type, access: PLACEHOLDER, refresh: PLACEHOLDER, expires: Number.MAX_SAFE_INTEGER };
return { type: String(type) };
}
function store(entries) {
const map = new Map();
for (const { name, type } of entries) if (typeof name === "string" && name) map.set(name, standIn(type));
return {
read: async (id) => map.get(id),
list: async () => [...map].map(([providerId, c]) => ({ providerId, type: c.type })),
modify: async (id) => map.get(id),
delete: async () => {},
};
}
export async function credentialCheck({ provider, entries = [], envNames = [], root = REPO } = {}) {
const pi = await loadPiAi(root);
if (!pi.createModels) return { decided: false, why: pi.why };
const names = new Set(envNames);
try {
const models = pi.createModels({
credentials: store(entries),
// The container has no ambient credential files: its pi directory starts empty.
authContext: { env: async (name) => (names.has(name) ? PLACEHOLDER : undefined), fileExists: async () => false },
});
for (const p of pi.builtinProviders()) models.setProvider(p);
const ids = models.getProviders().map((p) => p.id);
const providers = [];
for (const id of ids) if ((await models.checkAuth(id)) !== undefined) providers.push(id);
providers.sort();
return { decided: true, known: ids.includes(provider), has: providers.includes(provider), providers };
} catch {
return { decided: false, why: "pi's credential check failed" };
}
}
@@ -17,15 +17,47 @@ export const MODEL_ID = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,199}$/;
const MODEL = MODEL_ID;
const MAX_MODELS = 10000;
const family = url => url === ZAI_URL ? 'zai' : 'other';
// #1873 account-remove: the catalogue upstream an account of this provider adds models under,
// or null when the list does not name it. The impact read keys on it.
export function providerUpstream(provider, list = PROVIDERS) {
const p = list.find(entry => entry.id === provider);
return !p ? null : p.signIn === 'oauth' ? p.id : p.signIn === 'apikey' ? family(p.baseUrl) : null;
}
// #1873 (Jason 2026-11-19): a Claude model from an OAuth account runs on claude-code only;
// a model from an API key, Claude or not, runs on claude-code and pi. The rule keys on the account
// kind: the proxy lists OAuth accounts (credentials, upstream `claude`) apart from API-key groups.
// It routes a request by model name across every account, so a group model whose name an OAuth
// Claude account offers (`oauth`) could be served by that account and is held to claude-code too.
const harnesses = (upstream, oauth = false) => upstream === 'claude' || oauth ? ['claude-code'] : ['claude-code', 'pi'];
// #1873 models-offered (A9): the provider id an entry of the owner's offered file is keyed by. An
// OAuth provider's catalogue upstream is its id; a key provider's is the family of its base URL.
// A group on any other base URL is keyed `other`.
export const offeredProvider = upstream => PROVIDERS.find(p => (p.signIn === 'oauth' ? p.id : family(p.baseUrl)) === upstream)?.id ?? upstream;
// The name the owner's model list shows for a provider: its PROVIDERS label, or a plain word for a
// group on another base URL.
export const offeredLabel = provider => PROVIDERS.find(p => p.id === provider)?.label ?? 'Other';
const offeredShape = o => !!o && typeof o === 'object' && o.version === 1 && !!o.providers && typeof o.providers === 'object' && !Array.isArray(o.providers)
&& Object.values(o.providers).every(e => !!e && Array.isArray(e.on) && Array.isArray(e.seen));
// `offered` is the parsed file, null or undefined when there is none, or anything else when it is
// damaged. No file, or a provider with no entry yet, keeps every model on: the plugin has no rule
// for a provider's current models, so the first read seeds all present models on and the Console
// writes them as on and seen. With an entry, a model is on only when it is both seen and on; one
// not seen yet is New and starts off. A damaged file switches every model off. Only `available`,
// `reason` and `isNew` change: `harnesses` never does, so the file cannot widen where a model runs.
function applyOffered(models, offered) {
if (offered === undefined || offered === null) return;
const whole = offeredShape(offered);
for (const m of models) {
const id = offeredProvider(m.upstream), entry = whole && Object.hasOwn(offered.providers, id) ? offered.providers[id] : undefined;
if (whole && entry === undefined) continue;
const seen = whole && entry.seen.includes(m.model);
if (whole && !seen) m.isNew = true;
if (!(seen && entry.on.includes(m.model))) { m.available = false; m.reason = 'model-off'; }
}
}
const slug = (upstream, model) => `${upstream}-${model}`.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/-+$/g, '');
export async function catalogue({ listAccounts, listModels, listGroups, now }) {
export async function catalogue({ listAccounts, listModels, listGroups, now, offered }) {
try {
const [accounts, groups] = await Promise.all([listAccounts(), listGroups()]);
if (!Array.isArray(accounts?.files) || !Array.isArray(groups) || accounts.files.length > MAX_MODELS || groups.length > MAX_MODELS) throw Error();
@@ -69,6 +101,7 @@ export async function catalogue({ listAccounts, listModels, listGroups, now }) {
for (const model of group.models) add(upstream, model?.alias || model?.name);
}
for (const m of models) m.harnesses = harnesses(m.upstream, oauth.has(m.model.toLowerCase()));
applyOffered(models, offered);
return { status: 'ready', observedAt: now(), models, notices: [...new Set(notices)] };
} catch (error) {
return { status: 'unavailable', observedAt: null, models: [], notices: [], ...(error.code === 'model-key-collision' ? { reason: error.code } : {}) };
@@ -11,7 +11,7 @@ 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 { fixtureDir, 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";
@@ -27,7 +27,7 @@ const CREDENTIAL = { ANTHROPIC_API_KEY: "fixture-api-key" };
const DIGEST = /^[a-f0-9]{64}$/;
function scratch(t) {
const root = mkdtempSync(join(tmpdir(), "mosaic-watch-"));
const root = fixtureDir("mosaic-watch-");
t.after(() => rmSync(root, { recursive: true, force: true }));
return root;
}
@@ -11,7 +11,7 @@ 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 { fixtureDir, 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";
@@ -255,15 +255,17 @@ test("W1 redaction covers real paths, trailing slashes, the user name, rule ids,
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";
// A multi-segment loose path stays a path even when tmpdir() is just /tmp.
const looseTempPath = join(tmpdir(), "mosaic-redaction-probe");
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 },
surfaces: { help: `usage: ${dir}/tool\nsee /opt/fixture-tools/bin/tool and ${looseTempPath}`, "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);
for (const p of [dir, realpathSync(dir), secret, env.MOSAIC_FIXTURE_BIN, "/opt/fixture-tools/bin", env.HOME, looseTempPath]) 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"]);
@@ -289,7 +291,9 @@ test("W1 real pi from node_modules gives a version, a digest and fit pass", NO_B
// 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);
// Outside /tmp, so the fit check runs it whatever the temp directory is (pi-stand-in.mjs).
const root = fixtureDir("mosaic-watch-pi-");
t.after(() => rmSync(root, { recursive: true, force: true }));
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" } }));
+2 -2
View File
@@ -10,7 +10,7 @@ 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";
import { fixtureDir, piStandIn } from "../../harness/tests/pi-stand-in.mjs";
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
@@ -25,7 +25,7 @@ async function load(name) {
}
function scratch(t) {
const root = mkdtempSync(join(tmpdir(), "mosaic-harness-plugin-"));
const root = fixtureDir("mosaic-harness-plugin-");
t.after(() => rmSync(root, { recursive: true, force: true }));
return root;
}
@@ -1,6 +1,6 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { catalogue, preflight } from '../model-provider/cliproxy/index.mjs';
import { catalogue, preflight, providerUpstream, PROVIDERS, route } from '../model-provider/cliproxy/index.mjs';
const ZAI = 'https://api.z.ai/api/anthropic';
const deps = () => ({
@@ -179,3 +179,88 @@ test('a Claude OAuth account whose models cannot be read makes the catalogue una
d.listModels = async () => { throw new Error('fixture private error'); };
assert.equal((await catalogue(d)).status, 'ready');
});
// #1873 account-remove: the impact read finds an account's catalogue rows by providerUpstream, so it
// must name the upstream catalogue() files that account's models under, and null when it cannot.
test('providerUpstream names the upstream catalogue() uses for each provider, and null for one it cannot place', async () => {
const c = await catalogue({ ...deps(), listAccounts: async () => ({ files: [{ name: 'credential-1', provider: 'claude', status: 'active' }, { name: 'credential-2', provider: 'codex', status: 'active' }] }),
listModels: async name => ({ models: [{ id: name === 'credential-1' ? 'fixture-chat' : 'fixture-codex' }] }) });
assert.deepEqual([...new Set(c.models.map(m => m.upstream))].sort(), ['claude', 'codex', 'zai']);
assert.deepEqual(PROVIDERS.map(p => [p.id, providerUpstream(p.id)]), [['claude', 'claude'], ['codex', 'codex'], ['z.ai', 'zai']]);
assert.equal(providerUpstream('other'), null);
assert.equal(providerUpstream('z.ai', [{ id: 'z.ai', signIn: 'apikey', baseUrl: 'https://example.invalid/anthropic' }]), 'other');
assert.equal(providerUpstream('x', [{ id: 'x', signIn: 'device' }]), null);
});
// #1873 models-offered (A9): the owner's on/off file as the catalogue input `offered`.
const offeredDeps = () => {
const d = deps();
d.listModels = async name => ({ models: name === 'credential-1' ? [{ id: 'fixture-chat' }, { id: 'fixture-chat-older' }] : [] });
d.listGroups = async () => [{ 'base-url': ZAI, keys: [{ auth_index: 'k' }], models: [{ name: 'fixture-glm' }, { name: 'fixture-glm-new' }] }];
return d;
};
const state = c => Object.fromEntries(c.models.map(m => [m.model, [m.available, m.reason, m.isNew === true, m.harnesses]]));
test('models-offered: no file, or no entry for a provider, keeps every model as the plugin lists it', async () => {
const plain = state(await catalogue(offeredDeps()));
for (const offered of [undefined, null, { version: 1, providers: {} }]) assert.deepEqual(state(await catalogue({ ...offeredDeps(), offered })), plain, JSON.stringify(offered));
assert.deepEqual(plain['fixture-chat'], [true, null, false, ['claude-code']]);
assert.deepEqual(plain['fixture-glm'], [true, null, false, ['claude-code', 'pi']]);
});
test('models-offered: an off model and an unseen one are unavailable with model-off, the unseen one is New, and harnesses never change', async () => {
const offered = { version: 1, providers: { claude: { on: ['fixture-chat'], seen: ['fixture-chat', 'fixture-chat-older'] }, 'z.ai': { on: ['fixture-glm'], seen: ['fixture-glm'] } } };
const c = await catalogue({ ...offeredDeps(), offered });
assert.equal(c.status, 'ready');
assert.deepEqual(state(c), {
'fixture-chat': [true, null, false, ['claude-code']],
'fixture-chat-older': [false, 'model-off', false, ['claude-code']],
'fixture-glm': [true, null, false, ['claude-code', 'pi']],
'fixture-glm-new': [false, 'model-off', true, ['claude-code', 'pi']],
});
// Off is refused at admission like any unavailable model.
const older = c.models.find(m => m.model === 'fixture-chat-older');
assert.deepEqual(preflight({ selection: older, harness: 'claude-code', catalogue: c }), { allow: false, code: 'model-provider-unavailable' });
// On in a file that also lists it never widens an OAuth Claude model to pi.
const chat = c.models.find(m => m.model === 'fixture-chat');
assert.deepEqual(preflight({ selection: chat, harness: 'pi', catalogue: c }), { allow: false, code: 'model-harness-incompatible' });
});
test('models-offered: a key model whose name an OAuth Claude account offers stays claude-code only when switched on', async () => {
const d = offeredDeps();
d.listGroups = async () => [{ 'base-url': ZAI, keys: [{ auth_index: 'k' }], models: [{ name: 'fixture-chat' }] }];
const c = await catalogue({ ...d, offered: { version: 1, providers: { claude: { on: ['fixture-chat'], seen: ['fixture-chat'] }, 'z.ai': { on: ['fixture-chat'], seen: ['fixture-chat'] } } } });
const zai = c.models.find(m => m.upstream === 'zai');
assert.deepEqual([zai.available, zai.harnesses], [true, ['claude-code']]);
// A selection of unknown account kind (no catalogue module) named as Claude is still held to claude-code.
assert.deepEqual(route({ selection: { provider: 'zai', model: 'fixture-claude-chat' }, harness: 'pi' }), { code: 'model-harness-incompatible' });
// A Claude account whose state is unknown, signed out or switched off is not listed, but its names
// still count as OAuth: switching the same-named key model on keeps it claude-code only.
for (const accountState of [{ status: 'unknown' }, { status: 'error' }, { status: 'active', disabled: true }, { status: 'active', unavailable: true }]) {
const u = offeredDeps();
u.listAccounts = async () => ({ files: [{ name: 'credential-1', provider: 'claude', ...accountState }] });
u.listGroups = async () => [{ 'base-url': ZAI, keys: [{ auth_index: 'k' }], models: [{ name: 'fixture-chat' }] }];
const cu = await catalogue({ ...u, offered: { version: 1, providers: { 'z.ai': { on: ['fixture-chat'], seen: ['fixture-chat'] } } } });
const label = JSON.stringify(accountState);
assert.deepEqual(cu.models.map(m => [m.upstream, m.model, m.available, m.harnesses]), [['zai', 'fixture-chat', true, ['claude-code']]], label);
assert.deepEqual(preflight({ selection: cu.models[0], harness: 'pi', catalogue: cu }), { allow: false, code: 'model-harness-incompatible' }, label);
}
});
test('models-offered: a damaged file switches every model off and never falls back to all on', async () => {
for (const offered of ['damaged', 'x', 42, [], { version: 2, providers: {} }, { version: 1 }, { version: 1, providers: [] }, { version: 1, providers: { claude: { on: 'fixture-chat', seen: [] } } }, { version: 1, providers: { claude: null } }]) {
const c = await catalogue({ ...offeredDeps(), offered });
assert.equal(c.status, 'ready', JSON.stringify(offered));
assert.ok(c.models.length === 4 && c.models.every(m => m.available === false && m.reason === 'model-off'), JSON.stringify(offered));
assert.ok(c.models.every(m => m.harnesses.length > 0), 'harnesses are kept, never widened or blanked');
}
});
test('models-offered: an entry keyed by an inherited name is not an entry', async () => {
const d = offeredDeps();
d.listAccounts = async () => ({ files: [] });
d.listGroups = async () => [{ 'base-url': 'https://fixture.invalid', keys: [{ auth_index: 'b' }], models: [{ name: 'fixture-other' }] }];
const providers = Object.create({ other: { on: [], seen: [] } });
const c = await catalogue({ ...d, offered: { version: 1, providers } });
assert.deepEqual([c.models[0].available, c.models[0].reason], [true, 'unverified']);
});
+2 -2
View File
@@ -9,14 +9,14 @@ 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";
import { fixtureDir, 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-"));
const root = fixtureDir("mosaic-pi-bin-");
t.after(() => rmSync(root, { recursive: true, force: true }));
return root;
}
+2 -1
View File
@@ -5,6 +5,7 @@
export { RunsError } from "./errors.mjs";
export { RUNS_DIRNAME, STATE_DIRNAME } from "./paths.mjs";
export {
RUN_ID_PATTERN, RUN_DOCUMENTS, isRunId, listRunIds, readRunDocument, listRunRecords, readRunRecord,
RUN_ID_PATTERN, RUN_DOCUMENTS, RUN_TEXTS, RUN_TEXT_TAIL, isRunId, listRunIds, readRunDocument, listRunRecords,
readRunRecord, readRunText,
} from "./runs.mjs";
export { POINTER_FILE, ACTIVATION_LOG_FILE, readActivePointer, readActivationLog } from "./state.mjs";
+37
View File
@@ -70,3 +70,40 @@ export function readRunRecord(dataRoot, runId) {
artifacts,
};
}
export const RUN_TEXTS = ["stderr.txt"];
export const RUN_TEXT_TAIL = 64 * 1024;
// The last RUN_TEXT_TAIL bytes of one of RUN_TEXTS from a run, as UTF-8 text, or null when it is
// missing, not a regular file, unreadable or outside the data root. A cut that lands inside a
// line drops that partial first line.
export function readRunText(dataRoot, runId, name) {
requireRunId(runId);
if (!RUN_TEXTS.includes(name)) throw new RunsError(`unknown run text: ${JSON.stringify(name)}`);
let fd;
try {
const file = resolveInside(dataRoot, RUNS_DIRNAME, runId, name);
if (file === null) return null;
fd = fs.openSync(file, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW);
const stat = fs.fstatSync(fd);
if (!stat.isFile()) return null;
const length = Math.min(stat.size, RUN_TEXT_TAIL);
const buffer = Buffer.alloc(length);
let read = 0;
while (read < length) {
const n = fs.readSync(fd, buffer, read, length - read, stat.size - length + read);
if (n === 0) break;
read += n;
}
let text = buffer.subarray(0, read).toString("utf8");
if (stat.size > length) {
const newline = text.indexOf("\n");
text = newline < 0 ? "" : text.slice(newline + 1);
}
return text;
} catch {
return null;
} finally {
if (fd !== undefined) fs.closeSync(fd);
}
}
+27 -1
View File
@@ -3,7 +3,7 @@ import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import {
RunsError, isRunId, listRunIds, listRunRecords, readRunDocument, readRunRecord,
RUN_TEXT_TAIL, RunsError, isRunId, listRunIds, listRunRecords, readRunDocument, readRunRecord, readRunText,
} from "../src/index.mjs";
import { RESULT, scratch, tree, write } from "./helpers.mjs";
@@ -241,3 +241,29 @@ test("the readers write nothing", (t) => {
readRunDocument(dataRoot, A, "mission.json");
assert.equal(tree(dataRoot), before);
});
test("readRunText reads a run's stderr.txt, its last RUN_TEXT_TAIL bytes from a line start", (t) => {
const { dataRoot } = scratch(t);
write(path.join(dataRoot, "runs", A, "stderr.txt"), "one\ntwo\n");
assert.equal(readRunText(dataRoot, A, "stderr.txt"), "one\ntwo\n");
const long = `${"x".repeat(RUN_TEXT_TAIL)}\nlast line\n`;
write(path.join(dataRoot, "runs", B, "stderr.txt"), long);
assert.equal(readRunText(dataRoot, B, "stderr.txt"), "last line\n");
});
test("readRunText is null for a missing run, a directory or a file linked out of the data root", (t) => {
const { dataRoot, outside } = scratch(t);
assert.equal(readRunText(dataRoot, A, "stderr.txt"), null);
fs.mkdirSync(path.join(dataRoot, "runs", A, "stderr.txt"), { recursive: true });
assert.equal(readRunText(dataRoot, A, "stderr.txt"), null);
write(path.join(outside, "secret.txt"), "outside\n");
fs.mkdirSync(path.join(dataRoot, "runs", B), { recursive: true });
fs.symlinkSync(path.join(outside, "secret.txt"), path.join(dataRoot, "runs", B, "stderr.txt"));
assert.equal(readRunText(dataRoot, B, "stderr.txt"), null);
});
test("readRunText reads only stderr.txt and refuses an invalid run id", (t) => {
const { dataRoot } = scratch(t);
assert.throws(() => readRunText(dataRoot, A, "result.json"), (e) => e instanceof RunsError && /unknown run text/.test(e.message));
assert.throws(() => readRunText(dataRoot, "../x", "stderr.txt"), (e) => e instanceof RunsError && /invalid run id/.test(e.message));
});
+32 -2
View File
@@ -35,7 +35,7 @@
import { spawnSync } from "node:child_process";
import { existsSync, lstatSync, mkdirSync, readlinkSync, realpathSync, statSync } from "node:fs";
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
import { dirname, isAbsolute, join, posix, relative, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { ID_PATTERN } from "../../organization/src/vocabulary.mjs";
@@ -234,9 +234,39 @@ export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWR
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", "--"] : [];
//
// pi-fit-tmp: bwrap mounts in the order it is given, and a mount hides every mount made earlier
// under its target. A fit run binds its scratch directory at its own path and the scratch's tmp/
// over /tmp, so with the scratch under /tmp (TMPDIR unset) the second bind hid the first and the
// run couldn't even chdir. The check's binds now go through mountOrder: a bind whose target lies
// under another's comes after it, whatever the caller's temp directory is.
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];
return [...owned(), ...bound(unshare), bwrap, "--die-with-parent", ...NAMESPACES, "--unshare-net", ...argsOf(mountOrder([...systemBinds(node), ...list])), "--remount-ro", "/", "--clearenv", ...setenv, "--chdir", chdir, "--", ...KILL_WITH_PARENT, ...command];
}
// pi-fit-tmp: list in an order bwrap can mount without one bind hiding another: each bind moves
// ahead of the first bind whose target lies strictly under its own. Binds with no such relation
// keep their order, and so do binds with the same target, so a later one still covers an earlier
// one at that same target. Only the order changes: the same binds, each with its own source and
// target. Targets compare in plain form ("/a/" and "/a/./" are "/a"); an entry with no target
// stays where it is. Limits, by design: no bind is left hidden under a later one, so a caller that
// counts on a later mount covering what an earlier one put under it (a tmpfs over a directory,
// or [/b at /a, /a/x, /c at /a], where /c no longer covers /a/x) must not use it. Only checkArgv
// does; a session's binds() keep their own order.
export function mountOrder(list) {
const target = (b) => {
const t = b.dest ?? b.src;
return typeof t === "string" && t.startsWith("/") ? posix.normalize(t).replace(/(.)\/+$/, "$1") : null;
};
const under = (path, dir) => path !== null && dir !== null && (dir === "/" ? path !== "/" : path.startsWith(`${dir}/`));
const out = [];
for (const b of list) {
const at = out.findIndex((o) => under(target(o), target(b)));
if (at < 0) out.push(b);
else out.splice(at, 0, b);
}
return out;
}
// 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];
+2
View File
@@ -11,6 +11,8 @@ export const AGENT_VERBS = ['instance.launch', 'instance.regenerate', 'instance.
// P4 owner transport only; the HTTP boundary and broker both refuse users/testers.
export const PROXY_VERBS = Object.freeze(['proxy.accounts', 'proxy.login.start', 'proxy.login.submit',
'proxy.login.status', 'proxy.login.cancel', 'proxy.apikey.add', 'proxy.priority.set', 'proxy.group.models.set', 'proxy.keys', 'plugins', 'plugins.set',
// #1873 account-remove: reached only through the instance owner's /api/proxy/admin/accounts/remove.
'proxy.account.remove',
// #1873: proxy setup from the page. The broker gates both to the human owner.
'proxy.init', 'proxy.fence.clear']);
export const TESTER_AGENT_VERBS = ['instance.launch', 'instance.end', 'instance.request'];
+39
View File
@@ -36,6 +36,9 @@ 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 models-offered (A9): the owner's per-provider model lists, and one provider's on list.
'/api/proxy/admin/models': ['GET', []],
'/api/proxy/admin/models/set': ['POST', ['provider', 'on', 'revision', 'confirmInUse']],
// #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', []],
@@ -44,6 +47,9 @@ const ROUTES = Object.freeze({
// 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', []],
// #1873 account-remove (Jason 2026-11-20): what removing one account takes away, then the remove.
'/api/proxy/admin/accounts/impact': ['GET', ['account']],
'/api/proxy/admin/accounts/remove': ['POST', ['accountId']],
});
// 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']);
@@ -107,6 +113,26 @@ export async function proxyAdminHTTP(req, res, { path, admin, call, named, teste
if (Object.keys(args).some(k => !allowed.includes(k))) throw Error();
} catch { return refuse('invalid-request'); }
}
// #1873 models-offered (A9). A model in use that a save would switch off is refused with who
// uses it (organization and instance names only, and each organization whose usage is unknown),
// so the page can ask in place. The read may write the first seed, so it seeds only when the
// request passes the write checks above: a sending Console, same-origin fetch metadata, and no
// other origin (a same-origin GET fetch sends none).
if (path === '/api/proxy/admin/models' || path === '/api/proxy/admin/models/set') {
try {
if (path === '/api/proxy/admin/models') {
const seed = send === true && req.headersDistinct['sec-fetch-site']?.length === 1 && req.headers['sec-fetch-site'] === 'same-origin'
&& (req.headers.origin === undefined || (req.headersDistinct.origin?.length === 1 && req.headers.origin === origin));
return done(200, await admin.models({ seed }));
}
if (!Array.isArray(args.on) || (Object.hasOwn(args, 'confirmInUse') && typeof args.confirmInUse !== 'boolean')) return refuse('invalid-request');
return done(200, await admin.modelsSet({ provider: args.provider, on: args.on, revision: args.revision, confirmInUse: args.confirmInUse === true, user: typeof user === 'string' ? user : null }));
} catch (error) {
const code = ['models-offered-conflict', 'models-in-use', 'models-invalid', 'invalid-request', 'not-configured', 'proxy-down', 'proxy-admin-busy'].includes(error?.code) ? error.code : 'outcome-unknown';
const status = code === 'models-offered-conflict' || code === 'models-in-use' ? 409 : STATUS[code] ?? 400;
return done(status, { error: code, ...(code === 'models-in-use' ? { usedBy: Array.isArray(error.usedBy) ? error.usedBy : [], unanswered: Array.isArray(error.unanswered) ? error.unanswered : [] } : {}) });
}
}
try {
switch (path) {
case '/api/proxy/admin/status': return done(200, await admin.status());
@@ -148,6 +174,19 @@ export async function proxyAdminHTTP(req, res, { path, admin, call, named, teste
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());
// #1873 account-remove. The impact read takes exactly one account query key, a 12-hex id.
// The remove's own refusals pass through by name; anything else falls to the codes below.
case '/api/proxy/admin/accounts/impact': {
let params; try { params = [...new URL(req.url, 'http://localhost').searchParams]; } catch { params = []; }
if (params.length !== 1 || params[0][0] !== 'account' || !/^[a-f0-9]{12}$/.test(params[0][1])) return refuse('invalid-request');
if (!call) return refuse('not-configured');
return done(200, await admin.accountImpact(call, { accountId: params[0][1] }));
}
case '/api/proxy/admin/accounts/remove':
if (Object.keys(args).length !== 1 || typeof args.accountId !== 'string' || !/^[a-f0-9]{12}$/.test(args.accountId)) return refuse('invalid-request');
if (!call) return refuse('not-configured');
try { return done(200, await admin.accountRemove(call, { accountId: args.accountId, user: typeof user === 'string' ? user : null })); }
catch (error) { if (['remove-write-refused', 'remove-unconfirmed'].includes(error?.code)) return refuse(error.code); throw error; }
}
} catch (error) {
const code = ['human-required', 'not-configured', 'invalid-request'].includes(error?.code) ? error.code : proxyCode(error, 'outcome-unknown');
+201 -5
View File
@@ -599,7 +599,8 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
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>'
+ '<div data-accounts-part><ul id="proxy-accounts" class="rows" aria-label="Accounts"></ul>'
+ '<p class="small muted">Removing an account is not built yet.</p>'
// #1873 account-remove: the instance owner removes an account here; anyone else is told where.
+ '<p id="proxy-remove-note" class="small muted" hidden>Only the instance owner removes an account, on this instance\'s Model setup page.</p>'
+ '<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">'
@@ -751,16 +752,108 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
else setCard('accounts', 'done', accountList.map(a => `${providerLabel(a.provider)} ${health(a.status)[1].toLowerCase()}`).join(' · '));
focusStep();
}
// #1873 account-remove (Jason 2026-11-20): the owner's Remove asks in place and names what moves,
// from GET /api/proxy/admin/accounts/impact. Only facts the read holds are named; nothing here
// says where an agent goes next. The remove is POST /api/proxy/admin/accounts/remove.
let removeAsk = null, removeBusy = false;
const REMOVE_WORDS = {
// A refused or unanswered write may still have taken effect, so none of these says nothing was
// removed; confirmRemove reads the accounts again after any failure.
'remove-write-refused': 'The model service refused to remove the account. Check the list before trying again.',
'remove-unconfirmed': 'Mosaic asked the model service to remove the account, but it is still listed. Read the accounts again before trying again.',
'proxy-uncertain': 'Mosaic could not confirm the remove: the model service is between writes. Check the service and the list before trying again.',
'proxy-down': 'The model service stopped answering, so Mosaic could not confirm the remove. Check the service and the list before trying again.',
'proxy-admin-busy': 'Another setup action is running. Try again when it finishes.',
'invalid-request': 'The model service no longer lists this account. Read the accounts again.',
'human-required': 'Only the instance owner removes an account.',
};
async function adminCall(path, args) {
const response = await fetch('/api/proxy/admin/' + path, { cache: 'no-store', credentials: 'same-origin', redirect: 'manual',
method: args === undefined ? 'GET' : 'POST', ...(args === undefined ? {} : { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(args) }) });
if (response.type === 'opaqueredirect' || response.status === 401) throw new Error('Sign in to the Console again.');
let value = null; try { value = await response.json(); } catch { /* refused below */ }
if (!response.ok) { const code = typeof value?.error === 'string' && Object.hasOwn(REMOVE_WORDS, value.error) ? value.error : null;
throw Object.assign(new Error(code ? REMOVE_WORDS[code] : 'The remove could not be confirmed. Nothing was retried. Read the accounts again before trying again.'), { code }); }
return value;
}
const orgLabel = id => window.MosaicOrg?.row?.(id)?.label || id;
const listWords = items => new Intl.ListFormat('en').format(items);
// What removing the account takes away, in plain words, from the impact read.
function impactLine(impact, name) {
if (!impact || typeof impact !== 'object') return `Mosaic could not read what this account serves, so it can't say which models leave.`;
if (impact.moves === 'none') return impact.others > 0 ? `Another ${name} account serves the same models, so no model leaves.` : 'This account serves no model now, so no model leaves.';
if (impact.moves === 'unknown') return impact.others > 0 ? `Another ${name} account stays and may serve its models. Mosaic can't tell which models only this account serves.` : `Mosaic can't tell which models this account serves.`;
const models = Array.isArray(impact.models) ? impact.models.filter(m => typeof m === 'string') : [];
const parts = [impact.moves === 'all' ? `Every ${name} model leaves your organizations.` : `${listWords(models)} ${models.length === 1 ? 'leaves' : 'leave'} your organizations: no other account offers ${models.length === 1 ? 'it' : 'them'}.`];
if (!Array.isArray(impact.organizations)) return `${parts[0]} Mosaic could not read your organizations, so it can't say which agents use them.`;
const orgs = impact.organizations;
const uses = orgs.filter(o => o?.readable === true && Array.isArray(o.uses)).flatMap(o => o.uses.map(u => `${orgLabel(o.id)}'s ${u.instance} uses ${u.model}${u.fallback ? ' as its fallback' : ''}`));
const unread = orgs.filter(o => o?.readable !== true).map(o => orgLabel(o.id));
if (uses.length) parts.push(`${listWords(uses)}. Pick another model on ${new Set(orgs.filter(o => o?.uses?.length).map(o => o.id)).size === 1 ? 'that organization\'s' : 'each organization\'s'} Models page.`);
else if (!unread.length) parts.push(orgs.length ? 'No agent uses them.' : 'No organization uses your models.');
if (unread.length) parts.push(`Mosaic could not read ${listWords(unread)}'s models.`);
return parts.join(' ');
}
async function askRemove(account) {
if (removeBusy || !proxyAdmin || !canWrite) return;
removeAsk = { accountId: account.accountId, phase: 'reading', impact: null, error: null }; paintAccounts();
let impact = null;
try { impact = await adminCall('accounts/impact?account=' + encodeURIComponent(account.accountId)); } catch (error) { impact = null; if (removeAsk?.accountId === account.accountId) removeAsk.error = error.code === 'invalid-request' ? error.message : null; }
if (removeAsk?.accountId !== account.accountId) return;
removeAsk.phase = 'ready'; removeAsk.impact = impact; paintAccounts();
$('proxy-remove-yes')?.focus();
}
async function confirmRemove(account) {
if (removeBusy || !proxyAdmin || !canWrite) return;
const name = providerShort(account.provider);
removeBusy = true; removeAsk.error = null; paintAccounts();
try {
await adminCall('accounts/remove', { accountId: account.accountId });
removeAsk = null; message(`${name} account removed.`);
await refresh(); await setup.reread();
} catch (error) {
if (removeAsk) removeAsk.error = error.message; message(error.message);
// The outcome may be unknown: read the list again so it shows what the service holds now.
try { await refresh(); } catch { /* the message above already says to check */ }
if (!accountList.some(a => a.accountId === account.accountId)) message(`${name} account is no longer listed.`);
}
finally { removeBusy = false; paintAccounts(); }
}
function removeBox(account) {
const name = providerShort(account.provider), box = el('div', '', 'proxy-program-confirm proxy-remove-ask');
box.setAttribute('role', 'group'); box.setAttribute('aria-labelledby', 'proxy-remove-q');
const ask = el('p'); ask.id = 'proxy-remove-q'; ask.append(el('strong', `Remove ${name}?`), ' ');
if (removeAsk.phase === 'reading') { ask.append('Reading what this account serves…'); box.append(ask); return box; }
const what = keyed(account.provider) ? ['key', 'add a key again'] : ['sign-in', 'sign in again'];
ask.append(`${removeAsk.error && !removeAsk.impact ? removeAsk.error : impactLine(removeAsk.impact, name)} Mosaic deletes your ${name} ${what[0]} from the model service. To use ${name} again you ${what[1]}.`);
const yes = el('button', removeBusy ? 'Removing…' : `Remove ${name}`, 'danger'); yes.type = 'button'; yes.id = 'proxy-remove-yes'; yes.disabled = removeBusy || !canWrite;
yes.addEventListener('click', () => confirmRemove(account));
const no = el('button', `Keep ${name}`); no.type = 'button'; no.id = 'proxy-remove-no'; no.disabled = removeBusy;
no.addEventListener('click', () => { removeAsk = null; paintAccounts(); $('proxy-accounts').querySelector(`[data-remove="${account.accountId}"]`)?.focus(); });
const foot = el('p', '', 'proxy-setup-history-foot'); foot.append(yes, no);
box.append(ask, foot);
if (removeAsk.error && removeAsk.impact) box.append(el('p', removeAsk.error, 'banner err'));
return box;
}
function paintAccounts() {
const list = $('proxy-accounts');
const note = $('proxy-remove-note'); if (note) note.hidden = proxyAdmin || !accountList.length;
if (removeAsk && !accountList.some(a => a.accountId === removeAsk.accountId)) removeAsk = null;
if (!accountList.length) { list.replaceChildren(el('li', 'No accounts yet.', 'muted')); return; }
list.replaceChildren(...accountList.map(account => {
const li = el('li'), name = el('span', providerShort(account.provider)), kind = el('span', providerKind(account.provider) || '—');
kind.append(el('br'), el('small', account.displayName, 'muted'));
const [cls, word] = health(account.status), badge = el('span', word, `badge ${cls}`);
li.dataset.account = account.accountId; li.append(name, kind, badge);
if (proxyAdmin && canWrite) {
const remove = el('button', 'Remove'); remove.type = 'button'; remove.dataset.remove = account.accountId;
remove.setAttribute('aria-label', `Remove ${account.displayName}`); remove.disabled = removeBusy || removeAsk !== null;
remove.addEventListener('click', () => askRemove(account));
li.classList.add('removable'); li.append(remove);
}
// The accounts read says why an account is not working (account.reason, a7362a8fc and later). Absent, nothing shows.
if (typeof account.reason === 'string' && account.reason) li.append(el('p', account.reason, 'small muted account-reason'));
if (removeAsk?.accountId === account.accountId && proxyAdmin && canWrite) li.append(removeBox(account));
return li;
}));
}
@@ -810,11 +903,112 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
setup.accounts(accountList.length);
paintAccountCard();
}
// Models offered: read-only, from the routes read of an organization whose model list is ready.
// Turning one model on or off is not built yet, so each switch reads Coming later.
// Models offered. The instance owner switches models on or off per provider (#1873 models-offered,
// A9, GET /api/proxy/admin/models and POST models/set). Anyone else reads the list from an
// organization whose model list is ready, and each switch reads Coming later.
let offeredRound = 0;
async function offeredCall(path, args) {
const response = await fetch('/api/proxy/admin/' + path, { cache: 'no-store', credentials: 'same-origin', redirect: 'manual', method: args === undefined ? 'GET' : 'POST',
...(args === undefined ? {} : { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(args) }) });
if (response.type === 'opaqueredirect' || response.status === 401) throw Object.assign(new Error(), { code: 'signed-out' });
let value = null; try { value = await response.json(); } catch { /* refused below */ }
if (!response.ok) throw Object.assign(new Error(), { code: typeof value?.error === 'string' && /^[a-z-]+$/.test(value.error) ? value.error : 'outcome-unknown', usedBy: Array.isArray(value?.usedBy) ? value.usedBy : [], unanswered: Array.isArray(value?.unanswered) ? value.unanswered : [] });
return value;
}
const OFFERED_SAYS = {
'signed-out': 'Sign in to the Console again.',
'models-offered-conflict': 'The model list changed since this page read it. Read it again, then make your change.',
'models-invalid': 'The model service no longer lists one of these models. Read the list again.',
'proxy-down': 'No organization that uses your model service answered, so its models can\'t be listed now.',
'not-configured': 'No organization uses your model service yet. Its models show here once one does.',
'proxy-admin-busy': 'Another change to the model service is running. Try again in a moment.',
'outcome-unknown': 'The outcome is unknown. Read the list again before you try again.',
};
const offeredSays = code => Object.hasOwn(OFFERED_SAYS, code) ? OFFERED_SAYS[code] : `The change was refused (${code}).`;
const offeredWho = u => { const org = window.MosaicOrg?.row?.(u.org)?.label || u.org; return `${u.instance ? `${org}'s ${u.instance}` : `an agent in ${org}`}${u.fallback ? ' fallback' : ''}`; };
let offeredSaved = null;
async function readOfferedOwner(round) {
const box = $('proxy-offered-list'), saved = offeredSaved;
const open = new Set([...box.querySelectorAll('details[data-offered][open]')].map(d => d.dataset.offered));
offeredSaved = null;
box.replaceChildren(el('p', 'Reading the models…', 'small muted'));
let view;
try { view = await offeredCall('models'); }
catch (error) { if (round === offeredRound) box.replaceChildren(el('p', offeredSays(error.code), 'small muted')); return; }
if (round !== offeredRound) return;
const providers = Array.isArray(view?.providers) ? view.providers : [];
if (!providers.length) { box.replaceChildren(el('p', 'Your model service lists no models yet.', 'small muted')); return; }
box.replaceChildren(...providers.map(p => offeredList(p, view.revision, open.has(p.provider), saved === p.provider)));
}
function offeredList(p, revision, open, saved) {
const name = String(p.label), models = Array.isArray(p.models) ? p.models : [];
const details = el('details'), summary = el('summary', `${name} models offered: ${p.on} of ${p.total} on`), inside = el('div', undefined, 'inside');
details.dataset.offered = p.provider; details.open = open; details.append(summary, inside);
const form = el('form', undefined, 'settings-form'), set = el('fieldset'), list = el('ul', undefined, 'model-list');
set.append(el('legend', `${name} models`), el('p', `A model you switch off is offered nowhere: not to any organization, agent or Triage. A model ${name} adds later shows here as New, switched off.`, 'help small muted'));
if (models.length > 8) {
const field = el('div', undefined, 'field'), id = `proxy-find-${String(p.provider).replace(/[^a-z0-9]/g, '')}`, label = el('label', `Find a ${name} model`), input = document.createElement('input');
label.htmlFor = id; input.id = id; input.type = 'search'; input.autocomplete = 'off'; input.placeholder = 'Part of a name';
input.addEventListener('input', () => { const q = input.value.trim().toLowerCase(); list.querySelectorAll('li').forEach(li => { li.hidden = Boolean(q) && !li.dataset.model.includes(q); }); });
field.append(label, input); set.append(field);
}
const boxes = models.map(m => {
const li = el('li'), label = el('label'), check = document.createElement('input'), note = el('span', '', 'small muted');
check.type = 'checkbox'; check.checked = m.on === true; check.value = m.id; check.dataset.was = String(m.on === true);
const users = Array.isArray(m.usedBy) ? m.usedBy : [];
const paint = () => {
const changed = String(check.checked) !== check.dataset.was;
note.textContent = [m.isNew ? 'New' : '', users.length ? `In use: ${users.map(offeredWho).join(', ')}` : '', changed ? `Switched ${check.checked ? 'on' : 'off'}, not saved yet` : ''].filter(Boolean).join(' · ');
};
check.addEventListener('change', () => { paint(); dirty(); });
label.append(check, ` ${m.label}`); li.dataset.model = String(m.id).toLowerCase(); li.append(label, note); paint();
list.append(li); return { check, users, model: m };
});
set.append(list);
const foot = el('div', undefined, 'form-foot'), save = el('button', `Save ${name} models`, 'btn primary'), said = el('p'), ask = el('div', undefined, 'banner ref');
save.type = 'button'; save.dataset.offeredSave = ''; save.disabled = true; said.setAttribute('role', 'status'); if (saved) { said.className = 'settings-success'; said.textContent = `Saved. ${p.on} of ${p.total} on.`; } said.setAttribute('aria-live', 'polite'); ask.hidden = true; ask.setAttribute('role', 'alert');
foot.append(save, said); form.append(set, foot, ask); inside.append(form);
if (!offeredSend) { set.disabled = true; said.className = 'small muted'; said.textContent = 'This Console is read-only. Switching models on or off is unavailable.'; }
function dirty() { save.disabled = !boxes.some(b => String(b.check.checked) !== b.check.dataset.was); ask.hidden = true; }
async function send(confirmInUse) {
save.disabled = true; set.disabled = true; ask.hidden = true; said.className = 'small muted'; said.textContent = 'Saving…';
const on = boxes.filter(b => b.check.checked).map(b => b.model.id);
try {
await offeredCall('models/set', { provider: p.provider, on, revision, ...(confirmInUse ? { confirmInUse: true } : {}) });
offeredSaved = p.provider; readOffered();
} catch (error) {
set.disabled = false; dirty();
if (error.code === 'models-in-use') {
said.textContent = ''; said.className = '';
const who = error.usedBy.map(u => `${u.model}: ${offeredWho(u)}`).join('; ');
const quiet = error.unanswered.map(org => window.MosaicOrg?.row?.(org)?.label || org).join(', ');
const yes = el('button', 'Switch off anyway', 'btn'), no = el('button', 'Keep them on', 'btn'), actions = el('div', undefined, 'actions');
yes.type = 'button'; no.type = 'button'; actions.append(yes, no);
const says = [];
if (who) says.push(el('p', `In use: ${who}. Switched off, a model is offered nowhere, so these can no longer use it. Give each one another model on its organization's Models page.`));
if (quiet) says.push(el('p', `Not answering now, so it is not known which models they use: ${quiet}. A model you switch off stops working for them too.`));
ask.replaceChildren(...says, actions);
ask.hidden = false;
yes.addEventListener('click', () => send(true));
no.addEventListener('click', () => { ask.hidden = true; save.focus(); });
yes.focus();
return;
}
said.className = 'banner err'; said.textContent = offeredSays(error.code);
if (['models-offered-conflict', 'models-invalid', 'outcome-unknown'].includes(error.code)) {
const again = el('button', 'Read the list again', 'btn'); again.type = 'button'; again.addEventListener('click', () => readOffered()); said.append(' ', again);
}
}
}
// A plain button, not a submit: the read-only rule for organization pages hides submit buttons,
// and this list is the instance owner's (offeredSend), not an organization's setting.
save.addEventListener('click', () => { if (!save.disabled && offeredSend) send(false); });
form.addEventListener('submit', event => event.preventDefault());
return details;
}
async function readOffered() {
const box = $('proxy-offered-list'), round = ++offeredRound;
if (proxyAdmin) return readOfferedOwner(round);
if (!accountList?.length) { box.replaceChildren(el('p', 'Add an account first. Its models show here.', 'small muted')); return; }
const source = setup.readyOrg();
const url = source ? `/api/orgs/${encodeURIComponent(source)}/bus/routes` : '/api/bus/routes';
@@ -1091,10 +1285,12 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
'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',
'account.remove': provider ? `${provider} account removed` : 'Account removed',
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`,
'models.seed': 'Model lists recorded, every model on', 'models.set': provider ? `${provider} models switched on or off` : 'Models switched on or off',
};
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}` : '';
@@ -1795,13 +1991,13 @@ if (document.documentElement.hasAttribute('data-settings-proxy-document') && !lo
document.addEventListener('mosaic-org-restarted', onRestarted);
const cleanup = () => { document.removeEventListener('mosaic-org-restarted', onRestarted); setup.stop(); clearTimeout(poll); login = null; const redirect=$('proxy-login-submit')?.elements.redirect_url, key=$('proxy-apikey')?.elements.key; if(redirect)redirect.value='';if(key)key.value=''; };
window.addEventListener('pagehide', cleanup, {once:true});
let devOwner = false, proxyAdmin = false;
let devOwner = false, proxyAdmin = false, offeredSend = false;
const NOT_SET = 'The model service is not set up on this instance.';
fetch('/api/config', { cache: 'no-store' }).then(r => r.json()).then(config => {
// F3: an organization's settings writes go with settingsSend (the org Console has no global send).
canWrite = (config.send === true || config.settingsSend === true) && config.tester !== true && config.user !== true;
devOwner = config.devOwner === true;
proxyAdmin = config.proxyAdmin === true;
proxyAdmin = config.proxyAdmin === true; offeredSend = proxyAdmin && config.proxyAdminSend === true;
applyWrite();
root.classList.toggle('solo', !proxyAdmin);
if (proxyAdmin) { root.querySelectorAll('[data-admin]').forEach(part => { part.hidden = false; }); setup.start(config); }
+3 -1
View File
@@ -351,6 +351,8 @@ 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}
/* #1873 account-remove: the owner's Remove button is a fourth column; its ask spans the row. */
#proxy-accounts li.removable{grid-template-columns:7rem minmax(0,1fr) auto auto}#proxy-accounts li>button{min-height:44px}#proxy-accounts li>.proxy-remove-ask{grid-column:1/-1;margin:4px 0 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)}
@@ -363,7 +365,7 @@ button:disabled,[aria-disabled=true]{cursor:not-allowed}
.err-line{color:var(--danger)}
.settings-content details{margin-top:var(--gap,12px)}
.settings-content details>.inside{margin-top:var(--gap,12px)}
@media(max-width:759px){.rows li{grid-template-columns:1fr}.rows li>.badge{justify-self:start}.rows li>.account-reason{grid-column:auto}.setup-step .summary,.setup-step .body{margin-left:0}.settings-form{max-width:none}.settings-form .w-num{max-width:none}}
@media(max-width:759px){.rows li,#proxy-accounts li.removable{grid-template-columns:1fr}.rows li>.badge{justify-self:start}.rows li>.account-reason{grid-column:auto}.setup-step .summary,.setup-step .body{margin-left:0}.settings-form{max-width:none}.settings-form .w-num{max-width:none}}
@media(max-width:959px) and (min-width:760px){.settings-layout{grid-template-columns:180px minmax(0,1fr);gap:24px}}
@media(max-width:759px){.settings-layout{display:block}.settings-layout>div:first-child{margin-bottom:20px}#settings-disclosure{width:100%;min-height:44px;display:flex;align-items:center;gap:12px;padding:6px 12px;border:1px solid var(--border);border-radius:var(--r);background:var(--surface);color:var(--text);text-align:left}#settings-disclosure small{color:var(--muted)}#settings-disclosure span{margin-left:auto}#settings-disclosure[aria-expanded=false]+#settings-nav{display:none}#settings-nav{padding:12px 0}.settings-content>h2{font-size:21px}.palette-grid{grid-template-columns:repeat(3,minmax(0,1fr));gap:11px}.palette-content{min-height:64px;padding:7px}.palette-tile input:checked+.palette-content{padding:6px}.settings-segments{display:flex;width:100%}.settings-segments label{flex:1;min-width:0}.settings-segments span,.settings-appearance .panel:last-child .settings-segments span{min-width:0;padding:0 4px}.settings-content .table-wrap{max-width:100%}}
.settings-content [aria-invalid=true]{border:2px solid var(--danger)!important}
@@ -0,0 +1,162 @@
// #1873 account-remove (Jason 2026-11-20): the Console's two routes for removing one model proxy
// account, over the real HTTP boundary. Only the instance owner reaches them; the remove is a
// same-origin JSON POST of exactly { accountId }; its refusals reach the page by name only.
import test from 'node:test';
import assert from 'node:assert/strict';
import { request } from 'node:http';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { startServer } from '../src/serve.mjs';
import { createDevOwner } from '../src/dev-owner.mjs';
import { consoleAccess } from '../src/access.mjs';
import { proxyAdminHTTP } from '../src/proxy-admin.mjs';
import { createSsoSession } from './helpers/sso-session.mjs';
import { REFUSALS } from '../../bus/src/refusals.mjs';
import { consoleBus } from '../src/console-bus.mjs';
const ID = 'a1b2c3d4e5f6';
const SECRET = 'remove-http-sentinel-key-0123456789';
const IMPACT = { accountId: ID, provider: 'claude', upstream: 'claude', moves: 'all', models: null, others: 0, organizations: [] };
const ROUTES = [['accounts/impact?account=' + ID, 'GET'], ['accounts/remove', 'POST', { accountId: ID }]];
function fakeAdmin() {
const calls = [];
return {
calls,
accountImpact: async (call, args) => { calls.push(['accountImpact', args]); return { ...IMPACT, read: await call('proxy.accounts', {}) ? 'ok' : 'none' }; },
accountRemove: async (call, args) => { calls.push(['accountRemove', args]); await call('proxy.account.remove', { accountId: args.accountId, confirm: true });
return { accountId: args.accountId, provider: 'claude', removed: true, receipt: { op: 'account.remove', provider: 'claude', accountId: args.accountId } }; },
};
}
const send = (port, path, { method = 'GET', headers = {}, body } = {}) => new Promise((resolve, reject) => {
const req = request({ host: '127.0.0.1', port, path, method, headers }, res => {
let text = ''; res.on('data', c => { text += c; }); res.on('end', () => resolve({ status: res.statusCode, text, json: (() => { try { return JSON.parse(text); } catch { return null; } })() }));
});
req.on('error', reject); req.end(body === undefined ? undefined : typeof body === 'string' ? body : JSON.stringify(body));
});
async function devServer(t, options = {}) {
const admin = fakeAdmin(), busCalls = [], logs = [];
const s = await startServer({ port: 0, board: 'http://127.0.0.1:9', sso: createDevOwner(), send: async () => ({}), proxyAdmin: admin,
proxyCall: async (verb, args) => { busCalls.push([verb, args]); return { accounts: [] }; }, log: line => logs.push(line), ...options });
t.after(() => new Promise(r => s.close(r)));
const port = s.address().port;
const headers = { host: `127.0.0.1:${port}`, origin: `http://127.0.0.1:${port}`, 'sec-fetch-site': 'same-origin', 'content-type': 'application/json' };
return { s, port, admin, busCalls, logs, headers };
}
const hit = (f, [path, method, body], extra = {}) => send(f.port, `/api/proxy/admin/${path}`, { method, headers: { ...f.headers, ...extra }, body });
test('the instance owner reads the impact and removes; the remove sends the confirmed bus verb with the signed-in name', async t => {
const f = await devServer(t);
const impact = await hit(f, ROUTES[0]);
assert.equal(impact.status, 200);
assert.deepEqual(impact.json, { ...IMPACT, read: 'ok' });
const removed = await hit(f, ROUTES[1]);
assert.equal(removed.status, 200);
assert.deepEqual(removed.json, { accountId: ID, provider: 'claude', removed: true, receipt: { op: 'account.remove', provider: 'claude', accountId: ID } });
assert.deepEqual(f.admin.calls, [['accountImpact', { accountId: ID }], ['accountRemove', { accountId: ID, user: 'Local operator' }]]);
assert.deepEqual(f.busCalls, [['proxy.accounts', {}], ['proxy.account.remove', { accountId: ID, confirm: true }]]);
for (const line of f.logs.filter(l => l.includes('/api/proxy/admin/'))) assert.match(line, /^\S+ (GET|POST) \/api\/proxy\/admin\/accounts\/(impact|remove) 200 user=/);
});
test('the impact read takes exactly one account key, a 12-hex id; the remove takes exactly { accountId }', async t => {
const f = await devServer(t);
for (const query of ['', '?account=', '?account=A1B2C3D4E5F6', '?account=a1b2c3d4e5f', `?account=${ID}&account=${ID}`, `?account=${ID}&x=1`, `?id=${ID}`, '?account=../etc'])
assert.deepEqual((await send(f.port, '/api/proxy/admin/accounts/impact' + query, { headers: f.headers })).json, { error: 'invalid-request' }, query);
for (const body of [{}, { accountId: ID, confirm: true }, { accountId: ID.toUpperCase() }, { accountId: 7 }, { accountId: '../x' }, [ID], 'null'])
assert.deepEqual((await hit(f, ['accounts/remove', 'POST', body])).json, { error: 'invalid-request' }, JSON.stringify(body));
assert.deepEqual((await send(f.port, '/api/proxy/admin/accounts/remove', { method: 'POST', headers: { ...f.headers, 'content-type': 'text/plain' }, body: { accountId: ID } })).json, { error: 'invalid-request' });
assert.deepEqual((await send(f.port, '/api/proxy/admin/accounts/remove', { method: 'POST', headers: f.headers, body: `{"accountId":"${ID}","pad":"${'x'.repeat(17000)}"}` })).json, { error: 'invalid-request' });
assert.deepEqual(f.admin.calls, []);
assert.deepEqual(f.busCalls, []);
});
test('the remove refuses a foreign Origin, missing or cross-site Fetch Metadata, a read-only Console and GET; the impact refuses POST', async t => {
const f = await devServer(t);
assert.equal((await hit(f, ROUTES[1], { origin: 'http://evil.example' })).status, 403);
assert.equal((await hit(f, ROUTES[1], { 'sec-fetch-site': 'cross-site' })).status, 403);
const noMeta = { ...f.headers }; delete noMeta['sec-fetch-site'];
assert.equal((await send(f.port, '/api/proxy/admin/accounts/remove', { method: 'POST', headers: noMeta, body: { accountId: ID } })).status, 403);
assert.equal((await send(f.port, '/api/proxy/admin/accounts/remove', { headers: f.headers })).status, 405);
assert.equal((await send(f.port, '/api/proxy/admin/accounts/impact?account=' + ID, { method: 'POST', headers: f.headers, body: {} })).status, 405);
const readOnly = await devServer(t, { send: null });
assert.deepEqual((await hit(readOnly, ROUTES[1])).json, { error: 'read-only' });
assert.deepEqual([...f.admin.calls, ...readOnly.admin.calls], []);
assert.deepEqual([...f.busCalls, ...readOnly.busCalls], []);
});
test('remove-write-refused and remove-unconfirmed reach the page by name with a next step; no bus transport is not-configured', async t => {
const f = await devServer(t);
for (const [code, status] of [['remove-write-refused', 400], ['remove-unconfirmed', 400], ['proxy-uncertain', 400], ['proxy-down', 503], ['proxy-admin-busy', 409]]) {
f.admin.accountRemove = async () => { throw Object.assign(new Error(`${SECRET} /home/owner/.config/x`), { code }); };
const r = await hit(f, ROUTES[1]);
assert.equal(r.status, status, code);
assert.deepEqual(r.json, { error: code });
assert.equal(r.text.includes(SECRET) || r.text.includes('/home/'), false);
assert.equal(typeof REFUSALS[code]?.nextStep, 'string', code);
}
f.admin.accountRemove = async () => { throw Object.assign(new Error(SECRET), { code: 'some-other-code' }); };
assert.deepEqual((await hit(f, ROUTES[1])).json, { error: 'outcome-unknown' });
f.admin.accountImpact = async () => { throw new Error(SECRET); };
assert.deepEqual((await hit(f, ROUTES[0])).json, { error: 'outcome-unknown' });
assert.equal(f.logs.join('\n').includes(SECRET), false);
const none = await devServer(t, { proxyCall: null });
for (const route of ROUTES) assert.deepEqual((await hit(none, route)).json, { error: 'not-configured' }, route[0]);
assert.deepEqual(none.admin.calls, []);
});
test('a granted SSO user gets human-required on both routes and nothing reaches the controller or bus', async t => {
const access = { accessVersion: 1, revision: 1,
users: [{ id: 'alice', home: 'local', subjects: [{ provider: 'writer', sub: 'alice-sub' }] }],
grants: [{ id: 'g-1', user: 'alice', business: 'demo', projects: ['*'], instances: ['*'], level: 'collaborator' }] };
const auth = await createSsoSession(t, { host: 'console.example', users: consoleAccess({ load: () => access, business: () => 'demo' }) });
const admin = fakeAdmin(), busCalls = [];
const s = await startServer({ port: 0, sso: auth.sso, send: async () => ({}), proxyAdmin: admin, proxyCall: async (verb, args) => { busCalls.push([verb, args]); return {}; }, log: () => {} });
t.after(() => new Promise(r => s.close(r)));
const port = s.address().port;
const owner = auth.writeHeaders(await auth.signIn(port));
assert.equal((await send(port, '/api/proxy/admin/accounts/impact?account=' + ID, { headers: owner })).status, 200);
const before = admin.calls.length, busBefore = busCalls.length;
const alice = auth.writeHeaders(await auth.signIn(port, { sub: 'alice-sub' }));
for (const [path, method, body] of ROUTES) {
const r = await send(port, `/api/proxy/admin/${path}`, { method, headers: alice, body });
assert.equal(r.status, 403, path);
assert.deepEqual(r.json, { error: 'human-required' }, path);
}
assert.equal(admin.calls.length, before);
assert.equal(busCalls.length, busBefore);
});
test('the tester listener gets 403 on both routes', async t => {
const { startTesterSocket } = await import('../src/tester-socket.mjs');
const { testerRequest } = await import('../../cli/src/tester-http.mjs');
const root = mkdtempSync(join(tmpdir(), 'ar-tester-'));
t.after(() => rmSync(root, { recursive: true, force: true }));
const admin = fakeAdmin();
let calls = 0;
const s = await startTesterSocket({ dataRoot: root, proxyAdmin: admin, proxyCall: async () => { calls++; return {}; }, log: () => {} });
t.after(() => new Promise(r => s.close(r)));
for (const [path, method, body] of ROUTES) assert.equal((await testerRequest({ dataRoot: root, path: `/api/proxy/admin/${path}`, method, body })).status, 403, path);
assert.equal(calls, 0);
assert.deepEqual(admin.calls, []);
});
test('a named session that is not the instance owner, such as an organization owner, gets human-required', async () => {
for (const [path, method] of [['/api/proxy/admin/accounts/impact', 'GET'], ['/api/proxy/admin/accounts/remove', 'POST']]) {
let status = 0, body = '';
const res = { writeHead: s => { status = s; }, end: b => { body = b; } };
const admin = fakeAdmin();
await proxyAdminHTTP({ method, url: path, headers: {}, headersDistinct: {} }, res, { path, admin, call: async () => ({}), named: true, tester: false, granted: false, owner: false, send: async () => ({}), origin: 'http://x', user: 'org-owner', log: () => {} });
assert.equal(status, 403, path);
assert.deepEqual(JSON.parse(body), { error: 'human-required' });
assert.deepEqual(admin.calls, []);
}
});
test('the Console owner transport lets proxy.account.remove through to the bus', async () => {
// Dropped from PROXY_VERBS, the admin route's call would throw before reaching the bus.
const seen = [];
const { proxyCall } = consoleBus(async (verb, args) => { seen.push([verb, args]); return { removed: true }; });
assert.deepEqual(await proxyCall('proxy.account.remove', { accountId: ID, confirm: true }), { removed: true });
assert.deepEqual(seen, [['proxy.account.remove', { accountId: ID, confirm: true }]]);
await assert.rejects(proxyCall('proxy.account.removeall', {}), /does not call/);
});
+164 -3
View File
@@ -20,6 +20,8 @@ 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 models-offered (A9): the owner's model lists, and one provider's on list.
['models', 'GET'], ['models/set', 'POST', { provider: 'z.ai', on: [], revision: 'r' }],
// #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.
@@ -45,6 +47,8 @@ function fakeAdmin() {
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: [] }; },
models: async args => { calls.push(['models', args]); return { revision: null, providers: [] }; },
modelsSet: async args => { calls.push(['modelsSet', args]); return { provider: args.provider, revision: 'r2', on: 0, total: 0, receipt: null }; },
};
}
const send = (port, path, { method = 'GET', headers = {}, body } = {}) => new Promise((resolve, reject) => {
@@ -66,8 +70,9 @@ async function devServer(t, options = {}) {
}
const hit = (f, route, extra = {}) => send(f.port, `/api/proxy/admin/${route[0]}`, { method: route[1], headers: { ...f.headers(), ...extra }, body: route[2] });
test('the route table is exactly the brief: status, render, fetch, init, four unit verbs, fence clear, lock clear, ride all, ride move, log, service key, history, programs off, access', () => {
assert.deepEqual(PROXY_ADMIN_PATHS, ROUTES.map(([p]) => `/api/proxy/admin/${p}`));
test('the route table is exactly the brief: status, render, fetch, init, four unit verbs, fence clear, lock clear, ride all, ride move, models, models set, log, service key, history, programs off, access', () => {
// #1873 account-remove: its two routes are tested in proxy-account-remove-http.test.mjs.
assert.deepEqual(PROXY_ADMIN_PATHS, [...ROUTES.map(([p]) => `/api/proxy/admin/${p}`), '/api/proxy/admin/accounts/impact', '/api/proxy/admin/accounts/remove']);
});
test('the dev local owner reaches every route; init and fence clear go to the bus; the page config offers setup', async t => {
@@ -76,7 +81,7 @@ test('the dev local owner reaches every route; init and fence clear go to the bu
const r = await hit(f, route);
assert.equal(r.status, route[0] === 'fetch' ? 202 : 200, route[0]);
}
assert.deepEqual(f.admin.calls.map(c => c[0]), ['status', 'render', 'fetchStart', 'init', 'unitAction', 'unitAction', 'unitAction', 'unitAction', 'fenceClear', 'initLockClear', 'rideAll', 'rideMove', 'log', 'serviceKey', 'history', 'programsOff', 'access']);
assert.deepEqual(f.admin.calls.map(c => c[0]), ['status', 'render', 'fetchStart', 'init', 'unitAction', 'unitAction', 'unitAction', 'unitAction', 'fenceClear', 'initLockClear', 'rideAll', 'rideMove', 'models', 'modelsSet', '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']);
@@ -506,3 +511,159 @@ test('A2 end to end: programs off writes one receipt, skips a down bus, and no a
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);
});
// #1873 models-offered (A9): the owner's per-provider model lists.
const MODELS = ROUTES.filter(([p]) => ['models', 'models/set'].includes(p));
test('models: a member and an organization owner get human-required on both routes, 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 MODELS) {
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('models set takes provider, an on list, revision and a true or false confirmInUse, nothing else; a refusal reaches the page as its code, in use with who', async t => {
const f = await devServer(t);
const base = { provider: 'z.ai', on: [], revision: 'a'.repeat(16) };
for (const body of [{}, { ...base, on: 'glm-5' }, { ...base, on: null }, { ...base, confirmInUse: 'yes' }, { ...base, confirmInUse: 1 }, { ...base, extra: 1 }, { ...base, harness: 'pi' }])
assert.deepEqual((await hit(f, ['models/set', 'POST', body])).json, { error: 'invalid-request' }, JSON.stringify(body));
assert.deepEqual(f.admin.calls, []);
assert.equal((await send(f.port, '/api/proxy/admin/models', { method: 'POST', headers: f.headers(), body: {} })).status, 405);
assert.equal((await send(f.port, '/api/proxy/admin/models/set', { headers: f.headers() })).status, 405);
assert.equal((await hit(f, ['models/set', 'POST', base])).status, 200);
assert.equal((await hit(f, ['models/set', 'POST', { ...base, confirmInUse: true }])).status, 200);
assert.deepEqual(f.admin.calls.map(c => c[1]), [{ ...base, confirmInUse: false, user: 'Local operator' }, { ...base, confirmInUse: true, user: 'Local operator' }]);
const usedBy = [{ model: 'glm-5', org: 'acme', instance: 'assistant', fallback: false }];
f.admin.modelsSet = async () => { throw Object.assign(new Error(`${SENTINEL} in use`), { code: 'models-in-use', usedBy, unanswered: ['bolt'] }); };
const inUse = await hit(f, ['models/set', 'POST', base]);
assert.equal(inUse.status, 409); assert.deepEqual(inUse.json, { error: 'models-in-use', usedBy, unanswered: ['bolt'] });
f.admin.modelsSet = async () => { throw Object.assign(new Error(`${SENTINEL} in use`), { code: 'models-in-use', usedBy: 'x' }); };
assert.deepEqual((await hit(f, ['models/set', 'POST', base])).json, { error: 'models-in-use', usedBy: [], unanswered: [] });
for (const [code, status] of [['models-offered-conflict', 409], ['models-invalid', 400], ['proxy-admin-busy', 409], ['proxy-down', 503], ['not-configured', 503]]) {
f.admin.modelsSet = async () => { throw Object.assign(new Error(`${SENTINEL} /home/owner/x`), { code, usedBy }); };
const r = await hit(f, ['models/set', 'POST', base]);
assert.equal(r.status, status, code); assert.deepEqual(r.json, { error: code }, code);
}
f.admin.modelsSet = async () => { throw Object.assign(new Error(SENTINEL), { code: 'proxy-file-refused' }); };
assert.deepEqual((await hit(f, ['models/set', 'POST', base])).json, { error: 'outcome-unknown' });
f.admin.models = async () => { throw new Error(SENTINEL); };
const read = await hit(f, ['models', 'GET']);
assert.equal(read.status, 503); assert.deepEqual(read.json, { error: 'outcome-unknown' });
assert.equal(f.logs.join('\n').includes(SENTINEL), false);
for (const code of ['models-offered-conflict', 'models-in-use', 'models-invalid']) assert.equal(typeof REFUSALS[code]?.nextStep, 'string', code);
});
// The read may write the first seed, so it asks for one only when the request passes the write checks.
test('models read: the seed is asked for only by a sending Console on a same-origin request', async t => {
const f = await devServer(t);
const seeds = async (headers, server = f) => {
server.admin.calls.length = 0;
const all = { ...server.headers(), ...headers };
for (const [k, v] of Object.entries(all)) if (v === undefined) delete all[k];
const r = await send(server.port, '/api/proxy/admin/models', { headers: all });
assert.equal(r.status, 200);
return server.admin.calls[0][1].seed;
};
assert.equal(await seeds({}), true, 'same origin, with the Origin header');
assert.equal(await seeds({ origin: undefined }), true, 'a same-origin GET fetch sends no Origin');
assert.equal(await seeds({ 'sec-fetch-site': 'cross-site' }), false, 'cross-site');
assert.equal(await seeds({ 'sec-fetch-site': 'same-site' }), false, 'same-site');
assert.equal(await seeds({ 'sec-fetch-site': undefined }), false, 'no fetch metadata');
f.admin.calls.length = 0;
assert.equal((await send(f.port, '/api/proxy/admin/models', { headers: { ...f.headers(), origin: 'http://evil.example' } })).status, 403, 'a foreign Origin never reaches the read');
assert.deepEqual(f.admin.calls, []);
const readOnly = await devServer(t, { send: null });
assert.equal(await seeds({}, readOnly), false, 'a read-only Console');
});
// The real controller behind the real routes, with fake organizations whose route answers carry a planted key.
test('models end to end: the first read seeds every present model on and seen; a stale revision, a model in use and an organization that does not answer refuse; confirm saves; a new model reads New and off; receipts and the file hold labels only', async t => {
const { createProxyAdmin } = await import('../../model-proxy/src/admin-control.mjs');
const { mkdirSync, statSync, existsSync } = await import('node:fs');
const root = mkdtempSync(join(tmpdir(), 'pa-mo-'));
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 planted = `msk-k1a2-Ab3_dE5-fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7dE`;
const catalogue = [['zai', 'glm-5'], ['zai', 'glm-4.6'], ['codex', 'gpt-5']];
const entry = ([upstream, model]) => ({ key: `${upstream}-${model}`.replace(/\./g, '-'), module: 'cliproxy', upstream, model, available: true, reason: null, harnesses: ['claude-code', 'pi'] });
const org = (id, { down = false, cliproxy = true, instances = {} } = {}) => ({
id, fixture: false, cliproxy, changes: '', down,
file: async () => ({ document: { roles: {} }, revision: 'c'.repeat(64) }),
async call(verb) {
if (this.down) throw Object.assign(new Error(planted), { code: 'outcome-unknown' });
if (verb === 'routes') return { business: id, revision: 'c'.repeat(64), note: planted, instances,
providerModels: { glm5: { module: 'cliproxy', upstream: 'zai', model: 'glm-5', provider: 'zai' }, gpt5: { module: 'cliproxy', upstream: 'codex', model: 'gpt-5', provider: 'codex' } },
catalogue: { status: 'ready', models: catalogue.map(entry), notices: [] } };
throw Object.assign(new Error(planted), { code: 'invalid-request' });
},
});
const orgs = [org('acme', { instances: { assistant: { model: 'glm5', fallback: { model: 'gpt5' } }, idle: { model: 'none' } } }), org('bolt', { down: true }), org('none', { cliproxy: false, instances: { assistant: { model: 'glm5' } } })];
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: () => [], rootAgents: () => ({ state: 'absent', agents: {}, assistant: null }) });
const f = await devServer(t, { proxyAdmin: admin });
const file = join(dataRoot, 'state', 'models-offered.json');
const crossSite = await hit(f, ['models', 'GET'], { 'sec-fetch-site': 'cross-site' });
assert.equal(crossSite.status, 200);
assert.equal(existsSync(file), false, 'a cross-site read seeds nothing');
const waiting = await hit(f, ['models', 'GET']);
assert.deepEqual(waiting.json.unanswered, ['bolt'], 'bolt\'s bus is down, so its usage is unknown');
assert.equal(existsSync(file), false, 'no seed while an organization\'s usage is unknown');
orgs[1].down = false;
const first = await hit(f, ['models', 'GET']);
assert.equal(first.status, 200);
assert.deepEqual(first.json.unanswered, []);
orgs[1].down = true;
assert.match(first.json.revision, /^[a-f0-9]{16}$/);
assert.deepEqual(first.json.providers.map(p => [p.provider, p.on, p.total]), [['z.ai', 2, 2], ['codex', 1, 1]]);
assert.deepEqual(first.json.providers[0].models, [
{ id: 'glm-5', label: 'glm-5', on: true, isNew: false, usedBy: [{ org: 'acme', instance: 'assistant', fallback: false }] },
{ id: 'glm-4.6', label: 'glm-4.6', on: true, isNew: false, usedBy: [] }]);
assert.deepEqual(first.json.providers[1].models[0].usedBy, [{ org: 'acme', instance: 'assistant', fallback: true }]);
assert.deepEqual(JSON.parse(readFileSync(file, 'utf8')), { version: 1, providers: { 'z.ai': { on: ['glm-5', 'glm-4.6'], seen: ['glm-5', 'glm-4.6'] }, codex: { on: ['gpt-5'], seen: ['gpt-5'] } } });
assert.equal(statSync(file).mode & 0o777, 0o600);
// A second read writes nothing more.
assert.equal((await hit(f, ['models', 'GET'])).json.revision, first.json.revision);
const stale = await hit(f, ['models/set', 'POST', { provider: 'z.ai', on: ['glm-4.6'], revision: 'f'.repeat(16) }]);
assert.equal(stale.status, 409); assert.deepEqual(stale.json, { error: 'models-offered-conflict' });
const inUse = await hit(f, ['models/set', 'POST', { provider: 'z.ai', on: ['glm-4.6'], revision: first.json.revision }]);
assert.equal(inUse.status, 409);
assert.deepEqual(inUse.json, { error: 'models-in-use', usedBy: [{ model: 'glm-5', org: 'acme', instance: 'assistant', fallback: false }], unanswered: ['bolt'] });
const fallback = await hit(f, ['models/set', 'POST', { provider: 'codex', on: [], revision: first.json.revision, confirmInUse: false }]);
assert.deepEqual(fallback.json, { error: 'models-in-use', usedBy: [{ model: 'gpt-5', org: 'acme', instance: 'assistant', fallback: true }], unanswered: ['bolt'] }, 'a fallback model counts as in use');
const quiet = await hit(f, ['models/set', 'POST', { provider: 'z.ai', on: ['glm-5'], revision: first.json.revision }]);
assert.deepEqual(quiet.json, { error: 'models-in-use', usedBy: [], unanswered: ['bolt'] }, 'glm-4.6 has no known user, but bolt did not answer');
const unknown = await hit(f, ['models/set', 'POST', { provider: 'z.ai', on: ['glm-4.6', 'made-up'], revision: first.json.revision }]);
assert.equal(unknown.status, 400); assert.deepEqual(unknown.json, { error: 'models-invalid' }, 'a model never listed cannot be switched on');
assert.deepEqual((await hit(f, ['models/set', 'POST', { provider: 'claude', on: [], revision: first.json.revision }])).json, { error: 'models-invalid' }, 'a provider no organization lists');
assert.deepEqual(JSON.parse(readFileSync(file, 'utf8')).providers['z.ai'].on, ['glm-5', 'glm-4.6'], 'nothing refused was written');
const saved = await hit(f, ['models/set', 'POST', { provider: 'z.ai', on: ['glm-4.6'], revision: first.json.revision, confirmInUse: true }]);
assert.equal(saved.status, 200);
assert.equal(saved.json.on, 1); assert.equal(saved.json.total, 2);
assert.notEqual(saved.json.revision, first.json.revision);
assert.deepEqual(saved.json.receipt, { time: new Date(0).toISOString(), op: 'models.set', provider: 'z.ai', on: [], off: ['glm-5'], inUse: [{ model: 'glm-5', org: 'acme', instance: 'assistant' }], unanswered: ['bolt'], user: 'Local operator' });
assert.deepEqual((await hit(f, ['models/set', 'POST', { provider: 'z.ai', on: ['glm-5', 'glm-4.6'], revision: first.json.revision }])).json, { error: 'models-offered-conflict' }, 'the old revision is stale now');
// A model the provider adds later reads New and off.
catalogue.push(['zai', 'glm-6']);
const later = await hit(f, ['models', 'GET']);
assert.equal(later.json.revision, saved.json.revision, 'a known provider is not seeded again');
assert.deepEqual(later.json.providers[0].models.map(m => [m.id, m.on, m.isNew]), [['glm-5', false, false], ['glm-4.6', true, false], ['glm-6', false, true]]);
const doc = JSON.parse(readFileSync(file, 'utf8'));
assert.deepEqual(Object.keys(doc), ['version', 'providers']);
for (const e of Object.values(doc.providers)) assert.deepEqual(Object.keys(e), ['on', 'seen']);
const lines = readFileSync(join(dataRoot, 'state', 'proxy-admin.jsonl'), 'utf8').trim().split('\n').map(l => JSON.parse(l));
assert.deepEqual(lines.map(l => l.op), ['models.seed', 'models.set']);
assert.deepEqual(lines[0].providers, [{ provider: 'z.ai', on: 2 }, { provider: 'codex', on: 1 }]);
const history = await hit(f, ['history', 'GET']);
assert.deepEqual(history.json.entries.map(e => e.op), ['models.seed', 'models.set']);
const all = [first.text, stale.text, inUse.text, saved.text, later.text, history.text, readFileSync(file, 'utf8'), JSON.stringify(lines), f.logs.join('\n')].join('\n');
for (const bit of [planted, 'msk-', root, '/home/']) assert.equal(all.includes(bit), false, bit);
});
@@ -21,10 +21,22 @@ test('a3: no provider id, sign-in host or key base URL is written out in client.
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']) {
for (const file of ['../../model-proxy/src/client.mjs', '../../bus/src/proxy-page.mjs', '../src/public/display.js', '../../model-proxy/src/models-offered.mjs']) {
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`);
}
});
// #1873 models-offered (A9): the offered file holds provider and model ids only. A harness, key or
// label field refuses the whole file, so the file can switch a model off but never says where it runs.
test('models-offered: the file takes version, providers, on and seen only; anything else reads as damaged', async () => {
const { parseOffered } = await import('../../model-proxy/src/models-offered.mjs');
const good = { version: 1, providers: { 'z.ai': { on: ['glm-5'], seen: ['glm-5'] } } };
assert.deepEqual(parseOffered(good), good);
for (const bad of [{ ...good, harness: 'pi' }, { ...good, version: 2 }, { version: 1, providers: { 'z.ai': { on: ['glm-5'], seen: ['glm-5'], harnesses: ['pi'] } } },
{ version: 1, providers: { 'z.ai': { on: ['glm-5'], seen: ['glm-5'], key: 'x' } } }, { version: 1, providers: { 'z.ai': { on: ['glm-5'], seen: [] } } },
{ version: 1, providers: { 'z.ai': { on: ['glm-5', 'glm-5'], seen: ['glm-5'] } } }, { version: 1, providers: { '../x': { on: [], seen: [] } } }, { version: 1, providers: [] }])
assert.equal(parseOffered(bad), null, JSON.stringify(bad));
});
+2
View File
@@ -167,6 +167,8 @@ test('proxy pages require sign-in and named host before redirect or content', as
test('proxy refuses route and body mismatch, unavailable transport, and bus refusal', async t => {
const { s, calls } = await setup(t);
assert.equal((await call(s, '/api/proxy/unknown')).status, 404);
// #1873 account-remove: only the instance owner's /api/proxy/admin/accounts/remove removes an account.
for (const path of ['account/remove', 'accounts/remove', 'remove']) assert.equal((await call(s, '/api/proxy/' + path, 'POST', { accountId: 'a'.repeat(12), confirm: true })).status, 404, path);
assert.equal((await call(s, '/api/proxy/accounts', 'POST', {})).status, 405);
assert.equal((await call(s, '/api/proxy/apikey/add', 'POST', { provider: 'z.ai', key: 'fixture', extra: true })).status, 400);
assert.deepEqual(calls, []);
@@ -0,0 +1,157 @@
// #1873 account-remove (Jason 2026-11-20; settings-backend DESIGN section 1, settings-walk DESIGN 3.2):
// the instance owner's Remove on Model setup's accounts list. Remove asks in place and names what the
// impact read says leaves; Keep closes the ask and gives focus back; Remove posts exactly
// { accountId } and reads the list again; a refusal is named and the account stays. Anyone else sees
// no Remove, only a note saying who removes. Every API answer is explicit (w15-fixture.mjs).
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { publicProviders } from '../../plugins/model-provider/cliproxy/index.mjs';
import { w15Fixture } from './w15-fixture.mjs';
const running = {
release: { fetched: true, verified: true, version: '8.0.42', panelVersion: '1.26.7' }, init: { initialized: true, partial: false, settings: { port: 18432 }, files: [] },
unit: { unit: 'mosaic-proxy.service', installed: true, active: 'active', pid: 4242, port: 18432, listening: true }, fence: { set: false },
initLock: { present: false, clearable: false, blockers: [] }, fetch: { state: 'idle' }, restart: null, busy: null,
organizations: [{ id: 'acme', proxy: 'shared', catalogue: null }], ownProxy: { available: false, state: 'not built', reason: 'One proxy per instance.' }, next: ['accounts'],
providers: publicProviders(),
};
const owner = { send: true, devOwner: true, proxyAdmin: true, proxyAdminSend: true };
const A = 'a1b2c3d4e5f6', B = 'b1b2c3d4e5f6';
const LIST = [
{ accountId: A, provider: 'claude', displayName: 'Claude work', status: 'active', priority: 1 },
{ accountId: B, provider: 'z.ai', displayName: 'z.ai key', status: 'active', priority: 0, models: ['glm-4.6'] },
];
const accounts = list => ({ 'GET /api/proxy/accounts': { body: { accounts: list } }, 'GET /api/proxy/keys': { body: { keys: [] } } });
const impactKey = id => `GET /api/proxy/admin/accounts/impact?account=${id}`;
const IMPACT_ALL = { accountId: A, provider: 'claude', upstream: 'claude', moves: 'all', models: null, others: 0,
organizations: [{ id: 'acme', readable: true, uses: [{ instance: 'coder', model: 'claude / claude-fixture', fallback: false }] }, { id: 'north', readable: false }] };
const askText = b => b.evaluate('document.querySelector("#proxy-accounts .proxy-remove-ask")?.textContent.replace(/\\s+/g, " ").trim() ?? null');
const writes = b => b.evaluate('window.__writes.map(w => [w.method, w.path, w.body])');
async function open(t, { config = owner, list = LIST, extra = {}, width = 1440 } = {}) {
const { f, b, wait } = await w15Fixture(t, { scenario: { '/api/config': config, '/api/proxy/admin/status': running, ...accounts(list), '/api/triage/route': { route: null }, ...extra } });
await b.viewport(width, width < 760 ? 844 : 900);
await b.navigate(f.base + '/#/settings/model-setup?step=accounts');
await wait(`document.querySelectorAll("#proxy-accounts li[data-account]").length === ${list.length}`);
return { f, b, wait };
}
test('account-remove: Remove asks in place with what leaves and who uses it; Keep closes the ask and gives focus back', { timeout: 90000 }, async t => {
const { b, wait } = await open(t, { extra: { [impactKey(A)]: { body: IMPACT_ALL } } });
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("#proxy-accounts button[data-remove]")].map(x => [x.textContent, x.getAttribute("aria-label")])'),
[['Remove', 'Remove Claude work'], ['Remove', 'Remove z.ai key']]);
assert.equal(await b.evaluate('document.querySelector("#proxy-remove-note").hidden'), true, 'the owner gets no note');
await b.evaluate(`document.querySelector('[data-remove="${A}"]').click(); true`);
await wait('document.activeElement?.id === "proxy-remove-yes"');
assert.equal(await askText(b), 'Remove Claude? Every Claude model leaves your organizations. acme\'s coder uses claude / claude-fixture. Pick another model on that organization\'s Models page. '
+ 'Mosaic could not read north\'s models. Mosaic deletes your Claude sign-in from the model service. To use Claude again you sign in again.Remove ClaudeKeep Claude');
assert.equal(await b.evaluate(`document.querySelector('[data-account="${A}"] .proxy-remove-ask').getAttribute("role")`), 'group');
assert.equal(await b.evaluate('document.querySelector("#proxy-remove-yes").classList.contains("danger")'), true);
assert.equal(await b.evaluate('[...document.querySelectorAll("#proxy-accounts button[data-remove]")].every(x => x.disabled)'), true, 'one ask at a time');
assert.deepEqual(await writes(b), [], 'asking writes nothing');
await b.evaluate('document.querySelector("#proxy-remove-no").click(); true');
await wait(`!document.querySelector(".proxy-remove-ask") && document.activeElement === document.querySelector('[data-remove="${A}"]')`);
assert.deepEqual(await writes(b), []);
});
test('account-remove: the ask says only what the read holds: some models, nothing, unknown, unreadable organizations, no read', { timeout: 90000 }, async t => {
const zaiSome = { accountId: B, provider: 'z.ai', upstream: 'zai', moves: 'some', models: ['glm-4.6'], others: 0, organizations: [] };
const cases = [
[B, zaiSome, 'Remove z.ai? glm-4.6 leaves your organizations: no other account offers it. No organization uses your models. Mosaic deletes your z.ai key from the model service. To use z.ai again you add a key again.'],
[B, { ...zaiSome, organizations: [{ id: 'acme', readable: true, uses: [] }] }, 'Remove z.ai? glm-4.6 leaves your organizations: no other account offers it. No agent uses them.'],
[A, { ...IMPACT_ALL, organizations: null }, 'Remove Claude? Every Claude model leaves your organizations. Mosaic could not read your organizations, so it can\'t say which agents use them.'],
[A, { ...IMPACT_ALL, moves: 'unknown', others: 1, organizations: [] }, 'Remove Claude? Another Claude account stays and may serve its models. Mosaic can\'t tell which models only this account serves.'],
[A, { ...IMPACT_ALL, moves: 'none', models: [], organizations: [] }, 'Remove Claude? This account serves no model now, so no model leaves.'],
[A, null, 'Remove Claude? Mosaic could not read what this account serves, so it can\'t say which models leave.'],
];
for (const [id, impact, start] of cases) {
const { b, wait } = await open(t, { extra: impact ? { [impactKey(id)]: { body: impact } } : { [impactKey(id)]: { status: 503, body: { error: 'proxy-down' } } } });
await b.evaluate(`document.querySelector('[data-remove="${id}"]').click(); true`);
await wait('!!document.querySelector("#proxy-remove-yes")');
const text = await askText(b);
assert.ok(text.startsWith(start), `${text}\n should start\n${start}`);
assert.doesNotMatch(text, /default model|moves to/, 'the ask never says where an agent goes next');
await b.close();
}
});
test('account-remove: Remove posts exactly { accountId }, reads the list again and says it is removed', { timeout: 90000 }, async t => {
const { b, wait } = await open(t, { extra: { [impactKey(A)]: { body: IMPACT_ALL },
'POST /api/proxy/admin/accounts/remove': { body: { accountId: A, provider: 'claude', removed: true, receipt: { op: 'account.remove', provider: 'claude', accountId: A } } } } });
await b.evaluate(`document.querySelector('[data-remove="${A}"]').click(); true`);
await wait('!!document.querySelector("#proxy-remove-yes")');
// The host would now list one account; the page reads it after the remove.
await b.evaluate(`window.__scenario['GET /api/proxy/accounts'] = { body: { accounts: ${JSON.stringify(LIST.slice(1))} } }; document.querySelector("#proxy-remove-yes").click(); true`);
await wait(`document.querySelectorAll("#proxy-accounts li[data-account]").length === 1 && !document.querySelector('[data-account="${A}"]')`);
assert.deepEqual(await writes(b), [['POST', '/api/proxy/admin/accounts/remove', JSON.stringify({ accountId: A })]]);
assert.equal(await b.evaluate('document.querySelector("#proxy-message").textContent'), 'Claude account removed.');
assert.equal(await b.evaluate('!!document.querySelector(".proxy-remove-ask")'), false);
assert.deepEqual(await b.evaluate('window.__unexpected'), []);
});
test('account-remove: remove-write-refused and remove-unconfirmed are named in the ask and the account stays', { timeout: 90000 }, async t => {
// No refusal says nothing was removed: a refused or unanswered write may still have taken effect.
for (const [code, words] of [['remove-write-refused', 'The model service refused to remove the account. Check the list before trying again.'],
['remove-unconfirmed', 'Mosaic asked the model service to remove the account, but it is still listed. Read the accounts again before trying again.'],
['proxy-uncertain', 'Mosaic could not confirm the remove: the model service is between writes. Check the service and the list before trying again.'],
['proxy-down', 'The model service stopped answering, so Mosaic could not confirm the remove. Check the service and the list before trying again.'],
['some-new-code', 'The remove could not be confirmed. Nothing was retried. Read the accounts again before trying again.']]) {
const { b, wait } = await open(t, { extra: { [impactKey(A)]: { body: IMPACT_ALL }, 'POST /api/proxy/admin/accounts/remove': { status: 400, body: { error: code } } } });
await b.evaluate(`document.querySelector('[data-remove="${A}"]').click(); true`);
await wait('!!document.querySelector("#proxy-remove-yes")');
await b.evaluate('document.querySelector("#proxy-remove-yes").click(); true');
await wait('!!document.querySelector(".proxy-remove-ask .banner.err")');
assert.equal(await b.evaluate('document.querySelector(".proxy-remove-ask .banner.err").textContent'), words, code);
assert.equal(await b.evaluate(`!!document.querySelector('[data-account="${A}"]')`), true, `${code}: the account stays`);
assert.equal(await b.evaluate('document.querySelector("#proxy-remove-yes").disabled'), false, `${code}: the owner may try again`);
assert.equal((await writes(b)).length, 1, `${code}: nothing is retried by itself`);
await b.close();
}
});
test('account-remove: after an unconfirmed remove the page reads the list again and shows the account gone when the service no longer lists it', { timeout: 90000 }, async t => {
for (const [code, status] of [['proxy-uncertain', 400], ['proxy-down', 503], ['remove-write-refused', 400]]) {
const { b, wait } = await open(t, { extra: { [impactKey(A)]: { body: IMPACT_ALL }, 'POST /api/proxy/admin/accounts/remove': { status, body: { error: code } } } });
await b.evaluate(`document.querySelector('[data-remove="${A}"]').click(); true`);
await wait('!!document.querySelector("#proxy-remove-yes")');
// The DELETE took effect although the answer says otherwise: the next read no longer lists A.
await b.evaluate(`window.__scenario['GET /api/proxy/accounts'] = { body: { accounts: ${JSON.stringify(LIST.slice(1))} } }; document.querySelector("#proxy-remove-yes").click(); true`);
await wait(`!document.querySelector('[data-account="${A}"]') && document.querySelectorAll("#proxy-accounts li[data-account]").length === 1`);
assert.equal(await b.evaluate('document.querySelector("#proxy-message").textContent'), 'Claude account is no longer listed.', code);
assert.equal(await b.evaluate('!!document.querySelector(".proxy-remove-ask")'), false, code);
assert.equal((await writes(b)).length, 1, `${code}: nothing is retried by itself`);
await b.close();
}
});
test('account-remove: a Console that is not the instance owner\'s, or is read-only, shows no Remove; a non-owner sees who removes', { timeout: 90000 }, async t => {
for (const [label, config, note] of [['organization owner', { send: true }, false], ['read-only owner', { ...owner, send: false, proxyAdminSend: false }, true]]) {
const { b } = await open(t, { config });
assert.equal(await b.evaluate('document.querySelectorAll("#proxy-accounts button[data-remove]").length'), 0, label);
assert.equal(await b.evaluate('document.querySelector("#proxy-remove-note").hidden'), note, label);
if (!note) assert.equal(await b.evaluate('document.querySelector("#proxy-remove-note").textContent'), 'Only the instance owner removes an account, on this instance\'s Model setup page.');
assert.equal(await b.evaluate('[...document.querySelectorAll("#proxy-accounts li[data-account]")].every(li => li.children.length === 3)'), true, label);
await b.close();
}
});
test('account-remove: at phone width the ask fills the row, its buttons are 44px tall and nothing scrolls sideways', { timeout: 90000 }, async t => {
const { b, wait } = await open(t, { width: 390, extra: { [impactKey(A)]: { body: IMPACT_ALL } } });
assert.equal(await b.evaluate('[...document.querySelectorAll("#proxy-accounts button[data-remove]")].every(x => x.getBoundingClientRect().height >= 44)'), true);
await b.evaluate(`document.querySelector('[data-remove="${A}"]').click(); true`);
await wait('!!document.querySelector("#proxy-remove-yes")');
assert.deepEqual(await b.evaluate(`(() => { const li = document.querySelector('[data-account="${A}"]').getBoundingClientRect(), ask = document.querySelector(".proxy-remove-ask").getBoundingClientRect();
return [Math.abs(ask.left - li.left) <= 1, Math.abs(ask.right - li.right) <= 1]; })()`), [true, true]);
assert.equal(await b.evaluate('[...document.querySelectorAll(".proxy-remove-ask button")].every(x => x.checkVisibility() && x.getBoundingClientRect().height >= 44 && x.getBoundingClientRect().right <= document.documentElement.clientWidth)'), true);
assert.equal(await b.evaluate('document.documentElement.scrollWidth <= document.documentElement.clientWidth'), true);
});
test('account-remove: the setup history names a removed account by its provider, and an unknown provider as an account', { timeout: 90000 }, async t => {
const T = '2026-11-20T12:00:00.000Z';
const { b, wait } = await open(t, { extra: { '/api/proxy/admin/history?limit=25': { entries: [{ op: 'account.remove', time: T, provider: 'claude', accountId: A }, { op: 'account.remove', time: T, provider: 'not-a-provider', accountId: B }], next: null },
'/api/proxy/admin/service-key': { madeAt: T, renewedAt: null, renewal: 'not-built', problem: null } } });
await b.evaluate('document.querySelector("#proxy-setup-tech").open = true; true');
await wait('document.querySelectorAll("#proxy-setup-history li").length === 2');
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("#proxy-setup-history li")].map(li => li.textContent.split(" · ")[0])').then(x => x.sort()), ['Account removed', 'Claude account removed']);
assert.equal(await b.evaluate(`document.querySelector("#proxy-setup-history").textContent.includes("${A}")`), false, 'the account id is not shown');
});
@@ -0,0 +1,264 @@
// #1873 models-offered (A9; settings-backend DESIGN.md section 2, settings-walk DESIGN.md 3.3, mockup
// models-offered): Model setup, Accounts, Models offered. The instance owner's page reads
// GET /api/proxy/admin/models and shows one list per provider with a count, New and In use notes, a
// search past eight, and a Save that stays disabled until a box changes. Save posts one provider's on
// list with the read's revision (POST /api/proxy/admin/models/set). A model in use that a save would
// switch off is refused with who uses it, and the page asks in place before it sends confirmInUse. A
// page without proxyAdmin sends no /api/proxy/admin/ request; a read-only owner page changes nothing.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { fileURLToPath } from 'node:url';
import { startServer } from '../src/serve.mjs';
import { createDevOwner } from '../src/dev-owner.mjs';
import { publicProviders } from '../../plugins/model-provider/cliproxy/index.mjs';
import { browser } from './browser.mjs';
import { close } from './fixture.mjs';
const LIST = [
{ id: 'acme', label: 'Acme Studio', theme: 'cobalt', busHealthy: true },
{ id: 'lab', label: 'Lab Bench', theme: 'cobalt', busHealthy: true },
];
const ROUTES = {
business: 'acme', revision: 'a'.repeat(64), harnesses: ['pi'], models: ['fixture-deterministic'],
providerModels: { 'fixture-deterministic': { module: 'fixture', upstream: 'fixture', model: 'fixture-deterministic' } },
levels: { pi: ['off', 'low'] }, instances: {}, catalogue: { status: 'ready', observedAt: new Date().toISOString(), models: [], notices: [] },
};
async function orgFixture() {
const { checkRootAgents } = await import('../../organization/src/index.mjs');
const rolesDir = fileURLToPath(new URL('../../../roles', import.meta.url));
const root = { state: 'ok', ...checkRootAgents({ agentsVersion: 1, agents: {} }, { rolesDir }) };
const empty = () => Promise.resolve([]);
const member = org => ({
id: org.id, doc: { appearance: { theme: org.theme }, roles: { chief: { definition: 'chief-of-staff' } }, projects: {} },
status: async () => ({ healthy: true, pid: 4242, boot: null }),
options: () => ({ bus: { inbox: empty, agents: empty, 'thread.claims': empty, tasks: empty, usage: () => Promise.resolve({}), limits: () => Promise.resolve({}), decisions: empty }, chat: { threads: empty, projects: empty } }),
});
const organizations = {
list: async () => LIST,
members: async () => LIST.map(member),
resolve: async id => {
const org = LIST.find(o => o.id === id); if (!org) return null;
return {
busHealthy: true,
reads: { business: async () => ({ business: { id, human: 'jason', projects: [], appearance: { theme: org.theme } } }), settings: async () => ({ business: id, release: 'fixture', notifier: {} }), queue: async () => ({ rows: [] }), busStatus: async () => ({}) },
bus: { messages: async () => ({ messages: [], unread: 0, total: 0 }), agents: empty, 'thread.claims': empty, inbox: empty, tasks: empty, usage: async () => ({}), routes: async () => ({ ...ROUTES, business: id }), instances: async () => ({ business: id, revision: 'a'.repeat(64), launch: true, noHire: [], retired: [], definitions: {}, instances: {} }) },
chat: { projects: empty, threads: empty },
};
},
};
return { organizations, agentLevels: { loadRoot: () => root, rolesDir, ctx: { configDir: '/cfg', dataRoot: '/data', instance: 'test' } } };
}
const SERVICE_UP = { next: ['accounts'], init: { initialized: true, partial: false, problem: null, settings: { port: 18432 }, files: [] },
release: { fetched: true, verified: true, version: '8.0.42', panelVersion: '1.26.7' },
unit: { unit: 'mosaic-proxy.service', installed: true, active: 'active', port: 18432, listening: true, healthy: true }, fence: { set: false }, initLock: { present: false, blockers: [] }, fetch: { state: 'idle' },
organizations: [{ id: 'acme', proxy: 'shared', catalogue: { status: 'ready', models: 12 } }], ride: [{ id: 'acme', state: 'already' }], providers: publicProviders() };
const ACCOUNTS = [{ accountId: 'a1', provider: 'claude', displayName: 'Claude work', status: 'active', priority: 1 }, { accountId: 'a2', provider: 'z.ai', displayName: 'z.ai key', status: 'active', priority: 0 }];
// The read's names and counts only: the page holds no model or provider list of its own.
const m = (id, on, extra = {}) => ({ id, label: id, on, isNew: false, usedBy: [], ...extra });
const VIEW = { revision: 'r1', providers: [
{ provider: 'claude', label: 'Claude', on: 2, total: 3, models: [m('claude-opus-5-5', true), m('claude-sonnet-5-5', true, { usedBy: [{ org: 'acme', instance: 'assistant', fallback: false }, { org: 'lab', instance: 'chief', fallback: true }] }), m('claude-next-6', false, { isNew: true })] },
{ provider: 'z.ai', label: 'z.ai', on: 9, total: 9, models: Array.from({ length: 9 }, (_, i) => m(`glm-5.${i}`, true)) },
] };
const SAVED = { revision: 'r2', providers: [{ ...VIEW.providers[0], on: 1, models: VIEW.providers[0].models.map(x => x.id === 'claude-opus-5-5' ? { ...x, on: false } : x) }, VIEW.providers[1]] };
// The owner's flags, the status, the accounts, and the admin reads and writes by name (a list: each
// call takes the next one and the last one stays). window.__admin records every /api/proxy/admin/
// request and window.__writes every write's body.
const stub = ({ owner = true, send = owner, admin = {} } = {}) => `(() => {
const real = window.fetch.bind(window);
window.__mo = ${JSON.stringify({ owner, send, admin, service: SERVICE_UP, accounts: ACCOUNTS })}; window.__admin = []; window.__writes = [];
const json = (body, status = 200) => new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } });
const next = list => Array.isArray(list) ? (list.length > 1 ? list.shift() : list[0]) : list;
window.fetch = async (input, init = {}) => {
const url = new URL(typeof input === 'string' ? input : input.url, location.href), path = url.pathname, method = (init.method || 'GET').toUpperCase();
const at = path.indexOf('/proxy/admin/');
if (at >= 0) {
const name = path.slice(at + 13) + url.search; window.__admin.push(method + ' ' + name);
if (method !== 'GET') window.__writes.push({ path: name, body: init.body ? JSON.parse(init.body) : null });
if (name === 'status') return json(__mo.service);
const a = next(__mo.admin[method === 'GET' ? name : method + ' ' + name]);
if (!a) return json({ error: 'fixture-unexpected-api' }, 403);
return a.status ? json(a.body, a.status) : json(a);
}
if (method !== 'GET' && /\\/proxy\\//.test(path)) { window.__writes.push({ path, body: null }); return json({ error: 'stub' }, 500); }
if (method !== 'GET') return real(input, init);
if (/\\/api\\/(orgs\\/[^/]+\\/)?config$/.test(path)) { const c = await (await real(input, init)).json(); return json({ ...c, orgSetup: true, ...(__mo.owner ? { proxyAdmin: true, proxyAdminSend: __mo.send } : {}) }); }
if (path.endsWith('/proxy/accounts')) return json({ accounts: __mo.accounts, providers: ${JSON.stringify(publicProviders())} });
if (path.endsWith('/proxy/keys')) return json({ keys: [] });
if (path.endsWith('/triage/route')) return json({ route: null });
return real(input, init);
};
})();`;
async function setup(t, { viewport = [1440, 900], ...options } = {}) {
const { organizations, agentLevels } = await orgFixture();
const server = await startServer({ port: 0, sso: createDevOwner(), organizations, agentLevels, triage: null, send: async () => ({}), log: () => {} });
t.after(() => close(server));
const b = await browser({ machine: false }); t.after(() => b.close());
await b.viewport(...viewport);
await b.call('Page.addScriptToEvaluateOnNewDocument', { source: stub(options) });
await b.navigate(`http://127.0.0.1:${server.address().port}/#/settings/model-setup?step=accounts`);
const wait = expression => b.evaluate(`(async()=>{for(let i=0;i<250;i++){if(${expression})return true;await new Promise(r=>setTimeout(r,30))}throw Error("wait failed: "+${JSON.stringify(expression)}+" HASH "+location.hash)})()`);
const click = sel => b.evaluate(`(() => { const el = document.querySelector(${JSON.stringify(sel)}); if (!el) throw Error("no ${sel.replace(/"/g, '')}"); el.click(); return true; })()`);
return { b, wait, click, admin: () => b.evaluate('window.__admin'), writes: () => b.evaluate('window.__writes') };
}
const LISTED = 'document.querySelectorAll("#proxy-accounts li[data-account]").length === 2';
const CLAUDE = '#proxy-offered-list details[data-offered=claude]';
const ZAI = '#proxy-offered-list details[data-offered="z.ai"]';
async function openOffered(b, wait) {
await wait(LISTED);
await b.evaluate('document.querySelector("#proxy-offered").open = true; true');
await wait(`document.querySelector(${JSON.stringify(CLAUDE)})`);
await b.evaluate(`document.querySelector(${JSON.stringify(CLAUDE)}).open = true; true`);
}
const rows = (b, sel) => b.evaluate(`[...document.querySelectorAll(${JSON.stringify(sel + ' li')})].map(li => ({ id: li.dataset.model, on: li.querySelector("input").checked, note: li.querySelector(".small").textContent }))`);
const toggle = (b, id) => b.evaluate(`(() => { const c = document.querySelector('${CLAUDE} input[value="${id}"]'); c.click(); return c.checked; })()`);
const save = sel => `document.querySelector(${JSON.stringify(sel + ' button[data-offered-save]')})`;
test('models-offered page: the owner sees one list per provider with its count, New and In use notes naming who, a search past eight, and a dimmed Save; reading writes nothing', { timeout: 90000 }, async t => {
const { b, wait, admin } = await setup(t, { admin: { models: VIEW } });
await openOffered(b, wait);
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("#proxy-offered-list details[data-offered] > summary")].map(s => s.textContent)'), ['Claude models offered: 2 of 3 on', 'z.ai models offered: 9 of 9 on']);
assert.equal(await b.evaluate(`document.querySelector(${JSON.stringify(CLAUDE + ' legend')}).textContent`), 'Claude models');
assert.match(await b.evaluate(`document.querySelector(${JSON.stringify(CLAUDE + ' .help')}).textContent`), /offered nowhere: not to any organization, agent or Triage\. A model Claude adds later shows here as New, switched off\./);
assert.deepEqual(await rows(b, CLAUDE), [
{ id: 'claude-opus-5-5', on: true, note: '' },
{ id: 'claude-sonnet-5-5', on: true, note: 'In use: Acme Studio\'s assistant, Lab Bench\'s chief fallback' },
{ id: 'claude-next-6', on: false, note: 'New' },
]);
// Every box has a label, and the Save of each list is disabled until a box in it changes.
assert.equal(await b.evaluate('[...document.querySelectorAll("#proxy-offered-list input[type=checkbox]")].every(c => c.closest("label") && !c.disabled)'), true);
assert.deepEqual(await b.evaluate(`[${save(CLAUDE)}, ${save(ZAI)}].map(s => [s.textContent, s.disabled])`), [['Save Claude models', true], ['Save z.ai models', true]]);
// Search only on the list longer than eight; it ignores case.
assert.equal(await b.evaluate(`!!document.querySelector(${JSON.stringify(CLAUDE + ' input[type=search]')})`), false);
await b.evaluate(`(() => { document.querySelector(${JSON.stringify(ZAI)}).open = true; const i = document.querySelector(${JSON.stringify(ZAI + ' input[type=search]')}); i.value = 'GLM-5.7'; i.dispatchEvent(new Event('input')); return true; })()`);
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll(${JSON.stringify(ZAI + ' li')})].filter(li => !li.hidden).map(li => li.dataset.model)`), ['glm-5.7']);
assert.equal(await b.evaluate(`document.querySelector('label[for="' + document.querySelector(${JSON.stringify(ZAI + ' input[type=search]')}).id + '"]').textContent`), 'Find a z.ai model');
assert.deepEqual((await admin()).filter(r => r.startsWith('POST')), [], 'reading the lists writes nothing');
assert.equal((await admin()).filter(r => r === 'GET models').length, 1);
});
test('models-offered page: a changed box says so and lights Save; changing it back dims Save; Save sends the list with the revision and keeps the list open with a saved line', { timeout: 90000 }, async t => {
const { b, wait, writes } = await setup(t, { admin: { models: [VIEW, SAVED], 'POST models/set': { provider: 'claude', revision: 'r2', on: 1, total: 3, receipt: { op: 'models.set' } } } });
await openOffered(b, wait);
assert.equal(await toggle(b, 'claude-next-6'), true);
assert.equal((await rows(b, CLAUDE))[2].note, 'New · Switched on, not saved yet');
assert.equal(await b.evaluate(`${save(CLAUDE)}.disabled`), false);
assert.equal(await b.evaluate(`${save(ZAI)}.disabled`), true, 'the other list keeps its Save dimmed');
await toggle(b, 'claude-next-6');
assert.equal((await rows(b, CLAUDE))[2].note, 'New');
assert.equal(await b.evaluate(`${save(CLAUDE)}.disabled`), true, 'back as read: Save is dimmed again');
assert.equal(await toggle(b, 'claude-opus-5-5'), false);
assert.equal((await rows(b, CLAUDE))[0].note, 'Switched off, not saved yet');
await b.evaluate(`${save(CLAUDE)}.click(); true`);
await wait(`document.querySelector(${JSON.stringify(CLAUDE + ' > summary')})?.textContent === "Claude models offered: 1 of 3 on"`);
assert.deepEqual(await writes(), [{ path: 'models/set', body: { provider: 'claude', on: ['claude-sonnet-5-5'], revision: 'r1' } }]);
assert.equal(await b.evaluate(`document.querySelector(${JSON.stringify(CLAUDE)}).open`), true, 'the saved list stays open');
assert.equal(await b.evaluate(`document.querySelector(${JSON.stringify(CLAUDE + ' [role=status]')}).textContent`), 'Saved. 1 of 3 on.');
assert.equal((await rows(b, CLAUDE))[0].on, false);
});
test('models-offered page: switching off a model in use asks in place, naming who uses it; Keep them on sends nothing more, Switch off anyway sends confirmInUse', { timeout: 90000 }, async t => {
const inUse = { status: 409, body: { error: 'models-in-use', usedBy: [{ model: 'claude-sonnet-5-5', org: 'acme', instance: 'assistant', fallback: false }, { model: 'claude-sonnet-5-5', org: 'lab', instance: 'chief', fallback: true }] } };
const done = { provider: 'claude', revision: 'r2', on: 1, total: 3, receipt: { op: 'models.set' } };
const { b, wait, writes } = await setup(t, { admin: { models: VIEW, 'POST models/set': [inUse, inUse, done] } });
await openOffered(b, wait);
await toggle(b, 'claude-sonnet-5-5');
await b.evaluate(`${save(CLAUDE)}.click(); true`);
const ASK = CLAUDE + ' [role=alert]';
await wait(`document.querySelector(${JSON.stringify(ASK)}) && !document.querySelector(${JSON.stringify(ASK)}).hidden`);
const ask = await b.evaluate(`document.querySelector(${JSON.stringify(ASK)}).textContent`);
assert.match(ask, /In use: claude-sonnet-5-5: Acme Studio's assistant; claude-sonnet-5-5: Lab Bench's chief fallback\./);
assert.match(ask, /Give each one another model on its organization's Models page\./);
assert.equal(await b.evaluate('document.activeElement.textContent'), 'Switch off anyway', 'focus moves to the question');
assert.deepEqual((await writes()).map(w => w.body.confirmInUse), [undefined], 'the first save does not confirm');
await b.evaluate(`[...document.querySelectorAll(${JSON.stringify(ASK + ' button')})].find(x => x.textContent === 'Keep them on').click(); true`);
assert.equal(await b.evaluate(`document.querySelector(${JSON.stringify(ASK)}).hidden`), true);
assert.equal((await writes()).length, 1, 'Keep them on sends nothing');
assert.equal((await rows(b, CLAUDE))[1].on, false, 'the box stays as the owner left it, still unsaved');
await b.evaluate(`${save(CLAUDE)}.click(); true`);
await wait(`!document.querySelector(${JSON.stringify(ASK)}).hidden`);
await b.evaluate(`[...document.querySelectorAll(${JSON.stringify(ASK + ' button')})].find(x => x.textContent === 'Switch off anyway').click(); true`);
await wait('window.__writes.length === 3');
assert.deepEqual((await writes())[2].body, { provider: 'claude', on: ['claude-opus-5-5'], revision: 'r1', confirmInUse: true });
});
test('models-offered page: an organization that is not answering is named in the ask, labelled where known; a user named by the organization file with no instance reads as an agent in it', { timeout: 90000 }, async t => {
const quiet = { status: 409, body: { error: 'models-in-use', usedBy: [{ model: 'claude-sonnet-5-5', org: 'acme', instance: null, fallback: false }], unanswered: ['lab', 'zeta'] } };
const done = { provider: 'claude', revision: 'r2', on: 1, total: 3, receipt: { op: 'models.set' } };
const { b, wait, writes } = await setup(t, { admin: { models: VIEW, 'POST models/set': [quiet, done] } });
await openOffered(b, wait);
await toggle(b, 'claude-sonnet-5-5');
await b.evaluate(`${save(CLAUDE)}.click(); true`);
const ASK = CLAUDE + ' [role=alert]';
await wait(`document.querySelector(${JSON.stringify(ASK)}) && !document.querySelector(${JSON.stringify(ASK)}).hidden`);
const ask = await b.evaluate(`document.querySelector(${JSON.stringify(ASK)}).textContent`);
assert.match(ask, /Not answering now, so it is not known which models they use: Lab Bench, zeta\. A model you switch off stops working for them too\./);
assert.match(ask, /In use: claude-sonnet-5-5: an agent in Acme Studio\./);
await b.evaluate(`[...document.querySelectorAll(${JSON.stringify(ASK + ' button')})].find(x => x.textContent === 'Switch off anyway').click(); true`);
await wait('window.__writes.length === 2');
assert.equal((await writes())[1].body.confirmInUse, true);
});
test('models-offered page: a save after another change is refused as a conflict, worded, with Read the list again; a busy or unknown answer is worded too', { timeout: 90000 }, async t => {
const conflict = { status: 409, body: { error: 'models-offered-conflict' } };
const busy = { status: 409, body: { error: 'proxy-admin-busy' } };
const { b, wait, admin } = await setup(t, { admin: { models: [VIEW, SAVED], 'POST models/set': [busy, conflict] } });
await openOffered(b, wait);
await toggle(b, 'claude-opus-5-5');
const SAID = CLAUDE + ' [role=status]';
await b.evaluate(`${save(CLAUDE)}.click(); true`);
await wait(`/Another change to the model service is running/.test(document.querySelector(${JSON.stringify(SAID)}).textContent)`);
assert.equal(await b.evaluate(`!!document.querySelector(${JSON.stringify(SAID + ' button')})`), false, 'busy: try again, no reread');
assert.equal(await b.evaluate(`${save(CLAUDE)}.disabled`), false, 'the change is kept, so Save can be pressed again');
await b.evaluate(`${save(CLAUDE)}.click(); true`);
await wait(`/The model list changed since this page read it/.test(document.querySelector(${JSON.stringify(SAID)}).textContent)`);
assert.equal(await b.evaluate(`document.querySelector(${JSON.stringify(SAID)}).className`), 'banner err');
await b.evaluate(`document.querySelector(${JSON.stringify(SAID + ' button')}).click(); true`);
await wait(`document.querySelector(${JSON.stringify(CLAUDE + ' > summary')})?.textContent === "Claude models offered: 1 of 3 on"`);
assert.equal((await admin()).filter(r => r === 'GET models').length, 2, 'Read the list again reads it');
});
test('models-offered page: a page without proxyAdmin sends no /api/proxy/admin/ request; a read-only owner page shows the list and can change nothing', { timeout: 90000 }, async t => {
{
const { b, wait, admin } = await setup(t, { owner: false });
await wait(LISTED);
await b.evaluate('document.querySelector("#proxy-offered").open = true; true');
await wait('document.querySelector("#proxy-offered-list p")');
assert.deepEqual(await admin(), [], 'no admin read or write at all');
}
{
const { b, wait, writes } = await setup(t, { send: false, admin: { models: VIEW } });
await openOffered(b, wait);
assert.equal(await b.evaluate(`document.querySelector(${JSON.stringify(CLAUDE + ' fieldset')}).disabled`), true);
assert.equal(await b.evaluate(`document.querySelector(${JSON.stringify(CLAUDE + ' [role=status]')}).textContent`), 'This Console is read-only. Switching models on or off is unavailable.');
await b.evaluate(`(() => { const s = ${save(CLAUDE)}; s.disabled = false; s.click(); document.querySelector(${JSON.stringify(CLAUDE + ' form')}).requestSubmit(); return true; })()`);
assert.deepEqual(await writes(), []);
}
});
test('models-offered page: a read the model service cannot answer is worded, never an empty list', { timeout: 90000 }, async t => {
for (const [body, line] of [
[{ error: 'proxy-down' }, /No organization that uses your model service answered/],
[{ error: 'not-configured' }, /No organization uses your model service yet/],
[{ error: 'outcome-unknown' }, /The outcome is unknown/],
]) {
const { b, wait } = await setup(t, { admin: { models: { status: 503, body } } });
await wait(LISTED);
await b.evaluate('document.querySelector("#proxy-offered").open = true; true');
await wait(`${line}.test(document.querySelector("#proxy-offered-list").textContent)`);
assert.equal(await b.evaluate('document.querySelectorAll("#proxy-offered-list details[data-offered]").length'), 0);
await b.close();
}
});
test('models-offered page: at 390 wide the lists fit, every row keeps 44px, and nothing scrolls sideways', { timeout: 90000 }, async t => {
const { b, wait } = await setup(t, { viewport: [390, 844], admin: { models: VIEW } });
await openOffered(b, wait);
await toggle(b, 'claude-next-6');
assert.equal(await b.evaluate('document.documentElement.scrollWidth <= document.documentElement.clientWidth'), true);
assert.equal(await b.evaluate(`[...document.querySelectorAll(${JSON.stringify(CLAUDE + ' li')})].every(li => li.getBoundingClientRect().height >= 44 && li.getBoundingClientRect().right <= document.documentElement.clientWidth)`), true);
assert.equal(await b.evaluate(`${save(CLAUDE)}.checkVisibility()`), true);
});
@@ -70,6 +70,7 @@ const ownerStub = (service = SERVICE_UP, extra = {}) => `(() => {
if (__s4.routes && /\\/bus\\/routes$/.test(path)) return json(__s4.routes);
if (/\\/api\\/(orgs\\/[^/]+\\/)?config$/.test(path)) { const c = await (await real(input, init)).json(); return json({ ...c, orgSetup: true, proxyAdmin: true, proxyAdminSend: true, ...(/\\/orgs\\//.test(path) ? { settingsSend: true } : {}) }); }
if (path.endsWith('/proxy/admin/status')) return json(__s4.service);
if (__s4.offered && path.endsWith('/proxy/admin/models')) return json(__s4.offered);
if (path.endsWith('/proxy/accounts')) return json({ accounts: __s4.accounts });
if (path.endsWith('/proxy/keys')) return json({ keys: [] });
if (path.endsWith('/triage/route')) return json({ route: null });
@@ -342,10 +343,13 @@ test('settings-s4: accounts read as rows with kind and health, the provider pick
await b.navigate(f.base + '/#/settings/model-setup');
await wait('document.querySelectorAll("#proxy-accounts li[data-account]").length === 4');
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("#proxy-accounts li")].map(li => [...li.children].map(c => c.innerText.replace(/\\s+/g, " ").trim()))'), [
['Claude', 'Subscription Claude work', 'Ready'], ['ChatGPT / Codex', 'Subscription Codex home', 'Not working'], ['z.ai', 'API key z.ai key', 'Off'], ['Claude', 'Subscription Claude old', 'Unknown']]);
// #1873 account-remove: the instance owner's rows end in Remove.
['Claude', 'Subscription Claude work', 'Ready', 'Remove'], ['ChatGPT / Codex', 'Subscription Codex home', 'Not working', 'Remove'], ['z.ai', 'API key z.ai key', 'Off', 'Remove'], ['Claude', 'Subscription Claude old', 'Unknown', 'Remove']]);
assert.equal(await b.evaluate('document.querySelector("#setup-step-accounts").dataset.state'), 'bad', 'an account that is not working needs the owner');
assert.match(await b.evaluate('document.querySelector("#setup-step-accounts [data-step-summary]").textContent'), /ChatGPT \/ Codex \(subscription\): not working/);
assert.match(await b.evaluate('document.querySelector("#setup-step-accounts").textContent'), /Removing an account is not built yet\./);
// #1873 account-remove: removing is built; the owner sees no note, only each row's Remove.
assert.doesNotMatch(await b.evaluate('document.querySelector("#setup-step-accounts").textContent'), /Removing an account is not built yet/);
assert.equal(await b.evaluate('document.querySelector("#proxy-remove-note").hidden'), true);
// The provider picks how to add: a sign-in for a subscription, Check and add for an API key.
await b.evaluate('(() => { document.querySelector("#proxy-add-box").open = true; const s = document.querySelector("#proxy-provider"); s.value = "z.ai"; s.dispatchEvent(new Event("change")); return true; })()');
assert.deepEqual(await b.evaluate('[document.querySelector("#proxy-login-go").checkVisibility(), document.querySelector("#proxy-apikey").checkVisibility(), document.querySelector("#proxy-apikey button[type=submit]").textContent]'), [false, true, 'Check and add']);
@@ -383,8 +387,8 @@ test('settings-s4 chat-fail F3: an account row shows the accounts read\'s reason
await b.navigate(f.base + '/#/settings/model-setup?step=accounts');
await wait('document.querySelectorAll("#proxy-accounts li[data-account]").length === 4');
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("#proxy-accounts li")].map(li => [...li.children].map(c => c.textContent.replace(/\\s+/g, " ").trim()))'), [
['Claude', 'SubscriptionClaude work', 'Not available', out], ['ChatGPT / Codex', 'SubscriptionCodex home', 'Not working', signin],
['Claude', 'SubscriptionClaude spare', 'Not working'], ['ChatGPT / Codex', 'SubscriptionCodex work', 'Ready']], `${width}: the reason follows the state; absent or empty shows nothing`);
['Claude', 'SubscriptionClaude work', 'Not available', 'Remove', out], ['ChatGPT / Codex', 'SubscriptionCodex home', 'Not working', 'Remove', signin],
['Claude', 'SubscriptionClaude spare', 'Not working', 'Remove'], ['ChatGPT / Codex', 'SubscriptionCodex work', 'Ready', 'Remove']], `${width}: the reason follows the state; absent or empty shows nothing`);
assert.equal(await b.evaluate('document.querySelectorAll("#proxy-accounts img").length + (window.__xss ?? 0)'), 0, 'the reason is text, never markup');
assert.equal(await b.evaluate('[...document.querySelectorAll("#proxy-accounts .account-reason")].every(p => p.checkVisibility() && p.getBoundingClientRect().right <= document.documentElement.clientWidth)'), true);
assert.equal(await b.evaluate('document.documentElement.scrollWidth <= document.documentElement.clientWidth'), true, `${width}: nothing scrolls sideways`);
@@ -392,13 +396,16 @@ test('settings-s4 chat-fail F3: an account row shows the accounts read\'s reason
}
});
test('settings-s4: Models offered lists each provider\'s models read-only with a count, a search past eight, and Coming later', { timeout: 90000 }, async t => {
// #1873 models-offered: the instance owner's page switches models on or off (settings-models-offered-
// browser.test.mjs). A page that is not the owner's still reads the list read-only, as here.
test('settings-s4: a page that is not the instance owner\'s lists each provider\'s models read-only with a count, a search past eight, and Coming later', { timeout: 90000 }, async t => {
const list = [{ accountId: 'a1', provider: 'claude', displayName: 'Claude work', status: 'active', priority: 0 }, { accountId: 'a2', provider: 'codex', displayName: 'Codex home', status: 'active', priority: 0 }];
const models = Array.from({ length: 10 }, (_, i) => ({ module: 'cliproxy', upstream: 'claude', model: `claude-model-${i}`, available: true }))
.concat([{ module: 'cliproxy', upstream: 'codex', model: 'gpt-x', available: true }, { module: 'cliproxy', upstream: 'codex', model: 'gpt-gone', available: false }, { module: 'fixture', upstream: 'fixture', model: 'fixture-deterministic', available: true }]);
const status = { ...running, organizations: [{ id: 'acme', proxy: 'shared', catalogue: { status: 'ready', models: 11 } }] };
const { f, b, wait } = await w15Fixture(t, { scenario: { '/api/config': config, '/api/proxy/admin/status': status, ...accounts(list), '/api/triage/route': { route: null },
'/api/orgs/acme/bus/routes': { rows: [{ catalogue: { status: 'ready', models } }] } } });
const { f, b, wait } = await w15Fixture(t, { scenario: { '/api/config': { send: true, devOwner: true }, '/api/proxy/admin/status': status, ...accounts(list),
'GET /api/proxy/accounts': { body: { accounts: list, providers: publicProviders() } }, '/api/triage/route': { route: null },
'/api/bus/routes': { rows: [{ catalogue: { status: 'ready', models } }] } } });
await b.navigate(f.base + '/#/settings/model-setup?step=accounts');
await wait('document.querySelectorAll("#proxy-accounts li[data-account]").length === 2');
await b.evaluate('document.querySelector("#proxy-offered").open = true; true');
@@ -411,6 +418,7 @@ test('settings-s4: Models offered lists each provider\'s models read-only with a
await b.evaluate('(() => { document.querySelector("#proxy-offered-list details[data-offered=claude]").open = true; const i = document.querySelector("#proxy-offered-list details[data-offered=claude] input[type=search]"); i.value = "model-7"; i.dispatchEvent(new Event("input")); return true; })()');
assert.deepEqual(await b.evaluate('[...document.querySelectorAll("#proxy-offered-list details[data-offered=claude] li")].filter(li => !li.hidden).map(li => li.dataset.model)'), ['claude-model-7']);
assert.deepEqual(await b.evaluate('window.__writes'), [], 'reading the models writes nothing');
assert.deepEqual(await b.evaluate('window.__unexpected'), [], 'no admin read: the page is not the owner\'s');
});
test('settings-s4 review N9: before setup, every control under Advanced takes focus with a visible ring', { timeout: 90000 }, async t => {
@@ -462,6 +470,9 @@ test('shots', { timeout: 900000, skip: !process.env.MOSAIC_UX_SHOTS }, async t =
const models = Array.from({ length: 10 }, (_, i) => ({ module: 'cliproxy', upstream: 'claude', model: `claude-sonnet-4-${i}`, available: true }))
.concat(['gpt-5.1', 'gpt-5.1-codex'].map(model => ({ module: 'cliproxy', upstream: 'codex', model, available: true })));
const routes = { rows: [{ ...VIEW, catalogue: { status: 'ready', observedAt: new Date().toISOString(), models, notices: [] } }] };
// The owner's lists (GET /api/proxy/admin/models), from the same models.
const listed = upstream => models.filter(x => x.upstream === upstream).map((x, i) => ({ id: x.model, label: x.model, on: i < 3, isNew: false, usedBy: i === 0 ? [{ org: 'acme', instance: 'assistant', fallback: false }] : [] }));
const offered = { revision: 'r1', providers: [{ provider: 'claude', label: 'Claude', on: 3, total: 10, models: listed('claude') }, { provider: 'codex', label: 'ChatGPT / Codex', on: 2, total: 2, models: listed('codex') }] };
const go = 'document.querySelector("#proxy-setup-go").click(); true';
const PAGES = [
{ name: 'nav-open', path: '/#/settings/routing?org=acme', ready: 'document.querySelector("#org-pick")', after: async (b, wait, width) => {
@@ -478,7 +489,7 @@ test('shots', { timeout: 900000, skip: !process.env.MOSAIC_UX_SHOTS }, async t =
await b.evaluate('window.open = () => null; document.querySelector("#proxy-add-box").open = true; document.querySelector("#proxy-login-start").requestSubmit(); true'); await wait('!document.querySelector("#proxy-signin").hidden'); } },
{ name: 'models-accounts-key', path: '/#/settings/model-setup?step=accounts', service: up, extra: { accounts, routes }, ready: 'document.querySelectorAll("#proxy-accounts li[data-account]").length === 2', after: async b => {
await b.evaluate('(() => { document.querySelector("#proxy-add-box").open = true; const s = document.querySelector("#proxy-provider"); s.value = "z.ai"; s.dispatchEvent(new Event("change")); return true; })()'); } },
{ name: 'models-offered', path: '/#/settings/model-setup?step=accounts', service: up, extra: { accounts, routes }, ready: 'document.querySelectorAll("#proxy-accounts li[data-account]").length === 2', after: async (b, wait) => {
{ name: 'models-offered', path: '/#/settings/model-setup?step=accounts', service: up, extra: { accounts, routes, offered }, ready: 'document.querySelectorAll("#proxy-accounts li[data-account]").length === 2', after: async (b, wait) => {
await b.evaluate('document.querySelector("#proxy-offered").open = true; true'); await wait('document.querySelector("#proxy-offered-list details[data-offered=claude]")');
await b.evaluate('document.querySelector("#proxy-offered-list details[data-offered=claude]").open = true; true'); } },
{ name: 'models-problems', path: '/#/settings/model-setup', service: { ...up, organizations: [{ id: 'acme', proxy: 'shared', catalogue: { status: 'ready', launchRestart: true } }, { id: 'north', proxy: 'shared', catalogue: { status: 'ready', models: 12 } }],
@@ -379,6 +379,15 @@ test('settings-s5b: a history entry whose action is not a known one reads Other
for (const item of await items(b)) assert.match(item.text, /^Other setup action · .+ · by jason$/);
});
test('settings-s5b: the models-offered entries read as what they did, never Other setup action', { timeout: 90000 }, async t => {
const { b, wait } = await setup(t, { admin: { ...OK, 'history?limit=25': { entries: [{ op: 'models.seed', time: T(1), user: 'jason' }, { op: 'models.set', provider: 'not-a-provider', time: T(2), user: 'jason' }], next: null } } });
await wait(READY); await openTech(b);
await wait('document.querySelectorAll("#proxy-setup-history li").length === 2');
const texts = (await items(b)).map(i => i.text).sort();
assert.match(texts[0], /^Model lists recorded, every model on · .+ · by jason$/);
assert.match(texts[1], /^Models switched on or off · .+ · by jason$/);
});
test('settings-s5b: a withheld entry shows as withheld and nothing else from it', { timeout: 90000 }, async t => {
const { b, wait } = await setup(t, { admin: { ...OK, 'history?limit=25': { entries: [{ withheld: true, op: 'init', time: T(2), user: 'jason', org: 'acme' }, { withheld: true }], next: null } } });
await wait(READY); await openTech(b);
+4
View File
@@ -73,6 +73,10 @@ case "$OP" in
else
echo "env-side credential-like names set: (none)"
fi
# lane health-reason: the provider the default run asks for, and whether the worker
# container compose starts will hold a credential for it (names and types only).
node scripts/worker-credential.mjs status 2>/dev/null \
|| echo "default run: provider ${MOSAIC_PROVIDER:-(unset)} (config execution.provider); worker container credential: not checked (the check could not run)"
echo "mosaic-managed accounts ($ACCOUNTS_DIR):"
shopt -s nullglob
FILES=("$ACCOUNTS_DIR"/*.json)
+31
View File
@@ -0,0 +1,31 @@
#!/usr/bin/env node
// lane health-reason: the reason line for a release health check that gave no run id, picked
// from what the runner wrote to stderr. The last refusal line from the runner or its helpers
// (mosaic-task:, run-task:, config:, common:) wins, with its prefix dropped; notes that don't stop
// a run are passed over; with no refusal line, the last line. The line is redacted (paths, links,
// emails, key-shaped strings) and withheld whole when it holds a value from the environment.
//
// node scripts/health-reason.mjs <stderr file> prints the line, or nothing when none can be shown
import { readFileSync, realpathSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { redactLine, secretValues } from "../packages/model-proxy/src/redact.mjs";
const PREFIX = /^(?:mosaic-task|run-task|config|common): /;
const NOTES = [/^run-task: note:/, /^mosaic-task: worker credential not checked:/];
export function runnerReason(text, env = process.env) {
const lines = String(text ?? "").split(/\r?\n/).filter((line) => line.trim());
const refusals = lines.filter((line) => PREFIX.test(line) && !NOTES.some((re) => re.test(line)));
const pick = refusals.length ? refusals[refusals.length - 1] : lines[lines.length - 1];
if (!pick) return null;
return redactLine(pick.replace(PREFIX, ""), secretValues(env)).text ?? null;
}
const self = fileURLToPath(import.meta.url);
let main = false;
try { main = realpathSync(process.argv[1] ?? "") === realpathSync(self); } catch {}
if (main) {
let text = "";
try { text = readFileSync(process.argv[2] ?? "", "utf8"); } catch {}
process.stdout.write(runnerReason(text) ?? "");
}
+64 -6
View File
@@ -36,8 +36,10 @@ import { randomBytes } from "node:crypto";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import { imageTag } from "./image-tag.mjs";
import { checkWorkerCredential, missingLine } from "./worker-credential.mjs";
import { BusinessError, validateRoleDocument } from "../packages/organization/src/index.mjs";
import { RunsError, isRunId, listRunRecords, readRunRecord } from "../packages/runs/src/index.mjs";
import { RunsError, isRunId, listRunRecords, readRunRecord, readRunText } from "../packages/runs/src/index.mjs";
import { redactLine, secretValues } from "../packages/model-proxy/src/redact.mjs";
import { initializeControl, assertProcessing } from "../packages/processing-control/src/state.mjs";
import { runContainer } from "../packages/processing-control/src/containers.mjs";
import { unregisterRuntime } from "../packages/processing-control/src/runtimes.mjs";
@@ -328,7 +330,7 @@ function readSessionFork(file) {
}
}
function runTask(taskFile, options = {}) {
async function runTask(taskFile, options = {}) {
const resolved = JSON.parse(
spawnSync(process.execPath, [path.join(PROJECT_ROOT, "scripts", "mosaic-config.mjs"), "validate"], {
cwd: PROJECT_ROOT,
@@ -344,6 +346,26 @@ function runTask(taskFile, options = {}) {
// package.json range. Read before the run directory exists, so a lock it can't read leaves none.
let tag;
try { tag = imageTag(PROJECT_ROOT); } catch (e) { fail(4, e.message); }
// lane health-reason: the provider the run asks for (config execution.provider) needs a
// credential the worker container receives, or pi stops before the prompt. Checked against the
// environment compose reads, before the run directory exists, so a refusal leaves no record.
const launchEnv = {
...process.env,
MOSAIC_ADAPTER: resolved.execution.adapter,
MOSAIC_PROVIDER: resolved.execution.provider,
MOSAIC_MODEL: resolved.execution.model,
MOSAIC_IMAGE_TAG: tag,
};
const credential = await checkWorkerCredential({
adapter: resolved.execution.adapter,
provider: resolved.execution.provider,
env: launchEnv,
root: PROJECT_ROOT,
});
if (credential.state === "missing") fail(3, missingLine(credential));
if (credential.state === "undecided") {
process.stderr.write(`mosaic-task: worker credential not checked: ${credential.why}\n`);
}
const runId = `r-${utcStamp()}-${randomBytes(3).toString("hex")}`;
const runDir = path.join(runsRoot(resolved), runId);
@@ -546,6 +568,41 @@ function listRuns() {
}
}
// lane health-reason: one plain line saying why a failed run failed, for `show` and the release
// health check. A reason the runner decided itself is said in words. For a worker that exited
// with an error, the line is taken from the run's stderr.txt: compose's own progress and warning
// lines and NAME=value lines are passed over, a line that reads as an error is preferred, and the
// line is redacted (links, paths, emails, key-shaped strings) and withheld whole when it holds a
// secret value from this process's environment. Never a host path or a value.
const PLAIN_REASONS = {
timeout: "the run passed its time limit and was stopped",
"expect-mismatch": "the response did not match the expected text",
"kill-switch": "processing was halted during the run",
};
const NOISE = [
/^\s*(?:Network|Container|Volume|Image)\s+\S+\s+(?:Creating|Created|Starting|Started|Stopping|Stopped|Removing|Removed|Pulling|Pulled|Waiting|Healthy|Running|Recreate|Recreated)\b/,
/^\s*(?:Warning|WARN)\b/i,
/^\s*time="[^"]*"\s+level=warn/i,
/^[A-Za-z_][A-Za-z0-9_]*=/,
];
const ERRORISH = /\b(?:error|errors|failed|fail|fails|no|not|cannot|can't|unable|refus\w*|denied|missing|invalid|unauthori[sz]ed|forbidden|expired)\b/i;
function runCause(dataRoot, runId, result) {
if (!result || result.status === "succeeded") return null;
const reason = typeof result.reason === "string" ? result.reason : "";
if (PLAIN_REASONS[reason]) return PLAIN_REASONS[reason];
const secrets = secretValues(process.env);
const text = readRunText(dataRoot, runId, "stderr.txt") ?? "";
const lines = text.split(/\r?\n/).filter((line) => line.trim() && !NOISE.some((re) => re.test(line)));
for (const line of [...lines.filter((l) => ERRORISH.test(l)), ...lines]) {
const shown = redactLine(line, secrets);
if (shown.text) return `worker said: ${shown.text}`;
}
if (reason.startsWith("spawn-error")) return "the worker container could not be started";
const code = Number.isInteger(result.exitCode) ? ` with code ${result.exitCode}` : "";
return `the worker exited${code} and wrote nothing that can be shown`;
}
function showRun(runId) {
if (!isRunId(runId)) {
fail(4, `invalid run id: ${JSON.stringify(runId)} (expected r-<id>)`);
@@ -574,6 +631,7 @@ function showRun(runId) {
result.retriedFrom ? `retriedFrom: ${result.retriedFrom}` : null,
`timing: ${result.startedAt} -> ${result.finishedAt} (${result.durationMs} ms)`,
`exit: ${result.exitCode}${result.signal ? ` signal=${result.signal}` : ""}`,
result.status !== "succeeded" ? `cause: ${runCause(resolved.dataRoot, runId, result)}` : null,
].filter((line) => line !== null).join("\n") + "\n",
);
} else {
@@ -585,7 +643,7 @@ function showRun(runId) {
process.exit(0);
}
function retryRun(runId) {
async function retryRun(runId) {
if (!/^r-[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/.test(runId)) {
fail(4, `invalid run id: ${JSON.stringify(runId)} (expected r-<id>)`);
}
@@ -632,7 +690,7 @@ function retryRun(runId) {
// Best-effort cleanup only.
}
});
runTask(tempTaskFile, { retriedFrom: runId });
await runTask(tempTaskFile, { retriedFrom: runId });
}
function pruneRuns(args) {
@@ -695,7 +753,7 @@ switch (operation) {
}
case "run":
if (!target) fail(4, "usage: mosaic-task.mjs run <taskFile>");
runTask(path.resolve(target));
await runTask(path.resolve(target));
break;
case "show":
if (!target) fail(4, "usage: mosaic-task.mjs show <runId>");
@@ -717,7 +775,7 @@ switch (operation) {
}
case "retry":
if (!target) fail(4, "usage: mosaic-task.mjs retry <runId>");
retryRun(target);
await retryRun(target);
break;
default:
fail(4, `unknown operation: ${JSON.stringify(operation ?? "")} (expected validate | run | show | list | retry | prune | resolve-role)`);
+23 -4
View File
@@ -39,10 +39,10 @@ append_log() { # event release imageTag note
image_exists() { docker image inspect "$1" >/dev/null 2>&1; }
health_check() { # returns 0 only when the marker path passes; $1 = fault injection label or empty
local tmp=""
local tmp
tmp="$(mktemp -d)" || { echo "release: health check can't make its temporary directory" >&2; return 1; }
local task="tasks/hello-marker.json"
if [ -n "${1:-}" ]; then
tmp="$(mktemp -d)"
# Fault injection: same prompt, deliberately wrong expectation.
printf '{"taskVersion":1,"id":"t-health-fault","prompt":"Return your startup marker and nothing else.","expectExact":"MOSAIC_FAULT_%s"}' \
"$RANDOM$RANDOM" > "$tmp/fault-task.json"
@@ -51,11 +51,30 @@ health_check() { # returns 0 only when the marker path passes; $1 = fault inject
local rc=0
# MOSAIC_ENSURE_SKIP: the gate's run must not re-enter release
# self-determination (recursion guard).
MOSAIC_ENSURE_SKIP=1 scripts/run-task.sh run "$task" >/dev/null 2>&1 || rc=$?
[ -n "$tmp" ] && rm -rf "$tmp"
MOSAIC_ENSURE_SKIP=1 scripts/run-task.sh run "$task" >"$tmp/out.txt" 2>"$tmp/err.txt" || rc=$?
[ "$rc" -eq 0 ] || health_reason "$tmp/out.txt" "$tmp/err.txt"
rm -rf "$tmp"
return "$rc"
}
# lane health-reason: why the health check failed, on stderr. With a run id: the id and the cause
# line `mosaic-task show` reads from its record. With none (the runner refused before it printed
# one): the last refusal line the runner printed, redacted by scripts/health-reason.mjs. Never a
# value or a host path.
health_reason() { # $1 = the run's stdout, $2 = its stderr
local run cause
run="$(sed -n 's/^run: \(r-[A-Za-z0-9][A-Za-z0-9._-]*\)$/\1/p' "$1" | head -n 1)" || run=""
if [ -n "$run" ]; then
echo "release: health check run: $run" >&2
cause="$(node scripts/mosaic-task.mjs show "$run" 2>/dev/null | sed -n 's/^cause: *//p' | head -n 1)" || cause=""
echo "release: reason: ${cause:-the run record names no cause}" >&2
return 0
fi
cause="$(node scripts/health-reason.mjs "$2" 2>/dev/null)" || cause=""
echo "release: health check gave no run id" >&2
echo "release: reason: ${cause:-the runner printed nothing that can be shown}" >&2
}
activate() { # $1 = release, $2 = imageTag, $3 = event name, $4 = fault label
local release="$1" imageTag="$2" event="$3" fault="${4:-}"
+51
View File
@@ -83,6 +83,57 @@ printf '%s\n' "$OUT" | grep -q "FAKE_TEST_API_KEY" \
printf '%s\n' "$OUT" | grep -q "$ENVVAL" \
&& check "env var values never reach output" 1 || check "env var values never reach output" 0
# status: the provider the default run asks for and whether the worker container holds a
# credential for it (lane health-reason). A pi config; the API key variables compose passes are
# unset so only the fixture decides.
printf '{"configVersion":1,"environment":"development","dataRoot":"%s","execution":{"backend":"docker","provider":"zai","model":"m","adapter":"pi"}}' "$SANDBOX/data" > "$SANDBOX/pi-config.json"
printf '{"configVersion":1,"environment":"development","dataRoot":"%s","execution":{"backend":"docker","provider":"openai","model":"m","adapter":"pi"}}' "$SANDBOX/data" > "$SANDBOX/pi-config-openai.json"
PI_ENV="env -u ZAI_API_KEY -u ANTHROPIC_API_KEY -u COMPOSE_FILE -u COMPOSE_ENV_FILES"
OUT="$($PI_ENV PI_AUTH_FILE="$SANDBOX/pi/auth.json" MOSAIC_CONFIG="$SANDBOX/pi-config.json" scripts/auth.sh status 2>&1)"; RC=$?
[ "$RC" -eq 0 ] \
&& printf '%s\n' "$OUT" | grep -qx 'default run: provider zai (config execution.provider); worker container credential: yes' \
&& check "status names the default run's provider and a credential the container will have" 0 \
|| check "status names the default run's provider and a credential the container will have" 1
OUT="$($PI_ENV PI_AUTH_FILE="$SANDBOX/pi/auth.json" MOSAIC_CONFIG="$SANDBOX/pi-config-openai.json" scripts/auth.sh status 2>&1)"
printf '%s\n' "$OUT" | grep -qx 'default run: provider openai (config execution.provider); worker container credential: none (it has: anthropic, zai)' \
&& ! printf '%s\n' "$OUT" | grep -q "$SECRET\|$TOKEN" \
&& check "status says the container has no credential for the default provider, lists what it has, prints no value" 0 \
|| check "status says the container has no credential for the default provider, lists what it has, prints no value" 1
OUT="$($PI_ENV ZAI_API_KEY=$ENVVAL PI_AUTH_FILE="$SANDBOX/pi/broken.json" MOSAIC_CONFIG="$SANDBOX/pi-config.json" scripts/auth.sh status 2>&1)"
printf '%s\n' "$OUT" | grep -qx 'default run: provider zai (config execution.provider); worker container credential: yes' \
&& ! printf '%s\n' "$OUT" | grep -q "$ENVVAL" \
&& check "a key variable compose passes counts for the credential line, its value never printed" 0 \
|| check "a key variable compose passes counts for the credential line, its value never printed" 1
# A key variable that is only spaces is unset to pi, so it is no credential.
OUT="$($PI_ENV ZAI_API_KEY=' ' PI_AUTH_FILE="$SANDBOX/pi/broken.json" MOSAIC_CONFIG="$SANDBOX/pi-config.json" scripts/auth.sh status 2>&1)"
printf '%s\n' "$OUT" | grep -qx 'default run: provider zai (config execution.provider); worker container credential: none (it has: none)' \
&& check "a key variable that is only spaces is no credential" 0 \
|| check "a key variable that is only spaces is no credential" 1
# A PI_AUTH_FILE under ~ is read as compose mounts it, from the home directory.
mkdir -p "$SANDBOX/home2"
cp "$SANDBOX/pi/auth.json" "$SANDBOX/home2/auth.json"
OUT="$($PI_ENV HOME="$SANDBOX/home2" PI_AUTH_FILE='~/auth.json' MOSAIC_CONFIG="$SANDBOX/pi-config.json" scripts/auth.sh status 2>&1)"
printf '%s\n' "$OUT" | grep -qx 'default run: provider zai (config execution.provider); worker container credential: yes' \
&& check "a PI_AUTH_FILE under ~ is read from the home directory, as compose mounts it" 0 \
|| check "a PI_AUTH_FILE under ~ is read from the home directory, as compose mounts it" 1
# Where compose reads more than the check does, or the file can't be read here, it says so.
OUT="$($PI_ENV COMPOSE_ENV_FILES="$SANDBOX/x.env" PI_AUTH_FILE="$SANDBOX/pi/auth.json" MOSAIC_CONFIG="$SANDBOX/pi-config.json" scripts/auth.sh status 2>&1)"
printf '%s\n' "$OUT" | grep -qx "default run: provider zai (config execution.provider); worker container credential: not checked (COMPOSE_ENV_FILES names a file this check doesn't read)" \
&& check "COMPOSE_ENV_FILES leaves the credential not checked" 0 \
|| check "COMPOSE_ENV_FILES leaves the credential not checked" 1
if [ "$(id -u)" -ne 0 ]; then
cp "$SANDBOX/pi/auth.json" "$SANDBOX/pi/locked.json"; chmod 000 "$SANDBOX/pi/locked.json"
OUT="$($PI_ENV PI_AUTH_FILE="$SANDBOX/pi/locked.json" MOSAIC_CONFIG="$SANDBOX/pi-config.json" scripts/auth.sh status 2>&1)"
chmod 600 "$SANDBOX/pi/locked.json"
printf '%s\n' "$OUT" | grep -qx "default run: provider zai (config execution.provider); worker container credential: not checked (this user can't read the auth file the container mounts)" \
&& check "an auth file this user can't read leaves the credential not checked" 0 \
|| check "an auth file this user can't read leaves the credential not checked" 1
fi
OUT="$(env PI_AUTH_FILE="$SANDBOX/pi/auth.json" $CFG scripts/auth.sh status 2>&1)"
printf '%s\n' "$OUT" | grep -qx 'default run: provider zai (config execution.provider); the adapter needs no credential' \
&& check "status says the mock adapter needs no credential" 0 \
|| check "status says the mock adapter needs no credential" 1
# accounts: mosaic-managed, under the data root — a data root with no accounts dir is the default state: (none), exit 0, creates nothing
printf '{"configVersion":1,"environment":"development","dataRoot":"%s","execution":{"backend":"docker","provider":"zai","model":"m","adapter":"mock"}}' "$SANDBOX/empty" > "$SANDBOX/mock-config-empty.json"
OUT="$(env PI_AUTH_FILE="$SANDBOX/pi/auth.json" MOSAIC_CONFIG="$SANDBOX/mock-config-empty.json" scripts/auth.sh accounts 2>&1)"; RC=$?
+40
View File
@@ -74,6 +74,24 @@ bash -c 'source scripts/common.sh && load_release' >/dev/null 2>&1
bash -c 'source scripts/common.sh && load_release && case "$MOSAIC_IMAGE_TAG" in mosaic-poc-agent:*-r'"$(cat RELEASE)"') exit 0;; *) exit 1;; esac' >/dev/null 2>&1
check "valid RELEASE leaves image tag consistent with version" $?
# ---------- fast: the reason line when the health check gave no run id (lane health-reason) ----------
# The runner's last refusal line wins over notes, loses its prefix and is redacted: no host path and
# no key-shaped value. A line holding a value from the environment is withheld whole.
cat > "$SANDBOX/runner-err.txt" <<EOF
run-task: note: the session is new
mosaic-task: cannot fork: source session dir not found: $SANDBOX/sessions/x token=ghp_FIXTURE0123456789
mosaic-task: worker credential not checked: compose.yaml can't be read
EOF
REASON="$(node scripts/health-reason.mjs "$SANDBOX/runner-err.txt")"
case "$REASON" in "cannot fork: source session dir not found: "*) ok=0 ;; *) ok=1 ;; esac
[ "$ok" = 0 ] && ! printf '%s' "$REASON" | grep -qF "$SANDBOX" && ! printf '%s' "$REASON" | grep -qF 'ghp_FIXTURE' \
&& check "reason with no run id is the runner's refusal, redacted" 0 \
|| { check "reason with no run id is the runner's refusal, redacted" 1; printf '%s\n' "$REASON" >&2; }
printf 'mosaic-task: cannot start: quietharbourlamp refused\n' > "$SANDBOX/runner-err2.txt"
REASON="$(FIXTURE_TOKEN=quietharbourlamp node scripts/health-reason.mjs "$SANDBOX/runner-err2.txt")"
[ -z "$REASON" ] && check "reason holding an environment secret is withheld" 0 \
|| { check "reason holding an environment secret is withheld" 1; printf '%s\n' "$REASON" >&2; }
# ---------- sandboxed state machine (Docker required) ----------
if docker info >/dev/null 2>&1; then
mkdir -p "$SANDBOX/data"
@@ -106,6 +124,28 @@ process.exit(e.event === "refused" && e.release && e.imageTag && e.at ? 0 : 1);
check "refusal logged exactly once with valid fields" 1
fi
# The refusal says which run failed and why, from the run record (lane health-reason).
env MOSAIC_MOCK_RESPONSE=MOSAIC_HELLO_OK scripts/release.sh activate --fault-injection >"$SANDBOX/fault-out.txt" 2>&1
FAULT_RUN="$(sed -n 's/^release: health check run: \(r-[A-Za-z0-9][A-Za-z0-9._-]*\)$/\1/p' "$SANDBOX/fault-out.txt")"
[ -n "$FAULT_RUN" ] && [ -f "$SANDBOX/data/runs/$FAULT_RUN/result.json" ] \
&& grep -qx 'release: reason: the response did not match the expected text' "$SANDBOX/fault-out.txt" \
&& [ "$(tail -n 1 "$SANDBOX/fault-out.txt")" = "release: refusing activate: health check failed" ] \
&& ! grep -qF "$SANDBOX" "$SANDBOX/fault-out.txt" \
&& check "refused activation prints its run id and reason, no host path" 0 \
|| { check "refused activation prints its run id and reason, no host path" 1; cat "$SANDBOX/fault-out.txt" >&2; }
# A pi config whose provider the container has no credential for refuses before a run record;
# the reason is the runner's refusal line.
cat > "$SANDBOX/pi-config.json" <<EOF
{"configVersion":1,"environment":"development","dataRoot":"$SANDBOX/data","execution":{"backend":"docker","provider":"zai","model":"glm-5.3-flash","adapter":"pi"}}
EOF
env -u ZAI_API_KEY -u ANTHROPIC_API_KEY -u COMPOSE_FILE -u COMPOSE_ENV_FILES MOSAIC_CONFIG="$SANDBOX/pi-config.json" \
scripts/release.sh activate >"$SANDBOX/cred-out.txt" 2>&1
grep -qx 'release: health check gave no run id' "$SANDBOX/cred-out.txt" \
&& grep -q '^release: reason: the worker container has no credential for provider zai (config execution.provider); it has credentials for: none\.' "$SANDBOX/cred-out.txt" \
&& [ "$(tail -n 1 "$SANDBOX/cred-out.txt")" = "release: refusing activate: health check failed" ] \
&& check "activation with no worker credential prints the refusal as its reason" 0 \
|| { check "activation with no worker credential prints the refusal as its reason" 1; cat "$SANDBOX/cred-out.txt" >&2; }
expect_exit "healthy activation succeeds" 0 -- env MOSAIC_MOCK_RESPONSE=MOSAIC_HELLO_OK scripts/release.sh activate
node -e '
const fs = require("fs");
+80
View File
@@ -170,6 +170,86 @@ NEWEST="r-20260903T0100_05Z-suite005"
|| check "sessions/workspaces untouched by prune" 1
expect_exit "prune with invalid keep exits 4" 4 -- env MOSAIC_CONFIG="$SANDBOX/prune-config.json" node scripts/mosaic-task.mjs prune --keep=0 --yes
# ---------- worker credential check and run cause (lane health-reason; fixtures, stub docker) ----------
# A pi run whose provider has no credential the worker container receives refuses before the run
# directory exists. Auth files are fixtures; the API key variables compose passes are unset; a
# stub docker stands in for the container, so nothing reaches a provider or the host's pi.
CRED_ROOT="$SANDBOX/cred-data"
mkdir -p "$CRED_ROOT" "$SANDBOX/cred-bin" "$SANDBOX/pi"
cat > "$SANDBOX/cred-config.json" <<EOF
{"configVersion":1,"environment":"development","dataRoot":"$CRED_ROOT","execution":{"backend":"docker","provider":"zai","model":"glm-5.3-flash","adapter":"pi"}}
EOF
printf '{}' > "$SANDBOX/pi/cred-empty.json"
printf '{"anthropic":{"type":"oauth","access":"ACCESS-SHOULD-NOT-PRINT"}}' > "$SANDBOX/pi/cred-other.json"
printf '{"zai":{"type":"api_key","key":"KEY-SHOULD-NOT-PRINT"}}' > "$SANDBOX/pi/cred-zai.json"
chmod 600 "$SANDBOX/pi"/cred-*.json
# The stub writes a compose progress line, a compose warning and what pi writes when it has no key (or STUB_STDERR),
# then fails the way the container would.
cat > "$SANDBOX/cred-bin/docker" <<'STUB'
#!/usr/bin/env bash
printf 'Network x_default Creating\n' >&2
printf 'WARN[0000] The "X" variable is not set. Defaulting to a blank string.\n' >&2
printf '%s\n' "${STUB_STDERR:-No API key found for zai.}" >&2
exit 1
STUB
chmod 755 "$SANDBOX/cred-bin/docker"
cred_run() { # auth fixture, then extra env; stdout and stderr go to $SANDBOX/cred-out.txt
local auth="$1"; shift
env -u ZAI_API_KEY -u ANTHROPIC_API_KEY -u COMPOSE_FILE -u COMPOSE_ENV_FILES PATH="$SANDBOX/cred-bin:$PATH" \
MOSAIC_CONFIG="$SANDBOX/cred-config.json" PI_AUTH_FILE="$auth" "$@" \
node scripts/mosaic-task.mjs run "$SANDBOX/ok.json" >"$SANDBOX/cred-out.txt" 2>&1
}
cred_runs() { ls "$CRED_ROOT/runs" 2>/dev/null | grep -c '^r-'; }
cred_run "$SANDBOX/pi/cred-empty.json"; RC=$?
[ "$RC" -eq 3 ] \
&& grep -q "^mosaic-task: the worker container has no credential for provider zai (config execution.provider); it has credentials for: none\. Change the instance's execution.provider to one of those, or add a credential for zai" "$SANDBOX/cred-out.txt" \
&& [ "$(cred_runs)" -eq 0 ] && ! grep -qF "$SANDBOX" "$SANDBOX/cred-out.txt" \
&& check "pi run with no credential for its provider refuses (exit 3) before a run record, no host path" 0 \
|| { check "pi run with no credential for its provider refuses (exit 3) before a run record, no host path (exit $RC)" 1; cat "$SANDBOX/cred-out.txt" >&2; }
cred_run "$SANDBOX/pi/cred-other.json"; RC=$?
[ "$RC" -eq 3 ] && grep -q 'it has credentials for: anthropic\.' "$SANDBOX/cred-out.txt" \
&& ! grep -q 'ACCESS-SHOULD-NOT-PRINT' "$SANDBOX/cred-out.txt" \
&& check "credential refusal lists the providers the container has, never a value" 0 \
|| { check "credential refusal lists the providers the container has, never a value (exit $RC)" 1; cat "$SANDBOX/cred-out.txt" >&2; }
# The check refuses on the empty fixture and lets a key variable compose passes, or an auth file
# entry, through: one case, so it fails when either side is wrong.
cred_run "$SANDBOX/pi/cred-empty.json"; RC_EMPTY=$?
cred_run "$SANDBOX/pi/cred-empty.json" ZAI_API_KEY=; RC_BLANK=$?
cred_run "$SANDBOX/pi/cred-empty.json" ZAI_API_KEY=ENV-SHOULD-NOT-PRINT; RC_ENV=$?
ENV_OUT="$(cat "$SANDBOX/cred-out.txt")"
cred_run "$SANDBOX/pi/cred-zai.json"; RC=$?
CRED_RUN="$(sed -n 's/^run: \(r-.*\)$/\1/p' "$SANDBOX/cred-out.txt")"
[ "$RC_EMPTY" -eq 3 ] && [ "$RC_BLANK" -eq 3 ] && [ "$RC_ENV" -eq 1 ] && [ "$RC" -eq 1 ] && [ -n "$CRED_RUN" ] \
&& ! printf '%s\n' "$ENV_OUT" | grep -q 'no credential for provider\|ENV-SHOULD-NOT-PRINT' \
&& ! grep -q 'no credential for provider\|KEY-SHOULD-NOT-PRINT' "$SANDBOX/cred-out.txt" \
&& check "the check refuses no credential or an empty key variable, and lets a passed key variable or an auth file entry through" 0 \
|| { check "the check refuses no credential or an empty key variable, and lets a passed key variable or an auth file entry through (exits $RC_EMPTY $RC_BLANK $RC_ENV $RC)" 1; cat "$SANDBOX/cred-out.txt" >&2; }
SHOW="$(env MOSAIC_CONFIG="$SANDBOX/cred-config.json" $TASK show "$CRED_RUN" 2>&1)"
printf '%s\n' "$SHOW" | grep -qx 'cause: worker said: No API key found for zai\.' \
&& check "show prints the cause a failed run's stderr gives" 0 \
|| { check "show prints the cause a failed run's stderr gives" 1; printf '%s\n' "$SHOW" >&2; }
cred_run "$SANDBOX/pi/cred-zai.json" STUB_STDERR="Error: cannot read $SANDBOX/x.json with token=TOKEN-SHOULD-NOT-PRINT-123456"
CRED_RUN="$(sed -n 's/^run: \(r-.*\)$/\1/p' "$SANDBOX/cred-out.txt")"
SHOW="$(env MOSAIC_CONFIG="$SANDBOX/cred-config.json" $TASK show "$CRED_RUN" 2>&1)"
printf '%s\n' "$SHOW" | grep -q '^cause: worker said: Error: cannot read \[redacted\]' \
&& ! printf '%s\n' "$SHOW" | grep -qF "$SANDBOX" && ! printf '%s\n' "$SHOW" | grep -q 'TOKEN-SHOULD-NOT-PRINT' \
&& check "show's cause line redacts host paths and tokens" 0 \
|| { check "show's cause line redacts host paths and tokens" 1; printf '%s\n' "$SHOW" >&2; }
# A provider pi has no entry for can't be judged: the run is not refused, and one note says so.
sed 's/"provider":"zai"/"provider":"mosaic-unknown-provider"/' "$SANDBOX/cred-config.json" > "$SANDBOX/cred-unknown.json"
env -u ZAI_API_KEY -u ANTHROPIC_API_KEY -u COMPOSE_FILE -u COMPOSE_ENV_FILES PATH="$SANDBOX/cred-bin:$PATH" \
MOSAIC_CONFIG="$SANDBOX/cred-unknown.json" PI_AUTH_FILE="$SANDBOX/pi/cred-empty.json" \
node scripts/mosaic-task.mjs run "$SANDBOX/ok.json" >"$SANDBOX/cred-out.txt" 2>&1; RC=$?
[ "$RC" -eq 1 ] && grep -qx 'mosaic-task: worker credential not checked: pi has no provider named mosaic-unknown-provider' "$SANDBOX/cred-out.txt" \
&& grep -q '^run: r-' "$SANDBOX/cred-out.txt" \
&& check "a provider pi doesn't know is not refused, and a note says it wasn't checked" 0 \
|| { check "a provider pi doesn't know is not refused, and a note says it wasn't checked (exit $RC)" 1; cat "$SANDBOX/cred-out.txt" >&2; }
# ---------- adapter seam: deterministic mock cases (Docker, no provider) ----------
if docker info >/dev/null 2>&1; then
good_task "$SANDBOX/ok.json"
+218
View File
@@ -0,0 +1,218 @@
#!/usr/bin/env node
// lane health-reason: will the worker container hold a credential for the provider a run asks for?
//
// The run's provider is config execution.provider (mosaic-task.mjs sets MOSAIC_PROVIDER, compose
// passes it as PI_PROVIDER, the adapter as --provider). What the container receives is read from
// compose.yaml's mosaic-agent service, interpolated against the run's environment the way compose
// does: each environment name that ends up with a value, and the auth file the service mounts.
// From that file only provider names and credential types are read, as scripts/auth.sh status
// does. The adapter's harness plugin (packages/plugins/harness/<adapter>/worker-credential.mjs)
// answers whether those are enough. An adapter with no such module (mock) needs no credential.
// Nothing here prints or keeps a value.
//
// checkWorkerCredential({ adapter, provider, env, root })
// answers { state: "none-needed" | "has" | "missing" | "undecided", provider, providers, why }
//
// node scripts/worker-credential.mjs status one line for scripts/auth.sh status, read from
// MOSAIC_ADAPTER and MOSAIC_PROVIDER (load_config)
import { existsSync, lstatSync, readFileSync, realpathSync } from "node:fs";
import { isAbsolute, join, resolve } from "node:path";
import { pathToFileURL, fileURLToPath } from "node:url";
const ROOT = fileURLToPath(new URL("..", import.meta.url));
const SERVICE = "mosaic-agent";
const NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
const ADAPTER = /^[a-z0-9][a-z0-9-]{0,31}$/;
// Compose interpolation of one string: $$, $VAR, ${VAR}, ${VAR:-d}, ${VAR-d}, ${VAR:?m}, ${VAR?m},
// ${VAR:+a}, ${VAR+a}, with defaults interpolated in turn. A required variable that is missing
// gives "" here; compose itself refuses the run then.
export function interpolate(text, env) {
let out = "";
let i = 0;
while (i < text.length) {
const c = text[i];
if (c !== "$") { out += c; i += 1; continue; }
const next = text[i + 1];
if (next === "$") { out += "$"; i += 2; continue; }
if (next === "{") {
let depth = 1;
let j = i + 2;
while (j < text.length && depth > 0) {
if (text[j] === "{") depth += 1;
else if (text[j] === "}") depth -= 1;
if (depth > 0) j += 1;
}
if (depth !== 0) throw new Error("compose.yaml has an unclosed ${");
const body = text.slice(i + 2, j);
const m = /^([A-Za-z_][A-Za-z0-9_]*)(?:(:?[-?+])([\s\S]*))?$/.exec(body);
if (!m) throw new Error("compose.yaml has an interpolation this check can't read");
const [, name, op, rest = ""] = m;
const set = Object.hasOwn(env, name) && typeof env[name] === "string";
const filled = set && env[name] !== "";
const value = set ? env[name] : "";
if (!op) out += value;
else if (op === ":-") out += filled ? value : interpolate(rest, env);
else if (op === "-") out += set ? value : interpolate(rest, env);
else if (op === ":+") out += filled ? interpolate(rest, env) : "";
else if (op === "+") out += set ? interpolate(rest, env) : "";
else out += value; // :? and ?
i = j + 1;
continue;
}
const m = /^[A-Za-z_][A-Za-z0-9_]*/.exec(text.slice(i + 1));
if (m) { out += typeof env[m[0]] === "string" ? env[m[0]] : ""; i += 1 + m[0].length; continue; }
out += "$";
i += 1;
}
return out;
}
// Read and parse compose.yaml. Errors are fixed words: node's and yaml's own messages carry the
// file's path or quote its lines, and this text is printed.
async function readCompose(root) {
let YAML;
try { YAML = (await import("yaml")).default; } catch { throw new Error("the yaml package isn't installed in this checkout (run npm ci)"); }
let text;
try { text = readFileSync(join(root, "compose.yaml"), "utf8"); } catch { throw new Error("compose.yaml can't be read"); }
try { return YAML.parse(text); } catch { throw new Error("compose.yaml isn't valid YAML"); }
}
// A bind source as compose resolves it: a leading ~ is the home directory, and a relative path is
// relative to the project directory. Anything else is a named volume, which this check can't see.
function bindSource(source, root, env) {
if (source === "~" || source.startsWith("~/")) {
if (!env.HOME) return null;
return join(env.HOME, source.slice(1));
}
if (isAbsolute(source)) return source;
if (source === "." || source === ".." || source.startsWith("./") || source.startsWith("../")) return resolve(root, source);
return null;
}
// What compose gives the worker service: the names whose value ends up set (pi counts a value
// that is only spaces as unset), and the host source of each bind by its container target (null
// for a named volume). Refuses (throws) where compose reads more than this check does: another
// compose file, an override file, a .env file, COMPOSE_ENV_FILES or an env_file.
export async function containerInputs(root, env) {
for (const name of ["COMPOSE_FILE", "COMPOSE_ENV_FILES"]) {
if (env[name]) throw new Error(`${name} names a file this check doesn't read`);
}
for (const extra of ["compose.override.yaml", "compose.override.yml", "docker-compose.override.yaml", "docker-compose.override.yml", ".env"]) {
if (existsSync(join(root, extra))) throw new Error(`compose also reads ${extra}, which this check doesn't`);
}
const doc = await readCompose(root);
const service = doc?.services?.[SERVICE];
if (!service || typeof service !== "object") throw new Error(`compose.yaml has no ${SERVICE} service`);
if (service.env_file !== undefined) throw new Error(`the ${SERVICE} service reads an env_file, which this check doesn't`);
const filled = [];
const environment = service.environment ?? {};
const pairs = Array.isArray(environment)
? environment.map((item) => {
const s = String(item);
const at = s.indexOf("=");
return at < 0 ? [s, null] : [s.slice(0, at), s.slice(at + 1)];
})
: Object.entries(environment);
for (const [name, raw] of pairs) {
if (!NAME.test(name)) continue;
// A name with no value passes the launcher's own value through.
const value = raw === null || raw === undefined ? (env[name] ?? "") : interpolate(String(raw), env);
if (value.trim() !== "") filled.push(name);
}
const mounts = new Map();
for (const volume of service.volumes ?? []) {
if (typeof volume === "string") {
const parts = interpolate(volume, env).split(":");
if (parts.length >= 2) mounts.set(parts[1], bindSource(parts[0], root, env));
} else if (volume && typeof volume === "object" && typeof volume.target === "string" && typeof volume.source === "string") {
mounts.set(interpolate(volume.target, env), bindSource(interpolate(volume.source, env), root, env));
}
}
return { filled, mounts };
}
// Provider names and credential types from an auth file, never a value. A missing file, anything
// but a regular file, or a file that isn't a JSON object gives no entries, with why. A file this
// user may not read is unknown, not empty: Docker mounts it and the container reads it as its own
// user, so it gives no entries and `unreadable`.
const DENIED = new Set(["EACCES", "EPERM"]);
export function authEntries(file) {
if (file === null) return { entries: [], why: "the auth file is mounted from a named volume this check can't see", unreadable: true };
if (!file) return { entries: [], why: "no auth file is mounted" };
const unreadable = { entries: [], why: "this user can't read the auth file the container mounts", unreadable: true };
let stat;
try { stat = lstatSync(file); } catch (e) { return DENIED.has(e?.code) ? unreadable : { entries: [], why: "the auth file the container mounts is missing" }; }
if (stat.isSymbolicLink()) {
try { stat = lstatSync(realpathSync(file)); } catch (e) { return DENIED.has(e?.code) ? unreadable : { entries: [], why: "the auth file the container mounts is missing" }; }
}
if (!stat.isFile()) return { entries: [], why: "the auth file the container mounts isn't a regular file" };
let text;
try { text = readFileSync(file, "utf8"); } catch (e) { return DENIED.has(e?.code) ? unreadable : { entries: [], why: "the auth file the container mounts can't be read" }; }
let doc;
try { doc = JSON.parse(text.replace(/^\uFEFF/, "")); } catch { return { entries: [], why: "the auth file the container mounts can't be read as JSON" }; }
if (typeof doc !== "object" || doc === null || Array.isArray(doc)) return { entries: [], why: "the auth file the container mounts isn't a JSON object" };
const entries = Object.keys(doc).map((name) => {
const e = doc[name];
const type = typeof e === "object" && e !== null && typeof e.type === "string" ? e.type : "unknown";
return { name, type };
});
return { entries, why: null };
}
async function harnessModule(root, adapter) {
if (!ADAPTER.test(adapter ?? "")) return null;
const file = join(root, "packages", "plugins", "harness", adapter, "worker-credential.mjs");
if (!existsSync(file)) return null;
return import(pathToFileURL(file).href);
}
export async function checkWorkerCredential({ adapter, provider, env = process.env, root = ROOT } = {}) {
const base = { provider, providers: [], why: null };
let mod;
try { mod = await harnessModule(root, adapter); } catch { return { ...base, state: "undecided", why: `the ${adapter} harness check can't be loaded` }; }
if (!mod) return { ...base, state: "none-needed" };
if (typeof mod.credentialCheck !== "function" || typeof mod.authFileInContainer !== "string") {
return { ...base, state: "undecided", why: `the ${adapter} harness check is incomplete` };
}
let inputs;
try { inputs = await containerInputs(root, env); } catch (e) { return { ...base, state: "undecided", why: e.message }; }
const mounted = inputs.mounts.has(mod.authFileInContainer) ? inputs.mounts.get(mod.authFileInContainer) : undefined;
const { entries, why: fileWhy, unreadable } = authEntries(mounted);
const answer = await mod.credentialCheck({ provider, entries, envNames: inputs.filled, root });
if (!answer?.decided) return { ...base, state: "undecided", why: answer?.why ?? "the harness check gave no answer" };
if (!answer.known) return { ...base, providers: answer.providers, state: "undecided", why: `${adapter} has no provider named ${provider}` };
if (!answer.has && unreadable) return { ...base, providers: answer.providers, state: "undecided", why: fileWhy };
return { ...base, providers: answer.providers, state: answer.has ? "has" : "missing", why: answer.has ? null : fileWhy };
}
// The refusal: names the provider, says the container has none for it, lists what it has, and
// says what to do. No path, no value.
export function missingLine(check) {
const has = check.providers.length ? check.providers.join(", ") : "none";
return `the worker container has no credential for provider ${check.provider} (config execution.provider); ` +
`it has credentials for: ${has}. Change the instance's execution.provider to one of those, ` +
`or add a credential for ${check.provider} (a PI_AUTH_FILE entry, or an environment key compose passes to it).`;
}
export function statusLine(check) {
const head = `default run: provider ${check.provider ?? "(unset)"} (config execution.provider)`;
if (check.state === "none-needed") return `${head}; the adapter needs no credential`;
if (check.state === "has") return `${head}; worker container credential: yes`;
if (check.state === "missing") {
return `${head}; worker container credential: none (it has: ${check.providers.length ? check.providers.join(", ") : "none"})`;
}
return `${head}; worker container credential: not checked (${check.why})`;
}
const self = fileURLToPath(import.meta.url);
let main = false;
try { main = realpathSync(process.argv[1] ?? "") === realpathSync(self); } catch {}
if (main) {
if (process.argv[2] !== "status") {
process.stderr.write("usage: node scripts/worker-credential.mjs status\n");
process.exit(4);
}
const check = await checkWorkerCredential({ adapter: process.env.MOSAIC_ADAPTER, provider: process.env.MOSAIC_PROVIDER });
process.stdout.write(`${statusLine(check)}\n`);
}