Add PROOF_SINGLE_REPLICA mode to avoid false LIVE_DOC_UNAVAILABLE on single-replica self-hosts#58
Open
nishfaria wants to merge 1 commit into
Open
Conversation
…single-replica self-hosts The live-doc mutation gate assumes a multi-replica fleet: a recent collab-session lease with no live connection on the current node (total > 0 && exactEpochCount === 0) is read as "another replica holds the live document" and returns LIVE_DOC_UNAVAILABLE, blocking /edit/v2 (and rewrite.apply) until the lease expires. On a single-replica deployment there are no sibling nodes, so this is a false positive: after any viewer disconnects, all programmatic edits are blocked for the COLLAB_SESSION_TTL_SECONDS window (default 5 min), which pushes agents to recreate documents instead of updating in place. Add an opt-in PROOF_SINGLE_REPLICA flag (isSingleReplicaDeployment) plus a derived isHostedMultiReplicaRewriteEnvironment helper. When single-replica: - the canonical mutation derives activeCollabClients from exactEpochCount (this node's own connections) instead of ghost leases, and skips the hostedRemoteLiveLease block - agent edit/v2 strict live-client count uses exactEpochCount Default behavior is unchanged when the flag is unset. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Problem
On a single-replica self-hosted deployment, all programmatic edits (
/edit/v2,rewrite.apply) get blocked withLIVE_DOC_UNAVAILABLEfor several minutes after any viewer opens a document — even once nobody is connected.The live-doc mutation gate is designed for a multi-replica fleet. It treats a recent collab-session lease with no live connection on the current node as evidence that another replica holds the live document:
When a viewer disconnects,
exactEpochCountdrops to0but the session lease lingers forCOLLAB_SESSION_TTL_SECONDS(default 300s). On a single node there is no sibling replica, sototal > 0 && exactEpochCount === 0simply means "nobody is connected" — yet the gate returnsLIVE_DOC_UNAVAILABLEand blocks the edit. In practice this pushes agents to recreate documents instead of updating in place (which loses comments/marks anchored to the original).isHostedRewriteEnvironment()is true wheneverPROOF_ENV=production, so any production single-container deployment hits this.Fix
Add an opt-in
PROOF_SINGLE_REPLICAflag. When set, the gate trusts this node's own connection view instead of ghost leases.rewrite-policy.ts: newisSingleReplicaDeployment()+ derivedisHostedMultiReplicaRewriteEnvironment().canonical-document.ts:activeCollabClientsderives fromexactEpochCount(own connections) instead oftotal; thehostedRemoteLiveLeaseblock is skipped when single-replica.agent-edit-v2.ts:getStrictLiveClientCount/…WithGraceuseexactEpochCountwhen single-replica.Default behavior is unchanged when the flag is unset — multi-replica deployments keep the existing conservative gating.
Result (verified on a single-container deployment)
With
PROOF_SINGLE_REPLICA=1:/edit/v2succeeds while a viewer is actively connected — the edit merges into the live Yjs document, and the connected browser receives it in real time (collab.status: confirmed).LIVE_DOC_UNAVAILABLEfalse positives after a viewer disconnects.Notes
RAILWAY_REPLICA_ID"), since cold starts could misclassify; happy to switch to auto-detection if preferred.