Peach: Local-first video metadata scraper and media manager.
chore(hooks): 发版提醒的钩子由 uv 解析解释器
longmeidaobac48a4d3173
diff --git a/.claude/settings.json b/.claude/settings.jsonindex d97eb6423..d2b17f7eb 100644--- a/.claude/settings.json+++ b/.claude/settings.json@@ -8,7 +8,7 @@"hooks": {"Stop": [{"hooks": [{"type": "command", "command": "${CLAUDE_PROJECT_DIR}/.venv/Scripts/python.exe", "args": ["${CLAUDE_PROJECT_DIR}/scripts/job_status.py", "--write", "--hook-event"], "timeout": 30},- {"type": "command", "command": "${CLAUDE_PROJECT_DIR}/.venv/Scripts/python.exe", "args": ["${CLAUDE_PROJECT_DIR}/scripts/release_due.py", "--hook-event"], "timeout": 30}+ {"type": "command", "command": "uv", "args": ["run", "--no-project", "python", "${CLAUDE_PROJECT_DIR}/scripts/release_due.py", "--hook-event"], "timeout": 30}]}],"StopFailure": [{"hooks": [{"type": "command", "command": "${CLAUDE_PROJECT_DIR}/.venv/Scripts/python.exe", "args": ["${CLAUDE_PROJECT_DIR}/scripts/job_status.py", "--write", "--hook-event"], "timeout": 30}]}],"SessionEnd": [{"hooks": [{"type": "command", "command": "${CLAUDE_PROJECT_DIR}/.venv/Scripts/python.exe", "args": ["${CLAUDE_PROJECT_DIR}/scripts/job_status.py", "--write", "--hook-event"], "timeout": 30}]}]diff --git a/.codex/hooks.json b/.codex/hooks.jsonindex a13b39c94..685ff121a 100644--- a/.codex/hooks.json+++ b/.codex/hooks.json@@ -1,7 +1,7 @@{"hooks": {"Stop": [{"hooks": [- {"type": "command", "command": ".venv/Scripts/python.exe scripts/release_due.py --hook-event", "timeout": 30}+ {"type": "command", "command": "uv run --no-project python scripts/release_due.py --hook-event", "timeout": 30}]}]}}diff --git a/docs/OPERATIONS.md b/docs/OPERATIONS.mdindex 44c4c47e9..ae6349ec7 100644--- a/docs/OPERATIONS.md+++ b/docs/OPERATIONS.md@@ -183,7 +183,8 @@ curl -s --noproxy '*' -o /dev/null -w '%{http_code}\n' https://peach.local/healt- 中途停下(网络断、CI 还在跑、等超时)就再跑一次同一条命令:每一步都先看当下状态再决定做不做,已提交的不重提,已推送的不重推,Test 没绿绝不打标签。- `scripts/agent_worktree.py integrate` 不碰版本号,只在结果里报当前值;主线上跑着的是哪一份由 commit 与 `build-info.json` 认定。- 「什么时候该发」也不靠人判断:`integrate` 的输出带一个 `release` 字段(`changelog.due()`),`due` 为真时智能体在收尾照 `why` 提出来。节奏是每周一次、攒够提前、破坏性变化与安全修复不等周期(`DUE_DAYS = 7`、`DUE_ENTRIES = 10`)。判据只数使用者看得见的条目,不数提交:一百个重构提交对使用者是零,那正是这份判据要跟「集成了多少次」分开的地方。-- 同一份判断还挂在每次会话收尾上:`scripts/release_due.py --hook-event` 由 Claude 的 `.claude/settings.json` 与 Codex 的 `.codex/hooks.json` 里的 `Stop` 钩子调用。两家的钩子约定一致——stdin 收 JSON、stdout 回 `systemMessage`、退出码 0,所以一个脚本供两边。挂在这里才覆盖得到「一周没集成」的那几天,而那正是最容易忘的时候。四道门先挡住不该出声的场合:不在主检出、不在 master、工作区不干净、这个 master sha 已经问过;四条全过且 `due` 为真才说话,其余情形一个字都不打印。闩记在 `<数据根>/state/release-due.json`,master 前进了才会再问。人手查用不带参数的 `python scripts/release_due.py`,那条只读,不动闩。钩子命令里写的是 `.venv/Scripts/python.exe`,在 macOS 上要换成 `.venv/bin/python` 才跑得起来。+- 同一份判断还挂在每次会话收尾上:`scripts/release_due.py --hook-event` 由 Claude 的 `.claude/settings.json` 与 Codex 的 `.codex/hooks.json` 里的 `Stop` 钩子调用。两家的钩子约定一致——stdin 收 JSON、stdout 回 `systemMessage`、退出码 0,所以一个脚本供两边。挂在这里才覆盖得到「一周没集成」的那几天,而那正是最容易忘的时候。四道门先挡住不该出声的场合:不在主检出、不在 master、工作区不干净、这个 master sha 已经问过;四条全过且 `due` 为真才说话,其余情形一个字都不打印。闩记在 `<数据根>/state/release-due.json`,master 前进了才会再问。人手查用不带参数的 `python scripts/release_due.py`,那条只读,不动闩。+- 钩子的解释器交给 `uv run --no-project` 解析,所以一条命令通吃两个平台,配置里不出现 `.venv/Scripts` 或 `.venv/bin`。钩子配置不支持按操作系统分支(`if` 只对工具事件生效,也不认平台),写死平台子目录的话另一台上解释器根本不存在,钩子每轮报一次错;挂两条让错的那条自然失败同样不行,报错本身就是每轮一行的噪音,而这条提醒的全部价值在于不该说的时候一个字都不说。`--no-project` 不是随手加的开关:`release_due.py` 整条导入链都是标准库,不需要项目的 venv;一旦让 `uv` 认项目,它在一个有 `pyproject.toml` 却还没建 venv 的检出里会顺手建一个空的,`scripts/test.ps1` 随后优先选中它,那棵树的测试连 `filelock` 都导不进来——钩子每轮都跑,这种副作用会落到每一个新工作树上。`tests/test_release_due.py` 里那条测试把配置中的命令原样执行一遍,并检查工作目录跑完没有多出 `.venv`。实测耗时 0.6 秒,`timeout` 给的 30 秒绰绰有余。这台机器上 `uv` 由 `winget install --id astral-sh.uv` 装的,走 winget 而不是把远端脚本管道进 shell 执行。`scripts/job_status.py` 那三个钩子仍写着 `.venv/Scripts/python.exe`,在 macOS 上跑不起来。- 时间那一半有先例可依(Firefox 四周、Ubuntu 与 GNOME 半年都把「要不要发」交给日历);条数那一半没有可靠样本。已发四版各带 6、8、6、9 条,但那是「每次集成推一格」时期的产物,反映的是那三天写了多少代码,不是多少变化值得让人下载一次。`DUE_ENTRIES` 是没有样本时的保守起点,`due()` 每次都报出实际条目数与分组,积累几次真实发布之后拿那几个数回来校准,别再拿旧机制的数字当依据。- PyInstaller 的资源直接位于 `sys._MEIPASS`,没有源码树的 `src/` 层;打包后的 `migrate`、Web 与品牌资源必须从这里解析,不能对 `config.py` 固定取 `parents[2]`。- 创建 Win32 窗口前必须启用 Per-Monitor V2 DPI;正常动作不弹模态 MessageBox,更新检查在后台线程执行并用 pystray 原生非模态通知反馈。diff --git a/tests/test_job_status.py b/tests/test_job_status.pyindex 9fd76ef9f..f7dc66d91 100644--- a/tests/test_job_status.py+++ b/tests/test_job_status.py@@ -23,7 +23,12 @@ class JobStatusTests(unittest.TestCase):这台机器上裸 `python` 曾解析到 Microsoft Store 的 MSIX 别名:路径存在、执行报 FileNotFoundError,而 hook 的失败没人盯着看。写成项目内的- `.venv` 解释器,缺了就是缺了,不会解析到另一个 Python。"""+ `.venv` 解释器,缺了就是缺了,不会解析到另一个 Python。++ `uv` 是唯一另一个允许出现的命令。它不是 `python`,顶不到那个别名上;而它那+ 一条的整条命令由 `tests/test_release_due.py` 原样执行一遍,PATH 上没有它就是+ 测试红,不是 hook 静默。走 `uv` 是因为那条 hook 要在 Windows 和 macOS 上都+ 跑,而 hook 配置不支持按平台分支。"""settings = json.loads((SCRIPT.parents[1] / ".claude" / "settings.json").read_text(encoding="utf-8"))@@ -33,7 +38,8 @@ class JobStatusTests(unittest.TestCase):for matcher in eventfor hook in matcher["hooks"]}- self.assertEqual(commands, {"${CLAUDE_PROJECT_DIR}/.venv/Scripts/python.exe"})+ self.assertEqual(commands,+ {"${CLAUDE_PROJECT_DIR}/.venv/Scripts/python.exe", "uv"})result = subprocess.run([sys.executable, str(SCRIPT), "--help"],cwd=SCRIPT.parents[1],diff --git a/tests/test_release_due.py b/tests/test_release_due.pyindex 0cd69de4d..52d36fe86 100644--- a/tests/test_release_due.py+++ b/tests/test_release_due.py@@ -171,21 +171,85 @@ class WiringTests(unittest.TestCase):ROOT = Path(__file__).resolve().parents[1]- def test_the_claude_stop_hook_runs_the_check(self):+ def claude_stop_argv(self) -> list[list[str]]:settings = json.loads((self.ROOT / ".claude" / "settings.json").read_text(encoding="utf-8"))- commands = [" ".join([entry["command"], *entry.get("args", [])])- for group in settings["hooks"]["Stop"] for entry in group["hooks"]]- self.assertTrue(any("scripts/release_due.py" in line and "--hook-event" in line- for line in commands), commands)+ return [[entry["command"], *entry.get("args", [])]+ for group in settings["hooks"]["Stop"] for entry in group["hooks"]]- def test_the_codex_stop_hook_runs_the_same_check(self):+ def claude_stop_commands(self) -> list[str]:+ return [" ".join(argv) for argv in self.claude_stop_argv()]++ def codex_stop_commands(self) -> list[str]:hooks = json.loads((self.ROOT / ".codex" / "hooks.json").read_text(encoding="utf-8"))- commands = [entry["command"]- for group in hooks["hooks"]["Stop"] for entry in group["hooks"]]- self.assertTrue(any("scripts/release_due.py" in line and "--hook-event" in line- for line in commands), commands)+ return [entry["command"]+ for group in hooks["hooks"]["Stop"] for entry in group["hooks"]]++ def called_by(self, commands: list[str]) -> str:+ found = [line for line in commands+ if "scripts/release_due.py" in line and "--hook-event" in line]+ self.assertEqual(len(found), 1, commands)+ return found[0]++ def called_by_argv(self, every: list[list[str]]) -> list[str]:+ found = [argv for argv in every+ if any("scripts/release_due.py" in part for part in argv)]+ self.assertEqual(len(found), 1, every)+ return found[0]++ def test_the_claude_stop_hook_runs_the_check(self):+ self.called_by(self.claude_stop_commands())++ def test_the_codex_stop_hook_runs_the_same_check(self):+ self.called_by(self.codex_stop_commands())++ def test_both_entries_launch_through_uv_so_one_line_serves_both_platforms(self):+ """解释器由 `uv` 解析,命令里不出现 venv 的平台子目录。++ 钩子配置不支持按操作系统分支(`if` 只对工具事件生效,也不认平台),所以写死+ `.venv/Scripts` 的那一台以外,解释器根本不存在,钩子每轮报一次错。挂两条让+ 错的那条自然失败也不行:报错本身就是每轮一行的噪音,而这条提醒的全部价值在于+ 不该说的时候一个字都不说。++ `--no-project` 是这里的重点,不是随手加的开关。这个脚本整条导入链都是标准库,+ 不需要项目的 venv;而带上项目的话,`uv` 在一个有 `pyproject.toml` 却还没建+ venv 的检出里会顺手建一个空的,`scripts/test.ps1` 随后优先选中它,那棵树的+ 测试从此连 `filelock` 都导不进来。钩子每轮都跑,这种副作用会落在每一个新工作树上。+ """+ for commands in (self.claude_stop_commands(), self.codex_stop_commands()):+ line = self.called_by(commands)+ self.assertRegex(line, r"(^|[/\\\s])uv(\.exe)?\s")+ self.assertIn("run", line)+ self.assertIn("--no-project", line)+ for platform_only in (".venv/Scripts", ".venv\\Scripts", ".venv/bin"):+ self.assertNotIn(platform_only, line)++ def test_the_configured_command_runs_without_leaving_a_venv_behind(self):+ """把配置里那条命令原样跑一遍,落在一个装成项目的空目录里。++ 两件事一起验。一是它跑得起来:这条提醒设计成不该说时一个字都不打印,所以+ 「命令根本跑不起来」和「没什么要说的」在屏幕上长得一模一样,字符串比对认不出+ 这种失败——装没装 `uv`、PATH 上是不是别的东西,只有真执行一次才知道;裸+ `python` 曾解析到 MSIX 别名,路径在、一执行就报错,正是这一类。二是它什么都+ 不留下:工作目录摆着 `pyproject.toml`,跑完那里不能多出一个 `.venv`。+ """+ argv = [part.replace("${CLAUDE_PROJECT_DIR}", str(self.ROOT))+ for part in self.called_by_argv(self.claude_stop_argv())]+ with tempfile.TemporaryDirectory() as elsewhere:+ looks_like_a_project = Path(elsewhere).resolve()+ (looks_like_a_project / "pyproject.toml").write_text(+ '[project]\nname = "bait"\nversion = "0"\n', encoding="utf-8")+ done = subprocess.run(+ argv, cwd=str(looks_like_a_project),+ input=json.dumps({"hook_event_name": "Stop",+ "cwd": str(looks_like_a_project)}),+ capture_output=True, text=True, encoding="utf-8",+ errors="replace", check=False)+ left_behind = sorted(path.name for path in looks_like_a_project.iterdir())+ self.assertEqual(done.returncode, 0, done.stderr)+ self.assertEqual(done.stdout, "")+ self.assertEqual(left_behind, ["pyproject.toml"])if __name__ == "__main__":