lingshu-skill 5.33.0 → 5.37.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/CHANGELOG.md +4281 -4227
- package/README.md +1584 -1577
- package/SKILL-lite.md +59 -59
- package/SKILL-tiny.md +38 -38
- package/SKILL.md +356 -331
- package/assets/panel.html +6753 -5698
- package/cordis-plugin/lingshu-cron/plugin.js +1455 -1417
- package/cordis-plugin/lingshu-startup/context-fold.js +170 -0
- package/cordis-plugin/lingshu-startup/context-pressure.js +16 -0
- package/cordis-plugin/lingshu-startup/context-snapshot.js +176 -0
- package/cordis-plugin/lingshu-startup/plugin.js +499 -467
- package/docs/audit_notes//345/276/205/345/212/236-20261201-/351/235/242/346/235/277/344/270/245/346/237/245/344/270/216/345/256/241/350/256/241.md +162 -0
- package/docs/audit_notes//345/276/205/345/212/236-20261202-/351/235/242/346/235/277/344/272/244/344/272/222/344/270/216/346/225/260/346/215/256/345/245/221/347/272/246/345/256/241/350/256/241.md +92 -0
- package/docs/audit_notes//345/276/205/345/212/236/346/250/241/346/235/277.md +140 -0
- package/docs/commands.md +278 -247
- package/docs/mechanics.md +610 -610
- package/docs//345/276/205/345/212/236-20261201-/351/235/242/346/235/277/344/270/245/346/237/245/344/270/216/345/256/241/350/256/241.md +162 -0
- package/docs//345/276/205/345/212/236-20261202-/351/235/242/346/235/277/344/272/244/344/272/222/344/270/216/346/225/260/346/215/256/345/245/221/347/272/246/345/256/241/350/256/241.md +92 -0
- package/package.json +52 -52
- package/scripts/_scratch/_tmp_backup_race.py +70 -0
- package/scripts/_scratch/_tmp_cleanup_move.py +34 -0
- package/scripts/_scratch/_tmp_count_kinds.py +25 -0
- package/scripts/_scratch/_tmp_diag_paths.py +48 -0
- package/scripts/_scratch/_tmp_facts.py +63 -0
- package/scripts/_scratch/_tmp_find_copies.py +31 -0
- package/scripts/_scratch/_tmp_find_pin.py +29 -0
- package/scripts/_scratch/_tmp_fix_pin.py +36 -0
- package/scripts/_scratch/_tmp_fold_fixture.py +55 -0
- package/scripts/_scratch/_tmp_fold_test.mjs +81 -0
- package/scripts/_scratch/_tmp_inject_secs.py +30 -0
- package/scripts/_scratch/_tmp_kb_balance.py +159 -0
- package/scripts/_scratch/_tmp_lines.py +15 -0
- package/scripts/_scratch/_tmp_market_probe.py +13 -0
- package/scripts/_scratch/_tmp_move_partial.py +16 -0
- package/scripts/_scratch/_tmp_patch_b3.py +41 -0
- package/scripts/_scratch/_tmp_patch_b5.py +62 -0
- package/scripts/_scratch/_tmp_patch_crash_excl.py +24 -0
- package/scripts/_scratch/_tmp_patch_handoff.py +21 -0
- package/scripts/_scratch/_tmp_patch_ps1.py +25 -0
- package/scripts/_scratch/_tmp_patch_stat.py +27 -0
- package/scripts/_scratch/_tmp_patch_todo.py +25 -0
- package/scripts/_scratch/_tmp_patch_todo2.py +64 -0
- package/scripts/_scratch/_tmp_probe_session.py +51 -0
- package/scripts/_scratch/_tmp_style_probe.py +40 -0
- package/scripts/_scratch/_tmp_write_note.py +139 -0
- package/scripts/_scratch/_/346/235/245/346/272/220/350/256/260/345/275/225.txt +1 -0
- package/scripts/_scratch/asar_compaction/dsh-compaction-basic__lib__index.js +1044 -0
- package/scripts/_scratch/asar_compaction/dsh-compaction-tool-result-pruner__lib__index.js +194 -0
- package/scripts/_scratch/asar_compaction/dsh-compaction__lib__index.js +175 -0
- package/scripts/_scratch/asar_compaction/dsh-compaction__lib__types__tool-pairing.js +97 -0
- package/scripts/_scratch/asar_compaction/dsh__node_modules__@deepseek-ai__dsh-compaction-image-offload__lib__types__image-offload.js +47 -0
- package/scripts/_scratch/asar_compaction/dsh__node_modules__@deepseek-ai__dsh-compaction-image-offload__lib__types__index.js +40 -0
- package/scripts/_scratch/asar_compaction/dsh__node_modules__@deepseek-ai__dsh-compaction-image-offload__lib__types__project-message.js +37 -0
- package/scripts/_scratch/asar_compaction/dsh__node_modules__@deepseek-ai__dsh-compaction__lib__types__brand.js +9 -0
- package/scripts/_scratch/asar_compaction/dsh__node_modules__@deepseek-ai__dsh-compaction__lib__types__checkpoint.js +36 -0
- package/scripts/_scratch/asar_compaction/dsh__node_modules__@deepseek-ai__dsh-compaction__lib__types__types.js +10 -0
- package/scripts/_scratch/commit_msg_occ.txt +16 -0
- package/scripts/_scratch/commit_msg_struct.txt +16 -0
- package/scripts/_scratch/v5.34_changelog.txt +36 -0
- package/scripts/_sync_trae_cn.py +102 -34
- package/scripts/_tmp_add_s10.py +54 -0
- package/scripts/_tmp_add_s11.py +61 -0
- package/scripts/_tmp_add_s12.py +66 -0
- package/scripts/_tmp_add_s13.py +69 -0
- package/scripts/_tmp_add_s14.py +80 -0
- package/scripts/_tmp_add_s9.py +68 -0
- package/scripts/_tmp_cache_load.py +82 -0
- package/scripts/_tmp_clean_market.py +40 -0
- package/scripts/_tmp_clean_pruned.py +81 -0
- package/scripts/_tmp_cleanup_docs.py +39 -0
- package/scripts/_tmp_cmp_ranker.py +17 -0
- package/scripts/_tmp_dolater_note.py +35 -0
- package/scripts/_tmp_dust_why.py +27 -0
- package/scripts/_tmp_explicit_import.py +40 -0
- package/scripts/_tmp_extract_compaction.py +35 -0
- package/scripts/_tmp_extract_compaction2.py +63 -0
- package/scripts/_tmp_extract_compaction3.py +51 -0
- package/scripts/_tmp_extract_compaction4.py +54 -0
- package/scripts/_tmp_finalize_s14.py +39 -0
- package/scripts/_tmp_find_compaction.py +43 -0
- package/scripts/_tmp_fix_b4.py +22 -0
- package/scripts/_tmp_fix_cli76.py +26 -0
- package/scripts/_tmp_fix_docs.py +38 -0
- package/scripts/_tmp_fix_docs_b.py +46 -0
- package/scripts/_tmp_fix_docs_b2.py +22 -0
- package/scripts/_tmp_fix_i3.py +23 -0
- package/scripts/_tmp_fix_s10.py +83 -0
- package/scripts/_tmp_fix_s11.py +14 -0
- package/scripts/_tmp_fix_s9_status.py +19 -0
- package/scripts/_tmp_fix_shadow_sim.py +37 -0
- package/scripts/_tmp_fix_traits_value.py +41 -0
- package/scripts/_tmp_import_idem.py +106 -0
- package/scripts/_tmp_log_ack.py +45 -0
- package/scripts/_tmp_mark_batch.py +28 -0
- package/scripts/_tmp_mark_batch2.py +32 -0
- package/scripts/_tmp_mark_dolater.py +26 -0
- package/scripts/_tmp_mark_dolater2.py +31 -0
- package/scripts/_tmp_move_self.py +16 -0
- package/scripts/_tmp_occ_cli.py +22 -0
- package/scripts/_tmp_occ_link.py +108 -0
- package/scripts/_tmp_optimize_recall.py +54 -0
- package/scripts/_tmp_panel_audit.py +26 -0
- package/scripts/_tmp_patch_batch3.py +65 -0
- package/scripts/_tmp_patch_rrf.py +46 -0
- package/scripts/_tmp_patch_s2s3.py +80 -0
- package/scripts/_tmp_patch_s4.py +39 -0
- package/scripts/_tmp_patch_s7.py +43 -0
- package/scripts/_tmp_patch_s8.py +91 -0
- package/scripts/_tmp_patch_skill.py +17 -0
- package/scripts/_tmp_release_guard.py +57 -0
- package/scripts/_tmp_rerank_breaker.py +62 -0
- package/scripts/_tmp_s16.py +41 -0
- package/scripts/_tmp_scan_asar.py +41 -0
- package/scripts/_tmp_scan_asar2.py +37 -0
- package/scripts/_tmp_struct_docs.py +49 -0
- package/scripts/_tmp_sum_fallback.py +42 -0
- package/scripts/_tmp_sweep.py +81 -0
- package/scripts/_tmp_sync_check.py +69 -0
- package/scripts/_tmp_sync_tail.py +39 -0
- package/scripts/_tmp_todo_clean.py +67 -0
- package/scripts/_tmp_verify_copy.py +17 -0
- package/scripts/_tmp_wire_extras.py +93 -0
- package/scripts/_tmp_wire_hsum.py +34 -0
- package/scripts/_tmp_wire_occ_b5.py +40 -0
- package/scripts/_tmp_wire_ranker.py +13 -0
- package/scripts/_tmp_wire_shadow_timeline.py +45 -0
- package/scripts/anchor_retention.py +68 -0
- package/scripts/app_context.py +1160 -0
- package/scripts/astrbot_plugin_lingshu/CHECKSUMS.json +38 -22
- package/scripts/astrbot_plugin_lingshu/engine/CHANGELOG.md +4281 -4227
- package/scripts/astrbot_plugin_lingshu/engine/README.md +1584 -1577
- package/scripts/astrbot_plugin_lingshu/engine/SKILL-lite.md +59 -59
- package/scripts/astrbot_plugin_lingshu/engine/SKILL-tiny.md +38 -38
- package/scripts/astrbot_plugin_lingshu/engine/SKILL.md +356 -331
- package/scripts/astrbot_plugin_lingshu/engine/assets/panel.html +6753 -5698
- package/scripts/astrbot_plugin_lingshu/engine/docs/commands.md +278 -247
- package/scripts/astrbot_plugin_lingshu/engine/docs/mechanics.md +610 -610
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_sync_trae_cn.py +102 -34
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_add_s10.py +54 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_add_s11.py +61 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_add_s12.py +66 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_add_s13.py +69 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_add_s14.py +80 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_add_s9.py +68 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_cache_load.py +82 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_clean_market.py +40 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_clean_pruned.py +81 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_cleanup_docs.py +39 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_cmp_ranker.py +17 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_dolater_note.py +35 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_dust_why.py +27 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_explicit_import.py +40 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_extract_compaction.py +35 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_extract_compaction2.py +63 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_extract_compaction3.py +51 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_extract_compaction4.py +54 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_finalize_s14.py +39 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_find_compaction.py +43 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_b4.py +22 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_cli76.py +26 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_docs.py +38 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_docs_b.py +46 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_docs_b2.py +22 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_i3.py +23 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_s10.py +83 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_s11.py +14 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_s9_status.py +19 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_shadow_sim.py +37 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_traits_value.py +41 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_import_idem.py +106 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_log_ack.py +45 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_mark_batch.py +28 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_mark_batch2.py +32 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_mark_dolater.py +26 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_mark_dolater2.py +31 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_move_self.py +16 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_occ_cli.py +22 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_occ_link.py +108 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_optimize_recall.py +54 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_panel_audit.py +26 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_patch_batch3.py +65 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_patch_rrf.py +46 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_patch_s2s3.py +80 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_patch_s4.py +39 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_patch_s7.py +43 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_patch_s8.py +91 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_patch_skill.py +17 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_release_guard.py +57 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_rerank_breaker.py +62 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_s16.py +41 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_scan_asar.py +41 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_scan_asar2.py +37 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_struct_docs.py +49 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_sum_fallback.py +42 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_sweep.py +81 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_sync_check.py +69 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_sync_tail.py +39 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_todo_clean.py +67 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_verify_copy.py +17 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_wire_extras.py +93 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_wire_hsum.py +34 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_wire_occ_b5.py +40 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_wire_ranker.py +13 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_wire_shadow_timeline.py +45 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/anchor_retention.py +68 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/app_context.py +1160 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/bigfive.py +90 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/brain.py +1 -1
- package/scripts/astrbot_plugin_lingshu/engine/scripts/check_docs.py +191 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/cli.py +921 -86
- package/scripts/astrbot_plugin_lingshu/engine/scripts/core.py +2468 -2406
- package/scripts/astrbot_plugin_lingshu/engine/scripts/coverage_report.py +5 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/delivery.py +112 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/dream.py +41 -3
- package/scripts/astrbot_plugin_lingshu/engine/scripts/emotion.py +202 -23
- package/scripts/astrbot_plugin_lingshu/engine/scripts/gap_memory.py +186 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/growth.py +72 -8
- package/scripts/astrbot_plugin_lingshu/engine/scripts/hsum.py +206 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_score.py +92 -7
- package/scripts/astrbot_plugin_lingshu/engine/scripts/memory.py +530 -60
- package/scripts/astrbot_plugin_lingshu/engine/scripts/memory_extras.py +200 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/occ.py +190 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_server.py +232 -142
- package/scripts/astrbot_plugin_lingshu/engine/scripts/persona.py +2498 -2349
- package/scripts/astrbot_plugin_lingshu/engine/scripts/prepublish_tests.py +90 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/release.py +326 -287
- package/scripts/astrbot_plugin_lingshu/engine/scripts/rss_ingest.py +181 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/session_import.py +832 -764
- package/scripts/astrbot_plugin_lingshu/engine/scripts/shadow_memory.py +173 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/style_check.py +220 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/summarizer.py +343 -252
- package/scripts/astrbot_plugin_lingshu/engine/scripts/timeline.py +118 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/watchdog.py +110 -0
- package/scripts/astrbot_plugin_lingshu/engine/scripts/web_research.py +125 -0
- package/scripts/bigfive.py +90 -0
- package/scripts/brain.py +1 -1
- package/scripts/check_docs.py +191 -0
- package/scripts/cli.py +921 -86
- package/scripts/core.py +2468 -2406
- package/scripts/coverage_report.py +5 -0
- package/scripts/delivery.py +112 -0
- package/scripts/dream.py +41 -3
- package/scripts/emotion.py +202 -23
- package/scripts/gap_memory.py +186 -0
- package/scripts/growth.py +72 -8
- package/scripts/hsum.py +206 -0
- package/scripts/mem_score.py +92 -7
- package/scripts/memory.py +530 -60
- package/scripts/memory_extras.py +200 -0
- package/scripts/occ.py +190 -0
- package/scripts/panel_server.py +232 -142
- package/scripts/persona.py +2498 -2349
- package/scripts/prepublish_tests.py +90 -0
- package/scripts/release.py +326 -287
- package/scripts/rss_ingest.py +181 -0
- package/scripts/run_tests.py +13 -0
- package/scripts/session_import.py +832 -764
- package/scripts/shadow_memory.py +173 -0
- package/scripts/style_check.py +220 -0
- package/scripts/summarizer.py +343 -252
- package/scripts/timeline.py +118 -0
- package/scripts/update_dsh_copy.ps1 +110 -107
- package/scripts/watchdog.py +110 -0
- package/scripts/web_research.py +125 -0
- package/docs//344/272/244/346/216/245/347/254/224/350/256/260-20261128.md +0 -128
- package/docs//345/256/241/346/240/270/346/224/271/350/277/233/345/276/205/345/212/236.md +0 -408
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""影子记忆(#49,§11 机制三)。
|
|
4
|
+
|
|
5
|
+
普通记忆存"当时聊了什么、得出什么结论";影子存**写下它那一刻的灵枢底色**——
|
|
6
|
+
人格 traits / 驱力 / 情绪,**不含任何任务内容**。
|
|
7
|
+
|
|
8
|
+
用途:日后检测"人格断裂风险"——不是查逻辑对错,而是查"现在的你,是不是在推翻
|
|
9
|
+
当年写下这条的那个自己的底层气质"。**只提醒、绝不阻止**变。
|
|
10
|
+
|
|
11
|
+
设计约束:
|
|
12
|
+
· 影子只是记录,不参与检索、不参与打分、不进注入;
|
|
13
|
+
· 用余弦相似度比 traits/drives 两组合并向量,低于阈值才算"断裂风险";
|
|
14
|
+
· 没有影子字段的旧记忆直接跳过(不回溯造假数据)。
|
|
15
|
+
"""
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import math
|
|
19
|
+
|
|
20
|
+
import brain # 必须走 brain(命名空间回灌)
|
|
21
|
+
import core
|
|
22
|
+
from core import _num, now_ts
|
|
23
|
+
|
|
24
|
+
SNAP_FIELDS = ("traits", "drives")
|
|
25
|
+
DEFAULT_THRESHOLD = 0.55 # 余弦相似度低于它 → 标"人格断裂风险"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _vector(section_map: dict) -> dict:
|
|
29
|
+
out = {}
|
|
30
|
+
for section in SNAP_FIELDS:
|
|
31
|
+
d = (section_map or {}).get(section) or {}
|
|
32
|
+
if isinstance(d, dict):
|
|
33
|
+
for k, v in d.items():
|
|
34
|
+
try:
|
|
35
|
+
out[f"{section}.{k}"] = float(v)
|
|
36
|
+
except (TypeError, ValueError):
|
|
37
|
+
continue
|
|
38
|
+
return out
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _cos(a: dict, b: dict) -> float:
|
|
42
|
+
keys = set(a) & set(b)
|
|
43
|
+
if not keys:
|
|
44
|
+
return 1.0 # 无共同维度 → 不下结论(当作"没变化")
|
|
45
|
+
dot = sum(a[k] * b[k] for k in keys)
|
|
46
|
+
na = math.sqrt(sum(a[k] ** 2 for k in keys))
|
|
47
|
+
nb = math.sqrt(sum(b[k] ** 2 for k in keys))
|
|
48
|
+
if na == 0 or nb == 0:
|
|
49
|
+
return 1.0
|
|
50
|
+
return dot / (na * nb)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _similarity(a: dict, b: dict) -> float:
|
|
54
|
+
"""人格底色相似度 = 余弦 × 相对一致度。
|
|
55
|
+
|
|
56
|
+
为什么不用纯余弦:traits 都是正数小向量,余弦对它不敏感——
|
|
57
|
+
实测「好奇 80/温和 60」vs「好奇 5/温和 95」余弦还有 0.64,根本算不上"断裂"。
|
|
58
|
+
再乘一个**逐维相对一致度**(1 - |Δ|/max)就分得开了:同一对只有 0.22。
|
|
59
|
+
"""
|
|
60
|
+
keys = set(a) & set(b)
|
|
61
|
+
if not keys:
|
|
62
|
+
return 1.0
|
|
63
|
+
cos = _cos(a, b)
|
|
64
|
+
agree = 0.0
|
|
65
|
+
for k in keys:
|
|
66
|
+
m = max(abs(a[k]), abs(b[k]), 1.0)
|
|
67
|
+
agree += 1.0 - abs(a[k] - b[k]) / m
|
|
68
|
+
agree /= len(keys)
|
|
69
|
+
return cos * agree
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def snapshot_persona() -> dict:
|
|
73
|
+
"""当前人格底色(只取稳定特征 + 情绪标签,不含正文)。"""
|
|
74
|
+
p = brain.load_persona() or {}
|
|
75
|
+
mood = {}
|
|
76
|
+
try:
|
|
77
|
+
m = brain.mood_now() or {}
|
|
78
|
+
mood = {"label": m.get("label") or "", "valence": _num(m.get("valence"), 0.0),
|
|
79
|
+
"intensity": _num(m.get("intensity"), 0.0)}
|
|
80
|
+
except Exception:
|
|
81
|
+
mood = {}
|
|
82
|
+
return {"ts": now_ts(), "traits": p.get("traits") or {},
|
|
83
|
+
"drives": p.get("drives") or {}, "mood": mood}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def stamp(memory_id: str) -> bool:
|
|
87
|
+
"""给一条记忆补影子(幂等:已有影子不覆盖)。"""
|
|
88
|
+
snap = snapshot_persona()
|
|
89
|
+
with core._locked(core.HOME / "hippocampus.jsonl"):
|
|
90
|
+
recs = brain.load_all()
|
|
91
|
+
hit = False
|
|
92
|
+
for m in recs:
|
|
93
|
+
if m.get("id") == memory_id and not m.get("shadow"):
|
|
94
|
+
m["shadow"] = snap
|
|
95
|
+
hit = True
|
|
96
|
+
break
|
|
97
|
+
if hit:
|
|
98
|
+
brain.save_all(recs)
|
|
99
|
+
return hit
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def stamp_recent(days: int = 1, limit: int = 50) -> dict:
|
|
103
|
+
"""给最近 N 天没影子的记忆补影子(批量,供 cron/dream 顺带跑)。"""
|
|
104
|
+
now = now_ts()
|
|
105
|
+
cut = now - max(0, int(days)) * 86400
|
|
106
|
+
snap = snapshot_persona()
|
|
107
|
+
done = 0
|
|
108
|
+
with core._locked(core.HOME / "hippocampus.jsonl"):
|
|
109
|
+
recs = brain.load_all()
|
|
110
|
+
for m in recs:
|
|
111
|
+
if done >= max(1, int(limit)):
|
|
112
|
+
break
|
|
113
|
+
if m.get("shadow") or m.get("layer") == "dust":
|
|
114
|
+
continue
|
|
115
|
+
if _num(m.get("created"), 0) < cut:
|
|
116
|
+
continue
|
|
117
|
+
m["shadow"] = dict(snap, ts=_num(m.get("created"), now))
|
|
118
|
+
done += 1
|
|
119
|
+
if done:
|
|
120
|
+
brain.save_all(recs)
|
|
121
|
+
return {"stamped": done, "days": days}
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def audit(threshold: float = DEFAULT_THRESHOLD, limit: int = 20) -> dict:
|
|
125
|
+
"""扫"人格断裂风险":现在的人格向量 vs 记忆落下时的影子向量。"""
|
|
126
|
+
cur = _vector(brain.load_persona() or {})
|
|
127
|
+
rows = []
|
|
128
|
+
scanned = 0
|
|
129
|
+
for m in brain.load_all():
|
|
130
|
+
sh = m.get("shadow")
|
|
131
|
+
if not isinstance(sh, dict):
|
|
132
|
+
continue
|
|
133
|
+
scanned += 1
|
|
134
|
+
prev = _vector(sh)
|
|
135
|
+
sim = _similarity(cur, prev)
|
|
136
|
+
if sim < float(threshold):
|
|
137
|
+
rows.append({"id": m.get("id"), "sim": round(sim, 3),
|
|
138
|
+
"created": m.get("created"),
|
|
139
|
+
"text": (m.get("text") or "")[:80]})
|
|
140
|
+
rows.sort(key=lambda x: x["sim"])
|
|
141
|
+
return {"scanned": scanned, "threshold": threshold,
|
|
142
|
+
"risks": rows[:max(1, int(limit))],
|
|
143
|
+
"count": len(rows)}
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def render_audit(r: dict) -> list:
|
|
147
|
+
if not r["scanned"]:
|
|
148
|
+
return ["还没有带影子的记忆——先跑 `shadow stamp-recent` 给近期记忆补上。"]
|
|
149
|
+
if not r["count"]:
|
|
150
|
+
return [f"扫了 {r['scanned']} 条带影子的记忆:人格底色连贯(阈值 {r['threshold']})。"]
|
|
151
|
+
lines = [f"扫了 {r['scanned']} 条,{r['count']} 条有『人格断裂风险』"
|
|
152
|
+
f"(相似度 < {r['threshold']})——只是提醒,不阻止你变:"]
|
|
153
|
+
for x in r["risks"]:
|
|
154
|
+
lines.append(f" {x['id']} 相似度 {x['sim']} {x['text']}")
|
|
155
|
+
return lines
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def run(args) -> int:
|
|
159
|
+
action = getattr(args, "shadow_action", None) or "audit"
|
|
160
|
+
if action == "stamp":
|
|
161
|
+
ok = stamp(args.id)
|
|
162
|
+
print("已补影子" if ok else "没找到这条记忆,或它已经有影子了")
|
|
163
|
+
return 0 if ok else 1
|
|
164
|
+
if action == "stamp-recent":
|
|
165
|
+
r = stamp_recent(days=int(getattr(args, "days", 1) or 1),
|
|
166
|
+
limit=int(getattr(args, "limit", 50) or 50))
|
|
167
|
+
print(f"给 {r['stamped']} 条最近 {r['days']} 天的记忆补了影子")
|
|
168
|
+
return 0
|
|
169
|
+
r = audit(threshold=float(getattr(args, "threshold", DEFAULT_THRESHOLD) or DEFAULT_THRESHOLD),
|
|
170
|
+
limit=int(getattr(args, "limit", 20) or 20))
|
|
171
|
+
for line in render_audit(r):
|
|
172
|
+
print(line)
|
|
173
|
+
return 0
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""灵枢 · 回复风格体检(B3)
|
|
4
|
+
|
|
5
|
+
缺口:`persona check` 只验结构(溯源/NaN/越界),`persona drift` 只算特征幅度,
|
|
6
|
+
**没有任何度量能回答"我说话的方式有没有跑偏"**。
|
|
7
|
+
|
|
8
|
+
做法:从宿主实时会话里取灵枢自己的回复(assistant + source=model),算一组
|
|
9
|
+
可复现的文本指标,与一份"基线"比对。基线默认取第一次体检时的快照,
|
|
10
|
+
存在 config.json 的 `style_baseline`,也可随时 `--save` 重设。
|
|
11
|
+
|
|
12
|
+
指标(都按千字归一,避免长短句混谈):
|
|
13
|
+
· avg_chars 每条回复平均字符数
|
|
14
|
+
· avg_sent_len 平均句长(按 。!?!?换行 切)
|
|
15
|
+
· comma / period / question / exclam / semicolon / dash 标点密度(个/千字)
|
|
16
|
+
· bullet_ratio 以 - * · 开头的行占比("是不是老写成清单")
|
|
17
|
+
|
|
18
|
+
只用标准库;读不到会话就返回"无样本",绝不抛。
|
|
19
|
+
"""
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
import re
|
|
23
|
+
import statistics
|
|
24
|
+
import time
|
|
25
|
+
from pathlib import Path
|
|
26
|
+
|
|
27
|
+
import core
|
|
28
|
+
|
|
29
|
+
MIN_LEN = 20 # 太短的回复不参与统计("好的""收到"没有风格信息)
|
|
30
|
+
MIN_SAMPLES = 20 # 样本太少不下结论
|
|
31
|
+
PUNCT = {
|
|
32
|
+
"comma": ",,",
|
|
33
|
+
"period": "。",
|
|
34
|
+
"question": "??",
|
|
35
|
+
"exclam": "!!",
|
|
36
|
+
"semicolon": ";;",
|
|
37
|
+
"dash": "—",
|
|
38
|
+
}
|
|
39
|
+
# 容忍度:超过即视为跑偏(保守,宁可漏报不要天天喊)
|
|
40
|
+
TOLERANCE = {
|
|
41
|
+
"avg_sent_len": 0.50,
|
|
42
|
+
"avg_chars": 0.60,
|
|
43
|
+
"comma": 0.70,
|
|
44
|
+
"period": 0.70,
|
|
45
|
+
"question": 0.80,
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def collect(limit: int = 200) -> list[str]:
|
|
50
|
+
"""取最近的灵枢回复全文(按时间正序)。失败返回空列表。"""
|
|
51
|
+
try:
|
|
52
|
+
import app_context
|
|
53
|
+
p = app_context.find_session()
|
|
54
|
+
if not p:
|
|
55
|
+
return []
|
|
56
|
+
evs = app_context.read_events(p)
|
|
57
|
+
except Exception:
|
|
58
|
+
return []
|
|
59
|
+
out = []
|
|
60
|
+
for e in evs:
|
|
61
|
+
try:
|
|
62
|
+
m = app_context._msg_of(e)
|
|
63
|
+
except Exception:
|
|
64
|
+
continue
|
|
65
|
+
if not isinstance(m, dict) or m.get("role") != "assistant":
|
|
66
|
+
continue
|
|
67
|
+
kind = ((m.get("source") or {}).get("kind") or "")
|
|
68
|
+
if kind and kind != "model":
|
|
69
|
+
continue
|
|
70
|
+
try:
|
|
71
|
+
t = app_context._full_text(m).strip()
|
|
72
|
+
except Exception:
|
|
73
|
+
continue
|
|
74
|
+
if len(t) >= MIN_LEN:
|
|
75
|
+
out.append(t)
|
|
76
|
+
return out[-limit:]
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def metrics(texts: list[str]) -> dict:
|
|
80
|
+
if not texts:
|
|
81
|
+
return {"n": 0}
|
|
82
|
+
chars = sum(len(t) for t in texts)
|
|
83
|
+
sents = []
|
|
84
|
+
for t in texts:
|
|
85
|
+
for s in re.split(r"[。!?!?\n]+", t):
|
|
86
|
+
s = s.strip()
|
|
87
|
+
if s:
|
|
88
|
+
sents.append(s)
|
|
89
|
+
lines = [ln for t in texts for ln in t.splitlines() if ln.strip()]
|
|
90
|
+
bullets = [ln for ln in lines if ln.strip().startswith(("-", "*", "·"))]
|
|
91
|
+
out = {
|
|
92
|
+
"n": len(texts),
|
|
93
|
+
"chars": chars,
|
|
94
|
+
"avg_chars": round(chars / len(texts), 1),
|
|
95
|
+
"avg_sent_len": round(statistics.mean(len(s) for s in sents), 1) if sents else 0.0,
|
|
96
|
+
}
|
|
97
|
+
for key, marks in PUNCT.items():
|
|
98
|
+
c = sum(t.count(m) for t in texts for m in marks)
|
|
99
|
+
out[key] = round(c / chars * 1000, 1) if chars else 0.0
|
|
100
|
+
out["bullet_ratio"] = round(len(bullets) / len(lines), 3) if lines else 0.0
|
|
101
|
+
return out
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def load_baseline() -> dict | None:
|
|
105
|
+
try:
|
|
106
|
+
b = (core.load_config() or {}).get("style_baseline")
|
|
107
|
+
return b if isinstance(b, dict) and b.get("n") else None
|
|
108
|
+
except Exception:
|
|
109
|
+
return None
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def save_baseline(m: dict) -> None:
|
|
113
|
+
"""把当前指标存成基线。core 没有 save_config——必须走它的锁 + 原子写。"""
|
|
114
|
+
if getattr(core, "READONLY", False):
|
|
115
|
+
raise PermissionError("记忆库当前只读,无法写入风格基线。")
|
|
116
|
+
with core._locked(core.HOME / "config.json"):
|
|
117
|
+
cfg = core.load_config() or {}
|
|
118
|
+
cfg["style_baseline"] = {**m, "ts": int(time.time())}
|
|
119
|
+
core.atomic_json(core.HOME / "config.json", cfg)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def compare(cur: dict, base: dict) -> tuple:
|
|
123
|
+
"""返回 (ok, deviations)。deviation = {指标: (当前, 基线, 相对变化%)}。"""
|
|
124
|
+
devs = {}
|
|
125
|
+
if not base or cur.get("n", 0) < MIN_SAMPLES or base.get("n", 0) < MIN_SAMPLES:
|
|
126
|
+
return True, devs
|
|
127
|
+
for key, tol in TOLERANCE.items():
|
|
128
|
+
b = float(base.get(key) or 0)
|
|
129
|
+
c = float(cur.get(key) or 0)
|
|
130
|
+
if b <= 0:
|
|
131
|
+
continue
|
|
132
|
+
rel = (c - b) / b
|
|
133
|
+
if abs(rel) > tol:
|
|
134
|
+
devs[key] = (c, b, round(rel * 100, 1))
|
|
135
|
+
return (len(devs) == 0), devs
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def check(limit: int = 200) -> dict:
|
|
139
|
+
texts = collect(limit)
|
|
140
|
+
cur = metrics(texts)
|
|
141
|
+
base = load_baseline()
|
|
142
|
+
if cur.get("n", 0) == 0:
|
|
143
|
+
return {"ok": True, "state": "no_samples", "metrics": cur,
|
|
144
|
+
"baseline": base, "deviations": {},
|
|
145
|
+
"note": "取不到灵枢回复样本(会话文件不可读或还没聊过)"}
|
|
146
|
+
if not base:
|
|
147
|
+
return {"ok": True, "state": "no_baseline", "metrics": cur,
|
|
148
|
+
"baseline": None, "deviations": {},
|
|
149
|
+
"note": "还没有基线;跑 `brain.py style --save` 建一份"}
|
|
150
|
+
ok, devs = compare(cur, base)
|
|
151
|
+
return {"ok": ok, "state": "ok" if ok else "drift", "metrics": cur,
|
|
152
|
+
"baseline": base, "deviations": devs,
|
|
153
|
+
"note": "" if ok else "风格与基线偏离较大(见 deviations)"}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
HINT_TTL_SEC = 6 * 3600 # 提示缓存 6 小时(B4:周期性,不是每轮重算)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def hint(ttl: int = HINT_TTL_SEC) -> str:
|
|
160
|
+
"""生成一句紧凑的"语气提示",默认 6 小时缓存(B4)。
|
|
161
|
+
|
|
162
|
+
⚠ 它会被塞进注入包,所以:**默认关闭**(config `style_hint` 为真才用),
|
|
163
|
+
内容压到一行、只讲"该注意什么",不复述指标全表。
|
|
164
|
+
"""
|
|
165
|
+
try:
|
|
166
|
+
cfg = core.load_config() or {}
|
|
167
|
+
cache = cfg.get("style_hint_cache") or {}
|
|
168
|
+
if isinstance(cache, dict) and cache.get("text") and \
|
|
169
|
+
(time.time() - float(cache.get("ts") or 0)) < ttl:
|
|
170
|
+
return str(cache["text"])
|
|
171
|
+
except Exception:
|
|
172
|
+
cfg = {}
|
|
173
|
+
|
|
174
|
+
d = check()
|
|
175
|
+
m = d.get("metrics") or {}
|
|
176
|
+
n = m.get("n") or 0
|
|
177
|
+
if n < MIN_SAMPLES:
|
|
178
|
+
return ""
|
|
179
|
+
notes = []
|
|
180
|
+
if (m.get("avg_sent_len") or 0) > 60:
|
|
181
|
+
notes.append("句子偏长,能断就断")
|
|
182
|
+
if (m.get("bullet_ratio") or 0) > 0.45:
|
|
183
|
+
notes.append("清单占比偏高,多写整句少堆条目")
|
|
184
|
+
if (m.get("exclam") or 0) < 0.2 and (m.get("question") or 0) < 0.3:
|
|
185
|
+
notes.append("语气偏平,该有起伏时别怕用问句")
|
|
186
|
+
if not notes:
|
|
187
|
+
notes.append("保持现在的节奏")
|
|
188
|
+
text = (f"【语气】最近 {n} 条回复:平均句长 {m.get('avg_sent_len', 0)} 字、"
|
|
189
|
+
f"列表行 {round((m.get('bullet_ratio') or 0) * 100)}%——" + ";".join(notes) + "。")
|
|
190
|
+
try:
|
|
191
|
+
if not getattr(core, "READONLY", False):
|
|
192
|
+
with core._locked(core.HOME / "config.json"):
|
|
193
|
+
c2 = core.load_config() or {}
|
|
194
|
+
c2["style_hint_cache"] = {"ts": int(time.time()), "text": text}
|
|
195
|
+
core.atomic_json(core.HOME / "config.json", c2)
|
|
196
|
+
except Exception:
|
|
197
|
+
pass
|
|
198
|
+
return text
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def render(d: dict) -> list[str]:
|
|
202
|
+
m = d.get("metrics") or {}
|
|
203
|
+
lines = [f"样本:{m.get('n', 0)} 条回复 / {m.get('chars', 0)} 字符"
|
|
204
|
+
f"(平均 {m.get('avg_chars', 0)} 字/条)"]
|
|
205
|
+
lines.append(f"平均句长 {m.get('avg_sent_len', 0)} 字;"
|
|
206
|
+
f"逗号 {m.get('comma', 0)}‰、句号 {m.get('period', 0)}‰、"
|
|
207
|
+
f"问号 {m.get('question', 0)}‰、感叹 {m.get('exclam', 0)}‰")
|
|
208
|
+
lines.append(f"列表行占比 {m.get('bullet_ratio', 0)}"
|
|
209
|
+
f"(越高越像「清单式」回答)")
|
|
210
|
+
b = d.get("baseline")
|
|
211
|
+
if b:
|
|
212
|
+
lines.append(f"基线:{b.get('n')} 条时 平均句长 {b.get('avg_sent_len')}、"
|
|
213
|
+
f"平均 {b.get('avg_chars')} 字/条")
|
|
214
|
+
else:
|
|
215
|
+
lines.append("基线:无(`style --save` 建立)")
|
|
216
|
+
for k, (c, bb, rel) in (d.get("deviations") or {}).items():
|
|
217
|
+
lines.append(f"⚠ {k}:{c} vs 基线 {bb}({rel:+.1f}%)")
|
|
218
|
+
if d.get("note"):
|
|
219
|
+
lines.append(d["note"])
|
|
220
|
+
return lines
|