← Back to PRs

#17816: Fix media store cleanup ignoring subdirectories (e.g. inbound)

by Clawborn open 2026-02-16 06:24 View on GitHub →
stale size: S trusted-contributor
## Description The `cleanOldMedia` function only scanned the root media directory, ignoring subdirectories like `inbound/` where Matrix, Signal, and other plugins store received media. This caused the `media/inbound` directory to grow indefinitely. ## Fix 1. Made `cleanOldMedia` recursive. 2. Trigger cleanup on inbound media save (was only on outbound). 3. Added throttling (max once per minute) to prevent excessive filesystem scanning. ## Tests Added `src/media/cleanup.test.ts` verifying: - Root files are cleaned up - Subdirectory files are cleaned up (failed before fix) ## AI Transparency - **Assisted by**: OpenClaw Agent (Claude 3.5 Sonnet / Opus) - **Testing level**: Fully tested with new unit tests (`src/media/cleanup.test.ts`) - **Prompt strategy**: Self-correction loop based on codebase analysis <!-- greptile_comment --> <h3>Greptile Summary</h3> Made `cleanOldMedia` recursive to scan subdirectories (like `media/inbound/`) instead of only the root `media/` directory. Added cleanup call in `saveMediaBuffer` so inbound media triggers cleanup. Added throttling (max once per minute) to prevent excessive filesystem scanning. **Key changes:** - Recursive directory scanning now cleans files in all subdirectories - Throttle mechanism prevents cleanup from running more than once per 60 seconds - `saveMediaBuffer` now triggers cleanup (previously only `saveMediaSource` did) - Added test coverage for both root and subdirectory cleanup **Note:** One inline comment about a potential race condition in the throttle mechanism is incorrect - the throttle is safe because the check and update happen synchronously before any `await` points in Node.js's single-threaded event loop. <h3>Confidence Score: 4/5</h3> - Safe to merge with one incorrect inline comment that can be disregarded - The implementation correctly makes cleanup recursive and adds throttling. Tests verify the fix works. One inline comment about a race condition is technically incorrect (the throttle is actually safe in Node's single-threaded model), but the code itself is correct. The fire-and-forget pattern in `saveMediaBuffer` (void + catch) is appropriate. - No files require special attention - the implementation is sound despite one mistaken inline comment <sub>Last reviewed commit: 1f63416</sub> <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs