How SiftrCode Works
A deep engineering overview of SiftrCode V2: multi-channel candidate recall, point-in-time versioned features, explainable ContextRank, submodular bundle composition, safe variable-resolution degradation, and constrained knapsack budget solving.
How SiftrCode Triages Repository Context
SiftrCode intercepts agent queries locally, evaluating repository candidates in <100ms before model inference to deliver outcome-optimized context with variable resolution.
Candidate Bloat
Lexical and graph discovery recalls 42 repository candidate files. Without Siftr, flooding all 42 full files into context causes latency and attention loss.
6-Stage Optimization
Runs point-in-time features, transparent ranking, submodular bundle composition, and knapsack solving to partition candidates into 3 optimal resolution tiers.
Outcome-Aware Bundle
Agent receives full implementation bodies for edit targets, typed AST interface skeletons for dependencies, and filtered distractor tokens.
Inspect Resolution Channels
Click a channel to inspect how SiftrCode assigns resolution, preserves types, and suppresses noise.
The 6-Stage Optimization Engine
SiftrCode V2 replaces naive single-file token stripping with a formal multi-stage context pipeline designed to maximize first-turn task completion rates while cutting context costs.
Multi-Channel Candidate Recall
Coding agents frequently fail when initial search misses non-obvious callers or pulls in hundreds of irrelevant results. SiftrCode V2 runs Multi-Channel Candidate Recall across 5 orthogonal channels in parallel:
Identifies exact symbol names, functions, classes, and file paths referenced in the prompt or error report.
Probabilistic keyword ranking over exported docstrings, parameter names, and error string tokens.
Directly parses stack traces, runtime exceptions, and test failure outputs to pinpoint exact file and line offsets.
Traverses AST import/export caller graphs and mines git history for files that frequently co-modify together.
Point-in-Time Versioned Features
Context optimization must be scientifically reproducible and immune to evaluation leakage. SiftrCode models every candidate file as an immutable versioned feature vector (ContextFeaturesV1) with a strict featureCutoff timestamp.
No post-task commits, downstream edits, or runtime test outputs can leak into feature calculation. Features capture lexical affinity, graph centrality, error proximity, line count, and edit recency with deterministic provenance.
Deterministic ContextRank & 7-Gate Model Governance
Black-box neural rankers introduce non-deterministic ordering and unpredictability into production workflows. SiftrCode V2 maintains deterministic ContextRank as the sole production decision-maker, calculating an explicit finalScore alongside auditable reasons and component breakdowns:
Score(c) = w_exact * s_exact + w_bm25 * s_bm25 + w_stack * s_stack + w_graph * s_graph - Penalties
To evaluate candidate ranking signals safely, Siftr forks candidate signals to a shadow JudgmentProvider in the background. Shadow evaluation analyzes continuous relevance, edit target probability, and root cause localization without altering production ContextPlans (bit-for-bit plan identity in shadow mode). Candidate evaluations run with strict decision budgeting, fail-closed probability validation, and zero hidden retries.
Before any learned model can be promoted to production, it must pass 7 strict promotion gates, including offline holdout NDCG@10 across 40 tasks on 4 independent repositories (Commander, Express, FastAPI, SiftrCode). In canonical holdout evaluations, candidate model gbdt_pairwise_v1 achieved 0.5034 NDCG@10 vs frozen V2 baseline's 0.5200 (delta -0.0166; 95% CI [-0.0478, +0.0076]). Because the candidate failed to beat the baseline, SiftrCode's fail-closed governance rejected promotion, preserving the deterministic production baseline.
Submodular Synergy Composition
Selecting context files independently leads to severe redundancy: multiple files defining identical helpers, or omitting the single type contract required to understand a causal function.
SiftrCode's BundleComposer applies submodular function optimization: each candidate added to the bundle is evaluated by its marginal utility gain relative to already-chosen files. Co-requisites (e.g. an interface file paired with its primary caller) receive a synergistic boost, while redundant utility mocks are quickly suppressed.
3-Stage Resolution Lineage & Discrete Method Slicing
The fatal flaw in pure skeletonization is stripping the code the agent actually needs to rewrite. SiftrCode V2 eliminates this with 3-stage resolution lineage and discrete method body slicing:
Every ContextUnit tracks this immutable progression through solver budget allocation and post-render degradation. When an agent invokes siftr_expand to BODY, the Discrete AST Slicer isolates precisely the target function/method body while strictly excluding sibling methods, preventing context pollution.
Direct edit targets. 0 lines stripped.
Exact target method. Sibling methods pruned.
Exported types, classes, interfaces.
Transitive dependencies & contracts.
Non-causal distractors eliminated.
Constrained Budget & Truthful Accounting
Context windows have hard operational ceilings. SiftrCode's BudgetSolver solves a constrained knapsack problem over candidate representations, maximizing cumulative utility per token without violating prompt budget limits.
Integrated with TokenizerRegistry, Siftr enforces pre-flight bounds calibrated against Claude BPE (~3.7 chars/tok) and GPT cl100k/o200k (~3.6 chars/tok), preventing context overflow and ensuring billing integrity.
Prompt Caching & Prefix Invariance Architecture
Frontier LLMs (Anthropic Claude 3.7, OpenAI GPT-4o, Google Gemini 2.0) offer prompt caching discounts of up to 90% on repeated input prefixes. However, standard coding agents constantly edit files, invoke tools, and inspect outputs. Each code modification invalidates prompt caches, causing costly cold turns where all input tokens are re-billed at full write rates. SiftrCode eliminates this penalty through a two-tier context hierarchy:
Invariant AST Interface Skeletons
Exported types, method signatures, and structural interfaces are ordered deterministically by canonical file path at the head of the prompt. Because structural contracts rarely change during edit loops, this prefix stays bit-for-bit invariant across dozens of agent turns, sustaining continuous 90% cache hits.
Target Bodies & Tool Returns
Full implementation bodies for files the agent is actively editing are placed strictly at the tail of the context bundle. When edits happen, only the small dynamic tail invalidates, preventing cache eviction of the large structural codebase skeleton.
Token Economics by Budget Profile
Modeled on 85k-token raw repository payload at 50 turns/day (1,400 turns/mo per dev).
| Profile | Optimized Payload | Token Reduction | Sonnet 3.7 / Turn | Monthly Savings | 10-Dev Team / Yr |
|---|---|---|---|---|---|
| Balanced (Default) | 18,200 tok | −78.6% | $0.055 | Save $280 / mo | +$33.7k / yr |
| Aggressive | 10,500 tok | −87.6% | $0.032 | Save $313 / mo | +$37.5k / yr |
| Thorough | 32,000 tok | −62.4% | $0.096 | Save $223 / mo | +$26.7k / yr |
SiftrBench: Holdout Benchmark & Paired Core-Thesis Verification
Before any model-based ranking signal can influence production ContextPlans, it must survive rigorous scientific evaluation. SiftrBench validates optimization across 40 holdout tasks on 4 independent repositories (Commander, Express, FastAPI, SiftrCode) and executes a Paired Core-Thesis Experiment Harness (Phase 21A) measuring live agent task outcomes:
10 tasks each on Commander, Express, FastAPI, SiftrCode. Continuous offline NDCG@10 validation.
Side-by-side agent runs tracking success rate delta, token reduction, and CPVST efficiency.
Requires statistically significant task win margin or demonstrable CPVST cost reduction.
Bit-for-bit plan identity in shadow mode (0% fallback rate, zero orphan signals, sub-10ms CPU).
Zero-Egress & Data Rights Technical Guarantees
SiftrCode V2 is governed by authoritative granular operation rights, fail-closed remote processing, and closed-loop data flywheel integrity:
Zero machine learning training on customer code. RightsFilter export gate strictly blocks raw source tables.
operationRights.*.retention.local governs every durable SQLite write; 0 raw source strings stored.
Strictly forbidden by default; requires explicit createJevPermittedDataRights policy.
Unexposed code emits label: null with tri-state verifiedSuccess; never forced to negative 0.
Direct arbitrary persistence is rejected; only genuine exports bearing a cryptographically sanctioned WeakSet token can persist.
Dataset V2 training pool validates canonical eligibility; unpermitted, UNKNOWN provenance, or orphan rows fail closed.
Snapshot immutability guards trigger structured replanning if files mutate mid-task on disk.
Automated regex redaction for AWS keys, Stripe secrets, and Bearer tokens prior to external dispatch.
Syntax-verified AST parsers for TypeScript, JavaScript, Python, Go, and Rust with line-span accuracy.
AST slicing isolates target method bodies strictly, excluding all adjacent sibling class methods.
Hard token gate: dynamic degradation (FULL → BODY → SKELETON → SIGNATURE → NAME → OMIT).
TokenizerRegistry calibrated against Claude BPE (~3.7 chars/tok) and GPT cl100k/o200k (~3.6 chars/tok).
Deterministic ContextRank baseline defaults in production; candidate models require 7 passed gates to promote.
Sub-100ms CPU execution with 0 outbound network bytes via CLI and standard Model Context Protocol (MCP) stdio.
Model Context Protocol (MCP) & CLI Tools
SiftrCode V2 implements the official Model Context Protocol (MCP) specification. Register it once and your coding agent automatically invokes siftr_context before writing code:
claude mcp add siftrcode -- npx -y siftrcode mcp
npx siftrcode context "fix webhook invoice race condition" --profile balanced -o context.xml
Full 6-stage outcome optimization for task prompt within token and cost limits.
Alias for siftr_context for outcome-aware context bundle optimization.
Progressive disclosure tool expanding skeletons into full implementation bodies.
Reports downstream verification signals (tests, builds, regressions) to close learning loop.
Inspects multi-turn session telemetry, trajectory events, and budget burn rate.
Diagnostic heuristic ranking with transparent scores and primary reasons.
AST interface extractor for TypeScript, Python, Go, and Rust source files.
Concurrent AST interface extraction across multiple target files.
Audits codebase token footprint and calculates potential team ROI savings.
Generates whole-directory pruned context packs for task-focused areas.
Ready to test it on your codebase?
Run a 5-second audit or register SiftrCode directly with your agent.
npx siftrcode audit