Skip to content

Hub 服务层接入 worker 模型发现(移植 #38) - #45

Open
Fei2411 wants to merge 9 commits into
mainfrom
devin/1795442852-hub-list-models
Open

Fei2411 wants to merge 9 commits into
mainfrom
devin/1795442852-hub-list-models

Conversation

@Fei2411

@Fei2411 Fei2411 commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

把 #38(作者 @edgexyz)的按需模型发现移植到 Hub 重构(#42,已合并到 main)上,并改走 Hub 服务层。原 #38 分支未改动。

提交结构

  • 7303c68:Add on-demand worker model discovery through MCP #38 内容 squash 合入,作者保留为 edge.xyz;解决了 config.py(hub + models 两个配置节并存)、SKILL.md、test_server_tools.py 的冲突。
  • f426c30:Hub 接入。
  • 8ea094f:模型发现进行中不触发空闲退出(审查发现)。
  • 78281a9:探测会话使用与派发相同的 session/new 元数据,列出的模型遵守配置的限制(审查发现)。
  • 5c23f74:补充 worker token 访问模型发现路由返回 403 的测试。
  • 其余为同步 devin/hub-api / main 的合并提交;base 已改为 main。

Hub 接入

MCP list_models  ->  _service(ctx).list_models(...)       # 之前直接调 _registry(ctx),Hub 模式下会绕过 Hub
BridgeService.list_models(agent, cwd, refresh) -> {"ok": True, **registry.list_models(...)}
ROUTES['list_models'] = ('GET', '/v1/agents/{agent}/models', 'agent')   # ?cwd=/abs/project&refresh=true|false
  • 有 Hub 运行时,所有协调器共享 Hub 进程里的一份按 worker/项目缓存(默认 1 小时),不再每个 MCP 进程各探测一次。
  • refresh 和 active_only 一样严格解析 true|false,其他值返回 400。

审查修复

Registry._model_discoveries += 1 / finally -= 1; touch_activity()
idle_exit_due(): 有进行中的模型发现时返回 False

# 之前:new_session(cwd, mcp_servers=[]),不带 session_meta
created = await self._call_new_session(conn, cwd, self._new_session_meta(Session(...无 model/effort)))
  • 与 Add on-demand worker model discovery through MCP #38 不同:探测会话现在带上 session_meta(与不指定模型的派发完全一致),worker 按同样配置(如 Claude 模型白名单)给出模型列表,列出的模型都能派发。代价是 session_meta 里固定的 modelId 会成为探测报告的 default_model,这与派发实际默认值一致。

安全边界(仅本机、仅管理员)

  • 只挂在 loopback 管理端 API 上,需要管理员 bearer token;worker token 不在允许列表里,访问返回 403。
  • 远程网关 remote.py 未添加该路由(测试断言返回 404)。
  • 继续保留 Add on-demand worker model discovery through MCP #38 的限制:仅 coordinator.dispatch_enabled=true 可用、cwd 必须是已存在的绝对路径、超时、诊断信息脱敏。

测试

  • 新增 test_list_models_route(含 401/403/400 用例)、test_list_models_unsupported;test_remote.py 增加远程网关 404 用例。
  • 新增 test_idle_exit_waits_for_inflight_discovery、test_claude_discovery_honors_session_meta_model_allowlist、test_discovery_meta_matches_default_dispatch。
  • 以 main 为 base 时 CI 四个平台全部通过;本地 pytest、ruff、mypy src 通过。
  • 未用真实 worker CLI 做端到端探测。

文档:HUB.md、HUB.zh-CN.md、SETUP.md 补充了路由说明。


AB Product Engineer opened this PR for Stull
Implemented in session Port worker model discovery onto hub
Review with Devin · Open in Devin Desktop


edgexyz and others added 2 commits November 23, 2026 14:07
Squash of PR #38 (feat/list-models) by edge.xyz onto devin/hub-api.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…nly)

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
devin-ai-integration[bot]

This comment was marked as resolved.

… honor configured restrictions

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

Fei2411 and others added 5 commits November 23, 2026 14:39
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration
devin-ai-integration Bot changed the base branch from devin/hub-api to main November 24, 2026 07:10

@klarkxy klarkxy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed via Gemini App · Model: Gemini · Commit: 6ea25c9d98ecf8e45308a01ed510d162ef857b36

整体评估

本 PR 将 #38 的按需 Worker 模型目录发现(list_models)完整移植到中心 Hub 服务层(GET /v1/agents/{agent}/models),实现了多协调器共享 TTL 缓存、在途探测防空闲自退(_model_discoveries)、与真实派发一致的 session_meta 白名单继承,以及严格的管理端权限边界(拒绝 abw_* Worker Token、Dashboard Cookie 与 remote.py 网关),4 平台 CI 全绿。审查发现 3 处协议解析、工作区副作用与 URL 参数编码缺陷,建议修复后再合入。

1. 建议修复(逻辑缺陷 / 潜在风险)

  1. _acp_model_catalog 漏判无 category 的 id="effort" 选项,且未过滤原生 DSH 的空字符串默认项 ""

    • 位置:src/agent_bridge/adapters/acp.py 第 723–731 行(_acp_model_catalog)
    • 触发条件与影响:
      • 漏判 Claude Code 推理档位:当前条件 option.category == "thought_level" or option.id in {"thinking", "reasoning_effort"} 未包含 "effort" 与 "reasoning"。对照本仓库真实夹具 tests/test_claude_acp.py 及同文件 _cursor_effort_option_ids,claude-agent-acp 返回的档位选项为 id="effort" 且不带 category,真实运行时会导致 supported_efforts 静默丢失为 None;
      • 暴露非法空字符串档位 "":原生 dsh --profile acp(见 tests/test_dsh_acp.py)的 reasoning_effort.options 首项为 {"value": "", "name": "Provider default"},当前未过滤空串,导致 list_models 返回 ["", "low", "high", "max"],协调器若原样回传 effort="" 会被 validate_selection 抛 ValueError 拒绝。
    • 修改建议:将 option.id 匹配集合对齐为 {"thinking", "effort", "reasoning", "reasoning_effort"},并在提取候选值时过滤空字符串(cleaned = [v for v in offered if v]),补充对应单测。
  2. 旧版 dsh-acp-demo 执行只读 list_models 探测时未隔离 DSH_SNAPSHOT_SESSIONS_ROOT,会在用户项目目录下创建 ./.sessions

    • 位置:src/agent_bridge/adapters/acp.py 第 782–783、797–809 行(AcpAdapter.list_models)
    • 触发条件与影响:常规派发在 _spawn 中传入了 session_id 以将 dsh-acp.cordis.yml 的 persistenceRoot 重定向至 $AGENT_BRIDGE_HOME/dsh-sessions/<session_id>;而 list_models 调用 prepare_dsh_launch(cmd, env) 时未传 session_id 且在用户 cwd 下执行 session/new,导致标注为只读的模型发现操作在用户项目工作区写入 .sessions/ 目录。
    • 修改建议:对 self.agent.name == "dsh" 且非原生 --profile acp 的探测子进程显式注入指向 self.home 下临时目录的 DSH_SNAPSHOT_SESSIONS_ROOT,并在 _cleanup_discovery 中清理。
  3. HubClient._call 拼接路径参数未做 URL 编码,特殊字符 agent 名可引发格式化异常或路径偏移

    • 位置:src/agent_bridge/hub_client.py 第 27、65–66 行(ROUTES['list_models'] 与 HubClient._call)
    • 触发条件与影响:path = path.format(**{key: kwargs.pop(key)}) 直接拼接原始字符串;当 agent 含 {/} 时会在客户端抛 KeyError/ValueError,含 /、..、?、# 时则会偏离 /v1/agents/{agent}/models 路由或污染查询串。
    • 修改建议:校验路径参数非空且不为 ./.. 后,使用 urllib.parse.quote(str(val), safe="") 替换路径占位符。

2. 建议改进(边界条件 / 健壮性)

  • Registry.stop() 主动取消并等待在途探测任务(src/agent_bridge/registry.py 第 186、556–600、653–682 行):建议将 _model_discoveries 从整型计数器改为 set[asyncio.Task],在 stop() 中取消并等待在途探测子进程回收完毕,避免进程退出时残留未收尾子进程。
  • scripts/smoke_models.py 沙箱环境变量隔离(第 45–50 行):建议在 StdioServerParameters.env 中显式设置 "AGENT_BRIDGE_HUB": "off" 并清空 "AGENT_BRIDGE_HUB_URL",防止宿主环境变量导致冒烟脚本误连外部运行中的 Hub。
  • _acp_model_catalog 字典兼容与未枚举 default_model 处理(adapters/acp.py 第 711–721 行):建议统一通过 _dump(option) 访问字段,并对不在候选 models 列表中的占位默认值(如 "default")做置空或告警提示。

3. 小细节(Nits)

  • 文档与诊断细节:README.md 中文工具表漏列 list_models;codex_app_server_list_models(model_discovery_exec.py 第 129 行)建议保留脱敏后的 stderr 尾部以便排查 -c 配置错误;Hub 路由缺省 cwd 查询参数时可前置校验以返回更清晰的 400 提示。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants