# HealthProbe Implementation Status

## Overview

HealthProbe's comprehensive snapshot + delta system has been implemented according to the detailed plan. The project builds successfully with no compilation errors.

## Completed Components (100%)

### Models (Step 1-3)
✅ **SnapshotQuality.swift** — All quality states (complete, partial, unauthorized, loading, failed)
✅ **AnomalyType.swift** — All anomaly types + Severity + TypeTransition + TypeDeltaReason enums
✅ **HealthSnapshot.swift** — Chain metadata, quality, trigger context, registry fingerprinting, timezone context
✅ **TypeCount.swift** — Count with hash, date range, quality, yearly counts with cascade relationship
✅ **SnapshotDelta.swift** — Local delta with checksums and cascade relationship to TypeDeltas
✅ **TypeDelta.swift** — Per-type local delta with transition, reason, quality before/after, yearly count note
✅ **AnomalyRecord.swift** — Anomaly record with deltaID set structurally by detector, never by caller
✅ **OperationLog.swift** — Audit trail for destructive operations with JSON-encoded affected snapshot IDs
✅ **YearlyCount.swift** — Per-year sample counts with approximation flag

### Services (Step 4-12)

#### Step 5: HashService ✅
- `typeHash()` — SHA256 of typeIdentifier|count|earliest|latest (ISO8601 with fractional seconds)
- `snapshotChecksum()` — Filters on quality==.complete (not hash!=""), concatenates type hashes
- `typeSetHash()` — SHA256 of sorted active typeIdentifiers (covers full intended registry)

#### Step 11 & 11b: HealthKitService & ObserverService ✅
- Per-type fetch with **15-second combined timeout** (distribution + earliestDate + latestDate)
- Concurrency capped at 6 simultaneous type fetches (prevents HealthKit resource exhaustion)
- Per-type quality detection (unauthorized, failed, complete)
- Real earliestDate/latestDate from separate HKSampleQuery (NOT from bin boundaries)
- YearlyCount population from distribution bins with isApproximate flag
- Snapshot quality aggregation (loading > unauthorized > partial > complete)
- Chain metadata set before save (previousSnapshotID, isChainStart, monitoredTypeSetHash)
- Auto-detect post-restore (full deny → complete transition, or chain start > 1000 records)
- **Post-save pipeline**: DeltaService → AnomalyDetector → OperationLog
- **ObserverService**: debounce (10 min), manual overlap suppression, all-monitored-types snapshot
- **Background delivery**: .immediate for heart rate/steps, .daily for others

#### Step 7: DeltaService ✅
- Computes and saves SnapshotDelta with TypeDeltas
- **Reason assignment with priority**: authorizationChanged > unsupported > registryChanged > unknown > normal
- **Unavailable count guard**: if either quality != .complete, countDelta = 0 (never from -1)
- **YearlyCount timezone guard**: if timezone changes, set countDelta = 0 and yearlyCountNote
- **Delta merge** (for intermediate deletion):
  - Recomputes checksums from surrounding snapshots (never carries old checksums)
  - Handles disappeared→appeared transition (remove from merged delta if type existed only in deleted snapshot)
  - Applies unavailable count guard and reason priority to merged result
  - Sets timezone note if either source had it

#### Step 8: AnomalyDetector ✅
- **Pure function**: no context mutation, receives TypeCount maps, returns DetectionResult
- **Quality gate**: both snapshots must be .complete (suppresses ALL detection including first auth after full deny)
- **Registry gate**: skips appeared/disappeared anomalies if reason != .normal
- **count = -1 guard**: skips any TypeDelta with qualityBefore or qualityAfter != .complete
- **Anomaly detection rules**:
  - `historicalInsertion` — countDelta > 0 AND (earlier earliest date OR recent latest with increased count)
  - `deletion` — countDelta < 0 (severity based on % loss)
  - `duplication` — countDelta > 50% AND date ranges within 1 day
  - `silentReplacement` — countDelta == 0 AND hash differs (best-effort, MVP limitation)
  - `syncAnomaly` — ≥4 types with |delta| > 10% (critical severity)
- **isPostRestore suppression**:
  - Suppresses syncAnomaly if previous.isPostRestore && previous.isPostRestoreSuppressedDeltaID == nil
  - Suppression token consumed via DetectionResult, persisted by HealthKitService
  - Forwarded past low-quality successors (quality gate prevents consumption on incomplete snapshots)
- **AnomalyRecord.deltaID**: set internally, structural guarantee (impossible to return record without deltaID)

#### Step 4: KeychainService ✅
- Stable device ID persisted in Keychain (service: "ro.xdev.healthprobe.deviceid", account: "stable_device_id")
- Detects DB reset: swiftDataStoreIsEmpty + existing keychain ID → recoveredDeviceID = true
- In-process cache for repeated lookups

