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,226 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""task-136: graph recall, time window, "你知道的" render, panel data tests."""
|
|
3
|
+
import tempfile
|
|
4
|
+
import unittest
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from biocore_model import entity_graph as eg
|
|
8
|
+
from biocore_model import graph_extract as gx
|
|
9
|
+
from biocore_model import graph_recall as gr
|
|
10
|
+
|
|
11
|
+
NOW = 1704844800.0
|
|
12
|
+
DAY = 86400.0
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _store(tmp):
|
|
16
|
+
return eg.open_store(Path(tmp) / "graph" / eg.SQLITE_FILE)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _seed(store, now=NOW):
|
|
20
|
+
eg.upsert_edge(store, "小夏", "afraid_of", "打雷", ts=now - 2 * DAY,
|
|
21
|
+
confidence=0.9, source_entry="m1")
|
|
22
|
+
eg.upsert_edge(store, "小夏", "planned", "海边", ts=now - DAY,
|
|
23
|
+
confidence=0.8, source_entry="m2")
|
|
24
|
+
eg.upsert_edge(store, "周末", "planned", "海边", ts=now - DAY,
|
|
25
|
+
confidence=0.6, source_entry="m3")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class WindowTest(unittest.TestCase):
|
|
29
|
+
def test_detect_window_words(self):
|
|
30
|
+
self.assertEqual(gr.detect_window("上周去了吗"), (7, "上周"))
|
|
31
|
+
self.assertEqual(gr.detect_window("昨天呢"), (1, "昨天"))
|
|
32
|
+
self.assertEqual(gr.detect_window("今天如何"), (None, ""))
|
|
33
|
+
self.assertEqual(gr.detect_window(""), (None, ""))
|
|
34
|
+
|
|
35
|
+
def test_window_filters_old_edges(self):
|
|
36
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
37
|
+
store = _store(tmp)
|
|
38
|
+
try:
|
|
39
|
+
eg.upsert_edge(store, "海边", "located_at", "旧码头",
|
|
40
|
+
ts=NOW - 30 * DAY, confidence=0.9)
|
|
41
|
+
eg.upsert_edge(store, "海边", "planned", "新行程",
|
|
42
|
+
ts=NOW - DAY, confidence=0.6)
|
|
43
|
+
recent = gr.recall(store, "上周去海边", now=NOW)
|
|
44
|
+
self.assertTrue(recent)
|
|
45
|
+
for row in recent:
|
|
46
|
+
self.assertGreaterEqual(row["ts"], NOW - 7 * DAY)
|
|
47
|
+
all_time = gr.recall(store, "海边", now=NOW)
|
|
48
|
+
self.assertTrue(any("旧码头" in row["line"] for row in all_time))
|
|
49
|
+
finally:
|
|
50
|
+
store.close()
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class RecallTest(unittest.TestCase):
|
|
54
|
+
def test_two_hop_association(self):
|
|
55
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
56
|
+
store = _store(tmp)
|
|
57
|
+
try:
|
|
58
|
+
_seed(store)
|
|
59
|
+
one = gr.recall(store, "海边", now=NOW, hops=1)
|
|
60
|
+
self.assertFalse(any("打雷" in row["line"] for row in one))
|
|
61
|
+
two = gr.recall(store, "海边", now=NOW, hops=2)
|
|
62
|
+
self.assertTrue(any("打雷" in row["line"] for row in two))
|
|
63
|
+
finally:
|
|
64
|
+
store.close()
|
|
65
|
+
|
|
66
|
+
def test_no_entity_match_is_empty(self):
|
|
67
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
68
|
+
store = _store(tmp)
|
|
69
|
+
try:
|
|
70
|
+
_seed(store)
|
|
71
|
+
self.assertEqual(gr.recall(store, "今天吃什么", now=NOW), [])
|
|
72
|
+
self.assertEqual(gr.render(store, "今天吃什么", now=NOW), "")
|
|
73
|
+
finally:
|
|
74
|
+
store.close()
|
|
75
|
+
|
|
76
|
+
def test_render_lines_and_cap(self):
|
|
77
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
78
|
+
store = _store(tmp)
|
|
79
|
+
try:
|
|
80
|
+
_seed(store)
|
|
81
|
+
for index in range(8):
|
|
82
|
+
eg.upsert_edge(store, "海边", "topic_%d" % index,
|
|
83
|
+
"话题节点%d" % index,
|
|
84
|
+
ts=NOW - index * 3600., confidence=0.5)
|
|
85
|
+
text = gr.render(store, "海边还去吗", now=NOW)
|
|
86
|
+
self.assertTrue(text.startswith("你知道的:"))
|
|
87
|
+
bullets = [line for line in text.splitlines()
|
|
88
|
+
if line.startswith("- ")]
|
|
89
|
+
self.assertLessEqual(len(bullets), gr.MAX_LINES)
|
|
90
|
+
self.assertLessEqual(len(text.splitlines()), gr.MAX_LINES + 1)
|
|
91
|
+
finally:
|
|
92
|
+
store.close()
|
|
93
|
+
|
|
94
|
+
def test_ranking_prefers_recent_and_confident(self):
|
|
95
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
96
|
+
store = _store(tmp)
|
|
97
|
+
try:
|
|
98
|
+
eg.upsert_edge(store, "海边", "planned", "旧计划",
|
|
99
|
+
ts=NOW - 20 * DAY, confidence=0.2)
|
|
100
|
+
eg.upsert_edge(store, "海边", "planned", "新计划",
|
|
101
|
+
ts=NOW - 3600.0, confidence=0.9)
|
|
102
|
+
rows = gr.recall(store, "海边", now=NOW)
|
|
103
|
+
self.assertIn("新计划", rows[0]["line"])
|
|
104
|
+
finally:
|
|
105
|
+
store.close()
|
|
106
|
+
|
|
107
|
+
def test_cross_entry_after_twenty_dialogues(self):
|
|
108
|
+
bags = [
|
|
109
|
+
[{"name": "小夏", "type": "人物"}, {"name": "打雷", "type": "话题"}],
|
|
110
|
+
[{"name": "小夏", "type": "人物"}, {"name": "海边", "type": "地点"}],
|
|
111
|
+
[{"name": "周末", "type": "事件"}, {"name": "海边", "type": "地点"}],
|
|
112
|
+
[{"name": "雨伞", "type": "话题"}, {"name": "下雨", "type": "事件"}],
|
|
113
|
+
]
|
|
114
|
+
relations = [
|
|
115
|
+
("小夏", "afraid_of", "打雷", 2),
|
|
116
|
+
("小夏", "planned", "海边", 1),
|
|
117
|
+
("周末", "planned", "海边", 1),
|
|
118
|
+
("下雨", "blocks", "海边", 0),
|
|
119
|
+
]
|
|
120
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
121
|
+
store = _store(tmp)
|
|
122
|
+
try:
|
|
123
|
+
for turn in range(20):
|
|
124
|
+
tag = {"entities": bags[turn % len(bags)],
|
|
125
|
+
"relations": [], "confidence": 0.8}
|
|
126
|
+
extracted = gx.extract(tag, now=NOW)
|
|
127
|
+
gx.apply(store, extracted, now=NOW, source_entry="m%d" % turn)
|
|
128
|
+
for src, rel, dst, days_ago in relations:
|
|
129
|
+
eg.upsert_edge(store, src, rel, dst,
|
|
130
|
+
ts=NOW - days_ago * DAY, confidence=0.8,
|
|
131
|
+
source_entry="r")
|
|
132
|
+
text = gr.render(store, "下雨了海边还去吗", now=NOW)
|
|
133
|
+
self.assertIn("你知道的:", text)
|
|
134
|
+
self.assertIn("打雷", text)
|
|
135
|
+
bullets = [line for line in text.splitlines()
|
|
136
|
+
if line.startswith("- ")]
|
|
137
|
+
self.assertLessEqual(len(bullets), 5)
|
|
138
|
+
finally:
|
|
139
|
+
store.close()
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class TimelinePanelTest(unittest.TestCase):
|
|
143
|
+
def test_timeline_newest_first(self):
|
|
144
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
145
|
+
store = _store(tmp)
|
|
146
|
+
try:
|
|
147
|
+
_seed(store)
|
|
148
|
+
rows = gr.timeline(store, "小夏", now=NOW)
|
|
149
|
+
self.assertGreaterEqual(len(rows), 2)
|
|
150
|
+
self.assertGreaterEqual(rows[0]["ts"], rows[-1]["ts"])
|
|
151
|
+
self.assertIn("—", rows[0]["text"])
|
|
152
|
+
finally:
|
|
153
|
+
store.close()
|
|
154
|
+
|
|
155
|
+
def test_timeline_window(self):
|
|
156
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
157
|
+
store = _store(tmp)
|
|
158
|
+
try:
|
|
159
|
+
eg.upsert_edge(store, "小夏", "old_rel", "旧事",
|
|
160
|
+
ts=NOW - 40 * DAY)
|
|
161
|
+
rows = gr.timeline(store, "小夏", now=NOW, window_days=7)
|
|
162
|
+
self.assertTrue(all(row["ts"] >= NOW - 7 * DAY for row in rows))
|
|
163
|
+
finally:
|
|
164
|
+
store.close()
|
|
165
|
+
|
|
166
|
+
def test_panel_data_entities_and_edges(self):
|
|
167
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
168
|
+
store = _store(tmp)
|
|
169
|
+
try:
|
|
170
|
+
_seed(store)
|
|
171
|
+
data = gr.panel_data(store, now=NOW)
|
|
172
|
+
self.assertEqual(data["stats"]["entities"], 4)
|
|
173
|
+
self.assertEqual(data["stats"]["edges"], 3)
|
|
174
|
+
names = [row["name"] for row in data["entities"]]
|
|
175
|
+
self.assertIn("小夏", names)
|
|
176
|
+
person = [row for row in data["entities"] if row["name"] == "小夏"][0]
|
|
177
|
+
self.assertEqual(person["type"], "topic")
|
|
178
|
+
self.assertEqual(person["type_label"], "话题")
|
|
179
|
+
edge = [row for row in data["edges"] if row["rel"] == "planned"][0]
|
|
180
|
+
self.assertEqual(edge["rel_label"], "计划")
|
|
181
|
+
self.assertIn(edge["src_name"], ("小夏", "周末"))
|
|
182
|
+
self.assertTrue(edge["when"])
|
|
183
|
+
finally:
|
|
184
|
+
store.close()
|
|
185
|
+
|
|
186
|
+
def test_source_entries(self):
|
|
187
|
+
rows = [{"source_entry": "m1"}, {"source_entry": "m1"},
|
|
188
|
+
{"source_entry": "m2"}, {"source_entry": ""}]
|
|
189
|
+
self.assertEqual(gr.source_entries(rows), ["m1", "m2"])
|
|
190
|
+
|
|
191
|
+
def test_panel_data_empty_db(self):
|
|
192
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
193
|
+
store = _store(tmp)
|
|
194
|
+
try:
|
|
195
|
+
data = gr.panel_data(store, now=NOW)
|
|
196
|
+
self.assertEqual(data["entities"], [])
|
|
197
|
+
self.assertEqual(data["edges"], [])
|
|
198
|
+
self.assertEqual(data["stats"]["entities"], 0)
|
|
199
|
+
finally:
|
|
200
|
+
store.close()
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class WiringStaticTest(unittest.TestCase):
|
|
204
|
+
def test_preset_and_main_have_graph_block(self):
|
|
205
|
+
repo = Path(__file__).resolve().parents[2]
|
|
206
|
+
preset_src = (repo / "biocore_model" / "preset.py").read_text(
|
|
207
|
+
encoding="utf-8")
|
|
208
|
+
main_src = (repo / "main.py").read_text(encoding="utf-8")
|
|
209
|
+
self.assertIn('"graph_memory"', preset_src)
|
|
210
|
+
self.assertIn("graph_recall", main_src)
|
|
211
|
+
self.assertIn("/astrbot_plugin_biocore/page/graph", main_src)
|
|
212
|
+
self.assertIn("async def page_graph", main_src)
|
|
213
|
+
graph_ui = repo / "pages" / "dashboard" / "graph.js"
|
|
214
|
+
self.assertTrue(graph_ui.is_file())
|
|
215
|
+
ui = graph_ui.read_text(encoding="utf-8")
|
|
216
|
+
self.assertIn("page/graph", ui)
|
|
217
|
+
self.assertIn("registerPage", ui)
|
|
218
|
+
self.assertIn("id: 'graph'", ui)
|
|
219
|
+
built = (repo / "pages" / "dashboard" / "index.html").read_text(
|
|
220
|
+
encoding="utf-8")
|
|
221
|
+
self.assertIn('id="view-graph"', built)
|
|
222
|
+
self.assertIn("__ui3GraphInstalled", built)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
if __name__ == "__main__":
|
|
226
|
+
unittest.main()
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""task-134 addendum: SQLite graph storage tests (temp dirs only)."""
|
|
3
|
+
import json
|
|
4
|
+
import sqlite3
|
|
5
|
+
import tempfile
|
|
6
|
+
import types
|
|
7
|
+
import unittest
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from biocore_model import entity_graph as eg
|
|
11
|
+
from biocore_model import migrate
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _store(tmp, name="graph"):
|
|
15
|
+
return eg.open_store(Path(tmp) / name / eg.SQLITE_FILE)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class SqliteSchemaTest(unittest.TestCase):
|
|
19
|
+
def test_tables_indexes_and_pragmas(self):
|
|
20
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
21
|
+
store = _store(tmp)
|
|
22
|
+
try:
|
|
23
|
+
tables = {row[0] for row in store._conn.execute(
|
|
24
|
+
"SELECT name FROM sqlite_master WHERE type='table'")}
|
|
25
|
+
indexes = {row[0] for row in store._conn.execute(
|
|
26
|
+
"SELECT name FROM sqlite_master WHERE type='index'")}
|
|
27
|
+
self.assertIn("entities", tables)
|
|
28
|
+
self.assertIn("edges", tables)
|
|
29
|
+
for key in ("idx_entities_name", "idx_edges_src",
|
|
30
|
+
"idx_edges_dst", "idx_edges_ts", "idx_edges_rel"):
|
|
31
|
+
self.assertIn(key, indexes)
|
|
32
|
+
self.assertEqual(store._conn.execute(
|
|
33
|
+
"PRAGMA journal_mode").fetchone()[0].lower(), "wal")
|
|
34
|
+
self.assertEqual(store._conn.execute(
|
|
35
|
+
"PRAGMA synchronous").fetchone()[0], 1) # NORMAL
|
|
36
|
+
self.assertEqual(store._conn.execute(
|
|
37
|
+
"PRAGMA busy_timeout").fetchone()[0], 5000)
|
|
38
|
+
self.assertTrue(store.path.is_file())
|
|
39
|
+
finally:
|
|
40
|
+
store.close()
|
|
41
|
+
|
|
42
|
+
def test_entity_crud_and_fields(self):
|
|
43
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
44
|
+
store = _store(tmp)
|
|
45
|
+
try:
|
|
46
|
+
store, eid = eg.upsert_entity(store, "小夏", type="person",
|
|
47
|
+
aliases=["夏夏"], now=100.0,
|
|
48
|
+
entry_id="m1")
|
|
49
|
+
found = eg.search(store, "夏夏来了")
|
|
50
|
+
self.assertEqual([row["name"] for row in found], ["小夏"])
|
|
51
|
+
row = store._find_entity("小夏")
|
|
52
|
+
self.assertEqual(row["id"], eid)
|
|
53
|
+
self.assertEqual(row["type"], "person")
|
|
54
|
+
self.assertEqual(row["first_seen"], 100.0)
|
|
55
|
+
self.assertIn("夏夏", row["aliases"])
|
|
56
|
+
self.assertEqual(row["sources"], ["m1"])
|
|
57
|
+
finally:
|
|
58
|
+
store.close()
|
|
59
|
+
|
|
60
|
+
def test_entity_dedupe_normalized(self):
|
|
61
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
62
|
+
store = _store(tmp)
|
|
63
|
+
try:
|
|
64
|
+
_, e1 = eg.upsert_entity(store, "Xiao Xia")
|
|
65
|
+
_, e2 = eg.upsert_entity(store, " xiao xia ")
|
|
66
|
+
self.assertEqual(e1, e2)
|
|
67
|
+
self.assertEqual(eg.stats(store)["entities"], 1)
|
|
68
|
+
finally:
|
|
69
|
+
store.close()
|
|
70
|
+
|
|
71
|
+
def test_alias_merge(self):
|
|
72
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
73
|
+
store = _store(tmp)
|
|
74
|
+
try:
|
|
75
|
+
_, e1 = eg.upsert_entity(store, "小夏", aliases=["夏夏"])
|
|
76
|
+
_, e2 = eg.upsert_entity(store, "夏夏", type="person")
|
|
77
|
+
self.assertEqual(e1, e2)
|
|
78
|
+
row = store._find_entity("小夏")
|
|
79
|
+
self.assertEqual(row["aliases"].count("夏夏"), 1)
|
|
80
|
+
self.assertEqual(row["type"], "person")
|
|
81
|
+
finally:
|
|
82
|
+
store.close()
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class SqliteQueryTest(unittest.TestCase):
|
|
86
|
+
def test_edge_crud_and_dedupe(self):
|
|
87
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
88
|
+
store = _store(tmp)
|
|
89
|
+
try:
|
|
90
|
+
store, r1 = eg.upsert_edge(store, "a", "knows", "b", ts=5.0,
|
|
91
|
+
confidence=0.4)
|
|
92
|
+
store, r2 = eg.upsert_edge(store, "a", "knows", "b", ts=5.0,
|
|
93
|
+
confidence=0.9, source_entry="m2")
|
|
94
|
+
self.assertEqual(r1, r2)
|
|
95
|
+
stats = eg.stats(store)
|
|
96
|
+
self.assertEqual(stats["edges"], 1)
|
|
97
|
+
row = store._conn.execute(
|
|
98
|
+
"SELECT confidence, source_entry FROM edges").fetchone()
|
|
99
|
+
self.assertEqual(row[0], 0.9)
|
|
100
|
+
self.assertEqual(row[1], "m2")
|
|
101
|
+
finally:
|
|
102
|
+
store.close()
|
|
103
|
+
|
|
104
|
+
def test_two_hop_cte(self):
|
|
105
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
106
|
+
store = _store(tmp)
|
|
107
|
+
try:
|
|
108
|
+
eg.upsert_edge(store, "a", "r1", "b", ts=10.0)
|
|
109
|
+
eg.upsert_edge(store, "b", "r2", "c", ts=11.0)
|
|
110
|
+
eg.upsert_edge(store, "c", "r3", "d", ts=12.0)
|
|
111
|
+
two = eg.neighbors(store, "a", hops=2)
|
|
112
|
+
self.assertEqual({row["name"] for row in two["entities"]},
|
|
113
|
+
{"a", "b", "c"})
|
|
114
|
+
self.assertEqual(len(two["edges"]), 2)
|
|
115
|
+
one = eg.neighbors(store, "a", hops=1)
|
|
116
|
+
self.assertEqual({row["name"] for row in one["entities"]},
|
|
117
|
+
{"a", "b"})
|
|
118
|
+
finally:
|
|
119
|
+
store.close()
|
|
120
|
+
|
|
121
|
+
def test_time_window(self):
|
|
122
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
123
|
+
store = _store(tmp)
|
|
124
|
+
try:
|
|
125
|
+
eg.upsert_edge(store, "a", "old", "b", ts=100.0)
|
|
126
|
+
eg.upsert_edge(store, "a", "new", "c", ts=900000.0)
|
|
127
|
+
now = 1000000.0
|
|
128
|
+
recent = eg.neighbors(store, "a", hops=1, window_days=7,
|
|
129
|
+
now=now)
|
|
130
|
+
self.assertEqual({row["name"] for row in recent["entities"]},
|
|
131
|
+
{"a", "c"})
|
|
132
|
+
self.assertEqual(len(recent["edges"]), 1)
|
|
133
|
+
self.assertEqual(len(eg.neighbors(store, "a", hops=1,
|
|
134
|
+
now=now)["edges"]), 2)
|
|
135
|
+
finally:
|
|
136
|
+
store.close()
|
|
137
|
+
|
|
138
|
+
def test_unknown_entity_empty(self):
|
|
139
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
140
|
+
store = _store(tmp)
|
|
141
|
+
try:
|
|
142
|
+
self.assertEqual(eg.neighbors(store, "nobody")["entities"], [])
|
|
143
|
+
self.assertEqual(eg.search(store, "nobody"), [])
|
|
144
|
+
finally:
|
|
145
|
+
store.close()
|
|
146
|
+
|
|
147
|
+
def test_search_length_priority(self):
|
|
148
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
149
|
+
store = _store(tmp)
|
|
150
|
+
try:
|
|
151
|
+
eg.upsert_entity(store, "夏")
|
|
152
|
+
eg.upsert_entity(store, "小夏")
|
|
153
|
+
names = [row["name"] for row in eg.search(store, "小夏在吗")]
|
|
154
|
+
self.assertEqual(names[0], "小夏")
|
|
155
|
+
self.assertIn("夏", names)
|
|
156
|
+
finally:
|
|
157
|
+
store.close()
|
|
158
|
+
|
|
159
|
+
def test_stats_aggregation(self):
|
|
160
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
161
|
+
store = _store(tmp)
|
|
162
|
+
try:
|
|
163
|
+
eg.upsert_entity(store, "a", type="person", aliases=["AA"])
|
|
164
|
+
eg.upsert_edge(store, "a", "r", "b", ts=42.0)
|
|
165
|
+
stats = eg.stats(store)
|
|
166
|
+
self.assertEqual(stats["entities"], 2)
|
|
167
|
+
self.assertEqual(stats["edges"], 1)
|
|
168
|
+
self.assertEqual(stats["types"].get("person"), 1)
|
|
169
|
+
self.assertEqual(stats["aliases"], 1)
|
|
170
|
+
self.assertEqual(stats["latest_ts"], 42.0)
|
|
171
|
+
finally:
|
|
172
|
+
store.close()
|
|
173
|
+
|
|
174
|
+
def test_corrupt_db_rebuild(self):
|
|
175
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
176
|
+
path = Path(tmp) / "graph" / eg.SQLITE_FILE
|
|
177
|
+
path.parent.mkdir(parents=True)
|
|
178
|
+
path.write_bytes(b"not a sqlite database")
|
|
179
|
+
store = eg.open_store(path)
|
|
180
|
+
try:
|
|
181
|
+
self.assertEqual(eg.stats(store)["entities"], 0)
|
|
182
|
+
eg.upsert_entity(store, "x", now=1.0)
|
|
183
|
+
self.assertEqual(eg.stats(store)["entities"], 1)
|
|
184
|
+
self.assertTrue(path.with_name(path.name + ".corrupt.bak")
|
|
185
|
+
.is_file())
|
|
186
|
+
finally:
|
|
187
|
+
store.close()
|
|
188
|
+
|
|
189
|
+
def test_export_import_roundtrip(self):
|
|
190
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
191
|
+
src = _store(tmp, "a")
|
|
192
|
+
try:
|
|
193
|
+
eg.upsert_edge(src, "a", "r", "b", ts=7.0, confidence=0.7)
|
|
194
|
+
json_path = Path(tmp) / "a" / "graph.json"
|
|
195
|
+
src.export_json(json_path)
|
|
196
|
+
payload = json.loads(json_path.read_text(encoding="utf-8"))
|
|
197
|
+
self.assertEqual(payload["schema"], 3)
|
|
198
|
+
self.assertEqual(len(payload["edges"]), 1)
|
|
199
|
+
finally:
|
|
200
|
+
src.close()
|
|
201
|
+
dst = _store(tmp, "b")
|
|
202
|
+
try:
|
|
203
|
+
imported = dst.import_json(Path(tmp) / "a" / "graph.json")
|
|
204
|
+
self.assertEqual(imported["edges"], 1)
|
|
205
|
+
self.assertEqual(eg.stats(dst)["edges"], 1)
|
|
206
|
+
finally:
|
|
207
|
+
dst.close()
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def _host(tmp, version=2):
|
|
211
|
+
data = Path(tmp) / "plugins" / "astrbot_plugin_biocore" / "data"
|
|
212
|
+
data.mkdir(parents=True, exist_ok=True)
|
|
213
|
+
(data / "ui_settings.json").write_text(
|
|
214
|
+
json.dumps({"schema": 1, "schema_version": version}),
|
|
215
|
+
encoding="utf-8")
|
|
216
|
+
host = types.SimpleNamespace(data_dir=str(data))
|
|
217
|
+
authority = Path(tmp) / "plugin_data" / "astrbot_plugin_biocore"
|
|
218
|
+
return host, authority
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
class MigrationSqliteTest(unittest.TestCase):
|
|
222
|
+
def test_migrate_imports_legacy_json(self):
|
|
223
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
224
|
+
host, authority = _host(tmp)
|
|
225
|
+
graph_dir = authority / "graph"
|
|
226
|
+
graph_dir.mkdir(parents=True)
|
|
227
|
+
seed = eg.empty_graph()
|
|
228
|
+
seed, _ = eg.upsert_edge(seed, "小夏", "planned", "体检", ts=9.0)
|
|
229
|
+
eg.save_graph(graph_dir / "graph.json", seed)
|
|
230
|
+
report = migrate.migrate(host)
|
|
231
|
+
self.assertEqual(report["stats"].get("graph"), "imported")
|
|
232
|
+
store = eg.open_store(graph_dir / eg.SQLITE_FILE)
|
|
233
|
+
try:
|
|
234
|
+
self.assertEqual(eg.stats(store)["edges"], 1)
|
|
235
|
+
finally:
|
|
236
|
+
store.close()
|
|
237
|
+
self.assertTrue((graph_dir / "graph.json").is_file())
|
|
238
|
+
|
|
239
|
+
def test_migrate_is_idempotent_with_sqlite(self):
|
|
240
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
241
|
+
host, authority = _host(tmp)
|
|
242
|
+
migrate.migrate(host)
|
|
243
|
+
graph_dir = authority / "graph"
|
|
244
|
+
db = graph_dir / eg.SQLITE_FILE
|
|
245
|
+
before = db.read_bytes()
|
|
246
|
+
second = migrate.migrate(host)
|
|
247
|
+
self.assertTrue(second["stats"].get("already"))
|
|
248
|
+
self.assertEqual(db.read_bytes(), before)
|
|
249
|
+
|
|
250
|
+
def test_migrate_recovers_corrupt_db(self):
|
|
251
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
252
|
+
host, authority = _host(tmp)
|
|
253
|
+
graph_dir = authority / "graph"
|
|
254
|
+
graph_dir.mkdir(parents=True)
|
|
255
|
+
(graph_dir / eg.SQLITE_FILE).write_bytes(b"garbage-db")
|
|
256
|
+
report = migrate.migrate(host)
|
|
257
|
+
self.assertEqual(report["stats"].get("graph"), "recovered")
|
|
258
|
+
self.assertTrue((graph_dir / (eg.SQLITE_FILE + ".corrupt.bak"))
|
|
259
|
+
.is_file())
|
|
260
|
+
store = eg.open_store(graph_dir / eg.SQLITE_FILE)
|
|
261
|
+
try:
|
|
262
|
+
self.assertEqual(eg.stats(store)["entities"], 0)
|
|
263
|
+
finally:
|
|
264
|
+
store.close()
|
|
265
|
+
|
|
266
|
+
def test_backup_and_restore_sqlite_graph(self):
|
|
267
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
268
|
+
host, authority = _host(tmp)
|
|
269
|
+
migrate.migrate(host)
|
|
270
|
+
graph_dir = authority / "graph"
|
|
271
|
+
store = eg.open_store(graph_dir / eg.SQLITE_FILE)
|
|
272
|
+
try:
|
|
273
|
+
eg.upsert_edge(store, "a", "r", "b", ts=1.0)
|
|
274
|
+
store.checkpoint()
|
|
275
|
+
finally:
|
|
276
|
+
store.close()
|
|
277
|
+
# force a second migration to produce a fresh backup with the graph
|
|
278
|
+
from biocore_model import ui_settings
|
|
279
|
+
auth = authority / ui_settings.FILE_NAME
|
|
280
|
+
settings = json.loads(auth.read_text(encoding="utf-8"))
|
|
281
|
+
settings["schema_version"] = 2
|
|
282
|
+
auth.write_text(json.dumps(settings), encoding="utf-8")
|
|
283
|
+
report = migrate.migrate(host)
|
|
284
|
+
backup = Path(report["backup_dir"])
|
|
285
|
+
self.assertTrue((backup / "graph" / eg.SQLITE_FILE).is_file())
|
|
286
|
+
self.assertTrue((backup / "graph" / "graph.json").is_file())
|
|
287
|
+
(graph_dir / eg.SQLITE_FILE).write_bytes(b"broken")
|
|
288
|
+
result = migrate.restore(str(backup), host)
|
|
289
|
+
self.assertGreaterEqual(result["restored"]["graph"], 1)
|
|
290
|
+
store = eg.open_store(graph_dir / eg.SQLITE_FILE)
|
|
291
|
+
try:
|
|
292
|
+
self.assertEqual(eg.stats(store)["edges"], 1)
|
|
293
|
+
finally:
|
|
294
|
+
store.close()
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
if __name__ == "__main__":
|
|
298
|
+
unittest.main()
|