The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix(desktop): drop wrong-size back buffers on macOS
tonyafb6e38e2b3e
diff --git a/desktop/CLAUDE.md b/desktop/CLAUDE.mdindex 1a66b1cbe..475c2796a 100644--- a/desktop/CLAUDE.md+++ b/desktop/CLAUDE.md@@ -442,6 +442,11 @@ add a kind there, not at the call site.- `macos/Runner/MainFlutterWindow.swift` installs native menu items and calls into Dart over the`harness/app_menu` MethodChannel (`checkForUpdates`, `flashFirmware`, `showShortcuts`, terminal fontsize). Keep the menu in Swift; only the handler lives in `RootShell`.+- `macos/Runner/SurfaceCacheGuard.swift` patches Flutter's private macOS back-buffer cache, which can+ hand a frame a surface of another size after an A → B → A resize or a display replug; Impeller then+ dereferences a null color texture in `Canvas::SetupRenderPass` ([flutter#185394](https://github.com/flutter/flutter/issues/185394)).+ It checks every private class, selector and type encoding and stays inert if they change. On a+ Flutter bump, check whether `removeSurfaceForSize:` compares each surface's size; delete it if so.- **Agent-account usage is what the native Models menu reads** (`lib/usage/`,`usage/models_menu_controller.dart`, `SwarmSubscriptionView` in `SwarmTitlebar.swift`): what theClaude and Codex accounts on this machine — and on the remote machines that answer `usage_read` —diff --git a/desktop/macos/Runner.xcodeproj/project.pbxproj b/desktop/macos/Runner.xcodeproj/project.pbxprojindex c9d953ca1..091cb1ecd 100644--- a/desktop/macos/Runner.xcodeproj/project.pbxproj+++ b/desktop/macos/Runner.xcodeproj/project.pbxproj@@ -25,6 +25,7 @@B20100000000000000000003 /* HarnessKeymap.swift in Sources */ = {isa = PBXBuildFile; fileRef = B20100000000000000000004 /* HarnessKeymap.swift */; };B20100000000000000000A01 /* HarnessNotifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = B20100000000000000000A02 /* HarnessNotifications.swift */; };B20100000000000000000B01 /* HarnessStatusMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = B20100000000000000000B02 /* HarnessStatusMenu.swift */; };+ B20100000000000000000C01 /* SurfaceCacheGuard.swift in Sources */ = {isa = PBXBuildFile; fileRef = B20100000000000000000C02 /* SurfaceCacheGuard.swift */; };331C80D8294CF71000263BE5 /* RunnerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 331C80D7294CF71000263BE5 /* RunnerTests.swift */; };335BBD1B22A9A15E00E9071D /* GeneratedPluginRegistrant.swift in Sources */ = {isa = PBXBuildFile; fileRef = 335BBD1A22A9A15E00E9071D /* GeneratedPluginRegistrant.swift */; };33CC10F12044A3C60003C045 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 33CC10F02044A3C60003C045 /* AppDelegate.swift */; };@@ -70,6 +71,7 @@B20100000000000000000004 /* HarnessKeymap.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HarnessKeymap.swift; sourceTree = "<group>"; };B20100000000000000000A02 /* HarnessNotifications.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HarnessNotifications.swift; sourceTree = "<group>"; };B20100000000000000000B02 /* HarnessStatusMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HarnessStatusMenu.swift; sourceTree = "<group>"; };+ B20100000000000000000C02 /* SurfaceCacheGuard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SurfaceCacheGuard.swift; sourceTree = "<group>"; };331C80D5294CF71000263BE5 /* RunnerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RunnerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };331C80D7294CF71000263BE5 /* RunnerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RunnerTests.swift; sourceTree = "<group>"; };333000ED22D3DE5D00554162 /* Warnings.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Warnings.xcconfig; sourceTree = "<group>"; };@@ -183,6 +185,7 @@B20100000000000000000004 /* HarnessKeymap.swift */,B20100000000000000000A02 /* HarnessNotifications.swift */,B20100000000000000000B02 /* HarnessStatusMenu.swift */,+ B20100000000000000000C02 /* SurfaceCacheGuard.swift */,33E51913231747F40026EE4D /* DebugProfile.entitlements */,33E51914231749380026EE4D /* Release.entitlements */,33CC11242044D66E0003C045 /* Resources */,@@ -375,6 +378,7 @@B20100000000000000000003 /* HarnessKeymap.swift in Sources */,B20100000000000000000A01 /* HarnessNotifications.swift in Sources */,B20100000000000000000B01 /* HarnessStatusMenu.swift in Sources */,+ B20100000000000000000C01 /* SurfaceCacheGuard.swift in Sources */,33CC10F12044A3C60003C045 /* AppDelegate.swift in Sources */,335BBD1B22A9A15E00E9071D /* GeneratedPluginRegistrant.swift in Sources */,);diff --git a/desktop/macos/Runner/MainFlutterWindow.swift b/desktop/macos/Runner/MainFlutterWindow.swiftindex 93ef9c045..4c343283c 100644--- a/desktop/macos/Runner/MainFlutterWindow.swift+++ b/desktop/macos/Runner/MainFlutterWindow.swift@@ -28,6 +28,9 @@ class MainFlutterWindow: NSWindow {private var notifications: HarnessNotifications?override func awakeFromNib() {+ // Before the engine exists: its first frame already draws from the+ // back-buffer cache this patches (flutter/flutter#185394).+ SurfaceCacheGuard.install()let flutterViewController = FlutterViewController()let windowFrame = self.frameself.contentViewController = flutterViewControllerdiff --git a/desktop/macos/Runner/SurfaceCacheGuard.swift b/desktop/macos/Runner/SurfaceCacheGuard.swiftnew file mode 100644index 000000000..3b9bd32e0--- /dev/null+++ b/desktop/macos/Runner/SurfaceCacheGuard.swift@@ -0,0 +1,105 @@+import Cocoa+import os++/// Stops Flutter's macOS back-buffer cache from handing a frame a surface of+/// another size — the raster-thread crash tracked upstream as+/// https://github.com/flutter/flutter/issues/185394 (open as of Flutter 3.47.2).+///+/// `-[FlutterBackBufferCache removeSurfaceForSize:]` (FlutterSurfaceManager.mm)+/// purges itself only when its FIRST surface has another size, then returns its+/// youngest free surface without looking at that one's size; `returnSurfaces:`+/// appends the previous frame's surfaces whatever theirs. So when the window+/// goes A → B → A — a live resize dragged back, a display unplugged and+/// replugged — while a web pane puts more than one layer on screen, a frame+/// drawn at A can be given a surface still sized B. Impeller wraps that texture+/// under the frame's size, the wrapped texture marks itself invalid, the render+/// target drops it without a word, and `Canvas::SetupRenderPass` dereferences+/// the missing color texture: EXC_BAD_ACCESS at 0x0 on io.flutter.raster.+/// Skia, which Intel builds use, is handed the same wrong-size texture.+///+/// The replacement below turns such a surface away: it empties the cache and+/// answers nil, which is an ordinary cache miss — the surface manager then+/// allocates a fresh surface of the requested size on its own path, with its+/// own device and color space. A surface of the right size passes through+/// untouched, so a normal frame costs one size comparison.+///+/// The cache is engine-private, so every class, selector and type encoding is+/// checked first; a Flutter release that renames or reshapes any of them leaves+/// the cache as it was and says so in the log. Delete this file once the pinned+/// Flutter carries the upstream fix.+enum SurfaceCacheGuard {+ private static let log = Logger(+ subsystem: Bundle.main.bundleIdentifier ?? "ai.autonomous.harness",+ category: "SurfaceCacheGuard"+ )+ private static var installed = false++ private typealias RemoveSurface = @convention(c) (AnyObject, Selector, CGSize) -> AnyObject?+ private typealias Flush = @convention(c) (AnyObject, Selector) -> Void+ private typealias SurfaceSize = @convention(c) (AnyObject, Selector) -> CGSize++ /// Call before the first FlutterViewController exists: the engine's first+ /// frame already draws from this cache. Main thread only; repeat calls are+ /// no-ops.+ static func install() {+ dispatchPrecondition(condition: .onQueue(.main))+ guard !installed else { return }+ installed = true++ let removeSelector = NSSelectorFromString("removeSurfaceForSize:")+ let flushSelector = NSSelectorFromString("flush")+ let sizeSelector = NSSelectorFromString("size")+ let sizeType = String(cString: NSValue(size: .zero).objCType)+ guard+ let cacheClass = NSClassFromString("FlutterBackBufferCache"),+ let surfaceClass = NSClassFromString("FlutterSurface"),+ let removeMethod = class_getInstanceMethod(cacheClass, removeSelector),+ let flushMethod = class_getInstanceMethod(cacheClass, flushSelector),+ let sizeMethod = class_getInstanceMethod(surfaceClass, sizeSelector),+ types(of: removeMethod) == ["@", "@", ":", sizeType],+ types(of: flushMethod) == ["v", "@", ":"],+ types(of: sizeMethod) == [sizeType, "@", ":"]+ else {+ log.error("Not installed: Flutter's back-buffer cache API has changed")+ return+ }++ let original = unsafeBitCast(method_getImplementation(removeMethod), to: RemoveSurface.self)+ let flush = unsafeBitCast(method_getImplementation(flushMethod), to: Flush.self)+ let surfaceSize = unsafeBitCast(method_getImplementation(sizeMethod), to: SurfaceSize.self)++ // Runs on the raster thread, once per backing store a frame asks for.+ let guarded: @convention(block) (AnyObject, CGSize) -> AnyObject? = { cache, size in+ guard let surface = original(cache, removeSelector, size) else { return nil }+ // Only a FlutterSurface is known to answer `size` through that IMP.+ guard let object = surface as? NSObject, object.isKind(of: surfaceClass) else {+ return surface+ }+ let actual = surfaceSize(object, sizeSelector)+ if actual == size { return surface }+ // The rest of the cache is likely just as stale; a miss on every+ // request until the next commit refills it with current-size surfaces.+ flush(cache, flushSelector)+ log.notice(+ "Dropped a \(Int(actual.width))x\(Int(actual.height)) back buffer for a \(Int(size.width))x\(Int(size.height)) frame"+ )+ return nil+ }+ method_setImplementation(removeMethod, imp_implementationWithBlock(guarded))+ log.info("Installed")+ }++ /// The method's return type followed by its argument types, self and _cmd+ /// included.+ private static func types(of method: Method) -> [String] {+ let returnType = method_copyReturnType(method)+ var types = [String(cString: returnType)]+ free(returnType)+ for index in 0..<method_getNumberOfArguments(method) {+ guard let argumentType = method_copyArgumentType(method, index) else { return [] }+ types.append(String(cString: argumentType))+ free(argumentType)+ }+ return types+ }+}