UNPKG

@code-yeongyu/senpi

Version:

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

63 lines • 2.28 kB
import { HOST_OCCUPANCY_RECORD } from "./host-observe-request.js"; export class HostOccupancyWatch { constructor(ports) { this.watchers = new Set(); this.inFlight = new Map(); /** Connections that held a session here at least once: only these stop counting once they hold nothing. */ this.heldOnce = new Set(); this.ports = ports; } get watching() { return this.watchers.size > 0; } watch(connection) { this.watchers.add(connection); this.reported = this.holders(); this.ports.send(connection, { type: HOST_OCCUPANCY_RECORD, holders: this.reported }); } /** A command from `connection` started; it holds the host until `end`. */ begin(connection) { this.inFlight.set(connection, (this.inFlight.get(connection) ?? 0) + 1); this.refresh(); } end(connection) { if (this.ports.holdsSessions(connection)) this.heldOnce.add(connection); const left = (this.inFlight.get(connection) ?? 0) - 1; if (left > 0) this.inFlight.set(connection, left); else this.inFlight.delete(connection); this.refresh(); } forget(connection) { this.watchers.delete(connection); this.inFlight.delete(connection); this.heldOnce.delete(connection); this.refresh(); } refresh() { if (this.watchers.size === 0) return; const holders = this.holders(); if (holders === this.reported) return; this.reported = holders; for (const watcher of this.watchers) this.ports.send(watcher, { type: HOST_OCCUPANCY_RECORD, holders }); } holders() { let holders = 0; for (const connection of this.ports.connections()) { if (this.watchers.has(connection) || !this.ports.attached(connection)) continue; const holding = this.ports.holdsSessions(connection); if (holding) this.heldOnce.add(connection); if (holding || this.inFlight.has(connection) || !this.heldOnce.has(connection)) holders++; } return holders; } } //# sourceMappingURL=host-occupancy-watch.js.map