== 取证时间: 2026-09-29 01:30:46 CST == == 目标 == 现役网关 ExecStart=/usr/local/lib/hermes-agent/.hermes/bin/hermes gateway run -- 是否 git 仓库/版本 -- 4b05f764fa M plugins/platforms/dingtalk/adapter.py ?? .playwright-mcp/ ?? hermes_cli/.web_dist-build-ZVb2Bm/ ?? plugins/platforms/dingtalk/adapter.py.bak-20260929-012606 -- 文件 -- -rw-r--r-- 1 root root 5044 Sep 29 01:29 /root/.hermes/patches/dingtalk-robot-fallback.patch -rw-r--r-- 1 root root 55369 Sep 29 01:26 /usr/local/lib/hermes-agent/plugins/platforms/dingtalk/adapter.py -rw-r--r-- 1 root root 53234 Sep 27 15:52 /usr/local/lib/hermes-agent/plugins/platforms/dingtalk/adapter.py.bak-20260929-012606 sha256 现文件: 73aaede081e81df15eb430e4775f32261be0176bf23d468f50cbb9c7a2743212 sha256 原文件: 8acb444412e8c833d33fe97ca5a98f0a2be3eaf6ca524eb41a32a935b4bfa46a sha256 补丁 : d0db676125c59957330ece26c4280b0e669db2780585bec0e34063653f672069 -- diff 统计 -- 32 (新增行数, 含空) -- 完整 diff -- --- /usr/local/lib/hermes-agent/plugins/platforms/dingtalk/adapter.py.bak-20260929-012606 2026-09-27 15:52:06.939908169 +0800 +++ /usr/local/lib/hermes-agent/plugins/platforms/dingtalk/adapter.py 2026-09-29 01:26:25.011350215 +0800 @@ -503,16 +503,19 @@ metadata = metadata or {} logger.debug("[%s] send() chat_id=%s card_enabled=%s", self.name, chat_id, bool(self._card_template_id and self._card_sdk)) session_webhook = metadata.get("session_webhook") or (self._get_valid_webhook(chat_id) or ("",))[0] + is_final_reply = reply_to is not None if not session_webhook: - logger.warning("[%s] No valid session_webhook for chat_id=%s", self.name, chat_id) - return SendResult(success=False, error="No valid session_webhook available. Reply must follow an incoming message.") + # No live webhook (fresh gateway restart, or the webhook expired): the reply would be + # silently dropped. Fall back to the robot oToMessages API so the chat still gets output. + logger.warning("[%s] No valid session_webhook for chat_id=%s -> robot fallback", self.name, chat_id) + return await self._send_via_robot_fallback(chat_id, content, is_final_reply) if not self._http_client: return SendResult(success=False, error="HTTP client not initialized") current_message = self._message_contexts.get(chat_id) # ``reply_to`` is only set by base.py:_send_with_retry for the FINAL reply to an inbound message; # tool-progress, commentary and stream first-sends leave it None. It decides (1) finalize-on-create # (intermediate cards stay open so edits don't flicker) and (2) whether to fire the Done reaction. - is_final_reply = reply_to is not None + # (is_final_reply is computed above, before the webhook check.) if self._card_template_id and current_message and self._card_sdk: await self._close_streaming_siblings(chat_id) # close lingering tool-progress cards before creating a new one result = await self._create_and_stream_card(chat_id, current_message, content, finalize=is_final_reply) @@ -532,13 +535,40 @@ self._fire_done_reaction(chat_id) return SendResult(success=True, message_id=uuid.uuid4().hex[:12]) logger.warning("[%s] Send failed HTTP %d: %s", self.name, resp.status_code, resp.text[:200]) + if resp.status_code >= 400: # expired/consumed webhook: don't lose the message + return await self._send_via_robot_fallback(chat_id, content, is_final_reply) return SendResult(success=False, error=f"HTTP {resp.status_code}: {resp.text[:200]}") except httpx.TimeoutException: - return SendResult(success=False, error="Timeout sending message to DingTalk") + return await self._send_via_robot_fallback(chat_id, content, is_final_reply) except Exception as e: logger.error("[%s] Send error: %s", self.name, e) return SendResult(success=False, error=str(e)) + # Fallback sender used when no live session webhook exists (e.g. right after a gateway restart, or the + # webhook expired). Shells out to the proven robot-API helper, which resolves the recipient staff id from + # DINGTALK_PUSH_USERID or the last inbound dingtalk DM in state.db (so it works without a live session). + _ROBOT_FALLBACK_CMD = "/root/.hermes/bin/dingtalk-push" + + async def _send_via_robot_fallback(self, chat_id: str, content: str, is_final_reply: bool) -> SendResult: + text = content[: self.MAX_MESSAGE_LENGTH] + try: + proc = await asyncio.create_subprocess_exec( + self._ROBOT_FALLBACK_CMD, text, + stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT) + out, _ = await asyncio.wait_for(proc.communicate(), timeout=30) + if proc.returncode == 0: + logger.info("[%s] Sent via robot fallback (no session_webhook) chat_id=%s", self.name, chat_id) + if is_final_reply: + self._fire_done_reaction(chat_id) + return SendResult(success=True, message_id=uuid.uuid4().hex[:12]) + logger.warning("[%s] Robot fallback rc=%s out=%s", self.name, proc.returncode, (out or b"")[:200]) + return SendResult(success=False, error=f"robot fallback rc={proc.returncode}: {(out or b'')[:120]!r}") + except FileNotFoundError: + return SendResult(success=False, error=f"robot fallback helper missing: {self._ROBOT_FALLBACK_CMD}") + except Exception as e: + logger.error("[%s] Robot fallback error: %s", self.name, e) + return SendResult(success=False, error=f"robot fallback error: {e}") + async def send_typing(self, chat_id: str, metadata=None) -> None: """DingTalk does not support typing indicators.""" -- 改动是否只在 dingtalk 适配器(常驻目录其余漂移)-- /usr/local/lib/hermes-agent/plugins/platforms/dingtalk/adapter.py -- 重启/生效证据 -- ActiveEnterTimestamp=Tue 2026-09-29 01:27:22 CST 2026-09-29 01:27:46,270 WARNING hermes_plugins.platforms__dingtalk.adapter: [Dingtalk] No valid session_webhook for chat_id=cid8JcY9Ofr6HswPgdt5UakkvZM5wFZ5Au7dctAkNlu6CI= -> robot fallback 2026-09-29 01:27:47,003 INFO hermes_plugins.platforms__dingtalk.adapter: [Dingtalk] Sent via robot fallback (no session_webhook) chat_id=cid8JcY9Ofr6HswPgdt5UakkvZM5wFZ5Au7dctAkNlu6CI= 2026-09-29 01:28:17,564 WARNING hermes_plugins.platforms__dingtalk.adapter: [Dingtalk] No valid session_webhook for chat_id=cid8JcY9Ofr6HswPgdt5UakkvZM5wFZ5Au7dctAkNlu6CI= -> robot fallback 2026-09-29 01:28:18,316 INFO hermes_plugins.platforms__dingtalk.adapter: [Dingtalk] Sent via robot fallback (no session_webhook) chat_id=cid8JcY9Ofr6HswPgdt5UakkvZM5wFZ5Au7dctAkNlu6CI= -- 阴性对照测试脚本 -- -rw-r--r-- 1 root root 1538 Sep 29 01:26 /root/.hermes/cache/scratch/rel-a5a63f5/test_robot_fallback.py