"""L1 离线探针:文件只走 S3 的 A/B/C 三层在 Hermes 源码上是否成立。 只读本机 Hermes 源码并在内存里调用,不连 Matrix、不发消息、不改任何配置。 用法(必须用 Hermes 自己的 venv): "$HERMES/venv/Scripts/python.exe" scripts/probe-s3-only-outbound.py --hermes "$HERMES" --out test-results/s3-only-20261201/l1-probe.json """ from __future__ import annotations import argparse import asyncio import json import os import sys import tempfile from pathlib import Path from unittest.mock import AsyncMock, MagicMock def probe_extraction(Base): """回复写成哪种格式,网关会把它当附件抽走(抽走 = Matrix 会尝试上传)。""" tmp = Path(tempfile.mkdtemp()) local = tmp / 'weekly.md' local.write_text('x', encoding='utf-8') url = 'https://s3.fmode.cn/deliverables/agent-lj/202612/did/weekly.md' img = 'https://s3.fmode.cn/deliverables/agent-lj/202612/did/chart.png' cases = { 'markdown_link': f'报告已生成:[weekly.md]({url})', 'name_newline_url': f'weekly.md\n{url}', 'bare_url': f'下载:{url}', 'markdown_image': f'图表:![chart]({img})', 'markdown_link_to_image': f'图表:[chart.png]({img})', 'bare_image_url': f'图表:{img}', 'media_tag_local': f'MEDIA:{local}', 'bare_local_path': f'文件在 {local}', } out = {} for name, text in cases.items(): media, rest = Base.extract_media(text) images, rest2 = Base.extract_images(rest) files, rest3 = Base.extract_local_files(rest2) out[name] = { 'input': text, 'media_tags': [m[0] for m in media], 'images': [i[0] for i in images], 'local_files': files, 'text_after': rest3.strip(), 'becomes_attachment': bool(media or images or files), } return out def probe_matrix_gate(adapter_mod, value: str): """A:MATRIX_MAX_MEDIA_BYTES=value 时出站上传是否被拦、入站原生媒体是否被拒。""" os.environ['MATRIX_MAX_MEDIA_BYTES'] = value cap = adapter_mod._env_number('MATRIX_MAX_MEDIA_BYTES', 100 * 1024 * 1024, int) Adapter = adapter_mod.MatrixAdapter adapter = Adapter.__new__(Adapter) adapter._max_media_bytes = cap client = MagicMock() client.upload_media = AsyncMock(return_value='mxc://fixture/x') adapter._client = client adapter._room_needs_encrypted_upload = AsyncMock(return_value=False) adapter._send_content_event = AsyncMock() result = asyncio.run(adapter._upload_and_send('!room:fixture', b'hello', 'weekly.md', 'text/markdown', 'm.file')) return { 'env': value, 'effective_cap': cap, 'upload_called': client.upload_media.await_count, 'client_calls': sorted({c[0].split('.')[0] for c in client.mock_calls}), 'event_sent': adapter._send_content_event.await_count, 'success': result.success if isinstance(getattr(result, 'success', None), bool) else 'reached_send', 'error': getattr(result, 'error', None) if isinstance(getattr(result, 'error', None), str) else None, 'inbound_native_media_size_1_rejected': bool(1 and 1 > cap), } def probe_hint(system_prompt_mod): agent = MagicMock() agent._platform_hint_overrides = { 'matrix': {'replace': 'MATRIX-HINT'}, 'wecom': None, } return { 'matrix_replace': system_prompt_mod._resolve_platform_hint(agent, 'matrix', 'DEFAULT'), 'wecom_untouched': system_prompt_mod._resolve_platform_hint(agent, 'wecom', 'DEFAULT'), 'append_only': system_prompt_mod._resolve_platform_hint( type('A', (), {'_platform_hint_overrides': {'matrix': {'append': 'EXTRA'}}})(), 'matrix', 'DEFAULT'), } def probe_toolsets(tools_config): keys = {'fmode_share'} cfg_default = {} cfg_wecom_known = {'known_plugin_toolsets': {'wecom': ['fmode_share']}} return { 'default_matrix': sorted(tools_config._enabled_plugin_toolsets(cfg_default, 'matrix', ['hermes-matrix'], keys)), 'default_wecom': sorted(tools_config._enabled_plugin_toolsets(cfg_default, 'wecom', ['hermes-wecom'], keys)), 'wecom_known_not_listed': sorted( tools_config._enabled_plugin_toolsets(cfg_wecom_known, 'wecom', ['hermes-wecom'], keys)), } def main(): parser = argparse.ArgumentParser() parser.add_argument('--hermes', required=True) parser.add_argument('--out', required=True) args = parser.parse_args() sys.path.insert(0, args.hermes) from gateway.platforms.base import BasePlatformAdapter from plugins.platforms.matrix import adapter as adapter_mod from agent import system_prompt from hermes_cli import tools_config report = { 'hermes': args.hermes, 'extraction': probe_extraction(BasePlatformAdapter), 'matrix_gate': [probe_matrix_gate(adapter_mod, v) for v in ('-1', '0', '104857600')], 'platform_hint': probe_hint(system_prompt), 'plugin_toolsets': probe_toolsets(tools_config), } Path(args.out).parent.mkdir(parents=True, exist_ok=True) Path(args.out).write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding='utf-8') print(json.dumps(report, ensure_ascii=False, indent=2)) if __name__ == '__main__': main()