@furongjun1999/dsh-memory 0.8.1 → 0.9.6

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 (714) hide show
  1. package/README.md +662 -659
  2. package/codebuddy/README.md +1 -1
  3. package/data/policy.json +17 -6
  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 +1 -1
  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 +18 -3
  18. package/lib/hooks.js +147 -18
  19. package/lib/index.d.ts +2 -2
  20. package/lib/index.js +18 -4
  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 +13 -1
  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 +16 -1
  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 +8 -2
  34. package/lib/tools.js +22 -3
  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 +22 -5
  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 +572 -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 +21 -0
  71. package/md_cg/conformance_core.py +753 -0
  72. package/md_cg/consistency.py +1278 -955
  73. package/md_cg/consolidate.py +180 -20
  74. package/md_cg/crypto.py +794 -472
  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 +14 -3
  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 +86 -5
  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 +7104 -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 +4 -1
  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 +188 -28
  108. package/md_cg/protocol.py +492 -411
  109. package/md_cg/provenance.py +7 -1
  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 +12 -3
  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 +256 -19
  123. package/md_cg/rollback_cli.py +21 -1
  124. package/md_cg/rotate.py +347 -0
  125. package/md_cg/routing.py +62 -1
  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 +1 -1
  133. package/md_cg/semantic/export_en_zh_map.py +2 -2
  134. package/md_cg/semantic/zh_en_atoms.py +2 -0
  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 +273 -15
  139. package/md_cg/state_events.py +75 -10
  140. package/md_cg/state_slots.py +312 -221
  141. package/md_cg/stg.py +82 -16
  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 +43 -3
  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_coldverify_drain.py +127 -0
  172. package/md_cg/test_condaccept.py +156 -0
  173. package/md_cg/test_condgate.py +141 -0
  174. package/md_cg/test_config_registry.py +55 -0
  175. package/md_cg/test_crypto_rotate.py +257 -0
  176. package/md_cg/test_csre_correctness.py +297 -0
  177. package/md_cg/test_csre_index_import_guard.py +161 -0
  178. package/md_cg/test_csre_mutations.py +34 -0
  179. package/md_cg/test_csre_session.py +69 -0
  180. package/md_cg/test_curiosity_budget.py +468 -0
  181. package/md_cg/test_d17_autostart_toolface.py +453 -0
  182. package/md_cg/test_datapath_tilde_home.py +271 -0
  183. package/md_cg/test_derived_cache_generations.py +256 -0
  184. package/md_cg/test_evidence_delivery_compact.py +54 -0
  185. package/md_cg/test_exact_text_cache.py +191 -0
  186. package/md_cg/test_excerpt_budget.py +188 -0
  187. package/md_cg/test_experience_consolidation.py +112 -0
  188. package/md_cg/test_export_default_collision.py +138 -0
  189. package/md_cg/test_export_snapshots.py +322 -0
  190. package/md_cg/test_fuzzy_substring_equivalence.py +127 -0
  191. package/md_cg/test_gain_gate.py +16 -5
  192. package/md_cg/test_goal_gen.py +355 -0
  193. package/md_cg/test_h2_session_view_norm.py +251 -233
  194. package/md_cg/test_h4_sustain_snapshot.py +1490 -1387
  195. package/md_cg/test_heartbeat_ledger.py +557 -0
  196. package/md_cg/test_hierarchy_audit_graph.py +237 -0
  197. package/md_cg/test_i50c_meta_passthrough.py +628 -608
  198. package/md_cg/test_i50e_readside_protection.py +6 -4
  199. package/md_cg/test_identity_attribution.py +96 -2
  200. package/md_cg/test_identity_contract.py +329 -0
  201. package/md_cg/test_imgskill.py +1444 -0
  202. package/md_cg/test_index_snapshot_shape.py +240 -0
  203. package/md_cg/test_interop_judgment.py +258 -228
  204. package/md_cg/test_issue111_gate_order.py +77 -0
  205. package/md_cg/test_issue111_scoring_contract.py +87 -0
  206. package/md_cg/test_issue121_calibration_bounds.py +293 -0
  207. package/md_cg/test_issue128_session_env_authority.py +246 -0
  208. package/md_cg/test_issue130_rrf_rank_credibility.py +217 -0
  209. package/md_cg/test_issue131_postings_content_staleness.py +424 -0
  210. package/md_cg/test_issue145_sleep_no_silent_rollback.py +168 -0
  211. package/md_cg/test_issue146_frontmatter_quarantine.py +428 -0
  212. package/md_cg/test_issue147_pack_id_escape.py +378 -0
  213. package/md_cg/test_issue148_merge_downgrade_leak.py +192 -0
  214. package/md_cg/test_issue149_accel_default_face.py +249 -0
  215. package/md_cg/test_issue150_empty_query_terms.py +328 -0
  216. package/md_cg/test_issue151_153_157_write_cache_index.py +152 -0
  217. package/md_cg/test_issue151b_base_add_importance_guard.py +166 -0
  218. package/md_cg/test_issue152_sealed_postings.py +309 -0
  219. package/md_cg/test_issue152b_decrypt_cache.py +218 -0
  220. package/md_cg/test_issue153b_cache_alias_and_canon.py +236 -0
  221. package/md_cg/test_issue153b_index_generation_invalidation.py +143 -0
  222. package/md_cg/test_issue157b_index_remainder.py +344 -0
  223. package/md_cg/test_issue189_conclusion_bidirectional.py +223 -0
  224. package/md_cg/test_issue190_tidy_idempotent.py +806 -0
  225. package/md_cg/test_issue191_import_trust.py +382 -0
  226. package/md_cg/test_issue192_move_layer_indices_guard.py +258 -0
  227. package/md_cg/test_issue193_consistency_knowledge_guard.py +524 -0
  228. package/md_cg/test_issue193_write_scope.py +378 -0
  229. package/md_cg/test_issue197_rank_credibility_property.py +201 -0
  230. package/md_cg/test_issue211_export_target.py +325 -0
  231. package/md_cg/test_issue212_writeface_gaps.py +349 -0
  232. package/md_cg/test_issue214_reject_sensitivity.py +267 -0
  233. package/md_cg/test_issue215_condition_source_gap.py +272 -0
  234. package/md_cg/test_issue216_clash_routes.py +214 -0
  235. package/md_cg/test_issue217_slot_routes.py +248 -0
  236. package/md_cg/test_issue218_postcondition_value_blind.py +222 -0
  237. package/md_cg/test_issue225_consistency_torn_log.py +433 -0
  238. package/md_cg/test_issue245_swarm_traversal.py +366 -0
  239. package/md_cg/test_issue258_pack_sensitivity.py +468 -0
  240. package/md_cg/test_issue265_coldverify_multiproc.py +438 -0
  241. package/md_cg/test_issue274_read_node_id_none.py +341 -0
  242. package/md_cg/test_issue275_layer_param.py +250 -0
  243. package/md_cg/test_issue277_node_id_traversal.py +384 -0
  244. package/md_cg/test_issue278_neg_separated_tension.py +271 -0
  245. package/md_cg/test_issue278_neg_window.py +294 -0
  246. package/md_cg/test_issue278_negative_layering.py +83 -0
  247. package/md_cg/test_issue279_signers_path_guard.py +652 -0
  248. package/md_cg/test_issue284_skip_not_pass.py +291 -0
  249. package/md_cg/test_issue294_flywheel_sensitivity.py +573 -0
  250. package/md_cg/test_issue295_sleep_root_binding.py +589 -0
  251. package/md_cg/test_issue297_rewrite_sensitivity.py +454 -0
  252. package/md_cg/test_issue298_consistency_ledger_sensitivity.py +421 -0
  253. package/md_cg/test_issue301_baseline_corpus_gate.py +245 -0
  254. package/md_cg/test_issue303_design_boundaries.py +262 -0
  255. package/md_cg/test_issue313_writelimit_sensitivity.py +404 -0
  256. package/md_cg/test_issue315_merge_branch_sensitivity.py +384 -0
  257. package/md_cg/test_issue315_sensitivity_passthrough_guard.py +514 -0
  258. package/md_cg/test_issue319_tenant_unregistered_bypass.py +257 -0
  259. package/md_cg/test_issue320_vstate_requires_verify.py +279 -0
  260. package/md_cg/test_issue321_mcp_deep_json.py +305 -0
  261. package/md_cg/test_issue322_node_write_lock.py +727 -0
  262. package/md_cg/test_issue323_aggregate_segment_boundary.py +365 -0
  263. package/md_cg/test_issue324_write_size_fields.py +215 -0
  264. package/md_cg/test_issue325_audit_poison.py +230 -0
  265. package/md_cg/test_issue326_append_jsonl_oserror.py +464 -0
  266. package/md_cg/test_issue326_ingest_auto_whitelist.py +331 -0
  267. package/md_cg/test_issue326_key_perms.py +355 -0
  268. package/md_cg/test_issue326_twin_parser_parity.py +323 -0
  269. package/md_cg/test_issue326_vm_call_frame.py +569 -0
  270. package/md_cg/test_issue345_writelimit_effective_sens.py +234 -0
  271. package/md_cg/test_issue346_overwrite_sensitivity_gate.py +424 -0
  272. package/md_cg/test_issue346_review_overwrite_guard.py +509 -0
  273. package/md_cg/test_issue348_insight_verify_op.py +200 -0
  274. package/md_cg/test_issue348_merge_rmw_lock.py +345 -0
  275. package/md_cg/test_issue350_casefold_collision.py +236 -0
  276. package/md_cg/test_issue351_ingest_root_fallback.py +202 -0
  277. package/md_cg/test_issue352_imgaudit.py +418 -0
  278. package/md_cg/test_issue352_pathsjson.py +537 -0
  279. package/md_cg/test_issue352_refindex.py +412 -0
  280. package/md_cg/test_issue352_rollback.py +604 -0
  281. package/md_cg/test_issue352_vstateinherit.py +196 -0
  282. package/md_cg/test_issue354_p1_writelimit_type.py +522 -0
  283. package/md_cg/test_issue354_p2_datapath_clock.py +381 -0
  284. package/md_cg/test_issue354_p2_writelimit_toctou.py +327 -0
  285. package/md_cg/test_issue354_rust_index_containment.py +571 -0
  286. package/md_cg/test_issue354_rust_wal_containment.py +606 -0
  287. package/md_cg/test_issue358_truncation_observable.py +247 -0
  288. package/md_cg/test_issue360_tidy_private_excerpt.py +196 -0
  289. package/md_cg/test_issue361_blindspot_not_silent_drop.py +494 -0
  290. package/md_cg/test_issue362_neg_condition.py +468 -0
  291. package/md_cg/test_issue363_lexical_idf.py +275 -0
  292. package/md_cg/test_issue370_readcache_assembly.py +488 -0
  293. package/md_cg/test_issue375_state_slot_retire.py +325 -0
  294. package/md_cg/test_issue376_heartbeat_failed.py +293 -0
  295. package/md_cg/test_issue377_layer_protect_stamp.py +324 -0
  296. package/md_cg/test_issue378_protected_floor.py +310 -0
  297. package/md_cg/test_issue383_state_event_write_gate.py +462 -0
  298. package/md_cg/test_issue397_sens_eff_wiring.py +435 -0
  299. package/md_cg/test_issue398_toolface_op_parity.py +511 -0
  300. package/md_cg/test_issue399_hive_json_shape.py +317 -0
  301. package/md_cg/test_issue400_hive_dirty_args.py +450 -0
  302. package/md_cg/test_issue401_wisdom_codegen.py +295 -0
  303. package/md_cg/test_issue404_heal_receipt.py +390 -0
  304. package/md_cg/test_issue405_git_pathspec.py +333 -0
  305. package/md_cg/test_issue408_eface_registry.py +291 -0
  306. package/md_cg/test_issue409_compact_index_completeness.py +509 -0
  307. package/md_cg/test_issue410_verify_demote_write.py +366 -0
  308. package/md_cg/test_issue411_scrub_idempotence.py +575 -0
  309. package/md_cg/test_issue412_discipline_producer_parity.py +312 -0
  310. package/md_cg/test_issue413_preimage_failclosed.py +239 -0
  311. package/md_cg/test_issue413_preimage_twin.py +340 -0
  312. package/md_cg/test_issue414_write_args_parity.py +613 -0
  313. package/md_cg/test_issue415_deps_gate_probe.py +358 -0
  314. package/md_cg/test_issue416_entry_key_subset.py +525 -0
  315. package/md_cg/test_issue417_twophase_merge_landed.py +423 -0
  316. package/md_cg/test_issue418_identity_role_type_gate.py +188 -0
  317. package/md_cg/test_issue418_role_type_gate.py +265 -0
  318. package/md_cg/test_issue419_pii_redaction.py +642 -0
  319. package/md_cg/test_issue424_merge_confirm_sensitivity.py +381 -0
  320. package/md_cg/test_issue425_ingest_library_path.py +331 -0
  321. package/md_cg/test_issue426a_dedup_receipt.py +242 -0
  322. package/md_cg/test_issue426b_root_clash_invisible.py +270 -0
  323. package/md_cg/test_issue426c_enqueue_not_silent.py +346 -0
  324. package/md_cg/test_issue426e_restore_overwrite_live.py +310 -0
  325. package/md_cg/test_issue426f_paths_gbk_failclosed.py +301 -0
  326. package/md_cg/test_issue427_serve_status_shape.py +191 -0
  327. package/md_cg/test_issue431_432_468_protect_single_point.py +322 -0
  328. package/md_cg/test_issue433_outlook_threshold_single_point.py +278 -0
  329. package/md_cg/test_issue434_435_float_gate.py +90 -0
  330. package/md_cg/test_issue439_sibling_sensitivity_inherit.py +448 -0
  331. package/md_cg/test_issue43_default_policy.py +13 -4
  332. package/md_cg/test_issue443a_state_event_length_gate.py +94 -0
  333. package/md_cg/test_issue443b_audit_record_length_gate.py +83 -0
  334. package/md_cg/test_issue443c_self_state_imgskill_length_gate.py +77 -0
  335. package/md_cg/test_issue444_scrub_evidence_plaintext.py +281 -0
  336. package/md_cg/test_issue445_metacognition_ledger_gate.py +454 -0
  337. package/md_cg/test_issue446_453_403_verified_evidence.py +382 -0
  338. package/md_cg/test_issue446_is_encrypted_envelope.py +166 -0
  339. package/md_cg/test_issue447_recent_sens_failclosed.py +270 -0
  340. package/md_cg/test_issue450_sensitivity_fallback_reason.py +51 -0
  341. package/md_cg/test_issue450_writegate_unknown_sens.py +203 -0
  342. package/md_cg/test_issue451_audit_actor.py +313 -0
  343. package/md_cg/test_issue452_ingest_root_hint.py +238 -0
  344. package/md_cg/test_issue452_ingest_root_sensitivity.py +299 -0
  345. package/md_cg/test_issue463_ghost_prune_guard.py +259 -0
  346. package/md_cg/test_issue466_basis_registry.py +469 -0
  347. package/md_cg/test_issue467_identity_sensitivity.py +204 -0
  348. package/md_cg/test_issue467_identity_sensitivity_parity.py +361 -0
  349. package/md_cg/test_issue469_scrub_override_passthrough.py +126 -0
  350. package/md_cg/test_issue470_wal_event_durable.py +427 -0
  351. package/md_cg/test_issue471_identity_readable.py +137 -0
  352. package/md_cg/test_issue472_audit_chain.py +91 -0
  353. package/md_cg/test_issue472_prune_ledger.py +106 -0
  354. package/md_cg/test_issue472_rollback_degraded.py +82 -0
  355. package/md_cg/test_issue491_compose_sandbox.py +460 -0
  356. package/md_cg/test_issue492_dup_readout_opt_in.py +292 -0
  357. package/md_cg/test_issue498_merge_trace_stale_index.py +280 -0
  358. package/md_cg/test_issue511_scoreboard_bad_files.py +256 -0
  359. package/md_cg/test_issue521_rotate_lock_suffix.py +110 -0
  360. package/md_cg/test_issue522_review_record_sensitivity.py +267 -0
  361. package/md_cg/test_issue524_orphan_noise_no_demote.py +88 -0
  362. package/md_cg/test_issue525_read_null_semantics.py +52 -0
  363. package/md_cg/test_issue538_hook_condition_space.py +97 -0
  364. package/md_cg/test_issue540_path_guard_whitespace.py +126 -0
  365. package/md_cg/test_issue541_tidy_protect_gate.py +141 -0
  366. package/md_cg/test_issue542_tenant_root_same.py +153 -0
  367. package/md_cg/test_issue543_issue_authorization.py +147 -0
  368. package/md_cg/test_issue544_sandbox_reflection_gate.py +175 -0
  369. package/md_cg/test_issue545_charset_vs_size_diagnostic.py +77 -0
  370. package/md_cg/test_issue546_ledger_concurrency.py +81 -0
  371. package/md_cg/test_issue547_append_jsonl_whitelist.py +115 -0
  372. package/md_cg/test_issue547_audit_durable.py +96 -0
  373. package/md_cg/test_issue549_durable_append.py +143 -0
  374. package/md_cg/test_issue557_r1_credential_value_forms.py +76 -0
  375. package/md_cg/test_issue557_r2_write_entries_size_gate.py +84 -0
  376. package/md_cg/test_issue557_r3_pem_block_redaction.py +75 -0
  377. package/md_cg/test_issue559_refine_prefix.py +70 -0
  378. package/md_cg/test_issue561_pending_review.py +109 -0
  379. package/md_cg/test_issue66_scrub_semantics.py +107 -0
  380. package/md_cg/test_issue67_scrub_control_pool.py +233 -0
  381. package/md_cg/test_issue69_audit_boundary.py +129 -0
  382. package/md_cg/test_issue71_queue_backlog.py +377 -0
  383. package/md_cg/test_issue72_coldverify_controlled_drain.py +420 -0
  384. package/md_cg/test_issue73_doc_chapter_refs.py +326 -0
  385. package/md_cg/test_issue75_neural_failure.py +582 -0
  386. package/md_cg/test_issue78_export_concurrency.py +334 -0
  387. package/md_cg/test_issue80_residual_flush.py +169 -0
  388. package/md_cg/test_issue82_credential_rules.py +271 -0
  389. package/md_cg/test_issue82_layer_and_size.py +120 -0
  390. package/md_cg/test_issue84_portability.py +95 -0
  391. package/md_cg/test_issue85_export_overwrite.py +113 -0
  392. package/md_cg/test_issue86_runtime_revoke.py +110 -0
  393. package/md_cg/test_issue87_default_sensitivity_single.py +201 -0
  394. package/md_cg/test_issue88_conclusion_face.py +280 -0
  395. package/md_cg/test_issue88_execution_conflicts.py +87 -0
  396. package/md_cg/test_issue89_zero_score.py +169 -0
  397. package/md_cg/test_issue89a_score_cap_tiebreak.py +279 -0
  398. package/md_cg/test_issue91_verifier_sandbox.py +314 -0
  399. package/md_cg/test_issue91_verifier_timeout.py +528 -0
  400. package/md_cg/test_issue93_remember_redaction.py +282 -0
  401. package/md_cg/test_issue96_key_fallback.py +102 -0
  402. package/md_cg/test_issue98_tenant_root.py +104 -0
  403. package/md_cg/test_jsonl_commit_watermark.py +221 -0
  404. package/md_cg/test_live_validity_cache.py +256 -0
  405. package/md_cg/test_logref.py +32 -0
  406. package/md_cg/test_mcp_call_argument_types.py +99 -0
  407. package/md_cg/test_md_access_parity.py +80 -5
  408. package/md_cg/test_md_cg_no_hive_dep.py +140 -0
  409. package/md_cg/test_memory_rsi.py +215 -0
  410. package/md_cg/test_memory_rsi_adversarial.py +91 -0
  411. package/md_cg/test_mode_parity.py +1413 -1393
  412. package/md_cg/test_mutation_residue.py +120 -0
  413. package/md_cg/test_mutation_rollback.py +62 -32
  414. package/md_cg/test_n123_derive_expiry_chain.py +10 -5
  415. package/md_cg/test_n130_verify_falsified_protect.py +185 -185
  416. package/md_cg/test_n209_verify_write_face_gates.py +7 -2
  417. package/md_cg/test_n230_dirty_replay.py +52 -9
  418. package/md_cg/test_n62_tenant_bind_failclosed.py +3 -3
  419. package/md_cg/test_neg_coverage_budget.py +500 -0
  420. package/md_cg/test_neg_tail_honesty.py +79 -9
  421. package/md_cg/test_p0.py +16 -2
  422. package/md_cg/test_p14_root_fallback.py +373 -0
  423. package/md_cg/test_p1x_ref_root.py +47 -10
  424. package/md_cg/test_p21_tokens.py +2 -1
  425. package/md_cg/test_p24_evidence.py +232 -227
  426. package/md_cg/test_p26_refindex.py +20 -3
  427. package/md_cg/test_p27_docindex.py +16 -1
  428. package/md_cg/test_p28_refcheck.py +45 -24
  429. package/md_cg/test_p29_session_ingest_export.py +6 -0
  430. package/md_cg/test_p2_mcp.py +474 -453
  431. package/md_cg/test_p2_six_elements.py +14 -1
  432. package/md_cg/test_p3.py +370 -261
  433. package/md_cg/test_p33_ccg_wiring.py +9 -2
  434. package/md_cg/test_p3_legacy_closure.py +6 -1
  435. package/md_cg/test_p40_refine_worklist.py +5 -2
  436. package/md_cg/test_p47_session_view.py +25 -9
  437. package/md_cg/test_p4_freshness.py +8 -1
  438. package/md_cg/test_p83_jsonrpc_parse_error.py +124 -0
  439. package/md_cg/test_p9c_dedup_hints.py +7 -2
  440. package/md_cg/test_policy_required_ccg.py +12 -0
  441. package/md_cg/test_postings_digest_memo.py +139 -0
  442. package/md_cg/test_predict_beta.py +138 -135
  443. package/md_cg/test_qmd.py +500 -0
  444. package/md_cg/test_query_excerpt.py +209 -0
  445. package/md_cg/test_reach.py +456 -378
  446. package/md_cg/test_reach_snapshot_admission.py +218 -0
  447. package/md_cg/test_readcache_prodpath.py +4 -0
  448. package/md_cg/test_recall_face_guards.py +854 -812
  449. package/md_cg/test_recall_metadata.py +422 -0
  450. package/md_cg/test_retr_s5.py +104 -5
  451. package/md_cg/test_retr_score_once.py +14 -2
  452. package/md_cg/test_review_cli_attribution.py +11 -7
  453. package/md_cg/test_review_queue_batch.py +123 -0
  454. package/md_cg/test_review_queue_index.py +130 -0
  455. package/md_cg/test_rrf_candidates.py +292 -0
  456. package/md_cg/test_rrf_qualified_topk.py +214 -0
  457. package/md_cg/test_rrf_unique_votes.py +218 -0
  458. package/md_cg/test_s1_ast_judge.py +110 -0
  459. package/md_cg/test_scrub_ledger.py +413 -0
  460. package/md_cg/test_security_audit_b26.py +34 -3
  461. package/md_cg/test_security_audit_v21.py +1 -2
  462. package/md_cg/test_sensitivity_predicate_single_point.py +184 -0
  463. package/md_cg/test_session5_identity.py +93 -0
  464. package/md_cg/test_session_attribution_consistency.py +813 -0
  465. package/md_cg/test_session_identity_tristate.py +486 -0
  466. package/md_cg/test_sleep_p1.py +35 -3
  467. package/md_cg/test_state_event_op.py +8 -1
  468. package/md_cg/test_state_projection_streaming.py +136 -0
  469. package/md_cg/test_stg_interval_candidates.py +280 -0
  470. package/md_cg/test_stg_time_blocks.py +239 -0
  471. package/md_cg/test_stgidx_index_parity.py +16 -12
  472. package/md_cg/test_subgraph_bounded_bfs.py +227 -0
  473. package/md_cg/test_sustain_bounded.py +30 -2
  474. package/md_cg/test_task_memory.py +394 -0
  475. package/md_cg/test_text_projection_mutations.py +101 -0
  476. package/md_cg/test_time_anchor.py +421 -0
  477. package/md_cg/test_time_core_lint.py +1024 -975
  478. package/md_cg/test_tool_face.py +200 -189
  479. package/md_cg/test_trust_bounded_frontier.py +248 -0
  480. package/md_cg/test_trust_edge_reachability.py +405 -0
  481. package/md_cg/test_w5_path_fingerprint.py +381 -0
  482. package/md_cg/test_w7_redlines.py +659 -0
  483. package/md_cg/test_write_return_shape.py +919 -0
  484. package/md_cg/test_writepipe.py +5 -3
  485. package/md_cg/timeanchor.py +534 -0
  486. package/md_cg/tokens.py +68 -5
  487. package/md_cg/tool_face.py +273 -260
  488. package/md_cg/trust.py +110 -26
  489. package/md_cg/twophase.py +74 -2
  490. package/md_cg/weights.py +642 -638
  491. package/md_cg/whitebox.py +9 -0
  492. package/md_cg/whitebox_kb/aeis_core/core.py +5026 -5026
  493. package/md_cg/whitebox_kb/wisdom/chat_engine.py +1504 -1506
  494. package/md_cg/whitebox_kb/wisdom/code_compose.py +876 -837
  495. package/md_cg/whitebox_kb/wisdom/compiler_code_units.py +3040 -3033
  496. package/md_cg/whitebox_kb/wisdom/compose_engine.py +3 -2
  497. package/md_cg/whitebox_kb/wisdom/core/condition_vm.py +22 -2
  498. package/md_cg/whitebox_kb/wisdom/core/parser.py +17 -0
  499. package/md_cg/whitebox_kb/wisdom/csre.py +412 -264
  500. package/md_cg/whitebox_kb/wisdom/csre_session.py +97 -0
  501. package/md_cg/whitebox_kb/wisdom/md_access.py +23 -3
  502. package/md_cg/whitebox_kb/wisdom/migrate_wisdom.py +21 -0
  503. package/md_cg/whitebox_kb/wisdom/mini_python.py +44 -7
  504. package/md_cg/whitebox_kb/wisdom/navigate.py +249 -248
  505. package/md_cg/whitebox_kb/wisdom/net_units.py +12 -7
  506. package/md_cg/whitebox_kb/wisdom/neural_retrieve.py +100 -10
  507. package/md_cg/whitebox_kb/wisdom/semantic_translate.py +13 -7
  508. package/md_cg/whitebox_kb/wisdom/test_ecosystem_assembly.py +158 -158
  509. package/md_cg/whitebox_kb/wisdom/test_safe_module_proxy.py +147 -0
  510. package/md_cg/whitebox_kb/wisdom/verifier.py +1918 -1297
  511. package/md_cg/writelimit.py +584 -390
  512. package/md_cg/writepipe.py +587 -76
  513. package/package.json +13 -9
  514. package/src/bridge.ts +34 -11
  515. package/src/cli.ts +75 -65
  516. package/src/hooks.ts +908 -776
  517. package/src/index.ts +19 -5
  518. package/src/init.ts +362 -361
  519. package/src/lib/datapath.ts +419 -326
  520. package/src/lib/hook_audit.ts +256 -243
  521. package/src/lib/mdcg_client.ts +10 -2
  522. package/src/lib/mutual.ts +481 -432
  523. package/src/lib/prompt_safety.ts +164 -149
  524. package/src/lib/roleplay_web.ts +30 -2
  525. package/src/lib/token_store.ts +274 -52
  526. package/src/tools.ts +249 -223
  527. package/zcode/README.md +1 -1
  528. 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
  529. 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
  530. 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
  531. 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
  532. 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
  533. 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
  534. 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
  535. package/docs/eval/DSH/347/253/257/347/274/272/351/231/267/344/270/223/351/241/271_v1.0.md +0 -254
  536. 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
  537. 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
  538. package/docs/eval/bench_lingshu_self/bench_self.py +0 -140
  539. package/docs/eval/bench_lingshu_self/self_bench_result.json +0 -404
  540. 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
  541. 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
  542. 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
  543. 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
  544. 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
  545. 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
  546. 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
  547. 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
  548. 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
  549. 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
  550. 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
  551. 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
  552. 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
  553. 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
  554. 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
  555. 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
  556. 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
  557. 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
  558. 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
  559. 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
  560. 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
  561. 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
  562. 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
  563. 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
  564. 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
  565. 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
  566. 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
  567. 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
  568. 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
  569. 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
  570. 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
  571. 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
  572. 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
  573. 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
  574. 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
  575. 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
  576. 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
  577. 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
  578. 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
  579. 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
  580. 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
  581. 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
  582. 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
  583. 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
  584. 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
  585. 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
  586. 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
  587. 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
  588. 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
  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_v12.md +0 -203
  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_v13.md +0 -233
  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_v14.md +0 -191
  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_v15.md +0 -213
  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_v16.md +0 -214
  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_v17.md +0 -199
  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_v18.md +0 -183
  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_v19.md +0 -207
  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_v2.md +0 -156
  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_v20.md +0 -283
  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_v21.md +0 -224
  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_v22.md +0 -223
  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_v23.md +0 -293
  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_v24.md +0 -303
  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_v25.md +0 -230
  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_v26.md +0 -196
  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_v27.md +0 -259
  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_v28.md +0 -217
  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_v29.md +0 -270
  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_v3.md +0 -152
  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_v30.md +0 -231
  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_v31.md +0 -281
  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_v4.md +0 -128
  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_v5.md +0 -114
  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_v6.md +0 -192
  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_v7.md +0 -187
  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_v8.md +0 -207
  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_v9.md +0 -203
  617. 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
  618. 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
  619. 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
  620. 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
  621. 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
  622. 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
  623. 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
  624. 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
  625. 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
  626. 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
  627. 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
  628. 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
  629. 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
  630. 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
  631. 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
  632. 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
  633. package/docs/images/lingshu-moonlight-covenant-poster-preview.jpg +0 -0
  634. package/docs/images/lingshu-moonlight-covenant-poster.png +0 -0
  635. 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
  636. package/docs/mdcg/README/350/257/246/347/273/206/347/211/210_v0.4.10.md +0 -746
  637. package/docs/mdcg/guardrail-charter.md +0 -164
  638. package/docs/mdcg/lingshu_tutorial.html +0 -14956
  639. package/docs/mdcg/memory-assessment.html +0 -923
  640. package/docs/mdcg/memory_score.html +0 -213
  641. package/docs/mdcg/memory_score.png +0 -0
  642. package/docs/mdcg/release_v0.3.0.md +0 -36
  643. package/docs/mdcg/release_v0.4.11.md +0 -49
  644. package/docs/mdcg/release_v0.4.5.md +0 -55
  645. package/docs/mdcg/tool_table_v0.3.0.md +0 -117
  646. 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
  647. 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
  648. 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
  649. 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
  650. 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
  651. package/docs/mdcg//345/217/221/345/270/203/351/227/250/347/246/201/351/223/276_v0.1.md +0 -134
  652. 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
  653. 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
  654. 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
  655. 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
  656. 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
  657. 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
  658. 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
  659. 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
  660. package/docs/mdcg//347/274/272/345/217/243/345/215/225_P0/346/224/266/345/217/243_v0.1.md +0 -270
  661. 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
  662. 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
  663. 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
  664. 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
  665. 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
  666. 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
  667. 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
  668. 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
  669. 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
  670. 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
  671. 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
  672. 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
  673. 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
  674. 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
  675. 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
  676. 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
  677. 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
  678. 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
  679. 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
  680. package/docs/promo/bilibili-cover.jpg +0 -0
  681. package/docs/promo/promo-visual-1.png +0 -0
  682. package/docs/promo/promo-visual-2.png +0 -0
  683. package/docs/promo/promo-visual-3.png +0 -0
  684. package/docs/promo/promo-visual-4.png +0 -0
  685. package/docs/promo/promo-visual-5.png +0 -0
  686. package/docs/swarm//350/234/202/347/276/244/344/272/222/350/201/224_v0.1.md +0 -704
  687. 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
  688. 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
  689. 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
  690. 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
  691. 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
  692. 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
  693. 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
  694. 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
  695. 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
  696. 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
  697. package/docs/theory//346/231/272/350/203/275/350/256/2723.4.md +0 -5260
  698. 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
  699. 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
  700. 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
  701. 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
  702. 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
  703. 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
  704. 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
  705. 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
  706. 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
  707. 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
  708. 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
  709. 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
  710. 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
  711. 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
  712. 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
  713. 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
  714. 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/weights.py CHANGED
