@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,427 +1,427 @@
1
- # -*- coding: utf-8 -*-
2
- r"""守卫 · issue #470(P1)swarm WAL 事件行 flush 是 no-op(durable 纪律背离)。
3
-
4
- 缺陷(本件修复 · swarm/rust_runtime/src/swarm.rs)
5
- --------------------------------------------------
6
- `open_wal_for_run` 返回 `std::fs::File`(无用户态缓冲),而 Rust 的
7
- `std::fs::File::flush()` 是 **no-op**(`write_all` 只进 page cache)。旧实现
8
- 在事件行写完后调 `wal.flush()` 并注释「事件行先 durable」,实为空背书:
9
- 事件行与轮末快照行一起等 `wal.sync_all()`,崩溃落在「事件行 write 后、轮末
10
- sync 前」窗口 ⇒ 整轮路由/ACK/水位蒸发,B1 断点恢复的可恢复边界比宣称窄。
11
-
12
- 修复:事件行(含修正信号行)写完后显式 `wal.sync_data()`(只同步数据),
13
- 之后才写轮末快照行;轮末 `sync_all()` 仍负责快照。并新增可观测读数
14
- `wal_durable.event_syncs / last_sync_round` 随报告透出——「是否真的过了一个
15
- durable 点」不再只能靠读注释。
16
-
17
- 【功能名】swarm WAL 事件行独立 durable 判据
18
- 【生效条件】`protocol_vm swarm --config --wal` 执行至少一轮(早退路径无事件行
19
- 写入,读数应为 0);源码面断言在任意状态可判。
20
- 【子功能】A 行为面(真二进制):每轮事件行 sync_data 计数 == 执行轮数、早退
21
- 不伪造计数;B 源断言:sync_data 在快照写之前、无 `wal.flush()` 代码用法、
22
- 报告出口在场、反陈化点名 issue;C 定点变异:回退修复 ⇒ 判据转红。
23
- 【执行】python -X utf8 -m md_cg.test_issue470_wal_event_durable
24
- python -X utf8 -m md_cg.test_issue470_wal_event_durable --mutate
25
- 【验证方式】退出码 0 全绿;`--mutate` 下每个变异体都点名预期红项且非空。
26
- 【不适用条件】无 cargo 且无预构建 protocol_vm 时行为面 SKIP(源断言面照判并
27
- 决定 rc);不覆盖 fsync 的物理介质语义(OS/文件系统层),只判「调用点与
28
- 顺序 + 可观测计数」。
29
- """
30
- from __future__ import annotations
31
-
32
- import hashlib
33
- import hmac as _hmac
34
- import io
35
- import json
36
- import os
37
- import shutil
38
- import subprocess
39
- import sys
40
- import tempfile
41
-
42
- try:
43
- sys.stdout.reconfigure(encoding="utf-8", errors="replace")
44
- except AttributeError:
45
- pass
46
-
47
- _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
48
- _RT_DIR = os.path.join(_REPO, "swarm", "rust_runtime")
49
- _SWARM_RS = os.path.join(_RT_DIR, "src", "swarm.rs")
50
-
51
- PASS = 0
52
- FAIL = 0
53
- FAILS = []
54
- SKIPS = []
55
- _SANDBOXES = []
56
- _SILENT = False
57
- _VIOL = []
58
-
59
- SECRET = "i470-哑密钥-" + os.urandom(6).hex()
60
- SNAPSHOT_TYPE = "__snapshot__"
61
-
62
-
63
- def check(name, cond, detail=""):
64
- global PASS, FAIL
65
- if _SILENT:
66
- if not cond:
67
- _VIOL.append(name)
68
- return bool(cond)
69
- if cond:
70
- PASS += 1
71
- print(" [PASS] %s%s" % (name, (" · " + str(detail)) if detail else ""))
72
- else:
73
- FAIL += 1
74
- FAILS.append(name)
75
- print(" [FAIL] %s · %s" % (name, detail))
76
- return bool(cond)
77
-
78
-
79
- def skip(name, why):
80
- SKIPS.append(name)
81
- print(" [SKIP] %s · %s" % (name, why))
82
-
83
-
84
- def _exe_path():
85
- env = os.environ.get("PROTOCOL_VM_EXE")
86
- if env:
87
- return env
88
- base = os.path.join(_RT_DIR, "target", "release", "protocol_vm")
89
- return base + ".exe" if os.name == "nt" else base
90
-
91
-
92
- EXE = _exe_path()
93
- _BUILD_CMD = "cargo build --release --manifest-path swarm/rust_runtime/Cargo.toml"
94
-
95
-
96
- def _ensure_exe():
97
- if os.path.isfile(EXE):
98
- return True
99
- if not shutil.which("cargo") or not os.path.isdir(_RT_DIR):
100
- print(" (%s 不在场且 cargo 不可用)" % EXE)
101
- return False
102
- print(" 自建:%s" % _BUILD_CMD)
103
- r = subprocess.run(["cargo", "build", "--release",
104
- "--manifest-path", os.path.join(_RT_DIR, "Cargo.toml")],
105
- capture_output=True, text=True, encoding="utf-8",
106
- errors="replace", timeout=900)
107
- if r.returncode != 0:
108
- print(" 构建失败:%s" % (r.stderr or "")[-400:])
109
- return False
110
- return os.path.isfile(EXE)
111
-
112
-
113
- # ============================ WAL 合成 ============================
114
-
115
- def _sign(seq, typ, frm, to, rnd, ts, payload):
116
- msg = "%d|%s|%s|%s|%d|%d|%s" % (seq, typ, frm, to, rnd, ts, payload)
117
- return _hmac.new(SECRET.encode(), msg.encode(), hashlib.sha256).hexdigest()
118
-
119
-
120
- def _line(seq, typ, frm, to, rnd, payload, ts=1758888800000):
121
- return ('{"seq":%d,"ts":%d,"from":"%s","to":"%s","type":"%s","round":%d,'
122
- '"level":0,"hmac":"%s","payload":%s}'
123
- % (seq, ts, frm, to, typ, rnd, _sign(seq, typ, frm, to, rnd, ts, payload),
124
- payload))
125
-
126
-
127
- def _snap(seq, rnd, trusts='"i1":0.5'):
128
- return _line(seq, SNAPSHOT_TYPE, "协调器", "协调器", rnd,
129
- '{"trusts":{%s},"states":{}}' % trusts)
130
-
131
-
132
- def _ev(seq, rnd, payload='{"x":1}'):
133
- return _line(seq, "MSG", "i1", "i1", rnd, payload)
134
-
135
-
136
- CONFIG = {"algo": "rust_swarm-0.1", "shared_secret": SECRET, "rounds": 2,
137
- "topology": "",
138
- "routes": [{"from": "i1", "event_type": "消息", "to": "i1",
139
- "payload": {"x": 1}}],
140
- "instances": [{"id": "i1", "role": "worker", "trust": 0.5, "symbols": {}}]}
141
-
142
- _PBC = [None]
143
- _PBC_DIR = tempfile.mkdtemp(prefix="i470_pbc_")
144
-
145
-
146
- def _pbc_path():
147
- if _PBC[0] is None:
148
- try:
149
- sys.path.insert(0, _REPO)
150
- from compiler.pbc import compile_to_pbc
151
- p = os.path.join(_PBC_DIR, "program.pbc")
152
- ok, _r = compile_to_pbc("术曰:\n1。道 新信任路径;\n2。止。\n", p)
153
- _PBC[0] = p if ok and os.path.exists(p) else ""
154
- except Exception as exc: # noqa: BLE001
155
- print(" (pbc 编译不可用,实例走自死形态:%s)" % exc)
156
- _PBC[0] = ""
157
- return _PBC[0]
158
-
159
-
160
- def _mkspace(tag):
161
- t = tempfile.mkdtemp(prefix="i470_%s_" % tag)
162
- _SANDBOXES.append(t)
163
- return t
164
-
165
-
166
- def _run(t, wal, rounds=2, timeout=120):
167
- cfg = dict(CONFIG)
168
- cfg["rounds"] = rounds
169
- cp = os.path.join(t, "swarm.json")
170
- io.open(cp, "w", encoding="utf-8", newline="").write(
171
- json.dumps(cfg, ensure_ascii=False))
172
- cmd = [EXE, "swarm", "--config", cp, "--wal", wal]
173
- pbc = _pbc_path()
174
- if pbc:
175
- cmd += ["--pbc", pbc]
176
- p = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8",
177
- errors="replace", timeout=timeout, cwd=t)
178
- rep = None
179
- if p.stdout.strip():
180
- try:
181
- rep = json.loads(p.stdout.strip().splitlines()[-1])
182
- except Exception: # noqa: BLE001
183
- rep = None
184
- return p.returncode, rep, (p.stderr or "")
185
-
186
-
187
- # ============================ [A] 行为面 ============================
188
-
189
- def a1_event_syncs_per_round():
190
- print("== A1 每轮事件行独立 durable:event_syncs == 执行轮数 ==")
191
- t = _mkspace("r2")
192
- wal = os.path.join(t, "events.jsonl")
193
- rc, rep, err = _run(t, wal, rounds=2)
194
- wd = (rep or {}).get("wal_durable")
195
- check("A1a 报告必须带 wal_durable 读数(可观测出口)",
196
- isinstance(wd, dict), wd)
197
- check("A1b coordinator 正常退出", rc == 0, (rc, err[-200:]))
198
- check("A1c event_syncs == 2(每轮一次,修前无此读数)",
199
- bool(wd) and wd.get("event_syncs") == 2, wd)
200
- check("A1d last_sync_round == 2", bool(wd) and wd.get("last_sync_round") == 2, wd)
201
- # 事件行与快照行都必须在盘上(顺序判据在 B 面做源码级断言)
202
- data = io.open(wal, "rb").read()
203
- rows = []
204
- for raw in data.split(b"\n"):
205
- if raw.strip():
206
- try:
207
- rows.append(json.loads(raw.decode("utf-8")))
208
- except Exception: # noqa: BLE001
209
- rows.append({"type": "<坏行>"})
210
- evs = [r for r in rows if r.get("type") != SNAPSHOT_TYPE]
211
- snaps = [r for r in rows if r.get("type") == SNAPSHOT_TYPE]
212
- check("A1e WAL 同时含事件行与快照行(有事件可判 durable 顺序)",
213
- bool(evs) and len(snaps) == 2, {"events": len(evs), "snaps": len(snaps)})
214
- shutil.rmtree(t, ignore_errors=True)
215
-
216
-
217
- def a2_early_exit_no_fake_count():
218
- print("== A2 早退路径(目标轮已全部持久化)不得伪造 durable 计数 ==")
219
- t = _mkspace("done")
220
- wal = os.path.join(t, "events.jsonl")
221
- io.open(wal, "wb").write(
222
- ("\n".join([_snap(1, 1), _ev(2, 1), _snap(3, 2)]) + "\n").encode("utf-8"))
223
- rc, rep, err = _run(t, wal, rounds=2)
224
- wd = (rep or {}).get("wal_durable")
225
- check("A2a 早退 rc=0", rc == 0, (rc, err[-200:]))
226
- check("A2b 早退 event_syncs == 0(本会话无事件行写入)",
227
- isinstance(wd, dict) and wd.get("event_syncs") == 0, wd)
228
- shutil.rmtree(t, ignore_errors=True)
229
-
230
-
231
- # ============================ [B] 源断言 ============================
232
-
233
- def _src():
234
- return io.open(_SWARM_RS, encoding="utf-8", errors="replace").read()
235
-
236
-
237
- def b_source_face():
238
- print("== B 源断言(行为面不可用时仍决定 rc)==")
239
- src = _src()
240
- i_sync = src.find("wal.sync_data()")
241
- i_snap = src.find("wal.write_all(snap_line.as_bytes())")
242
- check("B1 事件行显式 sync_data 在场", i_sync >= 0)
243
- check("B1b sync_data 在轮末快照写入之前(快照永不先于产生它的写入落盘)",
244
- i_sync >= 0 and i_snap >= 0 and i_sync < i_snap,
245
- (i_sync, i_snap))
246
- # 代码位置的 wal.flush() 用法必须消失(注释里点名旧实现不算)
247
- code_flush = [ln for ln in src.splitlines()
248
- if ln.strip().startswith("wal.flush(")]
249
- check("B2 事件行 durable 不再依赖 File::flush()(no-op)",
250
- not code_flush, code_flush[:2])
251
- check("B3 报告出口 wal_durable 在场",
252
- '\\"wal_durable\\":{{' in src and "rep.wal_durable.event_syncs" in src)
253
- check("B4 反陈化:修复点名 issue #470",
254
- "#470" in src)
255
- check("B5 轮末 sync_all 仍在(快照 durable 未回退)",
256
- "wal.sync_all()" in src)
257
-
258
-
259
- # ============================ 主流程 ============================
260
-
261
- _BEHAVIOR = (a1_event_syncs_per_round, a2_early_exit_no_fake_count)
262
-
263
-
264
- def _run_groups(behavior_ok):
265
- global PASS, FAIL
266
- PASS = 0
267
- FAIL = 0
268
- del FAILS[:]
269
- del SKIPS[:]
270
- for g in _BEHAVIOR:
271
- if not behavior_ok:
272
- skip(g.__name__, "protocol_vm 不可得(构建命令:%s)" % _BUILD_CMD)
273
- continue
274
- try:
275
- g()
276
- except Exception as exc: # noqa: BLE001
277
- check("断言组 %s 抛异常:%s" % (g.__name__, exc), False)
278
- import traceback
279
- print(traceback.format_exc()[-700:])
280
- b_source_face()
281
- return len(FAILS)
282
-
283
-
284
- def main():
285
- # 锚点自检(fail-closed):修法的关键判据不在源码里 ⇒ exit 2,不给假绿。
286
- src = _src()
287
- missing = [a for a in ("wal.sync_data()", '\\"wal_durable\\":{{',
288
- "rep.wal_durable.event_syncs") if a not in src]
289
- if missing:
290
- print("锚点自检失败:关键判据字面量缺失 %s" % missing)
291
- return 2
292
- behavior_ok = _ensure_exe()
293
- if not behavior_ok:
294
- print("!! 行为面 SKIP:%s 不在场且无法自建 —— 构建命令:%s"
295
- % (EXE, _BUILD_CMD))
296
- n = _run_groups(behavior_ok)
297
- print()
298
- print("issue470 WAL 事件行 durable 守卫:%d 通过,%d 失败,%d 跳过"
299
- % (PASS, n, len(SKIPS)))
300
- return 1 if n else 0
301
-
302
-
303
- # ============================ [C] 定点变异自证 ============================
304
-
305
- _SYNC_BLOCK = (
306
- " wal.sync_data().map_err(|e| format!(\"WAL 事件行 sync 失败: {e}\"))?;\n"
307
- " wal_durable.event_syncs += 1;\n"
308
- " wal_durable.last_sync_round = round;\n")
309
- _REPORT_BLOCK = (
310
- " // issue #470:事件行 durable 读数(每轮事件行 sync_data 后才写快照)。\n"
311
- " out.push_str(&format!(\n"
312
- " \",\\\"wal_durable\\\":{{\\\"event_syncs\\\":{},\\\"last_sync_round\\\":{}}}\",\n"
313
- " rep.wal_durable.event_syncs, rep.wal_durable.last_sync_round\n"
314
- " ));\n")
315
- _SNAP_SYNC_ALL = " wal.sync_all().map_err(|e| format!(\"WAL sync 失败: {e}\"))?;\n"
316
-
317
- _MUTATIONS = (
318
- ("V1 回退:sync_data 改回 File::flush()(no-op)",
319
- _SYNC_BLOCK,
320
- " wal.flush().map_err(|e| format!(\"WAL flush 失败: {e}\"))?;\n",
321
- "A1c", "B1", "B2"),
322
- ("V2 回退:删掉 wal_durable 报告出口",
323
- _REPORT_BLOCK,
324
- "",
325
- "A1a", "A1c", "B3"),
326
- ("V3 顺序回退:sync_data 挪到快照写入之后",
327
- _SYNC_BLOCK,
328
- "",
329
- "B1b"),
330
- )
331
-
332
-
333
- def _rebuild():
334
- r = subprocess.run(["cargo", "build", "--release",
335
- "--manifest-path", os.path.join(_RT_DIR, "Cargo.toml")],
336
- capture_output=True, text=True, encoding="utf-8",
337
- errors="replace", timeout=900)
338
- return r.returncode == 0, (r.stderr or "")[-400:]
339
-
340
-
341
- def _mutate():
342
- bad = []
343
- if not os.path.isfile(_SWARM_RS):
344
- print(" SOURCE-MISSING:%s" % _SWARM_RS)
345
- return 1
346
- if not shutil.which("cargo"):
347
- print(" NO-CARGO:变异自证需要 cargo(改源后必须重建)")
348
- return 1
349
- with io.open(_SWARM_RS, encoding="utf-8", newline="") as f:
350
- pristine = f.read()
351
- global _SILENT
352
- for name, anchor, repl, *want in _MUTATIONS:
353
- with io.open(_SWARM_RS, encoding="utf-8", newline="") as f:
354
- orig = f.read()
355
- NL = "\r\n" if "\r\n" in orig else "\n"
356
- a_ = anchor.replace("\n", NL)
357
- r_ = repl.replace("\n", NL)
358
- if a_ not in orig:
359
- print(" ANCHOR-MISS %s" % name)
360
- bad.append(name)
361
- continue
362
- mutated = orig.replace(a_, r_, 1)
363
- if name.startswith("V3"):
364
- # 顺序回退:删掉原位同步块,把同一调用挪到轮末 sync_all 之前
365
- # (此时快照行已 write_all 落进 page cache ⇒ 快照可先于事件行落盘)
366
- sw = _SNAP_SYNC_ALL.replace("\n", NL)
367
- if sw not in mutated:
368
- print(" ANCHOR-MISS %s(轮末 sync_all 点)" % name)
369
- bad.append(name)
370
- continue
371
- mutated = mutated.replace(
372
- sw,
373
- " wal.sync_data().map_err(|e| format!(\"WAL 事件行 sync 失败: {e}\"))?;\n"
374
- " wal_durable.event_syncs += 1;\n"
375
- " wal_durable.last_sync_round = round;\n".replace("\n", NL) + sw,
376
- 1)
377
- try:
378
- with io.open(_SWARM_RS, "w", encoding="utf-8", newline="") as f:
379
- f.write(mutated)
380
- ok_build, blog = _rebuild()
381
- if not ok_build:
382
- print(" BUILD-FAIL %s:%s" % (name, blog))
383
- bad.append(name + "(构建失败)")
384
- continue
385
- _SILENT = True
386
- del _VIOL[:]
387
- _run_groups(True)
388
- reds = list(_VIOL)
389
- hit = [w for w in want if any(x.startswith(w) for x in reds)]
390
- print(" %-4s %-46s 红项=%-3d 命中=%s"
391
- % ("OK" if len(hit) == len(want) else "BAD", name, len(reds),
392
- ",".join(hit) if hit else "无"))
393
- if len(hit) != len(want):
394
- print(" 期望红:%s;实红:%s" % (list(want), reds[:8]))
395
- bad.append(name)
396
- finally:
397
- _SILENT = False
398
- with io.open(_SWARM_RS, "w", encoding="utf-8", newline="") as f:
399
- f.write(pristine)
400
- with io.open(_SWARM_RS, encoding="utf-8", newline="") as f:
401
- if f.read() != pristine:
402
- print(" RESTORE-FAILED %s" % name)
403
- bad.append(name + "(恢复失败)")
404
- _rebuild()
405
- if bad:
406
- print()
407
- print("定点变异自证:FAIL -- %s" % "、".join(bad))
408
- return 1
409
- print()
410
- print("定点变异自证:PASS(每处判据都被打红)")
411
- return 0
412
-
413
-
414
- def _cleanup():
415
- for t in _SANDBOXES:
416
- shutil.rmtree(t, ignore_errors=True)
417
- shutil.rmtree(_PBC_DIR, ignore_errors=True)
418
-
419
-
420
- if __name__ == "__main__":
421
- if "--mutate" in sys.argv:
422
- rc = _mutate()
423
- _cleanup()
424
- sys.exit(rc)
425
- rc = main()
426
- _cleanup()
427
- sys.exit(rc)
1
+ # -*- coding: utf-8 -*-
2
+ r"""守卫 · issue #470(P1)swarm WAL 事件行 flush 是 no-op(durable 纪律背离)。
3
+
4
+ 缺陷(本件修复 · swarm/rust_runtime/src/swarm.rs)
5
+ --------------------------------------------------
6
+ `open_wal_for_run` 返回 `std::fs::File`(无用户态缓冲),而 Rust 的
7
+ `std::fs::File::flush()` 是 **no-op**(`write_all` 只进 page cache)。旧实现
8
+ 在事件行写完后调 `wal.flush()` 并注释「事件行先 durable」,实为空背书:
9
+ 事件行与轮末快照行一起等 `wal.sync_all()`,崩溃落在「事件行 write 后、轮末
10
+ sync 前」窗口 ⇒ 整轮路由/ACK/水位蒸发,B1 断点恢复的可恢复边界比宣称窄。
11
+
12
+ 修复:事件行(含修正信号行)写完后显式 `wal.sync_data()`(只同步数据),
13
+ 之后才写轮末快照行;轮末 `sync_all()` 仍负责快照。并新增可观测读数
14
+ `wal_durable.event_syncs / last_sync_round` 随报告透出——「是否真的过了一个
15
+ durable 点」不再只能靠读注释。
16
+
17
+ 【功能名】swarm WAL 事件行独立 durable 判据
18
+ 【生效条件】`protocol_vm swarm --config --wal` 执行至少一轮(早退路径无事件行
19
+ 写入,读数应为 0);源码面断言在任意状态可判。
20
+ 【子功能】A 行为面(真二进制):每轮事件行 sync_data 计数 == 执行轮数、早退
21
+ 不伪造计数;B 源断言:sync_data 在快照写之前、无 `wal.flush()` 代码用法、
22
+ 报告出口在场、反陈化点名 issue;C 定点变异:回退修复 ⇒ 判据转红。
23
+ 【执行】python -X utf8 -m md_cg.test_issue470_wal_event_durable
24
+ python -X utf8 -m md_cg.test_issue470_wal_event_durable --mutate
25
+ 【验证方式】退出码 0 全绿;`--mutate` 下每个变异体都点名预期红项且非空。
26
+ 【不适用条件】无 cargo 且无预构建 protocol_vm 时行为面 SKIP(源断言面照判并
27
+ 决定 rc);不覆盖 fsync 的物理介质语义(OS/文件系统层),只判「调用点与
28
+ 顺序 + 可观测计数」。
29
+ """
30
+ from __future__ import annotations
31
+
32
+ import hashlib
33
+ import hmac as _hmac
34
+ import io
35
+ import json
36
+ import os
37
+ import shutil
38
+ import subprocess
39
+ import sys
40
+ import tempfile
41
+
42
+ try:
43
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
44
+ except AttributeError:
45
+ pass
46
+
47
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
48
+ _RT_DIR = os.path.join(_REPO, "swarm", "rust_runtime")
49
+ _SWARM_RS = os.path.join(_RT_DIR, "src", "swarm.rs")
50
+
51
+ PASS = 0
52
+ FAIL = 0
53
+ FAILS = []
54
+ SKIPS = []
55
+ _SANDBOXES = []
56
+ _SILENT = False
57
+ _VIOL = []
58
+
59
+ SECRET = "i470-哑密钥-" + os.urandom(6).hex()
60
+ SNAPSHOT_TYPE = "__snapshot__"
61
+
62
+
63
+ def check(name, cond, detail=""):
64
+ global PASS, FAIL
65
+ if _SILENT:
66
+ if not cond:
67
+ _VIOL.append(name)
68
+ return bool(cond)
69
+ if cond:
70
+ PASS += 1
71
+ print(" [PASS] %s%s" % (name, (" · " + str(detail)) if detail else ""))
72
+ else:
73
+ FAIL += 1
74
+ FAILS.append(name)
75
+ print(" [FAIL] %s · %s" % (name, detail))
76
+ return bool(cond)
77
+
78
+
79
+ def skip(name, why):
80
+ SKIPS.append(name)
81
+ print(" [SKIP] %s · %s" % (name, why))
82
+
83
+
84
+ def _exe_path():
85
+ env = os.environ.get("PROTOCOL_VM_EXE")
86
+ if env:
87
+ return env
88
+ base = os.path.join(_RT_DIR, "target", "release", "protocol_vm")
89
+ return base + ".exe" if os.name == "nt" else base
90
+
91
+
92
+ EXE = _exe_path()
93
+ _BUILD_CMD = "cargo build --release --manifest-path swarm/rust_runtime/Cargo.toml"
94
+
95
+
96
+ def _ensure_exe():
97
+ if os.path.isfile(EXE):
98
+ return True
99
+ if not shutil.which("cargo") or not os.path.isdir(_RT_DIR):
100
+ print(" (%s 不在场且 cargo 不可用)" % EXE)
101
+ return False
102
+ print(" 自建:%s" % _BUILD_CMD)
103
+ r = subprocess.run(["cargo", "build", "--release",
104
+ "--manifest-path", os.path.join(_RT_DIR, "Cargo.toml")],
105
+ capture_output=True, text=True, encoding="utf-8",
106
+ errors="replace", timeout=900)
107
+ if r.returncode != 0:
108
+ print(" 构建失败:%s" % (r.stderr or "")[-400:])
109
+ return False
110
+ return os.path.isfile(EXE)
111
+
112
+
113
+ # ============================ WAL 合成 ============================
114
+
115
+ def _sign(seq, typ, frm, to, rnd, ts, payload):
116
+ msg = "%d|%s|%s|%s|%d|%d|%s" % (seq, typ, frm, to, rnd, ts, payload)
117
+ return _hmac.new(SECRET.encode(), msg.encode(), hashlib.sha256).hexdigest()
118
+
119
+
120
+ def _line(seq, typ, frm, to, rnd, payload, ts=1758888800000):
121
+ return ('{"seq":%d,"ts":%d,"from":"%s","to":"%s","type":"%s","round":%d,'
122
+ '"level":0,"hmac":"%s","payload":%s}'
123
+ % (seq, ts, frm, to, typ, rnd, _sign(seq, typ, frm, to, rnd, ts, payload),
124
+ payload))
125
+
126
+
127
+ def _snap(seq, rnd, trusts='"i1":0.5'):
128
+ return _line(seq, SNAPSHOT_TYPE, "协调器", "协调器", rnd,
129
+ '{"trusts":{%s},"states":{}}' % trusts)
130
+
131
+
132
+ def _ev(seq, rnd, payload='{"x":1}'):
133
+ return _line(seq, "MSG", "i1", "i1", rnd, payload)
134
+
135
+
136
+ CONFIG = {"algo": "rust_swarm-0.1", "shared_secret": SECRET, "rounds": 2,
137
+ "topology": "",
138
+ "routes": [{"from": "i1", "event_type": "消息", "to": "i1",
139
+ "payload": {"x": 1}}],
140
+ "instances": [{"id": "i1", "role": "worker", "trust": 0.5, "symbols": {}}]}
141
+
142
+ _PBC = [None]
143
+ _PBC_DIR = tempfile.mkdtemp(prefix="i470_pbc_")
144
+
145
+
146
+ def _pbc_path():
147
+ if _PBC[0] is None:
148
+ try:
149
+ sys.path.insert(0, _REPO)
150
+ from compiler.pbc import compile_to_pbc
151
+ p = os.path.join(_PBC_DIR, "program.pbc")
152
+ ok, _r = compile_to_pbc("术曰:\n1。道 新信任路径;\n2。止。\n", p)
153
+ _PBC[0] = p if ok and os.path.exists(p) else ""
154
+ except Exception as exc: # noqa: BLE001
155
+ print(" (pbc 编译不可用,实例走自死形态:%s)" % exc)
156
+ _PBC[0] = ""
157
+ return _PBC[0]
158
+
159
+
160
+ def _mkspace(tag):
161
+ t = tempfile.mkdtemp(prefix="i470_%s_" % tag)
162
+ _SANDBOXES.append(t)
163
+ return t
164
+
165
+
166
+ def _run(t, wal, rounds=2, timeout=120):
167
+ cfg = dict(CONFIG)
168
+ cfg["rounds"] = rounds
169
+ cp = os.path.join(t, "swarm.json")
170
+ io.open(cp, "w", encoding="utf-8", newline="").write(
171
+ json.dumps(cfg, ensure_ascii=False))
172
+ cmd = [EXE, "swarm", "--config", cp, "--wal", wal]
173
+ pbc = _pbc_path()
174
+ if pbc:
175
+ cmd += ["--pbc", pbc]
176
+ p = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8",
177
+ errors="replace", timeout=timeout, cwd=t)
178
+ rep = None
179
+ if p.stdout.strip():
180
+ try:
181
+ rep = json.loads(p.stdout.strip().splitlines()[-1])
182
+ except Exception: # noqa: BLE001
183
+ rep = None
184
+ return p.returncode, rep, (p.stderr or "")
185
+
186
+
187
+ # ============================ [A] 行为面 ============================
188
+
189
+ def a1_event_syncs_per_round():
190
+ print("== A1 每轮事件行独立 durable:event_syncs == 执行轮数 ==")
191
+ t = _mkspace("r2")
192
+ wal = os.path.join(t, "events.jsonl")
193
+ rc, rep, err = _run(t, wal, rounds=2)
194
+ wd = (rep or {}).get("wal_durable")
195
+ check("A1a 报告必须带 wal_durable 读数(可观测出口)",
196
+ isinstance(wd, dict), wd)
197
+ check("A1b coordinator 正常退出", rc == 0, (rc, err[-200:]))
198
+ check("A1c event_syncs == 2(每轮一次,修前无此读数)",
199
+ bool(wd) and wd.get("event_syncs") == 2, wd)
200
+ check("A1d last_sync_round == 2", bool(wd) and wd.get("last_sync_round") == 2, wd)
201
+ # 事件行与快照行都必须在盘上(顺序判据在 B 面做源码级断言)
202
+ data = io.open(wal, "rb").read()
203
+ rows = []
204
+ for raw in data.split(b"\n"):
205
+ if raw.strip():
206
+ try:
207
+ rows.append(json.loads(raw.decode("utf-8")))
208
+ except Exception: # noqa: BLE001
209
+ rows.append({"type": "<坏行>"})
210
+ evs = [r for r in rows if r.get("type") != SNAPSHOT_TYPE]
211
+ snaps = [r for r in rows if r.get("type") == SNAPSHOT_TYPE]
212
+ check("A1e WAL 同时含事件行与快照行(有事件可判 durable 顺序)",
213
+ bool(evs) and len(snaps) == 2, {"events": len(evs), "snaps": len(snaps)})
214
+ shutil.rmtree(t, ignore_errors=True)
215
+
216
+
217
+ def a2_early_exit_no_fake_count():
218
+ print("== A2 早退路径(目标轮已全部持久化)不得伪造 durable 计数 ==")
219
+ t = _mkspace("done")
220
+ wal = os.path.join(t, "events.jsonl")
221
+ io.open(wal, "wb").write(
222
+ ("\n".join([_snap(1, 1), _ev(2, 1), _snap(3, 2)]) + "\n").encode("utf-8"))
223
+ rc, rep, err = _run(t, wal, rounds=2)
224
+ wd = (rep or {}).get("wal_durable")
225
+ check("A2a 早退 rc=0", rc == 0, (rc, err[-200:]))
226
+ check("A2b 早退 event_syncs == 0(本会话无事件行写入)",
227
+ isinstance(wd, dict) and wd.get("event_syncs") == 0, wd)
228
+ shutil.rmtree(t, ignore_errors=True)
229
+
230
+
231
+ # ============================ [B] 源断言 ============================
232
+
233
+ def _src():
234
+ return io.open(_SWARM_RS, encoding="utf-8", errors="replace").read()
235
+
236
+
237
+ def b_source_face():
238
+ print("== B 源断言(行为面不可用时仍决定 rc)==")
239
+ src = _src()
240
+ i_sync = src.find("wal.sync_data()")
241
+ i_snap = src.find("wal.write_all(snap_line.as_bytes())")
242
+ check("B1 事件行显式 sync_data 在场", i_sync >= 0)
243
+ check("B1b sync_data 在轮末快照写入之前(快照永不先于产生它的写入落盘)",
244
+ i_sync >= 0 and i_snap >= 0 and i_sync < i_snap,
245
+ (i_sync, i_snap))
246
+ # 代码位置的 wal.flush() 用法必须消失(注释里点名旧实现不算)
247
+ code_flush = [ln for ln in src.splitlines()
248
+ if ln.strip().startswith("wal.flush(")]
249
+ check("B2 事件行 durable 不再依赖 File::flush()(no-op)",
250
+ not code_flush, code_flush[:2])
251
+ check("B3 报告出口 wal_durable 在场",
252
+ '\\"wal_durable\\":{{' in src and "rep.wal_durable.event_syncs" in src)
253
+ check("B4 反陈化:修复点名 issue #470",
254
+ "#470" in src)
255
+ check("B5 轮末 sync_all 仍在(快照 durable 未回退)",
256
+ "wal.sync_all()" in src)
257
+
258
+
259
+ # ============================ 主流程 ============================
260
+
261
+ _BEHAVIOR = (a1_event_syncs_per_round, a2_early_exit_no_fake_count)
262
+
263
+
264
+ def _run_groups(behavior_ok):
265
+ global PASS, FAIL
266
+ PASS = 0
267
+ FAIL = 0
268
+ del FAILS[:]
269
+ del SKIPS[:]
270
+ for g in _BEHAVIOR:
271
+ if not behavior_ok:
272
+ skip(g.__name__, "protocol_vm 不可得(构建命令:%s)" % _BUILD_CMD)
273
+ continue
274
+ try:
275
+ g()
276
+ except Exception as exc: # noqa: BLE001
277
+ check("断言组 %s 抛异常:%s" % (g.__name__, exc), False)
278
+ import traceback
279
+ print(traceback.format_exc()[-700:])
280
+ b_source_face()
281
+ return len(FAILS)
282
+
283
+
284
+ def main():
285
+ # 锚点自检(fail-closed):修法的关键判据不在源码里 ⇒ exit 2,不给假绿。
286
+ src = _src()
287
+ missing = [a for a in ("wal.sync_data()", '\\"wal_durable\\":{{',
288
+ "rep.wal_durable.event_syncs") if a not in src]
289
+ if missing:
290
+ print("锚点自检失败:关键判据字面量缺失 %s" % missing)
291
+ return 2
292
+ behavior_ok = _ensure_exe()
293
+ if not behavior_ok:
294
+ print("!! 行为面 SKIP:%s 不在场且无法自建 —— 构建命令:%s"
295
+ % (EXE, _BUILD_CMD))
296
+ n = _run_groups(behavior_ok)
297
+ print()
298
+ print("issue470 WAL 事件行 durable 守卫:%d 通过,%d 失败,%d 跳过"
299
+ % (PASS, n, len(SKIPS)))
300
+ return 1 if n else 0
301
+
302
+
303
+ # ============================ [C] 定点变异自证 ============================
304
+
305
+ _SYNC_BLOCK = (
306
+ " wal.sync_data().map_err(|e| format!(\"WAL 事件行 sync 失败: {e}\"))?;\n"
307
+ " wal_durable.event_syncs += 1;\n"
308
+ " wal_durable.last_sync_round = round;\n")
309
+ _REPORT_BLOCK = (
310
+ " // issue #470:事件行 durable 读数(每轮事件行 sync_data 后才写快照)。\n"
311
+ " out.push_str(&format!(\n"
312
+ " \",\\\"wal_durable\\\":{{\\\"event_syncs\\\":{},\\\"last_sync_round\\\":{}}}\",\n"
313
+ " rep.wal_durable.event_syncs, rep.wal_durable.last_sync_round\n"
314
+ " ));\n")
315
+ _SNAP_SYNC_ALL = " wal.sync_all().map_err(|e| format!(\"WAL sync 失败: {e}\"))?;\n"
316
+
317
+ _MUTATIONS = (
318
+ ("V1 回退:sync_data 改回 File::flush()(no-op)",
319
+ _SYNC_BLOCK,
320
+ " wal.flush().map_err(|e| format!(\"WAL flush 失败: {e}\"))?;\n",
321
+ "A1c", "B1", "B2"),
322
+ ("V2 回退:删掉 wal_durable 报告出口",
323
+ _REPORT_BLOCK,
324
+ "",
325
+ "A1a", "A1c", "B3"),
326
+ ("V3 顺序回退:sync_data 挪到快照写入之后",
327
+ _SYNC_BLOCK,
328
+ "",
329
+ "B1b"),
330
+ )
331
+
332
+
333
+ def _rebuild():
334
+ r = subprocess.run(["cargo", "build", "--release",
335
+ "--manifest-path", os.path.join(_RT_DIR, "Cargo.toml")],
336
+ capture_output=True, text=True, encoding="utf-8",
337
+ errors="replace", timeout=900)
338
+ return r.returncode == 0, (r.stderr or "")[-400:]
339
+
340
+
341
+ def _mutate():
342
+ bad = []
343
+ if not os.path.isfile(_SWARM_RS):
344
+ print(" SOURCE-MISSING:%s" % _SWARM_RS)
345
+ return 1
346
+ if not shutil.which("cargo"):
347
+ print(" NO-CARGO:变异自证需要 cargo(改源后必须重建)")
348
+ return 1
349
+ with io.open(_SWARM_RS, encoding="utf-8", newline="") as f:
350
+ pristine = f.read()
351
+ global _SILENT
352
+ for name, anchor, repl, *want in _MUTATIONS:
353
+ with io.open(_SWARM_RS, encoding="utf-8", newline="") as f:
354
+ orig = f.read()
355
+ NL = "\r\n" if "\r\n" in orig else "\n"
356
+ a_ = anchor.replace("\n", NL)
357
+ r_ = repl.replace("\n", NL)
358
+ if a_ not in orig:
359
+ print(" ANCHOR-MISS %s" % name)
360
+ bad.append(name)
361
+ continue
362
+ mutated = orig.replace(a_, r_, 1)
363
+ if name.startswith("V3"):
364
+ # 顺序回退:删掉原位同步块,把同一调用挪到轮末 sync_all 之前
365
+ # (此时快照行已 write_all 落进 page cache ⇒ 快照可先于事件行落盘)
366
+ sw = _SNAP_SYNC_ALL.replace("\n", NL)
367
+ if sw not in mutated:
368
+ print(" ANCHOR-MISS %s(轮末 sync_all 点)" % name)
369
+ bad.append(name)
370
+ continue
371
+ mutated = mutated.replace(
372
+ sw,
373
+ " wal.sync_data().map_err(|e| format!(\"WAL 事件行 sync 失败: {e}\"))?;\n"
374
+ " wal_durable.event_syncs += 1;\n"
375
+ " wal_durable.last_sync_round = round;\n".replace("\n", NL) + sw,
376
+ 1)
377
+ try:
378
+ with io.open(_SWARM_RS, "w", encoding="utf-8", newline="") as f:
379
+ f.write(mutated)
380
+ ok_build, blog = _rebuild()
381
+ if not ok_build:
382
+ print(" BUILD-FAIL %s:%s" % (name, blog))
383
+ bad.append(name + "(构建失败)")
384
+ continue
385
+ _SILENT = True
386
+ del _VIOL[:]
387
+ _run_groups(True)
388
+ reds = list(_VIOL)
389
+ hit = [w for w in want if any(x.startswith(w) for x in reds)]
390
+ print(" %-4s %-46s 红项=%-3d 命中=%s"
391
+ % ("OK" if len(hit) == len(want) else "BAD", name, len(reds),
392
+ ",".join(hit) if hit else "无"))
393
+ if len(hit) != len(want):
394
+ print(" 期望红:%s;实红:%s" % (list(want), reds[:8]))
395
+ bad.append(name)
396
+ finally:
397
+ _SILENT = False
398
+ with io.open(_SWARM_RS, "w", encoding="utf-8", newline="") as f:
399
+ f.write(pristine)
400
+ with io.open(_SWARM_RS, encoding="utf-8", newline="") as f:
401
+ if f.read() != pristine:
402
+ print(" RESTORE-FAILED %s" % name)
403
+ bad.append(name + "(恢复失败)")
404
+ _rebuild()
405
+ if bad:
406
+ print()
407
+ print("定点变异自证:FAIL -- %s" % "、".join(bad))
408
+ return 1
409
+ print()
410
+ print("定点变异自证:PASS(每处判据都被打红)")
411
+ return 0
412
+
413
+
414
+ def _cleanup():
415
+ for t in _SANDBOXES:
416
+ shutil.rmtree(t, ignore_errors=True)
417
+ shutil.rmtree(_PBC_DIR, ignore_errors=True)
418
+
419
+
420
+ if __name__ == "__main__":
421
+ if "--mutate" in sys.argv:
422
+ rc = _mutate()
423
+ _cleanup()
424
+ sys.exit(rc)
425
+ rc = main()
426
+ _cleanup()
427
+ sys.exit(rc)