UNPKG

@code-yeongyu/senpi

Version:

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

83 lines • 3.28 kB
/** * The session activity contract every occupancy decision shares. * * "Busy" means work the session OWNS that must outlive an idle-eviction * decision - not merely a streaming turn. There are exactly two registration * points for a new activity source: * * - in-session work reports a field on `SessionActivitySnapshot` (agent run, * bash, compaction, the session-work barrier); * - work owned by an extension publishes a `wake_source_state` event with a * non-zero `activeCount` (background terminal jobs, terminal monitors, * loop-guard holds), which `WakeSourceTracker` folds into the same predicate. * * Callers (the shared RPC host's occupancy sweep, and anything else deciding * whether a session may be torn down) MUST consult `isSessionBusySnapshot()` / * `AgentSession.isSessionBusy` instead of testing individual flags, so a source * added later cannot be silently missed by one call site. */ import { normalizeWaitItems, wakeSourceItems } from "./lifecycle-wait-items.js"; /** Live counts of extension-published wake sources, keyed by source name. */ export class WakeSourceTracker { constructor() { this.counts = new Map(); } /** * Folds one `wake_source_state` payload in. Unknown shapes are ignored: a * malformed publisher must never flip a session to "idle" by accident. */ observe(data) { if (typeof data !== "object" || data === null) return; if (!("source" in data) || !("activeCount" in data)) return; const { source, activeCount } = data; if (typeof source !== "string" || source.length === 0) return; if (typeof activeCount !== "number" || !Number.isFinite(activeCount)) return; if (activeCount > 0) this.counts.set(source, { activeCount, kind: "kind" in data && data.kind === "children" ? "children" : "background", ...normalizeWaitItems(wakeSourceItems(data)), }); else this.counts.delete(source); } /** Whether any published source currently reports live work. */ get hasActive() { return this.counts.size > 0; } /** Sources reporting live work, for diagnostics. */ get activeSources() { return [...this.counts.keys()]; } /** Live obligation counts; publishers without a kind remain background work. */ get activeCounts() { let children = 0; let background = 0; for (const record of this.counts.values()) { switch (record.kind) { case "children": children += record.activeCount; break; case "background": background += record.activeCount; break; default: record.kind; } } return { children, background }; } } /** The complete session-owned activity predicate. */ export function isSessionBusySnapshot(snapshot) { return (snapshot.isStreaming || snapshot.isBashRunning || snapshot.isCompacting || snapshot.hasSessionWork || snapshot.hasActiveWakeSource); } //# sourceMappingURL=session-activity.js.map