Skip to content

fix: handle missing Instance context in Read tool#8302

Closed
sureshsankaran wants to merge 1 commit intoanomalyco:devfrom
sureshsankaran:fix-read-tool-instance-context
Closed

fix: handle missing Instance context in Read tool#8302
sureshsankaran wants to merge 1 commit intoanomalyco:devfrom
sureshsankaran:fix-read-tool-instance-context

Conversation

@sureshsankaran
Copy link

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

When the Read tool is executed outside of an Instance context (e.g., via
MCP servers, plugins, or other non-standard code paths), accessing
Instance.worktree throws a Context.NotFound exception. However, before
this exception propagates, path.relative() receives undefined and throws
a cryptic native binding error:

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

This fix gracefully handles missing Instance context by:
1. Wrapping Instance.worktree access in try-catch in read.ts
2. Wrapping Instance.containsPath() in try-catch in external-directory.ts
3. Falling back to path.basename() for the title when context is missing
4. Proceeding to ask for permission when containsPath check cannot run

This is defensive programming that prevents cryptic errors in edge cases
while maintaining normal behavior when Instance context is available.
@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

sureshsankaran added a commit to sureshsankaran/opencode that referenced this pull request Jan 13, 2026
Gracefully handle missing Instance context to avoid cryptic native binding
errors when tools are executed outside normal server flow (e.g., MCP, plugins).

Related upstream PR: anomalyco#8302
@rekram1-node
Copy link
Collaborator

How are you triggering this error? Instance should be there

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.

Read tool crashes with "Binding expected string" error when Instance context is unavailable

2 participants