Skip to content

Let a tab activate a save while its stamp checks it - #3878

Merged
leaf-agent merged 3 commits into
mainfrom
grind/g-stamp-lock
Oct 27, 2026
Merged

leaf-agent merged 3 commits into
mainfrom
grind/g-stamp-lock

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 25, 2026 •

Copy link
Copy Markdown
Owner

leaf page stamp checked the source while it held the page transaction, so a
tab's state read that arrived after the stamp locked the page waited for the
whole check (about 450 ms on the corpus at low load) before checking the same
source again itself. A stamp in place now checks before taking the transaction
and holds it only to confirm and publish. Under the transaction it compares the
page's source and history readings (source_readings, the stamps activation
already keys on) with those it took before checking; a write that lands during
the check moves one, and the stamp checks again. In the common case the tab's
read activates the save during the stamp's first check, so the second check also
runs outside the transaction and the stamp names the revision the read wrote.
After UNLOCKED_CHECKS (2) moved checks, the last runs under the transaction, so
a page written faster than the stamp checks still gets its stamp.
--replace keeps its path: it writes the source under the transaction, so it
checks there too. Either way the stamp holds its publication as the next
activation's answer (hold_published, from #3942) keyed on the source reading
it took before the check that published.

Behavior change in tests: test_stamp_keeps_its_checked_log_snapshot_until_the_note
asserted that a browser write waits behind the stamp's check. It now asserts the
reverse: a user's action and the tab's activation both land during the check,
and the stamp then names the revision the tab activated (it fails on the old
code with "the tab waited on the stamp's check", and with the re-check removed
with "revision r2 already exists"). A second test covers the transaction-held
last check. Both move to tests/model/, since neither runs a server.

