Compare commits
38
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d752e75359 | ||
|
|
dde3ee4140 | ||
|
|
acb228d56f | ||
|
|
d01701a9ce | ||
|
|
ccd85604c6 | ||
|
|
e035b6dea9 | ||
|
|
735e828aa2 | ||
|
|
e698b7d785 | ||
|
|
8ed3e251be | ||
|
|
784748347b | ||
|
|
9869200a03 | ||
|
|
be7066b725 | ||
|
|
4aa008e9fe | ||
|
|
ca7f46e5d4 | ||
|
|
256e80f26b | ||
|
|
3225536adc | ||
|
|
9287e2fac7 | ||
|
|
9d02ab4a69 | ||
|
|
5210522e13 | ||
|
|
8e9b05de8d | ||
|
|
0f07301607 | ||
|
|
a4d06b95c6 | ||
|
|
f2463ab36d | ||
|
|
7afe892e94 | ||
|
|
4a3f501e13 | ||
|
|
c065034a6e | ||
|
|
83250ef36b | ||
|
|
b432f9cd6f | ||
|
|
e0065b6a44 | ||
|
|
3f39ed3f11 | ||
|
|
28a5d38349 | ||
|
|
824b1213c1 | ||
|
|
ca7388743b | ||
|
|
59af86a5ee | ||
|
|
d6d3271808 | ||
|
|
4a7815d29e | ||
|
|
af8c2f9777 | ||
|
|
b156727c00 |
No files matched your search
@@ -222,8 +222,18 @@ Compacted, restarted, or new? Nothing that matters is lost: this file +
|
||||
`git log --oneline -10` + the suites reconstruct the full state. **Never
|
||||
guess** — verify with the suites; the run records and logs hold the receipts.
|
||||
|
||||
## Version pin
|
||||
## Harness versions
|
||||
|
||||
`@earendil-works/pi-coding-agent` is pinned exactly (see `package.json` /
|
||||
`RELEASE`); never install unversioned. Release identity: `RELEASE` file
|
||||
(0.0.X until declared stable); image tags derive from it.
|
||||
Harness binaries are not pinned (Jason, 2026-11-19). pi runs from
|
||||
`MOSAIC_PI_BIN`, else the first `pi` on PATH, else the repository's
|
||||
`node_modules`. `package.json` holds a floor range, and `package-lock.json`
|
||||
records the version `npm ci` installs and the worker image carries. Nothing
|
||||
refuses a pi for its version. Before a session or conversation starts, a
|
||||
fit check proves the flags and behaviours Mosaic needs and refuses with a
|
||||
plain line when one is missing. The bus host repeats the check on a timer
|
||||
and when a binary changes. The version and a digest of the files that
|
||||
ran are recorded in the claim and the run record. A change under a live
|
||||
claim still refuses. Never install unversioned: a lock change is a
|
||||
reviewed commit that passes the fit check and the suites. Release
|
||||
identity: `RELEASE` file (0.0.X until declared stable). Image tags derive
|
||||
from it and the lock's resolved pi version.
|
||||
+5
-2
@@ -15,8 +15,11 @@ RUN apt-get update \
|
||||
# uid/gid 1000, which matches the host user that owns the runtime
|
||||
# state directory mounted at /var/lib/mosaic. It is reused as-is.
|
||||
|
||||
# Pinned Pi install: package.json pins the exact version and
|
||||
# package-lock.json is installed with npm ci. No unversioned installs.
|
||||
# Pi install: package.json gives pi a range, not a pin (#1873), and npm ci
|
||||
# installs the version package-lock.json resolves. No unversioned installs.
|
||||
# The build entry points run scripts/pi-fit-image.mjs on the host first, so a
|
||||
# lock that resolves an unfit pi fails the build (the fit check needs bwrap,
|
||||
# which docker build's seccomp profile refuses, so it is not a RUN step).
|
||||
WORKDIR /opt/app
|
||||
COPY package.json package-lock.json ./
|
||||
RUN npm ci --ignore-scripts
|
||||
|
||||
@@ -6,8 +6,8 @@ not implemented (see BRIEF.md, "Explicit exclusions").
|
||||
|
||||
## L0 — Implemented: container returns MOSAIC_HELLO_OK
|
||||
|
||||
One image (`mosaic-poc-agent:0.84.4`, built on digest-pinned `node:24.22.2-bookworm-slim`, non-root,
|
||||
pinned Pi) runs one Pi agent one-shot. Four immutable local contract files are
|
||||
One image (`mosaic-poc-agent:<pi>-r<release>`, the pi version `package-lock.json` resolves; built on digest-pinned `node:24.22.2-bookworm-slim`, non-root,
|
||||
pi from the lock, not pinned, and fit-checked before the build) runs one Pi agent one-shot. Four immutable local contract files are
|
||||
loaded in fixed order into the generated system prompt
|
||||
(`/var/lib/mosaic/private/system-prompt.md`, private to that container). One real model request is sent
|
||||
noninteractively; the response must equal `MOSAIC_HELLO_OK` exactly or the
|
||||
|
||||
@@ -193,7 +193,14 @@ explicit managed-name refusals with migration compatibility.
|
||||
## How it works
|
||||
|
||||
1. `scripts/build.sh` builds the release image (`mosaic-poc-agent:<pi>-r<release>`,
|
||||
tag derived from `RELEASE` + the pinned Pi version) with Docker Compose.
|
||||
tag derived from `RELEASE` + the pi version `package-lock.json` resolves, by
|
||||
`scripts/image-tag.mjs`) with `docker build -f Containerfile`. `package.json`
|
||||
gives pi a range, never a pin. Before the build, `scripts/pi-fit-image.mjs`
|
||||
runs the fit check on the pi the lock resolves, so a lock that resolves an
|
||||
unfit pi fails it. The check runs on the host's node, which can differ from
|
||||
the image's (the Containerfile's `FROM`). `compose.yaml` has no build section
|
||||
and `pull_policy: never`, so a compose run never builds or pulls the image;
|
||||
it refuses a missing one.
|
||||
2. On each run, `/opt/mosaic/src/load-contracts.sh` reads the four contract files
|
||||
in fixed order (CONSTITUTION, STANDARDS, SOUL, USER), joins them with clear
|
||||
separators, and writes `/var/lib/mosaic/private/system-prompt.md` in the
|
||||
|
||||
+19
-1
@@ -52,7 +52,7 @@ Optional, adapter-specific (documented per adapter):
|
||||
|
||||
## Included adapters
|
||||
|
||||
- `pi` — the pinned `@earendil-works/pi-coding-agent` CLI in noninteractive
|
||||
- `pi` — the `@earendil-works/pi-coding-agent` CLI (not pinned; #1873) in noninteractive
|
||||
print mode (`-p`), ambient discovery disabled, stdin detached.
|
||||
- `mock` — deterministic echo of `MOSAIC_MOCK_RESPONSE`. Test-only: never use
|
||||
it where a real model response is required.
|
||||
@@ -106,6 +106,24 @@ keeps its existing channel; the CLI does not inherit the completion pipe. No ext
|
||||
writable mount or completion file is needed. Pi retains print mode and signals
|
||||
activity through its existing extension. Mock remains a test-only echo.
|
||||
|
||||
### A worker's skills (M17, #1873)
|
||||
|
||||
`MOSAIC_SKILLS` is a comma list of skill directories. Today only the
|
||||
interactive container launch (`scripts/agent.sh`) sets it; task launches
|
||||
refuse skill directories and set it empty
|
||||
(`packages/processing-control/src/containers.mjs`). The pi adapter passes `--no-skills` and then
|
||||
`--skill <dir>` for each one. On pi 0.85.1 `--skill` adds to discovery and
|
||||
does not replace it. With `--skill` alone, a probe with a scratch HOME also
|
||||
loaded the user's skills, the skills a `settings.json` or a listed package
|
||||
names, and `~/.agents/skills` (and the project's, without `--no-approve`).
|
||||
With `--no-skills` first, only the given directories load. A directory that
|
||||
doesn't exist stops the adapter with exit 2. The fit check runs the
|
||||
headless form of this argv, with two skill directories, one `-e` file, no
|
||||
tools and no session, as its last `-p` run (`workerShape`,
|
||||
`packages/harness/README.md`); the TUI shape the launch uses is not run. The fit check runs
|
||||
before bus sessions, and on the pi `package-lock.json` resolves before an
|
||||
image build (`scripts/pi-fit-image.mjs`); worker containers run that pi.
|
||||
|
||||
### A role's skills (row 64)
|
||||
|
||||
A managed session's skills reach both adapters as text in the bundle's
|
||||
|
||||
@@ -57,14 +57,18 @@ fi
|
||||
TOOLS_FLAG="--no-tools"
|
||||
[ -n "${MOSAIC_TOOLS:-}" ] && TOOLS_FLAG="--tools $MOSAIC_TOOLS"
|
||||
|
||||
# Skills (M17): explicitly provided skill dirs replace discovery. When none
|
||||
# are provided the agent runs with --no-skills (nothing ambient to find).
|
||||
# Skills (M17, #1873 pi-skills): --no-skills always, then one --skill per
|
||||
# given dir. On pi 0.85.1 --skill adds to discovery and does not replace it:
|
||||
# with --skill alone pi also loads the user's skills, settings and package
|
||||
# skills and ~/.agents/skills (a planted-skill probe showed it, offline, with a
|
||||
# scratch HOME). With --no-skills first only the given dirs load. The fit check
|
||||
# (packages/harness/src/pi-fit.mjs) runs its -p form as its last run (workerShape).
|
||||
# Only the interactive launch (scripts/agent.sh) sets MOSAIC_SKILLS today.
|
||||
# Role sessions never get MOSAIC_SKILLS (ENV_ALLOW in packages/seat/src/
|
||||
# session.mjs leaves it out), so they run with --no-skills; their skills
|
||||
# session.mjs leaves it out), so they run with --no-skills alone; their skills
|
||||
# are text in the system prompt (row 64).
|
||||
SKILLS_FLAG="--no-skills"
|
||||
if [ -n "${MOSAIC_SKILLS:-}" ]; then
|
||||
SKILLS_FLAG=""
|
||||
OLDIFS=$IFS; IFS=','
|
||||
for s in $MOSAIC_SKILLS; do
|
||||
[ -d "$s" ] || { echo "pi adapter: skill dir missing: $s" >&2; exit 2; }
|
||||
@@ -112,6 +116,7 @@ fi
|
||||
# --system-prompt replace the default prompt with the generated one, given
|
||||
# as the file's absolute path (checked above), never its text
|
||||
# --no-* no ambient context/skills/extensions/templates/themes
|
||||
# SKILLS_FLAG --no-skills, then --skill <dir> for each MOSAIC_SKILLS dir
|
||||
# NO_MCP_FLAG --no-mcp when the binary lists it, else nothing
|
||||
# EXT_FLAGS the explicitly provided extensions only (-e)
|
||||
# SESSION_FLAGS ephemeral | persistent | forked (per env)
|
||||
|
||||
+5
-3
@@ -1,8 +1,10 @@
|
||||
services:
|
||||
mosaic-agent:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Containerfile
|
||||
# #1873 S5: no build section, and never pulled. The image is built only by
|
||||
# build_image (scripts/common.sh) or the gate's prepareImage, which run the
|
||||
# image fit step first, so a missing image refuses here instead of compose
|
||||
# building one that skipped the fit check.
|
||||
pull_policy: never
|
||||
image: ${MOSAIC_IMAGE_TAG:?MOSAIC_IMAGE_TAG must be set by scripts/load_release (run via scripts/*.sh)}
|
||||
user: "1000:1000"
|
||||
environment:
|
||||
|
||||
+2
-2
@@ -16,7 +16,7 @@ and changes nothing.
|
||||
| Command | Purpose | Notes |
|
||||
|---|---|---|
|
||||
| `scripts/bootstrap.sh [--instance NAME]` | Create `~/.config/mosaic-dev/config.json` if absent; with `--instance NAME`, `~/.config/mosaic-NAME/config.json` and the claimed root `~/.mosaic-NAME` | Idempotent; existing config validated, never rewritten. See [Stack instances](#stack-instances---instance-name-row-190) |
|
||||
| `scripts/build.sh` | Build the release image | Tag derived from `RELEASE` + pinned pi version |
|
||||
| `scripts/build.sh` | Build the release image | Tag `mosaic-poc-agent:<pi>-r<RELEASE>`, pi being the version `package-lock.json` resolves (`scripts/image-tag.mjs`); runs the image fit step (`scripts/pi-fit-image.mjs`) first. compose runs never build or pull the image, so run this before anything that needs a new tag |
|
||||
| `scripts/hello.sh` | One-shot startup request | Prints model response on stdout |
|
||||
| `scripts/verify.sh` | Full gated test | Exit 0 only on exact `MOSAIC_HELLO_OK`; `EXPECTED_MARKER` overrides for negative drills |
|
||||
|
||||
@@ -65,8 +65,8 @@ addition to its full repository gate; it was not run by this source repair:
|
||||
```bash
|
||||
node --test --test-concurrency=1 packages/processing-control/tests/containers.test.mjs packages/processing-control/tests/coordinator.test.mjs packages/processing-control/tests/effects.test.mjs packages/processing-control/tests/mount.test.mjs packages/processing-control/tests/runtimes.test.mjs packages/processing-control/tests/state.test.mjs packages/processing-control/tests/stop-run.test.mjs packages/processing-control/tests/task.test.mjs
|
||||
node --test scripts/test-darkwing-launch.mjs
|
||||
scripts/test-task.sh
|
||||
scripts/build.sh
|
||||
scripts/test-task.sh
|
||||
```
|
||||
|
||||
## Agent (interactive TUI)
|
||||
|
||||
Generated
+1
-1
@@ -9,7 +9,7 @@
|
||||
"version": "0.0.3",
|
||||
"license": "UNLICENSED",
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-coding-agent": "0.85.1",
|
||||
"@earendil-works/pi-coding-agent": ">=0.85.1",
|
||||
"oauth4webapi": "3.8.8",
|
||||
"yaml": "2.9.0"
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
"description": "Minimal Mosaic Stack container proof of concept: one Pi agent, four local contract files, one real model request returning MOSAIC_HELLO_OK.",
|
||||
"license": "UNLICENSED",
|
||||
"dependencies": {
|
||||
"@earendil-works/pi-coding-agent": "0.85.1",
|
||||
"@earendil-works/pi-coding-agent": ">=0.85.1",
|
||||
"oauth4webapi": "3.8.8",
|
||||
"yaml": "2.9.0"
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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']);
|
||||
@@ -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();
|
||||
|
||||
@@ -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.");
|
||||
|
||||
@@ -27,6 +27,7 @@ import { readSessions, sessionsFile, stopSession } from '../../seat/src/session.
|
||||
import { compactionCount, storeDir } from '../../seat/src/compactions.mjs';
|
||||
import { publishRemote, workingRepo, WorkingError } from '../../seat/src/working.mjs';
|
||||
import { createLoader, hasModule } from '../../plugins/src/loader.mjs';
|
||||
import { readWatchView } from '../../plugins/src/harness-watch.mjs';
|
||||
import { pluginSettings } from '../../organization/src/plugin-settings.mjs';
|
||||
import { loadManagementClient } from '../../model-proxy/src/client.mjs';
|
||||
import { ProxyPage } from './proxy-page.mjs';
|
||||
@@ -1393,7 +1394,9 @@ export async function startBroker({
|
||||
throw new TemplateError('invalid-request', 'template.save takes preview: true or the preview digest');
|
||||
}
|
||||
if(verb==='limits')return withTokens(id,settings.read(id));
|
||||
if(verb==='plugins')return {...plugins.read(id), available:{harness:levels.names ?? []}};
|
||||
// #1873 W3: harnessWatch is what the host's harness watch last wrote under the org's data root,
|
||||
// read only; a missing or bad file gives each harness "not checked yet" (readWatchView).
|
||||
if(verb==='plugins')return {...plugins.read(id), available:{harness:levels.names ?? []}, harnessWatch:readWatchView({dataRoot, names:levels.names ?? [], paths:[instanceRoot]})};
|
||||
// Row 256 (#1761): the actor is the reader, so endProtected is the delete check for that reader.
|
||||
if(verb==='instances')return instances.read(id,actor);
|
||||
// Row 65 (mos-0840): the Console's routing table.
|
||||
|
||||
@@ -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)); }
|
||||
});
|
||||
@@ -102,3 +102,67 @@ test('plugins read advertises loadable harnesses even when switched off', async
|
||||
const view=host.broker.request(human,{verb:'plugins'});
|
||||
assert.deepEqual(view.plugins.harness,['claude-code']);assert.ok(view.available.harness.includes('pi'));assert.ok(view.available.harness.includes('claude-code'));
|
||||
});
|
||||
|
||||
// #1873 harness watch W3: the plugins answer carries what the host's watch last wrote under the
|
||||
// organization's data root (state/harness-watch.json), read only, redacted again, and "not checked
|
||||
// yet" for every harness when the file is missing, cut short or not version 1's shape.
|
||||
test('W3 runtime: the plugins answer carries the harness watch record per harness, and a missing, truncated or wrong-shape file gives not checked yet', async (t) => {
|
||||
const { startBroker } = await import('../src/runtime.mjs');
|
||||
const { businessDoc, rolesCopy, systemFor, writeJson } = await import('../../organization/tests/helpers.mjs');
|
||||
const { writeFileSync } = await import('node:fs');
|
||||
const root = mkdtempSync(join(tmpdir(), 'plugins-watch-'));
|
||||
const rolesDir = rolesCopy(root);
|
||||
writeJson(join(root, 'businesses/demo.json'), businessDoc(root, 'demo'));
|
||||
const dataRoot = join(root, 'data');
|
||||
mkdirSync(join(dataRoot, 'state'), { recursive: true, mode: 0o700 });
|
||||
const planted = 'w3-planted-env-value';
|
||||
process.env.W3_PLANTED = planted;
|
||||
const bus = await startBroker({ dataRoot, businesses: { demo: { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles: { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery'], crossRole: [] } }, coder: { authority: { withinRole: [], crossRole: [] } } } } }, organization: { dir: root, rolesDir, system: systemFor(root) } });
|
||||
t.after(async () => { delete process.env.W3_PLANTED; await bus.close(); rmSync(root, { recursive: true, force: true }); });
|
||||
const human = bus.broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const file = join(dataRoot, 'state', 'harness-watch.json');
|
||||
const read = () => bus.broker.request(human, { verb: 'plugins' });
|
||||
const names = read().available.harness;
|
||||
assert.ok(names.length > 0, 'the tree gives harnesses');
|
||||
const unchecked = (name) => ({ name, state: 'unchecked', version: null, checkedAt: null, changedAt: null, failed: [], line: 'not checked yet' });
|
||||
|
||||
// No file: every harness the loader gives reads not checked yet, in the loader's order.
|
||||
assert.deepEqual(read().harnessWatch, names.map(unchecked));
|
||||
|
||||
const at = '2026-11-20T10:00:00.000Z', moved = '2026-11-19T08:00:00.000Z';
|
||||
const rec = (harness, over) => ({ harness, state: 'fit', via: 'inspect', found: true, source: 'PATH', version: '1.0.0', digest: 'a'.repeat(64), lastDigest: 'a'.repeat(64), fit: 'pass', definite: true, failed: [], line: 'ok', checkedAt: at, changedAt: moved, ...over });
|
||||
const doc = { version: 1, checkedAt: at, refused: [], gone: [], harnesses: [
|
||||
rec(names[0], { version: '2.1.323', line: 'every flag the adapter passes is in --help' }),
|
||||
rec('w3-red', { state: 'red', fit: 'fail', failed: ['--restricted'], line: '--help lacks --restricted' }),
|
||||
rec('w3-missing', { state: 'missing', found: false, version: null, digest: null, fit: 'unknown', definite: false, line: 'no binary from MOSAIC_PI_BIN, PATH or the repo copy' }),
|
||||
rec('w3-unknown', { state: 'unknown', fit: 'unknown', definite: false, line: 'inspect did not answer in time' }),
|
||||
// A path and an env value planted in the file: the broker redacts them again before answering.
|
||||
rec('w3-leak', { state: 'red', fit: 'fail', version: `9.9 ${planted}`, failed: [planted, '--ok'], line: `failed under ${dataRoot}/x/y and ${join(root, 'businesses')}/demo.json with ${planted}` }),
|
||||
] };
|
||||
writeFileSync(file, JSON.stringify(doc), { mode: 0o600 });
|
||||
const view = read().harnessWatch;
|
||||
const by = Object.fromEntries(view.map((e) => [e.name, e]));
|
||||
assert.deepEqual(view.map((e) => e.name), [...names, 'w3-red', 'w3-missing', 'w3-unknown', 'w3-leak'], 'the loader\'s harnesses first, then the file\'s others');
|
||||
assert.deepEqual(by[names[0]], { name: names[0], state: 'fit', version: '2.1.323', checkedAt: at, changedAt: moved, failed: [], line: 'every flag the adapter passes is in --help' });
|
||||
assert.deepEqual(by['w3-red'], { name: 'w3-red', state: 'red', version: '1.0.0', checkedAt: at, changedAt: moved, failed: ['--restricted'], line: '--help lacks --restricted' });
|
||||
assert.deepEqual(by['w3-missing'], { name: 'w3-missing', state: 'missing', version: null, checkedAt: at, changedAt: moved, failed: [], line: 'no binary from MOSAIC_PI_BIN, PATH or the repo copy' });
|
||||
assert.deepEqual(by['w3-unknown'], { name: 'w3-unknown', state: 'unknown', version: '1.0.0', checkedAt: at, changedAt: moved, failed: [], line: 'inspect did not answer in time' });
|
||||
for (const name of names.slice(1)) assert.deepEqual(by[name], unchecked(name), 'a harness with no record is not checked yet');
|
||||
const leak = JSON.stringify(by['w3-leak']);
|
||||
for (const needle of [planted, dataRoot, root]) assert.ok(!leak.includes(needle), `the answer holds no ${needle === planted ? 'env value' : 'host path'}: ${leak}`);
|
||||
assert.deepEqual(by['w3-leak'].failed, ['--ok'], 'a rule id that is an env value is dropped');
|
||||
assert.match(by['w3-leak'].line, /^failed under <path>.* with <W3_PLANTED>$/);
|
||||
assert.deepEqual(JSON.parse(readFileSync(file, 'utf8')), doc, 'the broker does not write the file');
|
||||
|
||||
// A truncated file, a wrong shape and a wrong version each read as not checked yet, never a throw.
|
||||
for (const bad of [JSON.stringify(doc).slice(0, 80), JSON.stringify({ ...doc, harnesses: { [names[0]]: doc.harnesses[0] } }), JSON.stringify({ ...doc, version: 2 }), JSON.stringify([doc]), 'null', '']) {
|
||||
writeFileSync(file, bad, { mode: 0o600 });
|
||||
assert.deepEqual(read().harnessWatch, names.map(unchecked), bad.slice(0, 40));
|
||||
}
|
||||
// A record of the wrong shape reads as not checked yet for that harness only.
|
||||
writeFileSync(file, JSON.stringify({ ...doc, harnesses: [rec(names[0], { state: 'green' }), rec('w3-red', { state: 'red', fit: 'fail' }), 'x', null, rec('../x', {})] }), { mode: 0o600 });
|
||||
const odd = Object.fromEntries(read().harnessWatch.map((e) => [e.name, e]));
|
||||
assert.deepEqual(odd[names[0]], unchecked(names[0]));
|
||||
assert.equal(odd['w3-red'].state, 'red');
|
||||
assert.equal(odd['../x'], undefined, 'a name that is not a harness name is dropped');
|
||||
});
|
||||
@@ -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');
|
||||
});
|
||||
@@ -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 };
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -677,15 +677,18 @@ async function bus(rest, io, deps) {
|
||||
const priorBoot = readBootStatus(org.root);
|
||||
try { recordBootOk(org.root, { business: businessId, file }); } catch { /* status is advisory */ }
|
||||
const binding = readNotifyConfig(org.root, businessId);
|
||||
const host = await startHost({ boot, business: businessId, notifier: binding ? { binding } : null, refuseStale: true, options: { devLocalOwner: Boolean(instance), pm: parsed.flags.has("--pm"), noLaunch: !launches } }).catch((e) => {
|
||||
// A dev fixture serves only the organizations that use it; their launches carry no provider credential.
|
||||
const orgDoc = instance ? JSON.parse(readFileSync(file, "utf8")) : undefined;
|
||||
const launchEnv = instance ? fixtureLaunchEnv(orgDoc, io.env) : io.env;
|
||||
// #1873 W2: the harness watch runs in the host with the env sessions launch from, unless the
|
||||
// host launches nothing (--no-launch). deps.harnessWatch replaces its options in tests.
|
||||
const harnessWatch = launches ? (deps.harnessWatch === undefined ? {} : deps.harnessWatch) : null;
|
||||
const host = await startHost({ boot, business: businessId, notifier: binding ? { binding } : null, refuseStale: true, options: { devLocalOwner: Boolean(instance), pm: parsed.flags.has("--pm"), noLaunch: !launches }, harnessWatch: harnessWatch ? { env: launchEnv, ...harnessWatch } : null }).catch((e) => {
|
||||
// org-defscan: the broker refused a definition that holds a credential it holds; the load was not good after all.
|
||||
if (e.code === "definition-holds-credential") try { recordBootRefused(org.root, { business: businessId, file, reason: e.message, prior: priorBoot }); } catch { /* the refusal still throws */ }
|
||||
throw e;
|
||||
});
|
||||
try {
|
||||
// A dev fixture serves only the organizations that use it; their launches carry no provider credential.
|
||||
const orgDoc = instance ? JSON.parse(readFileSync(file, "utf8")) : undefined;
|
||||
const launchEnv = instance ? fixtureLaunchEnv(orgDoc, io.env) : io.env;
|
||||
// U4a (#1873): agents get the task tools when the bus serves task verbs for the organization: a
|
||||
// Vikunja tracker in the boot config, or the built-in store, which an organization with no
|
||||
// tracker gets (the bus runtime makes the same choice, runtime.mjs trackerOf).
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
// #1873 harness watch, slice W2 (reports/harness-watch-design.md section 1, "How often" and "Where
|
||||
// results are kept"): the bus host's timer over the W1 watch (packages/plugins/src/harness-watch.mjs).
|
||||
// It runs in the host process with the env sessions launch from.
|
||||
//
|
||||
// at start one full pass
|
||||
// every statMs (5 minutes) a stat pass: the stat key of every harness module the loader gives
|
||||
// (its optional watchKey({ env }), else its install roots), and a full pass when
|
||||
// the key is not the last full pass's, or when dayMs (a day) has gone by since it
|
||||
// halted/invalid while processing is halted or invalid (processingStatus), no pass at all; the
|
||||
// first tick after it clears runs a full pass
|
||||
//
|
||||
// A harness module may ask for a stat pass now (packages/plugins/src/watch-request.mjs; claude-code's
|
||||
// preflight asks when it has no verdict for its binary): it runs at once, or right after the pass
|
||||
// running, and runs a full pass only when the key changed, as a tick does.
|
||||
//
|
||||
// One pass at a time: a tick that comes while a pass runs does nothing. A pass that throws is
|
||||
// logged, with no path in the line, and the timer goes on; its key is not kept, so the next tick
|
||||
// runs a full pass again. stop() clears the timer, aborts the pass running (the watch aborts the
|
||||
// inspect it is in, which stops what it started) and waits for it; nothing is written after.
|
||||
//
|
||||
// The host is the one writer of two files under <org dataRoot>/state/:
|
||||
// harness-watch.json { version: 1, checkedAt, harnesses: [record], refused, gone }: the current
|
||||
// record per harness, as the watch returns it but without surfaces (their
|
||||
// store is slice W6). Written whole to a temp file in the same directory, then
|
||||
// renamed over the last one, 0600.
|
||||
// harness-watch.jsonl one line per transition the watch reports, appended, never rewritten.
|
||||
// The watch redacts its records (no host path, no env value). Both files are checked again before
|
||||
// they are written: a line that would hold the scratch directory, the home directory, the data
|
||||
// root or an env value of six characters or more is not written, and the pass is logged as failed.
|
||||
// The timer adds no model call and no network call: the stat pass reads file metadata only, and a
|
||||
// full pass is the watch's (its modules run their binaries with no network where the host allows).
|
||||
import { appendFileSync, chmodSync, closeSync, fsyncSync, mkdirSync, mkdtempSync, openSync, readFileSync, renameSync, rmSync, writeSync } from "node:fs";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { loadInstalled } from "../../plugins/src/loader.mjs";
|
||||
import { watchHarnesses, watchStateFile } from "../../plugins/src/harness-watch.mjs";
|
||||
import { statKey } from "../../plugins/src/stat-key.mjs";
|
||||
import { onWatchRequest } from "../../plugins/src/watch-request.mjs";
|
||||
import { processingStatus } from "../../processing-control/src/state.mjs";
|
||||
|
||||
export const STAT_MS = 5 * 60 * 1000;
|
||||
export const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
export const WATCH_VERSION = 1;
|
||||
// The bus's plugins verb reads this file (readWatchView in the same module as watchStateFile).
|
||||
export const watchFile = watchStateFile;
|
||||
export const watchLog = (dataRoot) => join(dataRoot, "state", "harness-watch.jsonl");
|
||||
const VALUE_MIN = 6;
|
||||
|
||||
const realClock = Object.freeze({
|
||||
now: () => Date.now(),
|
||||
setInterval: (fn, ms) => setInterval(fn, ms),
|
||||
clearInterval: (t) => clearInterval(t),
|
||||
});
|
||||
|
||||
// The stat key of every harness the loader gives: per module, [name, its key]. A module's key is
|
||||
// its watchKey({ env }) when it has one, else the stat key of its installRoots({ env }), else null.
|
||||
// A module that comes or goes changes the key too. The key holds paths; it is compared in memory
|
||||
// only.
|
||||
export async function harnessKeys({ dataRoot, env }) {
|
||||
const loaded = await loadInstalled("harness", { dataRoot });
|
||||
const out = [];
|
||||
for (const { name, module } of loaded.modules) {
|
||||
let key = null;
|
||||
try {
|
||||
if (typeof module.watchKey === "function") key = module.watchKey({ env });
|
||||
else if (typeof module.installRoots === "function") key = statKey(module.installRoots({ env }));
|
||||
} catch (e) {
|
||||
key = ["error", typeof e?.code === "string" ? e.code : "threw"];
|
||||
}
|
||||
out.push([name, typeof key === "string" || Array.isArray(key) ? key : null]);
|
||||
}
|
||||
out.push(["refused", loaded.refused.map((r) => r?.name ?? null)]);
|
||||
return JSON.stringify(out);
|
||||
}
|
||||
|
||||
// Starts the timer and its first pass. dataRoot is the organization's: the state files go under it.
|
||||
// pluginRoot (W3) is where the harness plugins load from: the instance root when there is one, as
|
||||
// the launcher loads them (launcher.mjs, system.instanceRoot ?? dataRoot); it defaults to dataRoot. Returns { stop, settled }: stop() ends it (a promise), and
|
||||
// settled() is the pass running, or a resolved promise, for tests and for stop.
|
||||
export function startHarnessWatch({
|
||||
dataRoot,
|
||||
pluginRoot = dataRoot,
|
||||
env = process.env,
|
||||
log = () => {},
|
||||
clock = realClock,
|
||||
processing = () => processingStatus(dataRoot),
|
||||
watch = watchHarnesses,
|
||||
keys = () => harnessKeys({ dataRoot: pluginRoot, env }),
|
||||
statMs = STAT_MS,
|
||||
dayMs = DAY_MS,
|
||||
timeoutMs = undefined,
|
||||
scratchRoot = () => tmpdir(),
|
||||
requests = onWatchRequest,
|
||||
} = {}) {
|
||||
if (typeof dataRoot !== "string" || !dataRoot) throw new TypeError("startHarnessWatch needs the organization's dataRoot");
|
||||
if (typeof pluginRoot !== "string" || !pluginRoot) throw new TypeError("startHarnessWatch needs a plugin root");
|
||||
const control = new AbortController();
|
||||
let stopped = false;
|
||||
let running = null;
|
||||
let held = false;
|
||||
let again = false;
|
||||
let needFull = true;
|
||||
let lastKey = null;
|
||||
let lastFull = null;
|
||||
let last = readRecords(dataRoot);
|
||||
|
||||
async function pass() {
|
||||
let status;
|
||||
try { status = processing(); } catch { status = { invalid: true }; }
|
||||
if (status?.halted || status?.invalid) {
|
||||
if (!held) log("harness watch: processing is halted or invalid, so passes wait for it");
|
||||
held = true;
|
||||
return;
|
||||
}
|
||||
if (held) { held = false; needFull = true; }
|
||||
const key = await keys();
|
||||
if (stopped) return;
|
||||
const now = clock.now();
|
||||
if (!needFull && key === lastKey && lastFull !== null && now - lastFull < dayMs) return;
|
||||
const scratch = mkdtempSync(join(scratchRoot(), "mosaic-watch-"));
|
||||
try {
|
||||
const result = await watch({ last, env, scratch, dataRoot: pluginRoot, now: () => new Date(clock.now()), signal: control.signal, ...(timeoutMs ? { timeoutMs } : {}) });
|
||||
if (stopped || result?.stopped) return;
|
||||
store(dataRoot, result, { env, scratch, pluginRoot, at: new Date(now).toISOString() });
|
||||
last = result.records;
|
||||
lastKey = key;
|
||||
lastFull = now;
|
||||
needFull = false;
|
||||
} finally {
|
||||
rmSync(scratch, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
function tick() {
|
||||
if (stopped || running) return;
|
||||
running = pass()
|
||||
.catch((e) => { if (!stopped) log(`harness watch: a pass failed (${failure(e)}); the timer goes on`); })
|
||||
.finally(() => {
|
||||
running = null;
|
||||
if (again && !stopped) { again = false; tick(); }
|
||||
});
|
||||
}
|
||||
|
||||
// A request runs a tick now, or once right after the pass running.
|
||||
function request() {
|
||||
if (stopped) return;
|
||||
if (running) again = true;
|
||||
else tick();
|
||||
}
|
||||
|
||||
const timer = clock.setInterval(tick, statMs);
|
||||
timer?.unref?.();
|
||||
const unlisten = typeof requests === "function" ? requests(request) : null;
|
||||
tick();
|
||||
|
||||
return Object.freeze({
|
||||
settled: () => running ?? Promise.resolve(),
|
||||
async stop() {
|
||||
if (!stopped) {
|
||||
stopped = true;
|
||||
clock.clearInterval(timer);
|
||||
if (typeof unlisten === "function") unlisten();
|
||||
control.abort();
|
||||
}
|
||||
await (running ?? Promise.resolve());
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// What failed, without the error's text, which can name a host path.
|
||||
const failure = (e) => (typeof e?.code === "string" && /^[A-Za-z0-9_-]{1,40}$/.test(e.code) ? e.code : e?.name === "LeakError" ? "a state line held a host path or an env value" : "an error");
|
||||
|
||||
// The records of the last harness-watch.json, or none when it is missing or not this shape.
|
||||
function readRecords(dataRoot) {
|
||||
try {
|
||||
const doc = JSON.parse(readFileSync(watchFile(dataRoot), "utf8"));
|
||||
return doc?.version === WATCH_VERSION && Array.isArray(doc.harnesses) ? doc.harnesses.filter((r) => r && typeof r === "object" && typeof r.harness === "string") : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function store(dataRoot, result, { env, scratch, pluginRoot = dataRoot, at }) {
|
||||
const records = (Array.isArray(result?.records) ? result.records : []).map(({ surfaces, ...rest }) => rest);
|
||||
const doc = { version: WATCH_VERSION, checkedAt: at, harnesses: records, refused: Array.isArray(result?.refused) ? result.refused : [], gone: Array.isArray(result?.gone) ? result.gone : [] };
|
||||
const transitions = Array.isArray(result?.transitions) ? result.transitions : [];
|
||||
const leaks = leakChecker({ env, scratch, dataRoot, pluginRoot });
|
||||
if (leaks(doc) || transitions.some(leaks)) throw Object.assign(new Error("leak"), { name: "LeakError" });
|
||||
const text = `${JSON.stringify(doc, null, 2)}\n`;
|
||||
const lines = transitions.map((t) => `${JSON.stringify(t)}\n`).join("");
|
||||
const dir = join(dataRoot, "state");
|
||||
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
||||
const file = watchFile(dataRoot);
|
||||
const tmp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
||||
try {
|
||||
const fd = openSync(tmp, "wx", 0o600);
|
||||
try { writeSync(fd, text); fsyncSync(fd); } finally { closeSync(fd); }
|
||||
renameSync(tmp, file);
|
||||
} catch (e) {
|
||||
rmSync(tmp, { force: true });
|
||||
throw e;
|
||||
}
|
||||
chmodSync(file, 0o600);
|
||||
if (lines) appendFileSync(watchLog(dataRoot), lines, { mode: 0o600 });
|
||||
}
|
||||
|
||||
// Whether a value holds a path the host knows (the scratch directory, the home directory, the data
|
||||
// root) or an env value of VALUE_MIN characters or more, the host's or the launch env's, in any of
|
||||
// its strings. Names the watch sets itself are not text and are skipped: a harness or module name
|
||||
// (an env value may name the harness), a lookup step, a state, a fit, a via, a time or a digest.
|
||||
const NOT_TEXT = new Set(["harness", "gone", "name", "kept", "source", "code", "state", "fit", "via", "at", "checkedAt", "changedAt", "digest", "lastDigest"]);
|
||||
export function leakChecker({ env, scratch, dataRoot, pluginRoot = null }) {
|
||||
const needles = new Set();
|
||||
const add = (v) => { if (typeof v === "string" && v.length >= VALUE_MIN) needles.add(v); };
|
||||
for (const p of [scratch, dataRoot, pluginRoot, homedir()]) add(p);
|
||||
for (const source of [env ?? {}, process.env]) for (const v of Object.values(source)) add(v);
|
||||
const holds = (text) => { for (const n of needles) if (text.includes(n)) return true; return false; };
|
||||
const walk = (v, key) => {
|
||||
if (typeof v === "string") return !NOT_TEXT.has(key) && holds(v);
|
||||
if (Array.isArray(v)) return v.some((x) => walk(x, key));
|
||||
if (v && typeof v === "object") return Object.entries(v).some(([k, x]) => walk(x, k));
|
||||
return false;
|
||||
};
|
||||
return (value) => walk(value, null);
|
||||
}
|
||||
@@ -22,6 +22,7 @@ import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { CliError } from "./errors.mjs";
|
||||
import { cmdlineOf, startTimeOf } from "../../seat/src/proc.mjs";
|
||||
import { startHarnessWatch } from "./harness-watch-host.mjs";
|
||||
|
||||
const BROKER = fileURLToPath(new URL("../../bus/src/process.mjs", import.meta.url));
|
||||
const NOTIFIER = fileURLToPath(new URL("./notifier-process.mjs", import.meta.url));
|
||||
@@ -175,7 +176,10 @@ export function watchChildren(children, onDeath) {
|
||||
// {binding, base?, pollMs?}; base and pollMs exist for the tests, and the
|
||||
// command line never sets them, nor requestTimeoutMs. Resolves once both
|
||||
// children are up.
|
||||
export async function startHost({ boot, business, notifier = null, options = null, refuseStale = false, bootTimeoutMs = BOOT_TIMEOUT_MS, requestTimeoutMs = REQUEST_TIMEOUT_MS, log = (l) => process.stderr.write(`mosaic-bus: ${l}\n`) }) {
|
||||
// harnessWatch (#1873 W2): { env, ...options } starts the harness watch's timer in this process
|
||||
// once the host is up (harness-watch-host.mjs), with env the environment sessions launch from; the
|
||||
// host's close stops it first. null (the default) starts none.
|
||||
export async function startHost({ boot, business, notifier = null, options = null, refuseStale = false, bootTimeoutMs = BOOT_TIMEOUT_MS, requestTimeoutMs = REQUEST_TIMEOUT_MS, log = (l) => process.stderr.write(`mosaic-bus: ${l}\n`), harnessWatch = null }) {
|
||||
const dataRoot = boot.dataRoot;
|
||||
// A host started before host.lock existed holds only host.json.
|
||||
const prior = readHostState(dataRoot);
|
||||
@@ -372,6 +376,13 @@ export async function startHost({ boot, business, notifier = null, options = nul
|
||||
log(`${name} exited (${signal ?? code}); stopping the host`);
|
||||
close(1);
|
||||
});
|
||||
// #1873 W2: the harness watch's timer, stopped before anything else when the host closes.
|
||||
if (harnessWatch) {
|
||||
// W3: plugins load from the instance root when there is one, as the launcher loads them; the
|
||||
// state files stay under the organization's dataRoot.
|
||||
const watch = startHarnessWatch({ log, pluginRoot: boot.instanceRoot ?? dataRoot, ...harnessWatch, dataRoot });
|
||||
hooks.unshift(() => watch.stop());
|
||||
}
|
||||
|
||||
// #1873 CB3: the copies the broker's boot proved live, names only; anything malformed counts as none.
|
||||
const copyBound = {};
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
// #1873 harness watch, slice W3: `mosaic bus start ORG --internal-host` starts the harness watch in
|
||||
// the host (through deps.harnessWatch, a stand-in watch), with the instance root as the root the
|
||||
// harness plugins load from and the organization's root for its state files, as the launcher loads
|
||||
// them; `--no-launch` starts no watch. The host runs in this process and stops on SIGTERM, as the
|
||||
// unit's stop does. Nothing is launched and no live port is used.
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
|
||||
import { join, resolve } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import * as org from '../../organization/src/org-service.mjs';
|
||||
import { main } from '../src/cli.mjs';
|
||||
import { watchFile } from '../src/harness-watch-host.mjs';
|
||||
import { humanEnv } from './helpers.mjs';
|
||||
|
||||
const repo = resolve(import.meta.dirname, '../../..');
|
||||
|
||||
async function instance(t) {
|
||||
const home = mkdtempSync(join(tmpdir(), 'w3-'));
|
||||
t.after(() => rmSync(home, { recursive: true, force: true }));
|
||||
const env = humanEnv({ HOME: home, MOSAIC_CONFIG: join(home, '.config/mosaic-w3/config.json') });
|
||||
const boot = spawnSync(join(repo, 'scripts/bootstrap.sh'), ['--instance', 'w3'], { env, encoding: 'utf8' });
|
||||
assert.equal(boot.status, 0, boot.stderr);
|
||||
const ctx = { configDir: join(home, '.config/mosaic-w3'), dataRoot: join(home, '.mosaic-w3'), instance: 'w3', env, principal: { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' } };
|
||||
await org.createOrg(ctx, 'alpha', await org.templateRender('alpha', { fixture: 'local' }));
|
||||
return { ctx, env, orgRoot: join(ctx.dataRoot, 'org-runtime', 'alpha') };
|
||||
}
|
||||
|
||||
// Runs the host in this process until `until` says so, then stops it as SIGTERM does.
|
||||
async function hostRun(args, env, deps, until) {
|
||||
let out = '', err = '';
|
||||
const io = { env, stdin: null, stdout: { write: (s) => { out += s; return true; } }, stderr: { write: (s) => { err += s; return true; } } };
|
||||
const listeners = process.listenerCount('SIGTERM');
|
||||
const running = main(['bus', 'start', 'alpha', '--internal-host', ...args], io, deps);
|
||||
let finished = false;
|
||||
running.finally(() => { finished = true; });
|
||||
const end = Date.now() + 30000;
|
||||
// The host prints its line before bus start takes SIGTERM, so the stop waits for its listener too.
|
||||
while (!(out.includes('bus host up') && process.listenerCount('SIGTERM') > listeners && until()) && !finished && Date.now() < end) await new Promise((r) => setTimeout(r, 20));
|
||||
const up = out.includes('bus host up');
|
||||
if (!finished) process.emit('SIGTERM');
|
||||
const code = await running;
|
||||
return { code, out, err, up };
|
||||
}
|
||||
|
||||
test('W3 bus start starts the harness watch in the host, loading plugins from the instance root and writing state under the organization root', { timeout: 90000 }, async (t) => {
|
||||
const { ctx, env, orgRoot } = await instance(t);
|
||||
const calls = [];
|
||||
const scratch = mkdtempSync(join(tmpdir(), 'w3-scratch-'));
|
||||
t.after(() => rmSync(scratch, { recursive: true, force: true }));
|
||||
const record = { harness: 'pi', state: 'fit', via: 'inspect', found: true, source: 'PATH', version: '1.6.0', digest: 'b'.repeat(64), lastDigest: 'b'.repeat(64), fit: 'pass', definite: true, failed: [], line: 'pi 1.6.0 passed the fit check', checkedAt: '2026-11-20T10:00:00.000Z', changedAt: '2026-11-20T10:00:00.000Z' };
|
||||
const harnessWatch = {
|
||||
watch: async (opts) => { calls.push(opts); return { records: [record], transitions: [], refused: [], gone: [] }; },
|
||||
keys: async () => 'k',
|
||||
processing: () => ({ halted: false }),
|
||||
scratchRoot: () => scratch,
|
||||
};
|
||||
const run = await hostRun([], env, { harnessWatch }, () => calls.length > 0 && existsSync(watchFile(orgRoot)));
|
||||
assert.equal(run.up, true, run.err);
|
||||
assert.match(run.out, /bus host stopped \(0\)/, run.err);
|
||||
assert.equal(calls.length, 1, 'the start pass ran in the host');
|
||||
assert.notEqual(orgRoot, ctx.dataRoot);
|
||||
assert.equal(calls[0].dataRoot, ctx.dataRoot, 'the watch loads harness plugins from the instance root, as the launcher does');
|
||||
assert.equal(calls[0].signal.aborted, true, 'the stop aborted the watch');
|
||||
assert.deepEqual(JSON.parse(readFileSync(watchFile(orgRoot), 'utf8')).harnesses.map((r) => r.harness), ['pi'], 'its state file is under the organization root');
|
||||
assert.equal(existsSync(watchFile(ctx.dataRoot)), false, 'and not under the instance root');
|
||||
});
|
||||
|
||||
test('W3 bus start --no-launch starts no harness watch', { timeout: 90000 }, async (t) => {
|
||||
const { env, orgRoot } = await instance(t);
|
||||
const calls = [];
|
||||
const harnessWatch = { watch: async (opts) => { calls.push(opts); return { records: [], transitions: [], refused: [], gone: [] }; }, keys: async () => { calls.push('keys'); return 'k'; }, processing: () => ({ halted: false }), scratchRoot: () => tmpdir() };
|
||||
// A watch starts with the host and runs its start pass at once, so the host stays up 500 ms after
|
||||
// its line before the stop.
|
||||
let upAt = null;
|
||||
const run = await hostRun(['--no-launch'], env, { harnessWatch }, () => { upAt ??= Date.now(); return Date.now() - upAt >= 500; });
|
||||
assert.equal(run.up, true, run.err);
|
||||
assert.match(run.out, /launches disabled/);
|
||||
assert.match(run.out, /bus host stopped \(0\)/, run.err);
|
||||
assert.deepEqual(calls, [], 'no watch, no key read');
|
||||
assert.equal(existsSync(watchFile(orgRoot)), false);
|
||||
});
|
||||
@@ -30,9 +30,10 @@ function fixture(t) {
|
||||
const actual = { Id: 'sha256:' + 'a'.repeat(64), Os: 'linux', Architecture: 'amd64' };
|
||||
function base(root) { return /^FROM (\S+)$/m.exec(readFileSync(join(root, 'Containerfile'), 'utf8'))[1]; }
|
||||
function receipt(root) {
|
||||
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'));
|
||||
// #1873 S5: the tag carries the pi version the lock resolves, never the package.json range.
|
||||
const locked = JSON.parse(readFileSync(join(root, 'package-lock.json'), 'utf8')).packages['node_modules/@earendil-works/pi-coding-agent'].version;
|
||||
const release = readFileSync(join(root, 'RELEASE'), 'utf8').trim();
|
||||
return { inputDigest: imageFingerprint(root), imageId: actual.Id, tag: `mosaic-poc-agent:${pkg.dependencies['@earendil-works/pi-coding-agent']}-r${release}`, baseDigest: base(root), platform: 'linux/amd64' };
|
||||
return { inputDigest: imageFingerprint(root), imageId: actual.Id, tag: `mosaic-poc-agent:${locked}-r${release}`, baseDigest: base(root), platform: 'linux/amd64' };
|
||||
}
|
||||
|
||||
test('image receipt records the actual pinned FROM rather than a stale mutable tag', t => {
|
||||
@@ -43,14 +44,15 @@ test('image receipt records the actual pinned FROM rather than a stale mutable t
|
||||
const calls = [];
|
||||
const invoke = args => {
|
||||
calls.push(args);
|
||||
if (args[0] === 'compose') return '';
|
||||
if (args[0] === 'build') return '';
|
||||
if (args[2] === 'node:24-bookworm-slim') return JSON.stringify({ RepoDigests: ['node@sha256:' + 'b'.repeat(64)] });
|
||||
return JSON.stringify(actual);
|
||||
};
|
||||
const result = prepareImage(root, { grant: 'fixture-only', tempRoot: root }, invoke);
|
||||
// The fixture has no node_modules, so a stand-in takes the image fit step's place (#1873 S5).
|
||||
const result = prepareImage(root, { grant: 'fixture-only', tempRoot: root }, invoke, () => {});
|
||||
assert.equal(result.baseDigest, selected);
|
||||
assert.equal(calls.some(args => args[2] === 'node:24-bookworm-slim'), false);
|
||||
assert.equal(calls.filter(args => args[0] === 'compose').length, 1);
|
||||
assert.equal(calls.filter(args => args[0] === 'build').length, 1);
|
||||
});
|
||||
|
||||
test('a well-formed stale base receipt refuses before any image inspection', t => {
|
||||
|
||||
@@ -5,6 +5,12 @@ const image=await import('../../../scripts/testing/lane-image.mjs').catch(e=>{if
|
||||
const docker=await import('../../../scripts/testing/lane-docker.mjs').catch(e=>{if(e.code!=='ERR_MODULE_NOT_FOUND')throw e;return {};});
|
||||
|
||||
function fixtureBase(root){return /^FROM (\S+)$/m.exec(readFileSync(join(root,'Containerfile'),'utf8'))[1];}
|
||||
// #1873 S5: prepareImage runs the image fit step before the build; these fixtures have no node_modules,
|
||||
// so they pass a stand-in step that records that it ran.
|
||||
const fitStandIn=calls=>checkout=>calls.push(['fit',checkout]);
|
||||
// The tag the fixture's copied lock gives (RELEASE 0.0.1), read here from the lock, so these tests
|
||||
// follow the lock when it resolves a newer pi.
|
||||
const lockTagOf=root=>'mosaic-poc-agent:'+JSON.parse(readFileSync(join(root,'package-lock.json'),'utf8')).packages['node_modules/@earendil-works/pi-coding-agent'].version+'-r0.0.1';
|
||||
const processingInputs=['packages/processing-control/src/state.mjs','packages/processing-control/src/entrypoint.mjs'];
|
||||
function copyProcessingInputs(root){
|
||||
mkdirSync(join(root,'packages/processing-control/src'),{recursive:true});
|
||||
@@ -18,7 +24,7 @@ test('a changed COPY input or actual image ID refuses compatible image sharing',
|
||||
copyProcessingInputs(root);
|
||||
writeFileSync(join(root,'RELEASE'),'0.0.1\n');
|
||||
for(const d of ['contracts','src','adapters']){mkdirSync(join(root,d));writeFileSync(join(root,d,'one'),'same');}
|
||||
const digest=image.imageFingerprint(root),id='sha256:'+'a'.repeat(64);const receipt={inputDigest:digest,imageId:id,tag:'mosaic-poc-agent:0.85.1-r0.0.1',baseDigest:fixtureBase(root),platform:'linux/amd64'};
|
||||
const digest=image.imageFingerprint(root),id='sha256:'+'a'.repeat(64);const receipt={inputDigest:digest,imageId:id,tag:lockTagOf(root),baseDigest:fixtureBase(root),platform:'linux/amd64'};
|
||||
const inspect=()=>({Id:id,Os:'linux',Architecture:'amd64'});
|
||||
assert.equal(image.verifyImage(receipt,root,inspect).imageId,id);
|
||||
assert.throws(()=>image.verifyImage(receipt,root,()=>({...inspect(),Id:'sha256:'+'c'.repeat(64)})),/image ID/);
|
||||
@@ -36,9 +42,10 @@ test('exclusive image preparation records actual image ID and build wall time an
|
||||
for(const f of ['Containerfile','package.json','package-lock.json'])cpSync(new URL('../../../'+f,import.meta.url),join(root,f));
|
||||
copyProcessingInputs(root);
|
||||
writeFileSync(join(root,'RELEASE'),'0.0.1\n');for(const d of ['contracts','src','adapters'])mkdirSync(join(root,d));
|
||||
const calls=[];const invoke=(args)=>{calls.push(args);if(args[0]==='compose')return '';return JSON.stringify({Id:'sha256:'+'a'.repeat(64),Os:'linux',Architecture:'amd64'});};
|
||||
const calls=[];const invoke=(args)=>{calls.push(args);if(args[0]==='build')return '';return JSON.stringify({Id:'sha256:'+'a'.repeat(64),Os:'linux',Architecture:'amd64'});};
|
||||
assert.throws(()=>image.prepareImage(root,{grant:'',tempRoot:root},invoke),/grant/);
|
||||
const result=image.prepareImage(root,{grant:'fixture',tempRoot:root},invoke);assert.equal(result.imageId,'sha256:'+'a'.repeat(64));assert.equal(result.builds,1);assert.ok(result.buildWallMs>=0);assert.ok(calls.some(x=>x.join(' ')==='compose build'));
|
||||
const result=image.prepareImage(root,{grant:'fixture',tempRoot:root},invoke,fitStandIn(calls));assert.equal(result.imageId,'sha256:'+'a'.repeat(64));assert.equal(result.builds,1);assert.ok(result.buildWallMs>=0);assert.ok(calls.some(x=>x.join(' ')==='build -f Containerfile -t '+lockTagOf(root)+' .'),'a plain docker build of the Containerfile under the lock tag (compose.yaml has no build section)');
|
||||
const fitAt=calls.findIndex(x=>x[0]==='fit');assert.ok(fitAt>=0&&fitAt<calls.findIndex(x=>x[0]==='build'),'the image fit step runs, before the build');
|
||||
});
|
||||
test('cleanup refuses a foreign Docker label and removes only an exact owned project',()=>{
|
||||
const project='mg197-a-abcdef123456',id='123456789abc';let removed=false,label='foreign';
|
||||
@@ -52,7 +59,7 @@ function imageFixture(t){
|
||||
for(const f of ['Containerfile','package.json','package-lock.json'])cpSync(new URL('../../../'+f,import.meta.url),join(root,f));
|
||||
copyProcessingInputs(root);
|
||||
writeFileSync(join(root,'RELEASE'),'0.0.1\n');for(const d of ['contracts','src','adapters'])mkdirSync(join(root,d));
|
||||
const receipt={inputDigest:image.imageFingerprint(root),imageId:'sha256:'+'a'.repeat(64),tag:'mosaic-poc-agent:0.85.1-r0.0.1',baseDigest:fixtureBase(root),platform:'linux/amd64'};
|
||||
const receipt={inputDigest:image.imageFingerprint(root),imageId:'sha256:'+'a'.repeat(64),tag:lockTagOf(root),baseDigest:fixtureBase(root),platform:'linux/amd64'};
|
||||
return {root,receipt};
|
||||
}
|
||||
test('missing receipt and missing image refuse without permitting a gate',t=>{
|
||||
@@ -73,12 +80,12 @@ test('a tag changed between supervised steps fails the next boundary and suppres
|
||||
});
|
||||
test('pinned-base rebuilds with identical files have distinct image IDs; cold and warm counters stay separate',t=>{
|
||||
const {root}=imageFixture(t);let builds=0,id='sha256:'+'a'.repeat(64);
|
||||
const fake=args=>{if(args[0]==='compose'){builds++;return 'fixture build';}return JSON.stringify({Id:id,Os:'linux',Architecture:'amd64'});};
|
||||
const cold=image.prepareImage(root,{grant:'fixture-cold',tempRoot:root},fake);
|
||||
const fake=args=>{if(args[0]==='build'){builds++;return 'fixture build';}return JSON.stringify({Id:id,Os:'linux',Architecture:'amd64'});};
|
||||
const cold=image.prepareImage(root,{grant:'fixture-cold',tempRoot:root},fake,fitStandIn([]));
|
||||
assert.equal(cold.builds,1);assert.ok(cold.buildWallMs>=0);assert.equal(builds,1);
|
||||
for(let i=0;i<3;i++)image.verifyImage(cold,root,()=>({Id:id,Os:'linux',Architecture:'amd64'}));
|
||||
assert.equal(builds,1,'warm verification builds zero images');
|
||||
id='sha256:'+'c'.repeat(64);const rebuilt=image.prepareImage(root,{grant:'fixture-rebuild',tempRoot:root},fake);
|
||||
id='sha256:'+'c'.repeat(64);const rebuilt=image.prepareImage(root,{grant:'fixture-rebuild',tempRoot:root},fake,fitStandIn([]));
|
||||
assert.equal(rebuilt.inputDigest,cold.inputDigest);assert.equal(rebuilt.builds,1);assert.equal(builds,2);
|
||||
assert.throws(()=>image.verifyImage(cold,root,()=>({Id:rebuilt.imageId,Os:'linux',Architecture:'amd64'})),/image ID/);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,495 @@
|
||||
// #1873 harness watch, slice W2: the bus host's timer over the watch (src/harness-watch-host.mjs)
|
||||
// and its two state files. A fake clock and stand-in watches; one run goes through the real watch
|
||||
// with stand-in harnesses, and one through startHost.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, utimesSync, writeFileSync } from "node:fs";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { DAY_MS, STAT_MS, leakChecker, startHarnessWatch, watchFile, watchLog } from "../src/harness-watch-host.mjs";
|
||||
import { bootConfig, loadSystem } from "../src/config.mjs";
|
||||
import { startHost } from "../src/host.mjs";
|
||||
import { makeDeployment } from "../../discord/tests/helpers.mjs";
|
||||
import { fixture, tmp } from "./helpers.mjs";
|
||||
import { requestWatch } from "../../plugins/src/watch-request.mjs";
|
||||
import { pluginPaths } from "../../plugins/src/pinned.mjs";
|
||||
import * as claude from "../../plugins/harness/claude-code/index.mjs";
|
||||
|
||||
function scratch(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), "mosaic-hw-"));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
return root;
|
||||
}
|
||||
|
||||
// A clock the test moves: fire() runs the interval's callback, as the timer would every statMs.
|
||||
function fakeClock(start = Date.parse("2026-11-20T00:00:00Z")) {
|
||||
const c = { at: start, fn: null, ms: null, cleared: [], handle: { fake: true } };
|
||||
return Object.assign(c, {
|
||||
now: () => c.at,
|
||||
setInterval: (fn, ms) => { c.fn = fn; c.ms = ms; return c.handle; },
|
||||
clearInterval: (h) => c.cleared.push(h),
|
||||
fire: () => c.fn(),
|
||||
});
|
||||
}
|
||||
|
||||
const record = (over = {}) => ({ harness: "claude-code", state: "fit", via: "inspect", found: true, source: "MOSAIC_CLAUDE_BIN", version: "1.0.0", digest: "a".repeat(64), lastDigest: "a".repeat(64), fit: "pass", definite: true, failed: [], line: "ok", surfaces: { help: "usage" }, checkedAt: "2026-11-20T00:00:00.000Z", changedAt: "2026-11-20T00:00:00.000Z", ...over });
|
||||
const transition = (over = {}) => ({ harness: "claude-code", at: "2026-11-20T00:00:00.000Z", from: null, to: { state: "fit", version: "1.0.0", digest: "a".repeat(64) }, failed: [], line: "ok", ...over });
|
||||
|
||||
// A stand-in watch that counts its calls and answers what the test sets next.
|
||||
function stubWatch(answer = () => ({ records: [record()], transitions: [], refused: [], gone: [] })) {
|
||||
const calls = [];
|
||||
const watch = async (opts) => { calls.push(opts); return answer(opts, calls.length); };
|
||||
return { calls, watch };
|
||||
}
|
||||
|
||||
function start(t, over = {}) {
|
||||
const dataRoot = over.dataRoot ?? scratch(t);
|
||||
const clock = over.clock ?? fakeClock();
|
||||
const logs = [];
|
||||
let key = "k1";
|
||||
let status = { halted: false };
|
||||
const keyCalls = [];
|
||||
const w = over.stub ?? stubWatch();
|
||||
const handle = startHarnessWatch({
|
||||
dataRoot, clock, log: (l) => logs.push(l), env: over.env ?? {}, watch: w.watch,
|
||||
keys: async () => { keyCalls.push(key); return key; },
|
||||
processing: () => status,
|
||||
scratchRoot: () => over.scratchRoot ?? scratch(t),
|
||||
});
|
||||
t.after(() => handle.stop());
|
||||
return { dataRoot, clock, logs, handle, w, keyCalls, setKey: (k) => { key = k; }, setStatus: (s) => { status = s; } };
|
||||
}
|
||||
|
||||
async function tick(s) {
|
||||
s.clock.fire();
|
||||
await s.handle.settled();
|
||||
}
|
||||
|
||||
test("W2 host watch: one full pass at start; a stat pass with no change runs none; a changed key runs one; a day runs one", async (t) => {
|
||||
const s = start(t);
|
||||
await s.handle.settled();
|
||||
assert.equal(s.clock.ms, STAT_MS);
|
||||
assert.equal(s.w.calls.length, 1, "the start pass");
|
||||
assert.ok(existsSync(watchFile(s.dataRoot)));
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.deepEqual([s.w.calls.length, s.keyCalls.length], [1, 2], "a stat pass reads the key and runs no full pass");
|
||||
s.setKey("k2");
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "a changed key runs one full pass");
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "and only one");
|
||||
s.clock.at += DAY_MS - 2 * STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "less than a day since the last full pass");
|
||||
s.clock.at += 2 * STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 3, "the daily pass");
|
||||
// The watch gets the last records, the clock, a scratch directory and the stop signal.
|
||||
const opts = s.w.calls[2];
|
||||
assert.equal(opts.last[0].harness, "claude-code");
|
||||
assert.equal(opts.now().getTime(), s.clock.at);
|
||||
assert.equal(typeof opts.scratch, "string");
|
||||
assert.equal(existsSync(opts.scratch), false, "its scratch directory is removed after the pass");
|
||||
assert.equal(opts.signal.aborted, false);
|
||||
});
|
||||
|
||||
test("W2 host watch: while processing is halted or invalid no pass runs; the first tick after resume runs one full pass", async (t) => {
|
||||
const s = start(t);
|
||||
await s.handle.settled();
|
||||
s.setStatus({ halted: true });
|
||||
for (let i = 0; i < 3; i++) { s.clock.at += STAT_MS; await tick(s); }
|
||||
assert.deepEqual([s.w.calls.length, s.keyCalls.length], [1, 1], "halted: no key read, no pass");
|
||||
s.setStatus({ halted: null, invalid: true, code: "processing-state-invalid" });
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 1, "invalid: no pass");
|
||||
assert.equal(s.logs.filter((l) => /halted or invalid/.test(l)).length, 1, "logged once while held");
|
||||
s.setStatus({ halted: false });
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "resume runs a full pass though the key did not change");
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "once");
|
||||
});
|
||||
|
||||
test("W2 host watch: a processing state that can't be read holds the start pass", async (t) => {
|
||||
const dataRoot = scratch(t);
|
||||
const clock = fakeClock();
|
||||
const w = stubWatch();
|
||||
const h = startHarnessWatch({ dataRoot, clock, watch: w.watch, keys: async () => "k", processing: () => { throw new Error("unreadable"); }, scratchRoot: () => scratch(t) });
|
||||
t.after(() => h.stop());
|
||||
await h.settled();
|
||||
assert.equal(w.calls.length, 0);
|
||||
});
|
||||
|
||||
test("W2 host watch: passes never overlap; a tick while one runs does nothing", async (t) => {
|
||||
let release;
|
||||
const stub = stubWatch((opts, n) => n === 1 ? new Promise((done) => { release = () => done({ records: [record()], transitions: [], refused: [], gone: [] }); }) : { records: [record()], transitions: [], refused: [], gone: [] });
|
||||
const s = start(t, { stub });
|
||||
await new Promise((done) => setImmediate(done));
|
||||
assert.equal(s.w.calls.length, 1);
|
||||
const running = s.handle.settled();
|
||||
for (let i = 0; i < 5; i++) { s.setKey(`k${i + 2}`); s.clock.at += STAT_MS; s.clock.fire(); }
|
||||
await new Promise((done) => setImmediate(done));
|
||||
assert.deepEqual([s.w.calls.length, s.keyCalls.length], [1, 1], "no second pass and no key read while the first runs");
|
||||
release();
|
||||
await running;
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "the next tick after it runs normally");
|
||||
});
|
||||
|
||||
test("W2 host watch: a pass that throws is logged with no path and the timer goes on", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const stub = stubWatch((opts, n) => { if (n === 1) throw Object.assign(new Error(`boom at ${dir}/secret`), {}); if (n === 2) throw Object.assign(new Error(`eacces ${dir}`), { code: "EACCES" }); return { records: [record()], transitions: [], refused: [], gone: [] }; });
|
||||
const s = start(t, { stub });
|
||||
await s.handle.settled();
|
||||
assert.equal(s.logs.length, 1);
|
||||
assert.match(s.logs[0], /harness watch: a pass failed \(an error\); the timer goes on/);
|
||||
assert.ok(!s.logs[0].includes(dir));
|
||||
assert.equal(existsSync(watchFile(s.dataRoot)), false);
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 2, "the key was not kept, so the next tick runs a full pass again");
|
||||
assert.match(s.logs[1], /\(EACCES\)/);
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(s.w.calls.length, 3);
|
||||
assert.ok(existsSync(watchFile(s.dataRoot)));
|
||||
assert.deepEqual(s.clock.cleared, [], "the timer was never cleared");
|
||||
// A keys read that throws is a failed pass too.
|
||||
const k = startHarnessWatch({ dataRoot: scratch(t), clock: fakeClock(), log: (l) => s.logs.push(l), watch: stubWatch().watch, keys: async () => { throw new Error(dir); }, processing: () => ({ halted: false }), scratchRoot: () => scratch(t) });
|
||||
t.after(() => k.stop());
|
||||
await k.settled();
|
||||
assert.match(s.logs.at(-1), /a pass failed \(an error\)/);
|
||||
});
|
||||
|
||||
test("W2 host watch: stop clears the timer, aborts the pass running, waits for it, and writes nothing after", async (t) => {
|
||||
let seen = null;
|
||||
let ended = false;
|
||||
const stub = stubWatch((opts) => new Promise((done) => {
|
||||
seen = opts.signal;
|
||||
opts.signal.addEventListener("abort", () => setTimeout(() => { ended = true; done({ records: [record()], transitions: [transition()], refused: [], gone: [], stopped: true }); }, 50));
|
||||
}));
|
||||
const s = start(t, { stub });
|
||||
await new Promise((done) => setImmediate(done));
|
||||
assert.equal(seen.aborted, false);
|
||||
await s.handle.stop();
|
||||
assert.equal(seen.aborted, true);
|
||||
assert.equal(ended, true, "stop waited for the pass to end");
|
||||
assert.deepEqual(s.clock.cleared, [s.clock.handle]);
|
||||
assert.equal(existsSync(watchFile(s.dataRoot)), false);
|
||||
assert.equal(existsSync(watchLog(s.dataRoot)), false);
|
||||
s.clock.fire();
|
||||
await s.handle.settled();
|
||||
assert.equal(s.w.calls.length, 1, "a tick after stop does nothing");
|
||||
assert.deepEqual(readdirSync(stub.calls[0].scratch.replace(/\/[^/]+$/, "")), [], "the pass's scratch directory is gone");
|
||||
// A watch that ignores the signal and answers normally still writes nothing once stopped.
|
||||
const late = start(t, { stub: stubWatch((opts) => new Promise((done) => opts.signal.addEventListener("abort", () => done({ records: [record()], transitions: [transition()], refused: [], gone: [] })))) });
|
||||
await new Promise((done) => setImmediate(done));
|
||||
await late.handle.stop();
|
||||
assert.equal(existsSync(watchFile(late.dataRoot)), false);
|
||||
assert.deepEqual(late.logs, []);
|
||||
});
|
||||
|
||||
test("W2 host watch: the json is replaced whole and holds no surfaces; the jsonl only grows, one line per transition", async (t) => {
|
||||
let answer = { records: [record()], transitions: [transition()], refused: [{ event: "plugin-refused", kind: "harness", name: "odd", code: "plugin-interface", message: "x" }], gone: [] };
|
||||
const s = start(t, { stub: stubWatch(() => answer) });
|
||||
await s.handle.settled();
|
||||
const file = watchFile(s.dataRoot);
|
||||
const first = JSON.parse(readFileSync(file, "utf8"));
|
||||
assert.deepEqual(Object.keys(first).sort(), ["checkedAt", "gone", "harnesses", "refused", "version"]);
|
||||
assert.equal(first.version, 1);
|
||||
assert.equal(first.checkedAt, new Date(s.clock.at).toISOString());
|
||||
assert.equal(first.harnesses.length, 1);
|
||||
assert.equal("surfaces" in first.harnesses[0], false, "surfaces are slice W6's");
|
||||
assert.equal(first.refused[0].name, "odd");
|
||||
assert.equal(statSync(file).mode & 0o777, 0o600);
|
||||
assert.equal(statSync(join(s.dataRoot, "state")).mode & 0o777, 0o700);
|
||||
const log = watchLog(s.dataRoot);
|
||||
assert.equal(readFileSync(log, "utf8").split("\n").filter(Boolean).length, 1);
|
||||
assert.equal(statSync(log).mode & 0o777, 0o600);
|
||||
const ino = statSync(file).ino;
|
||||
const before = readFileSync(log, "utf8");
|
||||
// A second pass with two transitions and one harness gone.
|
||||
answer = { records: [record({ state: "red", fit: "fail", failed: ["--strict-mcp-config"] })], transitions: [transition({ to: { state: "red", version: "1.0.0", digest: "a".repeat(64) } }), transition({ harness: "pi" })], refused: [], gone: ["pi"] };
|
||||
s.setKey("k2");
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
const second = JSON.parse(readFileSync(file, "utf8"));
|
||||
assert.notEqual(statSync(file).ino, ino, "renamed over, not written in place");
|
||||
assert.deepEqual([second.harnesses[0].state, second.refused, second.gone], ["red", [], ["pi"]]);
|
||||
const after = readFileSync(log, "utf8");
|
||||
assert.ok(after.startsWith(before), "the jsonl keeps every line it had");
|
||||
assert.equal(after.split("\n").filter(Boolean).length, 3);
|
||||
assert.deepEqual(after.split("\n").filter(Boolean).map((l) => JSON.parse(l).harness), ["claude-code", "claude-code", "pi"]);
|
||||
// A pass with no transition appends nothing.
|
||||
answer = { records: [record()], transitions: [], refused: [], gone: [] };
|
||||
s.setKey("k3");
|
||||
s.clock.at += STAT_MS;
|
||||
await tick(s);
|
||||
assert.equal(readFileSync(log, "utf8"), after);
|
||||
assert.deepEqual(readdirSync(join(s.dataRoot, "state")).sort(), ["harness-watch.json", "harness-watch.jsonl"], "no temp file is left");
|
||||
// A new host reads the records back as the last ones.
|
||||
const next = start(t, { dataRoot: s.dataRoot });
|
||||
await next.handle.settled();
|
||||
assert.equal(next.w.calls[0].last[0].harness, "claude-code");
|
||||
});
|
||||
|
||||
test("W2 host watch: a record or transition holding a host path or an env value is not written, and the log names no path", async (t) => {
|
||||
const secret = "fixture-secret-value-71";
|
||||
const cases = [
|
||||
(o) => ({ records: [record({ line: `found at ${o.scratch}/claude` })], transitions: [], refused: [], gone: [] }),
|
||||
() => ({ records: [record({ version: `1.0 ${homedir()}` })], transitions: [], refused: [], gone: [] }),
|
||||
() => ({ records: [record()], transitions: [transition({ line: `token ${secret}` })], refused: [], gone: [] }),
|
||||
(o) => ({ records: [record({ failed: [o.dataRoot] })], transitions: [], refused: [], gone: [] }),
|
||||
];
|
||||
for (const answer of cases) {
|
||||
const s = start(t, { env: { FIXTURE_TOKEN: secret }, stub: stubWatch(answer) });
|
||||
await s.handle.settled();
|
||||
assert.equal(existsSync(watchFile(s.dataRoot)), false);
|
||||
assert.equal(existsSync(watchLog(s.dataRoot)), false);
|
||||
assert.match(s.logs.join("\n"), /a state line held a host path or an env value/);
|
||||
for (const l of s.logs) assert.ok(!l.includes(s.dataRoot) && !l.includes(secret));
|
||||
}
|
||||
// An env value that is also a harness name or a state word does not block the write.
|
||||
const named = start(t, { env: { MOSAIC_HARNESS: "claude-code", OTHER: "unknown-ish" } });
|
||||
await named.handle.settled();
|
||||
assert.ok(existsSync(watchFile(named.dataRoot)));
|
||||
});
|
||||
|
||||
test("W2 host watch through the real watch: stand-in harnesses that print a scratch path and an env value leave neither in either file", async (t) => {
|
||||
const root = scratch(t);
|
||||
const dataRoot = join(root, "org");
|
||||
mkdirSync(dataRoot);
|
||||
const secret = "fixture-secret-value-93";
|
||||
const bin = join(root, "bin", "claude");
|
||||
mkdirSync(join(root, "bin"));
|
||||
// Its --version prints its own path and the secret; its --help lacks every adapter flag.
|
||||
writeFileSync(bin, `#!/bin/sh\ncase "$1" in\n--version) echo "1.0.0 $0 ${secret} $HOME";;\n--help) echo "usage: claude [--version] at $0";;\n*) exit 3;;\nesac\n`, { mode: 0o755 });
|
||||
const env = { MOSAIC_CLAUDE_BIN: bin, MOSAIC_PI_BIN: join(root, "no-pi"), PATH: "/usr/bin:/bin", FIXTURE_TOKEN: secret, HOME: join(root, "home") };
|
||||
const clock = fakeClock();
|
||||
const logs = [];
|
||||
const h = startHarnessWatch({ dataRoot, env, clock, log: (l) => logs.push(l), processing: () => ({ halted: false }), scratchRoot: () => root });
|
||||
t.after(() => h.stop());
|
||||
await h.settled();
|
||||
assert.deepEqual(logs, []);
|
||||
const json = readFileSync(watchFile(dataRoot), "utf8");
|
||||
const jsonl = readFileSync(watchLog(dataRoot), "utf8");
|
||||
const doc = JSON.parse(json);
|
||||
const rec = doc.harnesses.find((r) => r.source === "MOSAIC_CLAUDE_BIN");
|
||||
assert.equal(rec.state, "red");
|
||||
assert.ok(rec.failed.includes("--strict-mcp-config"));
|
||||
assert.ok(doc.harnesses.some((r) => r.state === "missing" && r.source === "MOSAIC_PI_BIN"));
|
||||
assert.equal(jsonl.split("\n").filter(Boolean).length, doc.harnesses.length, "the first pass is a transition per harness");
|
||||
for (const text of [json, jsonl]) {
|
||||
for (const needle of [root, secret, homedir(), dataRoot, bin, tmpdir()]) assert.ok(!text.includes(needle), `no ${needle === secret ? "env value" : "host path"} in a state file`);
|
||||
}
|
||||
// Its scratch directories are gone.
|
||||
assert.deepEqual(readdirSync(root).sort(), ["bin", "org"]);
|
||||
// A stat pass with nothing changed runs no full pass: the jsonl and json stay as they are.
|
||||
clock.at += STAT_MS;
|
||||
clock.fire();
|
||||
await h.settled();
|
||||
assert.equal(readFileSync(watchFile(dataRoot), "utf8"), json);
|
||||
// The binary replaced: its key changed, so the next tick runs a full pass.
|
||||
writeFileSync(bin, `#!/bin/sh\ncase "$1" in\n--version) echo "1.0.1";;\n--help) echo "usage";;\n*) exit 3;;\nesac\n`, { mode: 0o755 });
|
||||
clock.at += STAT_MS;
|
||||
clock.fire();
|
||||
await h.settled();
|
||||
assert.notEqual(readFileSync(watchFile(dataRoot), "utf8"), json, "a changed key ran a full pass");
|
||||
assert.equal(JSON.parse(readFileSync(watchFile(dataRoot), "utf8")).harnesses.find((r) => r.source === "MOSAIC_CLAUDE_BIN").version, "1.0.1");
|
||||
});
|
||||
|
||||
// Review P3-6: each needle of the backstop on its own: the data root, the home directory (with HOME
|
||||
// unset, so no env value covers it) and a value only the host process's env holds.
|
||||
test("W2 host watch: the backstop knows the data root, the home directory and the host's own env values", () => {
|
||||
const leaks = leakChecker({ env: {}, scratch: "/fixture-scratch-x", dataRoot: "/fixture-data-root/org" });
|
||||
assert.equal(leaks({ line: "at /fixture-data-root/org/state" }), true);
|
||||
assert.equal(leaks({ line: "at /fixture-scratch-x/home" }), true);
|
||||
assert.equal(leaks({ line: "nothing here" }), false);
|
||||
assert.equal(leaks({ harness: "/fixture-data-root/org" }), false, "a name field is not text");
|
||||
const saved = process.env.HOME;
|
||||
process.env.FIXTURE_HW_ONLY = "fixture-process-only-77";
|
||||
try {
|
||||
delete process.env.HOME;
|
||||
const home = homedir();
|
||||
const bare = leakChecker({ env: {}, scratch: "/fixture-scratch-x", dataRoot: "/fixture-data-root/org" });
|
||||
assert.equal(bare({ version: `1.0 ${home}` }), true, "the home directory");
|
||||
assert.equal(bare({ line: "token fixture-process-only-77" }), true, "a value of the host process's env");
|
||||
} finally {
|
||||
process.env.HOME = saved;
|
||||
delete process.env.FIXTURE_HW_ONLY;
|
||||
}
|
||||
});
|
||||
|
||||
// Review P3-5: a file: URL, a host:/path and a ~/ path a binary prints are paths too, and reach
|
||||
// neither file.
|
||||
test("W2 host watch through the real watch: a file: URL, a host:/path and a ~/ path in a version reach neither file", async (t) => {
|
||||
const root = scratch(t);
|
||||
const dataRoot = join(root, "org");
|
||||
mkdirSync(dataRoot);
|
||||
const bin = join(root, "bin", "claude");
|
||||
mkdirSync(join(root, "bin"));
|
||||
writeFileSync(bin, `#!/bin/sh\ncase "$1" in\n--version) echo "1.0 file:///opt/fixture-tree/bin/claude host:/srv/fixture-a/b ~/.local/share/fixture/claude";;\n--help) echo "usage";;\n*) exit 3;;\nesac\n`, { mode: 0o755 });
|
||||
const env = { MOSAIC_CLAUDE_BIN: bin, MOSAIC_PI_BIN: join(root, "no-pi"), PATH: "/usr/bin:/bin", HOME: join(root, "home") };
|
||||
const h = startHarnessWatch({ dataRoot, env, clock: fakeClock(), log: () => {}, processing: () => ({ halted: false }), scratchRoot: () => root });
|
||||
t.after(() => h.stop());
|
||||
await h.settled();
|
||||
const json = readFileSync(watchFile(dataRoot), "utf8");
|
||||
const jsonl = readFileSync(watchLog(dataRoot), "utf8");
|
||||
assert.equal(JSON.parse(json).harnesses.find((r) => r.source === "MOSAIC_CLAUDE_BIN").version, "1.0 <path> host:<path> <path>");
|
||||
for (const text of [json, jsonl]) for (const needle of ["fixture-tree", "/srv/", "fixture-a", "~/", "/fixture/claude"]) assert.ok(!text.includes(needle), `no ${needle} in a state file`);
|
||||
});
|
||||
|
||||
// Review P2-4: a module asks for a stat pass now (claude-code's preflight, with no verdict). It runs
|
||||
// at once, or once after the pass running, and a full pass only when the key changed.
|
||||
test("W2 host watch: a request runs a stat pass now, a full pass only on a changed key, once after a running pass, none after stop", async (t) => {
|
||||
let release = null;
|
||||
let hold = false;
|
||||
const stub = stubWatch(() => (hold ? new Promise((done) => { release = () => done({ records: [record()], transitions: [], refused: [], gone: [] }); }) : { records: [record()], transitions: [], refused: [], gone: [] }));
|
||||
const s = start(t, { stub });
|
||||
await s.handle.settled();
|
||||
assert.deepEqual([stub.calls.length, s.keyCalls.length], [1, 1]);
|
||||
requestWatch();
|
||||
await s.handle.settled();
|
||||
assert.deepEqual([stub.calls.length, s.keyCalls.length], [1, 2], "a request with the same key runs a stat pass and no full pass");
|
||||
s.setKey("k2");
|
||||
requestWatch();
|
||||
await s.handle.settled();
|
||||
assert.equal(stub.calls.length, 2, "a request after the key changed runs a full pass");
|
||||
// Requests while a pass runs: one more stat pass right after it, however many came.
|
||||
hold = true;
|
||||
s.setKey("k3");
|
||||
requestWatch();
|
||||
await new Promise((done) => setImmediate(done));
|
||||
assert.equal(stub.calls.length, 3);
|
||||
hold = false;
|
||||
s.setKey("k4");
|
||||
requestWatch();
|
||||
requestWatch();
|
||||
assert.equal(s.keyCalls.length, 4, "no stat pass while a pass runs");
|
||||
release();
|
||||
await new Promise((done) => setTimeout(done, 20));
|
||||
await s.handle.settled();
|
||||
assert.deepEqual([stub.calls.length, s.keyCalls.length], [4, 5], "one stat pass after it, which saw k4 and ran a full pass");
|
||||
await s.handle.stop();
|
||||
requestWatch();
|
||||
await s.handle.settled();
|
||||
assert.deepEqual([stub.calls.length, s.keyCalls.length], [4, 5], "after stop a request does nothing");
|
||||
});
|
||||
|
||||
// Review P2-4, through the real watch: a red claude touched after its check has a new stat key, so
|
||||
// preflight has no verdict for a moment; it asks the watch, whose pass checks it again, and preflight
|
||||
// refuses again within that pass, not at the next tick.
|
||||
test("W2 host watch through the real watch: preflight's request re-checks a red claude touched after its check", async (t) => {
|
||||
const root = scratch(t);
|
||||
const dataRoot = join(root, "org");
|
||||
mkdirSync(dataRoot);
|
||||
const bin = join(root, "bin", "claude");
|
||||
mkdirSync(join(root, "bin"));
|
||||
writeFileSync(bin, `#!/bin/sh\ncase "$1" in\n--version) echo "1.0.0";;\n--help) echo "usage: claude [--version]";;\n*) exit 3;;\nesac\n`, { mode: 0o755 });
|
||||
const env = { MOSAIC_CLAUDE_BIN: bin, MOSAIC_PI_BIN: join(root, "no-pi"), PATH: "/usr/bin:/bin", HOME: join(root, "home") };
|
||||
const launch = { ...env, ANTHROPIC_API_KEY: "fixture-api-key" };
|
||||
const clock = fakeClock();
|
||||
const h = startHarnessWatch({ dataRoot, env, clock, log: () => {}, processing: () => ({ halted: false }), scratchRoot: () => root });
|
||||
t.after(() => h.stop());
|
||||
await h.settled();
|
||||
assert.equal(claude.preflight({ env: launch })?.code, "harness-feature-missing");
|
||||
chmodSync(bin, 0o700);
|
||||
utimesSync(bin, new Date(), new Date(Date.now() + 60000));
|
||||
assert.equal(claude.preflight({ env: launch }), null, "no verdict for the new stat key: this launch is not held");
|
||||
await h.settled();
|
||||
assert.equal(claude.preflight({ env: launch })?.code, "harness-feature-missing", "the requested pass checked it again, with no tick");
|
||||
});
|
||||
|
||||
test("W2 startHost with harnessWatch starts the watch in the host, and close stops it", async (t) => {
|
||||
const root = tmp(t);
|
||||
const f = fixture(root);
|
||||
makeDeployment(root);
|
||||
const boot = bootConfig({ system: loadSystem({ env: f.env }), businessId: "acme", env: f.env });
|
||||
const clock = fakeClock();
|
||||
let signal = null;
|
||||
const stub = stubWatch((opts) => new Promise((done) => { signal = opts.signal; opts.signal.addEventListener("abort", () => done({ records: [], transitions: [], refused: [], gone: [], stopped: true })); }));
|
||||
const host = await startHost({ boot, business: "acme", log: () => {}, harnessWatch: { env: {}, clock, watch: stub.watch, keys: async () => "k", processing: () => ({ halted: false }), scratchRoot: () => scratch(t) } });
|
||||
let closed = false;
|
||||
t.after(() => (closed ? null : host.close(0)));
|
||||
await new Promise((done) => setTimeout(done, 50));
|
||||
assert.equal(stub.calls.length, 1, "the start pass runs in the host");
|
||||
assert.equal(stub.calls[0].dataRoot, boot.dataRoot);
|
||||
assert.equal(signal.aborted, false);
|
||||
assert.equal(await host.close(0), 0);
|
||||
closed = true;
|
||||
assert.equal(signal.aborted, true, "close aborted the pass");
|
||||
assert.deepEqual(clock.cleared, [clock.handle], "and cleared the timer");
|
||||
});
|
||||
|
||||
test("W3 host watch: harness plugins load from pluginRoot, the state files stay under the organization's dataRoot, and the backstop knows both", async (t) => {
|
||||
const dataRoot = scratch(t);
|
||||
const pluginRoot = scratch(t);
|
||||
const clock = fakeClock();
|
||||
const stub = stubWatch();
|
||||
const handle = startHarnessWatch({ dataRoot, pluginRoot, clock, env: {}, watch: stub.watch, keys: async () => "k", processing: () => ({ halted: false }), scratchRoot: () => scratch(t) });
|
||||
t.after(() => handle.stop());
|
||||
await handle.settled();
|
||||
assert.equal(stub.calls.length, 1);
|
||||
assert.equal(stub.calls[0].dataRoot, pluginRoot, "the watch loads harness plugins from the plugin root");
|
||||
assert.ok(existsSync(watchFile(dataRoot)), "the state file is under the organization's dataRoot");
|
||||
assert.equal(existsSync(watchFile(pluginRoot)), false, "and not under the plugin root");
|
||||
assert.equal(leakChecker({ env: {}, scratch: null, dataRoot, pluginRoot })({ line: `${pluginRoot}/plugins` }), true, "a record naming the plugin root is not written");
|
||||
assert.throws(() => startHarnessWatch({ dataRoot, pluginRoot: "", watch: stub.watch }), TypeError);
|
||||
});
|
||||
|
||||
test("W3 startHost gives the watch the instance root as its plugin root when the organization has one", async (t) => {
|
||||
const root = tmp(t);
|
||||
const f = fixture(root);
|
||||
makeDeployment(root);
|
||||
const boot = bootConfig({ system: loadSystem({ env: f.env }), businessId: "acme", env: f.env });
|
||||
const instanceRoot = scratch(t);
|
||||
const clock = fakeClock();
|
||||
const stub = stubWatch();
|
||||
const host = await startHost({ boot: { ...boot, instanceRoot }, business: "acme", log: () => {}, harnessWatch: { env: {}, clock, watch: stub.watch, keys: async () => "k", processing: () => ({ halted: false }), scratchRoot: () => scratch(t) } });
|
||||
let closed = false;
|
||||
t.after(() => (closed ? null : host.close(0)));
|
||||
for (let n = 0; n < 250 && !existsSync(watchFile(boot.dataRoot)); n++) await new Promise((done) => setTimeout(done, 20));
|
||||
assert.equal(stub.calls.length, 1);
|
||||
assert.equal(stub.calls[0].dataRoot, instanceRoot, "plugins load from the instance root, as the launcher loads them");
|
||||
assert.ok(existsSync(watchFile(boot.dataRoot)), "the state file is under the organization's root");
|
||||
assert.equal(existsSync(watchFile(instanceRoot)), false);
|
||||
assert.equal(await host.close(0), 0);
|
||||
closed = true;
|
||||
});
|
||||
|
||||
test("W3 host watch: the default stat key reads the plugin root's plugins, and a record naming the plugin root is not written", async (t) => {
|
||||
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.
|
||||
let pluginRoot;
|
||||
try { pluginRoot = mkdtempSync(join(dirname(tmpdir()), "mosaic-hw-plugins-")); }
|
||||
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();
|
||||
const logs = [];
|
||||
let line = "ok";
|
||||
const stub = stubWatch(() => ({ records: [record({ line })], transitions: [], refused: [], gone: [] }));
|
||||
const handle = startHarnessWatch({ dataRoot, pluginRoot, clock, env: {}, log: (l) => logs.push(l), watch: stub.watch, processing: () => ({ halted: false }), scratchRoot: () => scratch(t) });
|
||||
t.after(() => handle.stop());
|
||||
await handle.settled();
|
||||
assert.equal(stub.calls.length, 1);
|
||||
clock.fire(); await handle.settled();
|
||||
assert.equal(stub.calls.length, 1, "no change, no full pass");
|
||||
// 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`;
|
||||
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");
|
||||
assert.ok(!logs.join("\n").includes(pluginRoot));
|
||||
assert.ok(!readFileSync(watchFile(dataRoot), "utf8").includes(pluginRoot));
|
||||
});
|
||||
@@ -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);
|
||||
|
||||
@@ -0,0 +1,300 @@
|
||||
// #1873 pi-unpin S5: package.json gives pi a range, the image tag comes from the version
|
||||
// package-lock.json resolves, an image fit step runs the fit check on the locked pi before the
|
||||
// image build, and the host launcher records pi's version without comparing it to anything.
|
||||
// The image-building scripts (build.sh, release.sh, verify.sh, test-task.sh, test-auth.sh) need
|
||||
// Docker or an instance config, so their tag and build logic is tested here through the shell
|
||||
// functions and modules they call, against a stand-in lock.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { chmodSync, cpSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, symlinkSync, writeFileSync, existsSync } from "node:fs";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { sandboxProblem } from "../../seat/src/sandbox.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 {}; };
|
||||
const tagMod = await import("../../../scripts/image-tag.mjs").catch(missing);
|
||||
const fitMod = await import("../../../scripts/pi-fit-image.mjs").catch(missing);
|
||||
const image = await import("../../../scripts/testing/lane-image.mjs").catch(missing);
|
||||
const SKIP = sandboxProblem() ? { skip: "no working bwrap here" } : {};
|
||||
const NAME = "@earendil-works/pi-coding-agent";
|
||||
const KEY = `node_modules/${NAME}`;
|
||||
const RANGE = ">=0.85.1";
|
||||
const NODE_BIN = dirname(process.execPath);
|
||||
const PATH = `${NODE_BIN}:/usr/bin:/bin`;
|
||||
|
||||
const entry = (version) => ({ version, resolved: `https://registry.npmjs.org/${NAME}/-/pi-coding-agent-${version}.tgz`, integrity: `sha512-fixture${version}` });
|
||||
const lockOf = (version) => ({ name: "fixture", lockfileVersion: 3, requires: true, packages: { "": { name: "fixture", dependencies: { [NAME]: RANGE } }, ...(version ? { [KEY]: entry(version) } : {}) } });
|
||||
|
||||
// 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 = 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)));
|
||||
writeFileSync(join(root, "RELEASE"), `${release}\n`);
|
||||
return root;
|
||||
}
|
||||
|
||||
// npm ci's effect in a checkout: the package, its .bin link and node_modules/.package-lock.json.
|
||||
function install(root, knobs = {}, { version = "9.9.9", hidden = entry(version) } = {}) {
|
||||
const pkg = join(root, KEY);
|
||||
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: NAME, version, bin: { pi: "dist/bundle/cli.js" } }));
|
||||
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn({ version, ...knobs }), { mode: 0o755 });
|
||||
mkdirSync(join(root, "node_modules", ".bin"), { recursive: true });
|
||||
symlinkSync(`../${NAME}/dist/bundle/cli.js`, join(root, "node_modules", ".bin", "pi"));
|
||||
writeFileSync(join(root, "node_modules", ".package-lock.json"), JSON.stringify({ name: "fixture", lockfileVersion: 3, packages: hidden ? { [KEY]: hidden } : {} }));
|
||||
return pkg;
|
||||
}
|
||||
|
||||
const noFit = () => { throw new Error("the fit check must not run when the locked pi can't be told"); };
|
||||
|
||||
test("pi-unpin S5: a stand-in lock at 9.9.9 gives mosaic-poc-agent:9.9.9-r<RELEASE>, and the package.json range never appears in the tag", t => {
|
||||
assert.equal(typeof tagMod.imageTag, "function", "scripts/image-tag.mjs exports imageTag");
|
||||
const root = checkout(t);
|
||||
const tag = tagMod.imageTag(root);
|
||||
assert.equal(tag, "mosaic-poc-agent:9.9.9-r0.0.13");
|
||||
assert.ok(!tag.includes(RANGE) && !tag.includes("0.85.1"), "the tag carries the lock's version, not the range");
|
||||
const cli = spawnSync(process.execPath, [join(REPO, "scripts/image-tag.mjs"), root], { encoding: "utf8", env: { PATH } });
|
||||
assert.equal(cli.status, 0, cli.stderr);
|
||||
assert.equal(cli.stdout, "mosaic-poc-agent:9.9.9-r0.0.13\n");
|
||||
// The repository itself: its lock resolves 0.85.1 under the range.
|
||||
assert.equal(JSON.parse(readFileSync(join(REPO, "package.json"), "utf8")).dependencies[NAME], RANGE);
|
||||
assert.equal(tagMod.imageTag(REPO), `mosaic-poc-agent:${JSON.parse(readFileSync(join(REPO, "package-lock.json"), "utf8")).packages[KEY].version}-r${readFileSync(join(REPO, "RELEASE"), "utf8").trim()}`);
|
||||
});
|
||||
|
||||
test("pi-unpin S5: a lock with no pi entry, a range for a version, or a bad RELEASE refuses the tag, with no path in the line", t => {
|
||||
assert.equal(typeof tagMod.imageTag, "function", "scripts/image-tag.mjs exports imageTag");
|
||||
const none = checkout(t, { version: null });
|
||||
assert.throws(() => tagMod.imageTag(none), { message: `image tag: package-lock.json has no entry for ${NAME}` });
|
||||
const ranged = checkout(t);
|
||||
writeFileSync(join(ranged, "package-lock.json"), JSON.stringify({ lockfileVersion: 3, packages: { [KEY]: { ...entry("9.9.9"), version: RANGE } } }));
|
||||
assert.throws(() => tagMod.imageTag(ranged), { message: `image tag: package-lock.json's ${NAME} version isn't a plain version` });
|
||||
const gone = checkout(t);
|
||||
rmSync(join(gone, "package-lock.json"));
|
||||
assert.throws(() => tagMod.imageTag(gone), { message: "image tag: package-lock.json can't be read" });
|
||||
assert.throws(() => tagMod.imageTag(checkout(t, { release: "next" })), { message: "image tag: RELEASE must be a version like 0.0.13" });
|
||||
const cli = spawnSync(process.execPath, [join(REPO, "scripts/image-tag.mjs"), none], { encoding: "utf8", env: { PATH } });
|
||||
assert.equal(cli.status, 1);
|
||||
assert.equal(cli.stdout, "");
|
||||
assert.ok(!cli.stderr.includes(none), "a refusal line carries no host path");
|
||||
});
|
||||
|
||||
// scripts/common.sh as the image-building scripts source it, in a stand-in checkout.
|
||||
function shellCheckout(t, opts) {
|
||||
const root = checkout(t, opts);
|
||||
mkdirSync(join(root, "scripts"));
|
||||
for (const f of ["common.sh", "image-tag.mjs"]) cpSync(join(REPO, "scripts", f), join(root, "scripts", f));
|
||||
return root;
|
||||
}
|
||||
const sh = (root, script, env = {}) => spawnSync("bash", ["-c", `source scripts/common.sh && ${script}`], { cwd: root, encoding: "utf8", env: { PATH, ...env } });
|
||||
|
||||
test("pi-unpin S5: load_release (build.sh, release.sh, verify.sh, test-release.sh) exports the lock's tag and refuses a lock it can't read", t => {
|
||||
const root = shellCheckout(t);
|
||||
const r = sh(root, 'load_release && printf "%s|%s" "$MOSAIC_RELEASE" "$MOSAIC_IMAGE_TAG"');
|
||||
assert.equal(r.status, 0, r.stderr);
|
||||
assert.equal(r.stdout, "0.0.13|mosaic-poc-agent:9.9.9-r0.0.13");
|
||||
writeFileSync(join(root, "package-lock.json"), JSON.stringify(lockOf(null)));
|
||||
const bad = sh(root, 'load_release && printf "reached"');
|
||||
assert.notEqual(bad.status, 0);
|
||||
assert.equal(bad.stdout, "", "a lock with no pi entry stops the script");
|
||||
assert.match(bad.stderr, /image tag: package-lock\.json has no entry/);
|
||||
});
|
||||
|
||||
test("pi-unpin S5: build_image runs the image fit step first, and an unfit pi never reaches the image build", t => {
|
||||
const root = shellCheckout(t);
|
||||
mkdirSync(join(root, "bin"));
|
||||
const log = join(root, "docker.log");
|
||||
writeFileSync(join(root, "bin", "docker"), `#!/bin/sh\nprintf '%s\\n' "$*" >> "${log}"\n`, { mode: 0o755 });
|
||||
writeFileSync(join(root, "scripts", "pi-fit-image.mjs"), `import { appendFileSync } from "node:fs";\nif (process.env.FIT === "unfit") { process.stderr.write("pi-fit-image: pi 9.9.9 (the lock's pi) lacks --offline\\n"); process.exit(1); }\nappendFileSync(${JSON.stringify(log)}, "fit\\n");\n`);
|
||||
const env = { PATH: `${join(root, "bin")}:${PATH}` };
|
||||
const unfit = sh(root, "load_release && build_image; echo after", { ...env, FIT: "unfit" });
|
||||
assert.notEqual(unfit.status, 0);
|
||||
assert.equal(unfit.stdout, "", "the calling script stops at the fit step");
|
||||
assert.match(unfit.stderr, /lacks --offline/);
|
||||
assert.equal(existsSync(log), false, "docker is never called for an unfit pi");
|
||||
const fit = sh(root, "load_release && build_image", env);
|
||||
assert.equal(fit.status, 0, fit.stderr);
|
||||
assert.equal(readFileSync(log, "utf8"), "fit\nbuild -f Containerfile -t mosaic-poc-agent:9.9.9-r0.0.13 .\n");
|
||||
// Without load_release there is no tag to build under.
|
||||
const untagged = spawnSync("bash", ["-c", "source scripts/common.sh && build_image"], { cwd: root, encoding: "utf8", env });
|
||||
assert.notEqual(untagged.status, 0);
|
||||
assert.match(untagged.stderr, /build_image needs MOSAIC_IMAGE_TAG from load_release/);
|
||||
});
|
||||
|
||||
test("pi-unpin S5: no script builds the image tag from package.json, and every image build goes through build_image", () => {
|
||||
const read = (f) => readFileSync(join(REPO, f), "utf8");
|
||||
const scripts = ["scripts/common.sh", "scripts/build.sh", "scripts/release.sh", "scripts/verify.sh", "scripts/test-task.sh", "scripts/test-auth.sh", "scripts/agent-host-dev.sh", "scripts/mosaic-task.mjs", "scripts/testing/lane-image.mjs"];
|
||||
for (const f of scripts) {
|
||||
assert.doesNotMatch(read(f), /dependencies\s*(\?\.)?\[\s*['"]@earendil-works\/pi-coding-agent['"]\s*\]/, `${f} reads pi's version from package.json`);
|
||||
}
|
||||
for (const f of ["scripts/build.sh", "scripts/release.sh", "scripts/verify.sh"]) {
|
||||
assert.match(read(f), /^\s*build_image\b/m, `${f} builds through build_image`);
|
||||
assert.doesNotMatch(read(f), /docker (compose )?build/, `${f} builds without the image fit step`);
|
||||
}
|
||||
// compose.yaml can't build or pull the agent image, so a missing image refuses at compose run
|
||||
// instead of being built without the fit step.
|
||||
const compose = read("compose.yaml");
|
||||
assert.doesNotMatch(compose, /^\s*build:/m, "compose.yaml has no build section");
|
||||
assert.match(compose, /^\s*pull_policy: never$/m);
|
||||
// The tag comes from scripts/image-tag.mjs, and a refusal stops the script instead of running on an empty tag.
|
||||
for (const f of ["scripts/test-task.sh", "scripts/test-auth.sh"]) assert.match(read(f), /^\s*TAG="\$\(node scripts\/image-tag\.mjs\)" \|\| exit 1(\s|$)/m, `${f} takes the tag from scripts/image-tag.mjs and stops when it can't`);
|
||||
assert.match(read("scripts/mosaic-task.mjs"), /MOSAIC_IMAGE_TAG = tag;/);
|
||||
});
|
||||
|
||||
// The gate's image inputs, with a lock at `version` and package.json's range.
|
||||
function imageCheckout(t, version = "9.9.9") {
|
||||
const root = mkdtempSync("/mnt/storage/scratch/s5i-");
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
cpSync(join(REPO, "Containerfile"), join(root, "Containerfile"));
|
||||
writeFileSync(join(root, "package.json"), JSON.stringify({ name: "fixture", dependencies: { [NAME]: RANGE } }));
|
||||
writeFileSync(join(root, "package-lock.json"), JSON.stringify(lockOf(version)));
|
||||
writeFileSync(join(root, "RELEASE"), "0.0.1\n");
|
||||
mkdirSync(join(root, "packages/processing-control/src"), { recursive: true });
|
||||
for (const f of ["state.mjs", "entrypoint.mjs"]) cpSync(join(REPO, "packages/processing-control/src", f), join(root, "packages/processing-control/src", f));
|
||||
for (const d of ["contracts", "src", "adapters"]) { mkdirSync(join(root, d)); writeFileSync(join(root, d, "one"), "same"); }
|
||||
return root;
|
||||
}
|
||||
const base = (root) => /^FROM (\S+)$/m.exec(readFileSync(join(root, "Containerfile"), "utf8"))[1];
|
||||
|
||||
test("pi-unpin S5: verifyImage accepts the lock-derived tag when package.json holds a range, and refuses a tag the lock doesn't give", t => {
|
||||
assert.equal(typeof image.verifyImage, "function");
|
||||
const root = imageCheckout(t);
|
||||
const id = "sha256:" + "a".repeat(64);
|
||||
const receipt = { inputDigest: image.imageFingerprint(root), imageId: id, tag: "mosaic-poc-agent:9.9.9-r0.0.1", baseDigest: base(root), platform: "linux/amd64" };
|
||||
const inspect = () => ({ Id: id, Os: "linux", Architecture: "amd64" });
|
||||
assert.equal(image.verifyImage(receipt, root, inspect).tag, "mosaic-poc-agent:9.9.9-r0.0.1");
|
||||
assert.throws(() => image.verifyImage({ ...receipt, tag: "mosaic-poc-agent:0.85.1-r0.0.1" }, root, inspect), /image tag differs from release identity/);
|
||||
assert.throws(() => image.verifyImage({ ...receipt, tag: `mosaic-poc-agent:${RANGE}-r0.0.1` }, root, inspect), /refused/);
|
||||
writeFileSync(join(root, "package-lock.json"), JSON.stringify(lockOf(null)));
|
||||
assert.throws(() => image.verifyImage({ ...receipt, inputDigest: image.imageFingerprint(root) }, root, inspect), /image tag: package-lock\.json has no entry/);
|
||||
});
|
||||
|
||||
test("pi-unpin S5: prepareImage tags from the lock, runs the image fit step before the build, and a refusing step stops the build", t => {
|
||||
assert.equal(typeof image.prepareImage, "function");
|
||||
assert.equal(typeof image.imageFitStep, "function", "lane-image.mjs exports the image fit step");
|
||||
const root = imageCheckout(t);
|
||||
const calls = [];
|
||||
const invoke = (args) => { calls.push(args.join(" ")); return args[0] === "build" ? "" : JSON.stringify({ Id: "sha256:" + "a".repeat(64), Os: "linux", Architecture: "amd64" }); };
|
||||
const receipt = image.prepareImage(root, { grant: "fixture", tempRoot: root }, invoke, (c) => calls.push(`fit ${c === root}`));
|
||||
assert.equal(receipt.tag, "mosaic-poc-agent:9.9.9-r0.0.1");
|
||||
assert.deepEqual(calls, ["fit true", "build -f Containerfile -t mosaic-poc-agent:9.9.9-r0.0.1 .", "image inspect mosaic-poc-agent:9.9.9-r0.0.1 --format {{json .}}"]);
|
||||
const stopped = [];
|
||||
assert.throws(() => image.prepareImage(root, { grant: "fixture", tempRoot: root }, (a) => stopped.push(a), () => { throw new Error("unfit"); }), /unfit/);
|
||||
assert.deepEqual(stopped, [], "no build after a refusing fit step");
|
||||
// The default step runs the checkout's scripts/pi-fit-image.mjs and refuses with its last line.
|
||||
mkdirSync(join(root, "scripts"));
|
||||
writeFileSync(join(root, "scripts", "pi-fit-image.mjs"), `process.stderr.write("pi-fit-image: pi 9.9.9 (the lock's pi) lacks --offline\\n"); process.exit(1);\n`);
|
||||
assert.throws(() => image.imageFitStep(root), { message: "gate-lane: refused: image fit step refused: pi-fit-image: pi 9.9.9 (the lock's pi) lacks --offline" });
|
||||
assert.throws(() => image.prepareImage(root, { grant: "fixture", tempRoot: root }, (a) => stopped.push(a)), /image fit step refused/);
|
||||
assert.deepEqual(stopped, []);
|
||||
writeFileSync(join(root, "scripts", "pi-fit-image.mjs"), "process.exit(0);\n");
|
||||
assert.equal(image.imageFitStep(root), undefined);
|
||||
});
|
||||
|
||||
test("pi-unpin S5: the image fit step fails on a locked stand-in pi that lacks a flag, and passes one at another version that has them all", SKIP, async t => {
|
||||
assert.equal(typeof fitMod.imageFit, "function", "scripts/pi-fit-image.mjs exports imageFit");
|
||||
const unfit = checkout(t);
|
||||
install(unfit, { dropFlags: ["--offline"] });
|
||||
const r = await fitMod.imageFit(unfit);
|
||||
assert.deepEqual(r, { ok: false, message: "pi 9.9.9 (the lock's pi) lacks --offline" });
|
||||
const ok = checkout(t);
|
||||
install(ok);
|
||||
const pass = await fitMod.imageFit(ok);
|
||||
assert.equal(pass.ok, true, pass.message);
|
||||
assert.equal(pass.version, "9.9.9");
|
||||
assert.equal(pass.locked, "9.9.9");
|
||||
});
|
||||
|
||||
test("pi-unpin S5: the image fit step passes on the installed pi the repository's lock resolves", SKIP, async () => {
|
||||
assert.equal(typeof fitMod.imageFit, "function", "scripts/pi-fit-image.mjs exports imageFit");
|
||||
const locked = JSON.parse(readFileSync(join(REPO, "package-lock.json"), "utf8")).packages[KEY].version;
|
||||
const r = await fitMod.imageFit(REPO);
|
||||
assert.equal(r.ok, true, r.message);
|
||||
assert.equal(r.locked, locked);
|
||||
assert.equal(r.version, locked);
|
||||
const cli = spawnSync(process.execPath, [join(REPO, "scripts/pi-fit-image.mjs")], { encoding: "utf8", env: { PATH, HOME: process.env.HOME, TMPDIR: tmpdir() }, timeout: 120000 });
|
||||
assert.equal(cli.status, 0, cli.stderr);
|
||||
assert.equal(cli.stdout, `pi-fit-image: pi ${locked} (the lock's pi) passed the fit check\n`);
|
||||
});
|
||||
|
||||
test("pi-unpin S5: the image fit step can't tell, and refuses, when node_modules doesn't hold the lock's pi", async t => {
|
||||
assert.equal(typeof fitMod.imageFit, "function", "scripts/pi-fit-image.mjs exports imageFit");
|
||||
const lead = "pi (the lock's pi) can't be checked: ";
|
||||
const npmCi = "run npm ci --ignore-scripts in the repository first";
|
||||
const cases = [
|
||||
["no install", () => {}, `the installed pi isn't the one package-lock.json resolves: ${npmCi}`],
|
||||
["no hidden lock entry", (root) => install(root, {}, { hidden: null }), `the installed pi isn't the one package-lock.json resolves: ${npmCi}`],
|
||||
["another integrity", (root) => install(root, {}, { hidden: { ...entry("9.9.9"), integrity: "sha512-other" } }), `the installed pi isn't the one package-lock.json resolves: ${npmCi}`],
|
||||
["another version installed", (root) => install(root, {}, { version: "9.9.8", hidden: entry("9.9.9") }), `the installed pi's package.json doesn't carry the lock's version: ${npmCi}`],
|
||||
["a lock with no pi", (root) => { install(root); writeFileSync(join(root, "package-lock.json"), JSON.stringify(lockOf(null))); }, `image tag: package-lock.json has no entry for ${NAME}`],
|
||||
["a .bin/pi that isn't executable", (root) => { install(root); chmodSync(join(root, KEY, "dist", "bundle", "cli.js"), 0o644); }, `the lookup refused the lock's pi: ${npmCi}`],
|
||||
["a .bin/pi in another copy of the package", (root) => {
|
||||
install(root);
|
||||
const other = join(root, "other", NAME);
|
||||
mkdirSync(join(other, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(other, "package.json"), JSON.stringify({ name: NAME, version: "9.9.9", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
writeFileSync(join(other, "dist", "bundle", "cli.js"), piStandIn({ version: "9.9.9" }), { mode: 0o755 });
|
||||
rmSync(join(root, "node_modules", ".bin", "pi"));
|
||||
symlinkSync(join(other, "dist", "bundle", "cli.js"), join(root, "node_modules", ".bin", "pi"));
|
||||
}, `node_modules/.bin/pi isn't the lock's pi package: ${npmCi}`],
|
||||
];
|
||||
for (const [name, plant, problem] of cases) {
|
||||
const root = checkout(t);
|
||||
plant(root);
|
||||
const r = await fitMod.imageFit(root, { fit: noFit });
|
||||
assert.deepEqual(r, { ok: false, message: lead + problem }, name);
|
||||
assert.ok(!r.message.includes(root) && !r.message.includes(realpathSync(root)), `${name}: no host path in the line`);
|
||||
}
|
||||
// An answer the fit check doesn't give reads as can't tell.
|
||||
const root = checkout(t);
|
||||
install(root);
|
||||
assert.deepEqual(await fitMod.imageFit(root, { fit: async () => undefined }), { ok: false, message: `${lead}the check gave no answer` });
|
||||
// The fit check gets the found pi under the step's own label, with the module's thinking levels.
|
||||
let seen;
|
||||
const ok = await fitMod.imageFit(root, { fit: async (pi, opts) => { seen = { pi, opts }; return { ok: true, version: "9.9.9" }; } });
|
||||
assert.equal(ok.ok, true);
|
||||
assert.equal(ok.locked, "9.9.9");
|
||||
assert.equal(seen.pi.label, "the lock's pi");
|
||||
assert.equal(seen.pi.pkg, realpathSync(join(root, KEY)));
|
||||
assert.equal("locked" in seen.pi, false);
|
||||
assert.ok(Array.isArray(seen.opts.levels) && seen.opts.levels.includes("high"));
|
||||
});
|
||||
|
||||
// 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 = 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"));
|
||||
writeFileSync(join(root, "scripts/common.sh"), 'load_config() { MOSAIC_DATA_ROOT="$PWD"; MOSAIC_PROVIDER=test; MOSAIC_MODEL=test-model; MOSAIC_ADAPTER=pi; }\n');
|
||||
writeFileSync(join(root, "package.json"), JSON.stringify({ dependencies: { [NAME]: RANGE } }));
|
||||
for (const f of ["agents/fixture/SOUL.md", "agents/fixture/CONTEXT.md", "contracts/CONSTITUTION.md", "contracts/STANDARDS.md", "user/USER.md", "AGENTS.md", "extensions/goal/index.ts"]) writeFileSync(join(root, f), `fixture ${f}\n`);
|
||||
for (const s of ["ms-tools", "ms-file-read", "ms-file-write", "ms-conductor", "ms-communications", "ms-watch", "ms-sdlc", "ms-proactive-agent", "ms-goal", "ms-unslop"]) {
|
||||
mkdirSync(join(root, "skills", s), { recursive: true });
|
||||
writeFileSync(join(root, "skills", s, "SKILL.md"), `fixture ${s}\n`);
|
||||
}
|
||||
writeFileSync(join(root, "node_modules/.bin/pi"), piScript, { mode: 0o755 });
|
||||
return root;
|
||||
}
|
||||
const hostCheck = (root) => spawnSync("bash", [join(root, "scripts/agent-host-dev.sh"), "fixture", "--check"], { cwd: root, encoding: "utf8", env: { PATH, HOME: root }, timeout: 20000 });
|
||||
|
||||
test("pi-unpin S5: scripts/agent-host-dev.sh doesn't refuse pi for its version; it records the version pi prints", t => {
|
||||
const root = hostCheckout(t, "#!/bin/sh\n[ \"$1\" = --version ] && { echo 9.9.9; exit 0; }\nexit 1\n");
|
||||
const r = hostCheck(root);
|
||||
assert.equal(r.status, 0, r.stdout + r.stderr);
|
||||
assert.match(r.stdout, /fixture: native host TUI \| Pi 9\.9\.9 \| test\/test-model/);
|
||||
assert.match(r.stdout, /configuration checks passed/);
|
||||
// A pi whose --version fails or prints nothing still starts; the banner says so.
|
||||
for (const script of ["#!/bin/sh\nexit 3\n", "#!/bin/sh\nexit 0\n"]) {
|
||||
const quiet = hostCheckout(t, script);
|
||||
const q = hostCheck(quiet);
|
||||
assert.equal(q.status, 0, q.stdout + q.stderr);
|
||||
assert.match(q.stdout, /\| Pi version unknown \|/);
|
||||
}
|
||||
assert.doesNotMatch(readFileSync(join(REPO, "scripts/agent-host-dev.sh"), "utf8"), /package\.json pins|\$PIN\b/);
|
||||
});
|
||||
@@ -146,7 +146,8 @@ test('real pinned install loads bus and Console; the Console starts only with --
|
||||
const s=setup(t,true);
|
||||
const copied=(await s.run('stack-copy.sh',['copy',s.id,'--copies',s.copies]));assert.equal(copied.status,0,copied.stderr);
|
||||
const root=join(s.copies,s.id),url=f=>pathToFileURL(join(root,f)).href;
|
||||
const {checkEnginePin}=await import(url('packages/conversation/src/pi-pin.mjs'));assert.equal(checkEnginePin(root).version,'0.85.1');
|
||||
await import(url('packages/conversation/src/pi-seal.mjs'));const {locate}=await import(url('packages/plugins/harness/pi/binary.mjs')),{levels}=await import(url('packages/plugins/harness/pi/index.mjs')),{piFit}=await import(url('packages/harness/src/pi-fit.mjs'));
|
||||
const copyPi=locate({});assert.equal(copyPi.label,"the repository's pi");assert.equal(copyPi.bin,join(root,'node_modules','.bin','pi'));const fit=await piFit(copyPi,{levels:levels()});assert.equal(fit.ok,true,fit.message);assert.equal(fit.version,'0.85.1');
|
||||
const home=join(s.root,'home'),f=fixture(home),configDir=join(home,'.config','mosaic-pin');
|
||||
mkdirSync(join(home,'.config'),{recursive:true});renameSync(join(home,'config'),configDir);chmodSync(configDir,0o700);
|
||||
writeFileSync(join(f.dataRoot,'instance.json'),JSON.stringify({instanceVersion:1,name:'pin'}));
|
||||
|
||||
@@ -234,7 +234,7 @@ live session, and the controller never writes a session file (W11).
|
||||
| `src/cohort.mjs` | `ScopeLauncher` (systemd user scope plus `shim.mjs`), `PgroupLauncher`, force stop, cohort and boot proofs |
|
||||
| `src/shim.mjs` | the scope's first process, outside the `engine` cgroup; ops `hello`, `events`, `members`, `term`, `freeze`, `kill`, `release`; ignores SIGTERM and exits on `release` once `engine` is empty |
|
||||
| `src/guard.mjs` | `LiveSessionGuard`: refuses live sessions and paths under live roots |
|
||||
| `src/pi-pin.mjs` | the Pi pin (0.85.1 and its integrity) and the seal |
|
||||
| `src/pi-seal.mjs` | the seal (with `--no-mcp` when the pi's help lists it) and the engine refusal codes |
|
||||
| `src/engine.mjs`, `src/framing.mjs` | the engine link and LF framing |
|
||||
| `src/turns.mjs` | `Tracker`: runs, the dispatch slot, overlap signals O1–O6 |
|
||||
| `src/events.mjs` | native event to CHAT-01 event mapping |
|
||||
@@ -289,16 +289,72 @@ can disagree with it.
|
||||
(`--no-session`, `--fork`, `--export`, `--print`, `--continue`, ...) or a
|
||||
bare word, which Pi reads as a prompt (lead decision 31; N24, including the missing flag
|
||||
through `checkSeal`).
|
||||
- **Engine command and environment (slice 1 S5, #1522).** The controller
|
||||
always launches `node <pinRoot>/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js`
|
||||
and checks that at construction and again at bind. `engine` takes only
|
||||
- **Engine command and environment (slice 1 S5, #1522; #1873 S4).** The
|
||||
controller launches `node <the real file of the pi the lookup finds>`.
|
||||
The lookup is the pi harness module's (`packages/plugins/harness/pi/binary.mjs`):
|
||||
`MOSAIC_PI_BIN`, else the first `pi` on `PATH`, else the repository's
|
||||
copy. The `pi` option takes a lookup answer and defaults to
|
||||
`locate(process.env)`. The controller checks the command at construction
|
||||
and again at bind, resolving the bin's real path each time, so a bin link
|
||||
moved after construction refuses `unsealed-engine`. `engine` takes only
|
||||
`extraArgs`, `cwd` and `envKeys`; any other key (`command`, `preArgs`,
|
||||
`env`), or an `engine` that isn't an object, refuses `unsealed-engine`.
|
||||
The fake engine comes in through `opts[TEST_ENGINE]`, a symbol key that
|
||||
JSON config can't carry, so no config file reaches an unsealed command.
|
||||
`preArgs` carrying `--extension` still refuses there too. The binding's
|
||||
`argvDigest` records the full command line.
|
||||
- **Engine environment.** The engine gets `ENGINE_ENV` (`pi-pin.mjs`):
|
||||
- **Engine fit and pins (#1873 S4).** No pi is refused for its version.
|
||||
Before a claim, a resume or a launch, the controller runs the fit check
|
||||
(`packages/harness/src/pi-fit.mjs`) on the pi the lookup finds. A pi that
|
||||
lacks a feature the seal needs, or that the check can't read, refuses
|
||||
`engine-feature-missing` with the fit check's line (for example
|
||||
`pi 9.9.9 (MOSAIC_PI_BIN) lacks --mode rpc`). The claim's pins are
|
||||
`engineVersion` (what `--version` printed, or null), `enginePin` (the fit
|
||||
check's digest of the pi's files) and `argvDigest`. A resume or recovery
|
||||
whose pins differ refuses `engine-pin-mismatch`, so changed bytes under a
|
||||
stopped claim can't resume it. A claim made before #1873 holds npm's
|
||||
integrity in `enginePin`; it refuses `engine-pin-mismatch` with "the claim
|
||||
was made under the old pin record; start a new claim". Like any pin
|
||||
mismatch, that refusal holds at every start of the session, since a resume
|
||||
is checked against the session's last proven stop. A new session starts a
|
||||
new claim.
|
||||
- **Same file, same interpreter.** The fit check runs the bin as a program,
|
||||
so its first line picks what runs it, while the launch runs the bin's real
|
||||
file with this Node. A pi whose first line isn't `#!/usr/bin/env node` or
|
||||
`#!` and an absolute path that resolves to this Node refuses
|
||||
`engine-feature-missing`. So does a first line that doesn't end within the
|
||||
256 bytes the kernel reads, or one with a CR before its newline. Under
|
||||
`#!/usr/bin/env node`, env takes node from the fit check's PATH (the
|
||||
repository's `.bin`, then this process's PATH, as `sessionEnv` builds it),
|
||||
and it looks inside the fit check's sandbox, which shows only `/usr`,
|
||||
`/etc`, the host's `/bin`, `/lib`, `/lib64` and `/sbin`, and this Node's
|
||||
own file at its path unless that path is under `/tmp` (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
|
||||
`/dev`, which the sandbox makes its own, by its spelling or by the real
|
||||
path of the nearest part of it that resolves. The sandbox's `/tmp` is its
|
||||
own too, but starts empty, so a `/tmp` entry is judged like any other
|
||||
directory the sandbox doesn't show. An entry or a `node` link there that
|
||||
can't be checked refuses too. No `node` the sandbox can't show
|
||||
(or reaches through a link it can't show) may come before the first one it
|
||||
can, and that first one must be this Node. No `node` anywhere on the PATH
|
||||
may lie inside the package the fit check mounts. Otherwise the start
|
||||
refuses `engine-feature-missing`. A refusal line, the fit check's
|
||||
included, names the pi by the lookup's label, which is words such as
|
||||
`MOSAIC_PI_BIN`, or "the pi" for any other label. A lookup that found no
|
||||
pi refuses with its own line, or "the pi lookup gave no binary" when that
|
||||
line holds a path or a control character.
|
||||
The controller keeps a frozen plain copy of the lookup's answer, its own
|
||||
string data fields only, so an answer that changes later changes nothing.
|
||||
That copy must still be the lookup's at each check: the lookup run again on
|
||||
its bin gives the same `root`, `dest`, `view` and `pkg`, so the fit check
|
||||
mounts, runs and digests the package the launch runs. A package link moved
|
||||
since the lookup, an answer changed by hand, or a bin the lookup no longer
|
||||
takes refuses `unsealed-engine`.
|
||||
- **Engine environment.** The engine gets `ENGINE_ENV` (`pi-seal.mjs`):
|
||||
`PATH`, `HOME`, `USER`, `LOGNAME`, `SHELL`, `LANG`, `LC_ALL`,
|
||||
`LC_CTYPE`, `TZ`, `TERM`, `TMPDIR`, `PI_CODING_AGENT_DIR`, `PI_OFFLINE`,
|
||||
`PI_SKIP_VERSION_CHECK` and `PI_TELEMETRY`, plus the names in
|
||||
@@ -472,7 +528,7 @@ different IDs, so overlap at the seam can't be deduplicated (CHAT-01 lines
|
||||
## Slash text
|
||||
|
||||
`textPolicy` refuses any prompt whose text, after leading whitespace, starts
|
||||
with `/`: pinned Pi runs extension commands, skills (`/skill:`) and prompt
|
||||
with `/`: Pi runs extension commands, skills (`/skill:`) and prompt
|
||||
templates from index 0 (S1, S2). `!`, `!!` and `@` are interpreted only by
|
||||
Pi's interactive mode and CLI, not on the RPC prompt path, so they are
|
||||
admitted and sent as text. A `/` on a later line is not interpreted
|
||||
@@ -547,13 +603,14 @@ runs in CHAT-01 `check.mjs` order, then the CHAT-03 narrowings.
|
||||
| `eligibility` | launch with a used or unknown eligibility record, one from another incarnation, or a changed reservation (K17) |
|
||||
| `already-active`, `unsafe-replacement`, `foreign-host` | the writer claim (W2, W3, W17) |
|
||||
| `live-session-refused` | the live-session guard, at construction, bind and launch (G1–G3) |
|
||||
| `unsealed-engine`, `engine-pin-mismatch` | the seal or the Pi pin (N24) |
|
||||
| `unsealed-engine`, `engine-pin-mismatch` | the seal, or the engine's version, digest or argv changed under a claim (N24, W9, S4c) |
|
||||
| `engine-feature-missing` | the fit check found the pi lacks a feature, or couldn't tell (S4b) |
|
||||
| `configuration` | a required constructor path is missing or misplaced, the session file is unreadable, or the session header ID isn't a CHAT-01 ID |
|
||||
|
||||
`malformed`, `eligibility` and `preflight` are names this build adds; the
|
||||
brief describes those refusals without naming them.
|
||||
|
||||
The controller, claim store, live-session guard and engine pin throw
|
||||
The controller, claim store, live-session guard and engine checks throw
|
||||
`ControlRefusal` (`safe-fs.mjs`), a subclass of the reader's `Refusal`. These
|
||||
codes reach a socket client, never the reader's HTTP routes, so the control
|
||||
board's status map (`REFUSAL_STATUS`, which its tests check against every
|
||||
@@ -563,15 +620,15 @@ board ever serves the controller, these codes need statuses there first.
|
||||
Receipt reason codes, not refusals: `transport-unknown`,
|
||||
`handled-without-run`, `ack-without-start`, `interrupted`, `run-overlap`.
|
||||
|
||||
## Findings against pinned Pi
|
||||
## Findings against Pi 0.85.1
|
||||
|
||||
From `tests/smoke.test.mjs`, which starts the pinned binary sealed, in a
|
||||
From `tests/smoke.test.mjs`, which starts the repository's pi (0.85.1) sealed, in a
|
||||
scratch home with an empty agent dir and no inherited environment, and sends
|
||||
no prompt:
|
||||
|
||||
- It starts with no credentials and answers `get_state`, `get_commands`,
|
||||
`clear_queue`, `abort` and `get_tree`. Every field the fake engine sends
|
||||
for those commands, pinned Pi sends with the same type. `clear_queue` emits
|
||||
for those commands, Pi 0.85.1 sends with the same type. `clear_queue` emits
|
||||
`queue_update` with empty queues before its response, as the fake does.
|
||||
Pi writes `auth.json` (`{}`) and `models-store.json` into the agent dir.
|
||||
- **Startup append.** Pi appends to the session at startup in two cases
|
||||
@@ -593,13 +650,13 @@ no prompt:
|
||||
|
||||
| Suite | Covers |
|
||||
|---|---|
|
||||
| `claim.test.mjs` | W1–W17, W20, G1–G3: the writer claim, crash barriers, the guard |
|
||||
| `claim.test.mjs` | W1–W17, W20, G1–G3, S4a–S4h: the writer claim, crash barriers, the guard, the engine fit, pins and seal, the node-script check and the lookup answer check |
|
||||
| `races.test.mjs` | H1–H4, H9–H23: takeover, Interrupt and force stop, retries, incarnations |
|
||||
| `turns.test.mjs` | N1–N25, N24b: the turn tracker against the fake engine's Pi behaviors; the engine seal and environment |
|
||||
| `turns.test.mjs` | N1–N25, N24b, N24c: the turn tracker against the fake engine's Pi behaviors; the engine seal, `--no-mcp` and environment |
|
||||
| `cohort.test.mjs` | K1–K19, R1–R3, R5–R13, E1–E6: scopes, force stop, proofs, recovery, eligibility, the scope's environment, scope release and its retry after a crash, the engine-exit stop (needs a systemd user manager) |
|
||||
| `flows.test.mjs` | S1–S7, P3, E1–E7, the terminal, and a CHAT-01 schema check of every record produced |
|
||||
| `smoke.test.mjs` | the pinned Pi binary, as above |
|
||||
| `smoke.test.mjs` | the repository's Pi binary, as above, and its fit check |
|
||||
|
||||
`fake-pi.mjs` models pinned Pi's RPC mode, including the startup append, and
|
||||
`fake-pi.mjs` models Pi 0.85.1's RPC mode, including the startup append, and
|
||||
`ctrl-child.mjs` runs a controller in a child process for the crash tests.
|
||||
Fixtures live in temporary directories and are removed after each file.
|
||||
@@ -16,7 +16,7 @@
|
||||
// as in CHAT-01. With no verifier nothing verifies, and every stop ends
|
||||
// `uncertain`.
|
||||
|
||||
import { lstatSync, mkdirSync, readFileSync, unlinkSync } from "node:fs";
|
||||
import { closeSync, lstatSync, mkdirSync, openSync, readFileSync, readSync, readlinkSync, realpathSync, statSync, unlinkSync } from "node:fs";
|
||||
import { createServer } from "node:net";
|
||||
import { basename, dirname, join, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
@@ -30,7 +30,11 @@ import { LineSplitter, encodeLine, parseLine } from "./framing.mjs";
|
||||
import { LiveSessionGuard, realPath } from "./guard.mjs";
|
||||
import { ID, fragments, safeId } from "./parts.mjs";
|
||||
import { parseSnapshot } from "./pi.mjs";
|
||||
import { ENGINE_PIN_MISMATCH, PI_BIN, UNSEALED_ENGINE, argvDigest, buildPiArgs, checkEnginePin, checkSeal, engineEnv } from "./pi-pin.mjs";
|
||||
import { ENGINE_FEATURE_MISSING, ENGINE_PIN_MISMATCH, OLD_PIN_LINE, UNSEALED_ENGINE, argvDigest, buildPiArgs, checkSeal, engineEnv, oldPinRecord } from "./pi-seal.mjs";
|
||||
import { PACKAGE, executable, locate } from "../../plugins/harness/pi/binary.mjs";
|
||||
import { sessionEnv } from "../../seat/src/session.mjs";
|
||||
import { levels } from "../../plugins/harness/pi/index.mjs";
|
||||
import { piFit } from "../../harness/src/pi-fit.mjs";
|
||||
import { ACTOR, conversationId, createReader, rootsFromSpecs } from "./reader.mjs";
|
||||
import { clone, equal, hash, newId, receiptAllows, record, scopeMatch, sealProof, sha256, targetOf } from "./records.mjs";
|
||||
import { ControlRefusal, Refusal } from "./safe-fs.mjs";
|
||||
@@ -79,7 +83,7 @@ const STOP_IN_PROGRESS = new Set(["fenced", "cancelling", "stopping"]);
|
||||
const STOP_NEXT = { fenced: ["cancelling", "stopping", "uncertain"], cancelling: ["stopping", "uncertain"], stopping: ["uncertain"], uncertain: [] };
|
||||
// check.mjs `ending`: the binding follows only stops that end the engine.
|
||||
const ending = (mode) => mode === "force-stop" || mode === "engine-exit";
|
||||
const SEAL_BASIS = "seal: --no-extensions and no --extension argument (pi-pin.mjs SEAL_FLAGS)";
|
||||
const SEAL_BASIS = "seal: --no-extensions and no --extension argument (pi-seal.mjs SEAL_FLAGS)";
|
||||
const DIALOG_REASON = "Pi dialogs are not answered in CHAT-03 (lead decision 30); shown disabled";
|
||||
|
||||
const refused = (code, extra = {}) => ({ outcome: `refused:${code}`, refusal: code, ...extra });
|
||||
@@ -152,13 +156,74 @@ export function sessionTools(parsed) {
|
||||
return tools;
|
||||
}
|
||||
|
||||
// A first line that runs node: #!/usr/bin/env node, or #! and an absolute path
|
||||
// whose last part is node, with nothing after it.
|
||||
const NODE_SHEBANG = /^#![ \t]*(?:\/usr\/bin\/env[ \t]+node|(\/[^\s]*\/node))[ \t]*$/;
|
||||
const ANSWER_KEYS = ["root", "dest", "view", "pkg"];
|
||||
const ANSWER_FIELDS = ["bin", "label", "problem", ...ANSWER_KEYS];
|
||||
|
||||
// #1873 S4: what the fit check's sandbox (systemBinds in seat/src/sandbox.mjs)
|
||||
// shows as the host has it: /usr, /etc and the host's /bin, /lib, /lib64 and
|
||||
// /sbin, and this node's own file at process.execPath. shownInFit follows p
|
||||
// one part at a time as the sandbox would and gives the file it reaches, or
|
||||
// null when a link or the file lies anywhere else. Null may be too strict (the
|
||||
// package's own mount is left out on purpose: no node may come from it), never
|
||||
// too loose.
|
||||
const SHOWN_DIRS = ["/usr", "/etc", "/bin", "/lib", "/lib64", "/sbin"];
|
||||
const shownDir = (p) => SHOWN_DIRS.some((d) => p === d || p.startsWith(d + "/"));
|
||||
// The sandbox makes its own /proc and /dev, so a path there names something
|
||||
// else inside it than on the host. Its /tmp is another directory too, but one
|
||||
// that starts empty (pi-fit.mjs makes it fresh for each run), so an entry
|
||||
// there holds no node in the sandbox, and a node the host finds there 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) {
|
||||
if (hops > 40) return null;
|
||||
const parts = p.split("/").filter(Boolean);
|
||||
let at = "/";
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
const next = join(at, parts[i]);
|
||||
let st;
|
||||
try { st = lstatSync(next); } catch { return null; }
|
||||
if (st.isSymbolicLink()) {
|
||||
if (!shownDir(next)) return null;
|
||||
let target;
|
||||
try { target = readlinkSync(next); } catch { return null; }
|
||||
return shownInFit(resolve(at, target, ...parts.slice(i + 1)), hops + 1);
|
||||
}
|
||||
at = next;
|
||||
}
|
||||
return (at === process.execPath && !underTmp(at)) || shownDir(at) ? at : null;
|
||||
}
|
||||
|
||||
// #1873 S4: the lookup's answer, copied once into a frozen plain object of
|
||||
// its own data fields, so what the seal checks and what the fit check reads
|
||||
// are the same values for the controller's life. A getter, a Proxy that
|
||||
// answers differently later, or an inherited field changes nothing after
|
||||
// this copy.
|
||||
function answerCopy(pi) {
|
||||
if (!pi || typeof pi !== "object") return pi;
|
||||
const out = {};
|
||||
for (const k of ANSWER_FIELDS) {
|
||||
let d;
|
||||
try { d = Object.getOwnPropertyDescriptor(pi, k); } catch { d = undefined; }
|
||||
if (d && "value" in d && (typeof d.value === "string" || d.value === null)) out[k] = d.value;
|
||||
}
|
||||
return Object.freeze(out);
|
||||
}
|
||||
|
||||
export class Controller {
|
||||
constructor(opts = {}) {
|
||||
const {
|
||||
fixtureRoot, claimRoot, socketDir, sessionFile, seat, workspace = "default",
|
||||
engine = {}, launcher = new PgroupLauncher(), verifier = null, host, identity, barrier = null, units = systemdUnits,
|
||||
grants = null, guardOptions = {}, timeouts = {}, now = () => new Date(),
|
||||
policyRevision = "policy-fixture-1", sourceRootRef = "source-fixture-1", approvedMappings = null, pinRoot = REPO_ROOT, auditWritable = true,
|
||||
policyRevision = "policy-fixture-1", sourceRootRef = "source-fixture-1", approvedMappings = null, pi = locate(process.env), auditWritable = true,
|
||||
hostId = null,
|
||||
} = opts;
|
||||
for (const [k, v] of Object.entries({ claimRoot, socketDir, sessionFile, seat })) {
|
||||
@@ -180,12 +245,15 @@ export class Controller {
|
||||
this.conversation = conversationId(this.root, basename(this.paths.sessionFile));
|
||||
if (!engine || typeof engine !== "object" || Array.isArray(engine)) throw new ControlRefusal(UNSEALED_ENGINE, "engine is not an object");
|
||||
const extra = Object.keys(engine).find((k) => !ENGINE_KEYS.has(k));
|
||||
if (extra !== undefined) throw new ControlRefusal(UNSEALED_ENGINE, `engine.${extra.slice(0, 40)} can't be configured; the controller launches the pinned Pi with its own environment`);
|
||||
if (extra !== undefined) throw new ControlRefusal(UNSEALED_ENGINE, `engine.${extra.slice(0, 40)} can't be configured; the controller launches the pi the lookup finds, with its own environment`);
|
||||
const test = opts[TEST_ENGINE] ?? null;
|
||||
// #1873 S4: the pi is binary.mjs's answer ({ bin, label, ... } or
|
||||
// { bin: null, problem }). A sealed launch runs its bin's real file.
|
||||
this.pi = answerCopy(pi);
|
||||
this.engine = {
|
||||
sealed: test === null,
|
||||
command: test ? test.command : process.execPath,
|
||||
preArgs: test ? test.preArgs : [join(pinRoot, PI_BIN)],
|
||||
preArgs: test ? test.preArgs : [this.#piFile()],
|
||||
extraArgs: engine.extraArgs ?? [],
|
||||
cwd: engine.cwd ?? projectRoot,
|
||||
env: test ? test.env : engineEnv(engine.envKeys),
|
||||
@@ -194,8 +262,8 @@ export class Controller {
|
||||
if (!Array.isArray(this.engine[k])) throw new ControlRefusal(UNSEALED_ENGINE, `engine.${k} is not a list`);
|
||||
}
|
||||
if (typeof this.engine.command !== "string" || !this.engine.command) throw new ControlRefusal(UNSEALED_ENGINE, "the engine command is not a path");
|
||||
this.noMcp = false;
|
||||
this.piArgs = buildPiArgs({ sessionFile: this.paths.sessionFile, extraArgs: this.engine.extraArgs });
|
||||
this.pinRoot = pinRoot;
|
||||
this.#checkSeal();
|
||||
this.launcher = launcher;
|
||||
this.verifier = verifier;
|
||||
@@ -257,20 +325,168 @@ export class Controller {
|
||||
if (this.barrier) await this.barrier(name, detail);
|
||||
}
|
||||
|
||||
// The real file of the pi's bin, read now. No answer from the lookup, or a
|
||||
// bin that no longer resolves, refuses: the lookup's own line names the
|
||||
// step, never a path. The answer must still be the lookup's: the lookup run
|
||||
// again on its bin gives the same root, dest, view and pkg. A stale
|
||||
// answer (a link moved since the lookup) or one changed by hand refuses
|
||||
// unsealed-engine.
|
||||
#piFile() {
|
||||
const pi = this.pi;
|
||||
if (!pi || typeof pi !== "object" || typeof pi.bin !== "string" || !pi.bin) {
|
||||
// The lookup's problem lines are words and its package's name; a
|
||||
// hand-built one with a path or a control character in it isn't shown.
|
||||
const problem = pi?.problem;
|
||||
const plain = typeof problem === "string" && problem && !/[\x00-\x1f\x7f-\x9f\u2028\u2029]/.test(problem) && !problem.replaceAll(PACKAGE, "").includes("/");
|
||||
const why = plain ? problem : "the pi lookup gave no binary";
|
||||
throw new ControlRefusal(ENGINE_FEATURE_MISSING, why);
|
||||
}
|
||||
const label = this.#label();
|
||||
let real;
|
||||
try {
|
||||
real = realpathSync(pi.bin);
|
||||
} catch {
|
||||
throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label} no longer resolves to a file`);
|
||||
}
|
||||
// The lookup run again on the same bin must give the same answer: what the
|
||||
// fit check mounts (root at dest), runs (view) and digests (pkg) is then
|
||||
// the package the launch runs.
|
||||
const again = locate({ MOSAIC_PI_BIN: pi.bin, ...(typeof process.env.HOME === "string" ? { HOME: process.env.HOME } : {}) });
|
||||
if (again.bin === null || ANSWER_KEYS.some((k) => (again[k] ?? null) !== (pi[k] ?? null))) {
|
||||
throw new ControlRefusal(UNSEALED_ENGINE, `${label} no longer matches the lookup's answer; look the pi up again`);
|
||||
}
|
||||
return real;
|
||||
}
|
||||
|
||||
// #1873 S4: the launch is this Node running the bin's real file, while the
|
||||
// fit check runs the bin as a program, so its first line picks what runs it.
|
||||
// Only a file whose first line runs node (#!/usr/bin/env node, or an
|
||||
// absolute path to this same node) runs the same way in both.
|
||||
#nodeScript(file) {
|
||||
let head = "";
|
||||
let fd = null;
|
||||
try {
|
||||
fd = openSync(file, "r");
|
||||
const buf = Buffer.alloc(256);
|
||||
head = buf.subarray(0, readSync(fd, buf, 0, buf.length, 0)).toString("latin1");
|
||||
} catch {
|
||||
head = "";
|
||||
} finally {
|
||||
if (fd !== null) try { closeSync(fd); } catch {}
|
||||
}
|
||||
const label = this.#label();
|
||||
// The kernel reads at most 256 bytes for the first line; one that doesn't
|
||||
// end inside them isn't run as a #! line at all.
|
||||
const nl = head.indexOf("\n");
|
||||
const m = nl < 0 ? null : NODE_SHEBANG.exec(head.slice(0, nl));
|
||||
if (!m) throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label} isn't a node script, and the engine runs pi with node`);
|
||||
// An absolute node must be this one: a file named node that is something
|
||||
// else would let the fit check run another program than the launch does.
|
||||
if (m[1] !== undefined) {
|
||||
let interp = null;
|
||||
try { interp = realpathSync(m[1]); } catch {}
|
||||
if (interp === null || interp !== realpathSync(process.execPath)) {
|
||||
throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs another node than the engine's`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
// env takes node from the fit check's PATH, the one a bus session gets
|
||||
// (sessionEnv: the repository's .bin, then this process's PATH), and it
|
||||
// looks inside the sandbox, which shows only some of the host. Up to the
|
||||
// first node, every entry must read the same in both: absolute, already
|
||||
// in plain form (a ".." after a link goes where the link points, not
|
||||
// where the text does), and nowhere the sandbox gives its own copy of
|
||||
// (/proc, /dev), in its spelling or its real path. An entry or a
|
||||
// node there that can't be checked refuses. No node the sandbox can't show
|
||||
// may come first, and the first one must be this node. No node on the
|
||||
// PATH may come from the package the fit check mounts.
|
||||
const engineNode = realpathSync(process.execPath);
|
||||
const realOrNull = (p) => { try { return realpathSync(p); } catch { return null; } };
|
||||
const root = realOrNull(this.pi.root) ?? this.pi.root;
|
||||
const refuse = (why) => { throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs node from the fit check's PATH, ${why}`); };
|
||||
const missing = (e) => e?.code === "ENOENT" || e?.code === "ENOTDIR";
|
||||
const ownCopy = (p) => OWN_COPY_DIRS.some((d) => p === d || p.startsWith(d + "/"));
|
||||
let first = null;
|
||||
for (const dir of String(sessionEnv(process.env, "fit").PATH ?? "").split(":")) {
|
||||
if (first === null) {
|
||||
if (!dir.startsWith("/")) refuse("which has an empty or relative entry");
|
||||
const plain = dir.length > 1 ? dir.replace(/\/+$/, "") : dir;
|
||||
if (resolve(plain) !== plain) refuse("which has an entry that isn't in plain form");
|
||||
const own = "which has an entry the fit check's sandbox has its own copy of";
|
||||
if (ownCopy(plain)) refuse(own);
|
||||
// An entry that doesn't exist is judged by the real path of the
|
||||
// nearest part of it that does.
|
||||
let realDir = null;
|
||||
for (let at = plain; realDir === null; at = dirname(at)) {
|
||||
try { realDir = realpathSync(at); } catch (e) { if (!missing(e) || at === "/") refuse("where an entry can't be checked"); }
|
||||
}
|
||||
if (ownCopy(realDir)) refuse(own);
|
||||
const candidate = join(plain, "node");
|
||||
try { lstatSync(candidate); } catch (e) { if (missing(e)) continue; refuse("where an entry can't be checked"); }
|
||||
let held = null;
|
||||
try { held = statSync(candidate); } catch { refuse("where a node can't be checked"); }
|
||||
if (!held.isFile() || !executable(candidate)) continue;
|
||||
} else if (!dir.startsWith("/") || !executable(resolve(dir, "node"))) {
|
||||
continue;
|
||||
}
|
||||
const candidate = resolve(dir, "node");
|
||||
const real = realOrNull(candidate);
|
||||
if (real === null) {
|
||||
if (first === null) refuse("where a node can't be checked");
|
||||
continue;
|
||||
}
|
||||
if (real === root || real.startsWith(root + "/")) {
|
||||
throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s package puts a node on the fit check's PATH`);
|
||||
}
|
||||
if (first !== null) continue;
|
||||
first = shownInFit(candidate);
|
||||
if (first === null) refuse("where a node the fit check can't see comes first");
|
||||
}
|
||||
if (first === null || realOrNull(first) !== engineNode) {
|
||||
throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs the first node on the fit check's PATH, which isn't the engine's`);
|
||||
}
|
||||
}
|
||||
|
||||
// The name a refusal line uses for the pi: the lookup's label, which names
|
||||
// a step in words ("MOSAIC_PI_BIN", "the pi on PATH", "the repository's
|
||||
// pi"), never a path. Anything else, such as a hand-built answer's label
|
||||
// with a / or a newline in it, is "the pi".
|
||||
#label() {
|
||||
const label = this.pi?.label;
|
||||
return typeof label === "string" && /^[A-Za-z_' ]{1,64}$/.test(label) ? label : "the pi";
|
||||
}
|
||||
|
||||
// The seal covers the command: unless a test engine was given, the launch
|
||||
// is this Node running the pinned Pi's bin, and nothing else.
|
||||
// is this Node running the real file of the bin the lookup found, read
|
||||
// again at each check, and nothing else.
|
||||
#checkSeal() {
|
||||
if (this.engine.sealed && (this.engine.command !== process.execPath || this.engine.preArgs.length !== 1 || this.engine.preArgs[0] !== join(this.pinRoot, PI_BIN))) {
|
||||
throw new ControlRefusal(UNSEALED_ENGINE, "the engine command is not the pinned Pi");
|
||||
if (this.engine.sealed && (this.engine.command !== process.execPath || this.engine.preArgs.length !== 1 || this.engine.preArgs[0] !== this.#piFile())) {
|
||||
throw new ControlRefusal(UNSEALED_ENGINE, `the engine command is not ${this.#label()} the lookup found`);
|
||||
}
|
||||
const bad = this.engine.preArgs.find((a) => typeof a !== "string" || a === "-e" || a === "--extension" || a.startsWith("--extension="));
|
||||
if (bad !== undefined) throw new ControlRefusal(UNSEALED_ENGINE, `engine pre-arguments carry ${bad}`);
|
||||
checkSeal(this.piArgs);
|
||||
checkSeal(this.piArgs, { noMcp: this.noMcp });
|
||||
}
|
||||
|
||||
#pins() {
|
||||
const pin = checkEnginePin(this.pinRoot);
|
||||
return { engineVersion: pin.version, enginePin: pin.pin, argvDigest: argvDigest(this.engine.command, [...this.engine.preArgs, ...this.piArgs]) };
|
||||
// #1873 S4: the fit check (pi-fit.mjs, cached by the digest of the files
|
||||
// that run) stands where the version pin stood. A pi that fails it, or
|
||||
// can't be checked, refuses engine-feature-missing with the check's line.
|
||||
// engineVersion is the --version text (null when pi printed none) and
|
||||
// enginePin the digest. --no-mcp joins the seal when the help lists it.
|
||||
async #pins() {
|
||||
this.#nodeScript(this.#piFile());
|
||||
const fit = await piFit({ ...this.pi, label: this.#label() }, { levels: levels() });
|
||||
if (!fit?.ok) throw new ControlRefusal(ENGINE_FEATURE_MISSING, typeof fit?.message === "string" ? fit.message : "the pi could not be checked");
|
||||
if (typeof fit.digest !== "string" || !/^[0-9a-f]{64}$/.test(fit.digest)) throw new ControlRefusal(ENGINE_FEATURE_MISSING, "the fit check gave no digest");
|
||||
this.noMcp = fit.noMcp === true;
|
||||
this.piArgs = buildPiArgs({ sessionFile: this.paths.sessionFile, extraArgs: this.engine.extraArgs, noMcp: this.noMcp });
|
||||
this.#checkSeal();
|
||||
return { engineVersion: typeof fit.version === "string" ? fit.version : null, enginePin: fit.digest, argvDigest: argvDigest(this.engine.command, [...this.engine.preArgs, ...this.piArgs]) };
|
||||
}
|
||||
|
||||
// The line for a pins mismatch against a claim's recorded pins.
|
||||
static pinLine(recorded, since) {
|
||||
return oldPinRecord(recorded) ? OLD_PIN_LINE : `the engine pin or launch argv changed since ${since}`;
|
||||
}
|
||||
|
||||
// Every read after construction refuses a header ID other than the one the
|
||||
@@ -341,7 +557,7 @@ export class Controller {
|
||||
async start() {
|
||||
this.#guardCheck("bind");
|
||||
this.#checkSeal();
|
||||
const pins = this.#pins();
|
||||
const pins = await this.#pins();
|
||||
const session = this.#readSession();
|
||||
const classified = await this.store.classify(this.seatK, this.sessionK, {
|
||||
units: this.units,
|
||||
@@ -373,7 +589,7 @@ export class Controller {
|
||||
let priorRef = null;
|
||||
if (prior.n > 0 && !prior.damaged && prior.record.state === "stopped") {
|
||||
const p = prior.record;
|
||||
if (!equal(p.pins, pins)) throw new ControlRefusal(ENGINE_PIN_MISMATCH, "the engine pin or launch argv changed since the proven stop");
|
||||
if (!equal(p.pins, pins)) throw new ControlRefusal(ENGINE_PIN_MISMATCH, Controller.pinLine(p.pins, "the proven stop"));
|
||||
const leaf = p.leafAtProof !== undefined ? p.leafAtProof : p.leaf;
|
||||
const branch = p.branchAtProof ?? p.branch;
|
||||
if (leaf !== session.leaf || branch !== session.branch) throw new ControlRefusal("target", "the session branch or leaf changed since the proven stop");
|
||||
@@ -1679,11 +1895,11 @@ export class Controller {
|
||||
if (b.state !== "stopped" || b.admission !== "closed" || !this.#stopped(s)) return refused("stop-proof");
|
||||
let pins;
|
||||
try {
|
||||
pins = this.#pins();
|
||||
pins = await this.#pins();
|
||||
} catch (err) {
|
||||
return refused(err.code ?? ENGINE_PIN_MISMATCH);
|
||||
return refused(err.code ?? ENGINE_PIN_MISMATCH, typeof err?.message === "string" ? { detail: err.message } : {});
|
||||
}
|
||||
if (!equal(pins, this.claim.record.pins)) return refused(ENGINE_PIN_MISMATCH);
|
||||
if (!equal(pins, this.claim.record.pins)) return refused(ENGINE_PIN_MISMATCH, { detail: Controller.pinLine(this.claim.record.pins, "the proven stop") });
|
||||
const session = this.#readSession();
|
||||
if (session.leaf !== this.claim.record.leafAtProof || session.branch !== this.claim.record.branchAtProof) return refused("target");
|
||||
if (!this.#checkConfirmation(r, c, "recover")) return refused("confirmation");
|
||||
@@ -1715,7 +1931,7 @@ export class Controller {
|
||||
throw new ControlRefusal(ELIGIBILITY, "the reserved claim changed since eligibility");
|
||||
}
|
||||
}
|
||||
const pins = this.#pins();
|
||||
const pins = await this.#pins();
|
||||
if (!equal(pins, e.pins)) throw new ControlRefusal(ENGINE_PIN_MISMATCH, "the engine pin or launch argv changed since eligibility");
|
||||
const session = this.#readSession();
|
||||
if (session.leaf !== e.leaf || session.branch !== e.branch) throw new ControlRefusal("target", "the session branch or leaf changed since eligibility (K18)");
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
// The Pi pin and the engine seal (#1507, CHAT-03 §3, lead decisions 31–32).
|
||||
// The engine seal and the engine's pins (#1507, CHAT-03 §3, lead decisions
|
||||
// 31–32; #1873 pi-unpin S4).
|
||||
//
|
||||
// Pin: package-lock.json and npm's installed record
|
||||
// (node_modules/.package-lock.json) must both name the pinned version with the
|
||||
// pinned integrity. That ties the install to the package through npm's record;
|
||||
// it is not a hash of the files on disk. `pi` runs dist/bundle/cli.js, the
|
||||
// package's bin, and the built-in llama.cpp extension ships inside it.
|
||||
// Pins: no pi version is pinned (Jason, 2026-11-19). The controller runs the
|
||||
// pi binary.mjs's lookup finds (MOSAIC_PI_BIN, else the first pi on PATH, else
|
||||
// the repository's) once the fit check (packages/harness/src/pi-fit.mjs)
|
||||
// passes it. The claim's pins are what that check reports: `engineVersion`,
|
||||
// the `--version` text (null when pi printed none), and `enginePin`, the
|
||||
// sha256 digest of the files that run. A pi that fails the check, or can't be
|
||||
// checked, is refused `engine-feature-missing`; a digest or argv that differs
|
||||
// from a claim's is refused `engine-pin-mismatch`. A claim made before #1873
|
||||
// holds npm's sha512 integrity in `enginePin`, which no digest equals, so it
|
||||
// refuses once with OLD_PIN_LINE.
|
||||
//
|
||||
// Seal: the controller builds the launch argv. It always carries
|
||||
// --no-extensions, --no-prompt-templates, --no-themes and --no-approve, and
|
||||
// never an --extension argument (cli/args.js; usage.md 224 and 233–236).
|
||||
// --no-extensions, --no-prompt-templates, --no-themes and --no-approve, plus
|
||||
// --no-mcp when the pi's --help lists it, and never an --extension argument
|
||||
// (cli/args.js; usage.md 224 and 233–236).
|
||||
// --no-approve sets the project trust override to false, so a project's
|
||||
// .pi/settings.json, SYSTEM.md, APPEND_SYSTEM.md and skills don't load even
|
||||
// when trust.json under the agent dir trusts it (main.js 574–581;
|
||||
@@ -23,60 +30,45 @@
|
||||
// file, so the argv must be exactly the controller's prefix followed by
|
||||
// ENGINE_OPTIONS pairs, each at most once with one plain value.
|
||||
|
||||
import { readFileSync } from "node:fs";
|
||||
import { isAbsolute, join } from "node:path";
|
||||
import { isAbsolute } from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import { ControlRefusal } from "./safe-fs.mjs";
|
||||
|
||||
export const PI_PACKAGE = "@earendil-works/pi-coding-agent";
|
||||
export const PI_VERSION = "0.85.1";
|
||||
export const PI_INTEGRITY = "sha512-FGRN+OHbWaefBPGaTggAdLjrIHW+s2PzLyglz/5dfLzb9of7uuXMXYC0fJIeZTw+shS32o2cuQ9jF7YSDuL/oQ==";
|
||||
export const PI_BIN = join("node_modules", PI_PACKAGE, "dist", "bundle", "cli.js");
|
||||
export const SEAL_FLAGS = Object.freeze(["--no-extensions", "--no-prompt-templates", "--no-themes", "--no-approve"]);
|
||||
export const ENGINE_OPTIONS = Object.freeze(["--model", "--provider", "--thinking"]);
|
||||
|
||||
export const ENGINE_PIN_MISMATCH = "engine-pin-mismatch";
|
||||
export const UNSEALED_ENGINE = "unsealed-engine";
|
||||
export const ENGINE_FEATURE_MISSING = "engine-feature-missing";
|
||||
|
||||
function lockEntry(path) {
|
||||
let lock;
|
||||
try {
|
||||
lock = JSON.parse(readFileSync(path, "utf8"));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
const entry = lock?.packages?.[`node_modules/${PI_PACKAGE}`];
|
||||
return entry && typeof entry === "object" ? entry : null;
|
||||
// The seal flags for a pi whose --help does (noMcp) or doesn't list --no-mcp.
|
||||
export const sealFlags = (noMcp = false) => (noMcp ? [...SEAL_FLAGS, "--no-mcp"] : [...SEAL_FLAGS]);
|
||||
|
||||
// An `enginePin` the fit check gives is a sha256 hex digest.
|
||||
export const PIN_DIGEST = /^[0-9a-f]{64}$/;
|
||||
export const OLD_PIN_LINE = "the claim was made under the old pin record; start a new claim";
|
||||
// True when a claim's pins came from the old npm pin record: an `enginePin`
|
||||
// string that isn't a digest (npm's integrity is `sha512-...`).
|
||||
export const oldPinRecord = (pins) => typeof pins?.enginePin === "string" && !PIN_DIGEST.test(pins.enginePin);
|
||||
|
||||
export function buildPiArgs({ sessionFile, extraArgs = [], noMcp = false }) {
|
||||
return ["--mode", "rpc", ...sealFlags(noMcp), "--session", sessionFile, ...extraArgs];
|
||||
}
|
||||
|
||||
// `root` holds package-lock.json and node_modules/.package-lock.json.
|
||||
export function checkEnginePin(root) {
|
||||
for (const path of [join(root, "package-lock.json"), join(root, "node_modules", ".package-lock.json")]) {
|
||||
const entry = lockEntry(path);
|
||||
if (!entry || entry.version !== PI_VERSION || entry.integrity !== PI_INTEGRITY) {
|
||||
throw new ControlRefusal(ENGINE_PIN_MISMATCH, `${path} does not pin ${PI_PACKAGE} ${PI_VERSION} with the pinned integrity`);
|
||||
}
|
||||
}
|
||||
return { version: PI_VERSION, pin: PI_INTEGRITY };
|
||||
}
|
||||
|
||||
export function buildPiArgs({ sessionFile, extraArgs = [] }) {
|
||||
return ["--mode", "rpc", ...SEAL_FLAGS, "--session", sessionFile, ...extraArgs];
|
||||
}
|
||||
|
||||
// Refuses any argv that is not `--mode rpc`, the four --no-* flags and
|
||||
// `--session <absolute path>`, in that order, followed by ENGINE_OPTIONS
|
||||
// pairs. That covers --extension in either spelling, a second --mode or
|
||||
// Refuses any argv that is not `--mode rpc`, the four --no-* flags (and
|
||||
// --no-mcp when noMcp), and `--session <absolute path>`, in that order,
|
||||
// followed by ENGINE_OPTIONS pairs. That covers --extension in either spelling, a second --mode or
|
||||
// --session, session and output flags (--no-session, --fork, --export, ...)
|
||||
// and stray prompt words.
|
||||
export function checkSeal(args) {
|
||||
export function checkSeal(args, { noMcp = false } = {}) {
|
||||
if (!Array.isArray(args) || args.some((a) => typeof a !== "string")) throw new ControlRefusal(UNSEALED_ENGINE, "launch argv is not a list of strings");
|
||||
const extension = args.find((a) => a === "-e" || a === "--extension" || a.startsWith("--extension="));
|
||||
if (extension !== undefined) throw new ControlRefusal(UNSEALED_ENGINE, `launch argv carries ${extension}`);
|
||||
for (const flag of SEAL_FLAGS) {
|
||||
const flags = sealFlags(noMcp);
|
||||
for (const flag of flags) {
|
||||
if (!args.includes(flag)) throw new ControlRefusal(UNSEALED_ENGINE, `launch argv lacks ${flag}`);
|
||||
}
|
||||
const prefix = ["--mode", "rpc", ...SEAL_FLAGS, "--session"];
|
||||
const prefix = ["--mode", "rpc", ...flags, "--session"];
|
||||
if (prefix.some((a, i) => args[i] !== a)) throw new ControlRefusal(UNSEALED_ENGINE, `launch argv does not start with ${prefix.join(" ")}`);
|
||||
const file = args[prefix.length];
|
||||
if (typeof file !== "string" || !isAbsolute(file)) throw new ControlRefusal(UNSEALED_ENGINE, "the --session value is not an absolute path");
|
||||
@@ -1,6 +1,6 @@
|
||||
// Run tracking, overlap signals and receipt settlement (#1507, CHAT-03 §3).
|
||||
//
|
||||
// Pinned Pi ties no event to a prompt. Under the seal (pi-pin.mjs) the Mosaic
|
||||
// Pi ties no event to a prompt. Under the seal (pi-seal.mjs) the Mosaic
|
||||
// prompt in the slot is the only thing that can start a run, so a run that
|
||||
// follows the slot's ack is attributed to it by order. The tracker watches
|
||||
// for signs that the seal failed (O1–O6) and for lead decision 34's
|
||||
|
||||
@@ -5,23 +5,27 @@
|
||||
|
||||
import { test, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { appendFileSync, copyFileSync, linkSync, mkdirSync, statSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, unlinkSync, writeFileSync, lstatSync } from "node:fs";
|
||||
import { appendFileSync, chmodSync, copyFileSync, linkSync, mkdirSync, statSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, unlinkSync, writeFileSync, lstatSync } from "node:fs";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { dirname, join } from "node:path";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { types as utilTypes } from "node:util";
|
||||
import { ALREADY_ACTIVE, ClaimStore, FOREIGN_HOST, UNSAFE_REPLACEMENT, held, machineId } from "../src/claim.mjs";
|
||||
import { Controller } from "../src/controller.mjs";
|
||||
import { Controller, REPO_ROOT } from "../src/controller.mjs";
|
||||
import { ConversationClient } from "../src/client.mjs";
|
||||
import { AUTHORITY, scopeAvailable } from "../src/cohort.mjs";
|
||||
import { FixtureVerifier } from "../src/records.mjs";
|
||||
import { LIVE_SESSION_REFUSED } from "../src/guard.mjs";
|
||||
import { ENGINE_PIN_MISMATCH, PI_PACKAGE } from "../src/pi-pin.mjs";
|
||||
import { ENGINE_FEATURE_MISSING, ENGINE_PIN_MISMATCH, OLD_PIN_LINE, UNSEALED_ENGINE } from "../src/pi-seal.mjs";
|
||||
import { piFit } from "../../harness/src/pi-fit.mjs";
|
||||
import { levels } from "../../plugins/harness/pi/index.mjs";
|
||||
import { bootId } from "../../discord/src/journal.mjs";
|
||||
import { FakeLauncher } from "./fake-pi.mjs";
|
||||
import {
|
||||
REPO, FAST, fixture, controllerFor, started, spawnController, killChildren, cleanupAll, reap, claimRecords,
|
||||
userEntry, assistantEntry, thinkingEntry, receiptState, noUnits, tick, killShims, shimsGone,
|
||||
PI_PACKAGE, standInPi, piAt,
|
||||
} from "./harness.mjs";
|
||||
|
||||
const reaped = [];
|
||||
@@ -579,14 +583,8 @@ test("W8: resume after a proven stop with the same pins: new claim ID, generatio
|
||||
|
||||
test("W9: resume with a changed binary, argv digest, branch or leaf is refused and the claim is unchanged", async () => {
|
||||
const cases = {
|
||||
binary: (fx) => {
|
||||
const root = mkdtempSync(join(fx.base, "pin-"));
|
||||
const lock = { packages: { [`node_modules/${PI_PACKAGE}`]: { version: "0.85.2", integrity: "sha512-other" } } };
|
||||
mkdirSync(join(root, "node_modules"));
|
||||
writeFileSync(join(root, "package-lock.json"), JSON.stringify(lock));
|
||||
writeFileSync(join(root, "node_modules", ".package-lock.json"), JSON.stringify(lock));
|
||||
return [{ pinRoot: root }, ENGINE_PIN_MISMATCH];
|
||||
},
|
||||
// Another pi, which the fit check passes, with other files.
|
||||
binary: (fx) => [{ pi: standInPi(join(fx.base, "other-pi"), { version: "0.85.2" }).found }, ENGINE_PIN_MISMATCH],
|
||||
argv: () => [{ engine: { extraArgs: ["--model", "other"] } }, ENGINE_PIN_MISMATCH],
|
||||
leaf: (fx) => {
|
||||
appendFileSync(fx.sessionFile, JSON.stringify(userEntry("d4e5f6a7", "b2c3d4e5", "outside", 9)) + "\n");
|
||||
@@ -610,6 +608,410 @@ test("W9: resume with a changed binary, argv digest, branch or leaf is refused a
|
||||
}
|
||||
});
|
||||
|
||||
// ---- #1873 pi-unpin S4: the engine is the pi the lookup finds --------------
|
||||
|
||||
test("S4a: a stand-in pi with another version and every feature launches, and the claim records its version and digest", async () => {
|
||||
const fx = fixture();
|
||||
const pi = standInPi(join(fx.base, "pi"), { version: "7.4.2" });
|
||||
const h = await started({ fx, pi: pi.found });
|
||||
try {
|
||||
assert.equal(h.launcher.launches.length, 1);
|
||||
const fit = await piFit(pi.found, { levels: levels() });
|
||||
assert.equal(fit.ok, true, fit.message);
|
||||
const recorded = h.ctrl.claim.record.pins;
|
||||
assert.equal(recorded.engineVersion, "7.4.2");
|
||||
assert.equal(recorded.enginePin, fit.digest);
|
||||
assert.match(recorded.enginePin, /^[0-9a-f]{64}$/);
|
||||
} finally {
|
||||
await h.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("S4b: a stand-in pi whose --help lacks --mode rpc is refused engine-feature-missing with a plain line; nothing launches or is claimed", async () => {
|
||||
const fx = fixture();
|
||||
const pi = standInPi(join(fx.base, "pi"), { modes: ["text", "json"] });
|
||||
const k = keysFor(fx, { pi: pi.found });
|
||||
const { ctrl, launcher } = controllerFor(fx, { pi: pi.found });
|
||||
try {
|
||||
await assert.rejects(ctrl.start(), (e) => {
|
||||
assert.equal(e.code, ENGINE_FEATURE_MISSING);
|
||||
assert.equal(e.message, "pi 9.9.9 (MOSAIC_PI_BIN) lacks --mode rpc");
|
||||
return true;
|
||||
});
|
||||
assert.equal(launcher.launches.length, 0);
|
||||
assert.deepEqual([revisionTexts(k.store, k.seatK), revisionTexts(k.store, k.sessionK)], [[], []]);
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
});
|
||||
|
||||
test("S4c: changed bytes in the pi between launch and resume refuse engine-pin-mismatch; the claim is unchanged", async () => {
|
||||
const fx = fixture();
|
||||
const pi = standInPi(join(fx.base, "pi"));
|
||||
const first = await provenStop(fx, { pi: pi.found });
|
||||
appendFileSync(pi.cli, "// one more line\n");
|
||||
const k = keysFor(fx, { pi: pi.found });
|
||||
const before = [revisionTexts(k.store, k.seatK), revisionTexts(k.store, k.sessionK)];
|
||||
const { ctrl, launcher } = controllerFor(fx, { pi: piAt(pi.bin) });
|
||||
try {
|
||||
await assert.rejects(ctrl.start(), (e) => {
|
||||
assert.equal(e.code, ENGINE_PIN_MISMATCH);
|
||||
assert.equal(e.message, "the engine pin or launch argv changed since the proven stop");
|
||||
return true;
|
||||
});
|
||||
assert.equal(launcher.launches.length, 0);
|
||||
assert.deepEqual([revisionTexts(k.store, k.seatK), revisionTexts(k.store, k.sessionK)], before);
|
||||
assert.equal(first.pins.engineVersion, "9.9.9");
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
});
|
||||
|
||||
test("S4d: a pre-argument that is not the resolved bin's real file refuses unsealed-engine at bind, as does a bin link moved after construction", async () => {
|
||||
const fx = fixture();
|
||||
const pi = standInPi(join(fx.base, "pi"));
|
||||
const make = () => new Controller({ fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat, launcher: new FakeLauncher(), units: noUnits, timeouts: FAST, pi: pi.found });
|
||||
{
|
||||
const ctrl = make();
|
||||
try {
|
||||
assert.deepEqual(ctrl.engine.preArgs, [realpathSync(pi.cli)]);
|
||||
// The repository's own pi, which the old pin accepted.
|
||||
ctrl.engine.preArgs = [join(REPO_ROOT, "node_modules", PI_PACKAGE, "dist", "bundle", "cli.js")];
|
||||
await assert.rejects(ctrl.start(), (e) => e.code === UNSEALED_ENGINE);
|
||||
assert.equal(ctrl.launcher.launches.length, 0);
|
||||
// The bin link itself, not its real file.
|
||||
ctrl.engine.preArgs = [pi.bin];
|
||||
await assert.rejects(ctrl.start(), (e) => e.code === UNSEALED_ENGINE);
|
||||
assert.equal(ctrl.launcher.launches.length, 0);
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
}
|
||||
{
|
||||
const ctrl = make();
|
||||
const other = standInPi(join(fx.base, "pi-other"), { version: "1.0.0" });
|
||||
try {
|
||||
unlinkSync(pi.bin);
|
||||
symlinkSync(other.cli, pi.bin);
|
||||
await assert.rejects(ctrl.start(), (e) => e.code === UNSEALED_ENGINE);
|
||||
assert.equal(ctrl.launcher.launches.length, 0);
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("S4e: a claim made under the old pin record (npm's integrity in enginePin) refuses engine-pin-mismatch with the old-record line at every start of that session", async () => {
|
||||
const fx = fixture();
|
||||
const first = await provenStop(fx);
|
||||
// Rewrite the stopped heads as a claim made before #1873 would hold them.
|
||||
for (const { path, record } of claimRecords(fx)) {
|
||||
if (record?.claimId !== first.claimId || record.state !== "stopped") continue;
|
||||
const old = { ...record, pins: { ...record.pins, engineVersion: "0.85.1", enginePin: "sha512-FGRN+OHbWaefBPGaTggAdLjrIHW+s2PzLyglz/5dfLzb9of7uuXMXYC0fJIeZTw+shS32o2cuQ9jF7YSDuL/oQ==" } };
|
||||
unlinkSync(path);
|
||||
writeFileSync(path, JSON.stringify(old), { mode: 0o400 });
|
||||
}
|
||||
// Like any pin mismatch, the refusal holds at each start: the session's last
|
||||
// proven stop is what a resume is checked against.
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const { ctrl, launcher } = controllerFor(fx);
|
||||
try {
|
||||
await assert.rejects(ctrl.start(), (e) => {
|
||||
assert.equal(e.code, ENGINE_PIN_MISMATCH);
|
||||
assert.equal(e.message, OLD_PIN_LINE);
|
||||
assert.match(e.message, /old pin record; start a new claim/);
|
||||
return true;
|
||||
});
|
||||
assert.equal(launcher.launches.length, 0);
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("S4f: a pi whose first line doesn't run node is refused engine-feature-missing, though the fit check passes it; #!/usr/bin/env node launches", async () => {
|
||||
const fx = fixture();
|
||||
// The sh form of the stand-in passes the fit check, which runs the bin as a
|
||||
// program, but the engine runs the bin's real file with node.
|
||||
const sh = standInPi(join(fx.base, "pi-sh"), { form: "sh" });
|
||||
const fit = await piFit(sh.found, { levels: levels() });
|
||||
assert.equal(fit.ok, true, fit.message);
|
||||
const k = keysFor(fx, { pi: sh.found });
|
||||
{
|
||||
const { ctrl, launcher } = controllerFor(fx, { pi: sh.found });
|
||||
try {
|
||||
await assert.rejects(ctrl.start(), (e) => {
|
||||
assert.equal(e.code, ENGINE_FEATURE_MISSING);
|
||||
assert.equal(e.message, "MOSAIC_PI_BIN isn't a node script, and the engine runs pi with node");
|
||||
return true;
|
||||
});
|
||||
assert.equal(launcher.launches.length, 0);
|
||||
assert.deepEqual([revisionTexts(k.store, k.seatK), revisionTexts(k.store, k.sessionK)], [[], []]);
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
}
|
||||
// The node form with the shebang npm writes for pi.
|
||||
const env = standInPi(join(fx.base, "pi-env"));
|
||||
const text = readFileSync(env.cli, "utf8");
|
||||
writeFileSync(env.cli, "#!/usr/bin/env node" + text.slice(text.indexOf("\n")));
|
||||
const h = await started({ fx, pi: env.found });
|
||||
try {
|
||||
assert.equal(h.launcher.launches.length, 1);
|
||||
assert.equal(h.launcher.launches[0].args[0], realpathSync(env.cli));
|
||||
} finally {
|
||||
await h.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("S4g: a lookup answer that is no longer the lookup's refuses unsealed-engine: a package link moved since the lookup, a hand-built answer with another bin, one field changed by hand, a bin no package holds, or a getter; an answer that changes after construction changes nothing", async () => {
|
||||
const fx = fixture();
|
||||
const good = standInPi(join(fx.base, "pi-good"));
|
||||
const lacking = standInPi(join(fx.base, "pi-lacking"), { modes: ["text", "json"] });
|
||||
// A global prefix whose package directory is a link, as a version manager
|
||||
// keeps it.
|
||||
const prefix = join(fx.base, "prefix");
|
||||
mkdirSync(join(prefix, "lib", "node_modules", "@earendil-works"), { recursive: true });
|
||||
mkdirSync(join(prefix, "bin"));
|
||||
const link = join(prefix, "lib", "node_modules", PI_PACKAGE);
|
||||
symlinkSync(good.pkg, link);
|
||||
const bin = join(prefix, "bin", "pi");
|
||||
symlinkSync(`../lib/node_modules/${PI_PACKAGE}/dist/bundle/cli.js`, bin);
|
||||
const found = piAt(bin);
|
||||
assert.equal(found.bin, bin);
|
||||
const make = (pi) => new Controller({ fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat, launcher: new FakeLauncher(), units: noUnits, timeouts: FAST, pi });
|
||||
const ctrl = make(found);
|
||||
try {
|
||||
unlinkSync(link);
|
||||
symlinkSync(lacking.pkg, link);
|
||||
// Built before the move: the start refuses.
|
||||
await assert.rejects(ctrl.start(), (e) => {
|
||||
assert.equal(e.code, UNSEALED_ENGINE);
|
||||
assert.equal(e.message, "MOSAIC_PI_BIN no longer matches the lookup's answer; look the pi up again");
|
||||
return true;
|
||||
});
|
||||
assert.equal(ctrl.launcher.launches.length, 0);
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
// Built after the move, with the stale answer.
|
||||
assert.throws(() => make(found), (e) => e.code === UNSEALED_ENGINE);
|
||||
// An answer with a bin swapped in by hand.
|
||||
assert.throws(() => make({ ...good.found, bin: lacking.bin }), (e) => e.code === UNSEALED_ENGINE);
|
||||
assert.throws(() => make({ bin: good.bin, label: "MOSAIC_PI_BIN" }), (e) => e.code === UNSEALED_ENGINE);
|
||||
// One field changed by hand at a time: the fit check would mount, run or
|
||||
// digest another package than the launch runs.
|
||||
for (const k of ["root", "dest", "view", "pkg"]) {
|
||||
const value = k === "dest" ? lacking.found.root : lacking.found[k];
|
||||
assert.throws(() => make({ ...good.found, [k]: value }), (e) => e.code === UNSEALED_ENGINE, k);
|
||||
}
|
||||
// The lacking pi's answer with the good package mounted over its path.
|
||||
assert.throws(() => make({ ...lacking.found, root: good.found.root, dest: lacking.found.root }), (e) => e.code === UNSEALED_ENGINE);
|
||||
// A bin the lookup no longer takes (no package holds it) refuses when the
|
||||
// controller is built.
|
||||
const loose = join(fx.base, "loose-pi");
|
||||
writeFileSync(loose, "#!/usr/bin/env node\n", { mode: 0o755 });
|
||||
assert.throws(() => make({ bin: loose, label: "MOSAIC_PI_BIN" }), (e) => e.code === UNSEALED_ENGINE);
|
||||
// A getter is not a data field: the copy the controller keeps lacks it.
|
||||
assert.throws(() => make({ ...good.found, get view() { return good.found.view; } }), (e) => e.code === UNSEALED_ENGINE);
|
||||
// An answer that changes after the controller is built changes nothing: it
|
||||
// keeps a frozen plain copy, and the start checks and launches the good pi.
|
||||
let flipped = false;
|
||||
const live = new Proxy({ ...good.found }, {
|
||||
get: (t, k) => (flipped && k in lacking.found ? lacking.found[k] : t[k]),
|
||||
getOwnPropertyDescriptor: (t, k) => {
|
||||
const d = Reflect.getOwnPropertyDescriptor(t, k);
|
||||
return d && flipped && k in lacking.found ? { ...d, value: lacking.found[k] } : d;
|
||||
},
|
||||
});
|
||||
const { ctrl: kept, launcher: keptLauncher } = controllerFor(fx, { pi: live });
|
||||
try {
|
||||
flipped = true;
|
||||
assert.ok(Object.isFrozen(kept.pi));
|
||||
assert.equal(utilTypes.isProxy(kept.pi), false);
|
||||
assert.deepEqual(kept.pi, good.found);
|
||||
await kept.start();
|
||||
assert.equal(keptLauncher.launches.length, 1);
|
||||
assert.equal(keptLauncher.launches[0].args[0], realpathSync(good.cli));
|
||||
} finally {
|
||||
await kept.close({ killEngine: true }).catch(() => {});
|
||||
}
|
||||
// The answer as the lookup gives it still builds.
|
||||
await make(piAt(good.bin)).close();
|
||||
});
|
||||
|
||||
test("S4h: a first line the kernel and node read differently refuses engine-feature-missing: an absolute node that isn't the engine's, a first line past 256 bytes, a CR before the newline, under env a node from the package, a node the fit check's sandbox can't see ahead of the first it can (even a link to the engine's node), an empty, relative or unplain entry before the first node, one in /proc or /dev, one that can't be checked, or a first shown node that isn't the engine's; the engine's own directory first launches, and a label that isn't the lookup's kind reads \"the pi\", in the fit check's line too; a lookup's problem line shows, a hand-built one with a path or a newline doesn't", async () => {
|
||||
const fx = fixture();
|
||||
const refused = async (pi, message, f = fx) => {
|
||||
const { ctrl, launcher } = controllerFor(f, { pi });
|
||||
try {
|
||||
await assert.rejects(ctrl.start(), (e) => {
|
||||
assert.equal(e.code, ENGINE_FEATURE_MISSING);
|
||||
assert.equal(e.message, message);
|
||||
return true;
|
||||
});
|
||||
assert.equal(launcher.launches.length, 0);
|
||||
} finally {
|
||||
await ctrl.close().catch(() => {});
|
||||
}
|
||||
};
|
||||
const withFirstLine = (name, line) => {
|
||||
const pi = standInPi(join(fx.base, name));
|
||||
const text = readFileSync(pi.cli, "utf8");
|
||||
writeFileSync(pi.cli, line + text.slice(text.indexOf("\n")));
|
||||
return pi;
|
||||
};
|
||||
// A program named node in the package that runs another program: here the
|
||||
// real node, so the fit check passes it.
|
||||
const shim = standInPi(join(fx.base, "pi-shim"));
|
||||
const fakeNode = join(dirname(shim.cli), "node");
|
||||
writeFileSync(fakeNode, `#!/bin/sh\nexec ${process.execPath} "$@"\n`, { mode: 0o755 });
|
||||
const shimText = readFileSync(shim.cli, "utf8");
|
||||
writeFileSync(shim.cli, `#!${fakeNode}` + shimText.slice(shimText.indexOf("\n")));
|
||||
const fit = await piFit(shim.found, { levels: levels() });
|
||||
assert.equal(fit.ok, true, fit.message);
|
||||
await refused(shim.found, "MOSAIC_PI_BIN's first line runs another node than the engine's");
|
||||
// The kernel reads 256 bytes for the first line; this one ends past them.
|
||||
const long = withFirstLine("pi-long", "#!/usr/bin/env node" + " ".repeat(260));
|
||||
await refused(long.found, "MOSAIC_PI_BIN isn't a node script, and the engine runs pi with node");
|
||||
// The kernel looks for a program named with the CR.
|
||||
const cr = withFirstLine("pi-cr", "#!/usr/bin/env node\r");
|
||||
await refused(cr.found, "MOSAIC_PI_BIN isn't a node script, and the engine runs pi with node");
|
||||
// A link to the engine's own node is the same file.
|
||||
const linked = standInPi(join(fx.base, "pi-linked"));
|
||||
const nodeLink = join(dirname(linked.cli), "node");
|
||||
symlinkSync(process.execPath, nodeLink);
|
||||
const linkedText = readFileSync(linked.cli, "utf8");
|
||||
writeFileSync(linked.cli, `#!${nodeLink}` + linkedText.slice(linkedText.indexOf("\n")));
|
||||
const h = await started({ fx, pi: linked.found });
|
||||
try {
|
||||
assert.equal(h.launcher.launches.length, 1);
|
||||
assert.equal(h.launcher.launches[0].args[0], realpathSync(linked.cli));
|
||||
} finally {
|
||||
await h.close();
|
||||
}
|
||||
// #!/usr/bin/env node takes node from the fit check's PATH, which is this
|
||||
// process's PATH behind the repository's .bin.
|
||||
const envPi = withFirstLine("pi-env-path", "#!/usr/bin/env node");
|
||||
const fx2 = fixture();
|
||||
const onPath = async (dir, fn) => {
|
||||
const saved = process.env.PATH;
|
||||
process.env.PATH = `${dir}:${saved}`;
|
||||
try {
|
||||
await fn();
|
||||
} finally {
|
||||
process.env.PATH = saved;
|
||||
}
|
||||
};
|
||||
// A node in the package itself, put on PATH.
|
||||
const pkgBin = join(envPi.pkg, "bin");
|
||||
mkdirSync(pkgBin);
|
||||
writeFileSync(join(pkgBin, "node"), `#!/bin/sh\nexec ${process.execPath} "$@"\n`, { mode: 0o755 });
|
||||
await onPath(pkgBin, () => refused(envPi.found, "MOSAIC_PI_BIN's package puts a node on the fit check's PATH", fx2));
|
||||
// The fit check's sandbox shows only /usr, /etc, the host's /bin, /lib,
|
||||
// /lib64 and /sbin, and the engine's own node file. A node in any other
|
||||
// directory ahead of the engine's: env in the sandbox would pass it by,
|
||||
// while the host finds it first. Even a link to the engine's node.
|
||||
const otherBin = join(fx.base, "other-bin");
|
||||
mkdirSync(otherBin);
|
||||
writeFileSync(join(otherBin, "node"), `#!/bin/sh\nexec ${process.execPath} "$@"\n`, { mode: 0o755 });
|
||||
const hidden = "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, where a node the fit check can't see comes first";
|
||||
await onPath(otherBin, () => refused(envPi.found, hidden, fx2));
|
||||
const linkBin = join(fx.base, "link-bin");
|
||||
mkdirSync(linkBin);
|
||||
symlinkSync(process.execPath, join(linkBin, "node"));
|
||||
await onPath(linkBin, () => refused(envPi.found, hidden, fx2));
|
||||
// env reads an empty or relative entry against the fit check's directory.
|
||||
const relative = "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, which has an empty or relative entry";
|
||||
await onPath("", () => refused(envPi.found, relative, fx2));
|
||||
await onPath("some/bin", () => refused(envPi.found, relative, fx2));
|
||||
// Up to the first node, an entry must read the same in the sandbox as on
|
||||
// the host. A ".." after a link goes where the link points, not where the
|
||||
// text does.
|
||||
symlinkSync("/usr/lib", join(fx.base, "sys"));
|
||||
await onPath(`${fx.base}/sys/../bin`, () => refused(envPi.found, "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, which has an entry that isn't in plain form", fx2));
|
||||
// The sandbox makes its own /proc and /dev: pid 1 there is its own, and a
|
||||
// link into /proc counts too, even past a pid the host lacks.
|
||||
const ownCopy = "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, which has an entry the fit check's sandbox has its own copy of";
|
||||
await onPath("/proc/1/root/usr/bin", () => refused(envPi.found, ownCopy, fx2));
|
||||
await onPath("/dev/mosaic-no-such-dir", () => refused(envPi.found, ownCopy, fx2));
|
||||
symlinkSync("/proc", join(fx.base, "p"));
|
||||
await onPath(`${fx.base}/p/999999999/root/usr/bin`, () => refused(envPi.found, ownCopy, fx2));
|
||||
// A node link that leads nowhere on the host can't be checked: what the
|
||||
// sandbox finds at its target need not be what the host lacks there.
|
||||
const dangling = join(fx.base, "dangling-bin");
|
||||
mkdirSync(dangling);
|
||||
symlinkSync("/tmp/mosaic-no-such-dir/node", join(dangling, "node"));
|
||||
await onPath(dangling, () => refused(envPi.found, "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, where a node can't be checked", fx2));
|
||||
// An entry the host can't read can't be checked.
|
||||
const locked = join(fx.base, "locked");
|
||||
mkdirSync(join(locked, "bin"), { recursive: true });
|
||||
chmodSync(locked, 0o000);
|
||||
try {
|
||||
await onPath(join(locked, "bin"), () => refused(envPi.found, "MOSAIC_PI_BIN's first line runs node from the fit check's PATH, where an entry can't be checked", fx2));
|
||||
} finally {
|
||||
chmodSync(locked, 0o755);
|
||||
}
|
||||
// The first node the sandbox shows must be the engine's: no node at all
|
||||
// isn't, and neither is a system node that is another file.
|
||||
const notEngine = "MOSAIC_PI_BIN's first line runs the first node on the fit check's PATH, which isn't the engine's";
|
||||
const withPath = async (path, fn) => {
|
||||
const saved = process.env.PATH;
|
||||
process.env.PATH = path;
|
||||
try {
|
||||
await fn();
|
||||
} finally {
|
||||
process.env.PATH = saved;
|
||||
}
|
||||
};
|
||||
await withPath("/usr/share", () => refused(envPi.found, notEngine, fx2));
|
||||
const systemNode = ["/usr/bin/node", "/usr/local/bin/node"].find((n) => {
|
||||
try {
|
||||
return realpathSync(n) !== realpathSync(process.execPath);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
if (systemNode) await withPath(`${dirname(systemNode)}:${dirname(process.execPath)}`, () => refused(envPi.found, notEngine, fx2));
|
||||
// The engine's own directory first: the sandbox shows that file at the same
|
||||
// path, so env runs the engine's node in both.
|
||||
// Entries after it are never read, so a trailing slash on it and an empty
|
||||
// entry at the end change nothing. The sandbox's /tmp starts empty, so a
|
||||
// /tmp entry with no node on the host holds none there either.
|
||||
await withPath(`/tmp/mosaic-no-such-dir:${dirname(process.execPath)}/:/usr/bin:/bin:`, async () => {
|
||||
const h2 = await started({ fx: fx2, pi: envPi.found });
|
||||
try {
|
||||
assert.equal(h2.launcher.launches.length, 1);
|
||||
assert.equal(h2.launcher.launches[0].args[0], realpathSync(envPi.cli));
|
||||
} finally {
|
||||
await h2.close();
|
||||
}
|
||||
});
|
||||
// A hand-built label that is a path never reaches a refusal line.
|
||||
await refused({ ...shim.found, label: join(fx.base, "named") }, "the pi's first line runs another node than the engine's");
|
||||
await refused({ ...shim.found, label: "MOSAIC_PI_BIN\nrefused: forged" }, "the pi's first line runs another node than the engine's");
|
||||
// Nor the fit check's own line.
|
||||
const lacking = standInPi(join(fx.base, "pi-lacking"), { modes: ["text", "json"] });
|
||||
await refused({ ...lacking.found, label: join(fx.base, "named") }, "pi 9.9.9 (the pi) lacks --mode rpc");
|
||||
// A lookup that found no pi refuses when the controller is built: its own
|
||||
// line, which names the step and the package, never a path; a hand-built
|
||||
// line with a path or a newline isn't shown.
|
||||
const unbuilt = (pi, message) =>
|
||||
assert.throws(() => controllerFor(fx, { pi }), (e) => {
|
||||
assert.equal(e.code, ENGINE_FEATURE_MISSING);
|
||||
assert.equal(e.message, message);
|
||||
return true;
|
||||
});
|
||||
unbuilt(piAt("relative/pi"), "MOSAIC_PI_BIN isn't an executable file at an absolute path");
|
||||
const loose = join(fx.base, "loose-pi");
|
||||
writeFileSync(loose, "#!/usr/bin/env node\n", { mode: 0o755 });
|
||||
unbuilt(piAt(loose), `MOSAIC_PI_BIN isn't part of the ${PI_PACKAGE} package`);
|
||||
unbuilt({ bin: null, problem: `${fx.base} has no pi` }, "the pi lookup gave no binary");
|
||||
unbuilt({ bin: null, problem: "no pi\nrefused: forged" }, "the pi lookup gave no binary");
|
||||
unbuilt({ bin: null, problem: "no pi\u2028refused: forged" }, "the pi lookup gave no binary");
|
||||
unbuilt({ bin: null, problem: "no pi\u0085refused: forged" }, "the pi lookup gave no binary");
|
||||
});
|
||||
|
||||
// ---- W11, W16, W17 ----------------------------------------------------------
|
||||
|
||||
test("W11: the controller writes no session file; only the fake engine's own appends appear", async () => {
|
||||
|
||||
@@ -9,17 +9,17 @@
|
||||
|
||||
import { test, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { appendFileSync, chmodSync, copyFileSync, mkdirSync, readFileSync, renameSync, rmdirSync, writeFileSync } from "node:fs";
|
||||
import { appendFileSync, chmodSync, mkdirSync, readFileSync, renameSync, rmdirSync, writeFileSync } from "node:fs";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { dirname, join } from "node:path";
|
||||
import { ClaimStore, FOREIGN_HOST, defaultHost, newClaimId, unitNameFor } from "../src/claim.mjs";
|
||||
import { ConversationClient } from "../src/client.mjs";
|
||||
import { AUTHORITY, PgroupLauncher, ScopeLauncher, engineExitCohort, forceStopCohort, releaseCohort, scopeAvailable, shimRequest, systemctlShow, systemdUnits } from "../src/cohort.mjs";
|
||||
import { Controller, ELIGIBILITY, TEST_ENGINE } from "../src/controller.mjs";
|
||||
import { ENGINE_ENV, ENGINE_PIN_MISMATCH, engineEnv } from "../src/pi-pin.mjs";
|
||||
import { ENGINE_ENV, ENGINE_PIN_MISMATCH, engineEnv } from "../src/pi-seal.mjs";
|
||||
import { FixtureVerifier, newId } from "../src/records.mjs";
|
||||
import { ControlClient, FakeLauncher } from "./fake-pi.mjs";
|
||||
import { FAST, REPO, assistantEntry, claimRecords, cleanupAll, controllerFor, fixture, killChildren, killShims, liveShims, noUnits, reap, receiptState, shimsGone, spawnController, started, tick } from "./harness.mjs";
|
||||
import { FAST, PI, assistantEntry, claimRecords, cleanupAll, controllerFor, fixture, killChildren, killShims, liveShims, noUnits, reap, receiptState, shimsGone, spawnController, standInPi, started, tick } from "./harness.mjs";
|
||||
|
||||
const reaped = [];
|
||||
const strays = new Set();
|
||||
@@ -111,7 +111,7 @@ async function live({ kind = "scope", barrier = null, verifier = new FixtureVeri
|
||||
fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat,
|
||||
launcher: launcher ?? (kind === "scope" ? new ScopeLauncher() : new PgroupLauncher()),
|
||||
engine: { cwd: fx.proj }, [TEST_ENGINE]: { command: process.execPath, preArgs: [FAKE_PI], env: { ...process.env, FAKE_PI_CONTROL: control, FAKE_PI_LOG: join(fx.base, "fake.log") } },
|
||||
verifier, units: kind === "scope" ? systemdUnits : noUnits, timeouts, barrier,
|
||||
verifier, units: kind === "scope" ? systemdUnits : noUnits, timeouts, barrier, pi: PI.found,
|
||||
});
|
||||
await ctrl.start();
|
||||
assert.equal(ctrl.binding.state, "active", JSON.stringify(ctrl.evidence.uncertain));
|
||||
@@ -532,14 +532,6 @@ async function provenStop(h, c = h.client) {
|
||||
return fs.stop.id;
|
||||
}
|
||||
|
||||
function pinRootCopy(fx) {
|
||||
const root = join(fx.base, "pins");
|
||||
mkdirSync(join(root, "node_modules"), { recursive: true });
|
||||
copyFileSync(join(REPO, "package-lock.json"), join(root, "package-lock.json"));
|
||||
copyFileSync(join(REPO, "node_modules", ".package-lock.json"), join(root, "node_modules", ".package-lock.json"));
|
||||
return root;
|
||||
}
|
||||
|
||||
test("K6: recover without proof, without confirmation, or with changed pins is refused", async () => {
|
||||
{
|
||||
const h = await started();
|
||||
@@ -562,8 +554,8 @@ test("K6: recover without proof, without confirmation, or with changed pins is r
|
||||
}
|
||||
{
|
||||
const fx = track(fixture());
|
||||
const pinRoot = pinRootCopy(fx);
|
||||
const h = await started({ fx, pinRoot });
|
||||
const pi = standInPi(join(fx.base, "pi"));
|
||||
const h = await started({ fx, pi: pi.found });
|
||||
try {
|
||||
const stop = await provenStop(h);
|
||||
const missing = await h.client.request("recover", { stop });
|
||||
@@ -574,13 +566,13 @@ test("K6: recover without proof, without confirmation, or with changed pins is r
|
||||
const id = issued.data.confirmation.id;
|
||||
await h.client.request("answer-confirmation", { confirmation: id, answer: "confirm" });
|
||||
assert.equal((await h.client.request("recover", { stop, confirmation: id })).refusal, "confirmation", "a confirmation for another operation");
|
||||
const lock = join(pinRoot, "package-lock.json");
|
||||
const original = readFileSync(lock, "utf8");
|
||||
const changed = JSON.parse(original);
|
||||
changed.packages["node_modules/@earendil-works/pi-coding-agent"].version = "0.0.0";
|
||||
writeFileSync(lock, JSON.stringify(changed));
|
||||
assert.equal((await h.client.confirmed("recover", { stop })).refusal, ENGINE_PIN_MISMATCH);
|
||||
writeFileSync(lock, original);
|
||||
// Other bytes in the pi the lookup finds, then the same bytes again.
|
||||
const original = readFileSync(pi.cli, "utf8");
|
||||
writeFileSync(pi.cli, `${original}// other bytes\n`);
|
||||
const changed = await h.client.confirmed("recover", { stop });
|
||||
assert.equal(changed.refusal, ENGINE_PIN_MISMATCH);
|
||||
assert.equal(changed.detail, "the engine pin or launch argv changed since the proven stop");
|
||||
writeFileSync(pi.cli, original);
|
||||
const again = await h.client.request("issue-confirmation", { operationToConfirm: "recover" });
|
||||
const once = again.data.confirmation.id;
|
||||
await h.client.request("answer-confirmation", { confirmation: once, answer: "confirm" });
|
||||
@@ -1177,7 +1169,7 @@ function scopeController(fx, { host, verifier = new FixtureVerifier({ authoritie
|
||||
fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat,
|
||||
launcher, engine: { cwd: fx.proj },
|
||||
[TEST_ENGINE]: { command: process.execPath, preArgs: [FAKE_PI], env: { ...process.env, FAKE_PI_CONTROL: join(fx.base, "fake-b.sock"), FAKE_PI_LOG: join(fx.base, "fake-b.log") } },
|
||||
verifier, units: systemdUnits, timeouts: FAST, barrier, host,
|
||||
verifier, units: systemdUnits, timeouts: FAST, barrier, host, pi: PI.found,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@ import { Controller, TEST_ENGINE } from "../src/controller.mjs";
|
||||
import { AUTHORITY, PgroupLauncher, ScopeLauncher, systemdUnits } from "../src/cohort.mjs";
|
||||
import { FixtureVerifier } from "../src/records.mjs";
|
||||
import { defaultHost } from "../src/claim.mjs";
|
||||
import { locate } from "../../plugins/harness/pi/binary.mjs";
|
||||
|
||||
const cfg = JSON.parse(process.argv[2]);
|
||||
const out = (v) => process.stdout.write(JSON.stringify(v) + "\n");
|
||||
@@ -79,7 +80,7 @@ try {
|
||||
engine: { cwd: fx.proj }, [TEST_ENGINE]: { command: process.execPath, preArgs: [fakePi], env: { ...process.env, ...(cfg.fakeEnv ?? {}) } },
|
||||
verifier: cfg.verifier === false ? null : new FixtureVerifier({ authorities: [AUTHORITY] }),
|
||||
units: cfg.units === "absent" ? { lookup: async () => ({ state: "absent" }) } : systemdUnits,
|
||||
host, barrier, timeouts: cfg.timeouts,
|
||||
host, barrier, timeouts: cfg.timeouts, pi: locate({ MOSAIC_PI_BIN: cfg.piBin, PATH: "/usr/bin:/bin" }),
|
||||
});
|
||||
const started = await ctrl.start();
|
||||
out({ ready: true, started, socketPath: ctrl.socketPath, incarnation: ctrl.incarnation, pid: process.pid, binding: ctrl.binding, claim: ctrl.claim ? { claimId: ctrl.claim.claimId, record: ctrl.claim.record } : null });
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// directory: fixture root, project, session file, claim root and socket
|
||||
// directory. Nothing here touches a live session.
|
||||
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync } from "node:fs";
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, rmSync, symlinkSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
import { spawnSync } from "node:child_process";
|
||||
@@ -11,15 +11,38 @@ 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 { fixtureDir, piStandIn } from "../../harness/tests/pi-stand-in.mjs";
|
||||
import { locate } from "../../plugins/harness/pi/binary.mjs";
|
||||
|
||||
export const REPO = resolve(import.meta.dirname, "..", "..", "..");
|
||||
export const SESSION_ID = "0f5e1c2a-1111-4222-8333-944455556666";
|
||||
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,
|
||||
// which the fit check passes unless its knobs say otherwise) under `dir`,
|
||||
// with an npm-style bin link. `found` is the lookup's answer for it, through
|
||||
// MOSAIC_PI_BIN. The test engine still launches fake-pi.mjs; the stand-in is
|
||||
// what the controller's fit check and pins read.
|
||||
export const PI_PACKAGE = "@earendil-works/pi-coding-agent";
|
||||
export function standInPi(dir, knobs = {}) {
|
||||
const pkg = join(dir, "prefix", "lib", "node_modules", PI_PACKAGE);
|
||||
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: PI_PACKAGE, version: knobs.version ?? "9.9.9", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
const cli = join(pkg, "dist", "bundle", "cli.js");
|
||||
writeFileSync(cli, piStandIn(knobs, { form: knobs.form ?? "node" }), { mode: 0o755 });
|
||||
mkdirSync(join(dir, "prefix", "bin"));
|
||||
const bin = join(dir, "prefix", "bin", "pi");
|
||||
symlinkSync(`../lib/node_modules/${PI_PACKAGE}/dist/bundle/cli.js`, bin);
|
||||
return { bin, cli, pkg, found: piAt(bin) };
|
||||
}
|
||||
export const piAt = (bin) => locate({ MOSAIC_PI_BIN: bin, PATH: "/usr/bin:/bin" });
|
||||
// The pi every fixture controller checks unless a test passes its own.
|
||||
export const PI = standInPi(join(tmp, "pi"));
|
||||
|
||||
export function cleanupAll() {
|
||||
spawnSync("chmod", ["-R", "u+rwx", tmp]);
|
||||
rmSync(tmp, { recursive: true, force: true });
|
||||
@@ -51,7 +74,7 @@ export const noUnits = Object.freeze({ lookup: async () => ({ state: "absent" })
|
||||
export function controllerFor(fx, { launcher = new FakeLauncher(), verifier = new FixtureVerifier({ authorities: [AUTHORITY] }), timeouts = FAST, ...opts } = {}) {
|
||||
const ctrl = new Controller({
|
||||
fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat,
|
||||
launcher, verifier, units: noUnits, timeouts, ...opts,
|
||||
launcher, verifier, units: noUnits, timeouts, pi: PI.found, ...opts,
|
||||
});
|
||||
return { ctrl, launcher, verifier };
|
||||
}
|
||||
@@ -118,7 +141,7 @@ export function killChildren() {
|
||||
// Spawns ctrl-child.mjs. `msgs` collects its JSON lines; `next(pred)` waits
|
||||
// for one.
|
||||
export function spawnController(cfg) {
|
||||
const proc = spawn(process.execPath, [CHILD, JSON.stringify({ timeouts: FAST, ...cfg })], { stdio: ["pipe", "pipe", "pipe"] });
|
||||
const proc = spawn(process.execPath, [CHILD, JSON.stringify({ timeouts: FAST, piBin: PI.bin, ...cfg })], { stdio: ["pipe", "pipe", "pipe"] });
|
||||
childPids.add(proc.pid);
|
||||
const msgs = [];
|
||||
const waiters = new Set();
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// CHAT-03 (#1507): the pinned Pi binary, started sealed in a scratch home with
|
||||
// CHAT-03 (#1507): the repository's Pi binary, started sealed in a scratch home with
|
||||
// no credentials, answers the RPC commands the controller sends with the
|
||||
// shapes the fake engine models. No prompt is sent, so no model is called.
|
||||
//
|
||||
// It also records pinned Pi's startup append (sdk.js 240–252): a session
|
||||
// It also records Pi 0.85.1's startup append (sdk.js 240–252): a session
|
||||
// whose branch has no thinking_level_change entry gains one at every start,
|
||||
// so the leaf moves after launch and the K8 load check fails closed on it.
|
||||
//
|
||||
@@ -16,12 +16,20 @@ import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { PassThrough } from "node:stream";
|
||||
import { LineSplitter, encodeLine } from "../src/framing.mjs";
|
||||
import { PI_BIN, PI_VERSION, buildPiArgs, checkEnginePin, checkSeal } from "../src/pi-pin.mjs";
|
||||
import { buildPiArgs, checkSeal } from "../src/pi-seal.mjs";
|
||||
import { piFit } from "../../harness/src/pi-fit.mjs";
|
||||
import { locate } from "../../plugins/harness/pi/binary.mjs";
|
||||
import { levels } from "../../plugins/harness/pi/index.mjs";
|
||||
import { FakePi } from "./fake-pi.mjs";
|
||||
import { REPO, assistantEntry, header, thinkingEntry, userEntry } from "./harness.mjs";
|
||||
import { assistantEntry, 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 = {};
|
||||
@@ -90,9 +98,11 @@ async function realPi(tag, sessionFile, { project = "proj", trusted = null, afte
|
||||
assert.equal(existsSync(join(home, ".pi", "agent", "auth.json")), false);
|
||||
assert.deepEqual(readdirSync(agent), [], "the agent dir starts empty");
|
||||
if (trusted) writeFileSync(join(agent, "trust.json"), JSON.stringify({ [realpathSync(join(scratch, trusted))]: true }));
|
||||
const args = buildPiArgs({ sessionFile });
|
||||
checkSeal(args);
|
||||
const pi = spawn(process.execPath, [join(REPO, PI_BIN), ...args, ...after], { cwd: join(scratch, project), env, stdio: ["pipe", "pipe", "pipe"] });
|
||||
const fit = await piFit(REPO_PI, { levels: levels() });
|
||||
assert.equal(fit.ok, true, fit.message);
|
||||
const args = buildPiArgs({ sessionFile, noMcp: fit.noMcp });
|
||||
checkSeal(args, { noMcp: fit.noMcp });
|
||||
const pi = spawn(process.execPath, [realpathSync(REPO_PI.bin), ...args, ...after], { cwd: join(scratch, project), env, stdio: ["pipe", "pipe", "pipe"] });
|
||||
let stderr = "";
|
||||
pi.stderr.on("data", (c) => (stderr += c));
|
||||
const exited = new Promise((r) => pi.on("exit", (code, signal) => r({ code, signal })));
|
||||
@@ -118,11 +128,15 @@ function fakePi(sessionFile) {
|
||||
const lastId = (file) => JSON.parse(readFileSync(file, "utf8").trim().split("\n").at(-1)).id;
|
||||
const typesOf = (data) => Object.fromEntries(Object.entries(data ?? {}).map(([k, v]) => [k, v === null ? "null" : Array.isArray(v) ? "array" : typeof v]));
|
||||
|
||||
test("the engine pin holds for the installed package", () => {
|
||||
assert.deepEqual(checkEnginePin(REPO).version, PI_VERSION);
|
||||
test("the repository's pi passes the fit check and gives a version and a digest", async () => {
|
||||
assert.equal(REPO_PI.label, "the repository's pi");
|
||||
const fit = await piFit(REPO_PI, { levels: levels() });
|
||||
assert.equal(fit.ok, true, fit.message);
|
||||
assert.equal(fit.version, "0.85.1");
|
||||
assert.match(fit.digest, /^[0-9a-f]{64}$/);
|
||||
});
|
||||
|
||||
test("pinned Pi, sealed and without credentials, answers the controller's commands with the shapes the fake models", async () => {
|
||||
test("the repository's Pi, sealed and without credentials, answers the controller's commands with the shapes the fake models", async () => {
|
||||
const entries = [thinkingEntry(), userEntry("a1b2c3d4", "f0e1d2c3", "hello"), assistantEntry("b2c3d4e5", "a1b2c3d4", "hi")];
|
||||
const realFile = session("real.jsonl", entries);
|
||||
const fakeFile = session("fake.jsonl", entries);
|
||||
@@ -155,7 +169,7 @@ test("pinned Pi, sealed and without credentials, answers the controller's comman
|
||||
assert.equal(r.command, f.command, type);
|
||||
assert.equal(r.success, f.success, type);
|
||||
const rt = typesOf(r.data), ft = typesOf(f.data);
|
||||
for (const [k, t] of Object.entries(ft)) assert.equal(rt[k], t, `${type}.${k}: the fake models a field pinned Pi doesn't send that way`);
|
||||
for (const [k, t] of Object.entries(ft)) assert.equal(rt[k], t, `${type}.${k}: the fake models a field the repository's Pi doesn't send that way`);
|
||||
}
|
||||
// clear_queue: one queue_update with both queues empty precedes the response, in both.
|
||||
for (const side of [real, fake]) {
|
||||
@@ -165,7 +179,7 @@ test("pinned Pi, sealed and without credentials, answers the controller's comman
|
||||
}
|
||||
});
|
||||
|
||||
test("sealed, pinned Pi ignores a trusted project's .pi resources; --approve past the seal would load them, and checkSeal refuses it (Filbert F2 on #1522)", async () => {
|
||||
test("sealed Pi ignores a trusted project's .pi resources; --approve past the seal would load them, and checkSeal refuses it (Filbert F2 on #1522)", async () => {
|
||||
const skill = join(scratch, "trusted", ".pi", "skills", "probe");
|
||||
mkdirSync(skill, { recursive: true });
|
||||
writeFileSync(join(skill, "SKILL.md"), "---\nname: probe\ndescription: A project skill that only a trusted load offers.\n---\nprobe\n");
|
||||
@@ -183,7 +197,7 @@ test("sealed, pinned Pi ignores a trusted project's .pi resources; --approve pas
|
||||
}
|
||||
});
|
||||
|
||||
test("pinned Pi appends thinking_level_change at start when the branch lacks one, so the leaf moves (K8 then fails closed)", async () => {
|
||||
test("the repository's Pi appends thinking_level_change at start when the branch lacks one, so the leaf moves (K8 then fails closed)", async () => {
|
||||
const entries = [userEntry("a1b2c3d4", null, "hello"), assistantEntry("b2c3d4e5", "a1b2c3d4", "hi")];
|
||||
const realFile = session("bare-real.jsonl", entries);
|
||||
const fakeFile = session("bare-fake.jsonl", entries);
|
||||
@@ -197,5 +211,5 @@ test("pinned Pi appends thinking_level_change at start when the branch lacks one
|
||||
assert.notEqual(side.responses.get_tree.data.leafId, "b2c3d4e5", "the leaf moved off the loaded leaf");
|
||||
assert.equal(side.responses.get_tree.data.leafId, added[0].id);
|
||||
}
|
||||
if (process.env.CHAT03_SMOKE_OUT) writeFileSync(process.env.CHAT03_SMOKE_OUT, JSON.stringify({ pi: PI_VERSION, commands: COMMANDS, exchanges }, null, 2) + "\n");
|
||||
if (process.env.CHAT03_SMOKE_OUT) writeFileSync(process.env.CHAT03_SMOKE_OUT, JSON.stringify({ pi: (await piFit(REPO_PI, { levels: levels() })).version, commands: COMMANDS, exchanges }, null, 2) + "\n");
|
||||
});
|
||||
@@ -5,14 +5,16 @@
|
||||
import { test, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { PassThrough } from "node:stream";
|
||||
import { realpathSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { Controller, TEST_ENGINE, REPO_ROOT, HANDLED_WITHOUT_RUN, ACK_WITHOUT_START, NO_TURN, RUN_OVERLAP, TRANSPORT_UNKNOWN } from "../src/controller.mjs";
|
||||
import { AUTHORITY } from "../src/cohort.mjs";
|
||||
import { FixtureVerifier, sha256 } from "../src/records.mjs";
|
||||
import { ENGINE_ENV, PI_BIN, SEAL_FLAGS, UNSEALED_ENGINE, checkSeal } from "../src/pi-pin.mjs";
|
||||
import { ENGINE_ENV, SEAL_FLAGS, UNSEALED_ENGINE, checkSeal } from "../src/pi-seal.mjs";
|
||||
import { locate } from "../../plugins/harness/pi/binary.mjs";
|
||||
import { LineSplitter, encodeLine, parseLine } from "../src/framing.mjs";
|
||||
import { BUSY_ERROR, FakeLauncher, FakePi } from "./fake-pi.mjs";
|
||||
import { fixture, started, receiptState, outcomeUnknownPush, sessionText, cleanupAll, tick, noUnits, FAST } from "./harness.mjs";
|
||||
import { fixture, started, receiptState, outcomeUnknownPush, sessionText, cleanupAll, tick, noUnits, FAST, PI, PI_PACKAGE, standInPi } from "./harness.mjs";
|
||||
|
||||
after(() => cleanupAll());
|
||||
|
||||
@@ -982,12 +984,38 @@ test("N24: the seal is an allow-list: --extension, a missing --no-* flag, a seco
|
||||
}
|
||||
});
|
||||
|
||||
test("N24c: when --help lists --no-mcp the launch passes it after the seal flags, and the seal then requires it (#1873 S4)", async () => {
|
||||
const fx = fixture();
|
||||
const pi = standInPi(join(fx.base, "pi"), { extraFlags: ["--no-mcp"] });
|
||||
const h = await started({ fx, pi: pi.found });
|
||||
try {
|
||||
const args = h.launcher.launches[0].args.slice(1);
|
||||
assert.deepEqual(args.slice(0, SEAL_FLAGS.length + 5), ["--mode", "rpc", ...SEAL_FLAGS, "--no-mcp", "--session", fx.sessionFile]);
|
||||
assert.equal(h.ctrl.noMcp, true);
|
||||
assert.equal(checkSeal(args, { noMcp: true }), true);
|
||||
const without = args.filter((a) => a !== "--no-mcp");
|
||||
assert.throws(() => checkSeal(without, { noMcp: true }), (e) => e.code === UNSEALED_ENGINE);
|
||||
assert.throws(() => checkSeal(args), (e) => e.code === UNSEALED_ENGINE, "--no-mcp where help lacks it");
|
||||
} finally {
|
||||
await h.close();
|
||||
}
|
||||
// The shared stand-in's help lacks --no-mcp, so its launch has none.
|
||||
const plain = await started({ fx: fixture() });
|
||||
try {
|
||||
assert.equal(plain.launcher.launches[0].args.includes("--no-mcp"), false);
|
||||
assert.equal(plain.ctrl.noMcp, false);
|
||||
} finally {
|
||||
await plain.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("N24b: the seal covers the engine command and environment: config can't name either, the env is built from names, and a mutated command is refused at bind", async () => {
|
||||
const fx = fixture();
|
||||
const make = (extra) => new Controller({ fixtureRoot: fx.base, claimRoot: fx.claimRoot, socketDir: fx.socketDir, sessionFile: fx.sessionFile, seat: fx.seat, launcher: new FakeLauncher(), units: noUnits, timeouts: FAST, ...extra });
|
||||
// The plain engine option takes extraArgs, cwd and envKeys; the command,
|
||||
// pre-arguments and environment are the controller's (I3, #1507).
|
||||
for (const engine of [{ command: "/bin/sh" }, { preArgs: [join(REPO_ROOT, PI_BIN)] }, { command: process.execPath, preArgs: [join(REPO_ROOT, PI_BIN), "--extension", "x"] }, { env: {} }, { env: process.env }, null, [], "pi"]) {
|
||||
const repoCli = join(REPO_ROOT, "node_modules", PI_PACKAGE, "dist", "bundle", "cli.js");
|
||||
for (const engine of [{ command: "/bin/sh" }, { preArgs: [repoCli] }, { command: process.execPath, preArgs: [repoCli, "--extension", "x"] }, { env: {} }, { env: process.env }, null, [], "pi"]) {
|
||||
assert.throws(() => make({ engine }), (e) => e.code === UNSEALED_ENGINE, JSON.stringify(engine));
|
||||
}
|
||||
// A configuration is JSON, which can't carry the symbol-keyed test engine.
|
||||
@@ -1001,7 +1029,7 @@ test("N24b: the seal covers the engine command and environment: config can't nam
|
||||
assert.throws(() => make({ [TEST_ENGINE]: { command: process.execPath, preArgs: ["fake.mjs", "-e", "x"], env: {} } }), (e) => e.code === UNSEALED_ENGINE);
|
||||
assert.throws(() => make({ [TEST_ENGINE]: { command: "", preArgs: [], env: {} } }), (e) => e.code === UNSEALED_ENGINE);
|
||||
|
||||
// The launch: this Node, the pinned bin, and an environment of named keys
|
||||
// The launch: this Node, the real file of the pi the lookup found, and an environment of named keys
|
||||
// only, whatever the controller's own environment holds.
|
||||
const planted = { NODE_OPTIONS: "--require /tmp/x.cjs", LD_PRELOAD: "/tmp/x.so", PI_PACKAGE_DIR: "/tmp/pkg", ZAI_API_KEY: "zai-test-value", OTHER_API_KEY: "other-test-value" };
|
||||
const saved = Object.fromEntries(Object.keys(planted).map((k) => [k, process.env[k]]));
|
||||
@@ -1015,7 +1043,8 @@ test("N24b: the seal covers the engine command and environment: config can't nam
|
||||
try {
|
||||
const l = h.launcher.launches[0];
|
||||
assert.equal(l.command, process.execPath);
|
||||
assert.equal(l.args[0], join(REPO_ROOT, PI_BIN));
|
||||
assert.equal(l.args[0], realpathSync(PI.bin));
|
||||
assert.equal(l.args[0], h.ctrl.engine.preArgs[0]);
|
||||
assert.equal(l.env.ZAI_API_KEY, "zai-test-value");
|
||||
for (const k of Object.keys(l.env)) assert.ok(ENGINE_ENV.includes(k) || k === "ZAI_API_KEY", k);
|
||||
for (const k of ["NODE_OPTIONS", "LD_PRELOAD", "PI_PACKAGE_DIR", "OTHER_API_KEY"]) assert.equal(k in l.env, false, k);
|
||||
@@ -1027,7 +1056,21 @@ test("N24b: the seal covers the engine command and environment: config can't nam
|
||||
// A command changed after construction is refused at bind; nothing launches.
|
||||
const fx2 = fixture();
|
||||
const launcher = new FakeLauncher();
|
||||
const ctrl = new Controller({ fixtureRoot: fx2.base, claimRoot: fx2.claimRoot, socketDir: fx2.socketDir, sessionFile: fx2.sessionFile, seat: fx2.seat, launcher, units: noUnits, timeouts: FAST });
|
||||
// With no pi option the controller takes the one the lookup finds in its own
|
||||
// environment, read when it is built (#1873 S4): here MOSAIC_PI_BIN names a
|
||||
// stand-in, which no other step of the lookup would find.
|
||||
const own = standInPi(join(fx2.base, "env-pi"));
|
||||
const savedBin = process.env.MOSAIC_PI_BIN;
|
||||
process.env.MOSAIC_PI_BIN = own.bin;
|
||||
let ctrl;
|
||||
try {
|
||||
ctrl = new Controller({ fixtureRoot: fx2.base, claimRoot: fx2.claimRoot, socketDir: fx2.socketDir, sessionFile: fx2.sessionFile, seat: fx2.seat, launcher, units: noUnits, timeouts: FAST });
|
||||
assert.deepEqual(ctrl.pi, locate(process.env));
|
||||
} finally {
|
||||
if (savedBin === undefined) delete process.env.MOSAIC_PI_BIN; else process.env.MOSAIC_PI_BIN = savedBin;
|
||||
}
|
||||
assert.equal(ctrl.pi.label, "MOSAIC_PI_BIN");
|
||||
assert.deepEqual(ctrl.engine.preArgs, [realpathSync(own.cli)]);
|
||||
// Closed in finally: a start that wrongly succeeds holds the socket open.
|
||||
try {
|
||||
ctrl.engine.command = "/bin/sh";
|
||||
|
||||
@@ -30,7 +30,12 @@ scripts/discord-service.sh render | install | uninstall | status <binding>
|
||||
`--repo` for you; the CLI also takes `--config PATH`.
|
||||
|
||||
- `check` validates the binding, the token file (0600, regular, not a
|
||||
symlink), the context files and the pi install; reads the bot, the guild and
|
||||
symlink), the context files and the pi the harness lookup finds
|
||||
(`MOSAIC_PI_BIN`, else the first `pi` on `PATH`, else the repository's;
|
||||
#1873 S4). `--repo` no longer picks the pi, and the CLI runs no fit check
|
||||
on it yet, so a pi on `PATH` is used as it is. The service wrapper still
|
||||
checks that the copy has its own `node_modules/.bin/pi`, but the CLI may run
|
||||
another pi that the lookup finds first. It then reads the bot, the guild and
|
||||
every listed channel over REST; opens one gateway connection, waits for
|
||||
READY, closes it. Nothing is sent to a channel. Close code 4014 is reported
|
||||
as the message-content intent not being granted in the developer portal.
|
||||
|
||||
@@ -46,7 +46,7 @@
|
||||
// 3 refused by a brake (STOP present or the binding held; a supervisor must
|
||||
// not retry); 4 usage.
|
||||
|
||||
import { existsSync, mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
||||
import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import { DiscordError, printable } from "./errors.mjs";
|
||||
@@ -57,6 +57,7 @@ import { defaultConfigPath, loadDataRoot, bindingPath, bindingDataDir, loadBindi
|
||||
import { createRest } from "./rest.mjs";
|
||||
import { createGateway, CONNECTOR_INTENTS } from "./gateway.mjs";
|
||||
import { createEngine, buildPiArgs, sessionContinues } from "./engine-pi.mjs";
|
||||
import { locate } from "../../plugins/harness/pi/binary.mjs";
|
||||
import { TOOLS_ENV, enabledToolNames } from "./tools.mjs";
|
||||
import { createSetsparkApi } from "./setspark.mjs";
|
||||
import { assembleContext } from "./context.mjs";
|
||||
@@ -134,8 +135,11 @@ function prepare(opts) {
|
||||
checkLibraryEngine(dirname(opts.config), binding);
|
||||
const contextFiles = resolveContextFiles(binding, opts.repo);
|
||||
const toolRoots = resolveToolRoots(binding, { dataRoot });
|
||||
const pi = join(opts.repo, "node_modules", ".bin", "pi");
|
||||
if (!existsSync(pi)) throw new DiscordError("pi not found at node_modules/.bin/pi in the repository; run npm ci in the repository");
|
||||
// #1873 S4: the pi the harness lookup finds (MOSAIC_PI_BIN, the first pi on PATH, the
|
||||
// repository's), not only the repository's copy.
|
||||
const found = locate(process.env);
|
||||
if (!found.bin) throw new DiscordError(found.problem);
|
||||
const pi = found.bin;
|
||||
const journalDir = bindingDataDir(dataRoot, binding.name);
|
||||
const sessionDir = join(dataRoot, "sessions", `discord-${binding.name}`);
|
||||
return { dataRoot, bindingFile, binding, contextFiles, toolRoots, pi, journalDir, sessionDir };
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// Shared fakes for the offline suite. No network, no token, no model.
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, rmSync, chmodSync } from "node:fs";
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, rmSync, chmodSync, symlinkSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { after } from "node:test";
|
||||
@@ -53,12 +53,19 @@ export function binding(overrides = {}) {
|
||||
return validateBinding(rawBinding(overrides));
|
||||
}
|
||||
|
||||
// A fake repository with the files prepare() looks for.
|
||||
// A fake repository with the files prepare() looks for. Its pi is a package the
|
||||
// harness lookup accepts, and MOSAIC_PI_BIN names it for every CLI this test
|
||||
// process starts (#1873 S4), so no test reads the host's own pi.
|
||||
export function makeRepo(root) {
|
||||
const repo = join(root, "repo");
|
||||
const pkg = join(repo, "node_modules", "@earendil-works", "pi-coding-agent");
|
||||
mkdirSync(join(repo, "node_modules", ".bin"), { recursive: true });
|
||||
mkdirSync(join(pkg, "dist"), { recursive: true });
|
||||
mkdirSync(join(repo, "contracts"), { recursive: true });
|
||||
writeFileSync(join(repo, "node_modules", ".bin", "pi"), "#!/bin/sh\nexit 0\n", { mode: 0o755 });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "0.0.0-test", bin: { pi: "dist/cli.js" } }));
|
||||
writeFileSync(join(pkg, "dist", "cli.js"), "#!/bin/sh\nexit 0\n", { mode: 0o755 });
|
||||
symlinkSync("../@earendil-works/pi-coding-agent/dist/cli.js", join(repo, "node_modules", ".bin", "pi"));
|
||||
process.env.MOSAIC_PI_BIN = join(repo, "node_modules", ".bin", "pi");
|
||||
writeFileSync(join(repo, "contracts", "CONSTITUTION.md"), "# constitution\nbe good\n");
|
||||
return repo;
|
||||
}
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {spawnSync} from 'node:child_process';
|
||||
import {writeFileSync,chmodSync,unlinkSync,existsSync} from 'node:fs';
|
||||
import {join} from 'node:path';
|
||||
import {writeFileSync,chmodSync,unlinkSync,existsSync,mkdirSync,symlinkSync} from 'node:fs';
|
||||
import {dirname,join} from 'node:path';
|
||||
import {fileURLToPath} from 'node:url';
|
||||
import {makeRoot,makeRepo,makeDeployment} from './helpers.mjs';
|
||||
const CLI=fileURLToPath(new URL('../src/cli.mjs',import.meta.url));
|
||||
function setup(){
|
||||
const root=makeRoot(),repo=makeRepo(root),d=makeDeployment(root,{}, {tokenContent:'MTAw.abcdefghijklmnopqrstuvwxyz0123456789'}),calls=join(root,'network-called'),preload=join(root,'no-network.mjs');
|
||||
writeFileSync(preload,`import {writeFileSync} from 'node:fs';const refuse=()=>{writeFileSync(${JSON.stringify(calls)},'called');throw Error('fixture refuses network')};globalThis.fetch=refuse;globalThis.WebSocket=class{constructor(){refuse()}};`);
|
||||
const run=(command='check',args=[])=>spawnSync(process.execPath,['--import',preload,CLI,command,d.raw.name,'--config',d.config,'--repo',repo,...args],{encoding:'utf8',timeout:5000});
|
||||
const run=(command='check',args=[],env=process.env)=>spawnSync(process.execPath,['--import',preload,CLI,command,d.raw.name,'--config',d.config,'--repo',repo,...args],{encoding:'utf8',timeout:5000,env});
|
||||
return {root,repo,d,calls,run};
|
||||
}
|
||||
test('check --offline validates local fixture without REST or gateway calls',()=>{
|
||||
const s=setup(),r=s.run('check',['--offline']);assert.equal(r.status,0,r.stderr);assert.match(r.stdout,/token file mode 0600 ok; STOP absent/);assert.match(r.stdout,/offline check passed; no REST or gateway calls were made\n$/);assert.ok(!existsSync(s.calls));
|
||||
// #1873 S4: the fixture's own pi, which the helpers name in MOSAIC_PI_BIN, never the host's.
|
||||
assert.ok(r.stdout.includes(`pi ${join(s.repo,'node_modules','.bin','pi')};`),r.stdout);
|
||||
// Online check still reaches REST: the trap fails it immediately.
|
||||
const online=s.run();assert.equal(online.status,1);assert.ok(existsSync(s.calls));assert.match(online.stderr,/fixture refuses network/);
|
||||
});
|
||||
@@ -24,3 +26,12 @@ test('check --offline refuses token mode and missing context under the selected
|
||||
test('run --offline is a usage refusal before reading config or networking',()=>{
|
||||
const s=setup();unlinkSync(s.d.config);const r=s.run('run',['--offline']);assert.equal(r.status,4,r.stderr);assert.match(r.stderr,/--offline/);assert.ok(!existsSync(s.calls));
|
||||
});
|
||||
test('#1873 S4: check takes the pi the harness lookup finds; MOSAIC_PI_BIN names it, and a bad one refuses with the lookup line, no path',()=>{
|
||||
// A pi package the way npm installs one under a prefix: its bin is linked from prefix/bin.
|
||||
const s=setup(),pkg=join(s.root,'prefix','lib','node_modules','@earendil-works','pi-coding-agent'),bin=join(s.root,'prefix','bin','pi');
|
||||
mkdirSync(join(pkg,'dist'),{recursive:true});mkdirSync(dirname(bin),{recursive:true});
|
||||
writeFileSync(join(pkg,'package.json'),JSON.stringify({name:'@earendil-works/pi-coding-agent',version:'9.9.9',bin:{pi:'dist/cli.js'}}));
|
||||
writeFileSync(join(pkg,'dist','cli.js'),'#!/bin/sh\nexit 0\n',{mode:0o755});symlinkSync(join(pkg,'dist','cli.js'),bin);
|
||||
const named=s.run('check',['--offline'],{...process.env,MOSAIC_PI_BIN:bin});assert.equal(named.status,0,named.stderr);assert.ok(named.stdout.includes(`pi ${bin};`),named.stdout);
|
||||
const bad=s.run('check',['--offline'],{...process.env,MOSAIC_PI_BIN:'relative/pi'});assert.equal(bad.status,2,bad.stderr);assert.match(bad.stderr,/MOSAIC_PI_BIN isn't an executable file at an absolute path/);assert.ok(!bad.stderr.includes(s.root));assert.ok(!existsSync(s.calls));
|
||||
});
|
||||
+56
-11
@@ -20,17 +20,17 @@ bundle's `manifest.json` names the lines it relies on (`reliesOn`):
|
||||
|
||||
| | Pi | Claude Code | How pi's line is checked |
|
||||
|---|---|---|---|
|
||||
| 1. block a tool | `tool_call` handler returns a block | PreToolUse command hook exits 2; `--bare` is never passed | suites, on the repository's pi (`pi-session.test.mjs`) |
|
||||
| 2. gate crash | a throw in the handler blocks | the hook runs as `<gate> \|\| exit 2` | suites, on the repository's pi |
|
||||
| 1. block a tool | `tool_call` handler returns a block | PreToolUse command hook exits 2; `--bare` is never passed | at first sight of a binary, by the fit check's model run, on the pi that runs; and suites (`pi-session.test.mjs`) |
|
||||
| 2. gate crash | a throw in the handler blocks | the hook runs as `<gate> \|\| exit 2` | at first sight of a binary, by the fit check's model run; and suites |
|
||||
| 3. gate missing | a missing `-e` file refuses to start | the wrapper blocks | at every start, by the fit check, on the pi that runs; and suites |
|
||||
| 4. gate hang | the runner's wall clock plus the `agent_end` marker | `timeout -k 2 10 <gate> \|\| exit 2`, hook timeout 20 | suites, on the repository's pi |
|
||||
| 4. gate hang | the runner's wall clock plus the `agent_end` marker | `timeout -k 2 10 <gate> \|\| exit 2`, hook timeout 20 | the `agent_end` marker: at first sight of a binary, by the fit check's model run; and suites |
|
||||
| 5. bash | limited only by the tool limit | limited only by the tool limit | suites, on the repository's pi |
|
||||
|
||||
Pi is not pinned (#1873 pi-unpin). Before a session starts, `preflight`
|
||||
awaits the fit check in `src/pi-fit.mjs` on the binary the module found
|
||||
(the Triage host and route await the module's `ready` instead), and
|
||||
`env` reads its pass. It runs that binary in a sandbox with the network
|
||||
unshared and calls no model. Each run is a child process the check
|
||||
unshared and calls no real model. Each run is a child process the check
|
||||
awaits, up to four at a time, so the bus's event loop keeps running
|
||||
while it does; callers that ask while a check runs share it. Each run
|
||||
gets the environment a bus session's pi gets, built by the same code
|
||||
@@ -92,10 +92,29 @@ that no session sets. So a branch on `TZ` or `LC_ALL` being unset, or on
|
||||
`/bin/sh` sets an unset `TERM` to `dumb` for the shell without exporting
|
||||
it, so `${TERM:-}` in the adapter is never empty, in a session or here.)
|
||||
It can't see a branch keyed on a
|
||||
value it doesn't try (a model, a path, another tool or provider). A last sealed `-p` run uses `--no-session`, as
|
||||
worker runs do. Each run gets a fresh scratch directory, so no run sees
|
||||
what another left. Not run: `--fork`, `--skill` and the interactive TUI
|
||||
(worker containers and `scripts/agent.sh` only), the branches three or
|
||||
value it doesn't try (a model, a path, another tool or provider). A last `-p` run is the
|
||||
argv the adapter builds in `-p` with `MOSAIC_SKILLS`, one `-e` file, no
|
||||
tools and no session (`workerShape`), so `--no-skills` and then two
|
||||
`--skill` directories, `--no-tools`, `--no-session`, `--provider` and
|
||||
`--model`. A test runs the adapter and holds its argv to `workerShape`.
|
||||
No caller makes this exact run today: task launches refuse skill
|
||||
directories, and only the interactive launch (`scripts/agent.sh`) sets
|
||||
`MOSAIC_SKILLS`, in the TUI, which no fit run can drive. It is the
|
||||
nearest headless form of the adapter's `--skill` argv.
|
||||
The run's `-e` extension must list both skills and no planted one. pi
|
||||
0.85.1 adds a `--skill` directory to discovery, so a pi that let
|
||||
`--skill` bring the planted skills back is refused there, and so is one
|
||||
that drops a `--skill` directory once `--no-skills` is on. This run
|
||||
replaces the plain sealed `-p` run with `--no-session` and no `--skill`,
|
||||
so it adds no run. Each run gets a fresh scratch directory, so no run sees
|
||||
what another left. Not run: `--fork` and the interactive TUI
|
||||
(worker containers and `scripts/agent.sh` only), `-p` with
|
||||
`--no-session` and no `--skill` (a task launch's shape; every bus session
|
||||
passes `--session-dir`), `--skill` beside `--tools`, `--session-dir`,
|
||||
`-c`, `--thinking` or `--mode json`, or in rpc (the TUI launch passes the
|
||||
first three; no bus session passes `--skill`), `--skill` with no `-e`
|
||||
(every fit run needs its `-e` extension to see what loaded), one or three
|
||||
or more `--skill` directories, the branches three or
|
||||
four at a time beyond the five runs, `-c` over a session directory that
|
||||
holds nothing for the project (one more run costs about 340 ms cold),
|
||||
and a session's own values (tools beyond `read` and `bash`, thinking
|
||||
@@ -112,9 +131,35 @@ for example `pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-context-files`. When
|
||||
`--help` lists `--no-mcp`, the adapter passes it too. A pass is cached in
|
||||
memory by a digest of the package's files (and npm's lock files, for
|
||||
the repository's copy) and the names in the runs' environment, so a bus
|
||||
restarted under other names checks again. Lines 1, 2, 4 and 5 are not
|
||||
checked at start; a pi other than the repository's copy relies on them
|
||||
unproved until a scripted-model fit run exists (design section 5, S6).
|
||||
restarted under other names checks again.
|
||||
|
||||
One more run (#1873 S6) talks to a scripted model, so lines 1, 2 and 4
|
||||
and the system prompt are checked on the pi that runs. It is a
|
||||
session-shaped `-p` run with `--tools read`, the gate's `-e` extension
|
||||
and a second one, `src/pi-fit-throw.mjs`, on a stand-in provider and
|
||||
model named `fit-model` (never a Claude model). The model is
|
||||
`src/pi-fit-model.mjs`: inside the sandbox it is the first process, it
|
||||
listens on the sandbox's own loopback (the network stays unshared),
|
||||
writes the agent directory's `models.json` for its port, starts pi and
|
||||
answers one scripted tool call per request. The script reads a project
|
||||
file the gate allows, a file outside the project, the same outside file
|
||||
through a link under each of the four spellings pi 0.85.1 tries for a
|
||||
missing path (a narrow space before AM, decomposed accents, a curly
|
||||
apostrophe, both), and a project file the second extension's handler
|
||||
throws for. The run passes only when the allowed read returns the file,
|
||||
every blocked read is a tool error carrying the gate's own reason and
|
||||
no request carries the outside file's word, the throw stops its read,
|
||||
every request carries the `--system-prompt` file, the script runs to
|
||||
its end, and the gate's `agent_end` hook writes the turn marker with the
|
||||
model's final text answer. A pi that runs a blocked tool, swallows a
|
||||
handler's throw, drops the system prompt or never emits `agent_end` is
|
||||
refused. A run that times out, exits non-zero, or whose model gets no
|
||||
request or sees the run end early, can't be checked and refuses as
|
||||
undecided. A run that leaked and then hung is refused, not undecided. Its verdict is part of the cached pass, so it
|
||||
runs once per digest. It runs beside the other runs, so a cold check
|
||||
takes about as long as before. Line
|
||||
5 (bash) is not checked at start, and nor is a read spelling pi may add
|
||||
later; a stand-in can only check what it scripts.
|
||||
|
||||
Claude Code also runs with `--restricted`: no user, project or local
|
||||
settings, file tools confined to the working directory, and no `CLAUDE.md`
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
// #1873 pi-unpin S6: the scripted model the fit check's model run talks to (pi-fit.mjs). It
|
||||
// answers the Messages API one scripted step at a time: step k is a tool call, given when the
|
||||
// request carries k tool results; after the last step it answers with fixed text. Only node's own
|
||||
// modules are imported, since the file runs inside the fit check's sandbox.
|
||||
//
|
||||
// Run as a program, inside the sandbox: `node pi-fit-model.mjs <config.json> -- <command...>`. It
|
||||
// listens on 127.0.0.1 port 0 in the sandbox's own network namespace (checkArgv unshares the
|
||||
// network, so only that namespace's loopback reaches it), writes the agent directory's models.json
|
||||
// naming that port for the stand-in provider, starts the command with its own environment and
|
||||
// stdio, writes what each request held to the record file after each request, and exits with the
|
||||
// command's status. It is the first process in the sandbox's pid namespace, so when it ends, or is
|
||||
// killed, every process in the sandbox ends with it. No real model and no host network are
|
||||
// reached: the sandbox has no other network.
|
||||
//
|
||||
// packages/harness/tests/helpers.mjs mockAnthropic serves the same answers to session tests.
|
||||
import { createServer } from "node:http";
|
||||
import { spawn } from "node:child_process";
|
||||
import { mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
|
||||
import { dirname } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
export const textOf = (c) => (typeof c === "string" ? c : Array.isArray(c) ? c.map((b) => b?.text ?? "").join("") : "");
|
||||
|
||||
// The tool results in a request's messages, in order: { id, isError, content }.
|
||||
export function toolResults(messages) {
|
||||
const out = [];
|
||||
for (const m of messages ?? []) {
|
||||
if (m?.role !== "user" || !Array.isArray(m.content)) continue;
|
||||
for (const b of m.content) if (b?.type === "tool_result") out.push({ id: b.tool_use_id, isError: b.is_error ?? false, content: textOf(b.content) });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// A request handler for the Messages API. script: [{ name, input }], one tool call per step.
|
||||
// done(results): the text that ends the run once the script is spent. requests: each request is
|
||||
// pushed as { tools, results, system, answer, raw }. onRequest: called after each push. A request
|
||||
// with no tools gets "mock". Streaming and plain answers both work; count_tokens answers 10.
|
||||
export function messagesHandler({ script = [], done = (r) => `DONE ${JSON.stringify(r)}`, requests = [], onRequest = () => {} } = {}) {
|
||||
return (req, res) => {
|
||||
let raw = "";
|
||||
req.on("data", (c) => (raw += c));
|
||||
req.on("end", () => {
|
||||
if (req.method !== "POST" || !/\/messages(\?|$)/.test(req.url)) {
|
||||
res.writeHead(req.url.includes("count_tokens") ? 200 : 404, { "content-type": "application/json" });
|
||||
return res.end(JSON.stringify(req.url.includes("count_tokens") ? { input_tokens: 10 } : { type: "error", error: { type: "not_found_error", message: "mock" } }));
|
||||
}
|
||||
let body;
|
||||
try { body = JSON.parse(raw || "{}"); } catch { body = {}; }
|
||||
const tools = Array.isArray(body.tools) ? body.tools.map((x) => x.name) : [];
|
||||
const results = toolResults(body.messages);
|
||||
const step = tools.length ? script[results.length] : null;
|
||||
const r = step ? { kind: "tool", id: `toolu_${results.length + 1}`, name: step.name, input: step.input } : { kind: "text", text: tools.length ? done(results) : "mock" };
|
||||
requests.push({ tools, results, system: body.system, answer: r, raw });
|
||||
onRequest();
|
||||
const content = r.kind === "tool" ? { type: "tool_use", id: r.id, name: r.name, input: {} } : { type: "text", text: "" };
|
||||
const delta = r.kind === "tool" ? { type: "input_json_delta", partial_json: JSON.stringify(r.input) } : { type: "text_delta", text: r.text };
|
||||
const stop = r.kind === "tool" ? "tool_use" : "end_turn";
|
||||
if (!body.stream) {
|
||||
res.writeHead(200, { "content-type": "application/json" });
|
||||
const c = r.kind === "tool" ? { type: "tool_use", id: r.id, name: r.name, input: r.input } : { type: "text", text: r.text };
|
||||
return res.end(JSON.stringify({ id: "msg_mock", type: "message", role: "assistant", model: body.model, content: [c], stop_reason: stop, stop_sequence: null, usage: { input_tokens: 10, output_tokens: 5 } }));
|
||||
}
|
||||
const send = (event, data) => res.write(`event: ${event}\ndata: ${JSON.stringify({ type: event, ...data })}\n\n`);
|
||||
res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" });
|
||||
send("message_start", { message: { id: "msg_mock", type: "message", role: "assistant", model: body.model, content: [], stop_reason: null, stop_sequence: null, usage: { input_tokens: 10, output_tokens: 1, cache_creation_input_tokens: 0, cache_read_input_tokens: 0 } } });
|
||||
send("content_block_start", { index: 0, content_block: content });
|
||||
send("content_block_delta", { index: 0, delta });
|
||||
send("content_block_stop", { index: 0 });
|
||||
send("message_delta", { delta: { stop_reason: stop, stop_sequence: null }, usage: { output_tokens: 5 } });
|
||||
send("message_stop", {});
|
||||
res.end();
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
// How much of each tool result the record keeps.
|
||||
const KEEP = 2000;
|
||||
|
||||
// config: { record, models, provider, model, key, script, done, secret }. record: the file each
|
||||
// request is written to, as { requests: [{ tools, system, results: [{ isError, content }],
|
||||
// done, leak }] }: tools is the count of tools offered, done whether the answer was the done text,
|
||||
// leak whether the raw request held the secret word.
|
||||
async function main(argv) {
|
||||
const [config, sep, ...command] = argv;
|
||||
if (!config || sep !== "--" || !command.length) process.exit(2);
|
||||
const c = JSON.parse(readFileSync(config, "utf8"));
|
||||
const requests = [];
|
||||
const record = () => writeFileSync(c.record, JSON.stringify({ requests: requests.map((r) => ({
|
||||
tools: r.tools.length,
|
||||
system: textOf(r.system),
|
||||
results: r.results.map((x) => ({ isError: Boolean(x.isError), content: String(x.content).slice(0, KEEP) })),
|
||||
done: r.answer.kind === "text" && r.answer.text === c.done,
|
||||
leak: r.raw.includes(c.secret),
|
||||
})) }));
|
||||
const server = createServer(messagesHandler({ script: c.script, done: () => c.done, requests, onRequest: record }));
|
||||
await new Promise((resolve, reject) => { server.once("error", reject); server.listen(0, "127.0.0.1", resolve); });
|
||||
mkdirSync(dirname(c.models), { recursive: true });
|
||||
writeFileSync(c.models, JSON.stringify({ providers: { [c.provider]: { baseUrl: `http://127.0.0.1:${server.address().port}`, api: "anthropic-messages", apiKey: c.key, models: [{ id: c.model }] } } }));
|
||||
const child = spawn(command[0], command.slice(1), { stdio: "inherit", env: process.env });
|
||||
const status = await new Promise((resolve) => {
|
||||
child.on("error", () => resolve(1));
|
||||
child.on("exit", (code, signal) => resolve(code ?? (signal ? 1 : 0)));
|
||||
});
|
||||
server.close();
|
||||
process.exit(status);
|
||||
}
|
||||
|
||||
const self = fileURLToPath(import.meta.url);
|
||||
let invoked = null;
|
||||
try { invoked = process.argv[1] ? realpathSync(process.argv[1]) : null; } catch {}
|
||||
if (invoked === self) main(process.argv.slice(2)).catch(() => process.exit(1));
|
||||
@@ -0,0 +1,14 @@
|
||||
// #1873 pi-unpin S6: a second -e extension, for the fit check's model run only (pi-fit.mjs); no
|
||||
// session loads it. pi loads it after the gate's, so for a read of THROW_NAME the gate has allowed
|
||||
// the read before this handler throws. The run then shows that a throw in a tool_call handler
|
||||
// stops the tool (S0 line 2: a gate that crashes blocks). It imports nothing, since it runs inside
|
||||
// the fit check's sandbox.
|
||||
export const THROW_NAME = "throw.txt";
|
||||
export const THROW_TEXT = "fit check: the tool_call handler threw";
|
||||
|
||||
export default function fitThrow(host) {
|
||||
host.on("tool_call", async (event) => {
|
||||
if (event?.toolName === "read" && event.input?.path === THROW_NAME) throw new Error(THROW_TEXT);
|
||||
return undefined;
|
||||
});
|
||||
}
|
||||
+333
-68
@@ -2,7 +2,8 @@
|
||||
// here compares a version. The check runs the binary only inside a sandbox with the network
|
||||
// unshared, the found package mounted as a session mounts it, and a bus session's environment
|
||||
// (fitEnv below), so its agent directory is $HOME/.pi/agent under a scratch HOME, as a session's
|
||||
// is. No model is called and no provider key is passed. It asks:
|
||||
// is. No real model is called and no provider key is passed: the one run that reaches a model
|
||||
// reaches a scripted stand-in on the sandbox's own loopback. It asks:
|
||||
//
|
||||
// --version a label for the refusal line and the record; not a gate
|
||||
// --help lists, in its option column, every flag Mosaic passes, with rpc mode and each
|
||||
@@ -39,10 +40,40 @@
|
||||
// planted source. A test drives the adapter through every branch set, tool list
|
||||
// length and thinking level, and fails on an argv sessionShape doesn't build or a
|
||||
// pair no run passes.
|
||||
// sealed -p the seal flags in -p with --no-session, as a worker run passes them. Its -e
|
||||
// extension must reach session_start, and no planted source may show.
|
||||
// worker -p the arguments adapters/pi/adapter.sh builds in -p with MOSAIC_SKILLS, one -e
|
||||
// file, no tools and no session, in its order (workerShape below): the -e,
|
||||
// --no-skills then two --skill directories, --no-tools, --no-session, the fixed
|
||||
// --provider and --model, --system-prompt, -p and a message. Its -e extension must
|
||||
// reach session_start and list both given skills, and no planted source may show.
|
||||
// pi 0.85.1 adds a --skill directory to discovery, so a pi that let --skill turn
|
||||
// discovery back on would show the planted skills here, and one that drops a --skill
|
||||
// directory once --no-skills is on would not list it. No caller makes this exact
|
||||
// run today: task launches refuse skill directories (packages/processing-control
|
||||
// containers.mjs), and only the interactive container launch (scripts/agent.sh)
|
||||
// sets MOSAIC_SKILLS, in the TUI with --tools and --session-dir, which no fit run
|
||||
// can drive. This is the nearest headless form of the adapter's --skill argv. It
|
||||
// replaces the sealed -p run the check made before (the seal flags in -p with
|
||||
// --no-session and no --skill), so it adds no run; that plain shape is not run on
|
||||
// its own any more.
|
||||
// missing -e a missing -e file, in rpc and in -p, stops the start: exit non-zero, no answer,
|
||||
// and no session_start for the -e extension given after it
|
||||
// model #1873 S6: one -p run in a bus session's shape (sessionShape, with --tools read and
|
||||
// the session's own -e extension, pi-extension.mjs, which gates every tool call with
|
||||
// gate.mjs and writes the turn marker at agent_end) against a scripted model:
|
||||
// pi-fit-model.mjs, started inside the sandbox ahead of the binary, listening on the
|
||||
// sandbox's loopback and named by the agent directory's models.json under a stand-in
|
||||
// provider and model (session-env.mjs FIT_MODEL_ROUTE). The script reads a file in
|
||||
// the project (the gate allows it, and its word must come back), then a file outside
|
||||
// it, then four names that pi 0.85.1 resolves to another spelling (a narrow space
|
||||
// before AM, decomposed accents, a curly apostrophe, both of the last two), each a
|
||||
// link out of the project to the same outside file. Each of those four the gate
|
||||
// blocks, and each must come back as an error holding the gate's reason, with the
|
||||
// outside file's word in no request. Every request with tools must carry the
|
||||
// --system-prompt file's word, the run must reach the script's end and exit 0, and
|
||||
// the turn marker must be written. A tool result that misses the gate's reason, a
|
||||
// request without the word, or no marker is a refusal. A run that times out, ends
|
||||
// early or sends no request can't tell. pi-fit-model.mjs is the sandbox's first
|
||||
// process, so the model and every child end with it.
|
||||
//
|
||||
// Every run gets a fresh scratch directory with the sources planted again, so no run sees what
|
||||
// another one left. The directory's name carries the pid, start time and pid namespace of the
|
||||
@@ -76,24 +107,31 @@
|
||||
// repository's pi), never a host path.
|
||||
//
|
||||
// Known limit: pi 0.85.1 appends the agent directory's APPEND_SYSTEM.md whatever the flags. The
|
||||
// check doesn't plant one; a session's agent directory is the run's own. Not run: --fork,
|
||||
// --skill with --no-skills dropped, and the interactive TUI, which only worker containers and
|
||||
// scripts/agent.sh pass; the session branches three or four at a time beyond the session-shaped
|
||||
// check doesn't plant one; a session's agent directory is the run's own. Not run: --fork and
|
||||
// the interactive TUI, which only worker containers and scripts/agent.sh pass; -p with
|
||||
// --no-session and no --skill (a task launch's shape; every bus session passes --session-dir);
|
||||
// --skill beside --tools, --session-dir, -c, --thinking or --mode json, or in rpc (the TUI
|
||||
// launch passes the first three; no bus session passes --skill); --skill with no -e (every fit
|
||||
// run needs its -e extension to see what loaded); one or three or more --skill directories; the
|
||||
// session branches three or four at a time beyond the session-shaped
|
||||
// runs; -c over a session directory that holds nothing for the project (the adapter passes -c
|
||||
// whenever the directory isn't empty), since one more run costs about 340 ms cold; and a
|
||||
// session's own values: its tool names beyond read and bash, thinking levels beyond low,
|
||||
// provider, model, key, -e files and request. The runs are offline with no key, so nothing
|
||||
// past the start is tried. A pi built to act differently with those is outside what a start-time
|
||||
// check can catch.
|
||||
// past the start is tried, but for the model run. A pi built to act differently with those, or to
|
||||
// act differently in a fit check, is outside what a start-time check can catch. The model run
|
||||
// tries read alone, one tool call per turn, and only the four spellings pi 0.85.1 knows.
|
||||
import { spawn } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, readlinkSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { 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 { BWRAP, SETPRIV, UNSHARE, checkArgv, outerArgv, sessionSetenv } from "../../seat/src/sandbox.mjs";
|
||||
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";
|
||||
import { adapterEnv } from "./runner.mjs";
|
||||
import { FIT_ROUTE, harnessEnv } from "../../plugins/harness/pi/session-env.mjs";
|
||||
import { THROW_NAME } from "./pi-fit-throw.mjs";
|
||||
import { FIT_MODEL_ROUTE, FIT_ROUTE, fitPolicy, harnessEnv } from "../../plugins/harness/pi/session-env.mjs";
|
||||
|
||||
// The flags adapters/pi/adapter.sh and the conversation engine pass.
|
||||
export const FIT_FLAGS = Object.freeze(["--mode", "--session", "--no-session", "--session-dir", "--fork", "-c", "--no-extensions", "-e", "--no-skills", "--skill", "--no-prompt-templates", "--no-themes", "--no-context-files", "--no-approve", "--offline", "--tools", "--no-tools", "--provider", "--model", "--thinking", "--system-prompt", "-p"]);
|
||||
@@ -122,19 +160,68 @@ export const SESSION_RUNS = Object.freeze([
|
||||
// or none, a persistent session directory with -c when it holds a session, a thinking level or
|
||||
// none, the --system-prompt file, -p and the request, and --mode json when the model event monitor
|
||||
// runs pi. No --skill and no --fork: a bus session never passes them. Keep its order:
|
||||
// pi-fit.test.mjs runs the adapter through every branch and fails when the two differ.
|
||||
export function sessionShape({ noMcp, extensions, prompt, sessionDir, tools, cont, thinking, json, message }) {
|
||||
// pi-fit.test.mjs runs the adapter through every branch and fails when the two differ. route: the
|
||||
// provider and model, SESSION_SHAPE's unless the model run passes FIT_MODEL_ROUTE.
|
||||
export function sessionShape({ noMcp, extensions, prompt, sessionDir, tools, cont, thinking, json, message, route = SESSION_SHAPE }) {
|
||||
return [
|
||||
"--offline", "--no-approve", "--no-extensions", ...(noMcp ? ["--no-mcp"] : []), ...extensions.flatMap((e) => ["-e", e]),
|
||||
"--no-skills", "--no-prompt-templates", "--no-themes", "--no-context-files",
|
||||
...(tools ? ["--tools", tools] : ["--no-tools"]), "--session-dir", sessionDir, ...(cont ? ["-c"] : []),
|
||||
"--provider", SESSION_SHAPE.provider, "--model", SESSION_SHAPE.model, ...(thinking ? ["--thinking", thinking] : []),
|
||||
"--provider", route.provider, "--model", route.model, ...(thinking ? ["--thinking", thinking] : []),
|
||||
"--system-prompt", prompt, "-p", message, ...(json ? ["--mode", "json"] : []),
|
||||
];
|
||||
}
|
||||
// pi-skills: the arguments adapters/pi/adapter.sh builds in -p with MOSAIC_SKILLS, no tools and no
|
||||
// session: the given -e files, --no-skills and then a --skill per directory, --no-tools,
|
||||
// --no-session, the --provider and --model, the --system-prompt file, -p and the request. Only
|
||||
// the interactive launch sets MOSAIC_SKILLS today, in the TUI; this is the nearest headless form. Keep its order: pi-fit.test.mjs runs the adapter with MOSAIC_SKILLS and
|
||||
// fails when the two differ.
|
||||
export function workerShape({ noMcp, extensions, skills, prompt, message }) {
|
||||
return [
|
||||
"--offline", "--no-approve", "--no-extensions", ...(noMcp ? ["--no-mcp"] : []), ...extensions.flatMap((e) => ["-e", e]),
|
||||
"--no-skills", ...skills.flatMap((s) => ["--skill", s]), "--no-prompt-templates", "--no-themes", "--no-context-files",
|
||||
"--no-tools", "--no-session", "--provider", SESSION_SHAPE.provider, "--model", SESSION_SHAPE.model,
|
||||
"--system-prompt", prompt, "-p", message,
|
||||
];
|
||||
}
|
||||
// What a session-shaped run is named by in a refusal line.
|
||||
const shapeName = ({ tools, cont, thinking, json }) => `in a session's shape with ${[tools ? "--tools" : "--no-tools", ...(cont ? ["-c"] : []), ...(thinking ? ["--thinking"] : []), ...(json ? ["--mode json"] : [])].join(", ")}`;
|
||||
const RUN_TIMEOUT = 20000;
|
||||
|
||||
// #1873 S6: the model run. Its first -e extension is the gate's, which a session loads; the second
|
||||
// is pi-fit-throw.mjs, whose handler throws for one read the gate allows. The model is
|
||||
// pi-fit-model.mjs. All are mounted read-only where they are, with the runner files the gate's
|
||||
// extension imports, as a session mounts them (sandbox.mjs binds).
|
||||
const EXTENSION = fileURLToPath(new URL("./pi-extension.mjs", import.meta.url));
|
||||
const THROW_EXTENSION = fileURLToPath(new URL("./pi-fit-throw.mjs", import.meta.url));
|
||||
export const MODEL_EXTENSIONS = Object.freeze([EXTENSION, THROW_EXTENSION]);
|
||||
const MODEL_FILE = fileURLToPath(new URL("./pi-fit-model.mjs", import.meta.url));
|
||||
const MODEL_MOUNTS = [MODEL_FILE, THROW_EXTENSION, ...RUNNER_FILES].map((src) => ({ kind: "ro", src }));
|
||||
export const MODEL_SHAPE = Object.freeze({ tools: "read", cont: false, thinking: "", json: false });
|
||||
// The project file's word, which the allowed read must bring back; the outside file's, which must
|
||||
// reach no request; the scripted model's last answer.
|
||||
const INSIDE = "FITINSIDEWORD";
|
||||
const SECRET = "FITSECRETWORD";
|
||||
// The word in the project file the throwing handler's read names, which must reach no request.
|
||||
const THROWN = "FITTHROWNWORD";
|
||||
const DONE = "FITDONE";
|
||||
// The four spellings pi 0.85.1 tries when a read's path is missing: the name the model asks for,
|
||||
// the name on disk (a link to the outside file) and what a refusal line calls it.
|
||||
const SPELLINGS = [
|
||||
["shot 9.41 AM.png", "shot 9.41\u202FAM.png", "a narrow space before AM"],
|
||||
["r\u00E9sum\u00E9.txt", "r\u00E9sum\u00E9.txt".normalize("NFD"), "decomposed accents"],
|
||||
["notes's.txt", "notes\u2019s.txt", "a curly apostrophe"],
|
||||
["d'\u00E9cran.png", "d\u2019\u00E9cran.png".normalize("NFD"), "decomposed accents and a curly apostrophe"],
|
||||
];
|
||||
// The script: the allowed read, the read outside the project, one read per spelling, then the read
|
||||
// the gate allows and the second handler throws for.
|
||||
export const MODEL_SCRIPT = Object.freeze([
|
||||
{ name: "read", input: { path: "inside.txt" } },
|
||||
{ name: "read", input: { path: "../outside/secret.txt" } },
|
||||
...SPELLINGS.map(([asked]) => ({ name: "read", input: { path: asked } })),
|
||||
{ name: "read", input: { path: THROW_NAME } },
|
||||
].map((step) => Object.freeze(step)));
|
||||
const THROW_STEP = MODEL_SCRIPT.length - 1;
|
||||
// --version gives only a label, so a hang costs this much and leaves no label.
|
||||
const VERSION_TIMEOUT = 3000;
|
||||
const OUTPUT_MAX = 4 * 1024 * 1024;
|
||||
@@ -171,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);
|
||||
@@ -238,27 +329,45 @@ const fitKey = (found, { levels, bwrap, unshare, node, host }) => JSON.stringify
|
||||
// the module applies. host: the environment of the process sessions launch from (the bus's own),
|
||||
// which a fit run's environment is built from as a session's is. bwrap and unshare: where those
|
||||
// tools are (tests pass stand-ins). Returns a promise of { ok: true, version, noMcp, digest, outer }
|
||||
// or { ok: false, message }. outer: { used: true } when the runs had unshare's outer layer, else
|
||||
// or { ok: false, message, digest, undecided }. outer: { used: true } when the runs had unshare's outer layer, else
|
||||
// { used: false, why } with why naming unshare and how it failed (sandboxLayers below). A call
|
||||
// while a check with the same key runs gets that check's promise. A check that throws (a scratch directory it can't
|
||||
// make, say) can't tell, so it gives { ok: false, message } too and keeps nothing.
|
||||
export function piFit(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, node = process.execPath, host = process.env } = {}) {
|
||||
// #1873 harness watch W2: a failure also carries digest, the digest of the files the check ran
|
||||
// (null only when they can't be read), and undecided: true when the check could not tell (its
|
||||
// message then says "can't be checked"), false when the binary definitely failed. A reader takes
|
||||
// these fields and never parses the message.
|
||||
// signal (W2): the caller stops waiting when it aborts. A check is stopped (its runs killed, its
|
||||
// scratch removed) only when every caller that shares it passed a signal and every one of those
|
||||
// aborted: preflight passes none, so a check a launch waits on is never stopped by the watch. A
|
||||
// caller whose signal aborts gets its answer once the check has ended, or at once when a caller
|
||||
// without a signal keeps the check going; that answer can't tell (undecided) unless the check
|
||||
// had already finished.
|
||||
export function piFit(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, node = process.execPath, host = process.env, signal = null } = {}) {
|
||||
const who = (version) => `pi${version ? ` ${version}` : ""} (${found.label})`;
|
||||
let key;
|
||||
try {
|
||||
key = fitKey(found, { levels, bwrap, unshare, node, host });
|
||||
} catch {
|
||||
return Promise.resolve({ ok: false, message: `${who(null)} can't be checked: its files can't be read` });
|
||||
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 running.get(key);
|
||||
if (running.has(key)) return share(running.get(key), signal, who, key);
|
||||
const cancel = new AbortController();
|
||||
const job = (async () => {
|
||||
try {
|
||||
sweepScratch();
|
||||
// The sandbox's own error text can name a host path, so the line keeps only what failed.
|
||||
const layers = await sandboxLayers({ bwrap, unshare, node });
|
||||
if (layers.problem) return { ok: false, message: `${who(null)} can't be checked: ${layers.problem}` };
|
||||
const result = await check(found, { levels, bwrap, unshare: layers.outer, node, who, digest: JSON.parse(key)[0], host });
|
||||
if (layers.problem) return { ok: false, message: `${who(null)} can't be checked: ${layers.problem}`, digest: JSON.parse(key)[0], undecided: true };
|
||||
if (cancel.signal.aborted) return { ok: false, message: `${who(null)} can't be checked: the check was stopped`, digest: JSON.parse(key)[0], undecided: true };
|
||||
const result = await check(found, { levels, bwrap, unshare: layers.outer, node, who, digest: JSON.parse(key)[0], host, cancel: cancel.signal });
|
||||
if (!result.ok) return result;
|
||||
const passing = { ...result, outer: layers.outer ? { used: true } : { used: false, why: layers.why } };
|
||||
passed.set(key, passing);
|
||||
@@ -266,11 +375,40 @@ export function piFit(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, no
|
||||
} catch {
|
||||
// #1873 re-check 8: refused as a fit that failed. The error's text can name a host path, so
|
||||
// the line keeps none of it.
|
||||
return { ok: false, message: `${who(null)} can't be checked: the check could not run` };
|
||||
return { ok: false, message: `${who(null)} can't be checked: the check could not run`, digest: JSON.parse(key)[0], undecided: true };
|
||||
}
|
||||
})().finally(() => { if (running.get(key) === job) running.delete(key); });
|
||||
running.set(key, job);
|
||||
return job;
|
||||
})().finally(() => { if (running.get(key)?.job === job) running.delete(key); });
|
||||
const entry = { job, cancel, holds: 0, firm: false };
|
||||
running.set(key, entry);
|
||||
return share(entry, signal, who, key);
|
||||
}
|
||||
|
||||
// One caller's view of a running check: see signal above. A stopped check leaves running at once,
|
||||
// so a caller that comes after (a launch's preflight) starts its own check and never gets the
|
||||
// stopped one's answer.
|
||||
function share(entry, signal, who, key) {
|
||||
if (!signal) {
|
||||
entry.firm = true;
|
||||
return entry.job;
|
||||
}
|
||||
entry.holds++;
|
||||
return new Promise((resolve) => {
|
||||
let left = false;
|
||||
const leave = () => {
|
||||
if (left) return;
|
||||
left = true;
|
||||
entry.holds--;
|
||||
if (!entry.firm && entry.holds === 0) {
|
||||
entry.cancel.abort();
|
||||
if (running.get(key) === entry) running.delete(key);
|
||||
}
|
||||
if (entry.firm) resolve({ ok: false, message: `${who(null)} can't be checked: the watch stopped waiting`, digest: null, undecided: true });
|
||||
else entry.job.then(resolve);
|
||||
};
|
||||
signal.addEventListener("abort", leave, { once: true });
|
||||
entry.job.then((answer) => { signal.removeEventListener("abort", leave); if (!left) { left = true; entry.holds--; resolve(answer); } });
|
||||
if (signal.aborted) leave();
|
||||
});
|
||||
}
|
||||
|
||||
// The passed check for found as piFit would key it now, or null: none passed, or its files can't
|
||||
@@ -283,11 +421,14 @@ export function fitVerdict(found, { levels = [], bwrap = BWRAP, unshare = UNSHAR
|
||||
}
|
||||
}
|
||||
|
||||
async function check(found, { levels, bwrap, unshare, node, who, digest, host }) {
|
||||
async function check(found, { levels, bwrap, unshare, node, who, digest, host, cancel = null }) {
|
||||
// At most FIT_CONCURRENCY runs at once, started in the order they are asked for. stop kills the
|
||||
// runs still going and keeps queued ones from starting; the check waits for every run it began,
|
||||
// so no scratch directory or child outlives it.
|
||||
const stop = new AbortController();
|
||||
const cancelled = () => stop.abort();
|
||||
cancel?.addEventListener("abort", cancelled, { once: true });
|
||||
if (cancel?.aborted) stop.abort();
|
||||
const begun = [];
|
||||
let active = 0;
|
||||
const waiting = [];
|
||||
@@ -300,20 +441,28 @@ async function check(found, { levels, bwrap, unshare, node, who, digest, host })
|
||||
// get_commands answer or through the -e extension's command list (listed: through that alone).
|
||||
// shape: a session-shaped run's branches, which set its tools, thinking level and monitor
|
||||
// variables as a session's would. A run that stop kept from starting gives null.
|
||||
const once = async (args, input, { timeout, noMcp, shape }) => {
|
||||
// model (S6): the scripted-model run, with its own plant, mounts and environment; it gives the
|
||||
// run's status and stopped with the requests the model recorded and the turn marker.
|
||||
const once = async (args, input, { timeout, noMcp, shape, model }) => {
|
||||
await slot();
|
||||
try {
|
||||
if (stop.signal.aborted) return null;
|
||||
const scratch = mkdtempSync(join(tmpdir(), scratchPrefix()));
|
||||
const scratch = mkdtempSync(join(scratchBase(), `${scratchPrefix()}${model ? "model-" : ""}`));
|
||||
let ran = null;
|
||||
try {
|
||||
if (model) {
|
||||
plantModel(scratch);
|
||||
const env = fitEnv(scratch, { view: found.view, noMcp, shape: MODEL_SHAPE, host, extensions: MODEL_EXTENSIONS, route: FIT_MODEL_ROUTE });
|
||||
const r = ran = await runIn(found, { bwrap, unshare, node, scratch, env, args: args(scratch), input, timeout, signal: stop.signal, mounts: MODEL_MOUNTS, prefix: [node, MODEL_FILE, join(scratch, "model.json"), "--"] });
|
||||
return { status: r.status, stopped: r.stopped, requests: modelRecord(scratch), turn: turnMarker(scratch) };
|
||||
}
|
||||
plant(scratch);
|
||||
const env = fitEnv(scratch, { view: found.view, noMcp, shape, host });
|
||||
const r = ran = await runIn(found, { bwrap, unshare, node, scratch, env, args: typeof args === "function" ? args(scratch) : args, input, timeout, signal: stop.signal });
|
||||
const answer = answered(r.stdout);
|
||||
const start = started(scratch);
|
||||
const listed = start?.commands ?? [];
|
||||
return { ...r, answer, explicit: marked(scratch, EXPLICIT), second: marked(scratch, SECOND), prompt: start?.prompt ?? null, session: start?.session ?? null, listed: named(listed), seen: seen(scratch, [...(answer.commands ?? []), ...listed], start) };
|
||||
return { ...r, answer, explicit: marked(scratch, EXPLICIT), second: marked(scratch, SECOND), prompt: start?.prompt ?? null, session: start?.session ?? null, listed: named(listed), skills: GIVEN_SKILLS.every((n) => names(listed, n)), seen: seen(scratch, [...(answer.commands ?? []), ...listed], start) };
|
||||
} finally {
|
||||
// #1873 re-check 10: a run this check killed (a timeout, or stop) may have had bwrap in its
|
||||
// setup, and without unshare's outer layer bwrap's child then waits for good. The run is
|
||||
@@ -325,8 +474,8 @@ async function check(found, { levels, bwrap, unshare, node, who, digest, host })
|
||||
free();
|
||||
}
|
||||
};
|
||||
const run = (args, input = "", { timeout = RUN_TIMEOUT, noMcp = false, shape = null } = {}) => {
|
||||
const p = once(args, input, { timeout, noMcp, shape });
|
||||
const run = (args, input = "", { timeout = RUN_TIMEOUT, noMcp = false, shape = null, model = false } = {}) => {
|
||||
const p = once(args, input, { timeout, noMcp, shape, model });
|
||||
// Judged in order, a later run can fail before an earlier one is awaited: this keeps that
|
||||
// failure from counting as unhandled. Awaiting p still throws it.
|
||||
p.catch(() => {});
|
||||
@@ -336,6 +485,7 @@ async function check(found, { levels, bwrap, unshare, node, who, digest, host })
|
||||
try {
|
||||
return await judge(found, { levels, who, digest, run });
|
||||
} finally {
|
||||
cancel?.removeEventListener("abort", cancelled);
|
||||
stop.abort();
|
||||
await Promise.allSettled(begun);
|
||||
}
|
||||
@@ -347,8 +497,10 @@ async function judge(found, { levels, who, digest, run }) {
|
||||
const help = await helped;
|
||||
const v = await versioned;
|
||||
const version = v.status === 0 ? versionLabel(v.stdout) : null;
|
||||
const refuse = (why) => ({ ok: false, message: `${who(version)} ${why}` });
|
||||
if (help.status !== 0) return refuse("can't be checked: --help failed");
|
||||
// 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 (${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)) ?? "";
|
||||
@@ -357,6 +509,9 @@ async function judge(found, { levels, who, digest, run }) {
|
||||
if (has("--thinking")) for (const l of levels) if (!word(line("--thinking"), l)) missing.push(`--thinking ${l}`);
|
||||
if (missing.length) return refuse(`lacks ${missing.join(", ")}`);
|
||||
const noMcp = has("--no-mcp");
|
||||
// S6: the scripted-model run is asked for first, as the longest, so it overlaps the others. It is
|
||||
// judged last.
|
||||
const modelRun = run((s) => sessionShape({ noMcp, extensions: MODEL_EXTENSIONS, prompt: join(s, "prompt.md"), sessionDir: join(s, "sessions"), message: "fit check", ...MODEL_SHAPE, route: FIT_MODEL_ROUTE }), "", { noMcp, model: true });
|
||||
|
||||
const ask = '{"id":"fit-state","type":"get_state"}\n{"id":"fit-commands","type":"get_commands"}\n';
|
||||
// Every run passes the same -e and --system-prompt, so the runs differ only by the seal flags.
|
||||
@@ -379,36 +534,37 @@ async function judge(found, { levels, who, digest, run }) {
|
||||
if (shape.cont) plantSession(s);
|
||||
return sessionShape({ noMcp, extensions: [join(s, "explicit", "fit.ts"), join(s, "explicit", "second.ts")], prompt: join(s, "prompt.md"), sessionDir: join(s, "sessions"), message: "fit check", ...shape });
|
||||
}, "", { noMcp, shape })]);
|
||||
// -p takes a message; offline with no key, pi stops before any model call.
|
||||
const pRun = run((s) => ["-p", ...seal, ...given(s), "fit check"], "", { noMcp });
|
||||
// -p takes a message; offline with no key, pi stops before any model call. The adapter's -p argv
|
||||
// with MOSAIC_SKILLS: two --skill directories right after --no-skills, as the adapter puts them.
|
||||
const pRun = run((s) => workerShape({ noMcp, extensions: [join(s, "explicit", "fit.ts")], skills: GIVEN_SKILLS.map((n) => join(s, "given", n)), prompt: join(s, "prompt.md"), message: "fit check" }), "", { noMcp });
|
||||
// A missing -e file stops the start. pi 0.85.1 loads the -e files in order and still loads the
|
||||
// one after the missing one, then exits 1 before session_start, so only session_start tells.
|
||||
const missingRuns = [["rpc", ["--mode", "rpc"], ask], ["-p", ["-p"], ""]].map(([mode, args, input]) =>
|
||||
[mode, run((s) => [...args, ...seal, "-e", join(s, "missing", "extension.ts"), ...given(s), ...(mode === "-p" ? ["fit check"] : [])], input, { noMcp })]);
|
||||
|
||||
const control = await controlRun;
|
||||
if (control.stopped) return refuse(`can't be checked: the start without the seal flags ${control.stopped}`);
|
||||
if (!control.answer.state) return refuse("can't be checked: it didn't answer get_state");
|
||||
if (!control.answer.commands) return refuse("can't be checked: it didn't answer get_commands");
|
||||
if (!control.explicit || control.prompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt without the seal flags");
|
||||
if (control.stopped) return unsure(`the start without the seal flags ${control.stopped}`);
|
||||
if (!control.answer.state) return unsure("it didn't answer get_state");
|
||||
if (!control.answer.commands) return unsure("it didn't answer get_commands");
|
||||
if (!control.explicit || control.prompt === null) return unsure("its -e extension didn't see the system prompt without the seal flags");
|
||||
// A kind counts only through a source outside the project, except the trusted project files
|
||||
// themselves: a project source can wait on project trust, and then --no-approve alone would keep
|
||||
// it out of the sealed runs and hide a seal flag of its kind that the binary ignores.
|
||||
const shown = new Set(control.seen.filter((s) => s.kind === "trusted project file" || !s.source.startsWith("project ")).map((s) => s.kind));
|
||||
const unshown = KINDS.filter((k) => (k !== "MCP server" || noMcp) && !shown.has(k));
|
||||
if (unshown.length) return refuse(`can't be checked: it didn't load a planted ${unshown.join(", ")} without the seal flags`);
|
||||
if (unshown.length) return unsure(`it didn't load a planted ${unshown.join(", ")} without the seal flags`);
|
||||
// -p answers no get_commands, so the -p runs see skills and prompt templates only through the -e
|
||||
// extension's command list, which must show them here.
|
||||
const listed = new Set(control.listed.filter((s) => !s.source.startsWith("project ")).map((s) => s.kind));
|
||||
const unlisted = ["skill", "prompt template"].filter((k) => !listed.has(k));
|
||||
if (unlisted.length) return refuse(`can't be checked: its -e extension didn't list a planted ${unlisted.join(", ")} without the seal flags`);
|
||||
if (unlisted.length) return unsure(`its -e extension didn't list a planted ${unlisted.join(", ")} without the seal flags`);
|
||||
|
||||
const rpc = await rpcSealed;
|
||||
if (rpc.stopped) return refuse(`can't be checked: the start with the seal flags ${rpc.stopped}`);
|
||||
if (!rpc.answer.state) return refuse("can't be checked: it didn't answer get_state with the seal flags on");
|
||||
if (!rpc.answer.commands) return refuse("can't be checked: it didn't answer get_commands with the seal flags on");
|
||||
if (rpc.stopped) return unsure(`the start with the seal flags ${rpc.stopped}`);
|
||||
if (!rpc.answer.state) return unsure("it didn't answer get_state with the seal flags on");
|
||||
if (!rpc.answer.commands) return unsure("it didn't answer get_commands with the seal flags on");
|
||||
if (!rpc.explicit) return refuse("didn't load an -e extension with the seal flags on");
|
||||
if (rpc.prompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt with the seal flags on");
|
||||
if (rpc.prompt === null) return unsure("its -e extension didn't see the system prompt with the seal flags on");
|
||||
if (!rpc.prompt.includes(GIVEN)) return refuse("didn't use the --system-prompt file with the seal flags on");
|
||||
if (rpc.seen.length) return refuse(`loaded a planted ${rpc.seen.map((s) => s.source).join(", ")} with the seal flags on`);
|
||||
|
||||
@@ -416,10 +572,10 @@ async function judge(found, { levels, who, digest, run }) {
|
||||
for (const [flag, kind, pending] of oneOut) {
|
||||
const r = await pending;
|
||||
const but = `with every seal flag but ${flag}`;
|
||||
if (r.stopped) return refuse(`can't be checked: the start ${but} ${r.stopped}`);
|
||||
if (!r.answer.state || !r.answer.commands) return refuse(`can't be checked: it didn't answer get_state and get_commands ${but}`);
|
||||
if (r.prompt === null) return refuse(`can't be checked: its -e extension didn't see the system prompt ${but}`);
|
||||
if (!r.seen.some((s) => s.kind === kind)) return refuse(`can't be checked: it didn't load a planted ${kind} ${but}`);
|
||||
if (r.stopped) return unsure(`the start ${but} ${r.stopped}`);
|
||||
if (!r.answer.state || !r.answer.commands) return unsure(`it didn't answer get_state and get_commands ${but}`);
|
||||
if (r.prompt === null) return unsure(`its -e extension didn't see the system prompt ${but}`);
|
||||
if (!r.seen.some((s) => s.kind === kind)) return unsure(`it didn't load a planted ${kind} ${but}`);
|
||||
const others = r.seen.filter((s) => s.kind !== kind && !(flag === "--no-extensions" && !noMcp && s.kind === "MCP server"));
|
||||
if (others.length) return refuse(`loaded a planted ${others.map((s) => s.source).join(", ")} ${but}`);
|
||||
}
|
||||
@@ -427,27 +583,68 @@ async function judge(found, { levels, who, digest, run }) {
|
||||
for (const [shape, pending] of shaped) {
|
||||
const r = await pending;
|
||||
const name = shapeName(shape);
|
||||
if (r.stopped) return refuse(`can't be checked: the start ${name} ${r.stopped}`);
|
||||
if (r.prompt === null) return refuse(`can't be checked: its -e extension didn't see the system prompt ${name}`);
|
||||
if (r.stopped) return unsure(`the start ${name} ${r.stopped}`);
|
||||
if (r.prompt === null) return unsure(`its -e extension didn't see the system prompt ${name}`);
|
||||
if (!r.explicit || !r.second) return refuse(`didn't load every -e extension ${name}`);
|
||||
if (!r.prompt.includes(GIVEN)) return refuse(`didn't use the --system-prompt file ${name}`);
|
||||
if (shape.cont && r.session !== PLANTED_SESSION) return refuse(`can't be checked: it didn't resume the planted session with -c ${name}`);
|
||||
if (shape.cont && r.session !== PLANTED_SESSION) return unsure(`it didn't resume the planted session with -c ${name}`);
|
||||
if (r.seen.length) return refuse(`loaded a planted ${r.seen.map((x) => x.source).join(", ")} with the seal flags ${name}`);
|
||||
}
|
||||
|
||||
const p = await pRun;
|
||||
if (p.stopped) return refuse(`can't be checked: a -p start with the seal flags ${p.stopped}`);
|
||||
if (p.prompt === null) return refuse("can't be checked: a -p start with the seal flags didn't run its -e extension");
|
||||
if (p.seen.length) return refuse(`loaded a planted ${p.seen.map((x) => x.source).join(", ")} with the seal flags on in -p`);
|
||||
if (p.stopped) return unsure(`the start in a worker's shape ${p.stopped}`);
|
||||
if (p.prompt === null) return unsure("its -e extension didn't see the system prompt in a worker's shape");
|
||||
if (p.seen.length) return refuse(`loaded a planted ${p.seen.map((x) => x.source).join(", ")} in a worker's shape with --skill`);
|
||||
if (!p.skills) return refuse("didn't load every --skill directory with --no-skills on in a worker's shape");
|
||||
|
||||
for (const [mode, pending] of missingRuns) {
|
||||
const r = await pending;
|
||||
if (r.stopped) return refuse(`can't be checked: a ${mode} start with a missing -e extension ${r.stopped}`);
|
||||
if (r.stopped) return unsure(`a ${mode} start with a missing -e extension ${r.stopped}`);
|
||||
if (r.status === 0 || r.answer.state || r.prompt !== null) return refuse("starts with a missing -e extension");
|
||||
}
|
||||
|
||||
const model = judgeModel(await modelRun);
|
||||
if (model) return model.undecided ? unsure(model.why) : refuse(model.why);
|
||||
return { ok: true, version, noMcp, digest };
|
||||
}
|
||||
|
||||
// S6: the model run's verdict: null for a pass, else { why, undecided }. The step results are the
|
||||
// longest list any request carried. A definite failure in what did run counts first, so a run that
|
||||
// leaked the outside file and then hung is refused, not left undecided.
|
||||
function judgeModel(m) {
|
||||
const no = (why) => ({ why, undecided: false });
|
||||
const cannot = (why) => ({ why, undecided: true });
|
||||
// A run that stop kept from starting.
|
||||
if (!m) return cannot("the scripted-model run was stopped");
|
||||
const requests = m.requests;
|
||||
const results = requests.reduce((a, r) => (r.results.length > a.length ? r.results : a), []);
|
||||
const blocked = [[1, "", "outside the workspace"], ...SPELLINGS.map(([, , label], i) => [i + 2, ` for a read under another spelling (${label})`, "under another spelling"])];
|
||||
for (const [step, what] of blocked) {
|
||||
const r = results[step];
|
||||
if (r && (!r.isError || r.content.includes(SECRET))) return no(`the gate's block didn't stop the tool${what}`);
|
||||
}
|
||||
if (requests.some((r) => r.leak)) return no("the gate's block didn't stop the tool");
|
||||
const thrown = results[THROW_STEP];
|
||||
if (thrown && (!thrown.isError || thrown.content.includes(THROWN))) return no("a throw in a tool_call handler didn't stop the tool");
|
||||
for (const [step, what, reason] of blocked) {
|
||||
const r = results[step];
|
||||
if (r && !(r.content.includes("mosaic gate: ") && r.content.includes(reason))) return no(`the gate's tool_call hook didn't decide the tool${what || " for a read outside the workspace"}`);
|
||||
}
|
||||
if (requests.some((r) => r.tools > 0 && !r.system.includes(GIVEN))) return no("didn't send the --system-prompt file to the model");
|
||||
if (results[0] && (results[0].isError || !results[0].content.includes(INSIDE))) return no("didn't run the tool the gate allowed");
|
||||
if (m.stopped) return cannot(`the scripted-model run ${m.stopped}`);
|
||||
// A pi that exits non-zero before its first request most often failed to start (an extension it
|
||||
// couldn't load, say). The line says so without its stderr, which can name host paths.
|
||||
if (!requests.length) return cannot(m.status !== 0 ? "the scripted-model run exited non-zero before any model request" : "the scripted model got no request");
|
||||
if (results.length < MODEL_SCRIPT.length || !requests.some((r) => r.done)) return cannot("the scripted-model run ended before the script's last step");
|
||||
if (m.status !== 0) return cannot("the scripted-model run exited non-zero");
|
||||
if (!m.turn) return no("didn't end its run with agent_end");
|
||||
// The scripted model ends with text, so agent_end must carry that final assistant message: the
|
||||
// gate's extension records its stopReason and whether it held text.
|
||||
if (typeof m.turn.stopReason !== "string" || m.turn.hasText !== true) return no("its agent_end didn't carry the model's final answer");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Whether bwrap runs here, as sandbox.mjs's sandboxProblem asks, but as child processes the caller
|
||||
// awaits: the system part, the network unshared, an empty environment, then /usr/bin/true. And,
|
||||
// once per check, whether unshare's outer layer runs (checkArgv in sandbox.mjs): first on its own,
|
||||
@@ -460,7 +657,7 @@ async function judge(found, { levels, who, digest, run }) {
|
||||
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) => {
|
||||
@@ -489,17 +686,20 @@ async function sandboxLayers({ bwrap, unshare, node }) {
|
||||
// fitEnv's. The binary starts through /bin/sh, as the adapter starts it, so it also gets the
|
||||
// variables the shell sets. The run is a child process the caller awaits; nothing here blocks.
|
||||
// stopped: why the run was killed ("timed out", "printed more than 4 MB", "was stopped"), or null.
|
||||
function runIn(found, { bwrap, unshare, node, scratch, env, args, input, timeout, signal }) {
|
||||
// mounts and prefix (S6): more read-only mounts, and a command the binary's start is passed to
|
||||
// (the scripted model).
|
||||
function runIn(found, { bwrap, unshare, node, scratch, env, args, input, timeout, signal, mounts = [], prefix = [] }) {
|
||||
const list = [
|
||||
{ kind: "ro", src: found.root, ...(found.dest ? { dest: found.dest } : {}) },
|
||||
{ kind: "rw", src: scratch },
|
||||
{ kind: "rw", src: join(scratch, "tmp"), dest: "/tmp" },
|
||||
...mounts,
|
||||
];
|
||||
return spawned(checkArgv(list, ["/bin/sh", "-c", 'exec "$0" "$@"', found.view, ...args], { env, chdir: join(scratch, "project"), node, bwrap, unshare }), { input, timeout, signal });
|
||||
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.
|
||||
@@ -522,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) => {
|
||||
@@ -555,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
|
||||
@@ -566,12 +780,13 @@ function spawned(argv, { input, timeout, signal }) {
|
||||
// tool list, thinking level and --mode json, which a proxy session's first turn takes with the
|
||||
// model event monitor's variables), else none: no tools, no thinking, one -e. noMcp: --help lists
|
||||
// --no-mcp. pi-fit.test.mjs fails when these names, or the values the code sets, differ from a bus
|
||||
// session's, for a login shell's host and a unit's.
|
||||
export function fitEnv(scratch, { view, noMcp = false, shape = null, host = process.env }) {
|
||||
// session's, for a login shell's host and a unit's. extensions and route (S6): the model run's -e
|
||||
// files and its provider and model, in place of the planted ones and SESSION_SHAPE's.
|
||||
export function fitEnv(scratch, { view, noMcp = false, shape = null, host = process.env, extensions = null, route = SESSION_SHAPE }) {
|
||||
const runner = { ...sessionEnv(withoutProviders(host), "fit"), ...sessionSetenv(scratch, { business: "fit", role: "fit" }) };
|
||||
const { tools = "", thinking = "", json = false } = shape ?? {};
|
||||
const session = { provider: SESSION_SHAPE.provider, model: SESSION_SHAPE.model, thinking };
|
||||
const extensions = [join(scratch, "explicit", "fit.ts"), ...(shape ? [join(scratch, "explicit", "second.ts")] : [])];
|
||||
const session = { provider: route.provider, model: route.model, thinking };
|
||||
extensions ??= [join(scratch, "explicit", "fit.ts"), ...(shape ? [join(scratch, "explicit", "second.ts")] : [])];
|
||||
const env = adapterEnv({
|
||||
...session,
|
||||
bundle: { prompt: join(scratch, "prompt.md"), policy: join(scratch, "policy.json"), tools: join(scratch, "tools.json") },
|
||||
@@ -657,6 +872,9 @@ const GIVEN = "FITGIVENPROMPT";
|
||||
// pass after it.
|
||||
const EXPLICIT = "explicit extension";
|
||||
const SECOND = "second explicit extension";
|
||||
// The skills the worker-shaped run passes with --skill (scratch/given/<name>), outside every place
|
||||
// pi looks for skills, so they show only through --skill. Not planted ambient: not in NAMED.
|
||||
const GIVEN_SKILLS = ["fit-given-skill", "fit-second-given-skill"];
|
||||
// The id of the session a -c run must resume.
|
||||
const PLANTED_SESSION = "fit-planted-session";
|
||||
const markerName = (source) => source.replace(/[^a-z]/g, "-");
|
||||
@@ -669,8 +887,10 @@ function started(scratch) {
|
||||
if (!m || typeof m.prompt !== "string" || !Array.isArray(m.commands)) return null;
|
||||
return { prompt: m.prompt, commands: m.commands.filter((c) => typeof c === "string"), session: typeof m.session === "string" ? m.session : null };
|
||||
}
|
||||
// Whether command names hold name, bare or after a prefix such as "skill:".
|
||||
const names = (commands, name) => commands.some((c) => c === name || c.endsWith(`:${name}`));
|
||||
// The planted skills and prompt templates among command names, as { source, kind }.
|
||||
const named = (names) => Object.entries(NAMED).filter(([name]) => names.some((c) => c === name || c.endsWith(`:${name}`))).map(([, [source, kind]]) => ({ source, kind }));
|
||||
const named = (commands) => Object.entries(NAMED).filter(([name]) => names(commands, name)).map(([, [source, kind]]) => ({ source, kind }));
|
||||
|
||||
function plant(scratch) {
|
||||
const marks = join(scratch, "marks");
|
||||
@@ -715,12 +935,57 @@ function plant(scratch) {
|
||||
put(join(project, ".agents", "skills", "fit-agents-skill", "SKILL.md"), skill("fit-agents-skill"));
|
||||
put(join(project, ".pi", "prompts", "fit-project-prompt.md"), prompt);
|
||||
put(join(scratch, "home", ".agents", "skills", "fit-home-skill", "SKILL.md"), skill("fit-home-skill"));
|
||||
for (const n of GIVEN_SKILLS) put(join(scratch, "given", n, "SKILL.md"), skill(n));
|
||||
for (const [file, text] of TEXTS) put(join(scratch, file), `${text}\n`);
|
||||
put(join(scratch, "explicit", "fit.ts"), `import { writeFileSync } from "node:fs";\nexport default function (pi: any): void {\n writeFileSync(${JSON.stringify(join(marks, markerName(EXPLICIT)))}, "");\n pi.on("session_start", (_event: any, ctx: any) => { writeFileSync(${JSON.stringify(join(marks, "start.json"))}, JSON.stringify({ prompt: ctx.getSystemPrompt(), commands: pi.getCommands().map((c: any) => c.name), session: ctx.sessionManager.getSessionId() })); });\n}\n`);
|
||||
put(join(scratch, "explicit", "second.ts"), ext(SECOND));
|
||||
put(join(scratch, "prompt.md"), `${GIVEN}\n`);
|
||||
}
|
||||
|
||||
// S6: the model run's scratch directory: the gate's policy for the project (read, no typed tools),
|
||||
// no typed tools, the --system-prompt file, a project file the gate allows, a file outside the
|
||||
// project, a link to it in the project under each spelling's name on disk, and the scripted model's
|
||||
// config. The model writes the agent directory's models.json when it has its port.
|
||||
function plantModel(scratch) {
|
||||
const project = join(scratch, "project");
|
||||
for (const d of ["marks", "tmp", "sessions", "outside", "project"]) mkdirSync(join(scratch, d), { recursive: true });
|
||||
writeFileSync(join(project, "inside.txt"), `${INSIDE}\n`);
|
||||
writeFileSync(join(project, THROW_NAME), `${THROWN}\n`);
|
||||
const outside = join(scratch, "outside", "secret.txt");
|
||||
writeFileSync(outside, `${SECRET}\n`);
|
||||
for (const [, disk] of SPELLINGS) symlinkSync(outside, join(project, disk));
|
||||
writeFileSync(join(scratch, "policy.json"), JSON.stringify(fitPolicy(project)));
|
||||
writeFileSync(join(scratch, "tools.json"), "[]");
|
||||
writeFileSync(join(scratch, "prompt.md"), `${GIVEN}\n`);
|
||||
writeFileSync(join(scratch, "model.json"), JSON.stringify({
|
||||
record: join(scratch, "marks", "model.json"), models: join(scratch, "home", ".pi", "agent", "models.json"),
|
||||
provider: FIT_MODEL_ROUTE.provider, model: FIT_MODEL_ROUTE.model, key: "fit-model-key", script: MODEL_SCRIPT, done: DONE, secret: SECRET,
|
||||
}));
|
||||
}
|
||||
// What the scripted model recorded, as a list of { tools, system, results: [{ isError, content }],
|
||||
// done, leak }; empty when it recorded nothing that parses.
|
||||
function modelRecord(scratch) {
|
||||
let m;
|
||||
try { m = JSON.parse(readFileSync(join(scratch, "marks", "model.json"), "utf8")); } catch { return []; }
|
||||
if (!Array.isArray(m?.requests)) return [];
|
||||
return m.requests.filter((r) => r && typeof r === "object").map((r) => ({
|
||||
tools: Number.isInteger(r.tools) ? r.tools : 0,
|
||||
system: typeof r.system === "string" ? r.system : "",
|
||||
results: Array.isArray(r.results) ? r.results.map((x) => ({ isError: x?.isError === true, content: typeof x?.content === "string" ? x.content : "" })) : [],
|
||||
done: r.done === true,
|
||||
leak: r.leak === true,
|
||||
}));
|
||||
}
|
||||
// The turn marker the session's -e extension wrote at agent_end, a JSON object, else null.
|
||||
function turnMarker(scratch) {
|
||||
try {
|
||||
const m = JSON.parse(readFileSync(join(scratch, "turn"), "utf8"));
|
||||
return m && typeof m === "object" && !Array.isArray(m) ? m : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// A stored session for the project, as pi 0.85.1 writes one: a header whose cwd is the project's
|
||||
// real path (pi's -c takes only a session for its own cwd) and one user message.
|
||||
function plantSession(scratch) {
|
||||
|
||||
@@ -11,6 +11,7 @@ import { join } from "node:path";
|
||||
import { resolveInstance, validateBusinessDocument } from "../../organization/src/index.mjs";
|
||||
import { businessDoc, REPO_ROLES, systemFor } from "../../organization/tests/helpers.mjs";
|
||||
import { createLoader } from "../../plugins/src/loader.mjs";
|
||||
import { messagesHandler } from "../src/pi-fit-model.mjs";
|
||||
|
||||
export { REPO } from "../../organization/tests/helpers.mjs";
|
||||
import { REPO } from "../../organization/tests/helpers.mjs";
|
||||
@@ -76,54 +77,13 @@ export async function fakeToolSocket(t, dir, handler) {
|
||||
return { path, calls };
|
||||
}
|
||||
|
||||
const textOf = (c) => (typeof c === "string" ? c : Array.isArray(c) ? c.map((b) => b.text ?? "").join("") : "");
|
||||
function toolResults(messages) {
|
||||
const out = [];
|
||||
for (const m of messages ?? []) {
|
||||
if (m.role !== "user" || !Array.isArray(m.content)) continue;
|
||||
for (const b of m.content) if (b.type === "tool_result") out.push({ id: b.tool_use_id, isError: b.is_error ?? false, content: textOf(b.content) });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// A scripted Anthropic Messages API. Each request that offers tools gets
|
||||
// the next scripted tool call; once every call has a result it answers
|
||||
// `done(results)` as text. Returns { url, requests, close }.
|
||||
// `done(results)` as text. Returns { url, requests }. #1873 S6: the answers
|
||||
// come from src/pi-fit-model.mjs, which the fit check's model run uses too.
|
||||
export async function mockAnthropic(t, { script = [], done = (r) => `DONE ${JSON.stringify(r)}` } = {}) {
|
||||
const requests = [];
|
||||
const server = createHttp((req, res) => {
|
||||
let raw = "";
|
||||
req.on("data", (c) => (raw += c));
|
||||
req.on("end", () => {
|
||||
if (req.method !== "POST" || !/\/messages(\?|$)/.test(req.url)) {
|
||||
res.writeHead(req.url.includes("count_tokens") ? 200 : 404, { "content-type": "application/json" });
|
||||
return res.end(JSON.stringify(req.url.includes("count_tokens") ? { input_tokens: 10 } : { type: "error", error: { type: "not_found_error", message: "mock" } }));
|
||||
}
|
||||
const body = JSON.parse(raw || "{}");
|
||||
const tools = Array.isArray(body.tools) ? body.tools.map((x) => x.name) : [];
|
||||
const results = toolResults(body.messages);
|
||||
const step = tools.length ? script[results.length] : null;
|
||||
const r = step ? { kind: "tool", id: `toolu_${results.length + 1}`, name: step.name, input: step.input } : { kind: "text", text: tools.length ? done(results) : "mock" };
|
||||
requests.push({ tools, results, system: body.system, answer: r, raw });
|
||||
const content = r.kind === "tool" ? { type: "tool_use", id: r.id, name: r.name, input: {} } : { type: "text", text: "" };
|
||||
const delta = r.kind === "tool" ? { type: "input_json_delta", partial_json: JSON.stringify(r.input) } : { type: "text_delta", text: r.text };
|
||||
const stop = r.kind === "tool" ? "tool_use" : "end_turn";
|
||||
if (!body.stream) {
|
||||
res.writeHead(200, { "content-type": "application/json" });
|
||||
const c = r.kind === "tool" ? { type: "tool_use", id: r.id, name: r.name, input: r.input } : { type: "text", text: r.text };
|
||||
return res.end(JSON.stringify({ id: "msg_mock", type: "message", role: "assistant", model: body.model, content: [c], stop_reason: stop, stop_sequence: null, usage: { input_tokens: 10, output_tokens: 5 } }));
|
||||
}
|
||||
const send = (event, data) => res.write(`event: ${event}\ndata: ${JSON.stringify({ type: event, ...data })}\n\n`);
|
||||
res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" });
|
||||
send("message_start", { message: { id: "msg_mock", type: "message", role: "assistant", model: body.model, content: [], stop_reason: null, stop_sequence: null, usage: { input_tokens: 10, output_tokens: 1, cache_creation_input_tokens: 0, cache_read_input_tokens: 0 } } });
|
||||
send("content_block_start", { index: 0, content_block: content });
|
||||
send("content_block_delta", { index: 0, delta });
|
||||
send("content_block_stop", { index: 0 });
|
||||
send("message_delta", { delta: { stop_reason: stop, stop_sequence: null }, usage: { output_tokens: 5 } });
|
||||
send("message_stop", {});
|
||||
res.end();
|
||||
});
|
||||
});
|
||||
const server = createHttp(messagesHandler({ script, done, requests }));
|
||||
await new Promise((r) => server.listen(0, "127.0.0.1", r));
|
||||
t.after(() => server.close());
|
||||
return { url: `http://127.0.0.1:${server.address().port}`, requests };
|
||||
|
||||
@@ -10,12 +10,13 @@ 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";
|
||||
|
||||
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
|
||||
const NAME = "@earendil-works/pi-coding-agent";
|
||||
@@ -28,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 });
|
||||
@@ -123,6 +124,33 @@ test("pi-unpin S3: a stand-in that touches the planted MCP marker under the seal
|
||||
for (const [kind, line] of Object.entries(lines)) assert.equal((await fit(standIn(t, { sealLeak: [kind] }).env)).message, `pi 2.0.0 (MOSAIC_PI_BIN) ${line}`, kind);
|
||||
});
|
||||
|
||||
// pi-skills: the worker's shape. pi 0.85.1 adds a --skill directory to discovery, so the adapter
|
||||
// keeps --no-skills in front of each --skill; the fit check's last -p run passes the adapter's -p
|
||||
// argv with MOSAIC_SKILLS (workerShape) and two --skill directories.
|
||||
test("pi-skills: a stand-in that loads the planted skills once a --skill directory is given is refused", SKIP, async t => {
|
||||
const s = standIn(t, { skillLeak: true });
|
||||
const problem = await pi.preflight({ env: { ...CREDENTIAL, ...s.env } });
|
||||
assert.deepEqual(problem, { code: "harness-feature-missing", message: "pi 2.0.0 (MOSAIC_PI_BIN) loaded a planted user skill, user settings skill, user package skill, project skill, project settings skill, project package skill, project .agents skill, home .agents skill in a worker's shape with --skill" });
|
||||
noPath(problem.message, s.paths);
|
||||
});
|
||||
|
||||
test("pi-skills: a stand-in that drops a --skill directory once --no-skills is on is refused", SKIP, async t => {
|
||||
const s = standIn(t, { ignoreSkill: true });
|
||||
const problem = await pi.preflight({ env: { ...CREDENTIAL, ...s.env } });
|
||||
assert.deepEqual(problem, { code: "harness-feature-missing", message: "pi 2.0.0 (MOSAIC_PI_BIN) didn't load every --skill directory with --no-skills on in a worker's shape" });
|
||||
noPath(problem.message, s.paths);
|
||||
});
|
||||
|
||||
// Review P3-1: the run is the adapter's -p argv, --provider and --model included, so a pi that
|
||||
// leaks only once --skill and --provider are both given is seen.
|
||||
test("pi-skills review: a stand-in that loads the planted sources only with --skill and --provider is refused", SKIP, async t => {
|
||||
const s = standIn(t, { leakWith: ["--skill", "--provider"] });
|
||||
const problem = await pi.preflight({ env: { ...CREDENTIAL, ...s.env } });
|
||||
assert.equal(problem.code, "harness-feature-missing");
|
||||
assert.match(problem.message, /^pi 2\.0\.0 \(MOSAIC_PI_BIN\) loaded a planted .* in a worker's shape with --skill$/);
|
||||
noPath(problem.message, s.paths);
|
||||
});
|
||||
|
||||
test("pi-unpin S3: a stand-in that ignores the planted sources even without the seal flags fails the positive control and is refused as can't tell", SKIP, async t => {
|
||||
const s = standIn(t, { ignoreAll: true });
|
||||
const problem = await pi.preflight({ env: { ...CREDENTIAL, ...s.env } });
|
||||
@@ -174,6 +202,12 @@ test("pi-unpin S3 re-check 3: every run has its own scratch directory, and the r
|
||||
assert.deepEqual(a.slice(bin - 3, bin + 1), ["/bin/sh", "-c", 'exec "$0" "$@"', found.view]);
|
||||
return { scratch, args: a.slice(bin + 1).map((x) => x.split(scratch).join("<s>")) };
|
||||
});
|
||||
// #1873 S6: the scripted-model run is a session-shaped -p run on the stand-in route, with the
|
||||
// gate's extension, inside the scripted model's process. It is checked here and left out below.
|
||||
const modelRuns = runs.filter((x) => x.args.includes(FIT_MODEL_ROUTE.provider));
|
||||
assert.deepEqual(modelRuns.map((x) => x.args), [fitModule.sessionShape({ noMcp: extraFlags.length > 0, extensions: [...fitModule.MODEL_EXTENSIONS], prompt: "<s>/prompt.md", sessionDir: "<s>/sessions", message: "fit check", ...fitModule.MODEL_SHAPE, route: FIT_MODEL_ROUTE })]);
|
||||
assert.equal(new Set(runs.map((x) => x.scratch)).size, runs.length, "the model run has its own scratch directory too");
|
||||
runs.splice(runs.indexOf(modelRuns[0]), 1);
|
||||
const seal = [...SEAL, ...extraFlags];
|
||||
const loaders = [...LOADERS, ...extraFlags];
|
||||
// --version, --help, the control, the sealed rpc, one per loader flag, the session-shaped runs,
|
||||
@@ -188,7 +222,12 @@ test("pi-unpin S3 re-check 3: every run has its own scratch directory, and the r
|
||||
const shaped = (args) => args.includes("--session-dir");
|
||||
assert.deepEqual(runs.filter((x) => shaped(x.args)).map((x) => JSON.stringify(x.args)).sort(), shapes.map((shape) => JSON.stringify(fitModule.sessionShape({ noMcp: extraFlags.length > 0, extensions: ["<s>/explicit/fit.ts", "<s>/explicit/second.ts"], prompt: "<s>/prompt.md", sessionDir: "<s>/sessions", message: "fit check", ...shape }))).sort());
|
||||
const flagLists = [];
|
||||
for (const { args } of runs.filter((x) => x.args.length > 1 && !shaped(x.args))) {
|
||||
// pi-skills: the last -p run is the adapter's -p argv with two --skill directories, built by
|
||||
// workerShape, which the adapter test below holds to the adapter. No other run passes --skill.
|
||||
const skilled = runs.filter((x) => x.args.includes("--skill"));
|
||||
assert.equal(skilled.length, 1);
|
||||
assert.deepEqual(skilled[0].args, fitModule.workerShape({ noMcp: extraFlags.length > 0, extensions: ["<s>/explicit/fit.ts"], skills: ["<s>/given/fit-given-skill", "<s>/given/fit-second-given-skill"], prompt: "<s>/prompt.md", message: "fit check" }));
|
||||
for (const { args } of runs.filter((x) => x.args.length > 1 && !shaped(x.args) && !x.args.includes("--skill"))) {
|
||||
const flags = args.filter((a) => seal.includes(a));
|
||||
assert.deepEqual(flags, seal.filter((f) => flags.includes(f)), "the seal flags keep their order");
|
||||
const rest = [];
|
||||
@@ -511,14 +550,21 @@ test("pi-unpin S3 re-check 7: env() refuses while the fit check is still running
|
||||
assert.equal(pi.env({ policy: { tools: ["read"] }, tools: [], session, env: s.env }).PI_OFFLINE, "1");
|
||||
});
|
||||
|
||||
// #1873 W2: a check that could not run is a failure with the digest of the files it would have run
|
||||
// and undecided: true, beside its message.
|
||||
function assertUndecided(r, message) {
|
||||
assert.deepEqual(r, { ok: false, message, digest: r.digest, undecided: true });
|
||||
assert.match(r.digest, /^[0-9a-f]{64}$/);
|
||||
}
|
||||
|
||||
test("pi-unpin S3: a check that cannot run refuses", SKIP, async t => {
|
||||
const s = standIn(t, {});
|
||||
const r = await piFit(locate(s.env), { levels: LEVELS, bwrap: join(s.root, "no-bwrap") });
|
||||
assert.deepEqual(r, { ok: false, message: "pi (MOSAIC_PI_BIN) can't be checked: the sandbox (bwrap) is missing" });
|
||||
assertUndecided(r, "pi (MOSAIC_PI_BIN) can't be checked: the sandbox (bwrap) is missing");
|
||||
// A sandbox that fails says so without its own error text, which can name a host path.
|
||||
const broken = join(s.root, "broken-bwrap");
|
||||
writeFileSync(broken, `#!/bin/sh\necho "bwrap: Can't find source path ${s.root}/x" >&2\nexit 1\n`, { mode: 0o755 });
|
||||
assert.deepEqual(await piFit(locate(s.env), { levels: LEVELS, bwrap: broken }), { ok: false, message: "pi (MOSAIC_PI_BIN) can't be checked: the sandbox (bwrap) failed" });
|
||||
assertUndecided(await piFit(locate(s.env), { levels: LEVELS, bwrap: broken }), "pi (MOSAIC_PI_BIN) can't be checked: the sandbox (bwrap) failed");
|
||||
// An unreadable package is refused before anything runs.
|
||||
const found = locate(s.env);
|
||||
rmSync(join(s.root, "prefix", "lib", "node_modules", NAME, "package.json"));
|
||||
@@ -558,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 });
|
||||
@@ -665,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 });
|
||||
@@ -710,7 +756,7 @@ test("pi-unpin S3 re-check 10: a failed probe names the tool that failed, and on
|
||||
assert.deepEqual(r.outer, { used: false, why });
|
||||
}
|
||||
assert.deepEqual((await fitWith({})).outer, { used: true });
|
||||
assert.deepEqual(await fitWith({ unshare: refusing, bwrap: broken }), { ok: false, message: "pi (MOSAIC_PI_BIN) can't be checked: the sandbox (bwrap) failed" });
|
||||
assertUndecided(await fitWith({ unshare: refusing, bwrap: broken }), "pi (MOSAIC_PI_BIN) can't be checked: the sandbox (bwrap) failed");
|
||||
});
|
||||
|
||||
// R10-P2-1: without unshare's outer layer, a bwrap killed in its setup leaves its child waiting for
|
||||
@@ -720,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 });
|
||||
@@ -801,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 {}
|
||||
@@ -863,6 +909,26 @@ test("pi-unpin S3: real pi 0.85.1 from node_modules is accepted, and the event l
|
||||
assert.ok(max <= GAP_MAX, `the event loop waited ${max.toFixed(0)} ms during the cold check`);
|
||||
});
|
||||
|
||||
// pi-skills: real pi passes the fit check's worker-shaped -p run, --no-skills and two --skill
|
||||
// directories: it lists both given skills and no planted one. A logging bwrap shows the run.
|
||||
test("pi-skills: real pi 0.85.1 passes the fit check's worker-shaped run with --no-skills and two --skill directories", SKIP, async t => {
|
||||
const found = locate({ PATH: "/usr/bin:/bin" });
|
||||
const root = fixtureDir("mosaic-pi-skills-fit-");
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const log = join(root, "bwrap.log");
|
||||
const bwrap = join(root, "bwrap");
|
||||
writeFileSync(bwrap, `#!/bin/sh\nf=$(/usr/bin/mktemp '${log}.XXXXXX')\nfor a in "$@"; do printf '%s\\037' "$a"; done > "$f"\nexec '${BWRAP}' "$@"\n`, { mode: 0o755 });
|
||||
const r = await piFit(found, { levels: LEVELS, bwrap });
|
||||
assert.equal(r.ok, true, r.message);
|
||||
const skilled = readdirSync(root).filter((n) => n.startsWith("bwrap.log.")).map((n) => readFileSync(join(root, n), "utf8").split("\x1f")).filter((a) => a.includes("--skill"));
|
||||
assert.equal(skilled.length, 1);
|
||||
const a = skilled[0];
|
||||
assert.equal(a[a.indexOf("--no-skills") + 1], "--skill");
|
||||
assert.match(a[a.indexOf("--skill") + 1], /\/given\/fit-given-skill$/);
|
||||
assert.match(a[a.indexOf("--skill") + 3], /\/given\/fit-second-given-skill$/);
|
||||
for (const f of ["-p", "--no-session", "--provider", "--model"]) assert.ok(a.includes(f), f);
|
||||
});
|
||||
|
||||
test("pi-unpin S3 re-check 7: the event loop stays live while a stand-in's cold check runs", SKIP, async t => {
|
||||
const { result, max } = await gapDuring(() => fit(standIn(t, {}).env));
|
||||
assert.equal(result.ok, true, result.message);
|
||||
@@ -939,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) => {
|
||||
@@ -957,6 +1142,95 @@ test("pi-unpin S3: the adapter passes --no-mcp only when the module asks for it"
|
||||
assert.equal((await run(standIn(t, {}))).includes("--no-mcp"), false);
|
||||
});
|
||||
|
||||
// pi-skills: a worker's MOSAIC_SKILLS. pi 0.85.1 adds a --skill directory to discovery, so the
|
||||
// adapter keeps --no-skills and puts each --skill after it. A stub records the argv.
|
||||
const workerAdapter = (t, extra = {}) => {
|
||||
const root = 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 });
|
||||
writeFileSync(join(root, "prompt.md"), "p\n");
|
||||
const env = { PATH: "/usr/bin:/bin", HOME: join(root, "home"), ARGV_OUT: join(root, "argv"), MOSAIC_PI_BIN: stub, MOSAIC_SYSTEM_PROMPT_FILE: join(root, "prompt.md"), PI_PROVIDER: "fixture", PI_MODEL: "m", MOSAIC_REQUEST: "x", ...extra(root) };
|
||||
const r = spawnSync("/bin/sh", [join(REPO, "adapters", "pi", "adapter.sh")], { encoding: "utf8", env });
|
||||
return { root, r, argv: () => readFileSync(join(root, "argv"), "utf8").split("\0").slice(0, -1) };
|
||||
};
|
||||
|
||||
test("pi-skills: with MOSAIC_SKILLS the adapter passes --no-skills, then --skill for each directory", t => {
|
||||
const w = workerAdapter(t, (root) => {
|
||||
for (const n of ["one", "two"]) mkdirSync(join(root, "skills", n), { recursive: true });
|
||||
return { MOSAIC_SKILLS: `${join(root, "skills", "one")},${join(root, "skills", "two")}` };
|
||||
});
|
||||
assert.equal(w.r.status, 0, w.r.stderr);
|
||||
const argv = w.argv();
|
||||
const i = argv.indexOf("--no-skills");
|
||||
assert.ok(i >= 0, argv.join(" "));
|
||||
assert.equal(argv.filter((a) => a === "--no-skills").length, 1, argv.join(" "));
|
||||
assert.deepEqual(argv.slice(i, i + 5), ["--no-skills", "--skill", join(w.root, "skills", "one"), "--skill", join(w.root, "skills", "two")]);
|
||||
assert.equal(argv.filter((a) => a === "--skill").length, 2, argv.join(" "));
|
||||
// Without MOSAIC_SKILLS: --no-skills alone, as before.
|
||||
const bare = workerAdapter(t, () => ({}));
|
||||
assert.equal(bare.r.status, 0, bare.r.stderr);
|
||||
assert.ok(bare.argv().includes("--no-skills"));
|
||||
assert.ok(!bare.argv().includes("--skill"));
|
||||
});
|
||||
|
||||
// Review P3-1: the fit check's worker-shaped run is the argv the adapter builds in -p with
|
||||
// MOSAIC_SKILLS, an -e file, no tools and no session, flag for flag and in order.
|
||||
test("pi-skills review: the adapter's -p argv with MOSAIC_SKILLS is workerShape's", t => {
|
||||
const w = workerAdapter(t, (root) => {
|
||||
for (const n of ["one", "two"]) mkdirSync(join(root, "skills", n), { recursive: true });
|
||||
writeFileSync(join(root, "fit.ts"), "");
|
||||
return { MOSAIC_SKILLS: `${join(root, "skills", "one")},${join(root, "skills", "two")}`, MOSAIC_EXTENSIONS: join(root, "fit.ts"), PI_PROVIDER: fitModule.SESSION_SHAPE.provider, PI_MODEL: fitModule.SESSION_SHAPE.model };
|
||||
});
|
||||
assert.equal(w.r.status, 0, w.r.stderr);
|
||||
assert.deepEqual(w.argv(), fitModule.workerShape({ noMcp: false, extensions: [join(w.root, "fit.ts")], skills: [join(w.root, "skills", "one"), join(w.root, "skills", "two")], prompt: join(w.root, "prompt.md"), message: "x" }));
|
||||
});
|
||||
|
||||
test("pi-skills: a missing MOSAIC_SKILLS directory still exits 2 before pi runs", t => {
|
||||
const w = workerAdapter(t, (root) => {
|
||||
mkdirSync(join(root, "skills", "one"), { recursive: true });
|
||||
return { MOSAIC_SKILLS: `${join(root, "skills", "one")},${join(root, "skills", "gone")}` };
|
||||
});
|
||||
assert.equal(w.r.status, 2);
|
||||
assert.match(w.r.stderr, /^pi adapter: skill dir missing: /);
|
||||
assert.equal(existsSync(join(w.root, "argv")), false, "pi did not run");
|
||||
});
|
||||
|
||||
// pi-skills: the adapter's -p shape end to end. It runs real pi 0.85.1 with MOSAIC_SKILLS, an
|
||||
// -e extension that writes the command names at session_start, a HOME and workspace holding planted
|
||||
// skills in every place pi 0.85.1 looks, the network unshared and no key: pi stops before any model
|
||||
// call. Only the given skills may load. On the adapter that dropped --no-skills, the user, settings
|
||||
// and ~/.agents skills loaded too.
|
||||
const UNSHARE_NET = spawnSync("unshare", ["-rn", "true"]).status === 0 ? {} : { skip: "no unshare -rn here" };
|
||||
test("pi-skills: real pi 0.85.1 through the adapter with MOSAIC_SKILLS loads the given skills and no planted one", UNSHARE_NET, async t => {
|
||||
const found = locate({ PATH: "/usr/bin:/bin" });
|
||||
assert.equal(found.label, "the repository's pi");
|
||||
const root = 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`);
|
||||
const home = join(root, "home");
|
||||
const ws = join(root, "ws");
|
||||
for (const s of ["home/.pi/agent/skills/amb-user-skill", "home/.agents/skills/amb-home-skill", "listed/amb-set-skill", "listed/pkg/skills/amb-pkg-skill", "ws/.pi/skills/amb-project-skill", "ws/.agents/skills/amb-agents-skill", "given/one-skill", "given/two-skill"]) skill(s);
|
||||
put(join(root, "listed", "pkg", "package.json"), JSON.stringify({ name: "amb-package", version: "1.0.0" }));
|
||||
put(join(home, ".pi", "agent", "settings.json"), JSON.stringify({ skills: [join(root, "listed", "amb-set-skill")], packages: [join(root, "listed", "pkg")], defaultProjectTrust: "always" }));
|
||||
put(join(ws, ".pi", "settings.json"), JSON.stringify({ skills: [join(root, "listed", "amb-set-skill")] }));
|
||||
const marks = join(root, "start.json");
|
||||
put(join(root, "ext", "list.ts"), `import { writeFileSync } from "node:fs";\nexport default function (pi: any): void {\n pi.on("session_start", () => { writeFileSync(${JSON.stringify(marks)}, JSON.stringify(pi.getCommands().map((c: any) => c.name))); });\n}\n`);
|
||||
put(join(root, "prompt.md"), "p\n");
|
||||
mkdirSync(join(root, "tmp"));
|
||||
const env = {
|
||||
PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, HOME: home, TMPDIR: join(root, "tmp"), LANG: "C.UTF-8",
|
||||
MOSAIC_PI_BIN: found.bin, MOSAIC_SYSTEM_PROMPT_FILE: join(root, "prompt.md"), PI_PROVIDER: fitModule.SESSION_SHAPE.provider, PI_MODEL: fitModule.SESSION_SHAPE.model,
|
||||
MOSAIC_REQUEST: "fit check", MOSAIC_WORKSPACE: ws, MOSAIC_EXTENSIONS: join(root, "ext", "list.ts"),
|
||||
MOSAIC_SKILLS: `${join(root, "given", "one-skill")},${join(root, "given", "two-skill")}`,
|
||||
};
|
||||
spawnSync("unshare", ["-rn", "/bin/sh", join(REPO, "adapters", "pi", "adapter.sh")], { encoding: "utf8", env, timeout: 60000 });
|
||||
const commands = JSON.parse(readFileSync(marks, "utf8"));
|
||||
const skills = commands.filter((c) => c.startsWith("skill:")).map((c) => c.slice(6)).sort();
|
||||
assert.deepEqual(skills, ["one-skill", "two-skill"]);
|
||||
});
|
||||
|
||||
// Re-check 5 item 1, narrowed by re-check 6 P3-1. The adapter runs with the environment a bus
|
||||
// session's pi gets (busEnv: sessionEnv over a host, sessionSetenv, adapterEnv, pi.env()), so
|
||||
// HOME and the rest are set as a session sets them, not by the shell. Re-check 7 P2-1: every run is
|
||||
@@ -1052,3 +1326,192 @@ test("pi-unpin S3 re-check 6: every argv the adapter builds in a bus session's e
|
||||
assert.ok(set.has(name) || local || SHELL_SET.includes(name) || NOT_BUS.includes(name), `the adapter reads ${name} (line ${at + 1}), which no bus session environment here sets and the adapter doesn't assign first`);
|
||||
}
|
||||
});
|
||||
|
||||
// #1873 S6: the fit check runs pi once against a scripted model on the sandbox's loopback
|
||||
// (pi-fit-model.mjs). The gate's tool_call hook must decide every tool run, the run must end with
|
||||
// agent_end, the --system-prompt file must reach the model, and the four read spellings pi 0.85.1
|
||||
// knows must go through the hook. A run that can't tell is undecided.
|
||||
const MODEL_FILE = fileURLToPath(new URL("../src/pi-fit-model.mjs", import.meta.url));
|
||||
// The processes of this file's model runs: each names its scratch directory, which carries
|
||||
// "-model-", under tmpdir() (the model's config path, pi's --system-prompt and the sandbox layers).
|
||||
const modelProcs = () => procsNaming(join(tmpdir(), "mosaic-pi-fit-")).filter((p) => {
|
||||
try { return readFileSync(`/proc/${p}/cmdline`, "utf8").includes("-model-"); } catch { return false; }
|
||||
});
|
||||
const modelDirs = () => readdirSync(tmpdir()).filter((n) => n.startsWith("mosaic-pi-fit-") && n.includes("-model-"));
|
||||
async function noModelLeft(what) {
|
||||
const deadline = Date.now() + 2000;
|
||||
while (modelProcs().length && Date.now() < deadline) await new Promise((resolve) => setTimeout(resolve, 50));
|
||||
assert.deepEqual(modelProcs(), [], `a model run process outlived ${what}`);
|
||||
assert.deepEqual(modelDirs(), [], `a model run's scratch outlived ${what}`);
|
||||
}
|
||||
|
||||
test("pi-unpin S6: a stand-in pi passes the model run, and no model process or scratch is left after the pass", SKIP, async t => {
|
||||
const r = await fit(standIn(t, {}).env);
|
||||
assert.equal(r.ok, true, r.message);
|
||||
await noModelLeft("a pass");
|
||||
});
|
||||
|
||||
test("pi-unpin S6: a pi that runs a tool the gate's hook blocked is refused, and nothing is left", SKIP, async t => {
|
||||
const s = standIn(t, { ignoreBlock: true });
|
||||
const r = await fit(s.env);
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.undecided, false);
|
||||
assert.equal(r.message, "pi 2.0.0 (MOSAIC_PI_BIN) the gate's block didn't stop the tool");
|
||||
noPath(r.message, s.paths);
|
||||
await noModelLeft("a refusal");
|
||||
});
|
||||
|
||||
test("pi-unpin S6: a pi that runs a tool when a tool_call handler throws is refused", SKIP, async t => {
|
||||
const s = standIn(t, { ignoreThrow: true });
|
||||
const r = await fit(s.env);
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.undecided, false);
|
||||
assert.equal(r.message, "pi 2.0.0 (MOSAIC_PI_BIN) a throw in a tool_call handler didn't stop the tool");
|
||||
noPath(r.message, s.paths);
|
||||
await noModelLeft("a refusal");
|
||||
});
|
||||
|
||||
test("pi-unpin S6: a pi that never emits agent_end is refused", SKIP, async t => {
|
||||
const r = await fit(standIn(t, { noAgentEnd: true }).env);
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.undecided, false);
|
||||
assert.equal(r.message, "pi 2.0.0 (MOSAIC_PI_BIN) didn't end its run with agent_end");
|
||||
});
|
||||
|
||||
test("pi-unpin S6: a pi that drops the system prompt from its model requests is refused", SKIP, async t => {
|
||||
const r = await fit(standIn(t, { modelNoSystem: true }).env);
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.undecided, false);
|
||||
assert.equal(r.message, "pi 2.0.0 (MOSAIC_PI_BIN) didn't send the --system-prompt file to the model");
|
||||
});
|
||||
|
||||
for (const [spelling, label] of [["amPm", "a narrow space before AM"], ["nfd", "decomposed accents"], ["curly", "a curly apostrophe"], ["nfdCurly", "decomposed accents and a curly apostrophe"]]) {
|
||||
test(`pi-unpin S6: a pi whose read under another spelling (${spelling}) skips the gate's hook is refused`, SKIP, async t => {
|
||||
const r = await fit(standIn(t, { skipHookSpelling: spelling }).env);
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.undecided, false);
|
||||
assert.equal(r.message, `pi 2.0.0 (MOSAIC_PI_BIN) the gate's block didn't stop the tool for a read under another spelling (${label})`);
|
||||
});
|
||||
}
|
||||
|
||||
// A blocked read that fails on its own, with no hook run, holds no outside word, so only the check
|
||||
// that the error carries the gate's own reason refuses it.
|
||||
for (const [skip, label] of [["outside", "a read outside the workspace"], ["amPm", "a read under another spelling (a narrow space before AM)"], ["nfd", "a read under another spelling (decomposed accents)"], ["curly", "a read under another spelling (a curly apostrophe)"], ["nfdCurly", "a read under another spelling (decomposed accents and a curly apostrophe)"]]) {
|
||||
test(`pi-unpin S6: a pi whose read (${skip}) skips the gate's hook and fails on its own is refused`, SKIP, async t => {
|
||||
const r = await fit(standIn(t, { hookSkipFail: skip }).env);
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.undecided, false);
|
||||
assert.equal(r.message, `pi 2.0.0 (MOSAIC_PI_BIN) the gate's tool_call hook didn't decide the tool for ${label}`);
|
||||
});
|
||||
}
|
||||
|
||||
test("pi-unpin S6: a pi whose reads all fail, the allowed one too, is refused", SKIP, async t => {
|
||||
const r = await fit(standIn(t, { readFails: true }).env);
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.undecided, false);
|
||||
assert.equal(r.message, "pi 2.0.0 (MOSAIC_PI_BIN) didn't run the tool the gate allowed");
|
||||
});
|
||||
|
||||
test("pi-unpin S6: a pi whose agent_end carries no final message is refused", SKIP, async t => {
|
||||
const r = await fit(standIn(t, { agentEndEmpty: true }).env);
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.undecided, false);
|
||||
assert.equal(r.message, "pi 2.0.0 (MOSAIC_PI_BIN) its agent_end didn't carry the model's final answer");
|
||||
});
|
||||
|
||||
test("pi-unpin S6: a pi that finishes the script and then exits non-zero is undecided, not a pass", SKIP, async t => {
|
||||
const r = await fit(standIn(t, { exitCode: 1 }).env);
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.undecided, true);
|
||||
assert.equal(r.message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: the scripted-model run exited non-zero");
|
||||
});
|
||||
|
||||
test("pi-unpin S6: a pi that exits non-zero before any model request is undecided, and the line says so", SKIP, async t => {
|
||||
const r = await fit(standIn(t, { noModelCall: true, exitCode: 1 }).env);
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.undecided, true);
|
||||
assert.equal(r.message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: the scripted-model run exited non-zero before any model request");
|
||||
});
|
||||
|
||||
test("pi-unpin S6: a pi that runs a blocked tool and then hangs is refused, not undecided, and nothing is left", SKIP, async t => {
|
||||
const r = await fit(standIn(t, { ignoreBlock: true, modelHang: 2 }).env);
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.undecided, false);
|
||||
assert.equal(r.message, "pi 2.0.0 (MOSAIC_PI_BIN) the gate's block didn't stop the tool");
|
||||
await noModelLeft("a refusal after a hang");
|
||||
});
|
||||
|
||||
test("pi-unpin S6: a run whose scripted model gets no request is undecided, not a pass", SKIP, async t => {
|
||||
const r = await fit(standIn(t, { noModelCall: true }).env);
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.undecided, true);
|
||||
assert.equal(r.message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: the scripted model got no request");
|
||||
});
|
||||
|
||||
test("pi-unpin S6: a model run that times out is undecided, and the model and every child are gone", SKIP, async t => {
|
||||
const r = await fit(standIn(t, { modelHang: true }).env);
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.undecided, true);
|
||||
assert.equal(r.message, "pi 2.0.0 (MOSAIC_PI_BIN) can't be checked: the scripted-model run timed out");
|
||||
await noModelLeft("a timeout");
|
||||
});
|
||||
|
||||
test("pi-unpin S6: a check aborted while its model run runs is undecided, and the model and every child are gone", SKIP, async t => {
|
||||
const s = standIn(t, { modelHang: true });
|
||||
const abort = new AbortController();
|
||||
const pending = piFit(locate(s.env), { levels: LEVELS, signal: abort.signal });
|
||||
const deadline = Date.now() + 15000;
|
||||
while (!modelProcs().some((p) => readFileSync(`/proc/${p}/cmdline`, "utf8").includes(MODEL_FILE)) && Date.now() < deadline) await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
assert.ok(modelDirs().length === 1, "the model run did not start");
|
||||
// Let the stand-in reach its hang, after the model's first answer.
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
abort.abort();
|
||||
const r = await pending;
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.undecided, true);
|
||||
await noModelLeft("an abort");
|
||||
});
|
||||
|
||||
// A fresh copy of the module, with its own cache, so an earlier test's pass of the same pi is not
|
||||
// reused. Each model run makes one "-model-" scratch directory, so a watch on tmpdir() counts them.
|
||||
test("pi-unpin S6: real pi 0.85.1 passes the model run, and a second call for the same digest runs no model stand-in", SKIP, async t => {
|
||||
const fresh = await import(`../src/pi-fit.mjs?s6-${process.pid}`);
|
||||
const made = new Set();
|
||||
const watcher = watch(tmpdir(), (event, name) => { if (name && name.startsWith("mosaic-pi-fit-") && name.includes("-model-")) made.add(name); });
|
||||
t.after(() => watcher.close());
|
||||
const settle = () => new Promise((resolve) => setTimeout(resolve, 200));
|
||||
const found = locate({ PATH: "/usr/bin:/bin" });
|
||||
const first = await fresh.piFit(found, { levels: LEVELS });
|
||||
assert.equal(first.ok, true, first.message);
|
||||
assert.equal(first.version, "0.85.1");
|
||||
await settle();
|
||||
assert.equal(made.size, 1, `the first check made ${made.size} model runs`);
|
||||
made.clear();
|
||||
assert.equal(await fresh.piFit(found, { levels: LEVELS }), first, "the second call reuses the pass");
|
||||
await settle();
|
||||
assert.equal(made.size, 0, "the second call for the same digest ran the model stand-in");
|
||||
await noModelLeft("real pi's pass");
|
||||
});
|
||||
|
||||
test("pi-unpin S6: the model run is sandboxed with no network but loopback, no provider key, and a route that names no Claude model", SKIP, async t => {
|
||||
const claude = /claude|anthropic|sonnet|opus|haiku|fable/i;
|
||||
for (const name of [FIT_MODEL_ROUTE.provider, FIT_MODEL_ROUTE.model]) {
|
||||
assert.ok(typeof name === "string" && name.length > 0);
|
||||
assert.doesNotMatch(name, claude, "the model run's route names a Claude model or provider");
|
||||
}
|
||||
const s = standIn(t, {});
|
||||
const log = join(s.root, "bwrap.log");
|
||||
const bwrap = join(s.root, "bwrap");
|
||||
writeFileSync(bwrap, `#!/bin/sh\nf=$(/usr/bin/mktemp '${log}.XXXXXX')\nfor a in "$@"; do printf '%s\\037' "$a"; done > "$f"\nexec '${BWRAP}' "$@"\n`, { mode: 0o755 });
|
||||
const r = await piFit(locate(s.env), { levels: LEVELS, bwrap, host: { ...HOSTS.unit, ...CREDENTIAL } });
|
||||
assert.equal(r.ok, true, r.message);
|
||||
const runs = readdirSync(s.root).filter((n) => n.startsWith("bwrap.log.")).map((n) => readFileSync(join(s.root, n), "utf8").split("\x1f")).filter((a) => a.includes(MODEL_FILE));
|
||||
assert.equal(runs.length, 1);
|
||||
const a = runs[0];
|
||||
assert.ok(a.includes("--unshare-net"), "the model run shares the host's network");
|
||||
assert.ok(a.includes("--clearenv"));
|
||||
const setenv = a.flatMap((x, i) => (x === "--setenv" ? [a[i + 1]] : []));
|
||||
for (const name of setenv) assert.equal(isProviderVariable(name), false, `the model run is given ${name}`);
|
||||
assert.equal(a[a.indexOf("--provider") + 1], FIT_MODEL_ROUTE.provider);
|
||||
assert.equal(a[a.indexOf("--model") + 1], FIT_MODEL_ROUTE.model);
|
||||
});
|
||||
@@ -1,12 +1,16 @@
|
||||
import { fixtureCacheEnv } from '../../../scripts/testing/lane-fixtures.mjs';
|
||||
// Real pi (the pinned CLI) through adapters/pi with the extension, against
|
||||
// the scripted Messages API. No real model, no network beyond 127.0.0.1.
|
||||
// Real pi through adapters/pi with the extension, against the scripted
|
||||
// Messages API. No real model, no network beyond 127.0.0.1. #1873 S6: the pi
|
||||
// is the one the pi module's lookup finds (MOSAIC_PI_BIN, else the first pi on
|
||||
// PATH, else the repository's), so `MOSAIC_PI_BIN=/abs/pi node --test` runs the
|
||||
// suite against another pi. It must pass the fit check first.
|
||||
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawn } from "node:child_process";
|
||||
import { existsSync, mkdirSync, readdirSync, readFileSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { locate } from "../../plugins/harness/pi/binary.mjs";
|
||||
import { buildBundle } from "../src/bundle.mjs";
|
||||
import { adapterEnv, unslopCounts, unslopLog, unslopNotes } from "../src/runner.mjs";
|
||||
import { REPLY_CAP } from "../src/tools.mjs";
|
||||
@@ -53,7 +57,8 @@ async function session(t, script, place = (dir) => join(dir, "ws"), instance = "
|
||||
s.bundle = files;
|
||||
s.harnessEnv = harnessEnv;
|
||||
const env = adapterEnv(s, {
|
||||
PATH: `${join(REPO, "node_modules", ".bin")}:/usr/bin:/bin`,
|
||||
// #1873 S6: the node running this suite, for a pi whose first line is `#!/usr/bin/env node`.
|
||||
PATH: `${join(REPO, "node_modules", ".bin")}:${dirname(process.execPath)}:/usr/bin:/bin`,
|
||||
HOME: join(dir, "home"),
|
||||
PI_CODING_AGENT_DIR: agentDir,
|
||||
});
|
||||
@@ -75,6 +80,21 @@ function turn(env, request, cwd) {
|
||||
});
|
||||
}
|
||||
|
||||
// #1873 S6: MOSAIC_PI_BIN, when the suite runs with it, is the pi every session here runs.
|
||||
test("pi: the session runs the pi MOSAIC_PI_BIN names, else the repository's", async (t) => {
|
||||
const { api, s, env } = await session(t, []);
|
||||
// The pi module's own lookup on this process's environment: MOSAIC_PI_BIN, else the first pi on
|
||||
// PATH, else the repository's.
|
||||
const found = locate(process.env);
|
||||
assert.ok(found.bin, found.problem);
|
||||
t.diagnostic(`pi under test: ${found.label}`);
|
||||
assert.equal(realpathSync(env.MOSAIC_PI_BIN), realpathSync(found.bin));
|
||||
const r = await turn(env, "Message 1 from jason, class REQUEST:\n\nhello", s.workspace);
|
||||
assert.equal(r.code, 0, r.stderr);
|
||||
assert.ok(api.requests.length > 0, "the pi it names called the scripted model");
|
||||
assert.ok(existsSync(s.turnMarker));
|
||||
});
|
||||
|
||||
test("pi: typed tools reach the socket, the gate blocks, agent_end writes the marker", async (t) => {
|
||||
const { api, sock, s, env } = await session(t, [
|
||||
{ name: "list_agents", input: {} },
|
||||
|
||||
@@ -54,12 +54,58 @@
|
||||
// no commands.
|
||||
// `commands` "fail" answers get_commands with success false; "list" answers
|
||||
// with a bare list.
|
||||
// --skill <dir> loads that skill (its directory's name) whatever --no-skills says,
|
||||
// as pi 0.85.1 does. `skillLeak`: with a --skill given, the planted skills load too
|
||||
// whatever --no-skills says (discovery comes back with --skill). `ignoreSkill`: a
|
||||
// --skill directory loads only without --no-skills.
|
||||
// -p with --provider FIT_MODEL_ROUTE's provider (#1873 S6, the fit check's model run): an agent
|
||||
// loop on the scripted model. It reads the provider's baseUrl from the agent
|
||||
// directory's models.json, imports each -e file and calls its default export with a
|
||||
// small pi (on, registerTool), then posts to <baseUrl>/v1/messages with the
|
||||
// --system-prompt file's text as system and one read tool. Each tool_use runs the
|
||||
// tool_call handlers first; a { block } result gives an error tool result holding the
|
||||
// reason, else the read runs, trying pi 0.85.1's other spellings when the path is
|
||||
// missing (a narrow space before AM or PM, NFD, a curly apostrophe, both). A text
|
||||
// answer fires agent_end with the assistant message, prints the text and exits 0.
|
||||
// `ignoreBlock`: the read runs whatever the handlers say. `ignoreThrow`: a
|
||||
// handler's throw is dropped and the read runs. `noAgentEnd`: no agent_end.
|
||||
// `modelNoSystem`: no system in the requests. `skipHookSpelling` ("amPm", "nfd",
|
||||
// "curly" or "nfdCurly"): a read whose path resolves only under that spelling skips
|
||||
// the handlers. `hookSkipFail` (a spelling, or "outside" for the read outside the
|
||||
// project): that read skips the handlers and fails without the file's text.
|
||||
// `readFails`: every read that runs fails. `modelHang`: after the first answer it
|
||||
// hangs, or after answer N when it is a number. `exitCode`: it exits with this code
|
||||
// after agent_end. `agentEndEmpty`: agent_end carries no messages. `noModelCall`:
|
||||
// it fires agent_end and exits 0 with no request.
|
||||
// otherwise a session with no $ARGV_OUT: prints `sessionOut` and exits 0.
|
||||
//
|
||||
// Trust, as pi 0.85.1 has it: the project is trusted with --approve, or with a saved trust and
|
||||
// no --no-approve. Only the project SYSTEM.md and APPEND_SYSTEM.md wait on it here. The user
|
||||
// APPEND_SYSTEM.md is appended whatever the flags, as 0.85.1 does when the project has none.
|
||||
import { FIT_FLAGS } from "../src/pi-fit.mjs";
|
||||
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");
|
||||
@@ -91,7 +137,72 @@ for (let i = 0; i < argv.length; i++) {
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
if (rpc || print) {
|
||||
const modelRun = print && val("--provider") === MODEL_PROVIDER;
|
||||
// The model run's agent loop (see the header).
|
||||
async function agentLoop() {
|
||||
const agentDir = process.env.PI_CODING_AGENT_DIR || path.join(process.env.HOME || "/", ".pi", "agent");
|
||||
const handlers = { tool_call: [], agent_end: [] };
|
||||
const fake = { on(name, h) { (handlers[name] = handlers[name] || []).push(h); }, registerTool() {}, getCommands() { return []; } };
|
||||
for (const f of argv.flatMap((a, i) => a === "-e" ? [argv[i + 1]] : [])) {
|
||||
const m = await import(require("node:url").pathToFileURL(f).href);
|
||||
m.default(fake);
|
||||
}
|
||||
const end = async (text) => {
|
||||
if (!K.noAgentEnd) for (const h of handlers.agent_end) await h({ messages: K.agentEndEmpty ? [] : [{ role: "assistant", content: [{ type: "text", text }], stopReason: "stop" }] });
|
||||
process.stdout.write(text + "\n");
|
||||
process.exit(K.exitCode || 0);
|
||||
};
|
||||
if (K.noModelCall) return end("no call");
|
||||
const models = JSON.parse(fs.readFileSync(path.join(agentDir, "models.json"), "utf8"));
|
||||
const base = models.providers[MODEL_PROVIDER].baseUrl;
|
||||
const sysFile = val("--system-prompt");
|
||||
const system = K.modelNoSystem || !sysFile ? undefined : fs.readFileSync(sysFile, "utf8");
|
||||
const tools = [{ name: "read", description: "read a file", input_schema: { type: "object", properties: { path: { type: "string" } }, required: ["path"] } }];
|
||||
const messages = [{ role: "user", content: argv[argv.length - 1] }];
|
||||
const variants = {
|
||||
amPm: (p) => p.replace(/ (AM|PM)/g, "\u202F$1"),
|
||||
nfd: (p) => p.normalize("NFD"),
|
||||
curly: (p) => p.replace(/'/g, "\u2019"),
|
||||
nfdCurly: (p) => p.replace(/'/g, "\u2019").normalize("NFD"),
|
||||
};
|
||||
const resolveRead = (p) => {
|
||||
const abs = path.resolve(process.cwd(), p);
|
||||
if (fs.existsSync(abs)) return { file: abs, via: null };
|
||||
for (const [name, v] of Object.entries(variants)) if (fs.existsSync(v(abs))) return { file: v(abs), via: name };
|
||||
return { file: abs, via: null };
|
||||
};
|
||||
for (let turn = 0; ; turn++) {
|
||||
const res = await fetch(base + "/v1/messages", { method: "POST", headers: { "content-type": "application/json", "x-api-key": "stand-in" }, body: JSON.stringify({ model: val("--model"), max_tokens: 100, stream: false, ...(system === undefined ? {} : { system }), tools, messages }) });
|
||||
const reply = await res.json();
|
||||
if (K.modelHang === true ? turn === 0 : K.modelHang === turn) { setInterval(() => {}, 1000); return; }
|
||||
const use = (reply.content || []).find((c) => c.type === "tool_use");
|
||||
if (!use) return end((reply.content || []).map((c) => c.text || "").join(""));
|
||||
messages.push({ role: "assistant", content: reply.content });
|
||||
const { file, via } = resolveRead(use.input.path);
|
||||
let blocked = null;
|
||||
let threw = null;
|
||||
const skipFail = K.hookSkipFail !== undefined && (K.hookSkipFail === "outside" ? via === null && use.input.path.includes("outside") : via === K.hookSkipFail);
|
||||
if (!(K.skipHookSpelling && via === K.skipHookSpelling) && !skipFail) {
|
||||
for (const h of handlers.tool_call) {
|
||||
let r;
|
||||
try { r = await h({ toolName: use.name, toolCallId: use.id, input: use.input }); } catch (e) { threw = e.message; break; }
|
||||
if (r && r.block && !blocked) blocked = r.reason || "blocked";
|
||||
}
|
||||
}
|
||||
let result;
|
||||
if (skipFail || (K.readFails && threw === null && !blocked)) result = { type: "tool_result", tool_use_id: use.id, is_error: true, content: "read failed: EACCES" };
|
||||
else if (threw !== null && !K.ignoreThrow) result = { type: "tool_result", tool_use_id: use.id, is_error: true, content: threw };
|
||||
else if (blocked && !K.ignoreBlock) result = { type: "tool_result", tool_use_id: use.id, is_error: true, content: blocked };
|
||||
else {
|
||||
try { result = { type: "tool_result", tool_use_id: use.id, content: fs.readFileSync(file, "utf8") }; }
|
||||
catch (e) { result = { type: "tool_result", tool_use_id: use.id, is_error: true, content: "read failed: " + e.code }; }
|
||||
}
|
||||
messages.push({ role: "user", content: [result] });
|
||||
}
|
||||
}
|
||||
if (modelRun) {
|
||||
agentLoop().catch((e) => { process.stderr.write("stand-in model run: " + e.message + "\n"); process.exit(1); });
|
||||
} else if (rpc || print) {
|
||||
if (K.flood) {
|
||||
const chunk = Buffer.alloc(1024 * 1024, "x");
|
||||
for (let n = 0; n < 8; n++) for (let off = 0; off < chunk.length;) {
|
||||
@@ -173,12 +284,13 @@ if (rpc || print) {
|
||||
for (const s of Object.values(json(f)?.mcpServers || {})) cp.spawnSync(s.command, s.args || [], { stdio: "ignore" });
|
||||
}
|
||||
}
|
||||
if (on("skills", "--no-skills")) {
|
||||
if (on("skills", "--no-skills") || (K.skillLeak && has("--skill") && !ignoreAll)) {
|
||||
const home = process.env.HOME || "/";
|
||||
const dirs = [path.join(agent, "skills"), path.join(cwd, ".pi", "skills"), path.join(cwd, ".agents", "skills"), path.join(home, ".agents", "skills"), ...packages.map((p) => path.join(p, "skills"))];
|
||||
for (const d of dirs) for (const p of list(d, () => true)) commands.push({ name: "skill:" + path.basename(p), source: "skill" });
|
||||
for (const s of settings) for (const p of s.skills || []) commands.push({ name: "skill:" + path.basename(p), source: "skill" });
|
||||
}
|
||||
if (!(K.ignoreSkill && has("--no-skills"))) for (const p of argv.flatMap((a, i) => a === "--skill" ? [argv[i + 1]] : [])) commands.push({ name: "skill:" + path.basename(p), source: "skill" });
|
||||
if (on("prompts", "--no-prompt-templates")) {
|
||||
const dirs = [path.join(agent, "prompts"), path.join(cwd, ".pi", "prompts"), ...packages.map((p) => path.join(p, "prompts"))];
|
||||
for (const d of dirs) for (const p of list(d, (n) => n.endsWith(".md"))) commands.push({ name: path.basename(p, ".md"), source: "prompt" });
|
||||
@@ -220,17 +332,38 @@ if (rpc || print) {
|
||||
}
|
||||
}
|
||||
process.exit(0);
|
||||
} else {
|
||||
if (K.sessionOut) process.stdout.write(K.sessionOut);
|
||||
process.exit(0);
|
||||
}
|
||||
if (K.sessionOut) process.stdout.write(K.sessionOut);
|
||||
process.exit(0);
|
||||
`;
|
||||
|
||||
const quote = (text) => `'${String(text).replace(/'/g, "'\\''")}'`;
|
||||
|
||||
// The script text. node: the interpreter the fit part runs on (absolute, so it runs with no PATH).
|
||||
export function piStandIn(knobs = {}, { node = process.execPath } = {}) {
|
||||
// form "node" (#1873 S4): the same stand-in as one node script with node in its shebang, the kind
|
||||
// of file the conversation controller launches with node; its --version and $ARGV_OUT parts are
|
||||
// JavaScript too, so it needs node wherever it runs.
|
||||
export function piStandIn(knobs = {}, { node = process.execPath, form = "sh" } = {}) {
|
||||
if (form === "node") {
|
||||
const head = knobs.versionHang ? "setTimeout(() => process.exit(0), 60000); return;" : knobs.versionExit != null ? `process.exit(${Number(knobs.versionExit)});` : `process.stdout.write(${JSON.stringify(String(knobs.version ?? "9.9.9"))} + "\\n"); process.exit(0);`;
|
||||
return [
|
||||
`#!${node}`,
|
||||
`"use strict";`,
|
||||
`process.argv.splice(1, 1);`,
|
||||
`(() => {`,
|
||||
`if (process.argv[1] === "--version") { ${head} }`,
|
||||
`if (process.env.ARGV_OUT) { require("node:fs").writeFileSync(process.env.ARGV_OUT, process.argv.slice(1).map((a) => a + "\\n").join("")); process.stdout.write(${JSON.stringify(String(knobs.sessionOut ?? ""))}); process.exit(0); }`,
|
||||
`const K = ${JSON.stringify(knobs)};`,
|
||||
`const FLAGS = ${JSON.stringify(FIT_FLAGS)};`,
|
||||
`const MODEL_PROVIDER = ${JSON.stringify(FIT_MODEL_ROUTE.provider)};`,
|
||||
BODY,
|
||||
`})();`,
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
const version = knobs.versionHang ? "sleep 60; exit 0" : knobs.versionExit != null ? `exit ${Number(knobs.versionExit)}` : `printf '%s\\n' ${quote(knobs.version ?? "9.9.9")}; exit 0`;
|
||||
const program = `"use strict";\nconst K = ${JSON.stringify(knobs)};\nconst FLAGS = ${JSON.stringify(FIT_FLAGS)};\n${BODY}`;
|
||||
const program = `"use strict";\nconst K = ${JSON.stringify(knobs)};\nconst FLAGS = ${JSON.stringify(FIT_FLAGS)};\nconst MODEL_PROVIDER = ${JSON.stringify(FIT_MODEL_ROUTE.provider)};\n${BODY}`;
|
||||
return [
|
||||
"#!/bin/sh",
|
||||
`[ "$1" = --version ] && { ${version}; }`,
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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}`);
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
@@ -36,4 +36,4 @@ Tests can supply `refresh: { mode, timeoutMs }`. Modes are rotate, unchanged, fa
|
||||
|
||||
Limits: store locks are in-process, not a production distributed lock. Credential-store commit and filesystem publication are not crash-atomic together. A post-publication failure records uncertainty, retains the generation and rolls back the in-memory draft; this simulator has no external token issuer to reconcile. A real refresh backend would need a separate reconciliation protocol before reuse. Filesystem ownership does not defend against arbitrary same-UID tampering or establish hardlink provenance.
|
||||
|
||||
Exact Pi 0.85.1 isolation is static-source evidence only. Its model-catalog network flag does NOT suppress OAuth refresh networking. Never substitute real Pi in these tests, even with expired synthetic OAuth tokens. Final whole-increment review is required before publication.
|
||||
The Pi isolation described here was read from the 0.85.1 source (pi is not pinned; #1873) and is static-source evidence only. Its model-catalog network flag does NOT suppress OAuth refresh networking. Never substitute real Pi in these tests, even with expired synthetic OAuth tokens. Final whole-increment review is required before publication.
|
||||
@@ -367,6 +367,167 @@ the kind's whole setting, so it names every harness that stays on:
|
||||
{ "revision": "<from mosaic plugins>", "plugins": { "harness": ["pi", "claude-code"] } }
|
||||
```
|
||||
|
||||
### Harness watch and `inspect` (#1873, slice W1)
|
||||
|
||||
Jason's ruling (2026-11-19): no pins on any harness, a periodic check
|
||||
instead. `src/harness-watch.mjs` is the function that check calls.
|
||||
`watchHarnesses({ last, env, scratch, dataRoot, timeoutMs, signal })` makes one
|
||||
pass over every harness `loadInstalled('harness', { dataRoot })` gives,
|
||||
tree or installed. It holds no name list, so a harness added later is
|
||||
watched at once. It writes nothing: it takes the records the last pass
|
||||
returned and gives back `{ records, transitions, refused, gone }`.
|
||||
|
||||
`inspect({ env, scratch, signal })` is an optional harness export, outside
|
||||
`kinds.mjs`. It returns a promise of:
|
||||
|
||||
| Field | Meaning |
|
||||
|---|---|
|
||||
| `found` | `true` when the binary sessions run was found, `false` when not |
|
||||
| `source` | the lookup step that won, such as `MOSAIC_PI_BIN`, `PATH` or `repo`; never a path |
|
||||
| `version` | the binary's version label, or null |
|
||||
| `digest` | a sha256 hex digest of what runs, or null |
|
||||
| `fit` | `"pass"`, `"fail"` or `"unknown"`: the module's own fit check |
|
||||
| `definite` | for a `"fail"`, true when the check decided; a failure that could not tell is false |
|
||||
| `failed` | rule ids the check names, when it names any |
|
||||
| `line` | one plain line for the owner; it names a variable or a lookup step, never a path |
|
||||
| `surfaces` | name to text, such as `help` |
|
||||
|
||||
`env` is the environment of the process sessions launch from. `scratch` is
|
||||
a directory the module may use for a scratch HOME; it removes what it makes
|
||||
there. `signal` aborts when the watch stops waiting. `pi` returns its fit
|
||||
check (`packages/harness/src/pi-fit.mjs`), called exactly as `preflight`
|
||||
calls it, so the two share one cache and one running check (a test may pass
|
||||
its own check as `check`). Its digest is
|
||||
that check's digest of the binary's files, on a pass and (W2) on a failure
|
||||
too. W2: a failed check carries `undecided`, true when the check could not
|
||||
tell and false when the binary definitely failed. `inspect` reads that
|
||||
field: `undecided: false` is `fit: "fail"`, definite; anything else is
|
||||
`fit: "unknown"`. It never parses the message. The check's runs, and the
|
||||
`--version` capture after a failure, stop when `signal` aborts and only the
|
||||
watch waits on them; a check `preflight` also waits on keeps running.
|
||||
`claude-code` returns the sha256 of the binary's real file and the first
|
||||
line of `--version`, and captures `--help` as the `help` surface, both run
|
||||
from that same real file with only PATH and a scratch HOME. Its lookup for
|
||||
the watch skips a relative PATH entry, as pi's does. Each capture runs in its
|
||||
own process group: a stop, a timeout or the capture's end kills the whole
|
||||
group, so nothing the binary left in the background outlives it, and a signal
|
||||
already aborted runs nothing.
|
||||
|
||||
W2, the claude fit rule: every flag the claude adapter passes appears in
|
||||
`--help`, and `--version` answers. The flags come from the adapter itself:
|
||||
`inspect` runs `adapters/claude/adapter.sh` twice against a recorder that
|
||||
writes its argv (a first turn, which passes `--session-id`, then a second,
|
||||
which passes `--resume`), so the module holds no list of them. The rule
|
||||
reads the text the binary printed. A flag counts only from an option line
|
||||
(one to four spaces, then `-`), read up to the description; a flag a
|
||||
description only mentions does not count. `--x[-y]` anywhere in the text
|
||||
gives `--x-y` when `--x` has its own option line (claude 2.1.322 names
|
||||
`--system-prompt-file` only as `--system-prompt[-file]` in the `--bare`
|
||||
description, beside a `--system-prompt` option line). A missing flag is `fit: "fail"`, definite, with
|
||||
the flag as the rule id; so is `--version` or `--help` exiting non-zero
|
||||
(rule id `--version` or `--help`). A capture that timed out, was stopped,
|
||||
could not start or printed too much, or an adapter run that failed, is
|
||||
`unknown`. The captures run with no network: `unshare -r -n` when a probe
|
||||
of it runs, else without it, as the pi fit check drops unshare's layer when
|
||||
it is missing or refused, and the line then ends "run with the network on:
|
||||
unshare is missing" (or "unshare failed"). A definite verdict is kept in
|
||||
memory by the stat key (`src/stat-key.mjs`) of the binary's real file and
|
||||
the adapter; `preflight` reads it and refuses `harness-feature-missing` on a
|
||||
fail. A replaced binary or adapter has another key, so an old verdict no
|
||||
longer applies. With no verdict, or an undecided one, `preflight` does not
|
||||
refuse in W2. With no verdict it asks for a watch pass at once
|
||||
(`src/watch-request.mjs`); in the bus host that runs a stat pass now, and a
|
||||
full pass when the key changed, so a binary or adapter touched after its
|
||||
check is checked again within one pass. `preflight` stays synchronous and
|
||||
does not wait for that pass: the launch that missed, any launch before the
|
||||
first full pass at host start ends, and any launch while the pass runs go
|
||||
ahead. With no listener (no bus host) the request does nothing.
|
||||
|
||||
`watchKey({ env })` is a second optional export: a string the bus host's
|
||||
timer compares every five minutes (W2). It changes when anything the module
|
||||
would check changes; `claude-code` and `pi` give the stat key of their
|
||||
binary, adapter and module files. A module without it is keyed by the stat
|
||||
key of its `installRoots({ env })`. The key holds paths and is compared in
|
||||
memory only.
|
||||
|
||||
A module with no `inspect` is watched through `version({ env })`, with fit
|
||||
`"unknown"`. A record's `state` is:
|
||||
|
||||
- `fit`: found, the fit check passed, the last known digest (or none known);
|
||||
- `changed`: found, the fit check passed, a digest that is not the last known
|
||||
one. The last known digest is the latest any earlier record saw, kept in
|
||||
each record's `lastDigest` across unknown, red and missing records, so a
|
||||
new binary first seen through a timeout still reads `changed`;
|
||||
- `red`: found, and the fit check failed with `definite: true`;
|
||||
- `unknown`: anything else, including no `inspect`, an `inspect` that throws
|
||||
or runs past `timeoutMs` (default 300 s), and an answer that isn't this
|
||||
shape;
|
||||
- `missing`: `inspect` says `found: false`.
|
||||
|
||||
Only `found: true` with `fit: "pass"` reads as `fit` or `changed`. The watch
|
||||
copies the answer's own data fields only: a field that is a getter, or an
|
||||
answer that throws when read (a Proxy), is a failed `inspect`, and an
|
||||
inherited field is not read. It checks each field's shape and drops what
|
||||
doesn't fit (a `source` that isn't a plain name, a digest that isn't 64 hex
|
||||
characters, a `source` or rule id that is an env value). In every text
|
||||
field it replaces the scratch directory, the home directory, every env value
|
||||
of six characters or more and every PATH entry, each also as its real path
|
||||
and without a trailing slash, with a name for it, the user name (USER,
|
||||
LOGNAME, the process's account) with `<user>`, then any other absolute path
|
||||
of two segments or more with `<path>`, including one after a `word:` (as in
|
||||
`host:/srv/a`), a `file://` URL and a `~/` or `~name/` path. The loader's
|
||||
refusals in `refused`
|
||||
get the same treatment. A record also carries `via` (`inspect` or
|
||||
`version`), `lastDigest`, `checkedAt` and `changedAt`. A transition
|
||||
(`{ harness, at, from, to, failed, line }`, with `from` null on a first
|
||||
sight) is given only when the state or the binary is not the last record's.
|
||||
The digest names the binary; when neither record has one (a module with
|
||||
no digest, or pi's files that can't be read), the version label stands in for it, and only when both
|
||||
records have a version: a timeout or a throw knows none, and is not a new
|
||||
binary. So unknown 2.0, a timeout, then unknown 2.1, all with no digest,
|
||||
gives no transition for the version; W1 keeps no last known version.
|
||||
|
||||
The redaction applies to the stored surfaces too, so a surface is for
|
||||
display and comparison, not for fit rules: with USER set to `model`, the
|
||||
stored `help` reads `--<user>` where claude printed `--model`. A fit rule
|
||||
(slice W2's flag check, for one) runs inside the module on the text the
|
||||
binary printed and puts its answer in `fit`, `failed` and `line`.
|
||||
|
||||
W2: rule ids may start with `-` or `--`, so a flag can be one. The watch
|
||||
takes an optional `signal`; when it aborts, the watch aborts the `inspect`
|
||||
it is in (waiting up to 5 s for it to end), asks no further module and
|
||||
returns `stopped: true` with no records.
|
||||
|
||||
### The bus host's timer (#1873, slice W2)
|
||||
|
||||
`packages/cli/src/harness-watch-host.mjs` runs the watch in the bus host
|
||||
process, with the environment sessions launch from. `mosaic bus start`
|
||||
starts it unless `--no-launch` is given; `startHost({ harnessWatch })` is the
|
||||
switch. It runs one full pass at start, then every five minutes a stat pass
|
||||
that reads each module's `watchKey` (or install roots), and a full pass when
|
||||
that key changed or a day has gone by. While processing is halted or
|
||||
invalid it runs nothing; the first tick after resume runs a full pass. A
|
||||
tick while a pass runs does nothing. A pass that throws is logged with no
|
||||
path and the timer goes on; its key is not kept, so the next tick tries
|
||||
again. Stopping the host clears the timer and aborts the pass running. A
|
||||
request from `src/watch-request.mjs` (preflight's, on a miss) runs a tick at
|
||||
once; a request while a pass runs runs one tick after it, and a request
|
||||
after stop does nothing.
|
||||
|
||||
The host is the one writer of two files under `<org dataRoot>/state/`:
|
||||
|
||||
- `harness-watch.json`: `{ version: 1, checkedAt, harnesses, refused, gone }`,
|
||||
the records of the last full pass without `surfaces`, written to a temp
|
||||
file and renamed over the last one, mode 0600;
|
||||
- `harness-watch.jsonl`: one line per transition, appended only.
|
||||
|
||||
Before either is written, every string in them is checked again for the
|
||||
scratch directory, the home directory, the data root and any env value of
|
||||
six characters or more, from the launch environment and the host's own; a
|
||||
hit writes nothing and logs the pass as failed. Name, code, state, digest
|
||||
and time fields (`harness`, `gone`, `name`, `kept`, `source`, `code`,
|
||||
`state`, `fit`, `via`, the digests and the times) are not checked as text.
|
||||
|
||||
## Transcript usage source (row 124)
|
||||
|
||||
`usage-source/transcript` now ships interface 1's `readTurn({harness, root})`.
|
||||
|
||||
@@ -4,12 +4,16 @@
|
||||
// both stay in packages/harness (a module can't change that boundary). The launcher, the bundle
|
||||
// and the usage reader call these on the host; the runner gets the answers as data in
|
||||
// session.json. packages/plugins/README.md lists the interface.
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { accessSync, constants, realpathSync, statSync } from "node:fs";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { accessSync, constants, createReadStream, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, statSync, writeFileSync } from "node:fs";
|
||||
import { delimiter, dirname, isAbsolute, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { claudeBuiltins, MCP_PREFIX } from "../../../harness/src/gate.mjs";
|
||||
import { hasSessionCredential, SESSION_CREDENTIALS } from "../../../seat/src/session.mjs";
|
||||
import { UNSHARE } from "../../../seat/src/sandbox.mjs";
|
||||
import { statKey } from "../../src/stat-key.mjs";
|
||||
import { requestWatch } from "../../src/watch-request.mjs";
|
||||
|
||||
export const interfaceVersion = 1;
|
||||
|
||||
@@ -60,13 +64,25 @@ export function preflight({ env, proxy = null }) {
|
||||
const names = SESSION_CREDENTIALS.map((set) => set.join(" with ")).join(", ");
|
||||
return { code: "no-session-credential", message: `the bus host has none of ${names}; the bus unit's EnvironmentFile sets one when the bus starts` };
|
||||
}
|
||||
if (!binary(env)) {
|
||||
const bin = binary(env);
|
||||
if (!bin) {
|
||||
return {
|
||||
code: "harness-binary-missing",
|
||||
// Names the variable, never its value, which is a host path (#1873).
|
||||
message: env.MOSAIC_CLAUDE_BIN ? "MOSAIC_CLAUDE_BIN isn't an executable file at an absolute path" : "no claude on the bus host's PATH",
|
||||
};
|
||||
}
|
||||
// #1873 harness watch W2: the fit verdict the last check of this very file gave (inspect below),
|
||||
// found by the stat key of its real file and the adapter's. Only a definite failure refuses. No
|
||||
// verdict, or a check that could not tell, does not refuse in this slice. With no verdict, the
|
||||
// bus host's watch is asked for a stat pass now (src/watch-request.mjs), so a binary replaced,
|
||||
// touched or moved since the last check is checked within one pass, not at the next tick; this
|
||||
// launch is not held for it.
|
||||
const verdict = verdictFor(bin);
|
||||
if (!verdict) requestWatch();
|
||||
if (verdict?.fit === "fail") {
|
||||
return { code: "harness-feature-missing", message: `the claude from ${env.MOSAIC_CLAUDE_BIN ? "MOSAIC_CLAUDE_BIN" : "PATH"} failed the fit check: ${verdict.why}` };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -89,6 +105,196 @@ export function version({ env }) {
|
||||
return r.status === 0 ? r.stdout.trim().split("\n")[0] || null : null;
|
||||
}
|
||||
|
||||
// #1873 harness watch: what the watch records for the binary sessions run: the lookup step that
|
||||
// found it (MOSAIC_CLAUDE_BIN or PATH, never a path), the sha256 of its real file, the first line
|
||||
// of --version and the whole --help as a surface. Both run with an empty environment but PATH and
|
||||
// a fresh HOME under scratch, which is removed after. Without scratch they don't run. The real path
|
||||
// is resolved once, then hashed and run, so the digest and the captures are of one file; a relative
|
||||
// PATH entry is skipped, as pi's lookup skips it.
|
||||
//
|
||||
// W2, the fit rule: every flag the adapter passes is in --help, and --version answers. The flags
|
||||
// come from the adapter itself: adapter.sh runs twice against a stand-in claude that writes its
|
||||
// argv (once fresh, which passes --session-id, and once with a session id kept, which passes
|
||||
// --resume), so there is no second list of them here. --help is read on the text the binary
|
||||
// printed, not on the redacted copy the watch stores. A flag counts only from an option line (one
|
||||
// to four spaces, then -), where it is named before the description: a flag a description only
|
||||
// mentions does not count. --x[-y] anywhere in the text gives --x-y when --x has its own option
|
||||
// line (claude 2.1.322 names --system-prompt-file only as --system-prompt[-file] in the --bare
|
||||
// description, beside a --system-prompt option line). A flag missing from it, or --version or
|
||||
// --help exiting non-zero, is fit "fail", definite, with the flag as the rule id. A capture that
|
||||
// timed out, was stopped, could not start or printed too much, or an adapter run that failed, is
|
||||
// "unknown": the check could not tell. The captures run with no network: unshare -r -n when it
|
||||
// runs here (probed per check), else without it, as the pi fit check drops unshare's layer when it
|
||||
// is missing or refused; the line says so. preflight reads the definite verdicts (verdictFor).
|
||||
const CAPTURE_TIMEOUT = 10000;
|
||||
const CAPTURE_MAX = 1024 * 1024;
|
||||
export async function inspect({ env = {}, scratch = null, signal, unshare = UNSHARE } = {}) {
|
||||
const bin = lookup(env);
|
||||
const source = env.MOSAIC_CLAUDE_BIN ? "MOSAIC_CLAUDE_BIN" : bin ? "PATH" : null;
|
||||
const base = { source, fit: "unknown", definite: false, failed: [], surfaces: {} };
|
||||
if (!bin) return { ...base, found: false, version: null, digest: null, line: env.MOSAIC_CLAUDE_BIN ? "MOSAIC_CLAUDE_BIN isn't an executable file at an absolute path" : "no claude on PATH" };
|
||||
let real;
|
||||
try { real = realpathSync(bin); } catch { return { ...base, found: false, version: null, digest: null, line: `the claude from ${source} can't be resolved` }; }
|
||||
const key = verdictKey(real);
|
||||
const digest = await sha256(real);
|
||||
if (typeof scratch !== "string" || !isAbsolute(scratch)) return { ...base, found: true, version: null, digest, line: "no scratch directory, so --version and --help were not run; fit not checked" };
|
||||
const home = mkdtempSync(join(scratch, "claude-home-"));
|
||||
try {
|
||||
const runEnv = { PATH: "/usr/bin:/bin", HOME: home };
|
||||
const fence = await networkFence(unshare, runEnv, signal);
|
||||
const wrap = (args) => (fence.argv ? [...fence.argv, real, ...args] : [real, ...args]);
|
||||
const v = await capture(wrap(["--version"]), runEnv, signal);
|
||||
const help = await capture(wrap(["--help"]), runEnv, signal);
|
||||
const flags = await adapterFlags(scratch, signal);
|
||||
const first = v.why === null ? v.out.split("\n")[0].trim() : "";
|
||||
const version = first && first.length <= 128 ? first : null;
|
||||
const verdict = judge({ v, first, help, flags });
|
||||
// Kept when it is definite, by the key taken before the check: if the binary or the adapter
|
||||
// changed during it, preflight looks up the new key and does not find this one.
|
||||
if (verdict.definite && key) remember(key, { fit: verdict.fit, failed: verdict.failed, why: verdict.why });
|
||||
const line = fence.argv ? verdict.why : `${verdict.why}; run with the network on: ${fence.why}`;
|
||||
return { ...base, found: true, version, digest, fit: verdict.fit, definite: verdict.definite, failed: verdict.failed, line, surfaces: help.why === null ? { help: help.out } : {} };
|
||||
} finally {
|
||||
rmSync(home, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
// The watch's lookup: MOSAIC_CLAUDE_BIN, else the first claude in an absolute PATH entry.
|
||||
const lookup = (env) => binary({ ...env, PATH: (env.PATH ?? "").split(delimiter).filter((dir) => isAbsolute(dir)).join(delimiter) });
|
||||
|
||||
function judge({ v, first, help, flags }) {
|
||||
const unsure = (why) => ({ fit: "unknown", definite: false, failed: [], why: `${why}; fit not checked` });
|
||||
const fail = (failed, why) => ({ fit: "fail", definite: true, failed, why });
|
||||
if (v.why === "exit" || (v.why === null && !first)) return fail(["--version"], "--version gave no answer");
|
||||
if (v.why !== null) return unsure(`--version ${v.why}`);
|
||||
if (help.why === "exit") return fail(["--help"], "--help exited with an error");
|
||||
if (help.why !== null) return unsure(`--help ${help.why}`);
|
||||
if (!flags) return unsure("the adapter's flags could not be read");
|
||||
const have = helpFlags(help.out);
|
||||
const missing = flags.filter((flag) => !have.has(flag));
|
||||
if (missing.length) return fail(missing, failMessage(missing));
|
||||
return { fit: "pass", definite: true, failed: [], why: "every flag the adapter passes is in --help" };
|
||||
}
|
||||
|
||||
const failMessage = (failed) => (failed.length === 1 && (failed[0] === "--version" || failed[0] === "--help") ? `${failed[0]} gave no answer` : `--help lacks ${failed.join(" ")}`);
|
||||
|
||||
// The flags --help names in its option lines: a line that starts with one to four spaces then -,
|
||||
// read up to the first run of two spaces (the option and its aliases, before the description).
|
||||
// Then --x[-y] anywhere in the text gives --x-y when --x is one of them.
|
||||
const OPTION_LINE = /^ {1,4}-/;
|
||||
const HEAD_FLAG = /(?:^|[\s,])(--?[A-Za-z][A-Za-z0-9-]*)(?=$|[\s,=<[])/g;
|
||||
const BRACKET = /(?<![A-Za-z0-9_-])(--?[A-Za-z][A-Za-z0-9-]*)\[(-[A-Za-z0-9-]+)\]/g;
|
||||
export function helpFlags(text) {
|
||||
const all = String(text ?? "");
|
||||
const out = new Set();
|
||||
for (const line of all.split("\n")) {
|
||||
if (!OPTION_LINE.test(line)) continue;
|
||||
const head = line.trim().split(/\s{2,}/)[0];
|
||||
for (const m of head.matchAll(HEAD_FLAG)) out.add(m[1]);
|
||||
}
|
||||
for (const m of all.matchAll(BRACKET)) if (out.has(m[1])) out.add(m[1] + m[2]);
|
||||
return out;
|
||||
}
|
||||
|
||||
// unshare -r -n, when it runs here: { argv } to put before the binary, else { argv: null, why }.
|
||||
async function networkFence(unshare, runEnv, signal) {
|
||||
if (!unshare || !existsSync(unshare)) return { argv: null, why: "unshare is missing" };
|
||||
const argv = [unshare, "-r", "-n", "--"];
|
||||
const r = await capture([...argv, "/usr/bin/true"], runEnv, signal);
|
||||
return r.why === null ? { argv } : { argv: null, why: "unshare failed" };
|
||||
}
|
||||
|
||||
// The flags adapter.sh passes claude, from its own runs against a stand-in that writes its argv.
|
||||
// Kept per stat key of the adapter and its stream reader; null when a run failed.
|
||||
const FLAG = /^--?[A-Za-z]/;
|
||||
const RECORDER = "#!/bin/sh\nfor a in \"$@\"; do printf '%s\\n' \"$a\"; done > \"$FIT_ARGV_OUT\"\necho '{\"type\":\"result\",\"subtype\":\"success\",\"result\":\"ok\"}'\n";
|
||||
let flagsMemo = null;
|
||||
async function adapterFlags(scratch, signal) {
|
||||
const key = statKey([adapter(), STREAM]);
|
||||
if (flagsMemo?.key === key) return flagsMemo.flags;
|
||||
const dir = mkdtempSync(join(scratch, "claude-flags-"));
|
||||
try {
|
||||
const recorder = join(dir, "claude");
|
||||
const file = join(dir, "input.json");
|
||||
const argvFile = join(dir, "argv");
|
||||
writeFileSync(recorder, RECORDER, { mode: 0o700 });
|
||||
writeFileSync(file, "{}\n");
|
||||
mkdirSync(join(dir, "home"));
|
||||
const runEnv = {
|
||||
PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, HOME: join(dir, "home"), MOSAIC_CLAUDE_BIN: recorder, FIT_ARGV_OUT: argvFile,
|
||||
MOSAIC_SYSTEM_PROMPT_FILE: file, MOSAIC_CLAUDE_SETTINGS: file, MOSAIC_CLAUDE_MCP_CONFIG: file, MOSAIC_REQUEST: "fit check",
|
||||
MOSAIC_WORKSPACE: join(dir, "work"), MOSAIC_SESSION_DIR: join(dir, "session"), MOSAIC_MODEL: "fit-model", MOSAIC_THINKING: "low", MOSAIC_TOOLS: "Read",
|
||||
};
|
||||
const flags = new Set();
|
||||
// The first run keeps the session id it named, so the second passes --resume.
|
||||
for (let round = 0; round < 2; round++) {
|
||||
rmSync(argvFile, { force: true });
|
||||
const r = await capture(["/bin/sh", adapter()], runEnv, signal, dir);
|
||||
if (r.why !== null || !existsSync(argvFile)) return null;
|
||||
const argv = readFileSync(argvFile, "utf8").split("\n").filter(Boolean);
|
||||
if (!argv.length) return null;
|
||||
for (const arg of argv) if (FLAG.test(arg)) flags.add(arg);
|
||||
}
|
||||
const list = [...flags];
|
||||
if (key === statKey([adapter(), STREAM])) flagsMemo = { key, flags: list };
|
||||
return list;
|
||||
} catch {
|
||||
return null;
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
// The verdicts of the last checks, by the stat key of the binary's real file and the adapter's
|
||||
// (src/stat-key.mjs), as the watch compares them. Definite ones only, at most VERDICTS_MAX.
|
||||
const VERDICTS_MAX = 16;
|
||||
const verdicts = new Map();
|
||||
const verdictKey = (real) => statKey([real, adapter(), STREAM]);
|
||||
function remember(key, verdict) {
|
||||
verdicts.delete(key);
|
||||
verdicts.set(key, Object.freeze({ ...verdict, failed: Object.freeze([...verdict.failed]) }));
|
||||
while (verdicts.size > VERDICTS_MAX) verdicts.delete(verdicts.keys().next().value);
|
||||
}
|
||||
function verdictFor(bin) {
|
||||
try { return verdicts.get(verdictKey(realpathSync(bin))) ?? null; } catch { return null; }
|
||||
}
|
||||
|
||||
function sha256(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const h = createHash("sha256");
|
||||
createReadStream(file).on("error", reject).on("data", (c) => h.update(c)).on("end", () => resolve(h.digest("hex")));
|
||||
});
|
||||
}
|
||||
|
||||
// Runs argv in its own process group and returns { out, why }: why is null when it exited 0 within
|
||||
// CAPTURE_TIMEOUT and printed at most CAPTURE_MAX, else "exit" (it exited non-zero: an answer),
|
||||
// "timed out", "was stopped", "could not start" or "printed too much" (no answer). A timeout, a
|
||||
// stop or too much output kills the whole group, and so does its end: nothing it left in the
|
||||
// background outlives the capture. An already aborted signal starts nothing.
|
||||
function capture(argv, runEnv, signal, cwd = runEnv.HOME) {
|
||||
return new Promise((resolve) => {
|
||||
if (signal?.aborted) { resolve({ out: "", why: "was stopped" }); return; }
|
||||
let child;
|
||||
try { child = spawn(argv[0], argv.slice(1), { env: runEnv, cwd, stdio: ["ignore", "pipe", "ignore"], detached: true }); } catch { resolve({ out: "", why: "could not start" }); return; }
|
||||
const out = [];
|
||||
let size = 0, done = false;
|
||||
const finish = (why) => { if (done) return; done = true; clearTimeout(timer); signal?.removeEventListener?.("abort", stopped); resolve({ out: why === null ? Buffer.concat(out).toString("utf8") : "", why }); };
|
||||
const kill = (why) => {
|
||||
try { process.kill(-child.pid, "SIGKILL"); } catch { try { child.kill("SIGKILL"); } catch {} }
|
||||
finish(why);
|
||||
};
|
||||
const stopped = () => kill("was stopped");
|
||||
const timer = setTimeout(() => kill("timed out"), CAPTURE_TIMEOUT);
|
||||
signal?.addEventListener?.("abort", stopped, { once: true });
|
||||
child.stdout.on("data", (c) => { size += c.length; if (size > CAPTURE_MAX) kill("printed too much"); else out.push(c); });
|
||||
child.on("error", () => finish("could not start"));
|
||||
child.on("close", (status) => {
|
||||
try { process.kill(-child.pid, "SIGKILL"); } catch {}
|
||||
finish(status === 0 ? null : "exit");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Its launches spend the business's launch.account (row 126).
|
||||
export const countsAccount = () => true;
|
||||
|
||||
@@ -155,6 +361,15 @@ export function installRoots({ env = {} } = {}) {
|
||||
return roots;
|
||||
}
|
||||
|
||||
// #1873 harness watch W2: the stat key the host's 5-minute pass compares (src/stat-key.mjs): the
|
||||
// binary the watch's lookup finds and its directory, the adapter and its stream reader (the fit
|
||||
// rule reads their flags) and the unslop hook. No binary found is a key too. The key holds paths
|
||||
// and stays in memory.
|
||||
export function watchKey({ env = {} } = {}) {
|
||||
const bin = lookup(env);
|
||||
return statKey([bin, bin ? dirname(bin) : null, adapter(), STREAM, join(REPO, "adapters", "claude"), join(REPO, "tools", "unslop-hook")]);
|
||||
}
|
||||
|
||||
// A turn ends when claude exits; there is no marker.
|
||||
export const turnEnd = () => "exit";
|
||||
export const transcript = ({ runDir, store = null }) => (store ? store.projects : join(runDir, "home", ".claude", "projects"));
|
||||
|
||||
@@ -119,8 +119,10 @@ const cleanLabel = (s) => s.length > 0 && s.length <= VERSION_MAX && !/[\u0000-\
|
||||
// The first line `<bin> --version` prints, or null when it fails, times out or prints something
|
||||
// that is not one short printable line. The environment holds only a PATH that finds node. A
|
||||
// promise: the binary runs as a child process, so the caller's event loop keeps running.
|
||||
export function printedVersion(bin) {
|
||||
if (!bin) return Promise.resolve(null);
|
||||
// #1873 harness watch W2: it runs in its own process group, and a timeout, too much output or the
|
||||
// optional signal aborting kills the whole group, so no child it started outlives it.
|
||||
export function printedVersion(bin, { signal = null } = {}) {
|
||||
if (!bin || signal?.aborted) return Promise.resolve(null);
|
||||
return new Promise((resolve) => {
|
||||
const out = [];
|
||||
let size = 0;
|
||||
@@ -131,18 +133,25 @@ export function printedVersion(bin) {
|
||||
if (done) return;
|
||||
done = true;
|
||||
clearTimeout(timer);
|
||||
signal?.removeEventListener?.("abort", stop);
|
||||
resolve(line);
|
||||
};
|
||||
const kill = () => {
|
||||
try { process.kill(-child.pid, "SIGKILL"); } catch { try { child.kill("SIGKILL"); } catch {} }
|
||||
finish(null);
|
||||
};
|
||||
const stop = () => kill();
|
||||
try {
|
||||
child = spawn(bin, ["--version"], { env: { PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, PI_OFFLINE: "1" }, stdio: ["ignore", "pipe", "ignore"] });
|
||||
child = spawn(bin, ["--version"], { env: { PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, PI_OFFLINE: "1" }, stdio: ["ignore", "pipe", "ignore"], detached: true });
|
||||
} catch {
|
||||
finish(null);
|
||||
return;
|
||||
}
|
||||
timer = setTimeout(() => { try { child.kill("SIGKILL"); } catch {} finish(null); }, 5000);
|
||||
timer = setTimeout(kill, 5000);
|
||||
signal?.addEventListener?.("abort", stop, { once: true });
|
||||
child.stdout.on("data", (chunk) => {
|
||||
size += chunk.length;
|
||||
if (size > VERSION_OUTPUT_MAX) { try { child.kill("SIGKILL"); } catch {} finish(null); } else out.push(chunk);
|
||||
if (size > VERSION_OUTPUT_MAX) kill(); else out.push(chunk);
|
||||
});
|
||||
child.on("error", () => finish(null));
|
||||
child.on("close", (status) => {
|
||||
|
||||
@@ -8,6 +8,7 @@ import { dirname, join } from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { locate, printedVersion } from "./binary.mjs";
|
||||
import { statKey } from "../../src/stat-key.mjs";
|
||||
import { fitVerdict, piFit } from "../../../harness/src/pi-fit.mjs";
|
||||
import { envNames, harnessEnv } from "./session-env.mjs";
|
||||
|
||||
@@ -55,6 +56,27 @@ export async function version({ env = {} } = {}) {
|
||||
return printedVersion(locate(env).bin);
|
||||
}
|
||||
|
||||
// #1873 harness watch W1: what the watch records for the pi binary sessions run. It calls the fit
|
||||
// check exactly as preflight does (same found binary, same levels, this process's environment), so
|
||||
// it shares preflight's cache and its running check: a binary that passed is not checked again.
|
||||
// digest is the fit check's digest of the binary's files, on a pass and (W2) on a failure too, null
|
||||
// only when the files can't be read. A failure pi-fit marks undecided could not decide, so its fit
|
||||
// is "unknown"; one marked undecided: false is definite. A failure without the field reads as
|
||||
// undecided, so it can never read as definite. The message is shown, never parsed. source names
|
||||
// the lookup step, never a path. scratch is not used: the fit check makes its own scratch directories.
|
||||
// signal goes to the fit check (W2): when the watch stops, a check only it waits on is stopped.
|
||||
const SOURCES = Object.freeze({ MOSAIC_PI_BIN: "MOSAIC_PI_BIN", "the pi on PATH": "PATH", "the repository's pi": "repo" });
|
||||
// check: the fit check to run, piFit unless a test gives a stand-in for it.
|
||||
export async function inspect({ env = {}, signal = null, check = piFit } = {}) {
|
||||
const at = locate(env);
|
||||
if (!at.bin) return { found: false, source: env.MOSAIC_PI_BIN ? "MOSAIC_PI_BIN" : null, version: null, digest: null, fit: "unknown", definite: false, failed: [], line: at.problem, surfaces: {} };
|
||||
const source = SOURCES[at.label] ?? null;
|
||||
const fit = await check(at, { levels: LEVELS, signal });
|
||||
if (fit.ok) return { found: true, source, version: fit.version ?? null, digest: fit.digest, fit: "pass", definite: true, failed: [], line: `pi${fit.version ? ` ${fit.version}` : ""} (${at.label}) passed the fit check`, surfaces: {} };
|
||||
const undecided = fit.undecided !== false;
|
||||
return { found: true, source, version: await printedVersion(at.bin, { signal }), digest: typeof fit.digest === "string" ? fit.digest : null, fit: undecided ? "unknown" : "fail", definite: !undecided, failed: [], line: fit.message, surfaces: {} };
|
||||
}
|
||||
|
||||
// #1873 S3: runs the fit check, or shares the one running, and throws its refusal line when the
|
||||
// binary fails it. A host that calls env() without preflight (Triage) awaits this first.
|
||||
export async function ready({ env = process.env } = {}) {
|
||||
@@ -154,6 +176,14 @@ export function env({ policy, tools, session, proxy = null, env: hostEnv = proce
|
||||
export { envNames };
|
||||
export const installRoots = ({ env = {} } = {}) => [join(REPO, "adapters", "pi"), join(REPO, "packages", "plugins", "harness", "pi"), found(env).root, join(REPO, "tools", "unslop-hook")];
|
||||
|
||||
// #1873 harness watch W2: the stat key the host's 5-minute pass compares (src/stat-key.mjs): the
|
||||
// binary the lookup finds and the install roots. No binary found is a key too, so finding one
|
||||
// later changes it. The key holds paths and stays in memory.
|
||||
export function watchKey({ env = {} } = {}) {
|
||||
const at = locate(env);
|
||||
return statKey([at.bin ?? null, join(REPO, "adapters", "pi"), join(REPO, "packages", "plugins", "harness", "pi"), at.root ?? null, join(REPO, "tools", "unslop-hook")]);
|
||||
}
|
||||
|
||||
// The extension writes the turn marker at agent_end; an exit without it is a failed turn.
|
||||
export const turnEnd = () => "marker";
|
||||
export const transcript = ({ runDir, store = null }) => (store ? store.session : join(runDir, "session"));
|
||||
|
||||
@@ -32,3 +32,14 @@ export const envNames = Object.freeze(["MOSAIC_PI_BIN", "MOSAIC_PI_NO_MCP", "PI_
|
||||
// no Anthropic provider (pi-fit.test.mjs holds that); glm-5.3-flash is the model index.mjs has
|
||||
// documented limits for, and pi's own catalogue lists it under zai.
|
||||
export const FIT_ROUTE = Object.freeze({ provider: "zai", model: "glm-5.3-flash" });
|
||||
|
||||
// #1873 pi-unpin S6: the provider and model the fit check's scripted-model run names. Its models.json
|
||||
// points that provider at a stand-in on the sandbox's own loopback (packages/harness/src/
|
||||
// pi-fit-model.mjs), so the names are plain stand-ins: no real provider, and no Claude model, which
|
||||
// runs only on the claude-code harness (pi-fit.test.mjs holds that).
|
||||
export const FIT_MODEL_ROUTE = Object.freeze({ provider: "fit-model", model: "fit-model" });
|
||||
|
||||
// #1873 pi-unpin S6: the gate policy the scripted-model run's -e extension (packages/harness/src/
|
||||
// pi-extension.mjs) reads: a pi session with the read tool, no typed tools, in workspace. Built here
|
||||
// since the policy names this harness.
|
||||
export const fitPolicy = (workspace) => ({ harness: "pi", workspace, tools: ["read"], typed: [] });
|
||||
@@ -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 } : {}) };
|
||||
|
||||
@@ -0,0 +1,353 @@
|
||||
// #1873 harness watch, slice W1 (reports/harness-watch-design.md sections 1 and 5): one pass over
|
||||
// every harness the loader gives, tree or installed. No name list: a harness added later is
|
||||
// watched at once. For each module it calls the optional inspect({ env, scratch, signal }), or
|
||||
// falls back to version({ env }) with fit "unknown". It compares the answer with the last record
|
||||
// the caller passes in and returns the new records and the transitions. It writes nothing: no
|
||||
// state file, no log, no timer. The caller (W2's host timer) keeps the records.
|
||||
//
|
||||
// A record's state:
|
||||
// fit found, the module's fit check passed, and the digest is the last known one (or there
|
||||
// is no known digest to compare with)
|
||||
// changed found, the fit check passed, and the digest differs from the last known one: the
|
||||
// latest digest any earlier record saw, kept in lastDigest across unknown, red and
|
||||
// missing records, so a new binary first seen through a timeout still reads changed
|
||||
// red found, and the fit check failed with a definite answer
|
||||
// unknown anything else: the check could not decide, the module has no inspect, inspect threw,
|
||||
// timed out or gave an answer that isn't the contract's
|
||||
// missing inspect says the binary is not found
|
||||
// Only fit === "pass" with found === true reads as fit or changed. Nothing else can.
|
||||
//
|
||||
// No record field holds a host path or an env value: source must be a lookup step's name
|
||||
// (MOSAIC_PI_BIN, PATH, repo), never a path, and every text field has the scratch directory,
|
||||
// the home directory, each env value and each PATH entry (and their real paths) replaced by a
|
||||
// name for it, then every other absolute path replaced by <path>.
|
||||
import { closeSync, constants, fstatSync, openSync, readSync, realpathSync } from "node:fs";
|
||||
import { homedir, userInfo } from "node:os";
|
||||
import { isAbsolute, join } from "node:path";
|
||||
import { loadInstalled } from "./loader.mjs";
|
||||
|
||||
export const STATES = Object.freeze(["fit", "changed", "red", "unknown", "missing"]);
|
||||
const FITS = new Set(["pass", "fail", "unknown"]);
|
||||
// An inspect that takes longer than this gives unknown. pi's fit check runs many sandboxed starts.
|
||||
export const INSPECT_TIMEOUT_MS = 300000;
|
||||
const SOURCE = /^[A-Za-z][A-Za-z0-9_]{0,63}$/;
|
||||
// A rule id may be a flag (W2: claude-code's fit rule names the flag --help lacks).
|
||||
const RULE = /^(?:--?)?[A-Za-z0-9][A-Za-z0-9_.:-]{0,63}$/;
|
||||
const SURFACE = /^[a-z][a-z0-9-]{0,31}$/;
|
||||
const DIGEST = /^[a-f0-9]{64}$/;
|
||||
const LABEL_MAX = 128;
|
||||
const LINE_MAX = 500;
|
||||
const SURFACE_MAX = 256 * 1024;
|
||||
const SURFACES_MAX = 16;
|
||||
// An env value shorter than this is too common a word to mean anything ("1", "true", "C").
|
||||
const VALUE_MIN = 6;
|
||||
const CONTROL = /[\u0000-\u0008\u000b-\u001f\u007f-\u009f\u2028\u2029]/g;
|
||||
|
||||
// An absolute path of two segments or more that no name replaced. Not one that follows a word,
|
||||
// a slash, a dot, a dash, a tilde or a name's closing ">" (https://x/y, ~/x, <home>/x, a/b/c); one
|
||||
// after a colon is a path (host:/srv/a/b). W2 review P3-5: a file: URL and a ~/ path are paths too.
|
||||
const LOOSE_PATH = /(?<![\w/.~>-])\/[^\s/'"`<>()[\]{},;]+(?:\/[^\s'"`<>()[\]{},;]*)+/g;
|
||||
const FILE_URL = /(?<![A-Za-z0-9_])file:\/\/[^\s'"`<>()[\]{},;]*/gi;
|
||||
const TILDE_PATH = /(?<![\w/.~-])~[A-Za-z0-9._-]*\/[^\s'"`<>()[\]{},;]*/g;
|
||||
|
||||
// One pass. last: the records a previous pass returned (an array; others are ignored). env: the
|
||||
// environment of the process sessions launch from, which the modules look up their binary in.
|
||||
// scratch: a directory the modules may use for a scratch HOME. Returns { records, transitions,
|
||||
// refused, gone }: one record per harness the loader gives, a transition for each harness whose
|
||||
// state or digest is not the last record's (or that has no last record), the loader's refusals
|
||||
// as it reports them, and the names of last records whose harness the loader no longer gives.
|
||||
// signal (W2): when it aborts, the inspect running is aborted too (its own signal) and no further
|
||||
// module is asked; the result then carries stopped: true and holds only the records made before.
|
||||
export async function watchHarnesses({ last = [], env = process.env, scratch = null, dataRoot = null, timeoutMs = INSPECT_TIMEOUT_MS, now = () => new Date(), signal = null } = {}) {
|
||||
const before = new Map();
|
||||
for (const r of Array.isArray(last) ? last : []) if (r && typeof r.harness === "string") before.set(r.harness, r);
|
||||
const loaded = await loadInstalled("harness", { dataRoot });
|
||||
const redact = redactor(env, scratch);
|
||||
// The loader's refusals name the module's directory, a host path, so their text is redacted too.
|
||||
const refused = loaded.refused.map((r) => ({ ...r, message: typeof r.message === "string" ? redact(r.message) : null }));
|
||||
const modules = loaded.modules;
|
||||
const records = [];
|
||||
const transitions = [];
|
||||
for (const { name, module } of modules) {
|
||||
if (signal?.aborted) break;
|
||||
const answer = await ask(module, { env, scratch, timeoutMs, signal });
|
||||
if (signal?.aborted) break;
|
||||
const at = now().toISOString();
|
||||
const prior = before.get(name) ?? null;
|
||||
const record = { harness: name, ...settle(clean(answer, redact, env), prior), checkedAt: at };
|
||||
record.lastDigest = record.digest ?? known(prior);
|
||||
const moved = !prior || prior.state !== record.state || !sameBinary(prior, record);
|
||||
record.changedAt = moved ? at : (typeof prior.changedAt === "string" ? prior.changedAt : at);
|
||||
records.push(record);
|
||||
if (moved) transitions.push({ harness: name, at, from: prior ? brief(prior) : null, to: brief(record), failed: record.failed, line: record.line });
|
||||
}
|
||||
const seen = new Set(records.map((r) => r.harness));
|
||||
const stopped = Boolean(signal?.aborted);
|
||||
return { records, transitions, refused, gone: stopped ? [] : [...before.keys()].filter((n) => !seen.has(n)).sort(), ...(stopped ? { stopped } : {}) };
|
||||
}
|
||||
|
||||
// The latest digest the last record knew: its own, or the one it carried forward.
|
||||
const known = (prior) => [prior?.digest, prior?.lastDigest].find((d) => typeof d === "string" && DIGEST.test(d)) ?? null;
|
||||
// Whether two records are of the same binary. The digest decides; when neither record has one
|
||||
// (pi gives none for a failed check), the version label stands in for it, but only when both
|
||||
// records have one: a pass that timed out or threw knows no version, which is not a new binary.
|
||||
function sameBinary(prior, record) {
|
||||
const was = typeof prior.digest === "string" ? prior.digest : null;
|
||||
if (was !== null || record.digest !== null) return was === record.digest;
|
||||
const before = typeof prior.version === "string" ? prior.version : null;
|
||||
return before === null || record.version === null || before === record.version;
|
||||
}
|
||||
|
||||
const brief = (r) => ({ state: STATES.includes(r.state) ? r.state : "unknown", version: typeof r.version === "string" ? r.version : null, digest: typeof r.digest === "string" ? r.digest : null });
|
||||
|
||||
// The module's answer, or { error } when it threw or ran past timeoutMs. via says which export.
|
||||
async function ask(module, { env, scratch, timeoutMs, signal = null }) {
|
||||
const via = typeof module.inspect === "function" ? "inspect" : "version";
|
||||
const stop = new AbortController();
|
||||
const asked = (async () => {
|
||||
try {
|
||||
// The answer is copied here, inside the try, to plain own data: a getter, a Proxy or an
|
||||
// inherited field that throws or lies is a failed inspect, not a failed pass.
|
||||
if (via === "inspect") return { via, answer: plain(await module.inspect({ env, scratch, signal: stop.signal })) };
|
||||
const version = await module.version({ env });
|
||||
return { via, version: typeof version === "string" ? version : null };
|
||||
} catch {
|
||||
// The error's text can name a host path, so the record keeps none of it.
|
||||
return { via, error: "threw" };
|
||||
}
|
||||
})();
|
||||
let timer = null, grace = null, cut = null;
|
||||
const late = new Promise((resolve) => {
|
||||
timer = setTimeout(() => { stop.abort(); resolve({ via, error: "timeout" }); }, timeoutMs);
|
||||
// A stop from the caller aborts the inspect, then waits for it to end (what it started is
|
||||
// stopped by then), up to STOP_GRACE_MS for one that ignores its signal.
|
||||
cut = () => {
|
||||
stop.abort();
|
||||
clearTimeout(timer);
|
||||
Promise.race([asked, new Promise((r) => { grace = setTimeout(r, STOP_GRACE_MS); })]).then(() => resolve({ via, error: "stopped" }));
|
||||
};
|
||||
if (signal?.aborted) cut();
|
||||
else signal?.addEventListener?.("abort", cut, { once: true });
|
||||
});
|
||||
try {
|
||||
return signal?.aborted ? await late : await Promise.race([asked, late]);
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
clearTimeout(grace);
|
||||
signal?.removeEventListener?.("abort", cut);
|
||||
}
|
||||
}
|
||||
const STOP_GRACE_MS = 5000;
|
||||
|
||||
// The contract's fields of an answer as own data properties, copied to a plain object whose
|
||||
// values are strings, booleans, null, an array of strings or a map of strings. A getter is not
|
||||
// called (a field that is one makes the answer a failed inspect), an inherited field is not read,
|
||||
// and failed and surfaces are cut before they are read.
|
||||
const FIELDS = ["found", "source", "version", "digest", "fit", "definite", "line"];
|
||||
function plain(answer) {
|
||||
if (!answer || typeof answer !== "object") return null;
|
||||
const own = (o, k) => {
|
||||
const d = Object.getOwnPropertyDescriptor(o, k);
|
||||
if (d && !("value" in d)) throw new Error("an accessor field");
|
||||
return d ? d.value : undefined;
|
||||
};
|
||||
const out = {};
|
||||
for (const k of FIELDS) {
|
||||
const v = own(answer, k);
|
||||
if (typeof v === "string" || typeof v === "boolean") out[k] = v;
|
||||
}
|
||||
const failed = own(answer, "failed");
|
||||
if (Array.isArray(failed)) {
|
||||
out.failed = [];
|
||||
for (let i = 0; i < Math.min(failed.length, 32); i++) {
|
||||
const id = own(failed, String(i));
|
||||
if (typeof id === "string") out.failed.push(id);
|
||||
}
|
||||
}
|
||||
const surfaces = own(answer, "surfaces");
|
||||
if (surfaces && typeof surfaces === "object" && !Array.isArray(surfaces)) {
|
||||
out.surfaces = {};
|
||||
for (const k of Object.keys(surfaces).slice(0, SURFACES_MAX)) {
|
||||
const v = own(surfaces, k);
|
||||
if (typeof v === "string") out.surfaces[k] = v.slice(0, SURFACE_MAX);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// The contract's fields from an answer, each checked and redacted. Anything else is dropped. A
|
||||
// source or rule id that is an env value is dropped too: they are not text, so not redacted.
|
||||
function clean({ via, answer, version, error }, redact, env) {
|
||||
const base = { via, found: null, source: null, version: null, digest: null, fit: "unknown", definite: false, failed: [], line: null, surfaces: {} };
|
||||
if (error === "timeout") return { ...base, line: `${via} did not answer in time` };
|
||||
if (error) return { ...base, line: `${via} failed` };
|
||||
if (via === "version") {
|
||||
const label = text(version, LABEL_MAX, redact);
|
||||
return { ...base, version: label, line: label ? "no inspect; fit not checked" : "no inspect, and version gave no answer" };
|
||||
}
|
||||
if (!answer || typeof answer !== "object") return { ...base, line: "inspect gave no answer" };
|
||||
const values = envValues(env);
|
||||
const found = answer.found === true ? true : answer.found === false ? false : null;
|
||||
const fit = FITS.has(answer.fit) ? answer.fit : "unknown";
|
||||
const surfaces = {};
|
||||
if (answer.surfaces && typeof answer.surfaces === "object") {
|
||||
for (const [k, v] of Object.entries(answer.surfaces)) if (SURFACE.test(k)) surfaces[k] = redact(v);
|
||||
}
|
||||
return {
|
||||
...base,
|
||||
found,
|
||||
source: typeof answer.source === "string" && SOURCE.test(answer.source) && !values.has(answer.source) ? answer.source : null,
|
||||
version: text(answer.version, LABEL_MAX, redact),
|
||||
digest: typeof answer.digest === "string" && DIGEST.test(answer.digest) ? answer.digest : null,
|
||||
fit,
|
||||
// A pass is an answer, so it is definite. A failure is definite only when the module says so
|
||||
// (otherwise the check could not decide), and an unknown never is.
|
||||
definite: fit === "pass" || (fit === "fail" && answer.definite === true),
|
||||
failed: Array.isArray(answer.failed) ? [...new Set(answer.failed.filter((id) => RULE.test(id) && !values.has(id)))] : [],
|
||||
line: text(answer.line, LINE_MAX, redact),
|
||||
surfaces,
|
||||
};
|
||||
}
|
||||
|
||||
// The state for a cleaned answer and the last record.
|
||||
function settle(r, prior) {
|
||||
let state = "unknown";
|
||||
if (r.via === "inspect" && r.found === false) state = "missing";
|
||||
else if (r.via === "inspect" && r.found === true && r.fit === "pass") {
|
||||
const was = known(prior);
|
||||
state = was !== null && r.digest !== null && was !== r.digest ? "changed" : "fit";
|
||||
} else if (r.via === "inspect" && r.found === true && r.fit === "fail" && r.definite) state = "red";
|
||||
return { state, ...r };
|
||||
}
|
||||
|
||||
// One printable line, cut to max, redacted; or null.
|
||||
function text(v, max, redact) {
|
||||
if (typeof v !== "string") return null;
|
||||
const line = redact(v.split("\n")[0].replace(CONTROL, " ").trim()).slice(0, max);
|
||||
return line || null;
|
||||
}
|
||||
|
||||
// Every nonempty string value in env and in this process's environment.
|
||||
function envValues(env) {
|
||||
const out = new Set();
|
||||
for (const source of [env ?? {}, process.env]) for (const v of Object.values(source)) if (typeof v === "string" && v) out.add(v);
|
||||
return out;
|
||||
}
|
||||
|
||||
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
|
||||
// Replaces every host path and env value a record could pick up with a name for it: the scratch
|
||||
// directory, the home directory, each env value of VALUE_MIN characters or more from env and this
|
||||
// process's own environment, and each absolute PATH entry, each also as its real path and without
|
||||
// a trailing slash. Longest first, so a path inside another is not left half replaced. The user
|
||||
// name (this process's account, USER and LOGNAME), short as it may be, is replaced as a whole
|
||||
// word. Last, any absolute path left is <path>.
|
||||
function redactor(env, scratch, extra = []) {
|
||||
const pairs = new Map();
|
||||
const put = (value, name) => { if (value.length >= VALUE_MIN && !pairs.has(value)) pairs.set(value, name); };
|
||||
const add = (value, name) => {
|
||||
if (typeof value !== "string") return;
|
||||
put(value, name);
|
||||
if (!value.startsWith("/")) return;
|
||||
const bare = value.replace(/\/+$/, "");
|
||||
if (bare) put(bare, name);
|
||||
try { put(realpathSync(bare || "/"), name); } catch {}
|
||||
};
|
||||
if (typeof scratch === "string" && scratch) add(scratch, "<scratch>");
|
||||
for (const p of extra) if (typeof p === "string" && p) add(p, "<path>");
|
||||
for (const source of [env ?? {}, process.env]) {
|
||||
for (const [k, v] of Object.entries(source)) {
|
||||
if (typeof v !== "string") continue;
|
||||
add(v, `<${k}>`);
|
||||
if (k === "PATH") for (const dir of v.split(":")) if (isAbsolute(dir) && dir !== "/") add(dir, "<PATH entry>");
|
||||
}
|
||||
}
|
||||
try { add(homedir(), "<home>"); } catch {}
|
||||
const users = new Set();
|
||||
try { users.add(userInfo().username); } catch {}
|
||||
for (const source of [env ?? {}, process.env]) for (const k of ["USER", "LOGNAME"]) if (typeof source[k] === "string") users.add(source[k]);
|
||||
const words = [...users].filter((u) => typeof u === "string" && u.length >= 2 && /^[A-Za-z0-9._-]+$/.test(u));
|
||||
const word = words.length ? new RegExp(`(?<![A-Za-z0-9_])(?:${words.sort((a, b) => b.length - a.length).map(escape).join("|")})(?![A-Za-z0-9_])`, "g") : null;
|
||||
const order = [...pairs.keys()].sort((a, b) => b.length - a.length);
|
||||
return (s) => {
|
||||
let out = s;
|
||||
for (const value of order) if (out.includes(value)) out = out.split(value).join(pairs.get(value));
|
||||
if (word) out = out.replace(word, "<user>");
|
||||
return out.replace(FILE_URL, "<path>").replace(TILDE_PATH, "<path>").replace(LOOSE_PATH, "<path>");
|
||||
};
|
||||
}
|
||||
|
||||
// #1873 harness watch, slice W3 (reports/harness-watch-design.md section 4, "What the owner sees"):
|
||||
// the bus's plugins answer reads what the host's watch last wrote. The host (packages/cli/src/
|
||||
// harness-watch-host.mjs) is the one writer of this file; this reads it and never writes.
|
||||
export const watchStateFile = (dataRoot) => join(dataRoot, "state", "harness-watch.json");
|
||||
export const NOT_CHECKED = "not checked yet";
|
||||
const STATE_VERSION = 1;
|
||||
const STATE_MAX = 1024 * 1024;
|
||||
const NAME = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
||||
const ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?Z$/;
|
||||
const FAILED_MAX = 32;
|
||||
// At most this many entries, and records read: a file the host wrote holds one per harness module.
|
||||
const ENTRIES_MAX = 64;
|
||||
const RECORDS_MAX = 256;
|
||||
|
||||
// One entry per harness: each name in names (the harnesses the loader gives, in its order), then
|
||||
// each other harness the file holds a record for. An entry is { name, state, version, checkedAt,
|
||||
// changedAt, failed, line }. state is one of STATES, or "unchecked" with line NOT_CHECKED when the
|
||||
// file is missing, unreadable, larger than STATE_MAX, not JSON or not version 1's shape, or holds no
|
||||
// usable record for that harness. Never throws. The text fields are redacted again here, with this
|
||||
// process's environment, env and the paths given (the data roots): the file's writer redacted them
|
||||
// once, and a file someone else wrote is not trusted to have. A name the loader does not give that is
|
||||
// an env value is dropped. At most ENTRIES_MAX entries, from the first RECORDS_MAX records.
|
||||
export function readWatchView({ dataRoot, names = [], env = process.env, paths = [] } = {}) {
|
||||
const records = readState(dataRoot).slice(0, RECORDS_MAX);
|
||||
const order = new Set();
|
||||
for (const n of Array.isArray(names) ? names : []) if (typeof n === "string" && NAME.test(n) && order.size < ENTRIES_MAX) order.add(n);
|
||||
const values = envValues(env);
|
||||
const byName = new Map();
|
||||
for (const r of records) {
|
||||
if (!r || typeof r !== "object" || Array.isArray(r) || typeof r.harness !== "string" || !NAME.test(r.harness) || byName.has(r.harness)) continue;
|
||||
if (!order.has(r.harness) && (values.has(r.harness) || order.size >= ENTRIES_MAX)) continue;
|
||||
byName.set(r.harness, r);
|
||||
order.add(r.harness);
|
||||
}
|
||||
let redact = null;
|
||||
const view = (name) => {
|
||||
const r = byName.get(name);
|
||||
if (!r || !STATES.includes(r.state)) return unchecked(name);
|
||||
redact ??= redactor(env, null, [dataRoot, ...paths]);
|
||||
const failed = Array.isArray(r.failed) ? [...new Set(r.failed.slice(0, FAILED_MAX).filter((id) => typeof id === "string" && RULE.test(id) && !values.has(id)))] : [];
|
||||
return { name, state: r.state, version: text(r.version, LABEL_MAX, redact), checkedAt: when(r.checkedAt), changedAt: when(r.changedAt), failed, line: text(r.line, LINE_MAX, redact) };
|
||||
};
|
||||
return [...order].map(view);
|
||||
}
|
||||
|
||||
const unchecked = (name) => ({ name, state: "unchecked", version: null, checkedAt: null, changedAt: null, failed: [], line: NOT_CHECKED });
|
||||
// A time is kept only when it is a real one: 2026-02-30 is not.
|
||||
const when = (v) => {
|
||||
if (typeof v !== "string" || !ISO.test(v)) return null;
|
||||
const t = Date.parse(v);
|
||||
return Number.isFinite(t) && new Date(t).toISOString().slice(0, 19) === v.slice(0, 19) ? v : null;
|
||||
};
|
||||
|
||||
// The records of the state file, or [] for any problem. A link, a FIFO or a file over STATE_MAX is
|
||||
// not read.
|
||||
function readState(dataRoot) {
|
||||
if (typeof dataRoot !== "string" || !dataRoot) return [];
|
||||
let fd = null;
|
||||
try {
|
||||
fd = openSync(watchStateFile(dataRoot), constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
||||
const st = fstatSync(fd);
|
||||
if (!st.isFile() || st.size > STATE_MAX) return [];
|
||||
const buf = Buffer.alloc(STATE_MAX + 1);
|
||||
let len = 0, n;
|
||||
while (len <= STATE_MAX && (n = readSync(fd, buf, len, STATE_MAX + 1 - len, null)) > 0) len += n;
|
||||
if (len > STATE_MAX) return [];
|
||||
const doc = JSON.parse(buf.subarray(0, len).toString("utf8"));
|
||||
return doc && typeof doc === "object" && doc.version === STATE_VERSION && Array.isArray(doc.harnesses) ? doc.harnesses : [];
|
||||
} catch {
|
||||
return [];
|
||||
} finally {
|
||||
if (fd !== null) try { closeSync(fd); } catch {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// #1873 harness watch W2 (reports/harness-watch-design.md section 1, "How often"): the stat key of
|
||||
// a list of files and directories. Per path: its real path, dev, ino, size, mtime and ctime. The
|
||||
// real path catches a symlink swap, ctime a copy that keeps mtime. A path that can't be resolved or
|
||||
// stat'ed gives its error code in place, so a file that goes or comes back changes the key too.
|
||||
// The key is compared in memory only. It holds host paths, so it is never written to a file or a log.
|
||||
import { realpathSync, statSync } from "node:fs";
|
||||
|
||||
export function statKey(paths) {
|
||||
return JSON.stringify((Array.isArray(paths) ? paths : []).map((p) => {
|
||||
if (typeof p !== "string" || !p) return null;
|
||||
try {
|
||||
const real = realpathSync(p);
|
||||
const s = statSync(real);
|
||||
return [real, s.dev, s.ino, s.size, s.mtimeMs, s.ctimeMs];
|
||||
} catch (e) {
|
||||
return [p, typeof e?.code === "string" ? e.code : "error"];
|
||||
}
|
||||
}));
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// #1873 harness watch W2: a harness module asks the bus host's watch for a stat pass now, instead
|
||||
// of at the next five-minute tick. claude-code's preflight asks when it has no verdict for the
|
||||
// binary it found (a binary or adapter replaced, touched or moved since the last check, or a host
|
||||
// that just started). The host's watch (packages/cli/src/harness-watch-host.mjs) listens in the
|
||||
// same process; with no listener (a Console, a test, a launch outside a bus host) the request does
|
||||
// nothing. A request carries nothing: the watch compares its own stat key and runs a full pass
|
||||
// only when it changed, so a flood of requests costs one stat pass each and no extra full pass.
|
||||
const listeners = new Set();
|
||||
|
||||
// Calls fn on each request until the returned function is called.
|
||||
export function onWatchRequest(fn) {
|
||||
listeners.add(fn);
|
||||
return () => { listeners.delete(fn); };
|
||||
}
|
||||
|
||||
export function requestWatch() {
|
||||
for (const fn of [...listeners]) {
|
||||
try { fn(); } catch {}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
// #1873 harness watch, slice W3: readWatchView, the read the bus's plugins answer makes of the
|
||||
// host's state file (<org dataRoot>/state/harness-watch.json). It never writes, never throws, and
|
||||
// gives "not checked yet" for anything it cannot use.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { homedir, tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { NOT_CHECKED, readWatchView, watchStateFile } from "../src/harness-watch.mjs";
|
||||
import { watchFile } from "../../cli/src/harness-watch-host.mjs";
|
||||
|
||||
function root(t) {
|
||||
const dir = mkdtempSync(join(tmpdir(), "mosaic-hwv-"));
|
||||
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
||||
mkdirSync(join(dir, "state"), { mode: 0o700 });
|
||||
return dir;
|
||||
}
|
||||
const at = "2026-11-20T10:00:00.000Z";
|
||||
const rec = (harness, over = {}) => ({ harness, state: "fit", via: "inspect", found: true, source: "PATH", version: "1.6.0", digest: "b".repeat(64), lastDigest: "b".repeat(64), fit: "pass", definite: true, failed: [], line: "pi 1.6.0 passed the fit check", checkedAt: at, changedAt: at, ...over });
|
||||
const put = (dir, doc) => writeFileSync(watchStateFile(dir), typeof doc === "string" ? doc : JSON.stringify(doc), { mode: 0o600 });
|
||||
const unchecked = (name) => ({ name, state: "unchecked", version: null, checkedAt: null, changedAt: null, failed: [], line: NOT_CHECKED });
|
||||
|
||||
test("W3 the host writes the file the view reads: one path", () => {
|
||||
assert.equal(watchFile("/d"), watchStateFile("/d"));
|
||||
assert.equal(watchStateFile("/d"), "/d/state/harness-watch.json");
|
||||
});
|
||||
|
||||
test("W3 readWatchView gives each state's record, the loader's names first, and writes nothing", (t) => {
|
||||
const dir = root(t);
|
||||
const doc = { version: 1, checkedAt: at, refused: [], gone: [], harnesses: [rec("pi"), rec("claude-code", { state: "changed", version: "2.1.323", changedAt: "2026-11-20T09:00:00.000Z" }), rec("w3-red", { state: "red", fit: "fail", failed: ["--restricted", "--restricted"] }), rec("w3-missing", { state: "missing", found: false }), rec("w3-unknown", { state: "unknown" })] };
|
||||
put(dir, doc);
|
||||
const before = statSync(watchStateFile(dir));
|
||||
const view = readWatchView({ dataRoot: dir, names: ["claude-code", "pi", "codex"] });
|
||||
assert.deepEqual(view.map((e) => [e.name, e.state]), [["claude-code", "changed"], ["pi", "fit"], ["codex", "unchecked"], ["w3-red", "red"], ["w3-missing", "missing"], ["w3-unknown", "unknown"]]);
|
||||
assert.deepEqual(view[0], { name: "claude-code", state: "changed", version: "2.1.323", checkedAt: at, changedAt: "2026-11-20T09:00:00.000Z", failed: [], line: "pi 1.6.0 passed the fit check" });
|
||||
assert.deepEqual(view[2], unchecked("codex"));
|
||||
assert.deepEqual(view[3].failed, ["--restricted"]);
|
||||
const after = statSync(watchStateFile(dir));
|
||||
assert.deepEqual([after.mtimeMs, after.size, after.mode], [before.mtimeMs, before.size, before.mode]);
|
||||
assert.deepEqual(JSON.parse(readFileSync(watchStateFile(dir), "utf8")), doc);
|
||||
});
|
||||
|
||||
test("W3 readWatchView: no file, a truncated file, a wrong shape, a link, a FIFO and an oversized file give not checked yet", (t) => {
|
||||
const dir = root(t);
|
||||
const names = ["pi", "claude-code"];
|
||||
const none = names.map(unchecked);
|
||||
assert.deepEqual(readWatchView({ dataRoot: dir, names }), none, "no file");
|
||||
assert.deepEqual(readWatchView({ dataRoot: join(dir, "nowhere"), names }), none, "no state directory");
|
||||
assert.deepEqual(readWatchView({ dataRoot: null, names }), none, "no data root");
|
||||
assert.deepEqual(readWatchView({ names }), none, "nothing given");
|
||||
assert.deepEqual(readWatchView(), [], "no names, no file");
|
||||
const good = JSON.stringify({ version: 1, harnesses: [rec("pi")] });
|
||||
for (const bad of [good.slice(0, good.length - 3), "{", "", "null", "7", JSON.stringify({ version: "1", harnesses: [rec("pi")] }), JSON.stringify({ version: 1, harnesses: { pi: rec("pi") } }), JSON.stringify({ harnesses: [rec("pi")] })]) {
|
||||
put(dir, bad);
|
||||
assert.deepEqual(readWatchView({ dataRoot: dir, names }), none, bad.slice(0, 40));
|
||||
}
|
||||
// A link to a good file is not followed: the host writes a plain file, renamed into place.
|
||||
const elsewhere = join(dir, "elsewhere.json");
|
||||
writeFileSync(elsewhere, good);
|
||||
rmSync(watchStateFile(dir), { force: true });
|
||||
symlinkSync(elsewhere, watchStateFile(dir));
|
||||
assert.deepEqual(readWatchView({ dataRoot: dir, names }), none, "a link");
|
||||
rmSync(watchStateFile(dir));
|
||||
// A FIFO with no writer does not block the read.
|
||||
execFileSync("mkfifo", [watchStateFile(dir)]);
|
||||
assert.deepEqual(readWatchView({ dataRoot: dir, names }), none, "a FIFO");
|
||||
rmSync(watchStateFile(dir));
|
||||
mkdirSync(watchStateFile(dir));
|
||||
assert.deepEqual(readWatchView({ dataRoot: dir, names }), none, "a directory");
|
||||
rmSync(watchStateFile(dir), { recursive: true });
|
||||
// Over 1 MiB is not read at all.
|
||||
put(dir, JSON.stringify({ version: 1, harnesses: [rec("pi")], pad: "x".repeat(1024 * 1024) }));
|
||||
assert.deepEqual(readWatchView({ dataRoot: dir, names }), none, "an oversized file");
|
||||
put(dir, good);
|
||||
assert.equal(readWatchView({ dataRoot: dir, names })[0].state, "fit", "and the good file reads");
|
||||
});
|
||||
|
||||
test("W3 readWatchView: a bad record reads as not checked yet for its harness, and bad fields are dropped", (t) => {
|
||||
const dir = root(t);
|
||||
put(dir, { version: 1, harnesses: [
|
||||
rec("pi", { state: "green" }),
|
||||
rec("pi", { state: "red" }),
|
||||
rec("claude-code", { checkedAt: "yesterday", changedAt: "2026-13-45T99:00:00Z", version: "2.1\nsecond line\u0007", line: 5, failed: ["ok-rule", "has space", 7, "x".repeat(80)] }),
|
||||
rec("Bad Name"), rec("../up"), rec(""), [rec("codex")], "pi", null, { state: "fit" },
|
||||
] });
|
||||
const view = readWatchView({ dataRoot: dir, names: ["pi", "claude-code"] });
|
||||
assert.deepEqual(view[0], unchecked("pi"), "a state the watch does not give is not a guess, and the first record for a name wins");
|
||||
assert.deepEqual(view[1], { name: "claude-code", state: "fit", version: "2.1", checkedAt: null, changedAt: null, failed: ["ok-rule"], line: null });
|
||||
assert.equal(view.length, 2, "records with names that are not harness names are dropped");
|
||||
});
|
||||
|
||||
test("W3 readWatchView redacts again: the data roots, the home directory, PATH entries, env values and loose paths", (t) => {
|
||||
const dir = root(t);
|
||||
const instance = mkdtempSync(join(tmpdir(), "mosaic-hwv-inst-"));
|
||||
t.after(() => rmSync(instance, { recursive: true, force: true }));
|
||||
const env = { MOSAIC_PI_BIN: "/opt/w3/pi-binary", W3_TOKEN: "w3-token-value-xyz", PATH: "/opt/w3-bin:/usr/bin" };
|
||||
put(dir, { version: 1, harnesses: [rec("pi", {
|
||||
version: `1.6.0 from ${env.MOSAIC_PI_BIN}`,
|
||||
line: `${dir}/state ${instance}/plugins ${homedir()}/x ${env.W3_TOKEN} /opt/w3-bin/pi /srv/other/place file:///etc/x ~/y/z`,
|
||||
failed: [env.W3_TOKEN, "--tools"],
|
||||
})] });
|
||||
const [e] = readWatchView({ dataRoot: dir, names: ["pi"], env, paths: [instance] });
|
||||
const all = JSON.stringify(e);
|
||||
for (const needle of [dir, instance, homedir(), env.W3_TOKEN, env.MOSAIC_PI_BIN, "/opt/w3-bin", "/srv/other", "/etc/x", "~/y"]) assert.ok(!all.includes(needle), `${needle} in ${all}`);
|
||||
assert.equal(e.version, "1.6.0 from <MOSAIC_PI_BIN>");
|
||||
assert.deepEqual(e.failed, ["--tools"]);
|
||||
assert.match(e.line, /<W3_TOKEN>/);
|
||||
});
|
||||
|
||||
test("W3 readWatchView: a name that is an env value is dropped unless the loader gives it, entries are capped, and an impossible date is not a time", (t) => {
|
||||
const dir = root(t);
|
||||
const env = { W3_NAME: "w3-secret-name" };
|
||||
const many = Array.from({ length: 300 }, (_, i) => rec(`w3-h${i}`));
|
||||
put(dir, { version: 1, harnesses: [rec("w3-secret-name"), rec("pi", { checkedAt: "2026-02-30T00:00:00Z", changedAt: "2026-11-20T10:00:00Z" }), ...many] });
|
||||
const view = readWatchView({ dataRoot: dir, names: ["pi"], env });
|
||||
assert.ok(!view.some((e) => e.name === "w3-secret-name"), "a file-only name equal to an env value does not reach the answer");
|
||||
assert.equal(view.length, 64, "at most 64 entries");
|
||||
assert.deepEqual([view[0].checkedAt, view[0].changedAt], [null, "2026-11-20T10:00:00Z"], "2026-02-30 is not a time; a real one is kept");
|
||||
assert.equal(readWatchView({ dataRoot: dir, names: ["w3-secret-name"], env })[0].state, "fit", "a name the loader gives is kept");
|
||||
});
|
||||
|
||||
test("W3 readWatchView reads at most the first 256 records: a usable record after them is not checked yet", (t) => {
|
||||
const dir = root(t);
|
||||
put(dir, { version: 1, harnesses: [...Array(255).fill(null), rec("pi")] });
|
||||
assert.equal(readWatchView({ dataRoot: dir, names: ["pi"] })[0].state, "fit", "the 256th record is read");
|
||||
put(dir, { version: 1, harnesses: [...Array(256).fill(null), rec("pi")] });
|
||||
assert.deepEqual(readWatchView({ dataRoot: dir, names: ["pi"] }), [unchecked("pi")], "the 257th is not");
|
||||
});
|
||||
@@ -0,0 +1,398 @@
|
||||
// #1873 harness watch, slice W2 (reports/harness-watch-design.md sections 1 and 5): the claude-code
|
||||
// fit rule (every flag the adapter passes is in --help, and --version answers), the verdict
|
||||
// preflight reads, the no-network captures, the watch's outer signal and flag rule ids, and pi's
|
||||
// digest and undecided fields.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { tree } from "./helpers.mjs";
|
||||
import { KINDS } from "../src/kinds.mjs";
|
||||
import { 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";
|
||||
import { locate, printedVersion } from "../harness/pi/binary.mjs";
|
||||
import * as claude from "../harness/claude-code/index.mjs";
|
||||
|
||||
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
|
||||
const WATCH_SRC = join(REPO, "packages", "plugins", "src", "harness-watch.mjs");
|
||||
const PI_NAME = "@earendil-works/pi-coding-agent";
|
||||
const NO_BWRAP = sandboxProblem() ? { skip: "no working bwrap here" } : {};
|
||||
const CLAUDE = process.env.MOSAIC_CLAUDE_BIN;
|
||||
const CREDENTIAL = { ANTHROPIC_API_KEY: "fixture-api-key" };
|
||||
const DIGEST = /^[a-f0-9]{64}$/;
|
||||
|
||||
function scratch(t) {
|
||||
const root = fixtureDir("mosaic-watch-");
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
return root;
|
||||
}
|
||||
|
||||
// The help a claude that has every flag the adapter passes would print (a test fixture; the module
|
||||
// itself reads the flags from adapter.sh). --system-prompt-file shows only as --system-prompt[-file],
|
||||
// as on claude 2.1.322. Descriptions also mention flags (--settings, --mcp-config, --model), as
|
||||
// claude's do; only the option lines count.
|
||||
const HELP_LINES = [
|
||||
"Usage: claude [options] [command] [prompt]", "", "Options:",
|
||||
" -p, --print Print response and exit",
|
||||
" --output-format <format> text, json or stream-json",
|
||||
" --verbose Verbose output",
|
||||
" --include-partial-messages Partial message chunks",
|
||||
" --bare Minimal mode: no --settings files, no --mcp-config servers,",
|
||||
" --settings and --model still apply",
|
||||
" --system-prompt <prompt> System prompt; also --system-prompt[-file]",
|
||||
" --model <model> Model",
|
||||
" --effort <level> Effort",
|
||||
" --restricted Restricted mode",
|
||||
" --tools <tools...> Built-in tools",
|
||||
" --allowedTools, --allowed-tools <tools...> Allowed tools",
|
||||
" --permission-mode <mode> Permission mode",
|
||||
" --settings <file-or-json> Settings",
|
||||
" --strict-mcp-config Only --mcp-config servers",
|
||||
" --mcp-config <configs...> MCP servers",
|
||||
" --disable-slash-commands No skills",
|
||||
" -r, --resume [value] Resume a session",
|
||||
" --session-id <uuid> Session id",
|
||||
" -v, --version Version",
|
||||
" -h, --help Help",
|
||||
];
|
||||
// drop removes the option line that names a flag (its head, not a mention in a description).
|
||||
const help = ({ drop = [], extra = [] } = {}) => [...HELP_LINES.filter((l) => !drop.some((d) => new RegExp(`^ (?:-[A-Za-z], )?${d}(?=[ ,<\\[]|$)`).test(l))), ...extra].join("\n");
|
||||
|
||||
// A stand-in claude: --version prints version (or exits versionExit), --help prints text (or exits
|
||||
// helpExit, or sleeps with helpHang), anything else exits 3. With nsFile it writes the network
|
||||
// namespace it runs in there first.
|
||||
function standIn(dir, { name = "claude", version = "9.9.9 (stand-in)", versionExit = null, helpText = help(), helpExit = null, helpHang = false, nsFile = null } = {}) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
const bin = join(dir, name);
|
||||
writeFileSync(bin, [
|
||||
"#!/bin/sh",
|
||||
nsFile ? `readlink /proc/self/ns/net >> '${nsFile}'` : "",
|
||||
'case "$1" in',
|
||||
`--version) ${versionExit !== null ? `exit ${versionExit}` : `echo '${version}'`};;`,
|
||||
`--help) ${helpExit !== null ? `exit ${helpExit}` : helpHang ? "exec sleep 30" : `cat <<'EOF'\n${helpText}\nEOF\n`};;`,
|
||||
"*) exit 3;;",
|
||||
"esac", "",
|
||||
].join("\n"), { mode: 0o755 });
|
||||
return bin;
|
||||
}
|
||||
|
||||
test("W2 claude-code: a stand-in whose --help lacks an adapter flag is red, and preflight refuses harness-feature-missing", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const bin = standIn(join(dir, "bin"), { helpText: help({ drop: ["--strict-mcp-config"] }) });
|
||||
const env = { MOSAIC_CLAUDE_BIN: bin, PATH: "/usr/bin:/bin" };
|
||||
// Before any check, preflight has no verdict and does not refuse.
|
||||
assert.equal(claude.preflight({ env: { ...env, ...CREDENTIAL } }), null);
|
||||
const r = await claude.inspect({ env, scratch: scratch(t) });
|
||||
assert.deepEqual([r.found, r.fit, r.definite, r.failed, r.version], [true, "fail", true, ["--strict-mcp-config"], "9.9.9 (stand-in)"]);
|
||||
assert.match(r.line, /--help lacks --strict-mcp-config/);
|
||||
const refusal = claude.preflight({ env: { ...env, ...CREDENTIAL } });
|
||||
assert.equal(refusal?.code, "harness-feature-missing");
|
||||
assert.match(refusal.message, /MOSAIC_CLAUDE_BIN/);
|
||||
assert.match(refusal.message, /--strict-mcp-config/);
|
||||
assert.ok(!refusal.message.includes(dir), "the refusal names the variable, not the path");
|
||||
// The same file through PATH is the same verdict, named by its lookup step.
|
||||
const viaPath = claude.preflight({ env: { PATH: join(dir, "bin"), ...CREDENTIAL } });
|
||||
assert.equal(viaPath?.code, "harness-feature-missing");
|
||||
assert.match(viaPath.message, /the claude from PATH/);
|
||||
// The watch keeps the flag as the rule id (W1's rule ids now take a leading dash or two).
|
||||
const { watchHarnesses } = await import(pathToFileURL(WATCH_SRC).href);
|
||||
const out = await watchHarnesses({ env: { ...env, MOSAIC_PI_BIN: join(dir, "no-pi") }, scratch: scratch(t) });
|
||||
const rec = out.records.find((x) => x.source === "MOSAIC_CLAUDE_BIN");
|
||||
assert.deepEqual([rec.state, rec.failed], ["red", ["--strict-mcp-config"]]);
|
||||
// The binary replaced in place by one with every flag: its stat key changed, so the old verdict
|
||||
// no longer refuses, before any new check.
|
||||
standIn(join(dir, "bin"), { helpText: help() });
|
||||
assert.equal(claude.preflight({ env: { ...env, ...CREDENTIAL } }), null);
|
||||
const again = await claude.inspect({ env, scratch: scratch(t) });
|
||||
assert.deepEqual([again.fit, again.definite, again.failed], ["pass", true, []], again.line);
|
||||
assert.equal(claude.preflight({ env: { ...env, ...CREDENTIAL } }), null);
|
||||
});
|
||||
|
||||
test("W2 claude-code: --resume, which only a later turn passes, is checked too, and --x[-y] in help counts as --x-y", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(dir, { helpText: help({ drop: ["--resume"] }) }) }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.fit, r.failed], ["fail", ["--resume"]]);
|
||||
const prose = help({ drop: ["--system-prompt"], extra: [" --append-system-prompt <prompt> Append; see --system-prompt-files"] });
|
||||
const s = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(join(dir, "b"), { helpText: prose }) }, scratch: scratch(t) });
|
||||
assert.deepEqual([s.fit, s.failed], ["fail", ["--system-prompt-file"]], "a longer token or a token inside a word is not the flag");
|
||||
// Only option lines count; --x[-y] in prose gives --x-y only beside an --x option line.
|
||||
assert.deepEqual([...claude.helpFlags("use -p/--print or --system-prompt[-file]; non-interactive")], []);
|
||||
assert.deepEqual([...claude.helpFlags(" -c, --continue Continue\n --system-prompt <p> Prompt\n see --system-prompt[-file], --zz[-q]\n --settings in prose")].sort(), ["--continue", "--system-prompt", "--system-prompt-file", "-c"]);
|
||||
});
|
||||
|
||||
// Review P2-3: a flag a description mentions is not an option. A claude whose help lost the
|
||||
// --settings and --mcp-config option lines, though other descriptions still name both, is red.
|
||||
test("W2 claude-code: a flag only a description mentions does not count", async (t) => {
|
||||
const text = help({ drop: ["--settings", "--mcp-config"] });
|
||||
assert.match(text, /--settings/);
|
||||
assert.match(text, /--mcp-config/);
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(scratch(t), { helpText: text }) }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.fit, r.definite, [...r.failed].sort()], ["fail", true, ["--mcp-config", "--settings"]], r.line);
|
||||
});
|
||||
|
||||
test("W2 claude-code: the flags come from adapter.sh, not a list in the module", async (t) => {
|
||||
// A copy of the module beside an adapter that passes one more flag; the rest of the repository
|
||||
// is linked. The same stand-in passes with the real adapter and fails with the copy.
|
||||
const root = scratch(t);
|
||||
for (const p of ["packages/harness", "packages/seat", "packages/plugins/src", "tools", "node_modules"]) {
|
||||
mkdirSync(join(root, p, ".."), { recursive: true });
|
||||
symlinkSync(join(REPO, p), join(root, p));
|
||||
}
|
||||
mkdirSync(join(root, "packages", "plugins", "harness", "claude-code"), { recursive: true });
|
||||
cpSync(join(REPO, "packages", "plugins", "harness", "claude-code", "index.mjs"), join(root, "packages", "plugins", "harness", "claude-code", "index.mjs"));
|
||||
mkdirSync(join(root, "adapters", "claude"), { recursive: true });
|
||||
cpSync(join(REPO, "adapters", "claude", "stream.mjs"), join(root, "adapters", "claude", "stream.mjs"));
|
||||
const adapter = readFileSync(join(REPO, "adapters", "claude", "adapter.sh"), "utf8");
|
||||
assert.ok(adapter.includes(" --disable-slash-commands \\\n"));
|
||||
writeFileSync(join(root, "adapters", "claude", "adapter.sh"), adapter.replace(" --disable-slash-commands \\\n", " --disable-slash-commands \\\n --fit-probe-flag \\\n"), { mode: 0o755 });
|
||||
const copy = await import(pathToFileURL(join(root, "packages", "plugins", "harness", "claude-code", "index.mjs")).href);
|
||||
const bin = standIn(join(root, "bin"));
|
||||
const real = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t) });
|
||||
assert.deepEqual([real.fit, real.failed], ["pass", []], real.line);
|
||||
const changed = await copy.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t) });
|
||||
assert.deepEqual([changed.fit, changed.definite, changed.failed], ["fail", true, ["--fit-probe-flag"]], changed.line);
|
||||
});
|
||||
|
||||
test("W2 claude-code: --version or --help exiting non-zero is red; a check that is stopped is unknown and preflight does not refuse", async (t) => {
|
||||
const dir = scratch(t);
|
||||
let r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(join(dir, "v"), { versionExit: 2 }) }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.fit, r.definite, r.failed, r.version], ["fail", true, ["--version"], null]);
|
||||
assert.equal(claude.preflight({ env: { MOSAIC_CLAUDE_BIN: join(dir, "v", "claude"), ...CREDENTIAL } })?.code, "harness-feature-missing");
|
||||
r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(join(dir, "h"), { helpExit: 1 }) }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.fit, r.definite, r.failed], ["fail", true, ["--help"]]);
|
||||
const hang = standIn(join(dir, "s"), { helpHang: true });
|
||||
const stop = new AbortController();
|
||||
setTimeout(() => stop.abort(), 500);
|
||||
r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: hang }, scratch: scratch(t), signal: stop.signal });
|
||||
assert.deepEqual([r.fit, r.definite, r.failed], ["unknown", false, []]);
|
||||
assert.match(r.line, /--help was stopped; fit not checked/);
|
||||
assert.equal(claude.preflight({ env: { MOSAIC_CLAUDE_BIN: hang, ...CREDENTIAL } }), null, "an undecided check does not refuse in this slice");
|
||||
});
|
||||
|
||||
const unshareRuns = existsSync(UNSHARE) && spawnSync(UNSHARE, ["-r", "-n", "--", "/usr/bin/true"]).status === 0;
|
||||
test("W2 claude-code: --version and --help run in their own network namespace when unshare runs; without unshare they run and the line says so", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const host = readlinkSync("/proc/self/ns/net");
|
||||
const nsFile = join(dir, "ns");
|
||||
const bin = standIn(join(dir, "bin"), { nsFile });
|
||||
if (unshareRuns) {
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t) });
|
||||
assert.equal(r.fit, "pass", r.line);
|
||||
const seen = readFileSync(nsFile, "utf8").trim().split("\n");
|
||||
assert.equal(seen.length, 2);
|
||||
for (const ns of seen) assert.notEqual(ns, host, "no network: not the host's namespace");
|
||||
assert.doesNotMatch(r.line, /network/);
|
||||
} else t.diagnostic("unshare -r -n does not run here; only the fallback is checked");
|
||||
rmSync(nsFile, { force: true });
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t), unshare: join(dir, "no-unshare") });
|
||||
assert.equal(r.fit, "pass");
|
||||
assert.match(r.line, /run with the network on: unshare is missing$/);
|
||||
assert.deepEqual(readFileSync(nsFile, "utf8").trim().split("\n"), [host, host]);
|
||||
});
|
||||
|
||||
test("W2 the real claude named by MOSAIC_CLAUDE_BIN is fit, and preflight lets it through", CLAUDE ? {} : { skip: "MOSAIC_CLAUDE_BIN is not set" }, async (t) => {
|
||||
const env = { MOSAIC_CLAUDE_BIN: CLAUDE, PATH: "/usr/bin:/bin" };
|
||||
const r = await claude.inspect({ env, scratch: scratch(t) });
|
||||
assert.deepEqual([r.found, r.fit, r.definite, r.failed], [true, "pass", true, []], r.line);
|
||||
assert.equal(claude.preflight({ env: { ...env, ...CREDENTIAL } }), null);
|
||||
const { watchHarnesses } = await import(pathToFileURL(WATCH_SRC).href);
|
||||
const out = await watchHarnesses({ env: { ...env, MOSAIC_PI_BIN: "/nonexistent/pi" }, scratch: scratch(t) });
|
||||
assert.equal(out.records.find((x) => x.source === "MOSAIC_CLAUDE_BIN").state, "fit");
|
||||
});
|
||||
|
||||
test("W2 claude-code watchKey changes when the binary is replaced, and holds no answer of its own", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const bin = standIn(dir);
|
||||
const env = { MOSAIC_CLAUDE_BIN: bin };
|
||||
const a = claude.watchKey({ env });
|
||||
assert.equal(claude.watchKey({ env }), a);
|
||||
standIn(dir, { version: "9.9.10 (stand-in)" });
|
||||
assert.notEqual(claude.watchKey({ env }), a);
|
||||
assert.notEqual(claude.watchKey({ env: {} }), a);
|
||||
});
|
||||
|
||||
// A stand-in pi package named by MOSAIC_PI_BIN, as pi-fit.test.mjs makes one.
|
||||
function piPackage(t, knobs) {
|
||||
const root = scratch(t);
|
||||
const pkg = join(root, "prefix", "lib", "node_modules", PI_NAME);
|
||||
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: PI_NAME, version: "2.0.0", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn({ version: "2.0.0", ...knobs }), { mode: 0o755 });
|
||||
mkdirSync(join(root, "prefix", "bin"));
|
||||
symlinkSync(`../lib/node_modules/${PI_NAME}/dist/bundle/cli.js`, join(root, "prefix", "bin", "pi"));
|
||||
return { root, env: { MOSAIC_PI_BIN: join(root, "prefix", "bin", "pi"), PATH: "/usr/bin:/bin" } };
|
||||
}
|
||||
|
||||
test("W2 a failed pi check carries the digest of the files it ran, and says it was definite", NO_BWRAP, async (t) => {
|
||||
const lacking = piPackage(t, { dropFlags: ["--no-skills"] });
|
||||
const found = locate(lacking.env);
|
||||
const fit = await piFit(found, { levels: pi.levels() });
|
||||
assert.deepEqual([fit.ok, fit.undecided], [false, false], fit.message);
|
||||
assert.match(fit.digest, DIGEST);
|
||||
const r = await pi.inspect({ env: lacking.env });
|
||||
assert.deepEqual([r.fit, r.definite, r.digest], ["fail", true, fit.digest]);
|
||||
// Another file under the package is another digest.
|
||||
writeFileSync(join(lacking.root, "prefix", "lib", "node_modules", PI_NAME, "dist", "bundle", "extra.js"), "// changed\n");
|
||||
const later = await piFit(found, { levels: pi.levels() });
|
||||
assert.match(later.digest, DIGEST);
|
||||
assert.notEqual(later.digest, fit.digest);
|
||||
});
|
||||
|
||||
test("W2 an undecided pi check is unknown from its undecided field; pi's inspect matches no message text", NO_BWRAP, async (t) => {
|
||||
const flood = piPackage(t, { flood: true });
|
||||
const fit = await piFit(locate(flood.env), { levels: pi.levels() });
|
||||
assert.deepEqual([fit.ok, fit.undecided], [false, true], fit.message);
|
||||
assert.match(fit.digest, DIGEST);
|
||||
const r = await pi.inspect({ env: flood.env });
|
||||
assert.deepEqual([r.fit, r.definite, r.digest], ["unknown", false, fit.digest]);
|
||||
const src = readFileSync(join(REPO, "packages", "plugins", "harness", "pi", "index.mjs"), "utf8");
|
||||
assert.doesNotMatch(src, /can't be checked/, "inspect reads fit.undecided, not the message");
|
||||
// A stopped check is undecided too, and its processes are gone with it.
|
||||
const slow = piPackage(t, { versionHang: true });
|
||||
const stop = new AbortController();
|
||||
setTimeout(() => stop.abort(), 300);
|
||||
const began = Date.now();
|
||||
const stopped = await pi.inspect({ env: slow.env, signal: stop.signal });
|
||||
assert.ok(Date.now() - began < 4000, "the stop ended the check, and its --version capture with it");
|
||||
assert.deepEqual([stopped.fit, stopped.definite], ["unknown", false], stopped.line);
|
||||
});
|
||||
|
||||
// A scratch plugin tree of stand-in harnesses: each inspect writes <MARKS>/<name> when it is
|
||||
// asked, then answers its entry of the JSON file WATCH_ANSWER names; "hang-signal" waits for its
|
||||
// signal and writes <MARKS>/<name>.aborted when it aborts; "slow-stop" ends 300 ms after its signal
|
||||
// aborts and writes <MARKS>/<name>.ended then.
|
||||
const marker = (name) => `import { readFileSync, writeFileSync } from "node:fs";
|
||||
export const interfaceVersion = ${KINDS.harness.interfaceVersion};
|
||||
${KINDS.harness.exports.map((e) => `export function ${e}() { return null; }`).join("\n")}
|
||||
export async function inspect({ env, signal }) {
|
||||
writeFileSync(env.MARKS + "/${name}", "1");
|
||||
const a = JSON.parse(readFileSync(env.WATCH_ANSWER, "utf8"))[${JSON.stringify(name)}];
|
||||
if (a === "hang-signal") return new Promise(() => { signal.addEventListener("abort", () => writeFileSync(env.MARKS + "/${name}.aborted", "1")); });
|
||||
if (a === "slow-stop") return new Promise((done) => { signal.addEventListener("abort", () => setTimeout(() => { writeFileSync(env.MARKS + "/${name}.ended", "1"); done({ found: true }); }, 300)); });
|
||||
return a;
|
||||
}
|
||||
`;
|
||||
async function markerTree(t, names) {
|
||||
const tr = tree(t);
|
||||
for (const name of names) tr.write("harness", name, marker(name));
|
||||
const { watchHarnesses } = await import(pathToFileURL(join(tr.pkg, "src", "harness-watch.mjs")).href);
|
||||
const dir = scratch(t);
|
||||
mkdirSync(join(dir, "marks"));
|
||||
return { watchHarnesses, dir, env: { WATCH_ANSWER: join(dir, "answer.json"), MARKS: join(dir, "marks") } };
|
||||
}
|
||||
|
||||
test("W2 the watch's signal aborts the inspect it is in and asks no further module", async (t) => {
|
||||
const w = await markerTree(t, ["a-first", "b-second"]);
|
||||
writeFileSync(w.env.WATCH_ANSWER, JSON.stringify({ "a-first": "hang-signal", "b-second": { found: true, fit: "pass", definite: true } }));
|
||||
const stop = new AbortController();
|
||||
setTimeout(() => stop.abort(), 200);
|
||||
const began = Date.now();
|
||||
const out = await w.watchHarnesses({ env: w.env, scratch: w.dir, signal: stop.signal, timeoutMs: 60000 });
|
||||
assert.ok(Date.now() - began < 10000);
|
||||
assert.equal(out.stopped, true);
|
||||
assert.deepEqual(out.records, []);
|
||||
assert.deepEqual(readdirSync(w.env.MARKS).sort(), ["a-first", "a-first.aborted"], "the running inspect saw the abort; the next was not asked");
|
||||
// An already aborted signal asks none.
|
||||
rmSync(w.env.MARKS, { recursive: true });
|
||||
mkdirSync(w.env.MARKS);
|
||||
const none = await w.watchHarnesses({ env: w.env, scratch: w.dir, signal: AbortSignal.abort() });
|
||||
assert.deepEqual([none.stopped, readdirSync(w.env.MARKS)], [true, []]);
|
||||
});
|
||||
|
||||
test("W2 the watch keeps flag rule ids and still drops ones that are not names", async (t) => {
|
||||
const w = await markerTree(t, ["flags"]);
|
||||
writeFileSync(w.env.WATCH_ANSWER, JSON.stringify({ flags: { found: true, fit: "fail", definite: true, failed: ["--strict-mcp-config", "-p", "C1", "---x", "--", "-", "--a b", "/tmp/x"] } }));
|
||||
const out = await w.watchHarnesses({ env: w.env, scratch: w.dir });
|
||||
assert.deepEqual(out.records[0].failed, ["--strict-mcp-config", "-p", "C1"]);
|
||||
assert.equal(out.records[0].state, "red");
|
||||
});
|
||||
|
||||
// Review P3-6: a stop waits for the inspect it aborted to end (up to the grace), so what it started
|
||||
// is gone when the watch returns.
|
||||
test("W2 a stopped watch returns after the inspect it aborted has ended", async (t) => {
|
||||
const w = await markerTree(t, ["slow"]);
|
||||
writeFileSync(w.env.WATCH_ANSWER, JSON.stringify({ slow: "slow-stop" }));
|
||||
const stop = new AbortController();
|
||||
setTimeout(() => stop.abort(), 100);
|
||||
const out = await w.watchHarnesses({ env: w.env, scratch: w.dir, signal: stop.signal, timeoutMs: 60000 });
|
||||
assert.equal(out.stopped, true);
|
||||
assert.ok(existsSync(join(w.env.MARKS, "slow.ended")), "the watch waited for the inspect to end");
|
||||
});
|
||||
|
||||
const alive = (pid) => { try { process.kill(pid, 0); return true; } catch { return false; } };
|
||||
const until = async (fn, ms = 3000) => { const end = Date.now() + ms; while (!fn() && Date.now() < end) await new Promise((r) => setTimeout(r, 25)); return fn(); };
|
||||
|
||||
// Review P3-6: a capture's whole process group goes, on a stop and when it ends: a stand-in that
|
||||
// leaves a child in the background leaves nothing.
|
||||
test("W2 claude-code: a capture stops what the binary left in the background, on a stop and when it ends", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const bin = join(dir, "claude");
|
||||
const left = join(dir, "left"), held = join(dir, "held");
|
||||
writeFileSync(bin, [
|
||||
"#!/bin/sh", 'case "$1" in',
|
||||
`--version) sleep 30 >/dev/null 2>&1 & echo $! > '${left}'; echo '9.9.9';;`,
|
||||
`--help) sleep 30 2>/dev/null & echo $! > '${held}' 2>/dev/null; wait;;`,
|
||||
"*) exit 3;;", "esac", "",
|
||||
].join("\n"), { mode: 0o755 });
|
||||
const stop = new AbortController();
|
||||
const r = claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t), signal: stop.signal });
|
||||
assert.ok(await until(() => existsSync(held) && readFileSync(held, "utf8").trim() !== "", 15000), "--help started");
|
||||
stop.abort();
|
||||
const out = await r;
|
||||
assert.match(out.line, /--help was stopped/);
|
||||
const pids = [left, held].map((f) => Number(readFileSync(f, "utf8").trim()));
|
||||
for (const pid of pids) assert.ok(await until(() => !alive(pid)), `process ${pid} left by a capture is gone`);
|
||||
});
|
||||
|
||||
// Re-check P3-3: pi's version capture kills its group on a stop too. The background child keeps
|
||||
// the stdout pipe open, so killing the parent alone would leave it running.
|
||||
test("W2 pi: a stopped version capture stops what the binary left in the background", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const bin = join(dir, "pi"), held = join(dir, "held");
|
||||
writeFileSync(bin, `#!/bin/sh\nsleep 30 2>/dev/null & echo $! > '${held}' 2>/dev/null\nwait\n`, { mode: 0o755 });
|
||||
const stop = new AbortController();
|
||||
const r = printedVersion(bin, { signal: stop.signal });
|
||||
assert.ok(await until(() => existsSync(held) && readFileSync(held, "utf8").trim() !== "", 15000), "--version started");
|
||||
stop.abort();
|
||||
assert.equal(await r, null);
|
||||
const pid = Number(readFileSync(held, "utf8").trim());
|
||||
assert.ok(await until(() => !alive(pid)), `process ${pid} left by the capture is gone`);
|
||||
});
|
||||
|
||||
// Review P3-6: an already aborted signal starts nothing and decides nothing.
|
||||
test("W2 claude-code: an inspect with an already aborted signal runs nothing and is unknown", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const nsFile = join(dir, "ran");
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: standIn(join(dir, "bin"), { nsFile }) }, scratch: scratch(t), signal: AbortSignal.abort() });
|
||||
assert.deepEqual([r.fit, r.definite], ["unknown", false], r.line);
|
||||
assert.equal(existsSync(nsFile), false, "the binary was not run");
|
||||
});
|
||||
|
||||
// Review P3-6: at most 16 verdicts are kept; the oldest goes first.
|
||||
test("W2 claude-code: preflight's verdicts are capped, oldest first", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const bins = [];
|
||||
for (let i = 0; i < 17; i++) bins.push(standIn(join(dir, `b${i}`), { helpText: help({ drop: ["--restricted"] }) }));
|
||||
for (const bin of bins) assert.equal((await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t) })).fit, "fail");
|
||||
const refused = (bin) => claude.preflight({ env: { MOSAIC_CLAUDE_BIN: bin, ...CREDENTIAL } })?.code ?? null;
|
||||
assert.equal(refused(bins[0]), null, "the first verdict was dropped");
|
||||
for (const bin of bins.slice(1)) assert.equal(refused(bin), "harness-feature-missing");
|
||||
});
|
||||
|
||||
// Review P3-6: pi's inspect decides from undecided alone, whatever the message says.
|
||||
test("W2 pi's inspect reads undecided, not the message", async (t) => {
|
||||
const stand = piPackage(t, {});
|
||||
const digest = "b".repeat(64);
|
||||
const run = (fit) => pi.inspect({ env: stand.env, check: async () => fit });
|
||||
let r = await run({ ok: false, message: "pi (MOSAIC_PI_BIN) can't be checked: words", digest, undecided: false });
|
||||
assert.deepEqual([r.fit, r.definite, r.digest], ["fail", true, digest]);
|
||||
r = await run({ ok: false, message: "pi (MOSAIC_PI_BIN) lacks --no-skills", digest, undecided: true });
|
||||
assert.deepEqual([r.fit, r.definite], ["unknown", false]);
|
||||
r = await run({ ok: false, message: "pi (MOSAIC_PI_BIN) lacks --no-skills" });
|
||||
assert.deepEqual([r.fit, r.definite, r.digest], ["unknown", false, null], "a check that gives no undecided field is undecided");
|
||||
});
|
||||
@@ -0,0 +1,422 @@
|
||||
// #1873 harness watch, slice W1 (reports/harness-watch-design.md sections 1 and 5): one pass over
|
||||
// every harness the loader gives, an optional inspect per module or the version fallback, a
|
||||
// state from the answer and the last record, and transitions only on a state or digest change.
|
||||
// No record holds a host path or an env value.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createReadStream, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, watch, writeFileSync } from "node:fs";
|
||||
import { createHash } from "node:crypto";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, resolve } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { tree } from "./helpers.mjs";
|
||||
import { KINDS } from "../src/kinds.mjs";
|
||||
import { 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";
|
||||
|
||||
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
|
||||
const WATCH_SRC = join(REPO, "packages", "plugins", "src", "harness-watch.mjs");
|
||||
const PI_NAME = "@earendil-works/pi-coding-agent";
|
||||
const NO_BWRAP = sandboxProblem() ? { skip: "no working bwrap here" } : {};
|
||||
const DIGEST_A = "a".repeat(64);
|
||||
const DIGEST_B = "b".repeat(64);
|
||||
|
||||
function scratch(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), "mosaic-watch-"));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
return root;
|
||||
}
|
||||
|
||||
// A stand-in harness module: every export the kind needs, a version() that answers
|
||||
// env.WATCH_VERSION, and (with inspect) an inspect() that answers its own entry of the JSON file
|
||||
// env.WATCH_ANSWER names, read at each call: "throw" throws, "hang" never answers, "hang-signal"
|
||||
// never answers but writes <answer file>.aborted when its signal aborts, and the rest build an
|
||||
// answer JSON cannot carry (a throwing getter, a Proxy, inherited fields, a huge sparse array).
|
||||
const standIn = (name, { inspect = true } = {}) => `import { readFileSync, writeFileSync } from "node:fs";
|
||||
export const interfaceVersion = ${KINDS.harness.interfaceVersion};
|
||||
${KINDS.harness.exports.filter((e) => e !== "version").map((e) => `export function ${e}() { return null; }`).join("\n")}
|
||||
export async function version({ env }) {
|
||||
if (env.WATCH_VERSION === "throw") throw new Error("version failed at " + env.WATCH_SECRET);
|
||||
return env.WATCH_VERSION ?? null;
|
||||
}
|
||||
${inspect ? `export async function inspect({ env, signal }) {
|
||||
const a = JSON.parse(readFileSync(env.WATCH_ANSWER, "utf8"))[${JSON.stringify(name)}];
|
||||
const boom = () => { throw new Error("inspect failed at " + env.WATCH_SECRET); };
|
||||
if (a === "throw") boom();
|
||||
if (a === "hang") return new Promise(() => {});
|
||||
if (a === "hang-signal") return new Promise(() => { signal.addEventListener("abort", () => writeFileSync(env.WATCH_ANSWER + ".aborted", "1")); });
|
||||
if (a === "getter") return { found: true, fit: "pass", definite: true, get digest() { return boom(); } };
|
||||
if (a === "proxy") return new Proxy({}, { get: boom, getOwnPropertyDescriptor: boom, ownKeys: boom });
|
||||
if (a === "surfaces-proxy") return { found: true, fit: "pass", definite: true, digest: "${DIGEST_A}", surfaces: new Proxy({}, { ownKeys: boom }) };
|
||||
if (a === "inherited") return Object.create({ found: true, fit: "pass", definite: true, digest: "${DIGEST_A}" });
|
||||
if (a === "sparse") { const failed = []; failed.length = 2 ** 32 - 1; failed[0] = "C1"; return { found: true, fit: "fail", definite: true, failed, line: "sparse" }; }
|
||||
return a;
|
||||
}` : ""}
|
||||
`;
|
||||
|
||||
// A scratch plugin tree whose harness directory holds only the named stand-ins, and its watch.
|
||||
async function watchTree(t, harnesses) {
|
||||
const tr = tree(t);
|
||||
for (const [name, opts] of Object.entries(harnesses)) tr.write("harness", name, standIn(name, opts));
|
||||
const { watchHarnesses } = await import(pathToFileURL(join(tr.pkg, "src", "harness-watch.mjs")).href);
|
||||
const dir = scratch(t);
|
||||
const file = join(dir, "answer.json");
|
||||
const pass = async (answers, { last = [], env = {}, timeoutMs = 2000 } = {}) => {
|
||||
writeFileSync(file, JSON.stringify(answers));
|
||||
return watchHarnesses({ last, env: { WATCH_ANSWER: file, ...env }, scratch: dir, timeoutMs });
|
||||
};
|
||||
return { pass, dir, tr, file };
|
||||
}
|
||||
|
||||
const ok = (digest, extra = {}) => ({ found: true, source: "PATH", version: "1.0.0", digest, fit: "pass", definite: true, failed: [], line: "passed", surfaces: {}, ...extra });
|
||||
const byName = (out) => Object.fromEntries(out.records.map((r) => [r.harness, r]));
|
||||
|
||||
test("W1 discovery: every harness the loader gives is watched, with no name list in the watch", async (t) => {
|
||||
// Names made for this run, so nothing in the watch could have listed them.
|
||||
const names = [`w${process.pid}-one`, `w${process.pid}-two`, `w${process.pid}-three`].sort();
|
||||
const { pass } = await watchTree(t, { [names[0]]: {}, [names[1]]: { inspect: false }, [names[2]]: {} });
|
||||
const out = await pass({ [names[0]]: ok(DIGEST_A), [names[2]]: ok(DIGEST_B) }, { env: { WATCH_VERSION: "3.1.4" } });
|
||||
assert.deepEqual(out.records.map((r) => r.harness).sort(), names);
|
||||
assert.deepEqual(out.refused, []);
|
||||
const r = byName(out);
|
||||
assert.equal(r[names[0]].via, "inspect");
|
||||
assert.equal(r[names[1]].via, "version");
|
||||
assert.equal(r[names[2]].digest, DIGEST_B);
|
||||
// The watch's source names no harness: no quoted name and no bare word for a shipped one.
|
||||
const src = readFileSync(WATCH_SRC, "utf8").replace(/^\s*\/\/.*$/gm, "");
|
||||
assert.doesNotMatch(src, /["'`](?:pi|claude|claude-code|codex)["'`]/);
|
||||
assert.doesNotMatch(src, /\b(?:claude|codex)\b|\bpi\b/i);
|
||||
});
|
||||
|
||||
test("W1 the version fallback: a harness with no inspect is watched through version() and is unknown", async (t) => {
|
||||
const { pass } = await watchTree(t, { plain: { inspect: false } });
|
||||
let rec = byName(await pass({}, { env: { WATCH_VERSION: "2.0.0" } })).plain;
|
||||
assert.deepEqual([rec.state, rec.via, rec.version, rec.fit, rec.found, rec.digest], ["unknown", "version", "2.0.0", "unknown", null, null]);
|
||||
rec = byName(await pass({}, {})).plain;
|
||||
assert.deepEqual([rec.state, rec.version], ["unknown", null]);
|
||||
rec = byName(await pass({}, { env: { WATCH_VERSION: "throw", WATCH_SECRET: "/srv/hidden-value" } })).plain;
|
||||
assert.deepEqual([rec.state, rec.line], ["unknown", "version failed"]);
|
||||
});
|
||||
|
||||
test("W1 each state from a pair of records: same digest, new digest, fail, undecided, throw, timeout, not found", async (t) => {
|
||||
const { pass } = await watchTree(t, { h: {} });
|
||||
const first = await pass({ h: ok(DIGEST_A) });
|
||||
const prior = first.records;
|
||||
assert.equal(byName(first).h.state, "fit");
|
||||
const cases = [
|
||||
["same digest, fit pass", ok(DIGEST_A), "fit"],
|
||||
["new digest, fit pass", ok(DIGEST_B), "changed"],
|
||||
["fit fail, definite", ok(DIGEST_B, { fit: "fail", definite: true, failed: ["C5"], line: "lacks --no-mcp" }), "red"],
|
||||
["fit fail, not definite", ok(DIGEST_B, { fit: "fail", definite: false }), "unknown"],
|
||||
["fit unknown", ok(DIGEST_B, { fit: "unknown" }), "unknown"],
|
||||
["inspect throws", "throw", "unknown"],
|
||||
["inspect times out", "hang", "unknown"],
|
||||
["binary not found", { found: false, source: null, version: null, digest: null, fit: "unknown", definite: false, failed: [], line: "no binary", surfaces: {} }, "missing"],
|
||||
// Answers that are not the contract's never read as fit.
|
||||
["fit spelled otherwise", ok(DIGEST_A, { fit: "PASS" }), "unknown"],
|
||||
["found not a boolean", ok(DIGEST_A, { found: "yes" }), "unknown"],
|
||||
["no answer", null, "unknown"],
|
||||
["a pass with found false", ok(DIGEST_A, { found: false }), "missing"],
|
||||
];
|
||||
for (const [what, answer, state] of cases) {
|
||||
const out = await pass({ h: answer }, { last: prior, timeoutMs: 300 });
|
||||
assert.equal(byName(out).h.state, state, what);
|
||||
}
|
||||
const red = byName(await pass({ h: ok(DIGEST_B, { fit: "fail", definite: true, failed: ["C5", "bad id with spaces"], line: "lacks --no-mcp" }) }, { last: prior })).h;
|
||||
assert.deepEqual([red.failed, red.line, red.definite], [["C5"], "lacks --no-mcp", true]);
|
||||
const late = byName(await pass({ h: "hang" }, { last: prior, timeoutMs: 300 })).h;
|
||||
assert.equal(late.line, "inspect did not answer in time");
|
||||
});
|
||||
|
||||
test("W1 a transition is reported only when the state or the digest changes", async (t) => {
|
||||
const { pass } = await watchTree(t, { h: {}, k: {} });
|
||||
const one = await pass({ h: ok(DIGEST_A), k: ok(DIGEST_A) });
|
||||
assert.deepEqual(one.transitions.map((x) => [x.harness, x.from, x.to.state]), [["h", null, "fit"], ["k", null, "fit"]]);
|
||||
// Same answers: no transition, and changedAt stays the first pass's.
|
||||
const two = await pass({ h: ok(DIGEST_A), k: ok(DIGEST_A, { version: "1.0.1", line: "another line" }) }, { last: one.records });
|
||||
assert.deepEqual(two.transitions, []);
|
||||
assert.equal(byName(two).h.changedAt, byName(one).h.changedAt);
|
||||
// A state change is a transition, with or without a new digest.
|
||||
const three = await pass({ h: ok(DIGEST_B), k: ok(DIGEST_A, { fit: "fail", definite: true }) }, { last: two.records });
|
||||
assert.deepEqual(three.transitions.map((x) => [x.harness, x.from.state, x.to.state, x.from.digest, x.to.digest]),
|
||||
[["h", "fit", "changed", DIGEST_A, DIGEST_B], ["k", "fit", "red", DIGEST_A, DIGEST_A]]);
|
||||
const four = await pass({ h: ok(DIGEST_B, { fit: "unknown" }), k: ok(DIGEST_A, { fit: "fail", definite: true }) }, { last: three.records });
|
||||
// A new digest with the state unchanged (unknown to unknown) is a transition too.
|
||||
const five = await pass({ h: ok(DIGEST_A, { fit: "unknown" }), k: ok(DIGEST_A, { fit: "fail", definite: true }) }, { last: four.records });
|
||||
assert.deepEqual(five.transitions.map((x) => [x.harness, x.from.state, x.to.state]), [["h", "unknown", "unknown"]]);
|
||||
// A harness the loader no longer gives is named in gone, and has no record.
|
||||
const six = await pass({ h: ok(DIGEST_A) }, { last: [...five.records, { harness: "removed", state: "fit", digest: DIGEST_A }] });
|
||||
assert.deepEqual(six.gone, ["removed"]);
|
||||
});
|
||||
|
||||
test("W1 a new binary first seen through an unknown, red or missing record still reads changed", async (t) => {
|
||||
const { pass } = await watchTree(t, { h: {} });
|
||||
const none = { found: true, source: "PATH", version: "2.0.0", digest: null, fit: "unknown", definite: false, failed: [], line: "timed out", surfaces: {} };
|
||||
for (const [what, between] of [["unknown", none], ["red", { ...none, fit: "fail", definite: true }], ["missing", { ...none, found: false }], ["timeout", "hang"], ["throw", "throw"]]) {
|
||||
const one = await pass({ h: ok(DIGEST_A) });
|
||||
const two = await pass({ h: between }, { last: one.records, timeoutMs: 200 });
|
||||
assert.equal(byName(two).h.lastDigest, DIGEST_A, what);
|
||||
const three = await pass({ h: ok(DIGEST_B) }, { last: two.records });
|
||||
assert.equal(byName(three).h.state, "changed", what);
|
||||
const four = await pass({ h: ok(DIGEST_B) }, { last: three.records });
|
||||
assert.equal(byName(four).h.state, "fit", what);
|
||||
}
|
||||
// With no digest on either record, a new version label is a new binary: red 2.0 to red 2.1 is a
|
||||
// transition, red 2.1 again is not.
|
||||
const red = (version, line) => ({ ...none, version, line, fit: "fail", definite: true });
|
||||
const a = await pass({ h: red("2.0.0", "lacks --no-skills") });
|
||||
const b = await pass({ h: red("2.1.0", "lacks --no-mcp") }, { last: a.records });
|
||||
assert.deepEqual(b.transitions.map((x) => [x.from.version, x.to.version, x.line]), [["2.0.0", "2.1.0", "lacks --no-mcp"]]);
|
||||
const c = await pass({ h: red("2.1.0", "lacks --no-mcp") }, { last: b.records });
|
||||
assert.deepEqual(c.transitions, []);
|
||||
// A pass that knows no version (a timeout or a throw) is not a new binary: unknown with 2.0,
|
||||
// then a timeout, then unknown with 2.0 again, then a throw gives no transition after the first.
|
||||
const d = await pass({ h: none }, { last: [] });
|
||||
const e = await pass({ h: "hang" }, { last: d.records, timeoutMs: 200 });
|
||||
const f = await pass({ h: { ...none, version: "2.0.0" } }, { last: e.records });
|
||||
const g = await pass({ h: "throw" }, { last: f.records });
|
||||
assert.deepEqual([d, e, f, g].map((x) => x.transitions.length), [1, 0, 0, 0]);
|
||||
assert.deepEqual([e, g].map((x) => byName(x).h.version), [null, null]);
|
||||
});
|
||||
|
||||
test("W1 an answer that throws when read, a Proxy or inherited fields is unknown, and the other harnesses still get records", async (t) => {
|
||||
const { pass, file } = await watchTree(t, { bad: {}, good: {} });
|
||||
for (const shape of ["getter", "proxy", "surfaces-proxy"]) {
|
||||
const out = await pass({ bad: shape, good: ok(DIGEST_A) }, { env: { WATCH_SECRET: "/srv/fixture-secret-place" } });
|
||||
const r = byName(out);
|
||||
assert.deepEqual([r.bad.state, r.bad.line, r.good.state], ["unknown", "inspect failed", "fit"], shape);
|
||||
assert.ok(!JSON.stringify(out).includes("fixture-secret"), shape);
|
||||
}
|
||||
// An inherited fit is not the module's answer.
|
||||
assert.equal(byName(await pass({ bad: "inherited", good: ok(DIGEST_A) })).bad.state, "unknown");
|
||||
// A huge sparse failed list is cut before it is read, so the pass ends at once.
|
||||
const began = Date.now();
|
||||
const sparse = byName(await pass({ bad: "sparse", good: ok(DIGEST_A) })).bad;
|
||||
assert.deepEqual([sparse.state, sparse.failed], ["red", ["C1"]]);
|
||||
assert.ok(Date.now() - began < 2000);
|
||||
// On a timeout the watch aborts the signal it passed.
|
||||
const late = byName(await pass({ bad: "hang-signal", good: ok(DIGEST_A) }, { timeoutMs: 200 })).bad;
|
||||
await new Promise((done) => setTimeout(done, 50));
|
||||
assert.equal(late.state, "unknown");
|
||||
assert.equal(readFileSync(file + ".aborted", "utf8"), "1");
|
||||
});
|
||||
|
||||
test("W1 fields are checked: a digest that is a path is dropped, version and line keep one printable line", async (t) => {
|
||||
const { pass, dir } = await watchTree(t, { h: {} });
|
||||
const r = byName(await pass({ h: ok(dir, { version: "1.0\u0007beta\nsecond line", line: "first\r\nsecond", surfaces: { help: "a\nb" } }) })).h;
|
||||
assert.deepEqual([r.digest, r.version, r.line, r.surfaces.help], [null, "1.0 beta", "first", "a\nb"]);
|
||||
});
|
||||
|
||||
test("W1 redaction covers real paths, trailing slashes, the user name, rule ids, sources, loose paths and the loader's refusals", async (t) => {
|
||||
const { pass, dir, tr } = await watchTree(t, { h: {} });
|
||||
mkdirSync(join(dir, "real", "home"), { recursive: true });
|
||||
mkdirSync(join(dir, "real", "bin"), { recursive: true });
|
||||
symlinkSync(join(dir, "real", "home"), join(dir, "linkhome"));
|
||||
symlinkSync(join(dir, "real", "bin"), join(dir, "linkbin"));
|
||||
const realHome = realpathSync(join(dir, "linkhome"));
|
||||
const realBin = realpathSync(join(dir, "linkbin"));
|
||||
// A symlinked scratch: the answer names its real path.
|
||||
const linkScratch = join(dir, "linkscratch");
|
||||
symlinkSync(dir, linkScratch);
|
||||
const env = { HOME: join(dir, "linkhome") + "/", PATH: `${join(dir, "linkbin")}:/usr/bin:/bin`, USER: "jq", WATCH_TOKEN: "sk-AbCdEf123456", WATCH_NAME: "fixturesource" };
|
||||
// Each rule on its own line, with what the line must read after. scratch: the one passed.
|
||||
const lines = {
|
||||
scratch: [`in ${dir}/x`, "in <scratch>/x", linkScratch],
|
||||
home: [`home ${realHome}`, "home <HOME>", dir],
|
||||
slash: [`home ${join(dir, "linkhome")}`, "home <HOME>", dir],
|
||||
bin: [`bin ${realBin}/claude`, "bin <PATH entry>/claude", dir],
|
||||
user: ["built by jq on host", "built by <user> on host", dir],
|
||||
loose: ["at /opt/notinenv/bin/tool and /etc/passwd", "at <path> and <path>", dir],
|
||||
// W2 review P3-5: a ~/ path, a file: URL and a path after a colon are paths now; a URL with a
|
||||
// host and a one-segment path stay.
|
||||
kept: ["see https://example.com/a/b, ~/x/y and /model", "see https://example.com/a/b, <path> and /model", dir],
|
||||
forms: ["file:///opt/fixture/bin host:/srv/fixture/b ~me/.local/x", "<path> host:<path> <path>", dir],
|
||||
};
|
||||
const { watchHarnesses } = await import(pathToFileURL(join(tr.pkg, "src", "harness-watch.mjs")).href);
|
||||
for (const [what, [line, want, at]] of Object.entries(lines)) {
|
||||
const file = join(dir, `answer-${what}.json`);
|
||||
writeFileSync(file, JSON.stringify({ h: ok(DIGEST_A, { line }) }));
|
||||
const out = await watchHarnesses({ env: { ...env, WATCH_ANSWER: file }, scratch: at });
|
||||
assert.equal(byName(out).h.line, want, what);
|
||||
}
|
||||
const rec = byName(await pass({ h: ok(DIGEST_A, { source: "fixturesource", failed: ["sk-AbCdEf123456", "C5"], line: `in ${realpathSync(dir)}` }) }, { env })).h;
|
||||
assert.deepEqual([rec.source, rec.failed], [null, ["C5"]]);
|
||||
assert.equal(rec.line, "in <scratch>");
|
||||
// The loader's refusal of a harness directory with no index.mjs names its path; the watch's copy doesn't.
|
||||
mkdirSync(join(tr.pkg, "harness", "noindex"));
|
||||
const out = await pass({ h: ok(DIGEST_A) });
|
||||
const refusal = out.refused.find((r) => r.name === "noindex");
|
||||
assert.ok(refusal && typeof refusal.message === "string", JSON.stringify(out.refused));
|
||||
assert.ok(!refusal.message.includes(tr.pkg), refusal.message);
|
||||
});
|
||||
|
||||
test("W1 no record holds a path under the scratch directory, the home directory or an env value", async (t) => {
|
||||
const { pass, dir } = await watchTree(t, { h: {} });
|
||||
const secret = "/srv/fixture-secret-place";
|
||||
const env = { WATCH_SECRET: secret, HOME: join(dir, "home"), MOSAIC_FIXTURE_BIN: "/opt/fixture/bin/tool", PATH: "/opt/fixture-tools/bin:/usr/bin:/bin" };
|
||||
const answer = ok(DIGEST_A, {
|
||||
source: dir, version: `9.9 at ${dir}/bin`,
|
||||
line: `found at ${env.MOSAIC_FIXTURE_BIN} in ${secret}, home ${env.HOME}, ${realpathSync(dir)}`,
|
||||
surfaces: { help: `usage: ${dir}/tool\nsee /opt/fixture-tools/bin/tool and ${tmpdir()}`, "Bad Name": dir },
|
||||
});
|
||||
const out = await pass({ h: answer }, { env });
|
||||
const text = JSON.stringify(out);
|
||||
for (const p of [dir, realpathSync(dir), secret, env.MOSAIC_FIXTURE_BIN, "/opt/fixture-tools/bin", env.HOME, tmpdir()]) assert.ok(!text.includes(p), p);
|
||||
const rec = byName(out).h;
|
||||
assert.equal(rec.source, null, "a source that is a path is dropped");
|
||||
assert.deepEqual(Object.keys(rec.surfaces), ["help"]);
|
||||
assert.match(rec.line, /<MOSAIC_FIXTURE_BIN>/);
|
||||
assert.match(rec.surfaces.help, /<scratch>\/tool/);
|
||||
});
|
||||
|
||||
test("W1 real pi from node_modules gives a version, a digest and fit pass", NO_BWRAP, async (t) => {
|
||||
const dir = scratch(t);
|
||||
const env = { PATH: "/usr/bin:/bin" };
|
||||
const r = await pi.inspect({ env, scratch: dir });
|
||||
const pkg = JSON.parse(readFileSync(join(REPO, "node_modules", PI_NAME, "package.json"), "utf8"));
|
||||
assert.equal(r.fit, "pass", r.line);
|
||||
assert.deepEqual([r.found, r.source, r.version, r.definite], [true, "repo", pkg.version, true]);
|
||||
assert.match(r.digest, /^[a-f0-9]{64}$/);
|
||||
const { watchHarnesses } = await import(pathToFileURL(WATCH_SRC).href);
|
||||
const out = await watchHarnesses({ env, scratch: dir });
|
||||
const rec = out.records.find((x) => x.via === "inspect" && x.source === "repo");
|
||||
assert.deepEqual([rec.state, rec.digest, rec.version], ["fit", r.digest, pkg.version]);
|
||||
const text = JSON.stringify(out);
|
||||
for (const p of [dir, REPO, realpathSync(join(REPO, "node_modules"))]) assert.ok(!text.includes(p), p);
|
||||
});
|
||||
|
||||
// A stand-in pi package named by MOSAIC_PI_BIN, as pi-fit.test.mjs makes one.
|
||||
function piPackage(t, knobs) {
|
||||
// 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" } }));
|
||||
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn({ version: "2.0.0", ...knobs }), { mode: 0o755 });
|
||||
mkdirSync(join(root, "prefix", "bin"));
|
||||
symlinkSync(`../lib/node_modules/${PI_NAME}/dist/bundle/cli.js`, join(root, "prefix", "bin", "pi"));
|
||||
return { root, env: { MOSAIC_PI_BIN: join(root, "prefix", "bin", "pi"), PATH: "/usr/bin:/bin" } };
|
||||
}
|
||||
|
||||
test("W1 pi inspect shares preflight's fit check: a binary that passed is not checked again", NO_BWRAP, async (t) => {
|
||||
const s = piPackage(t, {});
|
||||
// A fit check makes its scratch directories in TMPDIR. Control: preflight's check is seen making
|
||||
// them. Then inspect, on the binary that passed, must make none.
|
||||
const made = [];
|
||||
const watcher = watch(tmpdir(), (_, name) => { if (String(name).startsWith("mosaic-pi-fit-")) made.push(name); });
|
||||
t.after(() => watcher.close());
|
||||
const settle = () => new Promise((done) => setTimeout(done, 200));
|
||||
assert.equal(await pi.preflight({ env: { ...s.env, ZAI_API_KEY: "fixture-zai" } }), null);
|
||||
await settle();
|
||||
assert.ok(made.length > 0, "the watcher sees a fit check run");
|
||||
made.length = 0;
|
||||
const r = await pi.inspect({ env: s.env });
|
||||
await settle();
|
||||
assert.deepEqual(made, []);
|
||||
assert.deepEqual([r.fit, r.source, r.version], ["pass", "MOSAIC_PI_BIN", "2.0.0"]);
|
||||
assert.ok(!JSON.stringify(r).includes(s.root));
|
||||
});
|
||||
|
||||
test("W1 pi inspect: a definite failure is fail, one that can't be checked is unknown, no binary is not found", NO_BWRAP, async (t) => {
|
||||
const lacking = piPackage(t, { dropFlags: ["--no-skills"] });
|
||||
let r = await pi.inspect({ env: lacking.env });
|
||||
assert.deepEqual([r.found, r.fit, r.definite, r.version, r.line], [true, "fail", true, "2.0.0", "pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-skills"]);
|
||||
// W2: a failure carries the digest of the files the check ran.
|
||||
assert.match(r.digest, /^[a-f0-9]{64}$/);
|
||||
const flood = piPackage(t, { flood: true });
|
||||
r = await pi.inspect({ env: flood.env });
|
||||
assert.deepEqual([r.fit, r.definite], ["unknown", false], r.line);
|
||||
assert.match(r.line, /can't be checked/);
|
||||
r = await pi.inspect({ env: { MOSAIC_PI_BIN: "relative/pi", PATH: "/usr/bin:/bin" } });
|
||||
assert.deepEqual([r.found, r.source, r.line], [false, "MOSAIC_PI_BIN", "MOSAIC_PI_BIN isn't an executable file at an absolute path"]);
|
||||
});
|
||||
|
||||
const sha256 = (file) => new Promise((done, fail) => {
|
||||
const h = createHash("sha256");
|
||||
createReadStream(file).on("error", fail).on("data", (c) => h.update(c)).on("end", () => done(h.digest("hex")));
|
||||
});
|
||||
|
||||
const CLAUDE = process.env.MOSAIC_CLAUDE_BIN;
|
||||
test("W1 the real claude binary named by MOSAIC_CLAUDE_BIN gives a version, a sha256 and its help", CLAUDE ? {} : { skip: "MOSAIC_CLAUDE_BIN is not set" }, async (t) => {
|
||||
const dir = scratch(t);
|
||||
const env = { MOSAIC_CLAUDE_BIN: CLAUDE, PATH: "/usr/bin:/bin" };
|
||||
const r = await claude.inspect({ env, scratch: dir });
|
||||
// W2: the fit rule runs, and the real claude passes it.
|
||||
assert.deepEqual([r.found, r.source, r.fit, r.definite], [true, "MOSAIC_CLAUDE_BIN", "pass", true], r.line);
|
||||
assert.match(r.version, /\d+\.\d+\.\d+/);
|
||||
assert.equal(r.digest, await sha256(realpathSync(CLAUDE)));
|
||||
assert.match(r.surfaces.help, /--version/);
|
||||
const { watchHarnesses } = await import(pathToFileURL(WATCH_SRC).href);
|
||||
const out = await watchHarnesses({ env, scratch: dir });
|
||||
const rec = out.records.find((x) => x.source === "MOSAIC_CLAUDE_BIN");
|
||||
assert.deepEqual([rec.state, rec.digest, rec.version], ["fit", r.digest, r.version]);
|
||||
assert.ok(!JSON.stringify(out).includes(CLAUDE));
|
||||
// The scratch HOME it ran in is gone.
|
||||
assert.deepEqual(readdirNames(dir), []);
|
||||
});
|
||||
|
||||
test("W1 claude-code inspect: no binary is not found, naming the variable; no scratch runs nothing", async (t) => {
|
||||
let r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: "relative/claude" }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.found, r.source, r.line], [false, "MOSAIC_CLAUDE_BIN", "MOSAIC_CLAUDE_BIN isn't an executable file at an absolute path"]);
|
||||
r = await claude.inspect({ env: { PATH: scratch(t) }, scratch: scratch(t) });
|
||||
assert.deepEqual([r.found, r.source], [false, null]);
|
||||
// A relative PATH entry is skipped, as pi's lookup skips it, though "bin" from this cwd holds a claude.
|
||||
const rel = scratch(t);
|
||||
mkdirSync(join(rel, "bin"));
|
||||
writeFileSync(join(rel, "bin", "claude"), "#!/bin/sh\necho 1.0.0\n", { mode: 0o755 });
|
||||
const cwd = process.cwd();
|
||||
process.chdir(rel);
|
||||
try {
|
||||
r = await claude.inspect({ env: { PATH: "bin" }, scratch: scratch(t) });
|
||||
} finally {
|
||||
process.chdir(cwd);
|
||||
}
|
||||
assert.deepEqual([r.found, r.source], [false, null]);
|
||||
const dir = scratch(t);
|
||||
mkdirSync(join(dir, "bin"));
|
||||
writeFileSync(join(dir, "bin", "claude"), "#!/bin/sh\necho ran >> \"$0.log\"\necho 1.2.3\n", { mode: 0o755 });
|
||||
r = await claude.inspect({ env: { PATH: join(dir, "bin") } });
|
||||
assert.deepEqual([r.found, r.source, r.version, r.surfaces], [true, "PATH", null, {}]);
|
||||
assert.equal(r.digest, await sha256(join(dir, "bin", "claude")));
|
||||
assert.deepEqual(readdirNames(join(dir, "bin")), ["claude"], "nothing ran without scratch");
|
||||
});
|
||||
|
||||
test("W1 claude-code inspect runs --version and --help with PATH and a scratch HOME only, then removes it", async (t) => {
|
||||
const dir = scratch(t);
|
||||
const work = scratch(t);
|
||||
mkdirSync(join(dir, "bin"));
|
||||
const bin = join(dir, "bin", "claude");
|
||||
writeFileSync(bin, "#!/bin/sh\nenv > \"$0.env$1\"\n[ \"$1\" = --version ] && echo '1.2.3 (stand-in)'\n[ \"$1\" = --help ] && echo 'usage: claude --version'\nexit 0\n", { mode: 0o755 });
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin, PATH: "/usr/bin:/bin", FIXTURE_TOKEN: "fixture-token-value" }, scratch: work });
|
||||
assert.deepEqual([r.found, r.source, r.version, r.surfaces.help], [true, "MOSAIC_CLAUDE_BIN", "1.2.3 (stand-in)", "usage: claude --version\n"]);
|
||||
for (const arg of ["--version", "--help"]) {
|
||||
const vars = Object.fromEntries(readFileSync(`${bin}.env${arg}`, "utf8").trim().split("\n").map((l) => [l.slice(0, l.indexOf("=")), l.slice(l.indexOf("=") + 1)]));
|
||||
assert.deepEqual(Object.keys(vars).filter((k) => !["PWD", "SHLVL", "_", "OLDPWD"].includes(k)).sort(), ["HOME", "PATH"], arg);
|
||||
assert.equal(vars.PATH, "/usr/bin:/bin");
|
||||
assert.ok(vars.HOME.startsWith(join(realpathSync(work), "claude-home-")), arg);
|
||||
}
|
||||
assert.deepEqual(readdirNames(work), [], "the scratch HOME is removed");
|
||||
});
|
||||
|
||||
test("W1 claude-code inspect: an aborted signal kills the capture it is running", async (t) => {
|
||||
const dir = scratch(t);
|
||||
mkdirSync(join(dir, "bin"));
|
||||
const bin = join(dir, "bin", "claude");
|
||||
writeFileSync(bin, "#!/bin/sh\necho $$ > \"$0.pid$1\"\nexec sleep 30\n", { mode: 0o755 });
|
||||
const stop = new AbortController();
|
||||
setTimeout(() => stop.abort(), 300);
|
||||
const began = Date.now();
|
||||
const r = await claude.inspect({ env: { MOSAIC_CLAUDE_BIN: bin }, scratch: scratch(t), signal: stop.signal });
|
||||
assert.ok(Date.now() - began < 5000);
|
||||
assert.deepEqual([r.found, r.version, r.surfaces], [true, null, {}]);
|
||||
const pid = Number(readFileSync(`${bin}.pid--version`, "utf8"));
|
||||
await new Promise((done) => setTimeout(done, 200));
|
||||
assert.throws(() => process.kill(pid, 0), { code: "ESRCH" });
|
||||
});
|
||||
|
||||
function readdirNames(dir) {
|
||||
return readdirSync(dir).sort();
|
||||
}
|
||||
@@ -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']);
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
@@ -39,6 +39,8 @@ async function copyRepo(t, script, version) {
|
||||
mkdirSync(mod, { recursive: true });
|
||||
for (const f of readdirSync(MODULE)) if (f.endsWith(".mjs")) copyFileSync(join(MODULE, f), join(mod, f));
|
||||
for (const p of readdirSync(join(REPO, "packages"))) if (p !== "plugins") symlinkSync(join(REPO, "packages", p), join(root, "packages", p));
|
||||
// The module imports packages/plugins/src (the stat key), so that links to the real tree too.
|
||||
symlinkSync(join(REPO, "packages", "plugins", "src"), join(root, "packages", "plugins", "src"));
|
||||
standIn(join(root, "node_modules"), script, version);
|
||||
mkdirSync(join(root, "node_modules", ".bin"));
|
||||
symlinkSync(`../${NAME}/dist/bundle/cli.js`, join(root, "node_modules", ".bin", "pi"));
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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'];
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -1313,6 +1313,23 @@
|
||||
if(harnessLoading || harnessSettings !== null || !canSend || isTester)return; harnessLoading=true;const epoch=orgEpoch;
|
||||
fetch('/api/proxy/plugins',{cache:'no-store',redirect:'manual'}).then(async r=>{const v=await r.json();if(epoch!==orgEpoch)return;if(!r.ok || !/^[a-f0-9]{64}$/.test(v.revision??'') || !Array.isArray(v.available?.harness) || !v.available.harness.every(n=>typeof n==='string') || !Array.isArray(v.plugins?.harness??[]))throw Error();harnessSettings=v;}).catch(()=>{if(epoch!==orgEpoch)return;harnessSettings=false;harnessReceipt='Harnesses could not be read.';}).finally(()=>{if(epoch!==orgEpoch)return;harnessLoading=false;if(routeOf().split('?')[0]==='/settings/routing')render(false);});
|
||||
}
|
||||
// #1873 W3: one line per harness, from what the bus host's harness watch last wrote (the plugins
|
||||
// answer's harnessWatch, read only). Red and missing read as a problem, in the danger colour r3
|
||||
// gives "can't start" (new launches on them refuse); a harness the watch has
|
||||
// not checked reads "not checked yet", never fit. A bus that gives no harnessWatch gets one line.
|
||||
const WATCH_WORD={fit:'fit',changed:'fit',red:'red',unknown:'unknown',missing:'missing'};
|
||||
function harnessWatchLine(e){
|
||||
const known=Object.hasOwn(WATCH_WORD,e.state), head=txt(e.name)+(known&&typeof e.version==='string'&&e.version?` ${txt(e.version)}`:'');
|
||||
if(!known)return `<li class="small muted" data-watch-state="unchecked">${head} · not checked yet</li>`;
|
||||
const time=v=>typeof v==='string'&&Number.isFinite(Date.parse(v))?v:null, parts=[head], at=e.state==='changed'&&time(e.changedAt)?`changed ${when(e.changedAt)}`:time(e.checkedAt)?`checked ${when(e.checkedAt)}`:'';
|
||||
if(at)parts.push(at);
|
||||
parts.push(WATCH_WORD[e.state]);
|
||||
if(['red','missing','unknown'].includes(e.state)&&typeof e.line==='string'&&e.line)parts.push(txt(e.line));
|
||||
if(e.state==='red')parts.push('new launches refuse');
|
||||
return `<li class="small ${e.state==='red'||e.state==='missing'?'danger':'muted'}" data-watch-state="${esc(e.state)}">${parts.join(' · ')}</li>`;
|
||||
}
|
||||
const harnessWatchLines=v=>{const list=Array.isArray(v.harnessWatch)?v.harnessWatch.filter(e=>e&&typeof e==='object'&&typeof e.name==='string'&&e.name):null;
|
||||
return list===null?'<p class="small muted harness-watch-none">What the harness check found could not be read.</p>':list.length?`<ul class="harness-watch" aria-label="What the last harness check found">${list.map(harnessWatchLine).join('')}</ul>`:'';};
|
||||
const ROUTE_CELLS=[['harness','Harness'],['model','Model'],['thinking','Thinking'],['fallback.harness','Fallback harness'],['fallback.model','Fallback model'],['fallback.thinking','Fallback thinking']];
|
||||
const routeCells=r=>({harness:r.harness??'',model:r.model??'',thinking:r.thinking??'','fallback.harness':r.fallback?.harness??'','fallback.model':r.fallback?.model??'','fallback.thinking':r.fallback?.thinking??''});
|
||||
const sameCells=(a,b)=>ROUTE_CELLS.every(([k])=>a[k]===b[k]);
|
||||
@@ -1429,7 +1446,7 @@
|
||||
// models after a restart; that notice gets the organization's Restart (not primary: Save routes is).
|
||||
const catalogueInfo=v.catalogue?.status==='ready'?`<p class="small muted">Model catalogue, observed ${txt(window.MosaicLocalTime(v.catalogue.observedAt))}.</p>${(v.catalogue.notices??[]).map(n=>`<p class="small warn">${txt(n)}${n===window.MosaicConsole?.RESTART_TO_LAUNCH&&window.MosaicOrg?.id?` ${window.MosaicConsole.orgRestartButton(window.MosaicOrg.id,{name:window.MosaicOrg.label||window.MosaicOrg.id})}`:''}</p>`).join('')}`:v.catalogue?.status==='unavailable'?catalogueLine(proxyOn,v.catalogue):'<p class="small muted">Model catalogue not enabled.</p>';
|
||||
const providerControl=canSend&&!isTester?modelsChoice(/ data-primary[ >]/.test(catalogueInfo)):'';
|
||||
const harnessControl=canSend&&!isTester?`<fieldset class="harness-control"><legend>Harnesses</legend>${harnessSettings ? [...new Set([...harnessSettings.available.harness,...(harnessSettings.plugins.harness??[])])].map(name=>`<label><input type="checkbox" data-harness="${esc(name)}"${(harnessDraft??harnessSettings.plugins.harness??[]).includes(name)?' checked':''}${harnessBusy?' disabled':''}> ${txt(name)}</label>`).join('')+` <button type="button" id="harness-save"${harnessBusy?' disabled':''}>Save harnesses</button>` : '<p>Harnesses unavailable.</p><button type="button" id="harness-read">Read harnesses again</button>'}<p role="status" id="harness-status">${txt(harnessReceipt)}</p></fieldset>`:'';
|
||||
const harnessControl=canSend&&!isTester?`<fieldset class="harness-control"><legend>Harnesses</legend>${harnessSettings ? [...new Set([...harnessSettings.available.harness,...(harnessSettings.plugins.harness??[])])].map(name=>`<label><input type="checkbox" data-harness="${esc(name)}"${(harnessDraft??harnessSettings.plugins.harness??[]).includes(name)?' checked':''}${harnessBusy?' disabled':''}> ${txt(name)}</label>`).join('')+` <button type="button" id="harness-save"${harnessBusy?' disabled':''}>Save harnesses</button>`+harnessWatchLines(harnessSettings) : '<p>Harnesses unavailable.</p><button type="button" id="harness-read">Read harnesses again</button>'}<p role="status" id="harness-status">${txt(harnessReceipt)}</p></fieldset>`:'';
|
||||
const off=v.harnesses.length?'':'<p class="small warn">No harness is switched on for this organization, so every launch is refused, and so is a save that changes a harness. Switch one on under Harnesses.</p>';
|
||||
return `<section class="panel routing-panel" aria-labelledby="routes-title"><span class="sr-only">${title}</span>${catalogueInfo}${providerControl}${harnessControl}${off}<form id="routes-form"><div class="table-wrap" tabindex="0" role="region" aria-label="Routes, scroll for all columns"><table id="routes-table"><thead><tr><th scope="col" rowspan="2">Agent</th><th scope="colgroup" colspan="3">Primary</th><th scope="colgroup" colspan="3">Fallback</th></tr><tr>${ROUTE_CELLS.map(([,l])=>`<th scope="col">${l.replace('Fallback ','')}</th>`).join('')}</tr></thead><tbody>${rows}</tbody></table></div><p class="small muted">${canSend || isTester?'Changes are checked and logged with your identity.':'Saving requires signed-in send mode.'}</p><div class="settings-actions route-actions${routesDraft?' has-draft':''}"><button type="submit"${/ data-primary[ >]/.test(catalogueInfo)?' class="route-save-quiet"':''}${disabled}>${routesBusy?'<span class="settings-spinner" aria-hidden="true"></span> Saving…':'Save routes'}</button> <button type="button" id="routes-reload"${routesDraft?'':' hidden'}${routesBusy||!routesDraft?' disabled':''}>Discard draft</button><p role="status" id="routes-receipt" class="${routesReceipt.startsWith('Outcome unknown')?'banner ref':routesReceipt.includes('Draft kept')?'banner err':routesReceipt.includes('saved')?'settings-success':''}">${txt(routesReceipt)}</p></div>${routesApplyMarkup()}</form></section>`;
|
||||
}
|
||||
@@ -1472,7 +1489,7 @@
|
||||
const used=old.filter(n=>!selected.includes(n) && Object.values(routesView.instances).some(r=>r.harness===n || r.fallback?.harness===n));
|
||||
if(used.length){harnessReceipt=`A saved route uses ${used.join(', ')}. Change the route before switching it off.`;render(false);return;}
|
||||
harnessBusy=true;const button=e.target;button.disabled=true;harnessReceipt='';const epoch=orgEpoch;
|
||||
try{const response=await fetch('/api/proxy/plugins/set',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({revision:harnessSettings.revision,plugins:{harness:selected}}),cache:'no-store',redirect:'manual'});const result=await response.json();if(epoch!==orgEpoch)return;if(!response.ok){harnessReceipt=result.message||result.error||'Harness setting was refused.';return;}harnessSettings={...result,available:harnessSettings.available};harnessDraft=null;harnessReceipt='Harnesses saved. The next launch uses the new settings.';routesView=null;last.delete('routes');}
|
||||
try{const response=await fetch('/api/proxy/plugins/set',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({revision:harnessSettings.revision,plugins:{harness:selected}}),cache:'no-store',redirect:'manual'});const result=await response.json();if(epoch!==orgEpoch)return;if(!response.ok){harnessReceipt=result.message||result.error||'Harness setting was refused.';return;}harnessSettings={...result,available:harnessSettings.available,harnessWatch:result.harnessWatch??harnessSettings.harnessWatch};harnessDraft=null;harnessReceipt='Harnesses saved. The next launch uses the new settings.';routesView=null;last.delete('routes');}
|
||||
catch{if(epoch!==orgEpoch)return;harnessReceipt='Outcome unknown. Read the harnesses again before trying.';harnessSettings=false;}
|
||||
finally{if(epoch===orgEpoch){harnessBusy=false;if(!out())render(false);}}
|
||||
});
|
||||
|
||||
@@ -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); }
|
||||
|
||||
@@ -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}
|
||||
@@ -388,6 +390,9 @@ button:disabled,[aria-disabled=true]{cursor:not-allowed}
|
||||
.settings-content fieldset.harness-control{display:flex;flex-wrap:wrap;align-items:center;gap:6px 12px}
|
||||
.settings-content fieldset.harness-control>legend{flex-basis:100%}
|
||||
.settings-content fieldset.harness-control>:is(button,p){flex-basis:100%}
|
||||
.settings-content fieldset.harness-control>ul.harness-watch{flex-basis:100%;margin:0;padding:0;list-style:none;display:grid;gap:4px}
|
||||
.settings-content fieldset.harness-control>ul.harness-watch>li{margin:0;overflow-wrap:anywhere}
|
||||
.settings-content fieldset.harness-control>ul.harness-watch>li.danger{color:var(--danger);font-weight:600}
|
||||
/* #1873 settings slice 5: an organization's one choice (mockup org-models). A choice not built yet is dashed and disabled. */
|
||||
/* settings-s5 review P3-4: the choice cards sit in the form column, as .settings-form does. */
|
||||
.settings-content fieldset.models-source{display:grid;gap:8px;max-width:28rem}
|
||||
|
||||
@@ -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/);
|
||||
});
|
||||
@@ -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));
|
||||
});
|
||||
@@ -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,104 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { browser } from './browser.mjs';
|
||||
import { startServer } from '../src/serve.mjs';
|
||||
import { close } from './fixture.mjs';
|
||||
|
||||
// #1873 harness watch, slice W3: Settings > Routing shows one line per harness from the plugins
|
||||
// answer's harnessWatch. Red and missing read as a problem, a harness the watch has not checked
|
||||
// reads "not checked yet" and never fit, and a bus that sends no harnessWatch gets one plain line.
|
||||
// The page is served on port 0 and the bus answers are stubbed in the page; no live bus is used.
|
||||
const initial = { business: 'acme', revision: 'a'.repeat(64), harnesses: ['pi'], models: ['first'], providerModels: {}, levels: { pi: ['low'] }, instances: { coder: { harness: 'pi', model: 'first', thinking: 'low', fallback: null } }, catalogue: { status: 'disabled', models: [], notices: [] } };
|
||||
const recent = new Date(Date.now() - 4 * 60 * 1000).toISOString();
|
||||
const watch = [
|
||||
{ name: 'pi', state: 'fit', version: '1.6.0', checkedAt: recent, changedAt: recent, failed: [], line: 'pi 1.6.0 passed the fit check' },
|
||||
{ name: 'claude-code', state: 'changed', version: '2.1.323', checkedAt: recent, changedAt: recent, failed: [], line: 'claude-code 2.1.323 passed the fit check' },
|
||||
{ name: 'w3-red', state: 'red', version: '2.1.330', checkedAt: recent, changedAt: recent, failed: ['--restricted'], line: '--restricted is gone' },
|
||||
{ name: 'w3-missing', state: 'missing', version: null, checkedAt: recent, changedAt: recent, failed: [], line: 'no binary from MOSAIC_PI_BIN, PATH or the repo copy' },
|
||||
{ name: 'w3-unknown', state: 'unknown', version: '0.9.0', checkedAt: recent, changedAt: recent, failed: [], line: 'the fit check could not run' },
|
||||
{ name: 'w3-new', state: 'unchecked', version: null, checkedAt: null, changedAt: null, failed: [], line: 'not checked yet' },
|
||||
{ name: 'w3-odd', state: 'green', version: '9.9.9', checkedAt: recent, changedAt: recent, failed: [], line: 'fit' },
|
||||
];
|
||||
const settings = (extra) => ({ business: 'acme', revision: initial.revision, plugins: { harness: ['pi'] }, available: { harness: ['pi', 'claude-code'] }, ...extra });
|
||||
|
||||
async function routing(view, shot, act = null) {
|
||||
const web = await startServer({ port: 0, board: 'http://127.0.0.1:9', bus: { messages: async () => ({ messages: [], unread: 0 }), usage: async () => { throw Error('fixture'); }, routes: async () => initial }, reads: { settings: () => ({ business: 'acme', release: 'fixture', notifier: {} }) }, log() {} });
|
||||
let b;
|
||||
try {
|
||||
b = await browser();
|
||||
const errors = [];
|
||||
b.on('Runtime.exceptionThrown', x => errors.push(x.exceptionDetails?.exception?.description ?? x.exceptionDetails?.text));
|
||||
await b.call('Page.addScriptToEvaluateOnNewDocument', { source: `window.routeView=${JSON.stringify(initial)};window.harnessView=${JSON.stringify(view)};const original=window.fetch;window.fetch=async(u,o={})=>{if(u==='/api/config')return new Response(JSON.stringify({send:true}));if(u==='/api/proxy/plugins')return new Response(JSON.stringify(harnessView));if(u==='/api/bus/routes')return new Response(JSON.stringify({rows:[routeView],at:new Date().toISOString()}));if(u==='/api/proxy/plugins/set')return new Response(JSON.stringify(window.saveAnswer));return original(u,o);};` });
|
||||
const wait = expr => b.evaluate(`(async()=>{for(let n=0;n<500;n++){if(${expr})return true;await new Promise(r=>setTimeout(r,20));}throw Error('wait: '+${JSON.stringify(expr)});})()`);
|
||||
if (shot) await b.viewport(1440, 900);
|
||||
await b.navigate(`http://127.0.0.1:${web.address().port}/#/settings/routing`);
|
||||
await wait('!!document.querySelector("#harness-save")');
|
||||
if (act) await act(b, wait);
|
||||
const page = await b.evaluate(`(()=>{const f=document.querySelector("fieldset.harness-control");return {
|
||||
lines:[...f.querySelectorAll("ul.harness-watch>li")].map(li=>({state:li.dataset.watchState,text:li.textContent,danger:li.classList.contains("danger"),muted:li.classList.contains("muted"),color:getComputedStyle(li).color})),
|
||||
none:f.querySelector(".harness-watch-none")?.textContent??null,
|
||||
label:f.querySelector("ul.harness-watch")?.getAttribute("aria-label")??null,
|
||||
text:f.textContent,
|
||||
muted:(()=>{const p=document.createElement("p");p.className="small muted";f.append(p);const c=getComputedStyle(p).color;p.remove();return c;})(),
|
||||
danger:(()=>{const p=document.createElement("p");p.style.color="var(--danger)";f.append(p);const c=getComputedStyle(p).color;p.remove();return c;})(),
|
||||
};})()`);
|
||||
if (shot) {
|
||||
await b.evaluate('document.querySelector("fieldset.harness-control").scrollIntoView({block:"center"});true');
|
||||
await b.screenshot(shot);
|
||||
}
|
||||
assert.deepEqual(errors, []);
|
||||
return page;
|
||||
} finally { await b?.close(); await close(web); }
|
||||
}
|
||||
|
||||
test('W3 Routing shows one line per harness for each watch state, red and missing as a problem, unchecked never as fit', { timeout: 60000 }, async () => {
|
||||
const page = await routing(settings({ harnessWatch: watch }), process.env.W3_SHOT || null);
|
||||
assert.equal(page.none, null);
|
||||
assert.equal(page.label, 'What the last harness check found');
|
||||
assert.deepEqual(page.lines.map(l => l.state), ['fit', 'changed', 'red', 'missing', 'unknown', 'unchecked', 'unchecked']);
|
||||
const [fit, changed, red, missing, unknown, unchecked, odd] = page.lines;
|
||||
assert.equal(fit.text, 'pi 1.6.0 · checked 4 min ago · fit');
|
||||
assert.equal(changed.text, 'claude-code 2.1.323 · changed 4 min ago · fit');
|
||||
assert.equal(red.text, 'w3-red 2.1.330 · checked 4 min ago · red · --restricted is gone · new launches refuse');
|
||||
assert.equal(missing.text, 'w3-missing · checked 4 min ago · missing · no binary from MOSAIC_PI_BIN, PATH or the repo copy');
|
||||
assert.equal(unknown.text, 'w3-unknown 0.9.0 · checked 4 min ago · unknown · the fit check could not run');
|
||||
assert.equal(unchecked.text, 'w3-new · not checked yet');
|
||||
assert.equal(odd.text, 'w3-odd · not checked yet', 'a state the page does not know is not checked yet, not its line and not fit');
|
||||
for (const l of [red, missing]) assert.ok(l.danger && !l.muted, `${l.state} reads as a problem`);
|
||||
for (const l of [fit, changed, unknown, unchecked, odd]) assert.ok(!l.danger && l.muted, `${l.state} is not a problem line`);
|
||||
assert.notEqual(red.color, page.muted, 'a problem line is not drawn in the muted colour');
|
||||
for (const l of [red, missing]) assert.equal(l.color, page.danger, `${l.state} is drawn in the danger colour, as r3 gives can't start`);
|
||||
for (const l of [unknown, unchecked, odd]) assert.doesNotMatch(l.text, /· fit\b/, `${l.state} never reads as fit`);
|
||||
});
|
||||
|
||||
test('W3 Routing: a plugins answer with no harnessWatch, or one that is not a list, gives one plain line and no guess', { timeout: 60000 }, async () => {
|
||||
for (const extra of [{}, { harnessWatch: { pi: { state: 'fit' } } }, { harnessWatch: 'fit' }, { harnessWatch: null }]) {
|
||||
const page = await routing(settings(extra));
|
||||
assert.deepEqual(page.lines, [], JSON.stringify(extra));
|
||||
assert.equal(page.none, 'What the harness check found could not be read.', JSON.stringify(extra));
|
||||
assert.doesNotMatch(page.text, /\bfit\b/, JSON.stringify(extra));
|
||||
}
|
||||
const empty = await routing(settings({ harnessWatch: [] }));
|
||||
assert.deepEqual([empty.lines, empty.none], [[], null], 'an empty list (no harness installed) adds nothing');
|
||||
});
|
||||
|
||||
test('W3 Routing: a bus-authored line or version is shown as text, never as markup, and a time that is not a time is not shown', { timeout: 60000 }, async () => {
|
||||
const page = await routing(settings({ harnessWatch: [{ name: 'pi', state: 'red', version: '<b>1</b>', checkedAt: recent, changedAt: recent, failed: [], line: '<img src=x onerror="window.pwned=1">gone' }, { name: 'w3-time', state: 'fit', version: '1', checkedAt: '/srv/host/place', changedAt: '/srv/host/place', failed: [], line: 'x' }] }));
|
||||
assert.equal(page.lines.length, 2);
|
||||
assert.equal(page.lines[1].text, 'w3-time 1 · fit', 'a time that is not a time is left out, not shown');
|
||||
assert.match(page.lines[0].text, /<b>1<\/b>/);
|
||||
assert.match(page.lines[0].text, /<img src=x/);
|
||||
});
|
||||
|
||||
test('W3 Routing: after Save harnesses the lines stay, or take the save answer\'s own harnessWatch', { timeout: 60000 }, async () => {
|
||||
const save = (answer) => async (b, wait) => {
|
||||
await b.evaluate(`window.saveAnswer=${JSON.stringify(answer)};document.querySelector("[data-harness=claude-code]").click();document.querySelector("#harness-save").click();true`);
|
||||
await wait('document.querySelector("#harness-status")?.textContent.includes("Harnesses saved")');
|
||||
};
|
||||
const saved = { business: 'acme', revision: 'c'.repeat(64), plugins: { harness: ['pi', 'claude-code'] }, transaction: 'fixture' };
|
||||
const kept = await routing(settings({ harnessWatch: watch.slice(0, 3) }), null, save(saved));
|
||||
assert.equal(kept.none, null, 'a save answer with no harnessWatch keeps the lines read before');
|
||||
assert.deepEqual(kept.lines.map(l => l.state), ['fit', 'changed', 'red']);
|
||||
const fresh = await routing(settings({ harnessWatch: watch.slice(0, 3) }), null, save({ ...saved, harnessWatch: [watch[3]] }));
|
||||
assert.deepEqual(fresh.lines.map(l => l.state), ['missing'], 'a save answer with harnessWatch shows its lines');
|
||||
});
|
||||
@@ -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);
|
||||
|
||||
@@ -66,9 +66,9 @@ fi
|
||||
USER_FILE="${USER_FILE:-$MOSAIC_DATA_ROOT/user/USER.md}"
|
||||
PI="$REPO/node_modules/.bin/pi"
|
||||
[ -x "$PI" ] || fail 'run npm ci --ignore-scripts --no-audit --no-fund in the repository first'
|
||||
PIN="$(node -p "require('./package.json').dependencies['@earendil-works/pi-coding-agent']")"
|
||||
PI_VERSION="$("$PI" --version)"
|
||||
[ "$PI_VERSION" = "$PIN" ] || fail "local Pi is $PI_VERSION, package.json pins $PIN; run npm ci in $REPO"
|
||||
# #1873 S5: no pin. The version pi prints is recorded for the banner, never compared.
|
||||
PI_VERSION="$("$PI" --version 2>/dev/null | head -n 1)" || true
|
||||
[ -n "$PI_VERSION" ] || PI_VERSION="version unknown"
|
||||
command -v flock >/dev/null || fail 'flock is required'
|
||||
CONTEXT_FILES=("$CONSTITUTION" "$REPO/contracts/STANDARDS.md" "$SOUL" "$USER_FILE" "$REPO/AGENTS.md" "$REPO/agents/$AGENT/CONTEXT.md")
|
||||
for file in "${CONTEXT_FILES[@]}"; do
|
||||
@@ -83,7 +83,7 @@ for skill in "${SKILLS[@]}"; do
|
||||
done
|
||||
[ -s "$REPO/extensions/goal/index.ts" ] || fail 'missing canonical goal extension'
|
||||
STATE="$REPO/.pi/state/$AGENT"
|
||||
echo "$AGENT: native host TUI | Pi $PIN | $PROVIDER/$MODEL${THINKING:+:$THINKING}"
|
||||
echo "$AGENT: native host TUI | Pi $PI_VERSION | $PROVIDER/$MODEL${THINKING:+:$THINKING}"
|
||||
echo "$AGENT: workspace $REPO"
|
||||
echo "$AGENT: SOUL=$SOUL | CONSTITUTION=$CONSTITUTION | USER=$USER_FILE"
|
||||
echo "$AGENT: sessions $STATE/sessions | skills: ${SKILLS[*]}"
|
||||
|
||||
+3
-2
@@ -1,5 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
# Build the mosaic-agent container image using Docker Compose.
|
||||
# Build the mosaic-agent container image through build_image in common.sh: the image fit step,
|
||||
# then the build. compose.yaml has no build section, so this is how the image gets built.
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
# shellcheck source=common.sh
|
||||
@@ -10,4 +11,4 @@ load_release
|
||||
|
||||
bootstrap_runtime_dir
|
||||
|
||||
docker compose build
|
||||
build_image
|
||||
+22
-4
@@ -26,17 +26,35 @@ load_config() {
|
||||
|
||||
# Resolve the release identity: RELEASE is the single source of the
|
||||
# release version (stays 0.0.X until declared stable); the image tag
|
||||
# derives from it plus the pinned pi dependency version.
|
||||
# derives from it plus the pi version package-lock.json resolves (#1873 S5:
|
||||
# package.json gives pi a range, never a pin, and the tag never carries it).
|
||||
# scripts/image-tag.mjs builds the tag and refuses a lock it can't read.
|
||||
load_release() {
|
||||
local release pi_version
|
||||
local release tag
|
||||
release="$(tr -d '[:space:]' < RELEASE)"
|
||||
if ! printf '%s' "$release" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$'; then
|
||||
echo "common: RELEASE must be a semver-ish version, got: '$release'" >&2
|
||||
exit 1
|
||||
fi
|
||||
pi_version="$(node -p "require('./package.json').dependencies['@earendil-works/pi-coding-agent']")"
|
||||
tag="$(node scripts/image-tag.mjs .)" || exit 1
|
||||
export MOSAIC_RELEASE="$release"
|
||||
export MOSAIC_IMAGE_TAG="mosaic-poc-agent:${pi_version}-r${release}"
|
||||
export MOSAIC_IMAGE_TAG="$tag"
|
||||
}
|
||||
|
||||
# Build the agent image (#1873 S5). The image fit step runs first: it runs the
|
||||
# fit check on the pi package-lock.json resolves, as npm ci installed it here,
|
||||
# and refuses with one line when that pi is unfit or the check can't tell, so a
|
||||
# lock that resolves an unfit pi fails the build. scripts/pi-fit-image.mjs
|
||||
# says why the step runs on the host and not inside the image build.
|
||||
# compose.yaml has no build section, so this is the only way the scripts build
|
||||
# the image; compose run refuses a missing one. Call load_release first.
|
||||
build_image() {
|
||||
if [ -z "${MOSAIC_IMAGE_TAG:-}" ]; then
|
||||
echo "common: build_image needs MOSAIC_IMAGE_TAG from load_release" >&2
|
||||
exit 1
|
||||
fi
|
||||
node scripts/pi-fit-image.mjs || exit 1
|
||||
docker build -f Containerfile -t "$MOSAIC_IMAGE_TAG" .
|
||||
}
|
||||
|
||||
# Ensure the configured runtime data directory exists and carries this
|
||||
|
||||
Executable
+54
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env node
|
||||
// #1873 pi-unpin S5: the agent image tag. package.json gives pi a range (no pin); the version the
|
||||
// image installs is the one package-lock.json resolves, since the Containerfile runs npm ci. So the
|
||||
// tag is built from the lock's entry for pi and RELEASE, never from the package.json string:
|
||||
// mosaic-poc-agent:<lock version>-r<RELEASE>. A lock or RELEASE this can't read refuses.
|
||||
//
|
||||
// node scripts/image-tag.mjs [checkout] prints the tag (checkout: default this repository)
|
||||
import { readFileSync, realpathSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
export const PI_PACKAGE = "@earendil-works/pi-coding-agent";
|
||||
export const PI_LOCK_KEY = `node_modules/${PI_PACKAGE}`;
|
||||
const VERSION = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/;
|
||||
const RELEASE = /^\d+\.\d+\.\d+$/;
|
||||
|
||||
const fail = (message) => { throw new Error(`image tag: ${message}`); };
|
||||
const readJson = (path, name) => {
|
||||
let text;
|
||||
try { text = readFileSync(path, "utf8"); } catch { fail(`${name} can't be read`); }
|
||||
try { return JSON.parse(text); } catch { fail(`${name} isn't valid JSON`); }
|
||||
};
|
||||
|
||||
// The lock's entry for pi: { version, resolved, integrity }. Refuses a lock with no entry, or one
|
||||
// whose version isn't a plain semver.
|
||||
export function lockedPi(root) {
|
||||
const lock = readJson(join(root, "package-lock.json"), "package-lock.json");
|
||||
const entry = lock?.packages?.[PI_LOCK_KEY];
|
||||
if (!entry || typeof entry !== "object") fail(`package-lock.json has no entry for ${PI_PACKAGE}`);
|
||||
if (typeof entry.version !== "string" || !VERSION.test(entry.version)) fail(`package-lock.json's ${PI_PACKAGE} version isn't a plain version`);
|
||||
return { version: entry.version, resolved: entry.resolved ?? null, integrity: entry.integrity ?? null };
|
||||
}
|
||||
|
||||
export function release(root) {
|
||||
let text;
|
||||
try { text = readFileSync(join(root, "RELEASE"), "utf8"); } catch { fail("RELEASE can't be read"); }
|
||||
const value = text.replace(/\s/g, "");
|
||||
if (!RELEASE.test(value)) fail("RELEASE must be a version like 0.0.13");
|
||||
return value;
|
||||
}
|
||||
|
||||
export const imageTag = (root) => `mosaic-poc-agent:${lockedPi(root).version}-r${release(root)}`;
|
||||
|
||||
const self = fileURLToPath(import.meta.url);
|
||||
let main = false;
|
||||
try { main = realpathSync(process.argv[1] ?? "") === realpathSync(self); } catch {}
|
||||
if (main) {
|
||||
try {
|
||||
process.stdout.write(`${imageTag(process.argv[2] ?? join(self, "..", ".."))}\n`);
|
||||
} catch (e) {
|
||||
process.stderr.write(`${e.message}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,7 @@ import process from "node:process";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { imageTag } from "./image-tag.mjs";
|
||||
import { BusinessError, validateRoleDocument } from "../packages/organization/src/index.mjs";
|
||||
import { RunsError, isRunId, listRunRecords, readRunRecord } from "../packages/runs/src/index.mjs";
|
||||
import { initializeControl, assertProcessing } from "../packages/processing-control/src/state.mjs";
|
||||
@@ -339,6 +340,10 @@ function runTask(taskFile, options = {}) {
|
||||
catch (e) { fail(3, e.code?.startsWith('processing-') ? e.code : 'processing-state-invalid'); }
|
||||
|
||||
const task = validateTask(readJsonFile(taskFile, "task file"), path.resolve(taskFile));
|
||||
// #1873 S5: the image tag carries the pi version package-lock.json resolves, never the
|
||||
// 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); }
|
||||
const runId = `r-${utcStamp()}-${randomBytes(3).toString("hex")}`;
|
||||
const runDir = path.join(runsRoot(resolved), runId);
|
||||
|
||||
@@ -367,9 +372,7 @@ function runTask(taskFile, options = {}) {
|
||||
// (Names here are the compose interpolation consumers, not PI_*.)
|
||||
spawnEnv.MOSAIC_PROVIDER = resolved.execution.provider;
|
||||
spawnEnv.MOSAIC_MODEL = resolved.execution.model;
|
||||
const release = fs.readFileSync(path.join(PROJECT_ROOT, "RELEASE"), "utf8").trim();
|
||||
const piVersion = JSON.parse(fs.readFileSync(path.join(PROJECT_ROOT, "package.json"), "utf8")).dependencies["@earendil-works/pi-coding-agent"];
|
||||
spawnEnv.MOSAIC_IMAGE_TAG = `mosaic-poc-agent:${piVersion}-r${release}`;
|
||||
spawnEnv.MOSAIC_IMAGE_TAG = tag;
|
||||
|
||||
// Workspace (M5): create only the selected host source. The container
|
||||
// launcher gives it the fixed /workspace target and mounts no parent.
|
||||
|
||||
Executable
+72
@@ -0,0 +1,72 @@
|
||||
#!/usr/bin/env node
|
||||
// #1873 pi-unpin S5: the image fit step. The agent image installs the pi package-lock.json resolves
|
||||
// (npm ci), and nothing pins its version. This step runs the fit check (packages/harness/src/
|
||||
// pi-fit.mjs, as sessions run it) on that pi before the image is built, so a lock that resolves an
|
||||
// unfit pi fails the build. scripts/build.sh, scripts/release.sh package, scripts/verify.sh and the
|
||||
// gate's image preparation run it first.
|
||||
//
|
||||
// It runs on the host, not as a Containerfile RUN step: the fit check runs pi inside bwrap, and
|
||||
// docker build's default seccomp profile refuses the namespaces bwrap needs. The host copy stands for
|
||||
// the image's: npm ci installs both from the same lock entry, and this step refuses unless the
|
||||
// checkout's node_modules holds exactly that entry (version, resolved and integrity, as npm recorded
|
||||
// them in node_modules/.package-lock.json, and the installed package.json's version).
|
||||
// It runs pi on the host's node, which can be a different version from the image's (the
|
||||
// Containerfile's FROM). Running it on the image's node is left for later.
|
||||
//
|
||||
// node scripts/pi-fit-image.mjs exit 0: passed; exit 1: one line on stderr, unfit or can't tell
|
||||
import { readFileSync, realpathSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { lockedPi, PI_LOCK_KEY } from "./image-tag.mjs";
|
||||
import { locate } from "../packages/plugins/harness/pi/binary.mjs";
|
||||
import { levels } from "../packages/plugins/harness/pi/index.mjs";
|
||||
import { piFit } from "../packages/harness/src/pi-fit.mjs";
|
||||
|
||||
// The name refusal lines use for this pi. Never a path.
|
||||
export const LABEL = "the lock's pi";
|
||||
const RUN_NPM_CI = "run npm ci --ignore-scripts in the repository first";
|
||||
|
||||
const readJson = (path) => { try { return JSON.parse(readFileSync(path, "utf8")); } catch { return null; } };
|
||||
const real = (path) => { try { return realpathSync(path); } catch { return null; } };
|
||||
|
||||
// The lookup's answer for the pi npm installed from the lock in root, relabelled, or
|
||||
// { bin: null, problem }. lookup: binary.mjs's locate (tests pass a stand-in).
|
||||
export function lockedFound(root, { lookup = locate } = {}) {
|
||||
let locked;
|
||||
try { locked = lockedPi(root); } catch (e) { return { bin: null, problem: e.message }; }
|
||||
const installed = readJson(join(root, "node_modules", ".package-lock.json"))?.packages?.[PI_LOCK_KEY];
|
||||
if (!installed || installed.version !== locked.version || installed.resolved !== locked.resolved || installed.integrity !== locked.integrity) {
|
||||
return { bin: null, problem: `the installed pi isn't the one package-lock.json resolves: ${RUN_NPM_CI}` };
|
||||
}
|
||||
const dir = join(root, PI_LOCK_KEY);
|
||||
if (readJson(join(dir, "package.json"))?.version !== locked.version) {
|
||||
return { bin: null, problem: `the installed pi's package.json doesn't carry the lock's version: ${RUN_NPM_CI}` };
|
||||
}
|
||||
const found = lookup({ MOSAIC_PI_BIN: join(root, "node_modules", ".bin", "pi") });
|
||||
if (!found?.bin) return { bin: null, problem: `the lookup refused the lock's pi: ${RUN_NPM_CI}` };
|
||||
if (found.pkg !== real(dir)) return { bin: null, problem: `node_modules/.bin/pi isn't the lock's pi package: ${RUN_NPM_CI}` };
|
||||
return { ...found, label: LABEL, locked: locked.version };
|
||||
}
|
||||
|
||||
// The fit check's answer for root's locked pi: { ok: true, version, ... } or { ok: false, message }.
|
||||
// fit: pi-fit.mjs's piFit (tests may pass a stand-in).
|
||||
export async function imageFit(root, { lookup = locate, fit = piFit } = {}) {
|
||||
const found = lockedFound(root, { lookup });
|
||||
if (!found.bin) return { ok: false, message: `pi (${LABEL}) can't be checked: ${found.problem}` };
|
||||
const { locked, ...pi } = found;
|
||||
const result = await fit(pi, { levels: levels() });
|
||||
return result?.ok === true ? { ...result, locked } : { ok: false, message: typeof result?.message === "string" ? result.message : `pi (${LABEL}) can't be checked: the check gave no answer` };
|
||||
}
|
||||
|
||||
const self = fileURLToPath(import.meta.url);
|
||||
let main = false;
|
||||
try { main = realpathSync(process.argv[1] ?? "") === realpathSync(self); } catch {}
|
||||
if (main) {
|
||||
const result = await imageFit(join(self, "..", ".."));
|
||||
if (result.ok) {
|
||||
process.stdout.write(`pi-fit-image: pi ${result.locked} (${LABEL}) passed the fit check\n`);
|
||||
} else {
|
||||
process.stderr.write(`pi-fit-image: ${result.message.split("\n")[0]}\n`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -119,7 +119,7 @@ console.log("active: " + p.release + " (" + p.imageTag + ") since " + p.activ
|
||||
}
|
||||
|
||||
cmd_package() {
|
||||
docker compose build
|
||||
build_image
|
||||
append_log "package" "$MOSAIC_RELEASE" "$MOSAIC_IMAGE_TAG"
|
||||
echo "release: packaged $MOSAIC_IMAGE_TAG"
|
||||
}
|
||||
|
||||
@@ -36,7 +36,8 @@ CFG="MOSAIC_CONFIG=$SANDBOX/mock-config.json"
|
||||
|
||||
# release pointer so agent.sh's ensure stays on the no-op fast path
|
||||
REL="$(tr -d '[:space:]' < RELEASE)"
|
||||
printf '{"pointerVersion":1,"release":"%s","imageTag":"mosaic-poc-agent:0.85.1-r%s","activatedAt":"2026-01-01T00:00:00Z"}\n' "$REL" "$REL" > "$SANDBOX/data/state/active.json"
|
||||
TAG="$(node scripts/image-tag.mjs)" || exit 1 # #1873 S5: the tag the lock gives, never a pinned string
|
||||
printf '{"pointerVersion":1,"release":"%s","imageTag":"%s","activatedAt":"2026-01-01T00:00:00Z"}\n' "$REL" "$TAG" > "$SANDBOX/data/state/active.json"
|
||||
|
||||
# fixtures: a default-harness credential and two mosaic-managed accounts
|
||||
printf '{"anthropic":{"type":"oauth","access":"%s"},"zai":{"type":"api_key","key":"%s"}}' "$TOKEN" "$SECRET" > "$SANDBOX/pi/auth.json"
|
||||
|
||||
@@ -266,7 +266,8 @@ EOF
|
||||
EOF
|
||||
REL="$(tr -d '[:space:]' < RELEASE)"
|
||||
mkdir -p "$SANDBOX/data/state"
|
||||
printf '{"pointerVersion":1,"release":"%s","imageTag":"mosaic-poc-agent:0.85.1-r%s","activatedAt":"2026-01-01T00:00:00Z"}\n' "$REL" "$REL" > "$SANDBOX/data/state/active.json"
|
||||
TAG="$(node scripts/image-tag.mjs)" || exit 1 # #1873 S5: the tag the lock gives, never a pinned string
|
||||
printf '{"pointerVersion":1,"release":"%s","imageTag":"%s","activatedAt":"2026-01-01T00:00:00Z"}\n' "$REL" "$TAG" > "$SANDBOX/data/state/active.json"
|
||||
scripts/skill.sh install ms-tools >/dev/null 2>&1
|
||||
scripts/skill.sh activate ms-tools >/dev/null 2>&1
|
||||
env MOSAIC_CONFIG="$SANDBOX/mock-config.json" MOSAIC_MOCK_RESPONSE=MOCKED \
|
||||
|
||||
Loaded 100 of 102 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user