Commit 487faf7
committed
fix(hooks): persist tool stats from short-lived hook processes
PostToolUse hooks run in fresh Python processes that record exactly
one tool call before exiting. With flush_interval=10, that single
record_tool_call() never reached disk, leaving the Stop hook summary
permanently stuck at "[CB] Xm | 0 tools | 0 errors" no matter how
many tools were used in the session.
- post-tool-use.py: explicit stats.flush() right after record for
immediate visibility (statusline, Stop summary).
- stats.py: track every live SessionStats in a module-level WeakSet
and flush them via a single atexit handler. WeakSet keeps GC
unchanged and avoids the per-instance handler leak that a naive
atexit.register(self.method) would cause.
- finalize() drops the instance from the WeakSet so the post-finalize
flush sweep does not resurrect the cleaned-up file.
- Module docstring documents the short-lived process pattern.
- Regression tests cover: WeakSet membership, persistence via the
module flush handler, finalize() removal, and the no-handler-leak
invariant (20 instances must not register 20 handlers).1 parent 64168b1 commit 487faf7
3 files changed
Lines changed: 151 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
5 | 18 | | |
| 19 | + | |
6 | 20 | | |
7 | 21 | | |
8 | 22 | | |
| 23 | + | |
9 | 24 | | |
10 | 25 | | |
11 | 26 | | |
| |||
17 | 32 | | |
18 | 33 | | |
19 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
20 | 62 | | |
21 | | - | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
22 | 67 | | |
23 | 68 | | |
24 | 69 | | |
| |||
67 | 112 | | |
68 | 113 | | |
69 | 114 | | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
70 | 122 | | |
71 | 123 | | |
72 | 124 | | |
| |||
205 | 257 | | |
206 | 258 | | |
207 | 259 | | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
208 | 264 | | |
209 | 265 | | |
210 | 266 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
37 | 44 | | |
38 | 45 | | |
39 | 46 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
207 | 207 | | |
208 | 208 | | |
209 | 209 | | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
210 | 297 | | |
211 | 298 | | |
212 | 299 | | |
| |||
0 commit comments