@furongjun1999/dsh-memory 0.9.6 → 0.9.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (233) hide show
  1. package/README.md +150 -662
  2. package/codebuddy/CODEBUDDY.md +333 -204
  3. package/data/policy.json +38 -38
  4. package/docs/README.md +18 -0
  5. package/docs/discipline/harnesses.yaml +11 -3
  6. package/docs/discipline/templates/compact.txt.tmpl +20 -18
  7. package/docs/discipline/templates/full.md.tmpl +63 -61
  8. package/docs/discipline/templates/rules.mdc.tmpl +5 -3
  9. package/docs/discipline/templates/skill.md.tmpl +25 -23
  10. package/docs/discipline/templates/spec.txt.tmpl +2 -2
  11. package/docs/discipline/templates/zcode-user.md.tmpl +1 -1
  12. package/dsh/cordis.yml.example +139 -139
  13. package/lib/hooks.d.ts +19 -0
  14. package/lib/hooks.js +30 -7
  15. package/lib/index.js +18 -3
  16. package/lib/init.js +19 -4
  17. package/lib/lib/hook_audit.js +64 -2
  18. package/lib/lib/prompt_safety.js +14 -3
  19. package/lib/tools.d.ts +7 -0
  20. package/lib/tools.js +22 -1
  21. package/md_cg/_guard_kit.py +121 -121
  22. package/md_cg/anchor.py +114 -114
  23. package/md_cg/audit.py +15 -2
  24. package/md_cg/chain.py +3 -2
  25. package/md_cg/config_registry_guard.py +487 -487
  26. package/md_cg/conformance.py +747 -747
  27. package/md_cg/consolidate.py +1793 -1793
  28. package/md_cg/crypto.py +794 -794
  29. package/md_cg/hyperedge.py +262 -262
  30. package/md_cg/interface_schema.py +146 -146
  31. package/md_cg/lifecycle.py +426 -426
  32. package/md_cg/mdcos.py +12 -8
  33. package/md_cg/predict.py +1103 -1103
  34. package/md_cg/protect.py +654 -654
  35. package/md_cg/provenance.py +606 -606
  36. package/md_cg/queue_health.py +130 -130
  37. package/md_cg/refine.py +613 -613
  38. package/md_cg/retrieve.py +128 -128
  39. package/md_cg/revise.py +140 -140
  40. package/md_cg/rollback.py +657 -657
  41. package/md_cg/rollback_cli.py +124 -124
  42. package/md_cg/rotate.py +346 -346
  43. package/md_cg/routing.py +490 -490
  44. package/md_cg/self_surface.py +50 -50
  45. package/md_cg/semantic/canonical.py +144 -144
  46. package/md_cg/semantic/zh_en_atoms.py +141 -141
  47. package/md_cg/sources.py +1091 -1091
  48. package/md_cg/stg.py +3 -2
  49. package/md_cg/sustain.py +10 -1
  50. package/md_cg/test_audit_rotate_parity.py +279 -279
  51. package/md_cg/test_autonomy_modes.py +2013 -2013
  52. package/md_cg/test_cn_recall_grams.py +206 -206
  53. package/md_cg/test_code_kind_forbidden.py +101 -0
  54. package/md_cg/test_condaccept.py +156 -156
  55. package/md_cg/test_condgate.py +141 -141
  56. package/md_cg/test_heartbeat_ledger.py +9 -3
  57. package/md_cg/test_i50e_readside_protection.py +658 -658
  58. package/md_cg/test_issue111_gate_order.py +76 -76
  59. package/md_cg/test_issue111_scoring_contract.py +86 -86
  60. package/md_cg/test_issue121_calibration_bounds.py +293 -293
  61. package/md_cg/test_issue145_sleep_no_silent_rollback.py +167 -167
  62. package/md_cg/test_issue147_pack_id_escape.py +378 -378
  63. package/md_cg/test_issue148_merge_downgrade_leak.py +192 -192
  64. package/md_cg/test_issue149_accel_default_face.py +248 -248
  65. package/md_cg/test_issue150_empty_query_terms.py +328 -328
  66. package/md_cg/test_issue151_153_157_write_cache_index.py +151 -151
  67. package/md_cg/test_issue152_sealed_postings.py +309 -309
  68. package/md_cg/test_issue153b_index_generation_invalidation.py +142 -142
  69. package/md_cg/test_issue157b_index_remainder.py +344 -344
  70. package/md_cg/test_issue189_conclusion_bidirectional.py +223 -223
  71. package/md_cg/test_issue191_import_trust.py +382 -382
  72. package/md_cg/test_issue193_write_scope.py +378 -378
  73. package/md_cg/test_issue212_writeface_gaps.py +349 -349
  74. package/md_cg/test_issue214_reject_sensitivity.py +266 -266
  75. package/md_cg/test_issue215_condition_source_gap.py +272 -272
  76. package/md_cg/test_issue216_clash_routes.py +214 -214
  77. package/md_cg/test_issue217_slot_routes.py +248 -248
  78. package/md_cg/test_issue218_postcondition_value_blind.py +222 -222
  79. package/md_cg/test_issue258_pack_sensitivity.py +468 -468
  80. package/md_cg/test_issue265_coldverify_multiproc.py +438 -438
  81. package/md_cg/test_issue274_read_node_id_none.py +341 -341
  82. package/md_cg/test_issue275_layer_param.py +250 -250
  83. package/md_cg/test_issue277_node_id_traversal.py +384 -384
  84. package/md_cg/test_issue278_neg_separated_tension.py +271 -271
  85. package/md_cg/test_issue278_neg_window.py +294 -294
  86. package/md_cg/test_issue278_negative_layering.py +83 -83
  87. package/md_cg/test_issue284_skip_not_pass.py +291 -291
  88. package/md_cg/test_issue294_flywheel_sensitivity.py +573 -573
  89. package/md_cg/test_issue301_baseline_corpus_gate.py +245 -245
  90. package/md_cg/test_issue303_design_boundaries.py +262 -262
  91. package/md_cg/test_issue315_merge_branch_sensitivity.py +383 -383
  92. package/md_cg/test_issue319_tenant_unregistered_bypass.py +256 -256
  93. package/md_cg/test_issue321_mcp_deep_json.py +304 -304
  94. package/md_cg/test_issue322_node_write_lock.py +727 -727
  95. package/md_cg/test_issue324_write_size_fields.py +214 -214
  96. package/md_cg/test_issue325_audit_poison.py +229 -229
  97. package/md_cg/test_issue326_ingest_auto_whitelist.py +331 -331
  98. package/md_cg/test_issue326_key_perms.py +355 -355
  99. package/md_cg/test_issue326_twin_parser_parity.py +323 -323
  100. package/md_cg/test_issue326_vm_call_frame.py +569 -569
  101. package/md_cg/test_issue345_writelimit_effective_sens.py +233 -233
  102. package/md_cg/test_issue346_overwrite_sensitivity_gate.py +424 -424
  103. package/md_cg/test_issue348_insight_verify_op.py +199 -199
  104. package/md_cg/test_issue348_merge_rmw_lock.py +345 -345
  105. package/md_cg/test_issue350_casefold_collision.py +235 -235
  106. package/md_cg/test_issue351_ingest_root_fallback.py +201 -201
  107. package/md_cg/test_issue352_imgaudit.py +418 -418
  108. package/md_cg/test_issue352_pathsjson.py +537 -537
  109. package/md_cg/test_issue352_refindex.py +412 -412
  110. package/md_cg/test_issue352_rollback.py +604 -604
  111. package/md_cg/test_issue354_p2_writelimit_toctou.py +327 -327
  112. package/md_cg/test_issue354_rust_index_containment.py +571 -571
  113. package/md_cg/test_issue354_rust_wal_containment.py +606 -606
  114. package/md_cg/test_issue358_truncation_observable.py +247 -247
  115. package/md_cg/test_issue360_tidy_private_excerpt.py +195 -195
  116. package/md_cg/test_issue361_blindspot_not_silent_drop.py +494 -494
  117. package/md_cg/test_issue362_neg_condition.py +468 -468
  118. package/md_cg/test_issue363_lexical_idf.py +275 -275
  119. package/md_cg/test_issue370_readcache_assembly.py +488 -488
  120. package/md_cg/test_issue376_heartbeat_failed.py +293 -293
  121. package/md_cg/test_issue377_layer_protect_stamp.py +324 -324
  122. package/md_cg/test_issue378_protected_floor.py +310 -310
  123. package/md_cg/test_issue383_state_event_write_gate.py +462 -462
  124. package/md_cg/test_issue397_sens_eff_wiring.py +435 -435
  125. package/md_cg/test_issue398_toolface_op_parity.py +511 -511
  126. package/md_cg/test_issue399_hive_json_shape.py +317 -317
  127. package/md_cg/test_issue401_wisdom_codegen.py +295 -295
  128. package/md_cg/test_issue404_heal_receipt.py +389 -389
  129. package/md_cg/test_issue409_compact_index_completeness.py +509 -509
  130. package/md_cg/test_issue410_verify_demote_write.py +366 -366
  131. package/md_cg/test_issue411_scrub_idempotence.py +575 -575
  132. package/md_cg/test_issue412_discipline_producer_parity.py +311 -311
  133. package/md_cg/test_issue413_preimage_failclosed.py +239 -239
  134. package/md_cg/test_issue413_preimage_twin.py +339 -339
  135. package/md_cg/test_issue415_deps_gate_probe.py +357 -357
  136. package/md_cg/test_issue416_entry_key_subset.py +525 -525
  137. package/md_cg/test_issue417_twophase_merge_landed.py +423 -423
  138. package/md_cg/test_issue425_ingest_library_path.py +331 -331
  139. package/md_cg/test_issue426c_enqueue_not_silent.py +346 -346
  140. package/md_cg/test_issue426e_restore_overwrite_live.py +310 -310
  141. package/md_cg/test_issue426f_paths_gbk_failclosed.py +301 -301
  142. package/md_cg/test_issue427_serve_status_shape.py +191 -191
  143. package/md_cg/test_issue431_432_468_protect_single_point.py +322 -322
  144. package/md_cg/test_issue433_outlook_threshold_single_point.py +278 -278
  145. package/md_cg/test_issue434_435_float_gate.py +90 -90
  146. package/md_cg/test_issue439_sibling_sensitivity_inherit.py +448 -448
  147. package/md_cg/test_issue43_default_policy.py +874 -874
  148. package/md_cg/test_issue444_scrub_evidence_plaintext.py +281 -281
  149. package/md_cg/test_issue445_metacognition_ledger_gate.py +454 -454
  150. package/md_cg/test_issue446_453_403_verified_evidence.py +382 -382
  151. package/md_cg/test_issue450_sensitivity_fallback_reason.py +51 -51
  152. package/md_cg/test_issue450_writegate_unknown_sens.py +203 -203
  153. package/md_cg/test_issue451_audit_actor.py +313 -313
  154. package/md_cg/test_issue452_ingest_root_sensitivity.py +299 -299
  155. package/md_cg/test_issue463_ghost_prune_guard.py +259 -259
  156. package/md_cg/test_issue466_basis_registry.py +469 -469
  157. package/md_cg/test_issue467_identity_sensitivity_parity.py +360 -360
  158. package/md_cg/test_issue469_scrub_override_passthrough.py +126 -126
  159. package/md_cg/test_issue470_wal_event_durable.py +427 -427
  160. package/md_cg/test_issue471_identity_readable.py +136 -136
  161. package/md_cg/test_issue472_audit_chain.py +90 -90
  162. package/md_cg/test_issue472_prune_ledger.py +105 -105
  163. package/md_cg/test_issue472_rollback_degraded.py +81 -81
  164. package/md_cg/test_issue491_compose_sandbox.py +460 -460
  165. package/md_cg/test_issue498_merge_trace_stale_index.py +280 -280
  166. package/md_cg/test_issue511_scoreboard_bad_files.py +256 -256
  167. package/md_cg/test_issue521_rotate_lock_suffix.py +110 -110
  168. package/md_cg/test_issue524_orphan_noise_no_demote.py +88 -88
  169. package/md_cg/test_issue525_read_null_semantics.py +52 -52
  170. package/md_cg/test_issue547_append_jsonl_whitelist.py +114 -114
  171. package/md_cg/test_issue547_audit_durable.py +96 -96
  172. package/md_cg/test_issue557_r2_write_entries_size_gate.py +83 -83
  173. package/md_cg/test_issue559_refine_prefix.py +69 -69
  174. package/md_cg/test_issue562_retired_faces.py +150 -0
  175. package/md_cg/test_issue71_queue_backlog.py +376 -376
  176. package/md_cg/test_issue72_coldverify_controlled_drain.py +420 -420
  177. package/md_cg/test_issue82_credential_rules.py +271 -271
  178. package/md_cg/test_issue91_verifier_sandbox.py +314 -314
  179. package/md_cg/test_issue93_remember_redaction.py +282 -282
  180. package/md_cg/test_logref.py +1141 -1141
  181. package/md_cg/test_md_access_parity.py +330 -330
  182. package/md_cg/test_md_cg_no_hive_dep.py +139 -139
  183. package/md_cg/test_mutation_residue.py +119 -119
  184. package/md_cg/test_mutation_rollback.py +835 -835
  185. package/md_cg/test_n123_derive_expiry_chain.py +210 -210
  186. package/md_cg/test_n184_keys_concurrent_provision.py +1 -1
  187. package/md_cg/test_n209_verify_write_face_gates.py +466 -466
  188. package/md_cg/test_n62_tenant_bind_failclosed.py +200 -200
  189. package/md_cg/test_neg_tail_honesty.py +733 -733
  190. package/md_cg/test_p21_tokens.py +294 -294
  191. package/md_cg/test_p27_docindex.py +792 -792
  192. package/md_cg/test_p28_refcheck.py +1053 -1053
  193. package/md_cg/test_p2_six_elements.py +476 -476
  194. package/md_cg/test_p33_ccg_wiring.py +300 -300
  195. package/md_cg/test_p3_legacy_closure.py +438 -438
  196. package/md_cg/test_p40_refine_worklist.py +244 -244
  197. package/md_cg/test_p4_freshness.py +781 -781
  198. package/md_cg/test_p9c_dedup_hints.py +288 -288
  199. package/md_cg/test_policy_required_ccg.py +444 -444
  200. package/md_cg/test_read_scope_b27.py +2 -2
  201. package/md_cg/test_retr_s5.py +312 -312
  202. package/md_cg/test_s1_ast_judge.py +109 -109
  203. package/md_cg/test_security_audit_v21.py +249 -249
  204. package/md_cg/test_state_event_op.py +521 -521
  205. package/md_cg/test_stgidx_index_parity.py +1024 -1024
  206. package/md_cg/test_sustain_bad_stamp.py +178 -0
  207. package/md_cg/test_sustain_bounded.py +652 -652
  208. package/md_cg/test_trust_edge_reachability.py +405 -405
  209. package/md_cg/test_w7_redlines.py +659 -659
  210. package/md_cg/test_write_return_shape.py +919 -919
  211. package/md_cg/test_writepipe.py +228 -228
  212. package/md_cg/trust.py +1114 -1114
  213. package/md_cg/twophase.py +303 -303
  214. package/md_cg/whitebox.py +536 -536
  215. package/md_cg/whitebox_kb/wisdom/compose_engine.py +1634 -1634
  216. package/md_cg/whitebox_kb/wisdom/core/condition_vm.py +318 -318
  217. package/md_cg/whitebox_kb/wisdom/core/parser.py +1082 -1082
  218. package/md_cg/whitebox_kb/wisdom/md_access.py +490 -490
  219. package/md_cg/whitebox_kb/wisdom/migrate_wisdom.py +497 -497
  220. package/md_cg/whitebox_kb/wisdom/mini_python.py +995 -995
  221. package/md_cg/whitebox_kb/wisdom/net_units.py +2900 -2900
  222. package/md_cg/whitebox_kb/wisdom/semantic_translate.py +4060 -4060
  223. package/md_cg/writepipe.py +1337 -1337
  224. package/package.json +3 -2
  225. package/scripts/workspace_index.py +328 -0
  226. package/src/hooks.ts +932 -908
  227. package/src/index.ts +21 -6
  228. package/src/init.ts +16 -4
  229. package/src/lib/hook_audit.ts +48 -2
  230. package/src/lib/mdcg_client.ts +469 -469
  231. package/src/lib/prompt_safety.ts +17 -3
  232. package/src/tools.ts +23 -1
  233. package/zcode/AGENTS.md +333 -204
