openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
72 lines (71 loc) • 3.88 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as sameSessionTranscriptStorePath } from "./transcript-store-identity-jolHcJiu.mjs";
import { AsyncLocalStorage } from "node:async_hooks";
//#region src/config/sessions/cli-history-boundary.ts
const cliHistoryWriter = new AsyncLocalStorage();
/** The transcript tip moved between CLI history planning and the writer's commit. */
const CLI_HISTORY_CHANGED_BEFORE_PREPARATION = "CLI history changed before preparation";
function runWithCliHistoryWriter(e, t) {
return e ? cliHistoryWriter.run(e, t) : cliHistoryWriter.exit(t);
}
function cliHistoryWriterFacts(e) {
return {
runId: e.runId,
authFingerprint: e.authFingerprint,
lifecycleRevision: e.lifecycleRevision,
...e.confirmsOwner ? { confirmOwner: true } : {}
};
}
/**
* Whether coverage may advance for this writer. A writer whose facts ask for the host's owner
* check advances only on a confirmed answer, so a write path that cannot ask the host keeps
* its rows and leaves coverage where it is.
*/
function cliHistoryOwnerHolds(e, t) {
return "confirmOwner" in e && e.confirmOwner === true ? t?.() === true : t?.() !== false;
}
const OWNER_PROBE = "cli-history-owner-probe";
/**
* Worker side of the in-transaction owner check. The worker puts `fact` on a transaction or
* commit admission request; the host answers into the shared cell before it grants, so
* `holds()` is read only after that grant. No answer means the owner does not hold.
*/
function createCliHistoryOwnerProbe() {
const e = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT));
return {
fact: {
kind: OWNER_PROBE,
verdict: e.buffer
},
holds: () => Atomics.load(e, 0) === 1
};
}
/** Host side: answer a worker's owner probe synchronously, right before its admission grant. */
function answerCliHistoryOwnerProbe(e, t) {
if (!isRecord(e) || e.kind !== OWNER_PROBE || !(e.verdict instanceof SharedArrayBuffer) || e.verdict.byteLength !== Int32Array.BYTES_PER_ELEMENT) return false;
const n = t?.confirmsOwner?.() === true;
Atomics.store(new Int32Array(e.verdict), 0, n ? 1 : 0);
return true;
}
function getCliHistoryWriter(e) {
const t = cliHistoryWriter.getStore();
return t && t.target.agentId === e.agentId && t.target.sessionId === e.sessionId && t.target.sessionKey === e.sessionKey && sameSessionTranscriptStorePath(t.target.storePath, e.storePath) ? t : void 0;
}
function isKnownCliHistoryBoundary(e) {
return e?.version === 1 && e.state === "known" && typeof e.sessionId === "string" && e.sessionId.length > 0 && typeof e.authFingerprint === "string" && /^[a-f0-9]{64}$/.test(e.authFingerprint) && (e.generation === null || typeof e.generation === "string" && e.generation.length > 0) && (e.maxSeq === null || e.generation !== null && Number.isSafeInteger(e.maxSeq) && e.maxSeq >= 0) && typeof e.writerRunId === "string" && e.writerRunId.length > 0;
}
/** Advance only a contiguous prefix owned by the same prepared CLI account. */
function advanceCliHistoryBoundary(e, t, n, r, i) {
const a = e?.cliHistoryBoundary;
if (r.last < r.first || !e || !isKnownCliHistoryBoundary(a) || e.sessionId !== t || a.sessionId !== t || e.activeWriterRunId !== i.runId || e.lifecycleRevision !== i.lifecycleRevision || a.writerRunId !== i.runId || a.authFingerprint !== i.authFingerprint || (a.maxSeq === null ? r.first !== 0 : a.maxSeq !== r.first - 1) || !n || (a.generation === null ? r.first !== 0 : a.generation !== n)) return;
return {
...e,
cliHistoryBoundary: {
...a,
generation: n,
maxSeq: r.last
}
};
}
//#endregion
export { cliHistoryWriterFacts as a, isKnownCliHistoryBoundary as c, cliHistoryOwnerHolds as i, runWithCliHistoryWriter as l, advanceCliHistoryBoundary as n, createCliHistoryOwnerProbe as o, answerCliHistoryOwnerProbe as r, getCliHistoryWriter as s, CLI_HISTORY_CHANGED_BEFORE_PREPARATION as t };