Skip to content

v2 ACP: editing a filename containing a newline changes the file, then fails the session #54831

Description

@sishuaigong

Description

In OpenCode v2 over ACP, editing an existing file whose name contains a newline succeeds on disk and returns success to the model, but the client never receives completion. The session then fails with an internal error.

Tested on Linux at commit 4617210822bdbb31e5749751afbfb4584ccedac8, using diff 8.0.4. The relevant code remained unchanged on dev at e41e7e9064f1cb380f0905dd19c611756a577e79; that newer commit was not tested in a full session.

Plugins

No response

OpenCode version

No response

Steps to reproduce

Create a file:

python3 - <<'PY'
from pathlib import Path
Path('notes.txt\nx').write_text('hello\n')
PY

Through ACP, have the model call edit with:

{
  "path": "notes.txt\nx",
  "oldString": "hello",
  "newString": "world"
}

The simplified input above confirms the diff-parser failure. The full ACP failure was observed in an automated test using main.txt\nx and a longer replacement; the simplified ACP session was not rerun. Ordinary filenames do not trigger it.

Observed behavior and comparison

OpenCode changed the file and told the model Edited main.txt\nx (1 replacement). The client remained at in_progress, then received JSON-RPC error -32603: Internal error: Internal service failure. A subsequent shell call received no result.

In the same differential test, Claude Code, Codex, and DeepSeek Harness successfully edited the same filename and continued through both later calls. Gemini CLI rejected the filename with Invalid path: Path contains invalid characters (newlines or control characters), but continued normally. Only OpenCode failed the session.

Expected: a successful edit should report completion and allow further calls, even if its client diff cannot be displayed.

Screenshot and/or share link

No response

Operating System

No response

Terminal

No response

Activity

  1. opencode-agent commented on Oct 12, 2026

    @opencode-agent
    Contributor

    Thanks for the detailed report. I can confirm the underlying failure on the latest dev (e41e7e9), though only at the function level: I didn't run a full ACP session.

    When an edit finishes, the ACP layer rebuilds a client diff from the patch the tool returns (withCompletedDiffs in packages/cli/src/acp/permission.ts). For a file named notes.txt\nx, the newline breaks the patch header, and diff's parsePatch throws Unknown line 5 "x". That throw isn't caught the way other preview failures are. So instead of sending the completion without a diff, the update fails. Judging from the code in turn.ts, that would also end the turn, which fits the missing completion and the -32603 you saw. The same input with an ordinary filename works.

    The script below reproduces it with a small Bun test against that function. The relevant code is the same at the commit you tested (4617210).

    Reproduction script (acp-newline-filename.test.ts)
    // Copy into packages/cli/test/acp/ of an opencode checkout and run from packages/cli: bun test test/acp/acp-newline-filename.test.ts
    // Exercises the ACP completed-tool-call path (withCompletedDiffs) with the metadata core's edit tool
    // produces (fileDiff -> createTwoFilesPatch) for a file whose name contains a newline.
    import { test, expect } from "bun:test"
    import { Effect, Exit, Cause } from "effect"
    import { mkdtempSync, writeFileSync } from "node:fs"
    import { tmpdir } from "node:os"
    import { join } from "node:path"
    import { createTwoFilesPatch } from "diff"
    import * as ACPPermission from "../../src/acp/permission"
    
    const run = async (name: string) => {
      const cwd = mkdtempSync(join(tmpdir(), "acp-nl-"))
      writeFileSync(join(cwd, name), "world\n")
      const update = { sessionUpdate: "tool_call_update", toolCallId: "t1", status: "completed" } as any
      const source = {
        toolName: "edit",
        input: { path: name, oldString: "hello", newString: "world" },
        metadata: { files: [{ file: name, patch: createTwoFilesPatch(name, name, "hello\n", "world\n"), status: "modified", additions: 1, deletions: 1 }] },
      } as any
      return Effect.runPromiseExit(ACPPermission.withCompletedDiffs(update, source, cwd))
    }
    
    test("ordinary filename completes with a diff", async () => {
      const exit = await run("notes.txt")
      expect(Exit.isSuccess(exit)).toBe(true)
    })
    
    test("filename containing a newline should still complete", async () => {
      const exit = await run("notes.txt\nx")
      if (Exit.isFailure(exit)) console.log(Cause.pretty(exit.cause))
      expect(Exit.isSuccess(exit)).toBe(true)
    })

    Run it: bun acp-newline-filename.test.ts

  2. argszero commented on Oct 12, 2026

    @argszero
    Contributor

    I'd like to work on this.

    The failure is in packages/cli/src/acp/permission.ts, in withCompletedDiffs: the per-file preview pipeline wraps applyPatch in Effect.try but calls parsePatch(file.patch) bare, so the Unknown line 5 "x" that a newline in the filename causes is raised as a defect rather than as a typed failure. Effect.orElseSucceed at the end of that same pipeline only catches typed failures, so the defect escapes, the tool_call_update fails, and the turn ends — which matches both the missing completion and the -32603.

    Plan: make that call total the way its neighbour already is, so a patch whose header cannot be parsed yields no client diff and the tool call still completes. A preview is best effort (withCompletedDiffs already returns the update unchanged when it has no diffs), so I will also check the remaining unguarded step in that pipeline — structuredPatch after Patch.derive — rather than leave the next newline-shaped input to find it.

    Verification: your repro shape as a regression test in packages/cli/test/acp/, asserting the effect succeeds for a filename containing a newline, with the ordinary filename kept as the control case; red against the current code, green with the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions