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.
Rust acpagent-client-protocolai-agentsai-assistantclaudeclaude-codecodexcoding-agentscursorgeminigemini-cligrok-bot-alternativeiosiphonelinuxmacosmcpopen-sourcerustself-hosted
fix(ios): allow downloads after cancellation
Sean Ferguson78856abf0807
diff --git a/apps/ios/Kit/Sources/CodyncUI/Store/FileDownloads.swift b/apps/ios/Kit/Sources/CodyncUI/Store/FileDownloads.swiftindex 1b88bace..9a035470 100644--- a/apps/ios/Kit/Sources/CodyncUI/Store/FileDownloads.swift+++ b/apps/ios/Kit/Sources/CodyncUI/Store/FileDownloads.swift@@ -2,6 +2,14 @@ import CodyncKitimport Foundationimport Observation+protocol FileDownloadWriting: Sendable {+ func save(client: HostClient, entryId: String, file: SharedFile,+ directory: URL, progress: @Sendable (Int64) async -> Void) async throws -> URL+ func remove(_ url: URL) async throws+}++extension FileDownload: FileDownloadWriting {}+@MainActor @Observablepublic final class FileDownloads {public enum State {@@ -17,10 +25,12 @@ public final class FileDownloads {public var export: Export?@ObservationIgnored private var exported: Export?@ObservationIgnored private var tasks: [String: Task<Void, Never>] = [:]- @ObservationIgnored private let writer = FileDownload()+ @ObservationIgnored private let writer: any FileDownloadWriting@ObservationIgnored private var retired = false- public init() {}+ public init() { writer = FileDownload() }++ init(writer: any FileDownloadWriting) { self.writer = writer }public func save(client: HostClient?, entry: Entry, file: SharedFile) {guard !retired else { return }@@ -32,6 +42,7 @@ public final class FileDownloads {states[file.id] = .downloading(0)let directory = FileManager.default.temporaryDirectory.appendingPathComponent("CodyncDownloads", isDirectory: true)tasks[file.id] = Task {+ defer { tasks[file.id] = nil }do {let url = try await writer.save(client: client, entryId: entry.id, file: file, directory: directory) { [weak self] received inawait self?.update(file.id, received: received)@@ -46,7 +57,6 @@ public final class FileDownloads {} catch {if !Task.isCancelled, !retired { states[file.id] = .failed(error.localizedDescription) }}- tasks[file.id] = nil}}diff --git a/apps/ios/Kit/Tests/CodyncUITests/FileDownloadLifecycleTests.swift b/apps/ios/Kit/Tests/CodyncUITests/FileDownloadLifecycleTests.swiftnew file mode 100644index 00000000..040563e1--- /dev/null+++ b/apps/ios/Kit/Tests/CodyncUITests/FileDownloadLifecycleTests.swift@@ -0,0 +1,155 @@+import Foundation+import Testing+@testable import CodyncKit+@testable import CodyncUI++/// Pauses after the real writer has verified and moved a completed file.+private actor ControlledDownloadWriter: FileDownloadWriting {+ private let writer = FileDownload()+ private let directory: URL+ private var pauseCompletedFile: Bool+ private var completion: CheckedContinuation<Void, Never>?+ private(set) var pausedURL: URL?++ init(directory: URL, pauseCompletedFile: Bool) {+ self.directory = directory+ self.pauseCompletedFile = pauseCompletedFile+ }++ func save(client: HostClient, entryId: String, file: SharedFile,+ directory: URL, progress: @Sendable (Int64) async -> Void) async throws -> URL {+ let url = try await writer.save(client: client, entryId: entryId, file: file,+ directory: self.directory, progress: progress)+ if pauseCompletedFile {+ pauseCompletedFile = false+ pausedURL = url+ await withCheckedContinuation { completion = $0 }+ }+ return url+ }++ func releaseCompletedFile() { completion?.resume(); completion = nil }+ func remove(_ url: URL) async throws { try await writer.remove(url) }+}++private actor ControlledFileRead: HostTransport {+ private var delayFirstRead: Bool+ private var pending: CheckedContinuation<Data, any Error>?+ var readPending: Bool { pending != nil }++ init(delayFirstRead: Bool = false) { self.delayFirstRead = delayFirstRead }++ func call(_ method: String, body: Data, timeout: TimeInterval) async throws -> Data {+ #expect(method == "readFile")+ if delayFirstRead {+ delayFirstRead = false+ return try await withCheckedThrowingContinuation { pending = $0 }+ }+ return Self.emptyChunk+ }++ func releaseRead() { pending?.resume(returning: Self.emptyChunk); pending = nil }+ private static var emptyChunk: Data { Data(#"{"data":"","size":0}"#.utf8) }+ nonisolated func stream(_ request: HostStreamRequest) -> AsyncThrowingStream<Data, Error> { AsyncThrowingStream { $0.finish() } }+ nonisolated func states() -> AsyncStream<LinkState> { AsyncStream { $0.finish() } }+}++private func emptyFile(_ id: String) -> SharedFile {+ SharedFile(id: id, name: "empty", size: 0,+ sha256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")+}++private func fileEntry() -> Entry {+ Entry(id: "entry", seq: 1, botId: "bot", rev: 1, kind: "agent", turn: 1,+ data: EntryData(), createdAt: 0, updatedAt: 0)+}++@MainActor+private func downloadEventually(_ condition: @MainActor () async -> Bool) async -> Bool {+ for _ in 0..<200 {+ let ready = await condition()+ if ready { return true }+ try? await Task.sleep(for: .milliseconds(10))+ }+ return false+}++@MainActor @Test func cancellingCompletedFileDiscardsItAndAllowsAnotherExport() async throws {+ let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)+ defer { try? FileManager.default.removeItem(at: directory) }+ let writer = ControlledDownloadWriter(directory: directory, pauseCompletedFile: true)+ let downloads = FileDownloads(writer: writer)+ defer { downloads.retire() }+ let client = HostClient(transport: ControlledFileRead())+ let entry = fileEntry()+ let first = emptyFile("cancelled")+ downloads.save(client: client, entry: entry, file: first)+ let completed = await downloadEventually {+ let url = await writer.pausedURL+ return url != nil+ }+ try #require(completed)+ let completedURL = await writer.pausedURL+ let discardedURL = try #require(completedURL)+ #expect(FileManager.default.fileExists(atPath: discardedURL.path))++ downloads.cancel(first.id)+ await writer.releaseCompletedFile()+ let discarded = await downloadEventually { !FileManager.default.fileExists(atPath: discardedURL.path) }+ try #require(discarded)+ #expect(downloads.states[first.id] == nil)+ #expect(downloads.export == nil)++ let next = emptyFile("next")+ // Retry through the public action while asynchronous cancellation settles.+ let exported = await downloadEventually {+ downloads.save(client: client, entry: entry, file: next)+ return downloads.export?.id == next.id+ }+ try #require(exported)+ let item = try #require(downloads.export)+ #expect(try Data(contentsOf: item.url).isEmpty)+ #expect(item.botId == entry.botId)+ downloads.dismissExport()+ let removed = await downloadEventually { !FileManager.default.fileExists(atPath: item.url.path) }+ #expect(removed)+}++@MainActor @Test func cancellingOutstandingFileReadAllowsRetryAndExport() async throws {+ let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)+ defer { try? FileManager.default.removeItem(at: directory) }+ let writer = ControlledDownloadWriter(directory: directory, pauseCompletedFile: false)+ let downloads = FileDownloads(writer: writer)+ defer { downloads.retire() }+ let transport = ControlledFileRead(delayFirstRead: true)+ let client = HostClient(transport: transport)+ let entry = fileEntry()+ let file = emptyFile("retry")+ downloads.save(client: client, entry: entry, file: file)+ let pending = await downloadEventually {+ let pending = await transport.readPending+ return pending+ }+ try #require(pending)++ downloads.cancel(file.id)+ await transport.releaseRead()+ let cancelled = await downloadEventually {+ let folders = try? FileManager.default.contentsOfDirectory(atPath: directory.path)+ return folders?.isEmpty == true+ }+ try #require(cancelled)+ #expect(downloads.export == nil)+ #expect(downloads.states[file.id] == nil)++ let exported = await downloadEventually {+ downloads.save(client: client, entry: entry, file: file)+ return downloads.export?.id == file.id+ }+ try #require(exported)+ let item = try #require(downloads.export)+ #expect(try Data(contentsOf: item.url).isEmpty)+ downloads.dismissExport()+ let removed = await downloadEventually { !FileManager.default.fileExists(atPath: item.url.path) }+ #expect(removed)+}