UNPKG

@code-yeongyu/senpi

Version:

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

105 lines • 4.35 kB
const keyOf = ({ socketEpoch, sessionId }) => `${socketEpoch}\u0000${sessionId}`; /** * The stream records of one host: one per peer incarnation and routing session, replaced by a new session * incarnation, freed when the peer incarnation or the routing session ends. */ export class RecoveryStreams { constructor() { this.streams = new Map(); /** Per peer incarnation, each routed admission and how many of its streams route it (a mirror routes several). */ this.routed = new Map(); } get size() { return this.streams.size; } get(caller) { return this.streams.get(keyOf(caller)); } /** Another ordinary hold joins the installed cut without advancing its generation. */ join(route) { const stream = this.streams.get(keyOf(route)); if (stream) this.addRoute(stream, route.admissionId); } /** Whether any stream of the peer incarnation routes the admission (#5274), without walking its sessions. */ routes(socketEpoch, admissionId) { return this.routed.get(socketEpoch)?.has(admissionId) === true; } /** The bounded description a caller may hold: undefined once its peer incarnation or its session ended. */ describe(caller) { const stream = this.get(caller); if (!stream) return undefined; const { sessionEpoch, generation } = stream; return { sessionEpoch, generation, routes: [...stream.routes] }; } /** * Admits the route's token as the stream's next generation at `sessionEpoch`; a new session incarnation replaces * the record (carry 37). RULING-99 supplies the session-owned generation; returns the previous current token. */ next(route, sessionEpoch, tokenId, generation) { const key = keyOf(route); const existing = this.streams.get(key); if (existing && existing.sessionEpoch !== sessionEpoch) this.drop(key, existing); const stream = existing?.sessionEpoch === sessionEpoch ? existing : { socketEpoch: route.socketEpoch, sessionId: route.sessionId, sessionEpoch, generation, routes: new Set(), current: undefined, }; this.streams.set(key, stream); stream.generation = generation; this.addRoute(stream, route.admissionId); const retired = existing?.current; stream.current = tokenId; return retired === undefined ? {} : { retired }; } forgetPeer(socketEpoch) { for (const [key, stream] of this.streams) if (stream.socketEpoch === socketEpoch) this.drop(key, stream); } /** The routing session ended for good: its record goes on every peer (a sealed handle is never re-attached). */ forgetSession(sessionId) { for (const [key, stream] of this.streams) if (stream.sessionId === sessionId) this.drop(key, stream); } /** An admission ended (a detach today; S14 extends this): its route leaves the stream, the counter stays. */ retireRoute(caller, admissionId) { const stream = this.streams.get(keyOf(caller)); if (stream) this.removeRoute(stream, admissionId); } addRoute(stream, admissionId) { if (stream.routes.has(admissionId)) return; stream.routes.add(admissionId); const routed = this.routed.get(stream.socketEpoch) ?? new Map(); routed.set(admissionId, (routed.get(admissionId) ?? 0) + 1); this.routed.set(stream.socketEpoch, routed); } removeRoute(stream, admissionId) { if (!stream.routes.delete(admissionId)) return; const routed = this.routed.get(stream.socketEpoch); const remaining = (routed?.get(admissionId) ?? 1) - 1; if (remaining > 0) routed?.set(admissionId, remaining); else routed?.delete(admissionId); if (routed?.size === 0) this.routed.delete(stream.socketEpoch); } drop(key, stream) { for (const admissionId of [...stream.routes]) this.removeRoute(stream, admissionId); this.streams.delete(key); } } //# sourceMappingURL=compact-recovery-streams.js.map