feat(ADR-059): Context Optimization Engine — @claude-flow/context#1274
Open
shaal wants to merge 3 commits intoruvnet:mainfrom
Open
feat(ADR-059): Context Optimization Engine — @claude-flow/context#1274shaal wants to merge 3 commits intoruvnet:mainfrom
shaal wants to merge 3 commits intoruvnet:mainfrom
Conversation
… bounded context Add design documents for native context window compression engine inspired by claude-context-mode. Achieves 95-98% context reduction via sandbox-isolated execution, FTS5+HNSW dual-index knowledge base, and swarm-aware per-agent context budgets. Documents: - SPARC PRD with 4-phase delivery plan - ADR-059: master architecture decision - ADR-059a: FTS5 knowledge base with three-layer fuzzy search - ADR-059b: sandbox isolation and credential passthrough - ADR-059c: swarm-aware context budgets and progressive throttling - DDD bounded context: domain model, integration points, context map
…lementation New package implementing the Context Optimization Engine with DDD architecture: Domain layer: - Value objects: CompressionRatio, ContextBudget, SearchQuery, SnippetWindow - Entities: KnowledgeChunk (FTS5-indexed), SandboxInstance (lifecycle FSM) - Aggregates: CompressionSession (metrics), KnowledgeBase (dedup + eviction) - Domain events: OutputCompressed, ContentIndexed, BudgetExceeded, ChunksEvicted - Repository interfaces: IFTS5Repository, ICompressionSessionRepository, IContextBudgetRepository Infrastructure layer: - FTS5Repository: sql.js-backed search with BM25 scoring and Porter stemming - ChunkingEngine: heading-aware splitting with code block preservation - LevenshteinCorrector: edit distance with early-exit optimization Sandbox layer: - SandboxPool: process-isolated execution via child_process.spawn() - RuntimeDetector: auto-detection for 11 language runtimes - CredentialPassthrough: fail-closed env allowlisting Application layer: - CompressionPipelineService: 3-tier pipeline (passthrough/snippet/full) - FuzzySearchService: 3-layer cascade (stemming→trigram→Levenshtein) - UnifiedSearchService: RRF fusion combining keyword + HNSW semantic search - MetricsCollector: per-tool and per-session compression stats - CLI commands: context stats/doctor/search Hooks: - PreToolUseHook: budget-aware gate with progressive throttling - PostToolUseHook: automatic compression and KB indexing - SubagentRoutingHook: batch instruction injection for subagents Budget management: - ContextBudgetManager: topology-aware allocation, progressive throttling - SharedKnowledgeTracker: cross-agent content deduplication - SwarmBudgetIntegration: event-driven swarm lifecycle bridge 301 tests across 17 test files, all passing.
… hooks Wire the context optimization engine into three integration points: - CLI: `context` command with 5 subcommands (stats, search, doctor, budget, compress) delegating to MCP tools per ADR-005 - MCP: 5 tools (context_stats, context_search, context_doctor, context_budget, context_compress) with lazy singleton service bundle - Hooks: context-bridge adapts PreToolUseHook (budget enforcement), PostToolUseHook (output compression), and SubagentRoutingHook (batch hints) to HookRegistry All integration points use optional dependency pattern with try/catch dynamic imports — system works without @claude-flow/context installed.
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
Implements the Context Optimization Engine as a new bounded context package (
@claude-flow/context) and integrates it into the live Ruflo system through CLI commands, MCP tools, and hook registration.Closes #1273
What's Included
New Package:
@claude-flow/context(57 files, 7,770 lines)Domain Layer (DDD)
CompressionRatio,ContextBudget,SearchQuery,SnippetWindowKnowledgeChunk(FTS5-indexed),SandboxInstance(lifecycle FSM)CompressionSession(metrics tracking),KnowledgeBase(dedup + TTL eviction)OutputCompressed,ContentIndexed,BudgetExceeded,ChunksEvictedInfrastructure Layer
FTS5Repository: sql.js WASM-backed search with BM25 scoring and Porter stemmingChunkingEngine: heading-aware content splitting with code block preservation (2048-token chunks, 128-token overlap)LevenshteinCorrector: edit distance calculator with early-exit optimization for fuzzy Layer 3Sandbox Layer
SandboxPool: process-isolated execution viachild_process.spawn()with warm pool (3 default), max 8 concurrent, 30s timeout, 512MB memory limitRuntimeDetector: auto-detection for 11 language runtimes via shebang and syntax heuristicsCredentialPassthrough: fail-closed env allowlist (GitHub, AWS, K8s, Docker tokens)Application Layer
CompressionPipelineService: 3-tier pipeline — passthrough (<1KB), snippet (1-5KB), full pipeline (>5KB with intent filtering)FuzzySearchService: 3-layer cascade — FTS5 stemming → trigram substring → Levenshtein correctionUnifiedSearchService: Reciprocal Rank Fusion (k=60) combining keyword + HNSW semantic searchMetricsCollector: per-tool and per-session compression statscontext stats,context doctor,context searchHooks
PreToolUseHook: budget-aware gate — blocks at BLOCKED level, warns at REDUCED/MINIMALPostToolUseHook: compresses output through pipeline + indexes full content in knowledge baseSubagentRoutingHook: injects batch_execute instructions into Agent/Task tool promptsBudget Management
ContextBudgetManager: topology-aware allocation (hierarchical: coordinator 1.5x, mesh: equal), progressive throttling (NORMAL→REDUCED→MINIMAL→BLOCKED), budget reallocation on agent completionSharedKnowledgeTracker: cross-agent content deduplication via hash trackingSwarmBudgetIntegration: event-driven bridge to swarm coordinator lifecycle with runtime payload validationLive System Integration (3 new files, 5 edited files)
CLI Command (
cli/src/commands/context.ts)stats,search,doctor,budget,compressadvancedMCP Tools (
cli/src/mcp-tools/context-tools.ts)context_stats,context_search,context_doctor,context_budget,context_compressHook Bridge (
hooks/src/bridge/context-bridge.ts)PreToolUseHook→HookEvent.PreToolUse(priority: High) for budget enforcementPostToolUseHook→HookEvent.PostToolUse(priority: Normal) for output compressionSubagentRoutingHook→HookEvent.AgentSpawn(priority: Normal) for batch hintsinitializeHooks()Wiring
@claude-flow/contextadded asoptionalDependenciesto both@claude-flow/cliand@claude-flow/hookstry/catchdynamic imports — system works without@claude-flow/contextinstalledDesign Documents (from first commit)
v3/docs/PRD-context-optimization.mdKey Design Decisions
@claude-flow/contextinoptionalDependencies, try/catch dynamic imports everywherecallMCPTool()instead of importing domain servicesTest plan