@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
128 lines • 6.11 kB
JavaScript
import { randomUUID } from "node:crypto";
/** The one acquire feature this host negotiates: `mcp_form_resolved.elsewhere` and `mcp_form_settled`. */
const ELSEWHERE_FEATURE = "elsewhere_v1";
/** A grant that may answer secret prompts on the sealed route (D11 secret phase 4.3, 5.1). */
export const SECRET_INPUT_FEATURE = "secret_input_v1";
/** A signed acquire's feature list: at most 16 short strings. Unknown names are ignored, never an error. */
export function features(value) {
return (Array.isArray(value) &&
value.length <= 16 &&
value.every((name) => typeof name === "string" && name.length > 0 && name.length <= 64));
}
/** At most one grant per (principal, connection) on a binding; other holders coexist (D11 S3-3b). */
function holds(binding, principal, connection) {
return [...binding.grants].some((grant) => grant.registration.principal === principal && grant.registration.connection === connection);
}
export function enroll(state, principal = randomUUID(), identity = { principalKind: "inherited" }) {
const generation = randomUUID();
return {
register: (handle, sender, options) => {
const binding = [...state.bindings.values()].find((value) => value === handle);
const connection = state.connections.get(sender);
if (!binding || !connection?.attachments.has(binding) || holds(binding, principal, connection))
throw new Error("presenter_not_authorized");
const registration = {
id: randomUUID(),
principal,
generation,
binding,
connection,
kind: options.kind,
elsewhere: options.features?.includes(ELSEWHERE_FEATURE) === true,
secretInput: options.features?.includes(SECRET_INPUT_FEATURE) === true,
active: true,
};
state.registrations.set(registration, registration);
return registration;
},
acquire: (handle) => {
const registration = state.registrations.get(handle);
if (!registration || registration.principal !== principal || !registration.active)
throw new Error("presenter_not_authorized");
const { binding, connection } = registration;
const attachment = connection.attachments.get(binding);
if (!attachment || binding.lifetime.signal.aborted || holds(binding, principal, connection))
throw new Error("presenter_not_authorized");
const scope = {
hostInstance: state.instance,
durableSessionId: binding.durableId,
bindingEpoch: binding.epoch,
attachmentId: attachment.id,
attachmentEpoch: attachment.epoch,
connectionId: connection.id,
connectionEpoch: connection.epoch,
presenterPrincipalId: principal,
presenterGeneration: generation,
presentationBindingId: registration.id,
presenterKind: registration.kind,
purpose: "mcp_form",
};
const grant = {
id: randomUUID(),
registration,
scope,
answeredBy: { ...identity, presenterKind: registration.kind },
lifetime: new AbortController(),
dialogs: new Map(),
};
binding.grants.add(grant);
binding.presence ??= { id: randomUUID(), lifetime: new AbortController() };
for (const listener of binding.listeners)
listener();
return { grantId: grant.id, scope };
},
revoke: (handle) => {
const registration = state.registrations.get(handle);
if (registration?.principal === principal)
revoke(state, registration);
},
};
}
export function current(state, grant) {
const { registration, scope } = grant;
const { binding, connection } = registration;
return (registration.active &&
!binding.lifetime.signal.aborted &&
!grant.lifetime.signal.aborted &&
state.bindings.get(binding.sessionId) === binding &&
binding.grants.has(grant) &&
state.connections.get(connection) === connection &&
connection.attachments.get(binding)?.epoch === scope.attachmentEpoch);
}
export function holders(state, binding) {
return [...binding.grants].filter((grant) => current(state, grant));
}
/**
* Whether a current account-principal grant on this binding negotiated `secret_input_v1`: only those can become
* sealed holders (D11 secret phase 5.1 conditions 2 and 3), so only they need a host token.
*/
export function secretInputHeld(state, handle) {
return secretCandidates(state, handle).length > 0;
}
/** The current account-principal grants on this binding that negotiated `secret_input_v1` (5.1 conditions 1-3). */
export function secretCandidates(state, handle) {
const binding = [...state.bindings.values()].find((value) => value === handle);
return binding === undefined
? []
: holders(state, binding).filter((grant) => grant.registration.secretInput && grant.answeredBy.principalKind === "account");
}
export function revoke(state, registration, operation = false) {
registration.active = false;
state.registrations.delete(registration);
const binding = registration.binding;
const grant = [...binding.grants].find((value) => value.registration === registration);
if (!grant)
return;
binding.grants.delete(grant);
// Retain entry references after authority is fenced. Cleanup never validates the revoked sender.
for (const dialog of [...grant.dialogs.values()])
dialog.revoked(operation);
if (binding.grants.size === 0) {
binding.presence?.lifetime.abort();
binding.presence = undefined;
}
for (const listener of binding.listeners)
listener();
grant.lifetime.abort();
}
//# sourceMappingURL=mcp-presenter-grants.js.map