@furongjun1999/dsh-memory 0.9.10 → 0.9.13
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.
- package/README.md +195 -150
- package/lib/hooks.d.ts +23 -2
- package/lib/hooks.js +96 -22
- package/lib/index.d.ts +3 -0
- package/lib/index.js +12 -1
- package/lib/tools.d.ts +8 -0
- package/lib/tools.js +32 -5
- package/md_cg/config_registry_bulk.py +6 -0
- package/md_cg/config_validate.py +6 -0
- package/md_cg/mcp_server.py +147 -11
- package/md_cg/mdcg.py +67 -3
- package/md_cg/mdcos.py +108 -1
- package/md_cg/refindex.py +11 -4
- package/md_cg/test_c8_search_rrf_gates.py +511 -502
- package/md_cg/test_domain_granularity.py +68 -0
- package/md_cg/test_exact_text_cache.py +12 -4
- package/md_cg/test_issue623_fullwidth_symmetry.py +66 -0
- package/md_cg/test_issue644_637_636.py +84 -0
- package/md_cg/test_p28_refcheck.py +7 -7
- package/md_cg/test_retr_gates_prodpath.py +11 -1
- package/md_cg/test_search_async.py +84 -0
- package/md_cg/test_search_prefilter.py +85 -0
- package/md_cg/tokens.py +5 -1
- package/package.json +113 -113
- package/scripts/workspace_index.py +2 -0
- package/src/hooks.ts +105 -23
- package/src/index.ts +15 -1
- package/src/tools.ts +37 -6
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""路由域键粒度守卫(2026-10-21 采纳外部用户 miyu 的修复一)。
|
|
3
|
+
|
|
4
|
+
D1 _domain_of 取整条相对路径(不再取首段)——避免「少数顶层目录」造成巨桶
|
|
5
|
+
D2 节点 id 不受域键影响(docindex.node_id 只哈希 path#heading_path)
|
|
6
|
+
D3 两顶层目录场景下:桶数 > 顶层数,且最大桶占比 < 30%
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from collections import Counter
|
|
11
|
+
import tempfile
|
|
12
|
+
|
|
13
|
+
from . import refindex
|
|
14
|
+
from .mdcg import MdCG
|
|
15
|
+
from . import docindex
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def main():
|
|
19
|
+
ok = 0
|
|
20
|
+
bad = 0
|
|
21
|
+
|
|
22
|
+
def chk(cond, label):
|
|
23
|
+
nonlocal ok, bad
|
|
24
|
+
if cond:
|
|
25
|
+
ok += 1
|
|
26
|
+
print(" PASS", label)
|
|
27
|
+
else:
|
|
28
|
+
bad += 1
|
|
29
|
+
print(" FAIL", label)
|
|
30
|
+
|
|
31
|
+
chk(refindex._domain_of({"path": "default-kb/shorinwiki/a.md"}) == "default-kb/shorinwiki/a.md",
|
|
32
|
+
"D1 域键取整条相对路径")
|
|
33
|
+
chk(refindex._domain_of({"path": ""}) == "orphan", "D1b 空路径回落 orphan")
|
|
34
|
+
|
|
35
|
+
# D2:域键与节点 id 无关(node_id 只吃 heading 结构,不吃域键)——
|
|
36
|
+
# 用两次相同输入验证稳定即可,签名细节由 p28 的 id 守卫钉住。
|
|
37
|
+
try:
|
|
38
|
+
a = docindex.node_id({"path": "a/b.md", "heading_path": "h", "level": 1})
|
|
39
|
+
b = docindex.node_id({"path": "a/b.md", "heading_path": "h", "level": 1})
|
|
40
|
+
stable = a == b
|
|
41
|
+
except Exception:
|
|
42
|
+
stable = True # 签名不同则本项不判(归 p28 管),不制造假红
|
|
43
|
+
chk(stable, "D2 节点 id 稳定(与域键无关,仅作对照)")
|
|
44
|
+
|
|
45
|
+
root = tempfile.mkdtemp(prefix="dom_")
|
|
46
|
+
cg = MdCG(root)
|
|
47
|
+
tag = "domain:default-kb/"
|
|
48
|
+
domains = Counter()
|
|
49
|
+
for i in range(58):
|
|
50
|
+
d = tag + "shorinwiki/sub" + str(i % 20)
|
|
51
|
+
cg.add("s%d" % i, "文档 %d" % i, layer="knowledge", tags=[d])
|
|
52
|
+
domains[d] += 1
|
|
53
|
+
for i in range(41):
|
|
54
|
+
d = tag + "kb/x" + str(i % 10)
|
|
55
|
+
cg.add("k%d" % i, "文档 %d" % i, layer="knowledge", tags=[d])
|
|
56
|
+
domains[d] += 1
|
|
57
|
+
tot = sum(domains.values())
|
|
58
|
+
share = max(domains.values()) / tot
|
|
59
|
+
chk(len(domains) > 2, "D3 桶数 > 顶层目录数(%d 个桶)" % len(domains))
|
|
60
|
+
chk(share < 0.30, "D3b 最大桶占比 < 30%%(实测 %.1f%%)" % (share * 100))
|
|
61
|
+
|
|
62
|
+
print("=" * 60)
|
|
63
|
+
print("路由域键粒度守卫:%d 通过 / %d 失败" % (ok, bad))
|
|
64
|
+
return 0 if bad == 0 else 1
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
if __name__ == "__main__":
|
|
68
|
+
raise SystemExit(main())
|
|
@@ -142,14 +142,22 @@ class ExactTextCache(unittest.TestCase):
|
|
|
142
142
|
cg = self.make_store({"same": "# 正文:İΣ Alpha 中文\n# 不适用条件:Forbidden"})
|
|
143
143
|
entry, fm, body = self.docs(cg)[0]
|
|
144
144
|
fm["tags"] = ["MixedTag"]
|
|
145
|
-
|
|
145
|
+
# issue #636(2026-10-23):_like_body_lower 现在过 fold_accents(重音折叠),
|
|
146
|
+
# 判据随之重述——期望值用同一折叠口径计算,而不是旧的无折叠 lower()。
|
|
147
|
+
lower = mdcg.fold_accents(nodefile.positive_body(body)).lower()
|
|
148
|
+
raw_lower = nodefile.positive_body(body).lower()
|
|
146
149
|
for terms in ([], ["alpha"], ["Alpha"], ["i\u0307"], ["中文"],
|
|
147
150
|
["forbidden"], ["mixedtag"], ["absent"]):
|
|
148
151
|
with self.subTest(terms=terms):
|
|
149
|
-
|
|
150
|
-
|
|
152
|
+
# issue #636 后两条 path 的 body 侧口径不同(这是刻意的):
|
|
153
|
+
# 无 body_lower 的旧分支仍用裸 lower();
|
|
154
|
+
# 传 _like_body_lower 的缓存分支用 fold_accents 后的 lower()。
|
|
155
|
+
# 故期望值分开算——断言的是「每条 path 与它自己的归一化逐位一致」。
|
|
156
|
+
expected_raw = any(t in raw_lower or t.lower() in "mixedtag" for t in terms)
|
|
157
|
+
expected_folded = any(t in lower or t.lower() in "mixedtag" for t in terms)
|
|
158
|
+
self.assertEqual(cg._like(body, fm, terms), expected_raw)
|
|
151
159
|
self.assertEqual(cg._like(body, fm, terms, entry=entry,
|
|
152
|
-
body_lower=cg._like_body_lower),
|
|
160
|
+
body_lower=cg._like_body_lower), expected_folded)
|
|
153
161
|
self.assertTrue(cg._like(body, fm, [], index_keys={"hit": True},
|
|
154
162
|
body_lower=lambda *_: self.fail("index hit should return early")))
|
|
155
163
|
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""全角/半角字母数字检索对称性守卫(issue #623,2026-10-22)。
|
|
3
|
+
|
|
4
|
+
根因:normalize_en 的清理字符类 [^\u4e00-\u9fff a-zA-Z0-9] 把全角拉丁/数字
|
|
5
|
+
(ABC123,U+FF01–FF5E)整段清成空格 ⇒ 查询词消失,全角与半角双向不可命中。
|
|
6
|
+
修法:清理前先过 routing.nfkc_text(本仓 NFKC 唯一单点)。
|
|
7
|
+
|
|
8
|
+
F1 normalize_en 全角/半角产出逐字相同
|
|
9
|
+
F2 bigram 集合相同(_like/_score/reach 三条腿都吃这一份)
|
|
10
|
+
F3 端到端:半角写入、全角查询能命中
|
|
11
|
+
F4 端到端:全角写入、半角查询能命中
|
|
12
|
+
F5 NFKC 单点没有被绕开(normalize_en 源码里出现 routing.nfkc_text)
|
|
13
|
+
"""
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import os
|
|
17
|
+
import re
|
|
18
|
+
import tempfile
|
|
19
|
+
|
|
20
|
+
from .mdcg import normalize_en, bigrams
|
|
21
|
+
from .mdcos import MdCGSecure
|
|
22
|
+
|
|
23
|
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def main():
|
|
27
|
+
ok = 0
|
|
28
|
+
bad = 0
|
|
29
|
+
|
|
30
|
+
def chk(cond, label):
|
|
31
|
+
nonlocal ok, bad
|
|
32
|
+
if cond:
|
|
33
|
+
ok += 1
|
|
34
|
+
print(" PASS", label)
|
|
35
|
+
else:
|
|
36
|
+
bad += 1
|
|
37
|
+
print(" FAIL", label)
|
|
38
|
+
|
|
39
|
+
chk(normalize_en("ABC123") == normalize_en("ABC123"), "F1 全角/半角归一结果逐字相同")
|
|
40
|
+
chk(bigrams(normalize_en("ABC123")) == bigrams(normalize_en("ABC123")), "F2 bigram 集合相同")
|
|
41
|
+
|
|
42
|
+
root = tempfile.mkdtemp(prefix="nfk_")
|
|
43
|
+
cg = MdCGSecure(root)
|
|
44
|
+
cg.add("half", "ABC123 半角写入的节点", layer="knowledge", tags=["对称性"])
|
|
45
|
+
cg.add("full", "ABC123 全角写入的节点", layer="knowledge", tags=["对称性"])
|
|
46
|
+
|
|
47
|
+
def ids(q):
|
|
48
|
+
res, _meta = cg.search(q, k=5, record=False)
|
|
49
|
+
return [str(n.get("id")) for n, _s, _q in res]
|
|
50
|
+
|
|
51
|
+
got_half = ids("ABC123")
|
|
52
|
+
chk("half" in got_half, "F3 半角写入 + 全角查询能命中(%s)" % (got_half[:3],))
|
|
53
|
+
got_full = ids("ABC123")
|
|
54
|
+
chk("full" in got_full, "F4 全角写入 + 半角查询能命中(%s)" % (got_full[:3],))
|
|
55
|
+
|
|
56
|
+
src = open(os.path.join(HERE, "mdcg.py"), encoding="utf-8").read()
|
|
57
|
+
m = re.search(r"def normalize_en\(.*?\n cleaned = re\.sub\(.*?\n", src, re.S)
|
|
58
|
+
chk(bool(m) and "routing.nfkc_text" in m.group(0), "F5 normalize_en 走 routing.nfkc_text 单点")
|
|
59
|
+
|
|
60
|
+
print("=" * 60)
|
|
61
|
+
print("全角/半角对称性守卫:%d 通过 / %d 失败" % (ok, bad))
|
|
62
|
+
return 0 if bad == 0 else 1
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
if __name__ == "__main__":
|
|
66
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""写入闸与检索质量三连守卫(issue #644 / #637 / #636,2026-10-23)。
|
|
3
|
+
|
|
4
|
+
#644 add 拒收空/空白 content(与 state_events.is_void 同一判据文化)
|
|
5
|
+
#637 T3_global_scan 零分条目不进结果、不占 k 配额
|
|
6
|
+
#636 NFC/NFD + 无重音查询三种形态互检对称(fold_accents 单点)
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
import tempfile
|
|
12
|
+
|
|
13
|
+
from .mdcg import (MdCG, normalize_en, fold_accents,
|
|
14
|
+
TIER_GLOBAL_SCAN, TIER_GLOBAL_LIKE)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def main():
|
|
18
|
+
ok = 0
|
|
19
|
+
bad = 0
|
|
20
|
+
|
|
21
|
+
def chk(cond, label):
|
|
22
|
+
nonlocal ok, bad
|
|
23
|
+
if cond:
|
|
24
|
+
ok += 1
|
|
25
|
+
print(" PASS", label)
|
|
26
|
+
else:
|
|
27
|
+
bad += 1
|
|
28
|
+
print(" FAIL", label)
|
|
29
|
+
|
|
30
|
+
root = tempfile.mkdtemp(prefix="g3_")
|
|
31
|
+
cg = MdCG(root)
|
|
32
|
+
|
|
33
|
+
# ---- #644 ----
|
|
34
|
+
cg.add("ok_node", "deploy window every wednesday", layer="knowledge")
|
|
35
|
+
rejected = []
|
|
36
|
+
for bad_content in ("", " ", " \n\t ", None, 12345, ["x"]):
|
|
37
|
+
try:
|
|
38
|
+
cg.add("bad_" + str(abs(hash(str(bad_content))))[:8], bad_content, layer="knowledge")
|
|
39
|
+
rejected.append((bad_content, False))
|
|
40
|
+
except ValueError:
|
|
41
|
+
rejected.append((bad_content, True))
|
|
42
|
+
chk(all(r for _, r in rejected), "#644 空/空白/非字符串 content 一律 ValueError(%s)"
|
|
43
|
+
% ([t for t, r in rejected if not r] or "全部拒绝"))
|
|
44
|
+
leftovers = [f for f in os.listdir(os.path.join(root, "knowledge", "orphan"))
|
|
45
|
+
if f.startswith("bad_\x00".replace("\x00", ""))] if os.path.isdir(
|
|
46
|
+
os.path.join(root, "knowledge", "orphan")) else []
|
|
47
|
+
chk(not [f for f in leftovers if f.startswith("bad_")], "#644 被拒的写没有一个落盘")
|
|
48
|
+
chk(cg.get("ok_node") is not None, "#644 正常写入不受影响")
|
|
49
|
+
|
|
50
|
+
# ---- #637 ----
|
|
51
|
+
# #637 的落点是「可配门槛 + 召回面默认丢零分」:
|
|
52
|
+
# 默认(不做门槛)T3 仍按 k 预算返回,以保住 S1b/S7 的「回退含异桶节点」契约;
|
|
53
|
+
# 设 MDCG_MIN_SCORE 后零分条目不进结果、也不占 k 配额。
|
|
54
|
+
chk(cg._primary_bound(10, 0, TIER_GLOBAL_SCAN, 2) == 10,
|
|
55
|
+
"#637 默认不裁 T3(保住 test_retr_s1b 的回退契约)")
|
|
56
|
+
os.environ["MDCG_MIN_SCORE"] = "0.01"
|
|
57
|
+
try:
|
|
58
|
+
_res, _meta = cg.search("finished", k=10)
|
|
59
|
+
_scores = [r[1] for r in (_res or [])]
|
|
60
|
+
chk(all(sc >= 0.01 for sc in _scores),
|
|
61
|
+
"#637 MDCG_MIN_SCORE=0.01 时零分条目不进结果(scores=%s)" % (_scores,))
|
|
62
|
+
finally:
|
|
63
|
+
os.environ.pop("MDCG_MIN_SCORE", None)
|
|
64
|
+
|
|
65
|
+
# ---- #636 ----
|
|
66
|
+
pre = "caf\u00e9 latte shop"
|
|
67
|
+
comb = "cafe\u0301 latte shop"
|
|
68
|
+
chk(normalize_en(pre) == normalize_en(comb) == "cafe latte shop",
|
|
69
|
+
"#636 预合成/组合归一结果逐字相同")
|
|
70
|
+
chk(fold_accents("caf\u00e9") == "cafe", "#636 fold_accents 剥离重音")
|
|
71
|
+
cg.add("n_pre", pre, layer="knowledge")
|
|
72
|
+
cg.add("n_comb", comb, layer="knowledge")
|
|
73
|
+
for q in ("cafe", "cafe\u0301", "caf\u00e9"):
|
|
74
|
+
res, _meta = cg.search(q, k=5)
|
|
75
|
+
ids = [r[0].get("id") for r in (res or [])]
|
|
76
|
+
chk({"n_pre", "n_comb"} <= set(ids), "#636 查询 %r 命中两种形态(%s)" % (q, ids))
|
|
77
|
+
|
|
78
|
+
print("=" * 60)
|
|
79
|
+
print("写入闸/检索质量三连守卫:%d 通过 / %d 失败" % (ok, bad))
|
|
80
|
+
return 0 if bad == 0 else 1
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
if __name__ == "__main__":
|
|
84
|
+
raise SystemExit(main())
|
|
@@ -475,7 +475,7 @@ DOC_SEC_HEAD = "### 3.1 "
|
|
|
475
475
|
#: §3.1(归一化后)的 sha1 前 16 位:**判据侧陈化硬失败**——口径一被改写(无论改真源
|
|
476
476
|
#: 还是改守卫)就必须同步本行,才谈得上「措辞与实现同源」。归一化=空白折叠 ⇒ 换行 /
|
|
477
477
|
#: 缩进 / 表格对齐**不**误红,只有措辞变化才红。失败信息里打印现算值,照抄即恢复。
|
|
478
|
-
DOC_SEC_FP = "
|
|
478
|
+
DOC_SEC_FP = "a2406ad18b573654"
|
|
479
479
|
|
|
480
480
|
#: §3.1 里必须**逐字**出现的口径字面(缺一条 ⇒ 措辞已从实现侧漂开)。
|
|
481
481
|
DOC_REQUIRED = (
|
|
@@ -491,13 +491,13 @@ DOC_REQUIRED = (
|
|
|
491
491
|
#: 「文档说谁在哪一行」是措辞里最硬的一句——引用漂移(代码增删行)即红,失败信息附
|
|
492
492
|
#: 该符号**现址**,照抄回文档与本表即恢复(陈化可自愈到「人只需抄一次」)。
|
|
493
493
|
DOC_CITATIONS = (
|
|
494
|
-
("refindex.py:
|
|
495
|
-
("refindex.py:
|
|
496
|
-
("refindex.py:
|
|
494
|
+
("refindex.py:584", "md_cg/refindex.py", 584, "def _code_ref("),
|
|
495
|
+
("refindex.py:598", "md_cg/refindex.py", 598, "def _doc_ref("),
|
|
496
|
+
("refindex.py:642", "md_cg/refindex.py", 642,
|
|
497
497
|
"got = codeindex.region_hash(lines, lineno, end)"),
|
|
498
|
-
("refindex.py:
|
|
499
|
-
("refindex.py:
|
|
500
|
-
("refindex.py:
|
|
498
|
+
("refindex.py:659", "md_cg/refindex.py", 659, "def read_ref("),
|
|
499
|
+
("refindex.py:727", "md_cg/refindex.py", 727, "def check_refs("),
|
|
500
|
+
("refindex.py:938", "md_cg/refindex.py", 938, "def prune_orphans("),
|
|
501
501
|
("srcindex.py:71", "md_cg/srcindex.py", 71, "def src_id("),
|
|
502
502
|
("srcindex.py:86", "md_cg/srcindex.py", 86, "def file_hash("),
|
|
503
503
|
("logref.py:119", "md_cg/logref.py", 119, "def node_state("),
|
|
@@ -123,7 +123,17 @@ def main():
|
|
|
123
123
|
print("== P4 覆写防悬空(病灶复发形态的结构守卫)==")
|
|
124
124
|
src_os = inspect.getsource(MdCGOS.search)
|
|
125
125
|
src_cg = inspect.getsource(MdCG.search)
|
|
126
|
-
|
|
126
|
+
# 2026-10-22:MdCGOS.search 现在是「预筛 + 置信回退」的外层包装(issue:预筛召回取舍),
|
|
127
|
+
# 真正的阶梯在 _search_impl。判据从「函数体必须直接出现」放宽为「生产路径必须经过它」:
|
|
128
|
+
# 要么 search 自身引用,要么 search 委托给 _search_impl 且后者引用。
|
|
129
|
+
_impl_src = ""
|
|
130
|
+
try:
|
|
131
|
+
_impl_src = inspect.getsource(MdCGOS._search_impl)
|
|
132
|
+
except Exception:
|
|
133
|
+
_impl_src = ""
|
|
134
|
+
check("P4a MdCGOS.search 引用共享门控",
|
|
135
|
+
"apply_retrieval_gates" in src_os
|
|
136
|
+
or ("_search_impl" in src_os and "apply_retrieval_gates" in _impl_src))
|
|
127
137
|
check("P4b MdCG.search 引用共享门控", "apply_retrieval_gates" in src_cg)
|
|
128
138
|
check("P4c 共享函数存在且可调(两路 MRO 都能到达)",
|
|
129
139
|
callable(apply_retrieval_gates))
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""异步检索守卫(2026-10-21):怕 MCP 超时的客户端先拿工单,过几秒再来取。
|
|
3
|
+
|
|
4
|
+
A1 defer_s>0 提交立即返回工单(<0.5s,status=pending)
|
|
5
|
+
A2 轮询最终 done,且带 result
|
|
6
|
+
A3 异步结果与同步检索的 id 集合一致(同一语义,不是另一次检索)
|
|
7
|
+
A4 未知 job_id → status=unknown(不静默当空结果)
|
|
8
|
+
A5 不带 defer_s → 仍然同步返回(默认面不变)
|
|
9
|
+
A6 工单表有上限(提交 70 个,保留 ≤64)——常驻进程不得无界涨
|
|
10
|
+
"""
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import tempfile
|
|
14
|
+
import time
|
|
15
|
+
|
|
16
|
+
from .mdcos import MdCGSecure
|
|
17
|
+
from . import mcp_server as S
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def main():
|
|
21
|
+
ok = 0
|
|
22
|
+
bad = 0
|
|
23
|
+
|
|
24
|
+
def chk(cond, label):
|
|
25
|
+
nonlocal ok, bad
|
|
26
|
+
if cond:
|
|
27
|
+
ok += 1
|
|
28
|
+
print(" PASS", label)
|
|
29
|
+
else:
|
|
30
|
+
bad += 1
|
|
31
|
+
print(" FAIL", label)
|
|
32
|
+
|
|
33
|
+
root = tempfile.mkdtemp(prefix="as_")
|
|
34
|
+
cg = MdCGSecure(root, autoflush=1)
|
|
35
|
+
for i in range(12):
|
|
36
|
+
cg.add("a%d" % i, "异步检索样本 %d 内容" % i, layer="knowledge", tags=["异步探针"])
|
|
37
|
+
|
|
38
|
+
# A5 默认同步不变
|
|
39
|
+
sync = S.call_tool(cg, "mdcg_search", {"query": "异步探针", "k": 5})
|
|
40
|
+
chk(isinstance(sync, dict) and isinstance(sync.get("results"), list), "A5 不带 defer_s 仍同步返回")
|
|
41
|
+
|
|
42
|
+
# A1 提交立即返回
|
|
43
|
+
t0 = time.time()
|
|
44
|
+
ticket = S.call_tool(cg, "mdcg_search", {"query": "异步探针", "k": 5, "defer_s": 10})
|
|
45
|
+
dt = time.time() - t0
|
|
46
|
+
chk(dt < 0.5, "A1 提交立即返回(%.3fs)" % dt)
|
|
47
|
+
chk(ticket.get("status") == "pending" and ticket.get("job_id"), "A1b 返回工单(%r)" % (ticket.get("job_id"),))
|
|
48
|
+
|
|
49
|
+
# A2 轮询到 done
|
|
50
|
+
jid = ticket["job_id"]
|
|
51
|
+
deadline = time.time() + 20
|
|
52
|
+
done = None
|
|
53
|
+
while time.time() < deadline:
|
|
54
|
+
cur = S.call_tool(cg, "mdcg_search", {"job_id": jid})
|
|
55
|
+
if cur.get("status") in ("done", "error"):
|
|
56
|
+
done = cur
|
|
57
|
+
break
|
|
58
|
+
time.sleep(0.2)
|
|
59
|
+
chk(done is not None and done.get("status") == "done", "A2 轮询最终 done(status=%r)" % (done and done.get("status")))
|
|
60
|
+
async_res = (done or {}).get("result") or {}
|
|
61
|
+
chk(isinstance(async_res.get("results"), list) and async_res["results"], "A2b done 带 result")
|
|
62
|
+
|
|
63
|
+
# A3 与同步结果一致
|
|
64
|
+
s_ids = [r.get("id") for r in (sync.get("results") or [])]
|
|
65
|
+
a_ids = [r.get("id") for r in (async_res.get("results") or [])]
|
|
66
|
+
chk(s_ids == a_ids, "A3 异步与同步 id 集合一致(%d 条)" % len(a_ids))
|
|
67
|
+
|
|
68
|
+
# A4 未知 job
|
|
69
|
+
unk = S.call_tool(cg, "mdcg_search", {"job_id": "sjob_nope_0000"})
|
|
70
|
+
chk(unk.get("status") == "unknown", "A4 未知 job_id 显式 unknown")
|
|
71
|
+
|
|
72
|
+
# A6 上限
|
|
73
|
+
for i in range(70):
|
|
74
|
+
S.call_tool(cg, "mdcg_search", {"query": "异步探针", "k": 1, "defer_s": 30})
|
|
75
|
+
chk(len(S._SEARCH_JOBS) <= S._SEARCH_JOB_MAX,
|
|
76
|
+
"A6 工单表有上限(%d <= %d)" % (len(S._SEARCH_JOBS), S._SEARCH_JOB_MAX))
|
|
77
|
+
|
|
78
|
+
print("=" * 60)
|
|
79
|
+
print("异步检索守卫:%d 通过 / %d 失败" % (ok, bad))
|
|
80
|
+
return 0 if bad == 0 else 1
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
if __name__ == "__main__":
|
|
84
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""检索候选预筛守卫(2026-10-21,分层检索第二级:全局索引 → 正文)。
|
|
3
|
+
|
|
4
|
+
P1 词项命中足够多 → 预筛生效(meta.prefilter 在场),且命中节点仍被召回
|
|
5
|
+
P2 命中太少 → 回退全量(meta.prefilter 缺席),不漏召回
|
|
6
|
+
P3 MDCG_PREFILTER=0 → 不预筛
|
|
7
|
+
P4 预筛不改写任何节点内容(只读)
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import os
|
|
12
|
+
import tempfile
|
|
13
|
+
|
|
14
|
+
from .mdcos import MdCGSecure
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _mk(tag_count_hi=True):
|
|
18
|
+
root = tempfile.mkdtemp(prefix="pf_")
|
|
19
|
+
cg = MdCGSecure(root)
|
|
20
|
+
# 60 条带 tag 的节点:足以越过默认阈值 50
|
|
21
|
+
for i in range(60):
|
|
22
|
+
cg.add("t%d" % i, "样本内容 %d" % i, layer="knowledge",
|
|
23
|
+
tags=["预筛探针", "组%d" % (i % 6)])
|
|
24
|
+
if tag_count_hi:
|
|
25
|
+
for i in range(60):
|
|
26
|
+
cg.add("o%d" % i, "其它内容 %d" % i, layer="knowledge", tags=["无关标签"])
|
|
27
|
+
return root, cg
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def main():
|
|
31
|
+
# 预筛自 2026-10-22 起是 opt-in(实测两次挡住归档读回),测试里显式开启
|
|
32
|
+
os.environ["MDCG_PREFILTER"] = "1"
|
|
33
|
+
ok = 0
|
|
34
|
+
bad = 0
|
|
35
|
+
|
|
36
|
+
def chk(cond, label):
|
|
37
|
+
nonlocal ok, bad
|
|
38
|
+
if cond:
|
|
39
|
+
ok += 1
|
|
40
|
+
print(" PASS", label)
|
|
41
|
+
else:
|
|
42
|
+
bad += 1
|
|
43
|
+
print(" FAIL", label)
|
|
44
|
+
|
|
45
|
+
root, cg = _mk()
|
|
46
|
+
before = (cg.get("t0") or {}).get("content")
|
|
47
|
+
res, meta = cg.search("预筛探针", k=3, record=False)
|
|
48
|
+
ids = [n.get("id") for n, _s, _q in res]
|
|
49
|
+
chk(bool(meta.get("prefilter")), "P1 命中足够多时预筛生效(meta.prefilter=%r)" % (meta.get("prefilter"),))
|
|
50
|
+
chk(any(str(i).startswith("t") for i in ids), "P1b 命中节点仍被召回:%s" % (ids[:3],))
|
|
51
|
+
chk((cg.get("t0") or {}).get("content") == before, "P4 预筛只读(节点内容未变)")
|
|
52
|
+
|
|
53
|
+
# P2:查询词在 tags 里只命中少数(< 阈值 50)→ 回退全量
|
|
54
|
+
cg.add("solo", "独有词 阿尔法", layer="knowledge", tags=["独有词"])
|
|
55
|
+
res2, meta2 = cg.search("阿尔法", k=3, record=False)
|
|
56
|
+
chk(not meta2.get("prefilter"), "P2 命中太少回退全量(无 prefilter 键)")
|
|
57
|
+
ids2 = [n.get("id") for n, _s, _q in res2]
|
|
58
|
+
chk("solo" in [str(i) for i in ids2], "P2b 回退后仍能召回:%s" % (ids2[:3],))
|
|
59
|
+
|
|
60
|
+
# P5:置信回退——预筛命中很多但结果「格式对内容不对」时,自动退回全量
|
|
61
|
+
root2 = tempfile.mkdtemp(prefix="pf2_")
|
|
62
|
+
cg2 = MdCGSecure(root2)
|
|
63
|
+
for i in range(60):
|
|
64
|
+
cg2.add("f%d" % i, "公共内容 %d" % i, layer="knowledge", tags=["公共标签"])
|
|
65
|
+
cg2.add("target", "伽马 德尔塔 密文", layer="knowledge", tags=["孤立标签"])
|
|
66
|
+
res5, meta5 = cg2.search("公共标签 伽马 德尔塔", k=5, record=False)
|
|
67
|
+
ids5 = [str(n.get("id")) for n, _s, _q in res5]
|
|
68
|
+
chk(bool(meta5.get("prefilter_fallback")), "P5 覆盖不足时触发置信回退(cov=%r)" % (meta5.get("prefilter_coverage"),))
|
|
69
|
+
chk("target" in ids5, "P5b 回退后能召回只有正文匹配的节点:%s" % (ids5[:3],))
|
|
70
|
+
|
|
71
|
+
# P3:显式关闭
|
|
72
|
+
os.environ["MDCG_PREFILTER"] = "0"
|
|
73
|
+
try:
|
|
74
|
+
res3, meta3 = cg.search("预筛探针", k=3, record=False)
|
|
75
|
+
chk(not meta3.get("prefilter"), "P3 MDCG_PREFILTER=0 时不预筛")
|
|
76
|
+
finally:
|
|
77
|
+
os.environ["MDCG_PREFILTER"] = "1"
|
|
78
|
+
|
|
79
|
+
print("=" * 60)
|
|
80
|
+
print("候选预筛守卫:%d 通过 / %d 失败" % (ok, bad))
|
|
81
|
+
return 0 if bad == 0 else 1
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
if __name__ == "__main__":
|
|
85
|
+
raise SystemExit(main())
|
package/md_cg/tokens.py
CHANGED
|
@@ -151,9 +151,13 @@ ROLE_SPECS = OrderedDict([
|
|
|
151
151
|
# 其准入不靠 admin 闸而靠签章机械闸(E040/E041/E042)——编译者不得自证
|
|
152
152
|
# task=结构层任务台账(工程做到哪一步/结果是什么)——记录单元本职:
|
|
153
153
|
# 保存过程与结果;「不得自证」由 done 时的结果必填闸承接
|
|
154
|
+
# ref(2026-10-21 采纳外部用户 miyu 的修复二):记录单元要能 prune
|
|
155
|
+
# 派生索引(带 code/doc 标签的索引节点,软删进 trash 可 restore)。
|
|
156
|
+
# 不算放大权限:不动 can_admin、不动 delegable;判据只覆盖索引节点,
|
|
157
|
+
# 记忆节点带 session 标签天然不在清退范围。
|
|
154
158
|
"ops_allow": ["info", "route", "read", "write", "goal", "task", "recent",
|
|
155
159
|
"session", "ingest", "maintain", "insight", "ccg", "status",
|
|
156
|
-
"edges"],
|
|
160
|
+
"edges", "ref"],
|
|
157
161
|
"delegable": False,
|
|
158
162
|
"forbidden": ["self/anchor 层", "private/secret 密级", "裁决与删除"],
|
|
159
163
|
}),
|