Commit 16429c8
fix(cli): improve daemon lifecycle and socket security
Addresses all critical and high-priority code review issues:
**Daemon Lifecycle (Critical)**
- Add socket-based shutdown handshake (SHUTDOWN command with OK acknowledgment)
- Implement daemon health checks using parent PID monitoring (every 10s)
- Graceful cleanup with 1s timeout, fallback to force kill
- Prevents orphaned daemon processes
**Socket Security (High)**
- Use XDG_RUNTIME_DIR when available (user-specific, tmpfs-backed)
- Fallback to tmpdir() on non-XDG systems
- Set socket permissions to 0600 (owner-only access)
- Clean up stale socket files on startup
**Connection Reliability (High)**
- Implement retry logic with 5 attempts, 200ms delay
- Promise-based connection with proper event handling
- Remove verbose retry logging, keep only final status
**Code Quality**
- Extract magic numbers to named constants (timing, luminance coefficients)
- Fix type safety: clients Set<any> → Set<Socket>
- Add TTY write validation (verify bytes written)
- Extract duplicated OSC fallback logic into helper function
- Track and cleanup file watchers to prevent memory leaks
- Add structured debug logging for troubleshooting
**Testing**
- Fix integration test mock to return proper writeSync byte count
- All 20 tests passing
- Verified daemon cleanup in real-world scenarios
- Confirmed binary build skips daemon spawning
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>1 parent 36d7f68 commit 16429c8
File tree
6 files changed
+467
-96
lines changed- cli
- src/utils
- __tests__
6 files changed
+467
-96
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
Lines changed: 5 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
43 | 47 | | |
44 | 48 | | |
45 | 49 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
32 | 35 | | |
33 | 36 | | |
34 | 37 | | |
| |||
60 | 63 | | |
61 | 64 | | |
62 | 65 | | |
63 | | - | |
64 | | - | |
| 66 | + | |
| 67 | + | |
65 | 68 | | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
72 | 75 | | |
73 | | - | |
| 76 | + | |
74 | 77 | | |
75 | 78 | | |
76 | 79 | | |
| |||
103 | 106 | | |
104 | 107 | | |
105 | 108 | | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
| 109 | + | |
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| |||
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
204 | | - | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
205 | 209 | | |
206 | 210 | | |
207 | 211 | | |
| |||
225 | 229 | | |
226 | 230 | | |
227 | 231 | | |
228 | | - | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
229 | 237 | | |
230 | 238 | | |
231 | 239 | | |
| |||
295 | 303 | | |
296 | 304 | | |
297 | 305 | | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
302 | 309 | | |
303 | 310 | | |
304 | 311 | | |
| |||
371 | 378 | | |
372 | 379 | | |
373 | 380 | | |
374 | | - | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
375 | 384 | | |
376 | 385 | | |
377 | 386 | | |
| |||
398 | 407 | | |
399 | 408 | | |
400 | 409 | | |
401 | | - | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
402 | 413 | | |
403 | 414 | | |
404 | 415 | | |
| |||
430 | 441 | | |
431 | 442 | | |
432 | 443 | | |
433 | | - | |
434 | | - | |
435 | | - | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
436 | 448 | | |
437 | 449 | | |
438 | 450 | | |
439 | 451 | | |
440 | 452 | | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
445 | | - | |
446 | | - | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
447 | 461 | | |
448 | 462 | | |
449 | 463 | | |
| |||
659 | 673 | | |
660 | 674 | | |
661 | 675 | | |
662 | | - | |
| 676 | + | |
663 | 677 | | |
664 | 678 | | |
665 | 679 | | |
| |||
904 | 918 | | |
905 | 919 | | |
906 | 920 | | |
907 | | - | |
908 | | - | |
909 | | - | |
| 921 | + | |
910 | 922 | | |
911 | 923 | | |
912 | 924 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
18 | 24 | | |
19 | 25 | | |
20 | 26 | | |
| |||
133 | 139 | | |
134 | 140 | | |
135 | 141 | | |
136 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
137 | 155 | | |
138 | 156 | | |
139 | 157 | | |
| |||
185 | 203 | | |
186 | 204 | | |
187 | 205 | | |
188 | | - | |
| 206 | + | |
189 | 207 | | |
190 | 208 | | |
191 | 209 | | |
| |||
199 | 217 | | |
200 | 218 | | |
201 | 219 | | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
202 | 232 | | |
203 | 233 | | |
204 | 234 | | |
| |||
208 | 238 | | |
209 | 239 | | |
210 | 240 | | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
| 241 | + | |
217 | 242 | | |
218 | 243 | | |
219 | 244 | | |
220 | 245 | | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
| 246 | + | |
227 | 247 | | |
228 | 248 | | |
229 | 249 | | |
| |||
0 commit comments