Repository navigation
Watch only the directories a page's reading reads - #3900
Merged
Merged
Conversation
A page subscription watched the whole page tree recursively, though its reading stamps only the page's direct children, the files in page/ and data/, and the entries of each other direct subdirectory. reading.page_roots now names those directories beside page_input, and both page_changes and session.Watch subscribe them, so the contents of each revision and the vendored layer's modules go unwatched. The roots follow the page's subdirectories, so each owner takes a generation and then checks that a fresh plan still matches the installed subscription; page_changes now returns the subscription and that generation. A missing root collapses onto its ancestor without recursion. Refs #3491 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent
approved these changes
Oct 25, 2026
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.
A page's subscription now watches only the directories its reading reads, rather than the whole page tree. Under the polling fallback from #3848, the largest page on this Mac (73,714 entries, 73,248 of them under
revisions/) costs 6% of a core instead of 61–66%. With a native watcher, the history underrevisions/no longer takes inotify watches towardmax_user_watches. That matters because notify 8.0.0 drops a watch it can't add while running (add_watch(...).ok()), so a recursive watch that reached the limit stopped seeing new subdirectories and raised no error.One statement of what a reading reads
served_state/reading.pyalready declared a page's inputs:page_input(which paths inside the page count),outside_inputs(the claim, the claimant's session file, bound files) andheld_page_reading, which stamps them.page_roots(page_dir)now sits besidepage_inputand names the directories whose notifications report those inputs:revisions/,runtime/ormedia/, non-recursively, since adding or removing an entry moves that directory's stamp;page/anddata/(TREES), recursively, since the reading stamps every file in them.What each revision holds and the vendored layer's modules are not inputs, so nothing watches them.
_PageChanges.connectandsession.Watch.markboth subscribepage_roots. Each of them used to spell{page.parent: False, page: True}itself; now neither keeps a list of its own.Roots that follow the page's subdirectories
Recursion over the whole page meant a new subdirectory was always covered. Now each direct subdirectory is a root of its own, so the set of roots can change. A subdirectory created after the roots were listed but before the owner took its generation would be reported once, absorbed into that generation, and never subscribed. Both owners close that gap the same way. They install the subscription, take the generation, plan the roots again, and install again if the new plan differs. A subdirectory created before the generation is then subscribed, and one created after it wakes the owner, who connects again. For this,
page_changes(page)now returns the subscription together with the generation it was current at. Its four callers (follow_events,stop_when_service_ends,RowPublisher.runand one lifetime test's stub) take both from it, where before they calledmark()themselves._existing_rootsnow subscribes a missing directory's nearest ancestor without recursion. The ancestor reports the directory's creation, and every owner subscribes again on that wake. Recursing from the ancestor only watched whatever else lay beneath it: a page directory deleted mid-run would put a recursive watch onpages/, which holds every page.Tests
test_a_page_subscription_hears_every_input_its_reading_reads, for both the shared page subscription and a session'sWatch, finds the reading's inputs by recording everyfile_stampthatpage_readingtakes, not from a list. It then writes each input in turn and asserts that the write both wakes the subscription and moves the reading. Writes inside a revision, touser-views.jsonand to a staging file must do neither. It also asserts thatpage/anddata/are the only recursive roots and that no revision is a root. As a control, dropping the non-recursive subdirectory roots makes it fail, naminginstructions/,widgets/and five other directories whose entries then wake nothing.test_a_page_subscription_takes_in_a_directory_made_while_it_installs, for both owners, creates a subdirectory while the native watcher is being installed and asserts that the subscription handed back includes it. Removing the plan check after the generation makes it fail for each owner.connectand then written into; they now connect again first, as every owner does after a wake.test_native_subscription_rearms_a_root_replaced_during_installationnow expectsconnectto replan within the call. Before, it returned a stale subscription that the nextconnectreplaced. Intest_native_watch_follows_a_prepared_claim_until_ownership_commits[subscribing], a claim prepared during installation is now subscribed by that samemark, so the test asserts the prepared target is watched instead of waiting for a wake.Ran
tests/model/test_native_watch.py,tests/model/test_server_lifetime.py,tests/model/test_wait.py,tests/model/test_hooks.py, theevents --followtests intests/model/test_interact_document.py, and the row, neighbour, retirement and orphan tests intests/process/test_interact_server.py. All pass.Polling cost, measured
This is the CPU time of a process holding one page's shared subscription for 30 s with
WATCHFILES_FORCE_POLLING=1, as #3848 measured it. The pages are the largest, p90 and median by entry count of the 289 pages under~/.local/state/leaf/pages/. Before and after ran back to back, twice each, on a host with a load average of about 17–25. "Before" is main's roots ({parent: False, page: True}) through the sameFileChanges.guidance-code-survey-e4e7)guidance-code-removal-e4e7)leaf-organization-review)The largest page's roots now hold about 1,000 entries, not 73,714. What's left is roughly the same on every page. An empty directory with the same claim subscription costs 2.1%, and each page subscription also polls the shared
pages/(291 entries) andclaims/(635 entries) directories, non-recursively, to see its own page directory and claim file replaced. That floor grows with the number of pages on the machine, not with the page's own history.Refs #3491