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:
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
entryModuleIdto your primary screen so App Flow reachability checks and Prototype HTML export start cleanly. - Typed Cross-Module Links: Connect modules with
WorkflowLinks (state_syncfrom ViewModel → View,navigatebetween Screens,eventfor cross-module commands,dependencyfor shared Design/Proof contracts). - Auto-Organization: Run the
organize_solutionMCP tool (or/organizein 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) andcollectionnodes. Derive all KPIs, booleans, and validation messages viacomputed,predicate, andvalidator. - Total & Bounded Actions: Keep every
actiontransition total and within bounds (for exampleMath.min(maxLoad, cpuLoad + 10)andMath.max(0, cpuLoad - 10)) so Lean 4 invariant preservation theorems (step_preserves_inv) prove cleanly without manual surgery. - Explicit FSMs: Use
state_machinenodes for lifecycle workflows (Draft → Review → Published → Archived) with guard expressions and terminal states, validated viastep_state_machine. - Fast Authoring: Prefer
build_viewmodelover sequentialadd_nodecalls 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, orscroll). Wireparent.children → child.parentwithout 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 toaction.trigger. - Accessible by Construction: Give every interactive control (
button,input,slider,toggle,select,modal) a meaningfullabel/ariaLabel, and wire verified WCAG AA (>= 4.5:1) or AAA (>= 7.0:1)styletokens plus multi-localeresourcebundles. - Fast Authoring: Prefer
build_view_uito 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
subprogramnodes (manage_subprogram_or_group) or promote them into dedicated modules linked withmodule_ref+WorkflowLink. - Visual Grouping: Frame logical regions (
State & Invariants,Actions,Design Tokens,View Tree) usinggroupnodes. - Clean Routing: Use
reroutewaypoints or wirelessportal_out/portal_inpairs for long cross-canvas signals, then runapply_layout(mode="grouped")(or"layered"for linear pipelines).
5. Formal Lean 4 Proofs#
- Baseline Safety: Pair every bounded state variable with an
invariantnode and verify withverify_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, andlean_tacticnodes in a dedicatedProofs/project. - Zero
sorry: Use decidable, automated tactics (omega,simp,decide,trivial) and validate withbuild_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):
| Task | Fast Macro MCP Tool | Why It's Faster |
|---|---|---|
| Check guidelines | get_architecture_guidelines | Instant structured rules + anti-patterns by topic |
| Check exact ports | describe_node_kinds | Zero port-name guessing before wiring |
| Find anything across Solution | search_solution | Instant cross-project search for modules, nodes, diagnostics |
| Build a ViewModel | build_viewmodel | States, computeds, actions, FSMs & invariants in 1 call |
| Build a View screen | build_view_ui | Full nested View tree + ViewModel bindings + layout in 1 call |
| Author Lean 4 proofs | build_proof_suite | Types, defs, theorems, proof rules & Lean check in 1 call |
| Organize projects/folders | organize_solution | Canonical folders, DAG check, link cleanup in 1 call |
| Repair common errors | auto_fix_diagnostics | Auto-fixes WCAG contrast, missing labels & broken links |
| Show interactive UI in chat | render_genui_card | Live 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.