@furongjun1999/dsh-memory 0.4.11 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (504) hide show
  1. package/README.md +16 -16
  2. package/codebuddy/CODEBUDDY.md +11 -3
  3. package/codebuddy/README.md +92 -90
  4. package/codebuddy/mcp.json +27 -27
  5. package/docs/GBrain/345/217/257/345/200/237/351/211/264/347/202/271_/347/201/265/346/236/242/350/220/275/347/202/271/344/272/244/346/216/245_20260919.md +169 -169
  6. package/docs/Pi/345/217/257/345/255/246/344/271/240/344/274/230/347/202/271_/347/201/265/346/236/242/345/244/247/350/204/221/346/224/271/350/277/233/344/272/244/346/216/245_20260915.md +144 -144
  7. package/docs/README.md +142 -111
  8. package/docs/discipline/harnesses.yaml +244 -226
  9. package/docs/discipline/templates/full.md.tmpl +61 -61
  10. package/docs/discipline/templates/rules.mdc.tmpl +68 -0
  11. package/docs/discipline/templates/skill.md.tmpl +23 -23
  12. package/docs/eval/AGI/344/270/203/347/273/264/350/257/204/345/210/206/346/212/245/345/221/212_md_cg_v1.0.md +299 -299
  13. package/docs/eval/AGI/344/270/203/347/273/264/350/257/204/345/210/206/346/212/245/345/221/212_md_cg_v2.0.md +239 -239
  14. package/docs/eval//345/256/236/351/252/214/346/226/271/346/241/210_/345/255/246/344/271/240/351/227/255/347/216/257AB/344/270/216/346/250/252/350/257/204_v1.0.md +174 -174
  15. package/docs/eval//346/250/252/350/257/204_/345/205/255/345/256/266100/351/242/230/344/270/255/350/213/261/345/217/214/346/237/245_v1.0.md +223 -223
  16. package/docs/{mdcg → hive}//344/273/244/347/211/214/344/270/216/350/247/222/350/211/262/346/235/203/350/201/214/345/210/206/347/246/273_v0.1.md +165 -160
  17. package/docs/hive//344/273/244/347/211/214/350/257/255/344/271/211/344/277/256/346/255/243_/344/273/262/350/243/201/344/275/215_v0.1.md +76 -0
  18. package/docs/{mdcg → hive}//345/255/220/344/273/243/347/220/206/351/205/215/347/275/256/346/240/207/345/207/206_v0.5.md +236 -236
  19. package/docs/hive//346/243/200/347/264/242/346/224/266/346/225/233/345/256/236/346/265/213/344/270/216S1b/350/256/276/350/256/241_v0.1.md +43 -43
  20. package/docs/hive//346/243/200/347/264/242/347/256/227/346/263/225/345/217/243/345/276/204/345/257/271/347/205/247_v0.1.md +85 -0
  21. package/docs/hive//346/243/200/347/264/242/350/267/257/345/276/204/344/270/216/350/256/244/347/237/245/347/273/223/346/236/204/345/245/221/347/272/246_v0.1.md +102 -102
  22. package/docs/hive//350/234/202/345/267/242M6_ingest/345/256/236/346/226/275/350/256/241/345/210/222_v0.1.md +47 -0
  23. package/docs/hive//350/234/202/345/267/242/345/217/214/345/256/236/344/276/213/344/272/222/351/252/214_/350/256/276/350/256/241/345/256/232/347/250/277.md +503 -503
  24. package/docs/hive//350/234/202/345/267/242/345/267/245/344/275/234/350/256/260/345/277/206_/351/241/271/347/233/256/350/256/241/345/210/222.md +85 -85
  25. package/docs/hive//350/234/202/345/267/242/350/256/276/350/256/241_/347/220/206/350/256/272/345/257/271/351/275/220_v0.1.md +191 -0
  26. package/docs/hive//350/234/202/345/267/242/350/277/255/344/273/243_/345/256/217/350/247/202/344/270/216/347/276/244/344/275/223/350/260/203/345/272/246_v0.1.md +90 -0
  27. package/docs/mdcg/D_meta_/345/267/245/347/250/213/345/214/226/346/226/271/346/241/210_v0.2.md +216 -216
  28. package/docs/mdcg/README/350/257/246/347/273/206/347/211/210_v0.4.10.md +646 -646
  29. package/docs/mdcg/lingshu_tutorial.html +14449 -14449
  30. package/docs/mdcg/release_v0.4.11.md +49 -0
  31. package/docs/mdcg/release_v0.4.5.md +55 -55
  32. package/docs/mdcg/tool_table_v0.3.0.md +117 -117
  33. package/docs/mdcg//345/205/250/345/272/223/344/273/243/347/240/201/350/257/204/345/256/241/344/270/216/346/235/241/344/273/266/345/214/226/346/263/250/351/207/212_/350/256/241/345/210/222_v0.1.md +600 -600
  34. package/docs/mdcg//345/212/237/350/203/275/350/260/203/347/224/250/346/230/240/345/260/204/350/241/250_v0.1.md +40 -40
  35. package/docs/mdcg//345/215/225/345/205/203/350/207/252/346/210/221/351/224/232/347/202/271_/347/263/273/347/273/237/346/217/220/347/244/272/350/257/215/346/240/207/345/207/206_v0.3.md +275 -275
  36. package/docs/mdcg//345/217/221/345/270/203/351/227/250/347/246/201/351/223/276_v0.1.md +54 -0
  37. package/docs/mdcg//346/272/220/347/240/201/347/272/247/346/236/266/346/236/204/345/256/241/350/256/241_GPT/346/211/271/350/257/204/345/257/271/347/205/247_v1.0.md +130 -130
  38. package/docs/mdcg//347/201/265/346/236/24282/345/267/245/345/205/267_/345/212/237/350/203/275/346/225/264/347/220/206/344/270/216/350/277/201/347/247/273/346/230/240/345/260/204_v0.1.md +278 -278
  39. package/docs/mdcg//347/201/265/346/236/242/350/256/260/345/277/206/345/212/250/350/257/215/345/215/217/350/256/256_v1.0-draft.md +172 -172
  40. package/docs/mdcg//347/274/272/345/217/243/345/215/225_P0/346/224/266/345/217/243_v0.1.md +270 -270
  41. package/docs/mdcg//350/256/244/347/237/245/345/233/276_G4-G8/347/274/272/345/217/243/350/243/201/345/256/232/345/215/225_v0.1.md +491 -491
  42. package/docs/mdcg//350/256/244/347/237/245/345/233/276_/346/235/241/344/273/266/347/251/272/351/227/264/345/220/210/346/210/220/344/270/216/347/224/237/346/225/210/346/235/241/344/273/266/345/217/243/345/276/204_v0.1.md +340 -340
  43. package/docs/mdcg//350/256/244/347/237/245/345/233/276_/347/264/242/345/274/225/344/270/216/345/267/245/347/250/213/350/247/204/350/214/203/345/214/226_/350/256/241/345/210/222_v0.1.md +588 -588
  44. package/docs/plans/GridWorld/346/234/200/345/260/217/351/227/255/347/216/257/350/247/204/346/240/274_v0.1.md +176 -176
  45. package/docs/plans//345/221/275/345/220/215/346/262/273/347/220/206_/351/241/271/347/233/256/350/256/241/345/210/222.md +81 -81
  46. package/docs/swarm//350/234/202/347/276/244/344/272/222/350/201/224_v0.1.md +704 -704
  47. package/docs/swarm//350/234/202/347/276/244/345/220/214/351/224/231/346/243/200/346/265/213/345/256/236/351/252/214/345/215/217/350/256/256_v0.1.md +242 -242
  48. package/docs/theory//345/215/225/347/272/277/347/250/213/344/270/216/346/263/250/346/204/217/345/212/233/351/233/206/344/270/255_/346/227/240/344/272/211/350/256/256/347/220/206/350/256/272/346/226/207/346/241/243_v1.0.md +129 -129
  49. package/docs/theory//345/271/266/345/217/221/345/277/205/347/204/266/346/200/247/347/220/206/350/256/272_v0.2.md +134 -134
  50. package/docs/theory//346/231/272/350/203/275/350/256/2723.4.md +5260 -5260
  51. package/docs/theory//346/246/202/345/277/265/345/210/206/345/261/202/345/257/271/351/275/220/350/241/250_v0.1.md +145 -145
  52. package/docs/theory//347/220/206/350/256/272_/346/234/272/345/210/266_/344/273/243/347/240/201_/345/256/236/351/252/214_/347/274/272/345/217/243/347/237/251/351/230/265_v0.1.md +144 -144
  53. package/docs/theory//347/220/206/350/256/272/344/273/223/346/213/206/345/210/206/344/270/216/347/231/275/347/256/261/347/237/245/350/257/206/345/272/223/345/206/205/350/277/201_v0.1.md +198 -198
  54. package/docs/theory//350/256/244/347/237/245/344/273/243/347/220/206/344/270/216/346/224/266/346/225/233/347/273/223/346/236/204_/346/235/241/344/273/266/350/256/272/351/207/215/346/236/204_v0.1.md +221 -221
  55. package/docs/world_model//344/270/226/347/225/214/346/250/241/345/236/213_/347/245/236/347/273/217/347/275/221/347/273/234/345/272/225/345/261/202/346/236/266/346/236/204_v1.0.md +237 -237
  56. package/docs//345/217/221/345/270/203/344/273/266/345/233/236/346/272/257/350/257/264/346/230/216_v0.1.md +82 -82
  57. package/docs//345/267/245/344/275/234/347/272/252/345/276/213_/350/256/244/347/237/245/345/233/276/346/235/241/347/233/256_v1.1.json +28 -2
  58. package/dsh/README.md +82 -82
  59. package/dsh/cordis.yml.example +139 -139
  60. package/dsh/update-lingshu.bat +11 -11
  61. package/lib/hooks.js +36 -2
  62. package/lib/lib/roleplay_web.js +427 -427
  63. package/md_cg/__init__.py +7 -7
  64. package/md_cg/audit.py +368 -368
  65. package/md_cg/autonomy.py +287 -287
  66. package/md_cg/backfill.py +1327 -1327
  67. package/md_cg/backfill_bigdomain.py +34 -34
  68. package/md_cg/bench6_arms.py +410 -410
  69. package/md_cg/bench6_common.py +230 -230
  70. package/md_cg/bench6_competitors.py +212 -212
  71. package/md_cg/bench_axis_domain.py +257 -257
  72. package/md_cg/bench_blind_comp.py +308 -308
  73. package/md_cg/bench_en_atoms_public.py +230 -230
  74. package/md_cg/bench_governance.py +348 -348
  75. package/md_cg/bench_lme_zh.py +410 -410
  76. package/md_cg/bench_locomo.py +121 -121
  77. package/md_cg/bench_locomo_zh.py +450 -450
  78. package/md_cg/bench_locomo_zh_public.py +147 -147
  79. package/md_cg/bench_longmem.py +112 -112
  80. package/md_cg/bench_membench.py +632 -632
  81. package/md_cg/bench_p0.py +149 -149
  82. package/md_cg/bench_progressive.py +287 -287
  83. package/md_cg/bench_role_views.py +238 -238
  84. package/md_cg/bench_task_ab.py +243 -243
  85. package/md_cg/bench_task_ab_llm.py +408 -408
  86. package/md_cg/bench_unified_en.py +204 -204
  87. package/md_cg/bench_zh_mad.py +601 -601
  88. package/md_cg/blindspot_tickets.py +123 -123
  89. package/md_cg/branches.py +285 -285
  90. package/md_cg/build_postings.py +73 -73
  91. package/md_cg/ccgc.py +1005 -948
  92. package/md_cg/census.py +132 -132
  93. package/md_cg/chain.py +300 -300
  94. package/md_cg/codeindex.py +531 -531
  95. package/md_cg/coldverify.py +292 -292
  96. package/md_cg/comment_gate.py +337 -337
  97. package/md_cg/cond_compose.py +190 -190
  98. package/md_cg/cond_facts.py +154 -154
  99. package/md_cg/cond_template.json +106 -106
  100. package/md_cg/condition_anchor.py +142 -142
  101. package/md_cg/conformance.py +726 -726
  102. package/md_cg/consistency.py +717 -717
  103. package/md_cg/consolidate.py +1536 -1439
  104. package/md_cg/corpus.py +110 -110
  105. package/md_cg/crosscheck.py +1097 -1097
  106. package/md_cg/crypto.py +437 -437
  107. package/md_cg/d_meta.py +310 -310
  108. package/md_cg/datapath.py +334 -334
  109. package/md_cg/docindex.py +473 -473
  110. package/md_cg/eval_common.py +575 -575
  111. package/md_cg/evidence.py +580 -580
  112. package/md_cg/evolution.py +477 -477
  113. package/md_cg/export.py +220 -220
  114. package/md_cg/forgetting.py +581 -581
  115. package/md_cg/fsutil.py +329 -329
  116. package/md_cg/hotcache.py +238 -214
  117. package/md_cg/hyperedge.py +251 -251
  118. package/md_cg/identity.py +390 -390
  119. package/md_cg/insight.py +500 -500
  120. package/md_cg/interop.py +199 -0
  121. package/md_cg/lexicon/build_cedict_en_zh.py +329 -329
  122. package/md_cg/lexicon/build_standard_en.py +171 -171
  123. package/md_cg/lexicon/expand_en_zh.py +211 -211
  124. package/md_cg/lifecycle.py +272 -272
  125. package/md_cg/linkref.py +280 -280
  126. package/md_cg/links.py +622 -622
  127. package/md_cg/mcp_server.py +129 -32
  128. package/md_cg/md_whitebox.py +345 -345
  129. package/md_cg/mdcg.py +176 -117
  130. package/md_cg/mdcos.py +79 -13
  131. package/md_cg/metacognition.py +591 -591
  132. package/md_cg/migrate.py +119 -119
  133. package/md_cg/migrate_aeis.py +221 -221
  134. package/md_cg/migrate_roleplay.py +293 -293
  135. package/md_cg/migrate_wisdom_graph.py +360 -360
  136. package/md_cg/mreview/__init__.py +25 -25
  137. package/md_cg/mreview/__main__.py +110 -110
  138. package/md_cg/mreview/bundle.py +178 -178
  139. package/md_cg/mreview/candidates.py +262 -262
  140. package/md_cg/mreview/govern.py +693 -693
  141. package/md_cg/mreview/locate.py +939 -939
  142. package/md_cg/mreview/pipeline.py +728 -728
  143. package/md_cg/mreview/rules/duplication.json +21 -21
  144. package/md_cg/mreview/rules/field_coverage.json +54 -54
  145. package/md_cg/mreview/rules/source_license.json +21 -21
  146. package/md_cg/mreview/rules/template_flow.json +21 -21
  147. package/md_cg/mreview/ruleset.py +252 -252
  148. package/md_cg/nodefile.py +575 -575
  149. package/md_cg/pooling.py +484 -472
  150. package/md_cg/postings.py +298 -298
  151. package/md_cg/predict.py +1100 -1100
  152. package/md_cg/progressive.py +123 -123
  153. package/md_cg/protect.py +272 -272
  154. package/md_cg/protocol/md_cg_gate.proto +33 -33
  155. package/md_cg/protocol.py +372 -372
  156. package/md_cg/provenance.py +582 -582
  157. package/md_cg/reach.py +453 -453
  158. package/md_cg/readcache.py +85 -0
  159. package/md_cg/refindex.py +833 -833
  160. package/md_cg/refine.py +604 -604
  161. package/md_cg/roleviews.py +89 -89
  162. package/md_cg/routing.py +365 -365
  163. package/md_cg/scrub.py +852 -852
  164. package/md_cg/security.py +274 -274
  165. package/md_cg/self_state.py +1029 -1029
  166. package/md_cg/selfreport.py +151 -151
  167. package/md_cg/semantic/__init__.py +10 -10
  168. package/md_cg/semantic/canonical.py +122 -122
  169. package/md_cg/semantic/en_normalizer.py +364 -364
  170. package/md_cg/semantic/en_zh_map.json +28694 -0
  171. package/md_cg/semantic/export_en_zh_map.py +64 -0
  172. package/md_cg/semantic/unify.py +45 -0
  173. package/md_cg/semantic/zh_en_atoms.py +139 -139
  174. package/md_cg/signer.py +562 -562
  175. package/md_cg/sources.py +815 -582
  176. package/md_cg/statushdr.py +179 -179
  177. package/md_cg/stg.py +48 -37
  178. package/md_cg/subgraph.py +729 -729
  179. package/md_cg/sustain.py +1138 -1138
  180. package/md_cg/tasks.py +470 -470
  181. package/md_cg/test_action_derive.py +203 -203
  182. package/md_cg/test_audit_rotate.py +270 -270
  183. package/md_cg/test_autonomy.py +143 -143
  184. package/md_cg/test_bench_governance.py +102 -102
  185. package/md_cg/test_blindspot_tickets.py +166 -166
  186. package/md_cg/test_branches.py +249 -249
  187. package/md_cg/test_ccg_perturb.py +184 -184
  188. package/md_cg/test_ccgc.py +433 -433
  189. package/md_cg/test_census_prune.py +81 -81
  190. package/md_cg/test_cond_compose_anchors.py +76 -76
  191. package/md_cg/test_cond_match.py +165 -165
  192. package/md_cg/test_condition_anchor.py +81 -81
  193. package/md_cg/test_d_meta.py +412 -412
  194. package/md_cg/test_datapath_root.py +199 -199
  195. package/md_cg/test_en_pipeline.py +166 -166
  196. package/md_cg/test_gain_gate.py +212 -212
  197. package/md_cg/test_health_scale.py +173 -173
  198. package/md_cg/test_hive_ingest.py +285 -0
  199. package/md_cg/test_hot_cold.py +215 -215
  200. package/md_cg/test_hyperedge.py +245 -245
  201. package/md_cg/test_i26_empty_first_write.py +116 -0
  202. package/md_cg/test_i27_e041_identity.py +128 -0
  203. package/md_cg/test_i28_hotcache_prodpath.py +122 -0
  204. package/md_cg/test_identity_attribution.py +147 -147
  205. package/md_cg/test_index_durability.py +224 -224
  206. package/md_cg/test_interop.py +93 -0
  207. package/md_cg/test_lifecycle.py +309 -309
  208. package/md_cg/test_linkref.py +306 -306
  209. package/md_cg/test_lock.py +43 -43
  210. package/md_cg/test_md_access_parity.py +255 -255
  211. package/md_cg/test_md_writepath.py +345 -345
  212. package/md_cg/test_mdstore_search_parity.py +160 -0
  213. package/md_cg/test_mr_m2.py +587 -587
  214. package/md_cg/test_mr_m3.py +710 -710
  215. package/md_cg/test_mr_m4.py +485 -485
  216. package/md_cg/test_p0.py +250 -250
  217. package/md_cg/test_p1.py +316 -316
  218. package/md_cg/test_p10_identity.py +173 -173
  219. package/md_cg/test_p11_consistency.py +233 -233
  220. package/md_cg/test_p12_metacognition.py +212 -212
  221. package/md_cg/test_p13_encryption.py +241 -241
  222. package/md_cg/test_p14_sustain.py +249 -249
  223. package/md_cg/test_p15_scrub.py +280 -280
  224. package/md_cg/test_p16_self_state.py +301 -301
  225. package/md_cg/test_p17_predict.py +354 -354
  226. package/md_cg/test_p18_whitebox.py +171 -171
  227. package/md_cg/test_p19_migrate_roleplay.py +149 -149
  228. package/md_cg/test_p20_evolution.py +315 -315
  229. package/md_cg/test_p21_tokens.py +293 -270
  230. package/md_cg/test_p22_theory.py +175 -175
  231. package/md_cg/test_p23_links.py +311 -311
  232. package/md_cg/test_p24_evidence.py +227 -227
  233. package/md_cg/test_p25_weights.py +156 -156
  234. package/md_cg/test_p26_refindex.py +416 -416
  235. package/md_cg/test_p27_docindex.py +765 -765
  236. package/md_cg/test_p28_refcheck.py +305 -305
  237. package/md_cg/test_p29_session_ingest_export.py +354 -333
  238. package/md_cg/test_p3.py +11 -2
  239. package/md_cg/test_p30_maintain.py +330 -330
  240. package/md_cg/test_p31_insight.py +534 -534
  241. package/md_cg/test_p32_backfill.py +298 -298
  242. package/md_cg/test_p33_ccg_wiring.py +293 -293
  243. package/md_cg/test_p34_crosscheck.py +331 -331
  244. package/md_cg/test_p35_conditioned_claim.py +252 -252
  245. package/md_cg/test_p36_kp_align.py +230 -230
  246. package/md_cg/test_p37_condition_space.py +248 -248
  247. package/md_cg/test_p38_concurrent_flush.py +102 -0
  248. package/md_cg/test_p38_contextualize.py +273 -273
  249. package/md_cg/test_p39_verify_flow.py +113 -0
  250. package/md_cg/test_p39_vision_evidence.py +369 -369
  251. package/md_cg/test_p40_refine_worklist.py +241 -241
  252. package/md_cg/test_p41_evolve_patrol.py +224 -224
  253. package/md_cg/test_p42_provenance.py +269 -269
  254. package/md_cg/test_p43_pooling.py +412 -398
  255. package/md_cg/test_p44_md_whitebox.py +231 -231
  256. package/md_cg/test_p45_session_identity.py +219 -219
  257. package/md_cg/test_p46_unit_scope.py +272 -272
  258. package/md_cg/test_p47_session_view.py +281 -0
  259. package/md_cg/test_p4_fuzzy.py +223 -223
  260. package/md_cg/test_p5_semantic.py +226 -226
  261. package/md_cg/test_p6_consolidate.py +440 -387
  262. package/md_cg/test_p7_goals_recent.py +202 -202
  263. package/md_cg/test_p8_subgraph_chain.py +200 -200
  264. package/md_cg/test_p9_forget_protect.py +231 -231
  265. package/md_cg/test_predict_beta.py +135 -135
  266. package/md_cg/test_preflight_failclosed.py +100 -100
  267. package/md_cg/test_progressive.py +146 -146
  268. package/md_cg/test_protocol.py +243 -243
  269. package/md_cg/test_reach.py +378 -378
  270. package/md_cg/test_reach_keys.py +201 -201
  271. package/md_cg/test_read_clip.py +141 -141
  272. package/md_cg/test_readcache_prodpath.py +155 -0
  273. package/md_cg/test_retr_gates_prodpath.py +140 -0
  274. package/md_cg/test_retr_s1.py +340 -340
  275. package/md_cg/test_retr_s1b.py +209 -209
  276. package/md_cg/test_retr_s3.py +194 -194
  277. package/md_cg/test_retr_s4.py +163 -163
  278. package/md_cg/test_retr_s5.py +200 -200
  279. package/md_cg/test_retr_s6.py +157 -157
  280. package/md_cg/test_retr_s7.py +384 -384
  281. package/md_cg/test_retr_s8_time.py +369 -316
  282. package/md_cg/test_retr_s9_edges.py +286 -286
  283. package/md_cg/test_retr_s9_entity_ctx.py +175 -175
  284. package/md_cg/test_review_conformance.py +367 -367
  285. package/md_cg/test_role_views.py +354 -354
  286. package/md_cg/test_sem_noise.py +242 -242
  287. package/md_cg/test_semantic_canonical.py +241 -241
  288. package/md_cg/test_subproc_encoding.py +192 -192
  289. package/md_cg/test_sustain_mutual.py +153 -153
  290. package/md_cg/test_tasks.py +409 -409
  291. package/md_cg/test_tool_face.py +189 -189
  292. package/md_cg/test_transfer.py +180 -180
  293. package/md_cg/test_trust.py +361 -361
  294. package/md_cg/test_twophase.py +286 -286
  295. package/md_cg/test_v14_fixes.py +397 -397
  296. package/md_cg/test_validity_filter.py +280 -280
  297. package/md_cg/test_verify_answer.py +138 -138
  298. package/md_cg/test_wisdom_md_store.py +292 -292
  299. package/md_cg/test_writelimit.py +197 -197
  300. package/md_cg/test_writepipe.py +214 -214
  301. package/md_cg/theory.py +273 -273
  302. package/md_cg/tokens.py +677 -663
  303. package/md_cg/tool_face.py +260 -260
  304. package/md_cg/trust.py +986 -950
  305. package/md_cg/twophase.py +231 -231
  306. package/md_cg/units.py +667 -667
  307. package/md_cg/vision_evidence.py +666 -666
  308. package/md_cg/weights.py +624 -624
  309. package/md_cg/whitebox.py +527 -527
  310. package/md_cg/whitebox_kb/__init__.py +37 -37
  311. package/md_cg/whitebox_kb/aeis_core/__init__.py +42 -42
  312. package/md_cg/whitebox_kb/aeis_core/semantic.py +280 -280
  313. package/md_cg/whitebox_kb/aeis_core/textutil.py +13 -13
  314. package/md_cg/whitebox_kb/engine.py +310 -310
  315. package/md_cg/whitebox_kb/seed_knowledge//346/231/272/350/203/275/350/256/2723.4.md +5260 -5260
  316. package/md_cg/whitebox_kb/wisdom/browser_units.py +2631 -2631
  317. package/md_cg/whitebox_kb/wisdom/causal_discover.py +432 -432
  318. package/md_cg/whitebox_kb/wisdom/chat_engine.py +1506 -1506
  319. package/md_cg/whitebox_kb/wisdom/code_solidified.json +6195 -6195
  320. package/md_cg/whitebox_kb/wisdom/compiler_code_units.py +3033 -3033
  321. package/md_cg/whitebox_kb/wisdom/condition_algebra.py +112 -112
  322. package/md_cg/whitebox_kb/wisdom/condition_frame.py +315 -315
  323. package/md_cg/whitebox_kb/wisdom/condition_kb.py +108 -108
  324. package/md_cg/whitebox_kb/wisdom/conflict_map.json +4445 -4445
  325. package/md_cg/whitebox_kb/wisdom/core/lexer.py +512 -512
  326. package/md_cg/whitebox_kb/wisdom/core/name_checker.py +1023 -1023
  327. package/md_cg/whitebox_kb/wisdom/cspmn.py +258 -258
  328. package/md_cg/whitebox_kb/wisdom/csre.py +264 -264
  329. package/md_cg/whitebox_kb/wisdom/danmaku_audit.py +252 -252
  330. package/md_cg/whitebox_kb/wisdom/distilled_condition_units.json +6417 -6417
  331. package/md_cg/whitebox_kb/wisdom/docs/WB-EVAL-20260902b.json +1950 -1950
  332. package/md_cg/whitebox_kb/wisdom/docs/WB-EVAL-20260902c.json +1296 -1296
  333. package/md_cg/whitebox_kb/wisdom/docs/whitebox_capability_graph_demo.json +59 -59
  334. package/md_cg/whitebox_kb/wisdom/graph_db_units.py +3089 -3089
  335. package/md_cg/whitebox_kb/wisdom/knowledge_points.py +318 -318
  336. package/md_cg/whitebox_kb/wisdom/md_access.py +470 -470
  337. package/md_cg/whitebox_kb/wisdom/md_store.py +276 -251
  338. package/md_cg/whitebox_kb/wisdom/migrate_wisdom.py +476 -476
  339. package/md_cg/whitebox_kb/wisdom/navigate.py +248 -248
  340. package/md_cg/whitebox_kb/wisdom/neural_retrieve.py +224 -224
  341. package/md_cg/whitebox_kb/wisdom/os_units.py +2735 -2735
  342. package/md_cg/whitebox_kb/wisdom/pattern_separation.py +376 -376
  343. package/md_cg/whitebox_kb/wisdom/prereq_map.json +364 -364
  344. package/md_cg/whitebox_kb/wisdom/python_code_units.py +2766 -2766
  345. package/md_cg/whitebox_kb/wisdom/role_solidified.json +7 -7
  346. package/md_cg/whitebox_kb/wisdom/route_memory.py +244 -244
  347. package/md_cg/whitebox_kb/wisdom/scene_reconstruction.py +148 -148
  348. package/md_cg/whitebox_kb/wisdom/snr_report.json +40 -40
  349. package/md_cg/whitebox_kb/wisdom/test_code_compose_domains.py +7541 -7541
  350. package/md_cg/whitebox_kb/wisdom/test_compiler_self_bootstrap.py +354 -354
  351. package/md_cg/whitebox_kb/wisdom/test_ecosystem_assembly.py +158 -158
  352. package/md_cg/whitebox_kb/wisdom/test_ecosystem_demos.py +193 -193
  353. package/md_cg/whitebox_kb/wisdom/test_graph_db.py +292 -292
  354. package/md_cg/whitebox_kb/wisdom/test_python_self_bootstrap.py +160 -160
  355. package/md_cg/whitebox_kb/wisdom/trigger_words_index.json +4366 -4366
  356. package/md_cg/whitebox_kb/wisdom/verifier.py +1297 -1297
  357. package/md_cg/writelimit.py +356 -356
  358. package/md_cg/writepipe.py +550 -542
  359. package/package.json +97 -96
  360. package/skills/plugin.json +54 -54
  361. package/skills/skills/designer-perspective/SKILL.md +158 -158
  362. package/skills/skills/designer-perspective/references/01-observation-position.md +66 -66
  363. package/skills/skills/designer-perspective/references/02-structure-recognition.md +62 -62
  364. package/skills/skills/designer-perspective/references/03-direction-judgment.md +55 -55
  365. package/skills/skills/designer-perspective/references/04-qualification-verdict.md +72 -72
  366. package/skills/skills/designer-perspective/references/05-condition-attribution.md +74 -74
  367. package/skills/skills/designer-perspective/scripts/designer.py +545 -545
  368. package/skills/skills/designer-perspective/tests/cases.jsonl +17 -17
  369. package/skills/skills/designer-perspective/tests/selftest.py +61 -61
  370. package/skills/skills/lingshu-browser/SKILL.md +60 -60
  371. package/skills/skills/lingshu-compiler/SKILL.md +56 -56
  372. package/skills/skills/lingshu-compiler/units/analyze-type-infer/SKILL.md +45 -45
  373. package/skills/skills/lingshu-compiler/units/check-name-real/SKILL.md +45 -45
  374. package/skills/skills/lingshu-compiler/units/compile-assign/SKILL.md +45 -45
  375. package/skills/skills/lingshu-compiler/units/compile-expr-tree/SKILL.md +45 -45
  376. package/skills/skills/lingshu-compiler/units/compile-full-pipeline/SKILL.md +45 -45
  377. package/skills/skills/lingshu-compiler/units/compile-func-def/SKILL.md +45 -45
  378. package/skills/skills/lingshu-compiler/units/compile-if-then/SKILL.md +45 -45
  379. package/skills/skills/lingshu-compiler/units/compile-logic-expr/SKILL.md +45 -45
  380. package/skills/skills/lingshu-compiler/units/compile-recursive/SKILL.md +45 -45
  381. package/skills/skills/lingshu-compiler/units/compile-scope/SKILL.md +45 -45
  382. package/skills/skills/lingshu-compiler/units/compile-type-check/SKILL.md +45 -45
  383. package/skills/skills/lingshu-compiler/units/compile-while/SKILL.md +45 -45
  384. package/skills/skills/lingshu-compiler/units/compiler-0010c4bf/SKILL.md +45 -45
  385. package/skills/skills/lingshu-compiler/units/compiler-0355bffb/SKILL.md +45 -45
  386. package/skills/skills/lingshu-compiler/units/compiler-0361708a/SKILL.md +45 -45
  387. package/skills/skills/lingshu-compiler/units/compiler-054a0414/SKILL.md +45 -45
  388. package/skills/skills/lingshu-compiler/units/compiler-05a1691a/SKILL.md +45 -45
  389. package/skills/skills/lingshu-compiler/units/compiler-05eeed1e/SKILL.md +45 -45
  390. package/skills/skills/lingshu-compiler/units/compiler-0622a1f6/SKILL.md +45 -45
  391. package/skills/skills/lingshu-compiler/units/compiler-08b54217/SKILL.md +45 -45
  392. package/skills/skills/lingshu-compiler/units/compiler-0a62b70c/SKILL.md +45 -45
  393. package/skills/skills/lingshu-compiler/units/compiler-0ab24d00/SKILL.md +45 -45
  394. package/skills/skills/lingshu-compiler/units/compiler-0e093688/SKILL.md +45 -45
  395. package/skills/skills/lingshu-compiler/units/compiler-0e82b966/SKILL.md +45 -45
  396. package/skills/skills/lingshu-compiler/units/compiler-0ee9b9b9/SKILL.md +45 -45
  397. package/skills/skills/lingshu-compiler/units/compiler-0f3787e6/SKILL.md +45 -45
  398. package/skills/skills/lingshu-compiler/units/compiler-1028685f/SKILL.md +45 -45
  399. package/skills/skills/lingshu-compiler/units/compiler-11897630/SKILL.md +45 -45
  400. package/skills/skills/lingshu-compiler/units/compiler-16661b9b/SKILL.md +45 -45
  401. package/skills/skills/lingshu-compiler/units/compiler-1f722303/SKILL.md +45 -45
  402. package/skills/skills/lingshu-compiler/units/compiler-25be1262/SKILL.md +45 -45
  403. package/skills/skills/lingshu-compiler/units/compiler-2a76ba07/SKILL.md +45 -45
  404. package/skills/skills/lingshu-compiler/units/compiler-2dbea54a/SKILL.md +45 -45
  405. package/skills/skills/lingshu-compiler/units/compiler-2df52f16/SKILL.md +45 -45
  406. package/skills/skills/lingshu-compiler/units/compiler-2ec2c9d2/SKILL.md +45 -45
  407. package/skills/skills/lingshu-compiler/units/compiler-2f8c8f39/SKILL.md +45 -45
  408. package/skills/skills/lingshu-compiler/units/compiler-38378ac7/SKILL.md +45 -45
  409. package/skills/skills/lingshu-compiler/units/compiler-39457d2e/SKILL.md +45 -45
  410. package/skills/skills/lingshu-compiler/units/compiler-39fb5926/SKILL.md +45 -45
  411. package/skills/skills/lingshu-compiler/units/compiler-47f4fbfa/SKILL.md +45 -45
  412. package/skills/skills/lingshu-compiler/units/compiler-4a8cd1f1/SKILL.md +45 -45
  413. package/skills/skills/lingshu-compiler/units/compiler-4b230b6b/SKILL.md +45 -45
  414. package/skills/skills/lingshu-compiler/units/compiler-4cbbda95/SKILL.md +45 -45
  415. package/skills/skills/lingshu-compiler/units/compiler-4d5a68ff/SKILL.md +45 -45
  416. package/skills/skills/lingshu-compiler/units/compiler-56dc9bdd/SKILL.md +45 -45
  417. package/skills/skills/lingshu-compiler/units/compiler-57e76ebe/SKILL.md +45 -45
  418. package/skills/skills/lingshu-compiler/units/compiler-61ff016b/SKILL.md +45 -45
  419. package/skills/skills/lingshu-compiler/units/compiler-63eae588/SKILL.md +45 -45
  420. package/skills/skills/lingshu-compiler/units/compiler-64c4224b/SKILL.md +45 -45
  421. package/skills/skills/lingshu-compiler/units/compiler-66377955/SKILL.md +45 -45
  422. package/skills/skills/lingshu-compiler/units/compiler-674ab4f6/SKILL.md +45 -45
  423. package/skills/skills/lingshu-compiler/units/compiler-6af76fd6/SKILL.md +45 -45
  424. package/skills/skills/lingshu-compiler/units/compiler-6d979db2/SKILL.md +45 -45
  425. package/skills/skills/lingshu-compiler/units/compiler-6de326c0/SKILL.md +45 -45
  426. package/skills/skills/lingshu-compiler/units/compiler-6f1c0eea/SKILL.md +45 -45
  427. package/skills/skills/lingshu-compiler/units/compiler-724d6c9b/SKILL.md +45 -45
  428. package/skills/skills/lingshu-compiler/units/compiler-7690177f/SKILL.md +45 -45
  429. package/skills/skills/lingshu-compiler/units/compiler-7b229a7d/SKILL.md +45 -45
  430. package/skills/skills/lingshu-compiler/units/compiler-7f471b3a/SKILL.md +45 -45
  431. package/skills/skills/lingshu-compiler/units/compiler-815cad08/SKILL.md +45 -45
  432. package/skills/skills/lingshu-compiler/units/compiler-83c61634/SKILL.md +45 -45
  433. package/skills/skills/lingshu-compiler/units/compiler-8c798c81/SKILL.md +45 -45
  434. package/skills/skills/lingshu-compiler/units/compiler-8dd747ac/SKILL.md +45 -45
  435. package/skills/skills/lingshu-compiler/units/compiler-90324d0a/SKILL.md +45 -45
  436. package/skills/skills/lingshu-compiler/units/compiler-94b8d72d/SKILL.md +45 -45
  437. package/skills/skills/lingshu-compiler/units/compiler-94f12231/SKILL.md +45 -45
  438. package/skills/skills/lingshu-compiler/units/compiler-95937c16/SKILL.md +45 -45
  439. package/skills/skills/lingshu-compiler/units/compiler-98a5625b/SKILL.md +45 -45
  440. package/skills/skills/lingshu-compiler/units/compiler-98b4c42f/SKILL.md +45 -45
  441. package/skills/skills/lingshu-compiler/units/compiler-98e3894a/SKILL.md +45 -45
  442. package/skills/skills/lingshu-compiler/units/compiler-9bdfe4b8/SKILL.md +45 -45
  443. package/skills/skills/lingshu-compiler/units/compiler-9d9b4e83/SKILL.md +45 -45
  444. package/skills/skills/lingshu-compiler/units/compiler-9f7be5ad/SKILL.md +45 -45
  445. package/skills/skills/lingshu-compiler/units/compiler-a6daf076/SKILL.md +45 -45
  446. package/skills/skills/lingshu-compiler/units/compiler-a7995a0c/SKILL.md +45 -45
  447. package/skills/skills/lingshu-compiler/units/compiler-a8399248/SKILL.md +45 -45
  448. package/skills/skills/lingshu-compiler/units/compiler-aabbd099/SKILL.md +45 -45
  449. package/skills/skills/lingshu-compiler/units/compiler-afe169d8/SKILL.md +45 -45
  450. package/skills/skills/lingshu-compiler/units/compiler-b0678bda/SKILL.md +45 -45
  451. package/skills/skills/lingshu-compiler/units/compiler-b09bd196/SKILL.md +45 -45
  452. package/skills/skills/lingshu-compiler/units/compiler-b1396e23/SKILL.md +45 -45
  453. package/skills/skills/lingshu-compiler/units/compiler-b9b31ce0/SKILL.md +45 -45
  454. package/skills/skills/lingshu-compiler/units/compiler-c10264a7/SKILL.md +45 -45
  455. package/skills/skills/lingshu-compiler/units/compiler-cb1e8e4b/SKILL.md +45 -45
  456. package/skills/skills/lingshu-compiler/units/compiler-ccafd438/SKILL.md +45 -45
  457. package/skills/skills/lingshu-compiler/units/compiler-cda9c262/SKILL.md +45 -45
  458. package/skills/skills/lingshu-compiler/units/compiler-ce648068/SKILL.md +45 -45
  459. package/skills/skills/lingshu-compiler/units/compiler-cf5776a4/SKILL.md +45 -45
  460. package/skills/skills/lingshu-compiler/units/compiler-d974e5d3/SKILL.md +45 -45
  461. package/skills/skills/lingshu-compiler/units/compiler-e3979fd3/SKILL.md +45 -45
  462. package/skills/skills/lingshu-compiler/units/compiler-eb1cf2b5/SKILL.md +45 -45
  463. package/skills/skills/lingshu-compiler/units/compiler-ecb30d5b/SKILL.md +45 -45
  464. package/skills/skills/lingshu-compiler/units/compiler-f8c8b24b/SKILL.md +45 -45
  465. package/skills/skills/lingshu-compiler/units/compiler-f99fedbe/SKILL.md +45 -45
  466. package/skills/skills/lingshu-compiler/units/compiler-fa8ff5f7/SKILL.md +45 -45
  467. package/skills/skills/lingshu-compiler/units/compiler-fe1b058d/SKILL.md +45 -45
  468. package/skills/skills/lingshu-compiler/units/lex-chinese-program/SKILL.md +45 -45
  469. package/skills/skills/lingshu-compiler/units/lex-dao-de-jing/SKILL.md +45 -45
  470. package/skills/skills/lingshu-compiler/units/lex-nine-chapters/SKILL.md +45 -45
  471. package/skills/skills/lingshu-compiler/units/vm-arithmetic/SKILL.md +45 -45
  472. package/skills/skills/lingshu-compiler/units/vm-array-ops/SKILL.md +45 -45
  473. package/skills/skills/lingshu-compiler/units/vm-closure-call/SKILL.md +45 -45
  474. package/skills/skills/lingshu-compiler/units/vm-closure-create/SKILL.md +45 -45
  475. package/skills/skills/lingshu-compiler/units/vm-compare/SKILL.md +45 -45
  476. package/skills/skills/lingshu-compiler/units/vm-cond-jump/SKILL.md +45 -45
  477. package/skills/skills/lingshu-compiler/units/vm-cond-space/SKILL.md +45 -45
  478. package/skills/skills/lingshu-compiler/units/vm-exception/SKILL.md +45 -45
  479. package/skills/skills/lingshu-compiler/units/vm-func-call/SKILL.md +45 -45
  480. package/skills/skills/lingshu-compiler/units/vm-loop-run/SKILL.md +45 -45
  481. package/skills/skills/lingshu-compiler/units/vm-profiling/SKILL.md +45 -45
  482. package/skills/skills/lingshu-compiler/units/vm-refcount/SKILL.md +45 -45
  483. package/skills/skills/lingshu-compiler/units/vm-run-loop/SKILL.md +45 -45
  484. package/skills/skills/lingshu-compiler/units/vm-short-circuit/SKILL.md +45 -45
  485. package/skills/skills/lingshu-compiler/units/vm-stack-guard/SKILL.md +45 -45
  486. package/skills/skills/lingshu-compiler/units/vm-stack-ops/SKILL.md +45 -45
  487. package/skills/skills/lingshu-compiler/units/vm-trust-accum/SKILL.md +45 -45
  488. package/skills/skills/lingshu-graph/SKILL.md +63 -63
  489. package/skills/skills/lingshu-net/SKILL.md +48 -48
  490. package/skills/skills/lingshu-os/SKILL.md +64 -64
  491. package/skills/skills/lingshu-pylang/SKILL.md +71 -71
  492. package/src/bridge.ts +401 -401
  493. package/src/hooks.ts +38 -2
  494. package/src/lib/datapath.ts +326 -326
  495. package/src/lib/mdcg_client.ts +413 -413
  496. package/src/lib/mutual.ts +428 -428
  497. package/src/lib/prompt_safety.ts +62 -62
  498. package/src/lib/python_path.ts +71 -71
  499. package/src/lib/roleplay_web.ts +932 -932
  500. package/src/lib/token_store.ts +192 -192
  501. package/src/tools.ts +212 -212
  502. package/zcode/AGENTS.md +11 -3
  503. package/zcode/README.md +41 -41
  504. /package/docs/{mdcg → hive}//344/270/273/344/273/243/347/220/206/345/255/220/344/273/243/347/220/206/350/256/260/345/277/206/346/236/266/346/236/204/350/256/276/350/256/241.md" +0 -0
