{"record":{"id":"2fbacdccac410d21","repo":"HKUDS/Vibe-Trading","slug":"media-file-not-found-media-path","errorCode":null,"errorMessage":"Media file not found: {media_path}","messagePattern":"Media file not found: (.+?)","errorType":"exception","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"agent/src/channels/weixin.py","lineNumber":1342,"sourceCode":"\n    async def _send_media_file(\n        self,\n        to_user_id: str,\n        media_path: str,\n        context_token: str,\n    ) -> None:\n        \"\"\"Upload a local file to WeChat CDN and send it as a media message.\n\n        Follows the exact protocol from ``@tencent-weixin/openclaw-weixin`` v1.0.3:\n        1. Generate a random 16-byte AES key (client-side).\n        2. Call ``getuploadurl`` with file metadata + hex-encoded AES key.\n        3. AES-128-ECB encrypt the file and POST to CDN (``{cdnBaseUrl}/upload``).\n        4. Read ``x-encrypted-param`` header from CDN response as the download param.\n        5. Send a ``sendmessage`` with the appropriate media item referencing the upload.\n        \"\"\"\n        p = Path(media_path)\n        if not p.is_file():\n            raise FileNotFoundError(f\"Media file not found: {media_path}\")\n\n        raw_data = p.read_bytes()\n        raw_size = len(raw_data)\n        raw_md5 = hashlib.md5(raw_data).hexdigest()\n\n        # Determine upload media type from extension\n        ext = p.suffix.lower()\n        if ext in _IMAGE_EXTS:\n            upload_type = UPLOAD_MEDIA_IMAGE\n            item_type = ITEM_IMAGE\n            item_key = \"image_item\"\n        elif ext in _VIDEO_EXTS:\n            upload_type = UPLOAD_MEDIA_VIDEO\n            item_type = ITEM_VIDEO\n            item_key = \"video_item\"\n        elif ext in _VOICE_EXTS:\n            upload_type = UPLOAD_MEDIA_VOICE\n            item_type = ITEM_VOICE","sourceCodeStart":1324,"sourceCodeEnd":1360,"githubUrl":"https://github.com/HKUDS/Vibe-Trading/blob/80ffdda44c5c4db0dd84d70e051cca591cea67df/agent/src/channels/weixin.py#L1324-L1360","documentation":"_send_media_file verifies the media path with Path.is_file() before upload and raises FileNotFoundError if it does not exist. The media pipeline (AES-encrypt + CDN upload) requires the raw bytes, so a missing file fails fast before any network calls.","triggerScenarios":"Calling send() with a media attachment whose media_path does not exist or is a directory — e.g. temp file already cleaned up, wrong path, file deleted between generation and send.","commonSituations":"Tempfile garbage collection (cleanup handlers, NamedTemporaryFile context exit) deleting the media before send; agent-generated artifacts written to a different working directory; path built with a stale filename; container filesystem where the mount is missing.","solutions":["Verify the file exists (and capture its bytes) before queuing the send.","Generate media into a stable directory you control and pass an absolute path.","Keep the temp file alive until the send completes (close only after awaiting send).","Check mounts/permissions if running in a container."],"exampleFix":"// before\nawait channel.send_media(chat_id, media_path=tmp_path)\n\n# after\np = Path(tmp_path).resolve()\nif not p.is_file():\n    raise FileNotFoundError(tmp_path)\nawait channel.send_media(chat_id, str(p))","handlingStrategy":"validation","validationCode":"from pathlib import Path\n\ndef media_ok(path: str) -> bool:\n    p = Path(path)\n    return p.is_file() and p.stat().st_size > 0","typeGuard":"from pathlib import Path\n\ndef is_sendable_media(path: str) -> bool:\n    p = Path(path)\n    return p.is_file() and not p.is_dir()","tryCatchPattern":"try:\n    await channel.send_media(chat_id, path)\nexcept FileNotFoundError as e:\n    if \"Media file not found\" in str(e):\n        regenerate_or_skip(path)\n    else:\n        raise","preventionTips":["Use absolute paths for generated media.","Keep temp files alive until send completes.","Validate existence right before the send call."],"tags":["wechat","media-upload","file-not-found","filesystem"],"backgroundTag":"file-not-found","analyzedSha":"80ffdda44c5c4db0dd84d70e051cca591cea67df","analyzedAt":"2026-08-28T12:46:38.989Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}