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 3c7974a1111b2cb726a2101389b10d8e5c1ef4cc
parent 6f49edb5f4535ec8a817b414773e62eddc22c6b7
Author: Henry Unite <unitehenry@gmail.com>
Date:   Sun, 15 Nov 2026 19:50:27 +0000

refactor(context): make logging non-blocking

Queue log writes internally so callers can fire and forget instead of awaiting every log call. Revert shell wrapping to synchronous since logging no longer needs to be awaited.

Diffstat:
Mcommand/clear.ts | 8++++----
Mcommand/index.ts | 2+-
Mcontext/cleanup.ts | 18++++++++----------
Mcontext/index.ts | 2+-
Mcontext/logger.ts | 29+++++++++++++++++------------
Mcontroller/index.ts | 2+-
Mprompt/handle.ts | 26+++++++++++++-------------
Mprompt/index.ts | 2+-
Mshell/index.ts | 4++--
Mshell/wrap.test.ts | 24++++++++++++------------
Mshell/wrap.ts | 12++++++------
11 files changed, 66 insertions(+), 63 deletions(-)

diff --git a/command/clear.ts b/command/clear.ts @@ -5,11 +5,11 @@ import { exists } from "../context/fs"; export async function clear(deps: OpenpodContext): Promise<void> { const { logger, openpodRoot } = deps; - await logger.log("[clear] starting full openpod cleanup"); + void logger.log("[clear] starting full openpod cleanup"); try { if (!(await exists(openpodRoot))) { - await logger.log("[clear] openpod root does not exist"); + void logger.log("[clear] openpod root does not exist"); return; } @@ -24,8 +24,8 @@ export async function clear(deps: OpenpodContext): Promise<void> { // Remove the root directory entirely (includes .log etc.) await rm(openpodRoot, { recursive: true, force: true }); - await logger.log(`[clear] removed entire directory ${openpodRoot}`); + void logger.log(`[clear] removed entire directory ${openpodRoot}`); } catch (err) { - await logger.log(`[clear] error during cleanup: ${err}`); + void logger.log(`[clear] error during cleanup: ${err}`); } } diff --git a/command/index.ts b/command/index.ts @@ -14,7 +14,7 @@ export async function registerCommand( deps: OpenpodContext, ): Promise<void> { const { logger } = deps; - await logger.log("registering commands"); + void logger.log("registering commands"); await ctx.command.transform((editor) => { editor.add({ diff --git a/context/cleanup.ts b/context/cleanup.ts @@ -10,33 +10,31 @@ export type Cleanup = { }; export function initCleanup(openpodRoot: string, logger?: Logger): Cleanup { - const log = async (message: string, level?: string): Promise<void> => { - await logger?.log?.(message, level); + const log = (message: string, level?: string): void => { + void logger?.log?.(message, level); }; async function stopLimaInstance(limaName: string): Promise<void> { // best-effort stop (ignore errors) await exec("limactl", ["stop", limaName, "--force"]); - await log(`[cleanup] stopped ${limaName} (if running)`); + log(`[cleanup] stopped ${limaName} (if running)`); } async function deleteLimaInstance(limaName: string): Promise<void> { const result = await exec("limactl", ["delete", limaName, "--force"]); - await log( - `[cleanup] limactl delete ${limaName} exited with ${result.status}`, - ); + log(`[cleanup] limactl delete ${limaName} exited with ${result.status}`); } async function removePodDirectory(podDir: string): Promise<void> { try { if (await exists(podDir)) { await rm(podDir, { recursive: true, force: true }); - await log(`[cleanup] removed directory ${podDir}`); + log(`[cleanup] removed directory ${podDir}`); } else { - await log(`[cleanup] directory did not exist: ${podDir}`); + log(`[cleanup] directory did not exist: ${podDir}`); } } catch (err) { - await log(`[cleanup] failed to remove ${podDir}: ${err}`); + log(`[cleanup] failed to remove ${podDir}: ${err}`); } } @@ -46,7 +44,7 @@ export function initCleanup(openpodRoot: string, logger?: Logger): Cleanup { const limaName = sessionID; const podDir = path.join(openpodRoot, sessionID); - await log(`[cleanup] starting for ${sessionID}`); + log(`[cleanup] starting for ${sessionID}`); await stopLimaInstance(limaName); await deleteLimaInstance(limaName); diff --git a/context/index.ts b/context/index.ts @@ -26,7 +26,7 @@ export async function init(): Promise<OpenpodContext> { } if (!(await isCommandAvailable("limactl"))) { - await logger.log( + void logger.log( "`limactl` is not installed. Lima is required for openpod. See https://lima-vm.io/", "error", ); diff --git a/context/logger.ts b/context/logger.ts @@ -8,19 +8,24 @@ export type Logger = { export function initLogger(openpodRoot: string): Logger { const logDir = path.join(openpodRoot, ".log"); const logFile = path.join(logDir, "openpod.log"); + let queue = Promise.resolve(); - async function log(message: string, level: string = "info"): Promise<void> { - try { - await mkdir(logDir, { recursive: true }); - const timestamp = new Date().toISOString(); - const levelPart = level === "info" ? "" : `[${level}] `; - await appendFile( - logFile, - `[${timestamp}] [openpod] ${levelPart}${message}\n`, - ); - } catch { - // ignore logging failures - } + function log(message: string, level: string = "info"): Promise<void> { + const timestamp = new Date().toISOString(); + const levelPart = level === "info" ? "" : `[${level}] `; + const entry = `[${timestamp}] [openpod] ${levelPart}${message}\n`; + + // Writes are queued to keep log order, but callers do not need to wait. + queue = queue + .then(async () => { + await mkdir(logDir, { recursive: true }); + await appendFile(logFile, entry); + }) + .catch(() => { + // ignore logging failures + }); + + return queue; } return { log }; diff --git a/controller/index.ts b/controller/index.ts @@ -32,7 +32,7 @@ export async function registerController( // eslint-disable-next-line @typescript-eslint/no-explicit-any } catch (err: any) { if (err?.name !== "AbortError") { - await deps.logger.log(`event subscription error: ${err}`); + void deps.logger.log(`event subscription error: ${err}`); } } })(); diff --git a/prompt/handle.ts b/prompt/handle.ts @@ -49,7 +49,7 @@ export async function handle(params: HandleParams): Promise<void> { sessionId: string, target: string, ): Promise<void> { - await logger.log( + void logger.log( `creating lima instance ${sessionId} (mount-only ${target}:w)`, ); const createResult = await exec("limactl", [ @@ -60,15 +60,15 @@ export async function handle(params: HandleParams): Promise<void> { "--yes", ]); if (createResult.status !== 0) { - await logger.log(`limactl create exited with ${createResult.status}`); + void logger.log(`limactl create exited with ${createResult.status}`); } } async function startLimaInstance(sessionId: string): Promise<void> { - await logger.log(`starting lima instance ${sessionId}`); + void logger.log(`starting lima instance ${sessionId}`); const startResult = await exec("limactl", ["start", sessionId, "--yes"]); if (startResult.status !== 0) { - await logger.log(`limactl start exited with ${startResult.status}`); + void logger.log(`limactl start exited with ${startResult.status}`); } } @@ -79,7 +79,7 @@ export async function handle(params: HandleParams): Promise<void> { await mkdir(openpodRoot, { recursive: true }); const result = await exec("git", ["clone", originUrl, target]); if (result.status !== 0) { - await logger.log(`git clone failed with exit code ${result.status}`); + void logger.log(`git clone failed with exit code ${result.status}`); try { await rm(target, { recursive: true, force: true }); } catch { @@ -91,41 +91,41 @@ export async function handle(params: HandleParams): Promise<void> { } const gitEnabled = await isGitEnabled(dir); - await logger.log(`session ${sessionId} prompt - gitEnabled: ${gitEnabled}`); + void logger.log(`session ${sessionId} prompt - gitEnabled: ${gitEnabled}`); if (!gitEnabled) { - await logger.log(`git is not enabled`); + void logger.log(`git is not enabled`); return; } const target = path.join(openpodRoot, sessionId); const needsMove = dir !== target; - await logger.log( + void logger.log( `needs move for project dir ${dir}? ${needsMove} (target=${target})`, ); if (needsMove) { if (await exists(target)) { - await logger.log(`${target} already exists`); + void logger.log(`${target} already exists`); move(sessionId, target); } else { const originUrl = await getOriginUrl(dir); if (!originUrl) { - await logger.log(`no origin remote configured`); + void logger.log(`no origin remote configured`); return; } - await logger.log(`cloning from origin ${originUrl} -> ${target}`); + void logger.log(`cloning from origin ${originUrl} -> ${target}`); if (!(await cloneProject(originUrl, target))) { return; } - await logger.log(`clone complete, moving session to ${target}`); + void logger.log(`clone complete, moving session to ${target}`); move(sessionId, target); } } else { - await logger.log(`already in ${target} directory`); + void logger.log(`already in ${target} directory`); } if (!(await limaInstanceExists(sessionId))) { diff --git a/prompt/index.ts b/prompt/index.ts @@ -10,7 +10,7 @@ export async function registerPrompt( ): Promise<void> { const { logger } = deps; - await logger.log("registering prompt"); + void logger.log("registering prompt"); await ctx.session.hook("prompt", async (event) => { const session = await ctx.session.get({ sessionID: event.sessionID }); diff --git a/shell/index.ts b/shell/index.ts @@ -10,9 +10,9 @@ export async function registerShell( ): Promise<void> { const { logger } = deps; - await logger.log("registering create.before"); + void logger.log("registering create.before"); await ctx.shell.hook("create.before", async (event) => { - event.command = await wrap(deps, event.cwd, event.command); + event.command = wrap(deps, event.cwd, event.command); }); } diff --git a/shell/wrap.test.ts b/shell/wrap.test.ts @@ -12,45 +12,45 @@ describe("wrap", () => { ctx = await init(); }); - it("returns original command when cwd is outside openpodRoot", async () => { + it("returns original command when cwd is outside openpodRoot", () => { const command = "echo hello"; - const result = await wrap(ctx, "/tmp/some/random/dir", command); + const result = wrap(ctx, "/tmp/some/random/dir", command); expect(result).toBe(command); }); - it("returns original command when cwd is exactly openpodRoot", async () => { + it("returns original command when cwd is exactly openpodRoot", () => { const command = "ls"; - const result = await wrap(ctx, ctx.openpodRoot, command); + const result = wrap(ctx, ctx.openpodRoot, command); expect(result).toBe(command); }); - it("wraps command using first directory segment as lima name", async () => { + it("wraps command using first directory segment as lima name", () => { const session = "my-session-abc"; const cwd = path.join(ctx.openpodRoot, session); const command = "npm run dev"; - const result = await wrap(ctx, cwd, command); + const result = wrap(ctx, cwd, command); expect(result).toBe( `limactl shell --preserve-env --workdir='${cwd}' ${session} bash -c '${command}'`, ); }); - it("uses only the first segment even for nested directories", async () => { + it("uses only the first segment even for nested directories", () => { const session = "sess-xyz"; const cwd = path.join(ctx.openpodRoot, session, "src", "app"); const command = "cat package.json"; - const result = await wrap(ctx, cwd, command); + const result = wrap(ctx, cwd, command); expect(result).toBe( `limactl shell --preserve-env --workdir='${cwd}' ${session} bash -c '${command}'`, ); }); - it("escapes single quotes in cwd and command", async () => { + it("escapes single quotes in cwd and command", () => { const session = "sess'with'quotes"; const cwd = path.join(ctx.openpodRoot, session, "sub'dir"); const command = "echo 'hello world'"; - const result = await wrap(ctx, cwd, command); + const result = wrap(ctx, cwd, command); const escapedCwd = cwd.replace(/'/g, "'\\''"); const escapedCmd = command.replace(/'/g, "'\\''"); @@ -59,10 +59,10 @@ describe("wrap", () => { expect(result).toBe(expected); }); - it("returns original command for paths that would escape the tree", async () => { + it("returns original command for paths that would escape the tree", () => { const command = "pwd"; const outside = path.join(ctx.openpodRoot, "..", "escape"); - const result = await wrap(ctx, outside, command); + const result = wrap(ctx, outside, command); expect(result).toBe(command); }); }); diff --git a/shell/wrap.ts b/shell/wrap.ts @@ -1,15 +1,15 @@ import type { OpenpodContext } from "../context"; import * as path from "node:path"; -export async function wrap( +export function wrap( deps: OpenpodContext, cwd: string, command: string, -): Promise<string> { - async function getLimaName(dir: string): Promise<string | null> { +): string { + function getLimaName(dir: string): string | null { const { logger, openpodRoot } = deps; try { - await logger.log(`get lima name for ${openpodRoot} ${dir}`); + void logger.log(`get lima name for ${openpodRoot} ${dir}`); const rel = path.relative(openpodRoot, dir); if (rel.startsWith("..") || path.isAbsolute(rel)) { return null; @@ -21,7 +21,7 @@ export async function wrap( } } - const limaName = await getLimaName(cwd); + const limaName = getLimaName(cwd); if (!limaName) { return command; } @@ -30,7 +30,7 @@ export async function wrap( const escapedCmd = command.replace(/'/g, "'\\''"); const wrapped = `limactl shell --preserve-env --workdir='${escapedCwd}' ${limaName} bash -c '${escapedCmd}'`; - await deps.logger.log( + void deps.logger.log( `redirected shell command for lima ${limaName} (cwd=${cwd})`, );