Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(media): 补齐 AV1 软件解码与全链路诊断

longmeidao516a78ac5f0b
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 4d3b42801..17a42062c 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -47,7 +47,7 @@ jobs:
id: media-cache
uses: actions/[email protected]
with:
- key: windows-media-v1-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('scripts/build_ffmpeg_windows.sh', 'scripts/build_untrunc_windows.sh', 'scripts/fetch_ffmpeg_sources.py', 'scripts/fetch_untrunc_sources.py', 'scripts/ffmpeg-sources.json', 'scripts/untrunc-sources.json') }}
+ key: windows-media-v1-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('scripts/build_ffmpeg_windows.sh', 'scripts/dav1d-mingw64.ini', 'scripts/build_untrunc_windows.sh', 'scripts/fetch_ffmpeg_sources.py', 'scripts/fetch_untrunc_sources.py', 'scripts/ffmpeg-sources.json', 'scripts/untrunc-sources.json') }}
path: |
build/ffmpeg-windows/ffmpeg
build/ffmpeg-windows/ffmpeg-corresponding-source.zip
@@ -57,7 +57,7 @@ jobs:
if: steps.media-cache.outputs.cache-hit != 'true'
run: |
sudo apt-get update
- sudo apt-get install -y build-essential gcc-mingw-w64-x86-64-posix g++-mingw-w64-x86-64-posix mingw-w64-x86-64-dev nasm pkg-config python3 clang ca-certificates curl xz-utils
+ sudo apt-get install -y build-essential gcc-mingw-w64-x86-64-posix g++-mingw-w64-x86-64-posix mingw-w64-x86-64-dev nasm pkg-config python3 clang meson ninja-build ca-certificates curl xz-utils
- name: 构建固定版本媒体工具
if: steps.media-cache.outputs.cache-hit != 'true'
run: bash scripts/build_ffmpeg_windows.sh build/ffmpeg-windows
diff --git a/README.md b/README.md
index 8ab19674a..3159bb243 100644
--- a/README.md
+++ b/README.md
@@ -122,7 +122,7 @@ Windows 安装包和免安装包均携带 FFmpeg、ffprobe、untrunc,转码、
Windows 托盘独立运行,关闭启动它的 Codex、Claude 或终端不会退出 Peach。托盘异常退出后会自动尝试恢复;5 分钟内最多恢复 3 次。主动选择「退出 Peach」会保持关闭。退出原因和恢复结果可在日志目录的 `tray-lifecycle.log` 中查看。
-启动失败弹窗会给出诊断报告位置。程序能运行时,可从托盘「导出诊断报告」或系统诊断页「下载诊断报告」取得脱敏 ZIP,用于问题反馈。托盘导出完成后可打开报告所在目录、打开文件或关闭提示。
+启动失败弹窗会给出诊断报告位置。程序能运行时,可从托盘「导出诊断报告」或系统诊断页「下载诊断报告」取得脱敏 ZIP,用于问题反馈。馆藏处理失败时,重试失败项后导出同一份 ZIP 即可,无需逐个挑选日志。报告包含工具版本与解码能力、处理问题、启动准备、更新和隧道记录;已登录设备的同源浏览器错误也会记录。诊断报告下载限运行 Peach 的本机。托盘导出完成后可打开报告所在目录、打开文件或关闭提示。
### 从源码运行(Windows、macOS)
diff --git a/docs/DIAGNOSTICS_AUDIT.md b/docs/DIAGNOSTICS_AUDIT.md
new file mode 100644
index 000000000..79ee6c34c
--- /dev/null
+++ b/docs/DIAGNOSTICS_AUDIT.md
@@ -0,0 +1,59 @@
+# 日志与诊断核验
+
+核验范围覆盖 269 个 Python 模块的异常与子进程入口,以及浏览器错误上报、托盘、更新和诊断导出。静态枚举用于建立候选清单,各链路按故障语义复查;正常取消、缓存回退、来源未命中和任务互斥保留各自状态。
+
+## 反馈入口
+
+馆藏处理失败时,重试失败项,再从托盘「导出诊断报告」或系统诊断页「下载诊断报告」取得 ZIP。用户只需提供一个 ZIP。下载限运行 Peach 的本机,已登录的同源设备可以上报浏览器错误;开放访问模式不提供匿名远程上报。
+
+错误发生时必须实际记录底层证据。完整问题清单位于 `state/library-processing-<job_id>.issues.jsonl`。通用问题清单不能还原被丢弃的 FFmpeg 错误;没有原始记录的历史失败需要重试取证。
+
+## 证据链
+
+| 链路 | 持久证据与核验 |
+| --- | --- |
+| 启动、CLI、托盘 | 配置及可选依赖加载前安装异常钩子;主线程、后台线程、unraisable、asyncio 和 warning/error 日志写入 `errors-*.log`,原生故障写入 `crash-*.log`;不可写时使用备用目录 |
+| HTTP 请求 | 未处理异常与 HTTP 5xx 记录 traceback、方法、路由模板、状态码和 `error_id`;响应头 `X-Peach-Error-ID` 与记录关联;请求参数、正文和鉴权头不作为上下文采集 |
+| 后台任务 | 异常记录任务键、job/run 编号、线程与 traceback;显式返回失败的操作记录失败消息;暂停、互斥和取消沿用任务协议 |
+| 抽帧、拼图、探测 | 保留退出码、超时、采样位置、时长、工具来源、色彩重试和有界 stderr;`media-probe`、`preview`、`frame-capture`、`frame-tile` 与 `library-preview` 覆盖各入口 |
+| 图片有效性 | Pillow 完整解码确认图片;纯色小 JPEG 可用,残片、非零退出和陈旧输出不能算生成成功 |
+| 外部来源、下载 | 来源授权、限流、冷却与未命中使用领域状态;意外失败使用现有异常日志和任务回执;ZIP 包含来源健康状态与时间,来源响应原文留在本机 |
+| 更新与回滚 | 安装、回滚失败保留 traceback;ZIP 包含启动准备、更新状态、源码更新日志、托盘重启日志及近期迁移日志 |
+| 隧道 | ZIP 包含 `cloudflare-tunnel.log` 与脱敏运行状态 |
+| 浏览器 | 保存消息、堆栈、脚本文件名、行列、页面与浏览器标识;查询参数不进入页面字段;同源、有效登录、每分钟条数和请求体积门槛有回归 |
+| 媒体工具 | 诊断报告与 ZIP 检查实际版本、可执行性及 AV1 软件解码器;`/healthz` 只使用轻量路径检查,不执行外部工具 |
+
+## ZIP 内容
+
+- `report.json`:系统和版本信息、检查结果、媒体工具能力、来源健康摘要及文件收集回执。
+- `logs/<目录序号>/`:托盘、服务、重启、首次扫描、源码更新、隧道、错误及崩溃日志;固定日志包含近期自然月分段。
+- `processing/`:馆藏处理状态与近期问题清单、启动准备、自动更新、独立包更新及隧道状态。
+- `updates/`:近期更新迁移日志。
+
+每个文件读取尾部最多 96 KiB;错误日志最多 8 份、崩溃日志最多 4 份、处理问题清单最多 8 份、迁移日志最多 2 份。截断和读取失败进入收集回执,单个不可读文件不影响其他证据。JSON 与 JSONL 分别解析,正在写入的残行不进入共享报告。日志保留周期沿用半年及自然月分段策略。
+
+ZIP 不包含 ledger、配置文件、私钥、登录 Cookie 或媒体文件。路径、网址、地址、凭据字段及命令行凭据值脱敏;处理记录中的媒体名称隐藏。故障注入的默认日志落在测试进程临时目录。
+
+## AV1 样本
+
+样本 SHA256 为 `97e67a7e9521de3771480ed4f4ffe21461f3d4d13d95e68bc46618524461ace0`,体积 55,805,806 字节,视频为 AV1、1280×700、26 fps、585.153855 秒。
+
+未包含 `libdav1d` 的 FFmpeg 9.0.2 工具返回退出码 `69`,stderr 包含 `Your platform doesn't support hardware accelerated AV1 decoding` 和 `Function not implemented`。`scripts/build_ffmpeg_windows.sh` 静态编入固定版 dav1d;完整源码归档、许可证和 SHA256 清单通过 `media_tool_bundle.py --validate-only`。
+
+自构建工具对同一原文件完成全部 9 个采样点及 1440×789 九宫格,原媒体只读。发行验收使用内嵌的合成 AV1 片段,在 CPU 上完成解码和抽帧,不依赖机器显卡或 AV1 编码器。
+
+其余 52 项媒体未取得,不能据一个样本断言全部失败原因。AV1 修复随包含该工具构建的发行包交付;外部用户侧安装与剩余媒体重试尚未验证。
+
+## 影响面
+
+| 影响面 | 适用性 |
+| --- | --- |
+| 数据层 | 处理回执增加诊断证据;没有迁移或真实 ledger 写入 |
+| API/契约 | 5xx 关联编号、浏览器错误字段及授权上报;诊断 ZIP 内容扩展 |
+| 页面 | 共用浏览器异常入口采集上下文;没有页面布局改动 |
+| 反向状态 | 失败项重试成功后清除当前失败集合,历史错误按时间保留 |
+| 视口 | 桌面及手机使用同一采集模块和接口;布局沿用浏览器回归 |
+| 测试 | 故障注入、导出回读、权限、截断、脱敏、真实 FFmpeg 与发行源码门槛 |
+| 文档 | README、STATUS、REUSE 和本核验记录 |
+| 智能体入口 | 没有新增规则、技能或入口 |
+| 生产 | 本机源码运行态按托盘入口重启并严格校验 HTTPS;外部测试包发布独立验收 |
diff --git a/docs/REUSE.md b/docs/REUSE.md
index 94ad5bb2c..219733d35 100644
--- a/docs/REUSE.md
+++ b/docs/REUSE.md
@@ -547,6 +547,10 @@ Python、npm 与 GitHub Actions 的版本由 `.github/dependabot.yml` 每周检
Windows 媒体子进程复用 Python 标准库 `subprocess.CREATE_NO_WINDOW`:入库探测、抽帧、拼图、预览、关注封面与媒体修复均显式设置,输出捕获、超时和取消按各入口原有契约处理。该标记由 Windows 原生支持,不引入依赖;见 [Python 文档](https://docs.python.org/3/library/subprocess.html#subprocess.CREATE_NO_WINDOW) 与 [Windows 创建标记](https://learn.microsoft.com/en-us/windows/win32/procthread/process-creation-flags)。
+抽帧与拼图通过现有 Pillow 12.3.0(HPND)完整解码输出图像,不按压缩文件大小推断有效性。复用既有 FFmpeg、色彩元数据重试与 `support_bundle`:失败记录包含采样秒数、时长、退出码、超时、工具来源和各次尝试的错误输出,错误原文截断并脱敏。诊断 ZIP 收集处理状态、最近 8 份问题清单及启动、更新、隧道证据,每文件最多读取尾部 96 KiB,媒体名称留在本机。有效的不足 1 KiB JPEG、中文路径、失败重试和诊断导出均有回归;FFmpeg 参数依据[官方文档](https://ffmpeg.org/ffmpeg.html),不引入 ffmpeg-python 等命令包装层,不写真实 ledger。核验范围见 [日志与诊断核验](DIAGNOSTICS_AUDIT.md)。
+
+Windows FFmpeg 9.0.2 静态链接 VideoLAN dav1d 1.5.4(BSD-2-Clause,官方源码 1,038,852 字节,Meson + NASM,支持 Windows x64 与 8/10-bit AV1)。版本、HTTPS 来源与 SHA256 固定在 `scripts/ffmpeg-sources.json`,源码和许可证随对应源码归档分发;完整工具的 FFmpeg 二进制增加约 3 MiB,不新增 Python 运行依赖。原生 `av1` 包装器依赖硬件解码,浏览器可播放不能作为 FFmpeg 软件解码证据;CPU 解码直接采用维护中的 dav1d,不为当前仅需解码的链路引入 libaom 编码库。对照样本为 585.153855 秒的 AV1 视频,带 dav1d 的自构建工具已完成 9 帧与 1440×789 拼图;其余 52 项媒体未取得。构建参数依据 [FFmpeg libdav1d 文档](https://ffmpeg.org/ffmpeg-codecs.html#libdav1d) 和 [VideoLAN 固定发行源码](https://download.videolan.org/pub/videolan/dav1d/1.5.4/)。
+
Python 复用 MIT 许可的 [Ruff](https://docs.astral.sh/ruff/) 0.17.0,版本由开发依赖和 `uv.lock` 固定。规则配置在 `pyproject.toml`;正式入口通过 `tests/test_python_lint.py` 扫描全仓自有 Python 文件,检查 Pyflakes、Ruff 支持的 Pylint 错误规则与四项常见行为隐患。Ruff 的 Pylint 规则覆盖是部分覆盖;需要跨模块推断的问题仍由对应行为回归验证。
复用现有 Oxlint 1.85.0 的 correctness、`eqeqeq` 与 `oxc/no-accumulating-spread`,不新增依赖。源码和测试共用基础规则;shadcn 设计规则仅作用于自有 React 源码。
diff --git a/docs/STATUS.md b/docs/STATUS.md
index 7ff895de0..72c8def8a 100644
--- a/docs/STATUS.md
+++ b/docs/STATUS.md
@@ -33,7 +33,7 @@
- Python 最低与默认 3.14;Windows FFmpeg/ffprobe 在 `peach-data/tools/ffmpeg`,macOS 走 PATH。
- amane 0.20.0,安装一致;[功能与接入](reference-snapshots/upstream-review.md)。
- 发行名 `peach`、目录名 `peach-app`;Mac 维护与重启见待办 26。
-- 扫描、采集 120 秒无进展预警;资料 90 秒、封面 240 秒超时可重试;见 `state/library-processing-<job_id>.issues.jsonl`。
+- 扫描/采集无进展 120 秒预警;资料 90 秒、封面 240 秒超时可重试;[诊断与 AV1 核验](DIAGNOSTICS_AUDIT.md)。
- 资料逐项提交,页面按结果版本刷新;女优资料后继每轮存量至多 16 条。
- 下载后处理:16 条旧失败任务保留,部分资产已移动;`task_run` 外键异常 242 条未清理。
- [标签归一](reference-snapshots/tag-vocabulary-audit.md)
diff --git a/frontend/src/error-reporting.ts b/frontend/src/error-reporting.ts
index 7b5948f15..46ae40559 100644
--- a/frontend/src/error-reporting.ts
+++ b/frontend/src/error-reporting.ts
@@ -7,7 +7,7 @@ export function installErrorReporting(): void {
let start = Date.now();
let count = 0;
const seen = new Set<string>();
- const send = (kind: string, cause: unknown, fallback = '') => {
+ const send = (kind: string, cause: unknown, fallback = '', source?: ErrorEvent) => {
if (Date.now() - start >= 60_000) {
start = Date.now(); count = 0; seen.clear();
}
@@ -20,9 +20,11 @@ export function installErrorReporting(): void {
seen.add(key); count += 1;
void fetch('/api/diagnostics/client-error', {
method: 'POST', headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ kind, message, stack }), credentials: 'same-origin', keepalive: true,
+ body: JSON.stringify({ kind, message, stack, page: window.location.pathname,
+ source_file: source?.filename?.split('/').pop()?.split('?')[0],
+ line: source?.lineno, column: source?.colno }), credentials: 'same-origin', keepalive: true,
}).catch(() => {});
};
- window.addEventListener('error', event => send('error', event.error, event.message));
+ window.addEventListener('error', event => send('error', event.error, event.message, event));
window.addEventListener('unhandledrejection', event => send('unhandledrejection', event.reason, 'Promise 未处理的异常'));
}
diff --git a/frontend/test/error-reporting.test.ts b/frontend/test/error-reporting.test.ts
index 63a666401..1ea957376 100644
--- a/frontend/test/error-reporting.test.ts
+++ b/frontend/test/error-reporting.test.ts
@@ -13,11 +13,15 @@ it('浏览器异常有上限、去重,网络失败不会触发递归上报', a
installErrorReporting(); installErrorReporting();
expect(listeners.size).toBe(2);
const error = new Error('render failed');
- listeners.get('error')!({ error, message: error.message });
- listeners.get('error')!({ error, message: error.message });
+ const event = { error, message: error.message,
+ filename: 'https://peach.example/app-abc.js?token=private-token', lineno: 21, colno: 8 };
+ listeners.get('error')!(event);
+ listeners.get('error')!(event);
expect(fetch).toHaveBeenCalledTimes(1);
expect(fetch.mock.calls[0]![0]).toBe('/api/diagnostics/client-error');
expect(JSON.parse(fetch.mock.calls[0]![1].body)).toMatchObject({ kind: 'error', message: 'render failed' });
+ expect(JSON.parse(fetch.mock.calls[0]![1].body)).toMatchObject({ source_file: 'app-abc.js', line: 21, column: 8 });
+ expect(fetch.mock.calls[0]![1].body).not.toContain('private-token');
for (let i = 0; i < 30; i += 1) listeners.get('unhandledrejection')!({ reason: `failure ${i}` });
expect(fetch).toHaveBeenCalledTimes(10);
await Promise.resolve();
diff --git a/scripts/build_ffmpeg_windows.sh b/scripts/build_ffmpeg_windows.sh
index b7df4773b..a2806cc4b 100644
--- a/scripts/build_ffmpeg_windows.sh
+++ b/scripts/build_ffmpeg_windows.sh
@@ -3,7 +3,7 @@
set -euo pipefail
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
output="$(realpath -m -- "${1:?用法: build_ffmpeg_windows.sh <输出目录>}")"
-for tool in python3 make nasm pkg-config clang x86_64-w64-mingw32-gcc x86_64-w64-mingw32-strip; do
+for tool in python3 make nasm pkg-config clang meson ninja x86_64-w64-mingw32-gcc x86_64-w64-mingw32-strip; do
command -v "$tool" >/dev/null || { echo "缺少构建工具: $tool" >&2; exit 1; }
done
python3 "$script_dir/fetch_ffmpeg_sources.py" prepare "$output"
@@ -14,12 +14,19 @@ prefix="$work/prefix"
nasm -v
clang --version
make --version
+ meson --version
+ ninja --version
python3 --version
uname -a
} > "$output/build-environment.txt"
export PKG_CONFIG_PATH=
export PKG_CONFIG_LIBDIR="$prefix/lib/pkgconfig"
mkdir -p "$prefix" "$output/ffmpeg/bin"
+meson setup "$work/dav1d-build" "$work/dav1d" --cross-file "$script_dir/dav1d-mingw64.ini" \
+ --prefix "$prefix" --libdir lib --buildtype release --default-library static \
+ -Denable_tools=false -Denable_tests=false
+meson compile -C "$work/dav1d-build"
+meson install -C "$work/dav1d-build"
cd "$work/x264"
./configure --host=x86_64-w64-mingw32 --cross-prefix=x86_64-w64-mingw32- \
--prefix="$prefix" --enable-static --disable-cli --disable-opencl \
@@ -33,7 +40,7 @@ cd "$work/ffmpeg"
--cross-prefix=x86_64-w64-mingw32- --enable-cross-compile \
--disable-autodetect --disable-shared --enable-static --disable-debug \
--disable-doc --disable-ffplay --disable-pthreads --enable-w32threads \
- --enable-gpl --enable-version3 --disable-nonfree --enable-libx264 \
+ --enable-gpl --enable-version3 --disable-nonfree --enable-libx264 --enable-libdav1d \
--enable-cuda-llvm \
--enable-ffnvcodec --enable-cuda --enable-cuvid --enable-nvenc --enable-nvdec \
--enable-d3d11va --enable-dxva2 --enable-schannel \
diff --git a/scripts/dav1d-mingw64.ini b/scripts/dav1d-mingw64.ini
new file mode 100644
index 000000000..de1b057a5
--- /dev/null
+++ b/scripts/dav1d-mingw64.ini
@@ -0,0 +1,16 @@
+[binaries]
+c = 'x86_64-w64-mingw32-gcc'
+cpp = 'x86_64-w64-mingw32-g++'
+ar = 'x86_64-w64-mingw32-ar'
+strip = 'x86_64-w64-mingw32-strip'
+windres = 'x86_64-w64-mingw32-windres'
+pkg-config = 'pkg-config'
+
+[host_machine]
+system = 'windows'
+cpu_family = 'x86_64'
+cpu = 'x86_64'
+endian = 'little'
+
+[properties]
+needs_exe_wrapper = true
diff --git a/scripts/fetch_ffmpeg_sources.py b/scripts/fetch_ffmpeg_sources.py
index 494979348..96c75a291 100644
--- a/scripts/fetch_ffmpeg_sources.py
+++ b/scripts/fetch_ffmpeg_sources.py
@@ -21,8 +21,8 @@ def digest(path: Path) -> str:
def load_sources(path: Path = LOCK_PATH) -> dict:
lock = json.loads(path.read_text(encoding="utf-8"))
- if {source["name"] for source in lock["sources"]} != {"ffmpeg", "x264", "nv-codec-headers"}:
- raise ValueError("源码清单必须包含全部三个依赖")
+ if {source["name"] for source in lock["sources"]} != {"ffmpeg", "x264", "nv-codec-headers", "dav1d"}:
+ raise ValueError("源码清单必须包含全部四个依赖")
for source in lock["sources"]:
if len(source["sha256"]) != 64 or any(c not in "0123456789abcdef" for c in source["sha256"]):
raise ValueError(f"无效源码 SHA256: {source['name']}")
@@ -64,7 +64,7 @@ def finish(output: Path, lock: dict) -> dict:
work = output / "work"
shutil.copyfile(work / "ffmpeg/COPYING.GPLv3", bundle / "LICENSE.txt")
for source, license_name in (("ffmpeg", "COPYING.LGPLv2.1"), ("ffmpeg", "COPYING.LGPLv3"),
- ("x264", "COPYING")):
+ ("x264", "COPYING"), ("dav1d", "COPYING")):
shutil.copyfile(work / source / license_name, bundle / f"LICENSE-{source}-{license_name}.txt")
header_notices = []
for header in sorted((work / "nv-codec-headers/include/ffnvcodec").glob("*.h")):
@@ -77,7 +77,7 @@ def finish(output: Path, lock: dict) -> dict:
"# FFmpeg Windows x64\n\n"
"FFmpeg 9.0.2 与 libx264 按 GPL-3.0-or-later 分发。\n"
"版权与完整许可证见本目录 LICENSE 文件及对应源码归档。\n"
- "编解码使用 FFmpeg 内置实现与静态 libx264;NVIDIA 接口使用 nv-codec-headers。\n"
+ "编解码使用 FFmpeg 内置实现、静态 libx264 与 BSD-2-Clause 许可的 dav1d AV1 软件解码器;NVIDIA 接口使用 nv-codec-headers。\n"
"Windows 系统库由操作系统提供;NVIDIA 驱动由用户环境提供。\n"
"支持 NVENC、CUVID、NVDEC、D3D11VA、DXVA2 与 Schannel HTTPS。\n"
"包含 CUDA LLVM 与 scale_cuda;CUDA 解码和 NVENC 可用性取决于本机驱动。\n"
@@ -89,9 +89,9 @@ def finish(output: Path, lock: dict) -> dict:
archive = output / "ffmpeg-corresponding-source.zip"
instructions = (
"# Windows x64 FFmpeg 对应源码\n\n"
- "包含三份未经修改的上游源码归档和构建脚本。\n"
+ "包含四份未经修改的上游源码归档和构建脚本。\n"
"Linux/Ubuntu 安装:sudo apt-get install build-essential gcc-mingw-w64-x86-64-posix "
- "mingw-w64-x86-64-dev nasm pkg-config python3 clang ca-certificates xz-utils\n\n"
+ "mingw-w64-x86-64-dev nasm pkg-config python3 clang meson ninja-build ca-certificates xz-utils\n\n"
'在此目录执行:bash scripts/build_ffmpeg_windows.sh "$PWD/rebuilt"\n'
"离线重建:先建立 rebuilt/sources,再将 sources/ 内全部归档复制进去,再执行上述命令。\n"
"产物为 rebuilt/ffmpeg 与 rebuilt/ffmpeg-corresponding-source.zip。\n"
@@ -103,7 +103,7 @@ def finish(output: Path, lock: dict) -> dict:
if digest(path) != source["sha256"]:
raise ValueError(f"源码 SHA256 不匹配: {source['name']}")
package.write(path, f"sources/{path.name}")
- for name in ("build_ffmpeg_windows.sh", "fetch_ffmpeg_sources.py", "ffmpeg-sources.json"):
+ for name in ("build_ffmpeg_windows.sh", "fetch_ffmpeg_sources.py", "ffmpeg-sources.json", "dav1d-mingw64.ini"):
package.write(SCRIPT_DIR / name, f"scripts/{name}")
for license_file in sorted(bundle.glob("LICENSE*")):
package.write(license_file, f"licenses/{license_file.name}")
diff --git a/scripts/ffmpeg-sources.json b/scripts/ffmpeg-sources.json
index 22c57de33..bafff147c 100644
--- a/scripts/ffmpeg-sources.json
+++ b/scripts/ffmpeg-sources.json
@@ -2,6 +2,13 @@
"version": "9.0.2",
"license": "GPL-3.0-or-later",
"sources": [
+ {
+ "name": "dav1d",
+ "revision": "1.5.4",
+ "url": "https://download.videolan.org/pub/videolan/dav1d/1.5.4/dav1d-1.5.4.tar.xz",
+ "sha256": "686616b7c69eb88d44459391ab25cac13b6647a3b288835c5784e71c1514a5c5",
+ "archive": "dav1d-1.5.4.tar.xz"
+ },
{
"name": "ffmpeg",
"revision": "n9.0.2",
diff --git a/scripts/media_tool_bundle.py b/scripts/media_tool_bundle.py
index 97128d8ec..750d4a689 100644
--- a/scripts/media_tool_bundle.py
+++ b/scripts/media_tool_bundle.py
@@ -11,7 +11,7 @@ import shutil
REQUIRED_FILES = {"bin/ffmpeg.exe", "bin/ffprobe.exe", "LICENSE.txt", "NOTICE.md", "buildconf.txt"}
POLICIES = {
- "ffmpeg": (REQUIRED_FILES, {"ffmpeg", "x264", "nv-codec-headers"}, "GPL-3.0-or-later"),
+ "ffmpeg": (REQUIRED_FILES, {"ffmpeg", "x264", "nv-codec-headers", "dav1d"}, "GPL-3.0-or-later"),
"untrunc": ({"untrunc.exe", "LICENSE.txt", "NOTICE.md", "buildconf.txt"},
{"untrunc", "ffmpeg"}, "GPL-2.0-or-later"),
}
diff --git a/scripts/smoke_desktop.py b/scripts/smoke_desktop.py
index 358f7316c..227f952a2 100644
--- a/scripts/smoke_desktop.py
+++ b/scripts/smoke_desktop.py
@@ -2,6 +2,7 @@
from __future__ import annotations
import argparse
+import base64
from contextlib import contextmanager
import ctypes
from ctypes import wintypes
@@ -23,6 +24,35 @@ import urllib.parse
import uuid
+# 合成 64×64 testsrc2 的两帧 AV1;不依赖发行工具包含 AV1 编码器。
+_AV1_SAMPLE = (
+ 'REtJRgAAIABBVjAxQABAAAIAAAABAAAAAgAAAAAAAAAQAgAAAAAAAAAAAAASAAoKAAAAAq//m18gCDL/AxQAMoAY'
+ 'YQeKAgrbccEajCcFwrWZWZAcjk7HsQkwz6bl6C/RIMrhL7TGShgVqQJwYehp8Dk3NGsPnZpd9H/kMuUNCln4V/8Q'
+ 'sNsk8IXFEYPBxm3hH5FrNUGl0JHoXb5So71sC0w8QV8tnCFJTJAa+GuYX8XXKJuJpyg/HBFtHqiEEkLALQ/XUqV1'
+ 'LrdrAMGJ49bvAgoNjl5FDPy+4wDlFJYlV4y1RiocKER6pIiZK2g+N/lGJorkKg0vsmoI3olvguuTNs0+xIxZfToS'
+ '7Y0UK7UCr7w5NzJEkZ0kpLbVot5PtEax3tOtvGE6FBU1kZfmKMQcB8OYl18JooM7zn1mYiNVVmYSOVeICjSlAXFj'
+ 'M4M8NDeILhqcheLaAJ1q53QozK7pAYwIKBS4AF8DjvvAVnxT9VLa2a+5RGqL2XT1aJ2Pfyh6kvsGhib/5pYSn4Xk'
+ 'OrP/XNtfE0Pdli4ram0vyYdaMWWmkqgxDMMLEqnWXdWn/+2RVPAdGZV//gWLaIS/BVQ92TcmFE0jZfAu6lFZa9kr'
+ 'ejynNQpdnX6BtyCsXsCXFuFjWviueEYWX9kN3KxKcmYIqae/8rioTK/koM1WIkuN7wIr0/+YE4v9eqfwkRqGKG7u'
+ 'GF3ydM6SqhvPoE2YvyUjw7lkOwzOzma5AOOux53IQlzAEigiskKcks4nv4AOAQAAAQAAAAAAAAASADKJAjIB4EAA'
+ 'AANoADDEExRddQDdOU0PJApEvWVmn/OPmFU6GzRhHf19xENKaaOLsjU3y6xIX+RghuEbLRK9wN2yvwikcZaOrZTr'
+ 'eEt5qhzR3F4YxcfTJakEEe4U6a9FIaWXxEAwrn4RU+3r7JnNsIifqcHcrsC4HVxAe8KcHtW+fmENmZgnhfwLdXp+'
+ 'LhgOZVki5u57z8TbVswnUJYINlRS2IMU9Tua7xL8EFwM+rxDyvaP0X3Rht73L1z0NvyYzeUaigPsLRrLvEwWp+BE'
+ 'UijJPuANY7UCckwZAo2GKlk3pY+wW5Zgw6aLcZpBzsDk8oZjBW13QxHQHremUttJSFHH4bQZ+D8cmQFv8BA='
+)
+
+
+def assert_av1_decoder(ffmpeg: Path, root: Path) -> None:
+ """在 CPU 上验证随包 AV1 解码与缩图,不依赖显卡。"""
+ source, image = root / 'av1.ivf', root / 'av1-frame.jpg'
+ source.write_bytes(base64.b64decode(_AV1_SAMPLE))
+ result = subprocess.run([str(ffmpeg), '-v', 'error', '-c:v', 'libdav1d', '-i', str(source),
+ '-vf', 'scale=480:-1', '-frames:v', '1', str(image)],
+ capture_output=True, timeout=30)
+ assert result.returncode == 0, result.stderr.decode('utf-8', 'replace')[-4000:]
+ assert image.is_file() and image.stat().st_size > 0, 'AV1 抽帧未取得'
+
+
def assert_bundled_media_tools(application: Path) -> None:
"""在没有系统媒体工具的机器上验证随包转码、探测和抽帧。"""
tools = application / "tools" / "ffmpeg"
@@ -59,6 +89,7 @@ def assert_bundled_media_tools(application: Path) -> None:
capture_output=True, text=True, encoding="utf-8", timeout=15, check=True)
assert json.loads(result.stdout)["streams"][0]["codec_name"] == "h264"
assert image.stat().st_size > 0
+ assert_av1_decoder(ffmpeg, root)
# 合成片的 moov 位于 mdat 之后;移掉索引后用同编码参数的完整片重建。
payload = video.read_bytes()
offset = 0
diff --git a/scripts/test_runner.py b/scripts/test_runner.py
index e1092502e..d6de4e88d 100644
--- a/scripts/test_runner.py
+++ b/scripts/test_runner.py
@@ -853,4 +853,8 @@ def verified_run(args, requested, scopes, files) -> int:
if __name__ == "__main__":
- raise SystemExit(main())
+ # 故障注入的默认错误日志属于当前测试进程,不能写进真实数据目录。
+ from peach import error_reporting
+ with tempfile.TemporaryDirectory(prefix='peach-test-errors-') as error_logs:
+ error_reporting._log_dir = Path(error_logs)
+ raise SystemExit(main())
diff --git a/src/peach/api.py b/src/peach/api.py
index c9d8c6a67..9f8f0a420 100644
--- a/src/peach/api.py
+++ b/src/peach/api.py
@@ -20,6 +20,7 @@
"""
import asyncio
import logging
+import uuid
from concurrent.futures import ThreadPoolExecutor
from contextlib import asynccontextmanager
from urllib.parse import quote
@@ -188,6 +189,17 @@ async def _warm_ledger_aggregates(ledger_ready: bool, contract, stopping: asynci
LOGGER.debug("aggregate warmup failed: %s", path, exc_info=True)
+def _server_error_headers(request: Request, error: Exception, status_code=500):
+ """服务错误按路由模板关联;请求参数、正文与鉴权头留在请求中。"""
+ if status_code < 500:
+ return {}
+ from .error_reporting import event
+ error_id = uuid.uuid4().hex
+ event('http-request', error, error_id=error_id, method=request.method,
+ route=getattr(request.scope.get('route'), 'path', '<unmatched>').lstrip('/'), status_code=status_code)
+ return {'X-Peach-Error-ID': error_id, 'Cache-Control': 'no-store'}
+
+
def create_app(
settings: PeachSettings | None = None,
sync: LedgerSync | None = None,
@@ -461,6 +473,7 @@ def create_app(
# 只挂子类的话地址栏敲错一个字母仍会看到一行 JSON。
@app.exception_handler(StarletteHTTPException)
def _http_exception_handler(request: Request, exc: StarletteHTTPException):
+ headers = _server_error_headers(request, exc, exc.status_code)
detail = exc.detail
if isinstance(detail, dict):
# 表单校验那种带字段明细的 4xx:`message` 是总话,其余键原样带给页面。
@@ -473,8 +486,14 @@ def create_app(
and "text/html" in request.headers.get("accept", ""))
if navigation:
return HTMLResponse(routes_pages.error_page(exc.status_code, body["error"]),
- status_code=exc.status_code)
- return JSONResponse(body, status_code=exc.status_code)
+ status_code=exc.status_code, headers=headers)
+ return JSONResponse(body, status_code=exc.status_code, headers=headers)
+
+ @app.exception_handler(Exception)
+ def _server_exception_handler(request: Request, exc: Exception):
+ headers = _server_error_headers(request, exc)
+ return JSONResponse({'error': '服务处理失败,请导出诊断报告',
+ 'error_id': headers['X-Peach-Error-ID']}, status_code=500, headers=headers)
# 第三方前端依赖固定版本并随 Peach 自托管;局域网断网时仍可播放。
app.mount(
diff --git a/src/peach/diagnostics.py b/src/peach/diagnostics.py
index b6e47547d..87f8d846c 100644
--- a/src/peach/diagnostics.py
+++ b/src/peach/diagnostics.py
@@ -13,7 +13,7 @@ import time
from contextlib import closing
from . import __version__, access, distribution, settings_file
-from .ffmpeg import FFmpegResolver
+from .ffmpeg import FFmpegResolver, binary_diagnostics
from .health import inspect_database
from .mount_reachability import MountReachability, PROBE_TIMEOUT
@@ -70,19 +70,25 @@ def database_checks(settings):
return database, schema
-def tools(settings):
+def tools(settings, *, inspect=True):
resolver = FFmpegResolver(settings.ffmpeg_root)
found = {}
for name, choice in (("ffmpeg", resolver.ffmpeg()), ("ffprobe", resolver.ffprobe())):
- found[name] = {"available": choice is not None, "source": choice.source if choice else None,
- "path": str(choice.path) if choice else None}
+ found[name] = (binary_diagnostics(choice, decoders=name == 'ffmpeg') if inspect else
+ {'available': choice is not None, 'source': choice.source if choice else None,
+ 'path': str(choice.path) if choice else None})
openssl = shutil.which("openssl")
found["openssl"] = {"available": openssl is not None, "source": "path" if openssl else None,
"path": openssl}
missing = [name for name, value in found.items() if not value["available"]]
- return item("FFmpeg/OpenSSL", "warning" if missing else "ok",
- "未找到:" + "、".join(missing) if missing else "工具路径已找到",
- action="安装缺失工具" if missing else "", target="/settings/maintenance" if missing else None, details=found)
+ unavailable = [name for name in ('ffmpeg', 'ffprobe') if found[name]['available'] and not found[name].get('usable', True)]
+ incomplete = found['ffmpeg'].get('usable') and not found['ffmpeg'].get('av1_software')
+ problems = (["未找到:" + "、".join(missing)] if missing else [])
+ problems += (["无法执行:" + "、".join(unavailable)] if unavailable else [])
+ problems += ['FFmpeg 缺少 AV1 软件解码器'] if incomplete else []
+ return item("FFmpeg/OpenSSL", 'warning' if problems else 'ok',
+ ';'.join(problems) or ('媒体工具可执行,AV1 软件解码器可用' if inspect else '工具路径已找到'),
+ action='更新媒体工具' if problems else '', target='/settings/maintenance' if problems else None, details=found)
def security(settings):
@@ -112,12 +118,12 @@ def media_mounts(snapshot):
details=snapshot)
-def component_checks(settings, snapshot, *, config=None, validate_config=True):
+def component_checks(settings, snapshot, *, config=None, validate_config=True, inspect_tools=True):
config = config or settings_file.active()
database, schema = database_checks(settings)
checks = {"database": database, "schema": schema, "configured": configuration(config, settings.configured, validate=validate_config),
"media_mounts": media_mounts(snapshot), "security": security(settings)}
- checks["ffmpeg"] = tools(settings)
+ checks["ffmpeg"] = tools(settings, inspect=inspect_tools)
return checks
@@ -198,6 +204,6 @@ def report(settings, snapshot, *, config=None):
def health_components(settings, snapshot):
- checks = component_checks(settings, snapshot, validate_config=False)
+ checks = component_checks(settings, snapshot, validate_config=False, inspect_tools=False)
# 公开健康检查只保留分项原因;工具路径与详细修法由本机诊断提供。
return {key: {name: value[name] for name in ("status", "reason")} for key, value in checks.items()}
diff --git a/src/peach/error_reporting.py b/src/peach/error_reporting.py
index 9b8436b18..c6b871045 100644
--- a/src/peach/error_reporting.py
+++ b/src/peach/error_reporting.py
@@ -28,6 +28,10 @@ def redact(value: str) -> str:
text = str(value).encode("utf-8", errors="backslashreplace").decode("utf-8")
text = re.sub(r"(?i)\b(?:https?|ftp|file)://[^\s<>\"']+", "<url>", text)
text = re.sub(r"(?i)\b(?:bearer|basic)\s+[\w+/=.-]+", "<credential>", text)
+ text = re.sub(
+ r"(?i)(--(?:[a-z0-9_]+-)*(?:password|passwd|token|api[_-]?key|secret|cookie|authorization|access[_-]?key)\s+)"
+ r"(?:\"[^\"]*\"|'[^']*'|[^\s,;}]+)", r"\1<redacted>", text,
+ )
text = re.sub(r"(?im)(\b(?:cookie|set-cookie|authorization)[\"']?\s*:\s*)[^\r\n]*", r"\1<redacted>", text)
text = re.sub(
r"(?i)([\"']?(?:password|passwd|token|api[_-]?key|secret|cookie|authorization|access[_-]?key)"
@@ -113,7 +117,7 @@ def exception_facts(error: BaseException) -> dict:
def event(stage: str, error: BaseException | None = None, **fields) -> Path | None:
record = {"at": datetime.now(timezone.utc).isoformat(), "stage": stage,
- **system_facts(), **fields}
+ **system_facts(), 'thread': threading.current_thread().name, **fields}
if error is not None:
record.update(exception_facts(error))
payload = json.dumps(scrub(record), ensure_ascii=False, default=str) + "\n"
@@ -130,6 +134,15 @@ def event(stage: str, error: BaseException | None = None, **fields) -> Path | No
return None
+def subprocess_failure(stage: str, *, tool: str, error: BaseException | None = None,
+ returncode=None, stderr=b'', **fields) -> Path | None:
+ """媒体子进程仅保留工具名、错误码和有界错误输出,不记录完整命令。"""
+ raw = stderr or getattr(error, 'stderr', b'') or b''
+ text = raw.decode('utf-8', 'replace') if isinstance(raw, bytes) else str(raw)
+ return event(stage, error, tool=Path(tool).name, returncode=returncode,
+ stderr=text[-4000:], **fields)
+
+
class ErrorHandler(logging.Handler):
def emit(self, record: logging.LogRecord) -> None:
try:
diff --git a/src/peach/ffmpeg.py b/src/peach/ffmpeg.py
index 3de65337b..8e741b9c8 100644
--- a/src/peach/ffmpeg.py
+++ b/src/peach/ffmpeg.py
@@ -2,6 +2,7 @@ from __future__ import annotations
import os
import shutil
+import subprocess
import sys
from dataclasses import dataclass
from pathlib import Path
@@ -13,6 +14,33 @@ class BinaryChoice:
source: str
+def binary_diagnostics(choice: BinaryChoice | None, *, decoders=False) -> dict:
+ """检查实际工具版本与 AV1 软件解码器;路径存在和可执行分别报告。"""
+ facts = {'available': choice is not None, 'source': choice.source if choice else None,
+ 'path': str(choice.path) if choice else None, 'usable': False}
+ if choice is None:
+ return facts
+ try:
+ for option in ('-version', '-decoders') if decoders else ('-version',):
+ result = subprocess.run([str(choice.path), '-hide_banner', option], capture_output=True,
+ timeout=5, creationflags=subprocess.CREATE_NO_WINDOW if os.name == 'nt' else 0)
+ if result.returncode:
+ facts.update(returncode=result.returncode, error=(result.stderr or b'').decode('utf-8', 'replace')[-2000:])
+ return facts
+ output = result.stdout.decode('utf-8', 'replace')
+ if option == '-version':
+ facts['version'] = output.splitlines()[0][:300] if output.splitlines() else ''
+ else:
+ names = {parts[1] for line in output.splitlines()
+ if len(parts := line.split()) >= 2 and parts[0].startswith('V')}
+ facts['av1_software'] = bool(names & {'libdav1d', 'libaom-av1'})
+ facts['usable'] = bool(facts.get('version'))
+ except (OSError, subprocess.TimeoutExpired) as error:
+ facts.update(error=type(error).__name__, errno=getattr(error, 'errno', None),
+ winerror=getattr(error, 'winerror', None), timeout_seconds=5)
+ return facts
+
+
class FFmpegResolver:
"""按显式配置、随包工具、数据目录与 PATH 查找媒体工具。"""
diff --git a/src/peach/frame_capture.py b/src/peach/frame_capture.py
index 77078b1d2..a699460a1 100644
--- a/src/peach/frame_capture.py
+++ b/src/peach/frame_capture.py
@@ -12,6 +12,18 @@ import re
import subprocess
from pathlib import Path
+from PIL import Image
+
+
+def _readable_image(path: Path) -> bool:
+ """图片须完整可解码;纯色 JPEG 也算有效画面。"""
+ try:
+ with Image.open(path) as image:
+ image.load()
+ return image.width > 0 and image.height > 0
+ except (OSError, ValueError, Image.DecompressionBombError):
+ return False
+
#: 部分网盘视频把色彩原色写成 reserved(非法值),swscale 会拒绝缩放;声明为 bt709
#: 只是覆盖坏的元数据,不改动像素。
COLOR_OVERRIDE = ["-color_primaries", "bt709", "-color_trc", "bt709", "-colorspace", "bt709"]
@@ -27,12 +39,13 @@ COLOR_METADATA_ERROR = re.compile(
def capture_frame(ffmpeg: str, path: str, timestamp: float, destination: Path, *,
width: int = 480, color_override: bool = False,
- timeout: float = 45) -> tuple[bool, str]:
+ timeout: float = 45, diagnostics: dict | None = None) -> tuple[bool, str]:
"""抽一帧,返回(是否成功, stderr)。stderr 用于判断值不值得重试。
`-ss` 在 `-i` 之前:那是输入端 seek,只解目标附近的帧。放到后面就是从头解码到
那个时间点,两小时的片子抽最后一帧要几分钟。
"""
+ destination.unlink(missing_ok=True)
command = [ffmpeg, "-y", "-v", "error", "-rw_timeout", "8000000"]
if color_override:
command += COLOR_OVERRIDE
@@ -43,20 +56,46 @@ def capture_frame(ffmpeg: str, path: str, timestamp: float, destination: Path, *
try:
completed = subprocess.run(command, capture_output=True, timeout=timeout,
creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0)
- except subprocess.TimeoutExpired:
+ except subprocess.TimeoutExpired as error:
+ if diagnostics is not None:
+ diagnostics.update(reason='timeout', returncode=None,
+ stderr=(error.stderr or b'').decode('utf-8', 'replace'))
+ destination.unlink(missing_ok=True)
return False, "ffmpeg timeout"
- ok = destination.is_file() and destination.stat().st_size > 1024
- return ok, (completed.stderr or b"").decode("utf-8", "replace")
+ stderr = (completed.stderr or b'').decode('utf-8', 'replace')
+ ok = completed.returncode == 0 and _readable_image(destination)
+ if diagnostics is not None:
+ diagnostics.update(reason='success' if ok else
+ 'ffmpeg_error' if completed.returncode else 'no_frame',
+ returncode=completed.returncode, stderr=stderr)
+ if not ok:
+ destination.unlink(missing_ok=True)
+ return ok, stderr
def capture_with_retry(ffmpeg: str, path: str, timestamp: float, destination: Path,
- *, width: int = 480, timeout: float = 45) -> bool:
+ *, width: int = 480, timeout: float = 45,
+ diagnostics: dict | None = None) -> bool:
"""抽一帧;只在错误明说色彩元数据不合法时,带 bt709 声明重试一次。"""
+ attempts = []
+ detail = {}
ok, stderr = capture_frame(ffmpeg, path, timestamp, destination,
- width=width, timeout=timeout)
+ width=width, timeout=timeout, diagnostics=detail)
+ attempts.append(dict(detail, color_override=False))
if not ok and COLOR_METADATA_ERROR.search(stderr):
+ detail = {}
ok, _ = capture_frame(ffmpeg, path, timestamp, destination,
- width=width, color_override=True, timeout=timeout)
+ width=width, color_override=True, timeout=timeout, diagnostics=detail)
+ attempts.append(dict(detail, color_override=True))
+ if diagnostics is not None:
+ diagnostics.update(detail, timestamp_seconds=timestamp, timeout_seconds=timeout,
+ attempts=attempts)
+ elif not ok:
+ from .error_reporting import subprocess_failure
+ subprocess_failure('frame-capture', tool=ffmpeg, stderr=detail.get('stderr', ''),
+ returncode=detail.get('returncode'), timestamp_seconds=timestamp,
+ timeout_seconds=timeout, reason=detail.get('reason'),
+ attempts=[dict(attempt, stderr=attempt.get('stderr', '')[-4000:]) for attempt in attempts])
return ok
@@ -68,10 +107,22 @@ def tile_frames(ffmpeg: str, pattern: str, columns: int, rows: int, destination:
于是拼出来的图少了后半段,而命令本身照样退出 0。
"""
destination.parent.mkdir(parents=True, exist_ok=True)
- subprocess.run(
- [ffmpeg, "-y", "-v", "error", "-i", pattern,
- "-filter_complex", f"tile={int(columns)}x{int(rows)}", "-q:v", "4", str(destination)],
- capture_output=True, timeout=timeout,
- creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0,
- )
- return destination.is_file() and destination.stat().st_size > 4096
+ destination.unlink(missing_ok=True)
+ from .error_reporting import subprocess_failure
+ try:
+ completed = subprocess.run(
+ [ffmpeg, "-y", "-v", "error", "-i", pattern,
+ "-filter_complex", f"tile={int(columns)}x{int(rows)}", "-q:v", "4", str(destination)],
+ capture_output=True, timeout=timeout,
+ creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0,
+ )
+ except (OSError, subprocess.TimeoutExpired) as error:
+ destination.unlink(missing_ok=True)
+ subprocess_failure('frame-tile', tool=ffmpeg, error=error, timeout_seconds=timeout)
+ raise
+ ok = completed.returncode == 0 and _readable_image(destination)
+ if not ok:
+ destination.unlink(missing_ok=True)
+ subprocess_failure('frame-tile', tool=ffmpeg, stderr=completed.stderr,
+ returncode=completed.returncode, reason='tile_unavailable')
+ return ok
diff --git a/src/peach/jobs.py b/src/peach/jobs.py
index 0241d99d9..92b047b7a 100644
--- a/src/peach/jobs.py
+++ b/src/peach/jobs.py
@@ -568,6 +568,10 @@ class BackgroundJob:
"""执行一次操作并保存终态回执;重复提交只返回进行中的同一任务。"""
def work(job_id):
result = fn()
+ if not result.get('ok', True):
+ from .error_reporting import event
+ event('background-result', job_id=job_id, task_key=self.task_key,
+ message=result.get('error') or result.get('message') or '任务失败')
self.update(job_id, **result, status=(
"complete" if result.get("ok", True) else "failed"),
completed_at=time.time())
@@ -589,6 +593,9 @@ class BackgroundJob:
self.state.update(waiting_for_update=True, stage='等待安装完成后继续', error='',
message='任务已暂停,安装完成后自动继续。')
except Exception as error: # 后台失败必须变成可轮询的状态,不能只留在日志里
+ from .error_reporting import event
+ event('background-job', error, job_id=job_id, task_key=self.task_key,
+ run_id=self._run_ids.get(job_id))
# 抛出来就是没跑完:途中声明过的后继作废,结算时一条都不派。
with self.lock:
if self.state is not None and self.state.get(self.id_key) == job_id:
diff --git a/src/peach/library_processing.py b/src/peach/library_processing.py
index 353bcb407..b57b20017 100644
--- a/src/peach/library_processing.py
+++ b/src/peach/library_processing.py
@@ -769,12 +769,14 @@ def _issue_classification(message, retryable, paused):
return ('info' if note else 'paused' if paused else 'error'), retryable and not note
-def _record_issue(state, log_path, record):
+def _record_issue(state, log_path, record, *, diagnostics=None):
"""把一条记录写进日志,并按类型计进状态。
告知项只按类型各记一个数:它们占着那 20 条预览的话,一条真问题就被几百条
「来源没有这部片」挤出屏幕,而全库有大量片子任何目录站都收不到。
"""
+ if diagnostics is not None:
+ record['diagnostics'] = diagnostics
with open(log_path, 'a', encoding='utf-8') as handle:
handle.write(json.dumps(record, ensure_ascii=False) + '\n')
note = NOTE_KEYS.get(record['message'])
@@ -1802,6 +1804,20 @@ def _asset_selection(db_path, asset_ids):
return chosen
+class _PreviewCaptureError(ValueError):
+ """预览失败的操作提示及脱敏工具证据。"""
+
+ def __init__(self, diagnostics):
+ from .error_reporting import scrub
+ self.diagnostics = scrub(diagnostics)
+ message = {
+ 'timeout': '视频抽帧超时,可重试;反馈问题请导出诊断报告',
+ 'ffmpeg_error': 'FFmpeg 抽帧失败,反馈问题请导出诊断报告',
+ 'no_frame': '采样位置未取得画面,反馈问题请导出诊断报告',
+ }.get(diagnostics.get('reason'), '视频抽帧未取得,反馈问题请导出诊断报告')
+ super().__init__(message)
+
+
def _generate_preview(config, row, database, resolver, *, progress):
"""复用九宫格抽帧原语,产物可用后在共享写锁内登记。"""
from . import frame_capture, media_probe
@@ -1841,9 +1857,17 @@ def _generate_preview(config, row, database, resolver, *, progress):
return False
progress()
timestamp = duration * (0.03 + 0.94 * (index + 0.5) / 9)
+ diagnostic = {}
if not frame_capture.capture_with_retry(
- str(choice.path), str(video), timestamp, frames / f's{index:02d}.jpg'):
- raise ValueError('视频抽帧未取得,请检查媒体文件后重试')
+ str(choice.path), str(video), timestamp, frames / f's{index:02d}.jpg',
+ diagnostics=diagnostic):
+ for attempt in diagnostic.get('attempts', []):
+ attempt['stderr'] = attempt.get('stderr', '').replace(str(video), '<media>')[-4096:]
+ diagnostic['stderr'] = diagnostic.get('stderr', '').replace(str(video), '<media>')[-4096:]
+ diagnostic.update(frame_index=index, duration_seconds=duration,
+ tool_source=choice.source if hasattr(choice, 'source') else 'unknown',
+ tool_name=Path(choice.path).name)
+ raise _PreviewCaptureError(diagnostic)
progress()
with atomic_path(destination) as output:
if not frame_capture.tile_frames(str(choice.path), str(frames / 's%02d.jpg'), 3, 3, output):
@@ -1884,10 +1908,13 @@ def _generate_previews(config, rows, database, *, update, issue, guard,
except InterruptedError:
raise
except (OSError, ValueError, subprocess.SubprocessError) as error:
+ from .error_reporting import event
+ event('library-preview', error, asset_id=row['id'])
message = (str(error) if isinstance(error, ValueError) else
'生成预览图超时,请重试' if isinstance(error, subprocess.SubprocessError) else
'生成预览图失败,请检查 FFmpeg 与目录权限后重试')
- issue(row, message, action='generating_preview', retryable=True)
+ details = {'diagnostics': error.diagnostics} if isinstance(error, _PreviewCaptureError) else {}
+ issue(row, message, action='generating_preview', retryable=True, **details)
update(checked=index + 1, previews=made, current_asset_id=None, current_asset_name='',
current_action='', current_started_at=None, current_deadline_at=None)
return made
@@ -2074,7 +2101,7 @@ def process_library(config, db_path, candidate_root, cover_root, *, location='co
flush_state(force='status' in values)
report(dict(state))
- def issue(asset, message, *, action='', retryable=False, paused=False):
+ def issue(asset, message, *, action='', retryable=False, paused=False, diagnostics=None):
"""`asset` 是这一项的馆藏行,来源离线一类与具体项目无关的问题给 `None`。
每条问题都带上标题与路径:光有「NFO 无法解析」和一个链接,人得逐个点开
@@ -2088,7 +2115,7 @@ def process_library(config, db_path, candidate_root, cover_root, *, location='co
_record_issue(state, log_path, {
'asset_id': asset_id, 'title': title, 'path': asset_path, 'message': message,
'severity': severity, 'failed_action': action, 'retryable': retryable,
- 'last_failed_at': time.time()})
+ 'last_failed_at': time.time()}, diagnostics=diagnostics)
state['last_progress_at'] = time.time()
flush_state()
report(dict(state))
diff --git a/src/peach/media_probe.py b/src/peach/media_probe.py
index 2bc15491f..29f15a83f 100644
--- a/src/peach/media_probe.py
+++ b/src/peach/media_probe.py
@@ -67,6 +67,8 @@ def probe_file(ffprobe: str, path: str, timeout: float = TIMEOUT_SECONDS) -> tup
timeout=timeout,
creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0,
)
+ if result.returncode:
+ raise subprocess.CalledProcessError(result.returncode, 'ffprobe', stderr=result.stderr)
payload = json.loads(result.stdout or b"{}")
duration = float((payload.get("format") or {}).get("duration") or 0)
if duration <= 0:
@@ -93,8 +95,14 @@ def measure(ffprobe: str, asset_id: int, path: str, timeout: float = TIMEOUT_SEC
try:
duration, width, height, codec, fps, audio = probe_file(
ffprobe, resolve_case_insensitive(path), timeout)
- except Exception: # 超时、文件读不到、输出不是 JSON:对这一行都是「没拿到」
+ except Exception as error: # 超时、文件读不到、输出不是 JSON:对这一行都是「没拿到」
+ from .error_reporting import subprocess_failure
+ subprocess_failure('media-probe', tool=ffprobe, error=error, asset_id=asset_id,
+ returncode=getattr(error, 'returncode', None), timeout_seconds=timeout)
return (-1, 0, 0, None, 0, 0, None, None, None, asset_id)
+ if duration <= 0:
+ from .error_reporting import event
+ event('media-probe', asset_id=asset_id, reason='duration_unavailable')
return (duration, width, height, codec, fps, audio,
*context_fields(width, height, duration), asset_id)
diff --git a/src/peach/previews.py b/src/peach/previews.py
index 68660f32d..1dd63c0ce 100644
--- a/src/peach/previews.py
+++ b/src/peach/previews.py
@@ -346,9 +346,14 @@ class PreviewService:
result = subprocess.run(command, capture_output=True, timeout=30, check=False,
creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0)
except (OSError, subprocess.TimeoutExpired) as exc:
+ from .error_reporting import subprocess_failure
+ subprocess_failure('preview', tool=command[0], error=exc, timeout_seconds=30)
Path(command[-1]).unlink(missing_ok=True)
raise PreviewUnavailable("ffmpeg generation failed") from exc
if result.returncode != 0 or not Path(command[-1]).is_file():
+ from .error_reporting import subprocess_failure
+ subprocess_failure('preview', tool=command[0], returncode=result.returncode,
+ stderr=result.stderr, reason='generation_failed')
Path(command[-1]).unlink(missing_ok=True)
raise PreviewUnavailable("ffmpeg generation failed")
diff --git a/src/peach/routes_auth.py b/src/peach/routes_auth.py
index 3bf12311d..603a6ba51 100644
--- a/src/peach/routes_auth.py
+++ b/src/peach/routes_auth.py
@@ -174,6 +174,19 @@ def require_page_auth(request: Request) -> dict[str, str]:
return args
+def require_authenticated_client(request: Request) -> None:
+ """错误上报须有有效登录凭据,开放访问模式不提供匿名上报。"""
+ same_origin(request)
+ policy = _policy(request)
+ if policy['mode'] not in {'password', 'legacy'} or not policy.get('key'):
+ raise HTTPException(401, 'unauthorized')
+ authenticated = access.valid_session(policy, request.cookies.get(access.COOKIE, ''))
+ if policy['mode'] == 'legacy':
+ authenticated = _authorized(request, policy['key'], _first_query_values(request))
+ if not authenticated:
+ raise HTTPException(401, 'unauthorized')
+
+
def require_asset_auth(request: Request) -> dict[str, str]:
args = _first_query_values(request)
if not _authorized(request, request.app.state.settings.token, args):
diff --git a/src/peach/routes_configuration.py b/src/peach/routes_configuration.py
index e1ffecde1..55aecdb72 100644
--- a/src/peach/routes_configuration.py
+++ b/src/peach/routes_configuration.py
@@ -24,7 +24,7 @@ from filelock import FileLock, Timeout
from . import (access, distribution, entry_links, folder_picker, media_libraries, onboarding,
push_discovery, settings_file, media_configuration, tunnel, web_downloads)
-from .routes_auth import require_auth, same_origin
+from .routes_auth import require_auth, require_authenticated_client, same_origin
from .web_entry import runtime_fact_entries
from . import release_updates, standalone_update, peach_proxy, desktop_startup, desktop_uninstall
@@ -132,10 +132,11 @@ _CLIENT_ERROR_LOCK = threading.Lock()
@router.post("/api/diagnostics/client-error")
async def client_error(request: Request):
- """本机浏览器错误取证;首启与登录页也可调用。"""
+ """浏览器错误取证;本机首启与登录页允许上报,远程须登录。"""
from . import error_reporting
same_origin(request)
- local_only(request)
+ if not local_client(request):
+ require_authenticated_client(request)
with _CLIENT_ERROR_LOCK:
recent = getattr(request.app.state, "client_error_times", deque(maxlen=20))
request.app.state.client_error_times = recent
@@ -156,8 +157,11 @@ async def client_error(request: Request):
raise HTTPException(400, "错误记录格式无效") from None
if not isinstance(data, dict) or not isinstance(data.get("message"), str):
raise HTTPException(400, "错误记录缺少原因")
- fields = {key: value[:6000] for key in ("message", "stack", "kind")
+ fields = {key: value[:6000] for key in ("message", "stack", "kind", "page", "source_file")
if isinstance(value := data.get(key), str)}
+ fields.update({key: value for key in ('line', 'column')
+ if type(value := data.get(key)) is int and value >= 0})
+ fields['user_agent'] = request.headers.get('user-agent', '')[:512]
if error_reporting.event("browser", **fields) is None:
raise HTTPException(503, "错误日志无法写入")
return {"recorded": True}
diff --git a/src/peach/standalone_update.py b/src/peach/standalone_update.py
index 5fcf95926..a69709e55 100644
--- a/src/peach/standalone_update.py
+++ b/src/peach/standalone_update.py
@@ -370,6 +370,8 @@ def apply(manifest: Path, wait_pid: int) -> int:
raise TimeoutError("新版本未能就绪")
except Exception as exc:
if moved:
+ from .error_reporting import event
+ event('standalone-update-install', exc)
try:
if installed:
for window in find_tray_windows(target / "Peach.exe"):
@@ -381,8 +383,11 @@ def apply(manifest: Path, wait_pid: int) -> int:
start_tray(target / "Peach.exe")
message = f"更新失败,已恢复旧版本:{exc}"
except Exception as rollback:
+ event('standalone-update-rollback', rollback)
message = f"更新失败,回滚未完成:{rollback}"
else:
+ from .error_reporting import event
+ event('standalone-update-install', exc)
message = f"更新未安装:{exc}"
try:
restore_database(data)
@@ -391,6 +396,7 @@ def apply(manifest: Path, wait_pid: int) -> int:
if not process_alive(wait_pid):
start_tray(target / "Peach.exe")
except Exception as rollback:
+ event('standalone-update-rollback', rollback)
message = f"更新失败,数据库恢复未完成:{rollback}"
write(data, state="error", progress=0, message=message)
return 1
diff --git a/src/peach/support_bundle.py b/src/peach/support_bundle.py
index 89f6abfdc..519f01b20 100644
--- a/src/peach/support_bundle.py
+++ b/src/peach/support_bundle.py
@@ -12,10 +12,37 @@ from pathlib import Path
from . import error_reporting
LOG_NAMES = ("tray.log", "tray-lifecycle.log", "tray-setup.err.log", "tray-setup.out.log",
- "tray-http.err.log", "tray-http.out.log", "tray-https.err.log", "tray-https.out.log")
+ "tray-http.err.log", "tray-http.out.log", "tray-https.err.log", "tray-https.out.log",
+ 'tray-restart.out.log', 'tray-scan.out.log', 'windows-source-sync.log', 'cloudflare-tunnel.log')
+STATE_NAMES = ('library-processing.json', 'runtime-prepare.json', 'standalone-update.json',
+ 'automatic-updates.json', 'windows-source-sync.json', 'cloudflare-tunnel.json')
TAIL_BYTES = 96 * 1024
+def _processing_log(text: str) -> str:
+ """处理证据保留资产编号和错误,媒体名称留在本机。"""
+ def hide_names(value):
+ if isinstance(value, dict):
+ return {key: '<media>' if key in ('title', 'current_asset_name') else hide_names(item)
+ for key, item in value.items()}
+ if isinstance(value, list):
+ return [hide_names(item) for item in value]
+ return value
+
+ try:
+ values = [json.loads(text)]
+ except ValueError:
+ values = []
+ for line in text.splitlines():
+ try:
+ values.append(json.loads(line))
+ except ValueError:
+ continue
+ lines = [json.dumps(error_reporting.scrub(hide_names(value)), ensure_ascii=False)
+ for value in values if isinstance(value, (dict, list))]
+ return '\n'.join(lines) + ('\n' if lines else '')
+
+
def redact_log(text: str) -> str:
"""JSON 行按字段脱敏,保留异常链;普通文本按行处理。"""
lines = []
@@ -29,47 +56,100 @@ def redact_log(text: str) -> str:
return "\n".join(lines) + ("\n" if text.endswith("\n") else "")
+def _recent(directory: Path, pattern: str, limit: int, entries: list) -> list[Path]:
+ found = []
+ for path in directory.glob(pattern):
+ try:
+ found.append((path.stat().st_mtime, path))
+ except OSError as error:
+ entries.append({'name': path.name, 'status': type(error).__name__})
+ return [path for _, path in sorted(found, reverse=True)[:limit]]
+
+
+def _log_candidates(directory: Path, entries: list) -> list[Path]:
+ candidates = [directory / name for name in LOG_NAMES]
+ for name in LOG_NAMES:
+ candidates += _recent(directory, Path(name).stem + '.until-*.log', 2, entries)
+ return candidates + _recent(directory, 'errors-*.log', 8, entries) + _recent(directory, 'crash-*.log', 4, entries)
+
+
+def _archive_files(archive, directory, candidates, prefix, entries, *, structured=False):
+ for path in dict.fromkeys(candidates):
+ try:
+ if path.is_symlink() or not path.resolve().is_relative_to(directory.resolve()):
+ entries.append({'name': path.name, 'status': 'skipped_link'})
+ continue
+ if not path.is_file():
+ continue
+ with path.open('rb') as stream:
+ size = stream.seek(0, os.SEEK_END)
+ stream.seek(max(0, size - TAIL_BYTES))
+ raw = stream.read(TAIL_BYTES)
+ text = raw.decode('utf-8', errors='replace')
+ if size > TAIL_BYTES:
+ text = text.partition('\n')[2]
+ content = _processing_log(text) if structured else redact_log(text)
+ name = path.relative_to(directory).as_posix()
+ archive.writestr(f'{prefix}/{name}', content)
+ entries.append({'name': name, 'section': prefix, 'truncated': size > TAIL_BYTES,
+ 'status': 'included' if content or not raw else 'unreadable_json'})
+ except OSError as error:
+ entries.append({'name': path.name, 'status': type(error).__name__})
+
+
+def _archive_state(archive, directory, entries):
+ candidates = [directory / name for name in STATE_NAMES]
+ candidates += _recent(directory, 'library-processing-*.issues.jsonl', 8, entries)
+ _archive_files(archive, directory, candidates, 'processing', entries, structured=True)
+ migrations = _recent(directory, 'update-backups/*/migration.log', 2, entries)
+ _archive_files(archive, directory, migrations, 'updates', entries)
+
+
def bundle_bytes(*, report: dict | None = None, config=None) -> bytes:
facts = {"created_at": datetime.now(timezone.utc).isoformat(), "system": error_reporting.system_facts()}
directories = error_reporting.log_directories()
+ processing_directory = None
try:
from . import settings_file
config = config or settings_file.load_config()
facts["configuration"] = {"status": "available", "present": config.present,
"port": config.server.port}
directories.insert(0, config.directory("logs"))
+ processing_directory = config.directory('state')
+ from .ffmpeg import FFmpegResolver, binary_diagnostics
+ resolver = FFmpegResolver(config.directory('tools') / 'ffmpeg')
+ facts['media_tools'] = {'ffmpeg': binary_diagnostics(resolver.ffmpeg(), decoders=True),
+ 'ffprobe': binary_diagnostics(resolver.ffprobe())}
except Exception as error:
facts["configuration"] = {"status": "unavailable", **error_reporting.exception_facts(error)}
+ try:
+ root, _ = settings_file.discover_data_root()
+ processing_directory = root / 'state'
+ except Exception:
+ pass
if report:
# 报告分享仅保留系统检查,库清单、媒体挂载明细和来源原文留在本机页面。
facts["checks"] = {key: {field: value.get(field) for field in ("status", "reason", "action")}
for key, value in report.get("checks", {}).items()
if key not in ("media_mounts",)}
+ source_fields = ('source', 'status', 'reason', 'last_check_at', 'elapsed_s',
+ 'last_success_at', 'cooldown_until')
+ facts['sources'] = [{key: row.get(key) for key in source_fields}
+ for row in report.get('sources', []) if isinstance(row, dict)]
output = io.BytesIO()
entries = []
with zipfile.ZipFile(output, "w", compression=zipfile.ZIP_DEFLATED) as archive:
for index, directory in enumerate(dict.fromkeys(directories)):
try:
- candidates = [directory / name for name in LOG_NAMES]
- candidates += sorted(directory.glob("errors-*.log"), key=lambda path: path.stat().st_mtime, reverse=True)[:8]
- candidates += sorted(directory.glob("crash-*.log"), key=lambda path: path.stat().st_mtime, reverse=True)[:4]
- except OSError:
- continue
- for path in candidates:
- if path.is_symlink() or not path.is_file():
- continue
- try:
- with path.open("rb") as stream:
- size = stream.seek(0, os.SEEK_END)
- stream.seek(max(0, size - TAIL_BYTES))
- raw = stream.read(TAIL_BYTES)
- text = raw.decode("utf-8", errors="replace")
- if size > TAIL_BYTES:
- text = text.partition("\n")[2]
- archive.writestr(f"logs/{index}-{path.name}", redact_log(text))
- entries.append({"name": path.name, "truncated": size > TAIL_BYTES})
- except OSError as error:
- entries.append({"name": path.name, "status": type(error).__name__})
+ candidates = _log_candidates(directory, entries)
+ _archive_files(archive, directory, candidates, f'logs/{index}', entries)
+ except OSError as error:
+ entries.append({'name': '<logs>', 'status': type(error).__name__})
+ if processing_directory is not None:
+ try:
+ _archive_state(archive, processing_directory, entries)
+ except OSError as error:
+ entries.append({'name': '<state>', 'status': type(error).__name__})
facts["logs"] = entries
archive.writestr("report.json", json.dumps(error_reporting.scrub(facts), ensure_ascii=False, indent=2))
return output.getvalue()
diff --git a/tests/test_diagnostics.py b/tests/test_diagnostics.py
index 67d3eaf0b..f5c4901b6 100644
--- a/tests/test_diagnostics.py
+++ b/tests/test_diagnostics.py
@@ -178,8 +178,10 @@ class DiagnosticsTests(unittest.TestCase):
binary = BinaryChoice(self.root / "tools/ffmpeg", "peach-managed")
with patch.object(diagnostics.FFmpegResolver, "ffmpeg", return_value=binary), \
patch.object(diagnostics.FFmpegResolver, "ffprobe", return_value=binary), \
- patch.object(diagnostics.settings_file, "active", return_value=self.config):
+ patch.object(diagnostics.settings_file, "active", return_value=self.config), \
+ patch.object(diagnostics, 'binary_diagnostics') as inspect:
result = diagnostics.health_components(self.settings, EMPTY_MOUNTS)
+ inspect.assert_not_called()
self.assertEqual(set(result), {"database", "schema", "configured", "ffmpeg", "media_mounts", "security"})
self.assertTrue(all(set(value) == {"status", "reason"} for value in result.values()))
self.assertNotIn(str(self.root), json.dumps(result))
diff --git a/tests/test_error_reporting.py b/tests/test_error_reporting.py
index d482efb84..8b3f779ed 100644
--- a/tests/test_error_reporting.py
+++ b/tests/test_error_reporting.py
@@ -30,6 +30,94 @@ class ErrorReportingTests(unittest.TestCase):
env={**os.environ, "PYTHONPATH": str(ROOT / "src")},
capture_output=True, text=True, encoding="utf-8", timeout=30)
+ def test_processing_export_keeps_recent_bounded_evidence_and_hides_media_names(self):
+ from peach.settings_file import PeachConfig
+ config = PeachConfig(self.root, self.root / 'config.toml', present=True)
+ state = config.directory('state')
+ state.mkdir(parents=True)
+ row = {'asset_id': 23, 'title': 'private-movie.mp4', 'path': r'D:\private\movie.mp4',
+ 'diagnostics': {'reason': 'timeout', 'stderr': 'token=private-token',
+ 'timestamp_seconds': 12, 'timeout_seconds': 45}}
+ for index in range(10):
+ path = state / f'library-processing-{index}.issues.jsonl'
+ path.write_text(json.dumps(row) + '\n', encoding='utf-8')
+ os.utime(path, (100 + index, 100 + index))
+ (state / 'private-credentials.json').write_text('private-credentials', encoding='utf-8')
+ (state / 'library-processing.json').write_text(json.dumps({
+ 'status': 'failed', 'issue_preview': [row], 'current_asset_name': 'private-movie.mp4'}),
+ encoding='utf-8')
+ with mock.patch.object(error_reporting, 'log_directories', return_value=[]), \
+ zipfile.ZipFile(io.BytesIO(support_bundle.bundle_bytes(config=config))) as archive:
+ names = [name for name in archive.namelist() if name.startswith('processing/')]
+ self.assertEqual(len(names), 9)
+ self.assertNotIn('processing/library-processing-0.issues.jsonl', names)
+ exported = json.loads(archive.read('processing/library-processing-9.issues.jsonl'))
+ self.assertEqual(exported['asset_id'], 23)
+ self.assertEqual(exported['diagnostics']['timestamp_seconds'], 12)
+ contents = '\n'.join(archive.read(name).decode() for name in archive.namelist())
+ for private in ('private-movie', 'private-token', 'private-credentials', r'D:\private'):
+ self.assertNotIn(private, contents)
+
+ def test_processing_export_omits_partial_rows_and_limits_large_files(self):
+ from peach.settings_file import PeachConfig
+ config = PeachConfig(self.root, self.root / 'config.toml', present=True)
+ state = config.directory('state')
+ state.mkdir(parents=True)
+ path = state / 'library-processing-large.issues.jsonl'
+ row = json.dumps({'asset_id': 23, 'message': 'error' * 100}) + '\n'
+ path.write_text(row * 400 + '{"partial":', encoding='utf-8')
+ with mock.patch.object(error_reporting, 'log_directories', return_value=[]), \
+ zipfile.ZipFile(io.BytesIO(support_bundle.bundle_bytes(config=config))) as archive:
+ text = archive.read('processing/' + path.name).decode()
+ report = json.loads(archive.read('report.json'))
+ self.assertLessEqual(len(text.encode()), support_bundle.TAIL_BYTES)
+ self.assertTrue(text.splitlines())
+ self.assertTrue(all(json.loads(line)['asset_id'] == 23 for line in text.splitlines()))
+ self.assertTrue(next(row for row in report['logs'] if row['name'] == path.name)['truncated'])
+
+ def test_bundle_contains_update_tunnel_rotated_and_startup_evidence_with_invalid_config(self):
+ from peach import settings_file
+ self.logs.mkdir()
+ for name in ('tray-restart.out.log', 'tray-scan.out.log', 'windows-source-sync.log',
+ 'cloudflare-tunnel.log', 'tray.until-20261001.log'):
+ (self.logs / name).write_text('failure --token private-cli-token\n', encoding='utf-8')
+ state = self.root / 'state'
+ state.mkdir()
+ (state / 'runtime-prepare.json').write_text(json.dumps({
+ 'ok': False, 'steps': [{'name': 'dependencies', 'message': 'failed --password private-password'}]},
+ indent=2), encoding='utf-8')
+ migration = state / 'update-backups' / 'fixture' / 'migration.log'
+ migration.parent.mkdir(parents=True)
+ migration.write_text('migration failed\n', encoding='utf-8')
+ with mock.patch.object(settings_file, 'load_config', side_effect=ValueError('invalid config')), \
+ zipfile.ZipFile(io.BytesIO(support_bundle.bundle_bytes())) as archive:
+ names = archive.namelist()
+ self.assertTrue(any('tray.until-' in name for name in names))
+ for name in ('tray-restart.out.log', 'tray-scan.out.log', 'windows-source-sync.log', 'cloudflare-tunnel.log'):
+ self.assertTrue(any(item.endswith('/' + name) for item in names), name)
+ startup = json.loads(archive.read('processing/runtime-prepare.json'))
+ self.assertFalse(startup['ok'])
+ self.assertIn('updates/update-backups/fixture/migration.log', names)
+ contents = '\n'.join(archive.read(name).decode() for name in names)
+ for private in ('private-cli-token', 'private-password'):
+ self.assertNotIn(private, contents)
+
+ def test_one_unreadable_error_log_does_not_hide_other_logs(self):
+ self.logs.mkdir()
+ (self.logs / 'tray.log').write_text('service failed\n', encoding='utf-8')
+ (self.logs / 'errors-unreadable.log').write_text('error', encoding='utf-8')
+ original = Path.stat
+ def stat(path, *args, **kwargs):
+ if path.name == 'errors-unreadable.log':
+ raise PermissionError('not readable')
+ return original(path, *args, **kwargs)
+ with mock.patch.object(Path, 'stat', stat), \
+ zipfile.ZipFile(io.BytesIO(support_bundle.bundle_bytes())) as archive:
+ self.assertTrue(any(name.endswith('/tray.log') for name in archive.namelist()))
+ report = json.loads(archive.read('report.json'))
+ self.assertTrue(any(row['name'] == 'errors-unreadable.log' and row['status'] == 'PermissionError'
+ for row in report['logs']))
+
def records(self):
return [json.loads(line) for path in self.logs.glob("errors-*.log")
for line in path.read_text(encoding="utf-8").splitlines()]
@@ -178,7 +266,7 @@ class ErrorReportingTests(unittest.TestCase):
again = self.probe(f"from peach.support_bundle import main; raise SystemExit(main([{str(destination)!r}]))")
self.assertNotEqual(again.returncode, 0)
- def test_browser_errors_and_bundle_require_local_same_origin_access(self):
+ def test_browser_errors_require_same_origin_and_remote_login_while_export_is_local(self):
from fastapi import FastAPI
from fastapi.testclient import TestClient
from types import SimpleNamespace
@@ -187,7 +275,7 @@ class ErrorReportingTests(unittest.TestCase):
app = FastAPI()
app.include_router(routes_configuration.router)
app.dependency_overrides[require_auth] = lambda: {}
- app.state.settings = SimpleNamespace(mdns_name="peach")
+ app.state.settings = SimpleNamespace(mdns_name="peach", token='', access_path=self.root / 'access.json')
app.state.mount_reachability = SimpleNamespace(summary=lambda: {})
client = TestClient(app, base_url="http://127.0.0.1", client=("127.0.0.1", 1234))
path = "/api/diagnostics/client-error"
@@ -195,22 +283,110 @@ class ErrorReportingTests(unittest.TestCase):
headers={"Origin": "https://other.example"}).status_code, 403)
self.assertEqual(client.post(path, content=b"x" * 20_000).status_code, 413)
self.assertEqual(client.post(path, json={"message": 5}).status_code, 400)
- self.assertEqual(client.post(path, json={"message": "secret=private-browser", "stack": "at render"}).status_code, 200)
+ self.assertEqual(client.post(path, json={"message": "secret=private-browser", "stack": "at render",
+ 'source_file': 'app-abcd.js', 'line': 21, 'column': 8}).status_code, 200)
browser = next(row for row in self.records() if row["stage"] == "browser")
self.assertNotIn("private-browser", str(browser))
self.assertEqual(browser["stack"], "at render")
+ self.assertEqual(browser['line'], 21)
+ self.assertEqual(browser['source_file'], 'app-abcd.js')
with mock.patch("peach.diagnostics.report", side_effect=RuntimeError("diagnostics unavailable")):
response = client.get("/api/diagnostics/bundle")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.headers["cache-control"], "no-store")
self.assertTrue(zipfile.is_zipfile(io.BytesIO(response.content)))
remote = TestClient(app, base_url="http://127.0.0.1", client=("192.0.2.55", 1234))
- self.assertEqual(remote.post(path, json={"message": "example"}).status_code, 403)
+ self.assertEqual(remote.post(path, json={"message": "example"}).status_code, 401)
+ from peach import access
+ empty_policy = access.load(app.state.settings.access_path)
+ empty_policy['key'] = ''
+ remote.cookies.set(access.COOKIE, access.issue(empty_policy, 1)[0])
+ self.assertEqual(remote.post(path, json={'message': 'anonymous'}).status_code, 401)
+ policy = access.save(app.state.settings.access_path, 'valid-private-password')
+ remote.cookies.set(access.COOKIE, access.issue(policy, 1)[0])
+ self.assertEqual(remote.post(path, json={'message': 'phone failed'}).status_code, 200)
+ self.assertEqual(remote.post(path, json={'message': 'phone failed'},
+ headers={'Origin': 'https://other.example'}).status_code, 403)
+ remote.cookies.clear()
+ self.assertEqual(remote.post(path, json={'message': 'anonymous'}).status_code, 401)
self.assertEqual(remote.get("/api/diagnostics/bundle").status_code, 403)
for _ in range(20):
response = client.post(path, json={"message": "example"})
self.assertEqual(response.status_code, 429)
+ def test_background_failure_has_a_persistent_traceback_and_job_identifier(self):
+ from peach.jobs import BackgroundJob
+ job = BackgroundJob('test-diagnostic-job')
+ def fail(_job_id):
+ raise ValueError('failed --token private-cli-token')
+ state = job.start(fail)
+ job.thread.join(3)
+ self.assertFalse(job.thread.is_alive())
+ event = next(row for row in self.records() if row['stage'] == 'background-job')
+ self.assertEqual(event['job_id'], state['job_id'])
+ self.assertEqual(event['exception'][0]['type'], 'ValueError')
+ self.assertTrue(event['frames'])
+ self.assertNotIn('private-cli-token', str(event))
+ job.start_result(lambda: {'ok': False, 'error': 'expired --token private-result-token'})
+ job.thread.join(3)
+ result = next(row for row in self.records() if row['stage'] == 'background-result')
+ self.assertIn('expired', result['message'])
+ self.assertNotIn('private-result-token', str(result))
+
+ def test_export_keeps_safe_source_health_without_source_response_content(self):
+ source = {'source': 'javdb', 'status': 'warning', 'reason': '来源正在冷却',
+ 'cooldown_until': '2026-10-18T01:00:00Z', 'raw': 'private-source-response',
+ 'url': 'https://private.example/?token=private-token'}
+ with zipfile.ZipFile(io.BytesIO(support_bundle.bundle_bytes(report={'sources': [source]}))) as archive:
+ report = json.loads(archive.read('report.json'))
+ self.assertEqual(report['sources'][0]['status'], 'warning')
+ self.assertEqual(report['sources'][0]['cooldown_until'], source['cooldown_until'])
+ self.assertNotIn('private-source-response', str(report))
+
+ def test_probe_error_preserves_exit_code_and_stderr(self):
+ from peach import media_probe
+ result = subprocess.CompletedProcess(['ffprobe'], 69, b'{}', b'AV1 decoder unavailable')
+ with mock.patch.object(media_probe.subprocess, 'run', return_value=result), \
+ mock.patch.object(media_probe, 'resolve_case_insensitive', str):
+ measured = media_probe.measure('ffprobe', 23, 'private.mp4')
+ self.assertEqual(measured[0], -1)
+ event = next(row for row in self.records() if row['stage'] == 'media-probe')
+ self.assertEqual(event['asset_id'], 23)
+ self.assertEqual(event['returncode'], 69)
+ self.assertIn('AV1 decoder unavailable', event['stderr'])
+
+ def test_command_line_credentials_are_redacted(self):
+ for flag in ('--token', '--tunnel-token', '--api-key', '--access-key', '--password'):
+ self.assertNotIn('private-value', error_reporting.redact(f'{flag} "private-value"'))
+
+ def test_http_server_errors_are_correlated_without_request_contents(self):
+ from fastapi import HTTPException
+ from fastapi.testclient import TestClient
+ from peach.api import create_app
+ from peach.config import PeachSettings
+ app = create_app(PeachSettings(db_path=self.root / 'database/ledger.db'))
+ @app.get('/diagnostic-test/{identifier}')
+ def fail(identifier: str):
+ raise RuntimeError('request failed')
+ @app.get('/diagnostic-handled-test')
+ def handled():
+ raise HTTPException(503, 'service unavailable')
+ client = TestClient(app, raise_server_exceptions=False)
+ response = client.get('/diagnostic-test/private-identifier?token=private-query',
+ headers={'Authorization': 'Bearer private-auth'})
+ self.assertEqual(response.status_code, 500)
+ record = next(row for row in self.records() if row['stage'] == 'http-request')
+ self.assertEqual(record['route'], 'diagnostic-test/{identifier}')
+ self.assertEqual(record['error_id'], response.json()['error_id'])
+ self.assertEqual(response.headers['x-peach-error-id'], record['error_id'])
+ self.assertTrue(record['frames'])
+ for value in ('private-identifier', 'private-query', 'private-auth'):
+ self.assertNotIn(value, str(record))
+ self.assertEqual(client.get('/diagnostic-handled-test').status_code, 503)
+ self.assertEqual(len([row for row in self.records() if row['stage'] == 'http-request']), 2)
+ client.get('/diagnostic-missing')
+ self.assertEqual(len([row for row in self.records() if row['stage'] == 'http-request']), 2)
+
def test_asyncio_exceptions_are_recorded_and_keep_the_default_handler(self):
loop = mock.Mock()
error_reporting.asyncio_handler(loop, {"exception": ValueError("async failure"), "message": "task"})
diff --git a/tests/test_ffmpeg_build.py b/tests/test_ffmpeg_build.py
index 9746b7cae..85f161f36 100644
--- a/tests/test_ffmpeg_build.py
+++ b/tests/test_ffmpeg_build.py
@@ -23,7 +23,8 @@ class FFmpegSourceBuildTests(unittest.TestCase):
lock = builder.load_sources()
self.assertEqual(lock["version"], "9.0.2")
self.assertEqual(lock["license"], "GPL-3.0-or-later")
- self.assertEqual(len(lock["sources"]), 3)
+ self.assertEqual(len(lock["sources"]), 4)
+ self.assertIn('dav1d', {source['name'] for source in lock['sources']})
def make_sources(self, output):
lock = builder.load_sources()
@@ -64,7 +65,7 @@ class FFmpegSourceBuildTests(unittest.TestCase):
lock = self.make_sources(output)
builder.prepare(output, lock)
for path in ("ffmpeg/COPYING.GPLv3", "ffmpeg/COPYING.LGPLv2.1",
- "ffmpeg/COPYING.LGPLv3", "x264/COPYING"):
+ "ffmpeg/COPYING.LGPLv3", "x264/COPYING", "dav1d/COPYING"):
(output / "work" / path).write_text("license", encoding="utf-8")
headers = output / "work/nv-codec-headers/include/ffnvcodec"
headers.mkdir(parents=True)
diff --git a/tests/test_ffmpeg_bundle.py b/tests/test_ffmpeg_bundle.py
index e68eb2d10..8ea53bd77 100644
--- a/tests/test_ffmpeg_bundle.py
+++ b/tests/test_ffmpeg_bundle.py
@@ -21,7 +21,7 @@ class FFmpegBundleTests(unittest.TestCase):
self.manifest["files"][name] = ffmpeg_bundle.digest(path)
self.manifest["sources"] = [
{"name": name, "revision": "fixed", "url": f"https://example.org/{name}.tar.gz", "sha256": "1" * 64}
- for name in ("ffmpeg", "x264", "nv-codec-headers")]
+ for name in ("ffmpeg", "x264", "nv-codec-headers", "dav1d")]
archive = self.source.parent / "ffmpeg-corresponding-source.zip"
archive.write_bytes(b"source fixture")
self.manifest["source_archive"] = {"name": archive.name, "sha256": ffmpeg_bundle.digest(archive)}
@@ -86,7 +86,7 @@ class FFmpegBundleTests(unittest.TestCase):
self.manifest["files"].pop("bin/ffprobe.exe")
(self.source / "untrunc.exe").write_bytes(b"untrunc")
self.manifest["files"]["untrunc.exe"] = ffmpeg_bundle.digest(self.source / "untrunc.exe")
- self.manifest["sources"] = [s for s in self.manifest["sources"] if s["name"] != "x264"]
+ self.manifest["sources"] = [s for s in self.manifest["sources"] if s["name"] in ('ffmpeg', 'nv-codec-headers')]
self.manifest["sources"][1]["name"] = "untrunc"
archive = self.source.parent / "ffmpeg-corresponding-source.zip"
archive.rename(self.source.parent / "untrunc-corresponding-source.zip")
diff --git a/tests/test_frame_capture.py b/tests/test_frame_capture.py
index c71406c66..04f08cd41 100644
--- a/tests/test_frame_capture.py
+++ b/tests/test_frame_capture.py
@@ -9,7 +9,11 @@ import unittest
from pathlib import Path
from unittest.mock import patch
+from PIL import Image
+
from peach import frame_capture
+from peach.ffmpeg import FFmpegResolver
+from support.conditions import missing_prerequisite
class _Completed:
@@ -20,7 +24,7 @@ class _Completed:
class CaptureFrameTests(unittest.TestCase):
def setUp(self):
- self.root = Path(tempfile.mkdtemp(prefix="frame-capture-"))
+ self.root = Path(tempfile.mkdtemp(prefix="frame-capture-")).resolve()
self.addCleanup(lambda: shutil.rmtree(self.root, ignore_errors=True))
self.commands: list[list[str]] = []
@@ -30,7 +34,7 @@ class CaptureFrameTests(unittest.TestCase):
subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0)
self.commands.append(list(command))
if produce:
- Path(command[-1]).write_bytes(b"x" * 2048)
+ Image.new('RGB', (480, 270), 'green').save(command[-1], format='JPEG')
return _Completed(stderr)
return patch.object(frame_capture.subprocess, "run", fake)
@@ -64,6 +68,63 @@ class CaptureFrameTests(unittest.TestCase):
self.assertFalse(ok)
self.assertEqual(stderr, "ffmpeg timeout")
+ def test_small_decodable_frame_counts_as_a_picture(self):
+ def fake(command, **_kwargs):
+ Image.new('RGB', (32, 32), 'black').save(command[-1], format='JPEG')
+ return _Completed()
+ destination = self.root / 'small-valid.jpg'
+ with patch.object(frame_capture.subprocess, 'run', fake):
+ ok, _ = frame_capture.capture_frame('ffmpeg', 'movie.mp4', 1, destination)
+ self.assertTrue(ok)
+ self.assertLess(destination.stat().st_size, 1024)
+
+ def test_nonzero_exit_rejects_and_removes_a_partial_picture(self):
+ def fake(command, **_kwargs):
+ Image.new('RGB', (480, 270)).save(command[-1], format='JPEG')
+ result = _Completed(b'decoder failed')
+ result.returncode = 1
+ return result
+ detail = {}
+ destination = self.root / 'partial.jpg'
+ with patch.object(frame_capture.subprocess, 'run', fake):
+ self.assertFalse(frame_capture.capture_with_retry(
+ 'ffmpeg', 'movie.mp4', 5, destination, diagnostics=detail))
+ self.assertFalse(destination.exists())
+ self.assertEqual(detail['returncode'], 1)
+ self.assertEqual(detail['stderr'], 'decoder failed')
+ self.assertEqual(detail['timestamp_seconds'], 5)
+
+ def test_timeout_preserves_stderr_and_cleans_the_output(self):
+ destination = self.root / 'timed-out.jpg'
+ def fake(*_args, **_kwargs):
+ destination.write_bytes(b'partial')
+ raise subprocess.TimeoutExpired('ffmpeg', 45, stderr=b'decode stalled')
+ detail = {}
+ with patch.object(frame_capture.subprocess, 'run', fake):
+ self.assertFalse(frame_capture.capture_with_retry(
+ 'ffmpeg', 'movie.mp4', 8, destination, diagnostics=detail))
+ self.assertFalse(destination.exists())
+ self.assertEqual(detail['reason'], 'timeout')
+ self.assertEqual(detail['stderr'], 'decode stalled')
+ self.assertEqual(len(detail['attempts']), 1)
+
+ def test_color_retry_keeps_both_attempts(self):
+ detail = {}
+ with self._run(produce=False, stderr=b'Unsupported color primaries: reserved'):
+ self.assertFalse(frame_capture.capture_with_retry(
+ 'ffmpeg', 'movie.mp4', 1, self.root / 'color.jpg', diagnostics=detail))
+ self.assertEqual([item['color_override'] for item in detail['attempts']], [False, True])
+
+ def test_an_existing_frame_cannot_mask_an_empty_output(self):
+ destination = self.root / 'stale.jpg'
+ Image.new('RGB', (480, 270)).save(destination)
+ detail = {}
+ with self._run(produce=False):
+ self.assertFalse(frame_capture.capture_with_retry(
+ 'ffmpeg', 'movie.mp4', 1, destination, diagnostics=detail))
+ self.assertEqual(detail['reason'], 'no_frame')
+ self.assertFalse(destination.exists())
+
def test_only_bad_color_metadata_is_worth_a_second_try(self):
"""无条件重试会让网盘超时这类必然失败的文件每帧白跑第二次:单帧最坏耗时从
45 秒翻到 90 秒,9 帧就是 13.5 分钟。"""
@@ -85,7 +146,7 @@ class CaptureFrameTests(unittest.TestCase):
class TileFramesTests(unittest.TestCase):
def setUp(self):
- self.root = Path(tempfile.mkdtemp(prefix="frame-tile-"))
+ self.root = Path(tempfile.mkdtemp(prefix="frame-tile-")).resolve()
self.addCleanup(lambda: shutil.rmtree(self.root, ignore_errors=True))
def test_the_grid_size_reaches_the_filter(self):
@@ -95,7 +156,7 @@ class TileFramesTests(unittest.TestCase):
self.assertEqual(options["creationflags"],
subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0)
seen.append(list(command))
- Path(command[-1]).write_bytes(b"x" * 8192)
+ Image.new('RGB', (96, 96), 'black').save(command[-1], format='JPEG')
return _Completed()
with patch.object(frame_capture.subprocess, "run", fake):
@@ -112,5 +173,42 @@ class TileFramesTests(unittest.TestCase):
self.assertFalse(made)
+class FFmpegCaptureIntegrationTests(unittest.TestCase):
+ def test_av1_software_decode_uses_the_desktop_release_gate(self):
+ from scripts.smoke_desktop import assert_av1_decoder
+ with tempfile.TemporaryDirectory() as temporary:
+ root = Path(temporary).resolve()
+ choice = FFmpegResolver(root).ffmpeg()
+ if choice is None:
+ missing_prerequisite('AV1 解码回归需要 FFmpeg')
+ assert_av1_decoder(choice.path, root)
+
+ def test_real_small_jpegs_and_unicode_paths_produce_a_grid(self):
+ with tempfile.TemporaryDirectory() as temporary:
+ root = Path(temporary).resolve()
+ choice = FFmpegResolver(root).ffmpeg()
+ if choice is None:
+ missing_prerequisite('抽帧回归需要 FFmpeg')
+ ffmpeg = str(choice.path)
+ source = root / '中文 空格 (1).mp4'
+ subprocess.run([ffmpeg, '-y', '-v', 'error', '-f', 'lavfi', '-i',
+ 'color=c=black:s=320x180:r=5:d=1', '-c:v', 'mpeg4', str(source)],
+ capture_output=True, check=True, timeout=20)
+ for index in range(9):
+ detail = {}
+ self.assertTrue(frame_capture.capture_with_retry(
+ ffmpeg, str(source), .1, root / f's{index:02d}.jpg', diagnostics=detail), detail)
+ self.assertLess((root / 's00.jpg').stat().st_size, 1024)
+ target = root / 'grid.jpg'
+ self.assertTrue(frame_capture.tile_frames(ffmpeg, str(root / 's%02d.jpg'), 3, 3, target))
+ with Image.open(target) as image:
+ self.assertEqual(image.size, (1440, 810))
+ detail = {}
+ self.assertFalse(frame_capture.capture_with_retry(
+ ffmpeg, str(source), 10, root / 'beyond.jpg', diagnostics=detail))
+ self.assertIn(detail['reason'], ('no_frame', 'ffmpeg_error'))
+ self.assertIsInstance(detail['returncode'], int)
+
+
if __name__ == "__main__":
unittest.main()
diff --git a/tests/test_media.py b/tests/test_media.py
index eca1cdb02..e4b877329 100644
--- a/tests/test_media.py
+++ b/tests/test_media.py
@@ -1,4 +1,5 @@
import os
+import subprocess
import tempfile
import sys
import unittest
@@ -6,7 +7,7 @@ from pathlib import Path
from unittest.mock import patch
import peach.media as media_module
-from peach.ffmpeg import FFmpegResolver
+from peach.ffmpeg import BinaryChoice, FFmpegResolver, binary_diagnostics
from peach.media import (
FilesystemBackend, MediaEngine, MediaUnavailable, normalized_path,
resolve_case_insensitive,
@@ -15,6 +16,22 @@ from peach.repository import MediaAsset
class MediaEngineTests(unittest.TestCase):
+ def test_tool_diagnostics_distinguishes_software_av1_and_hardware_only(self):
+ for decoder, available in (('av1', False), ('libdav1d', True)):
+ results = [subprocess.CompletedProcess([], 0, b'ffmpeg version test\n', b''),
+ subprocess.CompletedProcess([], 0, f' V..... {decoder} AV1 decoder\n'.encode(), b'')]
+ with patch('peach.ffmpeg.subprocess.run', side_effect=results):
+ result = binary_diagnostics(BinaryChoice(Path('ffmpeg'), 'test'), decoders=True)
+ self.assertTrue(result['usable'])
+ self.assertEqual(result['av1_software'], available)
+
+ def test_tool_diagnostics_reports_execution_failure(self):
+ with patch('peach.ffmpeg.subprocess.run', side_effect=subprocess.TimeoutExpired('ffmpeg', 5)):
+ result = binary_diagnostics(BinaryChoice(Path('ffmpeg'), 'test'), decoders=True)
+ self.assertTrue(result['available'])
+ self.assertFalse(result['usable'])
+ self.assertEqual(result['error'], 'TimeoutExpired')
+
# 临时目录一律先 resolve 再喂给被测代码。媒体解析本身就以 resolve 后的真实路径
# 判断是否越过根目录,而 CI runner 的临时目录都是别名:macOS 的 /var 是
# /private/var 的软链,Windows runner 的 %TEMP% 是 C:\Users\RUNNER~1 这种 8.3
diff --git a/tests/test_media_probe.py b/tests/test_media_probe.py
index cb8a8deae..d99683dfc 100644
--- a/tests/test_media_probe.py
+++ b/tests/test_media_probe.py
@@ -114,7 +114,7 @@ class ProbeProcessTests(unittest.TestCase):
payload = b'{"format":{"duration":"12.5"},"streams":[{"width":320,"height":240,' \
b'"codec_name":"h264","avg_frame_rate":"25/1"}]}'
with mock.patch.object(media_probe.subprocess, "run",
- return_value=SimpleNamespace(stdout=payload)) as run:
+ return_value=SimpleNamespace(stdout=payload, returncode=0, stderr=b'')) as run:
result = media_probe.probe_file("ffprobe", "sample.mp4", timeout=3)
self.assertEqual(result, (12.5, 320, 240, "h264", 25.0, None))
options = run.call_args.kwargs
diff --git a/tests/test_metadata_library.py b/tests/test_metadata_library.py
index e894139d6..0e7f0ab47 100644
--- a/tests/test_metadata_library.py
+++ b/tests/test_metadata_library.py
@@ -2716,6 +2716,49 @@ class LibraryPreviewCompletionTests(unittest.TestCase):
with self.database.read_connection() as connection:
self.assertIsNone(connection.execute('SELECT snapshot_path FROM asset WHERE id=1').fetchone()[0])
+ def test_frame_failure_evidence_is_saved_and_included_in_the_support_zip(self):
+ import io
+ import zipfile
+ from peach import support_bundle
+
+ def fail(_ffmpeg, video, _timestamp, _destination, *, diagnostics):
+ diagnostics.update(reason='ffmpeg_error', returncode=69, timestamp_seconds=2.4667,
+ timeout_seconds=45, attempts=[],
+ stderr=f'Error decoding {video}\nInvalid data token=private-token')
+ return False
+
+ with patch('peach.library_processing.FFmpegResolver', return_value=self.resolver), \
+ patch('peach.frame_capture.capture_with_retry', side_effect=fail):
+ result = self.run_processing(stage='previews', asset_ids=[1])
+ self.assertEqual(result['status'], 'failed')
+ self.assertEqual(result['retryable_asset_ids'], [1])
+ issue = json.loads(Path(result['issues_log']).read_text(encoding='utf-8').splitlines()[0])
+ detail = issue['diagnostics']
+ self.assertEqual(detail['returncode'], 69)
+ self.assertEqual(detail['frame_index'], 0)
+ self.assertEqual(detail['duration_seconds'], 30)
+ self.assertIn('Invalid data', detail['stderr'])
+ self.assertNotIn(str(self.media), detail['stderr'])
+ self.assertNotIn('private-token', detail['stderr'])
+ self.assertIn('导出诊断报告', issue['message'])
+ with patch.object(support_bundle.error_reporting, 'log_directories', return_value=[]):
+ payload = support_bundle.bundle_bytes(config=self.config)
+ with zipfile.ZipFile(io.BytesIO(payload)) as archive:
+ evidence = json.loads(archive.read('processing/' + Path(result['issues_log']).name))
+ state = json.loads(archive.read('processing/library-processing.json'))
+ self.assertEqual(evidence['diagnostics'], detail)
+ self.assertEqual(evidence['asset_id'], 1)
+ self.assertEqual(evidence['title'], '<media>')
+ self.assertNotIn(str(self.media), evidence['path'])
+ self.assertEqual(state['retryable_asset_ids'], [1])
+ with patch('peach.library_processing.FFmpegResolver', return_value=self.resolver), \
+ patch('peach.frame_capture.capture_with_retry', return_value=True), \
+ patch('peach.frame_capture.tile_frames', side_effect=self.make_tile):
+ retried = self.run_processing(stage='previews', asset_ids=[1])
+ self.assertEqual(retried['status'], 'complete')
+ self.assertEqual(retried['issue_count'], 0)
+ self.assertEqual(retried['retryable_asset_ids'], [])
+
def test_selected_assets_require_active_videos(self):
for selection in ([], [True], [0], [-1], ['1'], '1', [99], [3], [4]):
with self.subTest(selection=selection), self.assertRaises(ValueError), \
diff --git a/tests/test_scripts.py b/tests/test_scripts.py
index fb949d318..4a31a1fe5 100644
--- a/tests/test_scripts.py
+++ b/tests/test_scripts.py
@@ -3036,6 +3036,8 @@ class OperationalScriptTests(unittest.TestCase):
from peach import media_probe as module
class _Empty:
+ returncode = 0
+ stderr = b""
stdout = b'{"format":{},"streams":[{"width":0,"height":0}]}'
original = module.subprocess.run
@@ -3112,6 +3114,8 @@ class OperationalScriptTests(unittest.TestCase):
符玄12.mp4 实测:`scale=480:-1` 报 Error -129,覆盖色彩声明后正常。
"""
+ from PIL import Image
+
sheets = self.sheets
with tempfile.TemporaryDirectory() as tmp:
dest = Path(tmp) / "sheet.jpg"
@@ -3120,11 +3124,11 @@ class OperationalScriptTests(unittest.TestCase):
def fake_run(command, **kwargs):
calls.append(list(command))
if "-filter_complex" in command:
- dest.write_bytes(b"x" * 8192)
+ Image.new("RGB", (1440, 810), "black").save(dest, "JPEG")
return type("R", (), {"returncode": 0})()
target = Path(command[-1])
if "bt709" in command:
- target.write_bytes(b"y" * 2048)
+ Image.new("RGB", (480, 270), "black").save(target, "JPEG")
return type("R", (), {"returncode": 0, "stderr": b""})()
return type("R", (), {
"returncode": 1,
diff --git a/web/dist/peach-app.js b/web/dist/peach-app.js
index d0e460048..4ceb6ef0d 100644
--- a/web/dist/peach-app.js
+++ b/web/dist/peach-app.js
@@ -19,24 +19,28 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
function d() {
if (u || typeof window > "u") return;
u = !0;
- let e = Date.now(), t = 0, n = /* @__PURE__ */ new Set(), r = (r, i, a = "") => {
+ let e = Date.now(), t = 0, n = /* @__PURE__ */ new Set(), r = (r, i, a = "", o) => {
if (Date.now() - e >= 6e4 && (e = Date.now(), t = 0, n.clear()), t >= 10) return;
- let o = (i instanceof Error ? i.message : typeof i == "string" ? i : a).slice(0, 2e3);
- if (!o) return;
- let s = i instanceof Error ? (i.stack ?? "").slice(0, 6e3) : "", c = `${r}:${o}:${s}`;
- n.has(c) || (n.add(c), t += 1, fetch("/api/diagnostics/client-error", {
+ let s = (i instanceof Error ? i.message : typeof i == "string" ? i : a).slice(0, 2e3);
+ if (!s) return;
+ let c = i instanceof Error ? (i.stack ?? "").slice(0, 6e3) : "", l = `${r}:${s}:${c}`;
+ n.has(l) || (n.add(l), t += 1, fetch("/api/diagnostics/client-error", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
kind: r,
- message: o,
- stack: s
+ message: s,
+ stack: c,
+ page: window.location.pathname,
+ source_file: o?.filename?.split("/").pop()?.split("?")[0],
+ line: o?.lineno,
+ column: o?.colno
}),
credentials: "same-origin",
keepalive: !0
}).catch(() => {}));
};
- window.addEventListener("error", (e) => r("error", e.error, e.message)), window.addEventListener("unhandledrejection", (e) => r("unhandledrejection", e.reason, "Promise 未处理的异常"));
+ window.addEventListener("error", (e) => r("error", e.error, e.message, e)), window.addEventListener("unhandledrejection", (e) => r("unhandledrejection", e.reason, "Promise 未处理的异常"));
}
//#endregion
//#region node_modules/react/cjs/react.production.js
diff --git a/web/dist/peach-pages.js b/web/dist/peach-pages.js
index f6a9b46a1..4435d41bf 100644
--- a/web/dist/peach-pages.js
+++ b/web/dist/peach-pages.js
@@ -12,24 +12,28 @@ var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescr
function u() {
if (l || typeof window > "u") return;
l = !0;
- let e = Date.now(), t = 0, n = /* @__PURE__ */ new Set(), r = (r, i, a = "") => {
+ let e = Date.now(), t = 0, n = /* @__PURE__ */ new Set(), r = (r, i, a = "", o) => {
if (Date.now() - e >= 6e4 && (e = Date.now(), t = 0, n.clear()), t >= 10) return;
- let o = (i instanceof Error ? i.message : typeof i == "string" ? i : a).slice(0, 2e3);
- if (!o) return;
- let s = i instanceof Error ? (i.stack ?? "").slice(0, 6e3) : "", c = `${r}:${o}:${s}`;
- n.has(c) || (n.add(c), t += 1, fetch("/api/diagnostics/client-error", {
+ let s = (i instanceof Error ? i.message : typeof i == "string" ? i : a).slice(0, 2e3);
+ if (!s) return;
+ let c = i instanceof Error ? (i.stack ?? "").slice(0, 6e3) : "", l = `${r}:${s}:${c}`;
+ n.has(l) || (n.add(l), t += 1, fetch("/api/diagnostics/client-error", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
kind: r,
- message: o,
- stack: s
+ message: s,
+ stack: c,
+ page: window.location.pathname,
+ source_file: o?.filename?.split("/").pop()?.split("?")[0],
+ line: o?.lineno,
+ column: o?.colno
}),
credentials: "same-origin",
keepalive: !0
}).catch(() => {}));
};
- window.addEventListener("error", (e) => r("error", e.error, e.message)), window.addEventListener("unhandledrejection", (e) => r("unhandledrejection", e.reason, "Promise 未处理的异常"));
+ window.addEventListener("error", (e) => r("error", e.error, e.message, e)), window.addEventListener("unhandledrejection", (e) => r("unhandledrejection", e.reason, "Promise 未处理的异常"));
}
//#endregion
//#region node_modules/scheduler/cjs/scheduler.production.js