@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
@@ -274,12 +274,37 @@ def check_call_site_not_toplevel():
274
274
  % (len(found), ", ".join("%s:%d" % (f[2], f[0]) for f in found)))
275
275
 
276
276
 
277
- # 五:键集对拍——对临时 root 上真起的 MCP 服务调 cg(op=info)
277
+ # issue #677(B,2026-11-04):`info` 拆快/慢后的新契约——
278
+ # 缺省 = **快照**(内存态 + 轻诊断,不含 health_os 慢面);
279
+ # `full=true` = 旧全集(含 PINNED_INFO_KEYS 里 health_os 的键)。
280
+ # 本守卫从「逐字钉死 20 键」改为「两条腿」:快照键必须齐、且快照 ⊂ full;full 必须含旧 20 键。
281
+ FAST_INFO_KEYS = set(NEW_INFO_KEYS) | {
282
+ "surface", "tools", "whoami", "node_count", "flush_state", "full",
283
+ "audit_kinds", "ccg_contract", "external_verifiers",
284
+ "write_policy", "write_gate", "theory", "links",
285
+ }
286
+ #: 快照模式**新增**的键(除 NEW_INFO_KEYS 外)——只允许这三个出现在 full 的「多出」里。
287
+ FAST_ONLY_KEYS = {"flush_state", "full", "node_count"}
288
+
289
+
290
+ def _parse_info(stdout, rid):
291
+ for line in stdout.splitlines():
292
+ line = line.strip()
293
+ if not line:
294
+ continue
295
+ try:
296
+ obj = json.loads(line)
297
+ except ValueError:
298
+ continue
299
+ if obj.get("id") == rid and "result" in obj:
300
+ return json.loads(obj["result"]["content"][0]["text"])
301
+ return None
302
+
303
+
304
+ # 五:键集对拍——对临时 root 上真起的 MCP 服务调 cg(op=info)(快照 + full 两条腿)
278
305
  def check_info_keys(tmp):
279
- print("\n== 五 键集对拍:cg(op=info) 真调(临时 root) ==")
306
+ print("\n== 五 键集对拍:cg(op=info) 快照/full 两条腿(临时 root) ==")
280
307
  env = _child_env(tmp)
281
- # 本条要验的是**工作区里这套接线**(不是副本),故包来源显式钉到当前树:
282
- # 记忆 root / aux / DSH_HOME 仍是临时目录(隔离),只有代码取自当前树。
283
308
  env["PYTHONPATH"] = REPO
284
309
  lines = [
285
310
  {"jsonrpc": "2.0", "id": 1, "method": "initialize",
@@ -287,7 +312,9 @@ def check_info_keys(tmp):
287
312
  "clientInfo": {"name": "generation-guard", "version": "0"}}},
288
313
  {"jsonrpc": "2.0", "id": 2, "method": "tools/call",
289
314
  "params": {"name": "cg", "arguments": {"op": "info"}}},
290
- {"jsonrpc": "2.0", "id": 3, "method": "shutdown", "params": {}},
315
+ {"jsonrpc": "2.0", "id": 3, "method": "tools/call",
316
+ "params": {"name": "cg", "arguments": {"op": "info", "full": True}}},
317
+ {"jsonrpc": "2.0", "id": 4, "method": "shutdown", "params": {}},
291
318
  ]
292
319
  payload = "\n".join(json.dumps(x, ensure_ascii=False) for x in lines) + "\n"
293
320
  r = subprocess.run([sys.executable, "-X", "utf8", "-m", "md_cg.mcp_server"],
@@ -295,50 +322,44 @@ def check_info_keys(tmp):
295
322
  text=True, encoding="utf-8", errors="replace",
296
323
  shell=False, timeout=600)
297
324
  if r.returncode != 0:
298
- _fail("五·键集", "mcp_server 退出码 %s(代校验接线可能把启动打死了):%s"
299
- % (r.returncode, (r.stderr or "")[-700:]))
325
+ _fail("五·键集", "mcp_server 退出码 %s:%s" % (r.returncode, (r.stderr or "")[-700:]))
300
326
  return
301
- info = None
302
- for line in r.stdout.splitlines():
303
- line = line.strip()
304
- if not line:
305
- continue
306
- try:
307
- obj = json.loads(line)
308
- except ValueError:
309
- continue
310
- if obj.get("id") == 2 and "result" in obj:
311
- info = json.loads(obj["result"]["content"][0]["text"])
312
- if info is None:
313
- _fail("五·键集", "没取到 cg(op=info) 响应:stdout=%s" % (r.stdout or "")[:400])
327
+ fast = _parse_info(r.stdout, 2)
328
+ full = _parse_info(r.stdout, 3)
329
+ if fast is None or full is None:
330
+ _fail("五·键集", "没取到 info 响应:stdout=%s" % (r.stdout or "")[:400])
314
331
  return
