@furongjun1999/dsh-memory 0.9.6 → 0.9.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/data/policy.json +38 -38
  2. package/docs/discipline/templates/spec.txt.tmpl +1 -1
  3. package/dsh/cordis.yml.example +139 -139
  4. package/lib/hooks.d.ts +19 -0
  5. package/lib/hooks.js +29 -6
  6. package/lib/index.js +11 -2
  7. package/lib/lib/hook_audit.js +38 -2
  8. package/lib/lib/prompt_safety.js +14 -3
  9. package/lib/tools.d.ts +7 -0
  10. package/lib/tools.js +22 -1
  11. package/md_cg/_guard_kit.py +121 -121
  12. package/md_cg/anchor.py +114 -114
  13. package/md_cg/audit.py +15 -2
  14. package/md_cg/chain.py +3 -2
  15. package/md_cg/config_registry_guard.py +487 -487
  16. package/md_cg/conformance.py +747 -747
  17. package/md_cg/consolidate.py +1793 -1793
  18. package/md_cg/crypto.py +794 -794
  19. package/md_cg/hyperedge.py +262 -262
  20. package/md_cg/interface_schema.py +146 -146
  21. package/md_cg/lifecycle.py +426 -426
  22. package/md_cg/mdcos.py +12 -8
  23. package/md_cg/predict.py +1103 -1103
  24. package/md_cg/protect.py +654 -654
  25. package/md_cg/provenance.py +606 -606
  26. package/md_cg/queue_health.py +130 -130
  27. package/md_cg/refine.py +613 -613
  28. package/md_cg/retrieve.py +128 -128
  29. package/md_cg/revise.py +140 -140
  30. package/md_cg/rollback.py +657 -657
  31. package/md_cg/rollback_cli.py +124 -124
  32. package/md_cg/rotate.py +346 -346
  33. package/md_cg/routing.py +490 -490
  34. package/md_cg/self_surface.py +50 -50
  35. package/md_cg/semantic/canonical.py +144 -144
  36. package/md_cg/semantic/zh_en_atoms.py +141 -141
  37. package/md_cg/sources.py +1091 -1091
  38. package/md_cg/stg.py +3 -2
  39. package/md_cg/test_audit_rotate_parity.py +279 -279
  40. package/md_cg/test_autonomy_modes.py +2013 -2013
  41. package/md_cg/test_cn_recall_grams.py +206 -206
  42. package/md_cg/test_code_kind_forbidden.py +101 -0
  43. package/md_cg/test_condaccept.py +156 -156
  44. package/md_cg/test_condgate.py +141 -141
  45. package/md_cg/test_heartbeat_ledger.py +557 -557
  46. package/md_cg/test_i50e_readside_protection.py +658 -658
  47. package/md_cg/test_issue111_gate_order.py +76 -76
  48. package/md_cg/test_issue111_scoring_contract.py +86 -86
  49. package/md_cg/test_issue121_calibration_bounds.py +293 -293
  50. package/md_cg/test_issue145_sleep_no_silent_rollback.py +167 -167
  51. package/md_cg/test_issue147_pack_id_escape.py +378 -378
  52. package/md_cg/test_issue148_merge_downgrade_leak.py +192 -192
  53. package/md_cg/test_issue149_accel_default_face.py +248 -248
  54. package/md_cg/test_issue150_empty_query_terms.py +328 -328
  55. package/md_cg/test_issue151_153_157_write_cache_index.py +151 -151
  56. package/md_cg/test_issue152_sealed_postings.py +309 -309
  57. package/md_cg/test_issue153b_index_generation_invalidation.py +142 -142
  58. package/md_cg/test_issue157b_index_remainder.py +344 -344
  59. package/md_cg/test_issue189_conclusion_bidirectional.py +223 -223
  60. package/md_cg/test_issue191_import_trust.py +382 -382
  61. package/md_cg/test_issue193_write_scope.py +378 -378
  62. package/md_cg/test_issue212_writeface_gaps.py +349 -349
  63. package/md_cg/test_issue214_reject_sensitivity.py +266 -266
  64. package/md_cg/test_issue215_condition_source_gap.py +272 -272
  65. package/md_cg/test_issue216_clash_routes.py +214 -214
  66. package/md_cg/test_issue217_slot_routes.py +248 -248
  67. package/md_cg/test_issue218_postcondition_value_blind.py +222 -222
  68. package/md_cg/test_issue258_pack_sensitivity.py +468 -468
  69. package/md_cg/test_issue265_coldverify_multiproc.py +438 -438
  70. package/md_cg/test_issue274_read_node_id_none.py +341 -341
  71. package/md_cg/test_issue275_layer_param.py +250 -250
  72. package/md_cg/test_issue277_node_id_traversal.py +384 -384
  73. package/md_cg/test_issue278_neg_separated_tension.py +271 -271
  74. package/md_cg/test_issue278_neg_window.py +294 -294
  75. package/md_cg/test_issue278_negative_layering.py +83 -83
  76. package/md_cg/test_issue284_skip_not_pass.py +291 -291
  77. package/md_cg/test_issue294_flywheel_sensitivity.py +573 -573
  78. package/md_cg/test_issue301_baseline_corpus_gate.py +245 -245
  79. package/md_cg/test_issue303_design_boundaries.py +262 -262
  80. package/md_cg/test_issue315_merge_branch_sensitivity.py +383 -383
  81. package/md_cg/test_issue319_tenant_unregistered_bypass.py +256 -256
  82. package/md_cg/test_issue321_mcp_deep_json.py +304 -304
  83. package/md_cg/test_issue322_node_write_lock.py +727 -727
  84. package/md_cg/test_issue324_write_size_fields.py +214 -214
  85. package/md_cg/test_issue325_audit_poison.py +229 -229
  86. package/md_cg/test_issue326_ingest_auto_whitelist.py +331 -331
  87. package/md_cg/test_issue326_key_perms.py +355 -355
  88. package/md_cg/test_issue326_twin_parser_parity.py +323 -323
  89. package/md_cg/test_issue326_vm_call_frame.py +569 -569
  90. package/md_cg/test_issue345_writelimit_effective_sens.py +233 -233
  91. package/md_cg/test_issue346_overwrite_sensitivity_gate.py +424 -424
  92. package/md_cg/test_issue348_insight_verify_op.py +199 -199
  93. package/md_cg/test_issue348_merge_rmw_lock.py +345 -345
  94. package/md_cg/test_issue350_casefold_collision.py +235 -235
  95. package/md_cg/test_issue351_ingest_root_fallback.py +201 -201
  96. package/md_cg/test_issue352_imgaudit.py +418 -418
  97. package/md_cg/test_issue352_pathsjson.py +537 -537
  98. package/md_cg/test_issue352_refindex.py +412 -412
  99. package/md_cg/test_issue352_rollback.py +604 -604
  100. package/md_cg/test_issue354_p2_writelimit_toctou.py +327 -327
  101. package/md_cg/test_issue354_rust_index_containment.py +571 -571
  102. package/md_cg/test_issue354_rust_wal_containment.py +606 -606
  103. package/md_cg/test_issue358_truncation_observable.py +247 -247
  104. package/md_cg/test_issue360_tidy_private_excerpt.py +195 -195
  105. package/md_cg/test_issue361_blindspot_not_silent_drop.py +494 -494
  106. package/md_cg/test_issue362_neg_condition.py +468 -468
  107. package/md_cg/test_issue363_lexical_idf.py +275 -275
  108. package/md_cg/test_issue370_readcache_assembly.py +488 -488
  109. package/md_cg/test_issue376_heartbeat_failed.py +293 -293
  110. package/md_cg/test_issue377_layer_protect_stamp.py +324 -324
  111. package/md_cg/test_issue378_protected_floor.py +310 -310
  112. package/md_cg/test_issue383_state_event_write_gate.py +462 -462
  113. package/md_cg/test_issue397_sens_eff_wiring.py +435 -435
  114. package/md_cg/test_issue398_toolface_op_parity.py +511 -511
  115. package/md_cg/test_issue399_hive_json_shape.py +317 -317
  116. package/md_cg/test_issue401_wisdom_codegen.py +295 -295
  117. package/md_cg/test_issue404_heal_receipt.py +389 -389
  118. package/md_cg/test_issue409_compact_index_completeness.py +509 -509
  119. package/md_cg/test_issue410_verify_demote_write.py +366 -366
  120. package/md_cg/test_issue411_scrub_idempotence.py +575 -575
  121. package/md_cg/test_issue412_discipline_producer_parity.py +311 -311
  122. package/md_cg/test_issue413_preimage_failclosed.py +239 -239
  123. package/md_cg/test_issue413_preimage_twin.py +339 -339
  124. package/md_cg/test_issue415_deps_gate_probe.py +357 -357
  125. package/md_cg/test_issue416_entry_key_subset.py +525 -525
  126. package/md_cg/test_issue417_twophase_merge_landed.py +423 -423
  127. package/md_cg/test_issue425_ingest_library_path.py +331 -331
  128. package/md_cg/test_issue426c_enqueue_not_silent.py +346 -346
  129. package/md_cg/test_issue426e_restore_overwrite_live.py +310 -310
  130. package/md_cg/test_issue426f_paths_gbk_failclosed.py +301 -301
  131. package/md_cg/test_issue427_serve_status_shape.py +191 -191
  132. package/md_cg/test_issue431_432_468_protect_single_point.py +322 -322
  133. package/md_cg/test_issue433_outlook_threshold_single_point.py +278 -278
  134. package/md_cg/test_issue434_435_float_gate.py +90 -90
  135. package/md_cg/test_issue439_sibling_sensitivity_inherit.py +448 -448
  136. package/md_cg/test_issue43_default_policy.py +874 -874
  137. package/md_cg/test_issue444_scrub_evidence_plaintext.py +281 -281
  138. package/md_cg/test_issue445_metacognition_ledger_gate.py +454 -454
  139. package/md_cg/test_issue446_453_403_verified_evidence.py +382 -382
  140. package/md_cg/test_issue450_sensitivity_fallback_reason.py +51 -51
  141. package/md_cg/test_issue450_writegate_unknown_sens.py +203 -203
  142. package/md_cg/test_issue451_audit_actor.py +313 -313
  143. package/md_cg/test_issue452_ingest_root_sensitivity.py +299 -299
  144. package/md_cg/test_issue463_ghost_prune_guard.py +259 -259
  145. package/md_cg/test_issue466_basis_registry.py +469 -469
  146. package/md_cg/test_issue467_identity_sensitivity_parity.py +360 -360
  147. package/md_cg/test_issue469_scrub_override_passthrough.py +126 -126
  148. package/md_cg/test_issue470_wal_event_durable.py +427 -427
  149. package/md_cg/test_issue471_identity_readable.py +136 -136
  150. package/md_cg/test_issue472_audit_chain.py +90 -90
  151. package/md_cg/test_issue472_prune_ledger.py +105 -105
  152. package/md_cg/test_issue472_rollback_degraded.py +81 -81
  153. package/md_cg/test_issue491_compose_sandbox.py +460 -460
  154. package/md_cg/test_issue498_merge_trace_stale_index.py +280 -280
  155. package/md_cg/test_issue511_scoreboard_bad_files.py +256 -256
  156. package/md_cg/test_issue521_rotate_lock_suffix.py +110 -110
  157. package/md_cg/test_issue524_orphan_noise_no_demote.py +88 -88
  158. package/md_cg/test_issue525_read_null_semantics.py +52 -52
  159. package/md_cg/test_issue547_append_jsonl_whitelist.py +114 -114
  160. package/md_cg/test_issue547_audit_durable.py +96 -96
  161. package/md_cg/test_issue557_r2_write_entries_size_gate.py +83 -83
  162. package/md_cg/test_issue559_refine_prefix.py +69 -69
  163. package/md_cg/test_issue562_retired_faces.py +150 -0
  164. package/md_cg/test_issue71_queue_backlog.py +376 -376
  165. package/md_cg/test_issue72_coldverify_controlled_drain.py +420 -420
  166. package/md_cg/test_issue82_credential_rules.py +271 -271
  167. package/md_cg/test_issue91_verifier_sandbox.py +314 -314
  168. package/md_cg/test_issue93_remember_redaction.py +282 -282
  169. package/md_cg/test_logref.py +1141 -1141
  170. package/md_cg/test_md_access_parity.py +330 -330
  171. package/md_cg/test_md_cg_no_hive_dep.py +139 -139
  172. package/md_cg/test_mutation_residue.py +119 -119
  173. package/md_cg/test_mutation_rollback.py +835 -835
  174. package/md_cg/test_n123_derive_expiry_chain.py +210 -210
  175. package/md_cg/test_n209_verify_write_face_gates.py +466 -466
  176. package/md_cg/test_n62_tenant_bind_failclosed.py +200 -200
  177. package/md_cg/test_neg_tail_honesty.py +733 -733
  178. package/md_cg/test_p21_tokens.py +294 -294
  179. package/md_cg/test_p27_docindex.py +792 -792
  180. package/md_cg/test_p28_refcheck.py +1053 -1053
  181. package/md_cg/test_p2_six_elements.py +476 -476
  182. package/md_cg/test_p33_ccg_wiring.py +300 -300
  183. package/md_cg/test_p3_legacy_closure.py +438 -438
  184. package/md_cg/test_p40_refine_worklist.py +244 -244
  185. package/md_cg/test_p4_freshness.py +781 -781
  186. package/md_cg/test_p9c_dedup_hints.py +288 -288
  187. package/md_cg/test_policy_required_ccg.py +444 -444
  188. package/md_cg/test_retr_s5.py +312 -312
  189. package/md_cg/test_s1_ast_judge.py +109 -109
  190. package/md_cg/test_state_event_op.py +521 -521
  191. package/md_cg/test_stgidx_index_parity.py +1024 -1024
  192. package/md_cg/test_sustain_bounded.py +652 -652
  193. package/md_cg/test_trust_edge_reachability.py +405 -405
  194. package/md_cg/test_w7_redlines.py +659 -659
  195. package/md_cg/test_write_return_shape.py +919 -919
  196. package/md_cg/test_writepipe.py +228 -228
  197. package/md_cg/trust.py +1114 -1114
  198. package/md_cg/twophase.py +303 -303
  199. package/md_cg/whitebox.py +536 -536
  200. package/md_cg/whitebox_kb/wisdom/compose_engine.py +1634 -1634
  201. package/md_cg/whitebox_kb/wisdom/core/condition_vm.py +318 -318
  202. package/md_cg/whitebox_kb/wisdom/core/parser.py +1082 -1082
  203. package/md_cg/whitebox_kb/wisdom/md_access.py +490 -490
  204. package/md_cg/whitebox_kb/wisdom/migrate_wisdom.py +497 -497
  205. package/md_cg/whitebox_kb/wisdom/mini_python.py +995 -995
  206. package/md_cg/whitebox_kb/wisdom/net_units.py +2900 -2900
  207. package/md_cg/whitebox_kb/wisdom/semantic_translate.py +4060 -4060
  208. package/md_cg/writepipe.py +1337 -1337
  209. package/package.json +3 -2
  210. package/scripts/workspace_index.py +328 -0
  211. package/src/hooks.ts +30 -6
  212. package/src/index.ts +14 -5
  213. package/src/lib/hook_audit.ts +282 -256
  214. package/src/lib/mdcg_client.ts +469 -469
  215. package/src/lib/prompt_safety.ts +17 -3
  216. package/src/tools.ts +23 -1
