@furongjun1999/dsh-memory 0.9.22 → 0.9.23

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 (103) hide show
  1. package/CHANGELOG.en.md +404 -0
  2. package/CHANGELOG.md +45 -0
  3. package/README.en.md +240 -0
  4. package/README.md +3 -1
  5. package/docs/README.md +2 -0
  6. package/docs/en/README.md +190 -0
  7. package/docs/en/TRANSLATION.md +78 -0
  8. package/docs/en/TRANSLATION_MANIFEST.json +170 -0
  9. package/docs/en/TRANSLATION_REPORT.md +11 -0
  10. package/docs/en/dev/README.md +10 -0
  11. package/docs/en/dev/coding-testing.md +72 -0
  12. package/docs/en/dev/switch-matrix.md +10 -0
  13. package/docs/en/glossary.md +97 -0
  14. package/docs/en/harnesses/claude.md +100 -0
  15. package/docs/en/harnesses/codebuddy.md +93 -0
  16. package/docs/en/harnesses/codex.md +88 -0
  17. package/docs/en/harnesses/dsh.md +84 -0
  18. package/docs/en/harnesses/zcode.md +42 -0
  19. package/docs/en/mdcg/ccg.md +32 -0
  20. package/docs/en/mdcg/generated-docs.md +20 -0
  21. package/docs/en/mdcg/guardrail-charter.md +170 -0
  22. package/docs/en/mdcg/mcp-tool-names.md +43 -0
  23. package/docs/en/mdcg/multi-harness.md +43 -0
  24. package/docs/en/mdcg/recall-metadata.md +30 -0
  25. package/docs/en/mdcg/world-model-endpoint.md +379 -0
  26. package/docs/en/mdcg/write-aliases.md +130 -0
  27. package/docs/en/ops/README.md +11 -0
  28. package/docs/en/ops/environment-known-issues.md +244 -0
  29. package/docs/en/ops/install.md +72 -0
  30. package/docs/en/ops/operations-troubleshooting.md +86 -0
  31. package/docs/en/ops/permissions-review.md +79 -0
  32. package/docs/en/ops/release-governance.md +49 -0
  33. package/docs/en/ops/tool-name-migration.md +30 -0
  34. package/docs/en/ops/ui-install-troubleshooting.md +53 -0
  35. package/docs/en/standards/ai-coding-standard-checklist.md +71 -0
  36. package/docs/en/standards/ai-coding-standard-lingshu.md +47 -0
  37. package/docs/en/standards/ai-coding-standard.md +185 -0
  38. package/docs/en/ui/minimal.md +62 -0
  39. package/docs/mdcg/cogmap_ops.md +15 -15
  40. package/lib/hooks.d.ts +22 -0
  41. package/lib/hooks.js +124 -32
  42. package/lib/index.js +19 -3
  43. package/lib/lib/console_web.mjs +30 -3
  44. package/lib/lib/hook_audit.d.ts +3 -2
  45. package/lib/lib/hook_audit.js +14 -4
  46. package/lib/lib/mdcg_client.d.ts +5 -2
  47. package/lib/lib/mdcg_client.js +8 -4
  48. package/lib/lib/prompt_safety.d.ts +24 -0
  49. package/lib/lib/prompt_safety.js +77 -1
  50. package/lib/lib/token_store.js +5 -2
  51. package/lib/tools.js +5 -3
  52. package/md_cg/fsutil.py +80 -25
  53. package/md_cg/mcp_server.py +5761 -5557
  54. package/md_cg/mdcg.py +570 -73
  55. package/md_cg/mdcos.py +36 -11
  56. package/md_cg/nodefile.py +147 -23
  57. package/md_cg/op_table.py +14 -4
  58. package/md_cg/reconcile.py +9 -11
  59. package/md_cg/rollback.py +16 -1
  60. package/md_cg/scrub.py +94 -14
  61. package/md_cg/security.py +4 -2
  62. package/md_cg/test_cond_term_fold.py +183 -0
  63. package/md_cg/test_cond_terms_colon.py +348 -0
  64. package/md_cg/test_generation_guard.py +63 -42
  65. package/md_cg/test_issue362_neg_condition.py +44 -7
  66. package/md_cg/test_issue39_utf8_stdio.py +3 -3
  67. package/md_cg/test_issue444_scrub_evidence_plaintext.py +14 -8
  68. package/md_cg/test_issue677_flush_batch_io.py +139 -0
  69. package/md_cg/test_issue677_info_fast.py +82 -0
  70. package/md_cg/test_issue677_remember_dedupe.py +106 -0
  71. package/md_cg/test_issue712_tokens_sessions_read.py +199 -0
  72. package/md_cg/test_issue722_strict_conditions.py +96 -0
  73. package/md_cg/test_issue730_narrowing_audit.py +121 -0
  74. package/md_cg/test_issue733_verify_override.py +193 -0
  75. package/md_cg/test_issue733_verify_override_and_overwrite_inherit.py +315 -0
  76. package/md_cg/test_issue740_filelock_fd_leak.py +218 -0
  77. package/md_cg/test_issue741_content_hash_same_source.py +158 -0
  78. package/md_cg/test_issue747_slot_contradiction.py +125 -0
  79. package/md_cg/test_issue754_nested_root_guard.py +197 -0
  80. package/md_cg/test_issue757_index_hash_compare.py +85 -0
  81. package/md_cg/test_issue758_disk_hash_single.py +67 -0
  82. package/md_cg/test_issue759_twophase_hash.py +67 -0
  83. package/md_cg/test_n204_n205_n226_n227_n228_n229_exit_gates.py +545 -542
  84. package/md_cg/test_n209_verify_write_face_gates.py +5 -2
  85. package/md_cg/test_neg_condition_hits.py +336 -333
  86. package/md_cg/test_scrub_source_line_false_positive.py +64 -0
  87. package/md_cg/test_twophase.py +286 -286
  88. package/md_cg/tokens.py +55 -7
  89. package/md_cg/twophase.py +15 -6
  90. package/md_cg/writepipe.py +35 -3
  91. package/package.json +137 -134
  92. package/scripts/workspace_index.py +2 -0
  93. package/src/hooks.ts +139 -33
  94. package/src/index.ts +19 -3
  95. package/src/lib/console_web.mjs +30 -3
  96. package/src/lib/hook_audit.ts +15 -5
  97. package/src/lib/mdcg_client.ts +8 -4
  98. package/src/lib/prompt_safety.ts +73 -1
  99. package/src/lib/token_store.ts +5 -2
  100. package/src/tools.ts +5 -3
  101. package/ui/minimal/integrity.json +2 -2
  102. package/ui/minimal/payload/lib/lib/console_web.mjs +30 -3
  103. package/ui/minimal/payload/src/lib/console_web.mjs +30 -3
@@ -0,0 +1,158 @@
1
+ # -*- coding: utf-8 -*-
2
+ """issue #741 守卫:content_hash 全写点同源(落盘形态规范化收进唯一实现)。
3
+
4
+ ## 病象
5
+ `_stage`(add 内联)手写 `sha256(sealed)[:12]`(无尾换行),而 `rebuild_index →
6
+ `_node_entry` 走 `nodefile.content_hash(读回正文)`(`dumps` 恒补尾换行)⇒ 同一节点
7
+ 同一正文在索引里先后出现两个值(实测 9eb1d121c59f / aaf2ffbbdd36),对账恒报
8
+ hash_drift 假阳性;#352 §B 已被迫绕行(mdcg.py「不用索引条目的 content_hash」)。
9
+
10
+ ## 修法(单点)
11
+ `nodefile.normalize_body()` =从 `dumps()` 抽出的尾换行规则;`nodefile.content_hash`
12
+ 内部先 normalize 再哈希 ⇒ **全部**写点(add 内联 / `_write_node` 索引同步 /
13
+ `_move_layer` 经 `_node_entry` / twophase)自动同源,无需逐点手补。
14
+
15
+ ## 断言面(每条=可定点变异的判据)
16
+ - G1 写入指纹 == 读回重建指纹(#741 主判据;修前必红)
17
+ - G2 `rebuild_index()` 后不变(幂等)
18
+ - G3 元数据更新(`update_tags` → `_write_node` 索引同步口)后仍同源
19
+ - G4 搬层(`_move_layer` → `_node_entry`)后仍同源
20
+ - G5 内容改动 → 指纹变化;重建后稳定
21
+ - G6 正文自带尾换行 → 幂等(不双补)
22
+ - G7 旧手写口径 != 新口径(升级首轮 hash_drift 属预期,`rebuild_index` 迁移)
23
+
24
+ 运行:python -X utf8 -m md_cg.test_issue741_content_hash_same_source
25
+ 退出码:0 = 全绿;1 = 有断言失败。沙箱纪律:临时库,绝不触在役库与 ~/.mdcg。
26
+ """
27
+ from __future__ import annotations
28
+
29
+ import hashlib
30
+ import os
31
+ import shutil
32
+ import tempfile
33
+
34
+ from md_cg import nodefile
35
+ from md_cg.mdcos import MdCGOS
36
+
37
+ PASS = FAIL = 0
38
+ FAILS = []
39
+
40
+
41
+ def ok(cond, msg):
42
+ global PASS, FAIL
43
+ if cond:
44
+ PASS += 1
45
+ else:
46
+ FAIL += 1
47
+ FAILS.append(msg)
48
+
49
+
50
+ def _idx_hash(cg, nid):
51
+ e = (cg.index.get("nodes") or {}).get(nid) or {}
52
+ return e.get("content_hash")
53
+
54
+
55
+ def _rb_hash(cg, nid):
56
+ node = cg.get(nid) or {}
57
+ return nodefile.content_hash(node.get("content") or "")
58
+
59
+
60
+ def _old_style(content):
61
+ """修前 `_stage` 的手写口径(无尾换行)——只在断言里用于证明鉴别力。"""
62
+ return hashlib.sha256(content.encode("utf-8")).hexdigest()[:12]
63
+
64
+
65
+ BODY = "\n".join([
66
+ "# 功能名:741 探针",
67
+ "# 生效条件:任意时刻",
68
+ "# 子功能:x",
69
+ "# 执行:x",
70
+ "# 验证方式:test",
71
+ "# 不适用条件:无",
72
+ "",
73
+ "正文第一行",
74
+ "正文第二行",
75
+ ]) # 不带尾换行 —— 旧实现必分叉形态
76
+
77
+
78
+ def main():
79
+ TMP = tempfile.mkdtemp(prefix="i741_")
80
+ try:
81
+ cg = MdCGOS(os.path.join(TMP, "root"))
82
+
83
+ # G1 写入 == 重建(#741 主判据)
84
+ cg.add("g1", BODY, layer="knowledge", tags=["a"], verification_basis="test")
85
+ h1 = _idx_hash(cg, "g1")
86
+ r1 = _rb_hash(cg, "g1")
87
+ ok(h1 == r1, "G1 写入索引指纹 == 读回重建指纹(%s vs %s)" % (h1, r1))
88
+ ok(h1 == nodefile.content_hash(BODY), "G1 指纹 == 唯一实现(正文)(normalize 内置)")
89
+ ok(h1 != _old_style(BODY), "G1 新口径 != 旧手写口径(证明本测试有鉴别力)")
90
+
91
+ # G2 rebuild 幂等
92
+ cg.rebuild_index()
93
+ ok(_idx_hash(cg, "g1") == h1, "G2 rebuild 后指纹不变")
94
+
95
+ # G3 元数据更新(_write_node 索引同步口)
96
+ cg.update_tags("g1", add=["t741"])
97
+ ok(_idx_hash(cg, "g1") == _rb_hash(cg, "g1"), "G3 update_tags 后仍同源")
98
+
99
+ # G4 搬层(_move_layer → _node_entry)
100
+ cg._move_layer("g1", "contextual", reason="i741")
101
+ ok(_idx_hash(cg, "g1") == _rb_hash(cg, "g1"), "G4 搬层后仍同源")
102
+
103
+ # G5 内容改动 → 指纹变;重建稳定
104
+ h_before = _idx_hash(cg, "g1")
105
+ cg.add("g1", BODY + "\n追加", layer="knowledge", tags=["a"], verification_basis="test")
106
+ h_after = _idx_hash(cg, "g1")
107
+ ok(h_after != h_before, "G5 内容改动 → 指纹变化")
108
+ cg.rebuild_index()
109
+ ok(_idx_hash(cg, "g1") == h_after, "G5 重建后指纹稳定")
110
+
111
+ # G6 尾换行幂等
112
+ cg.add("g6", BODY + "\n", layer="knowledge", tags=["a"], verification_basis="test")
113
+ # G6 改判据(原写法两侧同时双补 ⇒ 恒真;改为「BODY 与 BODY+\n 同值」)
114
+ ok(nodefile.content_hash(BODY) == nodefile.content_hash(BODY + "\n"),
115
+ "G6 尾换行幂等(normalize 只补不删:BODY 与 BODY+换行 同值)")
116
+
117
+ # G7 旧口径 ≠ 新口径(升级一次性 drift 的书面证据)
118
+ ok(_old_style(BODY) != nodefile.content_hash(BODY),
119
+ "G7 旧口径 != 新口径(升级首轮 hash_drift 属预期,rebuild_index 迁移)")
120
+
121
+ # G8/G9 空正文与 None(#741 口径变更的破坏性一面,显式钉住)
122
+ ok(nodefile.content_hash("") == hashlib.sha256(b"\n").hexdigest()[:12],
123
+ "G8 content_hash(空) == sha256(单个换行)(修前是 sha256 空串)")
124
+ ok(nodefile.content_hash(None) == nodefile.content_hash(""),
125
+ "G9 None 与空串同值(content_hash 侧兜底)")
126
+ # G10 normalize 只补不删(尾换行个数仍可区分)
127
+ ok(nodefile.content_hash("a\n\n") != nodefile.content_hash("a\n"),
128
+ "G10 尾换行个数仍可区分(normalize 不 rstrip)")
129
+ ok(nodefile.normalize_body(nodefile.normalize_body("a"))
130
+ == nodefile.normalize_body("a"), "G11 normalize_body 幂等")
131
+
132
+ # G12/G13 twophase legacy 双向守卫(旧口径 intent 放行 / 真 mismatch 拒)
133
+ from md_cg import twophase as _tp
134
+ cg.add("g12", BODY, layer="knowledge", tags=["a"], verification_basis="test")
135
+ # issue #759:旧 begin 的实际口径是 content_hash(_norm(x))(= sha256(rstrip + "\n")),
136
+ # 不是手写 sha256(rstrip)——后者少一个尾换行字节,从来不是真实旧口径。
137
+ _legacy_want = nodefile.content_hash(_tp._norm(BODY))
138
+ _st12, _cd12 = _tp._judge(cg, {"id": "g12", "content_hash": _legacy_want})
139
+ ok(_st12 == _tp.STATUS_COMMITTED,
140
+ "G12 旧口径 intent 放行(升级兼容,%s/%s)" % (_st12, _cd12))
141
+ cg.add("g12", BODY + "\n追加", layer="knowledge", tags=["a"],
142
+ verification_basis="test")
143
+ _st13, _cd13 = _tp._judge(cg, {"id": "g12", "content_hash": _legacy_want})
144
+ ok(_st13 == _tp.STATUS_INTERRUPTED and _cd13 == _tp.R_MISMATCH,
145
+ "G13 正文真改后旧 intent 仍判 mismatch(兼容不过宽,%s/%s)" % (_st13, _cd13))
146
+
147
+ cg.close()
148
+ finally:
149
+ shutil.rmtree(TMP, ignore_errors=True)
150
+
151
+ print("\ni741: %d passed, %d failed" % (PASS, FAIL))
152
+ for f in FAILS:
153
+ print(" - " + f)
154
+ raise SystemExit(1 if FAIL else 0)
155
+
156
+
157
+ if __name__ == "__main__":
158
+ main()
@@ -0,0 +1,125 @@
1
+ # -*- coding: utf-8 -*-
2
+ """issue #747:`scrub._contradiction` 按**槽位**比对(替代全文比对)。
3
+
4
+ ## 病灶
5
+ 旧实现用**全文**算 bigram 与极性:六要素模板头每节点都有(20+ 共享 bigram)⇒ `len(shared)>=3
6
+ 且 ratio>=0.15 几乎必过;`不适用条件` 天然带否定词 ⇒ 任意两条 CCG 节点都可能被判矛盾并 weaken+demote。
7
+
8
+ ## 修法(#747 讨论后的方案)
9
+ - 只取**主张正文**并按槽位切分(`_slot_claims`);
10
+ - 同槽位或「主张 vs 对端拒绝域」两半契约(`不适用条件` **保留**,不整行丢弃);
11
+ - **双值极性**:两侧该槽位都非零且严格异号;
12
+ - **绝对下限** `SLOT_MIN_SHARED=5`(剥模板后分母同步变小,仅看 ratio 会反向放宽);
13
+ - 补否定词(不会/没有/无须/不必/无需)——「不会重试」vs「会自动重试三次」这类真矛盾此前两侧都是 0。
14
+
15
+ 运行:python -X utf8 -m md_cg.test_issue747_slot_contradiction
16
+ """
17
+ from __future__ import annotations
18
+
19
+ import os
20
+ import sys
21
+
22
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
23
+
24
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
25
+ sys.path.insert(0, _REPO)
26
+
27
+ from md_cg import scrub # noqa: E402
28
+
29
+ FAILS = []
30
+
31
+
32
+ def ok(cond, name, detail=""):
33
+ if cond:
34
+ print(" PASS " + name)
35
+ else:
36
+ print(" FAIL " + name + " " + str(detail)[:300])
37
+ FAILS.append(name)
38
+
39
+
40
+ class CG:
41
+ """最小 cg 桩:只实现 _contradiction 用到的 get(node_id)。"""
42
+
43
+ def __init__(self, body):
44
+ self.body = body
45
+
46
+ def get(self, _i):
47
+ return {"content": self.body}
48
+
49
+
50
+ A = """# 功能名:billion-context 压缩代理配置
51
+ # 生效条件:调整 DSH 上下文压缩代理
52
+ # 子功能:配置文件热加载
53
+ # 执行:用 acp_status 查看
54
+ # 验证方式:代理自估 token 数高于上游
55
+ # 不适用条件:不要把代理估算值当作上游计费值"""
56
+
57
+ B = """# 功能名:部署链失败即停
58
+ # 生效条件:执行部署步骤
59
+ # 子功能:先 disconnect 再 reconnect
60
+ # 执行:部署写成 set -e 脚本
61
+ # 验证方式:可以核验 /ready
62
+ # 不适用条件:只读查询类命令串"""
63
+
64
+
65
+ def hit(cg_body, related=("b",)):
66
+ return scrub._contradiction(CG(cg_body), "X", [{"node_id": x} for x in related])
67
+
68
+
69
+ def main():
70
+ print("=" * 68)
71
+ print("issue #747 · 槽位比对(scrub._contradiction)")
72
+ print("=" * 68)
73
+ # ① 模板噪声不再制造矛盾(issue 里的 A/B 原样)
74
+ ok(scrub._contradiction(CG(B), A, [{"node_id": "b"}]) is None,
75
+ "① 两条语义无关的 CCG 节点不再互判矛盾(A/B 原样)")
76
+ # ② 同槽位真矛盾仍检出
77
+ C = "# 执行:可以手改 nft 规则管理防火墙端口"
78
+ D = "# 执行:不要手改 nft 规则管理防火墙端口"
79
+ r = scrub._contradiction(CG(D), C, [{"node_id": "b"}])
80
+ ok(r is not None and "槽位" in str(r.get("why")), "② 同槽位真矛盾仍检出", r)
81
+ # ③ 双值极性:一侧中立 ⇒ 不算矛盾
82
+ E = "# 执行:手改 nft 规则管理防火墙端口"
83
+ r3 = scrub._contradiction(CG(D), E, [{"node_id": "b"}])
84
+ ok(r3 is None, "③ 一侧中立(pol=0)不算矛盾", r3)
85
+ # ④ 拒绝域 ↔ 主张(两半契约)仍可检出
86
+ G = "# 生效条件:可以用于生产环境的批量导出任务"
87
+ H = "# 不适用条件:不要用于生产环境的批量导出任务"
88
+ r4 = scrub._contradiction(CG(H), G, [{"node_id": "b"}])
89
+ ok(r4 is not None, "④ 主张 vs 对端拒绝域(不适用条件)仍检出", r4)
90
+ # ⑤ h2/h3 历史写法同样按槽位识别
91
+ I = "## 执行:可以手改 nft 规则管理防火墙端口"
92
+ r5 = scrub._contradiction(CG(D), I, [{"node_id": "b"}])
93
+ ok(r5 is not None, "⑤ h2 写法的槽位同样识别", r5)
94
+ # ⑥ 纯模板重叠(正文不同)⇒ 不矛盾
95
+ T1 = "# 功能名:甲\n# 生效条件:乙\n# 子功能:丙\n# 执行:丁\n# 验证方式:戊\n# 不适用条件:己"
96
+ T2 = "# 功能名:子\n# 生效条件:丑\n# 子功能:寅\n# 执行:卯\n# 验证方式:辰\n# 不适用条件:巳"
97
+ ok(scrub._contradiction(CG(T2), T1, [{"node_id": "b"}]) is None,
98
+ "⑥ 纯模板重叠不算矛盾")
99
+ # ⑧ 绝对下限:共享 3 个(ratio 0.6 已过)仍不判——SLOT_MIN_SHARED 钉住
100
+ J = "# 执行:可以管理端口"
101
+ K = "# 执行:不要管理端口"
102
+ ok(scrub.SLOT_MIN_SHARED == 5, '⑧a SLOT_MIN_SHARED 常量 = 5', scrub.SLOT_MIN_SHARED)
103
+ ok(scrub._contradiction(CG(K), J, [{'node_id': 'b'}]) is None,
104
+ '⑧b 共享 3 个(低于绝对下限)不判矛盾')
105
+ # ⑨ 功能名不做自比(signature 不是主张)
106
+ F1 = "# 功能名:可以管理端口规则"
107
+ F2 = "# 功能名:不要管理端口规则"
108
+ ok(scrub._contradiction(CG(F2), F1, [{'node_id': 'b'}]) is None,
109
+ '⑨ 功能名槽位退出矛盾比对')
110
+ # ⑩ 自然语句不误认成槽位
111
+ sl2 = scrub._slot_claims("执行成功后再验证")
112
+ ok('执行' not in sl2 and '' in sl2, '⑩ 无分隔符的自然语句归入正文桶', list(sl2)[:3])
113
+
114
+ # ⑦ _slot_claims:剥前缀、保留拒绝域、不含模板标记名
115
+ sl = scrub._slot_claims(A)
116
+ ok("acp_status" in " ".join(sl.get("执行", [])), "⑦a 执行槽位保留主张", sl.get("执行"))
117
+ ok("不适用条件" in sl and "不要" in " ".join(sl["不适用条件"]),
118
+ "⑦b 拒绝域被保留(#747 复核 ②)", sl.get("不适用条件"))
119
+ print()
120
+ print("issue #747:%d 项失败" % len(FAILS) if FAILS else "issue #747:全部通过")
121
+ return 1 if FAILS else 0
122
+
123
+
124
+ if __name__ == "__main__":
125
+ sys.exit(main())
@@ -0,0 +1,197 @@
1
+ # -*- coding: utf-8 -*-
2
+ """issue #754 守卫:index_code/index_doc 的嵌套根守卫(父/子根互相包含时拒绝)。
3
+
4
+ ## 病象
5
+ `docindex.node_id` 的键是「相对传入 root 的 path」⇒ 同一物理文件从父根索引得 `sub/a.md`、
6
+ 从子根得 `a.md`,**两个 id、两套节点**(#352-E 的冲突检测只拦「同 id 被另一根占用」,
7
+ 本例 id 不同 ⇒ 静默双写)。实测影响:93 份文档各 3 遍、多 2971 切块 / 8 MB。
8
+
9
+ ## 修法(入口守卫)
10
+ `mcp_server._nested_root_guard(cg, root)`:从 `<cg.root>/_refindex.json` 的已索引源文件
11
+ 记录里取全部 root(realpath),新 root 与之**互相包含**且不是同一个根 ⇒ 返回该根;
12
+ `index_code` / `index_doc` 入口命中即拒(`error=nested_root`),`allow_nested=true` 可显式强制。
13
+ 水位缺失/损坏 ⇒ 不拦(fail-open,既有行为不变)。
14
+
15
+ ## 断言面
16
+ - N1 父根索引成功;
17
+ - N2 子根索引被拒(error=nested_root,conflicts_with 指向父根);
18
+ - N3 被拒时**零写入**(节点数不变);
19
+ - N4 同根重索引不被误拦;
20
+ - N5 反向(先子后父)同样被拒;
21
+ - N6 allow_nested=true 时放行;
22
+ - N7 index_code 同形;
23
+ - N8 路径等价写法(尾随 /.)不算嵌套。
24
+
25
+ 运行:python -X utf8 -m md_cg.test_issue754_nested_root_guard
26
+ """
27
+ from __future__ import annotations
28
+
29
+ import json
30
+ import os
31
+ import shutil
32
+ import sys
33
+ import tempfile
34
+
35
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
36
+
37
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
38
+ sys.path.insert(0, _REPO)
39
+ _TMP = tempfile.mkdtemp(prefix="iss754_")
40
+ os.environ.update({
41
+ "MDCG_INGEST_ROOT": _TMP,
42
+ "MDCG_SUSTAIN": "0",
43
+ "MDCG_SLEEP": "0",
44
+ })
45
+
46
+ from md_cg import mcp_server # noqa: E402
47
+ from md_cg.mdcos import MdCGOS # noqa: E402
48
+
49
+ FAILS = []
50
+
51
+
52
+ def ok(cond, name, detail=""):
53
+ if cond:
54
+ print(" PASS " + name)
55
+ else:
56
+ print(" FAIL " + name + " " + str(detail)[:300])
57
+ FAILS.append(name)
58
+
59
+
60
+ def _lib(tag):
61
+ return MdCGOS(os.path.join(_TMP, "brain_" + tag), autoflush=1)
62
+
63
+
64
+ def _call(cg, **a):
65
+ r = mcp_server.call_tool(cg, "cg", a)
66
+ if isinstance(r, dict) and "content" in r:
67
+ return json.loads(r["content"][0]["text"])
68
+ return r
69
+
70
+
71
+ def _nodes(cg):
72
+ return len((cg.index.get("nodes") or {}))
73
+
74
+
75
+ def _led_files(cg):
76
+ import json as _json
77
+ fp = os.path.join(cg.root, "_refindex.json")
78
+ if not os.path.exists(fp):
79
+ return {}
80
+ try:
81
+ return (_json.load(open(fp, encoding="utf-8")) or {}).get("files") or {}
82
+ except Exception:
83
+ return {}
84
+
85
+
86
+ def _mk(path, text):
87
+ os.makedirs(os.path.dirname(path), exist_ok=True)
88
+ with open(path, "w", encoding="utf-8") as f:
89
+ f.write(text)
90
+
91
+
92
+ def main():
93
+ print("=" * 68)
94
+ print("issue #754 守卫:嵌套根重复索引")
95
+ print("=" * 68)
96
+ docs = os.path.join(_TMP, "docs")
97
+ _mk(os.path.join(docs, "sub", "a.md"),
98
+ "# 标题\n\n## 小节\n\n正文。\n")
99
+ _mk(os.path.join(docs, "b.md"), "# 另一篇\n\n正文。\n")
100
+
101
+ cg = _lib("a")
102
+ r1 = _call(cg, op="index_doc", path=docs)
103
+ ok(r1.get("ok") is True and int(r1.get("indexed") or 0) >= 1,
104
+ "N1 父根索引成功", r1)
105
+ n_before = _nodes(cg)
106
+ _n_led = len(_led_files(cg))
107
+ r2 = _call(cg, op="index_doc", path=os.path.join(docs, "sub"))
108
+ ok(r2.get("ok") is False and r2.get("error") == "nested_root",
109
+ "N2 子根索引被拒(nested_root)", r2)
110
+ ok(os.path.realpath(str(r2.get("conflicts_with") or "")) == os.path.realpath(docs),
111
+ "N2b conflicts_with 指向父根", r2.get("conflicts_with"))
112
+ ok(_nodes(cg) == n_before, "N3 被拒时零写入(节点数不变)",
113
+ (_nodes(cg), n_before))
114
+ ok(len(_led_files(cg)) == _n_led, "N3b 被拒时水位未变",
115
+ (len(_led_files(cg)), _n_led))
116
+ r3 = _call(cg, op="index_doc", path=docs, incremental=True)
117
+ ok(r3.get("ok") is True, "N4 同根重索引不被误拦", r3)
118
+
119
+ cg2 = _lib("b")
120
+ r4 = _call(cg2, op="index_doc", path=os.path.join(docs, "sub"))
121
+ ok(r4.get("ok") is True, "N5 先子根索引成功", r4)
122
+ r5 = _call(cg2, op="index_doc", path=docs)
123
+ ok(r5.get("ok") is False and r5.get("error") == "nested_root",
124
+ "N5b 反向(后父根)同样被拒", r5)
125
+ r6 = _call(cg2, op="index_doc", path=docs, allow_nested=True)
126
+ ok(r6.get("ok") is True, "N6 allow_nested=true 放行", r6)
127
+
128
+ repo = os.path.join(_TMP, "code")
129
+ _mk(os.path.join(repo, "src", "m.py"),
130
+ "def f():\n \"\"\"doc\"\"\"\n return 1\n")
131
+ cg3 = _lib("c")
132
+ c1 = _call(cg3, op="index_code", path=repo)
133
+ ok(c1.get("ok") is True, "N7 index_code 父根成功", c1)
134
+ c2 = _call(cg3, op="index_code", path=os.path.join(repo, "src"))
135
+ ok(c2.get("ok") is False and c2.get("error") == "nested_root",
136
+ "N7b index_code 子根被拒", c2)
137
+
138
+ ok(bool(r6.get("nested_override")) is True
139
+ and os.path.realpath(str(r6.get("conflicts_with") or ""))
140
+ == os.path.realpath(os.path.join(docs, "sub")),
141
+ "N6b allow_nested 强制时回执留痕(nested_override + conflicts_with=已索引根)", r6)
142
+
143
+ # N9:尾随分隔符等价写法不算嵌套
144
+ cg5 = _lib("e")
145
+ e1 = _call(cg5, op="index_doc", path=docs)
146
+ e2 = _call(cg5, op="index_doc", path=docs + os.sep)
147
+ ok(e1.get("ok") is True and e2.get("ok") is True,
148
+ "N9 尾随分隔符不算嵌套", (e1.get("ok"), e2.get("ok")))
149
+
150
+ # N10:kind 隔离——代码根内的文档根可独立索引(两种域)
151
+ _mk(os.path.join(repo, "docs", "x.md"), "# 文档\n\n正文。\n")
152
+ cg6 = _lib("f")
153
+ f1 = _call(cg6, op="index_code", path=repo)
154
+ f2 = _call(cg6, op="index_doc", path=os.path.join(repo, "docs"))
155
+ ok(f1.get("ok") is True and f2.get("ok") is True,
156
+ "N10 kind 隔离:code 根内 doc 根可独立索引", (f1.get("ok"), f2.get("ok")))
157
+
158
+ # N11:默认 root(未显式给 path)不做嵌套拦截(部署侧全库语义)
159
+ cg7 = _lib("g")
160
+ g1 = _call(cg7, op="index_doc", path=docs)
161
+ g2 = _call(cg7, op="index_doc")
162
+ ok(g1.get("ok") is True and g2.get("ok") is True,
163
+ "N11 默认 root 不被嵌套守卫拦", (g1.get("ok"), g2.get("ok")))
164
+
165
+ # N12:水位不可用 ⇒ fail-open + 回执显式降级
166
+ cg8 = _lib("h")
167
+ h1 = _call(cg8, op="index_doc", path=docs)
168
+ _led = os.path.join(cg8.root, "_refindex.json")
169
+ if os.path.exists(_led):
170
+ with open(_led, "w", encoding="utf-8") as _f:
171
+ _f.write("{broken") # 损坏水位(非删档——删档=全新库,不是降级)
172
+ h2 = _call(cg8, op="index_doc", path=os.path.join(docs, "sub"))
173
+ ok(h2.get("ok") is True and h2.get("nested_guard") == "unavailable",
174
+ "N12 水位损坏 ⇒ 不拦且显式降级(nested_guard=unavailable)",
175
+ (h2.get("ok"), h2.get("nested_guard")))
176
+
177
+ cg4 = _lib("d")
178
+ d1 = _call(cg4, op="index_doc", path=docs)
179
+ d2 = _call(cg4, op="index_doc", path=docs + os.sep + ".")
180
+ ok(d1.get("ok") is True and d2.get("ok") is True,
181
+ "N8 等价写法(尾随 /.)不算嵌套", (d1.get("ok"), d2.get("ok")))
182
+
183
+ for lib in (cg, cg2, cg3, cg4, cg5, cg6, cg7, cg8):
184
+ try:
185
+ lib.close()
186
+ except Exception:
187
+ pass
188
+ print()
189
+ print("issue #754:%d 项失败" % len(FAILS) if FAILS else "issue #754:全部通过")
190
+ return 1 if FAILS else 0
191
+
192
+
193
+ if __name__ == "__main__":
194
+ try:
195
+ sys.exit(main())
196
+ finally:
197
+ shutil.rmtree(_TMP, ignore_errors=True)
@@ -0,0 +1,85 @@
1
+ # -*- coding: utf-8 -*-
2
+ """issue #757:验证态继承的正文比对改用索引 hash(不再读盘回正文)。"""
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import shutil
7
+ import sys
8
+ import tempfile
9
+ from types import SimpleNamespace
10
+
11
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
12
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
13
+ sys.path.insert(0, _REPO)
14
+
15
+ from md_cg.mdcg import MdCG # noqa: E402
16
+
17
+ FAILS = []
18
+
19
+
20
+ def ok(cond, name, detail=""):
21
+ if cond:
22
+ print(" PASS " + name)
23
+ else:
24
+ print(" FAIL " + name + " " + str(detail)[:300])
25
+ FAILS.append(name)
26
+
27
+
28
+ BODY = ("# 功能名:x\n# 生效条件:y\n# 子功能:z\n# 执行:e\n"
29
+ "# 验证方式:test\n# 不适用条件:无\n\n正文")
30
+
31
+
32
+ def _state(cg, nid):
33
+ return ((cg.get(nid) or {}).get("frontmatter") or {}).get("verification_state")
34
+
35
+
36
+ def main():
37
+ print("=" * 68)
38
+ print("issue #757 · 验证态继承用索引 hash 比对")
39
+ print("=" * 68)
40
+ tmp = tempfile.mkdtemp(prefix="iss757_")
41
+ try:
42
+ cg = MdCG(os.path.join(tmp, "r"), autoflush=1)
43
+ cg.add("n757", BODY, layer="knowledge", verification_basis="test")
44
+ cg.verify("n757", "初始确认", "confirmed")
45
+ ok(_state(cg, "n757") == "verified", "A0 前置:节点为 verified", _state(cg, "n757"))
46
+ # 无 verify 权限的 principal
47
+ cg.principal = SimpleNamespace(allows_op=lambda op: False)
48
+ # A1 正文未变 ⇒ 仍 verified(索引 hash 相等 ⇒ 不降级)
49
+ cg.add("n757", BODY, layer="knowledge")
50
+ ok(_state(cg, "n757") == "verified", "A1 正文未变 ⇒ 仍 verified", _state(cg, "n757"))
51
+ # A2 正文变了 ⇒ doubted
52
+ cg.add("n757", BODY + "\n追加一行", layer="knowledge")
53
+ ok(_state(cg, "n757") == "doubted", "A2 正文变 ⇒ 降 doubted", _state(cg, "n757"))
54
+ cg.close()
55
+ finally:
56
+ shutil.rmtree(tmp, ignore_errors=True)
57
+
58
+ # A3/A4(复核要求:钉实现路径,而不只钉语义)——直接测判定函数,
59
+ # 用会计数 cg.get 的假 cg 证明「有索引 hash 时不读盘」。
60
+ class _CountCG:
61
+ def __init__(self, content):
62
+ self.n = 0
63
+ self._c = content
64
+ def get(self, nid):
65
+ self.n += 1
66
+ return {'content': self._c}
67
+
68
+ from md_cg import nodefile as _nf
69
+ _c1 = _CountCG(BODY + chr(10))
70
+ _pe = {'content_hash': _nf.content_hash(BODY)}
71
+ _r1 = MdCG._vstate_body_changed(_c1, 'x', _pe, BODY)
72
+ ok(_r1 is False and _c1.n == 0, 'A3 有索引 hash 时判定不读盘(快路径)', (_r1, _c1.n))
73
+ _c2 = _CountCG(BODY + chr(10))
74
+ _r2 = MdCG._vstate_body_changed(_c2, 'x', {}, BODY)
75
+ ok(_r2 is False and _c2.n == 0, 'A4 无 prev_entry 时也不读盘', (_r2, _c2.n))
76
+ _c3 = _CountCG(BODY + chr(10))
77
+ _r3 = MdCG._vstate_body_changed(_c3, 'x', {'content_hash': ''}, BODY)
78
+ ok(_r3 is False and _c3.n == 1, 'A5 缺 hash 时回退读盘一次(同函数归一)', (_r3, _c3.n))
79
+ print()
80
+ print("issue #757:%d 项失败" % len(FAILS) if FAILS else "issue #757:全部通过")
81
+ return 1 if FAILS else 0
82
+
83
+
84
+ if __name__ == "__main__":
85
+ sys.exit(main())
@@ -0,0 +1,67 @@
1
+ # -*- coding: utf-8 -*-
2
+ """issue #758:reconcile 的盘面指纹收单值(#741 后二元组第二元素不可达)。"""
3
+ from __future__ import annotations
4
+
5
+ import os
6
+ import shutil
7
+ import sys
8
+ import tempfile
9
+
10
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
11
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
12
+ sys.path.insert(0, _REPO)
13
+
14
+ from md_cg import nodefile, reconcile # noqa: E402
15
+ from md_cg.mdcg import MdCG # noqa: E402
16
+
17
+ FAILS = []
18
+
19
+
20
+ def ok(cond, name, detail=""):
21
+ if cond:
22
+ print(" PASS " + name)
23
+ else:
24
+ print(" FAIL " + name + " " + str(detail)[:300])
25
+ FAILS.append(name)
26
+
27
+
28
+ BODY = ("# 功能名:x\n# 生效条件:y\n# 子功能:z\n# 执行:e\n"
29
+ "# 验证方式:test\n# 不适用条件:无\n\n正文")
30
+
31
+
32
+ def main():
33
+ print("=" * 68)
34
+ print("issue #758 · _disk_hash 单值(含端到端收紧证明)")
35
+ print("=" * 68)
36
+ ok(callable(getattr(reconcile, "_disk_hash", None)), "A1 存在 _disk_hash")
37
+ h1 = reconcile._disk_hash(BODY + "\n")
38
+ ok(isinstance(h1, str), "A2 返回单值 str", type(h1).__name__)
39
+ ok(h1 == nodefile.content_hash(BODY), "A2b 与索引同源(常盘 1 尾换行 == 正文本体)")
40
+ ok(reconcile._disk_hash(BODY + "\n\n") != h1,
41
+ "A3 ≥2 尾换行与 1 尾换行**不同值**(旧二元组会用它蒙混)")
42
+ # A4(复核要求:端到端)——索引是「剥尽形态」× 盘面多尾换行 ⇒ 必须判 drift,且 apply 后幂等
43
+ tmp = tempfile.mkdtemp(prefix="iss758_")
44
+ try:
45
+ cg = MdCG(os.path.join(tmp, "r"), autoflush=1)
46
+ cg.add("n758", BODY, layer="knowledge")
47
+ rel = cg.index["nodes"]["n758"].get("path")
48
+ fp = os.path.join(cg.root, rel)
49
+ with open(fp, "a", encoding="utf-8", newline="") as f:
50
+ f.write("\n") # 盘面变 2 尾换行
51
+ cg.index["nodes"]["n758"]["content_hash"] = nodefile.content_hash(BODY) # 剥尽形态(旧口径)
52
+ r1 = cg.reconcile_state(apply=False)
53
+ ok("n758" in (r1.get("hash_drift") or []),
54
+ "A4 索引剥尽 × 盘面多尾换行 ⇒ 判 drift(收紧生效)", r1.get("hash_drift"))
55
+ cg.reconcile_state(apply=True)
56
+ r2 = cg.reconcile_state(apply=False)
57
+ ok(not (r2.get("hash_drift") or []), "A4b apply 后幂等(二次扫描不再 drift)", r2.get("hash_drift"))
58
+ cg.close()
59
+ finally:
60
+ shutil.rmtree(tmp, ignore_errors=True)
61
+ print()
62
+ print("issue #758:%d 项失败" % len(FAILS) if FAILS else "issue #758:全部通过")
63
+ return 1 if FAILS else 0
64
+
65
+
66
+ if __name__ == "__main__":
67
+ sys.exit(main())