315
- keys = set(info)
316
- missing = [k for k in NEW_INFO_KEYS if k not in keys]
317
- if missing:
318
- _fail("五·键集", "缺新键:%s" % missing)
332
+ missing_fast = sorted(FAST_INFO_KEYS - set(fast))
333
+ if missing_fast:
334
+ _fail("五·键集", "快照缺键:%s" % missing_fast)
319
335
  return
320
- extra = sorted(keys - PINNED_INFO_KEYS - set(NEW_INFO_KEYS))
321
- lost = sorted(PINNED_INFO_KEYS - keys)
322
- if extra or lost:
323
- _fail("五·键集", "既有键集被改动:多出 %s / 丢失 %s" % (extra, lost))
336
+ if fast.get("full") is not False:
337
+ _fail("五·键集", "快照模式 full 应为 False,实得 %r" % (fast.get("full"),))
324
338
  return
325
- if info["code_generation"] != info["disk_generation"]:
326
- _fail("五·键集", "刚起的进程自报跨代:code=%s disk=%s"
327
- % (info["code_generation"], info["disk_generation"]))
339
+ if "os" in fast:
340
+ _fail("五·键集", "快照模式不得含 health_os 慢面(os 段)")
328
341
  return
329
- if info["stale_on_disk"] is not False or info["restart_required"] is not False:
330
- _fail("五·键集", "刚起的进程 stale_on_disk/restart_required 应为 False,"
331
- "实得 %r/%r" % (info["stale_on_disk"],
332
- info["restart_required"]))
342
+ if not set(fast) <= set(full):
343
+ _fail("五·键集", "快照键必须 ⊂ full:多出 %s" % sorted(set(fast) - set(full)))
333
344
  return
334
- if "重启常驻 MCP 进程" not in (info["hint"] or ""):
335
- _fail("五·键集", "hint 未含处置指引:%r" % (info["hint"],))
345
+ lost = sorted(PINNED_INFO_KEYS - set(full))
346
+ if lost:
347
+ _fail("五·键集", "full 丢失既有 20 键:%s" % lost)
336
348
  return
337
- _ok("五·键集", "五新键齐备、既有 20 键(18 + #53-B 扩键 bucket_scope + "
338
- "PR #54 扩键 skipped_unreadable)一字未动;"
339
- "代 %s(code==disk,restart_required=False)"
340
- % info["code_generation"])
341
-
349
+ extra = sorted(set(full) - PINNED_INFO_KEYS - set(NEW_INFO_KEYS) - FAST_ONLY_KEYS)
350
+ if extra:
351
+ _fail("五·键集", "full 多出未登记键:%s" % extra)
352
+ return
353
+ if full["code_generation"] != full["disk_generation"]:
354
+ _fail("五·键集", "刚起的进程自报跨代:code=%s disk=%s"
355
+ % (full["code_generation"], full["disk_generation"]))
356
+ return
357
+ if full["stale_on_disk"] is not False or full["restart_required"] is not False:
358
+ _fail("五·键集", "stale_on_disk/restart_required 应为 False,实得 %r/%r"
359
+ % (full["stale_on_disk"], full["restart_required"]))
360
+ return
361
+ _ok("五·键集", "快照 %d 键 ⊂ full %d 键;full 含既有 20 键 + 五新键;快照不含 health_os 慢面"
362
+ % (len(fast), len(full)))
342
363
 
343
364
  def main():
344
365
  print("代校验守卫(generation guard)——临时目录内进行,不碰工作区与在役库")
