@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
97 lines • 4.2 kB
JavaScript
import { AsyncLocalStorage } from "node:async_hooks";
import { isStaleExtensionContextError } from "../goal/stale-context.js";
import { isWakeSourceStateEvent, WAKE_SOURCE_STATE_EVENT } from "../monitor-state-event.js";
import { dispatchHookEvent } from "./dispatcher.js";
import { applyStopHookResult, buildStopHookInput, createStopTurnTracker } from "./stop-adapter.js";
/**
* Background work usually clears its wake source and wakes the session in the same
* completion handler, in either order. A drained live set therefore waits this long for
* the wake turn before Stop reports the session quiet; a turn that starts first cancels it.
*/
export const STOP_DRAIN_GRACE_MS = 2_000;
/** A pending question is the user's turn, which is exactly what Stop reports. */
const USER_TURN_WAKE_SOURCE = "ask-user";
/**
* Dispatches the Stop hook when the session actually stops. A turn that ends while a
* background wake source is live (subagent task, DAG run, terminal monitor, background bash
* session, detached eval cell, loop-guard hold) has handed the session to work that wakes it
* again, so its Stop waits: the wake turn's own end reports it, or the drain does when the
* work clears without waking the session.
*/
export function registerStopLifecycle(pi, deps) {
const turnTracker = createStopTurnTracker();
const backgroundWork = new Map();
let pending;
let drainTimer;
async function dispatchStop({ ctx, input, sessionId }, signal) {
const state = deps.refreshState(ctx);
const result = await dispatchHookEvent({
cwd: ctx.cwd,
handlers: state.parsed.executableHandlers,
input,
...(signal === undefined ? {} : { signal }),
trustOptions: { platform: process.platform },
trustState: state.trust,
});
await applyStopHookResult(pi, ctx, result, { sessionId, turnKey: turnTracker.turnKey(ctx, sessionId) });
}
function clearDrainTimer() {
if (drainTimer === undefined)
return;
clearTimeout(drainTimer);
drainTimer = undefined;
}
function dropPending() {
clearDrainTimer();
pending = undefined;
}
function dispatchPendingIfQuiet() {
drainTimer = undefined;
const stop = pending;
if (stop === undefined || backgroundWork.size > 0)
return;
// A wake turn that is already running or queued reports its own Stop when it ends.
if (!stop.ctx.isIdle() || stop.ctx.hasPendingMessages())
return;
pending = undefined;
// The session is quiet, so no turn signal applies: a local idle session's `ctx.signal` is undefined too.
stop
.resume(() => dispatchStop(stop, undefined))
.catch((error) => {
if (isStaleExtensionContextError(error))
return;
stop.ctx.ui.notify(`Stop hook dispatch failed: ${error instanceof Error ? error.message : String(error)}`, "error");
});
}
pi.events.on(WAKE_SOURCE_STATE_EVENT, (data) => {
if (!isWakeSourceStateEvent(data) || data.source === USER_TURN_WAKE_SOURCE)
return;
if (data.activeCount > 0)
backgroundWork.set(data.source, data.activeCount);
else
backgroundWork.delete(data.source);
clearDrainTimer();
if (backgroundWork.size > 0 || pending === undefined)
return;
drainTimer = setTimeout(dispatchPendingIfQuiet, STOP_DRAIN_GRACE_MS);
drainTimer.unref();
});
pi.on("agent_end", async (event, ctx) => {
const stop = {
ctx,
input: buildStopHookInput(event, ctx),
sessionId: ctx.sessionManager.getSessionId(),
resume: AsyncLocalStorage.snapshot(),
};
if (backgroundWork.size > 0) {
pending = stop;
return;
}
dropPending();
await dispatchStop(stop, ctx.signal);
});
pi.on("agent_start", () => dropPending());
pi.on("session_shutdown", () => dropPending());
return { resetTurn: () => turnTracker.reset() };
}
//# sourceMappingURL=stop-lifecycle.js.map