1+ ---
2+ description: Hierarchical rule loading system for optimized token usage
3+ globs: "**/rule-loading*/**", "**/optimization*/**"
4+ alwaysApply: false
5+ ---
6+
7+ # HIERARCHICAL RULE LOADING SYSTEM
8+
9+ > **TL;DR:** This rule implements an optimized loading system that only loads necessary rules based on context, complexity level, and current phase to maximize token efficiency.
10+
11+ ## 🧠 HIERARCHICAL RULE STRUCTURE
12+
13+ ```mermaid
14+ graph TD
15+ Root["Root Rules"] --> Core["Core Rules<br>(Always Loaded)"]
16+ Root --> Common["Common Rules<br>(Mode Independent)"]
17+ Root --> Mode["Mode-Specific<br>Rules"]
18+ Root --> Level["Complexity Level<br>Rules"]
19+
20+ Core --> Platform["Platform<br>Detection"]
21+ Core --> File["File<br>Operations"]
22+ Core --> Transition["Mode<br>Transitions"]
23+
24+ Mode --> VAN["VAN Mode<br>Rules"]
25+ Mode --> PLAN["PLAN Mode<br>Rules"]
26+ Mode --> CREATIVE["CREATIVE Mode<br>Rules"]
27+ Mode --> IMPLEMENT["IMPLEMENT Mode<br>Rules"]
28+ Mode --> REFLECT["REFLECT Mode<br>Rules"]
29+
30+ Level --> Level1["Level 1<br>Rules"]
31+ Level --> Level2["Level 2<br>Rules"]
32+ Level --> Level3["Level 3<br>Rules"]
33+ Level --> Level4["Level 4<br>Rules"]
34+
35+ style Root fill:#4da6ff,stroke:#0066cc,color:white
36+ style Core fill:#ffa64d,stroke:#cc7a30,color:white
37+ style Common fill:#4dbb5f,stroke:#36873f,color:white
38+ style Mode fill:#d94dbb,stroke:#a3378a,color:white
39+ style Level fill:#4dbbbb,stroke:#368787,color:white
40+ ```
41+
42+ ## 📊 RULE LOADING PROTOCOL
43+
44+ ```mermaid
45+ sequenceDiagram
46+ participant User
47+ participant LoadManager
48+ participant RuleCache
49+ participant FileSystem
50+
51+ User->>LoadManager: Request mode activation
52+ LoadManager->>RuleCache: Check cached core rules
53+ RuleCache-->>LoadManager: Return cached rules if available
54+
55+ LoadManager->>FileSystem: Load essential mode rules
56+ FileSystem-->>LoadManager: Return essential rules
57+
58+ LoadManager->>LoadManager: Register lazy loaders for specialized rules
59+ LoadManager->>User: Return initialized mode
60+
61+ User->>LoadManager: Request specialized functionality
62+ LoadManager->>RuleCache: Check specialized rule cache
63+ RuleCache-->>LoadManager: Return cached rule if available
64+
65+ alt Rule not in cache
66+ LoadManager->>FileSystem: Load specialized rule
67+ FileSystem-->>LoadManager: Return specialized rule
68+ LoadManager->>RuleCache: Cache specialized rule
69+ end
70+
71+ LoadManager->>User: Execute specialized functionality
72+ ```
73+
74+ ## 🔄 RULE LOADING IMPLEMENTATION
75+
76+ ```javascript
77+ // Pseudocode for hierarchical rule loading
78+ class RuleLoadManager {
79+ constructor() {
80+ this.cache = {
81+ core: {},
82+ common: {},
83+ mode: {},
84+ level: {}
85+ };
86+ this.lazyLoaders = {};
87+ }
88+
89+ // Initialize a mode with only essential rules
90+ initializeMode(modeName, complexityLevel) {
91+ // Always load core rules
92+ this.loadCoreRules();
93+
94+ // Load common rules
95+ this.loadCommonRules();
96+
97+ // Load essential mode-specific rules
98+ this.loadEssentialModeRules(modeName);
99+
100+ // Load complexity level rules
101+ this.loadComplexityRules(complexityLevel);
102+
103+ // Register lazy loaders for specialized functionality
104+ this.registerLazyLoaders(modeName, complexityLevel);
105+
106+ return {
107+ modeName,
108+ complexityLevel,
109+ status: "initialized"
110+ };
111+ }
112+
113+ // Load only when specialized functionality is needed
114+ loadSpecializedRule(ruleType) {
115+ if (this.lazyLoaders[ruleType]) {
116+ if (!this.cache.specialized[ruleType]) {
117+ const rule = this.lazyLoaders[ruleType]();
118+ this.cache.specialized[ruleType] = rule;
119+ }
120+ return this.cache.specialized[ruleType];
121+ }
122+ return null;
123+ }
124+
125+ // Register specialized rule loaders based on mode and complexity
126+ registerLazyLoaders(modeName, complexityLevel) {
127+ // Clear existing lazy loaders
128+ this.lazyLoaders = {};
129+
130+ // Register mode-specific lazy loaders
131+ if (modeName === "CREATIVE") {
132+ this.lazyLoaders["architecture"] = () => this.loadRule("creative-phase-architecture.mdc");
133+ this.lazyLoaders["algorithm"] = () => this.loadRule("creative-phase-algorithm.mdc");
134+ this.lazyLoaders["uiux"] = () => this.loadRule("creative-phase-uiux.mdc");
135+ } else if (modeName === "IMPLEMENT") {
136+ this.lazyLoaders["testing"] = () => this.loadRule("implementation-testing.mdc");
137+ this.lazyLoaders["deployment"] = () => this.loadRule("implementation-deployment.mdc");
138+ }
139+
140+ // Register complexity-specific lazy loaders
141+ if (complexityLevel >= 3) {
142+ this.lazyLoaders["comprehensive-planning"] = () => this.loadRule("planning-comprehensive.mdc");
143+ this.lazyLoaders["advanced-verification"] = () => this.loadRule("verification-advanced.mdc");
144+ }
145+ }
146+ }
147+ ```
148+
149+ ## 📋 RULE DEPENDENCY MAP
150+
151+ ```mermaid
152+ graph TD
153+ Main["main.mdc"] --> Core1["platform-awareness.mdc"]
154+ Main --> Core2["file-verification.mdc"]
155+ Main --> Core3["command-execution.mdc"]
156+
157+ subgraph "VAN Mode"
158+ VanMap["van-mode-map.mdc"] --> Van1["van-complexity-determination.mdc"]
159+ VanMap --> Van2["van-file-verification.mdc"]
160+ VanMap --> Van3["van-platform-detection.mdc"]
161+ end
162+
163+ subgraph "PLAN Mode"
164+ PlanMap["plan-mode-map.mdc"] --> Plan1["task-tracking-basic.mdc"]
165+ PlanMap --> Plan2["planning-comprehensive.mdc"]
166+ end
167+
168+ subgraph "CREATIVE Mode"
169+ CreativeMap["creative-mode-map.mdc"] --> Creative1["creative-phase-enforcement.mdc"]
170+ CreativeMap --> Creative2["creative-phase-metrics.mdc"]
171+ Creative1 & Creative2 -.-> CreativeSpecialized["Specialized Creative Rules"]
172+ CreativeSpecialized --> CArch["creative-phase-architecture.mdc"]
173+ CreativeSpecialized --> CAlgo["creative-phase-algorithm.mdc"]
174+ CreativeSpecialized --> CUIUX["creative-phase-uiux.mdc"]
175+ end
176+
177+ subgraph "IMPLEMENT Mode"
178+ ImplementMap["implement-mode-map.mdc"] --> Impl1["implementation-guide.mdc"]
179+ ImplementMap --> Impl2["testing-strategy.mdc"]
180+ end
181+ ```
182+
183+ ## 🔍 MODE-SPECIFIC RULE LOADING
184+
185+ ### VAN Mode Essential Rules
186+ ```markdown
187+ - main.mdc (Core)
188+ - platform-awareness.mdc (Core)
189+ - file-verification.mdc (Core)
190+ - van-mode-map.mdc (Mode)
191+ ```
192+
193+ ### PLAN Mode Essential Rules
194+ ```markdown
195+ - main.mdc (Core)
196+ - plan-mode-map.mdc (Mode)
197+ - task-tracking-[complexity].mdc (Level)
198+ ```
199+
200+ ### CREATIVE Mode Essential Rules
201+ ```markdown
202+ - main.mdc (Core)
203+ - creative-mode-map.mdc (Mode)
204+ - creative-phase-enforcement.mdc (Mode)
205+ ```
206+
207+ ### CREATIVE Mode Specialized Rules (Lazy Loaded)
208+ ```markdown
209+ - creative-phase-architecture.mdc (Specialized)
210+ - creative-phase-algorithm.mdc (Specialized)
211+ - creative-phase-uiux.mdc (Specialized)
212+ ```
213+
214+ ### IMPLEMENT Mode Essential Rules
215+ ```markdown
216+ - main.mdc (Core)
217+ - command-execution.mdc (Core)
218+ - implement-mode-map.mdc (Mode)
219+ ```
220+
221+ ## 🚀 IMPLEMENTATION BENEFITS
222+
223+ The hierarchical loading system provides:
224+
225+ 1. **Reduced Initial Loading**: Only essential rules loaded at start (~70% token reduction)
226+ 2. **Cached Core Rules**: Rules shared between modes are cached
227+ 3. **Specialized Rule Loading**: Specialized rules loaded only when needed
228+ 4. **Complexity-Based Loading**: Only load rules appropriate for task complexity
229+
230+ ## 📈 TOKEN USAGE COMPARISON
231+
232+ | Approach | Initial Tokens | Specialized Tokens | Total Tokens |
233+ |----------|---------------|-------------------|--------------|
234+ | Original System | ~70,000 | Included in initial | ~70,000 |
235+ | Hierarchical System | ~15,000 | ~10,000 (on demand) | ~25,000 |
236+ | **Token Reduction** | **~55,000 (78%)** | **N/A** | **~45,000 (64%)** |
237+
238+ ## 🔄 USAGE EXAMPLE
239+
240+ ### Example: Creative Phase with Architecture Rule
241+
242+ ```javascript
243+ // Initialize the CREATIVE mode with only essential rules
244+ const mode = ruleManager.initializeMode("CREATIVE", 3);
245+
246+ // Core and essential mode rules are loaded
247+ // Architecture rules are NOT loaded yet
248+
249+ // Later, when architecture design is needed:
250+ const architectureRule = ruleManager.loadSpecializedRule("architecture");
251+
252+ // Now the architecture rule is loaded and cached
253+ ```
254+
255+ ## 🧪 RULE LOADING VERIFICATION
256+
257+ To ensure the rule loading system is working optimally:
258+
259+ ```markdown
260+ ## Rule Loading Verification
261+
262+ - Core Rules: [Loaded]
263+ - Mode-Essential Rules: [Loaded]
264+ - Complexity-Level Rules: [Loaded]
265+ - Specialized Rules: [Not Loaded]
266+
267+ Current Token Usage: [X] tokens
268+ Potential Token Savings: [Y] tokens
269+ ```
270+
271+ This hierarchical approach ensures optimal token usage while maintaining all functionality.
0 commit comments