LeanUI Docs
← Open LeanUI Studio GitHub

LeanUI Architecture, Graph & Agentic Engineering Guidelines#

This guide defines the recommended patterns for structuring multi-project LeanUI Solutions, ViewModels, Views, ControlTemplates, Data Pipelines, Lean 4 Proofs, and Agentic / GenUI workflows so apps remain fast to author, easy to navigate at scale, and formally verifiable with 0 errors.


1. Multi-Project Solution Structure#

For any app larger than a single demo screen, organize work as a layered LeanUI Solution (.leanui-sln.json) with strictly acyclic project.references:

text
MyEnterpriseSolution
├── AppProofs        (namespace: Proofs)   — no project references
│   └── Proofs/      (lean_type, lean_def, lean_theorem, shared invariants)
├── AppCore          (namespace: Core)     — references: [AppProofs]
│   ├── Stores/      (state, collection, computed, predicate, action)
│   ├── ViewModels/  (screen-facing ViewModels & state_machine FSMs)
│   ├── Data/        (datasource, data_query pipelines)
│   └── Workflows/   (cross-screen orchestrators)
├── AppUIKit         (namespace: UIKit)    — references: [AppCore]
│   ├── Design/      (WCAG AA/AAA style tokens, multi-locale resource bundles)
│   ├── Components/  (reusable view components)
│   └── Templates/
│       └── Controls/ (control_template blueprints)
└── AppScreens       (namespace: Screens)  — references: [AppCore, AppUIKit]
    └── Views/
        ├── Dashboards/
        └── Forms/

Rules#

  • Single Entry Screen: Always configure entryModuleId to your primary screen so App Flow reachability checks and Prototype HTML export start cleanly.
  • Typed Cross-Module Links: Connect modules with WorkflowLinks (state_sync from ViewModel → View, navigate between Screens, event for cross-module commands, dependency for shared Design/Proof contracts).
  • Auto-Organization: Run the organize_solution MCP tool (or /organize in Studio Agent chat) to move modules into canonical folders, prune empty folders, validate reference DAGs, and clean broken links.

2. ViewModel, State & Finite State Machine Design#

  • Minimal Canonical State: Store only primary source-of-truth values in state (Nat, Int, Bool, String) and collection nodes. Derive all KPIs, booleans, and validation messages via computed, predicate, and validator.
  • Total & Bounded Actions: Keep every action transition total and within bounds (for example Math.min(maxLoad, cpuLoad + 10) and Math.max(0, cpuLoad - 10)) so Lean 4 invariant preservation theorems (step_preserves_inv) prove cleanly without manual surgery.
  • Explicit FSMs: Use state_machine nodes for lifecycle workflows (Draft → Review → Published → Archived) with guard expressions and terminal states, validated via step_state_machine.
  • Fast Authoring: Prefer build_viewmodel over sequential add_node calls to scaffold states, computeds, actions, FSMs, and invariants wired and laid out in a single atomic call.

3. View Hierarchy, Bindings & Accessibility#

  • Strict Layout Tree: Root every View in a semantic layout node (container, card, grid, split_view, or scroll). Wire parent.children → child.parent without cycles and avoid floating unparented controls.
  • Reactive Bindings: Wire ViewModel outputs (val, out) to View inputs (value, text, disabled, visible) and wire View events (onClick, onChange, onSubmit) back to action.trigger.
  • Accessible by Construction: Give every interactive control (button, input, slider, toggle, select, modal) a meaningful label / ariaLabel, and wire verified WCAG AA (>= 4.5:1) or AAA (>= 7.0:1) style tokens plus multi-locale resource bundles.
  • Fast Authoring: Prefer build_view_ui to construct an entire nested View hierarchy with automatic ViewModel bindings and layout in one call.

4. Graph Hygiene at Scale#

  • Target Size: Keep individual module graphs under ~35 nodes. Extract cohesive logic or UI subtrees into subprogram nodes (manage_subprogram_or_group) or promote them into dedicated modules linked with module_ref + WorkflowLink.
  • Visual Grouping: Frame logical regions (State & Invariants, Actions, Design Tokens, View Tree) using group nodes.
  • Clean Routing: Use reroute waypoints or wireless portal_out / portal_in pairs for long cross-canvas signals, then run apply_layout(mode="grouped") (or "layered" for linear pipelines).

5. Formal Lean 4 Proofs#

  • Baseline Safety: Pair every bounded state variable with an invariant node and verify with verify_graph + prove_lean.
  • Domain Proof Rules: Add Hoare transition rules (ruleKind: "transition"), view constraints (ruleKind: "view_constraint"), and reachability witnesses (ruleKind: "reachability").
  • Reusable Formal Libraries: Place shared lean_type (inductive/structure), lean_def (app-callable pure functions), lean_theorem, and lean_tactic nodes in a dedicated Proofs/ project.
  • Zero sorry: Use decidable, automated tactics (omega, simp, decide, trivial) and validate with build_proof_suite(verifyWithLean: true).

6. High-Velocity Agent, 16-Widget GenUI & ACP v2 Workflow#

When working with LeanUI's built-in agent, API providers (Gemini, OpenAI/Codex, Claude), local subscription CLIs (codex, claude, gemini), or ACP Latest (v2) (Local STDIO or Remote HTTP+SSE):

TaskFast Macro MCP ToolWhy It's Faster
Check guidelinesget_architecture_guidelinesInstant structured rules + anti-patterns by topic
Check exact portsdescribe_node_kindsZero port-name guessing before wiring
Find anything across Solutionsearch_solutionInstant cross-project search for modules, nodes, diagnostics
Build a ViewModelbuild_viewmodelStates, computeds, actions, FSMs & invariants in 1 call
Build a View screenbuild_view_uiFull nested View tree + ViewModel bindings + layout in 1 call
Author Lean 4 proofsbuild_proof_suiteTypes, defs, theorems, proof rules & Lean check in 1 call
Organize projects/foldersorganize_solutionCanonical folders, DAG check, link cleanup in 1 call
Repair common errorsauto_fix_diagnosticsAuto-fixes WCAG contrast, missing labels & broken links
Show interactive UI in chatrender_genui_cardLive HTML preview, clickable Solution map, Proof Matrix, WCAG Swatches, Interactive Forms

Studio Chat Shortcuts#

  • Type / in the Agent composer for instant commands: /full, /plan, /viewmodel, /view, /proof, /organize, /fix, /guidelines, /preview, /verify, /prove, /models, /clear.
  • Type @ in the Agent composer to reference any module or project in the active Solution directly.
  • Every GenUI card in chat is interactive — click module chips to open tabs, preview live HTML inline, trigger Lean 4 proofs, or submit structured agent forms without leaving the dock.
Diagram Preview
Enlarged diagram