@@ -1,639 +1,643 @@
1
- # -*- coding: utf-8 -*-
2
- """位置权重矩阵(序标定)——「不同身份,参数偏好不同」。
3
-
4
- 理论出处(本仓原文,非外部知识)
5
- --------------------------------
6
- `docs/theory/智能的公理化基石.md` §4.3:信任合成是
7
- P_trust = f(一致性, 位置可预测性, 版本对齐度)
8
- 其中 `版本对齐度` 已由 `theory` + `links.version_alignment` 提供,
9
- `位置可预测性` 见 `predict`,`一致性` 见 `consistency`。本模块只补一件
10
- 此前缺失的东西:**三个分量在不同位置下的偏好序**。
11
-
12
- 六个位置分为**两类**(由功能身份决定,非人为指定):
13
-
14
- · 认知视角(viewpoint):设计者 / 反思 / 验证
15
- ——「思考的不同视角」,**三分量都涉及**,只标定主导,**不设零**。
16
- · 功能单元(functional):记录 / 输出 / 维生
17
- ——功能需求决定,**身份即排除**:主导 + 明确排除一项(≈0)。
18
-
19
- 排除项可**推导**(派生律:功能身份 → 缺失分量),不是选出来的:
20
-
21
- · 记录单元**不猜** → 不做预测 → 排除 位置可预测性
22
- · 输出单元**不记** → 不复现对账 → 排除 一致性
23
- · 维生系统**不对外** → 无外部协议 → 排除 版本对齐度
24
-
25
- 设计者定位(本轮裁决):
26
- · **不参与日常评估**——它是元视角,思考总体规律;细节由反思/验证承担,
27
- 否则要做全局细节整理,耗时极长。
28
-
29
- 可推导不变量(`invariants()` 逐条自检)
30
- --------------------------------------
31
- 1. **排除唯一**——每个分量恰被 **1** 个位置排除;因视角「3 项都涉及」,
32
- 故每个分量的参与度 = **5/6**(不是 3/6);
33
- 2. **排除项只属功能单元**,且 A/B/C 各被排除一次;
34
- 3. **视角主导 ↔ 功能单元排除**一一配对:
35
- 设计者(A)↔维生、反思(B)↔记录、验证(C)↔输出。
36
-
37
- 诚实边界
38
- --------
39
- · 本模块只声明**序**(谁能压过谁),**不宣称数值**——与 `links.py` 的
40
- `UP_STEP`/`DECAY_DAYS` 同一纪律(文档 §4.3:「v0.1 只声明结构」)。
41
- · `blend()` 里的权重是**占位值**(用序秩当量级),仅供仿真,标定属 v0.3。
42
-
43
- 零第三方依赖。
44
- """
45
- from __future__ import annotations
46
-
47
- import math
48
- import os
49
- import time
50
-
51
- from .fsutil import append_jsonl, read_jsonl
52
-
53
- # --------------------------------------------------------------------------
54
- # 常量
55
- # --------------------------------------------------------------------------
56
-
57
- #: 三个分量(顺序即 A/B/C,稳定,供外部按位引用)
58
- COMPONENTS = ("version_alignment", "predictability", "consistency")
59
-
60
- #: 分量中文名(日志/自描述用)
61
- COMPONENT_LABELS = {
62
- "version_alignment": "版本对齐度",
63
- "predictability": "位置可预测性",
64
- "consistency": "一致性",
65
- }
66
-
67
- #: 位置类别
68
- VIEWPOINT = "viewpoint" # 认知视角:3 项都涉及,只标主导,不设零
69
- FUNCTIONAL = "functional" # 功能单元:主导 + 明确排除一项
70
-
71
- #: 位置 → 权重序规格
72
- #: primary 主导分量(权重最高)
73
- #: secondary 次主导分量(功能单元的第二个偏好;视角为 None=另两项并列)
74
- #: excluded 被排除分量(≈0;仅功能单元有;视角为 None=不设零)
75
- SPEC = {
76
- # ---- 认知视角(不参与日常评估者仅 designer)----
77
- "designer": {"class": VIEWPOINT, "primary": "version_alignment",
78
- "secondary": None, "excluded": None},
79
- "reflect": {"class": VIEWPOINT, "primary": "predictability",
80
- "secondary": None, "excluded": None},
81
- "verify": {"class": VIEWPOINT, "primary": "consistency",
82
- "secondary": None, "excluded": None},
83
- # ---- 功能单元(身份即排除)----
84
- "record": {"class": FUNCTIONAL, "primary": "consistency",
85
- "secondary": "version_alignment", "excluded": "predictability"},
86
- "output": {"class": FUNCTIONAL, "primary": "predictability",
87
- "secondary": "version_alignment", "excluded": "consistency"},
88
- "sustain": {"class": FUNCTIONAL, "primary": "consistency",
89
- "secondary": "predictability", "excluded": "version_alignment"},
90
- }
91
-
92
- POSITION_ORDER = ("designer", "record", "reflect", "verify", "output", "sustain")
93
-
94
- #: 参与**日常评估**的位置(设计者是元参照系,不入日常)
95
- DAILY = ("record", "reflect", "verify", "output", "sustain")
96
-
97
- #: 序秩(占位量级,仅仿真用):主导 2 / 次主导 1 / 排除 0;视角另两项 = 1
98
- RANK_PRIMARY = 2.0
99
- RANK_SECONDARY = 1.0
100
- RANK_EXCLUDED = 0.0
101
-
102
-
103
- class WeightError(Exception):
104
- """未知位置 / 未知分量。"""
105
-
106
-
107
- # --------------------------------------------------------------------------
108
- # 基础查询
109
- # --------------------------------------------------------------------------
110
-
111
- # 生效条件:position 经 str(position or "").strip() 作为 SPEC 的键,命中时返回对应 spec dict,未命中(含 position 为 None/空串/未知)抛 WeightError;
112
- def _spec(position: str) -> dict:
113
- s = SPEC.get(str(position or "").strip())
114
- if s is None:
115
- raise WeightError(f"未知位置:{position!r}(允许:{POSITION_ORDER})")
116
- return s
117
-
118
-
119
- # 生效条件:component 经 str(component or "").strip() 后若结果在 COMPONENTS 中返回该规范分量字符串,否则抛 WeightError;
120
- def _comp(component: str) -> str:
121
- c = str(component or "").strip()
122
- if c not in COMPONENTS:
123
- raise WeightError(f"未知分量:{component!r}(允许:{COMPONENTS})")
124
- return c
125
-
126
-
127
- # 生效条件:position 可被 _spec 解析且其结果 "class" 等于 VIEWPOINT 时返回 True,非 VIEWPOINT 返回 False,未知 position 抛 WeightError;
128
- def is_viewpoint(position: str) -> bool:
129
- """是否认知视角(3 项都涉及,不设零)。"""
130
- return _spec(position)["class"] == VIEWPOINT
131
-
132
-
133
- # 生效条件:position 可被 _spec 解析且其结果 "class" 等于 FUNCTIONAL 时返回 True,非 FUNCTIONAL 返回 False,未知 position 抛 WeightError;
134
- def is_functional(position: str) -> bool:
135
- """是否功能单元(身份即排除)。"""
136
- return _spec(position)["class"] == FUNCTIONAL
137
-
138
-
139
- # 生效条件:position 经 _spec(position) 验证可解析后,返回原始 position 是否在模块级 DAILY 中;带首尾空白等未 strip 的值会因不在 DAILY 返回 False,未知 position 抛 WeightError;
140
- def in_daily_eval(position: str) -> bool:
141
- """是否参与日常评估(设计者=False,元参照系)。"""
142
- _spec(position)
143
- return position in DAILY
144
-
145
-
146
- # 生效条件:position 可被 _spec 解析且其 spec 含 "primary" 键时返回该值,未知 position 抛 WeightError;
147
- def dominant(position: str) -> str:
148
- """主导分量(权重最高者)。"""
149
- return _spec(position)["primary"]
150
-
151
-
152
- # 生效条件:position 可被 _spec 解析且其 spec 含 "secondary" 键时返回该值(视角可为 None),未知 position 抛 WeightError;
153
- def secondary(position: str):
154
- """次主导分量;视角返回 None(另两项并列、均 >0)。"""
155
- return _spec(position)["secondary"]
156
-
157
-
158
- # 生效条件:position 可被 _spec 解析且其 spec 含 "excluded" 键时返回该值(视角可为 None),未知 position 抛 WeightError;
159
- def excluded(position: str):
160
- """被排除分量(≈0);视角返回 None(不设零)。"""
161
- return _spec(position)["excluded"]
162
-
163
-
164
- # --------------------------------------------------------------------------
165
- # 序比较(本模块的权威结论:只有序,没有数值)
166
- # --------------------------------------------------------------------------
167
-
168
- # 生效条件:position 与 component 分别经 _spec 与 _comp 验证后,若 component 等于该位置 spec["excluded"] 返回 RANK_EXCLUDED,否则若等于 spec["primary"] 返回 RANK_PRIMARY,否则返回 RANK_SECONDARY;
169
- def rank(position: str, component: str) -> float:
170
- """分量在该位置的序秩(占位量级):2 主导 / 1 参与 / 0 排除。"""
171
- s = _spec(position)
172
- c = _comp(component)
173
- if s["excluded"] == c:
174
- return RANK_EXCLUDED
175
- if s["primary"] == c:
176
- return RANK_PRIMARY
177
- return RANK_SECONDARY
178
-
179
-
180
- # 生效条件:position、a、b 经 rank 可得序秩,若 rank(position,a)==rank(position,b) 返回 None,否则返回 ra>rb 的 bool;任一参数未知则抛 WeightError;
181
- def prefers(position: str, a: str, b: str):
182
- """该位置是否**严格**偏好 a 胜过 b。
183
-
184
- 返回 True / False / None(None = 二者并列,无严格序)。
185
- """
186
- ra, rb = rank(position, a), rank(position, b)
187
- if ra == rb:
188
- return None
189
- return ra > rb
190
-
191
-
192
- # 生效条件:position 经 _spec 验证且 COMPONENTS 可用时,按 -rank(position,c) 与 COMPONENTS.index(c) 排序,返回 {"preferred":[rank>0], "excluded":[rank==0], "primary":dominant(position), "secondary":secondary(position)}(无 dominant 键);
193
- def order(position: str):
194
- """从高到低排列的分量(并列者按 COMPONENTS 稳定序);排除项单独列出。"""
195
- _spec(position)
196
- ranked = sorted(COMPONENTS, key=lambda c: (-rank(position, c), COMPONENTS.index(c)))
197
- kept = [c for c in ranked if rank(position, c) > 0]
198
- exc = [c for c in ranked if rank(position, c) == 0]
199
- return {"preferred": kept, "excluded": exc,
200
- "primary": dominant(position), "secondary": secondary(position)}
201
-
202
-
203
- # 生效条件:position 经 _spec 验证且 COMPONENTS 可用时,将各分量 rank 归一化到 4 位小数返回;若 total 为 0 则按 total=1.0 计算;
204
- def weights(position: str) -> dict:
205
- """占位数值权重(序秩归一化);**未标定**,仅供仿真。"""
206
- _spec(position)
207
- raw = {c: rank(position, c) for c in COMPONENTS}
208
- total = sum(raw.values()) or 1.0
209
- return {c: round(raw[c] / total, 4) for c in COMPONENTS}
210
-
211
-
212
- # --------------------------------------------------------------------------
213
- # 合成(占位:f(一致性, 位置可预测性, 版本对齐度))
214
- # --------------------------------------------------------------------------
215
-
216
- # 生效条件:position 经 _spec 可解析且 COMPONENTS 可用时,components 对每个分量经 .get(c,0.0) 取值并截到 [0,1];若 components 无 .get 或该值不能 float 则该分量值记 0.0,仍按 weights(position) 加权求和返回含 score/values/weights/dominant 的 dict;
217
- def blend(position: str, components: dict) -> dict:
218
- """按位置的权重合成单一信任标量。
219
-
220
- `components` 形如 {"version_alignment": 1.0, "predictability": 0.8,
221
- "consistency": 0.6};缺省按 0 计。
222
-
223
- **数值未标定**:这里用序秩当量级,只保证「主导分量对结果影响最大」这
224
- 一方向性事实。标定属路线图 v0.3。
225
- """
226
- s = _spec(position)
227
- w = weights(position)
228
- vals = {}
229
- for c in COMPONENTS:
230
- try:
231
- vals[c] = max(0.0, min(1.0, float(components.get(c, 0.0))))
232
- except (AttributeError, TypeError, ValueError):
233
- vals[c] = 0.0
234
- score = sum(w[c] * vals[c] for c in COMPONENTS)
235
- return {"position": position, "class": s["class"],
236
- "weights": w, "values": vals, "score": round(score, 4),
237
- "dominant": s["primary"],
238
- "note": "权重为占位序秩(未标定);仅方向性结论可信"}
239
-
240
-
241
- # --------------------------------------------------------------------------
242
- # 不变量自检(把上轮的三条结构约束落成可执行断言)
243
- # --------------------------------------------------------------------------
244
-
245
- # 生效条件:模块级 POSITION_ORDER、COMPONENTS、SPEC、VIEWPOINT、FUNCTIONAL 等常量齐备时,invariants() 逐条自检并返回 {"ok": all(checks[*].ok), "checks": ...};
246
- def invariants() -> dict:
247
- """逐条自检结构约束,返回 {ok, checks{name: {ok, detail}}}。"""
248
- checks = {}
249
-
250
- # ① 排除唯一:每分量恰被 1 个位置排除 ⇒ 参与度 = 5/6
251
- part = {c: [p for p in POSITION_ORDER if rank(p, c) > 0] for c in COMPONENTS}
252
- exc_of = {c: [p for p in POSITION_ORDER if rank(p, c) == 0] for c in COMPONENTS}
253
- checks["excluded_exactly_once"] = {
254
- "ok": all(len(exc_of[c]) == 1 for c in COMPONENTS)
255
- and all(len(part[c]) == len(POSITION_ORDER) - 1 for c in COMPONENTS),
256
- "detail": {c: {"participants": len(part[c]), "excluded_by": exc_of[c]}
257
- for c in COMPONENTS}}
258
-
259
- # ② 排除项只属功能单元,且每分量各被排除一次
260
- excs = {}
261
- for p in POSITION_ORDER:
262
- e = excluded(p)
263
- if e is None:
264
- continue
265
- excs.setdefault(e, []).append(p)
266
- only_functional = all(is_functional(p)
267
- for vs in excs.values() for p in vs)
268
- both = all(len(excs.get(c) or []) == 1 for c in COMPONENTS)
269
- checks["exclusion_functional_only_once_each"] = {
270
- "ok": bool(excs) and only_functional and both,
271
- "detail": {c: excs.get(c, []) for c in COMPONENTS}}
272
-
273
- # ③ 视角主导 ↔ 功能单元排除 一一配对
274
- dom_pairs = {} # 分量 → 视角
275
- for p in POSITION_ORDER:
276
- if is_viewpoint(p):
277
- dom_pairs.setdefault(dominant(p), []).append(p)
278
- pairing = {}
279
- for p in POSITION_ORDER:
280
- if not is_functional(p):
281
- continue
282
- e = excluded(p)
283
- if e and len(dom_pairs.get(e) or []) == 1:
284
- pairing[dom_pairs[e][0]] = p
285
- checks["viewpoint_dominant_pairs_unit_exclusion"] = {
286
- "ok": len(pairing) == 3,
287
- "detail": pairing}
288
-
289
- # ④ 功能单元恰有一个排除项;视角一个都没有
290
- checks["functional_excludes_exactly_one"] = {
291
- "ok": all(excluded(p) is not None for p in POSITION_ORDER
292
- if is_functional(p))
293
- and all(excluded(p) is None for p in POSITION_ORDER
294
- if is_viewpoint(p)),
295
- "detail": {p: excluded(p) for p in POSITION_ORDER}}
296
-
297
- # ⑤ 排除项序秩严格最低
298
- checks["excluded_is_lowest"] = {
299
- "ok": all(rank(p, excluded(p)) < rank(p, c)
300
- for p in POSITION_ORDER if excluded(p)
301
- for c in COMPONENTS if c != excluded(p)),
302
- "detail": "排除分量序秩 0,严格低于其余分量"}
303
-
304
- return {"ok": all(c["ok"] for c in checks.values()), "checks": checks}
305
-
306
-
307
- # --------------------------------------------------------------------------
308
- # 自描述
309
- # --------------------------------------------------------------------------
310
-
311
- # 生效条件:模块级 COMPONENTS、COMPONENT_LABELS、VIEWPOINT、FUNCTIONAL、SPEC、POSITION_ORDER、DAILY 齐备时,返回位置权重矩阵自描述 dict,其中 components 按 COMPONENT_LABELS、positions 按 SPEC、daily_eval 按 DAILY 展开;
312
- def catalog() -> dict:
313
- """位置权重矩阵自描述(供 MCP / 文档对照验证)。"""
314
- return {
315
- "question": "不同身份,参数偏好不同",
316
- "components": {c: COMPONENT_LABELS[c] for c in COMPONENTS},
317
- "classes": {
318
- VIEWPOINT: "认知视角:3 项都涉及,只标主导,不设零",
319
- FUNCTIONAL: "功能单元:身份即排除(不猜/不记/不对外)",
320
- },
321
- "positions": {p: dict(SPEC[p]) for p in POSITION_ORDER},
322
- "daily_eval": list(DAILY),
323
- "meta_reference": [p for p in POSITION_ORDER if p not in DAILY],
324
- "derivation": {"record": "不猜 → 排除 位置可预测性",
325
- "output": "不记 → 排除 一致性",
326
- "sustain": "不对外 → 排除 版本对齐度"},
327
- "formula": "P_trust = f(一致性, 位置可预测性, 版本对齐度)",
328
- "honest_boundary": "只声明序,不宣称数值;blend() 权重为占位序秩",
329
- }
330
-
331
-
332
- # ==========================================================================
333
- # 结构重要性重算(maintain.importance)
334
- # ==========================================================================
335
- #
336
- # 与上面的「位置权重矩阵」是**两件事**,不要混:
337
- # · 上面回答「不同身份偏好哪些信任分量」(序标定,作用于位置);
338
- # · 下面回答「一个节点在认知图结构里该有多大重要性」(重算,作用于节点)。
339
- #
340
- # 三个**可测**分量(对齐计划:按覆盖度、冗余度、验证基底重排):
341
- # coverage 覆盖度 —— 有多少节点指向/包含它(入度,结构中枢性)
342
- # redundancy 冗余度 —— 与同层节点内容哈希撞车的比例(越冗余越该降权)
343
- # basis 验证基底 —— 外部验证档位(formal_proof/compiler > test > measurement > data > other)
344
- #
345
- # 纪律:只改 `frontmatter.importance` 与索引快照,**不动正文**(减小 diff 与重写成本);
346
- # 全流程可预演(apply=False)、可留痕(`_maintain.jsonl`)、可回滚(rollback)。
347
- # 性能:dry-run 不读任何节点文件(只用索引快照);apply 只写「变动超阈值」的节点。
348
-
349
- MAINTAIN_LOG = "_maintain.jsonl"
350
-
351
- #: 验证基底 → 可信权重(枚举来自 nodefile.VERIFICATION_BASIS)
352
- #: 文科来源一致性档(textbook/public_kb)低于可复现档、高于 other:
353
- #: 教材/公开知识库是权威二手来源,但不是可复算证据。
354
- BASIS_TRUST = {
355
- "formal_proof": 1.00,
356
- "compiler": 0.90,
357
- "test": 0.80,
358
- "measurement": 0.65,
359
- "textbook": 0.60,
360
- "data": 0.45,
361
- "public_kb": 0.40,
362
- "other": 0.25,
363
- }
364
- BASIS_TRUST_MISSING = 0.25
365
-
366
- #: 三分量合成权重(占位量级,沿用「只声明序」纪律;分量序主导→次要)
367
- IMPORTANCE_MIX = {"basis": 0.40, "coverage": 0.35, "redundancy": 0.25}
368
- COV_SAT = 12.0 # 入度饱和点:约 12 条入边即视为覆盖满
369
- IMPORTANCE_FLOOR = 0.10
370
- IMPORTANCE_CEIL = 1.00
371
- IMPORTANCE_PROTECT = 0.70 # ≥ 此值:保护下限(已保护/高危节点不被降破)
372
- APPLY_DELTA = 0.05 # 变动小于此值不写盘(防 IO 放大)
373
-
374
-
375
- # 生效条件:e 为可 .get 的映射时,遍历 e.get("edges")(假值视为空)逐项按 dict 的 to/target/node/id 或标量转 str 追加非假值,再取 e.get("subgraph"),若为 dict 取其 "nodes" 否则取 subgraph 本身,且当其为 list/tuple 时追加其中 dict 的 id 或标量转 str 的非假值,返回 out;
376
- def _targets(e: dict):
377
- """索引快照里的「出边目标」:edges ∪ subgraph.nodes(兼容 dict/str)。"""
378
- out = []
379
- for x in (e.get("edges") or []):
380
- if isinstance(x, dict):
381
- t = x.get("to") or x.get("target") or x.get("node") or x.get("id")
382
- else:
383
- t = x
384
- if t:
385
- out.append(str(t))
386
- sg = e.get("subgraph")
387
- raw = sg.get("nodes") if isinstance(sg, dict) else sg
388
- if isinstance(raw, (list, tuple)):
389
- for n in raw:
390
- nid = n.get("id") if isinstance(n, dict) else n
391
- if nid:
392
- out.append(str(nid))
393
- return out
394
-
395
-
396
- # 生效条件:basis 为 None 时返回 BASIS_TRUST_MISSING;否则把 str(basis).strip().lower() 作为 BASIS_TRUST 的键,命中返回对应权重,未命中(含空串)返回 BASIS_TRUST_MISSING;
397
- def basis_trust(basis):
398
- """验证基底 → 可信权重;缺失/未知一律给最低档(不假装可信)。"""
399
- if basis is None:
400
- return BASIS_TRUST_MISSING
401
- return BASIS_TRUST.get(str(basis).strip().lower(), BASIS_TRUST_MISSING)
402
-
403
-
404
- # 生效条件:cg 的 index(getattr(cg,"index",None) or {})与其 "nodes" 同为真值时,返回以这些节点 id 为键的入度表,仅当 _targets(e) 给出的目标 t 也在 nodes 且 t != nid 时计数 +1;index 或 "nodes" 为假值(None/{})时 nodes 回落 {},直接返回空 dict。
405
- def coverage_index(cg) -> dict:
406
- """入度表:nid → 被多少节点指向(覆盖度,O(N) 免读文件)。"""
407
- nodes = (getattr(cg, "index", None) or {}).get("nodes") or {}
408
- indeg = {nid: 0 for nid in list(nodes)}
409
- for nid, e in list(nodes.items()):
410
- for t in _targets(e):
411
- if t in indeg and t != nid:
412
- indeg[t] += 1
413
- return indeg
414
-
415
-
416
- # 生效条件:cg.index 的 "nodes" 为 dict 时,layer 真值仅处理 e.get("layer")==layer 的节点,layer 假值不过滤;content_hash 为假值(None/空串/0 等)的节点输出 0.0,同 (e.get("layer"), content_hash) 第二次撞车时当前与首次节点均输出 1.0,否则输出 0.0;
417
- def redundancy_map(cg, layer=None) -> dict:
418
- """同层内容哈希撞车 → 冗余度 1.0,否则 0.0(精确冗余,O(N) 免读文件)。
419
-
420
- 诚实边界:这是**精确冗余**(同一层出现完全相同的正文),不是语义近似冗余。
421
- 语义近似由 `forgetting.redundancy`(bigram 覆盖)在写入侧单独负责。
422
- """
423
- nodes = (getattr(cg, "index", None) or {}).get("nodes") or {}
424
- seen, out = {}, {}
425
- for nid, e in list(nodes.items()):
426
- if layer and e.get("layer") != layer:
427
- continue
428
- h = e.get("content_hash") or ""
429
- if not h:
430
- out[nid] = 0.0
431
- continue
432
- key = (e.get("layer"), h)
433
- if key in seen:
434
- out[nid] = 1.0
435
- out[seen[key]] = 1.0
436
- else:
437
- seen[key] = nid
438
- out.setdefault(nid, 0.0)
439
- return out
440
-
441
-
442
- # 生效条件:cg.index 的 nodes 为 dict 且 entry 非 None 或 nodes.get(nid) 命中时,e 按 entry 优先否则 nodes.get(nid);indeg/red 分别仅在为 None 时回落 coverage_index(cg)/redundancy_map(cg, layer=e.get("layer"))(空容器不回落);按 IMPORTANCE_MIX、COV_SAT 及 e.get("verification_basis") 的 basis_trust 计算 coverage/redundancy/basis 分量,按 bool(e.get("protected")) 选择 IMPORTANCE_PROTECT 或 IMPORTANCE_FLOOR 作下限、IMPORTANCE_CEIL 作上限,返回含 before(e.get("importance",0.5) or 0.5)、after、components、protected 的 dict;entry 与 nodes.get(nid) 均为 None 返回 None;
443
- def node_importance(cg, nid, indeg=None, red=None, entry=None):
444
- """单节点结构重要性分解(可审计:给出分量而非只给分)。"""
445
- nodes = (getattr(cg, "index", None) or {}).get("nodes") or {}
446
- e = entry if entry is not None else nodes.get(nid)
447
- if e is None:
448
- return None
449
- indeg = indeg if indeg is not None else coverage_index(cg)
450
- red = red if red is not None else redundancy_map(cg, layer=e.get("layer"))
451
- d = int(indeg.get(nid, 0) or 0)
452
- cov = min(1.0, math.log1p(d) / math.log1p(COV_SAT))
453
- rr = max(0.0, min(1.0, float(red.get(nid, 0.0) or 0.0)))
454
- bt = basis_trust(e.get("verification_basis"))
455
- score = (IMPORTANCE_MIX["basis"] * bt
456
- + IMPORTANCE_MIX["coverage"] * cov
457
- + IMPORTANCE_MIX["redundancy"] * (1.0 - rr))
458
- protected = bool(e.get("protected"))
459
- floor = IMPORTANCE_PROTECT if protected else IMPORTANCE_FLOOR
460
- score = max(floor, min(IMPORTANCE_CEIL, score))
461
- return {"node_id": nid,
462
- "before": float(e.get("importance", 0.5) or 0.5),
463
- "after": round(score, 4),
464
- "components": {"coverage": round(cov, 4), "redundancy": round(rr, 4),
465
- "basis_trust": round(bt, 4), "in_degree": d,
466
- "verification_basis": e.get("verification_basis")},
467
- "protected": protected,
468
- "protected_floor": protected}
469
-
470
-
471
- # 生效条件:给定 cg 且 nodes = cg.index["nodes"] 时按 layer 过滤、limit 为真值才 ids = ids[:int(limit)] 逐节点重算,abs(after-before) < float(min_delta) 记 unchanged(**预演面不接档位闸**);仅 apply=True 才在**任何写盘之前**先经 autonomy_modes.e_gate() 档位判定(补强批次 v1.2·E 面接线)——非 ALLOW(plan 档无计划)即经 forbidden_result() fail-closed 早退(零写盘、零审计行),ALLOW(confirm/full)才把 frontmatter.importance/importance_source/importance_components 写回(after >= IMPORTANCE_PROTECT 且未 protected 时补写 protected/protection_reason),并向 cg.root 下 append_jsonl(..., MAINTAIN_LOG) 记 batch 后 rebuild_index。
472
- def recalc(cg, layer=None, limit=None, apply=False, min_delta=APPLY_DELTA,
473
- actor="maintain", dry_run_samples=10):
474
- """结构重要性重算:覆盖度 + 冗余度 + 验证基底 → 重排节点重要性。
475
-
476
- apply=False(默认)只出报表,不写盘(对应计划「可预演」)。
477
- apply=True 逐节点改写 `frontmatter.importance`,每批写一条 `_maintain.jsonl`
478
- (before/after/分量/actor),并提供 rollback 反向应用。
479
-
480
- **档位闸(补强批次 v1.2·E 面接线)**:`apply=True` 的**写盘前**先经
481
- `autonomy_modes.e_gate()`(设计 §三 plan 档 E「须命中计划步骤」)——plan 档
482
- 无计划 ⇒ `forbidden_result()` fail-closed 早退(零写盘、零审计行);confirm/
483
- full ⇒ ALLOW、**原链原样**(逐位不变)。`apply=False`(预演)不接闸。
484
- """
485
- nodes = (getattr(cg, "index", None) or {}).get("nodes") or {}
486
- indeg = coverage_index(cg)
487
- red = redundancy_map(cg, layer=layer)
488
- ids = [nid for nid, e in list(nodes.items())
489
- if not layer or e.get("layer") == layer]
490
- ids.sort()
491
- if limit:
492
- ids = ids[:int(limit)]
493
- t0 = time.time()
494
- planned, changed, unchanged, skipped = [], 0, 0, 0
495
- before_sum = after_sum = 0.0
496
- by_layer = {}
497
- batch = time.strftime("%Y%m%d-%H%M%S")
498
- samples = []
499
- for nid in ids:
500
- info = node_importance(cg, nid, indeg=indeg, red=red)
501
- if info is None:
502
- skipped += 1
503
- continue
504
- before, after = info["before"], info["after"]
505
- before_sum += before
506
- after_sum += after
507
- lay = (nodes.get(nid) or {}).get("layer") or "?"
508
- bl = by_layer.setdefault(lay, {"nodes": 0, "changed": 0,
509
- "avg_before": 0.0, "avg_after": 0.0})
510
- bl["nodes"] += 1
511
- if abs(after - before) < float(min_delta):
512
- unchanged += 1
513
- continue
514
- changed += 1
515
- bl["changed"] += 1
516
- plan = dict(info)
517
- plan["batch"] = batch
518
- plan["delta"] = round(after - before, 4)
519
- planned.append(plan)
520
- if len(samples) < int(dry_run_samples):
521
- samples.append(plan)
522
- for lay, bl in list(by_layer.items()):
523
- bl["avg_before"] = 0.0
524
- bl["avg_after"] = 0.0
525
- written = 0
526
- if apply:
527
- # 三档自治(补强批次 v1.2·E 面接线,设计 §三 plan 档 E「须命中计划步骤」):
528
- # E 类动作**写盘前**的档位判定单点(同 `freshness.recalc` 口径)——
529
- # 本函数无独立资格闸,闸口取「进入写盘支路的第一行」=任何盘面写入之前;
530
- # ALLOW(confirm/full)原链原样,plan 档无计划 ⇒ fail-closed 早退。
531
- from . import autonomy_modes as _am
532
- _dec = _am.e_gate()
533
- if _dec["decision"] != _am.ALLOW:
534
- return _am.forbidden_result(_dec, action="importance", dry_run=False,
535
- written=0, batch=batch)
536
- for plan in planned:
537
- nid = plan["node_id"]
538
- node = cg.get(nid)
539
- if not node:
540
- continue
541
- fm = node.get("frontmatter") or {}
542
- fm["importance"] = plan["after"]
543
- fm["importance_source"] = "recalc"
544
- fm["importance_components"] = plan["components"]
545
- if plan["after"] >= IMPORTANCE_PROTECT and not fm.get("protected"):
546
- fm["protected"] = True
547
- fm["protection_reason"] = (f"recalc importance={plan['after']:.2f}"
548
- f"≥{IMPORTANCE_PROTECT}")
549
- path = os.path.join(cg.root, (nodes.get(nid) or {}).get("path")
550
- or node.get("path") or f"{nid}.md")
551
- cg._write_node(nid, path, fm, node.get("content") or "")
552
- e = nodes.get(nid)
553
- if e is not None:
554
- e["importance"] = plan["after"]
555
- if fm.get("protected"):
556
- e["protected"] = True
557
- e["protection_reason"] = fm["protection_reason"]
558
- rec = {"t": time.time(), "action": "importance", "batch": batch,
559
- "id": nid, "layer": (e or {}).get("layer"),
560
- "before": plan["before"], "after": plan["after"],
561
- "delta": plan["delta"], "components": plan["components"],
562
- "actor": actor}
563
- append_jsonl(os.path.join(cg.root, MAINTAIN_LOG), rec)
564
- written += 1
565
- if written and hasattr(cg, "rebuild_index"):
566
- cg.rebuild_index() # 索引与盘面重新对齐,防内存/磁盘分叉
567
- n = max(1, len(ids))
568
- return {
569
- "ok": True, "action": "importance", "dry_run": not apply,
570
- "batch": batch, "nodes_scanned": len(ids), "changed": changed,
571
- "unchanged": unchanged, "skipped": skipped, "written": written,
572
- "min_delta": float(min_delta),
573
- "avg_before": round(before_sum / n, 4),
574
- "avg_after": round(after_sum / n, 4),
575
- "by_layer": {k: {"nodes": v["nodes"], "changed": v["changed"]}
576
- for k, v in by_layer.items()},
577
- "samples": samples, "elapsed_ms": int((time.time() - t0) * 1000),
578
- "log": MAINTAIN_LOG,
579
- "note": ("dry-run:未写盘;apply=True 才改写 importance"
580
- if not apply else
581
- f"已改写 {written} 个节点;回滚见 rollback(batch={batch})"),
582
- }
583
-
584
-
585
- # 生效条件:cg.root/MAINTAIN_LOG 可读出 action=="importance" 记录后,entry_ids 为真值时按记录 id 是否在 {str(x) for x in entry_ids} 过滤;否则 batch 为真值时按 r.get("batch")==batch 过滤;否则取最后一条记录的 batch 再按其过滤;无 importance 记录返回 {"ok":False,"error":"no_records","reverted":0},过滤后无记录返回 {"ok":False,"error":"batch_not_found",...};对命中且 cg.get(nid) 为真的记录写回 rec.get("before") 并计数,若 reverted 且 cg 有 rebuild_index 则调用,最后 append_jsonl 写 actor 并返回 {"ok":True,"batch":batch,"reverted":reverted,"ids":ids};
586
- def rollback(cg, batch=None, entry_ids=None, actor="maintain"):
587
- """把重要性重算反向应用(bulk 改写的可回滚兑现)。
588
-
589
- batch 为空则回滚**最近一批**。返回 {ok, reverted, batch, ids}。
590
- """
591
- log_path = os.path.join(cg.root, MAINTAIN_LOG)
592
- records = [r for r in read_jsonl(log_path)
593
- if r.get("action") == "importance"]
594
- if not records:
595
- return {"ok": False, "error": "no_records", "reverted": 0}
596
- if entry_ids:
597
- want = {str(x) for x in entry_ids}
598
- records = [r for r in records if str(r.get("id")) in want]
599
- elif batch:
600
- records = [r for r in records if r.get("batch") == batch]
601
- else:
602
- batch = records[-1].get("batch")
603
- records = [r for r in records if r.get("batch") == batch]
604
- if not records:
605
- return {"ok": False, "error": "batch_not_found", "batch": batch,
606
- "reverted": 0}
607
- nodes = (getattr(cg, "index", None) or {}).get("nodes") or {}
608
- reverted, ids = 0, []
609
- # 同一节点多条记录时以最早一条的 before 为准
610
- for rec in records:
611
- nid = rec.get("id")
612
- node = cg.get(nid)
613
- if not node:
614
- continue
615
- fm = node.get("frontmatter") or {}
616
- fm["importance"] = rec.get("before")
617
- fm["importance_source"] = "recalc_rollback"
618
- path = os.path.join(cg.root, (nodes.get(nid) or {}).get("path")
619
- or node.get("path") or f"{nid}.md")
620
- cg._write_node(nid, path, fm, node.get("content") or "")
621
- e = nodes.get(nid)
622
- if e is not None:
623
- e["importance"] = rec.get("before")
624
- reverted += 1
625
- ids.append(nid)
626
- if reverted and hasattr(cg, "rebuild_index"):
627
- cg.rebuild_index()
628
- append_jsonl(log_path, {"t": time.time(), "action": "importance_rollback",
629
- "batch": batch, "reverted": reverted, "actor": actor})
630
- return {"ok": True, "batch": batch, "reverted": reverted, "ids": ids}
631
-
632
-
633
- # 生效条件:cg.root/MAINTAIN_LOG 可读出 JSONL 记录后,action 为真值时按 r.get("action")==action 过滤,假值不过滤;返回 recs[-int(limit):],其中 limit=0 时 int(0)=0 使切片为 recs[0:] 返回全部而非空;
634
- def history(cg, limit=100, action=None):
635
- """维护留痕(最近 limit 条),可按 action 过滤。"""
636
- recs = list(read_jsonl(os.path.join(cg.root, MAINTAIN_LOG)))
637
- if action:
638
- recs = [r for r in recs if r.get("action") == action]
1
+ # -*- coding: utf-8 -*-
2
+ """位置权重矩阵(序标定)——「不同身份,参数偏好不同」。
3
+
4
+ 理论出处(本仓原文,非外部知识)
5
+ --------------------------------
6
+ `docs/theory/智能的公理化基石.md` §十「信任:对协作者行为的概率估计」(该文档章节用中文序号)给出信任的**定义**;
7
+ 其**合成式**的出处=`docs/swarm/蜂群互联_v0.1.md` §4.3(该文档用阿拉伯数字章节号;下文引用的 §4.3 均指它):
8
+ P_trust = f(一致性, 位置可预测性, 版本对齐度)
9
+ 其中 `版本对齐度` 已由 `theory` + `links.version_alignment` 提供,
10
+ `位置可预测性` 见 `predict`,`一致性` 见 `consistency`。本模块只补一件
11
+ 此前缺失的东西:**三个分量在不同位置下的偏好序**。
12
+
13
+ 六个位置分为**两类**(由功能身份决定,非人为指定):
14
+
15
+ · 认知视角(viewpoint):设计者 / 反思 / 验证
16
+ ——「思考的不同视角」,**三分量都涉及**,只标定主导,**不设零**。
17
+ · 功能单元(functional):记录 / 输出 / 维生
18
+ ——功能需求决定,**身份即排除**:主导 + 明确排除一项(≈0)。
19
+
20
+ 排除项可**推导**(派生律:功能身份 → 缺失分量),不是选出来的:
21
+
22
+ · 记录单元**不猜** → 不做预测 → 排除 位置可预测性
23
+ · 输出单元**不记** → 不复现对账 → 排除 一致性
24
+ · 维生系统**不对外** → 无外部协议 → 排除 版本对齐度
25
+
26
+ 设计者定位(本轮裁决):
27
+ · **不参与日常评估**——它是元视角,思考总体规律;细节由反思/验证承担,
28
+ 否则要做全局细节整理,耗时极长。
29
+
30
+ 可推导不变量(`invariants()` 逐条自检)
31
+ --------------------------------------
32
+ 1. **排除唯一**——每个分量恰被 **1** 个位置排除;因视角「3 项都涉及」,
33
+ 故每个分量的参与度 = **5/6**(不是 3/6);
34
+ 2. **排除项只属功能单元**,且 A/B/C 各被排除一次;
35
+ 3. **视角主导 ↔ 功能单元排除**一一配对:
36
+ 设计者(A)↔维生、反思(B)↔记录、验证(C)↔输出。
37
+
38
+ 诚实边界
39
+ --------
40
+ · 本模块只声明**序**(谁能压过谁),**不宣称数值**——与 `links.py` 的
41
+ `UP_STEP`/`DECAY_DAYS` 同一纪律(蜂群互联 §4.3:「只声明结构,不宣称权重数值」)。
42
+ · `blend()` 里的权重是**占位值**(用序秩当量级),仅供仿真,标定属 v0.3。
43
+
44
+ 零第三方依赖。
45
+ """
46
+ from __future__ import annotations
47
+
48
+ import math
49
+ import os
50
+ import time
51
+
52
+ from . import protect # #431:条目口径保护单点
53
+ from .fsutil import append_jsonl, read_jsonl
54
+
55
+ # --------------------------------------------------------------------------
56
+ # 常量
57
+ # --------------------------------------------------------------------------
58
+
59
+ #: 三个分量(顺序即 A/B/C,稳定,供外部按位引用)
60
+ COMPONENTS = ("version_alignment", "predictability", "consistency")
61
+
62
+ #: 分量中文名(日志/自描述用)
63
+ COMPONENT_LABELS = {
64
+ "version_alignment": "版本对齐度",
65
+ "predictability": "位置可预测性",
66
+ "consistency": "一致性",
67
+ }
68
+
69
+ #: 位置类别
70
+ VIEWPOINT = "viewpoint" # 认知视角:3 项都涉及,只标主导,不设零
71
+ FUNCTIONAL = "functional" # 功能单元:主导 + 明确排除一项
72
+
73
+ #: 位置 → 权重序规格
74
+ #: primary 主导分量(权重最高)
75
+ #: secondary 次主导分量(功能单元的第二个偏好;视角为 None=另两项并列)
76
+ #: excluded 被排除分量(≈0;仅功能单元有;视角为 None=不设零)
77
+ SPEC = {
78
+ # ---- 认知视角(不参与日常评估者仅 designer)----
79
+ "designer": {"class": VIEWPOINT, "primary": "version_alignment",
80
+ "secondary": None, "excluded": None},
81
+ "reflect": {"class": VIEWPOINT, "primary": "predictability",
82
+ "secondary": None, "excluded": None},
83
+ "verify": {"class": VIEWPOINT, "primary": "consistency",
84
+ "secondary": None, "excluded": None},
85
+ # ---- 功能单元(身份即排除)----
86
+ "record": {"class": FUNCTIONAL, "primary": "consistency",
87
+ "secondary": "version_alignment", "excluded": "predictability"},
88
+ "output": {"class": FUNCTIONAL, "primary": "predictability",
89
+ "secondary": "version_alignment", "excluded": "consistency"},
90
+ "sustain": {"class": FUNCTIONAL, "primary": "consistency",
91
+ "secondary": "predictability", "excluded": "version_alignment"},
92
+ }
93
+
94
+ POSITION_ORDER = ("designer", "record", "reflect", "verify", "output", "sustain")
95
+
96
+ #: 参与**日常评估**的位置(设计者是元参照系,不入日常)
97
+ DAILY = ("record", "reflect", "verify", "output", "sustain")
98
+
99
+ #: 序秩(占位量级,仅仿真用):主导 2 / 次主导 1 / 排除 0;视角另两项 = 1
100
+ RANK_PRIMARY = 2.0
101
+ RANK_SECONDARY = 1.0
102
+ RANK_EXCLUDED = 0.0
103
+
104
+
105
+ class WeightError(Exception):
106
+ """未知位置 / 未知分量。"""
107
+
108
+
109
+ # --------------------------------------------------------------------------
110
+ # 基础查询
111
+ # --------------------------------------------------------------------------
112
+
113
+ # 生效条件:position 经 str(position or "").strip() 作为 SPEC 的键,命中时返回对应 spec dict,未命中(含 position 为 None/空串/未知)抛 WeightError;
114
+ def _spec(position: str) -> dict:
115
+ s = SPEC.get(str(position or "").strip())
116
+ if s is None:
117
+ raise WeightError(f"未知位置:{position!r}(允许:{POSITION_ORDER})")
118
+ return s
119
+
120
+
121
+ # 生效条件:component 经 str(component or "").strip() 后若结果在 COMPONENTS 中返回该规范分量字符串,否则抛 WeightError;
122
+ def _comp(component: str) -> str:
123
+ c = str(component or "").strip()
124
+ if c not in COMPONENTS:
125
+ raise WeightError(f"未知分量:{component!r}(允许:{COMPONENTS})")
126
+ return c
127
+
128
+
129
+ # 生效条件:position 可被 _spec 解析且其结果 "class" 等于 VIEWPOINT 时返回 True,非 VIEWPOINT 返回 False,未知 position 抛 WeightError;
130
+ def is_viewpoint(position: str) -> bool:
131
+ """是否认知视角(3 项都涉及,不设零)。"""
132
+ return _spec(position)["class"] == VIEWPOINT
133
+
134
+
135
+ # 生效条件:position 可被 _spec 解析且其结果 "class" 等于 FUNCTIONAL 时返回 True,非 FUNCTIONAL 返回 False,未知 position 抛 WeightError;
136
+ def is_functional(position: str) -> bool:
137
+ """是否功能单元(身份即排除)。"""
138
+ return _spec(position)["class"] == FUNCTIONAL
139
+
140
+
141
+ # 生效条件:position 经 _spec(position) 验证可解析后,返回原始 position 是否在模块级 DAILY 中;带首尾空白等未 strip 的值会因不在 DAILY 返回 False,未知 position 抛 WeightError;
142
+ def in_daily_eval(position: str) -> bool:
143
+ """是否参与日常评估(设计者=False,元参照系)。"""
144
+ _spec(position)
145
+ return position in DAILY
146
+
147
+
148
+ # 生效条件:position 可被 _spec 解析且其 spec 含 "primary" 键时返回该值,未知 position 抛 WeightError;
149
+ def dominant(position: str) -> str:
150
+ """主导分量(权重最高者)。"""
151
+ return _spec(position)["primary"]
152
+
153
+
154
+ # 生效条件:position 可被 _spec 解析且其 spec 含 "secondary" 键时返回该值(视角可为 None),未知 position 抛 WeightError;
155
+ def secondary(position: str):
156
+ """次主导分量;视角返回 None(另两项并列、均 >0)。"""
157
+ return _spec(position)["secondary"]
158
+
159
+
160
+ # 生效条件:position 可被 _spec 解析且其 spec 含 "excluded" 键时返回该值(视角可为 None),未知 position 抛 WeightError;
161
+ def excluded(position: str):
162
+ """被排除分量(≈0);视角返回 None(不设零)。"""
163
+ return _spec(position)["excluded"]
164
+
165
+
166
+ # --------------------------------------------------------------------------
167
+ # 序比较(本模块的权威结论:只有序,没有数值)
168
+ # --------------------------------------------------------------------------
169
+
170
+ # 生效条件:position 与 component 分别经 _spec 与 _comp 验证后,若 component 等于该位置 spec["excluded"] 返回 RANK_EXCLUDED,否则若等于 spec["primary"] 返回 RANK_PRIMARY,否则返回 RANK_SECONDARY;
171
+ def rank(position: str, component: str) -> float:
172
+ """分量在该位置的序秩(占位量级):2 主导 / 1 参与 / 0 排除。"""
173
+ s = _spec(position)
174
+ c = _comp(component)
175
+ if s["excluded"] == c:
176
+ return RANK_EXCLUDED
177
+ if s["primary"] == c:
178
+ return RANK_PRIMARY
179
+ return RANK_SECONDARY
180
+
181
+
182
+ # 生效条件:position、a、b 经 rank 可得序秩,若 rank(position,a)==rank(position,b) 返回 None,否则返回 ra>rb 的 bool;任一参数未知则抛 WeightError;
183
+ def prefers(position: str, a: str, b: str):
184
+ """该位置是否**严格**偏好 a 胜过 b。
185
+
186
+ 返回 True / False / None(None = 二者并列,无严格序)。
187
+ """
188
+ ra, rb = rank(position, a), rank(position, b)
189
+ if ra == rb:
190
+ return None
191
+ return ra > rb
192
+
193
+
194
+ # 生效条件:position 经 _spec 验证且 COMPONENTS 可用时,按 -rank(position,c) 与 COMPONENTS.index(c) 排序,返回 {"preferred":[rank>0], "excluded":[rank==0], "primary":dominant(position), "secondary":secondary(position)}(无 dominant 键);
195
+ def order(position: str):
196
+ """从高到低排列的分量(并列者按 COMPONENTS 稳定序);排除项单独列出。"""
197
+ _spec(position)
198
+ ranked = sorted(COMPONENTS, key=lambda c: (-rank(position, c), COMPONENTS.index(c)))
199
+ kept = [c for c in ranked if rank(position, c) > 0]
200
+ exc = [c for c in ranked if rank(position, c) == 0]
201
+ return {"preferred": kept, "excluded": exc,
202
+ "primary": dominant(position), "secondary": secondary(position)}
203
+
204
+
205
+ # 生效条件:position 经 _spec 验证且 COMPONENTS 可用时,将各分量 rank 归一化到 4 位小数返回;若 total 为 0 则按 total=1.0 计算;
206
+ def weights(position: str) -> dict:
207
+ """占位数值权重(序秩归一化);**未标定**,仅供仿真。"""
208
+ _spec(position)
209
+ raw = {c: rank(position, c) for c in COMPONENTS}
210
+ total = sum(raw.values()) or 1.0
211
+ return {c: round(raw[c] / total, 4) for c in COMPONENTS}
212
+
213
+
214
+ # --------------------------------------------------------------------------
215
+ # 合成(占位:f(一致性, 位置可预测性, 版本对齐度))
216
+ # --------------------------------------------------------------------------
217
+
218
+ # 生效条件:position 经 _spec 可解析且 COMPONENTS 可用时,components 对每个分量经 .get(c,0.0) 取值并截到 [0,1];若 components 无 .get 或该值不能 float 则该分量值记 0.0,仍按 weights(position) 加权求和返回含 score/values/weights/dominant 的 dict;
219
+ def blend(position: str, components: dict) -> dict:
220
+ """按位置的权重合成单一信任标量。
221
+
222
+ `components` 形如 {"version_alignment": 1.0, "predictability": 0.8,
223
+ "consistency": 0.6};缺省按 0 计。
224
+
225
+ **数值未标定**:这里用序秩当量级,只保证「主导分量对结果影响最大」这
226
+ 一方向性事实。标定属路线图 v0.3。
227
+ """
228
+ s = _spec(position)
229
+ w = weights(position)
230
+ vals = {}
231
+ for c in COMPONENTS:
232
+ try:
233
+ vals[c] = max(0.0, min(1.0, float(components.get(c, 0.0))))
234
+ except (AttributeError, TypeError, ValueError):
235
+ vals[c] = 0.0
236
+ score = sum(w[c] * vals[c] for c in COMPONENTS)
237
+ return {"position": position, "class": s["class"],
238
+ "weights": w, "values": vals, "score": round(score, 4),
239
+ "dominant": s["primary"],
240
+ "note": "权重为占位序秩(未标定);仅方向性结论可信"}
241
+
242
+
243
+ # --------------------------------------------------------------------------
244
+ # 不变量自检(把上轮的三条结构约束落成可执行断言)
245
+ # --------------------------------------------------------------------------
246
+
247
+ # 生效条件:模块级 POSITION_ORDER、COMPONENTS、SPEC、VIEWPOINT、FUNCTIONAL 等常量齐备时,invariants() 逐条自检并返回 {"ok": all(checks[*].ok), "checks": ...};
248
+ def invariants() -> dict:
249
+ """逐条自检结构约束,返回 {ok, checks{name: {ok, detail}}}。"""
250
+ checks = {}
251
+
252
+ # ① 排除唯一:每分量恰被 1 个位置排除 ⇒ 参与度 = 5/6
253
+ part = {c: [p for p in POSITION_ORDER if rank(p, c) > 0] for c in COMPONENTS}
254
+ exc_of = {c: [p for p in POSITION_ORDER if rank(p, c) == 0] for c in COMPONENTS}
255
+ checks["excluded_exactly_once"] = {
256
+ "ok": all(len(exc_of[c]) == 1 for c in COMPONENTS)
257
+ and all(len(part[c]) == len(POSITION_ORDER) - 1 for c in COMPONENTS),
258
+ "detail": {c: {"participants": len(part[c]), "excluded_by": exc_of[c]}
259
+ for c in COMPONENTS}}
260
+
261
+ # ② 排除项只属功能单元,且每分量各被排除一次
262
+ excs = {}
263
+ for p in POSITION_ORDER:
264
+ e = excluded(p)
265
+ if e is None:
266
+ continue
267
+ excs.setdefault(e, []).append(p)
268
+ only_functional = all(is_functional(p)
269
+ for vs in excs.values() for p in vs)
270
+ both = all(len(excs.get(c) or []) == 1 for c in COMPONENTS)
271
+ checks["exclusion_functional_only_once_each"] = {
272
+ "ok": bool(excs) and only_functional and both,
273
+ "detail": {c: excs.get(c, []) for c in COMPONENTS}}
274
+
275
+ # ③ 视角主导 ↔ 功能单元排除 一一配对
276
+ dom_pairs = {} # 分量 → 视角
277
+ for p in POSITION_ORDER:
278
+ if is_viewpoint(p):
279
+ dom_pairs.setdefault(dominant(p), []).append(p)
280
+ pairing = {}
281
+ for p in POSITION_ORDER:
282
+ if not is_functional(p):
283
+ continue
284
+ e = excluded(p)
285
+ if e and len(dom_pairs.get(e) or []) == 1:
286
+ pairing[dom_pairs[e][0]] = p
287
+ checks["viewpoint_dominant_pairs_unit_exclusion"] = {
288
+ "ok": len(pairing) == 3,
289
+ "detail": pairing}
290
+
291
+ # ④ 功能单元恰有一个排除项;视角一个都没有
292
+ checks["functional_excludes_exactly_one"] = {
293
+ "ok": all(excluded(p) is not None for p in POSITION_ORDER
294
+ if is_functional(p))
295
+ and all(excluded(p) is None for p in POSITION_ORDER
296
+ if is_viewpoint(p)),
297
+ "detail": {p: excluded(p) for p in POSITION_ORDER}}
298
+
299
+ # ⑤ 排除项序秩严格最低
300
+ checks["excluded_is_lowest"] = {
301
+ "ok": all(rank(p, excluded(p)) < rank(p, c)
302
+ for p in POSITION_ORDER if excluded(p)
303
+ for c in COMPONENTS if c != excluded(p)),
304
+ "detail": "排除分量序秩 0,严格低于其余分量"}
305
+
306
+ return {"ok": all(c["ok"] for c in checks.values()), "checks": checks}
307
+
308
+
309
+ # --------------------------------------------------------------------------
310
+ # 自描述
311
+ # --------------------------------------------------------------------------
312
+
313
+ # 生效条件:模块级 COMPONENTS、COMPONENT_LABELS、VIEWPOINT、FUNCTIONAL、SPEC、POSITION_ORDER、DAILY 齐备时,返回位置权重矩阵自描述 dict,其中 components 按 COMPONENT_LABELS、positions 按 SPEC、daily_eval 按 DAILY 展开;
314
+ def catalog() -> dict:
315
+ """位置权重矩阵自描述(供 MCP / 文档对照验证)。"""
316
+ return {
317
+ "question": "不同身份,参数偏好不同",
318
+ "components": {c: COMPONENT_LABELS[c] for c in COMPONENTS},
319
+ "classes": {
320
+ VIEWPOINT: "认知视角:3 项都涉及,只标主导,不设零",
321
+ FUNCTIONAL: "功能单元:身份即排除(不猜/不记/不对外)",
322
+ },
323
+ "positions": {p: dict(SPEC[p]) for p in POSITION_ORDER},
324
+ "daily_eval": list(DAILY),
325
+ "meta_reference": [p for p in POSITION_ORDER if p not in DAILY],
326
+ "derivation": {"record": "不猜 → 排除 位置可预测性",
327
+ "output": "不记 → 排除 一致性",
328
+ "sustain": "不对外 → 排除 版本对齐度"},
329
+ "formula": "P_trust = f(一致性, 位置可预测性, 版本对齐度)",
330
+ "honest_boundary": "只声明序,不宣称数值;blend() 权重为占位序秩",
331
+ }
332
+
333
+
334
+ # ==========================================================================
335
+ # 结构重要性重算(maintain.importance)
336
+ # ==========================================================================
337
+ #
338
+ # 与上面的「位置权重矩阵」是**两件事**,不要混:
339
+ # · 上面回答「不同身份偏好哪些信任分量」(序标定,作用于位置);
340
+ # · 下面回答「一个节点在认知图结构里该有多大重要性」(重算,作用于节点)。
341
+ #
342
+ # 三个**可测**分量(对齐计划:按覆盖度、冗余度、验证基底重排):
343
+ # coverage 覆盖度 —— 有多少节点指向/包含它(入度,结构中枢性)
344
+ # redundancy 冗余度 —— 与同层节点内容哈希撞车的比例(越冗余越该降权)
345
+ # basis 验证基底 —— 外部验证档位(formal_proof/compiler > test > measurement > data > other)
346
+ #
347
+ # 纪律:只改 `frontmatter.importance` 与索引快照,**不动正文**(减小 diff 与重写成本);
348
+ # 全流程可预演(apply=False)、可留痕(`_maintain.jsonl`)、可回滚(rollback)。
349
+ # 性能:dry-run 不读任何节点文件(只用索引快照);apply 只写「变动超阈值」的节点。
350
+
351
+ MAINTAIN_LOG = "_maintain.jsonl"
352
+
353
+ #: 验证基底 → 可信权重(枚举来自 nodefile.VERIFICATION_BASIS)
354
+ #: 文科来源一致性档(textbook/public_kb)低于可复现档、高于 other:
355
+ #: 教材/公开知识库是权威二手来源,但不是可复算证据。
356
+ BASIS_TRUST = {
357
+ "formal_proof": 1.00,
358
+ "compiler": 0.90,
359
+ "test": 0.80,
360
+ "measurement": 0.65,
361
+ "textbook": 0.60,
362
+ "data": 0.45,
363
+ "public_kb": 0.40,
364
+ "other": 0.25,
365
+ }
366
+ BASIS_TRUST_MISSING = 0.25
367
+
368
+ #: 三分量合成权重(占位量级,沿用「只声明序」纪律;分量序主导→次要)
369
+ IMPORTANCE_MIX = {"basis": 0.40, "coverage": 0.35, "redundancy": 0.25}
370
+ COV_SAT = 12.0 # 入度饱和点:约 12 条入边即视为覆盖满
371
+ IMPORTANCE_FLOOR = 0.10
372
+ IMPORTANCE_CEIL = 1.00
373
+ IMPORTANCE_PROTECT = 0.70 # ≥ 此值:保护下限(已保护/高危节点不被降破)
374
+ APPLY_DELTA = 0.05 # 变动小于此值不写盘(防 IO 放大)
375
+
376
+
377
+ # 生效条件:e 为可 .get 的映射时,遍历 e.get("edges")(假值视为空)逐项按 dict 的 to/target/node/id 或标量转 str 追加非假值,再取 e.get("subgraph"),若为 dict 取其 "nodes" 否则取 subgraph 本身,且当其为 list/tuple 时追加其中 dict 的 id 或标量转 str 的非假值,返回 out;
378
+ def _targets(e: dict):
379
+ """索引快照里的「出边目标」:edges ∪ subgraph.nodes(兼容 dict/str)。"""
380
+ out = []
381
+ for x in (e.get("edges") or []):
382
+ if isinstance(x, dict):
383
+ t = x.get("to") or x.get("target") or x.get("node") or x.get("id")
384
+ else:
385
+ t = x
386
+ if t:
387
+ out.append(str(t))
388
+ sg = e.get("subgraph")
389
+ raw = sg.get("nodes") if isinstance(sg, dict) else sg
390
+ if isinstance(raw, (list, tuple)):
391
+ for n in raw:
392
+ nid = n.get("id") if isinstance(n, dict) else n
393
+ if nid:
394
+ out.append(str(nid))
395
+ return out
396
+
397
+
398
+ # 生效条件:basis 为 None 时返回 BASIS_TRUST_MISSING;否则把 str(basis).strip().lower() 作为 BASIS_TRUST 的键,命中返回对应权重,未命中(含空串)返回 BASIS_TRUST_MISSING;
399
+ def basis_trust(basis):
400
+ """验证基底 → 可信权重;缺失/未知一律给最低档(不假装可信)。"""
401
+ if basis is None:
402
+ return BASIS_TRUST_MISSING
403
+ return BASIS_TRUST.get(str(basis).strip().lower(), BASIS_TRUST_MISSING)
404
+
405
+
406
+ # 生效条件:cg 的 index(getattr(cg,"index",None) or {})与其 "nodes" 同为真值时,返回以这些节点 id 为键的入度表,仅当 _targets(e) 给出的目标 t 也在 nodes 且 t != nid 时计数 +1;index 或 "nodes" 为假值(None/{})时 nodes 回落 {},直接返回空 dict。
407
+ def coverage_index(cg) -> dict:
408
+ """入度表:nid → 被多少节点指向(覆盖度,O(N) 免读文件)。"""
409
+ nodes = (getattr(cg, "index", None) or {}).get("nodes") or {}
410
+ indeg = {nid: 0 for nid in list(nodes)}
411
+ for nid, e in list(nodes.items()):
412
+ for t in _targets(e):
413
+ if t in indeg and t != nid:
414
+ indeg[t] += 1
415
+ return indeg
416
+
417
+
418
+ # 生效条件:cg.index 的 "nodes" 为 dict 时,layer 真值仅处理 e.get("layer")==layer 的节点,layer 假值不过滤;content_hash 为假值(None/空串/0 等)的节点输出 0.0,同 (e.get("layer"), content_hash) 第二次撞车时当前与首次节点均输出 1.0,否则输出 0.0;
419
+ def redundancy_map(cg, layer=None) -> dict:
420
+ """同层内容哈希撞车 → 冗余度 1.0,否则 0.0(精确冗余,O(N) 免读文件)。
421
+
422
+ 诚实边界:这是**精确冗余**(同一层出现完全相同的正文),不是语义近似冗余。
423
+ 语义近似由 `forgetting.redundancy`(bigram 覆盖)在写入侧单独负责。
424
+ """
425
+ nodes = (getattr(cg, "index", None) or {}).get("nodes") or {}
426
+ seen, out = {}, {}
427
+ for nid, e in list(nodes.items()):
428
+ if layer and e.get("layer") != layer:
429
+ continue
430
+ h = e.get("content_hash") or ""
431
+ if not h:
432
+ out[nid] = 0.0
433
+ continue
434
+ key = (e.get("layer"), h)
435
+ if key in seen:
436
+ out[nid] = 1.0
437
+ out[seen[key]] = 1.0
438
+ else:
439
+ seen[key] = nid
440
+ out.setdefault(nid, 0.0)
441
+ return out
442
+
443
+
444
+ # 生效条件:cg.index 的 nodes 为 dict 且 entry 非 None 或 nodes.get(nid) 命中时,e 按 entry 优先否则 nodes.get(nid);indeg/red 分别仅在为 None 时回落 coverage_index(cg)/redundancy_map(cg, layer=e.get("layer"))(空容器不回落);按 IMPORTANCE_MIX、COV_SAT 及 e.get("verification_basis") 的 basis_trust 计算 coverage/redundancy/basis 分量,按 bool(e.get("protected")) 选择 IMPORTANCE_PROTECT 或 IMPORTANCE_FLOOR 作下限、IMPORTANCE_CEIL 作上限,返回含 before(e.get("importance",0.5) or 0.5)、after、components、protected 的 dict;entry 与 nodes.get(nid) 均为 None 返回 None;
445
+ def node_importance(cg, nid, indeg=None, red=None, entry=None):
446
+ """单节点结构重要性分解(可审计:给出分量而非只给分)。"""
447
+ nodes = (getattr(cg, "index", None) or {}).get("nodes") or {}
448
+ e = entry if entry is not None else nodes.get(nid)
449
+ if e is None:
450
+ return None
451
+ indeg = indeg if indeg is not None else coverage_index(cg)
452
+ red = red if red is not None else redundancy_map(cg, layer=e.get("layer"))
453
+ d = int(indeg.get(nid, 0) or 0)
454
+ cov = min(1.0, math.log1p(d) / math.log1p(COV_SAT))
455
+ rr = max(0.0, min(1.0, float(red.get(nid, 0.0) or 0.0)))
456
+ bt = basis_trust(e.get("verification_basis"))
457
+ score = (IMPORTANCE_MIX["basis"] * bt
458
+ + IMPORTANCE_MIX["coverage"] * cov
459
+ + IMPORTANCE_MIX["redundancy"] * (1.0 - rr))
460
+ # #431:此前只认 `protected` 位(窄:漏层保护与按分保护)⇒ self/anchor 节点被降破。
461
+ # 走条目口径单点 protect.is_protected_entry(与 is_protected 同一个函数)。
462
+ protected = protect.is_protected_entry(e)[0]
463
+ floor = IMPORTANCE_PROTECT if protected else IMPORTANCE_FLOOR
464
+ score = max(floor, min(IMPORTANCE_CEIL, score))
465
+ return {"node_id": nid,
466
+ "before": float(e.get("importance", 0.5) or 0.5),
467
+ "after": round(score, 4),
468
+ "components": {"coverage": round(cov, 4), "redundancy": round(rr, 4),
469
+ "basis_trust": round(bt, 4), "in_degree": d,
470
+ "verification_basis": e.get("verification_basis")},
471
+ "protected": protected,
472
+ "protected_floor": protected}
473
+
474
+
475
+ # 生效条件:给定 cg 且 nodes = cg.index["nodes"] 时按 layer 过滤、limit 为真值才 ids = ids[:int(limit)] 逐节点重算,abs(after-before) < float(min_delta) 记 unchanged(**预演面不接档位闸**);仅 apply=True 才在**任何写盘之前**先经 autonomy_modes.e_gate() 档位判定(补强批次 v1.2·E 面接线)——非 ALLOW(plan 档无计划)即经 forbidden_result() fail-closed 早退(零写盘、零审计行),ALLOW(confirm/full)才把 frontmatter.importance/importance_source/importance_components 写回(after >= IMPORTANCE_PROTECT 且未 protected 时补写 protected/protection_reason),并向 cg.root 下 append_jsonl(..., MAINTAIN_LOG) 记 batch 后 rebuild_index。
476
+ def recalc(cg, layer=None, limit=None, apply=False, min_delta=APPLY_DELTA,
477
+ actor="maintain", dry_run_samples=10):
478
+ """结构重要性重算:覆盖度 + 冗余度 + 验证基底 → 重排节点重要性。
479
+
480
+ apply=False(默认)只出报表,不写盘(对应计划「可预演」)。
481
+ apply=True 逐节点改写 `frontmatter.importance`,每批写一条 `_maintain.jsonl`
482
+ (before/after/分量/actor),并提供 rollback 反向应用。
483
+
484
+ **档位闸(补强批次 v1.2·E 面接线)**:`apply=True` 的**写盘前**先经
485
+ `autonomy_modes.e_gate()`(设计 §三 plan 档 E「须命中计划步骤」)——plan 档
486
+ 无计划 ⇒ `forbidden_result()` fail-closed 早退(零写盘、零审计行);confirm/
487
+ full ⇒ ALLOW、**原链原样**(逐位不变)。`apply=False`(预演)不接闸。
488
+ """
489
+ nodes = (getattr(cg, "index", None) or {}).get("nodes") or {}
490
+ indeg = coverage_index(cg)
491
+ red = redundancy_map(cg, layer=layer)
492
+ ids = [nid for nid, e in list(nodes.items())
493
+ if not layer or e.get("layer") == layer]
494
+ ids.sort()
495
+ if limit:
496
+ ids = ids[:int(limit)]
497
+ t0 = time.time()
498
+ planned, changed, unchanged, skipped = [], 0, 0, 0
499
+ before_sum = after_sum = 0.0
500
+ by_layer = {}
501
+ batch = time.strftime("%Y%m%d-%H%M%S")
502
+ samples = []
503
+ for nid in ids:
504
+ info = node_importance(cg, nid, indeg=indeg, red=red)
505
+ if info is None:
506
+ skipped += 1
507
+ continue
508
+ before, after = info["before"], info["after"]
509
+ before_sum += before
510
+ after_sum += after
511
+ lay = (nodes.get(nid) or {}).get("layer") or "?"
512
+ bl = by_layer.setdefault(lay, {"nodes": 0, "changed": 0,
513
+ "avg_before": 0.0, "avg_after": 0.0})
514
+ bl["nodes"] += 1
515
+ if abs(after - before) < float(min_delta):
516
+ unchanged += 1
517
+ continue
518
+ changed += 1
519
+ bl["changed"] += 1
520
+ plan = dict(info)
521
+ plan["batch"] = batch
522
+ plan["delta"] = round(after - before, 4)
523
+ planned.append(plan)
524
+ if len(samples) < int(dry_run_samples):
525
+ samples.append(plan)
526
+ for lay, bl in list(by_layer.items()):
527
+ bl["avg_before"] = 0.0
528
+ bl["avg_after"] = 0.0
529
+ written = 0
530
+ if apply:
531
+ # 三档自治(补强批次 v1.2·E 面接线,设计 §三 plan 档 E「须命中计划步骤」):
532
+ # E 类动作**写盘前**的档位判定单点(同 `freshness.recalc` 口径)——
533
+ # 本函数无独立资格闸,闸口取「进入写盘支路的第一行」=任何盘面写入之前;
534
+ # ALLOW(confirm/full)原链原样,plan 档无计划 ⇒ fail-closed 早退。
535
+ from . import autonomy_modes as _am
536
+ _dec = _am.e_gate()
537
+ if _dec["decision"] != _am.ALLOW:
538
+ return _am.forbidden_result(_dec, action="importance", dry_run=False,
539
+ written=0, batch=batch)
540
+ for plan in planned:
541
+ nid = plan["node_id"]
542
+ node = cg.get(nid)
543
+ if not node:
544
+ continue
545
+ fm = node.get("frontmatter") or {}
546
+ fm["importance"] = plan["after"]
547
+ fm["importance_source"] = "recalc"
548
+ fm["importance_components"] = plan["components"]
549
+ if plan["after"] >= IMPORTANCE_PROTECT and not fm.get("protected"):
550
+ fm["protected"] = True
551
+ fm["protection_reason"] = (f"recalc importance={plan['after']:.2f}"
552
+ f"≥{IMPORTANCE_PROTECT}")
553
+ path = os.path.join(cg.root, (nodes.get(nid) or {}).get("path")
554
+ or node.get("path") or f"{nid}.md")
555
+ cg._write_node(nid, path, fm, node.get("content") or "")
556
+ e = nodes.get(nid)
557
+ if e is not None:
558
+ e["importance"] = plan["after"]
559
+ if fm.get("protected"):
560
+ e["protected"] = True
561
+ e["protection_reason"] = fm["protection_reason"]
562
+ rec = {"t": time.time(), "action": "importance", "batch": batch,
563
+ "id": nid, "layer": (e or {}).get("layer"),
564
+ "before": plan["before"], "after": plan["after"],
565
+ "delta": plan["delta"], "components": plan["components"],
566
+ "actor": actor}
567
+ append_jsonl(os.path.join(cg.root, MAINTAIN_LOG), rec)
568
+ written += 1
569
+ if written and hasattr(cg, "rebuild_index"):
570
+ cg.rebuild_index() # 索引与盘面重新对齐,防内存/磁盘分叉
571
+ n = max(1, len(ids))
572
+ return {
573
+ "ok": True, "action": "importance", "dry_run": not apply,
574
+ "batch": batch, "nodes_scanned": len(ids), "changed": changed,
575
+ "unchanged": unchanged, "skipped": skipped, "written": written,
576
+ "min_delta": float(min_delta),
577
+ "avg_before": round(before_sum / n, 4),
578
+ "avg_after": round(after_sum / n, 4),
579
+ "by_layer": {k: {"nodes": v["nodes"], "changed": v["changed"]}
580
+ for k, v in by_layer.items()},
581
+ "samples": samples, "elapsed_ms": int((time.time() - t0) * 1000),
582
+ "log": MAINTAIN_LOG,
583
+ "note": ("dry-run:未写盘;apply=True 才改写 importance"
584
+ if not apply else
585
+ f"已改写 {written} 个节点;回滚见 rollback(batch={batch})"),
586
+ }
587
+
588
+
589
+ # 生效条件:cg.root/MAINTAIN_LOG 可读出 action=="importance" 记录后,entry_ids 为真值时按记录 id 是否在 {str(x) for x in entry_ids} 过滤;否则 batch 为真值时按 r.get("batch")==batch 过滤;否则取最后一条记录的 batch 再按其过滤;无 importance 记录返回 {"ok":False,"error":"no_records","reverted":0},过滤后无记录返回 {"ok":False,"error":"batch_not_found",...};对命中且 cg.get(nid) 为真的记录写回 rec.get("before") 并计数,若 reverted 且 cg 有 rebuild_index 则调用,最后 append_jsonl 写 actor 并返回 {"ok":True,"batch":batch,"reverted":reverted,"ids":ids};
590
+ def rollback(cg, batch=None, entry_ids=None, actor="maintain"):
591
+ """把重要性重算反向应用(bulk 改写的可回滚兑现)。
592
+
593
+ batch 为空则回滚**最近一批**。返回 {ok, reverted, batch, ids}。
594
+ """
595
+ log_path = os.path.join(cg.root, MAINTAIN_LOG)
596
+ records = [r for r in read_jsonl(log_path)
597
+ if r.get("action") == "importance"]
598
+ if not records:
599
+ return {"ok": False, "error": "no_records", "reverted": 0}
600
+ if entry_ids:
601
+ want = {str(x) for x in entry_ids}
602
+ records = [r for r in records if str(r.get("id")) in want]
603
+ elif batch:
604
+ records = [r for r in records if r.get("batch") == batch]
605
+ else:
606
+ batch = records[-1].get("batch")
607
+ records = [r for r in records if r.get("batch") == batch]
608
+ if not records:
609
+ return {"ok": False, "error": "batch_not_found", "batch": batch,
610
+ "reverted": 0}
611
+ nodes = (getattr(cg, "index", None) or {}).get("nodes") or {}
612
+ reverted, ids = 0, []
613
+ # 同一节点多条记录时以最早一条的 before 为准
614
+ for rec in records:
615
+ nid = rec.get("id")
616
+ node = cg.get(nid)
617
+ if not node:
618
+ continue
619
+ fm = node.get("frontmatter") or {}
620
+ fm["importance"] = rec.get("before")
621
+ fm["importance_source"] = "recalc_rollback"
622
+ path = os.path.join(cg.root, (nodes.get(nid) or {}).get("path")
623
+ or node.get("path") or f"{nid}.md")
624
+ cg._write_node(nid, path, fm, node.get("content") or "")
625
+ e = nodes.get(nid)
626
+ if e is not None:
627
+ e["importance"] = rec.get("before")
628
+ reverted += 1
629
+ ids.append(nid)
630
+ if reverted and hasattr(cg, "rebuild_index"):
631
+ cg.rebuild_index()
632
+ append_jsonl(log_path, {"t": time.time(), "action": "importance_rollback",
633
+ "batch": batch, "reverted": reverted, "actor": actor})
634
+ return {"ok": True, "batch": batch, "reverted": reverted, "ids": ids}
635
+
636
+
637
+ # 生效条件:cg.root/MAINTAIN_LOG 可读出 JSONL 记录后,action 为真值时按 r.get("action")==action 过滤,假值不过滤;返回 recs[-int(limit):],其中 limit=0 时 int(0)=0 使切片为 recs[0:] 返回全部而非空;
638
+ def history(cg, limit=100, action=None):
639
+ """维护留痕(最近 limit 条),可按 action 过滤。"""
640
+ recs = list(read_jsonl(os.path.join(cg.root, MAINTAIN_LOG)))
641
+ if action:
642
+ recs = [r for r in recs if r.get("action") == action]
639
643
  return recs[-int(limit):]