Skip to content

Feature/2833 add randomize config option for architecture diagrams#7457

Open
jeroensmink98 wants to merge 7 commits intomermaid-js:developfrom
jeroensmink98:feature/2833_add_randomize_config_option_for_architecture_diagrams
Open

Feature/2833 add randomize config option for architecture diagrams#7457
jeroensmink98 wants to merge 7 commits intomermaid-js:developfrom
jeroensmink98:feature/2833_add_randomize_config_option_for_architecture_diagrams

Conversation

@jeroensmink98
Copy link

@jeroensmink98 jeroensmink98 commented Mar 6, 2026

Summary

Add a randomize config option to architecture diagrams (architecture-beta) that controls whether initial node positions are randomized before running the fcose layout algorithm.

The cytoscape-fcose library defaults randomize to true, which causes architecture diagrams to produce a different layout on every render. This change defaults randomize to false, making layouts deterministic and identical across renders. Users can opt into randomized layout by setting randomize: true via frontmatter or mermaid.initialize().

Resolves #2833
Resolves #6167

Design Decisions

  • Schema-driven config: The randomize boolean is added to ArchitectureDiagramConfig in config.schema.yaml, following the existing pattern used by padding, iconSize, and fontSize. The TypeScript types in config.type.ts are auto-generated from the schema.
  • No changes to defaultConfig.ts or architectureDb.ts: The default value (false) is defined in the schema and flows through the existing getConfigField() mechanism, so no manual wiring in the DB or default config is needed.
  • quality: 'proof' compatibility: The renderer already sets quality: 'proof', which is required when randomize: false (fcose logs a warning if quality is 'draft' with randomize: false).
  • Un-skipped E2E tests: The architecture Cypress snapshot tests were wrapped in describe.skip precisely because layouts were non-deterministic. With this fix, they are re-enabled and include a new complex deeply-nested diagram test to guard against regressions.
  • Demo page: Added demos/dev/architecture-randomize.html with two versions of the same complex diagram (one deterministic, one randomized) for manual verification.

Tasks

Make sure you

  • 📖 have read the contribution guidelines
  • 💻 have added necessary unit/e2e tests.
  • 📓 have added documentation. Make sure MERMAID_RELEASE_VERSION is used for all new features.
  • 🦋 If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Screenshots

The screenshots below show the output of demos/dev/architecture-randomize.html:

  • Upper diagram (randomize: false, default): The layout stays the same after a page refresh.
  • Lower diagram (randomize: true): The layout changes after each refresh.

Screenshot 1:

Screenshot 2026-03-06 at 12 49 28

Screenshot 2:

Screenshot 2026-03-06 at 12 49 38

Screenshot 3:

Screenshot 2026-03-06 at 12 49 28

This shows that the default configuration produces deterministic layouts, while randomize: true restores the previous randomized behavior for users who want it.

…zation demo

- Re-enabled tests for architecture diagrams, ensuring deterministic layouts.
- Added a new test for complex deeply-nested diagrams.
- Introduced a new HTML demo for testing architecture randomization configurations.
…layout

- Introduced a new `randomize` configuration option to allow random initial node positions in architecture diagrams.
- Updated the configuration schema and types to include the new option, defaulting to `false` for deterministic layouts.
- Modified the architecture renderer to utilize the `randomize` setting during layout calculations.
@netlify
Copy link

netlify bot commented Mar 6, 2026

Deploy Preview for mermaid-js ready!

Name Link
🔨 Latest commit 3f4df67
🔍 Latest deploy log https://app.netlify.com/projects/mermaid-js/deploys/69b3db265b941400082400b9
😎 Deploy Preview https://deploy-preview-7457--mermaid-js.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions bot added the Type: Enhancement New feature or request label Mar 6, 2026
@changeset-bot
Copy link

changeset-bot bot commented Mar 6, 2026

🦋 Changeset detected

Latest commit: 3f4df67

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
mermaid Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 6, 2026

Open in StackBlitz

@mermaid-js/examples

npm i https://pkg.pr.new/@mermaid-js/examples@7457

mermaid

npm i https://pkg.pr.new/mermaid@7457

@mermaid-js/layout-elk

npm i https://pkg.pr.new/@mermaid-js/layout-elk@7457

@mermaid-js/layout-tidy-tree

npm i https://pkg.pr.new/@mermaid-js/layout-tidy-tree@7457

@mermaid-js/mermaid-zenuml

npm i https://pkg.pr.new/@mermaid-js/mermaid-zenuml@7457

@mermaid-js/parser

npm i https://pkg.pr.new/@mermaid-js/parser@7457

@mermaid-js/tiny

npm i https://pkg.pr.new/@mermaid-js/tiny@7457

commit: 4cdc2c9

@codecov
Copy link

codecov bot commented Mar 6, 2026

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 3.53%. Comparing base (adcf722) to head (3f4df67).
⚠️ Report is 118 commits behind head on develop.

Files with missing lines Patch % Lines
.../src/diagrams/architecture/architectureRenderer.ts 0.00% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           develop   #7457      +/-   ##
==========================================
- Coverage     3.53%   3.53%   -0.01%     
==========================================
  Files          490     495       +5     
  Lines        48968   49213     +245     
  Branches       766     772       +6     
==========================================
+ Hits          1733    1741       +8     
- Misses       47235   47472     +237     
Flag Coverage Δ
unit 3.53% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
packages/mermaid/src/config.type.ts 100.00% <ø> (ø)
.../src/diagrams/architecture/architectureRenderer.ts 0.24% <0.00%> (-0.01%) ⬇️

... and 45 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@argos-ci
Copy link

argos-ci bot commented Mar 6, 2026

The latest updates on your projects. Learn more about Argos notifications ↗︎

Build Status Details Updated (UTC)
default (Inspect) ⚠️ Changes detected (Review) 14 added Mar 13, 2026, 9:48 AM

- Updated the syntax for connections and labels in architecture diagram tests for consistency.
- Added a new test case to validate rendering with the `randomize` option set to true, ensuring no errors occur during the process.
@knsv
Copy link
Collaborator

knsv commented Mar 11, 2026

Thanks, it makes sense!

I will let my assistant do a review. I am trimming it in, hoping for greatness. It is ok to comment on the review if it comes out badly. I am reviewing it as well :)

Btw, spontaneously, I would also want the ability to set the number. This is to make it behave deterministically while still allowing it to be affected.

Copy link
Collaborator

@knsv-bot knsv-bot left a comment

Choose a reason for hiding this comment

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

[sisyphus-bot]

Review: PR #7457 — Add randomize config option for architecture diagrams

Thanks for this contribution, @jeroensmink98! Non-deterministic layouts have been a long-standing pain point for architecture diagrams (and the reason the E2E tests were skipped), so this is a really valuable fix. The approach is clean and well-considered — let's get it across the finish line.


What's working well

🎉 [praise] Excellent design decision to follow the schema-first config workflow. Adding randomize to config.schema.yaml and letting it flow through getConfigField() is exactly the right pattern — no manual wiring needed in architectureDb.ts or defaultConfig.ts.

🎉 [praise] Un-skipping the architecture E2E tests is a huge win. These have been describe.skip'd for a long time. Updating the edge syntax to the current format (db:L -- R:server instead of db L--R server) while you're at it is great housekeeping.

🎉 [praise] The screenshot: false option on the randomize: true E2E test is smart — it verifies the diagram renders without errors while avoiding flaky snapshot comparisons from the non-deterministic layout.

🎉 [praise] The demo page (architecture-randomize.html) with side-by-side deterministic vs randomized diagrams is a nice touch for manual verification. Screenshots in the PR body clearly demonstrate the fix.


Things to address

🟡 [important] Changeset bump vs prefix mismatch — The changeset uses patch bump but the message starts with feat:. Per project conventions, features should use minor and fixes should use patch. This change is arguably both — it fixes non-deterministic behavior (#2833) while adding a new config option. I'd suggest either:

  • Change to minor bump with feat: prefix (since it adds a new public config key), or
  • Keep patch but change the prefix to fix: (since the primary value is deterministic layouts)

Either is defensible — just make them consistent. File: .changeset/fix-architecture-deterministic-layout.md

🟡 [important] Documentation is missing — The PR template checkbox for documentation is unchecked. Since randomize is a new user-facing config option, it would be great to add a brief mention in the architecture diagram docs (packages/mermaid/src/docs/syntax/architecture.md or equivalent). Just a note in the configuration section explaining the option and that layouts are deterministic by default. Remember to use the MERMAID_RELEASE_VERSION placeholder for the version.

🟢 [nit] Generated file committed directlyconfig.type.ts appears in the diff with 7 added lines. It looks like the regeneration was run correctly (the output matches the schema), but worth confirming this was generated via pnpm run --filter mermaid types:build-config rather than hand-edited. CI's types:verify-config check should catch any mismatch, so this is just a verification note.

💡 [suggestion] Consider adding randomize to the required array — The existing architecture config fields (padding, iconSize, fontSize) are listed in the required array in the schema (config.schema.yaml:965). The new randomize field is not in that list. While it works fine with a default, adding it to required would be consistent with the sibling fields. Not blocking — the default value ensures it works either way.


Security

No XSS or injection issues identified. The change passes a schema-validated boolean (true/false) directly to the fcose layout engine's randomize option. No user-controlled strings are introduced into DOM sinks, SVG output, or sanitization paths.


Self-check

  • At least one 🎉 [praise] item exists
  • No duplicate comments
  • Severity tally: 🔴 0 / 🟡 2 / 🟢 1 / 💡 1 / 🎉 4
  • Verdict matches criteria: COMMENT (1-2 🟡)
  • Not a draft PR — COMMENT is appropriate
  • No inline comments used
  • Tone check: constructive and collaborative ✓

…in architecture diagrams

- Introduced a new section in the architecture documentation detailing the `randomize` option for initial node positions.
- Provided examples for enabling randomization via frontmatter and `mermaid.initialize()`.
- Updated the configuration table to include the new option and its default behavior.
@jeroensmink98
Copy link
Author

Thanks for the feedback! sofar I have made the following changes

  • Changeset: Switched to minor bump with feat: prefix to match feature semantics (new public config key).
  • Documentation: Added a Configuration section to the architecture docs (packages/mermaid/src/docs/syntax/architecture.md) documenting architecture.randomize with usage examples and v<MERMAID_RELEASE_VERSION>+ version marker.
  • Generated types: Confirmed config.type.ts was generated via pnpm --filter mermaid types:build-config and types:verify-config passes.
  • Schema required list: Added randomize to the required array for consistency with padding, iconSize, fontSize.
  • Seed support: Good idea -- will explore in a follow-up commit to investigate whether cytoscape-fcose supports stable seeding.

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

Labels

Type: Enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Architecture diagram - non uniform spacing of icons How to make diagrams stable

3 participants