JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix(mcp): 首个位置参数为唯一 entry 候选,解析失败拒绝缓存(!9045 CR-1)
huxiaokaie30686130ebf
diff --git a/jiuwenswarm/common/mcp_config.py b/jiuwenswarm/common/mcp_config.pyindex f5fe85660a..15c562b20c 100644--- a/jiuwenswarm/common/mcp_config.py+++ b/jiuwenswarm/common/mcp_config.py@@ -939,6 +939,7 @@ def _office_claw_mcp_build_fingerprint(args = [str(value or "").strip() for value in params.get("args") or []]entry_scanned = False+ saw_positional = Falseindex = 0while index < len(args):raw = args[index]@@ -966,26 +967,37 @@ def _office_claw_mcp_build_fingerprint(_stat_once(resolved)continueresolved = _resolve(raw)- if resolved is None or not resolved.is_file():+ resolves_to_file = resolved is not None and resolved.is_file()+ if not saw_positional:+ # 首个位置参数(node 入口)是唯一的 entry 候选+ saw_positional = True+ if resolves_to_file:+ entry_scanned = True+ parent_key = _normalized_path(str(resolved.parent))+ if parent_key in expanded_dirs:+ _stat_once(resolved)+ else:+ expanded_dirs.add(parent_key)+ tree = _fingerprint_bundle_tree(resolved.parent)+ if tree is None:+ return None+ for entry in tree:+ if entry["path"] in seen_paths:+ continue+ seen_paths.add(entry["path"])+ fingerprints.append(entry)+ # 首个位置参数解析失败(相对路径漂移/CLI 占位符):entry 缺失,+ # 循环结束后拒绝缓存——后续位置参数(脚本参数)不顶替 entry,+ # 其父目录树不得进入指纹顶缺(见 !9045 CR-1)。continue- if entry_scanned:+ if resolves_to_file:# 入口之后的位置参数是传给脚本的参数,单文件指纹_stat_once(resolved)- continue- entry_scanned = True- parent_key = _normalized_path(str(resolved.parent))- if parent_key in expanded_dirs:- _stat_once(resolved)- continue- expanded_dirs.add(parent_key)- tree = _fingerprint_bundle_tree(resolved.parent)- if tree is None:- return None- for entry in tree:- if entry["path"] in seen_paths:- continue- seen_paths.add(entry["path"])- fingerprints.append(entry)+ if saw_positional and not entry_scanned:+ # 首个位置参数未解析成文件 → 真实 bundle 目录不在指纹内。基于不完整+ # 指纹的键会让 bundle 变化永不轮换(永久 stale 且无日志),此处直接+ # 返回 None 拒绝缓存,不依赖 command 单文件指纹形成键。+ return Nonereturn sorted(fingerprints, key=lambda entry: entry["path"])@@ -995,8 +1007,9 @@ def _office_claw_mcp_schema_cache_key(params: Mapping[str, Any]) -> str | None:)build_files = _office_claw_mcp_build_fingerprint(params)if not build_files:- # No local bundle files (remote-style config) or an oversized bundle- # tree: the key could never rotate on a change, so refuse to cache.+ # No local bundle files (remote-style config), an unresolved entry+ # positional, or an oversized bundle tree: the key could never+ # rotate on a change, so refuse to cache.return Nonepayload = {"command": _normalized_path(str(params.get("command") or "")),diff --git a/tests/unit_tests/agentserver/test_mcp_schema_cache.py b/tests/unit_tests/agentserver/test_mcp_schema_cache.pyindex 1079fbd817..3576531c39 100644--- a/tests/unit_tests/agentserver/test_mcp_schema_cache.py+++ b/tests/unit_tests/agentserver/test_mcp_schema_cache.py@@ -428,3 +428,41 @@ async def test_require_import_aux_scripts_do_not_disable_cache((dist / "server-toolsets.js").write_text("toolset v2-with-more-bytes")await mcp_config.list_office_claw_mcp_tools(params)assert calls == 3, "entry tree change must rotate the key"+++async def test_unresolved_entry_positional_refuses_cache(+ tmp_path,+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """A first positional that fails to resolve must refuse the cache (CR-1).++ If the entry cannot be resolved (relative path with cwd drift, or a CLI+ placeholder), the real bundle directory would never enter the+ fingerprint, and a later positional that happens to resolve (a script+ config file) must not be mistaken for the entry — its parent tree+ would mask the missing bundle tree and the key would never rotate on a+ real bundle rebuild. Refuse to cache instead of forming a key from an+ incomplete fingerprint.+ """+ calls = 0++ async def _discover(params):+ nonlocal calls+ calls += 1+ return [_make_tool("office_claw_post_message")]++ monkeypatch.setattr(mcp_config, "_list_office_claw_mcp_tools_uncached", _discover)++ config = tmp_path / "config.json"+ config.write_text("{}")+ params = {+ "command": "node",+ "args": ["./no-such-entry.js", str(config)],+ "cwd": str(tmp_path),+ "env": {},+ }+ await mcp_config.list_office_claw_mcp_tools(params)+ await mcp_config.list_office_claw_mcp_tools(params)+ assert calls == 2, "unresolved entry positional must refuse the cache"+ assert len(mcp_config._office_claw_mcp_schema_cache) == 0