@furongjun1999/dsh-memory 0.9.6 → 0.9.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (216) hide show
  1. package/data/policy.json +38 -38
  2. package/docs/discipline/templates/spec.txt.tmpl +1 -1
  3. package/dsh/cordis.yml.example +139 -139
  4. package/lib/hooks.d.ts +19 -0
  5. package/lib/hooks.js +29 -6
  6. package/lib/index.js +11 -2
  7. package/lib/lib/hook_audit.js +38 -2
  8. package/lib/lib/prompt_safety.js +14 -3
  9. package/lib/tools.d.ts +7 -0
  10. package/lib/tools.js +22 -1
  11. package/md_cg/_guard_kit.py +121 -121
  12. package/md_cg/anchor.py +114 -114
  13. package/md_cg/audit.py +15 -2
  14. package/md_cg/chain.py +3 -2
  15. package/md_cg/config_registry_guard.py +487 -487
  16. package/md_cg/conformance.py +747 -747
  17. package/md_cg/consolidate.py +1793 -1793
  18. package/md_cg/crypto.py +794 -794
  19. package/md_cg/hyperedge.py +262 -262
  20. package/md_cg/interface_schema.py +146 -146
  21. package/md_cg/lifecycle.py +426 -426
  22. package/md_cg/mdcos.py +12 -8
  23. package/md_cg/predict.py +1103 -1103
  24. package/md_cg/protect.py +654 -654
  25. package/md_cg/provenance.py +606 -606
  26. package/md_cg/queue_health.py +130 -130
  27. package/md_cg/refine.py +613 -613
  28. package/md_cg/retrieve.py +128 -128
  29. package/md_cg/revise.py +140 -140
  30. package/md_cg/rollback.py +657 -657
  31. package/md_cg/rollback_cli.py +124 -124
  32. package/md_cg/rotate.py +346 -346
  33. package/md_cg/routing.py +490 -490
  34. package/md_cg/self_surface.py +50 -50
  35. package/md_cg/semantic/canonical.py +144 -144
  36. package/md_cg/semantic/zh_en_atoms.py +141 -141
  37. package/md_cg/sources.py +1091 -1091
  38. package/md_cg/stg.py +3 -2
  39. package/md_cg/test_audit_rotate_parity.py +279 -279
  40. package/md_cg/test_autonomy_modes.py +2013 -2013
  41. package/md_cg/test_cn_recall_grams.py +206 -206
  42. package/md_cg/test_code_kind_forbidden.py +101 -0
  43. package/md_cg/test_condaccept.py +156 -156
  44. package/md_cg/test_condgate.py +141 -141
  45. package/md_cg/test_heartbeat_ledger.py +557 -557
  46. package/md_cg/test_i50e_readside_protection.py +658 -658
  47. package/md_cg/test_issue111_gate_order.py +76 -76
  48. package/md_cg/test_issue111_scoring_contract.py +86 -86
  49. package/md_cg/test_issue121_calibration_bounds.py +293 -293
  50. package/md_cg/test_issue145_sleep_no_silent_rollback.py +167 -167
  51. package/md_cg/test_issue147_pack_id_escape.py +378 -378
  52. package/md_cg/test_issue148_merge_downgrade_leak.py +192 -192
  53. package/md_cg/test_issue149_accel_default_face.py +248 -248
  54. package/md_cg/test_issue150_empty_query_terms.py +328 -328
  55. package/md_cg/test_issue151_153_157_write_cache_index.py +151 -151
  56. package/md_cg/test_issue152_sealed_postings.py +309 -309
  57. package/md_cg/test_issue153b_index_generation_invalidation.py +142 -142
  58. package/md_cg/test_issue157b_index_remainder.py +344 -344
  59. package/md_cg/test_issue189_conclusion_bidirectional.py +223 -223
  60. package/md_cg/test_issue191_import_trust.py +382 -382
  61. package/md_cg/test_issue193_write_scope.py +378 -378
  62. package/md_cg/test_issue212_writeface_gaps.py +349 -349
  63. package/md_cg/test_issue214_reject_sensitivity.py +266 -266
  64. package/md_cg/test_issue215_condition_source_gap.py +272 -272
  65. package/md_cg/test_issue216_clash_routes.py +214 -214
  66. package/md_cg/test_issue217_slot_routes.py +248 -248
  67. package/md_cg/test_issue218_postcondition_value_blind.py +222 -222
  68. package/md_cg/test_issue258_pack_sensitivity.py +468 -468
  69. package/md_cg/test_issue265_coldverify_multiproc.py +438 -438
  70. package/md_cg/test_issue274_read_node_id_none.py +341 -341
  71. package/md_cg/test_issue275_layer_param.py +250 -250
  72. package/md_cg/test_issue277_node_id_traversal.py +384 -384
  73. package/md_cg/test_issue278_neg_separated_tension.py +271 -271
  74. package/md_cg/test_issue278_neg_window.py +294 -294
  75. package/md_cg/test_issue278_negative_layering.py +83 -83
  76. package/md_cg/test_issue284_skip_not_pass.py +291 -291
  77. package/md_cg/test_issue294_flywheel_sensitivity.py +573 -573
  78. package/md_cg/test_issue301_baseline_corpus_gate.py +245 -245
  79. package/md_cg/test_issue303_design_boundaries.py +262 -262
  80. package/md_cg/test_issue315_merge_branch_sensitivity.py +383 -383
  81. package/md_cg/test_issue319_tenant_unregistered_bypass.py +256 -256
  82. package/md_cg/test_issue321_mcp_deep_json.py +304 -304
  83. package/md_cg/test_issue322_node_write_lock.py +727 -727
  84. package/md_cg/test_issue324_write_size_fields.py +214 -214
  85. package/md_cg/test_issue325_audit_poison.py +229 -229
  86. package/md_cg/test_issue326_ingest_auto_whitelist.py +331 -331
  87. package/md_cg/test_issue326_key_perms.py +355 -355
  88. package/md_cg/test_issue326_twin_parser_parity.py +323 -323
  89. package/md_cg/test_issue326_vm_call_frame.py +569 -569
  90. package/md_cg/test_issue345_writelimit_effective_sens.py +233 -233
  91. package/md_cg/test_issue346_overwrite_sensitivity_gate.py +424 -424
  92. package/md_cg/test_issue348_insight_verify_op.py +199 -199
  93. package/md_cg/test_issue348_merge_rmw_lock.py +345 -345
  94. package/md_cg/test_issue350_casefold_collision.py +235 -235
  95. package/md_cg/test_issue351_ingest_root_fallback.py +201 -201
  96. package/md_cg/test_issue352_imgaudit.py +418 -418
  97. package/md_cg/test_issue352_pathsjson.py +537 -537
  98. package/md_cg/test_issue352_refindex.py +412 -412
  99. package/md_cg/test_issue352_rollback.py +604 -604
  100. package/md_cg/test_issue354_p2_writelimit_toctou.py +327 -327
  101. package/md_cg/test_issue354_rust_index_containment.py +571 -571
  102. package/md_cg/test_issue354_rust_wal_containment.py +606 -606
  103. package/md_cg/test_issue358_truncation_observable.py +247 -247
  104. package/md_cg/test_issue360_tidy_private_excerpt.py +195 -195
  105. package/md_cg/test_issue361_blindspot_not_silent_drop.py +494 -494
  106. package/md_cg/test_issue362_neg_condition.py +468 -468
  107. package/md_cg/test_issue363_lexical_idf.py +275 -275
  108. package/md_cg/test_issue370_readcache_assembly.py +488 -488
  109. package/md_cg/test_issue376_heartbeat_failed.py +293 -293
  110. package/md_cg/test_issue377_layer_protect_stamp.py +324 -324
  111. package/md_cg/test_issue378_protected_floor.py +310 -310
  112. package/md_cg/test_issue383_state_event_write_gate.py +462 -462
  113. package/md_cg/test_issue397_sens_eff_wiring.py +435 -435
  114. package/md_cg/test_issue398_toolface_op_parity.py +511 -511
  115. package/md_cg/test_issue399_hive_json_shape.py +317 -317
  116. package/md_cg/test_issue401_wisdom_codegen.py +295 -295
  117. package/md_cg/test_issue404_heal_receipt.py +389 -389
  118. package/md_cg/test_issue409_compact_index_completeness.py +509 -509
  119. package/md_cg/test_issue410_verify_demote_write.py +366 -366
  120. package/md_cg/test_issue411_scrub_idempotence.py +575 -575
  121. package/md_cg/test_issue412_discipline_producer_parity.py +311 -311
  122. package/md_cg/test_issue413_preimage_failclosed.py +239 -239
  123. package/md_cg/test_issue413_preimage_twin.py +339 -339
  124. package/md_cg/test_issue415_deps_gate_probe.py +357 -357
  125. package/md_cg/test_issue416_entry_key_subset.py +525 -525
  126. package/md_cg/test_issue417_twophase_merge_landed.py +423 -423
  127. package/md_cg/test_issue425_ingest_library_path.py +331 -331
  128. package/md_cg/test_issue426c_enqueue_not_silent.py +346 -346
  129. package/md_cg/test_issue426e_restore_overwrite_live.py +310 -310
  130. package/md_cg/test_issue426f_paths_gbk_failclosed.py +301 -301
  131. package/md_cg/test_issue427_serve_status_shape.py +191 -191
  132. package/md_cg/test_issue431_432_468_protect_single_point.py +322 -322
  133. package/md_cg/test_issue433_outlook_threshold_single_point.py +278 -278
  134. package/md_cg/test_issue434_435_float_gate.py +90 -90
  135. package/md_cg/test_issue439_sibling_sensitivity_inherit.py +448 -448
  136. package/md_cg/test_issue43_default_policy.py +874 -874
  137. package/md_cg/test_issue444_scrub_evidence_plaintext.py +281 -281
  138. package/md_cg/test_issue445_metacognition_ledger_gate.py +454 -454
  139. package/md_cg/test_issue446_453_403_verified_evidence.py +382 -382
  140. package/md_cg/test_issue450_sensitivity_fallback_reason.py +51 -51
  141. package/md_cg/test_issue450_writegate_unknown_sens.py +203 -203
  142. package/md_cg/test_issue451_audit_actor.py +313 -313
  143. package/md_cg/test_issue452_ingest_root_sensitivity.py +299 -299
  144. package/md_cg/test_issue463_ghost_prune_guard.py +259 -259
  145. package/md_cg/test_issue466_basis_registry.py +469 -469
  146. package/md_cg/test_issue467_identity_sensitivity_parity.py +360 -360
  147. package/md_cg/test_issue469_scrub_override_passthrough.py +126 -126
  148. package/md_cg/test_issue470_wal_event_durable.py +427 -427
  149. package/md_cg/test_issue471_identity_readable.py +136 -136
  150. package/md_cg/test_issue472_audit_chain.py +90 -90
  151. package/md_cg/test_issue472_prune_ledger.py +105 -105
  152. package/md_cg/test_issue472_rollback_degraded.py +81 -81
  153. package/md_cg/test_issue491_compose_sandbox.py +460 -460
  154. package/md_cg/test_issue498_merge_trace_stale_index.py +280 -280
  155. package/md_cg/test_issue511_scoreboard_bad_files.py +256 -256
  156. package/md_cg/test_issue521_rotate_lock_suffix.py +110 -110
  157. package/md_cg/test_issue524_orphan_noise_no_demote.py +88 -88
  158. package/md_cg/test_issue525_read_null_semantics.py +52 -52
  159. package/md_cg/test_issue547_append_jsonl_whitelist.py +114 -114
  160. package/md_cg/test_issue547_audit_durable.py +96 -96
  161. package/md_cg/test_issue557_r2_write_entries_size_gate.py +83 -83
  162. package/md_cg/test_issue559_refine_prefix.py +69 -69
  163. package/md_cg/test_issue562_retired_faces.py +150 -0
  164. package/md_cg/test_issue71_queue_backlog.py +376 -376
  165. package/md_cg/test_issue72_coldverify_controlled_drain.py +420 -420
  166. package/md_cg/test_issue82_credential_rules.py +271 -271
  167. package/md_cg/test_issue91_verifier_sandbox.py +314 -314
  168. package/md_cg/test_issue93_remember_redaction.py +282 -282
  169. package/md_cg/test_logref.py +1141 -1141
  170. package/md_cg/test_md_access_parity.py +330 -330
  171. package/md_cg/test_md_cg_no_hive_dep.py +139 -139
  172. package/md_cg/test_mutation_residue.py +119 -119
  173. package/md_cg/test_mutation_rollback.py +835 -835
  174. package/md_cg/test_n123_derive_expiry_chain.py +210 -210
  175. package/md_cg/test_n209_verify_write_face_gates.py +466 -466
  176. package/md_cg/test_n62_tenant_bind_failclosed.py +200 -200
  177. package/md_cg/test_neg_tail_honesty.py +733 -733
  178. package/md_cg/test_p21_tokens.py +294 -294
  179. package/md_cg/test_p27_docindex.py +792 -792
  180. package/md_cg/test_p28_refcheck.py +1053 -1053
  181. package/md_cg/test_p2_six_elements.py +476 -476
  182. package/md_cg/test_p33_ccg_wiring.py +300 -300
  183. package/md_cg/test_p3_legacy_closure.py +438 -438
  184. package/md_cg/test_p40_refine_worklist.py +244 -244
  185. package/md_cg/test_p4_freshness.py +781 -781
  186. package/md_cg/test_p9c_dedup_hints.py +288 -288
  187. package/md_cg/test_policy_required_ccg.py +444 -444
  188. package/md_cg/test_retr_s5.py +312 -312
  189. package/md_cg/test_s1_ast_judge.py +109 -109
  190. package/md_cg/test_state_event_op.py +521 -521
  191. package/md_cg/test_stgidx_index_parity.py +1024 -1024
  192. package/md_cg/test_sustain_bounded.py +652 -652
  193. package/md_cg/test_trust_edge_reachability.py +405 -405
  194. package/md_cg/test_w7_redlines.py +659 -659
  195. package/md_cg/test_write_return_shape.py +919 -919
  196. package/md_cg/test_writepipe.py +228 -228
  197. package/md_cg/trust.py +1114 -1114
  198. package/md_cg/twophase.py +303 -303
  199. package/md_cg/whitebox.py +536 -536
  200. package/md_cg/whitebox_kb/wisdom/compose_engine.py +1634 -1634
  201. package/md_cg/whitebox_kb/wisdom/core/condition_vm.py +318 -318
  202. package/md_cg/whitebox_kb/wisdom/core/parser.py +1082 -1082
  203. package/md_cg/whitebox_kb/wisdom/md_access.py +490 -490
  204. package/md_cg/whitebox_kb/wisdom/migrate_wisdom.py +497 -497
  205. package/md_cg/whitebox_kb/wisdom/mini_python.py +995 -995
  206. package/md_cg/whitebox_kb/wisdom/net_units.py +2900 -2900
  207. package/md_cg/whitebox_kb/wisdom/semantic_translate.py +4060 -4060
  208. package/md_cg/writepipe.py +1337 -1337
  209. package/package.json +3 -2
  210. package/scripts/workspace_index.py +328 -0
  211. package/src/hooks.ts +30 -6
  212. package/src/index.ts +14 -5
  213. package/src/lib/hook_audit.ts +282 -256
  214. package/src/lib/mdcg_client.ts +469 -469
  215. package/src/lib/prompt_safety.ts +17 -3
  216. package/src/tools.ts +23 -1
@@ -1,346 +1,346 @@
1
- # -*- coding: utf-8 -*-
2
- # 功能名:队列写入失败不得静默(issue #426-C:inbox/decisions 落盘失败仍回执「已入审核队列——不需要重试」)
3
- # 生效条件:python -u -X utf8 -m md_cg.test_issue426c_enqueue_not_silent [--mutate];被测面=md_cg/fsutil.py 的 append_jsonl(strict=) + md_cg/mdcos.py 的三处队列写入(入队/裁决/级联)
4
- # 子功能:A 组=正向腿(**失败路径**:注入不可写队列路径 ⇒ 抛错、不返回 pid、**失败被记账**);K 组=反向腿①(成功路径逐位不回归 + **strict=False 旧语义逐位不变**(126 处基数)+ 显式 strict 恰 3 处 AST 断言);E 组=源码面(strict 分支先记账再抛);M 组=反向腿②(--mutate,红项严格相等且不重叠)
5
- # 执行:--mutate 时在镜像树里逐条撤 strict/撤记账,守卫必须按指定项转红
6
- # 验证方式:全部 PASS 即通过(退出码 0);--mutate 下每条变异的红项集合须与预期逐项相符,锚点未命中即 fail-closed
7
- # 不适用条件:① 腿看不见「真实磁盘满 / 只读介质 / 权限拒绝」的**全部分支**——只能注入已知失败形态(本次用「路径不可创建」这一真实 OSError 分支);② 不覆盖**跨进程**下别的写者把 jsonl 删掉的情形(属进程内一致性边界);③ 不覆盖 `md_cg/` 外的两个调用点(scripts/ 两处,保持默认 strict=False,行为由 K 组代表点断言覆盖);④ 不判审计行内容与上限(属 U13)。
8
- # 形态名(供按名检索):⑤ 「**并列读数不得拼因果**」——同一份报告里的两个读数变化(本例:红 171→172 与 豁免 2096→2095)**必须各自定位来源**,不得归成一个因(我写成「一条从豁免变红」,Lead 照抄进派单 ⇒ 链条上多传一手错)。同族:把「相关」当「因果」、把「同时发生」当「同一件事」。
9
- # 形态名(供按名检索):④ 「**空转假绿**」——操作没落盘,回执却报成功(本例:入队失败仍回 `moved_to=review_queue` +「不需要重试」)。判据面上的同族是「写了却没生效」。
10
- """issue #426-C 守卫:不能丢的队列写入必须「失败即响」。
11
-
12
- 病象(修前):`fsutil.append_jsonl` 早就返回 bool,但 `mdcos` 的三处队列写入
13
- (`inbox_log` 入队 / `decisions_log` 裁决 / 裁决级联)**都不读返回值** ⇒ 落盘失败时
14
- 仍返回 pid 并构造「已入审核队列——不需要重试」的成功回执(**空转假绿**)。
15
- 修法(两半同批;**跨槽回触声明**:U06 槽内回触 U05 面的三处消费点;理由=修点(U06)
16
- 与消费面(U05)是同一件事的两半,只改一半零行为变化):
17
- · U06 半:`append_jsonl(path, record, strict=False)` —— 语义成文在**一处**(不让每个
18
- 调用方各自 if);strict 时**先记账再上抛**;
19
- · U05 半:三处队列写入走 strict;失败 ⇒ 回执构造不出来(异常按仓内既有「异常」编码上行,
20
- **不新增第四种失败编码**)。
21
- """
22
- from __future__ import annotations
23
-
24
- import ast
25
- import os
26
- import shutil
27
- import subprocess
28
- import sys
29
- import tempfile
30
-
31
- sys.stdout.reconfigure(encoding="utf-8", errors="replace")
32
- _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
33
- if _REPO not in sys.path:
34
- sys.path.insert(0, _REPO)
35
-
36
- _TMP = tempfile.mkdtemp(prefix="iss426c_")
37
- os.environ.update({"MDCG_ROOT": _TMP + "/brain", "MDCG_STATE_ROOT": _TMP + "/state",
38
- "MDCG_AUX_ROOT": _TMP + "/auxstore", "MDCG_DATA_ROOT": _TMP + "/datastore",
39
- "MDCG_EXPORT_ROOT": _TMP, "MDCG_SUSTAIN": "0", "MDCG_SLEEP": "0"})
40
- os.environ.pop("MDCG_READ_CACHE", None)
41
-
42
- from md_cg import fsutil as _fs # noqa: E402
43
- from md_cg import tokens as _tk # noqa: E402
44
- from md_cg.mdcos import MdCGSecure # noqa: E402
45
- from md_cg.security import Principal # noqa: E402
46
-
47
- PASSED, FAILS = [], []
48
- def _readonly_queue(tag):
49
- """真实失败注入:建一个**只读**文件当队列——读取/前置都正常,写才失败(Windows PermissionError)。"""
50
- p = os.path.join(_TMP, "ro_%s.jsonl" % tag)
51
- with open(p, "w", encoding="utf-8") as f:
52
- f.write("")
53
- os.chmod(p, 0o444)
54
- return p
55
-
56
-
57
- def _pid_of(r):
58
- return r if isinstance(r, str) else ((r or {}).get("pid") if isinstance(r, dict) else None)
59
-
60
-
61
- def _check(name, cond, detail=""):
62
- if cond:
63
- print(" PASS " + name)
64
- PASSED.append(name)
65
- else:
66
- print(" FAIL " + name + " " + str(detail)[:240])
67
- FAILS.append(name)
68
-
69
-
70
- def _ccg(v="队列探针"):
71
- return ("# 功能名:" + v + chr(10) + "# 生效条件:任何会话" + chr(10)
72
- + "# 子功能:" + v + chr(10) + "# 执行:" + v + chr(10)
73
- + "# 验证方式:外部裁决" + chr(10) + "# 不适用条件:无")
74
-
75
-
76
- def _cg(root):
77
- sp = _tk.role_spec("designer")
78
- return MdCGSecure(root, principal=Principal(
79
- actor="q426c", clearance="secret", can_write=sp["can_write"], can_admin=True,
80
- role="designer", layers_allow=sp["layers_allow"], ops_allow=sp["ops_allow"]),
81
- autoflush=1)
82
-
83
-
84
- def _raise_kind(fn):
85
- """返回 (是否抛, 异常类名, 返回值)。"""
86
- try:
87
- return False, None, fn()
88
- except OSError as e:
89
- return True, type(e).__name__, None
90
- except Exception as e: # noqa: BLE001
91
- return True, type(e).__name__, None
92
-
93
-
94
- def group_a(tmp):
95
- print("== A 组:正向腿(失败路径)——队列写不进去就必须响 ==")
96
- root = os.path.join(tmp, "a")
97
- cg = _cg(root)
98
- good_inbox = cg.inbox_log
99
- cg.inbox_log = _readonly_queue("inbox") # 注入真实失败(只读文件)
100
- n0 = _fs.APPEND_FAILURES
101
- raised, kind, ret = _raise_kind(lambda: cg.propose("q426c_a", _ccg("入队失败探针"),
102
- info=True))
103
- _check("A1 入队落盘失败 ⇒ 抛错(不再返回 pid)",
104
- raised and ret is None, "raised=%s kind=%s ret=%r" % (raised, kind, ret))
105
- _check("A2 **失败被记账**(不静默:APPEND_FAILURES 增长)",
106
- _fs.APPEND_FAILURES > n0, "before=%s after=%s" % (n0, _fs.APPEND_FAILURES))
107
- cg.inbox_log = good_inbox
108
- ok2, _k2, r2 = _raise_kind(lambda: cg.propose("q426c_b", _ccg("正常入队"), info=True))
109
- pid = _pid_of(r2)
110
- _check("A3 恢复可写后正常入队 ⇒ 返回 pid(成功路径不回归)",
111
- (not ok2) and isinstance(pid, str) and pid.startswith("prop_"), "r=%r" % (r2,))
112
- good_dec = cg.decisions_log
113
- cg.decisions_log = _readonly_queue("decisions") # 裁决面注入失败
114
- raised3, kind3, ret3 = _raise_kind(lambda: cg.review_decide(pid, "accept", reason="ok"))
115
- _check("A4 裁决落盘失败 ⇒ 抛错(不返回「已裁决」回执)",
116
- raised3 and ret3 is None, "raised=%s kind=%s ret=%r" % (raised3, kind3, str(ret3)[:60]))
117
- cg.decisions_log = good_dec
118
- rows = []
119
- if os.path.exists(cg.decisions_log):
120
- with open(cg.decisions_log, encoding="utf-8") as f:
121
- rows = [ln for ln in f if ln.strip()]
122
- _check("A5 失败那次**没有**落进 decisions(不留半条记录)", len(rows) == 0, "rows=%d" % len(rows))
123
- cg.close()
124
-
125
-
126
- def group_k(tmp):
127
- print("== K 组:反向腿①——成功路径不回归 / strict=False 旧语义逐位不变 ==")
128
- root = os.path.join(tmp, "k")
129
- cg = _cg(root)
130
- r1 = cg.propose("q426c_k1", _ccg("成功路径"), info=True)
131
- pid = _pid_of(r1)
132
- _check("K1 成功入队:返回 pid 且 rec 落盘(逐位形态不变)",
133
- isinstance(pid, str) and pid.startswith("prop_") and os.path.exists(cg.inbox_log),
134
- "r=%r inbox=%s" % (r1, cg.inbox_log))
135
- r2 = cg.propose("q426c_k1", _ccg("成功路径"), info=True) # 同内容 ⇒ 幂等 dedup
136
- _check("K2 同内容再次入队 ⇒ 幂等返回同一 pid(既有 dedup 分支不变)",
137
- _pid_of(r2) == pid and bool((r2 or {}).get("dedup")), "p1=%s r2=%r" % (pid, r2))
138
- cg.close()
139
- n0 = _fs.APPEND_FAILURES
140
- _ro = _readonly_queue("legacy")
141
- ok, kind, ret = _raise_kind(lambda: _fs.append_jsonl(_ro, {"a": 1}))
142
- _check("K3 **strict=False 旧语义逐位不变**:不可写路径下返回 False 且不抛",
143
- (not ok) and ret is False, "raised=%s ret=%r" % (ok, ret))
144
- _check("K4 旧语义同样记账(既有 best-effort 记账面未退化)",
145
- _fs.APPEND_FAILURES > n0, "before=%s after=%s" % (n0, _fs.APPEND_FAILURES))
146
-
147
-
148
- def _src(rel):
149
- with open(os.path.join(_REPO, rel), encoding="utf-8") as f:
150
- return f.read()
151
-
152
-
153
- def _strict_sites():
154
- tot = st = 0
155
- sites = []
156
- for base, dirs, files in os.walk(_REPO):
157
- dirs[:] = [d for d in dirs if d not in (".git", "node_modules", "__pycache__",
158
- ".tmp", ".pytest_cache")]
159
- for f in files:
160
- if not f.endswith(".py") or f == "test_issue426c_enqueue_not_silent.py":
161
- continue # 本守卫自身含 1 处对照调用,不计入基数
162
- p = os.path.join(base, f)
163
- try:
164
- tree = ast.parse(_src(os.path.relpath(p, _REPO)), p)
165
- except Exception: # noqa: BLE001
166
- continue
167
- for node in ast.walk(tree):
168
- if isinstance(node, ast.Call) and (
169
- (getattr(node.func, "attr", None) or getattr(node.func, "id", None))
170
- == "append_jsonl"):
171
- tot += 1
172
- if any(k.arg == "strict" and getattr(k.value, "value", None) is True
173
- for k in node.keywords):
174
- st += 1
175
- sites.append("%s:%d" % (os.path.relpath(p, _REPO).replace(os.sep, "/"),
176
- node.lineno))
177
- return tot, st, sorted(sites)
178
-
179
-
180
- def group_e(tmp):
181
- print("== E 组:源码面——strict 入口与恰 3 处调用点 ==")
182
- tot, st, sites = _strict_sites()
183
- print(" NOTE E1 读数:显式 strict=True 调用点 = %d 处 %s(总数 %d)" % (st, sites, tot))
184
- joined = "\n".join(sites)
185
- _check("E1a 入队站点走 strict(mdcos 的 inbox_log 写入)",
186
- any(":2647" in s or ":2648" in s or ":2649" in s or ":2650" in s for s in sites)
187
- or len(sites) == 3, "sites=%s" % sites)
188
- _check("E1b 裁决站点走 strict", len(sites) >= 2, "sites=%s" % sites)
189
- _check("E1c 级联站点走 strict(第三个静默点一并收口)", len(sites) >= 3, "sites=%s" % sites)
190
- _check("E2 三处都在 md_cg/mdcos.py(入队/裁决/级联)",
191
- all(s.startswith("md_cg/mdcos.py:") for s in sites), "sites=%s" % sites)
192
- # E3 是**读数**不是门(基数随新增守卫/新写点自然变化,钉死会变成「为数字而红」;
193
- # 向后兼容由 K3/K4 的**行为对照**证明——那才是判据):
194
- print(" NOTE E3 调用点读数:总数=%d 显式 strict=%d 保持默认(strict=False)=%d"
195
- "(本守卫自身那 1 处对照调用不计入)" % (tot, st, tot - st))
196
- src = _src("md_cg/fsutil.py")
197
- fn = None
198
- for node in ast.walk(ast.parse(src, "fsutil.py")):
199
- if isinstance(node, ast.FunctionDef) and node.name == "append_jsonl":
200
- fn = node
201
- strict_raises = 0
202
- note_calls = 0
203
- for node in ast.walk(fn) if fn is not None else []:
204
- if isinstance(node, ast.Raise):
205
- strict_raises += 1
206
- if isinstance(node, ast.Call) and getattr(node.func, "id", None) == "_note_append_failure":
207
- note_calls += 1
208
- _check("E4 fsutil.append_jsonl 内:记账调用 2 处(两个 OSError 分支各一)+ raise 2 处(strict 各一)",
209
- note_calls == 2 and strict_raises == 2, "note=%d raise=%d" % (note_calls, strict_raises))
210
-
211
-
212
- def _guard_group(tag, fn, tmp):
213
- try:
214
- fn(tmp)
215
- except Exception as exc: # noqa: BLE001
216
- import traceback
217
- _check("%s0 组内异常(fail-closed:异常即红)" % tag, False,
218
- "%s: %s" % (type(exc).__name__, exc))
219
- traceback.print_exc()
220
-
221
-
222
- def main():
223
- print("== #426-C 队列入队/裁决不得静默守卫 ==")
224
- _guard_group("A", group_a, _TMP)
225
- _guard_group("K", group_k, _TMP)
226
- _guard_group("E", group_e, _TMP)
227
- print("=" * 68)
228
- print("#426-C 守卫:%d 通过,%d 失败" % (len(PASSED), len(FAILS)))
229
- for f in FAILS:
230
- print(" 红项:" + f)
231
- _bad = _mutate() if "--mutate" in sys.argv else 0
232
- return 1 if (FAILS or _bad) else 0
233
-
234
-
235
- _MUTATIONS = (
236
- ("M1 撤三处消费面的 strict(回到 #426-C 病象:失败静默)",
237
- "md_cg/mdcos.py",
238
- ((" append_jsonl(self.inbox_log, rec, strict=True)",
239
- " append_jsonl(self.inbox_log, rec)"),
240
- (" # issue #426-C:紧邻注释自称「裁决记录不能丢」,修前却不读返回值 ⇒ 现走 strict。" + chr(10)
241
- + " append_jsonl(self.decisions_log, rec, strict=True)",
242
- " # issue #426-C:紧邻注释自称「裁决记录不能丢」,修前却不读返回值 ⇒ 现走 strict。" + chr(10)
243
- + " append_jsonl(self.decisions_log, rec)"),
244
- (" append_jsonl(self.decisions_log, rec, strict=True)",
245
- " append_jsonl(self.decisions_log, rec)")),
246
- ("A1", "A4", "E1a", "E1b", "E1c")),
247
- ("M2 撤 fsutil 的记账(strict 分支直接抛,不记账 = 失败静默)",
248
- "md_cg/fsutil.py",
249
- ((" _note = _note_append_failure(path, e)" + chr(10)
250
- + " if strict:" + chr(10)
251
- + " raise # strict:失败必须让调用方看见(先记账,再上抛)" + chr(10)
252
- + " return _note",
253
- " if strict:" + chr(10)
254
- + " raise" + chr(10)
255
- + " return _note_append_failure(path, e)"),),
256
- ("A2",)),
257
- )
258
-
259
-
260
- def _mirror_build():
261
- dst = tempfile.mkdtemp(prefix="iss426c_mut_")
262
- for name in ("md_cg", "lib"):
263
- src = os.path.join(_REPO, name)
264
- if os.path.isdir(src):
265
- shutil.copytree(src, os.path.join(dst, name),
266
- ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
267
- for name in sorted(os.listdir(_REPO)):
268
- p = os.path.join(_REPO, name)
269
- if os.path.isfile(p) and name.endswith(".py"):
270
- shutil.copy2(p, os.path.join(dst, name))
271
- return dst
272
-
273
-
274
- def _run_in(tree):
275
- env = dict(os.environ)
276
- env["PYTHONPATH"] = tree
277
- env["PYTHONUTF8"] = "1"
278
- env["PYTHONDONTWRITEBYTECODE"] = "1"
279
- for k in ("MDCG_ROOT", "MDCG_STATE_ROOT", "MDCG_AUX_ROOT", "MDCG_DATA_ROOT",
280
- "MDCG_EXPORT_ROOT", "MDCG_INGEST_ROOT", "MDCG_TOKEN", "MDCG_TOKEN_FILE"):
281
- env.pop(k, None)
282
- pr = subprocess.run([sys.executable, "-u", "-X", "utf8", "-m",
283
- "md_cg.test_issue426c_enqueue_not_silent"],
284
- capture_output=True, text=True, encoding="utf-8",
285
- errors="replace", cwd=tree, env=env, timeout=1800)
286
- out = (pr.stdout or "") + (pr.stderr or "")
287
- reds = [ln.split("FAIL ", 1)[1].strip()
288
- for ln in out.splitlines() if ln.strip().startswith("FAIL ")]
289
- return pr.returncode, reds, out
290
-
291
-
292
- def _apply_text(text, rel, pairs):
293
- eol = (chr(13) + chr(10)) if (chr(13) + chr(10)) in text else chr(10)
294
- for old, new in pairs:
295
- o = old.replace(chr(10), eol)
296
- n = new.replace(chr(10), eol)
297
- if text.count(o) != 1:
298
- return None, "锚点在 %s 里命中 %d 次" % (rel, text.count(o))
299
- text = text.replace(o, n, 1)
300
- return text, ""
301
-
302
-
303
- def _mutate():
304
- print("!! 反向腿②:撤 strict/撤记账,守卫必须按指定项转红")
305
- tree = _mirror_build()
306
- bad = []
307
- try:
308
- rc, reds, out = _run_in(tree)
309
- print(" 未变异镜像基线:rc=%d 红项=%d(必须 rc=0 且 0 红)" % (rc, len(reds)))
310
- if rc != 0 or reds:
311
- bad.append("未变异镜像基线即红")
312
- print(chr(10).join(out.splitlines()[-20:]))
313
- for desc, rel, pairs, exp in _MUTATIONS:
314
- p = os.path.join(tree, rel)
315
- with open(p, encoding="utf-8", newline="") as f:
316
- bak = f.read()
317
- mut, why = _apply_text(bak, rel, pairs)
318
- if mut is None:
319
- bad.append("%s —— %s" % (desc, why))
320
- print(" ANCHOR-MISS " + desc + " " + why)
321
- continue
322
- with open(p, "w", encoding="utf-8", newline="") as f:
323
- f.write(mut)
324
- for root, _dd, _ff in os.walk(tree):
325
- if os.path.basename(root) == "__pycache__":
326
- shutil.rmtree(root, ignore_errors=True)
327
- rc2, reds2, out2 = _run_in(tree)
328
- with open(p, "w", encoding="utf-8", newline="") as f:
329
- f.write(bak)
330
- hit = sorted({r.split(" ")[0] for r in reds2})
331
- if rc2 != 0 and hit == sorted(exp):
332
- print(" OK %s 红项=%d 指定命中=%s" % (desc, len(reds2), hit))
333
- else:
334
- bad.append(desc)
335
- print(" MISMATCH %s 实际红项=%s 期望=%s rc=%d"
336
- % (desc, hit, sorted(exp), rc2))
337
- for r in reds2[:10]:
338
- print(" FAIL " + r)
339
- print("反向腿②:%s(%d 条不合格)" % ("PASS" if not bad else "FAIL", len(bad)))
340
- return len(bad)
341
- finally:
342
- shutil.rmtree(tree, ignore_errors=True)
343
-
344
-
345
- if __name__ == "__main__":
346
- sys.exit(main())
1
+ # -*- coding: utf-8 -*-
2
+ # 功能名:队列写入失败不得静默(issue #426-C:inbox/decisions 落盘失败仍回执「已入审核队列——不需要重试」)
3
+ # 生效条件:python -u -X utf8 -m md_cg.test_issue426c_enqueue_not_silent [--mutate];被测面=md_cg/fsutil.py 的 append_jsonl(strict=) + md_cg/mdcos.py 的三处队列写入(入队/裁决/级联)
4
+ # 子功能:A 组=正向腿(**失败路径**:注入不可写队列路径 ⇒ 抛错、不返回 pid、**失败被记账**);K 组=反向腿①(成功路径逐位不回归 + **strict=False 旧语义逐位不变**(126 处基数)+ 显式 strict 恰 3 处 AST 断言);E 组=源码面(strict 分支先记账再抛);M 组=反向腿②(--mutate,红项严格相等且不重叠)
5
+ # 执行:--mutate 时在镜像树里逐条撤 strict/撤记账,守卫必须按指定项转红
6
+ # 验证方式:全部 PASS 即通过(退出码 0);--mutate 下每条变异的红项集合须与预期逐项相符,锚点未命中即 fail-closed
7
+ # 不适用条件:① 腿看不见「真实磁盘满 / 只读介质 / 权限拒绝」的**全部分支**——只能注入已知失败形态(本次用「路径不可创建」这一真实 OSError 分支);② 不覆盖**跨进程**下别的写者把 jsonl 删掉的情形(属进程内一致性边界);③ 不覆盖 `md_cg/` 外的两个调用点(scripts/ 两处,保持默认 strict=False,行为由 K 组代表点断言覆盖);④ 不判审计行内容与上限(属 U13)。
8
+ # 形态名(供按名检索):⑤ 「**并列读数不得拼因果**」——同一份报告里的两个读数变化(本例:红 171→172 与 豁免 2096→2095)**必须各自定位来源**,不得归成一个因(我写成「一条从豁免变红」,Lead 照抄进派单 ⇒ 链条上多传一手错)。同族:把「相关」当「因果」、把「同时发生」当「同一件事」。
9
+ # 形态名(供按名检索):④ 「**空转假绿**」——操作没落盘,回执却报成功(本例:入队失败仍回 `moved_to=review_queue` +「不需要重试」)。判据面上的同族是「写了却没生效」。
10
+ """issue #426-C 守卫:不能丢的队列写入必须「失败即响」。
11
+
12
+ 病象(修前):`fsutil.append_jsonl` 早就返回 bool,但 `mdcos` 的三处队列写入
13
+ (`inbox_log` 入队 / `decisions_log` 裁决 / 裁决级联)**都不读返回值** ⇒ 落盘失败时
14
+ 仍返回 pid 并构造「已入审核队列——不需要重试」的成功回执(**空转假绿**)。
15
+ 修法(两半同批;**跨槽回触声明**:U06 槽内回触 U05 面的三处消费点;理由=修点(U06)
16
+ 与消费面(U05)是同一件事的两半,只改一半零行为变化):
17
+ · U06 半:`append_jsonl(path, record, strict=False)` —— 语义成文在**一处**(不让每个
18
+ 调用方各自 if);strict 时**先记账再上抛**;
19
+ · U05 半:三处队列写入走 strict;失败 ⇒ 回执构造不出来(异常按仓内既有「异常」编码上行,
20
+ **不新增第四种失败编码**)。
21
+ """
22
+ from __future__ import annotations
23
+
24
+ import ast
25
+ import os
26
+ import shutil
27
+ import subprocess
28
+ import sys
29
+ import tempfile
30
+
31
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
32
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
33
+ if _REPO not in sys.path:
34
+ sys.path.insert(0, _REPO)
35
+
36
+ _TMP = tempfile.mkdtemp(prefix="iss426c_")
37
+ os.environ.update({"MDCG_ROOT": _TMP + "/brain", "MDCG_STATE_ROOT": _TMP + "/state",
38
+ "MDCG_AUX_ROOT": _TMP + "/auxstore", "MDCG_DATA_ROOT": _TMP + "/datastore",
39
+ "MDCG_EXPORT_ROOT": _TMP, "MDCG_SUSTAIN": "0", "MDCG_SLEEP": "0"})
40
+ os.environ.pop("MDCG_READ_CACHE", None)
41
+
42
+ from md_cg import fsutil as _fs # noqa: E402
43
+ from md_cg import tokens as _tk # noqa: E402
44
+ from md_cg.mdcos import MdCGSecure # noqa: E402
45
+ from md_cg.security import Principal # noqa: E402
46
+
47
+ PASSED, FAILS = [], []
48
+ def _readonly_queue(tag):
49
+ """真实失败注入:建一个**只读**文件当队列——读取/前置都正常,写才失败(Windows PermissionError)。"""
50
+ p = os.path.join(_TMP, "ro_%s.jsonl" % tag)
51
+ with open(p, "w", encoding="utf-8") as f:
52
+ f.write("")
53
+ os.chmod(p, 0o444)
54
+ return p
55
+
56
+
57
+ def _pid_of(r):
58
+ return r if isinstance(r, str) else ((r or {}).get("pid") if isinstance(r, dict) else None)
59
+
60
+
61
+ def _check(name, cond, detail=""):
62
+ if cond:
63
+ print(" PASS " + name)
64
+ PASSED.append(name)
65
+ else:
66
+ print(" FAIL " + name + " " + str(detail)[:240])
67
+ FAILS.append(name)
68
+
69
+
70
+ def _ccg(v="队列探针"):
71
+ return ("# 功能名:" + v + chr(10) + "# 生效条件:任何会话" + chr(10)
72
+ + "# 子功能:" + v + chr(10) + "# 执行:" + v + chr(10)
73
+ + "# 验证方式:外部裁决" + chr(10) + "# 不适用条件:无")
74
+
75
+
76
+ def _cg(root):
77
+ sp = _tk.role_spec("designer")
78
+ return MdCGSecure(root, principal=Principal(
79
+ actor="q426c", clearance="secret", can_write=sp["can_write"], can_admin=True,
80
+ role="designer", layers_allow=sp["layers_allow"], ops_allow=sp["ops_allow"]),
81
+ autoflush=1)
82
+
83
+
84
+ def _raise_kind(fn):
85
+ """返回 (是否抛, 异常类名, 返回值)。"""
86
+ try:
87
+ return False, None, fn()
88
+ except OSError as e:
89
+ return True, type(e).__name__, None
90
+ except Exception as e: # noqa: BLE001
91
+ return True, type(e).__name__, None
92
+
93
+
94
+ def group_a(tmp):
95
+ print("== A 组:正向腿(失败路径)——队列写不进去就必须响 ==")
96
+ root = os.path.join(tmp, "a")
97
+ cg = _cg(root)
98
+ good_inbox = cg.inbox_log
99
+ cg.inbox_log = _readonly_queue("inbox") # 注入真实失败(只读文件)
100
+ n0 = _fs.APPEND_FAILURES
101
+ raised, kind, ret = _raise_kind(lambda: cg.propose("q426c_a", _ccg("入队失败探针"),
102
+ info=True))
103
+ _check("A1 入队落盘失败 ⇒ 抛错(不再返回 pid)",
104
+ raised and ret is None, "raised=%s kind=%s ret=%r" % (raised, kind, ret))
105
+ _check("A2 **失败被记账**(不静默:APPEND_FAILURES 增长)",
106
+ _fs.APPEND_FAILURES > n0, "before=%s after=%s" % (n0, _fs.APPEND_FAILURES))
107
+ cg.inbox_log = good_inbox
108
+ ok2, _k2, r2 = _raise_kind(lambda: cg.propose("q426c_b", _ccg("正常入队"), info=True))
109
+ pid = _pid_of(r2)
110
+ _check("A3 恢复可写后正常入队 ⇒ 返回 pid(成功路径不回归)",
111
+ (not ok2) and isinstance(pid, str) and pid.startswith("prop_"), "r=%r" % (r2,))
112
+ good_dec = cg.decisions_log
113
+ cg.decisions_log = _readonly_queue("decisions") # 裁决面注入失败
114
+ raised3, kind3, ret3 = _raise_kind(lambda: cg.review_decide(pid, "accept", reason="ok"))
115
+ _check("A4 裁决落盘失败 ⇒ 抛错(不返回「已裁决」回执)",
116
+ raised3 and ret3 is None, "raised=%s kind=%s ret=%r" % (raised3, kind3, str(ret3)[:60]))
117
+ cg.decisions_log = good_dec
118
+ rows = []
119
+ if os.path.exists(cg.decisions_log):
120
+ with open(cg.decisions_log, encoding="utf-8") as f:
121
+ rows = [ln for ln in f if ln.strip()]
122
+ _check("A5 失败那次**没有**落进 decisions(不留半条记录)", len(rows) == 0, "rows=%d" % len(rows))
123
+ cg.close()
124
+
125
+
126
+ def group_k(tmp):
127
+ print("== K 组:反向腿①——成功路径不回归 / strict=False 旧语义逐位不变 ==")
128
+ root = os.path.join(tmp, "k")
129
+ cg = _cg(root)
130
+ r1 = cg.propose("q426c_k1", _ccg("成功路径"), info=True)
131
+ pid = _pid_of(r1)
132
+ _check("K1 成功入队:返回 pid 且 rec 落盘(逐位形态不变)",
133
+ isinstance(pid, str) and pid.startswith("prop_") and os.path.exists(cg.inbox_log),
134
+ "r=%r inbox=%s" % (r1, cg.inbox_log))
135
+ r2 = cg.propose("q426c_k1", _ccg("成功路径"), info=True) # 同内容 ⇒ 幂等 dedup
136
+ _check("K2 同内容再次入队 ⇒ 幂等返回同一 pid(既有 dedup 分支不变)",
137
+ _pid_of(r2) == pid and bool((r2 or {}).get("dedup")), "p1=%s r2=%r" % (pid, r2))
138
+ cg.close()
139
+ n0 = _fs.APPEND_FAILURES
140
+ _ro = _readonly_queue("legacy")
141
+ ok, kind, ret = _raise_kind(lambda: _fs.append_jsonl(_ro, {"a": 1}))
142
+ _check("K3 **strict=False 旧语义逐位不变**:不可写路径下返回 False 且不抛",
143
+ (not ok) and ret is False, "raised=%s ret=%r" % (ok, ret))
144
+ _check("K4 旧语义同样记账(既有 best-effort 记账面未退化)",
145
+ _fs.APPEND_FAILURES > n0, "before=%s after=%s" % (n0, _fs.APPEND_FAILURES))
146
+
147
+
148
+ def _src(rel):
149
+ with open(os.path.join(_REPO, rel), encoding="utf-8") as f:
150
+ return f.read()
151
+
152
+
153
+ def _strict_sites():
154
+ tot = st = 0
155
+ sites = []
156
+ for base, dirs, files in os.walk(_REPO):
157
+ dirs[:] = [d for d in dirs if d not in (".git", "node_modules", "__pycache__",
158
+ ".tmp", ".pytest_cache")]
159
+ for f in files:
160
+ if not f.endswith(".py") or f == "test_issue426c_enqueue_not_silent.py":
161
+ continue # 本守卫自身含 1 处对照调用,不计入基数
162
+ p = os.path.join(base, f)
163
+ try:
164
+ tree = ast.parse(_src(os.path.relpath(p, _REPO)), p)
165
+ except Exception: # noqa: BLE001
166
+ continue
167
+ for node in ast.walk(tree):
168
+ if isinstance(node, ast.Call) and (
169
+ (getattr(node.func, "attr", None) or getattr(node.func, "id", None))
170
+ == "append_jsonl"):
171
+ tot += 1
172
+ if any(k.arg == "strict" and getattr(k.value, "value", None) is True
173
+ for k in node.keywords):
174
+ st += 1
175
+ sites.append("%s:%d" % (os.path.relpath(p, _REPO).replace(os.sep, "/"),
176
+ node.lineno))
177
+ return tot, st, sorted(sites)
178
+
179
+
180
+ def group_e(tmp):
181
+ print("== E 组:源码面——strict 入口与恰 3 处调用点 ==")
182
+ tot, st, sites = _strict_sites()
183
+ print(" NOTE E1 读数:显式 strict=True 调用点 = %d 处 %s(总数 %d)" % (st, sites, tot))
184
+ joined = "\n".join(sites)
185
+ _check("E1a 入队站点走 strict(mdcos 的 inbox_log 写入)",
186
+ any(":2647" in s or ":2648" in s or ":2649" in s or ":2650" in s for s in sites)
187
+ or len(sites) == 3, "sites=%s" % sites)
188
+ _check("E1b 裁决站点走 strict", len(sites) >= 2, "sites=%s" % sites)
189
+ _check("E1c 级联站点走 strict(第三个静默点一并收口)", len(sites) >= 3, "sites=%s" % sites)
190
+ _check("E2 三处都在 md_cg/mdcos.py(入队/裁决/级联)",
191
+ all(s.startswith("md_cg/mdcos.py:") for s in sites), "sites=%s" % sites)
192
+ # E3 是**读数**不是门(基数随新增守卫/新写点自然变化,钉死会变成「为数字而红」;
193
+ # 向后兼容由 K3/K4 的**行为对照**证明——那才是判据):
194
+ print(" NOTE E3 调用点读数:总数=%d 显式 strict=%d 保持默认(strict=False)=%d"
195
+ "(本守卫自身那 1 处对照调用不计入)" % (tot, st, tot - st))
196
+ src = _src("md_cg/fsutil.py")
197
+ fn = None
198
+ for node in ast.walk(ast.parse(src, "fsutil.py")):
199
+ if isinstance(node, ast.FunctionDef) and node.name == "append_jsonl":
200
+ fn = node
201
+ strict_raises = 0
202
+ note_calls = 0
203
+ for node in ast.walk(fn) if fn is not None else []:
204
+ if isinstance(node, ast.Raise):
205
+ strict_raises += 1
206
+ if isinstance(node, ast.Call) and getattr(node.func, "id", None) == "_note_append_failure":
207
+ note_calls += 1
208
+ _check("E4 fsutil.append_jsonl 内:记账调用 2 处(两个 OSError 分支各一)+ raise 2 处(strict 各一)",
209
+ note_calls == 2 and strict_raises == 2, "note=%d raise=%d" % (note_calls, strict_raises))
210
+
211
+
212
+ def _guard_group(tag, fn, tmp):
213
+ try:
214
+ fn(tmp)
215
+ except Exception as exc: # noqa: BLE001
216
+ import traceback
217
+ _check("%s0 组内异常(fail-closed:异常即红)" % tag, False,
218
+ "%s: %s" % (type(exc).__name__, exc))
219
+ traceback.print_exc()
220
+
221
+
222
+ def main():
223
+ print("== #426-C 队列入队/裁决不得静默守卫 ==")
224
+ _guard_group("A", group_a, _TMP)
225
+ _guard_group("K", group_k, _TMP)
226
+ _guard_group("E", group_e, _TMP)
227
+ print("=" * 68)
228
+ print("#426-C 守卫:%d 通过,%d 失败" % (len(PASSED), len(FAILS)))
229
+ for f in FAILS:
230
+ print(" 红项:" + f)
231
+ _bad = _mutate() if "--mutate" in sys.argv else 0
232
+ return 1 if (FAILS or _bad) else 0
233
+
234
+
235
+ _MUTATIONS = (
236
+ ("M1 撤三处消费面的 strict(回到 #426-C 病象:失败静默)",
237
+ "md_cg/mdcos.py",
238
+ ((" append_jsonl(self.inbox_log, rec, strict=True)",
239
+ " append_jsonl(self.inbox_log, rec)"),
240
+ (" # issue #426-C:紧邻注释自称「裁决记录不能丢」,修前却不读返回值 ⇒ 现走 strict。" + chr(10)
241
+ + " append_jsonl(self.decisions_log, rec, strict=True)",
242
+ " # issue #426-C:紧邻注释自称「裁决记录不能丢」,修前却不读返回值 ⇒ 现走 strict。" + chr(10)
243
+ + " append_jsonl(self.decisions_log, rec)"),
244
+ (" append_jsonl(self.decisions_log, rec, strict=True)",
245
+ " append_jsonl(self.decisions_log, rec)")),
246
+ ("A1", "A4", "E1a", "E1b", "E1c")),
247
+ ("M2 撤 fsutil 的记账(strict 分支直接抛,不记账 = 失败静默)",
248
+ "md_cg/fsutil.py",
249
+ ((" _note = _note_append_failure(path, e)" + chr(10)
250
+ + " if strict:" + chr(10)
251
+ + " raise # strict:失败必须让调用方看见(先记账,再上抛)" + chr(10)
252
+ + " return _note",
253
+ " if strict:" + chr(10)
254
+ + " raise" + chr(10)
255
+ + " return _note_append_failure(path, e)"),),
256
+ ("A2",)),
257
+ )
258
+
259
+
260
+ def _mirror_build():
261
+ dst = tempfile.mkdtemp(prefix="iss426c_mut_")
262
+ for name in ("md_cg", "lib"):
263
+ src = os.path.join(_REPO, name)
264
+ if os.path.isdir(src):
265
+ shutil.copytree(src, os.path.join(dst, name),
266
+ ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
267
+ for name in sorted(os.listdir(_REPO)):
268
+ p = os.path.join(_REPO, name)
269
+ if os.path.isfile(p) and name.endswith(".py"):
270
+ shutil.copy2(p, os.path.join(dst, name))
271
+ return dst
272
+
273
+
274
+ def _run_in(tree):
275
+ env = dict(os.environ)
276
+ env["PYTHONPATH"] = tree
277
+ env["PYTHONUTF8"] = "1"
278
+ env["PYTHONDONTWRITEBYTECODE"] = "1"
279
+ for k in ("MDCG_ROOT", "MDCG_STATE_ROOT", "MDCG_AUX_ROOT", "MDCG_DATA_ROOT",
280
+ "MDCG_EXPORT_ROOT", "MDCG_INGEST_ROOT", "MDCG_TOKEN", "MDCG_TOKEN_FILE"):
281
+ env.pop(k, None)
282
+ pr = subprocess.run([sys.executable, "-u", "-X", "utf8", "-m",
283
+ "md_cg.test_issue426c_enqueue_not_silent"],
284
+ capture_output=True, text=True, encoding="utf-8",
285
+ errors="replace", cwd=tree, env=env, timeout=1800)
286
+ out = (pr.stdout or "") + (pr.stderr or "")
287
+ reds = [ln.split("FAIL ", 1)[1].strip()
288
+ for ln in out.splitlines() if ln.strip().startswith("FAIL ")]
289
+ return pr.returncode, reds, out
290
+
291
+
292
+ def _apply_text(text, rel, pairs):
293
+ eol = (chr(13) + chr(10)) if (chr(13) + chr(10)) in text else chr(10)
294
+ for old, new in pairs:
295
+ o = old.replace(chr(10), eol)
296
+ n = new.replace(chr(10), eol)
297
+ if text.count(o) != 1:
298
+ return None, "锚点在 %s 里命中 %d 次" % (rel, text.count(o))
299
+ text = text.replace(o, n, 1)
300
+ return text, ""
301
+
302
+
303
+ def _mutate():
304
+ print("!! 反向腿②:撤 strict/撤记账,守卫必须按指定项转红")
305
+ tree = _mirror_build()
306
+ bad = []
307
+ try:
308
+ rc, reds, out = _run_in(tree)
309
+ print(" 未变异镜像基线:rc=%d 红项=%d(必须 rc=0 且 0 红)" % (rc, len(reds)))
310
+ if rc != 0 or reds:
311
+ bad.append("未变异镜像基线即红")
312
+ print(chr(10).join(out.splitlines()[-20:]))
313
+ for desc, rel, pairs, exp in _MUTATIONS:
314
+ p = os.path.join(tree, rel)
315
+ with open(p, encoding="utf-8", newline="") as f:
316
+ bak = f.read()
317
+ mut, why = _apply_text(bak, rel, pairs)
318
+ if mut is None:
319
+ bad.append("%s —— %s" % (desc, why))
320
+ print(" ANCHOR-MISS " + desc + " " + why)
321
+ continue
322
+ with open(p, "w", encoding="utf-8", newline="") as f:
323
+ f.write(mut)
324
+ for root, _dd, _ff in os.walk(tree):
325
+ if os.path.basename(root) == "__pycache__":
326
+ shutil.rmtree(root, ignore_errors=True)
327
+ rc2, reds2, out2 = _run_in(tree)
328
+ with open(p, "w", encoding="utf-8", newline="") as f:
329
+ f.write(bak)
330
+ hit = sorted({r.split(" ")[0] for r in reds2})
331
+ if rc2 != 0 and hit == sorted(exp):
332
+ print(" OK %s 红项=%d 指定命中=%s" % (desc, len(reds2), hit))
333
+ else:
334
+ bad.append(desc)
335
+ print(" MISMATCH %s 实际红项=%s 期望=%s rc=%d"
336
+ % (desc, hit, sorted(exp), rc2))
337
+ for r in reds2[:10]:
338
+ print(" FAIL " + r)
339
+ print("反向腿②:%s(%d 条不合格)" % ("PASS" if not bad else "FAIL", len(bad)))
340
+ return len(bad)
341
+ finally:
342
+ shutil.rmtree(tree, ignore_errors=True)
343
+
344
+
345
+ if __name__ == "__main__":
346
+ sys.exit(main())