public struct DriverKeyLedger: Sendable, Equatable { public struct Release: Sendable, Equatable { public let key: UInt16 public let target: DriverInputTarget public init(key: UInt16, target: DriverInputTarget) { self.key = key self.target = target } } private var pending: [Release] = [] public private(set) var touchedKeys: Set = [] public init() {} public mutating func keyDown(_ key: UInt16, target: DriverInputTarget) { touchedKeys.insert(key) pending.append(Release(key: key, target: target)) } public mutating func keyUp(_ key: UInt16, target: DriverInputTarget) { if let index = pending.lastIndex(where: { $0.key == key && $0.target == target }) { pending.remove(at: index) } } public var releases: [Release] { pending.reversed() } public func requireBalanced() throws { guard pending.isEmpty else { throw DriverError("Safety stop: driver owes key-up for keys \(pending.map(\.key).sorted())") } } }