Peach: Local-first video metadata scraper and media manager.
fix(diagnostics): 修复断连误报与桥资源及转码故障证据
longmeidao5390cadd5129
diff --git a/MANIFEST.in b/MANIFEST.inindex 9f3212f2d..7ed458087 100644--- a/MANIFEST.in+++ b/MANIFEST.in@@ -1,3 +1,4 @@recursive-include migrations *.sqlrecursive-include web *recursive-include resources *.png *.jpg *.gif *.bmp *.webp *.avif *.svg *.ico *.icns *.json *.txt+include tools/amane-bridge/bridge.py tools/amane-bridge/pyproject.toml tools/amane-bridge/uv.lockdiff --git a/docs/DIAGNOSTICS.md b/docs/DIAGNOSTICS.mdindex d216e384f..05aab98bc 100644--- a/docs/DIAGNOSTICS.md+++ b/docs/DIAGNOSTICS.md@@ -69,6 +69,8 @@核验覆盖异常钩子、日志、后台任务、来源、HTTP、媒体、浏览器、启动与更新入口。筛选依据结构化状态和实际异常继承关系,不按错误原文、任意同名类或所有网络异常统一过滤。+整片转码的 `TranscodeUnavailable` 携带最后一次尝试的有界工具证据与退出码,并保留进程启动异常链。日志入口使用相同的媒体投影规则,只发送结构化退出码与已识别的 `media_failure` 类别;工具原文、媒体路径和尝试参数不进入远程事件。主动取消、超时和已识别的媒体输入条件仅保留本机记录。+| 状态 | 本机与远程处理 || --- | --- || 任务取消、暂停、互斥、计费与磁盘策略门槛 | 保留本机异常和任务回执,不占用错误上传额度 |@@ -77,7 +79,7 @@| warning 与重试日志 | 低于 error 的日志留在本机;error/critical 携带 Peach 栈时上报 || 媒体输入与环境条件 | 坏媒体、输入缺失、权限、空间与媒体工具超时留在本机;工具执行文件缺失、解码器能力缺失和未知技术错误继续上报;预览前置条件保留提示和可重试问题项 || 浏览器取消与扩展 | 主动取消请求的 AbortError 不发起记录请求;后端收到该类型仍仅写本机;扩展 URL 帧不上传,Peach 带构建参数的脚本帧保留行列位置 |-| 客户端中途断开 | Starlette 的 ClientDisconnect 留在本机,不当作服务端程序故障 |+| 客户端中途断开 | 响应头策略直接处理 ASGI 消息,文件响应收到断连后停止发送;服务器异常原样交给故障处理器。Starlette 的 ClientDisconnect 留在本机,不当作服务端程序故障 || 启动、CLI、托盘、更新与回滚 | 未处理的技术故障继续上报;安装辅助进程沿用仅本机诊断边界 || SDK、无堆栈日志与不透明浏览器错误 | 不递归上报 SDK 自身问题;无法取得 Peach 代码位置的日志和浏览器错误仅保留本机 |diff --git a/docs/REUSE.md b/docs/REUSE.mdindex 2370b223b..1ffa65c99 100644--- a/docs/REUSE.md+++ b/docs/REUSE.md@@ -38,6 +38,8 @@ JS/TS 文案复用 [Oxc parser](https://oxc.rs/docs/guide/usage/parser),开发## macOS 独立构建+Windows、macOS 与 wheel 通过 `bundle_resources.data_files` 共用运行资源清单,amane 桥只收集脚本、依赖清单和锁文件,运行环境仍位于用户数据目录。资源定位复用 `settings_file.PROJECT_ROOT`,覆盖源码、wheel 的 `_resources` 和 PyInstaller 的资源根,不按模块目录深度猜测。打包机制沿用官方 [PyInstaller 数据文件约定](https://www.pyinstaller.org/en/stable/runtime-information.html)与 setuptools 的资源复制,无新增依赖。+独立应用复用项目已有的 PyInstaller 6.22.3(GPL-2.0-or-later,带发行例外),由 macOS 原生 bootloader 承载 Python 3.14 与 Cocoa 菜单栏,资源与 CLI 入口沿用现有桌面实现。镜像使用系统自带的 `hdiutil`,无需新增运行时依赖。构建与签名依据 [PyInstaller macOS 文档](https://www.pyinstaller.org/en/stable/usage.html)和 [Apple 分发文档](https://developer.apple.com/documentation/xcode/packaging-mac-software-for-distribution)。Peach 负责独立数据目录、首启、构建身份和资源清单。arm64 与 x86_64 使用各自的 Python 和原生扩展,按 [PyInstaller 架构约束](https://pyinstaller.org/en/stable/feature-notes.html#macos-multi-arch-support)分别生成;不使用 lipo 合并冻结程序。应用不安装系统组件,采用 DMG 拖放安装;PKG 的系统级安装流程不属于这条路径。源码运行的 LaunchAgent 外壳继续使用自己的构建入口。独立制品的启动与读写验收使用隔离数据目录,不改变真实 ledger 或双机配置。@@ -88,6 +90,8 @@ DMM / MGS 的只读商品号识别复用 `code_key`、`release_code_from_text`## STRM 与挂载来源+响应头策略复用 Starlette 1.8.0(BSD-3-Clause,项目已有固定依赖)的 `MutableHeaders` 与官方 [ASGI 中间件模式](https://www.starlette.io/middleware/#pure-asgi-middleware),只在 `http.response.start` 阶段设置缓存、安全及机器人策略。文件响应继续使用同一 `receive` 的断连消息,真实异常直接传播,不维护异常文本匹配或统一忽略网络错误的分支。整片转码继续复用 FFmpeg 和现有回退链,Peach 只保留有界本机证据与隐私投影,无新增依赖。+STRM 采用文本指针、播放时解析的通用行为,参考 SmartStrm 文档、OpenList 的 `drivers/strm` 与 Jellyfin 的 STRM 探测入口;固定 revision、许可证与实际读取位置见 [取证记录](reference-snapshots/strm-source-evidence.md)。SmartStrm 核心闭源,源码未取得。Peach 独立实现受限 HTTP(S) 指针,复用 HTTPX、现有 Range 传输、`m3u8` 解析与播放器计划;无新增运行依赖,也不复制上游应用代码。光鸭、OneDrive 与其他网盘复用声明挂载根、路径翻译、扫描、文件整理与回滚、隔离删除和空目录清理。所有挂载来源统一开放读写,底层权限或不支持的操作由挂载工具报错;网盘登录与平台 API 由 CloudDrive2 或 OpenList 等挂载工具提供,不新增 SDK 或依赖。临时文件与账本回归覆盖三个来源的整理、回滚、删除、权限拒绝和空目录清理;真实账号兼容性见待办。挂载目录可单独设置代理流量保护,成本投影与批处理使用同一规则。STRM、ISO 跳过后台媒体读取,普通媒体保留按来源策略执行的探测;九宫格和时间轴共用本机视频预览开关。diff --git a/scripts/build_windows.ps1 b/scripts/build_windows.ps1index b7cb04ef1..dfffe1d21 100644--- a/scripts/build_windows.ps1+++ b/scripts/build_windows.ps1@@ -128,7 +128,13 @@ if ($Standalone) {throw "cloudflared SHA-256 mismatch: expected $($CloudflaredManifest.sha256), got $CloudflaredHash"}}-& $Python -m PyInstaller --noconfirm --clean @BuildMode --windowed --name Peach `+$ResourceArguments = @()+$ResourceEntries = & $Python -X utf8 (Join-Path $ProjectRoot 'src/peach/bundle_resources.py') --root $ProjectRoot+if ($LASTEXITCODE -ne 0) { throw 'Runtime resource manifest is incomplete.' }+foreach ($ResourceEntry in $ResourceEntries) {+ $ResourceArguments += @('--add-data', $ResourceEntry)+}+& $Python -m PyInstaller --noconfirm --clean @BuildMode @ResourceArguments --windowed --name Peach `--distpath $BuildDestination --workpath $WorkPath --specpath $BuildPath `--paths (Join-Path $ProjectRoot 'src') --python-option 'X utf8' `--collect-all curl_cffi --collect-all resvg_py `@@ -137,9 +143,6 @@ if ($Standalone) {--hidden-import uvicorn.protocols.http.auto --hidden-import uvicorn.protocols.websockets.auto `--hidden-import uvicorn.lifespan.on `--icon (Join-Path $ProjectRoot 'resources\peach.ico') `- --add-data "$(Join-Path $ProjectRoot 'web');web" `- --add-data "$(Join-Path $ProjectRoot 'migrations');migrations" `- --add-data "$(Join-Path $ProjectRoot 'resources');resources" `--add-data "${BuildInfoPath};." `--add-data "${SentryConfigPath};." `(Join-Path $ProjectRoot 'scripts\build_app_entry.py')diff --git a/scripts/peach_macos.spec b/scripts/peach_macos.specindex 971f86a95..c65ee7808 100644--- a/scripts/peach_macos.spec+++ b/scripts/peach_macos.spec@@ -2,6 +2,7 @@"""macOS 独立应用:原生启动器、内置解释器与只读资源。"""import jsonimport os+import runpyfrom pathlib import Pathimport sys@@ -13,7 +14,8 @@ from peach.appid import MACOS_BUNDLE_IDidentity = Path(os.environ["PEACH_MACOS_BUILD_INFO"])version = json.loads(identity.read_text(encoding="utf-8"))["version"]-datas = [(str(root / name), name) for name in ("web", "migrations", "resources")]+resource_manifest = runpy.run_path(str(root / 'src/peach/bundle_resources.py'))+datas = [(str(source), target) for source, target in resource_manifest['data_files'](root)]datas += [(str(identity), "."), (str(root / "scripts" / "standalone.txt"), ".")]datas += [(str(root / "LICENSE"), ".")]datas += copy_metadata("sentry-sdk")diff --git a/scripts/smoke_wheel.py b/scripts/smoke_wheel.pyindex 0947f6ad4..b00077788 100644--- a/scripts/smoke_wheel.py+++ b/scripts/smoke_wheel.py@@ -9,6 +9,37 @@ import timefrom pathlib import Path+def assert_bridge(client, headers):+ """桥资源与设置接口必须提供同一份固定版本事实。"""+ from peach.metadata_amane import BRIDGE_SCRIPT, locked_version, pinned_revision+ assert BRIDGE_SCRIPT.is_file()+ bridge = client.get('/api/scraping/amane-bridge', headers=headers)+ assert bridge.status_code == 200, bridge.text+ assert bridge.json()['revision'] == pinned_revision()+ assert bridge.json()['version'] == locked_version() and locked_version()+++def stop_server(process, log_root):+ """关闭本轮服务进程树,等待日志持有者实际退出。"""+ if os.name == 'nt':+ from peach.jobs import process_alive+ runtime_pids = [int(path.stem.removeprefix('crash-'))+ for path in log_root.glob('crash-*.log')+ if path.stem.removeprefix('crash-').isdigit()]+ stopped = subprocess.run([str(Path(os.environ['SystemRoot']) / 'System32/taskkill.exe'),+ '/PID', str(process.pid), '/T', '/F'], capture_output=True, timeout=15)+ if stopped.returncode and process.poll() is None:+ raise RuntimeError('wheel 冒烟服务进程树未能关闭')+ deadline = time.monotonic() + 15+ while any(process_alive(pid) for pid in runtime_pids):+ if time.monotonic() >= deadline:+ raise RuntimeError('wheel 冒烟服务仍持有临时日志')+ time.sleep(0.05)+ else:+ process.terminate()+ process.wait(timeout=15)++def main():with tempfile.TemporaryDirectory(prefix="peach-wheel-") as directory:root = Path(directory).resolve()@@ -48,8 +79,9 @@ def main():for name in ("peach-app.js", "peach-app.css", "peach-pages.js", "peach-pages.css"):assert (settings.page_path.parent / "dist" / name).is_file()assert any(settings.vendor_path.iterdir())+ assert_bridge(client, headers)print(json.dumps({"module": peach.__file__, "ready": ready.json(),- "checks": ["migrations", "web", "vendor", "frontend", "auth", "items"]}))+ "checks": ["migrations", "web", "vendor", "frontend", "auth", "items", "amane-bridge"]}))finally:client.close()with socket.socket() as reservation:@@ -78,8 +110,7 @@ def main():assert response.status_code == 200, (path, response.status_code)print(json.dumps({"tcp": "passed", "init_repeat": "existing_config_preserved"}))finally:- process.terminate()- process.wait(timeout=15)+ stop_server(process, root / 'data/logs')if __name__ == "__main__":diff --git a/setup.py b/setup.pyindex 270089c18..7df77f405 100644--- a/setup.py+++ b/setup.py@@ -1,5 +1,6 @@"""将运行时资源装入 wheel,源码树仍是资源的唯一维护位置。"""from pathlib import Path+import runpyfrom setuptools import setupfrom setuptools.command.build_py import build_py@@ -10,8 +11,14 @@ class BuildWithResources(build_py):super().run()root = Path(__file__).resolve().parentdestination = Path(self.build_lib) / "peach" / "_resources"- for name in ("migrations", "web", "resources"):- self.copy_tree(str(root / name), str(destination / name))+ manifest = runpy.run_path(str(root / 'src/peach/bundle_resources.py'))+ for source, target in manifest['data_files'](root):+ output = destination / target+ if source.is_dir():+ self.copy_tree(str(source), str(output))+ else:+ output.mkdir(parents=True, exist_ok=True)+ self.copy_file(str(source), str(output / source.name))setup(cmdclass={"build_py": BuildWithResources})diff --git a/src/peach/api.py b/src/peach/api.pyindex c4522dbba..05d19d534 100644--- a/src/peach/api.py+++ b/src/peach/api.py@@ -32,6 +32,7 @@ from fastapi import FastAPI, Requestfrom fastapi.responses import HTMLResponse, JSONResponse, PlainTextResponse, RedirectResponsefrom fastapi.staticfiles import StaticFilesfrom starlette.exceptions import HTTPException as StarletteHTTPException+from starlette.datastructures import MutableHeadersfrom starlette.middleware.gzip import DEFAULT_EXCLUDED_CONTENT_TYPES, GZipMiddlewarefrom . import (__version__, browser_transport, feeds, tunnel, web_contract,@@ -102,6 +103,32 @@ COMPRESSION_EXCLUDED_TYPES = ()+class ResponseHeadersMiddleware:+ """在 ASGI 响应头阶段设置缓存与安全策略,原样传递连接和异常。"""++ def __init__(self, app):+ self.app = app++ async def __call__(self, scope, receive, send):+ if scope['type'] != 'http':+ await self.app(scope, receive, send)+ return++ async def send_headers(message):+ if message['type'] == 'http.response.start':+ headers = MutableHeaders(scope=message)+ if scope['path'].startswith('/vendor/'):+ headers['Cache-Control'] = 'public, max-age=31536000, immutable'+ elif 'cache-control' not in headers:+ headers['Cache-Control'] = 'no-store'+ headers['X-Content-Type-Options'] = 'nosniff'+ headers['Referrer-Policy'] = 'same-origin'+ headers['X-Robots-Tag'] = routes_pages.ROBOTS_TAG+ await send(message)++ await self.app(scope, receive, send_headers)++#: Starlette 自己抛的那几种 HTTPException 带的是英文 detail,给人看之前换成中文。_DEFAULT_DETAILS = {"Not Found": "这个地址下没有页面。",@@ -517,24 +544,12 @@ def create_app(name="vendor",)- @app.middleware("http")- async def no_store(request: Request, call_next):- response = await call_next(request)- if request.url.path.startswith("/vendor/"):- response.headers["Cache-Control"] = "public, max-age=31536000, immutable"- elif "cache-control" not in response.headers:- response.headers["Cache-Control"] = "no-store"- response.headers["X-Content-Type-Options"] = "nosniff"- response.headers["Referrer-Policy"] = "same-origin"- # 公网入口开着的时候整个站都在互联网上。这一条覆盖每一个响应——页面、静态- # 资源和 API 都算,页面里的 `<meta name="robots">` 只管得到 HTML 那几页。- response.headers["X-Robots-Tag"] = routes_pages.ROBOTS_TAG- return response+ app.add_middleware(ResponseHeadersMiddleware)# JSON 契约、页面脚本与样式是仅有的几类大文本响应,压下去省的字节最多:# `/api/items`、主界面 bundle 与样式都通过内容类型选择压缩。# `add_middleware` 是 `insert(0)`,最后加的在最外层,所以压缩看到的是上面- # `no_store` 补完 Cache-Control 之后的最终响应头。+ # ResponseHeadersMiddleware 补完 Cache-Control 之后的最终响应头。# 自己写内容类型闸门的 ASGI 中间件是重复劳动:Starlette 这个已经按 Content-Type# 排除、跳过 206 与已编码响应、逐块流式压缩,还会把大块丢到工作线程去压,# 不占事件循环。只需要把它的排除名单补齐(见 COMPRESSION_EXCLUDED_TYPES)。diff --git a/src/peach/bundle_resources.py b/src/peach/bundle_resources.pynew file mode 100644index 000000000..ea0b77c7f--- /dev/null+++ b/src/peach/bundle_resources.py@@ -0,0 +1,22 @@+"""源码、wheel 与桌面构建共用的只读运行资源清单。"""+from pathlib import Path+++def data_files(root):+ root = Path(root).resolve()+ resources = [(root / name, name) for name in ('web', 'migrations', 'resources')]+ resources += [(root / 'tools/amane-bridge' / name, 'tools/amane-bridge')+ for name in ('bridge.py', 'pyproject.toml', 'uv.lock')]+ for source, _target in resources:+ if not source.exists():+ raise FileNotFoundError(source)+ return resources+++if __name__ == '__main__':+ import argparse+ parser = argparse.ArgumentParser(description=__doc__)+ parser.add_argument('--root', required=True, type=Path)+ args = parser.parse_args()+ for source, target in data_files(args.root):+ print(f'{source};{target}')diff --git a/src/peach/metadata_amane.py b/src/peach/metadata_amane.pyindex a3e2d4ce5..a69ba278a 100644--- a/src/peach/metadata_amane.py+++ b/src/peach/metadata_amane.py@@ -30,11 +30,12 @@ from .metadata import MetadataProviderError, identifies_code, validate_provider_from .sources.base import (COOLDOWN_ACTIONS, REASON_KINDS, FailureReason, SiteConfig, SiteRecord,SourceFailure)from .user_agent import USER_AGENT+from .settings_file import PROJECT_ROOTAMANE_REPOSITORY = "https://github.com/sqzw-x/amane"AMANE_LICENSE = "GPL-3.0"#: 桥的源码目录:清单、锁与脚本都在仓库里,随 Peach 分发的只有这三个文件,不含 amane 源码。-BRIDGE_ROOT = Path(__file__).resolve().parents[2] / "tools" / "amane-bridge"+BRIDGE_ROOT = PROJECT_ROOT / "tools" / "amane-bridge"BRIDGE_SCRIPT = BRIDGE_ROOT / "bridge.py"#: 桥的 venv 建在数据目录的工具区,和 FFmpeg 同一层;名字与源码目录一致。BRIDGE_TOOL_NAME = "amane-bridge"diff --git a/src/peach/remote_reporting.py b/src/peach/remote_reporting.pyindex ad7986f41..33f2d6886 100644--- a/src/peach/remote_reporting.py+++ b/src/peach/remote_reporting.py@@ -304,6 +304,11 @@ def _media_details(error, fields, codes):return ""+def _media_failure(stage, error):+ return stage in MEDIA_STAGES or any(cls.__module__ == 'peach.transcodes'+ and cls.__name__ == 'TranscodeUnavailable' for cls in type(error).__mro__)++def _expected_failure(stage, error, fields):if stage == "logging" and fields.get("level", "ERROR") not in {"ERROR", "CRITICAL"}:return True@@ -317,7 +322,7 @@ def _expected_failure(stage, error, fields):"not_found", "gone", "no_usable_metadata", "rate_limited", "timeout", "server_error", "network"}if ("peach.metadata", "MetadataProviderError") in kinds and error.detail != "parse_error":return error.kind in {"auth", "not_found"} or error.retryable is True or error.temporary is True- if stage in MEDIA_STAGES:+ if _media_failure(stage, error):detail = getattr(error, "diagnostics", {})if isinstance(error, subprocess.TimeoutExpired) or isinstance(detail, dict) and detail.get("reason") == "timeout":return True@@ -370,7 +375,7 @@ def _http_details(stage, fields):def _technical_projection(stage, error, fields):"""投影各入口的故障证据,排除缺少定位信息与已知媒体输入条件的记录。"""- media_stage = stage in MEDIA_STAGES+ media_stage = _media_failure(stage, error)if error is None and stage != "browser" and not media_stage:returnvalues, codes, _chain = _exception_details(error)diff --git a/src/peach/transcodes.py b/src/peach/transcodes.pyindex ac59ef424..768d5eee1 100644--- a/src/peach/transcodes.py+++ b/src/peach/transcodes.py@@ -41,7 +41,13 @@ class _TranscodeAttempt:class TranscodeUnavailable(RuntimeError):- pass+ """转码不可用;本机保留有界工具证据,远程仅投影错误码和类别。"""++ def __init__(self, message='', *, diagnostics=None):+ super().__init__(message)+ from .error_reporting import scrub+ self.diagnostics = scrub(diagnostics or {})+ self.returncode = self.diagnostics.get('returncode')class TranscodeCancelled(TranscodeUnavailable):@@ -153,6 +159,8 @@ class TranscodeService:temporary = target.with_name(f"{target.stem}.{uuid.uuid4().hex}.tmp.mp4")deadline = time.monotonic() + TRANSCODE_TIMEOUT_SECONDSlast_detail = ""+ last_diagnostics = {}+ last_error = Nonecompleted = Falsetry:with self._slots:@@ -175,6 +183,8 @@ class TranscodeService:raiseexcept (OSError, TranscodeUnavailable) as exc:last_detail = f"{attempt.name}: {exc}"+ last_diagnostics = getattr(exc, 'diagnostics', {})+ last_error = excif time.monotonic() >= deadline:breakcontinue@@ -186,8 +196,12 @@ class TranscodeService:breakdetail = (stderr or b"").decode("utf-8", "replace")[-1000:]last_detail = f"{attempt.name}: {detail or 'ffmpeg failed'}"+ last_diagnostics = {'reason': 'ffmpeg_error', 'returncode': returncode,+ 'stderr': detail, 'attempt': attempt.name}+ last_error = Noneif not completed:- raise TranscodeUnavailable(last_detail or "ffmpeg failed")+ raise TranscodeUnavailable(last_detail or "ffmpeg failed",+ diagnostics=last_diagnostics) from last_errorfinally:temporary.unlink(missing_ok=True)@@ -372,8 +386,10 @@ class TranscodeService:raise TranscodeCancelled(session)if time.monotonic() >= deadline:process.kill()- process.communicate()- raise TranscodeUnavailable(f"{label} timed out")+ _stdout, stderr = process.communicate()+ raise TranscodeUnavailable(f"{label} timed out", diagnostics={+ 'reason': 'timeout', 'returncode': process.returncode,+ 'stderr': (stderr or b'').decode('utf-8', 'replace')[-1000:]})self._raise_if_cancelled(session, registry)return (process.returncode if process.returncode is not None else -1,diff --git a/tests/test_complexity_ratchet.py b/tests/test_complexity_ratchet.pyindex c8cf8bcf9..f87689f38 100644--- a/tests/test_complexity_ratchet.py+++ b/tests/test_complexity_ratchet.py@@ -66,7 +66,7 @@ BASELINE: dict[str, int] = {#: 判过就该这么长的函数:`(分支数, 为什么拆开更差)`。键与 `BASELINE` 同形、不重叠。ACCEPTED: dict[str, tuple[int, str]] = {"src/peach/api.py:create_app": (- 42, "应用工厂:按启动顺序构造数据库、各项后台服务(含可选分享)与路由并接成一个 app,每个可注入的"+ 40, "应用工厂:按启动顺序构造数据库、各项后台服务(含可选分享)与路由并接成一个 app,每个可注入的""依赖各带一个缺省;挂载探测按配置状态启动。拆开会把同一条构造顺序分散到几处"),"src/peach/library_processing.py:process_library": (48, "顺序流水线:垃圾检查、媒体属性、本地资料、本地预览、联网采集、写候选、收尾共用一份进度状态"diff --git a/tests/test_reporting_fault_fixes.py b/tests/test_reporting_fault_fixes.pynew file mode 100644index 000000000..1ad374d8e--- /dev/null+++ b/tests/test_reporting_fault_fixes.py@@ -0,0 +1,158 @@+"""远程诊断的断连、独立资源与媒体故障证据行为回归。"""+import asyncio+import importlib.util+import json+from pathlib import Path+import shutil+import sys+import tempfile+import unittest+from unittest import mock++from peach import api, metadata_amane, remote_reporting, settings_file+from peach.ffmpeg import BinaryChoice+from peach.streaming import BufferedFileResponse+from peach.transcodes import TranscodeService, TranscodeUnavailable+++class FaultFixTests(unittest.TestCase):+ def test_early_disconnect_returns_without_a_server_exception(self):+ async def exercise(path):+ async def inner(scope, receive, send):+ await BufferedFileResponse(path)(scope, receive, send)++ async def receive():+ return {'type': 'http.disconnect'}++ async def send(message):+ pass++ await api.ResponseHeadersMiddleware(inner)(+ {'type': 'http', 'method': 'GET', 'path': '/stream', 'headers': []}, receive, send)++ with tempfile.TemporaryDirectory() as folder:+ path = Path(folder).resolve() / 'synthetic.mp4'+ path.write_bytes(b'synthetic')+ asyncio.run(exercise(path))++ def test_server_errors_propagate_even_after_client_disconnect(self):+ error = RuntimeError('synthetic server failure')++ async def inner(scope, receive, send):+ await receive()+ raise error++ async def receive():+ return {'type': 'http.disconnect'}++ with self.assertRaises(RuntimeError) as raised:+ asyncio.run(api.ResponseHeadersMiddleware(inner)(+ {'type': 'http', 'path': '/stream'}, receive, mock.AsyncMock()))+ self.assertIs(raised.exception, error)++ def test_response_headers_preserve_cache_policy_and_non_http_messages(self):+ class Messages(list):+ async def append_async(self, value):+ self.append(value)++ async def check(path, cache=None):+ messages = Messages()++ async def inner(scope, receive, send):+ headers = [] if cache is None else [(b'cache-control', cache)]+ await send({'type': 'http.response.start', 'status': 206, 'headers': headers})+ await send({'type': 'http.response.body', 'body': b'body'})++ await api.ResponseHeadersMiddleware(inner)(+ {'type': 'http', 'path': path}, mock.AsyncMock(), messages.append_async)+ return messages++ for path, cache, expected in (('/vendor/test.js', None, b'public, max-age=31536000, immutable'),+ ('/stream', None, b'no-store'), ('/api/test', b'public, no-cache', b'public, no-cache')):+ with self.subTest(path=path):+ messages = asyncio.run(check(path, cache))+ headers = dict(messages[0]['headers'])+ self.assertEqual(headers[b'cache-control'], expected)+ self.assertEqual(headers[b'x-content-type-options'], b'nosniff')+ self.assertEqual(headers[b'referrer-policy'], b'same-origin')+ self.assertIn(b'x-robots-tag', headers)+ self.assertEqual(messages[1]['body'], b'body')+ inner = mock.AsyncMock()+ asyncio.run(api.ResponseHeadersMiddleware(inner)({'type': 'websocket'}, None, None))+ inner.assert_awaited_once_with({'type': 'websocket'}, None, None)++ def test_bundle_manifest_loads_bridge_revision_in_frozen_and_wheel_layouts(self):+ from peach.bundle_resources import data_files+ root = Path(__file__).resolve().parents[1]+ bridge_data = [(Path(source), target) for source, target in data_files(root)+ if target == 'tools/amane-bridge']+ self.assertEqual({path.name for path, _ in bridge_data}, {'bridge.py', 'pyproject.toml', 'uv.lock'})+ for layout in ('_internal', 'site-packages/peach/_resources'):+ with self.subTest(layout=layout), tempfile.TemporaryDirectory() as folder:+ destination = Path(folder).resolve() / layout+ for source, target in bridge_data:+ parent = destination / target+ parent.mkdir(parents=True, exist_ok=True)+ shutil.copy2(source, parent / source.name)+ name = 'peach._bridge_resource_fixture'+ spec = importlib.util.spec_from_file_location(name, metadata_amane.__file__)+ module = importlib.util.module_from_spec(spec)+ with mock.patch.dict(sys.modules, {name: module}), \+ mock.patch.object(settings_file, 'PROJECT_ROOT', destination):+ spec.loader.exec_module(module)+ facts = module.describe(Path(folder).resolve() / 'data-tools')+ self.assertEqual(facts['revision'], metadata_amane.pinned_revision())+ self.assertTrue(module.BRIDGE_SCRIPT.is_file())+ self.assertFalse(facts['installed'])++ def test_transcode_failure_projects_bounded_structured_tool_evidence(self):+ with tempfile.TemporaryDirectory() as folder:+ root = Path(folder).resolve()+ source = root / 'synthetic.avi'+ source.write_bytes(b'synthetic')+ resolver = mock.Mock()+ resolver.ffmpeg.return_value = BinaryChoice(root / 'ffmpeg.exe', 'test')+ service = TranscodeService(resolver, root / 'cache', prefer_hardware=False)+ with mock.patch.object(service, '_probe', return_value=None), \+ mock.patch.object(service.hardware, 'encoders', return_value=()), \+ mock.patch.object(service, '_execute', return_value=(69, b'', b'Unknown decoder private_input')):+ try:+ service.browser_path(1, source)+ except TranscodeUnavailable as error:+ self.assertEqual(error.returncode, 69)+ with mock.patch.object(remote_reporting, 'send') as send:+ remote_reporting.capture('logging', error, logger='peach.routes_media', level='ERROR')+ event = send.call_args.args[1]+ else:+ self.fail('合成工具失败必须生成转码异常')+ self.assertEqual(event['tags']['returncode'], '69')+ self.assertEqual(event['tags']['media_failure'], 'decoder_unavailable')+ self.assertNotIn('private_input', json.dumps(event))++ def test_expected_media_inputs_and_cancellation_stay_local(self):+ from peach.transcodes import TranscodeCancelled+ for error in (TranscodeUnavailable('synthetic', diagnostics={'stderr': 'Invalid data found', 'returncode': 1}),+ TranscodeCancelled('synthetic')):+ with self.subTest(error=type(error).__name__), mock.patch.object(remote_reporting, 'send') as send:+ remote_reporting.capture('logging', error, logger='peach.routes_media', level='ERROR')+ send.assert_not_called()++ def test_transcode_input_failure_is_local_after_real_pipeline_frames(self):+ with tempfile.TemporaryDirectory() as folder:+ root = Path(folder).resolve()+ source = root / 'synthetic.avi'+ source.write_bytes(b'synthetic')+ resolver = mock.Mock()+ resolver.ffmpeg.return_value = BinaryChoice(root / 'ffmpeg.exe', 'test')+ service = TranscodeService(resolver, root / 'cache', prefer_hardware=False)+ with mock.patch.object(service, '_probe', return_value=None), \+ mock.patch.object(service.hardware, 'encoders', return_value=()), \+ mock.patch.object(service, '_execute', return_value=(1, b'', b'Invalid data found')):+ with mock.patch.object(remote_reporting, 'send') as send:+ try:+ service.browser_path(1, source)+ except TranscodeUnavailable as error:+ remote_reporting.capture('logging', error, logger='peach.routes_media', level='ERROR')+ else:+ self.fail('坏输入必须保留转码失败状态')+ send.assert_not_called()