sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

fix(watcher): handle on_created events for hot-reload in empty directories

Open strands-agent opened this issue 10 months ago • 1 comments

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 created
  • on_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

  1. ToolChangeHandler: Added on_created method that delegates to the same logic as on_modified (via _handle_tool_change)
  2. MasterChangeHandler: Added on_created method that delegates to all registered handlers (via _delegate_event)
  3. Tests: Added comprehensive tests for on_created event 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_created event 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

strands-agent avatar Jan 17 '26 00:01 strands-agent

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!

codecov[bot] avatar Jan 18 '26 02:01 codecov[bot]