@furongjun1999/dsh-memory 0.9.22 → 0.10.1

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 (179) hide show
  1. package/CHANGELOG.en.md +450 -0
  2. package/CHANGELOG.md +112 -0
  3. package/README.en.md +244 -0
  4. package/README.md +272 -260
  5. package/codebuddy/CODEBUDDY.md +1 -1
  6. package/docs/README.md +2 -0
  7. package/docs/discipline/templates/full.md.tmpl +1 -1
  8. package/docs/discipline/templates/rules.mdc.tmpl +1 -1
  9. package/docs/en/README.md +190 -0
  10. package/docs/en/TRANSLATION.md +78 -0
  11. package/docs/en/TRANSLATION_MANIFEST.json +170 -0
  12. package/docs/en/TRANSLATION_REPORT.md +11 -0
  13. package/docs/en/dev/README.md +10 -0
  14. package/docs/en/dev/coding-testing.md +72 -0
  15. package/docs/en/dev/switch-matrix.md +10 -0
  16. package/docs/en/glossary.md +97 -0
  17. package/docs/en/harnesses/claude.md +100 -0
  18. package/docs/en/harnesses/codebuddy.md +93 -0
  19. package/docs/en/harnesses/codex.md +88 -0
  20. package/docs/en/harnesses/dsh.md +84 -0
  21. package/docs/en/harnesses/zcode.md +42 -0
  22. package/docs/en/mdcg/ccg.md +32 -0
  23. package/docs/en/mdcg/generated-docs.md +20 -0
  24. package/docs/en/mdcg/guardrail-charter.md +170 -0
  25. package/docs/en/mdcg/mcp-tool-names.md +43 -0
  26. package/docs/en/mdcg/multi-harness.md +43 -0
  27. package/docs/en/mdcg/recall-metadata.md +30 -0
  28. package/docs/en/mdcg/world-model-endpoint.md +379 -0
  29. package/docs/en/mdcg/write-aliases.md +130 -0
  30. package/docs/en/ops/README.md +11 -0
  31. package/docs/en/ops/environment-known-issues.md +244 -0
  32. package/docs/en/ops/install.md +72 -0
  33. package/docs/en/ops/operations-troubleshooting.md +86 -0
  34. package/docs/en/ops/permissions-review.md +79 -0
  35. package/docs/en/ops/release-governance.md +49 -0
  36. package/docs/en/ops/tool-name-migration.md +30 -0
  37. package/docs/en/ops/ui-install-troubleshooting.md +53 -0
  38. package/docs/en/standards/ai-coding-standard-checklist.md +71 -0
  39. package/docs/en/standards/ai-coding-standard-lingshu.md +47 -0
  40. package/docs/en/standards/ai-coding-standard.md +185 -0
  41. package/docs/en/ui/minimal.md +62 -0
  42. package/docs/mdcg/cogmap_ops.md +16 -15
  43. package/lib/client.js +29 -3
  44. package/lib/hooks.d.ts +34 -0
  45. package/lib/hooks.js +167 -33
  46. package/lib/index.js +19 -3
  47. package/lib/lib/console_web.mjs +30 -3
  48. package/lib/lib/hook_audit.d.ts +3 -2
  49. package/lib/lib/hook_audit.js +14 -4
  50. package/lib/lib/mdcg_client.d.ts +5 -2
  51. package/lib/lib/mdcg_client.js +8 -4
  52. package/lib/lib/prompt_safety.d.ts +24 -0
  53. package/lib/lib/prompt_safety.js +77 -1
  54. package/lib/lib/token_store.js +5 -2
  55. package/lib/tools.js +5 -3
  56. package/md_cg/bucketindex.py +231 -0
  57. package/md_cg/bucketscope.py +339 -0
  58. package/md_cg/config_registry_bulk.py +19 -10
  59. package/md_cg/config_validate.py +14 -2
  60. package/md_cg/detrerank.py +137 -0
  61. package/md_cg/fsutil.py +215 -25
  62. package/md_cg/govern/__init__.py +23 -0
  63. package/md_cg/iface/__init__.py +46 -0
  64. package/md_cg/iface/_verify.py +609 -0
  65. package/md_cg/iface/contracts.py +337 -0
  66. package/md_cg/judgment_manifest.py +14 -0
  67. package/md_cg/kernel/__init__.py +27 -0
  68. package/md_cg/mcp_server.py +457 -23
  69. package/md_cg/mdcg.py +1034 -79
  70. package/md_cg/mdcos.py +263 -21
  71. package/md_cg/metacognition.py +66 -13
  72. package/md_cg/mutate_extract_shipped.py +126 -0
  73. package/md_cg/mutate_ledger_e2e.py +100 -0
  74. package/md_cg/nodefile.py +147 -23
  75. package/md_cg/op_table.py +22 -4
  76. package/md_cg/probe_c_ledger_integrity.py +258 -0
  77. package/md_cg/probe_d_retirement_discipline.py +263 -0
  78. package/md_cg/reconcile.py +9 -11
  79. package/md_cg/review_cli.py +6 -1
  80. package/md_cg/rollback.py +16 -1
  81. package/md_cg/run_tests.py +30 -1
  82. package/md_cg/scopeselect.py +214 -0
  83. package/md_cg/scrub.py +160 -23
  84. package/md_cg/security.py +4 -2
  85. package/md_cg/state_extract.py +149 -0
  86. package/md_cg/state_slots.py +58 -10
  87. package/md_cg/structboost.py +179 -0
  88. package/md_cg/test_alternatives_historical.py +162 -0
  89. package/md_cg/test_b2_sensitivity_landing.py +226 -223
  90. package/md_cg/test_b3_merge_keeps_content.py +608 -583
  91. package/md_cg/test_bucket_cards.py +85 -0
  92. package/md_cg/test_bucket_day_order.py +74 -0
  93. package/md_cg/test_bucket_quota.py +79 -0
  94. package/md_cg/test_bucket_recency_guards.py +75 -0
  95. package/md_cg/test_bucket_three_causes.py +83 -0
  96. package/md_cg/test_bucketindex.py +130 -0
  97. package/md_cg/test_bucketscope_early.py +71 -0
  98. package/md_cg/test_cond_term_fold.py +183 -0
  99. package/md_cg/test_cond_term_fold_gate.py +1210 -0
  100. package/md_cg/test_cond_term_neg_scope.py +147 -0
  101. package/md_cg/test_cond_terms_colon.py +348 -0
  102. package/md_cg/test_detrerank.py +157 -0
  103. package/md_cg/test_gate_four_kpi.py +136 -0
  104. package/md_cg/test_generation_guard.py +76 -43
  105. package/md_cg/test_issue192_move_layer_indices_guard.py +48 -0
  106. package/md_cg/test_issue362_neg_condition.py +44 -7
  107. package/md_cg/test_issue39_utf8_stdio.py +572 -572
  108. package/md_cg/test_issue413_preimage_failclosed.py +147 -9
  109. package/md_cg/test_issue444_scrub_evidence_plaintext.py +14 -8
  110. package/md_cg/test_issue677_flush_batch_io.py +139 -0
  111. package/md_cg/test_issue677_info_fast.py +82 -0
  112. package/md_cg/test_issue677_remember_dedupe.py +106 -0
  113. package/md_cg/test_issue712_tokens_sessions_read.py +199 -0
  114. package/md_cg/test_issue722_strict_conditions.py +96 -0
  115. package/md_cg/test_issue730_narrowing_audit.py +121 -0
  116. package/md_cg/test_issue733_verify_override.py +193 -0
  117. package/md_cg/test_issue733_verify_override_and_overwrite_inherit.py +315 -0
  118. package/md_cg/test_issue740_filelock_fd_leak.py +218 -0
  119. package/md_cg/test_issue741_content_hash_same_source.py +158 -0
  120. package/md_cg/test_issue747_slot_contradiction.py +125 -0
  121. package/md_cg/test_issue754_nested_root_guard.py +197 -0
  122. package/md_cg/test_issue757_index_hash_compare.py +85 -0
  123. package/md_cg/test_issue758_disk_hash_single.py +67 -0
  124. package/md_cg/test_issue759_twophase_hash.py +67 -0
  125. package/md_cg/test_issue768_overwrite_time_window_anchor.py +135 -0
  126. package/md_cg/test_issue789_log_plaintext_probe.py +100 -0
  127. package/md_cg/test_issue790_verb_negation.py +77 -0
  128. package/md_cg/test_issue810_root_guard.py +71 -0
  129. package/md_cg/test_issue812_info_index_stale.py +78 -0
  130. package/md_cg/test_issue827_weaken_dedup.py +361 -0
  131. package/md_cg/test_issue829_calibration_positive_gate.py +473 -0
  132. package/md_cg/test_kernel_iface_contracts.py +18 -0
  133. package/md_cg/test_md_cg_no_hive_dep.py +238 -17
  134. package/md_cg/test_n204_n205_n226_n227_n228_n229_exit_gates.py +545 -542
  135. package/md_cg/test_n209_verify_write_face_gates.py +5 -2
  136. package/md_cg/test_neg_condition_hits.py +336 -333
  137. package/md_cg/test_probe_cd_shipped.py +385 -0
  138. package/md_cg/test_recent_tail.py +93 -0
  139. package/md_cg/test_run_tests_entry_gates.py +110 -0
  140. package/md_cg/test_scopeselect.py +70 -0
  141. package/md_cg/test_scopeselect_clock.py +70 -0
  142. package/md_cg/test_scrub_source_line_false_positive.py +64 -0
  143. package/md_cg/test_session_attribution_consistency.py +835 -813
  144. package/md_cg/test_state_extract_shipped.py +162 -0
  145. package/md_cg/test_state_ledger_chain.py +207 -0
  146. package/md_cg/test_state_ledger_e2e.py +145 -0
  147. package/md_cg/test_structboost.py +102 -0
  148. package/md_cg/test_timeboost.py +83 -0
  149. package/md_cg/test_timeboost_relativedate.py +71 -0
  150. package/md_cg/test_topicagg.py +129 -0
  151. package/md_cg/test_twophase.py +286 -286
  152. package/md_cg/timeboost.py +223 -0
  153. package/md_cg/tokens.py +55 -7
  154. package/md_cg/topicagg.py +142 -0
  155. package/md_cg/twophase.py +15 -6
  156. package/md_cg/whitebox_kb/wisdom/code_compose.py +33 -5
  157. package/md_cg/whitebox_kb/wisdom/compose_engine.py +5 -1
  158. package/md_cg/whitebox_kb/wisdom/role_compose.py +271 -268
  159. package/md_cg/writepipe.py +35 -3
  160. package/package.json +4 -1
  161. package/scripts/workspace_index.py +2 -0
  162. package/src/hooks.ts +182 -34
  163. package/src/index.ts +19 -3
  164. package/src/lib/console_web.mjs +30 -3
  165. package/src/lib/hook_audit.ts +15 -5
  166. package/src/lib/mdcg_client.ts +8 -4
  167. package/src/lib/prompt_safety.ts +73 -1
  168. package/src/lib/token_store.ts +5 -2
  169. package/src/tools.ts +5 -3
  170. package/ui/minimal/integrity.json +2 -2
  171. package/ui/minimal/payload/build-ui.mjs +41 -2
  172. package/ui/minimal/payload/lib/client.js +29 -3
  173. package/ui/minimal/payload/lib/lib/console_web.mjs +30 -3
  174. package/ui/minimal/payload/src/lib/console_web.mjs +30 -3
  175. package/ui/minimal/payload/web/app.js +3 -1
  176. package/ui/minimal/payload/web/client.js +30 -3
  177. package/web/app.js +3 -1
  178. package/web/client.js +30 -3
  179. package/zcode/AGENTS.md +1 -1
