opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix: handle missing Instance context in Read tool

Open sureshsankaran opened this issue 23 hours ago • 2 comments

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

sureshsankaran avatar Jan 13 '26 23:01 sureshsankaran