@furongjun1999/dsh-memory 0.8.1 → 0.9.7

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 (717) hide show
  1. package/README.md +662 -659
  2. package/codebuddy/README.md +1 -1
  3. package/data/policy.json +38 -27
  4. package/docs/README.md +18 -2
  5. package/docs/discipline/harnesses.yaml +1 -1
  6. package/docs/discipline/templates/spec.txt.tmpl +23 -0
  7. package/docs//350/247/204/350/214/203/AI/347/274/226/347/250/213/351/200/232/347/224/250/350/247/204/350/214/203.md +179 -0
  8. package/docs//350/247/204/350/214/203/AI/347/274/226/347/250/213/351/200/232/347/224/250/350/247/204/350/214/203_/347/201/265/346/236/242/351/200/202/351/205/215.md +45 -0
  9. package/docs//350/247/204/350/214/203/AI/347/274/226/347/250/213/351/200/232/347/224/250/350/247/204/350/214/203_/350/207/252/346/243/200/346/270/205/345/215/225.md +69 -0
  10. package/dsh/README.md +1 -1
  11. package/dsh/cordis.yml.example +139 -139
  12. package/hive/README.md +705 -0
  13. package/hive/id_charset_blocks.txt +147 -0
  14. package/lib/bridge.d.ts +3 -0
  15. package/lib/bridge.js +32 -11
  16. package/lib/cli.js +14 -17
  17. package/lib/hooks.d.ts +37 -3
  18. package/lib/hooks.js +176 -24
  19. package/lib/index.d.ts +2 -2
  20. package/lib/index.js +29 -6
  21. package/lib/init.js +21 -21
  22. package/lib/lib/datapath.d.ts +56 -0
  23. package/lib/lib/datapath.js +102 -9
  24. package/lib/lib/hook_audit.d.ts +0 -1
  25. package/lib/lib/hook_audit.js +51 -3
  26. package/lib/lib/mdcg_client.d.ts +4 -1
  27. package/lib/lib/mdcg_client.js +10 -2
  28. package/lib/lib/mutual.js +61 -15
  29. package/lib/lib/prompt_safety.js +28 -2
  30. package/lib/lib/roleplay_web.js +30 -2
  31. package/lib/lib/token_store.d.ts +25 -6
  32. package/lib/lib/token_store.js +224 -33
  33. package/lib/tools.d.ts +15 -2
  34. package/lib/tools.js +44 -4
  35. package/md_cg/_guard_kit.py +121 -0
  36. package/md_cg/admission.py +718 -718
  37. package/md_cg/anchor.py +114 -0
  38. package/md_cg/audit.py +37 -7
  39. package/md_cg/autonomy.py +433 -288
  40. package/md_cg/autonomy_modes.py +781 -642
  41. package/md_cg/bench_beir_bm25.py +154 -0
  42. package/md_cg/bench_beir_scifact.py +170 -0
  43. package/md_cg/bench_bm25_blockmax.py +134 -0
  44. package/md_cg/bench_bm25_incremental.py +246 -0
  45. package/md_cg/bench_ccg_heading.py +118 -0
  46. package/md_cg/bench_csre_correctness.py +145 -0
  47. package/md_cg/bench_excerpt_budget.py +94 -0
  48. package/md_cg/bench_fuzzy_substrings.py +299 -0
  49. package/md_cg/bench_memory_rsi.py +358 -0
  50. package/md_cg/bench_memory_rsi_latency.py +82 -0
  51. package/md_cg/bench_memory_rsi_public.py +116 -0
  52. package/md_cg/bench_memory_rsi_tools.py +229 -0
  53. package/md_cg/bench_rrf_candidates.py +212 -0
  54. package/md_cg/bench_rrf_public.py +100 -0
  55. package/md_cg/bench_text_projections.py +221 -0
  56. package/md_cg/bench_timeanchor.py +357 -0
  57. package/md_cg/bm25.py +601 -0
  58. package/md_cg/branches.py +404 -327
  59. package/md_cg/build_postings.py +74 -73
  60. package/md_cg/ccgc.py +4 -2
  61. package/md_cg/chain.py +573 -379
  62. package/md_cg/coldverify.py +531 -292
  63. package/md_cg/condaccept.py +263 -0
  64. package/md_cg/condgate.py +308 -0
  65. package/md_cg/config_registry.py +329 -0
  66. package/md_cg/config_registry_bulk.py +482 -0
  67. package/md_cg/config_registry_guard.py +487 -0
  68. package/md_cg/config_render.py +354 -0
  69. package/md_cg/config_validate.py +455 -0
  70. package/md_cg/conformance.py +747 -726
  71. package/md_cg/conformance_core.py +753 -0
  72. package/md_cg/consistency.py +1278 -955
  73. package/md_cg/consolidate.py +1793 -1633
  74. package/md_cg/crypto.py +346 -24
  75. package/md_cg/datapath.py +137 -12
  76. package/md_cg/eval_common.py +522 -575
  77. package/md_cg/evidence.py +946 -585
  78. package/md_cg/evidence_delivery.py +101 -0
  79. package/md_cg/experience_consolidation.py +317 -0
  80. package/md_cg/export.py +329 -222
  81. package/md_cg/forgetting.py +1050 -922
  82. package/md_cg/freshness.py +601 -548
  83. package/md_cg/fsutil.py +1120 -824
  84. package/md_cg/goal_gen.py +458 -0
  85. package/md_cg/hotcache.py +107 -9
  86. package/md_cg/hyperedge.py +262 -251
  87. package/md_cg/identity.py +573 -391
  88. package/md_cg/imgskill.py +1712 -0
  89. package/md_cg/insight.py +540 -521
  90. package/md_cg/interface_schema.py +146 -0
  91. package/md_cg/interop.py +20 -0
  92. package/md_cg/judgment_manifest.py +24 -1
  93. package/md_cg/lifecycle.py +426 -345
  94. package/md_cg/links.py +27 -6
  95. package/md_cg/logref.py +332 -327
  96. package/md_cg/mcp_server.py +5227 -4449
  97. package/md_cg/mdcg.py +1301 -203
  98. package/md_cg/mdcos.py +7108 -5440
  99. package/md_cg/memory_rsi.py +308 -0
  100. package/md_cg/metacognition.py +726 -591
  101. package/md_cg/nodefile.py +151 -3
  102. package/md_cg/op_table.py +1128 -0
  103. package/md_cg/postings.py +524 -300
  104. package/md_cg/predict.py +1103 -1100
  105. package/md_cg/probe_memory_rsi.py +95 -0
  106. package/md_cg/probe_memory_rsi_mcp.py +66 -0
  107. package/md_cg/protect.py +654 -494
  108. package/md_cg/protocol.py +492 -411
  109. package/md_cg/provenance.py +606 -600
  110. package/md_cg/qmd.py +477 -0
  111. package/md_cg/qmd_llm.py +334 -0
  112. package/md_cg/query_excerpt.py +171 -0
  113. package/md_cg/queue_health.py +130 -0
  114. package/md_cg/reach.py +470 -453
  115. package/md_cg/readcache.py +115 -3
  116. package/md_cg/reconcile.py +239 -228
  117. package/md_cg/refindex.py +1191 -1042
  118. package/md_cg/refine.py +613 -604
  119. package/md_cg/retrieve.py +128 -0
  120. package/md_cg/review_cli.py +204 -26
  121. package/md_cg/revise.py +140 -0
  122. package/md_cg/rollback.py +657 -420
  123. package/md_cg/rollback_cli.py +124 -104
  124. package/md_cg/rotate.py +347 -0
  125. package/md_cg/routing.py +490 -429
  126. package/md_cg/rrf_candidates.py +204 -0
  127. package/md_cg/run_tests.py +19 -1
  128. package/md_cg/scrub.py +177 -23
  129. package/md_cg/security.py +269 -17
  130. package/md_cg/self_state.py +1053 -1029
  131. package/md_cg/self_surface.py +50 -0
  132. package/md_cg/semantic/canonical.py +144 -144
  133. package/md_cg/semantic/export_en_zh_map.py +2 -2
  134. package/md_cg/semantic/zh_en_atoms.py +141 -139
  135. package/md_cg/session5.py +106 -0
  136. package/md_cg/signer.py +76 -2
  137. package/md_cg/sleep.py +1779 -1583
  138. package/md_cg/sources.py +1091 -833
  139. package/md_cg/state_events.py +75 -10
  140. package/md_cg/state_slots.py +312 -221
  141. package/md_cg/stg.py +85 -18
  142. package/md_cg/stgidx.py +537 -281
  143. package/md_cg/subgraph.py +851 -729
  144. package/md_cg/sustain.py +458 -19
  145. package/md_cg/task_memory.py +367 -0
  146. package/md_cg/test_action_derive.py +210 -203
  147. package/md_cg/test_audit_heartbeat_20261015.py +417 -0
  148. package/md_cg/test_audit_protection_20261015.py +483 -0
  149. package/md_cg/test_audit_rotate_parity.py +280 -0
  150. package/md_cg/test_audit_state_20261015.py +253 -0
  151. package/md_cg/test_audit_write_20261015.py +161 -0
  152. package/md_cg/test_auto_defaults.py +43 -4
  153. package/md_cg/test_autonomy_emotion.py +328 -0
  154. package/md_cg/test_autonomy_modes.py +2013 -1973
  155. package/md_cg/test_b1_foreign_read.py +245 -0
  156. package/md_cg/test_bm25.py +438 -0
  157. package/md_cg/test_bm25_hydration_refill.py +265 -0
  158. package/md_cg/test_bm25_incremental.py +241 -0
  159. package/md_cg/test_bm25_incremental_mutations.py +160 -0
  160. package/md_cg/test_bm25_mutations.py +205 -0
  161. package/md_cg/test_bm25_pool_view.py +196 -0
  162. package/md_cg/test_boot_openblas_guard.py +218 -197
  163. package/md_cg/test_boundary_hit.py +1 -1
  164. package/md_cg/test_branches_sensitivity.py +375 -0
  165. package/md_cg/test_c8_search_rrf_gates.py +1 -1
  166. package/md_cg/test_canonical_timezones.py +211 -0
  167. package/md_cg/test_ccg_heading_parser.py +73 -0
  168. package/md_cg/test_chain_best_first.py +403 -0
  169. package/md_cg/test_chain_candidate_scope.py +227 -0
  170. package/md_cg/test_cn_recall_grams.py +206 -0
  171. package/md_cg/test_code_kind_forbidden.py +101 -0
  172. package/md_cg/test_coldverify_drain.py +127 -0
  173. package/md_cg/test_condaccept.py +156 -0
  174. package/md_cg/test_condgate.py +141 -0
  175. package/md_cg/test_config_registry.py +55 -0
  176. package/md_cg/test_crypto_rotate.py +257 -0
  177. package/md_cg/test_csre_correctness.py +297 -0
  178. package/md_cg/test_csre_index_import_guard.py +161 -0
  179. package/md_cg/test_csre_mutations.py +34 -0
  180. package/md_cg/test_csre_session.py +69 -0
  181. package/md_cg/test_curiosity_budget.py +468 -0
  182. package/md_cg/test_d17_autostart_toolface.py +453 -0
  183. package/md_cg/test_datapath_tilde_home.py +271 -0
  184. package/md_cg/test_derived_cache_generations.py +256 -0
  185. package/md_cg/test_evidence_delivery_compact.py +54 -0
  186. package/md_cg/test_exact_text_cache.py +191 -0
  187. package/md_cg/test_excerpt_budget.py +188 -0
  188. package/md_cg/test_experience_consolidation.py +112 -0
  189. package/md_cg/test_export_default_collision.py +138 -0
  190. package/md_cg/test_export_snapshots.py +322 -0
  191. package/md_cg/test_fuzzy_substring_equivalence.py +127 -0
  192. package/md_cg/test_gain_gate.py +16 -5
  193. package/md_cg/test_goal_gen.py +355 -0
  194. package/md_cg/test_h2_session_view_norm.py +251 -233
  195. package/md_cg/test_h4_sustain_snapshot.py +1490 -1387
  196. package/md_cg/test_heartbeat_ledger.py +557 -0
  197. package/md_cg/test_hierarchy_audit_graph.py +237 -0
  198. package/md_cg/test_i50c_meta_passthrough.py +628 -608
  199. package/md_cg/test_i50e_readside_protection.py +658 -656
  200. package/md_cg/test_identity_attribution.py +96 -2
  201. package/md_cg/test_identity_contract.py +329 -0
  202. package/md_cg/test_imgskill.py +1444 -0
  203. package/md_cg/test_index_snapshot_shape.py +240 -0
  204. package/md_cg/test_interop_judgment.py +258 -228
  205. package/md_cg/test_issue111_gate_order.py +77 -0
  206. package/md_cg/test_issue111_scoring_contract.py +87 -0
  207. package/md_cg/test_issue121_calibration_bounds.py +293 -0
  208. package/md_cg/test_issue128_session_env_authority.py +246 -0
  209. package/md_cg/test_issue130_rrf_rank_credibility.py +217 -0
  210. package/md_cg/test_issue131_postings_content_staleness.py +424 -0
  211. package/md_cg/test_issue145_sleep_no_silent_rollback.py +168 -0
  212. package/md_cg/test_issue146_frontmatter_quarantine.py +428 -0
  213. package/md_cg/test_issue147_pack_id_escape.py +378 -0
  214. package/md_cg/test_issue148_merge_downgrade_leak.py +192 -0
  215. package/md_cg/test_issue149_accel_default_face.py +249 -0
  216. package/md_cg/test_issue150_empty_query_terms.py +328 -0
  217. package/md_cg/test_issue151_153_157_write_cache_index.py +152 -0
  218. package/md_cg/test_issue151b_base_add_importance_guard.py +166 -0
  219. package/md_cg/test_issue152_sealed_postings.py +309 -0
  220. package/md_cg/test_issue152b_decrypt_cache.py +218 -0
  221. package/md_cg/test_issue153b_cache_alias_and_canon.py +236 -0
  222. package/md_cg/test_issue153b_index_generation_invalidation.py +143 -0
  223. package/md_cg/test_issue157b_index_remainder.py +344 -0
  224. package/md_cg/test_issue189_conclusion_bidirectional.py +223 -0
  225. package/md_cg/test_issue190_tidy_idempotent.py +806 -0
  226. package/md_cg/test_issue191_import_trust.py +382 -0
  227. package/md_cg/test_issue192_move_layer_indices_guard.py +258 -0
  228. package/md_cg/test_issue193_consistency_knowledge_guard.py +524 -0
  229. package/md_cg/test_issue193_write_scope.py +378 -0
  230. package/md_cg/test_issue197_rank_credibility_property.py +201 -0
  231. package/md_cg/test_issue211_export_target.py +325 -0
  232. package/md_cg/test_issue212_writeface_gaps.py +349 -0
  233. package/md_cg/test_issue214_reject_sensitivity.py +267 -0
  234. package/md_cg/test_issue215_condition_source_gap.py +272 -0
  235. package/md_cg/test_issue216_clash_routes.py +214 -0
  236. package/md_cg/test_issue217_slot_routes.py +248 -0
  237. package/md_cg/test_issue218_postcondition_value_blind.py +222 -0
  238. package/md_cg/test_issue225_consistency_torn_log.py +433 -0
  239. package/md_cg/test_issue245_swarm_traversal.py +366 -0
  240. package/md_cg/test_issue258_pack_sensitivity.py +468 -0
  241. package/md_cg/test_issue265_coldverify_multiproc.py +438 -0
  242. package/md_cg/test_issue274_read_node_id_none.py +341 -0
  243. package/md_cg/test_issue275_layer_param.py +250 -0
  244. package/md_cg/test_issue277_node_id_traversal.py +384 -0
  245. package/md_cg/test_issue278_neg_separated_tension.py +271 -0
  246. package/md_cg/test_issue278_neg_window.py +294 -0
  247. package/md_cg/test_issue278_negative_layering.py +83 -0
  248. package/md_cg/test_issue279_signers_path_guard.py +652 -0
  249. package/md_cg/test_issue284_skip_not_pass.py +291 -0
  250. package/md_cg/test_issue294_flywheel_sensitivity.py +573 -0
  251. package/md_cg/test_issue295_sleep_root_binding.py +589 -0
  252. package/md_cg/test_issue297_rewrite_sensitivity.py +454 -0
  253. package/md_cg/test_issue298_consistency_ledger_sensitivity.py +421 -0
  254. package/md_cg/test_issue301_baseline_corpus_gate.py +245 -0
  255. package/md_cg/test_issue303_design_boundaries.py +262 -0
  256. package/md_cg/test_issue313_writelimit_sensitivity.py +404 -0
  257. package/md_cg/test_issue315_merge_branch_sensitivity.py +384 -0
  258. package/md_cg/test_issue315_sensitivity_passthrough_guard.py +514 -0
  259. package/md_cg/test_issue319_tenant_unregistered_bypass.py +257 -0
  260. package/md_cg/test_issue320_vstate_requires_verify.py +279 -0
  261. package/md_cg/test_issue321_mcp_deep_json.py +305 -0
  262. package/md_cg/test_issue322_node_write_lock.py +727 -0
  263. package/md_cg/test_issue323_aggregate_segment_boundary.py +365 -0
  264. package/md_cg/test_issue324_write_size_fields.py +215 -0
  265. package/md_cg/test_issue325_audit_poison.py +230 -0
  266. package/md_cg/test_issue326_append_jsonl_oserror.py +464 -0
  267. package/md_cg/test_issue326_ingest_auto_whitelist.py +331 -0
  268. package/md_cg/test_issue326_key_perms.py +355 -0
  269. package/md_cg/test_issue326_twin_parser_parity.py +323 -0
  270. package/md_cg/test_issue326_vm_call_frame.py +569 -0
  271. package/md_cg/test_issue345_writelimit_effective_sens.py +234 -0
  272. package/md_cg/test_issue346_overwrite_sensitivity_gate.py +424 -0
  273. package/md_cg/test_issue346_review_overwrite_guard.py +509 -0
  274. package/md_cg/test_issue348_insight_verify_op.py +200 -0
  275. package/md_cg/test_issue348_merge_rmw_lock.py +345 -0
  276. package/md_cg/test_issue350_casefold_collision.py +236 -0
  277. package/md_cg/test_issue351_ingest_root_fallback.py +202 -0
  278. package/md_cg/test_issue352_imgaudit.py +418 -0
  279. package/md_cg/test_issue352_pathsjson.py +537 -0
  280. package/md_cg/test_issue352_refindex.py +412 -0
  281. package/md_cg/test_issue352_rollback.py +604 -0
  282. package/md_cg/test_issue352_vstateinherit.py +196 -0
  283. package/md_cg/test_issue354_p1_writelimit_type.py +522 -0
  284. package/md_cg/test_issue354_p2_datapath_clock.py +381 -0
  285. package/md_cg/test_issue354_p2_writelimit_toctou.py +327 -0
  286. package/md_cg/test_issue354_rust_index_containment.py +571 -0
  287. package/md_cg/test_issue354_rust_wal_containment.py +606 -0
  288. package/md_cg/test_issue358_truncation_observable.py +247 -0
  289. package/md_cg/test_issue360_tidy_private_excerpt.py +196 -0
  290. package/md_cg/test_issue361_blindspot_not_silent_drop.py +494 -0
  291. package/md_cg/test_issue362_neg_condition.py +468 -0
  292. package/md_cg/test_issue363_lexical_idf.py +275 -0
  293. package/md_cg/test_issue370_readcache_assembly.py +488 -0
  294. package/md_cg/test_issue375_state_slot_retire.py +325 -0
  295. package/md_cg/test_issue376_heartbeat_failed.py +293 -0
  296. package/md_cg/test_issue377_layer_protect_stamp.py +324 -0
  297. package/md_cg/test_issue378_protected_floor.py +310 -0
  298. package/md_cg/test_issue383_state_event_write_gate.py +462 -0
  299. package/md_cg/test_issue397_sens_eff_wiring.py +435 -0
  300. package/md_cg/test_issue398_toolface_op_parity.py +511 -0
  301. package/md_cg/test_issue399_hive_json_shape.py +317 -0
  302. package/md_cg/test_issue400_hive_dirty_args.py +450 -0
  303. package/md_cg/test_issue401_wisdom_codegen.py +295 -0
  304. package/md_cg/test_issue404_heal_receipt.py +390 -0
  305. package/md_cg/test_issue405_git_pathspec.py +333 -0
  306. package/md_cg/test_issue408_eface_registry.py +291 -0
  307. package/md_cg/test_issue409_compact_index_completeness.py +509 -0
  308. package/md_cg/test_issue410_verify_demote_write.py +366 -0
  309. package/md_cg/test_issue411_scrub_idempotence.py +575 -0
  310. package/md_cg/test_issue412_discipline_producer_parity.py +312 -0
  311. package/md_cg/test_issue413_preimage_failclosed.py +239 -0
  312. package/md_cg/test_issue413_preimage_twin.py +340 -0
  313. package/md_cg/test_issue414_write_args_parity.py +613 -0
  314. package/md_cg/test_issue415_deps_gate_probe.py +358 -0
  315. package/md_cg/test_issue416_entry_key_subset.py +525 -0
  316. package/md_cg/test_issue417_twophase_merge_landed.py +423 -0
  317. package/md_cg/test_issue418_identity_role_type_gate.py +188 -0
  318. package/md_cg/test_issue418_role_type_gate.py +265 -0
  319. package/md_cg/test_issue419_pii_redaction.py +642 -0
  320. package/md_cg/test_issue424_merge_confirm_sensitivity.py +381 -0
  321. package/md_cg/test_issue425_ingest_library_path.py +331 -0
  322. package/md_cg/test_issue426a_dedup_receipt.py +242 -0
  323. package/md_cg/test_issue426b_root_clash_invisible.py +270 -0
  324. package/md_cg/test_issue426c_enqueue_not_silent.py +346 -0
  325. package/md_cg/test_issue426e_restore_overwrite_live.py +310 -0
  326. package/md_cg/test_issue426f_paths_gbk_failclosed.py +301 -0
  327. package/md_cg/test_issue427_serve_status_shape.py +191 -0
  328. package/md_cg/test_issue431_432_468_protect_single_point.py +322 -0
  329. package/md_cg/test_issue433_outlook_threshold_single_point.py +278 -0
  330. package/md_cg/test_issue434_435_float_gate.py +90 -0
  331. package/md_cg/test_issue439_sibling_sensitivity_inherit.py +448 -0
  332. package/md_cg/test_issue43_default_policy.py +874 -865
  333. package/md_cg/test_issue443a_state_event_length_gate.py +94 -0
  334. package/md_cg/test_issue443b_audit_record_length_gate.py +83 -0
  335. package/md_cg/test_issue443c_self_state_imgskill_length_gate.py +77 -0
  336. package/md_cg/test_issue444_scrub_evidence_plaintext.py +281 -0
  337. package/md_cg/test_issue445_metacognition_ledger_gate.py +454 -0
  338. package/md_cg/test_issue446_453_403_verified_evidence.py +382 -0
  339. package/md_cg/test_issue446_is_encrypted_envelope.py +166 -0
  340. package/md_cg/test_issue447_recent_sens_failclosed.py +270 -0
  341. package/md_cg/test_issue450_sensitivity_fallback_reason.py +51 -0
  342. package/md_cg/test_issue450_writegate_unknown_sens.py +203 -0
  343. package/md_cg/test_issue451_audit_actor.py +313 -0
  344. package/md_cg/test_issue452_ingest_root_hint.py +238 -0
  345. package/md_cg/test_issue452_ingest_root_sensitivity.py +299 -0
  346. package/md_cg/test_issue463_ghost_prune_guard.py +259 -0
  347. package/md_cg/test_issue466_basis_registry.py +469 -0
  348. package/md_cg/test_issue467_identity_sensitivity.py +204 -0
  349. package/md_cg/test_issue467_identity_sensitivity_parity.py +361 -0
  350. package/md_cg/test_issue469_scrub_override_passthrough.py +126 -0
  351. package/md_cg/test_issue470_wal_event_durable.py +427 -0
  352. package/md_cg/test_issue471_identity_readable.py +137 -0
  353. package/md_cg/test_issue472_audit_chain.py +91 -0
  354. package/md_cg/test_issue472_prune_ledger.py +106 -0
  355. package/md_cg/test_issue472_rollback_degraded.py +82 -0
  356. package/md_cg/test_issue491_compose_sandbox.py +460 -0
  357. package/md_cg/test_issue492_dup_readout_opt_in.py +292 -0
  358. package/md_cg/test_issue498_merge_trace_stale_index.py +280 -0
  359. package/md_cg/test_issue511_scoreboard_bad_files.py +256 -0
  360. package/md_cg/test_issue521_rotate_lock_suffix.py +110 -0
  361. package/md_cg/test_issue522_review_record_sensitivity.py +267 -0
  362. package/md_cg/test_issue524_orphan_noise_no_demote.py +88 -0
  363. package/md_cg/test_issue525_read_null_semantics.py +52 -0
  364. package/md_cg/test_issue538_hook_condition_space.py +97 -0
  365. package/md_cg/test_issue540_path_guard_whitespace.py +126 -0
  366. package/md_cg/test_issue541_tidy_protect_gate.py +141 -0
  367. package/md_cg/test_issue542_tenant_root_same.py +153 -0
  368. package/md_cg/test_issue543_issue_authorization.py +147 -0
  369. package/md_cg/test_issue544_sandbox_reflection_gate.py +175 -0
  370. package/md_cg/test_issue545_charset_vs_size_diagnostic.py +77 -0
  371. package/md_cg/test_issue546_ledger_concurrency.py +81 -0
  372. package/md_cg/test_issue547_append_jsonl_whitelist.py +115 -0
  373. package/md_cg/test_issue547_audit_durable.py +96 -0
  374. package/md_cg/test_issue549_durable_append.py +143 -0
  375. package/md_cg/test_issue557_r1_credential_value_forms.py +76 -0
  376. package/md_cg/test_issue557_r2_write_entries_size_gate.py +84 -0
  377. package/md_cg/test_issue557_r3_pem_block_redaction.py +75 -0
  378. package/md_cg/test_issue559_refine_prefix.py +70 -0
  379. package/md_cg/test_issue561_pending_review.py +109 -0
  380. package/md_cg/test_issue562_retired_faces.py +150 -0
  381. package/md_cg/test_issue66_scrub_semantics.py +107 -0
  382. package/md_cg/test_issue67_scrub_control_pool.py +233 -0
  383. package/md_cg/test_issue69_audit_boundary.py +129 -0
  384. package/md_cg/test_issue71_queue_backlog.py +377 -0
  385. package/md_cg/test_issue72_coldverify_controlled_drain.py +420 -0
  386. package/md_cg/test_issue73_doc_chapter_refs.py +326 -0
  387. package/md_cg/test_issue75_neural_failure.py +582 -0
  388. package/md_cg/test_issue78_export_concurrency.py +334 -0
  389. package/md_cg/test_issue80_residual_flush.py +169 -0
  390. package/md_cg/test_issue82_credential_rules.py +271 -0
  391. package/md_cg/test_issue82_layer_and_size.py +120 -0
  392. package/md_cg/test_issue84_portability.py +95 -0
  393. package/md_cg/test_issue85_export_overwrite.py +113 -0
  394. package/md_cg/test_issue86_runtime_revoke.py +110 -0
  395. package/md_cg/test_issue87_default_sensitivity_single.py +201 -0
  396. package/md_cg/test_issue88_conclusion_face.py +280 -0
  397. package/md_cg/test_issue88_execution_conflicts.py +87 -0
  398. package/md_cg/test_issue89_zero_score.py +169 -0
  399. package/md_cg/test_issue89a_score_cap_tiebreak.py +279 -0
  400. package/md_cg/test_issue91_verifier_sandbox.py +314 -0
  401. package/md_cg/test_issue91_verifier_timeout.py +528 -0
  402. package/md_cg/test_issue93_remember_redaction.py +282 -0
  403. package/md_cg/test_issue96_key_fallback.py +102 -0
  404. package/md_cg/test_issue98_tenant_root.py +104 -0
  405. package/md_cg/test_jsonl_commit_watermark.py +221 -0
  406. package/md_cg/test_live_validity_cache.py +256 -0
  407. package/md_cg/test_logref.py +1141 -1109
  408. package/md_cg/test_mcp_call_argument_types.py +99 -0
  409. package/md_cg/test_md_access_parity.py +330 -255
  410. package/md_cg/test_md_cg_no_hive_dep.py +140 -0
  411. package/md_cg/test_memory_rsi.py +215 -0
  412. package/md_cg/test_memory_rsi_adversarial.py +91 -0
  413. package/md_cg/test_mode_parity.py +1413 -1393
  414. package/md_cg/test_mutation_residue.py +120 -0
  415. package/md_cg/test_mutation_rollback.py +835 -805
  416. package/md_cg/test_n123_derive_expiry_chain.py +210 -205
  417. package/md_cg/test_n130_verify_falsified_protect.py +185 -185
  418. package/md_cg/test_n209_verify_write_face_gates.py +466 -461
  419. package/md_cg/test_n230_dirty_replay.py +52 -9
  420. package/md_cg/test_n62_tenant_bind_failclosed.py +200 -200
  421. package/md_cg/test_neg_coverage_budget.py +500 -0
  422. package/md_cg/test_neg_tail_honesty.py +733 -663
  423. package/md_cg/test_p0.py +16 -2
  424. package/md_cg/test_p14_root_fallback.py +373 -0
  425. package/md_cg/test_p1x_ref_root.py +47 -10
  426. package/md_cg/test_p21_tokens.py +294 -293
  427. package/md_cg/test_p24_evidence.py +232 -227
  428. package/md_cg/test_p26_refindex.py +20 -3
  429. package/md_cg/test_p27_docindex.py +792 -777
  430. package/md_cg/test_p28_refcheck.py +1053 -1032
  431. package/md_cg/test_p29_session_ingest_export.py +6 -0
  432. package/md_cg/test_p2_mcp.py +474 -453
  433. package/md_cg/test_p2_six_elements.py +476 -463
  434. package/md_cg/test_p3.py +370 -261
  435. package/md_cg/test_p33_ccg_wiring.py +300 -293
  436. package/md_cg/test_p3_legacy_closure.py +438 -433
  437. package/md_cg/test_p40_refine_worklist.py +244 -241
  438. package/md_cg/test_p47_session_view.py +25 -9
  439. package/md_cg/test_p4_freshness.py +781 -774
  440. package/md_cg/test_p83_jsonrpc_parse_error.py +124 -0
  441. package/md_cg/test_p9c_dedup_hints.py +288 -283
  442. package/md_cg/test_policy_required_ccg.py +444 -432
  443. package/md_cg/test_postings_digest_memo.py +139 -0
  444. package/md_cg/test_predict_beta.py +138 -135
  445. package/md_cg/test_qmd.py +500 -0
  446. package/md_cg/test_query_excerpt.py +209 -0
  447. package/md_cg/test_reach.py +456 -378
  448. package/md_cg/test_reach_snapshot_admission.py +218 -0
  449. package/md_cg/test_readcache_prodpath.py +4 -0
  450. package/md_cg/test_recall_face_guards.py +854 -812
  451. package/md_cg/test_recall_metadata.py +422 -0
  452. package/md_cg/test_retr_s5.py +312 -213
  453. package/md_cg/test_retr_score_once.py +14 -2
  454. package/md_cg/test_review_cli_attribution.py +11 -7
  455. package/md_cg/test_review_queue_batch.py +123 -0
  456. package/md_cg/test_review_queue_index.py +130 -0
  457. package/md_cg/test_rrf_candidates.py +292 -0
  458. package/md_cg/test_rrf_qualified_topk.py +214 -0
  459. package/md_cg/test_rrf_unique_votes.py +218 -0
  460. package/md_cg/test_s1_ast_judge.py +110 -0
  461. package/md_cg/test_scrub_ledger.py +413 -0
  462. package/md_cg/test_security_audit_b26.py +34 -3
  463. package/md_cg/test_security_audit_v21.py +1 -2
  464. package/md_cg/test_sensitivity_predicate_single_point.py +184 -0
  465. package/md_cg/test_session5_identity.py +93 -0
  466. package/md_cg/test_session_attribution_consistency.py +813 -0
  467. package/md_cg/test_session_identity_tristate.py +486 -0
  468. package/md_cg/test_sleep_p1.py +35 -3
  469. package/md_cg/test_state_event_op.py +521 -514
  470. package/md_cg/test_state_projection_streaming.py +136 -0
  471. package/md_cg/test_stg_interval_candidates.py +280 -0
  472. package/md_cg/test_stg_time_blocks.py +239 -0
  473. package/md_cg/test_stgidx_index_parity.py +1024 -1020
  474. package/md_cg/test_subgraph_bounded_bfs.py +227 -0
  475. package/md_cg/test_sustain_bounded.py +652 -624
  476. package/md_cg/test_task_memory.py +394 -0
  477. package/md_cg/test_text_projection_mutations.py +101 -0
  478. package/md_cg/test_time_anchor.py +421 -0
  479. package/md_cg/test_time_core_lint.py +1024 -975
  480. package/md_cg/test_tool_face.py +200 -189
  481. package/md_cg/test_trust_bounded_frontier.py +248 -0
  482. package/md_cg/test_trust_edge_reachability.py +405 -0
  483. package/md_cg/test_w5_path_fingerprint.py +381 -0
  484. package/md_cg/test_w7_redlines.py +659 -0
  485. package/md_cg/test_write_return_shape.py +919 -0
  486. package/md_cg/test_writepipe.py +228 -226
  487. package/md_cg/timeanchor.py +534 -0
  488. package/md_cg/tokens.py +68 -5
  489. package/md_cg/tool_face.py +273 -260
  490. package/md_cg/trust.py +1114 -1030
  491. package/md_cg/twophase.py +303 -231
  492. package/md_cg/weights.py +642 -638
  493. package/md_cg/whitebox.py +536 -527
  494. package/md_cg/whitebox_kb/aeis_core/core.py +5026 -5026
  495. package/md_cg/whitebox_kb/wisdom/chat_engine.py +1504 -1506
  496. package/md_cg/whitebox_kb/wisdom/code_compose.py +876 -837
  497. package/md_cg/whitebox_kb/wisdom/compiler_code_units.py +3040 -3033
  498. package/md_cg/whitebox_kb/wisdom/compose_engine.py +1634 -1633
  499. package/md_cg/whitebox_kb/wisdom/core/condition_vm.py +318 -298
  500. package/md_cg/whitebox_kb/wisdom/core/parser.py +1082 -1065
  501. package/md_cg/whitebox_kb/wisdom/csre.py +412 -264
  502. package/md_cg/whitebox_kb/wisdom/csre_session.py +97 -0
  503. package/md_cg/whitebox_kb/wisdom/md_access.py +490 -470
  504. package/md_cg/whitebox_kb/wisdom/migrate_wisdom.py +497 -476
  505. package/md_cg/whitebox_kb/wisdom/mini_python.py +995 -958
  506. package/md_cg/whitebox_kb/wisdom/navigate.py +249 -248
  507. package/md_cg/whitebox_kb/wisdom/net_units.py +2900 -2895
  508. package/md_cg/whitebox_kb/wisdom/neural_retrieve.py +100 -10
  509. package/md_cg/whitebox_kb/wisdom/semantic_translate.py +4060 -4054
  510. package/md_cg/whitebox_kb/wisdom/test_ecosystem_assembly.py +158 -158
  511. package/md_cg/whitebox_kb/wisdom/test_safe_module_proxy.py +147 -0
  512. package/md_cg/whitebox_kb/wisdom/verifier.py +1918 -1297
  513. package/md_cg/writelimit.py +584 -390
  514. package/md_cg/writepipe.py +1337 -826
  515. package/package.json +14 -9
  516. package/scripts/workspace_index.py +328 -0
  517. package/src/bridge.ts +34 -11
  518. package/src/cli.ts +75 -65
  519. package/src/hooks.ts +932 -776
  520. package/src/index.ts +30 -7
  521. package/src/init.ts +362 -361
  522. package/src/lib/datapath.ts +419 -326
  523. package/src/lib/hook_audit.ts +42 -3
  524. package/src/lib/mdcg_client.ts +469 -461
  525. package/src/lib/mutual.ts +481 -432
  526. package/src/lib/prompt_safety.ts +178 -149
  527. package/src/lib/roleplay_web.ts +30 -2
  528. package/src/lib/token_store.ts +274 -52
  529. package/src/tools.ts +271 -223
  530. package/zcode/README.md +1 -1
  531. 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 +0 -169
  532. 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 +0 -144
  533. 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 +0 -299
  534. 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 +0 -239
  535. package/docs/eval/AGI/344/270/203/347/273/264/350/257/204/345/210/206/346/212/245/345/221/212_/345/205/255/345/256/266/350/256/260/345/277/206/347/263/273/347/273/237/345/220/214/345/260/272/345/256/236/346/265/213_v3.1.md +0 -340
  536. package/docs/eval/DSH/346/227/245/345/277/227/347/264/242/345/274/225v2_/345/217/202/350/200/203dsh-TUI_v1.0.md +0 -248
  537. package/docs/eval/DSH/346/227/245/345/277/227/347/264/242/345/274/225/346/225/210/346/236/234/351/252/214/350/257/201_v1.0.md +0 -209
  538. package/docs/eval/DSH/347/253/257/347/274/272/351/231/267/344/270/223/351/241/271_v1.0.md +0 -254
  539. package/docs/eval/N225_/347/264/242/345/274/225/346/227/245/345/277/227/351/235/236/345/257/271/350/261/241/350/243/205/350/275/275/351/235/242/347/261/273/345/236/213/351/227/270_v1.0.md +0 -418
  540. package/docs/eval/P1b2_/350/257/273/351/235/242/344/273/243/351/231/205/344/277/256/345/244/215_v1.0.md +0 -124
  541. package/docs/eval/bench_lingshu_self/bench_self.py +0 -140
  542. package/docs/eval/bench_lingshu_self/self_bench_result.json +0 -404
  543. package/docs/eval/bench_lingshu_self//347/201/265/346/236/242/350/207/252/345/272/223/347/253/257/345/210/260/347/253/257/346/243/200/347/264/242/345/256/236/346/265/213_v1.0.md +0 -38
  544. package/docs/eval/cons200_/345/206/262/347/252/201/346/243/200/346/265/213/351/200/211/351/235/242_/345/256/236/346/226/275/350/256/260/345/275/225_v1.0.md +0 -223
  545. package/docs/eval/cons200_/345/206/262/347/252/201/346/243/200/346/265/213/351/200/211/351/235/242_/345/256/236/346/226/275/350/256/260/345/275/225_v1.1.md +0 -340
  546. package/docs/eval/issue43_/351/273/230/350/256/244/347/255/226/347/225/245/344/270/216/351/224/256/347/261/273/345/236/213/351/227/270_v1.1.md +0 -441
  547. package/docs/eval/issue43_/351/273/230/350/256/244/347/255/226/347/225/245/345/212/240/350/275/275/344/270/216/345/207/255/346/215/256/346/230/216/346/226/207/351/230/237/345/210/227_v1.0.md +0 -398
  548. package/docs/eval/issue50_/345/205/203/346/225/260/346/215/256/351/200/217/344/274/240/344/270/216/345/205/234/345/272/225_/344/277/256/345/244/215/350/256/260/345/275/225_v1.0.md +0 -437
  549. package/docs/eval/issue50_/345/215/212/351/207/215/345/244/215/345/276/205/345/256/232/345/244/215/346/240/270_/344/277/256/345/244/215/350/256/260/345/275/225_v1.0.md +0 -488
  550. package/docs/eval/issue50_/345/276/205/345/256/232/345/244/215/346/240/270/345/205/245/351/230/237_/344/277/256/345/244/215/350/256/260/345/275/225_v1.0.md +0 -428
  551. package/docs/eval/issue50_/350/257/273/351/235/242/344/277/235/346/212/244/345/217/252/350/256/244/346/230/276/345/274/217/346/235/245/346/272/220_/344/277/256/345/244/215/350/256/260/345/275/225_v1.0.md +0 -271
  552. package/docs/eval/issue50_/351/207/215/350/246/201/345/272/246/345/220/214/346/272/220/344/270/216/344/277/235/346/212/244/350/257/255/344/271/211_/344/277/256/345/244/215/350/256/260/345/275/225_v1.0.md +0 -306
  553. package/docs/eval/issue51_/344/270/200/351/224/256/345/256/211/350/243/205/345/244/261/350/264/245_/345/275/222/345/261/236/345/210/244/345/256/232_v1.0.md +0 -58
  554. package/docs/eval/issue52_/346/235/241/344/273/266/345/205/210/350/241/214/344/270/216/346/210/252/346/226/255/345/217/257/350/247/202/346/265/213_/344/277/256/345/244/215/350/256/260/345/275/225_v1.0.md +0 -360
  555. package/docs/eval/issue64_replay_check_/346/240/270/351/252/214/344/270/216/344/277/256/345/244/215_v1.0.md +0 -284
  556. package/docs/eval/issue65_session_recall_/344/274/232/350/257/235/351/232/224/347/246/273_/346/240/270/351/252/214/344/270/216/344/277/256/345/244/215_v1.0.md +0 -329
  557. package/docs/eval//344/270/211/346/241/243/350/207/252/346/262/273_/346/255/245/351/252/244/342/221/241/346/241/243/344/275/215/345/215/225/344/270/200/345/205/245/345/217/243_/350/220/275/347/240/201/350/256/260/345/275/225_v1.0.md +0 -651
  558. package/docs/eval//344/270/211/346/241/243/350/207/252/346/262/273_/346/255/245/351/252/244/342/221/242/345/217/230/346/233/264/345/215/225/344/270/216/345/233/236/346/273/232/345/216/237/350/257/255_/350/220/275/347/240/201/350/256/260/345/275/225_v1.0.md +0 -439
  559. package/docs/eval//344/270/211/346/241/243/350/207/252/346/262/273_/346/255/245/351/252/244/342/221/243/345/207/206/345/205/245/350/257/273/346/225/260_/350/220/275/347/240/201/350/256/260/345/275/225_v1.0.md +0 -1337
  560. package/docs/eval//344/270/211/346/241/243/350/207/252/346/262/273_/346/255/245/351/252/244/342/221/244/346/224/266/345/256/230/344/270/216/345/205/250/351/223/276/351/252/214/346/224/266_/350/220/275/347/240/201/350/256/260/345/275/225_v1.0.md +0 -1817
  561. package/docs/eval//344/270/215/345/217/257/351/235/240/346/200/247/350/220/275/345/234/260_P0_v1.0.md +0 -185
  562. package/docs/eval//344/274/230/345/214/226/347/254/254/344/270/200/346/211/271_/346/216/245/347/272/277/344/270/216/347/255/211/344/273/267/345/217/230/346/215/242_v1.0.md +0 -498
  563. package/docs/eval//344/274/230/345/214/226/347/254/254/344/270/211/346/211/271_/351/227/250/347/246/201/350/275/254/346/255/243/344/270/216/350/260/203/345/272/246/346/255/242/350/241/200/344/270/216/351/227/250/346/216/247/346/224/266/345/217/243_v1.0.md +0 -669
  564. package/docs/eval//344/274/230/345/214/226/347/254/254/344/272/214/346/211/271_/344/276/235/350/265/226/351/200/217/344/274/240/344/270/216/345/257/271/346/213/215/345/217/243/345/276/204/344/270/216/350/264/237/347/274/223/345/255/230_v1.0.md +0 -683
  565. package/docs/eval//345/207/272/350/264/247/351/235/242/345/206/222/347/203/237_/350/277/233/350/264/247/351/227/250/347/246/201_v1.0.md +0 -460
  566. package/docs/eval//345/217/221/345/270/20306_/344/270/200/351/224/256/351/205/215/347/275/256/344/270/216DSH0172_v1.0.md +0 -171
  567. package/docs/eval//345/217/221/345/270/20307_/345/244/226/351/203/250/346/212/245/345/221/212/345/233/233/346/211/271/344/277/256/345/244/215/344/270/216/346/217/222/344/273/266/351/235/242/345/212/240/345/233/272_v1.0.md +0 -209
  568. package/docs/eval//345/217/221/345/270/20308_/350/207/252/350/277/255/344/273/243/344/270/216/347/235/241/347/234/240_/345/233/276/346/243/200/347/264/242/350/267/257/344/270/216/346/235/203/351/207/215_v1.0.md +0 -97
  569. package/docs/eval//345/217/221/345/270/20309_/346/243/200/347/264/242/351/235/242/344/270/211/346/211/271/346/224/266/345/217/243_v1.0.md +0 -66
  570. package/docs/eval//345/217/221/345/270/20310_/350/257/273/351/235/242/346/215/237/345/235/217UTF8/345/256/266/346/227/217/346/224/266/345/217/243_v1.0.md +0 -71
  571. package/docs/eval//345/217/221/345/270/20311_/345/244/226/346/212/245/345/205/255/350/277/236/344/277/256/344/270/216/344/270/226/347/225/214/346/250/241/345/236/213/345/272/225/345/272/247_v1.0.md +0 -91
  572. package/docs/eval//345/217/221/345/270/20312_/344/270/226/347/225/214/346/250/241/345/236/213/345/212/237/350/203/275/347/253/257_v1.0.md +0 -108
  573. package/docs/eval//345/217/221/345/270/20313_/344/270/212/344/270/213/346/226/207/350/207/252/347/256/241/347/220/206/346/234/272/345/210/266_v1.0.md +0 -114
  574. package/docs/eval//345/217/221/345/270/20314_/350/272/253/344/275/223/303/227/350/204/221/347/273/204/345/220/210_v1.0.md +0 -160
  575. package/docs/eval//345/217/221/345/270/20315_/344/274/232/350/257/235/351/232/224/347/246/273/344/277/256/345/244/215/344/270/216/350/272/253/344/275/223/345/272/223/345/261/225/347/244/272_v1.0.md +0 -88
  576. 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 +0 -174
  577. package/docs/eval//345/275/222/344/270/200/345/261/202/347/274/272/347/234/201/347/277/273/345/205/263_/344/277/256/345/244/215/350/256/260/345/275/225_v1.0.md +0 -458
  578. package/docs/eval//346/200/247/350/203/275/344/270/223/351/241/271_/345/206/267/346/237/245/350/257/242/344/270/216/345/206/205/345/255/230_v1.0.md +0 -218
  579. package/docs/eval//346/225/205/351/232/234/346/263/250/345/205/245/345/256/236/346/265/213_v1.0.md +0 -432
  580. 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 +0 -223
  581. package/docs/eval//347/224/250/346/210/267/350/243/201/345/206/263/345/244/204/347/275/256_/347/254/254/344/270/200/346/211/271_v1.0.md +0 -195
  582. package/docs/eval//347/224/250/346/210/267/350/243/201/345/206/263/345/244/204/347/275/256_/347/254/254/344/272/214/346/211/271B6_/346/234/254/346/234/272/350/267/257/345/276/204/347/233/270/345/257/271/345/214/226_v1.0.md +0 -78
  583. package/docs/eval//347/253/257/345/210/260/347/253/257LoCoMoQA/345/220/214/345/217/243/345/276/204/345/257/271/347/205/247_v1.0.md +0 -100
  584. package/docs/eval//347/253/257/345/210/260/347/253/257/345/271/262/346/211/260/346/261/240/350/257/204/346/265/213_/347/241/256/345/256/232/346/200/247/350/243/201/345/206/263vsLLM_judge_v1.1.md +0 -197
  585. package/docs/eval//347/254/2543/345/261/202stg/347/273/223/346/236/204/347/264/242/345/274/225_/345/256/236/346/226/275/350/256/260/345/275/225_v1.0.md +0 -250
  586. package/docs/eval//347/254/254/344/270/211/346/226/271/351/252/214/350/257/201/346/212/245/345/221/212_LoCoMo_/347/201/265/346/236/242_.png +0 -0
  587. package/docs/eval//347/254/254/344/270/211/346/226/271/351/252/214/350/257/201/346/212/245/345/221/212_/347/201/265/346/236/242_vs_dejavu_/347/273/237/344/270/200/350/257/204/345/210/206_v7.md +0 -202
  588. package/docs/eval//347/274/226/347/240/201/351/235/242/345/211/215/347/275/256_/345/205/245/345/217/243/350/207/252/344/277/235/350/257/201UTF8/344/270/216/346/226/207/346/234/254open/345/256/210/345/215/253_v1.0.md +0 -646
  589. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v1.md +0 -156
  590. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v10.md +0 -210
  591. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v11.md +0 -227
  592. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v12.md +0 -203
  593. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v13.md +0 -233
  594. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v14.md +0 -191
  595. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v15.md +0 -213
  596. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v16.md +0 -214
  597. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v17.md +0 -199
  598. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v18.md +0 -183
  599. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v19.md +0 -207
  600. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v2.md +0 -156
  601. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v20.md +0 -283
  602. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v21.md +0 -224
  603. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v22.md +0 -223
  604. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v23.md +0 -293
  605. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v24.md +0 -303
  606. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v25.md +0 -230
  607. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v26.md +0 -196
  608. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v27.md +0 -259
  609. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v28.md +0 -217
  610. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v29.md +0 -270
  611. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v3.md +0 -152
  612. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v30.md +0 -231
  613. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v31.md +0 -281
  614. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v4.md +0 -128
  615. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v5.md +0 -114
  616. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v6.md +0 -192
  617. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v7.md +0 -187
  618. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v8.md +0 -207
  619. package/docs/eval//347/274/272/351/231/267/346/214/226/346/216/230_/350/207/252/344/270/273/350/277/255/344/273/243_v9.md +0 -203
  620. package/docs/eval//350/257/255/344/271/211/346/227/266/347/251/272/345/233/276/350/241/245/345/205/250_P1_/346/212/275/345/217/226/345/231/250v4_/344/270/211/347/216/207/350/257/273/346/225/260_v1.0.md +0 -231
  621. package/docs/eval//351/262/270/345/250/230/344/270/203/347/261/273/347/212/266/346/200/201/350/277/275/350/270/252_v1.0.md +0 -176
  622. package/docs/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 -385
  623. package/docs/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 +0 -165
  624. 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 +0 -76
  625. package/docs/hive//345/244/232/347/253/257harness/351/200/232/344/277/241/345/245/221/347/272/246_v0.1.md +0 -42
  626. package/docs/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 +0 -236
  627. package/docs/hive//345/256/211/345/205/250/345/256/241/350/256/241/345/256/236/351/224/232_v0.1.md +0 -202
  628. 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 +0 -43
  629. 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 +0 -210
  630. 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 +0 -152
  631. 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 +0 -47
  632. 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 +0 -519
  633. 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 +0 -85
  634. 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 +0 -191
  635. 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 +0 -90
  636. package/docs/images/lingshu-moonlight-covenant-poster-preview.jpg +0 -0
  637. package/docs/images/lingshu-moonlight-covenant-poster.png +0 -0
  638. package/docs/mdcg/D_meta_/345/267/245/347/250/213/345/214/226/346/226/271/346/241/210_v0.2.md +0 -216
  639. package/docs/mdcg/README/350/257/246/347/273/206/347/211/210_v0.4.10.md +0 -746
  640. package/docs/mdcg/guardrail-charter.md +0 -164
  641. package/docs/mdcg/lingshu_tutorial.html +0 -14956
  642. package/docs/mdcg/memory-assessment.html +0 -923
  643. package/docs/mdcg/memory_score.html +0 -213
  644. package/docs/mdcg/memory_score.png +0 -0
  645. package/docs/mdcg/release_v0.3.0.md +0 -36
  646. package/docs/mdcg/release_v0.4.11.md +0 -49
  647. package/docs/mdcg/release_v0.4.5.md +0 -55
  648. package/docs/mdcg/tool_table_v0.3.0.md +0 -117
  649. package/docs/mdcg//344/270/226/347/225/214/346/250/241/345/236/213/345/212/237/350/203/275/347/253/257_/344/275/277/347/224/250/344/270/216/350/277/220/347/273/264_v1.0.md +0 -384
  650. package/docs/mdcg//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_/345/245/221/347/272/246_v0.1.md +0 -82
  651. 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 +0 -600
  652. 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 +0 -266
  653. 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 +0 -275
  654. package/docs/mdcg//345/217/221/345/270/203/351/227/250/347/246/201/351/223/276_v0.1.md +0 -134
  655. 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 +0 -130
  656. 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 +0 -278
  657. package/docs/mdcg//347/201/265/346/236/242MCP/345/267/245/345/205/267/346/200/273/350/241/250_v3.4.md +0 -164
  658. package/docs/mdcg//347/201/265/346/236/242_/350/207/252/346/210/221/345/261/202/345/256/232/344/271/211.md +0 -43
  659. package/docs/mdcg//347/201/265/346/236/242/344/270/211/345/261/202/346/213/206/345/210/206/350/247/204/345/210/222_v0.1.md +0 -181
  660. 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 +0 -177
  661. package/docs/mdcg//347/216/257/344/272/214_/347/231/275/347/256/261/345/241/253/345/205/205/346/265/201/346/260/264/347/272/277_v0.1.md +0 -31
  662. package/docs/mdcg//347/235/241/347/234/240/345/221/250/346/234/237_/350/277/220/347/273/264/345/211/215/346/217/220/344/270/216/347/273/264/346/212/244/346/214/207/345/215/227_v1.0.md +0 -337
  663. package/docs/mdcg//347/274/272/345/217/243/345/215/225_P0/346/224/266/345/217/243_v0.1.md +0 -270
  664. 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 +0 -491
  665. package/docs/mdcg//350/256/244/347/237/245/345/233/276_MD/347/233/256/345/275/225/346/226/271/346/241/210_v0.1.md +0 -552
  666. 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 +0 -340
  667. 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 +0 -624
  668. package/docs/mdcg//350/256/244/347/237/245/345/233/276/344/275/234/344/270/272/350/256/260/345/277/206/346/223/215/344/275/234/347/263/273/347/273/237_/350/257/204/344/274/260/344/270/216/350/267/257/347/272/277_v0.1.md +0 -94
  669. package/docs/mdcg//350/256/260/345/277/206/346/223/215/344/275/234/347/263/273/347/273/237_MdCGOS/344/270/216MCP/346/216/245/345/205/245_v0.1.md +0 -281
  670. package/docs/mdcg//350/267/250/347/253/257/351/252/214/350/257/201/344/270/216/345/220/214/346/255/245/345/215/217/350/256/256_v0.1.md +0 -93
  671. 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 +0 -176
  672. package/docs/plans/stg/346/235/241/344/273/266/345/214/226/344/270/216/347/273/223/346/236/204/347/264/242/345/274/225_/350/256/276/350/256/241_v0.1.md +0 -124
  673. package/docs/plans//345/205/250/344/270/255/346/226/207/347/274/226/347/240/201/344/270/216/350/234/202/345/267/242/344/273/273/345/212/241/346/240/207/350/257/206/345/245/221/347/272/246_v2.0.md +0 -220
  674. 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 +0 -81
  675. package/docs/plans//345/244/232/344/270/273/344/275/223/344/270/226/347/225/214/346/250/241/345/236/213_/345/257/271/351/275/220/350/257/204/344/274/260/344/270/216/350/220/275/345/234/260/350/256/276/350/256/241_v0.1.md +0 -141
  676. package/docs/plans//347/201/265/346/236/242/350/272/253/344/275/223/303/227/350/204/221_/344/270/226/347/225/214/346/250/241/345/236/213/345/257/271/346/216/245/350/256/276/350/256/241_v0.1.md +0 -119
  677. package/docs/plans//347/234/237/346/272/220/347/264/242/345/274/225_/351/200/232/347/224/250/346/234/272/345/210/266_v0.3.md +0 -227
  678. package/docs/plans//347/235/241/347/234/240/344/270/216/350/207/252/350/277/255/344/273/243_/345/212/237/350/203/275/344/274/230/345/214/226/350/256/276/350/256/241_v0.4.md +0 -547
  679. package/docs/plans//347/237/245/350/257/206/345/233/276/350/260/261/351/241/271/347/233/256_/344/272/244/346/216/245/346/226/207/346/241/243_20260914.md +0 -308
  680. package/docs/plans//350/234/202/345/267/242/346/250/241/345/236/213/345/257/206/351/222/245/351/205/215/347/275/256/351/235/242_v1.0.md +0 -385
  681. package/docs/plans//350/256/260/345/277/206/350/207/252/345/244/204/347/220/206/344/270/211/346/241/243/350/207/252/346/262/273_/350/256/276/350/256/241_v0.2.md +0 -207
  682. package/docs/plans//350/257/255/344/271/211/346/227/266/347/251/272/345/233/276/350/241/245/345/205/250_/344/270/226/347/225/214/346/250/241/345/236/213/345/212/237/350/203/275/347/253/257_/350/256/276/350/256/241_v0.1.md +0 -107
  683. package/docs/promo/bilibili-cover.jpg +0 -0
  684. package/docs/promo/promo-visual-1.png +0 -0
  685. package/docs/promo/promo-visual-2.png +0 -0
  686. package/docs/promo/promo-visual-3.png +0 -0
  687. package/docs/promo/promo-visual-4.png +0 -0
  688. package/docs/promo/promo-visual-5.png +0 -0
  689. package/docs/swarm//350/234/202/347/276/244/344/272/222/350/201/224_v0.1.md +0 -704
  690. 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 +0 -242
  691. package/docs/swarm//350/234/202/347/276/244/345/244/232/346/231/272/350/203/275/344/275/223_/344/275/277/347/224/250/346/212/245/345/221/212_20260913.md +0 -68
  692. package/docs/swarm//350/234/202/347/276/244/345/244/232/346/231/272/350/203/275/344/275/223_/345/212/237/350/203/275/350/257/264/346/230/216_v0.6.md +0 -130
  693. package/docs/swarm//350/234/202/347/276/244/347/233/262/345/214/272/346/240/207/350/256/260_v1.0.md +0 -89
  694. package/docs/theory//344/270/215/345/217/257/351/235/240/345/256/232/347/220/206/344/270/216/345/244/261/346/225/210/344/274/230/345/205/210/346/241/206/346/236/266_v0.3.md +0 -365
  695. package/docs/theory//344/277/241/346/201/257/345/267/256/344/270/272/344/273/200/344/271/210/345/277/205/347/204/266/345/255/230/345/234/250/344/270/224/350/207/252/347/204/266/346/211/251/345/244/247.md +0 -239
  696. 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 +0 -129
  697. 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 +0 -134
  698. package/docs/theory//346/231/272/350/203/275/347/232/204/345/205/254/347/220/206/345/214/226/345/237/272/347/237/263.md +0 -789
  699. package/docs/theory//346/231/272/350/203/275/347/232/204/350/256/244/347/237/245/350/277/207/347/250/213.md +0 -379
  700. package/docs/theory//346/231/272/350/203/275/350/256/2723.4.md +0 -5260
  701. 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 +0 -145
  702. 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 +0 -144
  703. 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 +0 -198
  704. package/docs/theory//347/231/275/347/256/261/346/231/272/350/203/275/346/230/257/344/273/200/344/271/210/357/274/237.md +0 -812
  705. package/docs/theory//347/231/275/347/256/261/346/231/272/350/203/275/347/263/273/345/210/227/302/267/347/254/254/344/272/224/347/257/207/357/274/232/350/256/251AI/347/234/237/346/255/243/350/243/205/344/270/212/350/256/260/345/277/206.md +0 -421
  706. 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 +0 -221
  707. 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 +0 -237
  708. package/docs//344/270/215/345/217/257/351/235/240/346/200/247/347/220/206/350/256/272_v0.1.md +0 -343
  709. 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 +0 -82
  710. package/docs//345/255/230/347/256/227/344/270/200/344/275/223/346/236/266/346/236/204/345/255/246/344/271/240_/345/244/226/351/203/250/347/220/206/350/256/272/345/257/271/347/205/247/344/270/216/350/267/257/347/272/277/344/272/244/346/216/245_20260916.md +0 -98
  711. package/docs//345/255/230/347/256/227/344/270/200/344/275/223/347/245/236/347/273/217/347/275/221/347/273/234/346/236/266/346/236/204_/346/200/273/347/272/262/344/270/216/344/272/244/346/216/245_20260916.md +0 -109
  712. package/docs//347/201/265/346/236/242/350/207/252/346/210/221/346/224/271/350/277/233/345/267/245/344/275/234/350/256/241/345/210/222_/345/244/226/351/203/250/347/240/224/347/251/266/347/263/273/345/210/227/345/220/270/346/224/266_v1_20260919.md +0 -286
  713. package/docs//347/231/275/347/256/261/345/214/226/347/272/262/351/242/206_/344/270/211/351/241/271/347/233/256/345/255/246/344/271/240/346/200/273/346/236/266/346/236/204/344/270/216Ghidra/344/272/244/346/216/245_20260916.md +0 -104
  714. package/docs//347/247/244_/350/256/260/345/277/206/347/263/273/347/273/237/350/257/204/346/265/213/350/247/204/350/214/203_v2.1.md +0 -225
  715. package/docs//350/256/260/345/277/206/347/263/273/347/273/237/345/215/263/347/231/275/347/256/261/345/214/226/346/231/272/350/203/275_/346/236/266/346/236/204/345/257/271/347/205/247_v0.2.md +0 -121
  716. package/docs//350/256/260/345/277/206/347/263/273/347/273/237/345/215/263/347/231/275/347/256/261/345/214/226/346/231/272/350/203/275_/346/236/266/346/236/204/345/257/271/347/205/247_v0.3.md +0 -117
  717. package/docs//350/256/260/345/277/206/350/257/204/345/256/241/347/263/273/347/273/237_/347/253/213/351/241/271/350/256/276/350/256/241/344/270/216/346/226/275/345/267/245/344/272/244/346/216/245_20260915.md +0 -183
package/md_cg/chain.py CHANGED
@@ -1,379 +1,573 @@
1
- # -*- coding: utf-8 -*-
2
- """关系链 / 因果链遍历:**因果链就是条件链**。
3
-
4
- 核心命题(AEIS 原始定义):
5
- causal = 条件依赖因果:A 是 B 成立/运行的条件(B 依赖 A 成立);
6
- 方向 = 依赖方向(基础 → 应用)。
7
- `dex_chain` 沿 causal 边正向展开,每步标注条件,**链 = 条件序列**。
8
-
9
- 所以「检索沿关系链走」与「回答『什么条件下会发生什么』」是同一件事:
10
- 一条 causal 链就是一条「前提 → … → 结论」的条件序列,链上的每一跳都带一个条件。
11
-
12
- 对齐 AEIS 的遍历参数(不自行发明):
13
- - 默认 `max_depth=5`(`aeis/core.py:648,789,2070` 三处一致)
14
- - 仅沿**出边**正向展开(依赖方向)
15
- - `visited` 剪枝、`max_nodes` 上限、无后继即收尾
16
- - 每步累积 `conf × edge.weight`(`aeis/prediction.py:147-210`、`wisdom/wisdom_book.py:875`)
17
- - 边类型传播权重 base:causal 0.85 / similar 0.75 / hierarchical 0.70 /
18
- sequential 0.60 / spatial 0.50(`激活引擎_理论稿_v0.1.md:14`)
19
- - 低可信关系降权不禁止(D-002 伪因果过滤门,`aeis/prediction.py:68-122`)
20
-
21
- 与 AEIS 的一处有意差异:`infer_causal_paths` 的排序键是 `(路径长度, -平均置信度)`
22
- (`aeis/core.py:789-821`,Occam 偏好短链);本模块默认按**累积强度**排序,因为检索
23
- 关心的是「哪条链最可信」,而非「哪条链最短」。需要对齐 AEIS 时传 `sort="length"`。
24
- """
25
- from __future__ import annotations
26
-
27
- import os
28
-
29
- # 边类型 → 传播权重 base(AEIS《激活引擎 v0.1》第 14 行)
30
- EDGE_WEIGHTS = {
31
- "causal": 0.85,
32
- "similar": 0.75,
33
- "hierarchical": 0.70,
34
- "part_of": 0.70,
35
- "parent_of": 0.70,
36
- "applies_to": 0.70,
37
- "sequential": 0.60,
38
- "spatial_contains": 0.55,
39
- "spatial_adjacent": 0.50,
40
- "spatial_connected": 0.50,
41
- "correlational": 0.45,
42
- "cyclic": 0.35,
43
- "opposite": 0.30,
44
- # 正文引用边(linkref,写入侧自动解析,2026-09-17):取弱权重——正文提及
45
- # ≠ 语义相似 ≠ 因果依赖,故与 DEFAULT_EDGE_WEIGHT 同值;显式登记的意义在
46
- # 「类型已知」(可审计、可按类型调参、可区分于未登记类型的兜底默认)。
47
- "reference": 0.50,
48
- }
49
- DEFAULT_EDGE_WEIGHT = 0.50
50
-
51
- CAUSAL_TYPES = ("causal",)
52
- # 检索默认沿「有语义方向」的关系走:因果 / 时序 / 条件适用
53
- # 注:reference **刻意不入**——正文提及不应进入「前提→结论」条件序列遍历,
54
- # 否则 causal 链会被「提到过」这类无向弱关联稀释(模式分离)。
55
- CHAIN_TYPES_DEFAULT = ("causal", "sequential", "applies_to")
56
-
57
- #: 边类型集可配(P3-causal,2026-10-01):逗号分隔,大小写不敏感。
58
- #: 动因(设计稿 §6.2 / 交接单)——**叙事/文档语料里没有 causal 边**:按章切出来
59
- #: 的 doc_ref 库里只有 `reference`(弱权重 0.5,上方注释明写刻意不入默认集)与
60
- #: 父章节结构边(`part_of`)。边类型集若写死,因果路在这类库上**恒空**,等于
61
- #: 没接。故做成 env 可配,缺省仍是 `CHAIN_TYPES_DEFAULT`(缺省行为一字不变)。
62
- #: 约束(不得违反):本处**只从既有 `EDGE_WEIGHTS` 里挑子集**——不新造边类型、
63
- #: 不写第二份权重表;未登记的形态一律剔除(剔除即回落缺省,而不是按
64
- #: `DEFAULT_EDGE_WEIGHT` 静默走一条没人登记过的边)。
65
- #:
66
- #: **可配面边界(P3 遗留 ④ 如实降级声明,2026-10-01)**:契约 S3 的不变量原文是
67
- #: 「边方向/关系类型权重可配」,实测只有前一半做成了可配,后一半**不可配**:
68
- #: · **类型集**:可配(本 env);
69
- #: · **权重**:沿用**唯一表** `EDGE_WEIGHTS`(仓内「真源唯一」纪律——禁止
70
- #: 第二份边权表;要调权重改表,不引入第二处口径);
71
- #: · **方向**:固定为**依赖方向(出边)**(`walk(direction="out")` 为缺省,
72
- #: `adjacency` 建的是出邻接;`direction="in"` 存在但不在检索路上用)。
73
- #: ⇒ 对 S3 ④ 属**部分满足**,不得表述为「全满足」。
74
- CHAIN_TYPES_ENV = "MDCG_CHAIN_TYPES"
75
-
76
- MAX_DEPTH_DEFAULT = 5
77
- MAX_DEPTH_HARD = 64
78
- MAX_NODES_DEFAULT = 500
79
-
80
-
81
- # 生效条件:environ(缺省 os.environ)里 CHAIN_TYPES_ENV 为真值时按逗号切分、逐项 strip().lower()、只保留 EDGE_WEIGHTS 里登记过的形态;结果为空(未设 / 全是不认识的名字 / 只有空项)时回落 CHAIN_TYPES_DEFAULT。返回小写元组(顺序 = env 声明序)。
82
- def chain_types_from_env(environ=None):
83
- """关系链边类型集的**唯一读取点**(检索 chain 路与显式调用方共用)。
84
-
85
- 形态与 `MDCG_SPREAD_DECAY` 同款:env 缺失 / 非法 → 回落缺省,不抛异常。
86
- """
87
- raw = (environ or os.environ).get(CHAIN_TYPES_ENV)
88
- if not raw:
89
- return CHAIN_TYPES_DEFAULT
90
- kept = tuple(t for t in (str(x).strip().lower() for x in str(raw).split(","))
91
- if t in EDGE_WEIGHTS)
92
- return kept or CHAIN_TYPES_DEFAULT
93
-
94
-
95
- # 生效条件:edge 为 dict 时按 relation_type→relation→type 顺序取首个真值、非 dict 时 rel 记为 None,两者统一返回 str(rel or "").strip().lower()——键缺失或全为假值时得空串。
96
- def edge_rel(edge):
97
- """取边的 relation_type(兼容 dict / 字符串两种写法),统一小写。"""
98
- if isinstance(edge, dict):
99
- rel = edge.get("relation_type") or edge.get("relation") or edge.get("type")
100
- else:
101
- rel = None
102
- return str(rel or "").strip().lower()
103
-
104
-
105
- # 生效条件:edge 为 dict 时取 target or target_id(前者假值回落后者),该值非 None 则返回其 str().strip()、为 None 返回 None;edge is None 返回 None;其余(裸字符串等)返回 str(edge).strip() 或空白串时 None。
106
- def edge_target(edge):
107
- """取边的目标节点 id(兼容 target / target_id / 裸字符串)。"""
108
- if isinstance(edge, dict):
109
- t = edge.get("target") or edge.get("target_id")
110
- return str(t).strip() if t is not None else None
111
- if edge is None:
112
- return None
113
- return str(edge).strip() or None
114
-
115
-
116
- # 生效条件:base 取 EDGE_WEIGHTS.get(edge_rel(edge), DEFAULT_EDGE_WEIGHT);edge 为 dict 时 conf 为 float(edge.get("confidence", 1.0))(缺该键得 1.0,值不可转 float 如 None 触发 TypeError/ValueError 时也回落 1.0),非 dict 时 conf 恒为 1.0,返回 round(base * max(0.0, min(1.0, conf)), 6);
117
- def edge_weight(edge):
118
- """边权重 = 类型 base × 边置信度(缺失置信度视为 1.0 的已声明边)。"""
119
- base = EDGE_WEIGHTS.get(edge_rel(edge), DEFAULT_EDGE_WEIGHT)
120
- conf = 1.0
121
- if isinstance(edge, dict):
122
- try:
123
- conf = float(edge.get("confidence", 1.0))
124
- except (TypeError, ValueError):
125
- conf = 1.0
126
- return round(base * max(0.0, min(1.0, conf)), 6)
127
-
128
-
129
- # 生效条件:edge 非 dict 时返回 "";edge 为 dict 时按 ("condition","conditions","条件") 顺序取第一个真值(list/tuple 先以 ";" 连接其中 str(x).strip() 非空的项,连接结果为空则视为假值跳过该键)并返回 str(v).strip();三键均无真值且 edge.get("condition_space") 为 dict 时返回 condition_space_text(cs, require_full=False);否则返回 "";
130
- def edge_condition(edge):
131
- """边的条件标注:条件链上「这一跳在什么条件下成立」。"""
132
- if not isinstance(edge, dict):
133
- return ""
134
- for key in ("condition", "conditions", "条件"):
135
- v = edge.get(key)
136
- if isinstance(v, (list, tuple)):
137
- v = ";".join(str(x) for x in v if str(x).strip())
138
- if v:
139
- return str(v).strip()
140
- cs = edge.get("condition_space")
141
- if isinstance(cs, dict):
142
- # 与生效条件**共用同一个**合成入口(缺失槽不写)——杜绝第二套拼法:
143
- # 旧版在此拼 "k=v;k=v",与 nodefile 的声明口径各说各话。
144
- from .nodefile import condition_space_text # 懒导入,避免循环依赖
145
- return condition_space_text(cs, require_full=False)
146
- return ""
147
-
148
-
149
- # 生效条件:cg.get(nid) 为假值(该节点缺失或为空)时返回 [];否则以 node.get("frontmatter") or {} 与 node.get("content") or "" 调 _declared_conditions,返回其正向条件列表 pos;
150
- def node_conditions(cg, nid):
151
- """节点自己声明的生效条件(CCG `# 生效条件:` 等三处来源合并)。"""
152
- node = cg.get(nid)
153
- if not node:
154
- return []
155
- from .mdcos import _declared_conditions # 懒导入,避免模块级循环依赖
156
- pos, _neg = _declared_conditions(node.get("frontmatter") or {},
157
- node.get("content") or "")
158
- return pos
159
-
160
-
161
- # 生效条件:cg 已有 _chain_adj 且其 [0] 等于 bool(include_hierarchy)、[1] 等于可见性闸存在位、[2] 等于 bool(skip_archived) 时直接返回缓存的 [3];否则以 cg.index["nodes"](无 index 或无该键时视为无节点)逐节点收集 frontmatter.edges 中 edge_target 非空的出边(skip_archived=False 时该轮退役过滤整体旁路——维护面全量口径),include_hierarchy 为真时再为 subgraph.nodes 各合成一条 relation_type="part_of"、confidence=1.0 的层级边,仅对有出边的 nid 建表,写回 cg._chain_adj=(bool(include_hierarchy), 闸存在位, bool(skip_archived), adj) 后返回 adj;skip_archived=True(缺省)时 lifecycle.is_archived 为真的节点出边整体不入表、指向退役目标的边(含层级合成边)截断(判据单点 lifecycle.is_archived,缺键=active fail-open;退役不删除、可显式恢复);cg 提供 _chain_visible(nid) 谓词(读隔离,MdCGSecure 注入)时不可见 nid 的出边整体不入表、指向不可见目标的边(含层级合成边)截断——不可见节点 id、其边条件与下游拓扑对调用方不存在(walk/explain/causal_path/expand_from_seeds 全部消费者同闸);
162
- def adjacency(cg, include_hierarchy=True, skip_archived=True):
163
- """出邻接表:nid → [(target_id, edge_dict)]。
164
-
165
- 来源两处:
166
- 1. 节点 frontmatter.edges(关系边,含 relation_type)
167
- 2. 节点 frontmatter.subgraph.nodes(层级边,合成 part_of)
168
-
169
- **代价口径(P3 遗留 ⑥ 如实降级声明,2026-10-01)**:本函数**不读正文**
170
- (`subgraph._fm` 优先走索引快照,条目带 `subgraph` 键即零 IO),但其构建是
171
- **O(N) 索引条目级**——`for nid in nodes` 遍历的是**全部索引节点**
172
- (`cg.index["nodes"]`),不是「只碰种子邻域」。故契约 S3「不走全表」这一句
173
- 对**本函数不成立**,正确表述是「不读正文;邻接构建为 O(N) 索引条目级」。
174
- 代价读数(`scripts/p2p4_probe.py` R7,400 条目确定性小库):冷建 ~0.24ms、
175
- 缓存命中 ~1µs;缓存键 `(include_hierarchy, 闸存在位, skip_archived)` 存于
176
- `cg._chain_adj`,写入/删除后由 `invalidate_cache` 作废。
177
- (可选硬化路径 (b1):在索引快照的**边集合**上增量构建邻接——本轮未做,
178
- 如需做见 P3 遗留 B 的二选一。)
179
-
180
- 只读索引快照,不读文件正文;结果缓存在 `cg._chain_adj`。
181
-
182
- 读隔离(2026-09-25):cg._chain_visible(nid) 谓词在位(MdCGSecure 注入,
183
- = 索引有条目 ∧ _readable)时按其过滤——不可见节点不入表、指向不可见
184
- 目标的边截断;谓词缺席(基类)行为零变化。缓存键含闸存在位;闸语义
185
- 实例内稳定(_readable 绑定档判定恒用 principal.session,见 mdcos
186
- _readable),同一实例不会跨身份串台。
187
-
188
- 退役纪律(《秤》v2.1 §5.2;2026-10-06 接线):`skip_archived=True`(缺省)时
189
- archived 节点出边整体不入表、指向 archived 目标的边截断——`cg(op=causal,
190
- action=chain)` 等**读/预测面**(chain()/predict.*)由此不再扩散进退役节点;
191
- **维护面显式旁路**:`scrub`(去污染抽查,按度数排序的维护写面)传
192
- `skip_archived=False` 取全量表——退役节点仍可被抽查(判据面 = 默认读/注入
193
- 面必消费、维护/管理面直读,见运维文档 §九.5 面的判据)。判据单点
194
- `lifecycle.is_archived`(缺键=active fail-open)。
195
- """
196
- cached = getattr(cg, "_chain_adj", None)
197
- vis = getattr(cg, "_chain_visible", None)
198
- vis_on = vis is not None
199
- if (cached is not None and cached[0] == bool(include_hierarchy)
200
- and cached[1] == vis_on
201
- and cached[2] == bool(skip_archived)):
202
- return cached[3]
203
- from . import subgraph as _sg
204
- from . import lifecycle as _lc
205
- nodes = ((getattr(cg, "index", None) or {}).get("nodes") or {})
206
- adj = {}
207
- for nid in list(nodes):
208
- if skip_archived and _lc.is_archived(nodes.get(nid)):
209
- continue # 退役节点:其出边与条件整体不入表(不解析 fm)
210
- if vis is not None and not vis(nid):
211
- continue # 不可见节点:其出边与条件整体不可见(不解析 fm)
212
- fm = _sg._fm(cg, nid)
213
- out = []
214
- for e in (fm.get("edges") or []):
215
- tgt = edge_target(e)
216
- if tgt:
217
- if skip_archived and _lc.is_archived(nodes.get(tgt)):
218
- continue # 拓扑边界:指向退役目标的边径直截断
219
- if vis is not None and not vis(tgt):
220
- continue # 拓扑边界:指向不可见目标的边对调用方不存在
221
- out.append((tgt, e if isinstance(e, dict) else {"target": tgt}))
222
- if include_hierarchy:
223
- for ch in _sg.declared(fm)["nodes"]:
224
- if skip_archived and _lc.is_archived(nodes.get(ch)):
225
- continue
226
- if vis is not None and not vis(ch):
227
- continue
228
- out.append((ch, {"target": ch, "relation_type": "part_of",
229
- "confidence": 1.0, "verified": 0}))
230
- if out:
231
- adj[nid] = out
232
- try:
233
- cg._chain_adj = (bool(include_hierarchy), vis_on, bool(skip_archived), adj)
234
- except Exception:
235
- pass
236
- return adj
237
-
238
-
239
- # 生效条件:调用即把 cg._chain_adj 置为 None(赋值抛异常时静默忽略),无返回值;
240
- def invalidate_cache(cg):
241
- """写入/删除节点后丢弃邻接缓存(与 subgraph.invalidate_cache 成对调用)。"""
242
- try:
243
- cg._chain_adj = None
244
- except Exception:
245
- pass
246
-
247
-
248
- # 生效条件:对 adj 的每个 src→[(tgt, e)] 逐条把 (src, e) 追加到 rev[tgt](同 tgt 多次追加保持出现顺序),adj 为空字典时 rev 为空字典并返回;
249
- def _reverse(adj):
250
- rev = {}
251
- for src, outs in adj.items():
252
- for tgt, e in outs:
253
- rev.setdefault(tgt, []).append((src, e))
254
- return rev
255
-
256
-
257
- # 生效条件:start_id 起步迭代 DFS——max_depth 非 None 时先钳为 max(0, min(int(max_depth), MAX_DEPTH_HARD)),rels 为 (relation_types or ()) 的小写元组(None 或空则不作类型过滤),direction=="in" 时邻接表改用 _reverse(adjacency(cg, include_hierarchy)),每跳要求 rel∈rels(rels 非空时)、tgt 不在 seen、weight*edge_weight(e) ≥ min_weight,链在 max_depth 为 None 或 len(hops+[hop]) ≤ max_depth 时收入、在 max_depth 为 None 或 len(hops+[hop]) < max_depth 时继续下探,首跳且边无显式条件时条件回退 start_id 的 node_conditions,扩展节点数受 max_nodes、收链数受 max_chains 限制,sort=="length" 时按 (depth, -avg_weight) 排序否则按 (-weight, -depth) 排序,返回 chains[:max_chains];
258
- def walk(cg, start_id, relation_types=CAUSAL_TYPES, max_depth=MAX_DEPTH_DEFAULT,
259
- direction="out", max_nodes=MAX_NODES_DEFAULT, min_weight=0.0,
260
- max_chains=200, include_hierarchy=True, sort="strength"):
261
- """从 start_id 沿关系链展开,返回链列表(每条链 = 一段条件序列)。
262
-
263
- 返回的每条链:
264
- {"start","nodes","hops","conditions","weight","depth","avg_weight"}
265
- hops[i] = {"from","to","relation_type","weight","condition"}
266
- conditions[i] = 第 i 跳的条件(边条件优先,回退起点节点声明的生效条件)
267
- """
268
- if max_depth is not None:
269
- max_depth = max(0, min(int(max_depth), MAX_DEPTH_HARD))
270
- rels = tuple(str(r).lower() for r in (relation_types or ()))
271
- adj = adjacency(cg, include_hierarchy=include_hierarchy)
272
- if direction == "in":
273
- adj = _reverse(adj)
274
-
275
- chains = []
276
- # 迭代 DFS:栈元素 = (当前节点, 已访问集合, hops, weight)
277
- stack = [(start_id, frozenset([start_id]), [], 1.0)]
278
- visited_nodes = 0
279
- while stack and len(chains) < max_chains and visited_nodes < max_nodes:
280
- cur, seen, hops, weight = stack.pop()
281
- visited_nodes += 1
282
- outs = adj.get(cur) or []
283
- extended = False
284
- for tgt, e in outs:
285
- rel = edge_rel(e)
286
- if rels and rel not in rels:
287
- continue
288
- if tgt in seen:
289
- continue
290
- w = edge_weight(e)
291
- nw = weight * w
292
- if nw < min_weight:
293
- continue
294
- cond = edge_condition(e)
295
- if not cond and not hops: # 首跳无显式条件 → 用起点节点声明的条件
296
- conds = node_conditions(cg, start_id)
297
- cond = ";".join(conds) if conds else ""
298
- hop = {"from": cur, "to": tgt, "relation_type": rel,
299
- "weight": w, "condition": cond}
300
- nhop = hops + [hop]
301
- if max_depth is None or len(nhop) <= max_depth:
302
- chains.append({
303
- "start": start_id, "nodes": [start_id] + [h["to"] for h in nhop],
304
- "hops": nhop,
305
- "conditions": [h["condition"] for h in nhop],
306
- "weight": round(nw, 6), "depth": len(nhop),
307
- "avg_weight": round(nw ** (1.0 / len(nhop)), 6),
308
- })
309
- extended = True
310
- if max_depth is None or len(nhop) < max_depth:
311
- stack.append((tgt, seen | {tgt}, nhop, nw))
312
- if not extended and not hops:
313
- continue
314
- # 终键 tuple(nodes):图遍历序取决于邻接结构的枚举序,并列(同 depth/
315
- # weight/avg_weight)时若无终键,截断结果随索引构建路径漂移。
316
- if sort == "length": # 对齐 AEIS infer_causal_paths 的 Occam 偏好
317
- chains.sort(key=lambda c: (c["depth"], -c["avg_weight"],
318
- tuple(c["nodes"])))
319
- else:
320
- chains.sort(key=lambda c: (-c["weight"], -c["depth"],
321
- tuple(c["nodes"])))
322
- return chains[:max_chains]
323
-
324
-
325
- # 生效条件:kw 未含 relation_types 时先注入 CAUSAL_TYPES,再以 walk(cg, start_id, **kw) 的每条链逐跳渲染(跳条件为空串则显示「(未声明条件)」),返回 {"start": start_id, "count": 渲染链数, "chains": rendered};
326
- def explain(cg, start_id, **kw):
327
- """人类可读的链式解释:「什么条件下 → 发生什么」。"""
328
- kw.setdefault("relation_types", CAUSAL_TYPES)
329
- chains = walk(cg, start_id, **kw)
330
- rendered = []
331
- for c in chains:
332
- steps = []
333
- for h in c["hops"]:
334
- cond = h["condition"] or "(未声明条件)"
335
- steps.append({"条件": cond, "关系": h["relation_type"],
336
- "置信": h["weight"], "结果": h["to"]})
337
- rendered.append({"链": c["nodes"], "条件序列": c["conditions"],
338
- "累积置信": c["weight"], "跳数": c["depth"],
339
- "步骤": steps})
340
- return {"start": start_id, "count": len(rendered), "chains": rendered}
341
-
342
-
343
- # 生效条件:seeds 为 dict 时取其 items、否则取 list(seeds or [])(假值 seeds 得空列表,返回空 best),逐 (sid, s0) 跳过 sid 为假值或 float(s0) 抛 TypeError/ValueError 的项,对 walk(cg, sid, relation_types, max_depth, max_nodes, max_chains, include_hierarchy) 每条链取 nodes[-1] 为 nid(nid==sid 则跳过)并按 s0*链 weight*decay^depth 对每个 nid 只保留最高分,返回 best;
344
- def expand_from_seeds(cg, seeds, relation_types=CHAIN_TYPES_DEFAULT,
345
- max_depth=MAX_DEPTH_DEFAULT, decay=0.9,
346
- max_nodes=MAX_NODES_DEFAULT, max_chains=500,
347
- include_hierarchy=True):
348
- """检索用:从带分数的种子出发沿链扩散。
349
-
350
- `seeds`: `{node_id: score}` 或 `[(node_id, score)]`。
351
- 每个节点保留**最强**的一条链:
352
- score = 种子分 × 链累积权重 × decay^跳数
353
- 返回 `{nid: {"score","chain","depth","conditions"}}`;种子本身不在返回里。
354
- """
355
- if isinstance(seeds, dict):
356
- seed_items = list(seeds.items())
357
- else:
358
- seed_items = list(seeds or [])
359
- best = {}
360
- for sid, s0 in seed_items:
361
- if not sid:
362
- continue
363
- try:
364
- s0 = float(s0)
365
- except (TypeError, ValueError):
366
- continue
367
- for c in walk(cg, sid, relation_types=relation_types,
368
- max_depth=max_depth, max_nodes=max_nodes,
369
- max_chains=max_chains,
370
- include_hierarchy=include_hierarchy):
371
- nid = c["nodes"][-1]
372
- if nid == sid:
373
- continue
374
- sc = s0 * c["weight"] * (decay ** c["depth"])
375
- cur = best.get(nid)
376
- if cur is None or sc > cur["score"]:
377
- best[nid] = {"score": round(sc, 6), "chain": c,
378
- "depth": c["depth"], "conditions": c["conditions"]}
379
- return best
1
+ # -*- coding: utf-8 -*-
2
+ """关系链 / 因果链遍历:**因果链就是条件链**。
3
+
4
+ 核心命题(AEIS 原始定义):
5
+ causal = 条件依赖因果:A 是 B 成立/运行的条件(B 依赖 A 成立);
6
+ 方向 = 依赖方向(基础 → 应用)。
7
+ `dex_chain` 沿 causal 边正向展开,每步标注条件,**链 = 条件序列**。
8
+
9
+ 所以「检索沿关系链走」与「回答『什么条件下会发生什么』」是同一件事:
10
+ 一条 causal 链就是一条「前提 → … → 结论」的条件序列,链上的每一跳都带一个条件。
11
+
12
+ 对齐 AEIS 的遍历参数(不自行发明):
13
+ - 默认 `max_depth=5`(`aeis/core.py:648,789,2070` 三处一致)
14
+ - 仅沿**出边**正向展开(依赖方向)
15
+ - `visited` 剪枝、`max_nodes` 上限、无后继即收尾
16
+ - 每步累积 `conf × edge.weight`(`aeis/prediction.py:147-210`、`wisdom/wisdom_book.py:875`)
17
+ - 边类型传播权重 base:causal 0.85 / similar 0.75 / hierarchical 0.70 /
18
+ sequential 0.60 / spatial 0.50(`激活引擎_理论稿_v0.1.md:14`)
19
+ - 低可信关系降权不禁止(D-002 伪因果过滤门,`aeis/prediction.py:68-122`)
20
+
21
+ 与 AEIS 的一处有意差异:`infer_causal_paths` 的排序键是 `(路径长度, -平均置信度)`
22
+ (`aeis/core.py:789-821`,Occam 偏好短链);本模块默认按**累积强度**排序,因为检索
23
+ 关心的是「哪条链最可信」,而非「哪条链最短」。需要对齐 AEIS 时传 `sort="length"`。
24
+ """
25
+ from __future__ import annotations
26
+
27
+ import os
28
+ import heapq
29
+ import math
30
+
31
+ # 边类型 → 传播权重 base(AEIS《激活引擎 v0.1》第 14 行)
32
+ EDGE_WEIGHTS = {
33
+ "causal": 0.85,
34
+ "similar": 0.75,
35
+ "hierarchical": 0.70,
36
+ "part_of": 0.70,
37
+ "parent_of": 0.70,
38
+ "applies_to": 0.70,
39
+ "sequential": 0.60,
40
+ "spatial_contains": 0.55,
41
+ "spatial_adjacent": 0.50,
42
+ "spatial_connected": 0.50,
43
+ "correlational": 0.45,
44
+ "cyclic": 0.35,
45
+ "opposite": 0.30,
46
+ # 正文引用边(linkref,写入侧自动解析,2026-09-17):取弱权重——正文提及
47
+ # ≠ 语义相似 ≠ 因果依赖,故与 DEFAULT_EDGE_WEIGHT 同值;显式登记的意义在
48
+ # 「类型已知」(可审计、可按类型调参、可区分于未登记类型的兜底默认)。
49
+ "reference": 0.50,
50
+ }
51
+ DEFAULT_EDGE_WEIGHT = 0.50
52
+
53
+ CAUSAL_TYPES = ("causal",)
54
+ # 检索默认沿「有语义方向」的关系走:因果 / 时序 / 条件适用
55
+ # 注:reference **刻意不入**——正文提及不应进入「前提→结论」条件序列遍历,
56
+ # 否则 causal 链会被「提到过」这类无向弱关联稀释(模式分离)。
57
+ CHAIN_TYPES_DEFAULT = ("causal", "sequential", "applies_to")
58
+
59
+ #: 边类型集可配(P3-causal,2026-10-01):逗号分隔,大小写不敏感。
60
+ #: 动因(设计稿 §6.2 / 交接单)——**叙事/文档语料里没有 causal 边**:按章切出来
61
+ #: 的 doc_ref 库里只有 `reference`(弱权重 0.5,上方注释明写刻意不入默认集)与
62
+ #: 父章节结构边(`part_of`)。边类型集若写死,因果路在这类库上**恒空**,等于
63
+ #: 没接。故做成 env 可配,缺省仍是 `CHAIN_TYPES_DEFAULT`(缺省行为一字不变)。
64
+ #: 约束(不得违反):本处**只从既有 `EDGE_WEIGHTS` 里挑子集**——不新造边类型、
65
+ #: 不写第二份权重表;未登记的形态一律剔除(剔除即回落缺省,而不是按
66
+ #: `DEFAULT_EDGE_WEIGHT` 静默走一条没人登记过的边)。
67
+ #:
68
+ #: **可配面边界(P3 遗留 ④ 如实降级声明,2026-10-01)**:契约 S3 的不变量原文是
69
+ #: 「边方向/关系类型权重可配」,实测只有前一半做成了可配,后一半**不可配**:
70
+ #: · **类型集**:可配(本 env);
71
+ #: · **权重**:沿用**唯一表** `EDGE_WEIGHTS`(仓内「真源唯一」纪律——禁止
72
+ #: 第二份边权表;要调权重改表,不引入第二处口径);
73
+ #: · **方向**:固定为**依赖方向(出边)**(`walk(direction="out")` 为缺省,
74
+ #: `adjacency` 建的是出邻接;`direction="in"` 存在但不在检索路上用)。
75
+ #: ⇒ 对 S3 ④ 属**部分满足**,不得表述为「全满足」。
76
+ CHAIN_TYPES_ENV = "MDCG_CHAIN_TYPES"
77
+
78
+ MAX_DEPTH_DEFAULT = 5
79
+ MAX_DEPTH_HARD = 64
80
+ MAX_NODES_DEFAULT = 500
81
+
82
+
83
+ # 生效条件:environ(缺省 os.environ)里 CHAIN_TYPES_ENV 为真值时按逗号切分、逐项 strip().lower()、只保留 EDGE_WEIGHTS 里登记过的形态;结果为空(未设 / 全是不认识的名字 / 只有空项)时回落 CHAIN_TYPES_DEFAULT。返回小写元组(顺序 = env 声明序)。
84
+ def chain_types_from_env(environ=None):
85
+ """关系链边类型集的**唯一读取点**(检索 chain 路与显式调用方共用)。
86
+
87
+ 形态与 `MDCG_SPREAD_DECAY` 同款:env 缺失 / 非法 → 回落缺省,不抛异常。
88
+ """
89
+ raw = (environ or os.environ).get(CHAIN_TYPES_ENV)
90
+ if not raw:
91
+ return CHAIN_TYPES_DEFAULT
92
+ kept = tuple(t for t in (str(x).strip().lower() for x in str(raw).split(","))
93
+ if t in EDGE_WEIGHTS)
94
+ return kept or CHAIN_TYPES_DEFAULT
95
+
96
+
97
+ # 生效条件:edge 为 dict 时按 relation_type→relation→type 顺序取首个真值、非 dict 时 rel 记为 None,两者统一返回 str(rel or "").strip().lower()——键缺失或全为假值时得空串。
98
+ def edge_rel(edge):
99
+ """取边的 relation_type(兼容 dict / 字符串两种写法),统一小写。"""
100
+ if isinstance(edge, dict):
101
+ rel = edge.get("relation_type") or edge.get("relation") or edge.get("type")
102
+ else:
103
+ rel = None
104
+ return str(rel or "").strip().lower()
105
+
106
+
107
+ # 生效条件:edge 为 dict 时取 target or target_id(前者假值回落后者),该值非 None 则返回其 str().strip()、为 None 返回 None;edge is None 返回 None;其余(裸字符串等)返回 str(edge).strip() 或空白串时 None。
108
+ def edge_target(edge):
109
+ """取边的目标节点 id(兼容 target / target_id / 裸字符串)。"""
110
+ if isinstance(edge, dict):
111
+ t = edge.get("target") or edge.get("target_id")
112
+ return str(t).strip() if t is not None else None
113
+ if edge is None:
114
+ return None
115
+ return str(edge).strip() or None
116
+
117
+
118
+ # 生效条件:base 取 EDGE_WEIGHTS.get(edge_rel(edge), DEFAULT_EDGE_WEIGHT);edge 为 dict 时 conf 为 float(edge.get("confidence", 1.0))(缺该键得 1.0,值不可转 float 如 None 触发 TypeError/ValueError 时也回落 1.0),非 dict 时 conf 恒为 1.0,返回 round(base * max(0.0, min(1.0, conf)), 6);
119
+ def edge_weight(edge):
120
+ """边权重 = 类型 base × 边置信度(缺失置信度视为 1.0 的已声明边)。"""
121
+ base = EDGE_WEIGHTS.get(edge_rel(edge), DEFAULT_EDGE_WEIGHT)
122
+ conf = 1.0
123
+ if isinstance(edge, dict):
124
+ try:
125
+ conf = float(edge.get("confidence", 1.0))
126
+ except (TypeError, ValueError):
127
+ conf = 1.0
128
+ return round(base * max(0.0, min(1.0, conf)), 6)
129
+
130
+
131
+ # 生效条件:edge 非 dict 时返回 "";edge 为 dict 时按 ("condition","conditions","条件") 顺序取第一个真值(list/tuple 先以 ";" 连接其中 str(x).strip() 非空的项,连接结果为空则视为假值跳过该键)并返回 str(v).strip();三键均无真值且 edge.get("condition_space") 为 dict 时返回 condition_space_text(cs, require_full=False);否则返回 "";
132
+ def edge_condition(edge):
133
+ """边的条件标注:条件链上「这一跳在什么条件下成立」。"""
134
+ if not isinstance(edge, dict):
135
+ return ""
136
+ for key in ("condition", "conditions", "条件"):
137
+ v = edge.get(key)
138
+ if isinstance(v, (list, tuple)):
139
+ v = ";".join(str(x) for x in v if str(x).strip())
140
+ if v:
141
+ return str(v).strip()
142
+ cs = edge.get("condition_space")
143
+ if isinstance(cs, dict):
144
+ # 与生效条件**共用同一个**合成入口(缺失槽不写)——杜绝第二套拼法:
145
+ # 旧版在此拼 "k=v;k=v",与 nodefile 的声明口径各说各话。
146
+ from .nodefile import condition_space_text # 懒导入,避免循环依赖
147
+ return condition_space_text(cs, require_full=False)
148
+ return ""
149
+
150
+
151
+ # 生效条件:cg.get(nid) 为假值(该节点缺失或为空)时返回 [];否则以 node.get("frontmatter") or {} 与 node.get("content") or "" 调 _declared_conditions,返回其正向条件列表 pos;
152
+ def node_conditions(cg, nid):
153
+ """节点自己声明的生效条件(CCG `# 生效条件:` 等三处来源合并)。"""
154
+ node = cg.get(nid)
155
+ if not node:
156
+ return []
157
+ from .mdcos import _declared_conditions # 懒导入,避免模块级循环依赖
158
+ pos, _neg = _declared_conditions(node.get("frontmatter") or {},
159
+ node.get("content") or "")
160
+ return pos
161
+
162
+
163
+ # 生效条件:cg 已有 _chain_adj 且其 [0] 等于 bool(include_hierarchy)、[1] 等于可见性闸存在位、[2] 等于 bool(skip_archived) 时直接返回缓存的 [3];否则以 cg.index["nodes"](无 index 或无该键时视为无节点)逐节点收集 frontmatter.edges 中 edge_target 非空的出边(skip_archived=False 时该轮退役过滤整体旁路——维护面全量口径),include_hierarchy 为真时再为 subgraph.nodes 各合成一条 relation_type="part_of"、confidence=1.0 的层级边,仅对有出边的 nid 建表,写回 cg._chain_adj=(bool(include_hierarchy), 闸存在位, bool(skip_archived), adj) 后返回 adj;skip_archived=True(缺省)时 lifecycle.is_archived 为真的节点出边整体不入表、指向退役目标的边(含层级合成边)截断(判据单点 lifecycle.is_archived,缺键=active fail-open;退役不删除、可显式恢复);cg 提供 _chain_visible(nid) 谓词(读隔离,MdCGSecure 注入)时不可见 nid 的出边整体不入表、指向不可见目标的边(含层级合成边)截断——不可见节点 id、其边条件与下游拓扑对调用方不存在(walk/explain/causal_path/expand_from_seeds 全部消费者同闸);
164
+ def adjacency(cg, include_hierarchy=True, skip_archived=True):
165
+ """出邻接表:nid → [(target_id, edge_dict)]。
166
+
167
+ 来源两处:
168
+ 1. 节点 frontmatter.edges(关系边,含 relation_type)
169
+ 2. 节点 frontmatter.subgraph.nodes(层级边,合成 part_of)
170
+
171
+ **代价口径(P3 遗留 ⑥ 如实降级声明,2026-10-01)**:本函数**不读正文**
172
+ (`subgraph._fm` 优先走索引快照,条目带 `subgraph` 键即零 IO),但其构建是
173
+ **O(N) 索引条目级**——`for nid in nodes` 遍历的是**全部索引节点**
174
+ (`cg.index["nodes"]`),不是「只碰种子邻域」。故契约 S3「不走全表」这一句
175
+ 对**本函数不成立**,正确表述是「不读正文;邻接构建为 O(N) 索引条目级」。
176
+ 代价读数(`scripts/p2p4_probe.py` R7,400 条目确定性小库):冷建 ~0.24ms、
177
+ 缓存命中 ~1µs;缓存键 `(include_hierarchy, 闸存在位, skip_archived)` 存于
178
+ `cg._chain_adj`,写入/删除后由 `invalidate_cache` 作废。
179
+ (可选硬化路径 (b1):在索引快照的**边集合**上增量构建邻接——本轮未做,
180
+ 如需做见 P3 遗留 B 的二选一。)
181
+
182
+ 只读索引快照,不读文件正文;结果缓存在 `cg._chain_adj`。
183
+
184
+ 读隔离(2026-09-25):cg._chain_visible(nid) 谓词在位(MdCGSecure 注入,
185
+ = 索引有条目 ∧ _readable)时按其过滤——不可见节点不入表、指向不可见
186
+ 目标的边截断;谓词缺席(基类)行为零变化。缓存键含闸存在位;闸语义
187
+ 实例内稳定(_readable 绑定档判定恒用 principal.session,见 mdcos
188
+ _readable),同一实例不会跨身份串台。
189
+
190
+ 退役纪律(《秤》v2.1 §5.2;2026-10-06 接线):`skip_archived=True`(缺省)时
191
+ archived 节点出边整体不入表、指向 archived 目标的边截断——`cg(op=causal,
192
+ action=chain)` 等**读/预测面**(chain()/predict.*)由此不再扩散进退役节点;
193
+ **维护面显式旁路**:`scrub`(去污染抽查,按度数排序的维护写面)传
194
+ `skip_archived=False` 取全量表——退役节点仍可被抽查(判据面 = 默认读/注入
195
+ 面必消费、维护/管理面直读,见运维文档 §九.5 面的判据)。判据单点
196
+ `lifecycle.is_archived`(缺键=active fail-open)。
197
+ """
198
+ generation = getattr(cg, "_cache_generation", None)
199
+ generation = generation() if callable(generation) else None
200
+ cached = getattr(cg, "_chain_adj", None)
201
+ vis = getattr(cg, "_chain_visible", None)
202
+ vis_on = vis is not None
203
+ if (cached is not None and cached[0] == bool(include_hierarchy)
204
+ and cached[1] == vis_on
205
+ and cached[2] == bool(skip_archived)
206
+ and getattr(cg, "_chain_generation", None) == generation):
207
+ return cached[3]
208
+ from . import subgraph as _sg
209
+ from . import lifecycle as _lc
210
+ nodes = ((getattr(cg, "index", None) or {}).get("nodes") or {})
211
+ adj = {}
212
+ for nid in list(nodes):
213
+ # 2026-10-18:is_archived → is_retired(archived ∪ superseded)。
214
+ # 被取代的旧版本其出边与条件同样不入拓扑表,与检索面口径一致。
215
+ if skip_archived and _lc.is_retired(nodes.get(nid)):
216
+ continue # 退役节点:其出边与条件整体不入表(不解析 fm)
217
+ if vis is not None and not vis(nid):
218
+ continue # 不可见节点:其出边与条件整体不可见(不解析 fm)
219
+ fm = _sg._fm(cg, nid)
220
+ out = []
221
+ for e in (fm.get("edges") or []):
222
+ tgt = edge_target(e)
223
+ if tgt:
224
+ # #562:边目标侧与源侧(:215)同口径——按退役面剔除。
225
+ if skip_archived and _lc.is_retired(nodes.get(tgt)):
226
+ continue # 拓扑边界:指向退役目标的边径直截断
227
+ if vis is not None and not vis(tgt):
228
+ continue # 拓扑边界:指向不可见目标的边对调用方不存在
229
+ out.append((tgt, e if isinstance(e, dict) else {"target": tgt}))
230
+ if include_hierarchy:
231
+ for ch in _sg.declared(fm)["nodes"]:
232
+ if skip_archived and _lc.is_retired(nodes.get(ch)):
233
+ continue
234
+ if vis is not None and not vis(ch):
235
+ continue
236
+ out.append((ch, {"target": ch, "relation_type": "part_of",
237
+ "confidence": 1.0, "verified": 0}))
238
+ if out:
239
+ adj[nid] = out
240
+ try:
241
+ cg._chain_adj = (bool(include_hierarchy), vis_on, bool(skip_archived), adj)
242
+ cg._chain_generation = generation
243
+ except Exception:
244
+ pass
245
+ return adj
246
+
247
+
248
+ # 生效条件:allowed_ids 非 None 时只保留候选内的起点与终点;None 保留既有表。
249
+ def _scoped_adjacency(cg, include_hierarchy, allowed_ids=None):
250
+ """Induced candidate subgraph: hidden nodes cannot act as bridges.
251
+
252
+ A missing scope preserves direct causal/explanation API behavior. The
253
+ projected table is query-local and never replaces the full adjacency cache.
254
+ """
255
+ adj = adjacency(cg, include_hierarchy=include_hierarchy)
256
+ if allowed_ids is None:
257
+ return adj
258
+ return {src: [(dst, edge) for dst, edge in adj.get(src, ())
259
+ if dst in allowed_ids] for src in allowed_ids}
260
+
261
+
262
+ # 生效条件:调用即把 cg._chain_adj 置为 None(赋值抛异常时静默忽略),无返回值。
263
+ def invalidate_cache(cg):
264
+ """写入/删除节点后丢弃邻接缓存(与 subgraph.invalidate_cache 成对调用)。"""
265
+ try:
266
+ cg._chain_adj = None
267
+ except Exception:
268
+ pass
269
+
270
+
271
+ # 生效条件:对 adj 的每个 src→[(tgt, e)] 逐条把 (src, e) 追加到 rev[tgt](同 tgt 多次追加保持出现顺序),adj 为空字典时 rev 为空字典并返回;
272
+ def _reverse(adj):
273
+ rev = {}
274
+ for src, outs in adj.items():
275
+ for tgt, e in outs:
276
+ rev.setdefault(tgt, []).append((src, e))
277
+ return rev
278
+
279
+
280
+ # 生效条件:start_id 起步迭代 DFS——max_depth 非 None 时先钳为 max(0, min(int(max_depth), MAX_DEPTH_HARD)),rels 为 (relation_types or ()) 的小写元组(None 或空则不作类型过滤),direction=="in" 时邻接表改用 _reverse(adjacency(cg, include_hierarchy)),每跳要求 rel∈rels(rels 非空时)、tgt 不在 seen、weight*edge_weight(e) ≥ min_weight,链在 max_depth 为 None 或 len(hops+[hop]) ≤ max_depth 时收入、在 max_depth 为 None 或 len(hops+[hop]) < max_depth 时继续下探,首跳且边无显式条件时条件回退 start_id 的 node_conditions,扩展节点数受 max_nodes、收链数受 max_chains 限制,sort=="length" 时按 (depth, -avg_weight) 排序否则按 (-weight, -depth) 排序,返回 chains[:max_chains];
281
+ def walk(cg, start_id, relation_types=CAUSAL_TYPES, max_depth=MAX_DEPTH_DEFAULT,
282
+ direction="out", max_nodes=MAX_NODES_DEFAULT, min_weight=0.0,
283
+ max_chains=200, include_hierarchy=True, sort="strength", allowed_ids=None):
284
+ """从 start_id 沿关系链展开,返回链列表(每条链 = 一段条件序列)。
285
+
286
+ 返回的每条链:
287
+ {"start","nodes","hops","conditions","weight","depth","avg_weight"}
288
+ hops[i] = {"from","to","relation_type","weight","condition"}
289
+ conditions[i] = 第 i 跳的条件(边条件优先,回退起点节点声明的生效条件)
290
+ """
291
+ if max_depth is not None:
292
+ max_depth = max(0, min(int(max_depth), MAX_DEPTH_HARD))
293
+ rels = tuple(str(r).lower() for r in (relation_types or ()))
294
+ if allowed_ids is not None:
295
+ allowed_ids = frozenset(allowed_ids)
296
+ if start_id not in allowed_ids:
297
+ return []
298
+ adj = _scoped_adjacency(cg, include_hierarchy, allowed_ids)
299
+ if direction == "in":
300
+ adj = _reverse(adj)
301
+
302
+ chains = []
303
+ # 迭代 DFS:栈元素 = (当前节点, 已访问集合, hops, weight)
304
+ stack = [(start_id, frozenset([start_id]), [], 1.0)]
305
+ visited_nodes = 0
306
+ while stack and len(chains) < max_chains and visited_nodes < max_nodes:
307
+ cur, seen, hops, weight = stack.pop()
308
+ visited_nodes += 1
309
+ outs = adj.get(cur) or []
310
+ extended = False
311
+ for tgt, e in outs:
312
+ rel = edge_rel(e)
313
+ if rels and rel not in rels:
314
+ continue
315
+ if tgt in seen:
316
+ continue
317
+ w = edge_weight(e)
318
+ nw = weight * w
319
+ if nw < min_weight:
320
+ continue
321
+ cond = edge_condition(e)
322
+ if not cond and not hops: # 首跳无显式条件 → 用起点节点声明的条件
323
+ conds = node_conditions(cg, start_id)
324
+ cond = ";".join(conds) if conds else ""
325
+ hop = {"from": cur, "to": tgt, "relation_type": rel,
326
+ "weight": w, "condition": cond}
327
+ nhop = hops + [hop]
328
+ if max_depth is None or len(nhop) <= max_depth:
329
+ chains.append({
330
+ "start": start_id, "nodes": [start_id] + [h["to"] for h in nhop],
331
+ "hops": nhop,
332
+ "conditions": [h["condition"] for h in nhop],
333
+ "weight": round(nw, 6), "depth": len(nhop),
334
+ "avg_weight": round(nw ** (1.0 / len(nhop)), 6),
335
+ })
336
+ extended = True
337
+ if max_depth is None or len(nhop) < max_depth:
338
+ stack.append((tgt, seen | {tgt}, nhop, nw))
339
+ if not extended and not hops:
340
+ continue
341
+ # 终键 tuple(nodes):图遍历序取决于邻接结构的枚举序,并列(同 depth/
342
+ # weight/avg_weight)时若无终键,截断结果随索引构建路径漂移。
343
+ if sort == "length": # 对齐 AEIS infer_causal_paths 的 Occam 偏好
344
+ chains.sort(key=lambda c: (c["depth"], -c["avg_weight"],
345
+ tuple(c["nodes"])))
346
+ else:
347
+ chains.sort(key=lambda c: (-c["weight"], -c["depth"],
348
+ tuple(c["nodes"])))
349
+ return chains[:max_chains]
350
+
351
+
352
+ # 生效条件:kw 未含 relation_types 时先注入 CAUSAL_TYPES,再以 walk(cg, start_id, **kw) 的每条链逐跳渲染(跳条件为空串则显示「(未声明条件)」),返回 {"start": start_id, "count": 渲染链数, "chains": rendered};
353
+ def explain(cg, start_id, **kw):
354
+ """人类可读的链式解释:「什么条件下 → 发生什么」。"""
355
+ kw.setdefault("relation_types", CAUSAL_TYPES)
356
+ chains = walk(cg, start_id, **kw)
357
+ rendered = []
358
+ for c in chains:
359
+ steps = []
360
+ for h in c["hops"]:
361
+ cond = h["condition"] or "(未声明条件)"
362
+ steps.append({"条件": cond, "关系": h["relation_type"],
363
+ "置信": h["weight"], "结果": h["to"]})
364
+ rendered.append({"链": c["nodes"], "条件序列": c["conditions"],
365
+ "累积置信": c["weight"], "跳数": c["depth"],
366
+ "步骤": steps})
367
+ return {"start": start_id, "count": len(rendered), "chains": rendered}
368
+
369
+
370
+ # 生效条件:seeds 为 dict 时取其 items、否则取 list(seeds or [])(假值 seeds 得空列表,返回空 best),逐 (sid, s0) 跳过 sid 为假值或 float(s0) 抛 TypeError/ValueError 的项,对 walk(cg, sid, relation_types, max_depth, max_nodes, max_chains, include_hierarchy) 每条链取 nodes[-1] 为 nid(nid==sid 则跳过)并按 s0*链 weight*decay^depth 对每个 nid 只保留最高分,返回 best;
371
+ def _expand_legacy(cg, seeds, relation_types=CHAIN_TYPES_DEFAULT,
372
+ max_depth=MAX_DEPTH_DEFAULT, decay=0.9,
373
+ max_nodes=MAX_NODES_DEFAULT, max_chains=500,
374
+ include_hierarchy=True, allowed_ids=None):
375
+ """检索用:从带分数的种子出发沿链扩散。
376
+
377
+ `seeds`: `{node_id: score}` 或 `[(node_id, score)]`。
378
+ 每个节点保留**最强**的一条链:
379
+ score = 种子分 × 链累积权重 × decay^跳数
380
+ 返回 `{nid: {"score","chain","depth","conditions"}}`;种子本身不在返回里。
381
+ """
382
+ if isinstance(seeds, dict):
383
+ seed_items = list(seeds.items())
384
+ else:
385
+ seed_items = list(seeds or [])
386
+ best = {}
387
+ for sid, s0 in seed_items:
388
+ if not sid:
389
+ continue
390
+ try:
391
+ s0 = float(s0)
392
+ except (TypeError, ValueError):
393
+ continue
394
+ for c in walk(cg, sid, relation_types=relation_types,
395
+ max_depth=max_depth, max_nodes=max_nodes,
396
+ max_chains=max_chains,
397
+ include_hierarchy=include_hierarchy, allowed_ids=allowed_ids):
398
+ nid = c["nodes"][-1]
399
+ if nid == sid:
400
+ continue
401
+ sc = s0 * c["weight"] * (decay ** c["depth"])
402
+ cur = best.get(nid)
403
+ if cur is None or sc > cur["score"]:
404
+ best[nid] = {"score": round(sc, 6), "chain": c,
405
+ "depth": c["depth"], "conditions": c["conditions"]}
406
+ return best
407
+
408
+
409
+ # 生效条件:单调边权/衰减下逐种子优先展开,较浅且不弱的标签支配后续同节点标签;累计乘积不在中间舍入,资源预算只计实际弹出的有效标签。
410
+ def _best_seed(cg, adj, sid, s0, rels, depth_limit, decay, max_nodes,
411
+ max_chains, report):
412
+ """Depth/weight Pareto labels; a deeper strong prefix cannot erase a
413
+ shallower weak prefix which still has enough hops to reach a descendant.
414
+ Results remain bounded best effort when either existing budget runs out.
415
+ """
416
+ frontiers, queue, choices = {}, [], {}
417
+ serial = 0
418
+ seed_conditions = None
419
+
420
+ def offer(nid, weight, nodes, hops):
421
+ nonlocal serial
422
+ depth = len(hops)
423
+ frontier = frontiers.setdefault(nid, {})
424
+ for old in frontier.values():
425
+ if old[1] <= depth and old[2] >= weight:
426
+ if (old[1] < depth or old[2] > weight or old[3] <= nodes):
427
+ report["dominated"] += 1
428
+ return
429
+ for old_depth, old in list(frontier.items()):
430
+ if depth <= old_depth and weight >= old[2]:
431
+ if depth < old_depth or weight > old[2] or nodes < old[3]:
432
+ del frontier[old_depth]
433
+ label = (nid, depth, weight, nodes, hops)
434
+ frontier[depth] = label
435
+ # walk exposes a six-decimal cumulative edge weight. Round only when
436
+ # constructing its public chain; never round intermediate products.
437
+ # The half-unit upper envelope is safe for that same public score.
438
+ upper = s0 * min(1.0, weight + 0.0000005) * (decay ** depth)
439
+ heapq.heappush(queue, (-upper, depth, nodes, serial, label))
440
+ serial += 1
441
+ report["enqueued"] += 1
442
+ report["peak_queue"] = max(report["peak_queue"], len(queue))
443
+ if depth and nid != sid:
444
+ rounded_weight = round(weight, 6)
445
+ score = s0 * rounded_weight * (decay ** depth)
446
+ key = (-score, depth, -weight, nodes)
447
+ old = choices.get(nid)
448
+ if old is None or key < old[0]:
449
+ conditions = [h["condition"] for h in hops]
450
+ c = {"start": sid, "nodes": list(nodes), "hops": list(hops),
451
+ "conditions": conditions, "weight": rounded_weight,
452
+ "depth": depth,
453
+ "avg_weight": round(weight ** (1.0 / depth), 6)}
454
+ choices[nid] = (key, {"score": round(score, 6), "chain": c,
455
+ "depth": depth, "conditions": conditions})
456
+
457
+ offer(sid, 1.0, (sid,), ())
458
+ popped_chains = 0
459
+ while (queue and report["expanded"] < max_nodes
460
+ and popped_chains < max_chains):
461
+ _upper, _depth, _nodes, _serial, label = heapq.heappop(queue)
462
+ cur, depth, weight, nodes, hops = label
463
+ if frontiers.get(cur, {}).get(depth) is not label:
464
+ report["stale_pops"] += 1
465
+ continue
466
+ report["expanded"] += 1
467
+ if depth:
468
+ popped_chains += 1
469
+ if depth_limit is not None and depth >= depth_limit:
470
+ continue
471
+ for tgt, edge in adj.get(cur, ()):
472
+ report["edges_scanned"] += 1
473
+ rel = edge_rel(edge)
474
+ if (rels and rel not in rels) or tgt in nodes:
475
+ continue
476
+ w = edge_weight(edge)
477
+ condition = edge_condition(edge)
478
+ if not condition and not hops:
479
+ if seed_conditions is None:
480
+ seed_conditions = ";".join(node_conditions(cg, sid))
481
+ condition = seed_conditions
482
+ hop = {"from": cur, "to": tgt, "relation_type": rel,
483
+ "weight": w, "condition": condition}
484
+ offer(tgt, weight * w, nodes + (tgt,), hops + (hop,))
485
+ report["pending_labels"] = sum(
486
+ frontiers.get(label[0], {}).get(label[1]) is label
487
+ for *_prefix, label in queue)
488
+ report["complete"] = report["pending_labels"] == 0
489
+ report["output_truncated"] = len(choices) > max_chains
490
+ report["complete"] = report["complete"] and not report["output_truncated"]
491
+ # Keep the existing per-seed output budget. A root with many direct edges
492
+ # can discover more targets than it is allowed to return.
493
+ return dict(sorted(choices.items(), key=lambda item: item[1][0])[:max_chains])
494
+
495
+
496
+ # 生效条件:有限非负种子分、0<=decay<=1、非负整数资源预算与[0,1]边权表时,沿已有可见性/退役过滤邻接表作强度优先搜索,保留每节点深度/原始累积权重的 Pareto 标签;每起点预算沿用 max_nodes/max_chains,预算未耗尽时返回每目标最强简单链,耗尽时 stats.complete=False 并返回已发现的有界结果;越出单调性前提时保留旧 walk 路径,不改权重、上限或明细字段。
497
+ def expand_from_seeds(cg, seeds, relation_types=CHAIN_TYPES_DEFAULT,
498
+ max_depth=MAX_DEPTH_DEFAULT, decay=0.9,
499
+ max_nodes=MAX_NODES_DEFAULT, max_chains=500,
500
+ include_hierarchy=True, stats=None, allowed_ids=None):
501
+ """Retrieve strongest chains without enumerating redundant DFS paths.
502
+
503
+ Scores retain seed * round(edge_product, 6) * decay**depth. Among retained
504
+ candidates, ties prefer shorter depth, greater unrounded edge product,
505
+ then node-id sequence. Equal depth/weight labels use node-id order; a
506
+ stronger prefix can dominate a lexically earlier one (e.g. before a zero
507
+ edge), so provenance is not the lexically first of *all* tied paths.
508
+ A start is not
509
+ returned through a zero-hop self hit; another seed may reach it by edges.
510
+ walk/explain keep their existing all-chain enumeration contract.
511
+
512
+ stats, if supplied, reports per-seed work and whether the bounded search
513
+ completed. Budget exhaustion is not a global-optimality certificate.
514
+ Nonmonotone/custom numeric inputs use the original compatibility path.
515
+ allowed_ids, when supplied, restricts seeds and every intermediate node to
516
+ the induced candidate subgraph, including on the legacy fallback. None
517
+ preserves the unscoped direct API. Query projections are never cached.
518
+ """
519
+ seed_items = list(seeds.items()) if isinstance(seeds, dict) else list(seeds or [])
520
+ if allowed_ids is not None:
521
+ allowed_ids = frozenset(allowed_ids)
522
+ seed_items = [(nid, score) for nid, score in seed_items if nid in allowed_ids]
523
+ parsed = []
524
+ safe = (isinstance(decay, (int, float)) and math.isfinite(decay)
525
+ and 0 <= decay <= 1
526
+ and isinstance(max_nodes, int) and max_nodes >= 0
527
+ and isinstance(max_chains, int) and max_chains >= 0)
528
+ for sid, value in seed_items:
529
+ if not sid:
530
+ continue
531
+ try:
532
+ score = float(value)
533
+ except (TypeError, ValueError):
534
+ continue
535
+ safe = safe and math.isfinite(score) and score >= 0
536
+ safe = safe and isinstance(sid, str)
537
+ parsed.append((sid, score))
538
+ try:
539
+ safe = safe and all(math.isfinite(w) and 0 <= w <= 1
540
+ for w in (*EDGE_WEIGHTS.values(), DEFAULT_EDGE_WEIGHT))
541
+ depth_limit = (None if max_depth is None else
542
+ max(0, min(int(max_depth), MAX_DEPTH_HARD)))
543
+ except (TypeError, ValueError, OverflowError):
544
+ safe = False
545
+ depth_limit = None
546
+ report = {"algorithm": "depth_pareto_best_first" if safe else "legacy_fallback",
547
+ "complete": True if safe else None, "seeds": [],
548
+ "expanded": 0, "edges_scanned": 0, "dominated": 0}
549
+ if not safe:
550
+ best = _expand_legacy(cg, seed_items, relation_types, max_depth, decay,
551
+ max_nodes, max_chains, include_hierarchy, allowed_ids)
552
+ else:
553
+ best, choices = {}, {}
554
+ rels = tuple(str(r).lower() for r in (relation_types or ()))
555
+ adj = _scoped_adjacency(cg, include_hierarchy, allowed_ids) if parsed else {}
556
+ for sid, score in parsed:
557
+ seed_report = {"start": sid, "expanded": 0, "edges_scanned": 0,
558
+ "dominated": 0, "enqueued": 0, "stale_pops": 0,
559
+ "peak_queue": 0}
560
+ found = _best_seed(cg, adj, sid, score, rels, depth_limit, decay,
561
+ max_nodes, max_chains, seed_report)
562
+ for nid, choice in found.items():
563
+ if nid not in choices or choice[0] < choices[nid][0]:
564
+ choices[nid] = choice
565
+ report["seeds"].append(seed_report)
566
+ report["complete"] = report["complete"] and seed_report["complete"]
567
+ for key in ("expanded", "edges_scanned", "dominated"):
568
+ report[key] += seed_report[key]
569
+ best = {nid: value[1] for nid, value in choices.items()}
570
+ if stats is not None:
571
+ stats.clear()
572
+ stats.update(report)
573
+ return best