fix(organization): an approved hire and the boot copy definitions run the definition check (#1873)
The hire service takes the bus's definition check as scanDefinition and runs it on the stored proposal entry before preview, check or apply goes further, and on the approved entry again just before the writers. An approval whose definition holds a credential the store came to hold after the raise refuses definition-holds-credential before the decision is consumed; nothing is written. Without the hook the entry still goes through assertClean whole. Bus start and credentials.issue also check the boot copy snapshot's definitions (A4 root agents file definitions, re-applied on every reload), which no organization file holds.
This commit is contained in:
1 parent
1a45a7274b
commit
16cdf5cdc1
6 files changed
+93
-11
No files matched your search
@@ -12,8 +12,9 @@ const deny = (code) => {
|
||||
throw new BusError(code);
|
||||
};
|
||||
// `organizations` lists every business this bus serves; issue checks each one's definitions. Without
|
||||
// it, only the issuing business's.
|
||||
export function credentialVerbs({ credentials, writer, organizations = null }) {
|
||||
// it, only the issuing business's. `boot` is the bus's boot copy snapshot's definitions, { org,
|
||||
// definitions }, checked as well.
|
||||
export function credentialVerbs({ credentials, writer, organizations = null, boot = null }) {
|
||||
const state = (instance, service) =>
|
||||
credentials.status().find((e) => e.instance === instance && e.service === service)?.state;
|
||||
return (id, verb, args, actor) => {
|
||||
@@ -36,7 +37,7 @@ export function credentialVerbs({ credentials, writer, organizations = null }) {
|
||||
const status = credentials.issue(
|
||||
instance, service, token, date,
|
||||
() => writer.write(id, next, { revision, actor, detail }),
|
||||
(clean) => scanOrganizations({ organizations: organizations?.() ?? [id], own: { id, document }, readRaw: (b) => writer.readRaw(b) }, clean),
|
||||
(clean) => scanOrganizations({ organizations: organizations?.() ?? [id], own: { id, document }, readRaw: (b) => writer.readRaw(b), boot }, clean),
|
||||
);
|
||||
// Row 210 (design D9): a host key's public line and fingerprint, for ops-01's authorized_keys.
|
||||
const host = status.fingerprint ? { publicKey: status.publicKey, fingerprint: status.fingerprint } : {};
|
||||
|
||||
@@ -57,9 +57,9 @@ export function heldPath(entry, assertClean) {
|
||||
|
||||
// The refusal for one definition. `name` is the definition's name without "org:". At issue time
|
||||
// (`organization` given) it names the organization too, and the value is the one being issued.
|
||||
export function definitionRefusal(name, path, { organization } = {}) {
|
||||
export function definitionRefusal(name, path, { organization, boot = false } = {}) {
|
||||
const quoted = typeof name === 'string' && NAME.test(name) ? name : '*';
|
||||
const where = name === null ? 'definitions' : `definitions.${quoted} ${path}`;
|
||||
const where = `${boot ? 'boot copy ' : ''}${name === null ? 'definitions' : `definitions.${quoted} ${path}`}`;
|
||||
if (organization === undefined) return new BusError(DEFINITION_HOLDS_CREDENTIAL, `${where} holds a credential this bus holds`);
|
||||
const org = typeof organization === 'string' && NAME.test(organization) ? organization : '*';
|
||||
return new BusError(DEFINITION_HOLDS_CREDENTIAL, `organization ${org} ${where} holds the credential being issued`);
|
||||
@@ -91,8 +91,10 @@ export function scanDefinitions(definitions, assertClean, options = {}) {
|
||||
// that already sits in any organization's definitions, so no definition text becomes a held value
|
||||
// after its launch checks passed. `organizations` is every business this bus serves; `own` is the
|
||||
// issuing one's document, already read. Any other file is read raw, as bus start reads it, so a
|
||||
// definition no instance names is covered; a file that can't be read refuses.
|
||||
export function scanOrganizations({ organizations, own, readRaw }, assertClean) {
|
||||
// definition no instance names is covered; a file that can't be read refuses. `boot` is the bus's boot
|
||||
// copy snapshot ({ org, definitions }, #1873 A3/A4): the root agents file's definitions merged with the
|
||||
// organization's at boot. Every reload re-applies it, so its text is live for the bus's lifetime.
|
||||
export function scanOrganizations({ organizations, own, readRaw, boot = null }, assertClean) {
|
||||
for (const organization of organizations) {
|
||||
let definitions;
|
||||
if (organization === own.id) definitions = own.document.definitions;
|
||||
@@ -108,4 +110,5 @@ export function scanOrganizations({ organizations, own, readRaw }, assertClean)
|
||||
}
|
||||
scanDefinitions(definitions, assertClean, { organization });
|
||||
}
|
||||
if (boot?.definitions !== undefined) scanDefinitions(boot.definitions, assertClean, { organization: boot.org, boot: true });
|
||||
}
|
||||
@@ -93,7 +93,7 @@ group(
|
||||
group(
|
||||
"definition-holds-credential",
|
||||
"R1: no held credential in an organization definition's role text",
|
||||
"Ask the organization owner to remove the credential from the named definition and key, then restart the bus, relaunch the agent or issue the credential again. Do not copy the text anywhere to inspect it."
|
||||
"Ask the organization owner to remove the credential from the named definition and key, then restart the bus, relaunch the agent or issue the credential again. For a hire, deny it and have the chief raise it again without that text. Do not copy the text anywhere to inspect it."
|
||||
);
|
||||
group(
|
||||
"definition-scan-unreadable",
|
||||
|
||||
@@ -399,6 +399,8 @@ export async function startBroker({
|
||||
const raw = organizationWriter({ dir: organization.dir, rolesDir: organization.rolesDir, dataRoot: instanceRoot ?? dataRoot }).readRaw(id);
|
||||
if (raw.readable) scanDefinitions(raw.document.definitions, clean);
|
||||
}
|
||||
// The boot copy snapshot's definitions (the root agents file's, A4): every reload re-applies them.
|
||||
if (copies?.definitions !== undefined) scanDefinitions(copies.definitions, clean, { boot: true });
|
||||
if (proxy) {
|
||||
const inventory = modelRunInventory(dataRoot, modelRuns, credentials);
|
||||
modelKeys = new ModelKeys({ credentials, client: modelProxyClient, configDir: org ? instanceConfigDir : organization.dir, org: org?.id ?? null, lock: proxyLock,
|
||||
@@ -890,7 +892,7 @@ export async function startBroker({
|
||||
// Row 194 (D2.3): the role files as they stand, for the hire checks.
|
||||
hires: organization ? (()=>{
|
||||
const service=hireDecisions({writer:organizationWriter({dir:organization.dir,rolesDir:organization.rolesDir,dataRoot:instanceRoot??dataRoot}),rolesDir:organization.rolesDir,configDir:organization.dir,dataRoot,
|
||||
system:organization.system,levels,catalogueFor:async(id,doc)=>doc.plugins?.['model-provider']?.length?catalogueFor(id,{fresh:true}):null,assertClean:value=>credentials.assertClean(value),registryFor:doc=>organizationPlugins(instanceRoot??dataRoot,doc.plugins).registry});
|
||||
system:organization.system,levels,catalogueFor:async(id,doc)=>doc.plugins?.['model-provider']?.length?catalogueFor(id,{fresh:true}):null,assertClean:value=>credentials.assertClean(value),scanDefinition:(name,entry)=>scanDefinitions({[name]:entry},value=>credentials.assertClean(value)),registryFor:doc=>organizationPlugins(instanceRoot??dataRoot,doc.plugins).registry});
|
||||
return {...service,apply:async(id,decision,caller)=>{
|
||||
const proposal=service.read(decision);
|
||||
const publish=async()=>{
|
||||
@@ -923,7 +925,7 @@ export async function startBroker({
|
||||
} : null,
|
||||
credentialVerbs: organization
|
||||
? credentialVerbs({ credentials, writer: organizationWriter({ dir: organization.dir, rolesDir: organization.rolesDir, dataRoot:instanceRoot ?? dataRoot }),
|
||||
organizations: () => Object.keys(businesses) })
|
||||
organizations: () => Object.keys(businesses), boot: copies?.definitions ? { org: copies.org, definitions: copies.definitions } : null })
|
||||
: null,
|
||||
// Row 65: a new launch binds only on a harness its business switches on, read from the file
|
||||
// as it stands now; an unreadable file switches none on.
|
||||
|
||||
@@ -63,6 +63,7 @@ test('org-defscan: the refusal code is in the TI refusal table with a next step'
|
||||
assert.match(entry.rail, /^R\d+:/);
|
||||
assert.match(entry.nextStep, /definition/);
|
||||
assert.match(entry.nextStep, /issue the credential again/);
|
||||
assert.match(entry.nextStep, /For a hire, deny it/);
|
||||
// The issue-time check's fail-closed code for an organization file it cannot read.
|
||||
const unreadable = REFUSALS[DEFINITION_SCAN_UNREADABLE];
|
||||
assert.ok(unreadable, 'definition-scan-unreadable is a known refusal');
|
||||
@@ -271,3 +272,65 @@ test('org-defscan: heldPath names the first place, a key by its parent, and neve
|
||||
assert.throws(() => scanDefinitions({ [held]: { instructions: 'ok' } }, assertClean), (e) => e.reason === 'definitions.* key holds a credential this bus holds' && !e.reason.includes(held));
|
||||
assert.doesNotThrow(() => scanDefinitions(undefined, assertClean));
|
||||
});
|
||||
|
||||
test('org-defscan: an approved hire whose definition holds a credential issued after the raise refuses before the decision is consumed', async (t) => {
|
||||
// The chief raises a hire whose instructions carry text the store does not hold yet. The proposal is
|
||||
// private, so the issue-time check cannot see it, and the store then comes to hold that text. The
|
||||
// approval runs the same definition check as start, launch and issue: the refusal names the code, the
|
||||
// decision stays open, and the organization file is as it was.
|
||||
const LATER = 'fake_later_token_0123456789';
|
||||
const o = org(t, (entry) => entry, { held: 'fake_boot_token_5555555555' });
|
||||
const boot = o.boot();
|
||||
const bus = await o.start(boot);
|
||||
t.after(() => bus.close());
|
||||
const chief = bus.broker.bindLaunch({ business: 'acme', role: 'chief', run: 'chief-run', harness: 'pi' });
|
||||
bus.broker.request(chief, { verb: 'role.claim' });
|
||||
const inline = { tools: ['read'], network: 'none', authority: { withinRole: [], crossRole: [] }, credentials: [], instructions: `Read and report.\nExample ${LATER}\n` };
|
||||
const raised = await bus.broker.request(chief, { verb: 'hire.raise', args: { op: 'hire', level: 'org', name: 'scout', role: { inline }, route: { harness: 'pi', model: 'glm-5.3-flash' }, reason: 'Need help' } });
|
||||
// Issued through the store, so the organization file and its revision stay as they were and the
|
||||
// approval reaches the definition check rather than the revision check.
|
||||
bus.credentials.issue('acme/coder', 'gitea', LATER, '2099-01-01');
|
||||
assert.ok(holds(bus, LATER));
|
||||
const writer = organizationWriter({ dir: boot.organization.dir, rolesDir: boot.organization.rolesDir, dataRoot: o.f.dataRoot });
|
||||
const before = readFileSync(o.file);
|
||||
const revision = writer.read('acme').revision;
|
||||
const owner = bus.broker.bindConsole({ business: 'acme', human: o.f.doc.human, subject: { provider: 'test', iss: 'https://issuer.test', sub: 'owner' } });
|
||||
const refused = (e) => {
|
||||
assert.equal(e.code, DEFINITION_HOLDS_CREDENTIAL);
|
||||
for (const text of [e.message, String(e.reason), String(e.stack), JSON.stringify(e)]) assert.ok(!text.includes(LATER), 'the refusal never carries the value');
|
||||
return true;
|
||||
};
|
||||
await assert.rejects(bus.broker.request(owner, { verb: 'hire.resolve.console', args: { id: raised.id, choice: 'yes', digest: raised.hire.digest } }), refused);
|
||||
// Nothing was written.
|
||||
assert.deepEqual(readFileSync(o.file), before);
|
||||
assert.equal(writer.read('acme').revision, revision);
|
||||
assert.ok(!Object.hasOwn(writer.read('acme').document.roles, 'scout'));
|
||||
// The decision was not consumed: a second approval meets the same check, not decision-closed.
|
||||
await assert.rejects(bus.broker.request(owner, { verb: 'hire.resolve.console', args: { id: raised.id, choice: 'yes', digest: raised.hire.digest } }), refused);
|
||||
assert.deepEqual(readFileSync(o.file), before);
|
||||
// A hire with clean text, raised and approved on the same bus, is written as before.
|
||||
const clean = await bus.broker.request(chief, { verb: 'hire.raise', args: { op: 'hire', level: 'org', name: 'helper', role: { inline: { ...inline, instructions: 'Read and report.\n' } }, route: { harness: 'pi', model: 'glm-5.3-flash' }, reason: 'Need help' } });
|
||||
assert.equal((await bus.broker.request(owner, { verb: 'hire.resolve.console', args: { id: clean.id, choice: 'yes', digest: clean.hire.digest } })).choice, 'yes');
|
||||
assert.ok(Object.hasOwn(writer.read('acme').document.roles, 'helper'));
|
||||
onlyInOrganization(join(o.root, 'config/businesses'), LATER);
|
||||
});
|
||||
|
||||
test('org-defscan: the boot copy snapshot\'s definitions (the root agents file\'s) are checked at start and at issue', async (t) => {
|
||||
// A4: a root definition reaches an organization through the boot copy snapshot, which every reload
|
||||
// re-applies. Its text is in no organization file, so the start and issue checks read the snapshot.
|
||||
const LATER = 'fake_later_token_0123456789';
|
||||
const snapshot = (instructions) => ({ org: 'acme', root: 'ok', rootAgents: {}, copies: {},
|
||||
definitions: { rooted: { ...entryFromTemplate(readTemplate(REPO_ROLES, 'coder')), instructions } } });
|
||||
const startWith = (o, copies, b = o.boot()) => startBroker({ dataRoot: o.f.dataRoot, businesses: b.businesses, organization: b.organization, copies });
|
||||
// Held at start: the start refuses, naming the snapshot's definition and key.
|
||||
const held = org(t);
|
||||
refusedWithout(await outcome(startWith(held, snapshot(`Root copy ${SENTINEL}\n`))), 'boot copy definitions.rooted instructions holds a credential this bus holds');
|
||||
// Not held at start: the bus starts, then issuing that text refuses and the store is as it was.
|
||||
const o = org(t, (entry) => entry, { held: 'fake_boot_token_5555555555' });
|
||||
const bus = await startWith(o, snapshot(`Root copy ${LATER}\n`));
|
||||
t.after(() => bus.close());
|
||||
assert.throws(() => verbs(o, bus).issue(LATER), (e) => issueRefused(e, LATER, 'organization acme boot copy definitions.rooted instructions holds the credential being issued'));
|
||||
assert.ok(!holds(bus, LATER));
|
||||
assert.equal(state(bus), 'valid');
|
||||
assert.equal(verbs(o, bus).issue('fake_clean_token_4444444444').state, 'valid');
|
||||
});
|
||||
@@ -35,7 +35,15 @@ const publicOf = p => ({ id:p.id, revision:p.revision, op:p.request.op, level:p.
|
||||
...(p.entry ? {role:structuredClone(p.entry.role)} : {}),
|
||||
// reason and all free text stay in the private proposal as well.
|
||||
flagged:p.flagged, credentialLines:p.credentialLines, expiresAt:p.expiresAt, digest:hash(p) });
|
||||
export function hireDecisions({ writer, rolesDir, configDir, dataRoot, assertClean, system = {}, levels = () => null, catalogueFor = async () => null, registryFor = () => buildRegistry() }) {
|
||||
// org-defscan (#1873): `scanDefinition(name, entry)` is the bus's definition check on the approved
|
||||
// entry; it throws (definition-holds-credential, naming the definition and key) when the live store
|
||||
// holds text in it. Without one, the entry still goes through assertClean whole.
|
||||
export function hireDecisions({ writer, rolesDir, configDir, dataRoot, assertClean, scanDefinition, system = {}, levels = () => null, catalogueFor = async () => null, registryFor = () => buildRegistry() }) {
|
||||
const scanEntry = (name, entry) => {
|
||||
if (typeof scanDefinition === 'function') return scanDefinition(name, structuredClone(entry));
|
||||
if (typeof assertClean !== 'function') fail('definition-secret-check-required');
|
||||
assertClean(entry);
|
||||
};
|
||||
const home = join(dataRoot,'authority-proposals');
|
||||
let receiptCache;
|
||||
const rootReceipts=()=>{
|
||||
@@ -151,6 +159,9 @@ export function hireDecisions({ writer, rolesDir, configDir, dataRoot, assertCle
|
||||
async function adjusted(business, decision, adjust) {
|
||||
const p=read(decision);
|
||||
if(p.business!==business)fail('decision-stale');
|
||||
// org-defscan (#1873): the stored entry is the definition text an approval writes. A credential
|
||||
// issued after the proposal refuses it here, before preview, check or apply goes further.
|
||||
if(p.entry)scanEntry(p.definition,p.entry);
|
||||
if(adjust===undefined)return p;
|
||||
keys(adjust,['role','credentials','route']);
|
||||
if(!p.entry)fail('invalid-request');
|
||||
@@ -216,6 +227,8 @@ export function hireDecisions({ writer, rolesDir, configDir, dataRoot, assertCle
|
||||
const vars=r.route ? {harness:r.route.harness,model:r.route.model,...(r.route.thinking?{thinking:r.route.thinking}:{})} : {};
|
||||
const currentDoc=r.level==='root'?(await readRootAgentsFile(configDir)).document:writer.read(business).document;
|
||||
const approvedEntry=p.entry?{...p.entry,rev:(currentDoc.definitions?.[p.definition]?.rev??0)+1}:null;
|
||||
// org-defscan (#1873): check() ran before the awaits above; the entry the writers get is checked again.
|
||||
if(approvedEntry)scanEntry(p.definition,approvedEntry);
|
||||
const excluded=r.level==='root' && r.op==='change' ? currentDoc.agents[r.name]?.excludeOrgs : undefined;
|
||||
const entry=r.op==='fire'?null:{definition:`org:${p.definition}`,vars,hireDecision:p.id,...(excluded===undefined?{}:{excludeOrgs:excluded})};
|
||||
if(r.level==='root') {
|
||||
|
||||
Reference in new issue
Block a user