Architectural Invariant Design: Capacitor 7 Background Task Budgeting and WakeLock Enforcement
Capacitor 7 Background Task Budgeting and WakeLock Enforcement defines an industrial-grade behavioral standard for mobile engineering. Enforces deterministic execution timeout fences and OS power governor compliance across iOS (BackgroundTasks/beginBackgroundTask) and Android (WorkManager/Doze Mode) in Capacitor 7. Mandates strict task budgeting fences (sub-25s on iOS), infallible completion handler dispatch via try/finally blocks, and idempotent Screen/CPU WakeLock acquisition-release cycles to prevent SIGKILL 0x8badf00d watchdog crashes and battery governor bans. 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 | Unbounded Background Tasks and Leaked WakeLocks Triggering OS Watchdog Terminations | 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 @capacitor/background-runner or @capacitor/background-task operations in an infallible try...finally block ensuring BackgroundTask.finish({ taskId }) executes unconditionally.
Engineers MUST ALWAYS wrap @capacitor/background-runner or @capacitor/background-task operations in an infallible try...finally block ensuring BackgroundTask.finish({ taskId }) executes unconditionally.
Engineers MUST NEVER exceed a 25-second execution budget within iOS background task delegates
Engineers MUST NEVER exceed a 25-second execution budget within iOS background task delegates; background operations must be bounded by an AbortController with a 20-second timeout fence to avoid the iOS 30-second watchdog SIGKILL (0x8badf00d).
Services MUST release any acquired ScreenReader or KeepAwake WakeLock prior to entering background states, listening to App.addListener('appStateChange') events to prevent Android OS power governor blacklisting.
Services MUST release any acquired ScreenReader or KeepAwake WakeLock prior to entering background states, listening to App.addListener('appStateChange') events to prevent Android OS power governor blacklisting.
Code MUST verify network and power constraints via native Network and Device APIs before scheduling heavy asynchronous synchronization queues in background tasks.
Code MUST verify network and power constraints via native Network and Device APIs before scheduling heavy asynchronous synchronization queues in background tasks.
Tasks running via native WorkManager delegates on Android MUST NOT execute continuous polling loops
Tasks running via native WorkManager delegates on Android MUST NOT execute continuous polling loops; defer to periodic WorkRequests or Push Notifications via FCM data payloads.
Engineers MUST handle task cancellation callbacks natively (e.g., expirationHandler on iOS) to immediately abort pending network requests and flush telemetry buffers before process termination.
Engineers MUST handle task cancellation callbacks natively (e.g., expirationHandler on iOS) to immediately abort pending network requests and flush telemetry buffers before process termination.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Capacitor 7 Background Task Budgeting and WakeLock Enforcement critical for AI coding assistants?
Enforces deterministic execution timeout fences and OS power governor compliance across iOS (BackgroundTasks/beginBackgroundTask) and Android (WorkManager/Doze Mode) in Capacitor 7. Mandates strict task budgeting fences (sub-25s on iOS), infallible completion handler dispatch via try/finally blocks, and idempotent Screen/CPU WakeLock acquisition-release cycles to prevent SIGKILL 0x8badf00d watchdog crashes and battery governor bans. 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 (Unbounded Background Tasks and Leaked WakeLocks Triggering OS Watchdog Terminations) 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.