lingshu-skill 5.33.0 → 5.37.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (266) hide show
  1. package/CHANGELOG.md +4281 -4227
  2. package/README.md +1584 -1577
  3. package/SKILL-lite.md +59 -59
  4. package/SKILL-tiny.md +38 -38
  5. package/SKILL.md +356 -331
  6. package/assets/panel.html +6753 -5698
  7. package/cordis-plugin/lingshu-cron/plugin.js +1455 -1417
  8. package/cordis-plugin/lingshu-startup/context-fold.js +170 -0
  9. package/cordis-plugin/lingshu-startup/context-pressure.js +16 -0
  10. package/cordis-plugin/lingshu-startup/context-snapshot.js +176 -0
  11. package/cordis-plugin/lingshu-startup/plugin.js +499 -467
  12. 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
  13. 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
  14. package/docs/audit_notes//345/276/205/345/212/236/346/250/241/346/235/277.md +140 -0
  15. package/docs/commands.md +278 -247
  16. package/docs/mechanics.md +610 -610
  17. 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
  18. 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
  19. package/package.json +52 -52
  20. package/scripts/_scratch/_tmp_backup_race.py +70 -0
  21. package/scripts/_scratch/_tmp_cleanup_move.py +34 -0
  22. package/scripts/_scratch/_tmp_count_kinds.py +25 -0
  23. package/scripts/_scratch/_tmp_diag_paths.py +48 -0
  24. package/scripts/_scratch/_tmp_facts.py +63 -0
  25. package/scripts/_scratch/_tmp_find_copies.py +31 -0
  26. package/scripts/_scratch/_tmp_find_pin.py +29 -0
  27. package/scripts/_scratch/_tmp_fix_pin.py +36 -0
  28. package/scripts/_scratch/_tmp_fold_fixture.py +55 -0
  29. package/scripts/_scratch/_tmp_fold_test.mjs +81 -0
  30. package/scripts/_scratch/_tmp_inject_secs.py +30 -0
  31. package/scripts/_scratch/_tmp_kb_balance.py +159 -0
  32. package/scripts/_scratch/_tmp_lines.py +15 -0
  33. package/scripts/_scratch/_tmp_market_probe.py +13 -0
  34. package/scripts/_scratch/_tmp_move_partial.py +16 -0
  35. package/scripts/_scratch/_tmp_patch_b3.py +41 -0
  36. package/scripts/_scratch/_tmp_patch_b5.py +62 -0
  37. package/scripts/_scratch/_tmp_patch_crash_excl.py +24 -0
  38. package/scripts/_scratch/_tmp_patch_handoff.py +21 -0
  39. package/scripts/_scratch/_tmp_patch_ps1.py +25 -0
  40. package/scripts/_scratch/_tmp_patch_stat.py +27 -0
  41. package/scripts/_scratch/_tmp_patch_todo.py +25 -0
  42. package/scripts/_scratch/_tmp_patch_todo2.py +64 -0
  43. package/scripts/_scratch/_tmp_probe_session.py +51 -0
  44. package/scripts/_scratch/_tmp_style_probe.py +40 -0
  45. package/scripts/_scratch/_tmp_write_note.py +139 -0
  46. package/scripts/_scratch/_/346/235/245/346/272/220/350/256/260/345/275/225.txt +1 -0
  47. package/scripts/_scratch/asar_compaction/dsh-compaction-basic__lib__index.js +1044 -0
  48. package/scripts/_scratch/asar_compaction/dsh-compaction-tool-result-pruner__lib__index.js +194 -0
  49. package/scripts/_scratch/asar_compaction/dsh-compaction__lib__index.js +175 -0
  50. package/scripts/_scratch/asar_compaction/dsh-compaction__lib__types__tool-pairing.js +97 -0
  51. package/scripts/_scratch/asar_compaction/dsh__node_modules__@deepseek-ai__dsh-compaction-image-offload__lib__types__image-offload.js +47 -0
  52. package/scripts/_scratch/asar_compaction/dsh__node_modules__@deepseek-ai__dsh-compaction-image-offload__lib__types__index.js +40 -0
  53. package/scripts/_scratch/asar_compaction/dsh__node_modules__@deepseek-ai__dsh-compaction-image-offload__lib__types__project-message.js +37 -0
  54. package/scripts/_scratch/asar_compaction/dsh__node_modules__@deepseek-ai__dsh-compaction__lib__types__brand.js +9 -0
  55. package/scripts/_scratch/asar_compaction/dsh__node_modules__@deepseek-ai__dsh-compaction__lib__types__checkpoint.js +36 -0
  56. package/scripts/_scratch/asar_compaction/dsh__node_modules__@deepseek-ai__dsh-compaction__lib__types__types.js +10 -0
  57. package/scripts/_scratch/commit_msg_occ.txt +16 -0
  58. package/scripts/_scratch/commit_msg_struct.txt +16 -0
  59. package/scripts/_scratch/v5.34_changelog.txt +36 -0
  60. package/scripts/_sync_trae_cn.py +102 -34
  61. package/scripts/_tmp_add_s10.py +54 -0
  62. package/scripts/_tmp_add_s11.py +61 -0
  63. package/scripts/_tmp_add_s12.py +66 -0
  64. package/scripts/_tmp_add_s13.py +69 -0
  65. package/scripts/_tmp_add_s14.py +80 -0
  66. package/scripts/_tmp_add_s9.py +68 -0
  67. package/scripts/_tmp_cache_load.py +82 -0
  68. package/scripts/_tmp_clean_market.py +40 -0
  69. package/scripts/_tmp_clean_pruned.py +81 -0
  70. package/scripts/_tmp_cleanup_docs.py +39 -0
  71. package/scripts/_tmp_cmp_ranker.py +17 -0
  72. package/scripts/_tmp_dolater_note.py +35 -0
  73. package/scripts/_tmp_dust_why.py +27 -0
  74. package/scripts/_tmp_explicit_import.py +40 -0
  75. package/scripts/_tmp_extract_compaction.py +35 -0
  76. package/scripts/_tmp_extract_compaction2.py +63 -0
  77. package/scripts/_tmp_extract_compaction3.py +51 -0
  78. package/scripts/_tmp_extract_compaction4.py +54 -0
  79. package/scripts/_tmp_finalize_s14.py +39 -0
  80. package/scripts/_tmp_find_compaction.py +43 -0
  81. package/scripts/_tmp_fix_b4.py +22 -0
  82. package/scripts/_tmp_fix_cli76.py +26 -0
  83. package/scripts/_tmp_fix_docs.py +38 -0
  84. package/scripts/_tmp_fix_docs_b.py +46 -0
  85. package/scripts/_tmp_fix_docs_b2.py +22 -0
  86. package/scripts/_tmp_fix_i3.py +23 -0
  87. package/scripts/_tmp_fix_s10.py +83 -0
  88. package/scripts/_tmp_fix_s11.py +14 -0
  89. package/scripts/_tmp_fix_s9_status.py +19 -0
  90. package/scripts/_tmp_fix_shadow_sim.py +37 -0
  91. package/scripts/_tmp_fix_traits_value.py +41 -0
  92. package/scripts/_tmp_import_idem.py +106 -0
  93. package/scripts/_tmp_log_ack.py +45 -0
  94. package/scripts/_tmp_mark_batch.py +28 -0
  95. package/scripts/_tmp_mark_batch2.py +32 -0
  96. package/scripts/_tmp_mark_dolater.py +26 -0
  97. package/scripts/_tmp_mark_dolater2.py +31 -0
  98. package/scripts/_tmp_move_self.py +16 -0
  99. package/scripts/_tmp_occ_cli.py +22 -0
  100. package/scripts/_tmp_occ_link.py +108 -0
  101. package/scripts/_tmp_optimize_recall.py +54 -0
  102. package/scripts/_tmp_panel_audit.py +26 -0
  103. package/scripts/_tmp_patch_batch3.py +65 -0
  104. package/scripts/_tmp_patch_rrf.py +46 -0
  105. package/scripts/_tmp_patch_s2s3.py +80 -0
  106. package/scripts/_tmp_patch_s4.py +39 -0
  107. package/scripts/_tmp_patch_s7.py +43 -0
  108. package/scripts/_tmp_patch_s8.py +91 -0
  109. package/scripts/_tmp_patch_skill.py +17 -0
  110. package/scripts/_tmp_release_guard.py +57 -0
  111. package/scripts/_tmp_rerank_breaker.py +62 -0
  112. package/scripts/_tmp_s16.py +41 -0
  113. package/scripts/_tmp_scan_asar.py +41 -0
  114. package/scripts/_tmp_scan_asar2.py +37 -0
  115. package/scripts/_tmp_struct_docs.py +49 -0
  116. package/scripts/_tmp_sum_fallback.py +42 -0
  117. package/scripts/_tmp_sweep.py +81 -0
  118. package/scripts/_tmp_sync_check.py +69 -0
  119. package/scripts/_tmp_sync_tail.py +39 -0
  120. package/scripts/_tmp_todo_clean.py +67 -0
  121. package/scripts/_tmp_verify_copy.py +17 -0
  122. package/scripts/_tmp_wire_extras.py +93 -0
  123. package/scripts/_tmp_wire_hsum.py +34 -0
  124. package/scripts/_tmp_wire_occ_b5.py +40 -0
  125. package/scripts/_tmp_wire_ranker.py +13 -0
  126. package/scripts/_tmp_wire_shadow_timeline.py +45 -0
  127. package/scripts/anchor_retention.py +68 -0
  128. package/scripts/app_context.py +1160 -0
  129. package/scripts/astrbot_plugin_lingshu/CHECKSUMS.json +38 -22
  130. package/scripts/astrbot_plugin_lingshu/engine/CHANGELOG.md +4281 -4227
  131. package/scripts/astrbot_plugin_lingshu/engine/README.md +1584 -1577
  132. package/scripts/astrbot_plugin_lingshu/engine/SKILL-lite.md +59 -59
  133. package/scripts/astrbot_plugin_lingshu/engine/SKILL-tiny.md +38 -38
  134. package/scripts/astrbot_plugin_lingshu/engine/SKILL.md +356 -331
  135. package/scripts/astrbot_plugin_lingshu/engine/assets/panel.html +6753 -5698
  136. package/scripts/astrbot_plugin_lingshu/engine/docs/commands.md +278 -247
  137. package/scripts/astrbot_plugin_lingshu/engine/docs/mechanics.md +610 -610
  138. package/scripts/astrbot_plugin_lingshu/engine/scripts/_sync_trae_cn.py +102 -34
  139. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_add_s10.py +54 -0
  140. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_add_s11.py +61 -0
  141. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_add_s12.py +66 -0
  142. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_add_s13.py +69 -0
  143. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_add_s14.py +80 -0
  144. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_add_s9.py +68 -0
  145. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_cache_load.py +82 -0
  146. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_clean_market.py +40 -0
  147. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_clean_pruned.py +81 -0
  148. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_cleanup_docs.py +39 -0
  149. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_cmp_ranker.py +17 -0
  150. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_dolater_note.py +35 -0
  151. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_dust_why.py +27 -0
  152. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_explicit_import.py +40 -0
  153. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_extract_compaction.py +35 -0
  154. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_extract_compaction2.py +63 -0
  155. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_extract_compaction3.py +51 -0
  156. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_extract_compaction4.py +54 -0
  157. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_finalize_s14.py +39 -0
  158. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_find_compaction.py +43 -0
  159. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_b4.py +22 -0
  160. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_cli76.py +26 -0
  161. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_docs.py +38 -0
  162. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_docs_b.py +46 -0
  163. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_docs_b2.py +22 -0
  164. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_i3.py +23 -0
  165. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_s10.py +83 -0
  166. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_s11.py +14 -0
  167. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_s9_status.py +19 -0
  168. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_shadow_sim.py +37 -0
  169. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_fix_traits_value.py +41 -0
  170. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_import_idem.py +106 -0
  171. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_log_ack.py +45 -0
  172. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_mark_batch.py +28 -0
  173. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_mark_batch2.py +32 -0
  174. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_mark_dolater.py +26 -0
  175. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_mark_dolater2.py +31 -0
  176. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_move_self.py +16 -0
  177. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_occ_cli.py +22 -0
  178. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_occ_link.py +108 -0
  179. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_optimize_recall.py +54 -0
  180. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_panel_audit.py +26 -0
  181. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_patch_batch3.py +65 -0
  182. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_patch_rrf.py +46 -0
  183. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_patch_s2s3.py +80 -0
  184. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_patch_s4.py +39 -0
  185. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_patch_s7.py +43 -0
  186. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_patch_s8.py +91 -0
  187. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_patch_skill.py +17 -0
  188. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_release_guard.py +57 -0
  189. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_rerank_breaker.py +62 -0
  190. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_s16.py +41 -0
  191. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_scan_asar.py +41 -0
  192. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_scan_asar2.py +37 -0
  193. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_struct_docs.py +49 -0
  194. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_sum_fallback.py +42 -0
  195. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_sweep.py +81 -0
  196. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_sync_check.py +69 -0
  197. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_sync_tail.py +39 -0
  198. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_todo_clean.py +67 -0
  199. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_verify_copy.py +17 -0
  200. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_wire_extras.py +93 -0
  201. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_wire_hsum.py +34 -0
  202. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_wire_occ_b5.py +40 -0
  203. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_wire_ranker.py +13 -0
  204. package/scripts/astrbot_plugin_lingshu/engine/scripts/_tmp_wire_shadow_timeline.py +45 -0
  205. package/scripts/astrbot_plugin_lingshu/engine/scripts/anchor_retention.py +68 -0
  206. package/scripts/astrbot_plugin_lingshu/engine/scripts/app_context.py +1160 -0
  207. package/scripts/astrbot_plugin_lingshu/engine/scripts/bigfive.py +90 -0
  208. package/scripts/astrbot_plugin_lingshu/engine/scripts/brain.py +1 -1
  209. package/scripts/astrbot_plugin_lingshu/engine/scripts/check_docs.py +191 -0
  210. package/scripts/astrbot_plugin_lingshu/engine/scripts/cli.py +921 -86
  211. package/scripts/astrbot_plugin_lingshu/engine/scripts/core.py +2468 -2406
  212. package/scripts/astrbot_plugin_lingshu/engine/scripts/coverage_report.py +5 -0
  213. package/scripts/astrbot_plugin_lingshu/engine/scripts/delivery.py +112 -0
  214. package/scripts/astrbot_plugin_lingshu/engine/scripts/dream.py +41 -3
  215. package/scripts/astrbot_plugin_lingshu/engine/scripts/emotion.py +202 -23
  216. package/scripts/astrbot_plugin_lingshu/engine/scripts/gap_memory.py +186 -0
  217. package/scripts/astrbot_plugin_lingshu/engine/scripts/growth.py +72 -8
  218. package/scripts/astrbot_plugin_lingshu/engine/scripts/hsum.py +206 -0
  219. package/scripts/astrbot_plugin_lingshu/engine/scripts/mem_score.py +92 -7
  220. package/scripts/astrbot_plugin_lingshu/engine/scripts/memory.py +530 -60
  221. package/scripts/astrbot_plugin_lingshu/engine/scripts/memory_extras.py +200 -0
  222. package/scripts/astrbot_plugin_lingshu/engine/scripts/occ.py +190 -0
  223. package/scripts/astrbot_plugin_lingshu/engine/scripts/panel_server.py +232 -142
  224. package/scripts/astrbot_plugin_lingshu/engine/scripts/persona.py +2498 -2349
  225. package/scripts/astrbot_plugin_lingshu/engine/scripts/prepublish_tests.py +90 -0
  226. package/scripts/astrbot_plugin_lingshu/engine/scripts/release.py +326 -287
  227. package/scripts/astrbot_plugin_lingshu/engine/scripts/rss_ingest.py +181 -0
  228. package/scripts/astrbot_plugin_lingshu/engine/scripts/session_import.py +832 -764
  229. package/scripts/astrbot_plugin_lingshu/engine/scripts/shadow_memory.py +173 -0
  230. package/scripts/astrbot_plugin_lingshu/engine/scripts/style_check.py +220 -0
  231. package/scripts/astrbot_plugin_lingshu/engine/scripts/summarizer.py +343 -252
  232. package/scripts/astrbot_plugin_lingshu/engine/scripts/timeline.py +118 -0
  233. package/scripts/astrbot_plugin_lingshu/engine/scripts/watchdog.py +110 -0
  234. package/scripts/astrbot_plugin_lingshu/engine/scripts/web_research.py +125 -0
  235. package/scripts/bigfive.py +90 -0
  236. package/scripts/brain.py +1 -1
  237. package/scripts/check_docs.py +191 -0
  238. package/scripts/cli.py +921 -86
  239. package/scripts/core.py +2468 -2406
  240. package/scripts/coverage_report.py +5 -0
  241. package/scripts/delivery.py +112 -0
  242. package/scripts/dream.py +41 -3
  243. package/scripts/emotion.py +202 -23
  244. package/scripts/gap_memory.py +186 -0
  245. package/scripts/growth.py +72 -8
  246. package/scripts/hsum.py +206 -0
  247. package/scripts/mem_score.py +92 -7
  248. package/scripts/memory.py +530 -60
  249. package/scripts/memory_extras.py +200 -0
  250. package/scripts/occ.py +190 -0
  251. package/scripts/panel_server.py +232 -142
  252. package/scripts/persona.py +2498 -2349
  253. package/scripts/prepublish_tests.py +90 -0
  254. package/scripts/release.py +326 -287
  255. package/scripts/rss_ingest.py +181 -0
  256. package/scripts/run_tests.py +13 -0
  257. package/scripts/session_import.py +832 -764
  258. package/scripts/shadow_memory.py +173 -0
  259. package/scripts/style_check.py +220 -0
  260. package/scripts/summarizer.py +343 -252
  261. package/scripts/timeline.py +118 -0
  262. package/scripts/update_dsh_copy.ps1 +110 -107
  263. package/scripts/watchdog.py +110 -0
  264. package/scripts/web_research.py +125 -0
  265. package/docs//344/272/244/346/216/245/347/254/224/350/256/260-20261128.md +0 -128
  266. 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,170 @@
