This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
mobilecritical PriorityID: capacitor-7-jsi-direct-memory-arraybuffer-bridge

Capacitor 7 JSI Zero-Copy ArrayBuffer Direct Memory Bridge

Enforces direct-memory off-heap ArrayBuffer communication across the web-to-native boundary in Capacitor 7, eliminating JSON serialization latency and memory thrashing. Mandates typed binary schemas, memory alignment, and non-blocking ring-buffer dispatch for high-frequency sensor and biometrics telemetry.

Prohibited Anti-Pattern: Marshalling High-Frequency Sensor Telemetry Through JSON-Serialized Bridge Messages

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Capacitor 7 JSI Zero-Copy ArrayBuffer Direct Memory Bridge - Enforces direct-memory off-heap ArrayBuffer communication across the web-to-native boundary in Capacitor 7, eliminating JSON serialization latency and memory thrashing. Mandates typed binary schemas, memory alignment, and non-blocking ring-buffer dispatch for high-frequency sensor and biometrics telemetry.
globs: src/**/*.{ts,tsx}, ios/**/*.{swift,mm}, android/**/*.kt
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/capacitor-7-jsi-direct-memory-arraybuffer-bridge"
---

# Capacitor 7 JSI Zero-Copy ArrayBuffer Direct Memory Bridge

> **Severity**: CRITICAL | **Category**: mobile
> **Anti-Pattern Warning**: Marshalling High-Frequency Sensor Telemetry Through JSON-Serialized Bridge Messages

## System Constraints
You are a Principal Mobile Architect enforcing Capacitor 7 JSI Direct-Memory ArrayBuffer Bridge standards. Reject any implementation that serializes streaming telemetry (>30Hz or >64KB payloads) over JSON or standard WebView message handlers. Require typed off-heap ArrayBuffers, explicit endianness management, zero-copy buffer handoffs, and memory-safe native pointer disposal.

## Strict Guidelines
- Engineers MUST ALWAYS transfer high-frequency telemetry via typed ArrayBuffer or DataView instances using Capacitor 7 JSI direct-memory binding instead of standard JSON bridge calls.
- Engineers MUST NOT call JSON.stringify, JSON.parse, or pass nested JavaScript objects through bridge methods executed at rates exceeding 30Hz.
- Code SHOULD pre-allocate static off-heap ring buffers to avoid engine garbage collection spikes and fragmentation during sustained biometrics streaming.
- Services MUST enforce explicit little-endian byte-packing specifications (Float32Array/Int16Array offsets) matching the native C++/Swift/Kotlin struct byte alignment.
- Components NEVER retain references to transferred or detached ArrayBuffers once ownership has been handed off to native threads.

## Bad vs Good Implementations
### High-Frequency Biometrics Telemetry Ingestion

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Standard Capacitor plugin calls serialize arguments into JSON strings across WKWebView/Android bridge channels. At 100Hz+, this introduces massive GC pressure, bridge saturation, and dropped frames.
```typescript
import { registerPlugin } from '@capacitor/core';

interface SensorFrame {
  timestamp: number;
  ppgChannel1: number;
  ppgChannel2: number;
  accelX: number;
  accelY: number;
  accelZ: number;
}

const TelemetryBridge = registerPlugin<{ streamData(payload: { frame: SensorFrame }): Promise<void> }>('Telemetry');

// ANTI-PATTERN: Serializes 100Hz sensor frame into JSON across standard bridge,
// causing GC churn, JSON parsing bottlenecks, and main thread frame drops.
export function dispatchTelemetryFrame(data: SensorFrame): void {
  TelemetryBridge.streamData({ frame: data });
}
```

