Skip to content

Make a preview slot its page directory, and watch before the URL goes out - #816

Merged
max-sixty merged 3 commits into
mainfrom
preview-locks-in-state-home
Sep 18, 2026
Merged

max-sixty merged 3 commits into
mainfrom
preview-locks-in-state-home

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

A slot was a page directory plus three files beside it: the watcher's identity and stop flag in <slot>.preview.json, the lease --stop waits on, and a second lock serializing writes to that flag. The two locks sat next to what they guard, which is why a discard could not remove them — a --reset that unlinked the lease would hand a waiting --stop an inode excluding nobody, and that stop would then take down the server the reset had just started. leaf.leases already keeps a stable per-path lock in the state home for exactly this reason, and the page's own transition lease is one, so both of a slot's locks are page locks now.

The stop flag is gone with them. --stop holds its request open for as long as it waits, so the watcher reads it between passes and wherever it would bring the server back up, an update in flight finishes, and its pending restart is suppressed — the guarantee the flag was for, minus the file, the lock that protected it, and the case where a watcher starting after the flag was written overwrote it and left the stop waiting forever. With one writer left, the watcher's own, the identity moves into the page's preview.json beside the fields the browser chrome shows: the slot's whole record in one file the discard already removes, and .tmp/previews is empty after one. preview_metadata projects the named safe fields, so the checkout and fixture paths in that record reach no browser, which is what page-storage.md already said the server does.

PreviewService owns the one difference between a reader preview's durable claimed service and an automation preview's process-owned temporary server. The watch loop branched on it seven times and now states what it wants — stop, refresh, start — without knowing which kind it has.

lock_is_held asks with a shared lock. An exclusive probe is refused by another probe as readily as by a lease, so two processes reading at once told each other a lease was held.

One bug found while testing this, and older than it: watchfiles starts its rust watcher on the subscription's first read, and the read the preview's loop makes comes after it has announced its URL. The edit a developer makes the moment that URL appears landed in a window nothing was watching — measured on the parent commit, six of six such edits were lost, and none of six are now. That is also what made test_a_detached_preview_restarts_under_its_original_codex_claim and test_stopping_a_preview_waits_for_its_active_recompose fail under load: both edit an input the instant the preview reports ready. watch_changes reads once before it returns and hands that batch on as its caller's first.

Three test assertions moved with the state they read: the lease and the stop request are read as locks, the fixture requires the directory it swept to be clean, and the released detached watcher is asked for the record that lets a later start resume rather than for a flag saying it stopped.

Verified

  • uv run pytest tests --run-nightly — 2728 passed, 4 failed. All four — test_render_drafts.py::test_a_single_space_is_message_content_in_every_composer (reply, general, composer) and test_render_options_addition.py::test_the_add_field_previews_the_option_it_will_make — fail identically on the merge base 9e5e0d3 with no branch in play: they assert a press face or preview disc differs from its transparent button, and Take the solid accent fill off every pressable #803 made both transparent.
  • uv run pytest tests — 1145 passed. uv run pre-commit run --all-files clean.
  • The startup window, measured with a probe that starts a background preview and writes the source the instant the command returns: 0 of 6 edits reached the page on d264163, 6 of 6 here. test_a_watch_subscription_collects_before_its_first_read is the bug-back — red on the merge base, green here.
  • Five rounds of the two load-sensitive preview tests against five on the merge base, launched together: 5 green here, 4 green and 1 red there.
  • By hand: start, repeat start (same URL, joins the running watcher), --stop, --reset --background, discard — after which .tmp/previews holds nothing.

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

max-sixty and others added 3 commits September 18, 2026 15:16
… out

A slot was a page directory plus three files beside it: the watcher's identity
and stop flag in `<slot>.preview.json`, the lease `--stop` waits on, and a
second lock serializing writes to that flag. The two locks sat next to what they
guard, which is why a discard could not remove them — a `--reset` that unlinked
the lease would hand a waiting `--stop` an inode excluding nobody, and that stop
would then take down the server the reset had just started. `leaf.leases` already
keeps a stable per-path lock in the state home for exactly this reason, and the
page's own transition lease is one, so both of a slot's locks are page locks now.

The stop flag is gone with them. `--stop` holds its request open for as long as
it waits, so the watcher reads it between passes and wherever it would bring the
server back up, an update in flight finishes, and its pending restart is
suppressed — the guarantee the flag was for, minus the file, the lock that
protected it, and the case where a watcher starting after the flag was written
overwrote it and left the stop waiting forever. With one writer left, the
watcher's own, the identity moves into the page's `preview.json` beside the
fields the browser chrome shows: the slot's whole record in one file the discard
already removes, and `.tmp/previews` is empty after one. `preview_metadata`
projects the named safe fields, so the checkout and fixture paths in that record
reach no browser, which is what `page-storage.md` already said the server does.

`PreviewService` owns the one difference between a reader preview's durable
claimed service and an automation preview's process-owned temporary server. The
watch loop branched on it seven times and now states what it wants — stop,
refresh, start — without knowing which kind it has.

`lock_is_held` asks with a shared lock. An exclusive probe is refused by another
probe as readily as by a lease, so two processes reading at once told each other
a lease was held.

One bug found while testing this, and older than it: `watchfiles` starts its rust
watcher on the subscription's first read, and the read the preview's loop makes
comes after it has announced its URL. The edit a developer makes the moment that
URL appears landed in a window nothing was watching — measured on the parent
commit, six of six such edits were lost, and none of six are now. That is also
what made `test_a_detached_preview_restarts_under_its_original_codex_claim` and
`test_stopping_a_preview_waits_for_its_active_recompose` fail under load: both
edit an input the instant the preview reports ready. `watch_changes` reads once
before it returns and hands that batch on as its caller's first.

Three test assertions moved with the state they read: the lease and the stop
request are read as locks, the fixture requires the directory it swept to be
clean, and the released detached watcher is asked for the record that lets a
later start resume rather than for a flag saying it stopped.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit 26a3b6f into main Sep 18, 2026
5 checks passed
@max-sixty
max-sixty deleted the preview-locks-in-state-home branch September 18, 2026 23:45
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