Skip to content

fix: strip OpenCode read envelope before Cursor sees it - #6

Merged
oakimov merged 2 commits into
mainfrom
fix/unwrap-opencode-read-envelope
Jul 16, 2026
Merged

oakimov merged 2 commits into
mainfrom
fix/unwrap-opencode-read-envelope

Conversation

@oakimov

@oakimov oakimov commented Jul 16, 2026 •

Copy link
Copy Markdown
Owner

What Problem This Solves

OpenCode's read tool wraps file content in an XML-ish envelope (<path>, <type>file</type>, <content>, numbered N: 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

  • Add unwrapReadOutput() to strip the envelope structurally (contiguous N: body run; no </content> substring search, so literal tags inside file lines are preserved).
  • Apply unwrap on native read_result always; on mcp_result only when toolName === "read" (OpenCode builtins arrive as MCP).
  • Thread toolName through tool-result replies and store it on pending execs so continuation unwrap gating does not depend solely on the prompt echoing the name.
  • Move trace() to src/debug.ts so protocol code does not import transport for logging.
  • Document the behavior in README; add regression tests for empty files, embedded tags, non-read MCP payloads, and ordered skeleton checks.
  • Widen @opencode-ai/plugin peer/dev range from ~1.17.13 to ^1.17.13 and lock to resolved 1.18.2 (with matching sdk) for current OpenCode plugin compatibility.

User Impact

  • Cursor-routed models receive raw file content from OpenCode read, not the wrapper.
  • Stops a class of silent file rewrites that previously corrupted source across multi-turn sessions.
  • Non-read MCP / tool output is left unchanged.
  • No auth, model list, or streaming behavior changes for end users.
  • Dev/peer installs can resolve newer OpenCode plugin 1.18.x.

Evidence

  • Unit coverage in test/tools.test.ts (unwrapReadOutput, buildTypedExecResult mcp/read paths) and test/session.test.ts (pending toolName).
  • bun test: 285 pass, 0 fail.
  • bun run typecheck: clean.
  • package.json / bun.lock: @opencode-ai/plugin ^1.17.13 → lockfile 1.18.2.

oakimov added 2 commits July 16, 2026 15:30
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.
@oakimov

oakimov commented Jul 16, 2026

Copy link
Copy Markdown
Owner Author

@renaudcerrato FYI, not sure if this is relevant for Kilo Code, but you may want to implement this too

@oakimov oakimov self-assigned this Jul 16, 2026
@renaudcerrato

Copy link
Copy Markdown
Contributor

@oakimov : thanks! For sure, I'm trying to merge every commits back in my fork - will do ! Thanks for the heads up

@oakimov
oakimov merged commit 37d3c8c into main Jul 16, 2026
@oakimov
oakimov deleted the fix/unwrap-opencode-read-envelope branch July 16, 2026 13:37
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants