Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f666590d54 | ||
|
|
2f20346719 |
No files matched your search
@@ -708,6 +708,7 @@ export class Broker {
|
||||
#credentialVerbs;
|
||||
#proxyInit;
|
||||
#proxyFenceClear;
|
||||
#proxyTriageKey;
|
||||
#proxyPage;
|
||||
#project;
|
||||
#library;
|
||||
@@ -772,7 +773,7 @@ export class Broker {
|
||||
#processing;
|
||||
#taskTracker;
|
||||
// credentialStatus is the runtime's metadata-only Credentials.status(); the broker never gets use().
|
||||
constructor({ store, businesses, pluginService = null, secretCheck = () => {}, credentialStatus = () => [], organization = null, hires = null, autonomy = null, project = null, library = null, usage = null, limits = null, harnessOn = null, credentialVerbs = null, proxyInit = null, proxyFenceClear = null, proxyPage = null, hostGrant = null, hostKeys = null, processing = null, testerOn = null, grantScope = null, roleFile = null, giteaGrant = null, copyGrant = null, copyLaunch = null, copyRevoker = null, giteaKeys = null, access = null, accessStamp = null, compactions = null, requestLifecycle = null, requirementsHost = null, workspaceRead = null, taskTracker = null }) {
|
||||
constructor({ store, businesses, pluginService = null, secretCheck = () => {}, credentialStatus = () => [], organization = null, hires = null, autonomy = null, project = null, library = null, usage = null, limits = null, harnessOn = null, credentialVerbs = null, proxyInit = null, proxyFenceClear = null, proxyTriageKey = null, proxyPage = null, hostGrant = null, hostKeys = null, processing = null, testerOn = null, grantScope = null, roleFile = null, giteaGrant = null, copyGrant = null, copyLaunch = null, copyRevoker = null, giteaKeys = null, access = null, accessStamp = null, compactions = null, requestLifecycle = null, requirementsHost = null, workspaceRead = null, taskTracker = null }) {
|
||||
if (!object(businesses) || !Object.keys(businesses).length) fail('invalid-business');
|
||||
// Row 194 b15 (sage-2501): the function-argument checks come before anything reads the store.
|
||||
if (harnessOn !== null && typeof harnessOn !== 'function') throw new TypeError('harnessOn must be a function');
|
||||
@@ -802,6 +803,7 @@ export class Broker {
|
||||
this.#credentialVerbs = credentialVerbs;
|
||||
this.#proxyInit = proxyInit;
|
||||
this.#proxyFenceClear = proxyFenceClear;
|
||||
this.#proxyTriageKey = proxyTriageKey;
|
||||
this.#proxyPage = proxyPage;
|
||||
this.#hostGrant = hostGrant;
|
||||
this.#hostKeys = hostKeys;
|
||||
@@ -6220,6 +6222,27 @@ export class Broker {
|
||||
return result;
|
||||
}).catch(error => { throw this.#refused(s, new BusError(/^proxy-[a-z-]+$/.test(error.code ?? '') ? error.code : 'proxy-down'), { verb: request.verb }); });
|
||||
}
|
||||
// proxy-scope slice 4 (Jason 2026-11-19): ensure or revoke the instance's Triage key through the
|
||||
// proxy this bus rides. Owner only, before every other gate, like proxy.fence.clear. The
|
||||
// answer, the event and the refusal carry key ids and codes only, never a key.
|
||||
if (request.verb === 'proxy.triage.key') {
|
||||
this.#human(s);
|
||||
if (!this.#proxyTriageKey) fail('not-configured');
|
||||
const args = request.args ?? {};
|
||||
keys(args, ['op'], ['op']);
|
||||
if (args.op !== 'ensure' && args.op !== 'revoke') fail('invalid-request');
|
||||
const ID = /^[0-9a-f]{12}$/;
|
||||
const idList = v => Array.isArray(v) && v.every(id => typeof id === 'string' && ID.test(id));
|
||||
return Promise.resolve().then(() => this.#proxyTriageKey(args.op)).then(result => {
|
||||
const ACTIONS = args.op === 'ensure' ? ['issued', 'held', 'replaced'] : ['revoked', 'none'];
|
||||
if (!object(result) || !ACTIONS.includes(result.action) || !idList(result.addedKeyIds) || !idList(result.removedKeyIds)
|
||||
|| (args.op === 'ensure' && !(typeof result.keyId === 'string' && ID.test(result.keyId)))) fail('proxy-client-response');
|
||||
const answer = { op: args.op, action: result.action, addedKeyIds: [...result.addedKeyIds], removedKeyIds: [...result.removedKeyIds],
|
||||
...(args.op === 'ensure' ? { keyId: result.keyId, rotateBy: /^\d{4}-\d{2}-\d{2}$/.test(result.rotateBy ?? '') ? result.rotateBy : null } : {}) };
|
||||
this.#store.transaction(() => this.#event(s, 'action.allowed', { call: 'proxy.triage.key', ...answer }, null));
|
||||
return answer;
|
||||
}).catch(error => { throw this.#refused(s, error instanceof BusError ? error : new BusError('storage-unavailable', 'triage-key-failed'), { verb: request.verb }); });
|
||||
}
|
||||
// FED R3 (design 3.5, 3.7 trigger 1): a user reaches the collaborator list only, and each
|
||||
// request reads the access file again, so a removed or expired grant refuses the next one.
|
||||
let grants = null;
|
||||
|
||||
@@ -87,6 +87,10 @@ group("proxy-key-mismatch", "R1: a pasted management key is confirmed before ini
|
||||
group("proxy-lock-busy proxy-lock-invalid", "R4: one proxy key write at a time", "Wait for the running proxy write to finish and retry. An invalid lock file needs the owner to inspect the proxy state directory.");
|
||||
group("proxy-not-configured proxy-unit-usage proxy-unit-exists proxy-unit-dropin proxy-unit-copy proxy-unit-path proxy-unit-missing proxy-unit-unhealthy proxy-unit-failed", "R1/R4: proxy user unit is explicit creation", "Initialize the proxy first, then install the unit from the Proxy page or with mosaic proxy unit install. An existing unit or drop-in is never replaced; remove it by hand. An unhealthy unit: read its journal by unit name and restart it.");
|
||||
group("proxy-client-invalid proxy-client-refused proxy-client-response", "R1: proxy management answers", "Check the proxy unit is running and its port answers on the Proxy page, then retry. Upstream bodies are never shown.");
|
||||
// proxy-scope slice 4 (Jason 2026-11-19): the Triage key lives with a bus that rides the owner's
|
||||
// proxy, and a Triage session on a key past its retireAt ends before the key leaves the proxy.
|
||||
group("triage-no-riding-bus", "R1/R4: the Triage key is issued and rotated by a bus that rides the owner's proxy", "Triage needs one organization that uses the owner's models. Start an organization that rides the owner's model proxy, then repair Triage access from Settings, Triage.");
|
||||
group("triage-key-rotated", "R1/R6: a session ends when its Triage key's grace ends", "The Triage key was rotated and the old key's grace has ended. Send the next message; Triage starts again on the current key.");
|
||||
group(
|
||||
"block-recipient block-reply-required block-sender message-recipient-refused message-sender-refused tester-reply-required tester-thread-human user-reply-required user-thread-human",
|
||||
"Role messaging: permitted sender, recipient and reply context",
|
||||
|
||||
@@ -34,6 +34,7 @@ import { createModelCatalogue } from './model-catalogue.mjs';
|
||||
import { sha256 } from '../../model-proxy/src/common.mjs';
|
||||
import { initializeProxy, loadProxy } from '../../model-proxy/src/settings.mjs';
|
||||
import { createProxyLock } from './proxy-lock.mjs';
|
||||
import { ensureKey, readKey as readTriageKey, revokeKeys, sweepDue, sweepKeys } from './triage-key.mjs';
|
||||
import { verifyRestart } from '../../model-proxy/src/admin.mjs';
|
||||
import { ModelKeys, modelOrganization, stopModelRuns, MODEL_STOP_TIMEOUT_MS } from './model-keys.mjs';
|
||||
import { instanceSettings } from '../../organization/src/instance-settings.mjs';
|
||||
@@ -622,6 +623,10 @@ export async function startBroker({
|
||||
// P1 validated sidecar from the trusted host; never a socket request setting.
|
||||
proxy = null,
|
||||
org = null, instanceDataRoot = null, instanceConfigDir = null,
|
||||
// proxy-scope slice 4: the Triage key sweep on a riding org bus. intervalMs (default 30 s) and
|
||||
// marginMs (default 30 s past a retiring key's retireAt, so the Console stops a session on it
|
||||
// first) are in-process seams, as are now and hook (triage-key.mjs's clock and stage hook).
|
||||
triageKeys = {},
|
||||
// Host-only preparation/admission capabilities. The host builds its own from the organization writer and `trackers`
|
||||
// (request-host.mjs); anything given here replaces the host's, and a capability nothing supplies leaves a setup wait.
|
||||
requestServices = {},
|
||||
@@ -657,6 +662,8 @@ export async function startBroker({
|
||||
throw new BusError('invalid-host-request');
|
||||
let modelProxyClient = null, proxyPage = null, proxyUsage = null, usageTimer = null, proxyLock = null, proxyPort = null;
|
||||
let modelKeys = null, modelTimer = null, modelSweep = null, modelCatalogues = null;
|
||||
// proxy-scope slice 4: the settings of the proxy this bus rides (boot, picked up or initialized).
|
||||
let proxySettings = null, triageTimer = null, triageSweepRun = null;
|
||||
const modelRuns = new Map();
|
||||
let store, credentials, server, adapter, processing, syncTimer = null, syncPass = null, accessTimer = null, requestTimer = null, copyLaunch = null, copyRevoker = null, copyTimer = null;
|
||||
// #1873 CB4: a revoke's stop waits for the broker; a start that fails before it exists stops nothing.
|
||||
@@ -707,7 +714,7 @@ export async function startBroker({
|
||||
proxyLock = createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${proxy.port}` });
|
||||
}
|
||||
modelProxyClient = await loadManagementClient({ configDir: org ? instanceConfigDir : organization.dir, proxy, receipt: proxyReceipt });
|
||||
proxyPort = proxy.port;
|
||||
proxyPort = proxy.port; proxySettings = proxy;
|
||||
}
|
||||
|
||||
// Row 234 (#1740, r6 X4 third bullet): each host grant record gives its agent a store reference to
|
||||
@@ -845,7 +852,7 @@ export async function startBroker({
|
||||
// is no client: static text and a refusal code, never a path or a key.
|
||||
let lateProxy = false, proxyCause = null, proxySync = Promise.resolve();
|
||||
const dropLateProxy = cause => {
|
||||
if (lateProxy) { modelProxyClient = null; proxyPage = null; proxyPort = null; proxyLock = null; lateProxy = false; modelCatalogues?.invalidate?.(); }
|
||||
if (lateProxy) { modelProxyClient = null; proxyPage = null; proxyPort = null; proxyLock = null; proxySettings = null; lateProxy = false; modelCatalogues?.invalidate?.(); }
|
||||
proxyCause = cause;
|
||||
};
|
||||
// The host refuses fixture-proxy-configured at boot when a dev fixture is recorded for the
|
||||
@@ -879,11 +886,65 @@ export async function startBroker({
|
||||
catch { return dropLateProxy('The shared proxy lock does not load (proxy-lock-invalid). Check the proxy state for this instance, then restart this bus.'); }
|
||||
}
|
||||
proxyCause = null;
|
||||
proxySettings = found;
|
||||
// Same port, key and lock still load: keep the client and its page, which holds login states.
|
||||
if (modelProxyClient) return;
|
||||
modelProxyClient = client; proxyPort = found.port; proxyLock = lock; lateProxy = true;
|
||||
configureProxyPage(); modelCatalogues?.invalidate?.();
|
||||
}
|
||||
// proxy-scope slice 4: what triage-key.mjs needs from a riding org bus. A key error becomes a bus
|
||||
// refusal the table already holds: proxy codes it knows pass through, the rest name the key
|
||||
// store's code in the reason only. Neither carries a key, a path or upstream text.
|
||||
// The verb checks the proxy first, as proxy.fence.clear does; the sweep uses what this bus holds
|
||||
// and never picks a proxy up on its own timer.
|
||||
const triageOptions = async ({ sync = true } = {}) => {
|
||||
if (sync) await syncProxy();
|
||||
if (org === null || !organization || !proxyLock || !modelProxyClient || !proxySettings || typeof instanceConfigDir !== 'string' || typeof instanceDataRoot !== 'string')
|
||||
throw new BusError('triage-no-riding-bus');
|
||||
return { configDir: instanceConfigDir, system: { dataRoot: instanceDataRoot, proxy: proxySettings }, client: modelProxyClient, lock: proxyLock,
|
||||
...(triageKeys.now ? { now: triageKeys.now } : {}), ...(triageKeys.hook ? { hook: triageKeys.hook } : {}) };
|
||||
};
|
||||
const TRIAGE_PASS = new Set(['proxy-lock-busy', 'proxy-lock-invalid', 'proxy-lock-uncertain', 'proxy-client-invalid', 'proxy-client-refused', 'proxy-client-response', 'proxy-management-key-invalid']);
|
||||
const TRIAGE_MAP = { 'triage-key-no-proxy': 'triage-no-riding-bus', 'triage-key-raced': 'proxy-lock-busy', 'triage-key-unconfirmed': 'proxy-lock-uncertain',
|
||||
'proxy-uncertain': 'proxy-lock-uncertain', 'proxy-client-down': 'proxy-client-response', 'proxy-client-redirect': 'proxy-client-response', 'triage-key-proxy-open': 'proxy-not-configured' };
|
||||
const triageRefusal = error => {
|
||||
if (error instanceof BusError) return error;
|
||||
const code = typeof error?.code === 'string' ? error.code : '';
|
||||
if (TRIAGE_PASS.has(code)) return new BusError(code);
|
||||
if (Object.hasOwn(TRIAGE_MAP, code)) return new BusError(TRIAGE_MAP[code], code);
|
||||
return new BusError('storage-unavailable', /^triage-key-[a-z-]+$/.test(code) ? code : 'triage-key-failed');
|
||||
};
|
||||
// The sweep: a cheap read of the file first, so the proxy is asked only when something is due;
|
||||
// sweepKeys decides again under the lock. It never issues: no file, nothing.
|
||||
const triageMarginMs = Number.isSafeInteger(triageKeys.marginMs) && triageKeys.marginMs >= 0 ? triageKeys.marginMs : 30_000;
|
||||
// A refusal that repeats on every tick is logged when it first appears or changes, and then once
|
||||
// an hour while it stays the same; any other outcome clears it.
|
||||
let triageLastRefusal = null;
|
||||
const sweepRefused = (now, entry) => {
|
||||
const sig = `${entry.code}|${entry.reason ?? ''}`;
|
||||
if (triageLastRefusal?.sig === sig && now - triageLastRefusal.at < 3_600_000) return;
|
||||
triageLastRefusal = { sig, at: now };
|
||||
proxyReceipt({ action: 'triage.key.sweep-refused', ...entry });
|
||||
};
|
||||
const sweepTriage = async () => {
|
||||
let opts;
|
||||
try { opts = await triageOptions({ sync: false }); } catch { return { action: 'none' }; }
|
||||
const now = (opts.now ?? Date.now)();
|
||||
let record;
|
||||
try { record = readTriageKey(opts.configDir); } catch { sweepRefused(now, { code: 'triage-key-unreadable' }); return { action: 'none' }; }
|
||||
if (!sweepDue(record, now, triageMarginMs)) { triageLastRefusal = null; return { action: 'none' }; }
|
||||
try {
|
||||
const result = await sweepKeys(opts, { marginMs: triageMarginMs });
|
||||
triageLastRefusal = null;
|
||||
if (result.action !== 'none') proxyReceipt({ action: `triage.key.${result.action}`, keyId: result.keyId ?? null, addedKeyIds: result.addedKeyIds, removedKeyIds: result.removedKeyIds });
|
||||
return result;
|
||||
} catch (error) {
|
||||
const refusal = triageRefusal(error);
|
||||
sweepRefused(now, { code: refusal.code, ...(typeof refusal.reason === 'string' ? { reason: refusal.reason } : {}) });
|
||||
return { action: 'refused', code: refusal.code };
|
||||
}
|
||||
};
|
||||
const triageSweepOnce = () => (triageSweepRun ??= sweepTriage().finally(() => { triageSweepRun = null; }));
|
||||
// One check at a time; an unexpected failure drops a late client, never keeps it.
|
||||
const syncProxy = () => (proxySync = proxySync.then(pickProxy).catch(() => dropLateProxy('The model proxy could not be checked (proxy-down). Try again shortly.')));
|
||||
// Unavailable with no client: the catalogue names the cause instead of a bare unavailable.
|
||||
@@ -1419,7 +1480,7 @@ export async function startBroker({
|
||||
const lock = org && !proxyLock ? createProxyLock({ stateDir: join(instanceDataRoot, 'proxy'), identity: `${instanceConfigDir}:${settings.port}` }) : proxyLock;
|
||||
// proxy-pickup: a client this bus did not boot with is checked again like a picked-up one.
|
||||
if (!proxy) { lateProxy = true; proxyCause = null; }
|
||||
modelProxyClient = client; proxyPort = settings.port; proxyLock = lock;
|
||||
modelProxyClient = client; proxyPort = settings.port; proxyLock = lock; proxySettings = settings;
|
||||
configureProxyPage(); modelCatalogues?.invalidate?.();
|
||||
return settings;
|
||||
} : null,
|
||||
@@ -1430,6 +1491,15 @@ export async function startBroker({
|
||||
if (!proxyLock || !modelProxyClient) throw new BusError('not-configured', proxyCause ?? undefined);
|
||||
return proxyLock.clear({ restarted: true, read: () => modelProxyClient.getKeys(), verify: previous => verifyRestart(evidence, { fence: previous, port: proxyPort }) });
|
||||
} : null,
|
||||
// proxy-scope slice 4 (Jason 2026-11-19): ensure or revoke the instance's Triage key through the
|
||||
// proxy this org bus rides, under the bus's own client and instance proxy lock. The answer is
|
||||
// ids only. A no-org bus, or an org bus with no proxy client and lock, refuses
|
||||
// triage-no-riding-bus.
|
||||
proxyTriageKey: async (op) => {
|
||||
const opts = await triageOptions();
|
||||
try { return op === 'ensure' ? await ensureKey(opts) : await revokeKeys(opts); }
|
||||
catch (error) { throw triageRefusal(error); }
|
||||
},
|
||||
credentialVerbs: organization
|
||||
? credentialVerbs({ credentials, writer: organizationWriter({ dir: organization.dir, rolesDir: organization.rolesDir, dataRoot:instanceRoot ?? dataRoot }),
|
||||
organizations: () => Object.keys(businesses), boot: copies?.definitions ? { org: copies.org, definitions: copies.definitions } : null })
|
||||
@@ -1737,6 +1807,12 @@ export async function startBroker({
|
||||
}, access.intervalMs ?? ACCESS_SWEEP_MS);
|
||||
accessTimer.unref();
|
||||
}
|
||||
// proxy-scope slice 4: only an org bus sweeps the Triage key; each tick checks it still rides.
|
||||
if (org !== null && organization) {
|
||||
triageTimer = setInterval(() => { triageSweepOnce(); },
|
||||
Number.isSafeInteger(triageKeys.intervalMs) && triageKeys.intervalMs > 0 ? triageKeys.intervalMs : 30_000);
|
||||
triageTimer.unref();
|
||||
}
|
||||
if (modelKeys) {
|
||||
modelTimer = setInterval(() => {
|
||||
if (modelSweep) return;
|
||||
@@ -1905,6 +1981,8 @@ export async function startBroker({
|
||||
definitionScan,
|
||||
launches: bound,
|
||||
readers: readCaps,
|
||||
// proxy-scope slice 4: one Triage key sweep now, as the timer runs it (tests).
|
||||
triageKeySweep: () => triageSweepOnce(),
|
||||
async close() {
|
||||
clearInterval(copyTimer);
|
||||
brokerUp(null);
|
||||
@@ -1913,6 +1991,8 @@ export async function startBroker({
|
||||
await proxyUsage.close();
|
||||
clearInterval(modelTimer);
|
||||
await modelSweep;
|
||||
clearInterval(triageTimer);
|
||||
await triageSweepRun;
|
||||
clearInterval(accessTimer);
|
||||
await stopSync();
|
||||
await server.close();
|
||||
@@ -1930,6 +2010,7 @@ export async function startBroker({
|
||||
clearInterval(usageTimer);
|
||||
await proxyUsage?.close();
|
||||
clearInterval(modelTimer);
|
||||
clearInterval(triageTimer);
|
||||
clearInterval(accessTimer);
|
||||
await server?.close();
|
||||
await adapter?.close?.();
|
||||
|
||||
@@ -155,6 +155,8 @@ export async function serve({ broker, path, authenticateHuman = null, authentica
|
||||
// Row 236 (#1742): the grants verbs may call Gitea several times, 15 s each at most.
|
||||
GRANTS_VERBS.includes(r.verb) ||
|
||||
r.verb === 'proxy.init' ||
|
||||
// proxy-scope slice 4: the Triage key verb waits on the proxy lock and the proxy.
|
||||
r.verb === 'proxy.triage.key' ||
|
||||
(r.verb === 'decision.raise' && (Object.hasOwn(r.args ?? {}, 'spec_path') || Object.hasOwn(r.args ?? {}, 'spec_commit') ||
|
||||
Object.hasOwn(r.args ?? {}, 'grant')))) {
|
||||
const own = transient;
|
||||
|
||||
File renamed without changes.
@@ -1,5 +1,9 @@
|
||||
// #1873 triage-key: Triage's own model proxy client key, issued from the owner's terminal
|
||||
// (design report reports/triage-route-design.md slice 3, Jason's ruling 2026-11-18).
|
||||
// (design report reports/triage-route-design.md slice 3, Jason's ruling 2026-11-18), and since
|
||||
// proxy-scope slice 4 (Jason's ruling 2026-11-19) by the bus too: the verb proxy.triage.key
|
||||
// (ensure, revoke) and a riding org bus's sweep call this module with the bus's own management
|
||||
// client and instance proxy lock. It moved here from packages/cli/src so the bus can import it; the
|
||||
// terminal verbs (packages/cli/src/triage.mjs) call it unchanged.
|
||||
//
|
||||
// <configDir>/secrets/triage/model (directory 0700, file 0600, the running user's)
|
||||
// { version, key, keyId, issuedAt, rotateBy, retiring: [{ key, keyId, issuedAt, retireAt }] }
|
||||
@@ -16,9 +20,11 @@
|
||||
// uncertain fence, which every proxy writer on the instance refuses until a human clears it
|
||||
// (`mosaic proxy fence clear --proxy-restarted`).
|
||||
//
|
||||
// Nothing runs on a timer. rotate records when the old key's grace ends; the key stays in the proxy
|
||||
// until a later retire, rotate or issue removes it, and status says when that is due. The same goes
|
||||
// for rotateBy: status marks it due; only rotate replaces the key.
|
||||
// rotate records when the old key's grace ends; the key stays in the proxy until a later retire,
|
||||
// rotate, issue or sweep removes it, and status says when that is due. A riding org bus runs
|
||||
// sweepKeys on a timer: under the lock it reads the file again and rotates when rotateBy is due, or
|
||||
// retires keys whose grace (plus a margin, so the Console stops a session on the key first) has
|
||||
// passed. The sweep only replaces a key that exists; it never issues the first one.
|
||||
//
|
||||
// Nothing here prints, logs or records a key. Receipts and output carry key ids only (the first 12
|
||||
// hex of sha256(key), the id the bus uses). Messages name files and options, never values.
|
||||
@@ -27,7 +33,7 @@ import { closeSync, constants, fchmodSync, fsyncSync, lstatSync, openSync, readd
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { join } from "node:path";
|
||||
import { isDeepStrictEqual } from "node:util";
|
||||
import { createProxyLock } from "../../bus/src/proxy-lock.mjs";
|
||||
import { createProxyLock } from "./proxy-lock.mjs";
|
||||
import { loadManagementClient } from "../../model-proxy/src/client.mjs";
|
||||
|
||||
// #1873 triage-wiring: the key file's reader lives in triage-key-file.mjs (node built-ins only), so
|
||||
@@ -136,20 +142,24 @@ const proxyFailure = (code) => {
|
||||
};
|
||||
|
||||
// opts.uid, opts.now and opts.hook(stage) are test seams; a stage hook may throw or kill the process.
|
||||
function context({ configDir, system, uid, fileUid, now, hook }) {
|
||||
// opts.client and opts.lock: the bus passes its own management client and instance proxy lock, so a
|
||||
// bus change runs under the lock object its model key sweep uses and reads no key file of its own.
|
||||
function context({ configDir, system, uid, fileUid, now, hook, client, lock }) {
|
||||
if (!system?.proxy || !Number.isInteger(system.proxy.port)) throw new TriageKeyError("triage-key-no-proxy", "the instance has no model proxy set up (proxy.json)");
|
||||
return {
|
||||
configDir, dataRoot: system.dataRoot, proxy: system.proxy, stateDir: join(system.dataRoot, "proxy"),
|
||||
uid: uid ?? process.getuid(), fileUid, now: now ?? Date.now, hook: hook ?? (() => {}),
|
||||
uid: uid ?? process.getuid(), fileUid, now: now ?? Date.now, hook: hook ?? (() => {}), client, lock,
|
||||
};
|
||||
}
|
||||
|
||||
function lockFor(c) {
|
||||
if (c.lock) return c.lock;
|
||||
try { return createProxyLock({ stateDir: c.stateDir, identity: `${c.configDir}:${c.proxy.port}` }); }
|
||||
catch (e) { throw proxyFailure(e.code ?? "proxy-lock-invalid"); }
|
||||
}
|
||||
|
||||
async function clientFor(c) {
|
||||
if (c.client) return c.client;
|
||||
try { return await loadManagementClient({ configDir: c.configDir, proxy: c.proxy }); }
|
||||
catch (e) { throw proxyFailure(e.code ?? "proxy-management-key-invalid"); }
|
||||
}
|
||||
@@ -162,9 +172,10 @@ function checkSentinel(c, keys) {
|
||||
}
|
||||
|
||||
// Under the instance proxy lock: read the list, check the sentinel, run `plan`, write the result.
|
||||
// `plan(keys, record)` returns { after, record, removed, added, done }; `record` null leaves the
|
||||
// file alone. The proxy changes first, then the file, then one receipt line.
|
||||
async function change(c, op, plan) {
|
||||
// `plan(keys, record)` returns { after, record, remove, op, done }; `record` null leaves the file
|
||||
// alone, `remove` true unlinks it (revoke), `op` names the change when the plan picks it (sweep).
|
||||
// The proxy changes first, then the file, then one receipt line.
|
||||
async function change(c, opName, plan) {
|
||||
const lock = lockFor(c);
|
||||
const client = await clientFor(c);
|
||||
try {
|
||||
@@ -173,6 +184,8 @@ async function change(c, op, plan) {
|
||||
checkSentinel(c, before);
|
||||
const current = readKey(c.configDir, { uid: c.uid, fileUid: c.fileUid });
|
||||
const result = plan(before, current);
|
||||
const op = result.op ?? opName;
|
||||
const remove = result.remove === true && current !== null;
|
||||
if (result.record) prepareDirs(c.configDir, c.uid);
|
||||
removeTemps(c.configDir);
|
||||
receiptsReady(c.stateDir, c.uid);
|
||||
@@ -195,8 +208,8 @@ async function change(c, op, plan) {
|
||||
}
|
||||
c.hook("write");
|
||||
let unsynced = false;
|
||||
if (result.record) {
|
||||
try { writeKey(c.configDir, result.record, c.uid, c.hook); }
|
||||
if (result.record || remove) {
|
||||
try { if (remove) unlinkKey(c.configDir, c.hook); else writeKey(c.configDir, result.record, c.uid, c.hook); }
|
||||
catch (e) {
|
||||
if (e instanceof NotSynced) unsynced = true;
|
||||
else throw new TriageKeyError("triage-key-unwritten", `the proxy list changed but ${keyPaths(c.configDir).file} was not written (${e.message}); fix the file's directory, then run mosaic triage key ${op} again, which clears what this run left`);
|
||||
@@ -204,7 +217,7 @@ async function change(c, op, plan) {
|
||||
}
|
||||
c.hook("receipt");
|
||||
// A run that changed neither the proxy nor the file leaves no receipt.
|
||||
if (added.length || removed.length || result.record) receipt(c.stateDir, c.uid, { op: `triage.key.${op}`, time: new Date(c.now()).toISOString(), addedKeyIds: added.map(keyIdOf), removedKeyIds: removed.map(keyIdOf) });
|
||||
if (added.length || removed.length || result.record || remove) receipt(c.stateDir, c.uid, { op: `triage.key.${op}`, time: new Date(c.now()).toISOString(), addedKeyIds: added.map(keyIdOf), removedKeyIds: removed.map(keyIdOf) });
|
||||
// The file is in place and the receipt written; only its durability is in doubt.
|
||||
if (unsynced) throw new TriageKeyError("triage-key-unsynced", `${keyPaths(c.configDir).file} was written but its directory could not be synced, so it may not survive a power loss; run mosaic triage key status`);
|
||||
return { ...result.done, addedKeyIds: added.map(keyIdOf), removedKeyIds: removed.map(keyIdOf) };
|
||||
@@ -215,6 +228,18 @@ async function change(c, op, plan) {
|
||||
}
|
||||
}
|
||||
|
||||
// Revoke's file step: unlink, then sync the directory, with writeKey's stages and failures.
|
||||
function unlinkKey(configDir, hook = () => {}) {
|
||||
const { dir, file } = keyPaths(configDir);
|
||||
try { hook("rename"); unlinkSync(file); }
|
||||
catch (e) { if (e?.code !== "ENOENT") throw unreadable("the key file cannot be removed"); }
|
||||
try {
|
||||
hook("sync");
|
||||
const dfd = openSync(dir, constants.O_RDONLY | constants.O_DIRECTORY);
|
||||
try { fsyncSync(dfd); } finally { closeSync(dfd); }
|
||||
} catch { throw new NotSynced(); }
|
||||
}
|
||||
|
||||
const newKey = (configDir) => keyPrefix(configDir) + randomBytes(32).toString("base64url");
|
||||
|
||||
// Keys in the Triage namespace that the file does not hold: left by a crash between the PUT and the
|
||||
@@ -235,39 +260,117 @@ export async function issueKey(opts) {
|
||||
});
|
||||
}
|
||||
|
||||
// Retiring keys whose grace has passed, or that the proxy no longer holds.
|
||||
const dueOf = (record, keys, nowMs) => record.retiring.filter((r) => Date.parse(r.retireAt) <= nowMs || !keys.includes(r.key));
|
||||
// Retiring keys whose grace (plus marginMs, 0 for the terminal verbs) has passed, or that the proxy
|
||||
// no longer holds.
|
||||
const dueOf = (record, keys, nowMs, marginMs = 0) => record.retiring.filter((r) => Date.parse(r.retireAt) + marginMs <= nowMs || !keys.includes(r.key));
|
||||
const rotateDueAt = (record) => Date.parse(`${record.rotateBy}T00:00:00Z`);
|
||||
|
||||
// The rotate plan, shared by the terminal verb and the sweep.
|
||||
function rotatePlan(c, before, current, nowMs, marginMs = 0) {
|
||||
const orphans = orphansOf(c.configDir, before, current);
|
||||
const due = dueOf(current, before, nowMs, marginMs);
|
||||
const keep = before.includes(current.key);
|
||||
const retiring = [...current.retiring.filter((r) => !due.includes(r)),
|
||||
...(keep ? [{ key: current.key, keyId: current.keyId, issuedAt: current.issuedAt, retireAt: retireAtFor(nowMs, c.proxy) }] : [])];
|
||||
if (retiring.length > MAX_RETIRING) throw new TriageKeyError("triage-key-failed", "too many keys are retiring; run mosaic triage key retire once their grace has passed");
|
||||
const key = newKey(c.configDir);
|
||||
const record = { version: KEY_VERSION, key, keyId: keyIdOf(key), issuedAt: new Date(nowMs).toISOString(), rotateBy: rotateByFor(nowMs), retiring };
|
||||
const drop = [...orphans, ...due.map((r) => r.key)];
|
||||
return {
|
||||
after: [...before.filter((k) => !drop.includes(k)), key], record,
|
||||
done: { keyId: record.keyId, rotateBy: record.rotateBy, old: keep ? { keyId: current.keyId, retireAt: retiring.at(-1).retireAt } : { keyId: current.keyId, lost: true } },
|
||||
};
|
||||
}
|
||||
|
||||
export async function rotateKey(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "rotate", (before, current) => {
|
||||
if (!current) throw new TriageKeyError("triage-key-not-issued", "no Triage key is issued; use mosaic triage key issue");
|
||||
const nowMs = c.now();
|
||||
const orphans = orphansOf(c.configDir, before, current);
|
||||
const due = dueOf(current, before, nowMs);
|
||||
const keep = before.includes(current.key);
|
||||
const retiring = [...current.retiring.filter((r) => !due.includes(r)),
|
||||
...(keep ? [{ key: current.key, keyId: current.keyId, issuedAt: current.issuedAt, retireAt: retireAtFor(nowMs, c.proxy) }] : [])];
|
||||
if (retiring.length > MAX_RETIRING) throw new TriageKeyError("triage-key-failed", "too many keys are retiring; run mosaic triage key retire once their grace has passed");
|
||||
const key = newKey(c.configDir);
|
||||
const record = { version: KEY_VERSION, key, keyId: keyIdOf(key), issuedAt: new Date(nowMs).toISOString(), rotateBy: rotateByFor(nowMs), retiring };
|
||||
const drop = [...orphans, ...due.map((r) => r.key)];
|
||||
return {
|
||||
after: [...before.filter((k) => !drop.includes(k)), key], record,
|
||||
done: { keyId: record.keyId, rotateBy: record.rotateBy, old: keep ? { keyId: current.keyId, retireAt: retiring.at(-1).retireAt } : { keyId: current.keyId, lost: true } },
|
||||
};
|
||||
return rotatePlan(c, before, current, c.now());
|
||||
});
|
||||
}
|
||||
function retirePlan(c, before, current, nowMs, marginMs = 0) {
|
||||
const orphans = orphansOf(c.configDir, before, current);
|
||||
const due = current ? dueOf(current, before, nowMs, marginMs) : [];
|
||||
const record = current && due.length ? { ...current, retiring: current.retiring.filter((r) => !due.includes(r)) } : null;
|
||||
const drop = [...orphans, ...due.map((r) => r.key)];
|
||||
return { after: before.filter((k) => !drop.includes(k)), record, done: { droppedKeyIds: due.map((r) => r.keyId) } };
|
||||
}
|
||||
|
||||
export async function retireKeys(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "retire", (before, current) => {
|
||||
return change(c, "retire", (before, current) => retirePlan(c, before, current, c.now()));
|
||||
}
|
||||
|
||||
// proxy-scope slice 4: the bus verb proxy.triage.key op ensure. Leaves a held key alone, issues one
|
||||
// when none exists, replaces one the proxy lost. Never prints the key; the answer is ids only.
|
||||
// issued no file: a new key (keys in the namespace the file does not hold are removed)
|
||||
// held the proxy holds the file's key: only orphans and retiring keys the proxy lost go
|
||||
// replaced the proxy does not hold the file's key: a new key, the old one marked lost
|
||||
export async function ensureKey(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "ensure", (before, current) => {
|
||||
const nowMs = c.now();
|
||||
const orphans = orphansOf(c.configDir, before, current);
|
||||
const due = current ? dueOf(current, before, nowMs) : [];
|
||||
const record = current && due.length ? { ...current, retiring: current.retiring.filter((r) => !due.includes(r)) } : null;
|
||||
const drop = [...orphans, ...due.map((r) => r.key)];
|
||||
return { after: before.filter((k) => !drop.includes(k)), record, done: { droppedKeyIds: due.map((r) => r.keyId) } };
|
||||
const fresh = (retiring) => {
|
||||
const key = newKey(c.configDir);
|
||||
return { version: KEY_VERSION, key, keyId: keyIdOf(key), issuedAt: new Date(nowMs).toISOString(), rotateBy: rotateByFor(nowMs), retiring };
|
||||
};
|
||||
if (!current) {
|
||||
const record = fresh([]);
|
||||
return { after: [...before.filter((k) => !orphans.includes(k)), record.key], record, done: { action: "issued", keyId: record.keyId, rotateBy: record.rotateBy } };
|
||||
}
|
||||
const lost = current.retiring.filter((r) => !before.includes(r.key));
|
||||
const retiring = current.retiring.filter((r) => !lost.includes(r));
|
||||
if (before.includes(current.key)) {
|
||||
return {
|
||||
after: before.filter((k) => !orphans.includes(k)), record: lost.length ? { ...current, retiring } : null,
|
||||
done: { action: "held", keyId: current.keyId, rotateBy: current.rotateBy },
|
||||
};
|
||||
}
|
||||
const record = fresh(retiring);
|
||||
return {
|
||||
after: [...before.filter((k) => !orphans.includes(k)), record.key], record,
|
||||
done: { action: "replaced", keyId: record.keyId, rotateBy: record.rotateBy, old: { keyId: current.keyId, lost: true } },
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
// proxy-scope slice 4: op revoke, when the owner turns Triage off. Every name in this instance's
|
||||
// Triage namespace leaves the proxy list, then the key file goes. No file and no names: nothing.
|
||||
export async function revokeKeys(opts) {
|
||||
const c = context(opts);
|
||||
return change(c, "revoke", (before, current) => {
|
||||
const mine = before.filter(inNamespace(c.configDir));
|
||||
return { after: before.filter((k) => !mine.includes(k)), record: null, remove: true, done: { action: mine.length || current ? "revoked" : "none" } };
|
||||
});
|
||||
}
|
||||
|
||||
// What a sweep would do from the file alone: "rotate" when rotateBy has come, "retire" when a
|
||||
// retiring key's grace plus marginMs has passed, else null. A cheap check without the lock or the
|
||||
// proxy; sweepKeys decides again under the lock.
|
||||
export function sweepDue(record, nowMs, marginMs = 0) {
|
||||
if (!record) return null;
|
||||
if (rotateDueAt(record) <= nowMs) return "rotate";
|
||||
return record.retiring.some((r) => Date.parse(r.retireAt) + marginMs <= nowMs) ? "retire" : null;
|
||||
}
|
||||
|
||||
// The bus sweep. Under the instance proxy lock it reads the file again and acts only if the action
|
||||
// is still due, so two buses that wake together change the proxy once. No file: nothing, ever.
|
||||
export async function sweepKeys(opts, { marginMs = 0 } = {}) {
|
||||
const c = context(opts);
|
||||
return change(c, "sweep", (before, current) => {
|
||||
const nowMs = c.now();
|
||||
const due = sweepDue(current, nowMs, marginMs);
|
||||
if (due === "rotate") {
|
||||
const plan = rotatePlan(c, before, current, nowMs, marginMs);
|
||||
return { ...plan, op: "rotate", done: { action: "rotated", ...plan.done } };
|
||||
}
|
||||
if (due === "retire") {
|
||||
const plan = retirePlan(c, before, current, nowMs, marginMs);
|
||||
return { ...plan, op: "retire", done: { action: "retired", ...plan.done } };
|
||||
}
|
||||
return { after: before, record: null, done: { action: "none" } };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,305 @@
|
||||
// proxy-scope slice 4 (#1873): the Triage key in the bus. Real organization buses on one instance,
|
||||
// a stand-in proxy management server on loopback and a temporary config directory; nothing here
|
||||
// reads a real config, proxy or secret. Each test scans every answer, event, receipt line and
|
||||
// stderr line for every Triage key the stand-in ever held.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import http from 'node:http';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
import { startBroker } from '../src/runtime.mjs';
|
||||
import { busOrganization } from '../src/organization-context.mjs';
|
||||
import { keyPaths, readKey } from '../src/triage-key.mjs';
|
||||
import { Registry } from '../../seat/src/session.mjs';
|
||||
import { loadBusiness } from '../../organization/src/organization-config.mjs';
|
||||
import { resolveInstance } from '../../organization/src/resolve.mjs';
|
||||
import { businessDoc, rolesCopy, systemFor, writeJson } from '../../organization/tests/helpers.mjs';
|
||||
|
||||
const NOW = Date.parse('2026-11-18T12:00:00.000Z');
|
||||
const MIN = 60_000, HOUR = 60 * MIN, DAY = 24 * HOUR;
|
||||
const MANAGE = 'fx1873-s4-manage';
|
||||
const KEY_PATH = '/v8/management/config/access/api-keys';
|
||||
const sha = s => createHash('sha256').update(s).digest('hex');
|
||||
const tag = dir => sha(dir).slice(0, 12);
|
||||
const id = key => sha(key).slice(0, 12);
|
||||
const b64 = () => randomBytes(32).toString('base64url');
|
||||
const code = name => e => e.code === name;
|
||||
|
||||
// Every Triage key the stand-in ever held, and every text a bus produced, across the file.
|
||||
const SEEN = new Set();
|
||||
const TEXTS = [];
|
||||
function leaks(text) {
|
||||
for (const key of SEEN) {
|
||||
const secret = key.slice(-43);
|
||||
if (text.includes(key) || text.includes(secret)) return key.slice(0, 8);
|
||||
for (let i = 0; i + 8 <= secret.length; i++) if (text.includes(secret.slice(i, i + 8))) return key.slice(0, 8);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Every stderr line while the test runs: the bus's model-proxy receipt lines go there.
|
||||
function captureStderr(t) {
|
||||
const write = process.stderr.write, lines = [];
|
||||
process.stderr.write = (chunk, ...rest) => { lines.push(String(chunk)); return write.call(process.stderr, chunk, ...rest); };
|
||||
t.after(() => { process.stderr.write = write; TEXTS.push(lines.join('')); });
|
||||
return lines;
|
||||
}
|
||||
|
||||
async function instance(t) {
|
||||
const root = fs.realpathSync(fs.mkdtempSync(join(tmpdir(), 'tkb-')));
|
||||
const configDir = join(root, 'config');
|
||||
fs.mkdirSync(join(configDir, 'secrets'), { recursive: true, mode: 0o700 });
|
||||
fs.chmodSync(configDir, 0o700); fs.chmodSync(join(configDir, 'secrets'), 0o700);
|
||||
fs.writeFileSync(join(configDir, 'secrets', 'proxy-management.key'), MANAGE, { mode: 0o600 });
|
||||
fs.mkdirSync(join(root, 'proxy'), { mode: 0o700 });
|
||||
const sentinel = `msk-${tag(configDir)}-sentinel-${b64()}`, orgKey = `mso-${tag(configDir)}-other-key-${b64()}`;
|
||||
// triagePuts counts the writes that changed a Triage name; the organization buses' own key
|
||||
// reconcile writes the list too, and those do not count.
|
||||
const state = { keys: [sentinel, 'foreign-client-key', orgKey], puts: [], triagePuts: 0 };
|
||||
const triageNames = list => JSON.stringify(list.filter(k => k.startsWith('mst-')).sort());
|
||||
const server = http.createServer(async (req, res) => {
|
||||
if (req.url !== KEY_PATH) {
|
||||
const key = req.headers.authorization?.slice(7);
|
||||
res.writeHead(state.keys.includes(key) ? 200 : 401); res.end('{}'); return;
|
||||
}
|
||||
if (req.headers.authorization !== `Bearer ${MANAGE}`) { res.writeHead(401); res.end('{}'); return; }
|
||||
if (req.method === 'GET') { res.end(JSON.stringify(state.keys)); return; }
|
||||
const chunks = []; for await (const c of req) chunks.push(c);
|
||||
const next = JSON.parse(Buffer.concat(chunks));
|
||||
for (const k of next) if (/^mst-.*-[A-Za-z0-9_-]{43}$/.test(k)) SEEN.add(k);
|
||||
state.puts.push(next);
|
||||
if (triageNames(next) !== triageNames(state.keys)) state.triagePuts++;
|
||||
state.keys = next;
|
||||
res.end('{"status":"ok"}');
|
||||
});
|
||||
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
||||
const port = server.address().port;
|
||||
const closers = [];
|
||||
t.after(async () => {
|
||||
for (const close of closers.reverse()) await close().catch(() => {});
|
||||
server.closeAllConnections(); await new Promise(resolve => server.close(resolve));
|
||||
const receipts = join(root, 'proxy', 'mosaic-keys.receipts.jsonl');
|
||||
if (fs.existsSync(receipts)) TEXTS.push(fs.readFileSync(receipts, 'utf8'));
|
||||
fs.rmSync(root, { recursive: true, force: true });
|
||||
});
|
||||
const rolesDir = rolesCopy(root), system = systemFor(root);
|
||||
const f = {
|
||||
root, configDir, port, state, closers, rolesDir, system, sentinel, orgKey, clock: { now: NOW },
|
||||
prefix: `mst-${tag(configDir)}-triage-`,
|
||||
file: keyPaths(configDir).file,
|
||||
record: () => readKey(configDir),
|
||||
triageKeys: () => f.state.keys.filter(k => k.startsWith(f.prefix)),
|
||||
receipts: () => {
|
||||
const p = join(root, 'proxy', 'mosaic-keys.receipts.jsonl');
|
||||
return fs.existsSync(p) ? fs.readFileSync(p, 'utf8').trim().split('\n').filter(Boolean).map(JSON.parse) : [];
|
||||
},
|
||||
// A real organization bus on this instance. `proxy: false` starts it with no proxy settings, as
|
||||
// a bus on an instance with no proxy.json does.
|
||||
async bus(orgId, { proxy = true, triageKeys = {} } = {}) {
|
||||
const doc = businessDoc(root, orgId);
|
||||
doc.plugins = { ...doc.plugins, harness: ['pi'] };
|
||||
writeJson(join(configDir, 'businesses', `${orgId}.json`), doc);
|
||||
const business = loadBusiness(orgId, { dir: configDir, rolesDir }), resolved = {};
|
||||
for (const name of Object.keys(business.roles)) resolved[name] = resolveInstance({ system, business, instance: name });
|
||||
const dataRoot = join(root, `${orgId}-bus`);
|
||||
fs.mkdirSync(dataRoot, { mode: 0o700 }); new Registry(dataRoot);
|
||||
const bus = await startBroker({ dataRoot, businesses: { [orgId]: busOrganization(business, resolved) },
|
||||
organization: { dir: configDir, rolesDir, system }, ...(proxy ? { proxy: { port, binary: '/fixture-unused' } } : {}),
|
||||
org: { id: orgId }, instanceDataRoot: root, instanceConfigDir: configDir,
|
||||
triageKeys: { intervalMs: 3_600_000, now: () => f.clock.now, ...triageKeys } });
|
||||
closers.push(() => bus.close());
|
||||
const cap = bus.broker.bindHuman({ business: orgId, human: business.human ?? doc.human, via: 'cli', outsideAgent: true });
|
||||
const ask = async (verb, args) => {
|
||||
try { const answer = await bus.broker.request(cap, { verb, args }); TEXTS.push(JSON.stringify(answer)); return answer; }
|
||||
catch (error) { TEXTS.push(JSON.stringify({ code: error.code, reason: error.reason, message: error.message, nextStep: error.nextStep })); throw error; }
|
||||
};
|
||||
const sweep = async () => { const r = await bus.triageKeySweep(); TEXTS.push(JSON.stringify(r)); return r; };
|
||||
return { bus, ask, sweep, dataRoot };
|
||||
},
|
||||
};
|
||||
return f;
|
||||
}
|
||||
|
||||
// Every byte under a bus's data root (its store, with events and refusals, and its logs), as text.
|
||||
function eventsText(b, called = true) {
|
||||
const out = [];
|
||||
const walk = dir => {
|
||||
for (const name of fs.readdirSync(dir)) {
|
||||
const p = join(dir, name), st = fs.lstatSync(p);
|
||||
if (st.isDirectory()) walk(p);
|
||||
else if (st.isFile()) out.push(fs.readFileSync(p).toString('latin1'));
|
||||
}
|
||||
};
|
||||
walk(b.dataRoot);
|
||||
if (called) assert.ok(out.join('').includes('proxy.triage.key'), 'the store holds the verb\'s events');
|
||||
return out.join('\n');
|
||||
}
|
||||
|
||||
test('ensure issues one key, a second ensure keeps it, and both answers carry ids only', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha');
|
||||
const first = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.equal(first.action, 'issued');
|
||||
assert.equal(f.triageKeys().length, 1);
|
||||
const record = f.record();
|
||||
assert.equal(first.keyId, record.keyId);
|
||||
assert.equal(first.keyId, id(f.triageKeys()[0]));
|
||||
assert.deepEqual(first.addedKeyIds, [record.keyId]);
|
||||
assert.equal(fs.statSync(f.file).mode & 0o777, 0o600);
|
||||
const second = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.deepEqual(second, { op: 'ensure', action: 'held', addedKeyIds: [], removedKeyIds: [], keyId: record.keyId, rotateBy: record.rotateBy });
|
||||
assert.equal(f.triageKeys().length, 1, 'ensure twice gives one key');
|
||||
assert.equal(f.record().key, record.key);
|
||||
// Through a second riding bus on the same instance: still the one key.
|
||||
const b = await f.bus('beta');
|
||||
assert.equal((await b.ask('proxy.triage.key', { op: 'ensure' })).action, 'held');
|
||||
assert.equal(f.triageKeys().length, 1);
|
||||
assert.equal(f.state.triagePuts, 1, 'only the first ensure wrote the proxy list');
|
||||
// The receipt names the change by op and ids.
|
||||
const receipts = f.receipts().filter(r => r.op?.startsWith('triage.'));
|
||||
assert.deepEqual(receipts.map(r => [r.op, r.addedKeyIds, r.removedKeyIds]), [['triage.key.ensure', [record.keyId], []]]);
|
||||
for (const text of [eventsText(a), eventsText(b)]) assert.equal(leaks(text), null);
|
||||
});
|
||||
|
||||
test('ensure replaces a key the proxy lost, and revoke removes every Triage name and the file', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha');
|
||||
const issued = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
// The proxy loses the key (removed by hand): ensure issues a new one and names the old as lost.
|
||||
f.state.keys = f.state.keys.filter(k => !k.startsWith(f.prefix));
|
||||
const replaced = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.equal(replaced.action, 'replaced');
|
||||
assert.notEqual(replaced.keyId, issued.keyId);
|
||||
assert.equal(f.triageKeys().length, 1);
|
||||
// An orphan in the namespace (a crash left it) goes with revoke too.
|
||||
const orphan = `${f.prefix}${b64()}`; SEEN.add(orphan);
|
||||
f.state.keys.push(orphan);
|
||||
const revoked = await a.ask('proxy.triage.key', { op: 'revoke' });
|
||||
assert.equal(revoked.action, 'revoked');
|
||||
assert.deepEqual(revoked.removedKeyIds.sort(), [replaced.keyId, id(orphan)].sort());
|
||||
assert.equal(f.triageKeys().length, 0);
|
||||
assert.equal(fs.existsSync(f.file), false);
|
||||
assert.equal(f.record(), null);
|
||||
// Revoke again: nothing to do, nothing written.
|
||||
const puts = f.state.triagePuts;
|
||||
assert.deepEqual(await a.ask('proxy.triage.key', { op: 'revoke' }), { op: 'revoke', action: 'none', addedKeyIds: [], removedKeyIds: [] });
|
||||
assert.equal(f.state.triagePuts, puts);
|
||||
// Namespace guard: the sentinel, the organization key and the foreign key are still there.
|
||||
for (const k of [f.sentinel, f.orgKey, 'foreign-client-key']) assert.ok(f.state.keys.includes(k), k.slice(0, 4));
|
||||
assert.equal(leaks(eventsText(a)), null);
|
||||
});
|
||||
|
||||
test('a bus that does not ride the owner\'s proxy refuses triage-no-riding-bus, and its sweep does nothing', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const loose = await f.bus('loose', { proxy: false });
|
||||
await assert.rejects(loose.ask('proxy.triage.key', { op: 'ensure' }), e => e.code === 'triage-no-riding-bus'
|
||||
&& /^Triage needs one organization that uses the owner's models\./.test(e.nextStep));
|
||||
await assert.rejects(loose.ask('proxy.triage.key', { op: 'revoke' }), code('triage-no-riding-bus'));
|
||||
assert.equal(f.state.triagePuts, 0);
|
||||
// With a key issued through a riding bus and its rotation due, the loose bus still does nothing.
|
||||
const a = await f.bus('alpha');
|
||||
await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
f.clock.now += 31 * DAY;
|
||||
assert.deepEqual(await loose.sweep(), { action: 'none' });
|
||||
assert.equal(f.state.triagePuts, 1);
|
||||
});
|
||||
|
||||
test('the sweep rotates when rotateBy comes, once across two buses that wake together, and again a rotation later', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha'), b = await f.bus('beta');
|
||||
const issued = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
// Not due yet: neither bus touches the proxy.
|
||||
assert.deepEqual([await a.sweep(), await b.sweep()], [{ action: 'none' }, { action: 'none' }]);
|
||||
assert.equal(f.state.triagePuts, 1);
|
||||
f.clock.now = Date.parse(`${issued.rotateBy}T00:00:00Z`);
|
||||
const both = await Promise.all([a.sweep(), b.sweep()]);
|
||||
assert.deepEqual(both.map(r => r.action).sort(), ['none', 'rotated']);
|
||||
assert.equal(f.state.triagePuts, 2, 'two buses rotated once');
|
||||
const rotated = both.find(r => r.action === 'rotated');
|
||||
const record = f.record();
|
||||
assert.equal(rotated.keyId, record.keyId);
|
||||
assert.deepEqual(record.retiring.map(r => r.keyId), [issued.keyId]);
|
||||
assert.equal(f.triageKeys().length, 2, 'the old key stays live through its grace');
|
||||
// Inside the grace both buses leave it; at retireAt plus the margin one of them retires it.
|
||||
const retireAt = Date.parse(record.retiring[0].retireAt);
|
||||
f.clock.now = retireAt + 10_000;
|
||||
assert.deepEqual((await Promise.all([a.sweep(), b.sweep()])).map(r => r.action), ['none', 'none']);
|
||||
f.clock.now = retireAt + 30_000;
|
||||
const retired = await Promise.all([a.sweep(), b.sweep()]);
|
||||
assert.deepEqual(retired.map(r => r.action).sort(), ['none', 'retired']);
|
||||
assert.equal(f.state.triagePuts, 3);
|
||||
assert.deepEqual(f.triageKeys().map(id), [record.keyId]);
|
||||
assert.deepEqual(f.record().retiring, []);
|
||||
// A second rotation 30 days on: again once.
|
||||
f.clock.now = Date.parse(`${record.rotateBy}T00:00:00Z`) + HOUR;
|
||||
const again = await Promise.all([b.sweep(), a.sweep()]);
|
||||
assert.deepEqual(again.map(r => r.action).sort(), ['none', 'rotated']);
|
||||
assert.equal(f.state.triagePuts, 4);
|
||||
assert.equal(f.triageKeys().length, 2);
|
||||
// Namespace guard across every sweep.
|
||||
for (const k of [f.sentinel, f.orgKey, 'foreign-client-key']) assert.ok(f.state.keys.includes(k), k.slice(0, 4));
|
||||
const ops = f.receipts().filter(r => r.op?.startsWith('triage.')).map(r => r.op);
|
||||
assert.deepEqual(ops, ['triage.key.ensure', 'triage.key.rotate', 'triage.key.retire', 'triage.key.rotate']);
|
||||
for (const bus of [a, b]) assert.equal(leaks(eventsText(bus, bus === a)), null);
|
||||
});
|
||||
|
||||
test('the sweep never issues the first key, and a sweep refusal is a receipt line with a code only', async t => {
|
||||
const lines = captureStderr(t);
|
||||
const f = await instance(t);
|
||||
const a = await f.bus('alpha');
|
||||
f.clock.now += 400 * DAY;
|
||||
assert.deepEqual(await a.sweep(), { action: 'none' });
|
||||
assert.equal(f.state.triagePuts, 0);
|
||||
assert.equal(f.record(), null);
|
||||
// A due rotation on a proxy with no sentinel (it may be another proxy): refused, nothing written.
|
||||
await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
f.state.keys = f.state.keys.filter(k => k !== f.sentinel);
|
||||
f.clock.now += 31 * DAY;
|
||||
const r = await a.sweep();
|
||||
assert.deepEqual(r, { action: 'refused', code: 'proxy-not-configured' });
|
||||
assert.equal(f.state.triagePuts, 1);
|
||||
const refusal = lines.join('').split('\n').filter(l => l.includes('triage.key.sweep-refused'));
|
||||
assert.equal(refusal.length, 1);
|
||||
assert.deepEqual(JSON.parse(refusal[0].replace(/^model-proxy: /, '')), { action: 'triage.key.sweep-refused', code: 'proxy-not-configured', reason: 'triage-key-proxy-open' });
|
||||
// The same refusal on the next ticks is not logged again within the hour; after it, once more.
|
||||
const refusals = () => lines.join('').split('\n').filter(l => l.includes('triage.key.sweep-refused')).length;
|
||||
for (let i = 0; i < 3; i++) { f.clock.now += 30_000; assert.equal((await a.sweep()).code, 'proxy-not-configured'); }
|
||||
assert.equal(refusals(), 1);
|
||||
f.clock.now += HOUR;
|
||||
await a.sweep();
|
||||
assert.equal(refusals(), 2);
|
||||
});
|
||||
|
||||
test('a revoke stopped between the proxy write and the file is finished by the next revoke; ensure after a stopped revoke replaces the key', async t => {
|
||||
captureStderr(t);
|
||||
const f = await instance(t);
|
||||
let stage = null;
|
||||
const a = await f.bus('alpha', { triageKeys: { hook: s => { if (s === stage) throw new Error(`stop at ${s}`); } } });
|
||||
await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
stage = 'write';
|
||||
await assert.rejects(a.ask('proxy.triage.key', { op: 'revoke' }), e => e.code === 'storage-unavailable' && !/stop at/.test(e.message));
|
||||
assert.equal(f.triageKeys().length, 0, 'the proxy write happened');
|
||||
assert.ok(f.record(), 'the file stayed');
|
||||
stage = null;
|
||||
// ensure now sees a file whose key the proxy lacks: a new key, the old named lost.
|
||||
const replaced = await a.ask('proxy.triage.key', { op: 'ensure' });
|
||||
assert.equal(replaced.action, 'replaced');
|
||||
stage = 'rename';
|
||||
await assert.rejects(a.ask('proxy.triage.key', { op: 'revoke' }), code('storage-unavailable'));
|
||||
stage = null;
|
||||
assert.equal((await a.ask('proxy.triage.key', { op: 'revoke' })).action, 'revoked');
|
||||
assert.equal(f.record(), null);
|
||||
assert.equal(f.triageKeys().length, 0);
|
||||
});
|
||||
|
||||
test('no answer, event, receipt line or stderr line in this file holds any Triage key the stand-in saw', () => {
|
||||
assert.ok(SEEN.size >= 8, `only ${SEEN.size} keys were seen`);
|
||||
assert.ok(TEXTS.length >= 30, `only ${TEXTS.length} texts`);
|
||||
for (const text of TEXTS) assert.equal(leaks(text), null);
|
||||
});
|
||||
@@ -0,0 +1,125 @@
|
||||
// proxy-scope slice 4 (#1873): the bus verb proxy.triage.key at the broker. Only the instance
|
||||
// owner's path-bound proof (a human cap from the cli transport, outside any agent) reaches the
|
||||
// runtime's hook; an agent, a tester, a Console proof, a grant holder and an organization owner get
|
||||
// human-required, a reader read-only. The answer and the event carry key ids only, and a hook
|
||||
// failure that is not a bus refusal becomes storage-unavailable with no text of its own.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import fs from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { Broker, BusError } from '../src/broker.mjs';
|
||||
import { Store } from '../src/store.mjs';
|
||||
import { REFUSALS } from '../src/refusals.mjs';
|
||||
|
||||
const role = { authority: { withinRole: ['message.send'], crossRole: [] } };
|
||||
const code = name => error => error.code === name;
|
||||
const SUBJECT = { provider: 'fixture', iss: 'https://sso.invalid', sub: 'owner-1' };
|
||||
// A Triage key as triage-key.mjs makes them, so a leak check has a real-looking value to look for.
|
||||
const KEY = `mst-0123456789ab-triage-${'Q'.repeat(20)}xY_-${'z'.repeat(19)}`;
|
||||
|
||||
function fixture(t, { proxyTriageKey = null } = {}) {
|
||||
const store = new Store(fs.mkdtempSync(join(tmpdir(), 'tkv-bus-')));
|
||||
t.after(() => store.close());
|
||||
const b = new Broker({ store, proxyTriageKey, businesses: { demo: { id: 'demo', human: 'owner', arbiters: { technical: 'chief', delivery: 'chief' }, vars: { 'bus.tester': true }, roles: { chief: { definition: 'chief-of-staff', authority: { withinRole: ['decision.resolve.technical', 'decision.resolve.delivery'], crossRole: [] } }, coder: role } } } });
|
||||
return {
|
||||
store, b,
|
||||
human: b.bindHuman({ business: 'demo', human: 'owner', via: 'cli', outsideAgent: true }),
|
||||
tester: b.bindTester({ business: 'demo' }), reader: b.bindReader({ business: 'demo' }),
|
||||
agent: b.bindLaunch({ business: 'demo', role: 'coder', run: 'tkv-run', harness: 'pi' }),
|
||||
// The owner's Console proof, a granted account that holds org.hire, and an organization owner
|
||||
// who is not the instance owner: none is the path-bound cli proof.
|
||||
console: b.bindConsole({ business: 'demo', human: 'owner', subject: SUBJECT }),
|
||||
grant: b.bindHireConsole({ business: 'demo', actor: 'grantee', subject: { ...SUBJECT, sub: 'grantee-1' }, rights: ['org.hire', 'decide'] }),
|
||||
orgOwner: b.bindHireConsole({ business: 'demo', actor: 'orgowner', subject: { ...SUBJECT, sub: 'orgowner-1' }, rights: ['org.hire'], owner: true }),
|
||||
};
|
||||
}
|
||||
const events = store => JSON.stringify(store.all('SELECT body FROM events'));
|
||||
const bodies = store => store.all('SELECT body FROM events').map(r => JSON.parse(r.body));
|
||||
|
||||
test('proxy.triage.key refuses an agent, a tester, a Console proof, a grant holder and an organization owner with human-required, and a reader with read-only', async t => {
|
||||
let calls = 0;
|
||||
const f = fixture(t, { proxyTriageKey: async () => { calls++; return { action: 'issued', keyId: '0123456789ab', rotateBy: '2026-12-19', addedKeyIds: ['0123456789ab'], removedKeyIds: [] }; } });
|
||||
for (const op of ['ensure', 'revoke']) {
|
||||
for (const [name, cap] of [['agent', f.agent], ['tester', f.tester], ['console', f.console], ['grant', f.grant], ['orgOwner', f.orgOwner]])
|
||||
assert.throws(() => f.b.request(cap, { verb: 'proxy.triage.key', args: { op } }), code('human-required'), `${name} ${op}`);
|
||||
assert.throws(() => f.b.request(f.reader, { verb: 'proxy.triage.key', args: { op } }), code('read-only'));
|
||||
}
|
||||
assert.equal(calls, 0);
|
||||
// The same verb runs for the owner's cli proof.
|
||||
const answer = await f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } });
|
||||
assert.equal(answer.action, 'issued');
|
||||
assert.equal(calls, 1);
|
||||
});
|
||||
|
||||
test('proxy.triage.key takes op ensure or revoke and nothing else, and a bus with no hook is not-configured', async t => {
|
||||
let calls = 0;
|
||||
const f = fixture(t, { proxyTriageKey: async () => { calls++; return { action: 'none', addedKeyIds: [], removedKeyIds: [] }; } });
|
||||
for (const args of [{}, { op: 'rotate' }, { op: 'issue' }, { op: 'ensure', key: KEY }, { op: 'revoke', force: true }, { op: ['ensure'] }])
|
||||
assert.throws(() => f.b.request(f.human, { verb: 'proxy.triage.key', args }), code('invalid-request'), JSON.stringify(args));
|
||||
assert.throws(() => f.b.request(f.human, { verb: 'proxy.triage.key' }), code('invalid-request'));
|
||||
assert.equal(calls, 0);
|
||||
assert.equal(events(f.store).includes(KEY), false);
|
||||
assert.equal(events(f.store).includes(KEY.slice(-43)), false);
|
||||
const g = fixture(t);
|
||||
assert.throws(() => g.b.request(g.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } }), code('not-configured'));
|
||||
});
|
||||
|
||||
test('the answer and the event of each op carry ids only, even when the hook returns more', async t => {
|
||||
const seen = [];
|
||||
const f = fixture(t, { proxyTriageKey: async op => {
|
||||
seen.push(op);
|
||||
// A hook that returns the key beside the ids: the broker copies the named fields only.
|
||||
return op === 'ensure'
|
||||
? { action: 'issued', keyId: '0123456789ab', rotateBy: '2026-12-19', addedKeyIds: ['0123456789ab'], removedKeyIds: ['ba9876543210'], key: KEY, old: { key: KEY } }
|
||||
: { action: 'revoked', addedKeyIds: [], removedKeyIds: ['0123456789ab'], key: KEY };
|
||||
} });
|
||||
const ensure = await f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } });
|
||||
assert.deepEqual(ensure, { op: 'ensure', action: 'issued', addedKeyIds: ['0123456789ab'], removedKeyIds: ['ba9876543210'], keyId: '0123456789ab', rotateBy: '2026-12-19' });
|
||||
const revoke = await f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'revoke' } });
|
||||
assert.deepEqual(revoke, { op: 'revoke', action: 'revoked', addedKeyIds: [], removedKeyIds: ['0123456789ab'] });
|
||||
assert.deepEqual(seen, ['ensure', 'revoke']);
|
||||
assert.deepEqual(bodies(f.store).filter(b => b.call === 'proxy.triage.key'), [{ call: 'proxy.triage.key', ...ensure }, { call: 'proxy.triage.key', ...revoke }]);
|
||||
for (const text of [JSON.stringify(ensure), JSON.stringify(revoke), events(f.store)]) {
|
||||
assert.equal(text.includes(KEY), false);
|
||||
assert.equal(text.includes(KEY.slice(-43)), false);
|
||||
}
|
||||
});
|
||||
|
||||
test('an answer the broker cannot vouch for refuses proxy-client-response and records no allowed event', async t => {
|
||||
const answers = [
|
||||
null, { action: 'issued' },
|
||||
{ action: 'deleted', keyId: '0123456789ab', addedKeyIds: [], removedKeyIds: [] },
|
||||
{ action: 'issued', keyId: KEY, addedKeyIds: [], removedKeyIds: [] },
|
||||
{ action: 'issued', keyId: '0123456789ab', addedKeyIds: [KEY], removedKeyIds: [] },
|
||||
{ action: 'held', keyId: '0123456789ab', addedKeyIds: [], removedKeyIds: 'none' },
|
||||
];
|
||||
let next = 0;
|
||||
const f = fixture(t, { proxyTriageKey: async () => answers[next++] });
|
||||
for (const answer of answers)
|
||||
await assert.rejects(f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } }), code('proxy-client-response'), JSON.stringify(answer));
|
||||
await assert.rejects(Promise.resolve().then(() => fixture(t, { proxyTriageKey: async () => ({ action: 'issued', addedKeyIds: [], removedKeyIds: [] }) }))
|
||||
.then(g => g.b.request(g.human, { verb: 'proxy.triage.key', args: { op: 'revoke' } })), code('proxy-client-response'));
|
||||
assert.equal(bodies(f.store).some(b => b.call === 'proxy.triage.key'), false);
|
||||
assert.equal(events(f.store).includes(KEY.slice(-43)), false);
|
||||
});
|
||||
|
||||
test('a bus refusal from the hook passes through with its guidance; any other failure is storage-unavailable and names nothing from the error', async t => {
|
||||
const f = fixture(t, { proxyTriageKey: async op => { throw op === 'ensure' ? new BusError('triage-no-riding-bus') : Object.assign(new Error(`boom ${KEY} /home/x`), { code: 'EACCES' }); } });
|
||||
await assert.rejects(f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'ensure' } }), e => e.code === 'triage-no-riding-bus'
|
||||
&& /Triage needs one organization that uses the owner's models/.test(e.nextStep ?? REFUSALS['triage-no-riding-bus'].nextStep));
|
||||
await assert.rejects(f.b.request(f.human, { verb: 'proxy.triage.key', args: { op: 'revoke' } }), e => e.code === 'storage-unavailable'
|
||||
&& !JSON.stringify({ ...e, message: e.message }).includes(KEY.slice(-43)) && !String(e.message).includes('/home/x'));
|
||||
assert.equal(events(f.store).includes(KEY.slice(-43)), false);
|
||||
});
|
||||
|
||||
test('the two slice 4 codes have refusal-table entries with a next step and no absolute path', () => {
|
||||
for (const c of ['triage-no-riding-bus', 'triage-key-rotated']) {
|
||||
const entry = REFUSALS[c];
|
||||
assert.ok(entry, c);
|
||||
assert.equal(typeof entry.nextStep, 'string', c);
|
||||
assert.ok(entry.nextStep.length > 20, c);
|
||||
assert.equal(/\/(home|mnt|srv|tmp)\//.test(entry.nextStep), false, c);
|
||||
}
|
||||
assert.match(REFUSALS['triage-no-riding-bus'].nextStep, /^Triage needs one organization that uses the owner's models\./);
|
||||
});
|
||||
@@ -16,7 +16,7 @@
|
||||
//
|
||||
// mosaic triage key issue | rotate | retire | status
|
||||
//
|
||||
// Triage's own model proxy client key (packages/cli/src/triage-key.mjs). Human verbs as well,
|
||||
// Triage's own model proxy client key (packages/bus/src/triage-key.mjs, shared with the bus). Human verbs as well,
|
||||
// refused in an agent run before anything is read. No output names a key, only its id.
|
||||
import { lstatSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
@@ -24,7 +24,7 @@ import { fileURLToPath } from "node:url";
|
||||
import { readRoute, RouteStoreError, writeRoute } from "../../webui/src/triage-route-store.mjs";
|
||||
import { loadSystem } from "./config.mjs";
|
||||
import { configDir } from "../../organization/src/organization-config.mjs";
|
||||
import { issueKey, keyPaths, keyStatus, retireKeys, rotateKey, TriageKeyError } from "./triage-key.mjs";
|
||||
import { issueKey, keyPaths, keyStatus, retireKeys, rotateKey, TriageKeyError } from "../../bus/src/triage-key.mjs";
|
||||
import { CliError } from "./errors.mjs";
|
||||
import { refuseInsideAgent } from "./transport.mjs";
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import { join } from "node:path";
|
||||
import { createHash, randomBytes } from "node:crypto";
|
||||
import { run } from "../src/cli.mjs";
|
||||
import { AGENT_MARKERS } from "../src/transport.mjs";
|
||||
import { DEFAULT_ROTATE_GRACE_MINUTES, keyPaths, retireAtFor, rotateByFor } from "../src/triage-key.mjs";
|
||||
import { DEFAULT_ROTATE_GRACE_MINUTES, keyPaths, retireAtFor, rotateByFor } from "../../bus/src/triage-key.mjs";
|
||||
import { ModelKeys } from "../../bus/src/model-keys.mjs";
|
||||
import { ManagementClient } from "../../model-proxy/src/client.mjs";
|
||||
import { startBroker } from "../../bus/src/runtime.mjs";
|
||||
|
||||
@@ -45,6 +45,8 @@ import { composeSnapshot, readSystemConfig, readOrgConfig } from './triage-snaps
|
||||
import { busesStopped } from '../../cli/src/proxy-admin-host.mjs';
|
||||
import { agentLevelsWriteRoutes } from './agent-levels-write.mjs';
|
||||
import { triageRouteRoutes } from './triage-route-http.mjs';
|
||||
import { keyState, ridingOrgs, triageKeyCall } from './triage-access.mjs';
|
||||
import { readKey as readTriageKey } from '../../bus/src/triage-key-file.mjs';
|
||||
import { RESTART_EXIT } from '../../cli/src/org-console.mjs';
|
||||
import { ID_PATTERN, systemOnly } from '../../organization/src/vocabulary.mjs';
|
||||
import { selectorScope } from '../../organization/src/selector.mjs';
|
||||
@@ -226,7 +228,7 @@ try {
|
||||
// (read only) and, through `call`, from its bus as the owner. A file that cannot be read gives
|
||||
// undefined fields, which Ride all names as unreadable. The change log is read with the writer's
|
||||
// own check (no symlink, a regular file this user owns, not group or world writable).
|
||||
const rideRiders = async () => {
|
||||
const rideRiders = async ({ timeoutMs = 5000, deadline } = {}) => {
|
||||
const writer = organizationWriter({ dir: configDir(), rolesDir: rolesDir() });
|
||||
const local = id => readRider({ id, read: () => writer.read(id).document, readLog: () => ownedText(writer.logFile(id), { mask: 0o022 }), isFixture: orgUsesFixture });
|
||||
const down = async () => { throw Object.assign(new Error('the bus is not reachable'), { code: 'no-bus-host' }); };
|
||||
@@ -235,7 +237,7 @@ try {
|
||||
const out = [];
|
||||
for (const org of await listOrgs({ configDir: configDir(), dataRoot: system.dataRoot, instance })) {
|
||||
if (org.state !== 'active') continue;
|
||||
let call = down; try { const paths = orgPaths(org.root); call = busCaller({ dataRoot: org.root, socket: paths.socket, business: org.id, timeoutMs: 5000 }); } catch {}
|
||||
let call = down; try { const paths = orgPaths(org.root); call = busCaller({ dataRoot: org.root, socket: paths.socket, business: org.id, timeoutMs, ...(deadline ? { deadline } : {}) }); } catch {}
|
||||
// The organization admin page's stopped test reads this bus status (its process id).
|
||||
out.push({ ...local(org.id), call, status: () => orgBusStatus({ configDir: configDir(), dataRoot: system.dataRoot, instance }, org.id) });
|
||||
}
|
||||
@@ -292,6 +294,8 @@ try {
|
||||
const triage = triageRoute ? createTriageHost({
|
||||
dataRoot: system.dataRoot, route: triageRoute, proxyPort: triageProxyPort, tokenPattern: triageTokenPattern(configDir()),
|
||||
devOwner: Boolean(devOwner), log: line => console.log(line),
|
||||
// proxy-scope slice 4: a session on a key past its retireAt stops with triage-key-rotated.
|
||||
keyRecord: () => readTriageKey(configDir()),
|
||||
readSnapshot: () => composeSnapshot({
|
||||
orgs: () => listOrgs({ configDir: configDir(), dataRoot: system.dataRoot, instance }),
|
||||
busStatus: id => orgBusStatus({ configDir: configDir(), dataRoot: system.dataRoot, instance }, id),
|
||||
@@ -325,6 +329,22 @@ try {
|
||||
// next admit.
|
||||
const triageRoutes = system && (devOwner || ssoOn) ? [triageRouteRoutes({
|
||||
dataRoot: system.dataRoot, log: line => console.log(line),
|
||||
// proxy-scope slice 4: the Triage key through one riding organization's bus (proxy.triage.key),
|
||||
// as Proxy setup's init asks one bus. The Console reads the key file's state only; it never
|
||||
// loads the proxy management client or the key verbs' module.
|
||||
access: {
|
||||
state: () => keyState(configDir()),
|
||||
// The same list the key walk asks (cliproxy, not a fixture), read from local files only.
|
||||
// The owner's proxy must be set up (proxy.json names a port), as ownerModels reads it.
|
||||
riding: async () => {
|
||||
let proxy = false;
|
||||
try { proxy = Number.isInteger(loadProxy(configDir(), system.dataRoot)?.port); } catch {}
|
||||
return proxy && ridingOrgs(await rideRiders()).length > 0;
|
||||
},
|
||||
// One 30 s deadline over the whole walk, not 30 s for each bus.
|
||||
key: op => { const deadline = Date.now() + 30000; return triageKeyCall({ riders: () => rideRiders({ timeoutMs: 30000, deadline }), op }); },
|
||||
stop: () => (triage ? triage.stop() : Promise.resolve()),
|
||||
},
|
||||
catalogues: async () => {
|
||||
const read = async call => { try { return (await call('routes'))?.catalogue ?? { status: 'unknown' }; } catch { return { status: 'bus-unavailable' }; } };
|
||||
if (!organizations) return ownerCall ? [await read(ownerCall)] : [];
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
const $ = id => document.getElementById(id);
|
||||
const esc = s => String(s).replace(/[&<>"']/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]);
|
||||
const PATH = '/api/triage/route';
|
||||
// proxy-scope slice 4 (#1873): Repair access and Turn Triage off (triage-route-http.mjs).
|
||||
const ACCESS_PATH = '/api/triage/access';
|
||||
// The time zones this browser knows, offered as suggestions only; the server decides.
|
||||
const zones = (() => { try { return Intl.supportedValuesOf('timeZone'); } catch { return []; } })();
|
||||
// draft holds the typed fields; base is the revision they were read at, which a save sends, so a
|
||||
@@ -68,10 +70,47 @@
|
||||
+ `</form>${send ? '' : '<p class="small muted" id="triage-route-readonly">This Console is read-only, so the route cannot be saved here.</p>'}`;
|
||||
}
|
||||
|
||||
// proxy-scope slice 4: the key's state, ids and days only, and the two access buttons.
|
||||
let accessLine = '';
|
||||
function access() {
|
||||
const a = data?.access;
|
||||
if (!a || typeof a !== 'object') return '';
|
||||
const state = a.key === 'issued' ? `Triage key ${esc(a.keyId)} is issued. It rotates on ${esc(a.rotateBy)}.`
|
||||
: a.key === 'missing' ? 'No Triage key is issued, so Triage will not start.'
|
||||
: 'The Triage key file is not safe to use, so Triage will not start.';
|
||||
const warn = [a.rotation === 'rotation overdue' ? 'Key rotation overdue.' : '', a.code === 'triage-no-riding-bus' ? `${a.message}.` : ''].filter(Boolean).join(' ');
|
||||
const off = busy || data?.send !== true ? ' disabled' : '';
|
||||
return `<div id="triage-access" data-key="${esc(a.key)}" data-code="${esc(a.code ?? '')}" data-rotation="${esc(a.rotation ?? '')}"><p class="small">${state}</p>`
|
||||
+ (warn ? `<p class="small warn" id="triage-access-warn">${esc(warn)}</p>` : '')
|
||||
+ `<div class="settings-actions"><button type="button" class="btn" id="triage-access-repair"${off}>Repair access</button> <button type="button" class="btn" id="triage-access-off"${off}>Turn Triage off</button> <span role="status" id="triage-access-line" tabindex="-1">${esc(accessLine)}</span></div></div>`;
|
||||
}
|
||||
|
||||
// Each key action in its own plain sentence (proxy.triage.key answers).
|
||||
const ACTION_LINE = {
|
||||
issued: 'A Triage key was issued.', replaced: 'A new Triage key replaced one the proxy no longer held.',
|
||||
held: 'The Triage key is in place.', revoked: 'Triage is off. Its key was removed.', none: 'Triage is off. No key was held.',
|
||||
};
|
||||
async function accessOp(op) {
|
||||
if (busy || data?.send !== true) return;
|
||||
if (op === 'off' && typeof window.confirm === 'function' && !window.confirm('Turn Triage off? This stops any Triage session and removes its key. Repair access issues a new one.')) return;
|
||||
busy = true; accessLine = op === 'off' ? 'Turning Triage off…' : 'Repairing access…'; repaint();
|
||||
let r, a = null;
|
||||
try { r = await window.fetch(ACCESS_PATH, { cache: 'no-store', redirect: 'manual', method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ op }) }); }
|
||||
catch { busy = false; accessLine = 'Outcome unknown. Reload the page to see the key state.'; repaint(); return; }
|
||||
try { a = await r.json(); } catch { /* refused below */ }
|
||||
busy = false;
|
||||
if (a?.access && data) data = { ...data, access: a.access };
|
||||
if (r.type === 'opaqueredirect') accessLine = 'Sign in again, then retry.';
|
||||
else if (r.ok) accessLine = ACTION_LINE[a?.key?.action] ?? (op === 'off' ? 'Triage is off.' : 'Access was repaired.');
|
||||
else accessLine = typeof a?.message === 'string' ? a.message : 'The Triage key was not changed.';
|
||||
repaint();
|
||||
$('triage-access-line')?.focus?.();
|
||||
}
|
||||
|
||||
function markup() {
|
||||
const body = data === null
|
||||
? `<p class="small warn">${esc(readLine || 'The Triage route could not be read.')}</p><p role="status" id="triage-route-line" tabindex="-1">${esc(line)}</p>`
|
||||
: `${stored()}${form()}<p class="small muted" id="triage-route-note">${esc(data.note ?? 'Saving a route does not start Triage. Triage also needs its key.')}</p>`;
|
||||
: `${stored()}${access()}${form()}<p class="small muted" id="triage-route-note">${esc(data.note ?? 'Saving a route does not start Triage. Triage also needs its key.')}</p>`;
|
||||
return `<section class="panel" id="triage-route" aria-labelledby="triage-route-title"><h3 id="triage-route-title">Triage model route</h3>${body}</section>`;
|
||||
}
|
||||
const repaint = () => { const panel = $('triage-route'); if (panel) panel.outerHTML = markup(); };
|
||||
@@ -93,7 +132,7 @@
|
||||
if (r.ok && a?.route) {
|
||||
data = { ...data, route: a.route, revision: a.revision, routeMessage: undefined };
|
||||
draft = fromRoute(a.route); base = a.revision;
|
||||
line = `Saved ${savedAt(a.route.updatedAt)}.`;
|
||||
line = `Saved ${savedAt(a.route.updatedAt)}.` + (a.key?.error ? ` ${a.key.message}` : ACTION_LINE[a.key?.action] ? ` ${ACTION_LINE[a.key.action]}` : '');
|
||||
repaint();
|
||||
$('triage-route-line')?.focus?.();
|
||||
return;
|
||||
@@ -127,7 +166,11 @@
|
||||
repaint(); $('triage-route-model')?.focus();
|
||||
});
|
||||
document.addEventListener('submit', e => { if (e.target?.id !== 'triage-route-form') return; e.preventDefault(); save(); });
|
||||
document.addEventListener('click', e => { if (e.target?.id === 'triage-route-reload') reload(); });
|
||||
document.addEventListener('click', e => {
|
||||
if (e.target?.id === 'triage-route-reload') reload();
|
||||
if (e.target?.id === 'triage-access-repair') accessOp('repair');
|
||||
if (e.target?.id === 'triage-access-off') accessOp('off');
|
||||
});
|
||||
|
||||
const hasDraft = () => !!(draft && data && JSON.stringify(draft) !== JSON.stringify(fromRoute(data.route)));
|
||||
window.MosaicTriageRoute = { section, hasDraft };
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
// proxy-scope slice 4 (#1873): the Console's side of the Triage key. The Console never holds the
|
||||
// proxy management key, so it never issues or removes a Triage key itself: it asks one riding
|
||||
// organization's bus to, through the bus verb `proxy.triage.key` (op ensure or revoke), as the owner,
|
||||
// the way Proxy setup's init asks one bus for `proxy.init` (packages/model-proxy/src/admin-control.mjs).
|
||||
// A riding organization lists cliproxy and is not a fixture organization; its bus decides for itself
|
||||
// whether it holds the owner's proxy client, and refuses triage-no-riding-bus when it does not.
|
||||
//
|
||||
// A bus that is down or not riding did nothing, so it is passed over and the next one is asked. A bus
|
||||
// whose outcome is unknown (a timeout, or an answer that could not be read) may have done the work, so
|
||||
// the walk stops there and the caller shows the key file's state. Any other refusal is the answer.
|
||||
// With no bus left the answer is triage-no-riding-bus.
|
||||
//
|
||||
// The key file is read only through packages/bus/src/triage-key-file.mjs, the reader with no proxy
|
||||
// client and no lock. No answer here holds a key: only ids, days and codes.
|
||||
import { readKey } from '../../bus/src/triage-key-file.mjs';
|
||||
|
||||
export const NO_RIDING_MESSAGE = "Triage needs one organization that uses the owner's models";
|
||||
// A bus sweep runs every 30 s; a rotation still not done this long after its day began means no
|
||||
// riding bus could act.
|
||||
export const OVERDUE_ALLOWANCE_MS = 10 * 60 * 1000;
|
||||
// A refusal that says only "not this bus": ask the next one.
|
||||
const NEXT = new Set(['triage-no-riding-bus', 'not-configured', 'no-bus-host', 'unknown-verb', 'bus-unavailable']);
|
||||
// An outcome the Console cannot know (webui bus.mjs OPEN): stop, and answer outcome-unknown.
|
||||
const OPEN = new Set(['outcome-unknown', 'invalid-response', 'response-too-large']);
|
||||
const ACTIONS = new Set(['issued', 'held', 'replaced', 'revoked', 'none']);
|
||||
const KEY_ID = /^[0-9a-f]{12}$/;
|
||||
const DAY = /^\d{4}-\d{2}-\d{2}$/;
|
||||
|
||||
const ids = v => (Array.isArray(v) ? v.filter(x => typeof x === 'string' && KEY_ID.test(x)) : []);
|
||||
// The fields the Console keeps from a bus answer, re-checked here: ids and a day, never a key.
|
||||
const clean = (op, a) => ({
|
||||
ok: true, op,
|
||||
action: ACTIONS.has(a?.action) ? a.action : 'unknown',
|
||||
addedKeyIds: ids(a?.addedKeyIds), removedKeyIds: ids(a?.removedKeyIds),
|
||||
...(op === 'ensure' && typeof a?.keyId === 'string' && KEY_ID.test(a.keyId) ? { keyId: a.keyId } : {}),
|
||||
...(op === 'ensure' && typeof a?.rotateBy === 'string' && DAY.test(a.rotateBy) ? { rotateBy: a.rotateBy } : {}),
|
||||
});
|
||||
|
||||
// triageKeyCall({ riders, op }) -> { ok: true, op, action, addedKeyIds, removedKeyIds, keyId?, rotateBy?, org }
|
||||
// | { ok: false, code, org? }
|
||||
// riders async () -> [{ id, fixture, cliproxy, call }] (webui cli.mjs rideRiders)
|
||||
// The organizations whose bus may hold the Triage key: they list cliproxy and are not fixtures. The
|
||||
// status reads the same list, so it says no riding organization exactly when the walk has none to ask.
|
||||
export const ridingOrgs = list => (Array.isArray(list) ? list : []).filter(r => r?.cliproxy === true && r.fixture !== true && typeof r.call === 'function');
|
||||
|
||||
export async function triageKeyCall({ riders, op }) {
|
||||
if (op !== 'ensure' && op !== 'revoke') return { ok: false, code: 'invalid-request' };
|
||||
let list = [];
|
||||
try { list = await riders(); } catch { list = []; }
|
||||
for (const r of ridingOrgs(list)) {
|
||||
let answer;
|
||||
try { answer = await r.call('proxy.triage.key', { op }); }
|
||||
catch (e) {
|
||||
const code = typeof e?.code === 'string' && /^[a-z][a-z0-9-]{0,63}$/.test(e.code) ? e.code : 'outcome-unknown';
|
||||
if (NEXT.has(code)) continue;
|
||||
return { ok: false, code: OPEN.has(code) ? 'outcome-unknown' : code, org: r.id };
|
||||
}
|
||||
return { ...clean(op, answer), org: r.id };
|
||||
}
|
||||
return { ok: false, code: 'triage-no-riding-bus' };
|
||||
}
|
||||
|
||||
// The key's state from the file alone, for Settings > Triage: never the key, only whether one is
|
||||
// held, its id, its rotation day, how many old keys are in grace, and whether the rotation is overdue.
|
||||
// { key: 'missing' } | { key: 'unsafe' } | { key: 'issued', keyId, rotateBy, retiring, overdue }
|
||||
export function keyState(configDir, { now = () => Date.now(), keyOptions = {} } = {}) {
|
||||
let record;
|
||||
try { record = readKey(configDir, keyOptions); } catch { return { key: 'unsafe' }; }
|
||||
if (!record) return { key: 'missing' };
|
||||
const due = Date.parse(`${record.rotateBy}T00:00:00Z`);
|
||||
return {
|
||||
key: 'issued', keyId: record.keyId, rotateBy: record.rotateBy, retiring: record.retiring.length,
|
||||
due: Number.isFinite(due) && now() >= due,
|
||||
overdue: Number.isFinite(due) && now() > due + OVERDUE_ALLOWANCE_MS,
|
||||
};
|
||||
}
|
||||
|
||||
// The status Settings > Triage shows: the key state, and the named code when no organization rides
|
||||
// the owner's models (so no bus can issue, rotate or retire the key). The rotation is overdue when
|
||||
// its day has begun and no riding bus can act, or when it is still not done well after (a riding
|
||||
// organization whose bus did not rotate it).
|
||||
export function accessStatus(state, { riding }) {
|
||||
const out = { ...state, riding: riding === true };
|
||||
if (riding !== true) Object.assign(out, { code: 'triage-no-riding-bus', message: NO_RIDING_MESSAGE });
|
||||
if (state.key === 'issued' && (state.overdue || (state.due && riding !== true))) out.rotation = 'rotation overdue';
|
||||
return out;
|
||||
}
|
||||
@@ -91,6 +91,12 @@ export const USAGE_NOT_READ = "Triage could not read its token usage or its cap,
|
||||
// #1873 triage-wiring: two turns in a row whose usage reads unknown end the session (cap fail closed).
|
||||
export const UNKNOWN_TURNS_MAX = 2;
|
||||
export const USAGE_UNKNOWN_ENDED = "Triage ended this session: the token usage of two turns in a row could not be read, so the daily cap could not be kept. The next message starts a new session.";
|
||||
// proxy-scope slice 4 (#1873): a session still running on a key whose grace has ended (its retireAt
|
||||
// in the key file has passed, or the key is gone from the file) stops with this code, before the bus
|
||||
// sweep removes the key from the proxy (the sweep waits 30 s past retireAt).
|
||||
export const KEY_ROTATED_CODE = "triage-key-rotated";
|
||||
export const KEY_ROTATED = "Triage stopped this session: its key was rotated and the old key's grace ended. The next message starts a new session on the current key.";
|
||||
export const KEY_POLL_MS = 5000;
|
||||
// #1873 triage-wiring: the route's refusals (triage-route.mjs), each with the one plain line the
|
||||
// drawer shows. None carries a path, a key or an env value.
|
||||
export const ROUTE_REFUSALS = Object.freeze({
|
||||
@@ -323,13 +329,18 @@ export function createRelay({ socket, cap, take, read, onSend, usage = null, sto
|
||||
// repository's packages/plugins/harness/<name> by default
|
||||
// home HOME, which no mount may reach outside the stack's own code; the Console user's by default
|
||||
// usageSource { readTurn({ harness, root }) } the transcript usage-source; the repository's own by default
|
||||
export function createTriageHost({ dataRoot, route = null, readSnapshot = async () => null, env = process.env, node = process.execPath, bwrap = BWRAP, now = () => new Date(), devOwner = false, log = () => {}, roleDir = join(REPO, "roles"), readOnly = [], pollInterval = null, proxyPort = null, tokenPattern = null, harnessModule = repoHarnessModule, home = homedir(), usageSource = { readTurn } }) {
|
||||
// keyRecord () -> the Triage key file's record (packages/bus/src/triage-key-file.mjs readKey) or
|
||||
// null; throws when unsafe. With it, a session on a key past its retireAt, or a key
|
||||
// gone from the file, stops with triage-key-rotated. Without it, nothing is checked.
|
||||
// keyPollMs how often a running session's key is checked
|
||||
export function createTriageHost({ dataRoot, route = null, readSnapshot = async () => null, env = process.env, node = process.execPath, bwrap = BWRAP, now = () => new Date(), devOwner = false, log = () => {}, roleDir = join(REPO, "roles"), readOnly = [], pollInterval = null, proxyPort = null, tokenPattern = null, harnessModule = repoHarnessModule, home = homedir(), usageSource = { readTurn }, keyRecord = null, keyPollMs = KEY_POLL_MS }) {
|
||||
if (typeof dataRoot !== "string" || !dataRoot.startsWith("/")) throw new TriageError("triage-no-data-root", "no data root");
|
||||
const validPort = (p) => Number.isInteger(p) && p > 0 && p <= 65535;
|
||||
if (proxyPort !== null && typeof proxyPort !== "function" && !validPort(proxyPort)) throw new TypeError("proxyPort must be a port number, null or a function");
|
||||
if (tokenPattern !== null && !(tokenPattern instanceof RegExp)) throw new TypeError("tokenPattern must be a RegExp or null");
|
||||
if (typeof harnessModule !== "function") throw new TypeError("harnessModule must be a function");
|
||||
if (typeof usageSource?.readTurn !== "function") throw new TypeError("usageSource must have readTurn");
|
||||
if (keyRecord !== null && typeof keyRecord !== "function") throw new TypeError("keyRecord must be a function or null");
|
||||
const tokenShape = tokenPattern && new RegExp(`^(?:${tokenPattern.source})$`, tokenPattern.flags.replace(/[gmy]/g, ""));
|
||||
const tokenAnywhere = tokenPattern && new RegExp(tokenPattern.source, tokenPattern.flags.replace(/[gmy]/g, "") + "g");
|
||||
// What no mount source may lie in or around, besides the run directory and the fixed list. HOME
|
||||
@@ -355,6 +366,11 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
// host/ too, so a Console restart between the end and the next message keeps it.
|
||||
let freshSession = false;
|
||||
const badSeen = new Set();
|
||||
// proxy-scope slice 4: the key the running session holds (memory only, never written or logged),
|
||||
// the timer that checks it, and the last stop the host made for a rotated key.
|
||||
let heldKey = null;
|
||||
let keyTimer = null;
|
||||
let lastStop = null;
|
||||
|
||||
const ensureDirs = () => {
|
||||
mkdirSync(dataRoot, { recursive: true, mode: 0o700 });
|
||||
@@ -765,6 +781,49 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
return true;
|
||||
};
|
||||
|
||||
// proxy-scope slice 4. Where the held key stands in the key file: "current", the retireAt (ms) of
|
||||
// the retiring entry that holds it, "gone", or "unknown" (no reader, no key, or an unsafe file:
|
||||
// nothing is decided on a file that cannot be read; the next admit refuses it).
|
||||
const keyStanding = (held) => {
|
||||
if (!keyRecord || typeof held !== "string" || !held) return "unknown";
|
||||
let r;
|
||||
try { r = keyRecord(); } catch { return "unknown"; }
|
||||
if (!r) return "gone";
|
||||
if (r.key === held) return "current";
|
||||
const old = Array.isArray(r.retiring) ? r.retiring.find((x) => x?.key === held) : null;
|
||||
if (!old) return "gone";
|
||||
const at = Date.parse(old.retireAt);
|
||||
return Number.isFinite(at) ? at : "gone";
|
||||
};
|
||||
const keyDue = (held) => {
|
||||
const s = keyStanding(held);
|
||||
return s === "gone" || (typeof s === "number" && s <= now().getTime());
|
||||
};
|
||||
// SIGTERM, then SIGKILL after the grace; resolves when the process has exited (or 2 s after SIGKILL).
|
||||
const endChild = (c, graceMs) => new Promise((resolve) => {
|
||||
if (!c || c.exitCode !== null || c.signalCode !== null) return resolve();
|
||||
let kill = null, last = null;
|
||||
c.once("exit", () => { clearTimeout(kill); clearTimeout(last); resolve(); });
|
||||
try { process.kill(-c.pid, "SIGTERM"); } catch {}
|
||||
kill = setTimeout(() => {
|
||||
try { process.kill(-c.pid, "SIGKILL"); } catch {}
|
||||
last = setTimeout(resolve, 2000);
|
||||
}, graceMs);
|
||||
});
|
||||
// The session on a rotated key stops: a host line says why, the runner is told its session is gone
|
||||
// at its next state or receive, and the process group ends. Queued messages get their lines at exit.
|
||||
let rotating = null;
|
||||
const stopRotated = (c, { graceMs = 5000 } = {}) => rotating ??= (async () => {
|
||||
if (!c || child !== c) return;
|
||||
if (usage) usage.halted = true;
|
||||
lastStop = { code: KEY_ROTATED_CODE, at: now().toISOString() };
|
||||
log(`triage host: stopping the session: ${KEY_ROTATED_CODE}`);
|
||||
try { append({ at: now().toISOString(), from: "host", text: KEY_ROTATED }); }
|
||||
catch { log("triage host: transcript write failed"); }
|
||||
await endChild(c, graceMs);
|
||||
})().finally(() => { rotating = null; });
|
||||
const clearKeyTimer = () => { if (keyTimer) clearInterval(keyTimer); keyTimer = null; };
|
||||
|
||||
const start = async ({ admitted, installRoots, transcriptRoot, mod }) => {
|
||||
ensureDirs();
|
||||
const { run, harnessFiles } = writeBundle(admitted, mod);
|
||||
@@ -837,6 +896,8 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
if (child !== mine) return;
|
||||
lastExit = { code: Number.isInteger(code) ? code : null, signal: signal ?? null, at: now().toISOString() };
|
||||
child = null;
|
||||
heldKey = null;
|
||||
clearKeyTimer();
|
||||
// A turn the session began and never ended (killed, crashed) still spent tokens: it gets its line.
|
||||
// The open turns are read when this runs, after any usage.end already in the chain.
|
||||
chainUsage(async () => { for (const k of [...u.baselines.keys()]) await finishTurn(u, k); });
|
||||
@@ -857,6 +918,13 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
log(`triage host: launcher did not run: ${e.code ?? "error"}`);
|
||||
throw new TriageError("triage-start-failed", "the Triage session did not start");
|
||||
}
|
||||
// proxy-scope slice 4: the key this session holds is checked while it runs.
|
||||
heldKey = key;
|
||||
clearKeyTimer();
|
||||
if (keyRecord && key) {
|
||||
keyTimer = setInterval(() => { if (child === mine && keyDue(key)) stopRotated(mine).catch(() => {}); }, keyPollMs);
|
||||
keyTimer.unref?.();
|
||||
}
|
||||
};
|
||||
|
||||
// The cap is the route's to count. Once it answers over the cap with a time in the next two days,
|
||||
@@ -908,6 +976,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
resetsAt: unset ? "not set" : c.resetsAt ?? "not read",
|
||||
running: child !== null,
|
||||
lastExit,
|
||||
lastStop,
|
||||
snapshot: last,
|
||||
transcript: transcript(),
|
||||
};
|
||||
@@ -935,6 +1004,9 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
try { append({ at: now().toISOString(), from: "host", in_reply_to: id, text: e?.code === "triage-route-invalid" ? ROUTE_REFUSED : NOT_STARTED }); } catch {}
|
||||
throw e instanceof TriageError ? e : new TriageError("triage-route-invalid", "the model route's answer is not usable");
|
||||
}
|
||||
// proxy-scope slice 4: a session on a key past its grace stops before this message is queued,
|
||||
// so the message starts a new session on the key the route answered with.
|
||||
if (child && keyDue(heldKey)) await stopRotated(child);
|
||||
queue.push({ id, body: text, at, delivered: false });
|
||||
if (!child) {
|
||||
starting ??= start(checked).finally(() => { starting = null; });
|
||||
@@ -952,6 +1024,7 @@ export function createTriageHost({ dataRoot, route = null, readSnapshot = async
|
||||
return { id, at };
|
||||
},
|
||||
async stop({ graceMs = 5000 } = {}) {
|
||||
clearKeyTimer();
|
||||
const c = child;
|
||||
if (c) {
|
||||
try { process.kill(-c.pid, "SIGTERM"); } catch {}
|
||||
|
||||
@@ -19,8 +19,12 @@ import { fileURLToPath } from 'node:url';
|
||||
import { checkRoutes } from '../../organization/src/launch-settings.mjs';
|
||||
import { isDailyModelQuota } from '../../plugins/src/model-quota.mjs';
|
||||
import { readRoute, writeRoute } from './triage-route-store.mjs';
|
||||
import { NO_RIDING_MESSAGE, accessStatus } from './triage-access.mjs';
|
||||
|
||||
export const TRIAGE_ROUTE_PATH = '/api/triage/route';
|
||||
// proxy-scope slice 4 (#1873): Repair access (ensure) and Turn Triage off (stop, then revoke).
|
||||
export const TRIAGE_ACCESS_PATH = '/api/triage/access';
|
||||
export const ACCESS_OPS = Object.freeze(['repair', 'off']);
|
||||
export const BODY_MAX = 16384;
|
||||
export const POST_KEYS = Object.freeze(['model', 'harness', 'cap', 'timeZone', 'revision']);
|
||||
export const NOT_SET_MESSAGE = 'No Triage route is set.';
|
||||
@@ -63,6 +67,7 @@ const STATUS = Object.freeze({
|
||||
'triage-route-unreadable': 503,
|
||||
'triage-route-unrecorded': 500,
|
||||
'outcome-unknown': 503,
|
||||
'triage-no-riding-bus': 503,
|
||||
});
|
||||
// Fixed words for each code. The store's own messages name lock files, so none is forwarded.
|
||||
const MESSAGES = Object.freeze({
|
||||
@@ -85,7 +90,16 @@ const MESSAGES = Object.freeze({
|
||||
'triage-route-unreadable': 'The stored route could not be read.',
|
||||
'triage-route-unrecorded': 'The route was saved, but its receipt line was not written.',
|
||||
'outcome-unknown': 'The route service did not answer.',
|
||||
'triage-no-riding-bus': `${NO_RIDING_MESSAGE}. Start an organization that uses them, then choose Repair access.`,
|
||||
});
|
||||
// proxy-scope slice 4: a bus refusal of proxy.triage.key, in fixed words. The code is a bus code
|
||||
// (lowercase and dashes); nothing from the bus's text is shown.
|
||||
const keyFailed = code => code === 'triage-no-riding-bus' ? MESSAGES['triage-no-riding-bus']
|
||||
: `The Triage key was not changed. The organization's bus refused with ${/^[a-z][a-z0-9-]{0,63}$/.test(code ?? '') ? code : 'an unknown code'}.`;
|
||||
// What a key step answers in a response: ids, action and day, or the code and its fixed words.
|
||||
const keyView = r => r?.ok === true
|
||||
? { action: r.action, ...(r.keyId ? { keyId: r.keyId } : {}), ...(r.rotateBy ? { rotateBy: r.rotateBy } : {}) }
|
||||
: { error: r?.code ?? 'outcome-unknown', message: keyFailed(r?.code) };
|
||||
|
||||
const json = (res, status, value) => {
|
||||
res.writeHead(status, { 'content-type': 'application/json', 'cache-control': 'no-store' });
|
||||
@@ -191,7 +205,25 @@ async function readBody(req) {
|
||||
|
||||
// Registers both routes. `catalogues` returns each active organization's `routes` catalogue (or a
|
||||
// status when its bus did not answer); `harnesses` lists the installed harness modules.
|
||||
export function triageRouteRoutes({ dataRoot, catalogues, harnesses = installedHarnesses, log = () => {} }) {
|
||||
// proxy-scope slice 4: `access`, when given, is the Triage key's side:
|
||||
// state() the key file's state (triage-access.mjs keyState), never the key
|
||||
// riding() async -> true when one organization rides the owner's models
|
||||
// key(op) async -> triageKeyCall's answer for op ensure or revoke, through one riding bus
|
||||
// stop() async -> stops a running Triage session (the host's stop)
|
||||
// A route save with no key held runs ensure once; POST /api/triage/access runs Repair access
|
||||
// (ensure) or Turn Triage off (stop, then revoke). With no access, neither happens.
|
||||
export function triageRouteRoutes({ dataRoot, catalogues, harnesses = installedHarnesses, log = () => {}, access = null }) {
|
||||
const accessNow = async () => {
|
||||
if (!access) return undefined;
|
||||
let state;
|
||||
try { state = access.state(); } catch { state = { key: 'unsafe' }; }
|
||||
let riding = false;
|
||||
try { riding = (await access.riding()) === true; } catch { riding = false; }
|
||||
return accessStatus(state, { riding });
|
||||
};
|
||||
const keyStep = async op => {
|
||||
try { return await access.key(op); } catch { return { ok: false, code: 'outcome-unknown' }; }
|
||||
};
|
||||
async function catalogue() {
|
||||
let list;
|
||||
try { list = await catalogues(); } catch { list = []; }
|
||||
@@ -222,9 +254,11 @@ export function triageRouteRoutes({ dataRoot, catalogues, harnesses = installedH
|
||||
let doc;
|
||||
try { doc = readRoute(dataRoot); } catch (error) { return refuse(error?.code === 'triage-route-unreadable' ? 'triage-route-unreadable' : 'outcome-unknown'); }
|
||||
const route = view(doc);
|
||||
const status = await accessNow();
|
||||
return done(200, {
|
||||
route, ...(route ? {} : { routeMessage: NOT_SET_MESSAGE }), revision: route ? route.revision : 0,
|
||||
choices: choices(await catalogue()), send: context.send === true, note: NOT_STARTED_NOTE,
|
||||
...(status ? { access: status } : {}),
|
||||
});
|
||||
}
|
||||
if (context.send !== true) return refuse('read-only');
|
||||
@@ -247,7 +281,17 @@ export function triageRouteRoutes({ dataRoot, catalogues, harnesses = installedH
|
||||
if (by === null) return refuse('human-required');
|
||||
try {
|
||||
const doc = writeRoute(dataRoot, { model, harness, limit: cap, timeZone: zone, by, revision });
|
||||
return done(200, { route: view(doc), revision: doc.revision, note: NOT_STARTED_NOTE });
|
||||
// proxy-scope slice 4: the owner's first save (the one that creates the route, at revision 0),
|
||||
// with no key held, asks a riding bus for one. A later save never does: after Turn Triage off
|
||||
// only Repair access brings a key back. A key that is not issued leaves the route saved; the
|
||||
// answer says why.
|
||||
let key;
|
||||
if (access && revision === 0) {
|
||||
let state;
|
||||
try { state = access.state(); } catch { state = { key: 'unsafe' }; }
|
||||
if (state.key === 'missing') key = keyView(await keyStep('ensure'));
|
||||
}
|
||||
return done(200, { route: view(doc), revision: doc.revision, note: NOT_STARTED_NOTE, ...(key ? { key } : {}) });
|
||||
} catch (error) {
|
||||
const code = typeof error?.code === 'string' && Object.hasOwn(MESSAGES, error.code) && error.code.startsWith('triage-route-') ? error.code : 'outcome-unknown';
|
||||
if (code !== 'triage-route-stale') return refuse(code);
|
||||
@@ -256,7 +300,40 @@ export function triageRouteRoutes({ dataRoot, catalogues, harnesses = installedH
|
||||
return refuse(code, { revision: stored ? stored.revision : 0 });
|
||||
}
|
||||
}
|
||||
// POST /api/triage/access { op: 'repair' | 'off' }: the owner's Repair access and Turn Triage off.
|
||||
async function handleAccess(req, res, context) {
|
||||
const done = (status, value) => {
|
||||
log(`${new Date().toISOString()} ${req.method} ${TRIAGE_ACCESS_PATH} ${status}`);
|
||||
return json(res, status, value);
|
||||
};
|
||||
const refuse = code => done(STATUS[code] ?? 503, { error: code, message: MESSAGES[code] ?? MESSAGES['outcome-unknown'] });
|
||||
if (req.method !== 'POST') return refuse('method-not-allowed');
|
||||
if (context.owner !== true || !context.session) return refuse('human-required');
|
||||
if (one(req, 'origin') !== context.origin) return refuse('cross-origin');
|
||||
if (one(req, 'sec-fetch-site') !== 'same-origin') return refuse('cross-site');
|
||||
if (context.send !== true) return refuse('read-only');
|
||||
if (!access) return refuse('outcome-unknown');
|
||||
let op;
|
||||
try {
|
||||
if ((req.headers['content-type'] ?? '').split(';')[0].trim().toLowerCase() !== 'application/json') throw Error();
|
||||
const chunks = []; let size = 0;
|
||||
for await (const chunk of req) { size += chunk.length; if (size > BODY_MAX) throw Error(); chunks.push(chunk); }
|
||||
const body = JSON.parse(Buffer.concat(chunks));
|
||||
if (!plain(body) || Object.keys(body).length !== 1 || !ACCESS_OPS.includes(body.op)) throw Error();
|
||||
op = body.op;
|
||||
} catch { return done(400, { error: 'invalid-request', message: 'The request takes op, repair or off.' }); }
|
||||
if (op === 'off') {
|
||||
// The session stops first, so nothing runs on a key while it is removed.
|
||||
try { await access.stop(); } catch { log('triage access: the session stop failed'); }
|
||||
}
|
||||
const result = await keyStep(op === 'repair' ? 'ensure' : 'revoke');
|
||||
const key = keyView(result);
|
||||
const status = await accessNow();
|
||||
if (result?.ok === true) return done(200, { op, key, access: status });
|
||||
return done(STATUS[result?.code] ?? 502, { op, ...key, key, access: status });
|
||||
}
|
||||
return registered => {
|
||||
registered.add('POST', TRIAGE_ACCESS_PATH, handleAccess);
|
||||
registered.add('GET', TRIAGE_ROUTE_PATH, handle);
|
||||
registered.add('POST', TRIAGE_ROUTE_PATH, handle);
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// #1873 triage-wiring: the Triage host's model route, resolved again at each admit (design report
|
||||
// reports/triage-route-design.md slice 5). Four sources, each read fresh:
|
||||
// the route store harness name, model key and cap (triage-route-store.mjs)
|
||||
// the key file <configDir>/secrets/triage/model (packages/cli/src/triage-key-file.mjs, the
|
||||
// the key file <configDir>/secrets/triage/model (packages/bus/src/triage-key-file.mjs, the
|
||||
// key verbs' own reader and checks; never the module that holds the proxy client)
|
||||
// the model catalogue model and provider for the key, read from each active organization's bus
|
||||
// `routes` verb and merged (mergeCatalogues), then held to checkRoutes, the
|
||||
@@ -32,7 +32,7 @@ import { lstatSync, readdirSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath, pathToFileURL } from "node:url";
|
||||
import { checkRoutes } from "../../organization/src/launch-settings.mjs";
|
||||
import { SECRET, keyPrefix, readKey } from "../../cli/src/triage-key-file.mjs";
|
||||
import { SECRET, keyPrefix, readKey } from "../../bus/src/triage-key-file.mjs";
|
||||
import { readRoute } from "./triage-route-store.mjs";
|
||||
import { ROUTE_KEY_NAMES, ROUTE_REFUSALS, refusedHarnessName } from "./triage-host.mjs";
|
||||
import { checkedTransport } from "../../harness/src/bundle.mjs";
|
||||
|
||||
@@ -0,0 +1,316 @@
|
||||
// proxy-scope slice 4 (#1873): the Console's side of the Triage key. The Console asks one riding
|
||||
// organization's bus for proxy.triage.key (it never holds the management key); a bus that is down or
|
||||
// not riding is passed over; with none left the status is triage-no-riding-bus with its named
|
||||
// message. Settings > Triage shows "rotation overdue" when the day has passed and no riding bus can
|
||||
// act. The owner's first save with no key held runs ensure; Repair access runs ensure; Turn Triage
|
||||
// off stops the session, then runs revoke. No answer, response or log line holds a key.
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { request } from 'node:http';
|
||||
import { chmodSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { startServer } from '../src/serve.mjs';
|
||||
import { createDevOwner } from '../src/dev-owner.mjs';
|
||||
import { keyIdOf, keyPrefix } from '../../bus/src/triage-key-file.mjs';
|
||||
import { NO_RIDING_MESSAGE, OVERDUE_ALLOWANCE_MS, accessStatus, keyState, ridingOrgs, triageKeyCall } from '../src/triage-access.mjs';
|
||||
import { ACCESS_OPS, TRIAGE_ACCESS_PATH, TRIAGE_ROUTE_PATH, triageRouteRoutes } from '../src/triage-route-http.mjs';
|
||||
|
||||
const scratch = prefix => mkdtempSync(join(process.env.TMPDIR, prefix));
|
||||
const T = '2026-11-18T12:00:00.000Z';
|
||||
const SECRET = 'S'.repeat(20) + 'e_-' + 'c'.repeat(20);
|
||||
const KEY = `mst-0123456789ab-triage-${SECRET}`;
|
||||
const busError = code => Object.assign(new Error(`bus said ${code} ${KEY}`), { code });
|
||||
|
||||
// A rider as webui cli.mjs rideRiders lists them; `answer` is what its bus does with the call.
|
||||
const rider = (id, answer, over = {}) => {
|
||||
const r = { id, fixture: false, cliproxy: true, calls: [], call: async (verb, args) => { r.calls.push([verb, args]); return answer(args); }, ...over };
|
||||
return r;
|
||||
};
|
||||
const ISSUED = () => ({ op: 'ensure', action: 'issued', keyId: '0123456789ab', rotateBy: '2026-12-18', addedKeyIds: ['0123456789ab'], removedKeyIds: [], key: KEY, retiring: [{ key: KEY }] });
|
||||
|
||||
test('triageKeyCall asks the first riding bus as the owner, passes over a bus that is down or not riding, and skips fixture and non-cliproxy organizations', async () => {
|
||||
const fixtureOrg = rider('fix', ISSUED, { fixture: true });
|
||||
const ownProxy = rider('own', ISSUED, { cliproxy: false });
|
||||
const down = rider('down', () => { throw busError('no-bus-host'); });
|
||||
const notRiding = rider('other', () => { throw busError('triage-no-riding-bus'); });
|
||||
const good = rider('alpha', ISSUED);
|
||||
const after = rider('beta', ISSUED);
|
||||
const r = await triageKeyCall({ riders: async () => [fixtureOrg, ownProxy, down, notRiding, good, after], op: 'ensure' });
|
||||
assert.deepEqual(r, { ok: true, op: 'ensure', action: 'issued', addedKeyIds: ['0123456789ab'], removedKeyIds: [], keyId: '0123456789ab', rotateBy: '2026-12-18', org: 'alpha' });
|
||||
assert.deepEqual(good.calls, [['proxy.triage.key', { op: 'ensure' }]]);
|
||||
for (const x of [fixtureOrg, ownProxy, after]) assert.deepEqual(x.calls, [], x.id);
|
||||
for (const x of [down, notRiding]) assert.equal(x.calls.length, 1, x.id);
|
||||
assert.equal(JSON.stringify(r).includes(SECRET), false, 'no key in the answer');
|
||||
});
|
||||
|
||||
test('triageKeyCall: a bus whose outcome is unknown (a timeout, an unreadable or oversized answer) may have done the work, so the walk stops there and asks no other bus', async () => {
|
||||
for (const c of ['outcome-unknown', 'invalid-response', 'response-too-large']) {
|
||||
const slow = rider('slow', () => { throw busError(c); });
|
||||
const next = rider('beta', ISSUED);
|
||||
for (const op of ['ensure', 'revoke'])
|
||||
assert.deepEqual(await triageKeyCall({ riders: async () => [slow, next], op }), { ok: false, code: 'outcome-unknown', org: 'slow' }, `${c} ${op}`);
|
||||
assert.deepEqual(next.calls, [], c);
|
||||
}
|
||||
// ridingOrgs is the list the walk asks, and the status reads the same list.
|
||||
const list = [rider('fix', ISSUED, { fixture: true }), rider('own', ISSUED, { cliproxy: false }), rider('alpha', ISSUED), { id: 'nocall', cliproxy: true }, null];
|
||||
assert.deepEqual(ridingOrgs(list).map(r => r.id), ['alpha']);
|
||||
assert.deepEqual(ridingOrgs('x'), []);
|
||||
});
|
||||
|
||||
test('triageKeyCall: with no riding bus left the answer is triage-no-riding-bus; any other refusal is the answer and stops the walk', async () => {
|
||||
for (const list of [[], [rider('own', ISSUED, { cliproxy: false })], [rider('fix', ISSUED, { fixture: true })], [rider('a', () => { throw busError('triage-no-riding-bus'); }), rider('b', () => { throw busError('bus-unavailable'); })]])
|
||||
assert.deepEqual(await triageKeyCall({ riders: async () => list, op: 'revoke' }), { ok: false, code: 'triage-no-riding-bus' });
|
||||
assert.deepEqual(await triageKeyCall({ riders: async () => { throw new Error('x'); }, op: 'ensure' }), { ok: false, code: 'triage-no-riding-bus' });
|
||||
const refused = rider('alpha', () => { throw busError('human-required'); });
|
||||
const next = rider('beta', ISSUED);
|
||||
assert.deepEqual(await triageKeyCall({ riders: async () => [refused, next], op: 'ensure' }), { ok: false, code: 'human-required', org: 'alpha' });
|
||||
assert.deepEqual(next.calls, []);
|
||||
// A code that is not a bus code is outcome-unknown, which stops the walk.
|
||||
const odd = rider('odd', () => { throw busError(`rm -rf ${KEY}`); });
|
||||
assert.deepEqual(await triageKeyCall({ riders: async () => [odd, next], op: 'ensure' }), { ok: false, code: 'outcome-unknown', org: 'odd' });
|
||||
for (const op of ['rotate', undefined, 'ENSURE']) assert.deepEqual(await triageKeyCall({ riders: async () => [next], op }), { ok: false, code: 'invalid-request' });
|
||||
assert.deepEqual(next.calls, []);
|
||||
});
|
||||
|
||||
test('triageKeyCall keeps ids and a day only: a key, a key-shaped id or an unknown action from a bus is dropped', async () => {
|
||||
const leaky = rider('alpha', () => ({ action: 'replaced', keyId: KEY, rotateBy: `${KEY}`, addedKeyIds: [KEY, '0123456789ab'], removedKeyIds: ['ba9876543210', { key: KEY }], key: KEY }));
|
||||
const r = await triageKeyCall({ riders: async () => [leaky], op: 'ensure' });
|
||||
assert.deepEqual(r, { ok: true, op: 'ensure', action: 'replaced', addedKeyIds: ['0123456789ab'], removedKeyIds: ['ba9876543210'], org: 'alpha' });
|
||||
const strange = rider('alpha', () => ({ action: KEY, keyId: '0123456789ab', addedKeyIds: 'x' }));
|
||||
assert.deepEqual(await triageKeyCall({ riders: async () => [strange], op: 'revoke' }), { ok: true, op: 'revoke', action: 'unknown', addedKeyIds: [], removedKeyIds: [], org: 'alpha' });
|
||||
});
|
||||
|
||||
// A key file as the bus writes it (triage-key.mjs), in a temp config directory.
|
||||
function keyFile(configDir, { rotateBy = '2026-12-18', mode = 0o600 } = {}) {
|
||||
const dir = join(configDir, 'secrets', 'triage');
|
||||
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
||||
chmodSync(join(configDir, 'secrets'), 0o700);
|
||||
chmodSync(dir, 0o700);
|
||||
const key = `${keyPrefix(configDir)}${SECRET}`;
|
||||
writeFileSync(join(dir, 'model'), JSON.stringify({ version: 1, key, keyId: keyIdOf(key), issuedAt: T, rotateBy, retiring: [] }), { mode });
|
||||
chmodSync(join(dir, 'model'), mode);
|
||||
return key;
|
||||
}
|
||||
|
||||
test('keyState reads the file through the reader alone: missing, issued with its id and day, due and overdue, or unsafe; never the key', t => {
|
||||
const root = scratch('ta-ks-');
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config');
|
||||
mkdirSync(configDir, { mode: 0o700 });
|
||||
assert.deepEqual(keyState(configDir), { key: 'missing' });
|
||||
const key = keyFile(configDir);
|
||||
const at = s => () => Date.parse(s);
|
||||
const before = keyState(configDir, { now: at('2026-12-17T23:59:59Z') });
|
||||
assert.deepEqual(before, { key: 'issued', keyId: keyIdOf(key), rotateBy: '2026-12-18', retiring: 0, due: false, overdue: false });
|
||||
assert.equal(JSON.stringify(before).includes(SECRET), false);
|
||||
assert.deepEqual(keyState(configDir, { now: at('2026-12-18T00:00:00Z') }), { ...before, due: true });
|
||||
assert.equal(OVERDUE_ALLOWANCE_MS, 600000);
|
||||
assert.deepEqual(keyState(configDir, { now: at('2026-12-18T00:10:00Z') }), { ...before, due: true });
|
||||
assert.deepEqual(keyState(configDir, { now: at('2026-12-18T00:10:01Z') }), { ...before, due: true, overdue: true });
|
||||
chmodSync(join(configDir, 'secrets', 'triage', 'model'), 0o644);
|
||||
assert.deepEqual(keyState(configDir), { key: 'unsafe' });
|
||||
rmSync(join(configDir, 'secrets', 'triage', 'model'));
|
||||
symlinkSync('/dev/null', join(configDir, 'secrets', 'triage', 'model'));
|
||||
assert.deepEqual(keyState(configDir), { key: 'unsafe' });
|
||||
});
|
||||
|
||||
test('accessStatus: no riding organization names triage-no-riding-bus and its message; rotation overdue shows when the day has passed and no riding bus can act', () => {
|
||||
const issued = { key: 'issued', keyId: '0123456789ab', rotateBy: '2026-12-18', retiring: 0, due: false, overdue: false };
|
||||
assert.deepEqual(accessStatus(issued, { riding: true }), { ...issued, riding: true });
|
||||
assert.deepEqual(accessStatus(issued, { riding: false }), { ...issued, riding: false, code: 'triage-no-riding-bus', message: NO_RIDING_MESSAGE });
|
||||
assert.equal(NO_RIDING_MESSAGE, "Triage needs one organization that uses the owner's models");
|
||||
assert.equal(accessStatus({ ...issued, due: true }, { riding: false }).rotation, 'rotation overdue');
|
||||
assert.equal(accessStatus({ ...issued, due: true }, { riding: true }).rotation, undefined, 'a riding bus has its sweep to act');
|
||||
assert.equal(accessStatus({ ...issued, due: true, overdue: true }, { riding: true }).rotation, 'rotation overdue', 'still not done well after the day began');
|
||||
assert.deepEqual(accessStatus({ key: 'missing' }, { riding: false }), { key: 'missing', riding: false, code: 'triage-no-riding-bus', message: NO_RIDING_MESSAGE });
|
||||
assert.equal(accessStatus({ key: 'missing' }, { riding: 'yes' }).riding, false);
|
||||
});
|
||||
|
||||
// --- Over HTTP ---------------------------------------------------------------------------------
|
||||
const MODEL = { key: 'sonnet', module: 'anthropic', upstream: 'cliproxy', provider: 'anthropic', model: 'claude-sonnet', label: 'SONNET', available: true, reason: null, harnesses: ['claude-code', 'pi'] };
|
||||
const READY = () => ({ status: 'ready', observedAt: T, notices: [], models: [MODEL] });
|
||||
const BODY = { model: 'sonnet', harness: 'claude-code', cap: 200000, timeZone: 'Europe/Berlin', revision: 0 };
|
||||
const send = (port, path, { method = 'GET', headers = {}, body } = {}) => new Promise((resolve, reject) => {
|
||||
const req = request({ host: '127.0.0.1', port, path, method, headers }, res => {
|
||||
let text = ''; res.on('data', c => { text += c; }); res.on('end', () => resolve({ status: res.statusCode, text, json: (() => { try { return JSON.parse(text); } catch { return null; } })() }));
|
||||
});
|
||||
req.on('error', reject); req.end(body === undefined ? undefined : typeof body === 'string' ? body : JSON.stringify(body));
|
||||
});
|
||||
|
||||
// A dev-owner Console with the access side wired as cli.mjs wires it: the real keyState over a temp
|
||||
// config directory and the real triageKeyCall over stand-in riders. A rider's ensure writes the key
|
||||
// file, as the bus does; its revoke removes it.
|
||||
async function console_(t, { riders: make = null, serve = {} } = {}) {
|
||||
const root = scratch('ta-http-');
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
const configDir = join(root, 'config'), dataRoot = join(root, 'data');
|
||||
mkdirSync(configDir, { mode: 0o700 });
|
||||
mkdirSync(dataRoot, { mode: 0o700 });
|
||||
const order = [];
|
||||
const busAnswer = ({ op }) => {
|
||||
order.push(`bus ${op}`);
|
||||
if (op === 'ensure') { keyFile(configDir); return ISSUED(); }
|
||||
rmSync(join(configDir, 'secrets', 'triage', 'model'), { force: true });
|
||||
return { op: 'revoke', action: 'revoked', addedKeyIds: [], removedKeyIds: ['0123456789ab'], key: KEY };
|
||||
};
|
||||
const list = make ? make(busAnswer) : [rider('alpha', busAnswer)];
|
||||
const logs = [], routeLogs = [];
|
||||
const access = {
|
||||
state: () => keyState(configDir, { now: () => Date.parse(T) }),
|
||||
// As cli.mjs: the same list the walk asks (the proxy is set up in this harness).
|
||||
riding: async () => ridingOrgs(list).length > 0,
|
||||
key: op => triageKeyCall({ riders: async () => list, op }),
|
||||
stop: async () => { order.push('stop'); },
|
||||
};
|
||||
const s = await startServer({ port: 0, board: 'http://127.0.0.1:9', sso: createDevOwner(), send: async () => ({}), log: l => logs.push(l),
|
||||
routes: [triageRouteRoutes({ dataRoot, catalogues: async () => [READY()], harnesses: () => ['claude-code', 'pi'], log: l => routeLogs.push(l), access })], ...serve });
|
||||
t.after(() => new Promise(r => s.close(r)));
|
||||
const port = s.address().port;
|
||||
const read = { host: `127.0.0.1:${port}`, 'sec-fetch-site': 'same-origin' };
|
||||
const write = { ...read, origin: `http://127.0.0.1:${port}`, 'content-type': 'application/json' };
|
||||
return {
|
||||
port, configDir, list, order, logs, routeLogs, write,
|
||||
get: () => send(port, TRIAGE_ROUTE_PATH, { headers: read }),
|
||||
save: (body = BODY) => send(port, TRIAGE_ROUTE_PATH, { method: 'POST', headers: write, body }),
|
||||
op: (op, headers = write) => send(port, TRIAGE_ACCESS_PATH, { method: 'POST', headers, body: { op } }),
|
||||
};
|
||||
}
|
||||
|
||||
test('the first save with no key held asks one riding bus for ensure; a later save, with the key held, does not', async t => {
|
||||
const f = await console_(t);
|
||||
const before = await f.get();
|
||||
assert.deepEqual(before.json.access, { key: 'missing', riding: true });
|
||||
const first = await f.save();
|
||||
assert.equal(first.status, 200, first.text);
|
||||
assert.deepEqual(first.json.key, { action: 'issued', keyId: '0123456789ab', rotateBy: '2026-12-18' });
|
||||
assert.deepEqual(f.list[0].calls, [['proxy.triage.key', { op: 'ensure' }]]);
|
||||
const second = await f.save({ ...BODY, revision: 1, cap: 300000 });
|
||||
assert.equal(second.status, 200, second.text);
|
||||
assert.equal(Object.hasOwn(second.json, 'key'), false);
|
||||
assert.equal(f.list[0].calls.length, 1, 'ensure runs once');
|
||||
assert.equal((await f.get()).json.access.key, 'issued');
|
||||
});
|
||||
|
||||
test('with no riding organization, a save keeps the route and says why; the status names triage-no-riding-bus and its message; Repair answers 503 with it', async t => {
|
||||
const f = await console_(t, { riders: answer => [rider('own', answer, { cliproxy: false }), rider('fix', answer, { fixture: true })] });
|
||||
const r = await f.get();
|
||||
assert.deepEqual(r.json.access, { key: 'missing', riding: false, code: 'triage-no-riding-bus', message: NO_RIDING_MESSAGE });
|
||||
const saved = await f.save();
|
||||
assert.equal(saved.status, 200);
|
||||
assert.equal(saved.json.route.model, 'sonnet', 'the route is saved');
|
||||
assert.equal(saved.json.key.error, 'triage-no-riding-bus');
|
||||
assert.match(saved.json.key.message, /^Triage needs one organization that uses the owner's models\./);
|
||||
const repair = await f.op('repair');
|
||||
assert.equal(repair.status, 503);
|
||||
assert.equal(repair.json.error, 'triage-no-riding-bus');
|
||||
assert.equal(repair.json.access.code, 'triage-no-riding-bus');
|
||||
for (const x of f.list) assert.deepEqual(x.calls, [], x.id);
|
||||
});
|
||||
|
||||
test('rotation overdue shows when the key day has passed and no organization rides the owner models', async t => {
|
||||
const f = await console_(t, { riders: answer => [rider('own', answer, { cliproxy: false })] });
|
||||
keyFile(f.configDir, { rotateBy: '2026-11-17' });
|
||||
const r = await f.get();
|
||||
assert.equal(r.json.access.rotation, 'rotation overdue');
|
||||
assert.equal(r.json.access.code, 'triage-no-riding-bus');
|
||||
assert.equal(r.json.access.key, 'issued');
|
||||
});
|
||||
|
||||
test('Repair access runs ensure; Turn Triage off stops the session first, then runs revoke', async t => {
|
||||
const f = await console_(t);
|
||||
assert.deepEqual(ACCESS_OPS, ['repair', 'off']);
|
||||
const repair = await f.op('repair');
|
||||
assert.equal(repair.status, 200, repair.text);
|
||||
assert.deepEqual(repair.json, { op: 'repair', key: { action: 'issued', keyId: '0123456789ab', rotateBy: '2026-12-18' }, access: { key: 'issued', keyId: repair.json.access.keyId, rotateBy: '2026-12-18', retiring: 0, due: false, overdue: false, riding: true } });
|
||||
const off = await f.op('off');
|
||||
assert.equal(off.status, 200, off.text);
|
||||
assert.deepEqual(off.json, { op: 'off', key: { action: 'revoked' }, access: { key: 'missing', riding: true } });
|
||||
assert.deepEqual(f.order, ['bus ensure', 'stop', 'bus revoke']);
|
||||
assert.deepEqual(f.list[0].calls.map(c => c[1].op), ['ensure', 'revoke']);
|
||||
});
|
||||
|
||||
test('after Turn Triage off, a later save does not issue the key again; only Repair access brings it back', async t => {
|
||||
const f = await console_(t);
|
||||
assert.equal((await f.save()).json.key.action, 'issued');
|
||||
assert.equal((await f.op('off')).status, 200);
|
||||
const again = await f.save({ ...BODY, revision: 1, cap: 300000 });
|
||||
assert.equal(again.status, 200, again.text);
|
||||
assert.equal(Object.hasOwn(again.json, 'key'), false);
|
||||
assert.equal((await f.get()).json.access.key, 'missing');
|
||||
assert.deepEqual(f.order, ['bus ensure', 'stop', 'bus revoke']);
|
||||
assert.equal((await f.op('repair')).json.key.action, 'issued');
|
||||
assert.deepEqual(f.order, ['bus ensure', 'stop', 'bus revoke', 'bus ensure']);
|
||||
});
|
||||
|
||||
test('a bus refusal on Off leaves the key and says why with a fixed line; the session was still stopped', async t => {
|
||||
const f = await console_(t, { riders: () => [rider('alpha', () => { throw busError('storage-unavailable'); })] });
|
||||
const off = await f.op('off');
|
||||
assert.equal(off.status, 502);
|
||||
assert.equal(off.json.error, 'storage-unavailable');
|
||||
assert.equal(off.json.message, 'The Triage key was not changed. The organization\'s bus refused with storage-unavailable.');
|
||||
assert.deepEqual(f.order, ['stop']);
|
||||
assert.equal(off.text.includes(SECRET), false);
|
||||
});
|
||||
|
||||
test('the access path holds the owner, same-origin and read-only gates, takes op only, and the tester listener cannot reach it', async t => {
|
||||
const f = await console_(t);
|
||||
const { origin, ...noOrigin } = f.write;
|
||||
assert.equal((await f.op('repair', noOrigin)).status, 403);
|
||||
assert.equal((await f.op('repair', { ...f.write, origin: 'http://evil.example' })).status, 403);
|
||||
assert.equal((await f.op('repair', { ...f.write, 'sec-fetch-site': 'cross-site' })).status, 403);
|
||||
const { 'sec-fetch-site': _, ...noSite } = f.write;
|
||||
assert.equal((await f.op('repair', noSite)).status, 403);
|
||||
for (const body of [{}, { op: 'ensure' }, { op: 'revoke' }, { op: 'repair', key: KEY }, { op: ['repair'] }, 'repair'])
|
||||
assert.equal((await send(f.port, TRIAGE_ACCESS_PATH, { method: 'POST', headers: f.write, body })).status, 400, JSON.stringify(body));
|
||||
assert.equal((await send(f.port, TRIAGE_ACCESS_PATH, { method: 'POST', headers: { ...f.write, 'content-type': 'text/plain' }, body: { op: 'repair' } })).status, 400);
|
||||
assert.notEqual((await send(f.port, TRIAGE_ACCESS_PATH, { headers: f.write })).status, 200, 'a GET is not an op');
|
||||
assert.deepEqual(f.list[0].calls, [], 'no refused request reached a bus');
|
||||
assert.deepEqual(f.order, []);
|
||||
|
||||
// The handler's own owner and same-origin lines, behind the serve gate: a caller who is not the
|
||||
// instance owner, or a doubled Origin or Sec-Fetch-Site, is refused before any bus is asked.
|
||||
let handler, unitAsked = 0;
|
||||
const unitAccess = { state: () => ({ key: 'missing' }), riding: async () => true, key: async () => { unitAsked++; return { ok: true }; }, stop: async () => { unitAsked++; } };
|
||||
triageRouteRoutes({ dataRoot: f.configDir, catalogues: async () => [READY()], harnesses: () => ['claude-code', 'pi'], access: unitAccess })({ add: (method, path, fn) => { if (path === TRIAGE_ACCESS_PATH) handler = fn; } });
|
||||
const o = 'https://console.example';
|
||||
const call = (headersDistinct, extra = {}) => new Promise(resolve => {
|
||||
const res = { writeHead(status) { this.status = status; }, end(text) { resolve({ status: this.status, json: JSON.parse(text) }); } };
|
||||
const req = { method: 'POST', headersDistinct, headers: { 'content-type': 'application/json' }, async *[Symbol.asyncIterator]() { yield Buffer.from('{"op":"off"}'); } };
|
||||
handler(req, res, { owner: true, session: { provider: 'local-dev', sub: 'local-dev-owner' }, origin: o, send: true, ...extra });
|
||||
});
|
||||
const same = { origin: [o], 'sec-fetch-site': ['same-origin'] };
|
||||
assert.equal((await call(same, { owner: false })).json.error, 'human-required');
|
||||
assert.equal((await call(same, { session: null })).json.error, 'human-required');
|
||||
assert.equal((await call({ ...same, origin: [o, o] })).json.error, 'cross-origin');
|
||||
assert.equal((await call({ ...same, 'sec-fetch-site': ['same-origin', 'same-origin'] })).json.error, 'cross-site');
|
||||
assert.equal((await call(same, { send: false })).json.error, 'read-only');
|
||||
assert.equal(unitAsked, 0);
|
||||
assert.equal((await call(same)).status, 200);
|
||||
assert.equal(unitAsked, 2, 'the owner\'s own call stops and revokes');
|
||||
|
||||
const ro = await console_(t, { serve: { send: null } });
|
||||
const r = await ro.op('off');
|
||||
assert.equal(r.status, 403);
|
||||
assert.equal(r.json.error, 'read-only');
|
||||
assert.deepEqual(ro.order, []);
|
||||
|
||||
const { startTesterSocket } = await import('../src/tester-socket.mjs');
|
||||
const { testerRequest } = await import('../../cli/src/tester-http.mjs');
|
||||
const root = scratch('ta-tester-');
|
||||
t.after(() => rmSync(root, { recursive: true, force: true }));
|
||||
let asked = 0;
|
||||
const access = { state: () => ({ key: 'missing' }), riding: async () => true, key: async () => { asked++; return { ok: true }; }, stop: async () => { asked++; } };
|
||||
const s = await startTesterSocket({ dataRoot: root, log: () => {}, routes: [triageRouteRoutes({ dataRoot: root, catalogues: async () => [READY()], harnesses: () => ['claude-code', 'pi'], access })] });
|
||||
t.after(() => new Promise(r2 => s.close(r2)));
|
||||
for (const op of ['repair', 'off']) assert.equal((await testerRequest({ dataRoot: root, path: TRIAGE_ACCESS_PATH, method: 'POST', body: { op } })).status, 403, op);
|
||||
assert.equal(asked, 0);
|
||||
});
|
||||
|
||||
test('no response or log line holds a key, even when a bus answers with one', async t => {
|
||||
const f = await console_(t);
|
||||
const texts = [];
|
||||
for (const r of [await f.get(), await f.save(), await f.op('repair'), await f.op('off'), await f.get()]) texts.push(r.text);
|
||||
const all = texts.join('\n') + f.logs.join('\n') + f.routeLogs.join('\n');
|
||||
assert.equal(all.includes(SECRET), false);
|
||||
assert.equal(all.includes(f.configDir), false, 'no config path');
|
||||
});
|
||||
@@ -12,7 +12,7 @@ import { REPO, RUNNER_FILES, checkHostedRoute, sandboxProblem } from "../../seat
|
||||
import { isProviderVariable } from "../../seat/src/session.mjs";
|
||||
import {
|
||||
NOT_STARTED, NOT_TAKEN, RELAY_REFUSED, ROUTE_ENV_ALLOW, ROUTE_REFUSED, SPAWN_ENV_ALLOW, SPAWN_PATH_ALLOW, UNANSWERED, createRelay, createTriageHost, hostPrompt,
|
||||
USAGE_NOT_READ, USAGE_UNKNOWN_ENDED, UNKNOWN_TURNS_MAX, overCapNotTaken, refusedHarnessName, triageEnv,
|
||||
USAGE_NOT_READ, USAGE_UNKNOWN_ENDED, UNKNOWN_TURNS_MAX, KEY_ROTATED, KEY_ROTATED_CODE, overCapNotTaken, refusedHarnessName, triageEnv,
|
||||
} from "../src/triage-host.mjs";
|
||||
import { writeRoute } from "../src/triage-route-store.mjs";
|
||||
import { appendUsage, readUsage, usagePath } from "../src/triage-usage.mjs";
|
||||
@@ -1336,3 +1336,85 @@ test("cap fail closed: a Console restart between the unknown end and the next me
|
||||
assert.deepEqual(readdirSync(join(second.dataRoot, "triage", "agent", "session")), [], "a fresh session directory");
|
||||
assert.equal(existsSync(marker), false, "the marker is used once");
|
||||
});
|
||||
|
||||
// proxy-scope slice 4 (#1873): a Triage session still running on a key whose grace has ended stops
|
||||
// with triage-key-rotated: the host line says why, the state names the code, the process ends, and
|
||||
// no line, log or state holds the key. The key file is read through the keyRecord the Console hands
|
||||
// the host (packages/bus/src/triage-key-file.mjs readKey); here a stand-in record.
|
||||
const OLD_KEY = `mst-0123456789ab-triage-${"o".repeat(43)}`;
|
||||
const NEW_KEY = `mst-0123456789ab-triage-${"n".repeat(43)}`;
|
||||
const keyed = (root, key) => ({ asked: 0, key, admit: async function () { this.asked++; return answer(root, { env: { ANTHROPIC_BASE_URL: "http://127.0.0.1:9", ANTHROPIC_AUTH_TOKEN: this.key } }); } });
|
||||
const recordOf = (key, retiring = []) => ({ version: 1, key, keyId: "000000000000", issuedAt: "2026-11-18T00:00:00.000Z", rotateBy: "2026-12-18", retiring });
|
||||
|
||||
test("key rotation: a session on the old key runs through its grace and stops at its retireAt with triage-key-rotated", { timeout: 30000 }, async (t) => {
|
||||
const root = scratch();
|
||||
const logs = [];
|
||||
let clock = Date.parse("2026-11-18T12:00:00.000Z");
|
||||
let record = recordOf(OLD_KEY);
|
||||
const { h, dataRoot } = host(t, root, { route: keyed(root, OLD_KEY), bwrap: standIn(root), usageSource: spendSource, now: () => new Date(clock), log: (l) => logs.push(l), keyRecord: () => record, keyPollMs: 50 });
|
||||
assert.equal(KEY_ROTATED_CODE, "triage-key-rotated");
|
||||
await h.message("one");
|
||||
await runnerClient(root, dataRoot);
|
||||
const pid = h.pid;
|
||||
// The bus rotates: the old key is retiring until 12:10. Through the grace the session keeps running.
|
||||
record = recordOf(NEW_KEY, [{ key: OLD_KEY, keyId: "111111111111", issuedAt: "2026-10-18T00:00:00.000Z", retireAt: "2026-11-18T12:10:00.000Z" }]);
|
||||
clock = Date.parse("2026-11-18T12:09:59.000Z");
|
||||
await new Promise((r) => setTimeout(r, 300));
|
||||
assert.equal(h.running, true, "inside the grace the session runs");
|
||||
assert.equal((await h.state()).lastStop, null);
|
||||
// At the retireAt it stops, before the sweep (30 s later) removes the key from the proxy.
|
||||
clock = Date.parse("2026-11-18T12:10:00.000Z");
|
||||
await waitFor(() => !h.running, 10000);
|
||||
assert.throws(() => process.kill(pid, 0), "the process is gone");
|
||||
const s = await h.state();
|
||||
assert.deepEqual(s.lastStop, { code: "triage-key-rotated", at: "2026-11-18T12:10:00.000Z" });
|
||||
assert.equal(s.transcript.filter((e) => e.from === "host" && e.text === KEY_ROTATED).length, 1, "one host line says why");
|
||||
assert.ok(logs.includes("triage host: stopping the session: triage-key-rotated"), logs.join(" | "));
|
||||
const text = JSON.stringify(s) + logs.join("\n");
|
||||
for (const k of [OLD_KEY, NEW_KEY]) assert.equal(text.includes(k.slice(-43)), false, "no key in the state or the logs");
|
||||
});
|
||||
|
||||
test("key rotation: a message to a session whose key left the file stops it first, and the message starts a new session on the current key", { timeout: 30000 }, async (t) => {
|
||||
const root = scratch();
|
||||
let record = recordOf(OLD_KEY);
|
||||
const route = keyed(root, OLD_KEY);
|
||||
// The poll is slow here, so only the message path can stop the session.
|
||||
const { h, dataRoot } = host(t, root, { route, bwrap: standIn(root), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z"), keyRecord: () => record, keyPollMs: 600000 });
|
||||
await h.message("one");
|
||||
await runnerClient(root, dataRoot);
|
||||
const first = h.pid;
|
||||
// The owner turned Triage off and on: the old key is gone; the route now answers the new key.
|
||||
record = recordOf(NEW_KEY);
|
||||
route.key = NEW_KEY;
|
||||
rmSync(join(root, "stdin.json"));
|
||||
await h.message("two");
|
||||
// The stop happens inside message(), before the message is queued: assert it now, so a missing
|
||||
// stop fails here rather than at the test timeout below.
|
||||
assert.equal((await h.state()).lastStop?.code, "triage-key-rotated", "message() stops the session on the gone key");
|
||||
await runnerClient(root, dataRoot);
|
||||
assert.notEqual(h.pid, first, "a new session");
|
||||
assert.throws(() => process.kill(first, 0), "the old process is gone");
|
||||
const s = await h.state();
|
||||
assert.equal(s.lastStop.code, "triage-key-rotated");
|
||||
assert.equal(s.transcript.filter((e) => e.from === "host" && e.text === KEY_ROTATED).length, 1);
|
||||
assert.equal(s.transcript.filter((e) => e.from === "owner").length, 2, "the second message is kept and delivered");
|
||||
// The new session holds the current key: a message to it does not stop it.
|
||||
await h.message("three");
|
||||
assert.equal(h.pid !== first && h.running, true);
|
||||
assert.equal((await h.state()).transcript.filter((e) => e.text === KEY_ROTATED).length, 1);
|
||||
});
|
||||
|
||||
test("key rotation: with no key reader, or a key file that cannot be read, a running session is not stopped", { timeout: 30000 }, async (t) => {
|
||||
for (const keyRecord of [null, () => { throw Object.assign(new Error("unsafe"), { code: "triage-key-unsafe" }); }]) {
|
||||
const root = scratch();
|
||||
const { h, dataRoot } = host(t, root, { route: keyed(root, OLD_KEY), bwrap: standIn(root), usageSource: spendSource, now: () => new Date("2026-11-18T12:00:00.000Z"), keyRecord, keyPollMs: 50 });
|
||||
await h.message("one");
|
||||
await runnerClient(root, dataRoot);
|
||||
await new Promise((r) => setTimeout(r, 300));
|
||||
await h.message("two");
|
||||
assert.equal(h.running, true, String(keyRecord));
|
||||
assert.equal((await h.state()).lastStop, null);
|
||||
await h.stop();
|
||||
}
|
||||
assert.throws(() => createTriageHost({ dataRoot: "/x", keyRecord: "path" }), /keyRecord/);
|
||||
});
|
||||
@@ -23,7 +23,7 @@ import { readUsage, usagePath } from "../src/triage-usage.mjs";
|
||||
import { startServer } from "../src/serve.mjs";
|
||||
import { createDevOwner } from "../src/dev-owner.mjs";
|
||||
import { NO_ROUTE_MESSAGE, capMessage } from "../src/triage-http.mjs";
|
||||
import { keyIdOf, keyPrefix } from "../../cli/src/triage-key-file.mjs";
|
||||
import { keyIdOf, keyPrefix } from "../../bus/src/triage-key-file.mjs";
|
||||
|
||||
const FIXTURE = fileURLToPath(new URL("./fixtures/triage-adapter.mjs", import.meta.url));
|
||||
const SRC = fileURLToPath(new URL("../src/", import.meta.url));
|
||||
@@ -916,8 +916,8 @@ function loaded(t, args) {
|
||||
return readFileSync(out, "utf8").split("\n").filter(Boolean).map((u) => (u.startsWith("file:") ? fileURLToPath(u) : u));
|
||||
}
|
||||
const CLIENT = join(REPO_ROOT, "packages", "model-proxy", "src", "client.mjs");
|
||||
const KEY_VERBS = join(REPO_ROOT, "packages", "cli", "src", "triage-key.mjs");
|
||||
const KEY_FILE = join(REPO_ROOT, "packages", "cli", "src", "triage-key-file.mjs");
|
||||
const KEY_VERBS = join(REPO_ROOT, "packages", "bus", "src", "triage-key.mjs");
|
||||
const KEY_FILE = join(REPO_ROOT, "packages", "bus", "src", "triage-key-file.mjs");
|
||||
|
||||
test("the Console process never imports the proxy management client or the key verbs' module", { timeout: 120000 }, (t) => {
|
||||
// The control: the hook sees them when a module that uses them loads.
|
||||
|
||||
Reference in new issue
Block a user