Skip to content

core: grep fails entirely when a matching line is not valid UTF-8 #51912

Closed

Description

@nkootstra

Description

Grep fails with Invalid ripgrep match output and returns no matches at all when any matching line isn't valid UTF-8, such as a Latin-1 or GBK source file. Matches in other files are lost too.

rg --json reports data that isn't valid UTF-8 as { "bytes": "<base64>" } instead of { "text": "..." }. RawMatch in packages/core/src/ripgrep.ts only accepts text for path, lines and submatches[].match. The failed decode fails the whole result stream, not just that row.

This is the same problem as #23629, which was auto-closed as stale. That report was against the old packages/opencode grep; the current packages/core implementation has the same gap.

OpenCode version

2.0.18 (96dd9f7)

Steps to reproduce

  1. Create utf8.txt containing needle, and latin1.txt containing the bytes needle caf followed by 0xE9.
  2. Grep for needle in that directory.
  3. Grep fails with Invalid ripgrep match output instead of returning both matches.

Operating System

macOS 26

Activity

  1. github-actions commented on Sep 28, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of an existing issue. Please check:

  2. opencodejr commented on Oct 28, 2026

    @opencodejr
    Contributor

    Thanks for the clear report. I was able to reproduce this on 2.0.18, and it still happens on the latest dev (86ce19d).

    Steps: in a project containing utf8.txt (needle) and latin1.txt (needle caf + byte 0xE9), the model calls grep with pattern needle. The tool returns Invalid ripgrep match output and no matches at all, so the valid utf8.txt match is lost too. In the TUI the step shows as a failed search. The recording below (from 2.0.18) shows it, and the script below reproduces it with a simulated model.

    This matches your analysis: RawMatch in packages/core/src/ripgrep.ts only accepts { text }, while ripgrep sends { bytes } for non-UTF-8 data, and the decode failure aborts the whole stream. Related: #23629 (the same symptom in the old v1 grep) and your PR #51913.

    recording.mp4

    Screenshot

    Reproduction script (drive.ts)
    // Reproduces #51912: grep fails when a matching line is not valid UTF-8.
    import { Effect, Stream } from "effect"
    import { Llm, OpenCodeDriver } from "opencode-drive"
    
    const latin1 = new Uint8Array([...new TextEncoder().encode("needle caf"), 0xe9, 0x0a])
    
    export default OpenCodeDriver.use(
      {
        ...(process.env.OC_DEV ? { opencode: { dev: process.env.OC_DEV } } : {}),
        project: {
          git: true,
          files: {
            "utf8.txt": "needle\n",
            "latin1.txt": latin1,
          },
        },
        tui: { recording: true },
      },
      ({ ui, llm }) =>
        Effect.gen(function* () {
          yield* llm.serve((request, index) => {
            const body = JSON.stringify(request)
            // First turn (no grep result yet): ask for a grep call.
            if (!body.includes("call_grep"))
              return Stream.make(
                Llm.toolCall({ index: 0, id: "call_grep", name: "grep", input: { pattern: "needle" } }),
                Llm.finish("tool-calls"),
              )
            // Print what the grep tool returned to the model.
            const hit = body.match(/"[^"]*(Invalid ripgrep|latin1\.txt|utf8\.txt)[^"]*"/g)
            console.log("GREP TOOL RESULT SEEN BY MODEL:", hit)
            return Stream.make(Llm.text("Grep done."))
          })
          yield* ui.submit("Search for needle")
          yield* ui.waitFor("Grep done.", { timeout: 30000 })
          yield* Effect.sleep("1 second")
          yield* ui.screenshot("grep-result")
        }),
    )

    Run it with opencode-drive: opencode-drive run ./drive.ts. It drives opencode from your PATH; set OC_DEV to the path of an OpenCode checkout to run that from source instead.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions