@furongjun1999/dsh-memory 0.9.8 → 0.9.10
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 +1 -1
- package/codebuddy/CODEBUDDY.md +4 -0
- package/docs/README.md +3 -0
- 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.md +4 -0
- package/dsh/cordis.yml.example +2 -2
- package/lib/hooks.d.ts +11 -1
- package/lib/hooks.js +58 -3
- package/lib/index.d.ts +1 -1
- package/lib/index.js +2 -2
- package/lib/init.js +11 -2
- package/lib/lib/hook_audit.d.ts +3 -0
- package/lib/lib/hook_audit.js +37 -0
- package/md_cg/autonomy.py +1 -1
- package/md_cg/config_registry.py +329 -329
- package/md_cg/config_registry_bulk.py +2 -0
- package/md_cg/config_render.py +27 -0
- package/md_cg/config_validate.py +10 -8
- package/md_cg/conflict_surface.py +127 -0
- package/md_cg/mcp_server.py +105 -8
- package/md_cg/mdcg.py +60 -2
- package/md_cg/mdcos.py +61 -6
- package/md_cg/pooling.py +487 -485
- package/md_cg/qmd_llm.py +334 -334
- package/md_cg/reach.py +10 -4
- package/md_cg/sleep.py +2 -2
- package/md_cg/sustain.py +3 -3
- package/md_cg/test_auto_defaults.py +463 -463
- package/md_cg/test_autonomy_emotion.py +328 -328
- package/md_cg/test_bm25_default.py +56 -0
- package/md_cg/test_c3_transient_read_negative.py +797 -793
- package/md_cg/test_conflict_surface.py +75 -0
- package/md_cg/test_conflict_value_untrusted.py +63 -0
- package/md_cg/test_exact_text_cache.py +3 -0
- package/md_cg/test_hotcache_tool.py +121 -0
- package/md_cg/test_index_snapshot_fastpath.py +72 -0
- package/md_cg/test_issue274_read_node_id_none.py +29 -6
- package/md_cg/test_issue561_pending_review.py +89 -5
- package/md_cg/test_issue578_service_info_gate.py +264 -0
- package/md_cg/test_p3_legacy_closure.py +5 -2
- package/md_cg/test_p43_pooling.py +419 -412
- package/md_cg/test_reach.py +5 -1
- package/md_cg/test_reach_exits.py +54 -0
- package/md_cg/test_reach_single_char.py +74 -0
- package/md_cg/test_search_bm25_default.py +60 -0
- package/md_cg/test_sleep_p1.py +1032 -1032
- package/md_cg/test_untrusted_audit.py +55 -0
- package/md_cg/test_untrusted_classifier.py +42 -0
- package/md_cg/test_untrusted_enforcement.py +66 -0
- package/md_cg/untrusted.py +45 -0
- package/md_cg/untrusted_audit.py +87 -0
- package/md_cg/value_conflict.py +71 -0
- package/package.json +7 -7
- package/src/hooks.ts +986 -932
- package/src/index.ts +2 -2
- package/src/init.ts +7 -2
- package/src/lib/hook_audit.ts +32 -0
- package/zcode/AGENTS.md +4 -0
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# 功能名:mdcg_service_info 必须与 mdcg_whoami 同闸,且工具面闸表对 TOOLS 完备(issue #578 / dsh-bounty#435)
|
|
3
|
+
# 生效条件:python -X utf8 -m md_cg.test_issue578_service_info_gate [--mutate];被测面=md_cg/mcp_server.py 的 _MDCG_TOOL_OP / _MDCG_OP_REQUIRE 与 _dispatch 对该入口的处置
|
|
4
|
+
# 子功能:G01-G03 静态完备性(对 TOOLS 求双向差集 + 同载荷同档);G04-G06 行为同判(4 个 principal 逐格对照 whoami,含反向对照);G07 载荷收敛**观察项**(不判红,设计取舍未定);M 组=--mutate 定点变异(删掉那一行 ⇒ 必须转红)
|
|
5
|
+
# 执行:--mutate 时在镜像树里删掉 "mdcg_service_info": "read", 一行(回缺陷),守卫必须按指定项转红
|
|
6
|
+
# 验证方式:全部 PASS 即通过(退出码 0);--mutate 下红项集合须与预期逐项相符
|
|
7
|
+
# 不适用条件:① 不判 mdcg_whitebox 的工具面口径(#398 KNOWN_WEAK 已登记、另案);② 不判 MDCG_MCP_SURFACE 的面成员(tools/call 不受 surface 约束,另案);③ 不判载荷该不该带 token_id/session(G07 只打印观察值,不改判据);④ 不判 service_info 的触发面是否应更窄(full surface 才有 tools/list 条目,属产品取舍)。
|
|
8
|
+
"""issue #578 守卫:mdcg_service_info 不得绕过工具面闸表。
|
|
9
|
+
|
|
10
|
+
病象(修前):该入口**不在** _MDCG_TOOL_OP(31 个工具里唯一缺项)⇒ _dispatch 取
|
|
11
|
+
_MDCG_OP_REQUIRE.get(name) 得 None ⇒ 跳过 require_op;而它回吐的 principal.as_dict()
|
|
12
|
+
与**受 read 闸管着**的 mdcg_whoami 是同一份身份清单(tenant/actor/role/clearance/
|
|
13
|
+
can_write/can_admin/session/token_id/layers_allow/ops_allow)+库根绝对路径 ⇒ 只读访客
|
|
14
|
+
与「被 --ops-allow 收窄到不含 read」的令牌都能拿到它。
|
|
15
|
+
|
|
16
|
+
同时钉住上一轮修法没覆盖到的盲点:#398 的守卫
|
|
17
|
+
test_issue398_toolface_op_parity.py 的 E02 断言是
|
|
18
|
+
len(_MDCG_TOOL_OP) == len(_MDCG_OP_REQUIRE) >= 30 —— 两边同源派生,**自比自恒真**,
|
|
19
|
+
「映射缺一项」在结构上不可见。本守卫改为**对 TOOLS 求双向差集**,缺项当场点名。
|
|
20
|
+
"""
|
|
21
|
+
from __future__ import annotations
|
|
22
|
+
|
|
23
|
+
import os
|
|
24
|
+
import shutil
|
|
25
|
+
import subprocess
|
|
26
|
+
import sys
|
|
27
|
+
import tempfile
|
|
28
|
+
|
|
29
|
+
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
30
|
+
|
|
31
|
+
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
32
|
+
if _REPO not in sys.path:
|
|
33
|
+
sys.path.insert(0, _REPO)
|
|
34
|
+
|
|
35
|
+
FAILS = []
|
|
36
|
+
NOTES = []
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _check(name, cond, detail=""):
|
|
40
|
+
if cond:
|
|
41
|
+
print(" PASS " + name)
|
|
42
|
+
else:
|
|
43
|
+
print(" FAIL " + name + " " + str(detail)[:220])
|
|
44
|
+
FAILS.append(name)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _note(name, detail):
|
|
48
|
+
print(" NOTE " + name + " " + str(detail)[:200])
|
|
49
|
+
NOTES.append(name)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
_TMP = tempfile.mkdtemp(prefix="iss578_")
|
|
53
|
+
os.environ.update({"MDCG_ROOT": _TMP + "/brain", "MDCG_STATE_ROOT": _TMP + "/state",
|
|
54
|
+
"MDCG_AUX_ROOT": _TMP + "/auxstore",
|
|
55
|
+
"MDCG_DATA_ROOT": _TMP + "/datastore",
|
|
56
|
+
"MDCG_EXPORT_ROOT": _TMP, "MDCG_SUSTAIN": "0", "MDCG_SLEEP": "0",
|
|
57
|
+
"MDCG_TOKEN_FILE": _TMP + "/auxstore/tokens.json"})
|
|
58
|
+
|
|
59
|
+
from md_cg import crypto # noqa: E402
|
|
60
|
+
os.environ[crypto.MASTER_ENV] = "ab" * 32
|
|
61
|
+
from md_cg import mcp_server as _ms # noqa: E402
|
|
62
|
+
from md_cg.mdcos import MdCGSecure # noqa: E402
|
|
63
|
+
from md_cg.security import Principal # noqa: E402
|
|
64
|
+
|
|
65
|
+
_KEK = b"k" * 32
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _err(fn):
|
|
69
|
+
"""调 fn:正常返回 ("", 结果);抛异常(含 BaseException)返回 (类型名, 文本)。"""
|
|
70
|
+
try:
|
|
71
|
+
return "", fn()
|
|
72
|
+
except BaseException as exc: # noqa: BLE001 —— 变异态不得崩栈
|
|
73
|
+
return type(exc).__name__, str(exc)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
#: 四个 principal:闸的**两侧**都要覆盖(有 read / 无 read / 空 ops / 全域)。
|
|
77
|
+
#: C 是 B 的更极端形态(连 info 都没有)——只看 B 会漏掉 ops_allow 为空这一支。
|
|
78
|
+
_MATRIX = (
|
|
79
|
+
("A 无令牌等价 guest(含 read)",
|
|
80
|
+
dict(role="guest", clearance="internal", can_write=False,
|
|
81
|
+
ops_allow=["info", "route", "read", "recent", "whitebox", "status", "edges"],
|
|
82
|
+
layers_allow=[]),
|
|
83
|
+
"pass"), # guest 本就持 read ⇒ 同闸后仍应放行
|
|
84
|
+
("B 窄化令牌(有 write 无 read)",
|
|
85
|
+
dict(role="designer", clearance="internal", can_write=True,
|
|
86
|
+
ops_allow=["write", "info"], layers_allow=["knowledge"]),
|
|
87
|
+
"deny"), # 修前放行、修后 AccessDenied
|
|
88
|
+
("C 空 ops_allow",
|
|
89
|
+
dict(role="designer", clearance="internal", can_write=True,
|
|
90
|
+
ops_allow=[], layers_allow=["knowledge"]),
|
|
91
|
+
"deny"),
|
|
92
|
+
("D 全域 designer(ops_allow=None)",
|
|
93
|
+
dict(role="designer", clearance="internal", can_write=True,
|
|
94
|
+
ops_allow=None, layers_allow=None),
|
|
95
|
+
"pass"),
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _cg(tag, kw):
|
|
100
|
+
d = os.path.join(_TMP, tag)
|
|
101
|
+
os.makedirs(d, exist_ok=True)
|
|
102
|
+
return MdCGSecure(d + "/root",
|
|
103
|
+
principal=Principal(tenant="default", actor="iss578", **kw),
|
|
104
|
+
master_key=_KEK, autoflush=1)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
print("== G 组 站点 1:闸表对 TOOLS 的**完备性**(静态,双向差集) ==")
|
|
108
|
+
|
|
109
|
+
_tools = [t["name"] for t in _ms.TOOLS]
|
|
110
|
+
_tool_set = set(_tools)
|
|
111
|
+
_toolop = set(_ms._MDCG_TOOL_OP)
|
|
112
|
+
_opreq = set(_ms._MDCG_OP_REQUIRE)
|
|
113
|
+
_missing = sorted(_tool_set - _toolop)
|
|
114
|
+
_extra = sorted(_toolop - _tool_set)
|
|
115
|
+
|
|
116
|
+
_check("G01 ★:TOOLS(%d) 与闸表(%d) 双向差集为空——漏登的工具当场点名(修前漏登 = ['mdcg_service_info'])"
|
|
117
|
+
% (len(_tool_set), len(_toolop)),
|
|
118
|
+
not _missing and not _extra,
|
|
119
|
+
{"漏登": _missing, "多余": _extra})
|
|
120
|
+
_check("G02 派生表与工具表同源同形(_MDCG_OP_REQUIRE 由 _MDCG_TOOL_OP 派生,不得多键/少键)",
|
|
121
|
+
_opreq == _toolop, sorted(_opreq ^ _toolop))
|
|
122
|
+
_check("G03 ★:mdcg_service_info 在闸表内,且与 mdcg_whoami **同档**"
|
|
123
|
+
"(同一份 principal 载荷,闸词不得一有一无)",
|
|
124
|
+
_ms._MDCG_TOOL_OP.get("mdcg_service_info") is not None
|
|
125
|
+
and _ms._MDCG_TOOL_OP.get("mdcg_service_info") == _ms._MDCG_TOOL_OP.get("mdcg_whoami"),
|
|
126
|
+
(_ms._MDCG_TOOL_OP.get("mdcg_service_info"), _ms._MDCG_TOOL_OP.get("mdcg_whoami")))
|
|
127
|
+
|
|
128
|
+
print("== G 组 站点 2:同载荷同闸(行为,逐格对照 whoami) ==")
|
|
129
|
+
|
|
130
|
+
_rows = []
|
|
131
|
+
for _tag, _kw, _expect in _MATRIX:
|
|
132
|
+
_c = _cg(_tag.split()[0], _kw)
|
|
133
|
+
_ts, _ = _err(lambda c=_c: _ms._dispatch(c, "mdcg_service_info", {}))
|
|
134
|
+
_tw, _ = _err(lambda c=_c: _ms._dispatch(c, "mdcg_whoami", {}))
|
|
135
|
+
_rows.append((_tag, _ts or "200", _tw or "200", _expect))
|
|
136
|
+
|
|
137
|
+
_check("G04 ★:service_info 与 whoami 对同一 principal **同判**"
|
|
138
|
+
"(修前 B/C 两格分叉:service_info=200 / whoami=AccessDenied)",
|
|
139
|
+
all(si == wh for _, si, wh, _ in _rows),
|
|
140
|
+
[(t, si, wh) for t, si, wh, _ in _rows if si != wh])
|
|
141
|
+
_check("G05 ★:无 read 的两格对 service_info 是 AccessDenied"
|
|
142
|
+
"(修前 200:身份清单 + token_id/session/ops_allow + 库根绝对路径全量回吐)",
|
|
143
|
+
[si for _, si, _, ex in _rows if ex == "deny"] == ["AccessDenied", "AccessDenied"]
|
|
144
|
+
and [si for _, si, _, ex in _rows if ex == "pass"] == ["200", "200"],
|
|
145
|
+
_rows)
|
|
146
|
+
_check("G06 反向对照:whoami 的读数在任何修法下都不得变化(同闸 ≠ 把 whoami 一起关掉)",
|
|
147
|
+
[wh for _, _, wh, _ in _rows] == ["200", "AccessDenied", "AccessDenied", "200"],
|
|
148
|
+
[(t, wh) for t, _, wh, _ in _rows])
|
|
149
|
+
|
|
150
|
+
print("== G 组 站点 3:载荷收敛观察项(不判红) ==")
|
|
151
|
+
|
|
152
|
+
_guest_cg = _cg("payload", _MATRIX[0][1])
|
|
153
|
+
_, _payload = _err(lambda: _ms._dispatch(_guest_cg, "mdcg_service_info", {}))
|
|
154
|
+
_pr = (_payload or {}).get("principal") or {}
|
|
155
|
+
_exposed = [k for k in ("token_id", "session", "ops_allow", "layers_allow", "harness")
|
|
156
|
+
if k in _pr]
|
|
157
|
+
if _exposed:
|
|
158
|
+
_note("G07 观察:guest 的 service_info 仍带 %s(与 whoami 同载荷)——"
|
|
159
|
+
"「要不要只回版本/计数」属设计取舍,本守卫不判红,交维护者裁定" % _exposed,
|
|
160
|
+
_exposed)
|
|
161
|
+
else:
|
|
162
|
+
_note("G07 观察:guest 的 service_info 已不带头部身份句柄(只回版本/计数)", "clean")
|
|
163
|
+
|
|
164
|
+
# ---------------------------------------------------------------- 定点变异
|
|
165
|
+
_ANCHOR = ' "mdcg_service_info": "read",'
|
|
166
|
+
_EXPECT_RED = ("G01", "G03", "G04", "G05")
|
|
167
|
+
_EOL = chr(13) + chr(10)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _mirror_build():
|
|
171
|
+
"""镜像树:md_cg/ + 仓根 .py(utf8_boot.py 等引导依赖)+ README/pyproject/package.json。
|
|
172
|
+
|
|
173
|
+
只拷 md_cg/ 会在 import 期炸 `No module named 'utf8_boot'`(mcp_server 的引导依赖,
|
|
174
|
+
第 66 行),与 #398 守卫 _mirror_build 的口径保持一致。
|
|
175
|
+
"""
|
|
176
|
+
dst = tempfile.mkdtemp(prefix="iss578_mut_")
|
|
177
|
+
shutil.copytree(os.path.join(_REPO, "md_cg"), os.path.join(dst, "md_cg"),
|
|
178
|
+
ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
|
|
179
|
+
for name in sorted(os.listdir(_REPO)):
|
|
180
|
+
p = os.path.join(_REPO, name)
|
|
181
|
+
if os.path.isfile(p) and (name.endswith(".py")
|
|
182
|
+
or name in ("README.md", "pyproject.toml",
|
|
183
|
+
"package.json")):
|
|
184
|
+
shutil.copy2(p, os.path.join(dst, name))
|
|
185
|
+
return dst
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _run_in(tree):
|
|
189
|
+
"""在镜像树里跑本守卫(禁变异递归),返回 (rc, 红项代号集合, 输出)。"""
|
|
190
|
+
env = dict(os.environ)
|
|
191
|
+
env["PYTHONPATH"] = tree
|
|
192
|
+
env["PYTHONUTF8"] = "1"
|
|
193
|
+
env["PYTHONDONTWRITEBYTECODE"] = "1"
|
|
194
|
+
env["MDCG_ISSUE578_NO_MUTATE"] = "1"
|
|
195
|
+
for k in ("MDCG_ROOT", "MDCG_STATE_ROOT", "MDCG_AUX_ROOT",
|
|
196
|
+
"MDCG_DATA_ROOT", "MDCG_EXPORT_ROOT", "MDCG_TOKEN_FILE"):
|
|
197
|
+
env.pop(k, None)
|
|
198
|
+
pr = subprocess.run([sys.executable, "-X", "utf8", "-m",
|
|
199
|
+
"md_cg.test_issue578_service_info_gate"],
|
|
200
|
+
capture_output=True, text=True, encoding="utf-8",
|
|
201
|
+
errors="replace", cwd=tree, env=env, timeout=600)
|
|
202
|
+
out = (pr.stdout or "") + (pr.stderr or "")
|
|
203
|
+
reds = sorted({ln.split("FAIL ", 1)[1].split(" ")[0].strip()
|
|
204
|
+
for ln in out.splitlines() if ln.strip().startswith("FAIL ")})
|
|
205
|
+
return pr.returncode, reds, out
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def _mutate():
|
|
209
|
+
print("!! 定点变异自证:删掉闸表那一行(回缺陷),守卫必须按指定项转红")
|
|
210
|
+
tree = _mirror_build()
|
|
211
|
+
bad = []
|
|
212
|
+
try:
|
|
213
|
+
rc, reds, out = _run_in(tree)
|
|
214
|
+
print(" 未变异镜像基线:rc=%d 红项=%s(必须 rc=0 且 0 红)" % (rc, reds))
|
|
215
|
+
if rc != 0 or reds:
|
|
216
|
+
bad.append("未变异镜像基线即红")
|
|
217
|
+
print(chr(10).join(out.splitlines()[-20:]))
|
|
218
|
+
p = os.path.join(tree, "md_cg", "mcp_server.py")
|
|
219
|
+
with open(p, encoding="utf-8", newline="") as f:
|
|
220
|
+
bak = f.read()
|
|
221
|
+
eol = chr(13) + chr(10) if (chr(13) + chr(10)) in bak else chr(10)
|
|
222
|
+
anchor = (_ANCHOR + chr(10)).replace(chr(10), eol)
|
|
223
|
+
if bak.count(anchor) != 1:
|
|
224
|
+
bad.append("变异锚点命中 %d 次(要求 1)" % bak.count(anchor))
|
|
225
|
+
print(" ANCHOR-MISS 删行变异:锚点命中 %d 次" % bak.count(anchor))
|
|
226
|
+
else:
|
|
227
|
+
with open(p, "w", encoding="utf-8", newline="") as f:
|
|
228
|
+
f.write(bak.replace(anchor, "", 1))
|
|
229
|
+
for root, _dd, _ff in os.walk(tree):
|
|
230
|
+
if os.path.basename(root) == "__pycache__":
|
|
231
|
+
shutil.rmtree(root, ignore_errors=True)
|
|
232
|
+
rc2, reds2, out2 = _run_in(tree)
|
|
233
|
+
with open(p, "w", encoding="utf-8", newline="") as f:
|
|
234
|
+
f.write(bak)
|
|
235
|
+
if rc2 != 0 and reds2 == sorted(_EXPECT_RED):
|
|
236
|
+
print(" OK 删行变异:红项=%s" % reds2)
|
|
237
|
+
else:
|
|
238
|
+
bad.append("删行变异红项不符")
|
|
239
|
+
print(" MISMATCH 删行变异:实际=%s 期望=%s rc=%d"
|
|
240
|
+
% (reds2, sorted(_EXPECT_RED), rc2))
|
|
241
|
+
for ln in out2.splitlines():
|
|
242
|
+
if ln.strip().startswith("FAIL "):
|
|
243
|
+
print(" " + ln[:170])
|
|
244
|
+
print("定点变异自证:%s(%d 条不合格)" % ("PASS" if not bad else "FAIL", len(bad)))
|
|
245
|
+
return len(bad)
|
|
246
|
+
finally:
|
|
247
|
+
shutil.rmtree(tree, ignore_errors=True)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def main():
|
|
251
|
+
_bad = 0
|
|
252
|
+
if "--mutate" in sys.argv and not os.environ.get("MDCG_ISSUE578_NO_MUTATE"):
|
|
253
|
+
_bad = _mutate()
|
|
254
|
+
shutil.rmtree(_TMP, ignore_errors=True)
|
|
255
|
+
print("")
|
|
256
|
+
if FAILS:
|
|
257
|
+
print("issue #578 结果:%d 失败(%s)" % (len(FAILS), ", ".join(FAILS)))
|
|
258
|
+
else:
|
|
259
|
+
print("issue #578 结果:全部通过(观察项 %d 条未判红)" % len(NOTES))
|
|
260
|
+
return 1 if (FAILS or _bad) else 0
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
if __name__ == "__main__":
|
|
264
|
+
sys.exit(main())
|
|
@@ -163,8 +163,11 @@ def ga_fusion():
|
|
|
163
163
|
print(f" max ={mx}")
|
|
164
164
|
check("A2 默认口径 == max(逐条相同)", d == mx, f"{d} vs {mx}")
|
|
165
165
|
check("A2 默认口径 ≠ sum(逐条不同:分数或名次)", d != s, f"{d} vs {s}")
|
|
166
|
-
|
|
167
|
-
|
|
166
|
+
# 口径差的可观测形式(2026-10-21 重述):**分数或名次**任一不同即算观测到差异。
|
|
167
|
+
# 原断言只比 id——BM25 默认开之后 sum/max 的 id 顺序可能相同、但分数按 2 倍差异,
|
|
168
|
+
# 只比 id 会把"口径确实不同"误判成"没差异"。
|
|
169
|
+
check("A2 top-1 读数在 sum/max 下不同(分数或名次,口径差可观测)",
|
|
170
|
+
bool(d) and bool(s) and d[0] != s[0],
|
|
168
171
|
f"{d[:1]} vs {s[:1]}")
|
|
169
172
|
finally:
|
|
170
173
|
cg.close()
|