Goal: Make codescan a sharper, leaner alternative to Serena for LLM-driven code comprehension and editing. Zero config, zero ceremony, single static binary. The tool an LLM wants to reach for.
| Dimension | Serena | Codescan (planned) |
|---|---|---|
| Setup | pip install + project.yml + activate + onboard | codescan symbols src/main.zig — just works |
| Startup | Seconds to eagerly start LSP servers | Instant for tree-sitter ops; lazy LSP only when needed |
| Binary | Python + venv + 30+ pip dependencies | Single static Zig binary |
| Language detection | Config file required | Auto-detect from extensions + shebangs (already done) |
| Semantic search | Not included | Already has embedding + FTS5 hybrid search |
| Tool count | ~30 MCP tools, most noise | ~10 CLI commands, all useful |
| Interface | MCP server (requires MCP client) | CLI — LLM already has shell access |
| Overhead per invocation | MCP JSON-RPC round-trip | Direct subprocess, streaming stdout |
The 6 tools that matter:
find_symbol— Find symbols by hierarchical name path (Class/method), optionally include body/infoget_symbols_overview— Quick overview of top-level symbols grouped by kindfind_referencing_symbols— Find all references to a symbol across the projectreplace_symbol_body— Replace a symbol's entire body by name path (precise, no near-misses)insert_after_symbol/insert_before_symbol— Insert code adjacent to a named symbolrename_symbol— Rename across entire codebase
Everything else in Serena is either redundant with shell access or unnecessary ceremony:
create_text_file,read_file,list_dir,find_file— shell does this- Memory tools — LLM writes its own markdown
- Dashboard, JetBrains, config modes — not relevant
think_about_*, onboarding, activation — prompting tricks that belong in the LLM, not the toolreplace_content— that's just regex find-replace;sed/editor tools already existexecute_shell_command— the LLM already has Bash
Codescan already has tree-sitter parsers for 12+ languages. Each extractor walks the AST and extracts:
- Function/method names and their source text
- Class/struct/enum definitions
- Module declarations and doc comments
- Source locations (line numbers, byte offsets)
This is currently used for embedding into SQLite for semantic search. The same infrastructure serves targeted code editing with minimal new work.
Inspired by The Harness Problem.
LLMs fail at expressing edits not because they don't understand the task, but because edit formats are fragile:
- String replacement needs exact character matching → "string not found" errors
- Diffs need valid patch syntax → 46-50% failure rates on some models
- Line numbers alone shift on any insertion/deletion
Annotate each line with a short content-chain hash:
44:k7m|fn init(self: *Self) void {
45:r2p| self.count = 0;
46:a9x| self.buffer = undefined;
47:3bw| self.ready = false;
48:npq|}
- 3 characters, base-36 (0-9 a-z) — 46,656 possible values
- Hash chain: each line's hash incorporates the previous line's hash
hash[0] = base36(short_hash(line[0])) hash[i] = base36(short_hash(hash[i-1] || line[i])) - Per-symbol chains: chain resets at each symbol boundary (tree-sitter gives us these)
3 chars base-36 (not 2-char hex):
| Encoding | 2 chars | 3 chars |
|---|---|---|
| Hex (base 16) | 256 values | 4,096 values |
| Base-36 (0-9 a-z) | 1,296 values | 46,656 values |
| Base-62 (mixed case) | 3,844 values | 238,328 values |
- False-negative rate (edit goes undetected): 1/46,656 = 0.002%
- Birthday collision 50% threshold: ~216 lines (most functions are <100 lines)
- No case ambiguity (mixed case is risky — LLMs normalize case in short tokens)
- Visually distinct from line numbers (digits-only) at a glance
- LLMs see alphanumeric short-hashes constantly in training data
Hash chains (not content-only hashes):
Content-only hash: inserting a line at line 5 doesn't invalidate the hash at line 50. Hash chain: any edit above cascades through all subsequent hashes.
If the file changed above your edit point, your mental model is stale. Better to force a re-read than silently apply a wrong edit.
Per-symbol chains (not whole-file chains):
A comment typo fix at line 1 shouldn't invalidate hashes in an unrelated function at line 500. Per-symbol chains reset at each tree-sitter symbol boundary, so:
- Edits within a function invalidate the rest of that function's hashes (correct)
- Edits in one function don't affect another function's hashes (pragmatic)
Every codescan command that outputs code uses hashlines:
$ codescan find-symbol MyStruct/init --include-body
MyStruct/init (src/lib.zig:44-48) [Function]
44:k7m|fn init(self: *Self) void {
45:r2p| self.count = 0;
46:a9x| self.buffer = undefined;
47:3bw| self.ready = false;
48:npq|}
Editing commands accept hashline references:
$ echo 'self.count = 0;
self.buffer = null;
self.ready = true;' | codescan replace-lines --file src/lib.zig --from 45:r2p --to 47:3bw
$ echo 'self.initialized = true;' | codescan insert-at 47:3bw --file src/lib.zig
Hash mismatch = immediate error, not silent corruption:
$ codescan replace-lines --file src/lib.zig --from 45:r2p --to 47:3bw
error: hashline mismatch at line 45 (expected r2p, got x4f) — file changed since last read
codescan symbols <file>
List all symbols in a file grouped by kind. Compact overview for orientation. Output: JSON or human-formatted table of symbol names, kinds, line ranges.
codescan find-symbol <name_path> [--file <path>] [--include-body] [--depth N]
Find symbols matching a hierarchical name path pattern:
- Simple:
init— matches any symbol named "init" - Relative:
MyStruct/init— matches suffix of name path - Absolute:
/MyStruct/init— exact match within file tree Body output includes hashlines.
codescan replace-symbol <name_path> --file <path> --body <stdin>
Replace a symbol's entire body. Tree-sitter provides exact byte range. Safer than string replacement — symbol name path is unambiguous.
codescan insert-after <name_path> --file <path> --body <stdin>
codescan insert-before <name_path> --file <path> --body <stdin>
Insert code adjacent to a named symbol. Handles newline normalization (functions get blank line separators, fields don't — inferred from AST node type).
codescan replace-lines --file <path> --from <line:hash> --to <line:hash> --body <stdin>
codescan insert-at <line:hash> --file <path> --body <stdin>
Hashline-anchored line-level edits within a file. Hash mismatch = error.
codescan references <name_path> --file <path>
Find all references to a symbol across the project. Auto-detect language, lazy-start appropriate LSP server (zls, rust-analyzer, etc.).
codescan rename <name_path> --file <path> --to <new_name>
Rename symbol across entire codebase via LSP textDocument/rename.
Auto-detect language, lazy LSP startup.
Watch for file changes (kqueue/FSEvents on macOS) and re-index in background. Or: make indexing fast enough that on-demand re-indexing of changed files is negligible.
codescan CLI
|
+-- search (EXISTING) ---------- embedding pipeline + FTS5 hybrid
+-- index/update (EXISTING) ---- scan -> extract -> embed -> store
|
+-- symbols (NEW, Phase 1) ----- tree-sitter AST query, zero-config
+-- find-symbol (NEW, Phase 1) - name path matching over AST
|
+-- replace-symbol (NEW, Phase 2) -- tree-sitter location -> byte-level splice
+-- insert-after (NEW, Phase 2) ---- tree-sitter anchor -> byte-level insert
+-- insert-before (NEW, Phase 2) --- tree-sitter anchor -> byte-level insert
+-- replace-lines (NEW, Phase 2) --- hashline-anchored range replacement
+-- insert-at (NEW, Phase 2) ------- hashline-anchored insertion
|
+-- references (NEW, Phase 3) ------ LSP lazy-start -> textDocument/references
+-- rename (NEW, Phase 3) ---------- LSP lazy-start -> textDocument/rename
|
+-- (Phase 4) ---------------------- background file watcher + incremental reindex
Tree-sitter gives parent-child AST nesting. A function node inside a struct node
gives StructName/function_name. Codescan's extractors already walk these trees
for embedding — just need to record and expose the hierarchy.
Name path matching follows Serena's proven scheme:
method— match any symbol with that name (suffix match)Class/method— match any name path ending with that suffix/Class/method— exact match of full name path within file
Tree-sitter nodes have start_byte/end_byte and start_point/end_point (line, col).
This is all we need for replace-symbol and insert-after/before:
- Read file into memory
- Find symbol's byte range from tree-sitter
- Splice:
file[0..start] ++ new_body ++ file[end..] - Write back
All editing commands accept body from stdin. This lets LLMs pipe multi-line code without shell escaping issues:
echo 'fn new_impl() void {
// ...
}' | codescan replace-symbol MyStruct/init --file src/lib.zig
Already implemented. src/plugin.zig has a registry mapping extensions to languages.
src/scan.zig detects file types including shebang scripts. No configuration needed.
When codescan references is invoked:
- Detect language from file extension (plugin registry)
- Check if appropriate LS binary is on PATH (zls, rust-analyzer, etc.)
- Start it, send initialize + didOpen
- Make the request, return result
- Optionally keep running for subsequent calls (with timeout)
- Memory system — LLM writes its own markdown files
- Dashboard / web UI — CLI is the interface
- Project activation / onboarding — auto-detect everything
- "Thinking" tools — the LLM handles its own reasoning
- File I/O tools — shell already does this
- Configuration modes — one mode: "it works"
- MCP server — unnecessary indirection; CLI via shell is simpler
- Hash function for hashlines: xxhash? CRC32 truncated? FNV-1a? (needs to be fast, good avalanche)
- Should
codescan symbolsoutput hashlines too, or just line ranges? - For Phase 3 LSP: manage a persistent daemon, or start/stop per invocation?
- Should editing commands do a tree-sitter re-parse after edit to verify the result is valid syntax?
- How to handle languages without tree-sitter grammars (fallback to line-based editing only?)