openpod

An opencode plugin that spins up a dedicated virtual machine for each session to isolate agent work.
git clone https://unitehenry.com/git/openpod.git
Log | Files | Refs | README

commit 8bc7bd9c7d3730ec26cb59a0d706126a5669d9da
parent c41cd39eb0dee37e4735534d77727a1254f34765
Author: Henry Unite <unitehenry@gmail.com>
Date:   Sun, 15 Nov 2026 19:40:49 +0000

refactor(context): add async fs and exec helpers

Add promise-based helpers for checking paths and running commands so callers no longer need synchronous fs or child_process APIs.

Diffstat:
Acontext/exec.test.ts | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acontext/exec.ts | 38++++++++++++++++++++++++++++++++++++++
Acontext/fs.test.ts | 32++++++++++++++++++++++++++++++++
Acontext/fs.ts | 10++++++++++
4 files changed, 181 insertions(+), 0 deletions(-)

diff --git a/context/exec.test.ts b/context/exec.test.ts @@ -0,0 +1,101 @@ +import { describe, it, expect, beforeEach, vi } from "vitest"; +import { execFile } from "node:child_process"; + +import { exec } from "./exec"; + +vi.mock("node:child_process", async (importOriginal) => { + const actual = await importOriginal<typeof import("node:child_process")>(); + return { + ...actual, + execFile: vi.fn(), + }; +}); + +describe("exec", () => { + const execFileMock = execFile as unknown as ReturnType<typeof vi.fn>; + + beforeEach(() => { + vi.clearAllMocks(); + }); + + it("resolves stdout, stderr, and status for a successful command", async () => { + execFileMock.mockImplementation( + ( + _command: string, + _args: readonly string[], + _options: unknown, + callback: (error: unknown, stdout: string, stderr: string) => void, + ) => callback(null, "hello\n", ""), + ); + + const result = await exec("git", ["status"]); + + expect(result).toEqual({ + status: 0, + stdout: "hello\n", + stderr: "", + }); + expect(execFileMock).toHaveBeenCalledWith( + "git", + ["status"], + expect.objectContaining({ maxBuffer: 10 * 1024 * 1024 }), + expect.any(Function), + ); + }); + + it("resolves a numeric exit status instead of rejecting", async () => { + execFileMock.mockImplementation( + ( + _command: string, + _args: readonly string[], + _options: unknown, + callback: ( + error: { code: number }, + stdout: string, + stderr: string, + ) => void, + ) => callback({ code: 2 }, "", "failed"), + ); + + const result = await exec("limactl", ["list", "missing"]); + + expect(result).toEqual({ + status: 2, + stdout: "", + stderr: "failed", + }); + }); + + it("uses status 1 for non-numeric execution errors", async () => { + execFileMock.mockImplementation( + ( + _command: string, + _args: readonly string[], + _options: unknown, + callback: ( + error: { code: string }, + stdout: string, + stderr: string, + ) => void, + ) => callback({ code: "ENOENT" }, "", ""), + ); + + const result = await exec("missing-command", []); + + expect(result.status).toBe(1); + }); + + it("resolves status 1 when execFile throws synchronously", async () => { + execFileMock.mockImplementation(() => { + throw new Error("synchronous failure"); + }); + + const result = await exec("broken", []); + + expect(result).toEqual({ + status: 1, + stdout: "", + stderr: "", + }); + }); +}); diff --git a/context/exec.ts b/context/exec.ts @@ -0,0 +1,38 @@ +import { execFile, type ExecFileOptions } from "node:child_process"; + +export type ExecResult = { + status: number; + stdout: string; + stderr: string; +}; + +/** + * Run a command without blocking the event loop. Unlike `execFile`'s promise + * form, this resolves for non-zero exit codes so callers can handle command + * failures explicitly. + */ +export function exec( + command: string, + args: readonly string[], + options: ExecFileOptions = {}, +): Promise<ExecResult> { + return new Promise((resolve) => { + try { + execFile( + command, + args, + { maxBuffer: 10 * 1024 * 1024, ...options }, + (error, stdout, stderr) => { + const code = (error as NodeJS.ErrnoException | null)?.code; + resolve({ + status: typeof code === "number" ? code : error ? 1 : 0, + stdout: stdout?.toString() ?? "", + stderr: stderr?.toString() ?? "", + }); + }, + ); + } catch { + resolve({ status: 1, stdout: "", stderr: "" }); + } + }); +} diff --git a/context/fs.test.ts b/context/fs.test.ts @@ -0,0 +1,32 @@ +import { describe, it, expect, beforeEach, afterEach } from "vitest"; +import * as fs from "node:fs"; +import * as path from "node:path"; +import * as os from "node:os"; + +import { exists } from "./fs"; + +describe("fs", () => { + let tempRoot: string; + + beforeEach(() => { + tempRoot = path.join( + os.tmpdir(), + `openpod-test-${Date.now()}-${Math.random()}`, + ); + fs.mkdirSync(tempRoot, { recursive: true }); + }); + + afterEach(() => { + if (fs.existsSync(tempRoot)) { + fs.rmSync(tempRoot, { recursive: true, force: true }); + } + }); + + it("returns true for an existing path", async () => { + expect(await exists(tempRoot)).toBe(true); + }); + + it("returns false for a missing path", async () => { + expect(await exists(path.join(tempRoot, "missing"))).toBe(false); + }); +}); diff --git a/context/fs.ts b/context/fs.ts @@ -0,0 +1,10 @@ +import { access } from "node:fs/promises"; + +export async function exists(target: string): Promise<boolean> { + try { + await access(target); + return true; + } catch { + return false; + } +}