dsh-biocore 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (210) hide show
  1. package/lib/index.js +170 -27
  2. package/package.json +1 -1
  3. package/python/biocore_runtime/pipeline.py +42 -0
  4. package/python/biocore_runtime/rerank.py +201 -0
  5. package/python/mindbase/__init__.py +18 -0
  6. package/python/mindbase/__main__.py +3 -0
  7. package/python/mindbase/actions.py +56 -0
  8. package/python/mindbase/active_inference.py +54 -0
  9. package/python/mindbase/addressing.py +69 -0
  10. package/python/mindbase/affect.py +86 -0
  11. package/python/mindbase/affective.py +111 -0
  12. package/python/mindbase/allostasis.py +28 -0
  13. package/python/mindbase/appraisal.py +101 -0
  14. package/python/mindbase/attachment.py +49 -0
  15. package/python/mindbase/attention.py +30 -0
  16. package/python/mindbase/autonomy.py +81 -0
  17. package/python/mindbase/autonomy_bench.py +251 -0
  18. package/python/mindbase/autonomy_evolve.py +342 -0
  19. package/python/mindbase/autonomy_loop.py +65 -0
  20. package/python/mindbase/autonomy_meta.py +91 -0
  21. package/python/mindbase/base_only.py +28 -0
  22. package/python/mindbase/benchmark.py +61 -0
  23. package/python/mindbase/bio_adaptation.py +274 -0
  24. package/python/mindbase/bio_noise.py +39 -0
  25. package/python/mindbase/bio_semantic.py +138 -0
  26. package/python/mindbase/bm25.py +46 -0
  27. package/python/mindbase/bootstrap.py +256 -0
  28. package/python/mindbase/bootstrap_real.py +231 -0
  29. package/python/mindbase/brain.py +178 -0
  30. package/python/mindbase/care.py +78 -0
  31. package/python/mindbase/chaos_search.py +79 -0
  32. package/python/mindbase/character_profile.py +33 -0
  33. package/python/mindbase/circadian.py +38 -0
  34. package/python/mindbase/circuit.py +235 -0
  35. package/python/mindbase/clock.py +53 -0
  36. package/python/mindbase/companion_runtime.py +366 -0
  37. package/python/mindbase/companion_train.py +126 -0
  38. package/python/mindbase/config.py +66 -0
  39. package/python/mindbase/connection_state.py +76 -0
  40. package/python/mindbase/connectome_prior.py +35 -0
  41. package/python/mindbase/connectome_world.py +158 -0
  42. package/python/mindbase/constructs.py +88 -0
  43. package/python/mindbase/context_builder.py +58 -0
  44. package/python/mindbase/critical.py +36 -0
  45. package/python/mindbase/criticality.py +123 -0
  46. package/python/mindbase/defaults.py +27 -0
  47. package/python/mindbase/demo.py +163 -0
  48. package/python/mindbase/diary.py +116 -0
  49. package/python/mindbase/diary_service.py +40 -0
  50. package/python/mindbase/digital_prior.py +37 -0
  51. package/python/mindbase/diversity.py +110 -0
  52. package/python/mindbase/drives.py +106 -0
  53. package/python/mindbase/embedding.py +132 -0
  54. package/python/mindbase/embedding_memory.py +74 -0
  55. package/python/mindbase/embodiment.py +83 -0
  56. package/python/mindbase/emotion_calibration.py +39 -0
  57. package/python/mindbase/emotion_calibrator.py +50 -0
  58. package/python/mindbase/emotion_core.py +113 -0
  59. package/python/mindbase/emotion_dynamics.py +104 -0
  60. package/python/mindbase/emotion_intent.py +164 -0
  61. package/python/mindbase/emotion_layer.py +200 -0
  62. package/python/mindbase/emotion_schema.py +92 -0
  63. package/python/mindbase/engine.py +781 -0
  64. package/python/mindbase/episode_log.py +90 -0
  65. package/python/mindbase/evaluation.py +493 -0
  66. package/python/mindbase/event_appraisal.py +84 -0
  67. package/python/mindbase/evolution.py +77 -0
  68. package/python/mindbase/evolve_offline.py +232 -0
  69. package/python/mindbase/experiments.py +358 -0
  70. package/python/mindbase/expression.py +54 -0
  71. package/python/mindbase/external_brain.py +157 -0
  72. package/python/mindbase/fly_mb.py +159 -0
  73. package/python/mindbase/fly_tasks.py +45 -0
  74. package/python/mindbase/forage.py +179 -0
  75. package/python/mindbase/goal_generator.py +98 -0
  76. package/python/mindbase/goals.py +153 -0
  77. package/python/mindbase/grid_world.py +123 -0
  78. package/python/mindbase/group_autonomy.py +92 -0
  79. package/python/mindbase/group_context.py +67 -0
  80. package/python/mindbase/homeostatic.py +71 -0
  81. package/python/mindbase/homeostatic_action.py +38 -0
  82. package/python/mindbase/human_standard.py +53 -0
  83. package/python/mindbase/identity_suite.py +50 -0
  84. package/python/mindbase/image_context.py +27 -0
  85. package/python/mindbase/impression.py +119 -0
  86. package/python/mindbase/initiative_quota.py +145 -0
  87. package/python/mindbase/insight.py +51 -0
  88. package/python/mindbase/intelligence_search.py +108 -0
  89. package/python/mindbase/intention.py +76 -0
  90. package/python/mindbase/interaction_log.py +30 -0
  91. package/python/mindbase/interoception.py +67 -0
  92. package/python/mindbase/invariants.py +53 -0
  93. package/python/mindbase/iteration.py +29 -0
  94. package/python/mindbase/layered_memory.py +170 -0
  95. package/python/mindbase/layers/__init__.py +18 -0
  96. package/python/mindbase/layers/l0_substrate.py +39 -0
  97. package/python/mindbase/layers/l1_homeostasis.py +6 -0
  98. package/python/mindbase/layers/l2_affect.py +6 -0
  99. package/python/mindbase/layers/l3_attention.py +6 -0
  100. package/python/mindbase/layers/l4_learning.py +17 -0
  101. package/python/mindbase/layers/l5_self_social.py +35 -0
  102. package/python/mindbase/layers/l6_expression.py +23 -0
  103. package/python/mindbase/layers/l7_evaluation.py +45 -0
  104. package/python/mindbase/learner.py +196 -0
  105. package/python/mindbase/lexicon.py +33 -0
  106. package/python/mindbase/life_state.py +169 -0
  107. package/python/mindbase/lifecycle.py +87 -0
  108. package/python/mindbase/liquid_readout.py +48 -0
  109. package/python/mindbase/llm_adapter.py +233 -0
  110. package/python/mindbase/llm_cache.py +77 -0
  111. package/python/mindbase/llm_gate.py +85 -0
  112. package/python/mindbase/llm_openai.py +130 -0
  113. package/python/mindbase/llm_setup.py +99 -0
  114. package/python/mindbase/longrun.py +83 -0
  115. package/python/mindbase/longterm_memory.py +606 -0
  116. package/python/mindbase/mb/__init__.py +14 -0
  117. package/python/mindbase/mb/apl.py +22 -0
  118. package/python/mindbase/mb/dan.py +14 -0
  119. package/python/mindbase/mb/habituation.py +27 -0
  120. package/python/mindbase/mb/kc.py +28 -0
  121. package/python/mindbase/mb/mbon.py +23 -0
  122. package/python/mindbase/mb/mushroom_body.py +116 -0
  123. package/python/mindbase/memory.py +201 -0
  124. package/python/mindbase/memory_framework.py +158 -0
  125. package/python/mindbase/memory_retriever.py +110 -0
  126. package/python/mindbase/memory_summarizer.py +177 -0
  127. package/python/mindbase/memory_task.py +32 -0
  128. package/python/mindbase/memory_trace.py +46 -0
  129. package/python/mindbase/meta.py +32 -0
  130. package/python/mindbase/motivation.py +31 -0
  131. package/python/mindbase/multi_session.py +19 -0
  132. package/python/mindbase/narrative.py +87 -0
  133. package/python/mindbase/needs.py +77 -0
  134. package/python/mindbase/neural.py +45 -0
  135. package/python/mindbase/neuron_models.py +134 -0
  136. package/python/mindbase/noise.py +309 -0
  137. package/python/mindbase/one_on_one.py +60 -0
  138. package/python/mindbase/online_iteration.py +63 -0
  139. package/python/mindbase/ostracism.py +35 -0
  140. package/python/mindbase/parallel_eval.py +60 -0
  141. package/python/mindbase/parallel_qd.py +64 -0
  142. package/python/mindbase/persona.py +221 -0
  143. package/python/mindbase/persona_ab.py +64 -0
  144. package/python/mindbase/persona_bridge.py +129 -0
  145. package/python/mindbase/persona_compile.py +34 -0
  146. package/python/mindbase/persona_dsh.py +120 -0
  147. package/python/mindbase/persona_learner.py +157 -0
  148. package/python/mindbase/physiology.py +197 -0
  149. package/python/mindbase/population_lifecycle.py +83 -0
  150. package/python/mindbase/preferences.py +50 -0
  151. package/python/mindbase/proactive_agent.py +274 -0
  152. package/python/mindbase/proactive_gate.py +53 -0
  153. package/python/mindbase/proactive_group.py +119 -0
  154. package/python/mindbase/proactive_policy.py +422 -0
  155. package/python/mindbase/proactive_profile.py +196 -0
  156. package/python/mindbase/proactive_timing.py +28 -0
  157. package/python/mindbase/pyramid_stages.py +61 -0
  158. package/python/mindbase/quantum.py +132 -0
  159. package/python/mindbase/quantum_chaos.py +98 -0
  160. package/python/mindbase/reflection.py +68 -0
  161. package/python/mindbase/regulation_posture.py +50 -0
  162. package/python/mindbase/relationship_session.py +431 -0
  163. package/python/mindbase/relationship_suite.py +45 -0
  164. package/python/mindbase/relationships.py +70 -0
  165. package/python/mindbase/replay.py +22 -0
  166. package/python/mindbase/reply_diversity.py +53 -0
  167. package/python/mindbase/reply_gate.py +52 -0
  168. package/python/mindbase/rerank.py +41 -0
  169. package/python/mindbase/rerank_local.py +46 -0
  170. package/python/mindbase/ripple.py +187 -0
  171. package/python/mindbase/robust_search.py +68 -0
  172. package/python/mindbase/runtime.py +113 -0
  173. package/python/mindbase/scoped_identity.py +116 -0
  174. package/python/mindbase/self_insight.py +51 -0
  175. package/python/mindbase/self_model.py +116 -0
  176. package/python/mindbase/selftest.py +118 -0
  177. package/python/mindbase/semantic_memory.py +108 -0
  178. package/python/mindbase/session_store.py +175 -0
  179. package/python/mindbase/session_window.py +31 -0
  180. package/python/mindbase/shadow_eval.py +42 -0
  181. package/python/mindbase/sim_env.py +86 -0
  182. package/python/mindbase/social.py +138 -0
  183. package/python/mindbase/social_constraint.py +141 -0
  184. package/python/mindbase/social_feedback.py +95 -0
  185. package/python/mindbase/social_norm.py +225 -0
  186. package/python/mindbase/spiking_reservoir.py +204 -0
  187. package/python/mindbase/spontaneity.py +40 -0
  188. package/python/mindbase/story_loop.py +203 -0
  189. package/python/mindbase/survival_gauntlet.py +128 -0
  190. package/python/mindbase/symbolic.py +170 -0
  191. package/python/mindbase/teacher.py +244 -0
  192. package/python/mindbase/theory.py +42 -0
  193. package/python/mindbase/theory_of_mind.py +98 -0
  194. package/python/mindbase/translation_layer.py +80 -0
  195. package/python/mindbase/turn_eval.py +52 -0
  196. package/python/mindbase/types.py +78 -0
  197. package/python/mindbase/uncertainty_field.py +61 -0
  198. package/python/mindbase/user_loop.py +45 -0
  199. package/python/mindbase/user_profile.py +73 -0
  200. package/python/mindbase/value_model.py +64 -0
  201. package/python/mindbase/values.py +102 -0
  202. package/python/mindbase/vector_store.py +78 -0
  203. package/python/mindbase/viability.py +41 -0
  204. package/python/mindbase/virtual_body.py +90 -0
  205. package/python/mindbase/will.py +60 -0
  206. package/python/mindbase/working_memory.py +32 -0
  207. package/python/mindbase/workspace.py +55 -0
  208. package/ui/panel.css +16 -5
  209. package/ui/panel.html +25 -1
  210. package/ui/panel.js +57 -15
