import AppKit import Foundation @MainActor func checkApplicationRule(_ session: RealitySession) async throws { let bundleID = RealApp.textEdit.bundleID guard !session.ignoredBundles.contains(bundleID), NSRunningApplication.runningApplications(withBundleIdentifier: bundleID).isEmpty else { throw RealitySkip("Application-wide rule requires no existing TextEdit process") } try session.configure() let owned = try await session.startingWindows([.textEdit, .finder]) try await session.start() try await session.tiles(owned) let before = try session.readFrames(owned) let gaps = try session.configuredLayout().gaps let processes = NSRunningApplication.runningApplications(withBundleIdentifier: bundleID) guard processes.allSatisfy({ session.apps.ownedProcessIDs.contains($0.processIdentifier) }) else { throw RealityError("Safety stop: an unowned process appeared before the application-wide rule") } var configuration = try Data(contentsOf: session.config) configuration.append(Data("\n[[rules]]\nbundle-id = \(RealityProcess.quoted(bundleID))\naction = \"float\"\n".utf8)) try configuration.write(to: session.config, options: .atomic) let result = try await session.cli(["reload", "--json"]) guard result.status == 0 else { throw RealityError("Application rule reload failed: \(result.output) \(result.errors)") } struct Reload: Decodable { let reloaded: Bool; let path: String } let receipt = try JSONDecoder().decode(Reload.self, from: Data(result.output.utf8)) guard receipt.reloaded, receipt.path == session.config.path else { throw RealityError("Application rule reload did not acknowledge the isolated configuration") } try await session.expect("The chosen application keeps its real frame while Finder expands") { let frames = try FrameOracle.frames($0, ids: owned.map(\.id)) return frames[0].matches(before[0], tolerance: 1) && frames[1].matches(session.visible.inset(gaps.outer), tolerance: 1) } try await session.command("retile") try await session.expect("Retile leaves the chosen application floating") { let frames = try FrameOracle.frames($0, ids: owned.map(\.id)) return frames[0].matches(before[0], tolerance: 1) && frames[1].matches(session.visible.inset(gaps.outer), tolerance: 1) } }