@@ -36,6 +36,7 @@
36
36
  """
37
37
  from __future__ import annotations
38
38
 
39
+ import ast
39
40
  import inspect
40
41
  import json
41
42
  import os
@@ -209,11 +210,43 @@ def group_e():
209
210
  sites.append((rel, i))
210
211
  _check('E1 NFKC 归一实现全仓唯一(排除 test_*.py;不得长出第二份口径)',
211
212
  len(sites) == 1 and sites[0][0] == "md_cg/routing.py", sites)
212
- fold_src = inspect.getsource(fold_for_match)
213
- _check('E2 fold_for_match 走 routing.fold_variants 单点(不新造一份全角/大小写处理)',
214
- 'routing.fold_variants(' in fold_src, fold_src[:160])
213
+ # E2(#753/#755 复核:改 AST 判据)——文本切片会被 docstring/注释骗(假绿或假红):
214
+ # 走 AST 只认**真实调用**:fold_for_match 必须调 fold_for_match_keep,
215
+ # 且 fold_for_match_keep 必须调 routing.fold_variants。
216
+ def _fn_body(fn):
217
+ _tree = ast.parse(inspect.getsource(fn)).body[0]
218
+ _b = _tree.body
219
+ if _b and isinstance(_b[0], ast.Expr) and isinstance(_b[0].value, ast.Constant):
220
+ _b = _b[1:] # 剥 docstring(注释不进 AST,天然免疫)
221
+ return _b
222
+
223
+ def _has_call(body, attr, mod=None):
224
+ for _n in ast.walk(ast.Module(body=body, type_ignores=[])):
225
+ if (isinstance(_n, ast.Call) and isinstance(_n.func, ast.Attribute)
226
+ and _n.func.attr == attr):
227
+ if mod is None:
228
+ return True
229
+ if isinstance(_n.func.value, ast.Name) and _n.func.value.id == mod:
230
+ return True
231
+ return False
232
+
233
+ def _has_name_call(body, name):
234
+ for _n in ast.walk(ast.Module(body=body, type_ignores=[])):
235
+ if isinstance(_n, ast.Call) and isinstance(_n.func, ast.Name) and _n.func.id == name:
236
+ return True
237
+ return False
238
+
239
+ _keep_body = _fn_body(M.fold_for_match_keep)
240
+ _check('E2 fold_for_match 走 routing.fold_variants 单点(#753/#755 后归一收进 fold_for_match_keep)',
241
+ _has_name_call(_fn_body(fold_for_match), 'fold_for_match_keep')
242
+ and _has_call(_keep_body, 'fold_variants', 'routing'),
243
+ (ast.unparse(ast.Module(body=_keep_body, type_ignores=[]))[:160],))
215
244
  _check('E3 fold_variants 走 nfkc_text 单点',
216
245
  'nfkc_text(' in inspect.getsource(RT.fold_variants))
246
+ # E6(#753/#755 复核):归一**顺序**必须与 main 原式一致(fold_accents → fold_variants)。
247
+ # 实测 BMP 有 8 个码点因此分叉(希腊下加点区):「ᾀ」正序 → α、反序 → αι。
248
+ _check('E6 归一顺序 fold_accents → fold_variants(ᾀ → α;反序得 αι)',
249
+ fold_for_match("ᾀ") == "α", fold_for_match("ᾀ"))
217
250
  _check('E4 judge_qualification 的负条件腿走 neg_condition_hits 单点',
218
251
  'neg_condition_hits(' in inspect.getsource(MdCG.judge_qualification))
219
252
  n_def = sum(len(re.findall(r'def neg_condition_hits', src))
@@ -332,18 +365,18 @@ _MUTATIONS = (
332
365
  ' if all(not _negated_at(scene_norm, k)'),),
333
366
  ('K5',)),
334
367
  ('M5 归一退化:fold_for_match 退回只做去空白(#362b 修前形态)', 'md_cg/mdcg.py',
335
- ((' return compact_exact_text(routing.fold_variants(text))',
368
+ ((' return routing.fold_variants(fold_accents(text))',
336
369
  ' return compact_exact_text(text)'),),
337
- ('A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'C1', 'E2', 'K11')),
370
+ ('A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'C1', 'E2', 'E6', 'K11')),
338
371
  ('M6 判据退化:全词命中 all → any(单个通用词即整片否决)', 'md_cg/mdcg.py',
339
372
  ((' if words and all(w in scene_norm for w in words):',
340
373
  ' if words and any(w in scene_norm for w in words):'),),
341
374
  ('K8b',)),
342
375
  ('M7 单点分裂:fold_for_match 自写一份内联 NFKC(不走 routing 单点)', 'md_cg/mdcg.py',
343
- ((' return compact_exact_text(routing.fold_variants(text))',
376
+ ((' return routing.fold_variants(fold_accents(text))',
344
377
  ' return compact_exact_text(' + chr(10)
345
378
  + ' __import__(' + _Q + 'unicodedata' + _Q + ').normalize(' + _Q + 'NFKC' + _Q + ', str(text))).casefold()'),),
346
- ('C1', 'E1', 'E2')),
379
+ ('C1', 'E1', 'E2', 'E6')),
347
380
  ('M8 案②回退:摘掉 reason 里的「未摘词面原判 REJECT」证据(可观测性缺失)',
348
381
  'md_cg/mdcg.py',
349
382
  ((' why += (";**摘词面前原判 %s**(查询逐字含本节点的拒绝域条目,该 REJECT "'
@@ -355,6 +388,10 @@ _MUTATIONS = (
355
388
  ((' if j >= 1 and j + 1 < len(s) and s[j - 1] == s[j + 1]:',
356
389
  ' if False:'),),
357
390
  ('K12',)),
391
+ ('M10 顺序交换:fold_for_match_keep 反序(fold_accents ∘ fold_variants)', 'md_cg/mdcg.py',
392
+ ((' return routing.fold_variants(fold_accents(text))',
393
+ ' return fold_accents(routing.fold_variants(text))'),),
394
+ ('E6',)),
358
395
  )
359
396
 
360
397
 
@@ -39,7 +39,7 @@ hive 面**两趟**(契约 c4/c5/c6;id 契约 v2 之后 `_submit` 改调 Rust
39
39
  · 安全前提(真 exe 为什么安全,码上必须写明):``alloc-id`` 是**纯分配**通道
40
40
  (Rust ``job::alloc_job_id`` 只 ``fs::create_dir`` 落池,不读不启动 serve);
41
41
  且 ``HIVE_CONFIG`` 钉在不存在路径 ⇒ ``_ensure_serve`` 的拉起分支在
42
- ``serve_start.start()`` 的 ``load_config`` 即返回失败(serve_start.py:462-464),
42
+ ``serve_start.start()`` 的 ``load_config`` 即返回失败(serve_start.py:493),
43
43
  Popen 那一步不执行——成功趟据此断言 ``serve.started is False`` 自证(失败趟的
44
44
  响应是 context 闸的拒收,本就**没有** serve 字段,故该断言只落在成功趟)。
45
45
 
@@ -211,7 +211,7 @@ def _hive_env(tmp: str, exe: str | None = None) -> dict:
211
211
  生效条件:exe 为 None(失败趟)⇒ HIVE_EXE 钉在**不存在的探针路径**(`_alloc_job_id`
212
212
  在 isfile 即抛显式 SubmitError);exe 给定时(成功趟)⇒ HIVE_EXE 指向该**真实**
213
213
  二进制。两趟的 HIVE_CONFIG 一律指向不存在的路径:`_ensure_serve` 的拉起分支因此
214
- 在 `serve_start.start()` 的 `load_config` 即返回失败(serve_start.py:462-464),
214
+ 在 `serve_start.start()` 的 `load_config` 即返回失败(serve_start.py:493),
215
215
  Popen 那一步**不执行** ⇒ 真 exe 也绝不拉起在役 serve。HIVE_JOBS_DIR 一律指向本
216
216
  守卫自己的临时池。
217
217
  """
