Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
fix(spaces-macos): stop the app's memory from growing without bound (#4478)
ddupont9313551ef346
diff --git a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/SpaceThumbnails.swift b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/SpaceThumbnails.swiftindex e251e5587e..bf18da2f25 100644--- a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/SpaceThumbnails.swift+++ b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Models/SpaceThumbnails.swift@@ -38,9 +38,21 @@ public final class SpaceThumbnails {public struct Entry: Equatable {public var image: NSImagepublic var capturedAt: Date+ /// The decoded pixels' size in bytes (what the cap counts).+ public var bytes: Int}public private(set) var entries: [String: Entry] = [:]+ /// The most bytes of images held; past it the least recently set or+ /// read Space's image goes first. Images are thumbnails (the policy's+ /// `maxDimension`, about 0.4 MB each), so the cap holds well over a+ /// hundred Spaces and only bites on an oversized image.+ @ObservationIgnored public var byteCap = SpaceThumbnails.defaultByteCap+ public static let defaultByteCap = 64 << 20+ /// Bytes held now.+ public private(set) var totalBytes = 0+ /// Space ids, least recently used first.+ @ObservationIgnored private var recency: [String] = []/// The SDK call (`Space.thumbnail(maxAgeMs:)`); none: memory only.@ObservationIgnored public var fetch: ((String, UInt64?) async -> SpaceThumbnailData?)?@ObservationIgnored private var inFlight: Set<String> = []@@ -53,7 +65,11 @@ public final class SpaceThumbnails {/// The Space's latest image, read synchronously (nil: none yet).public subscript(id: String) -> NSImage? {- get { entries[id]?.image }+ get {+ guard let entry = entries[id] else { return nil }+ touch(id)+ return entry.image+ }set {if let newValue { set(id, newValue) } else { remove(id) }}@@ -62,16 +78,46 @@ public final class SpaceThumbnails {/// Keeps `image` as the Space's latest, unless a newer one is held.public func set(_ id: String, _ image: NSImage, at capturedAt: Date = Date()) {if let held = entries[id], held.capturedAt > capturedAt { return }- entries[id] = Entry(image: image, capturedAt: capturedAt)+ let bytes = Self.bytes(of: image)+ totalBytes += bytes - (entries[id]?.bytes ?? 0)+ entries[id] = Entry(image: image, capturedAt: capturedAt, bytes: bytes)+ touch(id)+ evict(keeping: id)}public func remove(_ id: String) {- entries[id] = nil+ guard let held = entries.removeValue(forKey: id) else { return }+ totalBytes -= held.bytes+ recency.removeAll { $0 == id }}/// Forgets the Spaces not in `ids` (deleted or forgotten).public func retain(_ ids: Set<String>) {- for id in entries.keys where !ids.contains(id) { entries[id] = nil }+ for id in Array(entries.keys) where !ids.contains(id) { remove(id) }+ }++ private func touch(_ id: String) {+ if recency.last == id { return }+ recency.removeAll { $0 == id }+ recency.append(id)+ }++ /// Drops the least recently used images until the rest fit the cap+ /// (`keeping`'s own image stays even when it alone is over).+ private func evict(keeping id: String) {+ var i = 0+ while totalBytes > byteCap, i < recency.count {+ let victim = recency[i]+ if victim == id { i += 1; continue }+ remove(victim)+ }+ }++ /// The decoded size of an image's largest representation.+ static func bytes(of image: NSImage) -> Int {+ let reps = image.representations.map { $0.pixelsWide * $0.pixelsHigh * 4 }+ if let largest = reps.max(), largest > 0 { return largest }+ return Int(image.size.width * image.size.height * 4)}/// Asks the SDK for the Space's thumbnail no older than `maxAge`@@ -125,10 +171,23 @@ public final class SpaceThumbnails {}/// The last frame of a stream as an image (it is newer than any- /// thumbnail when the stream stops).- public static func image(_ buffer: CVPixelBuffer) -> NSImage? {- let ci = CIImage(cvPixelBuffer: buffer)- guard let cg = CIContext().createCGImage(ci, from: ci.extent) else { return nil }+ /// thumbnail when the stream stops), scaled down to a thumbnail's size:+ /// a full-size frame is tens of megabytes held for as long as the app+ /// runs.+ public static func image(_ buffer: CVPixelBuffer,+ maxDimension: Int = Int(SpaceThumbnails.policy.maxDimension)) -> NSImage? {+ var ci = CIImage(cvPixelBuffer: buffer)+ let long = max(ci.extent.width, ci.extent.height)+ if maxDimension > 0, long > CGFloat(maxDimension) {+ let k = CGFloat(maxDimension) / long+ ci = ci.transformed(by: CGAffineTransform(scaleX: k, y: k))+ }+ let rect = ci.extent.integral+ guard let cg = context.createCGImage(ci, from: rect) else { return nil }return NSImage(cgImage: cg, size: NSSize(width: cg.width, height: cg.height))}++ /// One Core Image context for the app (each one holds its own GPU+ /// caches; one per call grew them per call).+ static let context = CIContext()}diff --git a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Views/DesktopCover.swift b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Views/DesktopCover.swiftindex f938316564..98f3242e83 100644--- a/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Views/DesktopCover.swift+++ b/apps/cua-spaces-macos/Sources/CuaSpacesMacKit/Views/DesktopCover.swift@@ -28,7 +28,15 @@ struct DesktopCoverView: View {/// and colors only, no text).static let blurShare: CGFloat = 0.035static let dim = 0.28- private static let blurs = NSCache<NSImage, NSImage>()+ /// Blurred images by source image. Bounded: the key retains its source+ /// image, so an unbounded cache kept every thumbnail ever shown (one+ /// per Space every 90 s) alive with its blur until memory pressure.+ static let blurs: NSCache<NSImage, NSImage> = {+ let cache = NSCache<NSImage, NSImage>()+ cache.countLimit = 16+ cache.totalCostLimit = 32 << 20+ return cache+ }()private static let context = CIContext()/// The image blurred once (Core Image, edges clamped so they stay@@ -43,7 +51,7 @@ struct DesktopCoverView: View {.cropped(to: input.extent) as CIImage?,let out = context.createCGImage(output, from: input.extent) else { return image }let result = NSImage(cgImage: out, size: image.size)- blurs.setObject(result, forKey: image)+ blurs.setObject(result, forKey: image, cost: out.width * out.height * 4)return result}diff --git a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/DesktopCoverTests.swift b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/DesktopCoverTests.swiftindex b042d698eb..c38d27006e 100644--- a/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/DesktopCoverTests.swift+++ b/apps/cua-spaces-macos/Tests/CuaSpacesMacTests/DesktopCoverTests.swift@@ -2,6 +2,7 @@// Copyright (c) 2026 Cua AI, Inc.import AppKit+import CoreVideoimport CuaSpacesFFI@testable import CuaSpacesMacKitimport Foundation@@ -94,6 +95,57 @@ struct DesktopCoverTests {#expect(store.entries.isEmpty)}+ /// The store never holds more than its byte cap, however many Spaces+ /// or refreshes come through: the least recently used image goes first.+ @Test func theStoreStaysWithinItsByteCap() {+ let store = SpaceThumbnails()+ store.byteCap = 4 << 20+ // 640 x 400 x 4 bytes, about 1 MB each.+ let each = SpaceThumbnails.bytes(of: DesktopCoverTests.bitmap(640, 400))+ #expect(each == 640 * 400 * 4)+ for round in 0..<20 {+ for i in 0..<10 {+ store.set("s\(i)", DesktopCoverTests.bitmap(640, 400),+ at: Date().addingTimeInterval(Double(round)))+ #expect(store.totalBytes <= store.byteCap, "round \(round) Space \(i)")+ }+ }+ #expect(store.entries.count == store.byteCap / each)+ #expect(store.totalBytes == store.entries.values.reduce(0) { $0 + $1.bytes })+ // The newest are kept; a read keeps an image from going next.+ #expect(store["s9"] != nil && store["s0"] == nil)+ _ = store["s6"]+ store.set("s0", DesktopCoverTests.bitmap(640, 400))+ #expect(store["s6"] != nil && store["s7"] == nil)+ store.retain([])+ #expect(store.totalBytes == 0 && store.entries.isEmpty)+ }++ /// A stream's last frame is kept at thumbnail size, not full size.+ @Test func theLastFrameIsKeptAtThumbnailSize() throws {+ var buffer: CVPixelBuffer?+ CVPixelBufferCreate(nil, 2560, 1600, kCVPixelFormatType_32BGRA,+ [kCVPixelBufferIOSurfacePropertiesKey: [:]] as CFDictionary, &buffer)+ let frame = try #require(buffer)+ let image = try #require(SpaceThumbnails.image(frame))+ let max = Int(SpaceThumbnails.policy.maxDimension)+ #expect(Int(image.size.width) == max && Int(image.size.height) == max * 1600 / 2560)+ }++ /// The cover's blur cache is bounded (its keys retain their images).+ @Test func theBlurCacheIsBounded() {+ #expect(DesktopCoverView.blurs.countLimit > 0 && DesktopCoverView.blurs.totalCostLimit > 0)+ }++ static func bitmap(_ w: Int, _ h: Int) -> NSImage {+ let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: w, pixelsHigh: h, bitsPerSample: 8,+ samplesPerPixel: 4, hasAlpha: true, isPlanar: false,+ colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)!+ let image = NSImage(size: NSSize(width: w, height: h))+ image.addRepresentation(rep)+ return image+ }+@Test func warmingAsksTheCacheOncePerSpace() async {let store = SpaceThumbnails()var asked: [UInt64?] = []diff --git a/libs/spaces-app-swift/Sources/CuaSpacesStreaming/LiveStreamSession.swift b/libs/spaces-app-swift/Sources/CuaSpacesStreaming/LiveStreamSession.swiftindex 4480a88471..88b203a8f5 100644--- a/libs/spaces-app-swift/Sources/CuaSpacesStreaming/LiveStreamSession.swift+++ b/libs/spaces-app-swift/Sources/CuaSpacesStreaming/LiveStreamSession.swift@@ -98,6 +98,8 @@ public final class LiveStreamSession: ObservableObject {private var media: SpaceStreamSession?private var relay: FrameRelay?private var decoder: H264Decoder?+ /// The current open's frame mailbox (tests read how much it holds).+ private(set) var mailbox: FrameMailbox?private var sessionID: SessionID?/// The window presence cursors are reported against. `nil` for the desktop/// source, which is what the protocol wants for a screen-wide cursor.@@ -227,6 +229,9 @@ public final class LiveStreamSession: ObservableObject {inputFailure = nildecoder?.reset()decoder = nil+ // A frame still waiting for the main actor is let go now.+ mailbox?.clear()+ mailbox = nil}// MARK: - Source@@ -234,17 +239,29 @@ public final class LiveStreamSession: ObservableObject {private func open(_ source: StreamSource, generation: UInt64) async {let decoder = H264Decoder()// Frames and events of a superseded open never reach this session.+ //+ // Decoded frames go through a one-slot mailbox, newest wins: at most+ // one hop to the main actor is queued, and it takes whatever frame is+ // newest when it runs. A hop per frame queued one decoded IOSurface+ // per frame for as long as the main actor was busy (VideoToolbox+ // allocates a fresh surface for each), with no bound.+ let mailbox = FrameMailbox()+ self.mailbox = mailboxdecoder.onFrame = { [weak self] buffer, descriptor in- // A decoded buffer is never written again: handing it to the- // main actor is safe, which CoreVideo cannot declare.- let frame = DecodedFrame(buffer: buffer)+ guard mailbox.post(buffer, descriptor) else { return }Task { @MainActor [weak self] in+ guard let (frame, skipped) = mailbox.take() else { return }guard let self, self.generation == generation else { return }- self.ingest(frame.buffer, descriptor: descriptor)+ self.ingest(frame.buffer, descriptor: frame.descriptor, skipped: skipped)}}+ // One keyframe request queued at a time (the decoder asks once per+ // dropped frame).+ let keyframeAsk = Coalescer()decoder.onNeedsKeyframe = { [weak self] in+ guard keyframeAsk.arm() else { return }Task { @MainActor [weak self] in+ keyframeAsk.disarm()guard let self, self.generation == generation else { return }self.requestKeyframeThrottled()}@@ -370,20 +387,59 @@ public final class LiveStreamSession: ObservableObject {let members = try await session.roster()publish(roster.apply(me: me, members: members, surfaceSize: surfaceSize))presenceTask = Task { [weak self] in- // Bounded per call; ends with the task (teardown cancels it).+ // Bounded per call; ends with the task (teardown cancels it),+ // when the presence stream ends, or after repeated failures.+ //+ // `nextEvent` answers nil at once, every time, once the+ // stream has ended (a Space or daemon restart, sleep, the+ // network): this loop used to `continue` straight into the+ // next call, spinning the main actor on SDK calls for as+ // long as the stream stayed open.+ var failures = 0while !Task.isCancelled {- guard let event = try? await session.nextEvent(timeoutMs: 1_000) else {- if Task.isCancelled { return }+ let event: CuaSDK.PresenceEvent?+ do {+ event = try await session.nextEvent(timeoutMs: 1_000)+ failures = 0+ } catch CuaError.Timeout {+ failures = 0+ continue+ } catch {+ failures += 1+ guard failures < Self.presenceRetries else { break }+ try? await Task.sleep(for: Self.presenceBackoff(failures))continue}+ guard let event else { break }self?.apply(event)}+ if !Task.isCancelled { self?.presenceEnded(session) }}} catch {// Presence is optional: a Space without it still streams.}}+ /// Consecutive presence failures before presence is given up on (until+ /// the next open joins again), and the wait before each retry.+ static let presenceRetries = 5+ static func presenceBackoff(_ failures: Int) -> Duration {+ .milliseconds(min(250 << min(failures, 6), 5_000))+ }++ /// The presence stream ended: drop its cursors. The next open (a+ /// reconnect or a source switch) joins again.+ private func presenceEnded(_ session: SpacePresence) {+ guard presence === session else { return }+ presenceTask = nil+ presence = nil+ presenceView = nil+ roster = PresenceRoster()+ participants = []+ localParticipant = nil+ Task { try? await session.leave() }+ }+private func apply(_ event: CuaSDK.PresenceEvent) {_ = presenceView?.apply(event: event, localMs: presenceNowMs())publish(roster.apply(event: event, surfaceSize: surfaceSize))@@ -443,16 +499,22 @@ public final class LiveStreamSession: ObservableObject {private var pendingCursor: PresenceCursor?private var cursorSend: Task<Void, Never>?- private func ingest(_ buffer: CVPixelBuffer, descriptor: VideoFrameDescriptor) {+ private func ingest(_ buffer: CVPixelBuffer, descriptor: VideoFrameDescriptor, skipped: Int = 0) {frame = buffer- lastFrameDimensions = CGSize(width: CVPixelBufferGetWidth(buffer),- height: CVPixelBufferGetHeight(buffer))- surfaceSize = CGSize(width: descriptor.width_px, height: descriptor.height_px)+ // Every assignment to a `@Published` property re-renders each view+ // observing the session, equal or not: set only what changed, so a+ // frame costs one render and the main thread keeps up.+ let dimensions = CGSize(width: CVPixelBufferGetWidth(buffer), height: CVPixelBufferGetHeight(buffer))+ if lastFrameDimensions != dimensions { lastFrameDimensions = dimensions }+ let surface = CGSize(width: descriptor.width_px, height: descriptor.height_px)+ if surfaceSize != surface { surfaceSize = surface }// Counted here rather than read from the decoder, which is replaced on- // every source switch.- decodedFrameCount += 1- droppedBeforeKeyframeCount = decoder?.droppedBeforeKeyframeCount ?? droppedBeforeKeyframeCount- decodeFailureCount = decoder?.decodeFailureCount ?? decodeFailureCount+ // every source switch; frames the mailbox replaced were decoded too.+ decodedFrameCount += 1 + skipped+ let dropped = decoder?.droppedBeforeKeyframeCount ?? droppedBeforeKeyframeCount+ if droppedBeforeKeyframeCount != dropped { droppedBeforeKeyframeCount = dropped }+ let failures = decoder?.decodeFailureCount ?? decodeFailureCount+ if decodeFailureCount != failures { decodeFailureCount = failures }if status != .streaming { status = .streaming }}@@ -504,6 +566,77 @@ public final class LiveStreamSession: ObservableObject {/// buffer it has handed out, so sharing it is safe.struct DecodedFrame: @unchecked Sendable {let buffer: CVPixelBuffer+ var descriptor: VideoFrameDescriptor+}++/// Hands decoded frames from the decoder's queue to the main actor, holding+/// at most one: a newer frame replaces (and releases) one not yet taken.+/// `post` answers whether the caller must schedule a `take`; while one is+/// scheduled, later posts only replace the frame, so however far the main+/// actor falls behind, one frame and one hop are pending, never a backlog.+final class FrameMailbox: @unchecked Sendable {+ private let lock = NSLock()+ private var pending: DecodedFrame?+ private var scheduled = false+ private var replaced = 0++ /// Leaves `buffer` as the newest frame. True: schedule a `take`.+ func post(_ buffer: CVPixelBuffer, _ descriptor: VideoFrameDescriptor) -> Bool {+ lock.lock()+ defer { lock.unlock() }+ if pending != nil { replaced += 1 }+ pending = DecodedFrame(buffer: buffer, descriptor: descriptor)+ if scheduled { return false }+ scheduled = true+ return true+ }++ /// The newest frame and how many it replaced since the last take.+ func take() -> (DecodedFrame, Int)? {+ lock.lock()+ defer { lock.unlock() }+ scheduled = false+ guard let frame = pending else { return nil }+ let skipped = replaced+ pending = nil+ replaced = 0+ return (frame, skipped)+ }++ /// Drops the waiting frame.+ func clear() {+ lock.lock()+ pending = nil+ replaced = 0+ lock.unlock()+ }++ /// Frames held (0 or 1).+ var held: Int {+ lock.lock()+ defer { lock.unlock() }+ return pending == nil ? 0 : 1+ }+}++/// At most one queued hop: `arm` is true once until `disarm`.+final class Coalescer: @unchecked Sendable {+ private let lock = NSLock()+ private var armed = false++ func arm() -> Bool {+ lock.lock()+ defer { lock.unlock() }+ if armed { return false }+ armed = true+ return true+ }++ func disarm() {+ lock.lock()+ armed = false+ lock.unlock()+ }}/// Receives the SDK's frames on its delivery thread and feeds the decoder in@@ -515,6 +648,8 @@ final class FrameRelay: FrameSink, @unchecked Sendable {// read here: a torn read of a `String`.)private let lock = NSLock()private var decoder: H264Decoder?+ /// The current open's frame mailbox (tests read how much it holds).+ private(set) var mailbox: FrameMailbox?private var session = SessionID("")private let onEvent: @Sendable (MediaEvent) -> Voiddiff --git a/libs/spaces-app-swift/Tests/CuaSpacesStreamingTests/StreamMemoryBoundsTests.swift b/libs/spaces-app-swift/Tests/CuaSpacesStreamingTests/StreamMemoryBoundsTests.swiftnew file mode 100644index 0000000000..f33a735428--- /dev/null+++ b/libs/spaces-app-swift/Tests/CuaSpacesStreamingTests/StreamMemoryBoundsTests.swift@@ -0,0 +1,184 @@+// SPDX-License-Identifier: FSL-1.1-MIT+// Copyright (c) 2026 Cua AI, Inc.++import CoreVideo+import Cua+import Foundation+import Testing+@testable import CuaSpaces+@testable import CuaSpacesStreaming++/// What a live stream may hold on to: decoded frames wait in a one-slot+/// mailbox (newest wins) instead of one main-actor hop each, each holding+/// its IOSurface; and an ended presence stream is not polled in a loop.+@MainActor+@Suite("Stream memory bounds")+struct StreamFrameBacklogTests {++ /// Frames posted faster than they are taken: one is held, one hop is+ /// asked for, and every replaced frame is released at once (the pool's+ /// allocation threshold refuses a buffer while too many are alive).+ @Test func theMailboxHoldsOneFrameAndReleasesTheRest() throws {+ let pool = try Self.pool(width: 64, height: 64)+ let aux = [kCVPixelBufferPoolAllocationThresholdKey: 3] as CFDictionary+ let mailbox = FrameMailbox()+ var hops = 0+ for i in 0..<10_000 {+ var buffer: CVPixelBuffer?+ let status = CVPixelBufferPoolCreatePixelBufferWithAuxAttributes(nil, pool, aux, &buffer)+ #expect(status == kCVReturnSuccess, "buffer \(i): earlier frames were not released")+ guard let buffer else { return }+ if mailbox.post(buffer, Self.descriptor(width: 64, height: 64, sequence: UInt64(i))) { hops += 1 }+ #expect(mailbox.held == 1)+ }+ #expect(hops == 1, "one hop queued, however many frames arrive before it runs")+ let (frame, skipped) = try #require(mailbox.take())+ #expect(frame.descriptor.sequence == 9_999 && skipped == 9_999)+ #expect(mailbox.held == 0 && mailbox.take() == nil)+ // After a take, the next frame asks for a new hop.+ var buffer: CVPixelBuffer?+ CVPixelBufferPoolCreatePixelBuffer(nil, pool, &buffer)+ #expect(mailbox.post(try #require(buffer), Self.descriptor(width: 64, height: 64, sequence: 0)))+ mailbox.clear()+ #expect(mailbox.held == 0)+ }++ @Test func keyframeRequestsCoalesce() {+ let c = Coalescer()+ #expect(c.arm())+ for _ in 0..<100 { #expect(!c.arm()) }+ c.disarm()+ #expect(c.arm())+ }++ /// A presence stream that ended answers nil at once, forever; one+ /// that lost its connection fails at once. Either way the session stops+ /// asking (it used to spin the main actor on SDK calls, about 85,000 a+ /// second, for as long as the stream stayed open) and drops the cursors.+ @Test(arguments: [EndedPresence.Ending.ends, .fails])+ func anEndedPresenceStreamIsNotPolledInALoop(_ ending: EndedPresence.Ending) async throws {+ let presence = EndedPresence(ending)+ let provider = FrameProvider(presence: presence)+ let session = LiveStreamSession(provider: provider)+ await session.start()+ await session.joinPresence(as: "Me")+ #expect(session.presenceView != nil)+ presence.end()+ try await Task.sleep(for: .milliseconds(1_200))+ let calls = presence.callsAfterEnd+ #expect(calls <= 3, "\(calls) calls in 1.2 s after the presence stream ended")+ if ending == .ends {+ #expect(session.presenceView == nil && session.participants.isEmpty)+ }+ await session.stop()+ }++ @Test func presenceRetriesBackOff() {+ let waits = (1..<LiveStreamSession.presenceRetries).map { LiveStreamSession.presenceBackoff($0) }+ #expect(waits == waits.sorted() && waits.first! >= .milliseconds(250) && waits.last! <= .seconds(5))+ }++ // MARK: - Fixtures++ static func pool(width: Int, height: Int) throws -> CVPixelBufferPool {+ var pool: CVPixelBufferPool?+ let attributes: [CFString: Any] = [+ kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_32BGRA,+ kCVPixelBufferWidthKey: width, kCVPixelBufferHeightKey: height,+ kCVPixelBufferIOSurfacePropertiesKey: [:] as CFDictionary,+ ]+ CVPixelBufferPoolCreate(nil, nil, attributes as CFDictionary, &pool)+ return try #require(pool)+ }++ static func descriptor(width: Int, height: Int, sequence: UInt64) -> VideoFrameDescriptor {+ VideoFrameDescriptor(videoFrame(Data(), width: width, height: height, sequence: sequence),+ session: SessionID("test"))+ }++ static func videoFrame(_ data: Data, width: Int, height: Int, sequence: UInt64) -> VideoFrame {+ VideoFrame(sequence: sequence, codec: "h264", keyframe: true, width: UInt32(width), height: UInt32(height),+ captureTimestampUs: sequence * 16_666, codecEpoch: 1, geometryEpoch: 1,+ data: data, headerJson: "")+ }+}++/// A media session that never touches Rust.+private final class StillMedia: SpaceStreamSession, @unchecked Sendable {+ init() { super.init(noHandle: NoHandle()) }+ required init(unsafeFromHandle handle: UInt64) { fatalError("not used") }+ override func codec() -> String { "h264" }+ override func mediaSessionId() -> String { "media-backlog" }+ override func requestKeyframe() throws {}+ override func sendText(json: String) throws {}+ override func isOpen() -> Bool { true }+ override func close() async throws -> SpaceStreamStats {+ SpaceStreamStats(frames: 0, keyframes: 0, framesDropped: 0, framesGated: 0, keyframeRequests: 0,+ audioPackets: 0, audioLost: 0, events: 0, malformed: 0)+ }+}++/// Presence that answers normally until `end()`, then as an ended stream+/// (nil at once) or a lost connection (`Closed` at once).+final class EndedPresence: SpacePresence, @unchecked Sendable {+ enum Ending: Sendable { case ends, fails }+ let ending: Ending+ private let lock = NSLock()+ private var ended = false+ private var after = 0++ init(_ ending: Ending) {+ self.ending = ending+ super.init(noHandle: NoHandle())+ }+ required init(unsafeFromHandle handle: UInt64) { fatalError("not used") }++ func end() { lock.lock(); ended = true; lock.unlock() }+ var callsAfterEnd: Int { lock.lock(); defer { lock.unlock() }; return after }++ override func view() -> PresenceView { PresenceView(me: "p-me") }+ override func me() async throws -> PresenceParticipant {+ PresenceParticipant(participantId: "p-me", principalId: "u", displayName: "Me", color: "#3b82f6",+ kind: "human")+ }+ override func roster() async throws -> [PresenceMember] { [] }+ override func updateCursor(cursor: PresenceCursor) async throws {}+ override func leave() async throws {}+ override func nextEvent(timeoutMs: UInt64?) async throws -> CuaSDK.PresenceEvent? {+ lock.lock()+ let done = ended+ if done { after += 1 }+ lock.unlock()+ guard done else {+ try await Task.sleep(for: .milliseconds(20))+ throw CuaError.Timeout(message: "waiting for a presence event")+ }+ switch ending {+ case .ends: return nil+ case .fails: throw CuaError.Closed(message: "presence session")+ }+ }+}++/// Opens at once and keeps the frame sink, so the test is the transport.+private final class FrameProvider: SpaceStreamSourceProviding, @unchecked Sendable {+ private let lock = NSLock()+ private var held: FrameSink?+ private let presence: SpacePresence?++ init(presence: SpacePresence? = nil) { self.presence = presence }+ var sink: FrameSink? { lock.lock(); defer { lock.unlock() }; return held }++ func availableWindows() async throws -> [StreamWindow] { [] }++ func openSession(_ source: StreamSource, frames: FrameSink,+ audio: AudioSink?) async throws -> SpaceStreamSession {+ lock.lock(); held = frames; lock.unlock()+ return StillMedia()+ }++ func joinPresence(name: String, color: String?) async throws -> SpacePresence {+ guard let presence else { throw StreamError.noStream("no presence in this test") }+ return presence+ }+}