Repository navigation
fix: add bounds checking to BatchKVCache::filter() and optimizer state observability - #18
Conversation
…e observability BatchKVCache::filter() previously had no bounds validation on batch indices, which could panic (or cause UB in release mode) with negative or out-of-range values. Added upfront validation returning descriptive errors instead of panicking, plus 5 new test cases covering edge cases. Added state_count() to all 4 optimizers (Adam, AdamW, SGD, RMSprop) for monitoring tracked parameter count and memory usage. Added 7 new tests verifying reset() properly clears all state. +12 new Rust tests, 726 total passing. Co-Authored-By: Claude Opus 4.6 <[email protected]>
📝 WalkthroughWalkthroughThis PR adds state tracking observability methods ( Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~30 minutes Poem
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…eflight after restore, document display-only sizing Fix-delta review of the review-#17 fixes surfaced two recovery-ordering defects (one a regression from the #17 preflight hoist) plus one display-only residual. All verified via control-flow analysis. - Finding 3 (download.ts, regression): the #17 hoist put the ownership preflight above sweepStaging, so a preflight-refused job (unowned/foreign finalDir) threw before the only dead-private-staging-tree reap ran, leaking crashed multi-GB staging indefinitely. Split sweepStaging into reapDeadStagingTrees() (process-wide, pid-gated dead-`<slug>@<sha>.<pid>` reap, run BEFORE the preflight so a refused job still frees its tree) and recoverBackup(slug, finalDir) (slug-scoped backup restore/reap, run AFTER the preflight so a foreign finalDir link is already refused and never followed). - Finding 1 (download.ts): the backup restore branch renamed any backup back onto an absent finalDir; a foreign-symlink backup (from a prior overwrite crash) would be restored after the only ownership check and then followed by skip-detection. Restore now requires the backup itself be an owned real directory (no-follow lstat + our marker), and the no-follow ownership preflight is re-run after recovery, before skip-detection. - Finding 2 (models.ts + docs, document-and-accept): walkDirStats traverses lexical paths, so a concurrent ancestor/TOCTOU symlink swap during the walk can redirect sizing. Its output is display-only (the Models table + the delete-confirmation "(X GB)" hint) and never drives a destructive decision (deletion uses the path-safe name, not the size), so the worst case is a wrong size number under adversarial mutation of the user's own store. A robust fix needs descriptor-relative traversal node:fs does not expose; documented as an accepted residual in code and docs/dashboard.md. Gate: typecheck 0; dashboard 202/202 (×2); lint 0. Dashboard-only — native and api.ts unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01VKtaz9j1ih1YtSPBf5TtQm
…eflight after restore, document display-only sizing Fix-delta review of the review-#17 fixes surfaced two recovery-ordering defects (one a regression from the #17 preflight hoist) plus one display-only residual. All verified via control-flow analysis. - Finding 3 (download.ts, regression): the #17 hoist put the ownership preflight above sweepStaging, so a preflight-refused job (unowned/foreign finalDir) threw before the only dead-private-staging-tree reap ran, leaking crashed multi-GB staging indefinitely. Split sweepStaging into reapDeadStagingTrees() (process-wide, pid-gated dead-`<slug>@<sha>.<pid>` reap, run BEFORE the preflight so a refused job still frees its tree) and recoverBackup(slug, finalDir) (slug-scoped backup restore/reap, run AFTER the preflight so a foreign finalDir link is already refused and never followed). - Finding 1 (download.ts): the backup restore branch renamed any backup back onto an absent finalDir; a foreign-symlink backup (from a prior overwrite crash) would be restored after the only ownership check and then followed by skip-detection. Restore now requires the backup itself be an owned real directory (no-follow lstat + our marker), and the no-follow ownership preflight is re-run after recovery, before skip-detection. - Finding 2 (models.ts + docs, document-and-accept): walkDirStats traverses lexical paths, so a concurrent ancestor/TOCTOU symlink swap during the walk can redirect sizing. Its output is display-only (the Models table + the delete-confirmation "(X GB)" hint) and never drives a destructive decision (deletion uses the path-safe name, not the size), so the worst case is a wrong size number under adversarial mutation of the user's own store. A robust fix needs descriptor-relative traversal node:fs does not expose; documented as an accepted residual in code and docs/dashboard.md. Gate: typecheck 0; dashboard 202/202 (×2); lint 0. Dashboard-only — native and api.ts unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01VKtaz9j1ih1YtSPBf5TtQm
…eflight after restore, document display-only sizing Fix-delta review of the review-#17 fixes surfaced two recovery-ordering defects (one a regression from the #17 preflight hoist) plus one display-only residual. All verified via control-flow analysis. - Finding 3 (download.ts, regression): the #17 hoist put the ownership preflight above sweepStaging, so a preflight-refused job (unowned/foreign finalDir) threw before the only dead-private-staging-tree reap ran, leaking crashed multi-GB staging indefinitely. Split sweepStaging into reapDeadStagingTrees() (process-wide, pid-gated dead-`<slug>@<sha>.<pid>` reap, run BEFORE the preflight so a refused job still frees its tree) and recoverBackup(slug, finalDir) (slug-scoped backup restore/reap, run AFTER the preflight so a foreign finalDir link is already refused and never followed). - Finding 1 (download.ts): the backup restore branch renamed any backup back onto an absent finalDir; a foreign-symlink backup (from a prior overwrite crash) would be restored after the only ownership check and then followed by skip-detection. Restore now requires the backup itself be an owned real directory (no-follow lstat + our marker), and the no-follow ownership preflight is re-run after recovery, before skip-detection. - Finding 2 (models.ts + docs, document-and-accept): walkDirStats traverses lexical paths, so a concurrent ancestor/TOCTOU symlink swap during the walk can redirect sizing. Its output is display-only (the Models table + the delete-confirmation "(X GB)" hint) and never drives a destructive decision (deletion uses the path-safe name, not the size), so the worst case is a wrong size number under adversarial mutation of the user's own store. A robust fix needs descriptor-relative traversal node:fs does not expose; documented as an accepted residual in code and docs/dashboard.md. Gate: typecheck 0; dashboard 202/202 (×2); lint 0. Dashboard-only — native and api.ts unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01VKtaz9j1ih1YtSPBf5TtQm
BatchKVCache::filter() previously had no bounds validation on batch indices, which could panic (or cause UB in release mode) with negative or out-of-range values. Added upfront validation returning descriptive errors instead of panicking, plus 5 new test cases covering edge cases.
Added state_count() to all 4 optimizers (Adam, AdamW, SGD, RMSprop) for monitoring tracked parameter count and memory usage. Added 7 new tests verifying reset() properly clears all state.
+12 new Rust tests, 726 total passing.
Note
Low Risk
Changes are additive and defensive (new checks + observability) with test coverage, and should only affect previously-invalid inputs or provide new inspection APIs.
Overview
Prevents invalid batch indices from causing panics/UB by adding upfront bounds checks in
BatchKVCache::filter(), returning a descriptiveInvalidArgerror; adds new tests covering out-of-range, negative, empty, duplicate, and empty-cache filtering.Adds
state_count()toAdam,AdamW,SGD, andRMSpropto report how many parameters have optimizer state allocated (useful for memory monitoring), and extends optimizer tests to verify state tracking and thatreset()fully clears state (andstepwhere applicable), including for batch updates.Written by Cursor Bugbot for commit 57626f4. This will update automatically on new commits. Configure here.
Summary by CodeRabbit
New Features
Tests