@@ -442,7 +442,7 @@ def main():
442
442
  # 落池 + 回显 job_id),**不读也不启动任何 serve**;
443
443
  # ② `HIVE_CONFIG` 仍钉在不存在路径 ⇒ `_ensure_serve` 的拉起分支即使被走到,
444
444
  # 也在 `serve_start.start()` 的 `load_config` 即返回 {"ok": False,…}
445
- # (serve_start.py:462-464),`Popen([EXE, "serve", …])` 不执行;
445
+ # (serve_start.py:493),`Popen([EXE, "serve", …])` 不执行;
446
446
  # ③ 池是 HIVE_JOBS_DIR 指针指向的**临时**目录 ⇒ 在役池零触碰(2g/2j 自证)。
447
447
  print("[2'] hive 成功路径实弹(真 exe + 守卫自己的临时池;alloc-id 不启动 serve)")
448
448
  binexe = _hive_bin()
@@ -137,12 +137,19 @@ def _src(rel):
137
137
 
138
138
 
139
139
  def _contra_src():
140
- tree = ast.parse(_src("md_cg/scrub.py"), "scrub.py")
140
+ """证据串生成面的源码段:`_contradiction` + 槽位支 `_slot_contradiction`(#747)。
141
+
142
+ #747 把极性支搬到 `_slot_contradiction` 后,只扫 `_contradiction` 会让 E1b 失去覆盖面;
143
+ 故扫描**证据串可能出现的全部函数**(新增分支也要加进来)。
144
+ """
145
+ src = _src("md_cg/scrub.py")
146
+ tree = ast.parse(src, "scrub.py")
147
+ segs = []
141
148
  for node in ast.walk(tree):
