fix(watcher): handle on_created events for hot-reload in empty directories
Description
When the ./tools directory is empty at Agent initialization and a new tool file is created, the watchdog observer fires an on_created event (not on_modified). The previous implementation only handled on_modified events, causing the first tool added to an empty directory to not be hot-reloaded.
This fix adds on_created handlers to both ToolChangeHandler and MasterChangeHandler classes, ensuring tools are loaded when files are created as well as modified.
Root Cause
The watchdog library fires different events for different file system operations:
on_created: When a new file is createdon_modified: When an existing file is modified
When adding a file to an empty directory, only on_created fires. The previous code only handled on_modified, so the first tool added to an empty ./tools directory was never detected.
Changes
ToolChangeHandler: Addedon_createdmethod that delegates to the same logic ason_modified(via_handle_tool_change)MasterChangeHandler: Addedon_createdmethod that delegates to all registered handlers (via_delegate_event)- Tests: Added comprehensive tests for
on_createdevent handling
Related Issues
Fixes #264
Documentation PR
No documentation changes required - this is a bug fix.
Type of Change
- [x] Bug fix (non-breaking change which fixes an issue)
Testing
- [x] I ran
hatch run prepare - [x] Added 4 new tests for
on_createdevent handling - All 12 watcher tests pass
Checklist
- [x] I have read the CONTRIBUTING document
- [x] I have added tests that prove my fix is effective
- [x] I have updated the documentation accordingly (N/A - bug fix)
- [x] My changes generate no new warnings
- [x] Any dependent changes have been merged and published
Codecov Report
:x: Patch coverage is 72.72727% with 3 lines in your changes missing coverage. Please review.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/strands/tools/watcher.py | 72.72% | 3 Missing :warning: |
:loudspeaker: Thoughts on this report? Let us know!