Skip to content

Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more

fix(history): keep readable chats and file transcripts when one entry fails to decode (#1066)

postoso9fc889596eef
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 07c9ba7b..1e7b1769 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -94,6 +94,9 @@ jobs:
- name: Run release highlights regressions
run: sh Tests/run_release_highlights_tests.sh
+ - name: Run history decode recovery regressions
+ run: sh Tests/run_history_decode_recovery_tests.sh
+
- name: Run settings backup concurrency regressions
run: python3 Tests/run_settings_backup_concurrency_tests.py
diff --git a/Sources/Fluid/Persistence/ChatHistoryStore.swift b/Sources/Fluid/Persistence/ChatHistoryStore.swift
index 83f00eeb..2fd63d39 100644
--- a/Sources/Fluid/Persistence/ChatHistoryStore.swift
+++ b/Sources/Fluid/Persistence/ChatHistoryStore.swift
@@ -131,7 +131,7 @@ final class ChatHistoryStore: ObservableObject {
static let shared = ChatHistoryStore()
static let maxArchivedChats = 30
- private let defaults = UserDefaults.standard
+ private let defaults: UserDefaults
private let maxChats = 30
private enum Keys {
@@ -142,7 +142,8 @@ final class ChatHistoryStore: ObservableObject {
@Published private(set) var sessions: [ChatSession] = []
@Published var currentChatID: String?
- private init() {
+ init(defaults: UserDefaults = .standard) {
+ self.defaults = defaults
self.loadSessions()
// Archived sessions remain saved, but cannot become the active conversation on launch.
@@ -307,13 +308,21 @@ final class ChatHistoryStore: ObservableObject {
// MARK: - Private Methods
private func loadSessions() {
+ // Skip a chat this build cannot read instead of emptying the list;
+ // the launch save in init would persist an empty list.
guard let data = defaults.data(forKey: Keys.chatSessions),
- let decoded = try? JSONDecoder().decode([ChatSession].self, from: data)
+ let decoded = PersistedHistory.decode(ChatSession.self, from: data)
else {
self.sessions = []
return
}
- self.sessions = decoded
+ self.sessions = decoded.entries
+ if decoded.skipped > 0 {
+ DebugLogger.shared.info(
+ "Skipped \(decoded.skipped) unreadable chat sessions",
+ source: "ChatHistoryStore"
+ )
+ }
// Load current chat ID
self.currentChatID = self.defaults.string(forKey: Keys.currentChatID)
diff --git a/Sources/Fluid/Persistence/FileTranscriptionHistoryStore.swift b/Sources/Fluid/Persistence/FileTranscriptionHistoryStore.swift
index 4acf66ed..d4f58ca6 100644
--- a/Sources/Fluid/Persistence/FileTranscriptionHistoryStore.swift
+++ b/Sources/Fluid/Persistence/FileTranscriptionHistoryStore.swift
@@ -225,13 +225,21 @@ final class FileTranscriptionHistoryStore: ObservableObject {
// MARK: - Persistence
private func loadEntries() {
+ // Skip an entry this build cannot read instead of emptying the history;
+ // the next save would persist an empty list.
guard let data = self.defaults.data(forKey: Keys.fileTranscriptionHistory),
- let decoded = try? JSONDecoder().decode([FileTranscriptionEntry].self, from: data)
+ let decoded = PersistedHistory.decode(FileTranscriptionEntry.self, from: data)
else {
self.entries = []
return
}
- self.entries = decoded
+ self.entries = decoded.entries
+ if decoded.skipped > 0 {
+ DebugLogger.shared.info(
+ "Skipped \(decoded.skipped) unreadable file transcription entries",
+ source: "FileTranscriptionHistoryStore"
+ )
+ }
}
private func saveEntries() {
diff --git a/Sources/Fluid/Persistence/PersistedHistory.swift b/Sources/Fluid/Persistence/PersistedHistory.swift
new file mode 100644
index 00000000..03332404
--- /dev/null
+++ b/Sources/Fluid/Persistence/PersistedHistory.swift
@@ -0,0 +1,29 @@
+//
+// PersistedHistory.swift
+// Fluid
+//
+// Loading for history saved as one JSON array in UserDefaults.
+//
+
+import Foundation
+
+enum PersistedHistory {
+ /// Decodes each saved element on its own, so one element this build cannot read is
+ /// skipped instead of emptying the whole list. Returns nil when the data is not a JSON array.
+ static func decode<Entry: Decodable>(_: Entry.Type, from data: Data) -> (entries: [Entry], skipped: Int)? {
+ guard let elements = try? JSONDecoder().decode([DecodedElement<Entry>].self, from: data) else {
+ return nil
+ }
+ let entries = elements.compactMap(\.value)
+ return (entries, elements.count - entries.count)
+ }
+
+ /// One array element, or nil when that element alone fails to decode.
+ private struct DecodedElement<Value: Decodable>: Decodable {
+ let value: Value?
+
+ init(from decoder: Decoder) throws {
+ self.value = try? decoder.singleValueContainer().decode(Value.self)
+ }
+ }
+}
diff --git a/Tests/CommandArchiveSearchTests.swift b/Tests/CommandArchiveSearchTests.swift
index 0f3da69a..c86bc48e 100644
--- a/Tests/CommandArchiveSearchTests.swift
+++ b/Tests/CommandArchiveSearchTests.swift
@@ -15,6 +15,28 @@ final class SearchIndex: Sendable {
static let shared = SearchIndex()
}
+@MainActor final class SearchIndexCoordinator {
+ static let shared = SearchIndexCoordinator()
+ func prepareForSearch() async throws {}
+}
+
+enum ParakeetVocabularyStore {
+ enum VocabularyConfig {
+ struct Term: Sendable {}
+ }
+
+ static func readSearchTerms() async throws -> [VocabularyConfig.Term] { [] }
+}
+
+extension Notification.Name {
+ static let parakeetVocabularyDidChange = Notification.Name("ArchiveSearchTests.vocabularyDidChange")
+}
+
+final class DebugLogger {
+ static let shared = DebugLogger()
+ func info(_: String, source _: String) {}
+}
+
final class ZeppelinCancellationToken {}
enum SettingsSearchTarget: Hashable, Sendable { case test }
@@ -27,6 +49,11 @@ struct TranscriptionHistoryEntry {
let windowTitle: String
}
+@MainActor enum TranscriptionHistoryStore {
+ static let shared = Self.self
+ static func retryLoadingIfNeeded() {}
+}
+
struct FileTranscriptionEntry {
let id: UUID
let timestamp: Date
diff --git a/Tests/CommandSessionBoundaryTests.swift b/Tests/CommandSessionBoundaryTests.swift
index 381f101f..d0150ed9 100644
--- a/Tests/CommandSessionBoundaryTests.swift
+++ b/Tests/CommandSessionBoundaryTests.swift
@@ -16,6 +16,11 @@ final class UserDefaults {
}
}
+final class DebugLogger {
+ static let shared = DebugLogger()
+ func info(_: String, source _: String) {}
+}
+
struct TerminalService {}
@MainActor final class NotchOverlayManager {
diff --git a/Tests/HistoryDecodeRecoveryTests.swift b/Tests/HistoryDecodeRecoveryTests.swift
new file mode 100644
index 00000000..4ad799f8
--- /dev/null
+++ b/Tests/HistoryDecodeRecoveryTests.swift
@@ -0,0 +1,146 @@
+import Foundation
+
+// Standalone harness: compile the real chat and file transcription stores against
+// in-memory persistence. Never touches the installed app's history.
+final class UserDefaults {
+ static let standard = UserDefaults()
+ private var values: [String: Any] = [:]
+ func data(forKey key: String) -> Data? { self.values[key] as? Data }
+ func string(forKey key: String) -> String? { self.values[key] as? String }
+ func set(_ value: Any?, forKey key: String) { self.values[key] = value }
+}
+
+struct SpeakerTranscriptSegment: Codable, Equatable { let text: String }
+struct SpeakerTranscriptGap: Codable, Equatable { let text: String }
+struct TranscriptionResult {
+ let id: UUID
+ let text: String
+ let confidence: Float
+ let duration: TimeInterval
+ let processingTime: TimeInterval
+ let fileName: String
+ let timestamp: Date
+ let speakerSegments: [SpeakerTranscriptSegment]
+ let speakerLabelingNotice: String?
+ let speakerLabelingGaps: [SpeakerTranscriptGap]
+}
+
+final class DebugLogger {
+ static let shared = DebugLogger()
+ func debug(_: String, source _: String) {}
+ func info(_: String, source _: String) {}
+}
+
+@main @MainActor struct HistoryDecodeRecoveryTests {
+ static let chatKey = "CommandModeChatSessions"
+ static let fileKey = "FileTranscriptionHistoryEntries"
+
+ static func main() throws {
+ var checks = 0
+ func check(_ condition: Bool, _ message: String) {
+ precondition(condition, message)
+ checks += 1
+ }
+
+ // File transcription history: one unreadable entry between two readable ones.
+ let files = (0..<2).map {
+ FileTranscriptionEntry(fileName: "meeting\($0).wav", duration: 60, processingTime: 5, confidence: 0.9, text: "Transcript \($0)")
+ }
+ var unreadableFile = try self.object(files[0])
+ unreadableFile["id"] = UUID().uuidString
+ unreadableFile["confidence"] = NSNull()
+ let fileDefaults = UserDefaults()
+ fileDefaults.set(try self.array(files, inserting: unreadableFile), forKey: self.fileKey)
+ let fileStore = FileTranscriptionHistoryStore(defaults: fileDefaults)
+ check(fileStore.entries == files, "Readable file transcripts must survive one unreadable entry")
+ fileStore.addEntry(TranscriptionResult(
+ id: UUID(),
+ text: "New transcript",
+ confidence: 0.8,
+ duration: 10,
+ processingTime: 1,
+ fileName: "new.wav",
+ timestamp: Date(),
+ speakerSegments: [],
+ speakerLabelingNotice: nil,
+ speakerLabelingGaps: []
+ ))
+ let reloadedFiles = FileTranscriptionHistoryStore(defaults: fileDefaults).entries
+ check(reloadedFiles.count == 3 && Array(reloadedFiles.dropFirst()) == files, "The next save must keep every readable transcript, newest first")
+
+ // Literal JSON, so no encoder normalizes the inputs: other element shapes and an
+ // out-of-range number are skipped or ignored per element.
+ // A complete entry that only becomes unreadable because its duration overflows Double.
+ let overflowSource = try self.json(FileTranscriptionEntry(fileName: "overflow.wav", duration: 60, processingTime: 5, confidence: 0.9, text: "Overflow"))
+ let overflowing = overflowSource.replacingOccurrences(of: #""duration":60"#, with: #""duration":1e400"#)
+ check(overflowing != overflowSource, "The overflow fixture must replace the duration")
+ check((try? JSONDecoder().decode(FileTranscriptionEntry.self, from: Data(overflowSource.utf8))) != nil, "The overflow fixture is readable before the change")
+ let literalDefaults = UserDefaults()
+ literalDefaults.set(Data(#"""
+ [\#(try self.json(files[0])),"junk",null,\#(overflowing),\#(try self.json(files[1], extra: #""futureField":1e400"#))]
+ """#.utf8), forKey: self.fileKey)
+ check(FileTranscriptionHistoryStore(defaults: literalDefaults).entries == files, "Readable transcripts survive any mix of unreadable elements")
+
+ let garbageDefaults = UserDefaults()
+ garbageDefaults.set(Data("not json".utf8), forKey: self.fileKey)
+ check(FileTranscriptionHistoryStore(defaults: garbageDefaults).entries.isEmpty, "Unparseable history loads empty")
+
+ let cleanFileDefaults = UserDefaults()
+ let cleanFileBytes = try JSONEncoder().encode(files)
+ cleanFileDefaults.set(cleanFileBytes, forKey: self.fileKey)
+ check(FileTranscriptionHistoryStore(defaults: cleanFileDefaults).entries == files, "Readable history loads unchanged")
+ check(cleanFileDefaults.data(forKey: self.fileKey) == cleanFileBytes, "Loading readable history must not rewrite it")
+
+ // Command Mode chats: the launch save used to rewrite the key with a single new chat.
+ let chats = (0..<2).map {
+ ChatSession(title: "Chat \($0)", messages: [ChatMessage(role: .user, content: "Hello \($0)")])
+ }
+ var unreadableChat = try self.object(chats[0])
+ let unreadableChatID = UUID().uuidString
+ unreadableChat["id"] = unreadableChatID
+ guard var messages = unreadableChat["messages"] as? [[String: Any]] else { preconditionFailure("Encoded chat must carry messages") }
+ messages[0]["stepType"] = "stepFromANewerVersion"
+ unreadableChat["messages"] = messages
+ let chatDefaults = UserDefaults()
+ chatDefaults.set(try self.array(chats, inserting: unreadableChat), forKey: self.chatKey)
+ chatDefaults.set(unreadableChatID, forKey: "CommandModeCurrentChatID")
+ let chatStore = ChatHistoryStore(defaults: chatDefaults)
+ check(chatStore.sessions.map(\.id) == chats.map(\.id), "Readable chats must survive one unreadable chat")
+ check(chats.contains { $0.id == chatStore.currentChatID }, "A missing current chat falls back to a readable one")
+ let savedChats = try JSONDecoder().decode([ChatSession].self, from: chatDefaults.data(forKey: self.chatKey) ?? Data())
+ check(savedChats.map(\.id) == chats.map(\.id), "The launch save must keep every readable chat")
+
+ let cleanChatDefaults = UserDefaults()
+ let cleanChatBytes = try JSONEncoder().encode(chats)
+ cleanChatDefaults.set(cleanChatBytes, forKey: self.chatKey)
+ cleanChatDefaults.set(chats[1].id, forKey: "CommandModeCurrentChatID")
+ let cleanChatStore = ChatHistoryStore(defaults: cleanChatDefaults)
+ check(cleanChatStore.sessions == chats && cleanChatStore.currentChatID == chats[1].id, "Readable chats load unchanged")
+ check(cleanChatDefaults.data(forKey: self.chatKey) == cleanChatBytes, "Loading readable chats must not rewrite them")
+
+ print("History decode recovery: \(checks) checks passed")
+ }
+
+ static func object(_ value: some Encodable) throws -> [String: Any] {
+ guard let object = try JSONSerialization.jsonObject(with: JSONEncoder().encode(value)) as? [String: Any] else {
+ preconditionFailure("Encoded history entry must be a JSON object")
+ }
+ return object
+ }
+
+ static func json(_ value: some Encodable, extra: String? = nil) throws -> String {
+ guard let text = String(bytes: try JSONEncoder().encode(value), encoding: .utf8) else {
+ preconditionFailure("Encoded history entry must be UTF-8")
+ }
+ guard let extra else { return text }
+ return String(text.dropLast()) + "," + extra + "}"
+ }
+
+ static func array(_ values: [some Encodable], inserting unreadable: [String: Any]) throws -> Data {
+ guard var array = try JSONSerialization.jsonObject(with: JSONEncoder().encode(values)) as? [Any] else {
+ preconditionFailure("Encoded history must be a JSON array")
+ }
+ array.insert(unreadable, at: 1)
+ return try JSONSerialization.data(withJSONObject: array)
+ }
+}
diff --git a/Tests/run_command_archive_search_tests.sh b/Tests/run_command_archive_search_tests.sh
index 52c0253f..9596770b 100755
--- a/Tests/run_command_archive_search_tests.sh
+++ b/Tests/run_command_archive_search_tests.sh
@@ -41,6 +41,7 @@ filter_source = service[filter_start:filter_end]
(output / "AppSearchSnapshot.swift").write_text(
prefix
+ " private func schedule() {}\n"
+ + " func refresh() { self.schedule() }\n"
+ " func publishForTests(_ groups: [AppSearchGroup]) { self.groups = groups }\n"
+ filter_source
+ "}\n"
@@ -49,7 +50,7 @@ filter_source = service[filter_start:filter_end]
store_path = Path("Sources/Fluid/Persistence/ChatHistoryStore.swift")
store = store_path.read_text()
unique_position(store, "final class ChatHistoryStore: ObservableObject", store_path)
-unique_position(store, "private let defaults = UserDefaults.standard", store_path)
+unique_position(store, "init(defaults: UserDefaults = .standard)", store_path)
if "Foundation.UserDefaults" in store:
raise SystemExit("Archive search test isolation failed: store bypasses the in-memory UserDefaults double")
(output / "ChatHistoryStore.swift").write_text(
@@ -63,6 +64,7 @@ PY
xcrun swiftc -parse-as-library \
"$task_test_dir/AppSearchSnapshot.swift" \
"$task_test_dir/ChatHistoryStore.swift" \
+ Sources/Fluid/Persistence/PersistedHistory.swift \
Sources/Fluid/Persistence/Search/SearchIndexRecord.swift \
Tests/CommandArchiveSearchTests.swift \
-o "$task_test_dir/archive-search-tests"
diff --git a/Tests/run_command_cancellation_tests.sh b/Tests/run_command_cancellation_tests.sh
index bb552e85..636eb693 100644
--- a/Tests/run_command_cancellation_tests.sh
+++ b/Tests/run_command_cancellation_tests.sh
@@ -26,6 +26,7 @@ PYCODE
xcrun swiftc -parse-as-library \
Sources/Fluid/Services/CommandModeService.swift \
Sources/Fluid/Persistence/ChatHistoryStore.swift \
+ Sources/Fluid/Persistence/PersistedHistory.swift \
Tests/CommandCancellationTests.swift \
"$task_test_dir/CommandUI.swift" \
-o "$task_test_dir/cancellation-tests"
diff --git a/Tests/run_command_session_boundary_tests.sh b/Tests/run_command_session_boundary_tests.sh
index 310dc1f7..bef7814e 100644
--- a/Tests/run_command_session_boundary_tests.sh
+++ b/Tests/run_command_session_boundary_tests.sh
@@ -60,6 +60,7 @@ SWIFT
xcrun swiftc -parse-as-library \
"$task_test_dir/CommandModeService.swift" \
"$task_test_dir/ChatHistoryStore.swift" \
+ Sources/Fluid/Persistence/PersistedHistory.swift \
Tests/CommandSessionBoundaryTests.swift \
-o "$task_test_dir/session-tests"
"$task_test_dir/session-tests"
diff --git a/Tests/run_file_transcript_rename_tests.sh b/Tests/run_file_transcript_rename_tests.sh
index e22469a0..2141f30f 100644
--- a/Tests/run_file_transcript_rename_tests.sh
+++ b/Tests/run_file_transcript_rename_tests.sh
@@ -9,5 +9,5 @@ fi
export DEVELOPER_DIR="$task_developer_dir"
task_test_dir=$(mktemp -d /tmp/fluidvoice-file-rename.XXXXXX)
trap 'rm -rf "$task_test_dir"' EXIT HUP INT TERM
-xcrun swiftc -parse-as-library Sources/Fluid/Persistence/FileTranscriptionHistoryStore.swift Tests/FileTranscriptRenameTests.swift -o "$task_test_dir/tests"
+xcrun swiftc -parse-as-library Sources/Fluid/Persistence/FileTranscriptionHistoryStore.swift Sources/Fluid/Persistence/PersistedHistory.swift Tests/FileTranscriptRenameTests.swift -o "$task_test_dir/tests"
"$task_test_dir/tests"
diff --git a/Tests/run_history_decode_recovery_tests.sh b/Tests/run_history_decode_recovery_tests.sh
new file mode 100644
index 00000000..325abccc
--- /dev/null
+++ b/Tests/run_history_decode_recovery_tests.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+set -eu
+cd "$(dirname "$0")/.."
+task_developer_dir="${DEVELOPER_DIR:-$(xcode-select -p)}"
+if [ ! -d "$task_developer_dir/Platforms/MacOSX.platform" ]; then
+ echo "Set DEVELOPER_DIR to a full Xcode before running tests." >&2
+ exit 1
+fi
+export DEVELOPER_DIR="$task_developer_dir"
+task_test_dir=$(mktemp -d /tmp/fluidvoice-history-decode.XXXXXX)
+trap 'rm -rf "$task_test_dir"' EXIT HUP INT TERM
+xcrun swiftc -parse-as-library \
+ Sources/Fluid/Persistence/ChatHistoryStore.swift \
+ Sources/Fluid/Persistence/FileTranscriptionHistoryStore.swift \
+ Sources/Fluid/Persistence/PersistedHistory.swift \
+ Tests/HistoryDecodeRecoveryTests.swift \
+ -o "$task_test_dir/tests"
+"$task_test_dir/tests"