This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: bun-native-websocket-kernel-backpressure-standard

Bun 1.2 Clustered WebSocket Kernel Backpressure & Buffer Guardrail

Enforces strict socket-level backpressure management and native drain lifecycle handling in Bun 1.2 clustered WebSocket runtimes. Prevents catastrophic OOM container restarts caused by unbounded kernel/C++ buffer accumulation when streaming high-frequency LLM tokens to slow TCP clients.

Prohibited Anti-Pattern: Unbounded WebSocket Token Streaming without Native Buffer Probing or Drain Suspension

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Bun 1.2 Clustered WebSocket Kernel Backpressure & Buffer Guardrail - Enforces strict socket-level backpressure management and native drain lifecycle handling in Bun 1.2 clustered WebSocket runtimes. Prevents catastrophic OOM container restarts caused by unbounded kernel/C++ buffer accumulation when streaming high-frequency LLM tokens to slow TCP clients.
globs: src/**/*.ts, server/**/*.ts, workers/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-native-websocket-kernel-backpressure-standard"
---

# Bun 1.2 Clustered WebSocket Kernel Backpressure & Buffer Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded WebSocket Token Streaming without Native Buffer Probing or Drain Suspension

## System Constraints
You are a Principal Software Architect enforcing the Bun 1.2 Native Clustered WebSocket Kernel Backpressure Standard. High-throughput LLM streaming easily overwhelms slow mobile or rate-limited client sockets, causing silent heap/kernel buffer bloat in native C++ structures that bypass V8/JSC GC metrics. You must mandate strict socket buffer introspection (`ws.getBufferedAmount()`), explicit backpressure high-water limits, and asynchronous stream suspension integrated with Bun's native `drain` hook.

## Strict Guidelines
- Engineers MUST ALWAYS check `ws.getBufferedAmount()` against a strict high-water mark before dispatching payloads via `ws.send()`.
- Engineers MUST NOT execute unthrottled `for await` loops over LLM token generators directly piping into `ws.send()` without awaiting drain state.
- Code SHOULD define explicit backpressure thresholds (e.g., `HIGH_WATERMARK_BYTES = 64 * 1024` and `LOW_WATERMARK_BYTES = 16 * 1024`) per connection type.
- Services MUST implement Bun's native `drain(ws)` WebSocket handler to resolve backpressure promises and resume suspended token iterators.
- Components NEVER drop or ignore the byte count or status returned by `ws.send()`, and MUST abort inference streams immediately if client connection termination or buffer overflow conditions persist.

## Bad vs Good Implementations
### Unbounded LLM WebSocket Streaming vs. Native Drain Flow Control

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Iterating through an LLM async generator and firing `ws.send()` unconditionally ignores Bun's native C++ WebSocket buffer. If the client socket stalls or experiences network latency, memory balloons off-heap until the Bun cluster node crashes due to an uncatchable OS OOM signal.
```typescript
import type { ServerWebSocket } from "bun";

interface SessionData {
  sessionId: string;
}

// ANTI-PATTERN: Emits tokens directly into native buffer without inspecting backpressure.
// Under slow network conditions or high concurrency, buffered native memory explodes, causing OOM.
export async function streamLLMResponse(
  ws: ServerWebSocket<SessionData>,
  tokenStream: AsyncIterable<string>
): Promise<void> {
  for await (const token of tokenStream) {
    // Native socket buffer accumulates uncontrollably if client bandwidth < LLM generation rate
    ws.send(JSON.stringify({ type: "chunk", data: token }));
  }
  ws.send(JSON.stringify({ type: "done" }));
}
```

