fix(server): refcount heartbeat clients instead of exiting on first disconnect - #1
Draft
jscheid wants to merge 4 commits into
Draft
fix(server): refcount heartbeat clients instead of exiting on first disconnect#1jscheid wants to merge 4 commits into
jscheid wants to merge 4 commits into
Conversation
Previously the heartbeat handler shut the server down as soon as any one SSE connection closed, so a second open tab killed the review for everyone. Wire in the ReviewLifecycle from Task 1: track connect/ disconnect counts, and only shut down once the client count has been zero for idleGraceMs (default 10s, overridable via ServerOptions).
Closing on error disabled EventSource's built-in retry, so a single transient drop made a live tab look permanently gone to the server's refcount-based idle detection. Log and let the browser reconnect.
Adds a --idle-grace <seconds> option that maps to ServerOptions.idleGraceMs (converted to milliseconds), so the heartbeat-refcount grace period is tunable without editing code. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This was referenced Sep 4, 2026
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.
Replaces "exit when any one browser disconnects" with a refcounted, grace-period idle decision, and stops the client suppressing its own SSE reconnect.
Groundwork for the one-way review handoff (
docs/design/one-way-review-handoff.md§5–6): an agent can only be told a review is finished if the server can tell that nobody is looking at it any more.Why
Two live bugs in
main:/api/heartbeatclose handler shut the whole server down when any single stream closed. With two tabs open, closing one ended the other's session.eventSource.close()inonerror, which disables the browser's built-inEventSourceretry. One transient drop made a live tab look permanently gone. Rare on loopback; routine behind a reverse proxy with an idle timeout.Approach
The decision logic is a pure, clock-injected
ReviewLifecyclevalue in a new module —stateAt(now)never reads the wall clock, so the grace period is tested without sleeping. The Express handler keeps only the imperative parts: counting, and one timer that asks the pure value whether the review has gone terminal.New module rather than more logic in
src/server/server.ts(~1150 lines, changes often upstream) to keep rebase conflict surface small.Notes for the reviewer
disconnectscounts transitions to zero clients, not individual stream closes. Counting every close would make a reviewer closing three tabs look identical to three network drops. Nothing consumes it yet; a follow-up branch will.useFileWatchstill hand-rolls reconnection for/api/watchwith a five-attempt cap, after which the client goes permanently stale. Deliberately not copied here, and deliberately not fixed here — separate defect.--keep-alive, the "staying alive" log now prints after the grace period rather than immediately on disconnect.unref'd and cleared onserver.close(), so it cannot outlive its server.afterEachin the test suite restores the realprocess.exitbefore closing, so a leaked timer would have been able to kill the test runner.Verification
pnpm test879 passed / 2 skipped ·pnpm checkclean ·pnpm buildclean.Run everything under the pinned toolchain (
mise exec -- pnpm …). Node 26 produces ~70 spuriouslocalStoragefailures against this repo's happy-dom/vitest pairing and has segfaultedtsgolint;mise.tomlpins Node 24.