@@ -18,6 +18,8 @@ protocol MacNodeHostWorking: Sendable {
1818 func start ( command : [ String ] ) async throws -> MacNodeHostManifest
1919 func supports ( _ command : String ) async -> Bool
2020 func invoke ( _ request : BridgeInvokeRequest ) async -> BridgeInvokeResponse
21+ func handleInput ( invokeId : String , seq : Int , payloadJSON : String ) async
22+ func cancel ( invokeId : String ) async
2123 func setRoute ( _ route : GatewayNodeSessionRoute ? , authorityGeneration : UInt64 ) async -> Bool
2224 func publishInventory ( ifCurrentRoute route : GatewayNodeSessionRoute ) async
2325 func stop ( ) async
@@ -28,6 +30,13 @@ protocol MacNodeHostWorking: Sendable {
2830/// and keeps TCC-sensitive execution behind the native exec-host socket.
2931final class MacNodeHostWorker : MacNodeHostWorking , @unchecked Sendable {
3032 nonisolated static let defaultStartupTimeout : TimeInterval = 300
33+ private static let maxPendingInvokeControlIDs = 32
34+ private static let maxPendingInvokeControlsPerID = 64
35+
36+ private enum PendingInvokeControl {
37+ case input ( seq : Int , payloadJSON : String )
38+ case cancel
39+ }
3140
3241 enum WorkerError : LocalizedError {
3342 case unavailable ( String )
@@ -60,6 +69,8 @@ final class MacNodeHostWorker: MacNodeHostWorking, @unchecked Sendable {
6069 private var routeAuthorityGeneration : UInt64 = 0
6170 private var startContinuation : CheckedContinuation < MacNodeHostManifest , Error > ?
6271 private var invokeContinuations : [ String : CheckedContinuation < BridgeInvokeResponse , Never > ] = [ : ]
72+ private var pendingInvokeControls : [ String : [ PendingInvokeControl ] ] = [ : ]
73+ private var pendingInvokeControlOrder : [ String ] = [ ]
6374 private var startTimer : DispatchSourceTimer ?
6475 private var eventDeliveryTask : Task < Void , Never > ?
6576 private var inventoryPublicationTask : Task < Void , Never > ?
@@ -131,6 +142,9 @@ final class MacNodeHostWorker: MacNodeHostWorking, @unchecked Sendable {
131142 " type " : " invoke " ,
132143 " request " : workerRequest ,
133144 ] )
145+ for control in self . takePendingInvokeControlsLocked ( invokeId : request . id ) {
146+ try self . enqueueInvokeControlLocked ( control , invokeId : request . id )
147+ }
134148 } catch {
135149 self . invokeContinuations . removeValue ( forKey : request . id ) ? . resume ( returning :
136150 Self . unavailableResponse ( request . id , " UNAVAILABLE: node-host worker write failed " ) )
@@ -139,6 +153,83 @@ final class MacNodeHostWorker: MacNodeHostWorking, @unchecked Sendable {
139153 }
140154 }
141155
156+ func handleInput ( invokeId : String , seq : Int , payloadJSON : String ) async {
157+ await withCheckedContinuation { continuation in
158+ self . queue . async {
159+ let control = PendingInvokeControl . input ( seq : seq , payloadJSON : payloadJSON )
160+ if self . invokeContinuations [ invokeId ] != nil {
161+ try? self . enqueueInvokeControlLocked ( control , invokeId : invokeId )
162+ } else if self . process ? . isRunning == true , self . manifest != nil {
163+ self . bufferInvokeControlLocked ( control , invokeId : invokeId )
164+ }
165+ continuation . resume ( )
166+ }
167+ }
168+ }
169+
170+ func cancel ( invokeId : String ) async {
171+ await withCheckedContinuation { continuation in
172+ self . queue . async {
173+ let control = PendingInvokeControl . cancel
174+ if self . invokeContinuations [ invokeId ] != nil {
175+ try? self . enqueueInvokeControlLocked ( control , invokeId : invokeId )
176+ } else if self . process ? . isRunning == true , self . manifest != nil {
177+ self . bufferInvokeControlLocked ( control , invokeId : invokeId )
178+ }
179+ continuation . resume ( )
180+ }
181+ }
182+ }
183+
184+ private func bufferInvokeControlLocked ( _ control : PendingInvokeControl , invokeId : String ) {
185+ // Gateway control events can overtake detached invoke dispatch. Keep the
186+ // short race window bounded, then flush controls after the invoke frame.
187+ if self . pendingInvokeControls [ invokeId ] == nil {
188+ if self . pendingInvokeControlOrder . count >= Self . maxPendingInvokeControlIDs ,
189+ let oldest = self . pendingInvokeControlOrder . first
190+ {
191+ self . pendingInvokeControlOrder . removeFirst ( )
192+ self . pendingInvokeControls . removeValue ( forKey : oldest )
193+ }
194+ self . pendingInvokeControlOrder . append ( invokeId )
195+ self . pendingInvokeControls [ invokeId ] = [ ]
196+ }
197+ var controls = self . pendingInvokeControls [ invokeId ] ?? [ ]
198+ if controls . contains ( where : {
199+ if case . cancel = $0 { return true }
200+ return false
201+ } ) {
202+ return
203+ }
204+ if controls . count >= Self . maxPendingInvokeControlsPerID {
205+ controls . removeFirst ( )
206+ }
207+ controls . append ( control )
208+ self . pendingInvokeControls [ invokeId ] = controls
209+ }
210+
211+ private func takePendingInvokeControlsLocked ( invokeId : String ) -> [ PendingInvokeControl ] {
212+ self . pendingInvokeControlOrder . removeAll { $0 == invokeId }
213+ return self . pendingInvokeControls . removeValue ( forKey : invokeId ) ?? [ ]
214+ }
215+
216+ private func enqueueInvokeControlLocked ( _ control : PendingInvokeControl , invokeId : String ) throws {
217+ switch control {
218+ case let . input ( seq , payloadJSON ) :
219+ try self . enqueueWriteLocked ( [
220+ " type " : " invoke-input " ,
221+ " invokeId " : invokeId ,
222+ " seq " : seq ,
223+ " payloadJSON " : payloadJSON ,
224+ ] )
225+ case . cancel :
226+ try self . enqueueWriteLocked ( [
227+ " type " : " invoke-cancel " ,
228+ " invokeId " : invokeId ,
229+ ] )
230+ }
231+ }
232+
142233 func setRoute ( _ route : GatewayNodeSessionRoute ? , authorityGeneration : UInt64 ) async -> Bool {
143234 await withCheckedContinuation { continuation in
144235 self . queue . async {
@@ -561,6 +652,8 @@ final class MacNodeHostWorker: MacNodeHostWorking, @unchecked Sendable {
561652 }
562653 let pending = self . invokeContinuations
563654 self . invokeContinuations . removeAll ( )
655+ self . pendingInvokeControls . removeAll ( )
656+ self . pendingInvokeControlOrder . removeAll ( )
564657 for ( id , continuation ) in pending {
565658 continuation . resume ( returning : Self . unavailableResponse ( id , " UNAVAILABLE: node-host worker stopped " ) )
566659 }
0 commit comments