Architectural Invariant Design: Bun 1.2 WebSocket Engine Server-Side Hibernation & Memory Eviction Guardrail
Bun 1.2 WebSocket Engine Server-Side Hibernation & Memory Eviction Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces Bun 1.2 native uWebSockets-backed server-side hibernation semantics for ultra-dense real-time connection pooling. Prevents JavaScript V8/JSC heap exhaustion by eliminating long-lived JavaScript closure references, external maps, and non-pruned buffers, allowing idle sockets to drop their JS wrappers and sleep in kernel memory without V8 heap allocation. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.
Hazard Analysis: What Breaks Without This Rule
The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:
| Failure Mode | AI Symptom (The Anti-Pattern) | Severity | Production Impact & Remedy |
|---|---|---|---|
| AI Hallucination & Deprecation Drift | Retaining JavaScript Object References and External Map Registries for Dormant WebSockets | Critical | Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production. Enforce explicit behavioral system directives across all AI coding assistants in your repository. |
| Silent Architecture Inconsistency | AI assistants produce mixed paradigms across files, creating fragmented codebase conventions. | Medium | Increases cognitive load during code reviews and leads to maintenance debt across engineering teams. Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails. |
Step-by-Step Production Directives
Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:
Engineers MUST ALWAYS leverage Bun.serve({ websocket
Engineers MUST ALWAYS leverage Bun.serve({ websocket: { ... } }) native hibernation hooks (open, message, close, drain) without binding socket instances to outer closure scopes.
Engineers MUST NOT store `ServerWebSocket` instances in global `Map`, `Set`, or array registries, as holding a reference prevents Bun from deallocating the JS wrapper object during hibernation.
Engineers MUST NOT store `ServerWebSocket` instances in global `Map`, `Set`, or array registries, as holding a reference prevents Bun from deallocating the JS wrapper object during hibernation.
Code MUST store connection-specific metadata strictly inside the `ws.data` contextual payload initialized during the HTTP upgrade step via `server.upgrade(req, { data
Code MUST store connection-specific metadata strictly inside the `ws.data` contextual payload initialized during the HTTP upgrade step via `server.upgrade(req, { data: ... })`.
Services MUST delegate all fan-out multicasting to native topic subscriptions via `ws.subscribe(topic)` and `server.publish(topic, data)` rather than iterating over collections of socket instances in JavaScript.
Services MUST delegate all fan-out multicasting to native topic subscriptions via `ws.subscribe(topic)` and `server.publish(topic, data)` rather than iterating over collections of socket instances in JavaScript.
Components NEVER attach custom event listeners or instantiate long-lived closures over `ws` references inside lifecycle callbacks.
Components NEVER attach custom event listeners or instantiate long-lived closures over `ws` references inside lifecycle callbacks.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Bun 1.2 WebSocket Engine Server-Side Hibernation & Memory Eviction Guardrail critical for AI coding assistants?
Enforces Bun 1.2 native uWebSockets-backed server-side hibernation semantics for ultra-dense real-time connection pooling. Prevents JavaScript V8/JSC heap exhaustion by eliminating long-lived JavaScript closure references, external maps, and non-pruned buffers, allowing idle sockets to drop their JS wrappers and sleep in kernel memory without V8 heap allocation. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.
Q:How does adopting this rule prevent AI hallucinations?
This rule explicitly declares what the AI must avoid (Retaining JavaScript Object References and External Map Registries for Dormant WebSockets) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.
Q:Which AI coding environments are supported?
This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).
Q:Can I combine this rule with other technology stacks?
Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.