UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

160 lines (159 loc) • 5.77 kB
import { n as sanitizeForLog } from "./ansi-CZa2t0Hf.mjs"; import { l as resolveLogicalTurnContextEngines, r as hasSameContextEngineInstance, u as disposeContextEngineSources } from "./registry-BNPXTWdk.mjs"; import { a as evaluateContextEngineHostSupport, o as supportsContextEngineDurableTurnAdvancement } from "./host-compat-D2mUtPDd.mjs"; import { n as recordAgentCleanupFailure, r as runAgentCleanupStep } from "./run-cleanup-timeout-Bk3vN1Oh.mjs"; import { t as registerLegacyContextEngine } from "./init-CNW5HSik.mjs"; //#region src/agents/harness/context-engine-logical-turn.ts async function createContextEngineLogicalTurnLease(e) { const { runId: t, sessionId: n } = e.identity; const resolve = () => resolveLogicalTurnContextEngines(e.config, { initialize: registerLegacyContextEngine, agentDir: e.agentDir, workspaceDir: e.workspaceDir, beforeCustomEngine: e.awaitSessionDiffBaseline, onCleanupFailure: recordAgentCleanupFailure }); let r = await resolve(); let i = "unselected"; let a = r.configured; let o = r.configuredFailure; let s = false; const c = /* @__PURE__ */ new Set(); const l = /* @__PURE__ */ new Map(); const u = r.configuredFailure === void 0 && r.configured.registeredId === r.fallback.registeredId; const asEffective = () => ({ ...a, mode: o ? "legacy-degraded" : "configured", ...o ? { reason: o } : {} }); const warnOnce = (e) => { if (s) return; s = true; console.warn(`[context-engine] Context engine "${sanitizeForLog(r.configuredId)}" degraded to "${sanitizeForLog(r.fallback.registeredId)}" for this logical turn: ${sanitizeForLog(e)}. The "${sanitizeForLog(r.fallback.registeredId)}" engine will handle only this turn; configuration is unchanged, and "${sanitizeForLog(r.configuredId)}" will be retried next turn.`); }; const degradeBeforeStart = (e) => { if (i === "disposed") throw new Error("context-engine logical turn selection is already pinned"); if (u) { if (i === "unselected") i = "selected"; return asEffective(); } if (i === "started") throw new Error("context-engine logical turn selection is already pinned"); o ??= e; a = r.fallback; i = "selected"; warnOnce(o); return asEffective(); }; const resolveSelectionIssue = (e) => { const t = evaluateContextEngineHostSupport({ contextEngineInfo: a.engine.info, operation: e.operation, host: e.host }); if (!t.ok) return `host "${e.host.id}" is missing ${t.missingCapabilities.join(", ")}`; if (u) return; if (e.requiresDurableCommit && a.engine.info.transcriptSemantics?.currentTurnFence !== "before-current-turn-entry-v1") return "current-turn transcript fencing is not declared"; if (e.requiresDurableCommit && !supportsContextEngineDurableTurnAdvancement(a.engine)) return "atomic idempotent turn advancement is not declared"; }; if (r.configuredFailure) degradeBeforeStart(r.configuredFailure); const disposeEngines = async (e) => { const r = [e.configured.engine, e.fallback.engine]; const i = r.filter((e, t) => r.slice(0, t).every((t) => !hasSameContextEngineInstance(e, t))); await Promise.allSettled(i.map((i) => runAgentCleanupStep({ runId: t, sessionId: n, step: "context-engine-dispose", log: { warn: console.warn }, cleanup: async () => { const t = new Set(r.filter((e) => hasSameContextEngineInstance(i, e)).flatMap((t) => e.sourceResources?.get(t) ?? [])); await disposeContextEngineSources(i, [...t]); } }))); }; return { get engine() { return a.engine; }, get effectiveEngine() { return a.engine; }, get effectiveEngineId() { return a.registeredId; }, get effectiveEnginePluginId() { return a.ownerPluginId; }, get degraded() { return o !== void 0; }, get degradedReason() { return o; }, get disposed() { return i === "disposed"; }, selectForHost(e) { if (i === "disposed") throw new Error("context-engine logical turn lease is already disposed"); if (o) return asEffective(); const t = resolveSelectionIssue(e); if (t) { if (i === "started") throw new Error(`context-engine logical turn cannot change to incompatible ${e.host.label}: ${t}`); return degradeBeforeStart(t); } if (i === "unselected") i = "selected"; return asEffective(); }, degradeBeforeStart, begin() { if (i === "disposed") throw new Error("context-engine logical turn lease is already disposed"); i = "started"; return asEffective(); }, async handOver() { if (i !== "started") return; i = "handed-over"; await disposeEngines(r); }, async rebind() { if (i !== "handed-over") return; const e = await resolve(); if (!o && e.configuredFailure) { await disposeEngines(e); throw new Error(`context engine "${r.configuredId}" is unavailable after plugin reload: ${e.configuredFailure}`); } r = e; a = o ? e.fallback : e.configured; i = "started"; }, onDispose(e, t) { if (i === "disposed") throw new Error("context-engine logical turn lease is already disposed"); l.set(e, t); }, deferDisposalUntil(e) { if (i === "disposed") throw new Error("context-engine logical turn lease is already disposed"); c.add(e); e.finally(() => c.delete(e)).catch(() => {}); }, async dispose() { if (i === "disposed") return; const e = i !== "handed-over"; i = "disposed"; await Promise.all([...l.values()].map((e) => runAgentCleanupStep({ runId: t, sessionId: n, step: "context-engine-turn-settlement", log: { warn: console.warn }, cleanup: e }))); l.clear(); if (!e) return; if (c.size > 0) { Promise.allSettled(c).then(() => disposeEngines(r)); return; } await disposeEngines(r); } }; } //#endregion export { createContextEngineLogicalTurnLease as t };