UNPKG

@code-yeongyu/senpi

Version:

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

166 lines • 6.27 kB
import { processBootAtMs } from "./process-identity.js"; import { loadedPty, loadPty } from "./pty.lazy.js"; import { DEFAULT_MAX_SESSIONS } from "./shared.js"; /** * Owns the live terminal sessions for one agent session. Delegates id allocation, * capacity caps, LRU-exited pruning, and tree-kill teardown to the pi-pty * {@link SessionRegistry}, while keeping the richer {@link TerminalRuntimeSession} * wrappers (screen model + output buffer) in lock-step. */ export class TerminalManager { constructor(options = {}) { this.runtimes = new Map(); /** Stable "mon_" identities bound to runtime session ids; survives PTY exit, then drops on session prune or teardown. */ this.monitorIds = new Map(); this.exited = new Set(); this.reservations = 0; this.maxSessions = options.maxSessions ?? DEFAULT_MAX_SESSIONS; this.scrollback = options.scrollback; if (options.idScope !== undefined) this.idScope = options.idScope; } get size() { return this.runtimes.size; } get activeSize() { this.reconcileRuntimes(); return this.runtimes.size - this.exited.size + this.reservations; } reserve() { this.reconcileRuntimes(); if (this.runtimes.size - this.exited.size + this.reservations >= this.maxSessions) return null; this.reservations += 1; let released = false; return () => { if (released) return; released = true; this.reservations -= 1; }; } /** Spawn a new terminal session and register it under an allocated `bash_N` id. */ async create(command, options) { const bootAtMs = await processBootAtMs(); const pty = await loadPty(); const release = this.reserve(); if (!release) throw new pty.SessionRegistryCapacityError(this.maxSessions); this.registry ??= new pty.SessionRegistry({ maxSessions: this.maxSessions, ...(this.idScope === undefined ? {} : { idScope: this.idScope }), }); const registry = this.registry; const runtimeOptions = { ...options, scrollback: this.scrollback }; const { TerminalRuntimeSession } = await import("./runtime-session.js"); const runtime = new TerminalRuntimeSession(command, runtimeOptions, bootAtMs); let entry; try { entry = await registry.create({ command, session: runtime.session }); } catch (error) { release(); runtime.dispose(); runtime.session.kill(); throw error; } this.runtimes.set(entry.id, runtime); runtime.session.onExit(() => { this.exited.add(entry.id); }); release(); this.reconcileRuntimes(); return { id: entry.id, runtime }; } /** Look up a live-or-exited session, refreshing its LRU timestamp. */ get(id) { const registry = this.registry; if (!registry) return null; const entry = registry.get(id); if (!entry) return null; return this.runtimes.get(id) ?? null; } /** Bind a stable "mon_" id to its runtime session id; a later restore re-binds the same id. */ bindMonitorId(monitorId, sessionId) { this.monitorIds.set(monitorId, sessionId); } /** * Resolve a "mon_" monitor id to its current runtime "bash_N" (or "watch_N"), passing a * runtime id through unchanged; undefined when neither resolves. Bindings intentionally * outlive PTY exit for a final output read, then drop when the session is pruned or torn down. */ monitorIdOf(runtimeId) { for (const [monitorId, sessionId] of this.monitorIds) if (sessionId === runtimeId) return monitorId; return undefined; } resolveId(idOrMonitorId) { if (idOrMonitorId.startsWith("mon_")) return this.monitorIds.get(idOrMonitorId); const registry = this.registry; if (!registry) return undefined; return registry.get(idOrMonitorId) ? idOrMonitorId : undefined; } list() { const registry = this.registry; if (!registry) return []; const result = []; for (const entry of registry.list()) { const runtime = this.runtimes.get(entry.id); if (runtime) result.push({ id: entry.id, runtime }); } return result; } /** Tree-kill one session; the exited entry is kept for a final output read until swept. */ async stop(id) { const registry = this.registry; if (!registry) return false; const stopped = await registry.stop(id); this.reconcileRuntimes(); return stopped; } /** Tree-kill every session and dispose all runtime wrappers. */ async teardown() { const registry = this.registry; if (registry) await registry.teardown(); this.registry = undefined; for (const runtime of this.runtimes.values()) runtime.dispose(); this.runtimes.clear(); this.exited.clear(); this.monitorIds.clear(); } /** Dispose runtime wrappers whose registry entry was pruned (capacity/LRU eviction). */ reconcileRuntimes() { const registry = this.registry; if (!registry) return; const liveIds = new Set(registry.list().map((entry) => entry.id)); for (const [id, runtime] of this.runtimes) { if (liveIds.has(id)) continue; runtime.dispose(); this.runtimes.delete(id); this.exited.delete(id); for (const [monitorId, sessionId] of this.monitorIds) { if (sessionId === id) this.monitorIds.delete(monitorId); } } } } export function isCapacityError(error) { const pty = loadedPty(); if (pty) return error instanceof pty.SessionRegistryCapacityError; return error instanceof Error && error.name === "SessionRegistryCapacityError"; } //# sourceMappingURL=manager.js.map