Repository navigation
fix: support apply_patch hosts and report opencode's 50 KB read cap (#18) - #19
Conversation
) 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.
|
Tested Fault A —
|
Fixes #18 — both faults.
Fault A —
write/editunavailable on GPT modelsOpenCode 1.x does not merely add
apply_patchfor GPT models; it removeseditandwritefrom the catalog in exchange (ToolRegistry.tools— model id containsgpt-and neitherossnorgpt-4). Cursor keeps sending its native write/edit exec requests regardless, so every file change was refused as an unavailable tool.remapEditToolsForCatalogtranslates those intoapply_patchenvelopes:*** Add File:(which overwrites, per opencode's own test "adds file overwriting existing file", so no diff or existence check is needed)*** Update File:with a minimal@@chunk, widened to whole lines becauseapply_patchmatches line sequences whileeditis substring-basedUnreadable, absent, ambiguous, or overlapping edits are refused with a specific message rather than patched into the wrong region. Source files above 50 MB are refused too, since the expansion read is synchronous on the Run pump.
Keyed only off the advertised tool set — never a model id or host version — so it is inert wherever
edit/writeare offered normally, and inert again on OpenCode 2.0, which has no such substitution.Advertisement needed no change:
toolsToDescriptorsalready forwardsopencode-apply_patchto 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(Output capped at 50 KB. Showing lines X-Y. Use offset=N to continue.).unwrapReadOutputstripped that footer along with the envelope, so the model treated a capped read as the complete file and rewrote it — destroying everything past the cap.That also explains the reporter's numbers: 51,144 and 51,167 bytes, both just under 51,200, differing because the line boundary falls differently per file; and the 35.6 KB file surviving intact.
The envelope is still stripped byte-for-byte — preventing wrappers being echoed into writes is why it exists — but the cap is now re-stated:
read_result(native)[Partial read: …]marker appended after contentmcp_resultpi_read_resultPiReadExecSuccess.truncationReads the caller bounded itself with an explicit
offset/limitare not marked.The structured
truncatedflag was already being sent onread_resultand is not sufficient on its own — see the A/B below.Also
Decodes
WriteArgs.file_bytes(#5) andencoding_hint(#6). The schema declared only fields 1–3, and an internal-keys filter discarded both. Cursor's ownLocalWriteExecutorprefers bytes when non-empty, so a byte-encoded write would have decoded as empty content. Latent — never observed on the wire — but the schema now matchesagent.v1.WriteArgs.Verification
bun run typecheck,bun run build,bun test— 658 pass / 0 fail. Three new test files, including a deterministic reproduction that ports opencode's read accumulation loop and pins the cut at exactly 800 lines / 51,199 bytes for a 63-byte-line fixture.End-to-end against OpenCode 1.18.9:
cursor/gpt-5.4-miniadvertised[apply_patch, bash, …]— noedit, nowrite.cursor/grok-4.5advertisededit+writeand noapply_patch. The gate, live, on both sides.resultField=write_resultremapped totoolName=apply_patch; a 1200-line whole-file reversal completed with all lines intact.ZZZEND. I'm highly confident" — confidently wronggrok-4.5-high, same probe: "truncated by a 50 KB host limit and is not the complete file, so confidence is only moderate (~60%)".
Known issue, not addressed here
grok-4.5's ~76 KB single
writestalled and tripped the provider's ownCursor semantic-progress timeout after 120000ms. The file was left intact — no data loss — but the write never landed. That looks like a distinct third fault (large writes on the native channel stalling) and wants its own investigation; nobody should read this PR as proving large-file writes work end-to-end.Note on commit structure
Intended as two commits, one per fault, but the changes are interleaved within
tools.tsand non-interactive hunk splitting was not available. Squashed into one rather than risk patch surgery.Draft pending review.