openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
100 lines (99 loc) • 3.83 kB
JavaScript
import { a as resolveGlobalSingleton } from "./global-singleton-D7yf05nR.mjs";
import { d as runWithSessionSourceScope, o as composeSessionSourceAssertion } from "./session-source-authority-TlTpfRVE.mjs";
import { o as getGatewayToolCallerIdentity } from "./gateway-caller-context-BNidAW8G.mjs";
import { a as copyAgentToolMetadata } from "./agent-tool-metadata-nrQgRaqv.mjs";
import { AsyncLocalStorage } from "node:async_hooks";
//#region src/agents/agent-tool-source-execution-guard.ts
const executionBudgetContext = resolveGlobalSingleton(Symbol.for("openclaw.agentToolExecutionBudgetContext"), () => new AsyncLocalStorage());
/** One invocation owns the count and closes every tool closure it created. */
function createAgentToolExecutionBudget(e) {
let t = 0;
let n = true;
const assertCurrent = () => {
e.signal.throwIfAborted();
try {
if (!n || e.isCurrent?.() === false) throw new Error("Agent tool execution scope is no longer active");
} catch (t) {
if (t instanceof Error) e.abort(t);
throw t;
}
};
const admit = () => {
assertCurrent();
if (e.maxToolCalls !== void 0 && t >= e.maxToolCalls) {
const t = /* @__PURE__ */ new Error("Agent tool-call budget exhausted");
e.abort(t);
throw t;
}
t += 1;
};
return {
get toolCalls() {
return t;
},
async run(e) {
try {
return await executionBudgetContext.run({
admit,
assertCurrent
}, e);
} finally {
n = false;
}
}
};
}
/** Capture at tool construction so retained tools cannot borrow a later budget. */
function captureAgentToolExecutionBudget() {
return executionBudgetContext.getStore()?.admit;
}
/** Freeze the invocation's operational fence before asynchronous source work. */
function captureAgentToolSourceExecutionGuard(e) {
const t = getGatewayToolCallerIdentity()?.receiptAuthority;
const n = executionBudgetContext.getStore()?.assertCurrent;
const r = Object.assign(() => {
if (t?.() === false) throw new Error("tool invocation authority is no longer active");
}, t);
return composeSessionSourceAssertion([r], (t) => {
e?.throwIfAborted();
n?.();
t();
});
}
/** Prepare storage authority for one effect; its final callback checks the retained receipt. */
function runWithAgentToolSourceExecutionScope(e, t) {
return runWithSessionSourceScope(e, t, { checkAfter: false });
}
const SOURCE_EXECUTION_GUARD = Symbol.for("openclaw.agentToolSourceExecutionGuard");
function appendSourceExecutionGuard(e, t) {
const n = e[SOURCE_EXECUTION_GUARD];
if (n === t) return;
Object.defineProperty(e, SOURCE_EXECUTION_GUARD, {
value: n ? composeSessionSourceAssertion([n, t]) : t,
configurable: true,
enumerable: false
});
}
/** Bind a host-owned guard without mutating a tool that another attempt may reuse. */
function bindAgentToolSourceExecutionGuard(e, t) {
const n = copyAgentToolMetadata(e, { ...e });
copyAgentToolSourceExecutionGuard(e, n);
appendSourceExecutionGuard(n, t);
return n;
}
function copyAgentToolSourceExecutionGuard(e, t) {
const n = e[SOURCE_EXECUTION_GUARD];
if (n) appendSourceExecutionGuard(t, n);
}
function runAgentToolSourceExecutionGuard(e) {
e[SOURCE_EXECUTION_GUARD]?.();
}
/** Third-party tools enter their implementation with freshly prepared source authority. */
function runWithBoundAgentToolSourceExecutionScope(e, t) {
return runWithAgentToolSourceExecutionScope(e[SOURCE_EXECUTION_GUARD], async () => {
runAgentToolSourceExecutionGuard(e);
return t();
});
}
//#endregion
export { createAgentToolExecutionBudget as a, runWithBoundAgentToolSourceExecutionScope as c, copyAgentToolSourceExecutionGuard as i, captureAgentToolExecutionBudget as n, runAgentToolSourceExecutionGuard as o, captureAgentToolSourceExecutionGuard as r, runWithAgentToolSourceExecutionScope as s, bindAgentToolSourceExecutionGuard as t };