Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(pi): keep resuming a session file pi has not written yet
milind-soni31ddcdc23f5a
diff --git a/server/drivers/pi-session-recovery.test.ts b/server/drivers/pi-session-recovery.test.tsindex c8614947cc..4779c96901 100644--- a/server/drivers/pi-session-recovery.test.ts+++ b/server/drivers/pi-session-recovery.test.ts@@ -1,6 +1,6 @@-import { chmodSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";+import { chmodSync, mkdtempSync, readFileSync } from "node:fs";import { tmpdir } from "node:os";-import { dirname, join } from "node:path";+import { join } from "node:path";import { fileURLToPath } from "node:url";import { describe, expect, it } from "vitest";@@ -25,10 +25,7 @@ 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"), "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 cursor = join(piSessionDir({ threadId }, "pi-recovery"), "missing.json");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 7fd29d0e59..e63af946d2 100644--- a/server/drivers/pi-session-spawn.test.ts+++ b/server/drivers/pi-session-spawn.test.ts@@ -79,7 +79,6 @@ describe("Pi session spawn setup", () => {expect(new Set(turns.map((turn) => turn.dir)).size).toBe(4);engineHomes.stop();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();diff --git a/server/drivers/pi-session.ts b/server/drivers/pi-session.tsindex b338963f6b..48651da0d1 100644--- a/server/drivers/pi-session.ts+++ b/server/drivers/pi-session.ts@@ -1,5 +1,5 @@import { createHash } from "node:crypto";-import { existsSync, mkdirSync } from "node:fs";+import { mkdirSync } from "node:fs";import { dirname, join, resolve } from "node:path";import { DATA_DIR } from "../config.ts";@@ -19,11 +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. 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. */+/** Never resume another audience's or conversation's session, even with a stale cursor. Such a cursor (an older one, or+ * one from 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) || !existsSync(turn.resumeCursor)) return null;+ if (dirname(resolve(turn.resumeCursor)) !== resolve(dir)) return null;return turn.resumeCursor;}