1
+ // 灵枢 · 上下文折叠应用器(v5.34)
2
+ //
3
+ // 分工:Python 侧(app_context.py)负责**生成摘要并记账**——把某一整轮
4
+ // 压成五段式摘要,写进 HOME/context-folds.json;本模块只负责在
5
+ // agent/pre-step 把账本里「生效中」的那些轮从 decision.messages 里替换掉。
6
+ //
7
+ // 为什么按「整轮」而不是任意消息区间:
8
+ // · 折叠会改写提示词缓存前缀,删中段等于让后续全部失效(cron 的
9
+ // pruneInjected 注释里记过这个坑);
10
+ // · 更危险的是把 assistant 的工具调用和它的 tool 结果拆开——上游会 400。
11
+ // 整轮替换天然不会产生悬挂的 toolCallId。
12
+ //
13
+ // 安全约束:
14
+ // · 只按消息 id 匹配(UUID 天然按会话隔离,不会串会话);
15
+ // · 每次 pre-step 都重放同一份账本 → 结果确定(宿主每步重发历史也不漂);
16
+ // · 若某个保留消息引用了被折叠轮里的工具调用 → 整条折叠跳过(宁可不压);
17
+ // · 任何异常吞掉,绝不让 pre-step 冒泡;默认关闭,需 cfg.contextFold=true。
18
+
19
+ import { readFileSync, writeFileSync } from 'node:fs'
20
+ import { homedir } from 'node:os'
21
+ import { join } from 'node:path'
22
+
23
+ const FOLD_SOURCE = 'lingshu-fold'
24
+ const asText = (v) => String(v ?? '').trim()
25
+
26
+ // 宿主 checkpoint 只提示一次(每进程),避免每步刷日志。
27
+ let hostCheckpointNoted = false
28
+ function noteHostCheckpointOnce() {
29
+ if (hostCheckpointNoted) return
30
+ hostCheckpointNoted = true
31
+ try {
32
+ console.log('[lingshu] 检测到宿主 compaction checkpoint:本次不折叠(让位给宿主压缩)')
33
+ } catch { /* 忽略 */ }
34
+ }
35
+
36
+ function memoryHome() {
37
+ const env = asText(process.env.BRAIN_MEMORY_HOME)
38
+ if (env) return env
39
+ const base = join(homedir(), '.brain-memory')
40
+ try {
41
+ const ptr = asText(readFileSync(join(base, '.home'), 'utf8'))
42
+ if (ptr) return ptr
43
+ } catch { /* 没有指针就用默认目录 */ }
44
+ return base
45
+ }
46
+
47
+ function loadFolds() {
48
+ try {
49
+ const raw = readFileSync(join(memoryHome(), 'context-folds.json'), 'utf8')
50
+ const doc = JSON.parse(raw)
51
+ return doc && Array.isArray(doc.folds) ? doc : { version: 1, folds: [] }
52
+ } catch {
53
+ return { version: 1, folds: [] }
54
+ }
55
+ }
56
+
57
+ function saveFolds(doc) {
58
+ try {
59
+ writeFileSync(join(memoryHome(), 'context-folds.json'),
60
+ JSON.stringify(doc, null, 2), 'utf8')
61
+ } catch { /* 写失败不影响本步 */ }
62
+ }
63
+
64
+ /** 一条消息里声明的 tool-call id 们(assistant 侧)。 */
65
+ function toolCallIdsOf(msg) {
66
+ const out = []
67
+ const c = msg?.content
68
+ if (Array.isArray(c)) {
69
+ for (const p of c) {
70
+ if (p && typeof p === 'object' && p.type === 'tool-call' && p.id) out.push(String(p.id))
71
+ }
72
+ }
73
+ return out
74
+ }
75
+
76
+ /**
77
+ * 把账本里生效中的折叠应用到 decision.messages。
78
+ * @returns {ok:boolean, changed:boolean, folded:number, reason?:string}
79
+ */
80
+ export function applyFolds(decision, maxChars = 1600) {
81
+ const msgs = Array.isArray(decision?.messages) ? decision.messages : null
82
+ if (!msgs || !msgs.length) return { ok: true, changed: false, folded: 0 }
83
+
84
+ // §54 与宿主 compaction 协同:宿主有自己的压缩(dsh-compaction),它把一段 surface 换成
85
+ // 一条带 `source.kind === 'compact-checkpoint'` 的用户消息。若宿主已经压过,我们再压就是
86
+ // **重复压缩**——不但省不了多少,还可能把宿主的摘要再摘要一次,越压越糊。
87
+ // 所以:只要本次请求里出现 hub 的 checkpoint,就让位给宿主(只记一次日志,不打扰)。
88
+ try {
89
+ const hasHostCheckpoint = msgs.some((m) => {
90
+ const d = m?.data
91
+ const src = d?.source ?? d?.message?.source
92
+ return src && src.kind === 'compact-checkpoint'
93
+ })
94
+ if (hasHostCheckpoint) {
95
+ noteHostCheckpointOnce()
96
+ return { ok: true, changed: false, folded: 0, reason: 'host-compacted' }
97
+ }
98
+ } catch { /* 检测失败就当没有,继续按老逻辑 */ }
99
+
100
+ const doc = loadFolds()
101
+ const active = doc.folds.filter(
102
+ (f) => f && !f.restored && asText(f.summary) && Array.isArray(f.message_ids) && f.message_ids.length,
103
+ )
104
+ if (!active.length) return { ok: true, changed: false, folded: 0 }
105
+
106
+ // 消息 id → 归属的折叠
107
+ const byId = new Map()
108
+ for (const f of active) {
109
+ for (const id of f.message_ids) byId.set(String(id), f)
110
+ }
111
+
112
+ // 安全检查:被折叠消息里声明的 tool-call id,若仍被「保留」的消息引用,
113
+ // 说明这次折叠会留下悬挂的工具结果(上游 400)→ 该折叠整条跳过。
114
+ const droppedCalls = new Map() // callId -> foldId
115
+ for (const m of msgs) {
116
+ if (!byId.has(String(m?.id))) continue
117
+ const f = byId.get(String(m?.id))
118
+ for (const cid of toolCallIdsOf(m)) droppedCalls.set(cid, f.id)
119
+ }
120
+ const broken = new Set()
121
+ for (const m of msgs) {
122
+ if (byId.has(String(m?.id))) continue
123
+ const cid = m?.toolCallId ? String(m.toolCallId) : ''
124
+ if (cid && droppedCalls.has(cid)) broken.add(droppedCalls.get(cid))
125
+ }
126
+
127
+ const usable = new Map()
128
+ for (const f of active) {
129
+ if (!broken.has(f.id)) usable.set(f.id, f)
130
+ }
131
+ if (!usable.size) return { ok: true, changed: false, folded: 0, reason: '工具配对保护,全部跳过' }
132
+
133
+ const emitted = new Set()
134
+ const out = []
135
+ let folded = 0
136
+ let changed = false
137
+ for (const m of msgs) {
138
+ const f = usable.get(byId.get(String(m?.id))?.id)
139
+ if (!f) { out.push(m); continue }
140
+ if (!emitted.has(f.id)) {
141
+ emitted.add(f.id)
142
+ const sum = asText(f.summary).slice(0, Math.max(120, maxChars))
143
+ const digest = asText(f.tool_digest)
144
+ const body = digest ? `${sum}\n工具:${digest}` : sum
145
+ out.push({
146
+ id: `fold-${f.id}`,
147
+ role: 'user',
148
+ content: [{
149
+ type: 'text',
150
+ text: `【已压缩的历史 · 第${f.turn ?? '?'}轮】\n${body}\n`
151
+ + `(这段原文已折叠;需要细节时用 \`context search\` 或 \`context unfold ${f.id}\`)`,
152
+ }],
153
+ source: { kind: 'skill-invocation', name: FOLD_SOURCE, form: 'instructions' },
154
+ })
155
+ folded += 1
156
+ }
157
+ changed = true
158
+ }
159
+ if (!changed) return { ok: true, changed: false, folded: 0 }
160
+
161
+ // 首次真正应用后回写 applied(只写一次,避免每步 IO)
162
+ let dirty = false
163
+ for (const f of usable.values()) {
164
+ if (!f.applied) { f.applied = true; dirty = true }
165
+ }
166
+ if (dirty) saveFolds(doc)
167
+
168
+ decision.messages = out
169
+ return { ok: true, changed: true, folded }
170
+ }
@@ -92,3 +92,19 @@ export function clearContextPressure() {
92
92
  rmSync(join(memoryHome(), '.context-pressure.json'), { force: true })
93
93
  } catch { /* 清不掉也不影响:首轮只是显示旧值 */ }
