@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
209 lines • 9.69 kB
JavaScript
/** The terminal extension's per-generation state, the bundle event sinks it binds, and the tool context over it. */
import { getShellEnv } from "../../../../utils/shell.js";
import { TERMINAL_BACKGROUND_AGE_EVENT, TERMINAL_MONITOR_ENDED_EVENT, TERMINAL_MONITOR_STATE_EVENT, WAKE_SOURCE_STATE_EVENT, } from "../monitor-state-event.js";
import { withTerminalContext } from "./context-lifetime.js";
import { tapBackgroundSession, untapBackgroundSession } from "./job-widget-tap.js";
import { removeMonitorStateDir, terminalStateDir } from "./monitor-state-dir.js";
import { describeBackgroundAge } from "./notify.js";
import { TerminalSessionBundle } from "./session-bundle.js";
/** Tests and SDK callers may hand partial contexts without a session manager. */
export function sessionKeyOf(ctx) {
return ctx?.sessionManager?.getSessionId?.();
}
export function createBundle(state) {
return new TerminalSessionBundle({
maxSessions: state.settings.maxSessions,
scrollback: state.settings.scrollback,
});
}
/** Sinks read the live instance state at call time, so notifier swaps need no re-bind. */
export function bundleSinks(pi, state) {
const retire = () => {
state.monitorNotifier?.dispose();
state.monitorNotifier = null;
state.notifier = null;
state.statusTicker.stop();
state.keeper?.stop();
};
return {
onMonitorEvent: (event) => state.monitorNotifier?.notifyEvent(event),
onMonitorEnded: (event) => {
pi.events?.emit(TERMINAL_MONITOR_ENDED_EVENT, event);
pi.rpc?.emit(TERMINAL_MONITOR_ENDED_EVENT, event);
// A watch that ended in this process (its command exited, it was killed or expired) never
// runs again, so its restore baseline goes. Disposal is a shutdown or reload: the watch is
// suspended, not ended, and a later restore still needs the dir.
if (event.reason === "disposed")
return;
const dir = withTerminalContext(() => terminalStateDir(state.ctx), retire);
const monitorId = state.bundle?.manager.monitorIdOf(event.id);
if (dir === undefined || monitorId === undefined)
return;
removeMonitorStateDir(dir, monitorId).catch((error) => {
withTerminalContext(() => state.ctx?.ui?.notify?.(`Could not remove the state dir of ${monitorId}: ${error instanceof Error ? error.message : String(error)}`, "warning"), retire);
});
},
onMonitorState: (snapshot, transition = true) => {
if (!state.parked)
state.statusTicker.sync(snapshot);
const payload = {
activeCount: snapshot.length,
monitors: snapshot.map((entry) => ({
id: entry.id,
description: entry.description,
paused: entry.paused,
startedAtMs: entry.startedAtMs,
command: entry.command,
filter: entry.filter,
persistent: entry.persistent,
deadlineMs: entry.deadlineMs,
fireCount: entry.fireCount,
lastFiredAtMs: entry.lastFiredAtMs,
})),
};
pi.events?.emit(TERMINAL_MONITOR_STATE_EVENT, payload);
pi.rpc?.emit(TERMINAL_MONITOR_STATE_EVENT, payload);
if (!transition)
return;
pi.events?.emit(WAKE_SOURCE_STATE_EVENT, {
source: "terminal-monitors",
activeCount: snapshot.length,
// The stable "mon_" id: runtime ids churn on restore, and item ids feed the custody signature.
monitors: snapshot.map((entry) => ({
id: entry.monitorId ?? entry.id,
description: entry.description,
startedAtMs: entry.startedAtMs,
})),
});
if (state.manifestWriter)
void state.manifestWriter.observeMonitorState(snapshot);
},
onBackgroundState: (snapshot) => {
pi.events?.emit(WAKE_SOURCE_STATE_EVENT, {
source: "terminal-background-sessions",
activeCount: snapshot.length,
items: snapshot,
});
const record = () => {
const writer = state.manifestWriter;
if (!writer)
return;
for (const entry of snapshot) {
if (state.recordedBackgroundIds.has(entry.id))
continue;
state.recordedBackgroundIds.add(entry.id);
const identity = state.bundle?.manager.get(entry.id)?.identity();
void writer.recordBackgroundStart(entry.id, entry.description ?? entry.id, entry.startedAtMs, identity);
}
};
if (state.manifestWriter === null && state.ensurePersistence !== null && snapshot.length > 0) {
void state.ensurePersistence().then(record);
return;
}
record();
},
onBackgroundAge: (event) => {
pi.events?.emit(TERMINAL_BACKGROUND_AGE_EVENT, event);
pi.rpc?.emit(TERMINAL_BACKGROUND_AGE_EVENT, event);
withTerminalContext(() => state.ctx?.ui?.notify?.(describeBackgroundAge(event), event.kind === "running" ? "info" : "warning"), retire);
// A reap reaches the model through the session's completion notice, which names the reap.
if (event.kind !== "reaped")
state.notifier?.notifyAge(event);
},
onBackgroundExit: (id, runtime) => {
state.recordedBackgroundIds.delete(id);
if (state.manifestWriter)
void state.manifestWriter.recordBackgroundExit(id);
state.notifier?.notifyCompletion(id, runtime);
},
};
}
/** A background session belongs to the turn that started it, so interrupting that turn with `background: "stop"` stops it. */
function recordRunJob(state, id, description) {
const bundle = state.bundle;
if (bundle === null)
return;
const release = withTerminalContext(() => state.ctx?.registerRunBackgroundJob?.({
kind: "bash",
id,
description,
stop: (turnId) => bundle.stopForInterrupt(id, turnId),
}), () => { });
if (release !== undefined)
bundle.trackRunJob(id, release);
}
export function buildToolContext(pi, state) {
const requireBundle = () => {
// Lazily create a bundle so the tools work even when invoked directly (e.g. via the
// SDK) before `session_start` initializes one. `session_start` replaces it with a
// settings-configured bundle and tears down any earlier one.
if (!state.bundle) {
state.bundle = createBundle(state);
state.bundle.monitors.setParked(state.parked);
state.bundle.bind(bundleSinks(pi, state));
}
return state.bundle;
};
return {
get manager() {
return requireBundle().manager;
},
get cwd() {
return state.ctx?.cwd ?? process.cwd();
},
get shellPath() {
return state.shellPath;
},
get redactSecretsInOutput() {
return state.settings.redactSecretsInOutput;
},
get defaultCols() {
return state.settings.defaultCols;
},
get defaultRows() {
return state.settings.defaultRows;
},
get timeoutAction() {
return state.settings.timeoutAction;
},
get maxDurableMonitors() {
return state.settings.maxDurableMonitors;
},
get backgroundAutoReapMinutes() {
return state.settings.backgroundAutoReapMinutes;
},
get monitorRegistry() {
return requireBundle().monitors;
},
getEnv: () => getShellEnv(),
getSessionContext: () => state.ctx,
// Exit listeners registered before a reload reach the post-reload owner through the
// shared bundle, so this must dispatch via the bundle, never the instance notifier.
onBackgroundStart: (id, description, startedAtMs, command, autoReapMinutes) => {
state.bundle?.notifyBackgroundStart(id, description, startedAtMs, {
...(command === undefined ? {} : { command }),
...(autoReapMinutes === undefined ? {} : { autoReapMinutes }),
});
recordRunJob(state, id, description);
const runtime = state.bundle?.manager.get(id);
if (runtime) {
const key = withTerminalContext(() => sessionKeyOf(state.ctx), () => { });
tapBackgroundSession(key, id, runtime, {
command: command ?? description,
description,
});
}
},
onSessionInput: (id) => untapBackgroundSession(state.bundle?.manager.get(id)),
onBackgroundExit: (id, runtime) => {
state.bundle?.notifyBackgroundExit(id, runtime);
},
onBackgroundReapFailed: (id, autoReapMinutes, reason) => {
state.bundle?.notifyBackgroundReapFailed(id, autoReapMinutes, reason);
},
onMonitorRearmed: (id) => state.monitorNotifier?.rearm(id),
onMonitorsResumed: (ids) => state.monitorNotifier?.resume(ids),
ensurePersistence: () => state.ensurePersistence?.() ?? Promise.resolve(),
};
}
//# sourceMappingURL=extension-state.js.map