Context Compression
Algorithms to surgically slice AST chunks to fit strict LLM token budgets.
Implementation Sourcesrc/core/compiler/compiler.ts
The Context Window Problem
If a user queries for the validate() method, standard vector DBs return the entire file. If that file is 3,000 lines long, it consumes 30k+ tokens instantly.
ContextOS compiles results at the AST node level, enforcing a hard maxTokenBudget (default: 40k).
Token Budgeting Algorithm
ContextOS iterates over the ranked results and executes a greedy knapsack-style packing algorithm.
- Group by File: We group top chunks by their source file. We do not inject chunks out of file order.
- Calculate Framing Overhead: For each file, we calculate the tokens required for XML framing:
<file path="...">...</file>. - Exact Token Measurement: We use the
gpt-tokenizerpackage to physically count tokens on the fly.
Fallback Mechanisms
If the budget is exceeded mid-file, ContextOS triggers graceful degradation:
if (currentTokenCount + chunkTokens > budget) {
// Option 1: Try stripping comments
const stripped = stripComments(chunk.body);
if (count(stripped) <= budget) {
return add(stripped);
}
// Option 2: Try truncating the body entirely, leaving just the signature
const signature = extractSignature(chunk.body);
if (count(signature) <= budget) {
return add(signature + '\n // ... (body truncated)');
}
// Option 3: Drop the chunk entirely to preserve budget integrity
return drop();
}Complexity Analysis
Time Complexity
O(C)
Space Complexity
O(C)
Average Case
~15ms (bound by tokenizer)
Worst Case
~50ms