Просмотр исходного кода

fix(workspace): native 车道按 provider 分流 — 切 qwen 退回 react 车道

切换 workspace.assistant 基座模型到 qwen(provider=dashscope)暴露路由缺口:native
车道判定原只看 agent_template、不看 provider → 会把 `--model qwen-max` 传给
`claude -p`(只认 sonnet/opus/haiku)→ 跑不起来。

修:`_is_native_workspace(agent, config)` —— 工作区模板**且** provider=claude-code
才走 native;qwen/ollama/openai 等退回 react 车道(text-provider+平台工具,对非
agentic-CLI 模型本来就是对的模式,且 react 那些坑是 claude-code 专属的)。两个
入口(run_agent / run_agent_stream)都改用此判定。回归 test_workspace_guard(6)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
kenny67nju 5 месяцев назад
Родитель
Сommit
21b2760cdf
2 измененных файлов с 31 добавлено и 3 удалено
  1. 16 3
      agentpaas/src/agentpaas/api/v1/agents.py
  2. 15 0
      tests/test_workspace_guard.py

+ 16 - 3
agentpaas/src/agentpaas/api/v1/agents.py

@@ -507,6 +507,17 @@ async def rollback_agent(
 _WORKSPACE_CONV_TEMPLATES = {"workspace.assistant"}
 
 
+def _is_native_workspace(agent, config) -> bool:
+    """是否走原生 claude-code 车道:工作区对话型模板 **且** provider=claude-code。
+    native 车道是 `claude -p`(只认 claude 自己的模型);切到 qwen/ollama/openai 等
+    必须退回 react 车道(text-provider+平台工具),否则会把 --model qwen-max 传给
+    claude-code、跑不起来。"""
+    if (agent.get("agent_template") or "") not in _WORKSPACE_CONV_TEMPLATES:
+        return False
+    provider = ((config or {}).get("model") or {}).get("provider") or ""
+    return provider == "claude-code"
+
+
 def _require_workspace_dir(agent, req):
     """workspace 对话型 agent 必须带有效 context.work_dir,否则 400 拒绝(不回退空家目录)。"""
     import os as _os
@@ -615,8 +626,10 @@ async def run_agent(
     _inplace_dir = _ctx_wd if (_ctx_wd and _os.path.isdir(_ctx_wd)) else ""
     if _inplace_dir:
         work_dir = source_dir = _inplace_dir
-    # 车道 B:工作区对话型模板走原生 claude-code(见 _execute_agent native 分支)。
-    _native_ws = (agent.get("agent_template") or "") in _WORKSPACE_CONV_TEMPLATES
+    # 车道 B:工作区对话型模板 **且 provider=claude-code** 才走原生车道。native 车道是
+    # `claude -p`(只认 sonnet/opus/haiku);切成 qwen/ollama 等必须退回 react 车道
+    # (text-provider+平台工具,对非 agentic-CLI 模型本来就是对的模式)。
+    _native_ws = _is_native_workspace(agent, config)
 
     # 注入顺序(MEMORY_DESIGN §2.2):本会话前情 → 相关历史(L3) → KB → 用户问题
     enriched_input = req.input
@@ -780,7 +793,7 @@ async def run_agent_stream(
     _inplace_dir = _ctx_wd if (_ctx_wd and _os.path.isdir(_ctx_wd)) else ""
     if _inplace_dir:
         work_dir = source_dir = _inplace_dir
-    _native_ws = (agent.get("agent_template") or "") in _WORKSPACE_CONV_TEMPLATES
+    _native_ws = _is_native_workspace(agent, config)
 
     # 会话记忆 P1:thread 解析(run 记录在下方流式分支创建,这里先拿 id)
     from agentpaas.engine import thread_memory as _tm

+ 15 - 0
tests/test_workspace_guard.py

@@ -47,3 +47,18 @@ def test_non_workspace_agent_unaffected():
     agent = {"agent_template": "research.top-journal-reviewer"}
     _require_workspace_dir(agent, _Req({}))  # 不抛
     _require_workspace_dir({"agent_template": ""}, _Req({}))  # 空模板也不抛
+
+
+from agentpaas.api.v1.agents import _is_native_workspace
+
+
+def test_native_lane_only_for_claude_code_provider():
+    ws = {"agent_template": "workspace.assistant"}
+    assert _is_native_workspace(ws, {"model": {"provider": "claude-code"}}) is True
+    # 切到 qwen/ollama/openai → 退回 react 车道(native 跑不了非 claude 模型)
+    assert _is_native_workspace(ws, {"model": {"provider": "dashscope", "name": "qwen-max"}}) is False
+    assert _is_native_workspace(ws, {"model": {"provider": "ollama"}}) is False
+    assert _is_native_workspace(ws, {"model": {}}) is False
+    # 非工作区模板:永不走 native
+    assert _is_native_workspace({"agent_template": "research.x"},
+                                {"model": {"provider": "claude-code"}}) is False