@furongjun1999/dsh-memory 0.9.6 → 0.9.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +150 -662
- package/codebuddy/CODEBUDDY.md +333 -204
- package/data/policy.json +38 -38
- package/docs/README.md +18 -0
- package/docs/discipline/harnesses.yaml +11 -3
- package/docs/discipline/templates/compact.txt.tmpl +20 -18
- package/docs/discipline/templates/full.md.tmpl +63 -61
- package/docs/discipline/templates/rules.mdc.tmpl +5 -3
- package/docs/discipline/templates/skill.md.tmpl +25 -23
- package/docs/discipline/templates/spec.txt.tmpl +2 -2
- package/docs/discipline/templates/zcode-user.md.tmpl +1 -1
- package/dsh/cordis.yml.example +139 -139
- package/lib/hooks.d.ts +19 -0
- package/lib/hooks.js +30 -7
- package/lib/index.js +18 -3
- package/lib/init.js +19 -4
- package/lib/lib/hook_audit.js +64 -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/sustain.py +10 -1
- 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 +9 -3
- 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_n184_keys_concurrent_provision.py +1 -1
- 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_read_scope_b27.py +2 -2
- package/md_cg/test_retr_s5.py +312 -312
- package/md_cg/test_s1_ast_judge.py +109 -109
- package/md_cg/test_security_audit_v21.py +249 -249
- 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_bad_stamp.py +178 -0
- 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 +932 -908
- package/src/index.ts +21 -6
- package/src/init.ts +16 -4
- package/src/lib/hook_audit.ts +48 -2
- package/src/lib/mdcg_client.ts +469 -469
- package/src/lib/prompt_safety.ts +17 -3
- package/src/tools.ts +23 -1
- package/zcode/AGENTS.md +333 -204
|
@@ -1,604 +1,604 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
# 功能名:变更单回滚不得抹掉执行之后的写入、不得越过保护位、必须留痕(issue #352-A)
|
|
3
|
-
# 生效条件:python -X utf8 -m md_cg.test_issue352_rollback [--mutate];被测面=md_cg/rollback.py 的 rollback_mutation/_land + md_cg/rollback_cli.py + md_cg/mdcos.py 的执行时点后像补全
|
|
4
|
-
# 子功能:A 组=漂移闸(执行之后又被写 / B 第二张合并单 / D 删除后同 id 重建 / 执行后加 immutable)+ 回滚留痕 + 被覆盖版本留档 + --dry-run/--force;K 组=非恒真对照(合法回滚照常 / 命令串幂等重放 / 旧记录无 after_hash 仍放行 / 元数据改写不算漂移 / 前像本就受保护时不得拦死);E 组=源码面单点
|
|
5
|
-
# 执行:confirm 档出 C/B/D 三类变更单并 accept,执行后按场景追加写入,再调统一回滚入口或执行记录里的回滚命令串(subprocess)
|
|
6
|
-
# 验证方式:全部 PASS 即通过;--mutate 走镜像树定点变异,变异后红项必须**恰为**预期集合
|
|
7
|
-
# 不适用条件:①只判「回滚是否越权/抹掉执行之后的写入」,不判前像本身是否拍对(既有 test_mutation_rollback P/Q/R/S 组覆盖)②不判加密库的密文字节可比性(既有模块 docstring 已声明口径)③不判 D 的 restore 语义本身④不改 protect.py 既有 guard_*/snapshot 行为(守卫钉死)
|
|
8
|
-
"""issue #352-A 守卫:回滚越权(抹掉执行之后的写入 / 越 immutable / 不留痕)。
|
|
9
|
-
|
|
10
|
-
病象(修前):`rollback_mutation` 用**执行时点前像整体覆写**目标,不检查目标在
|
|
11
|
-
执行之后有没有再被写过——
|
|
12
|
-
· C 改写单:执行之后别人写进去的正文全部消失;
|
|
13
|
-
· B 合并单:当前正文以前像为前缀时,前缀之后的**所有**聚合行被剥掉,包括后续
|
|
14
|
-
另一张合并单追加的行;剥不精确时退回整份前像正文;
|
|
15
|
-
· D 删除单:删除之后同 id 新写入的内容被前像校准覆写;
|
|
16
|
-
· 执行之后目标被标 immutable 时照样覆写,immutable 标记被一并清掉;
|
|
17
|
-
· 被覆盖的版本不进 `_protected_history`、`_audit.jsonl` 里也没有回滚这一行。
|
|
18
|
-
回执是 `ok:true / bytes_equal_preimage:true`,执行回滚的人看不到任何提示。
|
|
19
|
-
|
|
20
|
-
处置(三条,缺一不可):
|
|
21
|
-
1. **漂移闸**:执行桥在执行落盘**之后**补一个盘面指纹 `after_hash`
|
|
22
|
-
(`rollback.node_fingerprint`),回滚前与现盘面比对;不等即 fail-closed
|
|
23
|
-
(`drifted_since_execute`)。旧记录无该键 ⇒ 退化为 decisions.jsonl 次序
|
|
24
|
-
判据,**不是「无键即放行」**。**幂等重放豁免**:现盘面已等于前像时放行
|
|
25
|
-
(此时覆写是空操作,抹不掉东西)。
|
|
26
|
-
2. **保护闸 + 留档**:`_land` 落盘前过既有单点 `protect.is_immutable`,落盘前
|
|
27
|
-
调 `protect.snapshot` 把将被覆盖的版本留进 `_protected_history`。
|
|
28
|
-
判据刻意**只在「现受保护 而 前像不带同一保护位」时拒**——否则受保护节点的
|
|
29
|
-
合法回滚会被一并拦死(K3/K7 是这条的对照)。
|
|
30
|
-
3. **留痕**:回滚成功即向 `_audit.jsonl` 写一行 `op="rollback"`(改前这一支
|
|
31
|
-
一行审计都不写)。
|
|
32
|
-
|
|
33
|
-
★**过度收紧方向**:本条的修复天然是「加拒绝」,故 K 组里 K3/K7 专门盯
|
|
34
|
-
「前像本就带保护位时**必须放行**」、K4 盯「旧记录不能一律拒」、K5 盯「元数据
|
|
35
|
-
改写不算漂移」;M7 是**过度收紧方向的定点变异**(把保护闸改成「现受保护即拒」),
|
|
36
|
-
它必须打红 K3/K7——证明这两条判据有判别力,不是恒真对照。
|
|
37
|
-
"""
|
|
38
|
-
import inspect
|
|
39
|
-
import json
|
|
40
|
-
import os
|
|
41
|
-
import shlex
|
|
42
|
-
import shutil
|
|
43
|
-
import subprocess
|
|
44
|
-
import sys
|
|
45
|
-
import tempfile
|
|
46
|
-
|
|
47
|
-
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
48
|
-
|
|
49
|
-
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
50
|
-
sys.path.insert(0, _REPO)
|
|
51
|
-
_TMP = tempfile.mkdtemp(prefix="iss352a_")
|
|
52
|
-
_POLICY = os.path.join(_TMP, "policy.json")
|
|
53
|
-
with open(_POLICY, "w", encoding="utf-8") as _f:
|
|
54
|
-
json.dump({"forbidden": ["FORBIDDEN_WORD"], "required": ["PASSED"]}, _f)
|
|
55
|
-
os.environ.update({"MDCG_ROOT": _TMP + "/brainroot",
|
|
56
|
-
"MDCG_STATE_ROOT": _TMP + "/state",
|
|
57
|
-
"MDCG_AUX_ROOT": _TMP + "/auxstore",
|
|
58
|
-
"MDCG_DATA_ROOT": _TMP + "/datastore",
|
|
59
|
-
"MDCG_EXPORT_ROOT": _TMP,
|
|
60
|
-
"MDCG_POLICY_FILE": _POLICY,
|
|
61
|
-
"MDCG_SUSTAIN": "0", "MDCG_SLEEP": "0"})
|
|
62
|
-
|
|
63
|
-
from md_cg import autonomy_modes # noqa: E402
|
|
64
|
-
|
|
65
|
-
os.environ[autonomy_modes.AUTONOMY_ENV_KEYS["mode"]] = "confirm"
|
|
66
|
-
|
|
67
|
-
from md_cg import forgetting, protect, rollback as _rb # noqa: E402
|
|
68
|
-
from md_cg import writepipe # noqa: E402
|
|
69
|
-
from md_cg.mdcos import MdCGOS # noqa: E402
|
|
70
|
-
|
|
71
|
-
FAILS = []
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
def _check(name, cond, detail=""):
|
|
75
|
-
if cond:
|
|
76
|
-
print(" PASS " + name)
|
|
77
|
-
else:
|
|
78
|
-
print(" FAIL " + name + " " + str(detail)[:400])
|
|
79
|
-
FAILS.append(name)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
_PLAIN = "PASSED 端口 %s"
|
|
83
|
-
_BODY = ("# 功能名:部署面端口约定\n"
|
|
84
|
-
"# 生效条件:prod 集群;2026-09-30 起;部署清单核对\n"
|
|
85
|
-
"# 子功能:登记各服务监听端口\n"
|
|
86
|
-
"# 执行:核对 manifest 的 ports 段\n"
|
|
87
|
-
"# 验证方式:test\n"
|
|
88
|
-
"# 不适用条件:无\n"
|
|
89
|
-
"\n网关监听端口 8080/HTTP;指标面监听端口 %s/HTTP\n")
|
|
90
|
-
|
|
91
|
-
_RUN = [0]
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
def _lib(tag):
|
|
95
|
-
_RUN[0] += 1
|
|
96
|
-
return MdCGOS(os.path.join(_TMP, "%s_%d" % (tag, _RUN[0])), autoflush=0)
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
def _pipe():
|
|
100
|
-
return writepipe.install_default_pipes() if hasattr(writepipe, "install_default_pipes") \
|
|
101
|
-
else writepipe.install_default_gates(writepipe.WritePipeline())
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
def _c_accept(cg, nid, new, old="8080"):
|
|
105
|
-
"""出一张 C 改写单并 accept;返回 ({"pid","mutation"}, 写链返回体)。"""
|
|
106
|
-
cg.add(nid, _PLAIN % old, layer="knowledge", verification_basis="test")
|
|
107
|
-
cg.flush()
|
|
108
|
-
o = _pipe().execute(cg, {"content_kind": "text", "content": _PLAIN % new,
|
|
109
|
-
"layer": "knowledge", "node_id": nid})
|
|
110
|
-
if o.get("moved_to") != "review_queue" or not o.get("pid"):
|
|
111
|
-
return None, o
|
|
112
|
-
d = cg.review_decide(o["pid"], "accept", reason="issue352 守卫")
|
|
113
|
-
return {"pid": o["pid"], "mutation": d.get("mutation") or {},
|
|
114
|
-
"accept": d}, o
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
def _audit_rows(cg):
|
|
118
|
-
p = os.path.join(cg.root, "_audit.jsonl")
|
|
119
|
-
if not os.path.isfile(p):
|
|
120
|
-
return []
|
|
121
|
-
out = []
|
|
122
|
-
with open(p, encoding="utf-8", errors="replace") as f:
|
|
123
|
-
for ln in f:
|
|
124
|
-
ln = ln.strip()
|
|
125
|
-
if ln:
|
|
126
|
-
try:
|
|
127
|
-
out.append(json.loads(ln))
|
|
128
|
-
except ValueError:
|
|
129
|
-
pass
|
|
130
|
-
return out
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
def _history_blobs(cg, nid):
|
|
134
|
-
import glob
|
|
135
|
-
d = os.path.join(cg.root, protect.HISTORY_DIR, nid)
|
|
136
|
-
blobs = []
|
|
137
|
-
for p in sorted(glob.glob(os.path.join(d, "*.md"))):
|
|
138
|
-
with open(p, encoding="utf-8", errors="replace") as f:
|
|
139
|
-
blobs.append(f.read())
|
|
140
|
-
return blobs
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
def _run_command(cmd, extra=None):
|
|
144
|
-
"""执行记录里的**原命令串**(切分 → python 换本解释器 → subprocess)。"""
|
|
145
|
-
argv = shlex.split(cmd, posix=False)
|
|
146
|
-
argv = [a[1:-1] if len(a) >= 2 and a[0] == a[-1] == '"' else a for a in argv]
|
|
147
|
-
if not argv:
|
|
148
|
-
return {"rc": -1, "json": None, "err": "empty command"}
|
|
149
|
-
argv[0] = sys.executable
|
|
150
|
-
if extra:
|
|
151
|
-
argv = argv + list(extra)
|
|
152
|
-
env = dict(os.environ)
|
|
153
|
-
env["PYTHONPATH"] = _REPO
|
|
154
|
-
env["PYTHONUTF8"] = "1"
|
|
155
|
-
env["PYTHONDONTWRITEBYTECODE"] = "1"
|
|
156
|
-
p = subprocess.run(argv, capture_output=True, text=True, encoding="utf-8",
|
|
157
|
-
errors="replace", cwd=_REPO, env=env, timeout=600)
|
|
158
|
-
try:
|
|
159
|
-
js = json.loads((p.stdout or "").strip().splitlines()[-1])
|
|
160
|
-
except Exception: # noqa: BLE001
|
|
161
|
-
js = None
|
|
162
|
-
return {"rc": p.returncode, "json": js, "err": (p.stderr or "")[-200:]}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
# ========================================================= A 组:漂移 / 保护 / 留痕
|
|
166
|
-
print("== A 组:执行之后的写入不得被回滚抹掉 ==")
|
|
167
|
-
|
|
168
|
-
# --- A1:C 改写单 accept 之后目标又被直写 ---
|
|
169
|
-
_a1 = _lib("a1")
|
|
170
|
-
_a1i, _a1o = _c_accept(_a1, "a1", "8081")
|
|
171
|
-
_a1pre = _a1i["mutation"].get("after_hash") if _a1i else None
|
|
172
|
-
_a1.add("a1", _PLAIN % "9090", layer="knowledge", verification_basis="test")
|
|
173
|
-
_a1r = _rb.rollback_mutation(_a1, _a1i["mutation"], reason="A1", pid=_a1i["pid"])
|
|
174
|
-
_a1c = (_a1.get("a1") or {}).get("content") or ""
|
|
175
|
-
print(" [读数] A1 出单=%s after_hash=%s 回滚=%s 正文末=%r"
|
|
176
|
-
% (bool(_a1i), bool(_a1pre), {k: _a1r.get(k) for k in ("ok", "error", "audited")},
|
|
177
|
-
_a1c.strip()[-24:]))
|
|
178
|
-
_check("A1 C 改写单 accept 后第三方再写:回滚 fail-closed "
|
|
179
|
-
"(drifted_since_execute)且后续写入仍在",
|
|
180
|
-
_a1r.get("ok") is False
|
|
181
|
-
and _a1r.get("error") == "drifted_since_execute"
|
|
182
|
-
and "9090" in _a1c, _a1r)
|
|
183
|
-
_a1.close()
|
|
184
|
-
|
|
185
|
-
# --- A2:B 合并单 accept 之后另有一张合并单并入同一目标 ---
|
|
186
|
-
_a2 = _lib("a2")
|
|
187
|
-
_a2.add("m_t", _BODY % "8081", layer="knowledge", verification_basis="test")
|
|
188
|
-
_a2.flush()
|
|
189
|
-
_r1 = _a2.remember_gated("m_n1", _BODY % "9595", layer="knowledge")
|
|
190
|
-
_d1 = _a2.review_decide(_r1["pid"], "accept", reason="A2-1")
|
|
191
|
-
_m1 = _d1.get("mutation") or {}
|
|
192
|
-
_r2 = _a2.remember_gated("m_n2", _BODY % "7777", layer="knowledge")
|
|
193
|
-
_d2 = _a2.review_decide(_r2["pid"], "accept", reason="A2-2")
|
|
194
|
-
_m2 = _d2.get("mutation") or {}
|
|
195
|
-
_a2r = _rb.rollback_mutation(_a2, _m1, reason="A2", pid=_r1["pid"])
|
|
196
|
-
_a2c = (_a2.get("m_t") or {}).get("content") or ""
|
|
197
|
-
print(" [读数] A2 第2单 accept=%s 回滚=%s 含7777=%s 聚合行=%d"
|
|
198
|
-
% (_d2.get("ok"), {k: _a2r.get(k) for k in ("ok", "error")},
|
|
199
|
-
"7777" in _a2c, _a2c.count(forgetting.AGG_MARK)))
|
|
200
|
-
_check("A2 B 合并单回滚:第 2 张合并单的聚合行仍在(fail-closed,不整体剥掉)",
|
|
201
|
-
_a2r.get("ok") is False
|
|
202
|
-
and _a2r.get("error") == "drifted_since_execute"
|
|
203
|
-
and "7777" in _a2c, _a2r)
|
|
204
|
-
_a2.close()
|
|
205
|
-
|
|
206
|
-
# --- A3:D 删除单 accept 之后同 id 被重新写入 ---
|
|
207
|
-
_a3 = _lib("a3")
|
|
208
|
-
_a3.add("d_1", _PLAIN % "初版", layer="knowledge", verification_basis="test")
|
|
209
|
-
_a3.flush()
|
|
210
|
-
_dg = _a3.forget_gated("d_1", reason="A3 删除")
|
|
211
|
-
_d3 = _a3.review_decide(_dg["pid"], "accept", reason="A3")
|
|
212
|
-
_md = _d3.get("mutation") or {}
|
|
213
|
-
_a3.add("d_1", _PLAIN % "重新登记版", layer="knowledge",
|
|
214
|
-
verification_basis="test")
|
|
215
|
-
_a3r = _rb.rollback_mutation(_a3, _md, reason="A3", pid=_dg["pid"])
|
|
216
|
-
_a3c = (_a3.get("d_1") or {}).get("content") or ""
|
|
217
|
-
print(" [读数] A3 回滚=%s 正文=%r"
|
|
218
|
-
% ({k: _a3r.get(k) for k in ("ok", "error")}, _a3c.strip()[-24:]))
|
|
219
|
-
_check("A3 D 删除单回滚:不覆写删除后同 id 的新写入(fail-closed)",
|
|
220
|
-
_a3r.get("ok") is False
|
|
221
|
-
and _a3r.get("error") == "drifted_since_execute"
|
|
222
|
-
and "重新登记版" in _a3c, _a3r)
|
|
223
|
-
_a3.close()
|
|
224
|
-
|
|
225
|
-
# --- A4a:执行之后目标被标 immutable(执行记录带 after_hash)---
|
|
226
|
-
_a4 = _lib("a4")
|
|
227
|
-
_a4i, _ = _c_accept(_a4, "i_1", "8081")
|
|
228
|
-
_a4.add("i_1", _PLAIN % "9090", layer="knowledge", verification_basis="test",
|
|
229
|
-
immutable=True, protection_reason="定稿锁定", override=True)
|
|
230
|
-
_a4r = _rb.rollback_mutation(_a4, _a4i["mutation"], reason="A4a", pid=_a4i["pid"])
|
|
231
|
-
_a4fm = (_a4.get("i_1") or {}).get("frontmatter") or {}
|
|
232
|
-
print(" [读数] A4a 回滚=%s immutable=%s"
|
|
233
|
-
% ({k: _a4r.get(k) for k in ("ok", "error")}, _a4fm.get("immutable")))
|
|
234
|
-
_check("A4a 执行后被标 immutable 的目标:回滚被拒(拒绝或不清标记,二者必有其一)",
|
|
235
|
-
_a4r.get("ok") is False and _a4fm.get("immutable") is True, _a4r)
|
|
236
|
-
_a4.close()
|
|
237
|
-
|
|
238
|
-
# --- A4b:**旧执行记录**(无 after_hash)+ 执行之后加 immutable 且正文不变 ---
|
|
239
|
-
# 这条只有 _land 的保护闸能拦(漂移闸拿不到后像、decisions 里也没有后续 accept)
|
|
240
|
-
_a5 = _lib("a5")
|
|
241
|
-
_a5i, _ = _c_accept(_a5, "i_2", "8081")
|
|
242
|
-
_cur = (_a5.get("i_2") or {}).get("content") or ""
|
|
243
|
-
_a5.add("i_2", _cur, layer="knowledge", verification_basis="test",
|
|
244
|
-
immutable=True, protection_reason="定稿锁定", override=True)
|
|
245
|
-
_legacy = {k: v for k, v in _a5i["mutation"].items() if k != "after_hash"}
|
|
246
|
-
_a5r = _rb.rollback_mutation(_a5, _legacy, reason="A4b", pid=_a5i["pid"])
|
|
247
|
-
_a5fm = (_a5.get("i_2") or {}).get("frontmatter") or {}
|
|
248
|
-
print(" [读数] A4b 旧记录回滚=%s immutable=%s"
|
|
249
|
-
% ({k: _a5r.get(k) for k in ("ok", "error", "protection")},
|
|
250
|
-
_a5fm.get("immutable")))
|
|
251
|
-
_check("A4b 旧执行记录(无 after_hash)+ 执行后加 immutable:保护闸拦下且标记仍在",
|
|
252
|
-
_a5r.get("ok") is False and _a5fm.get("immutable") is True
|
|
253
|
-
and "9090" not in str(_a5r), _a5r)
|
|
254
|
-
_a5.close()
|
|
255
|
-
|
|
256
|
-
# --- A5:回滚在 _audit.jsonl 留痕 ---
|
|
257
|
-
_a6 = _lib("a6")
|
|
258
|
-
_a6i, _ = _c_accept(_a6, "au_1", "8081")
|
|
259
|
-
_a6r = _rb.rollback_mutation(_a6, _a6i["mutation"], reason="A5 留痕",
|
|
260
|
-
pid=_a6i["pid"], actor="guard")
|
|
261
|
-
_rows = [r for r in _audit_rows(_a6)
|
|
262
|
-
if r.get("op") == "rollback" and r.get("id") == "au_1"]
|
|
263
|
-
print(" [读数] A5 回滚=%s 审计行=%s"
|
|
264
|
-
% (_a6r.get("ok"), [(r.get("op"), r.get("pid"), r.get("method"))
|
|
265
|
-
for r in _rows]))
|
|
266
|
-
_check("A5 回滚在 _audit.jsonl 留痕(op=rollback 且带 pid)",
|
|
267
|
-
_a6r.get("ok") is True and len(_rows) == 1
|
|
268
|
-
and _rows[0].get("pid") == _a6i["pid"], (_a6r.get("audited"), _rows))
|
|
269
|
-
_a6.close()
|
|
270
|
-
|
|
271
|
-
# --- A6:被覆盖的版本进 _protected_history(回滚前留档)---
|
|
272
|
-
_a7 = _lib("a7")
|
|
273
|
-
_a7i, _ = _c_accept(_a7, "h_1", "8081")
|
|
274
|
-
_a7r = _rb.rollback_mutation(_a7, _a7i["mutation"], reason="A6 留档")
|
|
275
|
-
_blobs = _history_blobs(_a7, "h_1")
|
|
276
|
-
print(" [读数] A6 历史文件=%d 含被覆盖版本(8081)=%s"
|
|
277
|
-
% (len(_blobs), any("8081" in b for b in _blobs)))
|
|
278
|
-
_check("A6 回滚前把**将被覆盖的版本**留档进 _protected_history(修前只留下前像)",
|
|
279
|
-
_a7r.get("ok") is True and len(_blobs) >= 2
|
|
280
|
-
and any("8081" in b for b in _blobs), len(_blobs))
|
|
281
|
-
_a7.close()
|
|
282
|
-
|
|
283
|
-
# --- A7:--dry-run 只预演不落盘 ---
|
|
284
|
-
_a8 = _lib("a8")
|
|
285
|
-
_a8i, _ = _c_accept(_a8, "dr_1", "8081")
|
|
286
|
-
_a8.add("dr_1", _PLAIN % "9090", layer="knowledge", verification_basis="test")
|
|
287
|
-
_a8d = _rb.rollback_mutation(_a8, _a8i["mutation"], reason="A7", pid=_a8i["pid"],
|
|
288
|
-
dry_run=True)
|
|
289
|
-
_a8c = (_a8.get("dr_1") or {}).get("content") or ""
|
|
290
|
-
_a8cli = _run_command(_a8i["mutation"].get("rollback") or "", ["--dry-run"])
|
|
291
|
-
print(" [读数] A7 dry-run=%s CLI rc=%s 正文仍含9090=%s"
|
|
292
|
-
% ({k: _a8d.get(k) for k in ("ok", "error", "would_apply")},
|
|
293
|
-
_a8cli["rc"], "9090" in _a8c))
|
|
294
|
-
_a8j = _a8cli["json"] or {}
|
|
295
|
-
# 注意:dry-run 遇到漂移时**照实回报「会拒绝」**(would_apply=False + 漂移原因),
|
|
296
|
-
# 不是「预演失败」——预演什么都没做,故 CLI 仍返回 rc=0。这正是它的用处:
|
|
297
|
-
# 执行回滚的人在动手前就能看见「这一单现在回滚会抹掉别的东西」。
|
|
298
|
-
_check("A7 dry-run 只回报差异与漂移判定、不落盘(进程内与 CLI 两路一致)",
|
|
299
|
-
_a8d.get("dry_run") is True and _a8d.get("would_apply") is False
|
|
300
|
-
and _a8d.get("error") == "drifted_since_execute"
|
|
301
|
-
and "9090" in _a8c
|
|
302
|
-
and _a8cli["rc"] == 0 and _a8j.get("dry_run") is True
|
|
303
|
-
and _a8j.get("would_apply") is False
|
|
304
|
-
and _a8j.get("error") == "drifted_since_execute"
|
|
305
|
-
and "9090" in _a8c, (_a8d, _a8j, _a8cli["rc"]))
|
|
306
|
-
_a8.close()
|
|
307
|
-
|
|
308
|
-
# --- A8:--force 显式越过闸并落盘 ---
|
|
309
|
-
_a9 = _lib("a9")
|
|
310
|
-
_a9i, _ = _c_accept(_a9, "fo_1", "8081")
|
|
311
|
-
_a9.add("fo_1", _PLAIN % "9090", layer="knowledge", verification_basis="test")
|
|
312
|
-
_a9cli = _run_command(_a9i["mutation"].get("rollback") or "", ["--force"])
|
|
313
|
-
_a9._maybe_reload_index()
|
|
314
|
-
_a9c = (_a9.get("fo_1") or {}).get("content") or ""
|
|
315
|
-
print(" [读数] A8 --force rc=%s ok=%s 正文回前像=%s audited=%s"
|
|
316
|
-
% (_a9cli["rc"], (_a9cli["json"] or {}).get("ok"), "8080" in _a9c,
|
|
317
|
-
(_a9cli["json"] or {}).get("audited")))
|
|
318
|
-
_check("A8 --force 越过漂移闸并落盘(闸不是硬墙:显式承担后果的通道存在)",
|
|
319
|
-
_a9cli["rc"] == 0 and (_a9cli["json"] or {}).get("ok") is True
|
|
320
|
-
and "8080" in _a9c, _a9cli)
|
|
321
|
-
_a9.close()
|
|
322
|
-
|
|
323
|
-
# ========================================================= K 组:非恒真对照
|
|
324
|
-
print("== K 组:非恒真对照(含**过度收紧**方向)==")
|
|
325
|
-
|
|
326
|
-
# --- K1:合法回滚(执行之后没人动过)照常成功 ---
|
|
327
|
-
_k1 = _lib("k1")
|
|
328
|
-
_k1i, _ = _c_accept(_k1, "k1", "8081")
|
|
329
|
-
_k1r = _rb.rollback_mutation(_k1, _k1i["mutation"], reason="K1")
|
|
330
|
-
_k1c = (_k1.get("k1") or {}).get("content") or ""
|
|
331
|
-
print(" [读数] K1 回滚=%s 逐字节=%s 正文回前像=%s"
|
|
332
|
-
% (_k1r.get("ok"), _k1r.get("bytes_equal_preimage"), "8080" in _k1c))
|
|
333
|
-
_check("K1 合法回滚(无第三方写入)照常 ok + 逐字节回前像(修复没把正常路径关掉)",
|
|
334
|
-
_k1r.get("ok") is True and _k1r.get("bytes_equal_preimage") is True
|
|
335
|
-
and "8080" in _k1c, _k1r)
|
|
336
|
-
_k1.close()
|
|
337
|
-
|
|
338
|
-
# --- K2:同一条回滚命令串二次演练(幂等重放)仍 rc=0 ---
|
|
339
|
-
_k2 = _lib("k2")
|
|
340
|
-
_k2i, _ = _c_accept(_k2, "k2", "8081")
|
|
341
|
-
_rb.rollback_mutation(_k2, _k2i["mutation"], reason="K2-1")
|
|
342
|
-
_k2cli = _run_command(_k2i["mutation"].get("rollback") or "")
|
|
343
|
-
print(" [读数] K2 二次演练 rc=%s ok=%s(幂等重放豁免)"
|
|
344
|
-
% (_k2cli["rc"], (_k2cli["json"] or {}).get("ok")))
|
|
345
|
-
_check("K2 命令串二次演练(幂等重放)仍 rc=0 ok=True——现盘面已等于前像即放行",
|
|
346
|
-
_k2cli["rc"] == 0 and (_k2cli["json"] or {}).get("ok") is True, _k2cli)
|
|
347
|
-
_k2.close()
|
|
348
|
-
|
|
349
|
-
# --- K3:(过度收紧方向)前像**本就** immutable 的节点,回滚必须放行 ---
|
|
350
|
-
def _hand_rollback(tag, layer, immutable):
|
|
351
|
-
cg = _lib(tag)
|
|
352
|
-
kw = {"layer": layer, "verification_basis": "test"}
|
|
353
|
-
if immutable:
|
|
354
|
-
kw.update(immutable=True, protection_reason="定稿锁定")
|
|
355
|
-
cg.add(tag, _PLAIN % "8080", **kw)
|
|
356
|
-
cg.flush()
|
|
357
|
-
rel = protect.snapshot_preimage(cg, tag, action="C", pid="p_" + tag)
|
|
358
|
-
cg.add(tag, _PLAIN % "8081", override=True, **kw)
|
|
359
|
-
m = {"action": "C", "target": tag, "before": rel,
|
|
360
|
-
"impact": _rb.collect_impact(cg, tag),
|
|
361
|
-
"after_hash": _rb.node_fingerprint(cg.get(tag)), "rollback": ""}
|
|
362
|
-
r = _rb.rollback_mutation(cg, m, reason="K3/K7", pid="p_" + tag)
|
|
363
|
-
c = (cg.get(tag) or {}).get("content") or ""
|
|
364
|
-
cg.close()
|
|
365
|
-
return r, c
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
_k3r, _k3c = _hand_rollback("k3", "knowledge", True)
|
|
369
|
-
print(" [读数] K3 前像本就 immutable:回滚=%s 正文回前像=%s"
|
|
370
|
-
% ({k: _k3r.get(k) for k in ("ok", "error")}, "8080" in _k3c))
|
|
371
|
-
_check("K3 前像本就带 immutable:回滚**必须放行**(保护闸只拦「执行之后才加的」)",
|
|
372
|
-
_k3r.get("ok") is True and "8080" in _k3c, _k3r)
|
|
373
|
-
|
|
374
|
-
# --- K4:旧执行记录(无 after_hash)且无后续执行 → 仍放行(向后兼容)---
|
|
375
|
-
_k4 = _lib("k4")
|
|
376
|
-
_k4i, _ = _c_accept(_k4, "k4", "8081")
|
|
377
|
-
_k4legacy = {k: v for k, v in _k4i["mutation"].items() if k != "after_hash"}
|
|
378
|
-
_k4r = _rb.rollback_mutation(_k4, _k4legacy, reason="K4", pid=_k4i["pid"])
|
|
379
|
-
_k4c = (_k4.get("k4") or {}).get("content") or ""
|
|
380
|
-
print(" [读数] K4 旧记录回滚=%s 正文回前像=%s"
|
|
381
|
-
% ({k: _k4r.get(k) for k in ("ok", "error")}, "8080" in _k4c))
|
|
382
|
-
_check("K4 旧执行记录(无 after_hash)+ 无后续执行:仍放行(不把老库存量单一律拒掉)",
|
|
383
|
-
_k4r.get("ok") is True and "8080" in _k4c, _k4r)
|
|
384
|
-
_k4.close()
|
|
385
|
-
|
|
386
|
-
# --- K5:执行之后只改元数据(正文不变)不算漂移 ---
|
|
387
|
-
_k5 = _lib("k5")
|
|
388
|
-
_k5i, _ = _c_accept(_k5, "k5", "8081")
|
|
389
|
-
_k5c0 = (_k5.get("k5") or {}).get("content") or ""
|
|
390
|
-
_k5.add("k5", _k5c0, layer="knowledge", verification_basis="test",
|
|
391
|
-
tags=["后加标签"])
|
|
392
|
-
_k5r = _rb.rollback_mutation(_k5, _k5i["mutation"], reason="K5", pid=_k5i["pid"])
|
|
393
|
-
_k5c = (_k5.get("k5") or {}).get("content") or ""
|
|
394
|
-
print(" [读数] K5 元数据改写后回滚=%s 正文回前像=%s"
|
|
395
|
-
% ({k: _k5r.get(k) for k in ("ok", "error")}, "8080" in _k5c))
|
|
396
|
-
_check("K5 执行之后只加 tags(正文不变)不算漂移:回滚仍放行(指纹不取每次落盘都变的键)",
|
|
397
|
-
_k5r.get("ok") is True and "8080" in _k5c, _k5r)
|
|
398
|
-
_k5.close()
|
|
399
|
-
|
|
400
|
-
# --- K6:dry-run 在**无漂移**场景也不落盘 ---
|
|
401
|
-
_k6 = _lib("k6")
|
|
402
|
-
_k6i, _ = _c_accept(_k6, "k6", "8081")
|
|
403
|
-
_k6d = _rb.rollback_mutation(_k6, _k6i["mutation"], reason="K6", dry_run=True)
|
|
404
|
-
_k6c = (_k6.get("k6") or {}).get("content") or ""
|
|
405
|
-
print(" [读数] K6 dry-run(无漂移)=%s 正文未动=%s"
|
|
406
|
-
% ({k: _k6d.get(k) for k in ("ok", "dry_run", "would_apply")},
|
|
407
|
-
"8081" in _k6c))
|
|
408
|
-
_check("K6 dry-run 在无漂移场景同样只预演:would_apply=True 且正文未动",
|
|
409
|
-
_k6d.get("ok") is True and _k6d.get("would_apply") is True
|
|
410
|
-
and "8081" in _k6c, _k6d)
|
|
411
|
-
_k6.close()
|
|
412
|
-
|
|
413
|
-
# --- K7:(过度收紧方向)前像就在保护层(self)的节点,回滚必须放行 ---
|
|
414
|
-
_k7r, _k7c = _hand_rollback("k7", "self", False)
|
|
415
|
-
print(" [读数] K7 前像本在 self 层:回滚=%s 正文回前像=%s"
|
|
416
|
-
% ({k: _k7r.get(k) for k in ("ok", "error")}, "8080" in _k7c))
|
|
417
|
-
_check("K7 前像本就在保护层(self):回滚**必须放行**(层保护不构成对合法回滚的硬墙)",
|
|
418
|
-
_k7r.get("ok") is True and "8080" in _k7c, _k7r)
|
|
419
|
-
|
|
420
|
-
# ========================================================= E 组:源码面单点
|
|
421
|
-
print("== E 组:源码面单点 ==")
|
|
422
|
-
_src_rb = inspect.getsource(_rb.rollback_mutation)
|
|
423
|
-
_src_land = inspect.getsource(_rb._land)
|
|
424
|
-
_src_me = inspect.getsource(autonomy_modes.mutation_executed)
|
|
425
|
-
_cli = os.path.join(_REPO, "md_cg", "rollback_cli.py")
|
|
426
|
-
_cli_src = open(_cli, encoding="utf-8").read()
|
|
427
|
-
_check("E1 漂移闸在统一入口内(drifted_since_execute + after_hash 比对)",
|
|
428
|
-
"drifted_since_execute" in _src_rb and 'mutation.get("after_hash")' in _src_rb
|
|
429
|
-
and "preimage_fingerprint" in _src_rb)
|
|
430
|
-
_check("E2 _land 接既有保护单点与留档单点(protect.is_immutable / protect.snapshot)",
|
|
431
|
-
"protect.is_immutable(" in _src_land and "protect.snapshot(" in _src_land)
|
|
432
|
-
_check("E3 mutation_executed 增加 after_hash 形参且仅在非 None 时补键",
|
|
433
|
-
"after_hash=None" in _src_me and 'if after_hash is not None:' in _src_me
|
|
434
|
-
and 'out["after_hash"] = after_hash' in _src_me)
|
|
435
|
-
_check("E4 CLI 暴露 --dry-run / --force 且转交统一入口",
|
|
436
|
-
'"--dry-run"' in _cli_src and '"--force"' in _cli_src
|
|
437
|
-
and "force=args.force" in _cli_src and "dry_run=args.dry_run" in _cli_src)
|
|
438
|
-
_mdcos_src = open(os.path.join(_REPO, "md_cg", "mdcos.py"),
|
|
439
|
-
encoding="utf-8").read()
|
|
440
|
-
_check("E5 执行桥用**同一个**指纹单点补后像(rollback.node_fingerprint)",
|
|
441
|
-
"_rb.node_fingerprint(" in _mdcos_src
|
|
442
|
-
and "after_hash=_after_hash" in _mdcos_src)
|
|
443
|
-
|
|
444
|
-
shutil.rmtree(_TMP, ignore_errors=True)
|
|
445
|
-
|
|
446
|
-
print("")
|
|
447
|
-
if FAILS:
|
|
448
|
-
print("issue #352-A 结果:%d 失败 → %s" % (len(FAILS), ", ".join(FAILS)))
|
|
449
|
-
else:
|
|
450
|
-
print("issue #352-A 结果:全部通过")
|
|
451
|
-
|
|
452
|
-
# ------------------------------------------------------------- 定点变异(镜像树)
|
|
453
|
-
_MUTATIONS = (
|
|
454
|
-
# (描述, ((文件, 旧锚点行组, 新锚点行组), ...), 预期红项集合)
|
|
455
|
-
("M1 摘掉漂移闸(回到「执行时点前像整体覆写」的修前形态)",
|
|
456
|
-
(("md_cg/rollback.py",
|
|
457
|
-
[" if cur_fp != pre_fp and not force: # 相等=幂等重放,放行(见 docstring)"],
|
|
458
|
-
[" if False:"]),),
|
|
459
|
-
# A4a 不在预期集里:执行之后加的 immutable 会**同时**被漂移闸与 _land 的
|
|
460
|
-
# 保护闸命中,摘掉漂移闸后仍被第二道拦下(M2 单独证明第二道的判别力)。
|
|
461
|
-
("A1", "A2", "A3", "A7")),
|
|
462
|
-
("M2 摘掉 _land 的保护闸(executed 之后加的 immutable 被一并清掉)",
|
|
463
|
-
(("md_cg/rollback.py",
|
|
464
|
-
[" if prot and not force:"],
|
|
465
|
-
[" if False:"]),),
|
|
466
|
-
("A4b",)),
|
|
467
|
-
("M3 摘掉回滚留痕(_audit.jsonl 不再有 op=rollback)",
|
|
468
|
-
(("md_cg/rollback.py",
|
|
469
|
-
[' out["audited"] = _audit_rollback(cg, tgt, act, rel, out, actor=actor,',
|
|
470
|
-
' pid=pid, reason=reason)'],
|
|
471
|
-
[' out["audited"] = False']),),
|
|
472
|
-
("A5",)),
|
|
473
|
-
("M4 摘掉落盘前的被覆盖版本留档(protect.snapshot)",
|
|
474
|
-
(("md_cg/rollback.py",
|
|
475
|
-
[" protect.snapshot(cg, node_id)"],
|
|
476
|
-
[" pass"]),),
|
|
477
|
-
("A6", "E2")),
|
|
478
|
-
("M5 摘掉 CLI 的 --force 接线(显式承担后果的通道消失)",
|
|
479
|
-
(("md_cg/rollback_cli.py",
|
|
480
|
-
[" force=args.force, dry_run=args.dry_run)"],
|
|
481
|
-
[" force=False, dry_run=args.dry_run)"]),),
|
|
482
|
-
("A8", "E4")),
|
|
483
|
-
("M6 摘掉执行桥的后像补全(回滚只剩 decisions 次序判据,直写类漂移看不见)",
|
|
484
|
-
(("md_cg/mdcos.py",
|
|
485
|
-
[" after_hash=_after_hash)"],
|
|
486
|
-
[" after_hash=None)"]),),
|
|
487
|
-
# A7 也在集里:dry-run 场景的漂移同样靠 after_hash 才看得见(直写不经 accept,
|
|
488
|
-
# decisions 次序判据拿不到证据)。
|
|
489
|
-
("A1", "A3", "A7", "E5")),
|
|
490
|
-
("M7 **过度收紧方向**:保护闸改成「现盘面受保护即拒」(不看前像是否本就带保护位)",
|
|
491
|
-
(("md_cg/rollback.py",
|
|
492
|
-
[" if not _pre_prot:"],
|
|
493
|
-
[" if True:"]),),
|
|
494
|
-
("K3", "K7")),
|
|
495
|
-
)
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
def _mirror_build():
|
|
499
|
-
dst = tempfile.mkdtemp(prefix="iss352a_mut_")
|
|
500
|
-
for name in ("md_cg", "scripts"):
|
|
501
|
-
src = os.path.join(_REPO, name)
|
|
502
|
-
if os.path.isdir(src):
|
|
503
|
-
shutil.copytree(src, os.path.join(dst, name),
|
|
504
|
-
ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
|
|
505
|
-
for name in sorted(os.listdir(_REPO)):
|
|
506
|
-
p = os.path.join(_REPO, name)
|
|
507
|
-
if os.path.isfile(p) and name.endswith(".py"):
|
|
508
|
-
shutil.copy2(p, os.path.join(dst, name))
|
|
509
|
-
return dst
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
def _nl_of(path):
|
|
513
|
-
with open(path, "rb") as fh:
|
|
514
|
-
raw = fh.read(8192)
|
|
515
|
-
return "\r\n" if b"\r\n" in raw else "\n"
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
def _anchor(parts, nl):
|
|
519
|
-
return nl.join(parts)
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
def _run_in(tree):
|
|
523
|
-
env = dict(os.environ)
|
|
524
|
-
env["PYTHONPATH"] = tree
|
|
525
|
-
env["PYTHONUTF8"] = "1"
|
|
526
|
-
env["PYTHONDONTWRITEBYTECODE"] = "1"
|
|
527
|
-
pr = subprocess.run([sys.executable, "-X", "utf8", "-m",
|
|
528
|
-
"md_cg.test_issue352_rollback"],
|
|
529
|
-
capture_output=True, text=True, encoding="utf-8",
|
|
530
|
-
errors="replace", cwd=tree, env=env, timeout=3600)
|
|
531
|
-
out = (pr.stdout or "") + (pr.stderr or "")
|
|
532
|
-
reds = [ln.split("FAIL ", 1)[1].strip()
|
|
533
|
-
for ln in out.splitlines() if "FAIL " in ln]
|
|
534
|
-
return pr.returncode, reds, out
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
def _mutate():
|
|
538
|
-
print("!! 定点变异自证:把修复点改回改动前形态 / 摘掉接线,守卫必须转红")
|
|
539
|
-
tree = _mirror_build()
|
|
540
|
-
try:
|
|
541
|
-
rc, reds, out = _run_in(tree)
|
|
542
|
-
print(" 未变异镜像基线:rc=%d 红项=%d(必须 rc=0 且 0 红)" % (rc, len(reds)))
|
|
543
|
-
if rc != 0 or reds:
|
|
544
|
-
print(chr(10).join(out.splitlines()[-30:]))
|
|
545
|
-
for ln in out.splitlines():
|
|
546
|
-
if "[读数]" in ln:
|
|
547
|
-
print(" 修后读数(未变异镜像):" + ln.strip())
|
|
548
|
-
okc, bad = 0, []
|
|
549
|
-
for desc, edits, exp in _MUTATIONS:
|
|
550
|
-
backups, miss = [], []
|
|
551
|
-
for rel, oldp, newp in edits:
|
|
552
|
-
p = os.path.join(tree, rel)
|
|
553
|
-
nl = _nl_of(p)
|
|
554
|
-
old, new = _anchor(oldp, nl), _anchor(newp, nl)
|
|
555
|
-
with open(p, encoding="utf-8", newline="") as fh:
|
|
556
|
-
t = fh.read()
|
|
557
|
-
if t.count(old) != 1:
|
|
558
|
-
miss.append("%s —— 锚点命中 %d 次" % (rel, t.count(old)))
|
|
559
|
-
continue
|
|
560
|
-
backups.append((p, t))
|
|
561
|
-
with open(p, "w", encoding="utf-8", newline="") as fh:
|
|
562
|
-
fh.write(t.replace(old, new, 1))
|
|
563
|
-
if miss:
|
|
564
|
-
bad.append("%s —— %s" % (desc, ";".join(miss)))
|
|
565
|
-
print(" ANCHOR-MISS " + desc)
|
|
566
|
-
for p, t in backups:
|
|
567
|
-
with open(p, "w", encoding="utf-8", newline="") as fh:
|
|
568
|
-
fh.write(t)
|
|
569
|
-
continue
|
|
570
|
-
rc2, reds2, out2 = _run_in(tree)
|
|
571
|
-
for p, t in backups:
|
|
572
|
-
with open(p, "w", encoding="utf-8", newline="") as fh:
|
|
573
|
-
fh.write(t)
|
|
574
|
-
hit = sorted({e for r in reds2 for e in exp if r.startswith(e + " ")})
|
|
575
|
-
if rc2 != 0 and hit == sorted(exp) and len(reds2) == len(exp):
|
|
576
|
-
okc += 1
|
|
577
|
-
print(" OK %s 红项=%d 指定命中=%d/%d"
|
|
578
|
-
% (desc, len(reds2), len(hit), len(exp)))
|
|
579
|
-
else:
|
|
580
|
-
bad.append(desc)
|
|
581
|
-
print(" MISMATCH %s 红项=%d 指定命中=%d/%d rc=%d"
|
|
582
|
-
% (desc, len(reds2), len(hit), len(exp), rc2))
|
|
583
|
-
for r in reds2[:14]:
|
|
584
|
-
print(" FAIL " + r)
|
|
585
|
-
for ln in out2.splitlines():
|
|
586
|
-
if "[读数]" in ln and "基线" not in ln:
|
|
587
|
-
pass
|
|
588
|
-
print("定点变异自证:%s(%d 条不合格)"
|
|
589
|
-
% ("PASS" if not bad else "FAIL", len(bad)))
|
|
590
|
-
for b in bad:
|
|
591
|
-
print(" 不合格:" + b)
|
|
592
|
-
finally:
|
|
593
|
-
shutil.rmtree(tree, ignore_errors=True)
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
def main():
|
|
597
|
-
if "--mutate" in sys.argv:
|
|
598
|
-
_mutate()
|
|
599
|
-
return 1 if FAILS else 0
|
|
600
|
-
return 0 if not FAILS else 1
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
if __name__ == "__main__":
|
|
604
|
-
sys.exit(main())
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# 功能名:变更单回滚不得抹掉执行之后的写入、不得越过保护位、必须留痕(issue #352-A)
|
|
3
|
+
# 生效条件:python -X utf8 -m md_cg.test_issue352_rollback [--mutate];被测面=md_cg/rollback.py 的 rollback_mutation/_land + md_cg/rollback_cli.py + md_cg/mdcos.py 的执行时点后像补全
|
|
4
|
+
# 子功能:A 组=漂移闸(执行之后又被写 / B 第二张合并单 / D 删除后同 id 重建 / 执行后加 immutable)+ 回滚留痕 + 被覆盖版本留档 + --dry-run/--force;K 组=非恒真对照(合法回滚照常 / 命令串幂等重放 / 旧记录无 after_hash 仍放行 / 元数据改写不算漂移 / 前像本就受保护时不得拦死);E 组=源码面单点
|
|
5
|
+
# 执行:confirm 档出 C/B/D 三类变更单并 accept,执行后按场景追加写入,再调统一回滚入口或执行记录里的回滚命令串(subprocess)
|
|
6
|
+
# 验证方式:全部 PASS 即通过;--mutate 走镜像树定点变异,变异后红项必须**恰为**预期集合
|
|
7
|
+
# 不适用条件:①只判「回滚是否越权/抹掉执行之后的写入」,不判前像本身是否拍对(既有 test_mutation_rollback P/Q/R/S 组覆盖)②不判加密库的密文字节可比性(既有模块 docstring 已声明口径)③不判 D 的 restore 语义本身④不改 protect.py 既有 guard_*/snapshot 行为(守卫钉死)
|
|
8
|
+
"""issue #352-A 守卫:回滚越权(抹掉执行之后的写入 / 越 immutable / 不留痕)。
|
|
9
|
+
|
|
10
|
+
病象(修前):`rollback_mutation` 用**执行时点前像整体覆写**目标,不检查目标在
|
|
11
|
+
执行之后有没有再被写过——
|
|
12
|
+
· C 改写单:执行之后别人写进去的正文全部消失;
|
|
13
|
+
· B 合并单:当前正文以前像为前缀时,前缀之后的**所有**聚合行被剥掉,包括后续
|
|
14
|
+
另一张合并单追加的行;剥不精确时退回整份前像正文;
|
|
15
|
+
· D 删除单:删除之后同 id 新写入的内容被前像校准覆写;
|
|
16
|
+
· 执行之后目标被标 immutable 时照样覆写,immutable 标记被一并清掉;
|
|
17
|
+
· 被覆盖的版本不进 `_protected_history`、`_audit.jsonl` 里也没有回滚这一行。
|
|
18
|
+
回执是 `ok:true / bytes_equal_preimage:true`,执行回滚的人看不到任何提示。
|
|
19
|
+
|
|
20
|
+
处置(三条,缺一不可):
|
|
21
|
+
1. **漂移闸**:执行桥在执行落盘**之后**补一个盘面指纹 `after_hash`
|
|
22
|
+
(`rollback.node_fingerprint`),回滚前与现盘面比对;不等即 fail-closed
|
|
23
|
+
(`drifted_since_execute`)。旧记录无该键 ⇒ 退化为 decisions.jsonl 次序
|
|
24
|
+
判据,**不是「无键即放行」**。**幂等重放豁免**:现盘面已等于前像时放行
|
|
25
|
+
(此时覆写是空操作,抹不掉东西)。
|
|
26
|
+
2. **保护闸 + 留档**:`_land` 落盘前过既有单点 `protect.is_immutable`,落盘前
|
|
27
|
+
调 `protect.snapshot` 把将被覆盖的版本留进 `_protected_history`。
|
|
28
|
+
判据刻意**只在「现受保护 而 前像不带同一保护位」时拒**——否则受保护节点的
|
|
29
|
+
合法回滚会被一并拦死(K3/K7 是这条的对照)。
|
|
30
|
+
3. **留痕**:回滚成功即向 `_audit.jsonl` 写一行 `op="rollback"`(改前这一支
|
|
31
|
+
一行审计都不写)。
|
|
32
|
+
|
|
33
|
+
★**过度收紧方向**:本条的修复天然是「加拒绝」,故 K 组里 K3/K7 专门盯
|
|
34
|
+
「前像本就带保护位时**必须放行**」、K4 盯「旧记录不能一律拒」、K5 盯「元数据
|
|
35
|
+
改写不算漂移」;M7 是**过度收紧方向的定点变异**(把保护闸改成「现受保护即拒」),
|
|
36
|
+
它必须打红 K3/K7——证明这两条判据有判别力,不是恒真对照。
|
|
37
|
+
"""
|
|
38
|
+
import inspect
|
|
39
|
+
import json
|
|
40
|
+
import os
|
|
41
|
+
import shlex
|
|
42
|
+
import shutil
|
|
43
|
+
import subprocess
|
|
44
|
+
import sys
|
|
45
|
+
import tempfile
|
|
46
|
+
|
|
47
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
48
|
+
|
|
49
|
+
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
50
|
+
sys.path.insert(0, _REPO)
|
|
51
|
+
_TMP = tempfile.mkdtemp(prefix="iss352a_")
|
|
52
|
+
_POLICY = os.path.join(_TMP, "policy.json")
|
|
53
|
+
with open(_POLICY, "w", encoding="utf-8") as _f:
|
|
54
|
+
json.dump({"forbidden": ["FORBIDDEN_WORD"], "required": ["PASSED"]}, _f)
|
|
55
|
+
os.environ.update({"MDCG_ROOT": _TMP + "/brainroot",
|
|
56
|
+
"MDCG_STATE_ROOT": _TMP + "/state",
|
|
57
|
+
"MDCG_AUX_ROOT": _TMP + "/auxstore",
|
|
58
|
+
"MDCG_DATA_ROOT": _TMP + "/datastore",
|
|
59
|
+
"MDCG_EXPORT_ROOT": _TMP,
|
|
60
|
+
"MDCG_POLICY_FILE": _POLICY,
|
|
61
|
+
"MDCG_SUSTAIN": "0", "MDCG_SLEEP": "0"})
|
|
62
|
+
|
|
63
|
+
from md_cg import autonomy_modes # noqa: E402
|
|
64
|
+
|
|
65
|
+
os.environ[autonomy_modes.AUTONOMY_ENV_KEYS["mode"]] = "confirm"
|
|
66
|
+
|
|
67
|
+
from md_cg import forgetting, protect, rollback as _rb # noqa: E402
|
|
68
|
+
from md_cg import writepipe # noqa: E402
|
|
69
|
+
from md_cg.mdcos import MdCGOS # noqa: E402
|
|
70
|
+
|
|
71
|
+
FAILS = []
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _check(name, cond, detail=""):
|
|
75
|
+
if cond:
|
|
76
|
+
print(" PASS " + name)
|
|
77
|
+
else:
|
|
78
|
+
print(" FAIL " + name + " " + str(detail)[:400])
|
|
79
|
+
FAILS.append(name)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
_PLAIN = "PASSED 端口 %s"
|
|
83
|
+
_BODY = ("# 功能名:部署面端口约定\n"
|
|
84
|
+
"# 生效条件:prod 集群;2026-09-30 起;部署清单核对\n"
|
|
85
|
+
"# 子功能:登记各服务监听端口\n"
|
|
86
|
+
"# 执行:核对 manifest 的 ports 段\n"
|
|
87
|
+
"# 验证方式:test\n"
|
|
88
|
+
"# 不适用条件:无\n"
|
|
89
|
+
"\n网关监听端口 8080/HTTP;指标面监听端口 %s/HTTP\n")
|
|
90
|
+
|
|
91
|
+
_RUN = [0]
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _lib(tag):
|
|
95
|
+
_RUN[0] += 1
|
|
96
|
+
return MdCGOS(os.path.join(_TMP, "%s_%d" % (tag, _RUN[0])), autoflush=0)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _pipe():
|
|
100
|
+
return writepipe.install_default_pipes() if hasattr(writepipe, "install_default_pipes") \
|
|
101
|
+
else writepipe.install_default_gates(writepipe.WritePipeline())
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _c_accept(cg, nid, new, old="8080"):
|
|
105
|
+
"""出一张 C 改写单并 accept;返回 ({"pid","mutation"}, 写链返回体)。"""
|
|
106
|
+
cg.add(nid, _PLAIN % old, layer="knowledge", verification_basis="test")
|
|
107
|
+
cg.flush()
|
|
108
|
+
o = _pipe().execute(cg, {"content_kind": "text", "content": _PLAIN % new,
|
|
109
|
+
"layer": "knowledge", "node_id": nid})
|
|
110
|
+
if o.get("moved_to") != "review_queue" or not o.get("pid"):
|
|
111
|
+
return None, o
|
|
112
|
+
d = cg.review_decide(o["pid"], "accept", reason="issue352 守卫")
|
|
113
|
+
return {"pid": o["pid"], "mutation": d.get("mutation") or {},
|
|
114
|
+
"accept": d}, o
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _audit_rows(cg):
|
|
118
|
+
p = os.path.join(cg.root, "_audit.jsonl")
|
|
119
|
+
if not os.path.isfile(p):
|
|
120
|
+
return []
|
|
121
|
+
out = []
|
|
122
|
+
with open(p, encoding="utf-8", errors="replace") as f:
|
|
123
|
+
for ln in f:
|
|
124
|
+
ln = ln.strip()
|
|
125
|
+
if ln:
|
|
126
|
+
try:
|
|
127
|
+
out.append(json.loads(ln))
|
|
128
|
+
except ValueError:
|
|
129
|
+
pass
|
|
130
|
+
return out
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _history_blobs(cg, nid):
|
|
134
|
+
import glob
|
|
135
|
+
d = os.path.join(cg.root, protect.HISTORY_DIR, nid)
|
|
136
|
+
blobs = []
|
|
137
|
+
for p in sorted(glob.glob(os.path.join(d, "*.md"))):
|
|
138
|
+
with open(p, encoding="utf-8", errors="replace") as f:
|
|
139
|
+
blobs.append(f.read())
|
|
140
|
+
return blobs
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _run_command(cmd, extra=None):
|
|
144
|
+
"""执行记录里的**原命令串**(切分 → python 换本解释器 → subprocess)。"""
|
|
145
|
+
argv = shlex.split(cmd, posix=False)
|
|
146
|
+
argv = [a[1:-1] if len(a) >= 2 and a[0] == a[-1] == '"' else a for a in argv]
|
|
147
|
+
if not argv:
|
|
148
|
+
return {"rc": -1, "json": None, "err": "empty command"}
|
|
149
|
+
argv[0] = sys.executable
|
|
150
|
+
if extra:
|
|
151
|
+
argv = argv + list(extra)
|
|
152
|
+
env = dict(os.environ)
|
|
153
|
+
env["PYTHONPATH"] = _REPO
|
|
154
|
+
env["PYTHONUTF8"] = "1"
|
|
155
|
+
env["PYTHONDONTWRITEBYTECODE"] = "1"
|
|
156
|
+
p = subprocess.run(argv, capture_output=True, text=True, encoding="utf-8",
|
|
157
|
+
errors="replace", cwd=_REPO, env=env, timeout=600)
|
|
158
|
+
try:
|
|
159
|
+
js = json.loads((p.stdout or "").strip().splitlines()[-1])
|
|
160
|
+
except Exception: # noqa: BLE001
|
|
161
|
+
js = None
|
|
162
|
+
return {"rc": p.returncode, "json": js, "err": (p.stderr or "")[-200:]}
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
# ========================================================= A 组:漂移 / 保护 / 留痕
|
|
166
|
+
print("== A 组:执行之后的写入不得被回滚抹掉 ==")
|
|
167
|
+
|
|
168
|
+
# --- A1:C 改写单 accept 之后目标又被直写 ---
|
|
169
|
+
_a1 = _lib("a1")
|
|
170
|
+
_a1i, _a1o = _c_accept(_a1, "a1", "8081")
|
|
171
|
+
_a1pre = _a1i["mutation"].get("after_hash") if _a1i else None
|
|
172
|
+
_a1.add("a1", _PLAIN % "9090", layer="knowledge", verification_basis="test")
|
|
173
|
+
_a1r = _rb.rollback_mutation(_a1, _a1i["mutation"], reason="A1", pid=_a1i["pid"])
|
|
174
|
+
_a1c = (_a1.get("a1") or {}).get("content") or ""
|
|
175
|
+
print(" [读数] A1 出单=%s after_hash=%s 回滚=%s 正文末=%r"
|
|
176
|
+
% (bool(_a1i), bool(_a1pre), {k: _a1r.get(k) for k in ("ok", "error", "audited")},
|
|
177
|
+
_a1c.strip()[-24:]))
|
|
178
|
+
_check("A1 C 改写单 accept 后第三方再写:回滚 fail-closed "
|
|
179
|
+
"(drifted_since_execute)且后续写入仍在",
|
|
180
|
+
_a1r.get("ok") is False
|
|
181
|
+
and _a1r.get("error") == "drifted_since_execute"
|
|
182
|
+
and "9090" in _a1c, _a1r)
|
|
183
|
+
_a1.close()
|
|
184
|
+
|
|
185
|
+
# --- A2:B 合并单 accept 之后另有一张合并单并入同一目标 ---
|
|
186
|
+
_a2 = _lib("a2")
|
|
187
|
+
_a2.add("m_t", _BODY % "8081", layer="knowledge", verification_basis="test")
|
|
188
|
+
_a2.flush()
|
|
189
|
+
_r1 = _a2.remember_gated("m_n1", _BODY % "9595", layer="knowledge")
|
|
190
|
+
_d1 = _a2.review_decide(_r1["pid"], "accept", reason="A2-1")
|
|
191
|
+
_m1 = _d1.get("mutation") or {}
|
|
192
|
+
_r2 = _a2.remember_gated("m_n2", _BODY % "7777", layer="knowledge")
|
|
193
|
+
_d2 = _a2.review_decide(_r2["pid"], "accept", reason="A2-2")
|
|
194
|
+
_m2 = _d2.get("mutation") or {}
|
|
195
|
+
_a2r = _rb.rollback_mutation(_a2, _m1, reason="A2", pid=_r1["pid"])
|
|
196
|
+
_a2c = (_a2.get("m_t") or {}).get("content") or ""
|
|
197
|
+
print(" [读数] A2 第2单 accept=%s 回滚=%s 含7777=%s 聚合行=%d"
|
|
198
|
+
% (_d2.get("ok"), {k: _a2r.get(k) for k in ("ok", "error")},
|
|
199
|
+
"7777" in _a2c, _a2c.count(forgetting.AGG_MARK)))
|
|
200
|
+
_check("A2 B 合并单回滚:第 2 张合并单的聚合行仍在(fail-closed,不整体剥掉)",
|
|
201
|
+
_a2r.get("ok") is False
|
|
202
|
+
and _a2r.get("error") == "drifted_since_execute"
|
|
203
|
+
and "7777" in _a2c, _a2r)
|
|
204
|
+
_a2.close()
|
|
205
|
+
|
|
206
|
+
# --- A3:D 删除单 accept 之后同 id 被重新写入 ---
|
|
207
|
+
_a3 = _lib("a3")
|
|
208
|
+
_a3.add("d_1", _PLAIN % "初版", layer="knowledge", verification_basis="test")
|
|
209
|
+
_a3.flush()
|
|
210
|
+
_dg = _a3.forget_gated("d_1", reason="A3 删除")
|
|
211
|
+
_d3 = _a3.review_decide(_dg["pid"], "accept", reason="A3")
|
|
212
|
+
_md = _d3.get("mutation") or {}
|
|
213
|
+
_a3.add("d_1", _PLAIN % "重新登记版", layer="knowledge",
|
|
214
|
+
verification_basis="test")
|
|
215
|
+
_a3r = _rb.rollback_mutation(_a3, _md, reason="A3", pid=_dg["pid"])
|
|
216
|
+
_a3c = (_a3.get("d_1") or {}).get("content") or ""
|
|
217
|
+
print(" [读数] A3 回滚=%s 正文=%r"
|
|
218
|
+
% ({k: _a3r.get(k) for k in ("ok", "error")}, _a3c.strip()[-24:]))
|
|
219
|
+
_check("A3 D 删除单回滚:不覆写删除后同 id 的新写入(fail-closed)",
|
|
220
|
+
_a3r.get("ok") is False
|
|
221
|
+
and _a3r.get("error") == "drifted_since_execute"
|
|
222
|
+
and "重新登记版" in _a3c, _a3r)
|
|
223
|
+
_a3.close()
|
|
224
|
+
|
|
225
|
+
# --- A4a:执行之后目标被标 immutable(执行记录带 after_hash)---
|
|
226
|
+
_a4 = _lib("a4")
|
|
227
|
+
_a4i, _ = _c_accept(_a4, "i_1", "8081")
|
|
228
|
+
_a4.add("i_1", _PLAIN % "9090", layer="knowledge", verification_basis="test",
|
|
229
|
+
immutable=True, protection_reason="定稿锁定", override=True)
|
|
230
|
+
_a4r = _rb.rollback_mutation(_a4, _a4i["mutation"], reason="A4a", pid=_a4i["pid"])
|
|
231
|
+
_a4fm = (_a4.get("i_1") or {}).get("frontmatter") or {}
|
|
232
|
+
print(" [读数] A4a 回滚=%s immutable=%s"
|
|
233
|
+
% ({k: _a4r.get(k) for k in ("ok", "error")}, _a4fm.get("immutable")))
|
|
234
|
+
_check("A4a 执行后被标 immutable 的目标:回滚被拒(拒绝或不清标记,二者必有其一)",
|
|
235
|
+
_a4r.get("ok") is False and _a4fm.get("immutable") is True, _a4r)
|
|
236
|
+
_a4.close()
|
|
237
|
+
|
|
238
|
+
# --- A4b:**旧执行记录**(无 after_hash)+ 执行之后加 immutable 且正文不变 ---
|
|
239
|
+
# 这条只有 _land 的保护闸能拦(漂移闸拿不到后像、decisions 里也没有后续 accept)
|
|
240
|
+
_a5 = _lib("a5")
|
|
241
|
+
_a5i, _ = _c_accept(_a5, "i_2", "8081")
|
|
242
|
+
_cur = (_a5.get("i_2") or {}).get("content") or ""
|
|
243
|
+
_a5.add("i_2", _cur, layer="knowledge", verification_basis="test",
|
|
244
|
+
immutable=True, protection_reason="定稿锁定", override=True)
|
|
245
|
+
_legacy = {k: v for k, v in _a5i["mutation"].items() if k != "after_hash"}
|
|
246
|
+
_a5r = _rb.rollback_mutation(_a5, _legacy, reason="A4b", pid=_a5i["pid"])
|
|
247
|
+
_a5fm = (_a5.get("i_2") or {}).get("frontmatter") or {}
|
|
248
|
+
print(" [读数] A4b 旧记录回滚=%s immutable=%s"
|
|
249
|
+
% ({k: _a5r.get(k) for k in ("ok", "error", "protection")},
|
|
250
|
+
_a5fm.get("immutable")))
|
|
251
|
+
_check("A4b 旧执行记录(无 after_hash)+ 执行后加 immutable:保护闸拦下且标记仍在",
|
|
252
|
+
_a5r.get("ok") is False and _a5fm.get("immutable") is True
|
|
253
|
+
and "9090" not in str(_a5r), _a5r)
|
|
254
|
+
_a5.close()
|
|
255
|
+
|
|
256
|
+
# --- A5:回滚在 _audit.jsonl 留痕 ---
|
|
257
|
+
_a6 = _lib("a6")
|
|
258
|
+
_a6i, _ = _c_accept(_a6, "au_1", "8081")
|
|
259
|
+
_a6r = _rb.rollback_mutation(_a6, _a6i["mutation"], reason="A5 留痕",
|
|
260
|
+
pid=_a6i["pid"], actor="guard")
|
|
261
|
+
_rows = [r for r in _audit_rows(_a6)
|
|
262
|
+
if r.get("op") == "rollback" and r.get("id") == "au_1"]
|
|
263
|
+
print(" [读数] A5 回滚=%s 审计行=%s"
|
|
264
|
+
% (_a6r.get("ok"), [(r.get("op"), r.get("pid"), r.get("method"))
|
|
265
|
+
for r in _rows]))
|
|
266
|
+
_check("A5 回滚在 _audit.jsonl 留痕(op=rollback 且带 pid)",
|
|
267
|
+
_a6r.get("ok") is True and len(_rows) == 1
|
|
268
|
+
and _rows[0].get("pid") == _a6i["pid"], (_a6r.get("audited"), _rows))
|
|
269
|
+
_a6.close()
|
|
270
|
+
|
|
271
|
+
# --- A6:被覆盖的版本进 _protected_history(回滚前留档)---
|
|
272
|
+
_a7 = _lib("a7")
|
|
273
|
+
_a7i, _ = _c_accept(_a7, "h_1", "8081")
|
|
274
|
+
_a7r = _rb.rollback_mutation(_a7, _a7i["mutation"], reason="A6 留档")
|
|
275
|
+
_blobs = _history_blobs(_a7, "h_1")
|
|
276
|
+
print(" [读数] A6 历史文件=%d 含被覆盖版本(8081)=%s"
|
|
277
|
+
% (len(_blobs), any("8081" in b for b in _blobs)))
|
|
278
|
+
_check("A6 回滚前把**将被覆盖的版本**留档进 _protected_history(修前只留下前像)",
|
|
279
|
+
_a7r.get("ok") is True and len(_blobs) >= 2
|
|
280
|
+
and any("8081" in b for b in _blobs), len(_blobs))
|
|
281
|
+
_a7.close()
|
|
282
|
+
|
|
283
|
+
# --- A7:--dry-run 只预演不落盘 ---
|
|
284
|
+
_a8 = _lib("a8")
|
|
285
|
+
_a8i, _ = _c_accept(_a8, "dr_1", "8081")
|
|
286
|
+
_a8.add("dr_1", _PLAIN % "9090", layer="knowledge", verification_basis="test")
|
|
287
|
+
_a8d = _rb.rollback_mutation(_a8, _a8i["mutation"], reason="A7", pid=_a8i["pid"],
|
|
288
|
+
dry_run=True)
|
|
289
|
+
_a8c = (_a8.get("dr_1") or {}).get("content") or ""
|
|
290
|
+
_a8cli = _run_command(_a8i["mutation"].get("rollback") or "", ["--dry-run"])
|
|
291
|
+
print(" [读数] A7 dry-run=%s CLI rc=%s 正文仍含9090=%s"
|
|
292
|
+
% ({k: _a8d.get(k) for k in ("ok", "error", "would_apply")},
|
|
293
|
+
_a8cli["rc"], "9090" in _a8c))
|
|
294
|
+
_a8j = _a8cli["json"] or {}
|
|
295
|
+
# 注意:dry-run 遇到漂移时**照实回报「会拒绝」**(would_apply=False + 漂移原因),
|
|
296
|
+
# 不是「预演失败」——预演什么都没做,故 CLI 仍返回 rc=0。这正是它的用处:
|
|
297
|
+
# 执行回滚的人在动手前就能看见「这一单现在回滚会抹掉别的东西」。
|
|
298
|
+
_check("A7 dry-run 只回报差异与漂移判定、不落盘(进程内与 CLI 两路一致)",
|
|
299
|
+
_a8d.get("dry_run") is True and _a8d.get("would_apply") is False
|
|
300
|
+
and _a8d.get("error") == "drifted_since_execute"
|
|
301
|
+
and "9090" in _a8c
|
|
302
|
+
and _a8cli["rc"] == 0 and _a8j.get("dry_run") is True
|
|
303
|
+
and _a8j.get("would_apply") is False
|
|
304
|
+
and _a8j.get("error") == "drifted_since_execute"
|
|
305
|
+
and "9090" in _a8c, (_a8d, _a8j, _a8cli["rc"]))
|
|
306
|
+
_a8.close()
|
|
307
|
+
|
|
308
|
+
# --- A8:--force 显式越过闸并落盘 ---
|
|
309
|
+
_a9 = _lib("a9")
|
|
310
|
+
_a9i, _ = _c_accept(_a9, "fo_1", "8081")
|
|
311
|
+
_a9.add("fo_1", _PLAIN % "9090", layer="knowledge", verification_basis="test")
|
|
312
|
+
_a9cli = _run_command(_a9i["mutation"].get("rollback") or "", ["--force"])
|
|
313
|
+
_a9._maybe_reload_index()
|
|
314
|
+
_a9c = (_a9.get("fo_1") or {}).get("content") or ""
|
|
315
|
+
print(" [读数] A8 --force rc=%s ok=%s 正文回前像=%s audited=%s"
|
|
316
|
+
% (_a9cli["rc"], (_a9cli["json"] or {}).get("ok"), "8080" in _a9c,
|
|
317
|
+
(_a9cli["json"] or {}).get("audited")))
|
|
318
|
+
_check("A8 --force 越过漂移闸并落盘(闸不是硬墙:显式承担后果的通道存在)",
|
|
319
|
+
_a9cli["rc"] == 0 and (_a9cli["json"] or {}).get("ok") is True
|
|
320
|
+
and "8080" in _a9c, _a9cli)
|
|
321
|
+
_a9.close()
|
|
322
|
+
|
|
323
|
+
# ========================================================= K 组:非恒真对照
|
|
324
|
+
print("== K 组:非恒真对照(含**过度收紧**方向)==")
|
|
325
|
+
|
|
326
|
+
# --- K1:合法回滚(执行之后没人动过)照常成功 ---
|
|
327
|
+
_k1 = _lib("k1")
|
|
328
|
+
_k1i, _ = _c_accept(_k1, "k1", "8081")
|
|
329
|
+
_k1r = _rb.rollback_mutation(_k1, _k1i["mutation"], reason="K1")
|
|
330
|
+
_k1c = (_k1.get("k1") or {}).get("content") or ""
|
|
331
|
+
print(" [读数] K1 回滚=%s 逐字节=%s 正文回前像=%s"
|
|
332
|
+
% (_k1r.get("ok"), _k1r.get("bytes_equal_preimage"), "8080" in _k1c))
|
|
333
|
+
_check("K1 合法回滚(无第三方写入)照常 ok + 逐字节回前像(修复没把正常路径关掉)",
|
|
334
|
+
_k1r.get("ok") is True and _k1r.get("bytes_equal_preimage") is True
|
|
335
|
+
and "8080" in _k1c, _k1r)
|
|
336
|
+
_k1.close()
|
|
337
|
+
|
|
338
|
+
# --- K2:同一条回滚命令串二次演练(幂等重放)仍 rc=0 ---
|
|
339
|
+
_k2 = _lib("k2")
|
|
340
|
+
_k2i, _ = _c_accept(_k2, "k2", "8081")
|
|
341
|
+
_rb.rollback_mutation(_k2, _k2i["mutation"], reason="K2-1")
|
|
342
|
+
_k2cli = _run_command(_k2i["mutation"].get("rollback") or "")
|
|
343
|
+
print(" [读数] K2 二次演练 rc=%s ok=%s(幂等重放豁免)"
|
|
344
|
+
% (_k2cli["rc"], (_k2cli["json"] or {}).get("ok")))
|
|
345
|
+
_check("K2 命令串二次演练(幂等重放)仍 rc=0 ok=True——现盘面已等于前像即放行",
|
|
346
|
+
_k2cli["rc"] == 0 and (_k2cli["json"] or {}).get("ok") is True, _k2cli)
|
|
347
|
+
_k2.close()
|
|
348
|
+
|
|
349
|
+
# --- K3:(过度收紧方向)前像**本就** immutable 的节点,回滚必须放行 ---
|
|
350
|
+
def _hand_rollback(tag, layer, immutable):
|
|
351
|
+
cg = _lib(tag)
|
|
352
|
+
kw = {"layer": layer, "verification_basis": "test"}
|
|
353
|
+
if immutable:
|
|
354
|
+
kw.update(immutable=True, protection_reason="定稿锁定")
|
|
355
|
+
cg.add(tag, _PLAIN % "8080", **kw)
|
|
356
|
+
cg.flush()
|
|
357
|
+
rel = protect.snapshot_preimage(cg, tag, action="C", pid="p_" + tag)
|
|
358
|
+
cg.add(tag, _PLAIN % "8081", override=True, **kw)
|
|
359
|
+
m = {"action": "C", "target": tag, "before": rel,
|
|
360
|
+
"impact": _rb.collect_impact(cg, tag),
|
|
361
|
+
"after_hash": _rb.node_fingerprint(cg.get(tag)), "rollback": ""}
|
|
362
|
+
r = _rb.rollback_mutation(cg, m, reason="K3/K7", pid="p_" + tag)
|
|
363
|
+
c = (cg.get(tag) or {}).get("content") or ""
|
|
364
|
+
cg.close()
|
|
365
|
+
return r, c
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
_k3r, _k3c = _hand_rollback("k3", "knowledge", True)
|
|
369
|
+
print(" [读数] K3 前像本就 immutable:回滚=%s 正文回前像=%s"
|
|
370
|
+
% ({k: _k3r.get(k) for k in ("ok", "error")}, "8080" in _k3c))
|
|
371
|
+
_check("K3 前像本就带 immutable:回滚**必须放行**(保护闸只拦「执行之后才加的」)",
|
|
372
|
+
_k3r.get("ok") is True and "8080" in _k3c, _k3r)
|
|
373
|
+
|
|
374
|
+
# --- K4:旧执行记录(无 after_hash)且无后续执行 → 仍放行(向后兼容)---
|
|
375
|
+
_k4 = _lib("k4")
|
|
376
|
+
_k4i, _ = _c_accept(_k4, "k4", "8081")
|
|
377
|
+
_k4legacy = {k: v for k, v in _k4i["mutation"].items() if k != "after_hash"}
|
|
378
|
+
_k4r = _rb.rollback_mutation(_k4, _k4legacy, reason="K4", pid=_k4i["pid"])
|
|
379
|
+
_k4c = (_k4.get("k4") or {}).get("content") or ""
|
|
380
|
+
print(" [读数] K4 旧记录回滚=%s 正文回前像=%s"
|
|
381
|
+
% ({k: _k4r.get(k) for k in ("ok", "error")}, "8080" in _k4c))
|
|
382
|
+
_check("K4 旧执行记录(无 after_hash)+ 无后续执行:仍放行(不把老库存量单一律拒掉)",
|
|
383
|
+
_k4r.get("ok") is True and "8080" in _k4c, _k4r)
|
|
384
|
+
_k4.close()
|
|
385
|
+
|
|
386
|
+
# --- K5:执行之后只改元数据(正文不变)不算漂移 ---
|
|
387
|
+
_k5 = _lib("k5")
|
|
388
|
+
_k5i, _ = _c_accept(_k5, "k5", "8081")
|
|
389
|
+
_k5c0 = (_k5.get("k5") or {}).get("content") or ""
|
|
390
|
+
_k5.add("k5", _k5c0, layer="knowledge", verification_basis="test",
|
|
391
|
+
tags=["后加标签"])
|
|
392
|
+
_k5r = _rb.rollback_mutation(_k5, _k5i["mutation"], reason="K5", pid=_k5i["pid"])
|
|
393
|
+
_k5c = (_k5.get("k5") or {}).get("content") or ""
|
|
394
|
+
print(" [读数] K5 元数据改写后回滚=%s 正文回前像=%s"
|
|
395
|
+
% ({k: _k5r.get(k) for k in ("ok", "error")}, "8080" in _k5c))
|
|
396
|
+
_check("K5 执行之后只加 tags(正文不变)不算漂移:回滚仍放行(指纹不取每次落盘都变的键)",
|
|
397
|
+
_k5r.get("ok") is True and "8080" in _k5c, _k5r)
|
|
398
|
+
_k5.close()
|
|
399
|
+
|
|
400
|
+
# --- K6:dry-run 在**无漂移**场景也不落盘 ---
|
|
401
|
+
_k6 = _lib("k6")
|
|
402
|
+
_k6i, _ = _c_accept(_k6, "k6", "8081")
|
|
403
|
+
_k6d = _rb.rollback_mutation(_k6, _k6i["mutation"], reason="K6", dry_run=True)
|
|
404
|
+
_k6c = (_k6.get("k6") or {}).get("content") or ""
|
|
405
|
+
print(" [读数] K6 dry-run(无漂移)=%s 正文未动=%s"
|
|
406
|
+
% ({k: _k6d.get(k) for k in ("ok", "dry_run", "would_apply")},
|
|
407
|
+
"8081" in _k6c))
|
|
408
|
+
_check("K6 dry-run 在无漂移场景同样只预演:would_apply=True 且正文未动",
|
|
409
|
+
_k6d.get("ok") is True and _k6d.get("would_apply") is True
|
|
410
|
+
and "8081" in _k6c, _k6d)
|
|
411
|
+
_k6.close()
|
|
412
|
+
|
|
413
|
+
# --- K7:(过度收紧方向)前像就在保护层(self)的节点,回滚必须放行 ---
|
|
414
|
+
_k7r, _k7c = _hand_rollback("k7", "self", False)
|
|
415
|
+
print(" [读数] K7 前像本在 self 层:回滚=%s 正文回前像=%s"
|
|
416
|
+
% ({k: _k7r.get(k) for k in ("ok", "error")}, "8080" in _k7c))
|
|
417
|
+
_check("K7 前像本就在保护层(self):回滚**必须放行**(层保护不构成对合法回滚的硬墙)",
|
|
418
|
+
_k7r.get("ok") is True and "8080" in _k7c, _k7r)
|
|
419
|
+
|
|
420
|
+
# ========================================================= E 组:源码面单点
|
|
421
|
+
print("== E 组:源码面单点 ==")
|
|
422
|
+
_src_rb = inspect.getsource(_rb.rollback_mutation)
|
|
423
|
+
_src_land = inspect.getsource(_rb._land)
|
|
424
|
+
_src_me = inspect.getsource(autonomy_modes.mutation_executed)
|
|
425
|
+
_cli = os.path.join(_REPO, "md_cg", "rollback_cli.py")
|
|
426
|
+
_cli_src = open(_cli, encoding="utf-8").read()
|
|
427
|
+
_check("E1 漂移闸在统一入口内(drifted_since_execute + after_hash 比对)",
|
|
428
|
+
"drifted_since_execute" in _src_rb and 'mutation.get("after_hash")' in _src_rb
|
|
429
|
+
and "preimage_fingerprint" in _src_rb)
|
|
430
|
+
_check("E2 _land 接既有保护单点与留档单点(protect.is_immutable / protect.snapshot)",
|
|
431
|
+
"protect.is_immutable(" in _src_land and "protect.snapshot(" in _src_land)
|
|
432
|
+
_check("E3 mutation_executed 增加 after_hash 形参且仅在非 None 时补键",
|
|
433
|
+
"after_hash=None" in _src_me and 'if after_hash is not None:' in _src_me
|
|
434
|
+
and 'out["after_hash"] = after_hash' in _src_me)
|
|
435
|
+
_check("E4 CLI 暴露 --dry-run / --force 且转交统一入口",
|
|
436
|
+
'"--dry-run"' in _cli_src and '"--force"' in _cli_src
|
|
437
|
+
and "force=args.force" in _cli_src and "dry_run=args.dry_run" in _cli_src)
|
|
438
|
+
_mdcos_src = open(os.path.join(_REPO, "md_cg", "mdcos.py"),
|
|
439
|
+
encoding="utf-8").read()
|
|
440
|
+
_check("E5 执行桥用**同一个**指纹单点补后像(rollback.node_fingerprint)",
|
|
441
|
+
"_rb.node_fingerprint(" in _mdcos_src
|
|
442
|
+
and "after_hash=_after_hash" in _mdcos_src)
|
|
443
|
+
|
|
444
|
+
shutil.rmtree(_TMP, ignore_errors=True)
|
|
445
|
+
|
|
446
|
+
print("")
|
|
447
|
+
if FAILS:
|
|
448
|
+
print("issue #352-A 结果:%d 失败 → %s" % (len(FAILS), ", ".join(FAILS)))
|
|
449
|
+
else:
|
|
450
|
+
print("issue #352-A 结果:全部通过")
|
|
451
|
+
|
|
452
|
+
# ------------------------------------------------------------- 定点变异(镜像树)
|
|
453
|
+
_MUTATIONS = (
|
|
454
|
+
# (描述, ((文件, 旧锚点行组, 新锚点行组), ...), 预期红项集合)
|
|
455
|
+
("M1 摘掉漂移闸(回到「执行时点前像整体覆写」的修前形态)",
|
|
456
|
+
(("md_cg/rollback.py",
|
|
457
|
+
[" if cur_fp != pre_fp and not force: # 相等=幂等重放,放行(见 docstring)"],
|
|
458
|
+
[" if False:"]),),
|
|
459
|
+
# A4a 不在预期集里:执行之后加的 immutable 会**同时**被漂移闸与 _land 的
|
|
460
|
+
# 保护闸命中,摘掉漂移闸后仍被第二道拦下(M2 单独证明第二道的判别力)。
|
|
461
|
+
("A1", "A2", "A3", "A7")),
|
|
462
|
+
("M2 摘掉 _land 的保护闸(executed 之后加的 immutable 被一并清掉)",
|
|
463
|
+
(("md_cg/rollback.py",
|
|
464
|
+
[" if prot and not force:"],
|
|
465
|
+
[" if False:"]),),
|
|
466
|
+
("A4b",)),
|
|
467
|
+
("M3 摘掉回滚留痕(_audit.jsonl 不再有 op=rollback)",
|
|
468
|
+
(("md_cg/rollback.py",
|
|
469
|
+
[' out["audited"] = _audit_rollback(cg, tgt, act, rel, out, actor=actor,',
|
|
470
|
+
' pid=pid, reason=reason)'],
|
|
471
|
+
[' out["audited"] = False']),),
|
|
472
|
+
("A5",)),
|
|
473
|
+
("M4 摘掉落盘前的被覆盖版本留档(protect.snapshot)",
|
|
474
|
+
(("md_cg/rollback.py",
|
|
475
|
+
[" protect.snapshot(cg, node_id)"],
|
|
476
|
+
[" pass"]),),
|
|
477
|
+
("A6", "E2")),
|
|
478
|
+
("M5 摘掉 CLI 的 --force 接线(显式承担后果的通道消失)",
|
|
479
|
+
(("md_cg/rollback_cli.py",
|
|
480
|
+
[" force=args.force, dry_run=args.dry_run)"],
|
|
481
|
+
[" force=False, dry_run=args.dry_run)"]),),
|
|
482
|
+
("A8", "E4")),
|
|
483
|
+
("M6 摘掉执行桥的后像补全(回滚只剩 decisions 次序判据,直写类漂移看不见)",
|
|
484
|
+
(("md_cg/mdcos.py",
|
|
485
|
+
[" after_hash=_after_hash)"],
|
|
486
|
+
[" after_hash=None)"]),),
|
|
487
|
+
# A7 也在集里:dry-run 场景的漂移同样靠 after_hash 才看得见(直写不经 accept,
|
|
488
|
+
# decisions 次序判据拿不到证据)。
|
|
489
|
+
("A1", "A3", "A7", "E5")),
|
|
490
|
+
("M7 **过度收紧方向**:保护闸改成「现盘面受保护即拒」(不看前像是否本就带保护位)",
|
|
491
|
+
(("md_cg/rollback.py",
|
|
492
|
+
[" if not _pre_prot:"],
|
|
493
|
+
[" if True:"]),),
|
|
494
|
+
("K3", "K7")),
|
|
495
|
+
)
|
|
496
|
+
|
|
497
|
+
|
|
498
|
+
def _mirror_build():
|
|
499
|
+
dst = tempfile.mkdtemp(prefix="iss352a_mut_")
|
|
500
|
+
for name in ("md_cg", "scripts"):
|
|
501
|
+
src = os.path.join(_REPO, name)
|
|
502
|
+
if os.path.isdir(src):
|
|
503
|
+
shutil.copytree(src, os.path.join(dst, name),
|
|
504
|
+
ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
|
|
505
|
+
for name in sorted(os.listdir(_REPO)):
|
|
506
|
+
p = os.path.join(_REPO, name)
|
|
507
|
+
if os.path.isfile(p) and name.endswith(".py"):
|
|
508
|
+
shutil.copy2(p, os.path.join(dst, name))
|
|
509
|
+
return dst
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
def _nl_of(path):
|
|
513
|
+
with open(path, "rb") as fh:
|
|
514
|
+
raw = fh.read(8192)
|
|
515
|
+
return "\r\n" if b"\r\n" in raw else "\n"
|
|
516
|
+
|
|
517
|
+
|
|
518
|
+
def _anchor(parts, nl):
|
|
519
|
+
return nl.join(parts)
|
|
520
|
+
|
|
521
|
+
|
|
522
|
+
def _run_in(tree):
|
|
523
|
+
env = dict(os.environ)
|
|
524
|
+
env["PYTHONPATH"] = tree
|
|
525
|
+
env["PYTHONUTF8"] = "1"
|
|
526
|
+
env["PYTHONDONTWRITEBYTECODE"] = "1"
|
|
527
|
+
pr = subprocess.run([sys.executable, "-X", "utf8", "-m",
|
|
528
|
+
"md_cg.test_issue352_rollback"],
|
|
529
|
+
capture_output=True, text=True, encoding="utf-8",
|
|
530
|
+
errors="replace", cwd=tree, env=env, timeout=3600)
|
|
531
|
+
out = (pr.stdout or "") + (pr.stderr or "")
|
|
532
|
+
reds = [ln.split("FAIL ", 1)[1].strip()
|
|
533
|
+
for ln in out.splitlines() if "FAIL " in ln]
|
|
534
|
+
return pr.returncode, reds, out
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
def _mutate():
|
|
538
|
+
print("!! 定点变异自证:把修复点改回改动前形态 / 摘掉接线,守卫必须转红")
|
|
539
|
+
tree = _mirror_build()
|
|
540
|
+
try:
|
|
541
|
+
rc, reds, out = _run_in(tree)
|
|
542
|
+
print(" 未变异镜像基线:rc=%d 红项=%d(必须 rc=0 且 0 红)" % (rc, len(reds)))
|
|
543
|
+
if rc != 0 or reds:
|
|
544
|
+
print(chr(10).join(out.splitlines()[-30:]))
|
|
545
|
+
for ln in out.splitlines():
|
|
546
|
+
if "[读数]" in ln:
|
|
547
|
+
print(" 修后读数(未变异镜像):" + ln.strip())
|
|
548
|
+
okc, bad = 0, []
|
|
549
|
+
for desc, edits, exp in _MUTATIONS:
|
|
550
|
+
backups, miss = [], []
|
|
551
|
+
for rel, oldp, newp in edits:
|
|
552
|
+
p = os.path.join(tree, rel)
|
|
553
|
+
nl = _nl_of(p)
|
|
554
|
+
old, new = _anchor(oldp, nl), _anchor(newp, nl)
|
|
555
|
+
with open(p, encoding="utf-8", newline="") as fh:
|
|
556
|
+
t = fh.read()
|
|
557
|
+
if t.count(old) != 1:
|
|
558
|
+
miss.append("%s —— 锚点命中 %d 次" % (rel, t.count(old)))
|
|
559
|
+
continue
|
|
560
|
+
backups.append((p, t))
|
|
561
|
+
with open(p, "w", encoding="utf-8", newline="") as fh:
|
|
562
|
+
fh.write(t.replace(old, new, 1))
|
|
563
|
+
if miss:
|
|
564
|
+
bad.append("%s —— %s" % (desc, ";".join(miss)))
|
|
565
|
+
print(" ANCHOR-MISS " + desc)
|
|
566
|
+
for p, t in backups:
|
|
567
|
+
with open(p, "w", encoding="utf-8", newline="") as fh:
|
|
568
|
+
fh.write(t)
|
|
569
|
+
continue
|
|
570
|
+
rc2, reds2, out2 = _run_in(tree)
|
|
571
|
+
for p, t in backups:
|
|
572
|
+
with open(p, "w", encoding="utf-8", newline="") as fh:
|
|
573
|
+
fh.write(t)
|
|
574
|
+
hit = sorted({e for r in reds2 for e in exp if r.startswith(e + " ")})
|
|
575
|
+
if rc2 != 0 and hit == sorted(exp) and len(reds2) == len(exp):
|
|
576
|
+
okc += 1
|
|
577
|
+
print(" OK %s 红项=%d 指定命中=%d/%d"
|
|
578
|
+
% (desc, len(reds2), len(hit), len(exp)))
|
|
579
|
+
else:
|
|
580
|
+
bad.append(desc)
|
|
581
|
+
print(" MISMATCH %s 红项=%d 指定命中=%d/%d rc=%d"
|
|
582
|
+
% (desc, len(reds2), len(hit), len(exp), rc2))
|
|
583
|
+
for r in reds2[:14]:
|
|
584
|
+
print(" FAIL " + r)
|
|
585
|
+
for ln in out2.splitlines():
|
|
586
|
+
if "[读数]" in ln and "基线" not in ln:
|
|
587
|
+
pass
|
|
588
|
+
print("定点变异自证:%s(%d 条不合格)"
|
|
589
|
+
% ("PASS" if not bad else "FAIL", len(bad)))
|
|
590
|
+
for b in bad:
|
|
591
|
+
print(" 不合格:" + b)
|
|
592
|
+
finally:
|
|
593
|
+
shutil.rmtree(tree, ignore_errors=True)
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
def main():
|
|
597
|
+
if "--mutate" in sys.argv:
|
|
598
|
+
_mutate()
|
|
599
|
+
return 1 if FAILS else 0
|
|
600
|
+
return 0 if not FAILS else 1
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
if __name__ == "__main__":
|
|
604
|
+
sys.exit(main())
|