Skip to content

Watch only the directories a page's reading reads - #3900

Merged
max-sixty merged 1 commit into
mainfrom
fix/3491-narrow-page-watch
Oct 25, 2026
Merged

max-sixty merged 1 commit into
mainfrom
fix/3491-narrow-page-watch

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

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 under revisions/ no longer takes inotify watches toward max_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.py already declared a page's inputs: page_input (which paths inside the page count), outside_inputs (the claim, the claimant's session file, bound files) and held_page_reading, which stamps them. page_roots(page_dir) now sits beside page_input and names the directories whose notifications report those inputs:

  • the page's parent, non-recursively, which reports the page directory being replaced;
  • the page directory, non-recursively, which reports its direct children;
  • each other direct subdirectory, such as revisions/, runtime/ or media/, non-recursively, since adding or removing an entry moves that directory's stamp;
  • page/ and data/ (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.connect and session.Watch.mark both subscribe page_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.run and one lifetime test's stub) take both from it, where before they called mark() themselves.

_existing_roots now 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 on pages/, which holds every page.

Tests

  • test_a_page_subscription_hears_every_input_its_reading_reads, for both the shared page subscription and a session's Watch, finds the reading's inputs by recording every file_stamp that page_reading takes, 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, to user-views.json and to a staging file must do neither. It also asserts that page/ and data/ are the only recursive roots and that no revision is a root. As a control, dropping the non-recursive subdirectory roots makes it fail, naming instructions/, 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.
  • Behavior changes in existing tests. In two tests a subdirectory is made after connect and then written into; they now connect again first, as every owner does after a wake. test_native_subscription_rearms_a_root_replaced_during_installation now expects connect to replan within the call. Before, it returned a stale subscription that the next connect replaced. In test_native_watch_follows_a_prepared_claim_until_ownership_commits[subscribing], a claim prepared during installation is now subscribed by that same mark, 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, the events --follow tests in tests/model/test_interact_document.py, and the row, neighbour, retirement and orphan tests in tests/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 same FileChanges.

page entries before after
largest (guidance-code-survey-e4e7) 73,714 66.0%, 61.3% 6.1%, 6.0%
p90 (guidance-code-removal-e4e7) 10,495 23.3%, 26.6% 4.9%, 4.4%
median (leaf-organization-review) 1,486 11.3%, 10.1% 6.3%, 6.0%

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) and claims/ (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

This was written by Claude Code on behalf of max-sixty

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]>
@max-sixty
max-sixty merged commit c742734 into main Oct 25, 2026
3 checks passed
@max-sixty
max-sixty deleted the fix/3491-narrow-page-watch branch October 25, 2026 05:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants