Update source: strata/memory.py
Browse files- strata/memory.py +127 -0
strata/memory.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
STRATA-MEMORY: 3-layer memory with continuous scores, decay, and promotion.
|
| 3 |
+
|
| 4 |
+
All pattern values are continuous scores (0..1), never boolean.
|
| 5 |
+
Short → Mid → Long promotion based on frequency and consistency.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
from typing import Dict, List, Optional
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
DECAY_RATE = 0.90 # per-step importance decay for Short/Mid
|
| 12 |
+
PROMOTE_MIN_N = 2 # v2: lowered 3→2; fake_breakout needs faster promotion on 1m
|
| 13 |
+
PROMOTE_THRESHOLD = 0.50 # v2: lowered 0.60→0.50; allow weaker patterns to reach Mid
|
| 14 |
+
LONG_THRESHOLD = 0.65 # v2: lowered 0.70→0.65; Mid→Long graduation less strict
|
| 15 |
+
FORGET_THRESHOLD = 0.05 # importance below this → deleted
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class MemoryLayer:
|
| 19 |
+
"""Single memory layer: key → {score, count}."""
|
| 20 |
+
|
| 21 |
+
def __init__(self, name: str):
|
| 22 |
+
self.name = name
|
| 23 |
+
self._store: Dict[str, Dict] = {}
|
| 24 |
+
|
| 25 |
+
def upsert(self, key: str, score: float) -> None:
|
| 26 |
+
"""Insert or update a pattern with a new score observation."""
|
| 27 |
+
if key in self._store:
|
| 28 |
+
old = self._store[key]
|
| 29 |
+
# Running average weighted toward new observation
|
| 30 |
+
old["score"] = old["score"] * 0.6 + score * 0.4
|
| 31 |
+
old["count"] += 1
|
| 32 |
+
else:
|
| 33 |
+
self._store[key] = {"score": score, "count": 1}
|
| 34 |
+
|
| 35 |
+
def decay(self) -> None:
|
| 36 |
+
"""Apply per-step importance decay and prune forgotten entries."""
|
| 37 |
+
to_delete = []
|
| 38 |
+
for key, entry in self._store.items():
|
| 39 |
+
entry["score"] *= DECAY_RATE
|
| 40 |
+
if entry["score"] < FORGET_THRESHOLD:
|
| 41 |
+
to_delete.append(key)
|
| 42 |
+
for key in to_delete:
|
| 43 |
+
del self._store[key]
|
| 44 |
+
|
| 45 |
+
def get(self, key: str, default: float = 0.0) -> float:
|
| 46 |
+
return self._store.get(key, {}).get("score", default)
|
| 47 |
+
|
| 48 |
+
def get_count(self, key: str) -> int:
|
| 49 |
+
return self._store.get(key, {}).get("count", 0)
|
| 50 |
+
|
| 51 |
+
def all_scores(self) -> Dict[str, float]:
|
| 52 |
+
return {k: v["score"] for k, v in self._store.items()}
|
| 53 |
+
|
| 54 |
+
def pop(self, key: str) -> Optional[Dict]:
|
| 55 |
+
return self._store.pop(key, None)
|
| 56 |
+
|
| 57 |
+
def keys(self):
|
| 58 |
+
return list(self._store.keys())
|
| 59 |
+
|
| 60 |
+
def __repr__(self):
|
| 61 |
+
return f"MemoryLayer({self.name}, {self._store})"
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
class StrataMEMORY:
|
| 65 |
+
"""
|
| 66 |
+
3-layer memory system:
|
| 67 |
+
Short → recent per-step observations
|
| 68 |
+
Mid → session-level patterns (promoted from Short)
|
| 69 |
+
Long → stable pattern bank (promoted from Mid)
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
def __init__(self):
|
| 73 |
+
self.short = MemoryLayer("short")
|
| 74 |
+
self.mid = MemoryLayer("mid")
|
| 75 |
+
self.long = MemoryLayer("long")
|
| 76 |
+
|
| 77 |
+
def observe(self, pattern: str, score: float) -> None:
|
| 78 |
+
"""Record a pattern observation into Short memory."""
|
| 79 |
+
self.short.upsert(pattern, score)
|
| 80 |
+
|
| 81 |
+
def step(self) -> None:
|
| 82 |
+
"""
|
| 83 |
+
Advance one time step:
|
| 84 |
+
1. Try to promote Short → Mid
|
| 85 |
+
2. Try to promote Mid → Long
|
| 86 |
+
3. Decay all layers
|
| 87 |
+
"""
|
| 88 |
+
self._promote_short_to_mid()
|
| 89 |
+
self._promote_mid_to_long()
|
| 90 |
+
self.short.decay()
|
| 91 |
+
self.mid.decay()
|
| 92 |
+
# Long memory decays much slower
|
| 93 |
+
for key, entry in self.long._store.items():
|
| 94 |
+
entry["score"] *= 0.995
|
| 95 |
+
|
| 96 |
+
def _promote_short_to_mid(self) -> None:
|
| 97 |
+
for key in self.short.keys():
|
| 98 |
+
score = self.short.get(key)
|
| 99 |
+
count = self.short.get_count(key)
|
| 100 |
+
if count >= PROMOTE_MIN_N and score >= PROMOTE_THRESHOLD:
|
| 101 |
+
self.mid.upsert(key, score)
|
| 102 |
+
|
| 103 |
+
def _promote_mid_to_long(self) -> None:
|
| 104 |
+
for key in self.mid.keys():
|
| 105 |
+
score = self.mid.get(key)
|
| 106 |
+
count = self.mid.get_count(key)
|
| 107 |
+
if count >= PROMOTE_MIN_N and score >= LONG_THRESHOLD:
|
| 108 |
+
self.long.upsert(key, score)
|
| 109 |
+
|
| 110 |
+
def snapshot(self) -> Dict[str, float]:
|
| 111 |
+
"""
|
| 112 |
+
Return merged view of all layers for STRATA-CORE consumption.
|
| 113 |
+
Long > Mid > Short priority when keys overlap.
|
| 114 |
+
"""
|
| 115 |
+
merged = {}
|
| 116 |
+
merged.update(self.short.all_scores())
|
| 117 |
+
merged.update(self.mid.all_scores())
|
| 118 |
+
merged.update(self.long.all_scores())
|
| 119 |
+
return merged
|
| 120 |
+
|
| 121 |
+
def __repr__(self):
|
| 122 |
+
return (
|
| 123 |
+
f"STRATA-MEMORY\n"
|
| 124 |
+
f" Short: {self.short.all_scores()}\n"
|
| 125 |
+
f" Mid: {self.mid.all_scores()}\n"
|
| 126 |
+
f" Long: {self.long.all_scores()}"
|
| 127 |
+
)
|