Repository navigation
fix(utils): report a watched file once it has finished being written - #5385
Conversation
FileChangeHandler ran its callback on the watchdog observer thread at the first event for a path, then ignored that path's events for debounce_seconds. A file being copied into a watched folder was reindexed half-written, its final content never was, and a slow reindex held up delivery of every other event. Callbacks now run on the handler's own dispatch thread. A created or modified file is reported once it has gone debounce_seconds with no events and no change in size or mtime, so a burst of saves becomes one callback; deletes and moves are reported in order and cancel a pending report. A callback that raises is logged with its traceback. wait_idle() and close() let callers and tests drain or stop the dispatcher; FileWatcher.stop() closes it.
…se the chat agent's handlers Review follow-ups: os.stat no longer runs under the dispatcher lock, so a slow share cannot stall the observer; a still-changing file is rechecked no sooner than every 50 ms, so a zero window does not spin; the chat agent keeps its handlers and stop_watching closes them, so reindexing stops when watching does. Docs note that a file that never pauses is not reported.
|
Verdict: Approve with suggestions This fixes a real bug: a file copied into a watched folder used to be reindexed while it was still half-written, and later changes inside two seconds were dropped, so search could answer from a cut-off copy. The watcher now waits until the file stops changing, then reindexes on its own thread so one slow reindex no longer holds up other changes. The threading looks correct: I traced how deletes, re-creates and late events interleave and found no ordering bug. Nothing blocks merge. Two small things worth doing:
The Agent UI check in the test plan (copy a large PDF in, ask about its last page) isn't ticked yet. Most users are on Windows, so it's worth running there before release: Windows may not update a file's size and timestamp while it is being copied the way Linux does. Real-world evidenceN/A: no evidence bundle for this run. There's no CLI or API surface to exercise here, and the Agent UI end-to-end check is still open in the PR's test plan. This verdict rests on reading the code. I also couldn't run the tests, because pytest isn't installed on this runner. The author reports 161 passing. 🔍 Technical detailsIssues found 🟢 A failed This makes other 🟢 Callbacks keep running after 🟢 Dependency timing (no action if this lands before 0.25.0 final). Strengths
|
…on close A file the watcher could not stat for a reason other than being gone (for example a permission error) was dropped with only a debug log, so it was never reindexed and nobody was told. It now logs a warning naming the file. Closing the watcher also stops the rest of a batch the dispatcher had already taken, so reindexing no longer continues after stop_watching returns.
…patch alive Review follow-ups. - close() did not stop the batch already being dispatched: every queued callback still ran, and close() blocked until they had. Dispatch now checks for close before each callback, so close() waits only for the running one. - A callback that hit PermissionError because the writer still held the file exclusively (Windows) was never retried until another event arrived. It is now retried after 1, 2 and 4 windows, then given up on with an error. - An exception in dispatch itself, outside a callback, ended the thread with a traceback on stderr. It is now logged and dispatch continues. - The headline test waits by polling rather than wait_idle(), so on main it fails on the half-written report itself. - docs/spec/file-change-handler.mdx described the old leading-edge debounce in its interface sketch, test example and implementation notes; every description now matches the settling behaviour.
The chat agent pins amd-gaia>=0.25.0, and a core without close() reindexes inline, so stop_watching now closes only handlers that have it. An agent that never ran __init__ no longer logs a missing file_handlers error at GC.
The core unit lane has no gaia_agent_chat, so the file skipped there.
When a file was copied into a folder the chat agent watches, it was reindexed at the first file event, often while still half-written. Later changes within two seconds were ignored, so the finished file was never indexed and search answered from a truncated copy. The reindex also ran on the file watcher's own thread, so one slow reindex held up every other change. Now a file is reindexed once it has stopped changing (no events, and no change in size or modification time, for the debounce window). That happens on a separate thread, and a burst of saves gives one reindex.
Reproduced by
tests/unit/test_file_watcher_settle.py; it fails on main and passes here.🔍 Technical details
FileChangeHandlernow has a lazily started dispatch thread:_pendinguntil settled;_readyqueue and cancel a pending report for the path;debounce_secondsnow means "unchanged for this long", not "ignore repeats for this long". The leading-edge_is_debouncedcache is gone.wait_idle(timeout),close()andreopen().FileWatcher.stop()closes the dispatcher andstart()reopens it.PermissionErrorfrom a writer still holding the file exclusively is retried after 1, 2 and 4 windows, then given up on with an error. An exception in dispatch itself is logged and dispatch continues.close()stops the batch being dispatched before its next callback, and waits only for the one already running. A file that can't be checked for any reason other than being gone is logged as a warning.wait_idle, since dispatch is asynchronous. The headline "written in pieces" test polls instead, so on main it fails on the half-written report itself.stop_watchingcloses them; its CHANGELOG names the change.docs/sdk/utils/file-watcher.mdxanddocs/spec/file-change-handler.mdx(feature list, interface sketch, test example and implementation notes).Test plan
python -m pytest tests/unit/test_file_watcher.py tests/unit/test_file_watcher_settle.py tests/unit/test_skills_manager.py tests/test_chat_agent.py tests/unit/test_chat_fix_contracts.py: 166 passed, three runs in a rowpython util/lint.py --black --isort --flake8 --pylint --test-lanes