94
94
  }
95
+
96
+ /**
97
+ * 读当前水位样本(不采集,只读盘)。
98
+ *
99
+ * 为什么需要:agent/created 里"先采样、采到就不清"的逻辑要判断样本是否可用,
100
+ * 采样函数本身会写盘——这里提供一个纯读的出口,避免为判断而多写一次盘。
101
+ * 返回 null = 文件不存在/读不了;否则返回解析后的 JSON(可能 available=false)。
102
+ */
103
+ export function readPressureSample() {
104
+ try {
105
+ const raw = readFileSync(join(memoryHome(), '.context-pressure.json'), 'utf8')
106
+ return JSON.parse(raw)
107
+ } catch {
108
+ return null
109
+ }
110
+ }
@@ -0,0 +1,176 @@
1
+ // 灵枢 · APP 上下文快照采集器(v5.34 新增)
2
+ //
3
+ // 为什么需要它:灵枢原先只能读到 `.context-pressure.json` 里的两个数字
4
+ // (pressureTokens / contextWindow)。它知道"水位多高",但完全不知道
5
+ // "上下文里有什么"——几条消息、谁说的、多大、哪些是灵枢自己注入的、
6
+ // 哪些段落可以折叠压缩。于是压缩只能靠宿主自带的粗暴截断,或者靠用户手工开新会话。
7
+ //
8
+ // 本模块把 agent/pre-step 里拿得到的 `decision.messages`(构建完、请求前)
9
+ // 摘成一份**只读描述快照**写到 HOME/.app-context.json,给 brain.py 的
10
+ // `context` 命令和面板读。它不改 messages、不阻塞请求、不抛异常。
11
+ //
12
+ // 与 billion-context 的关系:不依赖它、不做兼容层。这里只是把同一类事实
13
+ // (上下文构成)变成灵枢自己能查、后面能据以决策的本地数据。
14
+ //
15
+ // 设计约束:
16
+ // - 只读 decision,绝不改写(改写留给后续的折叠模块单独负责);
17
+ // - 每个 step 都算,但按"消息数变化 / 超过 20 秒"节流落盘,避免高频 IO;
18
+ // - 任何异常吞掉;写盘失败只影响这份快照,绝不影响请求。
19
+
20
+ import { readFileSync, writeFileSync } from 'node:fs'
21
+ import { homedir } from 'node:os'
22
+ import { join } from 'node:path'
23
+
24
+ const MAX_LISTED = 600 // 快照里最多列多少条消息(够定位,不至于文件过大)
25
+ const MIN_INTERVAL_MS = 20000 // 最短落盘间隔
26
+
27
+ const asText = (v) => String(v ?? '').trim()
28
+
29
+ /** 记忆目录:BRAIN_MEMORY_HOME > ~/.brain-memory/.home 指针 > ~/.brain-memory。 */
30
+ function memoryHome() {
31
+ const env = asText(process.env.BRAIN_MEMORY_HOME)
32
+ if (env) return env
33
+ const base = join(homedir(), '.brain-memory')
34
+ try {
35
+ const ptr = asText(readFileSync(join(base, '.home'), 'utf8'))
36
+ if (ptr) return ptr
37
+ } catch { /* 没有指针文件就用默认目录 */ }
38
+ return base
39
+ }
40
+
41
+ function asArray(v) { return Array.isArray(v) ? v : [] }
42
+
43
+ /** 一条消息的文本总长度(只数真正的 text 段,别把 base64 图片算成文本)。 */
44
+ function textChars(msg) {
45
+ let n = 0
46
+ for (const c of asArray(msg?.content)) {
47
+ if (c && c.type === 'text' && typeof c.text === 'string') n += c.text.length
48
+ }
49
+ if (!n && typeof msg?.content === 'string') n = msg.content.length
50
+ return n
51
+ }
52
+
53
+ /** 这条消息里是否带工具调用 / 是否是工具结果(这两类不能随便折叠,否则 400)。 */
54
+ function isToolish(msg) {
55
+ if (msg?.role === 'tool') return true
56
+ for (const c of asArray(msg?.content)) {
57
+ const t = asText(c?.type).toLowerCase()
58
+ if (t.includes('tool')) return true
59
+ }
60
+ return false
61
+ }
62
+
63
+ /** 灵枢自己注入的(startup / cron / im / guard)——折叠前要先认出来。 */
64
+ function isLingshuInjected(msg) {
65
+ const name = asText(msg?.source?.name)
66
+ return name.startsWith('lingshu')
67
+ }
68
+
69
+ /**
70
+ * 采集一次 APP 上下文快照。由 startup 插件在 agent/pre-step 里调用。
71
+ * @param {object} decision pre-step 的决策对象(含 messages)
72
+ * @param {object} agent 当前 agent(用于取 session 标识)
73
+ */
74
+ export function sampleAppContext(decision, agent) {
75
+ try {
76
+ const msgs = asArray(decision?.messages)
77
+ if (!msgs.length) return null
78
+
79
+ const byRole = {}
80
+ const bySource = {}
81
+ let chars = 0
82
+ let toolCalls = 0
83
+ let toolResults = 0
84
+ let folded = 0
85
+
86
+ const listed = []
87
+ const start = Math.max(0, msgs.length - MAX_LISTED)
88
+ for (let i = 0; i < msgs.length; i++) {
89
+ const m = msgs[i]
90
+ const role = asText(m?.role) || 'unknown'
91
+ byRole[role] = (byRole[role] || 0) + 1
92
+ const src = asText(m?.source?.name) || 'host'
93
+ bySource[src] = (bySource[src] || 0) + 1
94
+ const n = textChars(m)
95
+ chars += n
96
+ if (isToolish(m)) {
97
+ if (role === 'tool') toolResults += 1
98
+ else toolCalls += 1
99
+ }
100
+ // 折叠候选:人/助理的纯文本、不是灵枢注入、不是工具相关
101
+ const foldable = (role === 'user' || role === 'assistant')
102
+ && !isLingshuInjected(m) && !isToolish(m) && n > 0
103
+ if (i >= start) {
104
+ listed.push({
105
+ i,
106
+ id: asText(m?.id).slice(0, 24),
107
+ role,
108
+ chars: n,
109
+ src,
110
+ foldable,
111
+ })
112
+ }
113
+ }
114
+
115
+ // 连续可折叠段:>=4 条才算一段(零散折叠没价值,还会碎掉缓存前缀)
116
+ let runs = 0
117
+ let runMsgs = 0
118
+ let cur = 0
119
+ let curChars = 0
120
+ let bestMsgs = 0
121
+ let bestChars = 0
122
+ const flush = () => {
123
+ if (cur >= 4) { runs += 1; runMsgs += cur }
124
+ if (cur > bestMsgs) { bestMsgs = cur; bestChars = curChars }
125
+ cur = 0
126
+ curChars = 0
127
+ }
128
+ for (const it of listed) {
129
+ if (it.foldable) { cur += 1; curChars += it.chars } else flush()
130
+ }
131
+ flush()
132
+
133
+ const session = asText(agent?.session?.id || agent?.session?.sessionId || '').slice(0, 32)
134
+
135
+ const snap = {
136
+ ts: Math.floor(Date.now() / 1000),
137
+ available: true,
138
+ source: 'app-pre-step',
139
+ session,
140
+ totals: {
141
+ messages: msgs.length,
142
+ characters: chars,
143
+ by_role: byRole,
144
+ tool_calls: toolCalls,
145
+ tool_results: toolResults,
146
+ },
147
+ sources: bySource,
148
+ foldable: {
149
+ runs,
150
+ messages: runMsgs,
151
+ characters: bestChars,
152
+ longest_run: bestMsgs,
153
+ },
154
+ listed_from: start,
155
+ truncated: start > 0,
156
+ messages: listed,
157
+ }
158
+ return throttledWrite(snap)
159
+ } catch {
160
+ return null
161
+ }
162
+ }
163
+
164
+ // 节流:消息数没变且距上次落盘不足 20 秒,就不写。
165
+ let _last = { ts: 0, count: -1 }
166
+
167
+ function throttledWrite(snap) {
168
+ const now = Date.now()
169
+ if (snap.totals.messages === _last.count && now - _last.ts < MIN_INTERVAL_MS) return snap
170
+ _last = { ts: now, count: snap.totals.messages }
171
+ try {
172
+ const file = join(memoryHome(), '.app-context.json')
173
+ writeFileSync(file, JSON.stringify(snap) + '\n', 'utf8')
174
+ } catch { /* 写失败只影响快照,不影响请求 */ }
175
+ return snap
176
+ }