[PR #8302] fix: handle missing Instance context in Read tool #12697

Closed
opened 2026-02-16 18:17:35 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/8302

State: closed
Merged: No


Summary

Fixes #8301

  • Gracefully handle missing Instance context in Read tool and external-directory helper
  • Prevents cryptic native binding errors when tools are executed outside normal server flow
  • Falls back to path.basename() for title when Instance.worktree is unavailable

Problem

When the Read tool is executed outside of an Instance context (e.g., via MCP, plugins), accessing Instance.worktree throws a Context.NotFound exception. However, before this propagates, path.relative(undefined, filepath) throws:

TypeError: Binding expected string, TypedArray, boolean, number, bigint or null

This is confusing and does not indicate the actual problem.

Solution

Wrap Instance.worktree and Instance.containsPath() access in try-catch blocks:

  • In read.ts: Fall back to path.basename(filepath) for the title
  • In external-directory.ts: Proceed to ask for permission when containsPath check cannot run

Verification

  • All existing read tool tests pass (26 tests)
  • Tested fix logic with simulated missing context
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/8302 **State:** closed **Merged:** No --- ## Summary Fixes #8301 - Gracefully handle missing Instance context in Read tool and external-directory helper - Prevents cryptic native binding errors when tools are executed outside normal server flow - Falls back to `path.basename()` for title when `Instance.worktree` is unavailable ## Problem When the Read tool is executed outside of an Instance context (e.g., via MCP, plugins), accessing `Instance.worktree` throws a `Context.NotFound` exception. However, before this propagates, `path.relative(undefined, filepath)` throws: ``` TypeError: Binding expected string, TypedArray, boolean, number, bigint or null ``` This is confusing and does not indicate the actual problem. ## Solution Wrap `Instance.worktree` and `Instance.containsPath()` access in try-catch blocks: - In `read.ts`: Fall back to `path.basename(filepath)` for the title - In `external-directory.ts`: Proceed to ask for permission when containsPath check cannot run ## Verification - All existing read tool tests pass (26 tests) - Tested fix logic with simulated missing context
yindo added the pull-request label 2026-02-16 18:17:35 -05:00
yindo closed this issue 2026-02-16 18:17:35 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12697