@@ -1,509 +1,509 @@
1
- # -*- coding: utf-8 -*-
2
- # 功能名:compact_index 的完整性判据必须 sound,「可信吗」判据必须单点(issue #409)
3
- # 生效条件:python -X utf8 -m md_cg.test_issue409_compact_index_completeness [--mutate];被测面=md_cg/mdcg.py 的 compact_index / _load_index / _snapshot_trusted / _covers_disk
4
- # 子功能:A 组=五形态「净 .md 计数为零而语义已变」(旧判据假阳 + 新判据能抓 + 端到端读数正确);K 组=非恒真对照(正常路径不得重扫、幂等 compact、可信快照装载不扫盘);E 组=源码面(「可信吗」单点唯一实现、两个判据互不调用、接线在位)
5
- # 执行:--mutate 时在镜像树里逐条把判据改回缺陷形态 / 过度收紧(恒判不可信、恒不过闸),守卫必须按指定项转红
6
- # 验证方式:全部 PASS 即通过(退出码 0);--mutate 下每条变异的红项集合须与预期逐项相符(多红/少红皆不合格)
7
- # 不适用条件:不判「外部纯内容改写(不动文件名)」——那是 _dir_fingerprint 自述的协议外边界(合作写者协议,见其 docstring);不判「实例已打开期间、库外纯改名/对调且路径集合不变」这一残余形态(需以装载时指纹为基线,本批不做,见汇报未覆盖;issue 原文的「更稳」建议亦不覆盖);不判 Rust 侧索引装载(另一实现面);不判 compact 的原子写/锁(属既有守卫)
8
- #
9
- # ── 本改动的硬边界登记(改动时撞上的既有限制,及为何这样绕)──────────────────
10
- # ① 有效快照的重开路径不得扫盘:md_cg/test_index_snapshot_shape.py:191-196 用
11
- # mock.patch.object(MdCG, "_scan_nodes", side_effect=AssertionError(...)) 钉死
12
- # ⇒ _snapshot_trusted 必须保热路径(置信即用,不重扫);本次未动该路径。
13
- # ② compact 里的变异锚点唯一性:同文件 :227-229 的定点变异锚点
14
- # 'if not _usable_index_snapshot(d):' 必须原样唯一存在于 compact_index
15
- # ⇒ 本批一字未动该行(形状准入闸保持原样)。
16
- # ③ 源码窗口区间:md_cg/test_n212_n213_n224_generation_gates.py:481 取
17
- # 「def _maybe_reload_index → 首个 def _dir_fingerprint」为窗口并断言含三个
18
- # invalidate_cache ⇒ 新函数不得插在该区间内:_snapshot_trusted / _md_rel_paths /
19
- # _covers_disk 一律放在 _dir_fingerprint 之后。
20
- # ④ _count_md_files 的语义必须保持(层内 .md 计数):
21
- # md_cg/test_n225_nonobject_load.py:688 断言 len(index.nodes) == c._count_md_files()
22
- # ⇒ 它改为「len(self._md_rel_paths())」——计数不变,只把走查收成同源一份。
23
- # ⑤ 完整性闸的**位置**(不是既有约束,是本批的论证——写在这里防后人挪动):
24
- # 闸装在**重放之前**。闸问的是「我拿到的这份**基底**是否恰好描述当前盘面」;
25
- # 而增量日志是**合作写者协议下的权威写入记录**,其条目**允许指向盘外路径**
26
- # ——这一点由 md_cg/test_n225_nonobject_load.py 的 E3(同批里的合法 upsert 仍
27
- # 生效)与 E6(compact 后 nodes 与装载面逐键相等,且同批坏载荷同样记账:
28
- # 子面共 6 条)钉死。若把闸装在**重放之后**,就会把「日志的正确性」误判成
29
- # 「基底不完整」、并因此**多付一次重放** ⇒ E6 的记账面立刻转红(本批第一版正是
30
- # 这样被打红的:payload/total 9 而非 6;根因与前后读数见汇报 §7)。
31
- # 同源依据:_load_index 的兜底也在**重放之前**换基底(无快照/不可信 ⇒ 先扫盘,
32
- # 再统一重放)——两面的时序一致。
33
- #
34
- # 另一条(不是限制而是先例):_usable_index_snapshot(N225 准入闸)与 _apply_log
35
- # (N225 重放)本来就是 load/compact 共用单点 ⇒ _snapshot_trusted 是延续既有形态,
36
- # 不是新立机制。
37
- """issue #409 守卫:compact_index 的完整性命中判据必须 sound,「快照可信吗」必须单点。
38
-
39
- 病象(修前):同一个问题(这份快照可信吗)有两套判据——
40
- · _load_index 用目录指纹(新鲜度);
41
- · compact_index 用 .md 计数相等(自称完整性),随后无条件把当前指纹盖上去。
42
- 「净计数为零」的库外变化(改名 / 换目录 / 两文件内容对调 / 删一加一 / 另一实例
43
- 已落盘而其索引增量还在它自己的 _dirty 里)让计数判据必然假阳 ⇒ 陈旧
44
- 「节点→path」映射被原样保留 + 盖上当前指纹 ⇒ 下次打开指纹相符、不重扫 ⇒
45
- get(id) 返回别人的正文;此后一次普通写回(读到→改→写回)会让别人的正文
46
- 全库无副本(_protected_history / trash / _index_log 均无)——只有全量
47
- rebuild_index() 能救。
48
-
49
- 修法(两件事,各自单点、互不调用):
50
- · 可信吗(新鲜度)⇒ _snapshot_trusted:唯一实现,_load_index 与 compact_index
51
- 共用(收成单点,不是给 compact 再加一条);
52
- · 写完安全吗(完整性)⇒ _covers_disk:把原来的 .md 计数相等升级为
53
- 「索引路径集合 == 盘面 .md 路径集合」(集合相等 ⇒ 计数相等,反之不然),
54
- 且只在基底确定后、重放之前判定(日志条目按合作写者协议是权威写入记录,
55
- 允许指向盘外路径——N225 守卫 E3/E6 钉死)。
56
- 不可信的旧快照不作基底,改吃本进程内存索引;不采用「不可信一律重扫」
57
- (正常 close 路径里本进程刚写过文件 ⇒ 盘上快照指纹必然失配,一律重扫会退化成
58
- 每次 close 全库重扫,正是 issue #33 要避免的)。
59
- """
60
- import json
61
- import os
62
- import shutil
63
- import subprocess
64
- import sys
65
- import tempfile
66
- from unittest import mock
67
-
68
- sys.stdout.reconfigure(encoding="utf-8", errors="replace")
69
-
70
- _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
71
- if _REPO not in sys.path:
72
- sys.path.insert(0, _REPO)
73
-
74
- from md_cg.mdcg import MdCG # noqa: E402
75
- from md_cg import mdcg as _mdcg # noqa: E402
76
-
77
- FAILS = []
78
- _TMP = tempfile.mkdtemp(prefix="iss409_")
79
- _K = "knowledge/orphan"
80
- _BOOM = AssertionError("正常路径不得扫盘(_scan_nodes 被调用)")
81
-
82
-
83
- def _check(name, cond, detail=""):
84
- if cond:
85
- print(" PASS " + name)
86
- else:
87
- print(" FAIL " + name + " " + str(detail)[:220])
88
- FAILS.append(name)
89
-
90
-
91
- def _no_scan():
92
- return mock.patch.object(MdCG, "_scan_nodes", side_effect=_BOOM)
93
-
94
-
95
- def _root(tag):
96
- d = os.path.join(_TMP, tag)
97
- os.makedirs(d, exist_ok=True)
98
- return d
99
-
100
-
101
- def _build(root, nodes=(("alpha", "甲"), ("beta", "乙"))):
102
- cg = MdCG(root)
103
- for nid, mark in nodes:
104
- cg.add(nid, "MARK-" + mark)
105
- cg.flush()
106
- cg.close() # 首次 close:无快照 → rebuild_index 落 S1
107
- return _snap(root)
108
-
109
-
110
- def _snap(root):
111
- d = json.load(open(os.path.join(root, "_index.json"), encoding="utf-8"))
112
- return ({k: v["path"] for k, v in d["nodes"].items()}, d.get("_fingerprint"))
113
-
114
-
115
- def _count_md(root):
116
- n = 0
117
- for layer in ("anchor", "structural", "knowledge", "contextual", "self",
118
- "rejected", "unresolved", "goals"):
119
- for _dp, _dd, ff in os.walk(os.path.join(root, layer)):
120
- n += sum(1 for f in ff if f.endswith(".md"))
121
- return n
122
-
123
-
124
- def _write_md(root, rel, nid, mark):
125
- p = os.path.join(root, rel.replace("/", os.sep))
126
- os.makedirs(os.path.dirname(p), exist_ok=True)
127
- with open(p, "w", encoding="utf-8") as f:
128
- f.write("---" + chr(10) + "id: %s" % nid + chr(10) + "layer: knowledge"
129
- + chr(10) + "---" + chr(10) + "MARK-%s" % mark + chr(10))
130
-
131
-
132
- def _gen3(root):
133
- """第三代:只用快照(指纹相符即不重扫)→ 逐 id 读回 {fm_id, mark}。"""
134
- cg = MdCG(root)
135
- out = {}
136
- for nid in tuple(sorted(cg.index["nodes"])): # H-4(a):迭代前取快照
137
- n = cg.get(nid)
138
- out[nid] = None if n is None else {
139
- "fm_id": (n.get("frontmatter") or {}).get("id"),
140
- "mark": [l for l in (n.get("content") or "").splitlines()
141
- if l.startswith("MARK")]}
142
- cg.close()
143
- return out
144
-
145
-
146
- def _probe_stale_base(root, while_open=None):
147
- """库外改动已发生、compact 未跑的时点取三条判据读数。
148
-
149
- 返回 (old_judge, gate_judge, fresh_judge, disk_n, base_n):
150
- · old_judge = 旧判据「.md 计数相等」是否假阳(True ⇒ 它认为陈旧基底可用)
151
- · gate_judge = 完整性闸 _covers_disk(陈旧基底)(路径集合是否仍覆盖盘面)
152
- · fresh_judge= 新鲜度判据 _snapshot_trusted(盘上快照)(盘面动过 ⇒ False)
153
- """
154
- cg = MdCG(root) if while_open is None else while_open
155
- if while_open is None:
156
- d = json.load(open(os.path.join(root, "_index.json"), encoding="utf-8"))
157
- base = dict(d["nodes"])
158
- else:
159
- base = dict(cg.index["nodes"])
160
- old_judge = (cg._count_md_files() == len(base))
161
- gate_judge = cg._covers_disk(base)
162
- fresh_judge = (cg._snapshot_trusted(
163
- json.load(open(os.path.join(root, "_index.json"), encoding="utf-8"))))
164
- if while_open is None:
165
- cg.close()
166
- return old_judge, gate_judge, fresh_judge, len(cg._md_rel_paths()), len(base)
167
-
168
-
169
- def _form(tag, mutate, expect, while_open=False):
170
- root = _root(tag.split(" ")[0])
171
- _build(root)
172
- if while_open:
173
- cg = MdCG(root) # 实例先打开(已装载 S1)
174
- mutate(root)
175
- old_judge, gate_judge, fresh_judge, dn, bn = _probe_stale_base(
176
- root, while_open=cg)
177
- cg.close() # close → compact
178
- else:
179
- mutate(root)
180
- old_judge, gate_judge, fresh_judge, dn, bn = _probe_stale_base(root)
181
- cg = MdCG(root) # 第二代装载(指纹失配 ⇒ 全量重扫)
182
- cg.close() # close → compact
183
- _check("A·%s·旧判据假阳吗(计数相等 ⇒ 它认为陈旧基底可用)" % tag,
184
- old_judge is True and dn == bn, (old_judge, dn, bn))
185
- _check("A·%s·新判据能抓吗①新鲜度:陈旧快照被判不可信(不作基底)" % tag,
186
- fresh_judge is False, fresh_judge)
187
- if expect.get("gate", True):
188
- _check("A·%s·新判据能抓吗②完整性:_covers_disk 判陈旧基底不覆盖盘面" % tag,
189
- gate_judge is False, gate_judge)
190
- got = _gen3(root)
191
- _check("A·%s·端到端读数正确:%s" % (tag, expect["label"]),
192
- expect["ok"](got), got)
193
- return got
194
-
195
-
196
- def f1(root):
197
- os.rename(os.path.join(root, _K, "alpha.md"),
198
- os.path.join(root, _K, "alpha2.md"))
199
-
200
-
201
- def f2(root):
202
- os.makedirs(os.path.join(root, "knowledge/other"), exist_ok=True)
203
- os.rename(os.path.join(root, _K, "alpha.md"),
204
- os.path.join(root, "knowledge/other/alpha.md"))
205
-
206
-
207
- def f3(root):
208
- a = os.path.join(root, _K, "alpha.md")
209
- b = os.path.join(root, _K, "beta.md")
210
- t = a + ".tmp"
211
- os.rename(a, t)
212
- os.rename(b, a)
213
- os.rename(t, b)
214
-
215
-
216
- def f4(root):
217
- os.remove(os.path.join(root, _K, "beta.md"))
218
- _write_md(root, _K + "/brand_new.md", "brand_new", "新")
219
-
220
-
221
- def _is_node(got, nid, mark):
222
- v = got.get(nid)
223
- return bool(v) and v.get("fm_id") == nid and v.get("mark") == ["MARK-" + mark]
224
-
225
-
226
- print("== A 组:净 .md 计数为零的五形态(#409 正题) ==")
227
- _form("F1 改名(alpha.md→alpha2.md)", f1,
228
- {"label": "alpha 仍指向 alpha 的正文(修前为 None=幽灵)",
229
- "ok": lambda g: _is_node(g, "alpha", "甲") and _is_node(g, "beta", "乙")})
230
- _form("F2 移动目录(knowledge/orphan→knowledge/other)", f2,
231
- {"label": "alpha 仍指向 alpha 的正文(修前为 None)",
232
- "ok": lambda g: _is_node(g, "alpha", "甲") and _is_node(g, "beta", "乙")})
233
- # F3 的**路径集合不变**(只是 id↔path 指派陈旧)⇒ 完整性闸抓不住它,
234
- # 由**新鲜度判据**拦(陈旧快照不可信 ⇒ 基底改用本进程内存索引=重扫结果)。
235
- # 这是两个判据分工的活证据:闸只管「路径集合是否覆盖盘面」,不管指派语义。
236
- _form("F3 两条对调(改名式互换内容)", f3,
237
- {"label": "get(alpha).fm_id == alpha(修前为 beta=返回别人的正文)",
238
- "ok": lambda g: _is_node(g, "alpha", "甲") and _is_node(g, "beta", "乙"),
239
- "gate": False})
240
- _form("F4 删一加一(库外同时删 beta.md / 加 brand_new.md)", f4,
241
- {"label": "新增节点可见且幽灵消失(修前:brand_new 不可见 + beta 幽灵)",
242
- "ok": lambda g: _is_node(g, "alpha", "甲") and _is_node(g, "brand_new", "新")
243
- and "beta" not in g})
244
- _form("F5 删一加一·实例已打开期间(他实例写已落盘、其索引增量未 flush)", f4,
245
- {"label": "同上(修前:陈旧内存映射被盖章)",
246
- "ok": lambda g: _is_node(g, "alpha", "甲") and _is_node(g, "brand_new", "新")
247
- and "beta" not in g}, while_open=True)
248
-
249
- print("== K 组:非恒真对照(正常路径不得被过度收紧) ==")
250
- _root_k = _root("k01")
251
- _build(_root_k)
252
- try:
253
- with _no_scan():
254
- _cg = MdCG(_root_k)
255
- _cg.get("alpha")
256
- _cg.close()
257
- _k01 = True
258
- except AssertionError:
259
- _k01 = False
260
- _check("K01 有效快照:装载与 close(compact) 全程不扫盘(#33 热路径未被收紧)", _k01)
261
-
262
- _root_k2 = _root("k02")
263
- _build(_root_k2)
264
- try:
265
- with _no_scan():
266
- _cg = MdCG(_root_k2)
267
- _cg.add("n2", "MARK-二")
268
- _cg.flush()
269
- _cg.close()
270
- _k02 = True
271
- except AssertionError:
272
- _k02 = False
273
- _snap2, _ = _snap(_root_k2)
274
- _check("K02 本进程新增后 close:不扫盘且新节点进快照", _k02 and "n2" in _snap2,
275
- sorted(_snap2))
276
-
277
- _root_k3 = _root("k03")
278
- _build(_root_k3)
279
- try:
280
- with _no_scan():
281
- _cg = MdCG(_root_k3)
282
- _cg.compact_index()
283
- _cg.compact_index()
284
- _k03 = True
285
- except AssertionError:
286
- _k03 = False
287
- _check("K03 连续两次 compact:幂等且不扫盘", _k03)
288
-
289
- _root_k4 = _root("k04")
290
- _build(_root_k4)
291
- try:
292
- _mine = MdCG(_root_k4)
293
- _other = MdCG(_root_k4)
294
- _other.add("n_other", "MARK-他")
295
- _other.flush()
296
- _other.close()
297
- with _no_scan():
298
- _mine.add("n_mine", "MARK-我")
299
- _mine.flush()
300
- _mine.close()
301
- _k04 = True
302
- except AssertionError:
303
- _k04 = False
304
- _snap4, _ = _snap(_root_k4)
305
- _check("K04 他实例 compact 后本实例 close:不扫盘且两边节点都在",
306
- _k04 and sorted(_snap4) == ["alpha", "beta", "n_mine", "n_other"],
307
- sorted(_snap4))
308
-
309
- _root_k5 = _root("k05")
310
- _build(_root_k5)
311
- _cg5 = MdCG(_root_k5)
312
- _check("K05 _count_md_files 语义保持(== 手工层内 .md 计数 == len(_md_rel_paths))",
313
- _cg5._count_md_files() == _count_md(_root_k5)
314
- and _cg5._count_md_files() == len(_cg5._md_rel_paths()),
315
- (_cg5._count_md_files(), _count_md(_root_k5)))
316
-
317
- _d = json.load(open(os.path.join(_root_k5, "_index.json"), encoding="utf-8"))
318
- _check("K06 _snapshot_trusted 边界:真快照 True;无键/None/不符/非 dict 皆 False",
319
- _cg5._snapshot_trusted(_d) is True
320
- and _cg5._snapshot_trusted({"nodes": {}}) is False
321
- and _cg5._snapshot_trusted({"nodes": {}, "_fingerprint": None}) is False
322
- and _cg5._snapshot_trusted({"nodes": {}, "_fingerprint": 1}) is False
323
- and _cg5._snapshot_trusted(None) is False
324
- and _cg5._snapshot_trusted([]) is False, "")
325
- _cg5.close()
326
-
327
- print("== E 组:源码面(单点唯一实现 / 判据互不调用 / 硬边界) ==")
328
- _SRC = open(os.path.join(_REPO, "md_cg", "mdcg.py"), encoding="utf-8").read()
329
- _doc_t = _mdcg.MdCG._snapshot_trusted.__doc__ or ""
330
- _doc_c = _mdcg.MdCG._covers_disk.__doc__ or ""
331
- import ast as _ast # noqa: E402
332
-
333
-
334
- def _fn_code(src, name):
335
- """抽函数**代码体**(剔除 docstring 与注释)——判据互调用的判据必须只看代码。"""
336
- for node in _ast.walk(_ast.parse(src)):
337
- if isinstance(node, _ast.FunctionDef) and node.name == name:
338
- body = [n for n in node.body
339
- if not (isinstance(n, _ast.Expr)
340
- and isinstance(n.value, _ast.Constant)
341
- and isinstance(n.value.value, str))]
342
- return chr(10).join(_ast.unparse(n) for n in body)
343
- return ""
344
-
345
-
346
- _trusted_body = _fn_code(_SRC, "_snapshot_trusted")
347
- _covers_body = _fn_code(_SRC, "_covers_disk")
348
- _check("E01 「可信吗」判定只有一处实现(指纹比较式全文件唯一且在 _snapshot_trusted 体内)",
349
- _SRC.count("== self._dir_fingerprint()") == 1
350
- and "== self._dir_fingerprint()" in _trusted_body,
351
- _SRC.count("== self._dir_fingerprint()"))
352
- _check("E02 装载面与落盘面都接同一单点(load/compact 各恰一处调用)",
353
- _SRC.split("def _load_index")[1].split("def _apply_log")[0]
354
- .count("_snapshot_trusted(") == 1
355
- and _SRC.split("def compact_index")[1].split("def _scan_nodes")[0]
356
- .count("_snapshot_trusted(") == 1)
357
- _check("E03 两个判据互不调用(可信吗里无完整性面;完整性里无可信吗/指纹)",
358
- "_covers_disk" not in _trusted_body and "_md_rel_paths" not in _trusted_body
359
- and "_snapshot_trusted" not in _covers_body
360
- and "_dir_fingerprint" not in _covers_body)
361
- _check("E04 判据自证命名:可信吗 docstring 写明「唯一」「可信」;完整性 docstring 写明「安全」且声明「不是」可信",
362
- "唯一" in _doc_t and "可信" in _doc_t
363
- and "安全" in _doc_c and "不是" in _doc_c)
364
- _check("E05 硬边界①(热路径在位):_load_index 仍走 _snapshot_trusted 不扫盘",
365
- "if not self._snapshot_trusted(idx):" in _SRC)
366
- _check("E06 硬边界②(变异锚点唯一):'if not _usable_index_snapshot(d):' 全文件恰一处且在 compact 内",
367
- _SRC.count("if not _usable_index_snapshot(d):") == 1
368
- and "if not _usable_index_snapshot(d):" in _SRC.split("def compact_index")[1])
369
- _win = _SRC.split("def _maybe_reload_index")[1].split("def _dir_fingerprint")[0]
370
- _check("E07 硬边界③(源码窗口不被截断):窗口仍含三个 invalidate_cache",
371
- all(t in _win for t in ("chain.invalidate_cache", "subgraph.invalidate_cache",
372
- "trust.invalidate_cache")))
373
- _check("E08 硬边界④(计数语义单点):_count_md_files 体内只剩 len(self._md_rel_paths())",
374
- "return len(self._md_rel_paths())"
375
- in _SRC.split("def _count_md_files")[1][:200])
376
- _check("E09 先例延续:_snapshot_trusted docstring 点名 N225 两个共用单点",
377
- "_usable_index_snapshot" in _doc_t and "_apply_log" in _doc_t)
378
-
379
- shutil.rmtree(_TMP, ignore_errors=True)
380
- print("")
381
- if FAILS:
382
- print("issue #409 结果:%d 失败" % len(FAILS))
383
- else:
384
- print("issue #409 结果:全部通过")
385
-
386
-
387
- # ---------------------------------------------------------------- 定点变异
388
- _RAW = open(os.path.join(_REPO, "md_cg", "mdcg.py"), "rb").read()
389
- _NL = "\r\n" if _RAW[:8192].count(b"\r\n") else "\n"
390
-
391
-
392
- def _j(*lines):
393
- """跨行锚点按**目标文件实际行尾**拼接(本仓 CRLF/LF 混合)。"""
394
- return _NL.join(lines)
395
-
396
- _MUTATIONS = (
397
- # 忠实回缺陷:① 陈旧快照照旧作基底(撤掉基底选择)② 摘掉重放前的完整性闸
398
- # ③ 把「.md 计数相等」判据装回**重放之后**(旧形态的时序:重放 → 计数 → 重扫+重放)
399
- ("M1 回缺陷:陈旧快照作基底 + 完整性退回重放后的「.md 计数相等」", "md_cg/mdcg.py",
400
- ((" if not scan_fallback and not self._snapshot_trusted(idx):",
401
- " if False: # 变异:陈旧快照照旧作基底"),
402
- (_j(" if not scan_fallback and not self._covers_disk(idx[\"nodes\"]):",
403
- " idx = {\"schema\": SCHEMA, \"nodes\": self._scan_nodes(),",
404
- " \"buckets\": {}}"),
405
- " pass # 变异:摘掉重放前的完整性闸"),
406
- (_j(" self._apply_log(idx[\"nodes\"])",
407
- " idx[\"buckets\"] = self._count_buckets(idx[\"nodes\"])"),
408
- _j(" self._apply_log(idx[\"nodes\"])",
409
- " if not scan_fallback and self._count_md_files() != len(idx[\"nodes\"]):",
410
- " idx = {\"schema\": SCHEMA,",
411
- " \"nodes\": self._apply_log(self._scan_nodes()),",
412
- " \"buckets\": {}}",
413
- " idx[\"buckets\"] = self._count_buckets(idx[\"nodes\"])"))),
414
- ("A·F1", "A·F2", "A·F3", "A·F4", "A·F5", "E02")),
415
- ("M2 过度收紧:恒判快照不可信(正常路径恒重扫)", "md_cg/mdcg.py",
416
- ((" return fp is not None and fp == self._dir_fingerprint()",
417
- " return False # 变异:恒判不可信"),),
418
- ("E01", "K01", "K02", "K03", "K04", "K06")),
419
- ("M3 过度收紧:完整性闸恒不过(正常路径恒重扫)", "md_cg/mdcg.py",
420
- ((" return len(nodes) == len(disk) and paths == disk",
421
- " return False # 变异:恒不过闸"),),
422
- ("K01", "K02", "K03", "K04")),
423
- )
424
-
425
-
426
- def _mirror_build():
427
- dst = tempfile.mkdtemp(prefix="iss409_mut_")
428
- for name in ("md_cg", "scripts"):
429
- src = os.path.join(_REPO, name)
430
- if os.path.isdir(src):
431
- shutil.copytree(src, os.path.join(dst, name),
432
- ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
433
- for name in sorted(os.listdir(_REPO)):
434
- p = os.path.join(_REPO, name)
435
- if os.path.isfile(p) and (name.endswith(".py")
436
- or name in ("README.md", "pyproject.toml",
437
- "package.json")):
438
- shutil.copy2(p, os.path.join(dst, name))
439
- return dst
440
-
441
-
442
- def _run_in(tree):
443
- env = dict(os.environ)
444
- env["PYTHONPATH"] = tree
445
- env["PYTHONUTF8"] = "1"
446
- env["PYTHONDONTWRITEBYTECODE"] = "1"
447
- env.pop("MDCG_ROOT", None)
448
- pr = subprocess.run([sys.executable, "-X", "utf8", "-m",
449
- "md_cg.test_issue409_compact_index_completeness"],
450
- capture_output=True, text=True, encoding="utf-8",
451
- errors="replace", cwd=tree, env=env, timeout=3600)
452
- out = (pr.stdout or "") + (pr.stderr or "")
453
- reds = [ln.split("FAIL ", 1)[1].strip()
454
- for ln in out.splitlines() if ln.strip().startswith("FAIL ")]
455
- return pr.returncode, reds, out
456
-
457
-
458
- def _mutate():
459
- print("!! 定点变异自证:回缺陷 + 两个方向的过度收紧,守卫必须按指定项转红")
460
- tree = _mirror_build()
461
- try:
462
- rc, reds, out = _run_in(tree)
463
- print(" 未变异镜像基线:rc=%d 红项=%d(必须 rc=0 且 0 红)" % (rc, len(reds)))
464
- if rc != 0 or reds:
465
- print(chr(10).join(out.splitlines()[-25:]))
466
- bad = []
467
- for desc, rel, pairs, exp in _MUTATIONS:
468
- p = os.path.join(tree, rel)
469
- t = open(p, encoding="utf-8", newline="").read()
470
- bak = t
471
- if any(t.count(o) != 1 for o, _n in pairs):
472
- bad.append("%s —— 锚点命中 %s" % (desc, [t.count(o) for o, _n in pairs]))
473
- print(" ANCHOR-MISS " + desc)
474
- continue
475
- for o, n in pairs:
476
- t = t.replace(o, n, 1)
477
- open(p, "w", encoding="utf-8", newline="").write(t)
478
- for root, dd, ff in os.walk(tree):
479
- if os.path.basename(root) == "__pycache__":
480
- shutil.rmtree(root, ignore_errors=True)
481
- rc2, reds2, out2 = _run_in(tree)
482
- open(p, "w", encoding="utf-8", newline="").write(bak)
483
- hit = sorted({r.split(" ")[0] for r in reds2})
484
- want = sorted(exp)
485
- if rc2 != 0 and hit == want:
486
- print(" OK %s 红项=%d 指定命中=%d/%d"
487
- % (desc, len(reds2), len(hit), len(want)))
488
- else:
489
- bad.append(desc)
490
- print(" MISMATCH %s 实际红项=%s 期望=%s rc=%d"
491
- % (desc, hit[:8], want[:8], rc2))
492
- for r in reds2[:10]:
493
- print(" FAIL " + r)
494
- print("定点变异自证:%s(%d 条不合格)"
495
- % ("PASS" if not bad else "FAIL", len(bad)))
496
- for b in bad:
497
- print(" 不合格:" + b)
498
- return len(bad)
499
- finally:
500
- shutil.rmtree(tree, ignore_errors=True)
501
-
502
-
503
- def main():
504
- _bad = _mutate() if "--mutate" in sys.argv else 0
505
- return 1 if (FAILS or _bad) else 0
506
-
507
-
508
- if __name__ == "__main__":
509
- sys.exit(main())
1
+ # -*- coding: utf-8 -*-
2
+ # 功能名:compact_index 的完整性判据必须 sound,「可信吗」判据必须单点(issue #409)
3
+ # 生效条件:python -X utf8 -m md_cg.test_issue409_compact_index_completeness [--mutate];被测面=md_cg/mdcg.py 的 compact_index / _load_index / _snapshot_trusted / _covers_disk
4
+ # 子功能:A 组=五形态「净 .md 计数为零而语义已变」(旧判据假阳 + 新判据能抓 + 端到端读数正确);K 组=非恒真对照(正常路径不得重扫、幂等 compact、可信快照装载不扫盘);E 组=源码面(「可信吗」单点唯一实现、两个判据互不调用、接线在位)
5
+ # 执行:--mutate 时在镜像树里逐条把判据改回缺陷形态 / 过度收紧(恒判不可信、恒不过闸),守卫必须按指定项转红
6
+ # 验证方式:全部 PASS 即通过(退出码 0);--mutate 下每条变异的红项集合须与预期逐项相符(多红/少红皆不合格)
7
+ # 不适用条件:不判「外部纯内容改写(不动文件名)」——那是 _dir_fingerprint 自述的协议外边界(合作写者协议,见其 docstring);不判「实例已打开期间、库外纯改名/对调且路径集合不变」这一残余形态(需以装载时指纹为基线,本批不做,见汇报未覆盖;issue 原文的「更稳」建议亦不覆盖);不判 Rust 侧索引装载(另一实现面);不判 compact 的原子写/锁(属既有守卫)
8
+ #
9
+ # ── 本改动的硬边界登记(改动时撞上的既有限制,及为何这样绕)──────────────────
10
+ # ① 有效快照的重开路径不得扫盘:md_cg/test_index_snapshot_shape.py:191-196 用
11
+ # mock.patch.object(MdCG, "_scan_nodes", side_effect=AssertionError(...)) 钉死
12
+ # ⇒ _snapshot_trusted 必须保热路径(置信即用,不重扫);本次未动该路径。
13
+ # ② compact 里的变异锚点唯一性:同文件 :227-229 的定点变异锚点
14
+ # 'if not _usable_index_snapshot(d):' 必须原样唯一存在于 compact_index
15
+ # ⇒ 本批一字未动该行(形状准入闸保持原样)。
16
+ # ③ 源码窗口区间:md_cg/test_n212_n213_n224_generation_gates.py:481 取
17
+ # 「def _maybe_reload_index → 首个 def _dir_fingerprint」为窗口并断言含三个
18
+ # invalidate_cache ⇒ 新函数不得插在该区间内:_snapshot_trusted / _md_rel_paths /
19
+ # _covers_disk 一律放在 _dir_fingerprint 之后。
20
+ # ④ _count_md_files 的语义必须保持(层内 .md 计数):
21
+ # md_cg/test_n225_nonobject_load.py:688 断言 len(index.nodes) == c._count_md_files()
22
+ # ⇒ 它改为「len(self._md_rel_paths())」——计数不变,只把走查收成同源一份。
23
+ # ⑤ 完整性闸的**位置**(不是既有约束,是本批的论证——写在这里防后人挪动):
24
+ # 闸装在**重放之前**。闸问的是「我拿到的这份**基底**是否恰好描述当前盘面」;
25
+ # 而增量日志是**合作写者协议下的权威写入记录**,其条目**允许指向盘外路径**
26
+ # ——这一点由 md_cg/test_n225_nonobject_load.py 的 E3(同批里的合法 upsert 仍
27
+ # 生效)与 E6(compact 后 nodes 与装载面逐键相等,且同批坏载荷同样记账:
28
+ # 子面共 6 条)钉死。若把闸装在**重放之后**,就会把「日志的正确性」误判成
29
+ # 「基底不完整」、并因此**多付一次重放** ⇒ E6 的记账面立刻转红(本批第一版正是
30
+ # 这样被打红的:payload/total 9 而非 6;根因与前后读数见汇报 §7)。
31
+ # 同源依据:_load_index 的兜底也在**重放之前**换基底(无快照/不可信 ⇒ 先扫盘,
32
+ # 再统一重放)——两面的时序一致。
33
+ #
34
+ # 另一条(不是限制而是先例):_usable_index_snapshot(N225 准入闸)与 _apply_log
35
+ # (N225 重放)本来就是 load/compact 共用单点 ⇒ _snapshot_trusted 是延续既有形态,
36
+ # 不是新立机制。
37
+ """issue #409 守卫:compact_index 的完整性命中判据必须 sound,「快照可信吗」必须单点。
38
+
39
+ 病象(修前):同一个问题(这份快照可信吗)有两套判据——
40
+ · _load_index 用目录指纹(新鲜度);
41
+ · compact_index 用 .md 计数相等(自称完整性),随后无条件把当前指纹盖上去。
42
+ 「净计数为零」的库外变化(改名 / 换目录 / 两文件内容对调 / 删一加一 / 另一实例
43
+ 已落盘而其索引增量还在它自己的 _dirty 里)让计数判据必然假阳 ⇒ 陈旧
44
+ 「节点→path」映射被原样保留 + 盖上当前指纹 ⇒ 下次打开指纹相符、不重扫 ⇒
45
+ get(id) 返回别人的正文;此后一次普通写回(读到→改→写回)会让别人的正文
46
+ 全库无副本(_protected_history / trash / _index_log 均无)——只有全量
47
+ rebuild_index() 能救。
48
+
49
+ 修法(两件事,各自单点、互不调用):
50
+ · 可信吗(新鲜度)⇒ _snapshot_trusted:唯一实现,_load_index 与 compact_index
51
+ 共用(收成单点,不是给 compact 再加一条);
52
+ · 写完安全吗(完整性)⇒ _covers_disk:把原来的 .md 计数相等升级为
53
+ 「索引路径集合 == 盘面 .md 路径集合」(集合相等 ⇒ 计数相等,反之不然),
54
+ 且只在基底确定后、重放之前判定(日志条目按合作写者协议是权威写入记录,
55
+ 允许指向盘外路径——N225 守卫 E3/E6 钉死)。
56
+ 不可信的旧快照不作基底,改吃本进程内存索引;不采用「不可信一律重扫」
57
+ (正常 close 路径里本进程刚写过文件 ⇒ 盘上快照指纹必然失配,一律重扫会退化成
58
+ 每次 close 全库重扫,正是 issue #33 要避免的)。
59
+ """
60
+ import json
61
+ import os
62
+ import shutil
63
+ import subprocess
64
+ import sys
65
+ import tempfile
66
+ from unittest import mock
67
+
68
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
69
+
70
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
71
+ if _REPO not in sys.path:
72
+ sys.path.insert(0, _REPO)
73
+
74
+ from md_cg.mdcg import MdCG # noqa: E402
75
+ from md_cg import mdcg as _mdcg # noqa: E402
76
+
77
+ FAILS = []
78
+ _TMP = tempfile.mkdtemp(prefix="iss409_")
79
+ _K = "knowledge/orphan"
80
+ _BOOM = AssertionError("正常路径不得扫盘(_scan_nodes 被调用)")
81
+
82
+
83
+ def _check(name, cond, detail=""):
84
+ if cond:
85
+ print(" PASS " + name)
86
+ else:
87
+ print(" FAIL " + name + " " + str(detail)[:220])
88
+ FAILS.append(name)
89
+
90
+
91
+ def _no_scan():
92
+ return mock.patch.object(MdCG, "_scan_nodes", side_effect=_BOOM)
93
+
94
+
95
+ def _root(tag):
96
+ d = os.path.join(_TMP, tag)
97
+ os.makedirs(d, exist_ok=True)
98
+ return d
99
+
100
+
101
+ def _build(root, nodes=(("alpha", "甲"), ("beta", "乙"))):
102
+ cg = MdCG(root)
103
+ for nid, mark in nodes:
104
+ cg.add(nid, "MARK-" + mark)
105
+ cg.flush()
106
+ cg.close() # 首次 close:无快照 → rebuild_index 落 S1
107
+ return _snap(root)
108
+
109
+
110
+ def _snap(root):
111
+ d = json.load(open(os.path.join(root, "_index.json"), encoding="utf-8"))
112
+ return ({k: v["path"] for k, v in d["nodes"].items()}, d.get("_fingerprint"))
113
+
114
+
115
+ def _count_md(root):
116
+ n = 0
117
+ for layer in ("anchor", "structural", "knowledge", "contextual", "self",
118
+ "rejected", "unresolved", "goals"):
119
+ for _dp, _dd, ff in os.walk(os.path.join(root, layer)):
120
+ n += sum(1 for f in ff if f.endswith(".md"))
121
+ return n
122
+
123
+
124
+ def _write_md(root, rel, nid, mark):
125
+ p = os.path.join(root, rel.replace("/", os.sep))
126
+ os.makedirs(os.path.dirname(p), exist_ok=True)
127
+ with open(p, "w", encoding="utf-8") as f:
128
+ f.write("---" + chr(10) + "id: %s" % nid + chr(10) + "layer: knowledge"
129
+ + chr(10) + "---" + chr(10) + "MARK-%s" % mark + chr(10))
130
+
131
+
132
+ def _gen3(root):
133
+ """第三代:只用快照(指纹相符即不重扫)→ 逐 id 读回 {fm_id, mark}。"""
134
+ cg = MdCG(root)
135
+ out = {}
136
+ for nid in tuple(sorted(cg.index["nodes"])): # H-4(a):迭代前取快照
137
+ n = cg.get(nid)
138
+ out[nid] = None if n is None else {
139
+ "fm_id": (n.get("frontmatter") or {}).get("id"),
140
+ "mark": [l for l in (n.get("content") or "").splitlines()
141
+ if l.startswith("MARK")]}
142
+ cg.close()
143
+ return out
144
+
145
+
146
+ def _probe_stale_base(root, while_open=None):
147
+ """库外改动已发生、compact 未跑的时点取三条判据读数。
148
+
149
+ 返回 (old_judge, gate_judge, fresh_judge, disk_n, base_n):
150
+ · old_judge = 旧判据「.md 计数相等」是否假阳(True ⇒ 它认为陈旧基底可用)
151
+ · gate_judge = 完整性闸 _covers_disk(陈旧基底)(路径集合是否仍覆盖盘面)
152
+ · fresh_judge= 新鲜度判据 _snapshot_trusted(盘上快照)(盘面动过 ⇒ False)
153
+ """
154
+ cg = MdCG(root) if while_open is None else while_open
155
+ if while_open is None:
156
+ d = json.load(open(os.path.join(root, "_index.json"), encoding="utf-8"))
157
+ base = dict(d["nodes"])
158
+ else:
159
+ base = dict(cg.index["nodes"])
160
+ old_judge = (cg._count_md_files() == len(base))
161
+ gate_judge = cg._covers_disk(base)
162
+ fresh_judge = (cg._snapshot_trusted(
163
+ json.load(open(os.path.join(root, "_index.json"), encoding="utf-8"))))
164
+ if while_open is None:
165
+ cg.close()
166
+ return old_judge, gate_judge, fresh_judge, len(cg._md_rel_paths()), len(base)
167
+
168
+
169
+ def _form(tag, mutate, expect, while_open=False):
170
+ root = _root(tag.split(" ")[0])
171
+ _build(root)
172
+ if while_open:
173
+ cg = MdCG(root) # 实例先打开(已装载 S1)
174
+ mutate(root)
175
+ old_judge, gate_judge, fresh_judge, dn, bn = _probe_stale_base(
176
+ root, while_open=cg)
177
+ cg.close() # close → compact
178
+ else:
179
+ mutate(root)
180
+ old_judge, gate_judge, fresh_judge, dn, bn = _probe_stale_base(root)
181
+ cg = MdCG(root) # 第二代装载(指纹失配 ⇒ 全量重扫)
182
+ cg.close() # close → compact
183
+ _check("A·%s·旧判据假阳吗(计数相等 ⇒ 它认为陈旧基底可用)" % tag,
184
+ old_judge is True and dn == bn, (old_judge, dn, bn))
185
+ _check("A·%s·新判据能抓吗①新鲜度:陈旧快照被判不可信(不作基底)" % tag,
186
+ fresh_judge is False, fresh_judge)
187
+ if expect.get("gate", True):
188
+ _check("A·%s·新判据能抓吗②完整性:_covers_disk 判陈旧基底不覆盖盘面" % tag,
189
+ gate_judge is False, gate_judge)
190
+ got = _gen3(root)
191
+ _check("A·%s·端到端读数正确:%s" % (tag, expect["label"]),
192
+ expect["ok"](got), got)
193
+ return got
194
+
195
+
196
+ def f1(root):
197
+ os.rename(os.path.join(root, _K, "alpha.md"),
198
+ os.path.join(root, _K, "alpha2.md"))
199
+
200
+
201
+ def f2(root):
202
+ os.makedirs(os.path.join(root, "knowledge/other"), exist_ok=True)
203
+ os.rename(os.path.join(root, _K, "alpha.md"),
204
+ os.path.join(root, "knowledge/other/alpha.md"))
205
+
206
+
207
+ def f3(root):
208
+ a = os.path.join(root, _K, "alpha.md")
209
+ b = os.path.join(root, _K, "beta.md")
210
+ t = a + ".tmp"
211
+ os.rename(a, t)
212
+ os.rename(b, a)
213
+ os.rename(t, b)
214
+
215
+
216
+ def f4(root):
217
+ os.remove(os.path.join(root, _K, "beta.md"))
218
+ _write_md(root, _K + "/brand_new.md", "brand_new", "新")
219
+
220
+
221
+ def _is_node(got, nid, mark):
222
+ v = got.get(nid)
223
+ return bool(v) and v.get("fm_id") == nid and v.get("mark") == ["MARK-" + mark]
224
+
225
+
226
+ print("== A 组:净 .md 计数为零的五形态(#409 正题) ==")
227
+ _form("F1 改名(alpha.md→alpha2.md)", f1,
228
+ {"label": "alpha 仍指向 alpha 的正文(修前为 None=幽灵)",
229
+ "ok": lambda g: _is_node(g, "alpha", "甲") and _is_node(g, "beta", "乙")})
230
+ _form("F2 移动目录(knowledge/orphan→knowledge/other)", f2,
231
+ {"label": "alpha 仍指向 alpha 的正文(修前为 None)",
232
+ "ok": lambda g: _is_node(g, "alpha", "甲") and _is_node(g, "beta", "乙")})
233
+ # F3 的**路径集合不变**(只是 id↔path 指派陈旧)⇒ 完整性闸抓不住它,
234
+ # 由**新鲜度判据**拦(陈旧快照不可信 ⇒ 基底改用本进程内存索引=重扫结果)。
235
+ # 这是两个判据分工的活证据:闸只管「路径集合是否覆盖盘面」,不管指派语义。
236
+ _form("F3 两条对调(改名式互换内容)", f3,
237
+ {"label": "get(alpha).fm_id == alpha(修前为 beta=返回别人的正文)",
238
+ "ok": lambda g: _is_node(g, "alpha", "甲") and _is_node(g, "beta", "乙"),
239
+ "gate": False})
240
+ _form("F4 删一加一(库外同时删 beta.md / 加 brand_new.md)", f4,
241
+ {"label": "新增节点可见且幽灵消失(修前:brand_new 不可见 + beta 幽灵)",
242
+ "ok": lambda g: _is_node(g, "alpha", "甲") and _is_node(g, "brand_new", "新")
243
+ and "beta" not in g})
244
+ _form("F5 删一加一·实例已打开期间(他实例写已落盘、其索引增量未 flush)", f4,
245
+ {"label": "同上(修前:陈旧内存映射被盖章)",
246
+ "ok": lambda g: _is_node(g, "alpha", "甲") and _is_node(g, "brand_new", "新")
247
+ and "beta" not in g}, while_open=True)
248
+
249
+ print("== K 组:非恒真对照(正常路径不得被过度收紧) ==")
250
+ _root_k = _root("k01")
251
+ _build(_root_k)
252
+ try:
253
+ with _no_scan():
254
+ _cg = MdCG(_root_k)
255
+ _cg.get("alpha")
256
+ _cg.close()
257
+ _k01 = True
258
+ except AssertionError:
259
+ _k01 = False
260
+ _check("K01 有效快照:装载与 close(compact) 全程不扫盘(#33 热路径未被收紧)", _k01)
261
+
262
+ _root_k2 = _root("k02")
263
+ _build(_root_k2)
264
+ try:
265
+ with _no_scan():
266
+ _cg = MdCG(_root_k2)
267
+ _cg.add("n2", "MARK-二")
268
+ _cg.flush()
269
+ _cg.close()
270
+ _k02 = True
271
+ except AssertionError:
272
+ _k02 = False
273
+ _snap2, _ = _snap(_root_k2)
274
+ _check("K02 本进程新增后 close:不扫盘且新节点进快照", _k02 and "n2" in _snap2,
275
+ sorted(_snap2))
276
+
277
+ _root_k3 = _root("k03")
278
+ _build(_root_k3)
279
+ try:
280
+ with _no_scan():
281
+ _cg = MdCG(_root_k3)
282
+ _cg.compact_index()
283
+ _cg.compact_index()
284
+ _k03 = True
285
+ except AssertionError:
286
+ _k03 = False
287
+ _check("K03 连续两次 compact:幂等且不扫盘", _k03)
288
+
289
+ _root_k4 = _root("k04")
290
+ _build(_root_k4)
291
+ try:
292
+ _mine = MdCG(_root_k4)
293
+ _other = MdCG(_root_k4)
294
+ _other.add("n_other", "MARK-他")
295
+ _other.flush()
296
+ _other.close()
297
+ with _no_scan():
298
+ _mine.add("n_mine", "MARK-我")
299
+ _mine.flush()
300
+ _mine.close()
301
+ _k04 = True
302
+ except AssertionError:
303
+ _k04 = False
304
+ _snap4, _ = _snap(_root_k4)
305
+ _check("K04 他实例 compact 后本实例 close:不扫盘且两边节点都在",
306
+ _k04 and sorted(_snap4) == ["alpha", "beta", "n_mine", "n_other"],
307
+ sorted(_snap4))
308
+
309
+ _root_k5 = _root("k05")
310
+ _build(_root_k5)
311
+ _cg5 = MdCG(_root_k5)
312
+ _check("K05 _count_md_files 语义保持(== 手工层内 .md 计数 == len(_md_rel_paths))",
313
+ _cg5._count_md_files() == _count_md(_root_k5)
314
+ and _cg5._count_md_files() == len(_cg5._md_rel_paths()),
315
+ (_cg5._count_md_files(), _count_md(_root_k5)))
316
+
317
+ _d = json.load(open(os.path.join(_root_k5, "_index.json"), encoding="utf-8"))
318
+ _check("K06 _snapshot_trusted 边界:真快照 True;无键/None/不符/非 dict 皆 False",
319
+ _cg5._snapshot_trusted(_d) is True
320
+ and _cg5._snapshot_trusted({"nodes": {}}) is False
321
+ and _cg5._snapshot_trusted({"nodes": {}, "_fingerprint": None}) is False
322
+ and _cg5._snapshot_trusted({"nodes": {}, "_fingerprint": 1}) is False
323
+ and _cg5._snapshot_trusted(None) is False
324
+ and _cg5._snapshot_trusted([]) is False, "")
325
+ _cg5.close()
326
+
327
+ print("== E 组:源码面(单点唯一实现 / 判据互不调用 / 硬边界) ==")
328
+ _SRC = open(os.path.join(_REPO, "md_cg", "mdcg.py"), encoding="utf-8").read()
329
+ _doc_t = _mdcg.MdCG._snapshot_trusted.__doc__ or ""
330
+ _doc_c = _mdcg.MdCG._covers_disk.__doc__ or ""
331
+ import ast as _ast # noqa: E402
332
+
333
+
334
+ def _fn_code(src, name):
335
+ """抽函数**代码体**(剔除 docstring 与注释)——判据互调用的判据必须只看代码。"""
336
+ for node in _ast.walk(_ast.parse(src)):
337
+ if isinstance(node, _ast.FunctionDef) and node.name == name:
338
+ body = [n for n in node.body
339
+ if not (isinstance(n, _ast.Expr)
340
+ and isinstance(n.value, _ast.Constant)
341
+ and isinstance(n.value.value, str))]
342
+ return chr(10).join(_ast.unparse(n) for n in body)
343
+ return ""
344
+
345
+
346
+ _trusted_body = _fn_code(_SRC, "_snapshot_trusted")
347
+ _covers_body = _fn_code(_SRC, "_covers_disk")
348
+ _check("E01 「可信吗」判定只有一处实现(指纹比较式全文件唯一且在 _snapshot_trusted 体内)",
349
+ _SRC.count("== self._dir_fingerprint()") == 1
350
+ and "== self._dir_fingerprint()" in _trusted_body,
351
+ _SRC.count("== self._dir_fingerprint()"))
352
+ _check("E02 装载面与落盘面都接同一单点(load/compact 各恰一处调用)",
353
+ _SRC.split("def _load_index")[1].split("def _apply_log")[0]
354
+ .count("_snapshot_trusted(") == 1
355
+ and _SRC.split("def compact_index")[1].split("def _scan_nodes")[0]
356
+ .count("_snapshot_trusted(") == 1)
357
+ _check("E03 两个判据互不调用(可信吗里无完整性面;完整性里无可信吗/指纹)",
358
+ "_covers_disk" not in _trusted_body and "_md_rel_paths" not in _trusted_body
359
+ and "_snapshot_trusted" not in _covers_body
360
+ and "_dir_fingerprint" not in _covers_body)
361
+ _check("E04 判据自证命名:可信吗 docstring 写明「唯一」「可信」;完整性 docstring 写明「安全」且声明「不是」可信",
362
+ "唯一" in _doc_t and "可信" in _doc_t
363
+ and "安全" in _doc_c and "不是" in _doc_c)
364
+ _check("E05 硬边界①(热路径在位):_load_index 仍走 _snapshot_trusted 不扫盘",
365
+ "if not self._snapshot_trusted(idx):" in _SRC)
366
+ _check("E06 硬边界②(变异锚点唯一):'if not _usable_index_snapshot(d):' 全文件恰一处且在 compact 内",
367
+ _SRC.count("if not _usable_index_snapshot(d):") == 1
368
+ and "if not _usable_index_snapshot(d):" in _SRC.split("def compact_index")[1])
369
+ _win = _SRC.split("def _maybe_reload_index")[1].split("def _dir_fingerprint")[0]
370
+ _check("E07 硬边界③(源码窗口不被截断):窗口仍含三个 invalidate_cache",
371
+ all(t in _win for t in ("chain.invalidate_cache", "subgraph.invalidate_cache",
372
+ "trust.invalidate_cache")))
373
+ _check("E08 硬边界④(计数语义单点):_count_md_files 体内只剩 len(self._md_rel_paths())",
374
+ "return len(self._md_rel_paths())"
375
+ in _SRC.split("def _count_md_files")[1][:200])
376
+ _check("E09 先例延续:_snapshot_trusted docstring 点名 N225 两个共用单点",
377
+ "_usable_index_snapshot" in _doc_t and "_apply_log" in _doc_t)
378
+
379
+ shutil.rmtree(_TMP, ignore_errors=True)
380
+ print("")
381
+ if FAILS:
382
+ print("issue #409 结果:%d 失败" % len(FAILS))
383
+ else:
384
+ print("issue #409 结果:全部通过")
385
+
386
+
387
+ # ---------------------------------------------------------------- 定点变异
388
+ _RAW = open(os.path.join(_REPO, "md_cg", "mdcg.py"), "rb").read()
389
+ _NL = "\r\n" if _RAW[:8192].count(b"\r\n") else "\n"
390
+
391
+
392
+ def _j(*lines):
393
+ """跨行锚点按**目标文件实际行尾**拼接(本仓 CRLF/LF 混合)。"""
394
+ return _NL.join(lines)
395
+
396
+ _MUTATIONS = (
397
+ # 忠实回缺陷:① 陈旧快照照旧作基底(撤掉基底选择)② 摘掉重放前的完整性闸
398
+ # ③ 把「.md 计数相等」判据装回**重放之后**(旧形态的时序:重放 → 计数 → 重扫+重放)
399
+ ("M1 回缺陷:陈旧快照作基底 + 完整性退回重放后的「.md 计数相等」", "md_cg/mdcg.py",
400
+ ((" if not scan_fallback and not self._snapshot_trusted(idx):",
401
+ " if False: # 变异:陈旧快照照旧作基底"),
402
+ (_j(" if not scan_fallback and not self._covers_disk(idx[\"nodes\"]):",
403
+ " idx = {\"schema\": SCHEMA, \"nodes\": self._scan_nodes(),",
404
+ " \"buckets\": {}}"),
405
+ " pass # 变异:摘掉重放前的完整性闸"),
406
+ (_j(" self._apply_log(idx[\"nodes\"])",
407
+ " idx[\"buckets\"] = self._count_buckets(idx[\"nodes\"])"),
408
+ _j(" self._apply_log(idx[\"nodes\"])",
409
+ " if not scan_fallback and self._count_md_files() != len(idx[\"nodes\"]):",
410
+ " idx = {\"schema\": SCHEMA,",
411
+ " \"nodes\": self._apply_log(self._scan_nodes()),",
412
+ " \"buckets\": {}}",
413
+ " idx[\"buckets\"] = self._count_buckets(idx[\"nodes\"])"))),
414
+ ("A·F1", "A·F2", "A·F3", "A·F4", "A·F5", "E02")),
415
+ ("M2 过度收紧:恒判快照不可信(正常路径恒重扫)", "md_cg/mdcg.py",
416
+ ((" return fp is not None and fp == self._dir_fingerprint()",
417
+ " return False # 变异:恒判不可信"),),
418
+ ("E01", "K01", "K02", "K03", "K04", "K06")),
419
+ ("M3 过度收紧:完整性闸恒不过(正常路径恒重扫)", "md_cg/mdcg.py",
420
+ ((" return len(nodes) == len(disk) and paths == disk",
421
+ " return False # 变异:恒不过闸"),),
422
+ ("K01", "K02", "K03", "K04")),
423
+ )
424
+
425
+
426
+ def _mirror_build():
427
+ dst = tempfile.mkdtemp(prefix="iss409_mut_")
428
+ for name in ("md_cg", "scripts"):
429
+ src = os.path.join(_REPO, name)
430
+ if os.path.isdir(src):
431
+ shutil.copytree(src, os.path.join(dst, name),
432
+ ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
433
+ for name in sorted(os.listdir(_REPO)):
434
+ p = os.path.join(_REPO, name)
435
+ if os.path.isfile(p) and (name.endswith(".py")
436
+ or name in ("README.md", "pyproject.toml",
437
+ "package.json")):
438
+ shutil.copy2(p, os.path.join(dst, name))
439
+ return dst
440
+
441
+
442
+ def _run_in(tree):
443
+ env = dict(os.environ)
444
+ env["PYTHONPATH"] = tree
445
+ env["PYTHONUTF8"] = "1"
446
+ env["PYTHONDONTWRITEBYTECODE"] = "1"
447
+ env.pop("MDCG_ROOT", None)
448
+ pr = subprocess.run([sys.executable, "-X", "utf8", "-m",
449
+ "md_cg.test_issue409_compact_index_completeness"],
450
+ capture_output=True, text=True, encoding="utf-8",
451
+ errors="replace", cwd=tree, env=env, timeout=3600)
452
+ out = (pr.stdout or "") + (pr.stderr or "")
453
+ reds = [ln.split("FAIL ", 1)[1].strip()
454
+ for ln in out.splitlines() if ln.strip().startswith("FAIL ")]
455
+ return pr.returncode, reds, out
456
+
457
+
458
+ def _mutate():
459
+ print("!! 定点变异自证:回缺陷 + 两个方向的过度收紧,守卫必须按指定项转红")
460
+ tree = _mirror_build()
461
+ try:
462
+ rc, reds, out = _run_in(tree)
463
+ print(" 未变异镜像基线:rc=%d 红项=%d(必须 rc=0 且 0 红)" % (rc, len(reds)))
464
+ if rc != 0 or reds:
465
+ print(chr(10).join(out.splitlines()[-25:]))
466
+ bad = []
467
+ for desc, rel, pairs, exp in _MUTATIONS:
468
+ p = os.path.join(tree, rel)
469
+ t = open(p, encoding="utf-8", newline="").read()
470
+ bak = t
471
+ if any(t.count(o) != 1 for o, _n in pairs):
472
+ bad.append("%s —— 锚点命中 %s" % (desc, [t.count(o) for o, _n in pairs]))
473
+ print(" ANCHOR-MISS " + desc)
474
+ continue
475
+ for o, n in pairs:
476
+ t = t.replace(o, n, 1)
477
+ open(p, "w", encoding="utf-8", newline="").write(t)
478
+ for root, dd, ff in os.walk(tree):
479
+ if os.path.basename(root) == "__pycache__":
480
+ shutil.rmtree(root, ignore_errors=True)
481
+ rc2, reds2, out2 = _run_in(tree)
482
+ open(p, "w", encoding="utf-8", newline="").write(bak)
483
+ hit = sorted({r.split(" ")[0] for r in reds2})
484
+ want = sorted(exp)
485
+ if rc2 != 0 and hit == want:
486
+ print(" OK %s 红项=%d 指定命中=%d/%d"
487
+ % (desc, len(reds2), len(hit), len(want)))
488
+ else:
489
+ bad.append(desc)
490
+ print(" MISMATCH %s 实际红项=%s 期望=%s rc=%d"
491
+ % (desc, hit[:8], want[:8], rc2))
492
+ for r in reds2[:10]:
493
+ print(" FAIL " + r)
494
+ print("定点变异自证:%s(%d 条不合格)"
495
+ % ("PASS" if not bad else "FAIL", len(bad)))
496
+ for b in bad:
497
+ print(" 不合格:" + b)
498
+ return len(bad)
499
+ finally:
500
+ shutil.rmtree(tree, ignore_errors=True)
501
+
502
+
503
+ def main():
504
+ _bad = _mutate() if "--mutate" in sys.argv else 0
505
+ return 1 if (FAILS or _bad) else 0
506
+
507
+
508
+ if __name__ == "__main__":
509
+ sys.exit(main())