#### Step 6 & 9: IntegrityService & Quality Aggregation ✅
- `validate()` — strict mode:
  - Recomputes checksum from TypeCounts
  - Compares with delta.checksumAfter
  - Returns .valid or .checksumMismatch / .missingDelta / .corrupted
- `validateChain()` — walk backwards from latest via previousSnapshotID:
  - **Fork detection**: asserts no duplicate previousSnapshotID (returns .corrupted immediately)
  - Stops at first mismatch (no auto-repair, no skips)
- **Quality aggregation**: loading > unauthorized (only if ALL) > partial (any failed/unauthorized) > complete

#### Step 10: SnapshotLifecycleService ✅
- `previewDeletion()` — advisory integrity check, surfaces willBreakChain warning to UI
- `delete()` — handles all position cases (oldest, latest, intermediate):
  - **Oldest**: set next as chain start
  - **Latest**: just delete
  - **Intermediate**: merge deltas, recompute checksums, update nextSnapshot.previousSnapshotID
- **OperationLog**: always written atomically with deletive changes
- **Post-save verification**: re-fetches log by ID, recovery re-insert if missing, logs critical error

#### Step 12: Local-only storage refactor ✅
- Removed CloudKitSyncService and CloudKit-pending chain states
- **ModelContainer split**:
  - uiCacheConfig: HealthSnapshot, TypeCount, YearlyCount, SnapshotDelta, TypeDelta, AnomalyRecord (derived local UI/index data)
  - localConfig: OperationLog, DeviceProfile, MetricTimeoutProfile (local-only settings and operation metadata)
- Added `HealthArchiveStore` protocol for the single local archive store source of truth
- Added `SQLiteHealthArchiveStore`: actor-isolated SQLite archive with WAL, per-sample upsert, disappearance marking, verification timestamps, semantic fingerprints, metadata JSON, and scoped JSON report export
- HealthKit anchored-query pages now archive samples/deletions before SwiftData snapshot/index rows are built
- Schema migration recovery: removes legacy SwiftData stores and retries once on failure

### UI (Step 13)

✅ **SnapshotRow** — Shows:
  - Chain indicators: "Chain start" / "DB reset / recovered device ID" / "Post-restore baseline" / "Observer-triggered snapshot"
  - Anomaly warning badge (exclamationmark.triangle) if anomalyFlags non-empty
  - Incomplete snapshot warning if quality != .complete

✅ **SnapshotTypeCountRow** — Shows:
  - "Unsupported" for isUnsupported = true (read directly, no delta needed)
  - "Unavailable" for count = -1
  - Numeric count with warning color if quality != .complete
  - Delta badge vs. baseline (green/amber)

✅ **DashboardView** — Anomaly summary section:
  - Counts unresolved anomalies by severity (critical/warning)
  - Shows only if unresolved anomalies exist

✅ **Full feature coverage**:
  - Snapshot creation with observer triggers
  - Chain visualization and deletion with integrity warnings
  - Quality badges and anomaly indicators
  - Timezone/registry change awareness
  - Baseline comparison across multiple devices

## Build Status

```
✅ BUILD SUCCEEDED
  Target: HealthProbe (iOS 26.4)
  No compilation errors or warnings
  App signs successfully
```

## Verification Checklist (32 items from plan)

These tests should be run to ensure all backend functionality is correct:

### Basic Snapshot & Chain (1-3)
- [ ] 1. Build succeeds with no errors
- [ ] 2. First snapshot: isChainStart=true, previousSnapshotID=nil, no delta created
- [ ] 3. Second snapshot: SnapshotDelta created with correct checksumBefore/After

### Quality & Anomalies (4-7)
- [ ] 4. Revoke permission → type quality=.unauthorized, snapshot=.partial, no anomalies
- [ ] 5. All permissions revoked → snapshot=.unauthorized, no anomalies  
- [ ] 6. Timeout simulation (1ms) → count=-1, quality=.failed, "Unavailable" in UI
- [ ] 7. Post-authorize after full deny → first delta suppressed, snapshot marked post-restore

### Chain Operations (8-10)
- [ ] 8. 3 snapshots A→B→C, delete B → single merged delta A→C, C.previousSnapshotID==A.id
- [ ] 9. Hash stability → no changes between snapshots = identical hashes/checksums
- [ ] 10. Integrity strict mode → corrupted checksum = validation stops, no auto-repair

