@furongjun1999/dsh-memory 0.9.6 → 0.9.8

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 (233) hide show
  1. package/README.md +150 -662
  2. package/codebuddy/CODEBUDDY.md +333 -204
  3. package/data/policy.json +38 -38
  4. package/docs/README.md +18 -0
  5. package/docs/discipline/harnesses.yaml +11 -3
  6. package/docs/discipline/templates/compact.txt.tmpl +20 -18
  7. package/docs/discipline/templates/full.md.tmpl +63 -61
  8. package/docs/discipline/templates/rules.mdc.tmpl +5 -3
  9. package/docs/discipline/templates/skill.md.tmpl +25 -23
  10. package/docs/discipline/templates/spec.txt.tmpl +2 -2
  11. package/docs/discipline/templates/zcode-user.md.tmpl +1 -1
  12. package/dsh/cordis.yml.example +139 -139
  13. package/lib/hooks.d.ts +19 -0
  14. package/lib/hooks.js +30 -7
  15. package/lib/index.js +18 -3
  16. package/lib/init.js +19 -4
  17. package/lib/lib/hook_audit.js +64 -2
  18. package/lib/lib/prompt_safety.js +14 -3
  19. package/lib/tools.d.ts +7 -0
  20. package/lib/tools.js +22 -1
  21. package/md_cg/_guard_kit.py +121 -121
  22. package/md_cg/anchor.py +114 -114
  23. package/md_cg/audit.py +15 -2
  24. package/md_cg/chain.py +3 -2
  25. package/md_cg/config_registry_guard.py +487 -487
  26. package/md_cg/conformance.py +747 -747
  27. package/md_cg/consolidate.py +1793 -1793
  28. package/md_cg/crypto.py +794 -794
  29. package/md_cg/hyperedge.py +262 -262
  30. package/md_cg/interface_schema.py +146 -146
  31. package/md_cg/lifecycle.py +426 -426
  32. package/md_cg/mdcos.py +12 -8
  33. package/md_cg/predict.py +1103 -1103
  34. package/md_cg/protect.py +654 -654
  35. package/md_cg/provenance.py +606 -606
  36. package/md_cg/queue_health.py +130 -130
  37. package/md_cg/refine.py +613 -613
  38. package/md_cg/retrieve.py +128 -128
  39. package/md_cg/revise.py +140 -140
  40. package/md_cg/rollback.py +657 -657
  41. package/md_cg/rollback_cli.py +124 -124
  42. package/md_cg/rotate.py +346 -346
  43. package/md_cg/routing.py +490 -490
  44. package/md_cg/self_surface.py +50 -50
  45. package/md_cg/semantic/canonical.py +144 -144
  46. package/md_cg/semantic/zh_en_atoms.py +141 -141
  47. package/md_cg/sources.py +1091 -1091
  48. package/md_cg/stg.py +3 -2
  49. package/md_cg/sustain.py +10 -1
  50. package/md_cg/test_audit_rotate_parity.py +279 -279
  51. package/md_cg/test_autonomy_modes.py +2013 -2013
  52. package/md_cg/test_cn_recall_grams.py +206 -206
  53. package/md_cg/test_code_kind_forbidden.py +101 -0
  54. package/md_cg/test_condaccept.py +156 -156
  55. package/md_cg/test_condgate.py +141 -141
  56. package/md_cg/test_heartbeat_ledger.py +9 -3
  57. package/md_cg/test_i50e_readside_protection.py +658 -658
  58. package/md_cg/test_issue111_gate_order.py +76 -76
  59. package/md_cg/test_issue111_scoring_contract.py +86 -86
  60. package/md_cg/test_issue121_calibration_bounds.py +293 -293
  61. package/md_cg/test_issue145_sleep_no_silent_rollback.py +167 -167
  62. package/md_cg/test_issue147_pack_id_escape.py +378 -378
  63. package/md_cg/test_issue148_merge_downgrade_leak.py +192 -192
  64. package/md_cg/test_issue149_accel_default_face.py +248 -248
  65. package/md_cg/test_issue150_empty_query_terms.py +328 -328
  66. package/md_cg/test_issue151_153_157_write_cache_index.py +151 -151
  67. package/md_cg/test_issue152_sealed_postings.py +309 -309
  68. package/md_cg/test_issue153b_index_generation_invalidation.py +142 -142
  69. package/md_cg/test_issue157b_index_remainder.py +344 -344
  70. package/md_cg/test_issue189_conclusion_bidirectional.py +223 -223
  71. package/md_cg/test_issue191_import_trust.py +382 -382
  72. package/md_cg/test_issue193_write_scope.py +378 -378
  73. package/md_cg/test_issue212_writeface_gaps.py +349 -349
  74. package/md_cg/test_issue214_reject_sensitivity.py +266 -266
  75. package/md_cg/test_issue215_condition_source_gap.py +272 -272
  76. package/md_cg/test_issue216_clash_routes.py +214 -214
  77. package/md_cg/test_issue217_slot_routes.py +248 -248
  78. package/md_cg/test_issue218_postcondition_value_blind.py +222 -222
  79. package/md_cg/test_issue258_pack_sensitivity.py +468 -468
  80. package/md_cg/test_issue265_coldverify_multiproc.py +438 -438
  81. package/md_cg/test_issue274_read_node_id_none.py +341 -341
  82. package/md_cg/test_issue275_layer_param.py +250 -250
  83. package/md_cg/test_issue277_node_id_traversal.py +384 -384
  84. package/md_cg/test_issue278_neg_separated_tension.py +271 -271
  85. package/md_cg/test_issue278_neg_window.py +294 -294
  86. package/md_cg/test_issue278_negative_layering.py +83 -83
  87. package/md_cg/test_issue284_skip_not_pass.py +291 -291
  88. package/md_cg/test_issue294_flywheel_sensitivity.py +573 -573
  89. package/md_cg/test_issue301_baseline_corpus_gate.py +245 -245
  90. package/md_cg/test_issue303_design_boundaries.py +262 -262
  91. package/md_cg/test_issue315_merge_branch_sensitivity.py +383 -383
  92. package/md_cg/test_issue319_tenant_unregistered_bypass.py +256 -256
  93. package/md_cg/test_issue321_mcp_deep_json.py +304 -304
  94. package/md_cg/test_issue322_node_write_lock.py +727 -727
  95. package/md_cg/test_issue324_write_size_fields.py +214 -214
  96. package/md_cg/test_issue325_audit_poison.py +229 -229
  97. package/md_cg/test_issue326_ingest_auto_whitelist.py +331 -331
  98. package/md_cg/test_issue326_key_perms.py +355 -355
  99. package/md_cg/test_issue326_twin_parser_parity.py +323 -323
  100. package/md_cg/test_issue326_vm_call_frame.py +569 -569
  101. package/md_cg/test_issue345_writelimit_effective_sens.py +233 -233
  102. package/md_cg/test_issue346_overwrite_sensitivity_gate.py +424 -424
  103. package/md_cg/test_issue348_insight_verify_op.py +199 -199
  104. package/md_cg/test_issue348_merge_rmw_lock.py +345 -345
  105. package/md_cg/test_issue350_casefold_collision.py +235 -235
  106. package/md_cg/test_issue351_ingest_root_fallback.py +201 -201
  107. package/md_cg/test_issue352_imgaudit.py +418 -418
  108. package/md_cg/test_issue352_pathsjson.py +537 -537
  109. package/md_cg/test_issue352_refindex.py +412 -412
  110. package/md_cg/test_issue352_rollback.py +604 -604
  111. package/md_cg/test_issue354_p2_writelimit_toctou.py +327 -327
  112. package/md_cg/test_issue354_rust_index_containment.py +571 -571
  113. package/md_cg/test_issue354_rust_wal_containment.py +606 -606
  114. package/md_cg/test_issue358_truncation_observable.py +247 -247
  115. package/md_cg/test_issue360_tidy_private_excerpt.py +195 -195
  116. package/md_cg/test_issue361_blindspot_not_silent_drop.py +494 -494
  117. package/md_cg/test_issue362_neg_condition.py +468 -468
  118. package/md_cg/test_issue363_lexical_idf.py +275 -275
  119. package/md_cg/test_issue370_readcache_assembly.py +488 -488
  120. package/md_cg/test_issue376_heartbeat_failed.py +293 -293
  121. package/md_cg/test_issue377_layer_protect_stamp.py +324 -324
  122. package/md_cg/test_issue378_protected_floor.py +310 -310
  123. package/md_cg/test_issue383_state_event_write_gate.py +462 -462
  124. package/md_cg/test_issue397_sens_eff_wiring.py +435 -435
  125. package/md_cg/test_issue398_toolface_op_parity.py +511 -511
  126. package/md_cg/test_issue399_hive_json_shape.py +317 -317
  127. package/md_cg/test_issue401_wisdom_codegen.py +295 -295
  128. package/md_cg/test_issue404_heal_receipt.py +389 -389
  129. package/md_cg/test_issue409_compact_index_completeness.py +509 -509
  130. package/md_cg/test_issue410_verify_demote_write.py +366 -366
  131. package/md_cg/test_issue411_scrub_idempotence.py +575 -575
  132. package/md_cg/test_issue412_discipline_producer_parity.py +311 -311
  133. package/md_cg/test_issue413_preimage_failclosed.py +239 -239
  134. package/md_cg/test_issue413_preimage_twin.py +339 -339
  135. package/md_cg/test_issue415_deps_gate_probe.py +357 -357
  136. package/md_cg/test_issue416_entry_key_subset.py +525 -525
  137. package/md_cg/test_issue417_twophase_merge_landed.py +423 -423
  138. package/md_cg/test_issue425_ingest_library_path.py +331 -331
  139. package/md_cg/test_issue426c_enqueue_not_silent.py +346 -346
  140. package/md_cg/test_issue426e_restore_overwrite_live.py +310 -310
  141. package/md_cg/test_issue426f_paths_gbk_failclosed.py +301 -301
  142. package/md_cg/test_issue427_serve_status_shape.py +191 -191
  143. package/md_cg/test_issue431_432_468_protect_single_point.py +322 -322
  144. package/md_cg/test_issue433_outlook_threshold_single_point.py +278 -278
  145. package/md_cg/test_issue434_435_float_gate.py +90 -90
  146. package/md_cg/test_issue439_sibling_sensitivity_inherit.py +448 -448
  147. package/md_cg/test_issue43_default_policy.py +874 -874
  148. package/md_cg/test_issue444_scrub_evidence_plaintext.py +281 -281
  149. package/md_cg/test_issue445_metacognition_ledger_gate.py +454 -454
  150. package/md_cg/test_issue446_453_403_verified_evidence.py +382 -382
  151. package/md_cg/test_issue450_sensitivity_fallback_reason.py +51 -51
  152. package/md_cg/test_issue450_writegate_unknown_sens.py +203 -203
  153. package/md_cg/test_issue451_audit_actor.py +313 -313
  154. package/md_cg/test_issue452_ingest_root_sensitivity.py +299 -299
  155. package/md_cg/test_issue463_ghost_prune_guard.py +259 -259
  156. package/md_cg/test_issue466_basis_registry.py +469 -469
  157. package/md_cg/test_issue467_identity_sensitivity_parity.py +360 -360
  158. package/md_cg/test_issue469_scrub_override_passthrough.py +126 -126
  159. package/md_cg/test_issue470_wal_event_durable.py +427 -427
  160. package/md_cg/test_issue471_identity_readable.py +136 -136
  161. package/md_cg/test_issue472_audit_chain.py +90 -90
  162. package/md_cg/test_issue472_prune_ledger.py +105 -105
  163. package/md_cg/test_issue472_rollback_degraded.py +81 -81
  164. package/md_cg/test_issue491_compose_sandbox.py +460 -460
  165. package/md_cg/test_issue498_merge_trace_stale_index.py +280 -280
  166. package/md_cg/test_issue511_scoreboard_bad_files.py +256 -256
  167. package/md_cg/test_issue521_rotate_lock_suffix.py +110 -110
  168. package/md_cg/test_issue524_orphan_noise_no_demote.py +88 -88
  169. package/md_cg/test_issue525_read_null_semantics.py +52 -52
  170. package/md_cg/test_issue547_append_jsonl_whitelist.py +114 -114
  171. package/md_cg/test_issue547_audit_durable.py +96 -96
  172. package/md_cg/test_issue557_r2_write_entries_size_gate.py +83 -83
  173. package/md_cg/test_issue559_refine_prefix.py +69 -69
  174. package/md_cg/test_issue562_retired_faces.py +150 -0
  175. package/md_cg/test_issue71_queue_backlog.py +376 -376
  176. package/md_cg/test_issue72_coldverify_controlled_drain.py +420 -420
  177. package/md_cg/test_issue82_credential_rules.py +271 -271
  178. package/md_cg/test_issue91_verifier_sandbox.py +314 -314
  179. package/md_cg/test_issue93_remember_redaction.py +282 -282
  180. package/md_cg/test_logref.py +1141 -1141
  181. package/md_cg/test_md_access_parity.py +330 -330
  182. package/md_cg/test_md_cg_no_hive_dep.py +139 -139
  183. package/md_cg/test_mutation_residue.py +119 -119
  184. package/md_cg/test_mutation_rollback.py +835 -835
  185. package/md_cg/test_n123_derive_expiry_chain.py +210 -210
  186. package/md_cg/test_n184_keys_concurrent_provision.py +1 -1
  187. package/md_cg/test_n209_verify_write_face_gates.py +466 -466
  188. package/md_cg/test_n62_tenant_bind_failclosed.py +200 -200
  189. package/md_cg/test_neg_tail_honesty.py +733 -733
  190. package/md_cg/test_p21_tokens.py +294 -294
  191. package/md_cg/test_p27_docindex.py +792 -792
  192. package/md_cg/test_p28_refcheck.py +1053 -1053
  193. package/md_cg/test_p2_six_elements.py +476 -476
  194. package/md_cg/test_p33_ccg_wiring.py +300 -300
  195. package/md_cg/test_p3_legacy_closure.py +438 -438
  196. package/md_cg/test_p40_refine_worklist.py +244 -244
  197. package/md_cg/test_p4_freshness.py +781 -781
  198. package/md_cg/test_p9c_dedup_hints.py +288 -288
  199. package/md_cg/test_policy_required_ccg.py +444 -444
  200. package/md_cg/test_read_scope_b27.py +2 -2
  201. package/md_cg/test_retr_s5.py +312 -312
  202. package/md_cg/test_s1_ast_judge.py +109 -109
  203. package/md_cg/test_security_audit_v21.py +249 -249
  204. package/md_cg/test_state_event_op.py +521 -521
  205. package/md_cg/test_stgidx_index_parity.py +1024 -1024
  206. package/md_cg/test_sustain_bad_stamp.py +178 -0
  207. package/md_cg/test_sustain_bounded.py +652 -652
  208. package/md_cg/test_trust_edge_reachability.py +405 -405
  209. package/md_cg/test_w7_redlines.py +659 -659
  210. package/md_cg/test_write_return_shape.py +919 -919
  211. package/md_cg/test_writepipe.py +228 -228
  212. package/md_cg/trust.py +1114 -1114
  213. package/md_cg/twophase.py +303 -303
  214. package/md_cg/whitebox.py +536 -536
  215. package/md_cg/whitebox_kb/wisdom/compose_engine.py +1634 -1634
  216. package/md_cg/whitebox_kb/wisdom/core/condition_vm.py +318 -318
  217. package/md_cg/whitebox_kb/wisdom/core/parser.py +1082 -1082
  218. package/md_cg/whitebox_kb/wisdom/md_access.py +490 -490
  219. package/md_cg/whitebox_kb/wisdom/migrate_wisdom.py +497 -497
  220. package/md_cg/whitebox_kb/wisdom/mini_python.py +995 -995
  221. package/md_cg/whitebox_kb/wisdom/net_units.py +2900 -2900
  222. package/md_cg/whitebox_kb/wisdom/semantic_translate.py +4060 -4060
  223. package/md_cg/writepipe.py +1337 -1337
  224. package/package.json +3 -2
  225. package/scripts/workspace_index.py +328 -0
  226. package/src/hooks.ts +932 -908
  227. package/src/index.ts +21 -6
  228. package/src/init.ts +16 -4
  229. package/src/lib/hook_audit.ts +48 -2
  230. package/src/lib/mdcg_client.ts +469 -469
  231. package/src/lib/prompt_safety.ts +17 -3
  232. package/src/tools.ts +23 -1
  233. package/zcode/AGENTS.md +333 -204
