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(ios): keep unsent messages that can't move into the account at sign-in

leepokai26a9bd5163c3
diff --git a/apps/ios/Kit/Sources/CodyncKit/Client/SharedStore.swift b/apps/ios/Kit/Sources/CodyncKit/Client/SharedStore.swift
index b35b0f34..03570036 100644
--- a/apps/ios/Kit/Sources/CodyncKit/Client/SharedStore.swift
+++ b/apps/ios/Kit/Sources/CodyncKit/Client/SharedStore.swift
@@ -111,12 +111,15 @@ public enum SharedStore {
}
/// Signing out or removing the account: device keys, computers, widget snapshot and caches go.
- public func erase() {
+ public func erase() { erase(keeping: []) }
+
+ /// `erase()`, except for the mirror caches in `kept` (`adopt(from:)`).
+ fileprivate func erase(keeping kept: [URL]) {
for name in Self.names { defaults.removeObject(forKey: key(name)) }
DeviceIdentity.delete(context: self)
do { try ActivityIconCache.erase(context: id) }
catch { Logger(subsystem: "com.pokai.Codync", category: "CustomIcon").error("Cannot erase activity icons: \(error.localizedDescription)") }
- for file in mirrorCaches() { try? FileManager.default.removeItem(at: file) }
+ for file in mirrorCaches() where !kept.contains(file) { try? FileManager.default.removeItem(at: file) }
}
/// This context's mirror caches (`BotStore.saveCache`): `codync-mirror-<app>-<id>-<computer>.json`.
@@ -146,7 +149,8 @@ public extension SharedStore.Context {
guard accountID != nil, local.accountID == nil, local.suite == suite else { return nil }
let moving = local.computers
guard !moving.isEmpty || DeviceIdentity.exists(context: local) else { return nil }
- defer { local.erase() }
+ var unmoved: [URL] = []
+ defer { local.erase(keeping: unmoved) }
if !DeviceIdentity.exists(context: self), computers.isEmpty {
do {
try DeviceIdentity.move(from: local, to: self)
@@ -155,7 +159,7 @@ public extension SharedStore.Context {
return merge(moving)
}
for name in Self.names { defaults.set(local.defaults.object(forKey: local.key(name)), forKey: key(name)) }
- moveMirrorCaches(from: local)
+ unmoved = moveMirrorCaches(from: local)
return Adoption(computers: moving.count, keys: .moved)
}
return merge(moving)
@@ -163,7 +167,10 @@ public extension SharedStore.Context {
/// Unsent and failed messages live only in the mirror caches: they come along rather than go with `erase()`.
/// A rename in one folder: only a cache left at the account's name (it has no computers yet) is in the way.
- private func moveMirrorCaches(from local: Self) {
+ /// Returns the caches that couldn't move: `erase` leaves them on disk rather than delete the only copy, though
+ /// nothing loads them again (Start over removes them).
+ private func moveMirrorCaches(from local: Self) -> [URL] {
+ var unmoved: [URL] = []
for file in local.mirrorCaches() {
let name = file.lastPathComponent
guard let marker = name.range(of: "-\(local.id)-") else { continue }
@@ -173,8 +180,10 @@ public extension SharedStore.Context {
try FileManager.default.moveItem(at: file, to: moved)
} catch {
log.error("Cannot move unsent messages: \(error.localizedDescription)")
+ unmoved.append(file)
}
}
+ return unmoved
}
private func merge(_ moving: [Computer]) -> Adoption {
diff --git a/apps/ios/Kit/Tests/CodyncKitTests/SignedOutAdoptionTests.swift b/apps/ios/Kit/Tests/CodyncKitTests/SignedOutAdoptionTests.swift
index 20615726..c9c25295 100644
--- a/apps/ios/Kit/Tests/CodyncKitTests/SignedOutAdoptionTests.swift
+++ b/apps/ios/Kit/Tests/CodyncKitTests/SignedOutAdoptionTests.swift
@@ -62,4 +62,27 @@ import Testing
#expect(account.adopt(from: local) == nil)
#expect(local.adopt(from: account) == nil, "only into an account context")
}
+
+ @Test func aCacheThatCannotMoveStays() throws {
+ let (local, account, suite) = contexts()
+ defer { account.erase(); UserDefaults(suiteName: suite)?.removePersistentDomain(forName: suite) }
+ _ = try DeviceIdentity.load(context: local)
+ local.computers = [v.computer]
+ let cache = URL.cachesDirectory.appending(path: "codync-mirror-tests-\(local.id)-\(v.computer.id).json")
+ try Data("unsent".utf8).write(to: cache)
+ defer { try? FileManager.default.removeItem(at: cache) }
+ // A folder that can't be emptied sits at the account's name, so the rename fails.
+ let blocked = URL.cachesDirectory.appending(path: "codync-mirror-tests-\(account.id)-\(v.computer.id).json")
+ try FileManager.default.createDirectory(at: blocked, withIntermediateDirectories: true)
+ try Data().write(to: blocked.appending(path: "inner"))
+ try FileManager.default.setAttributes([.posixPermissions: 0o555], ofItemAtPath: blocked.path())
+ defer {
+ try? FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: blocked.path())
+ try? FileManager.default.removeItem(at: blocked)
+ }
+
+ #expect(account.adopt(from: local) == .init(computers: 1, keys: .moved))
+ #expect(try Data(contentsOf: cache) == Data("unsent".utf8), "the only copy of those messages isn't deleted")
+ #expect(local.computers.isEmpty && !DeviceIdentity.exists(context: local))
+ }
}
diff --git a/docs/plans/require-sign-in.md b/docs/plans/require-sign-in.md
index 250b0ad9..68e7f46f 100644
--- a/docs/plans/require-sign-in.md
+++ b/docs/plans/require-sign-in.md
@@ -97,13 +97,13 @@ Runs on iPhone and Android when an account becomes active and the signed-out con
| **New** (no device key, no computers) | Everything moves: the device and push keys, the computers, order, drafts, shortcut lead, widget snapshot. Hosts see the same key, so nothing is paired again and push keeps working |
| **Already used** (has its own key) | The computer lists merge (the account's entry wins for the same id). Computers paired only with the signed-out key are refused by their hosts; they show the existing "pair again" state (`isStale`) |
-After either case the signed-out context is erased, and when computers moved an analytics event `signed_out_pairings_moved {computers, keys: moved|rescan}` is sent (in [analytics events](../reference/analytics-events.md) and both apps).
+After either case the signed-out context is erased (on iOS except a mirror cache that couldn't be renamed, below), and when computers moved an analytics event `signed_out_pairings_moved {computers, keys: moved|rescan}` is sent (in [analytics events](../reference/analytics-events.md) and both apps).
**iOS**
- `SharedStore.Context.adopt(from:)` in the Kit:
- Moves the Keychain items `com.pokai.Codync.device-key` / `push-key` from account `local` to the context id.
- Moves the `UserDefaults` names in `Context.names` to their `account.<id>.` keys.
- - Drops `ActivityIconCache`, which rebuilds. When the keys move, the signed-out mirror caches are renamed into the account's, since they hold the messages not yet sent; otherwise they go.
+ - Drops `ActivityIconCache`, which rebuilds. When the keys move, the signed-out mirror caches are renamed into the account's, since they hold the messages not yet sent; otherwise they go. A cache whose rename fails stays on disk under the signed-out name rather than be deleted, though nothing loads it again (Start over removes it).
- The Kit's test bundle has no Keychain entitlement, so its tests keep keys in memory (`Keychain.memory`, debug builds only).
- Unit tests in `CodyncKitTests` cover both cases.
@@ -111,7 +111,7 @@ After either case the signed-out context is erased, and when computers moved an
- `LocalStore.adopt(from)`:
- The identity is sealed with a per-scope Keystore alias and AAD, so the seeds are decrypted with the old key and sealed again under the new scope.
- Every other file in the folder is copied. Drafts and queued messages name their attachments by full path, so after the copy they're pointed at the new folder's `outgoing/` (`AttachmentFiles.moved`, `DraftBook.relocate`, `MirrorDatabase.relocate`).
- - In the merge case only `computers.json` merges.
+ - In the merge case only `computers.json` merges. A key or file that fails to copy makes it the merge case.
- Unit tests cover both cases.
### 5. Desktop: computer in another account