This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: bun-worker-memory-arena-scoping

Deterministic Worker Memory Arena Scoping and Native Transfer Invariant in Bun 1.2

Enforces strict lifecycle boundaries and zero-copy ArrayBuffer transfer semantics across Bun 1.2 worker pools. Prevents non-deterministic V8/JavaScriptCore heap retention and native memory fragmentation by mandating explicit arena deallocation via transfer lists and manual worker lifecycle scoping.

Prohibited Anti-Pattern: Implicit Structured Cloning of Large Buffers Across Worker Boundaries Causing Silent Heap Bloat

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Deterministic Worker Memory Arena Scoping and Native Transfer Invariant in Bun 1.2 - Enforces strict lifecycle boundaries and zero-copy ArrayBuffer transfer semantics across Bun 1.2 worker pools. Prevents non-deterministic V8/JavaScriptCore heap retention and native memory fragmentation by mandating explicit arena deallocation via transfer lists and manual worker lifecycle scoping.
globs: src/workers/**/*.{ts,js}, src/services/**/*.{ts,js}, src/lib/pool/**/*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-worker-memory-arena-scoping"
---

# Deterministic Worker Memory Arena Scoping and Native Transfer Invariant in Bun 1.2

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Implicit Structured Cloning of Large Buffers Across Worker Boundaries Causing Silent Heap Bloat

## System Constraints
You are a Principal Backend Systems Architect enforcing strict Bun 1.2 Multi-Worker Memory Arena Scoping. You must mandate zero-copy typed array transfers via Transferable objects, reject cross-worker structural cloning of unbounded payloads, and ensure worker execution contexts deterministically release native heap allocations without relying on opportunistic garbage collection cycles.

## Strict Guidelines
- Engineers MUST ALWAYS pass high-volume ArrayBuffers, TypedArrays, and message payloads using the transferable objects list in `worker.postMessage(message, [transfer])` to ensure zero-copy semantics and immediate source thread detachment.
- Engineers MUST NOT rely on implicit structured cloning across long-lived Bun workers for data payloads exceeding 64 KiB, as this duplicates heap allocations and induces severe native heap fragmentation.
- Code MUST explicitly coordinate bounded worker lifetimes or recycle long-running worker contexts through periodic drain-and-terminate cycles using `worker.terminate()` when accumulated arena metrics breach predefined watermark thresholds.
- Services MUST isolate foreign data parsing and native buffer deserialization into distinct, ephemeral worker scopes that detach shared memory immediately upon completion.
- Components NEVER capture persistent outer-scope references to transferable buffers after passing them through `postMessage`, as reading from a detached ArrayBuffer produces runtime TypeErrors or silent 0-byte corruptions.

## Bad vs Good Implementations
### Zero-Copy Worker Ingestion vs Structured Clone Heap Doubling

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Passing a TypedArray directly in the message payload without specifying it in the transfer list triggers structured cloning. Both the main thread and worker thread retain heap allocations for identical data, leading to heap fragmentation and uncoordinated JSC memory sweeps.
```typescript
import { Worker } from "bun";

export class PayloadProcessor {
  private worker = new Worker(new URL("./worker.ts", import.meta.url));

  public async processData(data: Uint8Array): Promise<void> {
    // BAD: Implicit structured clone causes native heap duplication and memory bloat
    // In Bun 1.2, heavy loads cause GC thrashing and fragmented native heap allocations
    this.worker.postMessage({ 
      type: "PROCESS_CHUNK", 
      payload: data 
    });
  }
}
```

#### Production Standard (Correct)
> **Standard**: By transferring the underlying ArrayBuffer via the second argument array to postMessage, Bun re-assigns the memory pointer to the target worker without memory duplication. The original thread's view is instantly detached, guaranteeing deterministic memory bounds.
```typescript
import { Worker } from "bun";

export class PayloadProcessor {
  private worker = new Worker(new URL("./worker.ts", import.meta.url));

  public async processData(data: Uint8Array): Promise<void> {
    // GOOD: Explicit zero-copy transfer of the underlying ArrayBuffer arena
    // Source buffer is instantly detached from caller memory, leaving zero residual heap
    const buffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
    
    this.worker.postMessage(
      { 
        type: "PROCESS_CHUNK", 
        byteLength: buffer.byteLength, 
        payload: buffer 
      },
      [buffer]
    );
  }
}
```

