Skip to content

refactor(server): move connection authority and teardown into createConnections - #1344

Merged
devswha merged 2 commits into
mainfrom
chore/core-connections
Oct 18, 2026
Merged

devswha merged 2 commits into
mainfrom
chore/core-connections

Conversation

@devswha

@devswha devswha commented Oct 18, 2026

Copy link
Copy Markdown
Owner

Change

Step 4 of splitting the stateful core of server/index.ts by state owner. A connection's authority and its end now have one owner. No behavior changes.

  • closing, revoked, mode, roles, watches and unwatchDevice leave SocketData. server/core/connections.ts keeps them in a WeakMap keyed by the data object the upgrade passed, and only that file writes them. Other code reads mode, roles and closing through connectionOf(client), which returns the live state, not a copy.
  • createConnections takes mayType, authorizeSocket, the device revocation watch (admit), the role switch (setRole), the observe processes (watchPane, unwatchPane, killWatches) and end(client, reason).
  • end replaces stopSlowClient, the revoke callback and the WebSocket close body. Its three branches are those three bodies, each in its old order. detachClaimed and dropClaims in index.ts hold the claim loop and the three clears that each body repeated.
  • connectionData builds the data for both the local and the relayed upgrade.

Scope: clients, present, presence and ping stay in index.ts. The panes a connection holds (attached, conversationInterests, output) stay on SocketData until the terminal registry steps. The role frame still holds pending input before setRole, as before. This step was built from the series' handoff summary and the code; the full design note was not available to this session.

Why this is safe

  • The three teardowns differ, and every difference is kept. A stall returns early on a second call and leaves present and the device watch to the socket's close. A revoke has no early return and leaves pending.close to the socket's close. A close runs after both, and for a relayed connection it only closes the relay. The new server/core/connections.test.ts pins each of these, the detach-before-drop order, and the immediate end of a device revoked before its socket opened.
  • Each moved body is the old body with client.data.<field> replaced by the state. A script rewrote the 34 reads to connectionOf(...), and its counts match an inventory taken on main (11 closing, 13 mode, 10 roles). The writes moved by hand.
  • The typechecker enforces the single writer: SocketData no longer has these fields, and the state type is not exported.
  • Bun hands every handler the data object the upgrade passed. A probe on Bun 1.4.3 found ws.data === data in open, message and close. If a Bun upgrade changes that, stateOf throws instead of reading a missing state.
  • createConnections is called where mayType was declared. Everything it takes is declared above it or is a function declaration, and it runs nothing when it is built.
  • This touches device revocation, observe mode and pending.close, so it waits for a real CodeRabbit review per AGENTS.md.

server/index.ts goes from 2,381 to 2,319 lines.

Validation

  • Pins. bun run check run bun test --timeout 15000 over the contract files whose behavior this step can break (device-access, output, watch, submit, pending-input, take-over, ping, keys-lifecycle, input-membership, secret, conversation-push, api): 340 pass, 0 fail on an untouched main and on this branch. After the rebase onto the current main, the same files with the new unit test give 345 pass, 0 fail.
  • The new unit test fails on each of six "cleanups" applied to connections.ts one at a time: a closing guard on the close branch, pending.close on revoke, dropping claims before detaching, admitting a revoked device, dropping presence on a stall, and counting a switch to the same mode.
  • bun run check fast, before the rebase: typecheck and build are ok, and 4,439 unit tests pass with 0 failures. The convention tests pass, and the fixed-sleep baseline is unchanged (no Bun.sleep moves).
  • bun run check integration, before the rebase: 585 pass, 0 fail, 1 skip in 43 files. The rebase brought in only fix(gjc): retain bindings across unrelated SDK heartbeats #1343's GJC files.

🤖 Generated with Claude Code

devswha and others added 2 commits October 18, 2026 20:46
…core/connections

Step 4 of splitting the stateful core of server/index.ts by state owner.
closing, revoked, mode, roles, watches and unwatchDevice leave SocketData
for state that only server/core/connections.ts writes, keyed by the data
object the upgrade passed. createConnections owns mayType, authorizeSocket,
admit, setRole, the observe processes and end(), which keeps the stalled,
revoked and closed teardowns as they were.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A stall ends once and leaves presence and the device watch to the
socket's close. A revoke leaves pending.close to it. A relayed close only
closes the relay. Every end detaches before it drops the claims, and a
device revoked before its socket opened is not admitted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: devswha/herdr-web-ui/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 86ecef75-3a3b-423d-a596-721947a1d2db
📥 Commits

Reviewing files that changed from the base of the PR and between b5cdbdb and 0a11abb.

📒 Files selected for processing: 3
  • server/core/connections.test.ts
  • server/core/connections.ts
  • server/index.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 1 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Revoked devices can no longer connect or send typing activity, and connections are closed when access is revoked.
    • Read-only connections are limited to watching, and role changes are reflected in connection access.
    • Stalled connections are ended without removing presence until the socket closes.
    • Closing a relayed connection now also closes its relay.

The change moves connection lifecycle state and handling into shared helpers. The server uses these helpers for connection admission, access checks, pane watches, stalled output, and cleanup.

Changes: Connection lifecycle

Layer / File(s) Summary
Connection state and lifecycle
server/core/connections.ts, server/core/connections.test.ts
A WeakMap now stores connection lifecycle and role state. Shared helpers handle authorization, revocation, pane watches, and termination. Tests cover stalled connections, revoked devices, relays, role changes, and read-only access.
Server lifecycle wiring
server/index.ts
The server delegates admission, pane-watch management, and cleanup to shared helpers. Output-stall paths now end connections with the stalled reason.
Server access and role checks
server/index.ts
Server handlers use the shared connection view to check mode, closing state, and role counts while processing pane, input, and pending-action requests.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Merge Risk: ⚪ Minimal · up to 0a11a

No connection-lifecycle regression was identified. The change is ready to merge after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 26.92% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 26 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: moving connection authority and teardown into createConnections.
Description check Passed The description includes the required Change and Validation sections. It explains the behavior, scope, safety considerations, and test results in sufficient detail.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@devswha

devswha commented Oct 18, 2026

Copy link
Copy Markdown
Owner Author

CodeRabbit did not review this PR: its first run stopped at "Review limit reached". This step touches device revocation, observe mode and pending-input teardown, so per AGENTS.md it waits for a real CodeRabbit review before merge.

@devswha

devswha commented Oct 18, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@devswha
devswha merged commit 8dc4acc into main Oct 18, 2026
17 checks passed
@devswha
devswha deleted the chore/core-connections branch October 18, 2026 13:12
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