vod-devkit 0.5.5 → 0.5.8
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.md +32 -0
- package/QUICKSTART.md +47 -23
- package/README.en.md +7 -2
- package/README.md +6 -2
- package/bin/vod-devkit +12 -1164
- package/contract/tools.schema.json +1 -1
- package/package.json +8 -2
- package/plugins/vod-devkit/.codex-plugin/plugin.json +1 -1
- package/plugins/vod-devkit/build-info.json +3 -3
- package/plugins/vod-devkit/contract/tools.schema.json +446 -0
- package/plugins/vod-devkit/safety/policy.json +48 -0
- package/plugins/vod-devkit/vod_mcp/__init__.py +1 -1
- package/plugins/vod-devkit/vod_mcp/__pycache__/__init__.cpython-314.pyc +0 -0
- package/plugins/vod-devkit/vod_mcp/__pycache__/attribution.cpython-314.pyc +0 -0
- package/plugins/vod-devkit/vod_mcp/__pycache__/net.cpython-314.pyc +0 -0
- package/plugins/vod-devkit/vod_mcp/__pycache__/paths.cpython-314.pyc +0 -0
- package/plugins/vod-devkit/vod_mcp/__pycache__/policy.cpython-314.pyc +0 -0
- package/plugins/vod-devkit/vod_mcp/__pycache__/provenance.cpython-314.pyc +0 -0
- package/plugins/vod-devkit/vod_mcp/__pycache__/runtime_state.cpython-314.pyc +0 -0
- package/plugins/vod-devkit/vod_mcp/__pycache__/skills_registry.cpython-314.pyc +0 -0
- package/plugins/vod-devkit/vod_mcp/__pycache__/tools.cpython-314.pyc +0 -0
- package/plugins/vod-devkit/vod_mcp/host_adapters/base.py +3 -1
- package/plugins/vod-devkit/vod_mcp/host_adapters/hermes.py +88 -11
- package/plugins/vod-devkit/vod_mcp/hosts.py +3 -1
- package/plugins/vod-devkit/vod_mcp/net.py +251 -0
- package/plugins/vod-devkit/vod_mcp/packs/__pycache__/__init__.cpython-314.pyc +0 -0
- package/plugins/vod-devkit/vod_mcp/packs/__pycache__/collect.cpython-314.pyc +0 -0
- package/plugins/vod-devkit/vod_mcp/packs/__pycache__/core.cpython-314.pyc +0 -0
- package/plugins/vod-devkit/vod_mcp/packs/__pycache__/knowledge.cpython-314.pyc +0 -0
- package/plugins/vod-devkit/vod_mcp/packs/collect.py +684 -0
- package/plugins/vod-devkit/vod_mcp/packs/core.py +80 -0
- package/plugins/vod-devkit/vod_mcp/packs/knowledge.py +17 -0
- package/plugins/vod-devkit/vod_mcp/paths.py +38 -0
- package/plugins/vod-devkit/vod_mcp/payload.py +3 -1
- package/plugins/vod-devkit/vod_mcp/policy.py +3 -2
- package/plugins/vod-devkit/vod_mcp/provenance.py +3 -2
- package/plugins/vod-devkit/vod_mcp/runtime_state.py +324 -0
- package/plugins/vod-devkit/vod_mcp/server.py +10 -7
- package/plugins/vod-devkit/vod_mcp/skills_registry.py +5 -4
- package/plugins/vod-devkit/vod_mcp/tools.py +23 -1298
- package/plugins/vod-devkit/vod_mcp/transport_remote.py +21 -9
- package/plugins/vod-devkit/vod_mcp/update_check.py +1 -1
- package/vod_mcp/__init__.py +1 -1
- package/vod_mcp/host_adapters/__pycache__/__init__.cpython-39.pyc +0 -0
- package/vod_mcp/host_adapters/__pycache__/atomcode.cpython-39.pyc +0 -0
- package/vod_mcp/host_adapters/__pycache__/base.cpython-314.pyc +0 -0
- package/vod_mcp/host_adapters/__pycache__/base.cpython-39.pyc +0 -0
- package/vod_mcp/host_adapters/__pycache__/codearts.cpython-39.pyc +0 -0
- package/vod_mcp/host_adapters/__pycache__/codeartswork.cpython-39.pyc +0 -0
- package/vod_mcp/host_adapters/__pycache__/codex.cpython-39.pyc +0 -0
- package/vod_mcp/host_adapters/__pycache__/codex_desktop.cpython-39.pyc +0 -0
- package/vod_mcp/host_adapters/__pycache__/dsh.cpython-39.pyc +0 -0
- package/vod_mcp/host_adapters/__pycache__/generic.cpython-39.pyc +0 -0
- package/vod_mcp/host_adapters/__pycache__/hermes.cpython-314.pyc +0 -0
- package/vod_mcp/host_adapters/__pycache__/hermes.cpython-39.pyc +0 -0
- package/vod_mcp/host_adapters/__pycache__/opencode.cpython-39.pyc +0 -0
- package/vod_mcp/host_adapters/__pycache__/workbuddy.cpython-39.pyc +0 -0
- package/vod_mcp/host_adapters/base.py +3 -1
- package/vod_mcp/host_adapters/hermes.py +88 -11
- package/vod_mcp/hosts.py +3 -1
- package/vod_mcp/net.py +251 -0
- package/vod_mcp/packs/__pycache__/__init__.cpython-314.pyc +0 -0
- package/vod_mcp/packs/__pycache__/__init__.cpython-39.pyc +0 -0
- package/vod_mcp/packs/__pycache__/collect.cpython-314.pyc +0 -0
- package/vod_mcp/packs/__pycache__/collect.cpython-39.pyc +0 -0
- package/vod_mcp/packs/__pycache__/core.cpython-314.pyc +0 -0
- package/vod_mcp/packs/__pycache__/core.cpython-39.pyc +0 -0
- package/vod_mcp/packs/__pycache__/knowledge.cpython-314.pyc +0 -0
- package/vod_mcp/packs/__pycache__/knowledge.cpython-39.pyc +0 -0
- package/vod_mcp/packs/collect.py +684 -0
- package/vod_mcp/packs/core.py +80 -0
- package/vod_mcp/packs/knowledge.py +17 -0
- package/vod_mcp/paths.py +38 -0
- package/vod_mcp/payload.py +3 -1
- package/vod_mcp/policy.py +3 -2
- package/vod_mcp/provenance.py +3 -2
- package/vod_mcp/runtime_state.py +324 -0
- package/vod_mcp/server.py +10 -7
- package/vod_mcp/skills_registry.py +5 -4
- package/vod_mcp/tools.py +23 -1298
- package/vod_mcp/transport_remote.py +21 -9
- package/vod_mcp/update_check.py +1 -1
- package/vodkit_cli/__init__.py +10 -0
- package/vodkit_cli/__pycache__/__init__.cpython-314.pyc +0 -0
- package/vodkit_cli/__pycache__/__init__.cpython-39.pyc +0 -0
- package/vodkit_cli/__pycache__/cli.cpython-314.pyc +0 -0
- package/vodkit_cli/__pycache__/cli.cpython-39.pyc +0 -0
- package/vodkit_cli/__pycache__/common.cpython-314.pyc +0 -0
- package/vodkit_cli/__pycache__/common.cpython-39.pyc +0 -0
- package/vodkit_cli/__pycache__/doctor.cpython-314.pyc +0 -0
- package/vodkit_cli/__pycache__/doctor.cpython-39.pyc +0 -0
- package/vodkit_cli/__pycache__/install.cpython-314.pyc +0 -0
- package/vodkit_cli/__pycache__/install.cpython-39.pyc +0 -0
- package/vodkit_cli/__pycache__/remote.cpython-314.pyc +0 -0
- package/vodkit_cli/__pycache__/remote.cpython-39.pyc +0 -0
- package/vodkit_cli/__pycache__/uninstall.cpython-314.pyc +0 -0
- package/vodkit_cli/__pycache__/uninstall.cpython-39.pyc +0 -0
- package/vodkit_cli/__pycache__/update.cpython-314.pyc +0 -0
- package/vodkit_cli/__pycache__/update.cpython-39.pyc +0 -0
- package/vodkit_cli/cli.py +86 -0
- package/vodkit_cli/common.py +254 -0
- package/vodkit_cli/doctor.py +269 -0
- package/vodkit_cli/install.py +260 -0
- package/vodkit_cli/remote.py +200 -0
- package/vodkit_cli/uninstall.py +161 -0
- package/vodkit_cli/update.py +45 -0
- /package/plugins/vod-devkit/vod_mcp/{packs.py → packs/__init__.py} +0 -0
- /package/vod_mcp/{packs.py → packs/__init__.py} +0 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"""core pack:自述、认证自检、版本提醒、产品候选检索(只读,零写盘副作用)。"""
|
|
2
|
+
from vod_mcp import attribution as _attrib
|
|
3
|
+
from vod_mcp import net, provenance
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def search_products(params: dict) -> dict:
|
|
7
|
+
"""产品候选检索(只读):给模型 3-5 个候选去问用户,而不是从名录里瞎猜。
|
|
8
|
+
名录=引擎 meta 接口的 productCatalog(长度随运营变更,插件不内置)。"""
|
|
9
|
+
query = str(params.get("query") or "").strip()
|
|
10
|
+
limit = int(params.get("limit") or 5)
|
|
11
|
+
limit = max(1, min(limit, 20))
|
|
12
|
+
meta = net._load_meta()
|
|
13
|
+
catalog = meta.get("catalog") or []
|
|
14
|
+
if not query:
|
|
15
|
+
return {"candidates": [], "total": len(catalog)}
|
|
16
|
+
idx = _attrib.build_alias_index(meta)
|
|
17
|
+
hits = _attrib.match_products(query, idx)
|
|
18
|
+
seen, cands = set(), []
|
|
19
|
+
for h in hits:
|
|
20
|
+
pid = str(h.get("id") or "")
|
|
21
|
+
if not pid or pid in seen:
|
|
22
|
+
continue
|
|
23
|
+
seen.add(pid)
|
|
24
|
+
cands.append({"id": pid, "name": h["product"],
|
|
25
|
+
"score": round(min(1.0, len(h["alias"]) / max(len(query), 1)), 2),
|
|
26
|
+
"matchReason": f"alias:{h['alias']}"})
|
|
27
|
+
if not cands: # 别名没命中:退回名字子串(用户可能打错/只说一半)
|
|
28
|
+
q = query.lower()
|
|
29
|
+
for item in catalog:
|
|
30
|
+
name = str(item.get("name") or "")
|
|
31
|
+
if q and (q in name.lower() or name.lower() in q):
|
|
32
|
+
cands.append({"id": str(item.get("id")), "name": name, "score": 0.5,
|
|
33
|
+
"matchReason": "name-substring"})
|
|
34
|
+
if not cands: # 完全无关联词:给名录前 N 个当"可选项"(问用户用)
|
|
35
|
+
for item in catalog[:limit]:
|
|
36
|
+
cands.append({"id": str(item.get("id")), "name": str(item.get("name") or ""),
|
|
37
|
+
"score": 0.0, "matchReason": "catalog-default"})
|
|
38
|
+
cands.sort(key=lambda c: (c["score"], len(c["name"])), reverse=True)
|
|
39
|
+
return {"candidates": cands[:limit], "total": len(catalog)}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def devkit_info(params: dict, client_info: dict | None = None) -> dict:
|
|
43
|
+
return provenance.info_payload(client_info)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def auth_status(params: dict, client_info: dict | None = None) -> dict:
|
|
47
|
+
"""认证自检:指纹化输出(明文永不出现),无 key 也跑并给引导。
|
|
48
|
+
|
|
49
|
+
远程面(HTTP)缺陷修正:keyCheck 探针旧实现只看本地 net.JAVA_KEY(env/runtime),
|
|
50
|
+
远程面没有本地 key 时 keyCheck 永远 null——远程 agent 永远无法验证自己
|
|
51
|
+
请求头里的 key(真实场景:坏 key 装上、提交才 401)。修法:服务面下探
|
|
52
|
+
net._eff_key()(请求头 Bearer),本地态 javaKey 段照实报"本机未配置",另加
|
|
53
|
+
requestKey 段报请求头 key 的指纹与探针结果——身份随请求走,两段不混。
|
|
54
|
+
"""
|
|
55
|
+
req_key = net._eff_key() if net._SERVER_FACE.get() else None
|
|
56
|
+
out = {
|
|
57
|
+
"javaKey": net._key_state(),
|
|
58
|
+
"packs": sorted(__import__("vod_mcp.packs", fromlist=["enabled_packs"]).enabled_packs()),
|
|
59
|
+
}
|
|
60
|
+
if req_key:
|
|
61
|
+
out["requestKey"] = {"fingerprint": net._fingerprint(req_key),
|
|
62
|
+
"note": "服务面:身份来自请求头 Bearer"}
|
|
63
|
+
# 探针显式传请求头 key(net.probe_key 默认取本地 net.JAVA_KEY——服务面必须覆盖)
|
|
64
|
+
out["keyCheck"] = net.probe_key(req_key)
|
|
65
|
+
elif net.JAVA_KEY:
|
|
66
|
+
# 主动探针(devkit auth init 真调 IAM 的廉价版):用户问"key 好没好"有确定答案
|
|
67
|
+
out["keyCheck"] = net.probe_key()
|
|
68
|
+
if net._explicit_key():
|
|
69
|
+
# 旧版残留凭据检测位(评审拍板项):显式 key 存活即在位提示自愈路径,
|
|
70
|
+
# 不等提交撞 401 才发现(codex 0.2.3 残留 key 挡匿名链的实锤教训)
|
|
71
|
+
out["legacyKeyHint"] = net._legacy_key_hint()
|
|
72
|
+
if not net.JAVA_KEY and not req_key:
|
|
73
|
+
out["guidance"] = net._KEY_GUIDANCE
|
|
74
|
+
out["tokenReady"] = "首次提交时将自动申请匿名令牌(零字段,日限额低)"
|
|
75
|
+
return out
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def check_update(params: dict, client_info: dict | None = None) -> dict:
|
|
79
|
+
from vod_mcp import update_check as uc
|
|
80
|
+
return uc.check(params)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""knowledge pack:规则技能披露薄壳(实现在 skills_registry,本层只做错误码转译)。"""
|
|
2
|
+
from vod_mcp import net
|
|
3
|
+
|
|
4
|
+
def list_skills(params: dict, client_info: dict | None = None) -> dict:
|
|
5
|
+
from vod_mcp import skills_registry as sr
|
|
6
|
+
try:
|
|
7
|
+
return sr.list_skills(params)
|
|
8
|
+
except sr.SkillError as e:
|
|
9
|
+
raise net.ToolError(str(e), code=e.code)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def retrieve_skill(params: dict, client_info: dict | None = None) -> dict:
|
|
13
|
+
from vod_mcp import skills_registry as sr
|
|
14
|
+
try:
|
|
15
|
+
return sr.retrieve_skill(params)
|
|
16
|
+
except sr.SkillError as e:
|
|
17
|
+
raise net.ToolError(str(e), code=e.code)
|
package/vod_mcp/paths.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""根定位单一真源(P1)。
|
|
2
|
+
|
|
3
|
+
此前有 11 处各自用 `Path(__file__).resolve().parent.parent` 反推仓库根:
|
|
4
|
+
tools / server / skills_registry / policy / provenance / hosts /
|
|
5
|
+
transport_remote / payload / host_adapters.base / bin。分散的代价不是不好看,
|
|
6
|
+
而是“想挪文件”本身有成本——每挪一个模块都要重算相对深度,于是没人敢动目录。
|
|
7
|
+
|
|
8
|
+
三种根形态必须同时成立(都已实测):
|
|
9
|
+
① 仓内直跑:ROOT = 仓库根(vod_mcp 的父目录)
|
|
10
|
+
② 载荷:~/.vod-devkit/payloads/<ver>/(payload.stage 固化,含 vod_mcp 与资源)
|
|
11
|
+
③ 插件副本:plugins/vod-devkit/(宿主隔离安装的独立根,资源随包携带)
|
|
12
|
+
三者的共同点:`<ROOT>/vod_mcp/`、`<ROOT>/contract/`、`<ROOT>/safety/`、`<ROOT>/skills/`
|
|
13
|
+
都在同一层。所以“ROOT = vod_mcp 的父目录”这个算式对三者都成立——前提是这三份
|
|
14
|
+
形态都真的把资源带齐(插件副本此前缺 contract/safety,靠本提交补齐)。
|
|
15
|
+
|
|
16
|
+
环境变量优先是为了把隐式相对路径变成显式契约:宿主在隔离安装里把插件目录整块
|
|
17
|
+
拷走时,可以在启动配置里声明 VOD_DEVKIT_ROOT,而不是赌 __file__ 的深度。
|
|
18
|
+
"""
|
|
19
|
+
import os
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
|
|
22
|
+
# 物理默认根:本包目录的父目录。不读环境变量,供 sys.path 自举等“必须物理正确”
|
|
23
|
+
# 的场合使用(见 server.py 的 bootstrap)。
|
|
24
|
+
DEFAULT_ROOT = Path(__file__).resolve().parent.parent
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def root() -> Path:
|
|
28
|
+
"""运行期根:环境变量优先,物理位置兜底。"""
|
|
29
|
+
env = os.environ.get("VOD_DEVKIT_ROOT")
|
|
30
|
+
return Path(env).expanduser().resolve() if env else DEFAULT_ROOT
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
ROOT = root()
|
|
34
|
+
SKILLS = ROOT / "skills"
|
|
35
|
+
CONTRACT = ROOT / "contract" / "tools.schema.json"
|
|
36
|
+
POLICY = ROOT / "safety" / "policy.json"
|
|
37
|
+
INSTALL_MANIFEST = ROOT / "install-manifest.json"
|
|
38
|
+
RUNTIME_JSON = ROOT / "vod_mcp" / "runtime.json"
|
package/vod_mcp/payload.py
CHANGED
package/vod_mcp/policy.py
CHANGED
package/vod_mcp/provenance.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"""溯源与宿主身份:build-info.json(构建烧录)+ 握手 clientInfo + install-manifest。"""
|
|
2
2
|
import json
|
|
3
|
-
from pathlib import Path
|
|
4
3
|
|
|
5
|
-
|
|
4
|
+
from vod_mcp import paths
|
|
5
|
+
|
|
6
|
+
_ROOT = paths.ROOT
|
|
6
7
|
_BUILD_INFO = _ROOT / "build-info.json"
|
|
7
8
|
_MANIFEST = _ROOT / "install-manifest.json"
|
|
8
9
|
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
"""运行期状态:runtime.json 读写/锁、账本、草稿、会话记忆、install nonce、日志。
|
|
2
|
+
|
|
3
|
+
为什么要单独成模块(P2):状态是"读改写 + 内存镜像"的单点,散在 tools.py 里时
|
|
4
|
+
两处吃亏——远程面并发覆盖(F3)与手工 `_RT[k]=v` 漏同步(F2,曾致首提必 401)。
|
|
5
|
+
本模块只依赖 paths 与 stdlib;需要身份(net._eff_key)时**函数内懒导入**,避免
|
|
6
|
+
net ↔ runtime_state 的循环。
|
|
7
|
+
"""
|
|
8
|
+
import json
|
|
9
|
+
import os
|
|
10
|
+
import stat
|
|
11
|
+
import threading
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from vod_mcp import attribution as _attrib
|
|
15
|
+
from vod_mcp import paths
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
_RT_LOCK = threading.RLock()
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _runtime_path() -> Path:
|
|
22
|
+
return paths.RUNTIME_JSON
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _runtime_cfg() -> dict:
|
|
26
|
+
# 路径单源:一律走 _runtime_path()(曾与 _save_runtime 双写路径,
|
|
27
|
+
# 注入/重定位场景读写分叉——测试实锤的真缺陷)
|
|
28
|
+
p = _runtime_path()
|
|
29
|
+
if p.exists():
|
|
30
|
+
try:
|
|
31
|
+
return json.loads(p.read_text(encoding="utf-8"))
|
|
32
|
+
except Exception:
|
|
33
|
+
return {}
|
|
34
|
+
return {}
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
_RT = _runtime_cfg()
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _runtime_update(update: dict, replace: bool = False) -> None:
|
|
41
|
+
"""原子更新运行期状态:落盘 → 镜像(同锁内)。
|
|
42
|
+
|
|
43
|
+
replace=False 时按单参调用 _save_runtime:测试桩普遍是 `lambda u: ...`
|
|
44
|
+
(历史签名),多传一个默认值会把所有桩打红——签名兼容性属于接口契约。
|
|
45
|
+
"""
|
|
46
|
+
with _RT_LOCK:
|
|
47
|
+
if replace:
|
|
48
|
+
_save_runtime(update, replace=True)
|
|
49
|
+
else:
|
|
50
|
+
_save_runtime(update)
|
|
51
|
+
if replace:
|
|
52
|
+
_RT.clear()
|
|
53
|
+
_RT.update(update)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _save_runtime(update: dict, replace: bool = False) -> None:
|
|
57
|
+
"""低层落盘(唯一写文件处)。整段读改写必须持 _RT_LOCK——锁可重入,
|
|
58
|
+
上层 _runtime_update/_ledger_append/draft 复合操作嵌套调用安全。"""
|
|
59
|
+
with _RT_LOCK:
|
|
60
|
+
p = _runtime_path()
|
|
61
|
+
cfg = {}
|
|
62
|
+
if p.exists() and not replace:
|
|
63
|
+
try:
|
|
64
|
+
cfg = json.loads(p.read_text(encoding="utf-8"))
|
|
65
|
+
except Exception:
|
|
66
|
+
cfg = {}
|
|
67
|
+
cfg.update(update)
|
|
68
|
+
p.write_text(json.dumps(cfg, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
69
|
+
# S1 式写后验(借 huaweicloud-devkit ensurePrivateMode 模式):写完 chmod 0600
|
|
70
|
+
# 并 stat 回验——不假设 chmod 按文档工作(WSL/drvfs 会静默忽略 chmod),
|
|
71
|
+
# 验的是效果不是动作;runtime.json 含 java_key 明文,权限失效必须告警
|
|
72
|
+
try:
|
|
73
|
+
os.chmod(p, 0o600)
|
|
74
|
+
mode = stat.S_IMODE(os.stat(p).st_mode)
|
|
75
|
+
if mode != 0o600:
|
|
76
|
+
_warn(f"runtime.json 权限异常({oct(mode)},期望 0600)——"
|
|
77
|
+
"key 可能被同机其他用户读取,请检查文件系统(WSL 挂载盘建议移回 Linux 原生盘)")
|
|
78
|
+
except OSError as e:
|
|
79
|
+
_warn(f"runtime.json 权限校验失败:{e}")
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _log(line: str, path: str = "/tmp/vod_mcp_server.log") -> None:
|
|
83
|
+
"""统一日志出口,1MB 截断轮转
|
|
84
|
+
(D-key-guard-3⑤:客户机长期运行防无限增长;截断保留尾部近期日志)。"""
|
|
85
|
+
try:
|
|
86
|
+
p = path
|
|
87
|
+
if os.path.exists(p) and os.path.getsize(p) > 1024 * 1024:
|
|
88
|
+
with open(p, "r", encoding="utf-8", errors="replace") as f:
|
|
89
|
+
tail = f.read()[-200 * 1024:]
|
|
90
|
+
os.replace(p, p + ".old")
|
|
91
|
+
with open(p, "w", encoding="utf-8") as f:
|
|
92
|
+
f.write("== truncated ==\n" + tail)
|
|
93
|
+
with open(p, "a", encoding="utf-8") as f:
|
|
94
|
+
f.write(line.rstrip("\n") + "\n")
|
|
95
|
+
except Exception:
|
|
96
|
+
pass
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _warn(msg: str) -> None:
|
|
100
|
+
"""告警出口:server 日志(排障真源),失败静默——不阻断业务主链。"""
|
|
101
|
+
_log(f"warn {msg}")
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _distribution() -> str:
|
|
105
|
+
"""分发形态位(D-key-guard-3):install --distribution preset 写入 manifest。
|
|
106
|
+
devkit=自装自管;preset=产品随 agent 分发(客户机首启重生成运行时状态)。"""
|
|
107
|
+
p = paths.INSTALL_MANIFEST
|
|
108
|
+
if not p.exists():
|
|
109
|
+
return "devkit"
|
|
110
|
+
try:
|
|
111
|
+
return json.loads(p.read_text(encoding="utf-8")).get("distribution", "devkit")
|
|
112
|
+
except Exception:
|
|
113
|
+
return "devkit"
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
_RUNTIME_STATE_KEYS = ("java_key", "token_tier", "install_nonce", "submissions",
|
|
117
|
+
"meta_cache")
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _ensure_first_boot(rt_path: Path | None = None) -> dict:
|
|
121
|
+
"""首启标记 + 构建机残留清理(D-key-guard-3①,preset 形态兜底)。
|
|
122
|
+
|
|
123
|
+
主防线是打包排除(分发产物不含 runtime.json);本函数兜底打包者漏排:
|
|
124
|
+
preset 形态 + runtime.json 存在但无 firstBootAt + 携带运行时状态
|
|
125
|
+
(key/nonce/账本/缓存)→ 判构建机残留 → 自动清空这些状态(保留
|
|
126
|
+
product/java_base 等装机配置)+ 重新生成 per-install nonce + 告警。
|
|
127
|
+
处置用自动重生成而非拒绝启动:残留状态全是本机可再生数据,
|
|
128
|
+
自动清零伤害半径为零,拒启会把误判放大成全功能瘫痪。
|
|
129
|
+
firstBootAt 首启写入,此后不再触发(防误伤正常运行中的安装)。"""
|
|
130
|
+
p = rt_path or _runtime_path()
|
|
131
|
+
if not p.exists():
|
|
132
|
+
return {"action": "none"}
|
|
133
|
+
try:
|
|
134
|
+
cfg = json.loads(p.read_text(encoding="utf-8"))
|
|
135
|
+
except Exception:
|
|
136
|
+
return {"action": "none"}
|
|
137
|
+
if cfg.get("firstBootAt"):
|
|
138
|
+
_warn_if_cloned(cfg, p)
|
|
139
|
+
return {"action": "none"}
|
|
140
|
+
residual = [k for k in _RUNTIME_STATE_KEYS if cfg.get(k)]
|
|
141
|
+
import time as _t
|
|
142
|
+
if _distribution() == "preset" and residual:
|
|
143
|
+
# 残留清理=整体重建(合并删不掉旧键),但保留装机配置(product/java_base 等)
|
|
144
|
+
_save_runtime({**{k: v for k, v in cfg.items() if k not in _RUNTIME_STATE_KEYS},
|
|
145
|
+
"firstBootAt": _t.strftime("%Y-%m-%dT%H:%M:%S")},
|
|
146
|
+
replace=True)
|
|
147
|
+
_warn(f"检测到构建机残留状态({','.join(residual)})已自动清空重生成——"
|
|
148
|
+
"分发产物应只含代码与配置,不含安装结果(D-key-guard-3①)")
|
|
149
|
+
return {"action": "cleaned", "removed": residual}
|
|
150
|
+
_save_runtime({"firstBootAt": _t.strftime("%Y-%m-%dT%H:%M:%S")})
|
|
151
|
+
return {"action": "stamped"}
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _uptime_seconds() -> float:
|
|
155
|
+
"""本机 uptime(秒)。独立函数:测试可 monkeypatch,消除对宿主机器的依赖
|
|
156
|
+
(实锤:test_clone_image_detection_warns 曾硬编码 boot_age=7 天与真机
|
|
157
|
+
/proc/uptime 实值比较,uptime>7 天的机器必红——环境相关测试缺陷)。"""
|
|
158
|
+
return float(open("/proc/uptime").read().split()[0])
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _warn_if_cloned(cfg: dict, p: Path) -> None:
|
|
162
|
+
"""preset 克隆镜像检测(D-key-guard-3①延伸,仅告警不阻断):
|
|
163
|
+
firstBootAt 距今显著超过本机 uptime(>1h 容差)→ 本机状态大概率来自
|
|
164
|
+
母机镜像克隆——所有克隆机共享同一 device 锚点/账本,声量与配额会收敛。
|
|
165
|
+
判据:firstBootAt 的"年龄" > uptime+容差 ⇒ 首启发生在"这台机器"存在之前。"""
|
|
166
|
+
import time as _t
|
|
167
|
+
if _distribution() != "preset":
|
|
168
|
+
return
|
|
169
|
+
try:
|
|
170
|
+
boot = _t.mktime(_t.strptime(cfg["firstBootAt"], "%Y-%m-%dT%H:%M:%S"))
|
|
171
|
+
uptime_s = _uptime_seconds()
|
|
172
|
+
except (KeyError, ValueError, OSError) as ex:
|
|
173
|
+
# 收窄捕获(原 except Exception: pass 吞掉真实异常,告警失效不可见):
|
|
174
|
+
# 只吞解析类异常(缺字段/格式坏/读不到 /proc),其余如实上抛
|
|
175
|
+
_log(f"clone 检测跳过(无法解析 firstBootAt/uptime):{ex}")
|
|
176
|
+
return
|
|
177
|
+
if _t.time() - boot > uptime_s + 3600:
|
|
178
|
+
_warn("疑似镜像克隆机:firstBootAt 早于本机 uptime——克隆分发未清 "
|
|
179
|
+
"runtime.json,所有克隆机共享同一设备锚点/账本。请在镜像冻结前 "
|
|
180
|
+
"uninstall 或删除 runtime.json(装机 checklist ②)")
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _install_nonce() -> str:
|
|
184
|
+
"""per-install 8 位 hex:runtime.json 首次生成常驻——legacy 共用 key 场景下
|
|
185
|
+
source 命名空间 per-machine 化,杜绝不同用户提交同款内容被确定性
|
|
186
|
+
voiceId 合并成一条(跨用户吞声量,切换补丁前置修复)。"""
|
|
187
|
+
n = _RT.get("install_nonce")
|
|
188
|
+
if not n:
|
|
189
|
+
import uuid
|
|
190
|
+
n = uuid.uuid4().hex[:8]
|
|
191
|
+
# 首装 nonce 必须同步回写内存态 _RT(D-dev-nonce-sync,2026-11-09 真机实锤):
|
|
192
|
+
# _RT 仅模块导入时加载一次,_save_runtime 只落盘——不同步则同进程内每次调用
|
|
193
|
+
# 生成新 nonce:领令牌(设备 A)与提交(设备 B)设备绑定校验失败,重试仍
|
|
194
|
+
# 新 nonce(设备 C)再 401——全新安装首次提交必失败冒给用户(存量安装
|
|
195
|
+
# runtime.json 已有 nonce 不受影响)。同步后领令牌/提交同设备,401→领令牌
|
|
196
|
+
# →重试全程进程内无感。
|
|
197
|
+
try:
|
|
198
|
+
_runtime_update({"install_nonce": n})
|
|
199
|
+
except Exception:
|
|
200
|
+
_RT["install_nonce"] = n # 落盘失败也要在进程内保持一致
|
|
201
|
+
return str(n)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
_SESSION_TTL = 30 * 60 # 会话内滑动记忆(D-product-ask-1)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
_SESSION_MEM: dict = {} # 进程=一次宿主会话:重启即清,跨会话不继承
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def _session_key(client_info) -> str:
|
|
211
|
+
"""记忆作用域(Scope):宿主 + 身份。
|
|
212
|
+
|
|
213
|
+
进程内即一次 stdio 会话;远程面是多租户共享进程,所以身份也必须进 key,
|
|
214
|
+
否则 A 用户的"不知道→CodeArts"会串到 B 用户。
|
|
215
|
+
"""
|
|
216
|
+
ci = client_info or {}
|
|
217
|
+
import hashlib
|
|
218
|
+
from vod_mcp import net as _net # 懒导入:net 依赖本模块,导入期成环
|
|
219
|
+
ident = _net._eff_key() or ""
|
|
220
|
+
ident_hash = hashlib.sha256(ident.encode()).hexdigest()[:8] if ident else _install_nonce()
|
|
221
|
+
return f"{ci.get('name') or 'unknown'}|{ident_hash}"
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def _memory_get(client_info) -> dict:
|
|
225
|
+
import time as _t
|
|
226
|
+
cur = _SESSION_MEM.get(_session_key(client_info)) or {}
|
|
227
|
+
if cur and _t.time() - float(cur.get("at") or 0) > _SESSION_TTL:
|
|
228
|
+
return {}
|
|
229
|
+
return dict(cur)
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def _memory_put(client_info, **fields) -> None:
|
|
233
|
+
import time as _t
|
|
234
|
+
with _RT_LOCK: # get→update→set 持锁:远程面并发请求不互相丢字段
|
|
235
|
+
key = _session_key(client_info)
|
|
236
|
+
cur = _memory_get(client_info)
|
|
237
|
+
cur.update(fields)
|
|
238
|
+
cur["at"] = int(_t.time()) # 滑动过期
|
|
239
|
+
_SESSION_MEM[key] = cur
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
_ATTR_HISTORY_CAP = 10
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def _remember_attribution(result: dict, client_info: dict | None = None) -> None:
|
|
246
|
+
"""写记忆:只接受「字面证据唯一命中」,歧义/无证据/记忆本身不写(S100)。
|
|
247
|
+
|
|
248
|
+
覆盖时留快照(D-attrib-B 追加,评审意见采纳项):旧条目进
|
|
249
|
+
attribution_history(cap 10,带 replacedAt),事后能回答「当时为什么归
|
|
250
|
+
到 A 而不是 B」——单条记忆被覆盖后判据不可追是排障黑洞。
|
|
251
|
+
"""
|
|
252
|
+
# 会话记忆(D-product-ask-1):只记「有证据/用户明确回答/用户说不知道」三种
|
|
253
|
+
src = result.get("source")
|
|
254
|
+
if result.get("product"):
|
|
255
|
+
_memory_put(client_info, ask_failures=0)
|
|
256
|
+
if src in ("user_input", "user_answer"):
|
|
257
|
+
_memory_put(client_info, product=result.get("product"),
|
|
258
|
+
productName=result.get("productName"),
|
|
259
|
+
alias=(result.get("evidence") or [{}])[0].get("alias"))
|
|
260
|
+
elif src == "host_default":
|
|
261
|
+
_memory_put(client_info, not_specified=True,
|
|
262
|
+
product=result.get("product"), productName=result.get("productName"))
|
|
263
|
+
mem = _attrib.remember_if_evidenced(result)
|
|
264
|
+
if not mem:
|
|
265
|
+
return
|
|
266
|
+
with _RT_LOCK:
|
|
267
|
+
prev = _RT.get("attribution_memory") or {}
|
|
268
|
+
if prev and prev.get("product"):
|
|
269
|
+
hist = list(_RT.get("attribution_history") or [])
|
|
270
|
+
hist.append({**prev, "replacedAt": mem["at"]})
|
|
271
|
+
hist = hist[-_ATTR_HISTORY_CAP:]
|
|
272
|
+
_runtime_update({"attribution_history": hist})
|
|
273
|
+
_runtime_update({"attribution_memory": mem})
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
_LEDGER_KEY = "submissions"
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
_LEDGER_CAP = 200
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def _ledger_append(entries: list) -> None:
|
|
283
|
+
"""本地提交账本(层级①):本机提交记录,供 vod_list_submitted 对账。
|
|
284
|
+
cap 200 条(环形截断,防 runtime.json 膨胀)。
|
|
285
|
+
实现纪律:每次从文件读改写、只动 submissions 键——不从内存 _RT
|
|
286
|
+
合并(陈旧配置会整体回写,覆盖其他键,且把无关配置泄入调用方)。
|
|
287
|
+
D-key-guard-3④:local_ledger=false(企业数据驻留要求)→ 账本关闭,
|
|
288
|
+
vod_list_submitted 随之不可用——留痕是可选项不是义务。"""
|
|
289
|
+
if not entries:
|
|
290
|
+
return
|
|
291
|
+
with _RT_LOCK: # 读改写整段持锁:远程面并发提交不再互相覆盖账本
|
|
292
|
+
fresh = _runtime_cfg()
|
|
293
|
+
if fresh.get("local_ledger") is False:
|
|
294
|
+
return
|
|
295
|
+
ledger = (fresh.get(_LEDGER_KEY) or [])[-_LEDGER_CAP:]
|
|
296
|
+
ledger.extend(entries)
|
|
297
|
+
_save_runtime({_LEDGER_KEY: ledger[-_LEDGER_CAP:]})
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
_DRAFT_KEY = "drafts"
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
_DRAFT_CAP = 10
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
_DRAFT_TTL = 600 # 秒;过期 commit 拒收,草稿自动作废
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def _drafts_purge(store: dict) -> dict:
|
|
310
|
+
"""过期草稿清除(读时惰性,无后台任务)。"""
|
|
311
|
+
import time as _time
|
|
312
|
+
now = _time.time()
|
|
313
|
+
return {k: v for k, v in store.items()
|
|
314
|
+
if v.get("expiresAt", 0) > now}
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def _drafts_load() -> dict:
|
|
318
|
+
"""草稿库现读 + 过期惰性清除(draft/commit 单源共用,禁双边漂移)。"""
|
|
319
|
+
return _drafts_purge(_runtime_cfg().get(_DRAFT_KEY) or {})
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def _sha256(s: str) -> str:
|
|
323
|
+
import hashlib
|
|
324
|
+
return hashlib.sha256(s.encode()).hexdigest()
|
package/vod_mcp/server.py
CHANGED
|
@@ -9,10 +9,13 @@ import sys
|
|
|
9
9
|
from pathlib import Path
|
|
10
10
|
|
|
11
11
|
# 自举包路径(对任意 MCP 宿主成立:不依赖宿主透传 PYTHONPATH——
|
|
12
|
-
# Hermes 等宿主出于安全过滤环境变量,stdio
|
|
12
|
+
# Hermes 等宿主出于安全过滤环境变量,stdio 子进程只继承白名单)。
|
|
13
|
+
# 这里是全仓唯一保留物理相对深度的位置:本文件会被当脚本直跑
|
|
14
|
+
# (python vod_mcp/server.py),此时包还不可导入,无法先 import paths。
|
|
15
|
+
# 其余所有资源路径一律走 vod_mcp/paths.py(含 VOD_DEVKIT_ROOT 覆盖)。
|
|
13
16
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
14
17
|
|
|
15
|
-
from vod_mcp import guardrails, packs, tools # noqa: E402
|
|
18
|
+
from vod_mcp import guardrails, packs, runtime_state, tools # noqa: E402
|
|
16
19
|
|
|
17
20
|
_PROTOCOL_VERSION = "2025-06-18"
|
|
18
21
|
_client_info = {}
|
|
@@ -103,13 +106,13 @@ def main():
|
|
|
103
106
|
|
|
104
107
|
# 启动自诊(排障:写 /tmp,宿主过滤环境变量时可观察实际 env)
|
|
105
108
|
try:
|
|
106
|
-
|
|
109
|
+
runtime_state._ensure_first_boot() # D-key-guard-3①:首启标记+构建机残留清理(preset 兜底)
|
|
107
110
|
except Exception:
|
|
108
111
|
pass
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
112
|
+
runtime_state._log(f"started pid={os.getpid()} argv={sys.argv} "
|
|
113
|
+
f"VOD_PACKS={os.environ.get('VOD_PACKS')!r} "
|
|
114
|
+
f"distribution={runtime_state._distribution()} "
|
|
115
|
+
f"cwd={os.getcwd()}")
|
|
113
116
|
if args.transport == "remote":
|
|
114
117
|
try:
|
|
115
118
|
from vod_mcp import transport_remote
|
|
@@ -14,9 +14,10 @@
|
|
|
14
14
|
(vod_list_skills / vod_retrieve_skill),裁剪时拒绝并给指引,不是文件可见性。
|
|
15
15
|
"""
|
|
16
16
|
import re
|
|
17
|
-
from pathlib import Path
|
|
18
17
|
|
|
19
|
-
|
|
18
|
+
from vod_mcp import paths
|
|
19
|
+
|
|
20
|
+
SKILLS_ROOT = paths.SKILLS
|
|
20
21
|
|
|
21
22
|
# 技能 → 归属 pack(core 常驻;knowledge 可裁剪)
|
|
22
23
|
SKILL_PACK = {
|
|
@@ -62,8 +63,8 @@ def _skill_dirs() -> list:
|
|
|
62
63
|
|
|
63
64
|
|
|
64
65
|
def _server_face() -> bool:
|
|
65
|
-
from vod_mcp import
|
|
66
|
-
return bool(
|
|
66
|
+
from vod_mcp import net
|
|
67
|
+
return bool(net._SERVER_FACE.get())
|
|
67
68
|
|
|
68
69
|
|
|
69
70
|
def list_skills(params: dict) -> dict:
|