Imported from dwijendrapydimarri-collab/cardinal (
AGENTS.md). Install upstream withnpx skills add dwijendrapydimarri-collab/cardinal. Copyright stays with the author.
Cardinal: On-Device Perceptual Threat Defense Architecture
1. Operational Directives & Golden Path
Cardinal is a real-time, 100% on-device perceptual threat defense system engineered for Android (optimized for iQOO / Vivo devices with Snapdragon Hexagon NPU). It intercepts screen content via MediaProjection, processes downsampled perceptual probes, detects sensitive information exposures (OTP, PAN) and phishing attempts, and applies hardware-accelerated overlays (redaction masks and non-occluding threat banners).
5-Phase Golden Path
- Phase 1: Foundation
- MediaProjection foreground service (
CardinalProjectionService) withforegroundServiceType="mediaProjection". - VirtualDisplay calibrated at
270x600probe intake. ImageReaderbuffer intake with zero-allocation direct byte buffer pool.WindowManagerTYPE_APPLICATION_OVERLAYpassthrough canvas harness.
- MediaProjection foreground service (
- Phase 2: Deterministic Fast-Path
- Bundled offline ML Kit Latin Text Recognition.
- Deterministic Regex matchers for 6-digit OTPs (
\b(?<!\d)\d{6}(?!\d)\b) and Indian PAN identifiers ([A-Z]{5}[0-9]{4}[A-Z]{1}). ProximityAnchorGraphlinking contextual auth anchors ("OTP", "Code", "PAN") to numeric tokens via spatial 2D Euclidean distance.RedactionMaskViewdynamic coordinate black box with 500ms hold-to-reveal safety mechanism.
- Phase 3: Catalytic Risk Engine & Threat Card
FrameAdmissionController64-bit luminance pHash change gate with Hamming distance< 2static frame rejection.- Vivo/iQOO secure keyboard lower 40% screen blackout handling.
- Dark mode contrast inversion LUT for OCR fidelity.
CatalyticPolicyArbitermultiplicative risk formula:compositeScore = clamp(0, 100, floor(BaseScore * AttackMultiplier * TrustModifier))ThreatCardViewnon-occluding bottom banner with 36dp lateral margins andsystemGestureExclusionRects = emptyList().
- Phase 4: On-Device Model & Hardware Acceleration
- ExecuTorch runtime integration targeting Snapdragon Hexagon NPU.
- 3-tier fallback architecture: NPU Hexagon delegate -> Quantized CPU thread -> Deterministic Heuristic Arbiter.
- Phase 5: Test Lab & Airplane Mode Hardening
DemoHarnessActivityoffline test lab with mock fixtures in assets.DeskLensActivitywith CameraX offline scanning.- Zero-network manifest verification test.
- Unit and integration tests covering pHash, admission, regex, proximity graph, policy arbiter, and SHA-256 tamper-evident hash chain.
2. Hardware Invariants & Coding Rules
-
Zero-Egress Invariant:
android.permission.INTERNETandandroid.permission.ACCESS_NETWORK_STATEare strictly forbidden.- Zero network libraries (no OkHttp, Retrofit, Ktor, Volley).
- Cardinal runs completely offline, guaranteed in Airplane Mode.
-
Buffer Lifecycle Management:
- Always acquire and release
Imageobjects insidetry-finallyimmediately (image.close()infinally). - Prevents
ImageReaderqueue starvation and pipeline stalls.
- Always acquire and release
-
Zero Allocations in the Per-Frame Hot Loop:
- No
Bitmap.createBitmap(), newByteArray(), or short-lived allocations insideonImageAvailable()or frame admission. - Direct ByteBuffers (
ByteBuffer.allocateDirect()) and bitmaps are pre-allocated and pooled.
- No
-
System Touch & Gesture Safety:
- Redaction overlays must declare
FLAG_NOT_FOCUSABLEandFLAG_NOT_TOUCH_MODAL. - Passthrough outside bounds: Touch events outside redaction boxes return false to allow normal OS interaction.
- Threat cards enforce 36dp lateral touch corridors for edge back gestures.
- Redaction overlays must declare
-
Coordinate Space Affine Mapping:
- Overlay positions are dynamically scaled from 270x600 probe intake coordinates to device window metrics bounds (
WindowManager.currentWindowMetrics.bounds).
- Overlay positions are dynamically scaled from 270x600 probe intake coordinates to device window metrics bounds (