UNPKG

@code-yeongyu/senpi

Version:

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

132 lines • 5.79 kB
import { RecoveryRepairController } from "../../../vendor/pi-client/index.js"; import { z } from "zod"; import { RpcCommandError } from "./rpc-command-error.js"; const routeSchema = z.object({ sessionId: z.string(), admissionId: z.string(), generation: z.number().int().nonnegative(), token: z.string().optional(), }); const answerSchema = routeSchema.extend({ data: z.object({ recovery: z.object({ token: z.string(), componentRef: z.string() }) }), }); /** Sent admission IDs this client remembers, least recently sent dropped first, so the memory stays bounded. */ const SENT_ADMISSION_IDS = 4096; /** Pre-send admission routes and event-driven repair. No prompt, mutable-root fallback, timer or retry loop. */ export class RpcClientAdmissions { constructor(send) { this.routes = new Map(); /** * Admission IDs this client instance sent, kept across reconnects (RULING-125): a re-send is a C4.4 retry and * carries `retry: true`, so a new socket that does not know the ID answers admission_unknown instead of opening. */ this.sent = new Set(); this.serial = 0; this.send = send; } clear() { this.routes.clear(); } /** Record a send of `admissionId` as the most recent one; true when this client sent it before. */ remember(admissionId) { const retry = this.sent.delete(admissionId); this.sent.add(admissionId); for (const oldest of this.sent) { if (this.sent.size <= SENT_ADMISSION_IDS) break; this.sent.delete(oldest); } return retry; } receive(input) { if (this.routes.size === 0) return; const parsed = routeSchema.safeParse(input); if (!parsed.success) return; const frame = parsed.data; const route = this.routes.get(frame.admissionId); if (!route || frame.generation < route.generation) return; route.sessionId = frame.sessionId; route.generation = frame.generation; if (frame.token !== undefined) route.token = frame.token; } accept(response) { if (!response.success) throw new RpcCommandError(response.error, response.errorCode, response.errorData); const parsed = answerSchema.parse(response); this.receive({ ...parsed, token: parsed.data.recovery.token }); const current = this.routes.get(parsed.admissionId); return { sessionId: current?.sessionId ?? parsed.sessionId, admissionId: parsed.admissionId, generation: current?.generation ?? parsed.generation, recovery: { ...parsed.data.recovery, token: current?.token ?? parsed.data.recovery.token }, }; } async open(options) { const existing = this.routes.has(options.admissionId); if (!existing) this.routes.set(options.admissionId, { repair: new RecoveryRepairController(), generation: 0, notBefore: 0 }); const retry = this.remember(options.admissionId); try { const opened = this.accept(await this.send({ ...options, type: "open_session", ...(retry ? { retry: true } : {}) })); // open returns routing identity, not page content. page retains wait/backoff state; // a catalog restart replaces that identity before the caller can use it. const manifest = await this.page(options.admissionId, "manifest"); return manifest.action === "restart_from_catalog" ? manifest.identity : opened; } catch (cause) { if (!existing) this.routes.delete(options.admissionId); throw cause; } } async page(admissionId, componentRef, cursor) { const route = this.routes.get(admissionId); if (!route?.token || !route.sessionId) throw new RpcCommandError("unknown compact admission", "unknown_admission", { code: "unknown_admission", admissionId, }); const now = Date.now(); if (now < route.notBefore) return { action: "wait", retryAfterMs: route.notBefore - now }; const token = route.token; const response = await this.send({ type: "get_recovery_page", sessionId: route.sessionId, admissionId, token, componentRef, requestId: `page-${++this.serial}`, ...(cursor === undefined ? {} : { cursor }), }); const parsed = z .object({ repair: z.unknown(), reason: z.string().optional() }) .safeParse(response.success ? ("data" in response ? response.data : undefined) : response.errorData); if (!parsed.success) { if (!response.success) throw new RpcCommandError(response.error, response.errorCode, response.errorData); throw parsed.error; } const body = parsed.data; const received = Date.now(); const action = route.repair.observe(body.repair, received, { token, reason: body.reason ?? "" }); switch (action.action) { case "ready": return { action: "ready", response }; case "wait": route.notBefore = received + action.retryAfterMs; return action; case "restart_from_catalog": { const reset = await this.send({ type: "reset_stream", sessionId: route.sessionId, admissionId }); const identity = this.accept(reset); return { action: "restart_from_catalog", response: reset, identity }; } } } } //# sourceMappingURL=rpc-client-admissions.js.map