openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
789 lines (788 loc) • 30.6 kB
JavaScript
import { c as normalizeOptionalString } from "./string-coerce-DW4mBlAt.js";
import "./agent-scope-CESPhp3L.js";
import { a as isSubagentSessionKey, c as parseAgentSessionKey } from "./session-key-utils-C7uT9A4s.js";
import { u as normalizeAgentId } from "./session-key-BsbwBJwv.js";
import { c as resolveDefaultAgentId, n as listAgentIds, o as resolveAgentWorkspaceDir } from "./agent-scope-config-ChfGvhEr.js";
import { r as logVerbose } from "./globals-Cjirhr4c.js";
import { i as getRuntimeConfig } from "./io-CwmOK6NP.js";
import { s as getActivePluginRegistry } from "./runtime-Bn9lQ3kL.js";
import { t as getGlobalHookRunner } from "./hook-runner-global-Bm5WihiA.js";
import { m as triggerInternalHook, n as createInternalHookEvent } from "./internal-hooks-eGNEjFJ2.js";
import { S as snapshotSessionOrigin, j as resolveSessionStoreKey } from "./store-CWhnH-Ye.js";
import { a as resolveSessionFilePathOptions, i as resolveSessionFilePath } from "./paths-BcGVZo_k.js";
import { f as canonicalizeAbsoluteSessionFilePath, p as rewriteSessionFileForNewSessionId } from "./types-CoDcFuoc.js";
import { t as extractGeneratedTranscriptSessionId } from "./generated-transcript-session-id-C-WpjhM8.js";
import { C as resetSessionEntryLifecycle } from "./session-accessor-ArmkJJUW.js";
import { t as loadCombinedSessionStoreForGateway } from "./combined-store-gateway-BU9syWDf.js";
import "./sessions-BoPlPkQh.js";
import { n as runPluginHostCleanup } from "./host-hook-cleanup-BePkVQ98.js";
import { t as getAcpSessionManager } from "./manager-oWiLWowS.js";
import { t as getAcpRuntimeBackend } from "./registry-DQ1RGwsj.js";
import { o as upsertAcpSessionMeta, r as readAcpSessionMeta, s as writeAcpSessionMetaForMigration } from "./session-meta-CKXLc--x.js";
import { S as waitForEmbeddedAgentRunEnd, n as abortEmbeddedAgentRun } from "./runs-pVtbFfgt.js";
import { n as archiveSessionTranscriptsDetailed, o as resolveStableSessionEndTranscript } from "./session-transcript-files.fs-BzKAix0F.js";
import { o as readSessionMessagesAsync } from "./session-transcript-readers-BwGy3Gk4.js";
import { b as resolveSessionModelRef, g as resolveGatewaySessionStoreTarget, u as loadSessionEntry } from "./session-utils-B4hOl7Zv.js";
import { n as getSessionBindingService } from "./session-binding-service-Dz0wDjnb.js";
import { u as retireSessionMcpRuntime } from "./agent-bundle-mcp-runtime-BcscePMD.js";
import "./agent-bundle-mcp-tools-CAlLtgQ_.js";
import { t as clearBootstrapSnapshot } from "./bootstrap-cache-CXFUJZDk.js";
import { t as clearAllCliSessions } from "./cli-session-SXvjcghv.js";
import { mn as errorShape, pn as ErrorCodes } from "./schema-BrDi0lZs.js";
import "./src-BNueln22.js";
import "./embedded-agent-BgF2MOkH.js";
import { i as stopSubagentsForRequester } from "./abort-DSOUCxgu.js";
import { a as buildSessionStartHookPayload, i as buildSessionEndHookPayload, n as listActiveSessionsForShutdown, r as noteActiveSessionForShutdown, t as forgetActiveSessionForShutdown } from "./active-sessions-shutdown-tracker-B0Sb_aoa.js";
import { n as clearSessionResetRuntimeState, t as resolveResetPreservedSelection } from "./reset-preserved-selection-BxQNCvdr.js";
import { t as cleanupBrowserSessionsForLifecycleEnd } from "./browser-lifecycle-cleanup-BrLC2D45.js";
import path from "node:path";
import { randomUUID } from "node:crypto";
//#region src/gateway/session-child-sessions.ts
/** Returns true when a session store row is a direct child of the parent key. */
function isDirectChildSessionEntry(params) {
const parentKey = normalizeOptionalString(params.parentKey);
if (!parentKey || params.sessionKey === parentKey || !params.entry) return false;
return normalizeOptionalString(params.entry.spawnedBy) === parentKey || normalizeOptionalString(params.entry.parentSessionKey) === parentKey;
}
/** Finds direct child sessions for a parent session across the combined gateway store. */
function findDirectChildSessionsForParent(params) {
const { store } = loadCombinedSessionStoreForGateway(params.cfg);
return Object.entries(store).filter(([sessionKey, entry]) => isDirectChildSessionEntry({
sessionKey,
entry,
parentKey: params.parentKey
})).map(([sessionKey, entry]) => ({
sessionKey,
entry
}));
}
//#endregion
//#region src/gateway/session-reset-service.ts
const ACP_RUNTIME_CLEANUP_TIMEOUT_MS = 15e3;
function resolveResetSessionFile(params) {
const currentEntry = params.currentEntry;
const rotationPreviousSessionId = extractGeneratedTranscriptSessionId(currentEntry?.sessionFile) ?? currentEntry?.sessionId;
const rewrittenSessionFile = rotationPreviousSessionId ? rewriteSessionFileForNewSessionId({
sessionFile: currentEntry?.sessionFile,
previousSessionId: rotationPreviousSessionId,
nextSessionId: params.nextSessionId
}) : void 0;
const preservedSessionFile = (rewrittenSessionFile && path.isAbsolute(rewrittenSessionFile) ? canonicalizeAbsoluteSessionFilePath(rewrittenSessionFile) : rewrittenSessionFile) ?? currentEntry?.sessionFile;
return resolveSessionFilePath(params.nextSessionId, preservedSessionFile ? { sessionFile: preservedSessionFile } : void 0, resolveSessionFilePathOptions({
storePath: params.storePath,
agentId: params.agentId
}));
}
function stripRuntimeModelState(entry) {
if (!entry) return entry;
return {
...entry,
model: void 0,
modelProvider: void 0,
contextTokens: void 0,
contextBudgetStatus: void 0,
systemPromptReport: void 0
};
}
function archiveSessionTranscriptsForSessionDetailed(params) {
if (!params.sessionId) return [];
return archiveSessionTranscriptsDetailed({
sessionId: params.sessionId,
storePath: params.storePath,
sessionFile: params.sessionFile,
agentId: params.agentId,
reason: params.reason,
onArchiveError: params.onArchiveError
});
}
function emitGatewaySessionEndPluginHook(params) {
if (!params.sessionId) return;
forgetActiveSessionForShutdown(params.sessionId);
const hookRunner = getGlobalHookRunner();
if (!hookRunner?.hasHooks("session_end")) return;
const transcript = resolveStableSessionEndTranscript({
sessionId: params.sessionId,
storePath: params.storePath,
sessionFile: params.sessionFile,
agentId: params.agentId,
archivedTranscripts: params.archivedTranscripts
});
const payload = buildSessionEndHookPayload({
sessionId: params.sessionId,
sessionKey: params.sessionKey,
cfg: params.cfg,
reason: params.reason,
sessionFile: transcript.sessionFile,
transcriptArchived: transcript.transcriptArchived,
nextSessionId: params.nextSessionId,
nextSessionKey: params.nextSessionKey
});
hookRunner.runSessionEnd(payload.event, payload.context).catch((err) => {
logVerbose(`session_end hook failed: ${String(err)}`);
});
}
function emitGatewaySessionStartPluginHook(params) {
if (!params.sessionId) return;
if (params.storePath) noteActiveSessionForShutdown({
cfg: params.cfg,
sessionKey: params.sessionKey,
sessionId: params.sessionId,
storePath: params.storePath,
sessionFile: params.sessionFile,
agentId: params.agentId
});
const hookRunner = getGlobalHookRunner();
if (!hookRunner?.hasHooks("session_start")) return;
const payload = buildSessionStartHookPayload({
sessionId: params.sessionId,
sessionKey: params.sessionKey,
cfg: params.cfg,
resumedFrom: params.resumedFrom
});
hookRunner.runSessionStart(payload.event, payload.context).catch((err) => {
logVerbose(`session_start hook failed: ${String(err)}`);
});
}
const SHUTDOWN_DRAIN_DEFAULT_TOTAL_TIMEOUT_MS = 2e3;
/**
* Emit a typed `session_end` for every session that received `session_start`
* but did not yet receive a paired `session_end`. The bounded total timeout
* mirrors the gateway lifecycle hook timeout so a slow plugin cannot block
* SIGTERM/SIGINT past the runtime's overall shutdown grace window.
*
* Sessions that have already been finalized through replace / reset / delete /
* compaction are forgotten from the tracker by `emitGatewaySessionEndPluginHook`
* before this drain runs, so they will not be double-fired here.
*/
async function drainActiveSessionsForShutdown(params) {
const tracked = listActiveSessionsForShutdown();
if (tracked.length === 0) return {
emittedSessionIds: [],
timedOut: false
};
const totalTimeoutMs = Math.max(100, Math.floor(params.totalTimeoutMs ?? SHUTDOWN_DRAIN_DEFAULT_TOTAL_TIMEOUT_MS));
const emittedSessionIds = [];
const hookRunner = getGlobalHookRunner();
let settledEmissions = 0;
const drain = Promise.allSettled(tracked.map(async (entry) => {
try {
forgetActiveSessionForShutdown(entry.sessionId);
emittedSessionIds.push(entry.sessionId);
if (!hookRunner?.hasHooks("session_end")) return;
const transcript = resolveStableSessionEndTranscript({
sessionId: entry.sessionId,
storePath: entry.storePath,
sessionFile: entry.sessionFile,
agentId: entry.agentId
});
const payload = buildSessionEndHookPayload({
sessionId: entry.sessionId,
sessionKey: entry.sessionKey,
cfg: entry.cfg,
reason: params.reason,
sessionFile: transcript.sessionFile,
transcriptArchived: transcript.transcriptArchived
});
await hookRunner.runSessionEnd(payload.event, payload.context);
} catch (err) {
logVerbose(`session_end hook failed during shutdown drain: ${String(err)}`);
} finally {
settledEmissions++;
}
}));
let timer;
const timeout = new Promise((resolve) => {
timer = setTimeout(() => resolve("timeout"), totalTimeoutMs);
timer.unref?.();
});
try {
if (await Promise.race([drain.then(() => "ok"), timeout]) === "timeout") {
logVerbose(`shutdown session-end drain timed out after ${totalTimeoutMs}ms with ${tracked.length - settledEmissions} session_end handler(s) still pending`);
return {
emittedSessionIds,
timedOut: true
};
}
return {
emittedSessionIds,
timedOut: false
};
} finally {
if (timer) clearTimeout(timer);
}
}
async function emitSessionUnboundLifecycleEvent(params) {
const targetKind = isSubagentSessionKey(params.targetSessionKey) ? "subagent" : "acp";
await getSessionBindingService().unbind({
targetSessionKey: params.targetSessionKey,
reason: params.reason
});
if (params.emitHooks === false) return;
const hookRunner = getGlobalHookRunner();
if (!hookRunner?.hasHooks("subagent_ended")) return;
await hookRunner.runSubagentEnded({
targetSessionKey: params.targetSessionKey,
targetKind,
reason: params.reason,
sendFarewell: true,
outcome: params.reason === "session-reset" ? "reset" : "deleted"
}, { childSessionKey: params.targetSessionKey });
}
async function ensureSessionRuntimeCleanup(params) {
const closeTrackedBrowserTabs = async () => {
params.assertCurrent?.();
const closeKeys = new Set([
params.key,
params.target.canonicalKey,
...params.target.storeKeys,
params.sessionId ?? ""
]);
await cleanupBrowserSessionsForLifecycleEnd({
cfg: params.cfg,
sessionKeys: [...closeKeys],
onWarn: (message) => logVerbose(message)
});
params.assertCurrent?.();
};
params.assertCurrent?.();
const queueKeys = new Set(params.target.storeKeys);
queueKeys.add(params.target.canonicalKey);
if (params.sessionId) queueKeys.add(params.sessionId);
clearSessionResetRuntimeState([...queueKeys]);
stopSubagentsForRequester({
cfg: params.cfg,
requesterSessionKey: params.target.canonicalKey
});
if (!params.sessionId) {
params.assertCurrent?.();
clearBootstrapSnapshot(params.target.canonicalKey);
await closeTrackedBrowserTabs();
return;
}
params.assertCurrent?.();
abortEmbeddedAgentRun(params.sessionId);
const ended = await waitForEmbeddedAgentRunEnd(params.sessionId, 15e3);
params.assertCurrent?.();
clearBootstrapSnapshot(params.target.canonicalKey);
if (ended) {
params.assertCurrent?.();
await retireSessionMcpRuntime({
sessionId: params.sessionId,
reason: "gateway-session-cleanup",
onError: (error, sessionId) => {
logVerbose(`sessions cleanup: failed to dispose bundle MCP runtime for ${sessionId}: ${String(error)}`);
}
});
params.assertCurrent?.();
await closeTrackedBrowserTabs();
return;
}
return errorShape(ErrorCodes.UNAVAILABLE, `Session ${params.key} is still active; try again in a moment.`);
}
async function runAcpCleanupStep(params) {
let timer;
const timeoutPromise = new Promise((resolve) => {
timer = setTimeout(() => resolve({ status: "timeout" }), ACP_RUNTIME_CLEANUP_TIMEOUT_MS);
});
const opPromise = params.op().then(() => ({ status: "ok" })).catch((error) => ({
status: "error",
error
}));
const outcome = await Promise.race([opPromise, timeoutPromise]);
if (timer) clearTimeout(timer);
return outcome;
}
async function closeAcpRuntimeForSession(params) {
if (params.shouldCleanup && !params.shouldCleanup()) return;
params.assertCurrent?.();
const sessionKeys = Array.from(new Set([params.sessionKey, ...params.fallbackSessionKeys ?? []].map((key) => typeof key === "string" ? key.trim() : "").filter(Boolean)));
let acpMeta;
let acpSessionKey = params.sessionKey;
for (const sessionKey of sessionKeys) {
acpMeta = readAcpSessionMeta({ sessionKey });
if (acpMeta) {
acpSessionKey = sessionKey;
break;
}
}
if (!acpMeta) return;
const acpManager = getAcpSessionManager();
if (params.shouldCleanup && !params.shouldCleanup()) return;
params.assertCurrent?.();
const cancelOutcome = await runAcpCleanupStep({ op: async () => {
await acpManager.cancelSession({
cfg: params.cfg,
sessionKey: acpSessionKey,
reason: params.reason
});
} });
if (params.shouldCleanup && !params.shouldCleanup()) return;
params.assertCurrent?.();
if (cancelOutcome.status === "timeout") return errorShape(ErrorCodes.UNAVAILABLE, `Session ${params.sessionKey} is still active; try again in a moment.`);
if (cancelOutcome.status === "error") logVerbose(`sessions.${params.reason}: ACP cancel failed for ${params.sessionKey}: ${String(cancelOutcome.error)}`);
if (params.shouldCleanup && !params.shouldCleanup()) return;
params.assertCurrent?.();
const closeOutcome = await runAcpCleanupStep({ op: async () => {
await acpManager.closeSession({
cfg: params.cfg,
sessionKey: acpSessionKey,
reason: params.reason,
discardPersistentState: true,
requireAcpSession: false,
allowBackendUnavailable: true
});
} });
if (params.shouldCleanup && !params.shouldCleanup()) return;
params.assertCurrent?.();
if (closeOutcome.status === "timeout") return errorShape(ErrorCodes.UNAVAILABLE, `Session ${params.sessionKey} is still active; try again in a moment.`);
if (closeOutcome.status === "error") logVerbose(`sessions.${params.reason}: ACP runtime close failed for ${params.sessionKey}: ${String(closeOutcome.error)}`);
if (params.reason === "session-delete") {
params.assertCurrent?.();
await upsertAcpSessionMeta({
cfg: params.cfg,
sessionKey: acpSessionKey,
mutate: () => null
});
params.assertCurrent?.();
} else if (params.deferResetState) params.onDeferredResetState?.({
sessionKey: acpSessionKey,
meta: acpMeta
});
else {
const resetMeta = await ensureFreshAcpResetState({
cfg: params.cfg,
sessionKey: acpSessionKey,
reason: params.reason,
acpMeta,
assertCurrent: params.assertCurrent,
shouldApply: params.shouldCleanup
});
if (resetMeta) params.onResetMeta?.({
sessionKey: acpSessionKey,
meta: resetMeta
});
}
}
function buildPendingAcpMeta(base, now) {
const currentIdentity = base.identity;
const nextIdentity = currentIdentity ? {
state: "pending",
...currentIdentity.acpxRecordId ? { acpxRecordId: currentIdentity.acpxRecordId } : {},
source: currentIdentity.source,
lastUpdatedAt: now
} : void 0;
return {
backend: base.backend,
agent: base.agent,
runtimeSessionName: base.runtimeSessionName,
...nextIdentity ? { identity: nextIdentity } : {},
mode: base.mode,
...base.runtimeOptions ? { runtimeOptions: base.runtimeOptions } : {},
...base.cwd ? { cwd: base.cwd } : {},
state: "idle",
lastActivityAt: now
};
}
async function ensureFreshAcpResetState(params) {
if (params.reason !== "session-reset") return;
const latestMeta = readAcpSessionMeta({ sessionKey: params.sessionKey }) ?? params.acpMeta;
if (!latestMeta?.identity || latestMeta.identity.state !== "resolved" || !latestMeta.identity.acpxSessionId && !latestMeta.identity.agentSessionId) return;
const backendId = (latestMeta.backend || params.cfg.acp?.backend || "").trim() || void 0;
if (params.shouldApply && !params.shouldApply()) return;
try {
params.assertCurrent?.();
await getAcpRuntimeBackend(backendId)?.runtime.prepareFreshSession?.({ sessionKey: params.sessionKey });
if (params.shouldApply && !params.shouldApply()) return;
params.assertCurrent?.();
} catch (error) {
params.assertCurrent?.();
logVerbose(`sessions.${params.reason}: ACP prepareFreshSession failed for ${params.sessionKey}: ${String(error)}`);
}
const now = Date.now();
let resetMeta;
if (params.shouldApply && !params.shouldApply()) return;
params.assertCurrent?.();
await upsertAcpSessionMeta({
cfg: params.cfg,
sessionKey: params.sessionKey,
mutate: (current) => {
if (params.shouldApply && !params.shouldApply()) return current;
resetMeta = buildPendingAcpMeta(current ?? latestMeta, now);
return resetMeta;
}
});
params.assertCurrent?.();
return resetMeta;
}
async function closeChildAcpRuntimesForParent(params) {
let children;
try {
if (params.shouldCleanup && !params.shouldCleanup()) return;
params.assertCurrent?.();
children = findDirectChildSessionsForParent({
cfg: params.cfg,
parentKey: params.parentKey
}).flatMap(({ sessionKey }) => {
return readAcpSessionMeta({ sessionKey }) ? [{ sessionKey }] : [];
});
} catch (error) {
logVerbose(`sessions.${params.reason}: failed to enumerate sessions for child ACP cleanup: ${String(error)}`);
return;
}
if (params.shouldCleanup && !params.shouldCleanup()) return;
params.assertCurrent?.();
await Promise.allSettled(children.map(({ sessionKey }) => closeAcpRuntimeForSession({
cfg: params.cfg,
sessionKey,
reason: params.reason,
assertCurrent: params.assertCurrent,
shouldCleanup: params.shouldCleanup
}).then((childError) => {
if (childError) logVerbose(`sessions.${params.reason}: child ACP cleanup incomplete for ${sessionKey}`);
})));
if (params.shouldCleanup && !params.shouldCleanup()) return;
params.assertCurrent?.();
}
async function cleanupSessionBeforeMutation(params) {
const cleanupError = await ensureSessionRuntimeCleanup({
cfg: params.cfg,
key: params.key,
target: params.target,
sessionId: params.entry?.sessionId,
assertCurrent: params.assertCurrent
});
if (cleanupError) return cleanupError;
const pluginCleanup = await runPluginHostCleanup({
cfg: params.cfg,
registry: getActivePluginRegistry(),
reason: params.reason === "session-reset" ? "reset" : "delete",
sessionKey: params.target.canonicalKey ?? params.key,
shouldCleanup: () => {
params.assertCurrent?.();
return true;
}
});
params.assertCurrent?.();
for (const failure of pluginCleanup.failures) logVerbose(`plugin host cleanup failed for ${failure.pluginId}/${failure.hookId}: ${String(failure.error)}`);
const parentSessionKey = params.target.canonicalKey ?? params.canonicalKey ?? params.key;
const parentAcpError = await closeAcpRuntimeForSession({
cfg: params.cfg,
sessionKey: parentSessionKey,
fallbackSessionKeys: [
params.canonicalKey,
params.legacyKey,
params.key
],
reason: params.reason,
onResetMeta: params.onAcpResetMeta,
assertCurrent: params.assertCurrent
});
params.assertCurrent?.();
await closeChildAcpRuntimesForParent({
cfg: params.cfg,
parentKey: params.target.canonicalKey ?? params.canonicalKey ?? params.key,
reason: params.reason,
assertCurrent: params.assertCurrent
});
params.assertCurrent?.();
return parentAcpError;
}
async function emitGatewayBeforeResetPluginHook(params) {
const hookRunner = getGlobalHookRunner();
if (!hookRunner?.hasHooks("before_reset")) return;
const sessionKey = params.target.canonicalKey ?? params.key;
const sessionId = params.entry?.sessionId;
const sessionFile = params.entry?.sessionFile;
const agentId = normalizeAgentId(params.target.agentId ?? resolveDefaultAgentId(params.cfg));
const workspaceDir = resolveAgentWorkspaceDir(params.cfg, agentId);
let messages = [];
try {
if (typeof sessionId === "string" && sessionId.trim().length > 0) messages = await readSessionMessagesAsync({
agentId,
sessionEntry: params.entry,
sessionId,
sessionKey,
storePath: params.storePath
}, {
mode: "full",
reason: "before_reset hook payload"
});
} catch (err) {
logVerbose(`before_reset: failed to read session messages for ${sessionId ?? "(none)"}; firing hook with empty messages (${String(err)})`);
}
hookRunner.runBeforeReset({
sessionFile,
messages,
reason: params.reason
}, {
agentId,
sessionKey,
sessionId,
workspaceDir
}).catch((err) => {
logVerbose(`before_reset hook failed: ${String(err)}`);
});
}
async function performGatewaySessionReset(params) {
const resetTarget = (() => {
const cfg = getRuntimeConfig();
const explicitAgentId = params.agentId ? normalizeAgentId(params.agentId) : void 0;
const parsedKey = parseAgentSessionKey(params.key);
const inferredGlobalAgentId = !explicitAgentId && parsedKey && resolveSessionStoreKey({
cfg,
sessionKey: params.key
}) === "global" ? normalizeAgentId(parsedKey.agentId) : void 0;
const requestedAgentId = explicitAgentId ?? inferredGlobalAgentId;
if (requestedAgentId && !listAgentIds(cfg).includes(requestedAgentId)) return {
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, `Unknown agent id: ${requestedAgentId}`)
};
if (explicitAgentId && parsedKey?.agentId && normalizeAgentId(parsedKey.agentId) !== explicitAgentId) return {
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, "session key agent does not match agentId")
};
const target = resolveGatewaySessionStoreTarget({
cfg,
key: params.key,
...requestedAgentId ? { agentId: requestedAgentId } : {}
});
return {
ok: true,
cfg,
target,
storePath: target.storePath,
requestedAgentId
};
})();
if (!resetTarget.ok) return resetTarget;
const { cfg, target, storePath, requestedAgentId } = resetTarget;
const { entry, legacyKey, canonicalKey } = loadSessionEntry(params.key, requestedAgentId ? { agentId: requestedAgentId } : void 0);
const hadExistingEntry = Boolean(entry);
const workspaceDir = resolveAgentWorkspaceDir(cfg, normalizeAgentId(target.agentId ?? resolveDefaultAgentId(cfg)));
const resetPluginRegistry = getActivePluginRegistry();
const isResetLifecycleCurrent = () => {
try {
params.assertCurrent?.();
return true;
} catch {
return false;
}
};
let deferredAcpResetState;
const hookEvent = createInternalHookEvent("command", params.reason, target.canonicalKey ?? params.key, {
sessionEntry: entry,
previousSessionEntry: entry,
commandSource: params.commandSource,
cfg,
workspaceDir
});
params.assertCurrent?.();
await triggerInternalHook(hookEvent);
params.assertCurrent?.();
const runtimeCleanupError = await ensureSessionRuntimeCleanup({
cfg,
key: params.key,
target,
sessionId: entry?.sessionId
});
if (runtimeCleanupError) return {
ok: false,
error: runtimeCleanupError
};
const parentAcpError = await closeAcpRuntimeForSession({
cfg,
sessionKey: target.canonicalKey ?? canonicalKey ?? params.key,
fallbackSessionKeys: [
canonicalKey,
legacyKey,
params.key
],
reason: "session-reset",
deferResetState: true,
onDeferredResetState: (state) => {
deferredAcpResetState = state;
}
});
if (parentAcpError) return {
ok: false,
error: parentAcpError
};
const pluginCleanup = await runPluginHostCleanup({
cfg,
registry: resetPluginRegistry,
reason: "reset",
sessionKey: target.canonicalKey ?? params.key,
skipPersistentSessionState: true
});
for (const failure of pluginCleanup.failures) logVerbose(`plugin host cleanup failed for ${failure.pluginId}/${failure.hookId}: ${String(failure.error)}`);
await closeChildAcpRuntimesForParent({
cfg,
parentKey: target.canonicalKey ?? canonicalKey ?? params.key,
reason: "session-reset"
});
const lifecycle = await resetSessionEntryLifecycle({
agentId: target.agentId,
storePath,
target: {
canonicalKey: target.canonicalKey,
storeKeys: target.storeKeys
},
buildNextEntry: ({ currentEntry, primaryKey }) => {
if (!isResetLifecycleCurrent() && currentEntry?.sessionId !== entry?.sessionId) params.assertCurrent?.();
const sessionAgentId = normalizeAgentId(parseAgentSessionKey(primaryKey)?.agentId ?? target.agentId ?? requestedAgentId ?? resolveDefaultAgentId(cfg));
const resetPreservedSelection = resolveResetPreservedSelection({ entry: currentEntry });
const resetEntry = {
...stripRuntimeModelState(currentEntry),
providerOverride: void 0,
modelOverride: void 0,
modelOverrideSource: void 0,
authProfileOverride: void 0,
authProfileOverrideSource: void 0,
authProfileOverrideCompactionCount: void 0,
...resetPreservedSelection
};
const resolvedModel = resolveSessionModelRef(cfg, resetEntry, sessionAgentId);
const now = Date.now();
const nextSessionId = randomUUID();
const nextEntry = {
sessionId: nextSessionId,
sessionFile: resolveResetSessionFile({
nextSessionId,
currentEntry,
storePath,
agentId: sessionAgentId
}),
updatedAt: now,
systemSent: false,
abortedLastRun: false,
thinkingLevel: currentEntry?.thinkingLevel,
fastMode: currentEntry?.fastMode,
verboseLevel: currentEntry?.verboseLevel,
traceLevel: currentEntry?.traceLevel,
reasoningLevel: currentEntry?.reasoningLevel,
elevatedLevel: currentEntry?.elevatedLevel,
ttsAuto: currentEntry?.ttsAuto,
execHost: currentEntry?.execHost,
execSecurity: currentEntry?.execSecurity,
execAsk: currentEntry?.execAsk,
execNode: currentEntry?.execNode,
responseUsage: currentEntry?.responseUsage,
...resetPreservedSelection,
groupActivation: currentEntry?.groupActivation,
groupActivationNeedsSystemIntro: currentEntry?.groupActivationNeedsSystemIntro,
chatType: currentEntry?.chatType,
model: resolvedModel.model,
modelProvider: resolvedModel.provider,
contextTokens: resetEntry?.contextTokens,
compactionCount: currentEntry?.compactionCount,
compactionCheckpoints: currentEntry?.compactionCheckpoints,
sendPolicy: currentEntry?.sendPolicy,
queueMode: currentEntry?.queueMode,
queueDebounceMs: currentEntry?.queueDebounceMs,
queueCap: currentEntry?.queueCap,
queueDrop: currentEntry?.queueDrop,
spawnedBy: currentEntry?.spawnedBy,
spawnedWorkspaceDir: currentEntry?.spawnedWorkspaceDir,
spawnedCwd: currentEntry?.spawnedCwd,
parentSessionKey: currentEntry?.parentSessionKey,
forkedFromParent: currentEntry?.forkedFromParent,
spawnDepth: currentEntry?.spawnDepth,
subagentRole: currentEntry?.subagentRole,
subagentControlScope: currentEntry?.subagentControlScope,
label: currentEntry?.label,
displayName: currentEntry?.displayName,
channel: currentEntry?.channel,
groupId: currentEntry?.groupId,
subject: currentEntry?.subject,
groupChannel: currentEntry?.groupChannel,
space: currentEntry?.space,
origin: snapshotSessionOrigin(currentEntry),
deliveryContext: currentEntry?.deliveryContext,
cliSessionBindings: currentEntry?.cliSessionBindings,
cliSessionIds: currentEntry?.cliSessionIds,
claudeCliSessionId: currentEntry?.claudeCliSessionId,
lastChannel: currentEntry?.lastChannel,
lastTo: currentEntry?.lastTo,
lastAccountId: currentEntry?.lastAccountId,
lastThreadId: currentEntry?.lastThreadId,
inputTokens: 0,
outputTokens: 0,
totalTokens: 0,
totalTokensFresh: true
};
if (!isSubagentSessionKey(primaryKey)) clearAllCliSessions(nextEntry);
return nextEntry;
},
afterEntryMutation: async (mutation) => {
let committedAcpResetState;
if (deferredAcpResetState) {
const identity = deferredAcpResetState.meta.identity;
if (identity?.state === "resolved" && (identity.acpxSessionId || identity.agentSessionId)) {
committedAcpResetState = {
sessionKey: deferredAcpResetState.sessionKey,
meta: buildPendingAcpMeta(deferredAcpResetState.meta, Date.now())
};
writeAcpSessionMetaForMigration({
sessionKey: committedAcpResetState.sessionKey,
sessionId: mutation.nextEntry.sessionId,
meta: committedAcpResetState.meta
});
}
}
params.onCommitted?.({
key: target.canonicalKey,
sessionId: mutation.nextEntry.sessionId
});
if (committedAcpResetState && isResetLifecycleCurrent()) try {
await getAcpRuntimeBackend((committedAcpResetState.meta.backend || cfg.acp?.backend || "").trim() || void 0)?.runtime.prepareFreshSession?.({ sessionKey: committedAcpResetState.sessionKey });
} catch (error) {
logVerbose(`sessions.session-reset: ACP prepareFreshSession failed for ${committedAcpResetState.sessionKey}: ${String(error)}`);
}
await emitGatewayBeforeResetPluginHook({
cfg,
key: params.key,
target,
storePath,
entry: mutation.previousEntry,
reason: params.reason
});
}
});
const next = lifecycle.nextEntry;
const oldSessionId = lifecycle.previousSessionId;
const oldSessionFile = lifecycle.previousSessionFile;
const archivedTranscripts = lifecycle.archivedTranscripts;
emitGatewaySessionEndPluginHook({
cfg,
sessionKey: target.canonicalKey ?? params.key,
sessionId: oldSessionId,
storePath,
sessionFile: oldSessionFile,
agentId: target.agentId,
reason: params.reason,
archivedTranscripts,
nextSessionId: next.sessionId
});
emitGatewaySessionStartPluginHook({
cfg,
sessionKey: target.canonicalKey ?? params.key,
sessionId: next.sessionId,
resumedFrom: oldSessionId,
storePath,
sessionFile: next.sessionFile,
agentId: target.agentId
});
if (hadExistingEntry) await emitSessionUnboundLifecycleEvent({
targetSessionKey: target.canonicalKey ?? params.key,
reason: "session-reset"
});
return {
ok: true,
key: target.canonicalKey,
entry: next,
agentId: target.agentId,
storePath
};
}
//#endregion
export { emitGatewaySessionEndPluginHook as a, performGatewaySessionReset as c, emitGatewayBeforeResetPluginHook as i, cleanupSessionBeforeMutation as n, emitGatewaySessionStartPluginHook as o, drainActiveSessionsForShutdown as r, emitSessionUnboundLifecycleEvent as s, archiveSessionTranscriptsForSessionDetailed as t };