@furongjun1999/dsh-memory 0.9.12 → 0.9.14
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 +21 -4
- package/codebuddy/CODEBUDDY.md +8 -3
- package/codebuddy/README.md +92 -92
- package/docs/discipline/templates/zcode-user.md.tmpl +52 -52
- package/docs//345/267/245/344/275/234/347/272/252/345/276/213_/350/256/244/347/237/245/345/233/276/346/235/241/347/233/256_v1.1.json +488 -461
- package/docs//350/247/204/350/214/203/AI/347/274/226/347/250/213/351/200/232/347/224/250/350/247/204/350/214/203_/347/201/265/346/236/242/351/200/202/351/205/215.md +45 -45
- package/dsh/README.md +82 -82
- package/lib/hooks.d.ts +7 -1
- package/lib/hooks.js +62 -31
- package/lib/index.js +4 -1
- package/md_cg/config_registry_bulk.py +2 -0
- package/md_cg/config_validate.py +463 -461
- package/md_cg/conformance_core.py +28 -0
- package/md_cg/consistency.py +9 -2
- package/md_cg/mcp_server.py +91 -0
- package/md_cg/mdcg.py +162 -7
- package/md_cg/mdcos.py +53 -6
- package/md_cg/nodefile.py +39 -1
- package/md_cg/op_table.py +1 -0
- package/md_cg/relations.py +91 -0
- package/md_cg/scrub.py +20 -3
- package/md_cg/test_exact_text_cache.py +12 -4
- package/md_cg/test_issue218_postcondition_value_blind.py +4 -2
- package/md_cg/test_issue644_637_636.py +84 -0
- package/md_cg/test_issue650_658_f26.py +98 -0
- package/md_cg/test_issue659_r26_batch2.py +74 -0
- package/md_cg/test_issue665_scrub_process_records.py +59 -0
- package/md_cg/test_issue669_ccg_empty_colon.py +69 -0
- package/md_cg/test_issue672_validity_scope.py +47 -0
- package/md_cg/version_anchor.py +160 -0
- package/package.json +2 -2
- package/src/hooks.ts +64 -31
- package/src/index.ts +4 -1
- package/zcode/AGENTS.md +8 -3
- package/zcode/README.md +41 -41
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""关系类型白名单(relation_type 的唯一合法域真源)。
|
|
3
|
+
|
|
4
|
+
# 功能名:关系类型白名单与分级校验
|
|
5
|
+
# 生效条件:写入或校验节点 edges[].relation_type 时;扫描全库关系类型分布时
|
|
6
|
+
# 子功能:①合法类型集(按真实边分布定,非代码字面量)②空串=未标注(合法)③分级校验纯函数
|
|
7
|
+
# 执行:reltype_verdict / distribution / singletons / unlabeled_rate
|
|
8
|
+
# 验证方式:tests/test_relation_type_whitelist_guard.py
|
|
9
|
+
# 不适用条件:不判「类型名对了但语义错」(语义判定不可机械验证,同 I3/I6);
|
|
10
|
+
# 不适用于清理空串(那是未标注,不是错误)
|
|
11
|
+
"""
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
#: 合法关系类型集。实取依据(2026-10-23,全库 7350 条边):
|
|
15
|
+
#: generalizes=1034 / instance_of=456 / reference=310 / depends_on=4
|
|
16
|
+
#: derived_from=2 / collaborator=1(孤例) / 空串=5543(未标注)
|
|
17
|
+
RELATION_TYPES = frozenset({
|
|
18
|
+
"generalizes",
|
|
19
|
+
"instance_of",
|
|
20
|
+
"reference",
|
|
21
|
+
"depends_on",
|
|
22
|
+
"derived_from",
|
|
23
|
+
"collaborator", # 孤例:先进白名单避免存量变非法;列入待复核
|
|
24
|
+
"tension", # 新增(使用者裁定 A:允许写入,但无消费者遍历——只沉淀)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
#: 空串 = 未标注。合法,但计入「未标注率」。
|
|
28
|
+
#: 边类型是可选标注,与条件空间(必填,缺则 REJECT)地位不同。
|
|
29
|
+
UNLABELED = ""
|
|
30
|
+
|
|
31
|
+
#: 核心集。孤例复核参照:不在此集且频次为 1 者列入待复核。
|
|
32
|
+
CORE_TYPES = frozenset({"generalizes", "instance_of", "reference"})
|
|
33
|
+
|
|
34
|
+
#: 当前已知孤例(待复核:有效类型还是笔误)。
|
|
35
|
+
KNOWN_SINGLETONS = ("collaborator",)
|
|
36
|
+
|
|
37
|
+
#: 分级模式默认值(env MDCG_RELTYPE_GATE 覆盖):只报告不阻断。
|
|
38
|
+
DEFAULT_MODE = "observe"
|
|
39
|
+
VALID_MODES = ("observe", "enforce")
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def normalize(relation_type) -> str:
|
|
43
|
+
"""去空白后的类型串;None 归一为空串。"""
|
|
44
|
+
return "" if relation_type is None else str(relation_type).strip()
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def reltype_verdict(relation_type):
|
|
48
|
+
"""返回 (ok, kind)。kind 属于 {labeled_ok, unlabeled, unknown}。"""
|
|
49
|
+
rel = normalize(relation_type)
|
|
50
|
+
if rel == UNLABELED:
|
|
51
|
+
return True, "unlabeled"
|
|
52
|
+
if rel in RELATION_TYPES:
|
|
53
|
+
return True, "labeled_ok"
|
|
54
|
+
return False, "unknown"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def resolve_mode(raw, default: str = DEFAULT_MODE) -> str:
|
|
58
|
+
"""把调用方给的原始值归一为合法模式;非法/None 回落 default。
|
|
59
|
+
|
|
60
|
+
注意:本模块**不读环境变量**——读 env 是调用方的职责,模块只做纯判定。
|
|
61
|
+
(首版曾在模块内 os.environ.get,被 check_config_env_registry 的 C1 判为新缺口;
|
|
62
|
+
按「纯逻辑模块不读 env」的设计修正,而非往登记表加一行。)
|
|
63
|
+
"""
|
|
64
|
+
m = ("" if raw is None else str(raw)).strip().lower()
|
|
65
|
+
return m if m in VALID_MODES else default
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def should_block(relation_type, mode: str) -> bool:
|
|
69
|
+
"""enforce 模式下未知类型应阻断;observe 一律不阻断。"""
|
|
70
|
+
ok, _kind = reltype_verdict(relation_type)
|
|
71
|
+
return (not ok) and (mode == "enforce")
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def distribution(edges_iter) -> dict:
|
|
75
|
+
"""统计 relation_type 分布,返回 {type: count};空串键为 UNLABELED。"""
|
|
76
|
+
out = {}
|
|
77
|
+
for rel in edges_iter:
|
|
78
|
+
k = normalize(rel)
|
|
79
|
+
out[k] = out.get(k, 0) + 1
|
|
80
|
+
return out
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def singletons(dist: dict) -> list:
|
|
84
|
+
"""频次为 1 且非未标注的类型,返回待复核列表。"""
|
|
85
|
+
return sorted(k for k, v in dist.items() if v == 1 and k != UNLABELED)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def unlabeled_rate(dist: dict) -> float:
|
|
89
|
+
"""未标注率 = 空串边 / 全部边。无边界返回 0.0。"""
|
|
90
|
+
total = sum(dist.values())
|
|
91
|
+
return 0.0 if not total else dist.get(UNLABELED, 0) / total
|
package/md_cg/scrub.py
CHANGED
|
@@ -231,7 +231,7 @@ def _pool_candidates(cg, *, now, stale_days, unverified_days, max_reads=200):
|
|
|
231
231
|
for nid, e in nodes.items():
|
|
232
232
|
layer = str(e.get("layer") or "")
|
|
233
233
|
if layer in SELF_LAYERS:
|
|
234
|
-
continue
|
|
234
|
+
continue
|
|
235
235
|
created = float(e.get("created_at") or 0)
|
|
236
236
|
age_d = _days(created, now)
|
|
237
237
|
la = float(last.get(nid) or 0)
|
|
@@ -479,8 +479,11 @@ def associate(cg, node_id: str, *, hops: int = DEFAULT_HOPS, limit: int = 30,
|
|
|
479
479
|
# 两侧新增键须同步(交叉守卫 test_validity_filter)。
|
|
480
480
|
_EXPIRY_KEYS = ("effective_until", "valid_until", "expires_at", "expire_at",
|
|
481
481
|
"expiry", "deadline", "expired_at")
|
|
482
|
+
# issue #665:过程记录(rev_*/rej_*)的固定字段也在此列——它们是展示字段,
|
|
483
|
+
# 逐条取值不同,当键跨节点比对必然误报(假设/否决原因 是 rej 记录的两行)。
|
|
482
484
|
_SKIP_KEYS = ("功能名", "执行", "条件", "来源", "标签", "状态", "备注", "标题",
|
|
483
|
-
"描述", "name", "id", "title", "layer", "tags", "说明"
|
|
485
|
+
"描述", "name", "id", "title", "layer", "tags", "说明",
|
|
486
|
+
"假设", "否决原因", "验证")
|
|
484
487
|
_NEG_WORDS = ("禁止", "不得", "不要", "不能", "切勿", "避免", "不应", "不可")
|
|
485
488
|
_POS_WORDS = ("应当", "建议", "必须", "需要", "可以", "允许", "推荐", "宜")
|
|
486
489
|
|
|
@@ -517,6 +520,11 @@ def _kv_pairs(content) -> dict:
|
|
|
517
520
|
line = raw.strip().lstrip("#").strip()
|
|
518
521
|
if not line:
|
|
519
522
|
continue
|
|
523
|
+
# issue #665(#40 的残留面):行首为列表符的行是**展示性字段**
|
|
524
|
+
# (裁决记录的「- 提案 / - 轮次 / - 红队」等),逐条取值天然不同,
|
|
525
|
+
# 当键去跨节点比对必然误报「同键不同值」。不抽键。
|
|
526
|
+
if line[:2] in ("- ", "* ", "+ ", "· ", "• "):
|
|
527
|
+
continue
|
|
520
528
|
for sep in (":", ":"):
|
|
521
529
|
if sep in line:
|
|
522
530
|
k, _, v = line.partition(sep)
|
|
@@ -644,8 +652,17 @@ def audit(cg, node_ids=None, *, hops: int = 1, min_severity: str = "info",
|
|
|
644
652
|
if not e:
|
|
645
653
|
continue
|
|
646
654
|
layer = str(e.get("layer") or "")
|
|
655
|
+
# issue #665(#40 残留面):过程记录不参与矛盾比对——
|
|
656
|
+
# rev_/rej_ 是 contextual 层 role=tool-output 的裁决/否决留痕,属审计面而非知识断言。
|
|
657
|
+
if layer == 'contextual' and str(e.get('role') or '') == 'tool-output':
|
|
658
|
+
continue
|
|
647
659
|
if layer in SELF_LAYERS:
|
|
648
660
|
continue
|
|
661
|
+
# issue #665(#40 残留面):过程记录不参与矛盾比对——rev_/rej_ 是
|
|
662
|
+
# contextual 层 role=tool-output 的裁决/否决留痕,属审计面而非知识断言;
|
|
663
|
+
# 让它们互判「同键不同值」只会把真矛盾淹在误报里。
|
|
664
|
+
if layer == 'contextual' and str((nodes.get(nid) or {}).get('role') or '') == 'tool-output':
|
|
665
|
+
continue
|
|
649
666
|
try:
|
|
650
667
|
node = cg.get(nid)
|
|
651
668
|
except Exception:
|
|
@@ -1043,4 +1060,4 @@ def catalog() -> dict:
|
|
|
1043
1060
|
"unverified_days": UNVERIFIED_DAYS,
|
|
1044
1061
|
"low_conf": LOW_CONF,
|
|
1045
1062
|
"orphan_importance": ORPHAN_IMPORTANCE,
|
|
1046
|
-
"max_offset": MAX_OFFSET}}
|
|
1063
|
+
"max_offset": MAX_OFFSET}}
|
|
@@ -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
|
|
|
@@ -85,8 +85,10 @@ def j0_judgement_unchanged():
|
|
|
85
85
|
ok(nodefile.ELEMENT_TERM_MIN == 2, 'J0c ELEMENT_TERM_MIN 仍为 2',
|
|
86
86
|
nodefile.ELEMENT_TERM_MIN)
|
|
87
87
|
src = io.open(os.path.join(_HERE, "mdcg.py"), encoding="utf-8").read()
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
# issue #652:term 出口加了折叠(terms 与 post 两侧同口径),判据原文随之更新;
|
|
89
|
+
# 断言的是「仍是同一处单点门控」,不是逐字不变。
|
|
90
|
+
ok('if any(t and fold_accents(t) in _post_folded for t in (terms or [])):' in src,
|
|
91
|
+
'J0d term 出口判据原文未变(含 #652 折叠臂)')
|
|
90
92
|
ok("elif is_postcondition_query(q):" in src,
|
|
91
93
|
'J0e declared 出口判据原文未变(本件未取消它)')
|
|
92
94
|
|
|
@@ -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())
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""F26 批次守卫(issue #650 / #652 / #654 / #655 / #656,2026-10-24)。
|
|
3
|
+
|
|
4
|
+
每条对应一个 R26 审计发现的「修复只落在一条路径上」:
|
|
5
|
+
#650 判据面 fold_for_match 与召回面 fold_accents 口径不一致 → 资格态误报
|
|
6
|
+
#652 tags 臂 / index_key 臂未折叠 → 带重音召回回归
|
|
7
|
+
#654 MDCG_MIN_SCORE 只在 _emit 内生效,search_rrf(默认读路径)静默无效
|
|
8
|
+
#655 门槛作用域越界 → 删掉 S5 降权候选与 reach 零分邻居
|
|
9
|
+
#656 负覆盖词项折叠 vs 正文原文 → 带重音负记忆覆盖丢失
|
|
10
|
+
"""
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import os
|
|
14
|
+
import sys
|
|
15
|
+
import tempfile
|
|
16
|
+
|
|
17
|
+
from . import mdcg as M
|
|
18
|
+
from .mdcos import MdCGOS
|
|
19
|
+
from .mdcg import (MdCG, fold_accents, fold_for_match, is_void_content,
|
|
20
|
+
TIER_GLOBAL_SCAN, TIER_GLOBAL_LIKE)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def main():
|
|
24
|
+
ok = bad = 0
|
|
25
|
+
|
|
26
|
+
def chk(cond, label):
|
|
27
|
+
nonlocal ok, bad
|
|
28
|
+
if cond:
|
|
29
|
+
ok += 1
|
|
30
|
+
print(" PASS", label)
|
|
31
|
+
else:
|
|
32
|
+
bad += 1
|
|
33
|
+
print(" FAIL", label)
|
|
34
|
+
|
|
35
|
+
PRE = "caf\u00e9" # café
|
|
36
|
+
COMB = "cafe\u0301" # cafe + U+0301
|
|
37
|
+
NEST = "cafe"
|
|
38
|
+
|
|
39
|
+
# ---- #650:判据面与召回面同口径 ----
|
|
40
|
+
chk(fold_for_match(PRE) == fold_for_match(NEST) == fold_for_match(COMB),
|
|
41
|
+
"#650 fold_for_match 对三种形态给出同一结果")
|
|
42
|
+
chk(fold_accents(PRE) == fold_accents(COMB) == NEST,
|
|
43
|
+
"#650 fold_accents 与 fold_for_match 同口径(都剥组合记号)")
|
|
44
|
+
|
|
45
|
+
# ---- #651:空白判据扩展到 Cf/Mn ----
|
|
46
|
+
chk(all(is_void_content(c) for c in ("\u200b", "\ufeff", "\u2060", "\u0301" * 3)),
|
|
47
|
+
"#651 零宽/软连字符/纯组合记号判为无内容")
|
|
48
|
+
chk(not any(is_void_content(c) for c in (COMB, "正文", "a")),
|
|
49
|
+
"#651 NFD 正文与普通内容不误伤")
|
|
50
|
+
|
|
51
|
+
# ---- #652:tags 臂与 index_key 臂折叠 ----
|
|
52
|
+
cg = MdCG(tempfile.mkdtemp(prefix="f26_"))
|
|
53
|
+
cg.add("tag_node", "无关正文", layer="knowledge", tags=["caf\u00e9"])
|
|
54
|
+
cg.add("key_node", "无关正文", layer="knowledge",
|
|
55
|
+
postcondition_terms=["caf\u00e9 test"])
|
|
56
|
+
cg.flush()
|
|
57
|
+
hits_tags = M.normalize_en("caf\u00e9")
|
|
58
|
+
chk("cafe" in M.fold_accents(" ".join(["caf\u00e9"])),
|
|
59
|
+
"#652 tags 臂已折叠(café tag 对 cafe 查询可见)")
|
|
60
|
+
entry = {"postcondition_terms": ["caf\u00e9 test"]}
|
|
61
|
+
got = M.index_key_hits(entry, ["cafe"], "怎么验证的")
|
|
62
|
+
chk(got.get("post") is True and got.get("post_source") == "term",
|
|
63
|
+
"#652 index_key 臂已折叠(café 索引键被 cafe 词项命中)")
|
|
64
|
+
|
|
65
|
+
# ---- #654:search_rrf 也尊重 MDCG_MIN_SCORE ----
|
|
66
|
+
cg2 = MdCGOS(tempfile.mkdtemp(prefix="f654_"))
|
|
67
|
+
cg2.add("a", "deploy window every wednesday", layer="knowledge")
|
|
68
|
+
cg2.add("b", "totally unrelated content here", layer="knowledge")
|
|
69
|
+
cg2.flush()
|
|
70
|
+
os.environ.pop("MDCG_MIN_SCORE", None)
|
|
71
|
+
r0, _m0 = cg2.search_rrf("deploy window", k=10)
|
|
72
|
+
os.environ["MDCG_MIN_SCORE"] = "0.01"
|
|
73
|
+
try:
|
|
74
|
+
r1, m1 = cg2.search_rrf("deploy window", k=10)
|
|
75
|
+
chk(m1.get("min_score") == 0.01,
|
|
76
|
+
"#654 search_rrf 面已读到门槛(meta.min_score 落地)")
|
|
77
|
+
chk(len(r1 or []) <= len(r0 or []),
|
|
78
|
+
"#654 门槛后结果不增加(%d -> %d)" % (len(r0 or []), len(r1 or [])))
|
|
79
|
+
finally:
|
|
80
|
+
os.environ.pop("MDCG_MIN_SCORE", None)
|
|
81
|
+
|
|
82
|
+
# ---- #655:门槛只作用于 T3 ----
|
|
83
|
+
chk(cg2._primary_bound(10, 0, TIER_GLOBAL_LIKE, 2) == 10,
|
|
84
|
+
"#655 T2 主结果上界不受门槛影响(S5 降权/reach 邻居不被删)")
|
|
85
|
+
chk(cg2._primary_bound(10, 0, TIER_GLOBAL_SCAN, 2) == 10,
|
|
86
|
+
"#655 T3 默认(门槛关)同样不裁切")
|
|
87
|
+
|
|
88
|
+
# ---- #656:负覆盖判定按折叠口径 ----
|
|
89
|
+
chk(fold_accents("cafe") in fold_accents("caf\u00e9 latte"),
|
|
90
|
+
"#656 折叠后 'cafe' 能覆盖 'café' 正文(负记忆覆盖不再丢)")
|
|
91
|
+
|
|
92
|
+
print("=" * 60)
|
|
93
|
+
print("F26 批次守卫:%d 通过 / %d 失败" % (ok, bad))
|
|
94
|
+
return 0 if bad == 0 else 1
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
if __name__ == "__main__":
|
|
98
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""R26 清单(issue #659)第二批守卫:min_score 单点 / exclude_doc 类型与超取。
|
|
3
|
+
|
|
4
|
+
覆盖 F26-06(inf 让检索恒空)、F26-12(两处开关判据相反)、F26-14(门槛读数不可见)、
|
|
5
|
+
F26-15(负覆盖尾绕过门槛)、F26-28(字符串 "false" 反向开启)、F26-11(k 截断后过滤吃配额)。
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import os
|
|
10
|
+
import tempfile
|
|
11
|
+
|
|
12
|
+
from . import mcp_server
|
|
13
|
+
from .mdcg import parse_min_score
|
|
14
|
+
from .mdcos import MdCGOS
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def main():
|
|
18
|
+
ok = bad = 0
|
|
19
|
+
|
|
20
|
+
def chk(cond, label):
|
|
21
|
+
nonlocal ok, bad
|
|
22
|
+
if cond:
|
|
23
|
+
ok += 1
|
|
24
|
+
print(" PASS", label)
|
|
25
|
+
else:
|
|
26
|
+
bad += 1
|
|
27
|
+
print(" FAIL", label)
|
|
28
|
+
|
|
29
|
+
# ---------- F26-06 / F26-12:解析单点 ----------
|
|
30
|
+
chk(parse_min_score("inf", 0.0) == (0.0, "invalid"), "F26-06 inf 判非法并回落默认(不再全量滤空)")
|
|
31
|
+
chk(parse_min_score("-inf", 0.0) == (0.0, "invalid"), "F26-06 -inf 同样判非法")
|
|
32
|
+
chk(parse_min_score("nan", 0.0) == (0.0, "invalid"), "F26-12 nan 判非法(此前静默等同关闭)")
|
|
33
|
+
chk(parse_min_score("0.01", 0.0) == (0.01, "ok"), "F26-06 正常值原样解析")
|
|
34
|
+
chk(parse_min_score(None, -1.0) == (-1.0, "default"), "F26-12 缺省取各自默认(recall 面 -1)")
|
|
35
|
+
|
|
36
|
+
# ---------- F26-28:布尔强制 ----------
|
|
37
|
+
chk(mcp_server._as_flag("false") is False, "F26-28 字符串 false 不再反向开启")
|
|
38
|
+
chk(mcp_server._as_flag("true") is True, "F26-28 字符串 true 判真")
|
|
39
|
+
chk(mcp_server._as_flag(0) is False and mcp_server._as_flag(1) is True, "F26-28 数字按 0/非 0")
|
|
40
|
+
items = [{"id": "doc_1"}, {"id": "mem_1"}]
|
|
41
|
+
kept, dropped = mcp_server.drop_doc_nodes(items, lambda x: x["id"], "false")
|
|
42
|
+
chk(len(kept) == 2 and dropped == 0, "F26-28 传 \"false\" 时不过滤(旧实现会丢掉 doc_1)")
|
|
43
|
+
|
|
44
|
+
# ---------- F26-06 端到端:inf 不再把结果滤空 ----------
|
|
45
|
+
root = tempfile.mkdtemp(prefix="f659_")
|
|
46
|
+
cg = MdCGOS(root)
|
|
47
|
+
cg.add("mem_alpha", "# 功能名:甲\n# 执行:乙", layer="knowledge", tags=["t1"],
|
|
48
|
+
importance=0.6, verification_basis="test")
|
|
49
|
+
cg.flush()
|
|
50
|
+
# 默认读路径是 search_rrf(cg(op=read) bm25=True 走它),故在此验端到端。
|
|
51
|
+
os.environ["MDCG_MIN_SCORE"] = "inf"
|
|
52
|
+
try:
|
|
53
|
+
res, meta = cg.search_rrf("甲", paths=("bm25",), k=5)
|
|
54
|
+
chk(len(res) >= 1, "F26-06 MIN_SCORE=inf 时检索仍有结果(旧实现恒空,实际 %d 条)" % len(res))
|
|
55
|
+
chk(meta.get("min_score_input") == "invalid", "F26-06 非法输入如实记账 min_score_input=invalid")
|
|
56
|
+
finally:
|
|
57
|
+
del os.environ["MDCG_MIN_SCORE"]
|
|
58
|
+
|
|
59
|
+
# ---------- F26-14:门槛读数可观测 ----------
|
|
60
|
+
os.environ["MDCG_MIN_SCORE"] = "0.01"
|
|
61
|
+
try:
|
|
62
|
+
res, meta = cg.search_rrf("甲", paths=("bm25",), k=5)
|
|
63
|
+
chk(meta.get("min_score") == 0.01, "F26-14 生效门槛值透出到 meta(此前写入 stat 无人读)")
|
|
64
|
+
chk("min_score_dropped" in meta, "F26-14 被丢弃条数同样可观测")
|
|
65
|
+
finally:
|
|
66
|
+
del os.environ["MDCG_MIN_SCORE"]
|
|
67
|
+
|
|
68
|
+
print("=" * 56)
|
|
69
|
+
print("R26 第二批守卫:%d 通过 / %d 失败" % (ok, bad))
|
|
70
|
+
return 0 if bad == 0 else 1
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
if __name__ == "__main__":
|
|
74
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""scrub 过程记录误报守卫(issue #665 / #40 残留面)。"""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import tempfile
|
|
6
|
+
|
|
7
|
+
from . import scrub
|
|
8
|
+
from .mdcos import MdCGOS
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def main():
|
|
12
|
+
ok = bad = 0
|
|
13
|
+
|
|
14
|
+
def chk(cond, label):
|
|
15
|
+
nonlocal ok, bad
|
|
16
|
+
if cond:
|
|
17
|
+
ok += 1
|
|
18
|
+
print(" PASS", label)
|
|
19
|
+
else:
|
|
20
|
+
bad += 1
|
|
21
|
+
print(" FAIL", label)
|
|
22
|
+
|
|
23
|
+
# 1) _kv_pairs:列表行不抽键;rej 的两个固定字段也不抽
|
|
24
|
+
rev = "\n".join([
|
|
25
|
+
"# 功能名:裁决记录",
|
|
26
|
+
"- 提案:prop_aaa",
|
|
27
|
+
"- 轮次:1 裁决:accept",
|
|
28
|
+
])
|
|
29
|
+
chk(scrub._kv_pairs(rev) == {}, "#665 列表行不抽键(rev 记录零键)")
|
|
30
|
+
rej = "\n".join(["假设:Claude Desktop", "否决原因:V1 未过", "验证:test"])
|
|
31
|
+
chk(scrub._kv_pairs(rej) == {}, "#665 rej 固定字段已在 _SKIP_KEYS")
|
|
32
|
+
|
|
33
|
+
# 2) 真矛盾仍能被抽到(阴性对照:判据非恒假)
|
|
34
|
+
real = "\n".join(["端口:8080", "协议:tcp"])
|
|
35
|
+
chk(scrub._kv_pairs(real) == {"端口": "8080", "协议": "tcp"},
|
|
36
|
+
"#665 普通断言行仍可抽键(判据非恒假)")
|
|
37
|
+
|
|
38
|
+
# 3) audit 端到端:两条 rev 记录不得互判「同键不同值」
|
|
39
|
+
cg = MdCGOS(tempfile.mkdtemp(prefix="f665_"))
|
|
40
|
+
for i, prop in enumerate(("prop_aaa", "prop_bbb")):
|
|
41
|
+
cg.add("rev_x%d_r1" % i, "\n".join([
|
|
42
|
+
"# 功能名:裁决记录 %d" % i,
|
|
43
|
+
"- 提案:" + prop,
|
|
44
|
+
"- 轮次:1 裁决:accept",
|
|
45
|
+
]), layer="contextual", role="tool-output",
|
|
46
|
+
tags=["review"], importance=0.4, verification_basis="test")
|
|
47
|
+
cg.flush()
|
|
48
|
+
res = scrub.audit(cg, min_severity="info")
|
|
49
|
+
kinds = [i["kind"] for i in res.get("issues", [])
|
|
50
|
+
if i["node_id"].startswith("rev_x")]
|
|
51
|
+
chk("contradiction" not in kinds,
|
|
52
|
+
"#665 过程记录不再被判同键不同值(实际 kinds=%s)" % (kinds,))
|
|
53
|
+
print("=" * 56)
|
|
54
|
+
print("scrub 过程记录守卫:%d 通过 / %d 失败" % (ok, bad))
|
|
55
|
+
return 0 if bad == 0 else 1
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
if __name__ == "__main__":
|
|
59
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""issue #669 守卫:CCG 要素行「带冒号空值 + 换行正文」必须在写入闸门被拒。
|
|
3
|
+
|
|
4
|
+
病象:取值面 ccg_field_value 只看冒号后同一行(得空串),剥除面 _strip_ccg_segments
|
|
5
|
+
见冒号算非空只剥标题行 ⇒ 列表项残留进 body ⇒ _ban_hit(body, neg) 命中自身声明残片
|
|
6
|
+
⇒ verdict=REJECT / conflicts.type=self_negation / score=1.0(假「条件自相矛盾」)。
|
|
7
|
+
|
|
8
|
+
本守卫钉住:①坏形态 fail-closed 且给出两种修法;②三种合法形态一律放行;
|
|
9
|
+
③合规单行(含真正不相交的负条件)不再产生 self_negation(阳性对照:真冲突仍 ctor 命中)。
|
|
10
|
+
"""
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import tempfile
|
|
14
|
+
|
|
15
|
+
from .mdcg import MdCG
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def main():
|
|
19
|
+
ok = 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
|
+
cg = MdCG(tempfile.mkdtemp(prefix="f669_"))
|
|
31
|
+
|
|
32
|
+
def try_add(cid, content):
|
|
33
|
+
try:
|
|
34
|
+
cg.add(cid, content, layer="knowledge")
|
|
35
|
+
return None
|
|
36
|
+
except ValueError as exc:
|
|
37
|
+
return str(exc)
|
|
38
|
+
|
|
39
|
+
# ---------- ① 坏形态:fail-closed + 两种修法都在文案里 ----------
|
|
40
|
+
msg = try_add("bad1", "# 功能名:甲\n# 不适用条件:\n- 批量\n- 本人出镜")
|
|
41
|
+
chk(msg is not None, "#669 带冒号空值 + 列表 → 写入被拒(fail-closed)")
|
|
42
|
+
chk(msg is not None and "单行" in msg and "non_applicable_conditions" in msg,
|
|
43
|
+
"#669 拒绝文案给出两种修法(单行 / frontmatter 列表)")
|
|
44
|
+
chk(msg is not None and "第 2 行" in msg, "#669 文案点名具体行号")
|
|
45
|
+
|
|
46
|
+
# ---------- ② 合法形态一律放行 ----------
|
|
47
|
+
chk(try_add("good1", "# 功能名:甲\n# 不适用条件:批量;本人出镜") is None,
|
|
48
|
+
"#669 带冒号单行(多项用;分隔)放行")
|
|
49
|
+
chk(try_add("good2", "# 功能名:甲\n# 不适用条件\n批量;本人出镜") is None,
|
|
50
|
+
"#669 裸标题 + 单行值(CCG 文档形态)放行")
|
|
51
|
+
chk(try_add("good3", "# 功能名:甲\n# 不适用条件:\n# 执行:乙") is None,
|
|
52
|
+
"#669 带冒号空值但紧跟下一要素(值为空)放行")
|
|
53
|
+
|
|
54
|
+
# ---------- ③ 合规写法不再假 REJECT ----------
|
|
55
|
+
from .consistency import check
|
|
56
|
+
body = ("# 功能名:甲\n# 生效条件:用户问代码怎么写\n"
|
|
57
|
+
"# 执行:给示例\n# 不适用条件:本人出镜;涉密材料")
|
|
58
|
+
rec = check(cg, content=body, condition_space={"domain": "code"}, layer="knowledge")
|
|
59
|
+
types = [c.get("type") for c in (rec.get("conflicts") or [])]
|
|
60
|
+
chk("self_negation" not in types,
|
|
61
|
+
"#669 合规单行(负条件与自身不相交)不再判 self_negation(实际 %s)" % (types,))
|
|
62
|
+
|
|
63
|
+
print("=" * 56)
|
|
64
|
+
print("issue #669 守卫:%d 通过 / %d 失败" % (ok, bad))
|
|
65
|
+
return 0 if bad == 0 else 1
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
if __name__ == "__main__":
|
|
69
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""issue #672 守卫:validity 语义与 meta 口径透出(不改变过滤行为)。"""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import tempfile
|
|
6
|
+
|
|
7
|
+
from .mdcos import MdCGOS
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def main():
|
|
11
|
+
ok = bad = 0
|
|
12
|
+
|
|
13
|
+
def chk(cond, label):
|
|
14
|
+
nonlocal ok, bad
|
|
15
|
+
if cond:
|
|
16
|
+
ok += 1
|
|
17
|
+
print(" PASS", label)
|
|
18
|
+
else:
|
|
19
|
+
bad += 1
|
|
20
|
+
print(" FAIL", label)
|
|
21
|
+
|
|
22
|
+
cg = MdCGOS(tempfile.mkdtemp(prefix="f672_"))
|
|
23
|
+
cg.add("expired", "# 功能名:甲过期\n# 执行:乙", layer="knowledge", importance=0.6,
|
|
24
|
+
verification_basis="test", valid_until="2000-01-01")
|
|
25
|
+
cg.add("live", "# 功能名:甲在效\n# 执行:乙", layer="knowledge", importance=0.6,
|
|
26
|
+
verification_basis="test", valid_from="2000-01-01", valid_until="2099-01-01")
|
|
27
|
+
cg.add("future", "# 功能名:甲未生效\n# 执行:乙", layer="knowledge", importance=0.6,
|
|
28
|
+
verification_basis="test", valid_from="2099-01-01")
|
|
29
|
+
cg.flush()
|
|
30
|
+
|
|
31
|
+
res_off, meta_off = cg.search_rrf("甲", paths=("bm25",), k=10)
|
|
32
|
+
res_on, meta_on = cg.search_rrf("甲", paths=("bm25",), k=10, validity=True)
|
|
33
|
+
ids_off = sorted(r[0].get("id") for r in res_off)
|
|
34
|
+
ids_on = sorted(r[0].get("id") for r in res_on)
|
|
35
|
+
|
|
36
|
+
chk(meta_off.get("validity_scope") == "off", "默认(不启用)meta.validity_scope=off")
|
|
37
|
+
chk(meta_on.get("validity_scope") == "expired_only", "启用时 meta.validity_scope=expired_only")
|
|
38
|
+
chk("expired" in ids_off, "未启用:过期节点仍可见(默认不过滤)")
|
|
39
|
+
chk("expired" not in ids_on, "启用:过期节点被排除")
|
|
40
|
+
chk("future" in ids_on, "启用:**未生效节点仍保留**(名字易误读,本守卫钉住语义)")
|
|
41
|
+
chk("live" in ids_on, "启用:生效中节点保留")
|
|
42
|
+
|
|
43
|
+
return 0 if bad == 0 else 1
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
if __name__ == "__main__":
|
|
47
|
+
raise SystemExit(main())
|