2026-08-28 10:25:31 -07:00
|
|
|
|
# M3.8.1 — Context Optimizer Core Modules
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
|
|
|
|
|
| Field | Value |
|
|
|
|
|
|
|---|---|
|
|
|
|
|
|
| Phase | M3.8 — Context optimization |
|
|
|
|
|
|
| Size | L — 3–5 days |
|
2026-08-28 10:25:31 -07:00
|
|
|
|
| Status | 🟡 PARTIAL (modules done, needs ingest wiring) |
|
2026-08-28 09:04:40 -07:00
|
|
|
|
| Spec | `docs/CONTEXT_OPTIMIZER.md` |
|
2026-08-28 10:25:31 -07:00
|
|
|
|
| Blocks | M3.8.2 (ingest integration) |
|
2026-08-28 10:04:17 -07:00
|
|
|
|
| Depends | M3.7.7 (lesson.rs patterns), M3.7.8 (stop words) |
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
## Status: PARTIAL ⚠️
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
✅ **Core Compressor Modules Complete**: 1,100 LOC, 62 tests
|
|
|
|
|
|
- ContentRouter (Magika ML detection)
|
|
|
|
|
|
- LogCompressor, JsonCrusher, DiffCompressor, TextCompressor
|
|
|
|
|
|
- CacheAligner (drift detection)
|
|
|
|
|
|
- CcrStore (reversible compression)
|
|
|
|
|
|
- ContextOptimizer orchestrator
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
❌ **Integration in Wrong Place**:
|
|
|
|
|
|
- Currently: PromptBuilder.build_cache_aligned() (query path)
|
|
|
|
|
|
- Should be: rebuild.rs ingest pipeline (ingest path)
|
|
|
|
|
|
- Result: Improves only LLM input, not search quality
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
## What Was Done Right
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
✅ **Content Detection** (Magika ML + regex)
|
|
|
|
|
|
- <1ms classification
|
|
|
|
|
|
- Detects JSON, code, logs, diffs, config, text
|
|
|
|
|
|
- Thread-safe, ONNX local
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
✅ **5 Compressor Implementations**
|
|
|
|
|
|
- LogCompressor: 85-95% ratio (keep errors + stack traces)
|
|
|
|
|
|
- JsonCrusher: 70-90% ratio (field variance)
|
|
|
|
|
|
- DiffCompressor: 60-80% ratio (change lines only)
|
|
|
|
|
|
- TextCompressor: 30-50% ratio (token importance)
|
|
|
|
|
|
- ConfigCompressor: passthrough (already compact)
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
✅ **Cache Alignment**
|
|
|
|
|
|
- Detects dynamic patterns (timestamps, UUIDs, session IDs)
|
|
|
|
|
|
- Drift metric (0.0-1.0)
|
|
|
|
|
|
- Separates stable prefix from dynamic tail
|
2026-08-28 10:04:17 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
✅ **Reversible Compression** (CCR Store)
|
|
|
|
|
|
- LRU cache with SHA256
|
|
|
|
|
|
- TTL-based expiry
|
|
|
|
|
|
- Model can retrieve originals via hint injection
|
2026-08-28 10:04:17 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
## What Needs Fixing
|
2026-08-28 10:04:17 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
### Root Issue: Architecture Misunderstanding
|
2026-08-28 10:04:17 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
**Documented** (❌ Wrong):
|
|
|
|
|
|
```
|
|
|
|
|
|
Ingest → pgvector + OpenSearch (full noise)
|
|
|
|
|
|
↓
|
|
|
|
|
|
Query → M3.8 compression → LLM
|
2026-08-28 09:12:09 -07:00
|
|
|
|
```
|
|
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
**Should Be** (✅ Correct):
|
|
|
|
|
|
```
|
|
|
|
|
|
Ingest → M3.8 optimization → pgvector + OpenSearch (clean)
|
|
|
|
|
|
↓
|
|
|
|
|
|
Query → retrieve clean results → LLM
|
2026-08-28 10:04:17 -07:00
|
|
|
|
```
|
2026-08-28 09:30:58 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
**Why the correct way is better:**
|
|
|
|
|
|
1. Cleaner text → better embeddings (pgvector)
|
|
|
|
|
|
2. Signal-rich text → better BM25 ranking (OpenSearch)
|
|
|
|
|
|
3. One-time processing at ingest, not per-query
|
|
|
|
|
|
4. All users benefit from cleaner search results
|
|
|
|
|
|
5. LLM already gets optimized chunks
|
2026-08-28 09:30:58 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
### Next Steps
|
2026-08-28 09:30:58 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
**M3.8.2**: Ingest Pipeline Integration (1 day)
|
|
|
|
|
|
- Create OptimizerSink wrapper around ingest sources
|
|
|
|
|
|
- Wire into rebuild.rs
|
|
|
|
|
|
- Test with all source types
|
|
|
|
|
|
- Collect metrics
|
2026-08-28 09:30:58 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
**M3.8.3**: Metrics & Monitoring (1 day)
|
|
|
|
|
|
- Track compression ratio per chunk
|
|
|
|
|
|
- Aggregate per project/source/type
|
|
|
|
|
|
- Emit to tracing/Prometheus
|
|
|
|
|
|
- Dashboard visualization
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
**M3.8.4**: Query Path Cleanup (0.5 days)
|
|
|
|
|
|
- Remove PromptBuilder.build_cache_aligned() optimizer call
|
|
|
|
|
|
- Keep cache_metrics() for observability (drift tracking)
|
|
|
|
|
|
- Simplify PromptBuilder
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
## Test Summary
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
✅ **62 Unit Tests** (all passing)
|
|
|
|
|
|
- Phase 1: 17 (router, log)
|
|
|
|
|
|
- Phase 2: 15 (json, diff)
|
|
|
|
|
|
- Phase 3: 18 (cache align, CCR)
|
|
|
|
|
|
- Phase 4: 12 (text, config)
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
⏳ **20 New Tests Pending** (M3.8.2-3)
|
|
|
|
|
|
- Ingest source optimization
|
|
|
|
|
|
- Metrics collection
|
|
|
|
|
|
- End-to-end pipeline
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
## Files
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
**Implemented** (1,100 LOC):
|
|
|
|
|
|
- `crates/mem-core/src/optimizer/mod.rs`
|
|
|
|
|
|
- `crates/mem-core/src/optimizer/router.rs`
|
|
|
|
|
|
- `crates/mem-core/src/optimizer/log.rs`
|
|
|
|
|
|
- `crates/mem-core/src/optimizer/json.rs`
|
|
|
|
|
|
- `crates/mem-core/src/optimizer/diff.rs`
|
|
|
|
|
|
- `crates/mem-core/src/optimizer/text.rs`
|
|
|
|
|
|
- `crates/mem-core/src/optimizer/cache_align.rs`
|
|
|
|
|
|
- `crates/mem-core/src/optimizer/ccr.rs`
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
**Pending** (180 LOC):
|
|
|
|
|
|
- `crates/mem-ingest/src/optimizer_sink.rs` (M3.8.2)
|
|
|
|
|
|
- `crates/mem-core/src/optimizer/metrics.rs` (M3.8.3)
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
## Commits
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
1. `bf13e3a` — Phase 1: ContentRouter + LogCompressor
|
|
|
|
|
|
2. `a903a3f` — Phase 2: JsonCrusher + DiffCompressor
|
|
|
|
|
|
3. `edcc231` — Phase 3: CacheAligner + CcrStore
|
|
|
|
|
|
4. `8d8addc` — Phase 4: TextCompressor + env config
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
## Lessons Learned
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
1. **Ingest-time optimization > query-time**: Better for entire pipeline
|
|
|
|
|
|
2. **Compression ratios vary widely**: Log 85-95% vs text 30-50%
|
|
|
|
|
|
3. **Reversibility matters**: Model needs originals for detailed analysis
|
|
|
|
|
|
4. **Metrics > assumption**: Need to measure actual improvement in search quality
|
2026-08-28 09:04:40 -07:00
|
|
|
|
|
2026-08-28 10:25:31 -07:00
|
|
|
|
## Remediation
|
|
|
|
|
|
|
|
|
|
|
|
See **`tasks/M3.8-CORRECTED-architecture.md`** for complete re-architecture plan.
|