openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
206 lines (205 loc) • 7.66 kB
JavaScript
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs";
import { s as redactSensitiveText } from "./redact-Mj7pppxC.mjs";
import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs";
import { f as sessionEntryCommitGuardOptions, o as composeSessionSourceAssertion } from "./session-source-authority-TlTpfRVE.mjs";
import "./session-accessor-CizTbDQj.mjs";
import { r as getCliSessionBinding } from "./cli-session-binding-BEOgFSDg.mjs";
import { i as patchSessionEntryCore } from "./session-accessor.sqlite-entry-Da3xxVPX.mjs";
import { r as formatErrorMessageForDisplay } from "./error-diagnostics-CD3FocGA.mjs";
import { c as setCliSessionBinding, n as assertCliSessionBindingResultCommitAllowed, r as clearCliSession, t as applyCliSessionBindingResult } from "./cli-session-BsEOjZRc.mjs";
import { i as hasCommittedCliFinalReply, t as appendAgentRunFailure } from "./agent-run-result-Bi1gRTvj.mjs";
//#region src/agents/cli-session-store.ts
async function patchCliSessionBindingInStore(e) {
const { sessionKey: t, storePath: n } = e;
if (!t || !n) return;
const r = { ...e.expectedSession };
let i;
await patchSessionEntryCore({
agentId: e.agentId,
sessionKey: t,
storePath: n
}, (t) => {
if (t.sessionId !== r.sessionId || t.lifecycleRevision !== r.lifecycleRevision || t.activeWriterRunId !== r.activeWriterRunId) return null;
const n = { ...t };
if (!e.update(n)) return null;
return {
cliSessionIds: n.cliSessionIds,
cliSessionBindings: n.cliSessionBindings,
claudeCliSessionId: n.claudeCliSessionId
};
}, {
fallbackEntry: e.fallbackEntry,
...sessionEntryCommitGuardOptions(e.assertCommitAllowed),
preserveActivity: e.preserveActivity,
skipMaintenance: e.skipMaintenance,
onCommitted: (n) => {
i = n;
e.onCommitted?.();
if (e.sessionStore) e.sessionStore[t] = n;
}
});
return i;
}
async function patchCliSessionForkBinding(e, t) {
const { provider: n, sessionKey: r, sessionStore: i, expectedCliSessionId: a } = e;
const o = i[r];
if (!o || o.cliSessionBindings?.[n]?.sessionId !== a) return;
return await patchCliSessionBindingInStore({
...e,
expectedSession: o,
update: (e) => {
const r = e.cliSessionBindings?.[n];
if (r?.sessionId !== a) return false;
const i = t(r);
if (!i) return false;
setCliSessionBinding(e, n, i);
return true;
}
});
}
/** Clears the one-shot fork marker before the resumed CLI process starts. */
async function consumeCliSessionForkInStore(e) {
return await patchCliSessionForkBinding(e, (e) => {
if (e.forkNextResume !== true) return;
const { forkNextResume: t, ...n } = e;
return n;
});
}
/** Arms a fork marker for recovery, or re-arms one after a failed CLI turn. */
async function restoreCliSessionForkInStore(e) {
return await patchCliSessionForkBinding(e, (e) => e.forkNextResume === true ? void 0 : {
...e,
forkNextResume: true
});
}
/** Share fork publication and cancellation rules across command and live reply callers. */
function buildCliSessionForkRunParams(e, t) {
const n = {
...e,
assertCommitAllowed: composeSessionSourceAssertion([e.assertCommitAllowed], (t) => {
t();
e.abortSignal?.throwIfAborted();
})
};
return {
claimCliSessionFork: async () => {
const e = await consumeCliSessionForkInStore(n);
if (e) t(e);
return Boolean(e);
},
restoreCliSessionFork: async () => {
const n = await restoreCliSessionForkInStore(e);
if (n) t(n);
},
persistCliSessionForkSuccessor: async (e) => {
const r = await persistCliSessionForkSuccessorInStore({
...n,
successorCliSessionId: e
});
if (!r) throw new Error("CLI session fork successor could not be persisted");
t(r);
}
};
}
/** Rebinds a claimed fork without bypassing its retained account/environment checks. */
async function persistCliSessionForkSuccessorInStore(e) {
if (e.successorCliSessionId === e.expectedCliSessionId) return;
return await patchCliSessionForkBinding(e, (t) => t.forkNextResume === true ? void 0 : {
...t,
sessionId: e.successorCliSessionId
});
}
/** A rejected continuity write cannot erase completed effects or reopen model fallback. */
async function settleCliSessionResult(e, t) {
try {
await t();
return e;
} catch (t) {
if (hasCommittedCliFinalReply(e)) {
createSubsystemLogger("agents/cli-runner").warn(`CLI session continuity could not be saved after final reply commit: ${formatErrorMessageForDisplay(t)}`);
return e;
}
const n = redactSensitiveText(formatErrorMessageForDisplay(t), { mode: "tools" });
const r = truncateUtf16Safe(`CLI session continuity could not be saved: ${n}`, 1024);
return appendAgentRunFailure(e, r);
}
}
/** Publish native continuity before the placement owner releases its session lane. */
async function persistCliSessionBindingResult(e) {
const t = e.expectedSession;
if (!t) return e.result;
return await settleCliSessionResult(e.result, async () => {
await patchCliSessionBindingInStore({
...e,
expectedSession: t,
preserveActivity: true,
skipMaintenance: true,
update: (t) => {
const n = e.result.meta.agentMeta;
if (!e.preserveBinding) return applyCliSessionBindingResult(t, e.provider, n);
const r = getCliSessionBinding(t, e.provider);
const i = n?.cliSessionBinding;
const a = i?.transcriptCursor;
if (n?.clearCliSessionBinding === true || !r || r.sessionId !== i?.sessionId || !a) return false;
setCliSessionBinding(t, e.provider, {
...r,
transcriptCursor: a
});
return true;
},
assertCommitAllowed: composeSessionSourceAssertion([e.assertSettlementCurrent], (t) => assertCliSessionBindingResultCommitAllowed(e.result.meta.agentMeta, t, e.abortSignal))
});
});
}
/**
* A directly stopped first turn has no settled result, but its flushed native session is
* the chat's only continuity. The turn owner still guards the write; caller abort does not,
* and an existing binding is never replaced.
*/
async function persistStoppedFirstCliSessionInStore(e) {
if (!e.expectedSession || getCliSessionBinding(e.expectedSession, e.provider)) return;
await patchCliSessionBindingInStore({
...e,
expectedSession: e.expectedSession,
preserveActivity: true,
skipMaintenance: true,
assertCommitAllowed: e.assertSettlementCurrent,
update: (t) => {
if (getCliSessionBinding(t, e.provider)) return false;
setCliSessionBinding(t, e.provider, e.binding);
return true;
}
});
}
/** Clears a failed/invalid native binding; a turn owner supplies its exact commit guard. */
async function clearCliSessionInStore(e) {
const t = e.activeSessionEntry ?? (e.sessionKey ? e.sessionStore?.[e.sessionKey] : void 0);
if (!t) return;
const clearEntry = (t) => {
if (!t || e.expectedCliSessionId && getCliSessionBinding(t, e.provider)?.sessionId !== e.expectedCliSessionId) return false;
clearCliSession(t, e.provider);
t.updatedAt = Date.now();
return true;
};
const clearCachedEntries = () => {
clearEntry(e.activeSessionEntry);
clearEntry(e.sessionKey ? e.sessionStore?.[e.sessionKey] : void 0);
};
if (!e.sessionKey || !e.storePath) {
e.assertCommitAllowed?.();
clearCachedEntries();
return;
}
return await patchCliSessionBindingInStore({
...e,
expectedSession: {
...t,
sessionId: e.expectedSessionId ?? t.sessionId
},
fallbackEntry: e.expectedSessionId ? void 0 : t,
update: clearEntry,
onCommitted: clearCachedEntries
});
}
//#endregion
export { restoreCliSessionForkInStore as a, persistStoppedFirstCliSessionInStore as i, clearCliSessionInStore as n, settleCliSessionResult as o, persistCliSessionBindingResult as r, buildCliSessionForkRunParams as t };