Lattice - Large File Analysis
Use the Lattice MCP tools to analyze files that are too large to read directly. Lattice stores query results server-side and returns compact handle stubs, achieving 97%+ token savings.
When to Use
- File is larger than 500 lines (for smaller files, use Read directly)
- You need multiple searches on the same file
- You're extracting or aggregating structured data (counts, sums, patterns)
- You're doing exploratory analysis and don't know what you're looking for
- You want to avoid hallucination on factual queries about file contents
Core Workflow
The standard workflow is: load → query → expand → close
lattice_load — Open the file (starts a session)
lattice_query — Run Nucleus S-expression commands (returns handle stubs like $res1)
lattice_expand — Inspect actual data from a handle when you need to see contents
lattice_close — End the session when done
Efficiency Tips
- Chain operations in sequence rather than making many independent queries. Use
RESULTS to refer to the previous result and build a pipeline in a single query session.
- Start broad, then narrow:
grep first, then filter, then count/sum.
- Only expand when needed: Handle stubs give you counts and previews — expand only when you need to see actual data for decision-making.
- Prefer
(count RESULTS) or (sum RESULTS) over expanding and counting client-side.
Nucleus Command Reference
Search
(grep "pattern") ; Regex search — returns handle to matching lines
(fuzzy_search "query" 10) ; Fuzzy match — top N results by relevance
(lines 10 20) ; Get specific line range (start end)
Transform
(filter RESULTS (lambda x (match x "pattern" 0))) ; Keep matching items
(map RESULTS (lambda x (match x "(\\d+)" 1))) ; Extract regex group from each item
Aggregate
(count RESULTS) ; Count items (returns scalar directly)
(sum RESULTS) ; Sum numeric values (auto-extracts numbers)
Extract
(match str "pattern" 1) ; Extract regex capture group from a string
Code & Document Symbols (for .ts, .js, .py, .go, .rs, .md, etc.)
(list_symbols) ; List all functions, classes, methods, headings, etc.
(list_symbols "function") ; Filter by kind: "function", "class", "method", "interface", "type"
(get_symbol_body "funcName") ; Get full source code of a symbol
(find_references "identifier"); Find all usages of an identifier
Variable Bindings
RESULTS — Always points to the last array result. Use this in queries to chain operations.
_1, _2, _3, ... — Results from turn N. Use to reference older results in queries.
$res1, $res2, ... — Handle stubs. Use these ONLY with lattice_expand, NOT in queries.
Complete Workflow Example
Task: Find and count timeout errors in a log file
Load the document:
lattice_load("/path/to/server.log")
→ Loaded: 15,234 lines, 2.1 MB
Search for errors:
lattice_query('(grep "ERROR")')
→ $res1: Array(342) [2024-01-15 ERROR: Connection timeout...]
Filter for timeouts:
lattice_query('(filter RESULTS (lambda x (match x "timeout" 0)))')
→ $res2: Array(47) [2024-01-15 ERROR: Connection timeout...]
Count them:
lattice_query('(count RESULTS)')
→ Result: 47
Inspect a sample if needed:
lattice_expand("$res2", limit=5)
→ Shows first 5 actual timeout error lines
Close when done:
lattice_close()
Code Analysis Workflow
Task: Understand a large TypeScript file
Load and list symbols:
lattice_load("/path/to/large-module.ts")
lattice_query('(list_symbols)')
→ $res1: Array(45) [function handleRequest, class Router, ...]
Get a specific function:
lattice_query('(get_symbol_body "handleRequest")')
→ Returns full source code of the function
Find all references:
lattice_query('(find_references "handleRequest")')
→ $res2: Array(8) [line 45: handleRequest(req), ...]
1---2name: lattice3description: Analyze large files (>500 lines) using handle-based Nucleus queries for 97% token savings. Use when you need to search, filter, aggregate, or explore documents too large for direct context.4---56# Lattice - Large File Analysis78Use the Lattice MCP tools to analyze files that are too large to read directly. Lattice stores query results server-side and returns compact handle stubs, achieving 97%+ token savings.910## When to Use1112- File is **larger than 500 lines** (for smaller files, use Read directly)13- You need **multiple searches** on the same file14- You're **extracting or aggregating** structured data (counts, sums, patterns)15- You're doing **exploratory analysis** and don't know what you're looking for16- You want to **avoid hallucination** on factual queries about file contents1718## Core Workflow1920The standard workflow is: **load → query → expand → close**21221. `lattice_load` — Open the file (starts a session)232. `lattice_query` — Run Nucleus S-expression commands (returns handle stubs like `$res1`)243. `lattice_expand` — Inspect actual data from a handle when you need to see contents254. `lattice_close` — End the session when done2627### Efficiency Tips2829- **Chain operations in sequence** rather than making many independent queries. Use `RESULTS` to refer to the previous result and build a pipeline in a single query session.30- **Start broad, then narrow**: `grep` first, then `filter`, then `count`/`sum`.31- **Only expand when needed**: Handle stubs give you counts and previews — expand only when you need to see actual data for decision-making.32- Prefer `(count RESULTS)` or `(sum RESULTS)` over expanding and counting client-side.3334## Nucleus Command Reference3536### Search37```scheme38(grep "pattern") ; Regex search — returns handle to matching lines39(fuzzy_search "query" 10) ; Fuzzy match — top N results by relevance40(lines 10 20) ; Get specific line range (start end)41```4243### Transform44```scheme45(filter RESULTS (lambda x (match x "pattern" 0))) ; Keep matching items46(map RESULTS (lambda x (match x "(\\d+)" 1))) ; Extract regex group from each item47```4849### Aggregate50```scheme51(count RESULTS) ; Count items (returns scalar directly)52(sum RESULTS) ; Sum numeric values (auto-extracts numbers)53```5455### Extract56```scheme57(match str "pattern" 1) ; Extract regex capture group from a string58```5960### Code & Document Symbols (for .ts, .js, .py, .go, .rs, .md, etc.)61```scheme62(list_symbols) ; List all functions, classes, methods, headings, etc.63(list_symbols "function") ; Filter by kind: "function", "class", "method", "interface", "type"64(get_symbol_body "funcName") ; Get full source code of a symbol65(find_references "identifier"); Find all usages of an identifier66```6768## Variable Bindings6970- **`RESULTS`** — Always points to the last array result. Use this in queries to chain operations.71- **`_1`, `_2`, `_3`, ...** — Results from turn N. Use to reference older results in queries.72- **`$res1`, `$res2`, ...** — Handle stubs. Use these ONLY with `lattice_expand`, NOT in queries.7374## Complete Workflow Example7576**Task: Find and count timeout errors in a log file**77781. Load the document:79 ```80 lattice_load("/path/to/server.log")81 ```82 → `Loaded: 15,234 lines, 2.1 MB`83842. Search for errors:85 ```86 lattice_query('(grep "ERROR")')87 ```88 → `$res1: Array(342) [2024-01-15 ERROR: Connection timeout...]`89903. Filter for timeouts:91 ```92 lattice_query('(filter RESULTS (lambda x (match x "timeout" 0)))')93 ```94 → `$res2: Array(47) [2024-01-15 ERROR: Connection timeout...]`95964. Count them:97 ```98 lattice_query('(count RESULTS)')99 ```100 → `Result: 47`1011025. Inspect a sample if needed:103 ```104 lattice_expand("$res2", limit=5)105 ```106 → Shows first 5 actual timeout error lines1071086. Close when done:109 ```110 lattice_close()111 ```112113## Code Analysis Workflow114115**Task: Understand a large TypeScript file**1161171. Load and list symbols:118 ```119 lattice_load("/path/to/large-module.ts")120 lattice_query('(list_symbols)')121 ```122 → `$res1: Array(45) [function handleRequest, class Router, ...]`1231242. Get a specific function:125 ```126 lattice_query('(get_symbol_body "handleRequest")')127 ```128 → Returns full source code of the function1291303. Find all references:131 ```132 lattice_query('(find_references "handleRequest")')133 ```134 → `$res2: Array(8) [line 45: handleRequest(req), ...]`