Skip to main content

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.
The hash is order-independent. Same features, same hash.

Similarity match

If the exact hash misses, lookup walks every other pattern and scores numeric distance plus string equality.
The default similarity threshold is 0.8. Patterns below the confidence threshold are skipped.

Confidence

Confidence is a simple count: how many times the same decision has been seen for this exact feature vector. It saturates at 20.
Confidence below 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.
The store tracks consecutive failures per pattern. After 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 is similarity * stored confidence * fraction of top-k that agree on the decision. Disagreement among neighbors dilutes confidence.

Outcome reporting

Same shape as PatternStore:
Success bumps the entry’s confidence. Failure decays it. After 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.