This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: bun-worker-zero-copy-arraybuffer-transfer

Bun Clustered Worker Zero-Copy ArrayBuffer Transfer Standard

Enforces transfer-of-ownership semantics over structural cloning across worker boundaries in multi-threaded Bun microservices. By passing transferable objects via postMessage transfer lists, worker communication bypasses memory duplication, eradicating IPC latency and garbage collection fragmentation under high-throughput network I/O workloads.

Prohibited Anti-Pattern: Implicit Structural Cloning of Binary Payloads Across Bun Worker Threads

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Bun Clustered Worker Zero-Copy ArrayBuffer Transfer Standard - Enforces transfer-of-ownership semantics over structural cloning across worker boundaries in multi-threaded Bun microservices. By passing transferable objects via postMessage transfer lists, worker communication bypasses memory duplication, eradicating IPC latency and garbage collection fragmentation under high-throughput network I/O workloads.
globs: src/**/*.worker.ts, src/**/workers/**/*.ts, src/**/cluster/**/*.ts, src/**/pipeline/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-worker-zero-copy-arraybuffer-transfer"
---

# Bun Clustered Worker Zero-Copy ArrayBuffer Transfer Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Implicit Structural Cloning of Binary Payloads Across Bun Worker Threads

## System Constraints
You are a Principal Backend Systems Architect specializing in Bun runtime concurrency and low-latency IPC. Your mandate is to enforce absolute zero-copy memory transfers between the main thread and Bun Worker threads. You must reject any postMessage calls sending raw TypedArrays, Buffers, or ArrayBuffers that omit explicit transfer lists, and prevent any subsequent access to neutered ArrayBuffers on the sender side.

## Strict Guidelines
- Engineers MUST ALWAYS pass underlying ArrayBuffer references in the second parameter (transfer list) of `postMessage` calls across Bun Worker boundaries.
- Engineers MUST NOT send Node.js `Buffer` instances directly via `postMessage` without extracting and transferring their underlying `ArrayBuffer` slice, as Buffers undergo structural cloning by default.
- Engineers MUST ensure that any transferred `ArrayBuffer` is treated as detached (neutered) immediately after dispatch, preventing read or write attempts on the originating thread.
- Services MUST allocate pinned `Uint8Array` or custom `ArrayBuffer` slabs outside message serialization loops to avoid heap fragmentation during rapid message passing.
- Worker inbound handlers MUST validate `byteLength > 0` on incoming `ArrayBuffer` instances before processing to guard against accidentally received pre-detached buffers.

## Bad vs Good Implementations
### Disaster: Structural Cloning via Implicit Buffer/Object Passing

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Omitting the transfer list forces the JavaScript engine to recursively serialize and duplicate the memory allocation into the target worker's heap. Under sustained I/O, this duplicates multi-gigabyte streams, destroys L3 cache residency, and causes massive GC pauses.
```typescript
import { Worker } from "bun";

const worker = new Worker(new URL("./processor.worker.ts", import.meta.url));

export function dispatchChunk(chunk: Uint8Array): void {
  // ANTI-PATTERN: Passing chunk inside an object without transferList.
  // Bun falls back to structured cloning, copying every byte over IPC
  // and triggering severe GC pressure and thread stalls under high throughput.
  worker.postMessage({
    type: "PROCESS_CHUNK",
    payload: chunk
  });
}
```

#### Production Standard (Correct)
> **Standard**: By explicitly extracting the target slice and declaring `[buffer]` in the transfer list, the memory ownership moves atomically to the target worker thread with O(1) pointer updates. The buffer becomes detached in the sender, eliminating IPC overhead and GC spikes.
```typescript
import { Worker } from "bun";

const worker = new Worker(new URL("./processor.worker.ts", import.meta.url));

export function dispatchChunk(chunk: Uint8Array): void {
  // Extract underlying ArrayBuffer or copy to an isolated ArrayBuffer if shared
  const buffer: ArrayBuffer = chunk.buffer.slice(
    chunk.byteOffset,
    chunk.byteOffset + chunk.byteLength
  );

  // INVARIANT: Transfer ownership of the buffer slice.
  // Memory is unmapped from current thread and re-mapped in worker instantly (O(1)).
  worker.postMessage(
    {
      type: "PROCESS_CHUNK",
      payload: buffer
    },
    [buffer]
  );

  // buffer.byteLength is now 0 (neutered/detached); source thread cannot access it.
}
```

