import Foundation public import ParcelCore public import ParcelPlatform extension ParcelEngine { public func perform( _ command: Command, targeting window: WindowID, workspace: WorkspaceKey? = nil, activitySource: ActivitySource = .other ) async throws -> EngineCommandResult { guard isRunning else { throw EngineError.stopped } guard !spaceCommandRunning else { throw spaceFailure(.busy) } try Task.checkCancellation() let id = UUID() let captured = workspace ?? snapshot.state.workspace(containing: window) try requireCommandDisplay(command, display: captured?.display, window: window) return try await withTaskCancellationHandler { try await withCheckedThrowingContinuation { reply in guard !Task.isCancelled else { reply.resume(throwing: CancellationError()) return } queue.append( Pending( event: .commandForWindow(window, command, workspace: captured), reply: reply, commandID: id, activitySource: activitySource)) scheduleTurn() } } onCancel: { Task { @MainActor [weak self] in self?.cancelCommand(id) } } } }