Skip to content

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

Judge a missing session from the vendor's own message.

Ryan2f88c7398796
diff --git a/server/drivers/acp/rpc-error.test.ts b/server/drivers/acp/rpc-error.test.ts
index 8351919eba..3c95d05106 100644
--- a/server/drivers/acp/rpc-error.test.ts
+++ b/server/drivers/acp/rpc-error.test.ts
@@ -1,4 +1,5 @@
import { describe, expect, it } from "vitest";
+import { addHeldSecretScrubber } from "../../redact.ts";
import { acpMissingSession, acpRpcError } from "./rpc-error.ts";
describe("acpMissingSession", () => {
@@ -22,13 +23,32 @@ describe("acpMissingSession", () => {
expect(acpMissingSession(acpRpcError({ code: -32602, message: "Invalid params: Session \"session-10\" not found" }, "session/load"), "session-1")).toBe(false);
});
- it("reads the session id from the full diagnostic after the shown error is cut", () => {
+ it("matches the original message when redaction removes the session id from what is shown", () => {
+ const remove = addHeldSecretScrubber((text) => text.split(cursor).join("«secret»"));
+ try {
+ const error = acpRpcError({ code: -32602, message: `Invalid params: Session "${cursor}" not found` }, "session/load");
+ const shown = error as Error & { acpDiagnostic?: string };
+ expect(shown.message.includes(cursor)).toBe(false);
+ expect(shown.acpDiagnostic?.includes(cursor)).toBe(false);
+ expect(acpMissingSession(error, cursor)).toBe(true);
+ } finally {
+ remove();
+ }
+ });
+
+ it("compares the session id as written, and still accepts either case in the surrounding words", () => {
+ const upper = cursor.toUpperCase();
+ const error = acpRpcError({ code: -32602, message: `invalid params: SESSION "${upper}" NOT FOUND` }, "session/load");
+ expect(acpMissingSession(error, upper)).toBe(true);
+ expect(acpMissingSession(error, cursor)).toBe(false);
+ });
+
+ it("does not treat an earlier missing-session line quoted inside another error as this failure", () => {
const error = acpRpcError({
code: -32602,
- message: `${"x".repeat(1600)} Invalid params: Session "${cursor}" not found`,
+ message: "Invalid params: model not found",
+ data: { details: `Session "${cursor}" not found (session/load)` },
}, "session/load");
- expect(error.message.length).toBe(1500);
- expect(error.message.includes(cursor)).toBe(false);
- expect(acpMissingSession(error, cursor)).toBe(true);
+ expect(acpMissingSession(error, cursor)).toBe(false);
});
});
diff --git a/server/drivers/acp/rpc-error.ts b/server/drivers/acp/rpc-error.ts
index 11e6a5856e..c834af854c 100644
--- a/server/drivers/acp/rpc-error.ts
+++ b/server/drivers/acp/rpc-error.ts
@@ -1,5 +1,11 @@
import { redactSecretsInText } from "../../redact.ts";
+/** The vendor's own message and the method we called, before redaction or any attached detail. */
+const resumeRefusal = new WeakMap<object, { message: string; method: string }>();
+
+/** Words around the id are case-insensitive. The captured id keeps the vendor's case. */
+const MISSING_SESSION_MESSAGE = /^\s*(?:invalid params:\s*)?session\s+"([^"]+)"\s+not found\b(?:\s*\(session\/(?:load|resume)\))?[\s.]*$/i;
+
/** ACP vendors put the actionable cause in error.data under a generic JSON-RPC message. Only known
* text fields surface, redacted, never a response body or config dump. */
export function acpRpcError(value: any, method: string): Error {
@@ -18,6 +24,7 @@ export function acpRpcError(value: any, method: string): Error {
const diagnostic = `${detail && detail !== message ? `${message}: ${detail}` : message} (${context})`;
const shown = redactSecretsInText(diagnostic);
const error = new Error(shown.slice(0, 1500));
+ resumeRefusal.set(error, { message, method });
return Object.assign(error, { code: value?.code, data, acpDiagnostic: shown,
// -32603 is JSON-RPC's internal error; invalid params, unsupported methods and auth refusals keep the session.
acpSessionFailure: value?.code === -32603,
@@ -26,16 +33,14 @@ export function acpRpcError(value: any, method: string): Error {
/** The provider rejected this resume id. A bare invalidParams (a bad model or config) is not this. */
export function acpMissingSession(error: unknown, cursor: string): boolean {
- const record = error && typeof error === "object" ? error as { data?: unknown; acpDiagnostic?: unknown } : undefined;
+ const record = error && typeof error === "object" ? error as { data?: unknown } : undefined;
const data = record?.data;
if (data && typeof data === "object" && !Array.isArray(data)) {
const fields = data as Record<string, unknown>;
if (fields.sessionId === cursor && Object.keys(fields).length === 1) return true;
}
- const message = typeof record?.acpDiagnostic === "string" ? record.acpDiagnostic
- : error instanceof Error ? error.message
- : typeof error === "string" ? error : "";
- const id = cursor.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
- return new RegExp(`Session\\s+"${id}"\\s+not found\\b`, "i").test(message)
- && /\bsession\/(?:load|resume)\b/.test(message);
+ const refusal = record ? resumeRefusal.get(record) : undefined;
+ if (!refusal || (refusal.method !== "session/load" && refusal.method !== "session/resume")) return false;
+ const named = refusal.message.match(MISSING_SESSION_MESSAGE);
+ return named?.[1] === cursor;
}