@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
101 lines • 4.37 kB
JavaScript
import { releaseConnection } from "./session-router-attachments.js";
import { dispose, tearDownReleased } from "./session-router-close.js";
import { RouterContext } from "./session-router-context.js";
import { beginDrain, sweepIdleSessions } from "./session-router-occupancy.js";
import { reopenParked } from "./session-router-open.js";
import { handle } from "./session-router-routing.js";
import { authRecord } from "./rpc-auth-proof.js";
import { classifyRpcMethod } from "./method-classes.js";
import { ATTACH_GATE_EXEMPT } from "./session-attachment-gate.js";
export class SessionCommandRouter {
constructor(...args) {
this.context = new RouterContext(...args);
this.context.routes.reopen = (entry) => this.reopenParked(entry);
this.context.releaseTeardown = (sessionId, sessionPath) => this.tearDownReleased(sessionId, sessionPath);
}
/** The release_session park teardown. Routed through this member, as before the split (senpi#3681). */
tearDownReleased(sessionId, sessionPath) {
return tearDownReleased(this.context, sessionId, sessionPath);
}
get sessionCount() {
return this.context.registry.size;
}
handle(command) {
return handle(this.context, command);
}
configureRoutes(connection, record) {
return this.context.routes.configure(connection, record);
}
/** A successor connection can attach to existing writers; it cannot create work outside them. */
admitsRoutedRecord(connection, value) {
const record = authRecord(value);
if (!record)
return false;
switch (record.type) {
case "get_protocol_info":
case "list_sessions":
case "launch_proven":
case "declare_delegation":
return true;
case "set_client_info":
return (record.sessionId === undefined ||
(typeof record.sessionId === "string" &&
this.context.sessionsByConnection.get(connection)?.has(record.sessionId) === true));
case "open_session":
return typeof record.sessionPath === "string" && typeof record.expectedHostInstance === "string";
case "attach_session":
return typeof record.expectedHostInstance === "string" && typeof record.durableSessionId === "string";
case "park_session":
case "claim_session":
case "warm":
case "move_session":
case "select_hand":
case "begin_handover":
case "detach_connection":
case "subscribe_sessions":
case "unsubscribe_sessions":
case "list_hands":
case "find_session":
return false;
default:
return (typeof record.sessionId === "string" &&
(this.context.sessionsByConnection.get(connection)?.has(record.sessionId) === true ||
this.context.handoffClosed.has(record.sessionId) ||
(ATTACH_GATE_EXEMPT.has(record.type) &&
this.context.registry.peek(record.sessionId)?.state === "open")) &&
classifyRpcMethod(record) !== undefined);
}
}
beginDrain() {
beginDrain(this.context);
}
setMemoryPressure(pressure) {
this.context.memoryPressure = pressure;
}
setInheritedHandover(state) {
this.context.awaitingPredecessorScan = false;
this.context.inheritedHandover = state;
}
sweepIdleSessions() {
sweepIdleSessions(this.context);
}
dispose() {
this.context.routes.dispose();
return dispose(this.context);
}
reopenParked(entry) {
return reopenParked(this.context, entry);
}
/** Whether a connection holds a session here or has an open in flight (oh-my-openagent#14272). */
holdsSessions(connectionId) {
return ((this.context.sessionsByConnection.get(connectionId)?.size ?? 0) > 0 ||
(this.context.opensByConnection.get(connectionId)?.size ?? 0) > 0);
}
releaseConnection(connectionId) {
return releaseConnection(this.context, connectionId);
}
setHostMemoryView(view) {
this.context.hostMemoryView = view;
}
}
//# sourceMappingURL=session-command-router.js.map