142
- if isinstance(node, ast.FunctionDef) and node.name == "_contradiction":
143
- import ast as _a
144
- return _a.get_source_segment(_src("md_cg/scrub.py"), node) or ""
145
- return ""
149
+ if isinstance(node, ast.FunctionDef) and node.name in (
150
+ "_contradiction", "_slot_contradiction"):
151
+ segs.append(ast.get_source_segment(src, node) or "")
152
+ return "\n".join(segs)
146
153
 
147
154
 
148
155
  def group_e(tmp):
@@ -185,9 +192,8 @@ _MUTATIONS = (
185
192
  ("A2", "A3", "A4", "A5", "A6")),
186
193
  ("M2 极性支改回共享词面",
187
194
  "md_cg/scrub.py",
188
- ((" \"why\": f\"极性相反且共享内容:共享片段 {len(shared)} 个\"" + chr(10)
189
- + " f\"(占比 {ratio:.2f}),词面不入证据串\"}",
190
- " \"why\": \"极性相反且共享内容:\" + \"、\".join(sorted(shared)[:5])}"),),
195
+ (('f"(占比 {ratio:.2f}),词面不入证据串")',
196
+ 'f"(占比 {ratio:.2f}),词面不入证据串" + "、".join(sorted(shared)[:5]))'),),
191
197
  ("E1b",)),
192
198
  )
193
199
 
