@wardhq/opencode
v0.2.0
Published
Ward tool mediation for OpenCode V2
Readme
@wardhq/opencode
Thin Ward tool mediation for OpenCode V2. The first supported host is
OpenCode 2.1.2; the plugin uses its Promise V2 API and wraps registered tool
executors with ctx.tool.transform.
Install
opencode plugin add @wardhq/[email protected]The plugin activates inside a Ward-managed Modal runtime from the Ward session identity already supplied to that Sandbox. It does not require a client integration, client lifecycle endpoint, or link credential.
| Variable | Purpose |
| --- | --- |
| WARD_API_BASE_URL | HTTPS origin for the Ward API |
| WARD_SESSION_ID | Owning Ward session ID |
| WARD_SANDBOX_ID | Current Modal Sandbox ID |
| WARD_ATTEMPT | Current runtime attempt |
| WARD_ATTEMPT_CREDENTIAL | Ward-issued credential scoped to the current session attempt |
If all variables are absent, the plugin is inert. Partial or invalid configuration prevents OpenCode startup. The plugin removes the attempt credential from its process environment after reading it, so tool child processes do not inherit it.
Mediation flow
For each intercepted tool, the plugin sends POST
/sessions/{WARD_SESSION_ID}/tool-calls with the call ID, tool name, arguments,
the tool's output JSON Schema, and call attempt. The request carries Ward's
existing session, Sandbox, and runtime-attempt identity. The Worker forwards it
to that Ward session's Session DO, which records the pending call and wakes the
Ward agent. The plugin waits for Ward's simulation decision without setting its
own timeout. The OpenCode tool's cancellation signal cancels the request. The
original executor is never run. Ward validates the decision envelope and result
before resolving the plugin's request.
The Ward response is one of:
{ "call_id": "...", "attempt": 1, "decision": "simulate", "result": { ... } }: return the simulated OpenCode result without executing the original tool.{ "call_id": "...", "attempt": 1, "decision": "simulate", "error": { "message": "..." } }: fail the tool call with the simulated error.
The plugin serializes requests waiting for Ward so a Ward session has at most one pending call. It forwards the tool's output schema to Ward and transports the accepted decision back to OpenCode; it does not validate Ward's simulated result or send validation feedback as a second request. Ward's decision endpoint validates the result envelope and output schema before accepting a decision. Invalid results return a structured HTTP error to Ward's decision API call and leave the pending client tool call unchanged.
Requests are limited to 256 KiB, arguments/results to 128 KiB, and output schemas to 64 KiB. Tool arguments and simulated values are forwarded without redaction.
CodeMode's synthesized execute tool is mediated through OpenCode's
tool.execute.before and tool.execute.after hooks. Ward receives the full
CodeMode input and the pinned CodeMode output schema. Before OpenCode runs the
program, the plugin replaces it with a harmless stub; after the stub returns,
the plugin substitutes Ward's validated simulated result. The original
program, including direct fetch calls and nested tool calls, does not run.
Ward therefore simulates each CodeMode execute call as one unit; tools called
inside that program are not submitted as separate calls.
Runtime telemetry
The plugin writes structured ward.opencode_plugin.* events to OpenCode's
stdout. The client runtime forwards that stream to its compute environment,
where it is available in the session's container logs. Events identify plugin setup/configuration, the count
of wrapped tools and whether shell was wrapped, each intercepted call, Ward
decision receipt and latency, and whether the call was simulated. They include
tool names and generated call IDs, but never credentials, tool arguments,
simulated values, or tool results. Telemetry failures do not change plugin
behavior.
For a call that appears to bypass Ward, the expected sequence is
tool_intercepted → ward_request_started → decision_received →
tool_simulated. CodeMode execute is intercepted by hooks and is not included
in the tools_wrapped count. Missing setup_started points to plugin resolution or
loading; setup_failed points to configuration or hook registration; a
tools_wrapped record with shell_tool_wrapped: false means the built-in shell
tool was not present in the transform; and a request without
decision_received should have a matching ward_request_failed event. If it
has neither, the request is still pending or the OpenCode process exited
before it could emit an outcome.
Compatibility
This package peers on exactly @opencode/[email protected]. It must not be treated
as proof that every tool path in an OpenCode host is mediated. Verify built-in,
plugin, MCP, CodeMode, direct, dynamic, and delegated paths against the exact
host build before enabling it for customer runs.
Publishing
Merging a new package version to main publishes it after checks and production
deployment succeed. Increment both package.json and package-lock.json; the
publisher workflow skips versions already present on npm. Publishing uses npm
trusted publishing from GitHub Actions, with no long-lived npm token. The npm
package must trust the publish-opencode.yml workflow in halcyonHQ/Ward and
allow direct publishing.