### Advanced Features (11-20)
- [ ] 11. DB reset with Keychain survival → same deviceID, isChainStart=true, recoveredDeviceID=true
- [ ] 12. Local-only launch → app functions without iCloud/CloudKit entitlements
- [ ] 13. Observer debounce → 10 rapid callbacks = exactly 1 snapshot (triggerReason=observerCallback)
- [ ] 14. Unsupported type → TypeCount(count=-1, quality=.failed, isUnsupported=true), "Unsupported" UI
- [ ] 15. YearlyCount timezone → Calendar.current used, isApproximate=true if bucket > day
- [ ] 16. Delta merge with unavailable counts → merged countDelta=0, impaired reason preserved
- [ ] 17. Missing local delta/typeDeltas → integrity validation surfaces the fault, never hides it as sync latency
- [ ] 18. First auth after full deny (quality gate) → no anomalies, current.isPostRestore=true, isPostRestoreInferred=true
- [ ] 19. Chain fork → validateChain() returns .corrupted(reason: "chain fork detected"), stops
- [ ] 20. disappeared→appeared merge with -1 source → merged countDelta=0, reason != .normal

### Reason Priority & Suppression (21-26)
- [ ] 21. TypeDelta reason priority → .unauthorized wins over .registryChanged simultaneously
- [ ] 22. Debounce + manual overlap → no observer snapshot if manual created during debounce
- [ ] 23. completionHandler unconditional → called via defer, never gated on scheduling success
- [ ] 24. isPostRestore forwarding → suppression forwarded past low-quality, consumed on next .complete
- [ ] 25. Missing delta → validateChain() returns .missingDelta and stops
- [ ] 26. OperationLog verification → recovery re-insert if missing after save, log critical error

### Coherence & Edge Cases (27-32)
- [ ] 27. Per-type query concurrency → max 6 simultaneous HK queries (not 3N at N=20)
- [ ] 28. YearlyCount timezone drift → countDelta=0, yearlyCountNote set, no anomalies
- [ ] 29. isUnsupported on TypeCount → UI shows "Unsupported" without delta context
- [ ] 30. count/quality coherence assert → debug assert fires, release corrects to -1
- [ ] 31. snapshotChecksum filter → uses quality==.complete, not hash!="" (determinism)
- [ ] 32. AnomalyRecord.deltaID structural → every record has deltaID==delta.id (no external setter)

## Architectural Highlights

### Purity & Immutability
- **AnomalyDetector** is pure: no SwiftData mutations, explicit TypeCount maps, DetectionResult metadata
- **DeltaService** never carries old checksums during merge (recomputes from surrounding snapshots)
- **OperationLog** atomicity: log + destructive changes in single context.save()

### Quality Gates
- **Snapshot quality** aggregation prevents false positives:
  - All detection requires both snapshots .complete
  - Covers first authorization after full deny (quality gate alone is complete suppression)
  - isPostRestore suppression forwarded past low-quality successors

### Chain Integrity
- **previousSnapshotID** is the sole source of chain truth (not localSequenceNumber)
- **Fork detection** prevents chain divergence (asserts no duplicate previousSnapshotID)
- **Checksum validation** ensures data wasn't corrupted between snapshots

### Local Archive Direction
- CloudKit/iCloud sync is not a product goal
- SwiftData rows are derived UI/index data and must be rebuildable from the local archive store
- Missing deltas or type deltas are treated as local integrity faults, not remote sync latency

### Observability
- **Reason priority** makes anomaly suppression deterministic
  - authorizationChanged > unsupported > registryChanged > unknown > normal
  - Prevents .registryChanged from masking .authorizationChanged
- **YearlyCount timezone guard** prevents false loss attribution across DST
- **TypeDelta.yearlyCountNote** signals unreliable year-level attribution

## Known Limitations (MVP)

1. **Hash** covers only count + date range, not distribution (silentReplacement is best-effort)
2. **YearlyCount** precision requires daily bucket granularity (noted if isApproximate)
3. **Archive query/report UI is still pending** (store exists, UI still mostly reads SwiftData cache)
4. **No automatic cross-device reconstruction**; cross-device analysis is future macOS/report work

## Next Steps

### Immediate (Testing)
1. Run all 32 verification checks against real HealthKit data
2. Create unit tests for delta merge, reason priority, anomaly detection
3. Test observer callback debounce with real HKObserverQuery
4. Add archive status/report UI backed by `HealthArchiveStore`

### Post-MVP
1. Integrate actual BGTask expiration guard for observer snapshots (capture partial results)
2. Add delta comparison view showing TypeDelta reason and suppression explanations
3. Implement OperationLog viewer in UI (audit trail dashboard)
4. Add historical trend analysis (divergence detection, anomaly patterns)

---

**Built with:** SwiftUI, SwiftData, HealthKit, CryptoKit  
**Minimum iOS:** 17.0  
**Target iOS:** 26.4  
**Swift Version:** 5.9+
