Skip to content

Add native search and media capability tools - #39

Open
klarkxy wants to merge 1 commit into
Fei-Craft:mainfrom
klarkxy:codex/native-capabilities-upstream
Open

klarkxy wants to merge 1 commit into
Fei-Craft:mainfrom
klarkxy:codex/native-capabilities-upstream

Conversation

@klarkxy

@klarkxy klarkxy commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

Adds call_capability, get_capability_result, and cancel_capability for native search and media operations through existing local worker CLIs. Callers receive native sources, verified workspace-relative output files, and explicit job/durability status without managing worker sessions.

Supported integrations:

  • Codex: search and images, including fresh app-server image turns.
  • Grok: search, images, and video.
  • MiniMax: search.

The integration validates reference images and delivered files, persists native evidence, deduplicates retained requests across restarts, and supports explicit continuation of successful compatible calls. Native calls require durable state before provider execution. Results remain pending through worker cleanup; unconfirmed process exits retain worker/PID evidence and block continuation. No automatic generation resubmission occurs.

Scope is limited to search/media tools and their supporting adapters, result handling, documentation, and tests. This branch is based directly on upstream 5560e06; it does not include fork TaskNode metadata, orchestration rules, configuration defaults, diagnostic schemas, or private acceptance logs. Existing upstream request fingerprints, async result/persistence contracts, permissions, environment filtering, and ordinary dispatch behavior are preserved.

Validation on Windows / CPython 3.12.11:

  • Final complete offline suite: 864 passed, 4 skipped, 0 failures (170.10 seconds). Skips are Windows symbolic-link creation privileges; the real directory-junction regression passed.
  • Independent review accepted the integration and subsequent fixes; the three new Codex source/cleanup regressions failed before repair and pass afterward.
  • Repository-wide Ruff, mypy over 44 source files, and staged whitespace checks passed.
  • Offline wheel build passed; all 44 packaged Python modules matched the final source.
  • An isolated MCP stdio instance exposed all three tools, required the initial request UUID, rejected unsupported combinations before creating any session/task, and exited cleanly on host EOF.

An earlier full run encountered a transient Windows file-replacement denial in an unchanged upstream configuration test. All 28 configuration tests passed independently, followed by the clean full run reported above; configuration/persistence source files were not changed for that failure.

This verification made no real provider calls and did not upgrade a shared installation. Native operation counts are observed after execution rather than enforced as a provider-side billing/execution budget. Account availability and broader live-provider acceptance remain environment-specific.

@klarkxy klarkxy left a comment

Copy link
Copy Markdown
Collaborator Author

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: e94164bf578aedd913c55327bc239c291b590a99

整体评估

本 PR 围绕本地 CLI 原生搜索与媒体生成(Codex / Grok / MiniMax 搜索、Codex app-server 生图、Grok 生图与视频)构建了完整的防重放扣费状态机、O_NOFOLLOW + 容器头尾安全交付管线与离线故障注入测试。由于 main 分支已合入中心 Hub 重构 #42(20a0882a),且关联 PR #47 已将本 PR 的原生搜索部分迁移适配至 Hub 架构,当前分支处于结构性冲突状态(mergeable_state: "dirty")。建议不直接对 #39 强行解冲突合入,而是在 #47 合入后,基于 Hub 鉴权框架单独拆分迁移本 PR 独有的原生图片/视频媒体能力,并在迁移时一并修复以下查出的逻辑缺陷。

