Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(pi): a Pi cursor from elsewhere, or whose file is gone, starts a new session
milind-soni97b330193820
diff --git a/server/drivers/pi-session-recovery.test.ts b/server/drivers/pi-session-recovery.test.tsindex 4779c96901..c8614947cc 100644--- a/server/drivers/pi-session-recovery.test.ts+++ b/server/drivers/pi-session-recovery.test.ts@@ -1,6 +1,6 @@-import { chmodSync, mkdtempSync, readFileSync } from "node:fs";+import { chmodSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";-import { join } from "node:path";+import { dirname, join } from "node:path";import { fileURLToPath } from "node:url";import { describe, expect, it } from "vitest";@@ -25,7 +25,10 @@ describe("Pi persisted session correctness", () => {const { instance, dump, recorder } = await setup(mode);const threadId = "resume-fails-" + mode;try {- const cursor = join(piSessionDir({ threadId }, "pi-recovery"), "missing.json");+ const cursor = join(piSessionDir({ threadId }, "pi-recovery"), "unavailable.json");+ // The file is there, so the turn resumes it; pi itself then refuses or cancels the switch.+ mkdirSync(dirname(cursor), { recursive: true });+ writeFileSync(cursor, "[]");const { turnId } = await instance.adapter.sendTurn({ threadId, text: "follow-up", resumeCursor: cursor,model: "openai/gpt-4o", recoveryText: "Prior conversation: important context.\nfollow-up" });const done = await recorder.until((event) => event.type === "turn.completed" && event.turnId === turnId);diff --git a/server/drivers/pi-session-spawn.test.ts b/server/drivers/pi-session-spawn.test.tsindex a05c588a01..7fd29d0e59 100644--- a/server/drivers/pi-session-spawn.test.ts+++ b/server/drivers/pi-session-spawn.test.ts@@ -1,6 +1,6 @@import { chmodSync, mkdirSync, mkdtempSync, readFileSync } from "node:fs";import { tmpdir } from "node:os";-import { join } from "node:path";+import { dirname, join } from "node:path";import { fileURLToPath } from "node:url";import { afterEach, describe, expect, it, vi } from "vitest";@@ -9,7 +9,7 @@ import { planBox, visibleInBox } from "../bot-box/plan.ts";import * as procs from "../procs.ts";import { recordEvents } from "../testing/events.ts";import { engineHomes } from "../engine-homes/engine-homes.ts";-import { piSessionDir } from "./pi-session.ts";+import { piSessionDir, piSessionPath } from "./pi-session.ts";import { PiDriver } from "./pi.ts";vi.mock("node:fs", async (original) => {@@ -60,16 +60,12 @@ describe("Pi session spawn setup", () => {expect(rows.find((row) => row.loadedHistory)?.loadedHistory).toEqual(["first"]);expect(rows.filter((row) => row.rpc).map((row) => row.rpc)).toEqual(["new_session", "switch_session"]);await engineHomes.within("thread-b", async () => {- await expect(instance.adapter.sendTurn({ threadId: "thread-b", botId: "bot-a", text: "steal", resumeCursor: first }))- .rejects.toThrow("another audience or conversation");+ expect(piSessionPath({ resumeCursor: first }, piSessionDir({ threadId: "thread-b", botId: "bot-a" }, "pi-session-spawn"))).toBeNull();});await send("thread-b", "another conversation");audience = "member:bob";await engineHomes.within("thread-a", async () => {- const before = spawned.mock.calls.length;- await expect(instance.adapter.sendTurn({ threadId: "thread-a", botId: "bot-a", text: "steal", resumeCursor: first }))- .rejects.toThrow("another audience or conversation");- expect(spawned.mock.calls.length).toBe(before);+ expect(piSessionPath({ resumeCursor: first }, piSessionDir({ threadId: "thread-a", botId: "bot-a" }, "pi-session-spawn"))).toBeNull();});await send("thread-a", "another audience");audience = "team";@@ -82,8 +78,9 @@ describe("Pi session spawn setup", () => {}expect(new Set(turns.map((turn) => turn.dir)).size).toBe(4);engineHomes.stop();- await expect(instance.adapter.sendTurn({ threadId: "different-thread", botId: "bot-a", text: "steal", resumeCursor: first }))- .rejects.toThrow("another audience or conversation");+ expect(piSessionPath({ resumeCursor: first }, piSessionDir({ threadId: "different-thread", botId: "bot-a" }, "pi-session-spawn"))).toBeNull();+ expect(piSessionPath({ resumeCursor: join(dirname(first), "gone.json") }, dirname(first))).toBeNull();+ expect(piSessionPath({ resumeCursor: first }, dirname(first))).toBe(first);} finally {recorder.stop();await instance.dispose();diff --git a/server/drivers/pi-session.ts b/server/drivers/pi-session.tsindex 136486079c..b338963f6b 100644--- a/server/drivers/pi-session.ts+++ b/server/drivers/pi-session.ts@@ -1,5 +1,5 @@import { createHash } from "node:crypto";-import { mkdirSync } from "node:fs";+import { existsSync, mkdirSync } from "node:fs";import { dirname, join, resolve } from "node:path";import { DATA_DIR } from "../config.ts";@@ -19,9 +19,11 @@ export function piTurnArgs(dir: string): string[] {return ["--mode", "rpc", "--session-dir", dir];}-/** Never resume another audience's or conversation's session, even with a stale cursor. */+/** Never resume another audience's or conversation's session, even with a stale cursor. Such a cursor, or one whose file+ * is gone (an older cursor, a conversation moved to another computer), starts a new session rather than failing every+ * turn of the conversation. */export function piSessionPath(turn: Pick<SendTurnInput, "resumeCursor" | "sessionReset">, dir: string): string | null {if (turn.sessionReset || typeof turn.resumeCursor !== "string") return null;- if (dirname(resolve(turn.resumeCursor)) !== resolve(dir)) throw new Error("Pi session belongs to another audience or conversation; reset this session before retrying");+ if (dirname(resolve(turn.resumeCursor)) !== resolve(dir) || !existsSync(turn.resumeCursor)) return null;return turn.resumeCursor;}