|
| 1 | +# Agentic Architecture & Context Management |
| 2 | + |
| 3 | +## What We Fixed |
| 4 | + |
| 5 | +### 1. ✅ Connection Naming (word_word → Title Case) |
| 6 | +**Problem**: LLM was outputting `"in_relationship"`, `"close_friends_with"`, `"mother_son"` (snake_case) |
| 7 | +**Fix**: Added explicit CONNECTION NAMING RULES to prompt: |
| 8 | +``` |
| 9 | +✅ Good: "Romantic Partnership", "Inner Circle Bond", "Coaching Relationship" |
| 10 | +❌ Bad: "romantic_partnership", "inner_circle_bond", "coaching_relationship" |
| 11 | +``` |
| 12 | + |
| 13 | +### 2. ✅ Active Graph Context |
| 14 | +**Problem**: User said "add more" in "Swift-Kelce Network", but Wizard added to "NC State University" graph |
| 15 | +**Fix**: Added CRITICAL instruction to `create_node` intent: |
| 16 | +``` |
| 17 | +CRITICAL: You MUST set "graph": {"name": "{EXACT active graph name from CURRENT GRAPH context}"} - DO NOT invent a different graph name! |
| 18 | +``` |
| 19 | + |
| 20 | +### 3. ✅ Delete Operations |
| 21 | +**Verified**: Delete is working correctly. Log shows: `[Executor] delete_node_instance: Deleting instance inst-1763261052499-5-xarla9 from graph graph-1763261052233-uc9dif` |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## Your Bigger Question: Abstraction & Token Management |
| 26 | + |
| 27 | +> "we need the executor to build up agentic flows essentially right? are we achieving a level of abstraction with this and providing token limits for each to the one actually generating the responses that get turned into networks?" |
| 28 | +
|
| 29 | +### Yes! Here's What We're Achieving: |
| 30 | + |
| 31 | +## 1. **Separation of Concerns (Like Cursor's Agent System)** |
| 32 | + |
| 33 | +| Layer | Role | Token Budget | LLM Involvement | |
| 34 | +|-------|------|--------------|-----------------| |
| 35 | +| **Planner** | Decides WHAT to do | 2000 tokens | ✅ LLM call | |
| 36 | +| **Executor** | Generates operations | 0 tokens | ❌ Deterministic | |
| 37 | +| **Auditor** | Validates ops | 0 tokens | ❌ Rule-based | |
| 38 | +| **Committer** | Applies to UI | 0 tokens | ❌ Deterministic | |
| 39 | +| **Continuation** | Decides NEXT step | 1500 tokens | ✅ LLM call (agentic loop) | |
| 40 | + |
| 41 | +**Key Insight**: Only 2 LLM calls per iteration (Planner + Continuation), rest is deterministic graph operations. |
| 42 | + |
| 43 | +## 2. **Abstraction Levels** |
| 44 | + |
| 45 | +### Level 1: User Intent (Natural Language) |
| 46 | +``` |
| 47 | +User: "add more to the active graph" |
| 48 | +``` |
| 49 | + |
| 50 | +### Level 2: Semantic Plan (LLM Output) |
| 51 | +```json |
| 52 | +{ |
| 53 | + "intent": "create_node", |
| 54 | + "graph": {"name": "Swift-Kelce Network"}, |
| 55 | + "graphSpec": { |
| 56 | + "nodes": [{"name": "Jason Kelce", ...}], |
| 57 | + "edges": [{"source": "Jason Kelce", "target": "Travis Kelce", "type": "siblings"}] |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +### Level 3: Operations (Executor Output) |
| 63 | +```js |
| 64 | +[ |
| 65 | + { type: 'addNodePrototype', prototypeData: {...} }, |
| 66 | + { type: 'addNodeInstance', graphId: '...', prototypeId: '...', position: {x,y} }, |
| 67 | + { type: 'addEdge', edgeData: {...} } |
| 68 | +] |
| 69 | +``` |
| 70 | + |
| 71 | +### Level 4: UI Mutations (Committer Output) |
| 72 | +```js |
| 73 | +graphStore.applyMutations([...ops]) |
| 74 | +// Updates React state, triggers re-render |
| 75 | +``` |
| 76 | + |
| 77 | +**This is true abstraction**: Each layer only knows about its immediate inputs/outputs, not the full pipeline. |
| 78 | + |
| 79 | +## 3. **Token Budget Per Role** |
| 80 | + |
| 81 | +### Current Configuration: |
| 82 | +```js |
| 83 | +// PLANNER (initial request) |
| 84 | +const PLANNER_MAX_TOKENS = 2000; // Enough for ~10 nodes + edges + connection defs |
| 85 | + |
| 86 | +// CONTINUATION (agentic loop) |
| 87 | +const CONTINUE_MAX_TOKENS = 1500; // Smaller batches for iterative building |
| 88 | + |
| 89 | +// EXECUTOR (no LLM) |
| 90 | +// Just runs algorithms: fuzzy matching, layout, operation generation |
| 91 | + |
| 92 | +// AUDITOR (no LLM) |
| 93 | +// Schema validation, reference checks |
| 94 | + |
| 95 | +// COMMITTER (no LLM) |
| 96 | +// Applies operations, triggers UI updates |
| 97 | +``` |
| 98 | + |
| 99 | +### Why This Works: |
| 100 | +- **Planner** gets the full context (conversation, graph state, colors) → 2000 tokens |
| 101 | +- **Continuation** gets simplified context (just node names, counts) → 1500 tokens |
| 102 | +- **Executor** runs deterministic code (no token cost) |
| 103 | +- Total: ~3500 tokens output per iteration (vs 10,000+ for monolithic approaches) |
| 104 | + |
| 105 | +## 4. **Agentic Flows We're Building** |
| 106 | + |
| 107 | +### Flow 1: Create Graph (Single Iteration) |
| 108 | +``` |
| 109 | +User: "make a graph of X" |
| 110 | + ↓ |
| 111 | +Planner: { intent: "create_graph", graphSpec: {...} } |
| 112 | + ↓ |
| 113 | +Executor: Generate ops (nodes, edges, layout) |
| 114 | + ↓ |
| 115 | +Auditor: Validate |
| 116 | + ↓ |
| 117 | +Committer: Apply + Check if agenticLoop=true |
| 118 | + ↓ |
| 119 | +Continuation: LLM decides "continue" or "complete" |
| 120 | + ↓ (if continue) |
| 121 | +Planner: { intent: "create_node", graphSpec: {...} } |
| 122 | + ↓ |
| 123 | +[Loop repeats up to 5x] |
| 124 | +``` |
| 125 | + |
| 126 | +### Flow 2: Add to Graph (Context-Aware) |
| 127 | +``` |
| 128 | +User: "add more" [in active graph] |
| 129 | + ↓ |
| 130 | +Planner: Gets active graph context |
| 131 | + | 🎯 CURRENT GRAPH: "Swift-Kelce Network" |
| 132 | + | Status: 10 nodes, 12 edges |
| 133 | + | Example concepts: Taylor Swift, Travis Kelce, Selena Gomez... |
| 134 | + ↓ |
| 135 | +LLM: MUST use "graph": {"name": "Swift-Kelce Network"} |
| 136 | + ↓ |
| 137 | +Executor: Fuzzy dedup + link to existing nodes |
| 138 | + ↓ |
| 139 | +Committer: Apply + trigger continuation |
| 140 | +``` |
| 141 | + |
| 142 | +### Flow 3: Delete (CRUD Operation) |
| 143 | +``` |
| 144 | +User: "take Blake Lively out" |
| 145 | + ↓ |
| 146 | +Planner: { intent: "delete_node", delete: { target: "Blake Lively" } } |
| 147 | + ↓ |
| 148 | +Executor: Find instance by name → generate deleteNodeInstance op |
| 149 | + ↓ |
| 150 | +Auditor: Validate instance exists |
| 151 | + ↓ |
| 152 | +Committer: Apply deletion |
| 153 | + ↓ |
| 154 | +No continuation (delete is final) |
| 155 | +``` |
| 156 | + |
| 157 | +## 5. **Context Management (Like Cursor's @-mentions)** |
| 158 | + |
| 159 | +### What We Have: |
| 160 | +```js |
| 161 | +// Active graph context (injected into every prompt) |
| 162 | +🎯 CURRENT GRAPH: "Swift-Kelce Network" |
| 163 | +Status: 10 nodes, 12 edges |
| 164 | +Example concepts: Taylor Swift, Travis Kelce, Selena Gomez... |
| 165 | + |
| 166 | +// Conversation history (last 10 messages) |
| 167 | +📝 RECENT CONVERSATION: |
| 168 | +User: make a new graph for Taylor Swift |
| 169 | +You: I'll weave a fresh "Swift-Kelce Network"... |
| 170 | +User: add more |
| 171 | +You: I'll expand with 4 more associates... |
| 172 | + |
| 173 | +// Color palette (extracted from existing nodes) |
| 174 | +🎨 AVAILABLE COLORS: #8b0045, #00458b, #8b0000, ... |
| 175 | +``` |
| 176 | + |
| 177 | +### What We Could Add (Cursor-style): |
| 178 | +- **@graphs**: Explicit graph references (`@Swift-Kelce-Network`) |
| 179 | +- **@nodes**: Reference specific nodes (`@Taylor-Swift`) |
| 180 | +- **@definitions**: Reference connection types (`@Romantic-Partnership`) |
| 181 | +- **Implicit context switching**: "open the Solar System graph" → switches active graph |
| 182 | + |
| 183 | +### Implementation Path: |
| 184 | +1. **Parse @-mentions** in user message |
| 185 | +2. **Inject as explicit context** in prompt |
| 186 | +3. **Track context stack** (active graph, selected nodes, etc.) |
| 187 | +4. **Auto-detect context switches** (e.g., "in the X graph" changes active graph) |
| 188 | + |
| 189 | +## 6. **Token Efficiency: Before vs After** |
| 190 | + |
| 191 | +### Before (Monolithic) |
| 192 | +``` |
| 193 | +User: "make a Taylor Swift graph with associates" |
| 194 | + ↓ |
| 195 | +1 LLM call: Generate 20 nodes at once |
| 196 | + ↓ |
| 197 | +Token usage: 5000+ tokens |
| 198 | + ↓ |
| 199 | +Result: Truncated at 1200 tokens, incomplete JSON |
| 200 | +``` |
| 201 | + |
| 202 | +### After (Agentic Batching) |
| 203 | +``` |
| 204 | +User: "make a Taylor Swift graph with associates" |
| 205 | + ↓ |
| 206 | +Iteration 0: 6 nodes (2000 tokens) ✅ |
| 207 | + ↓ |
| 208 | +Continuation: "I need to add their associates" (800 tokens) ✅ |
| 209 | + ↓ |
| 210 | +Iteration 1: 4 nodes (1500 tokens) ✅ |
| 211 | + ↓ |
| 212 | +Continuation: "Graph sufficiently populated" (200 tokens) ✅ |
| 213 | + ↓ |
| 214 | +Total: 4500 tokens across 4 calls |
| 215 | +Result: Complete graph, no truncation |
| 216 | +``` |
| 217 | + |
| 218 | +**Efficiency Gain**: 4500 tokens (usable) vs 5000 tokens (truncated) |
| 219 | + |
| 220 | +## 7. **Are We Achieving True Abstraction?** |
| 221 | + |
| 222 | +### ✅ Yes, in these ways: |
| 223 | + |
| 224 | +1. **Semantic → Spatial Separation** |
| 225 | + - LLM outputs: node names, relationships, colors |
| 226 | + - Executor adds: x/y coordinates via layout algorithms |
| 227 | + - LLM never sees spatial data (solves original bottleneck!) |
| 228 | + |
| 229 | +2. **Plan → Execute Separation** |
| 230 | + - Planner outputs: high-level intent + graphSpec |
| 231 | + - Executor outputs: low-level operations |
| 232 | + - No mixing of concerns |
| 233 | + |
| 234 | +3. **Context Encapsulation** |
| 235 | + - Each role gets ONLY what it needs |
| 236 | + - Planner: full context (2000 tokens) |
| 237 | + - Continuation: minimal context (1500 tokens) |
| 238 | + - Executor: no context (deterministic) |
| 239 | + |
| 240 | +4. **Fuzzy Deduplication** |
| 241 | + - Pre-execution audit (before nodes are created) |
| 242 | + - Executor-level logic (no LLM needed) |
| 243 | + - Prevents 90%+ duplicates |
| 244 | + |
| 245 | +### ⚠️ Not Yet (But Could Be): |
| 246 | + |
| 247 | +1. **Explicit Context Management** |
| 248 | + - Need @-mention parsing |
| 249 | + - Need context stack tracking |
| 250 | + - Need auto-detection of context switches |
| 251 | + |
| 252 | +2. **Multi-Graph Awareness** |
| 253 | + - Currently only tracks "active graph" |
| 254 | + - Could track "referenced graphs" (all graphs mentioned in conversation) |
| 255 | + - Could support cross-graph operations ("link node X in graph A to node Y in graph B") |
| 256 | + |
| 257 | +3. **Adaptive Token Budgets** |
| 258 | + - Currently fixed (2000 for planner, 1500 for continuation) |
| 259 | + - Could adapt based on graph size (smaller graphs → fewer tokens) |
| 260 | + - Could adapt based on iteration (later iterations → smaller budgets) |
| 261 | + |
| 262 | +4. **Parallel Execution** |
| 263 | + - Currently sequential (one iteration at a time) |
| 264 | + - Could parallelize independent operations (e.g., "add nodes to graph A AND graph B") |
| 265 | + |
| 266 | +## 8. **Next Steps for True "Cursor-like" Context** |
| 267 | + |
| 268 | +### Phase 1: Explicit Context Mentions (Highest Priority) |
| 269 | +```js |
| 270 | +// Parse @-mentions |
| 271 | +const mentions = parseContextMentions(userMessage); |
| 272 | +// { graphs: ['Swift-Kelce Network'], nodes: ['Taylor Swift'], connections: [] } |
| 273 | + |
| 274 | +// Inject as explicit context |
| 275 | +const contextBlock = ` |
| 276 | +📍 REFERENCED CONTEXT: |
| 277 | +- Graph: "${mentions.graphs[0]}" (10 nodes, 12 edges) |
| 278 | +- Nodes: "Taylor Swift" (pop artist), "Travis Kelce" (NFL player) |
| 279 | +`; |
| 280 | +``` |
| 281 | + |
| 282 | +### Phase 2: Context Stack Management |
| 283 | +```js |
| 284 | +// Track active context |
| 285 | +const contextStack = { |
| 286 | + activeGraph: 'Swift-Kelce Network', |
| 287 | + selectedNodes: ['Taylor Swift', 'Travis Kelce'], |
| 288 | + recentGraphs: ['Swift-Kelce Network', 'Solar System', 'NC State'], |
| 289 | + conversationHistory: [...] |
| 290 | +}; |
| 291 | + |
| 292 | +// Auto-detect context switches |
| 293 | +if (message.includes('in the Solar System graph')) { |
| 294 | + contextStack.activeGraph = 'Solar System'; |
| 295 | +} |
| 296 | +``` |
| 297 | + |
| 298 | +### Phase 3: Multi-Graph Operations |
| 299 | +```js |
| 300 | +// Cross-graph linking |
| 301 | +{ |
| 302 | + "intent": "create_edge", |
| 303 | + "edge": { |
| 304 | + "sourceGraph": "Marvel Universe", |
| 305 | + "sourceNode": "Tony Stark", |
| 306 | + "targetGraph": "DC Universe", |
| 307 | + "targetNode": "Bruce Wayne", |
| 308 | + "type": "inspired_by" |
| 309 | + } |
| 310 | +} |
| 311 | +``` |
| 312 | + |
| 313 | +### Phase 4: Adaptive Batching |
| 314 | +```js |
| 315 | +// Adjust token budget based on graph complexity |
| 316 | +const PLANNER_MAX_TOKENS = graphSize < 10 ? 1500 : 2000; |
| 317 | + |
| 318 | +// Reduce continuation budget in later iterations |
| 319 | +const CONTINUE_MAX_TOKENS = 1500 - (iteration * 200); // 1500 → 1300 → 1100 ... |
| 320 | +``` |
| 321 | + |
| 322 | +## Summary |
| 323 | + |
| 324 | +**What we have**: |
| 325 | +- ✅ True separation of concerns (semantic vs spatial) |
| 326 | +- ✅ Token-efficient agentic batching |
| 327 | +- ✅ Deterministic executor layer (no wasted LLM calls) |
| 328 | +- ✅ Active graph context awareness |
| 329 | +- ✅ Pre-execution fuzzy deduplication |
| 330 | + |
| 331 | +**What we need**: |
| 332 | +- ⚠️ Explicit @-mention parsing (Cursor-style) |
| 333 | +- ⚠️ Context stack management (track multiple graphs) |
| 334 | +- ⚠️ Auto-detection of context switches |
| 335 | +- ⚠️ Multi-graph operations |
| 336 | + |
| 337 | +**Bottom line**: We're achieving significant abstraction and token efficiency, but there's room to make it even more "Cursor-like" with explicit context management. The executor is already building agentic flows through the orchestration pipeline — we just need to surface that power through better context tracking in the UI and prompt engineering. |
| 338 | + |
0 commit comments