Files
chuan 6438b77133 fix: 后台下载改用 Start-Process,避免子进程继承管道句柄阻塞 hook
start /b 派生的进程会继承 Claude 传给 hook 的 stdout 管道句柄,curl 攥着不放
导致 Claude 读不到 EOF、直到下载完才放行(hook 被卡住)。改用 powershell
Start-Process 启动下载,它默认不继承父进程句柄,hook 立即返回、下载后台继续。
2026-06-24 01:29:27 +08:00

67 lines
3.1 KiB
Batchfile

@echo off
rem ============================================================
rem Download URL (notify.exe is fetched from here on first run) -- edit as needed
set "DOWNLOAD_URL=https://git.pchuan.top/cc-tools/notify/releases/download/v1.0.0/notify.exe"
rem ============================================================
setlocal
set "BIN=%~dp0..\bin"
set "EXE=%BIN%\notify.exe"
set "PART=%BIN%\notify.exe.partial"
set "LOCK=%BIN%\notify.download.lock"
rem hidden self-reinvocation: detached background downloader (see :downloader)
if "%~1"=="__download" goto downloader
rem common path: exe present -> run directly (keeps piped stdin intact)
if exist "%EXE%" (
"%EXE%" %*
endlocal & exit /b
)
rem ---- exe missing: never block the hook ----
rem kick off the download once (atomic mkdir lock); a detached worker survives the
rem hook timeout. then report progress to Claude and return immediately.
if not exist "%BIN%" mkdir "%BIN%" 2>nul
rem self-heal: if a previous worker was hard-killed (shutdown/crash) it may leave a
rem stale lock that blocks all retries. reclaim it so -C - can resume the .partial.
if exist "%LOCK%" call :reclaim
rem atomic lock: only the first hook spawns the worker; others fall through and just
rem report progress -> no duplicate downloads even when hooks fire concurrently.
rem
rem must use Start-Process (not `start /b`): a child started by cmd inherits the
rem hook's stdout pipe handle, so Claude won't see EOF until the download ends ->
rem the hook would block. Start-Process spawns WITHOUT inheriting handles, so the
rem hook returns immediately while curl keeps running detached.
mkdir "%LOCK%" 2>nul && powershell -nop -w hidden -c "Start-Process -WindowStyle Hidden -FilePath '%~f0' -ArgumentList '__download'" >nul 2>&1
rem downloaded size so far (from the .partial file), shown as X.X MB
set "DLBYTES=0"
if exist "%PART%" for %%A in ("%PART%") do set "DLBYTES=%%~zA"
set /a DLKB=DLBYTES/1024
set /a DLMB=DLKB/1024
set /a DLF=(DLKB*10/1024)%%10
rem exit 0 so Claude parses this JSON; systemMessage is shown to the user, the
rem notification itself is skipped this time (suppressOutput hides raw stdout)
echo {"suppressOutput":true,"systemMessage":"[Claude Code Notify] notifier not ready, downloading in background: %DLMB%.%DLF% MB / ~14 MB done. Works automatically once finished; this notification is skipped."}
endlocal & exit /b 0
:downloader
rem detached worker: resume-capable download (-C -), atomic install, always free lock.
rem if curl fails (slow/flaky net), the .partial is kept and the next hook resumes it.
curl -fsSL -C - "%DOWNLOAD_URL%" -o "%PART%"
if not errorlevel 1 move /y "%PART%" "%EXE%" >nul 2>&1
rmdir "%LOCK%" 2>nul
endlocal & exit /b
:reclaim
rem no .partial yet => worker died before downloading anything; reclaim immediately
if not exist "%PART%" ( rmdir "%LOCK%" 2>nul & goto :eof )
rem .partial idle for >120s => worker is dead (a live curl writes continuously); reclaim
for /f "delims=" %%T in ('powershell -nop -c "[int]((Get-Date)-(Get-Item '%PART%').LastWriteTime).TotalSeconds" 2^>nul') do set "IDLE=%%T"
if not defined IDLE goto :eof
if %IDLE% geq 120 rmdir "%LOCK%" 2>nul
goto :eof