Deployment: Error Handling and Debugging
Relevant source files
The following files were used as context for generating this wiki page:This document outlines the error handling and debugging mechanisms within the OpenCode project. It covers two primary aspects: the debugging workflow performed by the AI agent when solving user tasks, and the tools and procedures available for developers to debug the OpenCode application itself. The agent's behavior is largely defined by its system prompts, which dictate a structured approach to problem-solving, including bug detection and resolution.
For developers contributing to OpenCode, specific CLI commands and setup instructions are provided to diagnose issues within the core application, particularly concerning Language Server Protocol (LSP) interactions and server behavior. This ensures both the agent's operational robustness and the stability of the underlying platform.
Agent-Driven Debugging Workflow
The OpenCode agent is instructed to follow a systematic workflow for identifying, debugging, and resolving issues in code. This process is defined within its core instruction prompts and emphasizes a cycle of investigation, implementation, and verification.
Sources: packages/opencode/src/session/prompt/beast.txt, packages/opencode/src/session/prompt/gemini.txt, packages/opencode/src/session/prompt/codex.txt
Core Debugging Steps
The agent's primary workflow for tackling problems, including debugging, is a multi-phase process.
This diagram illustrates the iterative debugging loop the agent is designed to follow.
Key activities in this workflow include:
- Deep Understanding: The agent first reads the issue carefully to understand expected behavior, edge cases, and potential pitfalls.
- Investigation: It explores relevant files, searches for key functions, and gathers context to identify the root cause.
- Planning: A clear, step-by-step plan is developed and tracked using a todo list.
- Implementation: Small, incremental, and testable code changes are made.
- Debugging and Testing: The agent uses debugging techniques to isolate issues and runs tests frequently after each change to verify correctness. It is instructed to determine the root cause rather than just addressing symptoms.
- Iteration: The cycle continues until the root cause is fixed and all tests pass.
Sources: packages/opencode/src/session/prompt/beast.txt:43-80, packages/opencode/src/session/prompt/copilot-gpt-5.txt:20-56
Automated Code Review for Bugs
When tasked with reviewing code, the agent acts as a code reviewer with a primary focus on finding bugs.
Sources: packages/opencode/src/command/template/review.txt
Review Targets
The agent can review different sets of changes based on the input:
| Input Type | Action |
|---|---|
| No arguments | Reviews all uncommitted changes (git diff and git diff --cached). |
| Commit hash | Reviews a specific commit (git show <hash>). |
| Branch name | Compares the current branch to the specified one (git diff <branch>...HEAD). |
| PR URL/Number | Reviews the pull request (gh pr diff <pr>). |
Sources: packages/opencode/src/command/template/review.txt:7-20
Bug Categories
The agent is instructed to look for specific types of bugs during a review:
- Logic Errors: Off-by-one mistakes, incorrect conditionals.
- Edge Cases: Null/empty inputs, error conditions, race conditions.
- Security Issues: Injection vulnerabilities, authentication bypass, data exposure.
- Error Handling: Broken or swallowed failures.
- Performance: Obvious issues like O(n²) loops, N+1 queries, or blocking I/O on hot paths.
Sources: packages/opencode/src/command/template/review.txt:26-33
Tool-Based Error Handling
The agent's tools, particularly the bash tool, have built-in error handling and retry logic, which is crucial for robust deployment and execution of commands.
Sources: packages/opencode/src/tool/bash.txt
Git Commit Error Handling
When committing changes, the agent follows a specific sequence that includes handling failures from pre-commit hooks.
The sequence diagram below shows the agent's process for committing code, including the retry mechanism for pre-commit hook failures.
If a commit fails due to pre-commit hook changes, the agent is instructed to retry the commit once. If it fails again, it assumes a persistent issue. If the commit succeeds but files were modified by the hook, the agent must amend the commit to include those changes.
Sources: packages/opencode/src/tool/bash.txt:62-63
Application Debugging for Developers
The OpenCode repository provides specific guidance and tools for developers to debug the application itself. This is distinct from the agent's debugging of user code.
Sources: CONTRIBUTING.md
Debugger Setup
The recommended way to debug OpenCode is to run it manually in a terminal with the --inspect flag and attach a debugger.
bun run --inspect=<url> dev ...
Sources: CONTRIBUTING.md:32-33
There are several important caveats for developers:
- Breakpoints in
*.tsxfiles may not be mapped correctly due to howBunPlugins handle source maps. Using adebugger;statement is the current workaround. - To debug the server while the TUI is running, developers might need to use
bun dev spawn, which avoids running the server in a worker thread where breakpoints might not trigger. - The
--inspect-waitor--inspect-brkflags can be used to pause execution on start.
Sources: CONTRIBUTING.md:36-46
VSCode Debug Configuration
The repository provides example configurations for VSCode debugging in .vscode/settings.example.json and .vscode/launch.example.json. It is noted that "request": "launch" configurations and the VSCode JavaScript Debug Terminal can sometimes lead to incorrectly mapped breakpoints.
Sources: CONTRIBUTING.md:50-57
Initializing Project Analysis
The initialize command template can be used to generate an AGENTS.md file. This file serves as a set of instructions for the agent, containing build/lint/test commands and code style guidelines, which are essential for debugging and maintaining code quality.
Please analyze this codebase and create an AGENTS.md file containing:
1. Build/lint/test commands - especially for running a single test
2. Code style guidelines including imports, formatting, types, naming conventions, error handling, etc.
Sources: packages/opencode/src/command/template/initialize.txt:1-4
Summary
Error handling and debugging in OpenCode are multifaceted. The AI agent employs a structured, iterative workflow guided by its system prompts to autonomously debug user code, complemented by a specialized code review process to proactively identify bugs. For command execution, tools like the bash interface have built-in resilience, such as retrying git commits after pre-commit hook failures. For developers of OpenCode, the project provides clear instructions and configurations for setting up debuggers to diagnose and resolve issues within the core application, ensuring the platform's overall stability and reliability.