openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
72 lines (71 loc) • 3.21 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { o as emitInternalDiagnosticEvent, t as areDiagnosticsEnabledForProcess } from "./diagnostic-events-BjAH1Kbx.mjs";
import { d as isIncognitoDiagnosticSession, g as resolveDiagnosticSessionKey, l as getDiagnosticSessionState, s as markDiagnosticActivity, t as diagnosticLogger } from "./diagnostic-runtime-BxwnWXDu.mjs";
import { o as markDiagnosticArgumentChurnObservation } from "./diagnostic-run-activity-BzWz2WN_.mjs";
import { a as recordToolCallOutcome, c as getArgumentChurnNoProgressStreak, i as recordToolCall, n as hashToolCall, t as detectToolCallLoop } from "./tool-loop-detection-CR_Hr4G6.mjs";
//#region src/logging/diagnostic-tool-loop.ts
/** Tool-loop diagnostic logging and trusted tool.loop event emission. */
function logToolLoopAction(e) {
if (!areDiagnosticsEnabledForProcess()) return;
if (!isIncognitoDiagnosticSession(e)) {
const t = `tool loop: sessionId=${e.sessionId ?? "unknown"} sessionKey=${e.sessionKey ?? "unknown"} tool=${e.toolName} level=${e.level} action=${e.action} detector=${e.detector} count=${e.count}${e.pairedToolName ? ` pairedTool=${e.pairedToolName}` : ""} message="${e.message}"`;
if (e.level === "critical") diagnosticLogger.error(t);
else diagnosticLogger.warn(t);
}
emitInternalDiagnosticEvent({
type: "tool.loop",
sessionId: e.sessionId,
sessionKey: resolveDiagnosticSessionKey(e),
...e.agentId ? { agentId: e.agentId } : {},
toolName: e.toolName,
level: e.level,
action: e.action,
detector: e.detector,
count: e.count,
message: e.message,
pairedToolName: e.pairedToolName
});
markDiagnosticActivity();
}
//#endregion
//#region src/agents/tool-loop-call-reconciliation.ts
/**
* Rebind the pending admission record to the arguments that will actually
* execute after trusted/plugin policy rewrites, then re-evaluate churn against
* the completed history that preceded that call.
*/
function reconcileToolCallExecutionParams(e, t) {
const n = e.toolCallHistory;
if (!n) return false;
const r = normalizeOptionalString(t.runId);
const i = hashToolCall(t.toolName, t.toolParams);
for (let e = n.length - 1; e >= 0; e -= 1) {
const a = n[e];
if (!a || normalizeOptionalString(a.runId) !== r) continue;
if (t.toolCallId && a.toolCallId !== t.toolCallId) continue;
if (a.toolName !== t.toolName || a.resultHash !== void 0 || a.outcomeKind !== void 0) continue;
a.argsHash = i;
const o = n.slice(0, e).filter((e) => normalizeOptionalString(e.runId) === r);
return getArgumentChurnNoProgressStreak(o, t.toolName, i).count >= t.warningThreshold;
}
return false;
}
//#endregion
//#region src/agents/agent-tools.before-tool-call.runtime.ts
/**
* Lazy runtime dependencies for before_tool_call handling.
* Keeps diagnostics and loop-detection imports behind a seam that tests can
* replace without loading the full runtime graph.
*/
const beforeToolCallRuntime = {
getArgumentChurnNoProgressStreak,
markDiagnosticArgumentChurnObservation,
getDiagnosticSessionState,
logToolLoopAction,
detectToolCallLoop,
reconcileToolCallExecutionParams,
recordToolCall,
recordToolCallOutcome
};
//#endregion
export { beforeToolCallRuntime };