1. 建议修复(逻辑缺陷 / 契约违背)

  1. Grok/MiniMax(AcpAdapter)与 Codex 搜索在子进程退出未确认时静默清除 PID 并放行续接,违背退出保护契约

    • 位置:src/agent_bridge/adapters/acp.py:1861-1879、src/agent_bridge/adapters/codex.py:283-309、src/agent_bridge/registry.py:1189-1208
    • 触发场景:processes.py 的 reap_subprocess 在杀进程超时后内部吞掉 TimeoutError 返回,此时 proc.returncode 仍为 None。但 AcpAdapter.shutdown 与 CodexAdapter(非生图分支)未检查 returncode is None,无条件调用 drop_pid、清空 session.pid 并返回正常结束,导致 Registry._run_task 误判 pending=False 并允许 previous_call_id 并发恢复仍在后台残留的会话。
    • 具体修改建议:与 codex_image.py:340-342 对齐,当内部能力会话子进程 returncode is None 时保留 pids.json 与存活句柄并抛出未确认退出异常,维持 pending=True 阻断续接。
  2. 取消挂起的 Codex 生图任务时,Registry.cancel_task 因未捕获前置 adapter.cancel 抛错而短路跳过状态收敛

    • 位置:src/agent_bridge/registry.py:1747-1754、src/agent_bridge/codex_image.py:312-318, 352-364
    • 触发场景:当 codex app-server 未在 2 秒内退出时,image.cancel() 直接抛出 RuntimeError,导致 Registry.cancel_task 在执行 bg.cancel() 前异常中断(后台任务保持 running);且 CodexImageTurn.run 的 except asyncio.CancelledError: 中 await self.cancel() 抛错也会跳过后续 finish_job(..., status="uncertain")。
    • 具体修改建议:在 Registry.cancel_task 中用 try/except 包裹 await adapter.cancel(session) 确保继续执行 bg.cancel();在 CodexImageTurn.run 的取消分支中用 try/finally 保证 job 先收敛落盘为 "uncertain"。
  3. Codex 搜索事件(codex exec --json)动作分类缺陷导致无 action 字段的 web_search 误判为 web_fetch 或触发工具名变更报错

    • 位置:src/agent_bridge/adapters/codex.py:188-194、src/agent_bridge/native_results.py:28-30、src/agent_bridge/capabilities.py:224-228
    • 触发场景:当 item.completed 收到不含嵌套 action 字典的 {"type": "web_search", ...} 事件时,codex.py:193 将其归类为 "web_fetch",导致 validate_capability_output 因缺少 "web_search" 将成功搜索判败;若 item.started 先带空 action: {}(记为 "web_fetch")而 item.completed 带 action: {"type": "search"},observe_call 会抛出 ValueError("native capability tool identity changed")。
    • 具体修改建议:仅在动作类型已确定或 item.completed 时记录调用,当 action is None 时对 web_search 事件默认归为 "web_search",仅对明确的 open_page/find_in_page 归为 "web_fetch"。
  4. checked_path 向上遍历越过受控根目录,在 macOS /private/tmp 软链或 Windows 8.3 短路径/挂载点下导致计费生成后交付失败

    • 位置:src/agent_bridge/output_files.py:21-39、src/agent_bridge/codex_image.py:182-185, 245-262、src/agent_bridge/acp_results.py:62-69, 195-196
    • 触发场景:checked_path 对 [absolute, *absolute.parents] 一路检查到文件系统根,只要 $HOME、/tmp(macOS /tmp -> /private/tmp)或上层挂载卷含系统软链/0x400 重解析点即无条件拒绝;且 CodexImageTurn 校验时用了 .resolve()、产物比对却用未解析的 .absolute()(Windows RUNNER~1 短路径必不相等),AcpNativeResults.__init__ 捕获前置目录异常仅置 setup_error = True 而不阻断远端扣费请求发送。
    • 具体修改建议:先将受控基准根 boundary = root.resolve() 规范化,仅对 boundary 内部相对子路径拦截软链与重解析点;CodexImageTurn 统一基于规范化路径比对;AcpNativeResults 对目录配置错误在发送 session/prompt 前直接报错中止。

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

  1. 避免 _validate_native_files 在只读查询路径无条件写盘并冲掉 task.checkpoint_error(src/agent_bridge/registry.py:1060-1064, 1730-1731、src/agent_bridge/capabilities.py:151-156):仅在确实剔除了失效文件且 not task.checkpoint_error 时尝试写盘并捕获 OSError,防止 get_capability_result 因磁盘满报错崩溃。
  2. 封堵 ACP 异步通知回调中 64/256 工具调用上限后的违规工具漏检(src/agent_bridge/acp_results.py:94-107、src/agent_bridge/native_results.py:32-34):超出上限或检测到禁用工具时在 Task 上设置显式违规标志并取消当前 prompt_task。
  3. 修正 _lexical_reference 中 normpath 前置导致的 .. 穿越段折叠放行(src/agent_bridge/registry.py:613-652):在执行 os.path.normpath 之前先检查原始路径分段是否包含 ".." 并对齐请求指纹去重规则。

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.

1 participant