Peach: Local-first video metadata scraper and media manager.
test: 完整验证 wheel 消费入口与分级健康检查
longmeidao3038bc795404
diff --git a/.github/workflows/test.yml b/.github/workflows/test.ymlindex dbc2101a9..a49eb01df 100644--- a/.github/workflows/test.yml+++ b/.github/workflows/test.yml@@ -21,21 +21,38 @@ env:PYTHONIOENCODING: "utf-8"jobs:+ wheel-build:+ runs-on: ubuntu-latest+ steps:+ - uses: actions/checkout@v7+ - uses: actions/setup-python@v7+ with:+ python-version: '3.14'+ - run: python -m pip wheel . --no-deps --no-cache-dir -w wheelhouse+ - uses: actions/upload-artifact@v7+ with:+ name: wheel-smoke+ path: |+ wheelhouse/*.whl+ scripts/smoke_wheel.pywheel-smoke:- name: wheel 最小安装 / ${{ matrix.os }}+ name: wheel 最小安装 / ${{ matrix.os }} / ${{ matrix.python }}+ needs: wheel-buildstrategy:matrix:os: [windows-latest, macos-latest]+ python: ['3.12', '3.14']runs-on: ${{ matrix.os }}steps:- - uses: actions/checkout@v7+ - uses: actions/download-artifact@v8+ with:+ name: wheel-smoke- uses: actions/setup-python@v7with:- python-version: '3.14'- - name: 构建并安装正式 wheel+ python-version: ${{ matrix.python }}+ - name: 安装正式 wheelshell: bashrun: |- python -m pip wheel . --no-deps --no-cache-dir -w wheelhousepython -m pip install --no-cache-dir wheelhouse/*.whlpython -m pip check- name: 源码树外冒烟diff --git a/README.en.md b/README.en.mdindex 98eb8dbba..bd8e2ce7f 100644--- a/README.en.md+++ b/README.en.md@@ -126,7 +126,7 @@ the same path is taken when stdin is not a terminal. A file written this way carFive facts:- Listening on `127.0.0.1` needs no token. Reaching the service from a phone or any other device on the LAN (`--host 0.0.0.0`, and everything the tray starts) does: `peach init` has already written one to `<data root>/secrets/auth-token`, `peach token` prints it, and each device pastes it into the login page once. `peach serve` refuses to start when it binds a non-loopback address without a token, because that puts the whole collection and the write endpoints on the local network.-- `-e` is a hard requirement: the wheel contains only the packages under `src/`, and the repository-root `migrations/` and `web/` are not in it, so under a non-editable install `peach init` fails outright because it cannot find the migrations directory.+- Use `-e` for source development. Regular installations and wheels include pages and migrations and support running from any working directory.- When the data root is not next to the repository, `peach serve` looks for it only via `PEACH_DATA_ROOT` and the `peach-data/` directories a few levels above the repository, so set `PEACH_DATA_ROOT` as well; the default data root needs no such step.- Ledger paths are always in the Windows shape. On Windows the media directory goes straight into `[media.locations]`; on macOS the declared root is `R:\media` and the directory goes into `[media.mounts]`, where the local mount point does the translation. CloudDrive is not required — any cloud drive that mounts as a local path works; 115 and PikPak are recommendations, not requirements.- The interface is currently available in Chinese only.diff --git a/README.md b/README.mdindex e72c05e93..00b1e6500 100644--- a/README.md+++ b/README.md@@ -120,7 +120,7 @@ peach-app/五个事实:- 只监听 `127.0.0.1` 不需要口令。要让手机或局域网里别的设备访问(`--host 0.0.0.0`、托盘起的服务都算),就必须有访问口令:`peach init` 已经在 `<数据根>/secrets/auth-token` 生成一份,`peach token` 打印出来,设备第一次访问时贴进登录页。绑非回环地址却读不到口令时 `peach serve` 拒绝启动,因为那等于把整个馆藏和写接口摆在同网段上。-- `-e` 是硬性要求:wheel 只含 `src/` 下的包,仓库根的 `migrations/` 与 `web/` 不在里面,非可编辑安装下 `peach init` 找不到迁移目录会直接报错。+- 源码开发使用 `-e`;普通安装与 wheel 自带页面和迁移文件,可从任意工作目录运行。- 数据根不在仓库同级时,`peach serve` 只按 `PEACH_DATA_ROOT` 和仓库上方几层的 `peach-data/` 找数据根,所以要同时设 `PEACH_DATA_ROOT`;用默认数据根没有这一步。- 账本里的路径一律是 Windows 形态。Windows 上媒体目录直接写进 `[media.locations]`;macOS 上声明根写 `R:\media`、目录写进 `[media.mounts]`,由本机挂载点负责翻译。CloudDrive 不是必需,任何能挂成本地路径的网盘都行;115 与 PikPak 是推荐项,不是要求。- 界面目前只有中文。diff --git a/docs/HANDOFF.md b/docs/HANDOFF.mdindex 261edf5b7..5a158a30f 100644--- a/docs/HANDOFF.md+++ b/docs/HANDOFF.md@@ -5,9 +5,6 @@## 界面、媒体与复核的既定判据-- Windows 和有 TLS 的 macOS 托盘入口由 HTTP 导航进程与 HTTPS 业务进程组成;HTTP 的 `/healthz` 仅证明跳转进程存活,`/healthz?ready=1` 在业务入口验证配置、页面、数据库和迁移校验和。-- wheel 将 `web`、`migrations`、`resources` 装入 `peach/_resources`;源码维护位置不变。`scripts/smoke_wheel.py` 必须用已安装 wheel 的解释器在仓库外运行,只使用临时账本。-页面密度、控件、提示与响应式的实现门槛见 `.claude/skills/peach-web-ui/SKILL.md`,这里只留代码看不出来的判据。- 截图与视觉验收的画面保护:审查遮挡(设置面板「安全」组,`#censorSetting`,localStorage `peach-censor`)默认关闭、不在导航栏。只有当本轮截图会交给会审查内容的模型(自动视觉审查或外发工具)时才开启,开完记得关;普通个人浏览一律不遮挡。diff --git a/docs/OPERATIONS.md b/docs/OPERATIONS.mdindex 1371b5d50..004313e32 100644--- a/docs/OPERATIONS.md+++ b/docs/OPERATIONS.md@@ -6,6 +6,9 @@## 首次运行与设置文件+- Windows 和有 TLS 的 macOS 托盘由 HTTP 导航进程与 HTTPS 业务进程组成;HTTP 的 `/healthz` 仅证明导航进程存活。业务入口 `/healthz?ready=1` 检查配置、页面、数据库查询和迁移校验和,不就绪返回 503;不带参数仍只探活。+- wheel 将 `web`、`migrations`、`resources` 装入 `peach/_resources`。`scripts/smoke_wheel.py` 在仓库外使用安装 wheel 的解释器运行;CI 消费任务只下载制品,不检出源码。测试只使用临时数据根。+- 设置文件固定是 `<数据根>/config.toml`。数据根按三步找:环境变量 `PEACH_DATA_ROOT`、项目根同级(含上溯四层,覆盖主检出、`peach-worktrees/<任务>` 和打包后的 `dist/Peach/_internal`)的 `peach-data/`、都没有就是「未配置」。优先级是环境变量 > 设置文件 > 内建默认。diff --git a/docs/PRODUCT_BACKLOG.md b/docs/PRODUCT_BACKLOG.mdindex a29394bed..11b07c992 100644--- a/docs/PRODUCT_BACKLOG.md+++ b/docs/PRODUCT_BACKLOG.md@@ -35,15 +35,15 @@9. 女优高清头像的写入侧:`scripts/audit_performer_portraits.py` 已能出候选与实测证据,仍缺按复核结果复制头像文件那一步,以及实体合并后孤立头像的 relink(如 `8022 <- 8168`:只有旧 ID 的 provenance 名唯一命中当前实体、当前目标又不存在时才算候选,不覆盖、不删除旧文件)。10. 文件名与网盘目录整理的落地:`scripts/clean_names.py` 与目录计划只出 dry-run CSV。真正改名要独立维护窗口——停同类任务、确认本机是 writer、SQLite backup、逐条同目录 rename 并同步账本 path/name、失败时文件名回滚,最后跑完整性、外键与路径存在性检查;不跨盘移动,也不按文件夹名猜创作者。11. 来源与默认值通用化(ADR-0023 第 5 阶段候选):`peach init` 的问答已按本机路径只声明 `local`,非交互路径写出的 `DEFAULT_LOCATION_ROOTS`(`R:\media`、`B:/`、`A:/`)仍是维护者的示例盘符。剩两件事:来源用「本地 / 远端挂载」类型字段代替代码里按 `local`/`115`/`pikpak` 名字点名(`web_resource_sync.py` 的 SQL、`media.py` 的 HLS 规则);复制链路支持 win↔win、mac↔mac 与任意一台当写者,目前只验证过 Windows 写者 + macOS 读者。-12. `pip install .`(非 editable)可用:wheel 只含 `src/` 下的包,`migrations/` 与 `web/` 留在仓库根,非可编辑安装下 `migrations.discover()` 抛 `FileNotFoundError`。把这两个目录做成 package data,并让 `PROJECT_ROOT` 的解析在 site-packages 里也成立。-13. `/healthz` 的 `db` 判据改为「`schema_migration` 有行」:目前只看文件存在,空账本也报 available。+12. 非 editable 安装的跨平台验收:wheel 资源与 Windows 基础依赖、仓库外 CLI 冒烟已就绪,仍需取得 macOS、Python 3.12 消费任务结果。+13. 健康检查生产验收:`db` 区分 missing、empty、available、unavailable,`?ready=1` 检查 schema 校验和;待部署后用项目 CA 验证 HTTPS 与损坏/未初始化状态。14. 界面国际化:界面目前只有中文,先补英文。15. 制品对下载者可用与更新渠道:Release 工作流已按 tag 产出 Windows 与 macOS 压缩包,但两者都只是托盘/菜单栏入口,服务仍由仓库 `.venv` 承担,下载者仍要先完成源码安装;要做成不依赖检出的独立发行版,并让托盘「检查更新」对下载的制品也成立(目前只对 git 检出成立)。16. 「第一个小时」教程与故障排查文档:init → 声明来源根 → scan → 打开页面 → 手机信任 CA → 托盘/菜单栏自启动,每一步写清失败表现与对应的排查动作;截图用一套小的 SFW 演示数据集生成,不取自真实馆藏。17. 项目网站:一页说明是什么、截图、安装入口与文档链接。18. 口味导入引导:`/taste` 上传(Takeout ZIP、browserexport 兼容文件)与 `scripts/taste_history.py` 直读本机浏览器库两条路都能用,但没有面向陌生人的文档。需要一页「各浏览器怎么导出、多台设备怎么各自刷新」教程,把脚本折进 `peach` CLI(见第 7 条),并写明定时刷新的安装方式。19. README 瘦身:把「依赖维护」「开发」两节移到 `CONTRIBUTING.md`,「目录」并入 `docs/ARCHITECTURE.md`,「主要页面」「关注与候选」压成一张表;README 只留是什么、边界、前置条件、安装、下载、文档入口与许可证。中英两份同步。-20. 局域网访问还差两件。一是局域网明文口只做跳到 HTTPS 的重定向,不再提供完整应用面:Windows 的 80 和 macOS 的 8900 现在都是完整的 HTTP 服务,口令、cookie 和媒体流在同网段上是明文。二是配对体验:设备第一次访问要人把 43 个字符手输或粘贴进登录页,改成桌面端显示一次性配对码或局域网 HTTPS 地址的二维码,扫完直接写设备 cookie。口令的生成、取用与「绑非回环地址却没有口令就拒绝启动」在 `src/peach/auth.py` 与 `cli._serve_token`,不在这条里。+20. 局域网访问:HTTP 导航与 HTTPS 单一业务入口的候选代码已就绪,待生产部署验证。配对仍需一次性配对码或 HTTPS 地址二维码,减少设备首次访问时手输口令;现有口令生成、取用与非回环无口令拒绝启动不重复实现。21. 全新安装的自动门槛:现有 `Test` 装的是 `-e ".[build,vision,maintenance-115,naming]"` 全套可选依赖、开着 pip 缓存、只跑单元测试,证明不了「陌生用户按 README 装完能用」。补三条互相独立的冒烟:① minimal source——全新 venv、`--no-cache-dir` 只装默认依赖、`peach init`(连跑两次验幂等)、`migrate status`、**离开仓库根目录**再 `peach serve`,请求 `/healthz`、`/`、`/api/items`,覆盖 3.12/3.14 × Windows/macOS 以及无 FFmpeg/OpenSSL/Node 的机器;② wheel——`python -m build` 后在不 checkout 源码的 job 里装 `dist/*.whl` 走同一条链路,它通过才能去掉 README 的 `-e` 硬要求(依赖第 12 条);③ artifact-only——只下载刚构建的制品、不 checkout 源码地跑起来(依赖第 15 条)。消费方一律不许 checkout:工作目录会替漏文件的制品兜底,那是假通过。失败场景也要覆盖:数据根不可写、端口被占、账本损坏、未配置媒体目录、无 FFmpeg、非回环监听但无口令、两个 writer 同时起。22. `peach doctor` 与分级 `/healthz`:`doctor`(另带 `--json`)逐项报版本、数据根可写性、配置文件合法性、数据库能否打开、schema 版本与待执行迁移、FFmpeg/ffprobe/OpenSSL 路径、挂载点可达性、端口占用、是否处在「局域网暴露但无口令」状态、后台任务最近一次失败;输出脱敏,不带口令、cookie、站点凭据和完整媒体路径。`/healthz` 相应从布尔改成分项状态(`database`/`schema`/`configured`/`ffmpeg`/`media_mounts`/`security`),与第 13 条一起做。23. 性能基准:用 SFW 合成数据生成 1k/10k/100k/500k 四档库,nightly 测冷启动到 `/healthz`、目录页与详情页 p95、两字以上搜索 p95、本地 SSD 与网盘挂载的 Range 首字节、空闲 RSS、后台扫描时前台退化倍数、备份期间读请求不失败。门槛用「相对上一次基线下降超过 20%」,不给绝对毫秒数——不同机器不可比。数据集与第 16 条的演示数据集共用。diff --git a/docs/STATUS.md b/docs/STATUS.mdindex d7b8b3a6b..aab845aac 100644--- a/docs/STATUS.md+++ b/docs/STATUS.md@@ -7,8 +7,6 @@## 运行态-- 复评候选代码包含 HTTP 导航入口、提交后缓存失效、图片复验、关系筛选与 wheel 资源;生产仍以下列已验证运行态为准。Windows 最小 wheel 安装与仓库外冒烟已通过,macOS 同项由 CI 验证,未取得本机 macOS 运行证据。-- Windows 是当前 ledger writer,入口 `dist\Peach\Peach.exe`;代码、`peach-data`、worktree 和共享传输点同在一个顶层目录,外置盘只提供 `R:\media`。- 托盘必须以普通权限启动:提升权限后的令牌看不到 CloudDrive 的 `A:` / `B:`,会把 PikPak 和 115 误报为脱盘。- Windows HTTP 为 `0.0.0.0:80`,HTTPS 为当前 LAN IPv4 的 443,mDNS 名见 `[server].mdns_name`;线上版本 `0.7.13`、`ledger_sync=writer`。diff --git a/scripts/smoke_wheel.py b/scripts/smoke_wheel.pyindex 69495f1d6..27cd9f198 100644--- a/scripts/smoke_wheel.py+++ b/scripts/smoke_wheel.py@@ -1,7 +1,11 @@"""在仅安装 wheel 的解释器中,从源码树外验证资源、迁移和 HTTP 关键入口。"""import jsonimport os+import socket+import subprocess+import sysimport tempfile+import timefrom pathlib import Path@@ -9,6 +13,18 @@ def main():with tempfile.TemporaryDirectory(prefix="peach-wheel-") as directory:root = Path(directory).resolve()os.environ["PEACH_DATA_ROOT"] = str(root / "data")+ environment = dict(os.environ, PYTHONIOENCODING="utf-8", PYTHONUTF8="1")+ environment.pop("PYTHONPATH", None)+ command = [sys.executable, "-m", "peach"]+ result = subprocess.run(command + ["init", "--no-input", "--host", "127.0.0.1"],+ cwd=root, env=environment, capture_output=True, text=True,+ encoding="utf-8", timeout=60)+ assert result.returncode == 0, result.stdout + result.stderr+ config = root / "data" / "config.toml"+ original = config.read_bytes()+ repeat = subprocess.run(command + ["init", "--no-input"], cwd=root, env=environment,+ capture_output=True, text=True, encoding="utf-8", timeout=30)+ assert repeat.returncode == 3 and config.read_bytes() == originalfrom fastapi.testclient import TestClientimport peachfrom peach.api import create_app@@ -36,6 +52,34 @@ def main():"checks": ["migrations", "web", "vendor", "island", "auth", "items"]}))finally:client.close()+ with socket.socket() as reservation:+ reservation.bind(("127.0.0.1", 0))+ port = reservation.getsockname()[1]+ import httpx+ with (root / "server.log").open("w", encoding="utf-8") as log:+ process = subprocess.Popen(command + ["serve", "--host", "127.0.0.1", "--port", str(port),+ "--db", str(database), "--token", "wheel-smoke",+ "--no-mdns", "--no-ledger-sync"], cwd=root, env=environment,+ stdout=log, stderr=subprocess.STDOUT,+ creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0)+ try:+ with httpx.Client(base_url=f"http://127.0.0.1:{port}", trust_env=False, timeout=2) as http:+ deadline = time.monotonic() + 30+ while True:+ try:+ assert http.get("/healthz?ready=1").status_code == 200+ break+ except httpx.TransportError:+ if process.poll() is not None or time.monotonic() > deadline:+ raise RuntimeError("wheel 服务未就绪")+ time.sleep(0.1)+ for path in ("/", "/api/items", "/app.js"):+ response = http.get(path, headers={"X-Token": "wheel-smoke"})+ 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)if __name__ == "__main__":diff --git a/src/peach/api.py b/src/peach/api.pyindex ef2cd351b..fac0eca2b 100644--- a/src/peach/api.py+++ b/src/peach/api.py@@ -264,8 +264,8 @@ def create_app(# 都显式声明了 GET+HEAD,只有这个漏了,HEAD 会拿到 405。@app.api_route("/healthz", methods=["GET", "HEAD"])def healthz(ready: bool = False):+ from .health import database_status, readinessif ready:- from .health import readinessresult = readiness(settings)return JSONResponse(result, status_code=200 if result["ready"] else 503,headers={"Cache-Control": "no-store"})@@ -275,7 +275,7 @@ def create_app(return {"ok": True, "service": "peach-api", "version": __version__, "mode": "fastapi",# 这台机器跑过 `peach init` 没有。未配置时服务照常起,只是没有数据。"configured": settings.configured,- "db": "available" if settings.db_path.is_file() else "missing",+ "db": database_status(settings.db_path),"ffmpeg": ffmpeg.source if ffmpeg else "unavailable","mdns": mdns.status if mdns is not None else "disabled","mdns_backend": mdns.backend if mdns is not None else None,diff --git a/src/peach/health.py b/src/peach/health.pyindex ac63d2085..2e049768b 100644--- a/src/peach/health.py+++ b/src/peach/health.py@@ -6,6 +6,18 @@ from .config import MIGRATIONS_DIR, PeachSettingsfrom .migrations import applied, discover+def database_status(path) -> str:+ if not path.is_file():+ return "missing"+ try:+ with closing(sqlite3.connect(path.resolve().as_uri() + "?mode=ro", uri=True,+ timeout=1)) as connection:+ connection.execute("PRAGMA query_only=ON")+ return "available" if applied(connection) else "empty"+ except (sqlite3.Error, OSError):+ return "unavailable"++def readiness(settings: PeachSettings) -> dict:checks = {"configured": bool(settings.configured),"web": settings.page_path.is_file(), "database": False, "schema": False}diff --git a/tests/test_runtime_consistency.py b/tests/test_runtime_consistency.pyindex 1c2ac43c8..d792c6c5f 100644--- a/tests/test_runtime_consistency.py+++ b/tests/test_runtime_consistency.py@@ -11,7 +11,7 @@ from peach.redirect import create_redirect_appfrom peach.routes_media import _image_responsefrom peach.web_state import WebContractfrom peach.config import PeachSettings-from peach.health import readiness+from peach.health import database_status, readinessfrom support.ledger import fresh_ledger@@ -23,7 +23,11 @@ class RuntimeConsistencyTests(unittest.TestCase):settings = PeachSettings(db_path=path, configured=True)self.assertFalse(readiness(settings)["ready"])self.assertFalse(path.exists())+ self.assertEqual(database_status(path), "missing")+ path.touch()+ self.assertEqual(database_status(path), "empty")fresh_ledger(root)+ self.assertEqual(database_status(path), "available")self.assertTrue(readiness(settings)["ready"])connection = sqlite3.connect(path)try: