Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions src/plugin/pty/manager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { spawn, type IPty } from "bun-pty";
import type { IPty } from "bun-pty";
import type { OpencodeClient } from "@opencode-ai/sdk";
import { RingBuffer } from "./buffer.ts";
import type { PTYSession, PTYSessionInfo, SpawnOptions, ReadResult, SearchResult } from "./types.ts";
import { createLogger } from "../logger.ts";
import { prepareShadowPty } from "./shadow.ts";

const log = createLogger("manager");

Expand All @@ -21,8 +22,17 @@ function generateId(): string {

class PTYManager {
private sessions: Map<string, PTYSession> = new Map();
private ptyModule: typeof import("bun-pty") | null = null;

spawn(opts: SpawnOptions): PTYSessionInfo {
private async ensureLoaded() {
if (this.ptyModule) return this.ptyModule;
await prepareShadowPty();
this.ptyModule = await import("bun-pty");
return this.ptyModule;
}

async spawn(opts: SpawnOptions): Promise<PTYSessionInfo> {
const { spawn } = await this.ensureLoaded();
const id = generateId();
const args = opts.args ?? [];
const workdir = opts.workdir ?? process.cwd();
Expand Down
53 changes: 53 additions & 0 deletions src/plugin/pty/shadow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import fs from "node:fs";
import path from "node:path";
import os from "node:os";
import crypto from "node:crypto";

/**
* Prepares a shadow copy of the bun-pty native binary on Windows.
* This prevents file locking issues when multiple OpenCode instances are running.
*/
export async function prepareShadowPty() {
if (process.platform !== "win32") {
return;
}

if (process.env.BUN_PTY_LIB) {
return;
}

try {
// Resolve the bun-pty package location
const entryPath = await Bun.resolve("bun-pty", import.meta.dir);
const packageRoot = path.dirname(path.dirname(entryPath));

const dllPath = path.join(packageRoot, "rust-pty", "target", "release", "rust_pty.dll");

if (!fs.existsSync(dllPath)) {
return;
}

const tempDir = path.join(os.tmpdir(), "opencode-pty", crypto.randomUUID());
fs.mkdirSync(tempDir, { recursive: true });

const targetDllPath = path.join(tempDir, "rust_pty.dll");
fs.copyFileSync(dllPath, targetDllPath);

// Set the environment variable that bun-pty uses to find its native library
process.env.BUN_PTY_LIB = targetDllPath;

// Ensure we clean up on exit
process.on("exit", () => {
try {
fs.rmSync(tempDir, { recursive: true, force: true });
} catch {
// Ignore cleanup errors
}
});
} catch (err) {
// Fail silently but log if in dev
if (process.env.NODE_ENV === "development") {
console.error("[opencode-pty] Failed to shadow copy native binary:", err);
}
}
}
2 changes: 1 addition & 1 deletion src/plugin/pty/tools/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const ptySpawn = tool({
}

const sessionId = ctx.sessionID;
const info = manager.spawn({
const info = await manager.spawn({
command: args.command,
args: args.args,
workdir: args.workdir,
Expand Down