Repository navigation
fix(realtime): never drop a duplicate event while the path's change is being processed - #507
Merged
HananKavitzAmat merged 1 commit intoNov 11, 2026
Conversation
…s being processed _consume_events suppressed a same-type event for a path within the 2 s dedup window, and the window's record is cleared only when that path's processing completes. A write whose event arrived after the change was dequeued and read, but before it finished, was dropped before add_file, the only place that reserves a follow-up for an active change, so the newer content stayed unindexed until the file changed again. A duplicate is still suppressed while the change is pending (the queued work reads the latest content); only a duplicate for an active change now reaches add_file. Debounce timing, status counters and the Watchman dedup behaviour are unchanged for every other case. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collaborator
|
looks good |
HananKavitzAmat
self-requested a review
November 11, 2026 09:17
This was referenced Nov 11, 2026
Merged
fstrasz
added a commit
to fstrasz/chunkhound
that referenced
this pull request
Nov 16, 2026
chunkhound#504) * fix(realtime): read-only watcher events never take an event-queue slot On Linux, watchdog's inotify emitter was scheduled with no event_filter, so it watched IN_OPEN / IN_CLOSE_NOWRITE / IN_CLOSE_WRITE and every plain read of an indexable file produced `opened` + `closed_no_write`. The handler admitted any event type on an indexable path, so each read took one of the 1,000 event-queue slots before the consumer, which acts only on created / modified / deleted / dir_*, dropped it. ChunkHound's own start-up scan reads every file, so it could overflow the queue and request an event_queue_overflow resync; a type-check or test run over the tree does the same. Fix, at the source: - events.py: CONTENT_EVENT_TYPES is the single list of event types that can mean a content change; on_any_event drops every other type first, with no callback. watchdog_event_filter() derives the watchdog classes from it. - startup.py (_bootstrap_fs_monitor) and pipeline.py (_add_directory_watch) pass that filter to observer.schedule. On inotify it narrows the kernel watch mask, so reads are never reported at all. Both call sites use the same filter because watchdog 7 shares one inotify fd per observer and each inotify_add_watch replaces the inode's mask. `attrib` is kept. On Windows, watchdog 7.1 reports an in-place append as `attrib` when the change time is ahead of the write time (3 of 3 appends in a probe), so for some writes it is the only event. The consumer does not act on `attrib` today; that, and Windows last-access updates arriving as `attrib`, are left for a separate change. Windows behaviour is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> * test(realtime): a write after an indexing read is indexed with reads filtered With read-only events dropped, two writes reach the consumer as modified, modified, with no open or close between them. The test drives the real handler with the Linux sequence (write, the indexing run's read, a second write inside the duplicate-event window) through the real consumer and process loop, and checks the second write is processed. It fails without the consumer's rule that a path being processed never has a duplicate dropped (chunkhound#507). Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(realtime): a suppressed duplicate write still refreshes a pending debounce With read-only events dropped, every save after the first reaches the consumer as a duplicate `modified` and is suppressed before add_file, which is what pushed the pending debounce back. On Linux a burst of saves was then indexed part-way through and again afterwards (10 saves 0.3 s apart: 5 passes instead of 1, each queueing an embed pass). The suppression branch now refreshes the debounce horizon for a write whose change is waiting in debounce. Tests: a burst of saves closer than the debounce delay is indexed once, with the final content; the write-after-read test now uses fake services, a long duplicate window so it always exercises the rule, and gathers its tasks on the way out. The events.py comment no longer says dropping `closed` changes nothing indexed: that holds only with the consumer's two rules. Co-Authored-By: Claude Opus 5.5 <[email protected]> * refactor(realtime): resolve the debounce key only for a suppressed write Review follow-up to the debounce refresh. The duplicate check is back to main's exact condition, so a repeated delete or directory event no longer resolves its path; only a suppressed write looks up its debounce key. The refresh applies on every backend that goes through the consumer (watchdog on any OS, Watchman): a burst of saves is indexed once, and a file written without pause is indexed once it goes quiet, which is what the debounce documents. The burst test now waits for the indexing pass instead of a fixed sleep, so a stalled runner cannot end it before the pass runs. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
fstrasz
added a commit
to fstrasz/chunkhound
that referenced
this pull request
Nov 18, 2026
…s raise no event (chunkhound#505) * fix(realtime): read-only watcher events never take an event-queue slot On Linux, watchdog's inotify emitter was scheduled with no event_filter, so it watched IN_OPEN / IN_CLOSE_NOWRITE / IN_CLOSE_WRITE and every plain read of an indexable file produced `opened` + `closed_no_write`. The handler admitted any event type on an indexable path, so each read took one of the 1,000 event-queue slots before the consumer, which acts only on created / modified / deleted / dir_*, dropped it. ChunkHound's own start-up scan reads every file, so it could overflow the queue and request an event_queue_overflow resync; a type-check or test run over the tree does the same. Fix, at the source: - events.py: CONTENT_EVENT_TYPES is the single list of event types that can mean a content change; on_any_event drops every other type first, with no callback. watchdog_event_filter() derives the watchdog classes from it. - startup.py (_bootstrap_fs_monitor) and pipeline.py (_add_directory_watch) pass that filter to observer.schedule. On inotify it narrows the kernel watch mask, so reads are never reported at all. Both call sites use the same filter because watchdog 7 shares one inotify fd per observer and each inotify_add_watch replaces the inode's mask. `attrib` is kept. On Windows, watchdog 7.1 reports an in-place append as `attrib` when the change time is ahead of the write time (3 of 3 appends in a probe), so for some writes it is the only event. The consumer does not act on `attrib` today; that, and Windows last-access updates arriving as `attrib`, are left for a separate change. Windows behaviour is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(realtime): index Windows in-place writes reported as attrib; reads raise no event On Windows, watchdog 7 classifies a FILE_ACTION_MODIFIED as `attrib` whenever the record's LastChangeTime is ahead of its LastModificationTime. An in-place append usually is: measured on NTFS, 26 of 30 appends arrived as `attrib` only and 4 as `modified` only. The consumer acts on created/modified/deleted, so most in-place writes were never indexed by the watcher. watchdog also subscribes to FILE_NOTIFY_CHANGE_LAST_ACCESS, so with last-access updates on, every read of a file not read for an hour raises an event (7: `attrib`, 6: `modified`) that takes an event-queue slot; ChunkHound's own start-up scan reads every file. - events.py: WATCHDOG_TO_CONSUMER_EVENT replaces CONTENT_EVENT_TYPES as the one place a watchdog type is admitted, mapping attrib to modified. On Linux, watchdog 6 already reports IN_ATTRIB as modified, so this is that behaviour on watchdog 7 too. - events.py: ignore_windows_last_access() clears the last-access bit from watchdog's module-global notify flags, which both 6.0 and 7.1 read on every ReadDirectoryChangesW call; called where the observer is created. Co-Authored-By: Claude Opus 5.5 <[email protected]> * test(realtime): a write after an indexing read is indexed with reads filtered With read-only events dropped, two writes reach the consumer as modified, modified, with no open or close between them. The test drives the real handler with the Linux sequence (write, the indexing run's read, a second write inside the duplicate-event window) through the real consumer and process loop, and checks the second write is processed. It fails without the consumer's rule that a path being processed never has a duplicate dropped (chunkhound#507). Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(realtime): a suppressed duplicate write still refreshes a pending debounce With read-only events dropped, every save after the first reaches the consumer as a duplicate `modified` and is suppressed before add_file, which is what pushed the pending debounce back. On Linux a burst of saves was then indexed part-way through and again afterwards (10 saves 0.3 s apart: 5 passes instead of 1, each queueing an embed pass). The suppression branch now refreshes the debounce horizon for a write whose change is waiting in debounce. Tests: a burst of saves closer than the debounce delay is indexed once, with the final content; the write-after-read test now uses fake services, a long duplicate window so it always exercises the rule, and gathers its tasks on the way out. The events.py comment no longer says dropping `closed` changes nothing indexed: that holds only with the consumer's two rules. Co-Authored-By: Claude Opus 5.5 <[email protected]> * refactor(realtime): resolve the debounce key only for a suppressed write Review follow-up to the debounce refresh. The duplicate check is back to main's exact condition, so a repeated delete or directory event no longer resolves its path; only a suppressed write looks up its debounce key. The refresh applies on every backend that goes through the consumer (watchdog on any OS, Watchman): a burst of saves is indexed once, and a file written without pause is indexed once it goes quiet, which is what the debounce documents. The burst test now waits for the indexing pass instead of a fixed sleep, so a stalled runner cannot end it before the pass runs. Co-Authored-By: Claude Opus 5.5 <[email protected]> * test(realtime): fit the Windows attrib test to main's watchdog observer factory Main's _create_watchdog_observer (chunkhound#509) now takes the inotify overflow callback and returns (observer, detection). The test's assertions are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A write to a file while that file's previous change is being processed can be dropped and never indexed.
_consume_events(chunkhound/services/realtime/pipeline.py, "Layer 3") suppresses an event whose type and path match the last recorded event for that path within_EVENT_DEDUP_WINDOW_SECONDS(2.0 s). A suppressed event never reachesadd_file.self._recent_file_events.pop(str(file_path), None)after a successfulprocess_file).add_fileis the only code that turns a change arriving during processing into work: when the path is in_active_change_generationsand no change is pending, it calls_reserve_change_follow_up, and the follow-up is requeued when processing ends._record_accepted_event), but nothing reserves a follow-up, and nothing compares generations at completion. The index keeps the earlier content until the file changes again.The exposure is the first change's processing time, not the whole 2 s window: the completion pop re-opens the gate.
Fix
A same-type duplicate inside the window is still suppressed, unless it is a
created/modifiedevent for a path whose change is active (in_active_change_generations, compared by_normalize_mutation_path, the keyadd_fileuses). That event then reachesadd_fileexactly as a fresh event does, which reserves the follow-up.Why only the active case:
add_filehas three outcomes for a created/modified event.add_filedoesRouting every duplicate into
add_filewas considered and rejected: each one refreshes_pending_debounce, and_debounced_add_filehas no maximum wait, so a file written continuously would not be indexed until the writes stopped. It would also change the meaning ofsuppressed_duplicate_count/coalesced_updatesand the Watchman multi-subscription dedup. The narrow rule changes behaviour only for a duplicate that arrives during processing.Cost: such a duplicate now costs one more
process_file(re-read, re-parse, a write transaction that diffs to no change when the content is unchanged, and an embed pass with nothing to embed). It happens at most once per processing run, because_reserve_change_follow_upkeeps one generation per path.This change does not depend on any other open PR. It applies to
mainon its own.Evidence
Not reproduced end to end. I tried, with a real
chunkhound mcp --no-daemon --no-embeddings(watchdog backend, observer alive) on a 1,436-file TypeScript repository on Windows 11. The probe: append token A to a file, wait a gap, append token B, wait for idle, regex-search both. 19 trials, with probe files of one line, 117 KB and 279 KB (the last also with--file-timeout-min-size-kb 100000, so it was parsed in the pool), and gaps from 0.6 to 1.8 s. B was indexed every time. That build was a fork ofmaincarrying other realtime fixes, not this one.The debug trace (
CHUNKHOUND_DEBUG=1) does show the mechanism live: B's event suppressed while A's change was being processed.B survived in every trial because the parse read the file after B's write: each run's chunk count rose by two, so A and B were parsed together, and the one follow-up run diffed to
chunks=0. The write is lost only when B lands after the parse has read the file, and its event arrives before processing ends and within 2 s of A's event. That ordering is what the test below drives deterministically.Test
tests/test_realtime_functional.py::test_write_during_processing_inside_dedup_window_is_indexed. Both events go through the realevent_queue,_consume_eventsand_process_loop, as the watcher's do. The first change'sprocess_fileis held open after it read the file; the file is rewritten and its event queued; the first is released; the test asserts the second content is processed.main: fails. The second processing never happens.Run on Windows, Python 3.13, serially (
-n 0):tests/realtime,tests/test_realtime_functional.py,tests/test_realtime_failures.py.mainThe 34 skips are mostly native-Watchman tests, which need the packaged Watchman runtime. They include
test_watchman_multi_scope_translation_deduplicates_across_subscriptions. By reading, it is unaffected: its duplicate arrives while the path is pending, not active. But it did not run here.🤖 Generated with Claude Code