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

Capacitor 7 Native JSI Zero-Copy SharedBuffer Isolation Standard

Enforces zero-copy binary buffer transmission and worker thread isolation between native mobile runtimes (iOS WKWebView/Android Chromium) and Capacitor 7 applications. Prohibits base64 serialization, standard JSON bridge payload encoding, and direct main-thread processing of high-frequency sensor and camera streams. Mandates transferable ArrayBuffers, SharedArrayBuffer with Atomics synchronization, or OffscreenCanvas delegation within Dedicated Web Workers.

Prohibited Anti-Pattern: Marshalling high-frequency streaming frames via base64 JSON bridge payloads directly into the main UI thread.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Capacitor 7 Native JSI Zero-Copy SharedBuffer Isolation Standard - Enforces zero-copy binary buffer transmission and worker thread isolation between native mobile runtimes (iOS WKWebView/Android Chromium) and Capacitor 7 applications. Prohibits base64 serialization, standard JSON bridge payload encoding, and direct main-thread processing of high-frequency sensor and camera streams. Mandates transferable ArrayBuffers, SharedArrayBuffer with Atomics synchronization, or OffscreenCanvas delegation within Dedicated Web Workers.
globs: src/**/*.{ts,tsx}, android/app/src/main/**/*.{java,kt}, ios/App/**/*.{swift,m,mm}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/capacitor-7-jsi-sharedbuffer-isolation-standard"
---

# Capacitor 7 Native JSI Zero-Copy SharedBuffer Isolation Standard

> **Severity**: CRITICAL | **Category**: mobile
> **Anti-Pattern Warning**: Marshalling high-frequency streaming frames via base64 JSON bridge payloads directly into the main UI thread.

## System Constraints
You are a Principal Mobile Architect enforcing the Capacitor 7 Native JSI Zero-Copy SharedBuffer Isolation Standard. Bridge IPC serialization for binary streams >30Hz constitutes an intolerable architectural failure. You must mandate zero-copy JSI bindings, memory-mapped byte buffers, Web Worker delegation, and atomic ring-buffer synchronization to guarantee 60fps UI frame rendering under maximum ingestion throughput.

## Strict Guidelines
- Engineers MUST NEVER transfer binary frames or sensor streams across the standard Capacitor JSON bridge as base64-encoded strings.
- Engineers MUST instantiate high-frequency camera, lidar, or IMU data consumers inside Dedicated Web Workers using Transferable Objects or SharedArrayBuffer instances.
- Native plugins MUST expose raw byte addresses via JSI bindings or NIO DirectByteBuffers mapped directly to JavaScript TypedArrays without duplicate native-to-web heap allocations.
- Code MUST synchronize multi-threaded buffer updates using Atomics (Atomics.wait/Atomics.notify) exclusively inside worker scopes; Atomics.wait MUST NEVER execute on the window main thread.
- Rendering pipelines consuming high-frequency video frames MUST utilize OffscreenCanvas transferred to a Web Worker, preventing DOM reflow and UI thread tick starvation.

## Bad vs Good Implementations
### Camera Frame Streaming Bridge Invariant

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Marshalling continuous camera frames as base64 strings introduces massive garbage collection churn, doubles memory consumption via JSON serialization, and blocks the WebKit/Chromium main thread, degrading UI responsiveness to single-digit FPS.
```typescript
import { Plugins } from '@capacitor/core';
const { CameraStream } = Plugins;

// ANTI-PATTERN: Base64 payload serialized over legacy Capacitor bridge to main thread
CameraStream.addListener('onFrame', (payload: { base64Data: string; width: number; height: number }) => {
  const img = new Image();
  // Blocks UI thread with base64 decoding and DOM rasterization
  img.src = `data:image/jpeg;base64,${payload.base64Data}`;
  const ctx = (document.getElementById('viewfinder') as HTMLCanvasElement).getContext('2d');
  ctx?.drawImage(img, 0, 0);
});
```