#### Production Standard (Correct)
> **Standard**: Inspects `ws.getBufferedAmount()` after dispatching chunks. If backpressure reaches the high-water threshold (64 KB), the iterator pauses upstream token generation and stores a resolver. When Bun's C++ network layer finishes flushing to the kernel socket buffer, it fires `drain(ws)`, resuming stream consumption without unbounded memory allocation.
```typescript
import type { ServerWebSocket, WebSocketHandler } from "bun";

const HIGH_WATERMARK_BYTES = 64 * 1024; // 64 KB

export interface FlowControlledData {
  sessionId: string;
  drainResolver?: () => void;
}

export const websocketHandlers: WebSocketHandler<FlowControlledData> = {
  open(ws) {
    ws.data.drainResolver = undefined;
  },
  message(ws, message) {
    // Dispatch incoming messages
  },
  drain(ws) {
    // Triggered by Bun's kernel/C++ layer when the socket buffer clears
    if (ws.data.drainResolver) {
      const resolve = ws.data.drainResolver;
      ws.data.drainResolver = undefined;
      resolve();
    }
  },
  close(ws) {
    if (ws.data.drainResolver) {
      ws.data.drainResolver();
      ws.data.drainResolver = undefined;
    }
  },
};

export async function streamLLMResponse(
  ws: ServerWebSocket<FlowControlledData>,
  tokenStream: AsyncIterable<string>
): Promise<void> {
  for await (const token of tokenStream) {
    if (ws.readyState !== 1) {
      break; // Socket closed, cancel upstream LLM generation
    }

    const payload = JSON.stringify({ type: "chunk", data: token });
    ws.send(payload);

    // Check kernel-level backpressure accumulation
    if (ws.getBufferedAmount() > HIGH_WATERMARK_BYTES) {
      await new Promise<void>((resolve) => {
        ws.data.drainResolver = resolve;
      });
    }
  }

  if (ws.readyState === 1) {
    ws.send(JSON.stringify({ type: "done" }));
  }
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Bun 1.2 Clustered WebSocket Kernel Backpressure & Buffer Guardrail

Bun 1.2 Clustered WebSocket Kernel Backpressure & Buffer Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces strict socket-level backpressure management and native drain lifecycle handling in Bun 1.2 clustered WebSocket runtimes. Prevents catastrophic OOM container restarts caused by unbounded kernel/C++ buffer accumulation when streaming high-frequency LLM tokens to slow TCP clients. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Unbounded WebSocket Token Streaming without Native Buffer Probing or Drain Suspension Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

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 ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftUnbounded WebSocket Token Streaming without Native Buffer Probing or Drain SuspensionCritical
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 InconsistencyAI 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.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS check `ws.getBufferedAmount()` against a strict high-water mark before dispatching payloads via `ws.send()`.

Engineers MUST ALWAYS check `ws.getBufferedAmount()` against a strict high-water mark before dispatching payloads via `ws.send()`.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT execute unthrottled `for await` loops over LLM token generators directly piping into `ws.send()` without awaiting drain state.

Engineers MUST NOT execute unthrottled `for await` loops over LLM token generators directly piping into `ws.send()` without awaiting drain state.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Code SHOULD define explicit backpressure thresholds (e.g., `HIGH_WATERMARK_BYTES = 64 * 1024` and `LOW_WATERMARK_BYTES = 16 * 1024`) per connection type.

Code SHOULD define explicit backpressure thresholds (e.g., `HIGH_WATERMARK_BYTES = 64 * 1024` and `LOW_WATERMARK_BYTES = 16 * 1024`) per connection type.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Services MUST implement Bun's native `drain(ws)` WebSocket handler to resolve backpressure promises and resume suspended token iterators.

Services MUST implement Bun's native `drain(ws)` WebSocket handler to resolve backpressure promises and resume suspended token iterators.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Components NEVER drop or ignore the byte count or status returned by `ws.send()`, and MUST abort inference streams immediately if client connection termination or buffer overflow conditions persist.

Components NEVER drop or ignore the byte count or status returned by `ws.send()`, and MUST abort inference streams immediately if client connection termination or buffer overflow conditions persist.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Bun 1.2 Clustered WebSocket Kernel Backpressure & Buffer Guardrail critical for AI coding assistants?

Enforces strict socket-level backpressure management and native drain lifecycle handling in Bun 1.2 clustered WebSocket runtimes. Prevents catastrophic OOM container restarts caused by unbounded kernel/C++ buffer accumulation when streaming high-frequency LLM tokens to slow TCP clients. 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 (Unbounded WebSocket Token Streaming without Native Buffer Probing or Drain Suspension) 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.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Bun