openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
43 lines (42 loc) • 1.99 kB
JavaScript
import { a as captureGatewayStateOwner } from "./gateway-state-owner-Bnv5owvG.mjs";
import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs";
import { p as errorShape } from "./error-codes-BzF7aRQ0.mjs";
import { u as resolveGatewayLockPaths } from "./gateway-lock-IYOwJQai.mjs";
import { i as withChannelReadAuthority } from "./channel-read-authority-BVl4WY3A.mjs";
import { statSync } from "node:fs";
import path from "node:path";
//#region src/gateway/server-methods/local-state-owner.ts
/** Optional for old clients; new local callers must bind to the physically held owner. */
function captureLocalStateMutationGuard(e, t, n) {
const { stateDir: r } = resolveGatewayLockPaths(process.env);
const i = captureGatewayStateOwner(path.join(r, "state", "openclaw.sqlite"));
const a = t.context.getRuntimeConfig();
const assertCurrent = () => {
t.signal?.throwIfAborted();
t.sessionMutationCommitGuard?.();
if (!i || i.role !== "gateway" || i.ownerId !== e || t.hasCurrentClientAuthority?.() === false || t.context.getRuntimeConfig() !== a) throw new Error("Gateway state owner or requester changed; rerun against the current owner.");
i.assertCurrent();
if (n) {
const e = statSync(n.path, { bigint: true });
if (`${e.dev}:${e.ino}` !== n.identity) throw new Error("Source repository changed; rerun worktrees create.");
}
};
assertCurrent();
return assertCurrent;
}
function localStateOwnerChangedError(e) {
return errorShape(ErrorCodes.UNAVAILABLE, String(e), {
details: {
reason: "STATE_OWNER_CHANGED",
mutationAccepted: false
},
retryable: false
});
}
/** Preserve the selected owner through plugin transport effects and response disclosure. */
function runWithLocalStateMutationOwner(e, t, n) {
const r = captureLocalStateMutationGuard(e, t);
return withChannelReadAuthority(r, () => n(r), t.signal);
}
//#endregion
export { localStateOwnerChangedError as n, runWithLocalStateMutationOwner as r, captureLocalStateMutationGuard as t };