Evidence, on an HTTP stand-in for the tab (save, start leaf page stamp, poll
/api/state every 250 ms until it names the new revision, fetch that
revision's document), base f00911ed1 against this change, eight alternating
runs at load 17–33 on 18 cores:

  • First poll fixed 500 ms after the save, where the base's stamp holds the
    lock: state answer median 2.25 s -> 1.19 s, document's last byte
    2.98 s -> 1.93 s.
  • First poll at a random phase (10 runs): both arms took the save route,
    0.75 s and 0.77 s to the state answer.

uv run leaf-dev bench-latency --page corpus --transition revision at load
20–46 could not tell the arms apart: medians 2742 vs 2855 and 2857 vs 3054 ms
(base vs head, five runs each) with spreads over 2 s, dominated by load. An
earlier version that re-checked under the transaction read 3291 vs 5480 ms; in
its runs the tab's later state reads waited on that re-check, so the second
check now runs outside the transaction too (attributing the whole gap to it is
a guess at that load).
The note's 1.11–1.22 s stamp-route reading was taken at load 4–7, which this
change has not been measured at. A stamp that follows the tab's activation still
repeats that check outside the lock, about 450 ms of CPU beside the tab's
document fetch and patch; on a loaded machine that work competes with the paint.
notes/user-feedback-responsiveness.md records the result and keeps the record
of a passed check as the remaining proposal.

Ran: tests/model/test_interact_document.py, test_interact_product.py,
test_interact_anchors.py, test_todos.py, test_revision_delivery.py,
interact_contract/test_revendoring.py and test_data_contracts.py in full,
and tests/process/test_interact_server.py -k "stamp or revision or activat or publish or sample".

Not verified: the browser benchmark at load 4–7, where the note's readings were
taken; the journeys and browser tiers.

Call Max may reverse: the record that a source passed its check (the note's
other proposal) is left unbuilt. It would need an identity for Leaf's Python
checker, which doesn't exist yet, and a new page-directory record; an earlier
Leaf would read that record as one more source file and check as before, so it
needs no protocol bump, but it is a new mechanism whose remaining benefit
(--replace stamps, and the second check a stamp repeats after the
tab's activation) should be measured first.

Co-Authored-By: Claude Opus 5.5 (1M context) [email protected]

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

max-sixty and others added 3 commits October 24, 2026 21:08
`leaf page stamp` checked the source while it held the page transaction, so a
tab's state read that arrived after the stamp locked the page waited for the
whole check (about 450 ms on the corpus at low load) before checking the same
source again itself. A stamp in place now checks before taking the transaction
and holds it only to confirm and publish. Under the transaction it compares the
page's source and history readings (`source_readings`, the stamps activation
already keys on) with those it took before checking; a write that lands during
the check moves one, and the stamp checks again. In the common case the tab's
read activates the save during the stamp's first check, so the second check also
runs outside the transaction and the stamp names the revision the read wrote.
After `UNLOCKED_CHECKS` (2) moved checks, the last runs under the transaction, so
a page written faster than the stamp checks still gets its stamp.
`--from-directory` keeps its path: it writes the source under the transaction,
so it checks there too.

Behavior change in tests: `test_stamp_keeps_its_checked_log_snapshot_until_the_note`
asserted that a browser write waits behind the stamp's check. It now asserts the
reverse: a user's action and the tab's activation both land during the check,
and the stamp then names the revision the tab activated (it fails on the old
code with "the tab waited on the stamp's check", and with the re-check removed
with "revision r2 already exists"). A second test covers the transaction-held
last check. Both move to `tests/model/`, since neither runs a server.

Evidence, on an HTTP stand-in for the tab (save, start `leaf page stamp`, poll
`/api/state` every 250 ms until it names the new revision, fetch that
revision's document), base `f00911ed1` against this change, eight alternating
runs at load 17–33 on 18 cores:

- First poll fixed 500 ms after the save, where the base's stamp holds the
  lock: state answer median 2.25 s -> 1.19 s, document's last byte
  2.98 s -> 1.93 s.
- First poll at a random phase (10 runs): both arms took the save route,
  0.75 s and 0.77 s to the state answer.

`uv run leaf-dev bench-latency --page corpus --transition revision` at load
20–46 could not tell the arms apart: medians 2742 vs 2855 and 2857 vs 3054 ms
(base vs head, five runs each) with spreads over 2 s, dominated by load. An
earlier version that re-checked under the transaction read 3291 vs 5480 ms; its
re-check held up the tab's later reads, which is why the second check now runs
outside it.
The note's 1.11–1.22 s stamp-route reading was taken at load 4–7, which this
change has not been measured at. A stamp that follows the tab's activation still
repeats that check outside the lock, about 450 ms of CPU beside the tab's
document fetch and patch; on a loaded machine that work competes with the paint.
`notes/user-feedback-responsiveness.md` records the result and keeps the record
of a passed check as the remaining proposal.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Main's #3942 holds a stamp's publication as the next activation's answer,
keyed on the source reading taken before the check, and #3850 renamed
--from-directory/--if-source to --replace. The stamp in place already takes
those readings before its unlocked check, so _stamp_locked now receives them
and holds the publication; the replace path takes them before its locked
check. The note names --replace.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@max-sixty
max-sixty marked this pull request as ready for review October 27, 2026 05:17
@leaf-agent
leaf-agent merged commit 69a470e into main Oct 27, 2026
6 checks passed
@leaf-agent
leaf-agent deleted the grind/g-stamp-lock branch October 27, 2026 05:36
leaf-agent added a commit that referenced this pull request Oct 27, 2026
A `leaf page stamp` could validate an outdated log while treating its
fingerprint as current. If an agent message landed after the log read
but before the fingerprint, the stamp could publish authored markup
using an ID that the message had just claimed. This fixes the read order
so the existing transaction check detects that append and validates
again before publishing.

The unlocked check now takes its source/history fingerprint before
reading events. Checks already holding the transaction continue using
its event list. This preserves #3878’s behavior: a tab can activate a
save while the stamp checks it, and publication alone holds the
transaction.

Validation: the new regression deterministically admits a message in the
read/fingerprint gap; it fails before the fix because stamping succeeds,
and passes afterward because the ID collision is refused without a
second version note. All 10 tests selected by
`tests/model/test_interact_document.py -k "stamp or publish"` pass. No
browser behavior changes.

Co-authored-by: leaf-agent <[email protected]>
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