Skip to content

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

Classify explicit app errors without reading unrelated JSON metadata

Afloat58277f00453c
diff --git a/server/connection-outcome-json.test.ts b/server/connection-outcome-json.test.ts
index bce7cd9679..f9ba38595f 100644
--- a/server/connection-outcome-json.test.ts
+++ b/server/connection-outcome-json.test.ts
@@ -33,4 +33,22 @@ describe("JSON text in a failed MCP tool result", () => {
expect(appCallOutcome(200, answer([JSON.stringify({ error: "Message not found" })], true), "application/json"))
.toEqual({ ok: true });
});
+
+ it.each([
+ { error: "Message not found", resource: "/messages/401" },
+ { message: "Message not found", resource: "/messages/403" },
+ { error: { message: "Message not found", resource: "/messages/401" } },
+ { data: { message: "Token expired; reconnect" }, requestId: "401" },
+ ])("ignores unrelated JSON fields in a failed result: %j", (value) => {
+ expect(appCallOutcome(200, answer([JSON.stringify(value)], true), "application/json"))
+ .toEqual({ ok: true });
+ });
+
+ it.each([
+ [{ error: { message: "invalid_token" } }, "sign-in"],
+ [{ error: { message: "403 Forbidden" }, resource: "/messages/401" }, "refused"],
+ ])("reads nested error messages without classifying metadata: %j", (value, error) => {
+ expect(appCallOutcome(200, answer([JSON.stringify(value)], true), "application/json"))
+ .toEqual({ ok: false, error });
+ });
});
diff --git a/server/connection-outcome.ts b/server/connection-outcome.ts
index 71bd83eb57..d15762612c 100644
--- a/server/connection-outcome.ts
+++ b/server/connection-outcome.ts
@@ -53,6 +53,14 @@ function failedErrors(value: unknown, depth = 0, out: string[] = []): string[] {
const classify = (text: string): ConnectionOutcome | null =>
SIGNED_OUT.test(text) ? { ok: false, error: "sign-in" } : REFUSED.test(text) ? { ok: false, error: "refused" } : null;
+function explicitErrorWords(value: unknown, depth = 0): string[] {
+ if (depth > 5) return [];
+ if (typeof value === "string") return [value];
+ if (!value || typeof value !== "object" || Array.isArray(value)) return [];
+ const { error, message } = value as Record<string, unknown>;
+ return [...explicitErrorWords(error, depth + 1), ...explicitErrorWords(message, depth + 1)];
+}
+
/** What a connected-apps tool call's answer through the relay showed: the account works, needs connecting again or was
* refused, or the service could not be reached. Null: it says nothing about the account. A tool's own error (a message
* not found) is the account working. Only a failed call's own error words are read, never what the tool returned. */
@@ -66,9 +74,14 @@ export function appCallOutcome(status: number, bytes: Uint8Array, contentType: s
: [];
const errors: string[] = [];
for (const text of texts) {
- try { failedErrors(JSON.parse(text), 0, errors); } catch {}
+ try {
+ const value: unknown = JSON.parse(text);
+ failedErrors(value, 0, errors);
+ if (frame.result.isError === true) errors.push(...explicitErrorWords(value));
+ } catch {
+ if (frame.result.isError === true) errors.push(text);
+ }
}
- if (frame.result.isError === true) errors.push(...texts);
if (!errors.length) return { ok: true };
return classify(errors.join("\n").slice(0, 4_000)) ?? { ok: true };
}
diff --git a/server/host/connection-health-host.test.ts b/server/host/connection-health-host.test.ts
index 382a179f67..fc46c59116 100644
--- a/server/host/connection-health-host.test.ts
+++ b/server/host/connection-health-host.test.ts
@@ -151,6 +151,17 @@ describe("health from real calls", () => {
host.appCall({ slugs: ["SLACK_SEND_MESSAGE"], status: 200, bytes: reply(JSON.stringify({ successful: true })), contentType: "application/json" });
expect((await read()).apps.slack.health).toBe("working");
});
+
+ it("does not expire a working app because its error metadata names resource 401", async () => {
+ const bytes = new TextEncoder().encode(JSON.stringify({
+ jsonrpc: "2.0", id: 1,
+ result: { isError: true, content: [{ type: "text", text: JSON.stringify({ error: "Message not found", resource: "/messages/401" }) }] },
+ }));
+ host.appCall({ slugs: ["SLACK_SEND_MESSAGE"], status: 200, bytes, contentType: "application/json" });
+ const { apps } = await read();
+ expect(apps.slack).toMatchObject({ health: "working", lastUsedAt: "2026-11-10T10:00:00.000Z" });
+ expect(apps.slack.lastError).toBeUndefined();
+ });
});
describe("the admin read", () => {