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: fetch existing records before save to avoid 'already exists' conflict

libokai1c07d0e52ad8
diff --git a/CodePulseShared/Sources/CloudKit/CloudKitManager.swift b/CodePulseShared/Sources/CloudKit/CloudKitManager.swift
index 769b0ae2..723c27cd 100644
--- a/CodePulseShared/Sources/CloudKit/CloudKitManager.swift
+++ b/CodePulseShared/Sources/CloudKit/CloudKitManager.swift
@@ -13,12 +13,30 @@ public final class CloudKitManager: Sendable {
// MARK: - Write (macOS)
- /// Save sessions to CloudKit. Uses individual saves (simple + reliable).
+ /// Save sessions to CloudKit. Fetches existing records first to avoid conflicts.
public func saveBatch(_ sessions: [SessionState]) async throws {
guard !sessions.isEmpty else { return }
+
+ // Fetch existing records to get change tags
+ let recordIDs = sessions.map { CKRecord.ID(recordName: $0.sessionId) }
+ var existing: [String: CKRecord] = [:]
+ if let results = try? await database.records(for: recordIDs) {
+ for (id, result) in results {
+ if case .success(let record) = result {
+ existing[id.recordName] = record
+ }
+ }
+ }
+
var savedCount = 0
for session in sessions {
- let record = CKRecordMapper.toRecord(session)
+ let record: CKRecord
+ if let server = existing[session.sessionId] {
+ CKRecordMapper.updateRecord(server, with: session)
+ record = server
+ } else {
+ record = CKRecordMapper.toRecord(session)
+ }
_ = try await database.save(record)
savedCount += 1
}