@@ -1,633 +1,633 @@
1
- # -*- coding: utf-8 -*-
2
- """memory-bench-1000 上的 A/B 评测:四路 RRF vs 五路 RRF(+fuzzy 分级隶属度)
3
-
4
- 数据集:data/memory-bench-1000.jsonl(真实记忆库分层抽样,保留真实噪声形态)
5
- 250 signal / 400 noise / 350 unlabeled —— 噪声率 40%,且噪声带强重复模式
6
- (`[会话X·要点N]`、`[consolidation] 演练 N`),正是词法路最容易被抢占的场景。
7
-
8
- 评测问题:**fuzzy 路(分级隶属度 + 加权同义词组)是净增益还是净损失?**
9
- 不做预设结论——四条查询模式 × 两臂 × 同批指标,如实报告。
10
-
11
- 查询模式(全部确定性可复现,不需要 LLM):
12
- orig 原文前 30 字 —— 回忆原话(词法路的天花板)
13
- head 原文前 8 字 —— 只记得开头
14
- para 同义词组改写 —— 泛化/同义查询(fuzzy 的靶心)
15
- drop 随机丢弃一半语义片段 —— 模糊记忆(词法 Jaccard 下降)
16
-
17
- 指标(Top-K):
18
- self@K 查询来源节点是否被召回(MRR 同报)
19
- sigP@K Top-K 中 signal 占比
20
- noiseP@K Top-K 中 noise 占比
21
- fuzzy_prov target 命中时 fuzzy 路参与的比例(可审计)
22
-
23
- 跑法:
24
- python -m md_cg.bench_membench # 全量 250 条
25
- python -m md_cg.bench_membench --n 60 # 抽样 60 条
26
- python -m md_cg.bench_membench --rebuild # 重建库
27
- """
28
- import os
29
- import io
30
- import re
31
- import sys
32
- import json
33
- import time
34
- import random
35
- import argparse
36
- import collections
37
-
38
- sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
39
-
40
- from md_cg.mdcg import SYNONYM_GROUPS_WEIGHTED, expand_query_terms
41
- from md_cg.mdcos import MdCGOS
42
-
43
- HERE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
44
- DATA = os.path.join(HERE, "data", "memory-bench-1000.jsonl")
45
- ROOT = os.path.join(HERE, "_md_cg_membench")
46
-
47
- ARM4 = ("lexical", "bucket", "entity", "graph")
48
- ARM5 = ("lexical", "bucket", "entity", "graph", "fuzzy")
49
-
50
- SEG_SPLIT = re.compile(r"([,。;:、,;.!?!?\s]+)")
51
-
52
-
53
- # --------------------------------------------------------------------------- 数据
54
-
55
- # 生效条件:按 path(默认模块常量 DATA)以 utf-8 打开文件逐行 json.loads,仅保留 l.strip() 为真的行,返回解析出的记录列表。
56
- def load_rows(path=DATA):
57
- with open(path, encoding="utf-8") as f:
58
- return [json.loads(l) for l in f if l.strip()]
59
-
60
-
61
- # 生效条件:以 root(默认模块常量 ROOT)建 MdCGOS,rows 为假值(None/空容器)时回落 load_rows() 取数据,逐行用 r["id"] 与 r["text"] 调 add,仅当 r["id"] 已在构建开始时取的 cg.index["nodes"] 快照 have 中才 continue(该快照不随新增 id 更新),verbose 假值则不打印,最后 cg.flush() 并返回 cg。
62
- def build(root=ROOT, rows=None, verbose=True):
63
- """把数据集写进 md 认知图(幂等:同 id 覆盖)。"""
64
- rows = rows or load_rows()
65
- cg = MdCGOS(root, autoflush=200)
66
- have = set(cg.index["nodes"])
67
- n_new = 0
68
- for r in rows:
69
- if r["id"] in have:
70
- continue
71
- cg.add(r["id"], r["text"], layer="contextual",
72
- tags=r.get("tags") or [], importance=float(r.get("importance") or 0.5),
73
- verification_basis="data",
74
- bench_label=r.get("label"), bench_cat=r.get("category"),
75
- bench_observed_at=r.get("observed_at"),
76
- bench_chars=r.get("chars"))
77
- n_new += 1
78
- cg.flush()
79
- if verbose:
80
- print(f" 建库 {root}:新增 {n_new},库内 {len(cg.index['nodes'])} 节点")
81
- return cg
82
-
83
-
84
- # --------------------------------------------------------------------- 查询构造
85
-
86
- # 生效条件:遍历 SYNONYM_GROUPS_WEIGHTED 每组,按权重降序找第一个出现在当前 out(初值 text,可能已被前组改写)里的词 w,若组内除 w 外还有词则用其中权重最大者 alt 执行一次 out.replace(w, alt, 1) 且 hits+1,随后 break 该组;返回 (out, hits)。
87
- def paraphrase(text):
88
- """按加权同义词组做一次同义/泛化改写,返回 (改写后, 替换次数)。
89
-
90
- 替换目标取同组权重最高者(通常是组心)——即「说成更泛的词」。
91
- """
92
- out, hits = text, 0
93
- for group in SYNONYM_GROUPS_WEIGHTED:
94
- for w in sorted(group, key=lambda x: -group[x]):
95
- if w in out:
96
- alts = [x for x in group if x != w]
97
- if alts:
98
- alt = max(alts, key=lambda x: group[x])
99
- out = out.replace(w, alt, 1)
100
- hits += 1
101
- break
102
- return out, hits
103
-
104
-
105
- # 生效条件:text 经 SEG_SPLIT.split 后剔除空白项与分隔符本身得 parts,len(parts)<=1 时原样返回 text,否则按 rng.random() < keep_p(默认 0.5;keep_p 为 0 时全被丢弃)保留片段,若一条未留则回退 [parts[0]],返回拼接串。
106
- def drop_segments(text, rng, keep_p=0.5):
107
- """随机丢弃约一半语义片段(模拟「记得不全」)。"""
108
- parts = [p for p in SEG_SPLIT.split(text)
109
- if p.strip() and not SEG_SPLIT.fullmatch(p)]
110
- if len(parts) <= 1:
111
- return text
112
- kept = [p for p in parts if rng.random() < keep_p]
113
- if not kept:
114
- kept = [parts[0]]
115
- return "".join(kept)
116
-
117
-
118
- # 生效条件:rows 中 r.get("label")=="signal" 且 r.get("text") 为真的行入选,n 为真值且 n < len(sig) 时才用 random.Random(seed) 抽 n 条(n=0 等假值不抽样),每条产生 orig/head/drop 三条查询,仅在 paraphrase(t[:60]) 的 hits 非零时追加 para 查询,返回 qs。
119
- def make_queries(rows, n=250, seed=7):
120
- """从 signal 条目构造查询集(多模式)。"""
121
- sig = [r for r in rows if r.get("label") == "signal" and r.get("text")]
122
- rng = random.Random(seed)
123
- if n and n < len(sig):
124
- sig = rng.sample(sig, n)
125
- qs = []
126
- for r in sig:
127
- t = r["text"]
128
- qs.append({"mode": "orig", "q": t[:30], "target": r["id"]})
129
- qs.append({"mode": "head", "q": t[:8], "target": r["id"]})
130
- p, hits = paraphrase(t[:60])
131
- if hits:
132
- qs.append({"mode": "para", "q": p, "target": r["id"]})
133
- qs.append({"mode": "drop", "q": drop_segments(t[:80], rng), "target": r["id"]})
134
- return qs
135
-
136
-
137
- # ----------------------------------------------------------------------- 评测
138
-
139
- # 生效条件:传入 cg 后把 cg._read 换成按 entry["path"] 缓存的闭包(该 path 未命中才调用原 _read),返回该 cache 字典。
140
- def install_read_cache(cg):
141
- """评测是只读的:把节点文件读进内存,避免每次检索重复 1000 次磁盘 I/O。"""
142
- cache = {}
143
- orig = cg._read
144
-
145
- # 生效条件:entry["path"] 不在闭包 cache 中时调用 orig(entry) 存入该键,随后返回 cache[p](同一 path 后续命中直接取缓存)。
146
- def _cached(entry):
147
- p = entry["path"]
148
- if p not in cache:
149
- cache[p] = orig(entry)
150
- return cache[p]
151
-
152
- cg._read = _cached
153
- return cache
154
-
155
-
156
- # 生效条件:对 queries 每条 item 以 cg.search_rrf(item["q"], k=k, paths=arm, judge=False, record=False, path_weights=path_weights, recall_only=recall_only, fusion=fusion) 取结果,rank 记 item["target"] 在返回 ids 中的 1 基位次(不在 ids 中记 0),逐条追加明细后返回 out。
157
- def run_arm(cg, queries, arm, k=10, path_weights=None, recall_only=None,
158
- fusion="sum"):
159
- """跑一臂,返回 per-query 明细。"""
160
- out = []
161
- for item in queries:
162
- res, meta = cg.search_rrf(item["q"], k=k, paths=arm, judge=False,
163
- record=False, path_weights=path_weights,
164
- recall_only=recall_only, fusion=fusion)
165
- ids = [r[0]["id"] for r in res]
166
- labels = [r[0]["frontmatter"].get("bench_label") for r in res]
167
- rank = ids.index(item["target"]) + 1 if item["target"] in ids else 0
168
- prov = next((r[3] for r in res if r[0]["id"] == item["target"]), [])
169
- out.append({
170
- "mode": item["mode"], "rank": rank, "labels": labels,
171
- "fuzzy_hit": any(p.get("path") == "fuzzy" for p in (prov or [])),
172
- "expand_source": meta.get("expand_source"),
173
- "n": len(res),
174
- })
175
- return out
176
-
177
-
178
- # 生效条件:对 queries 每条以 cg.search_rrf(item["q"], k=k, paths=(path,)) 单路取结果,rank 记 item["target"] 的 1 基位次(未命中记 0),fuzzy_hit 恒为 False、expand_source 恒为 None,返回 out。
179
- def run_single(cg, queries, path, k=10):
180
- """单路独立排序(用于诊断每路的自身质量)。"""
181
- out = []
182
- for item in queries:
183
- res, meta = cg.search_rrf(item["q"], k=k, paths=(path,), judge=False,
184
- record=False)
185
- ids = [r[0]["id"] for r in res]
186
- labels = [r[0]["frontmatter"].get("bench_label") for r in res]
187
- rank = ids.index(item["target"]) + 1 if item["target"] in ids else 0
188
- out.append({"mode": item["mode"], "rank": rank, "labels": labels,
189
- "fuzzy_hit": False, "expand_source": None, "n": len(res)})
190
- return out
191
-
192
-
193
- # 生效条件:rows 为空时返回 None;否则按 n=len(rows) 算 self@1、self@k(0<rank<=k,k 由形参给定)、mrr(仅 rank 非 0 行取 1/rank 再除 n)与标签占比(分母 sum(lab.values()) or 1,无标签时用 1),fuzzy_prov 在 hit_rows 为空时记 0.0。
194
- def summarize(rows, k=10):
195
- """汇总一个查询模式的指标。"""
196
- n = len(rows)
197
- if not n:
198
- return None
199
- self_hit = sum(1 for r in rows if r["rank"] == 1)
200
- in_k = sum(1 for r in rows if 0 < r["rank"] <= k)
201
- mrr = sum(1.0 / r["rank"] for r in rows if r["rank"]) / n
202
- lab = collections.Counter()
203
- for r in rows:
204
- lab.update(r["labels"])
205
- tot = sum(lab.values()) or 1
206
- hit_rows = [r for r in rows if r["rank"]]
207
- return {
208
- "n": n, "self@1": self_hit / n, f"self@{k}": in_k / n, "mrr": mrr,
209
- "sigP": lab["signal"] / tot, "noiseP": lab["noise"] / tot,
210
- "unlabP": lab["unlabeled"] / tot,
211
- "fuzzy_prov": (sum(1 for r in hit_rows if r["fuzzy_hit"]) / len(hit_rows)
212
- if hit_rows else 0.0),
213
- }
214
-
215
-
216
- # 生效条件:把 x 乘 100 后按 5 位宽保留一位小数加百分号返回,源码未对 x 做类型或范围校验。
217
- def _pct(x):
218
- return f"{x * 100:5.1f}%"
219
-
220
-
221
- # 生效条件:r4 长度为 0 时返回 {};否则以 seed 建 rng,对 (1,"self@1") 与 (k,f"self@{k}") 由 r4/r5 的 0<rank<=kk 命中向量算 b01/b10,m=b01+b10 为 0 时 p=1.0 否则取 McNemar 精确单侧和除以 2**m,并用 n_boot 次有放回重采样取 deltas[int(0.025*n_boot)] 与 deltas[int(0.975*n_boot)-1] 为 ci,另加 MRR 键(b01/b10/p 与 ci 均为 None)后返回。
222
- def paired_test(r4, r5, k=10, seed=0, n_boot=5000):
223
- """配对显著性:bootstrap 95% CI + McNemar 精确单侧 p(H1: 五路更好)。"""
224
- from math import comb
225
- rng = random.Random(seed)
226
- n = len(r4)
227
- if not n:
228
- return {}
229
- out = {}
230
- for kk, name in ((1, "self@1"), (k, f"self@{k}")):
231
- a = [1 if 0 < r["rank"] <= kk else 0 for r in r4]
232
- b = [1 if 0 < r["rank"] <= kk else 0 for r in r5]
233
- b01 = sum(1 for x, y in zip(a, b) if x == 0 and y == 1) # 四路漏→五路中
234
- b10 = sum(1 for x, y in zip(a, b) if x == 1 and y == 0) # 四路中→五路漏
235
- m = b01 + b10
236
- p = (sum(comb(m, i) for i in range(b01, m + 1)) / (2 ** m)) if m else 1.0
237
- deltas = []
238
- for _ in range(n_boot):
239
- idx = [rng.randrange(n) for _ in range(n)]
240
- deltas.append((sum(b[i] for i in idx) - sum(a[i] for i in idx)) / n)
241
- deltas.sort()
242
- out[name] = {"delta": (sum(b) - sum(a)) / n, "b01": b01, "b10": b10, "p": p,
243
- "ci": (deltas[int(0.025 * n_boot)],
244
- deltas[int(0.975 * n_boot) - 1])}
245
- mrr4 = sum(1.0 / r["rank"] for r in r4 if r["rank"]) / n
246
- mrr5 = sum(1.0 / r["rank"] for r in r5 if r["rank"]) / n
247
- out["MRR"] = {"delta": mrr5 - mrr4, "b01": None, "b10": None, "p": None,
248
- "ci": (None, None)}
249
- return out
250
-
251
-
252
- # 生效条件:scopes 以 ("全合并", r4, r5) 起头,仅当某模式在 r4 中有条目时才追加该模式(orig/head/para/drop)的 (模式名, 筛选后 a, 筛选后 b);对每个 scope 用 k、seed 调 paired_test 并逐项打印 self@1、self@k、MRR 的 delta/ci/b01-b10/p,函数本身不返回值。
253
- def print_sig(r4, r5, qs, k=10, seed=0):
254
- """按查询模式 + 全模式合并,打印配对显著性。"""
255
- print(f"\n配对显著性(五路 − 四路,配对 bootstrap 5000 次 + McNemar 精确单侧)")
256
- print(f"{'范围':<8}{'指标':<9}{'Δ':>9}{'95% CI':>19}{'b01/b10':>10}{'p':>9}")
257
- print("-" * 66)
258
- scopes = [("全合并", r4, r5)]
259
- for m in ("orig", "head", "para", "drop"):
260
- a = [r for r in r4 if r["mode"] == m]
261
- b = [r for r in r5 if r["mode"] == m]
262
- if a:
263
- scopes.append((m, a, b))
264
- for name, a, b in scopes:
265
- res = paired_test(a, b, k=k, seed=seed)
266
- for kk in ("self@1", f"self@{k}", "MRR"):
267
- s = res[kk]
268
- ci = s["ci"]
269
- ci_s = f"[{ci[0]:+.3f},{ci[1]:+.3f}]" if ci[0] is not None else ""
270
- dis = f"{s['b01']}/{s['b10']}" if s["b01"] is not None else ""
271
- p_s = f"{s['p']:.3f}" if s["p"] is not None else ""
272
- print(f"{name:<8}{kk:<9}{s['delta']:>+9.3f}{ci_s:>19}{dis:>10}{p_s:>9}")
273
- print("-" * 66)
274
-
275
-
276
- # 生效条件:把 cg._path_fuzzy 换成闭包,原结果按分数降序取前 50 后用 random.Random(seed) 打乱并改写为递减伪分;本函数自身无 return。
277
- def patch_shuffled_fuzzy(cg, seed=0):
278
- """把 fuzzy 路**候选集保留、内部顺序打乱**(对照:区分「候选集贡献」与「排序质量贡献」)。
279
-
280
- 注意:search_rrf 会在 _path_fuzzy 返回后按分数重排,因此必须先把真实 top-50
281
- 候选集定下来,再用递减伪分固化打乱后的顺序,否则 shuffle 会被排序覆盖(空控制)。
282
- """
283
- rng = random.Random(seed)
284
- orig = cg._path_fuzzy
285
-
286
- # 生效条件:以 (query, entries, context, expand=expand) 调用原 _path_fuzzy,结果按分数降序取前 50 条,用闭包 rng 打乱后改写为递减伪分 (node, float(n-i)),返回 (out, str(src)+"+shuffled")。
287
- def _shuf(query, entries, context=None, expand=None):
288
- out, src = orig(query, entries, context, expand=expand)
289
- out = sorted(out, key=lambda x: -x[1])[:50] # fuzzy 真实候选集
290
- rng.shuffle(out) # 打乱内部顺序
291
- n = len(out)
292
- out = [(node, float(n - i)) for i, (node, _s) in enumerate(out)]
293
- return out, str(src) + "+shuffled"
294
-
295
- cg._path_fuzzy = _shuf
296
-
297
-
298
- # 生效条件:把 cg._path_fuzzy 换成忽略 context/expand、对 cg._read_many 全量条目按 random.Random(seed) 随机分降序排序的闭包;本函数自身无 return。
299
- def patch_random_path(cg, seed=0):
300
- """把第 5 路替换为**全库随机排序**(对照:max 融合机制本身能带来多少增益)。"""
301
- rng = random.Random(seed)
302
-
303
- # 生效条件:忽略 context 与 expand,对 cg._read_many(entries, stat) 的每条记录用 rng.random() 打分并降序排序,节点 id 取 fm.get("id") or e["path"],返回 (out, "random")。
304
- def _rand(query, entries, context=None, expand=None):
305
- stat = {"scanned": 0}
306
- out = []
307
- for e, fm, c in cg._read_many(entries, stat):
308
- out.append(({"id": fm.get("id") or e["path"], "frontmatter": fm,
309
- "content": c, "path": e["path"]}, rng.random()))
310
- out.sort(key=lambda x: -x[1])
311
- return out, "random"
312
-
313
- cg._path_fuzzy = _rand
314
-
315
-
316
- # 生效条件:不使用 cg,直接把 md_cg.mdcos.GLOBAL_CAP 置为 10**9 并返回该值。
317
- def patch_symmetric_pool(cg):
318
- """【2026-09-16 起已冗余】抬高 GLOBAL_CAP,让 lexical 与 fuzzy 候选池对称。
319
-
320
- 背景:lexical 命中数超 GLOBAL_CAP 时按**插入序**截断,fuzzy 则全库打分后取
321
- Top-50;两条路候选池不对称,会系统性偏袒 fuzzy。当时以「抬高 cap」对冲。
322
-
323
- 现状:截断依据已改为**相关度**(`mdcg.cut_by_relevance` 与 `mdcos._lexical`
324
- 均先全量打分再排序截断,cap 值仍为 500)——插入序偏置的根因已消除,本 patch
325
- 不再必要。保留仅为兼容既有对照命令(`--symmetric-pool`)与历史口径复现。
326
- """
327
- from md_cg import mdcos as _m
328
- _m.GLOBAL_CAP = 10 ** 9
329
- return _m.GLOBAL_CAP
330
-
331
-
332
- # 生效条件:把 cg._lexical 换成先 cg._read_many(entries, stat) 全量读入、再 cg._score(docs, query, bigrams(query)) 打分的闭包;本函数自身无 return。
333
- def patch_lexical_full(cg):
334
- """让 lexical 也对**全量候选池**打分(消除「扫描范围」差异,只留排序算法差异)。
335
-
336
- 默认 _lexical 只在 LIKE 命中集上打分(中位约 20 条),而 _path_fuzzy 扫全库 1000 条。
337
- 注:截断依据改为相关度后(2026-09-16),「排序依据不对等」已消除,此处差异
338
- 仅剩**扫描范围**一项;本 patch 仍用于隔离排序算法差异的对照实验。
339
- """
340
- from md_cg.mdcg import bigrams
341
-
342
- # 生效条件:entries 经 cg._read_many(entries, stat) 全量读入后交给 cg._score(docs, query, bigrams(query)) 打分并返回其结果。
343
- def _lex(query, entries, stat):
344
- docs = cg._read_many(entries, stat)
345
- return cg._score(docs, query, bigrams(query))
346
-
347
- cg._lexical = _lex
348
-
349
-
350
- # 生效条件:把 cg._read 包装为仅当原 _read 返回的 fm 是 dict 时覆写 fm["importance"]=0.5(非 dict 不修改),返回 (fm, c);本函数自身无 return。
351
- def patch_zero_importance(cg):
352
- """抹掉 importance:数据集里 signal 的 importance 系统性偏高(≈0.6),
353
- 而 search_rrf 用 (-score, -importance) 做同分 tie-break → 会泄漏 label。
354
- """
355
- orig = cg._read
356
-
357
- # 生效条件:orig(entry) 返回的 fm 是 dict 时把 fm["importance"] 设为 0.5(非 dict 不修改),返回 (fm, c)。
358
- def _z(entry):
359
- fm, c = orig(entry)
360
- if isinstance(fm, dict):
361
- fm["importance"] = 0.5
362
- return fm, c
363
-
364
- cg._read = _z
365
-
366
-
367
- # 生效条件:cg 与 qs 均须可用;内部先经 cg._candidates() 取候选池、cg._read_many 读正文,再按每题 query terms 统计 _like 命中数;只打印诊断(含 GLOBAL_CAP 与截断计数)并返回 None,不改库;
368
- def pool_diag(cg, qs):
369
- """候选池对称性诊断:lexical 的 LIKE 命中数是否真的被 GLOBAL_CAP 截断。"""
370
- from md_cg import mdcos as _m
371
- entries = cg._candidates()
372
- stat = {"scanned": 0}
373
- docs = cg._read_many(entries, stat)
374
- dist = []
375
- for item in qs:
376
- terms = expand_query_terms(item["q"])
377
- dist.append(sum(1 for d in docs if cg._like(d[2], d[1], terms)))
378
- dist.sort()
379
- cap = _m.GLOBAL_CAP
380
- trunc = sum(1 for x in dist if x > cap)
381
- mid = dist[len(dist) // 2] if dist else 0
382
- print(f"\n候选池对称性诊断(候选池 {len(entries)},GLOBAL_CAP={cap})")
383
- print(f" lexical LIKE 命中数:min={dist[0]} 中位={mid} max={dist[-1]}")
384
- print(f" 被 GLOBAL_CAP 截断的查询:{trunc}/{len(dist)}"
385
- f"({trunc / max(len(dist), 1):.0%})")
386
- print(" 说明:截断按插入序取前 cap 条 → lexical 的候选池小于 fuzzy(fuzzy 全库打分)。")
387
- return 0
388
-
389
-
390
- # 生效条件:cg.search_rrf 可用、queries 每项含 q/target/mode 时,对目标未列首的题打印顶替者与目标在 ARM5 各单路的排名,最多打印 limit 例。
391
- def diag_failures(cg, queries, k=10, arm=ARM5, limit=6, path_weights=None):
392
- """打印失败案例:谁把目标挤掉了、目标在单路里排第几。"""
393
- print(f"\n失败案例诊断({'+'.join(arm)},Top-{k} 未把目标排第一)")
394
- shown = 0
395
- for item in queries:
396
- res, _m = cg.search_rrf(item["q"], k=k, paths=arm, judge=False,
397
- record=False, path_weights=path_weights)
398
- ids = [r[0]["id"] for r in res]
399
- if ids and ids[0] == item["target"]:
400
- continue
401
- rank = ids.index(item["target"]) + 1 if item["target"] in ids else 0
402
- per = {}
403
- for p in ARM5:
404
- r2, _ = cg.search_rrf(item["q"], k=50, paths=(p,), judge=False,
405
- record=False)
406
- i2 = [x[0]["id"] for x in r2]
407
- per[p] = i2.index(item["target"]) + 1 if item["target"] in i2 else 0
408
- top = res[0]
409
- tgt_prov = next((r[3] for r in res if r[0]["id"] == item["target"]), None)
410
- print(f"\n [{item['mode']}] q={item['q'][:26]!r} 目标 rank={rank or '未命中'}")
411
- print(f" 被谁顶掉:{top[0]['id']} label="
412
- f"{top[0]['frontmatter'].get('bench_label')} prov={top[3]}")
413
- print(f" 目标 prov={tgt_prov}")
414
- print(f" 目标单路 rank={per}")
415
- shown += 1
416
- if shown >= limit:
417
- break
418
- if not shown:
419
- print(" (无失败案例)")
420
-
421
-
422
- # 生效条件:args.ablate=="pool" 时直接返回 pool_diag(cg, qs);=="idcheck" 时对四路 sum 与四路 max 逐条比较 rank/labels 并打印差异数后返回 0;其余取值时先算四路基线与五路 fuzzy max 基线,再按 =="shuffle" 调 patch_shuffled_fuzzy、否则调 patch_random_path 做扰动臂,打印三臂指标后返回 0(k 取 args.k)。
423
- def ablate(cg, qs, args):
424
- """对照实验:分离「候选集贡献」「排序质量贡献」「融合机制红利」。"""
425
- k = args.k
426
- if args.ablate == "pool":
427
- return pool_diag(cg, qs)
428
- if args.ablate == "idcheck":
429
- a = run_arm(cg, qs, ARM4, k=k, fusion="sum")
430
- b = run_arm(cg, qs, ARM4, k=k, fusion="max")
431
- diff = [i for i, (x, y) in enumerate(zip(a, b))
432
- if x["rank"] != y["rank"] or x["labels"] != y["labels"]]
433
- print("\n恒等性自检:四路 sum vs 四路 max")
434
- print(" (若 bucket/entity/graph 三路确实全空,两者应逐条完全相同)")
435
- print(f" 不同查询:{len(diff)}/{len(a)}")
436
- for i in diff[:6]:
437
- print(f" [{qs[i]['mode']}] sum rank={a[i]['rank']} "
438
- f"max rank={b[i]['rank']}")
439
- return 0
440
-
441
- base = summarize(run_arm(cg, qs, ARM4, k=k), k=k)
442
- real = summarize(run_arm(cg, qs, ARM5, k=k, path_weights={"fuzzy": 1.0},
443
- fusion="max"), k=k)
444
- if args.ablate == "shuffle":
445
- patch_shuffled_fuzzy(cg, seed=args.seed)
446
- label = "fuzzy 候选同、顺序打乱"
447
- else:
448
- patch_random_path(cg, seed=args.seed)
449
- label = "第5路=全库随机排序"
450
- abl = summarize(run_arm(cg, qs, ARM5, k=k, path_weights={"fuzzy": 1.0},
451
- fusion="max"), k=k)
452
- print(f"\n对照实验(均 max 融合,Top-{k},n={len(qs)} 查询)")
453
- print(f"{'臂':<26}{'self@1':>9}{'self@%d' % k:>9}{'MRR':>8}{'sigP':>8}")
454
- print("-" * 62)
455
- for name, s in (("四路基线(仅 lexical 有效)", base), ("五路 fuzzy max", real),
456
- (label, abl)):
457
- print(f"{name:<26}{_pct(s['self@1']):>9}"
458
- f"{_pct(s['self@' + str(k)]):>9}{s['mrr']:>8.3f}"
459
- f"{_pct(s['sigP']):>8}")
460
- print("-" * 62)
461
- print(" 判读:若「打乱」或「随机」也拿到大部分 +self@10 → 增益来自融合机制,"
462
- "不是 fuzzy 的排序质量。")
463
- return 0
464
-
465
-
466
- # 生效条件:argv 经 argparse 解析(--n/--k/--seed/--rebuild/--per-path/--sweep/--diag/--ablate/--sympool/--fusion/--lexfull/--zero-importance 等);外部数据集缺失即抛异常、不静默降级;返回进程退出码;
467
- def main():
468
- ap = argparse.ArgumentParser()
469
- ap.add_argument("--n", type=int, default=250, help="抽样 signal 条数(0=全量)")
470
- ap.add_argument("--k", type=int, default=10)
471
- ap.add_argument("--seed", type=int, default=7)
472
- ap.add_argument("--rebuild", action="store_true")
473
- ap.add_argument("--per-path", action="store_true", help="逐路诊断(慢)")
474
- ap.add_argument("--sweep", action="store_true", help="融合策略扫描(慢)")
475
- ap.add_argument("--diag", action="store_true", help="打印失败案例诊断")
476
- ap.add_argument("--ablate", default="none",
477
- choices=["none", "idcheck", "shuffle", "random", "pool"],
478
- help="对照实验:恒等性自检 / 打乱 fuzzy 顺序 / 随机第5路 / 候选池对称性")
479
- ap.add_argument("--sympool", action="store_true",
480
- help="抬高 GLOBAL_CAP,使 lexical 与 fuzzy 候选池对称")
481
- ap.add_argument("--fusion", default="sum", choices=["sum", "max"],
482
- help="五路融合模式(sum=经典 RRF 求和;max=取各路最高贡献)")
483
- ap.add_argument("--lexfull", action="store_true",
484
- help="lexical 也对全量池打分(与 fuzzy 的扫描范围真正对等)")
485
- ap.add_argument("--zero-importance", action="store_true",
486
- help="抹掉 importance(消除数据集里 importance 对 label 的泄漏)")
487
- args = ap.parse_args()
488
-
489
- print("=" * 76)
490
- print("memory-bench-1000 · 四路 vs 五路(+fuzzy 分级隶属度)A/B")
491
- print("=" * 76)
492
-
493
- rows = load_rows()
494
- lab = collections.Counter(r.get("label") for r in rows)
495
- print(f"\n数据集:{len(rows)} 条 signal {lab['signal']} / noise {lab['noise']} "
496
- f"/ unlabeled {lab['unlabeled']} → 噪声率 {lab['noise'] / len(rows):.0%}")
497
-
498
- if args.rebuild and os.path.isdir(ROOT):
499
- import shutil
500
- shutil.rmtree(ROOT)
501
- t0 = time.perf_counter()
502
- cg = build(ROOT, rows)
503
- install_read_cache(cg)
504
- print(f" 建库耗时 {(time.perf_counter() - t0) * 1000:.0f} ms"
505
- f"({len(cg.index['nodes'])} 节点,{len(cg.index['buckets'])} 桶)")
506
-
507
- if args.sympool:
508
- cap = patch_symmetric_pool(cg)
509
- print(f" [对照] 候选池对称化:GLOBAL_CAP → {cap}(lexical 不再按插入序截断)")
510
- if args.zero_importance:
511
- patch_zero_importance(cg)
512
- print(" [对照] importance 已抹平为 0.5(消除 signal 的 label 泄漏)")
513
- if args.lexfull:
514
- patch_lexical_full(cg)
515
- print(" [对照] lexical 全量池打分(与 fuzzy 扫描范围对等)")
516
-
517
- qs = make_queries(rows, n=args.n, seed=args.seed)
518
- by_mode = collections.Counter(q["mode"] for q in qs)
519
- print(f"\n查询集:{len(qs)} 条 {dict(by_mode)}"
520
- f"(seed={args.seed},signal 抽样 {args.n})")
521
-
522
- if args.ablate != "none":
523
- return ablate(cg, qs, args)
524
-
525
- t0 = time.perf_counter()
526
- r4 = run_arm(cg, qs, ARM4, k=args.k, fusion="sum")
527
- t4 = time.perf_counter() - t0
528
- t0 = time.perf_counter()
529
- r5 = run_arm(cg, qs, ARM5, k=args.k, path_weights={"fuzzy": 1.0},
530
- fusion=args.fusion)
531
- t5 = time.perf_counter() - t0
532
- print(f"耗时:四路 {t4:.1f}s / 五路 {t5:.1f}s(五路融合={args.fusion})")
533
-
534
- modes = [m for m in ("orig", "head", "para", "drop") if by_mode.get(m)]
535
- print(f"\n{'模式':<6}{'臂':<5}{'n':>5}{'self@1':>9}{'self@%d' % args.k:>9}"
536
- f"{'MRR':>8}{'sigP':>8}{'noiseP':>9}{'fuzzy参与':>10}")
537
- print("-" * 76)
538
- delta = {}
539
- for m in modes:
540
- s4 = summarize([r for r in r4 if r["mode"] == m], k=args.k)
541
- s5 = summarize([r for r in r5 if r["mode"] == m], k=args.k)
542
- for arm, s in (("四路", s4), ("五路", s5)):
543
- print(f"{m:<6}{arm:<5}{s['n']:>5}{_pct(s['self@1']):>9}"
544
- f"{_pct(s['self@' + str(args.k)]):>9}{s['mrr']:>8.3f}"
545
- f"{_pct(s['sigP']):>8}{_pct(s['noiseP']):>9}"
546
- f"{_pct(s['fuzzy_prov']):>10}")
547
- delta[m] = {k: s5[k] - s4[k] for k in
548
- ("self@1", "self@" + str(args.k), "mrr", "sigP", "noiseP")}
549
- print("-" * 76)
550
-
551
- print(f"\n{'净变化(五路 − 四路)':<20}{'self@1':>10}{'self@%d' % args.k:>10}"
552
- f"{'MRR':>10}{'sigP':>10}{'noiseP':>10}")
553
- for m in modes:
554
- d = delta[m]
555
- print(f"{m:<20}{d['self@1']:>+10.1%}{d['self@' + str(args.k)]:>+10.1%}"
556
- f"{d['mrr']:>+10.3f}{d['sigP']:>+10.1%}{d['noiseP']:>+10.1%}")
557
-
558
- # 全模式合并(诚实口径:四模式混合,权重按出现次数)
559
- s4a = summarize(r4, k=args.k)
560
- s5a = summarize(r5, k=args.k)
561
- print(f"\n{'全模式合并':<20}{'self@1':>10}{'self@%d' % args.k:>10}"
562
- f"{'MRR':>10}{'sigP':>10}{'noiseP':>10}")
563
- print(f"{'四路':<20}{_pct(s4a['self@1']):>10}"
564
- f"{_pct(s4a['self@' + str(args.k)]):>10}{s4a['mrr']:>10.3f}"
565
- f"{_pct(s4a['sigP']):>10}{_pct(s4a['noiseP']):>10}")
566
- print(f"{'五路':<20}{_pct(s5a['self@1']):>10}"
567
- f"{_pct(s5a['self@' + str(args.k)]):>10}{s5a['mrr']:>10.3f}"
568
- f"{_pct(s5a['sigP']):>10}{_pct(s5a['noiseP']):>10}")
569
- print(f"{'净变化':<20}{s5a['self@1'] - s4a['self@1']:>+10.1%}"
570
- f"{s5a['self@' + str(args.k)] - s4a['self@' + str(args.k)]:>+10.1%}"
571
- f"{s5a['mrr'] - s4a['mrr']:>+10.3f}"
572
- f"{s5a['sigP'] - s4a['sigP']:>+10.1%}"
573
- f"{s5a['noiseP'] - s4a['noiseP']:>+10.1%}")
574
-
575
- print_sig(r4, r5, qs, k=args.k, seed=args.seed)
576
-
577
- if args.sweep:
578
- print(f"\n融合策略扫描(全模式合并,Top-{args.k};相对四路基线的净变化)")
579
- print(f"{'策略':<18}{'self@1':>9}{'Δself@1':>10}{'self@%d' % args.k:>9}"
580
- f"{'MRR':>8}{'sigP':>8}{'noiseP':>9}")
581
- print("-" * 76)
582
- base = summarize(r4, k=args.k)
583
- arms = [("四路 sum(基线)", ARM4, None, None, "sum"),
584
- ("四路 max", ARM4, None, None, "max"),
585
- ("五路 sum w=1.0", ARM5, {"fuzzy": 1.0}, None, "sum"),
586
- ("五路 sum w=0.3", ARM5, {"fuzzy": 0.3}, None, "sum"),
587
- ("五路 sum w=0.1", ARM5, {"fuzzy": 0.1}, None, "sum"),
588
- ("五路 max w=1.0", ARM5, {"fuzzy": 1.0}, None, "max"),
589
- ("五路 max w=0.5", ARM5, {"fuzzy": 0.5}, None, "max"),
590
- ("fuzzy 仅召回", ARM5, None, {"fuzzy"}, "sum")]
591
- if args.n and args.n > 100: # 全量时只保留关键臂,控制耗时
592
- keep = {"四路 sum(基线)", "五路 sum w=1.0", "五路 max w=1.0",
593
- "fuzzy 仅召回"}
594
- arms = [a for a in arms if a[0] in keep]
595
- for label, arm, pw, ro, fu in arms:
596
- if label.startswith("四路 sum"):
597
- s = base
598
- else:
599
- s = summarize(run_arm(cg, qs, arm, k=args.k, path_weights=pw,
600
- recall_only=ro, fusion=fu), k=args.k)
601
- print(f"{label:<18}{_pct(s['self@1']):>9}"
602
- f"{s['self@1'] - base['self@1']:>+10.1%}"
603
- f"{_pct(s['self@' + str(args.k)]):>9}{s['mrr']:>8.3f}"
604
- f"{_pct(s['sigP']):>8}{_pct(s['noiseP']):>9}")
605
- print("-" * 76)
606
-
607
- if args.per_path:
608
- print(f"\n逐路单独质量(各路径独立排序,Top-{args.k})")
609
- print(f"{'模式':<6}{'路':<9}{'n':>5}{'self@1':>9}{'self@%d' % args.k:>9}"
610
- f"{'MRR':>8}{'sigP':>8}{'noiseP':>9}")
611
- print("-" * 76)
612
- for m in modes:
613
- sub = [q for q in qs if q["mode"] == m]
614
- for p in ARM5:
615
- s = summarize(run_single(cg, sub, p, k=args.k), k=args.k)
616
- print(f"{m:<6}{p:<9}{s['n']:>5}{_pct(s['self@1']):>9}"
617
- f"{_pct(s['self@' + str(args.k)]):>9}{s['mrr']:>8.3f}"
618
- f"{_pct(s['sigP']):>8}{_pct(s['noiseP']):>9}")
619
- print("-" * 76)
620
-
621
- if args.diag:
622
- diag_failures(cg, [q for q in qs if q["mode"] == "drop"], k=args.k)
623
-
624
- src = collections.Counter(r["expand_source"] for r in r5)
625
- print(f"\nfuzzy 路扩展来源:{dict(src)}")
626
- print("\n诚实边界:本数据集无显式 edges → graph 路候选为空;节点无 domain: 标签 → "
627
- "fuzzy 的大域亲和项恒等(实际区分力全部来自加权覆盖率)。")
628
- return 0
629
-
630
-
631
- if __name__ == "__main__":
632
- sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
1
+ # -*- coding: utf-8 -*-
2
+ """memory-bench-1000 上的 A/B 评测:四路 RRF vs 五路 RRF(+fuzzy 分级隶属度)
3
+
4
+ 数据集:data/memory-bench-1000.jsonl(真实记忆库分层抽样,保留真实噪声形态)
5
+ 250 signal / 400 noise / 350 unlabeled —— 噪声率 40%,且噪声带强重复模式
6
+ (`[会话X·要点N]`、`[consolidation] 演练 N`),正是词法路最容易被抢占的场景。
7
+
8
+ 评测问题:**fuzzy 路(分级隶属度 + 加权同义词组)是净增益还是净损失?**
9
+ 不做预设结论——四条查询模式 × 两臂 × 同批指标,如实报告。
10
+
11
+ 查询模式(全部确定性可复现,不需要 LLM):
12
+ orig 原文前 30 字 —— 回忆原话(词法路的天花板)
13
+ head 原文前 8 字 —— 只记得开头
14
+ para 同义词组改写 —— 泛化/同义查询(fuzzy 的靶心)
15
+ drop 随机丢弃一半语义片段 —— 模糊记忆(词法 Jaccard 下降)
16
+
17
+ 指标(Top-K):
18
+ self@K 查询来源节点是否被召回(MRR 同报)
19
+ sigP@K Top-K 中 signal 占比
20
+ noiseP@K Top-K 中 noise 占比
21
+ fuzzy_prov target 命中时 fuzzy 路参与的比例(可审计)
22
+
23
+ 跑法:
24
+ python -m md_cg.bench_membench # 全量 250 条
25
+ python -m md_cg.bench_membench --n 60 # 抽样 60 条
26
+ python -m md_cg.bench_membench --rebuild # 重建库
27
+ """
28
+ import os
29
+ import io
30
+ import re
31
+ import sys
32
+ import json
33
+ import time
34
+ import random
35
+ import argparse
36
+ import collections
37
+
38
+ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
39
+
40
+ from md_cg.mdcg import SYNONYM_GROUPS_WEIGHTED, expand_query_terms
41
+ from md_cg.mdcos import MdCGOS
42
+
43
+ HERE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
44
+ DATA = os.path.join(HERE, "data", "memory-bench-1000.jsonl")
45
+ ROOT = os.path.join(HERE, "_md_cg_membench")
46
+
47
+ ARM4 = ("lexical", "bucket", "entity", "graph")
48
+ ARM5 = ("lexical", "bucket", "entity", "graph", "fuzzy")
49
+
50
+ SEG_SPLIT = re.compile(r"([,。;:、,;.!?!?\s]+)")
51
+
52
+
53
+ # --------------------------------------------------------------------------- 数据
54
+
55
+ # 生效条件:按 path(默认模块常量 DATA)以 utf-8 打开文件逐行 json.loads,仅保留 l.strip() 为真的行,返回解析出的记录列表。
56
+ def load_rows(path=DATA):
57
+ with open(path, encoding="utf-8") as f:
58
+ return [json.loads(l) for l in f if l.strip()]
59
+
60
+
61
+ # 生效条件:以 root(默认模块常量 ROOT)建 MdCGOS,rows 为假值(None/空容器)时回落 load_rows() 取数据,逐行用 r["id"] 与 r["text"] 调 add,仅当 r["id"] 已在构建开始时取的 cg.index["nodes"] 快照 have 中才 continue(该快照不随新增 id 更新),verbose 假值则不打印,最后 cg.flush() 并返回 cg。
62
+ def build(root=ROOT, rows=None, verbose=True):
63
+ """把数据集写进 md 认知图(幂等:同 id 覆盖)。"""
64
+ rows = rows or load_rows()
65
+ cg = MdCGOS(root, autoflush=200)
66
+ have = set(cg.index["nodes"])
67
+ n_new = 0
68
+ for r in rows:
69
+ if r["id"] in have:
70
+ continue
71
+ cg.add(r["id"], r["text"], layer="contextual",
72
+ tags=r.get("tags") or [], importance=float(r.get("importance") or 0.5),
73
+ verification_basis="data",
74
+ bench_label=r.get("label"), bench_cat=r.get("category"),
75
+ bench_observed_at=r.get("observed_at"),
76
+ bench_chars=r.get("chars"))
77
+ n_new += 1
78
+ cg.flush()
79
+ if verbose:
80
+ print(f" 建库 {root}:新增 {n_new},库内 {len(cg.index['nodes'])} 节点")
81
+ return cg
82
+
83
+
84
+ # --------------------------------------------------------------------- 查询构造
85
+
86
+ # 生效条件:遍历 SYNONYM_GROUPS_WEIGHTED 每组,按权重降序找第一个出现在当前 out(初值 text,可能已被前组改写)里的词 w,若组内除 w 外还有词则用其中权重最大者 alt 执行一次 out.replace(w, alt, 1) 且 hits+1,随后 break 该组;返回 (out, hits)。
87
+ def paraphrase(text):
88
+ """按加权同义词组做一次同义/泛化改写,返回 (改写后, 替换次数)。
89
+
90
+ 替换目标取同组权重最高者(通常是组心)——即「说成更泛的词」。
91
+ """
92
+ out, hits = text, 0
93
+ for group in SYNONYM_GROUPS_WEIGHTED:
94
+ for w in sorted(group, key=lambda x: -group[x]):
95
+ if w in out:
96
+ alts = [x for x in group if x != w]
97
+ if alts:
98
+ alt = max(alts, key=lambda x: group[x])
99
+ out = out.replace(w, alt, 1)
100
+ hits += 1
101
+ break
102
+ return out, hits
103
+
104
+
105
+ # 生效条件:text 经 SEG_SPLIT.split 后剔除空白项与分隔符本身得 parts,len(parts)<=1 时原样返回 text,否则按 rng.random() < keep_p(默认 0.5;keep_p 为 0 时全被丢弃)保留片段,若一条未留则回退 [parts[0]],返回拼接串。
106
+ def drop_segments(text, rng, keep_p=0.5):
107
+ """随机丢弃约一半语义片段(模拟「记得不全」)。"""
108
+ parts = [p for p in SEG_SPLIT.split(text)
109
+ if p.strip() and not SEG_SPLIT.fullmatch(p)]
110
+ if len(parts) <= 1:
111
+ return text
112
+ kept = [p for p in parts if rng.random() < keep_p]
113
+ if not kept:
114
+ kept = [parts[0]]
115
+ return "".join(kept)
116
+
117
+
118
+ # 生效条件:rows 中 r.get("label")=="signal" 且 r.get("text") 为真的行入选,n 为真值且 n < len(sig) 时才用 random.Random(seed) 抽 n 条(n=0 等假值不抽样),每条产生 orig/head/drop 三条查询,仅在 paraphrase(t[:60]) 的 hits 非零时追加 para 查询,返回 qs。
119
+ def make_queries(rows, n=250, seed=7):
120
+ """从 signal 条目构造查询集(多模式)。"""
121
+ sig = [r for r in rows if r.get("label") == "signal" and r.get("text")]
122
+ rng = random.Random(seed)
123
+ if n and n < len(sig):
124
+ sig = rng.sample(sig, n)
125
+ qs = []
126
+ for r in sig:
127
+ t = r["text"]
128
+ qs.append({"mode": "orig", "q": t[:30], "target": r["id"]})
129
+ qs.append({"mode": "head", "q": t[:8], "target": r["id"]})
130
+ p, hits = paraphrase(t[:60])
131
+ if hits:
132
+ qs.append({"mode": "para", "q": p, "target": r["id"]})
133
+ qs.append({"mode": "drop", "q": drop_segments(t[:80], rng), "target": r["id"]})
134
+ return qs
135
+
136
+
137
+ # ----------------------------------------------------------------------- 评测
138
+
139
+ # 生效条件:传入 cg 后把 cg._read 换成按 entry["path"] 缓存的闭包(该 path 未命中才调用原 _read),返回该 cache 字典。
140
+ def install_read_cache(cg):
141
+ """评测是只读的:把节点文件读进内存,避免每次检索重复 1000 次磁盘 I/O。"""
142
+ cache = {}
143
+ orig = cg._read
144
+
145
+ # 生效条件:entry["path"] 不在闭包 cache 中时调用 orig(entry) 存入该键,随后返回 cache[p](同一 path 后续命中直接取缓存)。
146
+ def _cached(entry):
147
+ p = entry["path"]
148
+ if p not in cache:
149
+ cache[p] = orig(entry)
150
+ return cache[p]
151
+
152
+ cg._read = _cached
153
+ return cache
154
+
155
+
156
+ # 生效条件:对 queries 每条 item 以 cg.search_rrf(item["q"], k=k, paths=arm, judge=False, record=False, path_weights=path_weights, recall_only=recall_only, fusion=fusion) 取结果,rank 记 item["target"] 在返回 ids 中的 1 基位次(不在 ids 中记 0),逐条追加明细后返回 out。
157
+ def run_arm(cg, queries, arm, k=10, path_weights=None, recall_only=None,
158
+ fusion="sum"):
159
+ """跑一臂,返回 per-query 明细。"""
160
+ out = []
161
+ for item in queries:
162
+ res, meta = cg.search_rrf(item["q"], k=k, paths=arm, judge=False,
163
+ record=False, path_weights=path_weights,
164
+ recall_only=recall_only, fusion=fusion)
165
+ ids = [r[0]["id"] for r in res]
166
+ labels = [r[0]["frontmatter"].get("bench_label") for r in res]
167
+ rank = ids.index(item["target"]) + 1 if item["target"] in ids else 0
168
+ prov = next((r[3] for r in res if r[0]["id"] == item["target"]), [])
169
+ out.append({
170
+ "mode": item["mode"], "rank": rank, "labels": labels,
171
+ "fuzzy_hit": any(p.get("path") == "fuzzy" for p in (prov or [])),
172
+ "expand_source": meta.get("expand_source"),
173
+ "n": len(res),
174
+ })
175
+ return out
176
+
177
+
178
+ # 生效条件:对 queries 每条以 cg.search_rrf(item["q"], k=k, paths=(path,)) 单路取结果,rank 记 item["target"] 的 1 基位次(未命中记 0),fuzzy_hit 恒为 False、expand_source 恒为 None,返回 out。
179
+ def run_single(cg, queries, path, k=10):
180
+ """单路独立排序(用于诊断每路的自身质量)。"""
181
+ out = []
182
+ for item in queries:
183
+ res, meta = cg.search_rrf(item["q"], k=k, paths=(path,), judge=False,
184
+ record=False)
185
+ ids = [r[0]["id"] for r in res]
186
+ labels = [r[0]["frontmatter"].get("bench_label") for r in res]
187
+ rank = ids.index(item["target"]) + 1 if item["target"] in ids else 0
188
+ out.append({"mode": item["mode"], "rank": rank, "labels": labels,
189
+ "fuzzy_hit": False, "expand_source": None, "n": len(res)})
190
+ return out
191
+
192
+
193
+ # 生效条件:rows 为空时返回 None;否则按 n=len(rows) 算 self@1、self@k(0<rank<=k,k 由形参给定)、mrr(仅 rank 非 0 行取 1/rank 再除 n)与标签占比(分母 sum(lab.values()) or 1,无标签时用 1),fuzzy_prov 在 hit_rows 为空时记 0.0。
194
+ def summarize(rows, k=10):
195
+ """汇总一个查询模式的指标。"""
196
+ n = len(rows)
197
+ if not n:
198
+ return None
199
+ self_hit = sum(1 for r in rows if r["rank"] == 1)
200
+ in_k = sum(1 for r in rows if 0 < r["rank"] <= k)
201
+ mrr = sum(1.0 / r["rank"] for r in rows if r["rank"]) / n
202
+ lab = collections.Counter()
203
+ for r in rows:
204
+ lab.update(r["labels"])
205
+ tot = sum(lab.values()) or 1
206
+ hit_rows = [r for r in rows if r["rank"]]
207
+ return {
208
+ "n": n, "self@1": self_hit / n, f"self@{k}": in_k / n, "mrr": mrr,
209
+ "sigP": lab["signal"] / tot, "noiseP": lab["noise"] / tot,
210
+ "unlabP": lab["unlabeled"] / tot,
211
+ "fuzzy_prov": (sum(1 for r in hit_rows if r["fuzzy_hit"]) / len(hit_rows)
212
+ if hit_rows else 0.0),
213
+ }
214
+
215
+
216
+ # 生效条件:把 x 乘 100 后按 5 位宽保留一位小数加百分号返回,源码未对 x 做类型或范围校验。
217
+ def _pct(x):
218
+ return f"{x * 100:5.1f}%"
219
+
220
+
221
+ # 生效条件:r4 长度为 0 时返回 {};否则以 seed 建 rng,对 (1,"self@1") 与 (k,f"self@{k}") 由 r4/r5 的 0<rank<=kk 命中向量算 b01/b10,m=b01+b10 为 0 时 p=1.0 否则取 McNemar 精确单侧和除以 2**m,并用 n_boot 次有放回重采样取 deltas[int(0.025*n_boot)] 与 deltas[int(0.975*n_boot)-1] 为 ci,另加 MRR 键(b01/b10/p 与 ci 均为 None)后返回。
222
+ def paired_test(r4, r5, k=10, seed=0, n_boot=5000):
223
+ """配对显著性:bootstrap 95% CI + McNemar 精确单侧 p(H1: 五路更好)。"""
224
+ from math import comb
225
+ rng = random.Random(seed)
226
+ n = len(r4)
227
+ if not n:
228
+ return {}
229
+ out = {}
230
+ for kk, name in ((1, "self@1"), (k, f"self@{k}")):
231
+ a = [1 if 0 < r["rank"] <= kk else 0 for r in r4]
232
+ b = [1 if 0 < r["rank"] <= kk else 0 for r in r5]
233
+ b01 = sum(1 for x, y in zip(a, b) if x == 0 and y == 1) # 四路漏→五路中
234
+ b10 = sum(1 for x, y in zip(a, b) if x == 1 and y == 0) # 四路中→五路漏
235
+ m = b01 + b10
236
+ p = (sum(comb(m, i) for i in range(b01, m + 1)) / (2 ** m)) if m else 1.0
237
+ deltas = []
238
+ for _ in range(n_boot):
239
+ idx = [rng.randrange(n) for _ in range(n)]
240
+ deltas.append((sum(b[i] for i in idx) - sum(a[i] for i in idx)) / n)
241
+ deltas.sort()
242
+ out[name] = {"delta": (sum(b) - sum(a)) / n, "b01": b01, "b10": b10, "p": p,
243
+ "ci": (deltas[int(0.025 * n_boot)],
244
+ deltas[int(0.975 * n_boot) - 1])}
245
+ mrr4 = sum(1.0 / r["rank"] for r in r4 if r["rank"]) / n
246
+ mrr5 = sum(1.0 / r["rank"] for r in r5 if r["rank"]) / n
247
+ out["MRR"] = {"delta": mrr5 - mrr4, "b01": None, "b10": None, "p": None,
248
+ "ci": (None, None)}
249
+ return out
250
+
251
+
252
+ # 生效条件:scopes 以 ("全合并", r4, r5) 起头,仅当某模式在 r4 中有条目时才追加该模式(orig/head/para/drop)的 (模式名, 筛选后 a, 筛选后 b);对每个 scope 用 k、seed 调 paired_test 并逐项打印 self@1、self@k、MRR 的 delta/ci/b01-b10/p,函数本身不返回值。
253
+ def print_sig(r4, r5, qs, k=10, seed=0):
254
+ """按查询模式 + 全模式合并,打印配对显著性。"""
255
+ print(f"\n配对显著性(五路 − 四路,配对 bootstrap 5000 次 + McNemar 精确单侧)")
256
+ print(f"{'范围':<8}{'指标':<9}{'Δ':>9}{'95% CI':>19}{'b01/b10':>10}{'p':>9}")
257
+ print("-" * 66)
258
+ scopes = [("全合并", r4, r5)]
259
+ for m in ("orig", "head", "para", "drop"):
260
+ a = [r for r in r4 if r["mode"] == m]
261
+ b = [r for r in r5 if r["mode"] == m]
262
+ if a:
263
+ scopes.append((m, a, b))
264
+ for name, a, b in scopes:
265
+ res = paired_test(a, b, k=k, seed=seed)
266
+ for kk in ("self@1", f"self@{k}", "MRR"):
267
+ s = res[kk]
268
+ ci = s["ci"]
269
+ ci_s = f"[{ci[0]:+.3f},{ci[1]:+.3f}]" if ci[0] is not None else ""
270
+ dis = f"{s['b01']}/{s['b10']}" if s["b01"] is not None else ""
271
+ p_s = f"{s['p']:.3f}" if s["p"] is not None else ""
272
+ print(f"{name:<8}{kk:<9}{s['delta']:>+9.3f}{ci_s:>19}{dis:>10}{p_s:>9}")
273
+ print("-" * 66)
274
+
275
+
276
+ # 生效条件:把 cg._path_fuzzy 换成闭包,原结果按分数降序取前 50 后用 random.Random(seed) 打乱并改写为递减伪分;本函数自身无 return。
277
+ def patch_shuffled_fuzzy(cg, seed=0):
278
+ """把 fuzzy 路**候选集保留、内部顺序打乱**(对照:区分「候选集贡献」与「排序质量贡献」)。
279
+
280
+ 注意:search_rrf 会在 _path_fuzzy 返回后按分数重排,因此必须先把真实 top-50
281
+ 候选集定下来,再用递减伪分固化打乱后的顺序,否则 shuffle 会被排序覆盖(空控制)。
282
+ """
283
+ rng = random.Random(seed)
284
+ orig = cg._path_fuzzy
285
+
286
+ # 生效条件:以 (query, entries, context, expand=expand) 调用原 _path_fuzzy,结果按分数降序取前 50 条,用闭包 rng 打乱后改写为递减伪分 (node, float(n-i)),返回 (out, str(src)+"+shuffled")。
287
+ def _shuf(query, entries, context=None, expand=None):
288
+ out, src = orig(query, entries, context, expand=expand)
289
+ out = sorted(out, key=lambda x: -x[1])[:50] # fuzzy 真实候选集
290
+ rng.shuffle(out) # 打乱内部顺序
291
+ n = len(out)
292
+ out = [(node, float(n - i)) for i, (node, _s) in enumerate(out)]
293
+ return out, str(src) + "+shuffled"
294
+
295
+ cg._path_fuzzy = _shuf
296
+
297
+
298
+ # 生效条件:把 cg._path_fuzzy 换成忽略 context/expand、对 cg._read_many 全量条目按 random.Random(seed) 随机分降序排序的闭包;本函数自身无 return。
299
+ def patch_random_path(cg, seed=0):
300
+ """把第 5 路替换为**全库随机排序**(对照:max 融合机制本身能带来多少增益)。"""
301
+ rng = random.Random(seed)
302
+
303
+ # 生效条件:忽略 context 与 expand,对 cg._read_many(entries, stat) 的每条记录用 rng.random() 打分并降序排序,节点 id 取 fm.get("id") or e["path"],返回 (out, "random")。
304
+ def _rand(query, entries, context=None, expand=None):
305
+ stat = {"scanned": 0}
306
+ out = []
307
+ for e, fm, c in cg._read_many(entries, stat):
308
+ out.append(({"id": fm.get("id") or e["path"], "frontmatter": fm,
309
+ "content": c, "path": e["path"]}, rng.random()))
310
+ out.sort(key=lambda x: -x[1])
311
+ return out, "random"
312
+
313
+ cg._path_fuzzy = _rand
314
+
315
+
316
+ # 生效条件:不使用 cg,直接把 md_cg.mdcos.GLOBAL_CAP 置为 10**9 并返回该值。
317
+ def patch_symmetric_pool(cg):
318
+ """【2026-09-16 起已冗余】抬高 GLOBAL_CAP,让 lexical 与 fuzzy 候选池对称。
319
+
320
+ 背景:lexical 命中数超 GLOBAL_CAP 时按**插入序**截断,fuzzy 则全库打分后取
321
+ Top-50;两条路候选池不对称,会系统性偏袒 fuzzy。当时以「抬高 cap」对冲。
322
+
323
+ 现状:截断依据已改为**相关度**(`mdcg.cut_by_relevance` 与 `mdcos._lexical`
324
+ 均先全量打分再排序截断,cap 值仍为 500)——插入序偏置的根因已消除,本 patch
325
+ 不再必要。保留仅为兼容既有对照命令(`--symmetric-pool`)与历史口径复现。
326
+ """
327
+ from md_cg import mdcos as _m
328
+ _m.GLOBAL_CAP = 10 ** 9
329
+ return _m.GLOBAL_CAP
330
+
331
+
332
+ # 生效条件:把 cg._lexical 换成先 cg._read_many(entries, stat) 全量读入、再 cg._score(docs, query, bigrams(query)) 打分的闭包;本函数自身无 return。
333
+ def patch_lexical_full(cg):
334
+ """让 lexical 也对**全量候选池**打分(消除「扫描范围」差异,只留排序算法差异)。
335
+
336
+ 默认 _lexical 只在 LIKE 命中集上打分(中位约 20 条),而 _path_fuzzy 扫全库 1000 条。
337
+ 注:截断依据改为相关度后(2026-09-16),「排序依据不对等」已消除,此处差异
338
+ 仅剩**扫描范围**一项;本 patch 仍用于隔离排序算法差异的对照实验。
339
+ """
340
+ from md_cg.mdcg import bigrams
341
+
342
+ # 生效条件:entries 经 cg._read_many(entries, stat) 全量读入后交给 cg._score(docs, query, bigrams(query)) 打分并返回其结果。
343
+ def _lex(query, entries, stat):
344
+ docs = cg._read_many(entries, stat)
345
+ return cg._score(docs, query, bigrams(query))
346
+
347
+ cg._lexical = _lex
348
+
349
+
350
+ # 生效条件:把 cg._read 包装为仅当原 _read 返回的 fm 是 dict 时覆写 fm["importance"]=0.5(非 dict 不修改),返回 (fm, c);本函数自身无 return。
351
+ def patch_zero_importance(cg):
352
+ """抹掉 importance:数据集里 signal 的 importance 系统性偏高(≈0.6),
353
+ 而 search_rrf 用 (-score, -importance) 做同分 tie-break → 会泄漏 label。
354
+ """
355
+ orig = cg._read
356
+
357
+ # 生效条件:orig(entry) 返回的 fm 是 dict 时把 fm["importance"] 设为 0.5(非 dict 不修改),返回 (fm, c)。
358
+ def _z(entry):
359
+ fm, c = orig(entry)
360
+ if isinstance(fm, dict):
361
+ fm["importance"] = 0.5
362
+ return fm, c
363
+
364
+ cg._read = _z
365
+
366
+
367
+ # 生效条件:cg 与 qs 均须可用;内部先经 cg._candidates() 取候选池、cg._read_many 读正文,再按每题 query terms 统计 _like 命中数;只打印诊断(含 GLOBAL_CAP 与截断计数)并返回 None,不改库;
368
+ def pool_diag(cg, qs):
369
+ """候选池对称性诊断:lexical 的 LIKE 命中数是否真的被 GLOBAL_CAP 截断。"""
370
+ from md_cg import mdcos as _m
371
+ entries = cg._candidates()
372
+ stat = {"scanned": 0}
373
+ docs = cg._read_many(entries, stat)
374
+ dist = []
375
+ for item in qs:
376
+ terms = expand_query_terms(item["q"])
377
+ dist.append(sum(1 for d in docs if cg._like(d[2], d[1], terms)))
378
+ dist.sort()
379
+ cap = _m.GLOBAL_CAP
380
+ trunc = sum(1 for x in dist if x > cap)
381
+ mid = dist[len(dist) // 2] if dist else 0
382
+ print(f"\n候选池对称性诊断(候选池 {len(entries)},GLOBAL_CAP={cap})")
383
+ print(f" lexical LIKE 命中数:min={dist[0]} 中位={mid} max={dist[-1]}")
384
+ print(f" 被 GLOBAL_CAP 截断的查询:{trunc}/{len(dist)}"
385
+ f"({trunc / max(len(dist), 1):.0%})")
386
+ print(" 说明:截断按插入序取前 cap 条 → lexical 的候选池小于 fuzzy(fuzzy 全库打分)。")
387
+ return 0
388
+
389
+
390
+ # 生效条件:cg.search_rrf 可用、queries 每项含 q/target/mode 时,对目标未列首的题打印顶替者与目标在 ARM5 各单路的排名,最多打印 limit 例。
391
+ def diag_failures(cg, queries, k=10, arm=ARM5, limit=6, path_weights=None):
392
+ """打印失败案例:谁把目标挤掉了、目标在单路里排第几。"""
393
+ print(f"\n失败案例诊断({'+'.join(arm)},Top-{k} 未把目标排第一)")
394
+ shown = 0
395
+ for item in queries:
396
+ res, _m = cg.search_rrf(item["q"], k=k, paths=arm, judge=False,
397
+ record=False, path_weights=path_weights)
398
+ ids = [r[0]["id"] for r in res]
399
+ if ids and ids[0] == item["target"]:
400
+ continue
401
+ rank = ids.index(item["target"]) + 1 if item["target"] in ids else 0
402
+ per = {}
403
+ for p in ARM5:
404
+ r2, _ = cg.search_rrf(item["q"], k=50, paths=(p,), judge=False,
405
+ record=False)
406
+ i2 = [x[0]["id"] for x in r2]
407
+ per[p] = i2.index(item["target"]) + 1 if item["target"] in i2 else 0
408
+ top = res[0]
409
+ tgt_prov = next((r[3] for r in res if r[0]["id"] == item["target"]), None)
410
+ print(f"\n [{item['mode']}] q={item['q'][:26]!r} 目标 rank={rank or '未命中'}")
411
+ print(f" 被谁顶掉:{top[0]['id']} label="
412
+ f"{top[0]['frontmatter'].get('bench_label')} prov={top[3]}")
413
+ print(f" 目标 prov={tgt_prov}")
414
+ print(f" 目标单路 rank={per}")
415
+ shown += 1
416
+ if shown >= limit:
417
+ break
418
+ if not shown:
419
+ print(" (无失败案例)")
420
+
421
+
422
+ # 生效条件:args.ablate=="pool" 时直接返回 pool_diag(cg, qs);=="idcheck" 时对四路 sum 与四路 max 逐条比较 rank/labels 并打印差异数后返回 0;其余取值时先算四路基线与五路 fuzzy max 基线,再按 =="shuffle" 调 patch_shuffled_fuzzy、否则调 patch_random_path 做扰动臂,打印三臂指标后返回 0(k 取 args.k)。
423
+ def ablate(cg, qs, args):
424
+ """对照实验:分离「候选集贡献」「排序质量贡献」「融合机制红利」。"""
425
+ k = args.k
426
+ if args.ablate == "pool":
427
+ return pool_diag(cg, qs)
428
+ if args.ablate == "idcheck":
429
+ a = run_arm(cg, qs, ARM4, k=k, fusion="sum")
430
+ b = run_arm(cg, qs, ARM4, k=k, fusion="max")
431
+ diff = [i for i, (x, y) in enumerate(zip(a, b))
432
+ if x["rank"] != y["rank"] or x["labels"] != y["labels"]]
433
+ print("\n恒等性自检:四路 sum vs 四路 max")
434
+ print(" (若 bucket/entity/graph 三路确实全空,两者应逐条完全相同)")
435
+ print(f" 不同查询:{len(diff)}/{len(a)}")
436
+ for i in diff[:6]:
437
+ print(f" [{qs[i]['mode']}] sum rank={a[i]['rank']} "
438
+ f"max rank={b[i]['rank']}")
439
+ return 0
440
+
441
+ base = summarize(run_arm(cg, qs, ARM4, k=k), k=k)
442
+ real = summarize(run_arm(cg, qs, ARM5, k=k, path_weights={"fuzzy": 1.0},
443
+ fusion="max"), k=k)
444
+ if args.ablate == "shuffle":
445
+ patch_shuffled_fuzzy(cg, seed=args.seed)
446
+ label = "fuzzy 候选同、顺序打乱"
447
+ else:
448
+ patch_random_path(cg, seed=args.seed)
449
+ label = "第5路=全库随机排序"
450
+ abl = summarize(run_arm(cg, qs, ARM5, k=k, path_weights={"fuzzy": 1.0},
451
+ fusion="max"), k=k)
452
+ print(f"\n对照实验(均 max 融合,Top-{k},n={len(qs)} 查询)")
453
+ print(f"{'臂':<26}{'self@1':>9}{'self@%d' % k:>9}{'MRR':>8}{'sigP':>8}")
454
+ print("-" * 62)
455
+ for name, s in (("四路基线(仅 lexical 有效)", base), ("五路 fuzzy max", real),
456
+ (label, abl)):
457
+ print(f"{name:<26}{_pct(s['self@1']):>9}"
458
+ f"{_pct(s['self@' + str(k)]):>9}{s['mrr']:>8.3f}"
459
+ f"{_pct(s['sigP']):>8}")
460
+ print("-" * 62)
461
+ print(" 判读:若「打乱」或「随机」也拿到大部分 +self@10 → 增益来自融合机制,"
462
+ "不是 fuzzy 的排序质量。")
463
+ return 0
464
+
465
+
466
+ # 生效条件:argv 经 argparse 解析(--n/--k/--seed/--rebuild/--per-path/--sweep/--diag/--ablate/--sympool/--fusion/--lexfull/--zero-importance 等);外部数据集缺失即抛异常、不静默降级;返回进程退出码;
467
+ def main():
468
+ ap = argparse.ArgumentParser()
469
+ ap.add_argument("--n", type=int, default=250, help="抽样 signal 条数(0=全量)")
470
+ ap.add_argument("--k", type=int, default=10)
471
+ ap.add_argument("--seed", type=int, default=7)
472
+ ap.add_argument("--rebuild", action="store_true")
473
+ ap.add_argument("--per-path", action="store_true", help="逐路诊断(慢)")
474
+ ap.add_argument("--sweep", action="store_true", help="融合策略扫描(慢)")
475
+ ap.add_argument("--diag", action="store_true", help="打印失败案例诊断")
476
+ ap.add_argument("--ablate", default="none",
477
+ choices=["none", "idcheck", "shuffle", "random", "pool"],
478
+ help="对照实验:恒等性自检 / 打乱 fuzzy 顺序 / 随机第5路 / 候选池对称性")
479
+ ap.add_argument("--sympool", action="store_true",
480
+ help="抬高 GLOBAL_CAP,使 lexical 与 fuzzy 候选池对称")
481
+ ap.add_argument("--fusion", default="sum", choices=["sum", "max"],
482
+ help="五路融合模式(sum=经典 RRF 求和;max=取各路最高贡献)")
483
+ ap.add_argument("--lexfull", action="store_true",
484
+ help="lexical 也对全量池打分(与 fuzzy 的扫描范围真正对等)")
485
+ ap.add_argument("--zero-importance", action="store_true",
486
+ help="抹掉 importance(消除数据集里 importance 对 label 的泄漏)")
487
+ args = ap.parse_args()
488
+
489
+ print("=" * 76)
490
+ print("memory-bench-1000 · 四路 vs 五路(+fuzzy 分级隶属度)A/B")
491
+ print("=" * 76)
492
+
493
+ rows = load_rows()
494
+ lab = collections.Counter(r.get("label") for r in rows)
495
+ print(f"\n数据集:{len(rows)} 条 signal {lab['signal']} / noise {lab['noise']} "
496
+ f"/ unlabeled {lab['unlabeled']} → 噪声率 {lab['noise'] / len(rows):.0%}")
497
+
498
+ if args.rebuild and os.path.isdir(ROOT):
499
+ import shutil
500
+ shutil.rmtree(ROOT)
501
+ t0 = time.perf_counter()
502
+ cg = build(ROOT, rows)
503
+ install_read_cache(cg)
504
+ print(f" 建库耗时 {(time.perf_counter() - t0) * 1000:.0f} ms"
505
+ f"({len(cg.index['nodes'])} 节点,{len(cg.index['buckets'])} 桶)")
506
+
507
+ if args.sympool:
508
+ cap = patch_symmetric_pool(cg)
509
+ print(f" [对照] 候选池对称化:GLOBAL_CAP → {cap}(lexical 不再按插入序截断)")
510
+ if args.zero_importance:
511
+ patch_zero_importance(cg)
512
+ print(" [对照] importance 已抹平为 0.5(消除 signal 的 label 泄漏)")
513
+ if args.lexfull:
514
+ patch_lexical_full(cg)
515
+ print(" [对照] lexical 全量池打分(与 fuzzy 扫描范围对等)")
516
+
517
+ qs = make_queries(rows, n=args.n, seed=args.seed)
518
+ by_mode = collections.Counter(q["mode"] for q in qs)
519
+ print(f"\n查询集:{len(qs)} 条 {dict(by_mode)}"
520
+ f"(seed={args.seed},signal 抽样 {args.n})")
521
+
522
+ if args.ablate != "none":
523
+ return ablate(cg, qs, args)
524
+
525
+ t0 = time.perf_counter()
526
+ r4 = run_arm(cg, qs, ARM4, k=args.k, fusion="sum")
527
+ t4 = time.perf_counter() - t0
528
+ t0 = time.perf_counter()
529
+ r5 = run_arm(cg, qs, ARM5, k=args.k, path_weights={"fuzzy": 1.0},
530
+ fusion=args.fusion)
531
+ t5 = time.perf_counter() - t0
532
+ print(f"耗时:四路 {t4:.1f}s / 五路 {t5:.1f}s(五路融合={args.fusion})")
533
+
534
+ modes = [m for m in ("orig", "head", "para", "drop") if by_mode.get(m)]
535
+ print(f"\n{'模式':<6}{'臂':<5}{'n':>5}{'self@1':>9}{'self@%d' % args.k:>9}"
536
+ f"{'MRR':>8}{'sigP':>8}{'noiseP':>9}{'fuzzy参与':>10}")
537
+ print("-" * 76)
538
+ delta = {}
539
+ for m in modes:
540
+ s4 = summarize([r for r in r4 if r["mode"] == m], k=args.k)
541
+ s5 = summarize([r for r in r5 if r["mode"] == m], k=args.k)
542
+ for arm, s in (("四路", s4), ("五路", s5)):
543
+ print(f"{m:<6}{arm:<5}{s['n']:>5}{_pct(s['self@1']):>9}"
544
+ f"{_pct(s['self@' + str(args.k)]):>9}{s['mrr']:>8.3f}"
545
+ f"{_pct(s['sigP']):>8}{_pct(s['noiseP']):>9}"
546
+ f"{_pct(s['fuzzy_prov']):>10}")
547
+ delta[m] = {k: s5[k] - s4[k] for k in
548
+ ("self@1", "self@" + str(args.k), "mrr", "sigP", "noiseP")}
549
+ print("-" * 76)
550
+
551
+ print(f"\n{'净变化(五路 − 四路)':<20}{'self@1':>10}{'self@%d' % args.k:>10}"
552
+ f"{'MRR':>10}{'sigP':>10}{'noiseP':>10}")
553
+ for m in modes:
554
+ d = delta[m]
555
+ print(f"{m:<20}{d['self@1']:>+10.1%}{d['self@' + str(args.k)]:>+10.1%}"
556
+ f"{d['mrr']:>+10.3f}{d['sigP']:>+10.1%}{d['noiseP']:>+10.1%}")
557
+
558
+ # 全模式合并(诚实口径:四模式混合,权重按出现次数)
559
+ s4a = summarize(r4, k=args.k)
560
+ s5a = summarize(r5, k=args.k)
561
+ print(f"\n{'全模式合并':<20}{'self@1':>10}{'self@%d' % args.k:>10}"
562
+ f"{'MRR':>10}{'sigP':>10}{'noiseP':>10}")
563
+ print(f"{'四路':<20}{_pct(s4a['self@1']):>10}"
564
+ f"{_pct(s4a['self@' + str(args.k)]):>10}{s4a['mrr']:>10.3f}"
565
+ f"{_pct(s4a['sigP']):>10}{_pct(s4a['noiseP']):>10}")
566
+ print(f"{'五路':<20}{_pct(s5a['self@1']):>10}"
567
+ f"{_pct(s5a['self@' + str(args.k)]):>10}{s5a['mrr']:>10.3f}"
568
+ f"{_pct(s5a['sigP']):>10}{_pct(s5a['noiseP']):>10}")
569
+ print(f"{'净变化':<20}{s5a['self@1'] - s4a['self@1']:>+10.1%}"
570
+ f"{s5a['self@' + str(args.k)] - s4a['self@' + str(args.k)]:>+10.1%}"
571
+ f"{s5a['mrr'] - s4a['mrr']:>+10.3f}"
572
+ f"{s5a['sigP'] - s4a['sigP']:>+10.1%}"
573
+ f"{s5a['noiseP'] - s4a['noiseP']:>+10.1%}")
574
+
575
+ print_sig(r4, r5, qs, k=args.k, seed=args.seed)
576
+
577
+ if args.sweep:
578
+ print(f"\n融合策略扫描(全模式合并,Top-{args.k};相对四路基线的净变化)")
579
+ print(f"{'策略':<18}{'self@1':>9}{'Δself@1':>10}{'self@%d' % args.k:>9}"
580
+ f"{'MRR':>8}{'sigP':>8}{'noiseP':>9}")
581
+ print("-" * 76)
582
+ base = summarize(r4, k=args.k)
583
+ arms = [("四路 sum(基线)", ARM4, None, None, "sum"),
584
+ ("四路 max", ARM4, None, None, "max"),
585
+ ("五路 sum w=1.0", ARM5, {"fuzzy": 1.0}, None, "sum"),
586
+ ("五路 sum w=0.3", ARM5, {"fuzzy": 0.3}, None, "sum"),
587
+ ("五路 sum w=0.1", ARM5, {"fuzzy": 0.1}, None, "sum"),
588
+ ("五路 max w=1.0", ARM5, {"fuzzy": 1.0}, None, "max"),
589
+ ("五路 max w=0.5", ARM5, {"fuzzy": 0.5}, None, "max"),
590
+ ("fuzzy 仅召回", ARM5, None, {"fuzzy"}, "sum")]
591
+ if args.n and args.n > 100: # 全量时只保留关键臂,控制耗时
592
+ keep = {"四路 sum(基线)", "五路 sum w=1.0", "五路 max w=1.0",
593
+ "fuzzy 仅召回"}
594
+ arms = [a for a in arms if a[0] in keep]
595
+ for label, arm, pw, ro, fu in arms:
596
+ if label.startswith("四路 sum"):
597
+ s = base
598
+ else:
599
+ s = summarize(run_arm(cg, qs, arm, k=args.k, path_weights=pw,
600
+ recall_only=ro, fusion=fu), k=args.k)
601
+ print(f"{label:<18}{_pct(s['self@1']):>9}"
602
+ f"{s['self@1'] - base['self@1']:>+10.1%}"
603
+ f"{_pct(s['self@' + str(args.k)]):>9}{s['mrr']:>8.3f}"
604
+ f"{_pct(s['sigP']):>8}{_pct(s['noiseP']):>9}")
605
+ print("-" * 76)
606
+
607
+ if args.per_path:
608
+ print(f"\n逐路单独质量(各路径独立排序,Top-{args.k})")
609
+ print(f"{'模式':<6}{'路':<9}{'n':>5}{'self@1':>9}{'self@%d' % args.k:>9}"
610
+ f"{'MRR':>8}{'sigP':>8}{'noiseP':>9}")
611
+ print("-" * 76)
612
+ for m in modes:
613
+ sub = [q for q in qs if q["mode"] == m]
614
+ for p in ARM5:
615
+ s = summarize(run_single(cg, sub, p, k=args.k), k=args.k)
616
+ print(f"{m:<6}{p:<9}{s['n']:>5}{_pct(s['self@1']):>9}"
617
+ f"{_pct(s['self@' + str(args.k)]):>9}{s['mrr']:>8.3f}"
618
+ f"{_pct(s['sigP']):>8}{_pct(s['noiseP']):>9}")
619
+ print("-" * 76)
620
+
621
+ if args.diag:
622
+ diag_failures(cg, [q for q in qs if q["mode"] == "drop"], k=args.k)
623
+
624
+ src = collections.Counter(r["expand_source"] for r in r5)
625
+ print(f"\nfuzzy 路扩展来源:{dict(src)}")
626
+ print("\n诚实边界:本数据集无显式 edges → graph 路候选为空;节点无 domain: 标签 → "
627
+ "fuzzy 的大域亲和项恒等(实际区分力全部来自加权覆盖率)。")
628
+ return 0
629
+
630
+
631
+ if __name__ == "__main__":
632
+ sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
633
633
  sys.exit(main())