dsh-biocore 0.2.0 → 0.3.0

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 (245) hide show
  1. package/README.md +11 -0
  2. package/lib/core-worker.js +268 -0
  3. package/lib/index.js +160 -70
  4. package/lib/modules/autonomy.js +125 -0
  5. package/lib/modules/cache-stats.js +48 -0
  6. package/lib/modules/core-turn.js +24 -6
  7. package/lib/modules/expression-inject.js +0 -10
  8. package/lib/modules/expression-refresh.js +0 -9
  9. package/lib/modules/memory-capture.js +2 -1
  10. package/lib/modules/memory-inject.js +50 -5
  11. package/lib/modules/memory-tools.js +7 -4
  12. package/lib/modules/python-env.js +46 -0
  13. package/lib/modules/safety-gate.js +0 -3
  14. package/lib/modules/series.js +54 -0
  15. package/lib/modules/switch.js +16 -0
  16. package/lib/py-bridge.js +81 -15
  17. package/package.json +1 -1
  18. package/python/biocore_memory/core/config.py +138 -127
  19. package/python/biocore_memory/engine.py +2653 -2476
  20. package/python/biocore_memory/recall/embedding.py +129 -0
  21. package/python/biocore_memory/recall/feedback.py +202 -0
  22. package/python/biocore_model/preference_drift.py +255 -0
  23. package/python/biocore_model/tests/__init__.py +1 -0
  24. package/python/biocore_model/tests/core_mode_ui_check.mjs +63 -0
  25. package/python/biocore_model/tests/core_status_check.mjs +29 -0
  26. package/python/biocore_model/tests/dom_safe_check.mjs +22 -0
  27. package/python/biocore_model/tests/leak_samples_run.py +18 -0
  28. package/python/biocore_model/tests/orb_ring_check.mjs +20 -0
  29. package/python/biocore_model/tests/panel_html_check.mjs +63 -0
  30. package/python/biocore_model/tests/persona_flow_check.mjs +17 -0
  31. package/python/biocore_model/tests/render_guard_check.mjs +19 -0
  32. package/python/biocore_model/tests/switch_groups_check.mjs +94 -0
  33. package/python/biocore_model/tests/tabs_state_check.mjs +24 -0
  34. package/python/biocore_model/tests/test_affective_labor.py +102 -0
  35. package/python/biocore_model/tests/test_appraisal.py +137 -0
  36. package/python/biocore_model/tests/test_appraisal_action.py +55 -0
  37. package/python/biocore_model/tests/test_arch_convergence.py +35 -0
  38. package/python/biocore_model/tests/test_attachment_behavior.py +95 -0
  39. package/python/biocore_model/tests/test_attachment_regulation.py +63 -0
  40. package/python/biocore_model/tests/test_attachment_state.py +29 -0
  41. package/python/biocore_model/tests/test_attribution.py +56 -0
  42. package/python/biocore_model/tests/test_attributor.py +108 -0
  43. package/python/biocore_model/tests/test_autonomic.py +69 -0
  44. package/python/biocore_model/tests/test_autonomy_accumulator.py +84 -0
  45. package/python/biocore_model/tests/test_autonomy_affect.py +62 -0
  46. package/python/biocore_model/tests/test_autonomy_blame.py +44 -0
  47. package/python/biocore_model/tests/test_autonomy_caps.py +29 -0
  48. package/python/biocore_model/tests/test_autonomy_check.py +40 -0
  49. package/python/biocore_model/tests/test_autonomy_clingy.py +41 -0
  50. package/python/biocore_model/tests/test_autonomy_cooldown_selfcontact.py +138 -0
  51. package/python/biocore_model/tests/test_autonomy_frustration.py +42 -0
  52. package/python/biocore_model/tests/test_autonomy_hourly_limit.py +46 -0
  53. package/python/biocore_model/tests/test_autonomy_loop.py +64 -0
  54. package/python/biocore_model/tests/test_autonomy_mode.py +37 -0
  55. package/python/biocore_model/tests/test_autonomy_model.py +60 -0
  56. package/python/biocore_model/tests/test_autonomy_model_v2.py +68 -0
  57. package/python/biocore_model/tests/test_autonomy_module_text.py +104 -0
  58. package/python/biocore_model/tests/test_autonomy_params.py +163 -0
  59. package/python/biocore_model/tests/test_autonomy_reason.py +34 -0
  60. package/python/biocore_model/tests/test_autonomy_self.py +153 -0
  61. package/python/biocore_model/tests/test_autonomy_send_gate.py +38 -0
  62. package/python/biocore_model/tests/test_autonomy_silence.py +37 -0
  63. package/python/biocore_model/tests/test_autonomy_status.py +35 -0
  64. package/python/biocore_model/tests/test_autonomy_test_trigger.py +24 -0
  65. package/python/biocore_model/tests/test_autonomy_tick.py +148 -0
  66. package/python/biocore_model/tests/test_autonomy_v4.py +64 -0
  67. package/python/biocore_model/tests/test_autonomy_v5.py +43 -0
  68. package/python/biocore_model/tests/test_autonomy_v7.py +48 -0
  69. package/python/biocore_model/tests/test_autonomy_v_theta.py +85 -0
  70. package/python/biocore_model/tests/test_batch1_wiring.py +231 -0
  71. package/python/biocore_model/tests/test_batch2_wiring.py +231 -0
  72. package/python/biocore_model/tests/test_batch3_wiring.py +223 -0
  73. package/python/biocore_model/tests/test_bio_tick.py +202 -0
  74. package/python/biocore_model/tests/test_boundary.py +183 -0
  75. package/python/biocore_model/tests/test_boundary_gate.py +295 -0
  76. package/python/biocore_model/tests/test_brain_split.py +99 -0
  77. package/python/biocore_model/tests/test_co_recall.py +350 -0
  78. package/python/biocore_model/tests/test_commitment_extractor.py +197 -0
  79. package/python/biocore_model/tests/test_compassion_fatigue.py +84 -0
  80. package/python/biocore_model/tests/test_connection_bids.py +69 -0
  81. package/python/biocore_model/tests/test_core_bridge.py +88 -0
  82. package/python/biocore_model/tests/test_core_bridge_episode.py +14 -0
  83. package/python/biocore_model/tests/test_core_bridge_forget.py +17 -0
  84. package/python/biocore_model/tests/test_core_bridge_identity.py +15 -0
  85. package/python/biocore_model/tests/test_core_bridge_media.py +17 -0
  86. package/python/biocore_model/tests/test_core_bridge_relation.py +17 -0
  87. package/python/biocore_model/tests/test_core_bridge_summary.py +17 -0
  88. package/python/biocore_model/tests/test_core_client.py +156 -0
  89. package/python/biocore_model/tests/test_core_commands.py +146 -0
  90. package/python/biocore_model/tests/test_core_mode.py +161 -0
  91. package/python/biocore_model/tests/test_core_state.py +240 -0
  92. package/python/biocore_model/tests/test_criticality.py +61 -0
  93. package/python/biocore_model/tests/test_cross_session_copy.py +67 -0
  94. package/python/biocore_model/tests/test_debug_values.py +169 -0
  95. package/python/biocore_model/tests/test_defense_mechanisms.py +177 -0
  96. package/python/biocore_model/tests/test_delta_budget.py +157 -0
  97. package/python/biocore_model/tests/test_diag_trace.py +201 -0
  98. package/python/biocore_model/tests/test_directives_compat.py +698 -0
  99. package/python/biocore_model/tests/test_dissonance.py +97 -0
  100. package/python/biocore_model/tests/test_drift_sign.py +87 -0
  101. package/python/biocore_model/tests/test_embedding_fallback.py +277 -0
  102. package/python/biocore_model/tests/test_embedding_local.py +279 -0
  103. package/python/biocore_model/tests/test_embedding_mode.py +175 -0
  104. package/python/biocore_model/tests/test_embedding_modes.py +342 -0
  105. package/python/biocore_model/tests/test_emotion_geometry.py +114 -0
  106. package/python/biocore_model/tests/test_emotion_probe.py +214 -0
  107. package/python/biocore_model/tests/test_emotion_probe_concurrency.py +128 -0
  108. package/python/biocore_model/tests/test_entity_graph.py +198 -0
  109. package/python/biocore_model/tests/test_entity_resolve.py +446 -0
  110. package/python/biocore_model/tests/test_envy.py +173 -0
  111. package/python/biocore_model/tests/test_exit_or_silence.py +209 -0
  112. package/python/biocore_model/tests/test_expr_guard.py +1 -0
  113. package/python/biocore_model/tests/test_expr_intent.py +66 -0
  114. package/python/biocore_model/tests/test_expr_state.py +66 -0
  115. package/python/biocore_model/tests/test_expr_template.py +71 -0
  116. package/python/biocore_model/tests/test_expr_tone.py +41 -0
  117. package/python/biocore_model/tests/test_extreme_scenarios.py +147 -0
  118. package/python/biocore_model/tests/test_feedback_flow.py +185 -0
  119. package/python/biocore_model/tests/test_feedback_guard.py +65 -0
  120. package/python/biocore_model/tests/test_feedback_llm.py +59 -0
  121. package/python/biocore_model/tests/test_feedback_llm_inferred.py +140 -0
  122. package/python/biocore_model/tests/test_feedback_memory.py +63 -0
  123. package/python/biocore_model/tests/test_feedback_user.py +61 -0
  124. package/python/biocore_model/tests/test_frustration_curve.py +66 -0
  125. package/python/biocore_model/tests/test_global_bio_state.py +116 -0
  126. package/python/biocore_model/tests/test_governance.py +181 -0
  127. package/python/biocore_model/tests/test_granularity.py +133 -0
  128. package/python/biocore_model/tests/test_graph_extract.py +224 -0
  129. package/python/biocore_model/tests/test_graph_recall.py +226 -0
  130. package/python/biocore_model/tests/test_graph_sqlite.py +298 -0
  131. package/python/biocore_model/tests/test_growth.py +301 -0
  132. package/python/biocore_model/tests/test_intent_render.py +60 -0
  133. package/python/biocore_model/tests/test_intero_regulation.py +224 -0
  134. package/python/biocore_model/tests/test_intero_style.py +146 -0
  135. package/python/biocore_model/tests/test_item3_numeric.py +105 -0
  136. package/python/biocore_model/tests/test_item6_numeric.py +116 -0
  137. package/python/biocore_model/tests/test_layered_memory.py +174 -0
  138. package/python/biocore_model/tests/test_leak_guard.py +75 -0
  139. package/python/biocore_model/tests/test_learning_probe.py +93 -0
  140. package/python/biocore_model/tests/test_life_loop.py +222 -0
  141. package/python/biocore_model/tests/test_life_loop_10s.py +131 -0
  142. package/python/biocore_model/tests/test_local_embedding_health.py +258 -0
  143. package/python/biocore_model/tests/test_memory_affect_encoding.py +79 -0
  144. package/python/biocore_model/tests/test_memory_backend.py +332 -0
  145. package/python/biocore_model/tests/test_memory_dedup.py +316 -0
  146. package/python/biocore_model/tests/test_memory_dual.py +219 -0
  147. package/python/biocore_model/tests/test_memory_dual_wiring.py +219 -0
  148. package/python/biocore_model/tests/test_memory_feedback.py +241 -0
  149. package/python/biocore_model/tests/test_memory_fixes.py +84 -0
  150. package/python/biocore_model/tests/test_memory_forgetting.py +58 -0
  151. package/python/biocore_model/tests/test_memory_inject.py +211 -0
  152. package/python/biocore_model/tests/test_memory_mood_recall.py +118 -0
  153. package/python/biocore_model/tests/test_memory_store.py +372 -0
  154. package/python/biocore_model/tests/test_metacog.py +103 -0
  155. package/python/biocore_model/tests/test_migrate.py +245 -0
  156. package/python/biocore_model/tests/test_migrate_store.py +194 -0
  157. package/python/biocore_model/tests/test_mind_wander.py +65 -0
  158. package/python/biocore_model/tests/test_mood.py +35 -0
  159. package/python/biocore_model/tests/test_narrative.py +114 -0
  160. package/python/biocore_model/tests/test_needs_esteem.py +51 -0
  161. package/python/biocore_model/tests/test_needs_events.py +205 -0
  162. package/python/biocore_model/tests/test_needs_sleep.py +54 -0
  163. package/python/biocore_model/tests/test_noise_bank.py +54 -0
  164. package/python/biocore_model/tests/test_numeric_schema.py +98 -0
  165. package/python/biocore_model/tests/test_output_protocol.py +65 -0
  166. package/python/biocore_model/tests/test_page_debug.py +147 -0
  167. package/python/biocore_model/tests/test_page_status_autonomy.py +215 -0
  168. package/python/biocore_model/tests/test_pipeline.py +345 -0
  169. package/python/biocore_model/tests/test_pipeline_api.py +192 -0
  170. package/python/biocore_model/tests/test_pipeline_mode.py +222 -0
  171. package/python/biocore_model/tests/test_plugin_config.py +85 -0
  172. package/python/biocore_model/tests/test_policy_learning.py +116 -0
  173. package/python/biocore_model/tests/test_possible_selves.py +88 -0
  174. package/python/biocore_model/tests/test_predictor.py +227 -0
  175. package/python/biocore_model/tests/test_preference_drift.py +142 -0
  176. package/python/biocore_model/tests/test_preset.py +142 -0
  177. package/python/biocore_model/tests/test_proactive_context.py +70 -0
  178. package/python/biocore_model/tests/test_proactive_history.py +90 -0
  179. package/python/biocore_model/tests/test_proactive_memory.py +141 -0
  180. package/python/biocore_model/tests/test_proactive_send_record.py +88 -0
  181. package/python/biocore_model/tests/test_probe_contract.py +71 -0
  182. package/python/biocore_model/tests/test_prospective.py +154 -0
  183. package/python/biocore_model/tests/test_provider_fallback.py +89 -0
  184. package/python/biocore_model/tests/test_reconstruction.py +111 -0
  185. package/python/biocore_model/tests/test_regulation_strategies.py +228 -0
  186. package/python/biocore_model/tests/test_relation_stage.py +114 -0
  187. package/python/biocore_model/tests/test_relationship_stage.py +59 -0
  188. package/python/biocore_model/tests/test_release_140.py +55 -0
  189. package/python/biocore_model/tests/test_repair_after_boundary.py +249 -0
  190. package/python/biocore_model/tests/test_reply_limit.py +86 -0
  191. package/python/biocore_model/tests/test_reply_limit_persist.py +48 -0
  192. package/python/biocore_model/tests/test_reply_split.py +52 -0
  193. package/python/biocore_model/tests/test_reply_split_astrbot.py +66 -0
  194. package/python/biocore_model/tests/test_reply_split_inject.py +53 -0
  195. package/python/biocore_model/tests/test_rerank.py +229 -0
  196. package/python/biocore_model/tests/test_rollback.py +175 -0
  197. package/python/biocore_model/tests/test_runtime.py +175 -0
  198. package/python/biocore_model/tests/test_runtime_active.py +205 -0
  199. package/python/biocore_model/tests/test_runtime_pipeline.py +1583 -0
  200. package/python/biocore_model/tests/test_runtime_proactive.py +231 -0
  201. package/python/biocore_model/tests/test_scheduler.py +79 -0
  202. package/python/biocore_model/tests/test_secrecy.py +105 -0
  203. package/python/biocore_model/tests/test_self_esteem.py +45 -0
  204. package/python/biocore_model/tests/test_self_intent.py +347 -0
  205. package/python/biocore_model/tests/test_self_interest.py +183 -0
  206. package/python/biocore_model/tests/test_self_layer_wiring.py +198 -0
  207. package/python/biocore_model/tests/test_self_verification.py +113 -0
  208. package/python/biocore_model/tests/test_situation.py +142 -0
  209. package/python/biocore_model/tests/test_sleep_circadian.py +41 -0
  210. package/python/biocore_model/tests/test_social_pain.py +49 -0
  211. package/python/biocore_model/tests/test_social_repair.py +67 -0
  212. package/python/biocore_model/tests/test_social_theory.py +78 -0
  213. package/python/biocore_model/tests/test_source_monitoring.py +204 -0
  214. package/python/biocore_model/tests/test_speak_like_group.py +36 -0
  215. package/python/biocore_model/tests/test_stages.py +226 -0
  216. package/python/biocore_model/tests/test_state_tag.py +86 -0
  217. package/python/biocore_model/tests/test_template_expr.py +65 -0
  218. package/python/biocore_model/tests/test_text_inference.py +70 -0
  219. package/python/biocore_model/tests/test_theta_params.py +115 -0
  220. package/python/biocore_model/tests/test_topic_control.py +200 -0
  221. package/python/biocore_model/tests/test_turn_memory.py +105 -0
  222. package/python/biocore_model/tests/test_turn_memory_panel.py +174 -0
  223. package/python/biocore_model/tests/test_ui_settings.py +84 -0
  224. package/python/biocore_model/tests/test_ui_settings_authority.py +179 -0
  225. package/python/biocore_model/tests/test_user_appraisal_async.py +155 -0
  226. package/python/biocore_model/tests/test_user_classify.py +122 -0
  227. package/python/biocore_model/tests/test_value_order.py +117 -0
  228. package/python/biocore_model/tests/test_voice_bank.py +105 -0
  229. package/python/biocore_model/tests/test_waiting_sense.py +216 -0
  230. package/python/biocore_model/tests/test_x_directives.py +101 -0
  231. package/python/biocore_model/tests/ui_compat_check.mjs +22 -0
  232. package/python/biocore_model/tests/view_mode_check.mjs +17 -0
  233. package/python/biocore_runtime/autonomy.py +208 -0
  234. package/python/biocore_runtime/core_state.py +302 -0
  235. package/python/biocore_runtime/governance.py +495 -0
  236. package/python/biocore_runtime/growth.py +528 -0
  237. package/python/biocore_runtime/pipeline.py +850 -23
  238. package/python/biocore_runtime/relationship_state.py +214 -0
  239. package/python/biocore_runtime/self_layer.py +103 -0
  240. package/python/biocore_runtime/service.py +162 -0
  241. package/python/biocore_runtime/standby.py +472 -0
  242. package/ui/panel-layout.js +83 -39
  243. package/ui/panel.css +885 -743
  244. package/ui/panel.html +356 -296
  245. package/ui/panel.js +739 -865
@@ -0,0 +1,169 @@
1
+ # -*- coding: utf-8 -*-
2
+ """Debug-page regression tests: discomfort, needs progress, autonomy drift."""
3
+ import ast
4
+ import json
5
+ import types
6
+ import unittest
7
+ from pathlib import Path
8
+
9
+ from biocore_model import autonomy_model as am
10
+ from biocore_model import autonomy_tick as at
11
+ from biocore_model import bio_tick as bt
12
+ from biocore_model import intero_core as ic
13
+
14
+ MAIN = Path(__file__).resolve().parents[2] / "main.py"
15
+ SOURCE = MAIN.read_text(encoding="utf-8")
16
+
17
+
18
+ class FakeCore:
19
+ def __init__(self, partial=False):
20
+ self.v = -0.2
21
+ self.a = 0.8
22
+ self.d = 0.4
23
+ self.social = 0.3
24
+ self.stress = 0.7
25
+ self.energy = 0.2
26
+ self.labels = {"emotion": "sadness", "intensity": 0.8}
27
+ self.allostatic_load = 0.5
28
+ self.boredom = 0.4
29
+ self.homeostasis = {"L_int": 0.3}
30
+ self.open_loops = [{"status": "open"}, {"status": "open"}]
31
+ self.open_loops_due = 2
32
+ self.needs = None
33
+ self.sleep_debt = 0.0
34
+ self.last_debt_ts = 0.0
35
+ self.last_bio_tick_ts = 0.0
36
+ self._bio_tick_count = 0
37
+ self.relationship_context = {"state": {"closeness": 0.3, "conflict": 0.6, "repair": 0.1}}
38
+ self.tension = 0.55
39
+ if partial:
40
+ self.intero = {"tension": 0.55}
41
+ else:
42
+ self.intero = {"tension": 0.55, "warmth": 0.6, "heaviness": 0.3, "clarity": 0.7}
43
+
44
+ def snapshot(self):
45
+ return {"v": self.v, "a": self.a, "d": self.d, "social": self.social,
46
+ "stress": self.stress, "energy": self.energy,
47
+ "allostatic_load": self.allostatic_load, "boredom": self.boredom,
48
+ "homeostasis": dict(self.homeostasis),
49
+ "open_loops": list(self.open_loops),
50
+ "open_loops_due": self.open_loops_due,
51
+ "labels": dict(self.labels)}
52
+
53
+
54
+ class PartsDriftTest(unittest.TestCase):
55
+ def test_parts_from_state_nonzero_drift(self):
56
+ core = FakeCore()
57
+ rel = {"state": {"closeness": 0.3, "conflict": 0.6, "repair": 0.1}}
58
+ parts = am.parts_from_state(core, rel, now=10000.0,
59
+ acc={"last_user_msg_ts": 10000.0 - 7200.0})
60
+ self.assertGreater(parts["social_gap"], 0.0)
61
+ self.assertGreater(parts["stress_load"], 0.0)
62
+ drift = am.drift_value(parts)
63
+ self.assertGreater(drift, 0.02)
64
+ motive = at._compute_motive({}, parts)
65
+ self.assertGreater(motive["drift"], 0.0)
66
+
67
+ def test_v_accumulates_with_built_parts(self):
68
+ core = FakeCore()
69
+ parts = am.parts_from_state(core, core.relationship_context, now=10000.0,
70
+ acc={"last_user_msg_ts": 10000.0 - 7200.0})
71
+ drift = am.drift_value(parts)
72
+ value = 0.0
73
+ for _ in range(5):
74
+ out = am.step(value, drift, theta=5.0, xi=0.0)
75
+ value = out["V"]
76
+ self.assertGreater(value, 0.02)
77
+
78
+ def test_parts_missing_inputs_still_bounded(self):
79
+ parts = am.parts_from_state(object(), {}, now=1.0, acc={})
80
+ self.assertTrue(parts)
81
+ for value in parts.values():
82
+ self.assertGreaterEqual(value, 0.0)
83
+ self.assertLessEqual(value, 1.0)
84
+
85
+
86
+ class NeedsProgressTest(unittest.TestCase):
87
+ def test_needs_move_over_ticks(self):
88
+ core = FakeCore()
89
+ first = bt.bio_tick(core, now=100000.0, dt_hours=1.0)
90
+ start = dict(first["log"]["needs"])
91
+ for index in range(5):
92
+ out = bt.bio_tick(core, now=100000.0 + (index + 1) * 3600.0, dt_hours=1.0)
93
+ end = dict(out["log"]["needs"])
94
+ self.assertLess(end["belonging"], start["belonging"])
95
+ self.assertNotEqual(end, start)
96
+
97
+
98
+ class DiscomfortTest(unittest.TestCase):
99
+ def test_tension_055_is_positive(self):
100
+ out = ic.discomfort({"tension": 0.55, "warmth": ic.COMFORT["warmth"],
101
+ "heaviness": ic.COMFORT["heaviness"],
102
+ "clarity": ic.COMFORT["clarity"]})
103
+ self.assertGreater(out["total"], 0.0)
104
+
105
+ def test_partial_intero_is_null_not_zero(self):
106
+ self.assertIsNone(_page_debug_discomfort({"tension": 0.55}))
107
+ self.assertIsNone(_page_debug_discomfort(None))
108
+ self.assertGreater(_page_debug_discomfort(
109
+ {"tension": 0.55, "warmth": 0.6, "heaviness": 0.3, "clarity": 0.7}), 0.0)
110
+
111
+
112
+ def _load_page_debug():
113
+ tree = ast.parse(SOURCE)
114
+ cls = next(n for n in tree.body
115
+ if isinstance(n, ast.ClassDef) and n.name == "BioCorePlugin")
116
+ node = next(f for f in cls.body
117
+ if isinstance(f, ast.AsyncFunctionDef) and f.name == "page_debug")
118
+ ns = {"_bc_blocks": types.SimpleNamespace(BLOCKS=[], _enabled_map=lambda plugin: {}),
119
+ "PROFILE_PATH": Path("/p.json"), "DATA_DIR": Path("/d")}
120
+ clone = ast.AsyncFunctionDef(name=node.name, args=node.args, body=node.body,
121
+ decorator_list=[], returns=node.returns,
122
+ type_comment=None, lineno=node.lineno,
123
+ col_offset=node.col_offset, end_lineno=node.end_lineno,
124
+ end_col_offset=node.end_col_offset)
125
+ exec(compile(ast.Module(body=[clone], type_ignores=[]), "<page_debug>", "exec"), ns)
126
+ return ns[node.name]
127
+
128
+
129
+ PAGE_DEBUG = _load_page_debug()
130
+
131
+
132
+ def _page_debug_discomfort(intero):
133
+ try:
134
+ if not isinstance(intero, dict):
135
+ return None
136
+ if any(dim not in intero for dim in ic.DIMS):
137
+ return None
138
+ return ic.discomfort(intero).get("total")
139
+ except Exception:
140
+ return None
141
+
142
+
143
+ class PageDebugTest(unittest.IsolatedAsyncioTestCase):
144
+ async def test_page_debug_discomfort_uses_same_intero(self):
145
+ plugin = types.SimpleNamespace(
146
+ cores={"s": FakeCore()}, _life_tick_n=3,
147
+ _bc_blocks=types.SimpleNamespace(BLOCKS=[], _enabled_map=lambda p: {}),
148
+ _load_policy_q=lambda: {}, _policy_pending=None,
149
+ data_dir="/tmp")
150
+ out = await PAGE_DEBUG(plugin)
151
+ self.assertGreater(out["data"]["bio"]["discomfort"], 0.0)
152
+
153
+ async def test_partial_intero_returns_null(self):
154
+ plugin = types.SimpleNamespace(
155
+ cores={"s": FakeCore(partial=True)}, _life_tick_n=3,
156
+ _load_policy_q=lambda: {}, _policy_pending=None, data_dir="/tmp")
157
+ out = await PAGE_DEBUG(plugin)
158
+ self.assertIsNone(out["data"]["bio"]["discomfort"])
159
+
160
+
161
+ class WiringStaticTest(unittest.TestCase):
162
+ def test_needs_log_and_parts_wiring(self):
163
+ self.assertIn("[biocore] needs tick %s", SOURCE)
164
+ self.assertIn("_am.parts_from_state(", SOURCE)
165
+ self.assertIn("if all(dim in intero for dim in _ic_debug.DIMS)", SOURCE)
166
+
167
+
168
+ if __name__ == "__main__":
169
+ unittest.main()
@@ -0,0 +1,177 @@
1
+ # -*- coding: utf-8 -*-
2
+ import unittest
3
+
4
+ from biocore_model import defense_mechanisms as dm
5
+
6
+
7
+ class DefenseMechanismSelectionTest(unittest.TestCase):
8
+ def test_denial_on_near_threat(self):
9
+ out = dm.assess(
10
+ {"kind": "failure", "valence": -0.9, "intensity": 0.9},
11
+ state={"v": -0.7, "a": 0.7, "d": 0.2, "stress": 0.85},
12
+ self_concept={"clarity": 0.15},
13
+ )
14
+ self.assertEqual(out["mechanism"], "denial")
15
+ self.assertEqual(out["label"], "否认")
16
+ self.assertGreaterEqual(out["strength"], 0.6)
17
+
18
+ def test_rationalization_on_self_caused_failure(self):
19
+ out = dm.assess(
20
+ {"kind": "failure", "valence": -0.6, "intensity": 0.7,
21
+ "self_caused": True},
22
+ state={"v": -0.4, "a": 0.4, "d": 0.6, "stress": 0.3},
23
+ self_concept={"clarity": 0.6, "competence": 0.85,
24
+ "honesty": 0.8},
25
+ )
26
+ self.assertEqual(out["mechanism"], "rationalization")
27
+ self.assertGreater(out["strength"], 0.5)
28
+
29
+ def test_projection_when_own_anger_lands_on_other(self):
30
+ out = dm.assess(
31
+ {"kind": "conflict", "valence": -0.5, "intensity": 0.7,
32
+ "target": "peer"},
33
+ state={"anger": 0.85, "fear": 0.2, "trust": 0.15,
34
+ "d": 0.5, "stress": 0.5},
35
+ self_concept={"clarity": 0.5},
36
+ )
37
+ self.assertEqual(out["mechanism"], "projection")
38
+ self.assertEqual(out["label"], "投射")
39
+
40
+ def test_displacement_when_source_is_untouchable(self):
41
+ out = dm.assess(
42
+ {"kind": "criticism", "valence": -0.6, "intensity": 0.8,
43
+ "target": "authority", "power_gap": 1.0},
44
+ state={"anger": 0.8, "d": 0.1, "trust": 0.5, "stress": 0.5},
45
+ self_concept={"clarity": 0.5},
46
+ )
47
+ self.assertEqual(out["mechanism"], "displacement")
48
+ self.assertEqual(out["label"], "转移")
49
+
50
+ def test_strongest_mechanism_wins(self):
51
+ scores = dm.mechanism_scores(
52
+ {"kind": "criticism", "valence": -0.6, "intensity": 0.8,
53
+ "target": "authority", "power_gap": 1.0},
54
+ state={"anger": 0.8, "d": 0.1, "trust": 0.5, "stress": 0.5},
55
+ self_concept={"clarity": 0.5},
56
+ )
57
+ self.assertGreater(scores["displacement"], scores["projection"])
58
+ out = dm.assess(
59
+ {"kind": "criticism", "valence": -0.6, "intensity": 0.8,
60
+ "target": "authority", "power_gap": 1.0},
61
+ state={"anger": 0.8, "d": 0.1, "trust": 0.5, "stress": 0.5},
62
+ self_concept={"clarity": 0.5},
63
+ )
64
+ self.assertEqual(out["mechanism"], "displacement")
65
+
66
+
67
+ class DefenseMechanismOutputTest(unittest.TestCase):
68
+ CASES = (
69
+ ({"kind": "failure", "valence": -0.9, "intensity": 0.9},
70
+ {"v": -0.7, "a": 0.7, "d": 0.2, "stress": 0.85},
71
+ {"clarity": 0.15}),
72
+ ({"kind": "failure", "valence": -0.6, "intensity": 0.7,
73
+ "self_caused": True}, {"stress": 0.3},
74
+ {"clarity": 0.6, "competence": 0.85}),
75
+ ({"kind": "conflict", "valence": -0.5, "intensity": 0.7,
76
+ "target": "peer"},
77
+ {"anger": 0.85, "trust": 0.15}, {"clarity": 0.5}),
78
+ ({"kind": "criticism", "valence": -0.6, "intensity": 0.8,
79
+ "target": "authority", "power_gap": 1.0},
80
+ {"anger": 0.8, "d": 0.1}, {"clarity": 0.5}),
81
+ )
82
+
83
+ def test_strength_and_cost_are_bounded(self):
84
+ for event, state, concept in self.CASES:
85
+ out = dm.assess(event, state=state, self_concept=concept)
86
+ self.assertGreaterEqual(out["strength"], 0.0)
87
+ self.assertLessEqual(out["strength"], 1.0)
88
+ self.assertGreaterEqual(out["cost"], 0.0)
89
+ self.assertLessEqual(out["cost"], 1.0)
90
+
91
+ def test_cost_scales_with_strength(self):
92
+ for event, state, concept in self.CASES:
93
+ out = dm.assess(event, state=state, self_concept=concept)
94
+ base = dm.COST_BASE[out["mechanism"]]
95
+ self.assertEqual(out["cost"], round(base * out["strength"], 4))
96
+
97
+ def test_reason_and_self_talk_present_for_each(self):
98
+ for event, state, concept in self.CASES:
99
+ out = dm.assess(event, state=state, self_concept=concept)
100
+ self.assertTrue(out["mechanism"])
101
+ self.assertTrue(out["reason"])
102
+ self.assertTrue(out["self_talk"])
103
+
104
+ def test_mechanism_scores_cover_all_four(self):
105
+ scores = dm.mechanism_scores({}, None, None)
106
+ self.assertEqual(set(scores), set(dm.MECHANISMS))
107
+ for value in scores.values():
108
+ self.assertGreaterEqual(value, 0.0)
109
+ self.assertLessEqual(value, 1.0)
110
+
111
+ def test_deterministic(self):
112
+ first = dm.assess(
113
+ {"kind": "conflict", "valence": -0.5, "intensity": 0.7},
114
+ state={"anger": 0.85, "trust": 0.15}, self_concept={"clarity": 0.5})
115
+ second = dm.assess(
116
+ {"kind": "conflict", "valence": -0.5, "intensity": 0.7},
117
+ state={"anger": 0.85, "trust": 0.15}, self_concept={"clarity": 0.5})
118
+ self.assertEqual(first, second)
119
+
120
+
121
+ class DefenseMechanismSwitchTest(unittest.TestCase):
122
+ DENIAL_EVENT = {"kind": "failure", "valence": -0.9, "intensity": 0.9}
123
+ DENIAL_STATE = {"v": -0.7, "a": 0.7, "d": 0.2, "stress": 0.85}
124
+ DENIAL_CONCEPT = {"clarity": 0.15}
125
+
126
+ def test_disabled_bool(self):
127
+ out = dm.assess(self.DENIAL_EVENT, state=self.DENIAL_STATE,
128
+ self_concept=self.DENIAL_CONCEPT, config=False)
129
+ self.assertFalse(out["enabled"])
130
+ self.assertEqual(out["mechanism"], "")
131
+ self.assertEqual(out["strength"], 0.0)
132
+ self.assertEqual(out["cost"], 0.0)
133
+ self.assertEqual(out["self_talk"], "")
134
+ self.assertTrue(out["reason"])
135
+
136
+ def test_disabled_config_dict(self):
137
+ out = dm.assess(self.DENIAL_EVENT, state=self.DENIAL_STATE,
138
+ self_concept=self.DENIAL_CONCEPT,
139
+ config={"enabled": False})
140
+ self.assertFalse(out["enabled"])
141
+ self.assertEqual(out["mechanism"], "")
142
+
143
+ def test_no_defense_when_nothing_negative(self):
144
+ out = dm.assess({"kind": "praise", "valence": 0.8, "intensity": 0.9},
145
+ state={"v": 0.6, "a": 0.5}, self_concept={"clarity": 0.6})
146
+ self.assertEqual(out["mechanism"], "")
147
+ self.assertEqual(out["strength"], 0.0)
148
+ self.assertEqual(out["self_talk"], "")
149
+ self.assertTrue(out["reason"])
150
+
151
+ def test_threshold_can_be_raised(self):
152
+ out = dm.assess(self.DENIAL_EVENT, state=self.DENIAL_STATE,
153
+ self_concept=self.DENIAL_CONCEPT,
154
+ config={"threshold": 0.95})
155
+ self.assertEqual(out["mechanism"], "")
156
+
157
+ def test_malformed_inputs_do_not_crash(self):
158
+ for event, state, concept in ((None, None, None),
159
+ ("bad", "bad", "bad"),
160
+ ({"valence": "x"}, {"stress": "x"}, [])):
161
+ out = dm.assess(event, state=state, self_concept=concept)
162
+ self.assertIn("mechanism", out)
163
+ self.assertIn("scores", out)
164
+ self.assertGreaterEqual(out["strength"], 0.0)
165
+ self.assertLessEqual(out["strength"], 1.0)
166
+
167
+ def test_view_fields(self):
168
+ out = dm.assess(self.DENIAL_EVENT, state=self.DENIAL_STATE,
169
+ self_concept=self.DENIAL_CONCEPT)
170
+ panel = dm.view(out)
171
+ for key in ("enabled", "mechanism", "label", "strength", "cost",
172
+ "reason", "self_talk"):
173
+ self.assertIn(key, panel)
174
+
175
+
176
+ if __name__ == "__main__":
177
+ unittest.main()
@@ -0,0 +1,157 @@
1
+ # -*- coding: utf-8 -*-
2
+ """P0-2: per-message total |dims delta| budget (delta_budget + main wiring)."""
3
+ import ast
4
+ import json
5
+ import types
6
+ import unittest
7
+ from pathlib import Path
8
+
9
+ from biocore_model import appraisal
10
+ from biocore_model import delta_budget as db
11
+
12
+ ROOT = Path(__file__).resolve().parents[2]
13
+ MAIN = ROOT / "main.py"
14
+ SOURCE = MAIN.read_text(encoding="utf-8")
15
+ SCHEMA = json.loads((ROOT / "_conf_schema.json").read_text(encoding="utf-8"))
16
+
17
+
18
+ def _load_budget_helper():
19
+ tree = ast.parse(SOURCE)
20
+ node = next(item for item in tree.body
21
+ if isinstance(item, ast.FunctionDef)
22
+ and item.name == "_budget_dims_deltas")
23
+ clone = ast.FunctionDef(name=node.name, args=node.args, body=node.body,
24
+ decorator_list=[], returns=node.returns,
25
+ type_comment=None, lineno=node.lineno,
26
+ col_offset=node.col_offset,
27
+ end_lineno=node.end_lineno,
28
+ end_col_offset=node.end_col_offset)
29
+ ns = {"logger": types.SimpleNamespace(
30
+ info=lambda *a, **k: None, warning=lambda *a, **k: None,
31
+ debug=lambda *a, **k: None)}
32
+ exec(compile(ast.Module(body=[clone], type_ignores=[]),
33
+ "<_budget_dims_deltas>", "exec"), ns)
34
+ return ns[node.name]
35
+
36
+
37
+ BUDGET_METHOD = _load_budget_helper()
38
+
39
+
40
+ class _FakePlugin:
41
+ def __init__(self, limit=0.2, enabled=True):
42
+ self.dims_delta_limit = limit
43
+ self.dims_delta_limit_enabled = enabled
44
+
45
+
46
+ class BudgetTest(unittest.TestCase):
47
+ def test_within_limit_unchanged(self):
48
+ deltas = {"v": 0.10, "a": -0.05}
49
+ scaled, report = db.budget(deltas)
50
+ self.assertFalse(report["scaled"])
51
+ self.assertEqual(scaled, deltas)
52
+ self.assertAlmostEqual(report["raw_total"], 0.15)
53
+ self.assertAlmostEqual(report["factor"], 1.0)
54
+
55
+ def test_over_limit_scaled_proportionally(self):
56
+ deltas = {"v": 0.6, "a": -0.3, "stress": 0.1}
57
+ scaled, report = db.budget(deltas, limit=0.2)
58
+ self.assertTrue(report["scaled"])
59
+ self.assertAlmostEqual(report["raw_total"], 1.0)
60
+ self.assertLessEqual(report["scaled_total"], 0.2 + 1e-6)
61
+ self.assertAlmostEqual(report["factor"], 0.2)
62
+ # direction and channel ratios are preserved
63
+ self.assertGreater(scaled["v"], 0)
64
+ self.assertLess(scaled["a"], 0)
65
+ self.assertAlmostEqual(scaled["v"] / scaled["a"], deltas["v"] / deltas["a"])
66
+ self.assertAlmostEqual(scaled["v"], 0.12, places=4)
67
+
68
+ def test_zero_deltas_unchanged(self):
69
+ scaled, report = db.budget({"v": 0.0, "a": 0.0})
70
+ self.assertEqual(scaled, {"v": 0.0, "a": 0.0})
71
+ self.assertFalse(report["scaled"])
72
+ self.assertEqual(report["raw_total"], 0.0)
73
+
74
+ def test_multi_field_sum_crosses_limit(self):
75
+ deltas = {"v": 0.08, "a": 0.08, "d": 0.08, "social": 0.04}
76
+ scaled, report = db.budget(deltas, limit=0.2)
77
+ self.assertTrue(report["scaled"])
78
+ self.assertAlmostEqual(report["raw_total"], 0.28)
79
+ self.assertLessEqual(db.total(scaled), 0.2 + 1e-6)
80
+ self.assertEqual(len(scaled), 4)
81
+
82
+ def test_disabled_returns_unchanged(self):
83
+ deltas = {"v": 0.6, "stress": 0.3}
84
+ scaled, report = db.budget(deltas, limit=0.2, enabled=False)
85
+ self.assertEqual(scaled, deltas)
86
+ self.assertFalse(report["scaled"])
87
+ self.assertFalse(report["enabled"])
88
+
89
+ def test_custom_limit_respected(self):
90
+ scaled, report = db.budget({"v": 0.5, "a": 0.5}, limit=0.5)
91
+ self.assertTrue(report["scaled"])
92
+ self.assertAlmostEqual(sum(abs(x) for x in scaled.values()), 0.5)
93
+
94
+ def test_invalid_values_dropped(self):
95
+ scaled, report = db.budget({"v": "abc", "a": None, "d": float("nan"),
96
+ "stress": float("inf"), "trust": 0.05})
97
+ self.assertEqual(scaled, {"trust": 0.05})
98
+ self.assertFalse(report["scaled"])
99
+
100
+ def test_invalid_limit_falls_back_to_default(self):
101
+ self.assertAlmostEqual(db.clean_limit(0), db.DEFAULT_LIMIT)
102
+ self.assertAlmostEqual(db.clean_limit("x"), db.DEFAULT_LIMIT)
103
+ self.assertAlmostEqual(db.clean_limit(None), db.DEFAULT_LIMIT)
104
+ self.assertAlmostEqual(db.clean_limit(5.0), db.MAX_LIMIT)
105
+ self.assertAlmostEqual(db.clean_limit(0.0001), db.MIN_LIMIT)
106
+
107
+ def test_negative_deltas_use_absolute_sum(self):
108
+ scaled, report = db.budget({"v": -0.5, "a": -0.5}, limit=0.2)
109
+ self.assertTrue(report["scaled"])
110
+ self.assertAlmostEqual(report["raw_total"], 1.0)
111
+ self.assertAlmostEqual(scaled["v"], -0.1, places=4)
112
+
113
+ def test_real_appraise_output_is_bounded(self):
114
+ # full-blown dims from the appraisal layer can exceed one message budget
115
+ dims = {"goal": 1.0, "blame": 1.0, "hit": 1.0, "involve": 1.0,
116
+ "control": 1.0, "novelty": 1.0, "certainty": 1.0}
117
+ deltas = appraisal.appraise(dims, confidence=1.0)
118
+ self.assertGreater(db.total(deltas), 0.2)
119
+ scaled, report = db.budget(deltas)
120
+ self.assertTrue(report["scaled"])
121
+ self.assertLessEqual(db.total(scaled), 0.2 + 1e-6)
122
+
123
+
124
+ class MainWiringTest(unittest.TestCase):
125
+ def test_config_attrs_and_call_sites(self):
126
+ self.assertIn("dims_delta_limit_enabled", SOURCE)
127
+ self.assertIn("dims_delta_limit", SOURCE)
128
+ self.assertIn("def _budget_dims_deltas(plugin, deltas", SOURCE)
129
+ self.assertEqual(SOURCE.count("_budget_dims_deltas(self, "), 2)
130
+ self.assertIn('"user"', SOURCE)
131
+ self.assertIn('"reply"', SOURCE)
132
+
133
+ def test_schema_entries(self):
134
+ for key in ("dims_delta_limit_enabled", "dims_delta_limit"):
135
+ self.assertIn(key, SCHEMA)
136
+ self.assertTrue(SCHEMA["dims_delta_limit_enabled"]["default"])
137
+ self.assertAlmostEqual(SCHEMA["dims_delta_limit"]["default"], 0.2)
138
+ self.assertEqual(SCHEMA["dims_delta_limit"]["type"], "float")
139
+
140
+ def test_method_scales_with_plugin_config(self):
141
+ scaled = BUDGET_METHOD(_FakePlugin(limit=0.2), {"v": 0.6, "a": 0.4},
142
+ "user")
143
+ self.assertAlmostEqual(sum(abs(x) for x in scaled.values()), 0.2,
144
+ places=4)
145
+
146
+ def test_method_honours_disabled_flag(self):
147
+ deltas = {"v": 0.6, "a": 0.4}
148
+ self.assertEqual(BUDGET_METHOD(_FakePlugin(enabled=False), deltas,
149
+ "reply"), deltas)
150
+
151
+ def test_method_without_config_uses_default(self):
152
+ scaled = BUDGET_METHOD(types.SimpleNamespace(), {"v": 0.9}, "reply")
153
+ self.assertAlmostEqual(scaled["v"], 0.2, places=4)
154
+
155
+
156
+ if __name__ == "__main__":
157
+ unittest.main()