Architectural Invariant Design: Capacitor 7 Background Task Execution Fence & Power-Budget Policy
Capacitor 7 Background Task Execution Fence & Power-Budget Policy defines an industrial-grade behavioral standard for mobile engineering. Enforces deterministic execution fences, abort controllers, and atomic checkpointing for SQLite batch synchronization within Capacitor 7 background tasks. Prevents immediate OS-level SIGKILL terminations and SQLite database corruption on iOS and Android when background processing limits (e.g., 30s) are exceeded. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.
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 Mode | AI Symptom (The Anti-Pattern) | Severity | Production Impact & Remedy |
|---|---|---|---|
| AI Hallucination & Deprecation Drift | Unfenced Async SQLite Loops in Capacitor App Lifecycle Transitions Causing Process Death and Corrupted State | 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 Inconsistency | AI 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. |
Step-by-Step Production Directives
Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:
Engineers MUST ALWAYS wrap any background synchronization task registered through Capacitor's BackgroundTask API in a defensive try/finally block that guarantees calling `BackgroundTask.finish({ taskId })`.
Engineers MUST ALWAYS wrap any background synchronization task registered through Capacitor's BackgroundTask API in a defensive try/finally block that guarantees calling `BackgroundTask.finish({ taskId })`.
Engineers MUST instantiate an `AbortController` bounded by an aggressive execution fence (default 25,000ms, leaving a 5,000ms safety margin for cleanup) whenever entering background sync execution.
Engineers MUST instantiate an `AbortController` bounded by an aggressive execution fence (default 25,000ms, leaving a 5,000ms safety margin for cleanup) whenever entering background sync execution.
Engineers MUST NOT execute open-ended, unbounded `Promise.all` or sequential unbounded loops over outbox SQLite queues in the background without checking the abort signal on every iteration.
Engineers MUST NOT execute open-ended, unbounded `Promise.all` or sequential unbounded loops over outbox SQLite queues in the background without checking the abort signal on every iteration.
Engineers MUST commit SQLite mutations in chunked atomic transactions and record an explicit cursor/watermark, ensuring interrupted jobs resume without duplicate mutations or partial writes.
Engineers MUST commit SQLite mutations in chunked atomic transactions and record an explicit cursor/watermark, ensuring interrupted jobs resume without duplicate mutations or partial writes.
Services MUST cancel and roll back in-flight SQLite write transactions immediately upon receiving an OS expiration event (`App.addListener('appStateChange')` or platform timeout) before the process is terminated.
Services MUST cancel and roll back in-flight SQLite write transactions immediately upon receiving an OS expiration event (`App.addListener('appStateChange')` or platform timeout) before the process is terminated.
Components NEVER execute raw HTTP mutations or direct sync operations within UI lifecycle hooks without passing through the central gated SyncCoordinator.
Components NEVER execute raw HTTP mutations or direct sync operations within UI lifecycle hooks without passing through the central gated SyncCoordinator.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Capacitor 7 Background Task Execution Fence & Power-Budget Policy critical for AI coding assistants?
Enforces deterministic execution fences, abort controllers, and atomic checkpointing for SQLite batch synchronization within Capacitor 7 background tasks. Prevents immediate OS-level SIGKILL terminations and SQLite database corruption on iOS and Android when background processing limits (e.g., 30s) are exceeded. 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 (Unfenced Async SQLite Loops in Capacitor App Lifecycle Transitions Causing Process Death and Corrupted State) 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.