Skip to content

fix(pty): graceful degradation when bun-pty native library unavailable#34

Merged
vtemian merged 1 commit intomainfrom
fix/bun-pty-graceful-degradation
Mar 10, 2026
Merged

fix(pty): graceful degradation when bun-pty native library unavailable#34
vtemian merged 1 commit intomainfrom
fix/bun-pty-graceful-degradation

Conversation

@vtemian
Copy link
Owner

@vtemian vtemian commented Mar 10, 2026

Summary

  • Auto-resolve BUN_PTY_LIB by probing .opencode/node_modules/ and other paths that bun-pty's resolveLibPath() misses
  • Load bun-pty dynamically so the plugin still loads (without PTY tools) when the native library can't be found
  • Log actionable warnings when PTY is unavailable instead of crashing

Fixes #20

Problem

When micode is installed as an OpenCode plugin, bun-pty's native library (librust_pty.so/.dylib) ends up in .opencode/node_modules/bun-pty/... — a path not in bun-pty's hardcoded search list. This causes the plugin to crash on startup.

Root cause is upstream: anomalyco/opencode#10556

Changes

New: src/tools/pty/pty-loader.ts

  • Probes additional paths (.opencode/node_modules/, .micode/node_modules/, require.resolve) and sets BUN_PTY_LIB env var before import
  • Wraps import("bun-pty") in try/catch for graceful degradation
  • Caches result so probing only happens once

Modified: src/tools/pty/manager.ts

  • Removed static import { spawn } from "bun-pty" (would crash at module load)
  • Added init(spawnFn) — spawn function is injected after successful loading
  • Added available getter for callers to check PTY status

Modified: src/index.ts

  • Uses loadBunPty() to dynamically load with path probing
  • Conditionally registers PTY tools only if bun-pty loaded successfully

Tests

  • All 6 PTY test files updated to use manager.init(spawn) pattern
  • 60/60 PTY tests pass, full suite 364/365 (1 pre-existing failure unrelated)

Manual workaround

If auto-resolution doesn't cover a specific setup:

export BUN_PTY_LIB=.opencode/node_modules/bun-pty/rust-pty/target/release/librust_pty.dylib

Auto-resolve BUN_PTY_LIB by probing .opencode/node_modules/ and other
paths that bun-pty's resolveLibPath() misses. Load bun-pty dynamically
so the plugin still works (without PTY tools) when the native library
can't be found.

Fixes #20
See: anomalyco/opencode#10556
@vtemian vtemian merged commit 9e63802 into main Mar 10, 2026
2 checks passed
@vtemian vtemian deleted the fix/bun-pty-graceful-degradation branch March 10, 2026 08:53
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 11 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="src/tools/pty/pty-loader.ts">

<violation number="1" location="src/tools/pty/pty-loader.ts:90">
P2: `loadBunPty()` has a concurrency race: concurrent callers can get `null` while the first import is still in flight. Cache and return the in-flight Promise instead of returning `cachedModule` immediately once `loadAttempted` is set.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

* Returns null if bun-pty cannot be loaded (native library missing, etc.)
*/
export async function loadBunPty(): Promise<BunPtyModule | null> {
if (loadAttempted) return cachedModule;
Copy link

@cubic-dev-ai cubic-dev-ai bot Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: loadBunPty() has a concurrency race: concurrent callers can get null while the first import is still in flight. Cache and return the in-flight Promise instead of returning cachedModule immediately once loadAttempted is set.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/tools/pty/pty-loader.ts, line 90:

<comment>`loadBunPty()` has a concurrency race: concurrent callers can get `null` while the first import is still in flight. Cache and return the in-flight Promise instead of returning `cachedModule` immediately once `loadAttempted` is set.</comment>

<file context>
@@ -0,0 +1,123 @@
+ * Returns null if bun-pty cannot be loaded (native library missing, etc.)
+ */
+export async function loadBunPty(): Promise<BunPtyModule | null> {
+  if (loadAttempted) return cachedModule;
+  loadAttempted = true;
+
</file context>
Fix with Cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When I install your plugin, found the same issue as shown in opencode

1 participant