openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
115 lines (114 loc) • 6.82 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { j as parseAgentSessionKey } from "./session-key-CqAyZeVk.mjs";
import { a as normalizeOptionalAgentRuntimeId } from "./agent-runtime-id-D8s1CaYB.mjs";
//#region src/sessions/agent-harness-session-key.ts
const AGENT_HARNESS_SESSION_KEY_PREFIX = "harness:";
const AGENT_HARNESS_SESSION_KEY_RESERVED_MESSAGE = "Session key namespace is reserved for agent harness-owned sessions.";
const AGENT_HARNESS_SESSION_ID_LOCKED_MESSAGE = "Agent harness-owned session identity is locked and cannot be replaced or shared.";
const AGENT_HARNESS_MODEL_RUN_FORBIDDEN_MESSAGE = "Agent harness-owned sessions cannot be used for one-shot model runs.";
const MODEL_SELECTION_LOCK_REMOVAL_MESSAGE = "Model-selection-locked sessions cannot be removed, unlocked, or reassigned.";
function resolveAgentHarnessSessionKeyRest(e) {
const t = e.trim().toLowerCase();
return parseAgentSessionKey(t)?.rest ?? t;
}
function resolveAgentHarnessSessionKeyOwner(e) {
const t = resolveAgentHarnessSessionKeyRest(e);
if (!t.startsWith(AGENT_HARNESS_SESSION_KEY_PREFIX)) return;
const n = t.slice(8).split(":", 1)[0];
return normalizeOptionalAgentRuntimeId(n);
}
/** Agent harnesses own this namespace; public session APIs must not create rows in it. */
function isAgentHarnessSessionKey(e) {
return resolveAgentHarnessSessionKeyRest(e).startsWith(AGENT_HARNESS_SESSION_KEY_PREFIX);
}
function resolveMissingAgentHarnessSessionError(e, t) {
return t === void 0 && isAgentHarnessSessionKey(e) ? AGENT_HARNESS_SESSION_KEY_RESERVED_MESSAGE : void 0;
}
/** Missing reserved keys fail closed; pre-feature unlocked collisions stay ordinary. */
function resolveAgentHarnessSessionContextError(e, t) {
if (!isAgentHarnessSessionKey(e)) return;
return t ? resolveAgentHarnessSessionStoreEntryError(e, t) : AGENT_HARNESS_SESSION_KEY_RESERVED_MESSAGE;
}
/** Trusted creation must bind the namespace owner to the persisted harness owner. */
function isAgentHarnessSessionKeyOwnedBy(e, t) {
const n = normalizeOptionalAgentRuntimeId(t);
return Boolean(n && n === resolveAgentHarnessSessionKeyOwner(e));
}
/** A trusted plugin owner outranks the harness observed during its last turn. */
function resolveSessionPinnedHarnessId(e) {
return e?.modelSelectionLocked === true && !normalizeOptionalString(e.pluginOwnerId) ? normalizeOptionalAgentRuntimeId(e.agentHarnessId) : void 0;
}
function sessionLockOwnerMatches(e, t) {
const n = normalizeOptionalString(e.pluginOwnerId);
const r = normalizeOptionalString(t.pluginOwnerId);
if (n || r) return n === r;
return normalizeOptionalString(e.agentHarnessId)?.toLowerCase() === normalizeOptionalString(t.agentHarnessId)?.toLowerCase();
}
function hasEquivalentRelocatedLockedEntry(e) {
if (isAgentHarnessSessionKey(e.previousKey)) return false;
const t = normalizeOptionalString(e.previousEntry.sessionId);
if (!t) return false;
return Object.entries(e.store).some(([n, r]) => n !== e.previousKey && r.modelSelectionLocked === true && r.sessionId === t && sessionLockOwnerMatches(e.previousEntry, r));
}
/** Preserves durable harness ownership across whole-store compatibility projections. */
function resolveAgentHarnessSessionStoreTransitionError(e) {
for (const [t, n] of e.before ?? []) {
const r = e.store[t];
if (r?.modelSelectionLocked === true && sessionLockOwnerMatches(n, r)) {
if (r.sessionId !== n.sessionId) return AGENT_HARNESS_SESSION_ID_LOCKED_MESSAGE;
continue;
}
if (r === void 0 && hasEquivalentRelocatedLockedEntry({
previousKey: t,
previousEntry: n,
store: e.store
})) continue;
return MODEL_SELECTION_LOCK_REMOVAL_MESSAGE;
}
}
/** True when a reserved-looking row carries the durable harness lock added with this feature. */
function isAgentHarnessSessionStoreEntryProtected(e, t) {
return isAgentHarnessSessionKey(e) && t.modelSelectionLocked === true;
}
/** Validates durable harness locks and prevents transcript identity aliases. */
function resolveAgentHarnessSessionStoreError(e) {
const t = /* @__PURE__ */ new Map();
for (const [n, r] of Object.entries(e)) {
const e = resolveAgentHarnessSessionStoreEntryError(n, r);
if (e) return e;
if (!isValidAgentHarnessSessionStoreEntry(n, r)) continue;
const i = normalizeOptionalString(r.sessionId);
if (!i || t.has(i)) return AGENT_HARNESS_SESSION_ID_LOCKED_MESSAGE;
t.set(i, n);
}
for (const [n, r] of Object.entries(e)) {
const e = normalizeOptionalString(r.sessionId);
const i = e ? t.get(e) : void 0;
if (i && i !== n) return AGENT_HARNESS_SESSION_ID_LOCKED_MESSAGE;
}
}
/** Rejects caller-selected transcript identities that would rotate a durable harness lock. */
function resolveAgentHarnessSessionIdMismatchError(e, t) {
if (!e || !resolveSessionPinnedHarnessId(e)) return;
const n = normalizeOptionalString(t);
if (!n) return;
return n === normalizeOptionalString(e.sessionId) ? void 0 : AGENT_HARNESS_SESSION_ID_LOCKED_MESSAGE;
}
/** Locked rows require durable identity; reserved rows must also match the key owner. */
function resolveAgentHarnessSessionStoreEntryError(e, t) {
if (t.modelSelectionLocked !== true) return;
const n = isAgentHarnessSessionKey(e);
if (normalizeOptionalString(t.pluginOwnerId)) return n ? AGENT_HARNESS_SESSION_KEY_RESERVED_MESSAGE : void 0;
const r = normalizeOptionalString(t.agentHarnessId)?.toLowerCase();
const i = Boolean(r) && r === normalizeOptionalAgentRuntimeId(r);
if (!normalizeOptionalString(t.sessionId) && (n || t.agentHarnessId !== void 0)) return AGENT_HARNESS_SESSION_ID_LOCKED_MESSAGE;
if (n) return i && isAgentHarnessSessionKeyOwnedBy(e, t.agentHarnessId) ? void 0 : AGENT_HARNESS_SESSION_KEY_RESERVED_MESSAGE;
if (t.agentHarnessId === void 0) return;
if (!i) return AGENT_HARNESS_SESSION_ID_LOCKED_MESSAGE;
}
/** True for any valid durable harness lock, including supported ordinary-key rows. */
function isValidAgentHarnessSessionStoreEntry(e, t) {
return t.modelSelectionLocked === true && (isAgentHarnessSessionKey(e) || resolveSessionPinnedHarnessId(t) !== void 0) && resolveAgentHarnessSessionStoreEntryError(e, t) === void 0;
}
//#endregion
export { isAgentHarnessSessionKey as a, isValidAgentHarnessSessionStoreEntry as c, resolveAgentHarnessSessionStoreEntryError as d, resolveAgentHarnessSessionStoreError as f, resolveSessionPinnedHarnessId as h, MODEL_SELECTION_LOCK_REMOVAL_MESSAGE as i, resolveAgentHarnessSessionContextError as l, resolveMissingAgentHarnessSessionError as m, AGENT_HARNESS_SESSION_ID_LOCKED_MESSAGE as n, isAgentHarnessSessionKeyOwnedBy as o, resolveAgentHarnessSessionStoreTransitionError as p, AGENT_HARNESS_SESSION_KEY_RESERVED_MESSAGE as r, isAgentHarnessSessionStoreEntryProtected as s, AGENT_HARNESS_MODEL_RUN_FORBIDDEN_MESSAGE as t, resolveAgentHarnessSessionIdMismatchError as u };