Skip to content

tui: cyclic Mermaid subgraphs overflow the renderer stack #60341

Open

Description

@andreivince

Description

A Mermaid flowchart can overflow the TUI renderer stack when repeated subgraph IDs form a cycle. Rendering should stay responsive and show the source if the diagram cannot be drawn.

Steps to reproduce

Open a session whose assistant message contains this Mermaid block:

```mermaid
flowchart TD
subgraph A
subgraph A
N[First]
end
end
```

This self-cycle and a two-ID cycle both fail with RangeError: Maximum call stack size exceeded in descendantsOf. Both were reproduced with isolated imported sessions on dev 726cf096.

OpenCode version

Source checkout at 726cf096396d5014c3b1845e231cc01efe0421ee.

Operating System

macOS ARM64, Ubuntu 24.04 and Windows Server 2025.

Terminal

Native PTY sessions.

Activity

  1. changed the title [-]tui: cyclic Mermaid subgraphs hang or crash rendering[/-] [+]tui: cyclic Mermaid subgraphs overflow the renderer stack[/+] on Oct 29, 2026
  2. opencodejr commented on Oct 29, 2026

    @opencodejr
    Contributor

    Thanks for the clear report. I reproduced this on the latest dev (bc7da10f3), running from source.

    I used a simulated assistant reply containing your flowchart TD / subgraph A / subgraph A / N[First] / end / end block. The TUI never renders the rest of the message and switches to the "OpenCode crashed" screen with Maximum call stack size exceeded. The stack trace is repeated descendantsOf frames in packages/merman/src/flowchart/parser.ts. In my run the self-cycle crashed with the stack overflow rather than hanging. Calling the flowchart parser directly throws the same RangeError for both the self-cycle and a two-ID cycle (A > B > A).

    The recording and screenshot below show it, and the script below reproduces it.

    recording.mp4

    Screenshot

    Reproduction script (drive.ts)
    import { Effect } from "effect"
    import { Stream } from "effect"
    import { Llm, OpenCodeDriver } from "opencode-drive"
    
    // Assistant reply containing a Mermaid flowchart whose subgraph IDs form a cycle.
    // Set CYCLE=two for the two-ID cycle (A > B > A).
    const diagram = process.env.CYCLE === "two"
      ? "flowchart TD\nsubgraph A\nsubgraph B\nsubgraph A\nN[First]\nend\nend\nend"
      : "flowchart TD\nsubgraph A\nsubgraph A\nN[First]\nend\nend"
    const reply = "Here is the diagram:\n\n```mermaid\n" + diagram + "\n```\n\nAFTER-DIAGRAM-MARKER"
    
    export default OpenCodeDriver.use(
      {
        ...(process.env.OC_DEV ? { opencode: { dev: process.env.OC_DEV } } : {}),
        tui: { recording: true },
      },
      ({ ui, llm }) =>
        Effect.gen(function* () {
          yield* ui.screenshot("01-home")
          yield* llm.serve(() => Stream.make(Llm.text(reply)))
          yield* ui.submit("Draw the diagram")
          const result = yield* ui.waitFor("AFTER-DIAGRAM-MARKER", { timeout: 20000 }).pipe(
            Effect.as("rendered"),
            Effect.catch((e: unknown) => Effect.succeed(`FAILED: ${String((e as any)?._tag ?? e)}`)),
          )
          yield* Effect.log(`result: ${result}`)
          yield* ui.screenshot("02-after").pipe(Effect.catch((e: unknown) => Effect.log(`screenshot failed: ${String((e as any)?._tag ?? e)}`)))
        }),
    )

    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

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions