Files
buzz/desktop/tests/e2e/helpers/twoRelayHarness.ts
Tyler 61cc738ee8 feat(desktop+acp): spawn a harness per (agent, community) pair at GUI startup — warm sockets, lazy LLM pool (#2122)
Signed-off-by: Tyler Longwell <tlongwell@block.xyz>
Signed-off-by: Matt Toohey <contact@matttoohey.com>
Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
Signed-off-by: Tyler <109685178+tlongwell-block@users.noreply.github.com>
Co-authored-by: npub1jh9wn95s0472h86ahapupaf7m6kx4v9sx2n0atj2hltcfer8k06s5n3pyf <95cae996907d7cab9f5dbf43c0f53edeac6ab0b032a6feae4abfd784e467b3f5@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: Tyler Longwell <tlongwell@block.xyz>
Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1t2tgm7d8f995uqvmnm8h88sg3wnpp9a5xysjf6dg3tjmgt3ltulqdp8ehr <5a968df9a7494b4e019b9ecf739e088ba61097b4312124e9a88ae5b42e3f5f3e@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: Dawn <c6237ef84fa537c78dcee78efd2d4e59f728859c7f194da42ac51ededfa0be05@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co>
Co-authored-by: Matt Toohey <contact@matttoohey.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: npub1hwqy0rnujtl25dzmlhn8qwux4kr8sjhas3ugltx9j5dm5dwkp2dsqjhytw <bb80478e7c92feaa345bfde6703b86ad86784afd84788facc5951bba35d60a9b@buzz.block.builderlab.xyz>
2026-07-22 13:24:46 -07:00

168 lines
5.2 KiB
TypeScript

import { spawn, type ChildProcess } from "node:child_process";
import { createWriteStream } from "node:fs";
import { mkdtemp, readFile, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { setTimeout as delay } from "node:timers/promises";
export type RelayPorts = { main: number; health: number; metrics: number };
export type RelaySpec = {
name: string;
ports: RelayPorts;
databaseUrl: string;
redisUrl: string;
};
type OwnedProcess = { name: string; child: ChildProcess; logPath: string };
export class TwoRelayHarness {
readonly root: string;
readonly relays: readonly [RelaySpec, RelaySpec];
private readonly processes: OwnedProcess[] = [];
get ownedPids(): number[] {
return this.processes.flatMap(({ child }) =>
child.pid ? [child.pid] : [],
);
}
private constructor(root: string, relays: readonly [RelaySpec, RelaySpec]) {
this.root = root;
this.relays = relays;
}
static async create(relays: readonly [RelaySpec, RelaySpec]) {
return new TwoRelayHarness(
await mkdtemp(join(tmpdir(), "buzz-ae-e2e-")),
relays,
);
}
async startRelays(binary = process.env.BUZZ_E2E_RELAY_BIN) {
if (!binary)
throw new Error("BUZZ_E2E_RELAY_BIN is required for the live gate");
await Promise.all(
this.relays.map((relay) => this.startRelay(binary, relay)),
);
}
async startAcp(
name: string,
relayWsUrl: string,
privateKey: string,
extraEnv: NodeJS.ProcessEnv = {},
) {
const binary = process.env.BUZZ_E2E_ACP_BIN;
if (!binary)
throw new Error("BUZZ_E2E_ACP_BIN is required for the live gate");
return this.spawnOwned(name, binary, [], {
BUZZ_RELAY_URL: relayWsUrl,
BUZZ_PRIVATE_KEY: privateKey,
BUZZ_AUTH_TAG: "",
BUZZ_ACP_LAZY_POOL: "true",
BUZZ_ACP_AGENT_COMMAND: process.execPath,
BUZZ_ACP_AGENT_ARGS: resolve("tests/e2e/fixtures/fake-acp-agent.mjs"),
BUZZ_E2E_CLI_BIN: process.env.BUZZ_E2E_CLI_BIN,
...extraEnv,
});
}
async logs(): Promise<string> {
const chunks = await Promise.all(
this.processes.map(async ({ name, logPath }) => {
const body = await readFile(logPath, "utf8").catch(() => "<no log>");
return `===== ${name} =====\n${body}`;
}),
);
return chunks.join("\n");
}
private signal(child: ChildProcess, signal: NodeJS.Signals) {
if (child.exitCode !== null || child.signalCode !== null || !child.pid)
return;
if (process.platform === "win32") {
child.kill(signal);
return;
}
try {
process.kill(-child.pid, signal);
} catch (error) {
const code = (error as NodeJS.ErrnoException).code;
if (code !== "ESRCH") throw error;
}
}
private async stopChild(child: ChildProcess) {
if (child.exitCode !== null || child.signalCode !== null) return;
const exited = new Promise<void>((resolveExit) =>
child.once("exit", () => resolveExit()),
);
this.signal(child, "SIGTERM");
if (await Promise.race([exited.then(() => true), delay(5_000, false)]))
return;
this.signal(child, "SIGKILL");
if (!(await Promise.race([exited.then(() => true), delay(2_000, false)]))) {
throw new Error(`child process ${child.pid ?? "unknown"} did not exit`);
}
}
async stop() {
await Promise.all(
[...this.processes].reverse().map(({ child }) => this.stopChild(child)),
);
await rm(this.root, { recursive: true, force: true });
}
private async startRelay(binary: string, relay: RelaySpec) {
const child = this.spawnOwned(relay.name, binary, [], {
DATABASE_URL: relay.databaseUrl,
REDIS_URL: relay.redisUrl,
RELAY_URL: `ws://127.0.0.1:${relay.ports.main}`,
BUZZ_BIND_ADDR: `127.0.0.1:${relay.ports.main}`,
BUZZ_HEALTH_PORT: String(relay.ports.health),
BUZZ_METRICS_PORT: String(relay.ports.metrics),
BUZZ_REQUIRE_AUTH_TOKEN: "false",
BUZZ_RECONCILE_CHANNELS: "true",
});
await this.waitForHealth(relay, child);
}
private spawnOwned(
name: string,
command: string,
args: string[],
env: NodeJS.ProcessEnv,
) {
const logPath = join(this.root, `${name}.log`);
const child = spawn(command, args, {
cwd: resolve(".."),
env: { ...process.env, ...env, RUST_LOG: process.env.RUST_LOG ?? "info" },
stdio: ["ignore", "pipe", "pipe"],
detached: process.platform !== "win32",
});
const log = createWriteStream(logPath, { flags: "a" });
child.stdout?.pipe(log, { end: false });
child.stderr?.pipe(log, { end: false });
child.on("exit", () => log.end());
this.processes.push({ name, child, logPath });
return child;
}
private async waitForHealth(relay: RelaySpec, child: ChildProcess) {
const deadline = Date.now() + 30_000;
while (Date.now() < deadline) {
if (child.exitCode !== null || child.signalCode !== null) {
throw new Error(`${relay.name} exited before readiness`);
}
try {
const response = await fetch(
`http://127.0.0.1:${relay.ports.health}/_readiness`,
);
if (response.ok) return;
} catch {}
await delay(100);
}
throw new Error(`${relay.name} was not ready within 30s`);
}
}