Peach: Local-first video metadata scraper and media manager.
fix(diagnostics): 核对源码托盘入口并补齐诊断回归接线
longmeidao9a46530e3ad8
diff --git a/docs/OPERATIONS.md b/docs/OPERATIONS.mdindex 991ec6982..a581c4d62 100644--- a/docs/OPERATIONS.md+++ b/docs/OPERATIONS.md@@ -157,6 +157,8 @@ Windows 测试包在配置页「更新与维护 → 卸载 Peach」里卸载:在安装 Peach 的电脑上运行 `peach doctor`,逐项查看版本、配置、账本迁移、工具路径、数据目录写入、媒体挂载、端口和最近一次任务失败。`peach doctor --json` 输出同一份脱敏报告,供本机诊断 API `/api/diagnostics` 使用。+独立包默认诊断配置端口;源码环境有完整 TLS 材料时诊断托盘的 HTTPS 入口(Windows 443、macOS 8443)。直接启动服务时,可用 `--host` 与 `--port` 指定实际监听地址。+`failed` 表示需要处理,命令退出码为 1;`warning` 或 `unknown` 保留具体原因,退出码为 0。诊断只在数据目录创建并清理临时写入探针,不执行迁移或修复。端口正在使用时只报告占用状态,进程归属需另行核对。公开 `/healthz` 只提供状态与原因,完整工具路径只在本机诊断中显示。## 访问地址、密码与证书diff --git a/src/peach/cli.py b/src/peach/cli.pyindex 8338b4175..3538859d9 100644--- a/src/peach/cli.py+++ b/src/peach/cli.py@@ -678,8 +678,10 @@ def _doctor(args: argparse.Namespace) -> int:from .mount_reachability import source_rootsconfig = settings_file.active()- settings = PeachSettings(db_path=args.db, host=config.server.host, port=config.server.port,+ host, port = diagnostics.doctor_endpoint(config)+ settings = PeachSettings(db_path=args.db, host=args.host or host, port=port if args.port is None else args.port,token=auth.read_token(config.directory("secrets")),+ tunnel_enabled=config.tunnel.enabled,access_path=config.directory("secrets") / "access.json")snapshot = {"state": "checking", "sources": [], "warnings": []}if diagnostics.configuration(config, settings.configured)["status"] != "failed":@@ -784,6 +786,8 @@ def build_parser() -> argparse.ArgumentParser:doctor = commands.add_parser("doctor", help="逐项诊断本机配置、数据库、工具与挂载,不执行修复")doctor.add_argument("--json", action="store_true", help="输出脱敏 JSON 报告")doctor.add_argument("--db", type=Path, default=DEFAULT_DB)+ doctor.add_argument("--host", help="诊断指定的服务监听地址")+ doctor.add_argument("--port", type=int, help="诊断指定的服务端口")doctor.set_defaults(handler=_doctor)ledger_sync = commands.add_parser("ledger-sync", help="synchronize the local ledger now")diff --git a/src/peach/diagnostics.py b/src/peach/diagnostics.pyindex 27c2e769a..6c41ecb68 100644--- a/src/peach/diagnostics.py+++ b/src/peach/diagnostics.py@@ -7,11 +7,12 @@ import platformimport shutilimport socketimport sqlite3+import sysimport tempfileimport timefrom contextlib import closing-from . import __version__, access, settings_file+from . import __version__, access, distribution, settings_filefrom .ffmpeg import FFmpegResolverfrom .health import inspect_databasefrom .mount_reachability import MountReachability, PROBE_TIMEOUT@@ -22,6 +23,24 @@ def item(label, status, reason, *, action="", target=None, details=None):"target": target, "details": details or {}}+def doctor_endpoint(config):+ """选择配置端口或具有 TLS 材料的源码托盘入口。"""+ configured = (config.server.host, config.server.port)+ if distribution.standalone():+ return configured+ tls = config.directory("secrets") / "tls"+ if not all((tls / name).is_file() for name in ("peach-local-ca.crt", "peach.crt", "peach.key")):+ return configured+ if sys.platform == "darwin":+ return "0.0.0.0", 8443+ from .mdns import lan_ipv4+ try:+ address = os.environ.get("PEACH_LAN_ADDRESS") or lan_ipv4()+ except (OSError, RuntimeError):+ address = "0.0.0.0"+ return address, 443++def configuration(config, configured, *, validate=True):try:if validate:diff --git a/tests/test_cli.py b/tests/test_cli.pyindex 406e7d77c..531dd1417 100644--- a/tests/test_cli.py+++ b/tests/test_cli.py@@ -388,7 +388,7 @@ class InteractiveInitTests(unittest.TestCase):sources = await client.get("/api/sources", headers={"Cookie": "tok=secret"})return health, items, sources- with mock.patch.object(routes_api, "LOCATION_ROOT_DECLARATIONS", dict(loaded.locations)), \+ with mock.patch("peach.config.LOCATION_ROOT_DECLARATIONS", dict(loaded.locations)), \mock.patch.object(web_resource_sync, "LOCATION_ROOT_DECLARATIONS",dict(loaded.locations)):health, items, sources = asyncio.run(probe())diff --git a/tests/test_diagnostics.py b/tests/test_diagnostics.pyindex 845cd0f69..cc886f79e 100644--- a/tests/test_diagnostics.py+++ b/tests/test_diagnostics.py@@ -29,9 +29,11 @@ class DiagnosticsTests(unittest.TestCase):self.addCleanup(self.temp.cleanup)self.root = Path(self.temp.name).resolve()self.db = self.root / "ledger.db"+ self.page = self.root / "index.html"+ self.page.write_text("<!doctype html><title>Peach</title>", encoding="utf-8")self.config = PeachConfig(self.root, self.root / "config.toml", data_root_found=True,server=ServerSettings(host="127.0.0.1", port=0))- self.settings = PeachSettings(db_path=self.db, page_path=ROOT / "web/index.html",+ self.settings = PeachSettings(db_path=self.db, page_path=self.page,host="127.0.0.1", port=0, configured=True,access_path=self.root / "access.json", token="private-token",ffmpeg_root=self.root / "tools")@@ -113,6 +115,22 @@ class DiagnosticsTests(unittest.TestCase):self.assertEqual(result["status"], "unknown")self.assertNotIn("secret", json.dumps(result))+ def test_doctor_uses_source_tray_tls_and_standalone_configuration_endpoints(self):+ self.assertEqual(diagnostics.doctor_endpoint(self.config), ("127.0.0.1", 0))+ tls = self.config.directory("secrets") / "tls"+ tls.mkdir(parents=True)+ for name in ("peach-local-ca.crt", "peach.crt", "peach.key"):+ (tls / name).write_text("fixture", encoding="utf-8")+ with patch.object(diagnostics.distribution, "standalone", return_value=False), \+ patch.object(diagnostics.sys, "platform", "win32"), \+ patch.dict(diagnostics.os.environ, {"PEACH_LAN_ADDRESS": "192.0.2.10"}):+ self.assertEqual(diagnostics.doctor_endpoint(self.config), ("192.0.2.10", 443))+ with patch.object(diagnostics.distribution, "standalone", return_value=False), \+ patch.object(diagnostics.sys, "platform", "darwin"):+ self.assertEqual(diagnostics.doctor_endpoint(self.config), ("0.0.0.0", 8443))+ with patch.object(diagnostics.distribution, "standalone", return_value=True):+ self.assertEqual(diagnostics.doctor_endpoint(self.config), ("127.0.0.1", 0))+def test_mount_unknown_and_failure_do_not_claim_online_or_break_service_readiness(self):upgrade(self.db, ROOT / "migrations")for state, expected in (("checking", "unknown"), ("degraded", "warning")):