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.
- package/README.md +11 -0
- package/lib/core-worker.js +268 -0
- package/lib/index.js +160 -70
- package/lib/modules/autonomy.js +125 -0
- package/lib/modules/cache-stats.js +48 -0
- package/lib/modules/core-turn.js +24 -6
- package/lib/modules/expression-inject.js +0 -10
- package/lib/modules/expression-refresh.js +0 -9
- package/lib/modules/memory-capture.js +2 -1
- package/lib/modules/memory-inject.js +50 -5
- package/lib/modules/memory-tools.js +7 -4
- package/lib/modules/python-env.js +46 -0
- package/lib/modules/safety-gate.js +0 -3
- package/lib/modules/series.js +54 -0
- package/lib/modules/switch.js +16 -0
- package/lib/py-bridge.js +81 -15
- package/package.json +1 -1
- package/python/biocore_memory/core/config.py +138 -127
- package/python/biocore_memory/engine.py +2653 -2476
- package/python/biocore_memory/recall/embedding.py +129 -0
- package/python/biocore_memory/recall/feedback.py +202 -0
- package/python/biocore_model/preference_drift.py +255 -0
- package/python/biocore_model/tests/__init__.py +1 -0
- package/python/biocore_model/tests/core_mode_ui_check.mjs +63 -0
- package/python/biocore_model/tests/core_status_check.mjs +29 -0
- package/python/biocore_model/tests/dom_safe_check.mjs +22 -0
- package/python/biocore_model/tests/leak_samples_run.py +18 -0
- package/python/biocore_model/tests/orb_ring_check.mjs +20 -0
- package/python/biocore_model/tests/panel_html_check.mjs +63 -0
- package/python/biocore_model/tests/persona_flow_check.mjs +17 -0
- package/python/biocore_model/tests/render_guard_check.mjs +19 -0
- package/python/biocore_model/tests/switch_groups_check.mjs +94 -0
- package/python/biocore_model/tests/tabs_state_check.mjs +24 -0
- package/python/biocore_model/tests/test_affective_labor.py +102 -0
- package/python/biocore_model/tests/test_appraisal.py +137 -0
- package/python/biocore_model/tests/test_appraisal_action.py +55 -0
- package/python/biocore_model/tests/test_arch_convergence.py +35 -0
- package/python/biocore_model/tests/test_attachment_behavior.py +95 -0
- package/python/biocore_model/tests/test_attachment_regulation.py +63 -0
- package/python/biocore_model/tests/test_attachment_state.py +29 -0
- package/python/biocore_model/tests/test_attribution.py +56 -0
- package/python/biocore_model/tests/test_attributor.py +108 -0
- package/python/biocore_model/tests/test_autonomic.py +69 -0
- package/python/biocore_model/tests/test_autonomy_accumulator.py +84 -0
- package/python/biocore_model/tests/test_autonomy_affect.py +62 -0
- package/python/biocore_model/tests/test_autonomy_blame.py +44 -0
- package/python/biocore_model/tests/test_autonomy_caps.py +29 -0
- package/python/biocore_model/tests/test_autonomy_check.py +40 -0
- package/python/biocore_model/tests/test_autonomy_clingy.py +41 -0
- package/python/biocore_model/tests/test_autonomy_cooldown_selfcontact.py +138 -0
- package/python/biocore_model/tests/test_autonomy_frustration.py +42 -0
- package/python/biocore_model/tests/test_autonomy_hourly_limit.py +46 -0
- package/python/biocore_model/tests/test_autonomy_loop.py +64 -0
- package/python/biocore_model/tests/test_autonomy_mode.py +37 -0
- package/python/biocore_model/tests/test_autonomy_model.py +60 -0
- package/python/biocore_model/tests/test_autonomy_model_v2.py +68 -0
- package/python/biocore_model/tests/test_autonomy_module_text.py +104 -0
- package/python/biocore_model/tests/test_autonomy_params.py +163 -0
- package/python/biocore_model/tests/test_autonomy_reason.py +34 -0
- package/python/biocore_model/tests/test_autonomy_self.py +153 -0
- package/python/biocore_model/tests/test_autonomy_send_gate.py +38 -0
- package/python/biocore_model/tests/test_autonomy_silence.py +37 -0
- package/python/biocore_model/tests/test_autonomy_status.py +35 -0
- package/python/biocore_model/tests/test_autonomy_test_trigger.py +24 -0
- package/python/biocore_model/tests/test_autonomy_tick.py +148 -0
- package/python/biocore_model/tests/test_autonomy_v4.py +64 -0
- package/python/biocore_model/tests/test_autonomy_v5.py +43 -0
- package/python/biocore_model/tests/test_autonomy_v7.py +48 -0
- package/python/biocore_model/tests/test_autonomy_v_theta.py +85 -0
- package/python/biocore_model/tests/test_batch1_wiring.py +231 -0
- package/python/biocore_model/tests/test_batch2_wiring.py +231 -0
- package/python/biocore_model/tests/test_batch3_wiring.py +223 -0
- package/python/biocore_model/tests/test_bio_tick.py +202 -0
- package/python/biocore_model/tests/test_boundary.py +183 -0
- package/python/biocore_model/tests/test_boundary_gate.py +295 -0
- package/python/biocore_model/tests/test_brain_split.py +99 -0
- package/python/biocore_model/tests/test_co_recall.py +350 -0
- package/python/biocore_model/tests/test_commitment_extractor.py +197 -0
- package/python/biocore_model/tests/test_compassion_fatigue.py +84 -0
- package/python/biocore_model/tests/test_connection_bids.py +69 -0
- package/python/biocore_model/tests/test_core_bridge.py +88 -0
- package/python/biocore_model/tests/test_core_bridge_episode.py +14 -0
- package/python/biocore_model/tests/test_core_bridge_forget.py +17 -0
- package/python/biocore_model/tests/test_core_bridge_identity.py +15 -0
- package/python/biocore_model/tests/test_core_bridge_media.py +17 -0
- package/python/biocore_model/tests/test_core_bridge_relation.py +17 -0
- package/python/biocore_model/tests/test_core_bridge_summary.py +17 -0
- package/python/biocore_model/tests/test_core_client.py +156 -0
- package/python/biocore_model/tests/test_core_commands.py +146 -0
- package/python/biocore_model/tests/test_core_mode.py +161 -0
- package/python/biocore_model/tests/test_core_state.py +240 -0
- package/python/biocore_model/tests/test_criticality.py +61 -0
- package/python/biocore_model/tests/test_cross_session_copy.py +67 -0
- package/python/biocore_model/tests/test_debug_values.py +169 -0
- package/python/biocore_model/tests/test_defense_mechanisms.py +177 -0
- package/python/biocore_model/tests/test_delta_budget.py +157 -0
- package/python/biocore_model/tests/test_diag_trace.py +201 -0
- package/python/biocore_model/tests/test_directives_compat.py +698 -0
- package/python/biocore_model/tests/test_dissonance.py +97 -0
- package/python/biocore_model/tests/test_drift_sign.py +87 -0
- package/python/biocore_model/tests/test_embedding_fallback.py +277 -0
- package/python/biocore_model/tests/test_embedding_local.py +279 -0
- package/python/biocore_model/tests/test_embedding_mode.py +175 -0
- package/python/biocore_model/tests/test_embedding_modes.py +342 -0
- package/python/biocore_model/tests/test_emotion_geometry.py +114 -0
- package/python/biocore_model/tests/test_emotion_probe.py +214 -0
- package/python/biocore_model/tests/test_emotion_probe_concurrency.py +128 -0
- package/python/biocore_model/tests/test_entity_graph.py +198 -0
- package/python/biocore_model/tests/test_entity_resolve.py +446 -0
- package/python/biocore_model/tests/test_envy.py +173 -0
- package/python/biocore_model/tests/test_exit_or_silence.py +209 -0
- package/python/biocore_model/tests/test_expr_guard.py +1 -0
- package/python/biocore_model/tests/test_expr_intent.py +66 -0
- package/python/biocore_model/tests/test_expr_state.py +66 -0
- package/python/biocore_model/tests/test_expr_template.py +71 -0
- package/python/biocore_model/tests/test_expr_tone.py +41 -0
- package/python/biocore_model/tests/test_extreme_scenarios.py +147 -0
- package/python/biocore_model/tests/test_feedback_flow.py +185 -0
- package/python/biocore_model/tests/test_feedback_guard.py +65 -0
- package/python/biocore_model/tests/test_feedback_llm.py +59 -0
- package/python/biocore_model/tests/test_feedback_llm_inferred.py +140 -0
- package/python/biocore_model/tests/test_feedback_memory.py +63 -0
- package/python/biocore_model/tests/test_feedback_user.py +61 -0
- package/python/biocore_model/tests/test_frustration_curve.py +66 -0
- package/python/biocore_model/tests/test_global_bio_state.py +116 -0
- package/python/biocore_model/tests/test_governance.py +181 -0
- package/python/biocore_model/tests/test_granularity.py +133 -0
- package/python/biocore_model/tests/test_graph_extract.py +224 -0
- package/python/biocore_model/tests/test_graph_recall.py +226 -0
- package/python/biocore_model/tests/test_graph_sqlite.py +298 -0
- package/python/biocore_model/tests/test_growth.py +301 -0
- package/python/biocore_model/tests/test_intent_render.py +60 -0
- package/python/biocore_model/tests/test_intero_regulation.py +224 -0
- package/python/biocore_model/tests/test_intero_style.py +146 -0
- package/python/biocore_model/tests/test_item3_numeric.py +105 -0
- package/python/biocore_model/tests/test_item6_numeric.py +116 -0
- package/python/biocore_model/tests/test_layered_memory.py +174 -0
- package/python/biocore_model/tests/test_leak_guard.py +75 -0
- package/python/biocore_model/tests/test_learning_probe.py +93 -0
- package/python/biocore_model/tests/test_life_loop.py +222 -0
- package/python/biocore_model/tests/test_life_loop_10s.py +131 -0
- package/python/biocore_model/tests/test_local_embedding_health.py +258 -0
- package/python/biocore_model/tests/test_memory_affect_encoding.py +79 -0
- package/python/biocore_model/tests/test_memory_backend.py +332 -0
- package/python/biocore_model/tests/test_memory_dedup.py +316 -0
- package/python/biocore_model/tests/test_memory_dual.py +219 -0
- package/python/biocore_model/tests/test_memory_dual_wiring.py +219 -0
- package/python/biocore_model/tests/test_memory_feedback.py +241 -0
- package/python/biocore_model/tests/test_memory_fixes.py +84 -0
- package/python/biocore_model/tests/test_memory_forgetting.py +58 -0
- package/python/biocore_model/tests/test_memory_inject.py +211 -0
- package/python/biocore_model/tests/test_memory_mood_recall.py +118 -0
- package/python/biocore_model/tests/test_memory_store.py +372 -0
- package/python/biocore_model/tests/test_metacog.py +103 -0
- package/python/biocore_model/tests/test_migrate.py +245 -0
- package/python/biocore_model/tests/test_migrate_store.py +194 -0
- package/python/biocore_model/tests/test_mind_wander.py +65 -0
- package/python/biocore_model/tests/test_mood.py +35 -0
- package/python/biocore_model/tests/test_narrative.py +114 -0
- package/python/biocore_model/tests/test_needs_esteem.py +51 -0
- package/python/biocore_model/tests/test_needs_events.py +205 -0
- package/python/biocore_model/tests/test_needs_sleep.py +54 -0
- package/python/biocore_model/tests/test_noise_bank.py +54 -0
- package/python/biocore_model/tests/test_numeric_schema.py +98 -0
- package/python/biocore_model/tests/test_output_protocol.py +65 -0
- package/python/biocore_model/tests/test_page_debug.py +147 -0
- package/python/biocore_model/tests/test_page_status_autonomy.py +215 -0
- package/python/biocore_model/tests/test_pipeline.py +345 -0
- package/python/biocore_model/tests/test_pipeline_api.py +192 -0
- package/python/biocore_model/tests/test_pipeline_mode.py +222 -0
- package/python/biocore_model/tests/test_plugin_config.py +85 -0
- package/python/biocore_model/tests/test_policy_learning.py +116 -0
- package/python/biocore_model/tests/test_possible_selves.py +88 -0
- package/python/biocore_model/tests/test_predictor.py +227 -0
- package/python/biocore_model/tests/test_preference_drift.py +142 -0
- package/python/biocore_model/tests/test_preset.py +142 -0
- package/python/biocore_model/tests/test_proactive_context.py +70 -0
- package/python/biocore_model/tests/test_proactive_history.py +90 -0
- package/python/biocore_model/tests/test_proactive_memory.py +141 -0
- package/python/biocore_model/tests/test_proactive_send_record.py +88 -0
- package/python/biocore_model/tests/test_probe_contract.py +71 -0
- package/python/biocore_model/tests/test_prospective.py +154 -0
- package/python/biocore_model/tests/test_provider_fallback.py +89 -0
- package/python/biocore_model/tests/test_reconstruction.py +111 -0
- package/python/biocore_model/tests/test_regulation_strategies.py +228 -0
- package/python/biocore_model/tests/test_relation_stage.py +114 -0
- package/python/biocore_model/tests/test_relationship_stage.py +59 -0
- package/python/biocore_model/tests/test_release_140.py +55 -0
- package/python/biocore_model/tests/test_repair_after_boundary.py +249 -0
- package/python/biocore_model/tests/test_reply_limit.py +86 -0
- package/python/biocore_model/tests/test_reply_limit_persist.py +48 -0
- package/python/biocore_model/tests/test_reply_split.py +52 -0
- package/python/biocore_model/tests/test_reply_split_astrbot.py +66 -0
- package/python/biocore_model/tests/test_reply_split_inject.py +53 -0
- package/python/biocore_model/tests/test_rerank.py +229 -0
- package/python/biocore_model/tests/test_rollback.py +175 -0
- package/python/biocore_model/tests/test_runtime.py +175 -0
- package/python/biocore_model/tests/test_runtime_active.py +205 -0
- package/python/biocore_model/tests/test_runtime_pipeline.py +1583 -0
- package/python/biocore_model/tests/test_runtime_proactive.py +231 -0
- package/python/biocore_model/tests/test_scheduler.py +79 -0
- package/python/biocore_model/tests/test_secrecy.py +105 -0
- package/python/biocore_model/tests/test_self_esteem.py +45 -0
- package/python/biocore_model/tests/test_self_intent.py +347 -0
- package/python/biocore_model/tests/test_self_interest.py +183 -0
- package/python/biocore_model/tests/test_self_layer_wiring.py +198 -0
- package/python/biocore_model/tests/test_self_verification.py +113 -0
- package/python/biocore_model/tests/test_situation.py +142 -0
- package/python/biocore_model/tests/test_sleep_circadian.py +41 -0
- package/python/biocore_model/tests/test_social_pain.py +49 -0
- package/python/biocore_model/tests/test_social_repair.py +67 -0
- package/python/biocore_model/tests/test_social_theory.py +78 -0
- package/python/biocore_model/tests/test_source_monitoring.py +204 -0
- package/python/biocore_model/tests/test_speak_like_group.py +36 -0
- package/python/biocore_model/tests/test_stages.py +226 -0
- package/python/biocore_model/tests/test_state_tag.py +86 -0
- package/python/biocore_model/tests/test_template_expr.py +65 -0
- package/python/biocore_model/tests/test_text_inference.py +70 -0
- package/python/biocore_model/tests/test_theta_params.py +115 -0
- package/python/biocore_model/tests/test_topic_control.py +200 -0
- package/python/biocore_model/tests/test_turn_memory.py +105 -0
- package/python/biocore_model/tests/test_turn_memory_panel.py +174 -0
- package/python/biocore_model/tests/test_ui_settings.py +84 -0
- package/python/biocore_model/tests/test_ui_settings_authority.py +179 -0
- package/python/biocore_model/tests/test_user_appraisal_async.py +155 -0
- package/python/biocore_model/tests/test_user_classify.py +122 -0
- package/python/biocore_model/tests/test_value_order.py +117 -0
- package/python/biocore_model/tests/test_voice_bank.py +105 -0
- package/python/biocore_model/tests/test_waiting_sense.py +216 -0
- package/python/biocore_model/tests/test_x_directives.py +101 -0
- package/python/biocore_model/tests/ui_compat_check.mjs +22 -0
- package/python/biocore_model/tests/view_mode_check.mjs +17 -0
- package/python/biocore_runtime/autonomy.py +208 -0
- package/python/biocore_runtime/core_state.py +302 -0
- package/python/biocore_runtime/governance.py +495 -0
- package/python/biocore_runtime/growth.py +528 -0
- package/python/biocore_runtime/pipeline.py +850 -23
- package/python/biocore_runtime/relationship_state.py +214 -0
- package/python/biocore_runtime/self_layer.py +103 -0
- package/python/biocore_runtime/service.py +162 -0
- package/python/biocore_runtime/standby.py +472 -0
- package/ui/panel-layout.js +83 -39
- package/ui/panel.css +885 -743
- package/ui/panel.html +356 -296
- package/ui/panel.js +739 -865
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""Probe concurrency tests: no probe-level deadline + inflight drop."""
|
|
3
|
+
import ast
|
|
4
|
+
import asyncio
|
|
5
|
+
import types
|
|
6
|
+
import unittest
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from biocore_model import emotion_probe as ep
|
|
10
|
+
|
|
11
|
+
MAIN = Path(__file__).resolve().parents[2] / "main.py"
|
|
12
|
+
SOURCE = MAIN.read_text(encoding="utf-8")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _load_helpers():
|
|
16
|
+
tree = ast.parse(SOURCE)
|
|
17
|
+
cls = next(n for n in tree.body
|
|
18
|
+
if isinstance(n, ast.ClassDef) and n.name == "BioCorePlugin")
|
|
19
|
+
wanted = {"_emotion_probe_acquire", "_emotion_probe_release"}
|
|
20
|
+
ns = {}
|
|
21
|
+
fns = {}
|
|
22
|
+
for node in cls.body:
|
|
23
|
+
if isinstance(node, ast.FunctionDef) and node.name in wanted:
|
|
24
|
+
clone = ast.FunctionDef(name=node.name, args=node.args, body=node.body,
|
|
25
|
+
decorator_list=[], returns=node.returns,
|
|
26
|
+
type_comment=None, lineno=node.lineno,
|
|
27
|
+
col_offset=node.col_offset,
|
|
28
|
+
end_lineno=node.end_lineno,
|
|
29
|
+
end_col_offset=node.end_col_offset)
|
|
30
|
+
exec(compile(ast.Module(body=[clone], type_ignores=[]), "<probe-conc>", "exec"),
|
|
31
|
+
ns)
|
|
32
|
+
fns[node.name] = ns[node.name]
|
|
33
|
+
return fns
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
FNS = _load_helpers()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class FakePlugin:
|
|
40
|
+
def __init__(self, max_inflight=None):
|
|
41
|
+
self._probe_inflight = 0
|
|
42
|
+
if max_inflight is not None:
|
|
43
|
+
self.emotion_probe_max_inflight = max_inflight
|
|
44
|
+
|
|
45
|
+
def acquire(self):
|
|
46
|
+
return FNS["_emotion_probe_acquire"](self)
|
|
47
|
+
|
|
48
|
+
def release(self):
|
|
49
|
+
return FNS["_emotion_probe_release"](self)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class ProbeTimeoutTest(unittest.IsolatedAsyncioTestCase):
|
|
53
|
+
async def test_no_timeout_does_not_cancel(self):
|
|
54
|
+
done = {"value": False}
|
|
55
|
+
|
|
56
|
+
async def call(prompt):
|
|
57
|
+
await asyncio.sleep(0.2)
|
|
58
|
+
done["value"] = True
|
|
59
|
+
return '{"deltas":{"v":-0.1},"why":"x","c":0.8}'
|
|
60
|
+
|
|
61
|
+
out = await ep.probe("x", "y", call, timeout=None, core=object(), now=1000.0)
|
|
62
|
+
self.assertTrue(done["value"])
|
|
63
|
+
self.assertEqual(out["deltas"], {"v": -0.1})
|
|
64
|
+
|
|
65
|
+
async def test_explicit_timeout_still_cancels(self):
|
|
66
|
+
async def call(prompt):
|
|
67
|
+
await asyncio.sleep(1.0)
|
|
68
|
+
return '{"deltas":{"v":-0.1},"c":0.8}'
|
|
69
|
+
|
|
70
|
+
out = await ep.probe("x", "y", call, timeout=0.05, core=object(), now=1000.0)
|
|
71
|
+
self.assertEqual(out, {})
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class InflightTest(unittest.TestCase):
|
|
75
|
+
def test_default_limit_is_one(self):
|
|
76
|
+
plugin = FakePlugin()
|
|
77
|
+
self.assertTrue(plugin.acquire())
|
|
78
|
+
self.assertFalse(plugin.acquire())
|
|
79
|
+
self.assertEqual(plugin._probe_inflight, 1)
|
|
80
|
+
|
|
81
|
+
def test_release_allows_next(self):
|
|
82
|
+
plugin = FakePlugin()
|
|
83
|
+
self.assertTrue(plugin.acquire())
|
|
84
|
+
plugin.release()
|
|
85
|
+
self.assertEqual(plugin._probe_inflight, 0)
|
|
86
|
+
self.assertTrue(plugin.acquire())
|
|
87
|
+
|
|
88
|
+
def test_exception_path_releases(self):
|
|
89
|
+
plugin = FakePlugin()
|
|
90
|
+
self.assertTrue(plugin.acquire())
|
|
91
|
+
try:
|
|
92
|
+
raise RuntimeError("probe failed")
|
|
93
|
+
except RuntimeError:
|
|
94
|
+
pass
|
|
95
|
+
finally:
|
|
96
|
+
plugin.release()
|
|
97
|
+
self.assertEqual(plugin._probe_inflight, 0)
|
|
98
|
+
self.assertTrue(plugin.acquire())
|
|
99
|
+
|
|
100
|
+
def test_max_inflight_two(self):
|
|
101
|
+
plugin = FakePlugin(max_inflight=2)
|
|
102
|
+
self.assertTrue(plugin.acquire())
|
|
103
|
+
self.assertTrue(plugin.acquire())
|
|
104
|
+
self.assertFalse(plugin.acquire())
|
|
105
|
+
plugin.release()
|
|
106
|
+
self.assertTrue(plugin.acquire())
|
|
107
|
+
|
|
108
|
+
def test_release_never_negative(self):
|
|
109
|
+
plugin = FakePlugin()
|
|
110
|
+
plugin.release()
|
|
111
|
+
plugin.release()
|
|
112
|
+
self.assertEqual(plugin._probe_inflight, 0)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class WiringStaticTest(unittest.TestCase):
|
|
116
|
+
def test_main_guard_and_finally(self):
|
|
117
|
+
self.assertIn("[biocore] emotion probe skipped (inflight)", SOURCE)
|
|
118
|
+
self.assertIn("if not self._emotion_probe_acquire():", SOURCE)
|
|
119
|
+
self.assertIn("self._emotion_probe_release()", SOURCE)
|
|
120
|
+
self.assertIn("timeout=None", SOURCE)
|
|
121
|
+
|
|
122
|
+
def test_probe_default_timeout_none(self):
|
|
123
|
+
import inspect
|
|
124
|
+
self.assertIsNone(inspect.signature(ep.probe).parameters["timeout"].default)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
if __name__ == "__main__":
|
|
128
|
+
unittest.main()
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""task-134: full entity graph + migration v3 tests."""
|
|
3
|
+
import json
|
|
4
|
+
import tempfile
|
|
5
|
+
import types
|
|
6
|
+
import unittest
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from biocore_model import entity_graph as eg
|
|
10
|
+
from biocore_model import migrate, ui_settings
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _host(tmp, version=2):
|
|
14
|
+
data = Path(tmp) / "plugins" / "astrbot_plugin_biocore" / "data"
|
|
15
|
+
data.mkdir(parents=True, exist_ok=True)
|
|
16
|
+
(data / "ui_settings.json").write_text(
|
|
17
|
+
json.dumps({"schema": 1, "schema_version": version}),
|
|
18
|
+
encoding="utf-8")
|
|
19
|
+
host = types.SimpleNamespace(data_dir=str(data))
|
|
20
|
+
authority = Path(tmp) / "plugin_data" / "astrbot_plugin_biocore"
|
|
21
|
+
return host, authority
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class GraphModelTest(unittest.TestCase):
|
|
25
|
+
def test_empty_and_normalize_invalid(self):
|
|
26
|
+
graph = eg.empty_graph()
|
|
27
|
+
self.assertEqual(graph["schema"], 3)
|
|
28
|
+
self.assertEqual(eg.normalize_graph(None), graph)
|
|
29
|
+
self.assertEqual(eg.normalize_graph("x")["entities"], [])
|
|
30
|
+
dirty = {"schema": "x", "entities": [None, {"name": ""}, {"name": "x"}],
|
|
31
|
+
"edges": [None, {"src": "a", "dst": "b", "rel": "r"}]}
|
|
32
|
+
clean = eg.normalize_graph(dirty)
|
|
33
|
+
self.assertEqual(len(clean["entities"]), 1)
|
|
34
|
+
self.assertEqual(clean["edges"], []) # dangling edge dropped
|
|
35
|
+
|
|
36
|
+
def test_upsert_entity_fields_and_last_seen(self):
|
|
37
|
+
graph, eid = eg.upsert_entity(eg.empty_graph(), "小夏", type="person",
|
|
38
|
+
now=100.0)
|
|
39
|
+
row = eg.find_entity(graph, "小夏")
|
|
40
|
+
self.assertEqual(row["id"], eid)
|
|
41
|
+
self.assertEqual(row["type"], "person")
|
|
42
|
+
self.assertEqual(row["first_seen"], 100.0)
|
|
43
|
+
graph, eid2 = eg.upsert_entity(graph, "小夏", now=200.0)
|
|
44
|
+
self.assertEqual(eid, eid2)
|
|
45
|
+
row = eg.find_entity(graph, "小夏")
|
|
46
|
+
self.assertEqual(row["first_seen"], 100.0)
|
|
47
|
+
self.assertEqual(row["last_seen"], 200.0)
|
|
48
|
+
self.assertEqual(len(graph["entities"]), 1)
|
|
49
|
+
|
|
50
|
+
def test_dedupe_ignores_case_and_spaces(self):
|
|
51
|
+
graph, eid = eg.upsert_entity(eg.empty_graph(), "Xiao Xia")
|
|
52
|
+
graph, eid2 = eg.upsert_entity(graph, " xiao xia ")
|
|
53
|
+
self.assertEqual(eid, eid2)
|
|
54
|
+
self.assertEqual(len(graph["entities"]), 1)
|
|
55
|
+
|
|
56
|
+
def test_alias_merge(self):
|
|
57
|
+
graph, eid = eg.upsert_entity(eg.empty_graph(), "小夏",
|
|
58
|
+
aliases=["夏夏", "小夏"])
|
|
59
|
+
graph, eid2 = eg.upsert_entity(graph, "夏夏")
|
|
60
|
+
self.assertEqual(eid, eid2)
|
|
61
|
+
row = eg.find_entity(graph, "夏夏")
|
|
62
|
+
self.assertIn("夏夏", row["aliases"])
|
|
63
|
+
self.assertEqual(row["aliases"].count("夏夏"), 1)
|
|
64
|
+
self.assertNotIn("小夏", row["aliases"])
|
|
65
|
+
|
|
66
|
+
def test_upsert_edge_creates_entities(self):
|
|
67
|
+
graph, rid = eg.upsert_edge(eg.empty_graph(), "小夏", "planned", "体检",
|
|
68
|
+
ts=1000.0, confidence=0.8,
|
|
69
|
+
source_entry="m1")
|
|
70
|
+
self.assertTrue(rid)
|
|
71
|
+
edge = graph["edges"][0]
|
|
72
|
+
self.assertEqual(edge["rel"], "planned")
|
|
73
|
+
self.assertEqual(edge["ts"], 1000.0)
|
|
74
|
+
self.assertEqual(edge["confidence"], 0.8)
|
|
75
|
+
self.assertEqual(edge["source_entry"], "m1")
|
|
76
|
+
self.assertEqual(len(graph["entities"]), 2)
|
|
77
|
+
|
|
78
|
+
def test_edge_dedupe_same_ts_keeps_max_confidence(self):
|
|
79
|
+
graph, rid = eg.upsert_edge(eg.empty_graph(), "a", "knows", "b", ts=5.0,
|
|
80
|
+
confidence=0.4)
|
|
81
|
+
graph, rid2 = eg.upsert_edge(graph, "a", "knows", "b", ts=5.0,
|
|
82
|
+
confidence=0.9)
|
|
83
|
+
self.assertEqual(rid, rid2)
|
|
84
|
+
self.assertEqual(len(graph["edges"]), 1)
|
|
85
|
+
self.assertEqual(graph["edges"][0]["confidence"], 0.9)
|
|
86
|
+
|
|
87
|
+
def test_two_hop_neighbors(self):
|
|
88
|
+
graph = eg.empty_graph()
|
|
89
|
+
graph, _ = eg.upsert_edge(graph, "a", "r1", "b", ts=10.0)
|
|
90
|
+
graph, _ = eg.upsert_edge(graph, "b", "r2", "c", ts=11.0)
|
|
91
|
+
graph, _ = eg.upsert_edge(graph, "c", "r3", "d", ts=12.0)
|
|
92
|
+
two = eg.neighbors(graph, "a", hops=2)
|
|
93
|
+
names = {row["name"] for row in two["entities"]}
|
|
94
|
+
self.assertEqual(names, {"a", "b", "c"})
|
|
95
|
+
self.assertEqual(len(two["edges"]), 2)
|
|
96
|
+
one = eg.neighbors(graph, "a", hops=1)
|
|
97
|
+
self.assertEqual({row["name"] for row in one["entities"]}, {"a", "b"})
|
|
98
|
+
|
|
99
|
+
def test_neighbors_window_days(self):
|
|
100
|
+
graph = eg.empty_graph()
|
|
101
|
+
graph, _ = eg.upsert_edge(graph, "a", "old", "b", ts=100.0)
|
|
102
|
+
graph, _ = eg.upsert_edge(graph, "a", "new", "c", ts=900000.0)
|
|
103
|
+
now = 1000000.0
|
|
104
|
+
recent = eg.neighbors(graph, "a", hops=1, window_days=7, now=now)
|
|
105
|
+
self.assertEqual({row["name"] for row in recent["entities"]},
|
|
106
|
+
{"a", "c"})
|
|
107
|
+
self.assertEqual(len(recent["edges"]), 1)
|
|
108
|
+
all_time = eg.neighbors(graph, "a", hops=1, now=now)
|
|
109
|
+
self.assertEqual(len(all_time["edges"]), 2)
|
|
110
|
+
|
|
111
|
+
def test_neighbors_unknown_entity_is_empty(self):
|
|
112
|
+
self.assertEqual(eg.neighbors(eg.empty_graph(), "nobody")["entities"],
|
|
113
|
+
[])
|
|
114
|
+
|
|
115
|
+
def test_search_by_name_and_alias(self):
|
|
116
|
+
graph = eg.empty_graph()
|
|
117
|
+
graph, _ = eg.upsert_entity(graph, "小夏", aliases=["夏夏"])
|
|
118
|
+
graph, _ = eg.upsert_entity(graph, "海边计划")
|
|
119
|
+
names = [row["name"] for row in eg.search(graph, "夏夏说海边计划真好")]
|
|
120
|
+
self.assertIn("小夏", names)
|
|
121
|
+
self.assertIn("海边计划", names)
|
|
122
|
+
self.assertEqual(eg.search(graph, ""), [])
|
|
123
|
+
|
|
124
|
+
def test_stats(self):
|
|
125
|
+
graph = eg.empty_graph()
|
|
126
|
+
graph, _ = eg.upsert_entity(graph, "a", type="person", aliases=["AA"])
|
|
127
|
+
graph, _ = eg.upsert_edge(graph, "a", "r", "b", ts=42.0)
|
|
128
|
+
out = eg.stats(graph)
|
|
129
|
+
self.assertEqual(out["entities"], 2)
|
|
130
|
+
self.assertEqual(out["edges"], 1)
|
|
131
|
+
self.assertEqual(out["types"].get("person"), 1)
|
|
132
|
+
self.assertEqual(out["aliases"], 1)
|
|
133
|
+
self.assertEqual(out["latest_ts"], 42.0)
|
|
134
|
+
|
|
135
|
+
def test_save_load_roundtrip_and_corrupt_recovery(self):
|
|
136
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
137
|
+
path = Path(tmp) / "graph" / "graph.json"
|
|
138
|
+
graph = eg.empty_graph()
|
|
139
|
+
graph, _ = eg.upsert_edge(graph, "a", "r", "b", ts=1.0)
|
|
140
|
+
eg.save_graph(path, graph)
|
|
141
|
+
loaded = eg.load_graph(path)
|
|
142
|
+
self.assertEqual(len(loaded["entities"]), 2)
|
|
143
|
+
self.assertEqual(len(loaded["edges"]), 1)
|
|
144
|
+
self.assertFalse((path.parent / "graph.json.tmp").exists())
|
|
145
|
+
path.write_text("{not json", encoding="utf-8")
|
|
146
|
+
recovered = eg.load_graph(path)
|
|
147
|
+
self.assertEqual(recovered["entities"], [])
|
|
148
|
+
self.assertTrue((path.parent / "graph.json.corrupt.bak").is_file())
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
class MigrationV3Test(unittest.TestCase):
|
|
152
|
+
def test_migrate_creates_graph_and_is_idempotent(self):
|
|
153
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
154
|
+
host, authority = _host(tmp, version=2)
|
|
155
|
+
report = migrate.migrate(host)
|
|
156
|
+
graph_path = authority / "graph" / "graph.json"
|
|
157
|
+
self.assertTrue(graph_path.is_file())
|
|
158
|
+
payload = json.loads(graph_path.read_text(encoding="utf-8"))
|
|
159
|
+
self.assertEqual(payload["schema"], 3)
|
|
160
|
+
self.assertEqual(report["stats"].get("graph"), "created")
|
|
161
|
+
before = graph_path.read_bytes()
|
|
162
|
+
second = migrate.migrate(host)
|
|
163
|
+
self.assertTrue(second["stats"].get("already"))
|
|
164
|
+
self.assertEqual(graph_path.read_bytes(), before)
|
|
165
|
+
|
|
166
|
+
def test_migrate_recovers_corrupt_graph(self):
|
|
167
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
168
|
+
host, authority = _host(tmp, version=2)
|
|
169
|
+
graph_dir = authority / "graph"
|
|
170
|
+
graph_dir.mkdir(parents=True, exist_ok=True)
|
|
171
|
+
(graph_dir / "graph.json").write_text("broken{", encoding="utf-8")
|
|
172
|
+
report = migrate.migrate(host)
|
|
173
|
+
self.assertEqual(report["stats"].get("graph"), "recovered")
|
|
174
|
+
payload = json.loads((graph_dir / "graph.json").read_text(
|
|
175
|
+
encoding="utf-8"))
|
|
176
|
+
self.assertEqual(payload["schema"], 3)
|
|
177
|
+
self.assertTrue((graph_dir / "graph.json.corrupt.bak").is_file())
|
|
178
|
+
|
|
179
|
+
def test_backup_and_restore_include_graph(self):
|
|
180
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
181
|
+
host, authority = _host(tmp, version=2)
|
|
182
|
+
graph_dir = authority / "graph"
|
|
183
|
+
graph_dir.mkdir(parents=True, exist_ok=True)
|
|
184
|
+
seed = eg.empty_graph()
|
|
185
|
+
seed, _ = eg.upsert_edge(seed, "a", "r", "b", ts=1.0)
|
|
186
|
+
eg.save_graph(graph_dir / "graph.json", seed)
|
|
187
|
+
report = migrate.migrate(host)
|
|
188
|
+
backup = Path(report["backup_dir"])
|
|
189
|
+
self.assertTrue((backup / "graph" / "graph.json").is_file())
|
|
190
|
+
(graph_dir / "graph.json").write_text("{}", encoding="utf-8")
|
|
191
|
+
result = migrate.restore(str(backup), host)
|
|
192
|
+
self.assertGreaterEqual(result["restored"]["graph"], 1)
|
|
193
|
+
restored = eg.load_graph(graph_dir / "graph.json")
|
|
194
|
+
self.assertEqual(len(restored["edges"]), 1)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
if __name__ == "__main__":
|
|
198
|
+
unittest.main()
|