Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Keep a failed tool call's error readable when the rest of the payload is omitted

koeria19de57f43d6b
diff --git a/server/tool-summary.test.ts b/server/tool-summary.test.ts
index c427685d8e..94002ab462 100644
--- a/server/tool-summary.test.ts
+++ b/server/tool-summary.test.ts
@@ -34,6 +34,24 @@ describe("toolDetailPreview", () => {
expect(toolDetailPreview({ api_key: 1234 })).not.toContain("1234");
expect(toolDetailPreview(1234n)).toBe("1234");
});
+ it("keeps a failed call's error readable when the rest of the payload is omitted", () => {
+ // The issue's shape: a deep or wide payload whose error field was elided with the rest.
+ const wide: Record<string, unknown> = { tool_slug: "GOOGLESHEETS_VALUES_GET", index: 0, error: "row must be a list of lists" };
+ wide.response = Object.fromEntries(Array.from({ length: 250 }, (_, i) => [`field_${i}`, "x".repeat(40)]));
+ const preview = toolDetailPreview(wide)!;
+ expect(preview).toContain("row must be a list of lists");
+ expect(JSON.parse(preview).error).toBe("row must be a list of lists");
+ // An error past the depth limit keeps its cause too.
+ const deep: Record<string, unknown> = {};
+ let node: Record<string, unknown> = deep;
+ for (let i = 0; i < 8; i++) { node.child = {}; node = node.child as Record<string, unknown>; }
+ node.error = "connection refused after 30s";
+ expect(toolDetailPreview(deep)!).toContain("connection refused after 30s");
+ // A long error is kept as a redacted prefix, not dropped whole.
+ const long = JSON.parse(toolDetailPreview({ ok: false, error: `${"cause ".repeat(200)}` })!);
+ expect(long.error.length).toBeLessThanOrEqual(500);
+ expect(toolDetailPreview({ ok: false, error: `boom ghp_${"a".repeat(36)}` })).not.toContain("ghp_");
+ });
it("scrubs structured JSON inside native text results without changing ordinary prose", () => {
const preview = toolDetailPreview([{ type: "text", text: JSON.stringify({ message: "request completed", password: "tiny", headers: { Cookie: "sid=x" }, env: [{ name: "API_KEY", value: "small" }] }) }]);
expect(preview).toContain("request completed");
diff --git a/server/tool-summary.ts b/server/tool-summary.ts
index cff1ffcf37..bb82d806a6 100644
--- a/server/tool-summary.ts
+++ b/server/tool-summary.ts
@@ -6,8 +6,43 @@ export function toolDetailPreview(value: unknown): string | undefined {
if (value === undefined) return undefined;
let budget = 200;
let textBudget = 256_000;
+ /** A failed call's error is the cause the model must read; it is kept whole even where the rest is omitted. */
+ const ERROR_KEY = /^error/i;
+ const ERROR_TEXT_MAX = 500;
+ const errorText = (value: unknown): string => {
+ let text: string;
+ if (typeof value === "string") text = value;
+ else {
+ try { text = JSON.stringify(redactSecrets(value)) ?? ""; }
+ catch { return "[error not serializable]"; }
+ }
+ // Redact before the cut, like commandSummary, so a sliced credential cannot slip through.
+ return redactSecretsInText(text).trim().slice(0, ERROR_TEXT_MAX);
+ };
+ /** The first /^error/i value anywhere below an omitted object: the cause a failed call must still give up. */
+ const findError = (item: Record<string, unknown> | object, depth = 0): unknown => {
+ if (depth > 12) return undefined;
+ for (const [key, child] of Object.entries(item).slice(0, 40)) {
+ if (ERROR_KEY.test(key) && isErrorValue(child)) return child;
+ }
+ for (const child of Object.values(item).slice(0, 40)) {
+ if (child !== null && typeof child === "object" && !Array.isArray(child)) {
+ const found = findError(child, depth + 1);
+ if (found !== undefined) return found;
+ }
+ }
+ return undefined;
+ };
+ const isErrorValue = (value: unknown) => typeof value === "string" || (value !== null && typeof value === "object");
const bounded = (item: unknown, depth = 0): unknown => {
- if (--budget < 0 || depth > 6) return "[additional data omitted]";
+ if (--budget < 0 || depth > 6) {
+ // Out of budget or depth, a payload carrying an error still gives up its cause instead of a bare placeholder.
+ if (item !== null && typeof item === "object" && !Array.isArray(item)) {
+ const error = findError(item);
+ if (error !== undefined) return { error: errorText(error), "…": "[additional data omitted]" };
+ }
+ return "[additional data omitted]";
+ }
if (typeof item === "string") {
if (item.length > textBudget) return "[large content omitted]";
if (/^data:[^,\s]+;base64,/i.test(item)) return "[binary content omitted]";
@@ -24,7 +59,9 @@ export function toolDetailPreview(value: unknown): string | undefined {
const entries = Object.entries(item);
const result: Record<string, unknown> = Object.create(null);
for (const [key, child] of entries.slice(0, 40)) {
- result[key] = /token|secret|password|passwd|cookie|authorization|api.?key|(^|[_.-])keys?$/i.test(key)
+ result[key] = ERROR_KEY.test(key) && isErrorValue(child)
+ ? errorText(child)
+ : /token|secret|password|passwd|cookie|authorization|api.?key|(^|[_.-])keys?$/i.test(key)
? "[redacted]"
: /^(data|base64|blob)$/i.test(key) && typeof child === "string"
? "[binary content omitted]"