[PR #13051] fix: prevent opencode run crash on malformed tool inputs #14494

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

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

State: closed
Merged: Yes


Fixes #12978

The tool() display function in run.ts reads raw LLM input before Zod validates it — so if the model omits a required field (like subagent_type on task), it crashes with a TypeError. This also kills the self-healing retry loop since Zod never gets a chance to tell the model what it got wrong.

Fix: wrapped the dispatch body in a try/catch that falls back to the existing fallback(part) handler. Valid inputs behave exactly the same, malformed ones get the generic display instead of a crash.

I know the style guide prefers .catch() over try/catch — but tool() is synchronous, so using .catch() would mean wrapping it in a Promise which changes the execution timing from sync to async and could introduce new bugs. Didn't think it was worth it.

Tested by mocking all 12 tool display functions with missing required fields — 9 crashed before, 0 after.

Thanks to @ariane-emory for helping me make my first PR to OpenCode.

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/13051 **State:** closed **Merged:** Yes --- Fixes #12978 The `tool()` display function in `run.ts` reads raw LLM input before Zod validates it — so if the model omits a required field (like `subagent_type` on `task`), it crashes with a TypeError. This also kills the self-healing retry loop since Zod never gets a chance to tell the model what it got wrong. Fix: wrapped the dispatch body in a try/catch that falls back to the existing `fallback(part)` handler. Valid inputs behave exactly the same, malformed ones get the generic display instead of a crash. I know the style guide prefers `.catch()` over try/catch — but `tool()` is synchronous, so using `.catch()` would mean wrapping it in a Promise which changes the execution timing from sync to async and could introduce new bugs. Didn't think it was worth it. Tested by mocking all 12 tool display functions with missing required fields — 9 crashed before, 0 after. Thanks to [@ariane-emory](https://github.com/ariane-emory) for helping me make my first PR to OpenCode.
yindo added the pull-request label 2026-02-16 18:19:17 -05:00
yindo closed this issue 2026-02-16 18:19:17 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14494