openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
65 lines (64 loc) • 4.29 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-1VuiLaaR.mjs";
import { r as resolveStateDir } from "./state-dir-CYkBUd_G.mjs";
import "./paths-BP0DYpE8.mjs";
import { t as createSubsystemLogger } from "./subsystem-4NzR43uz.mjs";
import "./session-key-DY3MOX4N.mjs";
import { c as resolveSessionStorePathCore, g as resolveUnsuffixedSqliteTargetFromSessionStorePath } from "./paths-Bonddxfk.mjs";
import { t as resolveAgentSessionDirs } from "./session-dirs-CzPwoVKq.mjs";
import { _ as readAgentDatabaseAdmissionRefusal } from "./agent-database-admission-CRwW7FsI.mjs";
import { C as listConfiguredSessionStoreAgentIds } from "./openclaw-state-db-DJZ7EKoI.mjs";
import { t as isPerAgentSessionStoreConfig } from "./session-store-config-BqhvsHyj.mjs";
import { i as prepareSessionStoreTargetInventory } from "./session-store-target-inventory-C9ERDnSH.mjs";
import { t as prepareSessionStoreTargetInventoryRead } from "./session-store-target-runtime-BCIudNUA.mjs";
import "./sessions-CRUJeu5H.mjs";
import path from "node:path";
//#region src/agents/main-session-recovery/main-session-restart-recovery-shared.ts
const mainSessionRecoveryLog = createSubsystemLogger("main-session-restart-recovery");
const DEFAULT_RECOVERY_DELAY_MS = 5e3;
/** A killed row may release fences only after the store owner proves there is no live work. */
function isKilledMainSessionRecoveryFenceOnly(entry) {
const state = entry.mainRestartRecovery;
return Boolean(entry.status === "killed" && entry.restartRecoveryRuns?.length && !entry.pendingFinalDelivery && !entry.restartRecoveryDeliveryRunId && !entry.restartRecoveryHarnessCompletion && !state?.reservation && !state?.foregroundClaims && !state?.tombstone);
}
function resolveRestartRecoveryTerminalClientRunId(entry) {
return entry.restartRecoverySourceIngress === "control-ui" || entry.restartRecoverySourceIngress === "internal" ? normalizeOptionalString(entry.restartRecoveryDeliverySourceRunId) : void 0;
}
async function discoverRestartRecoveryStoreTargets(params) {
if (params.shouldContinue?.() === false) return [];
const storeTargets = [];
const stateDir = params.stateDir ?? resolveStateDir(process.env);
const env = {
...process.env,
OPENCLAW_STATE_DIR: stateDir
};
if (params.cfg) {
const configuredAgentIds = listConfiguredSessionStoreAgentIds(params.cfg);
const selectedAgentIds = configuredAgentIds.filter((agentId) => !params.agentIds || params.agentIds.has(agentId));
const configuredStorePaths = new Set(configuredAgentIds.map((agentId) => path.resolve(resolveSessionStorePathCore(params.cfg?.session?.store, {
agentId,
env
}))));
const configuredAgentIdSet = new Set(configuredAgentIds);
const targets = await prepareSessionStoreTargetInventoryRead(prepareSessionStoreTargetInventory(params.cfg, isPerAgentSessionStoreConfig(params.cfg.session?.store) ? selectedAgentIds : [.../* @__PURE__ */ new Set([...configuredAgentIds, ...params.agentIds ?? []])], env, "recovery")).withRead(async (snapshot) => snapshot.agents.flatMap(({ result }) => result.available ? result.targets : []));
if (params.shouldContinue?.() === false) return [];
for (const target of targets) {
const storePath = path.resolve(target.storePath);
if (!configuredAgentIdSet.has(target.agentId) && !configuredStorePaths.has(storePath)) continue;
if (params.agentIds && !params.agentIds.has(target.agentId)) continue;
storeTargets.push({
...target,
storePath
});
}
} else for (const sessionsDir of await resolveAgentSessionDirs(stateDir)) {
const storePath = path.join(sessionsDir, "sessions.json");
storeTargets.push({
agentId: resolveUnsuffixedSqliteTargetFromSessionStorePath(storePath).agentId ?? "main",
storePath
});
}
if (params.shouldContinue?.() === false) return [];
return storeTargets.filter((target) => (params.cfg !== void 0 || !params.agentIds || params.agentIds.has(target.agentId)) && !readAgentDatabaseAdmissionRefusal(target.agentId, { env })).toSorted((a, b) => a.storePath.localeCompare(b.storePath) || a.agentId.localeCompare(b.agentId));
}
//#endregion
export { resolveRestartRecoveryTerminalClientRunId as a, mainSessionRecoveryLog as i, discoverRestartRecoveryStoreTargets as n, isKilledMainSessionRecoveryFenceOnly as r, DEFAULT_RECOVERY_DELAY_MS as t };