@furongjun1999/dsh-memory 0.10.1 → 0.10.3
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/CHANGELOG.en.md +65 -0
- package/CHANGELOG.md +642 -516
- package/README.en.md +1 -1
- package/README.md +2 -2
- package/docs/mdcg/cogmap_ops.md +15 -13
- package/lib/tools.d.ts +2 -2
- package/md_cg/bucketscope.py +36 -18
- package/md_cg/cohesion.py +337 -0
- package/md_cg/config_registry_bulk.py +5 -0
- package/md_cg/constraint_audit.py +231 -0
- package/md_cg/constraint_watch.py +206 -0
- package/md_cg/context_audit.py +240 -0
- package/md_cg/context_plan.py +142 -0
- package/md_cg/distill.py +247 -0
- package/md_cg/dossier.py +151 -0
- package/md_cg/entities.py +133 -0
- package/md_cg/mcp_server.py +60 -7
- package/md_cg/mdcg.py +27 -0
- package/md_cg/mdcos.py +319 -2
- package/md_cg/op_table.py +14 -0
- package/md_cg/profiles.py +298 -0
- package/md_cg/protocol.py +7 -1
- package/md_cg/reldate.py +142 -0
- package/md_cg/selfstmt.py +139 -0
- package/md_cg/state_slots.py +4 -0
- package/md_cg/state_view.py +181 -0
- package/md_cg/sustain.py +28 -2
- package/md_cg/test_alternatives_historical.py +4 -1
- package/md_cg/test_bucket_time_rounds.py +87 -0
- package/md_cg/test_cohesion.py +107 -0
- package/md_cg/test_constraint_audit.py +87 -0
- package/md_cg/test_constraint_watch.py +108 -0
- package/md_cg/test_context_audit.py +110 -0
- package/md_cg/test_context_plan.py +81 -0
- package/md_cg/tokens.py +1 -1
- package/md_cg/version_anchor.py +84 -1
- package/md_cg/whitebox_kb/wisdom/code_solidified.json +6195 -6195
- package/package.json +137 -137
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""md_cg.context_audit 的守卫测试。
|
|
3
|
+
|
|
4
|
+
固化的行为契约(2026-10-24,框架 v5 第⑤步「三种损失各查一遍」):
|
|
5
|
+
1. 三种损失各由一个读数覆盖:dropped / diluted / broken
|
|
6
|
+
2. 判定按层级裁剪——同一份上下文,L2 可 PASS 而 L3 必须 FAIL
|
|
7
|
+
3. 任一 required 项 False → FAIL
|
|
8
|
+
4. 无 False 但有 None → UNKNOWN(**不默认通过,也不判坏**)
|
|
9
|
+
5. L1/L2 不强制 broken,但读数偏低时给出「可升层」提示
|
|
10
|
+
6. 缺材料 → UNKNOWN 而非 PASS
|
|
11
|
+
|
|
12
|
+
第 2、5 条是实测修正的结果:L2 是「半连续」(步骤要连、章节可断),
|
|
13
|
+
若强制接缝,会把正常的结构化上下文判为失败。
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import os
|
|
17
|
+
import sys
|
|
18
|
+
|
|
19
|
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
20
|
+
|
|
21
|
+
from md_cg import context_audit as ca # noqa: E402
|
|
22
|
+
|
|
23
|
+
FAILS = []
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def check(name, cond, detail=""):
|
|
27
|
+
if cond:
|
|
28
|
+
print(" ok %s" % name)
|
|
29
|
+
else:
|
|
30
|
+
print(" FAIL %s %s" % (name, detail))
|
|
31
|
+
FAILS.append(name)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
# 两块内容不同、但都含目标词的段落(避免依赖外部文件)
|
|
35
|
+
A = ("第一段讨论摘要压缩的机制。摘要保留率等于 1 除以平均段长。"
|
|
36
|
+
"这一结论由位置效应推出,段首存活 93.2%。" * 3)
|
|
37
|
+
B = ("第二段讨论上下文稀释。上下文稀释达 36.7 倍。"
|
|
38
|
+
"稀释不单调,因为同主题重复会抬高词频。" * 3)
|
|
39
|
+
LIST = "C1 摘要保留率等于 1 除以平均段长\nC2 上下文稀释达 36.7 倍\n"
|
|
40
|
+
TGT = "摘要保留率等于 1 除以平均段长,上下文稀释达 36.7 倍。"
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def main():
|
|
44
|
+
# ① 三种损失都有读数
|
|
45
|
+
r = ca.audit("分析影响,必须给出可核对读数", blocks=[A, B], checklist=LIST, target_text=TGT)
|
|
46
|
+
check("三种损失都有读数",
|
|
47
|
+
set(r["losses"].keys()) == {"dropped", "diluted", "broken"}, r["losses"].keys())
|
|
48
|
+
|
|
49
|
+
# ② 层级裁剪:同一上下文 L2 与 L3 判定可不同
|
|
50
|
+
l2 = ca.audit("分析影响,必须给出可核对读数", blocks=[A, B], checklist=LIST, target_text=TGT)
|
|
51
|
+
l3 = ca.audit("导出数据,但不能覆盖旧文件", blocks=[A, B], checklist=LIST, target_text=TGT)
|
|
52
|
+
check("L2 判为 L2", l2["level"] == "L2", l2["level"])
|
|
53
|
+
check("L3 判为 L3", l3["level"] == "L3", l3["level"])
|
|
54
|
+
check("L2 不强制 broken", l2["required"]["broken"] is False, l2["required"])
|
|
55
|
+
check("L3 强制 broken", l3["required"]["broken"] is True, l3["required"])
|
|
56
|
+
|
|
57
|
+
# ③ 缺材料 → UNKNOWN(不默认通过)
|
|
58
|
+
r = ca.audit("导出数据,但不能覆盖旧文件")
|
|
59
|
+
check("缺材料 UNKNOWN", r["verdict"] == "UNKNOWN", r["verdict"])
|
|
60
|
+
check("缺材料 无 PASS", r["verdict"] != "PASS", r)
|
|
61
|
+
|
|
62
|
+
# ④ 约束缺失 → FAIL
|
|
63
|
+
bad = LIST + "C9 不得把火星数据写入木星表\n"
|
|
64
|
+
r = ca.audit("导出数据,但不能覆盖旧文件", blocks=[A, B], checklist=bad, target_text=TGT)
|
|
65
|
+
check("约束缺失 FAIL", r["verdict"] == "FAIL", r["verdict"])
|
|
66
|
+
check("约束缺失 列出 dropped", "dropped" in r["failed"], r["failed"])
|
|
67
|
+
|
|
68
|
+
# ⑤ L1 连 diluted 之外的 broken 也不要求
|
|
69
|
+
r = ca.audit("帮我查一下是哪个版本", blocks=[A, B], checklist=LIST, target_text=TGT)
|
|
70
|
+
check("L1 不强制 broken", r["required"]["broken"] is False, r["required"])
|
|
71
|
+
|
|
72
|
+
# ⑥ 只报不做:必带 note
|
|
73
|
+
r = ca.audit("分析影响", blocks=[A, B], checklist=LIST, target_text=TGT)
|
|
74
|
+
check("必带 note", bool(r["note"]) and "只报不做" in r["note"], r["note"][:40])
|
|
75
|
+
|
|
76
|
+
# ⑦ 可选回取(2026-10-24 第四十一轮补)
|
|
77
|
+
class FakeCG:
|
|
78
|
+
def search(self, q, k=2):
|
|
79
|
+
return ([{"id": "cand_1", "content": "候选出处内容"}], {})
|
|
80
|
+
bad = LIST + "C9 不得把火星数据写入木星表\n"
|
|
81
|
+
r_no = ca.audit("导出数据,但不能覆盖旧文件", blocks=[A, B], checklist=bad, target_text=TGT)
|
|
82
|
+
check("不传 cg 时无 sources",
|
|
83
|
+
all("sources" not in m for m in r_no["losses"]["dropped"].get("missing", [])),
|
|
84
|
+
r_no["losses"]["dropped"].get("missing"))
|
|
85
|
+
r_yes = ca.audit("导出数据,但不能覆盖旧文件", blocks=[A, B], checklist=bad,
|
|
86
|
+
target_text=TGT, cg=FakeCG())
|
|
87
|
+
missing = r_yes["losses"]["dropped"].get("missing", [])
|
|
88
|
+
check("传 cg 时补 sources",
|
|
89
|
+
any(m.get("sources") for m in missing), missing)
|
|
90
|
+
check("actions 含候选出处",
|
|
91
|
+
any("候选出处" in a for a in r_yes["actions"]), r_yes["actions"][:2])
|
|
92
|
+
check("note 声明只查不改", "只查不改" in r_yes["note"], r_yes["note"][:60])
|
|
93
|
+
# 限量:max_lookup 生效
|
|
94
|
+
many = LIST + "".join("C%d 不得写入木星表%d\n" % (i, i) for i in range(100, 110))
|
|
95
|
+
r_lim = ca.audit("导出数据,但不能覆盖旧文件", blocks=[A, B], checklist=many,
|
|
96
|
+
target_text=TGT, cg=FakeCG(), max_lookup=2)
|
|
97
|
+
withsrc = sum(1 for m in r_lim["losses"]["dropped"].get("missing", [])
|
|
98
|
+
if m.get("sources"))
|
|
99
|
+
check("max_lookup 限量生效", withsrc <= 2, withsrc)
|
|
100
|
+
|
|
101
|
+
print("")
|
|
102
|
+
if FAILS:
|
|
103
|
+
print("失败 %d 项:%s" % (len(FAILS), FAILS))
|
|
104
|
+
return 1
|
|
105
|
+
print("全部通过")
|
|
106
|
+
return 0
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
if __name__ == "__main__":
|
|
110
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""md_cg.context_plan 的守卫测试。
|
|
3
|
+
|
|
4
|
+
固化的行为契约(2026-10-24,上下文管理研究框架 v4 五步判据链):
|
|
5
|
+
1. 命中不可逆词 → L3(后果不可撤销,哪怕同时含强制词)
|
|
6
|
+
2. 只命中强制词 → L2
|
|
7
|
+
3. 查事实型且无强制/不可逆 → L1
|
|
8
|
+
4. 都没有 → 灰区(**不按 L1 兜底**)
|
|
9
|
+
5. 灰区的声明按 L3、策略按实际(解耦)
|
|
10
|
+
6. 每层输出都带 caveat(这是建议不是裁决)
|
|
11
|
+
|
|
12
|
+
第 4 条为什么重要:误判为高损失可逆有界,误判为低损失不可逆无界(研究第六轮)。
|
|
13
|
+
所以「判不出」必须落到灰区/L2,绝不能落到 L1。
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
import os
|
|
17
|
+
import sys
|
|
18
|
+
|
|
19
|
+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
20
|
+
|
|
21
|
+
from md_cg import context_plan as cp # noqa: E402
|
|
22
|
+
|
|
23
|
+
FAILS = []
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def check(name, cond, detail=""):
|
|
27
|
+
if cond:
|
|
28
|
+
print(" ok %s" % name)
|
|
29
|
+
else:
|
|
30
|
+
print(" FAIL %s %s" % (name, detail))
|
|
31
|
+
FAILS.append(name)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def main():
|
|
35
|
+
# ① 不可逆 → L3(即使同时有强制词)
|
|
36
|
+
r = cp.plan("导出数据,但不能覆盖旧文件,必须留下校验记录")
|
|
37
|
+
check("不可逆优先于强制 → L3", r["level"] == "L3", r["level_reason"])
|
|
38
|
+
check("L3 声明含外置清单", "外置" in r["declaration"], r["declaration"])
|
|
39
|
+
check("L3 核对不得静默", "不得静默" in r["check"], r["check"])
|
|
40
|
+
|
|
41
|
+
# ② 只强制 → L2
|
|
42
|
+
r = cp.plan("提交前必须跑一遍测试")
|
|
43
|
+
check("只强制 → L2", r["level"] == "L2", r["level_reason"])
|
|
44
|
+
|
|
45
|
+
# ③ 查事实 → L1
|
|
46
|
+
r = cp.plan("帮我查一下智能论是哪个版本")
|
|
47
|
+
check("查事实 → L1", r["level"] == "L1", r["level_reason"])
|
|
48
|
+
|
|
49
|
+
# ④ 判不出 → 灰区,不落 L1
|
|
50
|
+
r = cp.plan("把这份报告整理成三页")
|
|
51
|
+
check("判不出 → 灰区", r["level"] == "灰区", r["level_reason"])
|
|
52
|
+
check("灰区声明按 L3", "按 L3 声明" in r["declaration"], r["declaration"])
|
|
53
|
+
check("灰区策略按实际", "策略按实际" in r["strategy"], r["strategy"])
|
|
54
|
+
|
|
55
|
+
# ⑤ 各层形态互不相同(判据链第②步真的分流了)
|
|
56
|
+
forms = {lv: cp.plan(t)["form"] for lv, t in [
|
|
57
|
+
("L1", "查一下版本号是多少"),
|
|
58
|
+
("L2", "必须检查一遍"),
|
|
59
|
+
("L3", "删除旧文件"),
|
|
60
|
+
]}
|
|
61
|
+
check("三层形态不同", len(set(forms.values())) == 3, forms)
|
|
62
|
+
|
|
63
|
+
# ⑥ 每层都带 caveat
|
|
64
|
+
for t in ("查一下版本", "必须检查", "删除文件", "整理成三页"):
|
|
65
|
+
r = cp.plan(t)
|
|
66
|
+
check("caveat 存在(%s)" % r["level"], bool(r["caveat"]))
|
|
67
|
+
|
|
68
|
+
# ⑦ 空输入不炸,落灰区
|
|
69
|
+
r = cp.plan("")
|
|
70
|
+
check("空输入 → 灰区", r["level"] == "灰区", r["level"])
|
|
71
|
+
|
|
72
|
+
print("")
|
|
73
|
+
if FAILS:
|
|
74
|
+
print("失败 %d 项:%s" % (len(FAILS), FAILS))
|
|
75
|
+
return 1
|
|
76
|
+
print("全部通过")
|
|
77
|
+
return 0
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
if __name__ == "__main__":
|
|
81
|
+
raise SystemExit(main())
|
package/md_cg/tokens.py
CHANGED
|
@@ -58,7 +58,7 @@ ALL_LAYERS = ("anchor", "structural", "knowledge", "contextual", "self",
|
|
|
58
58
|
# 认知图 op 全集(与 mcp_server._cg_call 对齐,供 ops_allow 收窄)
|
|
59
59
|
# help 按需披露入口:工具面投影(见 md_cg/tool_face.py)后,被外置的完整
|
|
60
60
|
# op/参数语义在此从真源取回;只读元信息,不经角色闸(不属于写/裁决面)
|
|
61
|
-
ALL_OPS = ("help", "info", "route", "read", "write", "goal", "task", "recent", "verify",
|
|
61
|
+
ALL_OPS = ("state_view", "help", "info", "route", "read", "write", "goal", "task", "recent", "verify",
|
|
62
62
|
"review", "forget", "protect", "identity", "consistency",
|
|
63
63
|
"metacognition", "self_state", "evolution", "sustain", "scrub",
|
|
64
64
|
"predict", "causal", "whitebox", "index_code", "index_doc", "ref",
|
package/md_cg/version_anchor.py
CHANGED
|
@@ -26,7 +26,8 @@
|
|
|
26
26
|
|
|
27
27
|
import re
|
|
28
28
|
|
|
29
|
-
__all__ = ["FIELDS", "parse_anchor", "check_anchor", "
|
|
29
|
+
__all__ = ["FIELDS", "parse_anchor", "check_anchor", "check_staleness",
|
|
30
|
+
"scan_dir", "ANCHOR_LINE_RE"]
|
|
30
31
|
|
|
31
32
|
#: 三元字段(键名 → 中文别名,都接受)
|
|
32
33
|
FIELDS = ("theory", "architecture", "projection")
|
|
@@ -101,6 +102,88 @@ def check_anchor(text):
|
|
|
101
102
|
"found": rep["found"], "legacy": legacy}
|
|
102
103
|
|
|
103
104
|
|
|
105
|
+
def _norm(v):
|
|
106
|
+
"""归一化版本串:去空白与全角空格,便于比较「智能论 3.4」与「智能论3.4」。"""
|
|
107
|
+
import re as _re
|
|
108
|
+
return _re.sub(r"\s+", "", str(v or ""))
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
#: 架构名与版本的分隔约定(2026-10-24 第五十三轮):
|
|
112
|
+
#: 规范写法是「架构名 v版本」,如「上下文管理研究 v6」。
|
|
113
|
+
#: 若写成「上下文管理研究(四十九轮)」这类不带 v 的形式,会被判为 nonstandard——
|
|
114
|
+
#: **不猜它的版本**(第五十二轮实测:把轮次当版本会让检查变成噪声源)。
|
|
115
|
+
_ARCH_VER_RE = re.compile(r"^(.*?)\s+v(\d+(?:\.\d+)*)\s*$")
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def split_arch(arch_field):
|
|
119
|
+
"""把「架构名 v版本」拆成 (名字, 版本)。不规范返回 (原文, None)。"""
|
|
120
|
+
m = _ARCH_VER_RE.match(str(arch_field or "").strip())
|
|
121
|
+
if not m:
|
|
122
|
+
return str(arch_field or "").strip(), None
|
|
123
|
+
return m.group(1).strip(), "v" + m.group(2)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def check_staleness(text, current):
|
|
127
|
+
"""陈化检查——判断锚里的理论/架构版本是否还是当前版本。
|
|
128
|
+
|
|
129
|
+
为什么需要(2026-10-24 第五十一、五十二轮):
|
|
130
|
+
第五十一轮发现「过期」是三种不可测失效之一,对策是「版本锚」。
|
|
131
|
+
但实测发现:56 份文档 100% 都有锚,**却没有工具核对锚是否过期**——
|
|
132
|
+
写上 ≠ 核对。本函数补上核对这一步。
|
|
133
|
+
|
|
134
|
+
参数:
|
|
135
|
+
current 当前版本,形如 {"theory": "智能论 3.4", "architecture": "..."}。
|
|
136
|
+
**必须由调用方提供**——本模块不知道「现在是什么版本」,
|
|
137
|
+
那是外部事实,不能猜。
|
|
138
|
+
|
|
139
|
+
返回 dict:
|
|
140
|
+
ok True=锚与当前一致;False=**过期**;None=无锚或无法判定
|
|
141
|
+
stale [{field, found, current}] 过期的字段
|
|
142
|
+
anchor 解析出的锚
|
|
143
|
+
|
|
144
|
+
**「本投影」版本不参与比较**——它是本文档自己的版本,无所谓过期。
|
|
145
|
+
"""
|
|
146
|
+
rep = parse_anchor(text)
|
|
147
|
+
# legacy 材料:已显式标注为历史(I18 的 legacy_no_anchor)。
|
|
148
|
+
# 它「没有三元锚」是**明示的选择**,不是遗漏——故与「忘了标」区分开。
|
|
149
|
+
legacy = LEGACY_MARK in str(text or "")
|
|
150
|
+
if not rep.get("found"):
|
|
151
|
+
return {"ok": None, "stale": [], "anchor": {}, "legacy": legacy,
|
|
152
|
+
"reason": ("已标 legacy(历史材料),不参与陈化检查" if legacy
|
|
153
|
+
else "没有版本锚,无从判断是否过期")}
|
|
154
|
+
stale = []
|
|
155
|
+
notes = []
|
|
156
|
+
|
|
157
|
+
# theory:直接比较
|
|
158
|
+
want_t = (current or {}).get("theory")
|
|
159
|
+
got_t = rep["anchor"].get("theory")
|
|
160
|
+
if want_t and got_t and _norm(got_t) != _norm(want_t):
|
|
161
|
+
stale.append({"field": "theory", "found": got_t, "current": want_t})
|
|
162
|
+
|
|
163
|
+
# architecture:**按架构名分组比较**(2026-10-24 第五十三轮修正)
|
|
164
|
+
# 一个 current 比所有文档会让「不同架构」被判过期——实测 13 份里 5 份是别的架构。
|
|
165
|
+
got_a = rep["anchor"].get("architecture")
|
|
166
|
+
want_a = (current or {}).get("architecture")
|
|
167
|
+
arch_map = want_a if isinstance(want_a, dict) else None
|
|
168
|
+
if got_a:
|
|
169
|
+
name, ver = split_arch(got_a)
|
|
170
|
+
if arch_map is not None:
|
|
171
|
+
if name not in arch_map:
|
|
172
|
+
notes.append("架构「%s」不在当前版本表中,不判过期(可能是另一份架构或历史材料)" % name)
|
|
173
|
+
elif ver is None:
|
|
174
|
+
notes.append("架构字段「%s」不含 v 版本号,格式不规范,不判过期也不判通过" % got_a)
|
|
175
|
+
elif _norm(ver) != _norm(arch_map.get(name)):
|
|
176
|
+
stale.append({"field": "architecture", "found": got_a,
|
|
177
|
+
"current": "%s %s" % (name, arch_map.get(name))})
|
|
178
|
+
elif want_a:
|
|
179
|
+
if _norm(got_a) != _norm(want_a):
|
|
180
|
+
stale.append({"field": "architecture", "found": got_a, "current": want_a})
|
|
181
|
+
|
|
182
|
+
return {"ok": (None if notes else not stale), "stale": stale, "anchor": rep["anchor"],
|
|
183
|
+
"notes": notes,
|
|
184
|
+
"note": "本投影版本不参与比较;架构按名分组比较"}
|
|
185
|
+
|
|
186
|
+
|
|
104
187
|
def scan_dir(directory, suffix=".md"):
|
|
105
188
|
"""扫描目录下的对外材料,分类为 ok/legacy/todo。
|
|
106
189
|
|