#### Production Standard (Correct)
> **Standard**: Encodes telemetry into a pre-allocated DataView with strict 28-byte binary layout and forwards the raw ArrayBuffer via JSI direct memory, completely bypassing JSON serialization.
```typescript
import { registerPlugin } from '@capacitor/core';

// 28-byte aligned frame schema: Float64 (timestamp: 8b) + 5x Float32 (telemetry: 20b)
const BYTES_PER_FRAME = 28;
const telemetryBuffer = new ArrayBuffer(BYTES_PER_FRAME);
const telemetryView = new DataView(telemetryBuffer);

interface NativeJsiTelemetryPlugin {
  pushDirectBuffer(buffer: ArrayBuffer): void;
}

const JsiTelemetry = registerPlugin<NativeJsiTelemetryPlugin>('Telemetry', {
  // Configured with Capacitor 7 direct JSI memory transport
});

export function dispatchTelemetryDirect(
  timestamp: number,
  ppg1: number,
  ppg2: number,
  ax: number,
  ay: number,
  az: number
): void {
  // Invariant: Direct-memory binary encoding with deterministic little-endian alignment
  telemetryView.setFloat64(0, timestamp, true);
  telemetryView.setFloat32(8, ppg1, true);
  telemetryView.setFloat32(12, ppg2, true);
  telemetryView.setFloat32(16, ax, true);
  telemetryView.setFloat32(20, ay, true);
  telemetryView.setFloat32(24, az, true);

  // Zero-copy off-heap byte buffer transfer directly into native JSI target
  JsiTelemetry.pushDirectBuffer(telemetryBuffer);
}
```

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: Capacitor 7 JSI Zero-Copy ArrayBuffer Direct Memory Bridge

Capacitor 7 JSI Zero-Copy ArrayBuffer Direct Memory Bridge defines an industrial-grade behavioral standard for mobile engineering. Enforces direct-memory off-heap ArrayBuffer communication across the web-to-native boundary in Capacitor 7, eliminating JSON serialization latency and memory thrashing. Mandates typed binary schemas, memory alignment, and non-blocking ring-buffer dispatch for high-frequency sensor and biometrics telemetry. 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: Marshalling High-Frequency Sensor Telemetry Through JSON-Serialized Bridge Messages 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 DriftMarshalling High-Frequency Sensor Telemetry Through JSON-Serialized Bridge MessagesCritical
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 transfer high-frequency telemetry via typed ArrayBuffer or DataView instances using Capacitor 7 JSI direct-memory binding instead of standard JSON bridge calls.

Engineers MUST ALWAYS transfer high-frequency telemetry via typed ArrayBuffer or DataView instances using Capacitor 7 JSI direct-memory binding instead of standard JSON bridge calls.

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 call JSON.stringify, JSON.parse, or pass nested JavaScript objects through bridge methods executed at rates exceeding 30Hz.

Engineers MUST NOT call JSON.stringify, JSON.parse, or pass nested JavaScript objects through bridge methods executed at rates exceeding 30Hz.

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 pre-allocate static off-heap ring buffers to avoid engine garbage collection spikes and fragmentation during sustained biometrics streaming.

Code SHOULD pre-allocate static off-heap ring buffers to avoid engine garbage collection spikes and fragmentation during sustained biometrics streaming.

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 enforce explicit little-endian byte-packing specifications (Float32Array/Int16Array offsets) matching the native C++/Swift/Kotlin struct byte alignment.

Services MUST enforce explicit little-endian byte-packing specifications (Float32Array/Int16Array offsets) matching the native C++/Swift/Kotlin struct byte alignment.

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 retain references to transferred or detached ArrayBuffers once ownership has been handed off to native threads.

Components NEVER retain references to transferred or detached ArrayBuffers once ownership has been handed off to native threads.

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 Capacitor 7 JSI Zero-Copy ArrayBuffer Direct Memory Bridge critical for AI coding assistants?

Enforces direct-memory off-heap ArrayBuffer communication across the web-to-native boundary in Capacitor 7, eliminating JSON serialization latency and memory thrashing. Mandates typed binary schemas, memory alignment, and non-blocking ring-buffer dispatch for high-frequency sensor and biometrics telemetry. 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 (Marshalling High-Frequency Sensor Telemetry Through JSON-Serialized Bridge Messages) 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.

Related MOBILE Rules

View all rules →
1 Rule in Stack

Capacitor