Add size limits to prevent context overflow in large repos#19
Merged
JordanCoin merged 4 commits intomainfrom Feb 19, 2026
Merged
Add size limits to prevent context overflow in large repos#19JordanCoin merged 4 commits intomainfrom
JordanCoin merged 4 commits intomainfrom
Conversation
- Session-start hook now uses adaptive depth based on repo size: - >5000 files: depth 2 - >2000 files: depth 3 - Otherwise: depth 4 - Both hook and MCP get_structure enforce 60KB max output (~15k tokens) - Truncates cleanly at line boundaries with helpful message - Prevents consuming >10% of LLM context window Fixes issue where 10k+ file repos (like Rails monoliths) would output 1.3MB+ of tree structure, overwhelming Claude Code's context. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Hook output goes directly into Claude's "Messages" context, not system prompt. This means hook output competes with conversation history for the ~200k token limit. A 1.3MB output (like a full tree of a 10k file repo) equals ~500k tokens, causing instant context overflow. The size limits (adaptive depth + 60KB cap) are critical safeguards. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR keeps codemap hooks/MCP useful while making them safe on large repos.
Existing fix (kept)
>5000 files -> depth 2>2000 files -> depth 3depth 4get_structureenforce60KBmax output (~15k tokens, <10% of context)Additional hardening in this PR
limits/) so hook + MCP use one source of truth for:60KB)>5000 files) to avoid startup CPU/memory spikes.codemap/state.json(even when dep graph is unavailable), so hooks still getfile_count+ session event contextReadStatenow accepts stale state if daemon PID is still alive (avoids unnecessary expensive rescans after idle periods)codemap --diffget_structurenow:depthProblem
Large repos (10k+ files) could produce massive startup output and expensive repeated analysis:
Why this matters
Hooks need to be context-aware and resource-aware:
Test results
go test ./...passesBehavioral note
On very large repos, hub/dependency analysis is intentionally deferred in hook/session-start flows unless explicitly requested via dedicated tooling (
get_hubs, etc.).