Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(pi): await confirmed CLI termination before completing turns
Sebastian B00510c44932a
diff --git a/server/drivers/pi-lifecycle.test.ts b/server/drivers/pi-lifecycle.test.tsnew file mode 100644index 0000000000..cf4d1c50c3--- /dev/null+++ b/server/drivers/pi-lifecycle.test.ts@@ -0,0 +1,63 @@+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 { ensureDirs } from "../config.ts";+import type { ProviderInstance } from "../contracts.ts";+import { removeTempDir } from "../testing/cleanup.ts";+import { recordEvents, type EventRecorder } from "../testing/events.ts";+import { PiDriver } from "./pi.ts";++const CLI = fileURLToPath(new URL("../testing/fake-pi-lifecycle.mjs", import.meta.url));+let dir: string;+let instance: ProviderInstance;+let events: EventRecorder;+let ownedPid: number | undefined;++beforeEach(async () => {+ ensureDirs();+ chmodSync(CLI, 0o755);+ dir = mkdtempSync(join(tmpdir(), "omb-pi-lifecycle-"));+ instance = await PiDriver.create({+ instanceId: "pi-lifecycle",+ displayName: "Pi lifecycle fixture",+ enabled: true,+ config: { cli: CLI, fullAuto: false },+ environment: { FAKE_PI_LIFECYCLE_DUMP: join(dir, "rpc.jsonl") },+ });+ events = recordEvents(instance.adapter);+ ownedPid = undefined;+});++afterEach(async () => {+ await instance.dispose();+ if (ownedPid) {+ try { process.kill(ownedPid, "SIGKILL"); } catch {}+ }+ events.stop();+ await removeTempDir(dir);+});++it.each(["interrupt", "stopAll", "dispose"])("keeps ownership through %s until the CLI has stopped", async (action) => {+ await instance.adapter.sendTurn({ threadId: "cancel", text: "wait" });+ await events.until((event) => event.type === "item.started");+ const { pid } = JSON.parse(readFileSync(join(dir, "rpc.jsonl"), "utf8").trim()) as { pid: number };+ ownedPid = pid;+ let runningAtCompletion = false;+ instance.adapter.onEvent((event) => {+ if (event.type !== "turn.completed") return;+ try { process.kill(pid, 0); runningAtCompletion = true; } catch {}+ });+ const interrupted = action === "interrupt" ? instance.adapter.interruptTurn("cancel")+ : action === "stopAll" ? instance.adapter.stopAll() : instance.dispose();+ const ownedWhileStopping = instance.adapter.hasSession("cancel");+ const disposed = instance.dispose();+ await Promise.all([interrupted, disposed]);+ expect(ownedWhileStopping).toBe(true);+ expect(() => process.kill(pid, 0)).toThrow();+ expect(runningAtCompletion).toBe(false);+ expect(events.events.filter((event) => event.type === "turn.completed")).toMatchObject([+ { ok: true, stopReason: "cancelled" },+ ]);+});diff --git a/server/drivers/pi-rpc.ts b/server/drivers/pi-rpc.tsnew file mode 100644index 0000000000..23b15d24ea--- /dev/null+++ b/server/drivers/pi-rpc.ts@@ -0,0 +1,112 @@+/** omp's RPC v2 splits a frame over 1 MiB into base64 `rpc_chunk` pieces (`chunkId`/`count`/+ * `byteLength` per frame, sequential `index`, `data` per piece). Any inconsistency drops the sequence. */+export class PiRpcChunks {+ /** omp's advertised cap on one reassembled frame. */+ static readonly MAX_BYTES = 64 * 1024 * 1024;+ /** omp's cap on one decoded piece, which also bounds the piece count. */+ static readonly MAX_PIECE_BYTES = 256 * 1024;+ static readonly MAX_COUNT = PiRpcChunks.MAX_BYTES / PiRpcChunks.MAX_PIECE_BYTES;+ private chunkId: string | null = null;+ private count = 0;+ private byteLength = 0;+ private bytes = 0;+ private parts: Buffer[] = [];++ accept(message: Record<string, unknown>): Record<string, unknown> | null {+ const { chunkId, index, count, byteLength, data } = message;+ if (+ typeof chunkId !== "string" || chunkId.length === 0 || typeof data !== "string" ||+ data.length === 0 || data.length > Math.ceil(PiRpcChunks.MAX_PIECE_BYTES / 3) * 4 ||+ !Number.isInteger(index) || !Number.isInteger(count) || !Number.isInteger(byteLength) ||+ (index as number) < 0 || (count as number) < 2 || (count as number) > PiRpcChunks.MAX_COUNT ||+ (index as number) >= (count as number) ||+ (byteLength as number) < 0 || (byteLength as number) > PiRpcChunks.MAX_BYTES+ ) {+ this.reset();+ return null;+ }+ if (chunkId !== this.chunkId) {+ if (index !== 0) {+ this.reset();+ return null;+ }+ this.chunkId = chunkId;+ this.count = count as number;+ this.byteLength = byteLength as number;+ this.parts = [];+ this.bytes = 0;+ } else if (count !== this.count || byteLength !== this.byteLength) {+ this.reset();+ return null;+ }+ const part = Buffer.from(data, "base64");+ // An empty piece would let a sequence grow without approaching `byteLength`.+ if (+ index !== this.parts.length || part.length === 0 || part.length > PiRpcChunks.MAX_PIECE_BYTES ||+ this.bytes + part.length > this.byteLength+ ) {+ this.reset();+ return null;+ }+ this.parts.push(part);+ this.bytes += part.length;+ if (this.parts.length < this.count) return null;+ const complete = this.bytes === this.byteLength;+ const text = Buffer.concat(this.parts).toString("utf8");+ this.reset();+ if (!complete) return null;+ try {+ const frame = JSON.parse(text) as unknown;+ return frame && typeof frame === "object" && !Array.isArray(frame)+ ? frame as Record<string, unknown>+ : null;+ } catch {+ return null;+ }+ }++ private reset(): void {+ this.chunkId = null;+ this.count = 0;+ this.byteLength = 0;+ this.bytes = 0;+ this.parts = [];+ }+}++export function decodePiFrame(line: string, chunks: PiRpcChunks): Record<string, unknown> | null {+ let parsed: unknown;+ try {+ parsed = JSON.parse(line);+ } catch {+ return null;+ }+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null;+ const frame = parsed as Record<string, unknown>;+ return frame.type === "rpc_chunk" ? chunks.accept(frame) : frame;+}++const PI_MAX_LINE = PiRpcChunks.MAX_BYTES;++export function piLineReader(onLine: (line: string) => boolean | void): (chunk: string) => void {+ let buf = "";+ let dropping = false;+ return (chunk) => {+ buf += chunk;+ let nl: number;+ while ((nl = buf.indexOf("\n")) !== -1) {+ const line = buf.slice(0, nl);+ buf = buf.slice(nl + 1);+ if (dropping) {+ dropping = false;+ continue;+ }+ if (line.length > PI_MAX_LINE || !line.trim()) continue;+ if (onLine(line)) return;+ }+ if (buf.length > PI_MAX_LINE) {+ buf = "";+ dropping = true;+ }+ };+}diff --git a/server/drivers/pi.ts b/server/drivers/pi.tsindex 4932b1f630..ee15ebfb09 100644--- a/server/drivers/pi.ts+++ b/server/drivers/pi.ts@@ -47,6 +47,9 @@ import { appendNative } from "./native.ts";import { allowsTool, parseToolScope } from "../../shared/tool-scope.ts";import { buildMcpServers } from "./pi-mcp-servers.ts";+import { decodePiFrame, PiRpcChunks, piLineReader } from "./pi-rpc.ts";+export { decodePiFrame, PiRpcChunks, piLineReader } from "./pi-rpc.ts";+const DRIVER_KIND = "piAgent";const PI_ARGS = ["--mode", "rpc", "--no-session"];const PI_MODEL_UPDATE_ARGS = ["update", "--models", "--no-approve"];@@ -136,119 +139,6 @@ export function parsePiCatalog(stdout: string, fallbackDefault = ""): ModelCatalreturn { default: fallbackDefault, options: [] };}-/** omp's RPC v2 splits a frame over 1 MiB into base64 `rpc_chunk` pieces (`chunkId`/`count`/- * `byteLength` per frame, sequential `index`, `data` per piece). Any inconsistency drops the sequence. */-export class PiRpcChunks {- /** omp's advertised cap on one reassembled frame. */- static readonly MAX_BYTES = 64 * 1024 * 1024;- /** omp's cap on one decoded piece, which also bounds the piece count. */- static readonly MAX_PIECE_BYTES = 256 * 1024;- static readonly MAX_COUNT = PiRpcChunks.MAX_BYTES / PiRpcChunks.MAX_PIECE_BYTES;- private chunkId: string | null = null;- private count = 0;- private byteLength = 0;- private bytes = 0;- private parts: Buffer[] = [];-- accept(message: Record<string, unknown>): Record<string, unknown> | null {- const { chunkId, index, count, byteLength, data } = message;- if (- typeof chunkId !== "string" || chunkId.length === 0 || typeof data !== "string" ||- data.length === 0 || data.length > Math.ceil(PiRpcChunks.MAX_PIECE_BYTES / 3) * 4 ||- !Number.isInteger(index) || !Number.isInteger(count) || !Number.isInteger(byteLength) ||- (index as number) < 0 || (count as number) < 2 || (count as number) > PiRpcChunks.MAX_COUNT ||- (index as number) >= (count as number) ||- (byteLength as number) < 0 || (byteLength as number) > PiRpcChunks.MAX_BYTES- ) {- this.reset();- return null;- }- if (chunkId !== this.chunkId) {- if (index !== 0) {- this.reset();- return null;- }- this.chunkId = chunkId;- this.count = count as number;- this.byteLength = byteLength as number;- this.parts = [];- this.bytes = 0;- } else if (count !== this.count || byteLength !== this.byteLength) {- this.reset();- return null;- }- const part = Buffer.from(data, "base64");- // An empty piece would let a sequence grow without approaching `byteLength`.- if (- index !== this.parts.length || part.length === 0 || part.length > PiRpcChunks.MAX_PIECE_BYTES ||- this.bytes + part.length > this.byteLength- ) {- this.reset();- return null;- }- this.parts.push(part);- this.bytes += part.length;- if (this.parts.length < this.count) return null;- const complete = this.bytes === this.byteLength;- const text = Buffer.concat(this.parts).toString("utf8");- this.reset();- if (!complete) return null;- try {- const frame = JSON.parse(text) as unknown;- return frame && typeof frame === "object" && !Array.isArray(frame)- ? frame as Record<string, unknown>- : null;- } catch {- return null;- }- }-- private reset(): void {- this.chunkId = null;- this.count = 0;- this.byteLength = 0;- this.bytes = 0;- this.parts = [];- }-}--export function decodePiFrame(line: string, chunks: PiRpcChunks): Record<string, unknown> | null {- let parsed: unknown;- try {- parsed = JSON.parse(line);- } catch {- return null;- }- if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null;- const frame = parsed as Record<string, unknown>;- return frame.type === "rpc_chunk" ? chunks.accept(frame) : frame;-}--const PI_MAX_LINE = PiRpcChunks.MAX_BYTES;--export function piLineReader(onLine: (line: string) => boolean | void): (chunk: string) => void {- let buf = "";- let dropping = false;- return (chunk) => {- buf += chunk;- let nl: number;- while ((nl = buf.indexOf("\n")) !== -1) {- const line = buf.slice(0, nl);- buf = buf.slice(nl + 1);- if (dropping) {- dropping = false;- continue;- }- if (line.length > PI_MAX_LINE || !line.trim()) continue;- if (onLine(line)) return;- }- if (buf.length > PI_MAX_LINE) {- buf = "";- dropping = true;- }- };-}-export function splitPiModel(id: string): { provider: string; modelId: string } | null {const inject = decodeInjectId(id);if (inject) return { provider: inject.host, modelId: inject.model };@@ -563,6 +453,7 @@ export const PiDriver: ProviderDriver<PiConfig> = {const pending = new Map<string, (decision: { behavior: "allow" | "deny" | "answer"; message?: string }) => void>();const askTimers = new Set<ReturnType<typeof setTimeout>>();let settled = false;+ let completion: Promise<void> | null = null;let endStopReason: string | undefined;let endUsage: { input?: number; output?: number } | undefined;let endErrorMessage: string | undefined;@@ -646,8 +537,8 @@ export const PiDriver: ProviderDriver<PiConfig> = {emit({ ...base(threadId, turnId), type: "item.completed", itemType: "assistant_text", text });};- const settle = (ok: boolean, stopReason?: string | null, usage?: { input?: number; output?: number }) => {- if (settled) return;+ const settle = (ok: boolean, stopReason?: string | null, usage?: { input?: number; output?: number }): Promise<void> => {+ if (completion) return completion;settled = true;if (agentEndTimer) {clearTimeout(agentEndTimer);@@ -656,24 +547,30 @@ export const PiDriver: ProviderDriver<PiConfig> = {for (const timer of askTimers) clearTimeout(timer);askTimers.clear();pending.clear();- flushAssistantText();- emit({- ...base(threadId, turnId),- type: "turn.completed",- ok,- stopReason: stopReason ?? (ok ? "end_turn" : "failed"),- ...(usage ? { usage: { input: usage.input ?? 0, output: usage.output ?? 0 } } : {}),- });try {child.stdin.end();} catch {}- try {- killCliTree(child);- } catch {}- try {- rmSync(mcpTempDir, { recursive: true, force: true });- } catch {}- active.delete(threadId);+ completion = (async () => {+ if (!await killCliTree(child, 0)) throw new Error("Pi process termination could not be confirmed");+ rejectWaiters(new Error("pi turn ended"));+ try {+ rmSync(mcpTempDir, { recursive: true, force: true });+ } catch {}+ active.delete(threadId);+ flushAssistantText();+ emit({+ ...base(threadId, turnId),+ type: "turn.completed",+ ok,+ stopReason: stopReason ?? (ok ? "end_turn" : "failed"),+ ...(usage ? { usage: { input: usage.input ?? 0, output: usage.output ?? 0 } } : {}),+ });+ })();+ void completion.catch((error: Error) => {+ completion = null;+ emit({ ...base(threadId, turnId), type: "runtime.error", message: error.message });+ });+ return completion;};const finishRun = () => {@@ -694,10 +591,7 @@ export const PiDriver: ProviderDriver<PiConfig> = {try {send({ type: "abort" });} catch {}- try {- killCliTree(child);- } catch {}- settle(true, "cancelled");+ return settle(true, "cancelled");};// pi queues a steer into the running agent without aborting the step, answers success only after@@ -723,6 +617,7 @@ export const PiDriver: ProviderDriver<PiConfig> = {const onEvent = (evt: PiEvent) => {appendNative(threadId, { dir: "in", source: "pi.rpc", msg: evt });+ if (settled) return;switch (evt.type) {case "response": {const key = typeof evt.id === "string" && responseWaiters.has(evt.id) ? evt.id : evt.command;@@ -1072,7 +967,7 @@ export const PiDriver: ProviderDriver<PiConfig> = {queueing: true,},sendTurn,- interruptTurn: async (threadId) => active.get(threadId)?.stop(),+ interruptTurn: async (threadId) => { await active.get(threadId)?.stop(); },respondToRequest: async (threadId, requestId, decision) => {const entry = active.get(threadId);const answer = entry?.pending.get(requestId);@@ -1094,7 +989,7 @@ export const PiDriver: ProviderDriver<PiConfig> = {},hasSession: (threadId) => active.has(threadId),stopAll: async () => {- for (const { stop } of active.values()) stop();+ await Promise.all([...active.values()].map(({ stop }) => Promise.resolve(stop())));},onEvent: (listener) => {listeners.add(listener);@@ -1102,7 +997,7 @@ export const PiDriver: ProviderDriver<PiConfig> = {},},dispose: async () => {- for (const { stop } of active.values()) stop();+ await Promise.all([...active.values()].map(({ stop }) => Promise.resolve(stop())));listeners.clear();},};diff --git a/server/testing/fake-pi-lifecycle.mjs b/server/testing/fake-pi-lifecycle.mjsnew file mode 100755index 0000000000..72bf9e12d7--- /dev/null+++ b/server/testing/fake-pi-lifecycle.mjs@@ -0,0 +1,25 @@+#!/usr/bin/env node+import { appendFileSync } from "node:fs";+import { createInterface } from "node:readline";++const dump = (row) => appendFileSync(process.env.FAKE_PI_LIFECYCLE_DUMP, JSON.stringify(row) + "\n");+const send = (frame) => process.stdout.write(JSON.stringify(frame) + "\n");+const reply = (command, data) => send({ type: "response", command, success: true, data });+const model = { provider: "fixture", id: "default" };++process.on("SIGTERM", () => {});+setInterval(() => {}, 1_000);+createInterface({ input: process.stdin }).on("line", (line) => {+ const cmd = JSON.parse(line);+ if (cmd.type === "get_available_models") {+ process.stdout.write(JSON.stringify({ type: "response", command: cmd.type, success: true, data: { models: [model] } }) + "\n", () => process.exit(0));+ }+ if (["get_state", "new_session", "switch_session"].includes(cmd.type)) {+ reply(cmd.type, { sessionFile: "fixture-session.jsonl", model });+ }+ if (cmd.type === "prompt") {+ dump({ prompt: cmd.message, pid: process.pid });+ reply(cmd.type);+ send({ type: "tool_execution_start", toolCallId: "ready", toolName: "fixture" });+ }+});