UNPKG

@code-yeongyu/senpi

Version:

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

50 lines • 2.26 kB
import { isHandoffBusy } from "./handoff-activity.js"; import { sessionDirectoryRemoved } from "./session-path-key.js"; /** * One handoff-drain pass's decisions. Every session is judged on its own: a session whose activity * cannot be read cannot be proven busy, so it parks like any other (reported through `unreadable`) * and never strands the rest on a superseded generation. The pass adds no path resolution of its * own: the gone check only stats the directory of the path the registry already holds. */ export function selectDrainVerdicts(registry, inFlight, unreadable) { const park = []; const gone = []; for (const { sessionId, status, sessionPath } of registry.list()) { if (status !== "open" || inFlight(sessionId)) continue; const entry = registry.peek(sessionId); if (!entry || handoffBusy(entry, sessionId, unreadable)) continue; if (sessionPath !== undefined && sessionDirectoryRemoved(sessionPath)) gone.push(sessionId); else park.push(sessionId); } return { park, gone }; } function handoffBusy(entry, sessionId, unreadable) { try { // Both signals are optional: a runtime that does not publish an activity snapshot cannot be // proven busy, and a park that waits for a signal the runtime never emits would strand the // session on the old generation. const snapshot = entry.runtime?.session.activitySnapshot; return entry.worker?.handoffBusy === true || (snapshot !== undefined && isHandoffBusy(snapshot)); } catch (cause) { unreadable(sessionId, cause); return false; } } /** One drain pass for the host: logs what it skips, and `undefined` when the pass failed as a whole. */ export function readDrainVerdicts(registry, inFlight) { try { return selectDrainVerdicts(registry, inFlight, (sessionId, cause) => { process.stderr.write(`senpi rpc handoff activity of ${sessionId} unreadable, parking: ${String(cause)}\n`); }); } catch (cause) { process.stderr.write(`senpi rpc handoff drain pass failed, retrying: ${String(cause)}\n`); return undefined; } } //# sourceMappingURL=session-drain.js.map