fix: address code review - nested object hashing and error logging

This commit is contained in:
SK Akram
2026-02-15 12:52:34 +00:00
committed by Gustavo Madeira Santana
parent 40939b29a2
commit 917dddb27b
2 changed files with 16 additions and 5 deletions

View File

@@ -17,16 +17,25 @@ export const CRITICAL_THRESHOLD = 20;
*/
export function hashToolCall(toolName: string, params: unknown): string {
try {
const paramsStr =
typeof params === "object" && params !== null
? JSON.stringify(params, Object.keys(params).toSorted())
: String(params);
const paramsStr = stableStringify(params);
return `${toolName}:${paramsStr}`;
} catch {
return `${toolName}:${String(params)}`;
}
}
function stableStringify(value: unknown): string {
if (value === null || typeof value !== "object") {
return JSON.stringify(value);
}
if (Array.isArray(value)) {
return `[${value.map(stableStringify).join(",")}]`;
}
const obj = value as Record<string, unknown>;
const keys = Object.keys(obj).toSorted();
return `{${keys.map((k) => `${JSON.stringify(k)}:${stableStringify(obj[k])}`).join(",")}}`;
}
/**
* Detect if an agent is stuck in a repetitive tool call loop.
* Checks if the same tool+params combination has been called excessively.

View File

@@ -320,7 +320,9 @@ export function startDiagnosticHeartbeat() {
pruneStaleCommandPolls(state);
}
})
.catch(() => {});
.catch((err) => {
diag.debug(`command-poll-backoff prune failed: ${String(err)}`);
});
for (const [, state] of diagnosticSessionStates) {
const ageMs = now - state.lastActivity;