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 3fb84fb373f13f6c3b27025a47e5426c00fe394a
parent 252adc2f6f6a2425b505ef2561e2a40263633a0a
Author: Henry Unite <unitehenry@gmail.com>
Date:   Sun, 18 Oct 2026 19:01:19 +0000

feat: clone to openpod session repo

Diffstat:
Mindex.ts | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 84 insertions(+), 11 deletions(-)

diff --git a/index.ts b/index.ts @@ -1,5 +1,8 @@ import { Plugin } from "@opencode/plugin" -import { execSync } from "child_process" +import { execSync, spawnSync } from "node:child_process" +import * as fs from "node:fs" +import * as os from "node:os" +import * as path from "node:path" function isCommandAvailable(command: string): boolean { try { @@ -14,6 +17,49 @@ function isCommandAvailable(command: string): boolean { } } +function isGitEnabled(dir: string): boolean { + try { + const result = spawnSync("git", ["-C", dir, "rev-parse", "--is-inside-work-tree"], { + stdio: "ignore", + }) + return result.status === 0 + } catch { + return false + } +} + +function getRepoRoot(dir: string): string { + try { + const result = spawnSync("git", ["-C", dir, "rev-parse", "--show-toplevel"], { + encoding: "utf8", + stdio: ["ignore", "pipe", "ignore"], + }) + if (result.status !== 0 || !result.stdout) { + throw new Error(result.stderr?.toString() || "git rev-parse failed") + } + return result.stdout.trim() + } catch(err) { + console.log(`[openpod] failed to determine git root for ${dir}: ${err}`) + return null; + } +} + +function cloneRepo(source: string, target: string): void { + try { + const result = spawnSync("git", ["clone", source, target], { + stdio: "inherit", + }) + if (result.status !== 0) { + throw new Error(`git clone failed with exit code ${result.status}`) + } + } catch(err) { + console.error(`[openpod] ${err}`) + try { + fs.rmSync(target, { recursive: true, force: true }) + } catch {} + } +} + export default Plugin.define({ id: "openpod", async setup(ctx) { @@ -24,19 +70,46 @@ export default Plugin.define({ } await ctx.session.hook("prompt", async (event) => { - const messages = await ctx.session.context({ sessionID: event.sessionID }) - const isNew = messages.length === 0 - console.log(`[openpod] session ${event.sessionID} prompt - isNew: ${isNew}`) - const session = await ctx.session.get({ sessionID: event.sessionID }) const dir = session.location.directory - const vcs = await ctx.vcs.get({ location: { directory: dir } }) - const vcsEnabled = !!vcs.data.provider - console.log(`[openpod] vcs enabled for project dir ${dir}? ${vcsEnabled} (provider=${vcs.data.provider ?? "none"})`) + const gitEnabled = isGitEnabled(dir) + console.log(`[openpod] session ${event.sessionID} prompt - gitEnabled: ${gitEnabled}`) + + const openpodRoot = path.join(os.homedir(), ".local", "share", "openpod") + const target = path.join(openpodRoot, event.sessionID) + const needsMove = dir !== target + console.log(`[openpod] needs move for project dir ${dir}? ${needsMove} (target=${target})`) + + if (!gitEnabeld) { + console.log(`[openpod] git is not enabled`) + return + } + + if (!needsMove) { + console.log(`[openpod] already in ${target} directory`) + return; + } + + if (fs.existsSync(target)) { + console.log(`[openpod] ${target already exists}`) + await ctx.session.move({ sessionID: event.sessionID, directory: target }) + return + } + + const repoRoot = getRepoRoot(dir); + + if (!repoRoot) { + console.log(`[opencode] failed to get repo root`); + return; + } + + console.log(`[openpod] cloning ${repoRoot} -> ${target}`) + fs.mkdirSync(openpodRoot, { recursive: true }) + + cloneRepo(repoRoot, target) - // TODO: if new, and vcsEnabled - // git clone to `~/.local/share/openpod/<sessionId>` - // move session to clone repo + console.log(`[openpod] clone complete, moving session to ${target}`) + await ctx.session.move({ sessionID: event.sessionID, directory: target }) }) }, })