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,422 @@
1
+ """Learned proactive-intervention policy (no hand-written decision rules).
2
+
3
+ Why this module exists
4
+ ----------------------
5
+ The previous proactive path was gated by hand-written rules (quiet hours,
6
+ daily caps, cooldowns, fixed thresholds, ignored-times-a-row pause). That
7
+ produced spam and could not adapt: on 2026-10-20 the bot sent 21 unsolicited
8
+ private messages after the user explicitly asked it to stop.
9
+
10
+ Papers this design is based on
11
+ ------------------------------
12
+ * An, T. (2026). When Should the Agent Speak? A Survey of Intervention
13
+ Timing for Always-On AI Assistants (TMLR). Decision rule: intervene iff
14
+ expected benefit of acting > expected cost of interrupting; the survey
15
+ argues intervention timing should be learned online by randomizing decision
16
+ points against a validated proximal outcome. Here benefit and cost are
17
+ both learned from observed outcomes; there is no fixed timing heuristic.
18
+ * Liu et al. (CHI 25). Proactive Conversational Agents with Inner Thoughts:
19
+ proactivity comes from generated inner thoughts evaluated by an intrinsic
20
+ motivation, not from next-speaker prediction. The policy scores a latent
21
+ motivation to speak from the agent state and only then generates text.
22
+ * Houde et al. (IUI 25). Controlling AI Agent Participation in Group
23
+ Conversations (Koala): imperative decision logic for valuable contribution
24
+ is brittle; users want control over when/what/where the agent responds.
25
+ Here the policy is learned per user from outcome feedback instead of rules.
26
+ * Rizk et al. (2020). A Snooze-less User-Aware Notification System for
27
+ Proactive Conversational Agents (arXiv:2003.02097): alert fatigue is a
28
+ learning problem; learn from a wide set of users and customise per user.
29
+ Here a global model seeds every per-user model.
30
+ * When2Speak (duke-trust-lab): dataset of speak/silence decisions; local copy
31
+ in mindbase/data/real/When2Speak is the reference task definition.
32
+ * Li et al. (2010). LinUCB contextual bandit: linear reward model with an
33
+ upper-confidence exploration term. This module is a numpy-free variant.
34
+
35
+ Design
36
+ ------
37
+ For each user we keep a linear reward model theta = A^-1 b and an exploration
38
+ term alpha * sqrt(x^T A^-1 x). The predicted utility of initiating is the
39
+ learned expected reward of speaking now in this context; the alternative
40
+ action stay silent has utility 0. The agent initiates only when the learned
41
+ upper confidence bound beats 0 (the survey benefit/cost test with both terms
42
+ learned). Every observed outcome updates A and b; a population model is
43
+ updated with the same observations and seeds new users.
44
+
45
+ No keyword lists, quiet-hour rules, daily caps or fixed cooldowns live here.
46
+ Time of day, silence length, recent send count, acceptance history,
47
+ fear/joy/expectation, social deficit, sentiment, stress and latent impulse are
48
+ features; their weights are learned from outcomes.
49
+ """
50
+ from __future__ import annotations
51
+
52
+ import hashlib
53
+ import json
54
+ import math
55
+ import threading
56
+ import time
57
+ from pathlib import Path
58
+ from typing import Dict, List, Optional, Sequence
59
+
60
+ FEATURE_NAMES: List[str] = [
61
+ "bias",
62
+ "silence",
63
+ "since_proactive",
64
+ "sends_today",
65
+ "accept_rate",
66
+ "expectation",
67
+ "fear",
68
+ "joy",
69
+ "settle_horizon",
70
+ "social_deficit",
71
+ "unfinished",
72
+ "user_activity",
73
+ "sentiment",
74
+ "reply_latency",
75
+ "valence",
76
+ "stress",
77
+ "hour_sin",
78
+ "hour_cos",
79
+ "impulse",
80
+ ]
81
+
82
+ N_FEATURES = len(FEATURE_NAMES)
83
+ _DEFAULT_SINCE_PROACTIVE = 11.0 * 60.0
84
+
85
+
86
+ def _clamp(v: float, lo: float, hi: float) -> float:
87
+ try:
88
+ x = float(v)
89
+ except Exception:
90
+ return lo
91
+ return lo if x < lo else hi if x > hi else x
92
+
93
+
94
+ def context_features(m: Dict[str, float]) -> List[float]:
95
+ """Turn raw context metrics into the policy feature vector.
96
+
97
+ Log/linear scalings are feature engineering, not decision rules: every
98
+ weight that multiplies them is learned online.
99
+ """
100
+ hour = float(m.get("hour", 0.0))
101
+ return [
102
+ 1.0,
103
+ min(math.log1p(max(0.0, float(m.get("silence_sec", 0.0)))) / 8.0, 1.0),
104
+ min(math.log1p(max(0.0, float(m.get("since_proactive_sec", _DEFAULT_SINCE_PROACTIVE)))) / 11.0, 1.0),
105
+ min(max(0.0, float(m.get("sends_today", 0.0))) / 5.0, 1.0),
106
+ _clamp(m.get("accept_rate", 0.5), 0.0, 1.0),
107
+ _clamp(m.get("expectation", 0.5), 0.0, 1.0),
108
+ _clamp(m.get("fear", 0.0), 0.0, 1.0),
109
+ _clamp(m.get("joy", 0.0), 0.0, 1.0),
110
+ min(math.log1p(max(0.0, float(m.get("settle_after", 300.0)))) / 8.0, 1.0),
111
+ _clamp(m.get("social_deficit", 0.0), 0.0, 1.0),
112
+ _clamp(m.get("unfinished", 0.0), 0.0, 1.0),
113
+ min(max(0.0, float(m.get("user_msgs_today", 0.0))) / 10.0, 1.0),
114
+ _clamp(m.get("sentiment", 0.0), -1.0, 1.0),
115
+ min(math.log1p(max(0.0, float(m.get("delay_mean", 300.0)))) / 8.0, 1.0),
116
+ _clamp(m.get("valence", 0.0), -1.0, 1.0),
117
+ _clamp(m.get("stress", 0.0), 0.0, 1.0),
118
+ math.sin(2.0 * math.pi * (hour % 24.0) / 24.0),
119
+ math.cos(2.0 * math.pi * (hour % 24.0) / 24.0),
120
+ _clamp(m.get("impulse", 0.0), 0.0, 1.0),
121
+ ]
122
+
123
+
124
+ def _matvec(a: Sequence[Sequence[float]], x: Sequence[float]) -> List[float]:
125
+ return [sum(a[i][j] * x[j] for j in range(len(x))) for i in range(len(a))]
126
+
127
+
128
+ def _dot(a: Sequence[float], b: Sequence[float]) -> float:
129
+ return sum(x * y for x, y in zip(a, b))
130
+
131
+
132
+ def _inverse(matrix: Sequence[Sequence[float]]) -> List[List[float]]:
133
+ """Gauss-Jordan inverse with partial pivoting (numpy-free, n=19)."""
134
+ n = len(matrix)
135
+ a = [[float(v) for v in row] + [1.0 if i == j else 0.0 for j in range(n)] for i, row in enumerate(matrix)]
136
+ for col in range(n):
137
+ pivot = max(range(col, n), key=lambda r: abs(a[r][col]))
138
+ if abs(a[pivot][col]) < 1e-12:
139
+ a[pivot][col] = 1e-12
140
+ if pivot != col:
141
+ a[col], a[pivot] = a[pivot], a[col]
142
+ pv = a[col][col]
143
+ a[col] = [v / pv for v in a[col]]
144
+ for r in range(n):
145
+ if r == col:
146
+ continue
147
+ f = a[r][col]
148
+ if f == 0.0:
149
+ continue
150
+ a[r] = [a[r][j] - f * a[col][j] for j in range(2 * n)]
151
+ return [row[n:] for row in a]
152
+
153
+
154
+ class ProactivePolicy:
155
+ """Per-user online-learned proactive timing policy (LinUCB-style)."""
156
+
157
+ def __init__(self, root: Path | str, user_id: str, prior_reward: float = -0.5,
158
+ alpha: float = 0.20, ridge: float = 1.0, log_path: Path | str | None = None):
159
+ self.root = Path(root)
160
+ self.root.mkdir(parents=True, exist_ok=True)
161
+ self.user_id = str(user_id)
162
+ self.alpha = float(alpha)
163
+ self.ridge = float(ridge)
164
+ self.log_path = Path(log_path) if log_path else None
165
+ self._lock = threading.RLock()
166
+ self._Ainv: Optional[List[List[float]]] = None
167
+ self._load(prior_reward)
168
+
169
+ # ---------------------------------------------------------------- storage
170
+ def _path(self) -> Path:
171
+ key = hashlib.sha256(self.user_id.encode("utf-8")).hexdigest()[:16]
172
+ return self.root / (key + ".json")
173
+
174
+ def _global_path(self) -> Path:
175
+ return self.root / "_global.json"
176
+
177
+ def _blank(self, prior_reward: float) -> Dict[str, object]:
178
+ A = [[self.ridge if i == j else 0.0 for j in range(N_FEATURES)] for i in range(N_FEATURES)]
179
+ b = [0.0] * N_FEATURES
180
+ b[0] = float(prior_reward)
181
+ return {
182
+ "user_id": self.user_id, "A": A, "b": b, "alpha": self.alpha,
183
+ "n": 0, "sent": 0, "accepted": 0, "ignored": 0, "reward_sum": 0.0,
184
+ "last_send_ts": 0.0, "send_times": [], "user_msgs_date": "",
185
+ "user_msgs_today": 0, "sentiment_ema": 0.0, "pending": None,
186
+ "recent_texts": [], "last_update": 0.0,
187
+ }
188
+
189
+ def _load(self, prior_reward: float) -> None:
190
+ path = self._path()
191
+ data = None
192
+ if path.exists():
193
+ try:
194
+ data = json.loads(path.read_text(encoding="utf-8"))
195
+ except Exception:
196
+ data = None
197
+ if not isinstance(data, dict):
198
+ data = self._blank(prior_reward)
199
+ gp = self._global_path()
200
+ if gp.exists():
201
+ try:
202
+ g = json.loads(gp.read_text(encoding="utf-8"))
203
+ if isinstance(g.get("A"), list) and isinstance(g.get("b"), list) and len(g["b"]) == N_FEATURES:
204
+ data["A"] = [list(row) for row in g["A"]]
205
+ data["b"] = list(g["b"])
206
+ data["b"][0] = float(prior_reward)
207
+ except Exception:
208
+ pass
209
+ base = self._blank(prior_reward)
210
+ for k, v in data.items():
211
+ if k in ("A", "b"):
212
+ continue
213
+ base[k] = v
214
+ base["A"] = data.get("A") or base["A"]
215
+ base["b"] = data.get("b") or base["b"]
216
+ self.data: Dict[str, object] = base
217
+ self._Ainv = None
218
+
219
+ def save(self) -> None:
220
+ with self._lock:
221
+ try:
222
+ self._path().write_text(json.dumps(self.data, ensure_ascii=False), encoding="utf-8")
223
+ except Exception:
224
+ pass
225
+
226
+ def _update_global(self, x: Sequence[float], reward: float) -> None:
227
+ gp = self._global_path()
228
+ try:
229
+ current = json.loads(gp.read_text(encoding="utf-8")) if gp.exists() else None
230
+ except Exception:
231
+ current = None
232
+ if not isinstance(current, dict) or not isinstance(current.get("A"), list):
233
+ current = {"A": [[self.ridge if i == j else 0.0 for j in range(N_FEATURES)] for i in range(N_FEATURES)],
234
+ "b": [0.0] * N_FEATURES, "n": 0}
235
+ try:
236
+ A = current["A"]
237
+ b = current["b"]
238
+ for i in range(N_FEATURES):
239
+ xi = float(x[i])
240
+ b[i] = float(b[i]) + reward * xi
241
+ row = A[i]
242
+ for j in range(N_FEATURES):
243
+ row[j] = float(row[j]) + xi * float(x[j])
244
+ current["n"] = int(current.get("n", 0)) + 1
245
+ gp.write_text(json.dumps(current, ensure_ascii=False), encoding="utf-8")
246
+ except Exception:
247
+ pass
248
+
249
+ # -------------------------------------------------------------- utilities
250
+ def sends_today(self, now: float) -> int:
251
+ day = time.strftime("%Y-%m-%d", time.localtime(now))
252
+ return sum(1 for t in self.data.get("send_times", [])
253
+ if time.strftime("%Y-%m-%d", time.localtime(float(t))) == day)
254
+
255
+ def user_msgs_today(self, now: float) -> int:
256
+ day = time.strftime("%Y-%m-%d", time.localtime(now))
257
+ if str(self.data.get("user_msgs_date", "")) != day:
258
+ return 0
259
+ return int(self.data.get("user_msgs_today", 0))
260
+
261
+ @property
262
+ def sentiment_ema(self) -> float:
263
+ return float(self.data.get("sentiment_ema", 0.0))
264
+
265
+ @property
266
+ def pending(self) -> Optional[Dict[str, object]]:
267
+ return self.data.get("pending") if isinstance(self.data.get("pending"), dict) else None
268
+
269
+ @property
270
+ def last_send_ts(self) -> float:
271
+ return float(self.data.get("last_send_ts", 0.0))
272
+
273
+ @property
274
+ def recent_texts(self) -> List[str]:
275
+ return [str(x) for x in self.data.get("recent_texts", [])][-5:]
276
+
277
+ def _theta(self) -> List[float]:
278
+ if self._Ainv is None:
279
+ self._Ainv = _inverse(self.data["A"])
280
+ return _matvec(self._Ainv, self.data["b"])
281
+
282
+ def expected_value(self, x: Sequence[float]) -> float:
283
+ """Learned expected reward of initiating in this context."""
284
+ with self._lock:
285
+ return float(_dot(self._theta(), x))
286
+
287
+ def score(self, x: Sequence[float]) -> Dict[str, float]:
288
+ """Learned upper confidence bound used for the initiate / stay-silent decision."""
289
+ with self._lock:
290
+ if self._Ainv is None:
291
+ self._Ainv = _inverse(self.data["A"])
292
+ Ainv = self._Ainv
293
+ theta = _matvec(Ainv, self.data["b"])
294
+ mean = _dot(theta, x)
295
+ var = _dot(x, _matvec(Ainv, x))
296
+ bonus = float(self.alpha) * math.sqrt(max(0.0, float(var)))
297
+ return {"mean": float(mean), "bonus": float(bonus), "score": float(mean + bonus)}
298
+
299
+ def should_initiate(self, x: Sequence[float]) -> Dict[str, object]:
300
+ s = self.score(x)
301
+ return {"decide": bool(s["score"] > 0.0), **s}
302
+
303
+ # ---------------------------------------------------------------- learning
304
+ def learn(self, x: Sequence[float], reward: float) -> Dict[str, float]:
305
+ """Online update from one observed outcome (user model + population prior)."""
306
+ reward = _clamp(reward, -2.0, 1.0)
307
+ with self._lock:
308
+ A = self.data["A"]
309
+ b = self.data["b"]
310
+ for i in range(N_FEATURES):
311
+ xi = float(x[i])
312
+ b[i] = float(b[i]) + reward * xi
313
+ row = A[i]
314
+ for j in range(N_FEATURES):
315
+ row[j] = float(row[j]) + xi * float(x[j])
316
+ self.data["n"] = int(self.data.get("n", 0)) + 1
317
+ self.data["reward_sum"] = float(self.data.get("reward_sum", 0.0)) + reward
318
+ if reward > 0.0:
319
+ self.data["accepted"] = int(self.data.get("accepted", 0)) + 1
320
+ elif reward < 0.0:
321
+ self.data["ignored"] = int(self.data.get("ignored", 0)) + 1
322
+ self._Ainv = None
323
+ self.data["last_update"] = time.time()
324
+ self.save()
325
+ self._update_global(x, reward)
326
+ self._log({"event": "learn", "ts": time.time(), "reward": reward,
327
+ "n": int(self.data["n"]), "features": [float(v) for v in x]})
328
+ return {"reward": reward, "n": int(self.data["n"])}
329
+
330
+ def note_user_message(self, sentiment: float, now: float) -> Dict[str, float]:
331
+ """Record a user-initiated message (engagement outcome) and update the EMA feature."""
332
+ sentiment = _clamp(sentiment, -1.0, 1.0)
333
+ with self._lock:
334
+ ema = float(self.data.get("sentiment_ema", 0.0))
335
+ self.data["sentiment_ema"] = 0.7 * ema + 0.3 * sentiment
336
+ day = time.strftime("%Y-%m-%d", time.localtime(now))
337
+ if str(self.data.get("user_msgs_date", "")) != day:
338
+ self.data["user_msgs_date"] = day
339
+ self.data["user_msgs_today"] = 0
340
+ self.data["user_msgs_today"] = int(self.data.get("user_msgs_today", 0)) + 1
341
+ self.save()
342
+ return {"sentiment_ema": float(self.data.get("sentiment_ema", 0.0))}
343
+
344
+ def record_send(self, x: Sequence[float], text: str, now: float, hour: int, topic: str = "") -> None:
345
+ with self._lock:
346
+ self.data["pending"] = {"x": [float(v) for v in x], "text": str(text), "ts": float(now),
347
+ "hour": int(hour), "topic": str(topic)}
348
+ self.data["last_send_ts"] = float(now)
349
+ times = [float(t) for t in self.data.get("send_times", [])][-99:]
350
+ times.append(float(now))
351
+ self.data["send_times"] = times
352
+ self.data["sent"] = int(self.data.get("sent", 0)) + 1
353
+ recent = [str(t) for t in self.data.get("recent_texts", [])][-4:]
354
+ recent.append(str(text)[:200])
355
+ self.data["recent_texts"] = recent
356
+ self.save()
357
+ self._log({"event": "send", "ts": float(now), "text": str(text), "hour": int(hour),
358
+ "topic": str(topic), "score": self.score(x)["score"],
359
+ "features": [float(v) for v in x]})
360
+
361
+ def settle_timeout(self, now: float, horizon: float) -> Optional[Dict[str, object]]:
362
+ """If the pending initiative passed the learned wait horizon, treat it as ignored."""
363
+ with self._lock:
364
+ pending = self.pending
365
+ if pending is None:
366
+ return None
367
+ age = float(now) - float(pending.get("ts", now))
368
+ if horizon > 0 and age < float(horizon):
369
+ return None
370
+ x = pending.get("x") or []
371
+ self.data["pending"] = None
372
+ reward = -1.0
373
+ self.learn(x, reward)
374
+ out = {"reason": "timeout", "reward": reward, "age": age, "delay": age,
375
+ "hour": int(pending.get("hour", 0)), "topic": str(pending.get("topic", "")),
376
+ "text": str(pending.get("text", ""))}
377
+ self._log({"event": "outcome", **out})
378
+ return out
379
+
380
+ def settle_reply(self, sentiment: float, now: float, horizon: float) -> Optional[Dict[str, object]]:
381
+ """Settle a pending initiative with the user reply."""
382
+ with self._lock:
383
+ pending = self.pending
384
+ if pending is None:
385
+ return None
386
+ age = float(now) - float(pending.get("ts", now))
387
+ x = pending.get("x") or []
388
+ self.data["pending"] = None
389
+ s = _clamp(sentiment, -1.0, 1.0)
390
+ if horizon > 0 and age > float(horizon):
391
+ reward = s - 0.5
392
+ else:
393
+ reward = s + 0.5
394
+ reward = _clamp(reward, -2.0, 1.0)
395
+ self.learn(x, reward)
396
+ out = {"reason": "reply", "reward": reward, "age": age, "delay": age,
397
+ "hour": int(pending.get("hour", 0)), "topic": str(pending.get("topic", "")),
398
+ "text": str(pending.get("text", "")), "sentiment": s}
399
+ self._log({"event": "outcome", **out})
400
+ return out
401
+
402
+ # ------------------------------------------------------------------ misc
403
+ def _log(self, row: Dict[str, object]) -> None:
404
+ if self.log_path is None:
405
+ return
406
+ try:
407
+ self.log_path.parent.mkdir(parents=True, exist_ok=True)
408
+ with self.log_path.open("a", encoding="utf-8") as f:
409
+ f.write(json.dumps({"user": self.user_id, **row}, ensure_ascii=False) + "\n")
410
+ except Exception:
411
+ pass
412
+
413
+ def summary(self) -> Dict[str, object]:
414
+ return {"n": int(self.data.get("n", 0)), "sent": int(self.data.get("sent", 0)),
415
+ "accepted": int(self.data.get("accepted", 0)), "ignored": int(self.data.get("ignored", 0)),
416
+ "reward_sum": round(float(self.data.get("reward_sum", 0.0)), 4),
417
+ "alpha": self.alpha, "pending": bool(self.pending)}
418
+
419
+ def stats(self, now: float) -> Dict[str, object]:
420
+ return {"sent": int(self.data.get("sent", 0)), "accepted": int(self.data.get("accepted", 0)),
421
+ "ignored": int(self.data.get("ignored", 0)), "delay_sum": 0.0,
422
+ "policy_n": int(self.data.get("n", 0))}
@@ -0,0 +1,196 @@
1
+ from __future__ import annotations
2
+
3
+ import hashlib
4
+ import json
5
+ from pathlib import Path
6
+ from typing import Dict
7
+
8
+
9
+ class ProactiveProfile:
10
+ """Per-user Beta-Bernoulli acceptance model by hour and topic."""
11
+
12
+ ALPHA = 2.0
13
+ BETA = 2.0
14
+
15
+ def __init__(self, root: Path, user_id: str):
16
+ self.root = Path(root)
17
+ self.root.mkdir(parents=True, exist_ok=True)
18
+ self.user_id = str(user_id)
19
+ self.data = self._load()
20
+
21
+ def _path(self) -> Path:
22
+ key = hashlib.sha256(self.user_id.encode("utf-8")).hexdigest()[:16]
23
+ return self.root / (key + ".json")
24
+
25
+ def _blank(self) -> Dict[str, object]:
26
+ return {"hours": {}, "topics": {}, "sent": 0, "accepted": 0, "ignored": 0,
27
+ "expectation": 0.5, "fear": 0.0, "joy": 0.0, "last_update": 0.0,
28
+ "refractory_scale": 1.0, "delay_mean": 300.0, "delay_n": 0, "settle_scale": 1.0}
29
+
30
+ def _load(self) -> Dict[str, object]:
31
+ path = self._path()
32
+ if not path.exists():
33
+ return self._blank()
34
+ try:
35
+ data = json.loads(path.read_text(encoding="utf-8"))
36
+ base = self._blank()
37
+ base.update(data if isinstance(data, dict) else {})
38
+ return base
39
+ except Exception:
40
+ return self._blank()
41
+
42
+ def decay(self, now: float) -> None:
43
+ """Apply time decay to learned fear/joy and persist the clock.
44
+
45
+ The previous implementation mutated last_update but never saved it, so
46
+ a reloaded profile always believed it had just been created and the
47
+ learned fear never decayed. Saving here fixes that.
48
+ """
49
+ try:
50
+ last = float(self.data.get("last_update", 0.0) or 0.0)
51
+ now = float(now)
52
+ if last <= 0:
53
+ self.data["last_update"] = now
54
+ self.save()
55
+ return
56
+ hours = max(0.0, (now - last) / 3600.0)
57
+ if hours * 3600.0 < 30.0:
58
+ return
59
+ self.data["fear"] = max(0.0, float(self.data.get("fear", 0.0)) * (2.718281828 ** (-hours / 12.0)))
60
+ self.data["joy"] = max(0.0, float(self.data.get("joy", 0.0)) * (2.718281828 ** (-hours / 3.0)))
61
+ self.data["last_update"] = now
62
+ self.save()
63
+ except Exception:
64
+ pass
65
+
66
+ def settle_after(self) -> float:
67
+ mean_delay = float(self.data.get("delay_mean", 300.0))
68
+ scale = float(self.data.get("settle_scale", 1.0))
69
+ return max(120.0, min(86400.0, mean_delay * scale))
70
+
71
+ def anticipation(self) -> float:
72
+ expectation = float(self.data.get("expectation", 0.5))
73
+ fear = float(self.data.get("fear", 0.0))
74
+ return round(max(0.0, min(1.0, fear * (1.0 - expectation))), 4)
75
+
76
+ def save(self) -> None:
77
+ self._path().write_text(json.dumps(self.data, ensure_ascii=False), encoding="utf-8")
78
+
79
+ def record(self, hour: int, topic: str, accepted: bool, delay: float = 0.0, sentiment: float = 0.0, anxiety: float = 0.0) -> None:
80
+ hour_key = str(int(hour) % 24)
81
+ hours = self.data.setdefault("hours", {})
82
+ cell = hours.setdefault(hour_key, {"a": self.ALPHA, "b": self.BETA})
83
+ topics = self.data.setdefault("topics", {})
84
+ topic_key = str(topic or "general")[:24]
85
+ tcell = topics.setdefault(topic_key, {"a": self.ALPHA, "b": self.BETA})
86
+ outcome = 1.0 if accepted and float(sentiment) >= 0 else 0.0
87
+ expectation = float(self.data.get("expectation", 0.5))
88
+ self.data["expectation"] = max(0.0, min(1.0, expectation + 0.2 * (outcome - expectation)))
89
+ ax = max(0.0, min(1.0, float(anxiety)))
90
+ if outcome > 0:
91
+ self.data["fear"] = max(0.0, float(self.data.get("fear", 0.0)) - 0.05 * (1.0 + ax))
92
+ self.data["joy"] = min(1.0, float(self.data.get("joy", 0.0)) + 0.10 * (1.0 + ax))
93
+ self.data["refractory_scale"] = max(0.5, float(self.data.get("refractory_scale", 1.0)) * 0.95)
94
+ n_delay = int(self.data.get("delay_n", 0))
95
+ mean_delay = float(self.data.get("delay_mean", 300.0))
96
+ mean_delay = (mean_delay * n_delay + max(0.0, float(delay))) / (n_delay + 1)
97
+ self.data["delay_mean"] = min(86400.0, mean_delay)
98
+ self.data["delay_n"] = n_delay + 1
99
+ self.data["settle_scale"] = max(0.5, float(self.data.get("settle_scale", 1.0)) * 0.95)
100
+ cell["a"] = float(cell.get("a", self.ALPHA)) + 1.0
101
+ tcell["a"] = float(tcell.get("a", self.ALPHA)) + 1.0
102
+ self.data["accepted"] = int(self.data.get("accepted", 0)) + 1
103
+ else:
104
+ self.data["fear"] = min(0.95, float(self.data.get("fear", 0.0)) + 0.10 * (1.0 + ax))
105
+ self.data["joy"] = max(0.0, float(self.data.get("joy", 0.0)) - 0.05)
106
+ factor = 1.5 if float(sentiment) < 0 else 1.1
107
+ self.data["refractory_scale"] = min(12.0, float(self.data.get("refractory_scale", 1.0)) * factor)
108
+ self.data["settle_scale"] = min(8.0, float(self.data.get("settle_scale", 1.0)) * 1.2)
109
+ cell["b"] = float(cell.get("b", self.BETA)) + 1.0
110
+ tcell["b"] = float(tcell.get("b", self.BETA)) + 1.0
111
+ self.data["ignored"] = int(self.data.get("ignored", 0)) + 1
112
+ self.data["sent"] = int(self.data.get("sent", 0)) + 1
113
+ self.save()
114
+
115
+ def observe(self, reward: float, hour: int, topic: str, delay: float = 0.0, now: float = 0.0) -> float:
116
+ """Learn from a continuous outcome reward (no accept/reject threshold).
117
+
118
+ Positive reward reinforces the hour/topic Beta cell; negative reward
119
+ adds failure mass. Expectation/fear/joy and the learned wait horizon
120
+ move by exponential moving averages driven only by observed rewards,
121
+ so the profile adapts to the user instead of following fixed rules.
122
+ """
123
+ try:
124
+ r = max(-2.0, min(1.0, float(reward)))
125
+ except Exception:
126
+ return 0.0
127
+ try:
128
+ hour_key = str(int(hour) % 24)
129
+ topic_key = str(topic or "general")[:24]
130
+ pos = max(0.0, r)
131
+ neg = max(0.0, -r)
132
+ hours = self.data.setdefault("hours", {})
133
+ cell = hours.setdefault(hour_key, {"a": self.ALPHA, "b": self.BETA})
134
+ cell["a"] = float(cell.get("a", self.ALPHA)) + pos
135
+ cell["b"] = float(cell.get("b", self.BETA)) + neg
136
+ topics = self.data.setdefault("topics", {})
137
+ tcell = topics.setdefault(topic_key, {"a": self.ALPHA, "b": self.BETA})
138
+ tcell["a"] = float(tcell.get("a", self.ALPHA)) + pos
139
+ tcell["b"] = float(tcell.get("b", self.BETA)) + neg
140
+ expectation = float(self.data.get("expectation", 0.5))
141
+ self.data["expectation"] = max(0.0, min(1.0, expectation + 0.15 * (r - expectation)))
142
+ fear = float(self.data.get("fear", 0.0))
143
+ joy = float(self.data.get("joy", 0.0))
144
+ self.data["fear"] = max(0.0, min(0.95, fear - 0.10 * pos + 0.12 * neg))
145
+ self.data["joy"] = max(0.0, min(1.0, joy + 0.10 * pos - 0.08 * neg))
146
+ settle = float(self.data.get("settle_scale", 1.0))
147
+ refractory = float(self.data.get("refractory_scale", 1.0))
148
+ if r > 0.0:
149
+ n_delay = int(self.data.get("delay_n", 0))
150
+ mean_delay = float(self.data.get("delay_mean", 300.0))
151
+ delay = max(0.0, min(86400.0, float(delay)))
152
+ self.data["delay_mean"] = (mean_delay * n_delay + delay) / (n_delay + 1)
153
+ self.data["delay_n"] = n_delay + 1
154
+ self.data["settle_scale"] = max(0.5, settle * 0.97)
155
+ self.data["refractory_scale"] = max(0.5, refractory * 0.97)
156
+ else:
157
+ self.data["settle_scale"] = min(8.0, settle * 1.05)
158
+ self.data["refractory_scale"] = min(12.0, refractory * 1.05)
159
+ self.data["sent"] = int(self.data.get("sent", 0)) + 1
160
+ if r > 0.0:
161
+ self.data["accepted"] = int(self.data.get("accepted", 0)) + 1
162
+ elif r < 0.0:
163
+ self.data["ignored"] = int(self.data.get("ignored", 0)) + 1
164
+ self.save()
165
+ return r
166
+ except Exception:
167
+ return r
168
+
169
+ def _rate(self, cell) -> float:
170
+ a = float(cell.get("a", self.ALPHA))
171
+ b = float(cell.get("b", self.BETA))
172
+ return a / max(1e-6, a + b)
173
+
174
+ def acceptance(self, hour: int, topic: str) -> float:
175
+ hours = self.data.get("hours", {})
176
+ topics = self.data.get("topics", {})
177
+ h = self._rate(hours.get(str(int(hour) % 24), {"a": self.ALPHA, "b": self.BETA}))
178
+ t = self._rate(topics.get(str(topic or "general")[:24], {"a": self.ALPHA, "b": self.BETA}))
179
+ return 0.7 * h + 0.3 * t
180
+
181
+ def threshold(self, hour: int, topic: str, base: float) -> float:
182
+ rate = self.acceptance(hour, topic)
183
+ adjusted = float(base) - 0.20 * (rate - 0.5)
184
+ return max(0.40, min(0.85, round(adjusted, 4)))
185
+
186
+ def summary(self) -> Dict[str, object]:
187
+ return {"sent": self.data.get("sent", 0), "accepted": self.data.get("accepted", 0),
188
+ "ignored": self.data.get("ignored", 0), "hours": self.data.get("hours", {}),
189
+ "topics": self.data.get("topics", {}),
190
+ "expectation": round(float(self.data.get("expectation", 0.5)), 4),
191
+ "anticipation": self.anticipation(),
192
+ "fear": round(float(self.data.get("fear", 0.0)), 4),
193
+ "joy": round(float(self.data.get("joy", 0.0)), 4),
194
+ "refractory_scale": round(float(self.data.get("refractory_scale", 1.0)), 4),
195
+ "delay_mean": round(float(self.data.get("delay_mean", 300.0)), 2),
196
+ "settle_after": round(self.settle_after(), 2)}
@@ -0,0 +1,28 @@
1
+ from __future__ import annotations
2
+
3
+ import math
4
+ from dataclasses import dataclass
5
+
6
+
7
+ @dataclass
8
+ class ProactiveDecision:
9
+ value: float
10
+ cost: float
11
+ score: float
12
+ speak: bool
13
+
14
+
15
+ class ProactiveTiming:
16
+ """Decision-theoretic proactive timing shadow. No message is sent."""
17
+
18
+ def __init__(self, threshold: float = 0.05):
19
+ self.threshold = float(threshold)
20
+
21
+ def decide(self, relevance: float, novelty: float, idle_seconds: float, stress: float) -> ProactiveDecision:
22
+ relevance = max(0.0, min(1.0, float(relevance)))
23
+ novelty = max(0.0, min(1.0, float(novelty)))
24
+ stress = max(0.0, min(1.0, float(stress)))
25
+ value = 0.6 * relevance + 0.4 * novelty
26
+ cost = 0.5 * math.exp(-max(0.0, float(idle_seconds)) / 600.0) + 0.3 * stress + 0.1
27
+ score = value - cost
28
+ return ProactiveDecision(value=round(value, 4), cost=round(cost, 4), score=round(score, 4), speak=score > self.threshold)