Source Code:
src/gaia/utils/file_watcher.pyComponent: FileChangeHandler
Module:
gaia.utils.file_watcher
Import: from gaia.utils import FileChangeHandlerOverview
FileChangeHandler provides a reusable file system watcher for GAIA agents. It monitors directories for file changes (create, modify, delete) and triggers callbacks, enabling agents to automatically process new files. Key Features:- File event detection (create, modify, delete)
- Callback-based architecture
- File extension filtering
- Pattern-based ignore rules
- Reports a file once it has finished being written, so a burst of events is one callback
- Integration with watchdog library
Requirements
Functional Requirements
-
File Event Handling
- Detect file creation
- Detect file modification
- Detect file deletion
- Optional: Detect file moves
-
Filtering
- File extension filters
- Filename pattern matching
- Ignore patterns (e.g., temp files, hidden files)
-
Settling
- Report a created or modified file once it has had no events, and no size or mtime change, for the configured window
- Coalesce rapid successive changes into one callback
- Run callbacks on a dispatch thread, off the observer thread
-
Callback System
on_created(event)callbackon_modified(event)callbackon_deleted(event)callback- Pass file path and metadata
-
Integration with watchdog
- Extends
FileSystemEventHandler - Works with
Observer - Thread-safe
- Extends
Non-Functional Requirements
-
Performance
- Low overhead monitoring
- Efficient event filtering
- Non-blocking callbacks
-
Reliability
- Handle edge cases (permission errors, symlinks)
- Graceful degradation
- Proper cleanup
-
Usability
- Simple API
- Good defaults
- Clear error messages
API Specification
File Location
Public Interface
Testing Requirements
Unit Tests
File:tests/sdk/test_file_change_handler.py
Usage Examples
Example 1: Form Intake Agent (Auto-Process Forms)
Example 2: Document Indexing Agent
Implementation Details
Extraction from ChatAgent (Completed)
FileChangeHandler was originally embedded in hub/agents/chat/python/gaia_agent_chat/agent.py as a tightly-coupled inner class. It has been extracted to src/gaia/utils/file_watcher.py as a generic, callback-based implementation. ChatAgent now imports it from there:
Settling Implementation
A created or modified path is held in_pending with a due time
debounce_seconds after its latest event and its (size, mtime_ns) at that
event. A dispatch thread, never the observer’s, wakes at the earliest due time
and stats each due path outside the lock:
- unchanged since its last event: the callback runs;
- changed (still being written): it waits another full window;
- gone: it is dropped, since its delete event reports it.
on_created or on_modified callback that raises PermissionError (the writer
still holds the file exclusively) is retried after 1, 2 and 4 windows; an
on_moved callback that raises it has its destination retried the same way,
as a modification. After the last retry the file is given up on with one
error, logged with its traceback, that tells the user to close the program
using the file and save it again. A callback must let PermissionError
propagate for this to work; catching it disables the retry. A file that
never pauses (a log written constantly) is not reported until it does.
wait_idle() blocks until everything queued has been dispatched, and
close() stops the dispatcher, dropping (and logging) what it had not yet
reported.
Extension Filtering
Dependencies
Required Packages
Import Dependencies
Documentation Updates Required
docs/sdk/core/agent-system.mdx
Add new section after Tool Mixins:Implementation Checklist
Step 1: Create File
- Create
src/gaia/utils/directory - Create
src/gaia/utils/__init__.py - Create
src/gaia/utils/file_watcher.py - Add copyright header
Step 2: Implement Class
- Extend
FileSystemEventHandler - Implement
__init__with callbacks - Implement
on_created() - Implement
on_modified() - Implement
on_deleted() - Implement
_should_process() - Implement settling (dispatch thread, size/mtime check)
Step 3: Add Features
- Extension filtering
- Ignore patterns
- Settling
- Error handling in callbacks
- Logging
Step 4: Write Tests
- Create
tests/sdk/test_file_change_handler.py - Test imports
- Test callbacks
- Test filtering
- Test settling (a file written in pieces is reported once, complete)
- Test with real Observer
- Test error handling
Step 5: Export & Document
- Add to
src/gaia/__init__.py - Add to
__all__list - Update docs/sdk/core/agent-system.mdx
- Add examples to docs/sdk/core/agent-system.mdx
Step 6: Validate
- Can import:
from gaia import FileChangeHandler - Example code works
- All tests pass
FileChangeHandler Technical Specification