fix(beehive): verify durable registration completion

Signed-off-by: Logan Johnson <loganj@squareup.com>
This commit is contained in:
Logan Johnson
2026-09-16 16:35:11 -04:00
parent e3024fab94
commit 24a9e43cad
4 changed files with 45 additions and 4 deletions

View File

@@ -375,7 +375,9 @@ A Stop result is not a recent host report that confirms the agent is stopped.` }
}
runtime ??= this.saveAgentConfiguration(v);
const { profile, profileState } = this.profilePreview;
await this.credential({ action: 'register-agent', directory: this.hostDirectory, secret: v.secret, profile: { profile, profileState }, runtimeId:runtime.id, expectedRevision:readSettings(this.hostDirectory).revision },abort.signal); check();
await this.credential({ action: 'register-agent', directory: this.hostDirectory, secret: v.secret, publicKey:key, profile: { profile, profileState }, runtimeId:runtime.id, expectedRevision:readSettings(this.hostDirectory).revision },abort.signal); check();
const committed = readSettings(this.hostDirectory);
if (!committed.agents.some(agent => agent.publicKey === key)) throw plain('Agent registration completion was not verified. Inspect saved registration before retrying.');
this.profilePreview = undefined;
if (!pending) this.status = 'Agent registered. Configuration saved.';
else {

View File

@@ -26,8 +26,12 @@ process.once('message', async (input: any) => {
process.send?.({ ok: true });
} else if (input.action === 'register-agent') {
const secret = agentNsec(input.secret);
registerAgent(input.directory, secret, systemCredentials, input.profile, input.runtimeId, input.expectedRevision);
process.send?.({ ok: true });
const registered = registerAgent(input.directory, secret, systemCredentials, input.profile, input.runtimeId, input.expectedRevision);
const committed = readSettings(input.directory);
if (!committed.agents.some(agent => agent.publicKey === registered.publicKey)) throw Error('Registration commit missing');
// Public completion receipt only. The controller must not infer durable
// registration from a helper process merely accepting the request.
process.send?.({ ok: true, registration: { publicKey: registered.publicKey, settingsRevision: committed.revision } });
} else if (input.action === 'add-openai') {
addOpenAI(input.directory,input.name,input.secret,providerCredentials());
process.send?.({ ok: true });

View File

@@ -25,7 +25,11 @@ export function managerCredential(input: object, signal: AbortSignal, helper = n
clearTimeout(timer); signal.removeEventListener('abort', cancel);
if (request.action === 'signin-buzz' && (code !== 0 || failed || signal.aborted || !result?.ok)) reject(Error(buzzOwnerKeyErrors[code === 0 && !failed && !signal.aborted ? result?.reason : 'access'] ?? buzzOwnerKeyErrors.access));
else if (code !== 0 || failed || signal.aborted || !result?.ok) reject(Error('Could not complete key access. It may have been cancelled, timed out, denied, or unavailable. The key may not match. Changes may already be saved. Inspect before you try again. No key was reset or saved as plain text.'));
else resolve(result);
else if (request.action === 'register-agent' &&
(!result.registration || result.registration.publicKey !== request.publicKey ||
!Number.isSafeInteger(result.registration.settingsRevision))) {
reject(Error('Agent registration completion was not verified. Inspect saved registration before retrying.'));
} else resolve(result);
});
child.send(input, error => { if (error) cancel(); });
});

View File

@@ -0,0 +1,31 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { pathToFileURL } from 'node:url';
import { managerCredential } from '../src/manager-credential.ts';
const key = 'a'.repeat(64);
function helper(source: string) {
const path = join(mkdtempSync(join(tmpdir(), 'beehive-receipt-')), 'helper.mjs');
writeFileSync(path, source, { mode: 0o600 });
return pathToFileURL(path);
}
test('registration helper completion requires a matching durable public receipt', async () => {
const signal = new AbortController().signal;
await assert.rejects(
managerCredential({ action: 'register-agent', publicKey: key }, signal,
helper("process.once('message',()=>{process.send({ok:true});process.disconnect()})")),
/completion was not verified/,
);
await assert.rejects(
managerCredential({ action: 'register-agent', publicKey: key }, signal,
helper("process.once('message',()=>{process.send({ok:true,registration:{publicKey:'b'.repeat(64),settingsRevision:2}});process.disconnect()})")),
/completion was not verified/,
);
const result = await managerCredential({ action: 'register-agent', publicKey: key }, signal,
helper(`process.once('message',()=>{process.send({ok:true,registration:{publicKey:'${key}',settingsRevision:2}});process.disconnect()})`));
assert.deepEqual(result.registration, { publicKey: key, settingsRevision: 2 });
});