Skip to content

fix(realtime): never drop a duplicate event while the path's change is being processed - #507

Merged
HananKavitzAmat merged 1 commit into
chunkhound:mainfrom
fstrasz:fix/dedup-window-upstream
Nov 11, 2026
Merged

HananKavitzAmat merged 1 commit into
chunkhound:mainfrom
fstrasz:fix/dedup-window-upstream

Conversation

@fstrasz

@fstrasz fstrasz commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

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 reaches add_file.
  • The record is cleared only when processing of that path completes (self._recent_file_events.pop(str(file_path), None) after a successful process_file).
  • add_file is the only code that turns a change arriving during processing into work: when the path is in _active_change_generations and no change is pending, it calls _reserve_change_follow_up, and the follow-up is requeued when processing ends.
  • So a write whose event arrives after the first change was dequeued and read, but before that processing finished, is suppressed. Its source generation is advanced (_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/modified event for a path whose change is active (in _active_change_generations, compared by _normalize_mutation_path, the key add_file uses). That event then reaches add_file exactly as a fresh event does, which reserves the follow-up.

Why only the active case: add_file has three outcomes for a created/modified event.

path state add_file does suppressing the duplicate
pending in the debounce refreshes the debounce horizon loses nothing: the queued work reads the latest content
change mutation already pending coalesces loses nothing, same reason
active, no change pending reserves one follow-up loses the write

Routing every duplicate into add_file was considered and rejected: each one refreshes _pending_debounce, and _debounced_add_file has no maximum wait, so a file written continuously would not be indexed until the writes stopped. It would also change the meaning of suppressed_duplicate_count / coalesced_updates and 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_up keeps one generation per path.

This change does not depend on any other open PR. It applies to main on 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 of main carrying 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.

02:58:58.474805 RT: event modified: ...\src\e2eDedupProbe.ts
02:58:58.983882 RT: processing debounced file: ...\src\e2eDedupProbe.ts
02:58:59.970762 RT: suppressed duplicate modified: ...\src\e2eDedupProbe.ts
02:59:10.714730 RT: processed ...\src\e2eDedupProbe.ts operation=change

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 real event_queue, _consume_events and _process_loop, as the watcher's do. The first change's process_file is 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.

  • On main: fails. The second processing never happens.
  • With this change: passes.
  • With the new condition removed again: fails.

Run on Windows, Python 3.13, serially (-n 0): tests/realtime, tests/test_realtime_functional.py, tests/test_realtime_failures.py.

passed skipped
main 122 34
this branch 123 34

The 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

…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]>
@HananKavitzAmat

Copy link
Copy Markdown
Collaborator

looks good

@HananKavitzAmat
HananKavitzAmat added this pull request to the merge queue Nov 11, 2026
Merged via the queue into chunkhound:main with commit 545e255 Nov 11, 2026
25 checks passed
@HananKavitzAmat
HananKavitzAmat self-requested a review November 11, 2026 09:17
@HananKavitzAmat HananKavitzAmat added the bug Something isn't working label Nov 11, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants