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
perf(clipboard): cap snapshot representations at 32 MB
altic-devbbaedd94e2d5
diff --git a/Sources/Fluid/Services/PasteDeliveryCoordinator.swift b/Sources/Fluid/Services/PasteDeliveryCoordinator.swiftindex a874a8bb..28c51c8f 100644--- a/Sources/Fluid/Services/PasteDeliveryCoordinator.swift+++ b/Sources/Fluid/Services/PasteDeliveryCoordinator.swift@@ -120,6 +120,15 @@ final class SystemPasteboardManager: PasteboardManaging {ClipboardAudit.record(event, pasteboard: self.pasteboard, detail: detail)}+ /// A representation larger than this is left out of the snapshot: the+ /// paste must never wait on copying it, and the small ones (strings, file+ /// URLs) still restore. A missing snapshot would block the paste, so an+ /// oversized clipboard yields a smaller snapshot rather than none.+ static let maximumRepresentationBytes = 32 * 1024 * 1024+ /// Decoding and re-encoding an image to PNG costs time proportional to its+ /// pixels; skip it for large sources and keep their original bytes only.+ static let maximumPortableImageSourceBytes = 16 * 1024 * 1024+func captureSnapshot() -> PasteboardSnapshot? {let startedAt = ProcessInfo.processInfo.systemUptimefor attempt in 1...2 {@@ -146,6 +155,8 @@ final class SystemPasteboardManager: PasteboardManaging {var totalBytes = 0var totalPortableImageBytes = 0var totalRepresentations = 0+ var skippedBytes = 0+ var skippedTypes: [String] = []var failure: (reason: String, itemIndex: Int, type: NSPasteboard.PasteboardType?)?itemLoop: for (itemIndex, sourceItem) in sourceItems.enumerated() {@@ -162,10 +173,16 @@ final class SystemPasteboardManager: PasteboardManaging {failure = ("unavailable_representation", itemIndex, type)break itemLoop}+ guard data.count <= Self.maximumRepresentationBytes else {+ skippedBytes += data.count+ skippedTypes.append(type.rawValue)+ continue+ }representations.append(.init(type: type, data: data))totalBytes += data.counttotalRepresentations += 1}+ guard !representations.isEmpty else { continue }let item = PasteboardSnapshot.Item(representations: representations)let portableImage = Self.portableImageRepresentation(for: item)totalPortableImageBytes += portableImage?.data.count ?? 0@@ -201,6 +218,7 @@ final class SystemPasteboardManager: PasteboardManaging {self.log("clipboard_snapshot_complete items=\(items.count) representations=\(totalRepresentations) " +"bytes=\(totalBytes) portableImageBytes=\(totalPortableImageBytes) " ++ "skippedBytes=\(skippedBytes) skippedTypes=\(skippedTypes.joined(separator: ",")) " +"elapsedMs=\(Self.elapsedMs(since: startedAt)) firstItem=\(Self.representationSummary(items.first))")return PasteboardSnapshot(items: items)@@ -269,6 +287,12 @@ final class SystemPasteboardManager: PasteboardManaging {}private func restore(_ snapshot: PasteboardSnapshot, ifUnchangedSince changeCount: Int?) -> Bool {+ guard !snapshot.items.isEmpty else {+ // Every representation was too large to keep; leave the transcript+ // on the clipboard rather than clearing it.+ self.log("clipboard_restore_skipped reason=nothing_captured")+ return true+ }var items: [NSPasteboardItem] = []for snapshotItem in snapshot.items {let item = NSPasteboardItem()@@ -406,6 +430,7 @@ final class SystemPasteboardManager: PasteboardManaging {for representation in item.representations {guard UTType(representation.type.rawValue)?.conforms(to: .image) == true,+ representation.data.count <= Self.maximumPortableImageSourceBytes,let pngData = Self.pngData(from: representation.data)else {continuediff --git a/Tests/FluidDictationIntegrationTests/TypingServiceTransientPasteboardTests.swift b/Tests/FluidDictationIntegrationTests/TypingServiceTransientPasteboardTests.swiftindex 182fae73..2c320cd9 100644--- a/Tests/FluidDictationIntegrationTests/TypingServiceTransientPasteboardTests.swift+++ b/Tests/FluidDictationIntegrationTests/TypingServiceTransientPasteboardTests.swift@@ -147,6 +147,42 @@ final class TypingServiceTransientPasteboardTests: XCTestCase {XCTAssertFalse(types.contains { $0.hasSuffix(".PasteSession") })}+ func testSnapshotSkipsOversizedRepresentationsButKeepsTheRest() throws {+ let pasteboard = self.makePasteboard()+ defer { pasteboard.releaseGlobally() }+ let hugeType = NSPasteboard.PasteboardType("com.fluidvoice.tests.huge")+ let item = NSPasteboardItem()+ XCTAssertTrue(item.setData(Data(count: SystemPasteboardManager.maximumRepresentationBytes + 1), forType: hugeType))+ XCTAssertTrue(item.setString("keep me", forType: .string))+ XCTAssertTrue(pasteboard.writeObjects([item]))++ let manager = SystemPasteboardManager(pasteboard: pasteboard)+ let snapshot = try XCTUnwrap(manager.captureSnapshot())+ XCTAssertEqual(snapshot.items.count, 1)+ XCTAssertEqual(snapshot.items.first?.representations.map(\.type), [.string])+ XCTAssertTrue(manager.writeTemporaryText("temporary", sessionID: "session"))+ XCTAssertTrue(manager.restore(snapshot))+ XCTAssertEqual(pasteboard.string(forType: .string), "keep me")+ XCTAssertNil(pasteboard.data(forType: hugeType))+ }++ func testSnapshotWithNothingSmallEnoughStillLetsThePasteProceed() throws {+ let pasteboard = self.makePasteboard()+ defer { pasteboard.releaseGlobally() }+ let hugeType = NSPasteboard.PasteboardType("com.fluidvoice.tests.huge")+ let item = NSPasteboardItem()+ XCTAssertTrue(item.setData(Data(count: SystemPasteboardManager.maximumRepresentationBytes + 1), forType: hugeType))+ XCTAssertTrue(pasteboard.writeObjects([item]))++ let manager = SystemPasteboardManager(pasteboard: pasteboard)+ // A nil snapshot would refuse the paste; an empty one must not.+ let snapshot = try XCTUnwrap(manager.captureSnapshot())+ XCTAssertTrue(snapshot.items.isEmpty)+ XCTAssertTrue(manager.writeTemporaryText("temporary", sessionID: "session"))+ XCTAssertTrue(manager.restore(snapshot))+ XCTAssertEqual(pasteboard.string(forType: .string), "temporary")+ }+func testSnapshotRestoresEveryRepresentation() throws {let pasteboard = self.makePasteboard()defer { pasteboard.releaseGlobally() }