@@ -0,0 +1,162 @@
1
+ # -*- coding: utf-8 -*-
2
+ # 功能名:状态抽取器的「可发货性」守卫(issue 实测缺陷防复发,2026-11-02)
3
+ # 生效条件:python -X utf8 -m md_cg.test_state_extract_shipped;被测面=
4
+ # ① `md_cg/state_extract.py` 存在且是**生产方**(有 RULES/extract_turn/extract_and_record 定义)
5
+ # ② `scripts/state_extract.py` 是**薄垫片**(只 import 绑定,不定义判据)
6
+ # ③ 两者是**同一对象**(不是两份实现)
7
+ # ④ 生产方所在目录在 `package.json` 的 `files` 白名单覆盖范围内(=能随包发货)
8
+ # 子功能:A 本体在场且可执行;B 垫片无重复定义(AST 面);C 对象同一性;D 打包覆盖。
9
+ # 执行:纯静态 + 一次真抽取(temp 库),不写仓内任何文件。
10
+ # 验证方式:全部 PASS exit 0;每节注明「怎么让它红」。
11
+ # 不适用条件:不校验 npm 实际 tarball(那是 check_publish_artifact 的面);不判抽取
12
+ # 规则好坏(见 test_state_extract.py);不覆盖 DSH 端 TS 接线(宿主库内 TS 钩子
13
+ # 尚未接本链路——那是 `src/hooks.ts` 的面,且本机无 node_modules 无法构建验证,
14
+ # 不在此面冒充已验证)。
15
+ """为什么要它(真实缺陷,2026-11-02 实测):
16
+
17
+ `state_extract.py` 此前整份落在 `scripts/`,而 `package.json` 的 `files` 白名单
18
+ **只发 `scripts/workspace_index.py`**。实测 `npm pack --dry-run --ignore-scripts`:
19
+ 发货 1791 件,`scripts/` 下仅 1 件,`state_extract.py` MISSING。
20
+
21
+ 后果不是「少个文件」,而是**每轮记账链在装机环境整条断掉**:装机侧
22
+ `sync_zcode_session._load_state_extract()` 装载不到抽取器 ⇒ `state_events.append`
23
+ 永不发生 ⇒ 生产台账 `_state_events.jsonl` 长期 `events=0`(`state_slots --status`
24
+ 实测)。世界模型 L2 的读面(`state_chain`/`state_slots`/`alternatives`)做得再准,
25
+ 源头无数据就是空转。
26
+
27
+ **发布闸为什么不拦**:`scripts/check_publish_artifact.py` 的 R1–R7 校验的是
28
+ 「**清单内的文件安不安全**」(凭据/私有数据/隐私/非追踪件/路径逃逸/…),
29
+ 不校验「**必需文件在不在清单里**」——它没有「必需件」概念,故白名单漏项是它的
30
+ 盲区。本守卫补的正是这个盲区:把「运行时被装载的模块,其所在目录必须被 files
31
+ 白名单覆盖」钉成断言。
32
+
33
+ 判据单点:**生产方必须在可发货目录内**。`md_cg/` 整目录在白名单里;`scripts/`
34
+ 只剩 `workspace_index.py`。故本体归 `md_cg/`,垫片归 `scripts/`。
35
+ """
36
+ from __future__ import annotations
37
+
38
+ import ast
39
+ import json
40
+ import os
41
+ import shutil
42
+ import sys
43
+ import tempfile
44
+
45
+ HERE = os.path.dirname(os.path.abspath(__file__))
46
+ REPO = os.path.dirname(HERE)
47
+ sys.path.insert(0, REPO)
48
+
49
+ _PASS, _FAIL = [], []
50
+
51
+
52
+ def ok(cond, msg, detail=""):
53
+ (_PASS if cond else _FAIL).append(msg)
54
+ print((" PASS " if cond else " FAIL ") + msg
55
+ + ((" <- " + str(detail)) if detail and not cond else ""))
56
+
57
+
58
+ #: 生产方的公开面:垫片**不得**重新定义其中任何一个(否则就是两份实现)。
59
+ _PROD_SYMS = ("RULES", "extract_turn", "extract_and_record", "enabled",
60
+ "DEFAULT_SUBJECT")
61
+
62
+
63
+ def _defined_names(path):
64
+ """AST 面:该文件里**自己定义**的顶层名(函数/类/赋值目标)。"""
65
+ tree = ast.parse(open(path, encoding="utf-8").read(), filename=path)
66
+ out = set()
67
+ for node in tree.body:
68
+ if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
69
+ out.add(node.name)
70
+ elif isinstance(node, ast.Assign):
71
+ for t in node.targets:
72
+ if isinstance(t, ast.Name):
73
+ out.add(t.id)
74
+ elif isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name):
75
+ out.add(node.target.id)
76
+ return out
77
+
78
+
79
+ def main():
80
+ core_p = os.path.join(HERE, "state_extract.py")
81
+ shim_p = os.path.join(REPO, "scripts", "state_extract.py")
82
+ pkg_p = os.path.join(REPO, "package.json")
83
+
84
+ print("== A 本体在场(怎么让它红:删掉 md_cg/state_extract.py)")
85
+ ok(os.path.exists(core_p), "A1 md_cg/state_extract.py 存在")
86
+ if not os.path.exists(core_p):
87
+ print("\n本体缺失,后续断言无从进行")
88
+ return 1
89
+ core_defs = _defined_names(core_p)
90
+ for s in ("RULES", "extract_turn", "extract_and_record"):
91
+ ok(s in core_defs, "A2 本体定义了生产面 %s" % s)
92
+
93
+ print("== B 垫片无重复实现(怎么让它红:把原实现抄回 scripts/state_extract.py)")
94
+ ok(os.path.exists(shim_p), "B1 scripts/state_extract.py 仍在(既有 import 路径不变)")
95
+ if os.path.exists(shim_p):
96
+ shim_defs = _defined_names(shim_p)
97
+ dup = sorted(shim_defs & set(_PROD_SYMS))
98
+ ok(not dup, "B2 垫片不得重新定义生产面符号(发现重复定义即两份判据)", dup)
99
+ ok("enabled" not in dup and "extract_turn" not in dup,
100
+ "B3 关键两函数只在 md_cg 定义一次")
101
+
102
+ print("== C 对象同一性(怎么让它红:让垫片自己包一层 wrapper 函数)")
103
+ import importlib.util
104
+ from md_cg import state_extract as core
105
+ spec = importlib.util.spec_from_file_location(
106
+ "_shim_probe", shim_p) if os.path.exists(shim_p) else None
107
+ if spec is not None:
108
+ m = importlib.util.module_from_spec(spec)
109
+ spec.loader.exec_module(m)
110
+ ok(m.extract_turn is core.extract_turn,
111
+ "C1 垫片 extract_turn 与本体是同一对象")
112
+ ok(m.extract_and_record is core.extract_and_record,
113
+ "C2 垫片 extract_and_record 与本体是同一对象")
114
+ ok(m.RULES is core.RULES, "C3 规则表是同一对象(无第二份规则)")
115
+
116
+ print("== D 打包覆盖(怎么让它红:把本体移回 scripts/ 或删掉 files 里的 md_cg)")
117
+ pkg = json.load(open(pkg_p, encoding="utf-8"))
118
+ files = pkg.get("files") or []
119
+ # `files` 支持目录前缀("md_cg")——生产方所在目录须被某条目覆盖。
120
+ core_dir = os.path.basename(HERE) # "md_cg"
121
+ covered = any(
122
+ entry == core_dir or entry.startswith(core_dir + "/")
123
+ for entry in files)
124
+ ok(covered, "D1 生产方目录 %r 被 package.json files 白名单覆盖" % core_dir,
125
+ files)
126
+ ok("scripts" not in files,
127
+ "D2 scripts/ 不是整目录发货(故放生产方进去即漏发——本缺陷的成因)",
128
+ files)
129
+ ok(os.path.basename(core_p) == "state_extract.py",
130
+ "D3 生产方文件名与既有装载点期望一致")
131
+
132
+ print("== E 端到端(怎么让它红:让 extract_and_record 不落台账)")
133
+ saved = {k: os.environ.pop(k) for k in list(os.environ) if k.startswith("MDCG_")}
134
+ root = tempfile.mkdtemp(prefix="mdcg_ship_")
135
+ try:
136
+ os.environ["MDCG_ROOT"] = root
137
+ os.environ["MDCG_STATE_ROOT"] = os.path.join(root, "state")
138
+ os.environ["MDCG_AUX_ROOT"] = os.path.join(root, "aux-root")
139
+ from md_cg.mdcos import MdCGOS
140
+ from md_cg import state_events as _se
141
+ cg = MdCGOS(root)
142
+ n = core.extract_and_record(cg, "s-ship", 1, "user", "我搬到青岛了")
143
+ evs = _se.read(cg)
144
+ ok(n == 1 and evs and evs[-1]["new"] == "青岛",
145
+ "E1 从 md_cg 本体可直接抽取并落台账(不经 scripts/)", (n, evs))
146
+ finally:
147
+ for k in list(os.environ):
148
+ if k.startswith("MDCG_"):
149
+ os.environ.pop(k, None)
150
+ os.environ.update(saved)
151
+ shutil.rmtree(root, ignore_errors=True)
152
+
153
+ print()
154
+ print("状态抽取器可发货性守卫:%d 通过 / %d 失败" % (len(_PASS), len(_FAIL)))
155
+ if _FAIL:
156
+ for f in _FAIL:
157
+ print(" -", f)
158
+ return 0 if not _FAIL else 1
159
+
160
+
161
+ if __name__ == "__main__":
162
+ raise SystemExit(main())
@@ -0,0 +1,207 @@
1
+ # -*- coding: utf-8 -*-
2
+ # 功能名:状态事件台账「整链可达性」守卫(把可发货性判据从「一个文件」升级为「一条链」)
3
+ # 生效条件:python -X utf8 -m md_cg.test_state_ledger_chain;被测面=状态事件台账的
4
+ # 四段链路 ①落盘点 ②生产方 ③触发点 ④端到端可达 —— 每一段都必须在**随包发货面**内。
5
+ # 子功能:A 落盘点单点性;B 生产方随包;C 触发点随包且非测试;D 跨端(TS)接线;
6
+ # E 端到端可达性合成判定。
7
+ # 执行:纯静态(AST + package.json 白名单 + 文本扫描),零落盘、零网络。
8
+ # 验证方式:全绿 exit 0;**红的每一条都是链路上真实缺环**,不是环境缺失。
9
+ # 不适用条件:不判抽取规则好坏(见 test_state_extract.py);不判台账读写语义
10
+ # (见 test_state_events.py / test_state_slots.py);不判 npm 实际 tarball。
11
+
12
+ """为什么要它(2026-11-02,对上一轮修复的**自我纠正**):
13
+
14
+ 上一轮修了「`md_cg/state_extract.py` 未随包发货」,并加了守卫
15
+ `test_state_extract_shipped.py`。那条守卫钉住的判据是——
16
+ **「运行时被装载的模块,其所在目录必须被 files 白名单覆盖」**。
17
+
18
+ 它对,但**只覆盖了链路末端**。真实链是三段:
19
+
20
+ 触发点(sync_zcode_session.sync_to_window)
21
+ → 生产方(state_extract.extract_and_record)
22
+ → 落盘点(state_events.append → _state_events.jsonl)
23
+
24
+ 实测(2026-11-02):
25
+ · 落盘点 `md_cg/state_events.py` SHIPPED ✓
26
+ · 生产方 `md_cg/state_extract.py` SHIPPED ✓(上一轮迁入)
27
+ · 触发点 `scripts/sync_zcode_session.py` **MISSING**(files 白名单只发 scripts/workspace_index.py)
28
+
29
+ 即:**上一轮的守卫会绿,而链仍是断的。** 判据覆盖不到触发点,
30
+ 等于把一个"装载不到的模块"修成了"装得上但没人调"。
31
+
32
+ 这不是新缺陷,是**判据落点选错**——与本仓多次栽过的
33
+ 「同一判据两个落点」同族:我把"可发货"钉在了**被调方**上,
34
+ 而断点在**调用方**。
35
+
36
+ 另一条独立断点(DSH 端,更深):`src/hooks.ts` 每轮调
37
+ `graph.recentAdd()` 写 `_recent.jsonl`(实测在跑),但**全文件无
38
+ `state_event` / `extract_and_record` / `state_extract` 任何字样**——
39
+ DSH 端从未接入状态抽取。zcode 端脚本又不在装机包里。
40
+ 故装机环境里 `_state_events.jsonl` **零生产路径**(实测该文件 ABSENT)。
41
+
42
+ 本守卫把三段一起钉住,并要求**至少存在一条端到端可达**的路径。
43
+ """
44
+
45
+ from __future__ import annotations
46
+
47
+ import ast
48
+ import json
49
+ import os
50
+ import re
51
+ import sys
52
+
53
+ HERE = os.path.dirname(os.path.abspath(__file__))
54
+ REPO = os.path.dirname(HERE)
55
+ sys.path.insert(0, REPO)
56
+
57
+ _PASS, _FAIL = [], []
58
+
59
+
60
+ def ok(cond, msg, detail=""):
61
+ (_PASS if cond else _FAIL).append(msg)
62
+ print((" PASS " if cond else " FAIL ") + msg
63
+ + ((" <- " + str(detail)) if detail and not cond else ""))
64
+
65
+
66
+ # ── 判据单点:随包发货面 ────────────────────────────────────────────────
67
+ def _shipped_predicate():
68
+ """返回 shipped(rel)->bool;`files` 支持目录前缀("md_cg" 覆盖整目录)。"""
69
+ pkg = json.load(open(os.path.join(REPO, "package.json"), encoding="utf-8"))
70
+ entries = [e.replace("\\", "/").rstrip("/") for e in (pkg.get("files") or [])]
71
+
72
+ def shipped(rel):
73
+ rel = rel.replace("\\", "/")
74
+ return any(rel == e or rel.startswith(e + "/") for e in entries)
75
+ return shipped, entries
76
+
77
+
78
+ def _py_files():
79
+ """仓内全部 .py(跳过构建产物与依赖目录)。"""
80
+ out = []
81
+ for root, dirs, fs in os.walk(REPO):
82
+ dirs[:] = [d for d in dirs
83
+ if d not in {"node_modules", ".git", "__pycache__", "lib", "web"}]
84
+ for fn in fs:
85
+ if fn.endswith(".py"):
86
+ out.append(os.path.relpath(os.path.join(root, fn), REPO).replace("\\", "/"))
87
+ return sorted(out)
88
+
89
+
90
+ def _is_test(rel):
91
+ base = os.path.basename(rel)
92
+ return base.startswith("test_") or "/test/" in rel or rel.startswith("test/")
93
+
94
+
95
+ def _callers_of(sym, files):
96
+ """AST 面:哪些模块**调用了** sym(排除定义处与测试件)。"""
97
+ hits = []
98
+ for rel in files:
99
+ try:
100
+ tree = ast.parse(open(os.path.join(REPO, rel), encoding="utf-8").read(),
101
+ filename=rel)
102
+ except (SyntaxError, UnicodeDecodeError):
103
+ continue
104
+ for node in ast.walk(tree):
105
+ if not isinstance(node, ast.Call):
106
+ continue
107
+ f = node.func
108
+ name = None
109
+ if isinstance(f, ast.Attribute):
110
+ name = f.attr
111
+ elif isinstance(f, ast.Name):
112
+ name = f.id
113
+ if name == sym:
114
+ ln = getattr(node, "lineno", 0)
115
+ hits.append((rel, ln))
116
+ return hits
117
+
118
+
119
+ def main():
120
+ shipped, entries = _shipped_predicate()
121
+ files = _py_files()
122
+
123
+ # ── A 落盘点 ────────────────────────────────────────────────────
124
+ print("== A 落盘点(怎么让它红:把 state_events.py 移出 md_cg/ 或删 files 里的 md_cg)")
125
+ se_rel = "md_cg/state_events.py"
126
+ ok(os.path.exists(os.path.join(REPO, se_rel)), "A1 落盘模块在场 %s" % se_rel)
127
+ ok(shipped(se_rel), "A2 落盘模块随包发货", entries)
128
+
129
+ # append 必须是唯一落盘单点:别处不得自己 open(ledger,'a')
130
+ body = open(os.path.join(REPO, se_rel), encoding="utf-8").read()
131
+ ok("def append(" in body, "A3 落盘单点 state_events.append 存在")
132
+ foreign_writers = []
133
+ for rel in files:
134
+ if rel == se_rel or _is_test(rel):
135
+ continue
136
+ s = open(os.path.join(REPO, rel), encoding="utf-8").read()
137
+ if "_state_events.jsonl" in s and re.search(r"open\s*\([^)]*['\"]a", s):
138
+ foreign_writers.append(rel)
139
+ ok(not foreign_writers,
140
+ "A4 无第二个落盘点(别处不得直接以追加模式打开台账)", foreign_writers)
141
+
142
+ # ── B 生产方 ────────────────────────────────────────────────────
143
+ print("== B 生产方(怎么让它红:把 state_extract.py 移回 scripts/)")
144
+ sx_rel = "md_cg/state_extract.py"
145
+ ok(os.path.exists(os.path.join(REPO, sx_rel)), "B1 生产模块在场 %s" % sx_rel)
146
+ ok(shipped(sx_rel), "B2 生产模块随包发货", entries)
147
+ ok("def extract_and_record(" in open(os.path.join(REPO, sx_rel),
148
+ encoding="utf-8").read(),
149
+ "B3 生产面 extract_and_record 存在")
150
+
151
+ # ── C 触发点(上一轮漏掉的一环)──────────────────────────────────
152
+ print("== C 触发点(怎么让它红:删掉 mcp_server 的 recent 分支抽取块)")
153
+ callers = [(r, l) for r, l in _callers_of("extract_and_record", files)
154
+ if r != sx_rel and not _is_test(r)]
155
+ ok(bool(callers), "C1 存在非测试的 extract_and_record 调用点(触发点)",
156
+ "整仓零触发点 ⇒ 生产方永不执行")
157
+ live = [r for r, _ in callers if shipped(r)]
158
+ ok(bool(live),
159
+ "C2 至少一个触发点在随包发货面内(装机环境可达)",
160
+ "可发货触发点=0 ⇒ 装机的包里有抽取器却没人调")
161
+
162
+ # ── D 双端覆盖(判据已修正,见头注)──────────────────────────────
163
+ print("== D 双端覆盖(怎么让它红:把 recent add 分支里的抽取块删掉)")
164
+ srv = open(os.path.join(REPO, "md_cg", "mcp_server.py"), encoding="utf-8").read()
165
+ # 取出 recent 分支的源码段(到 state_event 分支为止),只在该段内找抽取调用
166
+ m = re.search(r'if op == "recent":(.*?)\n if op == "state_event":',
167
+ srv, flags=re.S)
168
+ recent_seg = m.group(1) if m else ""
169
+ # 去注释,避免「文档里提过」被当成「接上了」(本轮判据修正的直接教训)
170
+ seg_code = re.sub(r"#[^\n]*", "", recent_seg)
171
+ ok("extract_and_record" in seg_code,
172
+ "D1 服务端 recent·add 分支(双端共用单点)内直接调用抽取器",
173
+ "recent 分支是 DSH 与 zcode 都必经的出口;不在此则必有一端覆盖不到")
174
+
175
+ hooks = os.path.join(REPO, "src", "hooks.ts")
176
+ if os.path.exists(hooks):
177
+ s = open(hooks, encoding="utf-8").read()
178
+ code = re.sub(r"/\*.*?\*/", "", s, flags=re.S)
179
+ code = re.sub(r"^\s*//.*$", "", code, flags=re.M)
180
+ ok("recentAdd" in code,
181
+ "D2 DSH 端经 recentAdd 走上述单点(故不加 TS 调用也覆盖到)")
182
+ else:
183
+ ok(False, "D2 src/hooks.ts 不在(发行包内无 TS 源)", hooks)
184
+
185
+ ok(shipped("md_cg/mcp_server.py"),
186
+ "D3 服务端触发点本身随包发货", entries)
187
+
188
+ # ── E 端到端可达性合成 ──────────────────────────────────────────
189
+ print("== E 端到端可达性(三段同绿才算通)")
190
+ seg_land = os.path.exists(os.path.join(REPO, se_rel)) and shipped(se_rel)
191
+ seg_prod = os.path.exists(os.path.join(REPO, sx_rel)) and shipped(sx_rel)
192
+ seg_trig = bool(live) and "extract_and_record" in seg_code
193
+ ok(seg_land and seg_prod and seg_trig,
194
+ "E1 存在「触发→生产→落盘」全段可达的链路(装机环境能真正写出事件)",
195
+ "落盘=%s 生产=%s 触发=%s" % (seg_land, seg_prod, seg_trig))
196
+
197
+ print()
198
+ print("状态事件台账整链可达性:%d 通过 / %d 失败" % (len(_PASS), len(_FAIL)))
199
+ if _FAIL:
200
+ print(" 失败项(每一条都是链路上的真实缺环):")
201
+ for f in _FAIL:
202
+ print(" -", f)
203
+ return 0 if not _FAIL else 1
204
+
205
+
206
+ if __name__ == "__main__":
207
+ raise SystemExit(main())
@@ -0,0 +1,145 @@
1
+ # -*- coding: utf-8 -*-
2
+ # 功能名:状态事件台账「双端触发点」端到端守卫
3
+ # 生效条件:python -X utf8 -m md_cg.test_state_ledger_e2e;被测面=服务端
4
+ # `recent`·add 分支(DSH 的 recentAdd 与 zcode 的 sync_to_window **都**经过它)
5
+ # 是否真的把真人轮推进状态台账。
6
+ # 子功能:① DSH 形状触发 ② zcode 形状触发 ③ 三类负例不抽 ④ 同值去重 ⑤ 证据字段。
7
+ # 执行:temp 隔离库(MDCG_ROOT 全改),走**真实分发入口** `_cg_dispatch`,
8
+ # 不直接调抽取器——否则测的是抽取器而不是接线。
9
+ # 验证方式:5/5 PASS exit 0。
10
+ # 不适用条件:不判抽取规则是否该收窄(见 scripts/test_state_extract.py 的 S4b);
11
+ # 不判台账读写语义(见 test_state_events.py);不覆盖 npm 实际发货内容。
12
+
13
+ """为什么要它(2026-11-02):
14
+
15
+ `test_state_ledger_chain.py` 只能静态证明「链在源码里是通的」。静态绿不等于
16
+ 运行时真落盘——本仓有过「文件能 import ⇒ 以为判据成立」的假绿取证。
17
+ 故这条守卫做**真调用**:用 `_cg_dispatch` 模拟两端各自的真实载荷形状,
18
+ 读回 `state_events.read` 验落盘。
19
+
20
+ 关键:**必须走分发入口,不能直接调 `extract_and_record`**。
21
+ 直接调抽取器能过,但接线块被删掉照样能过——那就是假绿。
22
+
23
+ 夹具坑(本机实测):`MDCG_AUX_ROOT` 末段不能是 Windows 保留设备名
24
+ (首版用 `aux` 被 datapath 守卫拒;改用 `auxdata`)。
25
+ """
26
+
27
+ from __future__ import annotations
28
+
29
+ import os
30
+ import shutil
31
+ import sys
32
+ import tempfile
33
+
34
+ REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
35
+ sys.path.insert(0, REPO)
36
+
37
+ _P, _F = [], []
38
+
39
+
40
+ def ok(cond, msg, detail=""):
41
+ (_P if cond else _F).append(msg)
42
+ print((" PASS " if cond else " FAIL ") + msg
43
+ + ((" <- " + str(detail)) if detail and not cond else ""))
44
+
45
+
46
+ def main():
47
+ saved = {k: os.environ.pop(k) for k in list(os.environ) if k.startswith("MDCG_")}
48
+ root = tempfile.mkdtemp(prefix="chain_e2e_")
49
+ try:
50
+ os.environ["MDCG_ROOT"] = root
51
+ os.environ["MDCG_STATE_ROOT"] = os.path.join(root, "st")
52
+ # 末段不得是保留设备名(aux/con/nul/prn/com1…)——datapath 守卫会拒。
53
+ os.environ["MDCG_AUX_ROOT"] = os.path.join(root, "auxdata")
54
+ from md_cg.mdcos import MdCGOS
55
+ from md_cg import state_events as SE
56
+ from md_cg.mcp_server import _cg_dispatch
57
+ cg = MdCGOS(root)
58
+
59
+ def add(**kw):
60
+ return _cg_dispatch(cg, dict({"op": "recent", "action": "add"}, **kw))
61
+
62
+ print("== ① DSH 端形状(src/hooks.ts 的 recentAdd:meta 带 session)")
63
+ add(role="user", text="我搬到青岛了",
64
+ meta={"session": "s-dsh", "turn": 1})
65
+ evs = SE.read(cg)
66
+ ok(len(evs) == 1 and evs[0].get("new") == "青岛",
67
+ "① DSH 形状的窗口写入触发了状态记账", evs)
68
+
69
+ print("== ② zcode 端形状(sync_to_window:同分支不同 session)")
70
+ add(role="user", text="我最喜欢海边的日落",
71
+ meta={"session": "s-zcode", "turn": 1})
72
+ evs = SE.read(cg)
73
+ ok(len(evs) == 2 and evs[-1].get("new") == "海边的日落",
74
+ "② zcode 形状同样覆盖(证明单点覆盖双端)", evs)
75
+
76
+ print("== ③ 负例(怎么让它红:去掉 role/session 两个前置条件)")
77
+ n0 = len(SE.read(cg))
78
+ add(role="assistant", text="我搬到火星了",
79
+ meta={"session": "s-dsh", "turn": 2}) # 助手轮
80
+ add(role="user", text="我搬到月球了", meta={}) # 无 session
81
+ add(role="user", text="今天写了点代码",
82
+ meta={"session": "s-dsh", "turn": 3}) # 无规则命中
83
+ n1 = len(SE.read(cg))
84
+ ok(n1 == n0, "③ 助手轮 / 无 session / 无命中:一律不抽", (n0, n1))
85
+
86
+ print("== ④ 同值去重(怎么让它红:删掉 extract_and_record 里的 dedupe)")
87
+ add(role="user", text="我已经搬到了青岛",
88
+ meta={"session": "s-dsh", "turn": 4})
89
+ n2 = len(SE.read(cg))
90
+ ok(n2 == n1, "④ 同值不重复记账(防每轮重复写)", (n1, n2))
91
+
92
+ print("== ⑤ 证据字段(可整体甄别/清理的前提)")
93
+ last = SE.read(cg)[-1]
94
+ ok(str(last.get("actor")) == "state_extract"
95
+ and "[auto]" in str(last.get("evidence")),
96
+ "⑤ actor=state_extract 且 evidence 带 [auto] session/turn", last)
97
+
98
+ print("== ⑥ 隔离:抽取抛异常不得影响窗口写入")
99
+ # 为什么必须有这条(变异 M4 实测漏网):
100
+ # 上面 ⑤ 条都建立在「抽取器不抛异常」这个隐含前提上。若把 fail-soft
101
+ # 的 `except Exception` 收窄成某个具体异常,抽取器一旦真抛(规则表未来
102
+ # 改动、盘面权限、编码问题),异常会越过 recent 分支冒到窗口写入调用方
103
+ # ——**为了一个派生面赔掉运行态面**。这条断言专门钉住该边界。
104
+ from md_cg import state_extract as _sx
105
+ _orig = _sx.extract_and_record
106
+
107
+ def _boom(*a, **k):
108
+ raise RuntimeError("模拟抽取器内部故障")
109
+
110
+ _sx.extract_and_record = _boom
111
+ try:
112
+ # 断言必须能红:拿"窗口写入真的成功了"作判据——查 _recent 里
113
+ # 确实有这一轮,而不是只看"没抛异常"(后者过于宽松,恒真)。
114
+ r = add(role="user", text="我搬到威海了",
115
+ meta={"session": "s-dsh", "turn": 9})
116
+ wins = cg.recent_events(limit=0)
117
+ got = [w for w in wins
118
+ if "威海" in str(w.get("text") or "")]
119
+ ok(bool(got),
120
+ "⑥a 抽取器抛异常时窗口写入仍成功(派生面故障不拖垮运行态面)",
121
+ "recent 窗口里找不到该轮;返回=%r" % (r,))
122
+ # 且该轮确实没落台账(故障态不漏记也不假记)
123
+ ok(not any(e.get("new") == "威海" for e in SE.read(cg)),
124
+ "⑥b 故障轮不产生半截台账记录")
125
+ except Exception as exc: # noqa: BLE001
126
+ ok(False, "⑥a 抽取器抛异常时窗口写入仍成功(派生面故障不拖垮运行态面)",
127
+ exc)
128
+ finally:
129
+ _sx.extract_and_record = _orig
130
+ finally:
131
+ for k in list(os.environ):
132
+ if k.startswith("MDCG_"):
133
+ os.environ.pop(k, None)
134
+ os.environ.update(saved)
135
+ shutil.rmtree(root, ignore_errors=True)
136
+
137
+ print()
138
+ print("状态台账双端触发点端到端:%d 通过 / %d 失败" % (len(_P), len(_F)))
139
+ for f in _F:
140
+ print(" -", f)
141
+ return 0 if not _F else 1
142
+
143
+
144
+ if __name__ == "__main__":
145
+ raise SystemExit(main())
@@ -0,0 +1,102 @@
1
+ # -*- coding: utf-8 -*-
2
+ """结构信号重排(structboost)守卫。
3
+
4
+ 钉五件事:
5
+ 1. 章节解析(中文数字/阿拉伯数字);
6
+ 2. 章号来源优先级:正文标题 > ch:N 盖章 tag;
7
+ 3. **信息不足闸**:候选集里知道章号的比例 < CHAPTER_MIN_KNOWN ⇒ 章节信号整条弃用;
8
+ 4. 确定性:同输入两次结果逐位相同;
9
+ 5. 缺省关:env 未设时 search_rrf 不改任何顺序。
10
+ """
11
+ from __future__ import annotations
12
+
13
+ import json
14
+ import os
15
+ import sys
16
+ import tempfile
17
+
18
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
19
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
20
+ sys.path.insert(0, _REPO)
21
+
22
+ from md_cg import structboost as sb # noqa: E402
23
+ from md_cg.mdcos import MdCGOS # noqa: E402
24
+
25
+ FAILS = []
26
+
27
+
28
+ def ok(cond, name, detail=""):
29
+ if cond:
30
+ print(" PASS " + name)
31
+ else:
32
+ print(" FAIL " + name + " " + str(detail)[:180])
33
+ FAILS.append(name)
34
+
35
+
36
+ def node(nid, content, tags=None):
37
+ return {"id": nid, "content": content, "frontmatter": {"tags": tags or []}}
38
+
39
+
40
+ def main():
41
+ print("=" * 66)
42
+ print("结构信号重排(structboost)")
43
+ print("=" * 66)
44
+ ok(sb.cn2int("三") == 3 and sb.cn2int("十二") == 12 and sb.cn2int("21") == 21,
45
+ "S1 章号解析(中文/阿拉伯)", (sb.cn2int("三"), sb.cn2int("十二"), sb.cn2int("21")))
46
+ ok(sb.query_chapter("第2章「巧合还是阴谋」·陈默") == 2, "S2 题面取章号")
47
+
48
+ with_head = node("a", "## 第三章 身份谜团\n正文" + "x" * 50)
49
+ with_tag = node("b", "没有标题的块" + "y" * 50, tags=["ch:5"])
50
+ ok(sb.node_chapter(with_head) == 3, "S3a 正文标题优先", sb.node_chapter(with_head))
51
+ ok(sb.node_chapter(with_tag) == 5, "S3b ch:N 盖章 tag 可识别", sb.node_chapter(with_tag))
52
+ ok(sb.node_chapter(node("c", "无任何章信息")) is None, "S3c 无章号返回 None")
53
+
54
+ # 信息不足闸:10 个候选里只有 1 个知道章号 ⇒ 章节信号弃用。
55
+ # 夹具要「独苗分数低于他人」才有区分度:若闸失灵,×2 提升会把独苗顶到第一。
56
+ few = [(node("h", "## 第二章 x\n" + "z" * 60), 0.30, {}, [])]
57
+ few += [(node("n%d" % i, "无关" + "w" * 60), 0.40, {}, []) for i in range(9)]
58
+ out_few = sb.rerank(few, "第2章 某陈述")
59
+ ok(out_few[0][0]["id"] != "h",
60
+ "S4 信息不足闸:章节信号整条弃用(低分独苗不被顶到第一)",
61
+ [x[0]["id"] for x in out_few[:3]])
62
+
63
+ # 确定性命中:章号齐备时,同章候选被提升
64
+ many = [(node("g", "## 第二章 y\n" + "k" * 60), 0.3, {}, [])]
65
+ many += [(node("m%d" % i, "## 第一章 z\n" + "k" * 60), 0.4, {}, []) for i in range(3)]
66
+ ranked = sb.rerank(many, "第2章 陈述")
67
+ ok(ranked[0][0]["id"] == "g", "S5 章号齐备时同章候选被提升", [x[0]["id"] for x in ranked])
68
+
69
+ again = sb.rerank(many, "第2章 陈述")
70
+ ok([x[0]["id"] for x in ranked] == [x[0]["id"] for x in again], "S6 确定性(同输入同输出)")
71
+
72
+ # S7 缺省关:env 未设时 search_rrf 顺序不变
73
+ os.environ.pop("MDCG_STRUCT_BOOST", None)
74
+ os.environ["MDCG_RECENT_TAIL"] = "0"
75
+ os.environ["MDCG_TOPIC_AGG"] = "0"
76
+ g = MdCGOS(tempfile.mkdtemp(prefix="sbg_"), actor="test")
77
+ ccg = ("# 功能名:{n}\n# 生效条件:语料\n# 子功能:s\n# 执行:e\n"
78
+ "# 验证方式:bench\n# 不适用条件:无\n\n{body}\n")
79
+ g.add("c1", ccg.format(n="第一章块", s="w", e="1", body="## 第一章 开端\n话题甲 内容"),
80
+ tags=["ch:1"])
81
+ g.add("c2", ccg.format(n="第二章块", s="w", e="2", body="## 第二章 发展\n话题甲 内容"),
82
+ tags=["ch:2"])
83
+ res0, meta0 = g.search_rrf("话题甲", k=5)
84
+ ok(meta0.get("struct_boost") is False, "S7a 缺省关(meta.struct_boost=False)",
85
+ meta0.get("struct_boost"))
86
+ os.environ["MDCG_STRUCT_BOOST"] = "1"
87
+ try:
88
+ res1, meta1 = g.search_rrf("第2章 话题甲", k=5)
89
+ ok(meta1.get("struct_boost") is True, "S7b 开启后 meta.struct_boost=True")
90
+ ok(len(res1) <= 5, "S7c 对外仍只回 k 条(超取→重排→截断)", len(res1))
91
+ finally:
92
+ os.environ.pop("MDCG_STRUCT_BOOST", None)
93
+ os.environ.pop("MDCG_RECENT_TAIL", None)
94
+ os.environ.pop("MDCG_TOPIC_AGG", None)
95
+
96
+ print()
97
+ print("structboost:%d 项失败" % len(FAILS) if FAILS else "structboost:全部通过")
98
+ return 1 if FAILS else 0
99
+
100
+
101
+ if __name__ == "__main__":
102
+ sys.exit(main())