Skip to content

fix: resolve generated media through workspace for IM delivery - #2377

Closed
henryZhouLikeStudy wants to merge 3 commits into
TencentCloud:mainfrom
henryZhouLikeStudy:hotfix/2351-generated-media
Closed

henryZhouLikeStudy wants to merge 3 commits into
TencentCloud:mainfrom
henryZhouLikeStudy:hotfix/2351-generated-media

Conversation

@henryZhouLikeStudy

Copy link
Copy Markdown

Summary

Fixes image/video ToolResults from the built-in media-generation tools when they are delivered through IM channels such as Weixin/iLink.

generate_image returns both a workspace-relative path and a host-side localPath. The projection previously treated the relative path as a URL, causing InvalidUrlClientError and a user-visible fallback such as:

[Image: generated/images/<file>.png]

This change resolves relative and host-absolute workspace paths through BackendWorkspace, sends the bytes as base64 ImageContent, preserves existing base64/file:///HTTP(S)/data URL support, and rejects unsupported URL schemes.

When media cannot be read, the projection now logs the server-side diagnostic and emits a localized error instead of exposing the internal path. The locale is propagated through both normal and HITL-resumed streams.

Scope

  • tool_media.py: workspace path resolution and safe URL handling
  • stream_project.py: pass the resolved user locale to media projection
  • en.json / zh.json: localized media delivery failure message
  • Focused Gateway and stream-projection regression tests
  • CHANGELOG.md

No changes are required in octop-gateway or octop-harness.

Validation

Regression tests

  • uv run --frozen pytest tests/unit/gateway/test_tool_media.py tests/unit/gateway/test_stream_project_tools.py tests/unit/gateway/test_message_build.py tests/unit/i18n -q
    • 151 passed
  • uv run --frozen pytest tests/unit/gateway -q
    • 541 passed, 2 skipped
  • uv run --frozen ruff check src tests
    • passed
  • uv run --frozen ruff format --check src tests
    • 1240 files already formatted
  • uv run --frozen mypy src/octop
    • Success: no issues found in 597 source files

The full non-live suite completed with 4846 passed, 226 skipped. Its 12 failures are pre-existing Bash/WSL tests that cannot launch C:\Windows\System32\bash.exe on this native Windows host; none touch media projection or its dependencies. Linux/Windows CI remains authoritative.

Cloud staging

The patched modules compiled against the production octop-harness 1.0.4 / octop-gateway 1.0.3 environment. A deterministic staging check exercised:

  • a generate_image envelope with a relative workspace path and stale localPath;
  • a UTF-8 workspace path containing Chinese characters and a space;
  • conversion into base64 ImageContent with no URL;
  • a missing-file case that emitted only 无法发送媒体文件,请稍后重试。 without exposing the path.

Live iLink verification

The hotfix was deployed to the self-hosted production Octop instance after backing up the four changed files.

The live test constructed a real generate_image ToolResult envelope, resolved the image from the workspace, and sent the resulting ImageContent through the configured Weixin/iLink bot. The outbound operation returned successful delivery, the ImageContent had no URL, and no InvalidUrlClientError or media-load failure occurred.

The production health endpoint reports ok=true, db=true, and all five agents running.

Compatibility and safety

  • Workspace-relative paths remain the source of truth; host-absolute localPath is not sent to users.
  • Base64, file://, HTTP(S), and data-URL inputs retain their previous behavior.
  • FTP and other unsupported schemes are rejected.
  • Failed media delivery does not emit the internal workspace path.
  • The server backup is retained locally for rollback until the change is accepted upstream.

Checklist

  • Added focused regression tests
  • Cloud staging validation passed
  • Live Weixin/iLink delivery verified
  • Production health verified
  • No credentials, user identifiers, chat content, or generated user images included
  • CHANGELOG.md updated

Closes #2351

@henryZhouLikeStudy

Copy link
Copy Markdown
Author

CI workflows are currently action_required because this fork PR requires maintainer approval before jobs run: CodeQL run 40051628949 and CI run 40051629002. Cloud staging validation, production deployment, and the live Weixin/iLink media send were completed successfully.

@henryZhouLikeStudy

Copy link
Copy Markdown
Author

补充一个同一轮内的重复媒体问题,已包含在 head ebe3a4cda4daed13ccb1491ba45940f43fbbb726。

脱敏后的云环境证据:

  • 微信在最终文字之前先后收到两条相同的 ImageContent
  • 两次发送相隔约 74 秒
  • 同一张图片先由 generate_image 返回,随后又由 send_file_to_user 返回
  • 两个工具结果的 tool_call_id 不同,但文件名和路径哈希相同

根因是当前 IM 媒体去重只记录 tool_call_id,而 generate_image 与 send_file_to_user 会被视为两次独立交付。

修复方式:

  • 对解析后的实际媒体字节生成稳定 hash
  • 在每个 IM stream 内维护 emitted_media_keys
  • 即使工具名和 tool_call_id 不同,同一文件内容也只发送一次
  • 新生成的其他文件不受影响

验证:

  • tests/unit/gateway/test_stream_project_tools.py
  • tests/unit/gateway/test_tool_media.py
  • 合计 61 passed
  • Ruff、格式检查和 mypy 通过

这与 #1876 描述的跨 turn 历史重放去重是两个边界;本补丁解决同一轮内多个交付工具重复发送同一文件的问题。日志和回归测试均已脱敏。

@henryZhouLikeStudy

Copy link
Copy Markdown
Author

已在自有云环境完成 ebe3a4cda4daed13ccb1491ba45940f43fbbb726 中重复媒体修复的热部署和运行验证。

部署前确认:

  • 没有正在执行的工具、cron 或微信回复
  • 最后一条真实线程消息约 15 分钟前

部署与验证:

  • 两个生产文件先在 staging 完成语法、导入和 SHA256 校验
  • 替换前保存原文件及旧 SHA256
  • Octop 重启后 3 秒恢复健康,数据库正常,5 个 Agent 全部运行
  • 持续观察 20 秒:无 warning、无微信通道错误
  • 服务内存由部署前约 1.9 GiB 降至约 380 MiB
  • 生产文件已包含 media_content_dedup_key 和 emitted_media_keys

生产哈希:

9e203639524c03f96179f025d08606411909ae7af786c387097900cca5d628aa  tool_media.py
36466e226e7b1e5d0a318d1ab79f1c4e7c403313aeb41ef0a8d3753141369ced  stream_project.py

回滚备份已保留在自托管环境,不包含服务器地址、账号、聊天内容或凭据。

@jubaoliang

Copy link
Copy Markdown
Collaborator

请把这个变更重新提交到 develop。功能修改和问题修复都要指向 develop,不要直接指向 main。

Please open this change against develop instead of main.

@henryZhouLikeStudy

Copy link
Copy Markdown
Author

Superseded by #2683, which rebases this fix onto \develop\ as requested (preserves workspace guards and current-turn filtering, adds localized errors and IM attachment deduplication). This PR is now closed in favor of the new one.

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.

[bug] generate_image 的微信媒体相对路径被当作 URL,失败后发送 [Image: path]

2 participants