@@ -0,0 +1,14 @@
1
+ """Mushroom body (L4 learning core), split by component.
2
+
3
+ Import the public API from here:
4
+ from mindbase.mb import MushroomBody, KCField
5
+ """
6
+
7
+ from .apl import APL
8
+ from .dan import DAN
9
+ from .habituation import Habituation
10
+ from .kc import KCField
11
+ from .mbon import MBON
12
+ from .mushroom_body import MushroomBody
13
+
14
+ __all__ = ["KCField", "MBON", "DAN", "APL", "Habituation", "MushroomBody"]
@@ -0,0 +1,22 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import List
4
+
5
+ from ..noise import clip
6
+
7
+
8
+ class APL:
9
+ """Feedback inhibition loop: KC activity -> APL -> inhibition of KC output."""
10
+
11
+ def __init__(self, strength: float = 0.50, tau: float = 0.20):
12
+ self.strength = strength
13
+ self.tau = tau
14
+ self.activity = 0.0
15
+
16
+ def step(self, kc: List[int], n_kc: int, dt: float = 0.05) -> float:
17
+ if n_kc <= 0:
18
+ return 0.0
19
+ density = len(kc) / float(n_kc)
20
+ self.activity += (density - self.activity) * (max(0.0, dt) / max(self.tau, 1e-6))
21
+ self.activity = clip(self.activity, 0.0, 1.0)
22
+ return 1.0 / (1.0 + self.strength * self.activity * n_kc)
@@ -0,0 +1,14 @@
1
+ from __future__ import annotations
2
+
3
+ from ..noise import clip
4
+
5
+
6
+ class DAN:
7
+ """Dopaminergic teaching signal: reward minus prediction (RPE)."""
8
+
9
+ def __init__(self):
10
+ self.last = 0.0
11
+
12
+ def compute(self, reward: float, prediction: float) -> float:
13
+ self.last = clip(reward - prediction, -1.0, 1.0)
14
+ return self.last
@@ -0,0 +1,27 @@
1
+ from __future__ import annotations
2
+
3
+ import math
4
+ from typing import Dict
5
+
6
+ from ..noise import clip
7
+
8
+
9
+ class Habituation:
10
+ """Non-associative learning: repeated cue -> weaker response; threat -> sensitize."""
11
+
12
+ def __init__(self, tau: float = 300.0):
13
+ self.tau = tau
14
+ self.state: Dict[str, float] = {}
15
+
16
+ def response(self, cue: str) -> float:
17
+ return self.state.get(cue, 1.0)
18
+
19
+ def on_cue(self, cue: str, valence: float, dt: float) -> float:
20
+ if not cue:
21
+ return 1.0
22
+ h = self.state.get(cue, 1.0)
23
+ h *= math.exp(-max(0.0, dt) / max(self.tau, 1e-6))
24
+ if valence < -0.3:
25
+ h = clip(h + 0.30, 0.0, 1.0) # sensitization after negative event
26
+ self.state[cue] = clip(h, 0.0, 1.0)
27
+ return self.state[cue]
@@ -0,0 +1,28 @@
1
+ from __future__ import annotations
2
+
3
+ import hashlib
4
+ from dataclasses import dataclass
5
+ from typing import List
6
+
7
+ from .. import defaults
8
+
9
+
10
+ @dataclass
11
+ class KCField:
12
+ """Sparse Kenyon-cell code: a cue activates a small, fixed fraction of KCs."""
13
+
14
+ n: int = defaults.KC_SLOTS
15
+ k: int = defaults.KC_ACTIVE
16
+
17
+ def encode(self, cue: str) -> List[int]:
18
+ if not cue:
19
+ return []
20
+ idx: List[int] = []
21
+ i = 0
22
+ while len(idx) < min(self.k, self.n):
23
+ h = hashlib.sha1((cue + "|" + str(i)).encode("utf-8")).digest()
24
+ v = int.from_bytes(h[:4], "little") % self.n
25
+ if v not in idx:
26
+ idx.append(v)
27
+ i += 1
28
+ return sorted(idx)
@@ -0,0 +1,23 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import List
4
+
5
+ from ..noise import clip
6
+
7
+
8
+ class MBON:
9
+ """Mushroom-body output neuron: reads the sparse KC code, learns via DAN."""
10
+
11
+ def __init__(self, n_kc: int):
12
+ self.w: List[float] = [0.0] * n_kc
13
+
14
+ def predict(self, kc: List[int]) -> float:
15
+ if not kc:
16
+ return 0.0
17
+ return sum(self.w[i] for i in kc) / len(kc)
18
+
19
+ def learn(self, kc: List[int], dan: float, lr: float) -> None:
20
+ if not kc:
21
+ return
22
+ for i in kc:
23
+ self.w[i] = clip(self.w[i] + lr * dan, -1.0, 1.0)
@@ -0,0 +1,116 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Dict, List, Tuple
4
+
5
+ from .. import defaults
6
+ from ..connectome_prior import CONNECTOME
7
+ from ..digital_prior import FLY
8
+ from ..noise import clip
9
+ from .apl import APL
10
+ from .dan import DAN
11
+ from .habituation import Habituation
12
+ from .kc import KCField
13
+ from .mbon import MBON
14
+
15
+
16
+ class MushroomBody:
17
+ """Drosophila mushroom body with two DAN/MBON channels.
18
+
19
+ reward channel : appetitive value (approach)
20
+ punishment channel: aversive value (avoidance)
21
+ value = reward - punishment
22
+ """
23
+
24
+ def __init__(
25
+ self,
26
+ n_kc: int = defaults.KC_SLOTS,
27
+ k: int | None = None,
28
+ lr: float = 0.05,
29
+ punish_cross_lr: float = 0.5,
30
+ ):
31
+ k = FLY.compressed_kc(n_kc) if k is None else k
32
+ self.kc = KCField(n=n_kc, k=k)
33
+ self.mbon_reward = MBON(n_kc)
34
+ self.mbon_punishment = MBON(n_kc)
35
+ self.dan_reward = DAN()
36
+ self.dan_punishment = DAN()
37
+ self.apl = APL()
38
+ self.hab = Habituation()
39
+ self.lr = lr
40
+ self.punish_cross_lr = punish_cross_lr
41
+
42
+ def predict_channels(self, cue: str) -> Dict[str, object]:
43
+ kc = self.kc.encode(cue)
44
+ reward = self.mbon_reward.predict(kc)
45
+ punishment = self.mbon_punishment.predict(kc)
46
+ return {
47
+ "reward": reward,
48
+ "punishment": punishment,
49
+ "value": reward - punishment,
50
+ "kc": kc,
51
+ }
52
+
53
+ def predict(self, cue: str) -> Tuple[float, List[int]]:
54
+ ch = self.predict_channels(cue)
55
+ return float(ch["value"]), list(ch["kc"]) # type: ignore[arg-type]
56
+
57
+ def learn(self, cue: str, feedback: float, dt: float = 60.0) -> Dict[str, object]:
58
+ ch = self.predict_channels(cue)
59
+ kc = list(ch["kc"]) # type: ignore[arg-type]
60
+ reward_pred = float(ch["reward"])
61
+ punishment_pred = float(ch["punishment"])
62
+
63
+ # DAN compartments: appetitive and aversive are separate channels
64
+ r_plus = max(0.0, feedback)
65
+ r_minus = max(0.0, -feedback)
66
+ dan_r = self.dan_reward.compute(r_plus, reward_pred)
67
+ dan_p = self.dan_punishment.compute(r_minus, punishment_pred)
68
+
69
+ # PPL1 punishment weakens the approach (reward) MBON in proportion to KC firing.
70
+ dan_r_eff = dan_r - self.punish_cross_lr * r_minus
71
+
72
+ gain = self.apl.step(kc, self.kc.n, dt=0.05)
73
+ self.mbon_reward.learn(kc, dan_r_eff * gain, self.lr)
74
+ self.mbon_punishment.learn(kc, dan_p * gain, self.lr)
75
+ hab = self.hab.on_cue(cue, feedback, dt)
76
+
77
+ return {
78
+ "reward": reward_pred,
79
+ "punishment": punishment_pred,
80
+ "value": reward_pred - punishment_pred,
81
+ "dan_reward": dan_r_eff,
82
+ "dan_punishment": dan_p,
83
+ "punish_cross_lr": self.punish_cross_lr,
84
+ "habituation": hab,
85
+ "kc": float(len(kc)),
86
+ }
87
+
88
+ def homeostatic(self, target: float = 20.0) -> None:
89
+ """Synaptic scaling: keep total absolute weight bounded."""
90
+ for mbon in (self.mbon_reward, self.mbon_punishment):
91
+ total = sum(abs(w) for w in mbon.w)
92
+ if total > target:
93
+ scale = target / total
94
+ mbon.w = [clip(w * scale, -1.0, 1.0) for w in mbon.w]
95
+
96
+ def consolidate(self) -> None:
97
+ self.mbon_reward.w = [clip(w * 0.995, -1.0, 1.0) for w in self.mbon_reward.w]
98
+ self.mbon_punishment.w = [clip(w * 0.995, -1.0, 1.0) for w in self.mbon_punishment.w]
99
+ self.homeostatic()
100
+
101
+ def snapshot(self) -> Dict[str, object]:
102
+ def nonzero(w: List[float]) -> int:
103
+ return sum(1 for x in w if abs(x) > 1e-6)
104
+
105
+ return {
106
+ "prior": FLY.summary(),
107
+ "n_kc": self.kc.n,
108
+ "k": self.kc.k,
109
+ "reward_nonzero": nonzero(self.mbon_reward.w),
110
+ "punishment_nonzero": nonzero(self.mbon_punishment.w),
111
+ "reward_sum": round(sum(self.mbon_reward.w), 4),
112
+ "punishment_sum": round(sum(self.mbon_punishment.w), 4),
113
+ "apl_activity": round(self.apl.activity, 4),
114
+ "connectome": CONNECTOME.summary(),
115
+ "habituation": {k: round(v, 3) for k, v in list(self.hab.state.items())[-8:]},
116
+ }
@@ -0,0 +1,201 @@
1
+ from __future__ import annotations
2
+
3
+ import math
4
+ import random
5
+ from dataclasses import dataclass
6
+ from typing import Dict, List, Optional, Set
7
+
8
+ from .config import MindConfig
9
+ from .noise import clip, jaccard, sigmoid
10
+
11
+
12
+ @dataclass
13
+ class MemoryItem:
14
+ text: str
15
+ importance: float
16
+ t: float
17
+ hits: int = 0
18
+ chars: Set[str] = None # type: ignore
19
+ bigrams: Set[str] = None # type: ignore
20
+ last_access: float = 0.0
21
+ confidence: float = 0.50
22
+ semanticized: bool = False
23
+ source: str = "episodic"
24
+ valence: float = 0.0
25
+ arousal: float = 0.5
26
+
27
+ def __post_init__(self):
28
+ if self.chars is None:
29
+ self.chars = set(self.text)
30
+ if self.bigrams is None:
31
+ self.bigrams = {self.text[i:i + 2] for i in range(max(0, len(self.text) - 1))}
32
+ if self.last_access <= 0.0:
33
+ self.last_access = self.t
34
+
35
+
36
+ class SparseMemory:
37
+ """Sparse associative memory with DA-gated writes and top-1 retrieval.
38
+
39
+ Mirrors the mushroom-body motif: a cue activates few items, one wins.
40
+ """
41
+
42
+ def __init__(self, cfg: MindConfig, rng: random.Random):
43
+ self.cfg = cfg
44
+ self.rng = rng
45
+ self.items: List[MemoryItem] = []
46
+ self.current_valence = 0.0
47
+ self.current_arousal = 0.5
48
+ self.affect_weight = float(getattr(cfg, "memory_affect_weight", 0.0))
49
+
50
+ def set_affect(self, valence: float, arousal: float) -> None:
51
+ self.current_valence = clip(float(valence), -1.0, 1.0)
52
+ self.current_arousal = clip(float(arousal), 0.0, 1.0)
53
+
54
+ def activation_components(
55
+ self, item: MemoryItem, cue_chars: Set[str], cue_bigrams: Set[str], now: float
56
+ ) -> Dict[str, float]:
57
+ age = max(0.0, now - item.last_access)
58
+ recency = 0.5 ** (age / max(self.cfg.memory_half_life, 1e-6))
59
+ importance = float(item.importance)
60
+ frequency = min(1.0, math.log1p(item.hits) / math.log1p(10))
61
+ relevance = 0.7 * jaccard(item.bigrams, cue_bigrams) + 0.3 * jaccard(item.chars, cue_chars)
62
+ return {
63
+ "recency": clip(recency, 0.0, 1.0),
64
+ "importance": clip(importance, 0.0, 1.0),
65
+ "frequency": clip(frequency, 0.0, 1.0),
66
+ "relevance": clip(relevance, 0.0, 1.0),
67
+ }
68
+
69
+ def _activation(self, item: MemoryItem, cue_chars: Set[str], cue_bigrams: Set[str], now: float) -> float:
70
+ parts = self.activation_components(item, cue_chars, cue_bigrams, now)
71
+ weights = self.cfg.memory_activation_weights
72
+ total = sum(weights)
73
+ if total <= 0:
74
+ return 0.0
75
+ return sum(w * parts[k] for w, k in zip(weights, ("recency", "importance", "frequency", "relevance"))) / total
76
+
77
+ def write(self, text: str, importance: float, rpe: float, now: float, valence: float = 0.0, arousal: float = 0.5) -> bool:
78
+ if not text:
79
+ return False
80
+ p = sigmoid(2.0 * (importance - 0.3) + 0.8 * rpe)
81
+ if self.rng.random() > p:
82
+ return False
83
+ self.items.append(MemoryItem(text=text, importance=clip(importance, 0.0, 1.0), t=now,
84
+ valence=clip(float(valence), -1.0, 1.0), arousal=clip(float(arousal), 0.0, 1.0)))
85
+ if len(self.items) > self.cfg.memory_capacity:
86
+ self.consolidate(now)
87
+ return True
88
+
89
+ def _rank(self, cue: str, now: float) -> List[tuple]:
90
+ if not cue or not self.items:
91
+ return []
92
+ cc = set(cue)
93
+ cb = {cue[i:i + 2] for i in range(max(0, len(cue) - 1))}
94
+ scored = []
95
+ for item in self.items:
96
+ score = self._activation(item, cc, cb, now)
97
+ if self.affect_weight > 0.0:
98
+ dv = abs(float(item.valence) - self.current_valence) / 2.0
99
+ da = abs(float(item.arousal) - self.current_arousal)
100
+ congruence = 1.0 - min(1.0, 0.6 * dv + 0.4 * da)
101
+ score += self.affect_weight * congruence
102
+ scored.append((score, item))
103
+ scored.sort(key=lambda x: x[0], reverse=True)
104
+ return scored
105
+
106
+ def retrieve_top_k(self, cue: str, now: float, k: int = 3, threshold: float = 0.05) -> List[MemoryItem]:
107
+ ranked = [(a, i) for a, i in self._rank(cue, now) if a > threshold]
108
+ out: List[MemoryItem] = []
109
+ for _, item in ranked[: max(1, int(k))]:
110
+ item.hits += 1
111
+ item.last_access = now
112
+ out.append(item)
113
+ return out
114
+
115
+ def retrieve_embedding(self, cue: str, now: float, k: int = 3, client=None, chunk_chars: int = 0) -> List[MemoryItem]:
116
+ from .embedding import chunk_text, cosine
117
+
118
+ if not cue or not self.items or client is None:
119
+ return []
120
+ q = client.embed([cue])[0]
121
+ docs = []
122
+ for item in self.items:
123
+ parts = chunk_text(item.text, chunk_chars) if chunk_chars else [item.text]
124
+ vecs = client.embed(parts)
125
+ docs.append(max(vecs, key=lambda v: cosine(q, v)))
126
+ order = sorted(range(len(docs)), key=lambda i: cosine(q, docs[i]), reverse=True)[:k]
127
+ out: List[MemoryItem] = []
128
+ for idx in order:
129
+ item = self.items[idx]
130
+ item.hits += 1
131
+ item.last_access = now
132
+ out.append(item)
133
+ return out
134
+
135
+ def retrieve_hybrid(self, cue: str, now: float, k: int = 3, client=None, alpha: float = 0.6) -> List[MemoryItem]:
136
+ from .bm25 import BM25
137
+ from .embedding import cosine
138
+
139
+ if not cue or not self.items or client is None:
140
+ return []
141
+ docs = [i.text for i in self.items]
142
+ bm = [BM25(docs).score(cue, i) for i in range(len(docs))]
143
+ q = client.embed([cue])[0]
144
+ emb = [cosine(q, v) for v in client.embed(docs)]
145
+ def norm(xs):
146
+ lo, hi = min(xs), max(xs)
147
+ return [0.0] * len(xs) if hi <= lo else [(x - lo) / (hi - lo) for x in xs]
148
+ bn, en = norm(bm), norm(emb)
149
+ scores = [alpha * e + (1 - alpha) * b for b, e in zip(bn, en)]
150
+ order = sorted(range(len(scores)), key=lambda i: scores[i], reverse=True)[:k]
151
+ out: List[MemoryItem] = []
152
+ for idx in order:
153
+ item = self.items[idx]
154
+ item.hits += 1
155
+ item.last_access = now
156
+ out.append(item)
157
+ return out
158
+
159
+ def retrieve_bm25(self, cue: str, now: float, k: int = 3) -> List[MemoryItem]:
160
+ from .bm25 import BM25
161
+
162
+ if not cue or not self.items:
163
+ return []
164
+ ranked = BM25([i.text for i in self.items]).rank(cue, k=k)
165
+ out: List[MemoryItem] = []
166
+ for idx in ranked:
167
+ item = self.items[idx]
168
+ item.hits += 1
169
+ item.last_access = now
170
+ out.append(item)
171
+ return out
172
+
173
+ def retrieve(self, cue: str, now: float) -> Optional[MemoryItem]:
174
+ if getattr(self.cfg, "memory_retrieval", "bm25") == "bm25":
175
+ items = self.retrieve_bm25(cue, now, k=1)
176
+ return items[0] if items else None
177
+ ranked = self._rank(cue, now)
178
+ if not ranked or ranked[0][0] <= 0.05:
179
+ return None
180
+ item = ranked[0][1]
181
+ item.hits += 1
182
+ item.last_access = now
183
+ return item
184
+
185
+ def consolidate(self, now: float) -> None:
186
+ for item in self.items:
187
+ item.importance = clip(item.importance * 0.995, 0.0, 1.0)
188
+ self.items.sort(key=lambda i: i.importance, reverse=True)
189
+ self.items = self.items[: self.cfg.memory_capacity]
190
+
191
+ def snapshot(self) -> List[Dict[str, object]]:
192
+ return [
193
+ {
194
+ "text": i.text,
195
+ "importance": round(i.importance, 3),
196
+ "hits": i.hits,
197
+ "t": round(i.t, 1),
198
+ "last_access": round(i.last_access, 1),
199
+ }
200
+ for i in self.items
201
+ ]
@@ -0,0 +1,158 @@
1
+ """Unified memory framework.
2
+
3
+ Layers:
4
+ working memory -> MemoryStore (recent diary/impressions)
5
+ episodic -> LongTermMemory.episodes
6
+ semantic/fact -> LongTermMemory.facts
7
+ consolidated -> LongTermMemory.notes
8
+ vectors -> SQLite sidecar
9
+
10
+ Read: working + long-term hybrid recall.
11
+ Write: episode + fact extraction + consolidation.
12
+ """
13
+ from __future__ import annotations
14
+
15
+ from dataclasses import dataclass, field
16
+ import time
17
+ from typing import Dict, Optional
18
+
19
+ from .longterm_memory import LongTermMemory
20
+ from .scoped_identity import PublicDigest
21
+
22
+
23
+ @dataclass
24
+ class MemoryConfig:
25
+ working_limit: int = 4
26
+ recall_k: int = 3
27
+ summary_trigger_rounds: int = 20
28
+ fact_extract_interval: int = 5
29
+
30
+
31
+ @dataclass
32
+ class MemoryContext:
33
+ working: str = ""
34
+ long_term: str = ""
35
+ facts: list = field(default_factory=list)
36
+ open_loops: list = field(default_factory=list)
37
+ public_digest: str = ""
38
+ trace: dict = field(default_factory=dict)
39
+ query: str = ""
40
+
41
+ def as_text(self) -> str:
42
+ parts = []
43
+ if self.working:
44
+ parts.append("最近:\n" + self.working)
45
+ if self.long_term:
46
+ parts.append("长期记忆:\n" + self.long_term)
47
+ if self.open_loops:
48
+ parts.append("未闭环:\n" + "\n".join("- " + str(x) for x in self.open_loops))
49
+ if self.public_digest:
50
+ parts.append("群公共记忆:\n" + self.public_digest)
51
+ return "\n".join(parts)
52
+
53
+
54
+ class MemoryFramework:
55
+ def __init__(self, working_store, longterm: LongTermMemory,
56
+ user_id: str = "web", config: Optional[MemoryConfig] = None,
57
+ summarizer=None, digest: Optional[PublicDigest] = None, scope: str = "private"):
58
+ self.working = working_store
59
+ self.longterm = longterm
60
+ self.user_id = str(user_id or "web")
61
+ self.cfg = config or MemoryConfig()
62
+ self.summarizer = summarizer
63
+ self.digest = digest
64
+ self.scope = str(scope or "private")
65
+ self.turns = 0
66
+ self._pending_texts = []
67
+
68
+ def recall(self, query: str, affect_v: float = 0.0,
69
+ recent_context: str = "", k: Optional[int] = None) -> MemoryContext:
70
+ working = ""
71
+ try:
72
+ working = self.working.recall(self.user_id, limit=self.cfg.working_limit)
73
+ except Exception:
74
+ working = ""
75
+ ltm = ""
76
+ try:
77
+ ltm = self.longterm.recall(
78
+ str(query or ""), {"v": float(affect_v)}, k=int(k or self.cfg.recall_k),
79
+ recent_context=recent_context)
80
+ except Exception:
81
+ ltm = ""
82
+ facts = [x.summary for x in self.longterm.facts[-5:]]
83
+ loops = [x.summary for x in self.longterm.open_loops(5)]
84
+ trace = dict(getattr(self.longterm, "last_trace", {}) or {})
85
+ digest_text = ""
86
+ if self.digest is not None and self.scope == "private":
87
+ try:
88
+ digest_text = self.digest.read("", limit=3)
89
+ except Exception:
90
+ digest_text = ""
91
+ return MemoryContext(working=working, long_term=ltm, facts=facts, open_loops=loops,
92
+ public_digest=digest_text, trace=trace, query=str(query or ""))
93
+
94
+ def observe(self, text: str, reply: str, emotion: str,
95
+ affect_v: float = 0.0, reward: float = 0.5) -> Dict[str, object]:
96
+ self.turns += 1
97
+ out: Dict[str, object] = {"episode": False, "facts": 0, "llm_facts": 0,
98
+ "consolidated": False, "closed_loops": [], "llm_closed": []}
99
+ try:
100
+ self.longterm.add_episode(str(text), str(reply), str(emotion),
101
+ importance=max(0.3, float(reward)),
102
+ valence=float(affect_v))
103
+ out["episode"] = True
104
+ except Exception:
105
+ pass
106
+ try:
107
+ for fact in self.longterm.extract_facts(str(text)):
108
+ self.longterm.add_fact(fact)
109
+ out["facts"] = int(out["facts"]) + 1
110
+ except Exception:
111
+ pass
112
+ # periodic LLM fact extraction: regex above is the immediate fallback
113
+ try:
114
+ self._pending_texts.append(str(text))
115
+ self._pending_texts = self._pending_texts[-8:]
116
+ interval = max(1, int(self.cfg.fact_extract_interval))
117
+ if self.summarizer is not None and self.turns % interval == 0:
118
+ extracted = []
119
+ if hasattr(self.summarizer, "extract_memory"):
120
+ loops = [x.summary for x in self.longterm.open_loops(5)]
121
+ res = self.summarizer.extract_memory(self._pending_texts, loops or [])
122
+ extracted = list((res or {}).get("facts") or [])
123
+ for name in (res or {}).get("closed_loops") or []:
124
+ if self.longterm.close_open_loop(str(name)):
125
+ out["llm_closed"] = list(out["llm_closed"]) + [str(name)]
126
+ elif hasattr(self.summarizer, "extract_facts"):
127
+ extracted = self.summarizer.extract_facts(self._pending_texts) or []
128
+ for f in extracted:
129
+ days = float(f.get("expires_in_days") or 0.0)
130
+ self.longterm.add_fact(str(f.get("text") or ""), key=str(f.get("key") or ""),
131
+ expires_at=time.time() + days * 86400 if days > 0 else 0.0)
132
+ out["llm_facts"] = int(out["llm_facts"]) + 1
133
+ self._pending_texts = []
134
+ except Exception:
135
+ pass
136
+ try:
137
+ out["closed_loops"] = self.longterm.close_matching(str(text))
138
+ except Exception:
139
+ out["closed_loops"] = []
140
+ if self.turns % max(1, int(self.cfg.summary_trigger_rounds)) == 0:
141
+ try:
142
+ self.longterm.consolidate(summarizer=self.summarizer)
143
+ out["consolidated"] = True
144
+ except Exception:
145
+ pass
146
+ return out
147
+
148
+ def consolidate_now(self):
149
+ try:
150
+ return self.longterm.consolidate(summarizer=self.summarizer)
151
+ except Exception:
152
+ return None
153
+
154
+ def stats(self) -> Dict[str, object]:
155
+ st = dict(self.longterm.stats())
156
+ st["turns"] = self.turns
157
+ st["user_id"] = self.user_id
158
+ return st