Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(pi): cancel completion fallback during continuation and maintenance
Sebastian Bb1585d9f2580
diff --git a/server/drivers/pi-lifecycle.test.ts b/server/drivers/pi-lifecycle.test.tsindex ef2c34bc9f..706f38db2a 100644--- a/server/drivers/pi-lifecycle.test.ts+++ b/server/drivers/pi-lifecycle.test.ts@@ -2,7 +2,7 @@ import { chmodSync, mkdtempSync, readFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";import { fileURLToPath } from "node:url";-import { afterEach, beforeEach, expect, it } from "vitest";+import { afterEach, beforeEach, expect, it, vi } from "vitest";import { ensureDirs } from "../config.ts";import type { ProviderInstance } from "../contracts.ts";import { removeTempDir } from "../testing/cleanup.ts";@@ -80,3 +80,33 @@ it.each([await events.until((event) => event.type === "turn.completed");expect(events.events.find((event) => event.type === "session.started")).toMatchObject({ model: confirmed });});++it.each(["retry", "nonterminal", "compaction_start", "auto_compaction_start"])(+ "keeps a turn running beyond the completion grace after %s",+ async (kind) => {+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "Date"] });+ try {+ await instance.adapter.sendTurn({ threadId: "maintenance", text: `maintenance:${kind}` });+ await events.until((event) => event.type === "content.delta" && event.delta === "maintenance-ready");+ await vi.advanceTimersByTimeAsync(60_001);+ expect(await instance.adapter.steer!("maintenance", "complete")).toBe("steered");+ } finally {+ vi.useRealTimers();+ }+ const completed = await events.until((event) => event.type === "turn.completed");+ expect(completed).toMatchObject({ ok: true, stopReason: "end_turn" });+ expect(events.events.filter((event) => event.type === "turn.completed")).toHaveLength(1);+ },+);++it("retains the completion fallback when no continuation or maintenance arrives", async () => {+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "Date"] });+ try {+ await instance.adapter.sendTurn({ threadId: "legacy", text: "maintenance:legacy" });+ await events.until((event) => event.type === "content.delta" && event.delta === "maintenance-ready");+ await vi.advanceTimersByTimeAsync(60_001);+ } finally {+ vi.useRealTimers();+ }+ expect(await events.until((event) => event.type === "turn.completed")).toMatchObject({ ok: true, stopReason: "end_turn" });+});diff --git a/server/drivers/pi.ts b/server/drivers/pi.tsindex 866bafaa3e..16708ecc1f 100644--- a/server/drivers/pi.ts+++ b/server/drivers/pi.ts@@ -734,6 +734,8 @@ export const PiDriver: ProviderDriver<PiConfig> = {case "compaction_end":case "auto_compaction_start":case "auto_compaction_end": {+ if (agentEndTimer) clearTimeout(agentEndTimer);+ agentEndTimer = null;// pi compaction (manual or automatic) can absorb the turn that carried the standing prompt.if (sessionReady && sessionFile) {compactionObserved = true;@@ -745,6 +747,8 @@ export const PiDriver: ProviderDriver<PiConfig> = {{// A killed child can still flush one buffered frame after the turn settled.if (settled) return;+ if (agentEndTimer) clearTimeout(agentEndTimer);+ agentEndTimer = null;const sr = evt.message?.stopReason;// toolUse: pi ran a tool and auto-continues; the next turn_end carries the outcome.if (sr === "toolUse" || sr === "tool_use" || sr === "tool_calls") return;@@ -753,19 +757,21 @@ export const PiDriver: ProviderDriver<PiConfig> = {endErrorMessage = evt.message?.errorMessage;// pi can schedule maintenance (overflow-recovery compaction) after the final turn_end;// only agent_end proves the session is done rewriting itself.- if (agentEndTimer) clearTimeout(agentEndTimer);agentEndTimer = setTimeout(finishRun, PI_AGENT_END_GRACE_MS);agentEndTimer.unref?.();return;}+ case "agent_start": {+ if (agentEndTimer) clearTimeout(agentEndTimer);+ agentEndTimer = null;+ return;+ }case "agent_end": {// Non-terminal agent_end: upstream pi sets willRetry: true before an automatic retry; the omp// fork sets isTerminal: false while maintenance or async delivery is scheduled.+ if (agentEndTimer) clearTimeout(agentEndTimer);+ agentEndTimer = null;if (evt.isTerminal === false || evt.willRetry === true) return;- if (agentEndTimer) {- clearTimeout(agentEndTimer);- agentEndTimer = null;- }finishRun();return;}diff --git a/server/testing/fake-pi-lifecycle.mjs b/server/testing/fake-pi-lifecycle.mjsindex f2fe414681..c4c7dd0dbc 100755--- a/server/testing/fake-pi-lifecycle.mjs+++ b/server/testing/fake-pi-lifecycle.mjs@@ -26,10 +26,25 @@ createInterface({ input: process.stdin }).on("line", (line) => {dump({ prompt: cmd.message, pid: process.pid });reply(cmd.type);send({ type: "tool_execution_start", toolCallId: "ready", toolName: "fixture" });+ if (cmd.message.startsWith("maintenance:")) {+ send({ type: "turn_end", message: { stopReason: "end_turn" } });+ const kind = cmd.message.slice("maintenance:".length);+ if (kind === "retry") send({ type: "agent_end", willRetry: true });+ else if (kind === "nonterminal") send({ type: "agent_end", isTerminal: false });+ else send({ type: kind });+ send({ type: "message_update", assistantMessageEvent: { type: "text_delta", delta: "maintenance-ready" } });+ return;+ }if (cmd.message !== "wait") {send({ type: "turn_end", message: { stopReason: "end_turn" } });send({ type: "agent_end", isTerminal: true });send({ type: "agent_settled" });}}+ if (cmd.type === "steer") {+ send({ type: "response", command: cmd.type, id: cmd.id, success: true });+ send({ type: "turn_end", message: { stopReason: "end_turn" } });+ send({ type: "agent_end", isTerminal: true });+ send({ type: "agent_settled" });+ }});