How it works
ContextCraft converts a local repository into a ranked, budgeted artifact through separate analysis and generation stages. Keeping an analysis alive lets library callers try several budgets or focus queries without repeating the scan.
Discover and load
The scanner collects deterministic repository-relative paths, applies ignore policies, and loads eligible source under a per-file limit. Binary, invalid UTF-8, and oversized files retain path entries with omission reasons.
Directory traversal errors fail the scan. The CLI also fails on individual read errors, while lower-level scanner callers can inspect omissions for partial results.
Parse syntax
Tree-sitter grammars and language-specific capture queries identify declarations, references, documentation, and outline ranges. Captures drive extraction rather than a separate Zig-side walk through every native node.
Language handlers also identify embedded syntax, such as scripts and styles in HTML, Vue, and Svelte, translating captured spans back to the original source. Unsupported syntax still has readable FULL source when loading succeeds.
Discover projects and resolve references
Project handlers read literal manifests and workspace metadata to identify local packages, targets, source roots, and dependency visibility. Resolution combines that context with language-specific import rules to build directed local file edges.
Build scripts are not executed. Conditional or dynamic configuration can leave references unresolved, and syntax support is independent of project-aware resolution coverage. See languages.
Protect the generation view
High-level generation builds a protected view before ranking and rendering. Secret matches become replacements, sensitive files contribute only paths, and outline ranges are remapped to the protected source. Raw analysis remains unchanged for callers that need it.
Rank relevance
BM25F combines field-weighted lexical evidence, while personalized PageRank propagates relevance over the local dependency graph. A task query and favored paths influence ranking without forcing particular files or tiers into the result.
Render and allocate
Files offer TREE, SIGS, SKEL, and FULL where supported. The allocator weighs score-weighted utility against the incremental token cost of upgrades, then serializes and checks the entire output with the selected embedded tokenizer.
Detail is reduced until the complete artifact fits, including framing, escaping, and paths. This is a greedy allocation strategy, with exact final accounting rather than a global optimality guarantee.
Detail utilities
Ranking supplies a score per file, and the allocator weighs additional detail against its token cost. Cheap estimates guide candidates, with exact costs counted during refinement and the complete serialized artifact checked at the end.
The native implementation uses utilities of 0, 0.45, 0.82, and 1 for TREE, SIGS, SKEL, and FULL. This is a greedy selection process rather than a promise of a globally optimal combination.
Resource ownership
The reusable engine owns native parsers, compiled queries, the tokenizer, and scratch resources. Analyses own retained source and graph data independently of the engine, and artifacts own their output independently of an analysis.
Operation and ranking scratch are reused with bounded retained capacity, while returned results stay outside that scratch lifetime. In Python, use context managers to close engines and analyses, and keep the returned artifact values as ordinary Python-owned data.