Skip to content

fix(merman): avoid cyclic subgraph recursion - #60343

Closed
andreivince wants to merge 2 commits into
anomalyco:devfrom
andreivince:merman-subgraph-cycles
Closed

andreivince wants to merge 2 commits into
anomalyco:devfrom
andreivince:merman-subgraph-cycles

Conversation

@andreivince

@andreivince andreivince commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #60341

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Rendering cyclic Mermaid subgraphs overflows the TUI renderer stack. This checks the parent graph before recursive endpoint resolution and uses the existing source fallback when a cycle is found. Acyclic repeated IDs keep their current rendering.

How did you verify your code works?

The parser regressions and the streaming Markdown check fail on unmodified dev and pass here. All 511 Merman tests, the official compiler and bun run check pass locally. Linux and Windows checks of the same production changes cover the CLI/TUI suites, uncached root checks and native executable fallback. macOS application checks also cover streamed recovery and all three Markdown consumers. The final test simplification was verified on macOS.

Visual

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 72" width="240" height="72" fill="none">
  <style>
    @keyframes pulse { 0%,100% { opacity:.35 } 50% { opacity:1 } }
    .flow { animation:pulse 2.8s ease-in-out infinite }
    @media (prefers-reduced-motion:reduce) { .flow { animation:none } }
  </style>
  <g stroke="currentColor" stroke-width="1.5">
    <rect x="10" y="12" width="64" height="48" rx="5" opacity=".45"/>
    <rect x="23" y="28" width="38" height="23" rx="3" opacity=".8"/>
    <path class="flow" d="M87 36h29m-6-5 6 5-6 5" stroke-linecap="round" stroke-linejoin="round"/>
    <rect x="130" y="10" width="100" height="52" rx="5" opacity=".6"/>
  </g>
  <g fill="currentColor" font-family="monospace" font-size="9">
    <text x="18" y="24">A</text>
    <text x="38" y="43">A</text>
    <text x="139" y="25">flowchart TD</text>
    <text x="139" y="39" opacity=".7">subgraph A</text>
    <text x="139" y="53" opacity=".7">...</text>
  </g>
</svg>

Screenshots / recordings

The fallback uses the existing code block rendering.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #48573

Copilot AI balanced review requested due to automatic review settings October 29, 2026 19:10

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks correct and fixes the crash. With the issue's reproduction in the TUI (simulated model), the base shows the "OpenCode crashed" screen with Maximum call stack size exceeded in descendantsOf. With this change, both the self-cycle and the A > B > A cycle render as a plain code block and the rest of the message shows. All 509 merman tests pass, the new test fails on the base, and tsc --noEmit is clean.

The early return for unique IDs is safe, because a parent always opens before its child. The cycle check covers exactly the cycles that made descendantsOf recurse forever. It also protects the while (parentId) walks in the layout and routing code. While streaming, an unclosed cyclic diagram still fails with the existing "unclosed subgraph" error first, so the last good frame stays until the block completes.

Public API: unchanged. MermaidSubgraphCycleError is internal to the private @opencode/merman package.

One concern is the size of the test (inline).

Related: #48573

Before

After

import { expect, test } from "bun:test"
import { fileURLToPath } from "node:url"

test("keeps cyclic subgraph source visible without blocking Markdown rendering", async () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a lot of test for the fix: a separate fixture process, a 5 s kill timeout, a full Markdown renderer harness and several streamed frames, 141 lines in total. Since the cycle now fails fast with MermaidSubgraphCycleError instead of hanging, the core behaviour can be checked in-process in a few lines: parseMermaidFlowchartDiagram throws it for the self-cycle and the A > B > A cycle, and still parses an acyclic repeated ID. Could you cut it down to that? At most add one small renderer check that the cycle returns undefined, so the code block fallback is used.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My earlier point about the test size is addressed. The subprocess fixture is gone, replaced by two parser cases plus an acyclic repeated-ID case in diagnostics.test.ts, and a short cyclic fallback check added to the existing streaming test in markdown.test.ts. The non-test code hasn't changed since the last review, where I confirmed the fix in the TUI. All 511 merman tests pass and tsc --noEmit is clean. Public API: unchanged. No new concerns.

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tui: cyclic Mermaid subgraphs overflow the renderer stack

2 participants