openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
254 lines (253 loc) • 11.1 kB
JavaScript
import { r as sqlPublicationAnchor } from "./sql-connection-Beiyg5Dt.mjs";
import { s as stageSqliteTransactionState } from "./sqlite-post-commit-BrUVQObC.mjs";
import { r as findOpenClawAgentDatabaseIdentity } from "./openclaw-agent-db-identity-C8MGU8lp.mjs";
import { o as sessionChanges } from "./session-row-changes-BA4BqVwF.mjs";
import { r as invalidateOpenClawAgentWritableProjections } from "./openclaw-agent-db-borrower-BrziJlyo.mjs";
import { r as invalidateOpenClawAgentReadOnlyProjections } from "./openclaw-agent-db-readonly-scope-B8DxQsJ2.mjs";
import { A as createSessionEntryCreationOperation, C as publishRetainedSessionEntryPredicate, D as revokePreparedSessionEntryPredicate, E as recordAcquiringSessionMember, M as readSessionEntryCreationIdentity, O as updateSessionSharingField, T as recordAcquiringSessionEntry, b as retainedSharingReads, c as publishRetainedSessionEntryChange, g as recordCommittedSessionMetadataPublication, h as recordCommittedSessionEntryPublication, j as projectSessionSharingEntry, k as assertSessionEntryCreationTarget, o as preparedSharingChanges, r as markSessionSharingAuthorityUnknown, w as publishRetainedSessionGeneration, x as stageSessionSharingPublication } from "./session-accessor.sqlite-entry-cache-publication-state-B4mkg2yK.mjs";
import { o as sealSessionEntryPublicationSource } from "./session-entry-publication-source-C48dCGLP.mjs";
import { r as sessionEntryCaches, t as publishTrackedCacheUpdate } from "./session-accessor.sqlite-entry-cache-state-ChUHgtCJ.mjs";
//#region src/config/sessions/session-accessor.sqlite-entry-cache-publication.ts
function bindPreparedSessionEntryPublication(e, t) {
preparedSharingChanges.changes.set(e, t);
}
function bindSessionEntryPublicationSource(e, t) {
const n = findOpenClawAgentDatabaseIdentity(t);
if (n) bindPreparedSessionEntryPublication(e, {
...preparedSharingChanges.changes.get(e) ?? { kind: "source" },
databaseIdentity: n.identity,
canonicalPath: n.canonicalPath
});
return e;
}
function emitPreparedSessionSharingChange(e, t, n = e.agentId, r, i = {
kind: "marker",
sharingChange: "changed"
}) {
const a = {
agentId: n,
storePath: e.path,
sessionKey: t,
...r ? {
facts: r,
scope: "session-entry"
} : { factsInvalidated: true }
};
bindPreparedSessionEntryPublication(a, i);
bindSessionEntryPublicationSource(a, e);
if (i.kind === "metadata") {
const seal = () => sealSessionEntryPublicationSource(i.prepared.source);
if (!stageSqliteTransactionState(sqlPublicationAnchor(e.db), { commit: seal })) seal();
}
sessionChanges.emit(a, e.db);
}
function invalidateSessionEntryCaches(e) {
invalidateOpenClawAgentWritableProjections(e, (e) => sessionEntryCaches.delete(e));
invalidateOpenClawAgentReadOnlyProjections(e, (e) => sessionEntryCaches.delete(e));
}
/** Retire all facts in a failed installation, including pending older worker receipts. */
function invalidateSessionEntryPublication(e, t, n = true) {
if (n) markSessionSharingAuthorityUnknown(e, t);
publishRetainedSessionEntryChange(e, t, void 0, void 0, false);
}
/** A committed metadata-only worker write invalidates caches without changing retained identity. */
function publishSessionEntryWorkerMetadataInvalidation(e) {
invalidateSessionEntryCaches(e.databaseIdentity);
for (const t of retainedSharingReads(e.databaseIdentity, e.sessionKey) ?? []) publishRetainedSessionEntryPredicate(t, e.entry, e.entry !== void 0, {
preserveMatchingEntry: true,
fullEntry: e.fullEntry
});
const t = {
agentId: e.agentId,
storePath: e.storePath,
sessionKey: e.sessionKey,
scope: "session-entry",
facts: { kind: "unchanged" }
};
bindPreparedSessionEntryPublication(t, {
kind: "marker",
sharingChange: "unchanged",
databaseIdentity: e.databaseIdentity
});
sessionChanges.emit(t);
}
function creationMatchesDatabase(e, t) {
return e.source.kind === "native" ? e.source.database.db === t.db : e.source.kind === "file" && e.source.agentId === t.agentId && findOpenClawAgentDatabaseIdentity(t)?.identity === e.source.databaseIdentity;
}
/** Canonical creation scopes provenance only; caller and target guards still authorize each write. */
async function withSessionEntryCreationPublication(e, t) {
const { ensureReplicaSubscription: n } = await import("./session-actor-replica-CCb6UtXo.mjs");
n();
const r = createSessionEntryCreationOperation();
const i = {
agentId: e.agentId,
source: e.database ? {
kind: "native",
database: e.database,
agentId: e.database.agentId
} : {
kind: "file",
...e.file
},
sessionKey: e.sessionKey,
active: true
};
preparedSharingChanges.operations.set(r, i);
try {
e.bind?.(r);
return await runWithSessionEntryCreationPublication(r, () => t(r));
} finally {
i.active = false;
preparedSharingChanges.operations.delete(r);
}
}
/** Reenter only this operation's provenance after another owner restores its own context. */
function runWithSessionEntryCreationPublication(e, t) {
const n = preparedSharingChanges.operations.get(e);
return preparedSharingChanges.current.run(n, t);
}
function assertSessionEntryCreationPublication(e, t) {
assertSessionEntryCreationTarget(preparedSharingChanges.operations.get(e), t);
}
/** Only the actual inserted-placeholder producer supplies these known row facts. */
function publishSessionEntryPlaceholderInsertion(e, t) {
const { sessionKey: n, sessionId: r } = t;
const i = Object.freeze({ sessionId: r });
const a = preparedSharingChanges.current.getStore();
const o = a?.active && creationMatchesDatabase(a, e) && a.sessionKey === n ? a : void 0;
const s = {
kind: "placeholder",
creation: o,
databaseIdentity: o ? readSessionEntryCreationIdentity(o) : e.db,
sessionKey: n,
placeholder: i,
committed: false
};
let c = false;
c = publishTrackedCacheUpdate(e, () => {
recordCommittedSessionEntryPublication(e, n);
const t = c ? {
entry: void 0,
placeholder: i,
membership: /* @__PURE__ */ new Set()
} : void 0;
for (const r of retainedSharingReads(e, n) ?? []) {
revokePreparedSessionEntryPredicate(r);
if (r.acquisition) r.acquisition.invalidated = true;
publishRetainedSessionGeneration(r, void 0, c);
r.facts = t;
}
sessionEntryCaches.delete(e.db);
s.committed = c;
}, () => stageSessionSharingPublication(e, n), () => invalidateSessionEntryPublication(e, n));
emitPreparedSessionSharingChange(e, n, e.agentId, void 0, {
kind: "placeholder",
sharingChange: "changed",
receipt: s
});
}
/** Install a committed field from either a native transaction or a worker receipt. */
function commitSessionSharingFieldChange(e, t, n) {
recordCommittedSessionMetadataPublication(e, t, n);
for (const r of retainedSharingReads(e, t) ?? []) if (r.acquisition) {
if (n.kind === "member") recordAcquiringSessionMember(r.acquisition, n);
else recordAcquiringSessionEntry(r.acquisition, void 0, void 0);
} else if (r.facts) r.facts = updateSessionSharingField(r.facts, n);
}
function publishSessionSharingFieldChange(e, t, n) {
publishTrackedCacheUpdate(e, () => {
commitSessionSharingFieldChange(e, t, n);
}, () => stageSessionSharingPublication(e, t, n.kind === "category" ? void 0 : n), () => invalidateSessionEntryPublication(e, t, n.kind !== "category"));
}
function publishSessionSharingMemberChange(e, t, n, r = e.agentId) {
publishSessionSharingFieldChange(e, t, n);
emitPreparedSessionSharingChange(e, t, r, n);
}
/** Publish sharing state before the listing projection and its public change event. */
function publishSessionSharingEntryChange(e, t) {
const n = t.facts;
if (n?.kind === "owner" || n?.kind === "category") {
publishSessionSharingFieldChange(e, t.sessionKey, n);
return;
}
const r = n?.kind === "unchanged" || n?.kind === "participants";
const i = t.entry ? projectSessionSharingEntry(t.entry) : void 0;
if (r) publishTrackedCacheUpdate(e, () => recordCommittedSessionMetadataPublication(e, t.sessionKey, n, t.entry, t.fullEntry), void 0, () => invalidateSessionEntryPublication(e, t.sessionKey, false));
const a = t.previousEntry && {
sessionId: t.previousEntry.sessionId,
lifecycleRevision: t.previousEntry.lifecycleRevision
};
if (!r) publishTrackedCacheUpdate(e, () => {
publishRetainedSessionEntryChange(e, t.sessionKey, i, a, i !== void 0 || n?.kind === "removed", t.entry, t.fullEntry);
}, () => stageSessionSharingPublication(e, t.sessionKey), () => invalidateSessionEntryPublication(e, t.sessionKey));
}
//#endregion
//#region src/sessions/communication-policy.ts
/** Resolve preferences only; existing access and live authority remain separate ceilings. */
function resolveSessionCommunicationPolicy(e) {
const t = e.config.session?.communication;
return {
send: e.entry?.communication?.send ?? t?.send ?? "always",
receive: e.entry?.communication?.receive ?? t?.receive ?? "always"
};
}
//#endregion
//#region src/sessions/communication-admission.ts
/** Policies constrain peers, not completion already owned by a host task lifecycle. */
function planSessionCommunication(e) {
const t = e.source[0];
const n = e.target[0];
const r = Boolean(t?.entry?.sessionId && n?.entry?.sessionId && t.agentId === n.agentId && t.sessionKey === n.sessionKey && t.storePath === n.storePath && t.entry.sessionId === n.entry.sessionId);
if (e.ownedTask || r) return {
allowed: true,
approvals: []
};
const i = [];
for (const [t, n] of [["send", e.source], ["receive", e.target]]) for (const r of n) {
const n = resolveSessionCommunicationPolicy({
config: e.config,
entry: r.entry
})[t];
if (n === "never") return {
allowed: false,
error: "Session communication " + t + " is disabled for " + r.sessionKey + "."
};
if (n === "ask") i.push({
direction: t,
endpoint: r
});
}
return {
allowed: true,
approvals: i
};
}
/** Only communication-relevant facts bind an approval, not token/preview updates. */
function communicationEndpointBinding(e) {
return JSON.stringify([
e.agentId,
e.sessionKey,
e.storePath,
communicationEntryBinding(e.entry)
]);
}
/** Stored-policy projection carried by the existing committed row publication owner. */
function communicationEntryBinding(e) {
return JSON.stringify([
e?.sessionId,
e?.previousSessionId,
e?.lifecycleRevision,
e?.archivedAt,
e?.communication,
e?.sandbox,
e?.sandboxMode,
e?.visibility,
e?.parentSessionKey,
e?.parentSessionId,
e?.spawnedBy,
e?.owner,
e?.createdActor
]);
}
//#endregion
export { assertSessionEntryCreationPublication as a, emitPreparedSessionSharingChange as c, publishSessionEntryWorkerMetadataInvalidation as d, publishSessionSharingEntryChange as f, withSessionEntryCreationPublication as h, resolveSessionCommunicationPolicy as i, invalidateSessionEntryCaches as l, runWithSessionEntryCreationPublication as m, communicationEntryBinding as n, bindPreparedSessionEntryPublication as o, publishSessionSharingMemberChange as p, planSessionCommunication as r, commitSessionSharingFieldChange as s, communicationEndpointBinding as t, publishSessionEntryPlaceholderInsertion as u };