### Worker Inbound: Safe Handling and Bidirectional Zero-Copy Echo

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The worker receives a copied or transferred buffer, mutates it, and echoes it back via structured cloning, once again incurring allocation and serialization costs.
```typescript
declare const self: Worker;

self.onmessage = (event: MessageEvent) => {
  const { payload } = event.data;
  // ANTI-PATTERN: Sending back processed TypedArray without transfer list
  const result = new Uint8Array(payload);
  result[0] ^= 0xff;
  self.postMessage({ type: "DONE", result });
};
```

#### Production Standard (Correct)
> **Standard**: The worker strictly asserts buffer integrity (non-zero byteLength) and completes all modifications in-place. It then moves ownership back to the caller using a transfer list, ensuring zero allocations in the IPC loop.
```typescript
declare const self: Worker;

interface InboundMessage {
  type: "PROCESS_CHUNK";
  payload: ArrayBuffer;
}

self.onmessage = (event: MessageEvent<InboundMessage>) => {
  const { type, payload } = event.data;
  if (type !== "PROCESS_CHUNK" || payload.byteLength === 0) {
    throw new Error("Protocol violation: Received neutered or invalid ArrayBuffer");
  }

  const view = new Uint8Array(payload);
  view[0] ^= 0xff; // In-place processing

  // INVARIANT: Return zero-copy buffer back to supervisor thread
  self.postMessage(
    {
      type: "DONE",
      result: payload
    },
    [payload]
  );
};
```

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 Clustered Worker Zero-Copy ArrayBuffer Transfer Standard

Bun Clustered Worker Zero-Copy ArrayBuffer Transfer Standard defines an industrial-grade behavioral standard for backend engineering. Enforces transfer-of-ownership semantics over structural cloning across worker boundaries in multi-threaded Bun microservices. By passing transferable objects via postMessage transfer lists, worker communication bypasses memory duplication, eradicating IPC latency and garbage collection fragmentation under high-throughput network I/O workloads. 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: Implicit Structural Cloning of Binary Payloads Across Bun Worker Threads 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 DriftImplicit Structural Cloning of Binary Payloads Across Bun Worker ThreadsCritical
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 pass underlying ArrayBuffer references in the second parameter (transfer list) of `postMessage` calls across Bun Worker boundaries.

Engineers MUST ALWAYS pass underlying ArrayBuffer references in the second parameter (transfer list) of `postMessage` calls across Bun Worker boundaries.

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 send Node.js `Buffer` instances directly via `postMessage` without extracting and transferring their underlying `ArrayBuffer` slice, as Buffers undergo structural cloning by default.

Engineers MUST NOT send Node.js `Buffer` instances directly via `postMessage` without extracting and transferring their underlying `ArrayBuffer` slice, as Buffers undergo structural cloning by default.

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

Engineers MUST ensure that any transferred `ArrayBuffer` is treated as detached (neutered) immediately after dispatch, preventing read or write attempts on the originating thread.

Engineers MUST ensure that any transferred `ArrayBuffer` is treated as detached (neutered) immediately after dispatch, preventing read or write attempts on the originating thread.

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 allocate pinned `Uint8Array` or custom `ArrayBuffer` slabs outside message serialization loops to avoid heap fragmentation during rapid message passing.

Services MUST allocate pinned `Uint8Array` or custom `ArrayBuffer` slabs outside message serialization loops to avoid heap fragmentation during rapid message passing.

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

Worker inbound handlers MUST validate `byteLength > 0` on incoming `ArrayBuffer` instances before processing to guard against accidentally received pre-detached buffers.

Worker inbound handlers MUST validate `byteLength > 0` on incoming `ArrayBuffer` instances before processing to guard against accidentally received pre-detached buffers.

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 Clustered Worker Zero-Copy ArrayBuffer Transfer Standard critical for AI coding assistants?

Enforces transfer-of-ownership semantics over structural cloning across worker boundaries in multi-threaded Bun microservices. By passing transferable objects via postMessage transfer lists, worker communication bypasses memory duplication, eradicating IPC latency and garbage collection fragmentation under high-throughput network I/O workloads. 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 (Implicit Structural Cloning of Binary Payloads Across Bun Worker Threads) 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