Back to Blocks

Claude Terminal

Loading...

Task Details
PXL-808Done
High

📱 Track sessions on device with cloud sync

b2-heradatasync

Logs your app usage sessions locally on your device and syncs them to the cloud.


> Technical Details

Summary

Implement a comprehensive session logging system to aid debugging of sync race conditions and other intermittent issues. The system captures all console output to local log files and syncs them to CloudKit for cross-device access.

Problem

Debugging sync race conditions (PXL-801) and other issues is extremely difficult with live logs because:

  • Issues are intermittent and hard to reproduce
  • By the time an issue is noticed, the relevant logs have scrolled away
  • No way to capture logs from real-world usage sessions
  • Can't debug issues that happen when not connected to Xcode
Solution

Create an on-device session logging system that:

  1. Captures all console output to a local log file per app session
  2. Syncs logs to CloudKit public database for cross-device access
  3. Tracks session status: ACTIVETERMINATED or CRASHED
  4. Provides UI in Admin settings for viewing and exporting logs
  5. Auto-cleans old logs (7 days local, 30 days remote)
Key Features

Session Lifecycle

  • Session starts on app launch, ends only on true termination (not backgrounding)
  • Logs background/foreground transitions within the same session
  • Crash detection: missing SESSION END marker = crashed session

CloudKit Sync

  • Uploads every 60 seconds while app is open
  • Uploads immediately on foreground resume
  • Final upload on termination with status TERMINATED
  • On next launch after crash: upload previous session as CRASHED
  • 30-day retention with automatic cleanup

CloudKit Record Fields

  • userId (Supabase user ID)
  • cloudKitUserId (CKRecord.ID)
  • sessionId, filename, logFile (CKAsset)
  • status: "active" / "terminated" / "crashed"
  • startedAt, lastUpdatedAt, endedAt
  • deviceModel, iosVersion, appVersion, fileSizeBytes

UI (Admin → Session Logs)

  • Segment picker: Local / Cloud
  • Current session with "Running" indicator
  • Previous sessions with status badges (ACTIVE/TERMINATED/CRASHED)
  • Device icons (📱 iPhone, 💻 iPad)
  • Log detail view with search and export
  • Delete all logs option
Files to Create
  1. Packages/App/Sources/App/Services/SessionLogger.swift
  2. Packages/App/Sources/App/Services/SessionLogCloudKitManager.swift
  3. NutriKit/Views/Debug/SessionLogsView.swift
  4. NutriKit/Views/Debug/SessionLogDetailView.swift
Files to Modify
  1. NutriKit/NutriKitApp.swift
  2. NutriKit/Views/Debug/AdminSheet.swift
  3. Packages/App/Sources/App/Extensions/Logger.swift
Storage Estimates
  • Per session: 2-4 MB typical
  • Per user (30 days): ~100-200 MB
  • 1 PB capacity: Even 100K users = only 2% usage
Related

Build instruction: Use -destination 'platform=iOS Simulator,name=iPhone 17 Pro' when building this project

Created Dec 30, 2025, 1:34 PM | Updated Feb 6, 2026, 11:46 AM