@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.
- package/data/policy.json +38 -38
- package/docs/discipline/templates/spec.txt.tmpl +1 -1
- package/dsh/cordis.yml.example +139 -139
- package/lib/hooks.d.ts +19 -0
- package/lib/hooks.js +29 -6
- package/lib/index.js +11 -2
- package/lib/lib/hook_audit.js +38 -2
- package/lib/lib/prompt_safety.js +14 -3
- package/lib/tools.d.ts +7 -0
- package/lib/tools.js +22 -1
- package/md_cg/_guard_kit.py +121 -121
- package/md_cg/anchor.py +114 -114
- package/md_cg/audit.py +15 -2
- package/md_cg/chain.py +3 -2
- package/md_cg/config_registry_guard.py +487 -487
- package/md_cg/conformance.py +747 -747
- package/md_cg/consolidate.py +1793 -1793
- package/md_cg/crypto.py +794 -794
- package/md_cg/hyperedge.py +262 -262
- package/md_cg/interface_schema.py +146 -146
- package/md_cg/lifecycle.py +426 -426
- package/md_cg/mdcos.py +12 -8
- package/md_cg/predict.py +1103 -1103
- package/md_cg/protect.py +654 -654
- package/md_cg/provenance.py +606 -606
- package/md_cg/queue_health.py +130 -130
- package/md_cg/refine.py +613 -613
- package/md_cg/retrieve.py +128 -128
- package/md_cg/revise.py +140 -140
- package/md_cg/rollback.py +657 -657
- package/md_cg/rollback_cli.py +124 -124
- package/md_cg/rotate.py +346 -346
- package/md_cg/routing.py +490 -490
- package/md_cg/self_surface.py +50 -50
- package/md_cg/semantic/canonical.py +144 -144
- package/md_cg/semantic/zh_en_atoms.py +141 -141
- package/md_cg/sources.py +1091 -1091
- package/md_cg/stg.py +3 -2
- package/md_cg/test_audit_rotate_parity.py +279 -279
- package/md_cg/test_autonomy_modes.py +2013 -2013
- package/md_cg/test_cn_recall_grams.py +206 -206
- package/md_cg/test_code_kind_forbidden.py +101 -0
- package/md_cg/test_condaccept.py +156 -156
- package/md_cg/test_condgate.py +141 -141
- package/md_cg/test_heartbeat_ledger.py +557 -557
- package/md_cg/test_i50e_readside_protection.py +658 -658
- package/md_cg/test_issue111_gate_order.py +76 -76
- package/md_cg/test_issue111_scoring_contract.py +86 -86
- package/md_cg/test_issue121_calibration_bounds.py +293 -293
- package/md_cg/test_issue145_sleep_no_silent_rollback.py +167 -167
- package/md_cg/test_issue147_pack_id_escape.py +378 -378
- package/md_cg/test_issue148_merge_downgrade_leak.py +192 -192
- package/md_cg/test_issue149_accel_default_face.py +248 -248
- package/md_cg/test_issue150_empty_query_terms.py +328 -328
- package/md_cg/test_issue151_153_157_write_cache_index.py +151 -151
- package/md_cg/test_issue152_sealed_postings.py +309 -309
- package/md_cg/test_issue153b_index_generation_invalidation.py +142 -142
- package/md_cg/test_issue157b_index_remainder.py +344 -344
- package/md_cg/test_issue189_conclusion_bidirectional.py +223 -223
- package/md_cg/test_issue191_import_trust.py +382 -382
- package/md_cg/test_issue193_write_scope.py +378 -378
- package/md_cg/test_issue212_writeface_gaps.py +349 -349
- package/md_cg/test_issue214_reject_sensitivity.py +266 -266
- package/md_cg/test_issue215_condition_source_gap.py +272 -272
- package/md_cg/test_issue216_clash_routes.py +214 -214
- package/md_cg/test_issue217_slot_routes.py +248 -248
- package/md_cg/test_issue218_postcondition_value_blind.py +222 -222
- package/md_cg/test_issue258_pack_sensitivity.py +468 -468
- package/md_cg/test_issue265_coldverify_multiproc.py +438 -438
- package/md_cg/test_issue274_read_node_id_none.py +341 -341
- package/md_cg/test_issue275_layer_param.py +250 -250
- package/md_cg/test_issue277_node_id_traversal.py +384 -384
- package/md_cg/test_issue278_neg_separated_tension.py +271 -271
- package/md_cg/test_issue278_neg_window.py +294 -294
- package/md_cg/test_issue278_negative_layering.py +83 -83
- package/md_cg/test_issue284_skip_not_pass.py +291 -291
- package/md_cg/test_issue294_flywheel_sensitivity.py +573 -573
- package/md_cg/test_issue301_baseline_corpus_gate.py +245 -245
- package/md_cg/test_issue303_design_boundaries.py +262 -262
- package/md_cg/test_issue315_merge_branch_sensitivity.py +383 -383
- package/md_cg/test_issue319_tenant_unregistered_bypass.py +256 -256
- package/md_cg/test_issue321_mcp_deep_json.py +304 -304
- package/md_cg/test_issue322_node_write_lock.py +727 -727
- package/md_cg/test_issue324_write_size_fields.py +214 -214
- package/md_cg/test_issue325_audit_poison.py +229 -229
- package/md_cg/test_issue326_ingest_auto_whitelist.py +331 -331
- package/md_cg/test_issue326_key_perms.py +355 -355
- package/md_cg/test_issue326_twin_parser_parity.py +323 -323
- package/md_cg/test_issue326_vm_call_frame.py +569 -569
- package/md_cg/test_issue345_writelimit_effective_sens.py +233 -233
- package/md_cg/test_issue346_overwrite_sensitivity_gate.py +424 -424
- package/md_cg/test_issue348_insight_verify_op.py +199 -199
- package/md_cg/test_issue348_merge_rmw_lock.py +345 -345
- package/md_cg/test_issue350_casefold_collision.py +235 -235
- package/md_cg/test_issue351_ingest_root_fallback.py +201 -201
- package/md_cg/test_issue352_imgaudit.py +418 -418
- package/md_cg/test_issue352_pathsjson.py +537 -537
- package/md_cg/test_issue352_refindex.py +412 -412
- package/md_cg/test_issue352_rollback.py +604 -604
- package/md_cg/test_issue354_p2_writelimit_toctou.py +327 -327
- package/md_cg/test_issue354_rust_index_containment.py +571 -571
- package/md_cg/test_issue354_rust_wal_containment.py +606 -606
- package/md_cg/test_issue358_truncation_observable.py +247 -247
- package/md_cg/test_issue360_tidy_private_excerpt.py +195 -195
- package/md_cg/test_issue361_blindspot_not_silent_drop.py +494 -494
- package/md_cg/test_issue362_neg_condition.py +468 -468
- package/md_cg/test_issue363_lexical_idf.py +275 -275
- package/md_cg/test_issue370_readcache_assembly.py +488 -488
- package/md_cg/test_issue376_heartbeat_failed.py +293 -293
- package/md_cg/test_issue377_layer_protect_stamp.py +324 -324
- package/md_cg/test_issue378_protected_floor.py +310 -310
- package/md_cg/test_issue383_state_event_write_gate.py +462 -462
- package/md_cg/test_issue397_sens_eff_wiring.py +435 -435
- package/md_cg/test_issue398_toolface_op_parity.py +511 -511
- package/md_cg/test_issue399_hive_json_shape.py +317 -317
- package/md_cg/test_issue401_wisdom_codegen.py +295 -295
- package/md_cg/test_issue404_heal_receipt.py +389 -389
- package/md_cg/test_issue409_compact_index_completeness.py +509 -509
- package/md_cg/test_issue410_verify_demote_write.py +366 -366
- package/md_cg/test_issue411_scrub_idempotence.py +575 -575
- package/md_cg/test_issue412_discipline_producer_parity.py +311 -311
- package/md_cg/test_issue413_preimage_failclosed.py +239 -239
- package/md_cg/test_issue413_preimage_twin.py +339 -339
- package/md_cg/test_issue415_deps_gate_probe.py +357 -357
- package/md_cg/test_issue416_entry_key_subset.py +525 -525
- package/md_cg/test_issue417_twophase_merge_landed.py +423 -423
- package/md_cg/test_issue425_ingest_library_path.py +331 -331
- package/md_cg/test_issue426c_enqueue_not_silent.py +346 -346
- package/md_cg/test_issue426e_restore_overwrite_live.py +310 -310
- package/md_cg/test_issue426f_paths_gbk_failclosed.py +301 -301
- package/md_cg/test_issue427_serve_status_shape.py +191 -191
- package/md_cg/test_issue431_432_468_protect_single_point.py +322 -322
- package/md_cg/test_issue433_outlook_threshold_single_point.py +278 -278
- package/md_cg/test_issue434_435_float_gate.py +90 -90
- package/md_cg/test_issue439_sibling_sensitivity_inherit.py +448 -448
- package/md_cg/test_issue43_default_policy.py +874 -874
- package/md_cg/test_issue444_scrub_evidence_plaintext.py +281 -281
- package/md_cg/test_issue445_metacognition_ledger_gate.py +454 -454
- package/md_cg/test_issue446_453_403_verified_evidence.py +382 -382
- package/md_cg/test_issue450_sensitivity_fallback_reason.py +51 -51
- package/md_cg/test_issue450_writegate_unknown_sens.py +203 -203
- package/md_cg/test_issue451_audit_actor.py +313 -313
- package/md_cg/test_issue452_ingest_root_sensitivity.py +299 -299
- package/md_cg/test_issue463_ghost_prune_guard.py +259 -259
- package/md_cg/test_issue466_basis_registry.py +469 -469
- package/md_cg/test_issue467_identity_sensitivity_parity.py +360 -360
- package/md_cg/test_issue469_scrub_override_passthrough.py +126 -126
- package/md_cg/test_issue470_wal_event_durable.py +427 -427
- package/md_cg/test_issue471_identity_readable.py +136 -136
- package/md_cg/test_issue472_audit_chain.py +90 -90
- package/md_cg/test_issue472_prune_ledger.py +105 -105
- package/md_cg/test_issue472_rollback_degraded.py +81 -81
- package/md_cg/test_issue491_compose_sandbox.py +460 -460
- package/md_cg/test_issue498_merge_trace_stale_index.py +280 -280
- package/md_cg/test_issue511_scoreboard_bad_files.py +256 -256
- package/md_cg/test_issue521_rotate_lock_suffix.py +110 -110
- package/md_cg/test_issue524_orphan_noise_no_demote.py +88 -88
- package/md_cg/test_issue525_read_null_semantics.py +52 -52
- package/md_cg/test_issue547_append_jsonl_whitelist.py +114 -114
- package/md_cg/test_issue547_audit_durable.py +96 -96
- package/md_cg/test_issue557_r2_write_entries_size_gate.py +83 -83
- package/md_cg/test_issue559_refine_prefix.py +69 -69
- package/md_cg/test_issue562_retired_faces.py +150 -0
- package/md_cg/test_issue71_queue_backlog.py +376 -376
- package/md_cg/test_issue72_coldverify_controlled_drain.py +420 -420
- package/md_cg/test_issue82_credential_rules.py +271 -271
- package/md_cg/test_issue91_verifier_sandbox.py +314 -314
- package/md_cg/test_issue93_remember_redaction.py +282 -282
- package/md_cg/test_logref.py +1141 -1141
- package/md_cg/test_md_access_parity.py +330 -330
- package/md_cg/test_md_cg_no_hive_dep.py +139 -139
- package/md_cg/test_mutation_residue.py +119 -119
- package/md_cg/test_mutation_rollback.py +835 -835
- package/md_cg/test_n123_derive_expiry_chain.py +210 -210
- package/md_cg/test_n209_verify_write_face_gates.py +466 -466
- package/md_cg/test_n62_tenant_bind_failclosed.py +200 -200
- package/md_cg/test_neg_tail_honesty.py +733 -733
- package/md_cg/test_p21_tokens.py +294 -294
- package/md_cg/test_p27_docindex.py +792 -792
- package/md_cg/test_p28_refcheck.py +1053 -1053
- package/md_cg/test_p2_six_elements.py +476 -476
- package/md_cg/test_p33_ccg_wiring.py +300 -300
- package/md_cg/test_p3_legacy_closure.py +438 -438
- package/md_cg/test_p40_refine_worklist.py +244 -244
- package/md_cg/test_p4_freshness.py +781 -781
- package/md_cg/test_p9c_dedup_hints.py +288 -288
- package/md_cg/test_policy_required_ccg.py +444 -444
- package/md_cg/test_retr_s5.py +312 -312
- package/md_cg/test_s1_ast_judge.py +109 -109
- package/md_cg/test_state_event_op.py +521 -521
- package/md_cg/test_stgidx_index_parity.py +1024 -1024
- package/md_cg/test_sustain_bounded.py +652 -652
- package/md_cg/test_trust_edge_reachability.py +405 -405
- package/md_cg/test_w7_redlines.py +659 -659
- package/md_cg/test_write_return_shape.py +919 -919
- package/md_cg/test_writepipe.py +228 -228
- package/md_cg/trust.py +1114 -1114
- package/md_cg/twophase.py +303 -303
- package/md_cg/whitebox.py +536 -536
- package/md_cg/whitebox_kb/wisdom/compose_engine.py +1634 -1634
- package/md_cg/whitebox_kb/wisdom/core/condition_vm.py +318 -318
- package/md_cg/whitebox_kb/wisdom/core/parser.py +1082 -1082
- package/md_cg/whitebox_kb/wisdom/md_access.py +490 -490
- package/md_cg/whitebox_kb/wisdom/migrate_wisdom.py +497 -497
- package/md_cg/whitebox_kb/wisdom/mini_python.py +995 -995
- package/md_cg/whitebox_kb/wisdom/net_units.py +2900 -2900
- package/md_cg/whitebox_kb/wisdom/semantic_translate.py +4060 -4060
- package/md_cg/writepipe.py +1337 -1337
- package/package.json +3 -2
- package/scripts/workspace_index.py +328 -0
- package/src/hooks.ts +30 -6
- package/src/index.ts +14 -5
- package/src/lib/hook_audit.ts +282 -256
- package/src/lib/mdcg_client.ts +469 -469
- package/src/lib/prompt_safety.ts +17 -3
- package/src/tools.ts +23 -1
|
@@ -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)
|