Compare commits
95
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 | ||
|
|
2db4f24fd8 | ||
|
|
9bfe101176 | ||
|
|
869d259036 | ||
|
|
8200ea238c | ||
|
|
196505e6ef | ||
|
|
f7c0e4f46c | ||
|
|
5c975faddd | ||
|
|
462215b4bd | ||
|
|
d0293d6602 | ||
|
|
912138ff61 | ||
|
|
4a8d310cd5 | ||
|
|
c28ca57af6 | ||
|
|
255c1575bb | ||
|
|
0ba50111f6 | ||
|
|
dab07f0ff5 | ||
|
|
561ff77e48 | ||
|
|
e1642be760 | ||
|
|
b23ccdfc12 | ||
|
|
57af33630d | ||
|
|
17b972b92d | ||
|
|
d531d7181d | ||
|
|
67e1ff193d | ||
|
|
9a3004cba4 | ||
|
|
4b833d23e9 | ||
|
|
05be6c62bd | ||
|
|
c9fad00d25 | ||
|
|
4c8527fcd3 | ||
|
|
2d44750b96 | ||
|
|
9343baffff | ||
|
|
38ea639d70 | ||
|
|
fc558df31a | ||
|
|
56cc7b59dd | ||
|
|
c641dbfbc9 | ||
|
|
eb1146ebad | ||
|
|
4b5d027361 | ||
|
|
80ce06851d | ||
|
|
b39d5e2ff3 | ||
|
|
c85c768a4a | ||
|
|
b7649ecd73 | ||
|
|
0354452971 | ||
|
|
bbbefb3f28 | ||
|
|
2f91ff5bea | ||
|
|
1ca3a38f86 | ||
|
|
cb80b2b8a8 | ||
|
|
fd300b4fe3 | ||
|
|
a88302d6eb | ||
|
|
820f45e474 | ||
|
|
2e9a2068bd | ||
|
|
fe6c22e0f2 | ||
|
|
9eed814d3c | ||
|
|
7ee606cda2 | ||
|
|
b2d52b57c1 | ||
|
|
cbf1836bd7 |
No files matched your search
+117
-1
@@ -280,6 +280,11 @@ const canonical = (x) =>
|
||||
typeof x === 'string' && /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$/.test(x) &&
|
||||
Number.isFinite(Date.parse(x)) &&
|
||||
new Date(x).toISOString() === x;
|
||||
// proxy-pickup (#1873): the one plain line a bus without a proxy client gives for not-configured.
|
||||
// The runtime writes it from static text and a refusal code; only that line passes, and never
|
||||
// into refusal evidence.
|
||||
const proxyCause = error => error instanceof BusError && error.code === 'not-configured' && typeof error.reason === 'string' && error.reason.length <= 300 ? error.reason : undefined;
|
||||
|
||||
export class BusError extends Error {
|
||||
// Guidance is static policy text; never quote arguments or file bytes here.
|
||||
constructor(code, reason) {
|
||||
@@ -549,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).
|
||||
@@ -2001,6 +2062,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
|
||||
@@ -6089,7 +6205,7 @@ export class Broker {
|
||||
const args = request.args ?? {};
|
||||
if (!object(args)) fail('invalid-request');
|
||||
return Promise.resolve().then(() => this.#proxyPage(request.verb, args, { business: s.business, human: s.human }))
|
||||
.catch(error => { throw this.#refused(s, new BusError(PROXY_PAGE_CODES.includes(error.code) ? error.code : 'proxy-down'), { verb: request.verb }); });
|
||||
.catch(error => { throw this.#refused(s, new BusError(PROXY_PAGE_CODES.includes(error.code) ? error.code : 'proxy-down', proxyCause(error)), { verb: request.verb }); });
|
||||
}
|
||||
// #1873: the fence clear takes restart evidence, which the runtime re-proves on the host
|
||||
// before the lock clears. Owner only, before every other gate, like the proxy page verbs.
|
||||
|
||||
@@ -6,6 +6,8 @@ import { isDeepStrictEqual } from 'node:util';
|
||||
import { BusError } from './broker.mjs';
|
||||
export const REVOKE_TIMEOUT_MS = 5000;
|
||||
export const REVOKE_POLL_MS = 25;
|
||||
export const ISSUE_PROBE_TIMEOUT_MS = 2000;
|
||||
export const ISSUE_REPROBE_MIN_MS = 250;
|
||||
export const MODEL_STOP_TIMEOUT_MS = 5000;
|
||||
const keyId = key => createHash('sha256').update(key).digest('hex').slice(0, 12);
|
||||
const fail = code => { throw new BusError(code); };
|
||||
@@ -223,9 +225,21 @@ export class ModelKeys {
|
||||
this.#sentinel(keys);
|
||||
return this.#issue(instance, false, keys);
|
||||
});
|
||||
let status;
|
||||
try { status = await this.#client.probeKey(proxy.key); }
|
||||
catch { fail('proxy-down'); }
|
||||
// Every v8 CLIProxyAPI applies a key-list PUT by an async reload after it answers, so a
|
||||
// just-issued key can answer 401 for a moment. On 8.0.79 that moment outlasts the confirming
|
||||
// GET (measured, #1873). Only 401 is probed again, until a short deadline; a re-probe waits
|
||||
// about the time left, at least ISSUE_REPROBE_MIN_MS. Any other non-2xx refuses on the first
|
||||
// answer, as before.
|
||||
const deadline = performance.now() + ISSUE_PROBE_TIMEOUT_MS;
|
||||
let status, first = true;
|
||||
for (;;) {
|
||||
const options = first ? undefined : { timeout: Math.max(ISSUE_REPROBE_MIN_MS, Math.ceil(deadline - performance.now())) };
|
||||
first = false;
|
||||
try { status = await this.#client.probeKey(proxy.key, options); }
|
||||
catch { fail('proxy-down'); }
|
||||
if (status !== 401 || performance.now() >= deadline) break;
|
||||
await new Promise(resolve => setTimeout(resolve, REVOKE_POLL_MS));
|
||||
}
|
||||
if (status < 200 || status >= 300) fail('proxy-key-refused');
|
||||
return proxy;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -32,7 +32,7 @@ import { loadManagementClient } from '../../model-proxy/src/client.mjs';
|
||||
import { ProxyPage } from './proxy-page.mjs';
|
||||
import { createModelCatalogue } from './model-catalogue.mjs';
|
||||
import { sha256 } from '../../model-proxy/src/common.mjs';
|
||||
import { initializeProxy } from '../../model-proxy/src/settings.mjs';
|
||||
import { initializeProxy, loadProxy } from '../../model-proxy/src/settings.mjs';
|
||||
import { createProxyLock } from './proxy-lock.mjs';
|
||||
import { verifyRestart } from '../../model-proxy/src/admin.mjs';
|
||||
import { ModelKeys, modelOrganization, stopModelRuns, MODEL_STOP_TIMEOUT_MS } from './model-keys.mjs';
|
||||
@@ -631,6 +631,7 @@ export async function startBroker({
|
||||
builtinBinding = null,
|
||||
modelCatalogue = null, // in-process fixture seam; boot JSON cannot supply a service
|
||||
makeProviderLoader = createLoader, // in-process fixture seam; boot JSON cannot supply a function
|
||||
initProxy = initializeProxy, // in-process fixture seam (a test install pin); boot JSON cannot supply a function
|
||||
// #1873 A3: the boot's root and project agent copies (cli config.mjs copiesAtBoot), plain data. Every
|
||||
// reload of the organization file below re-applies this snapshot, never the root file.
|
||||
copies = null,
|
||||
@@ -638,6 +639,7 @@ export async function startBroker({
|
||||
// In-process only, like librarySync; production passes none.
|
||||
copyTransport = null,
|
||||
}) {
|
||||
if (typeof initProxy !== 'function') throw new BusError('invalid-host-request');
|
||||
if (copies !== null && (typeof copies !== 'object' || Array.isArray(copies) || typeof copies.org !== 'string'
|
||||
|| !Object.hasOwn(businesses ?? {}, copies.org) || copies.copies === null || typeof copies.copies !== 'object' || Array.isArray(copies.copies)))
|
||||
throw new BusError('invalid-host-request');
|
||||
@@ -835,7 +837,72 @@ export async function startBroker({
|
||||
// records. A daily key quota moved within one agent's lineage keeps its since and its count.
|
||||
const keyLineage=(business,keyId)=>modelKeyLineage(credentials.status(),business,keyId);
|
||||
const settings=organization?launchSettings({...organization,sessions:()=>readSessions(dataRoot),levels,keyLineage}):null;
|
||||
modelCatalogues = modelCatalogue ?? (settings ? createModelCatalogue({ pluginDataRoot:instanceRoot ?? dataRoot, client: () => modelProxyClient, settings: id => settings.current(id).plugins ?? {}, proxyLock }) : null);
|
||||
// #1873 proxy-pickup: a bus started before the instance had a shared proxy picks it up at the
|
||||
// first request that needs one, through the start path's checks, loader and lock. A client got
|
||||
// that way (or by this bus's own proxy.init) is checked again at every such request and dropped
|
||||
// when the start path would now refuse it. A bus started with a proxy keeps its boot client:
|
||||
// its ModelKeys holds that client. `proxyCause` is the one plain line the owner sees while there
|
||||
// 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?.(); }
|
||||
proxyCause = cause;
|
||||
};
|
||||
// The host refuses fixture-proxy-configured at boot when a dev fixture is recorded for the
|
||||
// instance and the organization routes a launch to the fixture (cli dev-fixture.mjs
|
||||
// fixtureLauncherOptions). Anything but a plain absence of the record counts as recorded.
|
||||
const fixtureRecorded = () => { try { lstatSync(join(instanceRoot ?? dataRoot, 'dev-fixture', 'fixture.json')); return true; } catch (error) { return error.code !== 'ENOENT'; } };
|
||||
const usesFixture = id => {
|
||||
try {
|
||||
const doc = settings.current(id), providers = doc.plugins?.['model-provider'];
|
||||
return (Array.isArray(providers) && providers.includes('fixture')) || Object.values(doc.launch?.models ?? {}).some(m => m?.module === 'fixture');
|
||||
} catch { return true; }
|
||||
};
|
||||
async function pickProxy() {
|
||||
if (proxy || !organization || !settings) return;
|
||||
if (org !== null && (!org || typeof org.id !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(org.id) || typeof instanceDataRoot !== 'string' || typeof instanceConfigDir !== 'string'))
|
||||
return dropLateProxy('This bus has no valid organization binding for the shared proxy (invalid-host-request). Restart it from the host.');
|
||||
const configDir = org ? instanceConfigDir : organization.dir;
|
||||
let found;
|
||||
try { found = loadProxy(configDir, instanceRoot ?? dataRoot); }
|
||||
catch (error) { return dropLateProxy(`The model proxy settings do not load (${/^proxy-[a-z-]+$/.test(error?.code ?? '') ? error.code : 'proxy-config-invalid'}). This organization reads no accounts or catalogue until they do.`); }
|
||||
if (found === undefined) return dropLateProxy('The model proxy has not been set up for this instance yet. Set it up on the Model proxy page.');
|
||||
if (modelProxyClient && found.port !== proxyPort) dropLateProxy(null);
|
||||
if (fixtureRecorded() && Object.keys(businesses).some(usesFixture))
|
||||
return dropLateProxy('This organization routes launches to the dev fixture, which the host refuses next to a model proxy (fixture-proxy-configured). Remove the fixture or stop using it here.');
|
||||
let client;
|
||||
try { client = await loadManagementClient({ configDir, proxy: found, receipt: proxyReceipt }); }
|
||||
catch { return dropLateProxy('The model proxy management key does not load (proxy-management-key-invalid). This bus cannot reach the proxy until the key file is fixed.'); }
|
||||
let lock = null;
|
||||
if (org) {
|
||||
try { lock = createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${found.port}` }); }
|
||||
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;
|
||||
// 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?.();
|
||||
}
|
||||
// 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.
|
||||
// pickup-launch: a late client serves accounts and the catalogue, but a launch on it needs what
|
||||
// only the start path builds (ModelKeys here, the proxy in the host launcher's boot system), so
|
||||
// while this bus holds one a ready catalogue says plainly that a restart is needed. Not on an
|
||||
// unavailable one: a fence, a collision or a down proxy still refuses after a restart.
|
||||
const RESTART_TO_LAUNCH = 'Restart this organization to start agents on these models.';
|
||||
const explainCatalogue = value => {
|
||||
if (value?.status === 'unavailable' && value.reason === undefined && !modelProxyClient && proxyCause)
|
||||
return { ...value, reason: 'proxy-not-configured', notices: [proxyCause] };
|
||||
if (lateProxy && value?.status === 'ready')
|
||||
return { ...value, notices: [...(value?.notices ?? []), RESTART_TO_LAUNCH] };
|
||||
return value;
|
||||
};
|
||||
const baseCatalogue = settings ? createModelCatalogue({ pluginDataRoot:instanceRoot ?? dataRoot, client: () => modelProxyClient, settings: id => settings.current(id).plugins ?? {},
|
||||
// A view, so a lock taken by a later pickup or init counts as it does for the start path's.
|
||||
proxyLock: { assertCertain: () => proxyLock?.assertCertain(), generation: () => proxyLock?.generation() } }) : null;
|
||||
modelCatalogues = modelCatalogue ?? (baseCatalogue ? { ...baseCatalogue, read: async (id, options) => { await syncProxy(); return explainCatalogue(await baseCatalogue.read(id, options)); } } : null);
|
||||
// Resolve legacy table entries with the enabled modules' declarations. A
|
||||
// missing or ambiguous mapping never becomes an available route.
|
||||
async function catalogueFor(id, options = {}) {
|
||||
@@ -1340,22 +1407,27 @@ export async function startBroker({
|
||||
// Row 199 (#1697, design section 6): issue, rotate, revoke and delete, live with no restart; each
|
||||
// rewrites or records through the organization writer, so changes.jsonl holds the receipt.
|
||||
proxyPage: async (verb, args, scope) => {
|
||||
if (!proxyPage) throw new BusError('not-configured');
|
||||
await syncProxy();
|
||||
if (!proxyPage) throw new BusError('not-configured', proxyCause ?? undefined);
|
||||
return proxyPage.request(verb, args, scope);
|
||||
},
|
||||
proxyInit: organization ? async args => {
|
||||
const settings = await initializeProxy({ configDir: organization.dir, dataRoot: instanceRoot ?? dataRoot, ...args });
|
||||
modelProxyClient = await loadManagementClient({ configDir: organization.dir, proxy: settings, receipt: proxyReceipt });
|
||||
proxyPort = settings.port;
|
||||
const settings = await initProxy({ configDir: organization.dir, dataRoot: instanceRoot ?? dataRoot, ...args });
|
||||
const client = await loadManagementClient({ configDir: organization.dir, proxy: settings, receipt: proxyReceipt });
|
||||
// #1873: an org bus that initializes the shared proxy also holds its write lock from now on.
|
||||
if (org && !proxyLock) proxyLock = createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${settings.port}` });
|
||||
configureProxyPage();
|
||||
// proxy-pickup: the lock comes first, so a lock that refuses leaves no client behind.
|
||||
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;
|
||||
configureProxyPage(); modelCatalogues?.invalidate?.();
|
||||
return settings;
|
||||
} : null,
|
||||
// #1873: clear the uncertain-write fence only on restart evidence this host re-proves: the
|
||||
// process on the proxy port is new, alive and started after the fence was set.
|
||||
proxyFenceClear: organization ? async ({ evidence }) => {
|
||||
if (!proxyLock || !modelProxyClient) throw new BusError('not-configured');
|
||||
await syncProxy();
|
||||
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,
|
||||
credentialVerbs: organization
|
||||
@@ -1792,6 +1864,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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -11,7 +11,7 @@ import { tmpdir } from 'node:os';
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
import { Credentials } from '../src/credentials.mjs';
|
||||
import { ManagementClient } from '../../model-proxy/src/client.mjs';
|
||||
import { ModelKeys, REVOKE_TIMEOUT_MS, REVOKE_POLL_MS } from '../src/model-keys.mjs';
|
||||
import { ModelKeys, REVOKE_TIMEOUT_MS, REVOKE_POLL_MS, ISSUE_PROBE_TIMEOUT_MS, ISSUE_REPROBE_MIN_MS } from '../src/model-keys.mjs';
|
||||
import { startBroker } from '../src/runtime.mjs';
|
||||
import { busOrganization } from '../src/organization-context.mjs';
|
||||
import { Registry } from '../../seat/src/session.mjs';
|
||||
@@ -42,6 +42,7 @@ async function fixture(t, { delayed = 0 } = {}) {
|
||||
const key = req.headers.authorization?.slice(7);
|
||||
const accepted = state.keys.includes(key) || (state.removed.has(key) && performance.now() - state.removed.get(key) < state.delay);
|
||||
state.onProbe?.(state.status ?? (accepted ? 200 : 401));
|
||||
if (state.stallProbe?.()) return;
|
||||
res.writeHead(state.status ?? (accepted ? 200 : 401)); res.end('{}');
|
||||
});
|
||||
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||
@@ -123,10 +124,33 @@ test('proxy-revoke-revision-conflict keeps local revoking record and ids-only re
|
||||
assert.equal(JSON.stringify(f.receipts).includes(key), false);
|
||||
await f.create().reconcile(); assert.equal(f.credentials.modelRecord('demo/coder') === null, true);
|
||||
});
|
||||
for (const status of [401, 503, 302]) test(`proxy-key-refused: preflight rejects HTTP ${status} with exactly one probe`, async t => {
|
||||
for (const status of [503, 302]) test(`proxy-key-refused: preflight rejects HTTP ${status} with exactly one probe`, async t => {
|
||||
const f = await fixture(t); await f.manager.reconcile(); await f.manager.ensure('demo/coder'); f.state.status = status;
|
||||
await assert.rejects(f.manager.preflight('demo/coder'), failCode('proxy-key-refused')); assert.equal(f.state.probes, 1);
|
||||
});
|
||||
test('proxy-key-refused: a key that keeps answering 401 refuses once the short probe deadline passes', async t => {
|
||||
const f = await fixture(t); await f.manager.reconcile(); await f.manager.ensure('demo/coder'); f.state.status = 401;
|
||||
const started = performance.now();
|
||||
await assert.rejects(f.manager.preflight('demo/coder'), failCode('proxy-key-refused'));
|
||||
const took = performance.now() - started;
|
||||
assert.ok(f.state.probes > 1, 'a 401 is probed again'); assert.ok(took >= ISSUE_PROBE_TIMEOUT_MS && took < ISSUE_PROBE_TIMEOUT_MS + 2000, `bounded: ${took}`);
|
||||
});
|
||||
test('proxy-down: a re-probe that stalls ends near the probe deadline, not the client default', async t => {
|
||||
const f = await fixture(t); await f.manager.reconcile(); await f.manager.ensure('demo/coder'); f.state.status = 401;
|
||||
f.state.stallProbe = () => f.state.probes >= 2;
|
||||
const started = performance.now();
|
||||
await assert.rejects(f.manager.preflight('demo/coder'), failCode('proxy-down'));
|
||||
const took = performance.now() - started;
|
||||
assert.equal(f.state.probes, 2); assert.ok(took < ISSUE_PROBE_TIMEOUT_MS + ISSUE_REPROBE_MIN_MS + 1000, `bounded: ${took}`);
|
||||
});
|
||||
// Every v8 CLIProxyAPI applies the PUT by an async reload; on 8.0.79 a just-issued key answers 401
|
||||
// past the confirming GET (#1873).
|
||||
test('first launch passes when the proxy applies the new key a moment after the PUT answers', async t => {
|
||||
const f = await fixture(t); await f.manager.reconcile(); f.state.status = 401;
|
||||
f.state.onProbe = () => { if (f.state.probes === 3) f.state.status = null; };
|
||||
const answer = await f.manager.preflight('demo/coder');
|
||||
assert.equal(f.state.probes, 3); assert.equal(f.state.keys.includes(answer.key), true);
|
||||
});
|
||||
test('rotate keeps old key while its run lives, new launches use new, exit removes old', async t => {
|
||||
const f = await fixture(t); await f.manager.reconcile(); const first = await f.manager.ensure('demo/coder');
|
||||
f.runs.push({ instance: 'demo/coder', run: 'old', keyId: hash(first.key) });
|
||||
|
||||
@@ -0,0 +1,306 @@
|
||||
// #1873 pickup-launch: a bus that picked up the shared proxy late serves accounts and a ready
|
||||
// catalogue, but a launch on a proxy model still needs a restart (no ModelKeys on the bus, no proxy
|
||||
// in the host launcher's boot system). While it holds such a client, its catalogue answer says so
|
||||
// in one plain notice, and a launch refuses exactly as before. A refused or dropped client shows
|
||||
// its cause instead. Real buses, a real launcher whose model ops go to the bus, a fake loopback
|
||||
// management server; no live config.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import http from 'node:http';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import { startBroker } from '../src/runtime.mjs';
|
||||
import { busOrganization } from '../src/organization-context.mjs';
|
||||
import { createProxyLock } from '../src/proxy-lock.mjs';
|
||||
import { Registry, launchLogFile } 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';
|
||||
import { PIN } from '../../model-proxy/src/common.mjs';
|
||||
import { initializeProxy, installPaths, loadProxy } from '../../model-proxy/src/settings.mjs';
|
||||
import { createLauncher } from '../../cli/src/launcher.mjs';
|
||||
import { initializeControl } from '../../processing-control/src/state.mjs';
|
||||
import { ZAI_URL } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
import { fakeHarnesses, humanEnv, withoutHostCredentials } from '../../cli/tests/helpers.mjs';
|
||||
import { REPO_SKILLS } from '../../harness/tests/helpers.mjs';
|
||||
|
||||
const RESTART = 'Restart this organization to start agents on these models.';
|
||||
const MODEL = 'claude-claude-opus-5-5';
|
||||
const sha = value => createHash('sha256').update(value).digest('hex');
|
||||
|
||||
// A loopback stand-in for the proxy's management API: one ready Claude account with one model,
|
||||
// and an access key list that keeps what a PUT sends, so a boot ModelKeys reconciles against it.
|
||||
async function fakeProxy(t, key, port = 0, { groups = [] } = {}) {
|
||||
const seen = { calls: 0, wrongKey: 0 };
|
||||
let accessKeys = [];
|
||||
const server = http.createServer((req, res) => {
|
||||
seen.calls++;
|
||||
if (req.headers.authorization !== `Bearer ${key}`) { seen.wrongKey++; res.writeHead(401); res.end('{}'); return; }
|
||||
const url = new URL(req.url, 'http://127.0.0.1');
|
||||
if (url.pathname === '/v8/management/config/access/api-keys' && req.method === 'PUT') {
|
||||
const chunks = [];
|
||||
req.on('data', c => chunks.push(c));
|
||||
req.on('end', () => { accessKeys = JSON.parse(Buffer.concat(chunks)); res.end(JSON.stringify({ status: 'ok' })); });
|
||||
return;
|
||||
}
|
||||
const answer = url.pathname === '/v8/management/credentials' ? { files: [{ id: 'acct-1', name: 'acct-1', provider: 'claude', status: 'ready', priority: 1 }] }
|
||||
: url.pathname === '/v8/management/credentials/models' ? { models: [{ id: 'claude-opus-5-5' }] }
|
||||
: url.pathname === '/v8/management/config/api-keys/claude' ? groups
|
||||
: url.pathname === '/v8/management/config/access/api-keys' ? accessKeys
|
||||
: null;
|
||||
if (answer === null) { res.writeHead(404); res.end('{}'); return; }
|
||||
res.end(JSON.stringify(answer));
|
||||
});
|
||||
await new Promise((resolve, reject) => { server.once('error', reject); server.listen(port, '127.0.0.1', resolve); });
|
||||
t.after(async () => { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); });
|
||||
return { port: server.address().port, seen, server };
|
||||
}
|
||||
|
||||
// One instance (config dir + data root) and a fake proxy install whose bytes a test pin names.
|
||||
function instance(t) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'pl-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'instance');
|
||||
fs.mkdirSync(join(configDir, 'secrets', 'agents'), { recursive: true, mode: 0o700 });
|
||||
fs.chmodSync(join(configDir, 'secrets'), 0o700);
|
||||
fs.mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const paths = installPaths(dataRoot);
|
||||
fs.mkdirSync(join(paths.dir, 'static'), { recursive: true, mode: 0o700 });
|
||||
fs.writeFileSync(paths.binary, 'fixture proxy binary\n', { mode: 0o700 });
|
||||
fs.writeFileSync(paths.panel, '<!doctype html>\n', { mode: 0o600 });
|
||||
const pin = { ...PIN, binary: { ...PIN.binary, sha256: sha('fixture proxy binary\n') }, panel: { ...PIN.panel, sha256: sha('<!doctype html>\n') } };
|
||||
const rolesDir = rolesCopy(root), system = systemFor(root);
|
||||
const key = `pl1873-${randomBytes(24).toString('hex')}`;
|
||||
return { root, configDir, dataRoot, paths, pin, rolesDir, system, key };
|
||||
}
|
||||
|
||||
// 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);
|
||||
// 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 });
|
||||
const dataRoot = join(f.root, `${id}-bus`); fs.mkdirSync(dataRoot, { mode: 0o700 }); new Registry(dataRoot);
|
||||
const bus = await startBroker({ dataRoot, businesses: { [id]: busOrganization(business, resolved) },
|
||||
organization: { dir: f.configDir, rolesDir: f.rolesDir, system: f.system },
|
||||
instanceRoot: f.dataRoot, org: { id }, instanceDataRoot: f.dataRoot, instanceConfigDir: f.configDir,
|
||||
initProxy: args => initializeProxy({ ...args, pin: f.pin }), ...binding });
|
||||
t.after(() => bus.close().catch(() => {}));
|
||||
const human = bus.broker.bindHuman({ business: id, human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const ask = (verb, args) => Promise.resolve().then(() => bus.broker.request(human, { verb, args }));
|
||||
return { id, bus, ask, dataRoot };
|
||||
}
|
||||
|
||||
// Every stderr line written while `run` is in flight.
|
||||
async function capture(run) {
|
||||
const write = process.stderr.write, lines = [];
|
||||
process.stderr.write = (chunk, ...rest) => { lines.push(String(chunk)); return write.call(process.stderr, chunk, ...rest); };
|
||||
try { return { value: await run(), lines }; } finally { process.stderr.write = write; }
|
||||
}
|
||||
|
||||
async function initThroughA(t, f, a, options) {
|
||||
const settings = await a.ask('proxy.init', { key: f.key });
|
||||
const proxy = await fakeProxy(t, f.key, settings.port, options);
|
||||
return { settings, proxy };
|
||||
}
|
||||
|
||||
// The owner picks the catalogue's proxy model for the coder on the Routing page, through the bus.
|
||||
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: '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
|
||||
// no proxy. The model ops go to the bus itself; the rest are the launch-routes stub, and
|
||||
// authorizeLaunch stops a launch that passed every check before it.
|
||||
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 });
|
||||
// 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);
|
||||
if (m.op === 'identity') return { business: o.id, role: 'pm' };
|
||||
if (m.op === 'mayLaunch') return { allow: true };
|
||||
if (m.op === 'modelAdmission' || m.op === 'modelPreflight') { const answer = await o.bus[m.op](m); answers.push({ op: m.op, answer }); return answer; }
|
||||
if (m.op === 'authorizeLaunch') throw Object.assign(Error('fixture authorization stop'), { code: 'fixture-policy' });
|
||||
return {};
|
||||
} };
|
||||
const cliproxy = JSON.stringify(pathToFileURL(new URL('../../plugins/model-provider/cliproxy/index.mjs', import.meta.url).pathname).href);
|
||||
// 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 };
|
||||
const launcher = createLauncher({ host, system: config, env, makeLoader, bwrap: '/usr/bin/true', log: () => {} });
|
||||
t.after(() => launcher.close?.().catch?.(() => {}));
|
||||
const events = () => { try { return fs.readFileSync(launchLogFile(o.dataRoot, o.id), 'utf8').trim().split('\n').filter(Boolean).map(l => JSON.parse(l)); } catch { return []; } };
|
||||
return { launch: () => launcher.fromSession('fixture-cap', 'coder'), answers, ops, events };
|
||||
}
|
||||
|
||||
const restartNotices = view => view.catalogue.notices.filter(n => n === RESTART).length;
|
||||
|
||||
test('a bus that picked up the proxy says on its ready catalogue that a restart starts agents on these models, and the launch refuses as before', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
const before = await b.ask('routes');
|
||||
assert.equal(before.catalogue.status, 'unavailable');
|
||||
assert.equal(restartNotices(before), 0, 'no client yet: the not-set-up cause, not the restart notice');
|
||||
await initThroughA(t, f, a);
|
||||
await pickModel(b);
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'ready');
|
||||
assert.equal(restartNotices(view), 1);
|
||||
assert.equal(restartNotices(await b.ask('routes')), 1, 'one notice at every read');
|
||||
// The bus that ran init holds a client it did not boot with too.
|
||||
const own = await a.ask('routes');
|
||||
assert.equal(own.catalogue.status, 'ready');
|
||||
assert.equal(restartNotices(own), 1);
|
||||
// The launch refuses as before, with the existing code and line, before any key or run: the
|
||||
// launcher booted with no proxy sends no proxy transport, and the bus's admission refuses that.
|
||||
const host = await hostLauncher(t, f, b);
|
||||
assert.deepEqual(await host.launch(), { ok: false, error: 'model-provider-unavailable' });
|
||||
assert.deepEqual(host.answers, [{ op: 'modelAdmission', answer: { allow: false, code: 'model-provider-unavailable' } }]);
|
||||
assert.equal(host.ops.includes('authorizeLaunch'), false);
|
||||
assert.equal(host.ops.includes('modelPreflight'), false);
|
||||
const refused = host.events().filter(e => e.event === 'refused');
|
||||
assert.equal(refused.length, 1);
|
||||
assert.equal(refused[0].code, 'model-provider-unavailable');
|
||||
assert.equal(refused[0].why, 'model cliproxy/claude/claude-opus-5-5 unavailable (model-provider-unavailable)');
|
||||
// The bus side has no ModelKeys: a preflight issues no key.
|
||||
assert.equal(await b.bus.modelPreflight({ business: 'beta', role: 'coder' }), null);
|
||||
});
|
||||
|
||||
test('the restart notice is added after the notices the catalogue already carries', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
// A z.ai upstream group with no models: the cliproxy catalogue's own notice.
|
||||
await initThroughA(t, f, a, { groups: [{ 'base-url': ZAI_URL, keys: [{ name: 'group-1' }] }] });
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'ready');
|
||||
assert.deepEqual(view.catalogue.notices, ['z.ai account has no models configured. Add them on Model proxy.', RESTART]);
|
||||
});
|
||||
|
||||
test('a bus started with the proxy shows no restart notice', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha');
|
||||
await initThroughA(t, f, a);
|
||||
const c = await orgBus(t, f, 'gamma', { binding: { proxy: loadProxy(f.configDir, f.dataRoot) } });
|
||||
const view = await c.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'ready', JSON.stringify(view.catalogue));
|
||||
assert.deepEqual(view.catalogue.notices, []);
|
||||
});
|
||||
|
||||
test('an organization with no model provider shows a disabled catalogue with no restart notice (the routes verb answers disabled itself)', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta', { providers: [] });
|
||||
await initThroughA(t, f, a);
|
||||
await b.ask('proxy.accounts', {});
|
||||
assert.ok(b.bus.modelProxy, 'the client is held');
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'disabled');
|
||||
assert.deepEqual(view.catalogue.notices, []);
|
||||
});
|
||||
|
||||
test('a fenced catalogue on a picked-up client reads unavailable with no restart notice, since a restart would not help', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
const { proxy } = await initThroughA(t, f, a);
|
||||
assert.equal(restartNotices(await b.ask('routes')), 1);
|
||||
createProxyLock({ stateDir: join(f.dataRoot, 'proxy'), identity: `${f.configDir}:${proxy.port}` }).fence({ org: 'alpha' });
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'unavailable');
|
||||
assert.ok(b.bus.modelProxy);
|
||||
assert.equal(restartNotices(view), 0);
|
||||
// A bus that booted with the proxy on the same fenced instance, which is what a restart gives,
|
||||
// reads the same unavailable catalogue: the restart notice would have sent the owner nowhere.
|
||||
const c = await orgBus(t, f, 'gamma', { binding: { proxy: loadProxy(f.configDir, f.dataRoot) } });
|
||||
assert.equal((await c.ask('routes')).catalogue.status, 'unavailable');
|
||||
// Once the fence is gone, the ready catalogue says again that a restart is needed.
|
||||
fs.rmSync(join(f.dataRoot, 'proxy', 'mosaic-keys.uncertain'));
|
||||
const after = await b.ask('routes');
|
||||
assert.equal(after.catalogue.status, 'ready', JSON.stringify(after.catalogue));
|
||||
assert.equal(restartNotices(after), 1);
|
||||
});
|
||||
|
||||
test('refused settings still refuse the launch and name their cause, not the restart notice', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
await initThroughA(t, f, a);
|
||||
await pickModel(b);
|
||||
// The management key file stops being private: the start path refuses it.
|
||||
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o644);
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'unavailable');
|
||||
assert.equal(view.catalogue.reason, 'proxy-not-configured');
|
||||
assert.equal(view.catalogue.notices.length, 1);
|
||||
assert.match(view.catalogue.notices[0], /management key does not load/);
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
const host = await hostLauncher(t, f, b);
|
||||
// The bus itself refuses now: the catalogue keeps the model it saw, marked unavailable.
|
||||
assert.deepEqual(await host.launch(), { ok: false, error: 'model-provider-unavailable' });
|
||||
assert.deepEqual(host.answers, [{ op: 'modelAdmission', answer: { allow: false, code: 'model-provider-unavailable' } }]);
|
||||
assert.match(host.events().at(-1).why, /^model cliproxy\/claude\/claude-opus-5-5 unavailable \(model-provider-unavailable\)$/);
|
||||
});
|
||||
|
||||
test('a dropped client refuses the next launch, and the catalogue names the cause instead of the restart notice', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
await initThroughA(t, f, a);
|
||||
await pickModel(b);
|
||||
const host = await hostLauncher(t, f, b);
|
||||
assert.deepEqual(await host.launch(), { ok: false, error: 'model-provider-unavailable' });
|
||||
assert.equal(restartNotices(await b.ask('routes')), 1);
|
||||
fs.rmSync(join(f.configDir, 'proxy.json'));
|
||||
// The next launch refuses with the same code and line.
|
||||
assert.deepEqual(await host.launch(), { ok: false, error: 'model-provider-unavailable' });
|
||||
assert.deepEqual(host.answers.map(a => a.answer), [{ allow: false, code: 'model-provider-unavailable' }, { allow: false, code: 'model-provider-unavailable' }]);
|
||||
assert.equal(host.events().filter(e => e.event === 'refused').length, 2);
|
||||
// The page's next read drops the client and names the cause.
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
assert.equal(view.catalogue.status, 'unavailable');
|
||||
assert.equal(view.catalogue.reason, 'proxy-not-configured');
|
||||
assert.deepEqual(view.catalogue.notices.length, 1);
|
||||
assert.match(view.catalogue.notices[0], /not been set up/);
|
||||
});
|
||||
|
||||
test('the management key is in no stderr line, response, refusal, launch log or file the launch path writes', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
await initThroughA(t, f, a);
|
||||
await pickModel(b);
|
||||
const host = await hostLauncher(t, f, b);
|
||||
const outputs = [];
|
||||
const { lines } = await capture(async () => {
|
||||
outputs.push(await b.ask('routes'));
|
||||
outputs.push(await host.launch());
|
||||
outputs.push(await b.bus.modelPreflight({ business: 'beta', role: 'coder' }));
|
||||
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o644);
|
||||
outputs.push(await b.ask('routes'));
|
||||
outputs.push(await host.launch());
|
||||
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o600);
|
||||
outputs.push(await b.ask('routes'));
|
||||
outputs.push(await host.launch());
|
||||
});
|
||||
outputs.push(host.answers, host.events());
|
||||
assert.equal(lines.join('').includes(f.key), false, 'stderr');
|
||||
assert.equal(JSON.stringify(outputs).includes(f.key), false, 'responses, answers and launch log');
|
||||
const walk = dir => fs.readdirSync(dir, { withFileTypes: true }).flatMap(e => e.isDirectory() ? walk(join(dir, e.name)) : e.isFile() ? [join(dir, e.name)] : []);
|
||||
const files = [...walk(b.dataRoot), ...walk(join(f.dataRoot, 'proxy')).filter(p => !p.endsWith('/config.yaml'))];
|
||||
assert.ok(files.some(p => p === launchLogFile(b.dataRoot, 'beta')), 'the launch log is scanned');
|
||||
for (const file of files) assert.equal(fs.readFileSync(file).includes(f.key), false, file);
|
||||
});
|
||||
@@ -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);
|
||||
});
|
||||
@@ -0,0 +1,371 @@
|
||||
// #1873 proxy-pickup: a running organization bus picks up the shared model proxy that another
|
||||
// organization's bus initialized, with no restart, through the start path's loader and lock. A
|
||||
// bus whose proxy settings the start path would refuse still refuses, and names the cause in one
|
||||
// plain line. Real buses and a fake loopback management server; no live config.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import http from 'node:http';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
import { startBroker } from '../src/runtime.mjs';
|
||||
import { busOrganization } from '../src/organization-context.mjs';
|
||||
import { createProxyLock } from '../src/proxy-lock.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';
|
||||
import { PIN } from '../../model-proxy/src/common.mjs';
|
||||
import { initializeProxy, installPaths } from '../../model-proxy/src/settings.mjs';
|
||||
import { FIXTURE_ENTRY, FIXTURE_KEY_NAME } from '../../plugins/model-provider/fixture/index.mjs';
|
||||
|
||||
const sha = value => createHash('sha256').update(value).digest('hex');
|
||||
|
||||
// A loopback stand-in for the proxy's management API: one ready Claude account with one model.
|
||||
async function fakeProxy(t, key, port = 0) {
|
||||
const seen = { calls: 0, wrongKey: 0 };
|
||||
const server = http.createServer((req, res) => {
|
||||
seen.calls++;
|
||||
if (req.headers.authorization !== `Bearer ${key}`) { seen.wrongKey++; res.writeHead(401); res.end('{}'); return; }
|
||||
const url = new URL(req.url, 'http://127.0.0.1');
|
||||
const answer = url.pathname === '/v8/management/credentials' ? { files: [{ id: 'acct-1', name: 'acct-1', provider: 'claude', status: 'ready', priority: 1 }] }
|
||||
: url.pathname === '/v8/management/credentials/models' ? { models: [{ id: 'claude-opus-5-5' }] }
|
||||
: url.pathname === '/v8/management/config/api-keys/claude' ? []
|
||||
: url.pathname === '/v8/management/config/access/api-keys' ? []
|
||||
: null;
|
||||
if (answer === null) { res.writeHead(404); res.end('{}'); return; }
|
||||
res.end(JSON.stringify(answer));
|
||||
});
|
||||
await new Promise((resolve, reject) => { server.once('error', reject); server.listen(port, '127.0.0.1', resolve); });
|
||||
t.after(async () => { server.closeAllConnections(); await new Promise(resolve => server.close(resolve)); });
|
||||
return { port: server.address().port, seen, server };
|
||||
}
|
||||
|
||||
// One instance (config dir + data root) and a fake proxy install whose bytes a test pin names.
|
||||
// The pin keeps the shipped version, so the pinned binary path is the one loadProxy accepts.
|
||||
function instance(t) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'pk-'));
|
||||
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'instance');
|
||||
fs.mkdirSync(join(configDir, 'secrets', 'agents'), { recursive: true, mode: 0o700 });
|
||||
fs.chmodSync(join(configDir, 'secrets'), 0o700);
|
||||
fs.mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const paths = installPaths(dataRoot);
|
||||
fs.mkdirSync(join(paths.dir, 'static'), { recursive: true, mode: 0o700 });
|
||||
fs.writeFileSync(paths.binary, 'fixture proxy binary\n', { mode: 0o700 });
|
||||
fs.writeFileSync(paths.panel, '<!doctype html>\n', { mode: 0o600 });
|
||||
const pin = { ...PIN, binary: { ...PIN.binary, sha256: sha('fixture proxy binary\n') }, panel: { ...PIN.panel, sha256: sha('<!doctype html>\n') } };
|
||||
const rolesDir = rolesCopy(root), system = systemFor(root);
|
||||
const key = `pk1873-${randomBytes(24).toString('hex')}`;
|
||||
return { root, configDir, dataRoot, paths, pin, rolesDir, system, key, closers: [] };
|
||||
}
|
||||
|
||||
// A real organization bus on the instance, started with no proxy (as cli bootConfig does when the
|
||||
// instance has no proxy.json at boot).
|
||||
async function orgBus(t, f, id, { providers = ['cliproxy'], edit = () => {}, org = { id }, binding = {} } = {}) {
|
||||
const doc = businessDoc(f.root, id);
|
||||
doc.plugins = { harness: ['pi'], 'model-provider': providers };
|
||||
edit(doc);
|
||||
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 });
|
||||
const dataRoot = join(f.root, `${id}-bus`); fs.mkdirSync(dataRoot, { mode: 0o700 }); new Registry(dataRoot);
|
||||
const bus = await startBroker({ dataRoot, businesses: { [id]: busOrganization(business, resolved) },
|
||||
organization: { dir: f.configDir, rolesDir: f.rolesDir, system: f.system },
|
||||
instanceRoot: f.dataRoot, org, instanceDataRoot: f.dataRoot, instanceConfigDir: f.configDir,
|
||||
initProxy: args => initializeProxy({ ...args, pin: f.pin }), ...binding });
|
||||
t.after(() => bus.close().catch(() => {}));
|
||||
const human = bus.broker.bindHuman({ business: id, human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const ask = (verb, args) => Promise.resolve().then(() => bus.broker.request(human, { verb, args }));
|
||||
return { bus, ask, dataRoot };
|
||||
}
|
||||
|
||||
// Every stderr line written while `run` is in flight.
|
||||
async function capture(run) {
|
||||
const write = process.stderr.write, lines = [];
|
||||
process.stderr.write = (chunk, ...rest) => { lines.push(String(chunk)); return write.call(process.stderr, chunk, ...rest); };
|
||||
try { return { value: await run(), lines }; } finally { process.stderr.write = write; }
|
||||
}
|
||||
|
||||
// The proxy init through bus A writes proxy.json and the key, then the proxy starts on that port.
|
||||
async function initThroughA(t, f, a) {
|
||||
const settings = await a.ask('proxy.init', { key: f.key });
|
||||
const proxy = await fakeProxy(t, f.key, settings.port);
|
||||
return { settings, proxy };
|
||||
}
|
||||
|
||||
const notConfigured = (pattern) => e => e.code === 'not-configured' && typeof e.reason === 'string' && pattern.test(e.reason) && e.reason.length <= 300;
|
||||
|
||||
test('a running organization bus answers accounts and a cliproxy catalogue once another bus initialized the shared proxy, with no restart', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
// Before init: bus B has nothing to pick up, and says so.
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/not been set up/));
|
||||
const before = await b.ask('routes');
|
||||
assert.equal(before.catalogue.status, 'unavailable');
|
||||
assert.equal(before.catalogue.reason, 'proxy-not-configured');
|
||||
assert.equal(before.catalogue.notices.length, 1);
|
||||
const { proxy } = await initThroughA(t, f, a);
|
||||
// Bus A holds the client from its own init; bus B picks it up at its next read.
|
||||
assert.deepEqual((await a.ask('proxy.accounts', {})).accounts.map(r => r.provider), ['claude']);
|
||||
const accounts = await b.ask('proxy.accounts', {});
|
||||
assert.deepEqual(accounts.accounts.map(r => [r.provider, r.status]), [['claude', 'ready']]);
|
||||
const after = await b.ask('routes');
|
||||
assert.equal(after.catalogue.status, 'ready');
|
||||
assert.deepEqual(after.catalogue.models.map(m => m.key), ['claude-claude-opus-5-5']);
|
||||
assert.equal(after.catalogue.reason, undefined);
|
||||
assert.equal(proxy.seen.wrongKey, 0);
|
||||
// Bus B reached the proxy through the same lock identity the start path uses.
|
||||
assert.ok(createProxyLock({ stateDir: join(f.dataRoot, 'proxy'), identity: `${f.configDir}:${proxy.port}` }));
|
||||
});
|
||||
|
||||
test('a bus started before the proxy existed and asked only for a catalogue picks the proxy up', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
assert.equal((await b.ask('routes')).catalogue.status, 'unavailable');
|
||||
await initThroughA(t, f, a);
|
||||
assert.equal((await b.ask('routes')).catalogue.status, 'ready');
|
||||
});
|
||||
|
||||
// Each case writes the shared proxy's files by hand into a state the start path refuses.
|
||||
const refusedStates = [
|
||||
['proxy.json names an unpinned binary', /settings do not load/, f => writeJson(join(f.configDir, 'proxy.json'), { port: 40999, binary: '/elsewhere/cli-proxy-api' })],
|
||||
['proxy.json is not JSON', /settings do not load/, f => fs.writeFileSync(join(f.configDir, 'proxy.json'), '{', { mode: 0o600 })],
|
||||
['the management key file is readable by others', /management key does not load/, f => {
|
||||
writeJson(join(f.configDir, 'proxy.json'), { port: 40999, binary: f.paths.binary });
|
||||
fs.writeFileSync(join(f.configDir, 'secrets', 'proxy-management.key'), f.key, { mode: 0o644 });
|
||||
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o644);
|
||||
}],
|
||||
['the management key file is missing', /management key does not load/, f => writeJson(join(f.configDir, 'proxy.json'), { port: 40999, binary: f.paths.binary })],
|
||||
['the proxy state names another port', /proxy lock/, f => {
|
||||
writeJson(join(f.configDir, 'proxy.json'), { port: 40999, binary: f.paths.binary });
|
||||
fs.writeFileSync(join(f.configDir, 'secrets', 'proxy-management.key'), f.key, { mode: 0o600 });
|
||||
fs.mkdirSync(join(f.dataRoot, 'proxy'), { mode: 0o700 });
|
||||
createProxyLock({ stateDir: join(f.dataRoot, 'proxy'), identity: `${f.configDir}:40998` });
|
||||
}],
|
||||
['the proxy state directory is open to others', /proxy lock/, f => {
|
||||
writeJson(join(f.configDir, 'proxy.json'), { port: 40999, binary: f.paths.binary });
|
||||
fs.writeFileSync(join(f.configDir, 'secrets', 'proxy-management.key'), f.key, { mode: 0o600 });
|
||||
fs.mkdirSync(join(f.dataRoot, 'proxy'), { mode: 0o755 }); fs.chmodSync(join(f.dataRoot, 'proxy'), 0o755);
|
||||
}],
|
||||
];
|
||||
for (const [name, cause, arrange] of refusedStates) test(`no pickup when ${name}: accounts and catalogue refuse with the cause`, async t => {
|
||||
const f = instance(t);
|
||||
const b = await orgBus(t, f, 'beta');
|
||||
arrange(f);
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(cause));
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'unavailable');
|
||||
assert.equal(view.catalogue.reason, 'proxy-not-configured');
|
||||
assert.match(view.catalogue.notices[0], cause);
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
// The host refuses fixture-proxy-configured only when a dev fixture is recorded for the instance.
|
||||
const recordFixture = f => {
|
||||
fs.mkdirSync(join(f.dataRoot, 'dev-fixture'), { mode: 0o700 });
|
||||
fs.writeFileSync(join(f.dataRoot, 'dev-fixture', 'fixture.json'), '{}\n', { mode: 0o600 });
|
||||
};
|
||||
|
||||
test('no pickup for an organization on the fixture model provider, which the host refuses with a proxy', async t => {
|
||||
const f = instance(t);
|
||||
recordFixture(f);
|
||||
const a = await orgBus(t, f, 'alpha');
|
||||
const b = await orgBus(t, f, 'beta', { providers: ['fixture', 'cliproxy'], edit: doc => { doc.launch.models[FIXTURE_KEY_NAME] = { ...FIXTURE_ENTRY }; } });
|
||||
await initThroughA(t, f, a);
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/fixture-proxy-configured/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.reason, 'proxy-not-configured');
|
||||
assert.match(view.catalogue.notices[0], /fixture/);
|
||||
});
|
||||
|
||||
test('no pickup for an organization whose launch table names the fixture module only', async t => {
|
||||
const f = instance(t);
|
||||
recordFixture(f);
|
||||
const a = await orgBus(t, f, 'alpha');
|
||||
const b = await orgBus(t, f, 'beta', { edit: doc => { doc.launch.models[FIXTURE_KEY_NAME] = { ...FIXTURE_ENTRY }; } });
|
||||
await initThroughA(t, f, a);
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/fixture-proxy-configured/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('an organization that lists the fixture still picks the proxy up when no fixture is recorded, as the host would start it', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha');
|
||||
const b = await orgBus(t, f, 'beta', { providers: ['fixture', 'cliproxy'] });
|
||||
await initThroughA(t, f, a);
|
||||
await b.ask('proxy.accounts', {});
|
||||
assert.ok(b.bus.modelProxy);
|
||||
// A fixture recorded later drops the client at the next request.
|
||||
recordFixture(f);
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/fixture-proxy-configured/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('a picked-up client is dropped when proxy.json goes away, its port changes, or the key stops loading', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
const { settings } = await initThroughA(t, f, a);
|
||||
await b.ask('proxy.accounts', {});
|
||||
assert.ok(b.bus.modelProxy);
|
||||
const file = join(f.configDir, 'proxy.json'), keyFile = join(f.configDir, 'secrets', 'proxy-management.key');
|
||||
const saved = fs.readFileSync(file);
|
||||
// Port change: the lock identity is bound to the port, so the bus refuses until a restart.
|
||||
writeJson(file, { ...settings, port: settings.port + 1 === 8317 ? settings.port + 2 : settings.port + 1 });
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/proxy lock/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
assert.equal((await b.ask('routes')).catalogue.status, 'unavailable');
|
||||
// Back to the original port, the bus picks it up again.
|
||||
fs.writeFileSync(file, saved);
|
||||
await b.ask('proxy.accounts', {});
|
||||
assert.ok(b.bus.modelProxy);
|
||||
// Key file no longer 0600.
|
||||
fs.chmodSync(keyFile, 0o640);
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/management key does not load/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
fs.chmodSync(keyFile, 0o600);
|
||||
await b.ask('proxy.accounts', {});
|
||||
// proxy.json removed.
|
||||
fs.rmSync(file);
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/not been set up/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'unavailable');
|
||||
assert.equal(view.catalogue.reason, 'proxy-not-configured');
|
||||
});
|
||||
|
||||
test('a default-instance bus (no organization binding, no lock) follows a port change to the new client, never the old one', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), d = await orgBus(t, f, 'delta', { org: null });
|
||||
const { settings, proxy } = await initThroughA(t, f, a);
|
||||
assert.deepEqual((await d.ask('proxy.accounts', {})).accounts.map(r => r.provider), ['claude']);
|
||||
const old = d.bus.modelProxy;
|
||||
// A listener on the new port that answers nothing to the right key: reaching it proves the move.
|
||||
const moved = await fakeProxy(t, `other-${f.key.slice(0, 6)}`);
|
||||
writeJson(join(f.configDir, 'proxy.json'), { ...settings, port: moved.port });
|
||||
const before = proxy.seen.calls;
|
||||
await assert.rejects(d.ask('proxy.accounts', {}));
|
||||
assert.ok(d.bus.modelProxy && d.bus.modelProxy !== old);
|
||||
assert.ok(moved.seen.calls > 0);
|
||||
assert.equal(proxy.seen.calls, before);
|
||||
});
|
||||
|
||||
test('a picked-up bus reads its catalogue through the shared lock: a fence makes it unavailable', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
const { proxy } = await initThroughA(t, f, a);
|
||||
assert.equal((await b.ask('routes')).catalogue.status, 'ready');
|
||||
createProxyLock({ stateDir: join(f.dataRoot, 'proxy'), identity: `${f.configDir}:${proxy.port}` }).fence({ org: 'alpha' });
|
||||
const view = await b.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'unavailable');
|
||||
assert.ok(b.bus.modelProxy, 'the client stays; the fence alone holds the catalogue');
|
||||
});
|
||||
|
||||
test('a fixture record path in any state but absent counts as recorded', async t => {
|
||||
const f = instance(t);
|
||||
fs.writeFileSync(join(f.dataRoot, 'dev-fixture'), 'not a directory\n', { mode: 0o600 });
|
||||
const a = await orgBus(t, f, 'alpha');
|
||||
const b = await orgBus(t, f, 'beta', { providers: ['fixture', 'cliproxy'] });
|
||||
await initThroughA(t, f, a);
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/fixture-proxy-configured/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('with a fixture recorded, an organization document that no longer validates counts as using it', async t => {
|
||||
const f = instance(t);
|
||||
recordFixture(f);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
await initThroughA(t, f, a);
|
||||
fs.writeFileSync(join(f.configDir, 'businesses', 'beta.json'), '{', { mode: 0o600 });
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/fixture-proxy-configured/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('the bus that ran init also drops its client when the key stops loading', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha');
|
||||
await initThroughA(t, f, a);
|
||||
await a.ask('proxy.accounts', {});
|
||||
fs.rmSync(join(f.configDir, 'secrets', 'proxy-management.key'));
|
||||
await assert.rejects(a.ask('proxy.accounts', {}), notConfigured(/management key does not load/));
|
||||
assert.equal(a.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('an init whose lock refuses leaves the init bus holding no client: no ready catalogue without the lock', async t => {
|
||||
const f = instance(t);
|
||||
// The real init, then the proxy state dir is left readable by the group, which the lock refuses.
|
||||
const initProxy = async args => { const settings = await initializeProxy({ ...args, pin: f.pin });
|
||||
fs.mkdirSync(join(f.dataRoot, 'proxy'), { recursive: true }); fs.chmodSync(join(f.dataRoot, 'proxy'), 0o750); return settings; };
|
||||
const a = await orgBus(t, f, 'alpha', { binding: { initProxy } });
|
||||
await assert.rejects(a.ask('proxy.init', { key: f.key }));
|
||||
await fakeProxy(t, f.key, JSON.parse(fs.readFileSync(join(f.configDir, 'proxy.json'), 'utf8')).port);
|
||||
assert.equal(a.bus.modelProxy, null);
|
||||
const view = await a.ask('routes');
|
||||
assert.equal(view.catalogue.status, 'unavailable');
|
||||
assert.match(view.catalogue.notices[0], /proxy-lock-invalid/);
|
||||
assert.equal(a.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('a default-instance bus (no lock generation) refreshes its catalogue at once on pickup and on drop', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), d = await orgBus(t, f, 'delta', { org: null });
|
||||
// An unavailable catalogue read before init must not be served after the pickup.
|
||||
assert.equal((await d.ask('routes')).catalogue.status, 'unavailable');
|
||||
await initThroughA(t, f, a);
|
||||
assert.equal((await d.ask('routes')).catalogue.status, 'ready');
|
||||
// A ready catalogue read through the picked-up client must not be served after the drop.
|
||||
fs.rmSync(join(f.configDir, 'proxy.json'));
|
||||
const gone = await d.ask('routes');
|
||||
assert.equal(gone.catalogue.status, 'unavailable');
|
||||
assert.equal(gone.catalogue.reason, 'proxy-not-configured');
|
||||
assert.equal(d.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('a picked-up bus clears a proxy fence through the same client and lock', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
await initThroughA(t, f, a);
|
||||
// proxy.fence.clear on a bus that has not read anything yet still picks the proxy up first; the
|
||||
// restart evidence check is the existing one and refuses this empty evidence.
|
||||
await assert.rejects(b.ask('proxy.fence.clear', { evidence: {} }));
|
||||
assert.ok(b.bus.modelProxy);
|
||||
});
|
||||
|
||||
test('a bus serving an invalid organization id never picks up a proxy', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha');
|
||||
await initThroughA(t, f, a);
|
||||
let b;
|
||||
try { b = await orgBus(t, f, 'beta', { binding: { instanceConfigDir: 42 } }); }
|
||||
catch (error) { assert.equal(error.code, 'invalid-host-request'); return; }
|
||||
await assert.rejects(b.ask('proxy.accounts', {}), notConfigured(/organization binding/));
|
||||
assert.equal(b.bus.modelProxy, null);
|
||||
});
|
||||
|
||||
test('the management key is in no stderr line, response, refusal, event or file the pickup path writes', async t => {
|
||||
const f = instance(t);
|
||||
const a = await orgBus(t, f, 'alpha'), b = await orgBus(t, f, 'beta');
|
||||
await initThroughA(t, f, a);
|
||||
const outputs = [];
|
||||
const { lines } = await capture(async () => {
|
||||
outputs.push(await b.ask('proxy.accounts', {}));
|
||||
outputs.push(await b.ask('routes'));
|
||||
// A refusal after the key stops loading, and the pickup after it loads again.
|
||||
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o644);
|
||||
await b.ask('proxy.accounts', {}).catch(error => outputs.push({ code: error.code, reason: error.reason, message: error.message, next: error.next }));
|
||||
outputs.push(await b.ask('routes'));
|
||||
fs.chmodSync(join(f.configDir, 'secrets', 'proxy-management.key'), 0o600);
|
||||
outputs.push(await b.ask('proxy.accounts', {}));
|
||||
});
|
||||
assert.ok(lines.some(line => line.startsWith('model-proxy: ')), 'the pickup client writes its usual receipts');
|
||||
assert.equal(lines.join('').includes(f.key), false, 'stderr');
|
||||
assert.equal(JSON.stringify(outputs).includes(f.key), false, 'responses');
|
||||
// Every file under bus B's data root (store, WAL, sessions) and the proxy state directory,
|
||||
// except the proxy's own config.yaml that init writes for the proxy process.
|
||||
const walk = dir => fs.readdirSync(dir, { withFileTypes: true }).flatMap(e => e.isDirectory() ? walk(join(dir, e.name)) : e.isFile() ? [join(dir, e.name)] : []);
|
||||
const files = [...walk(b.dataRoot), ...walk(join(f.dataRoot, 'proxy')).filter(p => !p.endsWith('/config.yaml'))];
|
||||
assert.ok(files.length > 0);
|
||||
for (const file of files) assert.equal(fs.readFileSync(file).includes(f.key), false, file);
|
||||
});
|
||||
@@ -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);
|
||||
});
|
||||
@@ -1425,3 +1425,100 @@ installer writes `<dataRoot>/.deployed` as mode 0444 naming the instance.
|
||||
`webui-service.sh` refuses accounts and deployed instances because its user
|
||||
manager runs under a human or development seat uid. The receipt uid identifies
|
||||
the invoking process; it does not attribute a human or resist tampering by that uid.
|
||||
|
||||
## Triage route (#1873, held for Jason's ruling)
|
||||
|
||||
`mosaic triage route show` prints the Triage route the instance owner set, or
|
||||
`triage route: not set` (exit 0). `mosaic triage route set --model KEY
|
||||
--harness NAME --cap TOKENS --time-zone ZONE [--revision N]` writes it. Both
|
||||
refuse inside an agent run before anything is read (exit 3).
|
||||
|
||||
The file is `<dataRoot>/triage/route.json`
|
||||
(packages/webui/src/triage-route-store.mjs): a catalogue model key, a harness
|
||||
name and a daily cap leaf `{limit, since, reset, timeZone}`. It holds no env
|
||||
value and no key. The day resets at 00:00 in the zone given, which has no
|
||||
default; an unknown zone refuses. The first cap counts from the time it is
|
||||
set; a later change keeps that `since`, as an organization's token limits do,
|
||||
so lowering the cap never drops tokens already spent. The directory is 0700 and the file 0600,
|
||||
owned by the running user; a link, a non-file, a wrong mode or owner, or an
|
||||
unknown field refuses (exit 3). A write is compare-and-set on `revision`
|
||||
(`--revision 0`: only when nothing is set), atomic, and appends one line with
|
||||
the old and new values to `route-receipts.jsonl`. A write that died leaves
|
||||
`route.lock`; the next write refuses and names it, and you remove it by hand.
|
||||
If the route changed but its receipt line could not be written, the write
|
||||
refuses with `triage-route-unrecorded` (exit 3) and names the new revision.
|
||||
`set` checks the harness against packages/plugins/harness/; it does not check
|
||||
the model key against a catalogue yet.
|
||||
|
||||
The Console does not read this file: the Triage host keeps `route: null` until
|
||||
the wiring slice.
|
||||
|
||||
## Triage key (#1873)
|
||||
|
||||
`mosaic triage key issue|rotate|retire|status` manage Triage's own model
|
||||
proxy client key. Every verb refuses inside an agent run before anything is
|
||||
read (exit 3), and takes no options.
|
||||
|
||||
The key is `mst-<tag>-triage-<43 base64url>`, where `<tag>` is the first 12
|
||||
hex of sha256 of the config directory, the tag on the instance's own `msk-`
|
||||
sentinel. An org bus reconciles only its `mso-<tag>-<org>-key-` names and a
|
||||
no-org bus only its `msk-<tag>-` names, so neither removes it. The file is
|
||||
`<configDir>/secrets/triage/model`: `{version, key, keyId, issuedAt,
|
||||
rotateBy, retiring}`. The directory is 0700 and the file 0600, owned by the
|
||||
running user. A link, a non-file, a second hard link, a wrong mode or owner, a
|
||||
file over 64 KiB, or a record that does not match the shape refuses every verb
|
||||
with `triage-key-unreadable` (exit 3).
|
||||
|
||||
- `issue` adds one key and writes the file. It refuses `triage-key-issued`
|
||||
when a key is already issued; that is `rotate`.
|
||||
- `rotate` adds a new key and moves the old one to `retiring`, with a retire
|
||||
time of now plus `rotateGraceMinutes` from proxy.json, where the bus reads
|
||||
it (default 120). `rotateBy` is 30 days on, as for an org key. An old key
|
||||
the proxy no longer holds is dropped at once.
|
||||
- `retire` removes retiring keys whose grace has passed or that the proxy no
|
||||
longer holds. It changes nothing before then.
|
||||
|
||||
Nothing runs on a timer. The retire time is the earliest a retiring key may
|
||||
go; the key stays live in the proxy until someone runs `retire` (or a later
|
||||
`rotate` or `issue`) after it. `rotateBy` works the same way: `status` marks
|
||||
it due, and only `rotate` replaces the key.
|
||||
- `status` prints whether a key is issued, its key id, its age, its rotate-by
|
||||
day, each retiring key, what the proxy list holds, a set fence, any key in
|
||||
the Triage namespace the file does not hold, and the next command. It takes
|
||||
no lock and writes nothing.
|
||||
|
||||
`issue`, `rotate` and `retire` hold the instance proxy lock that org buses
|
||||
take (`<dataRoot>/proxy`). Each reads the list, refuses without this
|
||||
instance's sentinel (`triage-key-proxy-open`), reads it again and refuses if
|
||||
it moved (`triage-key-raced`), sets the fence, writes, reads back and clears
|
||||
the fence. The proxy changes first and the file second. Each run that
|
||||
changes something appends one line of key ids, never keys, to
|
||||
`<dataRoot>/proxy/mosaic-keys.receipts.jsonl`. That file is checked under the
|
||||
lock before anything changes; an unsafe one refuses
|
||||
`triage-key-receipts-unsafe`. A receipt line that still fails after the change
|
||||
refuses `triage-key-unrecorded`. No output, error or receipt holds a key.
|
||||
|
||||
The file is written to a 0600 temp file in the same directory, synced, renamed
|
||||
into place, and the directory is synced. A failed directory sync after the
|
||||
rename refuses `triage-key-unsynced`: the file is in place but may not survive
|
||||
a power loss. `proxy-lock-busy` means another proxy writer holds the lock; if
|
||||
none is running, `mosaic proxy fence status` shows the lock state.
|
||||
|
||||
After a crash:
|
||||
|
||||
- Killed after the proxy write and before the read-back: the fence stays set
|
||||
and every proxy writer on the instance refuses `proxy-uncertain`. `status`
|
||||
shows the fence. Restart the proxy, run `mosaic proxy fence clear
|
||||
--proxy-restarted`, then run the verb again.
|
||||
- Killed after the read-back and before the file write: the proxy holds a key
|
||||
the file does not. `status` names it; the next `issue`, `rotate` or `retire`
|
||||
removes it. A file that could not be written refuses
|
||||
`triage-key-unwritten` and says to run the verb again.
|
||||
- Killed after the temp file is written and before its rename: a
|
||||
`.model.<pid>.<hex>.tmp` file that may hold a key stays in the triage
|
||||
directory. `status` counts it; the next `issue`, `rotate` or `retire`
|
||||
removes it under the lock.
|
||||
- A `retire` killed after the proxy write leaves a retiring key the proxy no
|
||||
longer holds. `status` marks it; the next `retire` drops it from the file.
|
||||
|
||||
Nothing gives the key to the Triage host yet; that is the wiring slice.
|
||||
@@ -56,6 +56,7 @@ import { ORG_CONFIG, runConfigCommand } from "../../organization/src/config-comm
|
||||
import { devCommand, DEV_USAGE, fixtureLaunchEnv, fixtureLauncherOptions } from "./dev-fixture.mjs";
|
||||
import { runCommand, RUN_USAGE } from "./run-evidence.mjs";
|
||||
import { tasksWriteCommand, TASKS_WRITE_OPS, TASKS_WRITE_USAGE } from "./tasks-write.mjs";
|
||||
import { triageCommand, TRIAGE_USAGE } from "./triage.mjs";
|
||||
|
||||
export const USAGE = `usage:
|
||||
${ACCOUNTS_USAGE}
|
||||
@@ -103,6 +104,7 @@ export const USAGE = `usage:
|
||||
mosaic console start [--dev-local-owner [--dev-insecure-bind ADDR --dev-allow-host NAME[,NAME...]]] [--detach] [--port auto|N]
|
||||
mosaic console stop|status
|
||||
mosaic console restart
|
||||
${TRIAGE_USAGE}
|
||||
${DEV_USAGE}
|
||||
${RUN_USAGE}`;
|
||||
|
||||
@@ -818,6 +820,8 @@ export async function main(argv, io = { env: process.env, stdin: process.stdin,
|
||||
if (verb === "launches") return launches(rest, io, deps);
|
||||
if (verb === "bus") return bus(rest, io, deps);
|
||||
if (verb === "console") return consoleCommand(rest, io, deps);
|
||||
// triage-route-store (#1873): the owner's Triage route file, read and written as the person.
|
||||
if (verb === "triage") return triageCommand(rest, io, deps);
|
||||
if (verb === "dev") return devCommand(rest, io, { system: deps.system });
|
||||
if (verb === "run") return runCommand(rest, io, { system: deps.system, callFor: (p) => channelCallFor(p, io, deps) });
|
||||
if (verb === "-h" || verb === "--help") return io.stdout.write(`${USAGE}\n`);
|
||||
|
||||
@@ -302,6 +302,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();
|
||||
@@ -896,7 +901,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 +1077,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);
|
||||
|
||||
@@ -5,6 +5,7 @@ import { listOrgs, orgLock, resolveOrg } from '../../organization/src/org-regist
|
||||
import { organizationWriter } from '../../organization/src/organization.mjs';
|
||||
import { templateRender, validateOrg, createOrg, showOrg, updateOrg, queueInit, queueShow, projectRepoInit, projectRepoShow, projectRepoSet, deleteOrg, archiveOrg, restoreOrg } from '../../organization/src/org-service.mjs';
|
||||
import { installedStarterPacks } from '../../plugins/src/starter-pack.mjs';
|
||||
import { appendReceipt } from '../../model-proxy/src/admin.mjs';
|
||||
import { DEV_OWNER, devOwnerInstance } from './dev-local-owner.mjs';
|
||||
import { loadSystem, orgPaths, rolesDir } from './config.mjs';
|
||||
import { CliError } from './errors.mjs';
|
||||
@@ -173,6 +174,18 @@ async function templateCommand(args, io, deps) {
|
||||
io.stdout.write(JSON.stringify(result, null, 2) + '\n');
|
||||
}
|
||||
const busStartVerb = id => `mosaic bus start ${id} --dev-local-owner --detach`;
|
||||
// #1873 ride-default: `mosaic org update` that takes cliproxy off an organization records the removal
|
||||
// in the instance's proxy receipts first, where Ride all reads it, as the Console's admin save does.
|
||||
// A record that cannot be written refuses the update and writes nothing. A stale revision records
|
||||
// nothing and is left to updateOrg's own refusal.
|
||||
const providerList = doc => (Array.isArray(doc?.plugins?.['model-provider']) ? doc.plugins['model-provider'] : []);
|
||||
export async function orgUpdate(ctx, id, doc, { ifRevision, record = org => appendReceipt(ctx.dataRoot, { time: new Date().toISOString(), op: 'ride-removed', org }) } = {}) {
|
||||
const shown = await showOrg(ctx, id);
|
||||
if (shown.revision === ifRevision && providerList(shown.doc).includes('cliproxy') && !providerList(doc).includes('cliproxy')) {
|
||||
try { await record(id); } catch { throw new CliError('the removal of the model proxy could not be recorded, so nothing was saved', 3); }
|
||||
}
|
||||
return updateOrg(ctx, id, doc, { ifRevision });
|
||||
}
|
||||
export async function orgCommand(args, io, deps = {}) {
|
||||
try {
|
||||
// #1873 T1: the definitions verbs take their own options.
|
||||
@@ -195,7 +208,7 @@ export async function orgCommand(args, io, deps = {}) {
|
||||
const instance = devOwnerInstance(io.env); const system = deps.system ?? loadSystem({ env: io.env, instance }); const ctx = { configDir: configDir(io.env), dataRoot: system.dataRoot, instance, principal: DEV_OWNER };
|
||||
if (words[0] === 'lock' && ['show', 'clear'].includes(words[1]) && words.length === 3) { allowed([], []); result = await orgLock(ctx, words[2], { clear: words[1] === 'clear' }); }
|
||||
else if (words[0] === 'create' && words.length === 2) { allowed([], ['--from']); result = await createOrg(ctx, words[1], await document(need('--from'))); }
|
||||
else if (words[0] === 'update' && words.length === 2) { allowed([], ['--from', '--if-revision']); const r = need('--if-revision'); if (!/^[1-9][0-9]*$/.test(r)) throw new CliError('invalid revision', 4); result = await updateOrg(ctx, words[1], await document(need('--from')), { ifRevision: Number(r) }); }
|
||||
else if (words[0] === 'update' && words.length === 2) { allowed([], ['--from', '--if-revision']); const r = need('--if-revision'); if (!/^[1-9][0-9]*$/.test(r)) throw new CliError('invalid revision', 4); result = await orgUpdate(ctx, words[1], await document(need('--from')), { ifRevision: Number(r) }); }
|
||||
else if (words[0] === 'archive' && words.length === 2) result = await archiveOrg(ctx, words[1], { ...lifecycle, busStop: busStopForDelete, copyRemote: await (deps.copyRemoteFor ?? copyRemoteFor)(ctx) });
|
||||
else if (words[0] === 'restore' && words.length === 2) {
|
||||
const startBus = !p.flags.has('--no-bus');
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
// #1873 triage-wiring: the Triage key file's reader, moved out of triage-key.mjs so the Console can
|
||||
// read the key at each Triage spawn without loading the proxy management client or the proxy lock.
|
||||
// It imports node built-ins only; packages/webui/tests/triage-wiring.test.mjs fails if the Console's
|
||||
// Triage modules load model-proxy/src/client.mjs. triage-key.mjs (the owner's key verbs) imports
|
||||
// these and re-exports what it exported before, so its callers are unchanged.
|
||||
//
|
||||
// <configDir>/secrets/triage/model (directory 0700, file 0600, the running user's)
|
||||
// { version, key, keyId, issuedAt, rotateBy, retiring: [{ key, keyId, issuedAt, retireAt }] }
|
||||
//
|
||||
// Messages name files, never values.
|
||||
import { closeSync, constants, fchmodSync, fstatSync, lstatSync, mkdirSync, openSync, readFileSync } from "node:fs";
|
||||
import { createHash } from "node:crypto";
|
||||
import { join } from "node:path";
|
||||
|
||||
export const KEY_VERSION = 1;
|
||||
const FIELDS = Object.freeze(["version", "key", "keyId", "issuedAt", "rotateBy", "retiring"]);
|
||||
const RETIRING_FIELDS = Object.freeze(["key", "keyId", "issuedAt", "retireAt"]);
|
||||
const MAX_BYTES = 64 * 1024;
|
||||
export const MAX_RETIRING = 16;
|
||||
export const SECRET = /^[A-Za-z0-9_-]{43}$/;
|
||||
const ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
|
||||
const DAY = /^\d{4}-\d{2}-\d{2}$/;
|
||||
|
||||
export class TriageKeyError extends Error {
|
||||
constructor(code, message, exitCode = 3) {
|
||||
super(message);
|
||||
this.name = "TriageKeyError";
|
||||
this.code = code;
|
||||
this.exitCode = exitCode;
|
||||
}
|
||||
}
|
||||
export const unreadable = (why) => new TriageKeyError("triage-key-unreadable", `the Triage key store is unreadable: ${why}`);
|
||||
|
||||
export const keyIdOf = (key) => createHash("sha256").update(key).digest("hex").slice(0, 12);
|
||||
export const instanceTag = (configDir) => createHash("sha256").update(configDir).digest("hex").slice(0, 12);
|
||||
export const keyPrefix = (configDir) => `mst-${instanceTag(configDir)}-triage-`;
|
||||
const ownKey = (configDir) => (k) => typeof k === "string" && k.startsWith(keyPrefix(configDir)) && SECRET.test(k.slice(keyPrefix(configDir).length));
|
||||
|
||||
export function keyPaths(configDir) {
|
||||
const secrets = join(configDir, "secrets");
|
||||
const dir = join(secrets, "triage");
|
||||
return { secrets, dir, file: join(dir, "model") };
|
||||
}
|
||||
|
||||
const plain = (v) => v !== null && typeof v === "object" && !Array.isArray(v) && Object.getPrototypeOf(v) === Object.prototype;
|
||||
const exactKeys = (v, keys) => plain(v) && Object.keys(v).length === keys.length && keys.every((k) => Object.hasOwn(v, k));
|
||||
const canonicalTime = (s) => typeof s === "string" && ISO.test(s) && !Number.isNaN(Date.parse(s)) && new Date(s).toISOString() === s;
|
||||
const canonicalDay = (s) => typeof s === "string" && DAY.test(s) && !Number.isNaN(Date.parse(`${s}T00:00:00Z`)) && new Date(`${s}T00:00:00Z`).toISOString().slice(0, 10) === s;
|
||||
|
||||
export function validRecord(doc, configDir) {
|
||||
const own = ownKey(configDir);
|
||||
return exactKeys(doc, FIELDS)
|
||||
&& doc.version === KEY_VERSION
|
||||
&& own(doc.key) && doc.keyId === keyIdOf(doc.key)
|
||||
&& canonicalTime(doc.issuedAt) && canonicalDay(doc.rotateBy)
|
||||
&& Array.isArray(doc.retiring) && doc.retiring.length <= MAX_RETIRING
|
||||
&& doc.retiring.every((r) => exactKeys(r, RETIRING_FIELDS) && own(r.key) && r.key !== doc.key && r.keyId === keyIdOf(r.key)
|
||||
&& canonicalTime(r.issuedAt) && canonicalTime(r.retireAt))
|
||||
&& new Set(doc.retiring.map((r) => r.key)).size === doc.retiring.length;
|
||||
}
|
||||
|
||||
const uidOf = (opts) => opts.uid ?? process.getuid();
|
||||
|
||||
// A real directory, the running user's, mode 0700. Missing: false, or made when `create` is on.
|
||||
export function checkDir(dir, uid, what, { create }) {
|
||||
let st;
|
||||
try { st = lstatSync(dir); }
|
||||
catch (e) {
|
||||
if (e.code !== "ENOENT") throw unreadable(`${what} cannot be read`);
|
||||
if (!create) return false;
|
||||
try { mkdirSync(dir, { mode: 0o700 }); } catch { throw unreadable(`${what} cannot be made`); }
|
||||
try {
|
||||
const fd = openSync(dir, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
|
||||
try { fchmodSync(fd, 0o700); } finally { closeSync(fd); }
|
||||
} catch { throw unreadable(`${what} cannot be made`); }
|
||||
st = lstatSync(dir);
|
||||
}
|
||||
if (st.isSymbolicLink()) throw unreadable(`${what} is a link`);
|
||||
if (!st.isDirectory()) throw unreadable(`${what} is not a directory`);
|
||||
if (st.uid !== uid) throw unreadable(`${what} is not owned by this user`);
|
||||
if ((st.mode & 0o777) !== 0o700) throw unreadable(`${what} mode is not 0700`);
|
||||
return true;
|
||||
}
|
||||
|
||||
export function checkFile(fd, uid, what, fail = unreadable) {
|
||||
const st = fstatSync(fd);
|
||||
if (!st.isFile()) throw fail(`${what} is not a regular file`);
|
||||
if (st.nlink !== 1) throw fail(`${what} has more than one link`);
|
||||
if (st.uid !== uid) throw fail(`${what} is not owned by this user`);
|
||||
if ((st.mode & 0o777) !== 0o600) throw fail(`${what} mode is not 0600`);
|
||||
return st;
|
||||
}
|
||||
|
||||
// The stored record, or null when no key is issued. Throws triage-key-unreadable on anything unsafe.
|
||||
// opts.uid (both directories and the file) and opts.fileUid (the file only) are test seams for the
|
||||
// owner checks.
|
||||
export function readKey(configDir, opts = {}) {
|
||||
const { secrets, dir, file } = keyPaths(configDir);
|
||||
const uid = uidOf(opts);
|
||||
if (!checkDir(secrets, uid, "the secrets directory", { create: false })) return null;
|
||||
if (!checkDir(dir, uid, "the triage secrets directory", { create: false })) return null;
|
||||
let fd;
|
||||
try { fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); }
|
||||
catch (e) {
|
||||
if (e.code === "ENOENT") return null;
|
||||
if (e.code === "ELOOP") throw unreadable("the key file is a link");
|
||||
throw unreadable("the key file cannot be opened");
|
||||
}
|
||||
try {
|
||||
const st = checkFile(fd, opts.fileUid ?? uid, "the key file");
|
||||
if (st.size > MAX_BYTES) throw unreadable("the key file is too large");
|
||||
let doc;
|
||||
try { doc = JSON.parse(readFileSync(fd, "utf8")); } catch { throw unreadable("the key file is not JSON"); }
|
||||
if (!validRecord(doc, configDir)) throw unreadable("the key file does not match the key shape");
|
||||
return doc;
|
||||
} finally { closeSync(fd); }
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
// #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).
|
||||
//
|
||||
// <configDir>/secrets/triage/model (directory 0700, file 0600, the running user's)
|
||||
// { version, key, keyId, issuedAt, rotateBy, retiring: [{ key, keyId, issuedAt, retireAt }] }
|
||||
//
|
||||
// The key is `mst-<instance tag>-triage-<43 base64url>`, where the tag is the first 12 hex of
|
||||
// sha256(configDir), the tag the instance's own `msk-` sentinel carries. An org bus reconciles only
|
||||
// its own `mso-<tag>-<org>-key-` names and a no-org bus only `msk-<tag>-`, so neither touches it.
|
||||
//
|
||||
// Every proxy write runs under the instance proxy lock (packages/bus/src/proxy-lock.mjs), the lock
|
||||
// org buses take: read the list, check this instance's sentinel, fence, PUT, read back, confirm.
|
||||
// The proxy changes first and the file second, as the bus does for an agent key. A crash between
|
||||
// leaves a key in the Triage namespace that the file does not hold; the next issue, rotate or retire
|
||||
// removes it, and status names it. A crash after the PUT and before the read-back leaves the lock's
|
||||
// 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.
|
||||
//
|
||||
// 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.
|
||||
// The Console reads the key at each Triage spawn through triage-key-file.mjs, never this module.
|
||||
import { closeSync, constants, fchmodSync, fsyncSync, lstatSync, openSync, readdirSync, renameSync, unlinkSync, writeSync } from "node:fs";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { join } from "node:path";
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
import { createProxyLock } from "../../bus/src/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
|
||||
// the Console reads the key without this module's proxy client. Re-exported here unchanged.
|
||||
import { KEY_VERSION, MAX_RETIRING, SECRET, TriageKeyError, checkDir, checkFile, instanceTag, keyIdOf, keyPaths, keyPrefix, readKey, unreadable, validRecord } from "./triage-key-file.mjs";
|
||||
export { KEY_VERSION, TriageKeyError, instanceTag, keyIdOf, keyPaths, keyPrefix, readKey, validRecord };
|
||||
|
||||
// The org key lifetime and the rotate grace default. The bus holds both as literals
|
||||
// (packages/bus/src/model-keys.mjs #issue: 30 days to rotateBy; packages/bus/src/runtime.mjs:
|
||||
// `proxy.rotateGraceMinutes ?? 120`) and exports neither, and this lane may not change the bus.
|
||||
// packages/cli/tests/triage-key.test.mjs drives the bus's own ModelKeys with the same clock and
|
||||
// proves these give the same rotateBy and retireAt, so a change on either side goes red there.
|
||||
// The grace itself is read from proxy.json first, where the bus reads it.
|
||||
export const KEY_LIFETIME_DAYS = 30;
|
||||
export const DEFAULT_ROTATE_GRACE_MINUTES = 120;
|
||||
const sentinelPrefix = (configDir) => `msk-${instanceTag(configDir)}-sentinel-`;
|
||||
// Any name under the prefix is Triage's to remove, well formed or not, as the bus treats its own
|
||||
// `msk-<tag>-` names. Only a well-formed key may be stored in the file.
|
||||
const inNamespace = (configDir) => (k) => typeof k === "string" && k.startsWith(keyPrefix(configDir));
|
||||
|
||||
// Both directories, checked (and the triage one made) before any proxy write, so a store that cannot
|
||||
// be written refuses while the proxy is still untouched.
|
||||
function prepareDirs(configDir, uid) {
|
||||
const { secrets, dir } = keyPaths(configDir);
|
||||
if (!checkDir(secrets, uid, "the secrets directory", { create: false })) throw unreadable("the secrets directory is missing");
|
||||
checkDir(dir, uid, "the triage secrets directory", { create: true });
|
||||
}
|
||||
|
||||
// A temp file writeKey left when it was killed before its rename. It may hold a key.
|
||||
const TEMP = /^\.model\.\d+\.[0-9a-f]{12}\.tmp$/;
|
||||
const tempsIn = (dir) => { try { return readdirSync(dir).filter((n) => TEMP.test(n)); } catch { return []; } };
|
||||
// Under the lock no other Triage writer runs, so every temp file left in the directory is dead.
|
||||
function removeTemps(configDir) {
|
||||
const { dir } = keyPaths(configDir);
|
||||
for (const name of tempsIn(dir)) {
|
||||
try { if (!lstatSync(join(dir, name)).isDirectory()) unlinkSync(join(dir, name)); }
|
||||
catch { throw unreadable("a leftover temp file in the triage secrets directory cannot be removed"); }
|
||||
}
|
||||
}
|
||||
|
||||
// A directory sync that failed after the rename: the file is in place but may not be durable.
|
||||
class NotSynced extends Error {}
|
||||
|
||||
// Atomic: a 0600 temp file in the same directory, fsync, rename, fsync of the directory.
|
||||
function writeKey(configDir, record, uid, hook = () => {}) {
|
||||
const { dir, file } = keyPaths(configDir);
|
||||
prepareDirs(configDir, uid);
|
||||
const temp = join(dir, `.model.${process.pid}.${randomBytes(6).toString("hex")}.tmp`);
|
||||
let fd;
|
||||
try { fd = openSync(temp, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600); }
|
||||
catch { throw unreadable("the key file cannot be written"); }
|
||||
try {
|
||||
try { fchmodSync(fd, 0o600); writeSync(fd, `${JSON.stringify(record, null, 2)}\n`); fsyncSync(fd); }
|
||||
finally { closeSync(fd); }
|
||||
hook("rename");
|
||||
renameSync(temp, file);
|
||||
} catch { try { unlinkSync(temp); } catch {} throw unreadable("the key file cannot be written"); }
|
||||
try {
|
||||
hook("sync");
|
||||
const dfd = openSync(dir, constants.O_RDONLY | constants.O_DIRECTORY);
|
||||
try { fsyncSync(dfd); } finally { closeSync(dfd); }
|
||||
} catch { throw new NotSynced(); }
|
||||
}
|
||||
|
||||
// One line per write in the proxy lock's receipts file, ids only. `receiptsReady` runs under the
|
||||
// lock before any change, so an unsafe receipts file refuses while nothing has changed yet.
|
||||
const RECEIPTS_FLAGS = constants.O_WRONLY | constants.O_APPEND | constants.O_CREAT | constants.O_NOFOLLOW | constants.O_NONBLOCK;
|
||||
const receiptsUnsafe = (why) => new TriageKeyError("triage-key-receipts-unsafe", `${why}; nothing was changed`);
|
||||
function receiptsReady(stateDir, uid) {
|
||||
let fd;
|
||||
try { fd = openSync(join(stateDir, "mosaic-keys.receipts.jsonl"), RECEIPTS_FLAGS, 0o600); }
|
||||
catch { throw receiptsUnsafe("the proxy receipts file cannot be opened"); }
|
||||
try { checkFile(fd, uid, "the proxy receipts file", receiptsUnsafe); } finally { closeSync(fd); }
|
||||
}
|
||||
function receipt(stateDir, uid, entry) {
|
||||
const unrecorded = new TriageKeyError("triage-key-unrecorded", "the change was made but its receipt line was not written; run mosaic triage key status");
|
||||
let fd;
|
||||
try { fd = openSync(join(stateDir, "mosaic-keys.receipts.jsonl"), RECEIPTS_FLAGS, 0o600); } catch { throw unrecorded; }
|
||||
try {
|
||||
checkFile(fd, uid, "the proxy receipts file", () => unrecorded);
|
||||
writeSync(fd, `${JSON.stringify(entry)}\n`);
|
||||
fsyncSync(fd);
|
||||
} catch { throw unrecorded; }
|
||||
finally { closeSync(fd); }
|
||||
}
|
||||
|
||||
// The grace the bus uses: proxy.json's rotateGraceMinutes, or the bus default.
|
||||
export const graceMinutes = (proxy) => proxy?.rotateGraceMinutes ?? DEFAULT_ROTATE_GRACE_MINUTES;
|
||||
export const rotateByFor = (nowMs) => new Date(nowMs + KEY_LIFETIME_DAYS * 86400_000).toISOString().slice(0, 10);
|
||||
export const retireAtFor = (nowMs, proxy) => new Date(nowMs + graceMinutes(proxy) * 60_000).toISOString();
|
||||
|
||||
const proxyFailure = (code) => {
|
||||
const words = {
|
||||
"proxy-lock-busy": "another proxy writer holds the instance proxy lock; try again when it ends, and if no writer is running, see mosaic proxy fence status",
|
||||
"proxy-lock-uncertain": "the instance proxy lock is in a state this verb will not guess about; check <dataRoot>/proxy by hand",
|
||||
"proxy-lock-invalid": "the instance proxy lock directory is missing or unsafe, or names another proxy",
|
||||
"proxy-uncertain": "an earlier proxy write has an uncertain outcome; see mosaic proxy fence status",
|
||||
"proxy-management-key-invalid": "the proxy management key file is missing or unsafe",
|
||||
"proxy-client-invalid": "the proxy settings are not usable",
|
||||
"proxy-client-down": "the proxy did not answer",
|
||||
"proxy-client-refused": "the proxy refused the management request",
|
||||
"proxy-client-redirect": "the proxy answered with a redirect",
|
||||
"proxy-client-response": "the proxy gave an answer this verb cannot use",
|
||||
};
|
||||
return new TriageKeyError(code, words[code] ?? "the proxy write failed");
|
||||
};
|
||||
|
||||
// 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 }) {
|
||||
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 ?? (() => {}),
|
||||
};
|
||||
}
|
||||
|
||||
function lockFor(c) {
|
||||
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) {
|
||||
try { return await loadManagementClient({ configDir: c.configDir, proxy: c.proxy }); }
|
||||
catch (e) { throw proxyFailure(e.code ?? "proxy-management-key-invalid"); }
|
||||
}
|
||||
|
||||
function checkSentinel(c, keys) {
|
||||
const prefix = sentinelPrefix(c.configDir);
|
||||
if (!keys.some((k) => k.startsWith(prefix) && SECRET.test(k.slice(prefix.length)))) {
|
||||
throw new TriageKeyError("triage-key-proxy-open", "the proxy's key list has no sentinel for this instance, so it may be another proxy");
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
const lock = lockFor(c);
|
||||
const client = await clientFor(c);
|
||||
try {
|
||||
return await lock.run(async () => {
|
||||
const before = await client.getKeys();
|
||||
checkSentinel(c, before);
|
||||
const current = readKey(c.configDir, { uid: c.uid, fileUid: c.fileUid });
|
||||
const result = plan(before, current);
|
||||
if (result.record) prepareDirs(c.configDir, c.uid);
|
||||
removeTemps(c.configDir);
|
||||
receiptsReady(c.stateDir, c.uid);
|
||||
const after = result.after;
|
||||
const removed = before.filter((k) => !after.includes(k));
|
||||
const added = after.filter((k) => !before.includes(k));
|
||||
// A key in another namespace never leaves the list.
|
||||
if (removed.some((k) => !inNamespace(c.configDir)(k))) throw new TriageKeyError("triage-key-failed", "the change would remove a key outside the Triage namespace");
|
||||
if (added.length || removed.length) {
|
||||
// As the bus does: a list that moved since the first read is not overwritten.
|
||||
if (!isDeepStrictEqual(await client.getKeys(), before)) throw new TriageKeyError("triage-key-raced", "the proxy's key list changed while this verb ran; run it again");
|
||||
lock.fence({ org: null, op: `triage.${op}` });
|
||||
await client.putKeys(after);
|
||||
c.hook("put");
|
||||
const confirmed = await client.getKeys();
|
||||
if (!confirmed.some((k) => k.startsWith(sentinelPrefix(c.configDir))) || added.some((k) => !confirmed.includes(k)) || removed.some((k) => confirmed.includes(k))) {
|
||||
throw new TriageKeyError("triage-key-unconfirmed", "the proxy did not confirm the change; the instance proxy fence stays set, see mosaic proxy fence status");
|
||||
}
|
||||
lock.confirm();
|
||||
}
|
||||
c.hook("write");
|
||||
let unsynced = false;
|
||||
if (result.record) {
|
||||
try { 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`);
|
||||
}
|
||||
}
|
||||
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) });
|
||||
// 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) };
|
||||
});
|
||||
} catch (e) {
|
||||
if (e instanceof TriageKeyError) throw e;
|
||||
throw proxyFailure(typeof e?.code === "string" && /^proxy-[a-z-]+$/.test(e.code) ? e.code : "triage-key-failed");
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
// file write, or by a file that was removed by hand.
|
||||
const orphansOf = (configDir, keys, record) => {
|
||||
const held = record ? [record.key, ...record.retiring.map((r) => r.key)] : [];
|
||||
return keys.filter((k) => inNamespace(configDir)(k) && !held.includes(k));
|
||||
};
|
||||
|
||||
export async function issueKey(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "issue", (before, current) => {
|
||||
if (current) throw new TriageKeyError("triage-key-issued", "a Triage key is already issued; use mosaic triage key rotate");
|
||||
const orphans = orphansOf(c.configDir, before, null);
|
||||
const key = newKey(c.configDir), nowMs = c.now();
|
||||
const record = { version: KEY_VERSION, key, keyId: keyIdOf(key), issuedAt: new Date(nowMs).toISOString(), rotateBy: rotateByFor(nowMs), retiring: [] };
|
||||
return { after: [...before.filter((k) => !orphans.includes(k)), key], record, done: { keyId: record.keyId, rotateBy: record.rotateBy } };
|
||||
});
|
||||
}
|
||||
|
||||
// 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));
|
||||
|
||||
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 } },
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export async function retireKeys(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "retire", (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) } };
|
||||
});
|
||||
}
|
||||
|
||||
// Read only: the file, and when the proxy is set up, what its list holds. No lock, no write.
|
||||
// The fence is checked by name only, so status writes nothing, not even the lock's identity file.
|
||||
export async function keyStatus(opts) {
|
||||
const { configDir, system, uid, fileUid } = opts;
|
||||
const record = readKey(configDir, { uid, fileUid });
|
||||
const proxy = { state: "not-set-up", fence: false, leftoverTemps: tempsIn(keyPaths(configDir).dir).length };
|
||||
if (system?.proxy && Number.isInteger(system.proxy.port)) {
|
||||
try { lstatSync(join(system.dataRoot, "proxy", "mosaic-keys.uncertain")); proxy.fence = true; }
|
||||
catch (e) { if (e.code !== "ENOENT") proxy.fence = true; }
|
||||
try {
|
||||
const client = await loadManagementClient({ configDir, proxy: system.proxy });
|
||||
const keys = await client.getKeys();
|
||||
const prefix = sentinelPrefix(configDir);
|
||||
proxy.state = keys.some((k) => k.startsWith(prefix) && SECRET.test(k.slice(prefix.length))) ? "read" : "no-sentinel";
|
||||
proxy.holdsKey = record ? keys.includes(record.key) : null;
|
||||
proxy.retiringHeld = record ? record.retiring.map((r) => keys.includes(r.key)) : [];
|
||||
proxy.orphanKeyIds = orphansOf(configDir, keys, record).map(keyIdOf);
|
||||
} catch (e) { proxy.state = "unreadable"; proxy.code = typeof e?.code === "string" && /^proxy-[a-z-]+$/.test(e.code) ? e.code : "proxy-client-response"; }
|
||||
}
|
||||
return { record, proxy };
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
// #1873 triage-route-store: the owner's terminal verbs for the Triage route file,
|
||||
// <dataRoot>/triage/route.json (packages/webui/src/triage-route-store.mjs).
|
||||
//
|
||||
// mosaic triage route show
|
||||
// mosaic triage route set --model KEY --harness NAME --cap TOKENS --time-zone ZONE [--revision N]
|
||||
//
|
||||
// Both are human verbs: an agent run is refused before anything is read (exit 3), from this
|
||||
// process's environment and the one the command was given, as the bus and Console lifecycle verbs
|
||||
// refuse it. `show` with no file prints "not set" and exits 0. Without --revision a write
|
||||
// compares with the revision read under the store's lock; pass --revision from `show` to make the
|
||||
// write conditional on what was shown (0: only when no route is set yet).
|
||||
//
|
||||
// Nothing here wires the route into the Triage host: the Console keeps `route: null` until the
|
||||
// wiring slice. `set` checks the harness against the repository's harness modules; it does not
|
||||
// check the model key against a catalogue, since none exists outside an organization's bus yet.
|
||||
//
|
||||
// mosaic triage key issue | rotate | retire | status
|
||||
//
|
||||
// Triage's own model proxy client key (packages/cli/src/triage-key.mjs). 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";
|
||||
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 { CliError } from "./errors.mjs";
|
||||
import { refuseInsideAgent } from "./transport.mjs";
|
||||
|
||||
export const TRIAGE_USAGE = "mosaic triage route show | set --model KEY --harness NAME --cap TOKENS --time-zone ZONE [--revision N] | key issue | rotate | retire | status";
|
||||
const KEY_VERBS = ["issue", "rotate", "retire", "status"];
|
||||
const HARNESS_ROOT = fileURLToPath(new URL("../../plugins/harness/", import.meta.url));
|
||||
const VALUES = ["--model", "--harness", "--cap", "--time-zone", "--revision"];
|
||||
const WHOLE = /^(0|[1-9][0-9]*)$/;
|
||||
// The actor every terminal write records: the instance owner, the only person who runs this verb.
|
||||
const ACTOR = "owner";
|
||||
|
||||
const usage = (why) => new CliError(`${why}\nusage: ${TRIAGE_USAGE}`, 4);
|
||||
|
||||
// Option names, never values, in a refusal.
|
||||
function parse(args) {
|
||||
const values = {}, positional = [];
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const a = args[i];
|
||||
if (VALUES.includes(a)) {
|
||||
if (i + 1 >= args.length) throw usage(`${a} needs one value`);
|
||||
if (Object.hasOwn(values, a)) throw usage(`${a} is given twice`);
|
||||
values[a] = args[++i];
|
||||
} else if (a.startsWith("--")) throw usage("unknown option");
|
||||
else positional.push(a);
|
||||
}
|
||||
return { values, positional };
|
||||
}
|
||||
|
||||
function whole(text, name) {
|
||||
if (!WHOLE.test(text)) throw new CliError(`triage-route-invalid: ${name} must be a whole number`, 2);
|
||||
return Number(text);
|
||||
}
|
||||
|
||||
function knownHarness(name) {
|
||||
if (!/^[a-z][a-z0-9-]{0,31}$/.test(name)) return false;
|
||||
try { return lstatSync(join(HARNESS_ROOT, name, "index.mjs")).isFile(); } catch { return false; }
|
||||
}
|
||||
|
||||
function storeError(error) {
|
||||
if (!(error instanceof RouteStoreError)) throw error;
|
||||
return new CliError(`${error.code}: ${error.message}`, error.code === "triage-route-invalid" ? 2 : 3);
|
||||
}
|
||||
|
||||
function show(route) {
|
||||
if (!route) return "triage route: not set\n";
|
||||
const { cap } = route;
|
||||
return [
|
||||
`triage route: revision ${route.revision}`,
|
||||
`model: ${route.model}`,
|
||||
`harness: ${route.harness}`,
|
||||
`cap: ${cap.limit} tokens a day, resets ${cap.reset} ${cap.timeZone}, counted since ${cap.since}`,
|
||||
`updated: ${route.updatedAt} by ${route.updatedBy}`,
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
// A key error prints its code and fixed words. Anything else becomes one fixed line, so no value
|
||||
// that an unexpected error might carry reaches the terminal.
|
||||
function keyError(error) {
|
||||
if (error instanceof CliError) return error;
|
||||
if (error instanceof TriageKeyError) return new CliError(`${error.code}: ${error.message}`, error.exitCode);
|
||||
return new CliError("triage-key-failed: the key verb stopped on an unexpected error; run mosaic triage key status", 3);
|
||||
}
|
||||
|
||||
const age = (ms) => {
|
||||
const m = Math.max(0, Math.floor(ms / 60_000));
|
||||
return `${Math.floor(m / 1440)}d ${Math.floor((m % 1440) / 60)}h ${m % 60}m`;
|
||||
};
|
||||
const ids = (list) => (list.length ? list.join(", ") : "none");
|
||||
|
||||
function statusText({ record, proxy }, nowMs, file) {
|
||||
const lines = [`triage key: ${record ? "issued" : "not issued"}`, `file: ${file}`];
|
||||
const rotateDue = record && Date.parse(`${record.rotateBy}T00:00:00Z`) <= nowMs;
|
||||
const retireDue = record ? record.retiring.filter((r) => Date.parse(r.retireAt) <= nowMs) : [];
|
||||
if (record) {
|
||||
lines.push(`key id: ${record.keyId}`, `issued: ${record.issuedAt} (age ${age(nowMs - Date.parse(record.issuedAt))})`, `rotate by: ${record.rotateBy}${rotateDue ? " (due)" : ""}`);
|
||||
record.retiring.forEach((r, i) => {
|
||||
const held = proxy.retiringHeld?.[i];
|
||||
lines.push(`retiring: ${r.keyId}, live until a retire after ${r.retireAt}${Date.parse(r.retireAt) <= nowMs ? " (due)" : ""}${held === false ? " (not in the proxy list)" : ""}`);
|
||||
});
|
||||
}
|
||||
const state = {
|
||||
"not-set-up": "not set up for this instance (no proxy.json)",
|
||||
"no-sentinel": "its key list has no sentinel for this instance",
|
||||
unreadable: `not read (${proxy.code})`,
|
||||
read: record ? (proxy.holdsKey ? "holds the key" : "does not hold the key") : "read",
|
||||
}[proxy.state];
|
||||
lines.push(`proxy: ${state}`);
|
||||
if (proxy.fence) lines.push("proxy fence: set, an earlier proxy write has an uncertain outcome");
|
||||
if (proxy.leftoverTemps) lines.push(`leftover temp files in the triage secrets directory: ${proxy.leftoverTemps} (the next issue, rotate or retire removes them)`);
|
||||
if (proxy.orphanKeyIds?.length) lines.push(`keys in the Triage namespace the file does not hold: ${ids(proxy.orphanKeyIds)}`);
|
||||
let next;
|
||||
if (proxy.fence) next = "restart the proxy, then mosaic proxy fence clear --proxy-restarted";
|
||||
else if (proxy.state !== "read") next = proxy.state === "not-set-up" ? "set up the model proxy first (mosaic proxy init)" : "check the proxy with mosaic proxy status";
|
||||
else if (!record) next = "mosaic triage key issue";
|
||||
else if (!proxy.holdsKey || rotateDue) next = "mosaic triage key rotate";
|
||||
else if (retireDue.length || proxy.leftoverTemps || proxy.orphanKeyIds?.length || proxy.retiringHeld?.includes(false)) next = "mosaic triage key retire";
|
||||
else next = "nothing";
|
||||
lines.push(`next: ${next}`, "");
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
async function keyCommand(sub, args, io, deps) {
|
||||
if (!KEY_VERBS.includes(sub)) throw usage("unknown triage key command");
|
||||
if (args.length) throw usage(args[0].startsWith("--") ? "unknown option" : "unexpected argument");
|
||||
try {
|
||||
const system = deps.system ?? loadSystem({ env: io.env });
|
||||
const dir = configDir(io.env);
|
||||
const now = deps.now ? () => deps.now().getTime() : Date.now;
|
||||
const opts = { configDir: dir, system, now, uid: deps.triageKeyUid, fileUid: deps.triageKeyFileUid, hook: deps.triageKeyHook };
|
||||
if (sub === "status") {
|
||||
io.stdout.write(statusText(await keyStatus(opts), now(), keyPaths(dir).file));
|
||||
return;
|
||||
}
|
||||
if (sub === "issue") {
|
||||
const r = await issueKey(opts);
|
||||
io.stdout.write(`triage key issued: ${r.keyId}, rotate by ${r.rotateBy}\nfile: ${keyPaths(dir).file}\n`);
|
||||
if (r.removedKeyIds.length) io.stdout.write(`removed keys this file did not hold: ${ids(r.removedKeyIds)}\n`);
|
||||
return;
|
||||
}
|
||||
if (sub === "rotate") {
|
||||
const r = await rotateKey(opts);
|
||||
io.stdout.write(`triage key rotated: ${r.keyId}, rotate by ${r.rotateBy}\n`);
|
||||
io.stdout.write(r.old.lost
|
||||
? `old key ${r.old.keyId} was not in the proxy list; nothing to retire\n`
|
||||
: `old key ${r.old.keyId} stays live until mosaic triage key retire runs at or after ${r.old.retireAt}\n`);
|
||||
if (r.removedKeyIds.length) io.stdout.write(`removed: ${ids(r.removedKeyIds)}\n`);
|
||||
return;
|
||||
}
|
||||
const r = await retireKeys(opts);
|
||||
io.stdout.write(r.removedKeyIds.length ? `triage key retire: removed ${ids(r.removedKeyIds)}\n` : "triage key retire: nothing to remove\n");
|
||||
} catch (e) { throw keyError(e); }
|
||||
}
|
||||
|
||||
export function triageCommand(rest, io, deps = {}) {
|
||||
refuseInsideAgent(process.env);
|
||||
refuseInsideAgent(io.env);
|
||||
const [noun, sub, ...args] = rest;
|
||||
if (noun === "key" && sub !== undefined) return keyCommand(sub, args, io, deps);
|
||||
if (noun !== "route" || !["show", "set"].includes(sub)) throw usage("unknown triage command");
|
||||
const { values, positional } = parse(args);
|
||||
if (positional.length) throw usage("unexpected argument");
|
||||
const dataRoot = () => (deps.system ?? loadSystem({ env: io.env })).dataRoot;
|
||||
if (sub === "show") {
|
||||
if (Object.keys(values).length) throw usage("show takes no options");
|
||||
let route;
|
||||
try { route = readRoute(dataRoot()); } catch (e) { throw storeError(e); }
|
||||
io.stdout.write(show(route));
|
||||
return;
|
||||
}
|
||||
for (const name of ["--model", "--harness", "--cap", "--time-zone"]) if (!Object.hasOwn(values, name)) throw usage(`set needs ${name}`);
|
||||
if (!knownHarness(values["--harness"])) throw new CliError("triage-route-invalid: no harness module has that name", 2);
|
||||
const input = {
|
||||
model: values["--model"], harness: values["--harness"], limit: whole(values["--cap"], "--cap"),
|
||||
timeZone: values["--time-zone"], by: ACTOR,
|
||||
...(Object.hasOwn(values, "--revision") ? { revision: whole(values["--revision"], "--revision") } : {}),
|
||||
};
|
||||
let route;
|
||||
try { route = writeRoute(dataRoot(), input, deps.now ? { now: deps.now } : {}); } catch (e) { throw storeError(e); }
|
||||
io.stdout.write(`triage route set: revision ${route.revision}\n`);
|
||||
}
|
||||
@@ -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),
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -151,7 +151,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 });
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
// #1873 ride-default, re-check 3 P1-1: `mosaic org update` that takes cliproxy off an organization
|
||||
// records the removal in the instance's proxy receipts before it writes, as the Console's admin save
|
||||
// does, so Ride all leaves the organization off.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { orgUpdate } from '../src/org.mjs';
|
||||
import * as orgService from '../../organization/src/org-service.mjs';
|
||||
import { createProxyAdmin } from '../../model-proxy/src/admin-control.mjs';
|
||||
import { receiptFile } from '../../model-proxy/src/admin.mjs';
|
||||
|
||||
const owner = { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' };
|
||||
function scratch(t) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'org-upd-'));
|
||||
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 });
|
||||
return { configDir, dataRoot, instance: 'test', principal: owner };
|
||||
}
|
||||
const receipts = dataRoot => { try { return fs.readFileSync(receiptFile(dataRoot), 'utf8').trim().split('\n').filter(Boolean).map(JSON.parse); } catch { return []; } };
|
||||
const withProviders = (doc, list) => ({ ...doc, plugins: { ...(doc.plugins ?? {}), 'model-provider': list } });
|
||||
async function cliproxyOrg(ctx, id) {
|
||||
const doc = withProviders(await orgService.templateRender(id), ['cliproxy']);
|
||||
await orgService.createOrg(ctx, id, doc);
|
||||
return orgService.showOrg(ctx, id);
|
||||
}
|
||||
|
||||
test('the reviewer probe: an organization taken off cliproxy by mosaic org update is skipped by Ride all as removed', async t => {
|
||||
const ctx = scratch(t);
|
||||
const shown = await cliproxyOrg(ctx, 'cli');
|
||||
await orgUpdate(ctx, 'cli', withProviders(shown.doc, []), { ifRevision: shown.revision });
|
||||
assert.deepEqual((await orgService.showOrg(ctx, 'cli')).doc.plugins['model-provider'], []);
|
||||
assert.deepEqual(receipts(ctx.dataRoot).map(r => [r.op, r.org]), [['ride-removed', 'cli']]);
|
||||
const writes = [];
|
||||
const call = async verb => {
|
||||
if (verb === 'plugins') return { revision: 'r', plugins: { 'model-provider': [] } };
|
||||
if (verb === 'proxy.accounts') return { accounts: [] };
|
||||
if (verb === 'plugins.set') { writes.push('cli'); return { revision: 'r2' }; }
|
||||
throw new Error(`unexpected ${verb}`);
|
||||
};
|
||||
const proxy = createProxyAdmin({ dataRoot: ctx.dataRoot, configDir: ctx.configDir, unit: { unit: 'u', status: async () => ({}) }, buses: async () => ({ stopped: true, running: [] }), fetch: async () => ({}),
|
||||
riders: async () => [{ id: 'cli', fixture: false, cliproxy: false, changes: '', call }] });
|
||||
const r = await proxy.rideAll();
|
||||
assert.deepEqual(r.added, []); assert.deepEqual(r.skipped, [{ id: 'cli', reason: 'removed' }]);
|
||||
assert.deepEqual(writes, []);
|
||||
});
|
||||
|
||||
test('mosaic org update records only a removal of cliproxy, and saves nothing when the record cannot be written', async t => {
|
||||
const ctx = scratch(t);
|
||||
const recorded = [];
|
||||
const record = async id => { recorded.push(id); };
|
||||
// Keeping cliproxy, or an organization that never had it, records nothing.
|
||||
let shown = await cliproxyOrg(ctx, 'keep');
|
||||
await orgUpdate(ctx, 'keep', { ...shown.doc }, { ifRevision: shown.revision, record });
|
||||
const plain = await orgService.templateRender('plain');
|
||||
await orgService.createOrg(ctx, 'plain', withProviders(plain, []));
|
||||
shown = await orgService.showOrg(ctx, 'plain');
|
||||
await orgUpdate(ctx, 'plain', { ...shown.doc }, { ifRevision: shown.revision, record });
|
||||
assert.deepEqual(recorded, []);
|
||||
// A stale revision records nothing and is refused by the service.
|
||||
shown = await cliproxyOrg(ctx, 'stale');
|
||||
await assert.rejects(orgUpdate(ctx, 'stale', withProviders(shown.doc, []), { ifRevision: shown.revision + 1, record }), { code: 'revision-conflict' });
|
||||
assert.deepEqual(recorded, []);
|
||||
// A record that fails refuses the update: the organization keeps cliproxy, at the same revision.
|
||||
await assert.rejects(orgUpdate(ctx, 'stale', withProviders(shown.doc, []), { ifRevision: shown.revision, record: async () => { throw Object.assign(new Error('disk full at /x/proxy-admin.jsonl'), { code: 'ENOSPC' }); } }),
|
||||
err => err.exitCode === 3 && err.message === 'the removal of the model proxy could not be recorded, so nothing was saved');
|
||||
const after = await orgService.showOrg(ctx, 'stale');
|
||||
assert.deepEqual(after.doc.plugins['model-provider'], ['cliproxy']); assert.equal(after.revision, shown.revision);
|
||||
// The removal itself records once, before the write.
|
||||
await orgUpdate(ctx, 'stale', withProviders(shown.doc, []), { ifRevision: shown.revision, record: async id => { recorded.push(id); assert.deepEqual((await orgService.showOrg(ctx, 'stale')).doc.plugins['model-provider'], ['cliproxy'], 'recorded before the write'); } });
|
||||
assert.deepEqual(recorded, ['stale']);
|
||||
});
|
||||
|
||||
test('the mosaic org update command records the removal in the instance\'s own proxy receipts', async t => {
|
||||
const { spawnSync } = await import('node:child_process');
|
||||
const { resolve } = await import('node:path');
|
||||
const repo = resolve(import.meta.dirname, '../../..');
|
||||
const home = fs.mkdtempSync(join(tmpdir(), 'org-upd-cli-'));
|
||||
t.after(() => fs.rmSync(home, { recursive: true, force: true }));
|
||||
const env = { ...process.env, HOME: home, MOSAIC_CONFIG: join(home, '.config/mosaic-orgupd/config.json') };
|
||||
const run = (cmd, ...args) => { const r = spawnSync(join(repo, cmd), args, { cwd: repo, env, encoding: 'utf8' }); assert.equal(r.status, 0, r.stderr); return r.stdout; };
|
||||
run('scripts/bootstrap.sh', '--instance', 'orgupd');
|
||||
const on = join(home, 'on.json'), off = join(home, 'off.json');
|
||||
const doc = withProviders(await orgService.templateRender('cliw'), ['cliproxy']);
|
||||
fs.writeFileSync(on, JSON.stringify(doc)); fs.writeFileSync(off, JSON.stringify(withProviders(doc, [])));
|
||||
run('scripts/mosaic', 'org', 'create', 'cliw', '--from', on);
|
||||
const { revision } = JSON.parse(run('scripts/mosaic', 'org', 'show', 'cliw', '--json'));
|
||||
run('scripts/mosaic', 'org', 'update', 'cliw', '--from', off, '--if-revision', String(revision));
|
||||
assert.deepEqual(JSON.parse(run('scripts/mosaic', 'org', 'show', 'cliw', '--json')).doc.plugins['model-provider'], []);
|
||||
assert.deepEqual(receipts(join(home, '.mosaic-orgupd')).map(r => [r.op, r.org]), [['ride-removed', 'cliw']]);
|
||||
});
|
||||
@@ -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),
|
||||
|
||||
@@ -0,0 +1,695 @@
|
||||
// #1873 triage-key: `mosaic triage key issue|rotate|retire|status`, Triage's own model proxy key.
|
||||
// A stand-in management server on loopback and a temporary config directory only; nothing here
|
||||
// reads a real config, proxy or secret. Every stdout, stderr, receipt and file the verbs leave is
|
||||
// scanned for every key the stand-in ever saw, at the end of each test and once more at the end.
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawn, spawnSync } from "node:child_process";
|
||||
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 { 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 { ModelKeys } from "../../bus/src/model-keys.mjs";
|
||||
import { ManagementClient } from "../../model-proxy/src/client.mjs";
|
||||
import { startBroker } from "../../bus/src/runtime.mjs";
|
||||
import { busOrganization } from "../../bus/src/organization-context.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";
|
||||
import { humanEnv, io } from "./helpers.mjs";
|
||||
|
||||
const REPO = new URL("../../../", import.meta.url).pathname;
|
||||
const NOW = Date.parse("2026-11-18T12:00:00.000Z");
|
||||
const MIN = 60_000, HOUR = 60 * MIN, DAY = 24 * HOUR;
|
||||
const MANAGE = "fx1873-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 iso = (ms) => new Date(ms).toISOString();
|
||||
|
||||
// Every output any verb produced in this file, and every Triage key the stand-in ever held.
|
||||
const OUTPUTS = [];
|
||||
const SEEN = new Set();
|
||||
const VERBS = ["issue", "rotate", "retire", "status"];
|
||||
|
||||
function leaks(text, keys = SEEN) {
|
||||
for (const key of keys) {
|
||||
const secret = key.slice(-43);
|
||||
if (text.includes(key) || text.includes(secret)) return true;
|
||||
for (let i = 0; i + 8 <= secret.length; i++) if (text.includes(secret.slice(i, i + 8))) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function walk(dir, skip, out = []) {
|
||||
for (const name of fs.readdirSync(dir)) {
|
||||
const p = join(dir, name), st = fs.lstatSync(p);
|
||||
if (st.isDirectory()) walk(p, skip, out);
|
||||
else if (st.isFile() && !skip.includes(p)) out.push(p);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
async function fixture(t, { sentinel = true } = {}) {
|
||||
const root = fs.realpathSync(fs.mkdtempSync(join(tmpdir(), "tk-")));
|
||||
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 prefix = `mst-${tag(configDir)}-triage-`;
|
||||
const state = {
|
||||
keys: [...(sentinel ? [`msk-${tag(configDir)}-sentinel-${b64()}`] : []), "foreign-client-key", `mso-${tag(configDir)}-demo-key-${b64()}`],
|
||||
gets: 0, puts: [], ignorePut: false, dropSentinelOnPut: false, onGet: null,
|
||||
};
|
||||
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") { state.gets++; const body = JSON.stringify(state.keys); state.onGet?.(state.gets); res.end(body); 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 (state.dropSentinelOnPut) state.keys = next.filter((k) => !k.startsWith("msk-") || !k.includes("-sentinel-"));
|
||||
else if (!state.ignorePut) 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) await close();
|
||||
server.closeAllConnections(); await new Promise((resolve) => server.close(resolve));
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
});
|
||||
const f = {
|
||||
root, configDir, prefix, state, server, port, closers, now: NOW,
|
||||
system: { dataRoot: root, proxy: { port, binary: "/fixture-unused" } },
|
||||
file: keyPaths(configDir).file, dir: keyPaths(configDir).dir,
|
||||
env: (extra = {}) => humanEnv({ MOSAIC_CONFIG: join(configDir, "config.json"), ...extra }),
|
||||
async cli(argv, { env, deps = {}, system = f.system } = {}) {
|
||||
const s = io(env ?? f.env());
|
||||
const code = await run(argv, s, { system, now: () => new Date(f.now), ...deps });
|
||||
const out = { code, stdout: s.out.text, stderr: s.err.text };
|
||||
OUTPUTS.push(out.stdout, out.stderr);
|
||||
return out;
|
||||
},
|
||||
// The same verb in a child process that SIGKILLs itself at `stage`.
|
||||
crash(argv, stage) {
|
||||
const script = `
|
||||
import { run } from ${JSON.stringify(new URL("../src/cli.mjs", import.meta.url).href)};
|
||||
import { io } from ${JSON.stringify(new URL("./helpers.mjs", import.meta.url).href)};
|
||||
const p = JSON.parse(process.env.TK_CRASH);
|
||||
const s = io(process.env);
|
||||
const code = await run(p.argv, s, { system: p.system, now: () => new Date(p.now),
|
||||
triageKeyHook: (stage) => { if (stage === p.stage) process.kill(process.pid, "SIGKILL"); } });
|
||||
process.stdout.write(s.out.text); process.stderr.write(s.err.text); process.exitCode = code;`;
|
||||
const env = f.env({ TK_CRASH: JSON.stringify({ argv, stage, system: f.system, now: f.now }) });
|
||||
return new Promise((resolve) => {
|
||||
const child = spawn(process.execPath, ["--input-type=module", "-e", script], { env, stdio: ["ignore", "pipe", "pipe"] });
|
||||
let stdout = "", stderr = "";
|
||||
child.stdout.on("data", (d) => { stdout += d; });
|
||||
child.stderr.on("data", (d) => { stderr += d; });
|
||||
child.on("exit", (code, signal) => { OUTPUTS.push(stdout, stderr); resolve({ code, signal, stdout, stderr }); });
|
||||
});
|
||||
},
|
||||
record: () => JSON.parse(fs.readFileSync(f.file, "utf8")),
|
||||
triageKeys: () => state.keys.filter((k) => k.startsWith(prefix)),
|
||||
receipts: () => {
|
||||
const file = join(root, "proxy", "mosaic-keys.receipts.jsonl");
|
||||
return fs.existsSync(file) ? fs.readFileSync(file, "utf8").split("\n").filter(Boolean).map((l) => JSON.parse(l)) : [];
|
||||
},
|
||||
fenced: () => fs.existsSync(join(root, "proxy", "mosaic-keys.uncertain")),
|
||||
// No file under the fixture but the key file itself holds a key; neither does any output.
|
||||
scan(alsoKeyFiles = []) {
|
||||
for (const k of state.keys) if (/^mst-.*-[A-Za-z0-9_-]{43}$/.test(k)) SEEN.add(k);
|
||||
for (const p of walk(root, [f.file, join(configDir, "secrets", "proxy-management.key"), ...alsoKeyFiles])) {
|
||||
assert.equal(leaks(fs.readFileSync(p, "latin1")), false, `a key is in ${p}`);
|
||||
}
|
||||
for (const text of OUTPUTS) assert.equal(leaks(text), false, "a key is in an output");
|
||||
},
|
||||
};
|
||||
return f;
|
||||
}
|
||||
|
||||
const ok = (r) => assert.equal(r.code, 0, r.stderr);
|
||||
const refusal = (r, code, words) => {
|
||||
assert.equal(r.code, 3, r.stderr);
|
||||
assert.equal(r.stdout, "");
|
||||
assert.match(r.stderr, new RegExp(`^mosaic: ${code}: ${words ?? ""}`));
|
||||
};
|
||||
|
||||
test("issue adds one key under the lock, writes the 0600 file, and records ids only", async (t) => {
|
||||
const f = await fixture(t);
|
||||
const before = [...f.state.keys];
|
||||
const r = await f.cli(["triage", "key", "issue"]);
|
||||
ok(r);
|
||||
const rec = f.record();
|
||||
assert.deepEqual(Object.keys(rec), ["version", "key", "keyId", "issuedAt", "rotateBy", "retiring"]);
|
||||
assert.match(rec.key, new RegExp(`^${f.prefix}[A-Za-z0-9_-]{43}$`));
|
||||
assert.equal(rec.keyId, id(rec.key));
|
||||
assert.equal(rec.issuedAt, iso(NOW));
|
||||
assert.equal(rec.rotateBy, "2026-12-18");
|
||||
assert.deepEqual(rec.retiring, []);
|
||||
assert.equal(r.stdout, `triage key issued: ${rec.keyId}, rotate by 2026-12-18\nfile: ${f.file}\n`);
|
||||
assert.deepEqual(f.state.keys, [...before, rec.key]);
|
||||
assert.equal(f.state.puts.length, 1);
|
||||
const file = fs.lstatSync(f.file), dir = fs.lstatSync(f.dir);
|
||||
assert.equal(file.mode & 0o777, 0o600); assert.equal(dir.mode & 0o777, 0o700);
|
||||
assert.equal(file.uid, process.getuid()); assert.equal(dir.uid, process.getuid());
|
||||
assert.deepEqual(f.receipts(), [{ op: "triage.key.issue", time: iso(NOW), addedKeyIds: [rec.keyId], removedKeyIds: [] }]);
|
||||
assert.equal(f.fenced(), false);
|
||||
assert.equal(fs.readFileSync(join(f.root, "proxy", "keys.generation"), "utf8"), "1\n");
|
||||
// A second issue refuses and changes nothing; rotate is the verb for that.
|
||||
const again = await f.cli(["triage", "key", "issue"]);
|
||||
refusal(again, "triage-key-issued", "a Triage key is already issued; use mosaic triage key rotate\n$");
|
||||
assert.equal(f.state.puts.length, 1);
|
||||
assert.deepEqual(f.record(), rec);
|
||||
f.scan();
|
||||
});
|
||||
|
||||
test("status reads the file and the list and writes nothing", async (t) => {
|
||||
const f = await fixture(t);
|
||||
const none = await f.cli(["triage", "key", "status"]);
|
||||
ok(none);
|
||||
assert.equal(none.stdout, `triage key: not issued\nfile: ${f.file}\nproxy: read\nnext: mosaic triage key issue\n`);
|
||||
ok(await f.cli(["triage", "key", "issue"]));
|
||||
const rec = f.record(), puts = f.state.puts.length, receipts = f.receipts().length;
|
||||
f.now = NOW + 3 * DAY + 4 * HOUR + 5 * MIN;
|
||||
const shown = await f.cli(["triage", "key", "status"]);
|
||||
ok(shown);
|
||||
assert.equal(shown.stdout, [
|
||||
"triage key: issued", `file: ${f.file}`, `key id: ${rec.keyId}`, `issued: ${iso(NOW)} (age 3d 4h 5m)`,
|
||||
"rotate by: 2026-12-18", "proxy: holds the key", "next: nothing", "",
|
||||
].join("\n"));
|
||||
f.now = Date.parse("2026-12-18T00:00:00.000Z");
|
||||
assert.match((await f.cli(["triage", "key", "status"])).stdout, /rotate by: 2026-12-18 \(due\)\n[^]*next: mosaic triage key rotate\n$/);
|
||||
f.now = NOW + HOUR;
|
||||
f.state.keys = f.state.keys.filter((k) => k !== rec.key);
|
||||
assert.match((await f.cli(["triage", "key", "status"])).stdout, /proxy: does not hold the key\nnext: mosaic triage key rotate\n$/);
|
||||
assert.equal(f.state.puts.length, puts);
|
||||
assert.equal(f.receipts().length, receipts);
|
||||
assert.equal(fs.readdirSync(join(f.root, "proxy")).includes("mosaic-keys.lock"), false);
|
||||
f.scan();
|
||||
});
|
||||
|
||||
test("rotate adds a new key and keeps the old one for the bus grace; retire removes it once due", async (t) => {
|
||||
const f = await fixture(t);
|
||||
refusal(await f.cli(["triage", "key", "rotate"]), "triage-key-not-issued", "no Triage key is issued; use mosaic triage key issue\n$");
|
||||
assert.equal(f.state.puts.length, 0);
|
||||
ok(await f.cli(["triage", "key", "issue"]));
|
||||
const first = f.record();
|
||||
f.now = NOW + HOUR;
|
||||
const r = await f.cli(["triage", "key", "rotate"]);
|
||||
ok(r);
|
||||
const second = f.record();
|
||||
const retireAt = iso(NOW + HOUR + 120 * MIN);
|
||||
assert.notEqual(second.key, first.key);
|
||||
assert.equal(second.issuedAt, iso(NOW + HOUR));
|
||||
assert.deepEqual(second.retiring, [{ key: first.key, keyId: first.keyId, issuedAt: first.issuedAt, retireAt }]);
|
||||
assert.equal(r.stdout, `triage key rotated: ${second.keyId}, rotate by 2026-12-18\nold key ${first.keyId} stays live until mosaic triage key retire runs at or after ${retireAt}\n`);
|
||||
assert.deepEqual(f.triageKeys(), [first.key, second.key]);
|
||||
f.now = NOW + HOUR + 119 * MIN;
|
||||
const early = await f.cli(["triage", "key", "retire"]);
|
||||
ok(early);
|
||||
assert.equal(early.stdout, "triage key retire: nothing to remove\n");
|
||||
assert.equal(f.state.puts.length, 2);
|
||||
assert.match((await f.cli(["triage", "key", "status"])).stdout, new RegExp(`retiring: ${first.keyId}, live until a retire after ${retireAt}\\n[^]*next: nothing\\n$`));
|
||||
f.now = NOW + HOUR + 120 * MIN;
|
||||
assert.match((await f.cli(["triage", "key", "status"])).stdout, new RegExp(`retiring: ${first.keyId}, live until a retire after ${retireAt} \\(due\\)\\n[^]*next: mosaic triage key retire\\n$`));
|
||||
const done = await f.cli(["triage", "key", "retire"]);
|
||||
ok(done);
|
||||
assert.equal(done.stdout, `triage key retire: removed ${first.keyId}\n`);
|
||||
assert.deepEqual(f.triageKeys(), [second.key]);
|
||||
assert.deepEqual(f.record(), { ...second, retiring: [] });
|
||||
assert.deepEqual(f.receipts().map((e) => e.op), ["triage.key.issue", "triage.key.rotate", "triage.key.retire"]);
|
||||
assert.deepEqual(f.receipts()[2].removedKeyIds, [first.keyId]);
|
||||
f.scan();
|
||||
});
|
||||
|
||||
test("rotate takes the grace from proxy.json, where the bus reads it, and drops an old key the proxy lost", async (t) => {
|
||||
const f = await fixture(t);
|
||||
f.system = { ...f.system, proxy: { ...f.system.proxy, rotateGraceMinutes: 30 } };
|
||||
ok(await f.cli(["triage", "key", "issue"]));
|
||||
const first = f.record();
|
||||
ok(await f.cli(["triage", "key", "rotate"]));
|
||||
const second = f.record();
|
||||
assert.equal(second.retiring[0].retireAt, iso(NOW + 30 * MIN));
|
||||
// The proxy lost the current key: rotate drops it at once, as the bus does for an agent key.
|
||||
f.state.keys = f.state.keys.filter((k) => k !== second.key);
|
||||
const r = await f.cli(["triage", "key", "rotate"]);
|
||||
ok(r);
|
||||
assert.match(r.stdout, new RegExp(`old key ${second.keyId} was not in the proxy list; nothing to retire\\n$`));
|
||||
assert.deepEqual(f.record().retiring.map((e) => e.keyId), [first.keyId]);
|
||||
// A retiring key past its time, or no longer in the list, leaves on the next rotate.
|
||||
f.now = NOW + 31 * MIN;
|
||||
ok(await f.cli(["triage", "key", "rotate"]));
|
||||
const last = f.record();
|
||||
assert.deepEqual(last.retiring.map((e) => e.keyId), [id(f.triageKeys()[0])]);
|
||||
assert.equal(f.triageKeys().includes(first.key), false);
|
||||
assert.equal(f.triageKeys().length, 2);
|
||||
f.scan();
|
||||
});
|
||||
|
||||
test("lifetime and grace match the bus's own ModelKeys for the same clock and proxy settings", async (t) => {
|
||||
const runtime = fs.readFileSync(join(REPO, "packages/bus/src/runtime.mjs"), "utf8");
|
||||
const literal = runtime.match(/rotateGraceMinutes: proxy\.rotateGraceMinutes \?\? (\d+)/);
|
||||
assert.ok(literal, "the bus no longer reads rotateGraceMinutes as this test expects");
|
||||
assert.equal(Number(literal[1]), DEFAULT_ROTATE_GRACE_MINUTES);
|
||||
for (const grace of [undefined, 45]) {
|
||||
const f = await fixture(t);
|
||||
if (grace !== undefined) f.system = { ...f.system, proxy: { ...f.system.proxy, rotateGraceMinutes: grace } };
|
||||
ok(await f.cli(["triage", "key", "issue"]));
|
||||
f.now = NOW + 3 * HOUR;
|
||||
ok(await f.cli(["triage", "key", "rotate"]));
|
||||
const ours = f.record();
|
||||
// The bus, driven the way runtime.mjs drives it, on its own in-memory list.
|
||||
let keys = [`msk-${tag(f.configDir)}-sentinel-${b64()}`], record = null, now = NOW;
|
||||
const credentials = { registerModel() {}, modelRecord: () => record, writeModel: (_, r) => { record = r; }, status: () => [] };
|
||||
const client = { getKeys: async () => [...keys], putKeys: async (k) => { keys = [...k]; return { ok: true }; } };
|
||||
const bus = new ModelKeys({ credentials, client, configDir: f.configDir, baseUrl: "http://127.0.0.1:1", now: () => now,
|
||||
...(grace === undefined ? {} : { rotateGraceMinutes: f.system.proxy.rotateGraceMinutes ?? Number(literal[1]) }) });
|
||||
await bus.reconcile();
|
||||
await bus.ensure("demo/x");
|
||||
assert.equal(record.rotateBy, rotateByFor(NOW));
|
||||
now = NOW + 3 * HOUR;
|
||||
await bus.rotate("demo/x");
|
||||
assert.equal(ours.rotateBy, record.rotateBy);
|
||||
assert.equal(ours.retiring[0].retireAt, record.retiring[0].retireAt);
|
||||
assert.equal(ours.retiring[0].retireAt, retireAtFor(now, f.system.proxy));
|
||||
f.scan();
|
||||
}
|
||||
});
|
||||
|
||||
test("an agent caller is refused before anything is read, for every key verb", async (t) => {
|
||||
const f = await fixture(t);
|
||||
for (const marker of AGENT_MARKERS) {
|
||||
for (const verb of VERBS) {
|
||||
const r = await f.cli(["triage", "key", verb], { env: { ...f.env(), [marker]: "1" } });
|
||||
assert.equal(r.code, 3, marker);
|
||||
assert.equal(r.stderr, `mosaic: refused: this runs only from a human shell, outside any agent run (found ${marker} in the environment)\n`);
|
||||
}
|
||||
const saved = process.env[marker];
|
||||
process.env[marker] = "1";
|
||||
try {
|
||||
for (const verb of VERBS) {
|
||||
const r = await f.cli(["triage", "key", verb]);
|
||||
assert.equal(r.code, 3, marker);
|
||||
assert.match(r.stderr, new RegExp(`found ${marker} in the environment`));
|
||||
}
|
||||
} finally { if (saved === undefined) delete process.env[marker]; else process.env[marker] = saved; }
|
||||
}
|
||||
assert.equal(f.state.gets, 0);
|
||||
assert.equal(f.state.puts.length, 0);
|
||||
assert.equal(fs.existsSync(f.dir), false);
|
||||
assert.deepEqual(fs.readdirSync(join(f.root, "proxy")), []);
|
||||
});
|
||||
|
||||
test("usage: unknown key verbs, options and arguments exit 4 and echo no value", async (t) => {
|
||||
const f = await fixture(t);
|
||||
for (const argv of [["triage", "key"], ["triage", "key", "nope"], ["triage", "key", "issue", "extra"], ["triage", "key", "status", "--json"]]) {
|
||||
const r = await f.cli(argv);
|
||||
assert.equal(r.code, 4, argv.join(" "));
|
||||
assert.equal(r.stdout, "");
|
||||
assert.match(r.stderr, /\nusage: mosaic triage route show [^\n]*\| key issue \| rotate \| retire \| status\n$/);
|
||||
}
|
||||
const r = await f.cli(["triage", "key", "rotate", "--key", "secret-value"]);
|
||||
assert.equal(r.code, 4);
|
||||
assert.match(r.stderr, /^mosaic: unknown option\n/);
|
||||
assert.doesNotMatch(r.stderr, /secret-value/);
|
||||
assert.equal(f.state.gets, 0);
|
||||
});
|
||||
|
||||
test("an unsafe key store refuses every verb before any proxy write", async (t) => {
|
||||
const uid = process.getuid();
|
||||
const cases = [
|
||||
["file mode", (f) => fs.chmodSync(f.file, 0o644), {}, "the key file mode is not 0600"],
|
||||
["file owner", () => {}, { triageKeyFileUid: uid + 1 }, "the key file is not owned by this user"],
|
||||
["file link", (f) => { fs.renameSync(f.file, join(f.dir, "real")); fs.symlinkSync(join(f.dir, "real"), f.file); }, {}, "the key file is a link"],
|
||||
["file is a directory", (f) => { fs.rmSync(f.file); fs.mkdirSync(f.file, { mode: 0o700 }); }, {}, "the key file is not a regular file"],
|
||||
["file is a FIFO", (f) => { fs.rmSync(f.file); assert.equal(spawnSync("mkfifo", ["-m", "600", f.file]).status, 0); }, {}, "the key file is not a regular file"],
|
||||
["hard link", (f) => fs.linkSync(f.file, join(f.dir, "second")), {}, "the key file has more than one link"],
|
||||
["not JSON", (f) => fs.writeFileSync(f.file, "{"), {}, "the key file is not JSON"],
|
||||
["extra field", (f) => fs.writeFileSync(f.file, JSON.stringify({ ...f.record(), extra: 1 })), {}, "the key file does not match the key shape"],
|
||||
["another instance's key", (f) => { const r = f.record(); const key = `mst-${tag("/elsewhere")}-triage-${b64()}`; fs.writeFileSync(f.file, JSON.stringify({ ...r, key, keyId: id(key) })); }, {}, "the key file does not match the key shape"],
|
||||
["wrong key id", (f) => fs.writeFileSync(f.file, JSON.stringify({ ...f.record(), keyId: "000000000000" })), {}, "the key file does not match the key shape"],
|
||||
["too large", (f) => fs.writeFileSync(f.file, " ".repeat(64 * 1024 + 1)), {}, "the key file is too large"],
|
||||
["triage directory mode", (f) => fs.chmodSync(f.dir, 0o755), {}, "the triage secrets directory mode is not 0700"],
|
||||
["triage directory link", (f) => { fs.renameSync(f.dir, join(f.configDir, "secrets", "moved")); fs.symlinkSync(join(f.configDir, "secrets", "moved"), f.dir); }, {}, "the triage secrets directory is a link"],
|
||||
["triage directory is a file", (f) => { fs.rmSync(f.dir, { recursive: true }); fs.writeFileSync(f.dir, "", { mode: 0o600 }); }, {}, "the triage secrets directory is not a directory"],
|
||||
["secrets mode", (f) => fs.chmodSync(join(f.configDir, "secrets"), 0o750), {}, "the secrets directory mode is not 0700"],
|
||||
["owner", () => {}, { triageKeyUid: uid + 1 }, "the secrets directory is not owned by this user"],
|
||||
];
|
||||
for (const [name, spoil, deps, words] of cases) {
|
||||
const f = await fixture(t);
|
||||
ok(await f.cli(["triage", "key", "issue"]));
|
||||
spoil(f);
|
||||
const keys = [...f.state.keys], puts = f.state.puts.length;
|
||||
for (const verb of VERBS) {
|
||||
const r = await f.cli(["triage", "key", verb], { deps });
|
||||
refusal(r, "triage-key-unreadable", `the Triage key store is unreadable: ${words.replace(/[.()]/g, "\\$&")}\n$`);
|
||||
assert.equal(f.state.puts.length, puts, `${name}: ${verb}`);
|
||||
}
|
||||
assert.deepEqual(f.state.keys, keys);
|
||||
// The spoils above move or link the key file itself; those copies are the key file.
|
||||
f.scan([join(f.dir, "real"), join(f.dir, "second"), join(f.configDir, "secrets", "moved", "model")]);
|
||||
}
|
||||
// No secrets directory at all: status says not issued; issue refuses before the proxy changes.
|
||||
const f = await fixture(t);
|
||||
fs.rmSync(join(f.configDir, "secrets"), { recursive: true });
|
||||
fs.mkdirSync(join(f.configDir, "secrets"), { mode: 0o700 });
|
||||
fs.writeFileSync(join(f.configDir, "secrets", "proxy-management.key"), MANAGE, { mode: 0o600 });
|
||||
fs.chmodSync(join(f.configDir, "secrets"), 0o755);
|
||||
refusal(await f.cli(["triage", "key", "issue"]), "triage-key-unreadable", "the Triage key store is unreadable: the secrets directory mode is not 0700\n$");
|
||||
assert.equal(f.state.puts.length, 0);
|
||||
});
|
||||
|
||||
test("the instance proxy lock, the fence, the sentinel and the proxy itself each refuse plainly", async (t) => {
|
||||
// Lock held by a live process: busy after the lock's wait, and nothing changes.
|
||||
const f = await fixture(t);
|
||||
const lockFile = join(f.root, "proxy", "mosaic-keys.lock");
|
||||
ok(await f.cli(["triage", "key", "status"]));
|
||||
fs.writeFileSync(lockFile, `${JSON.stringify({ pid: process.pid, token: "a".repeat(32) })}\n`, { mode: 0o600 });
|
||||
const busy = await f.cli(["triage", "key", "issue"]);
|
||||
refusal(busy, "proxy-lock-busy", "another proxy writer holds the instance proxy lock; try again when it ends, and if no writer is running, see mosaic proxy fence status\n$");
|
||||
assert.equal(f.state.gets, 1);
|
||||
// A lock left by a process that is gone is reaped, and the verb goes ahead.
|
||||
const dead = spawnSync(process.execPath, ["-e", ""]).pid;
|
||||
fs.writeFileSync(lockFile, `${JSON.stringify({ pid: dead, token: "b".repeat(32) })}\n`, { mode: 0o600 });
|
||||
ok(await f.cli(["triage", "key", "issue"]));
|
||||
assert.equal(fs.existsSync(lockFile), false);
|
||||
assert.equal(f.triageKeys().length, 1);
|
||||
fs.rmSync(f.file);
|
||||
f.state.keys = f.state.keys.filter((k) => !k.startsWith(f.prefix));
|
||||
// A set fence: every write refuses, status names it and the way out.
|
||||
const putsBefore = f.state.puts.length;
|
||||
fs.writeFileSync(join(f.root, "proxy", "mosaic-keys.uncertain"), "{}\n", { mode: 0o600 });
|
||||
for (const verb of ["issue", "rotate", "retire"]) refusal(await f.cli(["triage", "key", verb]), "proxy-uncertain");
|
||||
assert.match((await f.cli(["triage", "key", "status"])).stdout, /proxy fence: set, an earlier proxy write has an uncertain outcome\nnext: restart the proxy, then mosaic proxy fence clear --proxy-restarted\n$/);
|
||||
assert.equal(f.state.puts.length, putsBefore);
|
||||
// No sentinel for this instance (another instance's does not count).
|
||||
const g = await fixture(t, { sentinel: false });
|
||||
g.state.keys.push(`msk-${tag("/elsewhere")}-sentinel-${b64()}`);
|
||||
refusal(await g.cli(["triage", "key", "issue"]), "triage-key-proxy-open");
|
||||
assert.match((await g.cli(["triage", "key", "status"])).stdout, /proxy: its key list has no sentinel for this instance\nnext: check the proxy with mosaic proxy status\n$/);
|
||||
assert.equal(g.state.puts.length, 0);
|
||||
// No proxy set up, no management key, proxy down.
|
||||
const h = await fixture(t);
|
||||
const bare = { dataRoot: h.root };
|
||||
refusal(await h.cli(["triage", "key", "issue"], { system: bare }), "triage-key-no-proxy");
|
||||
assert.equal((await h.cli(["triage", "key", "status"], { system: bare })).stdout,
|
||||
`triage key: not issued\nfile: ${h.file}\nproxy: not set up for this instance (no proxy.json)\nnext: set up the model proxy first (mosaic proxy init)\n`);
|
||||
fs.chmodSync(join(h.configDir, "secrets", "proxy-management.key"), 0o644);
|
||||
refusal(await h.cli(["triage", "key", "issue"]), "proxy-management-key-invalid");
|
||||
fs.chmodSync(join(h.configDir, "secrets", "proxy-management.key"), 0o600);
|
||||
h.server.closeAllConnections(); await new Promise((resolve) => h.server.close(resolve));
|
||||
h.server.close = (cb) => cb();
|
||||
refusal(await h.cli(["triage", "key", "issue"]), "proxy-client-down");
|
||||
assert.match((await h.cli(["triage", "key", "status"])).stdout, /proxy: not read \(proxy-client-down\)\n/);
|
||||
assert.equal(fs.existsSync(h.file), false);
|
||||
f.scan(); g.scan(); h.scan();
|
||||
});
|
||||
|
||||
test("a write the proxy does not confirm keeps the fence; a list that moves mid-verb is not overwritten", async (t) => {
|
||||
const f = await fixture(t);
|
||||
f.state.ignorePut = true;
|
||||
refusal(await f.cli(["triage", "key", "issue"]), "triage-key-unconfirmed");
|
||||
assert.equal(f.fenced(), true);
|
||||
assert.equal(fs.existsSync(f.file), false);
|
||||
// The store's directory was made and checked before the proxy changed, not after.
|
||||
assert.equal(fs.lstatSync(f.dir).mode & 0o777, 0o700);
|
||||
assert.deepEqual(f.receipts(), []);
|
||||
// A PUT the proxy takes but whose read-back has lost this instance's sentinel is not confirmed.
|
||||
const s = await fixture(t);
|
||||
s.state.dropSentinelOnPut = true;
|
||||
refusal(await s.cli(["triage", "key", "issue"]), "triage-key-unconfirmed");
|
||||
assert.equal(s.fenced(), true);
|
||||
assert.equal(fs.existsSync(s.file), false);
|
||||
s.scan();
|
||||
const g = await fixture(t);
|
||||
g.state.onGet = (n) => { if (n === 1) g.state.keys = [...g.state.keys, "late-foreign-key"]; };
|
||||
refusal(await g.cli(["triage", "key", "issue"]), "triage-key-raced");
|
||||
assert.equal(g.state.puts.length, 0);
|
||||
assert.equal(g.fenced(), false);
|
||||
f.scan(); g.scan();
|
||||
});
|
||||
|
||||
test("keys outside the Triage namespace are never touched; Triage keys the file does not hold are removed", async (t) => {
|
||||
const f = await fixture(t);
|
||||
const agent = `msk-${tag(f.configDir)}-${b64()}`, other = `mst-${tag("/elsewhere")}-triage-${b64()}`;
|
||||
const orphan = `${f.prefix}${b64()}`, odd = `${f.prefix}short`;
|
||||
f.state.keys.push(agent, other, orphan, odd);
|
||||
const foreign = f.state.keys.filter((k) => k !== orphan && k !== odd);
|
||||
const r = await f.cli(["triage", "key", "issue"]);
|
||||
ok(r);
|
||||
// A malformed name under the Triage prefix is still Triage's, as the bus treats its own prefix.
|
||||
assert.match(r.stdout, new RegExp(`removed keys this file did not hold: ${id(orphan)}, ${id(odd)}\\n$`));
|
||||
for (const verb of ["rotate", "retire"]) { f.now += 3 * HOUR; ok(await f.cli(["triage", "key", verb])); }
|
||||
for (const k of foreign) assert.ok(f.state.keys.includes(k), "a key outside the namespace left the list");
|
||||
assert.equal(f.state.keys.includes(orphan), false);
|
||||
assert.equal(f.state.keys.includes(odd), false);
|
||||
assert.deepEqual(f.triageKeys(), [f.record().key]);
|
||||
f.scan();
|
||||
});
|
||||
|
||||
test("crash after the PUT, before the read-back: the fence holds; status says so; clear, then issue recovers", async (t) => {
|
||||
const f = await fixture(t);
|
||||
const crashed = await f.crash(["triage", "key", "issue"], "put");
|
||||
assert.equal(crashed.signal, "SIGKILL");
|
||||
assert.equal(f.fenced(), true);
|
||||
assert.equal(f.triageKeys().length, 1);
|
||||
assert.equal(fs.existsSync(f.file), false);
|
||||
const status = await f.cli(["triage", "key", "status"]);
|
||||
ok(status);
|
||||
assert.match(status.stdout, new RegExp(`proxy fence: set[^\\n]*\\nkeys in the Triage namespace the file does not hold: ${id(f.triageKeys()[0])}\\nnext: restart the proxy, then mosaic proxy fence clear --proxy-restarted\\n$`));
|
||||
refusal(await f.cli(["triage", "key", "issue"]), "proxy-uncertain");
|
||||
ok(await f.cli(["proxy", "fence", "clear", "--proxy-restarted"]));
|
||||
assert.equal(f.fenced(), false);
|
||||
const stray = f.triageKeys()[0];
|
||||
const r = await f.cli(["triage", "key", "issue"]);
|
||||
ok(r);
|
||||
assert.match(r.stdout, new RegExp(`removed keys this file did not hold: ${id(stray)}\\n$`));
|
||||
assert.deepEqual(f.triageKeys(), [f.record().key]);
|
||||
f.scan();
|
||||
});
|
||||
|
||||
test("crash after the read-back, before the file write: no fence; status names the stray key; issue removes it", async (t) => {
|
||||
const f = await fixture(t);
|
||||
assert.equal((await f.crash(["triage", "key", "issue"], "write")).signal, "SIGKILL");
|
||||
assert.equal(f.fenced(), false);
|
||||
assert.equal(fs.existsSync(f.file), false);
|
||||
const [stray] = f.triageKeys();
|
||||
assert.match((await f.cli(["triage", "key", "status"])).stdout,
|
||||
new RegExp(`^triage key: not issued\\n[^]*keys in the Triage namespace the file does not hold: ${id(stray)}\\nnext: mosaic triage key issue\\n$`));
|
||||
ok(await f.cli(["triage", "key", "issue"]));
|
||||
assert.deepEqual(f.triageKeys(), [f.record().key]);
|
||||
// The same crash in rotate: the file keeps the old key, the stray new one is named, retire removes it.
|
||||
const held = f.record();
|
||||
assert.equal((await f.crash(["triage", "key", "rotate"], "write")).signal, "SIGKILL");
|
||||
assert.deepEqual(f.record(), held);
|
||||
const strayNew = f.triageKeys().find((k) => k !== held.key);
|
||||
assert.match((await f.cli(["triage", "key", "status"])).stdout,
|
||||
new RegExp(`proxy: holds the key\\nkeys in the Triage namespace the file does not hold: ${id(strayNew)}\\nnext: mosaic triage key retire\\n$`));
|
||||
const retired = await f.cli(["triage", "key", "retire"]);
|
||||
ok(retired);
|
||||
assert.equal(retired.stdout, `triage key retire: removed ${id(strayNew)}\n`);
|
||||
assert.deepEqual(f.triageKeys(), [held.key]);
|
||||
f.scan();
|
||||
});
|
||||
|
||||
test("file written but the old key not yet retired: status shows it due; a crash inside retire recovers on the next retire", async (t) => {
|
||||
const f = await fixture(t);
|
||||
ok(await f.cli(["triage", "key", "issue"]));
|
||||
const first = f.record();
|
||||
ok(await f.cli(["triage", "key", "rotate"]));
|
||||
f.now = NOW + 121 * MIN;
|
||||
assert.match((await f.cli(["triage", "key", "status"])).stdout, new RegExp(`retiring: ${first.keyId}, live until a retire after [^\\n]* \\(due\\)\\n[^]*next: mosaic triage key retire\\n$`));
|
||||
// retire dies after the proxy dropped the key but before the file was rewritten.
|
||||
assert.equal((await f.crash(["triage", "key", "retire"], "write")).signal, "SIGKILL");
|
||||
assert.equal(f.state.keys.includes(first.key), false);
|
||||
assert.equal(f.record().retiring.length, 1);
|
||||
assert.match((await f.cli(["triage", "key", "status"])).stdout, /\(due\) \(not in the proxy list\)\n[^]*next: mosaic triage key retire\n$/);
|
||||
const puts = f.state.puts.length;
|
||||
const r = await f.cli(["triage", "key", "retire"]);
|
||||
ok(r);
|
||||
assert.equal(f.state.puts.length, puts);
|
||||
assert.deepEqual(f.record().retiring, []);
|
||||
// And a retire killed after its PUT leaves the fence, like any other write.
|
||||
ok(await f.cli(["triage", "key", "rotate"]));
|
||||
f.now += 121 * MIN;
|
||||
assert.equal((await f.crash(["triage", "key", "retire"], "put")).signal, "SIGKILL");
|
||||
assert.equal(f.fenced(), true);
|
||||
refusal(await f.cli(["triage", "key", "retire"]), "proxy-uncertain");
|
||||
ok(await f.cli(["proxy", "fence", "clear", "--proxy-restarted"]));
|
||||
ok(await f.cli(["triage", "key", "retire"]));
|
||||
assert.deepEqual(f.record().retiring, []);
|
||||
assert.deepEqual(f.triageKeys(), [f.record().key]);
|
||||
f.scan();
|
||||
});
|
||||
|
||||
test("a file that cannot be written after the proxy changed is reported with its recovery", async (t) => {
|
||||
const f = await fixture(t);
|
||||
ok(await f.cli(["triage", "key", "status"]));
|
||||
fs.mkdirSync(f.dir, { mode: 0o700 });
|
||||
const r = await f.cli(["triage", "key", "issue"], { deps: { triageKeyHook: (stage) => { if (stage === "write") fs.chmodSync(f.dir, 0o755); } } });
|
||||
refusal(r, "triage-key-unwritten", "the proxy list changed but ");
|
||||
assert.match(r.stderr, /run mosaic triage key issue again, which clears what this run left\n$/);
|
||||
assert.equal(f.triageKeys().length, 1);
|
||||
fs.chmodSync(f.dir, 0o700);
|
||||
ok(await f.cli(["triage", "key", "issue"]));
|
||||
assert.deepEqual(f.triageKeys(), [f.record().key]);
|
||||
f.scan();
|
||||
});
|
||||
|
||||
test("crash after the temp file, before its rename: status counts it; the next verb removes it", async (t) => {
|
||||
const f = await fixture(t);
|
||||
assert.equal((await f.crash(["triage", "key", "issue"], "rename")).signal, "SIGKILL");
|
||||
assert.equal(fs.existsSync(f.file), false);
|
||||
const temps = fs.readdirSync(f.dir).filter((n) => n.endsWith(".tmp"));
|
||||
assert.equal(temps.length, 1);
|
||||
assert.match(temps[0], /^\.model\.\d+\.[0-9a-f]{12}\.tmp$/);
|
||||
// The temp file holds the key the proxy now has; that is why it must go.
|
||||
const [stray] = f.triageKeys();
|
||||
assert.ok(fs.readFileSync(join(f.dir, temps[0]), "utf8").includes(stray));
|
||||
const status = await f.cli(["triage", "key", "status"]);
|
||||
ok(status);
|
||||
assert.match(status.stdout, new RegExp(`leftover temp files in the triage secrets directory: 1 \\(the next issue, rotate or retire removes them\\)\\nkeys in the Triage namespace the file does not hold: ${id(stray)}\\nnext: mosaic triage key issue\\n$`));
|
||||
// Something else in the directory is not a leftover and stays.
|
||||
fs.writeFileSync(join(f.dir, "notes.tmp"), "", { mode: 0o600 });
|
||||
ok(await f.cli(["triage", "key", "issue"]));
|
||||
assert.deepEqual(fs.readdirSync(f.dir).sort(), ["model", "notes.tmp"]);
|
||||
assert.deepEqual(f.triageKeys(), [f.record().key]);
|
||||
// Retire, with nothing else to do, still removes a leftover.
|
||||
assert.equal((await f.crash(["triage", "key", "rotate"], "rename")).signal, "SIGKILL");
|
||||
ok(await f.cli(["triage", "key", "retire"]));
|
||||
assert.deepEqual(fs.readdirSync(f.dir).sort(), ["model", "notes.tmp"]);
|
||||
assert.doesNotMatch((await f.cli(["triage", "key", "status"])).stdout, /leftover/);
|
||||
f.scan();
|
||||
});
|
||||
|
||||
test("the receipts file is checked before the proxy changes; a receipt that fails after is named", async (t) => {
|
||||
const receipts = (f) => join(f.root, "proxy", "mosaic-keys.receipts.jsonl");
|
||||
const spoils = [
|
||||
["mode", (f) => { fs.writeFileSync(receipts(f), "", { mode: 0o644 }); fs.chmodSync(receipts(f), 0o644); }, "the proxy receipts file mode is not 0600"],
|
||||
["link", (f) => { fs.writeFileSync(join(f.root, "elsewhere"), "", { mode: 0o600 }); fs.symlinkSync(join(f.root, "elsewhere"), receipts(f)); }, "the proxy receipts file cannot be opened"],
|
||||
["FIFO", (f) => assert.equal(spawnSync("mkfifo", ["-m", "600", receipts(f)]).status, 0), "the proxy receipts file cannot be opened"],
|
||||
];
|
||||
for (const [name, spoil, words] of spoils) {
|
||||
const f = await fixture(t);
|
||||
spoil(f);
|
||||
refusal(await f.cli(["triage", "key", "issue"]), "triage-key-receipts-unsafe", `${words}; nothing was changed\n$`);
|
||||
assert.equal(f.state.puts.length, 0, name);
|
||||
assert.equal(f.fenced(), false, name);
|
||||
assert.equal(fs.existsSync(f.file), false, name);
|
||||
f.scan();
|
||||
}
|
||||
// Safe before the change, unsafe at the receipt: the change stands and the refusal says so.
|
||||
const f = await fixture(t);
|
||||
const r = await f.cli(["triage", "key", "issue"], { deps: { triageKeyHook: (stage) => { if (stage === "receipt") fs.chmodSync(receipts(f), 0o644); } } });
|
||||
refusal(r, "triage-key-unrecorded", "the change was made but its receipt line was not written; run mosaic triage key status\n$");
|
||||
assert.deepEqual(f.triageKeys(), [f.record().key]);
|
||||
assert.equal(f.fenced(), false);
|
||||
assert.equal(fs.readFileSync(receipts(f), "utf8"), "");
|
||||
f.scan();
|
||||
});
|
||||
|
||||
test("a directory sync that fails after the rename is named, and the receipt is still written", async (t) => {
|
||||
const f = await fixture(t);
|
||||
const r = await f.cli(["triage", "key", "issue"], { deps: { triageKeyHook: (stage) => { if (stage === "sync") throw new Error("EIO"); } } });
|
||||
refusal(r, "triage-key-unsynced", `${f.file.replace(/[.]/g, "\\.")} was written but its directory could not be synced, so it may not survive a power loss; run mosaic triage key status\n$`);
|
||||
const rec = f.record();
|
||||
assert.deepEqual(f.triageKeys(), [rec.key]);
|
||||
assert.deepEqual(f.receipts().map((e) => e.addedKeyIds), [[rec.keyId]]);
|
||||
assert.equal(f.fenced(), false);
|
||||
assert.match((await f.cli(["triage", "key", "status"])).stdout, /proxy: holds the key\nnext: nothing\n$/);
|
||||
f.scan();
|
||||
});
|
||||
|
||||
test("an unexpected error prints fixed words, never the error's own text", async (t) => {
|
||||
const f = await fixture(t);
|
||||
const secret = `mst-${tag(f.configDir)}-triage-${b64()}`;
|
||||
SEEN.add(secret);
|
||||
// Outside the proxy write: the verb's own fallback line.
|
||||
const broken = { dataRoot: f.root, get proxy() { throw new Error(`boom ${secret}`); } };
|
||||
for (const verb of VERBS) {
|
||||
const r = await f.cli(["triage", "key", verb], { system: broken });
|
||||
assert.equal(r.code, 3, verb);
|
||||
assert.equal(r.stderr, "mosaic: triage-key-failed: the key verb stopped on an unexpected error; run mosaic triage key status\n");
|
||||
}
|
||||
// Inside the proxy write: the write's own fixed line, and the fence stays for a human.
|
||||
const r = await f.cli(["triage", "key", "issue"], { deps: { triageKeyHook: (stage) => { if (stage === "put") throw new Error(`boom ${secret}`); } } });
|
||||
refusal(r, "triage-key-failed", "the proxy write failed\n$");
|
||||
assert.equal(f.fenced(), true);
|
||||
f.scan();
|
||||
});
|
||||
|
||||
// The org bus reconciles its own `mso-` namespace at start and a no-org bus its `msk-` one; a
|
||||
// Triage key sits in neither, so both leave it. Stray keys in each namespace show the reconcile ran.
|
||||
test("an org bus that starts and reconciles, and a no-org reconcile, both leave the Triage key alone", async (t) => {
|
||||
const f = await fixture(t);
|
||||
ok(await f.cli(["triage", "key", "issue"]));
|
||||
const triage = f.record().key;
|
||||
const strayOrg = `mso-${tag(f.configDir)}-demo-key-${b64()}`;
|
||||
f.state.keys.push(strayOrg);
|
||||
const rolesDir = rolesCopy(f.root), system = systemFor(f.root), doc = businessDoc(f.root, "demo");
|
||||
doc.plugins = { ...doc.plugins, harness: ["pi"] };
|
||||
const file = writeJson(join(f.configDir, "businesses", "demo.json"), doc);
|
||||
assert.ok(file);
|
||||
const business = loadBusiness("demo", { dir: f.configDir, rolesDir }), resolved = {};
|
||||
for (const instance of Object.keys(business.roles)) resolved[instance] = resolveInstance({ system, business, instance });
|
||||
fs.mkdirSync(join(f.root, "data"), { mode: 0o700 });
|
||||
new Registry(join(f.root, "data"));
|
||||
const bus = await startBroker({ dataRoot: join(f.root, "data"), businesses: { demo: busOrganization(business, resolved) },
|
||||
organization: { dir: f.configDir, rolesDir, system }, proxy: { port: f.port, binary: "/fixture-unused" },
|
||||
org: { id: "demo" }, instanceDataRoot: f.root, instanceConfigDir: f.configDir });
|
||||
f.closers.push(() => bus.close());
|
||||
const orgKey = (await bus.modelPreflight({ business: "demo", role: "chief" })).key;
|
||||
assert.ok(orgKey.startsWith(`mso-${tag(f.configDir)}-demo-key-`));
|
||||
assert.equal(f.state.keys.includes(strayOrg), false, "the org reconcile did not run");
|
||||
assert.ok(f.state.keys.includes(triage), "the org bus removed the Triage key");
|
||||
// A no-org reconcile on the same instance.
|
||||
const strayAgent = `msk-${tag(f.configDir)}-${b64()}`;
|
||||
f.state.keys.push(strayAgent);
|
||||
const client = new ManagementClient({ baseUrl: `http://127.0.0.1:${f.port}`, key: MANAGE });
|
||||
const keys = new ModelKeys({ credentials: { status: () => [], modelRecord: () => null }, client, configDir: f.configDir, baseUrl: `http://127.0.0.1:${f.port}` });
|
||||
await keys.reconcile();
|
||||
assert.equal(f.state.keys.includes(strayAgent), false, "the no-org reconcile did not run");
|
||||
assert.ok(f.state.keys.includes(triage), "the no-org reconcile removed the Triage key");
|
||||
// Triage still works under the shared lock while the org bus runs, and leaves the org key.
|
||||
f.now += HOUR;
|
||||
ok(await f.cli(["triage", "key", "rotate"]));
|
||||
assert.ok(f.state.keys.includes(orgKey));
|
||||
assert.match((await f.cli(["triage", "key", "status"])).stdout, /proxy: holds the key\n/);
|
||||
f.scan();
|
||||
});
|
||||
|
||||
test("scripts/mosaic routes triage key status with the instance's own config directory", (t) => {
|
||||
const home = fs.mkdtempSync(join(tmpdir(), "tkm-"));
|
||||
t.after(() => fs.rmSync(home, { recursive: true, force: true }));
|
||||
const env = humanEnv({ HOME: home, MOSAIC_CONFIG: join(home, ".config/mosaic-tktest/config.json") });
|
||||
const setup = spawnSync(join(REPO, "scripts/bootstrap.sh"), ["--instance", "tktest"], { env, encoding: "utf8" });
|
||||
assert.equal(setup.status, 0, setup.stderr);
|
||||
const r = spawnSync(join(REPO, "scripts/mosaic"), ["triage", "key", "status"], { env, encoding: "utf8", timeout: 60000 });
|
||||
OUTPUTS.push(r.stdout, r.stderr);
|
||||
assert.equal(r.status, 0, r.stderr);
|
||||
assert.equal(r.stdout, `triage key: not issued\nfile: ${join(home, ".config/mosaic-tktest/secrets/triage/model")}\nproxy: not set up for this instance (no proxy.json)\nnext: set up the model proxy first (mosaic proxy init)\n`);
|
||||
const refused = spawnSync(join(REPO, "scripts/mosaic"), ["triage", "key", "issue"], { env: { ...env, CLAUDECODE: "1" }, encoding: "utf8", timeout: 60000 });
|
||||
assert.equal(refused.status, 3);
|
||||
assert.match(refused.stderr, /found CLAUDECODE in the environment/);
|
||||
});
|
||||
|
||||
test("no output of any verb in this file holds any key the stand-in saw", () => {
|
||||
assert.ok(SEEN.size >= 20, `only ${SEEN.size} keys were seen`);
|
||||
assert.ok(OUTPUTS.length >= 100);
|
||||
for (const text of OUTPUTS) assert.equal(leaks(text), false);
|
||||
});
|
||||
@@ -0,0 +1,149 @@
|
||||
// #1873 triage-route-store: `mosaic triage route show|set`, the owner's terminal verbs for the
|
||||
// Triage route file (packages/webui/src/triage-route-store.mjs). Human verbs: an agent run is
|
||||
// refused before anything is read (exit 3), as the other human verbs refuse it. `show` with no
|
||||
// file says the route is not set and exits 0.
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { run } from "../src/cli.mjs";
|
||||
import { AGENT_MARKERS } from "../src/transport.mjs";
|
||||
import { routePaths } from "../../webui/src/triage-route-store.mjs";
|
||||
import { humanEnv, io } from "./helpers.mjs";
|
||||
|
||||
const REPO = new URL("../../../", import.meta.url).pathname;
|
||||
const MOSAIC = join(REPO, "scripts/mosaic");
|
||||
const NOW = new Date("2026-11-18T12:00:00.000Z");
|
||||
const SET = ["triage", "route", "set", "--model", "glm-5", "--harness", "claude-code", "--cap", "200000", "--time-zone", "America/Chicago"];
|
||||
|
||||
function dataRoot(t) {
|
||||
const d = mkdtempSync(join(tmpdir(), "tr-"));
|
||||
t.after(() => rmSync(d, { recursive: true, force: true }));
|
||||
return d;
|
||||
}
|
||||
async function cli(argv, d, env = humanEnv()) {
|
||||
const s = io(env);
|
||||
const code = await run(argv, s, { system: { dataRoot: d }, now: () => NOW });
|
||||
return { code, stdout: s.out.text, stderr: s.err.text };
|
||||
}
|
||||
|
||||
test("show with no route says it is not set and exits 0", async (t) => {
|
||||
const d = dataRoot(t);
|
||||
assert.deepEqual(await cli(["triage", "route", "show"], d), { code: 0, stdout: "triage route: not set\n", stderr: "" });
|
||||
assert.equal(existsSync(routePaths(d).dir), false);
|
||||
});
|
||||
|
||||
test("set writes the route; show reads it back", async (t) => {
|
||||
const d = dataRoot(t);
|
||||
const set = await cli(SET, d);
|
||||
assert.equal(set.code, 0, set.stderr);
|
||||
assert.equal(set.stdout, "triage route set: revision 1\n");
|
||||
const shown = await cli(["triage", "route", "show"], d);
|
||||
assert.equal(shown.code, 0);
|
||||
assert.equal(shown.stdout, [
|
||||
"triage route: revision 1",
|
||||
"model: glm-5",
|
||||
"harness: claude-code",
|
||||
"cap: 200000 tokens a day, resets 00:00 America/Chicago, counted since 2026-11-18T12:00:00.000Z",
|
||||
"updated: 2026-11-18T12:00:00.000Z by owner",
|
||||
"",
|
||||
].join("\n"));
|
||||
const doc = JSON.parse(readFileSync(routePaths(d).file, "utf8"));
|
||||
assert.equal(doc.updatedBy, "owner");
|
||||
// --revision makes the write conditional on what show printed.
|
||||
const five = ["triage", "route", "set", "--model", "glm-5", "--harness", "claude-code", "--cap", "5", "--time-zone", "America/Chicago", "--revision", "1"];
|
||||
assert.equal((await cli(five, d)).code, 0);
|
||||
assert.equal(JSON.parse(readFileSync(routePaths(d).file, "utf8")).cap.limit, 5);
|
||||
const stale = await cli([...SET, "--revision", "1"], d);
|
||||
assert.equal(stale.code, 3);
|
||||
assert.match(stale.stderr, /^mosaic: triage-route-stale: /);
|
||||
});
|
||||
|
||||
test("refused input exits 2 or 4 and writes nothing", async (t) => {
|
||||
const d = dataRoot(t);
|
||||
const without = (flag) => { const a = [...SET]; const i = a.indexOf(flag); a.splice(i, 2); return a; };
|
||||
const swap = (flag, value) => { const a = [...SET]; a[a.indexOf(flag) + 1] = value; return a; };
|
||||
const cases = [
|
||||
[without("--model"), 4], [without("--harness"), 4], [without("--cap"), 4], [without("--time-zone"), 4],
|
||||
[swap("--cap", "-1"), 2], [swap("--cap", "1e3"), 2], [swap("--cap", "01"), 2], [swap("--cap", "9007199254740993"), 2],
|
||||
[swap("--time-zone", "Atlantis/Lost"), 2], [swap("--model", "Has Space"), 2],
|
||||
[swap("--harness", "nonesuch"), 2], [swap("--harness", "../pi"), 2],
|
||||
[[...SET, "--revision", "x"], 2], [[...SET, "--revision", "-1"], 2],
|
||||
[[...SET, "--cap", "5"], 4], [[...SET, "--unknown", "1"], 4], [[...SET, "extra"], 4],
|
||||
[["triage", "route", "show", "--model", "x"], 4], [["triage", "route", "show", "extra"], 4],
|
||||
[["triage", "route"], 4], [["triage", "route", "remove"], 4], [["triage"], 4], [["triage", "key"], 4],
|
||||
];
|
||||
for (const [argv, want] of cases) {
|
||||
const r = await cli(argv, d);
|
||||
assert.equal(r.code, want, `${argv.join(" ")}: ${r.stderr}`);
|
||||
assert.equal(r.stdout, "");
|
||||
}
|
||||
// An unknown option is named as one, and its value never reaches the message.
|
||||
const unknown = await cli([...SET, "--unknown", "secret-value"], d);
|
||||
assert.equal(unknown.code, 4);
|
||||
assert.match(unknown.stderr, /^mosaic: unknown option\n/);
|
||||
assert.doesNotMatch(unknown.stderr, /secret-value/);
|
||||
assert.equal(existsSync(routePaths(d).file), false);
|
||||
});
|
||||
|
||||
test("an unreadable store exits 3 on show and on set", async (t) => {
|
||||
const d = dataRoot(t);
|
||||
assert.equal((await cli(SET, d)).code, 0);
|
||||
chmodSync(routePaths(d).file, 0o644);
|
||||
for (const argv of [["triage", "route", "show"], [...SET, "--revision", "1"]]) {
|
||||
const r = await cli(argv, d);
|
||||
assert.equal(r.code, 3);
|
||||
assert.match(r.stderr, /^mosaic: triage-route-unreadable: the Triage route store is unreadable: route\.json mode is not 0600\n$/);
|
||||
}
|
||||
});
|
||||
|
||||
test("an agent caller is refused before anything is read, for show and set", async (t) => {
|
||||
const d = dataRoot(t);
|
||||
for (const marker of AGENT_MARKERS) {
|
||||
for (const argv of [["triage", "route", "show"], SET]) {
|
||||
const r = await cli(argv, d, { ...humanEnv(), [marker]: "1" });
|
||||
assert.equal(r.code, 3, marker);
|
||||
assert.equal(r.stderr, `mosaic: refused: this runs only from a human shell, outside any agent run (found ${marker} in the environment)\n`);
|
||||
}
|
||||
}
|
||||
assert.equal(existsSync(routePaths(d).dir), false);
|
||||
});
|
||||
|
||||
test("scripts/mosaic routes triage to the verb; a marker in the process env refuses", (t) => {
|
||||
const home = mkdtempSync(join(tmpdir(), "trm-"));
|
||||
t.after(() => rmSync(home, { recursive: true, force: true }));
|
||||
const env = humanEnv({ HOME: home, MOSAIC_CONFIG: join(home, ".config/mosaic-trtest/config.json") });
|
||||
const setup = spawnSync(join(REPO, "scripts/bootstrap.sh"), ["--instance", "trtest"], { env, encoding: "utf8" });
|
||||
assert.equal(setup.status, 0, setup.stderr);
|
||||
const mosaic = (args, extra = {}) => spawnSync(MOSAIC, args, { env: { ...env, ...extra }, encoding: "utf8", timeout: 60000 });
|
||||
const shown = mosaic(["triage", "route", "show"]);
|
||||
assert.equal(shown.status, 0, shown.stderr);
|
||||
assert.equal(shown.stdout, "triage route: not set\n");
|
||||
const set = mosaic(SET.slice(0));
|
||||
assert.equal(set.status, 0, set.stderr);
|
||||
assert.equal(set.stdout, "triage route set: revision 1\n");
|
||||
const dataDir = join(home, ".mosaic-trtest");
|
||||
assert.equal(JSON.parse(readFileSync(routePaths(dataDir).file, "utf8")).model, "glm-5");
|
||||
const refused = mosaic(["triage", "route", "show"], { CLAUDECODE: "1" });
|
||||
assert.equal(refused.status, 3);
|
||||
assert.match(refused.stderr, /found CLAUDECODE in the environment/);
|
||||
assert.equal(refused.stdout, "");
|
||||
});
|
||||
|
||||
test("a marker in this process's own environment refuses even when the command's env is clean", async (t) => {
|
||||
const d = dataRoot(t);
|
||||
for (const marker of AGENT_MARKERS) {
|
||||
const saved = process.env[marker];
|
||||
process.env[marker] = "1";
|
||||
try {
|
||||
for (const argv of [["triage", "route", "show"], SET]) {
|
||||
const r = await cli(argv, d, humanEnv());
|
||||
assert.equal(r.code, 3, marker);
|
||||
assert.match(r.stderr, new RegExp(`found ${marker} in the environment`));
|
||||
}
|
||||
} finally { if (saved === undefined) delete process.env[marker]; else process.env[marker] = saved; }
|
||||
}
|
||||
assert.equal(existsSync(routePaths(d).dir), false);
|
||||
});
|
||||
@@ -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}`;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
@@ -10,6 +10,7 @@ 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 +33,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 +91,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 +107,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 +132,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 +151,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);
|
||||
});
|
||||
@@ -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);
|
||||
|
||||
@@ -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]");
|
||||
});
|
||||
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"version": "8.0.42",
|
||||
"version": "8.0.79",
|
||||
"archive": {
|
||||
"asset": "CLIProxyAPI_8.0.42_linux_amd64.tar.gz",
|
||||
"url": "https://github.com/router-for-me/CLIProxyAPI/releases/download/v8.0.42/CLIProxyAPI_8.0.42_linux_amd64.tar.gz",
|
||||
"sha256": "1e10566fe0042ed5a9b80306cf296bf86a83de590cd967a8b177988b896c8d31"
|
||||
"asset": "CLIProxyAPI_8.0.79_linux_amd64.tar.gz",
|
||||
"url": "https://github.com/router-for-me/CLIProxyAPI/releases/download/v8.0.79/CLIProxyAPI_8.0.79_linux_amd64.tar.gz",
|
||||
"sha256": "7cbb9709ed939ff3684e10592a45cc538ee82d5da759daa7e18c7e1dc3b32acd"
|
||||
},
|
||||
"binary": {
|
||||
"sha256": "74661f8cf949977870231359e8bf4344e07946766a673c08fa25a3e5f72fc95e"
|
||||
"sha256": "4968bd27b04f813849add020b95a26dc7abf3e30810b97d291c0d88f495fe601"
|
||||
},
|
||||
"panel": {
|
||||
"version": "1.26.7",
|
||||
|
||||
@@ -56,8 +56,28 @@ uses a new temp root and private network namespace, with made-up credentials,
|
||||
and calls the page's bus verbs. If the page cannot pass against the new PIN,
|
||||
the upgrade stops; Sage takes Q46's fallback to Jason through Mos.
|
||||
|
||||
A running instance takes a new PIN in three steps, in this order. First run
|
||||
the new code: update the checkout or pinned copy the bus, Console and proxy
|
||||
unit start from. The proxy unit runs `run.mjs` and reads `PIN` from the tree
|
||||
it was installed from (`systemctl --user cat <unit>` shows it). If the new
|
||||
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 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
|
||||
Console as well so their status and Fetch use the new PIN. `proxy.json` keeps
|
||||
the path from the PIN it was created under. That path stays valid, and the
|
||||
proxy always runs the hash-verified install for the current PIN. Signed-in
|
||||
accounts in `<dataRoot>/proxy/auth/` carry over. From 8.0.55 the proxy also
|
||||
keeps a `kv.db` file in that directory, and 8.0.79 adds a `.codex-refresh/`
|
||||
directory there once a Codex account renews. The old release directory can be
|
||||
removed once the new one runs.
|
||||
|
||||
The native `/proxy` page uses human-only bus verbs; only the bus holds the
|
||||
management client. On PIN 8.0.42, OAuth credentials are listed under
|
||||
management client. On PIN 8.0.79, OAuth credentials are listed under
|
||||
`/v8/management/credentials` and priority is patched as `{name, priority}`.
|
||||
API-key groups are absent from that list: z.ai keys use the Claude-compatible
|
||||
provider configuration at `/v8/management/config/api-keys/claude`, whose keys
|
||||
@@ -128,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),
|
||||
|
||||
@@ -2,10 +2,12 @@
|
||||
// `mosaic proxy status|unit|init-lock|fence clear --restart`. Host actions run here; init and
|
||||
// the fence clear go to the bus, which decides them. Results and receipts carry labels, key IDs
|
||||
// and process ids only: never a key, token, absolute path or upstream text.
|
||||
import fs from 'node:fs';
|
||||
import { homedir } from 'node:os';
|
||||
import { fetchPinned } from './fetch.mjs';
|
||||
import { PIN } from './common.mjs';
|
||||
import { releaseState, initState, fenceState, initLockStatus, clearInitLock, appendReceipt, recentReceipts, createUnitControl, FILE_LABELS } from './admin.mjs';
|
||||
import { localRide, riddenOrgs } from './ride.mjs';
|
||||
import { releaseState, initState, fenceState, initLockStatus, clearInitLock, appendReceipt, recentReceipts, receiptsText, createUnitControl, FILE_LABELS } from './admin.mjs';
|
||||
|
||||
export const proxyCode = (error, fallback) => (/^proxy-[a-z-]+$/.test(error?.code ?? '') ? error.code : fallback);
|
||||
const failure = code => Object.assign(new Error(code), { code });
|
||||
@@ -13,7 +15,12 @@ const failure = code => Object.assign(new Error(code), { code });
|
||||
// `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.
|
||||
export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir = null, unit = null, fetch = fetchPinned, buses, orgs = async () => [], now = () => new Date(), env = process.env, pin = PIN }) {
|
||||
// #1873 ride-default: `riders()` lists the owner's active organizations for Ride all, read locally:
|
||||
// [{id, fixture, cliproxy, changes, call, status}] where `changes` is the text of the organization's
|
||||
// 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 }) {
|
||||
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 };
|
||||
@@ -30,6 +37,66 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
async function unitStatus() {
|
||||
try { return await control.status(); } catch (error) { return { unit: control.unit, installed: false, active: 'unknown', healthy: false, error: proxyCode(error, 'proxy-unit-failed') }; }
|
||||
}
|
||||
// The organizations this proxy's own receipts show cliproxy was or may have been on (riddenOrgs).
|
||||
// No receipt file yet is none; a file that fails receiptsText's check, or holds a line that does
|
||||
// not parse, is null, and localRide skips every organization without cliproxy as unreadable.
|
||||
function ridden() {
|
||||
const ids = riddenOrgs(receiptsText(dataRoot));
|
||||
return id => (ids === null ? null : ids.has(id));
|
||||
}
|
||||
const decide = (rider, wasRidden) => localRide({ ...rider, ridden: wasRidden(rider.id) });
|
||||
// Stopped is the organization admin page's test (org-admin bus stop): a bus status with no
|
||||
// process id. A status that cannot be read, or a rider with no status, is not stopped.
|
||||
async function stopped(rider) {
|
||||
if (typeof rider.status !== 'function') return false;
|
||||
try { return !Number.isSafeInteger((await rider.status())?.pid); } catch { return false; }
|
||||
}
|
||||
const failCode = error => (/^[a-z][a-z-]{0,63}$/.test(error?.code ?? '') ? error.code : 'failed');
|
||||
// Ride all, one organization: the local rules first, then the organization's bus, which must
|
||||
// answer and must hold the proxy client. The bus decides the write (plugins.set) as it would
|
||||
// for the owner on the Settings page. The answer names a reason, never bus or upstream text.
|
||||
// A ride-intent receipt is on disk before plugins.set is sent; with no receipt there is no write.
|
||||
// When plugins.set fails and the bus then shows no cliproxy, a ride-not-added receipt clears that
|
||||
// intent. Any other failure leaves it: the add may have happened.
|
||||
async function rideOne(rider, wasRidden) {
|
||||
const id = rider.id, local = decide(rider, wasRidden);
|
||||
// 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 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) {
|
||||
try {
|
||||
const after = await rider.call('plugins');
|
||||
if (Array.isArray(after?.plugins?.['model-provider']) && !after.plugins['model-provider'].includes('cliproxy')) receipt({ op: 'ride-not-added', org: id });
|
||||
} catch { /* unknown outcome: the intent stands */ }
|
||||
return { id, outcome: 'failed', reason: failCode(error) };
|
||||
}
|
||||
return { id, outcome: 'added' };
|
||||
}
|
||||
async function rideSummary() {
|
||||
// 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; }
|
||||
}
|
||||
function nextSteps({ release, init, unitState, fence, lock }) {
|
||||
if (!release.fetched || release.problem) return ['fetch'];
|
||||
if (lock.present) return lock.clearable ? ['init-lock-clear'] : ['init-lock-wait'];
|
||||
@@ -54,6 +121,10 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
const lock = init.lock ? initLockStatus({ configDir, dataRoot, buses: await buses() }) : { present: false, clearable: false, blockers: [] };
|
||||
let organizations = null;
|
||||
try { organizations = await orgs(); } catch { organizations = null; }
|
||||
const ride = await rideSummary();
|
||||
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');
|
||||
return {
|
||||
release: { version: release.version, panelVersion: release.panelVersion, fetched: release.fetched, verified: release.verified, problem: release.problem },
|
||||
init: {
|
||||
@@ -71,7 +142,8 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
// Ruling 8: the system config holds one proxy per instance; an organization's own proxy
|
||||
// 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.' },
|
||||
next: nextSteps({ release, init, unitState, fence, lock }),
|
||||
ride,
|
||||
next,
|
||||
};
|
||||
},
|
||||
// Runs in the background; the page polls status for its step and receipt.
|
||||
@@ -133,6 +205,55 @@ export function createProxyAdmin({ dataRoot, configDir, instance = null, unitDir
|
||||
return { cleared: result.cleared === true, receipt: receipt({ op: 'fence.clear', cleared: result.cleared === true, evidence: result.evidence ?? null }) };
|
||||
});
|
||||
},
|
||||
// Ride all: adds cliproxy to each organization that can take it, and names every one it skips.
|
||||
// Fixture organizations are listed, not changed: moving one spends real tokens, so it is a
|
||||
// separate confirmed action (rideMove).
|
||||
async rideAll() {
|
||||
return exclusive('ride-all', async () => {
|
||||
const list = await riders(), wasRidden = ridden();
|
||||
const results = [];
|
||||
for (const rider of list) results.push(await rideOne(rider, wasRidden));
|
||||
const added = results.filter(r => r.outcome === 'added').map(r => r.id);
|
||||
const skipped = results.filter(r => r.outcome === 'skipped' || r.outcome === 'failed').map(r => ({ id: r.id, reason: r.reason }));
|
||||
const fixtures = results.filter(r => r.outcome === 'fixture').map(r => ({ id: r.id, running: r.running }));
|
||||
const already = results.filter(r => r.outcome === 'already').map(r => r.id);
|
||||
// Each add already left its ride-intent receipt, so a summary that cannot be written still
|
||||
// answers what happened, with no receipt.
|
||||
let summary = null;
|
||||
try { summary = receipt({ op: 'ride-all', added, skipped, fixtures: fixtures.map(f => f.id) }); } catch { summary = null; }
|
||||
return { added, already, skipped, fixtures, receipt: summary };
|
||||
});
|
||||
},
|
||||
// Moves one stopped fixture organization to the owner's models. Its bus is refused at start
|
||||
// beside a real proxy, so the organization service writes its document (the same whole-document
|
||||
// update the organization's admin page uses); the owner starts its bus afterwards.
|
||||
async rideMove({ org, principal } = {}) {
|
||||
return exclusive('ride-move', async () => {
|
||||
const rider = (await riders()).find(r => r.id === org);
|
||||
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();
|
||||
if (owner?.proxy !== true) return { org, moved: false, reason: 'no-proxy' };
|
||||
if (owner.riding !== true) return { org, moved: false, reason: 'no-riding-bus' };
|
||||
let result;
|
||||
try { result = await move(org, owner.models, principal); }
|
||||
catch (error) { result = { moved: false, reason: failCode(error) }; }
|
||||
if (!result?.moved) return { org, moved: false, reason: result?.reason ?? 'failed' };
|
||||
return { org, moved: true, model: result.model, receipt: receipt({ op: 'ride-move', org, model: result.model }) };
|
||||
});
|
||||
},
|
||||
// The organization admin page calls this before it saves a document that drops cliproxy from
|
||||
// the model providers, and saves nothing when it throws. Ride all then leaves that organization off.
|
||||
rideRemoved(org) {
|
||||
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 });
|
||||
},
|
||||
async initLockClear() {
|
||||
return exclusive('init-lock-clear', async () => ({ receipt: clearInitLock({ configDir, dataRoot, buses: await buses(), now }) }));
|
||||
},
|
||||
|
||||
@@ -107,6 +107,26 @@ export function appendReceipt(dataRoot, record) {
|
||||
return record;
|
||||
}
|
||||
|
||||
// A file's text, read the way fenceState checks the fence file: opened without following a symlink
|
||||
// (and without blocking on a FIFO), a regular file the Console's own user owns, with none of the
|
||||
// `mask` permission bits. A missing file throws its ENOENT; any other failure throws
|
||||
// proxy-file-refused. `uid` is a test seam.
|
||||
export function ownedText(file, { mask = 0o077, uid = process.getuid() } = {}) {
|
||||
const fd = fs.openSync(file, fs.constants.O_RDONLY | fs.constants.O_NOFOLLOW | fs.constants.O_NONBLOCK);
|
||||
try {
|
||||
const stat = fs.fstatSync(fd);
|
||||
if (!stat.isFile() || stat.uid !== uid || (stat.mode & mask)) throw Object.assign(new Error('proxy-file-refused'), { code: 'proxy-file-refused' });
|
||||
return fs.readFileSync(fd, 'utf8');
|
||||
} finally { fs.closeSync(fd); }
|
||||
}
|
||||
|
||||
// The whole receipt file's text for Ride all, which decides from it (ownedText, no group or other
|
||||
// bits). No file yet answers ''; anything else that fails answers null, and the caller treats every
|
||||
// organization without cliproxy as unreadable.
|
||||
export function receiptsText(dataRoot, { uid = process.getuid() } = {}) {
|
||||
try { return ownedText(receiptFile(dataRoot), { uid }); } catch (error) { return error?.code === 'ENOENT' ? '' : null; }
|
||||
}
|
||||
|
||||
// The last few receipts, oldest first, for the page after a reload. Reads only the file's tail and
|
||||
// skips a line that does not parse (a torn last write). Receipts never hold a key or a body.
|
||||
export function recentReceipts(dataRoot, limit = 10) {
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
// #1873 ride-default slice 1: the rules for "Ride all", which adds the instance's model proxy
|
||||
// (the cliproxy model provider) to the owner's existing organizations. Pure functions: the
|
||||
// Console wiring (webui cli.mjs) reads each organization and its bus, and admin-control runs them.
|
||||
import { usableModel } from '../../plugins/model-provider/cliproxy/index.mjs';
|
||||
|
||||
export { usableModel };
|
||||
const has = (list, name) => Array.isArray(list) && list.includes(name);
|
||||
|
||||
// The model providers one change-log change shows before and after, by the change's path: the
|
||||
// list itself, the plugins object, or the whole document. Any other path shows none.
|
||||
function providersOf(change) {
|
||||
const at = value => change?.path === 'plugins.model-provider' ? value
|
||||
: change?.path === 'plugins' ? value?.['model-provider']
|
||||
: change?.path === '' ? value?.plugins?.['model-provider'] : undefined;
|
||||
return [at(change?.old), at(change?.new)];
|
||||
}
|
||||
const touchesProviders = path => path === '' || path === 'plugins' || path === 'plugins.model-provider';
|
||||
|
||||
// What the organization's change log shows of cliproxy, from its committed writes: `removed` when
|
||||
// a write took it off, `seen` when any write shows it on the organization before or after, or wrote
|
||||
// the providers without logging their values (a paths-only entry). A log that cannot be read
|
||||
// answers null, and the caller skips the organization as unreadable.
|
||||
export function cliproxyHistory(text) {
|
||||
if (typeof text !== 'string') return null;
|
||||
let removed = false, seen = false;
|
||||
try {
|
||||
for (const line of text.split('\n')) {
|
||||
if (!line.trim()) continue;
|
||||
const event = JSON.parse(line);
|
||||
if (event?.state !== 'committed' || !Array.isArray(event.changes)) continue;
|
||||
for (const change of event.changes) {
|
||||
if (!touchesProviders(change?.path)) continue;
|
||||
if (!Object.hasOwn(change, 'old') && !Object.hasOwn(change, 'new')) { seen = true; continue; }
|
||||
const [before, after] = providersOf(change);
|
||||
if (has(before, 'cliproxy') && !has(after, 'cliproxy')) removed = true;
|
||||
if (has(before, 'cliproxy') || has(after, 'cliproxy')) seen = true;
|
||||
}
|
||||
}
|
||||
return { removed, seen };
|
||||
} catch { return null; }
|
||||
}
|
||||
export function cliproxyRemoved(text) { const h = cliproxyHistory(text); return h === null ? null : h.removed; }
|
||||
|
||||
// The organizations the proxy's own receipts (proxy-admin.jsonl) show cliproxy was or may have
|
||||
// been on: each ride-all receipt's added list, each ride-move's org, each ride-removed's org (the
|
||||
// organization admin page writes one before it saves a document without cliproxy), and each
|
||||
// ride-intent's org (Ride all writes one before each plugins.set) unless a later ride-not-added for
|
||||
// that organization shows the bus did not take it. Any line that does not parse, and any ride
|
||||
// record of the wrong shape, answers null: the caller then skips every organization without
|
||||
// cliproxy as unreadable. Text that is not a string answers null too.
|
||||
const RIDE_ORG = new Set(['ride-move', 'ride-removed', 'ride-intent', 'ride-not-added']);
|
||||
export function riddenOrgs(text) {
|
||||
if (typeof text !== 'string') return null;
|
||||
const ids = new Set(), pending = new Set();
|
||||
for (const line of text.split('\n')) {
|
||||
if (!line.trim()) continue;
|
||||
let r; try { r = JSON.parse(line); } catch { return null; }
|
||||
if (!r || typeof r !== 'object' || Array.isArray(r)) return null;
|
||||
if (r.op === 'ride-all') {
|
||||
if (!Array.isArray(r.added) || !r.added.every(id => typeof id === 'string')) return null;
|
||||
for (const id of r.added) ids.add(id);
|
||||
} else if (RIDE_ORG.has(r.op)) {
|
||||
if (typeof r.org !== 'string') return null;
|
||||
if (r.op === 'ride-intent') pending.add(r.org);
|
||||
else if (r.op === 'ride-not-added') pending.delete(r.org);
|
||||
else ids.add(r.org);
|
||||
}
|
||||
}
|
||||
for (const id of pending) ids.add(id);
|
||||
return ids;
|
||||
}
|
||||
|
||||
// The local part of the decision, with no bus call: what Ride all does with one organization
|
||||
// before it asks the organization's bus anything. An organization that uses cliproxy now (and is
|
||||
// not a fixture organization) is already riding, whatever its history. One without cliproxy now
|
||||
// that had it, or may have had it, is removed: its change log or the proxy's receipts show it
|
||||
// (`ridden` is true from the receipts, null when they cannot be read).
|
||||
export function localRide({ fixture, cliproxy, changes, ridden = false }) {
|
||||
if (typeof fixture !== 'boolean' || typeof cliproxy !== 'boolean') return 'unreadable';
|
||||
if (cliproxy && !fixture) return 'already';
|
||||
const history = cliproxyHistory(changes);
|
||||
if (history === null || (ridden === null && !cliproxy)) return 'unreadable';
|
||||
if (history.removed || (!cliproxy && (ridden === true || history.seen))) return 'removed';
|
||||
if (fixture) return 'fixture';
|
||||
return 'ready';
|
||||
}
|
||||
|
||||
// One organization as Ride all reads it, without its bus: `read()` gives its document and
|
||||
// `readLog()` the text of its change log. A document that cannot be read leaves `fixture` and
|
||||
// `cliproxy` undefined, a log that does not exist yet is empty, and any other log failure is null;
|
||||
// localRide names either as unreadable. `isFixture(doc)` is the CLI's own fixture test.
|
||||
export function readRider({ id, read, readLog, isFixture }) {
|
||||
let doc = null, changes = null;
|
||||
try { doc = read(); } catch {}
|
||||
try { changes = readLog(); } catch (e) { changes = e?.code === 'ENOENT' ? '' : null; }
|
||||
const ok = !!doc && typeof doc === 'object';
|
||||
return { id, fixture: ok ? isFixture(doc) === true : undefined,
|
||||
cliproxy: ok ? (Array.isArray(doc.plugins?.['model-provider']) ? doc.plugins['model-provider'] : []).includes('cliproxy') : undefined, changes };
|
||||
}
|
||||
|
||||
// The owner's models: `proxy` is true when the instance's proxy.json names a port. Each organization
|
||||
// that lists cliproxy is asked for its routes at once, so a hung bus costs one timeout, not one each.
|
||||
// It rides when its bus answers a ready catalogue; the models are the cliproxy rows those
|
||||
// catalogues list, once each.
|
||||
export async function ownerModelsFrom({ proxy, riders }) {
|
||||
if (proxy !== true) return { proxy: false, riding: false, models: [] };
|
||||
const catalogues = await Promise.all((Array.isArray(riders) ? riders : []).filter(r => r?.cliproxy === true)
|
||||
.map(async r => { try { return (await r.call('routes'))?.catalogue; } catch { return null; } }));
|
||||
const models = new Map(); let riding = false;
|
||||
for (const catalogue of catalogues) {
|
||||
if (catalogue?.status !== 'ready' || !Array.isArray(catalogue.models)) continue;
|
||||
riding = true;
|
||||
// A key two buses list keeps the first usable row: one bus's stale row does not hide the model.
|
||||
for (const m of catalogue.models) {
|
||||
if (m?.module !== 'cliproxy' || typeof m.key !== 'string') continue;
|
||||
if (!models.has(m.key) || (!usableModel(models.get(m.key)) && usableModel(m))) models.set(m.key, m);
|
||||
}
|
||||
}
|
||||
return { proxy: true, riding, models: [...models.values()] };
|
||||
}
|
||||
|
||||
// The owner's model for an organization: the first usable catalogue model from claude, else
|
||||
// the first usable one, that serves every harness in `harnesses`.
|
||||
export function pickModel(models, harnesses = []) {
|
||||
const fits = (Array.isArray(models) ? models : []).filter(m => usableModel(m) && harnesses.every(h => m.harnesses.includes(h)));
|
||||
return fits.find(m => m.upstream === 'claude') ?? fits[0] ?? null;
|
||||
}
|
||||
|
||||
// A fixture organization moved to the owner's models: the fixture model provider becomes
|
||||
// cliproxy, each fixture model entry goes, and every role that used one uses the owner's model.
|
||||
// Answers { doc, model } or { reason } when no catalogue model serves the roles' harnesses.
|
||||
export function fixtureMove(doc, models) {
|
||||
const next = structuredClone(doc);
|
||||
const fixtureKeys = Object.entries(next.launch?.models ?? {}).filter(([, m]) => m?.module === 'fixture').map(([key]) => key);
|
||||
const uses = [];
|
||||
const visit = vars => {
|
||||
if (!vars || typeof vars !== 'object') return;
|
||||
for (const prefix of ['', 'fallback.']) if (fixtureKeys.includes(vars[prefix + 'model'])) uses.push({ vars, prefix });
|
||||
};
|
||||
// A model is an agent-layer var: an organization's roles and each project's own agents carry one.
|
||||
for (const role of Object.values(next.roles ?? {})) visit(role?.vars);
|
||||
for (const project of Object.values(next.projects ?? {})) for (const agent of Object.values(project?.agents ?? {})) visit(agent?.vars);
|
||||
const harnesses = [...new Set(uses.map(({ vars, prefix }) => vars[prefix + 'harness']).filter(h => typeof h === 'string'))];
|
||||
const model = pickModel(models, harnesses);
|
||||
if (!model) return { reason: 'no-model' };
|
||||
for (const key of fixtureKeys) delete next.launch.models[key];
|
||||
next.launch = next.launch ?? {}; next.launch.models = next.launch.models ?? {};
|
||||
next.launch.models[model.key] = { provider: model.provider, model: model.model, module: 'cliproxy', upstream: model.upstream };
|
||||
for (const { vars, prefix } of uses) vars[prefix + 'model'] = model.key;
|
||||
const providers = (next.plugins?.['model-provider'] ?? []).filter(n => n !== 'fixture');
|
||||
next.plugins = { ...(next.plugins ?? {}), 'model-provider': providers.includes('cliproxy') ? providers : [...providers, 'cliproxy'] };
|
||||
return { doc: next, model };
|
||||
}
|
||||
|
||||
// The move itself, through the organization service's existing whole-document update: read,
|
||||
// change, validate, then write only if the revision read is still current. Owner-only twice:
|
||||
// the Console route admits only the instance owner, and the service checks `ctx.principal`.
|
||||
export async function moveFixtureOrg({ service, ctx, id, models }) {
|
||||
const shown = await service.showOrg(ctx, id);
|
||||
const moved = fixtureMove(shown.doc, models);
|
||||
if (moved.reason) return { moved: false, reason: moved.reason };
|
||||
const checked = await service.validateOrg(moved.doc);
|
||||
if (!checked?.ok) return { moved: false, reason: 'invalid' };
|
||||
await service.updateOrg(ctx, id, moved.doc, { ifRevision: shown.revision });
|
||||
return { moved: true, model: moved.model.key };
|
||||
}
|
||||
@@ -7,12 +7,23 @@ export function installPaths(dataRoot, pin = PIN) {
|
||||
const dir = path.join(dataRoot, 'opt', 'cliproxyapi', pin.version);
|
||||
return { dir, binary: path.join(dir, 'cli-proxy-api'), panel: path.join(dir, 'static', 'management.html') };
|
||||
}
|
||||
// proxy.json records the install path from the PIN it was created under, and nothing
|
||||
// rewrites it. After a PIN bump an earlier release's path under this data root stays valid
|
||||
// and reads as the current pinned path. The runner executes only the hash-verified pinned
|
||||
// install, never the recorded path, so an old value selects nothing.
|
||||
const releasePath = (dataRoot, binary) => {
|
||||
const base = path.join(dataRoot, 'opt', 'cliproxyapi') + path.sep;
|
||||
if (typeof binary !== 'string' || !binary.startsWith(base)) return false;
|
||||
const [version, file, ...rest] = binary.slice(base.length).split(path.sep);
|
||||
return rest.length === 0 && file === 'cli-proxy-api' && /^\d{1,4}\.\d{1,4}\.\d{1,6}$/.test(version);
|
||||
};
|
||||
export function validateProxy(value, dataRoot, pin = PIN) {
|
||||
const pinned = installPaths(dataRoot, pin).binary;
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)
|
||||
|| Object.keys(value).some(k => !['binary', 'port', 'rotateGraceMinutes', 'firstOutputSeconds'].includes(k))
|
||||
|| !Number.isInteger(value.port) || value.port < 1024 || value.port > 65535 || value.port === 8317
|
||||
|| value.binary !== installPaths(dataRoot, pin).binary) refuse('proxy-config-invalid');
|
||||
return { port: value.port, binary: value.binary, ...proxyTiming(value) };
|
||||
|| (value.binary !== pinned && !releasePath(dataRoot, value.binary))) refuse('proxy-config-invalid');
|
||||
return { port: value.port, binary: pinned, ...proxyTiming(value) };
|
||||
}
|
||||
export function loadProxy(configDir, dataRoot) {
|
||||
const file = path.join(configDir, 'proxy.json');
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// P4 pinned8.0.42 regression: absent Claude provider config is HTTP404.
|
||||
// P4 regression since PIN 8.0.42 (still so on 8.0.79): absent Claude provider config is HTTP 404.
|
||||
// Normalize only that fixed GET; all other methods/endpoints/statuses refuse.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
|
||||
@@ -13,3 +13,13 @@ for (const [field, maximum, code] of [['rotateGraceMinutes', 1440, 'proxy-rotate
|
||||
assert.throws(() => validateProxy({ ...base, [field]: value }, dataRoot), e => e.code === code);
|
||||
});
|
||||
}
|
||||
// #1873 PIN bump: a sidecar written under an earlier PIN keeps the instance config valid.
|
||||
test('a proxy.json written under an earlier PIN reads as the current pinned path', () => {
|
||||
const old = { ...base, binary: '/fixture-data/opt/cliproxyapi/8.0.42/cli-proxy-api' };
|
||||
assert.deepEqual(validateProxy(old, dataRoot), base);
|
||||
assert.deepEqual(validateProxy({ ...old, rotateGraceMinutes: 60 }, dataRoot), { ...base, rotateGraceMinutes: 60 });
|
||||
});
|
||||
for (const binary of ['/bad', '/fixture-data/opt/cliproxyapi/8.0.42/other', '/fixture-data/opt/cliproxyapi/latest/cli-proxy-api',
|
||||
'/fixture-data/opt/cliproxyapi/8.0.42/x/cli-proxy-api', '/fixture-data/opt/cliproxyapi/../cliproxyapi/8.0.42/cli-proxy-api',
|
||||
'/other-data/opt/cliproxyapi/8.0.42/cli-proxy-api', '/fixture-data/opt/cliproxyapi/8.0/cli-proxy-api', 7])
|
||||
test(`proxy-config-invalid: binary ${JSON.stringify(binary)}`, () => assert.throws(() => validateProxy({ ...base, binary }, dataRoot), e => e.code === 'proxy-config-invalid'));
|
||||
@@ -1,4 +1,4 @@
|
||||
// P4 client contract; endpoint/payload cases are probe-pending on PIN8.0.42.
|
||||
// P4 client contract; endpoint/payload cases were probe-pending on PIN 8.0.42.
|
||||
// Catches management key/body/query values leaking into receipts, redirects/retries.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
// #1873 ride-default slice 1: once Ride all adds the model proxy to an organization, a fenced
|
||||
// proxy refuses each of that organization's launches with proxy-uncertain, a direct anthropic
|
||||
// route included. A real organization bus bound to the instance's proxy, a loopback stand-in for
|
||||
// the proxy's management API (made-up key, kept in this test's memory), and the real Ride all.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import http from 'node:http';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { startBroker } from '../../bus/src/runtime.mjs';
|
||||
import { busOrganization } from '../../bus/src/organization-context.mjs';
|
||||
import { createProxyLock } from '../../bus/src/proxy-lock.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';
|
||||
import { createProxyAdmin } from '../src/admin-control.mjs';
|
||||
|
||||
const MANAGE = 'ride-fence-manage';
|
||||
|
||||
async function ridingBus(t) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'ride-fence-')), configDir = join(root, 'config');
|
||||
fs.mkdirSync(join(configDir, 'secrets'), { recursive: true, mode: 0o700 });
|
||||
fs.mkdirSync(join(root, 'proxy'), { mode: 0o700 });
|
||||
let keys = ['msk-ride-fence-sentinel'];
|
||||
const server = http.createServer(async (req, res) => {
|
||||
const chunks = []; for await (const c of req) chunks.push(c);
|
||||
if (req.headers.authorization !== `Bearer ${MANAGE}`) { res.writeHead(401); res.end('{}'); return; }
|
||||
if (req.url === '/v8/management/config/access/api-keys') {
|
||||
if (req.method === 'PUT') keys = JSON.parse(Buffer.concat(chunks));
|
||||
res.end(JSON.stringify(req.method === 'GET' ? keys : { status: 'ok' })); return;
|
||||
}
|
||||
if (req.url.startsWith('/v8/management/credentials')) { res.end(JSON.stringify({ files: [], observed_at: '2026-11-19T00:00:00Z' })); return; }
|
||||
if (req.url.startsWith('/v8/management/config/api-keys/')) { res.end('[]'); return; }
|
||||
res.writeHead(404); res.end('{}');
|
||||
});
|
||||
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||
const port = server.address().port;
|
||||
const rolesDir = rolesCopy(root), system = systemFor(root), doc = businessDoc(root, 'demo');
|
||||
// A direct anthropic route and no model provider: before Ride all, no launch asks the proxy.
|
||||
doc.plugins = { ...doc.plugins, harness: ['pi'] };
|
||||
doc.launch.models['claude-opus-5-5'] = { provider: 'anthropic', model: 'claude-opus-5-5' };
|
||||
doc.roles.coder.vars = { ...doc.roles.coder.vars, harness: 'pi', model: 'claude-opus-5-5', thinking: 'low' };
|
||||
writeJson(join(configDir, 'businesses', 'demo.json'), doc);
|
||||
fs.writeFileSync(join(configDir, 'secrets', 'proxy-management.key'), MANAGE, { mode: 0o600 });
|
||||
const business = loadBusiness('demo', { dir: configDir, rolesDir }), resolved = {};
|
||||
for (const instance of Object.keys(business.roles)) resolved[instance] = resolveInstance({ system, business, instance });
|
||||
fs.mkdirSync(join(root, 'data'), { mode: 0o700 }); new Registry(join(root, 'data'));
|
||||
const bus = await startBroker({ dataRoot: join(root, 'data'), businesses: { demo: busOrganization(business, resolved) },
|
||||
organization: { dir: configDir, rolesDir, system }, proxy: { port, binary: '/fixture-unused' },
|
||||
org: { id: 'demo' }, instanceDataRoot: root, instanceConfigDir: configDir });
|
||||
t.after(async () => { await bus.close(); server.closeAllConnections(); await new Promise(r => server.close(r)); fs.rmSync(root, { recursive: true, force: true }); });
|
||||
const human = bus.broker.bindHuman({ business: 'demo', human: 'jason', via: 'cli', outsideAgent: true });
|
||||
const call = async (verb, args = {}) => bus.broker.request(human, { verb, args });
|
||||
const lock = createProxyLock({ stateDir: join(root, 'proxy'), identity: `${configDir}:${port}` });
|
||||
const admission = async () => {
|
||||
const current = JSON.parse(fs.readFileSync(join(configDir, 'businesses', 'demo.json'), 'utf8'));
|
||||
const revision = (await call('plugins')).revision, entry = current.launch.models['claude-opus-5-5'];
|
||||
return bus.modelAdmission({ business: 'demo', instance: 'coder', modelKey: 'claude-opus-5-5', harness: 'pi', revision, entry,
|
||||
provider: entry.provider, model: entry.model, account: null, route: 'primary', transport: null });
|
||||
};
|
||||
return { root, configDir, bus, call, lock, admission, doc: () => JSON.parse(fs.readFileSync(join(configDir, 'businesses', 'demo.json'), 'utf8')) };
|
||||
}
|
||||
|
||||
test('a fenced proxy refuses each riding organization\'s launches with proxy-uncertain, direct anthropic included', async t => {
|
||||
const b = await ridingBus(t);
|
||||
const dir = fs.mkdtempSync(join(tmpdir(), 'ride-fence-admin-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
||||
const admin = createProxyAdmin({ dataRoot: dir, configDir: dir, unit: { status: async () => ({}) }, buses: async () => [], fetch: async () => ({}),
|
||||
riders: async () => [{ id: 'demo', fixture: false, cliproxy: (b.doc().plugins['model-provider'] ?? []).includes('cliproxy'), changes: '', call: b.call }] });
|
||||
const ride = await admin.rideAll();
|
||||
assert.deepEqual(ride.added, ['demo'], JSON.stringify(ride));
|
||||
assert.deepEqual(b.doc().plugins['model-provider'], ['cliproxy']);
|
||||
assert.equal(b.doc().launch.models['claude-opus-5-5'].module, undefined, 'the direct anthropic route stays as it was');
|
||||
await b.lock.run(async () => b.lock.fence({ org: 'demo', op: 'ride-fence-test' }));
|
||||
assert.deepEqual(await b.admission(), { allow: false, code: 'proxy-uncertain' });
|
||||
assert.deepEqual((await admin.rideAll()).already, ['demo'], 'a second Ride all leaves a riding organization alone');
|
||||
});
|
||||
|
||||
// The launcher half: on an instance with a model proxy, a direct anthropic route never asks the
|
||||
// bus's admission until the organization lists a model provider. After Ride all it does, so the
|
||||
// fenced answer above refuses the launch before any run exists.
|
||||
test('after Ride all the launcher asks admission for a direct anthropic route and a fenced answer refuses the launch', async t => {
|
||||
const { readFileSync } = fs;
|
||||
const { createLauncher } = await import('../../cli/src/launcher.mjs');
|
||||
const { initializeControl } = await import('../../processing-control/src/state.mjs');
|
||||
const { fakeHarnesses, fixture, tmp, withoutHostCredentials } = await import('../../cli/tests/helpers.mjs');
|
||||
// Ride all writes the organization's providers through its bus's plugins.set; this stand-in bus
|
||||
// applies that write to the organization file the launcher reads.
|
||||
const ride = async file => {
|
||||
const dir = fs.mkdtempSync(join(tmpdir(), 'ride-fence-admin-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
||||
const call = async (verb, args = {}) => {
|
||||
const doc = JSON.parse(readFileSync(file, 'utf8'));
|
||||
if (verb === 'plugins') return { revision: 'r1', plugins: doc.plugins };
|
||||
if (verb === 'proxy.accounts') return { accounts: [] };
|
||||
if (verb === 'plugins.set') { assert.equal(args.revision, 'r1'); doc.plugins = { ...doc.plugins, ...args.plugins }; fs.writeFileSync(file, JSON.stringify(doc)); return { revision: 'r2' }; }
|
||||
throw Object.assign(Error('unexpected verb'), { code: 'invalid-request' });
|
||||
};
|
||||
const admin = createProxyAdmin({ dataRoot: dir, configDir: dir, unit: { status: async () => ({}) }, buses: async () => [], fetch: async () => ({}),
|
||||
riders: async () => [{ id: 'acme', fixture: false, cliproxy: false, changes: '', call }] });
|
||||
assert.deepEqual((await admin.rideAll()).added, ['acme']);
|
||||
};
|
||||
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: ['claude-code'] };
|
||||
doc.launch.models['claude-opus-5-5'] = { provider: 'anthropic', model: 'claude-opus-5-5' };
|
||||
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'));
|
||||
initializeControl(f.dataRoot); withoutHostCredentials(f.env, { piKey: true });
|
||||
const ops = [];
|
||||
const host = { business: 'acme', beforeClose: () => {}, op: async x => {
|
||||
ops.push(x.op);
|
||||
if (x.op === 'identity') return { business: 'acme', role: 'pm' };
|
||||
if (x.op === 'mayLaunch') return { allow: true };
|
||||
if (x.op === 'modelAdmission') return { allow: false, code: 'proxy-uncertain' };
|
||||
if (x.op === 'authorizeLaunch') throw Object.assign(Error('fixture authorization stop'), { code: 'fixture-policy' });
|
||||
return {};
|
||||
} };
|
||||
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 };
|
||||
};
|
||||
const before = await launchOf(false);
|
||||
assert.equal(before.ops.includes('modelAdmission'), false, JSON.stringify(before));
|
||||
assert.equal(before.ops.includes('mayLaunch'), true, 'the before launch reached the limit checks');
|
||||
assert.notDeepEqual(before.answer, { ok: false, error: 'proxy-uncertain' });
|
||||
const after = await launchOf(true);
|
||||
assert.equal(after.ops.includes('modelAdmission'), true, JSON.stringify(after));
|
||||
assert.deepEqual(after.answer, { ok: false, error: 'proxy-uncertain' });
|
||||
});
|
||||
@@ -0,0 +1,546 @@
|
||||
// #1873 ride-default slice 1: Ride all adds the instance's model proxy (cliproxy) to the owner's
|
||||
// organizations, skips and names the ones it must not change, and lists fixture organizations,
|
||||
// which move to the owner's models only by a separate confirmed action.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
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 { initFiles, receiptFile, appendReceipt, receiptsText, ownedText } from '../src/admin.mjs';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { PIN } from '../src/common.mjs';
|
||||
import { cliproxyRemoved, localRide, riddenOrgs, pickModel, fixtureMove, moveFixtureOrg, readRider, ownerModelsFrom } from '../src/ride.mjs';
|
||||
import * as orgService from '../../organization/src/org-service.mjs';
|
||||
import { orgUsesFixture } from '../../cli/src/dev-fixture.mjs';
|
||||
|
||||
const stopped = async () => ({ stopped: true, running: [] });
|
||||
const row = (over = {}) => ({ key: 'claude-claude-opus-5-5', module: 'cliproxy', upstream: 'claude', provider: 'anthropic', model: 'claude-opus-5-5', label: 'Claude Opus 5.5', available: true, reason: null, harnesses: ['claude-code', 'pi'], ...over });
|
||||
const glm = row({ key: 'zai-glm-5-3', upstream: 'zai', provider: 'zai', model: 'glm-5.3', label: 'GLM 5.3', harnesses: ['pi'] });
|
||||
const committed = changes => JSON.stringify({ state: 'committed', changes }) + '\n';
|
||||
const busError = code => Object.assign(new Error('bus text that must not leak'), { code });
|
||||
|
||||
// A fake organization bus: `plugins` answers the providers and revision; `proxy.accounts` answers
|
||||
// or throws; `plugins.set` records the write.
|
||||
function bus({ providers = ['fixture'], down = false, client = true, accounts = null, set = null } = {}) {
|
||||
const calls = [];
|
||||
const call = async (verb, args) => {
|
||||
calls.push([verb, args]);
|
||||
if (down) throw busError('no-bus-host');
|
||||
if (verb === 'plugins') return { business: 'x', revision: 'rev-1', plugins: { 'model-provider': providers }, available: {} };
|
||||
if (verb === 'proxy.accounts') { if (accounts) throw accounts; if (!client) throw busError('not-configured'); return { accounts: [] }; }
|
||||
if (verb === 'plugins.set') { if (set) throw set; providers = args.plugins['model-provider']; return { revision: 'rev-2' }; }
|
||||
throw new Error(`unexpected ${verb}`);
|
||||
};
|
||||
return { call, calls };
|
||||
}
|
||||
function admin(t, { riders, ownerModels, move, unitState } = {}) {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'rd-'));
|
||||
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 state = unitState ?? { installed: false, active: 'inactive', healthy: false };
|
||||
const unit = { unit: 'mosaic-proxy-zz.service', status: async () => ({ unit: 'mosaic-proxy-zz.service', ...state }) };
|
||||
const sha = text => createHash('sha256').update(text).digest('hex');
|
||||
const pin = { ...PIN, archive: { ...PIN.archive, sha256: sha('a') }, binary: { sha256: sha('b') }, panel: { ...PIN.panel, sha256: sha('p') } };
|
||||
const a = createProxyAdmin({ dataRoot, configDir, unit, buses: stopped, fetch: async () => ({}), now: () => new Date(0), pin,
|
||||
...(riders ? { riders } : {}), ...(ownerModels ? { ownerModels } : {}), ...(move !== undefined ? { move } : {}) });
|
||||
return { admin: a, configDir, dataRoot, pin, state };
|
||||
}
|
||||
const receipts = dataRoot => { try { return fs.readFileSync(receiptFile(dataRoot), 'utf8').trim().split('\n').filter(Boolean).map(JSON.parse); } catch { return []; } };
|
||||
|
||||
test('a change log shows cliproxy removed only when a committed write took it off', () => {
|
||||
assert.equal(cliproxyRemoved(''), false);
|
||||
assert.equal(cliproxyRemoved(committed([{ path: 'plugins.model-provider', old: ['fixture'], new: ['fixture', 'cliproxy'] }])), false);
|
||||
assert.equal(cliproxyRemoved(committed([{ path: 'plugins.model-provider', old: ['cliproxy'], new: [] }])), true);
|
||||
assert.equal(cliproxyRemoved(committed([{ path: 'plugins', old: { 'model-provider': ['cliproxy'] }, new: { 'model-provider': ['fixture'] } }])), true);
|
||||
assert.equal(cliproxyRemoved(JSON.stringify({ state: 'staged', changes: [{ path: 'plugins.model-provider', old: ['cliproxy'], new: [] }] }) + '\n'), false, 'a write that did not commit removed nothing');
|
||||
assert.equal(cliproxyRemoved(committed([{ path: 'plugins.model-provider', old: ['cliproxy'], new: [] }, { path: 'plugins.model-provider', old: [], new: ['cliproxy'] }]) ), true, 'a removal stays a removal once seen');
|
||||
assert.equal(cliproxyRemoved('{not json\n'), null);
|
||||
assert.equal(cliproxyRemoved(null), null);
|
||||
});
|
||||
|
||||
test('the local rule names unreadable, removed, fixture, already and ready in that order', () => {
|
||||
const removed = committed([{ path: 'plugins.model-provider', old: ['cliproxy'], new: [] }]);
|
||||
assert.equal(localRide({ fixture: undefined, cliproxy: false, changes: '' }), 'unreadable');
|
||||
assert.equal(localRide({ fixture: false, cliproxy: undefined, changes: '' }), 'unreadable');
|
||||
assert.equal(localRide({ fixture: false, cliproxy: false, changes: null }), 'unreadable');
|
||||
assert.equal(localRide({ fixture: true, cliproxy: false, changes: removed }), 'removed');
|
||||
assert.equal(localRide({ fixture: true, cliproxy: false, changes: '' }), 'fixture');
|
||||
assert.equal(localRide({ fixture: false, cliproxy: true, changes: '' }), 'already');
|
||||
assert.equal(localRide({ fixture: false, cliproxy: false, changes: '' }), 'ready');
|
||||
// Review round: an organization that uses cliproxy now is already riding, whatever its history.
|
||||
assert.equal(localRide({ fixture: false, cliproxy: true, changes: removed }), 'already', 'removed once and added back');
|
||||
assert.equal(localRide({ fixture: false, cliproxy: true, changes: null }), 'already');
|
||||
assert.equal(localRide({ fixture: true, cliproxy: true, changes: '' }), 'fixture');
|
||||
// The proxy's receipts show it put cliproxy on the organization, and it lacks cliproxy now.
|
||||
assert.equal(localRide({ fixture: false, cliproxy: false, changes: '', ridden: true }), 'removed');
|
||||
assert.equal(localRide({ fixture: true, cliproxy: false, changes: '', ridden: true }), 'removed');
|
||||
assert.equal(localRide({ fixture: false, cliproxy: true, changes: '', ridden: true }), 'already');
|
||||
assert.equal(localRide({ fixture: false, cliproxy: false, changes: '', ridden: null }), 'unreadable');
|
||||
assert.equal(localRide({ fixture: false, cliproxy: true, changes: '', ridden: null }), 'already');
|
||||
});
|
||||
|
||||
test('the proxy receipts name each organization ride all added, tried to add, moved or saw removed', () => {
|
||||
const lines = records => records.map(r => JSON.stringify(r)).join('\n') + '\n';
|
||||
const text = lines([{ op: 'ride-all', added: ['a', 'b'], skipped: [{ id: 'c', reason: 'bus-down' }], fixtures: ['d'] }, { op: 'ride-move', org: 'e', model: 'm' },
|
||||
{ op: 'fetch', added: ['f'] }, { op: 'ride-intent', org: 'g' }, { op: 'ride-removed', org: 'h' }]);
|
||||
assert.deepEqual([...riddenOrgs(text)].sort(), ['a', 'b', 'e', 'g', 'h']);
|
||||
assert.deepEqual([...riddenOrgs('')], []);
|
||||
assert.equal(riddenOrgs(null), null);
|
||||
// Round 3 (P2a): a line that does not parse, or a ride record of the wrong shape, fails closed.
|
||||
assert.equal(riddenOrgs(text + '{torn'), null, 'a torn line');
|
||||
assert.equal(riddenOrgs(lines([{ op: 'ride-all', added: [7] }])), null);
|
||||
assert.equal(riddenOrgs(lines([{ op: 'ride-all', added: 'a' }])), null);
|
||||
assert.equal(riddenOrgs(lines([{ op: 'ride-intent' }])), null);
|
||||
assert.equal(riddenOrgs(lines([{ op: 'ride-removed', org: 3 }])), null);
|
||||
assert.equal(riddenOrgs('7\n'), null, 'a line that is not a record');
|
||||
// Round 3 (P2b): an intent the bus then showed was not applied is cleared, and only that intent.
|
||||
assert.deepEqual([...riddenOrgs(lines([{ op: 'ride-intent', org: 'g' }, { op: 'ride-not-added', org: 'g' }]))], []);
|
||||
assert.deepEqual([...riddenOrgs(lines([{ op: 'ride-all', added: ['g'] }, { op: 'ride-intent', org: 'g' }, { op: 'ride-not-added', org: 'g' }]))], ['g'], 'an earlier add stays');
|
||||
assert.deepEqual([...riddenOrgs(lines([{ op: 'ride-removed', org: 'g' }, { op: 'ride-intent', org: 'g' }, { op: 'ride-not-added', org: 'g' }]))], ['g'], 'an earlier removal stays');
|
||||
assert.deepEqual([...riddenOrgs(lines([{ op: 'ride-not-added', org: 'g' }, { op: 'ride-intent', org: 'g' }]))], ['g'], 'a later intent stands');
|
||||
});
|
||||
|
||||
test('a change log that shows cliproxy on the organization at any time, or a write to its providers it cannot read, counts as removed once cliproxy is gone', () => {
|
||||
// Round 3 (P1 case b): added on the Settings page, then removed on the admin page, which writes no log entry.
|
||||
const added = committed([{ path: 'plugins.model-provider', old: [], new: ['cliproxy'] }]);
|
||||
assert.equal(localRide({ fixture: false, cliproxy: false, changes: added }), 'removed');
|
||||
assert.equal(localRide({ fixture: false, cliproxy: true, changes: added }), 'already');
|
||||
assert.equal(localRide({ fixture: true, cliproxy: true, changes: added }), 'fixture', 'a fixture organization that lists cliproxy is still listed to move');
|
||||
assert.equal(localRide({ fixture: false, cliproxy: false, changes: committed([{ path: 'plugins', old: null, new: { 'model-provider': ['cliproxy'] } }]) }), 'removed');
|
||||
assert.equal(localRide({ fixture: false, cliproxy: false, changes: committed([{ path: '', old: null, new: { plugins: { 'model-provider': ['cliproxy'] } } }]) }), 'removed');
|
||||
assert.equal(localRide({ fixture: false, cliproxy: false, changes: committed([{ path: 'plugins.model-provider', oldPresent: true, newPresent: true }]) }), 'removed', 'a paths-only write to the providers');
|
||||
assert.equal(localRide({ fixture: false, cliproxy: false, changes: committed([{ path: 'roles.chief.vars', oldPresent: true, newPresent: true }]) }), 'ready', 'a paths-only write elsewhere');
|
||||
assert.equal(localRide({ fixture: false, cliproxy: false, changes: committed([{ path: 'plugins.model-provider', old: ['fixture'], new: ['openai'] }]) }), 'ready');
|
||||
assert.equal(localRide({ fixture: false, cliproxy: false, changes: JSON.stringify({ state: 'aborted', changes: [{ path: 'plugins.model-provider', old: [], new: ['cliproxy'] }] }) + '\n' }), 'ready', 'a write that did not commit');
|
||||
});
|
||||
|
||||
test('the reviewer probe: an organization created from the cliproxy preset and saved without it on its admin page is skipped as removed', async t => {
|
||||
// P1 case a. Nothing in the change log: the preset made it, the admin page took it off. The
|
||||
// admin page leaves a ride-removed receipt (proxy admin rideRemoved) before it saves.
|
||||
const b = bus({ providers: [] });
|
||||
const { admin: a, dataRoot } = admin(t, { riders: async () => [{ id: 'p', fixture: false, cliproxy: false, changes: '', call: b.call }] });
|
||||
assert.equal(a.rideRemoved('p').op, 'ride-removed');
|
||||
const r = await a.rideAll();
|
||||
assert.deepEqual(r.added, []); assert.deepEqual(r.skipped, [{ id: 'p', reason: 'removed' }]);
|
||||
assert.equal(b.calls.some(([v]) => v === 'plugins.set'), false);
|
||||
assert.deepEqual(receipts(dataRoot).filter(x => x.op === 'ride-removed').map(x => x.org), ['p']);
|
||||
assert.throws(() => a.rideRemoved('../x'), /proxy-org-invalid|invalid/);
|
||||
});
|
||||
|
||||
test('the reviewer probe: an organization added on the Settings page and removed on its admin page is skipped as removed', async t => {
|
||||
// P1 case b, with no ride-removed receipt at all: the change log's add is enough.
|
||||
const b = bus({ providers: [] });
|
||||
const log = committed([{ path: 'plugins.model-provider', old: [], new: ['cliproxy'] }]);
|
||||
const { admin: a } = admin(t, { riders: async () => [{ id: 's', fixture: false, cliproxy: false, changes: log, call: b.call }] });
|
||||
const r = await a.rideAll();
|
||||
assert.deepEqual(r.added, []); assert.deepEqual(r.skipped, [{ id: 's', reason: 'removed' }]);
|
||||
assert.equal(b.calls.some(([v]) => v === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
test('each add leaves its intent receipt before plugins.set, and an add whose outcome is unknown stays recorded', async t => {
|
||||
// P2b. The bus reads the receipts at the moment of the write.
|
||||
let seen = null, dataRootOf = null;
|
||||
const order = [];
|
||||
const watching = id => { const b = bus({ providers: [] }); const call = async (verb, args) => { if (verb === 'plugins.set') { order.push(id); seen = receipts(dataRootOf()).map(r => `${r.op}:${r.org ?? ''}`); } return b.call(verb, args); }; return { ...b, call }; };
|
||||
const one = watching('one'), two = watching('two');
|
||||
const f = admin(t, { riders: async () => [{ id: 'one', fixture: false, cliproxy: false, changes: '', call: one.call }, { id: 'two', fixture: false, cliproxy: false, changes: '', call: two.call }] });
|
||||
dataRootOf = () => f.dataRoot;
|
||||
await f.admin.rideAll();
|
||||
assert.deepEqual(order, ['one', 'two']);
|
||||
assert.deepEqual(seen, ['ride-intent:one', 'ride-intent:two'], 'the first add is on record before the second starts');
|
||||
// Probe C: plugins.set commits, then the call times out. The bus shows cliproxy, so the intent stands.
|
||||
let providers = [];
|
||||
const timeout = { calls: [], call: async (verb, args) => {
|
||||
if (verb === 'plugins') return { revision: 'r1', plugins: { 'model-provider': providers } };
|
||||
if (verb === 'proxy.accounts') return { accounts: [] };
|
||||
if (verb === 'plugins.set') { providers = args.plugins['model-provider']; throw busError('timeout'); }
|
||||
} };
|
||||
let cp = false;
|
||||
const c = admin(t, { riders: async () => [{ id: 't', fixture: false, cliproxy: cp, changes: '', call: timeout.call }] });
|
||||
const first = await c.admin.rideAll();
|
||||
assert.deepEqual(first.skipped, [{ id: 't', reason: 'timeout' }]);
|
||||
assert.deepEqual(receipts(c.dataRoot).filter(r => r.org === 't').map(r => r.op), ['ride-intent']);
|
||||
providers = []; // the owner took it off
|
||||
assert.deepEqual((await c.admin.rideAll()).skipped, [{ id: 't', reason: 'removed' }]);
|
||||
// A refusal the bus shows was not applied clears the intent, so the organization can ride later.
|
||||
const refusedBus = bus({ providers: [], set: busError('revision-conflict') });
|
||||
const d = admin(t, { riders: async () => [{ id: 'q', fixture: false, cliproxy: false, changes: '', call: refusedBus.call }] });
|
||||
assert.deepEqual((await d.admin.rideAll()).skipped, [{ id: 'q', reason: 'revision-conflict' }]);
|
||||
assert.deepEqual(receipts(d.dataRoot).filter(r => r.org === 'q').map(r => r.op), ['ride-intent', 'ride-not-added']);
|
||||
assert.deepEqual((await d.admin.status()).ride, [{ id: 'q', state: 'ready' }]);
|
||||
// A bus that does not answer the re-read leaves the intent: the add may have happened.
|
||||
let gone = false;
|
||||
const lost = { call: async (verb, args) => { if (gone) throw busError('no-bus-host'); if (verb === 'plugins') return { revision: 'r1', plugins: { 'model-provider': [] } }; if (verb === 'proxy.accounts') return { accounts: [] }; if (verb === 'plugins.set') { gone = true; throw busError('timeout'); } } };
|
||||
const e = admin(t, { riders: async () => [{ id: 'l', fixture: false, cliproxy: false, changes: '', call: lost.call }] });
|
||||
await e.admin.rideAll();
|
||||
assert.deepEqual(receipts(e.dataRoot).filter(r => r.org === 'l').map(r => r.op), ['ride-intent']);
|
||||
});
|
||||
|
||||
test('a removal saved while Ride all works on earlier organizations is read before the later add', async t => {
|
||||
// Re-check 3, P3-1: the run's first read of the receipts showed nothing for 'late'. While the run
|
||||
// reads 'first', the owner takes cliproxy off 'late' on its admin page (a ride-removed receipt).
|
||||
let a = null;
|
||||
const first = bus({ providers: [] }), late = bus({ providers: [] });
|
||||
const firstCall = async (verb, args) => { if (verb === 'plugins') a.rideRemoved('late'); 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: '', call: late.call }] });
|
||||
a = f.admin;
|
||||
const r = await a.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), ['ride-removed']);
|
||||
// Receipts that turn unreadable during the run skip the later add as unreadable.
|
||||
const g1 = bus({ providers: [] }), g2 = bus({ providers: [] });
|
||||
let g = null;
|
||||
const tear = async (verb, args) => { if (verb === 'plugins') { appendReceipt(g.dataRoot, { op: 'fetch' }); fs.appendFileSync(receiptFile(g.dataRoot), '{torn\n'); } return g1.call(verb, args); };
|
||||
g = admin(t, { riders: async () => [{ id: 'one', fixture: false, cliproxy: false, changes: '', call: tear }, { id: 'two', fixture: false, cliproxy: false, changes: '', call: g2.call }] });
|
||||
const s = await g.admin.rideAll();
|
||||
assert.deepEqual(s.skipped, [{ id: 'one', reason: 'unreadable' }, { id: 'two', reason: 'unreadable' }]);
|
||||
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 }] });
|
||||
appendReceipt(f.dataRoot, { op: 'fetch' });
|
||||
fs.chmodSync(receiptFile(f.dataRoot), 0o400); // readable by its owner only, not writable
|
||||
const r = await f.admin.rideAll();
|
||||
assert.deepEqual(r.skipped, [{ id: 'w', reason: 'not-recorded' }]);
|
||||
assert.equal(b.calls.some(([v]) => v === 'plugins.set'), false);
|
||||
});
|
||||
|
||||
test('receipts that are torn, a symlink, not the owner\'s or open to others are unreadable, and every organization without cliproxy is skipped', async t => {
|
||||
// P2a and P3 (probes D and E).
|
||||
const b = bus({ providers: [] });
|
||||
const f = admin(t, { riders: async () => [{ id: 'z', fixture: false, cliproxy: false, changes: '', call: b.call }, { id: 'y', fixture: false, cliproxy: true, changes: '', call: b.call }] });
|
||||
const file = receiptFile(f.dataRoot);
|
||||
appendReceipt(f.dataRoot, { op: 'fetch' });
|
||||
fs.appendFileSync(file, '{"op":"ride-all","added":["z"]');
|
||||
let r = await f.admin.rideAll();
|
||||
assert.deepEqual(r.skipped, [{ id: 'z', reason: 'unreadable' }], 'torn'); assert.deepEqual(r.already, ['y']);
|
||||
fs.rmSync(file); fs.writeFileSync(join(f.dataRoot, 'state', 'empty'), '', { mode: 0o600 });
|
||||
fs.symlinkSync(join(f.dataRoot, 'state', 'empty'), file);
|
||||
assert.deepEqual((await f.admin.rideAll()).skipped, [{ id: 'z', reason: 'unreadable' }], 'symlink');
|
||||
assert.equal(fs.lstatSync(file).isSymbolicLink(), true);
|
||||
fs.rmSync(file); fs.writeFileSync(file, '', { mode: 0o600 }); fs.chmodSync(file, 0o640);
|
||||
assert.deepEqual((await f.admin.rideAll()).skipped, [{ id: 'z', reason: 'unreadable' }], 'group-readable');
|
||||
fs.rmSync(file); fs.mkdirSync(file);
|
||||
assert.deepEqual((await f.admin.rideAll()).skipped, [{ id: 'z', reason: 'unreadable' }], 'not a file');
|
||||
fs.rmSync(file, { recursive: true });
|
||||
assert.equal(b.calls.some(([v]) => v === 'plugins.set'), false);
|
||||
assert.deepEqual((await f.admin.rideAll()).added, ['z'], 'no receipt file is no history');
|
||||
});
|
||||
|
||||
test('the receipts are read only when the Console\'s own user owns them', t => {
|
||||
const f = admin(t);
|
||||
appendReceipt(f.dataRoot, { op: 'ride-removed', org: 'p' });
|
||||
assert.match(receiptsText(f.dataRoot), /"ride-removed"/);
|
||||
assert.equal(receiptsText(f.dataRoot, { uid: process.getuid() + 1 }), null, 'another user\'s file');
|
||||
});
|
||||
|
||||
test('a FIFO in place of the receipts is unreadable and never blocks the read', t => {
|
||||
const f = admin(t);
|
||||
fs.mkdirSync(join(f.dataRoot, 'state'), { mode: 0o700 });
|
||||
execFileSync('mkfifo', ['-m', '600', receiptFile(f.dataRoot)]);
|
||||
assert.equal(receiptsText(f.dataRoot), null);
|
||||
});
|
||||
|
||||
test('the change log read takes the writer\'s rule: group readable is fine, group writable, a symlink or a missing file is not', t => {
|
||||
const dir = fs.mkdtempSync(join(tmpdir(), 'rdo-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
||||
const file = join(dir, 'changes.jsonl'); fs.writeFileSync(file, 'x\n', { mode: 0o600 }); fs.chmodSync(file, 0o640);
|
||||
assert.equal(ownedText(file, { mask: 0o022 }), 'x\n');
|
||||
fs.chmodSync(file, 0o660);
|
||||
assert.throws(() => ownedText(file, { mask: 0o022 }), { code: 'proxy-file-refused' });
|
||||
fs.chmodSync(file, 0o600); fs.symlinkSync(file, join(dir, 'link'));
|
||||
assert.throws(() => ownedText(join(dir, 'link'), { mask: 0o022 }), { code: 'ELOOP' });
|
||||
assert.throws(() => ownedText(join(dir, 'gone'), { mask: 0o022 }), { code: 'ENOENT' });
|
||||
// readRider, as the Console wires it: a refused log is unreadable, a missing one is empty.
|
||||
const doc = { plugins: { 'model-provider': [] } };
|
||||
fs.chmodSync(file, 0o660);
|
||||
assert.equal(readRider({ id: 'a', read: () => doc, readLog: () => ownedText(file, { mask: 0o022 }), isFixture: () => false }).changes, null);
|
||||
assert.equal(readRider({ id: 'a', read: () => doc, readLog: () => ownedText(join(dir, 'gone'), { mask: 0o022 }), isFixture: () => false }).changes, '');
|
||||
});
|
||||
|
||||
test('a fixture organization that uses cliproxy now, with a removal in its log, is removed, not movable', () => {
|
||||
const log = committed([{ path: 'plugins.model-provider', old: ['fixture', 'cliproxy'], new: ['fixture'] }]) + committed([{ path: 'plugins.model-provider', old: ['fixture'], new: ['fixture', 'cliproxy'] }]);
|
||||
assert.equal(localRide({ fixture: true, cliproxy: true, changes: log }), 'removed');
|
||||
assert.equal(localRide({ fixture: true, cliproxy: true, changes: '' }), 'fixture');
|
||||
});
|
||||
|
||||
test('an organization ride all once added and its owner then saved without cliproxy is skipped as removed', async t => {
|
||||
// The organization's file as the Console reads it. The organization admin page saves a whole
|
||||
// document and writes no change-log entry, so the log stays empty after the owner's removal.
|
||||
let cliproxy = false;
|
||||
const b = bus({ providers: [] });
|
||||
const { admin: a, dataRoot } = admin(t, { riders: async () => [{ id: 'a', fixture: false, cliproxy, changes: '', call: b.call }] });
|
||||
assert.deepEqual((await a.rideAll()).added, ['a']);
|
||||
cliproxy = true;
|
||||
assert.deepEqual((await a.rideAll()).already, ['a']);
|
||||
cliproxy = false; // the owner saved the organization without cliproxy on its admin page
|
||||
const second = await a.rideAll();
|
||||
assert.deepEqual(second.skipped, [{ id: 'a', reason: 'removed' }], JSON.stringify(second));
|
||||
assert.equal(b.calls.filter(([verb]) => verb === 'plugins.set').length, 1, 'no second write');
|
||||
assert.deepEqual((await a.status()).ride, [{ id: 'a', state: 'removed' }]);
|
||||
// A fixture organization the proxy once moved, and its owner then put back on stand-in models, is not moved again.
|
||||
const moved = { id: 'f', fixture: true, cliproxy: false, changes: '', call: bus({ down: true }).call };
|
||||
const m = admin(t, { riders: async () => [moved], ownerModels: async () => ({ proxy: true, riding: true, models: [row()] }), move: async () => ({ moved: true, model: row().key }) });
|
||||
appendReceipt(m.dataRoot, { op: 'ride-move', org: 'f', model: row().key });
|
||||
assert.deepEqual(await m.admin.rideMove({ org: 'f' }), { org: 'f', moved: false, reason: 'removed' });
|
||||
// Receipts that cannot be read name every organization without cliproxy unreadable.
|
||||
fs.chmodSync(receiptFile(dataRoot), 0o200);
|
||||
assert.deepEqual((await a.rideAll()).skipped, [{ id: 'a', reason: 'unreadable' }]);
|
||||
});
|
||||
|
||||
test('the owner model is a usable catalogue row that serves every harness, claude first', () => {
|
||||
assert.equal(pickModel([glm, row()]).key, 'claude-claude-opus-5-5');
|
||||
assert.equal(pickModel([glm]).key, 'zai-glm-5-3');
|
||||
assert.equal(pickModel([glm, row()], ['claude-code']).key, 'claude-claude-opus-5-5');
|
||||
assert.equal(pickModel([glm], ['claude-code']), null);
|
||||
assert.equal(pickModel([row({ available: false }), row({ reason: 'unverified' }), row({ module: 'fixture' })]), null, 'a model the catalogue does not offer is never picked');
|
||||
assert.equal(pickModel(null), null);
|
||||
});
|
||||
|
||||
test('ride all adds cliproxy through plugins.set and skips and names each organization it must not change', async t => {
|
||||
const ready = bus({ providers: [] }), noClient = bus({ providers: [], client: false }), down = bus({ down: true }), checkFails = bus({ providers: [], accounts: busError('proxy-down') });
|
||||
const refused = bus({ providers: [], set: busError('revision-conflict') }), alreadyBus = bus({ providers: ['cliproxy'] }), removedBus = bus({ providers: [] }), fixtureBus = bus({ down: true });
|
||||
const unreadable = bus({ providers: [] }), raced = bus({ providers: ['cliproxy'] });
|
||||
const riders = async () => [
|
||||
{ id: 'ready', fixture: false, cliproxy: false, changes: '', call: ready.call },
|
||||
{ id: 'removed', fixture: false, cliproxy: false, changes: committed([{ path: 'plugins.model-provider', old: ['cliproxy'], new: [] }]), call: removedBus.call },
|
||||
{ id: 'down', fixture: false, cliproxy: false, changes: '', call: down.call },
|
||||
{ id: 'noclient', fixture: false, cliproxy: false, changes: '', call: noClient.call },
|
||||
{ id: 'checkfails', fixture: false, cliproxy: false, changes: '', call: checkFails.call },
|
||||
{ id: 'refused', fixture: false, cliproxy: false, changes: '', call: refused.call },
|
||||
{ id: 'already', fixture: false, cliproxy: true, changes: '', call: alreadyBus.call },
|
||||
{ id: 'raced', fixture: false, cliproxy: false, changes: '', call: raced.call },
|
||||
{ id: 'unreadable', fixture: undefined, cliproxy: undefined, changes: '', call: unreadable.call },
|
||||
{ id: 'stand-in', fixture: true, cliproxy: false, changes: '', call: fixtureBus.call, status: async () => ({ pid: null }) },
|
||||
];
|
||||
const { admin: a, dataRoot } = admin(t, { riders });
|
||||
const result = await a.rideAll();
|
||||
assert.deepEqual(result.added, ['ready']);
|
||||
assert.deepEqual(result.already, ['already', 'raced']);
|
||||
assert.deepEqual(result.skipped, [
|
||||
{ id: 'removed', reason: 'removed' }, { id: 'down', reason: 'bus-down' }, { id: 'noclient', reason: 'no-proxy-client' },
|
||||
{ id: 'checkfails', reason: 'proxy-check-failed' }, { id: 'refused', reason: 'revision-conflict' }, { id: 'unreadable', reason: 'unreadable' },
|
||||
]);
|
||||
assert.deepEqual(result.fixtures, [{ id: 'stand-in', running: false }]);
|
||||
assert.deepEqual(ready.calls.find(([v]) => v === 'plugins.set'), ['plugins.set', { revision: 'rev-1', plugins: { 'model-provider': ['cliproxy'] } }]);
|
||||
for (const [name, b] of Object.entries({ removedBus, down, noClient, checkFails, alreadyBus, raced, unreadable, fixtureBus }))
|
||||
assert.equal(b.calls.some(([v]) => v === 'plugins.set'), false, `${name} gets no write`);
|
||||
assert.equal(JSON.stringify(result).includes('must not leak'), false);
|
||||
const last = receipts(dataRoot).at(-1);
|
||||
assert.equal(last.op, 'ride-all'); assert.deepEqual(last.added, ['ready']); assert.deepEqual(last.fixtures, ['stand-in']);
|
||||
});
|
||||
|
||||
test('ride all keeps the providers an organization already has', async t => {
|
||||
const b = bus({ providers: ['openai'] });
|
||||
const { admin: a } = admin(t, { riders: async () => [{ id: 'o', fixture: false, cliproxy: false, changes: '', call: b.call }] });
|
||||
assert.deepEqual((await a.rideAll()).added, ['o']);
|
||||
assert.deepEqual(b.calls.find(([v]) => v === 'plugins.set')[1].plugins, { 'model-provider': ['openai', 'cliproxy'] });
|
||||
});
|
||||
|
||||
test('status names ride all after accounts while an organization can still ride', async t => {
|
||||
let list = [{ id: 'o', fixture: false, cliproxy: false, changes: '', call: bus().call }];
|
||||
const f = admin(t, { riders: async () => list, unitState: { installed: true, active: 'active', healthy: true, pid: 3 } });
|
||||
const binDir = join(f.dataRoot, 'opt', 'cliproxyapi', PIN.version);
|
||||
fs.mkdirSync(join(binDir, 'static'), { recursive: true });
|
||||
fs.writeFileSync(join(binDir, 'cli-proxy-api'), 'b'); fs.writeFileSync(join(binDir, 'static', 'management.html'), 'p');
|
||||
const files = initFiles(f.configDir, f.dataRoot);
|
||||
fs.mkdirSync(join(f.configDir, 'secrets'), { mode: 0o700 }); fs.writeFileSync(files.key, 'fixture', { mode: 0o600 });
|
||||
fs.mkdirSync(files.proxyDir, { mode: 0o700 });
|
||||
fs.writeFileSync(files.settings, JSON.stringify({ binary: join(binDir, 'cli-proxy-api'), port: 18432 }), { mode: 0o600 });
|
||||
let status = await f.admin.status();
|
||||
assert.deepEqual(status.next, ['accounts', 'ride-all']);
|
||||
assert.deepEqual(status.ride, [{ id: 'o', state: 'ready' }]);
|
||||
list = [{ id: 'o', fixture: true, cliproxy: false, changes: '', call: bus().call }];
|
||||
assert.deepEqual((await f.admin.status()).next, ['accounts', 'ride-all'], 'a fixture organization can still be moved');
|
||||
list = [{ id: 'o', fixture: false, cliproxy: true, changes: '', call: bus().call }];
|
||||
status = await f.admin.status();
|
||||
assert.deepEqual(status.next, ['accounts']);
|
||||
list = [{ id: 'o', fixture: false, cliproxy: false, changes: committed([{ path: 'plugins.model-provider', old: ['cliproxy'], new: [] }]), call: bus().call }];
|
||||
assert.deepEqual((await f.admin.status()).next, ['accounts'], 'a removed organization is not a reason to ride');
|
||||
f.state.healthy = false;
|
||||
list = [{ id: 'o', fixture: false, cliproxy: false, changes: '', call: bus().call }];
|
||||
assert.deepEqual((await f.admin.status()).next, ['unit-restart'], 'never before the proxy answers');
|
||||
});
|
||||
|
||||
test('a fixture organization moves only when stopped, with a proxy, a riding bus and a move seam', async t => {
|
||||
const owner = { proxy: true, riding: true, models: [row()] };
|
||||
const moves = [];
|
||||
const move = async (id, models, principal) => { moves.push({ id, models, principal }); return { moved: true, model: models[0].key }; };
|
||||
let stoppedBus = bus({ down: true }), runningBus = bus();
|
||||
// Round 3 (P2c): stopped is the organization admin page's test, a bus status with no process id.
|
||||
const stopped = async () => ({ healthy: false, pid: null }), running = async () => ({ healthy: true, pid: 4242 });
|
||||
const riders = async () => [
|
||||
{ id: 'stand-in', fixture: true, cliproxy: false, changes: '', call: stoppedBus.call, status: stopped },
|
||||
{ id: 'live', fixture: true, cliproxy: false, changes: '', call: runningBus.call, status: running },
|
||||
{ id: 'busy', fixture: true, cliproxy: false, changes: '', call: stoppedBus.call, status: async () => ({ healthy: false, pid: 4343 }) },
|
||||
{ id: 'unproven', fixture: true, cliproxy: false, changes: '', call: stoppedBus.call, status: async () => { throw busError('stale-host'); } },
|
||||
{ id: 'no-status', fixture: true, cliproxy: false, changes: '', call: stoppedBus.call },
|
||||
{ id: 'real', fixture: false, cliproxy: false, changes: '', call: stoppedBus.call, status: stopped },
|
||||
];
|
||||
let ownerNow = owner;
|
||||
const f = admin(t, { riders, ownerModels: async () => ownerNow, move });
|
||||
const principal = { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' };
|
||||
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, 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: 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 };
|
||||
assert.deepEqual(await f.admin.rideMove({ org: 'stand-in', principal }), { org: 'stand-in', moved: false, reason: 'no-riding-bus' });
|
||||
assert.equal(moves.length, 0);
|
||||
ownerNow = owner;
|
||||
const done = await f.admin.rideMove({ org: 'stand-in', principal });
|
||||
assert.equal(done.moved, true); assert.equal(done.model, 'claude-claude-opus-5-5');
|
||||
assert.deepEqual(moves, [{ id: 'stand-in', models: [row()], principal }]);
|
||||
const last = receipts(f.dataRoot).at(-1);
|
||||
assert.deepEqual({ op: last.op, org: last.org, model: last.model }, { op: 'ride-move', org: 'stand-in', model: 'claude-claude-opus-5-5' });
|
||||
const none = admin(t, { riders, ownerModels: async () => owner, move: null });
|
||||
assert.deepEqual(await none.admin.rideMove({ org: 'stand-in', principal }), { org: 'stand-in', moved: false, reason: 'not-available' });
|
||||
const throws = admin(t, { riders, ownerModels: async () => owner, move: async () => { throw Object.assign(new Error('raw text'), { code: 'revision-conflict' }); } });
|
||||
assert.deepEqual(await throws.admin.rideMove({ org: 'stand-in', principal }), { org: 'stand-in', moved: false, reason: 'revision-conflict' });
|
||||
});
|
||||
|
||||
test('the fixture move rewrites the model provider, the model entry and every role that used it', async () => {
|
||||
const doc = await orgService.templateRender('alpha', { fixture: 'local' });
|
||||
Object.assign(doc.roles.assistant.vars, { 'fallback.harness': 'pi', 'fallback.model': 'fixture-deterministic', 'fallback.thinking': 'off' });
|
||||
doc.projects.work.agents = { helper: { definition: 'coder', vars: { harness: 'pi', model: 'fixture-deterministic', thinking: 'off' } } };
|
||||
assert.deepEqual(await orgService.validateOrg(doc), { ok: true, errors: [] });
|
||||
assert.equal(orgUsesFixture(doc), true);
|
||||
const { doc: next, model } = fixtureMove(doc, [glm, row()]);
|
||||
assert.equal(model.key, 'claude-claude-opus-5-5', 'the fixture assistant runs pi; of the models that serve pi, claude comes first');
|
||||
assert.equal(orgUsesFixture(next), false);
|
||||
assert.deepEqual(next.plugins['model-provider'], ['cliproxy']);
|
||||
assert.deepEqual(next.launch.models, { 'claude-claude-opus-5-5': { provider: 'anthropic', model: 'claude-opus-5-5', module: 'cliproxy', upstream: 'claude' } });
|
||||
assert.equal(next.roles.assistant.vars.model, 'claude-claude-opus-5-5');
|
||||
assert.equal(next.roles.assistant.vars['fallback.model'], 'claude-claude-opus-5-5');
|
||||
assert.equal(next.roles.assistant.vars.harness, 'pi', 'the harness is left as it was');
|
||||
assert.equal(next.projects.work.agents.helper.vars.model, 'claude-claude-opus-5-5', 'a project\'s own agent moves too');
|
||||
assert.deepEqual(await orgService.validateOrg(next), { ok: true, errors: [] });
|
||||
const zai = fixtureMove(doc, [glm]);
|
||||
assert.equal(zai.model.key, 'zai-glm-5-3');
|
||||
assert.deepEqual(zai.doc.launch.models, { 'zai-glm-5-3': { provider: 'zai', model: 'glm-5.3', module: 'cliproxy', upstream: 'zai' } });
|
||||
assert.deepEqual(await orgService.validateOrg(zai.doc), { ok: true, errors: [] });
|
||||
assert.equal(doc.plugins['model-provider'][0], 'fixture', 'the input is not changed');
|
||||
assert.deepEqual(fixtureMove(doc, [row({ harnesses: ['claude-code'] })]), { reason: 'no-model' }, 'a model that cannot serve the harness is not used');
|
||||
assert.deepEqual(fixtureMove(doc, [row({ available: false, harnesses: ['pi'] })]), { reason: 'no-model' }, 'a model the catalogue does not offer is not used');
|
||||
});
|
||||
|
||||
test('the fixture move writes through the organization service as the owner, and only at the revision read', async () => {
|
||||
const root = fs.mkdtempSync(join(tmpdir(), 'rd-svc-'));
|
||||
try {
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
fs.mkdirSync(configDir, { mode: 0o700 }); fs.mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const ctx = { configDir, dataRoot, instance: 'test', principal: { provider: 'local-dev', iss: 'local-dev', sub: 'local-dev-owner' } };
|
||||
await orgService.createOrg(ctx, 'alpha', await orgService.templateRender('alpha', { fixture: 'local' }));
|
||||
const stranger = { ...ctx, principal: { provider: 'sso', iss: 'https://idp.invalid', sub: 'someone' } };
|
||||
await assert.rejects(moveFixtureOrg({ service: orgService, ctx: stranger, id: 'alpha', models: [row()] }));
|
||||
assert.equal(orgUsesFixture((await orgService.showOrg(ctx, 'alpha')).doc), true, 'a stranger moved nothing');
|
||||
assert.deepEqual(await moveFixtureOrg({ service: orgService, ctx, id: 'alpha', models: [row({ harnesses: ['claude-code'] })] }), { moved: false, reason: 'no-model' });
|
||||
// A moved document the service does not accept is not written.
|
||||
const refusing = { ...orgService, validateOrg: async () => ({ ok: false, errors: [{ path: '$', message: 'no' }] }) };
|
||||
assert.deepEqual(await moveFixtureOrg({ service: refusing, ctx, id: 'alpha', models: [row()] }), { moved: false, reason: 'invalid' });
|
||||
assert.equal(orgUsesFixture((await orgService.showOrg(ctx, 'alpha')).doc), true, 'an invalid move wrote nothing');
|
||||
// A write between the read and the update loses: the service refuses the stale revision.
|
||||
const racing = { ...orgService, validateOrg: async d => { const now = await orgService.showOrg(ctx, 'alpha'); await orgService.updateOrg(ctx, 'alpha', now.doc, { ifRevision: now.revision }); return orgService.validateOrg(d); } };
|
||||
await assert.rejects(moveFixtureOrg({ service: racing, ctx, id: 'alpha', models: [row()] }), { code: 'revision-conflict' });
|
||||
assert.equal(orgUsesFixture((await orgService.showOrg(ctx, 'alpha')).doc), true);
|
||||
assert.deepEqual(await moveFixtureOrg({ service: orgService, ctx, id: 'alpha', models: [row()] }), { moved: true, model: 'claude-claude-opus-5-5' });
|
||||
const after = (await orgService.showOrg(ctx, 'alpha')).doc;
|
||||
assert.equal(orgUsesFixture(after), false);
|
||||
assert.deepEqual(after.plugins['model-provider'], ['cliproxy']);
|
||||
assert.equal(after.roles.assistant.vars.model, 'claude-claude-opus-5-5');
|
||||
assert.deepEqual(after.launch.models['claude-claude-opus-5-5'], { provider: 'anthropic', model: 'claude-opus-5-5', module: 'cliproxy', upstream: 'claude' });
|
||||
} finally { fs.rmSync(root, { recursive: true, force: true }); }
|
||||
});
|
||||
|
||||
test('an organization is read from its file and change log; a missing log is empty and any other failure unreadable', () => {
|
||||
const enoent = () => { throw Object.assign(new Error('missing'), { code: 'ENOENT' }); };
|
||||
const eacces = () => { throw Object.assign(new Error('denied'), { code: 'EACCES' }); };
|
||||
const doc = providers => ({ plugins: { 'model-provider': providers } });
|
||||
assert.deepEqual(readRider({ id: 'a', read: () => doc(['cliproxy']), readLog: () => 'log', isFixture: () => false }), { id: 'a', fixture: false, cliproxy: true, changes: 'log' });
|
||||
assert.deepEqual(readRider({ id: 'b', read: () => doc(['fixture']), readLog: enoent, isFixture: () => true }), { id: 'b', fixture: true, cliproxy: false, changes: '' });
|
||||
assert.deepEqual(readRider({ id: 'c', read: () => ({}), readLog: eacces, isFixture: () => false }), { id: 'c', fixture: false, cliproxy: false, changes: null });
|
||||
const unread = readRider({ id: 'd', read: () => { throw new Error('bad json'); }, readLog: () => '', isFixture: () => true });
|
||||
assert.deepEqual(unread, { id: 'd', fixture: undefined, cliproxy: undefined, changes: '' });
|
||||
assert.equal(localRide(unread), 'unreadable');
|
||||
assert.equal(localRide(readRider({ id: 'c', read: () => ({}), readLog: eacces, isFixture: () => false })), 'unreadable');
|
||||
// The real fixture test decides the fixture field.
|
||||
assert.equal(readRider({ id: 'e', read: () => doc(['fixture']), readLog: () => '', isFixture: orgUsesFixture }).fixture, true);
|
||||
});
|
||||
|
||||
test('the owner models come only from riding buses that answer a ready catalogue, cliproxy rows once each', async () => {
|
||||
const asked = [];
|
||||
const rider = (id, cliproxy, answer) => ({ id, cliproxy, call: async verb => { asked.push([id, verb]); if (answer instanceof Error) throw answer; return answer; } });
|
||||
const ready = models => ({ catalogue: { status: 'ready', models } });
|
||||
assert.deepEqual(await ownerModelsFrom({ proxy: false, riders: [rider('a', true, ready([row()]))] }), { proxy: false, riding: false, models: [] });
|
||||
assert.deepEqual(asked, [], 'no proxy asks no bus');
|
||||
const fixtureRow = row({ key: 'fixture-deterministic', module: 'fixture' });
|
||||
const answer = await ownerModelsFrom({ proxy: true, riders: [
|
||||
rider('a', true, ready([row(), fixtureRow])), rider('b', true, ready([row({ label: 'later copy' }), glm])), rider('c', false, ready([row({ key: 'not-riding' })])),
|
||||
rider('d', true, { catalogue: { status: 'unavailable', models: [row({ key: 'fenced' })] } }), rider('e', true, busError('no-bus-host')),
|
||||
] });
|
||||
assert.deepEqual(answer, { proxy: true, riding: true, models: [row(), glm] });
|
||||
assert.deepEqual(asked.map(([id]) => id).sort(), ['a', 'b', 'd', 'e'], 'only organizations that list cliproxy are asked');
|
||||
assert.deepEqual(await ownerModelsFrom({ proxy: true, riders: [rider('d', true, { catalogue: { status: 'unavailable', models: [] } }), rider('e', true, busError('no-bus-host'))] }), { proxy: true, riding: false, models: [] });
|
||||
// A hung bus does not hold the others back: they are asked together.
|
||||
let release; const hung = { id: 'h', cliproxy: true, call: () => new Promise(r => { release = r; }) };
|
||||
const pending = ownerModelsFrom({ proxy: true, riders: [hung, rider('a', true, ready([row()]))] });
|
||||
await new Promise(r => setImmediate(r));
|
||||
assert.equal(asked.filter(([id]) => id === 'a').length, 2, 'the second bus was asked while the first hung');
|
||||
release(null); assert.deepEqual((await pending).models, [row()]);
|
||||
// Review round: a key one bus lists as unavailable and another as usable keeps the usable row.
|
||||
const stale = row({ available: false, reason: 'proxy-down', label: 'stale' });
|
||||
assert.deepEqual((await ownerModelsFrom({ proxy: true, riders: [rider('s', true, ready([stale])), rider('a', true, ready([row()]))] })).models, [row()]);
|
||||
assert.deepEqual((await ownerModelsFrom({ proxy: true, riders: [rider('a', true, ready([row()])), rider('s', true, ready([stale]))] })).models, [row()]);
|
||||
});
|
||||
@@ -83,15 +83,18 @@ async function discoverPresets() {
|
||||
}
|
||||
return presets;
|
||||
}
|
||||
export async function orgPresets({ env = process.env } = {}) {
|
||||
// `owner` (#1873 ride-default) is what the Console read from the owner's model proxy:
|
||||
// {proxy, riding, models}. A preset that does not use it ignores it.
|
||||
export async function orgPresets({ env = process.env, owner } = {}) {
|
||||
const presets = await discoverPresets();
|
||||
return Promise.all([...presets.values()].map(async p => {
|
||||
const availability = await p.available({ env });
|
||||
const availability = await p.available({ env, owner });
|
||||
if (!availability || typeof availability.ok !== 'boolean' || (!availability.ok && (typeof availability.reason !== 'string' || !availability.reason))) fail('org-invalid', 'invalid preset availability');
|
||||
return { name: p.name, label: p.label, description: p.description, thinking: structuredClone(p.thinking), available: availability.ok ? { ok: true } : { ok: false, reason: availability.reason } };
|
||||
}));
|
||||
}
|
||||
export async function templateRender(id, { fixture, preset, thinking } = {}) {
|
||||
// `model` (#1873 ride-default) is one row of the owner's catalogue, for a preset that routes to it.
|
||||
export async function templateRender(id, { fixture, preset, thinking, model } = {}) {
|
||||
if (fixture !== undefined && fixture !== 'local') fail('org-invalid', 'unknown fixture');
|
||||
if (fixture !== undefined && preset !== undefined) fail('org-invalid', 'choose fixture or preset');
|
||||
if (fixture === 'local') preset = 'fixture';
|
||||
@@ -105,7 +108,8 @@ export async function templateRender(id, { fixture, preset, thinking } = {}) {
|
||||
if (!p) fail('org-invalid', 'unknown preset');
|
||||
const level = thinking === undefined ? p.thinking.default : thinking;
|
||||
if (!p.thinking.levels.includes(level)) fail('org-invalid', 'unknown thinking level');
|
||||
const overlay = await p.overlay({ thinking: level });
|
||||
let overlay;
|
||||
try { overlay = await p.overlay({ thinking: level, model }); } catch (e) { fail('org-invalid', e?.code === 'org-invalid' && typeof e.message === 'string' ? e.message : 'invalid preset overlay'); }
|
||||
if (!overlay || typeof overlay !== 'object' || !overlay.plugins || !overlay.launch || !overlay.assistantVars) fail('org-invalid', 'invalid preset overlay');
|
||||
doc.plugins = structuredClone(overlay.plugins); doc.launch = structuredClone(overlay.launch);
|
||||
doc.roles.assistant.vars = structuredClone(overlay.assistantVars);
|
||||
|
||||
@@ -30,3 +30,45 @@ test('unknown presets and thinking levels refuse', async () => {
|
||||
await assert.rejects(templateRender('alpha', { preset: 'claude-code', thinking: 'off' }), /thinking/);
|
||||
await assert.rejects(templateRender('alpha', { thinking: 'low' }), /thinking/);
|
||||
});
|
||||
|
||||
// #1873 ride-default slice 1: the cliproxy preset routes a new organization to a model the
|
||||
// owner's catalogue lists, and says plainly why it is unavailable.
|
||||
const row = (over = {}) => ({ key: 'claude-claude-opus-5-5', module: 'cliproxy', upstream: 'claude', provider: 'anthropic', model: 'claude-opus-5-5',
|
||||
label: 'claude / claude-opus-5-5', available: true, reason: null, harnesses: ['claude-code', 'pi'], ...over });
|
||||
const cliproxyOf = async owner => (await service.orgPresets({ env: { PATH: '/usr/bin:/bin' }, owner })).find(p => p.name === 'cliproxy');
|
||||
|
||||
test('the cliproxy preset is discovered and names each reason it is unavailable', async () => {
|
||||
const none = await cliproxyOf(undefined);
|
||||
assert.equal(none.available.ok, false); assert.match(none.available.reason, /Set up the model proxy first/);
|
||||
assert.match((await cliproxyOf({ proxy: false, riding: true, models: [row()] })).available.reason, /Set up the model proxy first/);
|
||||
assert.match((await cliproxyOf({ proxy: true, riding: false, models: [row()] })).available.reason, /No organization uses the model proxy yet/);
|
||||
assert.match((await cliproxyOf({ proxy: true, riding: true, models: [] })).available.reason, /lists no model you can use/);
|
||||
for (const bad of [row({ available: false }), row({ reason: 'unverified' }), row({ module: 'fixture' }), row({ harnesses: [] })])
|
||||
assert.match((await cliproxyOf({ proxy: true, riding: true, models: [bad] })).available.reason, /lists no model you can use/);
|
||||
const ok = await cliproxyOf({ proxy: true, riding: true, models: [row()] });
|
||||
assert.deepEqual(ok.available, { ok: true });
|
||||
assert.deepEqual(ok.thinking, { levels: ['low', 'medium', 'high', 'xhigh', 'max'], default: 'low' });
|
||||
});
|
||||
|
||||
test('the cliproxy overlay routes to the catalogue model it is given and validates', async () => {
|
||||
const doc = await templateRender('alpha', { preset: 'cliproxy', model: row() });
|
||||
assert.deepEqual(doc.plugins['model-provider'], ['cliproxy']);
|
||||
assert.deepEqual(doc.launch.models, { 'claude-claude-opus-5-5': { provider: 'anthropic', model: 'claude-opus-5-5', module: 'cliproxy', upstream: 'claude' } });
|
||||
assert.deepEqual(doc.roles.assistant.vars, { harness: 'claude-code', model: 'claude-claude-opus-5-5', thinking: 'low' });
|
||||
assert.equal(doc.vars, undefined);
|
||||
assert.deepEqual(await validateOrg(doc), { ok: true, errors: [] });
|
||||
const glm = await templateRender('alpha', { preset: 'cliproxy', thinking: 'high', model: row({ key: 'zai-glm-5-3', upstream: 'zai', provider: 'zai', model: 'glm-5.3', harnesses: ['pi'] }) });
|
||||
assert.deepEqual(glm.roles.assistant.vars, { harness: 'pi', model: 'zai-glm-5-3', thinking: 'high' });
|
||||
assert.deepEqual(glm.plugins.harness, ['pi']);
|
||||
assert.deepEqual(await validateOrg(glm), { ok: true, errors: [] });
|
||||
await assert.rejects(templateRender('alpha', { preset: 'cliproxy' }), /lists no such model/);
|
||||
await assert.rejects(templateRender('alpha', { preset: 'cliproxy', model: row({ available: false }) }), /lists no such model/);
|
||||
});
|
||||
|
||||
test('existing presets ignore the new model and owner inputs', async () => {
|
||||
assert.deepEqual(await templateRender('alpha', { preset: 'fixture', model: row() }), await templateRender('alpha', { preset: 'fixture' }));
|
||||
assert.deepEqual(await templateRender('alpha', { preset: 'claude-code', model: row() }), await templateRender('alpha', { preset: 'claude-code' }));
|
||||
const presets = await service.orgPresets({ env: { PATH: '/usr/bin:/bin' }, owner: { proxy: true, riding: true, models: [row()] } });
|
||||
assert.equal(presets.find(p => p.name === 'fixture').available.ok, true);
|
||||
assert.equal(presets.find(p => p.name === 'claude-code').available.ok, false);
|
||||
});
|
||||
@@ -258,6 +258,28 @@ export returns:
|
||||
`gateTimeout`, `bundle`, `env`, `turnEnd`, `outputPolicy` and `transcript` give its
|
||||
sandbox binds, web, tool names, S0 lines, bundle files, adapter variables,
|
||||
how a turn ends and where its transcripts land.
|
||||
- `envNames` and `installRoots({env})` (#1873 triage-fences) are optional and
|
||||
outside `kinds.mjs`. A session the Console hosts with no organization
|
||||
(Triage) reads them; organization launches do not. `envNames` lists the
|
||||
names `env()` returns, less the provider's. The host takes a route's
|
||||
`harnessEnv` name only when it is listed here and is not one the host
|
||||
always refuses: `MOSAIC_*`, `NODE_*`, `LD_*`, `DYLD_*`, `PYTHON*`, `BASH_*`,
|
||||
`GIT_*`, `MALLOC_*`, `NPM_CONFIG_*`, `XDG_*`, any name ending in `_DIR`,
|
||||
`_CONF`, `_CONFIG`, `_ASKPASS`, `_CA_BUNDLE` or `_BASE_URL`, PATH, HOME,
|
||||
TMPDIR, ENV, the founder's and the provider's names, and named shell,
|
||||
proxy, resolver, libc, OpenSSL and Java hooks, Perl and Ruby start
|
||||
options and library paths, less's input filters, Lua's start code,
|
||||
curl's config home and a TLS key log. The full list is
|
||||
`REFUSED_NAMES` in `packages/webui/src/triage-host.mjs`. `installRoots` lists the directories
|
||||
where the adapter and the mounts live. A hosted adapter or mount source
|
||||
must resolve inside one of them, and never in or around the data root,
|
||||
the config directory, HOME, `/srv/mosaic` or a path with a `secret`
|
||||
segment. HOME does not count against a source inside the stack's own
|
||||
code, so a copy or a worktree under HOME still passes. Every claude-code
|
||||
name is `MOSAIC_*`, so a hosted session cannot run Claude Code yet. pi's
|
||||
`MOSAIC_TOOLS`, `MOSAIC_EXTENSIONS` and `MOSAIC_THINKING` are refused the
|
||||
same way, so a hosted pi gets no tools and no extension until the host
|
||||
sets those names itself.
|
||||
- `compactions({store, cursors, budget, streams, first})` reads a thread
|
||||
session's native store for row 270's compaction count (row 307). Core keeps
|
||||
the ledger (`packages/seat/src/compactions.mjs`) and passes `store` as
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
// session.json. packages/plugins/README.md lists the interface.
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { accessSync, constants, realpathSync, statSync } from "node:fs";
|
||||
import { delimiter, isAbsolute, join } from "node:path";
|
||||
import { delimiter, dirname, isAbsolute, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { claudeBuiltins, MCP_PREFIX } from "../../../harness/src/gate.mjs";
|
||||
import { hasSessionCredential, SESSION_CREDENTIALS } from "../../../seat/src/session.mjs";
|
||||
@@ -145,6 +145,16 @@ export function env({ files, policy, session, proxy = null }) {
|
||||
};
|
||||
}
|
||||
|
||||
// #1873 triage-fences: the names env() returns, less the proxy's, and the directories its adapter
|
||||
// and mounts live in: the pinned binary's when MOSAIC_CLAUDE_BIN is set. A hosted session's host
|
||||
// (Triage) takes harnessEnv names and mount sources only from these; launches don't read them.
|
||||
export const envNames = Object.freeze(["MOSAIC_TOOLS", "MOSAIC_CLAUDE_SETTINGS", "MOSAIC_CLAUDE_MCP_CONFIG", "MOSAIC_THINKING", "MOSAIC_MODEL_ACTIVITY_SOURCE"]);
|
||||
export function installRoots({ env = {} } = {}) {
|
||||
const roots = [join(REPO, "adapters", "claude"), join(REPO, "tools", "unslop-hook")];
|
||||
if (env.MOSAIC_CLAUDE_BIN) roots.push(dirname(realpathSync(env.MOSAIC_CLAUDE_BIN)));
|
||||
return roots;
|
||||
}
|
||||
|
||||
// A turn ends when claude exits; there is no marker.
|
||||
export const turnEnd = () => "exit";
|
||||
export const transcript = ({ runDir, store = null }) => (store ? store.projects : join(runDir, "home", ".claude", "projects"));
|
||||
|
||||
@@ -118,6 +118,14 @@ export function env({ policy, tools, session, proxy = null }) {
|
||||
};
|
||||
}
|
||||
|
||||
// #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")];
|
||||
|
||||
// The extension writes the turn marker at agent_end; an exit without it is a failed turn.
|
||||
export const turnEnd = () => "marker";
|
||||
export const transcript = ({ runDir, store = null }) => (store ? store.session : join(runDir, "session"));
|
||||
|
||||
@@ -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,40 @@ 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] };
|
||||
}
|
||||
|
||||
// #1873 ride-default slice 1: a new organization that uses the owner's models through the
|
||||
// instance's model proxy. The Console reads the owner's catalogue from a riding bus and passes
|
||||
// it as `owner`; this module never reaches the proxy itself.
|
||||
const THINKING = Object.freeze(['low', 'medium', 'high', 'xhigh', 'max']);
|
||||
export const usableModel = m => !!m && m.module === 'cliproxy' && m.available === true && m.reason === null
|
||||
&& typeof m.key === 'string' && /^[a-z][a-z0-9-]{0,63}$/.test(m.key) && typeof m.upstream === 'string' && Object.hasOwn(ROUTES, m.upstream)
|
||||
&& typeof m.provider === 'string' && typeof m.model === 'string' && MODEL.test(m.model)
|
||||
&& Array.isArray(m.harnesses) && (m.harnesses.includes('claude-code') || m.harnesses.includes('pi'));
|
||||
export function orgPresets() {
|
||||
return [{ name: 'cliproxy', label: 'Your models through the model proxy',
|
||||
description: 'Uses a model your model proxy lists. Each reply spends your own account.',
|
||||
thinking: { levels: [...THINKING], default: 'low' },
|
||||
available: ({ owner } = {}) => {
|
||||
if (!owner || owner.proxy !== true) return { ok: false, reason: 'Set up the model proxy first, on the Model proxy page.' };
|
||||
if (owner.riding !== true) return { ok: false, reason: 'No organization uses the model proxy yet, or none answered. Use Ride all on the Model proxy page.' };
|
||||
if (!Array.isArray(owner.models) || !owner.models.some(usableModel)) return { ok: false, reason: 'The model proxy lists no model you can use. Sign in to an account on the Model proxy page.' };
|
||||
return { ok: true };
|
||||
},
|
||||
overlay: ({ thinking, model }) => {
|
||||
if (!usableModel(model)) throw Object.assign(Error('the model proxy lists no such model'), { code: 'org-invalid' });
|
||||
const harness = model.harnesses.includes('claude-code') ? 'claude-code' : 'pi';
|
||||
return {
|
||||
plugins: { channel: ['cli', 'webui'], harness: [harness], 'model-provider': ['cliproxy'] },
|
||||
launch: { by: 'chief', instances: ['assistant'], models: { [model.key]: { provider: model.provider, model: model.model, module: 'cliproxy', upstream: model.upstream } } },
|
||||
assistantVars: { harness, model: model.key, 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');
|
||||
});
|
||||
@@ -34,7 +34,7 @@
|
||||
// the launch (sandbox-unavailable).
|
||||
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { existsSync, lstatSync, mkdirSync, readlinkSync, realpathSync } from "node:fs";
|
||||
import { existsSync, lstatSync, mkdirSync, readlinkSync, realpathSync, statSync } from "node:fs";
|
||||
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { ID_PATTERN } from "../../organization/src/vocabulary.mjs";
|
||||
@@ -193,6 +193,177 @@ export function sandboxArgv(list, runDir, command, { business, role, bwrap = BWR
|
||||
return [bwrap, ...NAMESPACES, ...argsOf(list), "--remount-ro", "/", "--setenv", "HOME", join(runDir, "home"), "--setenv", "TMPDIR", "/tmp", ...identity, "--chdir", runDir, "--", ...KILL_WITH_PARENT, ...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
|
||||
// that same root, and outside every protected path: the data root, the config directory and HOME
|
||||
// the caller passes, the run directory, ALWAYS_PROTECTED, and any path with a segment that
|
||||
// contains "secret" in any case. A protected path counts in its own spelling and its real path, and
|
||||
// a source may neither lie inside one nor contain one. The bind's source is the real path, so a
|
||||
// link swapped after the check cannot steer it. A mount's dest (its source when it names none) is
|
||||
// never /tmp, the run directory or a system bind's dest, nor inside or around one, never a parent
|
||||
// of or equal to a runner file, the adapter or node, and never in or around another mount's dest.
|
||||
// At most HOSTED_MOUNTS_MAX mounts. A refusal is a HostedRouteError naming its fence, never a path.
|
||||
// `codeRoot` is the stack's own code and `codeHolder` the one protected path that may hold it:
|
||||
// HOME, when the stack runs from a copy or a worktree under it. That path does not refuse a source
|
||||
// whose named and real paths both lie in the code root. Every other check still applies to it, and
|
||||
// no other protected path, the run directory or ALWAYS_PROTECTED is ever set aside. A code root in
|
||||
// or around the run directory or ALWAYS_PROTECTED, in either path's spelling or real path, is refused.
|
||||
export const HOSTED_MOUNTS_MAX = 8;
|
||||
const ROOTS_MAX = 16;
|
||||
const PATH_MAX = 4096;
|
||||
const ALWAYS_PROTECTED = Object.freeze(["/srv/mosaic", "/proc", "/sys", "/dev", "/run", "/root", "/boot"]);
|
||||
const MOUNT_KEYS = Object.freeze(["src", "dest", "why"]);
|
||||
|
||||
export class HostedRouteError extends Error {
|
||||
constructor(fence, message) {
|
||||
super(`hosted binds: ${message}`);
|
||||
this.code = "hosted-binds-refused";
|
||||
this.fence = fence;
|
||||
}
|
||||
}
|
||||
const refuseRoute = (fence, message) => { throw new HostedRouteError(fence, message); };
|
||||
|
||||
// Absolute, already normal (no "..", ".", "//" or trailing slash), not "/", no NUL.
|
||||
const cleanPath = (p) => typeof p === "string" && p.length > 1 && p.length <= PATH_MAX && !p.includes("\0") && isAbsolute(p) && resolve(p) === p;
|
||||
const within = (child, parent) => child === parent || child.startsWith(parent + "/");
|
||||
const overlaps = (a, b) => within(a, b) || within(b, a);
|
||||
const secretPath = (p) => p.split("/").some((seg) => /secret/i.test(seg));
|
||||
const realOr = (p) => { try { return realpathSync(p); } catch { return p; } };
|
||||
const plainObject = (o) => o !== null && typeof o === "object" && !Array.isArray(o) && [Object.prototype, null].includes(Object.getPrototypeOf(o));
|
||||
|
||||
// `alsoFixed` is a test hook: it adds stand-in fixed paths and never removes a shipped one.
|
||||
export function checkHostedRoute({ adapter, harnessMounts, installRoots, protectedPaths, codeRoot = null, codeHolder = null, runDir, node = process.execPath, alsoFixed = [] }) {
|
||||
if (!cleanPath(runDir)) refuseRoute("protected", "the run directory is not a clean absolute path");
|
||||
if (!Array.isArray(protectedPaths)) refuseRoute("protected", "no protected paths were given");
|
||||
if (!Array.isArray(alsoFixed)) refuseRoute("protected", "the added fixed paths are not a list");
|
||||
// Lists are copied once, so a Proxy cannot answer one length to a check and another to the map.
|
||||
protectedPaths = Array.from(protectedPaths);
|
||||
// Each guarded entry: its spellings, and whether a source in the code root is clear of it. The
|
||||
// fixed paths and the run dir never hold the code. Their spellings are looked up once, here, and
|
||||
// serve both the code-root check and their guards, so the two always see the same real path.
|
||||
const fixed = [...ALWAYS_PROTECTED, ...Array.from(alsoFixed), runDir].map((p) => {
|
||||
if (!cleanPath(p)) refuseRoute("protected", "a protected path is not a clean absolute path");
|
||||
return { spellings: [p, realOr(p)], holdsCode: false };
|
||||
});
|
||||
let code = null;
|
||||
if (codeRoot !== null) {
|
||||
if (!cleanPath(codeRoot)) refuseRoute("protected", "the code root is not a clean absolute path");
|
||||
if (!protectedPaths.includes(codeHolder)) refuseRoute("protected", "the code root's holder is not a protected path");
|
||||
code = [codeRoot, realOr(codeRoot)];
|
||||
if (code.some((c) => fixed.some((g) => g.spellings.some((s) => overlaps(c, s))))) refuseRoute("protected", "the code root is in or around a path that is never set aside");
|
||||
}
|
||||
const guarded = [...fixed];
|
||||
for (const p of protectedPaths) {
|
||||
if (!cleanPath(p)) refuseRoute("protected", "a protected path is not a clean absolute path");
|
||||
const spellings = [p, realOr(p)];
|
||||
const holdsCode = p === codeHolder && code !== null && spellings.some((g) => code.some((c) => c !== g && within(c, g)));
|
||||
guarded.push({ spellings, holdsCode });
|
||||
}
|
||||
if (!Array.isArray(installRoots)) refuseRoute("roots", "the harness module declares no usable install roots");
|
||||
installRoots = Array.from(installRoots);
|
||||
if (installRoots.length === 0 || installRoots.length > ROOTS_MAX) refuseRoute("roots", "the harness module declares no usable install roots");
|
||||
const roots = installRoots.map((r) => {
|
||||
if (!cleanPath(r)) refuseRoute("roots", "an install root is not a clean absolute path");
|
||||
let real;
|
||||
try { real = realpathSync(r); } catch { refuseRoute("roots", "an install root is missing"); }
|
||||
if (real === "/" || !statSync(real).isDirectory()) refuseRoute("roots", "an install root is not a directory");
|
||||
return { named: r, real };
|
||||
});
|
||||
const source = (src, fence, what) => {
|
||||
if (!cleanPath(src)) refuseRoute(fence, `${what} is not a clean absolute path`);
|
||||
let real;
|
||||
try { real = realpathSync(src); } catch { refuseRoute(fence, `${what} is missing`); }
|
||||
if (!roots.some((r) => (within(src, r.named) || within(src, r.real)) && within(real, r.real))) refuseRoute(fence, `${what} is not under an install root`);
|
||||
const inCode = code !== null && code.some((c) => within(src, c)) && within(real, code[1]);
|
||||
if (guarded.some((g) => !(g.holdsCode && inCode) && [src, real].some((p) => g.spellings.some((s) => overlaps(p, s))))) refuseRoute(fence, `${what} is in or around a protected path`);
|
||||
if ([src, real].some(secretPath)) refuseRoute(fence, `${what} is in a secrets path`);
|
||||
return real;
|
||||
};
|
||||
const adapterReal = source(adapter, "adapter", "the adapter");
|
||||
if (!statSync(adapterReal).isFile()) refuseRoute("adapter", "the adapter is not a file");
|
||||
if (!Array.isArray(harnessMounts)) refuseRoute("mounts", `a hosted session takes at most ${HOSTED_MOUNTS_MAX} harness mounts`);
|
||||
harnessMounts = Array.from(harnessMounts);
|
||||
if (harnessMounts.length > HOSTED_MOUNTS_MAX) refuseRoute("mounts", `a hosted session takes at most ${HOSTED_MOUNTS_MAX} harness mounts`);
|
||||
const apart = ["/tmp", runDir, ...systemBinds(node).map((b) => b.dest ?? b.src)];
|
||||
const kept = [...RUNNER_FILES, adapter, node];
|
||||
const dests = [];
|
||||
const mounts = harnessMounts.map((m, i) => {
|
||||
const what = `harness mount ${i + 1}`;
|
||||
if (!plainObject(m) || Object.keys(m).some((k) => !MOUNT_KEYS.includes(k))) refuseRoute("mounts", `${what} is not { src, dest, why }`);
|
||||
// Each field is read once, so a getter cannot answer one value to a check and another to the bind.
|
||||
const { src, dest: named, why } = m;
|
||||
if (typeof why !== "string" || !why || why.length > 256) refuseRoute("mounts", `${what} is not { src, dest, why }`);
|
||||
if (named !== undefined && !cleanPath(named)) refuseRoute("mount-dest", `${what}'s dest is not a clean absolute path`);
|
||||
const real = source(src, "mount-src", what);
|
||||
const dest = named ?? src;
|
||||
if (apart.some((p) => overlaps(dest, p)) || kept.some((p) => within(p, dest)) || dests.some((d) => overlaps(dest, d))) refuseRoute("mount-dest", `${what}'s dest would cover a path the session needs`);
|
||||
dests.push(dest);
|
||||
return { src: real, dest, why };
|
||||
});
|
||||
return { adapter: { src: adapterReal, dest: adapter }, mounts };
|
||||
}
|
||||
|
||||
// #1873 triage-host: the binds for a session the Console hosts in no organization (Triage). It
|
||||
// gets the system part, the runner's files, the adapter and the harness module's mounts like any
|
||||
// session, and from the data root exactly two directories: its own `runDir` read-write, and
|
||||
// `healthDir` read-only over runDir/work/health, inside the workspace the read tools' gate allows. The run's bundle/, session.json and the relay socket the runner
|
||||
// calls through stay read-only inside it, and its tmp/ is /tmp. There is no workspace, broker,
|
||||
// launch or git socket, thread store or org root. Each source inside runDir must be what the host
|
||||
// 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 = [], 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]) {
|
||||
let st;
|
||||
try { st = lstatSync(p); } catch { throw new Error(`hosted binds: ${p === healthDir ? "the health directory" : relative(runDir, p) || "the run directory"} is missing`); }
|
||||
if (st.isSymbolicLink() || !st.isDirectory()) throw new Error(`hosted binds: ${p === healthDir ? "the health directory" : relative(runDir, p) || "the run directory"} is not a directory`);
|
||||
}
|
||||
for (const [p, ok] of [[join(runDir, "session.json"), (st) => st.isFile()], [relaySocket, (st) => st.isSocket()]]) {
|
||||
let st;
|
||||
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" });
|
||||
out.push({ kind: "ro", src: route.adapter.src, ...(route.adapter.src !== adapter ? { dest: adapter } : {}), why: "the harness adapter the runner starts each turn" });
|
||||
for (const m of route.mounts) out.push({ kind: "ro", src: m.src, ...(m.dest !== m.src ? { dest: m.dest } : {}), why: m.why });
|
||||
for (const f of readOnly) out.push({ kind: "ro", src: f, why: "a test fixture" });
|
||||
out.push(
|
||||
{ kind: "rw", src: runDir, why: "the session's own state: turns/, session/, work/, tools.sock, turn.done, home/, tmp/" },
|
||||
{ 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" },
|
||||
);
|
||||
return out;
|
||||
}
|
||||
|
||||
// The command line for a hosted session: as sandboxArgv, with no git identity, since a hosted
|
||||
// session belongs to no organization and holds no git verb.
|
||||
export function hostedArgv(list, runDir, command, { bwrap = BWRAP } = {}) {
|
||||
return [bwrap, ...NAMESPACES, ...argsOf(list), "--remount-ro", "/", "--setenv", "HOME", join(runDir, "home"), "--setenv", "TMPDIR", "/tmp", "--chdir", runDir, "--", ...KILL_WITH_PARENT, ...command];
|
||||
}
|
||||
|
||||
// Whether bwrap runs here at all: the namespaces and the system binds, then
|
||||
// setpriv and `true`. Returns null, or why not.
|
||||
export function sandboxProblem({ bwrap = BWRAP } = {}) {
|
||||
|
||||
@@ -0,0 +1,276 @@
|
||||
// #1873 triage-host: the binds of a session the Console hosts in no organization. From the data root
|
||||
// it gets its own run directory and the health directory, read-only, and nothing else; every source
|
||||
// inside the run directory must be what the host made, never a link.
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createServer } from "node:net";
|
||||
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";
|
||||
|
||||
function layout(t) {
|
||||
const root = mkdtempSync(join(process.env.TMPDIR, "hb-"));
|
||||
const dataRoot = join(root, "data");
|
||||
const runDir = join(dataRoot, "triage", "agent");
|
||||
const healthDir = join(dataRoot, "triage", "health");
|
||||
for (const d of [join(dataRoot, "orgs"), join(dataRoot, "triage", "host"), healthDir, join(runDir, "bundle"), join(runDir, "tmp"), join(runDir, "work", "health")]) mkdirSync(d, { recursive: true });
|
||||
writeFileSync(join(runDir, "session.json"), "{}");
|
||||
const install = join(root, "install");
|
||||
mkdirSync(install);
|
||||
const adapter = join(install, "adapter.sh");
|
||||
writeFileSync(adapter, "exit 0\n");
|
||||
return new Promise((resolve) => {
|
||||
const server = createServer().listen(join(runDir, "relay.sock"), () => {
|
||||
t.after(() => server.close());
|
||||
resolve({ root, dataRoot, runDir, healthDir, adapter, install, relaySocket: join(runDir, "relay.sock"), installRoots: [install], protectedPaths: [dataRoot] });
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
test("hosted binds hold the run directory and the health directory of the data root, and nothing else of it", async (t) => {
|
||||
const p = await layout(t);
|
||||
const list = hostedBinds({ runDir: p.runDir, healthDir: p.healthDir, adapter: p.adapter, relaySocket: p.relaySocket, installRoots: p.installRoots, protectedPaths: p.protectedPaths });
|
||||
const fromData = list.filter((b) => typeof b.src === "string" && b.src.startsWith(p.dataRoot + "/"));
|
||||
assert.deepEqual(fromData.map((b) => [b.kind, b.src, b.dest ?? null]), [
|
||||
["rw", p.runDir, null],
|
||||
["ro", p.healthDir, join(p.runDir, "work", "health")],
|
||||
["ro", join(p.runDir, "bundle"), null],
|
||||
["ro", join(p.runDir, "session.json"), null],
|
||||
["ro", p.relaySocket, null],
|
||||
["rw", join(p.runDir, "tmp"), "/tmp"],
|
||||
]);
|
||||
for (const f of RUNNER_FILES) assert.ok(list.some((b) => b.kind === "ro" && b.src === f), f);
|
||||
assert.ok(list.some((b) => b.kind === "ro" && b.src === p.adapter));
|
||||
assert.ok(!list.some((b) => b.src === p.root || b.src === p.dataRoot || (typeof b.src === "string" && b.src.startsWith(join(p.dataRoot, "orgs")))));
|
||||
assert.ok(!list.some((b) => b.src === process.env.HOME || b.dest === process.env.HOME));
|
||||
assert.ok(list.every((b) => typeof b.why === "string" && b.why.length > 0));
|
||||
});
|
||||
|
||||
test("hosted binds refuse a relay socket elsewhere, and any source in the run directory that the host did not make", 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 };
|
||||
assert.throws(() => hostedBinds({ ...base, relaySocket: join(p.root, "bus.sock") }), /relay socket must be the run directory's relay\.sock/);
|
||||
// A link in place of each directory the binds read: bwrap would follow it on the host.
|
||||
for (const d of ["bundle", "tmp", "work"]) {
|
||||
rmSync(join(p.runDir, d), { recursive: true });
|
||||
symlinkSync(join(p.dataRoot, "orgs"), join(p.runDir, d));
|
||||
assert.throws(() => hostedBinds(base), /is not a directory/, d);
|
||||
rmSync(join(p.runDir, d));
|
||||
mkdirSync(join(p.runDir, d === "work" ? join("work", "health") : d), { recursive: true });
|
||||
}
|
||||
rmSync(join(p.runDir, "work", "health"), { recursive: true });
|
||||
symlinkSync(join(p.dataRoot, "triage", "host"), join(p.runDir, "work", "health"));
|
||||
assert.throws(() => hostedBinds(base), /is not a directory/);
|
||||
rmSync(join(p.runDir, "work", "health"));
|
||||
mkdirSync(join(p.runDir, "work", "health"));
|
||||
rmSync(join(p.runDir, "session.json"));
|
||||
symlinkSync(join(p.dataRoot, "orgs"), join(p.runDir, "session.json"));
|
||||
assert.throws(() => hostedBinds(base), /session\.json is not what the host made/);
|
||||
rmSync(join(p.runDir, "session.json"));
|
||||
writeFileSync(join(p.runDir, "session.json"), "{}");
|
||||
assert.throws(() => hostedBinds({ ...base, healthDir: join(p.root, "absent") }), /the health directory is missing/);
|
||||
hostedBinds(base);
|
||||
});
|
||||
|
||||
test("the hosted command line sets no git identity and runs the command under the namespaces", async (t) => {
|
||||
const p = await layout(t);
|
||||
const argv = hostedArgv(hostedBinds({ runDir: p.runDir, healthDir: p.healthDir, adapter: p.adapter, relaySocket: p.relaySocket, installRoots: p.installRoots, protectedPaths: p.protectedPaths }), p.runDir, ["node", "runner.mjs", p.runDir], { bwrap: "/b/bwrap" });
|
||||
assert.equal(argv[0], "/b/bwrap");
|
||||
assert.deepEqual(argv.slice(1, 5), ["--unshare-user", "--unshare-pid", "--unshare-ipc", "--as-pid-1"]);
|
||||
assert.deepEqual(argv.slice(-3), ["node", "runner.mjs", p.runDir]);
|
||||
assert.ok(!argv.some((a) => a.startsWith("GIT_")));
|
||||
const home = argv.indexOf("HOME");
|
||||
assert.equal(argv[home + 1], join(p.runDir, "home"));
|
||||
assert.ok(argv.includes("--remount-ro"));
|
||||
});
|
||||
|
||||
// #1873 triage-fences: hostedBinds checks the adapter and the mounts again, so a caller that skips
|
||||
// the host's own check still binds nothing outside the install roots.
|
||||
test("hosted binds refuse without install roots or protected paths, and refuse an adapter or a mount source outside the roots or in a protected path", 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 refused = (opts, fence, label) => assert.throws(() => hostedBinds({ ...base, ...opts }), (e) => e.code === "hosted-binds-refused" && e.fence === fence, label);
|
||||
refused({ installRoots: undefined }, "roots", "no roots");
|
||||
refused({ installRoots: [] }, "roots", "empty roots");
|
||||
refused({ installRoots: ["/"] }, "roots", "root /");
|
||||
refused({ installRoots: [join(p.root, "absent")] }, "roots", "missing root");
|
||||
refused({ installRoots: [p.adapter] }, "roots", "a file as root");
|
||||
refused({ protectedPaths: undefined }, "protected", "no protected paths");
|
||||
refused({ protectedPaths: ["relative"] }, "protected", "relative protected path");
|
||||
writeFileSync(join(p.root, "out.sh"), "x");
|
||||
refused({ adapter: join(p.root, "out.sh") }, "adapter", "adapter outside");
|
||||
symlinkSync(join(p.root, "out.sh"), join(p.install, "link.sh"));
|
||||
refused({ adapter: join(p.install, "link.sh") }, "adapter", "adapter link out");
|
||||
refused({ adapter: p.install }, "adapter", "adapter is a directory");
|
||||
refused({ harnessMounts: [{ src: join(p.install, "link.sh"), why: "w" }] }, "mount-src", "mount link out");
|
||||
refused({ harnessMounts: [{ src: join(p.dataRoot, "orgs"), why: "w" }], installRoots: [p.root] }, "mount-src", "mount in the data root");
|
||||
refused({ harnessMounts: [{ src: p.root, why: "w" }], installRoots: [p.root] }, "mount-src", "mount around the data root");
|
||||
mkdirSync(join(p.install, "secrets"));
|
||||
writeFileSync(join(p.install, "secrets", "k"), "x");
|
||||
refused({ harnessMounts: [{ src: join(p.install, "secrets", "k"), why: "w" }] }, "mount-src", "mount in secrets");
|
||||
writeFileSync(join(p.install, "m"), "x");
|
||||
refused({ harnessMounts: Array.from({ length: HOSTED_MOUNTS_MAX + 1 }, (_, i) => ({ src: join(p.install, "m"), dest: `/opt/m${i}`, why: "w" })) }, "mounts", "too many");
|
||||
refused({ harnessMounts: [{ src: join(p.install, "m"), dest: "/tmp/m", why: "w" }] }, "mount-dest", "dest under /tmp");
|
||||
refused({ harnessMounts: [{ src: join(p.install, "m"), dest: p.runDir, why: "w" }] }, "mount-dest", "dest is the run dir");
|
||||
refused({ harnessMounts: [{ src: join(p.install, "m"), dest: join(p.runDir, "session.json"), why: "w" }] }, "mount-dest", "dest is session.json");
|
||||
refused({ harnessMounts: [{ src: join(p.install, "m"), dest: "/usr", why: "w" }] }, "mount-dest", "dest is a system bind");
|
||||
refused({ harnessMounts: [{ src: join(p.install, "m"), dest: dirname(RUNNER_FILES[0]), why: "w" }] }, "mount-dest", "dest holds a runner file");
|
||||
refused({ harnessMounts: [{ src: join(p.install, "m"), why: "w", kind: "rw" }] }, "mounts", "a kind of its own");
|
||||
});
|
||||
|
||||
test("hosted binds bind the real file of a link that stays inside the install root, at the path the route named", async (t) => {
|
||||
const p = await layout(t);
|
||||
writeFileSync(join(p.install, "real.mjs"), "x");
|
||||
symlinkSync(join(p.install, "real.mjs"), join(p.install, "named.mjs"));
|
||||
symlinkSync(p.adapter, join(p.install, "adapter-link.sh"));
|
||||
const list = hostedBinds({
|
||||
runDir: p.runDir, healthDir: p.healthDir, adapter: join(p.install, "adapter-link.sh"), relaySocket: p.relaySocket, installRoots: p.installRoots, protectedPaths: p.protectedPaths,
|
||||
harnessMounts: [{ src: join(p.install, "named.mjs"), why: "w" }, { src: join(p.install, "real.mjs"), dest: "/opt/real.mjs", why: "w2" }],
|
||||
});
|
||||
assert.ok(list.some((b) => b.kind === "ro" && b.src === p.adapter && b.dest === join(p.install, "adapter-link.sh")));
|
||||
assert.ok(list.some((b) => b.kind === "ro" && b.src === join(p.install, "real.mjs") && b.dest === join(p.install, "named.mjs") && b.why === "w"));
|
||||
assert.ok(list.some((b) => b.kind === "ro" && b.src === join(p.install, "real.mjs") && b.dest === "/opt/real.mjs" && b.why === "w2"));
|
||||
assert.ok(!list.some((b) => b.src === join(p.install, "named.mjs")));
|
||||
});
|
||||
|
||||
test("a protected path that holds the code root (HOME over a copy or a worktree) does not refuse a source inside the code root; everything else still holds", async (t) => {
|
||||
const p = await layout(t);
|
||||
// home/ holds the code root code/, which holds the install root.
|
||||
const home = join(p.root, "home");
|
||||
const code = join(home, "code");
|
||||
const inst = join(code, "inst");
|
||||
mkdirSync(join(inst, "secrets"), { recursive: true });
|
||||
for (const f of ["adapter.sh", "m", join("secrets", "k")]) writeFileSync(join(inst, f), "x");
|
||||
writeFileSync(join(home, "outside"), "x");
|
||||
mkdirSync(join(code, "data"));
|
||||
const base = { runDir: p.runDir, healthDir: p.healthDir, adapter: join(inst, "adapter.sh"), relaySocket: p.relaySocket, installRoots: [inst], protectedPaths: [p.dataRoot, home], codeRoot: code, codeHolder: home };
|
||||
const refused = (opts, fence, label) => assert.throws(() => hostedBinds({ ...base, ...opts }), (e) => e.code === "hosted-binds-refused" && e.fence === fence, label);
|
||||
const list = hostedBinds({ ...base, harnessMounts: [{ src: join(inst, "m"), dest: "/opt/m", why: "w" }] });
|
||||
assert.ok(list.some((b) => b.src === join(inst, "adapter.sh")) && list.some((b) => b.src === join(inst, "m") && b.dest === "/opt/m"));
|
||||
// Without the code root, HOME refuses the same adapter.
|
||||
refused({ codeRoot: null }, "adapter", "no code root");
|
||||
// A code root that is HOME itself sets nothing aside.
|
||||
refused({ codeRoot: home }, "adapter", "code root is HOME");
|
||||
// A source under HOME outside the code root, even inside an install root.
|
||||
refused({ installRoots: [home], adapter: join(home, "outside") }, "adapter", "outside the code root");
|
||||
// A link inside the code root to a file outside it.
|
||||
symlinkSync(join(home, "outside"), join(inst, "out"));
|
||||
refused({ installRoots: [inst, home], harnessMounts: [{ src: join(inst, "out"), why: "w" }] }, "mount-src", "link out of the code root");
|
||||
// A protected path inside the code root still refuses.
|
||||
writeFileSync(join(code, "data", "f"), "x");
|
||||
refused({ installRoots: [code], protectedPaths: [p.dataRoot, home, join(code, "data")], harnessMounts: [{ src: join(code, "data", "f"), why: "w" }] }, "mount-src", "protected path in the code root");
|
||||
// The secrets check still holds inside the code root.
|
||||
refused({ harnessMounts: [{ src: join(inst, "secrets", "k"), why: "w" }] }, "mount-src", "secrets in the code root");
|
||||
// The run directory and the fixed list are never set aside, and a code root around them is refused.
|
||||
refused({ codeRoot: p.root, installRoots: [p.root], harnessMounts: [{ src: join(p.dataRoot, "orgs"), why: "w" }] }, "protected", "code root holds the run dir");
|
||||
refused({ codeRoot: "/srv/mosaic/x" }, "protected", "code root in a fixed path");
|
||||
refused({ codeRoot: "relative" }, "protected", "relative code root");
|
||||
// Only the named holder sets the code aside: another protected path that holds the code still refuses.
|
||||
refused({ protectedPaths: [p.dataRoot, home, p.root] }, "adapter", "a second protected path holds the code");
|
||||
refused({ codeHolder: null }, "protected", "no holder");
|
||||
refused({ codeHolder: join(p.root, "elsewhere") }, "protected", "a holder that is not a protected path");
|
||||
// A run directory named through a link: a code root in its real path is refused before any check.
|
||||
const realRun = join(home, "run");
|
||||
mkdirSync(join(realRun, "code", "inst"), { recursive: true });
|
||||
writeFileSync(join(realRun, "code", "inst", "a.sh"), "x");
|
||||
symlinkSync(realRun, join(p.root, "runlink"));
|
||||
assert.throws(() => checkHostedRoute({ adapter: join(realRun, "code", "inst", "a.sh"), harnessMounts: [], installRoots: [join(realRun, "code", "inst")], protectedPaths: [p.dataRoot, home], codeRoot: join(realRun, "code"), codeHolder: home, runDir: join(p.root, "runlink") }), (e) => e.fence === "protected", "code root in the run dir's real path");
|
||||
});
|
||||
|
||||
test("a fixed path named through a link: a code root in its real path is refused before any guard, and a source there is refused by its guard", async (t) => {
|
||||
const p = await layout(t);
|
||||
// None of the shipped fixed paths is a link on a test host, so the test hook adds a stand-in.
|
||||
const home = join(p.root, "home");
|
||||
// The real path lies outside HOME and the data root, so only the stand-in's own guard can refuse
|
||||
// a source there.
|
||||
const real = join(p.root, "fixedreal");
|
||||
const inst = join(real, "code", "inst");
|
||||
mkdirSync(inst, { recursive: true });
|
||||
writeFileSync(join(inst, "a.sh"), "x");
|
||||
const link = join(p.root, "fixedlink");
|
||||
symlinkSync(real, link);
|
||||
const base = { adapter: join(inst, "a.sh"), harnessMounts: [], installRoots: [inst], protectedPaths: [p.dataRoot, home], codeHolder: home, runDir: p.runDir, alsoFixed: [link] };
|
||||
const refused = (opts, fence, message, label) => assert.throws(() => checkHostedRoute({ ...base, ...opts }), (e) => e.code === "hosted-binds-refused" && e.fence === fence && message.test(e.message), label);
|
||||
// The code root lies in the stand-in's real path, not under the name the list gives.
|
||||
refused({ codeRoot: join(real, "code") }, "protected", /never set aside/, "code root in a fixed path's real path");
|
||||
// With the code root in HOME, the source in the stand-in's real path is refused by the stand-in's
|
||||
// guard, which holds the same real spelling. No other guard holds that path.
|
||||
mkdirSync(join(home, "code"), { recursive: true });
|
||||
refused({ codeRoot: join(home, "code") }, "adapter", /protected path/, "source in a fixed path's real path");
|
||||
// The same source passes without the stand-in: only the stand-in refused it.
|
||||
assert.equal(checkHostedRoute({ ...base, alsoFixed: [], codeRoot: join(home, "code") }).adapter.src, join(inst, "a.sh"));
|
||||
// The hook adds fixed paths and never removes one, and takes only a list of clean absolute paths.
|
||||
refused({ alsoFixed: [], codeRoot: "/srv/mosaic/x" }, "protected", /never set aside/, "shipped fixed path with an empty hook");
|
||||
refused({ alsoFixed: ["relative"], codeRoot: join(real, "code") }, "protected", /clean absolute/, "relative stand-in");
|
||||
refused({ alsoFixed: "/x", codeRoot: join(real, "code") }, "protected", /clean absolute|list/, "a hook that is not a list");
|
||||
});
|
||||
|
||||
test("a list that answers one length to the check and another later is read once", async (t) => {
|
||||
const p = await layout(t);
|
||||
const items = [];
|
||||
for (let i = 0; i <= HOSTED_MOUNTS_MAX; i++) { writeFileSync(join(p.install, `m${i}`), "x"); items.push({ src: join(p.install, `m${i}`), dest: `/opt/m${i}`, why: "w" }); }
|
||||
let reads = 0;
|
||||
const mounts = new Proxy(items, { get: (o, k) => (k === "length" ? (reads++ === 0 ? 1 : o.length) : o[k]) });
|
||||
assert.throws(() => checkHostedRoute({ adapter: p.adapter, harnessMounts: mounts, installRoots: p.installRoots, protectedPaths: p.protectedPaths, runDir: p.runDir }), (e) => e.fence === "mounts");
|
||||
// Install roots: two short answers pass the count, then a long one would add roots the count never saw.
|
||||
const roots = [];
|
||||
for (let i = 0; i <= 16; i++) { roots.push(join(p.root, `r${i}`)); mkdirSync(roots[i]); }
|
||||
writeFileSync(join(roots[16], "a.sh"), "x");
|
||||
let rootReads = 0;
|
||||
const rootList = new Proxy(roots, { get: (o, k) => (k === "length" ? (rootReads++ < 2 ? 1 : o.length) : o[k]) });
|
||||
assert.throws(() => checkHostedRoute({ adapter: join(roots[16], "a.sh"), harnessMounts: [], installRoots: rootList, protectedPaths: p.protectedPaths, runDir: p.runDir }), (e) => e.fence === "adapter");
|
||||
// Protected paths: the holder check sees HOME, then a shorter answer would leave HOME unguarded.
|
||||
const home = join(p.root, "home");
|
||||
mkdirSync(join(home, "code"), { recursive: true });
|
||||
writeFileSync(join(home, "f"), "x");
|
||||
let guardReads = 0;
|
||||
const guards = new Proxy([p.dataRoot, home], { get: (o, k) => (k === "length" ? (guardReads++ === 0 ? 2 : 1) : o[k]) });
|
||||
assert.throws(() => checkHostedRoute({ adapter: p.adapter, harnessMounts: [{ src: join(home, "f"), dest: "/opt/f", why: "w" }], installRoots: [p.install, home], protectedPaths: guards, codeRoot: join(home, "code"), codeHolder: home, runDir: p.runDir }), (e) => e.fence === "protected" || e.fence === "mount-src");
|
||||
});
|
||||
|
||||
test("each mount field is read once, and no mount's dest lies in or around another's", 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 };
|
||||
writeFileSync(join(p.install, "m"), "x");
|
||||
writeFileSync(join(p.install, "n"), "x");
|
||||
let reads = 0;
|
||||
const list = hostedBinds({ ...base, harnessMounts: [{ src: join(p.install, "m"), why: "w", get dest() { return reads++ === 0 ? undefined : "/opt/../tmp"; } }] });
|
||||
assert.equal(reads, 1);
|
||||
assert.ok(list.some((b) => b.src === join(p.install, "m") && b.dest === undefined));
|
||||
assert.ok(!list.some((b) => b.dest === "/opt/../tmp"));
|
||||
for (const [a, b] of [["/opt/x", "/opt/x/f"], ["/opt/x/f", "/opt/x"], ["/opt/x", "/opt/x"]]) {
|
||||
assert.throws(() => hostedBinds({ ...base, harnessMounts: [{ src: join(p.install, "m"), dest: a, why: "w" }, { src: join(p.install, "n"), dest: b, why: "w" }] }), (e) => e.fence === "mount-dest", `${a} then ${b}`);
|
||||
}
|
||||
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");
|
||||
});
|
||||
@@ -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
|
||||
|
||||
+103
-6
@@ -19,6 +19,10 @@ import { loadSystem, socketPath, orgPaths, REPO, rolesDir } from '../../cli/src/
|
||||
import { busStatus as orgBusStatus, busRevive as orgBusRevive } from '../../cli/src/org-bus.mjs';
|
||||
import { listOrgs, resolveOrg } from '../../organization/src/org-registry.mjs';
|
||||
import { showOrg } from '../../organization/src/org-service.mjs';
|
||||
import * as orgService from '../../organization/src/org-service.mjs';
|
||||
import { orgUsesFixture } from '../../cli/src/dev-fixture.mjs';
|
||||
import { moveFixtureOrg, ownerModelsFrom, readRider } from '../../model-proxy/src/ride.mjs';
|
||||
import { ownedText } from '../../model-proxy/src/admin.mjs';
|
||||
import { configDir } from '../../organization/src/organization-config.mjs';
|
||||
import { loadRootAgents } from '../../organization/src/agent-levels.mjs';
|
||||
import { organizationWriter } from '../../organization/src/organization.mjs';
|
||||
@@ -34,10 +38,15 @@ import { isAbsolute, join } from 'node:path';
|
||||
import { actionTrail } from './action-trail.mjs';
|
||||
import { reviveBuses } from './bus-revive.mjs';
|
||||
import { createProxyAdmin, orgCatalogue } from './proxy-admin.mjs';
|
||||
import { createTriageHost } from './triage-host.mjs';
|
||||
import { createTriageRoute, triageTokenPattern } from './triage-route.mjs';
|
||||
import { loadProxy } from '../../model-proxy/src/settings.mjs';
|
||||
import { composeSnapshot, readSystemConfig, readOrgConfig } from './triage-snapshot.mjs';
|
||||
import { busesStopped } from '../../cli/src/proxy-admin-host.mjs';
|
||||
import { agentLevelsWriteRoutes } from './agent-levels-write.mjs';
|
||||
import { triageRouteRoutes } from './triage-route-http.mjs';
|
||||
import { RESTART_EXIT } from '../../cli/src/org-console.mjs';
|
||||
import { ID_PATTERN } from '../../organization/src/vocabulary.mjs';
|
||||
import { ID_PATTERN, systemOnly } from '../../organization/src/vocabulary.mjs';
|
||||
import { selectorScope } from '../../organization/src/selector.mjs';
|
||||
// The bus verifies the Console's exact same-tree script in /proc, whose argv preserves a
|
||||
// relative shell argument even though Node's process.argv resolves it. Re-exec the same
|
||||
@@ -202,8 +211,9 @@ try {
|
||||
// and the preview are reads over requestRead.
|
||||
templatesWrite: session.user === undefined && sendOn ? templatesCaller(consoleCall({ dataRoot: root, socket: paths.socket, business: id })) : null,
|
||||
// Starter packs SP6 (#1873): the owner's workflow read, the organization file and its revision as the
|
||||
// writer reads them (the read `mosaic org workflow set` takes its revision from), and the V2 template names.
|
||||
workflowsRead: session.user === undefined ? () => ({ ...organizationWriter({ dir: configDir(), rolesDir: rolesDir() }).read(id), templates: templateHashes(rolesDir()).map(t => t.template) }) : null,
|
||||
// writer reads them (the read `mosaic org workflow set` takes its revision from), and the V2 template names
|
||||
// less the system-only ones. packs-http's workflowsRead drops them again and is the tested gate.
|
||||
workflowsRead: session.user === undefined ? () => ({ ...organizationWriter({ dir: configDir(), rolesDir: rolesDir() }).read(id), templates: templateHashes(rolesDir()).map(t => t.template).filter(t => !systemOnly(t)) }) : null,
|
||||
reads: consoleReads({ root: REPO, queueRoot: options.checkout ?? REPO, system, configDir: configDir(), rolesDir: rolesDir(), business: id, orgRoot: root }),
|
||||
};
|
||||
},
|
||||
@@ -212,6 +222,31 @@ try {
|
||||
const agentLevels = organizations ? { loadRoot: () => loadRootAgents(configDir(), { rolesDir: rolesDir() }), rolesDir: rolesDir(), ctx: { configDir: configDir(), dataRoot: system.dataRoot, instance } } : null;
|
||||
// #1873: proxy setup for the instance owner (dev local owner, or the root owner under --sso).
|
||||
// Instance-wide: one pinned release, one proxy.json, one unit. Each organization uses it (shared).
|
||||
// #1873 ride-default: each active organization as Ride all reads it, from its file and change log
|
||||
// (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 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 });
|
||||
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 {}
|
||||
// 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) });
|
||||
}
|
||||
return out;
|
||||
};
|
||||
// The owner's models (ride.mjs ownerModelsFrom): the proxy is set up when proxy.json names a port.
|
||||
const ownerModels = system && (devOwner || ssoOn) ? async () => {
|
||||
let proxy = false;
|
||||
try { proxy = Number.isInteger(loadProxy(configDir(), system.dataRoot)?.port); } catch {}
|
||||
return ownerModelsFrom({ proxy, riders: proxy ? await rideRiders() : [] });
|
||||
} : null;
|
||||
const proxyAdmin = system && (devOwner || ssoOn) ? createProxyAdmin({
|
||||
dataRoot: system.dataRoot, configDir: configDir(), instance, unitDir: options.proxyUnitDir ?? null,
|
||||
buses: () => busesStopped({ dataRoot: system.dataRoot, configDir: configDir() }),
|
||||
@@ -226,21 +261,83 @@ try {
|
||||
}
|
||||
return out;
|
||||
},
|
||||
riders: rideRiders,
|
||||
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.
|
||||
move: (id, models, principal) => moveFixtureOrg({ service: orgService, ctx: { configDir: configDir(), dataRoot: system.dataRoot, instance, principal }, id, models }),
|
||||
}) : null;
|
||||
// #1873 triage-host: the instance owner's Triage session, hosted by the Console in no organization.
|
||||
// Nothing starts here; the owner's first message starts it.
|
||||
// #1873 triage-wiring: the route is resolved at each admit from the route store, the key file, each
|
||||
// active organization's model catalogue (its bus `routes` verb, as proxy setup reads it) and the
|
||||
// harness module (triage-route.mjs). The host's fences hold the proxy port and the key's shape.
|
||||
// The port is read from proxy.json at each admit, so a proxy set up after the Console started counts.
|
||||
const triageProxyPort = () => {
|
||||
try { const p = loadProxy(configDir(), system.dataRoot)?.port; return Number.isInteger(p) ? p : null; } catch { return null; }
|
||||
};
|
||||
const triageRoute = system && (devOwner || ssoOn) ? createTriageRoute({
|
||||
dataRoot: system.dataRoot, configDir: configDir(), proxyPort: triageProxyPort,
|
||||
catalogues: async () => {
|
||||
// A bus that does not answer gives a catalogue that is not ready, so it confirms nothing.
|
||||
const read = async call => { try { return (await call('routes'))?.catalogue ?? { status: 'unknown' }; } catch { return { status: 'bus-unavailable' }; } };
|
||||
if (!organizations) return ownerCall ? [await read(ownerCall)] : [];
|
||||
const active = (await listOrgs({ configDir: configDir(), dataRoot: system.dataRoot, instance })).filter(org => org.state === 'active');
|
||||
return Promise.all(active.map(org => {
|
||||
let paths; try { paths = orgPaths(org.root); } catch { return { status: 'bus-unavailable' }; }
|
||||
return read(busCaller({ dataRoot: org.root, socket: paths.socket, business: org.id, timeoutMs: 5000 }));
|
||||
}));
|
||||
},
|
||||
}) : null;
|
||||
const triage = triageRoute ? createTriageHost({
|
||||
dataRoot: system.dataRoot, route: triageRoute, proxyPort: triageProxyPort, tokenPattern: triageTokenPattern(configDir()),
|
||||
devOwner: Boolean(devOwner), log: line => console.log(line),
|
||||
readSnapshot: () => composeSnapshot({
|
||||
orgs: () => listOrgs({ configDir: configDir(), dataRoot: system.dataRoot, instance }),
|
||||
busStatus: id => orgBusStatus({ configDir: configDir(), dataRoot: system.dataRoot, instance }, id),
|
||||
processing: async id => {
|
||||
const org = (await listOrgs({ configDir: configDir(), dataRoot: system.dataRoot, instance })).find(o => o.id === id);
|
||||
if (!org) return null;
|
||||
return busCaller({ dataRoot: org.root, socket: orgPaths(org.root).socket, business: id, timeoutMs: 5000 })('processing.status');
|
||||
},
|
||||
orgConfig: id => readOrgConfig(id),
|
||||
proxy: () => proxyAdmin.status(),
|
||||
systemConfig: () => readSystemConfig(),
|
||||
}),
|
||||
}) : null;
|
||||
let server, testerServer = null;
|
||||
// F3: the restart route closes every listener and connection, then exits RESTART_EXIT for the
|
||||
// supervisor to relaunch. Without --supervised the route answers not-supervised.
|
||||
const restart = supervised ? () => {
|
||||
sso?.close(); testerServer?.close();
|
||||
// The Triage session gets SIGTERM now and SIGKILL before the 2 s exit below.
|
||||
const triageStopped = triage ? triage.stop({ graceMs: 1500 }) : Promise.resolve();
|
||||
setTimeout(() => process.exit(RESTART_EXIT), 2000).unref();
|
||||
server.close(() => process.exit(RESTART_EXIT));
|
||||
server.close(() => triageStopped.finally(() => process.exit(RESTART_EXIT)));
|
||||
server.closeAllConnections();
|
||||
} : null;
|
||||
const trail = system ? actionTrail(join(system.dataRoot, 'console')) : null;
|
||||
// #1873 A2: Settings > Agents writes for the root and project levels, beside the levels read.
|
||||
const agentLevelRoutes = organizations ? [agentLevelsWriteRoutes({ configDir: configDir(), rolesDir: rolesDir(), trail })] : [];
|
||||
// #1873 triage-settings: Settings > Triage reads and saves the route store for the instance owner.
|
||||
// The model is checked against the catalogue each active organization's bus answers with, as an
|
||||
// organization's route change is. Saving starts nothing: the Triage host above reads the store at its
|
||||
// next admit.
|
||||
const triageRoutes = system && (devOwner || ssoOn) ? [triageRouteRoutes({
|
||||
dataRoot: system.dataRoot, log: line => console.log(line),
|
||||
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)] : [];
|
||||
// Every active organization is asked at once, so a hung bus costs one timeout, not one each.
|
||||
const active = (await listOrgs({ configDir: configDir(), dataRoot: system.dataRoot, instance })).filter(org => org.state === 'active');
|
||||
return Promise.all(active.map(org => {
|
||||
let paths; try { paths = orgPaths(org.root); } catch { return { status: 'bus-unavailable' }; }
|
||||
return read(busCaller({ dataRoot: org.root, socket: paths.socket, business: org.id, timeoutMs: 5000 }));
|
||||
}));
|
||||
},
|
||||
})] : [];
|
||||
try {
|
||||
server = await startServer({ restart, trail, ...(lan ? { host: lan.bind, devAllowHosts: lan.allowHosts } : {}), board: options.board, port: options.port, bus, reads, preview: options.preview, sso, proof, send, settingsWrite, projectFiles, projectFile, libraryFile, libraryStatus, librarySync, libraryRevert, librarySave, projectSave, chat, nameWrite, assistantStop, instancesWrite, routesWrite, agentWrite, proxyCall, proxyAdmin, processing, decisionWrite, requestRead, requestWrite, userOptions, federation: system?.federation ?? null, organizations, agentLevels, routes: agentLevelRoutes, preselect: options.business ?? null });
|
||||
server = await startServer({ restart, trail, ...(lan ? { host: lan.bind, devAllowHosts: lan.allowHosts } : {}), board: options.board, port: options.port, bus, reads, preview: options.preview, sso, proof, send, settingsWrite, projectFiles, projectFile, libraryFile, libraryStatus, librarySync, libraryRevert, librarySave, projectSave, chat, nameWrite, assistantStop, instancesWrite, routesWrite, agentWrite, proxyCall, proxyAdmin, triage, processing, decisionWrite, requestRead, requestWrite, userOptions, federation: system?.federation ?? null, organizations, agentLevels, ownerModels, rideRemoved: proxyAdmin ? id => proxyAdmin.rideRemoved(id) : null, routes: [...agentLevelRoutes, ...triageRoutes], preselect: options.business ?? null });
|
||||
} catch (err) { sso?.close(); throw err; }
|
||||
if (testerSocket) {
|
||||
const call = busCaller({ dataRoot: system.dataRoot, socket: socketPath(system.dataRoot), business: options.business ?? null, cli: TESTER_CLI });
|
||||
@@ -277,7 +374,7 @@ try {
|
||||
// D2 (P0-3): the dev Console reattaches to the buses that still run and brings back the ones its
|
||||
// unit stop killed. It runs after the Console answers, so a slow bus never delays the page.
|
||||
if (organizations && trail) reviveBuses({ ctx: { configDir: configDir(), dataRoot: system.dataRoot, instance }, listOrgs, busStatus: orgBusStatus, busRevive: orgBusRevive, trail, log: line => console.log(line) }).catch(() => console.log('bus revive: stopped on an unexpected error'));
|
||||
for (const signal of ['SIGINT', 'SIGTERM']) process.once(signal, () => { sso?.close(); server.close(); testerServer?.close(); });
|
||||
for (const signal of ['SIGINT', 'SIGTERM']) process.once(signal, () => { sso?.close(); server.close(); testerServer?.close(); triage?.stop(); });
|
||||
} catch (err) {
|
||||
console.error(`refused: ${err.message}`);
|
||||
process.exitCode = 2;
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
// handler returns false for them.
|
||||
|
||||
import { open } from 'node:fs/promises';
|
||||
import { pickModel, usableModel } from '../../model-proxy/src/ride.mjs';
|
||||
import { lstatSync } from 'node:fs';
|
||||
import { checkProject, initProject, projectRepo, recordContext } from '../../project/src/index.mjs';
|
||||
import { refuseInsideAgent } from '../../cli/src/transport.mjs';
|
||||
@@ -285,33 +286,53 @@ function refusal(res, error, { step, id, unknownOk = false }) {
|
||||
// Default service wiring: the real C1/C2/C3 modules, loaded on first use.
|
||||
// A missing module (a branch without S1 yet) answers not-configured.
|
||||
async function load(spec) { try { return await import(spec); } catch (e) { if (e?.code === 'ERR_MODULE_NOT_FOUND') return null; throw e; } }
|
||||
export function defaultOrgAdmin(env = process.env) {
|
||||
export function defaultOrgAdmin(env = process.env, { ownerModels = null, rideRemoved = null } = {}) {
|
||||
let system = null;
|
||||
// The same context `mosaic org` builds (packages/cli/src/org.mjs): the named
|
||||
// instance from MOSAIC_CONFIG, its data root and its config directory.
|
||||
const context = async () => {
|
||||
if (!system) {
|
||||
const { loadSystem } = await import('../../cli/src/config.mjs');
|
||||
const { configDir } = await import('../../organization/src/organization-config.mjs');
|
||||
const { devOwnerInstance } = await import('../../cli/src/dev-local-owner.mjs');
|
||||
let instance; try { instance = devOwnerInstance(env); } catch { return null; }
|
||||
system = { configDir: configDir(env), dataRoot: loadSystem({ env, instance }).dataRoot, instance };
|
||||
}
|
||||
return system;
|
||||
};
|
||||
return createOrgAdmin({
|
||||
ownerModels, rideRemoved: rideRemoved ?? removalRecorder(context),
|
||||
service: () => load('../../organization/src/org-service.mjs'),
|
||||
registry: () => load('../../organization/src/org-registry.mjs'),
|
||||
bus: () => load('../../cli/src/org-bus.mjs'),
|
||||
// The same context `mosaic org` builds (packages/cli/src/org.mjs): the named
|
||||
// instance from MOSAIC_CONFIG, its data root and its config directory.
|
||||
context: async () => {
|
||||
if (!system) {
|
||||
const { loadSystem } = await import('../../cli/src/config.mjs');
|
||||
const { configDir } = await import('../../organization/src/organization-config.mjs');
|
||||
const { devOwnerInstance } = await import('../../cli/src/dev-local-owner.mjs');
|
||||
let instance; try { instance = devOwnerInstance(env); } catch { return null; }
|
||||
system = { configDir: configDir(env), dataRoot: loadSystem({ env, instance }).dataRoot, instance };
|
||||
}
|
||||
return system;
|
||||
},
|
||||
context,
|
||||
});
|
||||
}
|
||||
|
||||
// A Console without the proxy setup page still records a removal of cliproxy, in the instance's
|
||||
// proxy receipts (model-proxy admin.mjs appendReceipt), where Ride all reads it. `context()` gives
|
||||
// the instance's data root; with none, the record and the save are refused.
|
||||
export function removalRecorder(context) {
|
||||
return async id => {
|
||||
const ctx = await context(), proxy = await load('../../model-proxy/src/admin.mjs');
|
||||
if (!ctx?.dataRoot || !proxy) throw Object.assign(new Error('not-configured'), { code: 'not-configured' });
|
||||
return proxy.appendReceipt(ctx.dataRoot, { time: new Date().toISOString(), op: 'ride-removed', org: id });
|
||||
};
|
||||
}
|
||||
|
||||
// `service`, `registry` and `bus` are module objects or functions returning
|
||||
// them (or null when absent). `context()` gives { configDir, dataRoot, instance }.
|
||||
export function createOrgAdmin({ service, registry = null, bus = null, context }) {
|
||||
// `ownerModels()` (#1873 ride-default) answers {proxy, riding, models} from the owner's model
|
||||
// proxy, read through the riding buses; without it the cliproxy preset reads unavailable.
|
||||
// `rideRemoved(id)` records in the proxy's receipts that a save took cliproxy off the organization,
|
||||
// so Ride all leaves it off; without it such a save is refused.
|
||||
export function createOrgAdmin({ service, registry = null, bus = null, context, ownerModels = null, rideRemoved = null }) {
|
||||
const get = async x => (typeof x === 'function' ? x() : x);
|
||||
return { service: () => get(service), registry: () => get(registry), bus: () => get(bus), context: () => get(context) };
|
||||
const owner = async () => { if (typeof ownerModels !== 'function') return { proxy: false, riding: false, models: [] }; try { return await ownerModels(); } catch { return { proxy: false, riding: false, models: [] }; } };
|
||||
return { service: () => get(service), registry: () => get(registry), bus: () => get(bus), context: () => get(context), ownerModels: owner, rideRemoved };
|
||||
}
|
||||
const providersOf = doc => (Array.isArray(doc?.plugins?.['model-provider']) ? doc.plugins['model-provider'] : []);
|
||||
const ownerOf = admin => (typeof admin.ownerModels === 'function' ? admin.ownerModels() : { proxy: false, riding: false, models: [] });
|
||||
|
||||
// The handler. Returns true when it answered. `owner` is the signed-in owner
|
||||
// session (provider, iss, sub) or null; `send` says --send is on; `origin` is
|
||||
@@ -413,17 +434,26 @@ async function route(req, res, { path, search, org, admin, principal, write, log
|
||||
if (path === '/api/org-presets' && !write) {
|
||||
if (new URLSearchParams(search).size) return json(res, 422, { error: 'invalid', message: 'unexpected query parameter' });
|
||||
const service = await need(admin, 'service', res, 'presets'); if (!service) return;
|
||||
try { return json(res, 200, { presets: await service.orgPresets() }); }
|
||||
try { return json(res, 200, { presets: await service.orgPresets({ owner: await ownerOf(admin) }) }); }
|
||||
catch (e) { return refusal(res, e, { step: 'presets' }); }
|
||||
}
|
||||
if (path === '/api/org-template' && !write) {
|
||||
const params = new URLSearchParams(search);
|
||||
for (const [key] of params) if (!['id', 'preset', 'thinking'].includes(key) || params.getAll(key).length !== 1) return json(res, 422, { error: 'invalid', message: 'invalid template query' });
|
||||
for (const [key] of params) if (!['id', 'preset', 'thinking', 'model'].includes(key) || params.getAll(key).length !== 1) return json(res, 422, { error: 'invalid', message: 'invalid template query' });
|
||||
if (params.has('model') && params.get('preset') !== 'cliproxy') return json(res, 422, { error: 'invalid', message: 'model applies to the cliproxy preset only' });
|
||||
const id = params.has('id') ? params.get('id') : 'new-org';
|
||||
if (!ID.test(id)) return json(res, 422, { error: 'invalid', errors: [{ path: '$.id', field: 'id', message: 'invalid organization id' }] });
|
||||
const service = await need(admin, 'service', res, 'template'); if (!service) return;
|
||||
// #1873 ride-default: the cliproxy preset routes to a model the owner's catalogue lists. A named
|
||||
// model must be one of them; otherwise the Console picks one (claude first).
|
||||
let model;
|
||||
if (params.get('preset') === 'cliproxy') {
|
||||
const owner = await ownerOf(admin), models = Array.isArray(owner?.models) ? owner.models : [];
|
||||
model = params.has('model') ? models.find(m => usableModel(m) && m.key === params.get('model')) : (owner?.proxy === true && owner?.riding === true ? pickModel(models) : null);
|
||||
if (!model) return json(res, 422, { error: 'invalid', step: 'template', errors: [{ path: '$.model', field: 'model', message: params.has('model') ? 'the model proxy lists no such model' : 'the model proxy lists no model you can use' }] });
|
||||
}
|
||||
let doc;
|
||||
try { doc = await service.templateRender(id, params.has('preset') || params.has('thinking') ? { ...(params.has('preset') ? { preset: params.get('preset') } : {}), ...(params.has('thinking') ? { thinking: params.get('thinking') } : {}) } : { fixture: 'local' }); } catch (e) { return refusal(res, e, { step: 'template' }); }
|
||||
try { doc = await service.templateRender(id, params.has('preset') || params.has('thinking') ? { ...(params.has('preset') ? { preset: params.get('preset') } : {}), ...(params.has('thinking') ? { thinking: params.get('thinking') } : {}), ...(model ? { model } : {}) } : { fixture: 'local' }); } catch (e) { return refusal(res, e, { step: 'template' }); }
|
||||
const shown = forBrowser(doc);
|
||||
return json(res, 200, { id, doc: shown.doc, held: shown.held, verb: verbFor('create', id) });
|
||||
}
|
||||
@@ -509,6 +539,12 @@ async function route(req, res, { path, search, org, admin, principal, write, log
|
||||
const doc = toCanonical(id, body.doc, shown.doc);
|
||||
const checked = await service.validateOrg(doc);
|
||||
if (!checked.ok) return json(res, 422, { error: 'invalid', step: 'update', errors: withField(checked.errors ?? []) });
|
||||
// #1873 ride-default: a save that takes cliproxy off leaves a proxy receipt first, so Ride all
|
||||
// does not put it back. No record, no save.
|
||||
if (providersOf(shown.doc).includes('cliproxy') && !providersOf(doc).includes('cliproxy')) {
|
||||
if (typeof admin.rideRemoved !== 'function') return json(res, 503, { error: 'not-configured', step: 'update', message: 'this Console cannot record that the model proxy was taken off, so nothing was saved' });
|
||||
try { await admin.rideRemoved(id); } catch { return json(res, 500, { error: 'internal', step: 'update', message: 'the removal of the model proxy could not be recorded, so nothing was saved' }); }
|
||||
}
|
||||
let receipt;
|
||||
try { receipt = await service.updateOrg(ctx, id, doc, { ifRevision: body.ifRevision }); } catch (e) { return refusal(res, e, { step: 'update', id, unknownOk: true }); }
|
||||
return json(res, 200, { outcome: 'done', step: 'update', receipt: forReceipt(ctx, receipt), verb: verbFor('update', id, { revision: body.ifRevision }) });
|
||||
|
||||
@@ -24,6 +24,8 @@
|
||||
// console cap alone, and runs the merge, the digest and the held-credential check itself. `workflows`
|
||||
// (SP6) is the owner's read of the organization file as the writer reads it ({document, revision,
|
||||
// templates}), the same read `mosaic org workflow set` takes its revision from; no bus verb answers it.
|
||||
import { systemOnly } from '../../organization/src/vocabulary.mjs';
|
||||
|
||||
export const PACKS_ROUTE = /^\/api\/(?:packs(?:\/(preview|apply|hire))?|board|workflows(?:\/([^/]+))?)$/;
|
||||
// Which requests the pack route answers. GET and HEAD /api/board stay the Board read (the Console's own task
|
||||
// board through its upstream); every other method on /api/board is this route's (POST writes, the rest 405).
|
||||
@@ -171,7 +173,8 @@ async function workflowsRead(res, { workflows, live, session, writable }) {
|
||||
}
|
||||
const roles = {
|
||||
definitions: Object.keys(plain(doc.definitions) ? doc.definitions : {}).sort(),
|
||||
templates: current.templates.filter(n => typeof n === 'string').sort(),
|
||||
// #1873 triage-followups: a system-only template (organization vocabulary SYSTEM_ONLY) is never offered.
|
||||
templates: current.templates.filter(n => typeof n === 'string' && !systemOnly(n)).sort(),
|
||||
};
|
||||
return json(res, 200, { workflows: flowsOf(doc), projects, roles, revision: current.revision, writable });
|
||||
}
|
||||
@@ -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 }; }
|
||||
}
|
||||
|
||||
@@ -28,6 +33,9 @@ const ROUTES = Object.freeze({
|
||||
'/api/proxy/admin/unit/restart': ['POST', []],
|
||||
'/api/proxy/admin/fence/clear': ['POST', []],
|
||||
'/api/proxy/admin/init-lock/clear': ['POST', []],
|
||||
// #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']],
|
||||
});
|
||||
export const PROXY_ADMIN_PATHS = Object.freeze(Object.keys(ROUTES));
|
||||
const INIT_KEYS = ['generate', 'key', 'confirm', 'port', 'rotateGraceMinutes', 'firstOutputSeconds'];
|
||||
@@ -46,7 +54,8 @@ const STATUS = { 'human-required': 403, 'proxy-key-transport': 403, 'not-configu
|
||||
|
||||
// `owner` is true only for the instance owner's session (the dev local owner, or the root owner
|
||||
// signed in under --sso). `call` is the owner's transport to the selected bus, or null.
|
||||
export async function proxyAdminHTTP(req, res, { path, admin, call, named, tester, granted, owner, send, origin, user, log }) {
|
||||
// `principal` is that session's {provider, iss, sub}; the organization service checks it again.
|
||||
export async function proxyAdminHTTP(req, res, { path, admin, call, named, tester, granted, owner, send, origin, user, principal = null, log }) {
|
||||
const route = ROUTES[path];
|
||||
const done = (status, value) => {
|
||||
// Path, method and status only: never a body, key, evidence or exception text.
|
||||
@@ -105,6 +114,10 @@ export async function proxyAdminHTTP(req, res, { path, admin, call, named, teste
|
||||
if (!call) return refuse('not-configured');
|
||||
return done(200, await admin.fenceClear(call));
|
||||
case '/api/proxy/admin/init-lock/clear': return done(200, await admin.initLockClear());
|
||||
case '/api/proxy/admin/ride-all': return done(200, await admin.rideAll());
|
||||
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 }));
|
||||
}
|
||||
} catch (error) {
|
||||
const code = ['human-required', 'not-configured', 'invalid-request'].includes(error?.code) ? error.code : proxyCode(error, 'outcome-unknown');
|
||||
|
||||
@@ -51,6 +51,8 @@ export async function proxyHTTP(req, res, { path, call, named, tester, granted,
|
||||
try { return done(200, 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';
|
||||
return done(code === 'human-required' ? 403 : ['not-configured', 'proxy-down', 'outcome-unknown'].includes(code) ? 503 : 400, { error: code });
|
||||
// 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 });
|
||||
}
|
||||
}
|
||||
@@ -193,7 +193,7 @@
|
||||
catch { return { unknown: true }; }
|
||||
if (res.type === 'opaqueredirect') return { signedOut: true };
|
||||
try { result = await res.json(); } catch { return { unknown: true }; }
|
||||
return { ok: res.ok, result };
|
||||
return { ok: res.ok, status: res.status, result };
|
||||
}
|
||||
// The request's closing read, or a reason the chain stops.
|
||||
async function closing(request, org) {
|
||||
@@ -215,31 +215,39 @@
|
||||
return e ? (Array.isArray(e.running) ? e.running.length : 0) : 0;
|
||||
} catch { return null; }
|
||||
}
|
||||
async function restartAgent(name, thread) {
|
||||
const org = window.MosaicOrg?.id ?? null;
|
||||
const r = restarts[name] = { busy: true, text: `Terminating ${name}…` };
|
||||
const paint = () => paintRestart(name, org);
|
||||
const stop = text => { r.busy = false; r.text = text; paint(); };
|
||||
paint();
|
||||
const asked = await send(pin(org, '/api/agent/terminate'), { instance: name, reason: 'route change' });
|
||||
if (asked.signedOut) return stop('Sign in again, then retry.');
|
||||
if (asked.unknown || !asked.ok || !REQUEST_ID.test(asked.result?.request ?? '')) return stop(asked.ok || asked.unknown ? UNKNOWN : refused(asked.result));
|
||||
// SP19 (#1873): the agent drawer's Restart runs this same chain. `org` is the agent's own organization
|
||||
// (on #/agents the page has none), `reason` goes with the terminate, `text(step, detail)` may give the
|
||||
// words for a step (nothing keeps the route-change words below), and `onStatus({ step, busy, text })`
|
||||
// takes each line in place of the Routing receipt's button. The chain never regenerates.
|
||||
async function restartAgent(name, thread, { org = window.MosaicOrg?.id ?? null, reason = 'route change', text, onStatus } = {}) {
|
||||
const r = { busy: true, text: '' };
|
||||
if (!onStatus) restarts[name] = r;
|
||||
const paint = step => onStatus ? onStatus({ step, busy: r.busy, text: r.text }) : paintRestart(name, org);
|
||||
const words = (step, detail, own) => (typeof text === 'function' ? text(step, detail) : undefined) ?? own;
|
||||
const say = (step, detail, own) => { r.text = words(step, detail, own); paint(step); };
|
||||
const stop = (step, detail, own) => { r.busy = false; say(step, detail, own); };
|
||||
say('ending', {}, `Terminating ${name}…`);
|
||||
const asked = await send(pin(org, '/api/agent/terminate'), { instance: name, reason });
|
||||
if (asked.signedOut) return stop('signedOut', {}, 'Sign in again, then retry.');
|
||||
if (asked.unknown || !asked.ok || !REQUEST_ID.test(asked.result?.request ?? '')) return asked.ok || asked.unknown ? stop('endUnknown', {}, UNKNOWN) : stop('endRefused', { status: asked.status, result: asked.result }, refused(asked.result));
|
||||
const ended = await closing(asked.result.request, org);
|
||||
if (ended.stop) return stop(ended.stop);
|
||||
if (ended.done.reason) return stop(`${name}: ${ended.done.reason}. It was not restarted.`);
|
||||
r.text = `Terminated ${name}. Waiting for its session to end…`; paint();
|
||||
if (ended.stop) return stop('endOpen', { stop: ended.stop }, ended.stop);
|
||||
if (ended.done.reason) return stop('endClosed', { reason: ended.done.reason }, `${name}: ${ended.done.reason}. It was not restarted.`);
|
||||
say('ended', {}, `Terminated ${name}. Waiting for its session to end…`);
|
||||
let left = false;
|
||||
for (let i = 0; i < RESTART_WAIT && !left; i++) { await new Promise(res => setTimeout(res, POLL_MS)); left = (await runs(name, org)) === 0; }
|
||||
if (!left) return stop(`${name} was asked to end but its session is still running. Launch it from Agents once it has ended.`);
|
||||
if (thread) return stop(`Terminated ${name} at ${new Date().toLocaleTimeString()}. Its next message starts it on the new route.`);
|
||||
r.text = `Launching ${name}…`; paint();
|
||||
if (!left) return stop('stillRunning', {}, `${name} was asked to end but its session is still running. Launch it from Agents once it has ended.`);
|
||||
const time = new Date().toLocaleTimeString();
|
||||
if (thread) return stop('threadEnded', { time }, `Terminated ${name} at ${time}. Its next message starts it on the new route.`);
|
||||
say('launching', {}, `Launching ${name}…`);
|
||||
const launch = await send(pin(org, '/api/agent/launch'), { instance: name });
|
||||
if (launch.signedOut) return stop('Sign in again. It was terminated and not launched.');
|
||||
if (launch.unknown || !launch.ok || !REQUEST_ID.test(launch.result?.request ?? '')) return stop(launch.ok || launch.unknown ? `${UNKNOWN} It was terminated.` : `${refused(launch.result)} It was terminated and not launched.`);
|
||||
if (launch.signedOut) return stop('launchSignedOut', {}, 'Sign in again. It was terminated and not launched.');
|
||||
if (launch.unknown || !launch.ok || !REQUEST_ID.test(launch.result?.request ?? '')) return launch.ok || launch.unknown ? stop('launchUnknown', {}, `${UNKNOWN} It was terminated.`) : stop('launchRefused', { status: launch.status, result: launch.result }, `${refused(launch.result)} It was terminated and not launched.`);
|
||||
const started = await closing(launch.result.request, org);
|
||||
if (started.stop) return stop(started.stop);
|
||||
if (started.done.reason) return stop(`${name}: ${started.done.reason}. It was terminated and not launched.`);
|
||||
return stop(`Restarted ${name} on the new route${started.done.done?.run ? ` (run ${started.done.done.run})` : ''}.`);
|
||||
if (started.stop) return stop('launchOpen', { stop: started.stop }, started.stop);
|
||||
if (started.done.reason) return stop('launchClosed', { reason: started.done.reason }, `${name}: ${started.done.reason}. It was terminated and not launched.`);
|
||||
const run = started.done.done?.run ?? null;
|
||||
return stop('restarted', { run }, `Restarted ${name} on the new route${run ? ` (run ${run})` : ''}.`);
|
||||
}
|
||||
document.addEventListener('click', e => {
|
||||
const b = e.target.closest?.('[data-agent-restart]');
|
||||
@@ -247,7 +255,7 @@
|
||||
restartAgent(b.dataset.agentRestart, b.dataset.agentThread === 'yes');
|
||||
});
|
||||
|
||||
window.MosaicAgents = Object.freeze({ section, restartMarkup, isThread: definition => threadDefinitions.has(definition) });
|
||||
window.MosaicAgents = Object.freeze({ section, restartMarkup, restart: restartAgent, isThread: definition => threadDefinitions.has(definition) });
|
||||
})();
|
||||
|
||||
/* #1873 A2: Settings > Agents, the root and project agent levels. bus.js awaits section() for the
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -321,7 +321,7 @@
|
||||
// render. That read marks nothing. Row 150 (#1644): since row 142, `mosaic talk` prints only the
|
||||
// reply to its own message, so a reply to a Console send stays unread in `mosaic inbox` until
|
||||
// the human marks it read with `mosaic inbox --read`.
|
||||
let isTester = false, orgSetup = false;
|
||||
let isTester = false, orgSetup = false, triageOwner = false;
|
||||
// Both Docs and project direction use the same owner-only records setup action.
|
||||
window.MosaicRecords = {
|
||||
markup(project) {
|
||||
@@ -1306,6 +1306,50 @@
|
||||
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 the model service 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);});
|
||||
function routesSection(view,instanceInfo){
|
||||
const title='<h2 id="routes-title">Routing</h2>';
|
||||
readHarnesses();
|
||||
@@ -1334,11 +1378,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>`:'<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?`<button type="button" id="provider-switch" data-enabled="${proxyOn}">${proxyOn?'Stop using the model service':'Use your models'}</button><p role="status" id="provider-switch-status"></p>`:'';
|
||||
const 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 +1403,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 the model service: press Stop using the model service, 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;}
|
||||
@@ -1388,7 +1434,7 @@
|
||||
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 on=names.includes('cliproxy');if(String(on)!==button.dataset.enabled){button.dataset.enabled=String(on);button.textContent=on?'Stop using the model service':'Use your models';button.disabled=false;status.textContent='The switch now shows the current setting. Press it again to change it.';return;}
|
||||
const next=on?names.filter(n=>n!=='cliproxy'):[...names,'cliproxy'];
|
||||
const response=await fetch('/api/proxy/plugins/set',{method:'POST',headers:{'content-type':'application/json'},body:JSON.stringify({revision:settings.revision,plugins:{'model-provider':next}}),cache:'no-store',redirect:'manual'});
|
||||
if(epoch!==orgEpoch)return;if(!response.ok)throw Error('Provider setting was refused.');harnessSettings=null;routesView=null;last.delete('routes');render(true);
|
||||
@@ -1431,22 +1477,39 @@
|
||||
});
|
||||
});
|
||||
// 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'].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'],['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 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-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;
|
||||
const label=pages.find(([id])=>id===page)?.[1] || '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()));
|
||||
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 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 || {};
|
||||
@@ -1457,15 +1520,26 @@
|
||||
// #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.'};
|
||||
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();}});
|
||||
|
||||
@@ -1672,7 +1746,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'; };
|
||||
@@ -1811,7 +1907,7 @@
|
||||
// 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;
|
||||
@@ -1827,6 +1923,8 @@
|
||||
window.MosaicChat?.leave(chatPath);
|
||||
if (path === '/business') { location.replace('#/settings/business'); return; }
|
||||
if (path === '/settings/display') { location.replace('#/settings/appearance'); return; }
|
||||
// #1873 settings slice 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`.
|
||||
@@ -1909,14 +2007,15 @@
|
||||
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; }
|
||||
if (navigated || (shown !== route && !samePage)) { unmountEditor(); $('bus-view').innerHTML = loadingView(SOURCES[path.split('/')[1]]); }
|
||||
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/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; }
|
||||
}
|
||||
@@ -1925,7 +2024,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;
|
||||
@@ -1941,7 +2040,8 @@
|
||||
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');
|
||||
@@ -2113,6 +2213,8 @@
|
||||
canMessage = !isTester && c?.send === true;
|
||||
chatSend = !isTester && c?.chatSend === true;
|
||||
orgSetup = !isTester && c?.orgSetup === true;
|
||||
// #1873 triage-settings: /api/config says proxyAdmin to the instance owner only, the same owner /api/triage/route serves.
|
||||
triageOwner = !isTester && c?.proxyAdmin === true;
|
||||
window.MosaicOrgBusStopped = !!window.MosaicOrg && c?.devOwner === true && orgSetup && c?.chatSend === false;
|
||||
// The header menu offers "New organization" to the owner of a Console with organizations; nobody else can create one.
|
||||
if (orgSetup && !$('org-new-link')) { const a = document.createElement('a'); a.id = 'org-new-link'; a.href = NEW_ORG; a.setAttribute('role', 'menuitem'); a.textContent = 'New organization'; document.querySelector('#avatar-menu > a[href="#/settings"]')?.after(a); }
|
||||
@@ -2159,6 +2261,7 @@
|
||||
instanceView = null; instanceDrafts = { add: null, change: null }; instanceReceipt = ''; instanceReceiptOp = null; instanceReceipts = Object.create(null);
|
||||
routesView = null; routesDraft = null; routesReceipt = ''; routesEditVersion++;
|
||||
harnessSettings = null; harnessDraft = null; harnessReceipt = '';
|
||||
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(() => {});
|
||||
@@ -2246,5 +2349,9 @@
|
||||
window.addEventListener('hashchange', e => render(!agentOnly(e.oldURL, e.newURL)));
|
||||
$('refresh').addEventListener('click', () => render(false));
|
||||
$('pause').addEventListener('click', () => setTimeout(schedule)); // after app.js flips aria-pressed
|
||||
render(false).then(() => { if (!$('bus-view').hidden) { const h1 = $('bus-view').querySelector('h1'); if (h1 && location.hash.startsWith('#/')) h1.focus(); } });
|
||||
// #1873 triage-followups: the first render ends by focusing the page's heading. A drawer or dialog the
|
||||
// reader opened before then keeps its focus: the Triage drawer, an agent drawer, the narrow-screen
|
||||
// menu or any open dialog.
|
||||
const holdsFocus = () => { const a = document.activeElement; return !!a && (!!a.closest('.ws-drawer:not([hidden]), [data-ws-drawer], dialog[open], [role="dialog"], [role="alertdialog"]') || (drawerOpen && nav.contains(a))); };
|
||||
render(false).then(() => { if (!$('bus-view').hidden && !holdsFocus()) { const h1 = $('bus-view').querySelector('h1'); if (h1 && location.hash.startsWith('#/')) h1.focus(); } });
|
||||
})();
|
||||
@@ -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; } });
|
||||
})();
|
||||
+1128
-181
File diff suppressed because it is too large.
Load diff
@@ -7,7 +7,7 @@
|
||||
<link rel="stylesheet" href="/processing.css"><script src="/processing.js" defer></script>
|
||||
<link rel="stylesheet" href="/org-create.css"><script src="/org-create.js" defer></script>
|
||||
<link rel="stylesheet" href="/onboarding.css"><script src="/onboarding.js" defer></script><link rel="stylesheet" href="/assistant.css">
|
||||
<script src="/workspace-data.js" defer></script><script src="/workspace.js" defer></script><script src="/tasks-board.js" defer></script><script src="/console-restart.js" defer></script><script src="/display.js" defer></script><script src="/inbox.js" defer></script><script src="/files.js" defer></script><script src="/project-remote.js" defer></script><link rel="stylesheet" href="/autonomy.css"><script src="/autonomy.js" defer></script><link rel="stylesheet" href="/packs.css"><script src="/packs.js" defer></script><link rel="stylesheet" href="/workflows.css"><script src="/workflows.js" defer></script><script src="/federation.js" defer></script><script src="/brand.js" defer></script><script src="/app.js" defer></script><script src="/markdown-preview.js" defer></script><script src="/projects.js" defer></script><script src="/chat.js" defer></script><script src="/project-archive.js" defer></script><script src="/agents.js" defer></script><script src="/assistant.js" defer></script><script src="/bus.js" defer></script><script src="/requests.js" defer></script></head>
|
||||
<script src="/workspace-data.js" defer></script><script src="/workspace.js" defer></script><script src="/tasks-board.js" defer></script><script src="/console-restart.js" defer></script><script src="/triage-settings.js" defer></script><script src="/display.js" defer></script><script src="/inbox.js" defer></script><script src="/files.js" defer></script><script src="/project-remote.js" defer></script><link rel="stylesheet" href="/autonomy.css"><script src="/autonomy.js" defer></script><link rel="stylesheet" href="/packs.css"><script src="/packs.js" defer></script><link rel="stylesheet" href="/workflows.css"><script src="/workflows.js" defer></script><script src="/federation.js" defer></script><script src="/brand.js" defer></script><script src="/app.js" defer></script><script src="/markdown-preview.js" defer></script><script src="/projects.js" defer></script><script src="/chat.js" defer></script><script src="/project-archive.js" defer></script><script src="/agents.js" defer></script><script src="/assistant.js" defer></script><script src="/bus.js" defer></script><script src="/requests.js" defer></script></head>
|
||||
<body>
|
||||
<div hidden aria-hidden="true"><select id="palette"></select><select id="mode"><option value="system">system</option><option value="light">light</option><option value="dim">dim</option><option value="dark">dark</option></select></div>
|
||||
<a class="skip" href="#main">Skip to content</a>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
[data-packs],[data-workflows]{margin-top:16px}
|
||||
[data-packs]{margin-top:16px}
|
||||
.pack-list,.pack-hires{list-style:none;margin:8px 0;padding:0}
|
||||
.pack-row,.pack-hire{border-top:1px solid var(--border);padding:12px 0;overflow-wrap:anywhere}
|
||||
.pack-preview{border:1px solid var(--border);border-radius:var(--r);padding:12px;margin:12px 0;overflow-wrap:anywhere}
|
||||
|
||||
@@ -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('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}
|
||||
@@ -109,6 +120,22 @@ table.s1-table td code{font-size:12.5px;white-space:nowrap}
|
||||
.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}
|
||||
/* #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 +291,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 +312,45 @@ 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}
|
||||
.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}
|
||||
@@ -329,7 +398,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 +414,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%; }
|
||||
@@ -631,6 +700,14 @@ html[data-organizations] .ws-orgcard .at{grid-column:3;grid-row:1;font:500 11.5p
|
||||
html[data-organizations] .ws-orgcard .why{grid-column:1/-1;font-size:12px;color:var(--muted)}
|
||||
html[data-organizations] .ws-orgcard.off a{border-style:dashed}
|
||||
html[data-organizations] .ws-home-split{margin-top:4px}
|
||||
/* SP18: the Dashboard's columns line up with its tile row. Three equal tracks with the tiles' gap, the left
|
||||
column spanning two, end on a tile edge whether the row holds six tiles (after the fourth) or three
|
||||
(after the second). Below 760 px the split is one column. */
|
||||
html[data-organizations] .ws-home>.ws-tiles{gap:var(--gap)}
|
||||
@media (min-width:760px){
|
||||
html[data-organizations] .ws-home-split{grid-template-columns:repeat(3,minmax(0,1fr));gap:var(--gap)}
|
||||
html[data-organizations] .ws-home-split>:first-child{grid-column:span 2}
|
||||
}
|
||||
html[data-organizations] .ws-trail{list-style:none;margin:0;padding:0;border:1px solid var(--line);border-radius:8px;background:var(--surface)}
|
||||
html[data-organizations] .ws-trail li{display:grid;grid-template-columns:48px 20px minmax(0,1fr);gap:10px;padding:7px 12px;border-bottom:1px solid color-mix(in srgb,var(--line) 60%,transparent);font-size:13px;align-items:center}
|
||||
html[data-organizations] .ws-trail li:last-child{border-bottom:0}
|
||||
@@ -770,6 +847,11 @@ html[data-organizations] .ws-drawer-acts .note{flex-basis:100%}
|
||||
@media (min-width:1200px){html[data-organizations] body:has(.ws-drawer) .content{padding-right:calc(420px + 22px)}}
|
||||
@media (max-width:700px){
|
||||
html[data-organizations] .ws-drawer{top:0;left:0;right:0;width:auto;border-left:0;box-shadow:none}
|
||||
/* SP18: the sheet covers the screen, so the page under it is held to one screen and does not scroll; it
|
||||
kept its own height before, and the sheet showed over a long blank area. workspace.js puts the page's
|
||||
place back on Close. */
|
||||
html[data-organizations]:has(#bus-view .ws-drawer){overflow:hidden}
|
||||
html[data-organizations]:has(#bus-view .ws-drawer) body{height:100dvh;overflow:hidden}
|
||||
}
|
||||
@media (max-width:1100px){
|
||||
html[data-organizations] .ws-tiles.six{grid-template-columns:repeat(3,minmax(0,1fr))}
|
||||
@@ -810,3 +892,55 @@ html[data-organizations] .ws-drawer-acts .note{flex-basis:100%}
|
||||
html[data-organizations] .ws-av.mob{display:inline-grid;margin-right:6px;vertical-align:-3px}
|
||||
html[data-organizations] .ws-tbl .t2.mono.hide-m{display:none}
|
||||
}
|
||||
/* #1873 triage-drawer: the owner's Triage drawer, in the agent drawer family. It sits above an open
|
||||
agent drawer, keeps its own header and message box in view, and scrolls the conversation alone. */
|
||||
#triage-drawer{position:fixed;top:var(--bar-h,52px);right:0;bottom:0;width:min(440px,100vw);z-index:41;display:flex;flex-direction:column;overflow:hidden;background:var(--surface);color:var(--text);border-left:1px solid var(--line);box-shadow:-8px 0 24px color-mix(in srgb,#000 14%,transparent)}
|
||||
#triage-drawer[hidden]{display:none}
|
||||
#triage-drawer .hd{display:flex;align-items:center;gap:8px;padding:12px 14px 12px 17px;border-bottom:1px solid var(--line);flex:none;box-shadow:inset 4px 0 0 var(--action)}
|
||||
#triage-drawer h2{margin:0;font-size:18px;font-weight:800;color:var(--strong)}
|
||||
#triage-drawer h2:focus{outline:none}
|
||||
#triage-drawer .x{margin-left:auto;flex:none;width:32px;height:32px;border:1px solid var(--line);border-radius:6px;background:var(--canvas);color:var(--strong);font-size:20px;line-height:1;cursor:pointer}
|
||||
#triage-drawer .triage-state{margin:0;padding:10px 14px;flex:none;font-size:14px;font-weight:600;color:var(--strong);border-bottom:1px solid var(--line);overflow-wrap:anywhere}
|
||||
#triage-drawer .triage-usage{margin:0;padding:6px 14px;flex:none;font-size:13px;color:var(--muted);border-bottom:1px solid var(--line);overflow-wrap:anywhere}
|
||||
#triage-drawer .triage-state[data-kind=off],#triage-drawer .triage-state[data-kind=cap]{color:var(--warning);background:color-mix(in srgb,var(--warning) 9%,var(--surface))}
|
||||
#triage-drawer .triage-state[data-kind=running]::before{content:"";display:inline-block;width:8px;height:8px;margin-right:7px;border-radius:50%;background:var(--success);vertical-align:1px}
|
||||
#triage-drawer .triage-health-sec{flex:none;padding:8px 14px;border-bottom:1px solid var(--line)}
|
||||
#triage-drawer .triage-health-sec summary{cursor:pointer;font:700 11px/1.6 var(--font);letter-spacing:.06em;text-transform:uppercase;color:var(--muted)}
|
||||
#triage-drawer .triage-health{list-style:none;margin:6px 0 0;padding:0;display:grid;gap:3px;max-height:24vh;overflow:auto;font-size:12.5px;color:var(--strong)}
|
||||
#triage-drawer .triage-health li{overflow-wrap:anywhere;padding-left:12px;position:relative}
|
||||
#triage-drawer .triage-health li::before{content:"";position:absolute;left:0;top:.5em;width:6px;height:6px;border-radius:50%;background:var(--line)}
|
||||
#triage-drawer .triage-health li.not-read{color:var(--muted)}
|
||||
#triage-drawer .triage-convo-title{margin:0;padding:10px 14px 4px;flex:none;font:700 11px/1.3 var(--font);letter-spacing:.06em;text-transform:uppercase;color:var(--muted)}
|
||||
#triage-drawer .triage-transcript{list-style:none;margin:0;padding:4px 14px 12px;flex:1 1 auto;min-height:120px;overflow:auto;overscroll-behavior:contain;display:flex;flex-direction:column;gap:8px}
|
||||
#triage-drawer .triage-transcript:focus-visible{outline:2px solid var(--action);outline-offset:-2px}
|
||||
#triage-drawer .triage-msg{border:1px solid var(--line);border-radius:8px;padding:8px 10px;background:var(--canvas);font-size:13.5px;overflow-wrap:anywhere}
|
||||
#triage-drawer .triage-msg.from-owner{background:color-mix(in srgb,var(--action) 8%,var(--surface));border-color:color-mix(in srgb,var(--action) 30%,var(--line))}
|
||||
#triage-drawer .triage-msg.from-host,#triage-drawer .triage-msg.from-other{background:transparent;border-style:dashed;color:var(--muted)}
|
||||
#triage-drawer .triage-msg-head{display:flex;gap:8px;align-items:baseline;margin-bottom:3px}
|
||||
#triage-drawer .triage-msg-head strong{color:var(--strong);font-size:13px}
|
||||
#triage-drawer .triage-when{font-size:11.5px;color:var(--muted)}
|
||||
#triage-drawer .triage-reply{margin:0 0 4px;padding-left:8px;border-left:3px solid var(--line);font-size:12.5px;color:var(--muted)}
|
||||
#triage-drawer .triage-text-body{margin:0;white-space:pre-wrap;color:var(--strong)}
|
||||
#triage-drawer .from-host .triage-text-body,#triage-drawer .from-other .triage-text-body{color:inherit}
|
||||
#triage-drawer .triage-cards{list-style:none;margin:8px 0 0;padding:0;display:grid;gap:6px}
|
||||
#triage-drawer .triage-card{display:grid;gap:4px;border:1px solid var(--line);border-radius:6px;padding:6px 8px;background:var(--surface)}
|
||||
#triage-drawer .triage-card-meta{font-size:12px;color:var(--muted)}
|
||||
#triage-drawer .triage-card-cmd{display:block;font:12px/1.5 var(--mono);color:var(--strong);white-space:pre-wrap;overflow-wrap:anywhere;user-select:all}
|
||||
#triage-drawer .triage-dropped{margin:6px 0 0;font-size:12px;color:var(--warning)}
|
||||
#triage-drawer .triage-empty{color:var(--muted);font-size:13px}
|
||||
#triage-drawer .triage-compose{flex:none;display:grid;gap:6px;padding:10px 14px 14px;border-top:1px solid var(--line);background:var(--surface)}
|
||||
#triage-drawer .triage-label{font-size:12px;font-weight:700;color:var(--muted)}
|
||||
#triage-drawer .triage-text{width:100%;box-sizing:border-box;min-height:64px;max-height:30vh;resize:vertical;padding:8px 10px;border:1px solid var(--line);border-radius:6px;background:var(--canvas);color:var(--strong);font:14px/1.45 var(--font)}
|
||||
#triage-drawer .triage-text:disabled{opacity:.6;cursor:not-allowed}
|
||||
#triage-drawer .triage-send-row{display:flex;align-items:center;gap:10px}
|
||||
#triage-drawer .triage-send{display:inline-flex;align-items:center;min-height:36px;padding:4px 16px;border:1px solid var(--action);border-radius:var(--r,6px);background:var(--action);color:var(--canvas);font:700 13px/1.4 var(--font);cursor:pointer}
|
||||
#triage-drawer .triage-send:disabled{background:var(--raised);border-color:var(--line);color:var(--muted);cursor:not-allowed}
|
||||
#triage-drawer .triage-hint{font-size:12px;color:var(--muted)}
|
||||
#triage-drawer .note{margin:0;font-size:12.5px;color:var(--warning)}
|
||||
#triage-drawer .note:empty,#triage-drawer .triage-send-status:empty{display:none}
|
||||
#triage-drawer .triage-send-status{margin:0;font-size:13px;color:var(--strong)}
|
||||
#triage-drawer .triage-send-status[data-kind=refused]{color:var(--danger)}
|
||||
#triage-drawer .triage-send-status[data-kind=unknown]{color:var(--warning)}
|
||||
#triage-drawer .triage-send-status[data-kind=ok]{color:var(--success)}
|
||||
@media (min-width:1200px){body:has(#triage-drawer:not([hidden])) .content{padding-right:calc(440px + 22px)}}
|
||||
@media (max-width:700px){#triage-drawer{top:0;left:0;width:auto;border-left:0;box-shadow:none;z-index:60}html:has(#triage-drawer:not([hidden])),body:has(#triage-drawer:not([hidden])){overflow:hidden}#triage-drawer .triage-health{max-height:18vh}}
|
||||
@@ -0,0 +1,134 @@
|
||||
/* #1873 triage-settings: Settings > Triage, the instance owner's Triage model route. It reads and
|
||||
writes /api/triage/route (triage-route-http.mjs); the server checks the owner, the Origin and Fetch
|
||||
Metadata, the catalogue and the time zone, and the store keeps the revision and the receipt. This
|
||||
file only asks and reports. bus.js offers the page only when /api/config says proxyAdmin, which is
|
||||
the same owner the route serves. The fields live here, so a repaint keeps what the owner typed. */
|
||||
(() => {
|
||||
'use strict';
|
||||
const $ = id => document.getElementById(id);
|
||||
const esc = s => String(s).replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]);
|
||||
const PATH = '/api/triage/route';
|
||||
// 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
|
||||
// change saved elsewhere since then answers as stale instead of being written over.
|
||||
let data = null, readLine = '', draft = null, base = 0, busy = false, line = '', conflict = false;
|
||||
|
||||
const call = init => window.fetch(PATH, { cache: 'no-store', redirect: 'manual', ...init });
|
||||
// The stored route's fields, or empty ones: there is no default model, harness, cap or zone.
|
||||
const fromRoute = r => ({ model: r?.model ?? '', harness: r?.harness ?? '', cap: r ? String(r.cap) : '', timeZone: r?.timeZone ?? '' });
|
||||
|
||||
async function read() {
|
||||
readLine = '';
|
||||
let r, a = null;
|
||||
try { r = await call({ method: 'GET' }); } catch { data = null; readLine = 'The Triage route could not be read. Reload the page to try again.'; return; }
|
||||
try { a = await r.json(); } catch { /* refused below */ }
|
||||
if (r.type === 'opaqueredirect') { data = null; readLine = 'Sign in again, then reload.'; return; }
|
||||
if (!r.ok || !a || typeof a !== 'object') { data = null; readLine = typeof a?.message === 'string' ? a.message : 'The Triage route could not be read.'; return; }
|
||||
// An untouched page follows the store; typed fields stay, with the revision they were read at.
|
||||
const untouched = draft === null || JSON.stringify(draft) === JSON.stringify(fromRoute(data?.route ?? null));
|
||||
data = a;
|
||||
if (untouched) { draft = fromRoute(a.route); base = Number.isSafeInteger(a.revision) ? a.revision : 0; }
|
||||
}
|
||||
|
||||
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" 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() {
|
||||
const d = draft ?? fromRoute(null);
|
||||
const send = data?.send === true;
|
||||
const off = busy || !send ? ' disabled' : '';
|
||||
const list = models();
|
||||
const offered = list.some(m => m.key === d.model);
|
||||
const modelOptions = `<option value=""${d.model === '' ? ' selected' : ''}>Choose a model</option>`
|
||||
+ (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 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" 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>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>`
|
||||
+ `<div class="settings-actions"><button type="submit" id="triage-route-save"${off}>${busy ? 'Saving…' : 'Save'}</button>${conflictButton} <span role="status" id="triage-route-line" tabindex="-1">${esc(line)}</span></div>`
|
||||
+ `</form>${send ? '' : '<p class="small muted" id="triage-route-readonly">This Console is read-only, so the route cannot be saved here.</p>'}`;
|
||||
}
|
||||
|
||||
function markup() {
|
||||
const body = data === null
|
||||
? `<p class="small warn">${esc(readLine || 'The Triage route could not be read.')}</p><p role="status" id="triage-route-line" tabindex="-1">${esc(line)}</p>`
|
||||
: `${stored()}${form()}<p class="small muted" id="triage-route-note">${esc(data.note ?? 'Saving a route does not start Triage. Triage also needs its key.')}</p>`;
|
||||
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(); };
|
||||
|
||||
// Called by bus.js when the page draws: a fresh read each time, the typed fields kept.
|
||||
async function section() { await read(); return markup(); }
|
||||
|
||||
async function save() {
|
||||
if (busy || data?.send !== true) return;
|
||||
const d = draft ?? fromRoute(null);
|
||||
const cap = /^\d+$/.test(d.cap.trim()) ? Number(d.cap.trim()) : d.cap.trim() === '' ? null : Number.NaN;
|
||||
const body = { model: d.model, harness: d.harness, cap: Number.isFinite(cap) ? cap : d.cap, timeZone: d.timeZone.trim(), revision: base };
|
||||
busy = true; conflict = false; line = 'Saving…'; repaint();
|
||||
let r, a = null;
|
||||
try { r = await call({ method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) }); }
|
||||
catch { busy = false; line = 'Outcome unknown. Reload the route to see whether it was saved.'; conflict = true; repaint(); return; }
|
||||
try { a = await r.json(); } catch { /* refused below */ }
|
||||
busy = false;
|
||||
if (r.ok && a?.route) {
|
||||
data = { ...data, route: a.route, revision: a.revision, routeMessage: undefined };
|
||||
draft = fromRoute(a.route); base = a.revision;
|
||||
line = `Saved ${savedAt(a.route.updatedAt)}.`;
|
||||
repaint();
|
||||
$('triage-route-line')?.focus?.();
|
||||
return;
|
||||
}
|
||||
if (r.type === 'opaqueredirect') line = 'Sign in again, then retry.';
|
||||
else line = typeof a?.message === 'string' ? a.message : 'The route was refused. Nothing was saved.';
|
||||
if (r.status === 409 && a?.error === 'triage-route-stale') conflict = true;
|
||||
repaint();
|
||||
$(conflict ? 'triage-route-reload' : 'triage-route-save')?.focus();
|
||||
}
|
||||
|
||||
async function reload() {
|
||||
if (busy) return;
|
||||
draft = null; conflict = false; line = '';
|
||||
await read();
|
||||
line = data ? 'Route read again. Your fields now show the stored route.' : '';
|
||||
repaint();
|
||||
}
|
||||
|
||||
const field = { 'triage-route-model': 'model', 'triage-route-harness': 'harness', 'triage-route-cap': 'cap', 'triage-route-zone': 'timeZone' };
|
||||
document.addEventListener('input', e => {
|
||||
const k = field[e.target?.id]; if (!k || !$('triage-route')) return;
|
||||
draft = { ...(draft ?? fromRoute(null)), [k]: e.target.value };
|
||||
});
|
||||
document.addEventListener('change', e => {
|
||||
if (e.target?.id !== 'triage-route-model') return;
|
||||
// A new model narrows the harness list; a harness the model does not take is cleared.
|
||||
const d = draft = { ...(draft ?? fromRoute(null)), model: e.target.value };
|
||||
const hs = harnessesOf(d.model);
|
||||
if (!hs.includes(d.harness)) d.harness = hs.length === 1 ? hs[0] : '';
|
||||
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(); });
|
||||
|
||||
const hasDraft = () => !!(draft && data && JSON.stringify(draft) !== JSON.stringify(fromRoute(data.route)));
|
||||
window.MosaicTriageRoute = { section, hasDraft };
|
||||
})();
|
||||
@@ -40,11 +40,15 @@
|
||||
return value;
|
||||
}
|
||||
const plain = v => v !== null && typeof v === 'object' && !Array.isArray(v);
|
||||
// #1873: the templates no organization may hold (organization vocabulary SYSTEM_ONLY). The server leaves
|
||||
// them out of the read; this filter stays so a step role never resolves to one and the picker never offers one.
|
||||
const SYSTEM_ONLY = ['triage'];
|
||||
const offered = list => (list || []).filter(r => !SYSTEM_ONLY.includes(r));
|
||||
async function read(org, s) {
|
||||
const value = await call(`${base(org)}/workflows`, 'GET');
|
||||
if (!plain(value) || !plain(value.workflows) || !plain(value.projects) || !plain(value.roles) || typeof value.revision !== 'string') throw Error('The workflow read returned an unexpected answer.');
|
||||
s.data = value;
|
||||
s.known = new Set([...(value.roles.definitions || []), ...(value.roles.templates || []), 'human']);
|
||||
s.known = new Set([...(value.roles.definitions || []), ...offered(value.roles.templates), 'human']);
|
||||
}
|
||||
// SP17: a write in flight holds the poll. Each organization counts its writes (epoch moves when one starts
|
||||
// and when it ends); a read that began before a write ended, or answers while one is in flight, is dropped
|
||||
@@ -310,7 +314,7 @@
|
||||
// The role picker: human, the organization's roles, the role templates, and first any role the step names
|
||||
// that resolves to none of those (checked and marked, so leaving it in is a choice the save check names).
|
||||
function rolePick(s, chosen, prefix, off = false) {
|
||||
const defs = [...(s.data.roles.definitions || [])].sort(), tpl = [...(s.data.roles.templates || [])].filter(r => !defs.includes(r)).sort();
|
||||
const defs = [...(s.data.roles.definitions || [])].sort(), tpl = offered(s.data.roles.templates).filter(r => !defs.includes(r)).sort();
|
||||
const odd = chosen.filter(r => !s.known.has(r));
|
||||
const box = (r, mark = '') => `<label class="wfg-pick"><input type="checkbox" name="role" value="${esc(r)}"${chosen.includes(r) ? ' checked' : ''} data-focus-key="${esc(`${prefix}:role:${r}`)}"> <code>${esc(r)}</code>${mark}</label>`;
|
||||
const group = (title, list, mark) => (list.length ? `<p class="wfg-pick-h">${title}</p>${list.map(r => box(r, mark)).join('')}` : '');
|
||||
@@ -620,9 +624,10 @@
|
||||
const d = drafts.get(key);
|
||||
if (!d || d.busy) return;
|
||||
// SP17 review: a new step typed in the add form is not saved over or dropped: Save says so and keeps the
|
||||
// form. An add form with nothing typed in it is closed and the rest is saved.
|
||||
// form. So is one in which only the state was changed. An add form left as it opened is closed and the
|
||||
// rest is saved.
|
||||
if (d.edit?.mode === 'add') {
|
||||
if (d.edit.title.trim() || d.edit.roles.length) {
|
||||
if (d.edit.title.trim() || d.edit.roles.length || editDirty(d.edit)) {
|
||||
d.atBar = false; d.status = `Not saved: new step ${d.edit.i + 1} is not added yet. Press Add step, or Cancel to drop it.`;
|
||||
say(d.status); paint(); return toAddTitle();
|
||||
}
|
||||
@@ -818,7 +823,5 @@
|
||||
window.matchMedia(NARROW).addEventListener('change', () => {
|
||||
if (document.querySelector('[data-wfg-graph]')) document.dispatchEvent(new CustomEvent('mosaic-workspace-render'));
|
||||
});
|
||||
// section(): the organization page's old form editor is gone (SP17); workspace.js still asks for it.
|
||||
const section = () => '';
|
||||
window.MosaicWorkflows = { load, section, page, card, hasDraft, forget, landed };
|
||||
window.MosaicWorkflows = { load, page, card, hasDraft, forget, landed };
|
||||
})();
|
||||
@@ -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;
|
||||
@@ -367,21 +372,20 @@
|
||||
${orgTiles}
|
||||
${docrow}
|
||||
<div class="ws-split">
|
||||
<section aria-label="Sessions and workflows">
|
||||
<section aria-label="Sessions, workflows, autonomy and starter packs">
|
||||
${secH('Active agent sessions', live ? (noRuns ? 'running not read' : live.length ? `${live.length} running${tileAgents ? `, ${liveWorking} working` : ', working not read'}` : 'none running') : '', `<a href="#/agents?${oq}" data-focus-key="agents-all">This organization's agents</a>`)}
|
||||
${sessions}
|
||||
${window.MosaicWorkflows?.card?.(id, { stopped, label: label(org) }) ?? ''}
|
||||
<div class="ws-wide">
|
||||
${stopped ? stoppedPanel('data-autonomy', 'Autonomy') : window.MosaicAutonomy?.section(id) ?? ''}
|
||||
${stopped ? stoppedPanel('data-packs', 'Starter packs') : window.MosaicPacks?.section(id) ?? ''}
|
||||
</div>
|
||||
</section>
|
||||
<section aria-label="Settings">
|
||||
${secH('Settings', 'every setting, with where its value comes from')}
|
||||
${settingsList(org, { business, settings, instances, noRuns, orgCount: list ? list.length : null })}
|
||||
</section>
|
||||
</div>
|
||||
<div class="ws-wide">
|
||||
${stopped ? stoppedPanel('data-autonomy', 'Autonomy') : window.MosaicAutonomy?.section(id) ?? ''}
|
||||
${stopped ? stoppedPanel('data-packs', 'Starter packs') : window.MosaicPacks?.section(id) ?? ''}
|
||||
${stopped ? stoppedPanel('data-workflows', 'Workflows') : window.MosaicWorkflows?.section(id) ?? ''}
|
||||
</div>
|
||||
${await drawerRead}
|
||||
</article>`;
|
||||
}
|
||||
@@ -1118,9 +1122,22 @@
|
||||
};
|
||||
const DRAWER_LIMIT = 5, STOP_REASON = 'Stopped from the agent drawer.';
|
||||
const STOP_UNKNOWN = 'Outcome unknown. Read the agents again before trying; nothing was retried.';
|
||||
// The Stop state of the drawer's agent survives repaints: `confirm` while it asks, `busy` while the
|
||||
// request is open, then the result in words beside the button.
|
||||
let stopState = null; // { id, confirm, busy, text }
|
||||
// SP20: an organization that refuses a Stop or a Restart, in the same line for both.
|
||||
const orgRefusal = (e, orgName) => e === 'org-archived' ? `${orgName} is archived, so its agents are not changed. Nothing changed.`
|
||||
: e === 'org-unavailable' ? `${orgName} is not available to this Console. Nothing changed.` : '';
|
||||
// Each drawer agent's question and Stop survive repaints: `confirm` while it asks, `busy` while the
|
||||
// request is open, then the result in words beside the button. SP19: `kind` says whether Stop or
|
||||
// Restart asks (no kind is Stop). While either asks or runs, both buttons wait. SP20: kept per agent,
|
||||
// as restarts are, so two Stops in two drawers each keep their line. An entry goes when the drawer
|
||||
// closes after it finished, when its question is cancelled, or when its Restart starts.
|
||||
const stops = new Map(); // id -> { kind, confirm, busy, text }
|
||||
// SP19: each drawer agent's restart, kept per agent so several can run at once and none loses its
|
||||
// line when another drawer asks or stops: `busy` while it runs, then its outcome in words. An entry
|
||||
// goes when a new question starts on that agent, or when the drawer closes after it finished.
|
||||
const restarts = new Map(); // id -> { busy, text }
|
||||
// The state a drawer draws: its own question or Stop first, else its restart.
|
||||
const actState = id => stops.has(id) ? { id, ...stops.get(id) } : restarts.has(id) ? { id, kind: 'restart', ...restarts.get(id) } : null;
|
||||
const RESTART_REASON = 'restart from the agent drawer';
|
||||
let drawerFocus = false, drawerShown = null;
|
||||
// The drawer holds at most one history entry. A row opened from the page without a drawer pushes one
|
||||
// entry marked in history.state; switching rows replaces it. Close goes Back only from that marked
|
||||
@@ -1133,11 +1150,16 @@
|
||||
const sansAgent = () => { const n = queryOf(); n.delete('agent'); const s = n.toString(); return `${/^#\/orgs\/[^/?]+/.exec(location.hash)?.[0] ?? '#'}${pathOf()}${s ? `?${s}` : ''}`; };
|
||||
// The row's name as a link that opens the drawer: a native link, so Tab reaches it and Enter opens it.
|
||||
const drawerLink = (q, a) => `<a class="ws-drawer-open" href="${esc(drawerHref(q, `${a.org}:${a.key}`))}" data-ws-drawer-open="${esc(`${a.org}:${a.key}`)}" data-focus-key="${esc(`agent:${a.org}:${a.key}`)}">${esc(a.name)}</a>`;
|
||||
const stopConfirm = (name, who) => `<span class="ws-drawer-confirm" data-drawer-stop-confirm>Stop ${esc(who)}? <button type="button" class="ws-btn danger" data-drawer-stop-yes data-focus-key="drawer-stop-yes">Stop ${esc(name)}</button> <button type="button" class="ws-btn" data-drawer-stop-no data-focus-key="drawer-stop-no">Cancel</button></span>`;
|
||||
function stopActions(pick, a, orgName) {
|
||||
const st = stopState?.id === pick.id ? stopState : null;
|
||||
const who = `${a.name} in ${orgName}`;
|
||||
return `<button type="button" class="ws-btn" data-drawer-stop data-focus-key="drawer-stop"${st?.busy || st?.confirm ? ' disabled' : ''}>${st?.busy ? 'Stopping…' : 'Stop'}<span class="ws-sr"> ${esc(who)}</span></button>${st?.confirm ? stopConfirm(a.name, who) : ''}`;
|
||||
// SP19: Stop and Restart ask in the same confirm step, each under its own data attributes.
|
||||
const actConfirm = (kind, name, question) => `<span class="ws-drawer-confirm" data-drawer-${kind}-confirm>${esc(question)} <button type="button" class="ws-btn danger" data-drawer-${kind}-yes data-focus-key="drawer-${kind}-yes">${kind === 'stop' ? 'Stop' : 'Restart'} ${esc(name)}</button> <button type="button" class="ws-btn" data-drawer-${kind}-no data-focus-key="drawer-${kind}-no">Cancel</button></span>`;
|
||||
const stopConfirm = (name, who) => actConfirm('stop', name, `Stop ${who}?`);
|
||||
const restartConfirm = (name, who, thread) => actConfirm('restart', name, `Restart ${who}? ${thread ? 'It ends its session; its next message starts it again.' : 'It ends its session and launches it again.'}`);
|
||||
function stopActions(pick, a, orgName, thread) {
|
||||
const st = actState(pick.id), kind = st?.kind || 'stop';
|
||||
const who = `${a.name} in ${orgName}`, hold = st?.busy || st?.confirm ? ' disabled' : '';
|
||||
const stop = `<button type="button" class="ws-btn" data-drawer-stop data-focus-key="drawer-stop"${hold}>${st?.busy && kind === 'stop' ? 'Stopping…' : 'Stop'}<span class="ws-sr"> ${esc(who)}</span></button>${st?.confirm && kind === 'stop' ? stopConfirm(a.name, who) : ''}`;
|
||||
if (!window.MosaicAgents?.restart) return stop;
|
||||
return `${stop} <button type="button" class="ws-btn" data-drawer-restart data-drawer-thread="${thread ? 'yes' : 'no'}" data-focus-key="drawer-restart"${hold}>${st?.busy && kind === 'restart' ? 'Restarting…' : 'Restart'}<span class="ws-sr"> ${esc(who)}</span></button>${st?.confirm && kind === 'restart' ? restartConfirm(a.name, who, thread) : ''}`;
|
||||
}
|
||||
// SP13 (#1873): the drawer's instances read gives the agent's state. The drawer does not hold the
|
||||
// page for it: once the page's own reads have answered (`opts.page`), a drawer whose instances read
|
||||
@@ -1336,7 +1358,8 @@
|
||||
: a.level !== 'org' ? 'No Message: a copy is not messaged from here.'
|
||||
: 'No Message: this Console messages only conversation agents, such as the Assistant.'}</p>`);
|
||||
else if (change) acts.push(`<p class="note" data-drawer-nomessage>No Message: this Console does not send chat in ${esc(orgName)}.</p>`);
|
||||
if (change && a.level === 'org' && !a.keep && running(item) > 0) acts.push(stopActions(pick, a, orgName));
|
||||
// SP19: a restart that is running keeps its buttons even when a repaint reads no run between its end and its launch.
|
||||
if (change && a.level === 'org' && !a.keep && (running(item) > 0 || restarts.get(id)?.busy)) acts.push(stopActions(pick, a, orgName, !!window.MosaicAgents?.isThread?.(def)));
|
||||
// Why Stop is not offered, in words, where this Console could otherwise change the organization's agents.
|
||||
// SP13: "not running" is a reason only once the instances read has given the agent's runs.
|
||||
else if (change) acts.push(`<p class="note" data-drawer-nostop>${reading ? 'Its state is being read. Stop is offered once it is read and the agent is running.'
|
||||
@@ -1344,10 +1367,25 @@
|
||||
: a.level !== 'org' ? 'A copy is not stopped from here.'
|
||||
: a.keep ? `${esc(a.name)} is one of ${esc(orgName)}’s protected agents, so it is not stopped from here.`
|
||||
: 'It is not running, so there is nothing to stop.'}</p>`);
|
||||
const st = stopState?.id === id ? stopState : null;
|
||||
const st = actState(id);
|
||||
const actions = chat || change
|
||||
? `<div class="ws-drawer-acts" data-drawer-actions>${acts.join(' ')}<p class="ws-drawer-status" id="ws-drawer-status" role="status" data-drawer-status>${esc(st?.text || '')}</p></div>`
|
||||
: `<p class="note" data-drawer-readonly>This Console only reads ${esc(orgName)}. It can not message or stop its agents.</p>`;
|
||||
// SP18: the agents read lists only roles a run holds now, and thread.claims only conversations a run
|
||||
// holds now; each row carries that run and when it took the role or the conversation. A claim counts
|
||||
// only when the instances read lists its run as running, so a claim left by a run that ended does not
|
||||
// date a stopped agent. Last activity is the latest such time. No read keeps when a run ended or when
|
||||
// the agent last sent a message (a thread's messages read pages from the oldest message), so neither is shown.
|
||||
const runs = !unknown && runsRead(item) ? item.running : null;
|
||||
const claims = [a.s ? { ...a.s, what: 'the role' } : null, ...arr(body?.sessions).filter(x => x?.kind === 'thread' && x.org === org && x.instance === key).map(x => ({ ...x, what: 'the conversation' }))]
|
||||
.filter(c => c && msOf(c.claimedAt) !== null && (!runs || runs.includes(c.run))).sort((x, y) => msOf(y.claimedAt) - msOf(x.claimedAt));
|
||||
const last = claims[0], claimWhy = notRead(['agents', 'claims']);
|
||||
// With the runs not read, nothing checked the claim against a live run, so the line says "a run".
|
||||
const activity = last ? `${!runs ? 'a run' : runs.length > 1 ? `one of its ${runs.length} runs` : 'its run'} took ${last.what} <time datetime="${esc(last.claimedAt)}" title="${esc(new Date(msOf(last.claimedAt)).toLocaleString())}">${esc(ago(last.claimedAt))}</time>`
|
||||
: `<span class="muted">${claimWhy ? `not read: ${esc(claimWhy)}`
|
||||
: runs?.length ? `not recorded: ${runs.length > 1 ? 'its runs hold' : 'its run holds'} no role or conversation yet`
|
||||
: !runs ? 'not recorded: the agents and claims reads show no run holding its role or a conversation, and its runs were not read'
|
||||
: 'not recorded: no run holds it now, and no read keeps when its last run ended'}</span>`;
|
||||
const fields = [
|
||||
field('org', 'Organization', orgChip(o, org)),
|
||||
// A pack hire's pack is named once, in Hired from; the Role row says only that it was hired.
|
||||
@@ -1358,11 +1396,7 @@
|
||||
model ? field('model', 'Model', `<span class="mono">${esc(model)}</span>`) : '',
|
||||
project ? field('project', 'Project', `<a href="${esc(`${orgHref(org)}/projects/${enc(project.id)}`)}">${esc(project.name || project.key || project.id)}</a>${project.from ? ` <span class="t2">${project.from}</span>` : ''}`) : '',
|
||||
task ? field('task', 'Task', `<a href="${esc(taskHref(task.ref))}">${esc(task.title || task.ref)}</a>${task.title ? ` <span class="t2 mono">${esc(task.ref)}</span>` : ''}`) : '',
|
||||
// SP16: no read the Console makes carries the time of a role agent's last message or run. The agents
|
||||
// read's time is when it took the role and `since` is when its present state began, so neither is shown.
|
||||
// An agent holding a conversation has its messages in that thread's messages read, but that read pages
|
||||
// from the oldest message, so its last message is a walk of every page away; the drawer does not make it.
|
||||
field('activity', 'Last activity', `<span class="muted">${thread?.thread ? 'not shown: reading it means reading the whole conversation' : 'not recorded'}</span>`),
|
||||
field('activity', 'Last activity', activity),
|
||||
].join('');
|
||||
return shell(a.name, `<dl class="ws-drawer-fields">${fields}</dl>
|
||||
<section class="ws-drawer-sec" data-drawer-tasks aria-label="Open tasks assigned to ${esc(a.name)}"><h3>Open tasks</h3>${tasksBody}${tasksAll}</section>
|
||||
@@ -1372,11 +1406,22 @@
|
||||
// Closing goes Back when a row link on this page pushed the current entry; otherwise it drops
|
||||
// `agent` from the address in place. Either way the page repaints without it and focus returns to the row.
|
||||
function closeDrawer() {
|
||||
if (!stopState?.busy) stopState = null;
|
||||
for (const [k, st] of stops) if (!st.busy) stops.delete(k);
|
||||
for (const [k, r] of restarts) if (!r.busy) restarts.delete(k);
|
||||
if (stateOf()?.[DRAWER_MARK] && queryOf().has('agent')) { history.back(); return; }
|
||||
history.replaceState(unmarked(), '', sansAgent());
|
||||
document.dispatchEvent(new CustomEvent('mosaic-workspace-render'));
|
||||
}
|
||||
// SP18: below 701 px the open drawer is a sheet and shell.css holds the page under it to one screen, which
|
||||
// scrolls the page to the top. The page's place is kept while no drawer is open, with the page the sheet
|
||||
// opened over. It is put back only when the drawer closes onto that same page (Close, Escape or Back); a
|
||||
// link in the sheet goes to another page, which starts at its top, and the kept place is dropped. Some
|
||||
// pages a link opens (a project, the Assistant) are drawn without afterPaint, so the place is dropped as
|
||||
// soon as the address leaves the page under the sheet.
|
||||
const SHEET = window.matchMedia('(max-width:700px)');
|
||||
let pageY = 0, sheetOver = null;
|
||||
window.addEventListener('scroll', () => { if (!document.querySelector('#bus-view [data-ws-drawer]')) pageY = window.scrollY; }, { passive: true });
|
||||
window.addEventListener('hashchange', () => { if (sheetOver !== null && sansAgent() !== sheetOver) { sheetOver = null; pageY = 0; } });
|
||||
// bus.js calls this after each paint: focus moves into a drawer a row opened, and back to the row
|
||||
// when the drawer has closed and focus was left nowhere.
|
||||
function afterPaint() {
|
||||
@@ -1384,8 +1429,13 @@
|
||||
const view = document.getElementById('bus-view'), d = view?.querySelector('[data-ws-drawer]');
|
||||
const now = d?.dataset.wsDrawer || null, was = drawerShown;
|
||||
drawerShown = now;
|
||||
if (now) sheetOver = sansAgent();
|
||||
if (d && drawerFocus) { drawerFocus = false; d.querySelector('h2')?.focus({ preventScroll: true }); return; }
|
||||
drawerFocus = false;
|
||||
if (!now && sheetOver !== null) {
|
||||
if (SHEET.matches && pageY && sheetOver === sansAgent()) window.scrollTo(0, pageY);
|
||||
sheetOver = null; pageY = window.scrollY;
|
||||
}
|
||||
if (!now && was) {
|
||||
const a = document.activeElement;
|
||||
if (!a || a === document.body || !view?.contains(a)) view?.querySelector(`[data-focus-key="${CSS.escape(`agent:${was}`)}"]`)?.focus({ preventScroll: true });
|
||||
@@ -1393,10 +1443,11 @@
|
||||
}
|
||||
async function stopAgent(d) {
|
||||
const id = d.dataset.wsDrawer, org = d.dataset.drawerOrgId, name = id.slice(org.length + 1), orgName = d.dataset.drawerOrgName;
|
||||
stopState = { id, busy: true, text: `Stopping ${name}…` };
|
||||
stops.set(id, { busy: true, text: `Stopping ${name}…` });
|
||||
const repaint = () => document.dispatchEvent(new CustomEvent('mosaic-workspace-render'));
|
||||
const paint = () => { const now = document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"]`); if (!now) return; const s = now.querySelector('[data-drawer-status]'); if (s) s.textContent = stopState.text; const b = now.querySelector('[data-drawer-stop]'); if (b) { b.disabled = !!stopState.busy; b.firstChild.textContent = stopState.busy ? 'Stopping…' : 'Stop'; } now.querySelector('[data-drawer-stop-confirm]')?.remove(); };
|
||||
const finish = text => { stopState = { id, text }; paint(); const b = document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"] [data-drawer-stop]`); b?.focus({ preventScroll: true }); };
|
||||
// Paint this agent's drawer in place, when it is on screen.
|
||||
const paint = () => { const now = document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"]`), st = stops.get(id); if (!now || !st) return; const s = now.querySelector('[data-drawer-status]'); if (s) s.textContent = st.text; const b = now.querySelector('[data-drawer-stop]'); if (b) { b.disabled = !!st.busy; b.firstChild.textContent = st.busy ? 'Stopping…' : 'Stop'; } const rb = now.querySelector('[data-drawer-restart]'); if (rb) rb.disabled = !!st.busy; now.querySelector('[data-drawer-stop-confirm]')?.remove(); };
|
||||
const finish = text => { stops.set(id, { text }); paint(); const b = document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"] [data-drawer-stop]`); b?.focus({ preventScroll: true }); };
|
||||
paint();
|
||||
let res, body = null;
|
||||
try { res = await fetch(`/api/orgs/${enc(org)}/agent/terminate`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ instance: name, reason: STOP_REASON }), cache: 'no-store', redirect: 'manual' }); }
|
||||
@@ -1404,6 +1455,8 @@
|
||||
if (res.type === 'opaqueredirect' || res.status === 401) return finish('Not stopped: sign in again, then read the agents.');
|
||||
try { body = await res.json(); } catch { body = null; }
|
||||
if (!res.ok || body?.sent !== true && !body?.request) {
|
||||
const orgWhy = orgRefusal(body?.error, orgName);
|
||||
if (orgWhy) return finish(`Not stopped: ${orgWhy}`);
|
||||
if (body?.sent === false && typeof body.message === 'string' && body.message) return finish(`Not stopped: ${body.message}`);
|
||||
if (res.status === 403 && body?.error === 'read-only') return finish(`Not stopped: this Console only reads agents in ${orgName} (read-only).`);
|
||||
if (body?.sent === false && typeof body.error === 'string') return finish(`Not stopped: the bus refused (${body.error}).`);
|
||||
@@ -1411,18 +1464,84 @@
|
||||
}
|
||||
const request = typeof body?.request === 'string' ? body.request : '';
|
||||
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(request)) return finish(STOP_UNKNOWN);
|
||||
stopState = { id, busy: true, text: `Stop asked: request ${request} is open.` }; paint();
|
||||
stops.set(id, { busy: true, text: `Stop asked: request ${request} is open.` }); paint();
|
||||
for (let tries = 0; tries < 90; tries++) {
|
||||
await new Promise(r => setTimeout(r, 2000));
|
||||
let r = null;
|
||||
try { const got = await fetch(`/api/orgs/${enc(org)}/agent/request?id=${enc(request)}`, { cache: 'no-store', redirect: 'manual' }); if (got.type === 'opaqueredirect') return finish('Sign in again, then read the agents.'); r = got.ok ? await got.json() : null; } catch { continue; }
|
||||
if (r?.request === request && r.state === 'closed') {
|
||||
if (r.reason) return finish(`Not stopped: ${r.reason}.`);
|
||||
stopState = { id, text: `Stopped ${name}.` }; repaint(); return;
|
||||
stops.set(id, { text: `Stopped ${name}.` }); repaint(); return;
|
||||
}
|
||||
}
|
||||
finish(`Request ${request} is still open. Read the agents again later.`);
|
||||
}
|
||||
// SP19: the drawer's Restart. The agents.js chain ends the agent through its own organization's routes
|
||||
// (the drawer's, which on #/agents is not the page's), waits for the request to close and its runs to
|
||||
// reach 0, then launches it; a conversation agent is not launched. It never regenerates. Each step is
|
||||
// painted in place; a refusal before the end leaves both buttons usable and focus on Restart.
|
||||
const refusedWhy = (d, orgName) => {
|
||||
const e = d?.result?.error, m = d?.result?.message;
|
||||
if (d?.status === 403 && e === 'read-only') return `this Console only reads agents in ${orgName} (read-only).`;
|
||||
if (d?.status === 403 && (e === 'unavailable' || e === 'owner-only')) return `this Console is not the owner of ${orgName}, so it does not restart its agents.`;
|
||||
if (e === 'no-bus-host') return `the bus of ${orgName} is not running. Nothing changed.`;
|
||||
const orgWhy = orgRefusal(e, orgName);
|
||||
if (orgWhy) return orgWhy;
|
||||
if (d?.result?.sent === false && typeof m === 'string' && m) return m;
|
||||
if (d?.result?.sent === false && typeof e === 'string') return `the bus refused (${e}).`;
|
||||
return STOP_UNKNOWN;
|
||||
};
|
||||
const restartWords = (name, orgName) => (step, d) => ({
|
||||
ending: `Restarting ${name}: asking it to end…`,
|
||||
signedOut: 'Not restarted: sign in again, then read the agents.',
|
||||
endUnknown: `Not restarted: ${STOP_UNKNOWN}`,
|
||||
endRefused: `Not restarted: ${refusedWhy(d, orgName)}`,
|
||||
endOpen: `Not restarted: ${d?.stop}`,
|
||||
endClosed: `Not restarted: ${name} was not ended (${d?.reason}).`,
|
||||
ended: `${name} was asked to end. Waiting for its session to end…`,
|
||||
stillRunning: `Not restarted: ${name} was asked to end, but its session is still running. Read the agents again before trying.`,
|
||||
threadEnded: `Ended ${name} at ${d?.time}. Its next message starts it.`,
|
||||
launching: `${name} has ended. Launching it…`,
|
||||
launchSignedOut: `${name} was ended and not launched: sign in again, then read the agents.`,
|
||||
launchUnknown: `${name} was ended. The launch outcome is unknown; read the agents again before trying. Nothing was retried.`,
|
||||
launchRefused: `${name} was ended and not launched: ${refusedWhy(d, orgName)}`,
|
||||
launchOpen: `${name} was ended. ${d?.stop}`,
|
||||
launchClosed: `${name} was ended and not launched: ${d?.reason}.`,
|
||||
restarted: `Restarted ${name}${d?.run ? ` (run ${d.run})` : ''}.`,
|
||||
})[step];
|
||||
async function restartAgent(d) {
|
||||
const id = d.dataset.wsDrawer, org = d.dataset.drawerOrgId, name = id.slice(org.length + 1), orgName = d.dataset.drawerOrgName;
|
||||
const thread = d.querySelector('[data-drawer-restart]')?.dataset.drawerThread === 'yes';
|
||||
if (restarts.get(id)?.busy) return;
|
||||
// The question is answered: from now on this agent's drawer draws its restart.
|
||||
stops.delete(id);
|
||||
restarts.set(id, { busy: true, text: '' });
|
||||
// Paint this agent's drawer in place, when it is on screen and no question of its own holds it.
|
||||
const paint = () => {
|
||||
const now = document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"]`), r = restarts.get(id); if (!now || !r || stops.has(id)) return;
|
||||
const s = now.querySelector('[data-drawer-status]'); if (s) s.textContent = r.text;
|
||||
const rb = now.querySelector('[data-drawer-restart]'), sb = now.querySelector('[data-drawer-stop]');
|
||||
if (rb) { rb.disabled = r.busy; rb.firstChild.textContent = r.busy ? 'Restarting…' : 'Restart'; }
|
||||
if (sb) sb.disabled = r.busy;
|
||||
now.querySelector('.ws-drawer-confirm')?.remove();
|
||||
};
|
||||
// The finished line stays in the entry. A refusal before the end changed nothing, so focus goes back
|
||||
// to Restart. Once the agent was ended its runs changed: the repaint reads the instances again, so the
|
||||
// buttons follow them (after a refused launch it has none, so Stop and Restart go), and focus goes to
|
||||
// the drawer's heading, which every repaint keeps.
|
||||
const after = new Set(['restarted', 'threadEnded', 'launchSignedOut', 'launchUnknown', 'launchRefused', 'launchOpen', 'launchClosed']);
|
||||
const done = step => {
|
||||
document.querySelector(`[data-ws-drawer="${CSS.escape(id)}"]`)?.querySelector(after.has(step) ? '[data-focus-key="drawer-h"]' : '[data-drawer-restart]')?.focus({ preventScroll: true });
|
||||
if (after.has(step)) document.dispatchEvent(new CustomEvent('mosaic-workspace-render'));
|
||||
};
|
||||
const onStatus = ({ step, busy, text }) => {
|
||||
restarts.set(id, { busy: !!busy, text });
|
||||
paint();
|
||||
if (!busy) done(step);
|
||||
};
|
||||
try { await window.MosaicAgents.restart(name, thread, { org, reason: RESTART_REASON, text: restartWords(name, orgName), onStatus }); }
|
||||
catch { restarts.set(id, { busy: false, text: `Not restarted: ${STOP_UNKNOWN}` }); paint(); done('endUnknown'); }
|
||||
}
|
||||
document.addEventListener('click', e => {
|
||||
const link = e.target.closest?.('[data-ws-drawer-open]');
|
||||
if (link) {
|
||||
@@ -1441,33 +1560,41 @@
|
||||
if (!d) return;
|
||||
if (e.target.closest('[data-drawer-close]')) { closeDrawer(); return; }
|
||||
const id = d.dataset.wsDrawer;
|
||||
if (e.target.closest('[data-drawer-stop]') && !(stopState?.id === id && stopState.busy)) {
|
||||
const b = d.querySelector('[data-drawer-stop]'), name = d.dataset.drawerName;
|
||||
stopState = { id, confirm: true, text: '' };
|
||||
d.querySelector('[data-drawer-stop-confirm]')?.remove();
|
||||
b.insertAdjacentHTML('afterend', stopConfirm(name, `${name} in ${d.dataset.drawerOrgName}`));
|
||||
b.disabled = true;
|
||||
// SP19: nothing starts while this agent's Stop or Restart runs.
|
||||
const held = !!stops.get(id)?.busy || !!restarts.get(id)?.busy;
|
||||
const ask = e.target.closest('[data-drawer-stop]') ? 'stop' : e.target.closest('[data-drawer-restart]') ? 'restart' : null;
|
||||
if (ask && !held) {
|
||||
const b = d.querySelector(`[data-drawer-${ask}]`), name = d.dataset.drawerName, who = `${name} in ${d.dataset.drawerOrgName}`;
|
||||
stops.set(id, { kind: ask, confirm: true, text: '' }); restarts.delete(id);
|
||||
d.querySelector('.ws-drawer-confirm')?.remove();
|
||||
b.insertAdjacentHTML('afterend', ask === 'stop' ? stopConfirm(name, who) : restartConfirm(name, who, b.dataset.drawerThread === 'yes'));
|
||||
for (const x of d.querySelectorAll('[data-drawer-stop], [data-drawer-restart]')) x.disabled = true;
|
||||
const s = d.querySelector('[data-drawer-status]'); if (s) s.textContent = '';
|
||||
d.querySelector('[data-drawer-stop-no]')?.focus({ preventScroll: true });
|
||||
d.querySelector(`[data-drawer-${ask}-no]`)?.focus({ preventScroll: true });
|
||||
return;
|
||||
}
|
||||
if (e.target.closest('[data-drawer-stop-no]')) { cancelStop(d); return; }
|
||||
if (ask) return;
|
||||
if (e.target.closest('[data-drawer-stop-no], [data-drawer-restart-no]')) { cancelStop(d); return; }
|
||||
if (held) return;
|
||||
if (e.target.closest('[data-drawer-stop-yes]')) stopAgent(d);
|
||||
else if (e.target.closest('[data-drawer-restart-yes]')) restartAgent(d);
|
||||
});
|
||||
// Cancel the Stop question: nothing sent, Stop enabled again and focused.
|
||||
// Cancel the Stop or Restart question: nothing sent, both buttons enabled again, focus on the one that asked.
|
||||
function cancelStop(d) {
|
||||
stopState = null;
|
||||
d.querySelector('[data-drawer-stop-confirm]')?.remove();
|
||||
const b = d.querySelector('[data-drawer-stop]'); if (b) { b.disabled = false; b.focus({ preventScroll: true }); }
|
||||
const kind = d.querySelector('[data-drawer-restart-confirm]') ? 'restart' : 'stop';
|
||||
stops.delete(d.dataset.wsDrawer);
|
||||
d.querySelector('.ws-drawer-confirm')?.remove();
|
||||
for (const x of d.querySelectorAll('[data-drawer-stop], [data-drawer-restart]')) x.disabled = false;
|
||||
d.querySelector(`[data-drawer-${kind}]`)?.focus({ preventScroll: true });
|
||||
}
|
||||
// Escape cancels the Stop question while it asks, else closes the drawer, unless a dialog is open
|
||||
// Escape cancels the Stop or Restart question while it asks, else closes the drawer, unless a dialog is open
|
||||
// or something else took the key.
|
||||
document.addEventListener('keydown', e => {
|
||||
if (e.key !== 'Escape' || e.defaultPrevented || document.querySelector('dialog[open]')) return;
|
||||
const d = document.querySelector('#bus-view [data-ws-drawer]');
|
||||
if (!d) return;
|
||||
e.preventDefault();
|
||||
if (d.querySelector('[data-drawer-stop-confirm]')) cancelStop(d); else closeDrawer();
|
||||
if (d.querySelector('.ws-drawer-confirm')) cancelStop(d); else closeDrawer();
|
||||
});
|
||||
|
||||
// ---- controls: filters write the hash query and repaint in place ----
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { AUTONOMY_ROUTE, autonomyHTTP } from './autonomy-http.mjs';
|
||||
import { proxyHTTP } from './proxy-http.mjs';
|
||||
import { proxyAdminHTTP } from './proxy-admin.mjs';
|
||||
import { triageHTTP, TRIAGE_PATHS } from './triage-http.mjs';
|
||||
import { createServer } from 'node:http';
|
||||
import { bindTesterSocket, TESTER_ORIGIN } from './tester-socket.mjs';
|
||||
import { readFileSync, openSync, fstatSync, closeSync, constants } from 'node:fs';
|
||||
@@ -65,7 +66,7 @@ const files = new Map([
|
||||
['/', ['index.html', 'text/html; charset=utf-8']],
|
||||
['/proxy.js', ['proxy.js', 'text/javascript; charset=utf-8']],
|
||||
['/project-remote.js', ['project-remote.js', 'text/javascript; charset=utf-8']],
|
||||
...['app.js', 'brand.js', 'bus.js', 'markdown-preview.js', 'chat.js', 'project-archive.js', 'processing.js', 'agents.js', 'federation.js', 'requests.js', 'org-create.js', 'console-restart.js', 'onboarding.js', 'workspace-data.js', 'workspace.js', 'tasks-board.js', 'assistant.js'].map(f => ['/' + f, [f, 'text/javascript; charset=utf-8']]),
|
||||
...['app.js', 'brand.js', 'bus.js', 'markdown-preview.js', 'chat.js', 'project-archive.js', 'processing.js', 'agents.js', 'federation.js', 'requests.js', 'org-create.js', 'console-restart.js', 'onboarding.js', 'workspace-data.js', 'workspace.js', 'tasks-board.js', 'assistant.js', 'triage-settings.js'].map(f => ['/' + f, [f, 'text/javascript; charset=utf-8']]),
|
||||
['/files.js', ['files.js', 'text/javascript; charset=utf-8']],
|
||||
['/projects.js', ['projects.js', 'text/javascript; charset=utf-8']],
|
||||
['/projects.css', ['projects.css', 'text/css; charset=utf-8']],
|
||||
@@ -811,7 +812,7 @@ async function libraryStatusRead(res, read, search) {
|
||||
// `log` takes one line per send attempt that got past sign-in; never the body. `fieldsFile` is for
|
||||
// tests; the field table is otherwise the committed packages/tasks/fields.json.
|
||||
// `agentWrite` (row 194, D7) is `(verb, args) => Promise` for the Agents section's verbs and instance.request.
|
||||
export async function startServer({ host = '127.0.0.1', port = 7330, board = DEFAULT_BOARD, timeout = 20000, bus = null, reads = null, preview = null, sso = null, tester = false, testerSocket = null, testerAuthHeader = null, proof = null, testerProofHeader = null, send = null, settingsWrite = null, projectFiles = null, projectFile = null, projectSave = null, libraryStatus = null, libraryRevert = null, chat = null, nameWrite = null, assistantStop = null, instancesWrite = null, routesWrite = null, agentWrite = null, proxyCall = null, proxyAdmin = null, processing = null, decisionWrite = null, requestRead = null, requestWrite = null, userOptions = null, federation = null, organizations = null, preselect = null, routes = [], orgAdmin = null, devAllowHosts = [], log = line => console.log(line), fieldsFile = undefined, restart = null, trail = null, boot = randomUUID(), workspaceTimeoutMs = undefined, agentLevels = null } = {}) {
|
||||
export async function startServer({ host = '127.0.0.1', port = 7330, board = DEFAULT_BOARD, timeout = 20000, bus = null, reads = null, preview = null, sso = null, tester = false, testerSocket = null, testerAuthHeader = null, proof = null, testerProofHeader = null, send = null, settingsWrite = null, projectFiles = null, projectFile = null, projectSave = null, libraryStatus = null, libraryRevert = null, chat = null, nameWrite = null, assistantStop = null, instancesWrite = null, routesWrite = null, agentWrite = null, proxyCall = null, proxyAdmin = null, triage = null, processing = null, decisionWrite = null, requestRead = null, requestWrite = null, userOptions = null, federation = null, organizations = null, preselect = null, routes = [], orgAdmin = null, ownerModels = null, rideRemoved = null, devAllowHosts = [], log = line => console.log(line), fieldsFile = undefined, restart = null, trail = null, boot = randomUUID(), workspaceTimeoutMs = undefined, agentLevels = null } = {}) {
|
||||
// org-lan-bind: the dev owner on the LAN is an explicit opt-in. `devAllowHosts` names the Host
|
||||
// names (without a port) a remote browser may use; only the dev owner takes them, and only with
|
||||
// them may it bind a non-loopback IPv4 address. SSO, preview and tester listeners stay loopback.
|
||||
@@ -841,7 +842,7 @@ export async function startServer({ host = '127.0.0.1', port = 7330, board = DEF
|
||||
const registered = routeTable();
|
||||
for (const registerRoutes of routes) registerRoutes(registered);
|
||||
registered.add('GET', '/api/themes', (req, res) => themesHTTP(req, res, { log }), { publicRead: true });
|
||||
const adminRoute = (req, res, context) => orgAdminHTTP(req, res, { ...context, admin: orgAdmin ??= defaultOrgAdmin(), log, trail });
|
||||
const adminRoute = (req, res, context) => orgAdminHTTP(req, res, { ...context, admin: orgAdmin ??= defaultOrgAdmin(process.env, { ownerModels, rideRemoved }), log, trail });
|
||||
for (const path of ['/api/org-template', '/api/org-presets', '/api/org-validate', '/api/org-admin', /^\/api\/orgs\/[^/]+\/(?:admin|setup|repo|delete|archive|restore|restart|stop)(?:\/(?:queue|bus|project\/[^/]+|records\/[^/]+))?$/]) {
|
||||
for (const method of ['GET', 'POST']) registered.add(method, path, adminRoute);
|
||||
}
|
||||
@@ -935,7 +936,10 @@ export async function startServer({ host = '127.0.0.1', port = 7330, board = DEF
|
||||
if (match) {
|
||||
orgId = match[1];
|
||||
path = `/api/${match[2]}`;
|
||||
} else if (!orgList && path !== '/api/config' && !hook) {
|
||||
} else if (!orgList && path !== '/api/config' && !hook
|
||||
// #1873 triage-host: the Triage routes pass only for the instance owner's own session on the
|
||||
// public name; every other caller gets this Console's unknown /api/ answer. The gate below checks again.
|
||||
&& !(triage && TRIAGE_PATHS.includes(path) && !tester && named && sso !== null && sso.session(req)?.rootOwner === true)) {
|
||||
return json(res, 404, { error: 'org-unavailable' });
|
||||
}
|
||||
}
|
||||
@@ -1044,10 +1048,16 @@ export async function startServer({ host = '127.0.0.1', port = 7330, board = DEF
|
||||
// the selected bus; only the instance owner's own session reaches them, never an org owner.
|
||||
if (path.startsWith('/api/proxy/admin/')) return proxyAdminHTTP(req, res, { path, admin: proxyAdmin, call: proxyCall, named, tester, granted,
|
||||
owner: !tester && !granted && named && sso !== null && currentSession?.rootOwner === true, send: owner.send !== null,
|
||||
origin: sso ? (localOrigin ?? sso.publicOrigin) : `https://${preview.host}`, user: signer?.user, log });
|
||||
origin: sso ? (localOrigin ?? sso.publicOrigin) : `https://${preview.host}`, user: signer?.user,
|
||||
principal: currentSession ? { provider: currentSession.provider, iss: currentSession.iss, sub: currentSession.sub } : null, log });
|
||||
if (path.startsWith('/api/proxy/')) return proxyHTTP(req, res, { path, call: proxyCall, named, tester, granted,
|
||||
send, origin: sso ? (localOrigin ?? sso.publicOrigin) : `https://${preview.host}`, user: signer?.user, log });
|
||||
}
|
||||
// #1873 triage-host: the Triage session's two routes answer only the instance owner's own session on
|
||||
// the public name, with no organization selected. Anyone else falls through to the answer an
|
||||
// unknown route gives them, so the routes are not visible to a caller who cannot use them.
|
||||
if (TRIAGE_PATHS.includes(path) && triage && !tester && !granted && named && sso !== null && orgId === null && !hook && currentSession?.rootOwner === true)
|
||||
return triageHTTP(req, res, { path, triage, send: owner.send !== null, origin: localOrigin ?? sso.publicOrigin, log });
|
||||
|
||||
// Row 252: Approve and Deny. The tester listener never offers them. Anywhere else without a signed-in
|
||||
// session on the public name under --sso and --send, they are read-only.
|
||||
@@ -1240,7 +1250,7 @@ export async function startServer({ host = '127.0.0.1', port = 7330, board = DEF
|
||||
// `send`: the page shows the composer only where a send can pass (the public name, --send on).
|
||||
// `edit`: the editor shows Save only where a save can pass, the same test as the route's.
|
||||
// `user` and `federation` (FED R6) only when there is one, so the owner's config is unchanged.
|
||||
if (path === '/api/config') return json(res, 200, { board: upstream, viewer: viewerOf(currentSession), ...(organizations && preselect ? { preselect } : {}), ...(tester ? { tester: true } : {}), ...(sso ? { sso: true } : {}), ...(sso?.devOwner ? { devOwner: true } : {}), ...(granted ? { user: true, chatGrants } : {}), ...(!tester && !granted && named && sso !== null && requestRead ? { requests: true, requestWrites: Boolean(send && requestWrite), workspaceFiles: true, projectRemotes: true, projectRemoteWrites: Boolean(send && requestWrite) } : {}), ...(hub ? { federation: hub } : {}), ...(organizations ? { orgSetup: !tester && !granted && named && sso !== null && owner.send !== null } : {}), ...(proxyAdmin && !tester && !granted && named && sso !== null && currentSession?.rootOwner === true ? { proxyAdmin: true, proxyAdminSend: owner.send !== null } : {}), send: !organizations && !tester && Boolean(send) && named, ...(organizations && orgId ? { chatSend: !tester && Boolean(send) && named && orgBusHealthy, settingsSend: !tester && !granted && Boolean(send) && named && orgBusHealthy } : {}), boot, consoleRestart: !tester && named && currentSession?.instanceRole === 'operator' && owner.send !== null && restart !== null && trail !== null, decide: (!organizations || (orgId !== null && orgBusHealthy)) && !tester && !granted && Boolean(send) && Boolean(decisionWrite) && named && sso !== null, edit: !organizations && !tester && !granted && Boolean(send) && Boolean(projectSave) && named, northStarApprove: (!organizations || (orgId !== null && orgBusHealthy)) && !tester && !granted && Boolean(send) && Boolean(projectSave) && Boolean(projectFile) && named });
|
||||
if (path === '/api/config') return json(res, 200, { board: upstream, viewer: viewerOf(currentSession), ...(organizations && preselect ? { preselect } : {}), ...(tester ? { tester: true } : {}), ...(sso ? { sso: true } : {}), ...(sso?.devOwner ? { devOwner: true } : {}), ...(granted ? { user: true, chatGrants } : {}), ...(!tester && !granted && named && sso !== null && requestRead ? { requests: true, requestWrites: Boolean(send && requestWrite), workspaceFiles: true, projectRemotes: true, projectRemoteWrites: Boolean(send && requestWrite) } : {}), ...(hub ? { federation: hub } : {}), ...(organizations ? { orgSetup: !tester && !granted && named && sso !== null && owner.send !== null } : {}), ...(proxyAdmin && !tester && !granted && named && sso !== null && currentSession?.rootOwner === true ? { proxyAdmin: true, proxyAdminSend: owner.send !== null } : {}), ...(triage && !tester && !granted && named && sso !== null && orgId === null && currentSession?.rootOwner === true ? { triage: true, triageSend: owner.send !== null } : {}), send: !organizations && !tester && Boolean(send) && named, ...(organizations && orgId ? { chatSend: !tester && Boolean(send) && named && orgBusHealthy, settingsSend: !tester && !granted && Boolean(send) && named && orgBusHealthy } : {}), boot, consoleRestart: !tester && named && currentSession?.instanceRole === 'operator' && owner.send !== null && restart !== null && trail !== null, decide: (!organizations || (orgId !== null && orgBusHealthy)) && !tester && !granted && Boolean(send) && Boolean(decisionWrite) && named && sso !== null, edit: !organizations && !tester && !granted && Boolean(send) && Boolean(projectSave) && named, northStarApprove: (!organizations || (orgId !== null && orgBusHealthy)) && !tester && !granted && Boolean(send) && Boolean(projectSave) && Boolean(projectFile) && named });
|
||||
if (path === '/healthz') return json(res, 200, { ok: true, ...(sso ? { sso: sso.health() } : {}) });
|
||||
if (path === '/favicon.ico') { res.writeHead(204); return res.end(); }
|
||||
const file = files.get(path);
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
// #1873 triage-host: the command cards a Triage reply may carry (design section 4). The agent
|
||||
// proposes a card as `{ step, org }` in a fenced ```cards block of its reply. The server keeps a card
|
||||
// only when its keys are exactly step and org, its step is one of CARD_STEPS and its org is in the
|
||||
// registry, and it renders the command text itself from the fixed map below. Anything else is
|
||||
// dropped and counted. The rest of the reply is plain text: control characters are removed and it
|
||||
// is clipped. A card has no Run action; the owner copies the command into a terminal.
|
||||
|
||||
import { ID_PATTERN } from "../../organization/src/vocabulary.mjs";
|
||||
|
||||
export const CARD_STEPS = Object.freeze({
|
||||
// On a --dev-local-owner Console the bus starts in that mode, as the Organization page's own step says.
|
||||
"bus-start": (org, { devOwner = false } = {}) => `mosaic bus start ${org}${devOwner === true ? " --dev-local-owner" : ""} --detach`,
|
||||
"bus-restart": (org) => `mosaic bus restart ${org}`,
|
||||
"bus-recover": (org) => `mosaic bus recover ${org}`,
|
||||
"bus-stop": (org) => `mosaic bus stop ${org}`,
|
||||
"org-config-validate": (org) => `mosaic org config validate ${org}`,
|
||||
});
|
||||
export const MAX_CARDS = 5;
|
||||
export const MAX_TEXT = 8000;
|
||||
const FENCE = /```cards[ \t]*\r?\n([\s\S]*?)\r?\n?```/g;
|
||||
|
||||
// Plain text: no control characters other than newline and tab, and no more than MAX_TEXT.
|
||||
export function plainText(value, max = MAX_TEXT) {
|
||||
if (typeof value !== "string") return "";
|
||||
// eslint-disable-next-line no-control-regex
|
||||
return value.replace(/\r\n?/g, "\n").replace(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f\u061c\u180e\u2028\u2029\ufeff---]/g, "").slice(0, max);
|
||||
}
|
||||
|
||||
// One card, or null. orgIds is the set of registry ids the snapshot read.
|
||||
export function validateCard(card, orgIds, opts = {}) {
|
||||
if (!card || typeof card !== "object" || Array.isArray(card) || Object.getPrototypeOf(card) !== Object.prototype) return null;
|
||||
const keys = Object.keys(card);
|
||||
if (keys.length !== 2 || !keys.includes("step") || !keys.includes("org")) return null;
|
||||
const { step, org } = card;
|
||||
if (typeof step !== "string" || !Object.hasOwn(CARD_STEPS, step)) return null;
|
||||
if (typeof org !== "string" || !ID_PATTERN.test(org) || !orgIds?.has?.(org)) return null;
|
||||
return { step, org, command: CARD_STEPS[step](org, opts) };
|
||||
}
|
||||
|
||||
// The reply split into its plain text and the cards the server keeps.
|
||||
export function extractCards(reply, orgIds, opts = {}) {
|
||||
const source = typeof reply === "string" ? reply : "";
|
||||
const cards = [];
|
||||
let dropped = 0;
|
||||
const seen = new Set();
|
||||
const text = source.replace(FENCE, (_, body) => {
|
||||
let parsed;
|
||||
try { parsed = JSON.parse(body); } catch { dropped += 1; return ""; }
|
||||
const list = Array.isArray(parsed) ? parsed : [parsed];
|
||||
for (const item of list) {
|
||||
const card = validateCard(item, orgIds, opts);
|
||||
const key = card && `${card.step} ${card.org}`;
|
||||
if (!card || seen.has(key) || cards.length >= MAX_CARDS) { dropped += 1; continue; }
|
||||
seen.add(key);
|
||||
cards.push(card);
|
||||
}
|
||||
return "";
|
||||
});
|
||||
return { text: plainText(text.trim()), cards, dropped };
|
||||
}
|
||||
@@ -0,0 +1,968 @@
|
||||
// #1873 triage-host: the Triage session's host, held by the Console (design sections 1 and 4).
|
||||
//
|
||||
// Directories, under <dataRoot>/triage/ (0700, real directories owned by this user):
|
||||
// health/ the snapshot the Console writes (triage-snapshot.mjs); mounted read-only
|
||||
// agent/ the session's own run directory; mounted read-write, with bundle/, session.json and
|
||||
// relay.sock read-only inside it
|
||||
// host/ the transcript (transcript.jsonl, 0600) and the runner's log; never mounted
|
||||
// Nothing else of the data root, no org root, bus socket, config directory or secrets directory is
|
||||
// in the sandbox (packages/seat/src/sandbox.mjs, hostedBinds).
|
||||
//
|
||||
// The session starts on the owner's first message, never when the Console starts. Its spawn
|
||||
// environment is SPAWN_ENV_ALLOW and the route's ROUTE_ENV_ALLOW names; bwrap adds HOME and TMPDIR.
|
||||
// No MOSAIC_CONFIG, no MOSAIC_BUS_* and no Console variable reaches it.
|
||||
//
|
||||
// The relay is the only broker the runner reaches. It answers the runner's own lifecycle verbs with
|
||||
// fixed answers that change nothing, hands over the owner's queued messages, and keeps a
|
||||
// message.send only when it is addressed to "human": that text goes into the transcript and its
|
||||
// cards are checked (triage-cards.mjs). Every other verb, decision.raise and event.emit included,
|
||||
// a send to anyone else and a call with the wrong capability are refused with RELAY_REFUSED.
|
||||
//
|
||||
// A model route comes from an injected `route` ({ admit() }). Without one nothing launches and the
|
||||
// state says so. The route's answer names the harness: adapter, its mounts, harnessEnv, turnEnd,
|
||||
// model and provider, and the proxy variables for the spawn. Over the cap the route refuses until
|
||||
// the next day, and the message is refused with its code.
|
||||
//
|
||||
// #1873 triage-fences: the host checks a copy of the route's answer before it writes anything, and
|
||||
// uses only that copy. harnessEnv names must be in the harness module's envNames and pass
|
||||
// refusedHarnessName; values are clean strings. env holds only ROUTE_ENV_ALLOW names, with
|
||||
// ANTHROPIC_BASE_URL loopback http on a port (the host's proxyPort when it names one) and the
|
||||
// token matching the host's tokenPattern when it names one. The adapter and the mounts pass
|
||||
// checkHostedRoute (sandbox.mjs) against the module's installRoots and the data root, the config
|
||||
// directory and HOME (HOME not counted against the stack's own code), here and again in
|
||||
// hostedBinds. A refused answer reaches no spawn, answers triage-route-invalid, and writes
|
||||
// ROUTE_REFUSED to the transcript; a refusal at the binds also removes what the start wrote.
|
||||
//
|
||||
// #1873 triage-wiring: the host sets the harness's MOSAIC_ names itself. At each start it calls the
|
||||
// module's bundle() into the run's bundle/ (Claude Code: settings.json with the tool gate hook and
|
||||
// mcp.json with the typed tools' server) and its env(), and keeps the MOSAIC_ names the
|
||||
// module declares, with clean string values, never a name the runner sets. Every other name is the
|
||||
// route's, under the fence above, so no name has two sources. The thinking
|
||||
// level is the module's "low", or its first level. These join the route's fenced harnessEnv in
|
||||
// session.json. The key is never among them: #1873 harness-matrix gives bundle() and env() the
|
||||
// real proxy (pi takes it through models.json), and a MOSAIC_ value or any file under the run's
|
||||
// bundle/, home/, work/ or tmp/ that holds the key, raw or JSON-decoded, refuses the start, as
|
||||
// does a URL other than the proxy's in a file the module wrote there, or a change the module made
|
||||
// to the rest of the run directory (session/). MOSAIC_CLAUDE_BIN, the pinned binary's absolute path, passes from the
|
||||
// Console's environment as it does for a bus session. An answer the agent sends has the key, and
|
||||
// anything shaped like it, replaced before it reaches the transcript.
|
||||
//
|
||||
// #1873 triage-cap: the relay counts each turn. usage.begin takes a baseline of the harness's
|
||||
// transcript with the transcript usage-source the bus uses; usage.end takes the delta (the bus's
|
||||
// own rules, triage-usage.mjs) and appends one line to <dataRoot>/triage/usage.jsonl. When a cap is
|
||||
// stored (triage-route-store.mjs), the daily-quota plugin decides over the lines in that file: before
|
||||
// a start (admit), before the runner takes queued messages (message.receive) and before each
|
||||
// message's turn (the runner's state read outside a turn). Over the cap, or with the store or the
|
||||
// file unreadable, the turn in flight finishes, every message still queued gets a host line and
|
||||
// leaves the queue, and the runner is told its session is gone. Nothing is held in memory: each
|
||||
// check reads the file again, so a restart counts what the file holds. With no cap stored, nothing
|
||||
// is refused for usage and the lines are still written.
|
||||
|
||||
import { spawn } from "node:child_process";
|
||||
import { createHash, randomBytes, timingSafeEqual } from "node:crypto";
|
||||
import { appendFileSync, closeSync, existsSync, lstatSync, mkdirSync, readdirSync, openSync, readFileSync, rmSync, chmodSync, writeFileSync } from "node:fs";
|
||||
import { createServer } from "node:net";
|
||||
import { homedir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { BWRAP, REPO, RUNNER_FILES, checkHostedRoute, hostedArgv, hostedBinds } from "../../seat/src/sandbox.mjs";
|
||||
import { isProviderVariable } from "../../seat/src/session.mjs";
|
||||
import { configDir as systemConfigDir } from "../../organization/src/organization-config.mjs";
|
||||
import { typedTools } from "../../harness/src/tools.mjs";
|
||||
import { builtinTools, checkedTransport } from "../../harness/src/bundle.mjs";
|
||||
import { FOUNDER_ENV } from "../../harness/src/runner.mjs";
|
||||
import { extractCards, plainText } from "./triage-cards.mjs";
|
||||
import { SNAPSHOT_FILE, writeSnapshot } from "./triage-snapshot.mjs";
|
||||
import { readRoute } from "./triage-route-store.mjs";
|
||||
import { ID as USAGE_ID, appendUsage, capState, delta, nextReset, readUsage, snapshot, usagePath } from "./triage-usage.mjs";
|
||||
import { readTurn } from "../../plugins/usage-source/transcript/index.mjs";
|
||||
|
||||
export const RUNNER = RUNNER_FILES[0];
|
||||
export const RELAY_REFUSED = "triage-refused";
|
||||
export const MESSAGE_MAX = 4000;
|
||||
export const TRANSCRIPT_KEEP = 200;
|
||||
export const QUEUE_MAX = 20;
|
||||
export const UNANSWERED = "Triage stopped before it answered this message. Send it again.";
|
||||
export const NOT_STARTED = "Triage did not start, so this message was not delivered.";
|
||||
export const NOT_TAKEN = "Triage stopped before it read this message. Send it again.";
|
||||
export const ROUTE_REFUSED = "Triage did not start: the model route's answer did not pass the host's checks, so this message was not delivered.";
|
||||
export const overCapNotTaken = (until) => `Triage reached its daily token cap before it took this message. Send it again after ${until}.`;
|
||||
export const USAGE_NOT_READ = "Triage could not read its token usage or its cap, so it did not take this message.";
|
||||
// #1873 triage-wiring: two turns in a row whose usage reads unknown end the session (cap fail closed).
|
||||
export const UNKNOWN_TURNS_MAX = 2;
|
||||
export const USAGE_UNKNOWN_ENDED = "Triage ended this session: the token usage of two turns in a row could not be read, so the daily cap could not be kept. The next message starts a new session.";
|
||||
// #1873 triage-wiring: the route's refusals (triage-route.mjs), each with the one plain line the
|
||||
// drawer shows. None carries a path, a key or an env value.
|
||||
export const ROUTE_REFUSALS = Object.freeze({
|
||||
"triage-key-missing": "No Triage key is issued, so Triage will not start. The instance owner issues one first.",
|
||||
"triage-key-unsafe": "The Triage key file is not safe to use, so Triage will not start.",
|
||||
"triage-proxy-down": "The model proxy is not running, so Triage will not start.",
|
||||
"triage-model-not-offered": "The model in the Triage route is no longer offered, so Triage will not start.",
|
||||
"triage-catalogue-unavailable": "No model catalogue can confirm the Triage model now, so Triage will not start.",
|
||||
"triage-harness-incompatible": "The harness in the Triage route cannot run its model, so Triage will not start.",
|
||||
});
|
||||
// The host's own words for each code message() throws.
|
||||
const ADMIT_MESSAGES = Object.freeze({
|
||||
"triage-over-cap": "Triage is over its daily cap",
|
||||
"triage-no-route": "no model route is set for Triage",
|
||||
"triage-usage-unreadable": "Triage's token usage could not be read",
|
||||
"triage-route-unreadable": "the model route could not be read",
|
||||
"triage-dir-unsafe": "a Triage directory is not a real directory",
|
||||
...ROUTE_REFUSALS,
|
||||
});
|
||||
// An over-cap answer is held for at most this long; a later time is not taken.
|
||||
export const CAP_HOLD_MAX_MS = 48 * 3600 * 1000;
|
||||
const ISO_TIME = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z$/;
|
||||
const LINE_MAX = 64 * 1024;
|
||||
// The id the host gives each owner message. A reply may name only such an id.
|
||||
const OWNER_ID = /^owner-[0-9a-f]{12}$/;
|
||||
const CAP = /^[0-9a-f]{64}$/;
|
||||
|
||||
// The spawn's environment: these names from the Console's own environment, PATH fixed, and the
|
||||
// route's proxy variables. bwrap sets HOME and TMPDIR inside.
|
||||
export const SPAWN_ENV_ALLOW = Object.freeze(["PATH", "LANG", "LC_ALL", "TZ", "TERM"]);
|
||||
// Passed only as an absolute path: the pinned harness binary (packages/seat/src/session.mjs ENV_ALLOW).
|
||||
export const SPAWN_PATH_ALLOW = Object.freeze(["MOSAIC_CLAUDE_BIN"]);
|
||||
// The names the runner sets for every harness (runner.mjs adapterEnv); the host's own names never replace them.
|
||||
const RUNNER_NAMES = new Set(["MOSAIC_SYSTEM_PROMPT_FILE", "MOSAIC_WORKSPACE", "MOSAIC_SESSION_DIR", "MOSAIC_POLICY_FILE", "MOSAIC_TOOLS_FILE",
|
||||
"MOSAIC_TOOL_SOCKET", "MOSAIC_TURN_MARKER", "MOSAIC_PROVIDER", "MOSAIC_MODEL", "MOSAIC_MODEL_ACTIVITY_DIR", "MOSAIC_UNSLOP_LOG",
|
||||
"MOSAIC_MODEL_EVENT_MONITOR", "MOSAIC_FIRST_OUTPUT_DIR", "MOSAIC_REQUEST"]);
|
||||
// What a key the agent sends becomes in the transcript.
|
||||
export const KEY_REMOVED = "[key removed]";
|
||||
// The marker, under host/ (never mounted), that the next start gets a fresh session/.
|
||||
const FRESH_MARKER = "fresh-session";
|
||||
// #1873 harness-matrix: the names a harness may take the Triage key through. claude-code takes it as
|
||||
// ANTHROPIC_AUTH_TOKEN; pi as ANTHROPIC_API_KEY, which its models.json names. A route's env holds
|
||||
// exactly one of them.
|
||||
export const ROUTE_KEY_NAMES = Object.freeze(["ANTHROPIC_AUTH_TOKEN", "ANTHROPIC_API_KEY"]);
|
||||
export const ROUTE_ENV_ALLOW = Object.freeze(["ANTHROPIC_BASE_URL", ...ROUTE_KEY_NAMES]);
|
||||
// The one key name a route env holds and its value, or null when it holds none or more than one.
|
||||
export const routeKey = (routeEnv) => {
|
||||
const held = ROUTE_KEY_NAMES.filter((k) => routeEnv?.[k] !== undefined);
|
||||
if (held.length !== 1 || typeof routeEnv[held[0]] !== "string" || !routeEnv[held[0]]) return null;
|
||||
return { name: held[0], value: routeEnv[held[0]] };
|
||||
};
|
||||
const ENV_NAME = /^[A-Z][A-Z0-9_]{0,63}$/;
|
||||
const ENV_VALUE_MAX = 4096;
|
||||
const HARNESS_ENV_MAX = 64;
|
||||
// Refused in harnessEnv even when the harness module declares them: the runner's own MOSAIC_*
|
||||
// values, loader, libc and interpreter hooks, the shell's start files, a tool's config directory
|
||||
// and the founder's, an askpass program, a CA bundle or a base URL, Lua's start code, curl's config
|
||||
// home and a TLS key log. A provider name (every ANTHROPIC_* among them) goes through route env or nowhere.
|
||||
const REFUSED_PREFIX = /^(MOSAIC_|NODE_|LD_|DYLD_|PYTHON|BASH_|GIT_|MALLOC_|NPM_CONFIG_|XDG_)/;
|
||||
const REFUSED_SUFFIX = /_(DIR|CONF|CONFIG|ASKPASS|CA_BUNDLE|BASE_URL)$/;
|
||||
const REFUSED_NAMES = new Set([
|
||||
"PATH", "HOME", "TMPDIR", "BASH_ENV", "ENV", "SSH_AUTH_SOCK", "SHELL", "SHELLOPTS", "BASHOPTS", "PS4", "IFS", "CDPATH",
|
||||
"GLOBIGNORE", "PROMPT_COMMAND", "ZDOTDIR", "PERL5OPT", "PERL5LIB", "RUBYOPT", "RUBYLIB", "PERLLIB", "LESSOPEN", "LESSCLOSE", "HOSTALIASES", "HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY",
|
||||
"GCONV_PATH", "GLIBC_TUNABLES", "LOCPATH", "OPENSSL_CONF", "OPENSSL_ENGINES", "OPENSSL_MODULES", "SSL_CERT_FILE", "SSL_CERT_DIR",
|
||||
"JAVA_TOOL_OPTIONS", "_JAVA_OPTIONS", "JDK_JAVA_OPTIONS", "LUA_INIT", "CURL_HOME", "SSLKEYLOGFILE",
|
||||
...FOUNDER_ENV,
|
||||
]);
|
||||
export const refusedHarnessName = (k) => typeof k !== "string" || !ENV_NAME.test(k) || REFUSED_PREFIX.test(k) || REFUSED_SUFFIX.test(k) || REFUSED_NAMES.has(k) || isProviderVariable(k);
|
||||
// No control character of any kind (C0, DEL, C1) and no line or paragraph separator.
|
||||
const cleanValue = (v) => typeof v === "string" && v.length <= ENV_VALUE_MAX && !/[\u0000-\u001f\u007f-\u009f\u2028\u2029]/.test(v);
|
||||
const plainObject = (o) => o !== null && typeof o === "object" && !Array.isArray(o) && [Object.prototype, null].includes(Object.getPrototypeOf(o));
|
||||
// The proxy is on this machine: http, 127.0.0.1, a port, nothing after it.
|
||||
const LOOPBACK_URL = /^http:\/\/127\.0\.0\.1:([1-9][0-9]{0,4})$/;
|
||||
const HARNESS_NAME = /^[a-z][a-z0-9-]{0,31}$/;
|
||||
|
||||
// The harness module a route names, from the repository's own tree; null when there is none.
|
||||
async function repoHarnessModule(name) {
|
||||
if (!HARNESS_NAME.test(name)) return null;
|
||||
const file = join(REPO, "packages", "plugins", "harness", name, "index.mjs");
|
||||
try { if (!lstatSync(file).isFile()) return null; } catch { return null; }
|
||||
return import(pathToFileURL(file).href);
|
||||
}
|
||||
|
||||
export function triageEnv(source, routeEnv = {}, { node = process.execPath } = {}) {
|
||||
// #1873 harness-matrix: the repository's node_modules/.bin comes first, as a bus session's PATH has
|
||||
// it (session.mjs sessionEnv): a harness whose adapter runs its binary from there finds it. It is in
|
||||
// the sandbox only when the harness's own mounts bind node_modules.
|
||||
const env = { PATH: `${join(REPO, "node_modules", ".bin")}:${dirname(node)}:/usr/bin:/bin` };
|
||||
for (const k of SPAWN_ENV_ALLOW) if (k !== "PATH" && typeof source?.[k] === "string" && source[k].length <= 256) env[k] = source[k];
|
||||
for (const k of SPAWN_PATH_ALLOW) if (typeof source?.[k] === "string" && source[k].startsWith("/") && source[k].length <= 256 && cleanValue(source[k])) env[k] = source[k];
|
||||
for (const k of ROUTE_ENV_ALLOW) if (typeof routeEnv?.[k] === "string" && routeEnv[k].length <= 4096) env[k] = routeEnv[k];
|
||||
return env;
|
||||
}
|
||||
|
||||
// The relay's fixed answers. None of them reaches a bus or changes anything.
|
||||
const LIFECYCLE = Object.freeze({
|
||||
"role.claim": () => ({ library: false }),
|
||||
state: () => ({ launch: { state: "on", at: null, event: null }, tasks: { open: [], total: 0, cap: 0 } }),
|
||||
"processing.status": () => ({ halted: false, invalid: false }),
|
||||
"usage.begin": () => ({}),
|
||||
"usage.end": () => ({}),
|
||||
"role.release": () => ({}),
|
||||
});
|
||||
|
||||
export class TriageError extends Error {
|
||||
constructor(code, message) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
|
||||
function realDir(path, { create = true } = {}) {
|
||||
let st;
|
||||
try {
|
||||
st = lstatSync(path);
|
||||
} catch (e) {
|
||||
if (e.code !== "ENOENT" || !create) throw new TriageError("triage-dir-unsafe", "a Triage directory is missing");
|
||||
mkdirSync(path, { mode: 0o700 });
|
||||
st = lstatSync(path);
|
||||
}
|
||||
if (st.isSymbolicLink() || !st.isDirectory()) throw new TriageError("triage-dir-unsafe", "a Triage directory is not a real directory");
|
||||
if (typeof process.getuid === "function" && st.uid !== process.getuid()) throw new TriageError("triage-dir-unsafe", "a Triage directory is not owned by the Console's user");
|
||||
chmodSync(path, 0o700);
|
||||
}
|
||||
|
||||
// A directory inside agent/, which the session can write: removed whole (rm never follows a link)
|
||||
// and made again, so a link the session left there can't steer a bind or a host write.
|
||||
function freshDir(path) {
|
||||
rmSync(path, { recursive: true, force: true });
|
||||
mkdirSync(path, { mode: 0o700 });
|
||||
}
|
||||
|
||||
export function triagePaths(dataRoot) {
|
||||
const root = join(dataRoot, "triage");
|
||||
return { root, health: join(root, "health"), agent: join(root, "agent"), host: join(root, "host"), transcript: join(root, "host", "transcript.jsonl"), log: join(root, "host", "runner.log") };
|
||||
}
|
||||
|
||||
// The prompt is the role's contract and the host's own lines: where the snapshot is and how a
|
||||
// card is written. It names the snapshot inside the session, never a host path of the data root.
|
||||
export function hostPrompt(contract, { snapshotPath, steps }) {
|
||||
return [
|
||||
contract.trimEnd(),
|
||||
"",
|
||||
"## This host",
|
||||
"",
|
||||
`- The health snapshot is the JSON file ${snapshotPath}. Read it at the start of each turn; it is the only health you can see. A field that says "not read" was not read; say so, never guess it.`,
|
||||
"- Each message arrives as one turn and your final answer goes back to the person. send_message reaches only \"human\"; this host refuses every other call.",
|
||||
"- Propose each fix as a card in one fenced block marked cards, holding a JSON array of objects with exactly two keys, step and org, for example:",
|
||||
"```cards",
|
||||
'[{"step": "bus-start", "org": "alpha"}]',
|
||||
"```",
|
||||
`- step is one of: ${steps.join(", ")}. org is an organization id from the snapshot. The Console writes the command for each card; never write a command yourself. A card with anything else is dropped.`,
|
||||
"",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function sameCap(a, b) {
|
||||
if (typeof a !== "string" || !CAP.test(a)) return false;
|
||||
return timingSafeEqual(Buffer.from(a), Buffer.from(b));
|
||||
}
|
||||
|
||||
// The relay server. `onSend({ body, in_reply_to, result })` keeps a human-bound send, `result` true
|
||||
// only for class RESULT, the class the runner gives its answer to a turn; `take()` returns the
|
||||
// queued owner messages; `read(id)` drops one that was answered.
|
||||
// #1873 triage-cap: `usage(phase, turn)` (a promise) counts the turn; the relay answers usage.begin
|
||||
// and usage.end only once it settles. `stopped(verb)` is asked before state and message.receive;
|
||||
// true answers "not-holder", which ends the runner's loop after the turn in flight.
|
||||
export function createRelay({ socket, cap, take, read, onSend, usage = null, stopped = () => false, log = () => {} }) {
|
||||
const answer = (conn, value) => { try { conn.end(JSON.stringify(value) + "\n"); } catch {} };
|
||||
const refuse = (conn) => answer(conn, { ok: false, error: RELAY_REFUSED, reason: "the Triage host answers only a message to the person" });
|
||||
const server = createServer((conn) => {
|
||||
let buf = "";
|
||||
let done = false;
|
||||
conn.setTimeout(30_000, () => conn.destroy());
|
||||
conn.on("error", () => {});
|
||||
conn.on("data", (chunk) => {
|
||||
if (done) return;
|
||||
buf += chunk.toString("utf8");
|
||||
if (buf.length > LINE_MAX) { done = true; return refuse(conn); }
|
||||
const nl = buf.indexOf("\n");
|
||||
if (nl < 0) return;
|
||||
done = true;
|
||||
let req;
|
||||
try { req = JSON.parse(buf.slice(0, nl)); } catch { return refuse(conn); }
|
||||
if (!req || typeof req !== "object" || !sameCap(req.cap, cap) || typeof req.verb !== "string") return refuse(conn);
|
||||
const args = req.args && typeof req.args === "object" && !Array.isArray(req.args) ? req.args : {};
|
||||
const verb = req.verb;
|
||||
try {
|
||||
if (usage && (verb === "usage.begin" || verb === "usage.end")) {
|
||||
return void Promise.resolve()
|
||||
.then(() => usage(verb === "usage.begin" ? "begin" : "end", args.turn))
|
||||
.catch((e) => log(`triage relay: ${verb} not counted: ${e?.code ?? "error"}`))
|
||||
.then(() => answer(conn, { ok: true, result: {} }));
|
||||
}
|
||||
if ((verb === "state" || verb === "message.receive") && stopped(verb)) return answer(conn, { ok: false, error: "not-holder" });
|
||||
if (Object.hasOwn(LIFECYCLE, verb)) return answer(conn, { ok: true, result: LIFECYCLE[verb]() });
|
||||
if (verb === "message.receive") return answer(conn, { ok: true, result: take() });
|
||||
if (verb === "message.read") { read(args.id); return answer(conn, { ok: true, result: {} }); }
|
||||
if (verb === "message.send" && args.to === "human" && typeof args.body === "string" && args.body.length <= 32768) {
|
||||
// Only an id the host issued is kept. The model writes this field, and the transcript serves it.
|
||||
onSend({ body: args.body, in_reply_to: typeof args.in_reply_to === "string" && OWNER_ID.test(args.in_reply_to) ? args.in_reply_to : null, result: args.class === "RESULT" });
|
||||
return answer(conn, { ok: true, result: { id: `triage-${randomBytes(6).toString("hex")}` } });
|
||||
}
|
||||
} catch (e) {
|
||||
log(`triage relay: ${verb} failed: ${e.code ?? "error"}`);
|
||||
}
|
||||
log(`triage relay: refused ${/^[a-z][a-z0-9._-]{0,63}$/.test(verb) ? verb : "a verb"}`);
|
||||
return refuse(conn);
|
||||
});
|
||||
});
|
||||
return new Promise((resolve, reject) => {
|
||||
server.once("error", reject);
|
||||
server.listen(socket, () => {
|
||||
server.off("error", reject);
|
||||
chmodSync(socket, 0o600);
|
||||
resolve(server);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// createTriageHost({ dataRoot, route, readSnapshot, env, node, bwrap, now, devOwner, log })
|
||||
// route { admit() -> { ok: true, adapter, harness, harnessEnv, harnessMounts, turnEnd, model,
|
||||
// provider, env } | { ok: false, code, until? } } or null for no route
|
||||
// readSnapshot async () -> the snapshot (triage-snapshot.mjs composeSnapshot), written to health/
|
||||
// pollInterval the runner's receive interval in ms, for tests; the runner's default otherwise
|
||||
// proxyPort the port ANTHROPIC_BASE_URL must name, or null for any port; or a function that
|
||||
// answers it, called at each admit (null from it refuses: no proxy is configured)
|
||||
// tokenPattern a RegExp the whole ANTHROPIC_AUTH_TOKEN must match, or null for no shape check
|
||||
// harnessModule async (name) -> the harness module (envNames, installRoots) or null; the
|
||||
// repository's packages/plugins/harness/<name> by default
|
||||
// home HOME, which no mount may reach outside the stack's own code; the Console user's by default
|
||||
// usageSource { readTurn({ harness, root }) } the transcript usage-source; the repository's own by default
|
||||
export function createTriageHost({ dataRoot, route = null, readSnapshot = async () => null, env = process.env, node = process.execPath, bwrap = BWRAP, now = () => new Date(), devOwner = false, log = () => {}, roleDir = join(REPO, "roles"), readOnly = [], pollInterval = null, proxyPort = null, tokenPattern = null, harnessModule = repoHarnessModule, home = homedir(), usageSource = { readTurn } }) {
|
||||
if (typeof dataRoot !== "string" || !dataRoot.startsWith("/")) throw new TriageError("triage-no-data-root", "no data root");
|
||||
const validPort = (p) => Number.isInteger(p) && p > 0 && p <= 65535;
|
||||
if (proxyPort !== null && typeof proxyPort !== "function" && !validPort(proxyPort)) throw new TypeError("proxyPort must be a port number, null or a function");
|
||||
if (tokenPattern !== null && !(tokenPattern instanceof RegExp)) throw new TypeError("tokenPattern must be a RegExp or null");
|
||||
if (typeof harnessModule !== "function") throw new TypeError("harnessModule must be a function");
|
||||
if (typeof usageSource?.readTurn !== "function") throw new TypeError("usageSource must have readTurn");
|
||||
const tokenShape = tokenPattern && new RegExp(`^(?:${tokenPattern.source})$`, tokenPattern.flags.replace(/[gmy]/g, ""));
|
||||
const tokenAnywhere = tokenPattern && new RegExp(tokenPattern.source, tokenPattern.flags.replace(/[gmy]/g, "") + "g");
|
||||
// What no mount source may lie in or around, besides the run directory and the fixed list. HOME
|
||||
// does not refuse the stack's own code when the stack runs from under it (a copy or a worktree).
|
||||
const protectedPaths = [dataRoot, systemConfigDir(env), home];
|
||||
const codeRoot = REPO;
|
||||
const paths = triagePaths(dataRoot);
|
||||
const role = JSON.parse(readFileSync(join(roleDir, "triage.json"), "utf8"));
|
||||
const contract = readFileSync(join(roleDir, role.contract), "utf8");
|
||||
let last = null; // the last snapshot written
|
||||
let lastAt = null; // when it was written, in ms
|
||||
let child = null;
|
||||
let relay = null;
|
||||
let starting = null;
|
||||
let lastExit = null;
|
||||
let cap = null;
|
||||
let overCapUntil = null; // the route's over-cap answer, held until its time passes
|
||||
const queue = [];
|
||||
let usage = null; // this run's counting: { run, root, harness, baselines, ended, inTurn }
|
||||
let usageChain = Promise.resolve(); // usage calls and exit records, one at a time
|
||||
let usageLost = false; // a turn's line could not be written: the count is short, so a stored cap refuses
|
||||
// The last session ended on unknown usage: the next start gets a new session/. Kept as a file under
|
||||
// host/ too, so a Console restart between the end and the next message keeps it.
|
||||
let freshSession = false;
|
||||
const badSeen = new Set();
|
||||
|
||||
const ensureDirs = () => {
|
||||
mkdirSync(dataRoot, { recursive: true, mode: 0o700 });
|
||||
for (const d of [paths.root, paths.health, paths.agent, paths.host]) realDir(d);
|
||||
};
|
||||
|
||||
const append = (entry) => {
|
||||
ensureDirs();
|
||||
const fd = openSync(paths.transcript, "a", 0o600);
|
||||
try { appendFileSync(fd, JSON.stringify(entry) + "\n"); } finally { closeSync(fd); }
|
||||
chmodSync(paths.transcript, 0o600);
|
||||
};
|
||||
|
||||
const transcript = () => {
|
||||
let text = "";
|
||||
try {
|
||||
const st = lstatSync(paths.transcript);
|
||||
if (!st.isFile()) return [];
|
||||
text = readFileSync(paths.transcript, "utf8");
|
||||
} catch { return []; }
|
||||
const out = [];
|
||||
for (const line of text.split("\n")) {
|
||||
if (!line) continue;
|
||||
try { out.push(JSON.parse(line)); } catch {}
|
||||
}
|
||||
return out.slice(-TRANSCRIPT_KEEP);
|
||||
};
|
||||
|
||||
const orgIds = () => new Set(Array.isArray(last?.organizations?.list) ? last.organizations.list.map((o) => o.id) : []);
|
||||
|
||||
// One refresh at a time: a read and a message that arrive together share it.
|
||||
let refreshing = null;
|
||||
const refresh = () => refreshing ??= (async () => {
|
||||
ensureDirs();
|
||||
let snap = null;
|
||||
try { snap = await readSnapshot(); } catch { snap = null; }
|
||||
if (snap && typeof snap === "object") {
|
||||
writeSnapshot(paths.health, snap);
|
||||
last = snap;
|
||||
lastAt = now().getTime();
|
||||
}
|
||||
return last;
|
||||
})().finally(() => { refreshing = null; });
|
||||
|
||||
// #1873 triage-wiring: the module's bundle files and its MOSAIC_ names, which the host sets itself.
|
||||
const hostHarnessEnv = (mod, { admitted, bundleDir, files, policy, tools, toolSocket }) => {
|
||||
if (typeof mod?.bundle !== "function" || typeof mod?.env !== "function") return { env: {}, files: [] };
|
||||
// #1873 harness-matrix: the module gets the real proxy and the route's transport, as a bus
|
||||
// session's bundle does, so a harness that takes the proxy through its own files (pi's
|
||||
// models.json) writes them here. Claude Code's bundle() takes no proxy. No file it writes, in
|
||||
// bundle/ or outside it, may hold the key or anything shaped like it.
|
||||
const routed = routeKey(admitted.env);
|
||||
const key = routed?.value;
|
||||
if (!key) throw new Error("no key");
|
||||
const holdsKey = (v) => typeof v === "string" && (v.includes(key) || Boolean(tokenAnywhere && v.match(tokenAnywhere)));
|
||||
// A file's text, and when it parses as JSON every key and string in it, decoded: an escaped key
|
||||
// or URL ("\u0061...", "http:\/\/...") is seen as the harness that parses the file sees it. A
|
||||
// harness can parse more loosely than JSON.parse (pi takes a byte order mark, comments and
|
||||
// trailing commas), so a file that does not parse may hold no backslash: its escapes would be
|
||||
// decoded by the harness and not here.
|
||||
const decoded = (text) => {
|
||||
const out = [text];
|
||||
let doc;
|
||||
try { doc = JSON.parse(text); } catch {
|
||||
if (text.includes("\\")) throw new Error("bundle file holds an escape it does not parse");
|
||||
return out;
|
||||
}
|
||||
const stack = [doc];
|
||||
while (stack.length) {
|
||||
const v = stack.pop();
|
||||
if (typeof v === "string") out.push(v);
|
||||
else if (v && typeof v === "object") for (const [k, x] of Object.entries(v)) { out.push(k); stack.push(x); }
|
||||
}
|
||||
return out;
|
||||
};
|
||||
const write = (name, text) => {
|
||||
if (typeof name !== "string" || !/^[a-z][a-z0-9.-]{0,63}$/.test(name) || typeof text !== "string") throw new Error("bundle file refused");
|
||||
if (decoded(text).some(holdsKey)) throw new Error("bundle file holds the key");
|
||||
writeFileSync(join(bundleDir, name), text, { mode: 0o600, flag: "wx" });
|
||||
return [name, createHash("sha256").update(text).digest("hex")];
|
||||
};
|
||||
const quote = (p) => {
|
||||
if (typeof p !== "string" || p.includes("'")) throw new Error("path can't hold a single quote");
|
||||
return `'${p}'`;
|
||||
};
|
||||
let levels = [];
|
||||
try { levels = typeof mod.levels === "function" ? mod.levels() : []; } catch { levels = []; }
|
||||
const thinking = Array.isArray(levels) && levels.includes("low") ? "low" : Array.isArray(levels) && typeof levels[0] === "string" ? levels[0] : "";
|
||||
const proxy = { baseUrl: admitted.env.ANTHROPIC_BASE_URL, key };
|
||||
const session = { model: admitted.model, provider: admitted.provider ?? "", thinking, toolSocket, ...(admitted.transport ? { proxyTransport: { ...admitted.transport } } : {}) };
|
||||
const models = { triage: { provider: admitted.provider ?? "", model: admitted.model } };
|
||||
const transports = admitted.transport ? { triage: { ...admitted.transport } } : {};
|
||||
// The rest of the run directory (session/, kept across starts) is the module's to leave alone:
|
||||
// what it holds before bundle() must be what it holds after env(). ctime is in the record
|
||||
// because no user process can set it back, as it can mtime.
|
||||
const scanned = new Set(["bundle", "home", "work", "tmp"]);
|
||||
const kept = () => {
|
||||
const seen = new Map();
|
||||
for (const name of readdirSync(paths.agent, { recursive: true })) {
|
||||
const rel = String(name);
|
||||
if (scanned.has(rel.split("/")[0])) continue;
|
||||
const st = lstatSync(join(paths.agent, rel));
|
||||
seen.set(rel, `${st.mode}:${st.ino}:${st.size}:${st.mtimeMs}:${st.ctimeMs}:${st.nlink}`);
|
||||
}
|
||||
return seen;
|
||||
};
|
||||
const before = kept();
|
||||
// The host's own bundle files (prompt, policy, tools) must be as the host wrote them.
|
||||
const hostFiles = new Map(Object.values(files).map((f) => [f, readFileSync(f, "utf8")]));
|
||||
const own = mod.bundle({ dir: bundleDir, files: { ...files }, session: { ...session }, write, quote, models, proxy: { ...proxy }, transports });
|
||||
const ownFiles = plainObject(own?.files) ? own.files : {};
|
||||
const given = mod.env({ files: { ...ownFiles }, policy, tools, session: { ...session }, models, proxy: { ...proxy } });
|
||||
if (!plainObject(given)) throw new Error("env refused");
|
||||
const after = kept();
|
||||
const changed = [...after].filter(([k, v]) => before.get(k) !== v).map(([k]) => k);
|
||||
if (changed.length || after.size !== before.size) {
|
||||
// What the module added or changed goes, and the next start takes a fresh session.
|
||||
for (const k of changed) rmSync(join(paths.agent, k), { recursive: true, force: true });
|
||||
freshSession = true;
|
||||
throw new Error("bundle changed the run directory");
|
||||
}
|
||||
// Each host bundle file is still there, a regular file with one link and the same bytes,
|
||||
// whether or not the scan below reaches it.
|
||||
for (const [f, text] of hostFiles) {
|
||||
const st = lstatSync(f, { throwIfNoEntry: false });
|
||||
if (!st?.isFile() || st.nlink !== 1 || readFileSync(f, "utf8") !== text) throw new Error("bundle changed a host file");
|
||||
}
|
||||
// A module can write by its own means too, and name only some of its files. writeBundle makes
|
||||
// bundle/, home/, work/ and tmp/ fresh just before, so every file in them came from this start:
|
||||
// each is read again, and each must be a regular file with no key in it, raw or decoded, and,
|
||||
// past the host's own bundle files, no URL but the proxy's (its root or /v1): a harness reads
|
||||
// files from its HOME and workspace that it does not declare (pi's ~/.pi/agent/models.json).
|
||||
// No string in them may be a pi command value: pi runs a config value that starts with "!" as a
|
||||
// shell command, so no decoded string starts with "!" and no text holds a string that does ('"!').
|
||||
// These fences see what the module left when env() returns, in the run directory only. They do not stop a module that
|
||||
// hides the key (base64, split across files) or writes after: harness modules are reviewed
|
||||
// repo code, and the fences catch their mistakes, not their intent.
|
||||
const proxyUrl = (u) => u === proxy.baseUrl || u === proxy.baseUrl + "/v1";
|
||||
const SPECIAL = new Set(["http:", "https:", "ws:", "wss:", "ftp:", "file:"]);
|
||||
// Every URL a string names, as written ("http://", "http:host", "http:\\host") or as a URL
|
||||
// parser reads the whole string (it drops tabs and newlines anywhere and controls at the ends).
|
||||
const urlsIn = (text) => {
|
||||
const out = [...text.matchAll(/[a-z][a-z0-9+.-]*:\/\/[^\s"'<>\\]*/gi), ...text.matchAll(/\b(?:https?|wss?|ftp|file):[^\s"'<>]*/gi)].map(([u]) => u);
|
||||
if (URL.canParse(text) && SPECIAL.has(new URL(text).protocol)) out.push(text);
|
||||
return out;
|
||||
};
|
||||
const strings = new Map();
|
||||
for (const d of ["bundle", "home", "work", "tmp"]) {
|
||||
const root = join(paths.agent, d);
|
||||
for (const name of readdirSync(root, { recursive: true })) {
|
||||
const f = join(root, String(name));
|
||||
const st = lstatSync(f);
|
||||
if (st.isDirectory()) continue;
|
||||
if (!st.isFile()) throw new Error("bundle file is not a regular file");
|
||||
const text = readFileSync(f, "utf8");
|
||||
if (hostFiles.has(f)) { if (text !== hostFiles.get(f)) throw new Error("bundle changed a host file"); continue; }
|
||||
const all = decoded(text);
|
||||
if (all.some(holdsKey)) throw new Error("bundle file holds the key");
|
||||
if (all.some((t) => urlsIn(t).some((u) => !proxyUrl(u)))) throw new Error("bundle file names another URL");
|
||||
if (all.some((t) => t.startsWith("!") || t.includes('"!'))) throw new Error("bundle file holds a command value");
|
||||
strings.set(f, all);
|
||||
}
|
||||
}
|
||||
// The key goes only into the one key name the route answered, as the key itself; the URL, when
|
||||
// env() names one, is the route's.
|
||||
for (const [k, v] of Object.entries(given)) {
|
||||
if (k === routed.name) continue;
|
||||
if (holdsKey(v)) throw new Error("env holds the key");
|
||||
if (k === "ANTHROPIC_BASE_URL" && v !== proxy.baseUrl) throw new Error("env names another proxy");
|
||||
}
|
||||
if (given[routed.name] !== key) throw new Error("env does not take the key as it is");
|
||||
// Files the module wrote outside bundle/ (pi's models.json): each a regular file the session's
|
||||
// HOME holds, read above, with no URL but the proxy's (its root or /v1), raw or decoded, bound read-only
|
||||
// over the run directory. A harness whose env() names no URL must take it from one of them.
|
||||
const homeDir = join(paths.agent, "home");
|
||||
const harnessFiles = [];
|
||||
let urlInFile = false;
|
||||
for (const f of Object.values(ownFiles)) {
|
||||
if (typeof f !== "string" || f.startsWith(bundleDir + "/")) continue;
|
||||
if (!f.startsWith(homeDir + "/") || f.split("/").includes("..")) throw new Error("bundle file outside the session's HOME");
|
||||
const st = lstatSync(f);
|
||||
if (!st.isFile() || st.nlink !== 1) throw new Error("bundle file is not a regular file");
|
||||
const all = strings.get(f);
|
||||
if (!all) throw new Error("bundle file was not read");
|
||||
if (all.some((t) => urlsIn(t).length > 0)) urlInFile = true;
|
||||
harnessFiles.push(f);
|
||||
}
|
||||
if (given.ANTHROPIC_BASE_URL === undefined && !urlInFile) throw new Error("harness takes no proxy URL");
|
||||
// A name the route's harnessEnv holds that env() also answers must have env()'s value.
|
||||
for (const [k, v] of Object.entries(admitted.harnessEnv ?? {})) if (Object.hasOwn(given, k) && given[k] !== v) throw new Error("harness env differs");
|
||||
const out = {};
|
||||
for (const [k, v] of Object.entries(given)) {
|
||||
if (!k.startsWith("MOSAIC_") || !mod.envNames.includes(k) || !ENV_NAME.test(k) || RUNNER_NAMES.has(k) || !cleanValue(v)) continue;
|
||||
out[k] = v;
|
||||
}
|
||||
return { env: out, files: harnessFiles };
|
||||
};
|
||||
|
||||
const writeBundle = (admitted, mod) => {
|
||||
const a = paths.agent;
|
||||
for (const d of ["bundle", "tmp", "work", "home"]) freshDir(join(a, d));
|
||||
mkdirSync(join(a, "work", "health"), { mode: 0o700 });
|
||||
// turns/ goes too: the runner numbers turns from 1 and opens each turn file exclusively, so a
|
||||
// turns/ left by the last run stops the next one at its first turn.
|
||||
for (const f of ["session.json", "relay.sock", "tools.sock", "turn.done", "turns"]) rmSync(join(a, f), { recursive: true, force: true });
|
||||
// The session's history stays across starts, unless the session put something other than a
|
||||
// real directory there, or the last session ended because its usage could not be read: then the
|
||||
// next one starts with a fresh session and transcript, so its turns count from a known baseline.
|
||||
const marker = join(paths.host, FRESH_MARKER);
|
||||
if (freshSession || existsSync(marker)) { freshDir(join(a, "session")); freshSession = false; rmSync(marker, { force: true }); }
|
||||
else { try { realDir(join(a, "session")); } catch { freshDir(join(a, "session")); } }
|
||||
const resolved = { limits: { authority: role.authority, tools: role.tools }, definition: "triage", pluginTools: [] };
|
||||
const tools = typedTools({ resolved }).filter((t) => t.name === "send_message");
|
||||
const workspace = join(a, "work");
|
||||
const policy = { harness: admitted.harness, workspace, tools: builtinTools(role.tools), typed: tools.map((t) => t.name), network: role.network, needs: [], credentials: [] };
|
||||
const bundle = { prompt: join(a, "bundle", "prompt.md"), policy: join(a, "bundle", "policy.json"), tools: join(a, "bundle", "tools.json") };
|
||||
const steps = ["bus-start", "bus-restart", "bus-recover", "bus-stop", "org-config-validate"];
|
||||
writeFileSync(bundle.prompt, hostPrompt(contract, { snapshotPath: join(workspace, "health", SNAPSHOT_FILE), steps }), { mode: 0o600, flag: "wx" });
|
||||
writeFileSync(bundle.policy, JSON.stringify(policy, null, 2) + "\n", { mode: 0o600, flag: "wx" });
|
||||
writeFileSync(bundle.tools, JSON.stringify(tools, null, 2) + "\n", { mode: 0o600, flag: "wx" });
|
||||
let hostEnv, harnessFiles;
|
||||
try { ({ env: hostEnv, files: harnessFiles } = hostHarnessEnv(mod, { admitted, bundleDir: join(a, "bundle"), files: bundle, policy, tools, toolSocket: join(a, "tools.sock") })); }
|
||||
catch {
|
||||
log("triage host: route refused: harness-bundle");
|
||||
for (const d of ["bundle", "home", "work", "tmp"]) rmSync(join(a, d), { recursive: true, force: true });
|
||||
throw new TriageError("triage-route-invalid", "the model route's answer is not usable");
|
||||
}
|
||||
const run = `triage-${now().toISOString().replace(/[^0-9]/g, "").slice(0, 14)}`;
|
||||
const session = {
|
||||
bundle, adapter: admitted.adapter, harness: admitted.harness, harnessEnv: { ...admitted.harnessEnv, ...hostEnv }, turnEnd: admitted.turnEnd,
|
||||
workspace, sessionDir: join(a, "session"), toolSocket: join(a, "tools.sock"), turnMarker: join(a, "turn.done"),
|
||||
brokerSocket: join(a, "relay.sock"), business: "system", instance: "triage", run,
|
||||
model: admitted.model, provider: admitted.provider ?? "",
|
||||
...(Number.isInteger(pollInterval) && pollInterval >= 100 ? { pollInterval } : {}),
|
||||
};
|
||||
writeFileSync(join(a, "session.json"), JSON.stringify(session, null, 2) + "\n", { mode: 0o600, flag: "wx" });
|
||||
return { run, harnessFiles };
|
||||
};
|
||||
|
||||
// The route's answer, checked. It returns a copy taken once, so a getter or a later change to
|
||||
// the route's object cannot hand the bundle or the binds anything the checks did not see, and
|
||||
// the module's install roots, for hostedBinds. The log names the fence, never a value.
|
||||
const checkAdmitted = async (answer) => {
|
||||
const refuse = (fence) => {
|
||||
log(`triage host: route refused: ${fence}`);
|
||||
throw new TriageError("triage-route-invalid", "the model route's answer is not usable");
|
||||
};
|
||||
let a;
|
||||
try { a = structuredClone(answer); } catch { refuse("shape"); }
|
||||
if (!plainObject(a) || typeof a.adapter !== "string" || !["marker", "exit"].includes(a.turnEnd)
|
||||
|| typeof a.model !== "string" || !a.model || typeof a.harness !== "string" || !HARNESS_NAME.test(a.harness)
|
||||
|| (a.provider !== undefined && typeof a.provider !== "string")) refuse("shape");
|
||||
let mod = null;
|
||||
try { mod = await harnessModule(a.harness); } catch { mod = null; }
|
||||
const declared = mod?.envNames;
|
||||
if (!Array.isArray(declared) || typeof mod.installRoots !== "function") refuse("module");
|
||||
const he = a.harnessEnv;
|
||||
if (!plainObject(he) || Object.keys(he).length > HARNESS_ENV_MAX) refuse("harness-env");
|
||||
for (const [k, v] of Object.entries(he)) if (refusedHarnessName(k) || !declared.includes(k) || !cleanValue(v)) refuse("harness-env");
|
||||
const re = a.env ?? {};
|
||||
if (!plainObject(re) || Object.entries(re).some(([k, v]) => !ROUTE_ENV_ALLOW.includes(k) || !cleanValue(v))) refuse("route-env");
|
||||
// #1873 harness-matrix: exactly one key name, and no harnessEnv value holds the key.
|
||||
const routed = routeKey(re);
|
||||
if (!routed) refuse("route-env");
|
||||
for (const v of Object.values(he)) if (v.includes(routed.value) || (tokenAnywhere && v.match(tokenAnywhere))) refuse("harness-env");
|
||||
const url = LOOPBACK_URL.exec(re.ANTHROPIC_BASE_URL ?? "");
|
||||
let port = proxyPort;
|
||||
if (typeof proxyPort === "function") {
|
||||
try { port = await proxyPort(); } catch { port = undefined; }
|
||||
if (!validPort(port)) refuse("route-env");
|
||||
}
|
||||
if (!url || Number(url[1]) > 65535 || (port !== null && Number(url[1]) !== port)) refuse("route-env");
|
||||
if (tokenShape && !tokenShape.test(routed.value)) refuse("route-env");
|
||||
// The route's transport, when it gives one, held to the bus's own transport check. This checks its
|
||||
// shape only: the harness rule ran at the route's admit, through the provider module's route().
|
||||
if (a.transport !== undefined) {
|
||||
try { checkedTransport({ provider: a.provider ?? "", model: a.model }, a.harness, () => ({ ...(plainObject(a.transport) ? a.transport : {}), provider: a.provider ?? "", model: a.model })); }
|
||||
catch { refuse("transport"); }
|
||||
}
|
||||
let installRoots;
|
||||
try { installRoots = structuredClone(mod.installRoots({ env })); } catch { refuse("module"); }
|
||||
try {
|
||||
checkHostedRoute({ adapter: a.adapter, harnessMounts: a.harnessMounts ?? [], installRoots, protectedPaths, codeRoot, codeHolder: home, runDir: paths.agent, node });
|
||||
} catch (e) {
|
||||
if (e?.code === "hosted-binds-refused") refuse(e.fence);
|
||||
throw e;
|
||||
}
|
||||
// Where the harness keeps its session files, as the bus binds it (the module's transcript()).
|
||||
// Only a path inside the run directory is read; anything else reads as no transcript.
|
||||
let transcriptRoot = null;
|
||||
try {
|
||||
const r = typeof mod.transcript === "function" ? mod.transcript({ runDir: paths.agent }) : null;
|
||||
if (typeof r === "string" && r.startsWith(paths.agent + "/") && !r.split("/").includes("..")) transcriptRoot = r;
|
||||
} catch { transcriptRoot = null; }
|
||||
return { admitted: a, installRoots, transcriptRoot, mod };
|
||||
};
|
||||
|
||||
// #1873 triage-cap. A baseline or a reading of the transcript, with the bus's codes.
|
||||
const sample = async (u) => {
|
||||
if (u.root === null) return { status: "unknown", code: "no-transcript" };
|
||||
try { return snapshot(await usageSource.readTurn({ harness: u.harness, root: u.root })); }
|
||||
catch { return { status: "unknown", code: "source-failed" }; }
|
||||
};
|
||||
// A turn id the runner names, or "unnamed". The first begin for a turn wins, as on the bus.
|
||||
const turnKey = (turn) => (typeof turn === "string" && USAGE_ID.test(turn) ? turn : null);
|
||||
const usageBegin = (u, turn) => {
|
||||
const named = turnKey(turn);
|
||||
if (!named) log("triage host: usage.begin without a usable turn id");
|
||||
const k = named ?? "unnamed";
|
||||
u.inTurn = true;
|
||||
if (!u.baselines.has(k)) u.baselines.set(k, sample(u));
|
||||
};
|
||||
// One line per turn: the delta since its baseline, known or unknown.
|
||||
const finishTurn = async (u, k) => {
|
||||
const base = u.baselines.has(k) ? await u.baselines.get(k) : null;
|
||||
u.baselines.delete(k);
|
||||
const result = delta(base, await sample(u));
|
||||
const known = result.status === "known";
|
||||
if (!known) log(`triage host: turn usage unknown: ${result.code}`);
|
||||
u.unknownInRow = known ? 0 : u.unknownInRow + 1;
|
||||
const line = { at: now().toISOString(), run: u.run, turn: k, status: result.status,
|
||||
input: known ? result.input : null, output: known ? result.output : null, cacheRead: known ? result.cacheRead : null, cacheWrite: known ? result.cacheWrite : null };
|
||||
try {
|
||||
appendUsage(usagePath(dataRoot), line);
|
||||
} catch (e) {
|
||||
usageLost = true;
|
||||
log(`triage host: usage line not written: ${e?.code ?? "error"}`);
|
||||
}
|
||||
if (u.unknownInRow >= UNKNOWN_TURNS_MAX && !u.halted) endUnknown(u);
|
||||
};
|
||||
// #1873 triage-wiring: the cap cannot be kept on turns it cannot count, so the second unknown turn
|
||||
// in a row ends the session: a host line says why, the runner is told its session is gone at its
|
||||
// next state or receive, and the process group gets SIGTERM, then SIGKILL. The next message starts
|
||||
// a new run with a fresh session directory.
|
||||
const endUnknown = (u) => {
|
||||
u.halted = true;
|
||||
freshSession = true;
|
||||
try { writeFileSync(join(paths.host, FRESH_MARKER), "", { mode: 0o600 }); }
|
||||
catch { log("triage host: fresh-session marker not written"); }
|
||||
log("triage host: ending the session: two turns in a row with unknown usage");
|
||||
try { append({ at: now().toISOString(), from: "host", text: USAGE_UNKNOWN_ENDED }); }
|
||||
catch { log("triage host: transcript write failed"); }
|
||||
const c = u.child;
|
||||
if (!c || child !== c) return;
|
||||
try { process.kill(-c.pid, "SIGTERM"); } catch {}
|
||||
const t = setTimeout(() => { if (child === c) { try { process.kill(-c.pid, "SIGKILL"); } catch {} } }, 5000);
|
||||
t.unref?.();
|
||||
};
|
||||
const usageEnd = async (u, turn) => {
|
||||
u.inTurn = false;
|
||||
const named = turnKey(turn);
|
||||
if (!named) log("triage host: usage.end without a usable turn id");
|
||||
if (named && u.ended.has(named)) { log("triage host: usage.end repeated for a turn; not counted twice"); return; }
|
||||
if (named) u.ended.add(named);
|
||||
await finishTurn(u, named ?? "unnamed");
|
||||
};
|
||||
const chainUsage = (fn) => (usageChain = usageChain.then(fn).catch((e) => log(`triage host: usage failed: ${e?.code ?? "error"}`)));
|
||||
|
||||
// The stored cap's state, read from the store and the usage file each time.
|
||||
// { state: "not set" } no cap stored
|
||||
// { state: "route-unreadable" } | { state: "usage-unreadable", cap, resetsAt? } refused
|
||||
// { state: "under" | "over", cap, usageToday, resetsAt }
|
||||
const capNow = () => {
|
||||
let stored;
|
||||
try { stored = readRoute(dataRoot); } catch { return { state: "route-unreadable" }; }
|
||||
if (!stored) return { state: "not set" };
|
||||
const at = now().getTime();
|
||||
let resetsAt;
|
||||
try { resetsAt = new Date(nextReset(stored.cap, at)).toISOString(); } catch { return { state: "usage-unreadable", cap: stored.cap }; }
|
||||
if (usageLost) return { state: "usage-unreadable", cap: stored.cap, resetsAt };
|
||||
let read;
|
||||
try { read = readUsage(usagePath(dataRoot)); } catch { return { state: "usage-unreadable", cap: stored.cap, resetsAt }; }
|
||||
for (const b of read.bad) {
|
||||
const key = `${read.ino}:${b.line}`;
|
||||
if (badSeen.has(key)) continue;
|
||||
badSeen.add(key);
|
||||
log(`triage host: usage.jsonl line ${b.line} is not a usage line; it counts as an unknown turn`);
|
||||
}
|
||||
try {
|
||||
const s = capState({ cap: stored.cap, records: read.records, now: at });
|
||||
return { state: s.allow ? "under" : "over", cap: stored.cap, usageToday: s.usageToday, resetsAt: s.resetsAt };
|
||||
} catch { return { state: "usage-unreadable", cap: stored.cap, resetsAt }; }
|
||||
};
|
||||
const unsafeRoot = () => {
|
||||
let st;
|
||||
try { st = lstatSync(paths.root); } catch { return false; }
|
||||
return st.isSymbolicLink() || !st.isDirectory() || (typeof process.getuid === "function" && st.uid !== process.getuid());
|
||||
};
|
||||
const refusedByCap = (c) => c.state === "over" || c.state === "usage-unreadable" || c.state === "route-unreadable";
|
||||
// Before the runner takes messages or starts a message's turn: over the cap, every message still
|
||||
// queued gets a host line and leaves the queue, and the runner's loop ends. That holds with nothing
|
||||
// queued too: an idle session over the cap ends at its next receive, and the next message is
|
||||
// refused by admit. A state read inside a turn (a continuation) is not stopped: the turn finishes.
|
||||
const capStop = (verb) => {
|
||||
const u = usage;
|
||||
if (u?.halted) return true;
|
||||
if (verb === "state" && u?.inTurn) return false;
|
||||
const c = capNow();
|
||||
if (!refusedByCap(c)) return false;
|
||||
log(`triage host: stopping the session at ${verb}: ${c.state === "over" ? "over the daily token cap" : "usage or cap not read"}`);
|
||||
// A message whose turn began and was never answered says so; one never begun says why not.
|
||||
const begun = (id) => Boolean(u && (u.baselines.has(id) || u.ended.has(id)));
|
||||
for (const m of queue.splice(0)) {
|
||||
const text = begun(m.id) ? UNANSWERED : c.state === "over" ? overCapNotTaken(c.resetsAt) : USAGE_NOT_READ;
|
||||
try { append({ at: now().toISOString(), from: "host", in_reply_to: m.id, text }); }
|
||||
catch { log("triage host: transcript write failed"); }
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
const start = async ({ admitted, installRoots, transcriptRoot, mod }) => {
|
||||
ensureDirs();
|
||||
const { run, harnessFiles } = writeBundle(admitted, mod);
|
||||
// The key this session holds, and anything shaped like a key, never reaches the transcript.
|
||||
const key = routeKey(admitted.env)?.value ?? null;
|
||||
const scrub = (body) => {
|
||||
let t = typeof body === "string" ? body : "";
|
||||
if (key) t = t.split(key).join(KEY_REMOVED);
|
||||
return tokenAnywhere ? t.replace(tokenAnywhere, KEY_REMOVED) : t;
|
||||
};
|
||||
const u = { run, root: transcriptRoot, harness: admitted.harness, baselines: new Map(), ended: new Set(), inTurn: false, unknownInRow: 0, halted: false, child: null };
|
||||
usage = u;
|
||||
cap = randomBytes(32).toString("hex");
|
||||
relay = await createRelay({
|
||||
socket: join(paths.agent, "relay.sock"), cap,
|
||||
take: () => {
|
||||
const out = queue.filter((m) => !m.delivered);
|
||||
for (const m of out) m.delivered = true;
|
||||
return out.map(({ id, body, at }) => ({ id, from_role: "human", class: "REQUEST", body, at }));
|
||||
},
|
||||
read: (id) => {
|
||||
const i = queue.findIndex((m) => m.id === id);
|
||||
if (i >= 0) queue.splice(i, 1);
|
||||
},
|
||||
// An answer (class RESULT) that names a queued message answers it: the message leaves the queue
|
||||
// here, so a session that ends before its message.read leaves no "unanswered" line under an
|
||||
// answer. A note in any other class stays in the transcript but leaves the message queued, so
|
||||
// a session that ends after a progress note still gets the "unanswered" line. The model's tool
|
||||
// can also pick class RESULT; such a note settles the message as the runner's answer would.
|
||||
onSend: ({ body, in_reply_to, result }) => {
|
||||
const { text, cards, dropped } = extractCards(scrub(body), orgIds(), { devOwner: devOwner === true });
|
||||
append({ at: now().toISOString(), from: "triage", text, cards, dropped, ...(in_reply_to ? { in_reply_to } : {}) });
|
||||
const i = in_reply_to && result === true ? queue.findIndex((m) => m.id === in_reply_to) : -1;
|
||||
if (i >= 0) queue.splice(i, 1);
|
||||
},
|
||||
usage: (phase, turn) => chainUsage(() => (phase === "begin" ? usageBegin(u, turn) : usageEnd(u, turn))),
|
||||
stopped: capStop,
|
||||
log,
|
||||
});
|
||||
let list;
|
||||
try {
|
||||
list = hostedBinds({ runDir: paths.agent, healthDir: paths.health, adapter: admitted.adapter, harnessMounts: admitted.harnessMounts ?? [], installRoots, protectedPaths, codeRoot, codeHolder: home, relaySocket: join(paths.agent, "relay.sock"), node, readOnly, harnessFiles });
|
||||
} catch (e) {
|
||||
if (e?.code !== "hosted-binds-refused") throw e;
|
||||
log(`triage host: route refused at the binds: ${e.fence}`);
|
||||
// Nothing of the refused start stays: the relay, its socket, the bundle and session.json go.
|
||||
relay?.close();
|
||||
relay = null;
|
||||
for (const f of ["relay.sock", "session.json", "bundle", "home", "work", "tmp"]) rmSync(join(paths.agent, f), { recursive: true, force: true });
|
||||
throw new TriageError("triage-route-invalid", "the model route's answer is not usable");
|
||||
}
|
||||
const argv = hostedArgv(list, paths.agent, [node, RUNNER, paths.agent], { bwrap });
|
||||
const out = openSync(paths.log, "a", 0o600);
|
||||
try {
|
||||
child = spawn(argv[0], argv.slice(1), { env: triageEnv(env, admitted.env, { node }), detached: true, stdio: ["pipe", out, out] });
|
||||
} finally {
|
||||
closeSync(out);
|
||||
}
|
||||
const mine = child;
|
||||
u.child = mine;
|
||||
const closeRelay = () => {
|
||||
relay?.close();
|
||||
relay = null;
|
||||
rmSync(join(paths.agent, "relay.sock"), { force: true });
|
||||
};
|
||||
mine.on("error", () => {});
|
||||
mine.stdin.on("error", () => {});
|
||||
mine.stdin.end(JSON.stringify({ cap }) + "\n");
|
||||
mine.on("exit", (code, signal) => {
|
||||
if (child !== mine) return;
|
||||
lastExit = { code: Number.isInteger(code) ? code : null, signal: signal ?? null, at: now().toISOString() };
|
||||
child = null;
|
||||
// A turn the session began and never ended (killed, crashed) still spent tokens: it gets its line.
|
||||
// The open turns are read when this runs, after any usage.end already in the chain.
|
||||
chainUsage(async () => { for (const k of [...u.baselines.keys()]) await finishTurn(u, k); });
|
||||
// Every message still queued leaves it, so none holds a slot with no session to answer it, and
|
||||
// the transcript says which: one the session took and never answered, or one it never read.
|
||||
for (const m of queue.splice(0)) {
|
||||
try { append({ at: now().toISOString(), from: "host", in_reply_to: m.id, text: m.delivered ? UNANSWERED : NOT_TAKEN }); } catch { log("triage host: transcript write failed"); }
|
||||
}
|
||||
closeRelay();
|
||||
});
|
||||
// A launcher that cannot run (missing, not executable) fires "error" and never "exit": the start
|
||||
// failed, so the caller writes NOT_STARTED and the host is not left reporting a session.
|
||||
try {
|
||||
await new Promise((resolve, reject) => { mine.once("spawn", resolve); mine.once("error", reject); });
|
||||
} catch (e) {
|
||||
if (child === mine) child = null;
|
||||
closeRelay();
|
||||
log(`triage host: launcher did not run: ${e.code ?? "error"}`);
|
||||
throw new TriageError("triage-start-failed", "the Triage session did not start");
|
||||
}
|
||||
};
|
||||
|
||||
// The cap is the route's to count. Once it answers over the cap with a time in the next two days,
|
||||
// the host refuses without asking again until that time, so a later answer cannot reopen the day.
|
||||
const capUntil = () => {
|
||||
if (overCapUntil !== null && Date.parse(overCapUntil) <= now().getTime()) overCapUntil = null;
|
||||
return overCapUntil;
|
||||
};
|
||||
const admit = async () => {
|
||||
if (!route || typeof route.admit !== "function") return { ok: false, code: "triage-no-route" };
|
||||
const held = capUntil();
|
||||
if (held !== null) return { ok: false, code: "triage-over-cap", until: held };
|
||||
// A turn still being counted (a session that just ended mid-turn, or a usage.end in flight) is in
|
||||
// the file before the count is read.
|
||||
await usageChain;
|
||||
const c = capNow();
|
||||
// The store sits in the Triage directory: one that is a link or not the Console user's is the
|
||||
// unsafe directory start would refuse, not an unreadable route.
|
||||
if (c.state === "route-unreadable") return { ok: false, code: unsafeRoot() ? "triage-dir-unsafe" : "triage-route-unreadable" };
|
||||
if (c.state === "usage-unreadable") return { ok: false, code: "triage-usage-unreadable" };
|
||||
if (c.state === "over") return { ok: false, code: "triage-over-cap", until: c.resetsAt };
|
||||
let a;
|
||||
try { a = await route.admit(); } catch { return { ok: false, code: "triage-route-unreadable" }; }
|
||||
if (a?.ok === true) return a;
|
||||
const code = ["triage-no-route", "triage-over-cap", "triage-route-unreadable"].includes(a?.code) || Object.hasOwn(ROUTE_REFUSALS, a?.code ?? "") ? a.code : "triage-route-refused";
|
||||
if (code !== "triage-over-cap") return { ok: false, code };
|
||||
const until = typeof a.until === "string" && ISO_TIME.test(a.until) ? Date.parse(a.until) : NaN;
|
||||
const ahead = until - now().getTime();
|
||||
if (!(ahead > 0 && ahead <= CAP_HOLD_MAX_MS)) return { ok: false, code };
|
||||
overCapUntil = new Date(until).toISOString();
|
||||
return { ok: false, code, until: overCapUntil };
|
||||
};
|
||||
|
||||
return {
|
||||
paths,
|
||||
get running() { return child !== null; },
|
||||
get pid() { return child?.pid ?? null; },
|
||||
// A route that can say whether one is stored (triage-route.mjs isSet) answers for the store.
|
||||
routeSet: () => Boolean(route && typeof route.admit === "function" && (typeof route.isSet !== "function" || route.isSet() === true)),
|
||||
async state() {
|
||||
const c = capNow();
|
||||
const unset = c.state === "not set";
|
||||
return {
|
||||
route: this.routeSet() ? "set" : "not set",
|
||||
overCapUntil: c.state === "over" ? c.resetsAt : capUntil(),
|
||||
// #1873 triage-cap: today's count against the stored cap. Names and numbers only.
|
||||
usageToday: unset ? "not set" : c.usageToday ?? "not read",
|
||||
cap: unset ? "not set" : c.cap ? { limit: c.cap.limit, reset: c.cap.reset, timeZone: c.cap.timeZone } : "not read",
|
||||
resetsAt: unset ? "not set" : c.resetsAt ?? "not read",
|
||||
running: child !== null,
|
||||
lastExit,
|
||||
snapshot: last,
|
||||
transcript: transcript(),
|
||||
};
|
||||
},
|
||||
refresh,
|
||||
snapshotAt: () => lastAt,
|
||||
// The owner's message: checked, admitted by the route, written to the transcript, queued, and
|
||||
// the session started when it isn't running.
|
||||
async message(raw) {
|
||||
const text = plainText(typeof raw === "string" ? raw : "", MESSAGE_MAX + 1).trim();
|
||||
if (!text) throw new TriageError("triage-message-empty", "the message is empty");
|
||||
if (text.length > MESSAGE_MAX) throw new TriageError("triage-message-too-long", `a message holds at most ${MESSAGE_MAX} characters`);
|
||||
if (queue.length >= QUEUE_MAX) throw new TriageError("triage-busy", "Triage has too many messages waiting");
|
||||
const admitted = await admit();
|
||||
if (!admitted.ok) throw Object.assign(new TriageError(admitted.code, ADMIT_MESSAGES[admitted.code] ?? "the model route refused"), admitted.until ? { until: admitted.until } : {});
|
||||
await refresh();
|
||||
const at = now().toISOString();
|
||||
const id = `owner-${randomBytes(6).toString("hex")}`;
|
||||
append({ at, from: "owner", id, text });
|
||||
// #1873 triage-wiring: every admitted answer passes the fences, a running session or not. One
|
||||
// that fails launches nothing and the message is not queued.
|
||||
let checked;
|
||||
try { checked = await checkAdmitted(admitted); }
|
||||
catch (e) {
|
||||
try { append({ at: now().toISOString(), from: "host", in_reply_to: id, text: e?.code === "triage-route-invalid" ? ROUTE_REFUSED : NOT_STARTED }); } catch {}
|
||||
throw e instanceof TriageError ? e : new TriageError("triage-route-invalid", "the model route's answer is not usable");
|
||||
}
|
||||
queue.push({ id, body: text, at, delivered: false });
|
||||
if (!child) {
|
||||
starting ??= start(checked).finally(() => { starting = null; });
|
||||
try { await starting; }
|
||||
catch (e) {
|
||||
const i = queue.findIndex((m) => m.id === id);
|
||||
if (i >= 0) queue.splice(i, 1);
|
||||
try { append({ at: now().toISOString(), from: "host", in_reply_to: id, text: e?.code === "triage-route-invalid" ? ROUTE_REFUSED : NOT_STARTED }); } catch {}
|
||||
relay?.close();
|
||||
relay = null;
|
||||
log(`triage host: start failed: ${e.code ?? "error"}`);
|
||||
throw e instanceof TriageError ? e : new TriageError("triage-start-failed", "the Triage session did not start");
|
||||
}
|
||||
}
|
||||
return { id, at };
|
||||
},
|
||||
async stop({ graceMs = 5000 } = {}) {
|
||||
const c = child;
|
||||
if (c) {
|
||||
try { process.kill(-c.pid, "SIGTERM"); } catch {}
|
||||
await new Promise((resolve) => {
|
||||
const t = setTimeout(() => { try { process.kill(-c.pid, "SIGKILL"); } catch {} resolve(); }, graceMs);
|
||||
c.once("exit", () => { clearTimeout(t); resolve(); });
|
||||
});
|
||||
}
|
||||
relay?.close();
|
||||
relay = null;
|
||||
await usageChain;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
// #1873 triage-host: the two owner routes of the Triage session. serve.mjs sends a request here only
|
||||
// for the instance owner's own session (rootOwner, signed in on the public name, no organization
|
||||
// selected); everyone else falls through to the answer an unknown route gives. The read carries the
|
||||
// transcript, the last health snapshot and the cards; the write carries one owner message and also
|
||||
// needs --send and the same-origin checks the org admin routes use. No body, path, token or
|
||||
// exception text reaches a response or a log line.
|
||||
|
||||
import { ROUTE_REFUSALS } from "./triage-host.mjs";
|
||||
|
||||
export const TRIAGE_PATHS = Object.freeze(["/api/triage", "/api/triage/message"]);
|
||||
export const BODY_MAX = 16384;
|
||||
// A read refreshes the snapshot when the one it holds is older than this.
|
||||
export const SNAPSHOT_MAX_AGE_MS = 30000;
|
||||
export const capMessage = until => `Triage is over its daily token cap. It takes messages again at ${until}.`;
|
||||
export const NO_ROUTE_MESSAGE = "No model route is set for Triage, so Triage will not start. The instance owner sets the route first.";
|
||||
|
||||
const STATUS = Object.freeze({
|
||||
"invalid-request": 400,
|
||||
"triage-message-empty": 400,
|
||||
"triage-message-too-long": 400,
|
||||
"triage-no-route": 409,
|
||||
"triage-route-refused": 409,
|
||||
"triage-route-invalid": 409,
|
||||
"triage-over-cap": 429,
|
||||
"triage-busy": 429,
|
||||
// #1873 triage-wiring: the route's refusals. Each launches nothing; the drawer shows its line.
|
||||
...Object.fromEntries(Object.keys(ROUTE_REFUSALS).map((code) => [code, 409])),
|
||||
});
|
||||
// Fixed words for each code; the host's own messages are fixed too, but the route never forwards text.
|
||||
const MESSAGES = Object.freeze({
|
||||
"invalid-request": "the request is not a Triage message",
|
||||
"triage-message-empty": "the message is empty",
|
||||
"triage-message-too-long": "the message is too long",
|
||||
"triage-no-route": NO_ROUTE_MESSAGE,
|
||||
"triage-route-refused": "the model route refused to start Triage",
|
||||
"triage-route-invalid": "the model route's answer is not usable",
|
||||
"triage-route-unreadable": "the model route could not be read",
|
||||
"triage-usage-unreadable": "Triage's token usage could not be read, so it takes no message",
|
||||
"triage-over-cap": "Triage is over its daily token cap and starts again after the time shown",
|
||||
"triage-busy": "Triage has too many messages waiting",
|
||||
"triage-dir-unsafe": "a Triage directory is not safe to use",
|
||||
"triage-start-failed": "the Triage session did not start",
|
||||
"outcome-unknown": "Triage did not answer",
|
||||
...ROUTE_REFUSALS,
|
||||
});
|
||||
|
||||
const json = (res, status, value) => {
|
||||
res.writeHead(status, { "content-type": "application/json", "cache-control": "no-store" });
|
||||
res.end(JSON.stringify(value));
|
||||
};
|
||||
const one = (req, name) => (req.headersDistinct[name]?.length === 1 ? req.headersDistinct[name][0] : null);
|
||||
|
||||
async function readBody(req) {
|
||||
if ((req.headers["content-type"] ?? "").split(";")[0].trim().toLowerCase() !== "application/json") throw Error();
|
||||
const chunks = [];
|
||||
let size = 0;
|
||||
for await (const chunk of req) {
|
||||
size += chunk.length;
|
||||
if (size > BODY_MAX) throw Error();
|
||||
chunks.push(chunk);
|
||||
}
|
||||
const value = JSON.parse(Buffer.concat(chunks));
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) throw Error();
|
||||
if (Object.keys(value).some((k) => k !== "text") || typeof value.text !== "string") throw Error();
|
||||
return value.text;
|
||||
}
|
||||
|
||||
// The read's answer. `send` tells the drawer whether it may offer the message box.
|
||||
export async function triageView(triage, { send, now = Date.now } = {}) {
|
||||
const held = triage.snapshotAt?.() ?? null;
|
||||
if (held === null || now() - held > SNAPSHOT_MAX_AGE_MS) await triage.refresh();
|
||||
const s = await triage.state();
|
||||
return {
|
||||
route: s.route,
|
||||
...(s.route === "set" ? {} : { routeMessage: NO_ROUTE_MESSAGE }),
|
||||
overCapUntil: s.overCapUntil ?? null,
|
||||
...(s.overCapUntil ? { capMessage: capMessage(s.overCapUntil) } : {}),
|
||||
// #1873 triage-cap: today's count, the stored cap and the next reset, or "not set" / "not read".
|
||||
...(Object.hasOwn(s, "usageToday") ? { usageToday: s.usageToday, cap: s.cap, resetsAt: s.resetsAt } : {}),
|
||||
running: s.running,
|
||||
lastExit: s.lastExit,
|
||||
snapshot: s.snapshot,
|
||||
transcript: s.transcript,
|
||||
send: send === true,
|
||||
};
|
||||
}
|
||||
|
||||
export async function triageHTTP(req, res, { path, triage, send, origin, log = () => {} }) {
|
||||
const done = (status, value) => {
|
||||
// Method, path and status only.
|
||||
log(`${new Date().toISOString()} ${req.method} ${path} ${status}`);
|
||||
return json(res, status, value);
|
||||
};
|
||||
// `code` is always a key of MESSAGES.
|
||||
const refuse = (code, extra = {}) => done(STATUS[code] ?? 503, { error: code, message: MESSAGES[code], ...extra });
|
||||
if (path === "/api/triage") {
|
||||
if (req.method !== "GET") return done(405, { error: "method not allowed" });
|
||||
try { return done(200, await triageView(triage, { send })); }
|
||||
catch { return refuse("outcome-unknown"); }
|
||||
}
|
||||
if (req.method !== "POST") return done(405, { error: "method not allowed" });
|
||||
if (!send) return done(403, { error: "read-only" });
|
||||
if (one(req, "origin") !== origin) return done(403, { error: "cross-origin request refused" });
|
||||
if (one(req, "sec-fetch-site") !== "same-origin") return done(403, { error: "cross-site request refused" });
|
||||
let text;
|
||||
try { text = await readBody(req); } catch { return refuse("invalid-request"); }
|
||||
try {
|
||||
return done(202, await triage.message(text));
|
||||
} catch (error) {
|
||||
const code = typeof error?.code === "string" && Object.hasOwn(MESSAGES, error.code) ? error.code : "outcome-unknown";
|
||||
return refuse(code, code === "triage-over-cap" && typeof error.until === "string" ? { until: error.until } : {});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
// #1873 triage-settings: the owner's read and write of the Triage model route, for Settings > Triage.
|
||||
// GET and POST /api/triage/route register through serve.mjs's route table, so the server's session
|
||||
// and Origin checks run first; this module then takes the proxy-admin gate (the instance owner's own
|
||||
// session on the public name, never a tester or a granted user) and the same-origin checks again.
|
||||
// A write goes through the route store (triage-route-store.mjs), so the lock, the revision compare
|
||||
// and the receipt are the store's. Before it, the model and harness are checked the way an
|
||||
// organization's route change is (checkRoutes in packages/organization/src/launch-settings.mjs):
|
||||
// offered by the catalogue, available, and compatible with the harness.
|
||||
//
|
||||
// The catalogue lives in each organization's bus (the trusted bus boot builds it; nothing outside
|
||||
// a bus holds the proxy client), so the Console reads it through each active organization's
|
||||
// `routes` verb, as the proxy setup page does, and merges the answers (mergeCatalogues).
|
||||
//
|
||||
// Saving a route starts nothing: the Triage host is not wired to the store in this slice.
|
||||
// No key, path, env value or exception text reaches a response or a log line.
|
||||
import { readdirSync, lstatSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { checkRoutes } from '../../organization/src/launch-settings.mjs';
|
||||
import { isDailyModelQuota } from '../../plugins/src/model-quota.mjs';
|
||||
import { readRoute, writeRoute } from './triage-route-store.mjs';
|
||||
|
||||
export const TRIAGE_ROUTE_PATH = '/api/triage/route';
|
||||
export const BODY_MAX = 16384;
|
||||
export const POST_KEYS = Object.freeze(['model', 'harness', 'cap', 'timeZone', 'revision']);
|
||||
export const NOT_SET_MESSAGE = 'No Triage route is set.';
|
||||
export const NOT_STARTED_NOTE = 'Saving a route does not start Triage. Triage also needs its key.';
|
||||
|
||||
const HARNESS_ROOT = fileURLToPath(new URL('../../plugins/harness/', import.meta.url));
|
||||
// The same shapes the store holds (triage-route-store.mjs).
|
||||
const MODEL_KEY = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
||||
const HARNESS_NAME = /^[a-z][a-z0-9-]{0,31}$/;
|
||||
const ACTOR = /^[a-z][a-z0-9-]{0,31}$/;
|
||||
// The store's zone rule (its knownZone): isDailyModelQuota's zone shape, then Intl.
|
||||
const knownZone = zone => typeof zone === 'string' && zone !== ''
|
||||
&& isDailyModelQuota('000000000000', { limit: 0, since: '2000-01-01T00:00:00.000Z', reset: '00:00', timeZone: zone });
|
||||
// The zone as Intl names it, so "america/chicago" is kept as "America/Chicago". A known zone that
|
||||
// Intl renames to something the store would not take is kept as typed.
|
||||
export function canonicalZone(zone) {
|
||||
try {
|
||||
const named = new Intl.DateTimeFormat('en-US', { timeZone: zone }).resolvedOptions().timeZone;
|
||||
return knownZone(named) ? named : zone;
|
||||
} catch { return zone; }
|
||||
}
|
||||
|
||||
const STATUS = Object.freeze({
|
||||
'human-required': 403,
|
||||
'read-only': 403,
|
||||
'cross-origin': 403,
|
||||
'cross-site': 403,
|
||||
'method-not-allowed': 405,
|
||||
'invalid-request': 400,
|
||||
'triage-cap-invalid': 400,
|
||||
'triage-zone-required': 400,
|
||||
'triage-zone-unknown': 400,
|
||||
'unknown-model': 400,
|
||||
'model-harness-incompatible': 400,
|
||||
'harness-off': 400,
|
||||
'triage-route-invalid': 400,
|
||||
'triage-route-stale': 409,
|
||||
'triage-route-busy': 409,
|
||||
'model-provider-unavailable': 503,
|
||||
'triage-route-unreadable': 503,
|
||||
'triage-route-unrecorded': 500,
|
||||
'outcome-unknown': 503,
|
||||
});
|
||||
// Fixed words for each code. The store's own messages name lock files, so none is forwarded.
|
||||
const MESSAGES = Object.freeze({
|
||||
'human-required': 'Only the instance owner can read or set the Triage route.',
|
||||
'read-only': 'This Console was started without writes, so the route cannot be saved.',
|
||||
'cross-origin': 'The request did not come from this Console page.',
|
||||
'cross-site': 'The request did not come from this Console page.',
|
||||
'method-not-allowed': 'This address takes GET and POST only.',
|
||||
'invalid-request': 'The request is not a Triage route. It takes model, harness, cap, timeZone and revision.',
|
||||
'triage-cap-invalid': 'The cap must be a whole number of tokens, 1 or more.',
|
||||
'triage-zone-required': 'A time zone is required. There is no default.',
|
||||
'triage-zone-unknown': 'That time zone is not known. Use a name like Europe/Berlin or America/Chicago.',
|
||||
'unknown-model': 'The model catalogue does not offer that model.',
|
||||
'model-harness-incompatible': 'That harness cannot run that model.',
|
||||
'harness-off': 'No harness module of that name is installed.',
|
||||
'triage-route-invalid': 'The route was refused as invalid.',
|
||||
'triage-route-stale': 'The route changed since this page read it. Reload the route before saving again.',
|
||||
'triage-route-busy': 'Another save of the route is in progress. Try again.',
|
||||
'model-provider-unavailable': 'The model catalogue cannot confirm that model now, so the route was not saved.',
|
||||
'triage-route-unreadable': 'The stored route could not be read.',
|
||||
'triage-route-unrecorded': 'The route was saved, but its receipt line was not written.',
|
||||
'outcome-unknown': 'The route service did not answer.',
|
||||
});
|
||||
|
||||
const json = (res, status, value) => {
|
||||
res.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' });
|
||||
res.end(JSON.stringify(value));
|
||||
};
|
||||
const one = (req, name) => (req.headersDistinct[name]?.length === 1 ? req.headersDistinct[name][0] : null);
|
||||
const plain = v => v !== null && typeof v === 'object' && !Array.isArray(v);
|
||||
const identity = m => JSON.stringify([m.provider, m.model, m.module, m.upstream]);
|
||||
|
||||
// The installed harness modules: a directory under packages/plugins/harness with an index.mjs.
|
||||
export function installedHarnesses(root = HARNESS_ROOT) {
|
||||
try {
|
||||
return readdirSync(root).filter(name => HARNESS_NAME.test(name) && (() => {
|
||||
try { return lstatSync(join(root, name, 'index.mjs')).isFile(); } catch { return false; }
|
||||
})()).sort();
|
||||
} catch { return []; }
|
||||
}
|
||||
|
||||
// Each active organization's catalogue, merged into the one the Triage route is checked against.
|
||||
// Only ready answers count. A key is offered when a ready catalogue lists it, and dropped when two
|
||||
// catalogues disagree on what it is (provider, model, module, upstream). It is available only when
|
||||
// every catalogue listing it says so, and its harnesses are those every listing names that are
|
||||
// installed here. Status is ready when one catalogue is; otherwise the first status seen.
|
||||
export function mergeCatalogues(list, harnesses) {
|
||||
const ready = (Array.isArray(list) ? list : []).filter(c => c?.status === 'ready' && Array.isArray(c.models));
|
||||
if (!ready.length) {
|
||||
const first = (Array.isArray(list) ? list : []).find(c => typeof c?.status === 'string')?.status;
|
||||
return { status: first && first !== 'ready' ? first : 'unavailable', models: [] };
|
||||
}
|
||||
const merged = new Map(), dropped = new Set();
|
||||
for (const catalogue of ready) {
|
||||
for (const m of catalogue.models) {
|
||||
if (!plain(m) || typeof m.key !== 'string' || !MODEL_KEY.test(m.key) || dropped.has(m.key)) continue;
|
||||
const names = Array.isArray(m.harnesses) ? m.harnesses.filter(h => typeof h === 'string') : [];
|
||||
const held = merged.get(m.key);
|
||||
if (!held) {
|
||||
merged.set(m.key, { key: m.key, label: typeof m.label === 'string' && m.label ? m.label : m.key, provider: m.provider, model: m.model, module: m.module, upstream: m.upstream, available: m.available === true, harnesses: names });
|
||||
continue;
|
||||
}
|
||||
if (identity(held) !== identity(m)) { merged.delete(m.key); dropped.add(m.key); continue; }
|
||||
held.available = held.available && m.available === true;
|
||||
held.harnesses = held.harnesses.filter(h => names.includes(h));
|
||||
}
|
||||
}
|
||||
const installed = new Set(harnesses);
|
||||
const models = [...merged.values()].map(m => ({ ...m, harnesses: m.harnesses.filter(h => installed.has(h)) })).sort((a, b) => a.key.localeCompare(b.key));
|
||||
return { status: 'ready', models };
|
||||
}
|
||||
|
||||
// The organization route check, called as it is for an organization's change: one role whose
|
||||
// vars name the harness and model, launch.models holding the catalogue's own entry for that key
|
||||
// (none when it is not offered), plugins.harness the installed harness modules, and
|
||||
// plugins.model-provider the entry's module. It throws unknown-model, model-provider-unavailable,
|
||||
// model-harness-incompatible or harness-off.
|
||||
export function checkTriageRoute({ model, harness, catalogue, harnesses }) {
|
||||
if (catalogue?.status !== 'ready') {
|
||||
const error = new Error('model-provider-unavailable'); error.code = 'model-provider-unavailable'; throw error;
|
||||
}
|
||||
const offered = catalogue.models.find(m => m.key === model) ?? null;
|
||||
const entry = offered ? { provider: offered.provider, model: offered.model, module: offered.module, upstream: offered.upstream } : null;
|
||||
checkRoutes({
|
||||
proposed: {
|
||||
roles: { triage: { vars: { harness, model } } },
|
||||
launch: { models: entry ? { [model]: entry } : {} },
|
||||
plugins: { harness: [...harnesses], 'model-provider': entry ? [entry.module] : [] },
|
||||
},
|
||||
old: null,
|
||||
levels: () => null,
|
||||
catalogue,
|
||||
});
|
||||
}
|
||||
|
||||
// What the signed-in owner is recorded as: their account id, or the dev local owner's subject.
|
||||
export function ownerActor(session) {
|
||||
if (typeof session?.account === 'string' && ACTOR.test(session.account)) return session.account;
|
||||
if (session?.provider === 'local-dev' && typeof session.sub === 'string' && ACTOR.test(session.sub)) return session.sub;
|
||||
return null;
|
||||
}
|
||||
|
||||
const view = doc => doc ? {
|
||||
model: doc.model, harness: doc.harness, cap: doc.cap.limit, reset: doc.cap.reset, timeZone: doc.cap.timeZone,
|
||||
revision: doc.revision, updatedAt: doc.updatedAt,
|
||||
} : null;
|
||||
// The panel's choices: no provider, upstream or module, only what a select shows.
|
||||
const choices = catalogue => ({
|
||||
status: catalogue.status,
|
||||
models: catalogue.models.map(m => ({ key: m.key, label: m.label, available: m.available, harnesses: [...m.harnesses] })),
|
||||
});
|
||||
|
||||
async function readBody(req) {
|
||||
if ((req.headers['content-type'] ?? '').split(';')[0].trim().toLowerCase() !== 'application/json') throw Error();
|
||||
const chunks = [];
|
||||
let size = 0;
|
||||
for await (const chunk of req) {
|
||||
size += chunk.length;
|
||||
if (size > BODY_MAX) throw Error();
|
||||
chunks.push(chunk);
|
||||
}
|
||||
const value = JSON.parse(Buffer.concat(chunks));
|
||||
if (!plain(value) || Object.keys(value).some(k => !POST_KEYS.includes(k))) throw Error();
|
||||
return value;
|
||||
}
|
||||
|
||||
// Registers both routes. `catalogues` returns each active organization's `routes` catalogue (or a
|
||||
// status when its bus did not answer); `harnesses` lists the installed harness modules.
|
||||
export function triageRouteRoutes({ dataRoot, catalogues, harnesses = installedHarnesses, log = () => {} }) {
|
||||
async function catalogue() {
|
||||
let list;
|
||||
try { list = await catalogues(); } catch { list = []; }
|
||||
return mergeCatalogues(list, harnesses());
|
||||
}
|
||||
async function handle(req, res, context) {
|
||||
const done = (status, value) => {
|
||||
// Method, path and status only.
|
||||
log(`${new Date().toISOString()} ${req.method} ${TRIAGE_ROUTE_PATH} ${status}`);
|
||||
return json(res, status, value);
|
||||
};
|
||||
const refuse = (code, extra = {}) => done(STATUS[code] ?? 503, { error: code, message: MESSAGES[code] ?? MESSAGES['outcome-unknown'], ...extra });
|
||||
if (req.method !== 'GET' && req.method !== 'POST') return refuse('method-not-allowed');
|
||||
// The proxy-admin gate: the instance owner's own session, never a tester or a granted user.
|
||||
if (context.owner !== true || !context.session) return refuse('human-required');
|
||||
// A write needs this Console's Origin and same-origin Fetch Metadata, exactly once each. A read
|
||||
// refuses another site's Origin or Fetch Metadata and lets their absence through, as the packs and
|
||||
// request reads do: a browser sends no Origin on a same-origin GET, and Chromium sends no
|
||||
// Fetch Metadata to a LAN http origin (serve.mjs marks only a LAN write).
|
||||
if (req.method === 'POST') {
|
||||
if (one(req, 'origin') !== context.origin) return refuse('cross-origin');
|
||||
if (one(req, 'sec-fetch-site') !== 'same-origin') return refuse('cross-site');
|
||||
} else {
|
||||
if (req.headersDistinct.origin && one(req, 'origin') !== context.origin) return refuse('cross-origin');
|
||||
if (req.headersDistinct['sec-fetch-site'] && !['same-origin', 'none'].includes(one(req, 'sec-fetch-site'))) return refuse('cross-site');
|
||||
}
|
||||
if (req.method === 'GET') {
|
||||
let doc;
|
||||
try { doc = readRoute(dataRoot); } catch (error) { return refuse(error?.code === 'triage-route-unreadable' ? 'triage-route-unreadable' : 'outcome-unknown'); }
|
||||
const route = view(doc);
|
||||
return done(200, {
|
||||
route, ...(route ? {} : { routeMessage: NOT_SET_MESSAGE }), revision: route ? route.revision : 0,
|
||||
choices: choices(await catalogue()), send: context.send === true, note: NOT_STARTED_NOTE,
|
||||
});
|
||||
}
|
||||
if (context.send !== true) return refuse('read-only');
|
||||
let body;
|
||||
try { body = await readBody(req); } catch { return refuse('invalid-request'); }
|
||||
const { model, harness, cap, timeZone, revision } = body;
|
||||
if (typeof model !== 'string' || !MODEL_KEY.test(model)) return refuse('unknown-model');
|
||||
if (typeof harness !== 'string' || !HARNESS_NAME.test(harness)) return refuse('harness-off');
|
||||
if (!Number.isSafeInteger(cap) || cap < 1) return refuse('triage-cap-invalid');
|
||||
if (timeZone === undefined || timeZone === null || timeZone === '') return refuse('triage-zone-required');
|
||||
if (!knownZone(timeZone)) return refuse('triage-zone-unknown');
|
||||
const zone = canonicalZone(timeZone);
|
||||
if (!Number.isSafeInteger(revision) || revision < 0) return refuse('invalid-request');
|
||||
try { checkTriageRoute({ model, harness, catalogue: await catalogue(), harnesses: harnesses() }); }
|
||||
catch (error) {
|
||||
const code = ['unknown-model', 'model-provider-unavailable', 'model-harness-incompatible', 'harness-off'].includes(error?.code) ? error.code : 'outcome-unknown';
|
||||
return refuse(code);
|
||||
}
|
||||
const by = ownerActor(context.session);
|
||||
if (by === null) return refuse('human-required');
|
||||
try {
|
||||
const doc = writeRoute(dataRoot, { model, harness, limit: cap, timeZone: zone, by, revision });
|
||||
return done(200, { route: view(doc), revision: doc.revision, note: NOT_STARTED_NOTE });
|
||||
} catch (error) {
|
||||
const code = typeof error?.code === 'string' && Object.hasOwn(MESSAGES, error.code) && error.code.startsWith('triage-route-') ? error.code : 'outcome-unknown';
|
||||
if (code !== 'triage-route-stale') return refuse(code);
|
||||
let stored = null;
|
||||
try { stored = view(readRoute(dataRoot)); } catch { /* the conflict still stands */ }
|
||||
return refuse(code, { revision: stored ? stored.revision : 0 });
|
||||
}
|
||||
}
|
||||
return registered => {
|
||||
registered.add('GET', TRIAGE_ROUTE_PATH, handle);
|
||||
registered.add('POST', TRIAGE_ROUTE_PATH, handle);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
// #1873 triage-route-store: the owner-only file that holds Triage's model route and daily cap,
|
||||
// <dataRoot>/triage/route.json (design report reports/triage-route-design.md section 1, option C).
|
||||
//
|
||||
// { version, revision, model, harness, cap: { limit, since, reset, timeZone }, updatedAt, updatedBy }
|
||||
//
|
||||
// Names only: a catalogue model key and a harness name. No env value and no key ever enter this
|
||||
// file; the host derives env, mounts and the adapter itself (a later slice). Nothing here is wired
|
||||
// into the Triage host or the Console yet: the Console keeps `route: null`.
|
||||
//
|
||||
// Fail closed. The triage directory is 0700 and route.json 0600, both owned by the running user;
|
||||
// a link, a hard link, a non-file, a wrong mode or owner, an unknown field or an unknown time zone
|
||||
// refuses. A write is compare-and-set on `revision` under an exclusive lock file, atomic (a 0600
|
||||
// temp file in the same directory, fsync, rename, fsync of the directory), and appends one line
|
||||
// to route-receipts.jsonl. Files and the directory the store makes get their modes set on the open
|
||||
// descriptor, so the owner's umask cannot leave them unreadable. A writer that dies leaves its lock; the next write refuses and names
|
||||
// it, and the person removes it by hand, as with the organization lifecycle fence.
|
||||
//
|
||||
// Choices Jason may still reverse each live in one place: the file location (routePaths), the
|
||||
// field names (ROUTE_FIELDS, CAP_FIELDS) and the reset rule (capFor).
|
||||
import { closeSync, constants, fchmodSync, fstatSync, fsyncSync, lstatSync, mkdirSync, openSync, readFileSync, renameSync, unlinkSync, writeSync } from "node:fs";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { join } from "node:path";
|
||||
import { isDailyModelQuota } from "../../plugins/src/model-quota.mjs";
|
||||
import { processState } from "../../seat/src/process-state.mjs";
|
||||
|
||||
export const ROUTE_VERSION = 1;
|
||||
export const ROUTE_FIELDS = Object.freeze(["version", "revision", "model", "harness", "cap", "updatedAt", "updatedBy"]);
|
||||
export const CAP_FIELDS = Object.freeze(["limit", "since", "reset", "timeZone"]);
|
||||
// The catalogue's model key shape (packages/bus/src/model-catalogue.mjs validId) and the Triage
|
||||
// host's harness name shape (packages/webui/src/triage-host.mjs HARNESS_NAME).
|
||||
const MODEL_KEY = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
||||
const HARNESS_NAME = /^[a-z][a-z0-9-]{0,31}$/;
|
||||
const ACTOR = /^[a-z][a-z0-9-]{0,31}$/;
|
||||
const ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
|
||||
// isDailyModelQuota checks one leaf under a twelve-hex key; the Triage cap has no key of its own.
|
||||
const LEAF_KEY = "000000000000";
|
||||
const MAX_BYTES = 64 * 1024;
|
||||
const INPUT_FIELDS = Object.freeze(["model", "harness", "limit", "timeZone", "by", "revision"]);
|
||||
|
||||
export class RouteStoreError extends Error {
|
||||
constructor(code, message) {
|
||||
super(message);
|
||||
this.name = "RouteStoreError";
|
||||
this.code = code;
|
||||
}
|
||||
}
|
||||
const unreadable = (why) => new RouteStoreError("triage-route-unreadable", `the Triage route store is unreadable: ${why}`);
|
||||
const invalid = (why) => new RouteStoreError("triage-route-invalid", `the Triage route is invalid: ${why}`);
|
||||
|
||||
// The file location, in one place.
|
||||
export function routePaths(dataRoot) {
|
||||
const dir = join(dataRoot, "triage");
|
||||
return { dir, file: join(dir, "route.json"), receipts: join(dir, "route-receipts.jsonl"), lock: join(dir, "route.lock") };
|
||||
}
|
||||
|
||||
// The reset rule, in one place: the day resets at 00:00 in the owner's zone, which has no default.
|
||||
// A first cap counts from now; a changed cap keeps its stored `since`, as an organization's token
|
||||
// limits do (packages/organization/src/launch-settings.mjs stamp), so lowering the cap never drops
|
||||
// the tokens already spent today. Only a reset would move it, and this slice has no reset verb.
|
||||
export function capFor({ limit, timeZone, previous, now }) {
|
||||
return { limit, since: previous ? previous.since : now, reset: "00:00", timeZone };
|
||||
}
|
||||
|
||||
const plain = (v) => v !== null && typeof v === "object" && !Array.isArray(v) && Object.getPrototypeOf(v) === Object.prototype;
|
||||
const exactKeys = (v, keys) => plain(v) && Object.keys(v).length === keys.length && keys.every((k) => Object.hasOwn(v, k));
|
||||
const canonicalTime = (s) => typeof s === "string" && ISO.test(s) && !Number.isNaN(Date.parse(s)) && new Date(s).toISOString() === s;
|
||||
// The same zone rule the stored cap is held to (isDailyModelQuota: its zone shape, then Intl).
|
||||
const knownZone = (zone) => typeof zone === "string"
|
||||
&& isDailyModelQuota(LEAF_KEY, { limit: 0, since: "2000-01-01T00:00:00.000Z", reset: "00:00", timeZone: zone });
|
||||
|
||||
export function validRoute(doc) {
|
||||
return exactKeys(doc, ROUTE_FIELDS)
|
||||
&& doc.version === ROUTE_VERSION
|
||||
&& Number.isSafeInteger(doc.revision) && doc.revision >= 1
|
||||
&& typeof doc.model === "string" && MODEL_KEY.test(doc.model)
|
||||
&& typeof doc.harness === "string" && HARNESS_NAME.test(doc.harness)
|
||||
// isDailyModelQuota also checks that the zone is one Intl knows.
|
||||
&& exactKeys(doc.cap, CAP_FIELDS) && isDailyModelQuota(LEAF_KEY, doc.cap)
|
||||
&& canonicalTime(doc.updatedAt)
|
||||
&& typeof doc.updatedBy === "string" && ACTOR.test(doc.updatedBy);
|
||||
}
|
||||
|
||||
const uidOf = (opts) => opts.uid ?? process.getuid();
|
||||
// The owner the files are held to: the same user, unless a test checks the file owner alone.
|
||||
const fileUidOf = (opts) => opts.fileUid ?? uidOf(opts);
|
||||
|
||||
// The triage directory: a real directory, the running user's, mode 0700. Returns false when it is
|
||||
// missing and `create` is off; with `create` it makes it (never the data root above it).
|
||||
function checkDir(dir, uid, { create }) {
|
||||
let st;
|
||||
try { st = lstatSync(dir); }
|
||||
catch (e) {
|
||||
if (e.code !== "ENOENT") throw unreadable("the triage directory cannot be read");
|
||||
if (!create) return false;
|
||||
try { mkdirSync(dir, { mode: 0o700 }); } catch { throw unreadable("the triage directory cannot be made"); }
|
||||
// The umask may have narrowed the mode; set it on the directory just made, never through a link.
|
||||
try {
|
||||
const fd = openSync(dir, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
|
||||
try { fchmodSync(fd, 0o700); } finally { closeSync(fd); }
|
||||
} catch { throw unreadable("the triage directory cannot be made"); }
|
||||
st = lstatSync(dir);
|
||||
}
|
||||
if (st.isSymbolicLink()) throw unreadable("the triage directory is a link");
|
||||
if (!st.isDirectory()) throw unreadable("the triage directory is not a directory");
|
||||
if (st.uid !== uid) throw unreadable("the triage directory is not owned by this user");
|
||||
if ((st.mode & 0o777) !== 0o700) throw unreadable("the triage directory mode is not 0700");
|
||||
return true;
|
||||
}
|
||||
|
||||
// An opened file's own checks: a regular file, one link, the running user's, mode 0600.
|
||||
function checkFile(fd, uid, what) {
|
||||
const st = fstatSync(fd);
|
||||
if (!st.isFile()) throw unreadable(`${what} is not a regular file`);
|
||||
if (st.nlink !== 1) throw unreadable(`${what} has more than one link`);
|
||||
if (st.uid !== uid) throw unreadable(`${what} is not owned by this user`);
|
||||
if ((st.mode & 0o777) !== 0o600) throw unreadable(`${what} mode is not 0600`);
|
||||
return st;
|
||||
}
|
||||
|
||||
function readFile(file, uid) {
|
||||
let fd;
|
||||
try { fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); }
|
||||
catch (e) {
|
||||
if (e.code === "ENOENT") return null;
|
||||
if (e.code === "ELOOP") throw unreadable("route.json is a link");
|
||||
throw unreadable("route.json cannot be opened");
|
||||
}
|
||||
try {
|
||||
const st = checkFile(fd, uid, "route.json");
|
||||
if (st.size > MAX_BYTES) throw unreadable("route.json is too large");
|
||||
let doc;
|
||||
try { doc = JSON.parse(readFileSync(fd, "utf8")); } catch { throw unreadable("route.json is not JSON"); }
|
||||
if (!validRoute(doc)) throw unreadable("route.json does not match the route shape");
|
||||
return doc;
|
||||
} finally { closeSync(fd); }
|
||||
}
|
||||
|
||||
// The stored route, or null when none is set. Throws triage-route-unreadable on anything unsafe.
|
||||
export function readRoute(dataRoot, opts = {}) {
|
||||
const { dir, file } = routePaths(dataRoot);
|
||||
if (!checkDir(dir, uidOf(opts), { create: false })) return null;
|
||||
return readFile(file, fileUidOf(opts));
|
||||
}
|
||||
|
||||
function checkInput(input) {
|
||||
if (!plain(input)) throw invalid("the input is not an object");
|
||||
for (const k of Object.keys(input)) if (!INPUT_FIELDS.includes(k)) throw invalid(`unknown field ${k}`);
|
||||
if (typeof input.model !== "string" || !MODEL_KEY.test(input.model)) throw invalid("model must be a catalogue model key");
|
||||
if (typeof input.harness !== "string" || !HARNESS_NAME.test(input.harness)) throw invalid("harness must be a harness name");
|
||||
if (!Number.isSafeInteger(input.limit) || input.limit < 0) throw invalid("the cap must be a whole number of tokens, 0 or more");
|
||||
if (input.timeZone === undefined) throw invalid("a time zone is required");
|
||||
if (!knownZone(input.timeZone)) throw invalid("the time zone is unknown");
|
||||
if (typeof input.by !== "string" || !ACTOR.test(input.by)) throw invalid("the actor name is invalid");
|
||||
if (input.revision !== undefined && (!Number.isSafeInteger(input.revision) || input.revision < 0)) throw invalid("revision must be a whole number, 0 or more");
|
||||
}
|
||||
|
||||
function holderLine(lock) {
|
||||
let holder = null;
|
||||
try {
|
||||
const fd = openSync(lock, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
||||
try { holder = JSON.parse(readFileSync(fd, "utf8")); } finally { closeSync(fd); }
|
||||
} catch { holder = null; }
|
||||
if (!Number.isSafeInteger(holder?.pid) || holder.pid <= 0) return `another write holds ${lock}, or a writer left it; remove it by hand once no write is running`;
|
||||
let alive;
|
||||
try { alive = processState(holder.pid).alive; } catch { alive = null; }
|
||||
if (alive === false) return `a writer that is not running (pid ${holder.pid}) left ${lock}; remove it by hand`;
|
||||
return `another write (pid ${holder.pid}) holds ${lock}; try again when it ends`;
|
||||
}
|
||||
|
||||
function takeLock(lock) {
|
||||
let fd;
|
||||
try { fd = openSync(lock, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600); }
|
||||
catch (e) {
|
||||
if (e.code === "EEXIST") throw new RouteStoreError("triage-route-busy", `the Triage route is busy: ${holderLine(lock)}`);
|
||||
throw unreadable("the lock cannot be made");
|
||||
}
|
||||
try {
|
||||
writeSync(fd, `${JSON.stringify({ pid: process.pid, at: new Date().toISOString() })}\n`);
|
||||
return { fd, st: fstatSync(fd) };
|
||||
} catch (e) {
|
||||
closeSync(fd);
|
||||
try { unlinkSync(lock); } catch {}
|
||||
throw unreadable("the lock cannot be written");
|
||||
}
|
||||
}
|
||||
|
||||
function dropLock(lock, held) {
|
||||
try {
|
||||
const now = lstatSync(lock);
|
||||
if (now.dev === held.st.dev && now.ino === held.st.ino) unlinkSync(lock);
|
||||
} catch {}
|
||||
closeSync(held.fd);
|
||||
}
|
||||
|
||||
// The receipts file, opened for append before the route changes, so a bad one refuses first. An
|
||||
// existing file is held to its checks as it is; only a file made here gets its mode set.
|
||||
function openReceipts(receipts, uid) {
|
||||
const flags = constants.O_WRONLY | constants.O_APPEND | constants.O_NOFOLLOW | constants.O_NONBLOCK;
|
||||
let fd;
|
||||
try { fd = openSync(receipts, flags); }
|
||||
catch (e) {
|
||||
if (e.code === "ELOOP") throw unreadable("route-receipts.jsonl is a link");
|
||||
if (e.code !== "ENOENT") throw unreadable("route-receipts.jsonl cannot be opened");
|
||||
try { fd = openSync(receipts, flags | constants.O_CREAT | constants.O_EXCL, 0o600); }
|
||||
catch { throw unreadable("route-receipts.jsonl cannot be made"); }
|
||||
try { fchmodSync(fd, 0o600); } catch { closeSync(fd); throw unreadable("route-receipts.jsonl cannot be made"); }
|
||||
}
|
||||
try { checkFile(fd, uid, "route-receipts.jsonl"); } catch (e) { closeSync(fd); throw e; }
|
||||
return fd;
|
||||
}
|
||||
|
||||
const summary = (doc) => (doc ? { model: doc.model, harness: doc.harness, cap: doc.cap } : null);
|
||||
|
||||
// Set the route. input: { model, harness, limit, timeZone, by, revision? }. With `revision` the
|
||||
// write happens only if the stored revision equals it (0: only when no route is set yet); without
|
||||
// it, the revision read under the lock. Returns the new route.
|
||||
// opts.now, opts.uid and opts.fileUid are for tests; opts.hook(stage) is a test seam at "write"
|
||||
// (inside the temp write), "crash" (after the temp file is written), "before-rename" and
|
||||
// "after-rename" (before the receipt line). A failure after the rename refuses as
|
||||
// triage-route-unrecorded: the route did change, and its receipt line is missing.
|
||||
export function writeRoute(dataRoot, input, opts = {}) {
|
||||
checkInput(input);
|
||||
const { dir, file, receipts, lock } = routePaths(dataRoot);
|
||||
const uid = uidOf(opts);
|
||||
const fileUid = fileUidOf(opts);
|
||||
const now = (opts.now ?? (() => new Date()))().toISOString();
|
||||
const hook = opts.hook ?? (() => {});
|
||||
checkDir(dir, uid, { create: true });
|
||||
const held = takeLock(lock);
|
||||
try {
|
||||
const current = readFile(file, fileUid);
|
||||
const stored = current?.revision ?? 0;
|
||||
if (input.revision !== undefined && input.revision !== stored) {
|
||||
throw new RouteStoreError("triage-route-stale", `the Triage route changed: the stored revision is ${stored}, not ${input.revision}`);
|
||||
}
|
||||
const next = {
|
||||
version: ROUTE_VERSION, revision: stored + 1, model: input.model, harness: input.harness,
|
||||
cap: capFor({ limit: input.limit, timeZone: input.timeZone, previous: current?.cap ?? null, now }),
|
||||
updatedAt: now, updatedBy: input.by,
|
||||
};
|
||||
if (!validRoute(next)) throw invalid("the new route does not match the route shape");
|
||||
const receiptFd = openReceipts(receipts, fileUid);
|
||||
try {
|
||||
const temp = join(dir, `.route.json.${process.pid}.${randomBytes(6).toString("hex")}.tmp`);
|
||||
let tfd;
|
||||
try { tfd = openSync(temp, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600); }
|
||||
catch { throw unreadable("route.json cannot be written"); }
|
||||
try {
|
||||
try {
|
||||
fchmodSync(tfd, 0o600);
|
||||
hook("write");
|
||||
writeSync(tfd, `${JSON.stringify(next, null, 2)}\n`);
|
||||
fsyncSync(tfd);
|
||||
} finally { closeSync(tfd); }
|
||||
} catch { try { unlinkSync(temp); } catch {} throw unreadable("route.json cannot be written"); }
|
||||
hook("crash");
|
||||
hook("before-rename");
|
||||
try { renameSync(temp, file); } catch (e) { try { unlinkSync(temp); } catch {} throw unreadable("route.json cannot be replaced"); }
|
||||
try {
|
||||
hook("after-rename");
|
||||
const dfd = openSync(dir, constants.O_RDONLY | constants.O_DIRECTORY);
|
||||
try { fsyncSync(dfd); } finally { closeSync(dfd); }
|
||||
writeSync(receiptFd, `${JSON.stringify({ at: now, by: input.by, revision: next.revision, from: summary(current), to: summary(next) })}\n`);
|
||||
fsyncSync(receiptFd);
|
||||
} catch {
|
||||
throw new RouteStoreError("triage-route-unrecorded", `the Triage route changed to revision ${next.revision}, but its receipt line was not written`);
|
||||
}
|
||||
} finally { closeSync(receiptFd); }
|
||||
return next;
|
||||
} finally { dropLock(lock, held); }
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
// #1873 triage-wiring: the Triage host's model route, resolved again at each admit (design report
|
||||
// reports/triage-route-design.md slice 5). Four sources, each read fresh:
|
||||
// the route store harness name, model key and cap (triage-route-store.mjs)
|
||||
// the key file <configDir>/secrets/triage/model (packages/cli/src/triage-key-file.mjs, the
|
||||
// key verbs' own reader and checks; never the module that holds the proxy client)
|
||||
// the model catalogue model and provider for the key, read from each active organization's bus
|
||||
// `routes` verb and merged (mergeCatalogues), then held to checkRoutes, the
|
||||
// check an organization's route change runs: offered, available, compatible
|
||||
// the harness module adapter, turnEnd, mounts and harnessEnv (packages/plugins/harness/<name>)
|
||||
// proxy.json the proxy's port, read at each admit (proxyPort may be a function)
|
||||
// the provider module the route's transport for this harness (packages/plugins/model-provider/
|
||||
// <module>, route()), the check a bus launch runs: a model the provider holds
|
||||
// off the harness (an OAuth Claude model on pi) is triage-harness-incompatible
|
||||
// The answer's env holds ANTHROPIC_BASE_URL (the instance proxy on 127.0.0.1) and the Triage key
|
||||
// under the one name the harness takes it by (ROUTE_KEY_NAMES: claude-code's ANTHROPIC_AUTH_TOKEN,
|
||||
// pi's ANTHROPIC_API_KEY). harnessEnv holds only the module's own names that the host's fence takes
|
||||
// (refusedHarnessName): no MOSAIC_* name, which the host sets itself from the module's bundle() and
|
||||
// env() (triage-host.mjs). #1873 harness-matrix: a harness runs Triage when it has bundle() and env()
|
||||
// and meets one contract, with no list of harness names: env() given a stand-in proxy puts the
|
||||
// stand-in's key, unchanged, in exactly one ROUTE_KEY_NAMES name and nowhere else, and names the
|
||||
// stand-in's URL only as ANTHROPIC_BASE_URL, if at all. A harness that takes the URL through its own
|
||||
// file (pi's models.json) is held to that at the host, which checks the file bundle() writes. The
|
||||
// host runs its fences on every answer (triage-host.mjs checkAdmitted), so nothing here is trusted
|
||||
// on its own.
|
||||
//
|
||||
// Each refusal is a code from ROUTE_REFUSALS (triage-host.mjs) with a fixed plain line. No code or line carries a path,
|
||||
// a key or an env value. Whether the key is in the proxy's list cannot be known without the proxy
|
||||
// management client, which the Console never loads, so that is not checked here: a key the proxy does
|
||||
// not hold fails at the model call.
|
||||
import { connect } from "node:net";
|
||||
import { lstatSync, readdirSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { checkRoutes } from "../../organization/src/launch-settings.mjs";
|
||||
import { SECRET, keyPrefix, readKey } from "../../cli/src/triage-key-file.mjs";
|
||||
import { readRoute } from "./triage-route-store.mjs";
|
||||
import { ROUTE_KEY_NAMES, ROUTE_REFUSALS, refusedHarnessName } from "./triage-host.mjs";
|
||||
import { checkedTransport } from "../../harness/src/bundle.mjs";
|
||||
|
||||
export { ROUTE_REFUSALS };
|
||||
|
||||
const HARNESS_ROOT = fileURLToPath(new URL("../../plugins/harness/", import.meta.url));
|
||||
const PROVIDER_ROOT = fileURLToPath(new URL("../../plugins/model-provider/", import.meta.url));
|
||||
const HARNESS_NAME = /^[a-z][a-z0-9-]{0,31}$/;
|
||||
const MODEL_KEY = /^[a-z0-9][a-z0-9._-]{0,63}$/;
|
||||
export const PROBE_TIMEOUT_MS = 1000;
|
||||
|
||||
const refuse = (code) => ({ ok: false, code });
|
||||
// The stand-in proxy env() is asked about. Neither value is a real URL or key.
|
||||
const PROXY_PROBE = Object.freeze({ baseUrl: "http://127.0.0.1:9", key: "triage-route-probe-key" });
|
||||
// The name env(), given the stand-in proxy, takes the key by, or null. The key goes unchanged into
|
||||
// exactly one ROUTE_KEY_NAMES name and is in no other value; the URL is in no value but
|
||||
// ANTHROPIC_BASE_URL, which, when present, is the stand-in's URL.
|
||||
export const routeKeyName = (given) => {
|
||||
if (!plain(given)) return null;
|
||||
const holders = Object.entries(given).filter(([, v]) => String(v).includes(PROXY_PROBE.key));
|
||||
if (holders.length !== 1 || !ROUTE_KEY_NAMES.includes(holders[0][0]) || holders[0][1] !== PROXY_PROBE.key) return null;
|
||||
if (given.ANTHROPIC_BASE_URL !== undefined && given.ANTHROPIC_BASE_URL !== PROXY_PROBE.baseUrl) return null;
|
||||
if (Object.entries(given).some(([k, v]) => k !== "ANTHROPIC_BASE_URL" && String(v).includes(PROXY_PROBE.baseUrl))) return null;
|
||||
return holders[0][0];
|
||||
};
|
||||
const validPort = (p) => Number.isInteger(p) && p > 0 && p <= 65535;
|
||||
const plain = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
||||
const identity = (m) => JSON.stringify([m.provider, m.model, m.module, m.upstream]);
|
||||
|
||||
// The whole shape of this instance's Triage key, for the host's tokenPattern fence.
|
||||
export const triageTokenPattern = (configDir) => new RegExp(keyPrefix(configDir).replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + SECRET.source.replace(/^\^|\$$/g, ""));
|
||||
|
||||
// The installed harness modules: a directory under packages/plugins/harness with an index.mjs.
|
||||
export function installedHarnesses(root = HARNESS_ROOT) {
|
||||
try {
|
||||
return readdirSync(root).filter((name) => HARNESS_NAME.test(name) && (() => {
|
||||
try { return lstatSync(join(root, name, "index.mjs")).isFile(); } catch { return false; }
|
||||
})()).sort();
|
||||
} catch { return []; }
|
||||
}
|
||||
|
||||
// The repository's harness module of that name, or null.
|
||||
export async function repoHarnessModule(name, root = HARNESS_ROOT) {
|
||||
if (!HARNESS_NAME.test(name)) return null;
|
||||
const file = join(root, name, "index.mjs");
|
||||
try { if (!lstatSync(file).isFile()) return null; } catch { return null; }
|
||||
return import(pathToFileURL(file).href);
|
||||
}
|
||||
|
||||
// The repository's model-provider module of that name, or null.
|
||||
export async function repoProviderModule(name, root = PROVIDER_ROOT) {
|
||||
if (!HARNESS_NAME.test(name ?? "")) return null;
|
||||
const file = join(root, name, "index.mjs");
|
||||
try { if (!lstatSync(file).isFile()) return null; } catch { return null; }
|
||||
return import(pathToFileURL(file).href);
|
||||
}
|
||||
|
||||
// Each active organization's catalogue, merged into one. Only ready answers count. A key is offered
|
||||
// when a ready catalogue lists it, and dropped when two disagree on what it is (provider, model,
|
||||
// module, upstream). It is available only when every catalogue listing it says so, and its harnesses
|
||||
// are those every listing names that are installed here. Status is ready when one catalogue is;
|
||||
// otherwise the first status seen. The triage-settings lane's Settings check merges the same way.
|
||||
export function mergeCatalogues(list, harnesses) {
|
||||
const all = Array.isArray(list) ? list : [];
|
||||
const ready = all.filter((c) => c?.status === "ready" && Array.isArray(c.models));
|
||||
if (!ready.length) {
|
||||
const first = all.find((c) => typeof c?.status === "string")?.status;
|
||||
return { status: first && first !== "ready" ? first : "unavailable", models: [] };
|
||||
}
|
||||
const merged = new Map();
|
||||
const dropped = new Set();
|
||||
for (const catalogue of ready) {
|
||||
for (const m of catalogue.models) {
|
||||
if (!plain(m) || typeof m.key !== "string" || !MODEL_KEY.test(m.key) || dropped.has(m.key)) continue;
|
||||
const names = Array.isArray(m.harnesses) ? m.harnesses.filter((h) => typeof h === "string") : [];
|
||||
const held = merged.get(m.key);
|
||||
if (!held) {
|
||||
merged.set(m.key, { key: m.key, provider: m.provider, model: m.model, module: m.module, upstream: m.upstream, available: m.available === true, harnesses: names });
|
||||
continue;
|
||||
}
|
||||
if (identity(held) !== identity(m)) { merged.delete(m.key); dropped.add(m.key); continue; }
|
||||
held.available = held.available && m.available === true;
|
||||
held.harnesses = held.harnesses.filter((h) => names.includes(h));
|
||||
}
|
||||
}
|
||||
const installed = new Set(harnesses);
|
||||
const models = [...merged.values()].map((m) => ({ ...m, harnesses: m.harnesses.filter((h) => installed.has(h)) }));
|
||||
return { status: "ready", models };
|
||||
}
|
||||
|
||||
// Does something accept a connection on 127.0.0.1:port within the timeout? Nothing is sent.
|
||||
export function probePort(port, { timeoutMs = PROBE_TIMEOUT_MS } = {}) {
|
||||
return new Promise((resolve) => {
|
||||
const sock = connect({ host: "127.0.0.1", port });
|
||||
const done = (ok) => { sock.destroy(); resolve(ok); };
|
||||
sock.setTimeout(timeoutMs, () => done(false));
|
||||
sock.once("connect", () => done(true));
|
||||
sock.once("error", () => done(false));
|
||||
});
|
||||
}
|
||||
|
||||
// The organization route check for the stored route against the merged catalogue: null when it
|
||||
// passes, else the refusal code.
|
||||
export function checkModel({ model, harness, catalogue, harnesses }) {
|
||||
// No ready catalogue: whether the model is offered is not known, which is its own refusal.
|
||||
if (catalogue.status !== "ready") return "triage-catalogue-unavailable";
|
||||
const offered = catalogue.models.find((m) => m.key === model);
|
||||
const entry = offered && typeof offered.module === "string" && offered.module
|
||||
? { provider: offered.provider, model: offered.model, module: offered.module, upstream: offered.upstream } : null;
|
||||
if (!entry) return "triage-model-not-offered";
|
||||
const proposed = {
|
||||
roles: { triage: { vars: { harness, model } } },
|
||||
launch: { models: { [model]: entry } },
|
||||
plugins: { harness: [...harnesses], "model-provider": [entry.module] },
|
||||
};
|
||||
try {
|
||||
checkRoutes({ proposed, old: null, levels: () => null, catalogue });
|
||||
return null;
|
||||
} catch (e) {
|
||||
// unknown-model, model-provider-unavailable, an unavailable model, or anything else: not offered.
|
||||
if (e?.code === "model-harness-incompatible" || e?.code === "harness-off") return "triage-harness-incompatible";
|
||||
return "triage-model-not-offered";
|
||||
}
|
||||
}
|
||||
|
||||
// createTriageRoute({ dataRoot, configDir, proxyPort, catalogues, harnessModule, providerModule, probe, env, installed })
|
||||
// proxyPort the instance proxy's port (proxy.json), or null when none is configured; or a
|
||||
// function that answers it, called at each admit
|
||||
// catalogues async () -> [catalogue] each active organization's `routes` catalogue
|
||||
// harnessModule async (name) -> the harness module or null; the repository's by default
|
||||
// providerModule async (name) -> the model-provider module or null; the repository's by default
|
||||
// probe async (port) -> true when the proxy accepts a connection
|
||||
// env the Console's environment, for the module's mounts (MOSAIC_CLAUDE_BIN)
|
||||
// installed () -> the installed harness names
|
||||
// Returns { isSet(), admit() }. isSet answers whether a route is stored (an unreadable store counts
|
||||
// as set, so the read does not hide it; admit refuses it).
|
||||
export function createTriageRoute({ dataRoot, configDir, proxyPort = null, catalogues = async () => [], harnessModule = repoHarnessModule, providerModule = repoProviderModule, probe = probePort, env = process.env, installed = installedHarnesses, keyOptions = {} }) {
|
||||
if (typeof dataRoot !== "string" || !dataRoot.startsWith("/")) throw new TypeError("dataRoot must be an absolute path");
|
||||
if (typeof configDir !== "string" || !configDir.startsWith("/")) throw new TypeError("configDir must be an absolute path");
|
||||
if (proxyPort !== null && typeof proxyPort !== "function" && !validPort(proxyPort)) throw new TypeError("proxyPort must be a port number, null or a function");
|
||||
return {
|
||||
isSet() {
|
||||
try { return readRoute(dataRoot) !== null; } catch { return true; }
|
||||
},
|
||||
async admit() {
|
||||
// 1. The stored route. Unreadable is the host's own code (it also checks the store first).
|
||||
let stored;
|
||||
try { stored = readRoute(dataRoot); } catch { return refuse("triage-route-unreadable"); }
|
||||
if (!stored) return refuse("triage-no-route");
|
||||
// 2. The key, with the key verbs' checks: a regular file, one link, this user's, 0600, both
|
||||
// directories 0700, no link, no FIFO (O_NONBLOCK, then the regular-file check).
|
||||
let record;
|
||||
try { record = readKey(configDir, keyOptions); } catch { return refuse("triage-key-unsafe"); }
|
||||
if (!record) return refuse("triage-key-missing");
|
||||
// 3. The proxy, on this machine, at the port proxy.json names now.
|
||||
let port = null;
|
||||
try { port = typeof proxyPort === "function" ? await proxyPort() : proxyPort; } catch { port = null; }
|
||||
if (!validPort(port)) return refuse("triage-proxy-down");
|
||||
let up = false;
|
||||
try { up = await probe(port); } catch { up = false; }
|
||||
if (up !== true) return refuse("triage-proxy-down");
|
||||
// 4. The model: offered, available, and runnable on the harness.
|
||||
const harnesses = installed();
|
||||
let list = [];
|
||||
try { list = await catalogues(); } catch { list = []; }
|
||||
const catalogue = mergeCatalogues(list, harnesses);
|
||||
const failed = checkModel({ model: stored.model, harness: stored.harness, catalogue, harnesses });
|
||||
if (failed) return refuse(failed);
|
||||
const offered = catalogue.models.find((m) => m.key === stored.model);
|
||||
// #1873 harness-matrix: the provider's own route for this harness, as a bus launch asks it, so
|
||||
// a stale catalogue row that names a harness the provider now holds the model off is refused.
|
||||
// A provider with no route() gives no transport; a harness that needs one then fails below.
|
||||
let transport = null;
|
||||
let provider = null;
|
||||
try { provider = await providerModule(offered.module); } catch { provider = null; }
|
||||
if (typeof provider?.route === "function") {
|
||||
const entry = { provider: offered.provider, model: offered.model, module: offered.module, upstream: offered.upstream };
|
||||
try { transport = checkedTransport(entry, stored.harness, provider.route); }
|
||||
catch (e) { return refuse(e?.code === "model-harness-incompatible" ? "triage-harness-incompatible" : "triage-model-not-offered"); }
|
||||
}
|
||||
// 5. The harness module's own answers.
|
||||
let mod = null;
|
||||
try { mod = await harnessModule(stored.harness); } catch { mod = null; }
|
||||
if (!mod || typeof mod.adapter !== "function" || typeof mod.turnEnd !== "function" || !Array.isArray(mod.envNames)
|
||||
|| typeof mod.bundle !== "function" || typeof mod.env !== "function") return refuse("triage-harness-incompatible");
|
||||
let adapter, turnEnd, harnessMounts, harnessEnv, keyName;
|
||||
try {
|
||||
adapter = mod.adapter();
|
||||
turnEnd = mod.turnEnd();
|
||||
harnessMounts = typeof mod.mounts === "function" ? mod.mounts({ env }) : [];
|
||||
// The module's env() for this model and the stand-in proxy, held to the contract, less every
|
||||
// name the host's fence refuses (MOSAIC_* and ANTHROPIC_* among them: the host sets those)
|
||||
// and every name the module does not declare.
|
||||
const session = { model: offered.model, provider: offered.provider ?? "", ...(transport ? { proxyTransport: { ...transport } } : {}) };
|
||||
const given = mod.env({ files: {}, policy: { tools: [] }, tools: [], session, models: {}, proxy: { ...PROXY_PROBE } });
|
||||
keyName = routeKeyName(given);
|
||||
if (!keyName) return refuse("triage-harness-incompatible");
|
||||
harnessEnv = Object.fromEntries(Object.entries(given)
|
||||
.filter(([k, v]) => mod.envNames.includes(k) && !refusedHarnessName(k) && typeof v === "string"));
|
||||
} catch { return refuse("triage-harness-incompatible"); }
|
||||
return {
|
||||
ok: true, harness: stored.harness, adapter, turnEnd, harnessMounts, harnessEnv,
|
||||
model: offered.model, provider: offered.provider ?? "",
|
||||
...(transport ? { transport } : {}),
|
||||
env: { ANTHROPIC_BASE_URL: `http://127.0.0.1:${port}`, [keyName]: record.key },
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
// #1873 triage-host: the health snapshot the Console writes for the Triage session (design
|
||||
// section 2). It is composed from reads that already exist, keeps only the fields on the fixed list
|
||||
// below, and is written as one JSON file in <dataRoot>/triage/health/, which the session mounts
|
||||
// read-only. States, times, counts and codes only: no token, key, host path, pid or upstream text.
|
||||
// A read that fails, times out or answers outside its fixed shape says "not read", never a guess.
|
||||
// Run records and logs are a later slice.
|
||||
//
|
||||
// The fixed field list (SNAPSHOT_FIELDS names the top level):
|
||||
// snapshotVersion 1
|
||||
// at when the Console composed it (ISO time)
|
||||
// console { state: "answering", at } the Console wrote this, so it answered at `at`
|
||||
// proxy { initialized, installed, active, healthy, fence } or "not read"
|
||||
// systemConfig { valid, exit, code } or "not read"
|
||||
// organizations { count, list: [ { id, bus, processing, config } ] } or "not read"
|
||||
// bus { state: running | not-answering | stopped | dead, lastStart: { ok, at } | null } or "not read"
|
||||
// processing { halted, invalid } or "not read"
|
||||
// config { valid, exit, code } or "not read"
|
||||
//
|
||||
// The system config check runs scripts/mosaic-config.mjs validate in a child. Its stdout is the
|
||||
// resolved config, so it is never read (stdio "ignore"), and loadSystem is not used. Its stderr
|
||||
// names the config path and data-root facts, so only its first line's leading word is matched
|
||||
// against SYSTEM_CONFIG_CODES; the text itself never leaves this module.
|
||||
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { spawn } from "node:child_process";
|
||||
import { chmodSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { join, resolve, dirname } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { ID_PATTERN } from "../../organization/src/vocabulary.mjs";
|
||||
|
||||
const REPO = resolve(dirname(fileURLToPath(import.meta.url)), "..", "..", "..");
|
||||
export const CONFIG_SCRIPT = join(REPO, "scripts", "mosaic-config.mjs");
|
||||
export const ORG_CONFIG_SCRIPT = join(REPO, "packages", "organization", "src", "cli.mjs");
|
||||
export const NOT_READ = "not read";
|
||||
export const SNAPSHOT_VERSION = 1;
|
||||
export const SNAPSHOT_FILE = "snapshot.json";
|
||||
export const SNAPSHOT_FIELDS = Object.freeze(["snapshotVersion", "at", "console", "proxy", "systemConfig", "organizations"]);
|
||||
export const BUS_STATES = Object.freeze(["running", "not-answering", "stopped", "dead"]);
|
||||
const UNIT_ACTIVE = Object.freeze(["active", "inactive", "failed", "activating", "deactivating", "reloading", "unknown"]);
|
||||
export const READ_TIMEOUT_MS = 5000;
|
||||
export const MAX_ORGS = 200;
|
||||
// At most this many organizations' config checks run at once; each starts a node child.
|
||||
export const ORG_READS_AT_ONCE = 4;
|
||||
|
||||
// What a failed system config check may say. Each refusal code the validator prints as its first
|
||||
// word passes as itself; a known sentence maps to a fixed code; anything else is the exit's general
|
||||
// code. Nothing else of stderr is kept.
|
||||
const PRINTED_CODES = Object.freeze([
|
||||
"instance-config-mismatch", "instance-name-invalid", "instance-root-unclaimed", "instance-root-mismatch",
|
||||
"proxy-rotate-grace-invalid", "proxy-first-output-invalid", "proxy-config-invalid",
|
||||
]);
|
||||
const SENTENCES = Object.freeze([
|
||||
["configuration not found", "config-not-found"],
|
||||
["configuration is not valid JSON", "config-not-json"],
|
||||
["configuration path must be a regular", "config-not-regular-file"],
|
||||
["configuration file is not readable", "config-unreadable"],
|
||||
["configuration must be a JSON object", "config-not-object"],
|
||||
["unsupported configVersion", "config-version-unsupported"],
|
||||
["unsupported environment", "config-environment-unsupported"],
|
||||
['execution "dataRoot"', "config-data-root-invalid"],
|
||||
["unsupported execution.", "config-execution-unsupported"],
|
||||
["unsupported ", "config-key-unsupported"],
|
||||
]);
|
||||
const EXIT_CODES = Object.freeze({ 1: "config-check-failed", 2: "config-invalid", 3: "config-missing-or-unclaimed", 4: "config-check-usage" });
|
||||
export const SYSTEM_CONFIG_CODES = Object.freeze([...new Set([...PRINTED_CODES, ...SENTENCES.map(([, c]) => c), ...Object.values(EXIT_CODES), "config-check-failed"])]);
|
||||
|
||||
// The org config check (`mosaic org config validate ID`) is read by its exit code alone.
|
||||
const ORG_EXIT_CODES = Object.freeze({ 2: "org-config-invalid", 3: "org-config-system-unreadable", 4: "org-config-check-usage" });
|
||||
export const ORG_CONFIG_CODES = Object.freeze([...Object.values(ORG_EXIT_CODES), "org-config-check-failed"]);
|
||||
|
||||
// stderr's first line, already without its "mosaic-config: " prefix, to one fixed code.
|
||||
export function systemConfigCode(exit, firstLine) {
|
||||
if (exit === 0) return null;
|
||||
const line = typeof firstLine === "string" ? firstLine.replace(/^mosaic-config: /, "") : "";
|
||||
const word = line.match(/^([a-z][a-z0-9-]{2,63})(?::|$)/)?.[1];
|
||||
if (word && PRINTED_CODES.includes(word)) return word;
|
||||
for (const [start, code] of SENTENCES) if (line.startsWith(start)) return code;
|
||||
return EXIT_CODES[exit] ?? "config-check-failed";
|
||||
}
|
||||
|
||||
// A child run for its exit code. stdout is never opened. stderr is read only when `stderr` is
|
||||
// true, at most 4 KiB, and only its first line is returned to the caller in this module.
|
||||
export function runExitOnly(argv, { env, timeoutMs = READ_TIMEOUT_MS, stderr = false } = {}) {
|
||||
return new Promise((done) => {
|
||||
let child;
|
||||
try {
|
||||
child = spawn(argv[0], argv.slice(1), { env, stdio: ["ignore", "ignore", stderr ? "pipe" : "ignore"] });
|
||||
} catch {
|
||||
return done(null);
|
||||
}
|
||||
let err = "";
|
||||
let settled = false;
|
||||
const finish = (value) => {
|
||||
if (settled) return;
|
||||
settled = true;
|
||||
clearTimeout(timer);
|
||||
done(value);
|
||||
};
|
||||
const timer = setTimeout(() => {
|
||||
try { child.kill("SIGKILL"); } catch {}
|
||||
finish(null);
|
||||
}, timeoutMs);
|
||||
child.stderr?.on("data", (b) => {
|
||||
if (err.length < 4096) err += b.toString("utf8").slice(0, 4096 - err.length);
|
||||
});
|
||||
child.on("error", () => finish(null));
|
||||
child.on("close", (code, signal) => finish(signal || !Number.isInteger(code) ? null : { exit: code, firstLine: err.split("\n")[0] }));
|
||||
});
|
||||
}
|
||||
|
||||
// { valid, exit, code } from the validator, or "not read" when it did not run to an exit.
|
||||
export async function readSystemConfig({ env = process.env, run = runExitOnly, node = process.execPath, timeoutMs = READ_TIMEOUT_MS } = {}) {
|
||||
const r = await run([node, CONFIG_SCRIPT, "validate"], { env, timeoutMs, stderr: true });
|
||||
if (!r || !Number.isInteger(r.exit)) return NOT_READ;
|
||||
const code = systemConfigCode(r.exit, r.firstLine);
|
||||
return { valid: r.exit === 0, exit: r.exit, code };
|
||||
}
|
||||
|
||||
export async function readOrgConfig(id, { env = process.env, run = runExitOnly, node = process.execPath, timeoutMs = READ_TIMEOUT_MS } = {}) {
|
||||
if (typeof id !== "string" || !ID_PATTERN.test(id)) return NOT_READ;
|
||||
const r = await run([node, ORG_CONFIG_SCRIPT, "validate", id], { env, timeoutMs, stderr: false });
|
||||
if (!r || !Number.isInteger(r.exit)) return NOT_READ;
|
||||
return { valid: r.exit === 0, exit: r.exit, code: r.exit === 0 ? null : ORG_EXIT_CODES[r.exit] ?? "org-config-check-failed" };
|
||||
}
|
||||
|
||||
const ISO = /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d{1,9})?Z$/;
|
||||
const isoOrNull = (v) => (typeof v === "string" && ISO.test(v) ? v : null);
|
||||
const bool = (v) => (typeof v === "boolean" ? v : null);
|
||||
|
||||
// Each read under its own deadline; a throw, a timeout or a null is "not read".
|
||||
async function bounded(fn, timeoutMs) {
|
||||
let timer;
|
||||
try {
|
||||
const value = await Promise.race([
|
||||
Promise.resolve().then(fn),
|
||||
new Promise((resolve) => { timer = setTimeout(() => resolve(NOT_READ), timeoutMs); }),
|
||||
]);
|
||||
return value ?? NOT_READ;
|
||||
} catch {
|
||||
return NOT_READ;
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
// The fixed shapes. Anything outside them is "not read".
|
||||
export function proxyFields(status) {
|
||||
if (!status || typeof status !== "object") return NOT_READ;
|
||||
const unit = status.unit && typeof status.unit === "object" ? status.unit : {};
|
||||
const out = {
|
||||
initialized: bool(status.init?.initialized),
|
||||
installed: bool(unit.installed),
|
||||
active: UNIT_ACTIVE.includes(unit.active) ? unit.active : "unknown",
|
||||
healthy: bool(unit.healthy),
|
||||
fence: bool(status.fence?.set),
|
||||
};
|
||||
return out.initialized === null && out.installed === null && out.healthy === null ? NOT_READ : out;
|
||||
}
|
||||
|
||||
// busStatus (packages/cli/src/org-bus.mjs) answers healthy and pid; a refusal from its state check
|
||||
// (a host it cannot account for, a CliError) is a dead host, as the Console's bus revive reads it.
|
||||
export function busFields(status, refusedDead = false) {
|
||||
if (refusedDead) return { state: "dead", lastStart: null };
|
||||
if (!status || typeof status !== "object" || typeof status.healthy !== "boolean") return NOT_READ;
|
||||
const state = status.healthy ? "running" : Number.isSafeInteger(status.pid) ? "not-answering" : "stopped";
|
||||
const boot = status.boot && typeof status.boot === "object" && typeof status.boot.ok === "boolean" && isoOrNull(status.boot.at)
|
||||
? { ok: status.boot.ok, at: status.boot.at } : null;
|
||||
return { state, lastStart: boot };
|
||||
}
|
||||
|
||||
export function processingFields(p) {
|
||||
if (!p || typeof p !== "object" || typeof p.halted !== "boolean") return NOT_READ;
|
||||
return { halted: p.halted, invalid: p.invalid === true };
|
||||
}
|
||||
|
||||
function configFields(c, codes) {
|
||||
if (!c || typeof c !== "object" || typeof c.valid !== "boolean" || !Number.isInteger(c.exit)) return NOT_READ;
|
||||
return { valid: c.valid, exit: c.exit, code: c.code === null || codes.includes(c.code) ? c.code : null };
|
||||
}
|
||||
|
||||
// readers: { orgs() -> [{ id, state }], busStatus(id) -> status (a CliError, or a throw with
|
||||
// `dead: true`, is a dead host), processing(id), orgConfig(id), proxy(), systemConfig() }. Each may be missing.
|
||||
// fn over items, at most `limit` running at once; the results keep the items' order.
|
||||
async function mapLimit(items, limit, fn) {
|
||||
const out = new Array(items.length);
|
||||
let next = 0;
|
||||
const worker = async () => { while (next < items.length) { const i = next++; out[i] = await fn(items[i]); } };
|
||||
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, worker));
|
||||
return out;
|
||||
}
|
||||
|
||||
// The organization stage runs under one deadline, `timeoutMs` from its start, however many
|
||||
// organizations there are: a read that has not finished by then is "not read", and one not yet
|
||||
// begun is not read at all. The bus and processing reads are socket reads and begin for every
|
||||
// organization at once. Only the config checks, a node child each, wait for one of
|
||||
// ORG_READS_AT_ONCE slots, so a hung check holds back other checks but never another
|
||||
// organization's socket reads. A whole snapshot takes at most about two timeouts.
|
||||
export async function composeSnapshot(readers = {}, { now = () => new Date(), timeoutMs = READ_TIMEOUT_MS, clock = Date.now } = {}) {
|
||||
const at = now().toISOString();
|
||||
const read = (name, ...args) => (typeof readers[name] === "function" ? bounded(() => readers[name](...args), timeoutMs) : Promise.resolve(NOT_READ));
|
||||
const [proxyRaw, systemRaw, orgsRaw] = await Promise.all([read("proxy"), read("systemConfig"), read("orgs")]);
|
||||
let organizations = NOT_READ;
|
||||
if (Array.isArray(orgsRaw)) {
|
||||
const active = orgsRaw.filter((o) => o && typeof o === "object" && o.state === "active" && typeof o.id === "string" && ID_PATTERN.test(o.id)).slice(0, MAX_ORGS);
|
||||
const deadline = clock() + timeoutMs;
|
||||
// fn bounded by what is left of the deadline, or NOT_READ once nothing is.
|
||||
const inTime = (fn) => {
|
||||
const left = deadline - clock();
|
||||
return left > 0 ? bounded(fn, left) : Promise.resolve(NOT_READ);
|
||||
};
|
||||
const readOrg = (name, id) => (typeof readers[name] === "function" ? inTime(() => readers[name](id)) : Promise.resolve(NOT_READ));
|
||||
const sockets = Promise.all(active.map(({ id }) => Promise.all([
|
||||
typeof readers.busStatus === "function"
|
||||
? inTime(async () => {
|
||||
try { return busFields(await readers.busStatus(id)); }
|
||||
catch (e) { return e?.name === "CliError" || e?.dead === true ? busFields(null, true) : NOT_READ; }
|
||||
})
|
||||
: NOT_READ,
|
||||
readOrg("processing", id),
|
||||
])));
|
||||
const configs = await mapLimit(active, ORG_READS_AT_ONCE, ({ id }) => readOrg("orgConfig", id));
|
||||
const socketReads = await sockets;
|
||||
const list = active.map(({ id }, i) => {
|
||||
const [bus, processing] = socketReads[i];
|
||||
return { id, bus: bus === NOT_READ ? NOT_READ : bus, processing: processingFields(processing), config: configFields(configs[i], ORG_CONFIG_CODES) };
|
||||
});
|
||||
organizations = { count: list.length, list };
|
||||
}
|
||||
return {
|
||||
snapshotVersion: SNAPSHOT_VERSION,
|
||||
at,
|
||||
console: { state: "answering", at },
|
||||
proxy: proxyFields(proxyRaw),
|
||||
systemConfig: configFields(systemRaw, SYSTEM_CONFIG_CODES),
|
||||
organizations,
|
||||
};
|
||||
}
|
||||
|
||||
// One file, replaced whole: written beside itself at 0600, then renamed over the last one.
|
||||
export function writeSnapshot(healthDir, snapshot) {
|
||||
const file = join(healthDir, SNAPSHOT_FILE);
|
||||
const temp = join(healthDir, `.${SNAPSHOT_FILE}.${process.pid}.${randomBytes(6).toString("hex")}.tmp`);
|
||||
try {
|
||||
writeFileSync(temp, JSON.stringify(snapshot, null, 2) + "\n", { mode: 0o600 });
|
||||
chmodSync(temp, 0o600);
|
||||
renameSync(temp, file);
|
||||
} finally {
|
||||
rmSync(temp, { force: true });
|
||||
}
|
||||
return file;
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
// #1873 triage-cap: Triage's token usage and its daily cap.
|
||||
//
|
||||
// The Triage host appends one line per turn to <dataRoot>/triage/usage.jsonl:
|
||||
// { at, run, turn, status, input, output, cacheRead, cacheWrite }
|
||||
// A known turn carries four non-negative safe integers; an unknown turn carries four nulls. The file
|
||||
// sits outside the run directory, so a new start never clears it. It is held to the route store's
|
||||
// file checks: a regular file, one link, the running user's, mode 0600. A link, a non-file, a wrong
|
||||
// mode or owner refuses the read and the append alike, and the append leaves the file as it was.
|
||||
// Lines are only ever appended. Nothing here prunes the file.
|
||||
//
|
||||
// The cap is the daily-quota plugin's own rule (packages/plugins/limit-check/daily-quota), called
|
||||
// with the organization limits shape and the stored cap as its one leaf, for one account named
|
||||
// "triage". No counting rule lives here: the count, the window, the DST handling and the hit all
|
||||
// come from mayLaunch and report. nextReset only asks report when the window turns over.
|
||||
import { closeSync, constants, fchmodSync, fstatSync, fsyncSync, openSync, readSync, writeSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { mayLaunch, report } from "../../plugins/limit-check/daily-quota/index.mjs";
|
||||
|
||||
export const USAGE_ACCOUNT = "triage";
|
||||
export const USAGE_FIELDS = Object.freeze(["at", "run", "turn", "status", "input", "output", "cacheRead", "cacheWrite"]);
|
||||
const KINDS = ["input", "output", "cacheWrite", "cacheRead"];
|
||||
const ISO = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
|
||||
// A run id is the host's own triage-<14 digits>; a turn id is the runner's message id, maybe with
|
||||
// ":retry". Both are held to one short, plain shape so a line never carries free text.
|
||||
export const ID = /^[A-Za-z0-9:._-]{1,80}$/;
|
||||
// 32 MiB holds well over a hundred thousand lines; a larger file refuses rather than stalls a read.
|
||||
export const USAGE_MAX_BYTES = 32 * 1024 * 1024;
|
||||
const HOUR = 3600000;
|
||||
|
||||
export class UsageError extends Error {
|
||||
constructor(why) {
|
||||
super(`the Triage usage file is unreadable: ${why}`);
|
||||
this.name = "UsageError";
|
||||
this.code = "triage-usage-unreadable";
|
||||
}
|
||||
}
|
||||
const unreadable = (why) => new UsageError(why);
|
||||
|
||||
export const usagePath = (dataRoot) => join(dataRoot, "triage", "usage.jsonl");
|
||||
|
||||
const plain = (v) => v !== null && typeof v === "object" && !Array.isArray(v) && Object.getPrototypeOf(v) === Object.prototype;
|
||||
const canonicalTime = (s) => typeof s === "string" && ISO.test(s) && !Number.isNaN(Date.parse(s)) && new Date(s).toISOString() === s;
|
||||
const count = (n) => Number.isSafeInteger(n) && n >= 0;
|
||||
|
||||
// One line's shape, exactly.
|
||||
export function validLine(v) {
|
||||
if (!plain(v) || Object.keys(v).length !== USAGE_FIELDS.length || !USAGE_FIELDS.every((k) => Object.hasOwn(v, k))) return false;
|
||||
if (!canonicalTime(v.at) || typeof v.run !== "string" || !ID.test(v.run) || typeof v.turn !== "string" || !ID.test(v.turn)) return false;
|
||||
if (v.status === "known") return KINDS.every((k) => count(v[k]));
|
||||
if (v.status === "unknown") return KINDS.every((k) => v[k] === null);
|
||||
return false;
|
||||
}
|
||||
|
||||
// An opened file's checks, as the route store holds route.json to.
|
||||
function checkFile(fd, uid) {
|
||||
const st = fstatSync(fd);
|
||||
if (!st.isFile()) throw unreadable("usage.jsonl is not a regular file");
|
||||
if (st.nlink !== 1) throw unreadable("usage.jsonl has more than one link");
|
||||
if (st.uid !== uid) throw unreadable("usage.jsonl is not owned by this user");
|
||||
if ((st.mode & 0o777) !== 0o600) throw unreadable("usage.jsonl mode is not 0600");
|
||||
return st;
|
||||
}
|
||||
|
||||
const uidOf = (opts) => opts.uid ?? process.getuid();
|
||||
|
||||
// Append one line. A torn last line (a write that died) is closed off with a newline first, so the
|
||||
// new line always stands alone and the torn one stays one bad line.
|
||||
export function appendUsage(file, line, opts = {}) {
|
||||
if (!validLine(line)) throw unreadable("the line does not match the usage shape");
|
||||
const text = JSON.stringify(Object.fromEntries(USAGE_FIELDS.map((k) => [k, line[k]]))) + "\n";
|
||||
const flags = constants.O_RDWR | constants.O_APPEND | constants.O_NOFOLLOW | constants.O_NONBLOCK;
|
||||
let fd;
|
||||
try { fd = openSync(file, flags); }
|
||||
catch (e) {
|
||||
if (e.code === "ELOOP") throw unreadable("usage.jsonl is a link");
|
||||
if (e.code !== "ENOENT") throw unreadable("usage.jsonl cannot be opened");
|
||||
try { fd = openSync(file, flags | constants.O_CREAT | constants.O_EXCL, 0o600); }
|
||||
catch { throw unreadable("usage.jsonl cannot be made"); }
|
||||
// The umask may have narrowed the mode; set it on the descriptor just made.
|
||||
try { fchmodSync(fd, 0o600); } catch { closeSync(fd); throw unreadable("usage.jsonl cannot be made"); }
|
||||
}
|
||||
try {
|
||||
const st = checkFile(fd, uidOf(opts));
|
||||
let lead = "";
|
||||
if (st.size > 0) {
|
||||
const last = Buffer.alloc(1);
|
||||
if (readSync(fd, last, 0, 1, st.size - 1) !== 1) throw unreadable("usage.jsonl cannot be read");
|
||||
if (last[0] !== 0x0a) lead = "\n";
|
||||
}
|
||||
try {
|
||||
writeSync(fd, lead + text);
|
||||
fsyncSync(fd);
|
||||
} catch { throw unreadable("usage.jsonl cannot be written"); }
|
||||
} finally { closeSync(fd); }
|
||||
}
|
||||
|
||||
const nulls = { input: null, output: null, cacheRead: null, cacheWrite: null };
|
||||
|
||||
// Every line as a daily-quota record for the triage account. A line that does not parse or does
|
||||
// not match the shape (a torn last line, an edit by hand) is refused: it becomes one unknown turn,
|
||||
// keyed by its line number, dated by the next good line (or the file's change time when none
|
||||
// follows), and is listed in `bad` so the host can log it. No file reads as no turns.
|
||||
export function readUsage(file, opts = {}) {
|
||||
const maxBytes = opts.maxBytes ?? USAGE_MAX_BYTES;
|
||||
let fd;
|
||||
try { fd = openSync(file, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK); }
|
||||
catch (e) {
|
||||
if (e.code === "ENOENT") return { records: [], bad: [] };
|
||||
if (e.code === "ELOOP") throw unreadable("usage.jsonl is a link");
|
||||
throw unreadable("usage.jsonl cannot be opened");
|
||||
}
|
||||
let st;
|
||||
let buf;
|
||||
try {
|
||||
st = checkFile(fd, uidOf(opts));
|
||||
if (st.size > maxBytes) throw unreadable("usage.jsonl is too large");
|
||||
buf = Buffer.alloc(st.size);
|
||||
let got = 0;
|
||||
while (got < st.size) {
|
||||
const n = readSync(fd, buf, got, st.size - got, got);
|
||||
if (n === 0) break;
|
||||
got += n;
|
||||
}
|
||||
buf = buf.subarray(0, got);
|
||||
} finally { closeSync(fd); }
|
||||
const lines = buf.toString("utf8").split("\n");
|
||||
if (lines.at(-1) === "") lines.pop();
|
||||
const parsed = lines.map((text) => {
|
||||
try {
|
||||
const v = JSON.parse(text);
|
||||
return validLine(v) ? v : null;
|
||||
} catch { return null; }
|
||||
});
|
||||
const records = new Array(parsed.length);
|
||||
const bad = [];
|
||||
let nextAt = st.mtime.toISOString();
|
||||
for (let i = parsed.length - 1; i >= 0; i--) {
|
||||
const v = parsed[i];
|
||||
if (v) {
|
||||
records[i] = { account: USAGE_ACCOUNT, ...v };
|
||||
nextAt = v.at;
|
||||
} else {
|
||||
records[i] = { account: USAGE_ACCOUNT, at: nextAt, run: null, turn: `line-${i + 1}`, status: "unknown", ...nulls };
|
||||
bad.unshift({ line: i + 1 });
|
||||
}
|
||||
}
|
||||
return { records, bad, ino: st.ino };
|
||||
}
|
||||
|
||||
// The organization limits shape with the stored cap as its one leaf.
|
||||
export const limitsFor = (cap) => ({ tokens: { daily: { [USAGE_ACCOUNT]: cap } } });
|
||||
const SUBJECT = Object.freeze({ account: USAGE_ACCOUNT });
|
||||
const iso = (ms) => new Date(ms).toISOString();
|
||||
|
||||
// The next reset after nowMs, or after the cap's `since` when that is later. A probe record at that
|
||||
// instant counts while the window holding t still starts at or before it; the reset is the first
|
||||
// millisecond at which report stops counting it. A day is at most 25 hours, so 49 hours always
|
||||
// holds the next one.
|
||||
export function nextReset(cap, nowMs) {
|
||||
const from = Math.max(nowMs, Date.parse(cap.since));
|
||||
const limits = limitsFor(cap);
|
||||
const probe = [{ account: USAGE_ACCOUNT, at: iso(from), run: "probe", turn: "probe", status: "known", input: 1, output: 0, cacheRead: 0, cacheWrite: 0 }];
|
||||
const counts = (t) => report({ limits, records: probe, now: iso(t) })[0].count > 0;
|
||||
let lo = from;
|
||||
let hi = from + 49 * HOUR;
|
||||
if (counts(hi)) throw unreadable("the cap has no reset in the next two days");
|
||||
while (hi - lo > 1) {
|
||||
const mid = Math.floor((lo + hi) / 2);
|
||||
if (counts(mid)) lo = mid; else hi = mid;
|
||||
}
|
||||
return hi;
|
||||
}
|
||||
|
||||
// Today's count, the decision and the next reset for the stored cap.
|
||||
export function capState({ cap, records, now }) {
|
||||
const at = iso(now);
|
||||
const limits = limitsFor(cap);
|
||||
const decision = mayLaunch({ limits, subject: SUBJECT, records, now: at });
|
||||
const [r] = report({ limits, records, now: at });
|
||||
return {
|
||||
allow: decision.allow,
|
||||
hit: decision.allow ? null : decision.hit,
|
||||
usageToday: { count: r.count, ...r.kinds, unknownTurns: r.unknownTurns },
|
||||
resetsAt: iso(nextReset(cap, now)),
|
||||
};
|
||||
}
|
||||
|
||||
// The turn delta, copied from packages/bus/src/usage.mjs (snapshot, delta; not exported there and
|
||||
// that package is not this lane's to change). Same codes, same rules: the turn's usage is the sum
|
||||
// of the transcript entries that are new since the turn began; a changed or missing earlier entry
|
||||
// is "transcript-rewritten", and no new entry is "no-new-usage".
|
||||
const unknown = (code) => ({ status: "unknown", code });
|
||||
export function snapshot(value) {
|
||||
if (value?.status === "unknown") return unknown(/^transcript-(path|size|line|entries|records|changed|parse|usage|harness)$/.test(value.code) ? value.code : "source-unknown");
|
||||
if (value?.status !== "known" || !value.entries || typeof value.entries !== "object" || Array.isArray(value.entries)) return unknown("source-invalid");
|
||||
const entries = Object.entries(value.entries);
|
||||
if (entries.length > 20000) return unknown("source-invalid");
|
||||
for (const [id, v] of entries) {
|
||||
if (id.length > 512 || !v || Object.keys(v).length !== 4 || KINDS.some((k) => !Number.isSafeInteger(v[k]) || v[k] < 0)) return unknown("source-invalid");
|
||||
}
|
||||
return { status: "known", entries: value.entries };
|
||||
}
|
||||
export function delta(before, after) {
|
||||
if (!before || before.status !== "known") return unknown("baseline-unknown");
|
||||
if (after.status !== "known") return after;
|
||||
for (const [id, v] of Object.entries(before.entries)) {
|
||||
if (!Object.hasOwn(after.entries, id) || KINDS.some((k) => after.entries[id][k] !== v[k])) return unknown("transcript-rewritten");
|
||||
}
|
||||
const sum = { input: 0, output: 0, cacheWrite: 0, cacheRead: 0 };
|
||||
let added = false;
|
||||
for (const [id, v] of Object.entries(after.entries)) {
|
||||
if (Object.hasOwn(before.entries, id)) continue;
|
||||
added = true;
|
||||
for (const k of KINDS) {
|
||||
sum[k] += v[k];
|
||||
if (!Number.isSafeInteger(sum[k])) return unknown("usage-overflow");
|
||||
}
|
||||
}
|
||||
return added ? { status: "known", ...sum } : unknown("no-new-usage");
|
||||
}
|
||||
@@ -18,7 +18,7 @@ const at = minutesAgo => new Date(Date.now() - minutesAgo * 60000).toISOString()
|
||||
const HIRE = '0f3c1a2b-4d5e-4f60-8a7b-9c0d1e2f3a4b';
|
||||
const THREAD = '7b1e2c3d-4f50-4a61-8b72-9c83d4e5f6a7';
|
||||
|
||||
function fixture() {
|
||||
function fixture(tweak) {
|
||||
const LIST = [
|
||||
{ id: 'acme', label: 'Acme Studio', theme: 'cobalt' },
|
||||
{ id: 'beta', label: 'Beta Works', theme: 'grove' },
|
||||
@@ -52,6 +52,7 @@ function fixture() {
|
||||
model: {},
|
||||
},
|
||||
};
|
||||
tweak?.(DATA);
|
||||
const value = (id, name) => Promise.resolve(DATA[id]?.[name] ?? (['usage', 'limits'].includes(name) ? {} : []));
|
||||
const opts = id => ({
|
||||
bus: { inbox: () => value(id, 'inbox'), agents: () => value(id, 'agents'), 'thread.claims': () => value(id, 'claims'), tasks: () => value(id, 'tasks'), usage: () => value(id, 'usage'), limits: () => value(id, 'limits'), decisions: () => value(id, 'decisions') },
|
||||
@@ -107,16 +108,26 @@ const STUB = `(() => {
|
||||
if (window.__packsBody) return new Response(window.__packsBody, { status: 200, headers: { 'content-type': 'text/html' } });
|
||||
return answer(200, { writable: false, applied: [{ id: 'coding', origin: 'shipped', project: null, hired: ['research-2'], open: [] }], packs: packs[1] === 'beta' ? [{ id: 'coding', origin: 'shipped', pack: { id: 'coding', label: 'Coding', version: 2, hires: [{ name: 'research-2', definition: 'researcher', reason: 'Research.' }], definitions: { researcher: { template: 'researcher', sha256: '${'a'.repeat(64)}' } } } }] : [] });
|
||||
}
|
||||
// SP19: a test may set __runs[org][name] to the runs the instances read lists for that agent.
|
||||
const inst = u.match(/^\\/api\\/orgs\\/([^/]+)\\/bus\\/instances$/);
|
||||
if (inst && window.__runs?.[inst[1]]) {
|
||||
const got = await real(url, init), v = await got.json();
|
||||
for (const [n, runs] of Object.entries(window.__runs[inst[1]])) if (v?.rows?.[0]?.instances?.[n]) v.rows[0].instances[n].running = runs;
|
||||
return answer(got.status, v);
|
||||
}
|
||||
if (!/\\/agent\\//.test(u) || (init.method || 'GET') === 'GET' && !u.includes('/agent/request')) return real(url, init);
|
||||
window.__posts.push({ url: u, method: init.method || 'GET', body: init.body ?? null });
|
||||
const a = window.__answers.shift();
|
||||
if (!a) throw new TypeError('no answer queued');
|
||||
// SP19: an answer may wait on a promise (\`hold\`) and run a step once it is given (\`then\`).
|
||||
if (a.hold) await a.hold;
|
||||
a.then?.();
|
||||
return answer(a.status ?? 200, a.body);
|
||||
};
|
||||
})();`;
|
||||
|
||||
async function setup(t, { viewport = [1440, 900], hash = '#/agents', stub = true, scripts = [] } = {}) {
|
||||
const server = await startServer({ port: 0, sso: createDevOwner(), organizations: fixture(), log: () => {} });
|
||||
async function setup(t, { viewport = [1440, 900], hash = '#/agents', stub = true, scripts = [], data } = {}) {
|
||||
const server = await startServer({ port: 0, sso: createDevOwner(), organizations: fixture(data), log: () => {} });
|
||||
t.after(() => close(server));
|
||||
const b = await browser({ machine: false }); t.after(() => b.close());
|
||||
await b.viewport(...viewport);
|
||||
@@ -185,9 +196,12 @@ test('the drawer shows each field from its read: name, organization, role and so
|
||||
// Decisions waiting on a human that it raised, each into the Inbox. Chief's is not its own.
|
||||
const decisions = await b.evaluate(`[...document.querySelectorAll('${d} [data-drawer-decisions] li a')].map(a => [a.textContent.trim(), a.getAttribute("href")])`);
|
||||
assert.deepEqual(decisions, [['Ship the hero copy?', '#/inbox?d=acme%3Adec-1']]);
|
||||
// SP16: no read gives the time of an agent's last activity. The agents read's time (when it took the
|
||||
// role) and `since` (when its present state began) are not that, so the drawer says not recorded.
|
||||
assert.equal(await text(`${d} [data-drawer-activity]`), 'not recorded');
|
||||
// SP18: the agents read lists only roles a run holds now, and its time is when that run took the role,
|
||||
// so Last activity is when coder's running run took the role, 40 minutes before the read, with the time itself.
|
||||
assert.match(await text(`${d} [data-drawer-activity]`), /^its run took the role 4[01] min ago$/);
|
||||
const started = await attr(`${d} [data-drawer-activity] time`, 'datetime');
|
||||
assert.ok(Math.abs(Date.now() - Date.parse(started) - 40 * 60000) < 90000, started);
|
||||
assert.match(await attr(`${d} [data-drawer-activity] time`, 'title'), /\d/);
|
||||
assert.equal(await b.evaluate(`document.querySelector('${d} [data-drawer-activity]').closest('.ws-drawer-f').querySelector('dt').textContent`), 'Last activity');
|
||||
// Not a pack hire: no packs read, and no Hired from line.
|
||||
assert.equal(await has(`${d} [data-drawer-hired]`), false);
|
||||
@@ -209,6 +223,53 @@ test('a hired agent shows where its role came from; a not-running agent says not
|
||||
assert.match(await text(`${d} [data-drawer-source]`), new RegExp(`hire decision ${HIRE}`));
|
||||
assert.match(await text(`${d} [data-drawer-state]`), /(^|\s)not running$/);
|
||||
assert.equal(await has(`${d} [data-drawer-model]`), false);
|
||||
// SP18: no run holds it, and no read keeps the time of a run that ended: one plain line, no time.
|
||||
assert.equal(await text(`${d} [data-drawer-activity]`), 'not recorded: no run holds it now, and no read keeps when its last run ended');
|
||||
assert.equal(await has(`${d} [data-drawer-activity] time`), false);
|
||||
});
|
||||
|
||||
// SP18 review: a claim dates the agent only when the instances read lists its run as running.
|
||||
test('SP18 review: Last activity counts only a claim whose run is running', async t => {
|
||||
// Coder runs run-2, but the role is still held by run-1, whose process ended: nothing dates run-2 yet.
|
||||
{
|
||||
const { wait, text, has } = await setup(t, { hash: '#/agents?agent=acme%3Acoder', data: d => { d.acme.instances.coder.running = ['run-2']; } });
|
||||
const d = '[data-ws-drawer="acme:coder"]';
|
||||
await wait(`${open('acme:coder')} && /running$/.test(document.querySelector('${d} [data-drawer-state]')?.textContent ?? '')`);
|
||||
assert.equal(await text(`${d} [data-drawer-activity]`), 'not recorded: its run holds no role or conversation yet');
|
||||
assert.equal(await has(`${d} [data-drawer-activity] time`), false);
|
||||
}
|
||||
// Coder runs nothing, and a claim from three days ago is left: not running, and no time from that claim.
|
||||
{
|
||||
const { wait, text, has } = await setup(t, { hash: '#/agents?agent=acme%3Acoder', data: d => { d.acme.instances.coder.running = []; d.acme.agents[0].at = at(3 * 24 * 60); d.acme.agents[0].working = null; } });
|
||||
const d = '[data-ws-drawer="acme:coder"]';
|
||||
await wait(`${open('acme:coder')} && /not running$/.test(document.querySelector('${d} [data-drawer-state]')?.textContent ?? '')`);
|
||||
assert.equal(await text(`${d} [data-drawer-activity]`), 'not recorded: no run holds it now, and no read keeps when its last run ended');
|
||||
assert.equal(await has(`${d} [data-drawer-activity] time`), false);
|
||||
}
|
||||
// Two runs, one holding the role: no read says which run is newer, so the line says one of them.
|
||||
{
|
||||
const { wait, text } = await setup(t, { hash: '#/agents?agent=acme%3Acoder', data: d => { d.acme.instances.coder.running = ['run-1', 'run-2']; } });
|
||||
const d = '[data-ws-drawer="acme:coder"]';
|
||||
await wait(`${open('acme:coder')} && !!document.querySelector('${d} [data-drawer-activity] time')`);
|
||||
assert.match(await text(`${d} [data-drawer-activity]`), /^one of its 2 runs took the role 4[01] min ago$/);
|
||||
}
|
||||
// The instances read fails: nothing checked the claim against a live run, so the line says "a run".
|
||||
{
|
||||
const fail = `(() => { const f = window.fetch; window.fetch = (u, i) => /\\/api\\/orgs\\/acme\\/bus\\/instances$/.test(String(u)) ? Promise.resolve(new Response('{}', { status: 503 })) : f(u, i); })();`;
|
||||
const { wait, text } = await setup(t, { hash: '#/agents?agent=acme%3Acoder', scripts: [fail] });
|
||||
const d = '[data-ws-drawer="acme:coder"]';
|
||||
await wait(`${open('acme:coder')} && /^State not read/.test(document.querySelector('${d} [data-drawer-state]')?.textContent ?? '')`);
|
||||
assert.match(await text(`${d} [data-drawer-activity]`), /^a run took the role 4[01] min ago$/);
|
||||
}
|
||||
// The same failed read for chief, which holds no role and no conversation: the line names both gaps.
|
||||
{
|
||||
const fail = `(() => { const f = window.fetch; window.fetch = (u, i) => /\\/api\\/orgs\\/acme\\/bus\\/instances$/.test(String(u)) ? Promise.resolve(new Response('{}', { status: 503 })) : f(u, i); })();`;
|
||||
const { wait, text, has } = await setup(t, { hash: '#/agents?agent=acme%3Achief', scripts: [fail] });
|
||||
const d = '[data-ws-drawer="acme:chief"]';
|
||||
await wait(`${open('acme:chief')} && /^State not read/.test(document.querySelector('${d} [data-drawer-state]')?.textContent ?? '')`);
|
||||
assert.equal(await text(`${d} [data-drawer-activity]`), 'not recorded: the agents and claims reads show no run holding its role or a conversation, and its runs were not read');
|
||||
assert.equal(await has(`${d} [data-drawer-activity] time`), false);
|
||||
}
|
||||
});
|
||||
|
||||
test('SP16: a pack hire names its pack and the template the packs read gives; a refused read says so', async t => {
|
||||
@@ -501,8 +562,8 @@ test('Message opens the agent\'s thread in an Assistant pane; Stop asks first an
|
||||
assert.equal(await attr('[data-ws-drawer="acme:assistant"] [data-drawer-message]', 'href'), `#/assistant?add=acme%3A${THREAD}`);
|
||||
// SP16: Message is offered, so no line says why it is not.
|
||||
assert.equal(await has('[data-ws-drawer="acme:assistant"] [data-drawer-nomessage]'), false);
|
||||
// It holds a conversation, whose messages read pages from the oldest message: the drawer says it does not read it.
|
||||
assert.equal(await text('[data-ws-drawer="acme:assistant"] [data-drawer-activity]'), 'not shown: reading it means reading the whole conversation');
|
||||
// SP18: a conversation agent's run took its conversation 5 minutes before the read (thread.claims' time).
|
||||
assert.match(await text('[data-ws-drawer="acme:assistant"] [data-drawer-activity]'), /^its run took the conversation [56] min ago$/);
|
||||
// A role agent holds no thread to open, so it has no Message; it can still be stopped.
|
||||
await b.evaluate('location.hash = "#/agents?agent=acme%3Acoder"');
|
||||
await wait(`${open('acme:coder')} && document.querySelector('[data-ws-drawer="acme:coder"] [data-drawer-stop]')`);
|
||||
@@ -577,3 +638,396 @@ test('at 390 wide the drawer is a full-width sheet', async t => {
|
||||
assert.equal(box[1], box[2]);
|
||||
assert.ok(box[3] <= box[2], `no horizontal page scroll: ${box[3]} > ${box[2]}`);
|
||||
});
|
||||
|
||||
// SP18: the sheet is fixed to the screen, but the page under it kept its own height, so the page still
|
||||
// scrolled behind the sheet and a full-page shot showed the sheet stretched down a long blank area. With the
|
||||
// sheet open the page is one screen tall; Close puts the page back where the reader left it.
|
||||
test('SP18: at 390 wide the open sheet is the whole screen, the page under it does not scroll, and Close keeps the place', async t => {
|
||||
const { b, wait } = await setup(t, { viewport: [390, 844], hash: '#/orgs/acme/organization' });
|
||||
const link = JSON.stringify(sessionLink('acme:coder'));
|
||||
await wait(`document.querySelector(${link}) && document.querySelectorAll("[data-org-settings] [data-org-setting]").length > 10`);
|
||||
await b.evaluate(`document.querySelector(${link}).scrollIntoView({ block: 'center' })`);
|
||||
const at = await b.evaluate('Math.round(scrollY)');
|
||||
assert.ok(at > 100, `the row is down the page: ${at}`);
|
||||
await b.evaluate(`document.querySelector(${link}).click()`);
|
||||
await wait(open('acme:coder'));
|
||||
await b.evaluate('new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r)))');
|
||||
const m = await b.evaluate(`(() => { const r = ${drawer('acme:coder')}.getBoundingClientRect(), e = document.documentElement; return { top: Math.round(r.top), bottom: Math.round(r.bottom), left: Math.round(r.left), width: Math.round(r.width), vh: innerHeight, vw: e.clientWidth, docH: e.scrollHeight, bodyH: Math.round(document.body.getBoundingClientRect().height), sw: e.scrollWidth }; })()`);
|
||||
assert.equal(m.top, 0, JSON.stringify(m));
|
||||
assert.equal(m.bottom, m.vh, `the sheet ends at the screen's foot: ${JSON.stringify(m)}`);
|
||||
assert.equal(m.left, 0); assert.equal(m.width, m.vw);
|
||||
assert.ok(m.docH <= m.vh && m.bodyH <= m.vh, `the page under the sheet is one screen tall, nothing below to scroll to: ${JSON.stringify(m)}`);
|
||||
assert.ok(m.sw <= m.vw, 'no sideways scroll');
|
||||
// A scroll on the page does nothing while the sheet is open.
|
||||
await b.evaluate('window.scrollTo(0, 2000)');
|
||||
assert.equal(await b.evaluate('Math.round(scrollY)'), 0);
|
||||
await b.evaluate(`${drawer('acme:coder')}.querySelector("[data-drawer-close]").click()`);
|
||||
await wait(noDrawer);
|
||||
await b.evaluate('new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r)))');
|
||||
assert.ok(Math.abs(await b.evaluate('Math.round(scrollY)') - at) <= 1, `Close puts the page back at ${at}: ${await b.evaluate('Math.round(scrollY)')}`);
|
||||
assert.ok(await b.evaluate('document.documentElement.scrollHeight') > 844, 'the page is its full height again');
|
||||
// At 1440 the drawer is a side panel: the page keeps its height and scrolls.
|
||||
await b.viewport(1440, 900);
|
||||
await b.evaluate(`document.querySelector(${link}).click()`);
|
||||
await wait(open('acme:coder'));
|
||||
assert.ok(await b.evaluate('document.documentElement.scrollHeight') > 900, 'at 1440 the page beside the drawer keeps its height');
|
||||
});
|
||||
|
||||
// SP18 review: only Close (or Back to the same page) puts the page back. A link in the sheet goes to a new
|
||||
// page, which starts at its top, and the place kept for the page under the sheet is not used later either.
|
||||
// The Task page is drawn through afterPaint; a project address is drawn by bus.js's chat branch without it
|
||||
// (this fixture has no chat backend, so that page is the Projects list saying the route is invalid).
|
||||
for (const [label, sel, hashStart, ready] of [
|
||||
['Task', '[data-drawer-task] a', '#/orgs/acme/tasks?task=', 'document.querySelector("[data-tasks-board]")'],
|
||||
['Project', '[data-drawer-project] a', '#/orgs/acme/projects/p-site', 'document.querySelector("#bus-view h1")?.textContent.trim() === "Projects"'],
|
||||
]) test(`SP18 review: at 390 the sheet's ${label} link opens its page at the top, and a later page does not jump`, async t => {
|
||||
const { b, wait } = await setup(t, { viewport: [390, 844], hash: '#/orgs/acme/organization' });
|
||||
const link = JSON.stringify(sessionLink('acme:coder'));
|
||||
await wait(`document.querySelector(${link}) && document.querySelectorAll("[data-org-settings] [data-org-setting]").length > 10`);
|
||||
// Every page is long, so a jump to the kept place would show wherever it lands.
|
||||
await b.evaluate('document.head.insertAdjacentHTML("beforeend", "<style>#bus-view{padding-bottom:4000px}</style>")');
|
||||
await b.evaluate(`document.querySelector(${link}).scrollIntoView({ block: 'center' })`);
|
||||
const at = await b.evaluate('Math.round(scrollY)');
|
||||
assert.ok(at > 100, `the row is down the page: ${at}`);
|
||||
await b.evaluate(`document.querySelector(${link}).click()`);
|
||||
await wait(open('acme:coder'));
|
||||
await b.evaluate(`${drawer('acme:coder')}.querySelector(${JSON.stringify(sel)}).click()`);
|
||||
await wait(`${noDrawer} && location.hash.startsWith(${JSON.stringify(hashStart)}) && ${ready}`);
|
||||
await b.evaluate('new Promise(r => setTimeout(r, 500))');
|
||||
const y = await b.evaluate('Math.round(scrollY)');
|
||||
assert.ok(y <= 1, `the ${label} page is at its top, not at ${at}: ${y}`);
|
||||
// The next page with no drawer does not take the kept place either.
|
||||
await b.evaluate('location.hash = "#/orgs/acme/organization"');
|
||||
await wait('document.querySelectorAll("[data-org-settings] [data-org-setting]").length > 10');
|
||||
await b.evaluate('new Promise(r => setTimeout(r, 500))');
|
||||
const later = await b.evaluate('Math.round(scrollY)');
|
||||
assert.ok(later <= 1, `the organization page opens at its top: ${later}`);
|
||||
});
|
||||
|
||||
// #1873 triage-followups: the page focuses its heading when its first render ends. On load the page
|
||||
// starts a second render that reads GET /api/workspace again, so when the first render's read is slow
|
||||
// the second draws the rows first and a reader can open a drawer before the first render ends. Its late
|
||||
// end must not take the drawer's focus. The page holds the first GET /api/workspace until __go.
|
||||
const HOLD_FIRST = `(() => { const f = window.fetch; let first = true; window.__firstDone = false; window.fetch = (u, o) => { if (first && String(u?.url ?? u).includes('/api/workspace')) { first = false; return new Promise(r => { const t = setInterval(() => { if (window.__go) { clearInterval(t); r(f(u, o).finally(() => setTimeout(() => { window.__firstDone = true; }, 0))); } }, 20); }); } return f(u, o); }; })()`;
|
||||
|
||||
test('triage-followups: a drawer opened before the first render ends keeps focus when that render ends', async t => {
|
||||
const { b, wait } = await setup(t, { scripts: [HOLD_FIRST] });
|
||||
await wait(`document.querySelector(${JSON.stringify(rowLink('acme:coder'))})`);
|
||||
assert.notEqual(await b.evaluate('document.activeElement?.tagName'), 'H1', 'the first render has not ended');
|
||||
await b.evaluate(`document.querySelector(${JSON.stringify(rowLink('acme:coder'))}).click()`);
|
||||
await wait(`${open('acme:coder')} && ${drawer('acme:coder')}.contains(document.activeElement)`);
|
||||
await b.evaluate('window.__go = true');
|
||||
await wait('window.__firstDone');
|
||||
await new Promise(r => setTimeout(r, 400));
|
||||
assert.equal(await b.evaluate(`${drawer('acme:coder')}.contains(document.activeElement)`), true, `focus stays in the drawer, not on ${await b.evaluate('document.activeElement?.tagName')}`);
|
||||
});
|
||||
|
||||
// SP19 (#1873): Restart in the drawer, offered only where Stop is. It reuses Stop's confirm step and
|
||||
// status line, ends the agent through its own organization's /agent/terminate, waits for the request to
|
||||
// close and for its runs to reach 0, then launches it through /agent/launch. It never regenerates. A
|
||||
// conversation agent is ended and not launched. The page's 2 s poll steps run every 20 ms here.
|
||||
const FAST = `(() => { const st = window.setTimeout.bind(window); window.setTimeout = (f, ms, ...a) => st(f, ms === 2000 ? 20 : ms, ...a); })()`;
|
||||
const END = '5e000000-0000-4000-8000-0000000000e1', LAUNCH = '5e000000-0000-4000-8000-0000000000a1';
|
||||
const closed = (request, action, extra = {}) => ({ request, instance: 'coder', action, state: 'closed', done: { run: null, transaction: null, refusal: null }, cancelled: null, ...extra });
|
||||
// Every distinct state of the drawer's actions, from now on: [status, Restart's label, Restart disabled, Stop disabled].
|
||||
const WATCH = id => `(() => { window.__seen = []; const snap = () => { const d = document.querySelector('[data-ws-drawer="${id}"]'); if (!d) return; const r = d.querySelector('[data-drawer-restart]'), s = d.querySelector('[data-drawer-stop]'); const v = [d.querySelector('[data-drawer-status]')?.textContent ?? '', r ? r.firstChild.textContent : null, !!r?.disabled, !!s?.disabled]; const l = window.__seen.at(-1); if (!l || l.join('|') !== v.join('|')) window.__seen.push(v); }; snap(); new MutationObserver(snap).observe(document.getElementById('bus-view'), { subtree: true, childList: true, characterData: true, attributes: true }); return true; })()`;
|
||||
const status = d => `(document.querySelector('${d} [data-drawer-status]')?.textContent ?? "")`;
|
||||
const usable = d => `document.querySelector('${d} [data-drawer-restart]')?.disabled === false && document.querySelector('${d} [data-drawer-stop]')?.disabled === false && !document.querySelector('${d} .ws-drawer-confirm')`;
|
||||
|
||||
test('SP19: Restart asks first, then ends and launches the agent in its own organization, and never regenerates', async t => {
|
||||
// The reader was last on Beta Works, so the Console's chosen organization is beta. #/agents shows every
|
||||
// organization's agents; the drawer's own organization is used, not the chosen one.
|
||||
const { b, wait, text } = await setup(t, { hash: '#/orgs/beta/organization', scripts: [FAST] });
|
||||
await wait(`window.MosaicOrg?.id === 'beta' && document.querySelectorAll("[data-org-settings] [data-org-setting]").length > 0`);
|
||||
await b.evaluate('location.hash = "#/agents?agent=acme%3Acoder"');
|
||||
const d = '[data-ws-drawer="acme:coder"]';
|
||||
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-restart]')`);
|
||||
assert.equal(await b.evaluate('window.MosaicOrg?.id ?? null'), 'beta');
|
||||
// Beside Stop, in the same action group as the status line.
|
||||
assert.equal(await b.evaluate(`document.querySelector('${d} [data-drawer-restart]').closest('[data-drawer-actions]') === document.querySelector('${d} [data-drawer-stop]').closest('[data-drawer-actions]')`), true);
|
||||
assert.equal(await b.evaluate(`document.querySelector('${d} [data-drawer-restart]').textContent`), 'Restart coder in Acme Studio');
|
||||
// It asks first, in Stop's confirm step. Cancel sends nothing and puts focus back on Restart.
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-restart]').click()`);
|
||||
await wait(`document.querySelector('${d} [data-drawer-restart-yes]')`);
|
||||
assert.match(await text(`${d} [data-drawer-restart-confirm]`), /^Restart coder in Acme Studio\? It ends its session and launches it again\. Restart coder Cancel$/);
|
||||
assert.equal(await b.evaluate(`document.querySelector('${d} [data-drawer-restart-confirm]').classList.contains('ws-drawer-confirm')`), true);
|
||||
assert.deepEqual(await b.evaluate(`[document.querySelector('${d} [data-drawer-restart]').disabled, document.querySelector('${d} [data-drawer-stop]').disabled]`), [true, true]);
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-restart-no]').click()`);
|
||||
await wait(`document.activeElement === document.querySelector('${d} [data-drawer-restart]') && ${usable(d)}`);
|
||||
// Escape cancels the question too, and the drawer stays.
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-restart]').click()`);
|
||||
await wait(`document.querySelector('${d} [data-drawer-restart-no]') === document.activeElement`);
|
||||
await b.key('Escape');
|
||||
await wait(`document.activeElement === document.querySelector('${d} [data-drawer-restart]') && ${usable(d)}`);
|
||||
assert.equal(await b.evaluate(agentParam), 'acme:coder');
|
||||
assert.deepEqual(await b.evaluate('window.__posts'), []);
|
||||
// Yes: one terminate, its request followed until it closes, the runs read until none is left, one launch.
|
||||
await b.evaluate(`window.__answers.push(
|
||||
{ body: { request: '${END}', runs: ['run-1'], cancelled: [], taken: [] }, then: () => { window.__runs = { acme: { coder: [] } }; } },
|
||||
{ body: ${JSON.stringify(closed(END, 'end'))} },
|
||||
{ body: { request: '${LAUNCH}' }, then: () => { window.__runs = { acme: { coder: ['run-2'] } }; } },
|
||||
{ body: ${JSON.stringify(closed(LAUNCH, 'launch', { done: { run: 'run-2', transaction: null, refusal: null } }))} })`);
|
||||
await b.evaluate(WATCH('acme:coder'));
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-restart]').click()`);
|
||||
await wait(`document.querySelector('${d} [data-drawer-restart-yes]')`);
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-restart-yes]').click()`);
|
||||
await wait(`/^Restarted coder \\(run run-2\\)\\.$/.test(${status(d)}) && ${usable(d)}`);
|
||||
const posts = await b.evaluate('window.__posts');
|
||||
assert.deepEqual(posts.map(p => [p.url, p.method]), [
|
||||
['/api/orgs/acme/agent/terminate', 'POST'], [`/api/orgs/acme/agent/request?id=${END}`, 'GET'],
|
||||
['/api/orgs/acme/agent/launch', 'POST'], [`/api/orgs/acme/agent/request?id=${LAUNCH}`, 'GET'],
|
||||
]);
|
||||
assert.deepEqual(JSON.parse(posts[0].body), { instance: 'coder', reason: 'restart from the agent drawer' });
|
||||
assert.deepEqual(JSON.parse(posts[2].body), { instance: 'coder' });
|
||||
assert.equal(posts.some(p => /regenerate/.test(p.url)), false, 'Restart never regenerates');
|
||||
// The status line names each step; while it runs, Restart reads Restarting… and both buttons wait.
|
||||
const seen = await b.evaluate('window.__seen');
|
||||
const steps = seen.map(s => s[0]).filter((s, i, all) => s && s !== all[i - 1]);
|
||||
assert.deepEqual(steps, ['Restarting coder: asking it to end…', 'coder was asked to end. Waiting for its session to end…', 'coder has ended. Launching it…', 'Restarted coder (run run-2).']);
|
||||
for (const [s, label, rd, sd] of seen.filter(s => steps.slice(0, 3).includes(s[0]))) assert.deepEqual([label, rd, sd], ['Restarting…', true, true], s);
|
||||
assert.equal(await b.evaluate(`document.querySelector('${d} [data-drawer-restart]').firstChild.textContent`), 'Restart');
|
||||
});
|
||||
|
||||
// The Assistant is a protected agent, so it has no Stop and no Restart; ideas is a conversation agent that is not.
|
||||
test('SP19: a conversation agent is ended and not launched; its next message starts it', async t => {
|
||||
const data = D => { D.acme.instances.ideas = { definition: 'ideas', running: ['run-4'] }; };
|
||||
const { b, wait } = await setup(t, { hash: '#/agents?agent=acme%3Aideas', scripts: [FAST], data });
|
||||
const d = '[data-ws-drawer="acme:ideas"]';
|
||||
await wait(`${open('acme:ideas')} && document.querySelector('${d} [data-drawer-restart]')`);
|
||||
assert.equal(await b.evaluate(`document.querySelector('${d} [data-drawer-restart]').dataset.drawerThread`), 'yes');
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-restart]').click()`);
|
||||
await wait(`document.querySelector('${d} [data-drawer-restart-yes]')`);
|
||||
assert.match(await b.evaluate(`document.querySelector('${d} [data-drawer-restart-confirm]').textContent`), /^Restart ideas in Acme Studio\? It ends its session; its next message starts it again\./);
|
||||
await b.evaluate(`window.__answers.push(
|
||||
{ body: { request: '${END}', runs: ['run-4'], cancelled: [], taken: [] }, then: () => { window.__runs = { acme: { ideas: [] } }; } },
|
||||
{ body: ${JSON.stringify({ ...closed(END, 'end'), instance: 'ideas' })} })`);
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-restart-yes]').click()`);
|
||||
await wait(`/^Ended ideas at .+\\. Its next message starts it\\.$/.test(${status(d)})`);
|
||||
const posts = await b.evaluate('window.__posts');
|
||||
assert.deepEqual(posts.map(p => [p.url, p.method]), [['/api/orgs/acme/agent/terminate', 'POST'], [`/api/orgs/acme/agent/request?id=${END}`, 'GET']]);
|
||||
assert.deepEqual(JSON.parse(posts[0].body), { instance: 'ideas', reason: 'restart from the agent drawer' });
|
||||
});
|
||||
|
||||
test('SP19: a second click while a restart runs does nothing', async t => {
|
||||
const { b, wait } = await setup(t, { hash: '#/agents?agent=acme%3Acoder', scripts: [FAST] });
|
||||
const d = '[data-ws-drawer="acme:coder"]';
|
||||
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-restart]')`);
|
||||
// The terminate's answer is held, so the restart is still running while the page is clicked again.
|
||||
await b.evaluate(`window.__answers.push({ hold: new Promise(r => { window.__release = r; }), status: 403, body: { error: 'unavailable', sent: false } })`);
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-restart]').click()`);
|
||||
await wait(`document.querySelector('${d} [data-drawer-restart-yes]')`);
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-restart-yes]').click()`);
|
||||
await wait(`window.__posts.length === 1 && document.querySelector('${d} [data-drawer-restart]').disabled`);
|
||||
// Even a Restart button made clickable again starts nothing: no second question, no second request.
|
||||
await b.evaluate(`(() => { const r = document.querySelector('${d} [data-drawer-restart]'), s = document.querySelector('${d} [data-drawer-stop]'); r.disabled = false; s.disabled = false; r.click(); s.click(); return true; })()`);
|
||||
await b.evaluate('new Promise(r => setTimeout(r, 300))');
|
||||
assert.equal(await b.evaluate(`!!document.querySelector('${d} .ws-drawer-confirm')`), false);
|
||||
assert.equal(await b.evaluate('window.__posts.length'), 1);
|
||||
await b.evaluate('window.__release()');
|
||||
await wait(`/^Not restarted:/.test(${status(d)}) && ${usable(d)}`);
|
||||
assert.equal(await b.evaluate('window.__posts.length'), 1);
|
||||
});
|
||||
|
||||
// SP19 review: a restart keeps its own state, so another drawer's question or Stop does not lose its
|
||||
// line, and its drawer still shows it running when the reader comes back.
|
||||
test('SP19: a restart keeps its line and its running state across another drawer\'s Stop', async t => {
|
||||
const data = D => { D.acme.instances.ideas = { definition: 'ideas', running: ['run-4'] }; };
|
||||
const { b, wait } = await setup(t, { hash: '#/agents?agent=acme%3Acoder', scripts: [FAST], data });
|
||||
const d = '[data-ws-drawer="acme:coder"]', i = '[data-ws-drawer="acme:ideas"]';
|
||||
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-restart]')`);
|
||||
await b.evaluate(`window.__answers.push({ hold: new Promise(r => { window.__release = r; }), status: 403, body: { error: 'unavailable', sent: false } })`);
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-restart]').click()`);
|
||||
await wait(`document.querySelector('${d} [data-drawer-restart-yes]')`);
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-restart-yes]').click()`);
|
||||
await wait(`window.__posts.length === 1`);
|
||||
// Ideas asks to stop and is cancelled; back on coder, its restart still shows as running.
|
||||
await b.evaluate('location.hash = "#/agents?agent=acme%3Aideas"');
|
||||
await wait(`${open('acme:ideas')} && document.querySelector('${i} [data-drawer-stop]')`);
|
||||
await b.evaluate(`document.querySelector('${i} [data-drawer-stop]').click()`);
|
||||
await wait(`document.querySelector('${i} [data-drawer-stop-no]')`);
|
||||
await b.evaluate(`document.querySelector('${i} [data-drawer-stop-no]').click()`);
|
||||
await b.evaluate('location.hash = "#/agents?agent=acme%3Acoder"');
|
||||
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-restart]')`);
|
||||
assert.deepEqual(await b.evaluate(`[${status(d)}, document.querySelector('${d} [data-drawer-restart]').firstChild.textContent, document.querySelector('${d} [data-drawer-restart]').disabled, document.querySelector('${d} [data-drawer-stop]').disabled]`),
|
||||
['Restarting coder: asking it to end…', 'Restarting…', true, true]);
|
||||
// Ideas' Stop question is open when coder's restart is refused; coder's drawer still says so after.
|
||||
await b.evaluate('location.hash = "#/agents?agent=acme%3Aideas"');
|
||||
await wait(`${open('acme:ideas')} && document.querySelector('${i} [data-drawer-stop]')`);
|
||||
await b.evaluate(`document.querySelector('${i} [data-drawer-stop]').click()`);
|
||||
await wait(`document.querySelector('${i} [data-drawer-stop-no]')`);
|
||||
await b.evaluate('window.__release()');
|
||||
await b.evaluate('new Promise(r => setTimeout(r, 300))');
|
||||
assert.equal(await b.evaluate(`!!document.querySelector('${i} [data-drawer-stop-confirm]')`), true, 'the other drawer keeps its question');
|
||||
await b.evaluate(`document.querySelector('${i} [data-drawer-stop-no]').click()`);
|
||||
await b.evaluate('location.hash = "#/agents?agent=acme%3Acoder"');
|
||||
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-restart]')`);
|
||||
await wait(`${status(d)} === 'Not restarted: this Console is not the owner of Acme Studio, so it does not restart its agents.' && ${usable(d)}`);
|
||||
assert.equal(await b.evaluate('window.__posts.length'), 1);
|
||||
});
|
||||
|
||||
// Each refusal the two routes can give says so in one plain line. Where the agent still runs, both
|
||||
// buttons are usable again and focus is on Restart; after a refused launch it has no run, so the
|
||||
// repaint drops Stop and Restart, keeps the line and puts focus on the drawer's heading.
|
||||
const ENDED = [{ body: { request: END, runs: ['run-1'], cancelled: [], taken: [] }, runs: [] }, { body: closed(END, 'end') }];
|
||||
for (const [label, answers, line] of [
|
||||
['not the owner', [{ status: 403, body: { error: 'unavailable', sent: false } }],
|
||||
/^Not restarted: this Console is not the owner of Acme Studio, so it does not restart its agents\.$/],
|
||||
['the organization is archived', [{ status: 409, body: { error: 'org-archived', message: 'organization is archived' } }],
|
||||
/^Not restarted: Acme Studio is archived, so its agents are not changed\. Nothing changed\.$/],
|
||||
['the bus is stopped', [{ status: 503, body: { error: 'no-bus-host', message: 'no-bus-host: no-bus-host. Nothing changed.', sent: false } }],
|
||||
/^Not restarted: the bus of Acme Studio is not running\. Nothing changed\.$/],
|
||||
['the end is refused', [{ body: { request: END, runs: ['run-1'], cancelled: [], taken: [] } }, { body: { ...closed(END, 'end', { done: { run: null, transaction: null, refusal: 'instance-kept' } }), reason: 'the instance is kept; unkeep it first' } }],
|
||||
/^Not restarted: coder was not ended \(the instance is kept; unkeep it first\)\.$/],
|
||||
['the launch is refused', [...ENDED, { status: 409, body: { error: 'capacity-full', message: 'capacity-full: a launch limit is reached. Nothing changed.', sent: false } }],
|
||||
/^coder was ended and not launched: capacity-full: a launch limit is reached\. Nothing changed\.$/],
|
||||
['the launch request closes refused', [...ENDED, { body: { request: LAUNCH } }, { body: { ...closed(LAUNCH, 'launch', { done: { run: null, transaction: null, refusal: 'no-route' } }), reason: 'the instance has no harness, model and thinking yet; set its route first' } }],
|
||||
/^coder was ended and not launched: the instance has no harness, model and thinking yet; set its route first\.$/],
|
||||
['its runs never reach 0', [{ body: { request: END, runs: ['run-1'], cancelled: [], taken: [] } }, { body: closed(END, 'end') }],
|
||||
/^Not restarted: coder was asked to end, but its session is still running\. Read the agents again before trying\.$/],
|
||||
]) test(`SP19: a refusal says so in the drawer: ${label}`, async t => {
|
||||
const { b, wait } = await setup(t, { hash: '#/agents?agent=acme%3Acoder', scripts: [FAST] });
|
||||
const d = '[data-ws-drawer="acme:coder"]';
|
||||
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-restart]')`);
|
||||
const queued = JSON.stringify(answers);
|
||||
await b.evaluate(`window.__answers.push(...${queued}.map(a => a.runs ? { ...a, then: () => { window.__runs = { acme: { coder: a.runs } }; } } : a))`);
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-restart]').click()`);
|
||||
await wait(`document.querySelector('${d} [data-drawer-restart-yes]')`);
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-restart-yes]').click()`);
|
||||
const launched = label.startsWith('the launch');
|
||||
await wait(launched ? `/ was ended and not launched: /.test(${status(d)}) && !document.querySelector('${d} [data-drawer-restart]')`
|
||||
: `/^Not restarted: /.test(${status(d)}) && document.querySelector('${d} [data-drawer-restart]')?.disabled === false`);
|
||||
await b.evaluate('new Promise(r => setTimeout(r, 300))');
|
||||
assert.match(await b.evaluate(status(d)), line);
|
||||
if (launched) {
|
||||
assert.deepEqual(await b.evaluate(`[!!document.querySelector('${d} [data-drawer-stop]'), document.querySelector('${d} [data-drawer-nostop]')?.textContent]`), [false, 'It is not running, so there is nothing to stop.']);
|
||||
assert.equal(await b.evaluate(`document.activeElement === document.querySelector('${d} h2')`), true, `focus is on the drawer heading, not ${await b.evaluate('document.activeElement?.outerHTML.slice(0, 80)')}`);
|
||||
} else {
|
||||
assert.equal(await b.evaluate(usable(d)), true, 'both buttons are usable again');
|
||||
assert.equal(await b.evaluate(`document.activeElement === document.querySelector('${d} [data-drawer-restart]')`), true, 'focus is back on Restart');
|
||||
}
|
||||
const posts = await b.evaluate('window.__posts');
|
||||
assert.equal(posts.some(p => /regenerate/.test(p.url)), false);
|
||||
assert.equal(posts.filter(p => p.url.endsWith('/agent/launch')).length, launched ? 1 : 0);
|
||||
assert.equal(await b.evaluate('window.__answers.length'), 0, 'every queued answer was asked for');
|
||||
});
|
||||
|
||||
// SP20 (#1873): an organization refusal of Stop says so in one plain line, the line Restart gives.
|
||||
for (const [label, answer, line] of [
|
||||
['the organization is archived', { status: 409, body: { error: 'org-archived', message: 'organization is archived' } },
|
||||
/^Not stopped: Acme Studio is archived, so its agents are not changed\. Nothing changed\.$/],
|
||||
['the organization is not available', { status: 404, body: { error: 'org-unavailable' } },
|
||||
/^Not stopped: Acme Studio is not available to this Console\. Nothing changed\.$/],
|
||||
]) test(`SP20: a Stop refusal says so in the drawer: ${label}`, async t => {
|
||||
const { b, wait } = await setup(t, { hash: '#/agents?agent=acme%3Acoder' });
|
||||
const d = '[data-ws-drawer="acme:coder"]';
|
||||
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-stop]')`);
|
||||
await b.evaluate(`window.__answers.push(${JSON.stringify(answer)})`);
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-stop]').click()`);
|
||||
await wait(`document.querySelector('${d} [data-drawer-stop-yes]')`);
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-stop-yes]').click()`);
|
||||
await wait(`/^Not stopped: /.test(${status(d)}) && ${usable(d)}`);
|
||||
assert.match(await b.evaluate(status(d)), line);
|
||||
assert.equal(await b.evaluate(`document.activeElement === document.querySelector('${d} [data-drawer-stop]')`), true, 'focus is back on Stop');
|
||||
assert.deepEqual((await b.evaluate('window.__posts')).map(p => [p.url, p.method]), [['/api/orgs/acme/agent/terminate', 'POST']]);
|
||||
});
|
||||
|
||||
// SP20: each Stop keeps its own state, as each restart does. Another drawer's question or Stop does not
|
||||
// lose a running Stop's line or its running state, and a Stop that ends does not drop another drawer's question.
|
||||
test('SP20: two Stops in two drawers each keep their own line and running state', async t => {
|
||||
const data = D => { D.acme.instances.ideas = { definition: 'ideas', running: ['run-4'] }; };
|
||||
const { b, wait } = await setup(t, { hash: '#/agents?agent=acme%3Acoder', data });
|
||||
const d = '[data-ws-drawer="acme:coder"]', i = '[data-ws-drawer="acme:ideas"]';
|
||||
const refused = who => ({ status: 409, body: { error: 'instance-protected', message: `instance-protected: ${who} is protected from ending. Nothing changed.`, sent: false } });
|
||||
const state = x => `[${status(x)}, document.querySelector('${x} [data-drawer-stop]')?.firstChild.textContent, document.querySelector('${x} [data-drawer-stop]')?.disabled, document.querySelector('${x} [data-drawer-restart]')?.disabled]`;
|
||||
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-stop]')`);
|
||||
// Coder's Stop runs: its terminate is held.
|
||||
await b.evaluate(`window.__answers.push({ hold: new Promise(r => { window.__release = r; }), ...${JSON.stringify(refused('coder'))} })`);
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-stop]').click()`);
|
||||
await wait(`document.querySelector('${d} [data-drawer-stop-yes]')`);
|
||||
await b.evaluate(`document.querySelector('${d} [data-drawer-stop-yes]').click()`);
|
||||
await wait(`window.__posts.length === 1`);
|
||||
// Ideas asks to stop and is cancelled; back on coder, its Stop still shows as running.
|
||||
await b.evaluate('location.hash = "#/agents?agent=acme%3Aideas"');
|
||||
await wait(`${open('acme:ideas')} && document.querySelector('${i} [data-drawer-stop]')`);
|
||||
await b.evaluate(`document.querySelector('${i} [data-drawer-stop]').click()`);
|
||||
await wait(`document.querySelector('${i} [data-drawer-stop-no]')`);
|
||||
await b.evaluate(`document.querySelector('${i} [data-drawer-stop-no]').click()`);
|
||||
await b.evaluate('location.hash = "#/agents?agent=acme%3Acoder"');
|
||||
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-stop]')`);
|
||||
assert.deepEqual(await b.evaluate(state(d)), ['Stopping coder…', 'Stopping…', true, true]);
|
||||
// Ideas' Stop question is open when coder's Stop is refused; each drawer keeps its own.
|
||||
await b.evaluate('location.hash = "#/agents?agent=acme%3Aideas"');
|
||||
await wait(`${open('acme:ideas')} && document.querySelector('${i} [data-drawer-stop]')`);
|
||||
await b.evaluate(`document.querySelector('${i} [data-drawer-stop]').click()`);
|
||||
await wait(`document.querySelector('${i} [data-drawer-stop-no]')`);
|
||||
await b.evaluate('window.__release()');
|
||||
await b.evaluate('new Promise(r => setTimeout(r, 300))');
|
||||
await b.evaluate('location.hash = "#/agents?agent=acme%3Acoder"');
|
||||
await wait(`${open('acme:coder')} && /^Not stopped: /.test(${status(d)}) && ${usable(d)}`);
|
||||
assert.equal(await b.evaluate(status(d)), 'Not stopped: instance-protected: coder is protected from ending. Nothing changed.');
|
||||
await b.evaluate('location.hash = "#/agents?agent=acme%3Aideas"');
|
||||
await wait(`${open('acme:ideas')} && document.querySelector('${i} [data-drawer-stop]')`);
|
||||
assert.equal(await b.evaluate(`!!document.querySelector('${i} [data-drawer-stop-yes]')`), true, 'ideas keeps its question');
|
||||
// Ideas' Stop runs and is refused; coder's line is still there after, and ideas' is too.
|
||||
await b.evaluate(`window.__answers.push(${JSON.stringify(refused('ideas'))})`);
|
||||
await b.evaluate(`document.querySelector('${i} [data-drawer-stop-yes]').click()`);
|
||||
await wait(`/^Not stopped: /.test(${status(i)}) && ${usable(i)}`);
|
||||
await b.evaluate('location.hash = "#/agents?agent=acme%3Acoder"');
|
||||
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-stop]')`);
|
||||
assert.deepEqual(await b.evaluate(state(d)), ['Not stopped: instance-protected: coder is protected from ending. Nothing changed.', 'Stop', false, false]);
|
||||
await b.evaluate('location.hash = "#/agents?agent=acme%3Aideas"');
|
||||
await wait(`${open('acme:ideas')} && document.querySelector('${i} [data-drawer-stop]')`);
|
||||
assert.deepEqual(await b.evaluate(state(i)), ['Not stopped: instance-protected: ideas is protected from ending. Nothing changed.', 'Stop', false, false]);
|
||||
assert.deepEqual((await b.evaluate('window.__posts')).map(p => JSON.parse(p.body).instance), ['coder', 'ideas']);
|
||||
});
|
||||
|
||||
// SP20 review: two Stops run at once. One that ends, here the one the bus takes, which repaints the page,
|
||||
// does not end the other's running state or line, and each drawer keeps its own line after.
|
||||
test('SP20: two Stops running at once in two drawers each keep their own state', async t => {
|
||||
const data = D => { D.acme.instances.ideas = { definition: 'ideas', running: ['run-4'] }; };
|
||||
const { b, wait } = await setup(t, { hash: '#/agents?agent=acme%3Acoder', scripts: [FAST], data });
|
||||
const d = '[data-ws-drawer="acme:coder"]', i = '[data-ws-drawer="acme:ideas"]';
|
||||
const REQ = '0f0e0d0c-0b0a-4908-8706-050403020101';
|
||||
const state = x => `[${status(x)}, document.querySelector('${x} [data-drawer-stop]')?.firstChild.textContent, document.querySelector('${x} [data-drawer-stop]')?.disabled, document.querySelector('${x} [data-drawer-restart]')?.disabled]`;
|
||||
const ask = async x => {
|
||||
await b.evaluate(`document.querySelector('${x} [data-drawer-stop]').click()`);
|
||||
await wait(`document.querySelector('${x} [data-drawer-stop-yes]')`);
|
||||
await b.evaluate(`document.querySelector('${x} [data-drawer-stop-yes]').click()`);
|
||||
};
|
||||
const show = async (agent, x) => {
|
||||
await b.evaluate(`location.hash = "#/agents?agent=acme%3A${agent}"`);
|
||||
await wait(`${open(`acme:${agent}`)} && document.querySelector('${x} [data-drawer-stop]')`);
|
||||
};
|
||||
// Both terminates are held: coder's the bus takes, ideas' it refuses.
|
||||
await b.evaluate(`window.__answers.push(
|
||||
{ hold: new Promise(r => { window.__releaseCoder = r; }), status: 200, body: { sent: true, request: '${REQ}' } },
|
||||
{ hold: new Promise(r => { window.__releaseIdeas = r; }), status: 409, body: { error: 'instance-protected', message: 'instance-protected: ideas is protected from ending. Nothing changed.', sent: false } },
|
||||
{ status: 200, body: { request: '${REQ}', state: 'closed' } })`);
|
||||
await wait(`${open('acme:coder')} && document.querySelector('${d} [data-drawer-stop]')`);
|
||||
await ask(d);
|
||||
await wait('window.__posts.length === 1');
|
||||
await show('ideas', i);
|
||||
await ask(i);
|
||||
await wait('window.__posts.length === 2');
|
||||
assert.deepEqual(await b.evaluate(state(i)), ['Stopping ideas…', 'Stopping…', true, true]);
|
||||
// Coder's Stop ends while ideas' drawer is on screen; ideas' Stop still runs.
|
||||
await b.evaluate('window.__releaseCoder()');
|
||||
await wait('window.__posts.length === 3');
|
||||
await b.evaluate('new Promise(r => setTimeout(r, 300))');
|
||||
await wait(`${open('acme:ideas')} && document.querySelector('${i} [data-drawer-stop]')`);
|
||||
assert.deepEqual(await b.evaluate(state(i)), ['Stopping ideas…', 'Stopping…', true, true]);
|
||||
await show('coder', d);
|
||||
assert.deepEqual(await b.evaluate(state(d)), ['Stopped coder.', 'Stop', false, false]);
|
||||
// Ideas' Stop is refused; each drawer keeps its own line.
|
||||
await show('ideas', i);
|
||||
assert.deepEqual(await b.evaluate(state(i)), ['Stopping ideas…', 'Stopping…', true, true]);
|
||||
await b.evaluate('window.__releaseIdeas()');
|
||||
await wait(`/^Not stopped: /.test(${status(i)}) && ${usable(i)}`);
|
||||
assert.deepEqual(await b.evaluate(state(i)), ['Not stopped: instance-protected: ideas is protected from ending. Nothing changed.', 'Stop', false, false]);
|
||||
await show('coder', d);
|
||||
assert.deepEqual(await b.evaluate(state(d)), ['Stopped coder.', 'Stop', false, false]);
|
||||
assert.deepEqual((await b.evaluate('window.__posts')).map(p => [p.url, p.method]), [
|
||||
['/api/orgs/acme/agent/terminate', 'POST'], ['/api/orgs/acme/agent/terminate', 'POST'], [`/api/orgs/acme/agent/request?id=${REQ}`, 'GET']]);
|
||||
assert.deepEqual((await b.evaluate('window.__posts')).slice(0, 2).map(p => JSON.parse(p.body).instance), ['coder', 'ideas']);
|
||||
assert.equal(await b.evaluate('window.__answers.length'), 0, 'every queued answer was asked for');
|
||||
});
|
||||
@@ -125,7 +125,7 @@ test('both drafts survive a refresh render and the other form\'s save; a draft k
|
||||
assert.equal(await b.evaluate('getComputedStyle(document.querySelector("#agent-levels-root-form .filters")).display'), 'grid', 'the level forms keep the Settings form layout');
|
||||
await type(b, 'agent-levels-root-form', { name: 'drafted', vars: 'limits.network=none' });
|
||||
await type(b, 'agent-levels-project-form', { name: 'helper', form: 'narrow', vars: 'limits.tools=read' });
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#settings-nav a[href$=\'settings/agents\'] .settings-dot")'), true, 'the nav marks unsaved changes');
|
||||
assert.equal(await b.evaluate('!!document.querySelector("#settings-nav a[href^=\'#/settings/agents\'] .settings-dot")'), true, 'the nav marks unsaved changes');
|
||||
// A full Settings render, as the ten-second poll does: the edit box is a new node, the drafts are in it.
|
||||
await b.evaluate('document.getElementById("agent-levels-edit").dataset.old = "1"; document.getElementById("refresh").click()');
|
||||
await wait('document.getElementById("agent-levels-edit") && !document.getElementById("agent-levels-edit").dataset.old');
|
||||
|
||||
@@ -218,8 +218,9 @@ test('row 194 N5: agents.js is served as JavaScript and index.html loads it once
|
||||
const s = await setup(t);
|
||||
const js = await get(s, '/agents.js');
|
||||
assert.equal(js.status, 200); assert.match(js.type, /^text\/javascript/);
|
||||
// F3 adds the agent restart receipt and the thread test that Settings > Routing uses.
|
||||
assert.match(js.text, /window\.MosaicAgents = Object\.freeze\(\{ section, restartMarkup, isThread: definition => threadDefinitions\.has\(definition\) \}\)/);
|
||||
// F3 adds the agent restart receipt and the thread test that Settings > Routing uses; SP19 the
|
||||
// restart chain the agent drawer calls.
|
||||
assert.match(js.text, /window\.MosaicAgents = Object\.freeze\(\{ section, restartMarkup, restart: restartAgent, isThread: definition => threadDefinitions\.has\(definition\) \}\)/);
|
||||
assert.match(js.text, /Agent actions are unavailable here\. Use mosaic org instance from the instance CLI\./);
|
||||
const html = await get(s, '/');
|
||||
assert.equal(html.text.match(/<script src="\/agents\.js" defer><\/script>/g)?.length, 1);
|
||||
|
||||
Loaded 100 of 141 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user