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: respect CloudKit quota backoff timer to allow recovery

libokai513b37a13f3a
diff --git a/CodePulse-macOS/Services/CloudKitSync.swift b/CodePulse-macOS/Services/CloudKitSync.swift
index f44a44e0..7ce4acc7 100644
--- a/CodePulse-macOS/Services/CloudKitSync.swift
+++ b/CodePulse-macOS/Services/CloudKitSync.swift
@@ -1,5 +1,6 @@
import Foundation
import Combine
+import CloudKit
import CodePulseShared
import os
@@ -11,6 +12,7 @@ final class CloudKitSync {
private var cancellables = Set<AnyCancellable>()
private var previousStates: [String: SessionState] = [:]
private var isSyncing = false
+ private var quotaBackoffUntil: Date?
init(stateManager: SessionStateManager) {
self.stateManager = stateManager
@@ -21,50 +23,26 @@ final class CloudKitSync {
}
.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 { return }
- guard !isSyncing else {
- Self.debugLog("Sync skipped — already in progress")
+ // Respect quota backoff
+ if let backoff = quotaBackoffUntil, Date() < backoff {
return
}
+ quotaBackoffUntil = nil
- // Filter to only changed active sessions (skip completed to save quota)
let changed = sessions.filter { session in
session.status != .completed
&& previousStates[session.sessionId]?.updatedAt != session.updatedAt
}
- guard !changed.isEmpty else {
- Self.debugLog("Sync skipped — no changes (\(sessions.count) sessions, \(previousStates.count) prev)")
- return
- }
+ guard !changed.isEmpty else { return }
isSyncing = true
logger.info("Syncing \(changed.count) changed sessions to CloudKit...")
- Self.debugLog("Starting sync of \(changed.count) sessions")
Task {
defer { isSyncing = false }
@@ -73,22 +51,21 @@ 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
- // 3s delay between saves to avoid quota exhaustion
try? await Task.sleep(for: .seconds(3))
+ } catch let error as CKError where error.code == .quotaExceeded || error.code == .requestRateLimited {
+ let retryAfter = error.retryAfterSeconds ?? 300
+ logger.warning("CloudKit quota hit, backing off \(retryAfter)s")
+ quotaBackoffUntil = Date().addingTimeInterval(retryAfter)
+ break
} catch {
- Self.debugLog("ERROR saving \(session.sessionId.prefix(8)): \(error)")
- // On quota exceeded, stop trying more sessions this cycle
+ logger.error("Sync failed for \(session.sessionId): \(error.localizedDescription)")
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?)")
}
}
}