Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Test that an idle Hermes conversation closing leaves the shared gateway running

Bugfixer 26831d6d40da8
diff --git a/server/drivers/acp/hermes-gateway.test.ts b/server/drivers/acp/hermes-gateway.test.ts
new file mode 100644
index 0000000000..55cf012ad7
--- /dev/null
+++ b/server/drivers/acp/hermes-gateway.test.ts
@@ -0,0 +1,90 @@
+// The Hermes gateway one conversation's `hermes acp` starts is shared by every conversation: closing one idle
+// conversation's engine must leave that gateway, and a turn another conversation is running, alive.
+import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { dirname, join } from "node:path";
+import { fileURLToPath } from "node:url";
+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";
+import { recordEvents, type EventRecorder } from "../../testing/events.ts";
+import { HermesAgentDriver } from "./hermes.ts";
+
+const FAKE_CLI = join(dirname(fileURLToPath(import.meta.url)), "../../testing/fake-acp-cli.ts");
+
+let scratch: string;
+let gatewayFile: string;
+let instance: ProviderInstance | undefined;
+let recorder: EventRecorder | undefined;
+
+const alive = (pid: number) => {
+ try { process.kill(pid, 0); return true; } catch { return false; }
+};
+
+beforeEach(() => {
+ ensureDirs();
+ scratch = mkdtempSync(join(tmpdir(), "omb-hermes-gateway-"));
+ gatewayFile = join(scratch, "gateway.pid");
+});
+
+afterEach(async () => {
+ recorder?.stop();
+ await instance?.dispose();
+ instance = undefined;
+ recorder = undefined;
+ // The fixture writes the gateway's pid as it starts it, so a test that failed before reading it still stops it.
+ const gateway = existsSync(gatewayFile) ? Number(readFileSync(gatewayFile, "utf8")) : 0;
+ if (gateway) try { process.kill(-gateway, "SIGKILL"); } catch {}
+ vi.unstubAllEnvs();
+ await removeTempDir(scratch);
+});
+
+it.runIf(process.platform === "linux")("closing one idle conversation leaves the shared Hermes gateway and another conversation's turn running", async () => {
+ // Ten seconds is the shortest idle close there is.
+ vi.stubEnv("OMB_ACP_SESSION_IDLE_MIN_MS", "10000");
+ vi.stubEnv("OMB_ACP_SESSION_IDLE_MS", "10000");
+ const hold = join(scratch, "prompt.hold");
+ instance = await HermesAgentDriver.create({
+ instanceId: "hermes-gateway",
+ displayName: "Gateway fixture",
+ enabled: true,
+ environment: {
+ HOME: scratch,
+ USERPROFILE: scratch,
+ HERMES_HOME: join(scratch, ".hermes"),
+ OPENMAUSBOT_PROBE_LOCAL_INJECT: "0",
+ FAKE_ACP_HOLD_FILE: hold,
+ FAKE_ACP_GATEWAY_PID_FILE: gatewayFile,
+ },
+ config: { cli: FAKE_CLI, fullAuto: true },
+ } as never) as ProviderInstance;
+ const events = recordEvents(instance.adapter);
+ recorder = events;
+ const adapter = instance.adapter;
+ /** Starts a turn held by the hold file and returns it with the pid of the `hermes acp` running it. */
+ const heldTurn = async (threadId: string) => {
+ writeFileSync(hold, "hold");
+ const { turnId } = await adapter.sendTurn({ threadId, text: "hello", cwd: scratch });
+ let pid: number | undefined;
+ await vi.waitFor(() => expect(pid = adapter.enginePid?.(threadId)).toBeGreaterThan(0), { timeout: 10_000 });
+ return { turnId, pid: pid! };
+ };
+ const completed = (turnId: string) => events.until((event) => event.turnId === turnId && event.type === "turn.completed");
+
+ const first = await heldTurn("conversation-a");
+ let gatewayPid = 0;
+ await vi.waitFor(() => expect(gatewayPid = Number(readFileSync(gatewayFile, "utf8"))).toBeGreaterThan(0), { timeout: 10_000 });
+ rmSync(hold);
+ expect(await completed(first.turnId)).toMatchObject({ ok: true });
+
+ const second = await heldTurn("conversation-b");
+ expect(second.pid).not.toBe(first.pid);
+ await vi.waitFor(() => expect(alive(first.pid)).toBe(false), { timeout: 20_000, interval: 200 });
+ expect(alive(gatewayPid), "the shared gateway").toBe(true);
+ expect(alive(second.pid), "conversation B's engine").toBe(true);
+
+ rmSync(hold);
+ expect(await completed(second.turnId)).toMatchObject({ ok: true });
+}, 45_000);
diff --git a/server/testing/fake-acp-cli.ts b/server/testing/fake-acp-cli.ts
index ea6285a25c..e93d9fbcd7 100755
--- a/server/testing/fake-acp-cli.ts
+++ b/server/testing/fake-acp-cli.ts
@@ -84,6 +84,8 @@
// FAKE_ACP_CANCEL_STOP_REASON the stopReason a cancelled hanging prompt answers (default cancelled; OpenCode answers end_turn)
// FAKE_ACP_HANG_ONCE_FILE with FAKE_ACP_HANG_ON, only the first matching prompt hangs; it writes this file
// FAKE_ACP_HOLD_FILE while this file exists, a session/prompt waits before it is answered
+// FAKE_ACP_GATEWAY_PID_FILE the first `acp` process starts a stand-in for Hermes' shared gateway the way Hermes does
+// (a session of its own, HERMES_GATEWAY_DETACHED=1, `gateway run`) and writes its pid here; later ones leave it be
// FAKE_ACP_AGY_STEPS_FILE JSON [[stepType, text], …] re-read per prompt and appended to the session's log under
// GEMINI_HOME (antigravity-acp/conversations/<session>.db, a folder made at startup), as agy_acp_server 1.4.5
// writes its trajectory
@@ -393,6 +395,16 @@ if (argv[0] === "status" || argv[0] === "whoami") {
console.log(JSON.stringify({ isAuthenticated: authenticated }));
process.exit(0);
}
+const gatewayPidFile = process.env.FAKE_ACP_GATEWAY_PID_FILE;
+if (gatewayPidFile && argv[0] === "acp" && !existsSync(gatewayPidFile)) {
+ const gateway = spawn(process.execPath, ["-e", "setInterval(() => {}, 1 << 30)", "gateway", "run", "--quiet"], {
+ detached: true,
+ stdio: "ignore",
+ env: { ...process.env, HERMES_GATEWAY_DETACHED: "1" },
+ });
+ gateway.unref();
+ writeFileSync(gatewayPidFile, String(gateway.pid));
+}
const catalogLock = process.env.FAKE_ACP_MODELS_LOCK;
if (catalogLock && (argv[0] === "models" || argv[0] === "acp") && !argv.includes("--refresh") && process.env.OPENCODE_DISABLE_MODELS_FETCH !== "1") {
writeFileSync(catalogLock, String(process.pid));