Matrioska v2 is an advanced orchestration system for large language models (LLMs) that implements a modular architecture based on files with shared state. Inspired by the concept of Russian nesting dolls, the system decomposes complex tasks into specialized files that communicate via a shared whiteboard (shared_state).
- ๐ File-Based Architecture: Automatic decomposition of projects into ordered files.
- ๐ง Shared State: Communication system between files via
shared_state. - ๐พ Full Persistence: Checkpoints of architecture and state between executions.
- โก Sequential Generation: Each file is generated in dependency order.
- ๐ Selective Context: Files access only relevant information from predecessors.
- ๐ฆ Optimized Code: Focus on minimal, complete, and efficient code using CDNs.
LocalLLM- Wrapper for Mistral models with 4-bit quantization.MatrioskaOrchestrator- Main pipeline orchestrator.ContextManager- Manages shared state and persistence.Architecture- Data structure for file-based planning.FileSpec- Individual file specification.FileArtifact- Generated file artifact.
pip install -q json-repair transformers accelerate bitsandbytes torch sentencepiece protobuf!rm -rf /content/log
!rm -rf /content/matrioska_artifacts
!rm -rf /content/matrioska_checkpointsfrom matrioska_v2 import LocalLLM, MatrioskaOrchestrator
# Initialize model
llm = LocalLLM("mistralai/Mistral-7B-Instruct-v0.3")
orchestrator = MatrioskaOrchestrator(llm, base_path="/content")
# Execute task
result = orchestrator.run("Create a library management system with authentication and dashboard")/content/
โโโ log/ # Prompt and response logs
โ โโโ log.txt # Complete generation history
โโโ matrioska_artifacts/ # Generated files
โ โโโ index.html
โ โโโ styles.css
โ โโโ app.js
โโโ matrioska_checkpoints/ # State and architecture
โโโ shared_state.json # Shared whiteboard
โโโ architecture.json # Architectural plan
@dataclass
class FileSpec:
name: str # Name without extension
extension: str # File extension
order: int # Creation order (1, 2, 3...)
shared_state_writes: List[str] # Info this file defines
shared_state_reads: List[str] # Info this file needs
content: str # Code generation prompt
details: str # Functional requirements{
"instructs": {
"files": [
{
"name": "index",
"extension": "html",
"order": 1,
"shared_state_writes": ["element_ids", "page_structure"],
"shared_state_reads": [],
"content": "Generate complete HTML structure for library system...",
"details": "Responsive layout, login form, book catalog, dashboard"
},
{
"name": "styles",
"extension": "css",
"order": 2,
"shared_state_writes": ["css_classes", "color_scheme"],
"shared_state_reads": ["element_ids", "page_structure"],
"content": "Generate complete CSS using Tailwind CDN...",
"details": "Modern design, dark mode, mobile-first"
},
{
"name": "app",
"extension": "js",
"order": 3,
"shared_state_writes": ["api_endpoints", "storage_keys"],
"shared_state_reads": ["element_ids", "css_classes"],
"content": "Generate JavaScript with authentication logic...",
"details": "JWT auth, localStorage, CRUD operations"
}
]
}
}# File 1 (HTML) generates IDs
SHARED_STATE_UPDATE:
{
"element_ids": ["#loginForm", "#bookList", "#dashboardStats"],
"page_structure": {
"login": "section#login",
"catalog": "section#catalog",
"dashboard": "section#dashboard"
}
}
# File 2 (CSS) automatically consumes IDs
# The ContextManager provides only the keys specified in shared_state_reads
quant_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4"
)max_new_tokens: 20,000 (configurable via_MAX_TOKEN_)temperature: 0.3top_p: 0.85do_sample: Truepad_token_id: Auto (eos_token_id)
The system uses ARCHITECT_SYSTEM_PROMPT which instructs the LLM to:
- Decompose the task into independent files
- Define creation order based on dependencies
- Specify contracts via
shared_state_reads/writes - Generate complete prompts for each file
- Focus on minimal code and use of CDNs/libraries
- Strict JSON structure with
instructsroot orderfield defining creation sequenceshared_state_writes: information the file definesshared_state_reads: information the file needscontent: complete code generation promptdetails: functional and non-functional requirements
result = orchestrator.run('''
Create a complete e-commerce system with:
- Product catalog with search
- Shopping cart functionality
- User authentication
- Admin dashboard
- Responsive design with Tailwind CDN
''')result = orchestrator.run('''
Build a task management app using React CDN with:
- Component-based architecture
- State management
- CRUD operations
- LocalStorage persistence
''')result = orchestrator.run('''
Create an analytics dashboard with:
- Chart.js for visualizations
- Real-time data updates
- Export to CSV functionality
- Responsive grid layout
''')================================================================================
๐ช MATRIOSKA ORCHESTRATOR - File-Based Architecture
================================================================================
๐๏ธ PHASE 1: ARCHITECTURE
--------------------------------------------------------------------------------
๐ Task: 'Create a library management system with authentication and dashboard'
โ Project: Project_3_Files
โ Files: 3
1. index.html ๐[] โ๏ธ['element_ids', 'page_structure']
2. styles.css ๐['element_ids', 'page_structure'] โ๏ธ['css_classes', 'color_scheme']
3. app.js ๐['element_ids', 'css_classes'] โ๏ธ['api_endpoints', 'storage_keys']
โก PHASE 2: CODE GENERATION
--------------------------------------------------------------------------------
๐ฏ Generating: index.html (Order: 1)
๐พ index.html โ /content/matrioska_artifacts/index.html
๐ง [SHARED STATE] Updated: ['element_ids', 'page_structure']
โ๏ธ Wrote: ['element_ids', 'page_structure']
โ Generated (2847 chars)
๐ฏ Generating: styles.css (Order: 2)
๐ Reading context: ['element_ids', 'page_structure']
๐พ styles.css โ /content/matrioska_artifacts/styles.css
๐ง [SHARED STATE] Updated: ['css_classes', 'color_scheme']
โ๏ธ Wrote: ['css_classes', 'color_scheme']
โ Generated (1923 chars)
๐ฏ Generating: app.js (Order: 3)
๐ Reading context: ['element_ids', 'css_classes']
๐พ app.js โ /content/matrioska_artifacts/app.js
๐ง [SHARED STATE] Updated: ['api_endpoints', 'storage_keys']
โ๏ธ Wrote: ['api_endpoints', 'storage_keys']
โ Generated (3456 chars)
โ
FINAL RESULT
================================================================================
๐ฆ Project_3_Files
๐ Generated Files: 3
1. index.html
2. styles.css
3. app.js
๐ง SharedState Keys: ['element_ids', 'page_structure', 'css_classes', 'color_scheme', 'api_endpoints', 'storage_keys']
================================================================================
๐ Artifacts: /content/matrioska_artifacts
๐ง SharedState: /content/matrioska_checkpoints/shared_state.json
- Persistent: Saved in
shared_state.jsonbetween executions. - Structured: JSON-serializable dictionary.
- Selective: Files access only keys specified in
shared_state_reads. - Incremental: Updated during the generation of each file.
- Architecture:
architecture.json- Complete project plan - SharedState:
shared_state.json- Current shared state - Artifacts: Individual files in
matrioska_artifacts/ - Logs: Complete history of prompts and responses in
log/log.txt
{
"element_ids": ["#loginForm", "#bookList", "#dashboard"],
"page_structure": {
"login": "section#login",
"catalog": "section#catalog"
},
"css_classes": ["btn-primary", "card", "nav-item"],
"color_scheme": {
"primary": "#3b82f6",
"secondary": "#8b5cf6"
},
"api_endpoints": {
"login": "/api/auth/login",
"books": "/api/books"
},
"storage_keys": ["authToken", "currentUser"]
}The system automatically detects updates in the format:
// At the end of the generated code
SHARED_STATE_UPDATE:
{
"key1": "value1",
"key2": ["item1", "item2"]
}
This marker is:
- Extracted and processed by the
ContextManager - Removed from the final code
- Persisted in
shared_state.json
result = orchestrator.run("Create app...")
# Returns a dictionary with:
{
"architecture": Architecture, # Object with the project plan
"artifacts": List[FileArtifact], # List of generated files
"shared_state": Dict[str, Any] # Final shared state
}- GPU: NVIDIA T4 (8GB VRAM) or superior
- RAM: 12GB+ recommended
- Python: 3.8+
- Libraries:
transformers(Hugging Face)torch(PyTorch)bitsandbytes(Quantization)accelerate(Optimization)json-repair(Robust Parsing)sentencepiece,protobuf(Tokenization)
All prompts and responses are saved in /content/log/log.txt:
PROMPT:
==========================================
[Complete prompt sent to LLM]
==========================================
RESULT:
==========================================
[LLM Response]
- File Order: HTML/DB first โ CSS/Styles โ JS/Logic โ API/Backend
- SharedState: Define clear contracts between files (IDs, classes, routes)
- Detailed Prompts: The
contentfield must be a complete generation prompt - CDNs: Prioritize libraries via CDN to reduce complexity
- Minimal Code: Focus on minimal and functional code
| Aspect | v1 (Modules) | v2 (Files) |
|---|---|---|
| Basic Unit | ModuleSpec |
FileSpec |
| Final Integration | Artifact assembly | Independent files |
| Structure | 3 phases | 2 phases |
| Focus | Conceptual modularity | Practical code generation |
| Output | Integrated result | Separate files |
This project is intended for research and educational development purposes.
Matrioska v2: Transforming ideas into structured code ๐ชโจ
