Skip to content

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

Closed

Description

@renaudcerrato

Summary

On Cursor models whose id matches OpenCode's gpt-5* substitution rule, OpenCode advertises apply_patch and hides write/edit. When the model then emits a native write_args (or edit_tool_call) exec — which it does regardless of the advertised MCP set — this provider maps it to write, the catalog check rejects it as "unavailable for the current agent", and the model falls back to bash printf > file.txt. File creation/editing still works, but loses the write/edit permission flow, LSP diagnostics, and file-history tracking.

Repro

opencode run -m cursor/gpt-5.5-1m "Create a file named hello.txt with: hi from cursor"

With CURSOR_PROVIDER_DEBUG=1, the advertised tools for the turn are:

extractTools: 12 incoming → 12 advertised [apply_patch,bash,custom_websearch,glob,grep,jbcontext_code_search,jbcontext_find_repositories,read,skill,task,todowrite,webfetch]

No write, no edit. The model reads hello.txt (missing), then emits write_args. The exec is rejected and the model retries with bash (printf > hello.txt).

Root cause (provider side)

OpenCode's tool registry substitutes apply_patch for edit/write on gpt-5* model ids (excluding oss and gpt-4*) — this is host-side behavior in OpenCode core, not something the provider can override. The provider then has two mappings that both target the now-unadvertised write:

  1. Exec path — write_args/pi_write_args → toolName: "write" (src/protocol/tools.ts:289,293, parse at :845). The catalog check at src/language-model.ts:1605 rejects it:

    OpenCode tool 'write' is unavailable for the current agent. ...
    

    (src/language-model.ts:1610)

  2. Display bridge — edit_tool_call → preferredToolName: "write" (src/protocol/tool-call-bridge.ts:202). The edit handshake first reads the target file; recoverMissingEditRead (src/language-model.ts:1121) is supposed to treat a missing-target read as an empty file so the model proceeds to the write step, but it bails early at src/language-model.ts:1129 because !advertisedToolNameSet.has("write"). The read goes to OpenCode, fails, and the model abandons the edit path.

Net: both native file-write paths the model can emit are dead-ended when only apply_patch is advertised, and the model degrades to bash.

What I expected

Either (a) the provider recognizes apply_patch is the advertised file-edit tool and translates write_args/pi_edit_args into an apply_patch call (synthesizing the V4 patchText), or (b) the edit-handshake recovery accepts apply_patch as a valid write target so the model isn't forced to bash.

Open question — write_args.file_text on large files

While investigating the above, I noticed something I couldn't fully explain and would value your take on. When the model uses write_args on a large existing file, the file_text field arrives at ~50 KB regardless of original size:

Original file write_args frame (decoded) file_text length Result
35.6 KB (1,200 lines) 35,867 B 35,605 B intact (under cap)
75.2 KB (2,520 lines) 51,417 B 51,144 B truncated to 1,716 lines
~540 KB (18,027 lines) 51,429 B 51,167 B truncated to 1,486 lines

Raw protobuf field walk of the 75 KB-case write_args frame showed exactly 3 fields (path, file_text, tool_call_id) — no blob_id or unknown field. The same Run stream carried KV set_blob_args frames with 125 KB / 108 KB / 103 KB of decoded data (full file content, 32-byte hash blob ids) around the same time.

I don't want to assert a cause — I couldn't determine whether the ~50 KB file_text is a Cursor server-side cap, an intended reference-only message the provider should resolve via the KV channel, a hand-written proto schema gap, or a frame-reassembly issue on this provider's side. Cursor's native client edits large files without corruption, so a hard cap on write_args seems unlikely by design. Flagging it in case you have visibility I don't.

Environment

  • Provider: cursor-opencode-provider @ main (36a5952, v0.2.9)
  • Model: cursor/gpt-5.5-1m