### Deterministic Worker Termination and Arena Disposal

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Creating workers without a deterministically managed termination cycle leaves active WebKit JSC contexts alive in the Bun runtime. Even idle worker contexts retain stack arenas, metadata structures, and baseline VM heap spaces indefinitely.
```typescript
export async function runHeavyTask(taskPayload: ArrayBuffer): Promise<ArrayBuffer> {
  const worker = new Worker(new URL("./heavy-task.ts", import.meta.url));
  
  return new Promise((resolve) => {
    worker.onmessage = (event) => {
      // BAD: Worker is left alive without termination or explicit arena cleanup
      resolve(event.data.result);
    };
    worker.postMessage({ taskPayload }, [taskPayload]);
  });
}
```

#### Production Standard (Correct)
> **Standard**: Guarantees complete native worker destruction and reclaim of thread-specific virtual memory immediately upon work conclusion or failure, leaving zero lingering threads or uncollected stack arenas.
```typescript
export async function runHeavyTask(taskPayload: ArrayBuffer): Promise<ArrayBuffer> {
  const worker = new Worker(new URL("./heavy-task.ts", import.meta.url));
  
  return new Promise((resolve, reject) => {
    const timeoutId = setTimeout(() => {
      worker.terminate();
      reject(new Error("Worker operation timed out"));
    }, 15000);

    worker.onmessage = (event: MessageEvent<{ result: ArrayBuffer }>) => {
      clearTimeout(timeoutId);
      const { result } = event.data;
      // GOOD: Immediately terminate worker to destroy isolated thread arena
      worker.terminate();
      resolve(result);
    };

    worker.onerror = (err) => {
      clearTimeout(timeoutId);
      worker.terminate();
      reject(err);
    };

    worker.postMessage({ taskPayload }, [taskPayload]);
  });
}
```

Recommended Agent Skills (askills.dev)

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

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Deterministic Worker Memory Arena Scoping and Native Transfer Invariant in Bun 1.2

Deterministic Worker Memory Arena Scoping and Native Transfer Invariant in Bun 1.2 defines an industrial-grade behavioral standard for backend engineering. Enforces strict lifecycle boundaries and zero-copy ArrayBuffer transfer semantics across Bun 1.2 worker pools. Prevents non-deterministic V8/JavaScriptCore heap retention and native memory fragmentation by mandating explicit arena deallocation via transfer lists and manual worker lifecycle scoping. 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 Structured Cloning of Large Buffers Across Worker Boundaries Causing Silent Heap Bloat 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 Structured Cloning of Large Buffers Across Worker Boundaries Causing Silent Heap BloatCritical
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 high-volume ArrayBuffers, TypedArrays, and message payloads using the transferable objects list in `worker.postMessage(message, [transfer])` to ensure zero-copy semantics and immediate source thread detachment.

Engineers MUST ALWAYS pass high-volume ArrayBuffers, TypedArrays, and message payloads using the transferable objects list in `worker.postMessage(message, [transfer])` to ensure zero-copy semantics and immediate source thread detachment.

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 rely on implicit structured cloning across long-lived Bun workers for data payloads exceeding 64 KiB, as this duplicates heap allocations and induces severe native heap fragmentation.

Engineers MUST NOT rely on implicit structured cloning across long-lived Bun workers for data payloads exceeding 64 KiB, as this duplicates heap allocations and induces severe native heap fragmentation.

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 MUST explicitly coordinate bounded worker lifetimes or recycle long-running worker contexts through periodic drain-and-terminate cycles using `worker.terminate()` when accumulated arena metrics breach predefined watermark thresholds.

Code MUST explicitly coordinate bounded worker lifetimes or recycle long-running worker contexts through periodic drain-and-terminate cycles using `worker.terminate()` when accumulated arena metrics breach predefined watermark thresholds.

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 isolate foreign data parsing and native buffer deserialization into distinct, ephemeral worker scopes that detach shared memory immediately upon completion.

Services MUST isolate foreign data parsing and native buffer deserialization into distinct, ephemeral worker scopes that detach shared memory immediately upon completion.

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 capture persistent outer-scope references to transferable buffers after passing them through `postMessage`, as reading from a detached ArrayBuffer produces runtime TypeErrors or silent 0-byte corruptions.

Components NEVER capture persistent outer-scope references to transferable buffers after passing them through `postMessage`, as reading from a detached ArrayBuffer produces runtime TypeErrors or silent 0-byte corruptions.

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 Deterministic Worker Memory Arena Scoping and Native Transfer Invariant in Bun 1.2 critical for AI coding assistants?

Enforces strict lifecycle boundaries and zero-copy ArrayBuffer transfer semantics across Bun 1.2 worker pools. Prevents non-deterministic V8/JavaScriptCore heap retention and native memory fragmentation by mandating explicit arena deallocation via transfer lists and manual worker lifecycle scoping. 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 Structured Cloning of Large Buffers Across Worker Boundaries Causing Silent Heap Bloat) 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

Deterministic