@@ -1,310 +1,310 @@
1
- # -*- coding: utf-8 -*-
2
- # 功能名:freshness 衰减下界与取数面接入既有保护规则(issue #378)
3
- # 生效条件:python -X utf8 -m md_cg.test_issue378_protected_floor [--mutate];被测面=md_cg/freshness.py::protected_floor + entry_weight
4
- # 子功能:A 组=#378 正题(层保护 / 显式位 / 缺省来源高分三条输入共用同一保护下界,且**正常检索取数入口**也吃到);K 组=非恒真对照(保护规则两侧边界、公式不变、未保护节点仍触 floor、取数优先级与依据字段不变);E 组=接线单点存在性
5
- # 执行:--mutate 时在镜像树里逐条把判据改回改动前形态 / 摘掉取数面漏掉的键 / 过度收紧,守卫必须按指定项转红
6
- # 验证方式:全部 PASS 即通过(退出码 0);--mutate 下每条变异的红项集合须与预期**逐项相符**(多红/少红皆不合格)
7
- # 不适用条件:不判检索总分的排序正确性(属 test_p4_* 与 _score 相关守卫);不判 Rust 侧 sort_path/freshness.rs(另一实现面,本守卫只盖 Python 面);不判写入路径与 recalc(apply=True) 的档位闸;不判 weights.IMPORTANCE_PROTECT 的取值(只断言同源)
8
- """issue #378 守卫:衰减下界必须与**既有保护规则**同源。
9
-
10
- 病象(修前):`protected_floor` 只读 `entry["protected"]` 一位 ⇒ `layer` 为
11
- anchor/self 或「缺省来源高分(importance ≥ 0.70)」的节点拿到普通下界
12
- (`SCORE_FLOOR`),衰减可把它们推过保护线——正是本模块 §7.3 要挡的「自动失去
13
- 保护」;而 `entry_weight` 的取数键集又漏了 `layer` / `importance_source`,
14
- 于是**只改下界函数覆盖不到正常检索入口**(`_score` → `entry_weight` →
15
- `protected_floor`)。
16
-
17
- 修法:下界判据改为「原显式 `protected` 位 **∪** `protect.is_protected`」
18
- (层保护 self/anchor ∪ 显式位 ∪ 按分保护 ≥0.70 且来源缺省/hint),取数键集补上
19
- `layer` 与 `importance_source`。衰减公式、`IMPORTANCE_PROTECT` 取值、
20
- `refresh_factor` 三档与返回体结构**一律不动**。
21
-
22
- **⚠ 这是一次检索行为变更(不是纯 bug fix)**:importance ≥ 0.70 且来源缺省的
23
- 节点,其衰减下界从 `0.001` 抬到 `0.70/importance`。本守卫的 K01/K02/K03/K04
24
- 把这四条边界**显式钉住**(享有 / 不享有各两侧),使这次变更以测试形式进仓,
25
- 而不是只表现为某条既有夹具的修改。
26
-
27
- **与 md_cg/test_p4_freshness.py G4 的关系(一轮裁定记录)**:该组原夹具
28
- `{"importance": 0.8}`(无来源标注)按本规则**是受保护节点**,与「未保护节点
29
- 不享有下界」的断言前提冲突;经 Lead 裁定补 `"importance_source": "heuristic"`,
30
- 断言一字未动。
31
- """
32
- import inspect
33
- import os
34
- import shutil
35
- import subprocess
36
- import sys
37
- import tempfile
38
-
39
- sys.stdout.reconfigure(encoding="utf-8", errors="replace")
40
-
41
- _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
42
- if _REPO not in sys.path:
43
- sys.path.insert(0, _REPO)
44
-
45
- from md_cg import freshness as F # noqa: E402
46
- from md_cg import protect as P # noqa: E402
47
- from md_cg import weights as W # noqa: E402
48
-
49
- FAILS = []
50
-
51
-
52
- def _check(name, cond, detail=""):
53
- if cond:
54
- print(" PASS " + name)
55
- else:
56
- print(" FAIL " + name + " " + str(detail)[:220])
57
- FAILS.append(name)
58
-
59
-
60
- #: 固定参照时刻(不在役库、不读 wall clock ⇒ 读数可复现)。
61
- REF = 2000000000.0
62
- DAY = F.SECONDS_PER_DAY
63
- #: issue 原文的探针距离:365 天(远超半衰期 ⇒ 衰减项被下界兜住)。
64
- FAR = REF - 365 * DAY
65
- #: 受保护下界期望值:IMPORTANCE_PROTECT / 0.9 = 0.7777…
66
- _PFLOOR_09 = W.IMPORTANCE_PROTECT / 0.9
67
-
68
-
69
- def _close(a, b, tol=1e-9):
70
- return abs(float(a) - float(b)) <= tol
71
-
72
-
73
- print("== A 组:#378 正题——三条输入共用同一保护下界 ==")
74
- _v = F.protected_floor({"importance": 0.9, "protected": True})
75
- _check("A01 显式 protected 位(修前既有行为,一字不变)",
76
- _close(_v, _PFLOOR_09), _v)
77
-
78
- _v = F.protected_floor({"importance": 0.9, "layer": "anchor"})
79
- _check("A02 layer=anchor(修前 0.001000,层保护未接线)",
80
- _close(_v, _PFLOOR_09), _v)
81
-
82
- _v = F.protected_floor({"importance": 0.9})
83
- _check("A03 缺省来源高分(修前 0.001000,按分保护未接线)",
84
- _close(_v, _PFLOOR_09), _v)
85
-
86
- _v = F.protected_floor({"importance": 0.9, "layer": "self"})
87
- _check("A04 layer=self 同样受保护(不是 anchor 特例)",
88
- _close(_v, _PFLOOR_09), _v)
89
-
90
- _v = F.protected_floor({"importance": 0.9, "importance_source": None})
91
- _check("A05 来源键显式为 None(=缺省来源)同样受保护",
92
- _close(_v, _PFLOOR_09), _v)
93
-
94
- # 正常检索入口:_score → entry_weight → protected_floor 的取数面
95
- _w, _why = F.entry_weight({"created_at": FAR, "importance": 0.9,
96
- "layer": "anchor"}, now=REF, enabled=True)
97
- _check("A06 取数面接线①:entry_weight 的 floor 依据读数吃到层保护(不是只有下界函数)",
98
- _close(_why.get("floor"), _PFLOOR_09), (_w, _why))
99
- _check("A07 取数面接线②:365 天后的乘子由 0.001000 回到约 0.777778(issue 原报告探针读数)",
100
- _close(_w, _PFLOOR_09), _w)
101
-
102
- _w2, _why2 = F.entry_weight({}, now=REF, enabled=True,
103
- fm={"created_at": FAR, "importance": 0.9,
104
- "layer": "anchor"})
105
- _check("A08 fm 覆盖路径同样带上 layer(写面刚落的新值不被取数面丢掉)",
106
- _close(_why2.get("floor"), _PFLOOR_09), (_w2, _why2))
107
-
108
- # 层保护的**唯一**保护来源腿:importance 低于保护线 ⇒ 只有 layer 能救它
109
- _w3, _why3 = F.entry_weight({"created_at": FAR, "importance": 0.5,
110
- "layer": "anchor"}, now=REF, enabled=True)
111
- _check("A09 取数面必须带 layer:importance=0.5 的 anchor 节点唯一保护来源是层(下界 1.0)",
112
- _close(_why3.get("floor"), 1.0), (_w3, _why3))
113
-
114
- print("== K 组:非恒真对照(保护规则两侧边界 + 未保护路径不得被关掉) ==")
115
- # —— Lead 强制附加项(#378 是一次检索行为变更,必须显式钉住):下方 K01–K04 ——
116
- _v = F.protected_floor({"importance": 0.9})
117
- _check("K01 【强制·享有侧】importance≥AUTO_PROTECT_IMPORTANCE(0.70) 且**无 importance_source** → 享有保护下界(与写侧自动保护规则一致)",
118
- _close(_v, _PFLOOR_09) and _v > F.SCORE_FLOOR, _v)
119
-
120
- _v = F.protected_floor({"importance": 0.5})
121
- _check("K02 【强制·不享有侧】importance<0.70 → 不享有(仍是 SCORE_FLOOR)",
122
- _close(_v, F.SCORE_FLOOR), _v)
123
-
124
- _v = F.protected_floor({"importance": 0.9, "importance_source": "heuristic"})
125
- _check("K03 【强制·不享有侧】importance_source 为显式非 hint 来源(heuristic)→ 不享有",
126
- _close(_v, F.SCORE_FLOOR), _v)
127
-
128
- _v = F.protected_floor({"importance": 0.9, "importance_source": "hint"})
129
- _check("K04 【强制·享有侧】importance_source == hint → 享有(边界另一侧)",
130
- _close(_v, _PFLOOR_09), _v)
131
-
132
- _check("K05 规则的唯一权威是 protect.is_protected(本模块不复制)",
133
- P.AUTO_PROTECT_IMPORTANCE == W.IMPORTANCE_PROTECT
134
- and _close(P.AUTO_PROTECT_IMPORTANCE, 0.70), P.AUTO_PROTECT_IMPORTANCE)
135
-
136
- _v = F.protected_floor({"importance": 0.5, "protected": True})
137
- _check("K06 公式不变:floor == max(SCORE_FLOOR, min(1.0, IMPORTANCE_PROTECT/importance))",
138
- _close(_v, 1.0), _v)
139
-
140
- _v = F.protected_floor({"importance": 0.2, "protected": True})
141
- _check("K07 下界被夹到 ≤1.0(importance<0.70 的受保护节点不衰减,也不放大)",
142
- _close(_v, 1.0), _v)
143
-
144
- # 夹具用低分:高分本身会经**按分保护**触发,测不出层位边界。
145
- _v = F.protected_floor({"importance": 0.5, "layer": "knowledge"})
146
- _check("K08 层保护只认 self/anchor:knowledge 层(低分、无来源)不受保护",
147
- _close(_v, F.SCORE_FLOOR), _v)
148
-
149
- _v = F.protected_floor({})
150
- _check("K09 空映射 / 无任何保护信号 → SCORE_FLOOR(不误判)",
151
- _close(_v, F.SCORE_FLOOR), _v)
152
-
153
- _w4, _why4 = F.entry_weight({"created_at": FAR, "importance": 0.8,
154
- "importance_source": "heuristic"},
155
- now=REF, enabled=True)
156
- _check("K10 未保护节点在**正常检索入口**仍触 floor(与 test_p4_freshness G4 同型,判别力在位)",
157
- _close(_w4, F.SCORE_FLOOR) and _close(_why4.get("floor"), F.SCORE_FLOOR),
158
- (_w4, _why4))
159
-
160
- _w5, _why5 = F.entry_weight({"created_at": FAR, "importance": 0.9,
161
- "importance_source": "heuristic"},
162
- now=REF, enabled=True)
163
- _check("K11 取数面必须带 importance_source:heuristic 不得被误读成缺省来源而认回保护",
164
- _close(_why5.get("floor"), F.SCORE_FLOOR), (_w5, _why5))
165
-
166
- _r5 = F.entry_weight({"created_at": FAR, "importance": 0.9,
167
- "importance_source": "heuristic"}, now=REF, enabled=False)
168
- _check("K12 enabled=False 早退仍 (1.0, {'disabled': True})(未受本批扰动)",
169
- _close(_r5[0], 1.0) and _r5[1] == {"disabled": True}, _r5)
170
-
171
- _w6, _why6 = F.entry_weight({"created_at": FAR, "importance": 0.9,
172
- "protected": True, "layer": "anchor"},
173
- now=REF, enabled=True)
174
- _check("K13 依据 dict 的键集未变(gamma/dt_days/unit/decay/refresh/floor/protected)",
175
- sorted(_why6) == sorted(["gamma", "dt_days", "unit", "decay", "refresh",
176
- "floor", "protected"]), sorted(_why6))
177
-
178
- _w7, _why7 = F.entry_weight({"created_at": 0, "importance": 0.9,
179
- "layer": "anchor"}, now=REF, enabled=True)
180
- _check("K14 取数优先级未变:缺 created_at 仍判无穷远、由 floor 兜住(不因新键改变口径)",
181
- _why7.get("dt_days") == float("inf") and _close(_w7, _PFLOOR_09),
182
- (_w7, _why7))
183
-
184
- print("== E 组:接线单点存在性 ==")
185
- _src = inspect.getsource(F.protected_floor)
186
- _check("E01 protected_floor 体内经 _is_protected_entry 接入判据(接线未脱焊)",
187
- "_is_protected_entry(e)" in _src, [l for l in _src.splitlines()
188
- if "_is_protected_entry" in l])
189
- _check("E02 探针交给 protect.is_protected(判据本体不在本模块复制)",
190
- "protect.is_protected(" in inspect.getsource(F._is_protected_entry), "")
191
- _src_w = inspect.getsource(F.entry_weight)
192
- _check("E03 entry_weight 取数键集含 layer 与 importance_source",
193
- '"layer"' in _src_w and '"importance_source"' in _src_w, "")
194
- _probe = F._EntryProbe({"layer": "anchor"})
195
- _check("E04 取数面不读盘:探针提供 get()(保护面回退分支得 None)且无 _maybe_reload_index(检索热路径零 IO)",
196
- callable(getattr(_probe, "get", None)) and _probe.get("x") is None
197
- and not hasattr(_probe, "_maybe_reload_index"), "")
198
-
199
- print("")
200
- if FAILS:
201
- print("issue #378 结果:%d 失败" % len(FAILS))
202
- else:
203
- print("issue #378 结果:全部通过")
204
-
205
-
206
- _MUTATIONS = (
207
- ("M1 回缺陷:下界只读显式 protected 位", "md_cg/freshness.py",
208
- (' if not (e.get("protected") or _is_protected_entry(e)[0]):',
209
- ' if not e.get("protected"):'),
210
- # 机制:旧判据只认显式位 ⇒ 层保护/按分保护两支全断(A 组正题),
211
- # 依据读数与乘子同步回落 0.001(K01/K04/K14 的享有侧亦随之转红);
212
- # E01 是源码形态腿(接线被删即脱焊,本就该红)。
213
- ("A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09",
214
- "K01", "K04", "K14", "E01")),
215
- ("M2 取数面漏键:keys 摘掉 layer / importance_source", "md_cg/freshness.py",
216
- (' "protected", "layer", "importance_source")',
217
- ' "protected")'),
218
- # 机制:取数面少了 layer ⇒ 层保护的**唯一**来源腿(A09)落到 floor;
219
- # 少了 importance_source ⇒ heuristic 被误读成缺省来源而认回保护(K10/K11);
220
- # E03 是源码形态腿。
221
- ("A09", "E03", "K10", "K11")),
222
- ("M3 过度收紧:恒判受保护", "md_cg/freshness.py",
223
- (' if not (e.get("protected") or _is_protected_entry(e)[0]):',
224
- ' if not (e.get("protected") or True):'),
225
- # 机制:恒判受保护 ⇒ 不享有侧全部被误保护(K 组判别力自证);
226
- # E01 是源码形态腿(判据块被整体替换)。
227
- ("K02", "K03", "K08", "K09", "K10", "K11", "E01")),
228
- )
229
-
230
-
231
- def _mirror_build():
232
- dst = tempfile.mkdtemp(prefix="iss378_mut_")
233
- for name in ("md_cg", "scripts"):
234
- src = os.path.join(_REPO, name)
235
- if os.path.isdir(src):
236
- shutil.copytree(src, os.path.join(dst, name),
237
- ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
238
- for name in sorted(os.listdir(_REPO)):
239
- p = os.path.join(_REPO, name)
240
- if os.path.isfile(p) and (name.endswith(".py")
241
- or name in ("README.md", "pyproject.toml",
242
- "package.json")):
243
- shutil.copy2(p, os.path.join(dst, name))
244
- return dst
245
-
246
-
247
- def _run_in(tree):
248
- env = dict(os.environ)
249
- env["PYTHONPATH"] = tree
250
- env["PYTHONUTF8"] = "1"
251
- env["PYTHONDONTWRITEBYTECODE"] = "1"
252
- env.pop("MDCG_ROOT", None)
253
- pr = subprocess.run([sys.executable, "-X", "utf8", "-m",
254
- "md_cg.test_issue378_protected_floor"],
255
- capture_output=True, text=True, encoding="utf-8",
256
- errors="replace", cwd=tree, env=env, timeout=1800)
257
- out = (pr.stdout or "") + (pr.stderr or "")
258
- reds = [ln.split("FAIL ", 1)[1].strip()
259
- for ln in out.splitlines() if ln.strip().startswith("FAIL ")]
260
- return pr.returncode, reds, out
261
-
262
-
263
- def _mutate():
264
- print("!! 定点变异自证:逐条把判据改回改动前形态 / 摘键 / 过度收紧,守卫必须按指定项转红")
265
- tree = _mirror_build()
266
- try:
267
- rc, reds, out = _run_in(tree)
268
- print(" 未变异镜像基线:rc=%d 红项=%d(必须 rc=0 且 0 红)" % (rc, len(reds)))
269
- if rc != 0 or reds:
270
- print(chr(10).join(out.splitlines()[-25:]))
271
- bad = []
272
- for desc, rel, (old, new), exp in _MUTATIONS:
273
- p = os.path.join(tree, rel)
274
- t = open(p, encoding="utf-8", newline="").read()
275
- if t.count(old) != 1:
276
- bad.append("%s —— 锚点在 %s 里命中 %d 次" % (desc, rel, t.count(old)))
277
- print(" ANCHOR-MISS " + desc)
278
- continue
279
- bak = t
280
- open(p, "w", encoding="utf-8", newline="").write(t.replace(old, new, 1))
281
- for root, dd, ff in os.walk(tree):
282
- if os.path.basename(root) == "__pycache__":
283
- shutil.rmtree(root, ignore_errors=True)
284
- rc2, reds2, out2 = _run_in(tree)
285
- open(p, "w", encoding="utf-8", newline="").write(bak)
286
- hit = sorted({r.split(" ")[0] for r in reds2})
287
- if rc2 != 0 and hit == sorted(exp):
288
- print(" OK %s 红项=%d 指定命中=%s" % (desc, len(reds2), hit))
289
- else:
290
- bad.append(desc)
291
- print(" MISMATCH %s 实际红项=%s 期望=%s rc=%d"
292
- % (desc, hit, sorted(exp), rc2))
293
- for r in reds2[:10]:
294
- print(" FAIL " + r)
295
- print("定点变异自证:%s(%d 条不合格)"
296
- % ("PASS" if not bad else "FAIL", len(bad)))
297
- for b in bad:
298
- print(" 不合格:" + b)
299
- return len(bad)
300
- finally:
301
- shutil.rmtree(tree, ignore_errors=True)
302
-
303
-
304
- def main():
305
- _bad = _mutate() if "--mutate" in sys.argv else 0
306
- return 1 if (FAILS or _bad) else 0
307
-
308
-
309
- if __name__ == "__main__":
310
- sys.exit(main())
1
+ # -*- coding: utf-8 -*-
2
+ # 功能名:freshness 衰减下界与取数面接入既有保护规则(issue #378)
3
+ # 生效条件:python -X utf8 -m md_cg.test_issue378_protected_floor [--mutate];被测面=md_cg/freshness.py::protected_floor + entry_weight
4
+ # 子功能:A 组=#378 正题(层保护 / 显式位 / 缺省来源高分三条输入共用同一保护下界,且**正常检索取数入口**也吃到);K 组=非恒真对照(保护规则两侧边界、公式不变、未保护节点仍触 floor、取数优先级与依据字段不变);E 组=接线单点存在性
5
+ # 执行:--mutate 时在镜像树里逐条把判据改回改动前形态 / 摘掉取数面漏掉的键 / 过度收紧,守卫必须按指定项转红
6
+ # 验证方式:全部 PASS 即通过(退出码 0);--mutate 下每条变异的红项集合须与预期**逐项相符**(多红/少红皆不合格)
7
+ # 不适用条件:不判检索总分的排序正确性(属 test_p4_* 与 _score 相关守卫);不判 Rust 侧 sort_path/freshness.rs(另一实现面,本守卫只盖 Python 面);不判写入路径与 recalc(apply=True) 的档位闸;不判 weights.IMPORTANCE_PROTECT 的取值(只断言同源)
8
+ """issue #378 守卫:衰减下界必须与**既有保护规则**同源。
9
+
10
+ 病象(修前):`protected_floor` 只读 `entry["protected"]` 一位 ⇒ `layer` 为
11
+ anchor/self 或「缺省来源高分(importance ≥ 0.70)」的节点拿到普通下界
12
+ (`SCORE_FLOOR`),衰减可把它们推过保护线——正是本模块 §7.3 要挡的「自动失去
13
+ 保护」;而 `entry_weight` 的取数键集又漏了 `layer` / `importance_source`,
14
+ 于是**只改下界函数覆盖不到正常检索入口**(`_score` → `entry_weight` →
15
+ `protected_floor`)。
16
+
17
+ 修法:下界判据改为「原显式 `protected` 位 **∪** `protect.is_protected`」
18
+ (层保护 self/anchor ∪ 显式位 ∪ 按分保护 ≥0.70 且来源缺省/hint),取数键集补上
19
+ `layer` 与 `importance_source`。衰减公式、`IMPORTANCE_PROTECT` 取值、
20
+ `refresh_factor` 三档与返回体结构**一律不动**。
21
+
22
+ **⚠ 这是一次检索行为变更(不是纯 bug fix)**:importance ≥ 0.70 且来源缺省的
23
+ 节点,其衰减下界从 `0.001` 抬到 `0.70/importance`。本守卫的 K01/K02/K03/K04
24
+ 把这四条边界**显式钉住**(享有 / 不享有各两侧),使这次变更以测试形式进仓,
25
+ 而不是只表现为某条既有夹具的修改。
26
+
27
+ **与 md_cg/test_p4_freshness.py G4 的关系(一轮裁定记录)**:该组原夹具
28
+ `{"importance": 0.8}`(无来源标注)按本规则**是受保护节点**,与「未保护节点
29
+ 不享有下界」的断言前提冲突;经 Lead 裁定补 `"importance_source": "heuristic"`,
30
+ 断言一字未动。
31
+ """
32
+ import inspect
33
+ import os
34
+ import shutil
35
+ import subprocess
36
+ import sys
37
+ import tempfile
38
+
39
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
40
+
41
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
42
+ if _REPO not in sys.path:
43
+ sys.path.insert(0, _REPO)
44
+
45
+ from md_cg import freshness as F # noqa: E402
46
+ from md_cg import protect as P # noqa: E402
47
+ from md_cg import weights as W # noqa: E402
48
+
49
+ FAILS = []
50
+
51
+
52
+ def _check(name, cond, detail=""):
53
+ if cond:
54
+ print(" PASS " + name)
55
+ else:
56
+ print(" FAIL " + name + " " + str(detail)[:220])
57
+ FAILS.append(name)
58
+
59
+
60
+ #: 固定参照时刻(不在役库、不读 wall clock ⇒ 读数可复现)。
61
+ REF = 2000000000.0
62
+ DAY = F.SECONDS_PER_DAY
63
+ #: issue 原文的探针距离:365 天(远超半衰期 ⇒ 衰减项被下界兜住)。
64
+ FAR = REF - 365 * DAY
65
+ #: 受保护下界期望值:IMPORTANCE_PROTECT / 0.9 = 0.7777…
66
+ _PFLOOR_09 = W.IMPORTANCE_PROTECT / 0.9
67
+
68
+
69
+ def _close(a, b, tol=1e-9):
70
+ return abs(float(a) - float(b)) <= tol
71
+
72
+
73
+ print("== A 组:#378 正题——三条输入共用同一保护下界 ==")
74
+ _v = F.protected_floor({"importance": 0.9, "protected": True})
75
+ _check("A01 显式 protected 位(修前既有行为,一字不变)",
76
+ _close(_v, _PFLOOR_09), _v)
77
+
78
+ _v = F.protected_floor({"importance": 0.9, "layer": "anchor"})
79
+ _check("A02 layer=anchor(修前 0.001000,层保护未接线)",
80
+ _close(_v, _PFLOOR_09), _v)
81
+
82
+ _v = F.protected_floor({"importance": 0.9})
83
+ _check("A03 缺省来源高分(修前 0.001000,按分保护未接线)",
84
+ _close(_v, _PFLOOR_09), _v)
85
+
86
+ _v = F.protected_floor({"importance": 0.9, "layer": "self"})
87
+ _check("A04 layer=self 同样受保护(不是 anchor 特例)",
88
+ _close(_v, _PFLOOR_09), _v)
89
+
90
+ _v = F.protected_floor({"importance": 0.9, "importance_source": None})
91
+ _check("A05 来源键显式为 None(=缺省来源)同样受保护",
92
+ _close(_v, _PFLOOR_09), _v)
93
+
94
+ # 正常检索入口:_score → entry_weight → protected_floor 的取数面
95
+ _w, _why = F.entry_weight({"created_at": FAR, "importance": 0.9,
96
+ "layer": "anchor"}, now=REF, enabled=True)
97
+ _check("A06 取数面接线①:entry_weight 的 floor 依据读数吃到层保护(不是只有下界函数)",
98
+ _close(_why.get("floor"), _PFLOOR_09), (_w, _why))
99
+ _check("A07 取数面接线②:365 天后的乘子由 0.001000 回到约 0.777778(issue 原报告探针读数)",
100
+ _close(_w, _PFLOOR_09), _w)
101
+
102
+ _w2, _why2 = F.entry_weight({}, now=REF, enabled=True,
103
+ fm={"created_at": FAR, "importance": 0.9,
104
+ "layer": "anchor"})
105
+ _check("A08 fm 覆盖路径同样带上 layer(写面刚落的新值不被取数面丢掉)",
106
+ _close(_why2.get("floor"), _PFLOOR_09), (_w2, _why2))
107
+
108
+ # 层保护的**唯一**保护来源腿:importance 低于保护线 ⇒ 只有 layer 能救它
109
+ _w3, _why3 = F.entry_weight({"created_at": FAR, "importance": 0.5,
110
+ "layer": "anchor"}, now=REF, enabled=True)
111
+ _check("A09 取数面必须带 layer:importance=0.5 的 anchor 节点唯一保护来源是层(下界 1.0)",
112
+ _close(_why3.get("floor"), 1.0), (_w3, _why3))
113
+
114
+ print("== K 组:非恒真对照(保护规则两侧边界 + 未保护路径不得被关掉) ==")
115
+ # —— Lead 强制附加项(#378 是一次检索行为变更,必须显式钉住):下方 K01–K04 ——
116
+ _v = F.protected_floor({"importance": 0.9})
117
+ _check("K01 【强制·享有侧】importance≥AUTO_PROTECT_IMPORTANCE(0.70) 且**无 importance_source** → 享有保护下界(与写侧自动保护规则一致)",
118
+ _close(_v, _PFLOOR_09) and _v > F.SCORE_FLOOR, _v)
119
+
120
+ _v = F.protected_floor({"importance": 0.5})
121
+ _check("K02 【强制·不享有侧】importance<0.70 → 不享有(仍是 SCORE_FLOOR)",
122
+ _close(_v, F.SCORE_FLOOR), _v)
123
+
124
+ _v = F.protected_floor({"importance": 0.9, "importance_source": "heuristic"})
125
+ _check("K03 【强制·不享有侧】importance_source 为显式非 hint 来源(heuristic)→ 不享有",
126
+ _close(_v, F.SCORE_FLOOR), _v)
127
+
128
+ _v = F.protected_floor({"importance": 0.9, "importance_source": "hint"})
129
+ _check("K04 【强制·享有侧】importance_source == hint → 享有(边界另一侧)",
130
+ _close(_v, _PFLOOR_09), _v)
131
+
132
+ _check("K05 规则的唯一权威是 protect.is_protected(本模块不复制)",
133
+ P.AUTO_PROTECT_IMPORTANCE == W.IMPORTANCE_PROTECT
134
+ and _close(P.AUTO_PROTECT_IMPORTANCE, 0.70), P.AUTO_PROTECT_IMPORTANCE)
135
+
136
+ _v = F.protected_floor({"importance": 0.5, "protected": True})
137
+ _check("K06 公式不变:floor == max(SCORE_FLOOR, min(1.0, IMPORTANCE_PROTECT/importance))",
138
+ _close(_v, 1.0), _v)
139
+
140
+ _v = F.protected_floor({"importance": 0.2, "protected": True})
141
+ _check("K07 下界被夹到 ≤1.0(importance<0.70 的受保护节点不衰减,也不放大)",
142
+ _close(_v, 1.0), _v)
143
+
144
+ # 夹具用低分:高分本身会经**按分保护**触发,测不出层位边界。
145
+ _v = F.protected_floor({"importance": 0.5, "layer": "knowledge"})
146
+ _check("K08 层保护只认 self/anchor:knowledge 层(低分、无来源)不受保护",
147
+ _close(_v, F.SCORE_FLOOR), _v)
148
+
149
+ _v = F.protected_floor({})
150
+ _check("K09 空映射 / 无任何保护信号 → SCORE_FLOOR(不误判)",
151
+ _close(_v, F.SCORE_FLOOR), _v)
152
+
153
+ _w4, _why4 = F.entry_weight({"created_at": FAR, "importance": 0.8,
154
+ "importance_source": "heuristic"},
155
+ now=REF, enabled=True)
156
+ _check("K10 未保护节点在**正常检索入口**仍触 floor(与 test_p4_freshness G4 同型,判别力在位)",
157
+ _close(_w4, F.SCORE_FLOOR) and _close(_why4.get("floor"), F.SCORE_FLOOR),
158
+ (_w4, _why4))
159
+
160
+ _w5, _why5 = F.entry_weight({"created_at": FAR, "importance": 0.9,
161
+ "importance_source": "heuristic"},
162
+ now=REF, enabled=True)
163
+ _check("K11 取数面必须带 importance_source:heuristic 不得被误读成缺省来源而认回保护",
164
+ _close(_why5.get("floor"), F.SCORE_FLOOR), (_w5, _why5))
165
+
166
+ _r5 = F.entry_weight({"created_at": FAR, "importance": 0.9,
167
+ "importance_source": "heuristic"}, now=REF, enabled=False)
168
+ _check("K12 enabled=False 早退仍 (1.0, {'disabled': True})(未受本批扰动)",
169
+ _close(_r5[0], 1.0) and _r5[1] == {"disabled": True}, _r5)
170
+
171
+ _w6, _why6 = F.entry_weight({"created_at": FAR, "importance": 0.9,
172
+ "protected": True, "layer": "anchor"},
173
+ now=REF, enabled=True)
174
+ _check("K13 依据 dict 的键集未变(gamma/dt_days/unit/decay/refresh/floor/protected)",
175
+ sorted(_why6) == sorted(["gamma", "dt_days", "unit", "decay", "refresh",
176
+ "floor", "protected"]), sorted(_why6))
177
+
178
+ _w7, _why7 = F.entry_weight({"created_at": 0, "importance": 0.9,
179
+ "layer": "anchor"}, now=REF, enabled=True)
180
+ _check("K14 取数优先级未变:缺 created_at 仍判无穷远、由 floor 兜住(不因新键改变口径)",
181
+ _why7.get("dt_days") == float("inf") and _close(_w7, _PFLOOR_09),
182
+ (_w7, _why7))
183
+
184
+ print("== E 组:接线单点存在性 ==")
185
+ _src = inspect.getsource(F.protected_floor)
186
+ _check("E01 protected_floor 体内经 _is_protected_entry 接入判据(接线未脱焊)",
187
+ "_is_protected_entry(e)" in _src, [l for l in _src.splitlines()
188
+ if "_is_protected_entry" in l])
189
+ _check("E02 探针交给 protect.is_protected(判据本体不在本模块复制)",
190
+ "protect.is_protected(" in inspect.getsource(F._is_protected_entry), "")
191
+ _src_w = inspect.getsource(F.entry_weight)
192
+ _check("E03 entry_weight 取数键集含 layer 与 importance_source",
193
+ '"layer"' in _src_w and '"importance_source"' in _src_w, "")
194
+ _probe = F._EntryProbe({"layer": "anchor"})
195
+ _check("E04 取数面不读盘:探针提供 get()(保护面回退分支得 None)且无 _maybe_reload_index(检索热路径零 IO)",
196
+ callable(getattr(_probe, "get", None)) and _probe.get("x") is None
197
+ and not hasattr(_probe, "_maybe_reload_index"), "")
198
+
199
+ print("")
200
+ if FAILS:
201
+ print("issue #378 结果:%d 失败" % len(FAILS))
202
+ else:
203
+ print("issue #378 结果:全部通过")
204
+
205
+
206
+ _MUTATIONS = (
207
+ ("M1 回缺陷:下界只读显式 protected 位", "md_cg/freshness.py",
208
+ (' if not (e.get("protected") or _is_protected_entry(e)[0]):',
209
+ ' if not e.get("protected"):'),
210
+ # 机制:旧判据只认显式位 ⇒ 层保护/按分保护两支全断(A 组正题),
211
+ # 依据读数与乘子同步回落 0.001(K01/K04/K14 的享有侧亦随之转红);
212
+ # E01 是源码形态腿(接线被删即脱焊,本就该红)。
213
+ ("A02", "A03", "A04", "A05", "A06", "A07", "A08", "A09",
214
+ "K01", "K04", "K14", "E01")),
215
+ ("M2 取数面漏键:keys 摘掉 layer / importance_source", "md_cg/freshness.py",
216
+ (' "protected", "layer", "importance_source")',
217
+ ' "protected")'),
218
+ # 机制:取数面少了 layer ⇒ 层保护的**唯一**来源腿(A09)落到 floor;
219
+ # 少了 importance_source ⇒ heuristic 被误读成缺省来源而认回保护(K10/K11);
220
+ # E03 是源码形态腿。
221
+ ("A09", "E03", "K10", "K11")),
222
+ ("M3 过度收紧:恒判受保护", "md_cg/freshness.py",
223
+ (' if not (e.get("protected") or _is_protected_entry(e)[0]):',
224
+ ' if not (e.get("protected") or True):'),
225
+ # 机制:恒判受保护 ⇒ 不享有侧全部被误保护(K 组判别力自证);
226
+ # E01 是源码形态腿(判据块被整体替换)。
227
+ ("K02", "K03", "K08", "K09", "K10", "K11", "E01")),
228
+ )
229
+
230
+
231
+ def _mirror_build():
232
+ dst = tempfile.mkdtemp(prefix="iss378_mut_")
233
+ for name in ("md_cg", "scripts"):
234
+ src = os.path.join(_REPO, name)
235
+ if os.path.isdir(src):
236
+ shutil.copytree(src, os.path.join(dst, name),
237
+ ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
238
+ for name in sorted(os.listdir(_REPO)):
239
+ p = os.path.join(_REPO, name)
240
+ if os.path.isfile(p) and (name.endswith(".py")
241
+ or name in ("README.md", "pyproject.toml",
242
+ "package.json")):
243
+ shutil.copy2(p, os.path.join(dst, name))
244
+ return dst
245
+
246
+
247
+ def _run_in(tree):
248
+ env = dict(os.environ)
249
+ env["PYTHONPATH"] = tree
250
+ env["PYTHONUTF8"] = "1"
251
+ env["PYTHONDONTWRITEBYTECODE"] = "1"
252
+ env.pop("MDCG_ROOT", None)
253
+ pr = subprocess.run([sys.executable, "-X", "utf8", "-m",
254
+ "md_cg.test_issue378_protected_floor"],
255
+ capture_output=True, text=True, encoding="utf-8",
256
+ errors="replace", cwd=tree, env=env, timeout=1800)
257
+ out = (pr.stdout or "") + (pr.stderr or "")
258
+ reds = [ln.split("FAIL ", 1)[1].strip()
259
+ for ln in out.splitlines() if ln.strip().startswith("FAIL ")]
260
+ return pr.returncode, reds, out
261
+
262
+
263
+ def _mutate():
264
+ print("!! 定点变异自证:逐条把判据改回改动前形态 / 摘键 / 过度收紧,守卫必须按指定项转红")
265
+ tree = _mirror_build()
266
+ try:
267
+ rc, reds, out = _run_in(tree)
268
+ print(" 未变异镜像基线:rc=%d 红项=%d(必须 rc=0 且 0 红)" % (rc, len(reds)))
269
+ if rc != 0 or reds:
270
+ print(chr(10).join(out.splitlines()[-25:]))
271
+ bad = []
272
+ for desc, rel, (old, new), exp in _MUTATIONS:
273
+ p = os.path.join(tree, rel)
274
+ t = open(p, encoding="utf-8", newline="").read()
275
+ if t.count(old) != 1:
276
+ bad.append("%s —— 锚点在 %s 里命中 %d 次" % (desc, rel, t.count(old)))
277
+ print(" ANCHOR-MISS " + desc)
278
+ continue
279
+ bak = t
280
+ open(p, "w", encoding="utf-8", newline="").write(t.replace(old, new, 1))
281
+ for root, dd, ff in os.walk(tree):
282
+ if os.path.basename(root) == "__pycache__":
283
+ shutil.rmtree(root, ignore_errors=True)
284
+ rc2, reds2, out2 = _run_in(tree)
285
+ open(p, "w", encoding="utf-8", newline="").write(bak)
286
+ hit = sorted({r.split(" ")[0] for r in reds2})
287
+ if rc2 != 0 and hit == sorted(exp):
288
+ print(" OK %s 红项=%d 指定命中=%s" % (desc, len(reds2), hit))
289
+ else:
290
+ bad.append(desc)
291
+ print(" MISMATCH %s 实际红项=%s 期望=%s rc=%d"
292
+ % (desc, hit, sorted(exp), rc2))
293
+ for r in reds2[:10]:
294
+ print(" FAIL " + r)
295
+ print("定点变异自证:%s(%d 条不合格)"
296
+ % ("PASS" if not bad else "FAIL", len(bad)))
297
+ for b in bad:
298
+ print(" 不合格:" + b)
299
+ return len(bad)
300
+ finally:
301
+ shutil.rmtree(tree, ignore_errors=True)
302
+
303
+
304
+ def main():
305
+ _bad = _mutate() if "--mutate" in sys.argv else 0
306
+ return 1 if (FAILS or _bad) else 0
307
+
308
+
309
+ if __name__ == "__main__":
310
+ sys.exit(main())