#### Production Standard (Correct)
> **Standard**: Utilizes direct binary buffers decoupled from the main thread via Transferable ArrayBuffers and OffscreenCanvas in a Dedicated Web Worker, ensuring zero-copy memory transfers and 0ms main thread blocking.
```typescript
// PRODUCTION INVARIANT: Offloaded zero-copy processing via Dedicated Worker and Transferable ArrayBuffer
// worker.ts
self.onmessage = (event: MessageEvent<{ frameBuffer: ArrayBuffer; width: number; height: number }>) => {
  const { frameBuffer, width, height } = event.data;
  const pixels = new Uint8ClampedArray(frameBuffer);
  const imageData = new ImageData(pixels, width, height);
  // offscreenCtx represents an OffscreenCanvas transferred during worker init
  offscreenCtx.putImageData(imageData, 0, 0);
};

// main.ts
import { NativeJSIStream } from './native-jsi-stream';

const worker = new Worker(new URL('./worker.ts', import.meta.url), { type: 'module' });
const canvas = document.getElementById('viewfinder') as HTMLCanvasElement;
const offscreen = canvas.transferControlToOffscreen();
worker.postMessage({ canvas: offscreen }, [offscreen]);

// JSI direct memory hook bypassing standard Capacitor string bridge
NativeJSIStream.registerBufferSink((buffer: ArrayBuffer, width: number, height: number) => {
  // Transfer memory ownership to worker with zero copy
  worker.postMessage({ frameBuffer: buffer, width, height }, [buffer]);
});
```

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 Native JSI Zero-Copy SharedBuffer Isolation Standard

Capacitor 7 Native JSI Zero-Copy SharedBuffer Isolation Standard defines an industrial-grade behavioral standard for mobile engineering. Enforces zero-copy binary buffer transmission and worker thread isolation between native mobile runtimes (iOS WKWebView/Android Chromium) and Capacitor 7 applications. Prohibits base64 serialization, standard JSON bridge payload encoding, and direct main-thread processing of high-frequency sensor and camera streams. Mandates transferable ArrayBuffers, SharedArrayBuffer with Atomics synchronization, or OffscreenCanvas delegation within Dedicated Web Workers. 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 streaming frames via base64 JSON bridge payloads directly into the main UI thread. 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 streaming frames via base64 JSON bridge payloads directly into the main UI thread.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 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 NEVER transfer binary frames or sensor streams across the standard Capacitor JSON bridge as base64-encoded strings.

Engineers MUST NEVER transfer binary frames or sensor streams across the standard Capacitor JSON bridge as base64-encoded strings.

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 instantiate high-frequency camera, lidar, or IMU data consumers inside Dedicated Web Workers using Transferable Objects or SharedArrayBuffer instances.

Engineers MUST instantiate high-frequency camera, lidar, or IMU data consumers inside Dedicated Web Workers using Transferable Objects or SharedArrayBuffer instances.

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

Native plugins MUST expose raw byte addresses via JSI bindings or NIO DirectByteBuffers mapped directly to JavaScript TypedArrays without duplicate native-to-web heap allocations.

Native plugins MUST expose raw byte addresses via JSI bindings or NIO DirectByteBuffers mapped directly to JavaScript TypedArrays without duplicate native-to-web heap allocations.

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

Code MUST synchronize multi-threaded buffer updates using Atomics (Atomics.wait/Atomics.notify) exclusively inside worker scopes

Code MUST synchronize multi-threaded buffer updates using Atomics (Atomics.wait/Atomics.notify) exclusively inside worker scopes; Atomics.wait MUST NEVER execute on the window main 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.
5

Rendering pipelines consuming high-frequency video frames MUST utilize OffscreenCanvas transferred to a Web Worker, preventing DOM reflow and UI thread tick starvation.

Rendering pipelines consuming high-frequency video frames MUST utilize OffscreenCanvas transferred to a Web Worker, preventing DOM reflow and UI thread tick starvation.

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 Native JSI Zero-Copy SharedBuffer Isolation Standard critical for AI coding assistants?

Enforces zero-copy binary buffer transmission and worker thread isolation between native mobile runtimes (iOS WKWebView/Android Chromium) and Capacitor 7 applications. Prohibits base64 serialization, standard JSON bridge payload encoding, and direct main-thread processing of high-frequency sensor and camera streams. Mandates transferable ArrayBuffers, SharedArrayBuffer with Atomics synchronization, or OffscreenCanvas delegation within Dedicated Web Workers. 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 streaming frames via base64 JSON bridge payloads directly into the main UI thread.) 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