UNPKG

@code-yeongyu/senpi

Version:

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

65 lines • 2.31 kB
import { SESSION_WORKER_LIMITS } from "./session-worker-protocol.js"; const SECRET_FRAME_TYPES = ["extension_ui_request", "secret_input_settled", "secret_sealed_offer"]; function secretFrameType(record) { const type = "type" in record ? record.type : undefined; return SECRET_FRAME_TYPES.find((known) => known === type); } export function isSealedOffer(record) { if (!("type" in record) || record.type !== "secret_sealed_offer") return false; const offer = record; return (typeof offer.hpkePk === "string" && /^[A-Za-z0-9_-]{43}$/.test(offer.hpkePk) && typeof offer.title === "string" && (offer.placeholder === undefined || typeof offer.placeholder === "string") && Number.isSafeInteger(offer.deadlineAtMs)); } /** * The secret ids one worker holds on the host, and the frames each may still send (senpi#5192). * Secret frames travel on the control exchange, outside the payload frame bound, so every count and size is bounded here. */ export class WorkerSecretIds { constructor() { this.sent = new Map(); } has(id) { return this.sent.has(id); } ids() { return this.sent.keys(); } admitsNew() { if (this.sent.size >= SESSION_WORKER_LIMITS.secretIds) return false; let open = 0; for (const sent of this.sent.values()) if (!sent.has("secret_input_settled")) open++; return open < SESSION_WORKER_LIMITS.secretInputs; } add(id) { if (!this.sent.has(id)) this.sent.set(id, new Set()); } /** * A live id's request, settled record and sealed offer (D11 secret phase 5.5), each at most once and within * `secretFrameBytes`; a refusal spends nothing. */ admitFrame(id, record) { const sent = this.sent.get(id); const type = secretFrameType(record); if (!sent || type === undefined || sent.has(type)) return false; if (Buffer.byteLength(JSON.stringify(record)) > SESSION_WORKER_LIMITS.secretFrameBytes) return false; sent.add(type); return true; } forget(id) { this.sent.delete(id); } clear() { this.sent.clear(); } } //# sourceMappingURL=session-worker-secrets.js.map