Compare commits
78
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ebfbdb13a0 | ||
|
|
4d378fc38b | ||
|
|
ab8633ede8 | ||
|
|
6ad11e6993 | ||
|
|
e52a6b714f | ||
|
|
2d065b936e | ||
|
|
098b3771c0 | ||
|
|
eac42663ba | ||
|
|
b41bc00b26 | ||
|
|
749d6d98f5 | ||
|
|
fa5ce3ae65 | ||
|
|
d009ccaec4 | ||
|
|
2613fb1c4c | ||
|
|
df26085303 | ||
|
|
7c1b87d154 | ||
|
|
8908df113e | ||
|
|
b8354e4304 | ||
|
|
f62b1b9518 | ||
|
|
7355b233a8 | ||
|
|
88d08c8437 | ||
|
|
4377941bd4 | ||
|
|
de39750ebb | ||
|
|
3b17326b74 | ||
|
|
d03ae494e8 | ||
|
|
e1b56d9ea7 | ||
|
|
e6f6f5445b | ||
|
|
55f9652f6a | ||
|
|
9e434c0e9c | ||
|
|
e732ae7450 | ||
|
|
66255c9bf2 | ||
|
|
cecc901854 | ||
|
|
81ca8399a2 | ||
|
|
37c3854849 | ||
|
|
db2dc96aa2 | ||
|
|
f666590d54 | ||
|
|
2f20346719 | ||
|
|
524132b42b | ||
|
|
ae53b1988a | ||
|
|
940ac53b65 | ||
|
|
799df9cb9f | ||
|
|
23964b1224 | ||
|
|
d692ca9b81 | ||
|
|
57e86d9867 | ||
|
|
d502a75e57 | ||
|
|
15801ccb07 | ||
|
|
50ca7f0341 | ||
|
|
b9b3e56821 | ||
|
|
32b56b8351 | ||
|
|
78d1ef7705 | ||
|
|
af1bbf7859 | ||
|
|
a7362a8fc5 | ||
|
|
5a07f9e1f9 | ||
|
|
07eca4a670 | ||
|
|
fc9e4dfca8 | ||
|
|
10a35ce65a | ||
|
|
3b9cac5452 | ||
|
|
bc6d3565b0 | ||
|
|
72ea7272ec | ||
|
|
27a359590e | ||
|
|
6c5ec23b47 | ||
|
|
dd0a8612c8 | ||
|
|
9276277970 | ||
|
|
517a3fe467 | ||
|
|
149712816b | ||
|
|
4f63176676 | ||
|
|
1e0aba8b37 | ||
|
|
680bb90af3 | ||
|
|
6a24cf743d | ||
|
|
06ee172a9d | ||
|
|
66676804eb | ||
|
|
a3ad6578c4 | ||
|
|
77a951cfe7 | ||
|
|
cd58ac23cd | ||
|
|
fbaae25d06 | ||
|
|
c9d64a341e | ||
|
|
4cbf3f07b0 | ||
|
|
48a2ed675c | ||
|
|
e70b6bd684 |
No files matched your search
+12
-4
@@ -1,6 +1,6 @@
|
||||
#!/bin/sh
|
||||
# Pi adapter: implements the Mosaic adapter contract for the pinned
|
||||
# @earendil-works/pi-coding-agent CLI.
|
||||
# Pi adapter: implements the Mosaic adapter contract for the
|
||||
# @earendil-works/pi-coding-agent CLI. Its behaviour notes are read from 0.85.1.
|
||||
#
|
||||
# Contract: see /opt/mosaic/adapters/README.md.
|
||||
# Headless (default): stdout = response only; stderr = diagnostics; exit 0.
|
||||
@@ -92,6 +92,11 @@ fi
|
||||
THINKING_FLAG=""
|
||||
[ -n "${MOSAIC_THINKING:-}" ] && THINKING_FLAG="--thinking $MOSAIC_THINKING"
|
||||
|
||||
# MCP (#1873 S3): the harness module sets MOSAIC_PI_NO_MCP=1 when the binary's --help lists
|
||||
# --no-mcp, so MCP stays off by its own flag and not only as a side effect of --no-extensions.
|
||||
NO_MCP_FLAG=""
|
||||
[ "${MOSAIC_PI_NO_MCP:-}" = "1" ] && NO_MCP_FLAG="--no-mcp"
|
||||
|
||||
# Mode (M13): interactive TUI or one-shot print.
|
||||
PRINT_MODE="-p"
|
||||
REQUEST_ARG=""
|
||||
@@ -107,6 +112,7 @@ fi
|
||||
# --system-prompt replace the default prompt with the generated one, given
|
||||
# as the file's absolute path (checked above), never its text
|
||||
# --no-* no ambient context/skills/extensions/templates/themes
|
||||
# NO_MCP_FLAG --no-mcp when the binary lists it, else nothing
|
||||
# EXT_FLAGS the explicitly provided extensions only (-e)
|
||||
# SESSION_FLAGS ephemeral | persistent | forked (per env)
|
||||
# TOOLS_FLAG per capabilities
|
||||
@@ -118,6 +124,7 @@ set -- \
|
||||
--offline \
|
||||
--no-approve \
|
||||
--no-extensions \
|
||||
$NO_MCP_FLAG \
|
||||
$EXT_FLAGS \
|
||||
$SKILLS_FLAG \
|
||||
--no-prompt-templates \
|
||||
@@ -134,6 +141,7 @@ set -- \
|
||||
[ "$INTERACTIVE" = "1" ] || set -- "$@" -p "$MOSAIC_REQUEST"
|
||||
if [ -n "${MOSAIC_MODEL_EVENT_MONITOR:-}" ] && [ "$INTERACTIVE" != "1" ]; then
|
||||
STREAM="$(dirname "$0")/../../packages/plugins/harness/pi/model-stream.mjs"
|
||||
exec node "$STREAM" pi "$MOSAIC_FIRST_OUTPUT_DIR" pi "$@" --mode json
|
||||
exec node "$STREAM" pi "$MOSAIC_FIRST_OUTPUT_DIR" "${MOSAIC_PI_BIN:-pi}" "$@" --mode json
|
||||
fi
|
||||
exec pi "$@"
|
||||
# #1873 S2: the binary the harness module found (MOSAIC_PI_BIN, its path inside the sandbox).
|
||||
exec "${MOSAIC_PI_BIN:-pi}" "$@"
|
||||
+135
-1
@@ -554,6 +554,62 @@ const INSTALLER_COMMENT = 'mosaic-installer-';
|
||||
// business ID_PATTERN, which starts with [a-z0-9], so no instance can be named this.
|
||||
const NOTICE_ROLE = '_bus';
|
||||
const NOTICE_RUN = 'bus';
|
||||
// #1873 chat-fail F2: the one plain line a refused thread start, or a run the model proxy stopped
|
||||
// for silence (proxy-stall), leaves in the conversation for each message it left unanswered
|
||||
// (failureNotice below). It is here, not in a file of its own, because the runner imports this
|
||||
// file in the session sandbox, which mounts only RUNNER_FILES (packages/seat/src/sandbox.mjs).
|
||||
//
|
||||
// The line has the form a failed turn's reply has (packages/harness/src/failure-notice.mjs), so the
|
||||
// chat renders it the same way: "The turn failed: <cause and next step>\nDetails: <code>". The
|
||||
// text is fixed per code. A refusal's own reason is never put in it: it can name paths, models
|
||||
// and accounts. Codes are the launcher's existing refusal codes and the existing proxy-stall stop
|
||||
// reason; none is added here.
|
||||
const MODEL = "the model this agent uses isn't available from the model service now. Pick another model, or check Settings > Model setup.";
|
||||
const PROXY = "the model proxy isn't ready. Check Settings > Model setup.";
|
||||
const ACCOUNT = "the agent's model account isn't signed in or set up. Sign in again, or check the account in Settings > Model setup.";
|
||||
const LIMIT = "the agent reached a usage limit. Wait for it to reset, or check the limits in Settings.";
|
||||
const SETUP = "the agent's setup can't start now. Check the agent in Agents.";
|
||||
const OTHER = "the assistant couldn't start. Try again later, or check the agent in Agents.";
|
||||
|
||||
const LINES = new Map([
|
||||
['proxy-stall', "the model service gave no answer in time, so the turn was stopped. Try again, or check the account in Settings > Model setup."],
|
||||
['model-provider-unavailable', MODEL],
|
||||
['model-catalogue-unavailable', MODEL],
|
||||
['model-harness-incompatible', MODEL],
|
||||
['proxy-uncertain', PROXY],
|
||||
['no-session-credential', PROXY],
|
||||
['account-credential-missing', ACCOUNT],
|
||||
['account-credential-ambiguous', ACCOUNT],
|
||||
['account-is-credential', ACCOUNT],
|
||||
['credential-unavailable', ACCOUNT],
|
||||
['limit-reached', LIMIT],
|
||||
['limit-check-failed', LIMIT],
|
||||
['harness-off', SETUP],
|
||||
['harness-refused', SETUP],
|
||||
['harness-failed', SETUP],
|
||||
['unsupported-harness', SETUP],
|
||||
['thinking-unsupported', SETUP],
|
||||
['plugin-disabled', SETUP],
|
||||
['skill-not-enabled', SETUP],
|
||||
['skill-refused', SETUP],
|
||||
['no-role-contract', SETUP],
|
||||
['definition-holds-credential', SETUP],
|
||||
['project-archived', "this project is archived, so the assistant can't reply here."],
|
||||
]);
|
||||
|
||||
// A start that waits for a free place, a run that is still ending, a held lock, a lost race, a
|
||||
// project still being set up or an operator's pause starts by itself once that clears: no line.
|
||||
const WAITS = new Set(['capacity-full', 'instance-running', 'legacy-run-live', 'session-workspace-busy',
|
||||
'processing-generation-changed', 'proxy-lock-busy', 'project-pending', 'processing-halted']);
|
||||
|
||||
const NOTICE_CODE = /^[a-z][a-z-]{0,63}$/;
|
||||
|
||||
// The body for a code, or null when the code leaves no line.
|
||||
function failureNoticeBody(code) {
|
||||
if (typeof code !== 'string' || !NOTICE_CODE.test(code) || WAITS.has(code)) return null;
|
||||
const line = LINES.get(code) ?? (code.startsWith('proxy-') ? PROXY : OTHER);
|
||||
return `The turn failed: ${line}\nDetails: ${code === 'proxy-stall' ? 'stopped' : 'start refused'}: ${code}`;
|
||||
}
|
||||
// Row 143 (#1637, REQ-PROJ-4): a human save of this file through project.file.save sends one notice to each
|
||||
// instance of these definitions. Their answer goes to the instance of SPEC_ANSWER_TO, the chief of staff,
|
||||
// because only the user and the chief of staff may send to the assistant (REQ-ASSIST-2).
|
||||
@@ -652,6 +708,7 @@ export class Broker {
|
||||
#credentialVerbs;
|
||||
#proxyInit;
|
||||
#proxyFenceClear;
|
||||
#proxyTriageKey;
|
||||
#proxyPage;
|
||||
#project;
|
||||
#library;
|
||||
@@ -716,7 +773,7 @@ export class Broker {
|
||||
#processing;
|
||||
#taskTracker;
|
||||
// credentialStatus is the runtime's metadata-only Credentials.status(); the broker never gets use().
|
||||
constructor({ store, businesses, pluginService = null, secretCheck = () => {}, credentialStatus = () => [], organization = null, hires = null, autonomy = null, project = null, library = null, usage = null, limits = null, harnessOn = null, credentialVerbs = null, proxyInit = null, proxyFenceClear = null, proxyPage = null, hostGrant = null, hostKeys = null, processing = null, testerOn = null, grantScope = null, roleFile = null, giteaGrant = null, copyGrant = null, copyLaunch = null, copyRevoker = null, giteaKeys = null, access = null, accessStamp = null, compactions = null, requestLifecycle = null, requirementsHost = null, workspaceRead = null, taskTracker = null }) {
|
||||
constructor({ store, businesses, pluginService = null, secretCheck = () => {}, credentialStatus = () => [], organization = null, hires = null, autonomy = null, project = null, library = null, usage = null, limits = null, harnessOn = null, credentialVerbs = null, proxyInit = null, proxyFenceClear = null, proxyTriageKey = null, proxyPage = null, hostGrant = null, hostKeys = null, processing = null, testerOn = null, grantScope = null, roleFile = null, giteaGrant = null, copyGrant = null, copyLaunch = null, copyRevoker = null, giteaKeys = null, access = null, accessStamp = null, compactions = null, requestLifecycle = null, requirementsHost = null, workspaceRead = null, taskTracker = null }) {
|
||||
if (!object(businesses) || !Object.keys(businesses).length) fail('invalid-business');
|
||||
// Row 194 b15 (sage-2501): the function-argument checks come before anything reads the store.
|
||||
if (harnessOn !== null && typeof harnessOn !== 'function') throw new TypeError('harnessOn must be a function');
|
||||
@@ -746,6 +803,7 @@ export class Broker {
|
||||
this.#credentialVerbs = credentialVerbs;
|
||||
this.#proxyInit = proxyInit;
|
||||
this.#proxyFenceClear = proxyFenceClear;
|
||||
this.#proxyTriageKey = proxyTriageKey;
|
||||
this.#proxyPage = proxyPage;
|
||||
this.#hostGrant = hostGrant;
|
||||
this.#hostKeys = hostKeys;
|
||||
@@ -2006,6 +2064,61 @@ export class Broker {
|
||||
this.#launcherReleased.add(key);
|
||||
return { released: true };
|
||||
}
|
||||
// #1873 chat-fail F2: a thread start the launcher refused, or a run the model proxy stopped for
|
||||
// silence (proxy-stall), leaves the person who wrote no answer. This puts one plain line in the
|
||||
// conversation for each message from the human, a user or the tester it left unanswered, as a
|
||||
// RESULT in reply to it, the way #landMessage reports a refused landing. A message in a thread is
|
||||
// to or from the thread's instance (trigger message_threads_match), so the line is from the
|
||||
// instance, with from_run NOTICE_RUN: no run sent it, the bus did, and a failed turn's own line is
|
||||
// the instance's too. With { thread, session } the messages are the session's pending ones, for a
|
||||
// refused start; with { run } they are the ones delivered to that run and never read, for a
|
||||
// stopped run. A message the instance has already answered gets no line, so a refusal the
|
||||
// launcher meets on every tick leaves one line per message. The text is fixed per code
|
||||
// (failureNoticeBody above). Trusted launcher API, deliberately NOT a socket verb.
|
||||
failureNotice(record) {
|
||||
keys(record, ['business', 'code', 'thread', 'session', 'run'], ['business', 'code']);
|
||||
this.#business(identifier(record.business));
|
||||
if (typeof record.code !== 'string' || !NOTICE_CODE.test(record.code)) fail('invalid-request');
|
||||
const byRun = record.run !== undefined;
|
||||
if (byRun === (record.thread !== undefined || record.session !== undefined)) fail('invalid-request');
|
||||
let session = null, instance, where, param;
|
||||
if (byRun) {
|
||||
const run = this.#runs.get(record.business + ':' + identifier(record.run)) ?? fail('unknown-run');
|
||||
session = run.session ?? null;
|
||||
instance = run.role;
|
||||
where = `EXISTS(SELECT 1 FROM deliveries d WHERE d.message=m.id AND d.op='delivered' AND d.holder_run=?)
|
||||
AND NOT EXISTS(SELECT 1 FROM deliveries d WHERE d.message=m.id AND d.op='read')`;
|
||||
param = run.run;
|
||||
} else {
|
||||
session = identifier(record.session);
|
||||
const se = this.#store.get('SELECT instance FROM sessions WHERE id=? AND thread=? AND business=?', session, identifier(record.thread), record.business) ?? fail('session-not-found');
|
||||
instance = se.instance;
|
||||
where = `NOT EXISTS(SELECT 1 FROM deliveries d WHERE d.message=m.id AND d.op IN ('delivered','read'))`;
|
||||
param = null;
|
||||
}
|
||||
const body = failureNoticeBody(record.code);
|
||||
if (body === null) return { posted: 0 };
|
||||
const scope = session ? 'm.id IN (SELECT message FROM message_threads WHERE session=?)' : 'NOT EXISTS(SELECT 1 FROM message_threads mt WHERE mt.message=m.id)';
|
||||
return this.#store.transaction(() => {
|
||||
const open = this.#store.all(
|
||||
`SELECT m.id, m.from_role FROM messages m WHERE m.business=? AND m.to_role=? AND ${scope}
|
||||
AND m.from_role IN ('human','user','tester') AND ${where}
|
||||
AND NOT EXISTS(SELECT 1 FROM message_cancels x WHERE x.message=m.id)
|
||||
AND NOT EXISTS(SELECT 1 FROM messages n WHERE n.in_reply_to=m.id AND n.from_role=m.to_role)
|
||||
ORDER BY m.seq`,
|
||||
...[record.business, instance, ...(session ? [session] : []), ...(param === null ? [] : [param])],
|
||||
);
|
||||
for (const m of open) {
|
||||
const mid = randomUUID();
|
||||
this.#store.run(
|
||||
'INSERT INTO messages(id,at,business,from_role,from_run,to_role,class,in_reply_to,decision,corrects,body) VALUES(?,?,?,?,?,?,?,?,?,?,?)',
|
||||
mid, this.#now(), record.business, instance, NOTICE_RUN, m.from_role, 'RESULT', m.id, null, null, body,
|
||||
);
|
||||
this.#route(record.business, mid, m.from_role, session);
|
||||
}
|
||||
return { posted: open.length };
|
||||
});
|
||||
}
|
||||
// Row 271 (264d, #1776, D7 steps 3-5): the business's clears still in the launcher's hands, as of
|
||||
// each op's latest row. An open op waits for its drained run's stop and exit proof, then its
|
||||
// retirement. A retired op whose successor hasn't started, and wasn't cancelled, waits for its
|
||||
@@ -6109,6 +6222,27 @@ export class Broker {
|
||||
return result;
|
||||
}).catch(error => { throw this.#refused(s, new BusError(/^proxy-[a-z-]+$/.test(error.code ?? '') ? error.code : 'proxy-down'), { verb: request.verb }); });
|
||||
}
|
||||
// proxy-scope slice 4 (Jason 2026-11-19): ensure or revoke the instance's Triage key through the
|
||||
// proxy this bus rides. Owner only, before every other gate, like proxy.fence.clear. The
|
||||
// answer, the event and the refusal carry key ids and codes only, never a key.
|
||||
if (request.verb === 'proxy.triage.key') {
|
||||
this.#human(s);
|
||||
if (!this.#proxyTriageKey) fail('not-configured');
|
||||
const args = request.args ?? {};
|
||||
keys(args, ['op'], ['op']);
|
||||
if (args.op !== 'ensure' && args.op !== 'revoke') fail('invalid-request');
|
||||
const ID = /^[0-9a-f]{12}$/;
|
||||
const idList = v => Array.isArray(v) && v.every(id => typeof id === 'string' && ID.test(id));
|
||||
return Promise.resolve().then(() => this.#proxyTriageKey(args.op)).then(result => {
|
||||
const ACTIONS = args.op === 'ensure' ? ['issued', 'held', 'replaced'] : ['revoked', 'none'];
|
||||
if (!object(result) || !ACTIONS.includes(result.action) || !idList(result.addedKeyIds) || !idList(result.removedKeyIds)
|
||||
|| (args.op === 'ensure' && !(typeof result.keyId === 'string' && ID.test(result.keyId)))) fail('proxy-client-response');
|
||||
const answer = { op: args.op, action: result.action, addedKeyIds: [...result.addedKeyIds], removedKeyIds: [...result.removedKeyIds],
|
||||
...(args.op === 'ensure' ? { keyId: result.keyId, rotateBy: /^\d{4}-\d{2}-\d{2}$/.test(result.rotateBy ?? '') ? result.rotateBy : null } : {}) };
|
||||
this.#store.transaction(() => this.#event(s, 'action.allowed', { call: 'proxy.triage.key', ...answer }, null));
|
||||
return answer;
|
||||
}).catch(error => { throw this.#refused(s, error instanceof BusError ? error : new BusError('storage-unavailable', 'triage-key-failed'), { verb: request.verb }); });
|
||||
}
|
||||
// FED R3 (design 3.5, 3.7 trigger 1): a user reaches the collaborator list only, and each
|
||||
// request reads the access file again, so a removed or expired grant refuses the next one.
|
||||
let grants = null;
|
||||
|
||||
@@ -28,6 +28,7 @@ const LAUNCH_OPS = new Set([
|
||||
'landHeld',
|
||||
'threadsState',
|
||||
'threadIdle',
|
||||
'failureNotice',
|
||||
'mayLaunch',
|
||||
'landHolders',
|
||||
'landRelease',
|
||||
@@ -67,6 +68,8 @@ function launchOp(m) {
|
||||
// Row 121 (#1615): the launcher's view of the business's threads, and its idle release.
|
||||
if (m.op === 'threadsState') return runtime.threadsState(m.business);
|
||||
if (m.op === 'threadIdle') return runtime.threadIdle(m.record);
|
||||
// #1873 chat-fail F2: one plain line per message a refused thread start or a proxy-stall stop left unanswered.
|
||||
if (m.op === 'failureNotice') return runtime.failureNotice(m.record);
|
||||
// Row 125 (#1619, REQ-LIMIT-4): the limit checks' answer before a launch (async).
|
||||
if (m.op === 'mayLaunch') return runtime.mayLaunch(m);
|
||||
// Row 156 (#1651): the launcher's landing pass: who holds a merge decision, and the stop of a
|
||||
|
||||
@@ -20,6 +20,42 @@ function view(id, provider, status, priority) {
|
||||
return { accountId: keyId(id), provider, displayName: `${providerName(provider)} account ${keyId(id)}`,
|
||||
status, priority: priorityOk(priority) ? priority : 0 };
|
||||
}
|
||||
// #1873 chat-fail F3: one plain line under an account the proxy reports as not working, with
|
||||
// the next step. It is built only from the structured fields of the account read this page
|
||||
// already makes. status_message is matched against fixed patterns and never shown; a label,
|
||||
// email, recovery hint or refresh error message is never read into the line.
|
||||
const SIGN_IN = 'The account needs to sign in again. Sign in again, or add another account.';
|
||||
const NO_REASON = 'The model service gave no reason. Wait and try again later, sign in again, or add another account.';
|
||||
const USAGE_TEXT = /usage limit|quota|rate.?limit|limit reached|too many requests|\b429\b|insufficient.?balance|credit balance/i;
|
||||
// A bare "expired" is not enough: a usage window expires too.
|
||||
const SIGN_IN_TEXT = /\b401\b|unauthori[sz]ed|invalid.?grant|\b(?:\w*[_-])?(?:tokens?|sessions?|credentials?|logins?|auth\w*|grants?)\s+(?:(?:has|have|is|are|was|were)\s+)?(?:been\s+)?expired|\brevoked\b|\bsign.?in\b|\b(?:re-?)?log.?in\b|\bre-?auth/i;
|
||||
const RFC3339 = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,9}))?(Z|[+-]\d{2}:\d{2})$/;
|
||||
// A reset time the read gives, as "YYYY-MM-DD HH:MM UTC" made from the parsed date, only when
|
||||
// it is a valid RFC 3339 time still ahead of now. Anything else gives no time.
|
||||
function resetTime(value, now) {
|
||||
const m = typeof value === 'string' ? RFC3339.exec(value) : null;
|
||||
if (!m || +m[2] < 1 || +m[2] > 12 || +m[3] < 1 || +m[3] > 31 || +m[4] > 23 || +m[5] > 59 || +m[6] > 59) return null;
|
||||
// A day the month doesn't have (02-31) would roll into the next month, so it is refused.
|
||||
const day = new Date(Date.UTC(2000, +m[2] - 1, +m[3]));
|
||||
day.setUTCFullYear(+m[1]);
|
||||
if (day.getUTCMonth() !== +m[2] - 1 || day.getUTCDate() !== +m[3]) return null;
|
||||
const at = Date.parse(`${m[1]}-${m[2]}-${m[3]}T${m[4]}:${m[5]}:${m[6]}${m[8]}`);
|
||||
if (!Number.isFinite(at) || at <= now) return null;
|
||||
return `${new Date(at).toISOString().slice(0, 16).replace('T', ' ')} UTC`;
|
||||
}
|
||||
function accountReason(row, now) {
|
||||
const message = typeof row.status_message === 'string' ? row.status_message.slice(0, 4096) : '';
|
||||
const time = resetTime(row.next_retry_after, now);
|
||||
const usage = time ? `The account is out of usage. It resets at ${time}. Wait until then, or add another account.`
|
||||
: 'The account is out of usage. Wait until it resets, or add another account.';
|
||||
// The structured flags come first, then the message patterns.
|
||||
if (row.requires_sign_in === true || (object(row.refresh_error) && Object.keys(row.refresh_error).length > 0)) return SIGN_IN;
|
||||
if (row.quota_blocked === true) return usage;
|
||||
if (SIGN_IN_TEXT.test(message)) return SIGN_IN;
|
||||
if (USAGE_TEXT.test(message)) return usage;
|
||||
if (time) return `The model service paused this account until ${time}. Wait until then, sign in again, or add another account.`;
|
||||
return NO_REASON;
|
||||
}
|
||||
export class ProxyPage {
|
||||
#client; #tag; #records; #lock; #org; #dirty = false; #now; #timeout; #onChange; #pending = new Map(); #writes = Promise.resolve();
|
||||
constructor({ client, instanceTag, keyRecords = () => [], now = Date.now, loginTimeoutSeconds = 300, onChange = () => {}, lock = null, org = null }) {
|
||||
@@ -51,7 +87,8 @@ export class ProxyPage {
|
||||
const provider = ['claude', 'codex'].includes(row.provider) ? row.provider : 'other';
|
||||
const status = row.disabled === true ? 'disabled' : row.unavailable === true ? 'unavailable'
|
||||
: ['ready', 'active', 'error', 'disabled', 'unavailable'].includes(row.status) ? row.status : 'unknown';
|
||||
rows.push({ ...view(row.id, provider, status, row.priority), source: 'oauth', name: row.name });
|
||||
rows.push({ ...view(row.id, provider, status, row.priority), source: 'oauth', name: row.name,
|
||||
...(['unavailable', 'error'].includes(status) ? { reason: accountReason(row, this.#now()) } : {}) });
|
||||
}
|
||||
for (const [groupIndex, group] of groups.entries()) {
|
||||
if (!Array.isArray(group?.keys)) continue;
|
||||
@@ -69,7 +106,8 @@ export class ProxyPage {
|
||||
}
|
||||
#public(row) {
|
||||
const { accountId, provider, displayName, status, priority } = row;
|
||||
return { accountId, provider, displayName, status, priority, ...(row.source === 'apikey' && provider === 'z.ai' ? { models: row.models } : {}) };
|
||||
return { accountId, provider, displayName, status, priority, ...(row.source === 'apikey' && provider === 'z.ai' ? { models: row.models } : {}),
|
||||
...(typeof row.reason === 'string' ? { reason: row.reason } : {}) };
|
||||
}
|
||||
#session(state, scope, { reportTimeout = false } = {}) {
|
||||
const entry = stateOk(state) ? this.#pending.get(state) : null;
|
||||
|
||||
@@ -87,6 +87,10 @@ group("proxy-key-mismatch", "R1: a pasted management key is confirmed before ini
|
||||
group("proxy-lock-busy proxy-lock-invalid", "R4: one proxy key write at a time", "Wait for the running proxy write to finish and retry. An invalid lock file needs the owner to inspect the proxy state directory.");
|
||||
group("proxy-not-configured proxy-unit-usage proxy-unit-exists proxy-unit-dropin proxy-unit-copy proxy-unit-path proxy-unit-missing proxy-unit-unhealthy proxy-unit-failed", "R1/R4: proxy user unit is explicit creation", "Initialize the proxy first, then install the unit from the Proxy page or with mosaic proxy unit install. An existing unit or drop-in is never replaced; remove it by hand. An unhealthy unit: read its journal by unit name and restart it.");
|
||||
group("proxy-client-invalid proxy-client-refused proxy-client-response", "R1: proxy management answers", "Check the proxy unit is running and its port answers on the Proxy page, then retry. Upstream bodies are never shown.");
|
||||
// proxy-scope slice 4 (Jason 2026-11-19): the Triage key lives with a bus that rides the owner's
|
||||
// proxy, and a Triage session on a key past its retireAt ends before the key leaves the proxy.
|
||||
group("triage-no-riding-bus", "R1/R4: the Triage key is issued and rotated by a bus that rides the owner's proxy", "Triage needs one organization that uses the owner's models. Start an organization that rides the owner's model proxy, then repair Triage access from Settings, Triage.");
|
||||
group("triage-key-rotated", "R1/R6: a session ends when its Triage key's grace ends", "The Triage key was rotated and the old key's grace has ended. Send the next message; Triage starts again on the current key.");
|
||||
group(
|
||||
"block-recipient block-reply-required block-sender message-recipient-refused message-sender-refused tester-reply-required tester-thread-human user-reply-required user-thread-human",
|
||||
"Role messaging: permitted sender, recipient and reply context",
|
||||
|
||||
@@ -34,6 +34,7 @@ import { createModelCatalogue } from './model-catalogue.mjs';
|
||||
import { sha256 } from '../../model-proxy/src/common.mjs';
|
||||
import { initializeProxy, loadProxy } from '../../model-proxy/src/settings.mjs';
|
||||
import { createProxyLock } from './proxy-lock.mjs';
|
||||
import { ensureKey, readKey as readTriageKey, revokeKeys, sweepDue, sweepKeys } from './triage-key.mjs';
|
||||
import { verifyRestart } from '../../model-proxy/src/admin.mjs';
|
||||
import { ModelKeys, modelOrganization, stopModelRuns, MODEL_STOP_TIMEOUT_MS } from './model-keys.mjs';
|
||||
import { instanceSettings } from '../../organization/src/instance-settings.mjs';
|
||||
@@ -622,6 +623,10 @@ export async function startBroker({
|
||||
// P1 validated sidecar from the trusted host; never a socket request setting.
|
||||
proxy = null,
|
||||
org = null, instanceDataRoot = null, instanceConfigDir = null,
|
||||
// proxy-scope slice 4: the Triage key sweep on a riding org bus. intervalMs (default 30 s) and
|
||||
// marginMs (default 30 s past a retiring key's retireAt, so the Console stops a session on it
|
||||
// first) are in-process seams, as are now and hook (triage-key.mjs's clock and stage hook).
|
||||
triageKeys = {},
|
||||
// Host-only preparation/admission capabilities. The host builds its own from the organization writer and `trackers`
|
||||
// (request-host.mjs); anything given here replaces the host's, and a capability nothing supplies leaves a setup wait.
|
||||
requestServices = {},
|
||||
@@ -657,6 +662,8 @@ export async function startBroker({
|
||||
throw new BusError('invalid-host-request');
|
||||
let modelProxyClient = null, proxyPage = null, proxyUsage = null, usageTimer = null, proxyLock = null, proxyPort = null;
|
||||
let modelKeys = null, modelTimer = null, modelSweep = null, modelCatalogues = null;
|
||||
// proxy-scope slice 4: the settings of the proxy this bus rides (boot, picked up or initialized).
|
||||
let proxySettings = null, triageTimer = null, triageSweepRun = null;
|
||||
const modelRuns = new Map();
|
||||
let store, credentials, server, adapter, processing, syncTimer = null, syncPass = null, accessTimer = null, requestTimer = null, copyLaunch = null, copyRevoker = null, copyTimer = null;
|
||||
// #1873 CB4: a revoke's stop waits for the broker; a start that fails before it exists stops nothing.
|
||||
@@ -707,7 +714,7 @@ export async function startBroker({
|
||||
proxyLock = createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${proxy.port}` });
|
||||
}
|
||||
modelProxyClient = await loadManagementClient({ configDir: org ? instanceConfigDir : organization.dir, proxy, receipt: proxyReceipt });
|
||||
proxyPort = proxy.port;
|
||||
proxyPort = proxy.port; proxySettings = proxy;
|
||||
}
|
||||
|
||||
// Row 234 (#1740, r6 X4 third bullet): each host grant record gives its agent a store reference to
|
||||
@@ -845,7 +852,7 @@ export async function startBroker({
|
||||
// is no client: static text and a refusal code, never a path or a key.
|
||||
let lateProxy = false, proxyCause = null, proxySync = Promise.resolve();
|
||||
const dropLateProxy = cause => {
|
||||
if (lateProxy) { modelProxyClient = null; proxyPage = null; proxyPort = null; proxyLock = null; lateProxy = false; modelCatalogues?.invalidate?.(); }
|
||||
if (lateProxy) { modelProxyClient = null; proxyPage = null; proxyPort = null; proxyLock = null; proxySettings = null; lateProxy = false; modelCatalogues?.invalidate?.(); }
|
||||
proxyCause = cause;
|
||||
};
|
||||
// The host refuses fixture-proxy-configured at boot when a dev fixture is recorded for the
|
||||
@@ -879,11 +886,65 @@ export async function startBroker({
|
||||
catch { return dropLateProxy('The shared proxy lock does not load (proxy-lock-invalid). Check the proxy state for this instance, then restart this bus.'); }
|
||||
}
|
||||
proxyCause = null;
|
||||
proxySettings = found;
|
||||
// Same port, key and lock still load: keep the client and its page, which holds login states.
|
||||
if (modelProxyClient) return;
|
||||
modelProxyClient = client; proxyPort = found.port; proxyLock = lock; lateProxy = true;
|
||||
configureProxyPage(); modelCatalogues?.invalidate?.();
|
||||
}
|
||||
// proxy-scope slice 4: what triage-key.mjs needs from a riding org bus. A key error becomes a bus
|
||||
// refusal the table already holds: proxy codes it knows pass through, the rest name the key
|
||||
// store's code in the reason only. Neither carries a key, a path or upstream text.
|
||||
// The verb checks the proxy first, as proxy.fence.clear does; the sweep uses what this bus holds
|
||||
// and never picks a proxy up on its own timer.
|
||||
const triageOptions = async ({ sync = true } = {}) => {
|
||||
if (sync) await syncProxy();
|
||||
if (org === null || !organization || !proxyLock || !modelProxyClient || !proxySettings || typeof instanceConfigDir !== 'string' || typeof instanceDataRoot !== 'string')
|
||||
throw new BusError('triage-no-riding-bus');
|
||||
return { configDir: instanceConfigDir, system: { dataRoot: instanceDataRoot, proxy: proxySettings }, client: modelProxyClient, lock: proxyLock,
|
||||
...(triageKeys.now ? { now: triageKeys.now } : {}), ...(triageKeys.hook ? { hook: triageKeys.hook } : {}) };
|
||||
};
|
||||
const TRIAGE_PASS = new Set(['proxy-lock-busy', 'proxy-lock-invalid', 'proxy-lock-uncertain', 'proxy-client-invalid', 'proxy-client-refused', 'proxy-client-response', 'proxy-management-key-invalid']);
|
||||
const TRIAGE_MAP = { 'triage-key-no-proxy': 'triage-no-riding-bus', 'triage-key-raced': 'proxy-lock-busy', 'triage-key-unconfirmed': 'proxy-lock-uncertain',
|
||||
'proxy-uncertain': 'proxy-lock-uncertain', 'proxy-client-down': 'proxy-client-response', 'proxy-client-redirect': 'proxy-client-response', 'triage-key-proxy-open': 'proxy-not-configured' };
|
||||
const triageRefusal = error => {
|
||||
if (error instanceof BusError) return error;
|
||||
const code = typeof error?.code === 'string' ? error.code : '';
|
||||
if (TRIAGE_PASS.has(code)) return new BusError(code);
|
||||
if (Object.hasOwn(TRIAGE_MAP, code)) return new BusError(TRIAGE_MAP[code], code);
|
||||
return new BusError('storage-unavailable', /^triage-key-[a-z-]+$/.test(code) ? code : 'triage-key-failed');
|
||||
};
|
||||
// The sweep: a cheap read of the file first, so the proxy is asked only when something is due;
|
||||
// sweepKeys decides again under the lock. It never issues: no file, nothing.
|
||||
const triageMarginMs = Number.isSafeInteger(triageKeys.marginMs) && triageKeys.marginMs >= 0 ? triageKeys.marginMs : 30_000;
|
||||
// A refusal that repeats on every tick is logged when it first appears or changes, and then once
|
||||
// an hour while it stays the same; any other outcome clears it.
|
||||
let triageLastRefusal = null;
|
||||
const sweepRefused = (now, entry) => {
|
||||
const sig = `${entry.code}|${entry.reason ?? ''}`;
|
||||
if (triageLastRefusal?.sig === sig && now - triageLastRefusal.at < 3_600_000) return;
|
||||
triageLastRefusal = { sig, at: now };
|
||||
proxyReceipt({ action: 'triage.key.sweep-refused', ...entry });
|
||||
};
|
||||
const sweepTriage = async () => {
|
||||
let opts;
|
||||
try { opts = await triageOptions({ sync: false }); } catch { return { action: 'none' }; }
|
||||
const now = (opts.now ?? Date.now)();
|
||||
let record;
|
||||
try { record = readTriageKey(opts.configDir); } catch { sweepRefused(now, { code: 'triage-key-unreadable' }); return { action: 'none' }; }
|
||||
if (!sweepDue(record, now, triageMarginMs)) { triageLastRefusal = null; return { action: 'none' }; }
|
||||
try {
|
||||
const result = await sweepKeys(opts, { marginMs: triageMarginMs });
|
||||
triageLastRefusal = null;
|
||||
if (result.action !== 'none') proxyReceipt({ action: `triage.key.${result.action}`, keyId: result.keyId ?? null, addedKeyIds: result.addedKeyIds, removedKeyIds: result.removedKeyIds });
|
||||
return result;
|
||||
} catch (error) {
|
||||
const refusal = triageRefusal(error);
|
||||
sweepRefused(now, { code: refusal.code, ...(typeof refusal.reason === 'string' ? { reason: refusal.reason } : {}) });
|
||||
return { action: 'refused', code: refusal.code };
|
||||
}
|
||||
};
|
||||
const triageSweepOnce = () => (triageSweepRun ??= sweepTriage().finally(() => { triageSweepRun = null; }));
|
||||
// One check at a time; an unexpected failure drops a late client, never keeps it.
|
||||
const syncProxy = () => (proxySync = proxySync.then(pickProxy).catch(() => dropLateProxy('The model proxy could not be checked (proxy-down). Try again shortly.')));
|
||||
// Unavailable with no client: the catalogue names the cause instead of a bare unavailable.
|
||||
@@ -1419,7 +1480,7 @@ export async function startBroker({
|
||||
const lock = org && !proxyLock ? createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${settings.port}` }) : proxyLock;
|
||||
// proxy-pickup: a client this bus did not boot with is checked again like a picked-up one.
|
||||
if (!proxy) { lateProxy = true; proxyCause = null; }
|
||||
modelProxyClient = client; proxyPort = settings.port; proxyLock = lock;
|
||||
modelProxyClient = client; proxyPort = settings.port; proxyLock = lock; proxySettings = settings;
|
||||
configureProxyPage(); modelCatalogues?.invalidate?.();
|
||||
return settings;
|
||||
} : null,
|
||||
@@ -1430,6 +1491,15 @@ export async function startBroker({
|
||||
if (!proxyLock || !modelProxyClient) throw new BusError('not-configured', proxyCause ?? undefined);
|
||||
return proxyLock.clear({ restarted: true, read: () => modelProxyClient.getKeys(), verify: previous => verifyRestart(evidence, { fence: previous, port: proxyPort }) });
|
||||
} : null,
|
||||
// proxy-scope slice 4 (Jason 2026-11-19): ensure or revoke the instance's Triage key through the
|
||||
// proxy this org bus rides, under the bus's own client and instance proxy lock. The answer is
|
||||
// ids only. A no-org bus, or an org bus with no proxy client and lock, refuses
|
||||
// triage-no-riding-bus.
|
||||
proxyTriageKey: async (op) => {
|
||||
const opts = await triageOptions();
|
||||
try { return op === 'ensure' ? await ensureKey(opts) : await revokeKeys(opts); }
|
||||
catch (error) { throw triageRefusal(error); }
|
||||
},
|
||||
credentialVerbs: organization
|
||||
? credentialVerbs({ credentials, writer: organizationWriter({ dir: organization.dir, rolesDir: organization.rolesDir, dataRoot:instanceRoot ?? dataRoot }),
|
||||
organizations: () => Object.keys(businesses), boot: copies?.definitions ? { org: copies.org, definitions: copies.definitions } : null })
|
||||
@@ -1737,6 +1807,12 @@ export async function startBroker({
|
||||
}, access.intervalMs ?? ACCESS_SWEEP_MS);
|
||||
accessTimer.unref();
|
||||
}
|
||||
// proxy-scope slice 4: only an org bus sweeps the Triage key; each tick checks it still rides.
|
||||
if (org !== null && organization) {
|
||||
triageTimer = setInterval(() => { triageSweepOnce(); },
|
||||
Number.isSafeInteger(triageKeys.intervalMs) && triageKeys.intervalMs > 0 ? triageKeys.intervalMs : 30_000);
|
||||
triageTimer.unref();
|
||||
}
|
||||
if (modelKeys) {
|
||||
modelTimer = setInterval(() => {
|
||||
if (modelSweep) return;
|
||||
@@ -1864,6 +1940,8 @@ export async function startBroker({
|
||||
},
|
||||
threadsState: (business) => broker.threadsState(business),
|
||||
threadIdle: (r) => broker.threadIdle(r),
|
||||
// #1873 chat-fail F2: the plain line a refused thread start or a proxy-stall stop leaves.
|
||||
failureNotice: (r) => broker.failureNotice(r),
|
||||
// Row 125 (#1619, REQ-LIMIT-4): the launcher asks the limit checks before every launch.
|
||||
mayLaunch: async ({ business, instance, provider, model, account = null }) => {
|
||||
processingWork();
|
||||
@@ -1903,6 +1981,8 @@ export async function startBroker({
|
||||
definitionScan,
|
||||
launches: bound,
|
||||
readers: readCaps,
|
||||
// proxy-scope slice 4: one Triage key sweep now, as the timer runs it (tests).
|
||||
triageKeySweep: () => triageSweepOnce(),
|
||||
async close() {
|
||||
clearInterval(copyTimer);
|
||||
brokerUp(null);
|
||||
@@ -1911,6 +1991,8 @@ export async function startBroker({
|
||||
await proxyUsage.close();
|
||||
clearInterval(modelTimer);
|
||||
await modelSweep;
|
||||
clearInterval(triageTimer);
|
||||
await triageSweepRun;
|
||||
clearInterval(accessTimer);
|
||||
await stopSync();
|
||||
await server.close();
|
||||
@@ -1928,6 +2010,7 @@ export async function startBroker({
|
||||
clearInterval(usageTimer);
|
||||
await proxyUsage?.close();
|
||||
clearInterval(modelTimer);
|
||||
clearInterval(triageTimer);
|
||||
clearInterval(accessTimer);
|
||||
await server?.close();
|
||||
await adapter?.close?.();
|
||||
|
||||
@@ -155,6 +155,8 @@ export async function serve({ broker, path, authenticateHuman = null, authentica
|
||||
// Row 236 (#1742): the grants verbs may call Gitea several times, 15 s each at most.
|
||||
GRANTS_VERBS.includes(r.verb) ||
|
||||
r.verb === 'proxy.init' ||
|
||||
// proxy-scope slice 4: the Triage key verb waits on the proxy lock and the proxy.
|
||||
r.verb === 'proxy.triage.key' ||
|
||||
(r.verb === 'decision.raise' && (Object.hasOwn(r.args ?? {}, 'spec_path') || Object.hasOwn(r.args ?? {}, 'spec_commit') ||
|
||||
Object.hasOwn(r.args ?? {}, 'grant')))) {
|
||||
const own = transient;
|
||||
|
||||
File renamed without changes.
@@ -1,5 +1,9 @@
|
||||
// #1873 triage-key: Triage's own model proxy client key, issued from the owner's terminal
|
||||
// (design report reports/triage-route-design.md slice 3, Jason's ruling 2026-11-18).
|
||||
// (design report reports/triage-route-design.md slice 3, Jason's ruling 2026-11-18), and since
|
||||
// proxy-scope slice 4 (Jason's ruling 2026-11-19) by the bus too: the verb proxy.triage.key
|
||||
// (ensure, revoke) and a riding org bus's sweep call this module with the bus's own management
|
||||
// client and instance proxy lock. It moved here from packages/cli/src so the bus can import it; the
|
||||
// terminal verbs (packages/cli/src/triage.mjs) call it unchanged.
|
||||
//
|
||||
// <configDir>/secrets/triage/model (directory 0700, file 0600, the running user's)
|
||||
// { version, key, keyId, issuedAt, rotateBy, retiring: [{ key, keyId, issuedAt, retireAt }] }
|
||||
@@ -16,9 +20,11 @@
|
||||
// uncertain fence, which every proxy writer on the instance refuses until a human clears it
|
||||
// (`mosaic proxy fence clear --proxy-restarted`).
|
||||
//
|
||||
// Nothing runs on a timer. rotate records when the old key's grace ends; the key stays in the proxy
|
||||
// until a later retire, rotate or issue removes it, and status says when that is due. The same goes
|
||||
// for rotateBy: status marks it due; only rotate replaces the key.
|
||||
// rotate records when the old key's grace ends; the key stays in the proxy until a later retire,
|
||||
// rotate, issue or sweep removes it, and status says when that is due. A riding org bus runs
|
||||
// sweepKeys on a timer: under the lock it reads the file again and rotates when rotateBy is due, or
|
||||
// retires keys whose grace (plus a margin, so the Console stops a session on the key first) has
|
||||
// passed. The sweep only replaces a key that exists; it never issues the first one.
|
||||
//
|
||||
// Nothing here prints, logs or records a key. Receipts and output carry key ids only (the first 12
|
||||
// hex of sha256(key), the id the bus uses). Messages name files and options, never values.
|
||||
@@ -27,7 +33,7 @@ import { closeSync, constants, fchmodSync, fsyncSync, lstatSync, openSync, readd
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { join } from "node:path";
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
import { createProxyLock } from "../../bus/src/proxy-lock.mjs";
|
||||
import { createProxyLock } from "./proxy-lock.mjs";
|
||||
import { loadManagementClient } from "../../model-proxy/src/client.mjs";
|
||||
|
||||
// #1873 triage-wiring: the key file's reader lives in triage-key-file.mjs (node built-ins only), so
|
||||
@@ -136,20 +142,24 @@ const proxyFailure = (code) => {
|
||||
};
|
||||
|
||||
// opts.uid, opts.now and opts.hook(stage) are test seams; a stage hook may throw or kill the process.
|
||||
function context({ configDir, system, uid, fileUid, now, hook }) {
|
||||
// opts.client and opts.lock: the bus passes its own management client and instance proxy lock, so a
|
||||
// bus change runs under the lock object its model key sweep uses and reads no key file of its own.
|
||||
function context({ configDir, system, uid, fileUid, now, hook, client, lock }) {
|
||||
if (!system?.proxy || !Number.isInteger(system.proxy.port)) throw new TriageKeyError("triage-key-no-proxy", "the instance has no model proxy set up (proxy.json)");
|
||||
return {
|
||||
configDir, dataRoot: system.dataRoot, proxy: system.proxy, stateDir: join(system.dataRoot, "proxy"),
|
||||
uid: uid ?? process.getuid(), fileUid, now: now ?? Date.now, hook: hook ?? (() => {}),
|
||||
uid: uid ?? process.getuid(), fileUid, now: now ?? Date.now, hook: hook ?? (() => {}), client, lock,
|
||||
};
|
||||
}
|
||||
|
||||
function lockFor(c) {
|
||||
if (c.lock) return c.lock;
|
||||
try { return createProxyLock({ stateDir: c.stateDir, identity: `${c.configDir}:${c.proxy.port}` }); }
|
||||
catch (e) { throw proxyFailure(e.code ?? "proxy-lock-invalid"); }
|
||||
}
|
||||
|
||||
async function clientFor(c) {
|
||||
if (c.client) return c.client;
|
||||
try { return await loadManagementClient({ configDir: c.configDir, proxy: c.proxy }); }
|
||||
catch (e) { throw proxyFailure(e.code ?? "proxy-management-key-invalid"); }
|
||||
}
|
||||
@@ -162,9 +172,10 @@ function checkSentinel(c, keys) {
|
||||
}
|
||||
|
||||
// Under the instance proxy lock: read the list, check the sentinel, run `plan`, write the result.
|
||||
// `plan(keys, record)` returns { after, record, removed, added, done }; `record` null leaves the
|
||||
// file alone. The proxy changes first, then the file, then one receipt line.
|
||||
async function change(c, op, plan) {
|
||||
// `plan(keys, record)` returns { after, record, remove, op, done }; `record` null leaves the file
|
||||
// alone, `remove` true unlinks it (revoke), `op` names the change when the plan picks it (sweep).
|
||||
// The proxy changes first, then the file, then one receipt line.
|
||||
async function change(c, opName, plan) {
|
||||
const lock = lockFor(c);
|
||||
const client = await clientFor(c);
|
||||
try {
|
||||
@@ -173,6 +184,8 @@ async function change(c, op, plan) {
|
||||
checkSentinel(c, before);
|
||||
const current = readKey(c.configDir, { uid: c.uid, fileUid: c.fileUid });
|
||||
const result = plan(before, current);
|
||||
const op = result.op ?? opName;
|
||||
const remove = result.remove === true && current !== null;
|
||||
if (result.record) prepareDirs(c.configDir, c.uid);
|
||||
removeTemps(c.configDir);
|
||||
receiptsReady(c.stateDir, c.uid);
|
||||
@@ -195,8 +208,8 @@ async function change(c, op, plan) {
|
||||
}
|
||||
c.hook("write");
|
||||
let unsynced = false;
|
||||
if (result.record) {
|
||||
try { writeKey(c.configDir, result.record, c.uid, c.hook); }
|
||||
if (result.record || remove) {
|
||||
try { if (remove) unlinkKey(c.configDir, c.hook); else writeKey(c.configDir, result.record, c.uid, c.hook); }
|
||||
catch (e) {
|
||||
if (e instanceof NotSynced) unsynced = true;
|
||||
else throw new TriageKeyError("triage-key-unwritten", `the proxy list changed but ${keyPaths(c.configDir).file} was not written (${e.message}); fix the file's directory, then run mosaic triage key ${op} again, which clears what this run left`);
|
||||
@@ -204,7 +217,7 @@ async function change(c, op, plan) {
|
||||
}
|
||||
c.hook("receipt");
|
||||
// A run that changed neither the proxy nor the file leaves no receipt.
|
||||
if (added.length || removed.length || result.record) receipt(c.stateDir, c.uid, { op: `triage.key.${op}`, time: new Date(c.now()).toISOString(), addedKeyIds: added.map(keyIdOf), removedKeyIds: removed.map(keyIdOf) });
|
||||
if (added.length || removed.length || result.record || remove) receipt(c.stateDir, c.uid, { op: `triage.key.${op}`, time: new Date(c.now()).toISOString(), addedKeyIds: added.map(keyIdOf), removedKeyIds: removed.map(keyIdOf) });
|
||||
// The file is in place and the receipt written; only its durability is in doubt.
|
||||
if (unsynced) throw new TriageKeyError("triage-key-unsynced", `${keyPaths(c.configDir).file} was written but its directory could not be synced, so it may not survive a power loss; run mosaic triage key status`);
|
||||
return { ...result.done, addedKeyIds: added.map(keyIdOf), removedKeyIds: removed.map(keyIdOf) };
|
||||
@@ -215,6 +228,18 @@ async function change(c, op, plan) {
|
||||
}
|
||||
}
|
||||
|
||||
// Revoke's file step: unlink, then sync the directory, with writeKey's stages and failures.
|
||||
function unlinkKey(configDir, hook = () => {}) {
|
||||
const { dir, file } = keyPaths(configDir);
|
||||
try { hook("rename"); unlinkSync(file); }
|
||||
catch (e) { if (e?.code !== "ENOENT") throw unreadable("the key file cannot be removed"); }
|
||||
try {
|
||||
hook("sync");
|
||||
const dfd = openSync(dir, constants.O_RDONLY | constants.O_DIRECTORY);
|
||||
try { fsyncSync(dfd); } finally { closeSync(dfd); }
|
||||
} catch { throw new NotSynced(); }
|
||||
}
|
||||
|
||||
const newKey = (configDir) => keyPrefix(configDir) + randomBytes(32).toString("base64url");
|
||||
|
||||
// Keys in the Triage namespace that the file does not hold: left by a crash between the PUT and the
|
||||
@@ -235,39 +260,117 @@ export async function issueKey(opts) {
|
||||
});
|
||||
}
|
||||
|
||||
// Retiring keys whose grace has passed, or that the proxy no longer holds.
|
||||
const dueOf = (record, keys, nowMs) => record.retiring.filter((r) => Date.parse(r.retireAt) <= nowMs || !keys.includes(r.key));
|
||||
// Retiring keys whose grace (plus marginMs, 0 for the terminal verbs) has passed, or that the proxy
|
||||
// no longer holds.
|
||||
const dueOf = (record, keys, nowMs, marginMs = 0) => record.retiring.filter((r) => Date.parse(r.retireAt) + marginMs <= nowMs || !keys.includes(r.key));
|
||||
const rotateDueAt = (record) => Date.parse(`${record.rotateBy}T00:00:00Z`);
|
||||
|
||||
// The rotate plan, shared by the terminal verb and the sweep.
|
||||
function rotatePlan(c, before, current, nowMs, marginMs = 0) {
|
||||
const orphans = orphansOf(c.configDir, before, current);
|
||||
const due = dueOf(current, before, nowMs, marginMs);
|
||||
const keep = before.includes(current.key);
|
||||
const retiring = [...current.retiring.filter((r) => !due.includes(r)),
|
||||
...(keep ? [{ key: current.key, keyId: current.keyId, issuedAt: current.issuedAt, retireAt: retireAtFor(nowMs, c.proxy) }] : [])];
|
||||
if (retiring.length > MAX_RETIRING) throw new TriageKeyError("triage-key-failed", "too many keys are retiring; run mosaic triage key retire once their grace has passed");
|
||||
const key = newKey(c.configDir);
|
||||
const record = { version: KEY_VERSION, key, keyId: keyIdOf(key), issuedAt: new Date(nowMs).toISOString(), rotateBy: rotateByFor(nowMs), retiring };
|
||||
const drop = [...orphans, ...due.map((r) => r.key)];
|
||||
return {
|
||||
after: [...before.filter((k) => !drop.includes(k)), key], record,
|
||||
done: { keyId: record.keyId, rotateBy: record.rotateBy, old: keep ? { keyId: current.keyId, retireAt: retiring.at(-1).retireAt } : { keyId: current.keyId, lost: true } },
|
||||
};
|
||||
}
|
||||
|
||||
export async function rotateKey(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "rotate", (before, current) => {
|
||||
if (!current) throw new TriageKeyError("triage-key-not-issued", "no Triage key is issued; use mosaic triage key issue");
|
||||
const nowMs = c.now();
|
||||
const orphans = orphansOf(c.configDir, before, current);
|
||||
const due = dueOf(current, before, nowMs);
|
||||
const keep = before.includes(current.key);
|
||||
const retiring = [...current.retiring.filter((r) => !due.includes(r)),
|
||||
...(keep ? [{ key: current.key, keyId: current.keyId, issuedAt: current.issuedAt, retireAt: retireAtFor(nowMs, c.proxy) }] : [])];
|
||||
if (retiring.length > MAX_RETIRING) throw new TriageKeyError("triage-key-failed", "too many keys are retiring; run mosaic triage key retire once their grace has passed");
|
||||
const key = newKey(c.configDir);
|
||||
const record = { version: KEY_VERSION, key, keyId: keyIdOf(key), issuedAt: new Date(nowMs).toISOString(), rotateBy: rotateByFor(nowMs), retiring };
|
||||
const drop = [...orphans, ...due.map((r) => r.key)];
|
||||
return {
|
||||
after: [...before.filter((k) => !drop.includes(k)), key], record,
|
||||
done: { keyId: record.keyId, rotateBy: record.rotateBy, old: keep ? { keyId: current.keyId, retireAt: retiring.at(-1).retireAt } : { keyId: current.keyId, lost: true } },
|
||||
};
|
||||
return rotatePlan(c, before, current, c.now());
|
||||
});
|
||||
}
|
||||
function retirePlan(c, before, current, nowMs, marginMs = 0) {
|
||||
const orphans = orphansOf(c.configDir, before, current);
|
||||
const due = current ? dueOf(current, before, nowMs, marginMs) : [];
|
||||
const record = current && due.length ? { ...current, retiring: current.retiring.filter((r) => !due.includes(r)) } : null;
|
||||
const drop = [...orphans, ...due.map((r) => r.key)];
|
||||
return { after: before.filter((k) => !drop.includes(k)), record, done: { droppedKeyIds: due.map((r) => r.keyId) } };
|
||||
}
|
||||
|
||||
export async function retireKeys(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "retire", (before, current) => {
|
||||
return change(c, "retire", (before, current) => retirePlan(c, before, current, c.now()));
|
||||
}
|
||||
|
||||
// proxy-scope slice 4: the bus verb proxy.triage.key op ensure. Leaves a held key alone, issues one
|
||||
// when none exists, replaces one the proxy lost. Never prints the key; the answer is ids only.
|
||||
// issued no file: a new key (keys in the namespace the file does not hold are removed)
|
||||
// held the proxy holds the file's key: only orphans and retiring keys the proxy lost go
|
||||
// replaced the proxy does not hold the file's key: a new key, the old one marked lost
|
||||
export async function ensureKey(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "ensure", (before, current) => {
|
||||
const nowMs = c.now();
|
||||
const orphans = orphansOf(c.configDir, before, current);
|
||||
const due = current ? dueOf(current, before, nowMs) : [];
|
||||
const record = current && due.length ? { ...current, retiring: current.retiring.filter((r) => !due.includes(r)) } : null;
|
||||
const drop = [...orphans, ...due.map((r) => r.key)];
|
||||
return { after: before.filter((k) => !drop.includes(k)), record, done: { droppedKeyIds: due.map((r) => r.keyId) } };
|
||||
const fresh = (retiring) => {
|
||||
const key = newKey(c.configDir);
|
||||
return { version: KEY_VERSION, key, keyId: keyIdOf(key), issuedAt: new Date(nowMs).toISOString(), rotateBy: rotateByFor(nowMs), retiring };
|
||||
};
|
||||
if (!current) {
|
||||
const record = fresh([]);
|
||||
return { after: [...before.filter((k) => !orphans.includes(k)), record.key], record, done: { action: "issued", keyId: record.keyId, rotateBy: record.rotateBy } };
|
||||
}
|
||||
const lost = current.retiring.filter((r) => !before.includes(r.key));
|
||||
const retiring = current.retiring.filter((r) => !lost.includes(r));
|
||||
if (before.includes(current.key)) {
|
||||
return {
|
||||
after: before.filter((k) => !orphans.includes(k)), record: lost.length ? { ...current, retiring } : null,
|
||||
done: { action: "held", keyId: current.keyId, rotateBy: current.rotateBy },
|
||||
};
|
||||
}
|
||||
const record = fresh(retiring);
|
||||
return {
|
||||
after: [...before.filter((k) => !orphans.includes(k)), record.key], record,
|
||||
done: { action: "replaced", keyId: record.keyId, rotateBy: record.rotateBy, old: { keyId: current.keyId, lost: true } },
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// proxy-scope slice 4: op revoke, when the owner turns Triage off. Every name in this instance's
|
||||
// Triage namespace leaves the proxy list, then the key file goes. No file and no names: nothing.
|
||||
export async function revokeKeys(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "revoke", (before, current) => {
|
||||
const mine = before.filter(inNamespace(c.configDir));
|
||||
return { after: before.filter((k) => !mine.includes(k)), record: null, remove: true, done: { action: mine.length || current ? "revoked" : "none" } };
|
||||
});
|
||||
}
|
||||
|
||||
// What a sweep would do from the file alone: "rotate" when rotateBy has come, "retire" when a
|
||||
// retiring key's grace plus marginMs has passed, else null. A cheap check without the lock or the
|
||||
// proxy; sweepKeys decides again under the lock.
|
||||
export function sweepDue(record, nowMs, marginMs = 0) {
|
||||
if (!record) return null;
|
||||
if (rotateDueAt(record) <= nowMs) return "rotate";
|
||||
return record.retiring.some((r) => Date.parse(r.retireAt) + marginMs <= nowMs) ? "retire" : null;
|
||||
}
|
||||
|
||||
// The bus sweep. Under the instance proxy lock it reads the file again and acts only if the action
|
||||
// is still due, so two buses that wake together change the proxy once. No file: nothing, ever.
|
||||
export async function sweepKeys(opts, { marginMs = 0 } = {}) {
|
||||
const c = context(opts);
|
||||
return change(c, "sweep", (before, current) => {
|
||||
const nowMs = c.now();
|
||||
const due = sweepDue(current, nowMs, marginMs);
|
||||
if (due === "rotate") {
|
||||
const plan = rotatePlan(c, before, current, nowMs, marginMs);
|
||||
return { ...plan, op: "rotate", done: { action: "rotated", ...plan.done } };
|
||||
}
|
||||
if (due === "retire") {
|
||||
const plan = retirePlan(c, before, current, nowMs, marginMs);
|
||||
return { ...plan, op: "retire", done: { action: "retired", ...plan.done } };
|
||||
}
|
||||
return { after: before, record: null, done: { action: "none" } };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtempSync, mkdirSync, rmSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { startBroker } from '../src/runtime.mjs';
|
||||
import { checkedTransport } from '../../harness/src/bundle.mjs';
|
||||
import { businessDoc, rolesCopy, systemFor, writeJson } from '../../organization/tests/helpers.mjs';
|
||||
|
||||
// #1873 harness-matrix: an OAuth Claude model runs on claude-code only. A stored route that puts an
|
||||
// OAuth Claude model on pi is refused at launch admission with model-harness-incompatible, even when a stale
|
||||
// catalogue row still says pi. A z.ai and a codex model are admitted on pi.
|
||||
const roles = { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery'], crossRole: [] } }, coder: { authority: { withinRole: [], crossRole: [] } } };
|
||||
|
||||
async function admission(t, entry, catalogueRow) {
|
||||
const root = mkdtempSync(join(tmpdir(), 'harness-matrix-')); t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const rolesDir = rolesCopy(root), doc = businessDoc(root, 'demo');
|
||||
doc.plugins = { ...doc.plugins, harness: ['pi'], 'model-provider': ['cliproxy'] };
|
||||
doc.launch.models['fixture-key'] = entry;
|
||||
doc.roles.coder.vars = { ...doc.roles.coder.vars, harness: 'pi', model: 'fixture-key', thinking: 'low' };
|
||||
writeJson(join(root, 'businesses/demo.json'), doc); mkdirSync(join(root, 'data'), { mode: 0o700 });
|
||||
const modelCatalogue = { invalidate() {}, read: async () => ({ status: 'ready', notices: [], models: [{ module: 'cliproxy', available: true, ...catalogueRow }] }) };
|
||||
const bus = await startBroker({ dataRoot: join(root, 'data'), businesses: { demo: { id: 'demo', human: 'jason', arbiters: { technical: 'chief', delivery: 'chief' }, roles } }, organization: { dir: root, rolesDir, system: systemFor(root) }, modelCatalogue });
|
||||
t.after(() => bus.close());
|
||||
const human = bus.broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const revision = bus.broker.request(human, { verb: 'limits' }).revision;
|
||||
// The launcher's own transport check refuses first; admission is the second gate, so the request
|
||||
// carries the transport the provider would give with no harness named.
|
||||
const transport = checkedTransport(entry);
|
||||
return bus.modelAdmission({ business: 'demo', instance: 'coder', modelKey: 'fixture-key', harness: 'pi', revision, entry, provider: entry.provider, model: entry.model, account: null, route: 'primary', transport });
|
||||
}
|
||||
|
||||
test('#1873 a stored Claude route on pi is refused model-harness-incompatible at admission, even with a stale catalogue row that says pi', async t => {
|
||||
const entry = { provider: 'anthropic', model: 'claude-opus-5-5', module: 'cliproxy', upstream: 'claude' };
|
||||
assert.deepEqual(await admission(t, entry, { upstream: 'claude', model: 'claude-opus-5-5', harnesses: ['claude-code', 'pi'] }), { allow: false, code: 'model-harness-incompatible' });
|
||||
});
|
||||
|
||||
test('#1873 a Claude model from an API-key group (upstream other) is admitted on pi; one whose name an OAuth account offers is refused', async t => {
|
||||
const entry = { provider: 'other', model: 'anthropic/claude-sonnet-5-5', module: 'cliproxy', upstream: 'other' };
|
||||
assert.deepEqual(await admission(t, entry, { upstream: 'other', model: 'anthropic/claude-sonnet-5-5', harnesses: ['claude-code', 'pi'] }), { allow: true });
|
||||
// The catalogue holds a group model to claude-code when an OAuth Claude account offers its name.
|
||||
assert.deepEqual(await admission(t, entry, { upstream: 'other', model: 'anthropic/claude-sonnet-5-5', harnesses: ['claude-code'] }), { allow: false, code: 'model-harness-incompatible' });
|
||||
});
|
||||
|
||||
test('#1873 a z.ai and a codex model are admitted on pi', async t => {
|
||||
for (const [provider, upstream, model] of [['zai', 'zai', 'glm-fixture'], ['codex', 'codex', 'gpt-fixture']]) {
|
||||
const entry = { provider, model, module: 'cliproxy', upstream };
|
||||
assert.deepEqual(await admission(t, entry, { upstream, model, harnesses: ['claude-code', 'pi'] }), { allow: true }, provider);
|
||||
}
|
||||
});
|
||||
@@ -27,7 +27,9 @@ export async function ownershipFixture(t,{launches=true,listed=true,keep=false,r
|
||||
// Available before any acquired resource/setup can fail; no later f/launcher TDZ.
|
||||
const launchLog=()=>{const file=launchLogFile(join(root,'data'),'acme');return fs.existsSync(file)?fs.readFileSync(file,'utf8').trim().split('\n').filter(Boolean).map(line=>JSON.parse(line)):[];};
|
||||
t.after(async()=>{const failures=[];for(const g of gates)g.release();try{await waitFor(()=>active.size===0,'owned operations did not settle',5000);}catch{failures.push('owned-operation-close');}for(const close of [...closers].reverse()){try{await close();}catch{failures.push('owned-close');}}try{server?.closeAllConnections();if(server?.listening)await new Promise((resolve,reject)=>server.close(e=>e?reject(e):resolve()));}catch{failures.push('owned-server-close');}try{checkHistory({logs,proxyReceipts,trace});}catch{failures.push('value-free-receipts');}try{checkHistory({launch:launchLog()});}catch{failures.push('owned-launch-log-guard');}if(stderr)process.stderr.write=stderr;try{fs.rmSync(root,{recursive:true,force:true});}catch{failures.push('owned-root-close');}if(failures.length)throw new AggregateError(failures.map(code=>Error(code)),'owned managed fixture cleanup failed');});
|
||||
const f=fixture(root,'acme',doc=>{doc.plugins={...doc.plugins,harness:['pi']};for(const role of Object.values(doc.roles))role.vars={...role.vars,harness:'pi'};doc.roles.chief.vars.model='claude-opus-5-5';doc.roles.aide={definition:'aide',vars:{harness:'pi',model:'claude-opus-5-5',thinking:'high'}};if(listed)doc.launch.instances.push('aide');if(keep)doc.launch.keep=['aide'];if(retired)doc.retired=['alpha'];if(noHire)doc.launch.noHire=['aide'];if(!listed)delete doc.roles.aide;return doc;});
|
||||
const f=fixture(root,'acme',doc=>{doc.plugins={...doc.plugins,harness:['pi']};for(const role of Object.values(doc.roles))role.vars={...role.vars,harness:'pi'};doc.roles.chief.vars.model='claude-opus-5-5';doc.roles.aide={definition:'aide',vars:{harness:'pi',model:'claude-opus-5-5',thinking:'high'}};if(listed)doc.launch.instances.push('aide');if(keep)doc.launch.keep=['aide'];if(retired)doc.retired=['alpha'];if(noHire)doc.launch.noHire=['aide'];if(!listed)delete doc.roles.aide;doc.launch.models['claude-opus-5-5']={provider:'zai',model:'fixture-aide-model'};return doc;});
|
||||
// #1873 harness-matrix: an OAuth Claude model runs on claude-code only, so on pi the model key
|
||||
// claude-opus-5-5 names a non-Claude upstream model.
|
||||
// The established scratch aide pattern only; repository roles/policy stay untouched.
|
||||
for(const name of ['aide','helper']){fs.writeFileSync(join(f.roles,name+'.md'),'# Scratch aide\n\n## Authority\n');writeJson(join(f.roles,name+'.json'),{roleVersion:2,name,title:'Aide',contract:name+'.md',tools:['read'],network:'api-only',authority:{withinRole:['message.send'],crossRole:[]},credentials:[]});}
|
||||
const chiefFile=join(f.roles,'chief-of-staff.json');
|
||||
@@ -74,6 +76,7 @@ export async function ownershipFixture(t,{launches=true,listed=true,keep=false,r
|
||||
case 'landRelease':return bus.landRelease(m.record);
|
||||
case 'threadsState':return bus.threadsState(m.business);
|
||||
case 'threadIdle':return bus.threadIdle(m.record);
|
||||
case 'failureNotice':return bus.failureNotice(m.record);
|
||||
case 'lifecycleRequests':return bus.lifecycleRequests(m.business);
|
||||
case 'lifecycleTake':return bus.lifecycleTake(m.record);
|
||||
case 'lifecycleDelete':return bus.lifecycleDelete(m.record);
|
||||
|
||||
@@ -6,6 +6,7 @@ import { join } from 'node:path';
|
||||
import { sandboxProblem } from '../../seat/src/sandbox.mjs';
|
||||
import { startTimeOf } from '../../seat/src/proc.mjs';
|
||||
import { ownershipFixture, waitFor } from './model-hire-ownership-fixture.mjs';
|
||||
import * as pi from '../../plugins/harness/pi/index.mjs';
|
||||
const unavailable=sandboxProblem()??false; // Any skip means UNVERIFIED, not green acceptance.
|
||||
const done=async box=>{await box.promise;return box;};
|
||||
const refused=async(box,code)=>{await done(box);assert.equal(box.ok,false,'real operation refuses');assert.equal(box.code,code,'real operation preserves exact refusal');};
|
||||
@@ -17,7 +18,12 @@ test('655 listed duplicate with no record refuses before any model or management
|
||||
assert.deepEqual(f.snapshot(),before,'duplicate hire makes no credential or management effects');f.assertHistoryGuard();f.valueFree();
|
||||
});
|
||||
|
||||
test('655 actual launch own preflight generation and bound consumer survive concurrent duplicate hire',{skip:unavailable,timeout:30000},async t=>{
|
||||
test('655 actual launch own preflight generation and bound consumer survive concurrent duplicate hire',{skip:unavailable,timeout:120000},async t=>{
|
||||
// #1873 pi-unpin re-check 9 and 10: the launch's preflight shares pi's fit check, which is cold in
|
||||
// a fresh process. Wait for it before the fixture starts its launcher, so the launcher's warm-up
|
||||
// and the launch's preflight hit the cache, the gate's 5 s times the launch, and the launch starts
|
||||
// at the same point in the launcher's 2 s tick as it did before the fit check existed.
|
||||
await pi.ready({env:{PATH:process.env.PATH,HOME:process.env.HOME,TMPDIR:process.env.TMPDIR}});
|
||||
const f=await ownershipFixture(t),pGet=f.hold('GET'),p=f.launchAide();await pGet.entered();
|
||||
// P's own acquisition GET is held. Its PUT confirmation GET is ordinal+1;
|
||||
// only an OLD H reuse GET can be ordinal+2. The correct early refusal removes it.
|
||||
|
||||
@@ -80,7 +80,9 @@ function instance(t) {
|
||||
// A real organization bus on the instance, started with no proxy unless `binding.proxy` gives one.
|
||||
async function orgBus(t, f, id, { providers = ['cliproxy'], binding = {} } = {}) {
|
||||
const doc = businessDoc(f.root, id);
|
||||
doc.plugins = { harness: ['pi'], 'model-provider': providers };
|
||||
// The coder's model is a Claude model on a legacy entry, which counts as OAuth and runs on
|
||||
// claude-code only, so the organization enables claude-code.
|
||||
doc.plugins = { harness: ['claude-code'], 'model-provider': providers };
|
||||
writeJson(join(f.configDir, 'businesses', `${id}.json`), doc);
|
||||
const business = loadBusiness(id, { dir: f.configDir, rolesDir: f.rolesDir }), resolved = {};
|
||||
for (const name of Object.keys(business.roles)) resolved[name] = resolveInstance({ system: f.system, business, instance: name });
|
||||
@@ -113,7 +115,7 @@ async function pickModel(o) {
|
||||
const view = await o.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'ready');
|
||||
assert.ok(view.catalogue.models.some(m => m.key === MODEL));
|
||||
await o.ask('routes.set', { revision: view.revision, routes: { coder: { harness: 'pi', model: MODEL, thinking: 'low', fallback: null } } });
|
||||
await o.ask('routes.set', { revision: view.revision, routes: { coder: { harness: 'claude-code', model: MODEL, thinking: 'low', fallback: null } } });
|
||||
}
|
||||
|
||||
// The host launcher as cli.mjs builds it for a bus that booted with no proxy.json: its system has
|
||||
@@ -123,7 +125,10 @@ async function hostLauncher(t, f, o, { system = {} } = {}) {
|
||||
initializeControl(o.dataRoot);
|
||||
// A launch whose role names a skill that is not enabled refuses before the model gate.
|
||||
if (!fs.existsSync(join(f.dataRoot, 'skills-enabled'))) fs.cpSync(REPO_SKILLS, join(f.dataRoot, 'skills-enabled'), { recursive: true });
|
||||
const env = withoutHostCredentials(humanEnv({ MOSAIC_CONFIG: join(f.configDir, 'config.json'), MOSAIC_ROLES_DIR: f.rolesDir }), { piKey: true });
|
||||
// claude-code's preflight needs a session login or a proxy key, and this launcher boots with no
|
||||
// proxy, so the env carries a stand-in login (as launcher.test.mjs does). No adapter ever runs.
|
||||
const env = withoutHostCredentials(humanEnv({ MOSAIC_CONFIG: join(f.configDir, 'config.json'), MOSAIC_ROLES_DIR: f.rolesDir }));
|
||||
env.CLAUDE_CODE_OAUTH_TOKEN = 'stand-in';
|
||||
const answers = [], ops = [];
|
||||
const host = { business: o.id, beforeClose: () => {}, op: async m => {
|
||||
ops.push(m.op);
|
||||
@@ -134,7 +139,9 @@ async function hostLauncher(t, f, o, { system = {} } = {}) {
|
||||
return {};
|
||||
} };
|
||||
const cliproxy = JSON.stringify(pathToFileURL(new URL('../../plugins/model-provider/cliproxy/index.mjs', import.meta.url).pathname).href);
|
||||
const makeLoader = await fakeHarnesses(t, { pi: null }, write => write('model-provider', 'cliproxy', `export * from ${cliproxy};\n`));
|
||||
// The stand-in adapter path skips only the binary check; the launch stops at the model gate or
|
||||
// authorizeLaunch, before any adapter runs.
|
||||
const makeLoader = await fakeHarnesses(t, { 'claude-code': '/fixture-unused' }, write => write('model-provider', 'cliproxy', `export * from ${cliproxy};\n`));
|
||||
// The validated system config shape (mosaic-config.mjs), as cli.mjs passes it with the org root.
|
||||
const config = { configVersion: 1, environment: 'development', dataRoot: o.dataRoot, instanceRoot: f.dataRoot,
|
||||
execution: { backend: 'docker', provider: 'zai', model: 'glm-5.3-flash', adapter: 'mock' }, ...system };
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
// #1873 chat-fail F3: an account the proxy reports as not working carries one plain line,
|
||||
// built only from the structured fields of the proxy's own account read. No upstream text,
|
||||
// label or email is ever reflected, and no new proxy call is made.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { fakeManagement } from './proxy-page-fixture.mjs';
|
||||
const PRIVATE = ['p4-private-label', 'p4-private-email', 'p4-private-token', 'p4-private-message'];
|
||||
const oauth = extra => ({ id: 'oauth-one.json', name: 'oauth-one.json', provider: 'claude', label: PRIVATE[0],
|
||||
email: PRIVATE[1], token: PRIVATE[2], status: 'ready', priority: 3, ...extra });
|
||||
const SIGN_IN = 'The account needs to sign in again. Sign in again, or add another account.';
|
||||
const USAGE = 'The account is out of usage. Wait until it resets, or add another account.';
|
||||
const usageAt = time => `The account is out of usage. It resets at ${time}. Wait until then, or add another account.`;
|
||||
const PAUSED = time => `The model service paused this account until ${time}. Wait until then, sign in again, or add another account.`;
|
||||
const NO_REASON = 'The model service gave no reason. Wait and try again later, sign in again, or add another account.';
|
||||
async function reasonOf(t, extra) {
|
||||
const s = await fakeManagement(t, { files: [oauth(extra)] });
|
||||
const result = await s.call('proxy.accounts');
|
||||
const text = JSON.stringify(result);
|
||||
for (const value of PRIVATE) assert.equal(text.includes(value), false, `${value} reached the page`);
|
||||
assert.deepEqual(s.state.calls, ['GET /v8/management/credentials', 'GET /v8/management/config/api-keys/claude'], 'no new proxy call');
|
||||
return result.accounts[0];
|
||||
}
|
||||
test('an account that must sign in again says so, from requires_sign_in or a refresh error', async t => {
|
||||
for (const extra of [{ status: 'error', requires_sign_in: true, next_action: 'sign_in', recovery_hint: PRIVATE[3] },
|
||||
{ status: 'error', refresh_error: { code: 'invalid_grant', message: PRIVATE[3], http_status: 400 } },
|
||||
{ unavailable: true, status_message: `401 Unauthorized ${PRIVATE[3]}` },
|
||||
{ status: 'error', status_message: `token expired ${PRIVATE[3]}` }])
|
||||
assert.equal((await reasonOf(t, extra)).reason, SIGN_IN, JSON.stringify(extra));
|
||||
});
|
||||
test('an account out of usage names the reset time when the read carries one', async t => {
|
||||
assert.equal((await reasonOf(t, { unavailable: true, quota_blocked: true, next_retry_after: '2099-03-04T05:06:07Z' })).reason,
|
||||
usageAt('2099-03-04 05:06 UTC'));
|
||||
assert.equal((await reasonOf(t, { status: 'error', status_message: `usage limit reached ${PRIVATE[3]}`, next_retry_after: '2099-03-04T05:06:07+02:00' })).reason,
|
||||
usageAt('2099-03-04 03:06 UTC'));
|
||||
// No time, a past time or a value that is not a time gives no time, never the value itself.
|
||||
for (const next_retry_after of [undefined, '2000-01-01T00:00:00Z', PRIVATE[3], '2099-13-40T99:99:99Z', 12345, { at: 1 }])
|
||||
assert.equal((await reasonOf(t, { unavailable: true, quota_blocked: true, next_retry_after })).reason, USAGE, String(next_retry_after));
|
||||
});
|
||||
test('a paused account with only a retry time says until when', async t => {
|
||||
assert.equal((await reasonOf(t, { unavailable: true, status_message: PRIVATE[3], next_retry_after: '2099-03-04T05:06:07.123456789Z' })).reason,
|
||||
PAUSED('2099-03-04 05:06 UTC'));
|
||||
});
|
||||
test('a failing account with nothing more in the read says the model service gave no reason', async t => {
|
||||
for (const extra of [{ status: 'error' }, { unavailable: true }, { status: 'unavailable', status_message: PRIVATE[3] },
|
||||
{ status: 'error', status_message: 42, refresh_error: null, requires_sign_in: 'yes', quota_blocked: 1 }])
|
||||
assert.equal((await reasonOf(t, extra)).reason, NO_REASON, JSON.stringify(extra));
|
||||
});
|
||||
test('a working, turned-off or unknown account keeps exactly its fields and gets no line', async t => {
|
||||
for (const extra of [{}, { status: 'active', status_message: 'usage limit reached' }, { disabled: true, requires_sign_in: true }, { status: 'mystery' }])
|
||||
assert.deepEqual(Object.keys(await reasonOf(t, extra)).sort(), ['accountId', 'displayName', 'priority', 'provider', 'status'], JSON.stringify(extra));
|
||||
});
|
||||
test('every line is plain: no em dash, no control character, at most one sentence pair', () => {
|
||||
for (const line of [SIGN_IN, USAGE, usageAt('2099-03-04 05:06 UTC'), PAUSED('2099-03-04 05:06 UTC'), NO_REASON]) {
|
||||
assert.doesNotMatch(line, /[—\u0000-\u001f\u007f]/);
|
||||
assert.ok(line.length <= 160, line);
|
||||
}
|
||||
});
|
||||
// Review round 1: a day the month doesn't have is refused, not rolled into the next month.
|
||||
test('an impossible date gives no reset time; a real leap day does', async t => {
|
||||
for (const next_retry_after of ['2099-02-31T00:00:00Z', '2099-04-31T10:00:00Z', '2099-02-29T00:00:00Z'])
|
||||
assert.equal((await reasonOf(t, { unavailable: true, quota_blocked: true, next_retry_after })).reason, USAGE, next_retry_after);
|
||||
assert.equal((await reasonOf(t, { unavailable: true, quota_blocked: true, next_retry_after: '2096-02-29T01:02:03Z' })).reason, usageAt('2096-02-29 01:02 UTC'));
|
||||
});
|
||||
// Review round 1: quota_blocked wins over a message word, and a bare "expired" is not a sign-in.
|
||||
test('an account blocked on usage whose message says expired gets the usage line', async t => {
|
||||
assert.equal((await reasonOf(t, { unavailable: true, quota_blocked: true, status_message: `usage window expired ${PRIVATE[3]}` })).reason, USAGE);
|
||||
assert.equal((await reasonOf(t, { status: 'error', status_message: `trial window expired ${PRIVATE[3]}` })).reason, NO_REASON);
|
||||
for (const status_message of ['session expired', 'credentials expired', 'OAuth token has expired'])
|
||||
assert.equal((await reasonOf(t, { status: 'error', status_message })).reason, SIGN_IN, status_message);
|
||||
});
|
||||
// Review round 2: the sign-in words match whole words only, and a prefixed or "is expired" token counts.
|
||||
test('a message word inside another word is not a sign-in; a prefixed or "is expired" token is', async t => {
|
||||
for (const status_message of ['assigning a slot', 'logging paused', 'design input'])
|
||||
assert.equal((await reasonOf(t, { status: 'error', status_message })).reason, NO_REASON, status_message);
|
||||
for (const status_message of ['access_token expired', 'token is expired', 'refresh-token has been expired', 'please sign in'])
|
||||
assert.equal((await reasonOf(t, { status: 'error', status_message })).reason, SIGN_IN, status_message);
|
||||
});
|
||||
// Review round 3: "relogin", plurals and past forms of "expired" read as a sign-in too.
|
||||
test('relogin, plural and past forms of an expired sign-in say sign in again', async t => {
|
||||
for (const status_message of ['relogin required', 'tokens have expired', 'session was expired', 'credentials were expired'])
|
||||
assert.equal((await reasonOf(t, { status: 'error', status_message })).reason, SIGN_IN, status_message);
|
||||
});
|
||||
@@ -656,3 +656,80 @@ test('C2-09a: creative bindLaunch without a thread refuses thread-required', t =
|
||||
assert.equal(ctx.count('SELECT count(*) n FROM threads'), 0);
|
||||
assert.equal(ctx.count('SELECT count(*) n FROM role_claims'), 0);
|
||||
});
|
||||
|
||||
// #1873 chat-fail F2: the launcher's trusted failureNotice puts one plain line in the conversation
|
||||
// for each message a refused start or a stalled run left unanswered.
|
||||
const LIMIT_LINE = "The turn failed: the agent reached a usage limit. Wait for it to reset, or check the limits in Settings.\nDetails: start refused: limit-reached";
|
||||
const STALL_LINE = "The turn failed: the model service gave no answer in time, so the turn was stopped. Try again, or check the account in Settings > Model setup.\nDetails: stopped: proxy-stall";
|
||||
const lines = (store, id) => store.all("SELECT * FROM messages WHERE in_reply_to=? AND from_run='bus'", id);
|
||||
|
||||
test('#1873 F2: a refused start puts one line per pending message in its session, once, and none for a wait', (t) => {
|
||||
const { b, store, call, A, B } = twoThreads(t);
|
||||
b.endLaunch({ business: 'demo', run: 'a-run', reason: 'idle' });
|
||||
const session = store.get('SELECT session FROM session_defaults WHERE thread=?', A).session;
|
||||
const notice = (code, extra = {}) => b.failureNotice({ business: 'demo', thread: A, session, code, ...extra });
|
||||
const one = call('human', 'thread.send', { thread: A, body: 'one' });
|
||||
const two = call('human', 'thread.send', { thread: A, body: 'two' });
|
||||
const other = call('human', 'thread.send', { thread: B, body: 'for B' });
|
||||
// A start that waits for a free place leaves no line.
|
||||
for (const code of ['capacity-full', 'instance-running', 'legacy-run-live', 'session-workspace-busy', 'processing-generation-changed', 'proxy-lock-busy', 'project-pending', 'processing-halted']) assert.deepEqual(notice(code), { posted: 0 });
|
||||
assert.deepEqual(notice('limit-reached'), { posted: 2 });
|
||||
for (const sent of [one, two]) {
|
||||
const [line] = lines(store, sent.id);
|
||||
assert.equal(line.body, LIMIT_LINE);
|
||||
assert.deepEqual([line.class, line.from_role, line.to_role, line.from_run], ['RESULT', 'assistant', 'human', 'bus']);
|
||||
assert.equal(store.get('SELECT session FROM message_threads WHERE message=?', line.id).session, session, 'the line is in the session');
|
||||
}
|
||||
assert.deepEqual(lines(store, other.id), [], "another thread's message gets none");
|
||||
// Every retry meets the same refusal: still one line each, under any code.
|
||||
assert.deepEqual(notice('limit-reached'), { posted: 0 });
|
||||
assert.deepEqual(notice('model-provider-unavailable'), { posted: 0 });
|
||||
assert.equal(lines(store, one.id).length, 1);
|
||||
// The message stays pending, so the next start still takes it.
|
||||
assert.equal(b.threadsState('demo').find((s) => s.thread === A).pending, 2);
|
||||
const three = call('human', 'thread.send', { thread: A, body: 'three' });
|
||||
assert.deepEqual(notice('some-new-code'), { posted: 1 });
|
||||
assert.match(lines(store, three.id)[0].body, /^The turn failed: the assistant couldn't start\. .*\nDetails: start refused: some-new-code$/);
|
||||
});
|
||||
|
||||
test('#1873 F2: a stalled run puts one line on each message delivered to it and unread, thread or role', (t) => {
|
||||
const { b, store, call, A } = twoThreads(t);
|
||||
const taken = call('human', 'thread.send', { thread: A, body: 'taken' });
|
||||
assert.deepEqual(call('a', 'message.receive').map((m) => m.id), [taken.id]);
|
||||
const waiting = call('human', 'thread.send', { thread: A, body: 'still waiting' });
|
||||
assert.deepEqual(b.failureNotice({ business: 'demo', run: 'a-run', code: 'proxy-stall' }), { posted: 1 });
|
||||
assert.equal(lines(store, taken.id)[0].body, STALL_LINE);
|
||||
assert.deepEqual(lines(store, waiting.id), [], 'a message the run never took waits for the next run');
|
||||
assert.deepEqual(b.failureNotice({ business: 'demo', run: 'a-run', code: 'proxy-stall' }), { posted: 0 });
|
||||
// A read message was answered by the turn: no line.
|
||||
const read = call('human', 'thread.send', { thread: A, body: 'read' });
|
||||
call('a', 'message.receive');
|
||||
call('a', 'message.read', { id: read.id });
|
||||
assert.deepEqual(b.failureNotice({ business: 'demo', run: 'a-run', code: 'proxy-stall' }), { posted: 1 }, 'only waiting, delivered now');
|
||||
assert.deepEqual(lines(store, read.id), []);
|
||||
// A role run: the human's message to the chief, outside any thread.
|
||||
const asked = call('human', 'message.send', { to: 'chief', class: 'REQUEST', body: 'chief, please' });
|
||||
assert.ok(call('chief', 'message.receive').some((m) => m.id === asked.id));
|
||||
assert.deepEqual(b.failureNotice({ business: 'demo', run: 'chief-run', code: 'proxy-stall' }), { posted: 1 });
|
||||
const [line] = lines(store, asked.id);
|
||||
assert.deepEqual([line.body, line.from_role, line.to_role], [STALL_LINE, 'chief', 'human']);
|
||||
assert.equal(store.get('SELECT count(*) n FROM message_threads WHERE message=?', line.id).n, 0);
|
||||
});
|
||||
|
||||
test('#1873 F2: failureNotice refuses a malformed record before any write', (t) => {
|
||||
const { b, store, A } = twoThreads(t);
|
||||
const session = store.get('SELECT session FROM session_defaults WHERE thread=?', A).session;
|
||||
const before = store.get('SELECT count(*) n FROM messages').n;
|
||||
const bad = [
|
||||
[{ business: 'demo', code: 'limit-reached' }, 'invalid-request'],
|
||||
[{ business: 'demo', code: 'limit-reached', run: 'a-run', thread: A, session }, 'invalid-request'],
|
||||
[{ business: 'demo', code: 'Limit Reached', run: 'a-run' }, 'invalid-request'],
|
||||
[{ business: 'demo', code: 'x'.repeat(65), run: 'a-run' }, 'invalid-request'],
|
||||
[{ business: 'demo', code: 7, run: 'a-run' }, 'invalid-request'],
|
||||
[{ business: 'demo', code: 'limit-reached', run: 'a-run', why: 'secret text' }, 'invalid-request'],
|
||||
[{ business: 'demo', code: 'limit-reached', run: 'nobody-run' }, 'unknown-run'],
|
||||
[{ business: 'demo', code: 'limit-reached', thread: A, session: 'no-such-session' }, 'session-not-found'],
|
||||
];
|
||||
for (const [record, code] of bad) assert.throws(() => b.failureNotice(record), { code }, JSON.stringify(record));
|
||||
assert.equal(store.get('SELECT count(*) n FROM messages').n, before);
|
||||
});
|
||||
@@ -0,0 +1,305 @@
|
||||
// proxy-scope slice 4 (#1873): the Triage key in the bus. Real organization buses on one instance,
|
||||
// a stand-in proxy management server on loopback and a temporary config directory; nothing here
|
||||
// reads a real config, proxy or secret. Each test scans every answer, event, receipt line and
|
||||
// stderr line for every Triage key the stand-in ever held.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import http from 'node:http';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
import { startBroker } from '../src/runtime.mjs';
|
||||
import { busOrganization } from '../src/organization-context.mjs';
|
||||
import { keyPaths, readKey } from '../src/triage-key.mjs';
|
||||
import { Registry } from '../../seat/src/session.mjs';
|
||||
import { loadBusiness } from '../../organization/src/organization-config.mjs';
|
||||
import { resolveInstance } from '../../organization/src/resolve.mjs';
|
||||
import { businessDoc, rolesCopy, systemFor, writeJson } from '../../organization/tests/helpers.mjs';
|
||||
|
||||
const NOW = Date.parse('2026-11-18T12:00:00.000Z');
|
||||
const MIN = 60_000, HOUR = 60 * MIN, DAY = 24 * HOUR;
|
||||
const MANAGE = 'fx1873-s4-manage';
|
||||
const KEY_PATH = '/v8/management/config/access/api-keys';
|
||||
const sha = s => createHash('sha256').update(s).digest('hex');
|
||||
const tag = dir => sha(dir).slice(0, 12);
|
||||
const id = key => sha(key).slice(0, 12);
|
||||
const b64 = () => randomBytes(32).toString('base64url');
|
||||
const code = name => e => e.code === name;
|
||||
|
||||
// Every Triage key the stand-in ever held, and every text a bus produced, across the file.
|
||||
const SEEN = new Set();
|
||||
const TEXTS = [];
|
||||
function leaks(text) {
|
||||
for (const key of SEEN) {
|
||||
const secret = key.slice(-43);
|
||||
if (text.includes(key) || text.includes(secret)) return key.slice(0, 8);
|
||||
for (let i = 0; i + 8 <= secret.length; i++) if (text.includes(secret.slice(i, i + 8))) return key.slice(0, 8);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Every stderr line while the test runs: the bus's model-proxy receipt lines go there.
|
||||
function captureStderr(t) {
|
||||
const write = process.stderr.write, lines = [];
|
||||
process.stderr.write = (chunk, ...rest) => { lines.push(String(chunk)); return write.call(process.stderr, chunk, ...rest); };
|
||||
t.after(() => { process.stderr.write = write; TEXTS.push(lines.join('')); });
|
||||
return lines;
|
||||
}
|
||||
|
||||
async function instance(t) {
|
||||
const root = fs.realpathSync(fs.mkdtempSync(join(tmpdir(), 'tkb-')));
|
||||
const configDir = join(root, 'config');
|
||||
fs.mkdirSync(join(configDir, 'secrets'), { recursive: true, mode: 0o700 });
|
||||
fs.chmodSync(configDir, 0o700); fs.chmodSync(join(configDir, 'secrets'), 0o700);
|
||||
fs.writeFileSync(join(configDir, 'secrets', 'proxy-management.key'), MANAGE, { mode: 0o600 });
|
||||
fs.mkdirSync(join(root, 'proxy'), { mode: 0o700 });
|
||||
const sentinel = `msk-${tag(configDir)}-sentinel-${b64()}`, orgKey = `mso-${tag(configDir)}-other-key-${b64()}`;
|
||||
// triagePuts counts the writes that changed a Triage name; the organization buses' own key
|
||||
// reconcile writes the list too, and those do not count.
|
||||
const state = { keys: [sentinel, 'foreign-client-key', orgKey], puts: [], triagePuts: 0 };
|
||||
const triageNames = list => JSON.stringify(list.filter(k => k.startsWith('mst-')).sort());
|
||||
const server = http.createServer(async (req, res) => {
|
||||
if (req.url !== KEY_PATH) {
|
||||
const key = req.headers.authorization?.slice(7);
|
||||
res.writeHead(state.keys.includes(key) ? 200 : 401); res.end('{}'); return;
|
||||
}
|
||||
if (req.headers.authorization !== `Bearer ${MANAGE}`) { res.writeHead(401); res.end('{}'); return; }
|
||||
if (req.method === 'GET') { res.end(JSON.stringify(state.keys)); return; }
|
||||
const chunks = []; for await (const c of req) chunks.push(c);
|
||||
const next = JSON.parse(Buffer.concat(chunks));
|
||||
for (const k of next) if (/^mst-.*-[A-Za-z0-9_-]{43}$/.test(k)) SEEN.add(k);
|
||||
state.puts.push(next);
|
||||
if (triageNames(next) !== triageNames(state.keys)) state.triagePuts++;
|
||||
state.keys = next;
|
||||
res.end('{"status":"ok"}');
|
||||
});
|
||||
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||
const port = server.address().port;
|
||||
const closers = [];
|
||||
t.after(async () => {
|
||||
for (const close of closers.reverse()) await close().catch(() => {});
|
||||
server.closeAllConnections(); await new Promise(resolve => server.close(resolve));
|
||||
const receipts = join(root, 'proxy', 'mosaic-keys.receipts.jsonl');
|
||||
if (fs.existsSync(receipts)) TEXTS.push(fs.readFileSync(receipts, 'utf8'));
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
});
|
||||
const rolesDir = rolesCopy(root), system = systemFor(root);
|
||||
const f = {
|
||||
root, configDir, port, state, closers, rolesDir, system, sentinel, orgKey, clock: { now: NOW },
|
||||
prefix: `mst-${tag(configDir)}-triage-`,
|
||||
file: keyPaths(configDir).file,
|
||||
record: () => readKey(configDir),
|
||||
triageKeys: () => f.state.keys.filter(k => k.startsWith(f.prefix)),
|
||||
receipts: () => {
|
||||
const p = join(root, 'proxy', 'mosaic-keys.receipts.jsonl');
|
||||
return fs.existsSync(p) ? fs.readFileSync(p, 'utf8').trim().split('\n').filter(Boolean).map(JSON.parse) : [];
|
||||
},
|
||||
// A real organization bus on this instance. `proxy: false` starts it with no proxy settings, as
|
||||
// a bus on an instance with no proxy.json does.
|
||||
async bus(orgId, { proxy = true, triageKeys = {} } = {}) {
|
||||
const doc = businessDoc(root, orgId);
|
||||
doc.plugins = { ...doc.plugins, harness: ['pi'] };
|
||||
writeJson(join(configDir, 'businesses', `${orgId}.json`), doc);
|
||||
const business = loadBusiness(orgId, { dir: configDir, rolesDir }), resolved = {};
|
||||
for (const name of Object.keys(business.roles)) resolved[name] = resolveInstance({ system, business, instance: name });
|
||||
const dataRoot = join(root, `${orgId}-bus`);
|
||||
fs.mkdirSync(dataRoot, { mode: 0o700 }); new Registry(dataRoot);
|
||||
const bus = await startBroker({ dataRoot, businesses: { [orgId]: busOrganization(business, resolved) },
|
||||
organization: { dir: configDir, rolesDir, system }, ...(proxy ? { proxy: { port, binary: '/fixture-unused' } } : {}),
|
||||
org: { id: orgId }, instanceDataRoot: root, instanceConfigDir: configDir,
|
||||
triageKeys: { intervalMs: 3_600_000, now: () => f.clock.now, ...triageKeys } });
|
||||
closers.push(() => bus.close());
|
||||
const cap = bus.broker.bindHuman({ business: orgId, human: business.human ?? doc.human, via: 'cli', outsideAgent: true });
|
||||
const ask = async (verb, args) => {
|
||||
try { const answer = await bus.broker.request(cap, { verb, args }); TEXTS.push(JSON.stringify(answer)); return answer; }
|
||||
catch (error) { TEXTS.push(JSON.stringify({ code: error.code, reason: error.reason, message: error.message, nextStep: error.nextStep })); throw error; }
|
||||
};
|
||||
const sweep = async () => { const r = await bus.triageKeySweep(); TEXTS.push(JSON.stringify(r)); return r; };
|
||||
return { bus, ask, sweep, dataRoot };
|
||||
},
|
||||
};
|
||||
return f;
|
||||
}
|
||||
|
||||
// Every byte under a bus's data root (its store, with events and refusals, and its logs), as text.
|
||||
function eventsText(b, called = true) {
|
||||
const out = [];
|
||||
const walk = dir => {
|
||||
for (const name of fs.readdirSync(dir)) {
|
||||
const p = join(dir, name), st = fs.lstatSync(p);
|
||||
if (st.isDirectory()) walk(p);
|
||||
else if (st.isFile()) out.push(fs.readFileSync(p).toString('latin1'));
|
||||
}
|
||||
};
|
||||
walk(b.dataRoot);
|
||||
if (called) assert.ok(out.join('').includes('proxy.triage.key'), 'the store holds the verb\'s events');
|
||||
return out.join('\n');
|
||||
}
|
||||
|
||||
test('ensure issues one key, a second ensure keeps it, and both answers carry ids only', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha');
|
||||
const first = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.equal(first.action, 'issued');
|
||||
assert.equal(f.triageKeys().length, 1);
|
||||
const record = f.record();
|
||||
assert.equal(first.keyId, record.keyId);
|
||||
assert.equal(first.keyId, id(f.triageKeys()[0]));
|
||||
assert.deepEqual(first.addedKeyIds, [record.keyId]);
|
||||
assert.equal(fs.statSync(f.file).mode & 0o777, 0o600);
|
||||
const second = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.deepEqual(second, { op: 'ensure', action: 'held', addedKeyIds: [], removedKeyIds: [], keyId: record.keyId, rotateBy: record.rotateBy });
|
||||
assert.equal(f.triageKeys().length, 1, 'ensure twice gives one key');
|
||||
assert.equal(f.record().key, record.key);
|
||||
// Through a second riding bus on the same instance: still the one key.
|
||||
const b = await f.bus('beta');
|
||||
assert.equal((await b.ask('proxy.triage.key', { op: 'ensure' })).action, 'held');
|
||||
assert.equal(f.triageKeys().length, 1);
|
||||
assert.equal(f.state.triagePuts, 1, 'only the first ensure wrote the proxy list');
|
||||
// The receipt names the change by op and ids.
|
||||
const receipts = f.receipts().filter(r => r.op?.startsWith('triage.'));
|
||||
assert.deepEqual(receipts.map(r => [r.op, r.addedKeyIds, r.removedKeyIds]), [['triage.key.ensure', [record.keyId], []]]);
|
||||
for (const text of [eventsText(a), eventsText(b)]) assert.equal(leaks(text), null);
|
||||
});
|
||||
|
||||
test('ensure replaces a key the proxy lost, and revoke removes every Triage name and the file', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha');
|
||||
const issued = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
// The proxy loses the key (removed by hand): ensure issues a new one and names the old as lost.
|
||||
f.state.keys = f.state.keys.filter(k => !k.startsWith(f.prefix));
|
||||
const replaced = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.equal(replaced.action, 'replaced');
|
||||
assert.notEqual(replaced.keyId, issued.keyId);
|
||||
assert.equal(f.triageKeys().length, 1);
|
||||
// An orphan in the namespace (a crash left it) goes with revoke too.
|
||||
const orphan = `${f.prefix}${b64()}`; SEEN.add(orphan);
|
||||
f.state.keys.push(orphan);
|
||||
const revoked = await a.ask('proxy.triage.key', { op: 'revoke' });
|
||||
assert.equal(revoked.action, 'revoked');
|
||||
assert.deepEqual(revoked.removedKeyIds.sort(), [replaced.keyId, id(orphan)].sort());
|
||||
assert.equal(f.triageKeys().length, 0);
|
||||
assert.equal(fs.existsSync(f.file), false);
|
||||
assert.equal(f.record(), null);
|
||||
// Revoke again: nothing to do, nothing written.
|
||||
const puts = f.state.triagePuts;
|
||||
assert.deepEqual(await a.ask('proxy.triage.key', { op: 'revoke' }), { op: 'revoke', action: 'none', addedKeyIds: [], removedKeyIds: [] });
|
||||
assert.equal(f.state.triagePuts, puts);
|
||||
// Namespace guard: the sentinel, the organization key and the foreign key are still there.
|
||||
for (const k of [f.sentinel, f.orgKey, 'foreign-client-key']) assert.ok(f.state.keys.includes(k), k.slice(0, 4));
|
||||
assert.equal(leaks(eventsText(a)), null);
|
||||
});
|
||||
|
||||
test('a bus that does not ride the owner\'s proxy refuses triage-no-riding-bus, and its sweep does nothing', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const loose = await f.bus('loose', { proxy: false });
|
||||
await assert.rejects(loose.ask('proxy.triage.key', { op: 'ensure' }), e => e.code === 'triage-no-riding-bus'
|
||||
&& /^Triage needs one organization that uses the owner's models\./.test(e.nextStep));
|
||||
await assert.rejects(loose.ask('proxy.triage.key', { op: 'revoke' }), code('triage-no-riding-bus'));
|
||||
assert.equal(f.state.triagePuts, 0);
|
||||
// With a key issued through a riding bus and its rotation due, the loose bus still does nothing.
|
||||
const a = await f.bus('alpha');
|
||||
await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
f.clock.now += 31 * DAY;
|
||||
assert.deepEqual(await loose.sweep(), { action: 'none' });
|
||||
assert.equal(f.state.triagePuts, 1);
|
||||
});
|
||||
|
||||
test('the sweep rotates when rotateBy comes, once across two buses that wake together, and again a rotation later', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha'), b = await f.bus('beta');
|
||||
const issued = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
// Not due yet: neither bus touches the proxy.
|
||||
assert.deepEqual([await a.sweep(), await b.sweep()], [{ action: 'none' }, { action: 'none' }]);
|
||||
assert.equal(f.state.triagePuts, 1);
|
||||
f.clock.now = Date.parse(`${issued.rotateBy}T00:00:00Z`);
|
||||
const both = await Promise.all([a.sweep(), b.sweep()]);
|
||||
assert.deepEqual(both.map(r => r.action).sort(), ['none', 'rotated']);
|
||||
assert.equal(f.state.triagePuts, 2, 'two buses rotated once');
|
||||
const rotated = both.find(r => r.action === 'rotated');
|
||||
const record = f.record();
|
||||
assert.equal(rotated.keyId, record.keyId);
|
||||
assert.deepEqual(record.retiring.map(r => r.keyId), [issued.keyId]);
|
||||
assert.equal(f.triageKeys().length, 2, 'the old key stays live through its grace');
|
||||
// Inside the grace both buses leave it; at retireAt plus the margin one of them retires it.
|
||||
const retireAt = Date.parse(record.retiring[0].retireAt);
|
||||
f.clock.now = retireAt + 10_000;
|
||||
assert.deepEqual((await Promise.all([a.sweep(), b.sweep()])).map(r => r.action), ['none', 'none']);
|
||||
f.clock.now = retireAt + 30_000;
|
||||
const retired = await Promise.all([a.sweep(), b.sweep()]);
|
||||
assert.deepEqual(retired.map(r => r.action).sort(), ['none', 'retired']);
|
||||
assert.equal(f.state.triagePuts, 3);
|
||||
assert.deepEqual(f.triageKeys().map(id), [record.keyId]);
|
||||
assert.deepEqual(f.record().retiring, []);
|
||||
// A second rotation 30 days on: again once.
|
||||
f.clock.now = Date.parse(`${record.rotateBy}T00:00:00Z`) + HOUR;
|
||||
const again = await Promise.all([b.sweep(), a.sweep()]);
|
||||
assert.deepEqual(again.map(r => r.action).sort(), ['none', 'rotated']);
|
||||
assert.equal(f.state.triagePuts, 4);
|
||||
assert.equal(f.triageKeys().length, 2);
|
||||
// Namespace guard across every sweep.
|
||||
for (const k of [f.sentinel, f.orgKey, 'foreign-client-key']) assert.ok(f.state.keys.includes(k), k.slice(0, 4));
|
||||
const ops = f.receipts().filter(r => r.op?.startsWith('triage.')).map(r => r.op);
|
||||
assert.deepEqual(ops, ['triage.key.ensure', 'triage.key.rotate', 'triage.key.retire', 'triage.key.rotate']);
|
||||
for (const bus of [a, b]) assert.equal(leaks(eventsText(bus, bus === a)), null);
|
||||
});
|
||||
|
||||
test('the sweep never issues the first key, and a sweep refusal is a receipt line with a code only', async t => {
|
||||
const lines = captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha');
|
||||
f.clock.now += 400 * DAY;
|
||||
assert.deepEqual(await a.sweep(), { action: 'none' });
|
||||
assert.equal(f.state.triagePuts, 0);
|
||||
assert.equal(f.record(), null);
|
||||
// A due rotation on a proxy with no sentinel (it may be another proxy): refused, nothing written.
|
||||
await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
f.state.keys = f.state.keys.filter(k => k !== f.sentinel);
|
||||
f.clock.now += 31 * DAY;
|
||||
const r = await a.sweep();
|
||||
assert.deepEqual(r, { action: 'refused', code: 'proxy-not-configured' });
|
||||
assert.equal(f.state.triagePuts, 1);
|
||||
const refusal = lines.join('').split('\n').filter(l => l.includes('triage.key.sweep-refused'));
|
||||
assert.equal(refusal.length, 1);
|
||||
assert.deepEqual(JSON.parse(refusal[0].replace(/^model-proxy: /, '')), { action: 'triage.key.sweep-refused', code: 'proxy-not-configured', reason: 'triage-key-proxy-open' });
|
||||
// The same refusal on the next ticks is not logged again within the hour; after it, once more.
|
||||
const refusals = () => lines.join('').split('\n').filter(l => l.includes('triage.key.sweep-refused')).length;
|
||||
for (let i = 0; i < 3; i++) { f.clock.now += 30_000; assert.equal((await a.sweep()).code, 'proxy-not-configured'); }
|
||||
assert.equal(refusals(), 1);
|
||||
f.clock.now += HOUR;
|
||||
await a.sweep();
|
||||
assert.equal(refusals(), 2);
|
||||
});
|
||||
|
||||
test('a revoke stopped between the proxy write and the file is finished by the next revoke; ensure after a stopped revoke replaces the key', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
let stage = null;
|
||||
const a = await f.bus('alpha', { triageKeys: { hook: s => { if (s === stage) throw new Error(`stop at ${s}`); } } });
|
||||
await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
stage = 'write';
|
||||
await assert.rejects(a.ask('proxy.triage.key', { op: 'revoke' }), e => e.code === 'storage-unavailable' && !/stop at/.test(e.message));
|
||||
assert.equal(f.triageKeys().length, 0, 'the proxy write happened');
|
||||
assert.ok(f.record(), 'the file stayed');
|
||||
stage = null;
|
||||
// ensure now sees a file whose key the proxy lacks: a new key, the old named lost.
|
||||
const replaced = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.equal(replaced.action, 'replaced');
|
||||
stage = 'rename';
|
||||
await assert.rejects(a.ask('proxy.triage.key', { op: 'revoke' }), code('storage-unavailable'));
|
||||
stage = null;
|
||||
assert.equal((await a.ask('proxy.triage.key', { op: 'revoke' })).action, 'revoked');
|
||||
assert.equal(f.record(), null);
|
||||
assert.equal(f.triageKeys().length, 0);
|
||||
});
|
||||
|
||||
test('no answer, event, receipt line or stderr line in this file holds any Triage key the stand-in saw', () => {
|
||||
assert.ok(SEEN.size >= 8, `only ${SEEN.size} keys were seen`);
|
||||
assert.ok(TEXTS.length >= 30, `only ${TEXTS.length} texts`);
|
||||
for (const text of TEXTS) assert.equal(leaks(text), null);
|
||||
});
|
||||
@@ -0,0 +1,125 @@
|
||||
// proxy-scope slice 4 (#1873): the bus verb proxy.triage.key at the broker. Only the instance
|
||||
// owner's path-bound proof (a human cap from the cli transport, outside any agent) reaches the
|
||||
// runtime's hook; an agent, a tester, a Console proof, a grant holder and an organization owner get
|
||||
// human-required, a reader read-only. The answer and the event carry key ids only, and a hook
|
||||
// failure that is not a bus refusal becomes storage-unavailable with no text of its own.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { Broker, BusError } from '../src/broker.mjs';
|
||||
import { Store } from '../src/store.mjs';
|
||||
import { REFUSALS } from '../src/refusals.mjs';
|
||||
|
||||
const role = { authority: { withinRole: ['message.send'], crossRole: [] } };
|
||||
const code = name => error => error.code === name;
|
||||
const SUBJECT = { provider: 'fixture', iss: 'https://sso.invalid', sub: 'owner-1' };
|
||||
// A Triage key as triage-key.mjs makes them, so a leak check has a real-looking value to look for.
|
||||
const KEY = `mst-0123456789ab-triage-${'Q'.repeat(20)}xY_-${'z'.repeat(19)}`;
|
||||
|
||||
function fixture(t, { proxyTriageKey = null } = {}) {
|
||||
const store = new Store(fs.mkdtempSync(join(tmpdir(), 'tkv-bus-')));
|
||||
t.after(() => store.close());
|
||||
const b = new Broker({ store, proxyTriageKey, businesses: { demo: { id: 'demo', human: 'owner', arbiters: { technical: 'chief', delivery: 'chief' }, vars: { 'bus.tester': true }, roles: { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery'], crossRole: [] } }, coder: role } } } });
|
||||
return {
|
||||
store, b,
|
||||
human: b.bindHuman({ business: 'demo', human: 'owner', via: 'cli', outsideAgent: true }),
|
||||
tester: b.bindTester({ business: 'demo' }), reader: b.bindReader({ business: 'demo' }),
|
||||
agent: b.bindLaunch({ business: 'demo', role: 'coder', run: 'tkv-run', harness: 'pi' }),
|
||||
// The owner's Console proof, a granted account that holds org.hire, and an organization owner
|
||||
// who is not the instance owner: none is the path-bound cli proof.
|
||||
console: b.bindConsole({ business: 'demo', human: 'owner', subject: SUBJECT }),
|
||||
grant: b.bindHireConsole({ business: 'demo', actor: 'grantee', subject: { ...SUBJECT, sub: 'grantee-1' }, rights: ['org.hire', 'decide'] }),
|
||||
orgOwner: b.bindHireConsole({ business: 'demo', actor: 'orgowner', subject: { ...SUBJECT, sub: 'orgowner-1' }, rights: ['org.hire'], owner: true }),
|
||||
};
|
||||
}
|
||||
const events = store => JSON.stringify(store.all('SELECT body FROM events'));
|
||||
const bodies = store => store.all('SELECT body FROM events').map(r => JSON.parse(r.body));
|
||||
|
||||
test('proxy.triage.key refuses an agent, a tester, a Console proof, a grant holder and an organization owner with human-required, and a reader with read-only', async t => {
|
||||
let calls = 0;
|
||||
const f = fixture(t, { proxyTriageKey: async () => { calls++; return { action: 'issued', keyId: '0123456789ab', rotateBy: '2026-12-19', addedKeyIds: ['0123456789ab'], removedKeyIds: [] }; } });
|
||||
for (const op of ['ensure', 'revoke']) {
|
||||
for (const [name, cap] of [['agent', f.agent], ['tester', f.tester], ['console', f.console], ['grant', f.grant], ['orgOwner', f.orgOwner]])
|
||||
assert.throws(() => f.b.request(cap, { verb: 'proxy.triage.key', args: { op } }), code('human-required'), `${name} ${op}`);
|
||||
assert.throws(() => f.b.request(f.reader, { verb: 'proxy.triage.key', args: { op } }), code('read-only'));
|
||||
}
|
||||
assert.equal(calls, 0);
|
||||
// The same verb runs for the owner's cli proof.
|
||||
const answer = await f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } });
|
||||
assert.equal(answer.action, 'issued');
|
||||
assert.equal(calls, 1);
|
||||
});
|
||||
|
||||
test('proxy.triage.key takes op ensure or revoke and nothing else, and a bus with no hook is not-configured', async t => {
|
||||
let calls = 0;
|
||||
const f = fixture(t, { proxyTriageKey: async () => { calls++; return { action: 'none', addedKeyIds: [], removedKeyIds: [] }; } });
|
||||
for (const args of [{}, { op: 'rotate' }, { op: 'issue' }, { op: 'ensure', key: KEY }, { op: 'revoke', force: true }, { op: ['ensure'] }])
|
||||
assert.throws(() => f.b.request(f.human, { verb: 'proxy.triage.key', args }), code('invalid-request'), JSON.stringify(args));
|
||||
assert.throws(() => f.b.request(f.human, { verb: 'proxy.triage.key' }), code('invalid-request'));
|
||||
assert.equal(calls, 0);
|
||||
assert.equal(events(f.store).includes(KEY), false);
|
||||
assert.equal(events(f.store).includes(KEY.slice(-43)), false);
|
||||
const g = fixture(t);
|
||||
assert.throws(() => g.b.request(g.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } }), code('not-configured'));
|
||||
});
|
||||
|
||||
test('the answer and the event of each op carry ids only, even when the hook returns more', async t => {
|
||||
const seen = [];
|
||||
const f = fixture(t, { proxyTriageKey: async op => {
|
||||
seen.push(op);
|
||||
// A hook that returns the key beside the ids: the broker copies the named fields only.
|
||||
return op === 'ensure'
|
||||
? { action: 'issued', keyId: '0123456789ab', rotateBy: '2026-12-19', addedKeyIds: ['0123456789ab'], removedKeyIds: ['ba9876543210'], key: KEY, old: { key: KEY } }
|
||||
: { action: 'revoked', addedKeyIds: [], removedKeyIds: ['0123456789ab'], key: KEY };
|
||||
} });
|
||||
const ensure = await f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } });
|
||||
assert.deepEqual(ensure, { op: 'ensure', action: 'issued', addedKeyIds: ['0123456789ab'], removedKeyIds: ['ba9876543210'], keyId: '0123456789ab', rotateBy: '2026-12-19' });
|
||||
const revoke = await f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'revoke' } });
|
||||
assert.deepEqual(revoke, { op: 'revoke', action: 'revoked', addedKeyIds: [], removedKeyIds: ['0123456789ab'] });
|
||||
assert.deepEqual(seen, ['ensure', 'revoke']);
|
||||
assert.deepEqual(bodies(f.store).filter(b => b.call === 'proxy.triage.key'), [{ call: 'proxy.triage.key', ...ensure }, { call: 'proxy.triage.key', ...revoke }]);
|
||||
for (const text of [JSON.stringify(ensure), JSON.stringify(revoke), events(f.store)]) {
|
||||
assert.equal(text.includes(KEY), false);
|
||||
assert.equal(text.includes(KEY.slice(-43)), false);
|
||||
}
|
||||
});
|
||||
|
||||
test('an answer the broker cannot vouch for refuses proxy-client-response and records no allowed event', async t => {
|
||||
const answers = [
|
||||
null, { action: 'issued' },
|
||||
{ action: 'deleted', keyId: '0123456789ab', addedKeyIds: [], removedKeyIds: [] },
|
||||
{ action: 'issued', keyId: KEY, addedKeyIds: [], removedKeyIds: [] },
|
||||
{ action: 'issued', keyId: '0123456789ab', addedKeyIds: [KEY], removedKeyIds: [] },
|
||||
{ action: 'held', keyId: '0123456789ab', addedKeyIds: [], removedKeyIds: 'none' },
|
||||
];
|
||||
let next = 0;
|
||||
const f = fixture(t, { proxyTriageKey: async () => answers[next++] });
|
||||
for (const answer of answers)
|
||||
await assert.rejects(f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } }), code('proxy-client-response'), JSON.stringify(answer));
|
||||
await assert.rejects(Promise.resolve().then(() => fixture(t, { proxyTriageKey: async () => ({ action: 'issued', addedKeyIds: [], removedKeyIds: [] }) }))
|
||||
.then(g => g.b.request(g.human, { verb: 'proxy.triage.key', args: { op: 'revoke' } })), code('proxy-client-response'));
|
||||
assert.equal(bodies(f.store).some(b => b.call === 'proxy.triage.key'), false);
|
||||
assert.equal(events(f.store).includes(KEY.slice(-43)), false);
|
||||
});
|
||||
|
||||
test('a bus refusal from the hook passes through with its guidance; any other failure is storage-unavailable and names nothing from the error', async t => {
|
||||
const f = fixture(t, { proxyTriageKey: async op => { throw op === 'ensure' ? new BusError('triage-no-riding-bus') : Object.assign(new Error(`boom ${KEY} /home/x`), { code: 'EACCES' }); } });
|
||||
await assert.rejects(f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } }), e => e.code === 'triage-no-riding-bus'
|
||||
&& /Triage needs one organization that uses the owner's models/.test(e.nextStep ?? REFUSALS['triage-no-riding-bus'].nextStep));
|
||||
await assert.rejects(f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'revoke' } }), e => e.code === 'storage-unavailable'
|
||||
&& !JSON.stringify({ ...e, message: e.message }).includes(KEY.slice(-43)) && !String(e.message).includes('/home/x'));
|
||||
assert.equal(events(f.store).includes(KEY.slice(-43)), false);
|
||||
});
|
||||
|
||||
test('the two slice 4 codes have refusal-table entries with a next step and no absolute path', () => {
|
||||
for (const c of ['triage-no-riding-bus', 'triage-key-rotated']) {
|
||||
const entry = REFUSALS[c];
|
||||
assert.ok(entry, c);
|
||||
assert.equal(typeof entry.nextStep, 'string', c);
|
||||
assert.ok(entry.nextStep.length > 20, c);
|
||||
assert.equal(/\/(home|mnt|srv|tmp)\//.test(entry.nextStep), false, c);
|
||||
}
|
||||
assert.match(REFUSALS['triage-no-riding-bus'].nextStep, /^Triage needs one organization that uses the owner's models\./);
|
||||
});
|
||||
@@ -215,12 +215,19 @@ const relayed = (failed) => failed && (typeof failed.message === "string" ? { ..
|
||||
// and names the module rather than escaping the host. What it threw is named by its system code
|
||||
// or its class, never its message, which the host didn't write (#1873).
|
||||
function call(name, what, fn) {
|
||||
try {
|
||||
return fn();
|
||||
} catch (e) {
|
||||
const failed = (e) => {
|
||||
const thrown = quietMessage(e, "a host file") ?? `threw ${/^[A-Za-z][A-Za-z0-9]{0,39}$/.test(String(e?.name)) ? e.name : "a value"} without a system code`;
|
||||
throw new Refusal("harness-failed", `harness ${name} ${what}: ${thrown}`);
|
||||
};
|
||||
let out;
|
||||
try {
|
||||
out = fn();
|
||||
} catch (e) {
|
||||
failed(e);
|
||||
}
|
||||
// #1873 pi-unpin re-check 7: a module may answer with a promise (pi's preflight runs its fit
|
||||
// check as child processes). The caller awaits it; a rejection is refused as a throw is.
|
||||
return typeof out?.then === "function" ? Promise.resolve(out).then(undefined, failed) : out;
|
||||
}
|
||||
|
||||
// host: startHost()'s handle. tracker: true when the broker has task verbs for
|
||||
@@ -302,6 +309,11 @@ export function createLauncher({
|
||||
let ticking = false;
|
||||
const stopping = new Set();
|
||||
const refusedThreads = new Map();
|
||||
// #1873 chat-fail F2: the refusal and message each thread session's plain line was asked for.
|
||||
const noticedThreads = new Map();
|
||||
// #1873 F2: the last refusal seen per thread start, and the last failed post of its line.
|
||||
const refusalSeen = new Map();
|
||||
const noticeFailed = new Map();
|
||||
const startedFor = new Map();
|
||||
// Row 271 (264d): each clear's stop time (by op), and each clear refusal logged once (by op and code).
|
||||
const clearStops = new Map();
|
||||
@@ -449,14 +461,11 @@ export function createLauncher({
|
||||
for (const m of [primary, fallback].filter(Boolean)) {
|
||||
if (!on.includes(m.harness)) throw new Refusal("harness-off", `harness ${m.harness} isn't switched on in plugins.harness`);
|
||||
}
|
||||
// One harness through a fresh loader, so a refusal never leaves an earlier load's module in use.
|
||||
const harnessOf = async (m) => {
|
||||
const loader = makeLoader({ dataRoot:system.instanceRoot ?? dataRoot, log: (entry) => record({ ...entry, instance }) });
|
||||
const { refused } = await loader.load({ harness: [m.harness] });
|
||||
const { refused, module } = await freshHarness(m, (entry) => record({ ...entry, instance }));
|
||||
// #1873: a loader message can quote the module's path or its load error; only a
|
||||
// missing-export one, which names the export, is relayed. Any other refusal names its code.
|
||||
if (refused.length) throw new Refusal("harness-refused", `harness ${m.harness}: ${refused.map((r) => r.code === "missing-export" ? `${r.code}: ${relay(r.message)}` : String(r.code)).join("; ")}`);
|
||||
const module = loader.modules("harness")[0];
|
||||
const levels = call(m.harness, "levels", () => module.levels());
|
||||
if (!levels.includes(m.thinking)) throw new Refusal("thinking-unsupported", `harness ${m.harness} takes thinking ${levels.join(", ")}, not ${m.thinking}`);
|
||||
return module;
|
||||
@@ -551,7 +560,7 @@ export function createLauncher({
|
||||
const pickRoute = async (proxyOf) => {
|
||||
let sessionProxy = await proxyOf(primary);
|
||||
// #1873: a preflight's message is the module's own text, relayed with host paths replaced.
|
||||
const failed = relayed(call(primary.harness, "preflight", () => harness.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
const failed = relayed(await call(primary.harness, "preflight", () => harness.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
if (failed) {
|
||||
if (!fallback) throw new Refusal(failed.code, failed.message);
|
||||
// The fallback only on a failed primary preflight, and only at launch (row 65). Any refusal of
|
||||
@@ -561,7 +570,7 @@ export function createLauncher({
|
||||
throw e;
|
||||
});
|
||||
sessionProxy = await proxyOf(fallback);
|
||||
const also = relayed(call(fallback.harness, "preflight", () => second.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
const also = relayed(await call(fallback.harness, "preflight", () => second.preflight({ env: sessionProxy ? sessionEnv(env, "preflight", sessionProxy) : env, proxy: sessionProxy })));
|
||||
if (also) throw new Refusal(also.code, `fallback ${fallback.harness}: ${also.message} (primary ${primary.harness}: ${failed.message})`);
|
||||
[model, harness, route, routeReason] = [fallback, second, "fallback", failed.code];
|
||||
}
|
||||
@@ -735,7 +744,7 @@ export function createLauncher({
|
||||
if (store) compactions(store, model.harness, harness, { instance, thread, session, run });
|
||||
const toolSocket = join(runDir, "tools.sock");
|
||||
const turnMarker = join(runDir, "turn.done");
|
||||
const version = harnessVersion !== undefined ? harnessVersion : call(model.harness, "version", () => harness.version({ env }));
|
||||
const version = harnessVersion !== undefined ? harnessVersion : await call(model.harness, "version", () => harness.version({ env }));
|
||||
// Row 137 (#1631): a role and thread holding an approved, unspent merge decision get land_work.
|
||||
// The broker checks the decision again on every land; this only decides what the bundle shows.
|
||||
// Row 156 (#1651): a non-thread instance's land and landing come from one landHolders read, so a
|
||||
@@ -896,7 +905,13 @@ export function createLauncher({
|
||||
}
|
||||
children.set(run, exited);
|
||||
const outputWatch = admittedProxy ? watchModelOutput({ runDir, armAtFirstTurn: true, keyId: modelKeyId, seconds: system.proxy.firstOutputSeconds ?? 120,
|
||||
stop: () => host.op({ op: 'modelStall', business: b.id, role: instance, run, keyId: modelKeyId }), receipt: record }) : null;
|
||||
stop: async () => {
|
||||
const out = await host.op({ op: 'modelStall', business: b.id, role: instance, run, keyId: modelKeyId });
|
||||
// #1873 chat-fail F2: the message the stopped turn was on gets one plain line, as a failed turn's.
|
||||
try { await host.op({ op: 'failureNotice', record: { business: b.id, run, code: 'proxy-stall' } }); }
|
||||
catch (err) { log(`run ${run}: failure line refused: ${err.code ?? 'failed'}`); }
|
||||
return out;
|
||||
}, receipt: record }) : null;
|
||||
exited.then(async ({ code, signal }) => {
|
||||
outputWatch?.cancel();
|
||||
let receipt;
|
||||
@@ -1066,8 +1081,30 @@ export function createLauncher({
|
||||
await start({ instance: t.instance, launchedBy: b.human, ...where, legacy: t.legacy, legacyThread: legacyThreads.has(t.thread), ...(await check(t.instance, where)) });
|
||||
startedFor.set(key, signature);
|
||||
refusedThreads.delete(key);
|
||||
noticedThreads.delete(key);
|
||||
refusalSeen.delete(key);
|
||||
noticeFailed.delete(key);
|
||||
} catch (e) {
|
||||
const code = e instanceof Refusal ? e.code : "launch-failed";
|
||||
// #1873 chat-fail F2: the person who wrote gets one plain line per message the refusal
|
||||
// leaves unanswered. Asked once per code and message; the bus keeps it to one per message.
|
||||
// launch-failed can be a lost race that the next try wins, so it gets a line only when
|
||||
// the same message is refused with it twice in a row. A post that fails is tried again
|
||||
// on the next tick, and its failure is logged once.
|
||||
const noticed = `${code}|${signature}`;
|
||||
const repeated = refusalSeen.get(key) === noticed;
|
||||
refusalSeen.set(key, noticed);
|
||||
if (noticedThreads.get(key) !== noticed && (code !== "launch-failed" || repeated)) {
|
||||
try {
|
||||
await host.op({ op: "failureNotice", record: { business: b.id, thread: t.thread, session: t.session, code } });
|
||||
noticedThreads.set(key, noticed);
|
||||
noticeFailed.delete(key);
|
||||
} catch (err) {
|
||||
const failed = `${noticed}|${err.code ?? "failed"}`;
|
||||
if (noticeFailed.get(key) !== failed) log(`thread ${t.thread} session ${t.session}: failure line refused: ${err.code ?? "failed"}`);
|
||||
noticeFailed.set(key, failed);
|
||||
}
|
||||
}
|
||||
// Logged once per code, not once per tick.
|
||||
if (refusedThreads.get(key) === code) return;
|
||||
refusedThreads.set(key, code);
|
||||
@@ -1637,6 +1674,38 @@ export function createLauncher({
|
||||
clearTimeout(timer);
|
||||
}
|
||||
|
||||
// One harness ({ harness: name }) through a fresh loader, so a refusal never leaves an earlier
|
||||
// load's module in use: { refused, module }, module null when refused. A launch (harnessOf) and
|
||||
// the start's fit warm-up below (#1873 re-check 8) both load through here.
|
||||
async function freshHarness(m, log) {
|
||||
const loader = makeLoader({ dataRoot:system.instanceRoot ?? dataRoot, log });
|
||||
const { refused } = await loader.load({ harness: [m.harness] });
|
||||
return { refused, module: refused.length ? null : loader.modules("harness")[0] };
|
||||
}
|
||||
|
||||
// #1873 pi-unpin re-check 8: each switched-on harness module with ready() (pi's fit check) starts
|
||||
// it here, in the background, so the first launch joins the check in flight instead of starting
|
||||
// it. Nothing waits on it: a launch still awaits its own preflight, which shares this check or
|
||||
// reads its pass. A warm-up that fails or throws is dropped; it caches nothing, and the launch's
|
||||
// preflight runs the check again. Re-check 9: a harness plugin the warm-up can't load leaves one
|
||||
// launch log entry, by its code only (a launch that loads it records the message).
|
||||
function warmHarnesses() {
|
||||
const on = Array.isArray(b.plugins?.harness) ? b.plugins.harness : [];
|
||||
for (const name of on) {
|
||||
(async () => {
|
||||
let logged = false;
|
||||
const refusal = (entry) => {
|
||||
if (logged || entry?.event !== "plugin-refused") return;
|
||||
logged = true;
|
||||
record({ event: "plugin-refused", kind: entry.kind, name: entry.name, code: entry.code, during: "warm-up" });
|
||||
};
|
||||
const { module } = await freshHarness({ harness: name }, refusal);
|
||||
if (typeof module?.ready === "function") await module.ready({ env });
|
||||
})().catch(() => {});
|
||||
}
|
||||
}
|
||||
if (launches) warmHarnesses();
|
||||
|
||||
host.beforeClose(close);
|
||||
return { business: b, registry, listen, launchPm, fromSession, tick, close };
|
||||
}
|
||||
@@ -16,7 +16,7 @@
|
||||
//
|
||||
// mosaic triage key issue | rotate | retire | status
|
||||
//
|
||||
// Triage's own model proxy client key (packages/cli/src/triage-key.mjs). Human verbs as well,
|
||||
// Triage's own model proxy client key (packages/bus/src/triage-key.mjs, shared with the bus). Human verbs as well,
|
||||
// refused in an agent run before anything is read. No output names a key, only its id.
|
||||
import { lstatSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
@@ -24,7 +24,7 @@ import { fileURLToPath } from "node:url";
|
||||
import { readRoute, RouteStoreError, writeRoute } from "../../webui/src/triage-route-store.mjs";
|
||||
import { loadSystem } from "./config.mjs";
|
||||
import { configDir } from "../../organization/src/organization-config.mjs";
|
||||
import { issueKey, keyPaths, keyStatus, retireKeys, rotateKey, TriageKeyError } from "./triage-key.mjs";
|
||||
import { issueKey, keyPaths, keyStatus, retireKeys, rotateKey, TriageKeyError } from "../../bus/src/triage-key.mjs";
|
||||
import { CliError } from "./errors.mjs";
|
||||
import { refuseInsideAgent } from "./transport.mjs";
|
||||
|
||||
|
||||
@@ -105,6 +105,7 @@ async function start(t, { f, adapter }, { businessId = "acme", orgRoot, copyTran
|
||||
credentialStatus: (m) => runtime.credentialStatus(m.business, m.role),
|
||||
threadsState: (m) => runtime.threadsState(m.business),
|
||||
threadIdle: (m) => runtime.threadIdle(m.record),
|
||||
failureNotice: (m) => runtime.failureNotice(m.record),
|
||||
mayLaunch: (m) => runtime.mayLaunch(m),
|
||||
landHeld: (m) => runtime.landHeld(m.record),
|
||||
landHolders: (m) => runtime.landHolders(m.business),
|
||||
|
||||
@@ -99,6 +99,7 @@ async function start(t, { f, adapter }) {
|
||||
credentialStatus: (m) => runtime.credentialStatus(m.business, m.role),
|
||||
threadsState: (m) => runtime.threadsState(m.business),
|
||||
threadIdle: (m) => runtime.threadIdle(m.record),
|
||||
failureNotice: (m) => runtime.failureNotice(m.record),
|
||||
mayLaunch: (m) => runtime.mayLaunch(m),
|
||||
landHeld: (m) => runtime.landHeld(m.record),
|
||||
landHolders: (m) => runtime.landHolders(m.business),
|
||||
|
||||
@@ -30,6 +30,7 @@ const ops = {
|
||||
credentialStatus: m => runtime.credentialStatus(m.business, m.role),
|
||||
threadsState: m => runtime.threadsState(m.business),
|
||||
threadIdle: m => runtime.threadIdle(m.record),
|
||||
failureNotice: m => runtime.failureNotice(m.record),
|
||||
mayLaunch: m => runtime.mayLaunch(m),
|
||||
landHeld: m => runtime.landHeld(m.record),
|
||||
landHolders: m => runtime.landHolders(m.business),
|
||||
|
||||
+2
-1
@@ -16,7 +16,7 @@ import { pushWorking, fetchReview } from '../../../harness/src/git-tools.mjs';
|
||||
import { callTool } from '../../../harness/src/tools.mjs';
|
||||
import { toolServer } from '../../../harness/src/runner.mjs';
|
||||
import { buildBundle } from '../../../harness/src/bundle.mjs';
|
||||
import { HARNESS } from '../../../harness/tests/helpers.mjs';
|
||||
import { HARNESS, harnessReady } from '../../../harness/tests/helpers.mjs';
|
||||
import { ensureCheckouts } from '../../../seat/src/checkout.mjs';
|
||||
import { newRun, Registry, readSessions, writeSessionFile, workspaceDir } from '../../../seat/src/session.mjs';
|
||||
import { startTimeOf } from '../../src/host.mjs';
|
||||
@@ -127,6 +127,7 @@ async function runtime() {
|
||||
const cap = bus.bindLaunch(launch_record).cap;
|
||||
roles[role] = { cap, run: id, workspace, runDir };
|
||||
call(role, 'role.claim');
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
const bundle = buildBundle({ dir: join(runDir, 'bundle'), resolved, contract: readFileSync(resolved.contract, 'utf8'),
|
||||
arbiter: role === 'cto', tracker: Boolean(resolved.tracker), credentials: [], skillsDir: join(dataRoot, 'skills-enabled'), harness: HARNESS.pi,
|
||||
harnessVersion: '0.85.1', session: { run: id, workspace, checkouts, toolSocket: join(runDir, 'tools.sock'), turnMarker: join(runDir, 'turn.done') } });
|
||||
|
||||
@@ -77,6 +77,7 @@ async function start(t, { f, adapter }, threadIdle) {
|
||||
credentialStatus: (m) => runtime.credentialStatus(m.business, m.role),
|
||||
threadsState: (m) => runtime.threadsState(m.business),
|
||||
threadIdle: (m) => threadIdle(runtime, m),
|
||||
failureNotice: (m) => runtime.failureNotice(m.record),
|
||||
mayLaunch: (m) => runtime.mayLaunch(m),
|
||||
landHeld: (m) => runtime.landHeld(m.record),
|
||||
landHolders: (m) => runtime.landHolders(m.business),
|
||||
|
||||
@@ -95,6 +95,7 @@ async function start(t, { f, adapter }, { tickMs = 100, ops: replace } = {}) {
|
||||
credentialStatus: (m) => runtime.credentialStatus(m.business, m.role),
|
||||
threadsState: (m) => runtime.threadsState(m.business),
|
||||
threadIdle: (m) => runtime.threadIdle(m.record),
|
||||
failureNotice: (m) => runtime.failureNotice(m.record),
|
||||
mayLaunch: (m) => runtime.mayLaunch(m),
|
||||
landHeld: (m) => runtime.landHeld(m.record),
|
||||
landHolders: (m) => runtime.landHolders(m.business),
|
||||
|
||||
@@ -48,6 +48,7 @@ function requestHost(x) {
|
||||
landHeld: m => x.bus.landHeld(m.record), landHolders: m => x.bus.landHolders(m.business),
|
||||
sessionClears: m => x.bus.sessionClears(m.business),
|
||||
threadsState: m => x.bus.threadsState(m.business), lifecycleRequests: m => x.bus.lifecycleRequests(m.business),
|
||||
failureNotice: m => x.bus.failureNotice(m.record),
|
||||
'request.admission': m => x.bus.admission(m), 'request.launches': m => x.bus.requestLaunches(m.business),
|
||||
'request.launchResult': m => x.bus.requestLaunchResult(m.record), 'request.manifest': m => x.bus.requestManifest(m.record),
|
||||
'request.release': m => x.bus.requestRelease(m.cap, m.record), 'request.chief': m => x.bus.requestChief(m.business),
|
||||
|
||||
@@ -12,7 +12,8 @@ import { createHash } from "node:crypto";
|
||||
import { once } from "node:events";
|
||||
import { createInterface } from "node:readline";
|
||||
import { connect } from "node:net";
|
||||
import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, symlinkSync, watch, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { Client } from "../../bus/src/client.mjs";
|
||||
@@ -24,6 +25,8 @@ import { bootConfig, loadSystem } from "../src/config.mjs";
|
||||
import { cmdlineOf, startHost, startTimeOf } from "../src/host.mjs";
|
||||
import { createLauncher } from "../src/launcher.mjs";
|
||||
import { mockAnthropic, REPO } from "../../harness/tests/helpers.mjs";
|
||||
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
|
||||
import * as pi from "../../plugins/harness/pi/index.mjs";
|
||||
import { fakeHarnesses, fixture, HOST_CREDENTIALS, tmp, withoutHostCredentials } from "./helpers.mjs";
|
||||
|
||||
const FAKE = fileURLToPath(new URL("../../harness/tests/fixtures/fake-adapter.mjs", import.meta.url));
|
||||
@@ -151,7 +154,8 @@ const log = (f) => readFileSync(launchLogFile(f.dataRoot, "acme"), "utf8").trim(
|
||||
test('P3 proxy key reaches the spawned runner but never session.json or the bundle', { skip, timeout: 30000 }, async t => {
|
||||
const proxy = { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' };
|
||||
const prepared = prepare(t, doc => {
|
||||
doc.launch.models['claude-opus-5-5'] = { provider: 'proxy', model: 'claude-fixture', route: 'anthropic' };
|
||||
// #1873 harness-matrix: a Claude route on pi is refused, so the key check rides a non-Claude proxy route.
|
||||
doc.launch.models['claude-opus-5-5'] = { provider: 'proxy', model: 'fixture-route-model', route: 'glm' };
|
||||
return doc;
|
||||
});
|
||||
const { launcher, f, close } = await setup(t, doc => doc, prepared, { testProxy: proxy });
|
||||
@@ -265,6 +269,142 @@ test("bus start --pm: the launcher instance (the chief of staff) runs under its
|
||||
assert.equal(end.exitCode, 0);
|
||||
});
|
||||
|
||||
// #1873 pi-unpin S1/S2: the version label comes from the pi the module finds. A pi whose --version
|
||||
// fails gives a null label, and the launch still goes ahead.
|
||||
test("pi-unpin: a pi named by MOSAIC_PI_BIN that exits 1 on --version records a null harnessVersion and still launches", { skip, timeout: 30000 }, async (t) => {
|
||||
const prepared = prepare(t);
|
||||
const pkg = join(prepared.root, "pi-prefix", "lib", "node_modules", "@earendil-works", "pi-coding-agent");
|
||||
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "3.0.0", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
// #1873 S3: the stand-in passes the fit check; only its --version fails.
|
||||
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn({ versionExit: 1 }), { mode: 0o755 });
|
||||
mkdirSync(join(prepared.root, "pi-prefix", "bin"));
|
||||
const bin = join(prepared.root, "pi-prefix", "bin", "pi");
|
||||
symlinkSync("../lib/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js", bin);
|
||||
prepared.f.env.MOSAIC_PI_BIN = bin;
|
||||
// env() reads the bus's own environment, as the launcher's is process.env outside tests.
|
||||
const before = process.env.MOSAIC_PI_BIN;
|
||||
process.env.MOSAIC_PI_BIN = bin;
|
||||
t.after(() => { if (before === undefined) delete process.env.MOSAIC_PI_BIN; else process.env.MOSAIC_PI_BIN = before; });
|
||||
const { f, launcher, close } = await setup(t, (doc) => doc, prepared, { harnessVersion: undefined });
|
||||
const chief = await launcher.launchPm();
|
||||
assert.equal(chief.harness, "pi");
|
||||
const [s] = readSessions(f.dataRoot);
|
||||
const manifest = JSON.parse(readFileSync(join(s.runDir, "bundle", "manifest.json"), "utf8"));
|
||||
assert.equal(manifest.harnessVersion, null);
|
||||
assert.equal(await close(), 0);
|
||||
});
|
||||
|
||||
// #1873 re-check 8: a pi stand-in under root/pi-prefix, as an npm prefix holds it. Returns its bin.
|
||||
function piPrefix(root, knobs) {
|
||||
const pkg = join(root, "pi-prefix", "lib", "node_modules", "@earendil-works", "pi-coding-agent");
|
||||
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "3.0.0", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), piStandIn(knobs), { mode: 0o755 });
|
||||
mkdirSync(join(root, "pi-prefix", "bin"));
|
||||
const bin = join(root, "pi-prefix", "bin", "pi");
|
||||
symlinkSync("../lib/node_modules/@earendil-works/pi-coding-agent/dist/bundle/cli.js", bin);
|
||||
return bin;
|
||||
}
|
||||
// Each fit run makes one scratch directory under tmpdir(), so a watch there counts the runs.
|
||||
// quiet() waits until no run has started for a while, so a check an earlier test left going
|
||||
// (the real pi's, warmed by its launcher) is not counted.
|
||||
function fitRuns(t) {
|
||||
const made = new Set();
|
||||
const watcher = watch(tmpdir(), (event, name) => { if (name && name.startsWith("mosaic-pi-fit-")) made.add(name); });
|
||||
t.after(() => watcher.close());
|
||||
const quiet = async () => {
|
||||
let n;
|
||||
do { n = made.size; await new Promise((r) => setTimeout(r, 700)); } while (made.size !== n);
|
||||
};
|
||||
return { made, quiet };
|
||||
}
|
||||
function busPiBin(t, prepared, bin) {
|
||||
prepared.f.env.MOSAIC_PI_BIN = bin;
|
||||
const before = process.env.MOSAIC_PI_BIN;
|
||||
process.env.MOSAIC_PI_BIN = bin;
|
||||
t.after(() => { if (before === undefined) delete process.env.MOSAIC_PI_BIN; else process.env.MOSAIC_PI_BIN = before; });
|
||||
}
|
||||
|
||||
// #1873 pi-unpin re-check 8: the launcher starts pi's fit check when it starts, before any launch,
|
||||
// and does not wait for it. The first launch joins that check: the two together make one check's
|
||||
// runs, not two.
|
||||
test("pi-unpin re-check 8: the launcher starts pi's fit check when it starts, and the first launch joins it", { skip, timeout: 60000 }, async (t) => {
|
||||
const { made, quiet } = fitRuns(t);
|
||||
// One check on its own, for another stand-in: how many runs it makes.
|
||||
const alone = piPrefix(tmp(t, "mosaic-launch-pi-"), {});
|
||||
await quiet();
|
||||
made.clear();
|
||||
assert.equal(await pi.preflight({ env: { ANTHROPIC_API_KEY: "stand-in", MOSAIC_PI_BIN: alone } }), null);
|
||||
await quiet();
|
||||
const runs = made.size;
|
||||
assert.ok(runs > 10, `one check made ${runs} runs`);
|
||||
const prepared = prepare(t);
|
||||
busPiBin(t, prepared, piPrefix(prepared.root, {}));
|
||||
made.clear();
|
||||
const { launcher, close } = await setup(t, (doc) => doc, prepared, { harnessVersion: undefined });
|
||||
// No launch yet, and the check is under way.
|
||||
await until(() => made.size > 0, 10000).catch(() => assert.fail("no fit run started before the first launch"));
|
||||
const chief = await launcher.launchPm();
|
||||
assert.equal(chief.harness, "pi");
|
||||
await quiet();
|
||||
assert.equal(made.size, runs, "the launch joined the check the launcher started");
|
||||
assert.equal(await close(), 0);
|
||||
});
|
||||
|
||||
// A warm-up that fails leaves no unhandled rejection and keeps no verdict: the next preflight runs
|
||||
// the check again and refuses on its own.
|
||||
test("pi-unpin re-check 8: a fit check the launcher starts that fails is dropped: no unhandled rejection, nothing kept", { skip, timeout: 60000 }, async (t) => {
|
||||
const { made, quiet } = fitRuns(t);
|
||||
const unhandled = [];
|
||||
const seen = (e) => unhandled.push(e);
|
||||
process.on("unhandledRejection", seen);
|
||||
t.after(() => process.off("unhandledRejection", seen));
|
||||
const prepared = prepare(t);
|
||||
const bin = piPrefix(prepared.root, { dropFlags: ["--no-themes"] });
|
||||
busPiBin(t, prepared, bin);
|
||||
await quiet();
|
||||
made.clear();
|
||||
const { close } = await setup(t, (doc) => doc, prepared, { harnessVersion: undefined });
|
||||
await until(() => made.size > 0, 10000).catch(() => assert.fail("no fit run started before the first launch"));
|
||||
// This preflight joins the failing check; then it is gone.
|
||||
const env = { ANTHROPIC_API_KEY: "stand-in", MOSAIC_PI_BIN: bin };
|
||||
const refusal = { code: "harness-feature-missing", message: "pi 9.9.9 (MOSAIC_PI_BIN) lacks --no-themes" };
|
||||
assert.deepEqual(await pi.preflight({ env }), refusal);
|
||||
await quiet();
|
||||
await new Promise((r) => setImmediate(r));
|
||||
assert.deepEqual(unhandled, []);
|
||||
made.clear();
|
||||
assert.deepEqual(await pi.preflight({ env }), refusal);
|
||||
await quiet();
|
||||
assert.ok(made.size > 0, "the failed check was kept");
|
||||
assert.equal(await close(), 0);
|
||||
});
|
||||
|
||||
// Re-check 9: a harness plugin the warm-up can't load leaves one launch log entry, by its code
|
||||
// only, so the message (which may name a host path) stays out of it.
|
||||
test("pi-unpin re-check 9: a harness plugin the launcher's warm-up can't load leaves one launch log entry, by code only", { skip, timeout: 30000 }, async (t) => {
|
||||
const given = prepare(t, (doc) => {
|
||||
doc.plugins = { ...doc.plugins, harness: [...doc.plugins.harness, "broken"] };
|
||||
return doc;
|
||||
});
|
||||
const makeLoader = await fakeHarnesses(t, { pi: given.adapter, "claude-code": given.adapter }, (write) => {
|
||||
write("harness", "broken", `throw new Error("broken at /srv/fit-secret-path");\n`);
|
||||
});
|
||||
const { f, close } = await setup(t, undefined, given, { makeLoader });
|
||||
const refused = () => existsSync(launchLogFile(f.dataRoot, "acme")) ? log(f).filter((e) => e.event === "plugin-refused") : [];
|
||||
await until(() => refused().length > 0, 10000).catch(() => assert.fail("the warm-up left no record of the refused plugin"));
|
||||
await new Promise((r) => setTimeout(r, 500));
|
||||
const entries = refused();
|
||||
assert.equal(entries.length, 1, JSON.stringify(entries));
|
||||
const [entry] = entries;
|
||||
assert.deepEqual(Object.keys(entry).sort(), ["at", "code", "during", "event", "kind", "name"]);
|
||||
assert.deepEqual([entry.kind, entry.name, entry.during], ["harness", "broken", "warm-up"]);
|
||||
assert.match(entry.code, /^[a-z][a-z-]+$/);
|
||||
assert.doesNotMatch(readFileSync(launchLogFile(f.dataRoot, "acme"), "utf8"), /fit-secret-path/);
|
||||
assert.equal(await close(), 0);
|
||||
});
|
||||
|
||||
test("the chief of staff launches a coder through its launch tool; the coder answers; refusals name their code", { skip, timeout: 30000 }, async (t) => {
|
||||
const { f, launcher, client, cto } = await setup(t);
|
||||
const sock = launchSocketPath(f.dataRoot);
|
||||
|
||||
@@ -95,6 +95,7 @@ async function start(t, { f, adapter }, options = {}) {
|
||||
definitionScan: (m) => runtime.definitionScan({ business: m.business, definition: m.definition, entry: m.entry }),
|
||||
threadsState: (m) => runtime.threadsState(m.business),
|
||||
threadIdle: (m) => runtime.threadIdle(m.record),
|
||||
failureNotice: (m) => runtime.failureNotice(m.record),
|
||||
mayLaunch: (m) => runtime.mayLaunch(m),
|
||||
landHeld: (m) => runtime.landHeld(m.record),
|
||||
landHolders: (m) => runtime.landHolders(m.business),
|
||||
|
||||
@@ -21,12 +21,20 @@ const id = value => createHash('sha256').update(value).digest('hex').slice(0,12)
|
||||
// #774 R2: extra names more harnesses to switch on and load (a fallback route's); real names the
|
||||
// harnesses loaded as the real module, so their own preflight runs; hostEnv adds launcher env
|
||||
// such as MOSAIC_CLAUDE_BIN.
|
||||
// #1873 harness-matrix: an OAuth Claude model runs on claude-code only, so the pi stream case reads
|
||||
// a non-Claude model the proxy serves in Anthropic format (upstream other) from a stand-in catalogue.
|
||||
// The chief's model key stays claude-opus-5-5; on pi its upstream model is not a Claude name.
|
||||
export const PI_CHIEF_MODEL='fixture-chief-model';
|
||||
const STREAM_OTHER=Object.freeze({provider:'other',model:'fixture-stream-model',module:'cliproxy',upstream:'other'});
|
||||
const streamCatalogue=()=>({invalidate(){},read:async()=>({status:'ready',observedAt:'2026-11-19T00:00:00.000Z',notices:[],
|
||||
models:[{key:'other-fixture-stream-model',module:'cliproxy',upstream:'other',provider:'other',model:'fixture-stream-model',label:'other / fixture-stream-model',available:true,reason:'unverified',harnesses:['claude-code','pi']}]})});
|
||||
export async function managedLaunchFixture(t,{proxy=true,launches=true,refuseProbe=false,harness='pi',bwrap=null,admit=null,extra=[],real=[],hostEnv={},modelStream=null,modelCatalogue=null}={}){
|
||||
const root=fs.mkdtempSync(join(tmpdir(),'ml653-')),closers=[],history=new Set(['fx653-manage']),proxyReceipts=[],logs=[];
|
||||
let server,bus,launcher,stderr;const trace={preflightAttempts:0,successfulPreflights:0,enableBeforeSpawn:[],bindings:[],stalls:[],ops:[]};
|
||||
const checkHistory=entries=>{const text=JSON.stringify(entries);for(const value of history)if(text.includes(value))throw Error('fixture credential leaked');};
|
||||
t.after(async()=>{const failures=[];for(const close of [...closers].reverse()){try{await close();}catch{failures.push('owned-close');}}try{server?.closeAllConnections();if(server?.listening)await new Promise((resolve,reject)=>server.close(e=>e?reject(e):resolve()));}catch{failures.push('owned-server-close');}try{checkHistory({logs,proxyReceipts,trace});}catch{failures.push('value-free-receipts');}if(stderr)process.stderr.write=stderr;try{fs.rmSync(root,{recursive:true,force:true});}catch{failures.push('owned-root-close');}if(failures.length)throw new AggregateError(failures.map(code=>Error(code)),'owned managed fixture cleanup failed');});
|
||||
const f=fixture(root,'acme',doc=>{doc.plugins={...doc.plugins,harness:[harness,...extra]};for(const role of Object.values(doc.roles))role.vars={...role.vars,harness};doc.roles.chief.vars.model='claude-opus-5-5';if(modelStream){doc.launch.models['claude-opus-5-5']={provider:'anthropic',model:'claude-opus-5-5'};}return doc;});
|
||||
const f=fixture(root,'acme',doc=>{doc.plugins={...doc.plugins,harness:[harness,...extra]};for(const role of Object.values(doc.roles))role.vars={...role.vars,harness};doc.roles.chief.vars.model='claude-opus-5-5';if(harness==='pi')doc.launch.models['claude-opus-5-5']={provider:'zai',model:PI_CHIEF_MODEL};if(modelStream){doc.launch.models['claude-opus-5-5']=harness==='pi'?STREAM_OTHER:{provider:'anthropic',model:'claude-opus-5-5'};if(harness==='pi')doc.plugins={...doc.plugins,'model-provider':['cliproxy']};}return doc;});
|
||||
if(modelStream&&harness==='pi'&&!modelCatalogue)modelCatalogue=streamCatalogue();
|
||||
const env={PATH:process.env.PATH,HOME:process.env.HOME,TMPDIR:process.env.TMPDIR,MOSAIC_CONFIG:f.config,MOSAIC_ROLES_DIR:f.roles,...hostEnv};
|
||||
// The nonproxy positive fixture supplies its own fake pi login. Managed
|
||||
// proxy fixtures deliberately hold no upstream credential at all.
|
||||
@@ -90,6 +98,7 @@ export async function managedLaunchFixture(t,{proxy=true,launches=true,refusePro
|
||||
case 'landRelease':return bus.landRelease(m.record);
|
||||
case 'threadsState':return bus.threadsState(m.business);
|
||||
case 'threadIdle':return bus.threadIdle(m.record);
|
||||
case 'failureNotice':return bus.failureNotice(m.record);
|
||||
case 'lifecycleRequests':return bus.lifecycleRequests(m.business);
|
||||
case 'sessionClears':return bus.sessionClears(m.business);
|
||||
default:throw Error('fixture unrequested trusted operation');
|
||||
|
||||
@@ -11,8 +11,11 @@ async function launched(t,options){const f=await managedLaunchFixture(t,options)
|
||||
test('653 actual managed proxy launch stays idle unarmed then stops first silent turn through trusted runtime',{skip,timeout:30000},async t=>{
|
||||
const {f,s}=await launched(t);assert.equal(f.trace.successfulPreflights,1);assert.deepEqual(f.trace.enableBeforeSpawn,[true]);assert.equal(f.trace.bindings[0].modelKeyId,f.expectedKeyId());assert.equal(s.modelKeyId,f.expectedKeyId());assert.notEqual(s.pid,s.runnerPid);
|
||||
await pause(1250);assert.equal(f.trace.stalls.length,0,'idle beyond configured initial budget does not stop');assert.equal(fs.existsSync(join(s.runDir,'proxy-first-turn')),false);assert.equal(startTimeOf(s.runnerPid),s.runnerStartTime);
|
||||
const before=process.hrtime.bigint();f.send('SILENT');await waitFor(()=>fs.existsSync(join(s.runDir,'proxy-first-turn')),'actual managed adapter turn publishes marker');const stamp=BigInt(fs.readFileSync(join(s.runDir,'proxy-first-turn'),'utf8').trim().split(' ')[1]);assert.ok(stamp>=before&&stamp<=process.hrtime.bigint());assert.equal(fs.existsSync(join(s.runDir,'proxy-first-output')),false);
|
||||
await waitFor(()=>f.trace.stalls.length===1,'actual caller watcher reaches trusted modelStall');assert.deepEqual(f.trace.stalls,[{run:s.run,keyId:s.modelKeyId,stopped:true}]);await waitFor(()=>f.sessions().length===0,'normal managed exit unregisters session');assert.equal(f.launchLog().some(r=>r.action==='proxy-stall'&&r.keyIds?.includes(s.modelKeyId)&&!r.code),true);f.assertHistoryGuard();f.valueFree();
|
||||
const before=process.hrtime.bigint();const sent=f.send('SILENT');await waitFor(()=>fs.existsSync(join(s.runDir,'proxy-first-turn')),'actual managed adapter turn publishes marker');const stamp=BigInt(fs.readFileSync(join(s.runDir,'proxy-first-turn'),'utf8').trim().split(' ')[1]);assert.ok(stamp>=before&&stamp<=process.hrtime.bigint());assert.equal(fs.existsSync(join(s.runDir,'proxy-first-output')),false);
|
||||
await waitFor(()=>f.trace.stalls.length===1,'actual caller watcher reaches trusted modelStall');assert.deepEqual(f.trace.stalls,[{run:s.run,keyId:s.modelKeyId,stopped:true}]);await waitFor(()=>f.sessions().length===0,'normal managed exit unregisters session');assert.equal(f.launchLog().some(r=>r.action==='proxy-stall'&&r.keyIds?.includes(s.modelKeyId)&&!r.code),true);f.assertHistoryGuard();
|
||||
// #1873 chat-fail F2: the stopped turn's message gets one plain line, as a failed turn's.
|
||||
const line=await f.reply(sent);assert.equal(line.body,"The turn failed: the model service gave no answer in time, so the turn was stopped. Try again, or check the account in Settings > Model setup.\nDetails: stopped: proxy-stall");assert.deepEqual([line.from_role,line.from_run,line.class],['chief','bus','RESULT']);
|
||||
f.valueFree();
|
||||
});
|
||||
test('653 actual managed proxy output cancels caller watcher and first timestamp never resets',{skip,timeout:30000},async t=>{
|
||||
const {f,s}=await launched(t);const first=f.send('OUTPUT');const answer=await f.reply(first);assert.equal(answer.body,'fx653-private-output');const file=join(s.runDir,'proxy-first-turn'),stamp=fs.readFileSync(file,'utf8');assert.equal(fs.statSync(file).mode&0o777,0o600);assert.equal(fs.readFileSync(join(s.runDir,'proxy-first-output'),'utf8'),'output\n');
|
||||
|
||||
@@ -90,6 +90,8 @@ async function routed(t, harness) {
|
||||
const makeLoader = await fakeHarnesses(t, { pi: null, 'claude-code': null }, write => {
|
||||
// fsthrows: its preflight reads a missing file, so the error's message quotes the path.
|
||||
write('harness', 'fsthrows', `export * from ${PI};\nimport { readFileSync } from "node:fs";\nexport function preflight() { readFileSync(${JSON.stringify(absent)}); }\n`);
|
||||
// fsrejects (#1873 re-check 8): an async preflight, as pi's is, whose promise rejects with that error.
|
||||
write('harness', 'fsrejects', `export * from ${PI};\nimport { readFileSync } from "node:fs";\nexport async function preflight() { readFileSync(${JSON.stringify(absent)}); }\n`);
|
||||
// plainthrows: a plain Error whose message quotes a host path, as a module's own text.
|
||||
write('harness', 'plainthrows', `export * from ${PI};\nexport function preflight() { throw new Error(${JSON.stringify(`cannot read ${absent}`)}); }\n`);
|
||||
// pathfails: a preflight that returns a failure whose message quotes a host path.
|
||||
@@ -146,6 +148,15 @@ test('a harness whose preflight throws a system error refuses harness-failed nam
|
||||
clean(JSON.stringify(s.last()), s.root);
|
||||
});
|
||||
|
||||
// #1873 re-check 8 P3-1: a preflight promise that rejects is refused as a throw is, by the call and
|
||||
// the system code, never the raw error or its path.
|
||||
test('a harness whose async preflight rejects with a system error refuses harness-failed naming the call and code, never the path', async t => {
|
||||
const s = await routed(t, 'fsrejects');
|
||||
assert.deepEqual(await s.launch(), { ok: false, error: 'harness-failed' });
|
||||
assert.equal(s.last().why, 'harness fsrejects preflight: cannot open a host file (ENOENT)');
|
||||
clean(JSON.stringify(s.last()), s.root);
|
||||
});
|
||||
|
||||
test('a harness the loader refuses names its code only, never the loader message or a path', async t => {
|
||||
const s = await routed(t, 'absent');
|
||||
assert.deepEqual(await s.launch(), { ok: false, error: 'harness-refused' });
|
||||
|
||||
@@ -34,10 +34,11 @@ function privateArtifacts(f, session, keys) {
|
||||
f.valueFree();
|
||||
}
|
||||
|
||||
// #1873 harness-matrix: an OAuth Claude model runs on claude-code only, so pi joins the z.ai and
|
||||
// codex routes on a non-Claude model, and a Claude route on pi refuses below.
|
||||
const ROUTE_MODEL = 'fixture-route-model';
|
||||
for (const [provider, route, generated, api] of [
|
||||
['zai', null, 'mosaic-proxy-glm', 'openai-completions'],
|
||||
['anthropic', null, 'anthropic', 'anthropic-messages'],
|
||||
['proxy', 'anthropic', 'anthropic', 'anthropic-messages'],
|
||||
['proxy', 'glm', 'mosaic-proxy-glm', 'openai-completions'],
|
||||
['proxy', 'codex', 'mosaic-proxy-codex', 'openai-responses'],
|
||||
]) {
|
||||
@@ -45,8 +46,7 @@ for (const [provider, route, generated, api] of [
|
||||
const f = await managedLaunchFixture(t);
|
||||
assert.deepEqual(f.upstreamCredentialNames, [], 'managed proxy fixture has no inherited fake upstream credential');
|
||||
editBusiness(f, doc => {
|
||||
doc.launch.models['claude-opus-5-5'].provider = provider;
|
||||
if (route) doc.launch.models['claude-opus-5-5'].route = route;
|
||||
doc.launch.models['claude-opus-5-5'] = { provider, model: ROUTE_MODEL, ...(route ? { route } : {}) };
|
||||
});
|
||||
const answer = await f.launcher.launchPm();
|
||||
const session = f.sessions().find(value => value.run === answer.run);
|
||||
@@ -69,7 +69,7 @@ for (const [provider, route, generated, api] of [
|
||||
assert.equal(models.providers[generated].api, api);
|
||||
assert.equal(models.providers[generated].baseUrl,
|
||||
other.baseUrl + (api === 'anthropic-messages' ? '' : '/v1'));
|
||||
assert.equal(models.providers[generated].models.some(model => model.id === 'claude-opus-5-5'), true);
|
||||
assert.equal(models.providers[generated].models.some(model => model.id === ROUTE_MODEL), true);
|
||||
for (const value of Object.values(models.providers)) assert.equal(value.apiKey, '$ANTHROPIC_API_KEY');
|
||||
for (const key of [own.key, other.key]) assert.equal(text.includes(key), false, 'models contain no key value');
|
||||
assert.equal(fs.statSync(path).mode & 0o777, 0o600);
|
||||
@@ -79,6 +79,26 @@ for (const [provider, route, generated, api] of [
|
||||
});
|
||||
}
|
||||
|
||||
// A legacy entry with no provider plugin gets no catalogue check, so a Claude-named model on a z.ai
|
||||
// or codex route is treated as OAuth too (review P1-1).
|
||||
for (const [provider, route, name] of [['anthropic', null], ['proxy', 'anthropic'], ['zai', null, 'claude-opus-5-5'], ['proxy', 'glm', 'claude-opus-5-5'], ['proxy', 'codex', 'claude-sonnet-5-5']]) {
|
||||
test(`#1873 a stored Claude route ${provider}${route ? ':' + route : ''}${name ? ' ' + name : ''} on pi refuses model-harness-incompatible with no key acquisition or session`, { skip, timeout: 30000 }, async t => {
|
||||
const f = await managedLaunchFixture(t);
|
||||
editBusiness(f, doc => {
|
||||
doc.launch.models['claude-opus-5-5'].provider = provider;
|
||||
if (route) doc.launch.models['claude-opus-5-5'].route = route;
|
||||
if (name) doc.launch.models['claude-opus-5-5'].model = name;
|
||||
});
|
||||
await assert.rejects(f.launcher.launchPm(), error => error.code === 'model-harness-incompatible');
|
||||
assert.equal(f.trace.preflightAttempts, 0, 'refused before P2 issues a key');
|
||||
assert.equal(f.trace.bindings.length, 0);
|
||||
assert.equal(f.sessions().length, 0);
|
||||
assert.equal(f.runDirectories().length, 0);
|
||||
assert.equal(f.launchLog().some(e => e.event === 'refused' && e.code === 'model-harness-incompatible'), true);
|
||||
f.valueFree();
|
||||
});
|
||||
}
|
||||
|
||||
test('P3 joined Claude harness receives the admitted P2 key only through transient auth env', { skip, timeout: 30000 }, async t => {
|
||||
const f = await managedLaunchFixture(t, { harness: 'claude-code' });
|
||||
assert.deepEqual(f.upstreamCredentialNames, [], 'Claude proxy fixture holds no upstream credential');
|
||||
@@ -117,7 +137,7 @@ test('P3 joined unsupported provider refuses with no key acquisition, fallback,
|
||||
test('row 332 R2 joined pi launch goes live when launch.models also holds an unroutable entry, which models.json leaves out', { skip, timeout: 30000 }, async t => {
|
||||
const f = await managedLaunchFixture(t);
|
||||
editBusiness(f, doc => {
|
||||
doc.launch.models['claude-opus-5-5'].provider = 'anthropic';
|
||||
doc.launch.models['claude-opus-5-5'] = { provider: 'zai', model: ROUTE_MODEL };
|
||||
doc.launch.models.router = { provider: 'openrouter', model: 'x' };
|
||||
});
|
||||
const answer = await f.launcher.launchPm();
|
||||
@@ -126,7 +146,7 @@ test('row 332 R2 joined pi launch goes live when launch.models also holds an unr
|
||||
assert.equal(f.trace.successfulPreflights, 1);
|
||||
assert.equal(f.runDirectories().length, 1, 'one run directory, the live one');
|
||||
const { providers } = JSON.parse(fs.readFileSync(join(session.runDir, 'home', '.pi', 'agent', 'models.json'), 'utf8'));
|
||||
assert.equal(providers.anthropic.models.some(model => model.id === 'claude-opus-5-5'), true);
|
||||
assert.equal(providers['mosaic-proxy-glm'].models.some(model => model.id === ROUTE_MODEL), true);
|
||||
assert.equal(Object.values(providers).some(value => value.models.some(model => model.id === 'x')), false, 'no openrouter entry');
|
||||
privateArtifacts(f, session, [f.bus.credentials.modelRecord('acme/chief').key]);
|
||||
});
|
||||
@@ -95,6 +95,7 @@ function requestHost(x) {
|
||||
landHeld: m => x.bus.landHeld(m.record), landHolders: m => x.bus.landHolders(m.business),
|
||||
sessionClears: m => x.bus.sessionClears(m.business),
|
||||
threadsState: m => x.bus.threadsState(m.business), lifecycleRequests: m => x.bus.lifecycleRequests(m.business),
|
||||
failureNotice: m => x.bus.failureNotice(m.record),
|
||||
'request.admission': m => x.bus.admission(m), 'request.launches': m => x.bus.requestLaunches(m.business),
|
||||
'request.launchResult': m => x.bus.requestLaunchResult(m.record), 'request.manifest': m => x.bus.requestManifest(m.record),
|
||||
'request.release': m => x.bus.requestRelease(m.cap, m.record), 'request.chief': m => x.bus.requestChief(m.business),
|
||||
|
||||
@@ -96,6 +96,7 @@ async function start(t, { f, adapter, access }, threadsState) {
|
||||
credentialStatus: (m) => runtime.credentialStatus(m.business, m.role),
|
||||
threadsState: (m) => threadsState(runtime, m),
|
||||
threadIdle: (m) => runtime.threadIdle(m.record),
|
||||
failureNotice: (m) => runtime.failureNotice(m.record),
|
||||
mayLaunch: (m) => runtime.mayLaunch(m),
|
||||
landHeld: (m) => runtime.landHeld(m.record),
|
||||
landHolders: (m) => runtime.landHolders(m.business),
|
||||
|
||||
@@ -92,6 +92,7 @@ async function start(t, { f, adapter }, { tickMs = 100, stopTimeoutMs } = {}) {
|
||||
credentialStatus: (m) => runtime.credentialStatus(m.business, m.role),
|
||||
threadsState: (m) => runtime.threadsState(m.business),
|
||||
threadIdle: (m) => runtime.threadIdle(m.record),
|
||||
failureNotice: (m) => runtime.failureNotice(m.record),
|
||||
mayLaunch: (m) => runtime.mayLaunch(m),
|
||||
landHeld: (m) => runtime.landHeld(m.record),
|
||||
landHolders: (m) => runtime.landHolders(m.business),
|
||||
|
||||
@@ -112,6 +112,7 @@ async function start(t, { f, adapter }, { tickMs = 100, ops: replace } = {}) {
|
||||
credentialStatus: (m) => runtime.credentialStatus(m.business, m.role),
|
||||
threadsState: (m) => runtime.threadsState(m.business),
|
||||
threadIdle: (m) => runtime.threadIdle(m.record),
|
||||
failureNotice: (m) => runtime.failureNotice(m.record),
|
||||
mayLaunch: (m) => runtime.mayLaunch(m),
|
||||
landHeld: (m) => runtime.landHeld(m.record),
|
||||
landHolders: (m) => runtime.landHolders(m.business),
|
||||
|
||||
@@ -93,6 +93,7 @@ async function start(t, { f, adapter }, { tickMs = 100, ops: replace } = {}) {
|
||||
credentialStatus: (m) => runtime.credentialStatus(m.business, m.role),
|
||||
threadsState: (m) => runtime.threadsState(m.business),
|
||||
threadIdle: (m) => runtime.threadIdle(m.record),
|
||||
failureNotice: (m) => runtime.failureNotice(m.record),
|
||||
mayLaunch: (m) => runtime.mayLaunch(m),
|
||||
landHeld: (m) => runtime.landHeld(m.record),
|
||||
landHolders: (m) => runtime.landHolders(m.business),
|
||||
@@ -496,6 +497,78 @@ test("REQ-LIMIT-3 REQ-LIMIT-4 a thread start whose instance is at its token limi
|
||||
assert.equal(launchLog(f).filter((e) => e.event === "refused" && e.thread === A).length, 1);
|
||||
});
|
||||
|
||||
// #1873 chat-fail F2: a refused start leaves one plain line in the chat for each message it left
|
||||
// unanswered, with the cause and next step, never one per retry and never the refusal's reason.
|
||||
const LIMIT_LINE = "The turn failed: the agent reached a usage limit. Wait for it to reset, or check the limits in Settings.\nDetails: start refused: limit-reached";
|
||||
test("#1873 F2 a refused thread start puts one plain line per unanswered message in the chat, not one per retry", { skip, timeout: 60000 }, async (t) => {
|
||||
const prepared = prepare(t, { edit: atLimit });
|
||||
const { f } = prepared;
|
||||
const host = await start(t, prepared);
|
||||
const project = host.call("project.create", { name: "stack work", repo: "stack" }).id;
|
||||
const A = host.call("thread.create", { project, name: "alpha" }).id;
|
||||
const first = host.call("thread.send", { thread: A, body: "for A" });
|
||||
const lines = (sent) => host.call("thread.messages", { thread: A }).filter((m) => m.in_reply_to === sent.id);
|
||||
const [line] = await until(() => lines(first).length && lines(first));
|
||||
assert.equal(line.body, LIMIT_LINE);
|
||||
assert.deepEqual([line.from_role, line.from_run], ["assistant", "bus"]);
|
||||
assert.equal(line.to_role, "human");
|
||||
assert.equal(line.class, "RESULT");
|
||||
assert.doesNotMatch(line.body, /token-limit|tokens\.instances|"limit"/, "the refusal's reason stays out of the chat");
|
||||
for (let i = 0; i < 3; i++) await host.launcher.tick();
|
||||
assert.equal(lines(first).length, 1, "a retry adds no line");
|
||||
// A second message gets its own line; the first keeps its one.
|
||||
const second = host.call("thread.send", { thread: A, body: "again" });
|
||||
await until(() => lines(second).length);
|
||||
for (let i = 0; i < 3; i++) await host.launcher.tick();
|
||||
assert.deepEqual(lines(second).map((m) => m.body), [LIMIT_LINE]);
|
||||
assert.equal(lines(first).length, 1);
|
||||
assert.deepEqual(threadRuns(f, A), []);
|
||||
// The messages stay pending: the start is retried and answers them once the limit allows.
|
||||
assert.equal(launchLog(f).filter((e) => e.event === "refused" && e.thread === A).length, 1);
|
||||
});
|
||||
|
||||
// #1873 review round 1 (P2-2): launch-failed can be a lost race that the next try wins, so one
|
||||
// such refusal posts nothing and the answer arrives alone; the same refusal twice posts one line.
|
||||
const failedLines = (host, thread) => host.call("thread.messages", { thread }).filter((m) => m.body.startsWith("The turn failed"));
|
||||
test("#1873 F2 one launch-failed refusal that the next start clears leaves no failure line", { skip, timeout: 60000 }, async (t) => {
|
||||
const prepared = prepare(t);
|
||||
let throws = 1;
|
||||
const host = await start(t, prepared, { ops: (runtime) => ({ landHeld: (m) => { if (throws-- > 0) throw new Error("fixture: lost race"); return runtime.landHeld(m.record); } }) });
|
||||
const project = host.call("project.create", { name: "stack work", repo: "stack" }).id;
|
||||
const A = host.call("thread.create", { project, name: "alpha" }).id;
|
||||
const sent = host.call("thread.send", { thread: A, body: "for A" });
|
||||
const reply = await until(() => host.call("thread.messages", { thread: A }).find((m) => m.in_reply_to === sent.id && m.class === "RESULT"));
|
||||
assert.match(reply.body, /THREAD \{/, "the first reply is the answer");
|
||||
assert.equal(throws, -1, "the first start was refused");
|
||||
assert.deepEqual(failedLines(host, A), []);
|
||||
});
|
||||
test("#1873 F2 the same launch-failed refusal twice for one message posts one line", { skip, timeout: 60000 }, async (t) => {
|
||||
const prepared = prepare(t);
|
||||
const host = await start(t, prepared, { ops: () => ({ landHeld: () => { throw new Error("fixture: always refused"); } }) });
|
||||
const project = host.call("project.create", { name: "stack work", repo: "stack" }).id;
|
||||
const A = host.call("thread.create", { project, name: "alpha" }).id;
|
||||
host.call("thread.send", { thread: A, body: "for A" });
|
||||
const [line] = await until(() => failedLines(host, A).length && failedLines(host, A));
|
||||
assert.match(line.body, /\nDetails: start refused: launch-failed$/);
|
||||
for (let i = 0; i < 3; i++) await host.launcher.tick();
|
||||
assert.equal(failedLines(host, A).length, 1);
|
||||
});
|
||||
// #1873 review round 1 (P3-3): a line whose post fails is posted on a later tick, once.
|
||||
test("#1873 F2 a failure line whose post is refused once arrives on a later tick, exactly once", { skip, timeout: 60000 }, async (t) => {
|
||||
const prepared = prepare(t, { edit: atLimit });
|
||||
let throws = 1;
|
||||
const host = await start(t, prepared, { ops: (runtime) => ({ failureNotice: (m) => { if (throws-- > 0) throw new Error("fixture: bus busy"); return runtime.failureNotice(m.record); } }) });
|
||||
const project = host.call("project.create", { name: "stack work", repo: "stack" }).id;
|
||||
const A = host.call("thread.create", { project, name: "alpha" }).id;
|
||||
host.call("thread.send", { thread: A, body: "for A" });
|
||||
const [line] = await until(() => failedLines(host, A).length && failedLines(host, A));
|
||||
assert.equal(line.body, LIMIT_LINE);
|
||||
assert.equal(throws, -1, "the first post was refused");
|
||||
for (let i = 0; i < 3; i++) await host.launcher.tick();
|
||||
assert.equal(failedLines(host, A).length, 1);
|
||||
assert.equal(host.logs.filter((l) => /failure line refused/.test(l)).length, 1);
|
||||
});
|
||||
|
||||
test("REQ-LIMIT-4 a thread run at its token limit stops at the end of its turn: the reply lands, and the end record names the limit", { skip, timeout: 60000 }, async (t) => {
|
||||
const prepared = prepare(t, { edit: atLimit });
|
||||
const { f } = prepared;
|
||||
|
||||
@@ -97,6 +97,7 @@ async function start(t, { f, adapter, access }) {
|
||||
credentialStatus: (m) => runtime.credentialStatus(m.business, m.role),
|
||||
threadsState: (m) => runtime.threadsState(m.business),
|
||||
threadIdle: (m) => runtime.threadIdle(m.record),
|
||||
failureNotice: (m) => runtime.failureNotice(m.record),
|
||||
mayLaunch: (m) => runtime.mayLaunch(m),
|
||||
landHeld: (m) => runtime.landHeld(m.record),
|
||||
landHolders: (m) => runtime.landHolders(m.business),
|
||||
|
||||
@@ -12,7 +12,7 @@ import { join } from "node:path";
|
||||
import { createHash, randomBytes } from "node:crypto";
|
||||
import { run } from "../src/cli.mjs";
|
||||
import { AGENT_MARKERS } from "../src/transport.mjs";
|
||||
import { DEFAULT_ROTATE_GRACE_MINUTES, keyPaths, retireAtFor, rotateByFor } from "../src/triage-key.mjs";
|
||||
import { DEFAULT_ROTATE_GRACE_MINUTES, keyPaths, retireAtFor, rotateByFor } from "../../bus/src/triage-key.mjs";
|
||||
import { ModelKeys } from "../../bus/src/model-keys.mjs";
|
||||
import { ManagementClient } from "../../model-proxy/src/client.mjs";
|
||||
import { startBroker } from "../../bus/src/runtime.mjs";
|
||||
|
||||
@@ -18,13 +18,103 @@ Every harness layer below is a row S0 line
|
||||
(`docs/plans/2026-10-04_slice-1.md`, "What S6 can rely on"), and each
|
||||
bundle's `manifest.json` names the lines it relies on (`reliesOn`):
|
||||
|
||||
| | Pi | Claude Code |
|
||||
|---|---|---|
|
||||
| 1. block a tool | `tool_call` handler returns a block | PreToolUse command hook exits 2; `--bare` is never passed |
|
||||
| 2. gate crash | a throw in the handler blocks | the hook runs as `<gate> \|\| exit 2` |
|
||||
| 3. gate missing | a missing `-e` file refuses to start | the wrapper blocks |
|
||||
| 4. gate hang | the runner's wall clock plus the `agent_end` marker | `timeout -k 2 10 <gate> \|\| exit 2`, hook timeout 20 |
|
||||
| 5. bash | limited only by the tool limit | limited only by the tool limit |
|
||||
| | Pi | Claude Code | How pi's line is checked |
|
||||
|---|---|---|---|
|
||||
| 1. block a tool | `tool_call` handler returns a block | PreToolUse command hook exits 2; `--bare` is never passed | suites, on the repository's pi (`pi-session.test.mjs`) |
|
||||
| 2. gate crash | a throw in the handler blocks | the hook runs as `<gate> \|\| exit 2` | suites, on the repository's pi |
|
||||
| 3. gate missing | a missing `-e` file refuses to start | the wrapper blocks | at every start, by the fit check, on the pi that runs; and suites |
|
||||
| 4. gate hang | the runner's wall clock plus the `agent_end` marker | `timeout -k 2 10 <gate> \|\| exit 2`, hook timeout 20 | suites, on the repository's pi |
|
||||
| 5. bash | limited only by the tool limit | limited only by the tool limit | suites, on the repository's pi |
|
||||
|
||||
Pi is not pinned (#1873 pi-unpin). Before a session starts, `preflight`
|
||||
awaits the fit check in `src/pi-fit.mjs` on the binary the module found
|
||||
(the Triage host and route await the module's `ready` instead), and
|
||||
`env` reads its pass. It runs that binary in a sandbox with the network
|
||||
unshared and calls no model. Each run is a child process the check
|
||||
awaits, up to four at a time, so the bus's event loop keeps running
|
||||
while it does; callers that ask while a check runs share it. Each run
|
||||
gets the environment a bus session's pi gets, built by the same code
|
||||
(the launcher's `ENV_ALLOW` and `sessionEnv`, the sandbox's
|
||||
`sessionSetenv`, the runner's `adapterEnv` and the pi module's
|
||||
`harnessEnv`) from the bus process's own environment, less the provider
|
||||
keys, and starts the binary through `/bin/sh` as the adapter does. So
|
||||
`HOME` is a scratch home, `TMPDIR` is `/tmp` bound to scratch, a run has
|
||||
`TERM`, `TZ` or `LC_ALL` only when the bus has it (a bus started by its
|
||||
unit has none), and no run sets `PI_CODING_AGENT_DIR`, which no session
|
||||
has: pi's agent directory is `$HOME/.pi/agent`, where the user sources
|
||||
are planted. A test fails when the fit runs' names, or the values the
|
||||
code sets, differ from a bus session's, apart from the provider
|
||||
variables, for a login shell's host and a unit's. It needs
|
||||
`--help` to list, in its option column, every flag the adapter passes
|
||||
(with rpc mode and each thinking level), and a missing `-e` file to stop
|
||||
the start in rpc and in `-p`. The planted ambient sources must stay
|
||||
unloaded with the seal flags on (skills and prompt templates are read
|
||||
from `get_commands` in rpc and from the `-e` extension's command list in
|
||||
`-p`): extensions, MCP
|
||||
servers, skills and prompt templates (found in their directories, listed
|
||||
in a `settings.json`, or in a local package a `settings.json` lists),
|
||||
context files (`AGENTS.md`, `CLAUDE.md`, `SYSTEM.md`) and a trusted
|
||||
project's `.pi/SYSTEM.md` and `.pi/APPEND_SYSTEM.md`. The project has a
|
||||
saved trust, so only `--no-approve` keeps those two out. An `-e`
|
||||
extension writes the system prompt, command names and session id it sees
|
||||
at `session_start`; the sealed runs must show the `--system-prompt` file
|
||||
and none of the planted sources. A control run passes the seal flags less every loader flag
|
||||
(`--no-approve`, `--no-extensions`, `--no-skills`,
|
||||
`--no-prompt-templates`, `--no-context-files`, and `--no-mcp` when
|
||||
listed), so it keeps `--offline`, `--no-themes`, `--no-tools` and
|
||||
`--no-session`, and the same `-e` and `--system-prompt`. It must show
|
||||
each kind of source (an MCP server only when `--help` lists `--no-mcp`)
|
||||
and answer `get_state` and `get_commands`, and its `-e` extension must
|
||||
list the planted skill and prompt template, or the check can't tell and
|
||||
refuses. For every kind but the trusted project files, only a source
|
||||
outside the project counts there, since a project source can wait on
|
||||
trust and `--no-approve` would then hide a seal flag the binary ignores.
|
||||
Then one sealed rpc run per loader flag leaves out only that flag: its
|
||||
kind must show, and no other kind (an MCP server may show beside an
|
||||
extension when `--help` lists no `--no-mcp`, since pi then ties MCP to
|
||||
`--no-extensions`). So each loader flag is shown to keep its own kind
|
||||
out, and no other seal flag, nor their order, can stand in for it. Five
|
||||
sealed `-p` runs have a bus session's shape: the arguments the adapter
|
||||
passes for a session, in its order, from `sessionShape` in
|
||||
`src/pi-fit.mjs`, with two `-e` and `--session-dir`. The adapter's four
|
||||
session branches (a tool list or `--no-tools`, `-c` over a stored
|
||||
session or none, `--thinking` or none, `--mode json` or none) are spread
|
||||
over the five runs so every pair of them is passed together. A `-c` run
|
||||
gets a stored session and must resume it. Each must load both `-e`
|
||||
files and show no planted source. A test drives the adapter, in a bus
|
||||
session's environment, through every branch set with no tools, one or
|
||||
two, `-c` over a pi-named session file or an unrelated file, each
|
||||
thinking level and each provider a session can name. It runs over a
|
||||
login shell's host and a unit's, and fails on an argv `sessionShape`
|
||||
doesn't build, a pair no run passes, or a variable the adapter reads
|
||||
that no session sets. So a branch on `TZ` or `LC_ALL` being unset, or on
|
||||
`TERM` missing from the environment, turns it red. (Bash run as
|
||||
`/bin/sh` sets an unset `TERM` to `dumb` for the shell without exporting
|
||||
it, so `${TERM:-}` in the adapter is never empty, in a session or here.)
|
||||
It can't see a branch keyed on a
|
||||
value it doesn't try (a model, a path, another tool or provider). A last sealed `-p` run uses `--no-session`, as
|
||||
worker runs do. Each run gets a fresh scratch directory, so no run sees
|
||||
what another left. Not run: `--fork`, `--skill` and the interactive TUI
|
||||
(worker containers and `scripts/agent.sh` only), the branches three or
|
||||
four at a time beyond the five runs, `-c` over a session directory that
|
||||
holds nothing for the project (one more run costs about 340 ms cold),
|
||||
and a session's own values (tools beyond `read` and `bash`, thinking
|
||||
levels beyond `low`, provider, model, key, `-e` files, request). The
|
||||
runs are offline with no key, so nothing past the start is tried. A pi
|
||||
built to act one way in the fit runs and another way with those is
|
||||
outside what a start-time check can catch. A run killed for taking
|
||||
over 20 s or printing over 4 MB can't be checked and says which. A
|
||||
`--version` that fails or hangs past 3 s only leaves no label. Pi
|
||||
0.85.1 appends the agent directory's `APPEND_SYSTEM.md` whatever the
|
||||
flags, so the check doesn't plant one. Any failure refuses the launch with
|
||||
`harness-feature-missing` and one line that names the binary by its label,
|
||||
for example `pi 2.0.0 (MOSAIC_PI_BIN) lacks --no-context-files`. When
|
||||
`--help` lists `--no-mcp`, the adapter passes it too. A pass is cached in
|
||||
memory by a digest of the package's files (and npm's lock files, for
|
||||
the repository's copy) and the names in the runs' environment, so a bus
|
||||
restarted under other names checks again. Lines 1, 2, 4 and 5 are not
|
||||
checked at start; a pi other than the repository's copy relies on them
|
||||
unproved until a scripted-model fit run exists (design section 5, S6).
|
||||
|
||||
Claude Code also runs with `--restricted`: no user, project or local
|
||||
settings, file tools confined to the working directory, and no `CLAUDE.md`
|
||||
|
||||
@@ -100,7 +100,8 @@ export function sessionModel(resolved, models = null, which = "primary", { proxy
|
||||
if (models === null) return { harness, provider: null, model, thinking };
|
||||
const entry = Object.hasOwn(models, model) ? models[model] : null;
|
||||
if (!entry) throw new BundleError(`instance ${resolved.instance}: unknown model table selection for ${ROUTE_VARS[which][1]} var`);
|
||||
const transport = entry.provider === "proxy" || proxy ? checkedTransport(entry, null, resolver) : null;
|
||||
// #1873 harness-matrix: the route's own harness, so a model the module holds off it refuses here.
|
||||
const transport = entry.provider === "proxy" || proxy ? checkedTransport(entry, harness, resolver) : null;
|
||||
return { harness, provider: entry.provider, model: entry.model, modelKey: model, thinking,
|
||||
...(transport ? { proxyRoute: transport.routeId, proxyTransport: transport } : {}) };
|
||||
|
||||
@@ -432,9 +433,13 @@ export function buildBundle({ dir, resolved, contract, arbiter = false, tracker
|
||||
];
|
||||
// The module's own files. A name the bundle already wrote refuses at write (flag wx).
|
||||
const transports = {};
|
||||
// #1873 harness-matrix: only models the provider's route() lets this session's harness run, so a
|
||||
// pi session's models.json never names an upstream claude model or a Claude-named legacy entry.
|
||||
// route() has no account data: a module entry the catalogue holds to claude-code for an OAuth name
|
||||
// can still be listed. Only the session's own model is admitted against the catalogue.
|
||||
if (proxy) for (const [key, entry] of Object.entries(models ?? {})) {
|
||||
try { transports[key] = checkedTransport(entry, null, resolver); }
|
||||
catch (e) { if (e?.code !== 'proxy-provider-unsupported') throw e; }
|
||||
try { transports[key] = checkedTransport(entry, model.harness, resolver); }
|
||||
catch (e) { if (!['proxy-provider-unsupported', 'model-harness-incompatible'].includes(e?.code)) throw e; }
|
||||
}
|
||||
const own = harness.bundle({ dir, files: { ...files }, session: { ...session, ...model }, write, quote, models, proxy, transports });
|
||||
Object.assign(files, own.files);
|
||||
|
||||
@@ -1,2 +1,155 @@
|
||||
// Shared by the sender and recipient's reply-cycle check.
|
||||
export const LENGTH_FAILURE_NOTICE_PREFIX = 'Mosaic turn failure: ';
|
||||
|
||||
// #1873 chat-fail F1: the plain cause of a failed turn. It lives here, not in a file of its own,
|
||||
// because the session sandbox mounts only RUNNER_FILES (packages/seat/src/sandbox.mjs) and this
|
||||
// file is already one of them.
|
||||
//
|
||||
// The adapter contract (adapters/README.md) puts the answer on stdout and makes a
|
||||
// nonzero exit a failure. A harness that ends on an error result may still print that
|
||||
// result on stdout before it exits nonzero. That text is the only cause the runner
|
||||
// has, so the chat line carries it, made safe first:
|
||||
//
|
||||
// 0. %XX escapes and HTML character references are decoded and the text is put in NFKC
|
||||
// form, so an encoded or full-width email, path or secret is seen as itself.
|
||||
// 1. ANSI escapes go, every control, format, separator, surrogate and private-use
|
||||
// character goes, and whitespace collapses to single spaces.
|
||||
// 2. If a secret the turn's environment holds (a variable named like a key, token,
|
||||
// secret, password or credential) appears in the text, even split by whitespace or
|
||||
// controls, no text is shown at all.
|
||||
// 3. Links, email addresses, absolute and Windows paths, "Bearer x" values, the value after
|
||||
// a key word ("key: x", "\"api_key\":\"x\"", "token x"), known key prefixes and long
|
||||
// letter-and-digit runs become [redacted].
|
||||
// 4. The rest is cut to CAUSE_MAX characters.
|
||||
//
|
||||
// Only the last READ_MAX characters are classified and shown. A harness prints its error
|
||||
// result last, and an answer streamed before the failure may itself talk about limits or
|
||||
// signing in. Redaction runs over the whole text first, so the cut never parts a key word
|
||||
// from its value.
|
||||
//
|
||||
// Three causes are recognised from the text and shown as a fixed plain line with the
|
||||
// next step: the account is out of usage, its sign-in expired or was refused, or it
|
||||
// doesn't offer the model. Anything else shows the cleaned text. The fixed lines never
|
||||
// carry any of the error's own words.
|
||||
|
||||
export const CAUSE_MAX = 300;
|
||||
const READ_MAX = 2000;
|
||||
export const PLAIN = Object.freeze({
|
||||
usage: "the model account is out of usage. Check Settings > Model setup.",
|
||||
signin: "the model account's sign-in expired or was refused. Sign in again in Settings > Model setup.",
|
||||
model: "the model this agent uses isn't offered by its account. Pick another model in Settings > Model setup.",
|
||||
});
|
||||
export const WITHHELD = "the error text held a secret, so it isn't shown. Check Settings > Model setup.";
|
||||
|
||||
const ANSI = /\u001b(?:\[[0-?]*[ -/]*[@-~]|\][^\u0007\u001b]*(?:\u0007|\u001b\\)?|[@-Z\\-_])/g;
|
||||
// Cf (format: zero-width, bidi overrides) is removed so a split token rejoins and is seen.
|
||||
const INVISIBLE = /[\p{Cf}\p{Cs}\p{Co}]/gu;
|
||||
// Cc (C0, DEL, C1), line and paragraph separators become spaces.
|
||||
const BREAKS = /[\p{Cc}\p{Zl}\p{Zp}]/gu;
|
||||
|
||||
const USAGE = /usage limit|hit your [\w -]{0,24}limit|(?<!context |output |token |length )limit reached|credit balance is too low|insufficient[_ ]quota|quota (?:exceeded|exhausted)|exceeded your current quota|out of (?:usage|credits)/i;
|
||||
const SIGNIN = /(?:oauth |access |auth )?token (?:has )?expired|invalid api key|invalid x-api-key|invalid bearer token|authentication_error|authentication failed|run \/login|not logged in|unauthori[sz]ed|(?:error|status)[: ]+401\b|sign[- ]?in (?:again|required|expired)|log ?in (?:again|required|expired)|invalid_grant/i;
|
||||
const MODEL = /issue with the selected model|model_not_found|\bmodel\b.{0,80}\b(?:not found|does not exist|not available|isn't available|not supported|may not exist|not offered|no access)|not_found_error.{0,80}\bmodel\b|unknown model|no such model/i;
|
||||
|
||||
const ENTITY = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'", commat: "@", lowbar: "_", period: ".", colon: ":", sol: "/", bsol: "\\", num: "#", equals: "=", hyphen: "-", dash: "-" };
|
||||
function codePoint(n) {
|
||||
return Number.isInteger(n) && n > 0 && n <= 0x10ffff && (n < 0xd800 || n > 0xdfff) ? String.fromCodePoint(n) : " ";
|
||||
}
|
||||
// %XX escapes (twice encoded included) and HTML character references are decoded, then NFKC
|
||||
// folds full-width and compatibility forms to their plain letters.
|
||||
export function decodeText(text) {
|
||||
let out = String(text);
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const next = out
|
||||
.replace(/(?:%[0-9a-f]{2})+/gi, run => { try { return decodeURIComponent(run); } catch { return run.replace(/%([0-9a-f]{2})/gi, (_, h) => String.fromCharCode(parseInt(h, 16))); } })
|
||||
.replace(/&#(\d{1,7});?|&#x([0-9a-f]{1,6});?|&([a-z]{2,7});/gi, (all, dec, hex, name) =>
|
||||
dec ? codePoint(Number(dec)) : hex ? codePoint(parseInt(hex, 16)) : ENTITY[name.toLowerCase()] ?? all);
|
||||
if (next === out) break;
|
||||
out = next;
|
||||
}
|
||||
return out.normalize("NFKC");
|
||||
}
|
||||
// The text with every space, control, format, mark, surrogate and private-use character gone,
|
||||
// for the env-secret check.
|
||||
function compactText(text) {
|
||||
return decodeText(text).replace(ANSI, "").replace(/[\p{Cc}\p{Cf}\p{Cs}\p{Co}\p{M}\p{Z}\s]/gu, "");
|
||||
}
|
||||
|
||||
const SECRET_NAME = /KEY|TOKEN|SECRET|PASS|CRED|AUTH|COOKIE/i;
|
||||
// Values from the turn's environment that must never reach a chat line.
|
||||
export function secretValues(env) {
|
||||
const out = [];
|
||||
for (const [name, value] of Object.entries(env ?? {})) {
|
||||
if (typeof value !== "string" || !SECRET_NAME.test(name)) continue;
|
||||
const compact = compactText(value);
|
||||
if (compact.length >= 8) out.push(compact);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
// A key word, with any prefix joined by _ or - (access_token, client_secret, x-api-key).
|
||||
const KEY_WORD = "(?:\\w*[_-])?(?:api[_-]?key|key|token|secret|password|passwd|authorization|cookie|session)";
|
||||
// Redaction runs over the whole text, up to the runner's 128000 characters, so a pattern that
|
||||
// could start anywhere inside a long run without spaces starts only where the run starts: it
|
||||
// matches the same text in linear time, not quadratic.
|
||||
const REDACT = [
|
||||
/(?<![a-z0-9+.-])[a-z0-9+.-]+:\/\/\S*/gi, // any link, its userinfo and query included, from the run's start
|
||||
/\bwww\.\S+/gi,
|
||||
/(?<!\S)(?=\S*\S@\S)\S+/g, // email addresses and user@host: the whole run holding the @
|
||||
/(?:^|(?<=[\s("'=:]))(?:~|\.{1,2})?\/\S+/g, // absolute and home paths
|
||||
/\b[A-Za-z]:\\\S*/g, // Windows paths
|
||||
/\\\\\S+/g, // UNC paths
|
||||
/\b(?:bearer|basic)\s+\S+/gi,
|
||||
// The value after a key word: "key: x", "key=x" and the JSON form "\"api_key\":\"x\"" always;
|
||||
// "token x" with only a space when x is quoted, holds a digit or is 8 characters or more.
|
||||
new RegExp(`\\b${KEY_WORD}["']?\\s*[:=]\\s*(?:"[^"]*"?|'[^']*'?|[^\\s,;}]+)`, "gi"),
|
||||
new RegExp(`\\b${KEY_WORD}["']?\\s+(?:"[^"]*"?|'[^']*'?|(?=[^\\s"',;}]*\\d)[^\\s"',;}]+|[^\\s"',;}]{8,})`, "gi"),
|
||||
/\b(?:sk|msk|rk|pk|ghp|gho|ghs|glpat|xox[a-z])[-_]\S+/gi,
|
||||
/\beyJ\S+/g, // JWT
|
||||
// A long run that mixes letters and digits, or any very long run, reads as a key.
|
||||
/[A-Za-z0-9_\-+/=.]{32,}/g,
|
||||
/(?<![A-Za-z0-9_\-+/=.])(?=[A-Za-z0-9_\-+/=.]*\d)(?=[A-Za-z0-9_\-+/=.]*[A-Za-z])[A-Za-z0-9_\-+/=.]{16,}/g,
|
||||
];
|
||||
|
||||
export function cleanText(text) {
|
||||
return String(text).replace(ANSI, "").replace(INVISIBLE, "").replace(BREAKS, " ").replace(/\s+/g, " ").trim();
|
||||
}
|
||||
|
||||
// A text read from its tail keeps its end, where the error is; any other keeps its start.
|
||||
function cap(text, fromEnd = false, n = CAUSE_MAX) {
|
||||
const chars = Array.from(text);
|
||||
if (chars.length <= n) return text;
|
||||
return fromEnd ? "..." + chars.slice(-(n - 3)).join("") : chars.slice(0, n - 3).join("") + "...";
|
||||
}
|
||||
|
||||
// The plain cause for a failed turn's stdout, or null when there is no text.
|
||||
// `secrets` is secretValues(env) for the turn.
|
||||
export function turnCause(stdout, secrets = []) {
|
||||
if (typeof stdout !== "string") return null;
|
||||
const decoded = decodeText(stdout);
|
||||
const clean = cleanText(decoded);
|
||||
if (!clean) return null;
|
||||
// The secret check covers the whole text, not only the part that is read.
|
||||
const compact = compactText(stdout);
|
||||
if (secrets.some(value => compact.includes(value))) return WITHHELD;
|
||||
// Only the tail is classified and shown. Redaction runs over the whole text before any cut,
|
||||
// so a cut that lands between a key word and its value can't leave the value bare.
|
||||
const tail = clean.length > READ_MAX;
|
||||
const read = tail ? clean.slice(-READ_MAX) : clean;
|
||||
if (USAGE.test(read)) return PLAIN.usage;
|
||||
if (SIGNIN.test(read)) return PLAIN.signin;
|
||||
if (MODEL.test(read)) return PLAIN.model;
|
||||
let shown = clean;
|
||||
for (const re of REDACT) shown = shown.replace(re, "[redacted]");
|
||||
shown = shown.replace(/(?:\[redacted\]\s*){2,}/g, "[redacted] ").trim();
|
||||
// The cut drops the partial word it lands in; a redacted text short enough needs no cut.
|
||||
if (shown.length > READ_MAX) shown = shown.slice(-READ_MAX).replace(/^\S+\s*/, "");
|
||||
// Text that redacts to nothing but [redacted] says nothing; the technical reason stands alone.
|
||||
return shown && shown !== "[redacted]" ? cap(shown, tail) : null;
|
||||
}
|
||||
|
||||
// The chat body for a failed turn: the plain cause first, the technical reason after.
|
||||
export const DETAILS = "\nDetails: ";
|
||||
export function failureBody(turn) {
|
||||
return turn.cause ? `The turn failed: ${turn.cause}${DETAILS}${turn.reason}` : `The turn failed: ${turn.reason}`;
|
||||
}
|
||||
@@ -0,0 +1,778 @@
|
||||
// #1873 pi-unpin S3: the start-time fit check for a pi binary the harness module found. Nothing
|
||||
// here compares a version. The check runs the binary only inside a sandbox with the network
|
||||
// unshared, the found package mounted as a session mounts it, and a bus session's environment
|
||||
// (fitEnv below), so its agent directory is $HOME/.pi/agent under a scratch HOME, as a session's
|
||||
// is. No model is called and no provider key is passed. It asks:
|
||||
//
|
||||
// --version a label for the refusal line and the record; not a gate
|
||||
// --help lists, in its option column, every flag Mosaic passes, with rpc mode and each
|
||||
// thinking level
|
||||
// control rpc, planted ambient sources, a saved trust for the project, the seal flags less
|
||||
// every loader flag (the ones that each keep one kind of source out: --no-approve,
|
||||
// --no-extensions, --no-skills, --no-prompt-templates, --no-context-files, and
|
||||
// --no-mcp when --help lists it), the --system-prompt file the sealed runs pass, and
|
||||
// one -e extension that writes what it sees at session_start: the system prompt, the
|
||||
// command names and the session id. The binary answers get_state and get_commands,
|
||||
// its -e extension lists the planted skill and prompt template (the -p runs see
|
||||
// those kinds only that way), and it shows each kind of planted source:
|
||||
// an extension, a skill, a prompt template, a context file, a trusted project file
|
||||
// (the project's .pi/APPEND_SYSTEM.md, which only the trust --no-approve must undo
|
||||
// lets in), and an MCP server when --help lists --no-mcp. Only a source outside the
|
||||
// project counts for the other kinds, since a project source can wait on trust. A
|
||||
// kind it doesn't show could not be told apart from a sealed one.
|
||||
// sealed rpc the adapter's seal flags (and --no-mcp when --help lists it), the -e extension and
|
||||
// --system-prompt. The binary answers both, loads that extension (a session's gate
|
||||
// comes in that way), uses the --system-prompt file, and shows no planted source.
|
||||
// one out one more sealed rpc run per loader flag, with every seal flag but that one. The
|
||||
// kind it keeps out must show, and no other kind (pi without --no-mcp ties MCP to
|
||||
// --no-extensions, so an MCP server may show beside an extension). So each loader flag
|
||||
// is shown to keep its own kind out, and no other seal flag, nor the order they come
|
||||
// in, can stand in for it.
|
||||
// session five -p runs in a bus session's shape, with the arguments adapters/pi/adapter.sh
|
||||
// passes for a session, in its order (sessionShape below): two -e, --session-dir in
|
||||
// place of --no-session, a fixed --provider and --model, -p and a message. The
|
||||
// adapter's four session branches (a tool list or --no-tools, -c over a stored
|
||||
// session or none, --thinking or none, --mode json or none) are spread over the five
|
||||
// runs so each pair of them is passed together (SESSION_RUNS). A -c run gets a
|
||||
// stored session for the project and must resume it. Each run loads both -e
|
||||
// extensions, uses the --system-prompt file, and shows no
|
||||
// planted source. A test drives the adapter through every branch set, tool list
|
||||
// length and thinking level, and fails on an argv sessionShape doesn't build or a
|
||||
// pair no run passes.
|
||||
// sealed -p the seal flags in -p with --no-session, as a worker run passes them. Its -e
|
||||
// extension must reach session_start, and no planted source may show.
|
||||
// missing -e a missing -e file, in rpc and in -p, stops the start: exit non-zero, no answer,
|
||||
// and no session_start for the -e extension given after it
|
||||
//
|
||||
// Every run gets a fresh scratch directory with the sources planted again, so no run sees what
|
||||
// another one left. The directory's name carries the pid, start time and pid namespace of the
|
||||
// process that made it, and each check first stops the processes and removes the directories a
|
||||
// process no longer running left (sweepScratch below). Every run dies with the process that runs
|
||||
// the check (checkArgv in packages/seat/src/sandbox.mjs), at once when unshare runs here and at
|
||||
// the next check's sweep when it doesn't.
|
||||
// The runs share no state: up to FIT_CONCURRENCY of them run at once,
|
||||
// each a child process the check awaits, so the process that runs the check (the bus) keeps
|
||||
// answering while it runs. --version and --help go first; the rest start in the order above and
|
||||
// are judged in that order, so the first failure is the one a refusal names, and the runs still
|
||||
// going are killed. Each run passes the same -e and --system-prompt; the other runs differ only by
|
||||
// their seal flags, their mode, the -p message and the missing -e file. Each run starts the binary
|
||||
// through /bin/sh, as the adapter does, with fitEnv's variables: the environment a bus session's pi
|
||||
// gets, built by the code that builds it (the launcher's ENV_ALLOW and sessionEnv, the sandbox's
|
||||
// sessionSetenv, the runner's adapterEnv and the pi module's harnessEnv) from the calling process's
|
||||
// own environment, less the provider variables. So a fit run has a name only when a session in that
|
||||
// process has it (TERM, TZ and LC_ALL only when the bus has them), no run sets a variable a session
|
||||
// lacks, such as PI_CODING_AGENT_DIR, and a test holds the two together.
|
||||
//
|
||||
// A check that cannot tell refuses: a sandbox that fails, a run that times out or prints more
|
||||
// than 4 MB, an answer that is missing or doesn't parse, a kind of source the control doesn't
|
||||
// show, or the kind a one-out run should show. A one-out run that shows another kind, or a
|
||||
// session-shaped run that shows any, is a leak. A --version that fails or hangs past 3 s only
|
||||
// leaves no label. A pass is cached in memory by a digest of the files that run (the package's
|
||||
// package.json, every file under dist/bundle/, every file in the directory tree that holds the
|
||||
// binary's real file, and, for a copy mounted with the node_modules it sits in, npm's lock files
|
||||
// there), the mount layout and the names in the fit runs' environment, so a process with other
|
||||
// names checks again. Callers that ask while a check for the same key is running share it.
|
||||
// A refusal line names the found binary by its label (MOSAIC_PI_BIN, the pi on PATH, the
|
||||
// repository's pi), never a host path.
|
||||
//
|
||||
// Known limit: pi 0.85.1 appends the agent directory's APPEND_SYSTEM.md whatever the flags. The
|
||||
// check doesn't plant one; a session's agent directory is the run's own. Not run: --fork,
|
||||
// --skill with --no-skills dropped, and the interactive TUI, which only worker containers and
|
||||
// scripts/agent.sh pass; the session branches three or four at a time beyond the session-shaped
|
||||
// runs; -c over a session directory that holds nothing for the project (the adapter passes -c
|
||||
// whenever the directory isn't empty), since one more run costs about 340 ms cold; and a
|
||||
// session's own values: its tool names beyond read and bash, thinking levels beyond low,
|
||||
// provider, model, key, -e files and request. The runs are offline with no key, so nothing
|
||||
// past the start is tried. A pi built to act differently with those is outside what a start-time
|
||||
// check can catch.
|
||||
import { spawn } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, readlinkSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join, relative } from "node:path";
|
||||
import { BWRAP, SETPRIV, UNSHARE, checkArgv, outerArgv, sessionSetenv } from "../../seat/src/sandbox.mjs";
|
||||
import { isProviderVariable, sessionEnv } from "../../seat/src/session.mjs";
|
||||
import { adapterEnv } from "./runner.mjs";
|
||||
import { FIT_ROUTE, harnessEnv } from "../../plugins/harness/pi/session-env.mjs";
|
||||
|
||||
// The flags adapters/pi/adapter.sh and the conversation engine pass.
|
||||
export const FIT_FLAGS = Object.freeze(["--mode", "--session", "--no-session", "--session-dir", "--fork", "-c", "--no-extensions", "-e", "--no-skills", "--skill", "--no-prompt-templates", "--no-themes", "--no-context-files", "--no-approve", "--offline", "--tools", "--no-tools", "--provider", "--model", "--thinking", "--system-prompt", "-p"]);
|
||||
// The adapter's seal flags, as it passes them in an ephemeral session with no tools.
|
||||
const SEAL = ["--offline", "--no-approve", "--no-extensions", "--no-skills", "--no-prompt-templates", "--no-themes", "--no-context-files", "--no-tools", "--no-session"];
|
||||
// The loader flags: each keeps one kind of planted source out. --no-mcp counts when --help lists it.
|
||||
const LOADERS = [["--no-approve", "trusted project file"], ["--no-extensions", "extension"], ["--no-skills", "skill"], ["--no-prompt-templates", "prompt template"], ["--no-context-files", "context file"]];
|
||||
// The session-shaped runs' fixed values: a two-tool list, a thinking level, a provider and model.
|
||||
// Offline with no key, pi stops before any model call. The provider and model are the pi module's
|
||||
// (session-env.mjs FIT_ROUTE), so no core source names them.
|
||||
export const SESSION_SHAPE = Object.freeze({ tools: "read,bash", thinking: "low", provider: FIT_ROUTE.provider, model: FIT_ROUTE.model });
|
||||
// The branches adapters/pi/adapter.sh takes for a bus session, each run once or not: a tool list or
|
||||
// --no-tools, -c over a stored session or a fresh one, --thinking or none, --mode json (the model
|
||||
// event monitor) or none. The five runs pass every pair of those together; pi-fit.test.mjs drives
|
||||
// the adapter through all sixteen and fails on a pair no run passes.
|
||||
const T = SESSION_SHAPE.tools;
|
||||
const L = SESSION_SHAPE.thinking;
|
||||
export const SESSION_RUNS = Object.freeze([
|
||||
{ tools: "", cont: false, thinking: "", json: false },
|
||||
{ tools: "", cont: true, thinking: L, json: true },
|
||||
{ tools: T, cont: false, thinking: L, json: true },
|
||||
{ tools: T, cont: true, thinking: "", json: true },
|
||||
{ tools: T, cont: true, thinking: L, json: false },
|
||||
].map((r) => Object.freeze(r)));
|
||||
// The arguments adapters/pi/adapter.sh builds for a bus session: the given -e files, a tool list
|
||||
// or none, a persistent session directory with -c when it holds a session, a thinking level or
|
||||
// none, the --system-prompt file, -p and the request, and --mode json when the model event monitor
|
||||
// runs pi. No --skill and no --fork: a bus session never passes them. Keep its order:
|
||||
// pi-fit.test.mjs runs the adapter through every branch and fails when the two differ.
|
||||
export function sessionShape({ noMcp, extensions, prompt, sessionDir, tools, cont, thinking, json, message }) {
|
||||
return [
|
||||
"--offline", "--no-approve", "--no-extensions", ...(noMcp ? ["--no-mcp"] : []), ...extensions.flatMap((e) => ["-e", e]),
|
||||
"--no-skills", "--no-prompt-templates", "--no-themes", "--no-context-files",
|
||||
...(tools ? ["--tools", tools] : ["--no-tools"]), "--session-dir", sessionDir, ...(cont ? ["-c"] : []),
|
||||
"--provider", SESSION_SHAPE.provider, "--model", SESSION_SHAPE.model, ...(thinking ? ["--thinking", thinking] : []),
|
||||
"--system-prompt", prompt, "-p", message, ...(json ? ["--mode", "json"] : []),
|
||||
];
|
||||
}
|
||||
// What a session-shaped run is named by in a refusal line.
|
||||
const shapeName = ({ tools, cont, thinking, json }) => `in a session's shape with ${[tools ? "--tools" : "--no-tools", ...(cont ? ["-c"] : []), ...(thinking ? ["--thinking"] : []), ...(json ? ["--mode json"] : [])].join(", ")}`;
|
||||
const RUN_TIMEOUT = 20000;
|
||||
// --version gives only a label, so a hang costs this much and leaves no label.
|
||||
const VERSION_TIMEOUT = 3000;
|
||||
const OUTPUT_MAX = 4 * 1024 * 1024;
|
||||
// How long a run's pipes may stay open after it exits or is killed (spawned).
|
||||
const PIPE_GRACE = 1000;
|
||||
const DIGEST_FILES_MAX = 20000;
|
||||
|
||||
// At most this many fit runs at once. Each run is one sandboxed pi start in its own scratch
|
||||
// directory.
|
||||
export const FIT_CONCURRENCY = 4;
|
||||
|
||||
const passed = new Map();
|
||||
const running = new Map();
|
||||
|
||||
// #1873 re-check 9: each run's scratch directory is named for the process that makes it, by its
|
||||
// pid and its start time (field 22 of /proc/<pid>/stat), so a pid used again later is not taken
|
||||
// for it. A process killed mid-check can't remove its own directories. So a check removes, before
|
||||
// it starts, every directory here that a process no longer running made: its pid is gone, or now
|
||||
// belongs to a process that started at another time. A directory whose process can't be read, or
|
||||
// one this uid doesn't own, or a symlink, is left alone.
|
||||
//
|
||||
// #1873 re-check 10: the name also carries the maker's pid namespace (the inode of
|
||||
// /proc/<pid>/ns/pid), and a sweep leaves alone every directory made in another one, since it
|
||||
// can't read that process. And before it removes a directory, the sweep stops the processes that
|
||||
// name it on their command line: a run's every layer does (the setpriv, shell, unshare and bwrap
|
||||
// argv, bwrap's child before its exec, pi's own -e and --system-prompt), and so do the sandbox
|
||||
// probes. Without unshare's outer layer a bwrap child killed in its setup waits for good (see
|
||||
// checkArgv in sandbox.mjs), and this is what stops it. A process is stopped only when an argument
|
||||
// is that directory or a path in it, its real and effective uid are this process's, and its start
|
||||
// time is the same before and after its command line is read. Never by name, never another uid's,
|
||||
// never one whose directory's maker still runs.
|
||||
const SCRATCH = /^mosaic-pi-fit-(\d+)-(\d+)-(\d+)-/;
|
||||
const startTime = (pid) => readFileSync(`/proc/${pid}/stat`, "utf8").split(") ").pop().split(" ")[19];
|
||||
const pidNamespace = (pid) => /^pid:\[(\d+)\]$/.exec(readlinkSync(`/proc/${pid}/ns/pid`))[1];
|
||||
let ownName = null;
|
||||
const scratchPrefix = () => `mosaic-pi-fit-${ownName ??= `${process.pid}-${startTime(process.pid)}-${pidNamespace("self")}`}-`;
|
||||
function sweepScratch(dir = tmpdir()) {
|
||||
let names;
|
||||
try {
|
||||
names = readdirSync(dir);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
const uid = process.getuid();
|
||||
const ns = pidNamespace("self");
|
||||
const stale = [];
|
||||
for (const name of names) {
|
||||
const m = SCRATCH.exec(name);
|
||||
if (!m || m[3] !== ns) continue;
|
||||
try {
|
||||
let alive;
|
||||
try {
|
||||
alive = startTime(m[1]) === m[2];
|
||||
} catch (e) {
|
||||
if (e.code !== "ENOENT") continue;
|
||||
alive = false;
|
||||
}
|
||||
if (alive) continue;
|
||||
const at = join(dir, name);
|
||||
const st = lstatSync(at);
|
||||
if (st.isDirectory() && st.uid === uid) stale.push(at);
|
||||
} catch {}
|
||||
}
|
||||
if (!stale.length) return;
|
||||
stopNaming(stale, uid);
|
||||
for (const at of stale) {
|
||||
try {
|
||||
rmSync(at, { recursive: true, force: true });
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
// SIGKILL to every process of uid (real and effective) with an argument that is one of dirs or a
|
||||
// path in one, other than this one.
|
||||
function stopNaming(dirs, uid) {
|
||||
let pids;
|
||||
try {
|
||||
pids = readdirSync("/proc").filter((p) => /^\d+$/.test(p) && Number(p) !== process.pid);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
const names = (arg) => dirs.some((d) => arg === d || arg.startsWith(`${d}/`));
|
||||
for (const pid of pids) {
|
||||
try {
|
||||
const start = startTime(pid);
|
||||
const ids = /^Uid:\s+(\d+)\s+(\d+)\s/m.exec(readFileSync(`/proc/${pid}/status`, "utf8"));
|
||||
if (!ids || Number(ids[1]) !== uid || Number(ids[2]) !== uid) continue;
|
||||
if (!readFileSync(`/proc/${pid}/cmdline`, "utf8").split("\0").some(names)) continue;
|
||||
if (startTime(pid) !== start) continue;
|
||||
process.kill(Number(pid), "SIGKILL");
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
// The provider variables never reach a fit run: no key is passed, since no model is called.
|
||||
const withoutProviders = (host) => Object.fromEntries(Object.entries(host ?? {}).filter(([k, v]) => typeof v === "string" && !isProviderVariable(k)));
|
||||
// The names a fit run's host part holds: sessionEnv's answer for the process's environment.
|
||||
export const fitNames = (host = process.env) => Object.keys(sessionEnv(withoutProviders(host), "fit")).sort();
|
||||
const fitKey = (found, { levels, bwrap, unshare, node, host }) => JSON.stringify([filesDigest(found), found.root, found.dest ?? null, found.view, bwrap, unshare, node, levels, fitNames(host)]);
|
||||
|
||||
// found: binary.mjs's answer ({ bin, label, root, dest, view, pkg }). levels: the thinking levels
|
||||
// the module applies. host: the environment of the process sessions launch from (the bus's own),
|
||||
// which a fit run's environment is built from as a session's is. bwrap and unshare: where those
|
||||
// tools are (tests pass stand-ins). Returns a promise of { ok: true, version, noMcp, digest, outer }
|
||||
// or { ok: false, message }. outer: { used: true } when the runs had unshare's outer layer, else
|
||||
// { used: false, why } with why naming unshare and how it failed (sandboxLayers below). A call
|
||||
// while a check with the same key runs gets that check's promise. A check that throws (a scratch directory it can't
|
||||
// make, say) can't tell, so it gives { ok: false, message } too and keeps nothing.
|
||||
export function piFit(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, node = process.execPath, host = process.env } = {}) {
|
||||
const who = (version) => `pi${version ? ` ${version}` : ""} (${found.label})`;
|
||||
let key;
|
||||
try {
|
||||
key = fitKey(found, { levels, bwrap, unshare, node, host });
|
||||
} catch {
|
||||
return Promise.resolve({ ok: false, message: `${who(null)} can't be checked: its files can't be read` });
|
||||
}
|
||||
if (passed.has(key)) return Promise.resolve(passed.get(key));
|
||||
if (running.has(key)) return running.get(key);
|
||||
const job = (async () => {
|
||||
try {
|
||||
sweepScratch();
|
||||
// The sandbox's own error text can name a host path, so the line keeps only what failed.
|
||||
const layers = await sandboxLayers({ bwrap, unshare, node });
|
||||
if (layers.problem) return { ok: false, message: `${who(null)} can't be checked: ${layers.problem}` };
|
||||
const result = await check(found, { levels, bwrap, unshare: layers.outer, node, who, digest: JSON.parse(key)[0], host });
|
||||
if (!result.ok) return result;
|
||||
const passing = { ...result, outer: layers.outer ? { used: true } : { used: false, why: layers.why } };
|
||||
passed.set(key, passing);
|
||||
return passing;
|
||||
} catch {
|
||||
// #1873 re-check 8: refused as a fit that failed. The error's text can name a host path, so
|
||||
// the line keeps none of it.
|
||||
return { ok: false, message: `${who(null)} can't be checked: the check could not run` };
|
||||
}
|
||||
})().finally(() => { if (running.get(key) === job) running.delete(key); });
|
||||
running.set(key, job);
|
||||
return job;
|
||||
}
|
||||
|
||||
// The passed check for found as piFit would key it now, or null: none passed, or its files can't
|
||||
// be read. Nothing is run.
|
||||
export function fitVerdict(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, node = process.execPath, host = process.env } = {}) {
|
||||
try {
|
||||
return passed.get(fitKey(found, { levels, bwrap, unshare, node, host })) ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function check(found, { levels, bwrap, unshare, node, who, digest, host }) {
|
||||
// At most FIT_CONCURRENCY runs at once, started in the order they are asked for. stop kills the
|
||||
// runs still going and keeps queued ones from starting; the check waits for every run it began,
|
||||
// so no scratch directory or child outlives it.
|
||||
const stop = new AbortController();
|
||||
const begun = [];
|
||||
let active = 0;
|
||||
const waiting = [];
|
||||
const slot = () => active < FIT_CONCURRENCY ? (active++, Promise.resolve()) : new Promise((resolve) => waiting.push(resolve));
|
||||
const free = () => { const next = waiting.shift(); if (next) next(); else active--; };
|
||||
// args: the arguments, or a function of the run's scratch directory that returns them (it may
|
||||
// plant more first). The answer is read before the scratch directory goes: the rpc answers,
|
||||
// whether each -e extension loaded, what the first one saw at session_start (the system prompt,
|
||||
// the command names, the session id), and the planted sources that showed, through the rpc
|
||||
// get_commands answer or through the -e extension's command list (listed: through that alone).
|
||||
// shape: a session-shaped run's branches, which set its tools, thinking level and monitor
|
||||
// variables as a session's would. A run that stop kept from starting gives null.
|
||||
const once = async (args, input, { timeout, noMcp, shape }) => {
|
||||
await slot();
|
||||
try {
|
||||
if (stop.signal.aborted) return null;
|
||||
const scratch = mkdtempSync(join(tmpdir(), scratchPrefix()));
|
||||
let ran = null;
|
||||
try {
|
||||
plant(scratch);
|
||||
const env = fitEnv(scratch, { view: found.view, noMcp, shape, host });
|
||||
const r = ran = await runIn(found, { bwrap, unshare, node, scratch, env, args: typeof args === "function" ? args(scratch) : args, input, timeout, signal: stop.signal });
|
||||
const answer = answered(r.stdout);
|
||||
const start = started(scratch);
|
||||
const listed = start?.commands ?? [];
|
||||
return { ...r, answer, explicit: marked(scratch, EXPLICIT), second: marked(scratch, SECOND), prompt: start?.prompt ?? null, session: start?.session ?? null, listed: named(listed), seen: seen(scratch, [...(answer.commands ?? []), ...listed], start) };
|
||||
} finally {
|
||||
// #1873 re-check 10: a run this check killed (a timeout, or stop) may have had bwrap in its
|
||||
// setup, and without unshare's outer layer bwrap's child then waits for good. The run is
|
||||
// over, so whatever still names its directory is left from it.
|
||||
if (ran?.stopped) stopNaming([scratch], process.getuid());
|
||||
rmSync(scratch, { recursive: true, force: true });
|
||||
}
|
||||
} finally {
|
||||
free();
|
||||
}
|
||||
};
|
||||
const run = (args, input = "", { timeout = RUN_TIMEOUT, noMcp = false, shape = null } = {}) => {
|
||||
const p = once(args, input, { timeout, noMcp, shape });
|
||||
// Judged in order, a later run can fail before an earlier one is awaited: this keeps that
|
||||
// failure from counting as unhandled. Awaiting p still throws it.
|
||||
p.catch(() => {});
|
||||
begun.push(p);
|
||||
return p;
|
||||
};
|
||||
try {
|
||||
return await judge(found, { levels, who, digest, run });
|
||||
} finally {
|
||||
stop.abort();
|
||||
await Promise.allSettled(begun);
|
||||
}
|
||||
}
|
||||
|
||||
async function judge(found, { levels, who, digest, run }) {
|
||||
const helped = run(["--help"]);
|
||||
const versioned = run(["--version"], "", { timeout: VERSION_TIMEOUT });
|
||||
const help = await helped;
|
||||
const v = await versioned;
|
||||
const version = v.status === 0 ? versionLabel(v.stdout) : null;
|
||||
const refuse = (why) => ({ ok: false, message: `${who(version)} ${why}` });
|
||||
if (help.status !== 0) return refuse("can't be checked: --help failed");
|
||||
const lines = help.stdout.split("\n");
|
||||
const has = (flag) => lines.some((l) => optionsIn(l).includes(flag));
|
||||
const line = (flag) => lines.find((l) => optionsIn(l).includes(flag)) ?? "";
|
||||
const missing = FIT_FLAGS.filter((f) => !has(f));
|
||||
if (has("--mode") && !word(line("--mode"), "rpc")) missing.push("--mode rpc");
|
||||
if (has("--thinking")) for (const l of levels) if (!word(line("--thinking"), l)) missing.push(`--thinking ${l}`);
|
||||
if (missing.length) return refuse(`lacks ${missing.join(", ")}`);
|
||||
const noMcp = has("--no-mcp");
|
||||
|
||||
const ask = '{"id":"fit-state","type":"get_state"}\n{"id":"fit-commands","type":"get_commands"}\n';
|
||||
// Every run passes the same -e and --system-prompt, so the runs differ only by the seal flags.
|
||||
const given = (scratch) => ["-e", join(scratch, "explicit", "fit.ts"), "--system-prompt", join(scratch, "prompt.md")];
|
||||
const seal = [...SEAL, ...(noMcp ? ["--no-mcp"] : [])];
|
||||
const loaders = [...LOADERS, ...(noMcp ? [["--no-mcp", "MCP server"]] : [])];
|
||||
const without = (...flags) => seal.filter((f) => !flags.includes(f));
|
||||
const rpcRun = (flags) => run((s) => ["--mode", "rpc", ...flags, ...given(s)], ask, { noMcp });
|
||||
|
||||
// Every run is asked for now, in the order they are judged below; none depends on another.
|
||||
// The control keeps the seal flags no planted source tells apart (--offline, --no-themes,
|
||||
// --no-tools, --no-session), so one of those can't stand in for a loader flag either.
|
||||
const controlRun = rpcRun(without(...loaders.map(([f]) => f)));
|
||||
const rpcSealed = rpcRun(seal);
|
||||
const oneOut = loaders.map(([flag, kind]) => [flag, kind, rpcRun(without(flag))]);
|
||||
// A bus session's shapes, in -p as its turns run: --session-dir in place of --no-session, two -e
|
||||
// as a session passes, and each pair of the adapter's session branches in one run. A -c run gets
|
||||
// a stored session for the project first and must resume it, since -c with none starts afresh.
|
||||
const shaped = SESSION_RUNS.map((shape) => [shape, run((s) => {
|
||||
if (shape.cont) plantSession(s);
|
||||
return sessionShape({ noMcp, extensions: [join(s, "explicit", "fit.ts"), join(s, "explicit", "second.ts")], prompt: join(s, "prompt.md"), sessionDir: join(s, "sessions"), message: "fit check", ...shape });
|
||||
}, "", { noMcp, shape })]);
|
||||
// -p takes a message; offline with no key, pi stops before any model call.
|
||||
const pRun = run((s) => ["-p", ...seal, ...given(s), "fit check"], "", { noMcp });
|
||||
// A missing -e file stops the start. pi 0.85.1 loads the -e files in order and still loads the
|
||||
// one after the missing one, then exits 1 before session_start, so only session_start tells.
|
||||
const missingRuns = [["rpc", ["--mode", "rpc"], ask], ["-p", ["-p"], ""]].map(([mode, args, input]) =>
|
||||
[mode, run((s) => [...args, ...seal, "-e", join(s, "missing", "extension.ts"), ...given(s), ...(mode === "-p" ? ["fit check"] : [])], input, { noMcp })]);
|
||||
|
||||
const control = await controlRun;
|
||||
if (control.stopped) return refuse(`can't be checked: the start without the seal flags ${control.stopped}`);
|
||||
if (!control.answer.state) return refuse("can't be checked: it didn't answer get_state");
|
||||
if (!control.answer.commands) return refuse("can't be checked: it didn't answer get_commands");
|
||||
if (!control.explicit || control.prompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt without the seal flags");
|
||||
// A kind counts only through a source outside the project, except the trusted project files
|
||||
// themselves: a project source can wait on project trust, and then --no-approve alone would keep
|
||||
// it out of the sealed runs and hide a seal flag of its kind that the binary ignores.
|
||||
const shown = new Set(control.seen.filter((s) => s.kind === "trusted project file" || !s.source.startsWith("project ")).map((s) => s.kind));
|
||||
const unshown = KINDS.filter((k) => (k !== "MCP server" || noMcp) && !shown.has(k));
|
||||
if (unshown.length) return refuse(`can't be checked: it didn't load a planted ${unshown.join(", ")} without the seal flags`);
|
||||
// -p answers no get_commands, so the -p runs see skills and prompt templates only through the -e
|
||||
// extension's command list, which must show them here.
|
||||
const listed = new Set(control.listed.filter((s) => !s.source.startsWith("project ")).map((s) => s.kind));
|
||||
const unlisted = ["skill", "prompt template"].filter((k) => !listed.has(k));
|
||||
if (unlisted.length) return refuse(`can't be checked: its -e extension didn't list a planted ${unlisted.join(", ")} without the seal flags`);
|
||||
|
||||
const rpc = await rpcSealed;
|
||||
if (rpc.stopped) return refuse(`can't be checked: the start with the seal flags ${rpc.stopped}`);
|
||||
if (!rpc.answer.state) return refuse("can't be checked: it didn't answer get_state with the seal flags on");
|
||||
if (!rpc.answer.commands) return refuse("can't be checked: it didn't answer get_commands with the seal flags on");
|
||||
if (!rpc.explicit) return refuse("didn't load an -e extension with the seal flags on");
|
||||
if (rpc.prompt === null) return refuse("can't be checked: its -e extension didn't see the system prompt with the seal flags on");
|
||||
if (!rpc.prompt.includes(GIVEN)) return refuse("didn't use the --system-prompt file with the seal flags on");
|
||||
if (rpc.seen.length) return refuse(`loaded a planted ${rpc.seen.map((s) => s.source).join(", ")} with the seal flags on`);
|
||||
|
||||
// Each loader flag on its own: with every other seal flag on, its kind must show.
|
||||
for (const [flag, kind, pending] of oneOut) {
|
||||
const r = await pending;
|
||||
const but = `with every seal flag but ${flag}`;
|
||||
if (r.stopped) return refuse(`can't be checked: the start ${but} ${r.stopped}`);
|
||||
if (!r.answer.state || !r.answer.commands) return refuse(`can't be checked: it didn't answer get_state and get_commands ${but}`);
|
||||
if (r.prompt === null) return refuse(`can't be checked: its -e extension didn't see the system prompt ${but}`);
|
||||
if (!r.seen.some((s) => s.kind === kind)) return refuse(`can't be checked: it didn't load a planted ${kind} ${but}`);
|
||||
const others = r.seen.filter((s) => s.kind !== kind && !(flag === "--no-extensions" && !noMcp && s.kind === "MCP server"));
|
||||
if (others.length) return refuse(`loaded a planted ${others.map((s) => s.source).join(", ")} ${but}`);
|
||||
}
|
||||
|
||||
for (const [shape, pending] of shaped) {
|
||||
const r = await pending;
|
||||
const name = shapeName(shape);
|
||||
if (r.stopped) return refuse(`can't be checked: the start ${name} ${r.stopped}`);
|
||||
if (r.prompt === null) return refuse(`can't be checked: its -e extension didn't see the system prompt ${name}`);
|
||||
if (!r.explicit || !r.second) return refuse(`didn't load every -e extension ${name}`);
|
||||
if (!r.prompt.includes(GIVEN)) return refuse(`didn't use the --system-prompt file ${name}`);
|
||||
if (shape.cont && r.session !== PLANTED_SESSION) return refuse(`can't be checked: it didn't resume the planted session with -c ${name}`);
|
||||
if (r.seen.length) return refuse(`loaded a planted ${r.seen.map((x) => x.source).join(", ")} with the seal flags ${name}`);
|
||||
}
|
||||
|
||||
const p = await pRun;
|
||||
if (p.stopped) return refuse(`can't be checked: a -p start with the seal flags ${p.stopped}`);
|
||||
if (p.prompt === null) return refuse("can't be checked: a -p start with the seal flags didn't run its -e extension");
|
||||
if (p.seen.length) return refuse(`loaded a planted ${p.seen.map((x) => x.source).join(", ")} with the seal flags on in -p`);
|
||||
|
||||
for (const [mode, pending] of missingRuns) {
|
||||
const r = await pending;
|
||||
if (r.stopped) return refuse(`can't be checked: a ${mode} start with a missing -e extension ${r.stopped}`);
|
||||
if (r.status === 0 || r.answer.state || r.prompt !== null) return refuse("starts with a missing -e extension");
|
||||
}
|
||||
return { ok: true, version, noMcp, digest };
|
||||
}
|
||||
|
||||
// Whether bwrap runs here, as sandbox.mjs's sandboxProblem asks, but as child processes the caller
|
||||
// awaits: the system part, the network unshared, an empty environment, then /usr/bin/true. And,
|
||||
// once per check, whether unshare's outer layer runs (checkArgv in sandbox.mjs): first on its own,
|
||||
// then with bwrap inside it. Returns { problem } when the check can't run, naming the tool that
|
||||
// failed without its own text, else { outer, why }: outer is the unshare to pass checkArgv, or null
|
||||
// when the layer can't run here, and why then names unshare and how it failed. A missing,
|
||||
// too old or refused unshare drops only that layer and is no refusal: bwrap's sandbox is the same
|
||||
// either way. The probes run in a scratch directory of their own and name it on their command
|
||||
// line, so a probe process a killed owner leaves is one the next sweep finds.
|
||||
async function sandboxLayers({ bwrap, unshare, node }) {
|
||||
if (!existsSync(bwrap)) return { problem: "the sandbox (bwrap) is missing" };
|
||||
if (!existsSync(SETPRIV)) return { problem: "the sandbox's parent-death tool (setpriv) is missing" };
|
||||
const scratch = mkdtempSync(join(tmpdir(), scratchPrefix()));
|
||||
let stopped = false;
|
||||
try {
|
||||
const probe = async (argv) => {
|
||||
const r = await spawned(argv, { input: "", timeout: 10000, signal: new AbortController().signal });
|
||||
stopped ||= Boolean(r.stopped);
|
||||
return r.status === 0;
|
||||
};
|
||||
const sandboxed = (outer) => checkArgv([{ kind: "ro", src: scratch }], ["/usr/bin/true"], { chdir: scratch, node, bwrap, unshare: outer });
|
||||
let why = "unshare is missing";
|
||||
if (existsSync(unshare)) {
|
||||
if (!await probe(outerArgv(["/usr/bin/true", scratch], { unshare }))) why = "unshare failed";
|
||||
else if (await probe(sandboxed(unshare))) return { outer: unshare, why: null };
|
||||
else why = "unshare failed with bwrap inside it";
|
||||
}
|
||||
if (!await probe(sandboxed(null))) return { problem: "the sandbox (bwrap) failed" };
|
||||
return { outer: null, why };
|
||||
} finally {
|
||||
// As for a run (once, in piFit): a probe killed at its timeout may leave what names its directory.
|
||||
if (stopped) stopNaming([scratch], process.getuid());
|
||||
rmSync(scratch, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
// The binary, inside the sandbox: the system part, the found package where a session sees it, the
|
||||
// scratch directory read-write, and its tmp/ as /tmp, as a session has it. The environment is
|
||||
// fitEnv's. The binary starts through /bin/sh, as the adapter starts it, so it also gets the
|
||||
// variables the shell sets. The run is a child process the caller awaits; nothing here blocks.
|
||||
// stopped: why the run was killed ("timed out", "printed more than 4 MB", "was stopped"), or null.
|
||||
function runIn(found, { bwrap, unshare, node, scratch, env, args, input, timeout, signal }) {
|
||||
const list = [
|
||||
{ kind: "ro", src: found.root, ...(found.dest ? { dest: found.dest } : {}) },
|
||||
{ kind: "rw", src: scratch },
|
||||
{ kind: "rw", src: join(scratch, "tmp"), dest: "/tmp" },
|
||||
];
|
||||
return spawned(checkArgv(list, ["/bin/sh", "-c", 'exec "$0" "$@"', found.view, ...args], { env, chdir: join(scratch, "project"), node, bwrap, unshare }), { input, timeout, signal });
|
||||
}
|
||||
|
||||
// One child process with an empty environment, awaited: its exit status, its stdout, and why it was
|
||||
// killed, or null. It settles PIPE_GRACE after the child exits or is killed even if its pipes stay
|
||||
// open: a process left behind can hold them (a kill of bwrap's outer process while its child still
|
||||
// sets up leaves that child waiting until the pid namespace checkArgv makes is killed, or, without
|
||||
// that layer, until the next check's sweep), and no wait here is unbounded.
|
||||
function spawned(argv, { input, timeout, signal }) {
|
||||
return new Promise((resolve) => {
|
||||
const out = [];
|
||||
const size = { stdout: 0, stderr: 0 };
|
||||
let stopped = null;
|
||||
let done = false;
|
||||
let child;
|
||||
let timer = null;
|
||||
let grace = null;
|
||||
let exited = null;
|
||||
const finish = (status, sig) => {
|
||||
if (done) return;
|
||||
done = true;
|
||||
clearTimeout(timer);
|
||||
clearTimeout(grace);
|
||||
child?.stdout?.destroy();
|
||||
child?.stderr?.destroy();
|
||||
signal.removeEventListener("abort", onAbort);
|
||||
if (!stopped && sig === "SIGKILL") stopped = "timed out";
|
||||
resolve({ status, stdout: Buffer.concat(out).toString("utf8"), stopped });
|
||||
};
|
||||
const settleSoon = () => { if (!grace) grace = setTimeout(() => finish(exited?.status ?? null, exited?.sig ?? "SIGKILL"), PIPE_GRACE); };
|
||||
const kill = (why) => {
|
||||
if (!stopped) stopped = why;
|
||||
try { child?.kill("SIGKILL"); } catch {}
|
||||
settleSoon();
|
||||
};
|
||||
const onAbort = () => kill("was stopped");
|
||||
try {
|
||||
child = spawn(argv[0], argv.slice(1), { env: {}, stdio: ["pipe", "pipe", "pipe"] });
|
||||
} catch {
|
||||
finish(null, null);
|
||||
return;
|
||||
}
|
||||
timer = setTimeout(() => kill("timed out"), timeout);
|
||||
if (signal.aborted) onAbort();
|
||||
else signal.addEventListener("abort", onAbort, { once: true });
|
||||
const count = (stream) => (chunk) => {
|
||||
size[stream] += chunk.length;
|
||||
if (size[stream] > OUTPUT_MAX) kill(`printed more than ${OUTPUT_MAX / 1024 / 1024} MB`);
|
||||
else if (stream === "stdout") out.push(chunk);
|
||||
};
|
||||
child.stdout.on("data", count("stdout"));
|
||||
child.stderr.on("data", count("stderr"));
|
||||
child.stdin.on("error", () => {});
|
||||
child.on("error", () => finish(null, null));
|
||||
child.on("exit", (status, sig) => { exited = { status, sig }; settleSoon(); });
|
||||
child.on("close", (status, sig) => finish(status, sig));
|
||||
child.stdin.end(input);
|
||||
});
|
||||
}
|
||||
|
||||
// A fit run's environment: what a bus session's pi gets, built the same way from host, the
|
||||
// environment of the process sessions launch from (the bus's own). The launcher passes host's
|
||||
// ENV_ALLOW names that are set through sessionEnv (less the provider variables here, since no key
|
||||
// is passed: no model is called), with host's PATH behind the repository's .bin; the sandbox sets
|
||||
// sessionSetenv's (HOME is the scratch home, so pi's agent directory is $HOME/.pi/agent); the
|
||||
// runner adds adapterEnv's with the pi module's harnessEnv, and MOSAIC_REQUEST for each turn. No
|
||||
// name is added that a session in that process would lack: a bus started by its unit has no TERM,
|
||||
// TZ or LC_ALL, and then no fit run has them either. shape: a session-shaped run's branches (its
|
||||
// tool list, thinking level and --mode json, which a proxy session's first turn takes with the
|
||||
// model event monitor's variables), else none: no tools, no thinking, one -e. noMcp: --help lists
|
||||
// --no-mcp. pi-fit.test.mjs fails when these names, or the values the code sets, differ from a bus
|
||||
// session's, for a login shell's host and a unit's.
|
||||
export function fitEnv(scratch, { view, noMcp = false, shape = null, host = process.env }) {
|
||||
const runner = { ...sessionEnv(withoutProviders(host), "fit"), ...sessionSetenv(scratch, { business: "fit", role: "fit" }) };
|
||||
const { tools = "", thinking = "", json = false } = shape ?? {};
|
||||
const session = { provider: SESSION_SHAPE.provider, model: SESSION_SHAPE.model, thinking };
|
||||
const extensions = [join(scratch, "explicit", "fit.ts"), ...(shape ? [join(scratch, "explicit", "second.ts")] : [])];
|
||||
const env = adapterEnv({
|
||||
...session,
|
||||
bundle: { prompt: join(scratch, "prompt.md"), policy: join(scratch, "policy.json"), tools: join(scratch, "tools.json") },
|
||||
workspace: join(scratch, "project"), sessionDir: join(scratch, "sessions"), toolSocket: join(scratch, "tools.sock"), turnMarker: join(scratch, "turn"),
|
||||
harnessEnv: harnessEnv({ bin: view, noMcp, tools: tools ? tools.split(",") : [], extensions, session }),
|
||||
runDir: scratch, proxyFirstTurn: json,
|
||||
}, runner);
|
||||
return { ...env, MOSAIC_REQUEST: "fit check" };
|
||||
}
|
||||
|
||||
// The options one help line lists in its option column: a line that starts with "-" (after its
|
||||
// indent), up to the first run of two spaces or a tab, split at commas; the first word of each
|
||||
// part. " --extension, -e <path> Load an extension" lists --extension and -e. A flag named in a
|
||||
// sentence is not listed.
|
||||
function optionsIn(line) {
|
||||
const t = line.trim();
|
||||
if (!t.startsWith("-")) return [];
|
||||
return t.split(/ {2,}|\t/)[0].split(",").map((part) => part.trim().split(/[ =<[]/)[0]).filter((o) => /^--?[A-Za-z0-9]/.test(o));
|
||||
}
|
||||
const word = (line, w) => line.split(/[^A-Za-z0-9-]+/).includes(w);
|
||||
|
||||
// The version a refusal line may carry: a version-shaped first line, else none. Anything else the
|
||||
// binary prints stays out of the line.
|
||||
function versionLabel(stdout) {
|
||||
const first = String(stdout).split("\n")[0].trim();
|
||||
return /^v?[0-9]+(\.[0-9]+){0,3}([-+][0-9A-Za-z.-]{1,32})?$/.test(first) ? first : null;
|
||||
}
|
||||
|
||||
// The rpc answers in the output: whether get_state succeeded, and the command names get_commands
|
||||
// listed, or null when it didn't answer with a { commands: [...] } list.
|
||||
function answered(stdout) {
|
||||
let state = false;
|
||||
let commands = null;
|
||||
for (const l of String(stdout).split("\n")) {
|
||||
let m;
|
||||
try { m = JSON.parse(l); } catch { continue; }
|
||||
if (!m || m.type !== "response" || m.success !== true) continue;
|
||||
if (m.id === "fit-state" && m.command === "get_state" && m.data && typeof m.data === "object") state = true;
|
||||
if (m.id === "fit-commands" && m.command === "get_commands" && Array.isArray(m.data?.commands)) {
|
||||
commands = m.data.commands.filter((c) => typeof c?.name === "string").map((c) => c.name);
|
||||
}
|
||||
}
|
||||
return { state, commands };
|
||||
}
|
||||
|
||||
// The planted sources, each with the kind the control must show. Each extension and each MCP
|
||||
// server touches its own marker; each skill and prompt template has a name get_commands lists;
|
||||
// each context and system prompt file holds a word the -e extension's view of the system prompt
|
||||
// shows. "user" sources sit in the agent directory, $HOME/.pi/agent: no fit run sets
|
||||
// PI_CODING_AGENT_DIR, as no bus session does. "settings" sources are listed by a settings.json;
|
||||
// "package" sources come from a local package a settings.json lists. "parent" is a directory above
|
||||
// the project.
|
||||
const KINDS = ["extension", "MCP server", "skill", "prompt template", "context file", "trusted project file"];
|
||||
const MARKED = {
|
||||
"user extension": "extension", "user settings extension": "extension", "user package extension": "extension",
|
||||
"project extension": "extension", "project settings extension": "extension", "project package extension": "extension",
|
||||
"user mcp.json": "MCP server", "project mcp.json": "MCP server", "project .mcp.json": "MCP server",
|
||||
};
|
||||
const NAMED = {
|
||||
"fit-user-skill": ["user skill", "skill"], "fit-user-set-skill": ["user settings skill", "skill"], "fit-user-pkg-skill": ["user package skill", "skill"],
|
||||
"fit-project-skill": ["project skill", "skill"], "fit-project-set-skill": ["project settings skill", "skill"],
|
||||
"fit-project-pkg-skill": ["project package skill", "skill"], "fit-agents-skill": ["project .agents skill", "skill"], "fit-home-skill": ["home .agents skill", "skill"],
|
||||
"fit-user-prompt": ["user prompt template", "prompt template"], "fit-user-set-prompt": ["user settings prompt template", "prompt template"],
|
||||
"fit-user-pkg-prompt": ["user package prompt template", "prompt template"],
|
||||
"fit-project-prompt": ["project prompt template", "prompt template"], "fit-project-set-prompt": ["project settings prompt template", "prompt template"],
|
||||
"fit-project-pkg-prompt": ["project package prompt template", "prompt template"],
|
||||
};
|
||||
// Planted file (under the scratch directory), the word it holds, the source, the kind. A project
|
||||
// SYSTEM.md or APPEND_SYSTEM.md loads only in a trusted project: the saved trust makes it trusted,
|
||||
// and only --no-approve undoes that with the seal flags on.
|
||||
const TEXTS = [
|
||||
["home/.pi/agent/SYSTEM.md", "FITUSERSYSTEM", "user SYSTEM.md", "context file"],
|
||||
["home/.pi/agent/AGENTS.md", "FITUSERAGENTS", "user AGENTS.md", "context file"],
|
||||
["project/.pi/SYSTEM.md", "FITPROJECTSYSTEM", "project SYSTEM.md", "trusted project file"],
|
||||
["project/.pi/APPEND_SYSTEM.md", "FITPROJECTAPPEND", "project APPEND_SYSTEM.md", "trusted project file"],
|
||||
["project/AGENTS.md", "FITPROJECTAGENTS", "project AGENTS.md", "context file"],
|
||||
["project/CLAUDE.md", "FITPROJECTCLAUDE", "project CLAUDE.md", "context file"],
|
||||
["CLAUDE.md", "FITPARENTCLAUDE", "parent CLAUDE.md", "context file"],
|
||||
];
|
||||
// The --system-prompt file's word, which the sealed runs must show.
|
||||
const GIVEN = "FITGIVENPROMPT";
|
||||
// The -e extension every run passes, which must load, and the second one the session-shaped runs
|
||||
// pass after it.
|
||||
const EXPLICIT = "explicit extension";
|
||||
const SECOND = "second explicit extension";
|
||||
// The id of the session a -c run must resume.
|
||||
const PLANTED_SESSION = "fit-planted-session";
|
||||
const markerName = (source) => source.replace(/[^a-z]/g, "-");
|
||||
const marked = (scratch, source) => { try { return readdirSync(join(scratch, "marks")).includes(markerName(source)); } catch { return false; } };
|
||||
// What the -e extension saw at session_start ({ prompt, commands, session }), or null when it wrote
|
||||
// nothing that parses into that shape.
|
||||
function started(scratch) {
|
||||
let m;
|
||||
try { m = JSON.parse(readFileSync(join(scratch, "marks", "start.json"), "utf8")); } catch { return null; }
|
||||
if (!m || typeof m.prompt !== "string" || !Array.isArray(m.commands)) return null;
|
||||
return { prompt: m.prompt, commands: m.commands.filter((c) => typeof c === "string"), session: typeof m.session === "string" ? m.session : null };
|
||||
}
|
||||
// The planted skills and prompt templates among command names, as { source, kind }.
|
||||
const named = (names) => Object.entries(NAMED).filter(([name]) => names.some((c) => c === name || c.endsWith(`:${name}`))).map(([, [source, kind]]) => ({ source, kind }));
|
||||
|
||||
function plant(scratch) {
|
||||
const marks = join(scratch, "marks");
|
||||
const put = (path, text) => { mkdirSync(dirname(path), { recursive: true }); writeFileSync(path, text); };
|
||||
const ext = (source) => `import { writeFileSync } from "node:fs";\nexport default function (): void { writeFileSync(${JSON.stringify(join(marks, markerName(source)))}, ""); }\n`;
|
||||
const mcp = (source) => JSON.stringify({ mcpServers: { fit: { command: "/bin/sh", args: ["-c", `: > ${join(marks, markerName(source))}`] } } });
|
||||
const skill = (name) => `---\nname: ${name}\ndescription: a planted skill\n---\nA planted skill.\n`;
|
||||
const prompt = "A planted prompt template.\n";
|
||||
const agent = join(scratch, "home", ".pi", "agent");
|
||||
const project = join(scratch, "project");
|
||||
mkdirSync(marks, { recursive: true });
|
||||
mkdirSync(join(scratch, "tmp"), { recursive: true });
|
||||
mkdirSync(project, { recursive: true });
|
||||
// The session-shaped runs' --session-dir, made as the adapter makes it.
|
||||
mkdirSync(join(scratch, "sessions"), { recursive: true });
|
||||
// Each settings.json lists an extension, a skill directory, a prompt template and a local
|
||||
// package holding one of each.
|
||||
const listed = (who) => {
|
||||
const dir = join(scratch, "listed", who);
|
||||
put(join(dir, "fit.ts"), ext(`${who} settings extension`));
|
||||
put(join(dir, `fit-${who}-set-skill`, "SKILL.md"), skill(`fit-${who}-set-skill`));
|
||||
put(join(dir, `fit-${who}-set-prompt.md`), prompt);
|
||||
const pkg = join(dir, "package");
|
||||
put(join(pkg, "package.json"), JSON.stringify({ name: `fit-${who}-package`, version: "1.0.0" }));
|
||||
put(join(pkg, "extensions", "fit.ts"), ext(`${who} package extension`));
|
||||
put(join(pkg, "skills", `fit-${who}-pkg-skill`, "SKILL.md"), skill(`fit-${who}-pkg-skill`));
|
||||
put(join(pkg, "prompts", `fit-${who}-pkg-prompt.md`), prompt);
|
||||
return { extensions: [join(dir, "fit.ts")], skills: [join(dir, `fit-${who}-set-skill`)], prompts: [join(dir, `fit-${who}-set-prompt.md`)], packages: [pkg] };
|
||||
};
|
||||
// A saved trust for the project, so the control loads its trust-gated files without --approve.
|
||||
put(join(agent, "settings.json"), JSON.stringify({ ...listed("user"), defaultProjectTrust: "always" }));
|
||||
put(join(agent, "trust.json"), JSON.stringify({ [realpathSync(project)]: true }));
|
||||
put(join(agent, "extensions", "fit.ts"), ext("user extension"));
|
||||
put(join(agent, "mcp.json"), mcp("user mcp.json"));
|
||||
put(join(agent, "skills", "fit-user-skill", "SKILL.md"), skill("fit-user-skill"));
|
||||
put(join(agent, "prompts", "fit-user-prompt.md"), prompt);
|
||||
put(join(project, ".pi", "settings.json"), JSON.stringify(listed("project")));
|
||||
put(join(project, ".pi", "extensions", "fit.ts"), ext("project extension"));
|
||||
put(join(project, ".pi", "mcp.json"), mcp("project mcp.json"));
|
||||
put(join(project, ".mcp.json"), mcp("project .mcp.json"));
|
||||
put(join(project, ".pi", "skills", "fit-project-skill", "SKILL.md"), skill("fit-project-skill"));
|
||||
put(join(project, ".agents", "skills", "fit-agents-skill", "SKILL.md"), skill("fit-agents-skill"));
|
||||
put(join(project, ".pi", "prompts", "fit-project-prompt.md"), prompt);
|
||||
put(join(scratch, "home", ".agents", "skills", "fit-home-skill", "SKILL.md"), skill("fit-home-skill"));
|
||||
for (const [file, text] of TEXTS) put(join(scratch, file), `${text}\n`);
|
||||
put(join(scratch, "explicit", "fit.ts"), `import { writeFileSync } from "node:fs";\nexport default function (pi: any): void {\n writeFileSync(${JSON.stringify(join(marks, markerName(EXPLICIT)))}, "");\n pi.on("session_start", (_event: any, ctx: any) => { writeFileSync(${JSON.stringify(join(marks, "start.json"))}, JSON.stringify({ prompt: ctx.getSystemPrompt(), commands: pi.getCommands().map((c: any) => c.name), session: ctx.sessionManager.getSessionId() })); });\n}\n`);
|
||||
put(join(scratch, "explicit", "second.ts"), ext(SECOND));
|
||||
put(join(scratch, "prompt.md"), `${GIVEN}\n`);
|
||||
}
|
||||
|
||||
// A stored session for the project, as pi 0.85.1 writes one: a header whose cwd is the project's
|
||||
// real path (pi's -c takes only a session for its own cwd) and one user message.
|
||||
function plantSession(scratch) {
|
||||
const at = new Date().toISOString();
|
||||
writeFileSync(join(scratch, "sessions", `${PLANTED_SESSION}.jsonl`), [
|
||||
{ type: "session", version: 3, id: PLANTED_SESSION, timestamp: at, cwd: realpathSync(join(scratch, "project")) },
|
||||
{ type: "message", id: "f17e0001", parentId: null, timestamp: at, message: { role: "user", content: [{ type: "text", text: "an earlier turn" }], timestamp: Date.now() } },
|
||||
].map((e) => `${JSON.stringify(e)}\n`).join(""));
|
||||
}
|
||||
|
||||
// The planted sources a run loaded, as { source, kind }: markers present, planted names among the
|
||||
// command names, and planted words in the system prompt the -e extension saw.
|
||||
function seen(scratch, commands, start) {
|
||||
let marks = [];
|
||||
try { marks = readdirSync(join(scratch, "marks")); } catch { marks = []; }
|
||||
const out = Object.entries(MARKED).filter(([s]) => marks.includes(markerName(s))).map(([source, kind]) => ({ source, kind }));
|
||||
out.push(...named(commands));
|
||||
const text = start?.prompt ?? "";
|
||||
for (const [, word, source, kind] of TEXTS) if (text.includes(word)) out.push({ source, kind });
|
||||
return out;
|
||||
}
|
||||
|
||||
// sha256 over the sorted (path, sha256) list of the package's package.json, every regular file
|
||||
// under dist/bundle/ (design section 4), every regular file in the directory tree that holds the
|
||||
// binary's real file, so a bin outside dist/bundle/ can't import a file the digest skips, and npm's
|
||||
// lock files for a copy mounted with its node_modules. A symlink there is read through.
|
||||
function filesDigest(found) {
|
||||
const pkg = found.pkg;
|
||||
const bin = realpathSync(found.bin);
|
||||
const files = new Set([join(pkg, "package.json"), bin]);
|
||||
const walk = (dir) => {
|
||||
let entries;
|
||||
try { entries = readdirSync(dir, { withFileTypes: true }); } catch (e) { if (e.code === "ENOENT") return; throw e; }
|
||||
for (const e of entries) {
|
||||
const p = join(dir, e.name);
|
||||
if (e.isDirectory()) walk(p);
|
||||
else if (e.isFile() || (e.isSymbolicLink() && lstatSync(realpathSync(p)).isFile())) files.add(p);
|
||||
if (files.size > DIGEST_FILES_MAX) throw new Error("too many files");
|
||||
}
|
||||
};
|
||||
walk(join(pkg, "dist", "bundle"));
|
||||
const binDir = dirname(bin);
|
||||
if (binDir !== pkg && binDir.startsWith(`${pkg}/`)) walk(binDir);
|
||||
// A copy mounted with the node_modules it sits in imports packages beside it: npm's lock files
|
||||
// there stand for them.
|
||||
if (found.root !== pkg) for (const f of [join(found.root, ".package-lock.json"), join(dirname(found.root), "package-lock.json")]) {
|
||||
try { lstatSync(f); } catch (e) { if (e.code === "ENOENT") continue; throw e; }
|
||||
files.add(f);
|
||||
}
|
||||
const h = createHash("sha256");
|
||||
for (const f of [...files].sort()) {
|
||||
h.update(relative(pkg, f)).update("\0").update(createHash("sha256").update(readFileSync(f)).digest("hex")).update("\n");
|
||||
}
|
||||
return h.digest("hex");
|
||||
}
|
||||
@@ -41,7 +41,7 @@ import { createServer, connect } from "node:net";
|
||||
import { basename, join } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { Client } from "../../bus/src/client.mjs";
|
||||
import { LENGTH_FAILURE_NOTICE_PREFIX } from "./failure-notice.mjs";
|
||||
import { LENGTH_FAILURE_NOTICE_PREFIX, failureBody, secretValues, turnCause } from "./failure-notice.mjs";
|
||||
import { ID_PATTERN } from "../../organization/src/vocabulary.mjs";
|
||||
import { firstOutputMarker, firstTurnMarker } from './first-output.mjs';
|
||||
import { GUARD_CODES, FIXED_TEXTS } from "../../library/src/reply-protocol.mjs";
|
||||
@@ -508,7 +508,12 @@ function runTurn({ session, request, n, env, onChild }) {
|
||||
const missing = missingCommand(stderrTail(errPath));
|
||||
return resolve({ ok: false, reason: `adapter exited 127${missing ? ` (command not found: ${missing})` : ""}` });
|
||||
}
|
||||
if (code !== 0) return resolve({ ok: false, reason: signal ? `adapter killed by ${signal}` : `adapter exited ${code}` });
|
||||
if (signal) return resolve({ ok: false, reason: `adapter killed by ${signal}` });
|
||||
if (code !== 0) {
|
||||
// #1873 chat-fail F1: an adapter that ends on an error result prints it on stdout.
|
||||
const cause = turnCause(out, secretValues(env));
|
||||
return resolve({ ok: false, reason: `adapter exited ${code}`, ...(cause ? { cause } : {}) });
|
||||
}
|
||||
// A "marker" harness (row 65: the module's turnEnd()) writes the marker when its turn ends.
|
||||
if (session.turnEnd === "marker" && !existsSync(marker)) return resolve({ ok: false, reason: `${session.harness} exited before the turn ended (no turn marker)` });
|
||||
resolve({ ok: true, text: out.trim() });
|
||||
@@ -801,11 +806,11 @@ export async function main(runDir, { stdin = process.stdin, env = process.env, l
|
||||
catch (e) { log(`runner: failure record for ${m.id} failed: ${e.code ?? e.message}`); if (ended(e, false)) break loop; }
|
||||
continue;
|
||||
}
|
||||
if (!turn.ok) log(`runner: turn ${n} for message ${m.id} failed: ${turn.reason}`);
|
||||
if (!turn.ok) log(`runner: turn ${n} for message ${m.id} failed: ${turn.reason}${turn.cause ? ` (${turn.cause})` : ""}`);
|
||||
try {
|
||||
if (!isLengthFailureNotice(m) && m.class !== "RESULT" && (m.class === "REQUEST" || ["human", "tester", "user"].includes(m.from_role) || m.in_reply_to_class !== "RESULT")) {
|
||||
if (!library) {
|
||||
const body = turn.ok ? turn.text || "(the session gave no answer)" : `The turn failed: ${turn.reason}`;
|
||||
const body = turn.ok ? turn.text || "(the session gave no answer)" : failureBody(turn);
|
||||
await client.call("message.send", { to: m.from_role, body: clip(body) + unslop.note(), class: "RESULT", in_reply_to: m.id });
|
||||
unslop.sent();
|
||||
} else {
|
||||
@@ -858,7 +863,7 @@ export async function main(runDir, { stdin = process.stdin, env = process.env, l
|
||||
await client.call("message.read", { id: m.id });
|
||||
continue;
|
||||
}
|
||||
if (!retry.ok) log(`runner: retry turn ${n} for message ${m.id} failed: ${retry.reason}`);
|
||||
if (!retry.ok) log(`runner: retry turn ${n} for message ${m.id} failed: ${retry.reason}${retry.cause ? ` (${retry.cause})` : ""}`);
|
||||
try { await send(retry); }
|
||||
catch (again) {
|
||||
if (!GUARD_CODES.includes(again.code) && again.code !== "request-too-large") throw again;
|
||||
|
||||
@@ -4,7 +4,8 @@ import { createHash } from "node:crypto";
|
||||
import { mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { BundleError, buildBundle, sessionModel, SOURCES } from "../src/bundle.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
import { decide } from "../src/gate.mjs";
|
||||
import { resolveInstance, validateBusinessDocument } from "../../organization/src/index.mjs";
|
||||
import { businessDoc, REPO_ROLES, systemFor } from "../../organization/tests/helpers.mjs";
|
||||
|
||||
@@ -205,3 +205,14 @@ test('a library continuation that succeeds sends the cited RESULT once', async t
|
||||
assert.equal(f.count(),2);
|
||||
assert.equal(f.store.get("SELECT count(*) n FROM messages WHERE from_role='coder'").n,1);
|
||||
});
|
||||
|
||||
// #1873 chat-fail review round 1: a failed citation retry logs its plain cause, as the first turn does.
|
||||
test('a failed citation retry logs the plain cause of the failure', async t => {
|
||||
let sends = 0;
|
||||
const f = await setup(t, commit => [{ text: citedBody([fixtureCite(commit)]) }, { text: 'Claude AI usage limit reached', exitCode: 1 }], {
|
||||
guard: async () => { if (sends++ === 0) throw new GuardError('citation-unavailable', 'The library is busy.'); return null; },
|
||||
});
|
||||
await f.call(f.human, 'message.send', { to: 'coder', body: 'Fixture question.', class: 'REQUEST' });
|
||||
await until(() => /runner: retry turn \d+ for message \S+ failed: /.test(f.log()));
|
||||
assert.match(f.log(), /runner: retry turn \d+ for message \S+ failed: adapter exited 1 \(the model account is out of usage/);
|
||||
});
|
||||
@@ -0,0 +1,142 @@
|
||||
import { fixtureCacheEnv } from '../../../scripts/testing/lane-fixtures.mjs';
|
||||
// #1873 harness-matrix: claude-code runs a proxy model that is not a Claude model. The host's claude
|
||||
// CLI runs through adapters/claude with the bundle and env a z.ai or codex selection gives, against a
|
||||
// local stand-in for the proxy's Anthropic-format route. The stand-in records the path, the model
|
||||
// name claude sends and the hash of the key it carries. Scratch CLAUDE_CONFIG_DIR and HOME, a fixture
|
||||
// key, nonessential traffic off: nothing reaches a real model or account. Whether the real proxy
|
||||
// turns that request into a z.ai or codex call is proved only on the dev instance.
|
||||
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { createServer as createHttp } from 'node:http';
|
||||
import { spawn, spawnSync } from 'node:child_process';
|
||||
import { mkdirSync, readFileSync } from 'node:fs';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { buildBundle, checkedTransport } from '../src/bundle.mjs';
|
||||
import { adapterEnv } from '../src/runner.mjs';
|
||||
import { fakeToolSocket, HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from './helpers.mjs';
|
||||
import * as cliproxy from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
|
||||
const ADAPTER = join(REPO, 'adapters', 'claude', 'adapter.sh');
|
||||
const which = spawnSync('sh', ['-c', 'command -v claude'], { encoding: 'utf8' });
|
||||
const CLAUDE = which.status === 0 ? which.stdout.trim() : null;
|
||||
const skip = CLAUDE ? false : 'claude is not on PATH';
|
||||
const sha = (s) => createHash('sha256').update(s).digest('hex');
|
||||
|
||||
// The proxy's Anthropic-format route, as a stand-in: every /v1/messages answers with one text block.
|
||||
async function standIn(t, text) {
|
||||
const seen = [];
|
||||
const server = createHttp((req, res) => {
|
||||
let raw = '';
|
||||
req.on('data', (c) => (raw += c));
|
||||
req.on('end', () => {
|
||||
const auth = String(req.headers.authorization ?? '').replace(/^Bearer /, '');
|
||||
const apiKey = String(req.headers['x-api-key'] ?? '');
|
||||
if (req.method !== 'POST' || !/^\/v1\/messages(\?|$)/.test(req.url)) {
|
||||
seen.push({ path: req.url, method: req.method, authSha: auth ? sha(auth) : null, apiKeySha: apiKey ? sha(apiKey) : null });
|
||||
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: 'stand-in' } }));
|
||||
}
|
||||
const body = JSON.parse(raw || '{}');
|
||||
seen.push({ path: req.url.split('?')[0], method: req.method, model: body.model, authSha: auth ? sha(auth) : null, apiKeySha: apiKey ? sha(apiKey) : null });
|
||||
const send = (event, data) => res.write(`event: ${event}\ndata: ${JSON.stringify({ type: event, ...data })}\n\n`);
|
||||
if (!body.stream) {
|
||||
res.writeHead(200, { 'content-type': 'application/json' });
|
||||
return res.end(JSON.stringify({ id: 'msg_si', type: 'message', role: 'assistant', model: body.model, content: [{ type: 'text', text }], stop_reason: 'end_turn', stop_sequence: null, usage: { input_tokens: 10, output_tokens: 5 } }));
|
||||
}
|
||||
res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-cache' });
|
||||
send('message_start', { message: { id: 'msg_si', 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: { type: 'text', text: '' } });
|
||||
send('content_block_delta', { index: 0, delta: { type: 'text_delta', text } });
|
||||
send('content_block_stop', { index: 0 });
|
||||
send('message_delta', { delta: { stop_reason: 'end_turn', stop_sequence: null }, usage: { output_tokens: 5 } });
|
||||
send('message_stop', {});
|
||||
res.end();
|
||||
});
|
||||
});
|
||||
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}`, seen };
|
||||
}
|
||||
|
||||
function turn(env, request, cwd) {
|
||||
return new Promise((resolve) => {
|
||||
const child = spawn('/bin/sh', [ADAPTER], { cwd, env: { ...env, ...fixtureCacheEnv(env.CLAUDE_CONFIG_DIR), MOSAIC_REQUEST: request }, stdio: ['ignore', 'pipe', 'pipe'], detached: true });
|
||||
let stdout = '', stderr = '';
|
||||
child.stdout.on('data', (b) => (stdout += b));
|
||||
child.stderr.on('data', (b) => (stderr += b));
|
||||
const clock = setTimeout(() => process.kill(-child.pid, 'SIGKILL'), 120_000);
|
||||
child.on('close', (code) => { clearTimeout(clock); resolve({ code, stdout, stderr }); });
|
||||
});
|
||||
}
|
||||
|
||||
const SELECTIONS = {
|
||||
zai: { provider: 'zai', model: 'glm-fixture', module: 'cliproxy', upstream: 'zai' },
|
||||
codex: { provider: 'codex', model: 'gpt-fixture', module: 'cliproxy', upstream: 'codex' },
|
||||
};
|
||||
|
||||
// The route's api and endpoint describe pi's wire. claude-code ignores them: its env is the proxy
|
||||
// root and the key, and it always posts Anthropic Messages there, whatever the upstream.
|
||||
test('#1873 the catalogue offers a z.ai and a codex model to claude-code and pi, and the route admits both harnesses', async () => {
|
||||
const c = await cliproxy.catalogue({
|
||||
listAccounts: async () => ({ files: [{ provider: 'codex', name: 'codex-fixture.json', status: 'active' }] }),
|
||||
listModels: async () => ({ models: [{ id: 'gpt-fixture' }] }),
|
||||
listGroups: async () => [{ 'base-url': cliproxy.ZAI_URL, keys: [{}], models: [{ name: 'glm-fixture' }] }],
|
||||
now: () => 1,
|
||||
});
|
||||
assert.equal(c.status, 'ready');
|
||||
for (const [name, entry] of Object.entries(SELECTIONS)) {
|
||||
const row = c.models.find((m) => m.upstream === entry.upstream && m.model === entry.model);
|
||||
assert.deepEqual(row?.harnesses, ['claude-code', 'pi'], name);
|
||||
for (const harness of ['claude-code', 'pi']) {
|
||||
assert.deepEqual(cliproxy.preflight({ selection: entry, harness, catalogue: c }), { allow: true }, `${name} ${harness}`);
|
||||
assert.equal(checkedTransport(entry, harness).piProvider, `mosaic-proxy-${entry.upstream === 'zai' ? 'glm' : 'codex'}`, `${name} ${harness}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
for (const [name, entry] of Object.entries(SELECTIONS)) {
|
||||
test(`#1873 claude-code sends the ${name} model name to the proxy's Anthropic-format route with the session key`, { skip }, async (t) => {
|
||||
const dir = scratch(t, 'mosaic-claude-proxy-');
|
||||
const workspace = join(dir, 'ws'); mkdirSync(workspace);
|
||||
const runDir = join(dir, 'run'); mkdirSync(runDir, { mode: 0o700 });
|
||||
const api = await standIn(t, `stand-in answer for ${entry.model}`);
|
||||
const key = `fx1873-claude-${name}`;
|
||||
const resolved = resolvedFor(t, 'coder', (d) => {
|
||||
d.launch.models.proxied = entry;
|
||||
d.roles.coder.vars = { harness: 'claude-code', model: 'proxied', thinking: 'low', 'limits.tools': ['read'] };
|
||||
});
|
||||
const sock = await fakeToolSocket(t, dir, (tool, args) => ({ tool, args }));
|
||||
const session = { workspace, sessionDir: join(dir, 'session'), toolSocket: sock.path, turnMarker: join(dir, 'turn.done'), runDir };
|
||||
const bundle = buildBundle({
|
||||
dir: join(runDir, 'bundle'), resolved, models: { proxied: entry }, contract: '# Coder\n',
|
||||
harness: HARNESS['claude-code'], skillsDir: REPO_SKILLS, proxy: { baseUrl: api.url, key },
|
||||
session: { ...session, run: 'r1873', launchedBy: 'pm' },
|
||||
});
|
||||
// The proxy's names come from the harness module's env, and nothing on disk holds the key.
|
||||
assert.equal(bundle.env.ANTHROPIC_BASE_URL, api.url);
|
||||
assert.equal(bundle.env.ANTHROPIC_AUTH_TOKEN, key);
|
||||
assert.equal(Object.hasOwn(bundle.env, 'ANTHROPIC_API_KEY'), false);
|
||||
for (const file of Object.values(bundle.files)) if (typeof file === 'string') {
|
||||
let text = ''; try { text = readFileSync(file, 'utf8'); } catch {}
|
||||
assert.equal(text.includes(key), false, file);
|
||||
}
|
||||
const s = { ...session, harness: 'claude-code', provider: entry.provider, model: entry.model, bundle: bundle.files, harnessEnv: bundle.env };
|
||||
const env = adapterEnv(s, {
|
||||
PATH: `${dirname(CLAUDE)}:${dirname(process.execPath)}:/usr/bin:/bin`,
|
||||
HOME: join(dir, 'home'), CLAUDE_CONFIG_DIR: join(dir, 'claude-config'),
|
||||
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1', DISABLE_TELEMETRY: '1', DISABLE_AUTOUPDATER: '1', DISABLE_ERROR_REPORTING: '1',
|
||||
});
|
||||
const r = await turn(env, 'Message 1 from pm, class ASSIGNMENT:\n\nsay hello', workspace);
|
||||
assert.equal(r.code, 0, r.stderr.replaceAll(key, '<key>'));
|
||||
assert.match(r.stdout, new RegExp(`stand-in answer for ${entry.model}`));
|
||||
const messages = api.seen.filter((x) => x.path === '/v1/messages');
|
||||
assert.ok(messages.length >= 1, JSON.stringify(api.seen));
|
||||
// The main turn names the selected model. Claude Code may add its own small background
|
||||
// requests; every request carries the session key as a bearer token and none uses x-api-key.
|
||||
assert.ok(messages.some((x) => x.model === entry.model), JSON.stringify(messages.map((x) => x.model)));
|
||||
for (const x of api.seen) { assert.equal(x.authSha, sha(key), x.path); assert.equal(x.apiKeySha, null, x.path); }
|
||||
assert.equal(r.stdout.includes(key) || r.stderr.includes(key), false);
|
||||
});
|
||||
}
|
||||
@@ -12,7 +12,8 @@ import { join, relative } from "node:path";
|
||||
import { buildBundle, PERSONA_CHARS } from "../src/bundle.mjs";
|
||||
import { decide } from "../src/gate.mjs";
|
||||
import { adapterEnv } from "../src/runner.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
|
||||
const sha = (data) => createHash("sha256").update(data).digest("hex");
|
||||
const CREATIVE = ["companion", "ideas", "research", "proofreader", "fact-checker", "continuity"];
|
||||
@@ -177,7 +178,8 @@ function runAdapter(t, harness, b, { prompt = b.files.prompt, cwd } = {}) {
|
||||
writeFileSync(cli, '#!/bin/sh\nprintf "%s\\0" "$@" > "$ARGV_OUT"\necho \'{"type":"result","subtype":"success","result":"ok"}\'\n');
|
||||
chmodSync(cli, 0o755);
|
||||
const s = { harness, provider: harness === "pi" ? "probe" : null, model: "claude-sonnet-5-5", workspace: b.workspace, sessionDir: join(b.dir, "session"), toolSocket: join(b.dir, "tools.sock"), turnMarker: join(b.dir, "turn.done"), bundle: { ...b.files, prompt }, harnessEnv: b.env };
|
||||
const env = { ...adapterEnv(s, { PATH: `${bin}:/usr/bin:/bin`, HOME: join(b.dir, "home") }), ARGV_OUT: join(b.dir, "argv"), MOSAIC_REQUEST: "x" };
|
||||
// #1873 S2: the pi adapter runs MOSAIC_PI_BIN, which the bundle sets to the pi it found.
|
||||
const env = { ...adapterEnv(s, { PATH: `${bin}:/usr/bin:/bin`, HOME: join(b.dir, "home") }), ARGV_OUT: join(b.dir, "argv"), MOSAIC_REQUEST: "x", ...(harness === "pi" ? { MOSAIC_PI_BIN: cli } : {}) };
|
||||
const r = spawnSync("/bin/sh", [join(REPO, "adapters", harness === "pi" ? "pi" : "claude", "adapter.sh")], { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], env });
|
||||
const argv = existsSync(env.ARGV_OUT) ? readFileSync(env.ARGV_OUT, "utf8").split("\0") : null;
|
||||
return { ...r, argv };
|
||||
|
||||
@@ -31,6 +31,12 @@ async function harnessModule(name) {
|
||||
return loader.modules("harness")[0];
|
||||
}
|
||||
export const HARNESS = Object.freeze({ pi: await harnessModule("pi"), "claude-code": await harnessModule("claude-code") });
|
||||
// #1873 re-check 7: a module whose fit check runs async has ready(), which the launcher's preflight
|
||||
// stands for. A test that calls buildBundle directly awaits this first. It is not run on import:
|
||||
// request-agent.mjs imports this file in every tool-call process, which builds no bundle.
|
||||
export async function harnessReady() {
|
||||
for (const mod of Object.values(HARNESS)) if (typeof mod.ready === "function") await mod.ready();
|
||||
}
|
||||
|
||||
// A session's route is its own three vars (row 65); a role the test doesn't route runs pi on the
|
||||
// test system's old execution.model at thinking high, so the bundle tests keep their models.
|
||||
|
||||
@@ -6,7 +6,8 @@ import { join } from 'node:path';
|
||||
import { typedTools } from '../src/tools.mjs';
|
||||
import { buildBundle } from '../src/bundle.mjs';
|
||||
import { TYPES } from '../../library/src/mapping.mjs';
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch } from './helpers.mjs';
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch, harnessReady } from './helpers.mjs';
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
|
||||
const LOOKUPS = ['library_find', 'library_get', 'library_search', 'library_manuscript', 'library_universe'];
|
||||
const CREATIVE = ['companion', 'research', 'ideas', 'proofreader', 'fact-checker', 'continuity'];
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -10,7 +10,8 @@ import { join } from "node:path";
|
||||
import { buildBundle } from "../src/bundle.mjs";
|
||||
import { adapterEnv, unslopCounts, unslopLog, unslopNotes } from "../src/runner.mjs";
|
||||
import { REPLY_CAP } from "../src/tools.mjs";
|
||||
import { fakeToolSocket, HARNESS, mockAnthropic, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
|
||||
import { fakeToolSocket, HARNESS, mockAnthropic, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
|
||||
const ADAPTER = join(REPO, "adapters", "pi", "adapter.sh");
|
||||
|
||||
|
||||
@@ -0,0 +1,241 @@
|
||||
// #1873 pi-unpin S3: a stand-in pi that the fit check (src/pi-fit.mjs) can run. It is one sh
|
||||
// script with its knobs written into it, because the fit check runs it with an empty
|
||||
// environment. Put it at dist/bundle/cli.js in a package named @earendil-works/pi-coding-agent.
|
||||
//
|
||||
// What it does, in order:
|
||||
// --version prints `version` (default 9.9.9), or exits `versionExit` when that is set, or
|
||||
// hangs for a minute with `versionHang`
|
||||
// $ARGV_OUT set: a session. Writes its argv there, prints `sessionOut`, exits 0. This part is
|
||||
// sh, so it runs in a sandbox that holds no node.
|
||||
// otherwise runs node (the absolute path, since the fit check gives it no PATH) on the rest:
|
||||
// --help lists every flag the fit check wants, less `dropFlags`, plus `extraFlags`, then
|
||||
// the `helpProse` lines as they are
|
||||
// -e <file> a file that doesn't exist stops it with exit 1, unless `missingExtOk` is true or
|
||||
// names the mode ("rpc" or "print") it starts in anyway
|
||||
// --mode rpc loads the planted sources the way pi 0.85.1 finds them, honouring the seal flags,
|
||||
// or -p runs each -e extension's session_start (one that writes JSON.stringify(...) gets
|
||||
// the prompt this stand-in builds, the command names and the session id), then
|
||||
// answers get_state and get_commands
|
||||
// (rpc), or prints `sessionOut` and exits 0 (-p).
|
||||
// An -e file that exists loads even with --no-extensions, unless `dropExplicit`
|
||||
// (then it loads only without --no-extensions). `printNoStart`: -p runs no
|
||||
// session_start. MCP servers stay out with --no-mcp when `extraFlags` lists it, else
|
||||
// with --no-extensions.
|
||||
// `ignoreAll` loads no planted source. `sealLeak` lists kinds (extensions, mcp,
|
||||
// skills, prompts, context, approve) loaded even when their seal flag is on.
|
||||
// `ignoreSystemPrompt` drops the --system-prompt file. `ignoreAppend` drops every
|
||||
// APPEND_SYSTEM.md. `sysReplacesAll`: a --system-prompt file also drops every
|
||||
// APPEND_SYSTEM.md and context file. `trustExtOnly`: the only extensions are the
|
||||
// project's .pi/extensions, loaded in a trusted project whatever --no-extensions
|
||||
// says. `trustCtxOnly`: the only context file is the project's AGENTS.md, loaded the
|
||||
// same way whatever --no-context-files says. `flood`: prints 8 MB first.
|
||||
// `hideWhen`: a cue, a flag or "order" (--offline before --no-session). With the cue
|
||||
// it loads no planted source; without it, every kind even with its seal flag on.
|
||||
// `standIn`: [flag, other]: it ignores flag, and other keeps flag's kind out instead.
|
||||
// `remember`: the first run in a scratch directory (it leaves a marker beside the
|
||||
// -e file's directory) loads every kind whatever the seal flags; a later run loads
|
||||
// none. `needs`: a flag the loader flags work only with; without it they do
|
||||
// nothing. `leakWithout`: flags; when any of them is absent, every kind loads.
|
||||
// `leakWith`: flags; when all of them are given, every kind loads (with
|
||||
// `leakWithout` too, both must hold). `mcpAlso`: a flag that keeps MCP servers out
|
||||
// as well as their own flag does. `leakNoAgentDir`: every kind loads when
|
||||
// PI_CODING_AGENT_DIR is unset, as in a bus session. `leakNoVar`: names; every
|
||||
// kind loads when any of them is unset (TERM, TZ, LC_ALL in a unit-started bus).
|
||||
// `leakNoMatch`: with -c, every
|
||||
// kind loads when no .jsonl in --session-dir names the working directory.
|
||||
// `leakEnv`: { json, other }, the variable names a run with --mode json and any
|
||||
// other run should get; every kind loads when the environment holds a name not in
|
||||
// the list or lacks one, apart from the names /bin/sh sets (PWD, OLDPWD, SHLVL, _).
|
||||
// An -e extension that writes JSON.stringify({...}) at session_start gets the
|
||||
// prompt, the command names, the session id (with -c, the id in the header of a
|
||||
// .jsonl in --session-dir whose cwd is the working directory, else a new one) and
|
||||
// the --thinking level (else "medium"). `noResume`: -c always starts a new
|
||||
// session. `oneExt`: only the first -e loads. `extNoCommands`: the extension sees
|
||||
// no commands.
|
||||
// `commands` "fail" answers get_commands with success false; "list" answers
|
||||
// with a bare list.
|
||||
// otherwise a session with no $ARGV_OUT: prints `sessionOut` and exits 0.
|
||||
//
|
||||
// Trust, as pi 0.85.1 has it: the project is trusted with --approve, or with a saved trust and
|
||||
// no --no-approve. Only the project SYSTEM.md and APPEND_SYSTEM.md wait on it here. The user
|
||||
// APPEND_SYSTEM.md is appended whatever the flags, as 0.85.1 does when the project has none.
|
||||
import { FIT_FLAGS } from "../src/pi-fit.mjs";
|
||||
|
||||
const BODY = String.raw`
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const cp = require("node:child_process");
|
||||
const argv = process.argv.slice(1);
|
||||
const has = (f) => argv.includes(f);
|
||||
const val = (f) => { const i = argv.indexOf(f); return i === -1 ? undefined : argv[i + 1]; };
|
||||
if (argv[0] === "--help") {
|
||||
const drop = new Set(K.dropFlags || []);
|
||||
const lines = ["Usage: pi [options] [messages...]", "", "Options:"];
|
||||
for (const f of FLAGS) {
|
||||
if (drop.has(f)) continue;
|
||||
if (f === "--mode") lines.push(" --mode <mode> Output mode: " + (K.modes || ["text", "json", "rpc"]).join(", "));
|
||||
else if (f === "--thinking") lines.push(" --thinking <level> Thinking level: " + (K.levels || ["off", "minimal", "low", "medium", "high", "xhigh", "max"]).join(", "));
|
||||
else lines.push(" " + f);
|
||||
}
|
||||
for (const f of K.extraFlags || []) lines.push(" " + f);
|
||||
for (const l of K.helpProse || []) lines.push(l);
|
||||
process.stdout.write(lines.join("\n") + "\n");
|
||||
process.exit(0);
|
||||
}
|
||||
const rpc = val("--mode") === "rpc";
|
||||
const print = !rpc && has("-p");
|
||||
const startsAnyway = K.missingExtOk === true || (rpc && K.missingExtOk === "rpc") || (print && K.missingExtOk === "print");
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
if (argv[i] === "-e" && !fs.existsSync(argv[i + 1]) && !startsAnyway) {
|
||||
process.stderr.write("Failed to load extension: it does not exist\n");
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
if (rpc || print) {
|
||||
if (K.flood) {
|
||||
const chunk = Buffer.alloc(1024 * 1024, "x");
|
||||
for (let n = 0; n < 8; n++) for (let off = 0; off < chunk.length;) {
|
||||
try { off += fs.writeSync(1, chunk, off); } catch (e) { if (e.code !== "EAGAIN") throw e; }
|
||||
}
|
||||
}
|
||||
const ALL = ["extensions", "mcp", "skills", "prompts", "context", "approve"];
|
||||
// The id in the header of the last .jsonl in dir whose cwd is the working directory, or null.
|
||||
const storedFor = (dir) => {
|
||||
let id = null;
|
||||
if (dir) for (const n of (() => { try { return fs.readdirSync(dir); } catch { return []; } })()) {
|
||||
if (!n.endsWith(".jsonl")) continue;
|
||||
let h = null;
|
||||
try { h = JSON.parse(fs.readFileSync(path.join(dir, n), "utf8").split("\n")[0]); } catch { h = null; }
|
||||
if (h && h.type === "session" && h.cwd === process.cwd()) id = h.id;
|
||||
}
|
||||
return id;
|
||||
};
|
||||
let ignoreAll = K.ignoreAll;
|
||||
let leak = new Set(K.sealLeak || []);
|
||||
if (K.hideWhen) {
|
||||
const cued = K.hideWhen === "order" ? argv.indexOf("--offline") !== -1 && argv.indexOf("--offline") < argv.indexOf("--no-session") : has(K.hideWhen);
|
||||
if (cued) ignoreAll = true;
|
||||
else leak = new Set(ALL);
|
||||
}
|
||||
if (K.remember) {
|
||||
const marker = path.join(path.dirname(path.dirname(val("-e"))), "seen-before");
|
||||
if (fs.existsSync(marker)) ignoreAll = true;
|
||||
else { fs.writeFileSync(marker, ""); leak = new Set(ALL); }
|
||||
}
|
||||
if ((K.leakWith || K.leakWithout) && (K.leakWith || []).every(has) && (!K.leakWithout || K.leakWithout.some((f) => !has(f)))) leak = new Set(ALL);
|
||||
if (K.leakNoAgentDir && !process.env.PI_CODING_AGENT_DIR) leak = new Set(ALL);
|
||||
if (K.leakNoVar && K.leakNoVar.some((n) => process.env[n] === undefined)) leak = new Set(ALL);
|
||||
if (K.leakNoMatch && has("-c") && !storedFor(val("--session-dir"))) leak = new Set(ALL);
|
||||
if (K.leakEnv) {
|
||||
const want = new Set(K.leakEnv[val("--mode") === "json" ? "json" : "other"]);
|
||||
const got = Object.keys(process.env).filter((n) => !["PWD", "OLDPWD", "SHLVL", "_"].includes(n));
|
||||
if (got.length !== want.size || got.some((n) => !want.has(n))) leak = new Set(ALL);
|
||||
}
|
||||
// Whether a seal flag is on: with standIn, its flag reads as the other flag; with needs, only
|
||||
// when that flag is given too.
|
||||
const sealed = (flag) => has(K.standIn && K.standIn[0] === flag ? K.standIn[1] : flag) && (!K.needs || has(K.needs));
|
||||
const on = (kind, flag) => !ignoreAll && (!sealed(flag) || leak.has(kind));
|
||||
const agent = process.env.PI_CODING_AGENT_DIR || path.join(process.env.HOME || "/", ".pi", "agent");
|
||||
const cwd = process.cwd();
|
||||
const list = (dir, test) => { try { return fs.readdirSync(dir).filter(test).map((n) => path.join(dir, n)); } catch { return []; } };
|
||||
const json = (file) => { try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return null; } };
|
||||
const read = (file) => { try { return fs.readFileSync(file, "utf8"); } catch { return null; } };
|
||||
const settings = [json(path.join(agent, "settings.json")), json(path.join(cwd, ".pi", "settings.json"))].filter(Boolean);
|
||||
const trust = json(path.join(agent, "trust.json")) || {};
|
||||
const saved = settings.some((s) => s.defaultProjectTrust === "always") || trust[cwd] === true;
|
||||
const trusted = !ignoreAll && (has("--approve") || (saved && (!sealed("--no-approve") || leak.has("approve"))));
|
||||
const commands = [];
|
||||
// A planted extension writes one marker on load; an -e extension may also write what it saw at
|
||||
// session_start. Do what each would do.
|
||||
const starts = [];
|
||||
const load = (f) => {
|
||||
const text = read(f);
|
||||
if (text === null) return;
|
||||
for (const m of text.matchAll(/writeFileSync\(("(?:[^"\\]|\\.)*")(, JSON\.stringify\()?/g)) {
|
||||
if (m[2]) starts.push(JSON.parse(m[1]));
|
||||
else fs.writeFileSync(JSON.parse(m[1]), "");
|
||||
}
|
||||
};
|
||||
const given = argv.flatMap((a, i) => a === "-e" ? [argv[i + 1]] : []);
|
||||
if (!(K.dropExplicit && has("--no-extensions"))) for (const f of K.oneExt ? given.slice(0, 1) : given) load(f);
|
||||
const packages = settings.flatMap((s) => (s.packages || []).map((p) => typeof p === "string" ? p : p.source));
|
||||
if (K.trustExtOnly) {
|
||||
if (trusted) for (const f of list(path.join(cwd, ".pi", "extensions"), (n) => n.endsWith(".ts"))) load(f);
|
||||
} else if (on("extensions", "--no-extensions")) {
|
||||
const files = [...list(path.join(agent, "extensions"), (n) => n.endsWith(".ts")), ...list(path.join(cwd, ".pi", "extensions"), (n) => n.endsWith(".ts"))];
|
||||
for (const s of settings) files.push(...(s.extensions || []));
|
||||
for (const p of packages) files.push(...list(path.join(p, "extensions"), (n) => n.endsWith(".ts")));
|
||||
for (const f of files) load(f);
|
||||
}
|
||||
const mcpFlag = (K.extraFlags || []).includes("--no-mcp") ? "--no-mcp" : "--no-extensions";
|
||||
if (!ignoreAll && (!(sealed(mcpFlag) || (K.mcpAlso && has(K.mcpAlso))) || leak.has("mcp"))) {
|
||||
for (const f of [path.join(agent, "mcp.json"), path.join(cwd, ".pi", "mcp.json"), path.join(cwd, ".mcp.json")]) {
|
||||
for (const s of Object.values(json(f)?.mcpServers || {})) cp.spawnSync(s.command, s.args || [], { stdio: "ignore" });
|
||||
}
|
||||
}
|
||||
if (on("skills", "--no-skills")) {
|
||||
const home = process.env.HOME || "/";
|
||||
const dirs = [path.join(agent, "skills"), path.join(cwd, ".pi", "skills"), path.join(cwd, ".agents", "skills"), path.join(home, ".agents", "skills"), ...packages.map((p) => path.join(p, "skills"))];
|
||||
for (const d of dirs) for (const p of list(d, () => true)) commands.push({ name: "skill:" + path.basename(p), source: "skill" });
|
||||
for (const s of settings) for (const p of s.skills || []) commands.push({ name: "skill:" + path.basename(p), source: "skill" });
|
||||
}
|
||||
if (on("prompts", "--no-prompt-templates")) {
|
||||
const dirs = [path.join(agent, "prompts"), path.join(cwd, ".pi", "prompts"), ...packages.map((p) => path.join(p, "prompts"))];
|
||||
for (const d of dirs) for (const p of list(d, (n) => n.endsWith(".md"))) commands.push({ name: path.basename(p, ".md"), source: "prompt" });
|
||||
for (const s of settings) for (const p of s.prompts || []) commands.push({ name: path.basename(p, ".md"), source: "prompt" });
|
||||
}
|
||||
// The system prompt: the --system-prompt file, else SYSTEM.md; then APPEND_SYSTEM.md; then the
|
||||
// context files, from the agent directory and from / down to the working directory.
|
||||
const sys = K.ignoreSystemPrompt ? null : val("--system-prompt");
|
||||
const own = (name) => (trusted && read(path.join(cwd, ".pi", name))) || (!ignoreAll && read(path.join(agent, name))) || "";
|
||||
const hide = K.sysReplacesAll && sys;
|
||||
const parts = [sys ? (read(sys) || "") : own("SYSTEM.md"), K.ignoreAppend || hide ? "" : own("APPEND_SYSTEM.md")];
|
||||
if (K.trustCtxOnly) {
|
||||
if (trusted) parts.push(read(path.join(cwd, "AGENTS.md")) || "");
|
||||
} else if (!hide && on("context", "--no-context-files")) {
|
||||
const dirs = [];
|
||||
for (let d = cwd; ; d = path.dirname(d)) { dirs.unshift(d); if (path.dirname(d) === d) break; }
|
||||
parts.push(read(path.join(agent, "AGENTS.md")) || "");
|
||||
for (const d of dirs) parts.push(read(path.join(d, "AGENTS.md")) || read(path.join(d, "CLAUDE.md")) || "");
|
||||
}
|
||||
// The session -c resumes: the newest .jsonl in --session-dir whose header names this cwd.
|
||||
let session = "stand-in-" + process.pid;
|
||||
const dir = val("--session-dir");
|
||||
if (has("-c") && dir && !K.noResume) session = storedFor(dir) || session;
|
||||
const start = { prompt: parts.join("\n"), commands: K.extNoCommands ? [] : commands.map((c) => c.name), session };
|
||||
if (!(print && K.printNoStart)) for (const f of starts) fs.writeFileSync(f, JSON.stringify(start));
|
||||
if (print) {
|
||||
if (K.sessionOut) process.stdout.write(K.sessionOut);
|
||||
process.exit(0);
|
||||
}
|
||||
let input = "";
|
||||
try { input = fs.readFileSync(0, "utf8"); } catch { input = ""; }
|
||||
for (const line of input.split("\n")) {
|
||||
let m;
|
||||
try { m = JSON.parse(line); } catch { continue; }
|
||||
if (m.type === "get_state") process.stdout.write(JSON.stringify({ id: m.id, type: "response", command: "get_state", success: true, data: { isStreaming: false } }) + "\n");
|
||||
if (m.type === "get_commands") {
|
||||
const answer = K.commands === "fail" ? { success: false, error: "no" } : { success: true, data: K.commands === "list" ? commands : { commands } };
|
||||
process.stdout.write(JSON.stringify({ id: m.id, type: "response", command: "get_commands", ...answer }) + "\n");
|
||||
}
|
||||
}
|
||||
process.exit(0);
|
||||
}
|
||||
if (K.sessionOut) process.stdout.write(K.sessionOut);
|
||||
process.exit(0);
|
||||
`;
|
||||
|
||||
const quote = (text) => `'${String(text).replace(/'/g, "'\\''")}'`;
|
||||
|
||||
// The script text. node: the interpreter the fit part runs on (absolute, so it runs with no PATH).
|
||||
export function piStandIn(knobs = {}, { node = process.execPath } = {}) {
|
||||
const version = knobs.versionHang ? "sleep 60; exit 0" : knobs.versionExit != null ? `exit ${Number(knobs.versionExit)}` : `printf '%s\\n' ${quote(knobs.version ?? "9.9.9")}; exit 0`;
|
||||
const program = `"use strict";\nconst K = ${JSON.stringify(knobs)};\nconst FLAGS = ${JSON.stringify(FIT_FLAGS)};\n${BODY}`;
|
||||
return [
|
||||
"#!/bin/sh",
|
||||
`[ "$1" = --version ] && { ${version}; }`,
|
||||
`if [ -n "\${ARGV_OUT:-}" ]; then printf '%s\\n' "$@" > "$ARGV_OUT"; printf '%s' ${quote(knobs.sessionOut ?? "")}; exit 0; fi`,
|
||||
`exec ${quote(node)} -e ${quote(program)} -- "$@"`,
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
@@ -4,12 +4,14 @@ import assert from 'node:assert/strict';
|
||||
import { mkdirSync, readFileSync, statSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { buildBundle } from '../src/bundle.mjs';
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch } from './helpers.mjs';
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch, harnessReady } from './helpers.mjs';
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
|
||||
const models = {
|
||||
a: { provider: 'proxy', model: 'claude-fixture', route: 'anthropic' },
|
||||
z: { provider: 'proxy', model: 'glm-fixture', route: 'glm' },
|
||||
g: { provider: 'proxy', model: 'gpt-fixture', route: 'codex' },
|
||||
o: { provider: 'other', model: 'fixture-other', module: 'cliproxy', upstream: 'other' },
|
||||
};
|
||||
function build(t, selection, proxy, declared) {
|
||||
const root = scratch(t, 'p332-'), runDir = join(root, 'run');
|
||||
@@ -32,14 +34,22 @@ function assertSafe(value) {
|
||||
else if (value && typeof value === 'object') for (const item of Object.values(value)) assertSafe(item);
|
||||
}
|
||||
|
||||
test('P3 Anthropic route uses messages at proxy root and the declared model id', t => {
|
||||
const b = build(t, 'a', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, { a: models.a });
|
||||
const { anthropic } = JSON.parse(b.text).providers;
|
||||
assert.equal(anthropic.api, 'anthropic-messages');
|
||||
assert.equal(anthropic.baseUrl, 'http://127.0.0.1:12345');
|
||||
assert.deepEqual(anthropic.models.map(m => m.id), ['claude-fixture']);
|
||||
assert.equal(b.env.PI_PROVIDER, 'anthropic');
|
||||
assert.equal(b.env.PI_MODEL, 'claude-fixture');
|
||||
// #1873 harness-matrix: a Claude account model runs on claude-code only, so a pi session on it
|
||||
// refuses before any file is written.
|
||||
test('a proxy Claude route on pi refuses model-harness-incompatible before model config generation', t => {
|
||||
for (const a of [models.a, { provider: 'anthropic', model: 'fixture-chat' }, { provider: 'anthropic', model: 'fixture-chat', module: 'cliproxy', upstream: 'claude' }])
|
||||
assert.throws(() => build(t, 'a', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, { a }), { code: 'model-harness-incompatible' }, JSON.stringify(a));
|
||||
});
|
||||
|
||||
test('an other-upstream route uses messages at proxy root under its own pi provider, never pi\'s built-in anthropic', t => {
|
||||
const b = build(t, 'o', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, { o: models.o });
|
||||
const providers = JSON.parse(b.text).providers;
|
||||
assert.deepEqual(Object.keys(providers), ['mosaic-proxy-other']);
|
||||
assert.equal(providers['mosaic-proxy-other'].api, 'anthropic-messages');
|
||||
assert.equal(providers['mosaic-proxy-other'].baseUrl, 'http://127.0.0.1:12345');
|
||||
assert.deepEqual(providers['mosaic-proxy-other'].models.map(m => m.id), ['fixture-other']);
|
||||
assert.equal(b.env.PI_PROVIDER, 'mosaic-proxy-other');
|
||||
assert.equal(b.env.PI_MODEL, 'fixture-other');
|
||||
});
|
||||
|
||||
test('P3 omitted route refuses proxy-route-missing before model config generation', t => {
|
||||
@@ -82,9 +92,11 @@ test('P3 a declared Codex route generates only the single build-time responses p
|
||||
|
||||
test('P3 models file is private, references the one session key, and stores no executable values or keys', t => {
|
||||
const key = 'fx332-agent-a';
|
||||
const b = build(t, 'a', { baseUrl: 'http://127.0.0.1:12345', key }, { a: models.a, z: models.z });
|
||||
const b = build(t, 'z', { baseUrl: 'http://127.0.0.1:12345', key }, { a: models.a, z: models.z, o: models.o });
|
||||
const document = JSON.parse(b.text);
|
||||
assert.deepEqual(Object.keys(document.providers).sort(), ['anthropic', 'mosaic-proxy-glm']);
|
||||
// #1873: the Claude account entry is left out of a pi session's file.
|
||||
assert.deepEqual(Object.keys(document.providers).sort(), ['mosaic-proxy-glm', 'mosaic-proxy-other']);
|
||||
assert.ok(!b.text.includes('claude-fixture'));
|
||||
for (const provider of Object.values(document.providers)) assert.equal(provider.apiKey, '$ANTHROPIC_API_KEY');
|
||||
assertSafe(document);
|
||||
assert.ok(!b.text.includes(key));
|
||||
@@ -96,20 +108,20 @@ test('P3 models file is private, references the one session key, and stores no e
|
||||
});
|
||||
|
||||
test('P3 every launch regenerates proxy port, declared models and own key without caching', t => {
|
||||
const first = build(t, 'a', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, { a: models.a, z: models.z });
|
||||
const second = build(t, 'a', { baseUrl: 'http://127.0.0.1:23456', key: 'fx332-agent-b' }, {
|
||||
a: { provider: 'proxy', model: 'changed-model-id', route: 'anthropic' },
|
||||
const first = build(t, 'z', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, { g: models.g, z: models.z });
|
||||
const second = build(t, 'z', { baseUrl: 'http://127.0.0.1:23456', key: 'fx332-agent-b' }, {
|
||||
g: { provider: 'proxy', model: 'changed-model-id', route: 'codex' },
|
||||
z: { provider: 'proxy', model: 'changed-glm-id', route: 'glm' },
|
||||
});
|
||||
assert.equal(second.env.ANTHROPIC_API_KEY, 'fx332-agent-b');
|
||||
assert.ok(!JSON.stringify(second).includes('fx332-agent-a'));
|
||||
const { providers } = JSON.parse(second.text);
|
||||
assert.deepEqual(Object.keys(providers), ['anthropic', 'mosaic-proxy-glm']);
|
||||
assert.equal(providers.anthropic.baseUrl, 'http://127.0.0.1:23456');
|
||||
assert.deepEqual(providers.anthropic.models.map(m => m.id), ['changed-model-id']);
|
||||
assert.deepEqual(Object.keys(providers), ['mosaic-proxy-codex', 'mosaic-proxy-glm']);
|
||||
assert.equal(providers['mosaic-proxy-codex'].baseUrl, 'http://127.0.0.1:23456/v1');
|
||||
assert.deepEqual(providers['mosaic-proxy-codex'].models.map(m => m.id), ['changed-model-id']);
|
||||
assert.equal(providers['mosaic-proxy-glm'].baseUrl, 'http://127.0.0.1:23456/v1');
|
||||
assert.deepEqual(providers['mosaic-proxy-glm'].models.map(m => m.id), ['changed-glm-id']);
|
||||
assert.equal(JSON.parse(first.text).providers.anthropic.baseUrl, 'http://127.0.0.1:12345');
|
||||
assert.equal(JSON.parse(first.text).providers['mosaic-proxy-glm'].baseUrl, 'http://127.0.0.1:12345/v1');
|
||||
});
|
||||
|
||||
// Row 332 R2 (#1830, review F2): launch.models is open table data. An entry whose provider has no
|
||||
@@ -121,14 +133,14 @@ test('row 332 R2 an unroutable model table entry is left out of models.json and
|
||||
glm: { provider: 'zai', model: 'glm-fixture' },
|
||||
router: { provider: 'openrouter', model: 'x' },
|
||||
};
|
||||
const b = build(t, 'opus', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, declared);
|
||||
const b = build(t, 'glm', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, declared);
|
||||
const { providers } = JSON.parse(b.text);
|
||||
assert.deepEqual(Object.keys(providers).sort(), ['anthropic', 'mosaic-proxy-glm']);
|
||||
assert.deepEqual(providers.anthropic.models.map(m => m.id), ['claude-fixture']);
|
||||
// #1873: the Claude account entry is one a pi session can't run, so it is left out too.
|
||||
assert.deepEqual(Object.keys(providers).sort(), ['mosaic-proxy-glm']);
|
||||
assert.deepEqual(providers['mosaic-proxy-glm'].models.map(m => m.id), ['glm-fixture']);
|
||||
assert.ok(!Object.values(providers).some(p => p.models.some(m => m.id === 'x')), 'the openrouter entry is not written');
|
||||
assert.equal(b.env.PI_MODEL, 'claude-fixture');
|
||||
assert.throws(() => build(t, 'opus', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, {
|
||||
assert.equal(b.env.PI_MODEL, 'glm-fixture');
|
||||
assert.throws(() => build(t, 'glm', { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' }, {
|
||||
...declared, broken: { provider: 'proxy', model: 'claude-fixture' },
|
||||
}), { code: 'proxy-route-missing' });
|
||||
});
|
||||
@@ -140,7 +152,7 @@ test('row 332 R2 a session proxy context without a key or loopback URL refuses i
|
||||
{ baseUrl: 'http://localhost:12345', key: 'fx332-agent-a' },
|
||||
{ baseUrl: 'http://127.0.0.1:12345' },
|
||||
]) {
|
||||
assert.throws(() => build(t, 'a', proxy, { a: models.a }),
|
||||
assert.throws(() => build(t, 'z', proxy, { z: models.z }),
|
||||
{ code: 'invalid-proxy-context', message: 'invalid session proxy context' }, JSON.stringify(proxy));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -3,12 +3,12 @@ import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { sessionModel } from '../src/bundle.mjs';
|
||||
const resolved = { instance: 'fixture', vars: { harness: 'pi', model: 'selected', thinking: 'low' } };
|
||||
for (const [provider, model, expected] of [
|
||||
['anthropic', 'glm-looking-id', 'anthropic'],
|
||||
['zai', 'gpt-looking-id', 'glm'],
|
||||
for (const [provider, model, expected, harness] of [
|
||||
['anthropic', 'glm-looking-id', 'anthropic', 'claude-code'],
|
||||
['zai', 'gpt-looking-id', 'glm', 'pi'],
|
||||
]) {
|
||||
test(`joined proxy context maps provider ${provider} by identity, not model name`, () => {
|
||||
const selected = sessionModel(resolved, { selected: { provider, model } }, 'primary', { proxy: true });
|
||||
const selected = sessionModel({ ...resolved, vars: { ...resolved.vars, harness } }, { selected: { provider, model } }, 'primary', { proxy: true });
|
||||
assert.equal(selected.proxyRoute, expected);
|
||||
assert.equal(selected.provider, provider, 'public/accounting provider identity is retained');
|
||||
assert.equal(selected.model, model);
|
||||
@@ -31,7 +31,7 @@ test('provider proxy still requires an explicit route inside joined context', ()
|
||||
assert.throws(() => sessionModel(resolved, { selected: { provider: 'proxy', model: 'fixture', route: 'custom' } }, 'primary', { proxy: true }), {
|
||||
code: 'proxy-route-unknown',
|
||||
});
|
||||
assert.equal(sessionModel(resolved, { selected: { provider: 'proxy', model: 'claude-looking-id', route: 'codex' } }, 'primary', { proxy: true }).proxyRoute, 'codex');
|
||||
assert.equal(sessionModel(resolved, { selected: { provider: 'proxy', model: 'fixture-codex-id', route: 'codex' } }, 'primary', { proxy: true }).proxyRoute, 'codex');
|
||||
});
|
||||
test('a module-backed selection carries the provider transport descriptor without changing accounting', () => {
|
||||
const selected = sessionModel(resolved, { selected: { provider: 'codex', model: 'fixture-codex', module: 'cliproxy', upstream: 'codex' } }, 'primary', { proxy: true });
|
||||
@@ -52,3 +52,23 @@ test('transport accepts bounded compatibility flags declared by a provider modul
|
||||
const resolver=()=>({provider:'fixture',model:'fixture-id',routeId:'fixture',endpoint:'v1',api:'openai-completions',piProvider:'fixture',compat:{supportsStore:false,thinkingFormat:'openai'}});
|
||||
assert.deepEqual(sessionModel(resolved,{selected:entry},'primary',{proxy:true,resolver}).proxyTransport.compat,{supportsStore:false,thinkingFormat:'openai'});
|
||||
});
|
||||
|
||||
// #1873 harness-matrix: the launch checks the route's own harness, so a stored OAuth Claude route on pi
|
||||
// is refused before any run, with or without a model-provider module switched on.
|
||||
test('a stored proxy Claude route on pi refuses model-harness-incompatible at launch', () => {
|
||||
for (const selected of [{ provider: 'anthropic', model: 'fixture-chat' }, { provider: 'proxy', route: 'anthropic', model: 'fixture-chat' },
|
||||
{ provider: 'anthropic', model: 'fixture-chat', module: 'cliproxy', upstream: 'claude' }])
|
||||
assert.throws(() => sessionModel(resolved, { selected }, 'primary', { proxy: true }), { code: 'model-harness-incompatible' }, JSON.stringify(selected));
|
||||
// Jason 2026-11-19: the rule keys on the account kind. A module entry, whose kind the catalogue
|
||||
// knows, runs on pi with a Claude name. A legacy entry carries no account data, so a Claude name
|
||||
// there is treated as OAuth.
|
||||
for (const selected of [{ provider: 'other', model: 'anthropic/claude-sonnet-5-5', module: 'cliproxy', upstream: 'other' },
|
||||
{ provider: 'zai', model: 'claude-looking-id', module: 'cliproxy', upstream: 'zai' }, { provider: 'zai', model: 'glm-fixture' }])
|
||||
assert.equal(sessionModel(resolved, { selected }, 'primary', { proxy: true }).model, selected.model, JSON.stringify(selected));
|
||||
for (const selected of [{ provider: 'proxy', route: 'codex', model: 'claude-looking-id' }, { provider: 'zai', model: 'claude-looking-id' }, { provider: 'proxy', route: 'glm', model: 'claude-opus-5-5' }])
|
||||
assert.throws(() => sessionModel(resolved, { selected }, 'primary', { proxy: true }), { code: 'model-harness-incompatible' }, JSON.stringify(selected));
|
||||
const claudeCode = { ...resolved, vars: { ...resolved.vars, harness: 'claude-code' } };
|
||||
assert.equal(sessionModel(claudeCode, { selected: { provider: 'anthropic', model: 'fixture-chat' } }, 'primary', { proxy: true }).proxyRoute, 'anthropic');
|
||||
for (const selected of [{ provider: 'zai', model: 'fixture-zai' }, { provider: 'codex', model: 'fixture-codex', module: 'cliproxy', upstream: 'codex' }])
|
||||
assert.equal(sessionModel(claudeCode, { selected }, 'primary', { proxy: true }).provider, selected.provider);
|
||||
});
|
||||
@@ -4,7 +4,8 @@ import { readFileSync, mkdirSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { buildBundle } from '../src/bundle.mjs';
|
||||
import { typedTools } from '../src/tools.mjs';
|
||||
import { resolvedFor, scratch, HARNESS, REPO_SKILLS } from './helpers.mjs';
|
||||
import { resolvedFor, scratch, HARNESS, REPO_SKILLS, harnessReady } from './helpers.mjs';
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
import { ownRails, railsText, decisionAutonomy } from '../../organization/src/rails.mjs';
|
||||
|
||||
const SECRET = 'sk-test-NOT-A-SECRET-1234';
|
||||
|
||||
@@ -9,7 +9,8 @@ import { createServer } from 'node:net';
|
||||
import { buildBundle, SOURCES } from '../src/bundle.mjs';
|
||||
import { callTool, typedTools } from '../src/tools.mjs';
|
||||
import * as runner from '../src/runner.mjs';
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from './helpers.mjs';
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from './helpers.mjs';
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
import { Broker } from '../../bus/src/broker.mjs';
|
||||
import { Store } from '../../bus/src/store.mjs';
|
||||
import { createRequestStore } from '../../bus/src/request-store.mjs';
|
||||
|
||||
@@ -322,6 +322,63 @@ test("r127 an adapter exit 127 names the program that wasn't found, through the
|
||||
r.child.kill("SIGTERM"); await r.exited;
|
||||
});
|
||||
|
||||
// #1873 chat-fail F1: a turn that ended on the harness's error result showed only "adapter exited 1".
|
||||
// The real claude adapter prints the CLI's final error result on stdout and exits with the CLI's code.
|
||||
test("chat-fail F1 an error result from the harness reaches the chat line as a plain cause and next step", async t => {
|
||||
const ctx = await setup(t, {session:{harness:"claude-code", turnEnd:"exit", adapter:new URL("../../../adapters/claude/adapter.sh", import.meta.url).pathname}});
|
||||
const sessionFile = join(ctx.runDir, "session.json");
|
||||
const session = JSON.parse(readFileSync(sessionFile, "utf8"));
|
||||
const bin = join(ctx.dir, "model-fixture");
|
||||
const result = {type:"result", subtype:"success", is_error:true, result:"You've hit your limit \u00b7 resets 3pm (UTC)"};
|
||||
writeFileSync(bin, `#!${process.execPath}\nconsole.log(${JSON.stringify(JSON.stringify(result))});\nprocess.exit(1);\n`, {mode:0o755});
|
||||
session.harnessEnv = {MOSAIC_CLAUDE_BIN:bin, MOSAIC_THINKING:"low", MOSAIC_CLAUDE_SETTINGS:session.bundle.policy, MOSAIC_CLAUDE_MCP_CONFIG:session.bundle.tools};
|
||||
writeFileSync(sessionFile, JSON.stringify(session));
|
||||
ctx.inbox = [];
|
||||
const r = startRunner(ctx.runDir, ctx.cap);
|
||||
t.after(() => r.child.kill("SIGKILL"));
|
||||
const sent = ctx.call(ctx.pm, "message.send", {to:"coder", body:"hello", class:"REQUEST"});
|
||||
const [reply] = await until(replies(ctx));
|
||||
await until(() => ctx.store.get("SELECT 1 FROM deliveries WHERE message=? AND op='read'", sent.id));
|
||||
assert.equal(reply.body, "The turn failed: the model account is out of usage. Check Settings > Model setup.\nDetails: adapter exited 1");
|
||||
assert.match(r.stderr(), /failed: adapter exited 1 \(the model account is out of usage/);
|
||||
r.child.kill("SIGTERM"); await r.exited;
|
||||
});
|
||||
|
||||
test("chat-fail F1 error text is cleaned, redacted and capped; a held secret withholds it; no text keeps the bare reason", async t => {
|
||||
const secret = "fixture-secret-0123456789abcdef";
|
||||
const ctx = await setup(t, {session:{harnessEnv:{MOSAIC_FIXTURE_TOKEN:secret}}});
|
||||
const adapter = join(ctx.dir, "adapter.sh");
|
||||
// HOSTILE: ANSI, C0, C1, DEL, bidi and zero-width characters, a link with a query and userinfo, an
|
||||
// email, a path, a Bearer value, key prefixes, a JWT and a long text. LEAK: the turn's own secret,
|
||||
// split by a newline and a zero-width space. QUIET: nothing on stdout.
|
||||
writeFileSync(adapter, [
|
||||
'case "$MOSAIC_REQUEST" in',
|
||||
// The long text comes first: the harness prints its error result last, and only the tail is read.
|
||||
`*HOSTILE*) head -c 2000 /dev/zero | tr '\\0' 'w'; printf '\\n\\033[31mAPI Error: 500\\033[0m \\001\\177\\302\\205 boom \\342\\200\\256evil\\342\\200\\213 https://u:[email protected]/x?key=abc [email protected] /home/someone/.config/x Bearer abc.def sk-ant-api03-zzz msk-abcdef eyJhbGciOi.eyJzdWIi.sig AbCdEf0123456789XyZ\\n'; exit 1 ;;`,
|
||||
`*LEAK*) printf 'failed: fixture-secret-01234\\n5678\\342\\200\\2139abcdef\\n'; exit 1 ;;`,
|
||||
`*QUIET*) exit 1 ;;`,
|
||||
'esac',
|
||||
].join("\n") + "\n");
|
||||
ctx.inbox = [];
|
||||
const r = startRunner(ctx.runDir, ctx.cap);
|
||||
t.after(() => r.child.kill("SIGKILL"));
|
||||
for (const body of ["HOSTILE", "LEAK", "QUIET"]) ctx.call(ctx.pm, "message.send", {to:"coder", body});
|
||||
await until(() => replies(ctx)() && ctx.inbox.length >= 3);
|
||||
const [hostile, leak, quiet] = ctx.inbox.map(m => m.body);
|
||||
const [cause, details] = hostile.slice("The turn failed: ".length).split("\nDetails: ");
|
||||
assert.equal(details, "adapter exited 1");
|
||||
// The long token before the error is redacted as a whole: it shows, if at all, as one [redacted].
|
||||
assert.match(cause, /^(?:\[redacted\] )?API Error: 500 boom evil /);
|
||||
assert.ok(Array.from(cause).length <= 300, cause.length);
|
||||
assert.equal(cause.includes("www"), false, "the long text before the error is not shown");
|
||||
assert.doesNotMatch(hostile, /[\u0000-\u0009\u000b-\u001f\u007f-\u009f\u200b-\u200f\u202a-\u202e\u2066-\u2069\ufeff]/u, "no control or format character but the one separating newline");
|
||||
for (const bad of ["pw@", "example.com", "key=abc", "mail@", "/home/someone", "abc.def", "sk-ant", "msk-abcdef", "eyJhbGciOi", "AbCdEf0123456789XyZ"]) assert.equal(hostile.includes(bad), false, bad);
|
||||
assert.equal(leak, "The turn failed: the error text held a secret, so it isn't shown. Check Settings > Model setup.\nDetails: adapter exited 1");
|
||||
assert.equal(quiet, "The turn failed: adapter exited 1");
|
||||
for (const line of [...ctx.inbox.map(m => m.body), r.stderr()]) assert.equal(line.replace(/\s|\u200b/g, "").includes(secret), false);
|
||||
r.child.kill("SIGTERM"); await r.exited;
|
||||
});
|
||||
|
||||
test("r127 an adapter whose own command is missing names it; a 127 with no such line keeps the bare reason", async t => {
|
||||
const ctx = await setup(t);
|
||||
const adapter = join(ctx.dir, "adapter.sh");
|
||||
@@ -394,7 +451,8 @@ for (const [harness, exit, activity] of [['claude-code', 1, false], ['claude-cod
|
||||
const r = startRunner(ctx.runDir, ctx.cap, { MOSAIC_MODEL_ACTIVITY_SOURCE: 'adapter' });
|
||||
t.after(async () => { r.child.kill('SIGTERM'); await r.exited; });
|
||||
const reply = await ask(ctx);
|
||||
if (exit) assert.match(reply.body, /The turn failed: adapter exited 1/);
|
||||
// #1873 chat-fail F1: the error text on stdout is the turn's plain cause.
|
||||
if (exit) assert.equal(reply.body, 'The turn failed: API Error: 400 synthetic-result\nDetails: adapter exited 1');
|
||||
else assert.equal(reply.body, 'API Error: 400 synthetic-result');
|
||||
const marker = join(ctx.runDir, 'proxy-first-output');
|
||||
assert.equal(existsSync(marker), activity);
|
||||
|
||||
@@ -10,7 +10,8 @@ import { chmodSync, cpSync, mkdirSync, readFileSync, rmSync, symlinkSync, writeF
|
||||
import { join } from "node:path";
|
||||
import { BundleError, buildBundle, readSkills, SKILL_BYTES } from "../src/bundle.mjs";
|
||||
import { adapterEnv } from "../src/runner.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
|
||||
import { HARNESS, REPO, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
|
||||
const sha = (text) => createHash("sha256").update(text).digest("hex");
|
||||
const MARKED = /<skill-file skill="([^"]+)" path="([^"]+)">\n([\s\S]*?)\n<\/skill-file>/g;
|
||||
@@ -183,7 +184,8 @@ test("each adapter passes the skills only in the prompt file, with skill discove
|
||||
const b = buildBundle({ dir: join(dir, "bundle"), resolved, contract: "# Contract\n", harness: HARNESS[harness], models, skillsDir: REPO_SKILLS, session: { ...s, run: "run-1", launchedBy: "pm" } });
|
||||
s.bundle = b.files;
|
||||
s.harnessEnv = b.env;
|
||||
const env = { ...adapterEnv(s, { PATH: `${join(dir, "bin")}:/usr/bin:/bin`, HOME: join(dir, "home") }), ARGV_OUT: join(dir, "argv"), MOSAIC_REQUEST: "x" };
|
||||
// #1873 S2: the pi adapter runs MOSAIC_PI_BIN, which the bundle sets to the pi it found.
|
||||
const env = { ...adapterEnv(s, { PATH: `${join(dir, "bin")}:/usr/bin:/bin`, HOME: join(dir, "home") }), ARGV_OUT: join(dir, "argv"), MOSAIC_REQUEST: "x", ...(harness === "pi" ? { MOSAIC_PI_BIN: cli } : {}) };
|
||||
const r = spawnSync("/bin/sh", [join(REPO, "adapters", harness === "pi" ? "pi" : "claude", "adapter.sh")], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"], env });
|
||||
assert.equal(r.status, 0, `${harness}: ${r.stderr}`);
|
||||
const argv = readFileSync(join(dir, "argv"), "utf8").split("\0");
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
// #1873 chat-fail F1: the plain cause of a failed turn, from the harness's error text.
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { CAUSE_MAX, PLAIN, WITHHELD, cleanText, failureBody, secretValues, turnCause } from "../src/failure-notice.mjs";
|
||||
|
||||
test("the three recognised causes map to a fixed plain line that carries none of the error's words", () => {
|
||||
for (const text of ["Claude AI usage limit reached|1700000000", "You've hit your limit \u00b7 resets 3pm (UTC)",
|
||||
"5-hour limit reached \u2219 resets 9pm", "Credit balance is too low", '{"error":{"type":"insufficient_quota"}}', "Quota exceeded for this account"])
|
||||
assert.equal(turnCause(text), PLAIN.usage, text);
|
||||
for (const text of ["OAuth token has expired. Please obtain a new token or refresh your existing token.", "Invalid API key \u00b7 Please run /login",
|
||||
'API Error: 401 {"type":"error","error":{"type":"authentication_error","message":"invalid x-api-key"}}', "Not logged in"])
|
||||
assert.equal(turnCause(text), PLAIN.signin, text);
|
||||
for (const text of ["There's an issue with the selected model (some-model). It may not exist or you may not have access to it.",
|
||||
'{"error":{"type":"not_found_error","message":"model: some-model"}}', "model_not_found", "The model some-model does not exist"])
|
||||
assert.equal(turnCause(text), PLAIN.model, text);
|
||||
// Other limits are not the account's usage.
|
||||
for (const text of ["Context limit reached \u00b7 /compact or /clear to continue", "output limit reached"])
|
||||
assert.notEqual(turnCause(text), PLAIN.usage, text);
|
||||
for (const line of Object.values(PLAIN)) assert.doesNotMatch(line, /\u2014/, "no em dash in UI text");
|
||||
});
|
||||
|
||||
test("no text, or text that is only controls or one long token, gives no cause", () => {
|
||||
for (const text of [undefined, null, "", " \n\t", "\u0000\u001b[0m\u200b", "x".repeat(5000)]) assert.equal(turnCause(text), null, JSON.stringify(text));
|
||||
});
|
||||
|
||||
test("control, format and separator characters go and the cause is capped", () => {
|
||||
assert.equal(cleanText("a\u0000b\u0007c\u001b[1;31md\u001b]0;title\u0007e\u007ff\u0085g\u2028h\u2029i\u202ej\u2066k\u200bl\ufeffm\tn\r\no"), "a b cde f g h ijklm n o");
|
||||
const long = turnCause("word ".repeat(400));
|
||||
assert.ok(Array.from(long).length <= CAUSE_MAX && long.endsWith("..."), long.length);
|
||||
// Astral characters are cut whole, never into a lone surrogate.
|
||||
const astral = turnCause("\u{1f600}".repeat(400));
|
||||
assert.equal(/[\ud800-\udfff]/u.test(astral.replace(/\u{1f600}/gu, "")), false);
|
||||
});
|
||||
|
||||
test("secrets, links, emails, paths and key-like runs never reach the cause", () => {
|
||||
const secret = "s3cr3t-value-for-the-fixture";
|
||||
const secrets = secretValues({ MOSAIC_MODEL_KEY: secret, MOSAIC_PROXY_TOKEN: "tok-0123456789", PATH: "/usr/bin:/bin", SHORT_KEY: "abc" });
|
||||
assert.deepEqual(secrets, [secret, "tok-0123456789"]);
|
||||
for (const text of [`boom ${secret}`, `boom ${secret.slice(0, 9)}\n${secret.slice(9)}`, `boom ${secret.slice(0, 5)}\u200b${secret.slice(5)}`, `boom ${secret.slice(0, 5)}\u0000${secret.slice(5)}`])
|
||||
assert.equal(turnCause(text, secrets), WITHHELD, JSON.stringify(text));
|
||||
const shown = turnCause("API Error: 500 at https://user:[email protected]/a?token=zz and www.host.example/q, [email protected], /home/u/.config/mosaic-dev/x ~/x ./y "
|
||||
+ "Bearer abc.def Authorization: Basic Zm9v api_key=k1 token: k2 sk-ant-api03-k3 ghp_k4 xoxb-k5 eyJa.b.c " + "A1".repeat(10) + " " + "z".repeat(40), secrets);
|
||||
for (const bad of ["user:pw", "host.example", "token=zz", "ops@", "/home/u", "~/x", "./y", "abc.def", "Zm9v", "k1", "k2", "sk-ant", "ghp_", "xoxb", "eyJa", "A1A1", "zzzz"])
|
||||
assert.equal(shown.includes(bad), false, `${bad} in ${shown}`);
|
||||
assert.match(shown, /^API Error: 500 at \[redacted\]/);
|
||||
});
|
||||
|
||||
test("the chat body puts the plain cause first and the technical reason after", () => {
|
||||
assert.equal(failureBody({ reason: "adapter exited 1", cause: PLAIN.usage }), `The turn failed: ${PLAIN.usage}\nDetails: adapter exited 1`);
|
||||
assert.equal(failureBody({ reason: "adapter exited 1" }), "The turn failed: adapter exited 1");
|
||||
});
|
||||
|
||||
test("review round 1: quoted keys, short or letters-only values and a space separator are redacted", () => {
|
||||
for (const [text, value] of [['{"error":"bad","password":"hunter2hunte"}', "hunter2hunte"], ['{"api_key":"abcdefghijklmno"}', "abcdefghijklmno"],
|
||||
['auth failed for key "Zq8xPw2LmN4"', "Zq8xPw2LmN4"], ["token abcd1234efgh567 rejected", "abcd1234efgh567"],
|
||||
["secret qwertyuiopasdfghjklzxcvbnmqwer", "qwertyuiop"], ['{"access_token":"shortvalue1"}', "shortvalue1"], ["client_secret=abc", "abc"]]) {
|
||||
const shown = turnCause(text);
|
||||
assert.ok(shown === null || !shown.includes(value), `${value} in ${shown}`);
|
||||
}
|
||||
// Ordinary words after "key" or "session" are left alone.
|
||||
assert.equal(turnCause("API Error: 500 the key point is the session ended"), "API Error: 500 the key point is the session ended");
|
||||
});
|
||||
|
||||
test("review round 1: encoded emails, Windows and UNC paths and encoded env secrets never reach the cause", () => {
|
||||
for (const [text, bad] of [["mail jason%40example.com", "example"], ["mail jason%2540example.com", "example"], ["mail jason\uff20example.com", "example"],
|
||||
["mail jason@example.com", "example"], ["read C:\\Users\\jason\\.claude\\creds.json", "jason"], ["read \\\\server\\share\\creds", "server"]]) {
|
||||
const shown = turnCause(text);
|
||||
assert.ok(shown === null || !shown.includes(bad), `${bad} in ${shown}`);
|
||||
}
|
||||
const secrets = secretValues({ MOSAIC_PROXY_KEY: "pxy_0123456789abcd" });
|
||||
for (const text of ["boom pxy%5F0123456789abcd", "boom pxy_0123456789abcd", "boom pxy_0123456789abcd", "boom pxy_01234\u030156789abcd", "boom \uff50xy_0123456789abcd"])
|
||||
assert.equal(turnCause(text, secrets), WITHHELD, JSON.stringify(text));
|
||||
});
|
||||
|
||||
test("review round 1: a long output is read from its end, so an answer that talks about limits is not the cause", () => {
|
||||
const cause = turnCause("Here is how limits work: usage limit reached means wait. " + "x ".repeat(1500) + "Error: adapter crashed: segfault");
|
||||
assert.notEqual(cause, PLAIN.usage);
|
||||
assert.match(cause, /segfault$/);
|
||||
assert.equal(turnCause("y ".repeat(1500) + "Claude AI usage limit reached"), PLAIN.usage);
|
||||
});
|
||||
|
||||
// Review round 2: the tail cut can land between a key word and its value. Redaction runs
|
||||
// before the cut, so the value is still redacted with its key word gone.
|
||||
test("review round 2: a tail cut between a key word and its value never shows the value", () => {
|
||||
for (const [key, value] of [["password:", "Zq8xPw2LmN"], ["Bearer", "Zq8xPw2LmN"], ['"api_key":', '"Zq8xPw2LmN"'], ["token", "Zq8xPw2LmN4"]]) {
|
||||
// The last 2000 characters start just before the value, so the cut parts it from its key
|
||||
// word; a near miss on either side is checked too.
|
||||
for (let shift = -2; shift <= 2; shift++) {
|
||||
const end = " then the adapter crashed";
|
||||
const head = ` ${value} https://e.example/`;
|
||||
const tail = head + "a".repeat(2000 - head.length - end.length + shift) + end;
|
||||
const cause = turnCause("w ".repeat(300) + key + tail);
|
||||
assert.ok(cause !== null && !cause.includes("Zq8xPw2LmN"), `${key} shift ${shift}: ${cause}`);
|
||||
assert.match(cause, /then the adapter crashed$/);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Review round 3: redaction runs over the whole kept stdout (up to 128000 characters), so a long
|
||||
// run with no spaces (base64, a data URI, minified JSON) must not make it slow.
|
||||
test("review round 3: a 128000-character run with no spaces is redacted in well under a second", () => {
|
||||
const N = 128000;
|
||||
for (const run of ["a".repeat(N), "a.".repeat(N / 2), "a-".repeat(N / 2), "a/".repeat(N / 2), "a:".repeat(N / 2), 'a"'.repeat(N / 2)]) {
|
||||
const started = performance.now();
|
||||
const cause = turnCause(`${run} then the adapter crashed`);
|
||||
const took = performance.now() - started;
|
||||
assert.ok(took < 1000, `${run.slice(0, 4)}... took ${Math.round(took)} ms`);
|
||||
assert.match(cause, /then the adapter crashed$/);
|
||||
}
|
||||
});
|
||||
// Review round 3: a long text that redacts down below the read limit isn't cut, so its first word stays.
|
||||
test("review round 3: a long text that redaction makes short keeps its first word", () => {
|
||||
assert.equal(turnCause(`Crash: adapter died at https://e.example/${"a".repeat(2500)}`), "Crash: adapter died at [redacted]");
|
||||
});
|
||||
@@ -9,7 +9,8 @@ import { join } from "node:path";
|
||||
// A namespace import, so the red-before test runs (and fails on its assertion) on a base without
|
||||
// the section's exports.
|
||||
import * as bundleModule from "../src/bundle.mjs";
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch } from "./helpers.mjs";
|
||||
import { HARNESS, REPO_SKILLS, resolvedFor, scratch, harnessReady } from "./helpers.mjs";
|
||||
await harnessReady(); // #1873 re-check 7: buildBundle reads a passed fit check
|
||||
import { REPO_ROLES } from "../../organization/tests/helpers.mjs";
|
||||
|
||||
const { buildBundle, sessionModel } = bundleModule;
|
||||
|
||||
@@ -64,7 +64,7 @@ code is in that same tree, a restart picks it up. If it is in another tree,
|
||||
the unit keeps the old PIN until it is installed again from the new tree:
|
||||
stop it, remove its unit file, then `mosaic proxy unit install` from the new
|
||||
tree, because install never replaces a unit. Then run `mosaic proxy fetch` (or Fetch on the Console's
|
||||
Model proxy settings), which installs the new release beside the old one
|
||||
Model setup page), which installs the new release beside the old one
|
||||
under `<dataRoot>/opt/cliproxyapi/<version>/`. Then restart the proxy with
|
||||
`mosaic proxy unit restart`. Before the fetch, the proxy fails to start with
|
||||
`proxy-install-missing`, and keeps failing until the fetch is done. Restart the bus and
|
||||
@@ -148,7 +148,7 @@ follow-up; do not run those writers concurrently with organization buses.
|
||||
|
||||
### Setup from the Console (#1873)
|
||||
|
||||
Settings > Model proxy has a Proxy setup panel for the instance owner: the
|
||||
Settings > Model setup (route `#/settings/model-proxy`) has a Proxy setup panel for the instance owner: the
|
||||
`--dev-local-owner` owner on a dev Console, or the root owner under `--sso`.
|
||||
Agents, testers and grant holders get `human-required` on every setup route,
|
||||
read or write. The panel shows the release (pinned version, hashes verified),
|
||||
|
||||
@@ -7,11 +7,66 @@ import { homedir } from 'node:os';
|
||||
import { fetchPinned } from './fetch.mjs';
|
||||
import { PIN } from './common.mjs';
|
||||
import { localRide, riddenOrgs } from './ride.mjs';
|
||||
import { releaseState, initState, fenceState, initLockStatus, clearInitLock, appendReceipt, recentReceipts, receiptsText, createUnitControl, FILE_LABELS } from './admin.mjs';
|
||||
import { releaseState, initState, fenceState, initLockStatus, clearInitLock, appendReceipt, recentReceipts, receiptsText, createUnitControl, FILE_LABELS, LOG_LINES, receiptHistory, serviceKeyState } from './admin.mjs';
|
||||
import { secretValues } from './redact.mjs';
|
||||
import { discoverModules, hasModule } from '../../plugins/src/loader.mjs';
|
||||
import { readPluginLock } from '../../plugins/src/pinned.mjs';
|
||||
|
||||
export const proxyCode = (error, fallback) => (/^proxy-[a-z-]+$/.test(error?.code ?? '') ? error.code : fallback);
|
||||
const failure = code => Object.assign(new Error(code), { code });
|
||||
|
||||
// #1873 settings-backend A2: the harness programs installed on this instance, from the plugin tree
|
||||
// through the plugins loader and from the instance's plugin lock, the way the bus lists them
|
||||
// (runtime.mjs harnessLevels). No name list here. A tree that cannot be read answers null; a lock
|
||||
// that cannot be read adds nothing, so a pinned program is then not offered.
|
||||
export function installedHarnesses(dataRoot = null) {
|
||||
let names;
|
||||
try { names = discoverModules('harness').names.filter(name => hasModule('harness', name)); } catch { return null; }
|
||||
if (dataRoot !== null) {
|
||||
try { names = [...new Set([...names, ...Object.keys(readPluginLock(dataRoot).plugins).filter(name => hasModule('harness', name, dataRoot))])]; } catch { /* the tree's list */ }
|
||||
}
|
||||
return names.sort();
|
||||
}
|
||||
|
||||
// #1873 settings-backend A2: every harness an organization document names for an agent: each
|
||||
// `harness` or `fallback.harness` string anywhere in it (role vars, project agent vars, or any
|
||||
// later place), outside its plugins list. Broader than the routes view on purpose: a program is
|
||||
// never turned off where anything in the file still names it.
|
||||
export function harnessesNamed(document) {
|
||||
const named = new Set();
|
||||
const walk = (value, depth) => {
|
||||
if (depth > 32 || value === null || typeof value !== 'object') return;
|
||||
if (Array.isArray(value)) { for (const item of value) walk(item, depth + 1); return; }
|
||||
for (const [key, item] of Object.entries(value)) {
|
||||
if ((key === 'harness' || key === 'fallback.harness') && typeof item === 'string') named.add(item);
|
||||
else walk(item, depth + 1);
|
||||
}
|
||||
};
|
||||
if (document === null || typeof document !== 'object' || Array.isArray(document)) return null;
|
||||
for (const [key, value] of Object.entries(document)) if (key !== 'plugins') walk(value, 1);
|
||||
return named;
|
||||
}
|
||||
|
||||
// #1873 settings-backend A2 (review P1-1): the harnesses the root agents an organization takes name.
|
||||
// `root` is what loadRootAgents answered ({state, agents, assistant, definitions?}). An organization
|
||||
// takes every root agent except the root assistant and the ones that exclude it, the way
|
||||
// materializeCopies copies them at bus boot. Anything not in that shape answers null (unreadable).
|
||||
export function rootHarnessesNamed(root, orgId) {
|
||||
if (root === null || typeof root !== 'object' || root.agents === null || typeof root.agents !== 'object' || Array.isArray(root.agents)) return null;
|
||||
const taken = {};
|
||||
for (const [name, entry] of Object.entries(root.agents)) {
|
||||
if (entry === null || typeof entry !== 'object' || !Array.isArray(entry.excludeOrgs)) return null;
|
||||
if (entry.definition === 'assistant' || entry.excludeOrgs.includes(orgId)) continue;
|
||||
taken[name] = entry.vars ?? {};
|
||||
}
|
||||
return harnessesNamed({ agents: taken, definitions: root.definitions ?? {} });
|
||||
}
|
||||
// A key id is the first 12 hex of the key's sha256. A label is a short plain name; anything that
|
||||
// looks like a Mosaic key prefix is dropped, so an answer can never carry key text.
|
||||
const KEY_ID = /^[a-f0-9]{12}$/;
|
||||
const plainLabel = value => (typeof value === 'string' && /^[A-Za-z0-9][A-Za-z0-9._:-]{0,63}$/.test(value) && !/ms[kotp][-_]/i.test(value) ? value : null);
|
||||
const isoTime = value => (typeof value === 'string' && /^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:.]{1,18}Z$/.test(value) ? value : null);
|
||||
|
||||
// `buses()` proves the init-lock condition "bus stopped" ({stopped, running:[label]}).
|
||||
// `orgs()` lists the organizations this proxy serves: [{id, catalogue}]. `unit` and `fetch` are
|
||||
// (and `pin`) seams for tests; the defaults run the real proxy-service.sh, systemctl and pinned fetch.
|
||||
@@ -20,7 +75,11 @@ const failure = code => Object.assign(new Error(code), { code });
|
||||
// change log, `call(verb, args)` reaches its bus as the owner and `status()` is its bus status. `ownerModels()` answers
|
||||
// {proxy, riding, models} from the riding buses' catalogues. `move(id, models, principal)` moves a
|
||||
// stopped fixture organization to the owner's models through the organization service.
|
||||
export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir = null, unit = null, fetch = fetchPinned, buses, orgs = async () => [], riders = async () => [], ownerModels = async () => ({ proxy: false, riding: false, models: [] }), move = null, now = () => new Date(), env = process.env, pin = PIN }) {
|
||||
// #1873 settings-backend A2: a rider may also have `file()`, its organization file as the writer
|
||||
// reads it ({document, revision}), read only. `harnesses()` lists the installed harness programs.
|
||||
// `rootAgents()` reads the root agent file (loadRootAgents); with none given, or one that throws,
|
||||
// no organization that has the program on is written (unreadable).
|
||||
export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir = null, unit = null, fetch = fetchPinned, buses, orgs = async () => [], riders = async () => [], harnesses = () => installedHarnesses(dataRoot), rootAgents = null, ownerModels = async () => ({ proxy: false, riding: false, models: [] }), move = null, now = () => new Date(), env = process.env, pin = PIN }) {
|
||||
const port = () => { try { return initState(configDir, dataRoot).settings?.port ?? null; } catch { return null; } };
|
||||
const control = unit ?? createUnitControl({ instance, unitDir, port, env });
|
||||
let job = { state: 'idle', step: null, receipt: null, error: null };
|
||||
@@ -60,17 +119,24 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
// intent. Any other failure leaves it: the add may have happened.
|
||||
async function rideOne(rider, wasRidden) {
|
||||
const id = rider.id, local = decide(rider, wasRidden);
|
||||
if (local === 'fixture') return { id, outcome: 'fixture', running: !(await stopped(rider)) };
|
||||
// settings-p3 (R2): with no bus status (the single-business Console) whether its bus runs is unknown (null).
|
||||
if (local === 'fixture') return { id, outcome: 'fixture', running: typeof rider.status === 'function' ? !(await stopped(rider)) : null };
|
||||
if (local !== 'ready') return { id, outcome: local === 'already' ? 'already' : 'skipped', reason: local };
|
||||
let current;
|
||||
try { current = await rider.call('plugins'); } catch { return { id, outcome: 'skipped', reason: 'bus-down' }; }
|
||||
try { await rider.call('proxy.accounts'); } catch (error) { return { id, outcome: 'skipped', reason: error?.code === 'not-configured' ? 'no-proxy-client' : 'proxy-check-failed' }; }
|
||||
const providers = Array.isArray(current?.plugins?.['model-provider']) ? current.plugins['model-provider'] : [];
|
||||
if (providers.includes('cliproxy')) return { id, outcome: 'already' };
|
||||
// The receipts are read again here: a removal saved while this run worked on earlier
|
||||
// organizations is on file now, and the decision above came from the run's first read.
|
||||
const now = ridden()(id);
|
||||
if (now !== false) return { id, outcome: 'skipped', reason: now === null ? 'unreadable' : 'removed' };
|
||||
// The organization (its file and change log) and the receipts are read again here: an add and
|
||||
// remove saved on its Settings page, or a removal saved while this run worked on earlier
|
||||
// organizations, is on file now, and the decision above came from the run's first read
|
||||
// (settings-p3 R1). A read that fails, or an organization no longer listed, is skipped as unreadable.
|
||||
let fresh;
|
||||
try { fresh = (await riders()).find(r => r?.id === id); } catch { fresh = null; }
|
||||
if (!fresh) return { id, outcome: 'skipped', reason: 'unreadable' };
|
||||
const again = decide(fresh, ridden());
|
||||
if (again === 'already') return { id, outcome: 'already' };
|
||||
if (again !== 'ready') return { id, outcome: 'skipped', reason: again };
|
||||
try { receipt({ op: 'ride-intent', org: id }); } catch { return { id, outcome: 'skipped', reason: 'not-recorded' }; }
|
||||
try { await rider.call('plugins.set', { revision: current.revision, plugins: { 'model-provider': [...providers, 'cliproxy'] } }); }
|
||||
catch (error) {
|
||||
@@ -83,7 +149,89 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
return { id, outcome: 'added' };
|
||||
}
|
||||
async function rideSummary() {
|
||||
try { const wasRidden = ridden(); return (await riders()).map(r => ({ id: r.id, state: decide(r, wasRidden) })); } catch { return null; }
|
||||
// settings-p3 (review P3-3): a stand-in organization with no bus status (the single-business
|
||||
// Console) can't be moved; running null says so before any Ride all run.
|
||||
try {
|
||||
const wasRidden = ridden();
|
||||
return (await riders()).map(r => { const state = decide(r, wasRidden); return state === 'fixture' && typeof r.status !== 'function' ? { id: r.id, state, running: null } : { id: r.id, state }; });
|
||||
} catch { return null; }
|
||||
}
|
||||
// #1873 settings-backend A2, item 5: which harness programs each organization has on, from its
|
||||
// bus's plugins read as the owner. null when the read fails, its answer has no harness list, or
|
||||
// any entry is not a plugin name (review P3-3: no bus text reaches the page).
|
||||
const harnessList = value => (Array.isArray(value?.plugins?.harness) && value.plugins.harness.every(n => typeof n === 'string' && /^[a-z][a-z0-9-]{0,63}$/.test(n)) ? [...value.plugins.harness] : null);
|
||||
async function programsSummary() {
|
||||
let installed = null;
|
||||
try { installed = harnesses(); if (!Array.isArray(installed)) installed = null; } catch { installed = null; }
|
||||
let list;
|
||||
try { list = await riders(); } catch { return { installed, organizations: null }; }
|
||||
const organizations = await Promise.all(list.map(async rider => {
|
||||
try { return { id: rider.id, on: harnessList(await rider.call('plugins')) }; } catch { return { id: rider.id, on: null }; }
|
||||
}));
|
||||
return { installed, organizations };
|
||||
}
|
||||
// Item 5, one organization: turn `name` off unless something still uses it or it is the only one.
|
||||
// Three reads (the plugins read, the routes view and the organization file) must show one revision;
|
||||
// the write carries it, so a change after them is refused by the writer (revision-conflict) and
|
||||
// reported, never retried. Every skip writes nothing. `root` is the root agent file read once per
|
||||
// call (undefined when it could not be read): the root agents this organization takes count too.
|
||||
async function offOne(rider, name, root) {
|
||||
const id = rider.id;
|
||||
let current;
|
||||
try { current = await rider.call('plugins'); } catch { return { id, outcome: 'bus-down' }; }
|
||||
const on = harnessList(current);
|
||||
if (on === null || typeof current?.revision !== 'string' || !/^[a-f0-9]{64}$/.test(current.revision)) return { id, outcome: 'unreadable' };
|
||||
if (!on.includes(name)) return { id, outcome: 'already-off' };
|
||||
let routes = null;
|
||||
try { routes = await rider.call('routes'); } catch (error) { if (error?.code !== 'launch-not-configured') return { id, outcome: 'unreadable' }; }
|
||||
let file;
|
||||
try { file = typeof rider.file === 'function' ? await rider.file() : null; } catch { file = null; }
|
||||
const named = harnessesNamed(file?.document);
|
||||
const fromRoot = root === undefined ? null : rootHarnessesNamed(root, id);
|
||||
if (named === null || fromRoot === null || typeof file?.revision !== 'string') return { id, outcome: 'unreadable' };
|
||||
for (const harness of fromRoot) named.add(harness);
|
||||
if (file.revision !== current.revision || (routes !== null && routes?.revision !== current.revision)) return { id, outcome: 'failed:revision-conflict' };
|
||||
// The routes view must be readable when there is one; each instance's route and fallback count.
|
||||
if (routes !== null) {
|
||||
if (!routes || typeof routes.instances !== 'object' || routes.instances === null) return { id, outcome: 'unreadable' };
|
||||
for (const route of Object.values(routes.instances)) {
|
||||
if (typeof route?.harness === 'string') named.add(route.harness);
|
||||
if (typeof route?.fallback?.harness === 'string') named.add(route.fallback.harness);
|
||||
}
|
||||
}
|
||||
// The harness rule: any route on this program, whatever account its model uses, keeps it on.
|
||||
if (named.has(name)) return { id, outcome: 'in-use' };
|
||||
if (on.length === 1) return { id, outcome: 'last-program' };
|
||||
try { await rider.call('plugins.set', { revision: current.revision, plugins: { harness: on.filter(n => n !== name) } }); }
|
||||
catch (error) { return { id, outcome: `failed:${failCode(error)}` }; }
|
||||
return { id, outcome: 'off' };
|
||||
}
|
||||
// Item 8, one organization: its proxy client keys by key id, from proxy.keys as the owner. Never
|
||||
// the usage queue. A key value never reaches here: proxy.keys answers ids, and every other field
|
||||
// is checked against a plain pattern before it is passed on.
|
||||
async function accessOne(rider) {
|
||||
const id = rider.id;
|
||||
if (typeof rider.fixture !== 'boolean' || typeof rider.cliproxy !== 'boolean') return { id, health: 'unreadable', reason: 'unreadable', keys: [] };
|
||||
if (rider.fixture || !rider.cliproxy) return { id, health: 'not-riding', keys: [] };
|
||||
let answer;
|
||||
try { answer = await rider.call('proxy.keys'); }
|
||||
catch (error) {
|
||||
const code = error?.code;
|
||||
if (code === 'proxy-uncertain') return { id, health: 'attention', reason: 'proxy-uncertain', keys: [] };
|
||||
const reason = code === 'not-configured' ? 'no-proxy-client' : code === 'outcome-unknown' || code === 'no-bus-host' ? 'bus-down' : 'unreadable';
|
||||
return { id, health: 'unreadable', reason, keys: [] };
|
||||
}
|
||||
if (!Array.isArray(answer?.keys)) return { id, health: 'unreadable', reason: 'unreadable', keys: [] };
|
||||
const keys = [];
|
||||
let other = 0, sentinel = 0;
|
||||
for (const key of answer.keys) {
|
||||
if (!key || typeof key !== 'object' || !KEY_ID.test(key.keyId ?? '')) return { id, health: 'unreadable', reason: 'unreadable', keys: [] };
|
||||
if (key.management === 'sentinel') sentinel++;
|
||||
else if (key.management === 'stack-managed') keys.push({ keyId: key.keyId, agent: plainLabel(key.agent), state: plainLabel(key.state), issuedAt: isoTime(key.issuedAt) });
|
||||
else other++;
|
||||
}
|
||||
const health = keys.length === 0 ? 'missing' : keys.every(k => k.state === 'active') ? 'ok' : 'attention';
|
||||
return { id, health, keys, other, sentinel };
|
||||
}
|
||||
function nextSteps({ release, init, unitState, fence, lock }) {
|
||||
if (!release.fetched || release.problem) return ['fetch'];
|
||||
@@ -110,6 +258,7 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
let organizations = null;
|
||||
try { organizations = await orgs(); } catch { organizations = null; }
|
||||
const ride = await rideSummary();
|
||||
const programs = await programsSummary();
|
||||
const next = nextSteps({ release, init, unitState, fence, lock });
|
||||
// Ride all comes after the proxy runs with no fence, while an organization can still ride.
|
||||
if (next[0] === 'accounts' && ride?.some(r => r.state === 'ready' || r.state === 'fixture')) next.push('ride-all');
|
||||
@@ -131,6 +280,7 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
// needs a config format that does not exist yet.
|
||||
ownProxy: { available: false, state: 'not built', reason: 'This instance has one shared proxy. A separate proxy per organization is not built yet.' },
|
||||
ride,
|
||||
programs,
|
||||
next,
|
||||
};
|
||||
},
|
||||
@@ -221,6 +371,9 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
if (!rider) return { org, moved: false, reason: 'unknown' };
|
||||
const local = decide(rider, ridden());
|
||||
if (local !== 'fixture') return { org, moved: false, reason: local };
|
||||
// settings-p3 (R2): with no bus status (the single-business Console) its bus can't be shown
|
||||
// stopped, so stopping it would not help: moving is not available there.
|
||||
if (typeof rider.status !== 'function') return { org, moved: false, reason: 'not-available' };
|
||||
if (!(await stopped(rider))) return { org, moved: false, reason: 'bus-running' };
|
||||
if (typeof move !== 'function') return { org, moved: false, reason: 'not-available' };
|
||||
const owner = await ownerModels();
|
||||
@@ -239,6 +392,65 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
if (typeof org !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(org)) throw failure('proxy-org-invalid');
|
||||
return receipt({ op: 'ride-removed', org });
|
||||
},
|
||||
// #1873 settings-backend A1: three reads for the instance owner's setup page.
|
||||
// The proxy unit's last journal lines, redacted. A unit that is not installed is not-configured;
|
||||
// a unit state that cannot be read is outcome-unknown, as is a journal that cannot be read.
|
||||
async log({ lines = LOG_LINES.default } = {}) {
|
||||
if (typeof control.log !== 'function') throw failure('not-configured');
|
||||
const state = await unitStatus();
|
||||
if (state.error) throw failure('outcome-unknown');
|
||||
if (!state.installed) throw failure('not-configured');
|
||||
return control.log(lines, { secrets: secretValues(env) });
|
||||
},
|
||||
// When the service key was made, from proxy.json's mtime; never the key or any part of it.
|
||||
serviceKey() { return serviceKeyState(configDir); },
|
||||
// The setup history, a page at a time (receiptHistory).
|
||||
history({ limit, before = null } = {}) { return receiptHistory(dataRoot, { limit, before, secrets: secretValues(env) }); },
|
||||
// An account change made through /api/proxy/* or an organization's /api/orgs/<id>/proxy/*,
|
||||
// recorded after its bus call answered. Labels only: the provider id, a 12-hex account id, a
|
||||
// priority or a model count, the organization whose route carried it, and the signed-in name.
|
||||
// Never a key, a login state, a callback URL, a model list or a body. Answers null when the verb
|
||||
// is not an account change, true when recorded and false when the append failed.
|
||||
recordAccount({ verb, args = {}, result = null, user = null, org = null } = {}) {
|
||||
const label = value => (typeof value === 'string' && /^[a-z0-9][a-z0-9._-]{0,31}$/.test(value) ? value : null);
|
||||
const account = value => (typeof value === 'string' && /^[a-f0-9]{12}$/.test(value) ? value : null);
|
||||
let record;
|
||||
if (verb === 'proxy.apikey.add') record = { op: 'account.proxy.apikey.add', provider: label(args.provider) };
|
||||
else if (verb === 'proxy.login.status' && result?.status === 'ok') record = { op: 'account.proxy.login.complete' };
|
||||
else if (verb === 'proxy.priority.set') record = { op: 'account.proxy.priority.set', accountId: account(args.accountId), priority: Number.isSafeInteger(args.priority) ? args.priority : null };
|
||||
else if (verb === 'proxy.group.models.set') record = { op: 'account.proxy.group.models.set', accountId: account(args.accountId), models: Array.isArray(args.models) ? args.models.length : null };
|
||||
else return null;
|
||||
if (typeof org === 'string' && /^[a-z0-9][a-z0-9._-]{0,63}$/.test(org)) record.org = org;
|
||||
if (typeof user === 'string' && user) record.user = user.slice(0, 64);
|
||||
try { receipt(record); return true; } catch { return false; }
|
||||
},
|
||||
// #1873 settings-backend A2, item 5: turns one harness program off for every current
|
||||
// organization that can do without it, through each organization's bus as the owner (the way
|
||||
// Ride all writes). Sequential, one organization at a time; one receipt names the outcome ids.
|
||||
async programsOff({ name, user = null } = {}) {
|
||||
return exclusive('programs', async () => {
|
||||
let installed;
|
||||
try { installed = harnesses(); } catch { installed = null; }
|
||||
if (typeof name !== 'string' || !Array.isArray(installed) || !installed.includes(name)) throw failure('invalid-request');
|
||||
const list = await riders();
|
||||
let root;
|
||||
try { root = typeof rootAgents === 'function' ? await rootAgents() : undefined; } catch { root = undefined; }
|
||||
const results = [];
|
||||
for (const rider of list) results.push(await offOne(rider, name, root));
|
||||
const record = { op: 'programs.off', name, results: results.map(r => ({ id: r.id, outcome: r.outcome })) };
|
||||
if (typeof user === 'string' && user) record.user = user.slice(0, 64);
|
||||
// Each write is the organization's own change log entry, so a receipt that cannot be
|
||||
// written still answers what happened, with no receipt.
|
||||
let written = null;
|
||||
try { written = receipt(record); } catch { written = null; }
|
||||
return { name, results, receipt: written };
|
||||
});
|
||||
},
|
||||
// #1873 settings-backend A2, item 8: each organization's proxy client keys and their health.
|
||||
async access() {
|
||||
const list = await riders();
|
||||
return { organizations: await Promise.all(list.map(rider => accessOne(rider).catch(() => ({ id: rider.id, health: 'unreadable', reason: 'unreadable', keys: [] })))) };
|
||||
},
|
||||
async initLockClear() {
|
||||
return exclusive('init-lock-clear', async () => ({ receipt: clearInitLock({ configDir, dataRoot, buses: await buses(), now }) }));
|
||||
},
|
||||
|
||||
@@ -9,6 +9,7 @@ import { execFile } from 'node:child_process';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { PIN, refuse } from './common.mjs';
|
||||
import { installPaths, loadProxy, verifyInstall } from './settings.mjs';
|
||||
import { holdsSecret, redactLine, redactValue } from './redact.mjs';
|
||||
|
||||
const REPO = fileURLToPath(new URL('../../../', import.meta.url)).replace(/\/$/, '');
|
||||
export const INSTANCE_NAME = /^[a-z][a-z0-9]{1,15}$/;
|
||||
@@ -142,6 +143,135 @@ export function recentReceipts(dataRoot, limit = 10) {
|
||||
} finally { fs.closeSync(fd); }
|
||||
}
|
||||
|
||||
// #1873 settings-backend A1 (item 9): the setup history, one page at a time, newest page first and
|
||||
// oldest first within a page. `before` is a byte offset the previous page answered as `next`: the
|
||||
// start of a line. The file is read the way ownedText reads (no symlink, no FIFO, owned by the
|
||||
// Console's user, no group or other bits) and backward from `before` in 64 KB chunks. A partial
|
||||
// last line (a write in progress) waits for a later read; a line that does not parse, or is longer
|
||||
// than 64 KB, is skipped. Each entry is redacted (redact.mjs) before it is answered, so a receipt
|
||||
// that ever held key text shows none. An entry whose redacted values read together, or whose keys
|
||||
// and values read together, still hold an environment secret split across them is answered as
|
||||
// { withheld: true }; pieces kept apart by another whole field are not seen (known limit). A missing
|
||||
// file is an empty history; any other failure to read refuses outcome-unknown. `uid` is a test seam.
|
||||
export const HISTORY_LIMIT = Object.freeze({ min: 1, max: 100, default: 25 });
|
||||
const HISTORY_CHUNK = 65536, HISTORY_LINE_MAX = 65536, HISTORY_SCAN_MAX = 2000;
|
||||
// A redacted entry's values joined in order, with each object key before its value when `keys`.
|
||||
const entryText = (value, keys) => (value !== null && typeof value === 'object'
|
||||
? Object.entries(value).map(([key, item]) => (keys && !Array.isArray(value) ? key : '') + entryText(item, keys)).join('')
|
||||
: String(value));
|
||||
export function receiptHistory(dataRoot, { limit = HISTORY_LIMIT.default, before = null, uid = process.getuid(), secrets = [] } = {}) {
|
||||
if (!Number.isSafeInteger(limit)) refuse('invalid-request');
|
||||
const n = Math.min(HISTORY_LIMIT.max, Math.max(HISTORY_LIMIT.min, limit));
|
||||
if (before !== null && !(Number.isSafeInteger(before) && before >= 0)) refuse('invalid-request');
|
||||
let fd;
|
||||
try { fd = fs.openSync(receiptFile(dataRoot), fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK); }
|
||||
catch (error) {
|
||||
if (error.code !== 'ENOENT') refuse('outcome-unknown');
|
||||
if (before !== null && before !== 0) refuse('invalid-request');
|
||||
return { entries: [], next: null };
|
||||
}
|
||||
try {
|
||||
let stat;
|
||||
try { stat = fs.fstatSync(fd); } catch { refuse('outcome-unknown'); }
|
||||
if (!stat.isFile() || stat.uid !== uid || (stat.mode & 0o077)) refuse('outcome-unknown');
|
||||
const size = stat.size;
|
||||
if (before !== null && before > size) refuse('invalid-request');
|
||||
// A window over the file: `buf` holds bytes [start, start + buf.length).
|
||||
let start = 0, buf = Buffer.alloc(0);
|
||||
const readAt = (offset, length) => {
|
||||
const out = Buffer.alloc(length);
|
||||
let got;
|
||||
try { got = fs.readSync(fd, out, 0, length, offset); } catch { refuse('outcome-unknown'); }
|
||||
if (got !== length) refuse('outcome-unknown');
|
||||
return out;
|
||||
};
|
||||
// The offset of the last newline in [0, from), or -1.
|
||||
const lastNewline = from => {
|
||||
let pos = from;
|
||||
while (pos > 0) {
|
||||
if (!(pos > start && pos <= start + buf.length)) { start = Math.max(0, pos - HISTORY_CHUNK); buf = readAt(start, pos - start); }
|
||||
const i = buf.lastIndexOf(0x0a, pos - start - 1);
|
||||
if (i >= 0) return start + i;
|
||||
pos = start;
|
||||
}
|
||||
return -1;
|
||||
};
|
||||
const bytes = (from, to) => (from >= start && to <= start + buf.length ? buf.subarray(from - start, to - start) : readAt(from, to - from));
|
||||
// `cursor` is always 0 or the offset just after a newline: the end of the next line to read.
|
||||
let cursor;
|
||||
if (before === null) cursor = lastNewline(size) + 1;
|
||||
else if (before > 0 && bytes(before - 1, before)[0] !== 0x0a) refuse('invalid-request');
|
||||
else cursor = before;
|
||||
const entries = [];
|
||||
for (let scanned = 0; cursor > 0 && entries.length < n && scanned < HISTORY_SCAN_MAX; scanned++) {
|
||||
const lineStart = lastNewline(cursor - 1) + 1, lineEnd = cursor - 1;
|
||||
if (lineEnd - lineStart <= HISTORY_LINE_MAX) {
|
||||
let value = null;
|
||||
try { value = JSON.parse(bytes(lineStart, lineEnd).toString('utf8')); } catch { /* torn line */ }
|
||||
if (value && typeof value === 'object' && !Array.isArray(value) && typeof value.op === 'string') {
|
||||
const safe = redactValue(value, secrets);
|
||||
const split = holdsSecret(entryText(safe, false), secrets) || holdsSecret(entryText(safe, true), secrets);
|
||||
entries.push(split ? { withheld: true } : safe);
|
||||
}
|
||||
}
|
||||
cursor = lineStart;
|
||||
}
|
||||
entries.reverse();
|
||||
return { entries, next: cursor > 0 ? cursor : null };
|
||||
} finally { fs.closeSync(fd); }
|
||||
}
|
||||
|
||||
// #1873 settings-backend A1 (item 7): when the service key was made. proxy.json is created in the
|
||||
// same init step as the management key, with exclusive create, and is never rewritten, so its
|
||||
// mtime is the key's creation time. Only proxy.json is lstat'ed: the key file is never opened or
|
||||
// stat'ed, and nothing is read. A missing proxy.json is `missing`; a symlink, a non-file, one owned
|
||||
// by another user or one that cannot be examined is `unreadable`, with no date. There is no
|
||||
// renewal yet (proxy-scope: no rotation in the MVP), so renewedAt is null and renewal 'not-built'.
|
||||
export function serviceKeyState(configDir, { uid = process.getuid() } = {}) {
|
||||
const out = { madeAt: null, renewedAt: null, renewal: 'not-built', problem: null };
|
||||
let stat;
|
||||
try { stat = fs.lstatSync(path.join(configDir, 'proxy.json')); }
|
||||
catch (error) { out.problem = error.code === 'ENOENT' ? 'missing' : 'unreadable'; return out; }
|
||||
if (!stat.isFile() || stat.uid !== uid || !Number.isFinite(stat.mtimeMs) || stat.mtimeMs <= 0) { out.problem = 'unreadable'; return out; }
|
||||
out.madeAt = new Date(stat.mtimeMs).toISOString();
|
||||
return out;
|
||||
}
|
||||
|
||||
// #1873 settings-backend A1 (item 3): the proxy unit's last journal lines, made safe to show. The
|
||||
// unit name is unitName()'s, never the caller's; `lines` is clamped to 1..200. journalctl runs
|
||||
// through `run` (defaultRun: its timeout and 1 MB cap). A missing journalctl, a nonzero exit or a
|
||||
// timeout refuses outcome-unknown, with no lines and no other source tried. Each line is redacted
|
||||
// (redact.mjs) and cut to 300 characters; a line that cannot be shown is dropped, never shown raw.
|
||||
// A secret from `secrets` found across the shown lines, though in no one line, drops every line.
|
||||
export const LOG_LINES = Object.freeze({ min: 1, max: 200, default: 50 });
|
||||
export async function unitLog({ run = defaultRun, unit, lines = LOG_LINES.default, env, secrets = [] } = {}) {
|
||||
if (typeof unit !== 'string' || !/^mosaic-proxy(-[a-z][a-z0-9]{1,15})?\.service$/.test(unit)) refuse('proxy-unit-usage');
|
||||
if (!Number.isSafeInteger(lines)) refuse('invalid-request');
|
||||
const n = Math.min(LOG_LINES.max, Math.max(LOG_LINES.min, lines));
|
||||
let result;
|
||||
try { result = await run('journalctl', ['--user', '-u', unit, '-n', String(n), '-o', 'cat', '--no-pager', '-q'], { env, timeout: 15000 }); }
|
||||
catch { refuse('outcome-unknown'); }
|
||||
if (!result || result.code !== 0 || typeof result.stdout !== 'string') refuse('outcome-unknown');
|
||||
const raw = result.stdout.split('\n');
|
||||
if (raw.at(-1) === '') raw.pop();
|
||||
const kept = raw.slice(-n);
|
||||
const out = [];
|
||||
let redacted = 0, dropped = 0;
|
||||
try {
|
||||
for (const line of kept) {
|
||||
const safe = redactLine(line, secrets);
|
||||
if (safe.text === null) { if (line.trim()) dropped++; continue; }
|
||||
if (safe.redacted) redacted++;
|
||||
out.push(safe.text);
|
||||
}
|
||||
// The lines that will be shown, read together: a secret split across them drops them all.
|
||||
// Only environment secrets are checked this way. A key pattern broken across two journal
|
||||
// lines, each part too short for the long-run rules, is a known limit shared with chat-fail.
|
||||
if (holdsSecret(out.join(''), secrets)) { dropped += out.length; out.length = 0; }
|
||||
} catch { refuse('outcome-unknown'); }
|
||||
return { unit, lines: out, redacted, dropped };
|
||||
}
|
||||
|
||||
export function unitName(instance) {
|
||||
if (instance !== null && instance !== undefined && instance !== 'dev' && !INSTANCE_NAME.test(instance)) refuse('proxy-unit-usage');
|
||||
return instance && instance !== 'dev' ? `mosaic-proxy-${instance}.service` : 'mosaic-proxy.service';
|
||||
@@ -215,6 +345,8 @@ export function createUnitControl({ instance = null, port = () => null, unitDir
|
||||
return {
|
||||
unit,
|
||||
status,
|
||||
// settings-backend A1: the unit's last journal lines, redacted (unitLog).
|
||||
log(lines, { secrets = [] } = {}) { return unitLog({ run, unit, lines, env, secrets }); },
|
||||
async render() { return (await service('render')).stdout; },
|
||||
async install() { await service('install'); return status(); },
|
||||
async start() {
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
// #1873 settings-backend A1: redaction for the model service's log lines and the setup history,
|
||||
// read by the instance owner on the setup page.
|
||||
//
|
||||
// It is the redaction chat-fail landed for a failed turn's cause (packages/harness/src/failure-notice.mjs,
|
||||
// its REDACT list), with Mosaic's own key prefixes added to the key-prefix line: mso- (an organization's
|
||||
// model key), mst- (the Triage key) and msp-, beside msk- (the owner's). One line is added after it:
|
||||
// those four Mosaic prefixes with no word boundary, so a key glued to a word (xmso-...) is caught
|
||||
// too. Over-redaction there is the safe side. failure-notice.mjs does not
|
||||
// export its list, and packages/harness/ belongs to another lane now, so the list is copied here.
|
||||
// tests/redact.test.mjs fails when a copied line no longer matches failure-notice.mjs.
|
||||
//
|
||||
// A text is decoded (%XX, HTML references, NFKC) and cleaned (ANSI, controls, whitespace) with
|
||||
// failure-notice's own exported helpers before it is redacted. A text that holds one of the
|
||||
// Console's environment secrets (secretValues), even split by spaces or controls, is withheld
|
||||
// whole. A text that redacts to nothing but [redacted] says nothing and is dropped.
|
||||
import { cleanText, decodeText } from '../../harness/src/failure-notice.mjs';
|
||||
|
||||
export { secretValues } from '../../harness/src/failure-notice.mjs';
|
||||
|
||||
export const LINE_MAX = 300;
|
||||
export const REDACTED = '[redacted]';
|
||||
|
||||
// Copied from failure-notice.mjs. The key-prefix line adds mso, mst and msp, and the line after
|
||||
// it is Mosaic's own; every other line is failure-notice's.
|
||||
const KEY_WORD = "(?:\\w*[_-])?(?:api[_-]?key|key|token|secret|password|passwd|authorization|cookie|session)";
|
||||
const REDACT = [
|
||||
/(?<![a-z0-9+.-])[a-z0-9+.-]+:\/\/\S*/gi, // any link, its userinfo and query included, from the run's start
|
||||
/\bwww\.\S+/gi,
|
||||
/(?<!\S)(?=\S*\S@\S)\S+/g, // email addresses and user@host: the whole run holding the @
|
||||
/(?:^|(?<=[\s("'=:]))(?:~|\.{1,2})?\/\S+/g, // absolute and home paths
|
||||
/\b[A-Za-z]:\\\S*/g, // Windows paths
|
||||
/\\\\\S+/g, // UNC paths
|
||||
/\b(?:bearer|basic)\s+\S+/gi,
|
||||
new RegExp(`\\b${KEY_WORD}["']?\\s*[:=]\\s*(?:"[^"]*"?|'[^']*'?|[^\\s,;}]+)`, "gi"),
|
||||
new RegExp(`\\b${KEY_WORD}["']?\\s+(?:"[^"]*"?|'[^']*'?|(?=[^\\s"',;}]*\\d)[^\\s"',;}]+|[^\\s"',;}]{8,})`, "gi"),
|
||||
/\b(?:sk|msk|mso|mst|msp|rk|pk|ghp|gho|ghs|glpat|xox[a-z])[-_]\S+/gi,
|
||||
/ms[kotp][-_]\S+/gi, // Mosaic's prefixes glued to a word: added here, not in failure-notice.mjs
|
||||
/\beyJ\S+/g, // JWT
|
||||
/[A-Za-z0-9_\-+/=.]{32,}/g,
|
||||
/(?<![A-Za-z0-9_\-+/=.])(?=[A-Za-z0-9_\-+/=.]*\d)(?=[A-Za-z0-9_\-+/=.]*[A-Za-z])[A-Za-z0-9_\-+/=.]{16,}/g,
|
||||
];
|
||||
|
||||
// The text with every space, mark and separator gone, after cleanText, for the secret check.
|
||||
// cleanText has already removed ANSI, format, surrogate and private-use characters and turned
|
||||
// controls into spaces, so this removes the same set failure-notice's compactText does.
|
||||
const compact = clean => clean.replace(/[\p{M}\p{Z}\s]/gu, '');
|
||||
|
||||
function cap(text, n = LINE_MAX) {
|
||||
const chars = Array.from(text);
|
||||
return chars.length <= n ? text : chars.slice(0, n - 3).join('') + '...';
|
||||
}
|
||||
|
||||
// One text made safe to show. Answers { text, redacted } where text is null when nothing can be
|
||||
// shown: an empty text, one holding a secret (withheld: true), or one that redacts to only
|
||||
// [redacted]. Redaction runs over the whole text before the cap, so the cut never parts a key word
|
||||
// from its value. `secrets` is secretValues(env).
|
||||
export function redactLine(line, secrets = []) {
|
||||
const clean = cleanText(decodeText(String(line)));
|
||||
if (!clean) return { text: null, redacted: false };
|
||||
if (secrets.length) {
|
||||
const flat = compact(clean);
|
||||
if (secrets.some(value => flat.includes(value))) return { text: null, redacted: true, withheld: true };
|
||||
}
|
||||
let shown = clean;
|
||||
for (const re of REDACT) shown = shown.replace(re, REDACTED);
|
||||
shown = shown.replace(/(?:\[redacted\]\s*){2,}/g, `${REDACTED} `).trim();
|
||||
if (!shown || shown === REDACTED) return { text: null, redacted: true };
|
||||
return { text: cap(shown), redacted: shown !== clean };
|
||||
}
|
||||
|
||||
// True when the whole text, joined with no spaces, holds a secret: one split across lines is seen.
|
||||
export function holdsSecret(text, secrets = []) {
|
||||
if (!secrets.length) return false;
|
||||
const flat = compact(cleanText(decodeText(String(text))));
|
||||
return secrets.some(value => flat.includes(value));
|
||||
}
|
||||
|
||||
// A stored value made safe to show: strings are redacted, and a string that cannot be shown
|
||||
// becomes [redacted]. Keys that redaction would change are dropped with their value. Depth and
|
||||
// size are bounded; anything past the bound becomes null.
|
||||
export function redactValue(value, secrets = [], depth = 0) {
|
||||
if (value === null || typeof value === 'number' || typeof value === 'boolean') return value;
|
||||
if (typeof value === 'string') return value === '' ? '' : redactLine(value, secrets).text ?? REDACTED;
|
||||
if (depth >= 4 || typeof value !== 'object') return null;
|
||||
if (Array.isArray(value)) return value.slice(0, 64).map(item => redactValue(item, secrets, depth + 1));
|
||||
const out = {};
|
||||
for (const [key, item] of Object.entries(value).slice(0, 64)) {
|
||||
if (!/^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(key) || redactLine(key, secrets).text !== key) continue;
|
||||
out[key] = redactValue(item, secrets, depth + 1);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -7,7 +7,10 @@ import fs from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { createProxyAdmin } from '../src/admin-control.mjs';
|
||||
import { createProxyAdmin, installedHarnesses, harnessesNamed, rootHarnessesNamed } from '../src/admin-control.mjs';
|
||||
import { pluginHash } from '../../plugins/src/pinned.mjs';
|
||||
import { installPlugin } from '../../plugins/src/install.mjs';
|
||||
import { KINDS } from '../../plugins/src/kinds.mjs';
|
||||
import { initFiles, receiptFile } from '../src/admin.mjs';
|
||||
import { PIN } from '../src/common.mjs';
|
||||
|
||||
@@ -15,7 +18,7 @@ const code = name => error => error.code === name;
|
||||
const SENTINEL = 'pa-sentinel-management-key-0123456789';
|
||||
const stopped = async () => ({ stopped: true, running: [] });
|
||||
|
||||
function fixture(t, { unitState = { installed: false, active: 'inactive', healthy: false }, restart = null, fetch = null, pin = PIN, statusHook = null } = {}) {
|
||||
function fixture(t, { unitState = { installed: false, active: 'inactive', healthy: false }, restart = null, fetch = null, pin = PIN, statusHook = null, extra = {} } = {}) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'pc-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
@@ -30,7 +33,7 @@ function fixture(t, { unitState = { installed: false, active: 'inactive', health
|
||||
stop: async () => { ops.push('stop'); return { ...unitState, active: 'inactive', pid: null }; },
|
||||
restart: async () => { ops.push('restart'); return restart ?? { state: unitState, evidence: { unit: 'mosaic-proxy-zz.service', confirmed: true, before: { pid: 10, since: 1, active: 'active' }, after: { pid: 11, since: 2, active: 'active', listening: true }, observedAt: new Date(0).toISOString() } }; },
|
||||
};
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit, buses: stopped, fetch: fetch ?? (async () => ({})), now: () => new Date(0), orgs: async () => [{ id: 'acme', catalogue: 'ready' }], pin });
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit, buses: stopped, fetch: fetch ?? (async () => ({})), now: () => new Date(0), orgs: async () => [{ id: 'acme', catalogue: 'ready' }], pin, ...extra });
|
||||
return { root, configDir, dataRoot, admin, ops, files: initFiles(configDir, dataRoot) };
|
||||
}
|
||||
const receipts = dataRoot => { try { return fs.readFileSync(receiptFile(dataRoot), 'utf8').trim().split('\n').filter(Boolean).map(JSON.parse); } catch { return []; } };
|
||||
@@ -227,3 +230,340 @@ test('status carries the last receipts from proxy-admin.jsonl, so the page shows
|
||||
assert.equal(receipts.length, 10);
|
||||
assert.ok(receipts.every(r => r.op === 'unit.stop'));
|
||||
});
|
||||
|
||||
// #1873 settings-backend A1: the controller's three reads and the account-change record.
|
||||
function a1Fixture(t, { unitState = { installed: true, active: 'active', healthy: true }, log = undefined, statusError = false, env = {} } = {}) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'pc-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
fs.mkdirSync(configDir, { mode: 0o700 }); fs.mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const calls = [];
|
||||
const unit = {
|
||||
unit: 'mosaic-proxy-zz.service',
|
||||
status: async () => { if (statusError) throw Object.assign(new Error('systemctl gone'), { code: 'proxy-unit-failed' }); return { unit: 'mosaic-proxy-zz.service', ...unitState }; },
|
||||
...(log === undefined ? { log: async (lines, options) => { calls.push({ lines, options }); return { unit: 'mosaic-proxy-zz.service', lines: ['ok'], redacted: 0, dropped: 0 }; } } : log === null ? {} : { log }),
|
||||
};
|
||||
const admin = createProxyAdmin({ dataRoot, configDir, unit, buses: stopped, now: () => new Date(0), env });
|
||||
return { admin, calls, configDir, dataRoot };
|
||||
}
|
||||
|
||||
test('log reads the unit journal with the env secrets; a missing unit or log reader is not-configured, an unreadable state outcome-unknown', async t => {
|
||||
const { admin, calls } = a1Fixture(t, { env: { MOSAIC_PROXY_KEY: 'plainwordsonly', PATH: '/usr/bin' } });
|
||||
assert.deepEqual(await admin.log(), { unit: 'mosaic-proxy-zz.service', lines: ['ok'], redacted: 0, dropped: 0 });
|
||||
assert.deepEqual(calls, [{ lines: 50, options: { secrets: ['plainwordsonly'] } }]);
|
||||
await admin.log({ lines: 7 });
|
||||
assert.equal(calls[1].lines, 7);
|
||||
await assert.rejects(a1Fixture(t, { unitState: { installed: false, active: 'inactive', healthy: false } }).admin.log(), code('not-configured'));
|
||||
await assert.rejects(a1Fixture(t, { log: null }).admin.log(), code('not-configured'));
|
||||
await assert.rejects(a1Fixture(t, { statusError: true }).admin.log(), code('outcome-unknown'));
|
||||
const failing = a1Fixture(t, { log: async () => { throw Object.assign(new Error('journal'), { code: 'outcome-unknown' }); } });
|
||||
await assert.rejects(failing.admin.log(), code('outcome-unknown'));
|
||||
});
|
||||
|
||||
test('serviceKey and history read the configured directories', async t => {
|
||||
const { admin, configDir, dataRoot } = a1Fixture(t);
|
||||
assert.equal(admin.serviceKey().problem, 'missing');
|
||||
fs.writeFileSync(join(configDir, 'proxy.json'), '{}', { mode: 0o600 });
|
||||
assert.equal(admin.serviceKey().problem, null);
|
||||
assert.match(admin.serviceKey().madeAt, /^\d{4}-\d\d-\d\dT/);
|
||||
assert.deepEqual(admin.history(), { entries: [], next: null });
|
||||
admin.recordAccount({ verb: 'proxy.login.status', result: { status: 'ok' } });
|
||||
assert.deepEqual(admin.history({ limit: 5 }), { entries: [{ time: new Date(0).toISOString(), op: 'account.proxy.login.complete' }], next: null });
|
||||
assert.equal(receipts(dataRoot).length, 1);
|
||||
});
|
||||
|
||||
test('history withholds an env secret a receipt holds', async t => {
|
||||
const { admin, dataRoot } = a1Fixture(t, { env: { PROXY_TOKEN: 'plainwordsonly' } });
|
||||
fs.mkdirSync(join(dataRoot, 'state'), { recursive: true, mode: 0o700 });
|
||||
fs.writeFileSync(receiptFile(dataRoot), JSON.stringify({ op: 'x', note: 'plain words only' }) + '\n', { mode: 0o600 });
|
||||
assert.deepEqual(admin.history().entries, [{ op: 'x', note: '[redacted]' }]);
|
||||
});
|
||||
|
||||
test('recordAccount records account changes by label only, never a key, login state, callback or model name', async t => {
|
||||
const { admin, dataRoot } = a1Fixture(t);
|
||||
const KEY = 'sk-proj-Ab3dE5fG7hJ9kL1mN3pQ5rS7tU9vW1xY';
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.apikey.add', args: { provider: 'openai', key: KEY }, result: { added: true }, user: 'jason', org: 'acme' }), true);
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.apikey.add', args: { provider: KEY, key: KEY } }), true);
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.login.status', args: { state: 'state-secret-123' }, result: { status: 'ok' } }), true);
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.login.status', args: { state: 'state-secret-123' }, result: { status: 'wait' } }), null);
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.priority.set', args: { accountId: '0123456789ab', priority: 2 } }), true);
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.group.models.set', args: { accountId: 'not-hex', models: ['secret-model-name', 'b'] }, org: '../x' }), true);
|
||||
for (const verb of ['proxy.login.start', 'proxy.login.submit', 'proxy.login.cancel', 'plugins.set', 'proxy.accounts', undefined]) {
|
||||
assert.equal(admin.recordAccount({ verb, args: { redirect_url: 'https://cb.example/?code=abc', provider: 'x' } }), null, String(verb));
|
||||
}
|
||||
const all = receipts(dataRoot);
|
||||
const at = new Date(0).toISOString();
|
||||
assert.deepEqual(all, [
|
||||
{ time: at, op: 'account.proxy.apikey.add', provider: 'openai', org: 'acme', user: 'jason' },
|
||||
{ time: at, op: 'account.proxy.apikey.add', provider: null },
|
||||
{ time: at, op: 'account.proxy.login.complete' },
|
||||
{ time: at, op: 'account.proxy.priority.set', accountId: '0123456789ab', priority: 2 },
|
||||
{ time: at, op: 'account.proxy.group.models.set', accountId: null, models: 2 },
|
||||
]);
|
||||
const text = fs.readFileSync(receiptFile(dataRoot), 'utf8');
|
||||
for (const bit of ['Ab3dE5', 'state-secret', 'secret-model', 'cb.example', 'code=abc']) assert.equal(text.includes(bit), false, bit);
|
||||
});
|
||||
|
||||
test('recordAccount answers false, and throws nothing, when the history cannot be written', async t => {
|
||||
const { admin, dataRoot } = a1Fixture(t);
|
||||
fs.mkdirSync(join(dataRoot, 'state'), { mode: 0o700 });
|
||||
fs.mkdirSync(receiptFile(dataRoot));
|
||||
assert.equal(admin.recordAccount({ verb: 'proxy.apikey.add', args: { provider: 'openai', key: 'k' } }), false);
|
||||
});
|
||||
|
||||
// #1873 settings-backend A2 (items 5 and 8): fake organizations behind the riders. The harness names
|
||||
// are made up; every answer carries a planted key, which no result, receipt or history entry may hold.
|
||||
const REV = 'a'.repeat(64), REV2 = 'b'.repeat(64);
|
||||
const PLANT = `mso-zz-acme-key-${'Q'.repeat(43)}`;
|
||||
const KEY_TEXT = /ms[kotp][-_][A-Za-z0-9_-]{6,}/;
|
||||
const busError = name => Object.assign(new Error(`refused ${PLANT}`), { code: name, reason: PLANT });
|
||||
function fakeOrg(id, { on = ['alpha', 'beta'], revision = REV, fileRevision = null, instances = {}, routesError = null, routesRevision = null, document = null, fileError = false, down = false, setError = null, keys = [], keysError = null, fixture = false, cliproxy = true, plugins = undefined } = {}) {
|
||||
const calls = [];
|
||||
const rider = {
|
||||
id, fixture, cliproxy, changes: '',
|
||||
file: () => { if (fileError) throw new Error(PLANT); return { document: document ?? { roles: {}, plugins: { harness: on }, secretNote: PLANT }, revision: fileRevision ?? revision }; },
|
||||
call: async (verb, args) => {
|
||||
calls.push(args === undefined ? [verb] : [verb, args]);
|
||||
if (down) throw busError('outcome-unknown');
|
||||
if (verb === 'plugins') return { business: id, revision, plugins: plugins !== undefined ? plugins : { harness: on, 'model-provider': ['cliproxy'] }, available: { harness: on }, note: PLANT };
|
||||
if (verb === 'routes') { if (routesError) throw busError(routesError); return { business: id, revision: routesRevision ?? revision, harnesses: on, models: ['m1'], providerModels: {}, levels: {}, instances, note: PLANT }; }
|
||||
if (verb === 'plugins.set') { if (setError) throw busError(setError); return { business: id, revision: REV2, plugins: args.plugins }; }
|
||||
if (verb === 'proxy.keys') { if (keysError) throw busError(keysError); return { keys }; }
|
||||
throw busError('invalid-request');
|
||||
},
|
||||
};
|
||||
return { rider, calls };
|
||||
}
|
||||
const NO_ROOT = { state: 'absent', agents: {}, assistant: null };
|
||||
const programsFixture = (t, orgs, extra = {}) => fixture(t, { extra: { riders: async () => orgs.map(o => o.rider), harnesses: () => ['alpha', 'beta', 'gamma'], rootAgents: () => NO_ROOT, ...extra } });
|
||||
const rootAgent = (vars, { definition = 'coder', excludeOrgs = [] } = {}) => ({ definition, title: 'Coder', vars, excludeOrgs });
|
||||
const route = harness => ({ harness, model: 'm1', thinking: null, fallback: null });
|
||||
|
||||
test('A2 item 5: the installed list comes from the harness plugin tree, and status lists it with no organization', async t => {
|
||||
const tree = new URL('../../plugins/harness/', import.meta.url);
|
||||
const fromTree = fs.readdirSync(tree).filter(name => { try { return fs.lstatSync(new URL(`${name}/index.mjs`, tree)).isFile(); } catch { return false; } }).sort();
|
||||
assert.ok(fromTree.length >= 1);
|
||||
assert.deepEqual(installedHarnesses(), fromTree);
|
||||
const { admin } = fixture(t);
|
||||
assert.deepEqual((await admin.status()).programs, { installed: fromTree, organizations: [] });
|
||||
});
|
||||
|
||||
test('A2 item 5: status names the programs each organization has on; one bus down is null and leaves the others', async t => {
|
||||
const acme = fakeOrg('acme', { on: ['alpha'] }), bolt = fakeOrg('bolt', { down: true }), crux = fakeOrg('crux', { plugins: { harness: 'alpha' } });
|
||||
const { admin } = programsFixture(t, [acme, bolt, crux]);
|
||||
const status = await admin.status();
|
||||
assert.deepEqual(status.programs, { installed: ['alpha', 'beta', 'gamma'], organizations: [{ id: 'acme', on: ['alpha'] }, { id: 'bolt', on: null }, { id: 'crux', on: null }] });
|
||||
assert.doesNotMatch(JSON.stringify(status), KEY_TEXT);
|
||||
});
|
||||
|
||||
test('A2 item 5: off skips a program any route still uses, whatever its model, and writes nothing there', async t => {
|
||||
// A role route, a fallback route, a project agent only the file shows, and a route whose model has
|
||||
// no known account kind: each keeps alpha on (the harness rule).
|
||||
const role = fakeOrg('role', { instances: { chief: route('alpha') } });
|
||||
const fallback = fakeOrg('fallback', { instances: { chief: { ...route('beta'), fallback: { harness: 'alpha', model: 'm1', thinking: null } } } });
|
||||
const project = fakeOrg('project', { routesError: 'launch-not-configured', document: { roles: {}, plugins: { harness: ['alpha', 'beta'] }, projects: { site: { agents: { dev: { definition: 'coder', vars: { harness: 'alpha' } } } } } } });
|
||||
const unknownKind = fakeOrg('unknown-kind', { instances: { chief: { harness: 'alpha', model: 'mystery-account-model', thinking: null, fallback: null } } });
|
||||
const { admin } = programsFixture(t, [role, fallback, project, unknownKind]);
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results.map(r => [r.id, r.outcome]), [['role', 'in-use'], ['fallback', 'in-use'], ['project', 'in-use'], ['unknown-kind', 'in-use']]);
|
||||
for (const o of [role, fallback, project, unknownKind]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
test('A2 item 5: off turns the program off where nothing uses it, skips the last program, already off and unreadable, and reports bus down', async t => {
|
||||
const free = fakeOrg('free', { instances: { chief: route('beta') } });
|
||||
const last = fakeOrg('last', { on: ['alpha'] });
|
||||
const already = fakeOrg('already', { on: ['beta'] });
|
||||
const down = fakeOrg('down', { down: true });
|
||||
const unreadable = fakeOrg('unreadable', { fileError: true });
|
||||
const noFile = fakeOrg('no-file'); delete noFile.rider.file;
|
||||
const badRoutes = fakeOrg('bad-routes', { routesError: 'organization-invalid' });
|
||||
const { admin } = programsFixture(t, [free, last, already, down, unreadable, noFile, badRoutes]);
|
||||
const out = await admin.programsOff({ name: 'alpha', user: 'owner' });
|
||||
assert.equal(out.name, 'alpha');
|
||||
assert.deepEqual(out.results, [
|
||||
{ id: 'free', outcome: 'off' }, { id: 'last', outcome: 'last-program' }, { id: 'already', outcome: 'already-off' },
|
||||
{ id: 'down', outcome: 'bus-down' }, { id: 'unreadable', outcome: 'unreadable' }, { id: 'no-file', outcome: 'unreadable' }, { id: 'bad-routes', outcome: 'unreadable' },
|
||||
]);
|
||||
assert.deepEqual(free.calls.find(([verb]) => verb === 'plugins.set'), ['plugins.set', { revision: REV, plugins: { harness: ['beta'] } }]);
|
||||
for (const o of [last, already, down, unreadable, noFile, badRoutes]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
test('A2 item 5: a plugins.set refusal is reported per organization, and a revision change is reported once, never retried', async t => {
|
||||
const refused = fakeOrg('refused', { setError: 'unknown-plugin' });
|
||||
const raced = fakeOrg('raced', { setError: 'revision-conflict' });
|
||||
const moved = fakeOrg('moved', { fileRevision: REV2 });
|
||||
const ok = fakeOrg('ok');
|
||||
const { admin } = programsFixture(t, [refused, raced, moved, ok]);
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results.map(r => r.outcome), ['failed:unknown-plugin', 'failed:revision-conflict', 'failed:revision-conflict', 'off']);
|
||||
assert.equal(raced.calls.filter(([verb]) => verb === 'plugins.set').length, 1);
|
||||
assert.equal(moved.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
test('A2 item 5: off writes one receipt by name and outcome ids, and no answer, receipt or history entry holds a key', async t => {
|
||||
const a = fakeOrg('acme'), b = fakeOrg('bolt', { setError: 'invalid-plugins' });
|
||||
const { admin, dataRoot } = programsFixture(t, [a, b]);
|
||||
const out = await admin.programsOff({ name: 'beta', user: 'owner' });
|
||||
const record = { time: new Date(0).toISOString(), op: 'programs.off', name: 'beta', results: [{ id: 'acme', outcome: 'off' }, { id: 'bolt', outcome: 'failed:invalid-plugins' }], user: 'owner' };
|
||||
assert.deepEqual(out.receipt, record);
|
||||
assert.deepEqual(receipts(dataRoot), [record]);
|
||||
const history = admin.history({});
|
||||
assert.deepEqual(history.entries, [record]);
|
||||
for (const value of [out, receipts(dataRoot), history]) assert.doesNotMatch(JSON.stringify(value), KEY_TEXT);
|
||||
});
|
||||
|
||||
test('A2 item 5: an unknown name is invalid-request and calls no bus; two off calls at once give proxy-admin-busy for one', async t => {
|
||||
let release;
|
||||
const gate = new Promise(r => { release = r; });
|
||||
const slow = fakeOrg('slow');
|
||||
const original = slow.rider.call;
|
||||
// Blocks only from the busy part on, so a name check that lets a bad name through goes red, not hangs.
|
||||
let blocking = false;
|
||||
slow.rider.call = async (verb, args) => { if (verb === 'plugins' && blocking) await gate; return original(verb, args); };
|
||||
const { admin, dataRoot } = programsFixture(t, [slow]);
|
||||
await assert.rejects(admin.programsOff({ name: 'delta' }), code('invalid-request'));
|
||||
await assert.rejects(admin.programsOff({ name: 42 }), code('invalid-request'));
|
||||
assert.deepEqual(slow.calls, []);
|
||||
assert.deepEqual(receipts(dataRoot), []);
|
||||
blocking = true;
|
||||
const first = admin.programsOff({ name: 'alpha' });
|
||||
await new Promise(r => setImmediate(r));
|
||||
// Bounded: without the lock the second call would wait on the same gate, so it answers 'waited'.
|
||||
const second = admin.programsOff({ name: 'beta' }).then(() => 'answered', e => e?.code);
|
||||
assert.equal(await Promise.race([second, new Promise(r => setTimeout(() => r('waited'), 1000))]), 'proxy-admin-busy');
|
||||
release();
|
||||
await second;
|
||||
assert.deepEqual((await first).results, [{ id: 'slow', outcome: 'off' }]);
|
||||
assert.equal(receipts(dataRoot).length, 1);
|
||||
});
|
||||
|
||||
test('A2 item 5: an installed list that cannot be read refuses off as invalid-request', async t => {
|
||||
const a = fakeOrg('acme');
|
||||
const { admin } = programsFixture(t, [a], { harnesses: () => { throw new Error('unreadable tree'); } });
|
||||
await assert.rejects(admin.programsOff({ name: 'alpha' }), code('invalid-request'));
|
||||
assert.deepEqual(a.calls, []);
|
||||
assert.equal((await admin.status()).programs.installed, null);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P1-1): a root agent the organization takes keeps its program on; an excluded organization and the root assistant do not count', async t => {
|
||||
// Only the root agent file names alpha: nothing in either organization's file or routes does.
|
||||
const root = { state: 'ok', assistant: 'helper', agents: {
|
||||
writer: rootAgent({ harness: 'gamma', 'fallback.harness': 'alpha' }, { excludeOrgs: ['spare'] }),
|
||||
helper: rootAgent({ harness: 'beta' }, { definition: 'assistant' }),
|
||||
} };
|
||||
const takes = fakeOrg('takes', { on: ['alpha', 'beta', 'gamma'] });
|
||||
const spare = fakeOrg('spare', { on: ['alpha', 'beta', 'gamma'] });
|
||||
const { admin } = programsFixture(t, [takes, spare], { rootAgents: () => root });
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results, [{ id: 'takes', outcome: 'in-use' }, { id: 'spare', outcome: 'off' }]);
|
||||
assert.equal(takes.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
// The root assistant is not copied into an organization, so its harness does not keep beta on.
|
||||
const again = await admin.programsOff({ name: 'beta' });
|
||||
assert.deepEqual(again.results.map(r => r.outcome), ['off', 'off']);
|
||||
// The root file is read once per call, not once per organization.
|
||||
let reads = 0;
|
||||
const counted = programsFixture(t, [fakeOrg('a'), fakeOrg('b')], { rootAgents: () => { reads++; return NO_ROOT; } });
|
||||
await counted.admin.programsOff({ name: 'alpha' });
|
||||
assert.equal(reads, 1);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P1-1): a root agent file that cannot be read, or no reader, writes nothing where the program is on', async t => {
|
||||
const on = fakeOrg('on'), already = fakeOrg('already', { on: ['beta'] });
|
||||
const { admin } = programsFixture(t, [on, already], { rootAgents: () => { throw Object.assign(new Error(`root agent file refused ${PLANT}`), { name: 'BusinessError' }); } });
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results, [{ id: 'on', outcome: 'unreadable' }, { id: 'already', outcome: 'already-off' }]);
|
||||
assert.doesNotMatch(JSON.stringify(out), KEY_TEXT);
|
||||
const bare = fakeOrg('bare');
|
||||
const none = fixture(t, { extra: { riders: async () => [bare.rider], harnesses: () => ['alpha', 'beta'] } });
|
||||
assert.deepEqual((await none.admin.programsOff({ name: 'alpha' })).results, [{ id: 'bare', outcome: 'unreadable' }]);
|
||||
for (const o of [on, already, bare]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
// A root answer of the wrong shape is unreadable too.
|
||||
assert.equal(rootHarnessesNamed({ agents: { w: { vars: { harness: 'alpha' } } } }, 'acme'), null);
|
||||
assert.equal(rootHarnessesNamed(null, 'acme'), null);
|
||||
assert.deepEqual([...rootHarnessesNamed(NO_ROOT, 'acme')], []);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P3-1): a revision that is not 64 hex, routes from another revision, and routes with no instances write nothing; later organizations still go', async t => {
|
||||
const badRev = fakeOrg('bad-rev', { revision: 'not-a-revision' });
|
||||
const routesMoved = fakeOrg('routes-moved', { routesRevision: REV2 });
|
||||
const noInstances = fakeOrg('no-instances', { instances: null });
|
||||
const after = fakeOrg('after');
|
||||
const { admin, dataRoot } = programsFixture(t, [badRev, routesMoved, noInstances, after]);
|
||||
const out = await admin.programsOff({ name: 'alpha' });
|
||||
assert.deepEqual(out.results, [{ id: 'bad-rev', outcome: 'unreadable' }, { id: 'routes-moved', outcome: 'failed:revision-conflict' }, { id: 'no-instances', outcome: 'unreadable' }, { id: 'after', outcome: 'off' }]);
|
||||
for (const o of [badRev, routesMoved, noInstances]) assert.equal(o.calls.some(([verb]) => verb === 'plugins.set'), false);
|
||||
assert.equal(receipts(dataRoot).length, 1);
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P3-1): a harness program the instance plugin lock pins is listed as installed', t => {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'pl-'));
|
||||
// The install leaves read-only directories; open them up before removing the tree.
|
||||
const thaw = p => { const st = fs.lstatSync(p); if (st.isSymbolicLink()) return; if (st.isDirectory()) { fs.chmodSync(p, 0o700); for (const n of fs.readdirSync(p)) thaw(join(p, n)); } };
|
||||
t.after(() => { thaw(root); fs.rmSync(root, { recursive: true, force: true }); });
|
||||
const src = join(root, 'src'), dataRoot = join(root, 'data');
|
||||
fs.mkdirSync(join(src, 'harness'), { recursive: true }); fs.mkdirSync(dataRoot);
|
||||
fs.writeFileSync(join(src, 'harness', 'index.mjs'), KINDS.harness.exports.map(name => `export function ${name}(){return ${name === 'levels' ? "['high']" : name === 'countsAccount' ? 'true' : 'null'};}`).join('\n') + '\nexport const interfaceVersion=1;');
|
||||
const manifest = { name: 'zetaplug', version: '1.0.0', kinds: ['harness'], interfaceVersions: { harness: 1 }, sha256: '0'.repeat(64), source: { path: 'fixture', version: '1.0.0' }, signature: null, tools: [], actions: [], services: [], roleTemplates: [] };
|
||||
fs.writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
manifest.sha256 = pluginHash(src);
|
||||
fs.writeFileSync(join(src, 'plugin.json'), JSON.stringify(manifest));
|
||||
assert.equal(installedHarnesses(dataRoot).includes('zetaplug'), false);
|
||||
installPlugin({ dataRoot, source: src, expectedHash: manifest.sha256 });
|
||||
const listed = installedHarnesses(dataRoot);
|
||||
assert.ok(listed.includes('zetaplug'));
|
||||
assert.deepEqual(listed.filter(n => n !== 'zetaplug'), installedHarnesses());
|
||||
});
|
||||
|
||||
test('A2 item 5 (review P3-3): an organization whose harness list holds anything but plugin names reads on: null', async t => {
|
||||
const odd = fakeOrg('odd', { plugins: { harness: ['alpha', `bad ${PLANT}`] } });
|
||||
const fine = fakeOrg('fine', { on: ['alpha'] });
|
||||
const { admin } = programsFixture(t, [odd, fine]);
|
||||
const status = await admin.status();
|
||||
assert.deepEqual(status.programs.organizations, [{ id: 'odd', on: null }, { id: 'fine', on: ['alpha'] }]);
|
||||
assert.doesNotMatch(JSON.stringify(status), KEY_TEXT);
|
||||
});
|
||||
|
||||
test('A2: harnessesNamed reads every harness and fallback.harness string outside the plugins list', () => {
|
||||
assert.deepEqual([...harnessesNamed({ plugins: { harness: ['alpha'] }, roles: { a: { vars: { harness: 'beta', 'fallback.harness': 'gamma' } } }, projects: { p: { agents: { x: { vars: { harness: 'delta' } } } } } })].sort(), ['beta', 'delta', 'gamma']);
|
||||
assert.equal(harnessesNamed(null), null);
|
||||
assert.equal(harnessesNamed([]), null);
|
||||
});
|
||||
|
||||
const key = (keyId, management, state, agent = 'dev') => ({ keyId, management, agent, issuedAt: '2026-11-01T10:00:00.000Z', state });
|
||||
test('A2 item 8: access gives each organization a health and key ids, never a key, and never reads the usage queue', async t => {
|
||||
const ok = fakeOrg('ok', { keys: [key('0123456789ab', 'stack-managed', 'active'), key('ba9876543210', 'not stack-managed', null, null), key('aaaaaaaaaaaa', 'sentinel', null, null)] });
|
||||
const attention = fakeOrg('attention', { keys: [key('0123456789ab', 'stack-managed', 'active'), key('bbbbbbbbbbbb', 'stack-managed', 'revoked')] });
|
||||
const missing = fakeOrg('missing', { keys: [key('cccccccccccc', 'not stack-managed', null, null)] });
|
||||
const fixtureOrg = fakeOrg('fixture', { fixture: true });
|
||||
const ownModels = fakeOrg('own', { cliproxy: false });
|
||||
const down = fakeOrg('down', { down: true });
|
||||
const noClient = fakeOrg('no-client', { keysError: 'not-configured' });
|
||||
const uncertain = fakeOrg('uncertain', { keysError: 'proxy-uncertain' });
|
||||
const proxyDown = fakeOrg('proxy-down', { keysError: 'proxy-down' });
|
||||
const badShape = fakeOrg('bad-shape', { keys: 'none' });
|
||||
const planted = fakeOrg('planted', { keys: [key('dddddddddddd', 'stack-managed', PLANT, PLANT), { ...key('eeeeeeeeeeee', 'stack-managed', 'active'), value: PLANT, issuedAt: PLANT }] });
|
||||
const plantedId = fakeOrg('planted-id', { keys: [key(PLANT, 'stack-managed', 'active')] });
|
||||
const unread = fakeOrg('unread'); unread.rider.fixture = undefined;
|
||||
const all = [ok, attention, missing, fixtureOrg, ownModels, down, noClient, uncertain, proxyDown, badShape, planted, plantedId, unread];
|
||||
const { admin } = programsFixture(t, all);
|
||||
const out = await admin.access();
|
||||
assert.deepEqual(out.organizations, [
|
||||
{ id: 'ok', health: 'ok', keys: [{ keyId: '0123456789ab', agent: 'dev', state: 'active', issuedAt: '2026-11-01T10:00:00.000Z' }], other: 1, sentinel: 1 },
|
||||
{ id: 'attention', health: 'attention', keys: [{ keyId: '0123456789ab', agent: 'dev', state: 'active', issuedAt: '2026-11-01T10:00:00.000Z' }, { keyId: 'bbbbbbbbbbbb', agent: 'dev', state: 'revoked', issuedAt: '2026-11-01T10:00:00.000Z' }], other: 0, sentinel: 0 },
|
||||
{ id: 'missing', health: 'missing', keys: [], other: 1, sentinel: 0 },
|
||||
{ id: 'fixture', health: 'not-riding', keys: [] },
|
||||
{ id: 'own', health: 'not-riding', keys: [] },
|
||||
{ id: 'down', health: 'unreadable', reason: 'bus-down', keys: [] },
|
||||
{ id: 'no-client', health: 'unreadable', reason: 'no-proxy-client', keys: [] },
|
||||
{ id: 'uncertain', health: 'attention', reason: 'proxy-uncertain', keys: [] },
|
||||
{ id: 'proxy-down', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
{ id: 'bad-shape', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
{ id: 'planted', health: 'attention', keys: [{ keyId: 'dddddddddddd', agent: null, state: null, issuedAt: '2026-11-01T10:00:00.000Z' }, { keyId: 'eeeeeeeeeeee', agent: 'dev', state: 'active', issuedAt: null }], other: 0, sentinel: 0 },
|
||||
{ id: 'planted-id', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
{ id: 'unread', health: 'unreadable', reason: 'unreadable', keys: [] },
|
||||
]);
|
||||
assert.doesNotMatch(JSON.stringify(out), KEY_TEXT);
|
||||
assert.notEqual(out.organizations.find(o => o.id === 'uncertain').health, 'ok');
|
||||
// proxy.keys is the only verb access sends; the usage queue is never read.
|
||||
for (const o of all) for (const [verb] of o.calls) assert.equal(verb, 'proxy.keys');
|
||||
});
|
||||
@@ -6,12 +6,15 @@ import fs from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { PIN } from '../src/common.mjs';
|
||||
import { installPaths } from '../src/settings.mjs';
|
||||
import {
|
||||
processTree, pidListensOn, processStartMs, verifyRestart, initLockStatus, clearInitLock, initFiles, initState,
|
||||
fenceState, createUnitControl, releaseState, receiptFile, unitName,
|
||||
unitLog, serviceKeyState, receiptHistory, appendReceipt, LOG_LINES, HISTORY_LIMIT,
|
||||
} from '../src/admin.mjs';
|
||||
import { secretValues } from '../src/redact.mjs';
|
||||
|
||||
const code = name => error => error.code === name;
|
||||
const scratch = t => { const dir = fs.mkdtempSync(join(tmpdir(), 'pa-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true })); return dir; };
|
||||
@@ -193,3 +196,198 @@ test('unit actions map proxy-service refusals by code and refuse a missing unit'
|
||||
assert.equal(started.healthy, true); assert.deepEqual(verbs, ['start']);
|
||||
assert.throws(() => createUnitControl({ unitDir: 'relative' }), code('proxy-unit-path'));
|
||||
});
|
||||
|
||||
// #1873 settings-backend A1: the reads behind the setup page's log, service key and history.
|
||||
// Planted keys, one of each Mosaic prefix and short enough that only the prefix rule finds them,
|
||||
// plus long ones of the real shapes.
|
||||
const TAIL = 'Ab3_dE5-fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7dE';
|
||||
const PLANTED = ['msk-tag-qwertyu', 'mso-tag-acme-qwerty', 'mst-tag-triage-zxcvb', 'msp-tag-plmokn',
|
||||
`msk-k1a2-${TAIL}`, `mso-k1a2-acme-${TAIL}`, `mst-k1a2-triage-${TAIL}`, `msp-k1a2-${TAIL}`];
|
||||
const ENV_SECRET = 'plainwordsonly';
|
||||
const leaks = text => [...PLANTED, ...PLANTED.map(k => k.slice(4)), TAIL.slice(0, 10), ENV_SECRET].filter(bit => text.includes(bit));
|
||||
|
||||
function journal(stdout, { code: exit = 0, calls = [] } = {}) {
|
||||
return async (command, args, options) => { calls.push({ command, args, options }); if (stdout instanceof Error) throw stdout; return { code: exit, stdout, stderr: 'raw stderr msk-tag-qwertyu' }; };
|
||||
}
|
||||
|
||||
test('unitLog asks journalctl for the named unit only, clamps the line count, and shows no planted key', async () => {
|
||||
const calls = [];
|
||||
const lines = ['proxy listening on 127.0.0.1:18432', ...PLANTED.map(k => `upstream refused ${k} for model x`), `auth with ${ENV_SECRET} ok`, 'msk-tag-qwertyu', '', 'last line'];
|
||||
const result = await unitLog({ run: journal(lines.join('\n') + '\n', { calls }), unit: 'mosaic-proxy-zz.service', lines: 10000, env: { X: '1' }, secrets: secretValues({ PROXY_KEY: ENV_SECRET }) });
|
||||
assert.deepEqual(calls[0].args, ['--user', '-u', 'mosaic-proxy-zz.service', '-n', String(LOG_LINES.max), '-o', 'cat', '--no-pager', '-q']);
|
||||
assert.equal(calls[0].command, 'journalctl');
|
||||
assert.deepEqual(calls[0].options.env, { X: '1' });
|
||||
assert.equal(result.unit, 'mosaic-proxy-zz.service');
|
||||
assert.equal(result.lines[0], 'proxy listening on 127.0.0.1:18432', 'a line with nothing secret shows as it is');
|
||||
assert.equal(result.lines.at(-1), 'last line');
|
||||
assert.equal(result.lines.filter(l => l === 'upstream refused [redacted] for model x').length, PLANTED.length);
|
||||
assert.equal(result.redacted, PLANTED.length, 'shown lines with a redaction; dropped lines count in dropped');
|
||||
assert.equal(result.dropped, 2, 'the env-secret line and the key-only line are dropped, the blank one is not counted');
|
||||
assert.deepEqual(leaks(JSON.stringify(result)), []);
|
||||
// At least one line, and a line count that is not an integer is refused.
|
||||
await unitLog({ run: journal('x\n', { calls }), unit: 'mosaic-proxy.service', lines: 0 });
|
||||
assert.equal(calls.at(-1).args[4], '1');
|
||||
await assert.rejects(unitLog({ run: journal('x\n'), unit: 'mosaic-proxy.service', lines: 2.5 }), code('invalid-request'));
|
||||
await assert.rejects(unitLog({ run: journal('x\n'), unit: 'mosaic-proxy.service', lines: '5' }), code('invalid-request'));
|
||||
});
|
||||
|
||||
test('unitLog keeps only the last lines asked for and never takes a unit name it was not given by unitName', async () => {
|
||||
const many = Array.from({ length: 30 }, (_, i) => `line ${i}`).join('\n');
|
||||
assert.deepEqual((await unitLog({ run: journal(many), unit: 'mosaic-proxy.service', lines: 3 })).lines, ['line 27', 'line 28', 'line 29']);
|
||||
for (const unit of ['sshd.service', 'mosaic-proxy.service; rm', '../mosaic-proxy.service', 'mosaic-proxy-A.service', undefined]) {
|
||||
await assert.rejects(unitLog({ run: journal('x'), unit, lines: 5 }), code('proxy-unit-usage'), String(unit));
|
||||
}
|
||||
});
|
||||
|
||||
test('a journal that cannot be read refuses outcome-unknown with no lines and no other source', async () => {
|
||||
for (const run of [journal(Object.assign(new Error('spawn journalctl ENOENT'), { code: 'ENOENT' })), journal('partial msk-tag-qwertyu', { code: 1 }), async () => ({ code: 0, stdout: null }), async () => null]) {
|
||||
await assert.rejects(unitLog({ run, unit: 'mosaic-proxy.service', lines: 5 }), error => error.code === 'outcome-unknown' && !leaks(String(error.message) + JSON.stringify(error)).length);
|
||||
}
|
||||
});
|
||||
|
||||
test('a secret split across journal lines drops every line', async () => {
|
||||
const result = await unitLog({ run: journal('ok one\nplain\nwordsonly\nok two\n'), unit: 'mosaic-proxy.service', lines: 10, secrets: [ENV_SECRET] });
|
||||
assert.deepEqual(result.lines, []);
|
||||
assert.equal(result.dropped, 4);
|
||||
});
|
||||
|
||||
test('createUnitControl.log runs unitLog for its own unit', async () => {
|
||||
const calls = [];
|
||||
const control = createUnitControl({ instance: 'zz', port: () => 1, run: journal('hello msk-tag-qwertyu\n', { calls }), probe: async () => true, waitMs: 0, pollMs: 0 });
|
||||
assert.deepEqual(await control.log(5), { unit: 'mosaic-proxy-zz.service', lines: ['hello [redacted]'], redacted: 1, dropped: 0 });
|
||||
assert.equal(calls[0].args[2], 'mosaic-proxy-zz.service');
|
||||
});
|
||||
|
||||
test('serviceKeyState answers proxy.json\'s mtime and never opens or stats the key file', t => {
|
||||
const dir = scratch(t);
|
||||
assert.deepEqual(serviceKeyState(dir), { madeAt: null, renewedAt: null, renewal: 'not-built', problem: 'missing' });
|
||||
const files = initFiles(dir, join(dir, 'data'));
|
||||
fs.writeFileSync(files.settings, '{}', { mode: 0o600 });
|
||||
fs.mkdirSync(join(files.key, '..'), { recursive: true, mode: 0o700 });
|
||||
fs.writeFileSync(files.key, 'msk-k1a2-' + TAIL, { mode: 0o600 });
|
||||
fs.utimesSync(join(dir, 'proxy.json'), new Date(Date.UTC(2026, 10, 1, 8)), new Date(Date.UTC(2026, 10, 1, 8)));
|
||||
const spies = ['openSync', 'readFileSync', 'statSync', 'lstatSync', 'accessSync', 'existsSync', 'realpathSync', 'readdirSync'].map(name => t.mock.method(fs, name));
|
||||
const state = serviceKeyState(dir);
|
||||
const seen = spies.flatMap(spy => spy.mock.calls.map(call => String(call.arguments[0])));
|
||||
spies.forEach(spy => spy.mock.restore());
|
||||
assert.deepEqual(state, { madeAt: '2026-11-01T08:00:00.000Z', renewedAt: null, renewal: 'not-built', problem: null });
|
||||
assert.deepEqual(seen, [join(dir, 'proxy.json')]);
|
||||
assert.deepEqual(leaks(JSON.stringify(state)), []);
|
||||
});
|
||||
|
||||
test('serviceKeyState gives no date for a proxy.json that is a symlink, a directory or another user\'s', t => {
|
||||
const dir = scratch(t), other = scratch(t);
|
||||
fs.writeFileSync(join(other, 'real.json'), '{}');
|
||||
fs.symlinkSync(join(other, 'real.json'), join(dir, 'proxy.json'));
|
||||
assert.deepEqual(serviceKeyState(dir), { madeAt: null, renewedAt: null, renewal: 'not-built', problem: 'unreadable' });
|
||||
fs.rmSync(join(dir, 'proxy.json')); fs.mkdirSync(join(dir, 'proxy.json'));
|
||||
assert.equal(serviceKeyState(dir).problem, 'unreadable');
|
||||
fs.rmSync(join(dir, 'proxy.json'), { recursive: true }); fs.writeFileSync(join(dir, 'proxy.json'), '{}');
|
||||
assert.equal(serviceKeyState(dir, { uid: process.getuid() + 1 }).problem, 'unreadable');
|
||||
assert.equal(serviceKeyState(dir).problem, null);
|
||||
});
|
||||
|
||||
// A receipt file with `count` entries; some lines padded so the file crosses several 64 KB chunks.
|
||||
function history(t, count, { pad = 0 } = {}) {
|
||||
const dataRoot = scratch(t);
|
||||
for (let i = 0; i < count; i++) appendReceipt(dataRoot, { time: new Date(i * 1000).toISOString(), op: 'fetch', n: i, ...(pad ? { note: 'word '.repeat(pad) } : {}) });
|
||||
return dataRoot;
|
||||
}
|
||||
const pages = (dataRoot, limit, extra = {}) => {
|
||||
const seen = []; let before = null, guard = 0;
|
||||
do {
|
||||
const page = receiptHistory(dataRoot, { limit, before, ...extra });
|
||||
seen.push(page.entries.map(e => e.n));
|
||||
before = page.next;
|
||||
} while (before !== null && ++guard < 1000);
|
||||
return seen;
|
||||
};
|
||||
|
||||
test('receiptHistory pages newest first, oldest first within a page, each entry exactly once across chunks', t => {
|
||||
const dataRoot = history(t, 300, { pad: 150 });
|
||||
assert.ok(fs.statSync(receiptFile(dataRoot)).size > 3 * 65536, 'the file spans several chunks');
|
||||
const seen = pages(dataRoot, 7);
|
||||
assert.deepEqual(seen[0], [293, 294, 295, 296, 297, 298, 299]);
|
||||
assert.deepEqual(seen.flat().sort((a, b) => a - b), Array.from({ length: 300 }, (_, i) => i));
|
||||
assert.equal(new Set(seen.flat()).size, 300);
|
||||
assert.deepEqual(seen.reverse().flat(), Array.from({ length: 300 }, (_, i) => i), 'pages read back to front are the whole history in order');
|
||||
const first = receiptHistory(dataRoot);
|
||||
assert.equal(first.entries.length, HISTORY_LIMIT.default);
|
||||
assert.equal(receiptHistory(dataRoot, { limit: 1000 }).entries.length, HISTORY_LIMIT.max, 'limit is clamped');
|
||||
assert.equal(receiptHistory(dataRoot, { limit: 0 }).entries.length, 1);
|
||||
assert.deepEqual(Object.keys(first.entries[0]).sort(), ['n', 'note', 'op', 'time']);
|
||||
});
|
||||
|
||||
test('receiptHistory skips a torn line and a partial last line, and a write after a page does not repeat or lose entries', t => {
|
||||
const dataRoot = history(t, 5);
|
||||
const file = receiptFile(dataRoot);
|
||||
const text = fs.readFileSync(file, 'utf8').split('\n');
|
||||
text.splice(2, 0, '{"op":"torn', '[1,2]', '{"no":"op"}', 'x'.repeat(70000));
|
||||
// The last line is a whole object whose newline is not written yet: it waits for the newline.
|
||||
fs.writeFileSync(file, text.join('\n') + '{"op":"partial","n":99}');
|
||||
const first = receiptHistory(dataRoot, { limit: 2 });
|
||||
assert.deepEqual(first.entries.map(e => e.n), [3, 4]);
|
||||
assert.deepEqual(receiptHistory(dataRoot, { limit: 1 }).entries.map(e => e.n), [4], 'not shown before its newline');
|
||||
fs.appendFileSync(file, '\n'); appendReceipt(dataRoot, { op: 'fetch', n: 100 });
|
||||
const rest = receiptHistory(dataRoot, { limit: 10, before: first.next });
|
||||
assert.deepEqual(rest.entries.map(e => e.n), [0, 1, 2]);
|
||||
assert.equal(rest.next, null);
|
||||
assert.deepEqual(receiptHistory(dataRoot, { limit: 3 }).entries.map(e => e.n), [4, 99, 100], 'the finished line shows on the next newest read');
|
||||
});
|
||||
|
||||
test('receiptHistory refuses a cursor that is not a line start, past the end, negative or not an integer', t => {
|
||||
const dataRoot = history(t, 3);
|
||||
const size = fs.statSync(receiptFile(dataRoot)).size;
|
||||
for (const before of [1, size + 1, -1, 1.5, '10', Number.MAX_SAFE_INTEGER + 2]) {
|
||||
assert.throws(() => receiptHistory(dataRoot, { before }), code('invalid-request'), String(before));
|
||||
}
|
||||
assert.throws(() => receiptHistory(dataRoot, { limit: 'all' }), code('invalid-request'));
|
||||
assert.deepEqual(receiptHistory(dataRoot, { before: 0 }), { entries: [], next: null });
|
||||
assert.equal(receiptHistory(dataRoot, { before: size }).entries.length, 3);
|
||||
});
|
||||
|
||||
test('receiptHistory withholds an entry whose fields hold an environment secret split across them', t => {
|
||||
const dataRoot = scratch(t);
|
||||
appendReceipt(dataRoot, { op: 'fetch', n: 1 });
|
||||
appendReceipt(dataRoot, { op: 'x', a: 'plainword', b: 'sonly' });
|
||||
appendReceipt(dataRoot, { op: 'x', plainw: 'ordsonly' });
|
||||
appendReceipt(dataRoot, { op: 'x', list: ['plainwo', 'rdsonly'] });
|
||||
appendReceipt(dataRoot, { op: 'fetch', n: 2 });
|
||||
const all = receiptHistory(dataRoot, { limit: 10, secrets: secretValues({ PROXY_TOKEN: ENV_SECRET }) });
|
||||
assert.deepEqual(all.entries, [{ op: 'fetch', n: 1 }, { withheld: true }, { withheld: true }, { withheld: true }, { op: 'fetch', n: 2 }]);
|
||||
assert.equal(receiptHistory(dataRoot, { limit: 10 }).entries[1].a, 'plainword', 'with no secrets nothing is withheld');
|
||||
});
|
||||
|
||||
test('receiptHistory: a missing file is an empty history; a symlink, FIFO, directory, group-readable or foreign file refuses', t => {
|
||||
const dataRoot = scratch(t);
|
||||
assert.deepEqual(receiptHistory(dataRoot), { entries: [], next: null });
|
||||
assert.throws(() => receiptHistory(dataRoot, { before: 5 }), code('invalid-request'));
|
||||
appendReceipt(dataRoot, { op: 'fetch' });
|
||||
const file = receiptFile(dataRoot);
|
||||
fs.chmodSync(file, 0o640);
|
||||
assert.throws(() => receiptHistory(dataRoot), code('outcome-unknown'));
|
||||
fs.chmodSync(file, 0o600);
|
||||
assert.throws(() => receiptHistory(dataRoot, { uid: process.getuid() + 1 }), code('outcome-unknown'));
|
||||
assert.equal(receiptHistory(dataRoot).entries.length, 1);
|
||||
const elsewhere = join(scratch(t), 'x.jsonl'); fs.writeFileSync(elsewhere, '{"op":"planted"}\n', { mode: 0o600 });
|
||||
fs.rmSync(file); fs.symlinkSync(elsewhere, file);
|
||||
assert.throws(() => receiptHistory(dataRoot), code('outcome-unknown'));
|
||||
fs.rmSync(file); fs.mkdirSync(file);
|
||||
assert.throws(() => receiptHistory(dataRoot), code('outcome-unknown'));
|
||||
fs.rmSync(file, { recursive: true });
|
||||
execFileSync('mkfifo', ['-m', '600', file]);
|
||||
assert.throws(() => receiptHistory(dataRoot), code('outcome-unknown'), 'a FIFO is refused without blocking');
|
||||
});
|
||||
|
||||
test('receiptHistory shows no planted key from any receipt field, nested value or key name', t => {
|
||||
const dataRoot = scratch(t);
|
||||
for (const key of PLANTED) appendReceipt(dataRoot, { time: new Date(0).toISOString(), op: 'init', note: `made ${key}`, nested: { list: [key, { deeper: key }] }, [key.replace(/-/g, '_')]: 1 });
|
||||
appendReceipt(dataRoot, { op: 'init', env: ENV_SECRET, [`x${ENV_SECRET}`]: 1 });
|
||||
appendReceipt(dataRoot, { op: PLANTED[1] });
|
||||
const all = receiptHistory(dataRoot, { limit: 100, secrets: secretValues({ PROXY_TOKEN: ENV_SECRET }) });
|
||||
assert.equal(all.entries.length, PLANTED.length + 2);
|
||||
assert.deepEqual(leaks(JSON.stringify(all)), []);
|
||||
assert.deepEqual(all.entries[0], { time: new Date(0).toISOString(), op: 'init', note: 'made [redacted]', nested: { list: ['[redacted]', { deeper: '[redacted]' }] } });
|
||||
assert.deepEqual(all.entries.at(-2), { op: 'init', env: '[redacted]' });
|
||||
assert.deepEqual(all.entries.at(-1), { op: '[redacted]' });
|
||||
});
|
||||
@@ -0,0 +1,122 @@
|
||||
// #1873 settings-backend A1: the redaction the setup page's log and history reads use. It is
|
||||
// chat-fail's list (packages/harness/src/failure-notice.mjs) with Mosaic's key prefixes added.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { redactLine, redactValue, holdsSecret, secretValues, REDACTED, LINE_MAX } from '../src/redact.mjs';
|
||||
|
||||
// Short keys of each Mosaic prefix, letters only, so no long-run or mixed-run rule catches them:
|
||||
// only the key-prefix line does.
|
||||
const SHORT = { msk: 'msk-tag-qwertyu', mso: 'mso-tag-acme-qwerty', mst: 'mst-tag-triage-zxcvb', msp: 'msp-tag-plmokn' };
|
||||
|
||||
for (const [prefix, key] of Object.entries(SHORT)) {
|
||||
test(`a short ${prefix}- key is redacted from a line, and the rest of the line stays`, () => {
|
||||
const safe = redactLine(`proxy said ${key} was refused`);
|
||||
assert.equal(safe.text, `proxy said ${REDACTED} was refused`);
|
||||
assert.equal(safe.redacted, true);
|
||||
assert.equal(safe.text.includes(key), false);
|
||||
// Upper case, and joined by _ instead of -, too.
|
||||
assert.equal(redactLine(`proxy said ${key.toUpperCase()} x`).text, `proxy said ${REDACTED} x`);
|
||||
assert.equal(redactLine(`proxy said ${key.replace('-', '_')} x`).text, `proxy said ${REDACTED} x`);
|
||||
});
|
||||
}
|
||||
|
||||
test('a short Mosaic key glued to a word or an underscore is redacted', () => {
|
||||
for (const key of Object.values(SHORT)) {
|
||||
for (const glued of [`x${key}`, `_${key}`, `id=x${key}`, `9${key.toUpperCase()}`]) {
|
||||
const safe = redactLine(`proxy said ${glued} was refused`);
|
||||
assert.equal(safe.redacted, true, glued);
|
||||
assert.equal(safe.text.toLowerCase().includes(key.slice(4)), false, glued);
|
||||
assert.match(safe.text, / was refused$/);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('the real key shapes of each prefix are redacted', () => {
|
||||
const tail = 'Ab3_dE5-fG7hJ9kL1mN3pQ5rS7tU9vW1xY3zA5bC7dE';
|
||||
for (const key of [`msk-k1a2-${tail}`, `mso-k1a2-acme-${tail}`, `mst-k1a2-triage-${tail}`, `msp-k1a2-${tail}`, `sk-ant-${tail}`]) {
|
||||
const safe = redactLine(`key ${key} rejected by upstream`);
|
||||
assert.equal(safe.text.includes(key.slice(0, 12)), false, key.slice(0, 4));
|
||||
assert.equal(safe.text.includes(tail.slice(0, 8)), false, key.slice(0, 4));
|
||||
}
|
||||
});
|
||||
|
||||
test('bearer and basic tokens, key=value pairs, links, emails and paths never show', () => {
|
||||
const line = 'POST https://u:[email protected]/v1?key=abc Authorization: Bearer abcdefgh api_key=hunter2 token "quoted value" '
|
||||
+ 'by [email protected] from /home/jane/.config/mosaic-dev/proxy.json and ~/.mosaic/x C:\\Users\\j \\\\host\\share Basic dXNlcjpwYXNz eyJhbGciOi.x.y';
|
||||
const safe = redactLine(line);
|
||||
for (const bit of ['u:p@', 'api.example', 'abcdefgh', 'hunter2', 'quoted value', 'jane@', '/home/jane', '.config', '.mosaic', 'Users', 'share', 'dXNlcjpwYXNz', 'eyJhbGci']) {
|
||||
assert.equal(safe.text.includes(bit), false, bit);
|
||||
}
|
||||
assert.match(safe.text, /^POST /);
|
||||
});
|
||||
|
||||
test('a line holding an environment secret is withheld whole, even split by spaces, controls or encoding', () => {
|
||||
const secrets = secretValues({ MOSAIC_PROXY_KEY: 'plainwordsonly', HOME: '/home/x', SHORT_TOKEN: 'abc' });
|
||||
assert.deepEqual(secrets, ['plainwordsonly'], 'only secret-named values of 8 or more characters');
|
||||
for (const line of ['started with plainwordsonly', 'started with plain words only', 'started p\u0000lainwords\u200bonly', 'started %70lainwordsonly', 'started plainwordsonly']) {
|
||||
assert.deepEqual(redactLine(line, secrets), { text: null, redacted: true, withheld: true }, JSON.stringify(line));
|
||||
}
|
||||
assert.equal(holdsSecret('plain\nwords\nonly', secrets), true, 'split across lines');
|
||||
assert.equal(holdsSecret('nothing here', secrets), false);
|
||||
assert.equal(holdsSecret('plainwordsonly', []), false);
|
||||
});
|
||||
|
||||
test('a line that is empty, blank or only redactions is dropped; a long one is cut to the line cap', () => {
|
||||
assert.deepEqual(redactLine(''), { text: null, redacted: false });
|
||||
assert.deepEqual(redactLine(' \u0000\t '), { text: null, redacted: false });
|
||||
assert.deepEqual(redactLine(`${SHORT.mso} ${SHORT.mst}`), { text: null, redacted: true });
|
||||
assert.equal(redactLine(`a ${SHORT.mso} ${SHORT.mst} b`).text, `a ${REDACTED} b`, 'repeated redactions collapse');
|
||||
const long = redactLine('word '.repeat(200));
|
||||
assert.equal(Array.from(long.text).length, LINE_MAX);
|
||||
assert.match(long.text, /\.\.\.$/);
|
||||
assert.equal(redactLine('\u001b[31mred\u001b[0m text').text, 'red text', 'ANSI goes');
|
||||
});
|
||||
|
||||
test('a stored value is redacted all through: strings, nested strings, and keys that read as secrets', () => {
|
||||
const value = { op: 'account.proxy.apikey.add', time: '2026-11-19T12:34:56.789Z', provider: 'openai', note: `got ${SHORT.mso}`, deep: { list: [SHORT.msk, 3, true, null] }, [SHORT.mst.replace(/-/g, '_')]: 'x', 'bad key': 1 };
|
||||
const out = redactValue(value);
|
||||
assert.deepEqual(out, { op: 'account.proxy.apikey.add', time: '2026-11-19T12:34:56.789Z', provider: 'openai', note: `got ${REDACTED}`, deep: { list: [REDACTED, 3, true, null] } });
|
||||
assert.equal(JSON.stringify(out).includes('qwerty'), false);
|
||||
// Depth and size are bounded.
|
||||
assert.deepEqual(redactValue({ a: { b: { c: { d: { e: 1 } } } } }), { a: { b: { c: { d: null } } } });
|
||||
assert.equal(redactValue(Array.from({ length: 100 }, (_, i) => i)).length, 64);
|
||||
assert.equal(redactValue('plainwordsonly', ['plainwordsonly']), REDACTED);
|
||||
assert.equal(redactValue(undefined), null);
|
||||
});
|
||||
|
||||
// chat-fail's shape: redaction runs over a whole line before the cut, so a long run with no spaces
|
||||
// must stay linear.
|
||||
test('a 128000-character run with no spaces is redacted in well under a second', () => {
|
||||
const N = 128000;
|
||||
const secrets = secretValues({ API_KEY: 'plainwordsonly' });
|
||||
for (const run of ['a'.repeat(N), 'a.'.repeat(N / 2), 'a-'.repeat(N / 2), 'a/'.repeat(N / 2), 'a:'.repeat(N / 2), 'a"'.repeat(N / 2), 'mso-'.repeat(N / 4), 'a@'.repeat(N / 2)]) {
|
||||
const started = performance.now();
|
||||
const safe = redactLine(`${run} then the unit stopped`, secrets);
|
||||
const took = performance.now() - started;
|
||||
assert.ok(took < 1000, `${run.slice(0, 4)}... took ${Math.round(took)} ms`);
|
||||
assert.ok(safe.text === null || Array.from(safe.text).length <= LINE_MAX);
|
||||
}
|
||||
// A run that reads as a key is redacted whole, so the words after it show.
|
||||
assert.equal(redactLine(`${'a'.repeat(N)} then the unit stopped`).text, `${REDACTED} then the unit stopped`);
|
||||
});
|
||||
|
||||
// The list is a copy of failure-notice.mjs's. Every line must still match it, except the key-prefix
|
||||
// line, which is the original with mso, mst and msp added after msk, and Mosaic's glued-prefix line
|
||||
// right after it.
|
||||
test('the redaction list matches failure-notice.mjs line for line, with only the prefixes and one line added', () => {
|
||||
const source = file => fs.readFileSync(fileURLToPath(new URL(file, import.meta.url)), 'utf8');
|
||||
const block = text => {
|
||||
const body = /const REDACT = \[\n([\s\S]*?)\n\];/.exec(text)[1];
|
||||
return body.split('\n').map(line => line.trim()).filter(line => line && !line.startsWith('//')).map(line => line.replace(/,\s*\/\/ .*$/, ',').replace(/,$/, ''));
|
||||
};
|
||||
const keyWord = text => /const KEY_WORD = (.*);/.exec(text)[1];
|
||||
const theirs = source('../../harness/src/failure-notice.mjs'), ours = source('../src/redact.mjs');
|
||||
assert.equal(keyWord(ours), keyWord(theirs));
|
||||
const expected = block(theirs).map(line => line.replace('(?:sk|msk|rk|', '(?:sk|msk|mso|mst|msp|rk|'));
|
||||
assert.equal(expected.filter((line, i) => line !== block(theirs)[i]).length, 1, 'the original has one key-prefix line');
|
||||
const at = expected.findIndex(line => line.includes('(?:sk|msk|mso|mst|msp|rk|'));
|
||||
expected.splice(at + 1, 0, '/ms[kotp][-_]\\S+/gi');
|
||||
assert.deepEqual(block(ours), expected);
|
||||
});
|
||||
@@ -102,10 +102,12 @@ test('after Ride all the launcher asks admission for a direct anthropic route an
|
||||
};
|
||||
const launchOf = async riding => {
|
||||
const root = tmp(t);
|
||||
// A Claude model on a legacy entry counts as OAuth and runs on claude-code only. The stand-in adapter
|
||||
// path skips only the binary check; the launch stops at authorizeLaunch, before any adapter runs.
|
||||
const f = fixture(root, 'acme', doc => {
|
||||
doc.plugins = { ...doc.plugins, harness: ['pi'] };
|
||||
doc.plugins = { ...doc.plugins, harness: ['claude-code'] };
|
||||
doc.launch.models['claude-opus-5-5'] = { provider: 'anthropic', model: 'claude-opus-5-5' };
|
||||
doc.roles.coder.vars = { harness: 'pi', model: 'claude-opus-5-5', thinking: 'low' };
|
||||
doc.roles.coder.vars = { harness: 'claude-code', model: 'claude-opus-5-5', thinking: 'low' };
|
||||
return doc;
|
||||
});
|
||||
if (riding) await ride(join(root, 'config', 'businesses', 'acme.json'));
|
||||
@@ -119,7 +121,7 @@ test('after Ride all the launcher asks admission for a direct anthropic route an
|
||||
if (x.op === 'authorizeLaunch') throw Object.assign(Error('fixture authorization stop'), { code: 'fixture-policy' });
|
||||
return {};
|
||||
} };
|
||||
const makeLoader = await fakeHarnesses(t, { pi: null }, write => write('model-provider', 'cliproxy', `export * from ${JSON.stringify(new URL('../../plugins/model-provider/cliproxy/index.mjs', import.meta.url).href)};`));
|
||||
const makeLoader = await fakeHarnesses(t, { 'claude-code': '/fixture-unused' }, write => write('model-provider', 'cliproxy', `export * from ${JSON.stringify(new URL('../../plugins/model-provider/cliproxy/index.mjs', import.meta.url).href)};`));
|
||||
const system = { ...JSON.parse(readFileSync(f.config, 'utf8')), proxy: { port: 1, binary: '/fixture-unused' } };
|
||||
const launcher = createLauncher({ host, system, env: f.env, makeLoader, bwrap: '/usr/bin/true', log: () => {} });
|
||||
return { answer: await launcher.fromSession('fixture-cap', 'coder'), ops };
|
||||
|
||||
@@ -201,6 +201,43 @@ test('a removal saved while Ride all works on earlier organizations is read befo
|
||||
assert.equal(g2.calls.some(([v]) => v === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
test('settings-p3 R1: the change log is read again before each add, and a read that fails skips the add', async t => {
|
||||
// While Ride all works on 'first', the owner adds cliproxy to 'late' on its Settings page and takes it
|
||||
// off again. Only its change log shows that; the receipts do not. The run's first read showed nothing.
|
||||
let lateLog = '';
|
||||
const first = bus({ providers: [] }), late = bus({ providers: [] });
|
||||
const firstCall = async (verb, args) => { if (verb === 'plugins') lateLog = committed([{ path: 'plugins.model-provider', old: [], new: ['cliproxy'] }]) + committed([{ path: 'plugins.model-provider', old: ['cliproxy'], new: [] }]); return first.call(verb, args); };
|
||||
const f = admin(t, { riders: async () => [{ id: 'first', fixture: false, cliproxy: false, changes: '', call: firstCall }, { id: 'late', fixture: false, cliproxy: false, changes: lateLog, call: late.call }] });
|
||||
const r = await f.admin.rideAll();
|
||||
assert.deepEqual(r.added, ['first']); assert.deepEqual(r.skipped, [{ id: 'late', reason: 'removed' }]);
|
||||
assert.equal(late.calls.some(([v]) => v === 'plugins.set'), false);
|
||||
assert.deepEqual(receipts(f.dataRoot).filter(x => x.org === 'late').map(x => x.op), []);
|
||||
// Fail closed: a change log that can no longer be read, an organization that is no longer listed,
|
||||
// or a list that can't be read again skips the add as unreadable, with no intent and no plugins.set.
|
||||
for (const [name, again] of [
|
||||
['log unreadable', b => [{ id: 'o', fixture: false, cliproxy: false, changes: null, call: b.call }]],
|
||||
['no longer listed', () => []],
|
||||
['list throws', () => { throw new Error('read failed'); }],
|
||||
]) {
|
||||
const b = bus({ providers: [] });
|
||||
let reads = 0;
|
||||
const g = admin(t, { riders: async () => (reads++ === 0 ? [{ id: 'o', fixture: false, cliproxy: false, changes: '', call: b.call }] : again(b)) });
|
||||
assert.deepEqual((await g.admin.rideAll()).skipped, [{ id: 'o', reason: 'unreadable' }], name);
|
||||
assert.equal(b.calls.some(([v]) => v === 'plugins.set'), false, name);
|
||||
assert.deepEqual(receipts(g.dataRoot).filter(x => x.op === 'ride-intent'), [], name);
|
||||
}
|
||||
});
|
||||
|
||||
test('settings-p3 review P3-1: an organization that took cliproxy during the run reads as already using it, with no add', async t => {
|
||||
const b = bus({ providers: [] });
|
||||
let reads = 0;
|
||||
const f = admin(t, { riders: async () => [{ id: 'o', fixture: false, cliproxy: reads++ > 0, changes: '', call: b.call }] });
|
||||
const r = await f.admin.rideAll();
|
||||
assert.deepEqual([r.added, r.already, r.skipped], [[], ['o'], []]);
|
||||
assert.equal(b.calls.some(([v]) => v === 'plugins.set'), false);
|
||||
assert.deepEqual(receipts(f.dataRoot).filter(x => x.op === 'ride-intent'), []);
|
||||
});
|
||||
|
||||
test('an intent receipt that cannot be written stops that add before plugins.set', async t => {
|
||||
const b = bus({ providers: [] });
|
||||
const f = admin(t, { riders: async () => [{ id: 'w', fixture: false, cliproxy: false, changes: '', call: b.call }] });
|
||||
@@ -388,10 +425,15 @@ test('a fixture organization moves only when stopped, with a proxy, a riding bus
|
||||
assert.deepEqual(await f.admin.rideMove({ org: 'nope', principal }), { org: 'nope', moved: false, reason: 'unknown' });
|
||||
assert.deepEqual(await f.admin.rideMove({ org: 'real', principal }), { org: 'real', moved: false, reason: 'ready' });
|
||||
assert.deepEqual(await f.admin.rideMove({ org: 'live', principal }), { org: 'live', moved: false, reason: 'bus-running' });
|
||||
// A slow or busy bus that does not answer, a status that cannot be read, or no status at all is running.
|
||||
for (const org of ['busy', 'unproven', 'no-status']) assert.deepEqual(await f.admin.rideMove({ org, principal }), { org, moved: false, reason: 'bus-running' }, org);
|
||||
// A slow or busy bus that does not answer, or a status that cannot be read, is running.
|
||||
for (const org of ['busy', 'unproven']) assert.deepEqual(await f.admin.rideMove({ org, principal }), { org, moved: false, reason: 'bus-running' }, org);
|
||||
// settings-p3 R2: no status at all (the single-business Console) can never show the bus stopped, so
|
||||
// "stop its bus" would not help: moving is not available there, and whether it runs is unknown.
|
||||
assert.deepEqual(await f.admin.rideMove({ org: 'no-status', principal }), { org: 'no-status', moved: false, reason: 'not-available' });
|
||||
const listed = await f.admin.rideAll();
|
||||
assert.deepEqual(listed.fixtures, [{ id: 'stand-in', running: false }, { id: 'live', running: true }, { id: 'busy', running: true }, { id: 'unproven', running: true }, { id: 'no-status', running: true }]);
|
||||
assert.deepEqual(listed.fixtures, [{ id: 'stand-in', running: false }, { id: 'live', running: true }, { id: 'busy', running: true }, { id: 'unproven', running: true }, { id: 'no-status', running: null }]);
|
||||
// settings-p3 review P3-3: the status summary says so before any run, without asking any bus.
|
||||
assert.deepEqual((await f.admin.status()).ride.filter(r => r.state === 'fixture'), [{ id: 'stand-in', state: 'fixture' }, { id: 'live', state: 'fixture' }, { id: 'busy', state: 'fixture' }, { id: 'unproven', state: 'fixture' }, { id: 'no-status', state: 'fixture', running: null }]);
|
||||
ownerNow = { ...owner, proxy: false };
|
||||
assert.deepEqual(await f.admin.rideMove({ org: 'stand-in', principal }), { org: 'stand-in', moved: false, reason: 'no-proxy' });
|
||||
ownerNow = { ...owner, riding: false };
|
||||
|
||||
@@ -246,10 +246,42 @@ export returns:
|
||||
`minimal`, `low`, `medium`, `high`, `xhigh` and `max` as `--thinking`,
|
||||
Claude Code takes `low`, `medium`, `high`, `xhigh` and `max` as
|
||||
`--effort`.
|
||||
- `preflight({env})` returns `null` or `{code, message}`: pi checks its
|
||||
binary in `node_modules` (`harness-binary-missing`), Claude Code its
|
||||
- `preflight({env})` returns `null` or `{code, message}`, or a promise of
|
||||
one, which the launcher awaits before it launches: pi checks that
|
||||
it finds a pi binary (`harness-binary-missing`), Claude Code its
|
||||
session credential (`no-session-credential`) and `MOSAIC_CLAUDE_BIN`.
|
||||
- `version({env})` is what the manifest records as `harnessVersion`.
|
||||
Pi (#1873 pi-unpin, `harness/pi/binary.mjs`) takes `MOSAIC_PI_BIN`,
|
||||
which must be an absolute path to an executable file; else the first
|
||||
`pi` on PATH, skipping the repository's own `node_modules/.bin`; else
|
||||
the repository's copy. The binary must belong to the
|
||||
`@earendil-works/pi-coding-agent` package: it is the bin that package's
|
||||
`package.json` names. With none, the message is
|
||||
"no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci". No
|
||||
version is pinned. Pi's `mounts` and `installRoots` hold the found
|
||||
package's directory (the repository's whole `node_modules` for the
|
||||
repository's copy, whose packages are hoisted beside pi), and its `env`
|
||||
sets `MOSAIC_PI_BIN` to the binary's path inside the session, which the
|
||||
adapter runs. A refusal names the variable or "the pi on PATH" or "the
|
||||
repository's pi", never a host path. Pi's `preflight` then awaits
|
||||
the fit check (#1873 S3, `packages/harness/src/pi-fit.mjs`), which
|
||||
runs its sandboxed pi starts as child processes, four at a time, so the
|
||||
bus keeps answering, and which every caller that asks while it runs
|
||||
shares. It refuses `harness-feature-missing` when the binary lacks a flag the
|
||||
adapter passes, starts with a missing `-e` file, loads a planted ambient
|
||||
source or context file with the seal flags on, or can't be checked. When its `--help`
|
||||
lists `--no-mcp`, `env` sets `MOSAIC_PI_NO_MCP=1` and the adapter
|
||||
passes `--no-mcp`; otherwise it sets `MOSAIC_PI_NO_MCP=0`. `env`'s
|
||||
variables are built in `harness/pi/session-env.mjs`, which the fit
|
||||
check also uses so its runs get a session's names, from the bus's own
|
||||
environment as a session's are. `env` runs nothing: it reads the passed
|
||||
check for the binary's files and the bus's names, and throws when there
|
||||
is none. A host that calls `env` without `preflight` awaits the
|
||||
optional `ready({env})` first (the Triage host and route do), which runs
|
||||
the check, or shares the one running, and throws its refusal line.
|
||||
- `version({env})` is what the manifest records as `harnessVersion`, or a
|
||||
promise of it, which the launcher awaits. Pi's is the first line its
|
||||
binary prints for `--version`, or null; the binary runs as a child
|
||||
process.
|
||||
- `countsAccount()` says whether its launches spend `launch.account` and get
|
||||
the row 126 account checks. The same answer decides whether an account
|
||||
quota covers authority hires (#1873); the bus reads it once at start, so
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
// #1873 pi-unpin: the pi binary a session runs, and the package it comes from. Nothing here is
|
||||
// pinned. The version label is read from the binary (or its package.json), never compared to a
|
||||
// fixed one. The launcher, the bundle and the usage reader call these on the host.
|
||||
import { accessSync, constants, readFileSync, realpathSync, statSync } from "node:fs";
|
||||
import { dirname, isAbsolute, join, relative } from "node:path";
|
||||
import { homedir } from "node:os";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { spawn } from "node:child_process";
|
||||
|
||||
const REPO = fileURLToPath(new URL("../../../../", import.meta.url));
|
||||
export const PACKAGE = "@earendil-works/pi-coding-agent";
|
||||
export const REPO_BIN = join(REPO, "node_modules", ".bin", "pi");
|
||||
const VERSION_MAX = 128;
|
||||
|
||||
export const executable = (path) => {
|
||||
try {
|
||||
accessSync(path, constants.X_OK);
|
||||
return statSync(path).isFile();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const REPO_NODE_MODULES = join(REPO, "node_modules");
|
||||
const realOrNull = (path) => { try { return realpathSync(path); } catch { return null; } };
|
||||
const inside = (path, root) => root === "/" || path === root || path.startsWith(root + "/");
|
||||
|
||||
// #1873 S2: the binary a session runs, with the name a refusal line uses for it (never a path):
|
||||
// MOSAIC_PI_BIN, which must be an absolute path to an executable file; else the first executable
|
||||
// pi on PATH, skipping the repository's own node_modules/.bin (a session PATH starts with it, so
|
||||
// the bus's own PATH and a session's give the same answer); else the repository's copy. The binary
|
||||
// must belong to the pi package: its package directory (or, for the repository's copy, the
|
||||
// repository's node_modules) is what the sandbox mounts. The answer: { bin, label, root, view,
|
||||
// dest, pkg } or { bin: null, problem }. root is the real directory mounted, dest where it is
|
||||
// mounted (null: at root itself), view the binary's path inside the session, pkg the package's
|
||||
// real directory.
|
||||
export function locate(env = {}) {
|
||||
if (env.MOSAIC_PI_BIN !== undefined && env.MOSAIC_PI_BIN !== "") {
|
||||
const bin = env.MOSAIC_PI_BIN;
|
||||
if (typeof bin !== "string" || !isAbsolute(bin) || !executable(bin)) return { bin: null, problem: "MOSAIC_PI_BIN isn't an executable file at an absolute path" };
|
||||
return placed(bin, "MOSAIC_PI_BIN", env);
|
||||
}
|
||||
const repoBin = realOrNull(join(REPO_NODE_MODULES, ".bin"));
|
||||
for (const dir of String(env.PATH ?? "").split(":")) {
|
||||
if (!isAbsolute(dir)) continue;
|
||||
const real = realOrNull(dir);
|
||||
if (real === null || (repoBin !== null && real === repoBin)) continue;
|
||||
if (executable(join(dir, "pi"))) return placed(join(dir, "pi"), "the pi on PATH", env);
|
||||
}
|
||||
if (executable(REPO_BIN)) return placed(REPO_BIN, "the repository's pi", env);
|
||||
return { bin: null, problem: "no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci" };
|
||||
}
|
||||
|
||||
function placed(bin, label, env) {
|
||||
const pkg = packageDir(bin);
|
||||
const realBin = realOrNull(bin);
|
||||
if (!pkg || !realBin) return { bin: null, problem: `${label} isn't part of the ${PACKAGE} package` };
|
||||
// The package directory is mounted whole, so the binary must be the pi bin its package.json
|
||||
// names (a string bin, or bin.pi), inside that directory. A stray executable under some wider
|
||||
// directory whose package.json carries the name doesn't count.
|
||||
if (namedBin(pkg) !== realBin) return { bin: null, problem: `${label} isn't the pi bin the ${PACKAGE} package names` };
|
||||
// The repository's copy keeps the mount every session has had: all of node_modules, at its own
|
||||
// path, since its packages are hoisted beside pi.
|
||||
const repoReal = realOrNull(REPO_NODE_MODULES);
|
||||
if (repoReal !== null && inside(pkg, repoReal)) {
|
||||
return { bin, label, root: repoReal, dest: REPO_NODE_MODULES, view: join(REPO_NODE_MODULES, relative(repoReal, realBin)), pkg };
|
||||
}
|
||||
// Any other copy: its package directory alone, at its real path. npm nests a global install's
|
||||
// dependencies inside it; a copy that needs packages beside it fails the fit check (S3).
|
||||
// A package directory that holds the home directory or the repository would mount them.
|
||||
const held = [homedir(), env.HOME, REPO].filter((p) => typeof p === "string" && isAbsolute(p)).map((p) => realOrNull(p) ?? p);
|
||||
if (held.some((p) => inside(p, pkg))) return { bin: null, problem: `${label}'s package directory holds the home directory or the repository` };
|
||||
return { bin, label, root: pkg, dest: null, view: realBin, pkg };
|
||||
}
|
||||
|
||||
// The real path of the pi bin the package.json in `pkg` names, inside `pkg`, or null.
|
||||
function namedBin(pkg) {
|
||||
let bin;
|
||||
try { bin = JSON.parse(readFileSync(join(pkg, "package.json"), "utf8")).bin; } catch { return null; }
|
||||
const rel = typeof bin === "string" ? bin : bin && typeof bin === "object" ? bin.pi : null;
|
||||
if (typeof rel !== "string" || !rel) return null;
|
||||
const real = realOrNull(join(pkg, rel));
|
||||
return real !== null && real !== pkg && inside(real, pkg) ? real : null;
|
||||
}
|
||||
|
||||
// The package directory that holds the binary's real file: the nearest directory above it whose
|
||||
// package.json has a name. That name must be PACKAGE, or there is none (null).
|
||||
export function packageDir(bin) {
|
||||
let dir;
|
||||
try { dir = dirname(realpathSync(bin)); } catch { return null; }
|
||||
for (let depth = 0; depth < 32; depth++) {
|
||||
let pkg = null;
|
||||
try { pkg = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")); } catch { pkg = null; }
|
||||
if (pkg && typeof pkg === "object" && pkg.name !== undefined) return pkg.name === PACKAGE ? dir : null;
|
||||
const up = dirname(dir);
|
||||
if (up === dir) return null;
|
||||
dir = up;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// The found package's own version, a file read with no spawn, or null.
|
||||
export function packageVersion(bin) {
|
||||
const dir = bin ? packageDir(bin) : null;
|
||||
if (!dir) return null;
|
||||
try {
|
||||
const v = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")).version;
|
||||
return typeof v === "string" && cleanLabel(v) ? v : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// The most a --version may print (spawnSync's default buffer, which the check used before).
|
||||
const VERSION_OUTPUT_MAX = 1024 * 1024;
|
||||
// One printable line, short enough to record.
|
||||
const cleanLabel = (s) => s.length > 0 && s.length <= VERSION_MAX && !/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/.test(s);
|
||||
|
||||
// The first line `<bin> --version` prints, or null when it fails, times out or prints something
|
||||
// that is not one short printable line. The environment holds only a PATH that finds node. A
|
||||
// promise: the binary runs as a child process, so the caller's event loop keeps running.
|
||||
export function printedVersion(bin) {
|
||||
if (!bin) return Promise.resolve(null);
|
||||
return new Promise((resolve) => {
|
||||
const out = [];
|
||||
let size = 0;
|
||||
let done = false;
|
||||
let child;
|
||||
let timer = null;
|
||||
const finish = (line) => {
|
||||
if (done) return;
|
||||
done = true;
|
||||
clearTimeout(timer);
|
||||
resolve(line);
|
||||
};
|
||||
try {
|
||||
child = spawn(bin, ["--version"], { env: { PATH: `${dirname(process.execPath)}:/usr/bin:/bin`, PI_OFFLINE: "1" }, stdio: ["ignore", "pipe", "ignore"] });
|
||||
} catch {
|
||||
finish(null);
|
||||
return;
|
||||
}
|
||||
timer = setTimeout(() => { try { child.kill("SIGKILL"); } catch {} finish(null); }, 5000);
|
||||
child.stdout.on("data", (chunk) => {
|
||||
size += chunk.length;
|
||||
if (size > VERSION_OUTPUT_MAX) { try { child.kill("SIGKILL"); } catch {} finish(null); } else out.push(chunk);
|
||||
});
|
||||
child.on("error", () => finish(null));
|
||||
child.on("close", (status) => {
|
||||
const line = Buffer.concat(out).toString("utf8").split("\n")[0].trim();
|
||||
finish(status === 0 && cleanLabel(line) ? line : null);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
// `compactions` through the harness kind (packages/plugins/README.md has the contract). This file
|
||||
// reads only the session's native pi store and the Claude history beside it.
|
||||
//
|
||||
// Pi (pinned 0.85.1): a completed compaction is one `type: "compaction"` entry,
|
||||
// Pi (read from 0.85.1): a completed compaction is one `type: "compaction"` entry,
|
||||
// written by SessionManager.appendCompaction after the summary exists. A start
|
||||
// or an aborted compaction writes none. A `compactionSummary` message, a
|
||||
// message inside a compaction's retainedTail and a branch_summary are not
|
||||
@@ -19,8 +19,10 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { closeSync, constants as fsc, fstatSync, lstatSync, openSync, readdirSync, readSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { locate, packageVersion } from "./binary.mjs";
|
||||
|
||||
export const PI_VERSION = "0.85.1";
|
||||
// #1873 S1: the found package's own version (its package.json, no spawn), or null.
|
||||
const foundVersion = () => packageVersion(locate(process.env).bin);
|
||||
const TAIL = 4096;
|
||||
const CHUNK = 1 << 20;
|
||||
const STREAM = /^[A-Za-z0-9._-]{1,200}\.jsonl$/;
|
||||
@@ -158,9 +160,9 @@ export function compactions({ store, cursors, budget, streams, first }) {
|
||||
if (first && claudeHistory(store)) {
|
||||
// Claude runs before this ledger: their compactions can't be counted
|
||||
// (Vikunja 466), so the count is unknown for good, as after a Claude run.
|
||||
return { found: [], cursors: {}, known: false, reason: "harness-unsupported", version: PI_VERSION };
|
||||
return { found: [], cursors: {}, known: false, reason: "harness-unsupported", version: foundVersion() };
|
||||
}
|
||||
const scan = scanPi(store.session, cursors, budget, streams);
|
||||
const reason = REASONS.find((p) => scan.problems.includes(p)) ?? null;
|
||||
return { found: scan.found, cursors: scan.cursors, known: reason === null, reason, version: PI_VERSION };
|
||||
return { found: scan.found, cursors: scan.cursors, known: reason === null, reason, version: foundVersion() };
|
||||
}
|
||||
@@ -1,18 +1,19 @@
|
||||
// The "pi" harness plug-in (row 65, REQ-PLUG-2, Q156): a managed session on the pinned
|
||||
// @earendil-works/pi-coding-agent CLI, the repository's node_modules/.bin/pi. The tool gate is the
|
||||
// The "pi" harness plug-in (row 65, REQ-PLUG-2, Q156): a managed session on the
|
||||
// @earendil-works/pi-coding-agent CLI that binary.mjs finds (#1873: no version pin). The tool gate is the
|
||||
// Pi extension, which stays in packages/harness (a module can't change that boundary). The
|
||||
// launcher, the bundle and the usage reader call these on the host; the runner gets the answers
|
||||
// as data in session.json. packages/plugins/README.md lists the interface.
|
||||
import { existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
|
||||
import { lstatSync, mkdirSync, writeFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { createHash } from "node:crypto";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { locate, printedVersion } from "./binary.mjs";
|
||||
import { fitVerdict, piFit } from "../../../harness/src/pi-fit.mjs";
|
||||
import { envNames, harnessEnv } from "./session-env.mjs";
|
||||
|
||||
export const interfaceVersion = 1;
|
||||
|
||||
const REPO = fileURLToPath(new URL("../../../../", import.meta.url));
|
||||
const PACKAGE = "@earendil-works/pi-coding-agent";
|
||||
const BIN = join(REPO, "node_modules", ".bin", "pi");
|
||||
const EXTENSION = fileURLToPath(new URL("../../../harness/src/pi-extension.mjs", import.meta.url));
|
||||
// Row 66 (#1559): the unslop check, loaded as a second extension. It logs to MOSAIC_UNSLOP_LOG,
|
||||
// which the runner sets, and the runner tells the operator.
|
||||
@@ -31,33 +32,53 @@ export const adapter = () => join(REPO, "adapters", "pi", "adapter.sh");
|
||||
export const levels = () => LEVELS;
|
||||
export const outputPolicy = ({ proxy }) => proxy ? "non-newline-stdout" : "stdout";
|
||||
|
||||
// The session runs pi from the repository's node_modules, which the sandbox mounts.
|
||||
export function preflight({ env = {}, proxy = null } = {}) {
|
||||
// The session runs the pi binary.mjs finds, from the package directory the sandbox mounts. Async:
|
||||
// the fit check runs child processes, and the launcher awaits the answer before it launches.
|
||||
export async function preflight({ env = {}, proxy = null } = {}) {
|
||||
if (!(typeof proxy?.key === "string" && proxy.key)
|
||||
&& !["ANTHROPIC_API_KEY", "OPENAI_API_KEY", "OPENROUTER_API_KEY", "ZAI_API_KEY"].some(name => Boolean(env[name]))) {
|
||||
return { code: "no-session-credential", message: "pi needs its own provider key or a session proxy key" };
|
||||
}
|
||||
if (!existsSync(BIN)) return { code: "harness-binary-missing", message: "pi isn't installed in the repository's node_modules; run npm ci in the repository" };
|
||||
const found = locate(env);
|
||||
if (!found.bin) return { code: "harness-binary-missing", message: found.problem };
|
||||
// #1873 S3: the found binary takes every flag the adapter passes and keeps the known ambient
|
||||
// sources off with them, or the launch is refused. A check that cannot tell refuses too. The
|
||||
// check's environment comes from this process's own (the bus's), as a session's does.
|
||||
const fit = await piFit(found, { levels: LEVELS });
|
||||
if (!fit.ok) return { code: "harness-feature-missing", message: fit.message };
|
||||
return null;
|
||||
}
|
||||
|
||||
// The exact pin from the repository's package.json.
|
||||
export function version() {
|
||||
try {
|
||||
const pkg = JSON.parse(readFileSync(join(REPO, "package.json"), "utf8"));
|
||||
return pkg.dependencies?.[PACKAGE] ?? pkg.devDependencies?.[PACKAGE] ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
// #1873 S1: what the found binary prints for --version (5 s timeout), or null. A label only:
|
||||
// nothing compares it to a fixed version. Async, so the bus keeps answering while it runs.
|
||||
export async function version({ env = {} } = {}) {
|
||||
return printedVersion(locate(env).bin);
|
||||
}
|
||||
|
||||
// #1873 S3: runs the fit check, or shares the one running, and throws its refusal line when the
|
||||
// binary fails it. A host that calls env() without preflight (Triage) awaits this first.
|
||||
export async function ready({ env = process.env } = {}) {
|
||||
const fit = await piFit(found(env), { levels: LEVELS });
|
||||
if (!fit.ok) throw new Error(fit.message);
|
||||
}
|
||||
|
||||
// Pi sessions run on a provider key, never the Claude account limit.
|
||||
export const countsAccount = () => false;
|
||||
export const mounts = () => [
|
||||
{ src: join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), why: "the managed model event stream" },
|
||||
{ src: realpathSync(join(REPO, "node_modules")), dest: join(REPO, "node_modules"), why: "pi and its packages, on the session's PATH" },
|
||||
...Object.values(UNSLOP).map((src) => ({ src, why: "the unslop check the session's second extension runs" })),
|
||||
];
|
||||
// #1873 S2: the found pi's package directory (the repository's copy: all of node_modules, as before).
|
||||
// One under /usr is already in every sandbox.
|
||||
const found = (env) => {
|
||||
const at = locate(env);
|
||||
if (!at.bin) throw new Error(at.problem);
|
||||
return at;
|
||||
};
|
||||
export function mounts({ env = {} } = {}) {
|
||||
const at = found(env);
|
||||
return [
|
||||
{ src: join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), why: "the managed model event stream" },
|
||||
...(at.root.startsWith("/usr/") ? [] : [{ src: at.root, ...(at.dest ? { dest: at.dest } : {}), why: `pi and its packages (${at.label})` }]),
|
||||
...Object.values(UNSLOP).map((src) => ({ src, why: "the unslop check the session's second extension runs" })),
|
||||
];
|
||||
}
|
||||
// Pi has no web tool of its own, so a granted role under harness-builtin is refused.
|
||||
export const builtinWeb = () => false;
|
||||
export const builtins = ({ tools }) => [...tools];
|
||||
@@ -105,26 +126,33 @@ export function bundle({ dir, models, proxy = null, transports = {} } = {}) {
|
||||
return { files: { proxyModels: file }, digests: [["../home/.pi/agent/models.json", createHash("sha256").update(text).digest("hex")]], sources };
|
||||
}
|
||||
|
||||
// The tool limit is the built-ins plus the typed tools, which the extension serves.
|
||||
export function env({ policy, tools, session, proxy = null }) {
|
||||
return {
|
||||
PI_OFFLINE: "1",
|
||||
MOSAIC_TOOLS: [...policy.tools, ...tools.map((t) => t.name)].join(","),
|
||||
MOSAIC_EXTENSIONS: [EXTENSION, UNSLOP.unslopExtension].join(","),
|
||||
PI_PROVIDER: proxy ? session.proxyTransport.piProvider : session.provider ?? "",
|
||||
PI_MODEL: session.model,
|
||||
MOSAIC_THINKING: session.thinking,
|
||||
...(proxy ? { ANTHROPIC_API_KEY: proxy.key } : {}),
|
||||
};
|
||||
// The tool limit is the built-ins plus the typed tools, which the extension serves. #1873 S2:
|
||||
// MOSAIC_PI_BIN is the found binary's path inside the session; the bus's own environment names it
|
||||
// (env defaults to it, since buildBundle and the Triage host call env() without one). preflight,
|
||||
// version and mounts get the launcher's env instead. Both find the same binary: the session env
|
||||
// copies PATH and MOSAIC_PI_BIN from the bus's, and locate() skips the repository's .bin on PATH.
|
||||
// env() runs nothing: it reads the fit check that preflight or ready() awaited for what it found.
|
||||
// With none passed for those files and this environment (they differ, the files changed since, or
|
||||
// nobody awaited one), it throws, so the launch fails closed (as harness-failed, not
|
||||
// harness-feature-missing).
|
||||
export function env({ policy, tools, session, proxy = null, env: hostEnv = process.env }) {
|
||||
const at = found(hostEnv);
|
||||
// #1873 S3: a session runs only a binary that passed the fit check.
|
||||
const fit = fitVerdict(at, { levels: LEVELS });
|
||||
if (!fit) throw new Error(`pi (${at.label}) has no passed fit check for its files; preflight or ready() runs one first`);
|
||||
return harnessEnv({ bin: at.view, noMcp: fit.noMcp, tools: [...policy.tools, ...tools.map((t) => t.name)], extensions: [EXTENSION, UNSLOP.unslopExtension], session, proxy });
|
||||
}
|
||||
|
||||
// #1873 triage-fences: the names env() returns, less the provider key, and the directories its
|
||||
// adapter and mounts live in. A hosted session's host (Triage) takes harnessEnv names and mount
|
||||
// sources only from these; launches don't read them. The host refuses every MOSAIC_* name from a
|
||||
// route, so MOSAIC_TOOLS, MOSAIC_EXTENSIONS and MOSAIC_THINKING are listed only to say what env()
|
||||
// hands a session: a hosted pi needs the host to set them itself (a later slice).
|
||||
export const envNames = Object.freeze(["PI_OFFLINE", "MOSAIC_TOOLS", "MOSAIC_EXTENSIONS", "PI_PROVIDER", "PI_MODEL", "MOSAIC_THINKING"]);
|
||||
export const installRoots = () => [join(REPO, "adapters", "pi"), join(REPO, "packages", "plugins", "harness", "pi"), realpathSync(join(REPO, "node_modules")), join(REPO, "tools", "unslop-hook")];
|
||||
// hands a session: a hosted pi needs the host to set them itself (a later slice). MOSAIC_PI_BIN
|
||||
// (#1873 S2) is the binary's path inside the session, which the host keeps from its own env() call;
|
||||
// MOSAIC_PI_NO_MCP (#1873 S3) is "1" when the binary's --help lists --no-mcp, else "0".
|
||||
// session-env.mjs holds the list beside the builder.
|
||||
export { envNames };
|
||||
export const installRoots = ({ env = {} } = {}) => [join(REPO, "adapters", "pi"), join(REPO, "packages", "plugins", "harness", "pi"), found(env).root, join(REPO, "tools", "unslop-hook")];
|
||||
|
||||
// The extension writes the turn marker at agent_end; an exit without it is a failed turn.
|
||||
export const turnEnd = () => "marker";
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
// The variables the pi module hands a session (index.mjs env() returns them as harnessEnv). Built
|
||||
// here, with no fit check, so packages/harness/src/pi-fit.mjs (#1873 S3) gives its runs the same
|
||||
// names as a session gets without importing index.mjs, which imports it.
|
||||
//
|
||||
// bin: the binary's path inside the session. noMcp: the binary's --help lists --no-mcp. tools: the
|
||||
// tool names. extensions: the -e files. session: { provider, model, thinking, proxyTransport }.
|
||||
// proxy: { key } when the session runs on the proxy, else null.
|
||||
export function harnessEnv({ bin, noMcp, tools, extensions, session, proxy = null }) {
|
||||
return {
|
||||
MOSAIC_PI_BIN: bin,
|
||||
// "1" when --help lists --no-mcp: the adapter then passes it, so MCP stays off by its own
|
||||
// flag. "0" otherwise, so every name in envNames is always set.
|
||||
MOSAIC_PI_NO_MCP: noMcp ? "1" : "0",
|
||||
PI_OFFLINE: "1",
|
||||
MOSAIC_TOOLS: tools.join(","),
|
||||
MOSAIC_EXTENSIONS: extensions.join(","),
|
||||
PI_PROVIDER: proxy ? session.proxyTransport.piProvider : session.provider ?? "",
|
||||
PI_MODEL: session.model,
|
||||
MOSAIC_THINKING: session.thinking,
|
||||
...(proxy ? { ANTHROPIC_API_KEY: proxy.key } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
// #1873 triage-fences: the names harnessEnv returns, less the provider key. index.mjs exports
|
||||
// them; its comment says what a hosted session's host does with them.
|
||||
export const envNames = Object.freeze(["MOSAIC_PI_BIN", "MOSAIC_PI_NO_MCP", "PI_OFFLINE", "MOSAIC_TOOLS", "MOSAIC_EXTENSIONS", "PI_PROVIDER", "PI_MODEL", "MOSAIC_THINKING"]);
|
||||
|
||||
// #1873 pi-unpin re-check 8: the provider and model the fit check's session-shaped runs pass
|
||||
// (packages/harness/src/pi-fit.mjs SESSION_SHAPE). The runs are offline with no key, so pi stops
|
||||
// before any model call; the names only need to be ones pi takes. Re-check 9: a Claude model is
|
||||
// offered, routed and tested only on the claude-code harness, so this names no Claude model and
|
||||
// no Anthropic provider (pi-fit.test.mjs holds that); glm-5.3-flash is the model index.mjs has
|
||||
// documented limits for, and pi's own catalogue lists it under zai.
|
||||
export const FIT_ROUTE = Object.freeze({ provider: "zai", model: "glm-5.3-flash" });
|
||||
@@ -5,7 +5,12 @@ 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';
|
||||
const harnesses = upstream => upstream === 'claude' ? ['claude-code', 'pi'] : ['pi'];
|
||||
// #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'];
|
||||
const slug = (upstream, model) => `${upstream}-${model}`.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/-+$/g, '');
|
||||
|
||||
export async function catalogue({ listAccounts, listModels, listGroups, now }) {
|
||||
@@ -25,12 +30,20 @@ export async function catalogue({ listAccounts, listModels, listGroups, now }) {
|
||||
reason: upstream === 'other' || upstream === 'codex' ? 'unverified' : null, harnesses: harnesses(upstream) });
|
||||
if (models.length > MAX_MODELS) throw Error();
|
||||
};
|
||||
// Every OAuth Claude account's names, whatever its state: one disabled, cooling down or signed
|
||||
// out now can serve a request later, before the catalogue is read again.
|
||||
const oauth = new Set();
|
||||
for (const row of accounts.files) {
|
||||
if (!['claude', 'codex'].includes(row?.provider) || row.disabled === true || row.unavailable === true || !['ready', 'active'].includes(row.status)) continue;
|
||||
if (!['claude', 'codex'].includes(row?.provider)) continue;
|
||||
const listed = row.disabled !== true && row.unavailable !== true && ['ready', 'active'].includes(row.status);
|
||||
if (!listed && row.provider !== 'claude') continue;
|
||||
if (typeof row.name !== 'string' || !row.name || row.name.length > 512) throw Error();
|
||||
const response = await listModels(row.name);
|
||||
if (!Array.isArray(response?.models) || response.models.length > MAX_MODELS) throw Error();
|
||||
for (const model of response.models) add(row.provider, model?.id);
|
||||
for (const model of response.models) {
|
||||
if (row.provider === 'claude' && typeof model?.id === 'string') oauth.add(model.id.toLowerCase());
|
||||
if (listed) add(row.provider, model?.id);
|
||||
}
|
||||
}
|
||||
for (const group of groups) {
|
||||
if (!Array.isArray(group?.keys)) throw Error();
|
||||
@@ -43,6 +56,7 @@ export async function catalogue({ listAccounts, listModels, listGroups, now }) {
|
||||
if (!Array.isArray(group.models) || group.models.length > MAX_MODELS) throw Error();
|
||||
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()));
|
||||
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 } : {}) };
|
||||
@@ -61,7 +75,9 @@ const ROUTES = Object.freeze({
|
||||
claude: { routeId: 'anthropic', endpoint: 'root', api: 'anthropic-messages', piProvider: 'anthropic', compat: null },
|
||||
zai: { routeId: 'glm', endpoint: 'v1', api: 'openai-completions', piProvider: 'mosaic-proxy-glm', compat: { thinkingFormat: 'zai', zaiToolStream: true } },
|
||||
codex: { routeId: 'codex', endpoint: 'v1', api: 'openai-responses', piProvider: 'mosaic-proxy-codex', compat: null },
|
||||
other: { routeId: 'anthropic', endpoint: 'root', api: 'anthropic-messages', piProvider: 'anthropic', compat: null },
|
||||
// #1873: its own pi provider, never pi's built-in "anthropic", whose Claude models an entry there
|
||||
// would keep and send to the proxy.
|
||||
other: { routeId: 'anthropic', endpoint: 'root', api: 'anthropic-messages', piProvider: 'mosaic-proxy-other', compat: null },
|
||||
});
|
||||
export function route({ selection, harness }) {
|
||||
let upstream = selection?.upstream;
|
||||
@@ -72,7 +88,11 @@ export function route({ selection, harness }) {
|
||||
}
|
||||
if (!upstream) upstream = ({ anthropic: 'claude', zai: 'zai' })[selection?.provider];
|
||||
if (!Object.hasOwn(ROUTES, upstream ?? '')) return { code: 'proxy-provider-unsupported' };
|
||||
if (harness && !harnesses(upstream).includes(harness)) return { code: 'model-harness-incompatible' };
|
||||
// A selection that names this module came from the catalogue, which knows the account kind and is
|
||||
// checked at admission. A legacy one (a provider or route name only) gets no catalogue check, so
|
||||
// the kind is unknown and a model named as Claude is treated as OAuth.
|
||||
const legacyClaude = selection?.module !== 'cliproxy' && /claude/i.test(String(selection?.model ?? ''));
|
||||
if (harness && !harnesses(upstream, legacyClaude).includes(harness)) return { code: 'model-harness-incompatible' };
|
||||
if (typeof selection.model !== 'string' || !MODEL.test(selection.model)) return { code: 'model-provider-unavailable' };
|
||||
return { provider: selection.provider ?? upstream, model: selection.model, upstream, ...ROUTES[upstream] };
|
||||
}
|
||||
|
||||
@@ -54,6 +54,7 @@ export const KINDS = Object.freeze({
|
||||
// each one's contract:
|
||||
// adapter() -> the adapter script's absolute path levels() -> the thinking levels it applies
|
||||
// preflight({ env }) -> null | { code, message } version({ env }) -> string | null
|
||||
// (either may answer with a promise of that; the launcher awaits it before it launches)
|
||||
// countsAccount() -> true when its launches spend launch.account
|
||||
// mounts({ env }) -> read-only binds [{ src, dest, why }]
|
||||
// builtinWeb() -> true when it serves harness-builtin web
|
||||
@@ -62,6 +63,8 @@ export const KINDS = Object.freeze({
|
||||
// gateTimeout() -> the gate hook's timeouts | null
|
||||
// bundle({ dir, files, session, write, quote }) -> { files, digests, sources }, its own bundle files
|
||||
// env({ files, policy, tools, session }) -> its adapter variables, written into session.json
|
||||
// (optional, outside the list) ready({ env }) -> a promise a host that calls env() without
|
||||
// preflight awaits first; it rejects when env() would refuse (pi's fit check)
|
||||
// turnEnd() -> "marker" (a turn ends only with the turn marker) | "exit"
|
||||
// outputPolicy({ proxy }) -> "adapter" | "non-newline-stdout" | "stdout"
|
||||
// transcript({ runDir, store }) -> the directory its transcripts land in, for usage
|
||||
|
||||
@@ -3,13 +3,14 @@
|
||||
// harness outside the exceptions the brief lists.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join, relative, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { createLoader } from "../src/loader.mjs";
|
||||
import { KINDS } from "../src/kinds.mjs";
|
||||
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
|
||||
|
||||
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
|
||||
|
||||
@@ -98,11 +99,11 @@ test("row 65 claude-code version asks MOSAIC_CLAUDE_BIN when set, else claude on
|
||||
assert.equal(claude.version({ env: { PATH: join(dir, "none") } }), null);
|
||||
});
|
||||
|
||||
test("row 65 pi preflight refuses missing credentials, and its version is the package pin", async () => {
|
||||
test("row 65 pi preflight refuses missing credentials, and its version is what the installed pi prints (#1873 S1)", async () => {
|
||||
const pi = await load("pi");
|
||||
assert.equal(pi.preflight({ env: {} }).code, "no-session-credential");
|
||||
const pkg = JSON.parse(readFileSync(join(REPO, "package.json"), "utf8"));
|
||||
assert.equal(pi.version({ env: {} }), pkg.dependencies?.["@earendil-works/pi-coding-agent"] ?? pkg.devDependencies?.["@earendil-works/pi-coding-agent"]);
|
||||
assert.equal((await pi.preflight({ env: {} })).code, "no-session-credential");
|
||||
const installed = JSON.parse(readFileSync(join(REPO, "node_modules", "@earendil-works", "pi-coding-agent", "package.json"), "utf8")).version;
|
||||
assert.equal(await pi.version({ env: {} }), installed);
|
||||
// Row 66: the unslop extension's files, bound read-only at their own paths.
|
||||
assert.deepEqual(pi.mounts({ env: {} }).map(m => m.dest ?? m.src), [join(REPO, "packages/plugins/harness/pi/model-stream.mjs"), join(REPO, "node_modules"), ...["extension.ts", "unslop-check.js", "lists.json"].map(f => join(REPO, "tools", "unslop-hook", f))]);
|
||||
});
|
||||
@@ -110,6 +111,8 @@ test("row 65 pi preflight refuses missing credentials, and its version is the pa
|
||||
test("row 65 env passes the thinking level to both adapters, and claude-code's bundle writes its hook and MCP files", async t => {
|
||||
const pi = await load("pi"), claude = await load("claude-code");
|
||||
const session = { provider: "fixture", model: "m", thinking: "high", toolSocket: "/run/tools.sock" };
|
||||
// #1873 S3: env() reads the fit check ready() awaits for the found pi.
|
||||
await pi.ready();
|
||||
const piEnv = pi.env({ policy: { tools: ["read"] }, tools: [{ name: "list_tasks" }], session });
|
||||
assert.equal(piEnv.PI_OFFLINE, "1", "pi avoids startup network operations");
|
||||
assert.deepEqual([piEnv.MOSAIC_THINKING, piEnv.PI_MODEL, piEnv.MOSAIC_TOOLS], ["high", "m", "read,list_tasks"]);
|
||||
@@ -140,10 +143,16 @@ test("row 65 a route's thinking reaches pi as --thinking and claude as --effort,
|
||||
const pi = await load("pi"), claude = await load("claude-code");
|
||||
const dir = scratch(t);
|
||||
mkdirSync(join(dir, "bin"));
|
||||
for (const name of ["pi", "claude"]) {
|
||||
writeFileSync(join(dir, "bin", name), '#!/bin/sh\nprintf "%s\\n" "$@" > "$ARGV_OUT"\necho \'{"type":"result","subtype":"success","result":"ok"}\'\n');
|
||||
chmodSync(join(dir, "bin", name), 0o755);
|
||||
}
|
||||
// #1873 S2: pi's stand-in sits in a pi package, as the module requires, and is found on PATH.
|
||||
const piPackage = join(dir, "lib", "node_modules", "@earendil-works", "pi-coding-agent");
|
||||
mkdirSync(join(piPackage, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(piPackage, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "0.0.1", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
const result = '{"type":"result","subtype":"success","result":"ok"}\n';
|
||||
writeFileSync(join(dir, "bin", "claude"), `#!/bin/sh\nprintf "%s\\n" "$@" > "$ARGV_OUT"\necho '${result.trim()}'\n`);
|
||||
// #1873 S3: pi's stand-in also passes the fit check that ready() runs and env() reads.
|
||||
writeFileSync(join(piPackage, "dist", "bundle", "cli.js"), piStandIn({ sessionOut: result }));
|
||||
for (const file of [join(dir, "bin", "claude"), join(piPackage, "dist", "bundle", "cli.js")]) chmodSync(file, 0o755);
|
||||
symlinkSync(join(piPackage, "dist", "bundle", "cli.js"), join(dir, "bin", "pi"));
|
||||
for (const f of ["prompt.md", "settings.json", "mcp.json"]) writeFileSync(join(dir, f), "{}");
|
||||
const run = (adapter, moduleEnv) => {
|
||||
const out = join(dir, "argv");
|
||||
@@ -163,9 +172,10 @@ test("row 65 a route's thinking reaches pi as --thinking and claude as --effort,
|
||||
assert.equal(argv.filter(a => a === name).length, 1, argv.join(" "));
|
||||
return argv[argv.indexOf(name) + 1];
|
||||
};
|
||||
await pi.ready({ env: { PATH: `${join(dir, "bin")}:/usr/bin:/bin` } });
|
||||
for (const thinking of ["xhigh", "low"]) {
|
||||
const session = { provider: "fixture", model: "m", thinking, toolSocket: "/run/tools.sock" };
|
||||
assert.equal(flag(run("pi", pi.env({ policy: { tools: ["read"] }, tools: [], session })), "--thinking"), thinking);
|
||||
assert.equal(flag(run("pi", pi.env({ policy: { tools: ["read"] }, tools: [], session, env: { PATH: `${join(dir, "bin")}:/usr/bin:/bin` } })), "--thinking"), thinking);
|
||||
const files = { settings: join(dir, "settings.json"), mcp: join(dir, "mcp.json") };
|
||||
assert.equal(flag(run("claude", claude.env({ files, policy: { tools: ["read"], typed: [] }, session })), "--effort"), thinking);
|
||||
}
|
||||
|
||||
@@ -17,15 +17,16 @@ test('catalogue attributes configured accounts, uses aliases, and enforces harne
|
||||
const result = await catalogue(deps());
|
||||
assert.equal(result.status, 'ready');
|
||||
const zai = result.models.find(x => x.upstream === 'zai');
|
||||
assert.deepEqual([zai.key, zai.model, zai.available, zai.harnesses], ['zai-fixture-alias', 'fixture-alias', true, ['pi']]);
|
||||
assert.deepEqual([zai.key, zai.model, zai.available, zai.harnesses], ['zai-fixture-alias', 'fixture-alias', true, ['claude-code', 'pi']]);
|
||||
const claude = result.models.find(x => x.upstream === 'claude');
|
||||
assert.deepEqual(claude.harnesses, ['claude-code', 'pi']);
|
||||
assert.deepEqual(preflight({ selection: zai, harness: 'claude-code', catalogue: result }), { allow: false, code: 'model-harness-incompatible' });
|
||||
assert.deepEqual(claude.harnesses, ['claude-code']);
|
||||
assert.deepEqual(preflight({ selection: zai, harness: 'claude-code', catalogue: result }), { allow: true });
|
||||
assert.deepEqual(preflight({ selection: zai, harness: 'fixture-harness', catalogue: result }), { allow: false, code: 'model-harness-incompatible' });
|
||||
assert.deepEqual(preflight({ selection: zai, harness: 'pi', catalogue: result }), { allow: true });
|
||||
assert.ok(!JSON.stringify(result).includes('credential-1'));
|
||||
});
|
||||
|
||||
test('an empty group offers no models; unknown groups offer pi only and are unverified', async () => {
|
||||
test('an empty group offers no models; unknown groups offer claude-code and pi and are unverified', async () => {
|
||||
const d = deps();
|
||||
d.listAccounts = async () => ({ files: [] });
|
||||
d.listGroups = async () => [
|
||||
@@ -34,7 +35,7 @@ test('an empty group offers no models; unknown groups offer pi only and are unve
|
||||
];
|
||||
const c = await catalogue(d);
|
||||
assert.equal(c.models.length, 1);
|
||||
assert.deepEqual([c.models[0].upstream, c.models[0].harnesses, c.models[0].reason], ['other', ['pi'], 'unverified']);
|
||||
assert.deepEqual([c.models[0].upstream, c.models[0].harnesses, c.models[0].reason], ['other', ['claude-code', 'pi'], 'unverified']);
|
||||
assert.ok(c.notices.includes('z.ai account has no models configured. Add them on Model proxy.'));
|
||||
});
|
||||
|
||||
@@ -50,7 +51,7 @@ test('module declares only supported transports and legacy routes without wideni
|
||||
assert.deepEqual(route({ selection: { upstream: 'codex', model: 'fixture-codex' }, harness: 'pi' }), {
|
||||
provider: 'codex', model: 'fixture-codex', upstream: 'codex', routeId: 'codex', endpoint: 'v1', api: 'openai-responses', piProvider: 'mosaic-proxy-codex', compat: null,
|
||||
});
|
||||
assert.equal(route({ selection: { upstream: 'zai', model: 'fixture-zai' }, harness: 'claude-code' }).code, 'model-harness-incompatible');
|
||||
assert.equal(route({ selection: { upstream: 'zai', model: 'fixture-zai' }, harness: 'fixture-harness' }).code, 'model-harness-incompatible');
|
||||
assert.equal(route({ selection: { provider: 'proxy', route: 'unknown', model: 'fixture' }, harness: 'pi' }).code, 'proxy-route-unknown');
|
||||
assert.equal(route({ selection: { provider: 'proxy', model: 'fixture' }, harness: 'pi' }).code, 'proxy-route-missing');
|
||||
});
|
||||
@@ -68,3 +69,113 @@ test('a second fixture module resolves independently and ambiguous legacy mappin
|
||||
assert.equal(selectModelProvider([first,second],{...entry,module:'first-fixture'}),null);
|
||||
assert.equal(selectModelProvider([first,second,{...second,name:'third-fixture'}],entry),null);
|
||||
});
|
||||
|
||||
// #1873 harness-matrix (Jason 2026-11-19): a Claude model from an OAuth account runs on
|
||||
// claude-code only; every other proxy model runs on claude-code and pi.
|
||||
test('an OAuth Claude model is offered on claude-code only and refused on pi at catalogue, preflight and route', async () => {
|
||||
const { route } = await import('../model-provider/cliproxy/index.mjs');
|
||||
const c = await catalogue(deps());
|
||||
const claude = c.models.find(x => x.upstream === 'claude');
|
||||
assert.deepEqual(claude.harnesses, ['claude-code']);
|
||||
assert.deepEqual(preflight({ selection: claude, harness: 'pi', catalogue: c }), { allow: false, code: 'model-harness-incompatible' });
|
||||
assert.deepEqual(preflight({ selection: claude, harness: 'claude-code', catalogue: c }), { allow: true });
|
||||
for (const selection of [{ upstream: 'claude', model: 'fixture-chat' }, { provider: 'anthropic', model: 'fixture-chat' }, { provider: 'proxy', route: 'anthropic', model: 'fixture-chat' }]) {
|
||||
assert.equal(route({ selection, harness: 'pi' }).code, 'model-harness-incompatible', JSON.stringify(selection));
|
||||
assert.equal(route({ selection, harness: 'claude-code' }).upstream, 'claude');
|
||||
}
|
||||
});
|
||||
|
||||
test('a z.ai and a codex model run on claude-code and pi', async () => {
|
||||
const { route } = await import('../model-provider/cliproxy/index.mjs');
|
||||
const d = deps();
|
||||
d.listAccounts = async () => ({ files: [{ name: 'credential-3', provider: 'codex', status: 'active' }] });
|
||||
d.listModels = async () => ({ models: [{ id: 'fixture-codex' }] });
|
||||
const c = await catalogue(d);
|
||||
for (const upstream of ['zai', 'codex']) {
|
||||
const m = c.models.find(x => x.upstream === upstream);
|
||||
assert.deepEqual(m.harnesses, ['claude-code', 'pi'], upstream);
|
||||
for (const harness of ['claude-code', 'pi']) {
|
||||
assert.deepEqual(preflight({ selection: m, harness, catalogue: c }), { allow: true }, `${upstream} ${harness}`);
|
||||
assert.equal(route({ selection: { upstream, model: m.model }, harness }).upstream, upstream, `${upstream} ${harness}`);
|
||||
}
|
||||
}
|
||||
assert.equal(route({ selection: { provider: 'zai', model: 'fixture-zai' }, harness: 'claude-code' }).routeId, 'glm');
|
||||
assert.equal(route({ selection: { provider: 'proxy', route: 'codex', model: 'fixture-codex' }, harness: 'claude-code' }).routeId, 'codex');
|
||||
});
|
||||
|
||||
test('an API-key model runs on claude-code and pi whatever its name; the rule keys on the account kind', async () => {
|
||||
const { route } = await import('../model-provider/cliproxy/index.mjs');
|
||||
const d = deps();
|
||||
// Jason 2026-11-19: a Claude model from an API key runs on any harness. A name that says Claude,
|
||||
// or an OpenRouter-style anthropic/ name, is not an OAuth account.
|
||||
d.listGroups = async () => [{ 'base-url': 'https://fixture.invalid', keys: [{ auth_index: 'k' }], models: [
|
||||
{ name: 'upstream-x', alias: 'Claude-Fixture' }, { name: 'anthropic/claude-sonnet-5-5' }, { name: 'claude-upstream', alias: 'fixture-safe' }] },
|
||||
{ 'base-url': ZAI, keys: [{ auth_index: 'z' }], models: [{ name: 'claude-zai-fixture' }] }];
|
||||
const c = await catalogue(d);
|
||||
for (const [upstream, model] of [['other', 'Claude-Fixture'], ['other', 'anthropic/claude-sonnet-5-5'], ['other', 'fixture-safe'], ['zai', 'claude-zai-fixture']]) {
|
||||
const row = c.models.find(x => x.upstream === upstream && x.model === model);
|
||||
assert.deepEqual(row.harnesses, ['claude-code', 'pi'], model);
|
||||
for (const harness of ['claude-code', 'pi'])
|
||||
assert.deepEqual(preflight({ selection: row, harness, catalogue: c }), { allow: true }, `${model} ${harness}`);
|
||||
}
|
||||
for (const upstream of ['other', 'zai', 'codex'])
|
||||
for (const model of ['claude-fixture', 'anthropic/claude-sonnet-5-5', 'fixture-safe'])
|
||||
for (const harness of ['claude-code', 'pi'])
|
||||
assert.equal(route({ selection: { module: 'cliproxy', upstream, model }, harness }).upstream, upstream, `${upstream} ${model} ${harness}`);
|
||||
});
|
||||
|
||||
test('a legacy selection carries no account data, so a Claude-named model on it is treated as OAuth and held to claude-code', async () => {
|
||||
const { route } = await import('../model-provider/cliproxy/index.mjs');
|
||||
// A legacy entry (a provider or route name, no module) gets no catalogue check at launch, and the
|
||||
// proxy may serve its name from an OAuth Claude account.
|
||||
for (const selection of [{ provider: 'zai', model: 'claude-opus-5-5' }, { provider: 'proxy', route: 'glm', model: 'Claude-Opus-5-5' },
|
||||
{ provider: 'proxy', route: 'codex', model: 'claude-sonnet-5-5' }, { upstream: 'other', model: 'anthropic/claude-sonnet-5-5' }]) {
|
||||
assert.equal(route({ selection, harness: 'pi' }).code, 'model-harness-incompatible', JSON.stringify(selection));
|
||||
assert.equal(route({ selection, harness: 'claude-code' }).model, selection.model, JSON.stringify(selection));
|
||||
// The same name from the catalogue, where the account kind is known, runs on pi.
|
||||
if (selection.upstream || selection.provider === 'zai') assert.equal(route({ selection: { ...selection, module: 'cliproxy' }, harness: 'pi' }).model, selection.model);
|
||||
}
|
||||
for (const selection of [{ provider: 'zai', model: 'glm-fixture' }, { provider: 'proxy', route: 'codex', model: 'gpt-fixture' }])
|
||||
assert.equal(route({ selection, harness: 'pi' }).model, selection.model, JSON.stringify(selection));
|
||||
});
|
||||
|
||||
test('an API-key model whose name an OAuth Claude account offers stays on claude-code, the account listed or not (fail closed)', async () => {
|
||||
const { route } = await import('../model-provider/cliproxy/index.mjs');
|
||||
// The proxy routes by model name across every account, so a group model named as an OAuth Claude
|
||||
// account's model can be served by that account. A disabled, unavailable or signed-out account
|
||||
// can serve it later, before the catalogue is read again, so its names count too.
|
||||
for (const state of [{ status: 'active' }, { status: 'active', disabled: true }, { status: 'active', unavailable: true }, { status: 'error' }]) {
|
||||
const d = deps();
|
||||
d.listAccounts = async () => ({ files: [{ name: 'credential-1', provider: 'claude', ...state }, { name: 'credential-2', provider: 'codex', status: 'active' }] });
|
||||
d.listModels = async name => ({ models: name === 'credential-1' ? [{ id: 'Fixture-Chat' }] : [{ id: 'fixture-codex' }] });
|
||||
d.listGroups = async () => [{ 'base-url': 'https://fixture.invalid', keys: [{ auth_index: 'k' }], models: [{ name: 'fixture-chat' }, { name: 'fixture-safe' }] },
|
||||
{ 'base-url': ZAI, keys: [{ auth_index: 'z' }], models: [{ name: 'upstream', alias: 'FIXTURE-CHAT' }] }];
|
||||
const c = await catalogue(d);
|
||||
const label = JSON.stringify(state);
|
||||
assert.equal(c.status, 'ready', label);
|
||||
const by = (upstream, model) => c.models.find(x => x.upstream === upstream && x.model === model);
|
||||
assert.deepEqual(by('other', 'fixture-chat').harnesses, ['claude-code'], label);
|
||||
assert.deepEqual(by('zai', 'FIXTURE-CHAT').harnesses, ['claude-code'], label);
|
||||
assert.deepEqual(by('other', 'fixture-safe').harnesses, ['claude-code', 'pi'], label);
|
||||
assert.deepEqual(by('codex', 'fixture-codex').harnesses, ['claude-code', 'pi'], label);
|
||||
assert.deepEqual(preflight({ selection: by('other', 'fixture-chat'), harness: 'pi', catalogue: c }), { allow: false, code: 'model-harness-incompatible' }, label);
|
||||
// Only a usable account's models are offered as rows.
|
||||
assert.equal(c.models.some(x => x.upstream === 'claude'), state.status === 'active' && !state.disabled && !state.unavailable, label);
|
||||
}
|
||||
// route() has no account data: it holds upstream claude only, and admission holds the rest by the catalogue.
|
||||
assert.equal(route({ selection: { upstream: 'other', model: 'fixture-chat' }, harness: 'pi' }).upstream, 'other');
|
||||
});
|
||||
|
||||
test('a Claude OAuth account whose models cannot be read makes the catalogue unavailable, listed or not', async () => {
|
||||
for (const state of [{ status: 'active' }, { status: 'active', disabled: true }]) {
|
||||
const d = deps();
|
||||
d.listAccounts = async () => ({ files: [{ name: 'credential-1', provider: 'claude', ...state }] });
|
||||
d.listModels = async () => { throw new Error('fixture private error'); };
|
||||
assert.equal((await catalogue(d)).status, 'unavailable', JSON.stringify(state));
|
||||
}
|
||||
// A codex account that is not usable is not read at all.
|
||||
const d = deps();
|
||||
d.listAccounts = async () => ({ files: [{ name: 'credential-2', provider: 'codex', status: 'active', disabled: true }] });
|
||||
d.listModels = async () => { throw new Error('fixture private error'); };
|
||||
assert.equal((await catalogue(d)).status, 'ready');
|
||||
});
|
||||
@@ -0,0 +1,216 @@
|
||||
// #1873 pi-unpin (design section 5, S1): pi's labels come from the binary the module finds, not
|
||||
// from a pin. The version is what `<bin> --version` prints, or null when that fails; the
|
||||
// compaction count records the found package's own version.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { sandboxProblem } from "../../seat/src/sandbox.mjs";
|
||||
import { piStandIn } from "../../harness/tests/pi-stand-in.mjs";
|
||||
|
||||
const REPO = resolve(fileURLToPath(new URL("../../../", import.meta.url)));
|
||||
const MODULE = join(REPO, "packages", "plugins", "harness", "pi");
|
||||
const NAME = "@earendil-works/pi-coding-agent";
|
||||
|
||||
function scratch(t) {
|
||||
const root = mkdtempSync(join(tmpdir(), "mosaic-pi-bin-"));
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
return root;
|
||||
}
|
||||
|
||||
// A pi package under `dir` (a node_modules directory), with `script` as its bin.
|
||||
function standIn(dir, script, version) {
|
||||
const pkg = join(dir, NAME);
|
||||
mkdirSync(join(pkg, "dist", "bundle"), { recursive: true });
|
||||
writeFileSync(join(pkg, "package.json"), JSON.stringify({ name: NAME, version, bin: { pi: "dist/bundle/cli.js" } }));
|
||||
writeFileSync(join(pkg, "dist", "bundle", "cli.js"), script, { mode: 0o755 });
|
||||
return join(pkg, "dist", "bundle", "cli.js");
|
||||
}
|
||||
|
||||
// A copy of the pi module in a scratch repository whose node_modules holds a stand-in, so the
|
||||
// module's repository copy is that stand-in and not the real pi. The rest of packages/ links to
|
||||
// the real tree, for the modules the copy imports.
|
||||
async function copyRepo(t, script, version) {
|
||||
const root = scratch(t);
|
||||
const mod = join(root, "packages", "plugins", "harness", "pi");
|
||||
mkdirSync(mod, { recursive: true });
|
||||
for (const f of readdirSync(MODULE)) if (f.endsWith(".mjs")) copyFileSync(join(MODULE, f), join(mod, f));
|
||||
for (const p of readdirSync(join(REPO, "packages"))) if (p !== "plugins") symlinkSync(join(REPO, "packages", p), join(root, "packages", p));
|
||||
standIn(join(root, "node_modules"), script, version);
|
||||
mkdirSync(join(root, "node_modules", ".bin"));
|
||||
symlinkSync(`../${NAME}/dist/bundle/cli.js`, join(root, "node_modules", ".bin", "pi"));
|
||||
return { root, pi: await import(pathToFileURL(join(mod, "index.mjs")).href) };
|
||||
}
|
||||
|
||||
const emptyStore = (t) => {
|
||||
const root = scratch(t);
|
||||
mkdirSync(join(root, "session"));
|
||||
return { session: join(root, "session"), projects: join(root, "claude-projects") };
|
||||
};
|
||||
|
||||
test("pi-unpin S1: the version is what the found binary prints, and the compaction count records its package's version", async t => {
|
||||
const { pi } = await copyRepo(t, "#!/bin/sh\n[ \"$1\" = --version ] && echo 9.9.9\n", "9.9.4");
|
||||
assert.equal(await pi.version({ env: {} }), "9.9.9");
|
||||
const counted = pi.compactions({ store: emptyStore(t), cursors: {}, budget: 1 << 20, streams: 16, first: false });
|
||||
assert.equal(counted.version, "9.9.4");
|
||||
});
|
||||
|
||||
test("pi-unpin S1: a binary that exits 1 on --version gives a null version, not a guess", async t => {
|
||||
const { pi } = await copyRepo(t, "#!/bin/sh\nexit 1\n", "8.8.8");
|
||||
assert.equal(await pi.version({ env: {} }), null);
|
||||
// The package's own file still names its version for the compaction count.
|
||||
assert.equal(pi.compactions({ store: emptyStore(t), cursors: {}, budget: 1 << 20, streams: 16, first: false }).version, "8.8.8");
|
||||
});
|
||||
|
||||
test("pi-unpin S1: a version line with a control character or over 128 characters is not recorded", async t => {
|
||||
const { pi } = await copyRepo(t, "#!/bin/sh\nprintf '1.0\\033[31m\\n'\n", "1.0.0");
|
||||
assert.equal(await pi.version({ env: {} }), null);
|
||||
const long = await copyRepo(t, `#!/bin/sh\necho ${"9".repeat(129)}\n`, "1.0.0");
|
||||
assert.equal(await long.pi.version({ env: {} }), null);
|
||||
});
|
||||
|
||||
test("pi-unpin S1: the repository's real pi reports its installed package's version", async () => {
|
||||
const pi = await import(pathToFileURL(join(MODULE, "index.mjs")).href);
|
||||
const installed = JSON.parse(readFileSync(join(REPO, "node_modules", NAME, "package.json"), "utf8")).version;
|
||||
assert.equal(await pi.version({ env: {} }), installed);
|
||||
});
|
||||
|
||||
// #1873 pi-unpin (design section 5, S2): the binary comes from MOSAIC_PI_BIN, else the first pi on
|
||||
// PATH, else the repository's copy. Mounts, install roots and the session's MOSAIC_PI_BIN follow
|
||||
// the package that binary belongs to. A refusal names the variable, never a path.
|
||||
const real = await import(pathToFileURL(join(MODULE, "index.mjs")).href);
|
||||
const CREDENTIAL = { ZAI_API_KEY: "fixture-zai" };
|
||||
// #1873 S3: the stand-in passes the fit check, prints 7.7.7 for --version, and as a session
|
||||
// records its argv in $ARGV_OUT.
|
||||
const ARGV_SCRIPT = piStandIn({ version: "7.7.7" });
|
||||
|
||||
// npm's global layout: prefix/lib/node_modules/<package>, with prefix/bin/pi a link to its bin.
|
||||
function prefix(t, script = ARGV_SCRIPT, version = "7.7.0") {
|
||||
const root = join(scratch(t), "prefix");
|
||||
const cli = standIn(join(root, "lib", "node_modules"), script, version);
|
||||
mkdirSync(join(root, "bin"), { recursive: true });
|
||||
symlinkSync(`../lib/node_modules/${NAME}/dist/bundle/cli.js`, join(root, "bin", "pi"));
|
||||
return { root, bin: join(root, "bin", "pi"), cli: realpathSync(cli), pkg: realpathSync(join(root, "lib", "node_modules", NAME)) };
|
||||
}
|
||||
const session = { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" };
|
||||
// pi.env() reads the fit check that preflight awaited for the same files.
|
||||
const piEnv = (env) => real.env({ policy: { tools: ["read"] }, tools: [], session, env });
|
||||
const noPath = (problem, ...paths) => { for (const p of paths) assert.ok(!problem.message.includes(p), problem.message); };
|
||||
|
||||
test("pi-unpin S2: a stand-in pi in an npm prefix, named by MOSAIC_PI_BIN, is found, mounted alone and run from the mount", async t => {
|
||||
const p = prefix(t);
|
||||
const env = { ...CREDENTIAL, MOSAIC_PI_BIN: p.bin, PATH: "/usr/bin:/bin" };
|
||||
assert.equal(await real.preflight({ env }), null);
|
||||
assert.equal(await real.version({ env }), "7.7.7");
|
||||
const mounts = real.mounts({ env });
|
||||
assert.deepEqual(mounts.filter(m => (m.dest ?? m.src) === p.pkg || m.src === p.pkg).map(m => m.src), [p.pkg]);
|
||||
assert.ok(!mounts.some(m => (m.dest ?? m.src) === join(REPO, "node_modules")), "the repository's node_modules is not mounted");
|
||||
const roots = real.installRoots({ env });
|
||||
assert.ok(roots.includes(p.pkg));
|
||||
assert.ok(!roots.includes(realpathSync(join(REPO, "node_modules"))));
|
||||
const given = piEnv(env);
|
||||
assert.equal(given.MOSAIC_PI_BIN, p.cli, "the session runs the real file inside the mounted package");
|
||||
assert.ok(real.envNames.includes("MOSAIC_PI_BIN"));
|
||||
|
||||
// Run the real adapter in a sandbox that holds the system, the adapter and the module's mounts,
|
||||
// and not the repository's node_modules: the stand-in records the argv it was given.
|
||||
if (sandboxProblem()) return t.skip("no usable bwrap");
|
||||
const out = join(scratch(t), "out");
|
||||
mkdirSync(out);
|
||||
writeFileSync(join(out, "prompt.md"), "p");
|
||||
const argv = ["/usr/bin/bwrap", "--unshare-user", "--unshare-pid", "--unshare-net", "--die-with-parent", "--clearenv", "--ro-bind", "/usr", "/usr"];
|
||||
for (const link of ["/bin", "/sbin", "/lib", "/lib64"]) {
|
||||
if (!existsSync(link)) continue;
|
||||
if (lstatSync(link).isSymbolicLink()) argv.push("--symlink", readlinkSync(link), link);
|
||||
else argv.push("--ro-bind", link, link);
|
||||
}
|
||||
argv.push("--proc", "/proc", "--dev", "/dev", "--ro-bind", join(REPO, "adapters", "pi"), join(REPO, "adapters", "pi"));
|
||||
for (const m of mounts) argv.push("--ro-bind", m.src, m.dest ?? m.src);
|
||||
// The extensions the adapter checks for, which a launch's bundle binds.
|
||||
for (const e of given.MOSAIC_EXTENSIONS.split(",")) argv.push("--ro-bind", e, e);
|
||||
argv.push("--bind", out, out);
|
||||
for (const [k, v] of Object.entries({ ...given, PATH: "/usr/bin:/bin", ARGV_OUT: join(out, "argv"), MOSAIC_SYSTEM_PROMPT_FILE: join(out, "prompt.md"),
|
||||
MOSAIC_REQUEST: "x", MOSAIC_WORKSPACE: join(out, "ws"), MOSAIC_SESSION_DIR: join(out, "session"), MOSAIC_MODEL: "m" })) argv.push("--setenv", k, v);
|
||||
argv.push("/bin/sh", join(REPO, "adapters", "pi", "adapter.sh"));
|
||||
const r = spawnSync(argv[0], argv.slice(1), { encoding: "utf8", timeout: 20000 });
|
||||
assert.equal(r.status, 0, r.stderr);
|
||||
const ran = readFileSync(join(out, "argv"), "utf8").split("\n");
|
||||
assert.ok(ran.includes("--no-context-files") && ran.includes("--thinking"), ran.join(" "));
|
||||
});
|
||||
|
||||
test("pi-unpin S2: a relative or non-executable MOSAIC_PI_BIN is refused with harness-binary-missing, naming the variable only", async t => {
|
||||
const p = prefix(t);
|
||||
const plain = join(scratch(t), "pi");
|
||||
writeFileSync(plain, "#!/bin/sh\n", { mode: 0o644 });
|
||||
for (const bin of ["bin/pi", "pi", plain, join(p.root, "missing")]) {
|
||||
const problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: bin, PATH: `${join(p.root, "bin")}:/usr/bin:/bin` } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", "MOSAIC_PI_BIN isn't an executable file at an absolute path"], bin);
|
||||
noPath(problem, plain, p.root);
|
||||
assert.equal(await real.version({ env: { MOSAIC_PI_BIN: bin } }), null, "no fallback to another pi");
|
||||
}
|
||||
});
|
||||
|
||||
test("pi-unpin S2: with MOSAIC_PI_BIN unset the first pi on PATH is used, and the repository's own .bin on PATH does not count", async t => {
|
||||
const p = prefix(t);
|
||||
const env = { ...CREDENTIAL, PATH: `${join(REPO, "node_modules", ".bin")}:/nonexistent:relative:${join(p.root, "bin")}:/usr/bin:/bin` };
|
||||
assert.equal(await real.preflight({ env }), null);
|
||||
assert.equal(await real.version({ env }), "7.7.7");
|
||||
assert.equal(piEnv(env).MOSAIC_PI_BIN, p.cli);
|
||||
assert.ok(real.mounts({ env }).some(m => m.src === p.pkg));
|
||||
});
|
||||
|
||||
test("pi-unpin S2: with neither, the repository's copy is used and mounted as before", async () => {
|
||||
for (const PATH of ["/usr/bin:/bin", `${join(REPO, "node_modules", ".bin")}:/usr/bin:/bin`]) {
|
||||
const env = { ...CREDENTIAL, PATH };
|
||||
assert.equal(await real.preflight({ env }), null, PATH);
|
||||
const nm = realpathSync(join(REPO, "node_modules"));
|
||||
assert.deepEqual(real.mounts({ env }).filter(m => m.dest === join(REPO, "node_modules")).map(m => m.src), [nm]);
|
||||
assert.ok(real.installRoots({ env }).includes(nm));
|
||||
const bin = piEnv(env).MOSAIC_PI_BIN;
|
||||
assert.ok(bin.startsWith(join(REPO, "node_modules") + "/"), bin);
|
||||
assert.equal(realpathSync(bin), realpathSync(join(REPO, "node_modules", ".bin", "pi")));
|
||||
}
|
||||
});
|
||||
|
||||
test("pi-unpin S2: a pi that is not in a pi package is refused, and so is a pi that cannot be found at all", async t => {
|
||||
const dir = join(scratch(t), "bin");
|
||||
mkdirSync(dir);
|
||||
writeFileSync(join(dir, "pi"), "#!/bin/sh\necho 1.0.0\n");
|
||||
chmodSync(join(dir, "pi"), 0o755);
|
||||
let problem = await real.preflight({ env: { ...CREDENTIAL, PATH: `${dir}:/usr/bin:/bin` } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `the pi on PATH isn't part of the ${NAME} package`]);
|
||||
problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(dir, "pi") } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't part of the ${NAME} package`]);
|
||||
noPath(problem, dir);
|
||||
// The scratch copy has no node_modules of its own: no pi anywhere.
|
||||
const { root, pi } = await copyRepo(t, "#!/bin/sh\n", "1.0.0");
|
||||
rmSync(join(root, "node_modules"), { recursive: true });
|
||||
problem = await pi.preflight({ env: { ...CREDENTIAL, PATH: "/usr/bin:/bin" } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", "no pi found: set MOSAIC_PI_BIN, put pi on PATH, or run npm ci"]);
|
||||
assert.throws(() => pi.env({ policy: { tools: [] }, tools: [], session, env: { PATH: "/usr/bin:/bin" } }), /no pi found/);
|
||||
});
|
||||
|
||||
// Review finding 8: the package directory is mounted whole, so the binary must be the pi bin its
|
||||
// package.json names. A stray executable under a directory whose package.json carries the name
|
||||
// (a wide tree, say) is refused, naming the variable only.
|
||||
test("pi-unpin S2 review: a binary that isn't the bin its package names is refused", async t => {
|
||||
const wide = scratch(t);
|
||||
writeFileSync(join(wide, "package.json"), JSON.stringify({ name: NAME, version: "1.0.0", bin: { pi: "dist/bundle/cli.js" } }));
|
||||
mkdirSync(join(wide, "tools"));
|
||||
writeFileSync(join(wide, "tools", "pi"), "#!/bin/sh\necho 1.0.0\n", { mode: 0o755 });
|
||||
let problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(wide, "tools", "pi") } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't the pi bin the ${NAME} package names`]);
|
||||
noPath(problem, wide);
|
||||
// A package.json with no bin names none.
|
||||
writeFileSync(join(wide, "package.json"), JSON.stringify({ name: NAME, version: "1.0.0" }));
|
||||
problem = await real.preflight({ env: { ...CREDENTIAL, MOSAIC_PI_BIN: join(wide, "tools", "pi") } });
|
||||
assert.deepEqual([problem?.code, problem?.message], ["harness-binary-missing", `MOSAIC_PI_BIN isn't the pi bin the ${NAME} package names`]);
|
||||
// A bin given as a plain string names it too.
|
||||
const dir = join(scratch(t), "node_modules");
|
||||
const cli = standIn(dir, piStandIn({ version: "1.2.3" }), "1.2.3");
|
||||
writeFileSync(join(dir, NAME, "package.json"), JSON.stringify({ name: NAME, version: "1.2.3", bin: "dist/bundle/cli.js" }));
|
||||
assert.equal(await real.version({ env: { MOSAIC_PI_BIN: cli } }), "1.2.3");
|
||||
});
|
||||
@@ -48,20 +48,20 @@ test('P3 Claude preflight accepts a proxy key without a host provider credential
|
||||
|
||||
test('P3 pi refuses no-session-credential with no provider key', async () => {
|
||||
const pi = await harness('pi');
|
||||
assert.equal(pi.preflight({ env: {} })?.code, 'no-session-credential');
|
||||
assert.equal((await pi.preflight({ env: {} }))?.code, 'no-session-credential');
|
||||
});
|
||||
|
||||
test('P3 pi refuses no-session-credential with only a Claude OAuth token', async () => {
|
||||
const pi = await harness('pi');
|
||||
assert.equal(pi.preflight({ env: { CLAUDE_CODE_OAUTH_TOKEN: 'fx332-oauth' } })?.code, 'no-session-credential');
|
||||
assert.equal((await pi.preflight({ env: { CLAUDE_CODE_OAUTH_TOKEN: 'fx332-oauth' } }))?.code, 'no-session-credential');
|
||||
});
|
||||
|
||||
test('P3 pi accepts its own provider key or a session proxy key', async () => {
|
||||
const pi = await harness('pi');
|
||||
for (const name of ['ANTHROPIC_API_KEY', 'OPENAI_API_KEY', 'OPENROUTER_API_KEY', 'ZAI_API_KEY']) {
|
||||
assert.equal(pi.preflight({ env: { [name]: 'fx332-own' } }), null, name);
|
||||
assert.equal(await pi.preflight({ env: { [name]: 'fx332-own' } }), null, name);
|
||||
}
|
||||
assert.equal(pi.preflight({ env: {}, proxy: { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' } }), null);
|
||||
assert.equal(await pi.preflight({ env: {}, proxy: { baseUrl: 'http://127.0.0.1:12345', key: 'fx332-agent-a' } }), null);
|
||||
});
|
||||
|
||||
// Row 332 R2 (#1830): callers that only want pi's bundle sources call bundle() with no argument,
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
//
|
||||
// read-only /usr, /etc and the host's /bin, /lib, /lib64, /sbin links;
|
||||
// the repository files the runner loads; the adapter; for pi
|
||||
// the repository's node_modules, for Claude Code its pinned
|
||||
// the found package's directory (#1873), for Claude Code its
|
||||
// binary; node when it lives outside /usr; the broker socket,
|
||||
// the launch socket for the role that may launch, and the
|
||||
// host's git socket for a role holding git.push.working or
|
||||
@@ -41,7 +41,8 @@ import { ID_PATTERN } from "../../organization/src/vocabulary.mjs";
|
||||
|
||||
export const REPO = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..", "..");
|
||||
export const BWRAP = "/usr/bin/bwrap";
|
||||
const KILL_WITH_PARENT = ["/usr/bin/setpriv", "--pdeathsig", "KILL", "--"];
|
||||
export const SETPRIV = "/usr/bin/setpriv";
|
||||
const KILL_WITH_PARENT = [SETPRIV, "--pdeathsig", "KILL", "--"];
|
||||
|
||||
// What the runner imports, and the three files the harness starts from the
|
||||
// bundle's settings (claude-gate, mcp-server, pi-extension) with theirs.
|
||||
@@ -185,14 +186,61 @@ export function gitIdentity({ business, role }) {
|
||||
return { GIT_AUTHOR_NAME: role, GIT_AUTHOR_EMAIL: email, GIT_COMMITTER_NAME: role, GIT_COMMITTER_EMAIL: email };
|
||||
}
|
||||
|
||||
// The full command line for `command` inside the sandbox. HOME is the run's
|
||||
// home/, empty at the start; the harness makes what it needs there. The git
|
||||
// identity is the role's, for the business it runs in.
|
||||
export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWRAP }) {
|
||||
const identity = Object.entries(gitIdentity({ business, role })).flatMap(([k, v]) => ["--setenv", k, v]);
|
||||
return [bwrap, ...NAMESPACES, ...argsOf(list), "--remount-ro", "/", "--setenv", "HOME", join(runDir, "home"), "--setenv", "TMPDIR", "/tmp", ...identity, "--chdir", runDir, "--", ...KILL_WITH_PARENT, ...command];
|
||||
// The variables the sandbox sets for a session's runner, over the session env
|
||||
// the launcher hands it. HOME is the run's home/, empty at the start; the
|
||||
// harness makes what it needs there. TMPDIR is the private /tmp. The git
|
||||
// identity is the role's, for the business it runs in. #1873 S3: the pi fit
|
||||
// check gives its runs these too, so they get the names a session gets.
|
||||
export function sessionSetenv(runDir, { business, role }) {
|
||||
return { HOME: join(runDir, "home"), TMPDIR: "/tmp", ...gitIdentity({ business, role }) };
|
||||
}
|
||||
|
||||
// The full command line for `command` inside the sandbox, with sessionSetenv's variables.
|
||||
export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWRAP }) {
|
||||
const setenv = Object.entries(sessionSetenv(runDir, { business, role })).flatMap(([k, v]) => ["--setenv", k, v]);
|
||||
return [bwrap, ...NAMESPACES, ...argsOf(list), "--remount-ro", "/", ...setenv, "--chdir", runDir, "--", ...KILL_WITH_PARENT, ...command];
|
||||
}
|
||||
|
||||
// #1873 pi-unpin S3: the command line for a start-time check of a harness binary. It has the
|
||||
// system part every session gets, then `list`, with the network unshared, an empty environment
|
||||
// but `env`, and `chdir` as the working directory.
|
||||
//
|
||||
// #1873 re-check 9: a check dies with the process that runs it, unlike a session. It must not
|
||||
// outlive a bus that was killed. These steps make sure of that:
|
||||
// - setpriv gives the first process SIGKILL as its parent-death signal. A shell then checks
|
||||
// that its parent is still this process ($PPID is read after the signal is set). So either
|
||||
// the owner dies later and the signal kills the chain, or the owner is already gone and the
|
||||
// shell exits before anything starts.
|
||||
// - bwrap's --die-with-parent ties bwrap to the process before it.
|
||||
// - unshare, when this host lets it (the outer layer below), runs bwrap as pid 1 of a fresh pid
|
||||
// namespace, and --kill-child kills it when unshare dies. When pid 1 of a namespace dies, the
|
||||
// kernel kills every process in it. That includes bwrap's own child, wherever it is in its
|
||||
// setup. bwrap's --die-with-parent can't reach that child: bwrap sets the child's parent-death
|
||||
// signal only just before its exec, after the child waits for bwrap to write its uid map. If
|
||||
// bwrap dies before that write, the child waits for good.
|
||||
// The one step left open with the outer layer is unshare's own: if unshare dies after it forks
|
||||
// and before its child sets the signal, bwrap still runs. bwrap is alive then, so the run sets up
|
||||
// and goes on as normal, and it ends when the binary exits. It does not wait for good.
|
||||
//
|
||||
// #1873 re-check 10: the outer layer is best effort. unshare may be missing, older than
|
||||
// util-linux 2.36 (the first with --map-current-user), or refused: on Ubuntu 24.04 with
|
||||
// kernel.apparmor_restrict_unprivileged_userns=1 only bwrap has a profile that lets it make a
|
||||
// user namespace, so an unconfined unshare can't write its uid map. The fit check probes it once
|
||||
// per check (pi-fit.mjs) and passes unshare: null when it can't run, which drops the outer layer
|
||||
// and nothing else. That is not a weaker sandbox: the outer layer only bounds how long a check's
|
||||
// processes live. The sandbox a fit run gets is bwrap's, and bwrap's command line is the same
|
||||
// either way (pi-fit.test.mjs compares the two). Without the outer layer a stuck bwrap child can
|
||||
// outlive a killed owner; the next check stops it (sweepScratch in pi-fit.mjs).
|
||||
export const UNSHARE = "/usr/bin/unshare";
|
||||
const owned = () => [...KILL_WITH_PARENT, "/bin/sh", "-c", '[ "$PPID" = "$0" ] && exec "$@"', String(process.pid)];
|
||||
const bound = (unshare) => unshare ? [unshare, "--user", "--map-current-user", "--pid", "--fork", "--kill-child", "--"] : [];
|
||||
export function checkArgv(list, command, { env = {}, chdir, node = process.execPath, bwrap = BWRAP, unshare = UNSHARE }) {
|
||||
const setenv = Object.entries(env).flatMap(([k, v]) => ["--setenv", k, v]);
|
||||
return [...owned(), ...bound(unshare), bwrap, "--die-with-parent", ...NAMESPACES, "--unshare-net", ...argsOf([...systemBinds(node), ...list]), "--remount-ro", "/", "--clearenv", ...setenv, "--chdir", chdir, "--", ...KILL_WITH_PARENT, ...command];
|
||||
}
|
||||
// The outer layer alone, for its probe: the steps before bwrap in checkArgv, then command.
|
||||
export const outerArgv = (command, { unshare = UNSHARE } = {}) => [...owned(), ...bound(unshare), ...command];
|
||||
|
||||
// #1873 triage-fences: what a model route may hand a hosted session, checked before the host
|
||||
// writes anything and again in hostedBinds. The adapter and each mount's source must be a clean
|
||||
// absolute path whose real path lies under one of the harness module's install roots, named under
|
||||
@@ -313,7 +361,7 @@ export function checkHostedRoute({ adapter, harnessMounts, installRoots, protect
|
||||
// just made there, never a link the session left: bwrap resolves a bind's source on the host.
|
||||
// The adapter and the mounts pass checkHostedRoute against `installRoots`, `protectedPaths` and
|
||||
// `codeRoot` with its `codeHolder` here too; without install roots nothing binds.
|
||||
export function hostedBinds({ runDir, healthDir, adapter, harnessMounts = [], installRoots, protectedPaths, codeRoot = null, codeHolder = null, relaySocket, node = process.execPath, readOnly = [] }) {
|
||||
export function hostedBinds({ runDir, healthDir, adapter, harnessMounts = [], installRoots, protectedPaths, codeRoot = null, codeHolder = null, relaySocket, node = process.execPath, readOnly = [], harnessFiles = [] }) {
|
||||
if (relaySocket !== join(runDir, "relay.sock")) throw new Error("hosted binds: the relay socket must be the run directory's relay.sock");
|
||||
const healthDest = join(runDir, "work", "health");
|
||||
for (const p of [runDir, healthDir, join(runDir, "bundle"), join(runDir, "tmp"), join(runDir, "work"), healthDest]) {
|
||||
@@ -326,6 +374,20 @@ export function hostedBinds({ runDir, healthDir, adapter, harnessMounts = [], in
|
||||
try { st = lstatSync(p); } catch { throw new Error(`hosted binds: ${relative(runDir, p)} is missing`); }
|
||||
if (!ok(st)) throw new Error(`hosted binds: ${relative(runDir, p)} is not what the host made`);
|
||||
}
|
||||
// #1873 harness-matrix: a harness's own config file the host wrote under the session's HOME (pi's
|
||||
// models.json), bound read-only over the writable run directory so the session cannot change
|
||||
// the proxy it reaches. Each is a regular file, not a link, inside runDir/home.
|
||||
const homeDir = join(runDir, "home");
|
||||
if (!Array.isArray(harnessFiles) || harnessFiles.length > HOSTED_MOUNTS_MAX) refuseRoute("harness-files", "too many harness files");
|
||||
for (const f of harnessFiles) {
|
||||
if (!cleanPath(f) || !f.startsWith(homeDir + "/")) refuseRoute("harness-files", "a harness file is outside the session's HOME");
|
||||
let st;
|
||||
try { st = lstatSync(f); } catch { refuseRoute("harness-files", "a harness file is missing"); }
|
||||
if (!st.isFile() || st.nlink !== 1) refuseRoute("harness-files", "a harness file is not a regular file");
|
||||
for (let parent = dirname(f); parent.length > runDir.length; parent = dirname(parent)) {
|
||||
if (lstatSync(parent).isSymbolicLink()) refuseRoute("harness-files", "a harness file's directory is a link");
|
||||
}
|
||||
}
|
||||
const route = checkHostedRoute({ adapter, harnessMounts, installRoots, protectedPaths, codeRoot, codeHolder, runDir, node });
|
||||
const out = systemBinds(node);
|
||||
for (const f of RUNNER_FILES) out.push({ kind: "ro", src: f, why: "a repository file the runner loads" });
|
||||
@@ -337,6 +399,7 @@ export function hostedBinds({ runDir, healthDir, adapter, harnessMounts = [], in
|
||||
{ kind: "ro", src: healthDir, dest: healthDest, why: "the health snapshots the Console writes, read-only, inside the workspace the read tools reach" },
|
||||
{ kind: "ro", src: join(runDir, "bundle"), why: "the run's bundle stays as the host wrote it" },
|
||||
{ kind: "ro", src: join(runDir, "session.json"), why: "the session file stays as the host wrote it" },
|
||||
...harnessFiles.map((f) => ({ kind: "ro", src: f, why: "the harness's own config file, as the host wrote it" })),
|
||||
{ kind: "ro", src: relaySocket, why: "the host's relay, the only broker the runner reaches" },
|
||||
{ kind: "rw", src: join(runDir, "tmp"), dest: "/tmp", why: "a private /tmp, on disk in the run directory" },
|
||||
);
|
||||
|
||||
@@ -254,7 +254,7 @@ export const ENV_ALLOW = Object.freeze([
|
||||
"HOME", "USER", "LOGNAME", "LANG", "LC_ALL", "TZ", "TERM",
|
||||
"ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_BASE_URL", "CLAUDE_CODE_OAUTH_TOKEN",
|
||||
"OPENAI_API_KEY", "OPENROUTER_API_KEY", "ZAI_API_KEY",
|
||||
"MOSAIC_CLAUDE_BIN",
|
||||
"MOSAIC_CLAUDE_BIN", "MOSAIC_PI_BIN",
|
||||
]);
|
||||
|
||||
// The ways a Claude Code session can log in, each a set of variables that
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createServer } from "node:net";
|
||||
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { linkSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { HOSTED_MOUNTS_MAX, RUNNER_FILES, checkHostedRoute, hostedArgv, hostedBinds } from "../src/sandbox.mjs";
|
||||
|
||||
@@ -243,3 +243,34 @@ test("each mount field is read once, and no mount's dest lies in or around anoth
|
||||
}
|
||||
assert.equal(hostedBinds({ ...base, harnessMounts: [{ src: join(p.install, "m"), dest: "/opt/x", why: "w" }, { src: join(p.install, "n"), dest: "/opt/xy", why: "w" }] }).filter((b) => b.dest?.startsWith("/opt/")).length, 2);
|
||||
});
|
||||
|
||||
// #1873 harness-matrix: a harness's own config file under the session's HOME (pi's models.json) is
|
||||
// bound read-only after the writable run directory, so the session cannot change the proxy it
|
||||
// reaches. A file outside HOME, a link, a hard link, a missing file or a linked directory is refused.
|
||||
test("hosted binds hold a harness file read-only over the run directory, and refuse one outside HOME or not a plain file", async (t) => {
|
||||
const p = await layout(t);
|
||||
const base = { runDir: p.runDir, healthDir: p.healthDir, adapter: p.adapter, relaySocket: p.relaySocket, installRoots: p.installRoots, protectedPaths: p.protectedPaths };
|
||||
const agentDir = join(p.runDir, "home", ".pi", "agent");
|
||||
mkdirSync(agentDir, { recursive: true });
|
||||
const file = join(agentDir, "models.json");
|
||||
writeFileSync(file, "{}");
|
||||
const list = hostedBinds({ ...base, harnessFiles: [file] });
|
||||
const rw = list.findIndex((b) => b.kind === "rw" && b.src === p.runDir);
|
||||
const ro = list.findIndex((b) => b.kind === "ro" && b.src === file && b.dest === undefined);
|
||||
assert.ok(rw >= 0 && ro > rw, "read-only, after the writable run directory");
|
||||
const refused = (files, label) => assert.throws(() => hostedBinds({ ...base, harnessFiles: files }), (e) => e.code === "hosted-binds-refused" && e.fence === "harness-files", label);
|
||||
writeFileSync(join(p.runDir, "work", "x.json"), "{}");
|
||||
refused([join(p.runDir, "work", "x.json")], "outside HOME");
|
||||
refused([join(p.runDir, "home", "..", "work", "x.json")], "a .. path");
|
||||
refused([join(p.runDir, "home", "absent.json")], "missing");
|
||||
refused([join(p.runDir, "home")], "a directory");
|
||||
symlinkSync(join(p.runDir, "work", "x.json"), join(p.runDir, "home", "link.json"));
|
||||
refused([join(p.runDir, "home", "link.json")], "a link");
|
||||
linkSync(join(p.runDir, "work", "x.json"), join(p.runDir, "home", "hard.json"));
|
||||
refused([join(p.runDir, "home", "hard.json")], "a hard link");
|
||||
mkdirSync(join(p.runDir, "work", "d"));
|
||||
writeFileSync(join(p.runDir, "work", "d", "m.json"), "{}");
|
||||
symlinkSync(join(p.runDir, "work", "d"), join(p.runDir, "home", "dlink"));
|
||||
refused([join(p.runDir, "home", "dlink", "m.json")], "a linked directory");
|
||||
refused(Array.from({ length: 9 }, () => file), "too many");
|
||||
});
|
||||
@@ -129,7 +129,7 @@ test("row 65 a harness module's mounts bind read-only after the adapter, each wi
|
||||
const list = binds({ ...args, harnessMounts: pi.mounts({ env: {} }) });
|
||||
const at = (src) => list.findIndex((b) => b.src === src);
|
||||
const modules = list[at(realpathSync(join(REPO, "node_modules")))];
|
||||
assert.deepEqual(modules, { kind: "ro", src: realpathSync(join(REPO, "node_modules")), dest: join(REPO, "node_modules"), why: "pi and its packages, on the session's PATH" });
|
||||
assert.deepEqual(modules, { kind: "ro", src: realpathSync(join(REPO, "node_modules")), dest: join(REPO, "node_modules"), why: "pi and its packages (the repository's pi)" });
|
||||
assert.ok(at(join(root, "a.sh")) < at(modules.src) && at(modules.src) < at(join(root, "bus.sock")));
|
||||
const fixture = binds({ ...args, harnessMounts: [{ src: join(root, "tool"), why: "a fixture harness's tool" }] });
|
||||
assert.deepEqual(fixture.find((b) => b.src === join(root, "tool")), { kind: "ro", src: join(root, "tool"), why: "a fixture harness's tool" });
|
||||
|
||||
@@ -58,6 +58,15 @@ test("sessionEnv passes only the allowlist, the repo's bin on PATH, and the run
|
||||
assert.equal(sessionEnv({ MOSAIC_CLAUDE_BIN: "/opt/claude" }, "r0123456789ab").MOSAIC_CLAUDE_BIN, "/opt/claude");
|
||||
});
|
||||
|
||||
// #1873 pi-unpin S2: the host's MOSAIC_PI_BIN reaches the session, as MOSAIC_CLAUDE_BIN does, and
|
||||
// it is the only name the slice adds: every other MOSAIC_* and PI_* name from the host stays out.
|
||||
test("pi-unpin S2: sessionEnv passes MOSAIC_PI_BIN and nothing else new", () => {
|
||||
const env = sessionEnv({ MOSAIC_PI_BIN: "/opt/pi/bin/pi", PI_CODING_AGENT_DIR: "/h/.pi", PI_OFFLINE: "0", MOSAIC_PI_ROOT: "/x", PATH: "/usr/bin" }, "r0123456789ab");
|
||||
assert.deepEqual(Object.keys(env).sort(), ["MOSAIC_PI_BIN", "MOSAIC_RUN_ID", "PATH"]);
|
||||
assert.equal(env.MOSAIC_PI_BIN, "/opt/pi/bin/pi");
|
||||
assert.deepEqual(ENV_ALLOW.filter((k) => k.startsWith("MOSAIC_")), ["MOSAIC_CLAUDE_BIN", "MOSAIC_PI_BIN"]);
|
||||
});
|
||||
|
||||
test("ZAI_API_KEY passes the session allowlist, and ZAI_TOKEN does not", () => {
|
||||
assert.ok(ENV_ALLOW.includes("ZAI_API_KEY"));
|
||||
const env = sessionEnv({ ZAI_API_KEY: "fixture-zai", ZAI_TOKEN: "t" }, "r0123456789ab");
|
||||
|
||||
@@ -1376,7 +1376,7 @@ The existing user notice names the governance records visible to the owner.
|
||||
|
||||
## Model proxy page (P4, #1828)
|
||||
|
||||
The owner's Settings → Model proxy page (`/settings/model-proxy`) signs in Claude/Codex accounts, accepts write-only
|
||||
The owner's Settings → Model setup page (named Model proxy before #1873) (`/settings/model-proxy`) signs in Claude/Codex accounts, accepts write-only
|
||||
z.ai API keys, sets account priority, and lists client-key IDs. It uses only
|
||||
`/api/proxy/` routes over the owner's human bus transport. The old `/proxy` bookmark redirects to this page after the same access checks. A missing instance
|
||||
proxy refuses `not-configured`; it does not ask for a management key. That key
|
||||
@@ -1394,6 +1394,16 @@ retires the state. Refreshing the page does not restore a pending login.
|
||||
Usage is reserved on this page for P5; P4 adds no usage source. PIN changes
|
||||
require the offline upgrade acceptance documented in packages/model-proxy.
|
||||
|
||||
Settings slice 1 (#1873). Once the service runs, the page lists each organization that can't see
|
||||
the models, or is not running. For each one that can't see them it reads that organization's own
|
||||
routes (`GET /api/orgs/<id>/bus/routes`, the read Routing uses) and shows the bus's cause as a
|
||||
plain line, with the bus's words under Technical details. A Restart shows only when the bus names
|
||||
no cause or a cause a fresh start clears. With the dev fixture a restart is refused and stops the
|
||||
organization, and no endpoint removes the fixture provider yet, so that row says it can't be changed
|
||||
from Console. Two models that get the same short name (`model-key-collision`) get no Restart either.
|
||||
Only an organization a restart can help holds the next step. A refused restart keeps its reason on its row. Only the next step has a
|
||||
button; done steps, Fence, Init lock and the service controls sit under Technical details.
|
||||
|
||||
Archived project routes use the inclusive project read for the name, saved threads and room history. They offer Unarchive in send mode; thread creation, resume, sending and clear controls are unavailable while archived. Archive styling uses only `.project-archive-control` and `.project-archived-filter`.
|
||||
|
||||
### Recovering local process state
|
||||
|
||||
@@ -45,6 +45,8 @@ import { composeSnapshot, readSystemConfig, readOrgConfig } from './triage-snaps
|
||||
import { busesStopped } from '../../cli/src/proxy-admin-host.mjs';
|
||||
import { agentLevelsWriteRoutes } from './agent-levels-write.mjs';
|
||||
import { triageRouteRoutes } from './triage-route-http.mjs';
|
||||
import { keyState, ridingOrgs, triageKeyCall } from './triage-access.mjs';
|
||||
import { readKey as readTriageKey } from '../../bus/src/triage-key-file.mjs';
|
||||
import { RESTART_EXIT } from '../../cli/src/org-console.mjs';
|
||||
import { ID_PATTERN, systemOnly } from '../../organization/src/vocabulary.mjs';
|
||||
import { selectorScope } from '../../organization/src/selector.mjs';
|
||||
@@ -226,16 +228,18 @@ try {
|
||||
// (read only) and, through `call`, from its bus as the owner. A file that cannot be read gives
|
||||
// undefined fields, which Ride all names as unreadable. The change log is read with the writer's
|
||||
// own check (no symlink, a regular file this user owns, not group or world writable).
|
||||
const rideRiders = async () => {
|
||||
const rideRiders = async ({ timeoutMs = 5000, deadline } = {}) => {
|
||||
const writer = organizationWriter({ dir: configDir(), rolesDir: rolesDir() });
|
||||
const local = id => readRider({ id, read: () => writer.read(id).document, readLog: () => ownedText(writer.logFile(id), { mask: 0o022 }), isFixture: orgUsesFixture });
|
||||
// settings-backend A2: `file()` is the organization file as the writer reads it ({document, revision}),
|
||||
// read again on each call, for turning a program off for every organization.
|
||||
const local = id => ({ ...readRider({ id, read: () => writer.read(id).document, readLog: () => ownedText(writer.logFile(id), { mask: 0o022 }), isFixture: orgUsesFixture }), file: () => writer.read(id) });
|
||||
const down = async () => { throw Object.assign(new Error('the bus is not reachable'), { code: 'no-bus-host' }); };
|
||||
// The single-business Console has no bus status here: its organization never reads as stopped.
|
||||
if (!organizations) return ownerCall && options.business ? [{ ...local(options.business), call: ownerCall }] : [];
|
||||
const out = [];
|
||||
for (const org of await listOrgs({ configDir: configDir(), dataRoot: system.dataRoot, instance })) {
|
||||
if (org.state !== 'active') continue;
|
||||
let call = down; try { const paths = orgPaths(org.root); call = busCaller({ dataRoot: org.root, socket: paths.socket, business: org.id, timeoutMs: 5000 }); } catch {}
|
||||
let call = down; try { const paths = orgPaths(org.root); call = busCaller({ dataRoot: org.root, socket: paths.socket, business: org.id, timeoutMs, ...(deadline ? { deadline } : {}) }); } catch {}
|
||||
// The organization admin page's stopped test reads this bus status (its process id).
|
||||
out.push({ ...local(org.id), call, status: () => orgBusStatus({ configDir: configDir(), dataRoot: system.dataRoot, instance }, org.id) });
|
||||
}
|
||||
@@ -262,6 +266,8 @@ try {
|
||||
return out;
|
||||
},
|
||||
riders: rideRiders,
|
||||
// The root agents each organization takes count when a program is turned off (review P1-1).
|
||||
rootAgents: () => loadRootAgents(configDir(), { rolesDir: rolesDir() }),
|
||||
ownerModels,
|
||||
// A stopped fixture organization moves through the organization service's whole-file update, the
|
||||
// write `POST /api/orgs/ID/admin` makes, as the signed-in owner. The owner starts its bus after.
|
||||
@@ -292,6 +298,8 @@ try {
|
||||
const triage = triageRoute ? createTriageHost({
|
||||
dataRoot: system.dataRoot, route: triageRoute, proxyPort: triageProxyPort, tokenPattern: triageTokenPattern(configDir()),
|
||||
devOwner: Boolean(devOwner), log: line => console.log(line),
|
||||
// proxy-scope slice 4: a session on a key past its retireAt stops with triage-key-rotated.
|
||||
keyRecord: () => readTriageKey(configDir()),
|
||||
readSnapshot: () => composeSnapshot({
|
||||
orgs: () => listOrgs({ configDir: configDir(), dataRoot: system.dataRoot, instance }),
|
||||
busStatus: id => orgBusStatus({ configDir: configDir(), dataRoot: system.dataRoot, instance }, id),
|
||||
@@ -325,6 +333,22 @@ try {
|
||||
// next admit.
|
||||
const triageRoutes = system && (devOwner || ssoOn) ? [triageRouteRoutes({
|
||||
dataRoot: system.dataRoot, log: line => console.log(line),
|
||||
// proxy-scope slice 4: the Triage key through one riding organization's bus (proxy.triage.key),
|
||||
// as Proxy setup's init asks one bus. The Console reads the key file's state only; it never
|
||||
// loads the proxy management client or the key verbs' module.
|
||||
access: {
|
||||
state: () => keyState(configDir()),
|
||||
// The same list the key walk asks (cliproxy, not a fixture), read from local files only.
|
||||
// The owner's proxy must be set up (proxy.json names a port), as ownerModels reads it.
|
||||
riding: async () => {
|
||||
let proxy = false;
|
||||
try { proxy = Number.isInteger(loadProxy(configDir(), system.dataRoot)?.port); } catch {}
|
||||
return proxy && ridingOrgs(await rideRiders()).length > 0;
|
||||
},
|
||||
// One 30 s deadline over the whole walk, not 30 s for each bus.
|
||||
key: op => { const deadline = Date.now() + 30000; return triageKeyCall({ riders: () => rideRiders({ timeoutMs: 30000, deadline }), op }); },
|
||||
stop: () => (triage ? triage.stop() : Promise.resolve()),
|
||||
},
|
||||
catalogues: async () => {
|
||||
const read = async call => { try { return (await call('routes'))?.catalogue ?? { status: 'unknown' }; } catch { return { status: 'bus-unavailable' }; } };
|
||||
if (!organizations) return ownerCall ? [await read(ownerCall)] : [];
|
||||
|
||||
@@ -60,11 +60,11 @@ const NONE = ' Nothing was applied.';
|
||||
const EITHER = ' Part of it may already be applied: check the Agents page before trying again.';
|
||||
const CHECK = ' Check the Agents page and the inbox before trying again.';
|
||||
const HIRE_START_REFUSALS = {
|
||||
'model-provider-unavailable': [409, 'The model proxy does not offer this model, or its catalogue is not ready. Pick a model it offers, or ask the owner to check Settings > Model proxy.' + NONE],
|
||||
'model-catalogue-unavailable': [503, 'The model catalogue could not be read, so this model could not be checked. Check Settings > Model proxy, then try again.' + NONE],
|
||||
'model-provider-unavailable': [409, 'The model service does not offer this model, or its catalogue is not ready. Pick a model it offers, or ask the owner to check Settings > Model setup.' + NONE],
|
||||
'model-catalogue-unavailable': [503, 'The model catalogue could not be read, so this model could not be checked. Check Settings > Model setup, then try again.' + NONE],
|
||||
'model-harness-incompatible': [409, 'This model does not run on the chosen harness. Pick another model or harness.' + NONE],
|
||||
'thinking-unsupported': [409, 'The chosen harness does not apply this thinking level. Clear Thinking or pick a level it applies.' + NONE],
|
||||
'unknown-model': [409, 'This model is not one of the organization\'s models. Pick a model listed under Settings > Model proxy.' + NONE],
|
||||
'unknown-model': [409, 'This model is not one of the organization\'s models. Pick a model listed under Settings > Routing.' + NONE],
|
||||
'harness-off': [409, 'This harness is not switched on for the organization. Pick a harness listed under Settings > Routing, or ask the owner to switch it on.' + NONE],
|
||||
'harness-refused': [409, 'The harness refused this route. Clear Thinking, or pick another model or harness under Settings > Routing.' + NONE],
|
||||
'template-system-only': [409, 'This role template is for the system, not for an organization. Pick another template.' + NONE],
|
||||
@@ -100,7 +100,7 @@ const HIRE_START_REFUSALS = {
|
||||
'credential-scope-exceeded': [409, 'The requested credential scope is wider than the role allows.' + CHECK],
|
||||
// The fields passed the shape check below, so a malformed answer here is a value the organization
|
||||
// checks refused: most often a model that is not a launch.models key.
|
||||
'invalid-request': [400, 'The bus refused a value in this proposal. Pick a model listed under Settings > Model proxy and a role the organization checks accept.' + EITHER],
|
||||
'invalid-request': [400, 'The bus refused a value in this proposal. Pick a model listed under Settings > Routing and a role the organization checks accept.' + EITHER],
|
||||
};
|
||||
// A reply the endpoint cannot read as a definite answer: no answer, or the bus server's request
|
||||
// codes. Anything else that reads as a code is a definite refusal.
|
||||
|
||||
@@ -10,10 +10,15 @@ export { createProxyAdmin };
|
||||
|
||||
// #1873: one organization's catalogue row for the setup page. A count is shown only when the bus
|
||||
// answered with a model list; otherwise models is null and the page shows a dash, never a made-up 0.
|
||||
const RESTART_TO_LAUNCH = 'Restart this organization to start agents on these models.';
|
||||
export async function orgCatalogue(call) {
|
||||
try {
|
||||
const value = await call('routes');
|
||||
return { status: value?.catalogue?.status ?? 'unknown', models: Array.isArray(value?.catalogue?.models) ? value.catalogue.models.length : null };
|
||||
const status = value?.catalogue?.status ?? 'unknown';
|
||||
// settings-p3 (re-check 3 P3-5): a bus that picked the service up after it started reads ready but
|
||||
// starts agents on these models only after a restart (bus runtime.mjs RESTART_TO_LAUNCH).
|
||||
const launchRestart = status === 'ready' && Array.isArray(value.catalogue.notices) && value.catalogue.notices.includes(RESTART_TO_LAUNCH);
|
||||
return { status, models: Array.isArray(value?.catalogue?.models) ? value.catalogue.models.length : null, ...(launchRestart ? { launchRestart: true } : {}) };
|
||||
} catch { return { status: 'bus-unavailable', models: null }; }
|
||||
}
|
||||
|
||||
@@ -31,7 +36,28 @@ const ROUTES = Object.freeze({
|
||||
// #1873 ride-default: Ride all, and the confirmed move of one stopped fixture organization.
|
||||
'/api/proxy/admin/ride-all': ['POST', []],
|
||||
'/api/proxy/admin/ride-move': ['POST', ['org']],
|
||||
// #1873 settings-backend A1: reads for Model setup. The list names each read's query keys.
|
||||
'/api/proxy/admin/log': ['GET', ['lines']],
|
||||
'/api/proxy/admin/service-key': ['GET', []],
|
||||
'/api/proxy/admin/history': ['GET', ['limit', 'before']],
|
||||
// #1873 settings-backend A2: one harness program off for every organization, and each
|
||||
// organization's proxy client key health. The program list itself is in status.
|
||||
'/api/proxy/admin/programs/off': ['POST', ['name']],
|
||||
'/api/proxy/admin/access': ['GET', []],
|
||||
});
|
||||
// The A1 reads take a query; every other route ignores one, as before.
|
||||
const QUERY_ROUTES = new Set(['/api/proxy/admin/log', '/api/proxy/admin/service-key', '/api/proxy/admin/history']);
|
||||
// Each query key at most once, only the route's own keys, each a plain decimal number.
|
||||
function readQuery(req, allowed) {
|
||||
let params;
|
||||
try { params = new URL(req.url, 'http://localhost').searchParams; } catch { return null; }
|
||||
const out = {};
|
||||
for (const [key, value] of params) {
|
||||
if (!allowed.includes(key) || Object.hasOwn(out, key) || !/^[0-9]{1,15}$/.test(value)) return null;
|
||||
out[key] = Number(value);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
export const PROXY_ADMIN_PATHS = Object.freeze(Object.keys(ROUTES));
|
||||
const INIT_KEYS = ['generate', 'key', 'confirm', 'port', 'rotateGraceMinutes', 'firstOutputSeconds'];
|
||||
const json = (res, status, value) => {
|
||||
@@ -66,6 +92,8 @@ export async function proxyAdminHTTP(req, res, { path, admin, call, named, teste
|
||||
if (req.headersDistinct.origin?.length !== 1 || req.headers.origin !== origin) return done(403, { error: 'cross-origin request refused' });
|
||||
if (req.headersDistinct['sec-fetch-site']?.length !== 1 || req.headers['sec-fetch-site'] !== 'same-origin') return done(403, { error: 'cross-site request refused' });
|
||||
}
|
||||
let query = {};
|
||||
if (QUERY_ROUTES.has(path)) { query = readQuery(req, route[1]); if (!query) return refuse('invalid-request'); }
|
||||
if (!admin) return refuse('not-configured');
|
||||
let args = {};
|
||||
if (req.method === 'POST') {
|
||||
@@ -113,6 +141,13 @@ export async function proxyAdminHTTP(req, res, { path, admin, call, named, teste
|
||||
case '/api/proxy/admin/ride-move':
|
||||
if (typeof args.org !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(args.org)) return refuse('invalid-request');
|
||||
return done(200, await admin.rideMove({ org: args.org, principal }));
|
||||
case '/api/proxy/admin/log': return done(200, await admin.log(Object.hasOwn(query, 'lines') ? { lines: query.lines } : {}));
|
||||
case '/api/proxy/admin/service-key': return done(200, await admin.serviceKey());
|
||||
case '/api/proxy/admin/history': return done(200, await admin.history({ ...(Object.hasOwn(query, 'limit') ? { limit: query.limit } : {}), before: query.before ?? null }));
|
||||
case '/api/proxy/admin/programs/off':
|
||||
if (typeof args.name !== 'string' || !/^[a-z][a-z0-9-]{0,31}$/.test(args.name)) return refuse('invalid-request');
|
||||
return done(200, await admin.programsOff({ name: args.name, user: typeof user === 'string' ? user : null }));
|
||||
case '/api/proxy/admin/access': return done(200, await admin.access());
|
||||
}
|
||||
} catch (error) {
|
||||
const code = ['human-required', 'not-configured', 'invalid-request'].includes(error?.code) ? error.code : proxyCode(error, 'outcome-unknown');
|
||||
|
||||
@@ -18,7 +18,10 @@ const codes = ['not-configured', 'human-required', 'provider-not-allowed', 'logi
|
||||
const json = (res, status, value) => {
|
||||
res.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' }); res.end(JSON.stringify(value));
|
||||
};
|
||||
export async function proxyHTTP(req, res, { path, call, named, tester, granted, send, origin, user, log }) {
|
||||
// `record` is the proxy setup controller's recordAccount (settings-backend A1), or null where this
|
||||
// Console has no proxy setup: after a write's bus call answers, an account change is added to the
|
||||
// setup history. A record that fails leaves the bus answer as it is and adds recorded: false.
|
||||
export async function proxyHTTP(req, res, { path, call, named, tester, granted, send, origin, user, log, record = null }) {
|
||||
const route = routes[path];
|
||||
const done = (status, value) => {
|
||||
// No request body, query, login state, authorization URL or exception text.
|
||||
@@ -48,11 +51,19 @@ export async function proxyHTTP(req, res, { path, call, named, tester, granted,
|
||||
} catch { return done(400, { error: 'invalid-request' }); }
|
||||
}
|
||||
if (path === '/api/proxy/plugins/set' && (!args.plugins || typeof args.plugins !== 'object' || Array.isArray(args.plugins) || Object.keys(args.plugins).length !== 1 || !['harness', 'model-provider'].includes(Object.keys(args.plugins)[0]) || !Array.isArray(Object.values(args.plugins)[0]) || !Object.values(args.plugins)[0].every(name => typeof name === 'string'))) return done(400, { error: 'invalid-request' });
|
||||
try { return done(200, await call(route[1], args)); }
|
||||
let value;
|
||||
try { value = await call(route[1], args); }
|
||||
catch (error) {
|
||||
const code = codes.includes(error.code) || (path === '/api/proxy/plugins/set' && typeof error.code === 'string' && /^[a-z-]{1,64}$/.test(error.code)) ? error.code : 'outcome-unknown';
|
||||
// proxy-pickup (#1873): a bus with no proxy client names the cause in one plain line.
|
||||
const reason = code === 'not-configured' && typeof error.reason === 'string' ? { reason: error.reason.slice(0, 300) } : {};
|
||||
return done(code === 'human-required' ? 403 : ['not-configured', 'proxy-down', 'outcome-unknown'].includes(code) ? 503 : 400, { error: code, ...reason });
|
||||
}
|
||||
if (req.method === 'POST' && typeof record === 'function') {
|
||||
let recorded;
|
||||
try { recorded = record({ verb: route[1], args, result: value, user: user ?? null }); } catch { recorded = false; }
|
||||
// All four recorded verbs answer a plain object; any other answer is passed on unmarked.
|
||||
if (recorded === false && value && typeof value === 'object' && !Array.isArray(value)) value = { ...value, recorded: false };
|
||||
}
|
||||
return done(200, value);
|
||||
}
|
||||
@@ -183,7 +183,9 @@
|
||||
function breadcrumbs() {
|
||||
draw();
|
||||
// SP1: every page inside an organization says which one, after the dashboard: Dashboard / Acme / ...
|
||||
const box = $('breadcrumbs'), org = /^#\/orgs\/[^/?]+/.test(location.hash) ? window.MosaicOrg : null;
|
||||
// #1873 settings slice 4: an organization's Settings page names it with ?org=, so it says which one too.
|
||||
const page = /^#\/settings\/([^/?]+)/.exec(location.hash)?.[1], orgSettings = !!page && !!window.MosaicOrg?.orgPage?.(page) && /[?&]org=/.test(location.hash);
|
||||
const box = $('breadcrumbs'), org = /^#\/orgs\/[^/?]+/.test(location.hash) || orgSettings ? window.MosaicOrg : null;
|
||||
if (box && org?.id && !box.querySelector('[data-crumb-org]') && !/^#\/orgs\/[^/?]+\/organization(?:[/?]|$)/.test(location.hash)) box.insertAdjacentHTML('afterbegin', `<a href="#/dashboard">Dashboard</a><span aria-hidden="true">/</span><a href="#/orgs/${encodeURIComponent(org.id)}/organization" data-crumb-org>${escape(org.label)}</a><span aria-hidden="true">/</span>`);
|
||||
// SP7: a trail with nothing above the page only repeats the page's heading, so the heading names it once.
|
||||
if (!box) return;
|
||||
|
||||
@@ -703,7 +703,14 @@
|
||||
<h2 id="b-for-you">Open decisions <span class="count">${rows.length}</span></h2>${body}
|
||||
<p class="small muted">Open a decision for its seen, DM and closing history.</p>`;
|
||||
}
|
||||
const messageText = m => m.from_role !== 'human' && typeof m.body === 'string' && m.body.startsWith('The turn failed: ') ? `The assistant couldn't reply. Try again, or check Agents.<span class="source">Details: ${txt(m.body.slice('The turn failed: '.length))}</span>` : txt(m.body);
|
||||
// #1873 settings slice 5: a failed turn whose body carries its own "\nDetails: " part (chat.js
|
||||
// messageBody) shows its cause and that part once.
|
||||
const messageText = m => {
|
||||
if (m.from_role === 'human' || typeof m.body !== 'string' || !m.body.startsWith('The turn failed: ')) return txt(m.body);
|
||||
const rest = m.body.slice('The turn failed: '.length), at = rest.indexOf('\nDetails: ');
|
||||
if (at > 0) return `The assistant couldn't reply: ${txt(rest.slice(0, at))}<span class="source">Details: ${txt(rest.slice(at + '\nDetails: '.length))}</span>`;
|
||||
return `The assistant couldn't reply. Try again, or check Agents.<span class="source">Details: ${txt(rest)}</span>`;
|
||||
};
|
||||
async function messagesView(get, q) {
|
||||
const after = q.get('after');
|
||||
if (after !== null && !SAFE.test(after)) return notFound('The message cursor is not a message id.');
|
||||
@@ -1299,6 +1306,9 @@
|
||||
}
|
||||
const routesApplyMarkup=()=>routesApply?.length?`<ul class="routes-apply" aria-label="How the change reaches each agent">${routesApply.map(a=>`<li><b>${txt(a.name)}</b>: ${txt(a.text)}${a.restart&&canSend&&!isTester?` ${window.MosaicAgents?.restartMarkup(a.name,a.thread)??''}`:''}</li>`).join('')}</ul>`:'';
|
||||
let harnessSettings=null, harnessDraft=null, harnessLoading=false, harnessReceipt='', harnessBusy=false;
|
||||
// #1873 settings slice 5: the organization's one choice, Use your models (the cliproxy module in
|
||||
// its model-provider list, from the plugins read above) or its own model service (Coming later).
|
||||
let modelsReceipt='', modelsReceiptKind='', modelsBusy=false, modelsFocus=false;
|
||||
function readHarnesses(){
|
||||
if(harnessLoading || harnessSettings !== null || !canSend || isTester)return; harnessLoading=true;const epoch=orgEpoch;
|
||||
fetch('/api/proxy/plugins',{cache:'no-store',redirect:'manual'}).then(async r=>{const v=await r.json();if(epoch!==orgEpoch)return;if(!r.ok || !/^[a-f0-9]{64}$/.test(v.revision??'') || !Array.isArray(v.available?.harness) || !v.available.harness.every(n=>typeof n==='string') || !Array.isArray(v.plugins?.harness??[]))throw Error();harnessSettings=v;}).catch(()=>{if(epoch!==orgEpoch)return;harnessSettings=false;harnessReceipt='Harnesses could not be read.';}).finally(()=>{if(epoch!==orgEpoch)return;harnessLoading=false;if(routeOf().split('?')[0]==='/settings/routing')render(false);});
|
||||
@@ -1306,6 +1316,87 @@
|
||||
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]);
|
||||
// #1873 settings slice 1 (D2): an organization that lists the model service but cannot see its
|
||||
// models. For the instance owner the page reads the model service's state once per visit, so the
|
||||
// line says which is true: the service is not set up, not running or waiting on a restart after
|
||||
// a key write (a link to its page), or it runs and this organization still cannot read it. A
|
||||
// running bus picks the service up at its next catalogue read (proxy-pickup), so a Restart is
|
||||
// offered only when the state says it helps: the bus names no cause, or names one a fresh start
|
||||
// clears (MosaicConsole.catalogueCause). With the dev fixture a restart is refused and stops the
|
||||
// organization, so that cause gets its plain line and Check again. The Restart is never a guess.
|
||||
let serviceState=null, serviceLoading=false;
|
||||
function readService(){
|
||||
if(serviceLoading||serviceState!==null||isTester||!triageOwner||!window.MosaicOrg)return; serviceLoading=true;const epoch=orgEpoch;
|
||||
fetch('/api/proxy/admin/status',{cache:'no-store',redirect:'manual'}).then(async r=>{const v=await r.json();if(epoch!==orgEpoch)return;if(!r.ok||!v||typeof v!=='object')throw Error();
|
||||
serviceState=v.init?.initialized!==true?'not-set-up':v.fence?.set===true?'fenced':v.unit?.active==='active'&&v.unit?.healthy===true?'running':'not-running';})
|
||||
.catch(()=>{if(epoch!==orgEpoch)return;serviceState=false;}).finally(()=>{if(epoch!==orgEpoch)return;serviceLoading=false;if(routeOf().split('?')[0]==='/settings/routing')render(false);});
|
||||
}
|
||||
function catalogueLine(proxyOn,catalogue){
|
||||
const label=window.MosaicOrg?.label||'This organization', org=txt(label), id=window.MosaicOrg?.id, owner=canSend&&!isTester;
|
||||
const lead=`<b>${org} can't see your models yet.</b>`, thinking='A change to thinking alone still saves.';
|
||||
// The bus's cause (proxy-pickup). A known cause reads as one plain line with the bus's words
|
||||
// under Technical details; an unknown one shows the bus's words as they are.
|
||||
const cause=window.MosaicConsole?.catalogueCause?.(label,catalogue?.notices,catalogue?.reason)??null;
|
||||
const words=!cause?'':cause.plain?`<details class="routes-cause-detail"><summary>Technical details</summary><p class="small muted">${txt(cause.detail)}</p></details>`:`<p class="small warn">${txt(cause.detail)}</p>`;
|
||||
const plain=cause?.plain?` ${txt(cause.plain)}`:'';
|
||||
const setupLink=text=>`<a href="#/settings/model-setup">${text}</a>`;
|
||||
if(!proxyOn)return `<p class="small warn" id="routes-catalogue">${lead}${plain} ${thinking}</p>${words}`;
|
||||
if(owner)readService();
|
||||
const recheck='<button type="button" id="routes-recheck">Check again</button>';
|
||||
const restart=primary=>window.MosaicConsole?.orgRestartButton?.(id,{name:label,primary})??'';
|
||||
if(owner&&serviceState==='running'&&cause){
|
||||
const fix=cause.fix==='service'?` ${setupLink('Open Model setup')}.`:'';
|
||||
const next=cause.restart?`Restart ${org} to try again. It stops for a moment and starts again.`:cause.code==='fixture-proxy-configured'?'':'Check again once that is fixed.';
|
||||
return `<div class="banner routes-catalogue" id="routes-catalogue" role="status"><p>${lead} ${cause.plain?`Your model service is running.${plain}${fix}`:`Your model service is running, but ${org} can't use it. ${org} says why:`}</p>${cause.plain?'':words}<p>${next} ${thinking}</p><p class="actions">${cause.restart?restart(true)+' ':''}${recheck}</p>${cause.plain?words:''}</div>`;
|
||||
}
|
||||
if(owner&&serviceState==='running')return `<div class="banner routes-catalogue" id="routes-catalogue" role="status"><p>${lead} Your model service is running, but ${org} can't read your models from it. Restart ${org} to connect it again. It stops for a moment and starts again. ${thinking}</p>${restart(true)}</div>`;
|
||||
if(owner&&serviceState==='not-set-up')return `<p class="small warn" id="routes-catalogue">${lead} Your model service isn't set up yet. ${setupLink('Set up the model service')}. ${thinking}</p>`;
|
||||
if(owner&&serviceState==='fenced')return `<p class="small warn" id="routes-catalogue">${lead} Your model service is waiting for a restart to finish. ${setupLink('Open Model setup to finish it')}. ${thinking}</p>`;
|
||||
if(owner&&serviceState==='not-running')return `<p class="small warn" id="routes-catalogue">${lead} The model service is not running. ${setupLink('Open Model setup')}. ${thinking}</p>${words}`;
|
||||
if(owner&&(serviceLoading||(serviceState===null&&triageOwner)))return `<p class="small warn" id="routes-catalogue">${lead} Checking the model service. ${thinking}</p>`;
|
||||
if(owner&&serviceState===false)return `<div class="banner routes-catalogue" id="routes-catalogue" role="status"><p>${lead}${plain} The model service's status could not be read. ${thinking}</p>${words}<p class="actions">${recheck}</p></div>`;
|
||||
return `<p class="small warn" id="routes-catalogue">${lead}${plain} ${thinking}${owner?' A new model needs the model service: ask the instance owner to set it up, or press Stop using your models when this organization does not need it.':''}</p>${words}`;
|
||||
}
|
||||
// Check again rereads Routing; the bus looks for the model service at that read. A draft is kept.
|
||||
document.addEventListener('click',e=>{if(e.target.id!=='routes-recheck')return;e.target.disabled=true;serviceState=null;routesView=null;last.delete('routes');render(true);});
|
||||
document.addEventListener('mosaic-org-restarted',e=>{if(e.detail?.id!==window.MosaicOrg?.id)return;serviceState=null;routesView=null;last.delete('routes');const path=routeOf().split('?')[0];if(path==='/settings/routing'||document.querySelector('[data-org-down]'))render(true);});
|
||||
// The choice reads only the plugins read: before it answers the radios wait, and a failed read
|
||||
// says so with Read again. Use your models writes at once; Stop using your models is the way back.
|
||||
// While the organization needs a restart, its banner's Restart is the one primary and the choice waits.
|
||||
// A Console with no organizations says "this business": lowercase inside a sentence, capitalized
|
||||
// at its start (cap).
|
||||
const modelsWho=cap=>window.MosaicOrg?(window.MosaicOrg.label||window.MosaicOrg.id):cap?'This business':'this business';
|
||||
const modelsWhose=cap=>`${modelsWho(cap)}'s`;
|
||||
// settings-s5 review P2-3: a choice that is not in force says when the model service behind it is
|
||||
// not set up or not answering (the owner's status read, as on the catalogue line). The choice
|
||||
// still writes: riding before setup is the design's "Nothing set up" state.
|
||||
function modelsService(){
|
||||
const link=text=>`<a href="#/settings/model-setup">${text}</a>`;
|
||||
return serviceState==='not-set-up'?`<p class="small warn" id="models-service">Your model service isn't set up. ${link('Set it up in Model setup')}.</p>`
|
||||
:serviceState==='not-running'?`<p class="small warn" id="models-service">Your model service isn't answering. ${link('Restart it in Model setup')}.</p>`
|
||||
:serviceState==='fenced'?`<p class="small warn" id="models-service">Your model service is waiting for a restart to finish. ${link('Open Model setup to finish it')}.</p>`:'';
|
||||
}
|
||||
function modelsChoice(restartFirst){
|
||||
const who=txt(modelsWho()), Who=txt(modelsWho(true)), whose=txt(modelsWhose()), Whose=txt(modelsWhose(true));
|
||||
const names=harnessSettings&&Array.isArray(harnessSettings.plugins?.['model-provider'])?harnessSettings.plugins['model-provider']:[];
|
||||
const read=!!harnessSettings, on=read&&names.includes('cliproxy'), wait=!read||modelsBusy||restartFirst||out();
|
||||
if(triageOwner)readService();
|
||||
const now=harnessSettings===false?`Which models ${who} uses could not be read.`:!read?`Reading ${whose} models…`:on?`Now: ${who} uses your models.`:`Now: ${who} does not use your models. Its agents use only the models in its own organization file.`;
|
||||
const change=!read?'':restartFirst?`<p class="small" id="models-wait">You can change ${whose} models after it restarts.</p>`
|
||||
:on?`<p class="small muted" id="models-change">Its agents can use the models your accounts offer. To switch back, press Stop using your models. A route on one of your models then stops working until you change it.</p>`
|
||||
:`<p class="small muted" id="models-change">Choose Use your models to switch. ${Whose} agents can then use the models your accounts offer. While your model service is down, a launch that needs it is refused. A running agent keeps its model until it starts again.</p>${modelsService()}`;
|
||||
// P2-1: while the organization needs a restart every control here waits. Stop is left out, as in
|
||||
// the mockup, since a disabled button here looks no different from an enabled one.
|
||||
const stop=on&&!restartFirst?`<button type="button" id="models-stop"${modelsBusy||out()?' disabled':''}>${modelsBusy?'<span class="settings-spinner" aria-hidden="true"></span> Saving…':'Stop using your models'}</button>`:'';
|
||||
const again=harnessSettings===false?'<button type="button" id="models-read">Read again</button>':'';
|
||||
// P3-1: Use your models has no button of its own, so its write shows Saving in the status line.
|
||||
const status=modelsBusy&&!on?'<span class="settings-spinner" aria-hidden="true"></span> Saving…':txt(modelsReceipt);
|
||||
return `<fieldset class="models-source" id="models-source"><legend>Where ${who} gets its models</legend>`
|
||||
+`<label class="choice"><input type="radio" name="models-source" id="models-yours" value="yours"${on?' checked':''}${wait?' disabled':''}><strong>Use your models</strong><span class="badge badge-ok">Recommended</span><span class="small muted">${Who} uses the model service and accounts in Model setup. Nothing to sign in to.</span></label>`
|
||||
+`<label class="choice off"><input type="radio" name="models-source" id="models-own" value="own" disabled><strong>Use its own model service</strong><span class="badge badge-muted">Coming later</span><span class="small muted">${Who} would run a second model service with its own accounts. Planned, not built yet, so you can't choose it today.</span></label>`
|
||||
+`<p id="models-in-force">${now}</p>${change}<p class="actions">${stop}${again}</p>`
|
||||
+`<p role="status" id="models-receipt" class="${modelsBusy&&!on?'':modelsReceiptKind}" tabindex="-1" data-focus-key="models-receipt">${status}</p></fieldset>`;
|
||||
}
|
||||
function routesSection(view,instanceInfo){
|
||||
const title='<h2 id="routes-title">Routing</h2>';
|
||||
readHarnesses();
|
||||
@@ -1334,11 +1425,13 @@
|
||||
// fixture-only catalogue is ready with no model proxy). Before the setting is read it falls
|
||||
// back to the catalogue status, as before.
|
||||
const proxyOn=harnessSettings?(Array.isArray(harnessSettings.plugins?.['model-provider'])?harnessSettings.plugins['model-provider']:[]).includes('cliproxy'):v.catalogue?.status!=='disabled';
|
||||
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)}</p>`).join('')}`:v.catalogue?.status==='unavailable'?`<p class="small warn">Model catalogue unavailable. A change to thinking alone still saves.${proxyOn&&canSend&&!isTester?' A new model needs the catalogue: set up the model proxy, or, when this organization has none, press Disable model proxy catalogue.':''}</p>${(v.catalogue.notices??[]).map(n=>`<p class="small warn">${txt(n)}</p>`).join('')}`:'<p class="small muted">Model catalogue not enabled.</p>';
|
||||
const providerControl=canSend&&!isTester?`<button type="button" id="provider-switch" data-enabled="${proxyOn}">${proxyOn?'Disable':'Enable'} model proxy catalogue</button><p role="status" id="provider-switch-status"></p>`:'';
|
||||
// settings-p3 (re-check 3 P3-5): a ready catalogue the bus picked up late says agents start on these
|
||||
// models after a restart; that notice gets the organization's Restart (not primary: Save routes is).
|
||||
const catalogueInfo=v.catalogue?.status==='ready'?`<p class="small muted">Model catalogue, observed ${txt(window.MosaicLocalTime(v.catalogue.observedAt))}.</p>${(v.catalogue.notices??[]).map(n=>`<p class="small warn">${txt(n)}${n===window.MosaicConsole?.RESTART_TO_LAUNCH&&window.MosaicOrg?.id?` ${window.MosaicConsole.orgRestartButton(window.MosaicOrg.id,{name:window.MosaicOrg.label||window.MosaicOrg.id})}`:''}</p>`).join('')}`:v.catalogue?.status==='unavailable'?catalogueLine(proxyOn,v.catalogue):'<p class="small muted">Model catalogue not enabled.</p>';
|
||||
const providerControl=canSend&&!isTester?modelsChoice(/ data-primary[ >]/.test(catalogueInfo)):'';
|
||||
const harnessControl=canSend&&!isTester?`<fieldset class="harness-control"><legend>Harnesses</legend>${harnessSettings ? [...new Set([...harnessSettings.available.harness,...(harnessSettings.plugins.harness??[])])].map(name=>`<label><input type="checkbox" data-harness="${esc(name)}"${(harnessDraft??harnessSettings.plugins.harness??[]).includes(name)?' checked':''}${harnessBusy?' disabled':''}> ${txt(name)}</label>`).join('')+` <button type="button" id="harness-save"${harnessBusy?' disabled':''}>Save harnesses</button>` : '<p>Harnesses unavailable.</p><button type="button" id="harness-read">Read harnesses again</button>'}<p role="status" id="harness-status">${txt(harnessReceipt)}</p></fieldset>`:'';
|
||||
const off=v.harnesses.length?'':'<p class="small warn">No harness is switched on in the business file\'s plugins.harness, so every launch refuses harness-off and a save that changes a harness refuses too. 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"${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>`;
|
||||
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>`;
|
||||
}
|
||||
function routesFields(form){const draft=Object.fromEntries(Object.keys(routesView.instances).map(name=>[name,routeCells(routesView.instances[name])]));for(const el of form.querySelectorAll('[data-route]')){const [name,key]=JSON.parse(el.dataset.route);if(draft[name])draft[name][key]=el.value;}return draft;}
|
||||
// The changed rows only. A fallback with all three cells empty is cleared; one with some set goes
|
||||
@@ -1357,13 +1450,13 @@
|
||||
// A viewer without the switch is sent to the owner.
|
||||
function catalogueStep(code,routes){
|
||||
if(!['model-catalogue-unavailable','model-provider-unavailable'].includes(code))return '';
|
||||
if(!canSend||isTester)return ' Ask the organization\'s owner to set up the model proxy, or to disable the model proxy catalogue when the change needs no proxy.';
|
||||
if(!canSend||isTester)return ' Ask the organization\'s owner to set up the model service, or to stop using it when the change does not need it.';
|
||||
const names=Array.isArray(harnessSettings?.plugins?.['model-provider'])?harnessSettings.plugins['model-provider']:[];
|
||||
if(!names.includes('cliproxy'))return '';
|
||||
const moved=Object.entries(routes).flatMap(([name,r])=>{const was=routesView?.instances?.[name]??{};
|
||||
return [[r.harness,r.model,was.harness,was.model],[r.fallback?.harness,r.fallback?.model,was.fallback?.harness,was.fallback?.model]].filter(([h,m,wh,wm])=>m&&(h!==wh||m!==wm)).map(([,m])=>m);});
|
||||
const module=key=>routesView?.providerModels?.[key]?.module;
|
||||
return moved.length&&moved.every(key=>module(key)&&module(key)!=='cliproxy'&&names.includes(module(key)))?' This change needs no model proxy: press Disable model proxy catalogue, then save again.':'';
|
||||
return moved.length&&moved.every(key=>module(key)&&module(key)!=='cliproxy'&&names.includes(module(key)))?' This change does not need your models: press Stop using your models, then save again.':'';
|
||||
}
|
||||
const routesAnswer=r=>/^[a-f0-9]{64}$/.test(r?.revision??'')&&Array.isArray(r.harnesses)&&Array.isArray(r.models)&&[r.levels,r.instances].every(o=>o&&typeof o==='object'&&!Array.isArray(o));
|
||||
for(const kind of ['input','change'])document.addEventListener(kind,e=>{const form=e.target?.closest?.('#routes-form');if(!form||!routesView)return;routesDraft=routesFields(form);routesEditVersion++;markSettingsDraft('routing',true);form.querySelector('.settings-actions')?.classList.add('has-draft');const discard=$('routes-reload');if(discard){discard.hidden=false;discard.disabled=false;}
|
||||
@@ -1383,17 +1476,32 @@
|
||||
catch{if(epoch!==orgEpoch)return;harnessReceipt='Outcome unknown. Read the harnesses again before trying.';harnessSettings=false;}
|
||||
finally{if(epoch===orgEpoch){harnessBusy=false;if(!out())render(false);}}
|
||||
});
|
||||
document.addEventListener('click',async e=>{if(e.target.id!=='provider-switch'||!canSend||isTester)return;
|
||||
const button=e.target,status=$('provider-switch-status');if(routesDraft){status.textContent='Save or discard the route draft first.';return;}button.disabled=true;const epoch=orgEpoch;
|
||||
try {const read=await fetch('/api/proxy/plugins',{cache:'no-store'});const settings=await read.json();if(epoch!==orgEpoch)return;if(!read.ok||!/^[a-f0-9]{64}$/.test(settings.revision??''))throw Error('Could not read provider settings.');
|
||||
const names=Array.isArray(settings.plugins?.['model-provider'])?settings.plugins['model-provider']:[];
|
||||
// The fresh read decides: a stale label never turns the proxy module on when it meant off.
|
||||
const on=names.includes('cliproxy');if(String(on)!==button.dataset.enabled){button.dataset.enabled=String(on);button.textContent=`${on?'Disable':'Enable'} model proxy catalogue`;button.disabled=false;status.textContent='The switch now shows the current setting. Press it again to change it.';return;}
|
||||
const next=on?names.filter(n=>n!=='cliproxy'):[...names,'cliproxy'];
|
||||
const response=await fetch('/api/proxy/plugins/set',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({revision:settings.revision,plugins:{'model-provider':next}}),cache:'no-store',redirect:'manual'});
|
||||
if(epoch!==orgEpoch)return;if(!response.ok)throw Error('Provider setting was refused.');harnessSettings=null;routesView=null;last.delete('routes');render(true);
|
||||
}catch(error){if(epoch!==orgEpoch)return;status.textContent=error.message;button.disabled=false;}
|
||||
});
|
||||
// The fresh read decides: a page that shows a stale choice is redrawn from it and nothing is written.
|
||||
async function setModels(want){
|
||||
if(!canSend||isTester||modelsBusy||!harnessSettings)return;
|
||||
const who=modelsWho(true), shown=(harnessSettings.plugins?.['model-provider']??[]).includes('cliproxy');
|
||||
let sent=false;
|
||||
if(routesDraft){modelsReceipt='Save or discard the route draft first.';modelsReceiptKind='banner err';modelsFocus=true;render(false);return;}
|
||||
modelsBusy=true;modelsReceipt='';modelsReceiptKind='';render(false);const epoch=orgEpoch;
|
||||
try{const read=await fetch('/api/proxy/plugins',{cache:'no-store',redirect:'manual'});const settings=await read.json();if(epoch!==orgEpoch)return;
|
||||
if(!read.ok||!/^[a-f0-9]{64}$/.test(settings.revision??'')||!Array.isArray(settings.available?.harness))throw Error('read');
|
||||
const names=Array.isArray(settings.plugins?.['model-provider'])?settings.plugins['model-provider']:[], on=names.includes('cliproxy');
|
||||
if(on!==shown){harnessSettings=settings;modelsReceipt=`${modelsWhose(true)} models changed since this page read it. The page now shows the current choice; choose again to change it.`;modelsReceiptKind='banner ref';return;}
|
||||
if(on===want)return;
|
||||
const next=want?[...names,'cliproxy']:names.filter(n=>n!=='cliproxy');
|
||||
sent=true;const response=await fetch('/api/proxy/plugins/set',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({revision:settings.revision,plugins:{'model-provider':next}}),cache:'no-store',redirect:'manual'});
|
||||
const result=await response.json().catch(()=>({}));if(epoch!==orgEpoch)return;
|
||||
if(!response.ok){modelsReceipt=`Not changed. ${result.message||result.error||'The change was refused.'}`;modelsReceiptKind='banner err';return;}
|
||||
modelsReceipt=want?`Saved. ${who} uses your models. An agent picks them up the next time it starts.`:`Saved. ${who} no longer uses your models. An agent on one of them keeps it until it starts again.`;modelsReceiptKind='settings-success';
|
||||
harnessSettings=null;routesView=null;last.delete('routes');window.MosaicModelDots?.read?.();
|
||||
// P3-2: a failed read before the write changed nothing and says so, with Read again.
|
||||
}catch{if(epoch!==orgEpoch)return;if(sent){modelsReceipt='Outcome unknown. Read the page again before trying.';modelsReceiptKind='banner ref';harnessSettings=null;}else{modelsReceipt='Not changed. Nothing was written.';modelsReceiptKind='banner err';harnessSettings=false;}}
|
||||
// P2-4: the render that shows the outcome moves focus to it (render() below).
|
||||
// Recheck N2: only while the page is still shown; a switch left mid-write does not take focus later.
|
||||
finally{if(epoch===orgEpoch){modelsBusy=false;modelsFocus=!!modelsReceipt&&routeOf().split('?')[0]==='/settings/routing';if(!out())render(harnessSettings===null);}}
|
||||
}
|
||||
document.addEventListener('change',e=>{if(e.target.id==='models-yours'&&e.target.checked)setModels(true);});
|
||||
document.addEventListener('click',e=>{if(e.target.closest?.('#models-stop'))setModels(false);else if(e.target.id==='models-read'){harnessSettings=null;harnessReceipt='';modelsReceipt='';modelsReceiptKind='';render(false);}});
|
||||
document.addEventListener('click',e=>{if(e.target.matches?.('.fallback-toggle'))e.target.closest('tr')?.classList.add('fallback-open');});
|
||||
document.addEventListener('click',e=>{if(e.target.id!=='routes-reload')return;routesDraft=null;routesView=null;routesReceipt='';routesApply=null;last.delete('routes');render(true);});
|
||||
document.addEventListener('submit',async e=>{
|
||||
@@ -1432,23 +1540,38 @@
|
||||
});
|
||||
// Organizations is writable when the server allows org setup, which it does in organization mode where send is off.
|
||||
const readonlyPage = page => !['appearance','business','console','triage'].includes(page) && !(page === 'organizations' && orgSetup) && !canSend && !isTester;
|
||||
const settingsPages = [['appearance','Appearance'],['business',window.MosaicOrg ? 'Organization' : 'Business'],['limits','Launch limits'],['organizations','Organizations'],['routing','Routing'],['agents','Agents'],['assistant','Assistant'],['tokens','Token use'],['model-proxy','Model proxy'],['triage','Triage'],['console','Console'],['about','About']];
|
||||
function markSettingsDraft(page,dirty){const link=document.querySelector(`#settings-nav a[href="#/settings/${page}"]`), dot='<span class="settings-dot"><span class="sr-only">unsaved changes</span></span>';if(link && dirty && !link.querySelector('.settings-dot'))link.insertAdjacentHTML('beforeend',dot);const button=$('settings-disclosure');if(dirty && link?.getAttribute('aria-current')==='page' && button && !button.querySelector('.settings-dot'))button.querySelector('strong').insertAdjacentHTML('beforeend',dot);}
|
||||
const settingsPages = [['appearance','Appearance'],['business',window.MosaicOrg ? 'Organization' : 'Business'],['limits','Launch limits'],['organizations','Organizations'],['routing','Routing'],['agents','Agents'],['assistant','Assistant'],['tokens','Token use'],['model-setup','Model setup'],['triage','Triage'],['console','Console'],['about','About']];
|
||||
function markSettingsDraft(page,dirty){const link=document.querySelector(`#settings-nav a[href^="#/settings/${page}"]`), dot='<span class="settings-dot"><span class="sr-only">unsaved changes</span></span>';if(link && dirty && !link.querySelector('.settings-dot'))link.insertAdjacentHTML('beforeend',dot);const button=$('settings-disclosure');if(dirty && link?.getAttribute('aria-current')==='page' && button && !button.querySelector('.settings-dot'))button.querySelector('strong').insertAdjacentHTML('beforeend',dot);}
|
||||
// #1873 settings slice 4 (DESIGN.md section 2): four groups. You; Your models; All organizations;
|
||||
// This organization, whose heading holds the organization picker. An organization's own pages carry
|
||||
// ?org=<id> so the address base never changes with the organization. Testers keep their short list.
|
||||
const ORG_SETTINGS = ['routing','agents','limits','tokens','assistant','business','names'];
|
||||
function settingsShell(page, body, helper='') {
|
||||
const pages=isTester ? [['appearance','Appearance'],['business','Business'],['routing','Routing'],['agents','Agents'],['names','Seat names']] : settingsPages.filter(([id])=>id!=='triage'||triageOwner);
|
||||
const label=pages.find(([id])=>id===page)?.[1] || 'Appearance';
|
||||
const org = window.MosaicOrg, orgId = org?.id ?? null, orgName = orgId ? (org.label || orgId) : null;
|
||||
const scoped = id => ORG_SETTINGS.includes(id);
|
||||
const href = id => `#/settings/${id}${orgId && scoped(id) ? `?org=${enc(orgId)}` : ''}`;
|
||||
const labels = isTester ? Object.fromEntries([['appearance','Appearance'],['business','Business'],['routing','Routing'],['agents','Agents'],['names','Seat names']]) : { appearance:'Appearance', 'model-setup':'Model setup', triage:'Triage', organizations:'Organizations', console:'Console', about:'About', routing: org ? 'Models' : 'Routing', agents:'Agents', limits:'Launch limits', tokens:'Token use', assistant:'Assistant', business: org ? 'Organization file' : 'Business' };
|
||||
const label = labels[page] || 'Appearance';
|
||||
const isDirty=id=>(id==='routing'&&routesDraft)||(id==='limits'&&settingsInputs)||(id==='assistant'&&(Object.keys(assistantDrafts).length||Object.keys(nameDrafts).length))||(id==='agents'&&(instanceDrafts.add||instanceDrafts.change||window.MosaicAgentLevels?.hasDraft()))||(id==='triage'&&window.MosaicTriageRoute?.hasDraft());
|
||||
const dot='<span class="settings-dot"><span class="sr-only">unsaved changes</span></span>';
|
||||
const links=pages.map(([id,name],i)=>`${i===0?'<div class="settings-group">Your browser</div>':i===1?'<div class="settings-group">This instance</div>':''}<a href="#/settings/${id}"${id===page?' aria-current="page"':''}>${name}${isDirty(id)?dot:''}</a>`).join('') + (!isTester ? '<div class="settings-group">Members and invites <span class="muted">not built</span></div>' : '');
|
||||
return `${head('Settings','Your browser and this instance.')}<div class="settings-layout"><div><button type="button" id="settings-disclosure" aria-expanded="false" aria-controls="settings-nav"><small>Section</small><strong>${label}${isDirty(page)?dot:''}</strong><span aria-hidden="true">⌄</span></button><nav id="settings-nav" aria-label="Settings sections">${links}</nav></div><div class="settings-content settings-${page}${readonlyPage(page)?' readonly':''}"><h2 id="settings-page-title" tabindex="-1">${label}</h2><p class="settings-helper">${helper}</p>${readonlyPage(page)?'<p class="settings-readonly">Read only. Saving requires signed-in send mode.</p>':''}${body}</div></div>`;
|
||||
const a = id => `<a href="${href(id)}"${id===page?' aria-current="page"':''}>${labels[id]}${isDirty(id)?dot:''}</a>`;
|
||||
const group = (title, ids) => ids.length ? `<div class="settings-group">${title}</div>${ids.map(a).join('')}` : '';
|
||||
const rows = org ? org.rows() : [];
|
||||
const picker = org && rows.length ? `<div class="settings-group org-pick"><label for="org-pick">This organization</label><select id="org-pick">${rows.map(r => `<option value="${txt(r.id)}"${r.id === orgId ? ' selected' : ''}>${txt(r.label)}</option>`).join('')}</select></div>` : `<div class="settings-group">${org ? 'This organization' : 'This business'}</div>`;
|
||||
const links = isTester ? group('Settings', ['appearance','business','routing','agents','names'])
|
||||
: group('You', ['appearance']) + group('Your models', ['model-setup', ...(triageOwner ? ['triage'] : [])]) + group(org ? 'All organizations' : 'This instance', ['organizations','console','about'])
|
||||
+ picker + ['routing','agents','limits','tokens','assistant','business'].map(a).join('');
|
||||
const where = orgName && scoped(page) ? `${txt(orgName)} · ` : '';
|
||||
const title = org && page === 'routing' && orgName ? `${txt(orgName)}'s models` : label;
|
||||
return `${head('Settings','Your models, your organizations and this browser.')}<div class="settings-layout"><div><button type="button" id="settings-disclosure" aria-expanded="false" aria-controls="settings-nav"><small>Section</small><strong>${where}${label}${isDirty(page)?dot:''}</strong><span aria-hidden="true">⌄</span></button><nav id="settings-nav" aria-label="Settings sections">${links}</nav></div><div class="settings-content settings-${page}${readonlyPage(page)?' readonly':''}"><h2 id="settings-page-title" tabindex="-1">${title}</h2><p class="settings-helper">${helper}</p>${readonlyPage(page)?'<p class="settings-readonly">Read only. Saving requires signed-in send mode.</p>':''}${body}</div></div>`;
|
||||
}
|
||||
async function settingsView(get, page='appearance') {
|
||||
if (page==='business') return businessView(get);
|
||||
if (page==='appearance') return settingsShell(page,appearance(),'These choices apply in this browser only.');
|
||||
if (page==='organizations') { await configRead; return settingsShell(page,window.MosaicOrgCreate.markup,'Create an organization from the template, edit it, and finish its setup. Each step names the command it equals.'); }
|
||||
if (page==='model-proxy') { await configRead; return settingsShell(page,window.MosaicProxy.markup,'Accounts used by this instance, their priority, and the client keys assigned to agents.'); }
|
||||
if (page==='model-setup') { await configRead; return settingsShell(page,window.MosaicProxy.markup,'Your model service, your accounts and the models they offer. Every organization uses them, including organizations you add later, unless it sets up its own.'); }
|
||||
// #1873 triage-settings: Settings > Triage (triage-settings.js), the instance owner's only; anyone else gets no page.
|
||||
if (page==='triage') { await configRead; if (!triageOwner || !window.MosaicTriageRoute) return notFound(); return settingsShell(page,await window.MosaicTriageRoute.section(),'The model, harness and daily token cap Triage uses. Only the instance owner sees this page.'); }
|
||||
if (page==='triage') { await configRead; if (!triageOwner || !window.MosaicTriageRoute) return notFound(); return settingsShell(page,await window.MosaicTriageRoute.section(),'The model Triage uses, what runs it, and its daily token limit. Only the instance owner sees this page.'); }
|
||||
// F3: Settings > Console (console-restart.js). The server checks the owner, Origin and trail.
|
||||
if (page==='console') { await configRead; await window.MosaicConsole?.ready; return settingsShell(page,window.MosaicConsole?.settingsMarkup()??'<section class="panel"><p class="small warn">The Console controls did not load. Reload the page to try again.</p></section>','Restart this Console on the same address and configuration.'); }
|
||||
const s = await get.api('settings'), n = s.notifier || {};
|
||||
@@ -1459,15 +1582,29 @@
|
||||
// #1873 A2: the root and project agent levels (agents.js MosaicAgentLevels), the owner's only.
|
||||
let agentLevels='';
|
||||
if (page==='agents' && window.MosaicOrg && window.MosaicAgentLevels && !isTester) agentLevels = await window.MosaicAgentLevels.section().catch(()=>'');
|
||||
if (page==='routing') { try { [routes] = await get('routes'); } catch (err) { if (err.status === 403 || err.code === 'signed-out') throw err; } }
|
||||
if (page==='routing') { await window.MosaicConsole?.ready; try { [routes] = await get('routes'); } catch (err) { if (err.status === 403 || err.code === 'signed-out') throw err; } }
|
||||
if (page==='tokens' && s.launch && !s.launch.refused) takeSnapshot(s.launch);
|
||||
const notify = typeof n.binding==='string' ? `<code>${txt(n.binding)}</code>` : n.refused ? `Could not be read: <code>${txt(n.refused)}</code>` : '<span class="muted">None set</span>';
|
||||
const contents={limits:()=>launchForm(s.launch),routing:()=>routesSection(routes,instances),agents:()=>agentsSection(instances)+agentLevels,assistant:()=>assistantSection(s.launch,names),tokens:()=>tokenLimits(limitRows,limitsRefused)+tokenUsage(usage),names:()=>testerNames(names,instances),about:()=>`<section class="panel"><dl class="kv"><dt>Address</dt><dd><code>${txt(s.address)||'unknown'}</code></dd><dt>Release</dt><dd>${txt(s.release)||'unknown'}</dd><dt>Business</dt><dd>${txt(s.business)||none}</dd><dt>Board</dt><dd><code>${txt(s.board)||'unknown'}</code></dd><dt>Notifier binding</dt><dd>${notify}</dd></dl></section>`, 'model-proxy':()=>'<section class="panel"><a href="#/settings/model-proxy">Open model proxy settings</a></section>'};
|
||||
const helpers={limits:'Leave a limit blank for no limit. Zero stops new launches. Running sessions keep running when limits or models change.',routing:"Each agent’s harness, model and thinking, and the fallback a launch takes when the primary harness fails its preflight. A fallback sets all three or none. Running sessions keep their route. The next launch reads the change.",agents:'Each agent with its definition, launch membership and runs. A launch refuses until the agent has a route in Routing.',assistant:'The definition stays an assistant one. Holder and credentials stay hand edits.',tokens:'Token limits the bus checks, and token use read from session transcripts.'};
|
||||
const contents={limits:()=>launchForm(s.launch),routing:()=>routesSection(routes,instances),agents:()=>agentsSection(instances)+agentLevels,assistant:()=>assistantSection(s.launch,names),tokens:()=>tokenLimits(limitRows,limitsRefused)+tokenUsage(usage),names:()=>testerNames(names,instances),about:()=>`<section class="panel"><dl class="kv"><dt>Address</dt><dd><code>${txt(s.address)||'unknown'}</code></dd><dt>Release</dt><dd>${txt(s.release)||'unknown'}</dd><dt>Business</dt><dd>${txt(s.business)||none}</dd><dt>Board</dt><dd><code>${txt(s.board)||'unknown'}</code></dd><dt>Notifier binding</dt><dd>${notify}</dd></dl></section>`};
|
||||
const helpers={limits:'Leave a limit blank for no limit. Zero stops new launches. Running sessions keep running when limits or models change.',routing:"Each agent’s harness, model and thinking, and the fallback a launch takes when the primary harness fails its preflight. A fallback sets all three or none. Running sessions keep their route. The next launch reads the change. A harness is the program that runs the model. Preflight is the check a launch makes before it starts.",agents:'Each agent with its definition, launch membership and runs. A launch refuses until the agent has a route in Routing.',assistant:'The definition stays an assistant one. Holder and credentials stay hand edits.',tokens:'Token limits the bus checks, and token use read from session transcripts.'};
|
||||
// settings-s5 review P2-2: the header says where the models come from only from the plugins read.
|
||||
const riding=harnessSettings?(harnessSettings.plugins?.['model-provider']??[]).includes('cliproxy'):null;
|
||||
if(window.MosaicOrg&&!isTester)helpers.routing=`The models ${modelsWhose()} agents use.${riding===true?' They come from your accounts in Model setup.':riding===false?' They come from its own organization file.':''} ${helpers.routing}`;
|
||||
return settingsShell(page,contents[page](),`${get.oldest()?`Read ${when(get.oldest())}.`: 'Reading…'} ${helpers[page]||''}`);
|
||||
}
|
||||
let settingsFocusDisclosure=false;
|
||||
document.addEventListener('change', e => { if (e.target.id === 'org-picker') window.MosaicOrg?.switchTo(e.target.value); });
|
||||
document.addEventListener('change', e => { if (e.target.id === 'org-picker') window.MosaicOrg?.switchTo(e.target.value); else if (e.target.id === 'org-pick') pickSettingsOrg(e.target.value); });
|
||||
// Slice 4: the picker re-reads the page in place. An organization's page keeps its page with the new
|
||||
// ?org=.
|
||||
function pickSettingsOrg(id) {
|
||||
const org = window.MosaicOrg; if (!org || !id) return;
|
||||
const [path, query = ''] = routeOf().split('?'), page = path.split('/')[2] || 'appearance';
|
||||
const rest = query.split('&').filter(p => p && !p.startsWith('org='));
|
||||
// settings-s4 review F1: from a page that is not an organization's (Model setup, Appearance,
|
||||
// Organizations) the picker opens that organization's first page, Routing, and keeps the
|
||||
// single-pane address base; it never builds an #/orgs/<id>/ address.
|
||||
location.hash = ORG_SETTINGS.includes(page) ? `#/settings/${page}?${[`org=${enc(id)}`, ...rest].join('&')}` : `#/settings/routing?org=${enc(id)}`;
|
||||
}
|
||||
document.addEventListener('click', e=>{const b=e.target.closest?.('#settings-disclosure');if(b){b.setAttribute('aria-expanded',String(b.getAttribute('aria-expanded')!=='true'));return;} if(e.target.closest?.('#settings-nav a')) { const b=$('settings-disclosure'); if(b?.getAttribute('aria-expanded')==='true') {b.setAttribute('aria-expanded','false');settingsFocusDisclosure=true;b.focus();} }});
|
||||
document.addEventListener('keydown', e=>{if(e.key==='Escape' && $('settings-disclosure')?.getAttribute('aria-expanded')==='true'){e.preventDefault();$('settings-disclosure').setAttribute('aria-expanded','false');$('settings-disclosure').focus();}});
|
||||
|
||||
@@ -1674,7 +1811,29 @@
|
||||
const refusal = err => err.status === 403 || ['not-configured', 'no-bus-host', 'queue-refused'].includes(err.code);
|
||||
// After a refusal nothing read before is shown anywhere: not in a view, the palette or the Inbox count.
|
||||
function forget() { window.MosaicChat?.forget(); last.clear(); inboxCount = null; messageCount = null; }
|
||||
const failedView = (err, path) => `${head(path === '/queue' ? 'Queue' : failTitle(err))}<div class="banner ${refusal(err) ? 'ref' : 'err'}" role="alert"><p>${refusal(err) ? `<b>refused: <code>${txt(err.code)}</code>.</b> ` : ''}${why(err, err.code === 'queue-refused')} Console shows nothing rather than a guess. Nothing was written.</p>${retry}</div>${empty('Nothing to show.', refusal(err) ? 'The view stays empty until the problem above is fixed.' : err.module ? 'Read again.' : 'Read again, or come back when the bus answers.')}`;
|
||||
// One shipped copy of the line every failed read ends with (failedView and settingsFailed).
|
||||
const NO_GUESS = 'Console shows nothing rather than a guess. Nothing was written.';
|
||||
const failedView = (err, path) => `${head(path === '/queue' ? 'Queue' : failTitle(err))}<div class="banner ${refusal(err) ? 'ref' : 'err'}" role="alert"><p>${refusal(err) ? `<b>refused: <code>${txt(err.code)}</code>.</b> ` : ''}${why(err, err.code === 'queue-refused')} ${NO_GUESS}</p>${retry}</div>${empty('Nothing to show.', refusal(err) ? 'The view stays empty until the problem above is fixed.' : err.module ? 'Read again.' : 'Read again, or come back when the bus answers.')}`;
|
||||
// #1873 settings slice 1 (D7): an organization page under Settings that cannot be read keeps the
|
||||
// Settings nav, so the instance pages (Appearance, Model setup, Triage, Console) stay one link
|
||||
// away. When the organization's bus is down the page says so in plain words and offers Restart,
|
||||
// whose refusal stays on screen (console-restart.js keeps it per organization); the header's own
|
||||
// "Bus unavailable" banner steps aside on this page so there is one Restart, not two.
|
||||
function settingsFailed(err, path) {
|
||||
const page = path.split('/')[2] || 'appearance', id = window.MosaicOrg?.id, row = id ? window.MosaicOrg.row?.(id) : null;
|
||||
const org = txt(window.MosaicOrg?.label || id || ''), name = settingsPages.find(([p]) => p === page)?.[1] || 'this page';
|
||||
if (id && row?.busHealthy === false && !refusal(err)) {
|
||||
const state = row.busStopped ? `${org} is stopped` : `${org} isn't running`;
|
||||
// Why it is down, from the read the dashboard row uses (MosaicConsole.orgDownReason). Only a
|
||||
// refused start is a reason it stopped; anything else is what the last check saw (P3-9).
|
||||
const label = window.MosaicOrg.label || id, whyText = v => !v ? '' : v.plain && v.plain !== `${label} did not start.` ? txt(v.plain) : `${v.refused === false ? 'Last check' : 'Why it stopped'}: ${txt(v.detail)}`;
|
||||
const why = window.MosaicConsole?.orgDownReason?.(id, label, v => { const p = document.querySelector(`[data-org-down="${CSS.escape(id)}"] [data-org-down-why]`); if (p) { p.innerHTML = whyText(v); p.hidden = !p.innerHTML; } }) ?? null;
|
||||
// settings-p3 (re-check 3 P3-4): after a refusal a restart can't clear, Restart is not the primary.
|
||||
const restart = window.MosaicConsole?.orgRestartButton?.(id, { name: label, primary: !(window.MosaicConsole?.orgRefusal?.(id)?.restart === false || why?.restart === false) }) || '';
|
||||
return settingsShell(page, `<section class="panel org-down" data-org-down="${txt(id)}"><div class="banner err" role="alert"><p><b>${state}, so ${txt(name)} can't be read.</b> Start it again to see and change this page. Nothing was written.</p><p data-org-down-why${why ? '' : ' hidden'}>${whyText(why)}</p>${restart ? `<p>${restart}</p>` : '<p class="small">Ask the instance owner to restart it.</p>'}</div><p class="small">This instance's own pages still work: <a href="#/settings/appearance">Appearance</a>, <a href="#/settings/model-setup">Model setup</a>${triageOwner ? ', <a href="#/settings/triage">Triage</a>' : ''} and <a href="#/settings/console">Console</a>.</p></section>`);
|
||||
}
|
||||
return settingsShell(page, `<div class="banner ${refusal(err) ? 'ref' : 'err'}" role="alert"><p><b>${txt(failTitle(err))}.</b> ${refusal(err) ? `<code>${txt(err.code)}</code>. ` : ''}${why(err, err.code === 'queue-refused')} ${NO_GUESS}</p>${retry}</div>`);
|
||||
}
|
||||
const staleBanner = err => `<div class="banner err s1-stale" role="alert"><p><b>The last read failed.</b> ${why(err)} This is what was read before, ${when(readAt)}. Nothing was changed.</p>${retry}</div>`;
|
||||
// Freshness line in the command bar for bus pages.
|
||||
const stamp = iso => { const t = Date.parse(iso); return Number.isFinite(t) ? `${new Date(t).toISOString().slice(11, 19)} UTC` : 'unknown'; };
|
||||
@@ -1813,10 +1972,15 @@
|
||||
// On the cross-organization pages a filter or selection changes only the query: that is a
|
||||
// repaint of the same page, not a navigation (no loading view, focus and scroll kept).
|
||||
const samePage = drawerPage(path) && typeof shown === 'string' && shown.split('?')[0] === path;
|
||||
if (path !== '/settings/model-proxy' && proxyCleanup) {proxyCleanup();proxyCleanup=null;}
|
||||
if (path !== '/settings/model-setup' && proxyCleanup) {proxyCleanup();proxyCleanup=null;}
|
||||
if ((navigated || path !== '/settings/organizations') && orgCleanup) {orgCleanup();orgCleanup=null;}
|
||||
// Leaving a decision discards its confirmation immediately, including a preview in flight.
|
||||
if (answer && (navigated || path !== (answer.org ? '/inbox' : `/inbox/${answer.id}`))) answer = null;
|
||||
// Recheck N1: arriving at Models reads the model service again; a status read on an earlier visit may be out of date.
|
||||
if (navigated && path === '/settings/routing' && (typeof shown === 'string' ? shown.split('?')[0] : shown) !== path && !serviceLoading) serviceState = null;
|
||||
// settings-s5b: a focus request for the Models outcome line belongs to that page. Going anywhere else drops it,
|
||||
// including Assistant, Projects and the board, which return before the focus chain at the end of render.
|
||||
if (navigated && path !== '/settings/routing') modelsFocus = false;
|
||||
const g = ++gen, routeVersion = routesEditVersion;
|
||||
const current = () => g === gen && route === routeOf() && !out();
|
||||
// In organization mode #/ is the cross-organization dashboard; the processing controls stay on the board.
|
||||
@@ -1829,6 +1993,10 @@
|
||||
window.MosaicChat?.leave(chatPath);
|
||||
if (path === '/business') { location.replace('#/settings/business'); return; }
|
||||
if (path === '/settings/display') { location.replace('#/settings/appearance'); return; }
|
||||
// #1873 settings slice 5: Members is gone from the nav; its old addresses land on the organization's Agents page.
|
||||
if (path === '/settings/members') { location.replace(`#/settings/agents${window.MosaicOrg ? `?org=${enc(window.MosaicOrg.id)}` : ''}`); return; }
|
||||
// #1873 settings slice 4: the owner's page is Model setup; its old address lands there, query kept.
|
||||
if (path === '/settings/model-proxy') { location.replace(`#/settings/model-setup${query ? `?${query}` : ''}`); return; }
|
||||
// #1873 SP6: the Assistant page draws its own thread panes (assistant.js); every other page
|
||||
// stops their polling. It is a system page: an organization's address for it, and the old
|
||||
// Side by side address, land on `#/assistant`.
|
||||
@@ -1911,7 +2079,7 @@
|
||||
sections(path); schedule();
|
||||
return;
|
||||
}
|
||||
if (path === '/settings/model-proxy' && shown === route && !navigated) return;
|
||||
if (path === '/settings/model-setup' && shown === route && !navigated) return;
|
||||
if (path === '/settings/triage' && shown === route && !navigated) return;
|
||||
if (path === '/settings/organizations' && shown?.split('?')[0] === path && !navigated) return;
|
||||
if (($('agent-dialog')?.open || document.querySelector('.agent-more[aria-expanded=true]')) && !navigated) { schedule(); return; }
|
||||
@@ -1919,7 +2087,7 @@
|
||||
let html, state = 'fresh';
|
||||
const live = reader(true);
|
||||
try {
|
||||
if (path !== '/organization' && !wsPage(path) && !(path === '/agents' && window.MosaicOrg) && path !== '/messages' && !['/settings','/settings/appearance','/settings/model-proxy','/settings/triage','/settings/organizations'].includes(path)) {
|
||||
if (path !== '/organization' && !wsPage(path) && !(path === '/agents' && window.MosaicOrg) && path !== '/messages' && !['/settings','/settings/appearance','/settings/model-setup','/settings/triage','/settings/organizations'].includes(path)) {
|
||||
try { await live('messages'); }
|
||||
catch (err) { messageCount = null; if (err.status === 403 || err.code === 'signed-out') throw err; }
|
||||
}
|
||||
@@ -1928,7 +2096,7 @@
|
||||
if (err.code === 'signed-out') return;
|
||||
const kept = reader(false);
|
||||
try { if (refusal(err)) throw err; html = await view(kept, path, new URLSearchParams(query), opts, current); readAt = kept.oldest(); html = staleBanner(err) + html; state = 'stale'; }
|
||||
catch { html = failedView(err, path) + (path === '/settings' ? appearance() : ''); state = refusal(err) ? 'refused' : 'failed'; }
|
||||
catch { if (window.MosaicOrg && path.startsWith('/settings/')) { await window.MosaicConsole?.ready; html = settingsFailed(err, path); } else html = failedView(err, path) + (path === '/settings' ? appearance() : ''); state = refusal(err) ? 'refused' : 'failed'; }
|
||||
if (refusal(err)) forget();
|
||||
}
|
||||
if (!current()) return;
|
||||
@@ -1944,12 +2112,17 @@
|
||||
syncAppearance(true);
|
||||
if (path.startsWith('/docs/')) paintDoc();
|
||||
if (path === '/organization') window.MosaicConsole?.attachBus(document.querySelector('[data-dashboard-bus]'));
|
||||
if (path === '/settings/model-proxy') proxyCleanup=window.MosaicProxy.mount();
|
||||
if ($('bus-view').querySelector('[data-org-down]')) { const down = $('bus-down'); if (down) down.hidden = true; }
|
||||
if (path === '/settings/model-setup') { proxyCleanup?.(); proxyCleanup=window.MosaicProxy.mount(); }
|
||||
if (path === '/settings/organizations') orgCleanup=window.MosaicOrgCreate.mount();
|
||||
sections(path);
|
||||
const h1 = $('bus-view').querySelector('h1');
|
||||
document.title = path === '/settings/business' && $('settings-page-title') ? `Settings · ${window.MosaicOrg ? 'Organization' : 'Business'} · Mosaic Console` : `${h1 ? h1.textContent : 'Console'} · Mosaic Console`;
|
||||
if (k) restore(k);
|
||||
// settings-s5 review P2-4: after a switch on Organization > Models, focus goes to its outcome line.
|
||||
// Recheck N2: a render that paints no outcome line drops the request instead of keeping it.
|
||||
const focusModels = modelsFocus && $('models-receipt'); modelsFocus = false;
|
||||
if (focusModels) { if (k) restore(k); focusModels.focus({ preventScroll: true }); }
|
||||
else if (k) restore(k);
|
||||
// Message on the Board (row 95) lands in the form it opened, addressed to the role.
|
||||
else if (focusSend && $('send-body')) { $('send-body').focus(); announce(`Message to ${draft.to}`); }
|
||||
// SP17: a step link on an organization's Workflows card lands in that step's title field.
|
||||
@@ -2164,6 +2337,8 @@
|
||||
instanceView = null; instanceDrafts = { add: null, change: null }; instanceReceipt = ''; instanceReceiptOp = null; instanceReceipts = Object.create(null);
|
||||
routesView = null; routesDraft = null; routesReceipt = ''; routesEditVersion++;
|
||||
harnessSettings = null; harnessDraft = null; harnessReceipt = '';
|
||||
modelsReceipt = ''; modelsReceiptKind = ''; modelsBusy = false; modelsFocus = false;
|
||||
serviceState = null; serviceLoading = false;
|
||||
// A file draft is keyed by project and path only; the page reload that used to drop it is gone.
|
||||
docKey = null; docStates.clear(); pkAll = []; pkShown = []; pkAt = 0;
|
||||
configRead = switchRead = fetch('/api/config', { cache: 'no-store', redirect: 'manual' }).then(r => { if (r.type === 'opaqueredirect') { signOut(); return null; } return r.json(); }).then(c => applyConfig(c, { quiet: true })).catch(() => {});
|
||||
|
||||
@@ -186,7 +186,12 @@
|
||||
return `<ul class="chat-cites">${list.map(c => `<li>${c.badge ? `<span class="chat-scope chat-scope-${c.badge}">${txt(c.badge)}</span> ` : ''}${c.link ? `<a href="${esc(c.link)}" rel="noopener noreferrer" target="_blank">${txt(c.ref)}</a>` : `<code>${txt(c.ref)}</code>`} <q>${txt(c.quote)}</q>${c.agent ? ` <span class="chat-label chat-agent-written">agent-written: ${txt(c.agent)}</span>` : ''}${c.changed ? ' <span class="chat-label chat-changed">changed since</span>' : ''}</li>`).join('')}</ul>`;
|
||||
}
|
||||
function messageBody(m) {
|
||||
if (m.from_role !== 'human' && typeof m.body === 'string' && m.body.startsWith('The turn failed: ')) return `<div class="chat-body">The assistant couldn't reply. Try again, or check Agents.<div class="small">Details: ${txt(m.body.slice('The turn failed: '.length))}</div></div>`;
|
||||
if (m.from_role !== 'human' && typeof m.body === 'string' && m.body.startsWith('The turn failed: ')) {
|
||||
// #1873 chat-fail: "The turn failed: <plain cause and next step>\nDetails: <technical reason>".
|
||||
const rest = m.body.slice('The turn failed: '.length), at = rest.indexOf('\nDetails: ');
|
||||
if (at > 0) return `<div class="chat-body">The assistant couldn't reply: ${txt(rest.slice(0, at))}<div class="small">Details: ${txt(rest.slice(at + '\nDetails: '.length))}</div></div>`;
|
||||
return `<div class="chat-body">The assistant couldn't reply. Try again, or check Agents.<div class="small">Details: ${txt(rest)}</div></div>`;
|
||||
}
|
||||
const r = renderReply?.(m.body, { check: m.check ?? null, name: m.from_role, changed: changedBy(m.check) });
|
||||
if (r?.kind === 'reply') return `<div class="chat-body">${prose(r.text)}</div>${r.citations.length ? cites(r.citations) : ''}`;
|
||||
if (r?.kind === 'notice') {
|
||||
|
||||
@@ -175,6 +175,8 @@
|
||||
}
|
||||
} catch (error) {
|
||||
busReceipt = error instanceof SyntaxError || error?.name === 'TypeError' ? 'Outcome unknown. Check the bus with mosaic bus status before retrying.' : error.message;
|
||||
// #1873 D7: the refusal follows the organization onto its Settings pages.
|
||||
if (!(error instanceof SyntaxError || error?.name === 'TypeError')) orgReceipts.set(id, { ok: false, ...refusalLine(window.MosaicOrg?.label || id, error.message) });
|
||||
} finally { busBusy = false; paintBus(); }
|
||||
});
|
||||
|
||||
@@ -196,5 +198,117 @@
|
||||
} finally { busBusy = false; paintBus(); }
|
||||
}
|
||||
|
||||
window.MosaicConsole = Object.freeze({ ready, settingsMarkup, busButton, attachBus, get busRestart() { return busAllowed(); }, get config() { return config; } });
|
||||
// #1873 settings slice 1 (D2, D3, D7): a Restart button for one named organization, for pages that
|
||||
// list organizations by id (Routing, Model setup, a stopped organization's Settings). Each
|
||||
// organization keeps its own receipt, so a refusal stays on screen across repaints until the next
|
||||
// press. It calls the same POST /api/orgs/<id>/restart/bus as the header's Restart bus.
|
||||
const orgReceipts = new Map(), orgBusy = new Set();
|
||||
const orgAllowed = () => config?.orgSetup === true && config?.tester !== true && config?.user !== true;
|
||||
// The refusal the bus host wrote, with one plain line in front when its code is known.
|
||||
function refusalLine(name, message) {
|
||||
const text = String(message || 'The restart was refused. Nothing was retried.');
|
||||
// settings-p3 (re-check 3 P3-4): a restart is refused the same way again, so `restart: false`
|
||||
// tells the page not to keep Restart as its primary action.
|
||||
if (/\bfixture-proxy-configured\b/.test(text)) return { plain: `${name} runs on test models, which can't share this machine with your model service. It did not start.`, detail: text, restart: false };
|
||||
return { plain: `${name} did not start.`, detail: text };
|
||||
}
|
||||
// #1873 review P1-1, P2-2: the cause a running bus gives for a catalogue it cannot read
|
||||
// (runtime.mjs pickProxy), as one plain line. `restart` says whether a restart can fix it: the
|
||||
// lock and the organization binding load again at a fresh start; with the dev fixture or a key
|
||||
// or settings file that does not load, the start is refused too or reads the same file. `fix`
|
||||
// names the page that fixes it. The bus's own words stay in `detail`; an unknown cause has no
|
||||
// plain line, so the page shows the bus's words.
|
||||
function catalogueCause(name, notices, reason) {
|
||||
// Two different models that slug to one key: the catalogue names it in `reason`, with no notice.
|
||||
if (reason === 'model-key-collision') return { code: reason, plain: `Two of your models get the same short name, so ${name} can't tell them apart. A restart won't change that. Take one of them out of an API key's Models list on Model setup. If both come from signed-in accounts, this can't be changed from Console yet.`, restart: false, fix: 'service', detail: reason };
|
||||
const list = (Array.isArray(notices) ? notices : []).filter(n => typeof n === 'string' && n);
|
||||
if (!list.length) return null;
|
||||
const detail = list.join(' '), code = /\(([a-z][a-z-]*)\)/.exec(detail)?.[1] ?? (/has not been set up/.test(detail) ? 'proxy-not-set-up' : null);
|
||||
const known = {
|
||||
// No existing endpoint removes the fixture provider, so the line names no step to take here.
|
||||
'fixture-proxy-configured': [`${name} runs on test models, which can't share this machine with your model service. This can't be changed from Console yet.`, false, null],
|
||||
'proxy-lock-invalid': [`${name} can't open the model service's shared lock. A restart opens it again.`, true, null],
|
||||
'invalid-host-request': [`${name} isn't linked to this instance's model service. A restart links it again.`, true, null],
|
||||
'proxy-management-key-invalid': [`${name} can't load the model service's key, so it can't reach the model service.`, false, 'service'],
|
||||
'proxy-down': [`${name} couldn't check the model service just now. Check again in a moment.`, false, null],
|
||||
'proxy-not-set-up': ['Your model service isn\'t set up yet.', false, 'service'],
|
||||
}[code] ?? (/^proxy-/.test(code ?? '') ? [`${name} can't load the model service's settings.`, false, 'service'] : null);
|
||||
return known ? { code, plain: known[0], restart: known[1], fix: known[2], detail } : { code, plain: null, restart: false, fix: null, detail };
|
||||
}
|
||||
function orgRestartButton(id, { name = id, label = `Restart ${name}`, primary = false } = {}) {
|
||||
if (!orgAllowed() || typeof id !== 'string' || !id) return '';
|
||||
const busy = orgBusy.has(id), r = orgReceipts.get(id);
|
||||
const receipt = !r ? '' : r.ok ? esc(r.text) : `<strong>${esc(r.plain)}</strong> <span class="small muted" data-org-restart-detail>${esc(r.detail)}</span>`;
|
||||
return `<span class="org-restart" data-org-restart-box="${esc(id)}"><button type="button" class="btn${primary ? ' primary' : ''}" data-org-restart="${esc(id)}" data-org-name="${esc(name)}" data-label="${esc(label)}"${primary ? ' data-primary' : ''}${busy ? ' disabled' : ''}>${esc(busy ? 'Restarting…' : label)}</button> <span role="status" data-org-restart-receipt class="${r && !r.ok ? 'org-restart-refused' : ''}">${receipt}</span></span>`;
|
||||
}
|
||||
const paintOrg = id => document.querySelectorAll(`[data-org-restart-box="${CSS.escape(id)}"]`).forEach(el => {
|
||||
const b = el.querySelector('[data-org-restart]'), r = orgReceipts.get(id);
|
||||
el.outerHTML = orgRestartButton(id, { name: b?.dataset.orgName || id, label: b?.dataset.label, primary: b?.hasAttribute('data-primary') && r?.restart !== false });
|
||||
});
|
||||
async function restartOrg(id, name) {
|
||||
if (orgBusy.has(id) || !orgAllowed()) return null;
|
||||
orgBusy.add(id); orgReceipts.delete(id); paintOrg(id);
|
||||
let result;
|
||||
try {
|
||||
const response = await nativeFetch(`/api/orgs/${encodeURIComponent(id)}/restart/bus`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: '{}', cache: 'no-store', redirect: 'manual' });
|
||||
if (response.type === 'opaqueredirect') throw new Error('Sign in again, then retry.');
|
||||
const body = await response.json();
|
||||
if (!response.ok || body.outcome !== 'done' || body.step !== 'bus-restart') throw new Error(body.message || 'The restart was refused. Nothing was retried.');
|
||||
result = { ok: true, healthy: body.healthy === true, text: `${name} ${body.restarted ? 'restarted' : 'started'} at ${clock()}. ${body.healthy ? 'It answers.' : `It has not answered yet. Check it with mosaic bus status ${id}.`}` };
|
||||
} catch (error) {
|
||||
result = error instanceof SyntaxError || error?.name === 'TypeError'
|
||||
? { ok: false, plain: 'Outcome unknown.', detail: `Check it with mosaic bus status ${id} before you press Restart again.` }
|
||||
: { ok: false, ...refusalLine(name, error.message) };
|
||||
}
|
||||
orgBusy.delete(id); orgReceipts.set(id, result); paintOrg(id);
|
||||
if (id === window.MosaicOrg?.id) { busReceipt = result.ok ? result.text : `${result.plain} ${result.detail}`; paintBus(); }
|
||||
await Promise.resolve(window.MosaicOrg?.refreshList?.()).catch(() => {});
|
||||
document.dispatchEvent(new CustomEvent('mosaic-org-restarted', { detail: { id, ok: result.ok, healthy: result.healthy === true } }));
|
||||
return result;
|
||||
}
|
||||
document.addEventListener('click', e => {
|
||||
const button = e.target.closest?.('[data-org-restart]');
|
||||
if (!button || button.disabled) return;
|
||||
restartOrg(button.dataset.orgRestart, button.dataset.orgName || button.dataset.orgRestart);
|
||||
});
|
||||
|
||||
// #1873 review 2 P2-D: why a stopped organization is down. GET /api/workspace carries each
|
||||
// organization's state and, for a refused start, "the bus refused to start: <message>"
|
||||
// (workspace-read.mjs). It is the reason the dashboard row shows. One read per organization at a
|
||||
// time, kept for 15 seconds; until it lands this returns what was read before, or null. A reason
|
||||
// that only restates "not running" is not a reason.
|
||||
// settings-p3 (re-check 3 P3-9): only a refused start is a stop reason (`refused: true`); any other
|
||||
// reason (a slow or failed read, a host that does not answer yet) is what the last check saw.
|
||||
// P3-11: the raw reason is kept and each caller's line is built with its own name when used, and
|
||||
// every caller that asked while the read was pending is told when it lands.
|
||||
const downReasons = new Map();
|
||||
const downLine = (raw, name) => !raw ? null : raw.refused ? { ...refusalLine(name, raw.text), refused: true } : { plain: null, detail: raw.text, refused: false };
|
||||
function orgDownReason(id, name = id, onRead = null) {
|
||||
if (typeof id !== 'string' || !id) return null;
|
||||
const got = downReasons.get(id);
|
||||
if (got && (got.pending || Date.now() - got.at < 15000)) {
|
||||
if (got.pending && typeof onRead === 'function') got.waiters.push([name, onRead]);
|
||||
return downLine(got.raw, name);
|
||||
}
|
||||
const entry = { at: got?.at ?? 0, raw: got?.raw ?? null, pending: true, waiters: typeof onRead === 'function' ? [[name, onRead]] : [] };
|
||||
downReasons.set(id, entry);
|
||||
nativeFetch(`/api/workspace?${new URLSearchParams({ org: id, sections: 'tasks' })}`, { cache: 'no-store', redirect: 'manual' })
|
||||
.then(r => (r.ok ? r.json() : null))
|
||||
.then(body => {
|
||||
const row = (Array.isArray(body?.orgs) ? body.orgs : []).find(o => o?.org === id);
|
||||
const reason = row && row.state !== 'ok' && typeof row.reason === 'string' ? row.reason.trim() : '';
|
||||
if (!reason || /^the bus is not (running|answering)\.?$/i.test(reason)) return null;
|
||||
const refused = /^the bus refused to start: ([\s\S]+)$/.exec(reason);
|
||||
return refused ? { refused: true, text: refused[1] } : { refused: false, text: reason };
|
||||
})
|
||||
.catch(() => null)
|
||||
.then(raw => {
|
||||
downReasons.set(id, { at: Date.now(), raw, pending: false, waiters: [] });
|
||||
for (const [who, callback] of entry.waiters) { try { callback(downLine(raw, who)); } catch { /* one caller's repaint does not stop the others */ } }
|
||||
});
|
||||
return downLine(got?.raw ?? null, name);
|
||||
}
|
||||
// The notice the bus adds to a ready catalogue it picked up after it started (bus runtime.mjs RESTART_TO_LAUNCH).
|
||||
const RESTART_TO_LAUNCH = 'Restart this organization to start agents on these models.';
|
||||
window.MosaicConsole = Object.freeze({ RESTART_TO_LAUNCH, ready, settingsMarkup, busButton, attachBus, orgRestartButton, restartOrg, catalogueCause, orgDownReason, orgRefusal: id => orgReceipts.get(id) ?? null, get busRestart() { return busAllowed(); }, get config() { return config; } });
|
||||
})();
|
||||
+1166
-237
File diff suppressed because it is too large.
Load diff
@@ -14,7 +14,7 @@
|
||||
const value = await response.json();
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) throw new Error('Sign in to the Console again.');
|
||||
if (value.error === 'not-configured') throw new Error(typeof value.reason === 'string' && value.reason ? value.reason : 'The model proxy is not configured for this instance.');
|
||||
if (value.error === 'not-configured') throw new Error(typeof value.reason === 'string' && value.reason ? value.reason : 'The model service is not set up on this instance.');
|
||||
if (value.error === 'login-state-unknown') throw new Error('This sign-in has ended. Start a new sign-in.');
|
||||
if (value.error === 'apikey-invalid') throw new Error('Enter a nonempty API key, with no spaces or line breaks.');
|
||||
throw new Error('The operation could not be confirmed. Nothing was retried. Refresh before trying again.');
|
||||
|
||||
@@ -98,6 +98,17 @@ table.s1-table td code{font-size:12.5px;white-space:nowrap}
|
||||
.console-reconnect>span{flex:1 1 220px;min-width:0;overflow-wrap:anywhere}
|
||||
.bus-restart{display:inline-flex;flex-wrap:wrap;gap:6px 10px;align-items:center;min-width:0}
|
||||
.bus-restart [role=status]:empty{display:none}
|
||||
/* #1873 settings slice 1: a Restart for one named organization (console-restart.js orgRestartButton). */
|
||||
.org-restart{display:inline-flex;flex-wrap:wrap;gap:6px 10px;align-items:center;min-width:0;max-width:100%}
|
||||
.org-restart [data-org-restart]{min-height:var(--action-target);white-space:normal;text-align:left}
|
||||
.org-restart [role=status]{min-width:0;overflow-wrap:anywhere}
|
||||
.org-restart [role=status]:empty{display:none}
|
||||
.org-restart .org-restart-refused strong,.org-restart-refused strong{color:var(--strong)}
|
||||
.org-restart [data-org-restart-detail]{display:block}
|
||||
.routes-catalogue{flex-direction:column;align-items:stretch}
|
||||
.routes-catalogue>p{flex:none}
|
||||
.settings-content .org-down .banner p{margin:0 0 8px;flex-basis:100%}
|
||||
.settings-content .org-down .banner p:last-child{margin-bottom:0}
|
||||
.routes-apply{margin:12px 0 0;padding-left:18px;display:grid;gap:8px}
|
||||
.routes-apply li{overflow-wrap:anywhere}
|
||||
.agent-restart{display:inline-flex;flex-wrap:wrap;gap:6px 10px;align-items:center;min-width:0}
|
||||
@@ -108,7 +119,35 @@ table.s1-table td code{font-size:12.5px;white-space:nowrap}
|
||||
.proxy-setup-next{display:flex;flex-wrap:wrap;align-items:center;gap:8px 16px;margin:0 0 12px}.proxy-setup-next p{margin:0;flex:1 1 280px}
|
||||
.proxy-setup-buttons{display:flex;flex-wrap:wrap;gap:8px}.proxy-setup-buttons button{min-height:44px}
|
||||
#proxy-setup pre{overflow-x:auto;max-width:100%;font-size:12px}#proxy-setup h3{margin:16px 0 4px}#proxy-setup details{margin:0 0 12px}#proxy-setup summary{min-height:44px;display:flex;align-items:center;cursor:pointer}
|
||||
#proxy-setup-receipts{padding-left:20px;overflow-wrap:anywhere;font-size:13px}
|
||||
/* settings-s5b: the setup history (GET /api/proxy/admin/history), the service key and the service's lines. */
|
||||
#proxy-setup-history{list-style:none;padding-left:0;overflow-wrap:anywhere;font-size:13px;margin:0 0 8px}#proxy-setup-history li{padding:2px 0}
|
||||
.proxy-setup-history-foot{display:flex;flex-wrap:wrap;gap:8px;margin:0}.proxy-setup-history-foot button,[data-log] button,#proxy-setup-keymade-retry{min-height:44px}
|
||||
/* settings-s5c: Programs that drive models under Advanced, and Access under Technical details. */
|
||||
.settings-form.proxy-programs{margin-bottom:20px;gap:8px}.proxy-programs-list{list-style:none;margin:8px 0 0;padding:0}.proxy-programs-list>li{padding:10px 0;border-top:1px solid var(--line);min-width:0}
|
||||
.proxy-program-head{display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:8px 12px}.proxy-program-name{font-weight:600;color:var(--strong)}
|
||||
.proxy-program-head button,.proxy-program-confirm button,#proxy-setup-access-retry{min-height:44px}.proxy-program-orgs{margin:4px 0 0;overflow-wrap:anywhere}
|
||||
.proxy-program-confirm{margin:8px 0 0;padding:10px 12px;border:1px solid color-mix(in srgb,var(--danger) 50%,var(--line));border-radius:var(--r);background:color-mix(in srgb,var(--danger) 6%,var(--surface))}.proxy-program-confirm>p:first-child{margin:0 0 8px;overflow-wrap:anywhere}
|
||||
.proxy-program-confirm button.danger{color:var(--danger);border-color:color-mix(in srgb,var(--danger) 60%,var(--line))}
|
||||
.proxy-programs-result{margin:12px 0 0;display:block}.proxy-programs-result[hidden]{display:none}.proxy-programs-result:focus-visible,#proxy-setup-access-title:focus-visible{outline:2px solid var(--focus);outline-offset:2px}.proxy-programs-result>p{margin:0}.proxy-programs-result>.proxy-programs-reread{margin-top:8px}.proxy-programs-result-title{font-weight:600;color:var(--strong)}
|
||||
.proxy-programs-outcomes{margin:6px 0 0;padding-left:18px;overflow-wrap:anywhere}.proxy-programs-outcomes li{padding:2px 0}.proxy-outcome-org{font-weight:600}
|
||||
.proxy-setup-access{margin:12px 0}.proxy-setup-access h4{margin:0 0 2px}.proxy-access li>.badge{justify-self:start}.proxy-setup-access .rows{margin-top:4px}#proxy-setup-access-message:empty{display:none}
|
||||
[data-log] .setup-log{margin:8px 0}[data-log] p{margin:0 0 8px}#proxy-setup-history-message:empty{display:none}
|
||||
/* #1873 settings slice 1 (D3, D4): the kept restart list and Technical details. */
|
||||
.proxy-setup-restart{margin:0 0 12px}
|
||||
.proxy-setup-restart-list{list-style:none;margin:8px 0 0;padding:0;display:grid;gap:8px}
|
||||
.proxy-setup-restart-list li{display:flex;flex-wrap:wrap;align-items:center;gap:6px 12px;padding:10px 12px;border:1px solid var(--line);border-radius:var(--r);min-width:0}
|
||||
.proxy-setup-restart-list li>.small{flex:1 1 220px;min-width:0;overflow-wrap:anywhere}
|
||||
.proxy-setup-restart-name{display:inline-flex;flex-wrap:wrap;align-items:center;gap:6px;min-width:0}
|
||||
.proxy-setup-restart-list li>details{flex:1 1 100%;min-width:0;overflow-wrap:anywhere}
|
||||
.routes-cause-detail,.proxy-setup-cause{margin:6px 0 0}
|
||||
.routes-cause-detail summary,.proxy-setup-cause summary{cursor:pointer}
|
||||
.settings-content button[type=submit].route-save-quiet{background:var(--surface);color:var(--text);border-color:var(--border)}
|
||||
.proxy-setup-tech>summary{font-weight:600}
|
||||
#triage-route-harness-help{display:block;margin-top:4px;max-width:36ch}
|
||||
#triage-route select{max-width:100%}
|
||||
.triage-state-fix{margin:0 0 8px}.triage-fix{white-space:normal}
|
||||
/* #1873 settings slice 1: at phone width a model select takes the whole row, so a long model name is not cut. */
|
||||
@media(max-width:759px){.routing-panel #routes-table td:nth-of-type(2),.routing-panel #routes-table td:nth-of-type(5){grid-column:1/-1}.routing-panel #routes-table select{width:100%;max-width:100%}}
|
||||
@media(max-width:479px){.proxy-setup-state{grid-template-columns:1fr}.proxy-setup-state dd{margin-bottom:6px}}
|
||||
/* Stream S4: which organization this is, in words. Colour (the theme) is never the only cue; the strip appears whenever the theme is not the built-in one. */
|
||||
.org-cue{margin:0 0 12px;padding:6px 10px;border:1px solid var(--line);border-left:4px solid var(--action);border-radius:var(--r-table);background:var(--raised);color:var(--text);font-size:13px;overflow-wrap:anywhere}.skel-rows{display:grid;gap:10px;padding:14px 12px;border:1px solid var(--line);border-radius:var(--r-table);background:var(--surface)}
|
||||
@@ -264,7 +303,10 @@ button:disabled,[aria-disabled=true]{cursor:not-allowed}
|
||||
.settings-content input:disabled,.settings-content select:disabled,.settings-content textarea:disabled{background:var(--raised);border-color:var(--line);color:var(--muted)}
|
||||
.settings-content button,.settings-content summary{min-height:44px;min-width:44px;border:1px solid var(--border);border-radius:var(--r);background:var(--surface);color:var(--text);padding:8px 18px;font:inherit;cursor:pointer}
|
||||
.settings-content button[type=submit]{background:var(--accent-fill);color:var(--on-accent);border-color:var(--accent-fill)}
|
||||
.settings-content :is(button,input,select,textarea,a,summary):focus-visible,#settings-nav a:focus-visible,#settings-disclosure:focus-visible{outline:2px solid var(--focus);outline-offset:2px}
|
||||
/* #1873 settings slice 4: Model setup has at most one primary button, which display.js marks .primary; its other submits read as plain buttons. */
|
||||
.settings-content .model-setup button[type=submit]:not(.primary){background:var(--surface);color:var(--text);border-color:var(--border)}
|
||||
#proxy-signin .form-foot{display:flex;flex-wrap:wrap;align-items:center;gap:var(--gap,12px)}
|
||||
.settings-content :is(button,input,select,textarea,a,summary):focus-visible,#settings-nav a:focus-visible,#settings-nav select:focus-visible,#settings-disclosure:focus-visible{outline:2px solid var(--focus);outline-offset:2px}
|
||||
.settings-segments{display:inline-flex;max-width:100%;border:1px solid var(--border);border-radius:var(--r);overflow:hidden}
|
||||
.settings-segments label{position:relative;display:block}
|
||||
.settings-segments input,.palette-tile input{position:absolute;opacity:0;width:1px;height:1px}
|
||||
@@ -282,6 +324,46 @@ button:disabled,[aria-disabled=true]{cursor:not-allowed}
|
||||
.palette-swatches{display:flex;gap:6px}.palette-swatches i{display:block;width:16px;height:16px;border-radius:50%;background:var(--swatch-canvas);border:1px solid var(--line)}.palette-swatches i:nth-child(2){background:var(--swatch-action);border:0}.palette-swatches i:nth-child(3){background:var(--swatch-accent);border:0}
|
||||
.settings-browser-status{color:var(--success)!important;margin:0!important}.settings-browser-status:not(:empty){margin-top:10px!important}.settings-browser-status.warning{color:var(--warning)!important}
|
||||
.settings-readonly{padding:12px 16px;background:var(--raised);border-radius:var(--r)}
|
||||
/* #1873 settings slice 4: the organization picker in the nav, and Model setup's step cards. */
|
||||
#settings-nav .org-pick{display:flex;flex-direction:column;gap:6px}
|
||||
#settings-nav .org-pick label{font:inherit;color:inherit}
|
||||
#settings-nav .org-pick select{min-height:44px;max-width:100%;font:inherit;font-size:14px;color:var(--text);background:var(--surface);border:1px solid var(--border);border-radius:var(--r);padding:0 10px}
|
||||
.settings-content .panel.setup-step{display:block;margin:0 0 var(--gap,12px)}
|
||||
.setup-step>header{display:flex;align-items:center;gap:var(--gap,12px)}
|
||||
.setup-step>header h3{flex:1;margin:0}
|
||||
.step-n{flex:none;display:inline-grid;place-items:center;width:28px;height:28px;border-radius:50%;border:1px solid var(--border);font-weight:700;color:var(--muted)}
|
||||
.setup-step.done .step-n{border-color:var(--success);color:var(--success)}
|
||||
.setup-step.next .step-n{border-color:var(--action);color:var(--action)}
|
||||
.setup-step.blocked .step-n{border-color:var(--danger);color:var(--danger)}
|
||||
.setup-step .summary{margin:6px 0 0 40px}
|
||||
.setup-step .body{margin:var(--gap,12px) 0 0 40px;min-width:0}
|
||||
.setup-step.waiting{color:var(--muted)}
|
||||
.setup-step.waiting h3{color:var(--muted)}
|
||||
.settings-form{display:grid;gap:var(--pad,16px);max-width:28rem;margin:0}
|
||||
.settings-form .field{display:flex;flex-direction:column;gap:6px;margin:0;min-width:0}
|
||||
.settings-form .field>label,.settings-form legend,.settings-form .lbl{font-weight:600;color:var(--strong)}
|
||||
.settings-form .field :is(input,select){width:100%}
|
||||
.settings-form .w-num{max-width:12rem}
|
||||
.settings-form .help{margin:0}
|
||||
.settings-form .form-foot{display:flex;align-items:center;gap:var(--gap,12px);flex-wrap:wrap}
|
||||
.settings-form fieldset{border:0;margin:0;padding:0;display:grid;gap:8px;min-width:0}
|
||||
.rows{margin:var(--gap,12px) 0 0;padding:0;list-style:none}
|
||||
.rows li{display:grid;grid-template-columns:7rem minmax(0,1fr) auto;gap:6px var(--gap,12px);align-items:center;padding:10px 0;border-top:1px solid var(--line)}
|
||||
.rows li>span:first-child{font-weight:600;color:var(--strong)}
|
||||
.rows li>.account-reason{grid-column:2/-1;margin:0}
|
||||
.rows li.muted{display:block}
|
||||
.progress{list-style:none;margin:var(--gap,12px) 0 0;padding:0;display:grid;gap:6px}
|
||||
.progress li::before{display:inline-block;width:1.4em;color:var(--muted)}
|
||||
.progress .ok::before{content:"✓";color:var(--success)}.progress .run::before{content:"●";color:var(--action)}.progress .todo::before{content:"○"}.progress .bad::before{content:"✕";color:var(--danger)}
|
||||
.mk-note{border-left:3px solid var(--warning);padding-left:10px}
|
||||
.model-list{display:grid;gap:0;margin:0;padding:0;list-style:none}
|
||||
.model-list li{display:flex;flex-wrap:wrap;gap:4px 10px;align-items:center;min-height:44px;padding:4px 0;border-top:1px solid var(--line)}
|
||||
.model-list li .small{margin-left:auto}
|
||||
.setup-log{font:13px/1.5 var(--mono,monospace);white-space:pre-wrap;overflow-wrap:anywhere;margin:var(--gap,12px) 0 0;padding:10px;border:1px solid var(--line);border-radius:var(--r);background:var(--raised)}
|
||||
.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: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}
|
||||
@@ -306,6 +388,19 @@ button:disabled,[aria-disabled=true]{cursor:not-allowed}
|
||||
.settings-content fieldset.harness-control{display:flex;flex-wrap:wrap;align-items:center;gap:6px 12px}
|
||||
.settings-content fieldset.harness-control>legend{flex-basis:100%}
|
||||
.settings-content fieldset.harness-control>:is(button,p){flex-basis:100%}
|
||||
/* #1873 settings slice 5: an organization's one choice (mockup org-models). A choice not built yet is dashed and disabled. */
|
||||
/* settings-s5 review P3-4: the choice cards sit in the form column, as .settings-form does. */
|
||||
.settings-content fieldset.models-source{display:grid;gap:8px;max-width:28rem}
|
||||
.settings-content fieldset.models-source>legend{font-size:14px;color:var(--strong);margin-bottom:8px}
|
||||
.settings-content fieldset.models-source>p{margin:0}
|
||||
.choice{display:grid;grid-template-columns:auto 1fr auto;gap:4px 10px;align-items:start;padding:12px;border:1px solid var(--border);border-radius:var(--r);cursor:pointer}
|
||||
.choice input{margin-top:4px}
|
||||
.choice:has(input:checked){border-color:var(--action);background:var(--raised)}
|
||||
.choice:has(input:disabled){cursor:default}
|
||||
.choice.off{color:var(--muted);cursor:not-allowed;border-style:dashed;background:transparent}
|
||||
.choice strong{color:var(--strong)}.choice.off strong{color:var(--muted)}
|
||||
.choice .small{grid-column:2/4;margin:0}
|
||||
.choice:has(input:focus-visible){outline:2px solid var(--focus);outline-offset:2px}
|
||||
.settings-content :is(.instance-form,.level-form) textarea,.settings-content .assistant-form textarea{display:block;width:100%}
|
||||
.route-definition{display:none}.settings-spinner{display:inline-block;width:16px;height:16px;border:2px solid currentColor;border-right-color:transparent;border-radius:50%;vertical-align:middle;animation:settings-spin .8s linear infinite}@keyframes settings-spin{to{transform:rotate(360deg)}}@media(prefers-reduced-motion:reduce){.settings-spinner{animation:none}}
|
||||
@media(max-width:759px){#routes-table .route-definition{display:inline;float:right;color:var(--muted);font-size:12px;font-weight:400}}
|
||||
@@ -329,7 +424,7 @@ button:disabled,[aria-disabled=true]{cursor:not-allowed}
|
||||
.settings-segments:has(input:focus-visible){outline:2px solid var(--focus);outline-offset:2px}
|
||||
.settings-segments input:focus-visible+span{outline:none}
|
||||
#proxy-authorize:not([hidden]){display:inline-flex;align-items:center;min-height:44px}
|
||||
.settings-content #proxy-accounts .proxy-priority-save{background:var(--surface);color:var(--text);border-color:var(--border)}
|
||||
.settings-content #proxy-order .proxy-priority-save{background:var(--surface);color:var(--text);border-color:var(--border)}
|
||||
@media(min-width:760px){#routes-table select{min-width:132px}}
|
||||
@media(max-width:759px){
|
||||
#agents-table :is(th,td,code){min-width:0;max-width:100%;white-space:normal;overflow-wrap:anywhere;word-break:break-word}
|
||||
@@ -345,9 +440,9 @@ button:disabled,[aria-disabled=true]{cursor:not-allowed}
|
||||
@media (max-width:759px) {
|
||||
.table-wrap:has(.s1-agents), .table-wrap:has(.s1-authority), .table-wrap:has(.s1-creds),
|
||||
.table-wrap:has(#token-usage-table), .table-wrap:has(#token-limits-table),
|
||||
.table-wrap:has(#proxy-accounts), .table-wrap:has(#proxy-keys) { overflow-x:scroll; max-width:100%; }
|
||||
.table-wrap:has(#proxy-keys) { overflow-x:scroll; max-width:100%; }
|
||||
.s1-agents, .s1-authority, .s1-creds, #token-usage-table, #token-limits-table,
|
||||
.table-wrap:has(#proxy-accounts) table, .table-wrap:has(#proxy-keys) table { min-width:650px; }
|
||||
.table-wrap:has(#proxy-keys) table { min-width:650px; }
|
||||
.s1-authority :is(th,td), .s1-creds :is(th,td) { white-space:nowrap; }
|
||||
/* #1873 A2: the root and project agent level grids scroll inside their card. */
|
||||
.table-wrap:has(#agent-levels-root-grid), .table-wrap:has(#agent-levels-project-grid) { overflow-x:scroll; max-width:100%; }
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
const $ = id => document.getElementById(id);
|
||||
const esc = s => String(s).replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]);
|
||||
const PATH = '/api/triage/route';
|
||||
// proxy-scope slice 4 (#1873): Repair access and Turn Triage off (triage-route-http.mjs).
|
||||
const ACCESS_PATH = '/api/triage/access';
|
||||
// The time zones this browser knows, offered as suggestions only; the server decides.
|
||||
const zones = (() => { try { return Intl.supportedValuesOf('timeZone'); } catch { return []; } })();
|
||||
// draft holds the typed fields; base is the revision they were read at, which a save sends, so a
|
||||
@@ -33,11 +35,12 @@
|
||||
|
||||
const models = () => (Array.isArray(data?.choices?.models) ? data.choices.models : []);
|
||||
const harnessesOf = key => models().find(m => m.key === key)?.harnesses ?? [];
|
||||
const savedAt = v => (Number.isFinite(Date.parse(v)) ? window.MosaicLocalTime(v) : 'at an unknown time');
|
||||
|
||||
function stored() {
|
||||
const r = data?.route;
|
||||
if (!r) return `<p id="triage-route-stored"><strong>Not set.</strong> ${esc(data?.routeMessage ?? 'No Triage route is set.')}</p>`;
|
||||
return `<p id="triage-route-stored">Stored route: <strong>${esc(r.model)}</strong> on <strong>${esc(r.harness)}</strong>, cap ${esc(Number(r.cap).toLocaleString('en-US'))} tokens a day, reset ${esc(r.reset)} ${esc(r.timeZone)}. Revision ${esc(r.revision)}, saved ${esc(r.updatedAt)}.</p>`;
|
||||
if (!r) return `<p id="triage-route-stored" data-revision="0"><strong>Not set.</strong> Triage has no model chosen yet, so it will not start.</p>`;
|
||||
return `<p id="triage-route-stored" data-revision="${esc(r.revision)}">Saved route: <strong>${esc(r.model)}</strong>, runs with <strong>${esc(r.harness)}</strong>. Daily token limit ${esc(Number(r.cap).toLocaleString('en-US'))}, resets at ${esc(r.reset)} ${esc(r.timeZone)}. Saved ${esc(savedAt(r.updatedAt))}.</p>`;
|
||||
}
|
||||
|
||||
function form() {
|
||||
@@ -50,16 +53,16 @@
|
||||
+ (d.model && !offered ? `<option value="${esc(d.model)}" selected>${esc(d.model)} (not offered now)</option>` : '')
|
||||
+ list.map(m => `<option value="${esc(m.key)}"${m.key === d.model ? ' selected' : ''}${m.available ? '' : ' disabled'}>${esc(m.label && m.label !== m.key ? `${m.label} (${m.key})` : m.key)}${m.available ? '' : ' (unavailable)'}</option>`).join('');
|
||||
const hs = harnessesOf(d.model);
|
||||
const harnessOptions = `<option value=""${d.harness === '' ? ' selected' : ''}>${d.model ? 'Choose a harness' : 'Choose a model first'}</option>`
|
||||
const harnessOptions = `<option value=""${d.harness === '' ? ' selected' : ''}>${d.model ? 'Choose what runs it' : 'Choose a model first'}</option>`
|
||||
+ (d.harness && !hs.includes(d.harness) ? `<option value="${esc(d.harness)}" selected>${esc(d.harness)} (not offered for this model)</option>` : '')
|
||||
+ hs.map(h => `<option value="${esc(h)}"${h === d.harness ? ' selected' : ''}>${esc(h)}</option>`).join('');
|
||||
const status = data?.choices?.status;
|
||||
const catalogue = status === 'ready' ? '' : `<p class="small warn" id="triage-route-catalogue">No organization bus offered a model catalogue (${esc(status ?? 'unknown')}), so no route can be saved now.</p>`;
|
||||
const catalogue = status === 'ready' ? '' : `<p class="small warn" id="triage-route-catalogue" data-status="${esc(status ?? 'unknown')}">No organization can show your models right now, so no route can be saved. <a href="#/settings/model-setup">Open Model setup</a> to see why each organization can't see your models.</p>`;
|
||||
const conflictButton = conflict ? ' <button type="button" class="btn" id="triage-route-reload">Reload route</button>' : '';
|
||||
return `${catalogue}<form id="triage-route-form" novalidate><div class="filters">`
|
||||
+ `<label>Model <select id="triage-route-model"${off}>${modelOptions}</select></label>`
|
||||
+ `<label>Harness <select id="triage-route-harness"${off}>${harnessOptions}</select></label>`
|
||||
+ `<label>Daily cap, tokens <input id="triage-route-cap" type="number" min="1" step="1" inputmode="numeric" value="${esc(d.cap)}"${off}></label>`
|
||||
+ `<label>Runs with <select id="triage-route-harness" aria-describedby="triage-route-harness-help"${off}>${harnessOptions}</select><span class="small muted" id="triage-route-harness-help">The program that runs the model, named as its module is installed. The list shows only the programs this model runs with.</span></label>`
|
||||
+ `<label>Daily token limit <input id="triage-route-cap" type="number" min="1" step="1" inputmode="numeric" value="${esc(d.cap)}"${off}></label>`
|
||||
+ `<label>Reset <input id="triage-route-reset" value="00:00" readonly aria-readonly="true"></label>`
|
||||
+ `<label>Time zone <input id="triage-route-zone" list="triage-route-zones" autocomplete="off" spellcheck="false" placeholder="Region/City" value="${esc(d.timeZone)}"${off}></label>`
|
||||
+ `</div><datalist id="triage-route-zones">${zones.map(z => `<option value="${esc(z)}"></option>`).join('')}</datalist>`
|
||||
@@ -67,10 +70,47 @@
|
||||
+ `</form>${send ? '' : '<p class="small muted" id="triage-route-readonly">This Console is read-only, so the route cannot be saved here.</p>'}`;
|
||||
}
|
||||
|
||||
// proxy-scope slice 4: the key's state, ids and days only, and the two access buttons.
|
||||
let accessLine = '';
|
||||
function access() {
|
||||
const a = data?.access;
|
||||
if (!a || typeof a !== 'object') return '';
|
||||
const state = a.key === 'issued' ? `Triage key ${esc(a.keyId)} is issued. It rotates on ${esc(a.rotateBy)}.`
|
||||
: a.key === 'missing' ? 'No Triage key is issued, so Triage will not start.'
|
||||
: 'The Triage key file is not safe to use, so Triage will not start.';
|
||||
const warn = [a.rotation === 'rotation overdue' ? 'Key rotation overdue.' : '', a.code === 'triage-no-riding-bus' ? `${a.message}.` : ''].filter(Boolean).join(' ');
|
||||
const off = busy || data?.send !== true ? ' disabled' : '';
|
||||
return `<div id="triage-access" data-key="${esc(a.key)}" data-code="${esc(a.code ?? '')}" data-rotation="${esc(a.rotation ?? '')}"><p class="small">${state}</p>`
|
||||
+ (warn ? `<p class="small warn" id="triage-access-warn">${esc(warn)}</p>` : '')
|
||||
+ `<div class="settings-actions"><button type="button" class="btn" id="triage-access-repair"${off}>Repair access</button> <button type="button" class="btn" id="triage-access-off"${off}>Turn Triage off</button> <span role="status" id="triage-access-line" tabindex="-1">${esc(accessLine)}</span></div></div>`;
|
||||
}
|
||||
|
||||
// Each key action in its own plain sentence (proxy.triage.key answers).
|
||||
const ACTION_LINE = {
|
||||
issued: 'A Triage key was issued.', replaced: 'A new Triage key replaced one the proxy no longer held.',
|
||||
held: 'The Triage key is in place.', revoked: 'Triage is off. Its key was removed.', none: 'Triage is off. No key was held.',
|
||||
};
|
||||
async function accessOp(op) {
|
||||
if (busy || data?.send !== true) return;
|
||||
if (op === 'off' && typeof window.confirm === 'function' && !window.confirm('Turn Triage off? This stops any Triage session and removes its key. Repair access issues a new one.')) return;
|
||||
busy = true; accessLine = op === 'off' ? 'Turning Triage off…' : 'Repairing access…'; repaint();
|
||||
let r, a = null;
|
||||
try { r = await window.fetch(ACCESS_PATH, { cache: 'no-store', redirect: 'manual', method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ op }) }); }
|
||||
catch { busy = false; accessLine = 'Outcome unknown. Reload the page to see the key state.'; repaint(); return; }
|
||||
try { a = await r.json(); } catch { /* refused below */ }
|
||||
busy = false;
|
||||
if (a?.access && data) data = { ...data, access: a.access };
|
||||
if (r.type === 'opaqueredirect') accessLine = 'Sign in again, then retry.';
|
||||
else if (r.ok) accessLine = ACTION_LINE[a?.key?.action] ?? (op === 'off' ? 'Triage is off.' : 'Access was repaired.');
|
||||
else accessLine = typeof a?.message === 'string' ? a.message : 'The Triage key was not changed.';
|
||||
repaint();
|
||||
$('triage-access-line')?.focus?.();
|
||||
}
|
||||
|
||||
function markup() {
|
||||
const body = data === null
|
||||
? `<p class="small warn">${esc(readLine || 'The Triage route could not be read.')}</p><p role="status" id="triage-route-line" tabindex="-1">${esc(line)}</p>`
|
||||
: `${stored()}${form()}<p class="small muted" id="triage-route-note">${esc(data.note ?? 'Saving a route does not start Triage. The key and the wiring are separate.')}</p>`;
|
||||
: `${stored()}${access()}${form()}<p class="small muted" id="triage-route-note">${esc(data.note ?? 'Saving a route does not start Triage. Triage also needs its key.')}</p>`;
|
||||
return `<section class="panel" id="triage-route" aria-labelledby="triage-route-title"><h3 id="triage-route-title">Triage model route</h3>${body}</section>`;
|
||||
}
|
||||
const repaint = () => { const panel = $('triage-route'); if (panel) panel.outerHTML = markup(); };
|
||||
@@ -92,7 +132,7 @@
|
||||
if (r.ok && a?.route) {
|
||||
data = { ...data, route: a.route, revision: a.revision, routeMessage: undefined };
|
||||
draft = fromRoute(a.route); base = a.revision;
|
||||
line = `Saved. Revision ${a.revision}.`;
|
||||
line = `Saved ${savedAt(a.route.updatedAt)}.` + (a.key?.error ? ` ${a.key.message}` : ACTION_LINE[a.key?.action] ? ` ${ACTION_LINE[a.key.action]}` : '');
|
||||
repaint();
|
||||
$('triage-route-line')?.focus?.();
|
||||
return;
|
||||
@@ -126,7 +166,11 @@
|
||||
repaint(); $('triage-route-model')?.focus();
|
||||
});
|
||||
document.addEventListener('submit', e => { if (e.target?.id !== 'triage-route-form') return; e.preventDefault(); save(); });
|
||||
document.addEventListener('click', e => { if (e.target?.id === 'triage-route-reload') reload(); });
|
||||
document.addEventListener('click', e => {
|
||||
if (e.target?.id === 'triage-route-reload') reload();
|
||||
if (e.target?.id === 'triage-access-repair') accessOp('repair');
|
||||
if (e.target?.id === 'triage-access-off') accessOp('off');
|
||||
});
|
||||
|
||||
const hasDraft = () => !!(draft && data && JSON.stringify(draft) !== JSON.stringify(fromRoute(data.route)));
|
||||
window.MosaicTriageRoute = { section, hasDraft };
|
||||
|
||||
@@ -52,7 +52,11 @@
|
||||
}
|
||||
const av = (org, size = '') => `<span class="ws-av ws-o${size ? ` ${size}` : ''}" style="${colours(org)}" aria-hidden="true">${esc(initials(org))}</span>`;
|
||||
const label = org => org.label || org.id;
|
||||
const state = org => (org.invalid ? ['s-error', '▲', 'invalid'] : org.busHealthy ? ['s-working', '●', 'bus running'] : org.busStopped ? ['s-offline', '○', 'bus stopped'] : ['s-offline', '○', 'bus unavailable']);
|
||||
// #1873 settings slice 4: the instance owner's model marks (display.js MosaicModelDots) sit over the
|
||||
// bus state. Can't start always shows; needs restart and paused only on a running bus.
|
||||
const MODEL_MARKS = { cant: ['s-error', '▲', "can't start"], restart: ['s-waiting', '◐', 'needs restart'], paused: ['s-waiting', '◐', 'paused'] };
|
||||
const busState = org => (org.invalid ? ['s-error', '▲', 'invalid'] : org.busHealthy ? ['s-working', '●', 'bus running'] : org.busStopped ? ['s-offline', '○', 'bus stopped'] : ['s-offline', '○', 'bus unavailable']);
|
||||
const state = org => { const mark = org.invalid ? null : window.MosaicModelDots?.get(org.id); return mark === 'cant' || (mark && org.busHealthy) ? MODEL_MARKS[mark] : busState(org); };
|
||||
// A status mark: a glyph plus its word, shown or read aloud only.
|
||||
const dot = (cls, glyph, word, shown = false) => `<span class="ws-dot ${cls}" title="${esc(word)}"><b aria-hidden="true">${glyph}</b>${shown ? esc(word) : `<span class="ws-sr">${esc(word)}</span>`}</span>`;
|
||||
const orgHref = id => `#/orgs/${enc(id)}`;
|
||||
@@ -126,7 +130,7 @@
|
||||
const key = focusKey(root);
|
||||
if (orgError || !orgs) { root.innerHTML = `<hr class="ws-hr"><p class="ws-line">${orgError ? 'Organizations unavailable' : 'Reading organizations…'}</p>`; return; }
|
||||
// SP1: a system route (the dashboard) marks no organization; an organization route marks its own.
|
||||
const here = route(), selected = /^#\/orgs\//.test(location.hash) ? window.MosaicOrg?.id : null;
|
||||
const here = route(), selected = /^#\/orgs\//.test(location.hash) || /^#\/settings\/[^?]+\?(?:[^#]*&)?org=/.test(location.hash) ? window.MosaicOrg?.id : null;
|
||||
const rows = orgs.map(org => {
|
||||
const [cls, glyph, word] = state(org), mine = org.id === selected, current = mine && here === '/organization';
|
||||
return `<li data-workspace-org="${esc(org.id)}"><a class="ws-nav ws-org-row${mine && !current ? ' sel' : ''}" href="${esc(orgHref(org.id))}/organization" data-focus-key="${esc(`org:${org.id}`)}"${current ? ' aria-current="page"' : ''}>${av(org)}<span class="t">${esc(label(org))}</span>${dot(cls, glyph, word, cls !== 's-working')}</a></li>`;
|
||||
@@ -157,6 +161,7 @@
|
||||
});
|
||||
window.addEventListener('hashchange', () => { if (orgs) paint(); });
|
||||
document.addEventListener('mosaic-org-list', e => refresh(e.detail?.rows));
|
||||
document.addEventListener('mosaic-model-dots', () => { if (orgs) paint(); });
|
||||
|
||||
// ---------- organization dashboard (sample 03) ----------
|
||||
const SAFE = /^[a-zA-Z0-9][a-zA-Z0-9_.:/-]{0,159}$/;
|
||||
@@ -200,7 +205,7 @@
|
||||
['ledger', 'Launch limits', !s ? unread : s.launch?.refused ? unread : Number.isFinite(total) ? `${plural(total, 'session')} at once` : 'No session limit', 'org file', 'limits'],
|
||||
['talk', 'Assistant', 'The assistant agent and its seat names', 'org file', 'assistant'],
|
||||
['ledger', 'Token use', 'Usage and limits per provider and model', 'system', 'tokens'],
|
||||
['term', 'Model proxy', 'Accounts, priority and client keys', 'system', 'model-proxy'],
|
||||
['term', 'Model setup', 'Your model service and its accounts', 'system', 'model-setup'],
|
||||
['restart', 'Console', 'Console restart', 'system', 'console'],
|
||||
['business', 'Organizations', orgCount === null ? unread : plural(orgCount, 'organization'), 'system', 'organizations'],
|
||||
['flag', 'About', typeof s?.release === 'string' ? `Release ${s.release}` : 'Address, release and notifier', 'system', 'about'],
|
||||
@@ -307,7 +312,7 @@
|
||||
const bus = restart ? (org.busHealthy ? `<span class="ws-busctl" data-dashboard-bus>${restart}</span>` : '') : `<span data-dashboard-bus><button type="button" class="ws-btn" data-bus-restart-off data-focus-key="bus-restart-off" disabled title="${esc(offTitle)}">${icon('restart')}Restart bus</button> <button type="button" class="ws-btn" data-bus-stop-off data-focus-key="bus-stop-off" disabled title="${esc(stopTitle)}">Stop bus</button></span>`;
|
||||
// SP11 (#1873): the line beside the disabled reload says how a change to the organization file loads.
|
||||
const reload = `<button type="button" class="ws-btn" data-org-reload data-focus-key="org-reload" disabled aria-describedby="${esc(why)}" title="Reloading without a restart is not available yet">${icon('doc')}Reload organization file</button>`;
|
||||
const [bcls, bglyph] = state(org);
|
||||
const [bcls, bglyph] = busState(org);
|
||||
const meta = `<p class="ws-meta"><span>Kind not recorded</span><span>id <code>${esc(id)}</code></span><span>Tracker: ${tracker ? esc(titleCase(tracker)) : 'not set'}</span><span>${dot(bcls, bglyph, org.busHealthy ? `bus running, read ${clock()}` : org.busStopped ? `bus stopped, read ${clock()}` : `bus unavailable, read ${clock()}`, true)}</span><span id="${esc(why)}">The bus reads the organization file when it starts. To load a change to the file, restart the bus.</span></p>`;
|
||||
|
||||
const projectName = pid => projectRows?.find(p => p.id === pid)?.name || pid;
|
||||
|
||||
Loaded 100 of 133 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user