Skip to content

Open source Grok Bot alternative, Bring your own agent and subscription: message Claude Code, Codex, Gemini CLI, Cursor, Copilot and 40+ AI agents as bots on your own computer, from iPhone, Mac, Windows, Linux or terminal. Agent-agnostic and free. Also a Muse and Dots alternative. Built in Rust.

fix: CloudKit sync — throttle 30s, break on quota, remove retry blocking

libokai9c59cf2b08d3
diff --git a/CodePulse-macOS/Services/CloudKitSync.swift b/CodePulse-macOS/Services/CloudKitSync.swift
index 6bb40235..f44a44e0 100644
--- a/CodePulse-macOS/Services/CloudKitSync.swift
+++ b/CodePulse-macOS/Services/CloudKitSync.swift
@@ -15,32 +15,56 @@ final class CloudKitSync {
init(stateManager: SessionStateManager) {
self.stateManager = stateManager
stateManager.$sessions
- .debounce(for: .seconds(5), scheduler: RunLoop.main)
+ .throttle(for: .seconds(30), scheduler: RunLoop.main, latest: true)
.sink { [weak self] sessions in
self?.syncToCloud(sessions)
}
.store(in: &cancellables)
logger.info("CloudKitSync initialized")
+ Self.debugLog("CloudKitSync initialized, subscribing to sessions publisher")
+ // Also add a direct observation to debug
+ stateManager.$sessions
+ .sink { sessions in
+ Self.debugLog("Publisher emitted \(sessions.count) sessions")
+ }
+ .store(in: &cancellables)
+ }
+
+ private static func debugLog(_ msg: String) {
+ let url = FileManager.default.homeDirectoryForCurrentUser
+ .appendingPathComponent(".codepulse/cloudkit-debug.log")
+ let line = "[\(Date())] \(msg)\n"
+ if let handle = try? FileHandle(forWritingTo: url) {
+ handle.seekToEndOfFile()
+ handle.write(line.data(using: .utf8)!)
+ try? handle.close()
+ } else {
+ try? line.write(to: url, atomically: false, encoding: .utf8)
+ }
}
private func syncToCloud(_ sessions: [SessionState]) {
+ Self.debugLog("syncToCloud called with \(sessions.count) sessions")
+
guard !isSyncing else {
- logger.debug("Sync skipped — already in progress")
+ Self.debugLog("Sync skipped — already in progress")
return
}
- // Filter to only changed sessions
+ // Filter to only changed active sessions (skip completed to save quota)
let changed = sessions.filter { session in
- previousStates[session.sessionId]?.updatedAt != session.updatedAt
+ session.status != .completed
+ && previousStates[session.sessionId]?.updatedAt != session.updatedAt
}
guard !changed.isEmpty else {
- logger.debug("Sync skipped — no changes (\(sessions.count) sessions unchanged)")
+ Self.debugLog("Sync skipped — no changes (\(sessions.count) sessions, \(previousStates.count) prev)")
return
}
isSyncing = true
logger.info("Syncing \(changed.count) changed sessions to CloudKit...")
+ Self.debugLog("Starting sync of \(changed.count) sessions")
Task {
defer { isSyncing = false }
@@ -49,16 +73,22 @@ final class CloudKitSync {
for session in changed {
do {
try await CloudKitManager.shared.save(session)
+ Self.debugLog("Saved \(session.sessionId.prefix(8)) OK")
previousStates[session.sessionId] = session
syncedCount += 1
- // Small delay between saves to avoid rate limiting
- try? await Task.sleep(for: .seconds(1))
+ // 3s delay between saves to avoid quota exhaustion
+ try? await Task.sleep(for: .seconds(3))
} catch {
- logger.error("Sync failed for \(session.sessionId): \(error.localizedDescription)")
+ Self.debugLog("ERROR saving \(session.sessionId.prefix(8)): \(error)")
+ // On quota exceeded, stop trying more sessions this cycle
+ break
}
}
if syncedCount > 0 {
logger.info("Synced \(syncedCount)/\(changed.count) sessions to CloudKit")
+ Self.debugLog("Synced \(syncedCount)/\(changed.count) sessions OK")
+ } else {
+ Self.debugLog("No sessions synced (all failed?)")
}
}
}
diff --git a/CodePulseShared/Sources/CloudKit/CloudKitManager.swift b/CodePulseShared/Sources/CloudKit/CloudKitManager.swift
index 1ceeaabd..cd51e064 100644
--- a/CodePulseShared/Sources/CloudKit/CloudKitManager.swift
+++ b/CodePulseShared/Sources/CloudKit/CloudKitManager.swift
@@ -7,7 +7,7 @@ private let logger = Logger(subsystem: "com.pokai.CodePulse", category: "CloudKi
public final class CloudKitManager: Sendable {
public static let shared = CloudKitManager()
private let container = CKContainer(identifier: "iCloud.com.pokai.CodePulse")
- private var database: CKDatabase { container.privateCloudDatabase }
+ public var database: CKDatabase { container.privateCloudDatabase }
private init() {}
@@ -15,9 +15,7 @@ public final class CloudKitManager: Sendable {
public func save(_ session: SessionState) async throws {
let record = CKRecordMapper.toRecord(session)
- try await retryOnQuotaExceeded {
- _ = try await self.database.save(record)
- }
+ _ = try await database.save(record)
logger.debug("Saved session \(session.sessionId) to CloudKit")
}