permissions hook helper
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix notification: use file-existence check instead of process kill, bump delay to 5s

+13 -19
+13 -19
chook.mjs
··· 247 247 248 248 // --- Pending notification --- 249 249 250 - const NOTIFY_DELAY_SECS = 3; 250 + const NOTIFY_DELAY_SECS = 5; 251 251 252 252 function pendingFile(sessionId) { 253 253 return `/tmp/chook-pending-${sessionId}`; ··· 255 255 256 256 function scheduleNotification(sessionId, toolName, detail) { 257 257 const short = detail.length > 80 ? detail.slice(0, 77) + "..." : detail; 258 - const msg = short.replace(/"/g, '\\"'); 259 - // Spawn a detached shell that sleeps then notifies 260 - const child = spawn( 261 - "sh", 262 - [ 263 - "-c", 264 - `sleep ${NOTIFY_DELAY_SECS} && osascript -e 'display notification "${msg}" with title "chook: ${toolName} needs approval"'`, 265 - ], 266 - { detached: true, stdio: "ignore" }, 267 - ); 258 + const msg = short.replace(/'/g, "'\\''"); 259 + const pf = pendingFile(sessionId); 260 + // The script checks if the pending file still exists before notifying. 261 + // PostToolUse deletes it to cancel. 262 + const script = `sleep ${NOTIFY_DELAY_SECS}; if [ -f '${pf}' ]; then osascript -e 'display notification "${msg}" with title "chook: ${toolName} needs approval"'; rm -f '${pf}'; fi`; 263 + const child = spawn("sh", ["-c", script], { 264 + detached: true, 265 + stdio: "ignore", 266 + }); 268 267 child.unref(); 269 - // Write PID so PostToolUse can cancel it 270 268 try { 271 - writeFileSync(pendingFile(sessionId), String(child.pid)); 269 + writeFileSync(pf, String(child.pid)); 272 270 } catch {} 273 271 } 274 272 275 273 function cancelNotification(sessionId) { 276 - const f = pendingFile(sessionId); 274 + // Just delete the file — the sleeping script checks for it before notifying 277 275 try { 278 - const pid = parseInt(readFileSync(f, "utf-8").trim()); 279 - // Kill the process group (negative PID) to get the sleep + osascript 280 - try { process.kill(-pid); } catch {} 281 - try { process.kill(pid); } catch {} 282 - unlinkSync(f); 276 + unlinkSync(pendingFile(sessionId)); 283 277 } catch {} 284 278 } 285 279