Repository navigation
fix: strip OpenCode read envelope before Cursor sees it - #6
Merged
Merged
Conversation
Prevent silent file corruption when Cursor echoes path/content wrappers and N: line prefixes from OpenCode's read tool into subsequent writes.
Widen the peer/dev range from a pinned ~1.17.13 so Bun can resolve the current OpenCode plugin/sdk used with this provider.
Owner
Author
|
@renaudcerrato FYI, not sure if this is relevant for Kilo Code, but you may want to implement this too |
Contributor
|
@oakimov : thanks! For sure, I'm trying to merge every commits back in my fork - will do ! Thanks for the heads up |
oakimov
added a commit
that referenced
this pull request
Aug 1, 2026
) Two independent faults from issue #18. Fault A — write/edit unavailable on GPT models. OpenCode 1.x does not merely add `apply_patch` for GPT models, it removes `edit` and `write` from the catalog in exchange (`ToolRegistry.tools`: modelID contains "gpt-" and neither "oss" nor "gpt-4"). Cursor keeps sending its native write/edit exec requests regardless, so every file change was refused as an unavailable tool. `remapEditToolsForCatalog` translates those requests into `apply_patch` envelopes: a whole-file write becomes `*** Add File:` (which overwrites, per opencode's own tests, so no diff is needed), and a substring edit is widened to whole lines because apply_patch matches line sequences. Unreadable, absent, ambiguous or overlapping edits are refused with a specific message rather than patched into the wrong region. Keyed only off the advertised tool set — never a model id or host version — so it is inert on hosts that offer edit/write normally, and inert again on OpenCode 2.0, which has no such substitution. Advertisement needed no change: `opencode-apply_patch` was already forwarded to Cursor verbatim; only the native exec channel was untranslated. Fault B — large files silently truncated at ~50 KB. The truncation is on the read, not the write. Opencode's read tool caps at `MAX_BYTES = 50 * 1024`, cuts on a whole-line boundary, and appends a footer saying so; `unwrapReadOutput` stripped that footer along with the envelope. The model then treated a capped read as the complete file and rewrote it, destroying everything past the cap. The envelope is still stripped byte-for-byte — echoing wrappers into writes remains the reason it exists — but the cap is now re-stated: native `read_result` appends a `[Partial read: …]` marker, `mcp_result` gets a separate notice item, and Pi reads carry structured `PiReadExecSuccess.truncation`. Reads the caller bounded itself with an explicit offset/limit are not marked. The structured `truncated` flag was already being sent and is not sufficient on its own: in live sessions both gpt-5.4-mini and grok-4.5 asserted "highly confident" that partial content was the whole file until the textual marker was added. Also decodes `WriteArgs.file_bytes` (#5) and `encoding_hint` (#6), which the schema omitted and an internal-keys filter discarded. Cursor's own executor prefers bytes when non-empty, so a byte-encoded write would have decoded as empty content. Verified end-to-end against OpenCode 1.18.9 with cursor/gpt-5.4-mini and cursor/grok-4.5.
oakimov
added a commit
that referenced
this pull request
Nov 6, 2026
analyzeReplayFrame reads an echo only from a frame validInteractionUpdateWire accepted, which already proved update #6 holds exactly one length-delimited user_message #1. appendedEcho checked that again, and its fallback branch could never run (Codecov: the one uncovered patch line). State the precondition instead, and cover an empty user_message.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What Problem This Solves
OpenCode's
readtool wraps file content in an XML-ish envelope (<path>,<type>file</type>,<content>, numberedN:lines, footer). Cursor models are not trained on that wrapper. When this provider forwarded the envelope verbatim, Cursor treated it as literal file content and wrote the tags and line prefixes back into files — silent corruption (writes still reported success).Why This Change Was Made
unwrapReadOutput()to strip the envelope structurally (contiguousN:body run; no</content>substring search, so literal tags inside file lines are preserved).read_resultalways; onmcp_resultonly whentoolName === "read"(OpenCode builtins arrive as MCP).toolNamethrough tool-result replies and store it on pending execs so continuation unwrap gating does not depend solely on the prompt echoing the name.trace()tosrc/debug.tsso protocol code does not import transport for logging.@opencode-ai/pluginpeer/dev range from~1.17.13to^1.17.13and lock to resolved1.18.2(with matching sdk) for current OpenCode plugin compatibility.User Impact
read, not the wrapper.Evidence
test/tools.test.ts(unwrapReadOutput,buildTypedExecResultmcp/read paths) andtest/session.test.ts(pendingtoolName).bun test: 285 pass, 0 fail.bun run typecheck: clean.package.json/bun.lock:@opencode-ai/plugin^1.17.13→ lockfile1.18.2.