@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,606 +1,606 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
r"""#354 P0-2 / P1-6 / P1-7 守卫(Rust 蜂群 WAL 面):损坏不得当成空、
|
|
3
|
-
seq/round 连续性可察觉、并发恢复不丢行。
|
|
4
|
-
|
|
5
|
-
缺陷(本件修复,2026-10-14 · swarm/rust_runtime/src/swarm.rs)
|
|
6
|
-
----------------------------------------------------------------
|
|
7
|
-
【P0-2】replay_wal 用 read_to_string **整文件一次性解码**,并把**所有** Err
|
|
8
|
-
(含「文件存在但含非法 UTF-8」的 InvalidData)一律当「无 WAL = 首跑」:
|
|
9
|
-
had_wal_file=false 且 completed=None ⇒ open_wal_for_run 走 File::create
|
|
10
|
-
**原位清空**且不归档 —— 已提交历史被无痕销毁,故障自愈退化成数据毁灭源。
|
|
11
|
-
实测(修前,真二进制 protocol_vm swarm,两场景只差尾字节可否解码):
|
|
12
|
-
· 合法快照+事件前缀 + 尾 0xFF 0xFE ⇒ 前缀消失、无 .corrupt-* 归档;
|
|
13
|
-
· 同一前缀 + 尾为**合法 UTF-8** 的半行 ⇒ 前缀完好(正常 B1 截断)。
|
|
14
|
-
【P1-6】WAL 无 seq/round 单调连续性校验 ⇒ 整行删除/乱序对逐行验签不可检,
|
|
15
|
-
丢行「不可察觉」(报告里也不存在任何可回读面)。
|
|
16
|
-
【P1-7】rewrite_wal_atomic 无进程排他锁且临时文件名**固定**({wal}.tmp)⇒
|
|
17
|
-
两个 coordinator 对同一 WAL 并发恢复时操作同一个 inode(O_TRUNC 互相截断 +
|
|
18
|
-
交替写入),rename 换入的 WAL 是两份内容的交错混合(半行/丢行)且失败方静默。
|
|
19
|
-
|
|
20
|
-
本守卫三层
|
|
21
|
-
----------
|
|
22
|
-
[A] 行为面(真二进制 protocol_vm swarm --config --wal,端到端):
|
|
23
|
-
W1 非法 UTF-8 尾 ⇒ 已提交前缀逐字节幸存(修前:整文件被清空);
|
|
24
|
-
W2 整段不可解码 ⇒ 必须归档留痕(.corrupt-* 逐字节),不是静默清空;
|
|
25
|
-
W3 阴性对照:合法 UTF-8 坏尾 ⇒ 前缀照常幸存(防过度修复);
|
|
26
|
-
W4 seq 跳号 ⇒ 报告 wal_continuity.ok=false + 明细 + stderr 透出(修前静默);
|
|
27
|
-
W5 锁被他人持有 ⇒ fail-closed(WAL 逐字节不动);释放后可恢复;
|
|
28
|
-
W6 两个 coordinator 并发恢复 ⇒ 双方 rc=0、前缀幸存、无撕裂行。
|
|
29
|
-
[B] 源断言(结构性,行为面不可用时仍决定 rc)。
|
|
30
|
-
[C] 定点变异:P0-2 回退 / P1-6 回退 / P1-7 回退 → 判据必须转红
|
|
31
|
-
(改源 → cargo build → 跑守卫转红 → 还原 → 重建)。
|
|
32
|
-
|
|
33
|
-
运行:
|
|
34
|
-
python -X utf8 -m md_cg.test_issue354_rust_wal_containment
|
|
35
|
-
python -X utf8 -m md_cg.test_issue354_rust_wal_containment --mutate
|
|
36
|
-
退出码:0 全绿 / 1 有失败。
|
|
37
|
-
环境:PROTOCOL_VM_EXE 可指向别处的 protocol_vm 可执行文件。
|
|
38
|
-
诚实边界:行为面需要 swarm/rust_runtime/target/release 下的可执行件(gitignored),
|
|
39
|
-
本守卫按仓内既有口径自建;cargo 与产物俱不可得时行为面显式 SKIP 并打印构建命令
|
|
40
|
-
(源断言面照判、照决定 rc)。W6 是**并发烟测**(时序相关):它的判别力弱于 W5 的
|
|
41
|
-
确定性锁判据,两者并置——W5 决定「锁在不在」,W6 证「并发下不撕裂」。
|
|
42
|
-
"""
|
|
43
|
-
from __future__ import annotations
|
|
44
|
-
|
|
45
|
-
import hashlib
|
|
46
|
-
import hmac as _hmac
|
|
47
|
-
import io
|
|
48
|
-
import json
|
|
49
|
-
import os
|
|
50
|
-
import shutil
|
|
51
|
-
import subprocess
|
|
52
|
-
import sys
|
|
53
|
-
import tempfile
|
|
54
|
-
import time
|
|
55
|
-
|
|
56
|
-
try:
|
|
57
|
-
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
58
|
-
except AttributeError:
|
|
59
|
-
pass
|
|
60
|
-
|
|
61
|
-
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
62
|
-
_RT_DIR = os.path.join(_REPO, "swarm", "rust_runtime")
|
|
63
|
-
_SWARM_RS = os.path.join(_RT_DIR, "src", "swarm.rs")
|
|
64
|
-
|
|
65
|
-
PASS = 0
|
|
66
|
-
FAIL = 0
|
|
67
|
-
FAILS = []
|
|
68
|
-
SKIPS = []
|
|
69
|
-
_SANDBOXES = []
|
|
70
|
-
_SILENT = False
|
|
71
|
-
_VIOL = []
|
|
72
|
-
|
|
73
|
-
#: 哑密钥(每测试唯一合成;绝不硬编码真实密钥)
|
|
74
|
-
SECRET = "i354-哑密钥-" + os.urandom(6).hex()
|
|
75
|
-
SNAPSHOT_TYPE = "__snapshot__"
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
def check(name, cond, detail=""):
|
|
79
|
-
global PASS, FAIL
|
|
80
|
-
if _SILENT:
|
|
81
|
-
if not cond:
|
|
82
|
-
_VIOL.append(name)
|
|
83
|
-
return bool(cond)
|
|
84
|
-
if cond:
|
|
85
|
-
PASS += 1
|
|
86
|
-
print(" [PASS] %s%s" % (name, (" · " + str(detail)) if detail else ""))
|
|
87
|
-
else:
|
|
88
|
-
FAIL += 1
|
|
89
|
-
FAILS.append(name)
|
|
90
|
-
print(" [FAIL] %s · %s" % (name, detail))
|
|
91
|
-
return bool(cond)
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
def skip(name, why):
|
|
95
|
-
SKIPS.append(name)
|
|
96
|
-
print(" [SKIP] %s · %s" % (name, why))
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
def _exe_path():
|
|
100
|
-
env = os.environ.get("PROTOCOL_VM_EXE")
|
|
101
|
-
if env:
|
|
102
|
-
return env
|
|
103
|
-
base = os.path.join(_RT_DIR, "target", "release", "protocol_vm")
|
|
104
|
-
return base + ".exe" if os.name == "nt" else base
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
EXE = _exe_path()
|
|
108
|
-
_BUILD_CMD = "cargo build --release (在 %s 下执行)" % _RT_DIR
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
def _ensure_exe():
|
|
112
|
-
if os.path.isfile(EXE):
|
|
113
|
-
return True
|
|
114
|
-
if not shutil.which("cargo") or not os.path.isdir(_RT_DIR):
|
|
115
|
-
print(" (%s 不在场且 cargo 不可用)" % EXE)
|
|
116
|
-
return False
|
|
117
|
-
print(" 自建:%s" % _BUILD_CMD)
|
|
118
|
-
r = subprocess.run(["cargo", "build", "--release"], cwd=_RT_DIR,
|
|
119
|
-
capture_output=True, text=True, encoding="utf-8",
|
|
120
|
-
errors="replace", timeout=900)
|
|
121
|
-
if r.returncode != 0:
|
|
122
|
-
print(" 构建失败:%s" % (r.stderr or "")[-400:])
|
|
123
|
-
return False
|
|
124
|
-
return os.path.isfile(EXE)
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
# ============================ WAL 合成(与在线写入同格式) ============================
|
|
128
|
-
|
|
129
|
-
def _sign(seq, typ, frm, to, rnd, ts, payload):
|
|
130
|
-
"""签名串 v0.7.1:seq|type|from|to|round|ts|payload(与 Rust sign_event 同式)。"""
|
|
131
|
-
msg = "%d|%s|%s|%s|%d|%d|%s" % (seq, typ, frm, to, rnd, ts, payload)
|
|
132
|
-
return _hmac.new(SECRET.encode(), msg.encode(), hashlib.sha256).hexdigest()
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
def _line(seq, typ, frm, to, rnd, payload, ts=1758888800000):
|
|
136
|
-
return ('{"seq":%d,"ts":%d,"from":"%s","to":"%s","type":"%s","round":%d,'
|
|
137
|
-
'"level":0,"hmac":"%s","payload":%s}'
|
|
138
|
-
% (seq, ts, frm, to, typ, rnd, _sign(seq, typ, frm, to, rnd, ts, payload),
|
|
139
|
-
payload))
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
def _snap(seq, rnd, trusts='"i1":0.5'):
|
|
143
|
-
return _line(seq, SNAPSHOT_TYPE, "协调器", "协调器", rnd,
|
|
144
|
-
'{"trusts":{%s},"states":{}}' % trusts)
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
def _ev(seq, rnd, payload='{"x":1}'):
|
|
148
|
-
return _line(seq, "MSG", "i1", "i1", rnd, payload)
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
CONFIG = {"algo": "rust_swarm-0.1", "shared_secret": SECRET, "rounds": 2,
|
|
152
|
-
"topology": "", "routes": [],
|
|
153
|
-
"instances": [{"id": "i1", "role": "worker", "trust": 0.5, "symbols": {}}]}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
def _mkspace(tag):
|
|
157
|
-
t = tempfile.mkdtemp(prefix="i354wal_%s_" % tag)
|
|
158
|
-
_SANDBOXES.append(t)
|
|
159
|
-
return t
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
def _write_cfg(t, rounds=2):
|
|
163
|
-
cfg = dict(CONFIG)
|
|
164
|
-
cfg["rounds"] = rounds
|
|
165
|
-
p = os.path.join(t, "swarm.json")
|
|
166
|
-
io.open(p, "w", encoding="utf-8", newline="").write(json.dumps(cfg, ensure_ascii=False))
|
|
167
|
-
return p
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
_PBC = [None] # 惰性编译(None=未尝试;""=不可用)
|
|
171
|
-
#: pbc 必须落在**跨用例存活**的位置:若编进某个用例的沙箱,该用例收尾 rmtree 后
|
|
172
|
-
#: 缓存路径即成死路径 ⇒ 后续用例的实例子进程载不到字节码而自死(本守卫首版实测:
|
|
173
|
-
#: W1 正常、W2/W5/W6 全报「实例终态非法」,噪声掩盖真判据)。
|
|
174
|
-
_PBC_DIR = tempfile.mkdtemp(prefix="i354wal_pbc_")
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
def _pbc_path(t=None):
|
|
178
|
-
if _PBC[0] is None:
|
|
179
|
-
try:
|
|
180
|
-
sys.path.insert(0, _REPO)
|
|
181
|
-
from compiler.pbc import compile_to_pbc
|
|
182
|
-
p = os.path.join(_PBC_DIR, "program.pbc")
|
|
183
|
-
ok, _r = compile_to_pbc("术曰:\n1。道 新信任路径;\n2。止。\n", p)
|
|
184
|
-
_PBC[0] = p if ok and os.path.exists(p) else ""
|
|
185
|
-
except Exception as exc: # noqa: BLE001
|
|
186
|
-
print(" (pbc 编译不可用,退化为「实例自死」形态:%s)" % exc)
|
|
187
|
-
_PBC[0] = ""
|
|
188
|
-
return _PBC[0]
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
def _argv(t, wal, rounds=2):
|
|
192
|
-
cmd = [EXE, "swarm", "--config", _write_cfg(t, rounds), "--wal", wal]
|
|
193
|
-
pbc = _pbc_path(t)
|
|
194
|
-
if pbc:
|
|
195
|
-
cmd += ["--pbc", pbc]
|
|
196
|
-
return cmd
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
def _run(t, wal, rounds=2, timeout=120):
|
|
200
|
-
"""跑一次 coordinator;返回 (rc, report|None, stderr)。"""
|
|
201
|
-
p = subprocess.run(_argv(t, wal, rounds), capture_output=True, text=True,
|
|
202
|
-
encoding="utf-8", errors="replace", timeout=timeout, cwd=t)
|
|
203
|
-
rep = None
|
|
204
|
-
if p.stdout.strip():
|
|
205
|
-
try:
|
|
206
|
-
rep = json.loads(p.stdout.strip().splitlines()[-1])
|
|
207
|
-
except Exception: # noqa: BLE001
|
|
208
|
-
rep = None
|
|
209
|
-
return p.returncode, rep, (p.stderr or "")
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
def _run_parallel(t, wal, rounds=2, n=2, timeout=180):
|
|
213
|
-
"""并发拉起 n 个 coordinator(同一 WAL);返回 [(rc, stderr)...]。"""
|
|
214
|
-
procs = [subprocess.Popen(_argv(t, wal, rounds), stdin=subprocess.DEVNULL,
|
|
215
|
-
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
216
|
-
text=True, encoding="utf-8", errors="replace", cwd=t)
|
|
217
|
-
for _ in range(n)]
|
|
218
|
-
out = []
|
|
219
|
-
for p in procs:
|
|
220
|
-
try:
|
|
221
|
-
so, se = p.communicate(timeout=timeout)
|
|
222
|
-
except subprocess.TimeoutExpired:
|
|
223
|
-
p.kill()
|
|
224
|
-
so, se = p.communicate()
|
|
225
|
-
out.append((p.returncode, so or "", se or ""))
|
|
226
|
-
return out
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
def _read(wal):
|
|
230
|
-
return io.open(wal, "rb").read()
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
def _archives(d, prefix="events.jsonl.corrupt-"):
|
|
234
|
-
return sorted(n for n in os.listdir(d) if n.startswith(prefix))
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
def _verify_lines(data):
|
|
238
|
-
"""逐行校验:能被本密钥验签的行数 / 撕裂(不可解析或缺 payload 标记)行数。"""
|
|
239
|
-
good = torn = 0
|
|
240
|
-
for raw in data.split(b"\n"):
|
|
241
|
-
if not raw.strip():
|
|
242
|
-
continue
|
|
243
|
-
try:
|
|
244
|
-
rec = json.loads(raw.decode("utf-8"))
|
|
245
|
-
except Exception: # noqa: BLE001
|
|
246
|
-
torn += 1
|
|
247
|
-
continue
|
|
248
|
-
try:
|
|
249
|
-
rawp = raw.decode("utf-8")
|
|
250
|
-
seg = rawp[rawp.index('"payload":') + len('"payload":'):]
|
|
251
|
-
payload = seg[:-1] if seg.endswith("}") else seg
|
|
252
|
-
msg = "%s|%s|%s|%s|%s|%s|%s" % (rec["seq"], rec["type"], rec["from"],
|
|
253
|
-
rec["to"], rec["round"], rec["ts"], payload)
|
|
254
|
-
exp = _hmac.new(SECRET.encode(), msg.encode(), hashlib.sha256).hexdigest()
|
|
255
|
-
except Exception: # noqa: BLE001
|
|
256
|
-
torn += 1
|
|
257
|
-
continue
|
|
258
|
-
if _hmac.compare_digest(exp, rec.get("hmac", "")):
|
|
259
|
-
good += 1
|
|
260
|
-
else:
|
|
261
|
-
torn += 1
|
|
262
|
-
return good, torn
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
# ============================ [A] 行为面 ============================
|
|
266
|
-
|
|
267
|
-
def w1_invalid_utf8_tail():
|
|
268
|
-
print("== W1 P0-2:非法 UTF-8 尾不得清空已提交历史 ==")
|
|
269
|
-
t = _mkspace("p02")
|
|
270
|
-
wal = os.path.join(t, "events.jsonl")
|
|
271
|
-
snap, ev = _snap(1, 1), _ev(2, 1)
|
|
272
|
-
orig = (snap + "\n" + ev + "\n").encode("utf-8") + b"\xff\xfe"
|
|
273
|
-
io.open(wal, "wb").write(orig)
|
|
274
|
-
rc, rep, err = _run(t, wal, rounds=2)
|
|
275
|
-
after = _read(wal)
|
|
276
|
-
check("W1 coordinator 正常退出(rc=0)", rc == 0, (rc, err[-200:]))
|
|
277
|
-
check("W1 已提交快照行逐字节幸存(修前:整文件被 File::create 清空)",
|
|
278
|
-
snap.encode("utf-8") in after, _head(after))
|
|
279
|
-
check("W1 未提交的非法尾被物理截断(B1 语义不变)",
|
|
280
|
-
b"\xff\xfe" not in after)
|
|
281
|
-
check("W1 有提交点时不得归档(另有 W2 专测零提交归档)",
|
|
282
|
-
not _archives(t), _archives(t))
|
|
283
|
-
check("W1 残留件核查:目录内无 .lock / .tmp(锁与临时件都该清干净)",
|
|
284
|
-
not [n for n in os.listdir(t) if n.endswith(".lock") or ".tmp" in n],
|
|
285
|
-
os.listdir(t))
|
|
286
|
-
shutil.rmtree(t, ignore_errors=True)
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
def w2_undecodable_archived():
|
|
290
|
-
print("== W2 P0-2:整段不可解码必须归档留痕(不得静默清空)==")
|
|
291
|
-
t = _mkspace("p02b")
|
|
292
|
-
wal = os.path.join(t, "events.jsonl")
|
|
293
|
-
orig = b"\xff\xfe\xfd\n\x80\x81\n"
|
|
294
|
-
io.open(wal, "wb").write(orig)
|
|
295
|
-
rc, rep, err = _run(t, wal, rounds=1)
|
|
296
|
-
arch = _archives(t)
|
|
297
|
-
check("W2 coordinator 正常退出(零提交 → 从轮 1 重跑)", rc == 0, (rc, err[-200:]))
|
|
298
|
-
check("W2 恰好一份 .corrupt-* 归档", len(arch) == 1, arch)
|
|
299
|
-
if arch:
|
|
300
|
-
check("W2 归档逐字节保留原 WAL(审计链可离线取证)",
|
|
301
|
-
_read(os.path.join(t, arch[0])) == orig)
|
|
302
|
-
check("W2 新 WAL 不含原坏字节(已另行归档,不是原位残留)",
|
|
303
|
-
b"\xff\xfe\xfd" not in _read(wal))
|
|
304
|
-
shutil.rmtree(t, ignore_errors=True)
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
def w3_valid_bad_tail_control():
|
|
308
|
-
print("== W3 阴性对照:合法 UTF-8 坏尾照常截断(防过度修复)==")
|
|
309
|
-
t = _mkspace("ctl")
|
|
310
|
-
wal = os.path.join(t, "events.jsonl")
|
|
311
|
-
snap, ev = _snap(1, 1), _ev(2, 1)
|
|
312
|
-
io.open(wal, "wb").write(
|
|
313
|
-
(snap + "\n" + ev + "\n" + '{"seq":3,"ts":1,"from":"BAD half').encode("utf-8"))
|
|
314
|
-
rc, rep, err = _run(t, wal, rounds=2)
|
|
315
|
-
after = _read(wal)
|
|
316
|
-
check("W3 rc=0 且前缀幸存", rc == 0 and snap.encode("utf-8") in after,
|
|
317
|
-
(rc, _head(after)))
|
|
318
|
-
check("W3 合法坏尾被物理截断", b"BAD half" not in after)
|
|
319
|
-
check("W3 不触发归档(有提交点)", not _archives(t), _archives(t))
|
|
320
|
-
shutil.rmtree(t, ignore_errors=True)
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
def w4_seq_gap_detectable():
|
|
324
|
-
print("== W4 P1-6:seq 跳号必须可察觉(报告 + stderr)==")
|
|
325
|
-
t = _mkspace("p16")
|
|
326
|
-
wal = os.path.join(t, "events.jsonl")
|
|
327
|
-
# 1(快照r1),2,[缺 3/4],5,6(快照r2):一处跳号;completed=2 → 目标 2 轮已完成,
|
|
328
|
-
# 走早退聚合路径(不 spawn 实例、不改 WAL),只读连续性读数。
|
|
329
|
-
io.open(wal, "wb").write(
|
|
330
|
-
("\n".join([_snap(1, 1), _ev(2, 1), _ev(5, 1), _snap(6, 2)]) + "\n").encode("utf-8"))
|
|
331
|
-
rc, rep, err = _run(t, wal, rounds=2)
|
|
332
|
-
wc = (rep or {}).get("wal_continuity")
|
|
333
|
-
check("W4 报告必须带 wal_continuity 字段(可回读面)", isinstance(wc, dict),
|
|
334
|
-
wc)
|
|
335
|
-
check("W4 跳号被报出(ok=false 且 breaks>=1)",
|
|
336
|
-
bool(wc) and wc.get("ok") is False and (wc.get("breaks") or 0) >= 1 and
|
|
337
|
-
len(wc.get("detail") or []) >= 1, wc)
|
|
338
|
-
if wc and wc.get("detail"):
|
|
339
|
-
d = wc["detail"][0]
|
|
340
|
-
check("W4 明细含 line/expected/actual/kind(可定位、可复核)",
|
|
341
|
-
d.get("line") == 3 and d.get("expected") == 3 and d.get("actual") == 5
|
|
342
|
-
and d.get("kind") == "跳号缺失", d)
|
|
343
|
-
check("W4 stderr 亦透出(旧实现完全静默 = 不可察觉)",
|
|
344
|
-
"连续性中断" in err and "P1-6" in err, err.strip()[-200:])
|
|
345
|
-
shutil.rmtree(t, ignore_errors=True)
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
def w4b_continuous_wal_silent():
|
|
349
|
-
print("== W4b 阴性对照:连续 WAL 不得误报、不得刷告警 ==")
|
|
350
|
-
t = _mkspace("p16ok")
|
|
351
|
-
wal = os.path.join(t, "events.jsonl")
|
|
352
|
-
io.open(wal, "wb").write(
|
|
353
|
-
("\n".join([_snap(1, 1), _ev(2, 1), _snap(3, 2)]) + "\n").encode("utf-8"))
|
|
354
|
-
rc, rep, err = _run(t, wal, rounds=2)
|
|
355
|
-
wc = (rep or {}).get("wal_continuity")
|
|
356
|
-
check("W4b ok=true 且 breaks=0", bool(wc) and wc.get("ok") is True
|
|
357
|
-
and (wc.get("breaks") or 0) == 0, wc)
|
|
358
|
-
check("W4b 不得刷连续性告警(零噪声)", "连续性中断" not in err, err.strip()[-160:])
|
|
359
|
-
shutil.rmtree(t, ignore_errors=True)
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
def w5_held_lock_fails_closed():
|
|
363
|
-
print("== W5 P1-7:锁被他人持有 ⇒ fail-closed(WAL 逐字节不动)==")
|
|
364
|
-
t = _mkspace("p17")
|
|
365
|
-
wal = os.path.join(t, "events.jsonl")
|
|
366
|
-
orig = (_snap(1, 1) + "\n" + '{"seq":2,"ts":1,"from":"BAD half').encode("utf-8")
|
|
367
|
-
io.open(wal, "wb").write(orig)
|
|
368
|
-
lock = wal + ".lock"
|
|
369
|
-
io.open(lock, "w", encoding="utf-8", newline="").write("99999 0\n")
|
|
370
|
-
rc, rep, err = _run(t, wal, rounds=2)
|
|
371
|
-
check("W5 持锁时不得改写:WAL 逐字节不动(修前:无锁照写)",
|
|
372
|
-
_read(wal) == orig, _head(_read(wal)))
|
|
373
|
-
check("W5 持锁时非零退出且原因点名排他锁(可诊断)",
|
|
374
|
-
rc != 0 and "排他锁" in err, (rc, err.strip()[-200:]))
|
|
375
|
-
os.remove(lock)
|
|
376
|
-
rc2, rep2, err2 = _run(t, wal, rounds=2)
|
|
377
|
-
after2 = _read(wal)
|
|
378
|
-
check("W5 释放后可恢复(锁不是一次性)",
|
|
379
|
-
rc2 == 0 and _snap(1, 1).encode("utf-8") in after2, (rc2, err2[-160:]))
|
|
380
|
-
check("W5 正常路径不留锁件",
|
|
381
|
-
not os.path.exists(lock), os.listdir(t))
|
|
382
|
-
shutil.rmtree(t, ignore_errors=True)
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
def w6_concurrent_recovery():
|
|
386
|
-
print("== W6 P1-7:两个 coordinator 并发恢复同一 WAL(烟测)==")
|
|
387
|
-
t = _mkspace("p17c")
|
|
388
|
-
wal = os.path.join(t, "events.jsonl")
|
|
389
|
-
snap = _snap(1, 1)
|
|
390
|
-
io.open(wal, "wb").write(
|
|
391
|
-
(snap + "\n" + '{"seq":2,"ts":1,"from":"BAD half').encode("utf-8"))
|
|
392
|
-
res = _run_parallel(t, wal, rounds=2, n=2)
|
|
393
|
-
rcs = [r[0] for r in res]
|
|
394
|
-
after = _read(wal)
|
|
395
|
-
good, torn = _verify_lines(after)
|
|
396
|
-
check("W6 双方 rc=0(旧实现固定 tmp 名 → rename 竞争,失败方 Err 退出)",
|
|
397
|
-
all(x == 0 for x in rcs), (rcs, [r[2][-120:] for r in res]))
|
|
398
|
-
check("W6 已提交前缀幸存", snap.encode("utf-8") in after, _head(after))
|
|
399
|
-
check("W6 终态 WAL 无撕裂行(每行均可解析且验签)", torn == 0,
|
|
400
|
-
{"good": good, "torn": torn, "tail": after[-160:]})
|
|
401
|
-
shutil.rmtree(t, ignore_errors=True)
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
def _head(b):
|
|
405
|
-
return bytes(b[:160])
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
# ============================ [B] 源断言 ============================
|
|
409
|
-
|
|
410
|
-
def _src():
|
|
411
|
-
return io.open(_SWARM_RS, encoding="utf-8", errors="replace").read()
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
def b_source_face():
|
|
415
|
-
print("== B 源断言(行为面不可用时仍决定 rc)==")
|
|
416
|
-
src = _src()
|
|
417
|
-
check("B1 P0-2:WAL 不再整文件解码(read_to_string(wal_path) 已消失)",
|
|
418
|
-
"read_to_string(wal_path)" not in src)
|
|
419
|
-
check("B1b P0-2:「文件不存在」与「文件损坏」分道",
|
|
420
|
-
"ErrorKind::NotFound => return Ok(rp)" in src
|
|
421
|
-
and "WAL 存在但不可读" in src)
|
|
422
|
-
check("B1c P0-2:行级容错解码(坏字节只作废其所在行)",
|
|
423
|
-
"std::str::from_utf8(chunk).ok()" in src)
|
|
424
|
-
# 按**代码用法**断言(带尾逗号/表达式形态),不按裸词匹配:三种 kind 的字面量
|
|
425
|
-
# 也出现在 SeqBreak 的文档注释里,裸词匹配会被注释喂饱(V2 首版即由此漏判)。
|
|
426
|
-
check("B2 P1-6:seq/round 连续性判据在场(三种 kind 的代码用法)",
|
|
427
|
-
'if ev.seq > exp { "跳号缺失" }' in src
|
|
428
|
-
and '"round 倒退",' in src
|
|
429
|
-
and '"快照轮号重复或倒退",' in src)
|
|
430
|
-
# 注意:源码里该键名是 Rust 字面量内的转义形态(\"wal_continuity\"),故按
|
|
431
|
-
# 「访问器 + 报告发射点」两个无歧义标记断言,不按裸引号形态匹配。
|
|
432
|
-
check("B2b P1-6:读数进报告(wal_continuity 出口)",
|
|
433
|
-
"pub fn ok(&self) -> bool" in src and "rep.wal_continuity.ok()" in src)
|
|
434
|
-
check("B3 P1-7:排他锁在场且被恢复路径取用",
|
|
435
|
-
"struct WalLock" in src and "WalLock::acquire(wal_path)?" in src
|
|
436
|
-
and "LOCK_STALE_MS" in src)
|
|
437
|
-
check("B3b P1-7:临时文件名含进程号(不再是固定 {wal}.tmp)",
|
|
438
|
-
'format!("{wal_path}.tmp.{}", std::process::id())' in src
|
|
439
|
-
and 'format!("{wal_path}.tmp")' not in src)
|
|
440
|
-
check("B4 反陈化:三处缺陷都点名 issue #354 与条号",
|
|
441
|
-
"#354 P0-2" in src and "#354 P1-6" in src and "#354 P1-7" in src)
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
# ============================ 主流程 ============================
|
|
445
|
-
|
|
446
|
-
_BEHAVIOR = (w1_invalid_utf8_tail, w2_undecodable_archived, w3_valid_bad_tail_control,
|
|
447
|
-
w4_seq_gap_detectable, w4b_continuous_wal_silent,
|
|
448
|
-
w5_held_lock_fails_closed, w6_concurrent_recovery)
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
def _run_groups(behavior_ok):
|
|
452
|
-
global PASS, FAIL
|
|
453
|
-
PASS = 0
|
|
454
|
-
FAIL = 0
|
|
455
|
-
del FAILS[:]
|
|
456
|
-
del SKIPS[:]
|
|
457
|
-
for g in _BEHAVIOR:
|
|
458
|
-
if not behavior_ok:
|
|
459
|
-
skip(g.__name__, "protocol_vm 可执行件不可得(构建命令:%s)" % _BUILD_CMD)
|
|
460
|
-
continue
|
|
461
|
-
try:
|
|
462
|
-
g()
|
|
463
|
-
except Exception as exc: # noqa: BLE001
|
|
464
|
-
check("断言组 %s 抛异常:%s" % (g.__name__, exc), False)
|
|
465
|
-
import traceback
|
|
466
|
-
print(traceback.format_exc()[-700:])
|
|
467
|
-
b_source_face()
|
|
468
|
-
return len(FAILS)
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
def main():
|
|
472
|
-
behavior_ok = _ensure_exe()
|
|
473
|
-
if not behavior_ok:
|
|
474
|
-
print("!! 行为面 SKIP:%s 不在场且无法自建 —— 构建命令:%s"
|
|
475
|
-
% (EXE, _BUILD_CMD))
|
|
476
|
-
n = _run_groups(behavior_ok)
|
|
477
|
-
print()
|
|
478
|
-
print("issue354 Rust WAL 守卫(P0-2 损坏不得当空 / P1-6 连续性可察觉 / P1-7 并发恢复):"
|
|
479
|
-
"%d 通过,%d 失败,%d 跳过" % (PASS, n, len(SKIPS)))
|
|
480
|
-
return 1 if n else 0
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
# ============================ [C] 定点变异自证 ============================
|
|
484
|
-
|
|
485
|
-
_MUTATIONS = (
|
|
486
|
-
("V1 P0-2 回退:整文件解码 + 所有 Err 当首跑",
|
|
487
|
-
" let bytes = match std::fs::read(wal_path) {\n"
|
|
488
|
-
" Ok(b) => b,\n"
|
|
489
|
-
" // 文件不存在 = 首跑(唯一可当「无 WAL」的分支)\n"
|
|
490
|
-
" Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(rp),\n"
|
|
491
|
-
" // 存在但不可读(权限 / IO / 路径是目录):损坏不得当成空 ⇒ 交调用方,\n"
|
|
492
|
-
" // 由它按「不销毁现场」的路径处置\n"
|
|
493
|
-
" Err(e) => {\n"
|
|
494
|
-
" return Err(format!(\n"
|
|
495
|
-
" \"WAL 存在但不可读(issue #354 P0-2:损坏不得当成空,拒绝继续): {wal_path}: {e}\"\n"
|
|
496
|
-
" ))\n"
|
|
497
|
-
" }\n"
|
|
498
|
-
" };\n"
|
|
499
|
-
" rp.had_wal_file = true;\n",
|
|
500
|
-
" let bytes = match std::fs::read_to_string(wal_path) {\n"
|
|
501
|
-
" Ok(t) => t.into_bytes(),\n"
|
|
502
|
-
" Err(_) => return Ok(rp),\n"
|
|
503
|
-
" };\n"
|
|
504
|
-
" rp.had_wal_file = true;\n",
|
|
505
|
-
"W1", "W2", "B1", "B1b"),
|
|
506
|
-
("V2 P1-6 回退:删掉 seq 连续性检测",
|
|
507
|
-
" if let Some(exp) = expected_seq {\n"
|
|
508
|
-
" if ev.seq != exp {\n"
|
|
509
|
-
" note_break(\n"
|
|
510
|
-
" &mut rp.continuity,\n"
|
|
511
|
-
" idx + 1,\n"
|
|
512
|
-
" exp,\n"
|
|
513
|
-
" ev.seq,\n"
|
|
514
|
-
" if ev.seq > exp { \"跳号缺失\" } else { \"重复或乱序\" },\n"
|
|
515
|
-
" &ev.event_type,\n"
|
|
516
|
-
" );\n"
|
|
517
|
-
" }\n"
|
|
518
|
-
" }\n",
|
|
519
|
-
" expected_seq = Some(ev.seq + 1);\n",
|
|
520
|
-
"W4", "B2"),
|
|
521
|
-
("V3 P1-7 回退:删掉排他锁取用",
|
|
522
|
-
" let _lock = WalLock::acquire(wal_path)?;\n",
|
|
523
|
-
"",
|
|
524
|
-
"W5", "B3"),
|
|
525
|
-
)
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
def _rebuild():
|
|
529
|
-
r = subprocess.run(["cargo", "build", "--release"], cwd=_RT_DIR,
|
|
530
|
-
capture_output=True, text=True, encoding="utf-8",
|
|
531
|
-
errors="replace", timeout=900)
|
|
532
|
-
return r.returncode == 0, (r.stderr or "")[-400:]
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
def _mutate():
|
|
536
|
-
bad = []
|
|
537
|
-
if not os.path.isfile(_SWARM_RS):
|
|
538
|
-
print(" SOURCE-MISSING:%s" % _SWARM_RS)
|
|
539
|
-
return 1
|
|
540
|
-
if not shutil.which("cargo"):
|
|
541
|
-
print(" NO-CARGO:变异自证需要 cargo(改源后必须重建,否则跑的还是旧二进制)")
|
|
542
|
-
return 1
|
|
543
|
-
with io.open(_SWARM_RS, encoding="utf-8", newline="") as f:
|
|
544
|
-
pristine = f.read()
|
|
545
|
-
global _SILENT
|
|
546
|
-
for name, anchor, repl, *want in _MUTATIONS:
|
|
547
|
-
with io.open(_SWARM_RS, encoding="utf-8", newline="") as f:
|
|
548
|
-
orig = f.read()
|
|
549
|
-
NL = "\r\n" if "\r\n" in orig else "\n"
|
|
550
|
-
a_ = anchor.replace("\n", NL)
|
|
551
|
-
r_ = repl.replace("\n", NL)
|
|
552
|
-
if a_ not in orig:
|
|
553
|
-
print(" ANCHOR-MISS %s" % name)
|
|
554
|
-
bad.append(name)
|
|
555
|
-
continue
|
|
556
|
-
try:
|
|
557
|
-
with io.open(_SWARM_RS, "w", encoding="utf-8", newline="") as f:
|
|
558
|
-
f.write(orig.replace(a_, r_, 1))
|
|
559
|
-
ok_build, blog = _rebuild()
|
|
560
|
-
if not ok_build:
|
|
561
|
-
print(" BUILD-FAIL %s:%s" % (name, blog))
|
|
562
|
-
bad.append(name + "(构建失败)")
|
|
563
|
-
continue
|
|
564
|
-
_SILENT = True
|
|
565
|
-
del _VIOL[:]
|
|
566
|
-
_run_groups(True)
|
|
567
|
-
reds = list(_VIOL)
|
|
568
|
-
hit = [w for w in want if any(x.startswith(w) for x in reds)]
|
|
569
|
-
print(" %-4s %-44s 红项=%-3d 命中=%s"
|
|
570
|
-
% ("OK" if len(hit) == len(want) else "BAD", name, len(reds),
|
|
571
|
-
",".join(hit) if hit else "无"))
|
|
572
|
-
if len(hit) != len(want):
|
|
573
|
-
print(" 期望红:%s;实红:%s" % (list(want), reds[:8]))
|
|
574
|
-
bad.append(name)
|
|
575
|
-
finally:
|
|
576
|
-
_SILENT = False
|
|
577
|
-
with io.open(_SWARM_RS, "w", encoding="utf-8", newline="") as f:
|
|
578
|
-
f.write(pristine)
|
|
579
|
-
with io.open(_SWARM_RS, encoding="utf-8", newline="") as f:
|
|
580
|
-
if f.read() != pristine:
|
|
581
|
-
print(" RESTORE-FAILED %s" % name)
|
|
582
|
-
bad.append(name + "(恢复失败)")
|
|
583
|
-
_rebuild()
|
|
584
|
-
if bad:
|
|
585
|
-
print()
|
|
586
|
-
print("定点变异自证:FAIL -- %s" % "、".join(bad))
|
|
587
|
-
return 1
|
|
588
|
-
print()
|
|
589
|
-
print("定点变异自证:PASS(每处判据都被打红)")
|
|
590
|
-
return 0
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
def _cleanup():
|
|
594
|
-
for t in _SANDBOXES:
|
|
595
|
-
shutil.rmtree(t, ignore_errors=True)
|
|
596
|
-
shutil.rmtree(_PBC_DIR, ignore_errors=True)
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
if __name__ == "__main__":
|
|
600
|
-
if "--mutate" in sys.argv:
|
|
601
|
-
rc = _mutate()
|
|
602
|
-
_cleanup()
|
|
603
|
-
sys.exit(rc)
|
|
604
|
-
rc = main()
|
|
605
|
-
_cleanup()
|
|
606
|
-
sys.exit(rc)
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
r"""#354 P0-2 / P1-6 / P1-7 守卫(Rust 蜂群 WAL 面):损坏不得当成空、
|
|
3
|
+
seq/round 连续性可察觉、并发恢复不丢行。
|
|
4
|
+
|
|
5
|
+
缺陷(本件修复,2026-10-14 · swarm/rust_runtime/src/swarm.rs)
|
|
6
|
+
----------------------------------------------------------------
|
|
7
|
+
【P0-2】replay_wal 用 read_to_string **整文件一次性解码**,并把**所有** Err
|
|
8
|
+
(含「文件存在但含非法 UTF-8」的 InvalidData)一律当「无 WAL = 首跑」:
|
|
9
|
+
had_wal_file=false 且 completed=None ⇒ open_wal_for_run 走 File::create
|
|
10
|
+
**原位清空**且不归档 —— 已提交历史被无痕销毁,故障自愈退化成数据毁灭源。
|
|
11
|
+
实测(修前,真二进制 protocol_vm swarm,两场景只差尾字节可否解码):
|
|
12
|
+
· 合法快照+事件前缀 + 尾 0xFF 0xFE ⇒ 前缀消失、无 .corrupt-* 归档;
|
|
13
|
+
· 同一前缀 + 尾为**合法 UTF-8** 的半行 ⇒ 前缀完好(正常 B1 截断)。
|
|
14
|
+
【P1-6】WAL 无 seq/round 单调连续性校验 ⇒ 整行删除/乱序对逐行验签不可检,
|
|
15
|
+
丢行「不可察觉」(报告里也不存在任何可回读面)。
|
|
16
|
+
【P1-7】rewrite_wal_atomic 无进程排他锁且临时文件名**固定**({wal}.tmp)⇒
|
|
17
|
+
两个 coordinator 对同一 WAL 并发恢复时操作同一个 inode(O_TRUNC 互相截断 +
|
|
18
|
+
交替写入),rename 换入的 WAL 是两份内容的交错混合(半行/丢行)且失败方静默。
|
|
19
|
+
|
|
20
|
+
本守卫三层
|
|
21
|
+
----------
|
|
22
|
+
[A] 行为面(真二进制 protocol_vm swarm --config --wal,端到端):
|
|
23
|
+
W1 非法 UTF-8 尾 ⇒ 已提交前缀逐字节幸存(修前:整文件被清空);
|
|
24
|
+
W2 整段不可解码 ⇒ 必须归档留痕(.corrupt-* 逐字节),不是静默清空;
|
|
25
|
+
W3 阴性对照:合法 UTF-8 坏尾 ⇒ 前缀照常幸存(防过度修复);
|
|
26
|
+
W4 seq 跳号 ⇒ 报告 wal_continuity.ok=false + 明细 + stderr 透出(修前静默);
|
|
27
|
+
W5 锁被他人持有 ⇒ fail-closed(WAL 逐字节不动);释放后可恢复;
|
|
28
|
+
W6 两个 coordinator 并发恢复 ⇒ 双方 rc=0、前缀幸存、无撕裂行。
|
|
29
|
+
[B] 源断言(结构性,行为面不可用时仍决定 rc)。
|
|
30
|
+
[C] 定点变异:P0-2 回退 / P1-6 回退 / P1-7 回退 → 判据必须转红
|
|
31
|
+
(改源 → cargo build → 跑守卫转红 → 还原 → 重建)。
|
|
32
|
+
|
|
33
|
+
运行:
|
|
34
|
+
python -X utf8 -m md_cg.test_issue354_rust_wal_containment
|
|
35
|
+
python -X utf8 -m md_cg.test_issue354_rust_wal_containment --mutate
|
|
36
|
+
退出码:0 全绿 / 1 有失败。
|
|
37
|
+
环境:PROTOCOL_VM_EXE 可指向别处的 protocol_vm 可执行文件。
|
|
38
|
+
诚实边界:行为面需要 swarm/rust_runtime/target/release 下的可执行件(gitignored),
|
|
39
|
+
本守卫按仓内既有口径自建;cargo 与产物俱不可得时行为面显式 SKIP 并打印构建命令
|
|
40
|
+
(源断言面照判、照决定 rc)。W6 是**并发烟测**(时序相关):它的判别力弱于 W5 的
|
|
41
|
+
确定性锁判据,两者并置——W5 决定「锁在不在」,W6 证「并发下不撕裂」。
|
|
42
|
+
"""
|
|
43
|
+
from __future__ import annotations
|
|
44
|
+
|
|
45
|
+
import hashlib
|
|
46
|
+
import hmac as _hmac
|
|
47
|
+
import io
|
|
48
|
+
import json
|
|
49
|
+
import os
|
|
50
|
+
import shutil
|
|
51
|
+
import subprocess
|
|
52
|
+
import sys
|
|
53
|
+
import tempfile
|
|
54
|
+
import time
|
|
55
|
+
|
|
56
|
+
try:
|
|
57
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
58
|
+
except AttributeError:
|
|
59
|
+
pass
|
|
60
|
+
|
|
61
|
+
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
62
|
+
_RT_DIR = os.path.join(_REPO, "swarm", "rust_runtime")
|
|
63
|
+
_SWARM_RS = os.path.join(_RT_DIR, "src", "swarm.rs")
|
|
64
|
+
|
|
65
|
+
PASS = 0
|
|
66
|
+
FAIL = 0
|
|
67
|
+
FAILS = []
|
|
68
|
+
SKIPS = []
|
|
69
|
+
_SANDBOXES = []
|
|
70
|
+
_SILENT = False
|
|
71
|
+
_VIOL = []
|
|
72
|
+
|
|
73
|
+
#: 哑密钥(每测试唯一合成;绝不硬编码真实密钥)
|
|
74
|
+
SECRET = "i354-哑密钥-" + os.urandom(6).hex()
|
|
75
|
+
SNAPSHOT_TYPE = "__snapshot__"
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def check(name, cond, detail=""):
|
|
79
|
+
global PASS, FAIL
|
|
80
|
+
if _SILENT:
|
|
81
|
+
if not cond:
|
|
82
|
+
_VIOL.append(name)
|
|
83
|
+
return bool(cond)
|
|
84
|
+
if cond:
|
|
85
|
+
PASS += 1
|
|
86
|
+
print(" [PASS] %s%s" % (name, (" · " + str(detail)) if detail else ""))
|
|
87
|
+
else:
|
|
88
|
+
FAIL += 1
|
|
89
|
+
FAILS.append(name)
|
|
90
|
+
print(" [FAIL] %s · %s" % (name, detail))
|
|
91
|
+
return bool(cond)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def skip(name, why):
|
|
95
|
+
SKIPS.append(name)
|
|
96
|
+
print(" [SKIP] %s · %s" % (name, why))
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _exe_path():
|
|
100
|
+
env = os.environ.get("PROTOCOL_VM_EXE")
|
|
101
|
+
if env:
|
|
102
|
+
return env
|
|
103
|
+
base = os.path.join(_RT_DIR, "target", "release", "protocol_vm")
|
|
104
|
+
return base + ".exe" if os.name == "nt" else base
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
EXE = _exe_path()
|
|
108
|
+
_BUILD_CMD = "cargo build --release (在 %s 下执行)" % _RT_DIR
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _ensure_exe():
|
|
112
|
+
if os.path.isfile(EXE):
|
|
113
|
+
return True
|
|
114
|
+
if not shutil.which("cargo") or not os.path.isdir(_RT_DIR):
|
|
115
|
+
print(" (%s 不在场且 cargo 不可用)" % EXE)
|
|
116
|
+
return False
|
|
117
|
+
print(" 自建:%s" % _BUILD_CMD)
|
|
118
|
+
r = subprocess.run(["cargo", "build", "--release"], cwd=_RT_DIR,
|
|
119
|
+
capture_output=True, text=True, encoding="utf-8",
|
|
120
|
+
errors="replace", timeout=900)
|
|
121
|
+
if r.returncode != 0:
|
|
122
|
+
print(" 构建失败:%s" % (r.stderr or "")[-400:])
|
|
123
|
+
return False
|
|
124
|
+
return os.path.isfile(EXE)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
# ============================ WAL 合成(与在线写入同格式) ============================
|
|
128
|
+
|
|
129
|
+
def _sign(seq, typ, frm, to, rnd, ts, payload):
|
|
130
|
+
"""签名串 v0.7.1:seq|type|from|to|round|ts|payload(与 Rust sign_event 同式)。"""
|
|
131
|
+
msg = "%d|%s|%s|%s|%d|%d|%s" % (seq, typ, frm, to, rnd, ts, payload)
|
|
132
|
+
return _hmac.new(SECRET.encode(), msg.encode(), hashlib.sha256).hexdigest()
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _line(seq, typ, frm, to, rnd, payload, ts=1758888800000):
|
|
136
|
+
return ('{"seq":%d,"ts":%d,"from":"%s","to":"%s","type":"%s","round":%d,'
|
|
137
|
+
'"level":0,"hmac":"%s","payload":%s}'
|
|
138
|
+
% (seq, ts, frm, to, typ, rnd, _sign(seq, typ, frm, to, rnd, ts, payload),
|
|
139
|
+
payload))
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _snap(seq, rnd, trusts='"i1":0.5'):
|
|
143
|
+
return _line(seq, SNAPSHOT_TYPE, "协调器", "协调器", rnd,
|
|
144
|
+
'{"trusts":{%s},"states":{}}' % trusts)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _ev(seq, rnd, payload='{"x":1}'):
|
|
148
|
+
return _line(seq, "MSG", "i1", "i1", rnd, payload)
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
CONFIG = {"algo": "rust_swarm-0.1", "shared_secret": SECRET, "rounds": 2,
|
|
152
|
+
"topology": "", "routes": [],
|
|
153
|
+
"instances": [{"id": "i1", "role": "worker", "trust": 0.5, "symbols": {}}]}
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def _mkspace(tag):
|
|
157
|
+
t = tempfile.mkdtemp(prefix="i354wal_%s_" % tag)
|
|
158
|
+
_SANDBOXES.append(t)
|
|
159
|
+
return t
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _write_cfg(t, rounds=2):
|
|
163
|
+
cfg = dict(CONFIG)
|
|
164
|
+
cfg["rounds"] = rounds
|
|
165
|
+
p = os.path.join(t, "swarm.json")
|
|
166
|
+
io.open(p, "w", encoding="utf-8", newline="").write(json.dumps(cfg, ensure_ascii=False))
|
|
167
|
+
return p
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
_PBC = [None] # 惰性编译(None=未尝试;""=不可用)
|
|
171
|
+
#: pbc 必须落在**跨用例存活**的位置:若编进某个用例的沙箱,该用例收尾 rmtree 后
|
|
172
|
+
#: 缓存路径即成死路径 ⇒ 后续用例的实例子进程载不到字节码而自死(本守卫首版实测:
|
|
173
|
+
#: W1 正常、W2/W5/W6 全报「实例终态非法」,噪声掩盖真判据)。
|
|
174
|
+
_PBC_DIR = tempfile.mkdtemp(prefix="i354wal_pbc_")
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _pbc_path(t=None):
|
|
178
|
+
if _PBC[0] is None:
|
|
179
|
+
try:
|
|
180
|
+
sys.path.insert(0, _REPO)
|
|
181
|
+
from compiler.pbc import compile_to_pbc
|
|
182
|
+
p = os.path.join(_PBC_DIR, "program.pbc")
|
|
183
|
+
ok, _r = compile_to_pbc("术曰:\n1。道 新信任路径;\n2。止。\n", p)
|
|
184
|
+
_PBC[0] = p if ok and os.path.exists(p) else ""
|
|
185
|
+
except Exception as exc: # noqa: BLE001
|
|
186
|
+
print(" (pbc 编译不可用,退化为「实例自死」形态:%s)" % exc)
|
|
187
|
+
_PBC[0] = ""
|
|
188
|
+
return _PBC[0]
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def _argv(t, wal, rounds=2):
|
|
192
|
+
cmd = [EXE, "swarm", "--config", _write_cfg(t, rounds), "--wal", wal]
|
|
193
|
+
pbc = _pbc_path(t)
|
|
194
|
+
if pbc:
|
|
195
|
+
cmd += ["--pbc", pbc]
|
|
196
|
+
return cmd
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _run(t, wal, rounds=2, timeout=120):
|
|
200
|
+
"""跑一次 coordinator;返回 (rc, report|None, stderr)。"""
|
|
201
|
+
p = subprocess.run(_argv(t, wal, rounds), capture_output=True, text=True,
|
|
202
|
+
encoding="utf-8", errors="replace", timeout=timeout, cwd=t)
|
|
203
|
+
rep = None
|
|
204
|
+
if p.stdout.strip():
|
|
205
|
+
try:
|
|
206
|
+
rep = json.loads(p.stdout.strip().splitlines()[-1])
|
|
207
|
+
except Exception: # noqa: BLE001
|
|
208
|
+
rep = None
|
|
209
|
+
return p.returncode, rep, (p.stderr or "")
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def _run_parallel(t, wal, rounds=2, n=2, timeout=180):
|
|
213
|
+
"""并发拉起 n 个 coordinator(同一 WAL);返回 [(rc, stderr)...]。"""
|
|
214
|
+
procs = [subprocess.Popen(_argv(t, wal, rounds), stdin=subprocess.DEVNULL,
|
|
215
|
+
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
216
|
+
text=True, encoding="utf-8", errors="replace", cwd=t)
|
|
217
|
+
for _ in range(n)]
|
|
218
|
+
out = []
|
|
219
|
+
for p in procs:
|
|
220
|
+
try:
|
|
221
|
+
so, se = p.communicate(timeout=timeout)
|
|
222
|
+
except subprocess.TimeoutExpired:
|
|
223
|
+
p.kill()
|
|
224
|
+
so, se = p.communicate()
|
|
225
|
+
out.append((p.returncode, so or "", se or ""))
|
|
226
|
+
return out
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def _read(wal):
|
|
230
|
+
return io.open(wal, "rb").read()
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _archives(d, prefix="events.jsonl.corrupt-"):
|
|
234
|
+
return sorted(n for n in os.listdir(d) if n.startswith(prefix))
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def _verify_lines(data):
|
|
238
|
+
"""逐行校验:能被本密钥验签的行数 / 撕裂(不可解析或缺 payload 标记)行数。"""
|
|
239
|
+
good = torn = 0
|
|
240
|
+
for raw in data.split(b"\n"):
|
|
241
|
+
if not raw.strip():
|
|
242
|
+
continue
|
|
243
|
+
try:
|
|
244
|
+
rec = json.loads(raw.decode("utf-8"))
|
|
245
|
+
except Exception: # noqa: BLE001
|
|
246
|
+
torn += 1
|
|
247
|
+
continue
|
|
248
|
+
try:
|
|
249
|
+
rawp = raw.decode("utf-8")
|
|
250
|
+
seg = rawp[rawp.index('"payload":') + len('"payload":'):]
|
|
251
|
+
payload = seg[:-1] if seg.endswith("}") else seg
|
|
252
|
+
msg = "%s|%s|%s|%s|%s|%s|%s" % (rec["seq"], rec["type"], rec["from"],
|
|
253
|
+
rec["to"], rec["round"], rec["ts"], payload)
|
|
254
|
+
exp = _hmac.new(SECRET.encode(), msg.encode(), hashlib.sha256).hexdigest()
|
|
255
|
+
except Exception: # noqa: BLE001
|
|
256
|
+
torn += 1
|
|
257
|
+
continue
|
|
258
|
+
if _hmac.compare_digest(exp, rec.get("hmac", "")):
|
|
259
|
+
good += 1
|
|
260
|
+
else:
|
|
261
|
+
torn += 1
|
|
262
|
+
return good, torn
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
# ============================ [A] 行为面 ============================
|
|
266
|
+
|
|
267
|
+
def w1_invalid_utf8_tail():
|
|
268
|
+
print("== W1 P0-2:非法 UTF-8 尾不得清空已提交历史 ==")
|
|
269
|
+
t = _mkspace("p02")
|
|
270
|
+
wal = os.path.join(t, "events.jsonl")
|
|
271
|
+
snap, ev = _snap(1, 1), _ev(2, 1)
|
|
272
|
+
orig = (snap + "\n" + ev + "\n").encode("utf-8") + b"\xff\xfe"
|
|
273
|
+
io.open(wal, "wb").write(orig)
|
|
274
|
+
rc, rep, err = _run(t, wal, rounds=2)
|
|
275
|
+
after = _read(wal)
|
|
276
|
+
check("W1 coordinator 正常退出(rc=0)", rc == 0, (rc, err[-200:]))
|
|
277
|
+
check("W1 已提交快照行逐字节幸存(修前:整文件被 File::create 清空)",
|
|
278
|
+
snap.encode("utf-8") in after, _head(after))
|
|
279
|
+
check("W1 未提交的非法尾被物理截断(B1 语义不变)",
|
|
280
|
+
b"\xff\xfe" not in after)
|
|
281
|
+
check("W1 有提交点时不得归档(另有 W2 专测零提交归档)",
|
|
282
|
+
not _archives(t), _archives(t))
|
|
283
|
+
check("W1 残留件核查:目录内无 .lock / .tmp(锁与临时件都该清干净)",
|
|
284
|
+
not [n for n in os.listdir(t) if n.endswith(".lock") or ".tmp" in n],
|
|
285
|
+
os.listdir(t))
|
|
286
|
+
shutil.rmtree(t, ignore_errors=True)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def w2_undecodable_archived():
|
|
290
|
+
print("== W2 P0-2:整段不可解码必须归档留痕(不得静默清空)==")
|
|
291
|
+
t = _mkspace("p02b")
|
|
292
|
+
wal = os.path.join(t, "events.jsonl")
|
|
293
|
+
orig = b"\xff\xfe\xfd\n\x80\x81\n"
|
|
294
|
+
io.open(wal, "wb").write(orig)
|
|
295
|
+
rc, rep, err = _run(t, wal, rounds=1)
|
|
296
|
+
arch = _archives(t)
|
|
297
|
+
check("W2 coordinator 正常退出(零提交 → 从轮 1 重跑)", rc == 0, (rc, err[-200:]))
|
|
298
|
+
check("W2 恰好一份 .corrupt-* 归档", len(arch) == 1, arch)
|
|
299
|
+
if arch:
|
|
300
|
+
check("W2 归档逐字节保留原 WAL(审计链可离线取证)",
|
|
301
|
+
_read(os.path.join(t, arch[0])) == orig)
|
|
302
|
+
check("W2 新 WAL 不含原坏字节(已另行归档,不是原位残留)",
|
|
303
|
+
b"\xff\xfe\xfd" not in _read(wal))
|
|
304
|
+
shutil.rmtree(t, ignore_errors=True)
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def w3_valid_bad_tail_control():
|
|
308
|
+
print("== W3 阴性对照:合法 UTF-8 坏尾照常截断(防过度修复)==")
|
|
309
|
+
t = _mkspace("ctl")
|
|
310
|
+
wal = os.path.join(t, "events.jsonl")
|
|
311
|
+
snap, ev = _snap(1, 1), _ev(2, 1)
|
|
312
|
+
io.open(wal, "wb").write(
|
|
313
|
+
(snap + "\n" + ev + "\n" + '{"seq":3,"ts":1,"from":"BAD half').encode("utf-8"))
|
|
314
|
+
rc, rep, err = _run(t, wal, rounds=2)
|
|
315
|
+
after = _read(wal)
|
|
316
|
+
check("W3 rc=0 且前缀幸存", rc == 0 and snap.encode("utf-8") in after,
|
|
317
|
+
(rc, _head(after)))
|
|
318
|
+
check("W3 合法坏尾被物理截断", b"BAD half" not in after)
|
|
319
|
+
check("W3 不触发归档(有提交点)", not _archives(t), _archives(t))
|
|
320
|
+
shutil.rmtree(t, ignore_errors=True)
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def w4_seq_gap_detectable():
|
|
324
|
+
print("== W4 P1-6:seq 跳号必须可察觉(报告 + stderr)==")
|
|
325
|
+
t = _mkspace("p16")
|
|
326
|
+
wal = os.path.join(t, "events.jsonl")
|
|
327
|
+
# 1(快照r1),2,[缺 3/4],5,6(快照r2):一处跳号;completed=2 → 目标 2 轮已完成,
|
|
328
|
+
# 走早退聚合路径(不 spawn 实例、不改 WAL),只读连续性读数。
|
|
329
|
+
io.open(wal, "wb").write(
|
|
330
|
+
("\n".join([_snap(1, 1), _ev(2, 1), _ev(5, 1), _snap(6, 2)]) + "\n").encode("utf-8"))
|
|
331
|
+
rc, rep, err = _run(t, wal, rounds=2)
|
|
332
|
+
wc = (rep or {}).get("wal_continuity")
|
|
333
|
+
check("W4 报告必须带 wal_continuity 字段(可回读面)", isinstance(wc, dict),
|
|
334
|
+
wc)
|
|
335
|
+
check("W4 跳号被报出(ok=false 且 breaks>=1)",
|
|
336
|
+
bool(wc) and wc.get("ok") is False and (wc.get("breaks") or 0) >= 1 and
|
|
337
|
+
len(wc.get("detail") or []) >= 1, wc)
|
|
338
|
+
if wc and wc.get("detail"):
|
|
339
|
+
d = wc["detail"][0]
|
|
340
|
+
check("W4 明细含 line/expected/actual/kind(可定位、可复核)",
|
|
341
|
+
d.get("line") == 3 and d.get("expected") == 3 and d.get("actual") == 5
|
|
342
|
+
and d.get("kind") == "跳号缺失", d)
|
|
343
|
+
check("W4 stderr 亦透出(旧实现完全静默 = 不可察觉)",
|
|
344
|
+
"连续性中断" in err and "P1-6" in err, err.strip()[-200:])
|
|
345
|
+
shutil.rmtree(t, ignore_errors=True)
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def w4b_continuous_wal_silent():
|
|
349
|
+
print("== W4b 阴性对照:连续 WAL 不得误报、不得刷告警 ==")
|
|
350
|
+
t = _mkspace("p16ok")
|
|
351
|
+
wal = os.path.join(t, "events.jsonl")
|
|
352
|
+
io.open(wal, "wb").write(
|
|
353
|
+
("\n".join([_snap(1, 1), _ev(2, 1), _snap(3, 2)]) + "\n").encode("utf-8"))
|
|
354
|
+
rc, rep, err = _run(t, wal, rounds=2)
|
|
355
|
+
wc = (rep or {}).get("wal_continuity")
|
|
356
|
+
check("W4b ok=true 且 breaks=0", bool(wc) and wc.get("ok") is True
|
|
357
|
+
and (wc.get("breaks") or 0) == 0, wc)
|
|
358
|
+
check("W4b 不得刷连续性告警(零噪声)", "连续性中断" not in err, err.strip()[-160:])
|
|
359
|
+
shutil.rmtree(t, ignore_errors=True)
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
def w5_held_lock_fails_closed():
|
|
363
|
+
print("== W5 P1-7:锁被他人持有 ⇒ fail-closed(WAL 逐字节不动)==")
|
|
364
|
+
t = _mkspace("p17")
|
|
365
|
+
wal = os.path.join(t, "events.jsonl")
|
|
366
|
+
orig = (_snap(1, 1) + "\n" + '{"seq":2,"ts":1,"from":"BAD half').encode("utf-8")
|
|
367
|
+
io.open(wal, "wb").write(orig)
|
|
368
|
+
lock = wal + ".lock"
|
|
369
|
+
io.open(lock, "w", encoding="utf-8", newline="").write("99999 0\n")
|
|
370
|
+
rc, rep, err = _run(t, wal, rounds=2)
|
|
371
|
+
check("W5 持锁时不得改写:WAL 逐字节不动(修前:无锁照写)",
|
|
372
|
+
_read(wal) == orig, _head(_read(wal)))
|
|
373
|
+
check("W5 持锁时非零退出且原因点名排他锁(可诊断)",
|
|
374
|
+
rc != 0 and "排他锁" in err, (rc, err.strip()[-200:]))
|
|
375
|
+
os.remove(lock)
|
|
376
|
+
rc2, rep2, err2 = _run(t, wal, rounds=2)
|
|
377
|
+
after2 = _read(wal)
|
|
378
|
+
check("W5 释放后可恢复(锁不是一次性)",
|
|
379
|
+
rc2 == 0 and _snap(1, 1).encode("utf-8") in after2, (rc2, err2[-160:]))
|
|
380
|
+
check("W5 正常路径不留锁件",
|
|
381
|
+
not os.path.exists(lock), os.listdir(t))
|
|
382
|
+
shutil.rmtree(t, ignore_errors=True)
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
def w6_concurrent_recovery():
|
|
386
|
+
print("== W6 P1-7:两个 coordinator 并发恢复同一 WAL(烟测)==")
|
|
387
|
+
t = _mkspace("p17c")
|
|
388
|
+
wal = os.path.join(t, "events.jsonl")
|
|
389
|
+
snap = _snap(1, 1)
|
|
390
|
+
io.open(wal, "wb").write(
|
|
391
|
+
(snap + "\n" + '{"seq":2,"ts":1,"from":"BAD half').encode("utf-8"))
|
|
392
|
+
res = _run_parallel(t, wal, rounds=2, n=2)
|
|
393
|
+
rcs = [r[0] for r in res]
|
|
394
|
+
after = _read(wal)
|
|
395
|
+
good, torn = _verify_lines(after)
|
|
396
|
+
check("W6 双方 rc=0(旧实现固定 tmp 名 → rename 竞争,失败方 Err 退出)",
|
|
397
|
+
all(x == 0 for x in rcs), (rcs, [r[2][-120:] for r in res]))
|
|
398
|
+
check("W6 已提交前缀幸存", snap.encode("utf-8") in after, _head(after))
|
|
399
|
+
check("W6 终态 WAL 无撕裂行(每行均可解析且验签)", torn == 0,
|
|
400
|
+
{"good": good, "torn": torn, "tail": after[-160:]})
|
|
401
|
+
shutil.rmtree(t, ignore_errors=True)
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def _head(b):
|
|
405
|
+
return bytes(b[:160])
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
# ============================ [B] 源断言 ============================
|
|
409
|
+
|
|
410
|
+
def _src():
|
|
411
|
+
return io.open(_SWARM_RS, encoding="utf-8", errors="replace").read()
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def b_source_face():
|
|
415
|
+
print("== B 源断言(行为面不可用时仍决定 rc)==")
|
|
416
|
+
src = _src()
|
|
417
|
+
check("B1 P0-2:WAL 不再整文件解码(read_to_string(wal_path) 已消失)",
|
|
418
|
+
"read_to_string(wal_path)" not in src)
|
|
419
|
+
check("B1b P0-2:「文件不存在」与「文件损坏」分道",
|
|
420
|
+
"ErrorKind::NotFound => return Ok(rp)" in src
|
|
421
|
+
and "WAL 存在但不可读" in src)
|
|
422
|
+
check("B1c P0-2:行级容错解码(坏字节只作废其所在行)",
|
|
423
|
+
"std::str::from_utf8(chunk).ok()" in src)
|
|
424
|
+
# 按**代码用法**断言(带尾逗号/表达式形态),不按裸词匹配:三种 kind 的字面量
|
|
425
|
+
# 也出现在 SeqBreak 的文档注释里,裸词匹配会被注释喂饱(V2 首版即由此漏判)。
|
|
426
|
+
check("B2 P1-6:seq/round 连续性判据在场(三种 kind 的代码用法)",
|
|
427
|
+
'if ev.seq > exp { "跳号缺失" }' in src
|
|
428
|
+
and '"round 倒退",' in src
|
|
429
|
+
and '"快照轮号重复或倒退",' in src)
|
|
430
|
+
# 注意:源码里该键名是 Rust 字面量内的转义形态(\"wal_continuity\"),故按
|
|
431
|
+
# 「访问器 + 报告发射点」两个无歧义标记断言,不按裸引号形态匹配。
|
|
432
|
+
check("B2b P1-6:读数进报告(wal_continuity 出口)",
|
|
433
|
+
"pub fn ok(&self) -> bool" in src and "rep.wal_continuity.ok()" in src)
|
|
434
|
+
check("B3 P1-7:排他锁在场且被恢复路径取用",
|
|
435
|
+
"struct WalLock" in src and "WalLock::acquire(wal_path)?" in src
|
|
436
|
+
and "LOCK_STALE_MS" in src)
|
|
437
|
+
check("B3b P1-7:临时文件名含进程号(不再是固定 {wal}.tmp)",
|
|
438
|
+
'format!("{wal_path}.tmp.{}", std::process::id())' in src
|
|
439
|
+
and 'format!("{wal_path}.tmp")' not in src)
|
|
440
|
+
check("B4 反陈化:三处缺陷都点名 issue #354 与条号",
|
|
441
|
+
"#354 P0-2" in src and "#354 P1-6" in src and "#354 P1-7" in src)
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
# ============================ 主流程 ============================
|
|
445
|
+
|
|
446
|
+
_BEHAVIOR = (w1_invalid_utf8_tail, w2_undecodable_archived, w3_valid_bad_tail_control,
|
|
447
|
+
w4_seq_gap_detectable, w4b_continuous_wal_silent,
|
|
448
|
+
w5_held_lock_fails_closed, w6_concurrent_recovery)
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def _run_groups(behavior_ok):
|
|
452
|
+
global PASS, FAIL
|
|
453
|
+
PASS = 0
|
|
454
|
+
FAIL = 0
|
|
455
|
+
del FAILS[:]
|
|
456
|
+
del SKIPS[:]
|
|
457
|
+
for g in _BEHAVIOR:
|
|
458
|
+
if not behavior_ok:
|
|
459
|
+
skip(g.__name__, "protocol_vm 可执行件不可得(构建命令:%s)" % _BUILD_CMD)
|
|
460
|
+
continue
|
|
461
|
+
try:
|
|
462
|
+
g()
|
|
463
|
+
except Exception as exc: # noqa: BLE001
|
|
464
|
+
check("断言组 %s 抛异常:%s" % (g.__name__, exc), False)
|
|
465
|
+
import traceback
|
|
466
|
+
print(traceback.format_exc()[-700:])
|
|
467
|
+
b_source_face()
|
|
468
|
+
return len(FAILS)
|
|
469
|
+
|
|
470
|
+
|
|
471
|
+
def main():
|
|
472
|
+
behavior_ok = _ensure_exe()
|
|
473
|
+
if not behavior_ok:
|
|
474
|
+
print("!! 行为面 SKIP:%s 不在场且无法自建 —— 构建命令:%s"
|
|
475
|
+
% (EXE, _BUILD_CMD))
|
|
476
|
+
n = _run_groups(behavior_ok)
|
|
477
|
+
print()
|
|
478
|
+
print("issue354 Rust WAL 守卫(P0-2 损坏不得当空 / P1-6 连续性可察觉 / P1-7 并发恢复):"
|
|
479
|
+
"%d 通过,%d 失败,%d 跳过" % (PASS, n, len(SKIPS)))
|
|
480
|
+
return 1 if n else 0
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
# ============================ [C] 定点变异自证 ============================
|
|
484
|
+
|
|
485
|
+
_MUTATIONS = (
|
|
486
|
+
("V1 P0-2 回退:整文件解码 + 所有 Err 当首跑",
|
|
487
|
+
" let bytes = match std::fs::read(wal_path) {\n"
|
|
488
|
+
" Ok(b) => b,\n"
|
|
489
|
+
" // 文件不存在 = 首跑(唯一可当「无 WAL」的分支)\n"
|
|
490
|
+
" Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(rp),\n"
|
|
491
|
+
" // 存在但不可读(权限 / IO / 路径是目录):损坏不得当成空 ⇒ 交调用方,\n"
|
|
492
|
+
" // 由它按「不销毁现场」的路径处置\n"
|
|
493
|
+
" Err(e) => {\n"
|
|
494
|
+
" return Err(format!(\n"
|
|
495
|
+
" \"WAL 存在但不可读(issue #354 P0-2:损坏不得当成空,拒绝继续): {wal_path}: {e}\"\n"
|
|
496
|
+
" ))\n"
|
|
497
|
+
" }\n"
|
|
498
|
+
" };\n"
|
|
499
|
+
" rp.had_wal_file = true;\n",
|
|
500
|
+
" let bytes = match std::fs::read_to_string(wal_path) {\n"
|
|
501
|
+
" Ok(t) => t.into_bytes(),\n"
|
|
502
|
+
" Err(_) => return Ok(rp),\n"
|
|
503
|
+
" };\n"
|
|
504
|
+
" rp.had_wal_file = true;\n",
|
|
505
|
+
"W1", "W2", "B1", "B1b"),
|
|
506
|
+
("V2 P1-6 回退:删掉 seq 连续性检测",
|
|
507
|
+
" if let Some(exp) = expected_seq {\n"
|
|
508
|
+
" if ev.seq != exp {\n"
|
|
509
|
+
" note_break(\n"
|
|
510
|
+
" &mut rp.continuity,\n"
|
|
511
|
+
" idx + 1,\n"
|
|
512
|
+
" exp,\n"
|
|
513
|
+
" ev.seq,\n"
|
|
514
|
+
" if ev.seq > exp { \"跳号缺失\" } else { \"重复或乱序\" },\n"
|
|
515
|
+
" &ev.event_type,\n"
|
|
516
|
+
" );\n"
|
|
517
|
+
" }\n"
|
|
518
|
+
" }\n",
|
|
519
|
+
" expected_seq = Some(ev.seq + 1);\n",
|
|
520
|
+
"W4", "B2"),
|
|
521
|
+
("V3 P1-7 回退:删掉排他锁取用",
|
|
522
|
+
" let _lock = WalLock::acquire(wal_path)?;\n",
|
|
523
|
+
"",
|
|
524
|
+
"W5", "B3"),
|
|
525
|
+
)
|
|
526
|
+
|
|
527
|
+
|
|
528
|
+
def _rebuild():
|
|
529
|
+
r = subprocess.run(["cargo", "build", "--release"], cwd=_RT_DIR,
|
|
530
|
+
capture_output=True, text=True, encoding="utf-8",
|
|
531
|
+
errors="replace", timeout=900)
|
|
532
|
+
return r.returncode == 0, (r.stderr or "")[-400:]
|
|
533
|
+
|
|
534
|
+
|
|
535
|
+
def _mutate():
|
|
536
|
+
bad = []
|
|
537
|
+
if not os.path.isfile(_SWARM_RS):
|
|
538
|
+
print(" SOURCE-MISSING:%s" % _SWARM_RS)
|
|
539
|
+
return 1
|
|
540
|
+
if not shutil.which("cargo"):
|
|
541
|
+
print(" NO-CARGO:变异自证需要 cargo(改源后必须重建,否则跑的还是旧二进制)")
|
|
542
|
+
return 1
|
|
543
|
+
with io.open(_SWARM_RS, encoding="utf-8", newline="") as f:
|
|
544
|
+
pristine = f.read()
|
|
545
|
+
global _SILENT
|
|
546
|
+
for name, anchor, repl, *want in _MUTATIONS:
|
|
547
|
+
with io.open(_SWARM_RS, encoding="utf-8", newline="") as f:
|
|
548
|
+
orig = f.read()
|
|
549
|
+
NL = "\r\n" if "\r\n" in orig else "\n"
|
|
550
|
+
a_ = anchor.replace("\n", NL)
|
|
551
|
+
r_ = repl.replace("\n", NL)
|
|
552
|
+
if a_ not in orig:
|
|
553
|
+
print(" ANCHOR-MISS %s" % name)
|
|
554
|
+
bad.append(name)
|
|
555
|
+
continue
|
|
556
|
+
try:
|
|
557
|
+
with io.open(_SWARM_RS, "w", encoding="utf-8", newline="") as f:
|
|
558
|
+
f.write(orig.replace(a_, r_, 1))
|
|
559
|
+
ok_build, blog = _rebuild()
|
|
560
|
+
if not ok_build:
|
|
561
|
+
print(" BUILD-FAIL %s:%s" % (name, blog))
|
|
562
|
+
bad.append(name + "(构建失败)")
|
|
563
|
+
continue
|
|
564
|
+
_SILENT = True
|
|
565
|
+
del _VIOL[:]
|
|
566
|
+
_run_groups(True)
|
|
567
|
+
reds = list(_VIOL)
|
|
568
|
+
hit = [w for w in want if any(x.startswith(w) for x in reds)]
|
|
569
|
+
print(" %-4s %-44s 红项=%-3d 命中=%s"
|
|
570
|
+
% ("OK" if len(hit) == len(want) else "BAD", name, len(reds),
|
|
571
|
+
",".join(hit) if hit else "无"))
|
|
572
|
+
if len(hit) != len(want):
|
|
573
|
+
print(" 期望红:%s;实红:%s" % (list(want), reds[:8]))
|
|
574
|
+
bad.append(name)
|
|
575
|
+
finally:
|
|
576
|
+
_SILENT = False
|
|
577
|
+
with io.open(_SWARM_RS, "w", encoding="utf-8", newline="") as f:
|
|
578
|
+
f.write(pristine)
|
|
579
|
+
with io.open(_SWARM_RS, encoding="utf-8", newline="") as f:
|
|
580
|
+
if f.read() != pristine:
|
|
581
|
+
print(" RESTORE-FAILED %s" % name)
|
|
582
|
+
bad.append(name + "(恢复失败)")
|
|
583
|
+
_rebuild()
|
|
584
|
+
if bad:
|
|
585
|
+
print()
|
|
586
|
+
print("定点变异自证:FAIL -- %s" % "、".join(bad))
|
|
587
|
+
return 1
|
|
588
|
+
print()
|
|
589
|
+
print("定点变异自证:PASS(每处判据都被打红)")
|
|
590
|
+
return 0
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
def _cleanup():
|
|
594
|
+
for t in _SANDBOXES:
|
|
595
|
+
shutil.rmtree(t, ignore_errors=True)
|
|
596
|
+
shutil.rmtree(_PBC_DIR, ignore_errors=True)
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
if __name__ == "__main__":
|
|
600
|
+
if "--mutate" in sys.argv:
|
|
601
|
+
rc = _mutate()
|
|
602
|
+
_cleanup()
|
|
603
|
+
sys.exit(rc)
|
|
604
|
+
rc = main()
|
|
605
|
+
_cleanup()
|
|
606
|
+
sys.exit(rc)
|