@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
170 lines • 6.86 kB
JavaScript
/**
* The terminal's presenter host surface (senpi#4249, D11 2.9 S3-6b): its control connections become presenters on the
* TUI's own `McpPresenterAuthority`, the one the terminal itself holds its `local_tui` grant on. A connection that
* declared `interactive_user` + `mcp_form_presenter_v1` is connected, attached while it holds a live attachment, and
* withdrawn when it loses a role; its private frames are answered on that connection only, as `mcp_form_reply`.
*
* Private records never pass through the terminal's record writer, its feed, an observer subscription, a session entry
* or a ledger: they are written straight to the connection's own socket, as the RPC host's `handleMcpForm` does.
*/
import { rm } from "node:fs/promises";
import { serializeJsonLine } from "../rpc/jsonl.js";
import { startMcpPresenterControl } from "../rpc/mcp-presenter-control-start.js";
import { presenterContextReply } from "../rpc/mcp-presenter-host-surface.js";
import { TerminalSealedSecrets } from "./terminal-sealed-secrets.js";
export const TERMINAL_PRESENTER_COMMANDS = [
"mcp_presenter_context",
"mcp_form_presented",
"mcp_form_response",
"mcp_form_sync",
"mcp_presenter_secret_answer",
];
/**
* The terminal's presenter surface, and its control endpoint when an account authority is injected: a terminal has no
* fd-4 path, so every other presenter on it is an account principal. A failed start leaves the surface without one.
*/
export async function startTerminalPresenters(presenter) {
const surface = new TerminalPresenterSurface(presenter.local);
const started = await startMcpPresenterControl(surface, { enforcement: true, account: presenter.account });
return {
surface,
dispose: async () => {
started?.control.dispose();
if (started?.dir)
await rm(started.dir, { recursive: true, force: true });
},
};
}
function presents(capabilities) {
return capabilities.includes("interactive_user") && capabilities.includes("mcp_form_presenter_v1");
}
function writePrivate(transport, record) {
try {
transport.writeRaw(serializeJsonLine(record));
return true;
}
catch {
return false;
}
}
export class TerminalPresenterSurface {
constructor(local) {
/** The TUI owns this authority: a control over it revokes only its own grants when it stops. */
this.sharedAuthority = true;
this.peers = new Map();
this.local = local;
this.authority = local.authority;
this.binding = local.binding;
this.sessionId = local.sessionId;
}
attachSealedSecrets(source) {
const sealed = new TerminalSealedSecrets(source, this.sessionId, this.local.admit);
this.local.sealed = sealed;
return () => {
if (this.local.sealed === sealed)
this.local.sealed = undefined;
sealed.dispose();
};
}
getMcpFormBinding(sessionId) {
return sessionId === this.sessionId ? this.binding : undefined;
}
findMcpPresenterConnection(nonce) {
for (const peer of this.peers.values())
if (peer.sender?.nonce === nonce)
return peer.sender;
return undefined;
}
getConnectionCapabilities(id) {
return this.peers.get(id)?.capabilities;
}
advertiseMcpPresenterControl(path) {
this.controlEndpoint = path;
}
withdrawMcpPresenterControl(path) {
if (this.controlEndpoint === path)
this.controlEndpoint = undefined;
}
opened(id, transport) {
this.peers.set(id, { transport, capabilities: [], attached: false });
}
declare(id, capabilities) {
const peer = this.peers.get(id);
if (!peer)
return;
peer.capabilities = [...capabilities];
if (!presents(capabilities)) {
if (peer.sender)
this.authority.withdraw(peer.sender);
return;
}
if (!peer.sender) {
const sender = this.authority.connect(id, (frame) => {
if (this.peers.get(id) !== peer || !writePrivate(peer.transport, frame)) {
this.authority.detach(sender);
return false;
}
// A pending drain does not undo an accepted write or drop the next resolution.
void peer.transport.waitForBackpressure().catch(() => this.authority.detach(sender));
return true;
});
peer.sender = sender;
}
if (peer.attached)
this.authority.attach(peer.sender, this.binding);
}
attach(id) {
const peer = this.peers.get(id);
if (!peer)
return;
peer.attached = true;
if (peer.sender)
this.authority.attach(peer.sender, this.binding);
}
detach(id) {
const peer = this.peers.get(id);
if (!peer)
return;
peer.attached = false;
if (peer.sender)
this.authority.detach(peer.sender, this.binding);
}
closed(id) {
const sender = this.peers.get(id)?.sender;
this.peers.delete(id);
if (sender)
this.authority.detach(sender);
}
/** A private frame from connection `id`, answered on that connection: authority is checked before any lookup. */
receive(id, frame) {
const peer = this.peers.get(id);
if (!peer)
return;
if (frame.type === "mcp_presenter_secret_answer") {
const lineId = typeof frame.id === "string" ? { id: frame.id } : {};
const answered = this.local.sealed?.receive(id, frame) ?? Promise.resolve({ error: "secret_answer_refused" });
void answered
.catch(() => ({ error: "secret_answer_refused" }))
.then((reply) => {
const written = writePrivate(peer.transport, {
type: "mcp_presenter_secret_reply",
...lineId,
...reply,
});
if (!written && peer.sender)
this.authority.detach(peer.sender);
});
return;
}
const sender = peer.sender;
const reply = frame.type === "mcp_presenter_context"
? presenterContextReply(this.authority, sender, frame, this.controlEndpoint)
: sender
? this.authority.receive(sender, frame)
: { error: "presenter_not_authorized" };
const answer = { type: "mcp_form_reply", ...(typeof frame.id === "string" ? { id: frame.id } : {}), ...reply };
if (!writePrivate(peer.transport, answer) && sender)
this.authority.detach(sender);
}
}
//# sourceMappingURL=terminal-presenter-surface.js.map