Skip to content

fix(content-parse,collab): converter small-write priority and full event-loop yields (wiki-v2 issue 731) - #82

Merged
PMQ9 merged 5 commits into
wiki-v2from
fix/731-converter-priority
Nov 8, 2026
Merged

PMQ9 merged 5 commits into
wiki-v2from
fix/731-converter-priority

Conversation

@PMQ9

@PMQ9 PMQ9 commented Nov 8, 2026 •

Copy link
Copy Markdown

TL;DR

The second half of the fork work for cccvu/wiki-v2 issue 731 (the first half was PR 79):

  • Converter priority: a small write now waits out at most one large conversion.
  • Every page-write yield is now a full event-loop turn, including the I/O poll.
  • The load skips a content read it does not use.
  • The store yields between its page-sized steps.
  • A cold live digest is warmed before the resident pre-check.

It is paired with the super PR, which carries the gitlink, UPSTREAM_MODIFICATIONS.md, ADR 0031/0019 and the load README.

What changed

  1. Converter priority (D3), authz/page-write/op-semaphore.ts. Before this change, under a saturating abuser, the 3 s aging rule gave the slot to an aged large waiter ahead of a queued small write. The small write then waited out the running conversion (up to the 7 s deadline and a reload) plus the aged one, overran its 8 s wait, and got 503 engine_busy (8 of 293 small writes in A1).
    • The new rule: when a large holder releases the slot, every small waiter still queued is marked. A marked small waiter goes before any aged large waiter.
    • Aging still prevents starvation, and the large-waiter bound is unaffected by the rate of small writes.
    • The comments in content-parse.service.ts now state the correct worst case.
  2. F1, authz/page-write/yield-to-event-loop.ts. A single setImmediate queued from a poll-phase callback (pg, Redis, a worker message) runs in the same check phase, before libuv polls again. So waiting requests saw the load and the transaction, or the build and the transaction, as one block. The yield is now setImmediate(() => setImmediate(resolve)). replace-update.ts re-exports it.
  3. F2, persistence-steps.ts plus seam §122. onLoadDocument read content (jsonb, which Kysely's CamelCasePlugin deep-walks) even when the row's ydoc is present. It now reads content only when the ydoc is missing.
  4. F3, §122. Inside the store's executeTx (row lock held) there are three yields:
  5. F4, doc-version.ts warmLiveContentDigest plus seam §2. On a resident CAS write whose memo is cold, the handler warms the digest and yields only if the warm-up walked the page. The resident lookup, the pre-check and the transaction then follow with no yield between the pre-check and the transaction (add a public switch, which can be viewed without logging in docmost/docmost#282).

Why

These close the two engine-side blockers that the Graviton-calibrated A1 re-run found (cccvu/wiki-v2 issue 731, comment 6725645012): the converter-priority engine_busy failures, and lag-probe blocks caused by yields that never reached the poll phase.

Tests

  • Semaphore (fake clock), five new specs:

    • a small waiter that waited out a large holder beats an aged large waiter;
    • aging still works;
    • the A1 pattern over 180 s simulated: 0 busy, and large throughput is not reduced;
    • no starvation under three closed-loop small writers;
    • a seeded random mix checking both bounds.
  • Pool specs: a regression of the i2-M-r1 trace.

  • F1: a real loopback TCP spec. An I/O frame that arrives during a step is handled before the continuation; the one-hop form fails it.

  • F2: unit cases and a real-stack load that never selects content when a ydoc exists, with byte-identical output. The legacy path is unchanged.

  • F3: pinned order lockedRead ~ reconcile fromYdoc encode ~ jsonToText ~ updatePage. An edit at each yield is persisted, and the next store's skip is decided correctly. content matches ydoc.

  • F4: order get, walk, tick, get(hit), open, transaction, and no macrotask between the pre-check and the transaction.

  • Mutation proofs: 3 for D3 and 8 for F1–F4, all red.

  • Runs:

    • full fork lane: 218 suites, 3,511 passed, 0 failed;
    • typecheck clean;
    • check-upstream-boundary.sh passes;
    • eslint clean.
  • Probe latency on the Mac (longest wait of an I/O event during one 990 KB write):

    Case Before After
    Non-resident 326/308 ms 177/178 ms
    Resident, cold memo 273/256 ms 188/189 ms

    What remains is the one replace transaction, about 175–195 ms.

Risk & rollback

  • Store row lock: it is held across a few extra loop turns. Other writers of that row wait in the database.
  • Large waiters: one can wait one extra small conversion in a deadline collision. The ADR records this.
  • Cold-memo warm-up: when it misses, the write pays one extra loop turn. A human edit that lands in it gets a correct 412.
  • Rollback: revert. There is no data change.

🤖 Generated with Claude Code

Implementation plan and progress (canonical, covers both PRs): https://github.com/cccvu/wiki-v2/issues/731#issuecomment-6726465600 . Review comments for this PR set live on cccvu/wiki-v2 PR 785.

PMQ9 and others added 5 commits November 8, 2026 13:27
…ion (docmost#731)

Aging granted an aged large waiter ahead of a small write at every release.
Whenever large conversions ran past the 3 s aging bound (always, at the A1
load), a small write waited the running conversion AND an aged one, past its
8 s wait: 503 engine_busy for 2.7% of A1's small writes.

An urgent waiter that has waited through a normal holder's turn is now served
before any aged one (OpSemaphore: a normal holder's release marks the queued
urgent waiters). So a small write waits out at most one large conversion
(≤ deadline + lag, reload credited) plus the small conversions queued ahead.
A large waiter still never starves: once it is the oldest and aged, no other
large runs first, so it waits at most the small writes that waited out the
last large conversion (one per small principal), whatever the small rate.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…uded (docmost#731)

yieldToEventLoop was one setImmediate. A content write's heavy steps run in
poll-phase callbacks (a pg or Redis reply, an HTTP request) or their promise
continuations, and an immediate queued there runs in the SAME iteration's
check phase, before libuv polls again: the steps on either side of the yield
were one block to every other request. It is now two hops, so the
continuation runs after the next poll.

The helper moves to its own dependency-free module (the persistence seam
uses it next) and replace-update re-exports it, so the handler's import is
unchanged. Every existing caller gets the fix: after the build, after a
fresh load (incl. the comment-mark paths through withYdocConnection).

Pinned with real loopback sockets: an I/O event that arrives during a
poll-phase step runs before the continuation, and a control shows the
one-hop form fails it (and the mutation reds the spec).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… between its page-sized steps (docmost#731)

F2. onLoadDocument read `content` (a page-sized jsonb parse plus Kysely's
CamelCasePlugin key walk) on every load but uses it only for a legacy row
with no ydoc. findPageRowForLoad reads the ydoc only, and reads again with
the content just for such a row; the branches use that second read, so they
see what one read at that moment would have returned.

F3. Inside onStoreDocument's transaction (row lock held) the store now yields
a loop turn after the locked read, after the snapshot and after jsonToText.
The snapshot (generation read, fromYdoc, encodeStateAsUpdate) is ONE
synchronous call, snapshotForStore, after the reconcile: content and ydoc are
always the same snapshot taken under the lock (docmost#390), and the generation
recorded is the snapshot's, so an edit landing in a later yield makes the
next store run (the docmost#731 skip). docmost#345, R4 and the failure signal unchanged.

Specs (real Hocuspocus 3.4.4 stack): no content read when the ydoc exists,
the legacy branch unchanged; the exact store step order with a macrotask
marker at each yield and none inside the snapshot; an edit in each yield is
persisted and never skipped over; an edit queued during fromYdoc would make
content and ydoc disagree if a yield were added inside the snapshot.
Each yield removed, a yield added inside the snapshot, and the old load read
all red the specs. content-write-passes counts unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e-check (docmost#731)

When a conditional write names a digest and the page is resident, an edit
since the settle left the memo cold, so the pre-check's page walk ran in the
same block as opening the connection and the transaction. The handler now
calls warmLiveContentDigest first and, only when that had to walk the page,
yields a loop turn; then the unchanged resident lookup, pre-check and
transaction. The memo is keyed on the afterTransaction generation, so an edit
in that yield makes the pre-check recompute (correct, never stale). Nothing
yields between the pre-check and the transaction (docmost#282). A warm memo (the
settle's digest) walks nothing and yields nothing extra.

Specs: warm → turn → pre-check (memo hit) → open → transaction; an edit in
the turn re-walks at the pre-check with no macrotask before the mutation;
the warm-up's hit/miss semantics against real Yjs. Dropping the warm yield,
or adding one after the pre-check, reds them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…re-safe JSON)

The store's snapshot moved into persistence-steps (docmost#731 F3), so docmost#773's storeSafeJson
wrap moves with it, and the store-safe-json fitness allowlist names persistence-steps.ts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@PMQ9
PMQ9 merged commit c429dd0 into wiki-v2 Nov 8, 2026
@PMQ9
PMQ9 deleted the fix/731-converter-priority branch November 8, 2026 20:55
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.

1 participant