@@ -1,331 +1,331 @@
1
- # -*- coding: utf-8 -*-
2
- # 功能名:摄取链不得把记忆库内部(库根/层目录/trash/保护历史)当外部文档重新索引——issue #425
3
- # 生效条件:python -X utf8 -m md_cg.test_issue425_ingest_library_path [--mutate];被测面=md_cg/sources.py::library_rel/library_reserved/library_ingest_guard/library_skip_rel 与 ingest_file/ingest_dir/ingest_jsonl 的过闸点
4
- # 子功能:A 组=显式 path=<库根> 与 action=file 指向 trash/<id>.md 时 restricted 正文/已删除节点不被访客检索到;B 组=白名单=库根上级目录时 dir 空 path 整枝剪掉库根;K 组=非恒真对照(库外目录照常摄取、库根下普通子目录照常摄取、private 节点不泄露)
5
- # 执行:--mutate 时在镜像树里回退单文件闸 / 目录闸 / 剪枝,守卫必须按指定项转红
6
- # 验证方式:全部 PASS 即通过(退出码 0);默认模式另做源码锚点自检(关键判据字面量缺失 ⇒ exit 2,fail-closed);--mutate 模式红项集合须与预期逐项相符
7
- # 不适用条件:不判真实 MCP stdio 子进程面(本守卫走 `mcp_server._dispatch`,即 tools/call 同一分派函数);不判 `op=index_doc/index_code` 的库内重索引(那条链保留源密级与保护闸,不在本条);不判库根下**普通子目录**里存放的库内节点副本(调用方自担);不判存量已泄露的 doc_* 节点清理
8
- """issue #425 守卫:库内节点文件 / 回收站 / 保护历史**不是外部文档**。
9
-
10
- 病象(修前):`cg(op=ingest, action=dir, path=<库根>)` 或 `action=file` 指向
11
- `trash/<id>.md` 时,摄取链把库内 md 当外部文档重新切成 `doc_*` 节点——
12
- restricted 正文被抄成 internal(无令牌访客可检索)、已 forget+accept 删除的
13
- 节点从 trash 复活。`MDCG_INGEST_ROOT` 设成库根上级目录时,空 path 的 dir 摄取
14
- 同样扫进整个库根。
15
-
16
- 修法(条件层):新增库内路径闸单点 `library_ingest_guard`(库根 / 层目录 /
17
- trash / _protected_history / hippocampus 等保留子树一律 fail-closed),
18
- `ingest_file` / `ingest_dir` / `ingest_jsonl` 三入口过同一闸;`ingest_dir` 在
19
- root 是库根**祖先**时把库根整枝剪进 `skip_dirs`(复用 codeindex.skip_matcher
20
- 唯一实现),并在回执里如实回报 `library_pruned`。
21
- """
22
- import json
23
- import os
24
- import shutil
25
- import subprocess
26
- import sys
27
- import tempfile
28
-
29
- sys.stdout.reconfigure(encoding="utf-8", errors="replace")
30
- _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
31
- if _REPO not in sys.path:
32
- sys.path.insert(0, _REPO)
33
-
34
- for _k in [k for k in os.environ if k.startswith("MDCG_")]:
35
- del os.environ[_k]
36
-
37
- FAILS = []
38
- _T = tempfile.mkdtemp(prefix="iss425_")
39
- ROOT = os.path.join(_T, "brain")
40
- EXT = os.path.join(_T, "extsrc")
41
- OUTSIDE = os.path.join(_T, "outside_root")
42
- os.environ.update(HOME=_T, MDCG_ROOT=ROOT,
43
- MDCG_STATE_ROOT=os.path.join(_T, "stateroot"),
44
- MDCG_AUX_ROOT=os.path.join(_T, "auxroot"),
45
- MDCG_DATA_ROOT=os.path.join(_T, "dataroot"),
46
- MDCG_EXPORT_ROOT=_T, MDCG_SUSTAIN="0", MDCG_SLEEP="0")
47
-
48
- from md_cg.mdcos import MdCGSecure # noqa: E402
49
- from md_cg.security import Principal # noqa: E402
50
- from md_cg.tokens import role_spec # noqa: E402
51
- from md_cg import mcp_server as ms # noqa: E402
52
- from md_cg import sources # noqa: E402
53
-
54
- HDR = "# 功能名:{0}\n# 生效条件:无条件\n# 执行:见下\n\n## 细节\n"
55
- REST = HDR.format("运维处置") + "RESTMK1 跳板机 10.0.0.7,误删主库后的处置细节与回滚步骤记录如下。" * 6
56
- FORGOT = HDR.format("前任联系方式") + "FORGOTMK 前任手机 13800001111 住址 某路 12 号,已决定忘掉这条。" * 6
57
- PLAIN = HDR.format("外部资料") + "EXTMK 公开资料正文,可正常摄取。" * 8
58
-
59
-
60
- def _check(name, cond, detail=""):
61
- if cond:
62
- print(" PASS " + name)
63
- else:
64
- print(" FAIL " + name + " " + str(detail)[:220])
65
- FAILS.append(name)
66
-
67
-
68
- def P(role, clr):
69
- s = role_spec(role)
70
- return Principal(role=role, clearance=clr, can_write=s["can_write"],
71
- can_admin=s["can_admin"], layers_allow=s["layers_allow"],
72
- ops_allow=s["ops_allow"])
73
-
74
-
75
- def _call(cg, tool, args):
76
- try:
77
- return ms._dispatch(cg, tool, args)
78
- except Exception as e: # noqa: BLE001
79
- return {"_exc": type(e).__name__, "_msg": str(e)[:140]}
80
-
81
-
82
- def _hits(cg, q, mark):
83
- rs = (_call(cg, "mdcg_search", {"query": q}) or {}).get("results") or []
84
- return [r["node"]["id"] for r in rs
85
- if mark in ((r.get("node") or {}).get("content") or "")]
86
-
87
-
88
- # ---- 夹具:属主写 restricted + internal 两条,后者 forget+accept ----
89
- os.makedirs(EXT, exist_ok=True)
90
- os.makedirs(OUTSIDE, exist_ok=True)
91
- own = MdCGSecure(ROOT, principal=P("designer", "secret"), autoflush=1)
92
- rid = _call(own, "mdcg_remember", {"content": REST, "sensitivity": "restricted",
93
- "layer": "knowledge"})["id"]
94
- fid = _call(own, "mdcg_remember", {"content": FORGOT, "layer": "contextual"})["id"]
95
- _call(own, "mdcg_forget", {"node_id": fid, "reason": "用户要求删除"})
96
- _pend = _call(own, "mdcg_review_list", {})
97
- _pend = _pend.get("pending") if isinstance(_pend, dict) else _pend
98
- for p in _pend or []:
99
- _call(own, "mdcg_review_decide", {"pid": p["pid"], "decision": "accept"})
100
- _pid = _call(own, "mdcg_remember", {"content": PLAIN + "PRIVMK 私密补充。",
101
- "sensitivity": "private", "layer": "knowledge"})["id"]
102
- own.close()
103
- with open(os.path.join(EXT, "note.md"), "w", encoding="utf-8") as f:
104
- f.write(PLAIN)
105
- os.makedirs(os.path.join(ROOT, "notes"), exist_ok=True)
106
- with open(os.path.join(ROOT, "notes", "ext_note.md"), "w", encoding="utf-8") as f:
107
- f.write(PLAIN + "SUBDIRMK 库根普通子目录里的外部资料。")
108
-
109
- print("== A 组:库根 / trash 不得被当外部文档摄取(#425 正题) ==")
110
- rec = MdCGSecure(ROOT, principal=P("record", "internal"), autoflush=1)
111
- print("[0] record get restricted ->", (_call(rec, "mdcg_get", {"node_id": rid}) or {}).get("error"))
112
- _d = _call(rec, "cg", {"op": "ingest", "action": "dir", "path": ROOT})
113
- _check("A1 显式 path=<库根> 的 dir 摄取被拒(fail-closed,error 点名 issue #425)",
114
- _d.get("ok") is False and "issue #425" in str(_d.get("error")), _d)
115
- _tp = os.path.join(ROOT, "trash", fid + ".md")
116
- _f = _call(rec, "cg", {"op": "ingest", "action": "file", "path": _tp})
117
- _check("A2 action=file 指向 trash/<id>.md 被拒(已删除节点不复活)",
118
- _f.get("ok") is False and "issue #425" in str(_f.get("error")), _f)
119
- _layer_md = os.path.join(ROOT, "knowledge", "orphan", rid + ".md")
120
- _f2 = _call(rec, "cg", {"op": "ingest", "action": "file", "path": _layer_md})
121
- _check("A3 action=file 指向层目录内节点文件被拒(restricted 正文不被抄成 internal)",
122
- _f2.get("ok") is False and "issue #425" in str(_f2.get("error")), _f2)
123
- _j = _call(rec, "cg", {"op": "ingest", "action": "jsonl",
124
- "path": os.path.join(ROOT, "hippocampus", "inbox.jsonl")})
125
- _check("A4 action=jsonl 指向库内台账被拒(库内账本不是外部会话源)",
126
- _j.get("ok") is False and "issue #425" in str(_j.get("error")), _j)
127
- rec.close()
128
-
129
- guest = MdCGSecure(ROOT, principal=P("guest", "internal"), autoflush=1)
130
- _g_rest = _hits(guest, "RESTMK1 跳板机", "RESTMK1")
131
- _g_forgot = _hits(guest, "FORGOTMK 前任手机", "FORGOTMK")
132
- _check("A5 访客检索不到 restricted 正文与已删除内容(修前:两条都命中)",
133
- not _g_rest and not _g_forgot, (_g_rest, _g_forgot))
134
- _check("A6 对照:访客仍读不到 private 节点正文",
135
- "PRIVMK" not in json.dumps(_call(guest, "mdcg_get", {"node_id": _pid}),
136
- ensure_ascii=False))
137
- guest.close()
138
-
139
- print("== B 组:白名单=库根上级目录时,dir 空 path 整枝剪掉库根(C7) ==")
140
- os.environ["MDCG_INGEST_ROOT"] = _T
141
- rec7 = MdCGSecure(ROOT, principal=P("record", "internal"), autoflush=1)
142
- _r7 = _call(rec7, "cg", {"op": "ingest", "action": "dir"})
143
- _doc7 = (_r7.get("chains") or {}).get("doc") or {}
144
- _check("B1 白名单=库根上级 + 空 path:库根被剪枝并如实回报",
145
- _r7.get("ok") is True and _r7.get("library_pruned") == ["brain"], _r7.get("library_pruned"))
146
- # 库根下的节点/trash 一个都不许被索引:把每个 doc 节点的「源根 + 相对路径」
147
- # 还原成源文件路径,判它是否落在库根内(同目录还有合法的库外兄弟目录 extsrc,
148
- # 故不能用 indexed==0 作判据;也不能只看 doc_ref.root——祖先摄取时 root 是上级)。
149
- _lib = os.path.normcase(os.path.realpath(ROOT))
150
- _from_lib = []
151
- for nid, e in list((rec7.index.get("nodes") or {}).items()): # 快照迭代(W7 G1a)
152
- if not nid.startswith("doc_"):
153
- continue
154
- node = _call(rec7, "mdcg_get", {"node_id": nid}) or {}
155
- ref = (node.get("frontmatter") or {}).get("doc_ref") or {}
156
- r, rel = ref.get("root"), ref.get("path")
157
- if not r or not rel:
158
- continue
159
- fp = os.path.normcase(os.path.realpath(os.path.join(r, rel)))
160
- if fp == _lib or fp.startswith(_lib + os.sep):
161
- _from_lib.append(nid)
162
- _check("B2 库根下没有任何节点被切成 doc_* 节点(trash/层目录/保护历史全部剪掉)",
163
- not _from_lib, _from_lib)
164
- guest7 = MdCGSecure(ROOT, principal=P("guest", "internal"), autoflush=1)
165
- _g7 = _hits(guest7, "RESTMK1 跳板机", "RESTMK1")
166
- _check("B3 访客检索不到库内 restricted 正文(修前:命中)", not _g7, _g7)
167
- guest7.close()
168
- rec7.close()
169
-
170
- print("== K 组:非恒真对照(只拒库内保留子树,不误伤合法摄取) ==")
171
- rec_k = MdCGSecure(ROOT, principal=P("record", "internal"), autoflush=1)
172
- _ke = _call(rec_k, "cg", {"op": "ingest", "action": "dir", "path": EXT})
173
- _check("K1 库外目录照常摄取(indexed>0)",
174
- _ke.get("ok") is True and ((_ke.get("chains") or {}).get("doc") or {}).get("indexed", 0) > 0,
175
- _ke)
176
- _ks = _call(rec_k, "cg", {"op": "ingest", "action": "dir", "path": os.path.join(ROOT, "notes")})
177
- _check("K2 库根下**普通子目录**仍可作外部资料根(保留既有用法)",
178
- _ks.get("ok") is True and ((_ks.get("chains") or {}).get("doc") or {}).get("indexed", 0) > 0,
179
- _ks)
180
- _kf = _call(rec_k, "cg", {"op": "ingest", "action": "file", "path": os.path.join(EXT, "note.md")})
181
- _check("K3 库外单文件照常摄取", _kf.get("ok") is True and _kf.get("indexed", 0) > 0, _kf)
182
- rec_k.close()
183
- os.environ.pop("MDCG_INGEST_ROOT", None)
184
-
185
- print("== C 组:判据单点(源码锚点) ==")
186
- _SRC = open(os.path.join(_REPO, "md_cg", "sources.py"), encoding="utf-8").read()
187
- _MISSING = [n for n in ("def library_ingest_guard(",
188
- "def library_rel(",
189
- "def library_reserved(",
190
- "def library_skip_rel(",
191
- "library_ingest_guard(self.cg, path, \"ingest file\")",
192
- "library_ingest_guard(self.cg, root, \"ingest dir\")",
193
- "library_ingest_guard(self.cg, path, \"ingest jsonl\")")
194
- if n not in _SRC]
195
- _check("C01 库内路径闸是单点(一个判定函数 + 三入口都调它)", not _MISSING, _MISSING)
196
- _check("C02 保留子树名单不复制层目录(层目录从 mdcg.LAYERS 取)",
197
- "from .mdcg import LAYERS" in _SRC)
198
- _check("C03 祖先剪枝复用 codeindex.skip_matcher 唯一实现(经 refindex.index_dir 的 skip_dirs)",
199
- "skip_dirs=_skip" in _SRC and "def skip_matcher(" in open(
200
- os.path.join(_REPO, "md_cg", "codeindex.py"), encoding="utf-8").read())
201
-
202
- if _MISSING and "--mutate" not in sys.argv:
203
- print("锚点自检失败:关键判据字面量缺失 %s" % _MISSING)
204
- shutil.rmtree(_T, ignore_errors=True)
205
- sys.exit(2)
206
-
207
-
208
- def _mirror_build():
209
- dst = tempfile.mkdtemp(prefix="iss425_mut_")
210
- for name in ("md_cg", "scripts"):
211
- src = os.path.join(_REPO, name)
212
- if os.path.isdir(src):
213
- shutil.copytree(src, os.path.join(dst, name),
214
- ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
215
- for name in sorted(os.listdir(_REPO)):
216
- p = os.path.join(_REPO, name)
217
- if os.path.isfile(p) and (name.endswith(".py")
218
- or name in ("README.md", "pyproject.toml")):
219
- shutil.copy2(p, os.path.join(dst, name))
220
- return dst
221
-
222
-
223
- def _run_in(tree):
224
- env = dict(os.environ)
225
- env.update({"PYTHONPATH": tree, "PYTHONUTF8": "1",
226
- "PYTHONDONTWRITEBYTECODE": "1"})
227
- for k in ("MDCG_ROOT", "MDCG_TOKEN", "MDCG_VERIFY_KEY", "MDCG_INGEST_ROOT"):
228
- env.pop(k, None)
229
- pr = subprocess.run([sys.executable, "-X", "utf8", "-m",
230
- "md_cg.test_issue425_ingest_library_path"],
231
- cwd=tree, env=env, capture_output=True, text=True,
232
- encoding="utf-8", errors="replace", timeout=1200)
233
- out = (pr.stdout or "") + (pr.stderr or "")
234
- reds = [ln.split("FAIL ", 1)[1].strip()
235
- for ln in out.splitlines() if ln.strip().startswith("FAIL ")]
236
- return pr.returncode, reds, out
237
-
238
-
239
- _NL = "\r\n" if b"\r\n" in open(os.path.join(_REPO, "md_cg", "sources.py"),
240
- "rb").read()[:8192] else "\n"
241
-
242
-
243
- def _j(*lines):
244
- return _NL.join(lines)
245
-
246
-
247
- # 如实登记变异下的红项集合:下游 #346 的同层覆写闸(protect.require_layer)会在
248
- # 「库内 restricted 节点被覆写」这一步抛 AccessDenied,于是「库根被扫进来」的后果
249
- # 可能被第二道闸挡住 ⇒ A5/B2/B3 在不同变异下红/不红取决于该下游闸是否恰好兜住。
250
- # 判据仍直接命中缺陷点:**指定项必须红**(附加红项 = 更强判别力,不作失败)。
251
- # · M1 回退单文件闸 ⇒ A2/A3(库内文件被当外部文档)与 C01 必红;
252
- # · M2 回退目录闸 ⇒ A1(dir 摄取库根未被本闸拒绝)与 C01 必红;
253
- # · M3 回退剪枝 ⇒ B1(剪枝回报缺失)必红。
254
- _MUTATIONS = (
255
- ("M1 回缺陷:单文件链不过库内闸", "md_cg/sources.py",
256
- (_j(" _g = library_ingest_guard(self.cg, path, \"ingest file\")",
257
- " if _g is not None:",
258
- " return _g"),
259
- _j(" _g = None # 变异:单文件链不过闸")),
260
- ("A2", "A3", "C01")),
261
- ("M2 回缺陷:目录链不过库内闸", "md_cg/sources.py",
262
- (_j(" _g = library_ingest_guard(self.cg, root, \"ingest dir\")",
263
- " if _g is not None:",
264
- " return _g"),
265
- _j(" _g = None # 变异:目录链不过闸")),
266
- ("A1", "C01")),
267
- ("M3 回缺陷:祖先目录不剪枝", "md_cg/sources.py",
268
- (_j(" _lib_rel = library_skip_rel(self.cg, root)",
269
- " _skip = [_lib_rel] if _lib_rel else None"),
270
- _j(" _lib_rel = None # 变异:不剪枝",
271
- " _skip = None")),
272
- ("B1",)),
273
- )
274
-
275
-
276
- def _mutate():
277
- print("!! 定点变异:回退三处库内闸,守卫必须按指定项转红")
278
- tree = _mirror_build()
279
- bad = []
280
- try:
281
- rc, reds, out = _run_in(tree)
282
- print(" 未变异镜像基线:rc=%d 红项=%d(必须 rc=0 且 0 红)" % (rc, len(reds)))
283
- if rc != 0 or reds:
284
- print("\n".join(out.splitlines()[-20:]))
285
- bad.append("镜像基线非绿")
286
- for desc, rel, (old, new), exp in _MUTATIONS:
287
- p = os.path.join(tree, rel)
288
- t = open(p, encoding="utf-8", newline="").read()
289
- if t.count(old) != 1:
290
- bad.append("%s 锚点命中 %d 次" % (desc, t.count(old)))
291
- print(" ANCHOR-MISS " + desc)
292
- continue
293
- bak = t
294
- open(p, "w", encoding="utf-8", newline="").write(t.replace(old, new, 1))
295
- for r, _dd, _ff in os.walk(tree):
296
- if os.path.basename(r) == "__pycache__":
297
- shutil.rmtree(r, ignore_errors=True)
298
- rc2, reds2, _o2 = _run_in(tree)
299
- open(p, "w", encoding="utf-8", newline="").write(bak)
300
- hit = sorted({r.split(" ")[0] for r in reds2})
301
- # 判据:**指定项必须红**;附加红项是更强判别力(下游 #346 覆写闸
302
- # 是否兜住取决于变异形态),不作失败——如实打印出来。
303
- _extra = sorted(set(hit) - set(exp))
304
- if rc2 != 0 and set(exp) <= set(hit):
305
- print(" OK %s 红项=%d 指定命中=%d/%d%s"
306
- % (desc, len(reds2), len(set(exp)), len(exp),
307
- (" 附加红项=" + str(_extra)) if _extra else ""))
308
- else:
309
- bad.append(desc)
310
- print(" MISMATCH %s 实际=%s 期望(必红)=%s rc=%d"
311
- % (desc, hit, sorted(exp), rc2))
312
- for r in reds2[:8]:
313
- print(" FAIL " + r)
314
- print("定点变异自证:%s(%d 条不合格)" % ("PASS" if not bad else "FAIL", len(bad)))
315
- return len(bad)
316
- finally:
317
- shutil.rmtree(tree, ignore_errors=True)
318
-
319
-
320
- shutil.rmtree(_T, ignore_errors=True)
321
- print("")
322
- print("issue #425 结果:%s" % ("全部通过" if not FAILS else "%d 失败" % len(FAILS)))
323
-
324
-
325
- def main():
326
- _bad = _mutate() if "--mutate" in sys.argv else 0
327
- return 1 if (FAILS or _bad) else 0
328
-
329
-
330
- if __name__ == "__main__":
331
- sys.exit(main())
1
+ # -*- coding: utf-8 -*-
2
+ # 功能名:摄取链不得把记忆库内部(库根/层目录/trash/保护历史)当外部文档重新索引——issue #425
3
+ # 生效条件:python -X utf8 -m md_cg.test_issue425_ingest_library_path [--mutate];被测面=md_cg/sources.py::library_rel/library_reserved/library_ingest_guard/library_skip_rel 与 ingest_file/ingest_dir/ingest_jsonl 的过闸点
4
+ # 子功能:A 组=显式 path=<库根> 与 action=file 指向 trash/<id>.md 时 restricted 正文/已删除节点不被访客检索到;B 组=白名单=库根上级目录时 dir 空 path 整枝剪掉库根;K 组=非恒真对照(库外目录照常摄取、库根下普通子目录照常摄取、private 节点不泄露)
5
+ # 执行:--mutate 时在镜像树里回退单文件闸 / 目录闸 / 剪枝,守卫必须按指定项转红
6
+ # 验证方式:全部 PASS 即通过(退出码 0);默认模式另做源码锚点自检(关键判据字面量缺失 ⇒ exit 2,fail-closed);--mutate 模式红项集合须与预期逐项相符
7
+ # 不适用条件:不判真实 MCP stdio 子进程面(本守卫走 `mcp_server._dispatch`,即 tools/call 同一分派函数);不判 `op=index_doc/index_code` 的库内重索引(那条链保留源密级与保护闸,不在本条);不判库根下**普通子目录**里存放的库内节点副本(调用方自担);不判存量已泄露的 doc_* 节点清理
8
+ """issue #425 守卫:库内节点文件 / 回收站 / 保护历史**不是外部文档**。
9
+
10
+ 病象(修前):`cg(op=ingest, action=dir, path=<库根>)` 或 `action=file` 指向
11
+ `trash/<id>.md` 时,摄取链把库内 md 当外部文档重新切成 `doc_*` 节点——
12
+ restricted 正文被抄成 internal(无令牌访客可检索)、已 forget+accept 删除的
13
+ 节点从 trash 复活。`MDCG_INGEST_ROOT` 设成库根上级目录时,空 path 的 dir 摄取
14
+ 同样扫进整个库根。
15
+
16
+ 修法(条件层):新增库内路径闸单点 `library_ingest_guard`(库根 / 层目录 /
17
+ trash / _protected_history / hippocampus 等保留子树一律 fail-closed),
18
+ `ingest_file` / `ingest_dir` / `ingest_jsonl` 三入口过同一闸;`ingest_dir` 在
19
+ root 是库根**祖先**时把库根整枝剪进 `skip_dirs`(复用 codeindex.skip_matcher
20
+ 唯一实现),并在回执里如实回报 `library_pruned`。
21
+ """
22
+ import json
23
+ import os
24
+ import shutil
25
+ import subprocess
26
+ import sys
27
+ import tempfile
28
+
29
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
30
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
31
+ if _REPO not in sys.path:
32
+ sys.path.insert(0, _REPO)
33
+
34
+ for _k in [k for k in os.environ if k.startswith("MDCG_")]:
35
+ del os.environ[_k]
36
+
37
+ FAILS = []
38
+ _T = tempfile.mkdtemp(prefix="iss425_")
39
+ ROOT = os.path.join(_T, "brain")
40
+ EXT = os.path.join(_T, "extsrc")
41
+ OUTSIDE = os.path.join(_T, "outside_root")
42
+ os.environ.update(HOME=_T, MDCG_ROOT=ROOT,
43
+ MDCG_STATE_ROOT=os.path.join(_T, "stateroot"),
44
+ MDCG_AUX_ROOT=os.path.join(_T, "auxroot"),
45
+ MDCG_DATA_ROOT=os.path.join(_T, "dataroot"),
46
+ MDCG_EXPORT_ROOT=_T, MDCG_SUSTAIN="0", MDCG_SLEEP="0")
47
+
48
+ from md_cg.mdcos import MdCGSecure # noqa: E402
49
+ from md_cg.security import Principal # noqa: E402
50
+ from md_cg.tokens import role_spec # noqa: E402
51
+ from md_cg import mcp_server as ms # noqa: E402
52
+ from md_cg import sources # noqa: E402
53
+
54
+ HDR = "# 功能名:{0}\n# 生效条件:无条件\n# 执行:见下\n\n## 细节\n"
55
+ REST = HDR.format("运维处置") + "RESTMK1 跳板机 10.0.0.7,误删主库后的处置细节与回滚步骤记录如下。" * 6
56
+ FORGOT = HDR.format("前任联系方式") + "FORGOTMK 前任手机 13800001111 住址 某路 12 号,已决定忘掉这条。" * 6
57
+ PLAIN = HDR.format("外部资料") + "EXTMK 公开资料正文,可正常摄取。" * 8
58
+
59
+
60
+ def _check(name, cond, detail=""):
61
+ if cond:
62
+ print(" PASS " + name)
63
+ else:
64
+ print(" FAIL " + name + " " + str(detail)[:220])
65
+ FAILS.append(name)
66
+
67
+
68
+ def P(role, clr):
69
+ s = role_spec(role)
70
+ return Principal(role=role, clearance=clr, can_write=s["can_write"],
71
+ can_admin=s["can_admin"], layers_allow=s["layers_allow"],
72
+ ops_allow=s["ops_allow"])
73
+
74
+
75
+ def _call(cg, tool, args):
76
+ try:
77
+ return ms._dispatch(cg, tool, args)
78
+ except Exception as e: # noqa: BLE001
79
+ return {"_exc": type(e).__name__, "_msg": str(e)[:140]}
80
+
81
+
82
+ def _hits(cg, q, mark):
83
+ rs = (_call(cg, "mdcg_search", {"query": q}) or {}).get("results") or []
84
+ return [r["node"]["id"] for r in rs
85
+ if mark in ((r.get("node") or {}).get("content") or "")]
86
+
87
+
88
+ # ---- 夹具:属主写 restricted + internal 两条,后者 forget+accept ----
89
+ os.makedirs(EXT, exist_ok=True)
90
+ os.makedirs(OUTSIDE, exist_ok=True)
91
+ own = MdCGSecure(ROOT, principal=P("designer", "secret"), autoflush=1)
92
+ rid = _call(own, "mdcg_remember", {"content": REST, "sensitivity": "restricted",
93
+ "layer": "knowledge"})["id"]
94
+ fid = _call(own, "mdcg_remember", {"content": FORGOT, "layer": "contextual"})["id"]
95
+ _call(own, "mdcg_forget", {"node_id": fid, "reason": "用户要求删除"})
96
+ _pend = _call(own, "mdcg_review_list", {})
97
+ _pend = _pend.get("pending") if isinstance(_pend, dict) else _pend
98
+ for p in _pend or []:
99
+ _call(own, "mdcg_review_decide", {"pid": p["pid"], "decision": "accept"})
100
+ _pid = _call(own, "mdcg_remember", {"content": PLAIN + "PRIVMK 私密补充。",
101
+ "sensitivity": "private", "layer": "knowledge"})["id"]
102
+ own.close()
103
+ with open(os.path.join(EXT, "note.md"), "w", encoding="utf-8") as f:
104
+ f.write(PLAIN)
105
+ os.makedirs(os.path.join(ROOT, "notes"), exist_ok=True)
106
+ with open(os.path.join(ROOT, "notes", "ext_note.md"), "w", encoding="utf-8") as f:
107
+ f.write(PLAIN + "SUBDIRMK 库根普通子目录里的外部资料。")
108
+
109
+ print("== A 组:库根 / trash 不得被当外部文档摄取(#425 正题) ==")
110
+ rec = MdCGSecure(ROOT, principal=P("record", "internal"), autoflush=1)
111
+ print("[0] record get restricted ->", (_call(rec, "mdcg_get", {"node_id": rid}) or {}).get("error"))
112
+ _d = _call(rec, "cg", {"op": "ingest", "action": "dir", "path": ROOT})
113
+ _check("A1 显式 path=<库根> 的 dir 摄取被拒(fail-closed,error 点名 issue #425)",
114
+ _d.get("ok") is False and "issue #425" in str(_d.get("error")), _d)
115
+ _tp = os.path.join(ROOT, "trash", fid + ".md")
116
+ _f = _call(rec, "cg", {"op": "ingest", "action": "file", "path": _tp})
117
+ _check("A2 action=file 指向 trash/<id>.md 被拒(已删除节点不复活)",
118
+ _f.get("ok") is False and "issue #425" in str(_f.get("error")), _f)
119
+ _layer_md = os.path.join(ROOT, "knowledge", "orphan", rid + ".md")
120
+ _f2 = _call(rec, "cg", {"op": "ingest", "action": "file", "path": _layer_md})
121
+ _check("A3 action=file 指向层目录内节点文件被拒(restricted 正文不被抄成 internal)",
122
+ _f2.get("ok") is False and "issue #425" in str(_f2.get("error")), _f2)
123
+ _j = _call(rec, "cg", {"op": "ingest", "action": "jsonl",
124
+ "path": os.path.join(ROOT, "hippocampus", "inbox.jsonl")})
125
+ _check("A4 action=jsonl 指向库内台账被拒(库内账本不是外部会话源)",
126
+ _j.get("ok") is False and "issue #425" in str(_j.get("error")), _j)
127
+ rec.close()
128
+
129
+ guest = MdCGSecure(ROOT, principal=P("guest", "internal"), autoflush=1)
130
+ _g_rest = _hits(guest, "RESTMK1 跳板机", "RESTMK1")
131
+ _g_forgot = _hits(guest, "FORGOTMK 前任手机", "FORGOTMK")
132
+ _check("A5 访客检索不到 restricted 正文与已删除内容(修前:两条都命中)",
133
+ not _g_rest and not _g_forgot, (_g_rest, _g_forgot))
134
+ _check("A6 对照:访客仍读不到 private 节点正文",
135
+ "PRIVMK" not in json.dumps(_call(guest, "mdcg_get", {"node_id": _pid}),
136
+ ensure_ascii=False))
137
+ guest.close()
138
+
139
+ print("== B 组:白名单=库根上级目录时,dir 空 path 整枝剪掉库根(C7) ==")
140
+ os.environ["MDCG_INGEST_ROOT"] = _T
141
+ rec7 = MdCGSecure(ROOT, principal=P("record", "internal"), autoflush=1)
142
+ _r7 = _call(rec7, "cg", {"op": "ingest", "action": "dir"})
143
+ _doc7 = (_r7.get("chains") or {}).get("doc") or {}
144
+ _check("B1 白名单=库根上级 + 空 path:库根被剪枝并如实回报",
145
+ _r7.get("ok") is True and _r7.get("library_pruned") == ["brain"], _r7.get("library_pruned"))
146
+ # 库根下的节点/trash 一个都不许被索引:把每个 doc 节点的「源根 + 相对路径」
147
+ # 还原成源文件路径,判它是否落在库根内(同目录还有合法的库外兄弟目录 extsrc,
148
+ # 故不能用 indexed==0 作判据;也不能只看 doc_ref.root——祖先摄取时 root 是上级)。
149
+ _lib = os.path.normcase(os.path.realpath(ROOT))
150
+ _from_lib = []
151
+ for nid, e in list((rec7.index.get("nodes") or {}).items()): # 快照迭代(W7 G1a)
152
+ if not nid.startswith("doc_"):
153
+ continue
154
+ node = _call(rec7, "mdcg_get", {"node_id": nid}) or {}
155
+ ref = (node.get("frontmatter") or {}).get("doc_ref") or {}
156
+ r, rel = ref.get("root"), ref.get("path")
157
+ if not r or not rel:
158
+ continue
159
+ fp = os.path.normcase(os.path.realpath(os.path.join(r, rel)))
160
+ if fp == _lib or fp.startswith(_lib + os.sep):
161
+ _from_lib.append(nid)
162
+ _check("B2 库根下没有任何节点被切成 doc_* 节点(trash/层目录/保护历史全部剪掉)",
163
+ not _from_lib, _from_lib)
164
+ guest7 = MdCGSecure(ROOT, principal=P("guest", "internal"), autoflush=1)
165
+ _g7 = _hits(guest7, "RESTMK1 跳板机", "RESTMK1")
166
+ _check("B3 访客检索不到库内 restricted 正文(修前:命中)", not _g7, _g7)
167
+ guest7.close()
168
+ rec7.close()
169
+
170
+ print("== K 组:非恒真对照(只拒库内保留子树,不误伤合法摄取) ==")
171
+ rec_k = MdCGSecure(ROOT, principal=P("record", "internal"), autoflush=1)
172
+ _ke = _call(rec_k, "cg", {"op": "ingest", "action": "dir", "path": EXT})
173
+ _check("K1 库外目录照常摄取(indexed>0)",
174
+ _ke.get("ok") is True and ((_ke.get("chains") or {}).get("doc") or {}).get("indexed", 0) > 0,
175
+ _ke)
176
+ _ks = _call(rec_k, "cg", {"op": "ingest", "action": "dir", "path": os.path.join(ROOT, "notes")})
177
+ _check("K2 库根下**普通子目录**仍可作外部资料根(保留既有用法)",
178
+ _ks.get("ok") is True and ((_ks.get("chains") or {}).get("doc") or {}).get("indexed", 0) > 0,
179
+ _ks)
180
+ _kf = _call(rec_k, "cg", {"op": "ingest", "action": "file", "path": os.path.join(EXT, "note.md")})
181
+ _check("K3 库外单文件照常摄取", _kf.get("ok") is True and _kf.get("indexed", 0) > 0, _kf)
182
+ rec_k.close()
183
+ os.environ.pop("MDCG_INGEST_ROOT", None)
184
+
185
+ print("== C 组:判据单点(源码锚点) ==")
186
+ _SRC = open(os.path.join(_REPO, "md_cg", "sources.py"), encoding="utf-8").read()
187
+ _MISSING = [n for n in ("def library_ingest_guard(",
188
+ "def library_rel(",
189
+ "def library_reserved(",
190
+ "def library_skip_rel(",
191
+ "library_ingest_guard(self.cg, path, \"ingest file\")",
192
+ "library_ingest_guard(self.cg, root, \"ingest dir\")",
193
+ "library_ingest_guard(self.cg, path, \"ingest jsonl\")")
194
+ if n not in _SRC]
195
+ _check("C01 库内路径闸是单点(一个判定函数 + 三入口都调它)", not _MISSING, _MISSING)
196
+ _check("C02 保留子树名单不复制层目录(层目录从 mdcg.LAYERS 取)",
197
+ "from .mdcg import LAYERS" in _SRC)
198
+ _check("C03 祖先剪枝复用 codeindex.skip_matcher 唯一实现(经 refindex.index_dir 的 skip_dirs)",
199
+ "skip_dirs=_skip" in _SRC and "def skip_matcher(" in open(
200
+ os.path.join(_REPO, "md_cg", "codeindex.py"), encoding="utf-8").read())
201
+
202
+ if _MISSING and "--mutate" not in sys.argv:
203
+ print("锚点自检失败:关键判据字面量缺失 %s" % _MISSING)
204
+ shutil.rmtree(_T, ignore_errors=True)
205
+ sys.exit(2)
206
+
207
+
208
+ def _mirror_build():
209
+ dst = tempfile.mkdtemp(prefix="iss425_mut_")
210
+ for name in ("md_cg", "scripts"):
211
+ src = os.path.join(_REPO, name)
212
+ if os.path.isdir(src):
213
+ shutil.copytree(src, os.path.join(dst, name),
214
+ ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
215
+ for name in sorted(os.listdir(_REPO)):
216
+ p = os.path.join(_REPO, name)
217
+ if os.path.isfile(p) and (name.endswith(".py")
218
+ or name in ("README.md", "pyproject.toml")):
219
+ shutil.copy2(p, os.path.join(dst, name))
220
+ return dst
221
+
222
+
223
+ def _run_in(tree):
224
+ env = dict(os.environ)
225
+ env.update({"PYTHONPATH": tree, "PYTHONUTF8": "1",
226
+ "PYTHONDONTWRITEBYTECODE": "1"})
227
+ for k in ("MDCG_ROOT", "MDCG_TOKEN", "MDCG_VERIFY_KEY", "MDCG_INGEST_ROOT"):
228
+ env.pop(k, None)
229
+ pr = subprocess.run([sys.executable, "-X", "utf8", "-m",
230
+ "md_cg.test_issue425_ingest_library_path"],
231
+ cwd=tree, env=env, capture_output=True, text=True,
232
+ encoding="utf-8", errors="replace", timeout=1200)
233
+ out = (pr.stdout or "") + (pr.stderr or "")
234
+ reds = [ln.split("FAIL ", 1)[1].strip()
235
+ for ln in out.splitlines() if ln.strip().startswith("FAIL ")]
236
+ return pr.returncode, reds, out
237
+
238
+
239
+ _NL = "\r\n" if b"\r\n" in open(os.path.join(_REPO, "md_cg", "sources.py"),
240
+ "rb").read()[:8192] else "\n"
241
+
242
+
243
+ def _j(*lines):
244
+ return _NL.join(lines)
245
+
246
+
247
+ # 如实登记变异下的红项集合:下游 #346 的同层覆写闸(protect.require_layer)会在
248
+ # 「库内 restricted 节点被覆写」这一步抛 AccessDenied,于是「库根被扫进来」的后果
249
+ # 可能被第二道闸挡住 ⇒ A5/B2/B3 在不同变异下红/不红取决于该下游闸是否恰好兜住。
250
+ # 判据仍直接命中缺陷点:**指定项必须红**(附加红项 = 更强判别力,不作失败)。
251
+ # · M1 回退单文件闸 ⇒ A2/A3(库内文件被当外部文档)与 C01 必红;
252
+ # · M2 回退目录闸 ⇒ A1(dir 摄取库根未被本闸拒绝)与 C01 必红;
253
+ # · M3 回退剪枝 ⇒ B1(剪枝回报缺失)必红。
254
+ _MUTATIONS = (
255
+ ("M1 回缺陷:单文件链不过库内闸", "md_cg/sources.py",
256
+ (_j(" _g = library_ingest_guard(self.cg, path, \"ingest file\")",
257
+ " if _g is not None:",
258
+ " return _g"),
259
+ _j(" _g = None # 变异:单文件链不过闸")),
260
+ ("A2", "A3", "C01")),
261
+ ("M2 回缺陷:目录链不过库内闸", "md_cg/sources.py",
262
+ (_j(" _g = library_ingest_guard(self.cg, root, \"ingest dir\")",
263
+ " if _g is not None:",
264
+ " return _g"),
265
+ _j(" _g = None # 变异:目录链不过闸")),
266
+ ("A1", "C01")),
267
+ ("M3 回缺陷:祖先目录不剪枝", "md_cg/sources.py",
268
+ (_j(" _lib_rel = library_skip_rel(self.cg, root)",
269
+ " _skip = [_lib_rel] if _lib_rel else None"),
270
+ _j(" _lib_rel = None # 变异:不剪枝",
271
+ " _skip = None")),
272
+ ("B1",)),
273
+ )
274
+
275
+
276
+ def _mutate():
277
+ print("!! 定点变异:回退三处库内闸,守卫必须按指定项转红")
278
+ tree = _mirror_build()
279
+ bad = []
280
+ try:
281
+ rc, reds, out = _run_in(tree)
282
+ print(" 未变异镜像基线:rc=%d 红项=%d(必须 rc=0 且 0 红)" % (rc, len(reds)))
283
+ if rc != 0 or reds:
284
+ print("\n".join(out.splitlines()[-20:]))
285
+ bad.append("镜像基线非绿")
286
+ for desc, rel, (old, new), exp in _MUTATIONS:
287
+ p = os.path.join(tree, rel)
288
+ t = open(p, encoding="utf-8", newline="").read()
289
+ if t.count(old) != 1:
290
+ bad.append("%s 锚点命中 %d 次" % (desc, t.count(old)))
291
+ print(" ANCHOR-MISS " + desc)
292
+ continue
293
+ bak = t
294
+ open(p, "w", encoding="utf-8", newline="").write(t.replace(old, new, 1))
295
+ for r, _dd, _ff in os.walk(tree):
296
+ if os.path.basename(r) == "__pycache__":
297
+ shutil.rmtree(r, ignore_errors=True)
298
+ rc2, reds2, _o2 = _run_in(tree)
299
+ open(p, "w", encoding="utf-8", newline="").write(bak)
300
+ hit = sorted({r.split(" ")[0] for r in reds2})
301
+ # 判据:**指定项必须红**;附加红项是更强判别力(下游 #346 覆写闸
302
+ # 是否兜住取决于变异形态),不作失败——如实打印出来。
303
+ _extra = sorted(set(hit) - set(exp))
304
+ if rc2 != 0 and set(exp) <= set(hit):
305
+ print(" OK %s 红项=%d 指定命中=%d/%d%s"
306
+ % (desc, len(reds2), len(set(exp)), len(exp),
307
+ (" 附加红项=" + str(_extra)) if _extra else ""))
308
+ else:
309
+ bad.append(desc)
310
+ print(" MISMATCH %s 实际=%s 期望(必红)=%s rc=%d"
311
+ % (desc, hit, sorted(exp), rc2))
312
+ for r in reds2[:8]:
313
+ print(" FAIL " + r)
314
+ print("定点变异自证:%s(%d 条不合格)" % ("PASS" if not bad else "FAIL", len(bad)))
315
+ return len(bad)
316
+ finally:
317
+ shutil.rmtree(tree, ignore_errors=True)
318
+
319
+
320
+ shutil.rmtree(_T, ignore_errors=True)
321
+ print("")
322
+ print("issue #425 结果:%s" % ("全部通过" if not FAILS else "%d 失败" % len(FAILS)))
323
+
324
+
325
+ def main():
326
+ _bad = _mutate() if "--mutate" in sys.argv else 0
327
+ return 1 if (FAILS or _bad) else 0
328
+
329
+
330
+ if __name__ == "__main__":
331
+ sys.exit(main())