Pattern store and adaptive memory
Two cache layers sit between the body and the LLM. The pattern store handles exact and very-similar situations. Adaptive memory handles “this looks like one we have seen before” situations.PatternStore
A feature-keyed cache. You give it a function that turns an entity into a small object of strings, numbers, and booleans.Exact match
lookup first hashes the feature vector and looks for that exact hash.
Similarity match
If the exact hash misses,lookup walks every other pattern and scores numeric distance plus string equality.
Confidence
Confidence is a simple count: how many times the same decision has been seen for this exact feature vector. It saturates at 20.confidenceThreshold (default 0.6) is treated as “not yet trusted” and forces a brain call.
Exploration rate
Even confident hits get re-checked every now and then so the cache does not drift.explorationRate: 0 makes the cache deterministic. Useful in tests.
Success rate monitoring
After every cached decision, report what happened.failureThreshold (default 3) the entry is purged and the store emits pattern_invalidated.
store.stats() returns hit rate, average success rate, low-confidence count, and totals.
AdaptiveMemory
Adaptive memory is a similarity-scored decision store that generalizes from past LLM decisions when the pattern store cannot.Scoring
Distance is weighted euclidean over normalized feature space. The final confidence issimilarity * stored confidence * fraction of top-k that agree on the decision. Disagreement among neighbors dilutes confidence.
Outcome reporting
Same shape asPatternStore:
failureThreshold consecutive failures the entry is purged.
Persistence
better-sqlite3 is an optional peer dep. If it is not installed, persistence calls return 0 and the store stays in memory.
Wiring both layers into a body
SCPBody.decideLocally consults the pattern store first, then the adaptive memory.
learnFromBrain is the only call that needs to know about both stores. The decision flow takes care of the rest.