UNPKG

@code-yeongyu/senpi

Version:

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

101 lines • 3.89 kB
import { isObservingRequest, observingHoldChange, requestOriginHostInstance } from "./host-observe-request.js"; const MAX_CLASSIFIED_LINE_BYTES = 64 * 1024; const NEWLINE = 0x0a; export class ClientOccupancy { constructor(onAttach, ownInstance) { this.clients = new Set(); this.observers = new Set(); this.attached = new Set(); this.held = new Set(); this.dependents = new Set(); this.closed = false; this.onAttach = onAttach; this.ownInstance = ownInstance; } /** Occupancy: attached clients plus observers holding the host. */ get attachedCount() { return this.attached.size + this.held.size; } /** * Occupancy when the host reported `sessionHolders` (its attached connections that still occupy it): held observers * plus at most that many attached clients. Unreported, every attached client counts. */ occupancy(sessionHolders) { const attached = sessionHolders === undefined ? this.attached.size : Math.min(this.attached.size, sessionHolders); return attached + this.held.size; } isAttached(client) { return this.attached.has(client); } get unclassifiedCount() { return this.clients.size - this.observers.size - this.attached.size - this.dependents.size; } admit(client) { if (this.closed) { client.destroy(); return false; } this.clients.add(client); let pending = Buffer.alloc(0); const attach = () => { client.off("data", classify); pending = Buffer.alloc(0); this.observers.delete(client); this.held.delete(client); this.attached.add(client); this.onAttach(); }; const depend = () => { client.off("data", classify); pending = Buffer.alloc(0); this.observers.delete(client); this.held.delete(client); this.dependents.add(client); }; const classify = (chunk) => { pending = Buffer.concat([pending, chunk]); for (let newline = pending.indexOf(NEWLINE); newline !== -1; newline = pending.indexOf(NEWLINE)) { const line = pending.subarray(0, newline).toString("utf8"); pending = pending.subarray(newline + 1); if (!isObservingRequest(line)) { if (this.ownInstance !== undefined && requestOriginHostInstance(line) === this.ownInstance) depend(); else attach(); return; } this.observers.add(client); const hold = observingHoldChange(line); if (hold === true && !this.held.has(client)) { this.held.add(client); this.onAttach(); } else if (hold === false) this.held.delete(client); } if (pending.length > MAX_CLASSIFIED_LINE_BYTES) attach(); }; client.on("data", classify); return true; } /** Returns whether this peer left without ever proving it was an observing read. */ release(client) { const unclassified = this.clients.has(client) && !this.observers.has(client) && !this.attached.has(client) && !this.dependents.has(client); this.clients.delete(client); this.observers.delete(client); this.attached.delete(client); this.held.delete(client); this.dependents.delete(client); return unclassified; } destroyAll() { this.closed = true; for (const client of this.clients) client.destroy(); } } //# sourceMappingURL=host-client-occupancy.js.map