opencode
opencode copied to clipboard
fix: handle missing Instance context in Read tool
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 whenInstance.worktreeis 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 topath.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