{"skill":{"slug":"midos-memory-cascade","displayName":"MidOS Memory Cascade","summary":"Auto-escalating multi-tier memory search that cascades from in-memory cache through SQLite, grep, and LanceDB vector search to find the best answer with mini...","description":"---\nname: midos-memory-cascade\ndescription: Auto-escalating multi-tier memory search that cascades from in-memory cache through SQLite, grep, and LanceDB vector search to find the best answer with minimal latency.\nmetadata:\n  {}\n---\n\n# MidOS Memory Cascade\n\nA self-tuning, auto-escalating search engine that tries each memory tier from fastest to slowest, stopping as soon as it finds a high-confidence answer.\n\n## What It Does\n\nInstead of the agent deciding which storage layer to query, the cascade tries each tier automatically:\n\n| Tier | Storage | Latency | Strategy |\n|------|---------|---------|----------|\n| T0 | In-memory session cache | <1ms | Exact + fuzzy key match |\n| T1 | JSON state files | <5ms | Filename + key match |\n| T2 | SQLite (pipeline_synergy.db) | <5ms | Structured SQL LIKE |\n| T3 | SQLite FTS5 | <1ms | Full-text keyword on 22K rows |\n| T4 | Grep over 46K chunks | ~3s | Brute-force ripgrep fallback |\n| T5 | LanceDB keyword (BM25) | slow | 670K vector rows, no embeddings |\n| T5b | LanceDB semantic | 3–30s | Embedding similarity, last resort |\n\n**Question routing:** Queries starting with how/what/why/etc. skip keyword tiers and route directly to semantic search.\n\n**Self-learning:** The cascade records which tier resolves each query. After enough history, `evolve()` learns shortcuts (skip directly to the winning tier) and marks consistently-empty tiers for skip.\n\n## Usage\n\n### Python API\n\n```python\nfrom tools.memory.memory_cascade import recall, store\n\n# Search across all tiers\nresult = recall(\"adaptive alpha reranking\")\n# → {\"answer\": {...}, \"tier\": \"T5:lancedb\", \"latency_ms\": 340, \"confidence\": 0.87}\n\n# Write to the right storage automatically\nstore(\"pattern\", content=\"...\", tags=[\"ml\", \"reranking\"])\n```\n\n### CLI\n\n```bash\n# Search\npython memory_cascade.py recall \"query here\"\n\n# View tier resolution stats\npython memory_cascade.py stats\n\n# Run self-evolution (learn shortcuts + tier skips)\npython memory_cascade.py evolve\n```\n\n### `recall()` Options\n\n```python\nrecall(\n    query: str,\n    min_confidence: float = 0.5,  # stop escalating at this threshold\n    max_tier: int = 6             # 0=T0 only, 6=all tiers\n)\n```\n\nReturns:\n```json\n{\n  \"answer\": { \"source\": \"...\", \"text\": \"...\" },\n  \"confidence\": 0.87,\n  \"latency_ms\": 340.2,\n  \"tiers_tried\": 3,\n  \"resolved_at\": \"T5:lancedb\",\n  \"shortcut\": null,\n  \"question_routed\": false,\n  \"escalation\": [...]\n}\n```\n\n## Requirements\n\n- Python 3.10+ (stdlib only for core cascade logic)\n- Optional: `hive_commons` for LanceDB tiers (T5/T5b)\n- Optional: `tools.memory.memory_router` for `store()` routing\n\nThe cascade degrades gracefully — if LanceDB is unavailable, it stops at grep (T4). All stdlib tiers (T0–T4) work with zero dependencies.\n\n## Architecture Notes\n\n- **Thread-safe:** Session cache uses `threading.Lock`; stats writes use separate locks\n- **Cross-process safe:** JSONL writes use OS-level file locking (`msvcrt` on Windows, `fcntl` on Unix)\n- **Confidence scoring:** Term overlap × score × content richness → normalized 0–1\n- **Stats persistence:** `knowledge/SYSTEM/cascade_stats.json` accumulates hit rates per tier\n\nBuilt with MidOS. 1 of 200+ skills. Full ecosystem at midos.dev/pro\n","topics":["Sqlite"],"tags":{"latest":"1.0.0"},"stats":{"comments":0,"downloads":788,"installsAllTime":30,"installsCurrent":2,"stars":0,"versions":2},"createdAt":1772647522873,"updatedAt":1778995296535},"latestVersion":{"version":"1.0.0","createdAt":1772872723141,"changelog":"Memory cascade system: multi-tier SQLite storage, decay scoring, contextual routing","license":null},"metadata":null,"owner":{"handle":"msruruguay","userId":"s178etmyyhpjh94kwr6h46c01n885rgg","displayName":"msruruguay","image":"https://avatars.githubusercontent.com/u/85544849?v=4"},"moderation":{"isSuspicious":false,"isMalwareBlocked":false,"verdict":"clean","reasonCodes":["review.llm_review"],"summary":"Review: review.llm_review","engineVersion":"v2.4.24","updatedAt":1780089791473}}