UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

117 lines • 7.11 kB
import { subscriptionLimits } from "../../core/subscription-limits/limit-store.js"; import { ConnectionRoles } from "./connection-roles.js"; import { compactUpdatesEnabled } from "../../core/engine-socket-flag.js"; import { ADMISSION_LIMITS, CompactAdmissions } from "./compact-admissions.js"; import { advertisedCompactCapabilities } from "./compact-negotiation.js"; import { latestSessionEffect } from "./effect-ledger.js"; import { createRpcSessionBinding } from "./session-binding.js"; import { drainSessionEffects, installSessionEffectSink } from "./session-effect-status.js"; import { SessionAttachOrder } from "./session-attach-order.js"; import { tearDownReleased } from "./session-router-close.js"; import { routingIdOf } from "./session-router-errors.js"; import { sweepIdleSessions } from "./session-router-occupancy.js"; import { followRetainedRecord } from "./session-router-residency.js"; import { registryStatusSource } from "./session-status-source.js"; import { HostParkLedger } from "./host-park-ledger.js"; import { closePark } from "./session-router-park.js"; import { PredecessorRoutes } from "./predecessor-routes.js"; import { hostGeneration } from "./protocol-identity.js"; export class RouterContext { constructor(registry, writer, defaults, createBinding = createRpcSessionBinding, connectionOptions = {}, idle) { this.routes = new PredecessorRoutes(this); this.parks = new HostParkLedger((opened) => closePark(this, opened)); this.admissions = compactUpdatesEnabled() ? new CompactAdmissions(ADMISSION_LIMITS, (socketEpoch, admissionId) => this.writer.routesAdmission(socketEpoch, admissionId)) : undefined; this.bindings = new Map(); /** Session handles opened by each connection, with one count per attachment. */ this.sessionsByConnection = new Map(); /** When each connection first attached to each session: relocation asks the earliest offerer (senpi#3994). */ this.attachOrder = new SessionAttachOrder(); /** Opens currently awaiting runtime/binding setup, keyed by connection. */ this.opensByConnection = new Map(); /** Connections whose socket has already been detached. */ this.releasedConnections = new Set(); this.pendingCapabilities = new Map(); /** Roles each connection declared through set_client_info (senpi#3811). */ this.connectionRoles = new ConnectionRoles(); this.finalizations = new Map(); this.draining = false; this.handoffParks = 0; this.drainExitRequested = false; this.handoffClosed = new Set(); /** Sessions whose automatic turns this draining generation already held (senpi#3114, `session-drain-hold.ts`). */ this.drainHeldSessions = new Set(); this.activeRequests = new Map(); /** Called after any request for that session settles; a release waits here for the others to finish. */ this.requestSettledListeners = new Map(); /** Halves the idle window while the host reports memory pressure; never refuses work. */ this.memoryPressure = false; /** senpi#7020: what this host is still taking over from a predecessor; `list_sessions` reports it as `handover`. */ this.inheritedHandover = undefined; /** The release_session teardown; the router installs its own member here, so it owns the step. */ this.releaseTeardown = (sessionId, sessionPath) => tearDownReleased(this, sessionId, sessionPath); // Every connection hears an account's new limit windows, whichever session's request reported them. this.stopLimitBroadcast = subscriptionLimits.subscribe((limits) => writer.broadcastHostRecord({ type: "subscription_limits", ...limits })); this.registry = registry; this.writer = writer; // The writer's background routes stay local (D11 S3-5): it reads each connection's declared origin from here. writer.originOf = (connection) => this.connectionRoles.originOf(connection); // omo-relay-daemon#307: roles read each delegated socket's live record, the same object the writer keeps. this.connectionRoles.useDelegations((connection) => writer.delegations.viewOf(connection)); this.defaults = defaults; this.awaitingPredecessorScan = idle?.handoverParkedDir !== undefined && hostGeneration(process.env) > 0; this.createBinding = createBinding; this.connectionOptions = { ...connectionOptions, capabilities: connectionOptions.capabilities && advertisedCompactCapabilities(connectionOptions.capabilities, false), }; this.idleNow = idle?.now ?? Date.now; this.idleEvictionMs = idle?.idleEvictionMs ?? Number.POSITIVE_INFINITY; this.emptyExitMs = idle?.emptyExitMs ?? Number.POSITIVE_INFINITY; this.onEmptyExit = idle?.onEmptyExit; this.onHandoffParked = idle?.onHandoffParked; this.handoverParkedDir = idle?.handoverParkedDir; this.canExitWhenEmpty = idle?.canExitWhenEmpty; writer.status.setSource(registryStatusSource(registry)); writer.status.setLastEffectProvider((sessionId) => { const session = this.registry.peek(sessionId)?.runtime?.session; if (session === undefined) return undefined; const latest = latestSessionEffect(session); return latest === undefined ? undefined : { effectId: latest.effectId, kind: latest.kind, state: latest.state }; }); writer.afterRecord = (sessionId) => { followRetainedRecord(this, sessionId); const session = this.registry.peek(sessionId)?.runtime?.session; if (session === undefined) return; for (const status of drainSessionEffects(session)) this.writer.status.publishEffect(sessionId, status); }; this.uninstallEffectSink = installSessionEffectSink({ owns: (session) => routingIdOf(this, session) !== undefined, flush: (session, statuses) => { const sessionId = routingIdOf(this, session); if (sessionId === undefined) return; for (const status of statuses) this.writer.status.publishEffect(sessionId, status); }, }); if (Number.isFinite(this.idleEvictionMs) || Number.isFinite(this.emptyExitMs)) { // Unref'd: occupancy hygiene must never be the reason the event loop stays open. const tickMs = Math.max(20, Math.min(5_000, Math.min(this.idleEvictionMs, this.emptyExitMs) / 4)); this.sweepTimer = setInterval(() => sweepIdleSessions(this), tickMs); this.sweepTimer.unref?.(); } } compactAvailable() { return (!this.awaitingPredecessorScan && !this.inheritedHandover?.predecessorDraining && this.registry.supportsCompact?.() === true); } } //# sourceMappingURL=session-router-context.js.map