Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Start a fresh provider session when the saved one no longer exists.
Ryan0e37275c8a8a
diff --git a/server/drivers/acp/acp.test.ts b/server/drivers/acp/acp.test.tsindex 76c0014447..8d009b57f6 100644--- a/server/drivers/acp/acp.test.ts+++ b/server/drivers/acp/acp.test.ts@@ -2472,8 +2472,9 @@ describe("ACP turns (fake CLI)", () => {expect(done).toMatchObject({ ok: true });});- it.each(["null", "opencode-not-found"])("rebuilds a missing native session from the canonical recovery text once (%s)", async (failure) => {+ it.each(["null", "opencode-not-found", "cursor-message"])("rebuilds a missing native session from the canonical recovery text once (%s)", async (failure) => {if (failure === "null") process.env.FAKE_ACP_LOAD_NULL = "1";+ else if (failure === "cursor-message") process.env.FAKE_ACP_LOAD_ERROR = JSON.stringify({ code: -32602, message: "Invalid params: Session \"missing-cursor\" not found" });else process.env.FAKE_ACP_LOAD_ERROR = JSON.stringify({ code: -32602, message: "Session not found", data: { sessionId: "missing-cursor" } });const dump = join(scratch, "recovery.json");process.env.FAKE_ACP_DUMP = dump;diff --git a/server/drivers/acp/core.ts b/server/drivers/acp/core.tsindex 1916cbe4b9..8695d198c5 100644--- a/server/drivers/acp/core.ts+++ b/server/drivers/acp/core.ts@@ -25,6 +25,7 @@ import {classifyQuiet, describeQuiet, formatQuietLimit, quietNoticeText, lastSeenPhrase, LIMIT_WORDS, LogTail, promptIdleTimeoutMs, quietKey, quietNoticeMs, quietTickMs, quietVerdict, sampleProcessTree,type LogSignal, type ProcessSample, type QuietState,} from "./quiet-status.ts";+import { acpMissingSession } from "./rpc-error.ts";import { readSessionReport } from "./session-reports.ts";import { pickSessionModel, SessionModelControls, type AcpModelSelection } from "./session-model-controls.ts";import { notThisCli, versionFromProbe } from "./version-probe.ts"; import { engineHomes } from "../../engine-homes/engine-homes.ts";@@ -1294,11 +1295,10 @@ export function createAcpDriver(support: AcpSupport): ProviderDriver<AcpConfig>);} catch (error) {const classification = support.classifyError?.(error);- // OpenCode encodes ACPSessionNotFoundError as invalidParams with just the rejected- // sessionId; other invalidParams (model/config errors) must not erase history.- const data = (error as any)?.data;- const missingSession = data && typeof data === "object" && !Array.isArray(data)- && data.sessionId === cursor && Object.keys(data).length === 1;+ // OpenCode sends invalidParams with only the rejected sessionId. Cursor puts the id in the+ // message: Invalid params: Session "<id>" not found (session/load). Other invalidParams must not+ // erase history.+ const missingSession = acpMissingSession(error, cursor);if (classification === "invalid_credentials" || classification === "inactive_subscription"|| ((error as any)?.code === -32602 && !missingSession)) throw error;}diff --git a/server/drivers/acp/rpc-error.test.ts b/server/drivers/acp/rpc-error.test.tsnew file mode 100644index 0000000000..7dff8442cc--- /dev/null+++ b/server/drivers/acp/rpc-error.test.ts@@ -0,0 +1,22 @@+import { describe, expect, it } from "vitest";+import { acpMissingSession, acpRpcError } from "./rpc-error.ts";++describe("acpMissingSession", () => {+ const cursor = "a09518ea-61e5-4534-8575-afbdb784a4b5";++ it("accepts OpenCode's invalidParams that carry only the rejected session id", () => {+ expect(acpMissingSession({ data: { sessionId: cursor } }, cursor)).toBe(true);+ expect(acpMissingSession({ data: { sessionId: cursor, model: "gone" } }, cursor)).toBe(false);+ });++ it("accepts Cursor's session/load message, which names the id and not a data field", () => {+ const error = acpRpcError({ code: -32602, message: `Invalid params: Session "${cursor}" not found` }, "session/load");+ expect(acpMissingSession(error, cursor)).toBe(true);+ });++ it("does not treat a bare invalidParams or another missing id as this session", () => {+ expect(acpMissingSession(acpRpcError({ code: -32602, message: "Invalid params" }, "session/load"), cursor)).toBe(false);+ expect(acpMissingSession(acpRpcError({ code: -32602, message: "Invalid params: model not found" }, "session/load"), cursor)).toBe(false);+ expect(acpMissingSession(acpRpcError({ code: -32602, message: `Invalid params: Session "${cursor}" not found` }, "session/prompt"), cursor)).toBe(false);+ });+});diff --git a/server/drivers/acp/rpc-error.ts b/server/drivers/acp/rpc-error.tsindex d213d3b010..86add83dce 100644--- a/server/drivers/acp/rpc-error.ts+++ b/server/drivers/acp/rpc-error.ts@@ -22,3 +22,14 @@ export function acpRpcError(value: any, method: string): Error {acpSessionFailure: value?.code === -32603,});}++/** 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 data = error && typeof error === "object" ? (error as { data?: unknown }).data : undefined;+ if (data && typeof data === "object" && !Array.isArray(data)) {+ const record = data as Record<string, unknown>;+ if (record.sessionId === cursor && Object.keys(record).length === 1) return true;+ }+ const message = error instanceof Error ? error.message : typeof error === "string" ? error : "";+ return message.includes(cursor) && /\bnot found\b/i.test(message) && /\bsession\/(?:load|resume)\b/.test(message);+}