@@ -0,0 +1,139 @@
1
+ # -*- coding: utf-8 -*-
2
+ """issue #677(C 根治):`_flush_batch` 的锁内 IO 从「每条一次 flush」降为「每批一次」。
3
+
4
+ ## 病灶
5
+ `_flush_batch` 在 `FileLock(index_path)` 内逐条 `ShardedLog.append`,而 `append` **每条都 flush**
6
+ ⇒ 锁内 IO 次数 = 脏集条数。大库一批上千条 = 上千次 flush,MCP stdio 串行时后续请求全排队,
7
+ 客户端 60s 超时(「写成功但没回话」)。
8
+
9
+ ## 修法
10
+ `ShardedLog.append_many`:**一次 write + 一次 flush**(批量编码、单次落盘);
11
+ `_flush_batch` 用它,整批仍在同一把 FileLock 内 ⇒ 与 compact 的 read→clear 互斥语义不变。
12
+
13
+ ## 断言
14
+ - B1 一批 300 条:`append` 调用 **0** 次、`append_many` 至少 1 次、总条数 300;
15
+ - B2 重开库后 300 条都在(日志重放正确);
16
+ - B3 单条 flush 仍工作(走 append_many,条数 1)。
17
+
18
+ 运行:python -X utf8 -m md_cg.test_issue677_flush_batch_io
19
+ """
20
+ from __future__ import annotations
21
+
22
+ import os
23
+ import shutil
24
+ import sys
25
+ import tempfile
26
+
27
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
28
+
29
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
30
+ sys.path.insert(0, _REPO)
31
+
32
+ from md_cg import fsutil # noqa: E402
33
+ from md_cg.mdcg import MdCG # noqa: E402
34
+
35
+ FAILS = []
36
+
37
+
38
+ def ok(cond, name, detail=""):
39
+ if cond:
40
+ print(" PASS " + name)
41
+ else:
42
+ print(" FAIL " + name + " " + str(detail)[:300])
43
+ FAILS.append(name)
44
+
45
+
46
+ BODY = "# 功能名:t%d\n# 生效条件:x\n# 子功能:y\n# 执行:z\n# 验证方式:test\n# 不适用条件:无\n\nbody %d"
47
+
48
+
49
+ def main():
50
+ print("=" * 68)
51
+ print("issue #677 C · flush 锁内批量 IO")
52
+ print("=" * 68)
53
+ counts = {"append": 0, "many": 0, "many_n": 0}
54
+ orig_append = fsutil.ShardedLog.append
55
+ orig_many = fsutil.ShardedLog.append_many
56
+
57
+ def _a1(self, record):
58
+ counts["append"] += 1
59
+ return orig_append(self, record)
60
+
61
+ def _a2(self, records):
62
+ n = orig_many(self, records)
63
+ counts["many"] += 1
64
+ counts["many_n"] += n
65
+ return n
66
+
67
+ fsutil.ShardedLog.append = _a1
68
+ fsutil.ShardedLog.append_many = _a2
69
+ tmp = tempfile.mkdtemp(prefix="iss677c_")
70
+ try:
71
+ cg = MdCG(os.path.join(tmp, "r"), autoflush=10 ** 9)
72
+ for i in range(300):
73
+ cg.add("n%03d" % i, BODY % (i, i), layer="contextual")
74
+ cg.flush()
75
+ ok(counts["append"] == 0, "B1a 锁内不再逐条 append", counts)
76
+ ok(counts["many"] >= 1 and counts["many_n"] == 300, "B1b 批量一次写盘、条数 300", counts)
77
+ cg.close()
78
+ cg2 = MdCG(os.path.join(tmp, "r"))
79
+ ok(len(cg2.index.get("nodes") or {}) == 300, "B2 重开后 300 条都在(重放正确)",
80
+ len(cg2.index.get("nodes") or {}))
81
+ cg2.close()
82
+ counts.update({"append": 0, "many": 0, "many_n": 0})
83
+ cg3 = MdCG(os.path.join(tmp, "r2"), autoflush=10 ** 9)
84
+ cg3.add("one", BODY % (0, 0), layer="contextual")
85
+ cg3.flush()
86
+ ok(counts["many_n"] == 1, "B3 单条 flush 仍工作", counts)
87
+ cg3.close()
88
+ # B4(#677 C 复核 P1):残尾修补——先造「无换行结尾」的半行,再 append_many;
89
+ # 不补换行时新记录会与残尾粘成一行(解析失败、残尾与新记录一起丢)。
90
+ _d = os.path.join(tmp, "shard_torn")
91
+ os.makedirs(_d, exist_ok=True)
92
+ _log = fsutil.ShardedLog(_d)
93
+ with open(_log.path, "a", encoding="utf-8", newline="\n") as _f:
94
+ _f.write('{"id":"torn"')
95
+ _log.append_many([{"id": "good", "e": {"layer": "contextual"}}])
96
+ _log.close()
97
+ _recs = fsutil.read_jsonl(_log.path)
98
+ ok([r.get("id") for r in _recs] == ["good"],
99
+ "B4 残尾修补:新记录不被粘行吞掉", [r.get("id") for r in _recs])
100
+ # B5(#677 C 复核 P2):失败回填必须保留 superseded 标记——
101
+ # 否则下一轮把「已判陈旧」的记录当新记录写进分片(旧盖新 = 内容错误)。
102
+ cg4 = MdCG(os.path.join(tmp, "r3"), autoflush=10 ** 9)
103
+ cg4.add("s0", BODY % (0, 0), layer="contextual")
104
+ cg4.add("s1", BODY % (1, 1), layer="contextual")
105
+ cg4._dirty.superseded_ids.add("s0")
106
+ _boom = {"on": True}
107
+
108
+ def _raise(self, records):
109
+ if _boom["on"]:
110
+ _boom["on"] = False
111
+ raise OSError("disk full (simulated)")
112
+ return orig_many(self, records)
113
+
114
+ fsutil.ShardedLog.append_many = _raise
115
+ try:
116
+ cg4.flush()
117
+ except OSError:
118
+ pass
119
+ fsutil.ShardedLog.append_many = _a2
120
+ cg4.flush()
121
+ _ids = []
122
+ for _root, _dn, _fns in os.walk(cg4.index_log_dir):
123
+ for _fn in _fns:
124
+ if _fn.endswith(".log"):
125
+ _ids += [r.get("id") for r in fsutil.read_jsonl(os.path.join(_root, _fn))]
126
+ ok("s0" not in _ids and "s1" in _ids,
127
+ "B5 失败回填保留 superseded(陈旧记录不落盘)", _ids)
128
+ cg4.close()
129
+ finally:
130
+ fsutil.ShardedLog.append = orig_append
131
+ fsutil.ShardedLog.append_many = orig_many
132
+ shutil.rmtree(tmp, ignore_errors=True)
133
+ print()
134
+ print("issue #677 C:%d 项失败" % len(FAILS) if FAILS else "issue #677 C:全部通过")
135
+ return 1 if FAILS else 0
136
+
137
+
138
+ if __name__ == "__main__":
139
+ sys.exit(main())
@@ -0,0 +1,82 @@
1
+ # -*- coding: utf-8 -*-
2
+ """issue #677(B):`cg(op=info)` 拆快/慢——诊断面不得因大库拖到 60s。
3
+
4
+ 病灶:info 原先一次性跑 health_os + 理论/链接/策略/代校验;MCP stdio 串行 ⇒ 慢 info 挡住后续
5
+ 请求(含写入应答),客户端 60s 超时。修法:缺省只回内存态快照;全量诊断走 full=true。
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import json
10
+ import os
11
+ import shutil
12
+ import sys
13
+ import tempfile
14
+
15
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
16
+
17
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
18
+ sys.path.insert(0, _REPO)
19
+
20
+ from md_cg import mcp_server # noqa: E402
21
+ from md_cg.mdcos import MdCGOS # noqa: E402
22
+
23
+ FAILS = []
24
+
25
+
26
+ def ok(cond, name, detail=""):
27
+ if cond:
28
+ print(" PASS " + name)
29
+ else:
30
+ print(" FAIL " + name + " " + str(detail)[:300])
31
+ FAILS.append(name)
32
+
33
+
34
+ def _call(cg, **a):
35
+ r = mcp_server.call_tool(cg, "cg", a)
36
+ if isinstance(r, dict) and "content" in r:
37
+ return json.loads(r["content"][0]["text"])
38
+ return r
39
+
40
+
41
+ BODY = "# 功能名:t\n# 生效条件:x\n# 子功能:y\n# 执行:z\n# 验证方式:test\n# 不适用条件:无\n\nbody"
42
+
43
+
44
+ def main():
45
+ print("=" * 68)
46
+ print("issue #677 B · op=info 快/慢拆分")
47
+ print("=" * 68)
48
+ tmp = tempfile.mkdtemp(prefix="iss677b_")
49
+ cg = MdCGOS(os.path.join(tmp, "r"), autoflush=1)
50
+ try:
51
+ cg.add("n1", BODY, layer="knowledge")
52
+ cg.flush()
53
+ _orig = cg.health_os
54
+
55
+ def _boom(*_a, **_k):
56
+ raise AssertionError("快照模式不得调用 health_os")
57
+
58
+ cg.health_os = _boom
59
+ fast = _call(cg, op="info")
60
+ ok(isinstance(fast, dict) and fast.get("full") is False, "I1 缺省 full=false", fast)
61
+ ok("flush_state" in fast and "node_count" in fast, "I1b 含内存态字段", list(fast)[:8])
62
+ ok("os" not in fast, "I3 缺省不含慢面(health_os 的 os 段)", list(fast)[:12])
63
+ ok("theory" in fast and "write_policy" in fast,
64
+ "I3b 既有诊断键仍在(兼容)", [k for k in fast if k in ("theory", "write_policy")])
65
+ cg.health_os = lambda *a, **k: {"health_marker": 1}
66
+ full = _call(cg, op="info", full=True)
67
+ ok(isinstance(full, dict) and full.get("full") is True, "I2 full=true", full)
68
+ ok(full.get("health_marker") == 1, "I2b 慢面被调用并并入", full.get("health_marker"))
69
+ cg.health_os = _orig
70
+ finally:
71
+ try:
72
+ cg.close()
73
+ except Exception:
74
+ pass
75
+ shutil.rmtree(tmp, ignore_errors=True)
76
+ print()
77
+ print("issue #677 B:%d 项失败" % len(FAILS) if FAILS else "issue #677 B:全部通过")
78
+ return 1 if FAILS else 0
79
+
80
+
81
+ if __name__ == "__main__":
82
+ sys.exit(main())
@@ -0,0 +1,106 @@
1
+ # -*- coding: utf-8 -*-
2
+ """issue #677(A):自动记忆面幂等——`mdcg_remember(dedupe=true)`。
3
+
4
+ ## 病灶
5
+ 写入是两级提交(节点文件 + 内存索引先落,应答在后),客户端 60s 超时衡量的是「收到应答」。
6
+ 超时后重试若不自幂等,就会新增一条同内容节点(实测三条,importance 0.86)。
7
+
8
+ ## 修法
9
+ `mdcg_remember` 走与 `cg(op=write)` **同一个闸**(`writepipe._gate_dedupe`:同层 + 内容指纹 ⇒ 复用既有 id),
10
+ 位置在链首(未落盘、未建边、不过闸审计)。缺省关 —— 既有调用方逐位不变。
11
+
12
+ ## 断言
13
+ - R1 首次写入:落盘、deduped 不存在;
14
+ - R2 同内容再写 + dedupe=true:**不新增**、复用同一 id、deduped=True;
15
+ - R3 同内容**不带** dedupe:新增一条(opt-in 语义,既有行为不变);
16
+ - R4 跨层同内容 + dedupe:不短路(跨层是不同语义)。
17
+
18
+ 运行:python -X utf8 -m md_cg.test_issue677_remember_dedupe
19
+ """
20
+ from __future__ import annotations
21
+
22
+ import os
23
+ import shutil
24
+ import sys
25
+ import tempfile
26
+
27
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
28
+
29
+ _REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
30
+ sys.path.insert(0, _REPO)
31
+
32
+ from md_cg import mcp_server # noqa: E402
33
+ from md_cg.mdcos import MdCGOS # noqa: E402
34
+
35
+ FAILS = []
36
+
37
+
38
+ def ok(cond, name, detail=""):
39
+ if cond:
40
+ print(" PASS " + name)
41
+ else:
42
+ print(" FAIL " + name + " " + str(detail)[:300])
43
+ FAILS.append(name)
44
+
45
+
46
+ BODY = """# 功能名:幂等写入口
47
+ # 生效条件:写入超时后重试时
48
+ # 子功能:同层内容指纹短路
49
+ # 执行:mdcg_remember(dedupe=true)
50
+ # 验证方式:节点数不增、id 相同
51
+ # 不适用条件:需要另存一份时"""
52
+
53
+
54
+ def _nodes(cg):
55
+ return len((cg.index.get("nodes") or {}))
56
+
57
+
58
+ def main():
59
+ print("=" * 68)
60
+ print("issue #677 A · 自动记忆面幂等(mdcg_remember dedupe)")
61
+ print("=" * 68)
62
+ tmp = tempfile.mkdtemp(prefix="iss677a_")
63
+ cg = MdCGOS(os.path.join(tmp, "r"), autoflush=1)
64
+ try:
65
+ r1 = mcp_server.call_tool(cg, "mdcg_remember", {"content": BODY, "layer": "contextual"})
66
+ d1 = r1 if isinstance(r1, dict) else None
67
+ ok(d1 is not None and d1.get("ok") is True, "R1 首次写入成功", r1)
68
+ ok(not (d1 or {}).get("deduped"), "R1b 首次不带 deduped 标记", d1)
69
+ n1 = _nodes(cg)
70
+ r2 = mcp_server.call_tool(cg, "mdcg_remember", {"content": BODY, "layer": "contextual", "dedupe": True})
71
+ d2 = r2 if isinstance(r2, dict) else None
72
+ ok(d2 is not None and d2.get("deduped") is True, "R2 同内容 + dedupe ⇒ 短路复用", r2)
73
+ ok(_nodes(cg) == n1, "R2b 节点数不增", (_nodes(cg), n1))
74
+ ok((d2 or {}).get("id") == (d1 or {}).get("id"), "R2c 复用同一 id",
75
+ ((d2 or {}).get("id"), (d1 or {}).get("id")))
76
+ r3 = mcp_server.call_tool(cg, "mdcg_remember", {"content": BODY, "layer": "contextual"})
77
+ ok(_nodes(cg) == n1 + 1, "R3 不带 dedupe ⇒ 新增一条(opt-in)", (_nodes(cg), n1))
78
+ r4 = mcp_server.call_tool(cg, "mdcg_remember", {"content": BODY, "layer": "knowledge", "dedupe": True})
79
+ d4 = r4 if isinstance(r4, dict) else None
80
+ ok(not (d4 or {}).get("deduped"), "R4 跨层同内容不短路", r4)
81
+ # R5(#677 @1):跨会话同内容 + dedupe ⇒ **不**短路(各写一条)。
82
+ # 否则两个会话写同一句会互相顶掉;叠加 sessionPrivate 时会「命中但读不到」。
83
+ _body = BODY + chr(10) + chr(10) + "(跨会话用例)"
84
+ mcp_server.call_tool(cg, "mdcg_remember",
85
+ {"content": _body, "layer": "contextual", "session": "sA"})
86
+ n_mid = _nodes(cg)
87
+ r5 = mcp_server.call_tool(cg, "mdcg_remember",
88
+ {"content": _body, "layer": "contextual",
89
+ "session": "sB", "dedupe": True})
90
+ d5 = r5 if isinstance(r5, dict) else None
91
+ ok(not (d5 or {}).get("deduped") and _nodes(cg) == n_mid + 1,
92
+ "R5 跨会话同内容不幂等(session 不同各写一条)",
93
+ (d5, _nodes(cg), n_mid))
94
+ finally:
95
+ try:
96
+ cg.close()
97
+ except Exception:
98
+ pass
99
+ shutil.rmtree(tmp, ignore_errors=True)
100
+ print()
101
+ print("issue #677 A:%d 项失败" % len(FAILS) if FAILS else "issue #677 A:全部通过")
102
+ return 1 if FAILS else 0
103
+
104
+
105
+ if __name__ == "__main__":
106
+ sys.exit(main())