Skip to content

fix: support apply_patch hosts and report opencode's 50 KB read cap (#18) - #19

Merged
oakimov merged 1 commit into
mainfrom
fix/18-apply-patch-and-read-cap
Aug 1, 2026
Merged

oakimov merged 1 commit into
mainfrom
fix/18-apply-patch-and-read-cap

Conversation

@oakimov

@oakimov oakimov commented Jul 31, 2026

Copy link
Copy Markdown
Owner

Fixes #18 — both faults.

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 — model id 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 into apply_patch envelopes:

  • whole-file write → *** Add File: (which overwrites, per opencode's own test "adds file overwriting existing file", so no diff or existence check is needed)
  • substring edit → *** Update File: with a minimal @@ chunk, widened to whole lines because apply_patch matches line sequences while edit is substring-based

Unreadable, 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/write are offered normally, and inert again on OpenCode 2.0, which has no such substitution.

Advertisement needed no change: toolsToDescriptors already forwards opencode-apply_patch 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 (Output capped at 50 KB. Showing lines X-Y. Use offset=N to continue.). unwrapReadOutput stripped 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:

path signal
read_result (native) [Partial read: …] marker appended after content
mcp_result separate notice content item, content stays byte-exact
pi_read_result structured PiReadExecSuccess.truncation

Reads the caller bounded itself with an explicit offset/limit are not marked.

The structured truncated flag was already being sent on read_result and is not sufficient on its own — see the A/B below.

Also

Decodes WriteArgs.file_bytes (#5) and encoding_hint (#6). The schema declared only fields 1–3, and an internal-keys filter discarded both. Cursor's own LocalWriteExecutor prefers 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 matches agent.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-mini advertised [apply_patch, bash, …] — no edit, no write. cursor/grok-4.5 advertised edit + write and no apply_patch. The gate, live, on both sides.
  • gpt-5.4-mini edit succeeded: native exec with resultField=write_result remapped to toolName=apply_patch; a 1200-line whole-file reversal completed with all lines intact.
  • Controlled A/B on a blind fixture (uniform lines, one unique token at the end, so the marker is the only possible signal):
marker gpt-5.4-mini
off "does not contain ZZZEND. I'm highly confident" — confidently wrong
on "confidence is low because the file output was truncated"

grok-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 write stalled and tripped the provider's own Cursor 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.ts and non-interactive hunk splitting was not available. Squashed into one rather than risk patch surgery.

Draft pending review.

)

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.
@renaudcerrato

Copy link
Copy Markdown
Contributor

Tested 259dc1e on cursor/gpt-5.4-mini and cursor/gpt-5.5-1m. Both faults confirmed fixed.

Fault A — write_args → apply_patch translation

Both models, new-file write:

exec: id=1 toolName=apply_patch resultField=write_result    ← native write_args remapped
exec: EMITTED tool-call toolName=apply_patch inputLen=174   ← patch emitted, no bash fallback

The resultField=write_result confirms it was a native write_args exec translated by remapEditToolsForCatalog, not a direct MCP call. No REFUSED unavailable tool on either model.

Existing-file edit (gpt-5.4-mini): native edit exec remapped to apply_patch with a 181-byte targeted patch. File edited correctly, no full-file rewrite.

Fault B — read truncation marker

1200-line / 76 KB file (gpt-5.4-mini), single read with no offset/limit:

First read returned 800 lines (51,199 bytes), capped at the host's 50 KB limit. The model then:

  • Recognized the truncation: "big.txt is larger than the initial read window"
  • Paged the rest with offset=801 (second read, 27,980 bytes)
  • Said "I've got the full file content now" before acting

Without the marker, the same model on the same file confidently echoed the 800 lines and stopped — believing it had the whole file.

Your three questions

1. Does the marker change model behavior? Yes, on gpt-5.4-mini. The model paged the remaining lines before writing, instead of treating the 800-line cap as the complete file. I did not A/B test with the marker off on my setup — you already have that evidence for gpt-5.4-mini and I confirmed the same model behaves as expected with it on.

2. Large writes stalling? Not on gpt-5.4-mini or gpt-5.5-1m in my testing. The whole-file writes I triggered were small (hello.txt). I did not test a ~76 KB single-call write end-to-end (the model chose targeted apply_patch MCP calls for larger edits, not full-file rewrites). So I can't confirm or deny the stall you saw on grok-4.5.

3. file_bytes on the wire? No. My field walk showed only fields 1/2/3 (path, file_text, tool_call_id) on every write_args exec I captured. I never observed field 5 populated.

@oakimov
oakimov marked this pull request as ready for review August 1, 2026 12:32
@oakimov
oakimov merged commit bfefd8f into main Aug 1, 2026
@oakimov
oakimov deleted the fix/18-apply-patch-and-read-cap branch August 1, 2026 12:34
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.

write/edit tool calls fail on GPT-5+ models — OpenCode advertises apply_patch only, provider rejects write_args/edit_tool_call as unavailable

2 participants