@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
192 lines • 8.38 kB
JavaScript
import { attachMcpGrantPresentation } from "../../core/mcp-grant-presentations.js";
import { serializeJsonLine } from "./jsonl.js";
import { RaiserNotices } from "./raiser-notices.js";
/** Actual socket presentations; a shared session has one handler, not one per window. */
export class RpcSecurityPresentations {
#sessions = new Map();
#attachments = new Map();
constructor(fanout, inConnection, flush, locality) {
this.fanout = fanout;
this.inConnection = inConnection;
this.flush = flush;
this.locality = locality;
this.notices = new RaiserNotices(fanout, flush);
}
bind(sessionId, durableId, notices, select) {
const previous = this.#sessions.get(sessionId);
this.#stop(previous);
const state = {
durableId,
select,
snapshot: notices.snapshot,
registrations: new Map(),
grants: new Map(),
replayed: previous?.durableId === durableId ? previous.replayed : new Map(),
};
this.#sessions.set(sessionId, state);
state.stop = notices.subscribe((snapshot) => this.acceptSnapshot(sessionId, snapshot));
for (const id of this.fanout.attachedConnections(sessionId))
this.#register(id, state);
return () => {
if (this.#sessions.get(sessionId) !== state)
return;
this.#stop(state);
this.#sessions.delete(sessionId);
};
}
acceptSnapshot(sessionId, snapshot) {
let state = this.#sessions.get(sessionId);
if (state === undefined) {
state = { snapshot, registrations: new Map(), grants: new Map(), replayed: new Map() };
this.#sessions.set(sessionId, state);
}
state.snapshot = snapshot;
for (const id of this.fanout.attachedConnections(sessionId))
this.#replay(sessionId, id, snapshot);
}
attach(id, sessionId) {
const attached = this.#attachments.get(id) ?? new Set();
if (attached.has(sessionId))
return;
attached.add(sessionId);
this.#attachments.set(id, attached);
const state = this.#sessions.get(sessionId);
if (state !== undefined) {
this.#register(id, state);
this.#replay(sessionId, id, state.snapshot);
}
}
detach(id, sessionId) {
this.#attachments.get(id)?.delete(sessionId);
if (this.#attachments.get(id)?.size === 0)
this.#attachments.delete(id);
const state = this.#sessions.get(sessionId);
state?.registrations.get(id)?.();
state?.registrations.delete(id);
state?.replayed.delete(id);
if (state !== undefined)
this.#cancelFor(state, id);
}
dropConnection(id) {
for (const sessionId of [...(this.#attachments.get(id) ?? [])])
this.detach(id, sessionId);
}
capabilitiesChanged(id) {
for (const sessionId of this.#attachments.get(id) ?? []) {
const state = this.#sessions.get(sessionId);
if (state === undefined)
continue;
this.#register(id, state);
if (!this.fanout.connectionHas(id, "grant_select"))
this.#cancelFor(state, id);
else if (this.locality.isRemote(id))
this.#cancelFor(state, id, true);
}
}
routeWorkerGrant(sessionId, requestId, preferred, fallbackCapabilities, cancel, title) {
const attached = this.fanout.attachedConnections(sessionId);
// A raiser that speaks for another device never answers a grant (D11 R-1): a remote requester could otherwise
// approve its own request (senpi#6901 review H1); the grant goes to a local presenter or nowhere.
const raiser = preferred !== undefined && attached.includes(preferred) && this.#localPresenter(preferred);
const connection = raiser ? preferred : attached.find((id) => this.#localPresenter(id));
const accepted = connection !== undefined || (this.fanout.isEmpty() && fallbackCapabilities.includes("grant_select"));
if (!accepted) {
cancel();
return { accepted: false };
}
let state = this.#sessions.get(sessionId);
if (state === undefined) {
state = {
snapshot: { generation: 0, notices: [] },
registrations: new Map(),
grants: new Map(),
replayed: new Map(),
};
this.#sessions.set(sessionId, state);
}
state.grants.set(requestId, { connection, cancel, background: !raiser });
if (title !== undefined)
this.noticeGrant(sessionId, requestId, title, connection);
return { accepted: true, connection };
}
/** Attached connections other than the presenter see that a grant select is pending; only the presenter answers. */
noticeGrant(sessionId, requestId, title, presenter) {
this.notices.raised(sessionId, requestId, [presenter], { type: "grant_select_notice", title });
}
acceptWorkerResponse(sessionId, requestId, connection) {
const state = this.#sessions.get(sessionId);
const pending = state?.grants.get(requestId);
if (pending === undefined)
return undefined;
if (pending.connection !== connection ||
(connection !== undefined && !this.fanout.connectionHas(connection, "grant_select")))
return false;
// The entry stays until the worker reports the grant settled (settleGrant): a frame the worker
// refuses leaves the grant pending, and it must stay answerable only by its recipient.
return true;
}
settleGrant(sessionId, requestId) {
this.#sessions.get(sessionId)?.grants.delete(requestId);
this.notices.settled(sessionId, requestId, { type: "grant_select_notice_settled" });
}
dropSession(sessionId) {
this.#stop(this.#sessions.get(sessionId));
this.#sessions.delete(sessionId);
this.notices.dropSession(sessionId);
for (const [id, sessions] of this.#attachments) {
sessions.delete(sessionId);
if (sessions.size === 0)
this.#attachments.delete(id);
}
}
/** The in-process path has no raiser, so a grant presentation is always a background route: local only. */
#localPresenter(id) {
return this.fanout.connectionHas(id, "grant_select") && !this.locality.isRemote(id);
}
#register(id, state) {
if (!this.#localPresenter(id)) {
state.registrations.get(id)?.();
state.registrations.delete(id);
return;
}
if (state.registrations.has(id) || state.select === undefined || state.durableId === undefined)
return;
const select = state.select;
state.registrations.set(id, attachMcpGrantPresentation(state.durableId, (title, choices, opts) => this.inConnection(id, () => select(title, choices, opts))));
}
#replay(sessionId, id, snapshot) {
const target = this.fanout.get(id);
if (target === undefined)
return;
const state = this.#sessions.get(sessionId);
const revision = JSON.stringify(snapshot);
if (state?.replayed.get(id) === revision)
return;
state?.replayed.set(id, revision);
for (const notice of snapshot.notices)
target.actor.enqueue(serializeJsonLine({
type: "extension_ui_request",
id: crypto.randomUUID(),
sessionId,
method: "notify",
notifyType: "warning",
message: notice.message,
}));
this.flush();
}
#cancelFor(state, id, backgroundOnly = false) {
for (const [requestId, pending] of state.grants)
if (pending.connection === id && (!backgroundOnly || pending.background)) {
state.grants.delete(requestId);
pending.cancel();
}
}
#stop(state) {
state?.stop?.();
for (const stop of state?.registrations.values() ?? [])
stop();
for (const pending of state?.grants.values() ?? [])
pending.cancel();
}
}
//# sourceMappingURL=security-presentations.js.map