Activity

  1. oakimov commented on Jul 31, 2026

    @oakimov
    Owner

    Both faults are fixed in #19 (draft) on branch fix/18-apply-patch-and-read-cap. Summary of the diagnosis, since the second one turned out not to be what it looked like.

    Fault A — you were right, and the root cause is upstream

    OpenCode 1.x doesn't merely add apply_patch for GPT models — it removes edit and write from the catalog in exchange. From ToolRegistry.tools:

    // use apply tool in same format as codex
    const usePatch =
      input.modelID.includes("gpt-") && !input.modelID.includes("oss") && !input.modelID.includes("gpt-4")
    if (tool.id === ApplyPatchTool.id) return usePatch
    if (tool.id === EditTool.id || tool.id === WriteTool.id) return !usePatch

    It's a filter, not a rename, so the provider's fixed write_args → write mapping resolved to a name that wasn't in the catalog and every write was refused. Confirmed live: cursor/gpt-5.4-mini advertises apply_patch and no edit/write, while cursor/grok-4.5 advertises edit + write and no apply_patch.

    The exclusivity is deliberate — a maintainer on opencode#34491 describes the gate as a "band-aid" whose purpose is that "GPT no longer receives edit + write + apply_patch simultaneously", and notes a plugin-side opt-out "isn't feasible today". opencode#19942 reported the same plugin-hostile behavior and was closed not_planned. opencode#35408 (open) would move the policy into a hook with a mutable tool roster, at which point this shim can retire. OpenCode 2.0 has no such gate, so this is 1.x-only in practice.

    On your two proposed fixes: (b) alone is insufficient — recoverMissingEditRead only covers the read-before-write handshake, so the actual write_args execs would still be refused. (a) is the substantive one, and that's what's implemented. One correction: the rejection is at language-model.ts:1620, not :1605.

    Fault B — the truncation is on the read, not the write

    This is not a Cursor server-side cap. OpenCode's own 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.). The provider strips that footer along with the read envelope — deliberately, so the model can't echo wrappers into a later write — which left the model believing a capped read was the complete file. It then rewrote the file from a partial view and everything past the cap was lost.

    That accounts for all three of your data points: 51,144 and 51,167 both sit just under 51,200 and differ because the line boundary lands differently per file, and the 35.6 KB file was under the cap so it survived. Cursor's own CLI uses the identical 50 KiB budget (const io = 51200), which is why a server cap looked plausible — coincidence of constants, not causation. Its write executor applies no cap at all, exactly like ours; it just never truncates a read without saying so.

    Your field walk was also correct that only fields 1/2/3 were on the wire — but agent.v1.WriteArgs actually has six, including file_bytes (#5). Cursor's executor prefers bytes when non-empty, so a byte-encoded write would have decoded as empty content here. Fixed too, though it's latent rather than something you hit.

    What changed

    Native writes/edits are translated into apply_patch envelopes when — and only when — the host advertises apply_patch and withholds edit/write. And capped reads now say so: a [Partial read: …] marker on native read_result, a separate notice item on mcp_result, structured truncation on Pi reads. Deliberately paged reads are left alone.

    Worth flagging: the structured truncated flag was already being sent and made no difference. In a controlled A/B on a fixture where the marker is the only possible signal, gpt-5.4-mini said "highly confident" the partial content was the whole file with the marker off, and "confidence is low because the file output was truncated" with it on. grok-4.5 behaved the same way.

    One thing this does not fix

    grok-4.5 writing a ~76 KB file in one call stalled and tripped the provider's own Cursor semantic-progress timeout after 120000ms. No data loss — the file was left intact — but the write never landed. That looks like a separate fault and wants its own issue; please don't read #19 as proving large-file writes work end to end. Happy to open one if you've seen it too.

  2. oakimov commented on Jul 31, 2026

    @oakimov
    Owner

    @renaudcerrato — would you be willing to test this against your setup before I take #19 out of draft? You hit both faults on real usage and I'd rather not land it on my repro alone.

    The branch is fix/18-apply-patch-and-read-cap, cut from current main (v0.3.0 — you reported on 36a5952/v0.2.9, so there are unrelated changes in between).

    Installing it

    There's no prepare script, so installing straight from the git URL won't build dist/. It needs a clone:

    git clone -b fix/18-apply-patch-and-read-cap https://github.com/oakimov/cursor-opencode-provider.git
    cd cursor-opencode-provider
    bun install && bun run build

    Then point your host config at the built entry — for OpenCode, in ~/.config/opencode/opencode.json:

    { "plugin": ["/abs/path/to/cursor-opencode-provider/dist/index.js"] }

    Fault A — your original repro

    opencode run -m cursor/gpt-5.5-1m "Create a file named hello.txt with: hi from cursor"

    Expected: the file is created through apply_patch, with no bash printf fallback and no "unavailable for the current agent" rejection. If you set CURSOR_PROVIDER_DEBUG=1 you should see toolName=apply_patch resultField=write_result — the write_result part is what tells you it came from a native write_args and was translated, rather than the model calling opencode-apply_patch directly.

    Worth also trying an edit of an existing file, which takes the other path (substring → *** Update File: widened to whole lines). That one refuses rather than guessing if the target text isn't unique, so if you see a refusal I'd like the message.

    Fault B — the ~50 KB truncation

    bun -e 'await Bun.write("big.txt", Array.from({length:1200},(_,i)=>{const id=String(i+1).padStart(4,"0");return `LINE ${id} ${".".repeat(44)} KEEP${id}`}).join("\n")+"\n")'

    That's 1200 lines × 63 bytes = 76,800 bytes. Ask the model to read it in a single call with no offset/limit and report the last line it received, then whether it got the whole file.

    Expected: it receives 800 lines (LINE 0800 … KEEP0800 — the cap lands there exactly for 63-byte lines) and says so, rather than claiming that's the end of the file. Then try a whole-file rewrite and check wc -l big.txt is still 1200 and grep -c KEEP1200 is still 1.

    What I'd most value

    1. Does the marker actually change your model's behavior? This is the part I'm least sure generalizes. gpt-5.5-1m isn't a model I tested — I used gpt-5.4-mini and grok-4.5. The structured truncated flag was already being sent before this change and both of those ignored it completely; only a marker in the content text moved them. If yours ignores the marker too, I want to know, because the fix is then incomplete rather than done.
    2. Have you seen large writes stall? grok-4.5 writing ~76 KB in one call tripped the provider's own Cursor semantic-progress timeout after 120000ms for me. No data loss, but the write never landed. That looks like a third, separate fault and I've deliberately not claimed it's fixed. If you've hit it I'll open a dedicated issue.
    3. Any sign of file_bytes on the wire? WriteArgs field 5 is now decoded, but I've never observed Cursor populating it — your field walk showed only 1/2/3, same as mine. It's a schema-correctness fix, not a confirmed bug, and I'd be glad to hear if it ever fires for you.

    Full diagnosis and the A/B evidence are in #19. Thanks for the writeup on the original report — the field-number walk on WriteArgs is what made the second fault tractable.

  3. self-assigned this
    on Jul 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions