@@ -2388,7 +2388,7 @@ struct GatewayNodeSessionTests {
23882388
23892389 @ Test
23902390 func `node invoke negotiation carries authoritative session envelopes` ( ) async throws {
2391- let session = FakeGatewayWebSocketSession ( )
2391+ let session = FakeGatewayWebSocketSession ( protocolFeaturesAutoResponse : false )
23922392 let gateway = GatewayNodeSession ( )
23932393 let capture = SessionKeyEnvelopeCapture ( )
23942394 let options = GatewayConnectOptions (
@@ -2423,9 +2423,9 @@ struct GatewayNodeSessionTests {
24232423 " node-invoke-session-key-envelope-v1 " ,
24242424 ] )
24252425
2426- try await waitUntil ( " receive loop ready for attributed invoke " ) {
2427- task . hasPendingReceiveHandler ( )
2428- }
2426+ try task . emitResponse (
2427+ id : # require ( publication [ " id " ] as? String ) ,
2428+ payload : [ " ok " : true ] )
24292429 task . emitInvokeRequest (
24302430 id : " attributed " ,
24312431 command : " mcp.tools.call.v1 " ,
@@ -2458,7 +2458,7 @@ struct GatewayNodeSessionTests {
24582458
24592459 @ Test
24602460 func `node invoke receipt timeout dispatch preserves authoritative session envelopes` ( ) async throws {
2461- let session = FakeGatewayWebSocketSession ( )
2461+ let session = FakeGatewayWebSocketSession ( protocolFeaturesAutoResponse : false )
24622462 let gateway = GatewayNodeSession ( )
24632463 let capture = SessionKeyEnvelopeCapture ( )
24642464 let options = nodeConnectOptions (
@@ -2480,7 +2480,11 @@ struct GatewayNodeSessionTests {
24802480 task . sentRequestCount ( method : " node.protocolFeatures.update " ) == 1 &&
24812481 task . hasPendingReceiveHandler ( )
24822482 }
2483+ let publication = try #require ( task . sentRequests ( method : " node.protocolFeatures.update " ) . first )
24832484
2485+ try task . emitResponse (
2486+ id : #require ( publication [ " id " ] as? String ) ,
2487+ payload : [ " ok " : true ] )
24842488 task . emitInvokeRequest (
24852489 id : " computer-attributed " ,
24862490 command : " computer.act " ,
@@ -2516,11 +2520,8 @@ struct GatewayNodeSessionTests {
25162520
25172521 @ Test
25182522 func `node invoke negotiation resets legacy fallback after reconnect` ( ) async throws {
2519- let legacySession = FakeGatewayWebSocketSession ( protocolFeaturesError : [
2520- " code " : " INVALID_REQUEST " ,
2521- " message " : " unknown method: node.protocolFeatures.update " ,
2522- ] )
2523- let currentSession = FakeGatewayWebSocketSession ( )
2523+ let legacySession = FakeGatewayWebSocketSession ( protocolFeaturesAutoResponse : false )
2524+ let currentSession = FakeGatewayWebSocketSession ( protocolFeaturesAutoResponse : false )
25242525 let gateway = GatewayNodeSession ( )
25252526 let capture = SessionKeyEnvelopeCapture ( )
25262527 let options = GatewayConnectOptions (
@@ -2551,6 +2552,14 @@ struct GatewayNodeSessionTests {
25512552 legacyTask . sentRequestCount ( method : " node.protocolFeatures.update " ) == 1 &&
25522553 legacyTask . hasPendingReceiveHandler ( )
25532554 }
2555+ let legacyPublication = try #require (
2556+ legacyTask . sentRequests ( method : " node.protocolFeatures.update " ) . first )
2557+ try legacyTask . emitError (
2558+ id : #require ( legacyPublication [ " id " ] as? String ) ,
2559+ error : [
2560+ " code " : " INVALID_REQUEST " ,
2561+ " message " : " unknown method: node.protocolFeatures.update " ,
2562+ ] )
25542563 legacyTask . emitInvokeRequest ( id : " legacy " , command : " mcp.tools.call.v1 " )
25552564 try await waitUntil ( " legacy envelope delivered " ) {
25562565 await capture . all ( ) . count == 1
@@ -2569,6 +2578,11 @@ struct GatewayNodeSessionTests {
25692578 currentTask . sentRequestCount ( method : " node.protocolFeatures.update " ) == 1 &&
25702579 currentTask . hasPendingReceiveHandler ( )
25712580 }
2581+ let currentPublication = try #require (
2582+ currentTask . sentRequests ( method : " node.protocolFeatures.update " ) . first )
2583+ try currentTask . emitResponse (
2584+ id : #require ( currentPublication [ " id " ] as? String ) ,
2585+ payload : [ " ok " : true ] )
25722586 currentTask . emitInvokeRequest ( id : " current " , command : " mcp.tools.call.v1 " )
25732587 try await waitUntil ( " current envelope delivered " ) {
25742588 await capture . all ( ) . count == 2
@@ -2583,10 +2597,7 @@ struct GatewayNodeSessionTests {
25832597
25842598 @ Test
25852599 func `node invoke negotiation fails closed on non compatibility errors` ( ) async throws {
2586- let session = FakeGatewayWebSocketSession ( protocolFeaturesError : [
2587- " code " : " UNAVAILABLE " ,
2588- " message " : " temporary failure " ,
2589- ] )
2600+ let session = FakeGatewayWebSocketSession ( protocolFeaturesAutoResponse : false )
25902601 let gateway = GatewayNodeSession ( )
25912602 let capture = SessionKeyEnvelopeCapture ( )
25922603 let options = GatewayConnectOptions (
@@ -2616,6 +2627,13 @@ struct GatewayNodeSessionTests {
26162627 task . sentRequestCount ( method : " node.protocolFeatures.update " ) == 1 &&
26172628 task . hasPendingReceiveHandler ( )
26182629 }
2630+ let publication = try #require ( task . sentRequests ( method : " node.protocolFeatures.update " ) . first )
2631+ try task . emitError (
2632+ id : #require ( publication [ " id " ] as? String ) ,
2633+ error : [
2634+ " code " : " UNAVAILABLE " ,
2635+ " message " : " temporary failure " ,
2636+ ] )
26192637 task . emitInvokeRequest ( id : " fail-closed " , command : " mcp.tools.call.v1 " )
26202638 try await waitUntil ( " fail-closed envelope delivered " ) {
26212639 await capture . all ( ) . count == 1
@@ -3037,6 +3055,53 @@ struct GatewayNodeSessionTests {
30373055 #expect ( await freshProbe . count ( ) == 1 )
30383056 }
30393057
3058+ @ Test
3059+ func `stale receipt retry dispatches exactly once without a deadline` ( ) async throws {
3060+ let gateway = GatewayNodeSession ( )
3061+ let staleGate = AsyncGate ( )
3062+ let freshProbe = ComputerInvokeProbe ( )
3063+ let paramsJSON = #"{"action":"type","text":"hello"}"#
3064+ let key = " computer.act:v1:stale-no-deadline "
3065+ let scope = " gateway:stale-no-deadline "
3066+ let stale = Task {
3067+ await gateway . invokeComputerWithReceiptForTesting (
3068+ requestId : " stale " ,
3069+ paramsJSON : paramsJSON ,
3070+ idempotencyKey : key ,
3071+ receiptScope : scope ,
3072+ onInvoke : { request in
3073+ await staleGate . wait ( )
3074+ return GatewayNodeSession . staleRouteInvokeResponse ( requestId : request . id )
3075+ } )
3076+ }
3077+ try await waitUntil ( " stale receipt without deadline is in flight " ) {
3078+ await staleGate . hasStarted ( )
3079+ }
3080+
3081+ let replay = Task {
3082+ await gateway . invokeComputerWithReceiptForTesting (
3083+ requestId : " replay " ,
3084+ paramsJSON : paramsJSON ,
3085+ idempotencyKey : key ,
3086+ receiptScope : scope ,
3087+ onInvoke : { request in await freshProbe . execute ( request ) } )
3088+ }
3089+ try await waitUntil ( " no-deadline replay joined the stale receipt " ) {
3090+ await gateway . computerReceiptJoinCountForTesting (
3091+ idempotencyKey : key ,
3092+ receiptScope : scope ) == 1
3093+ }
3094+ await staleGate . release ( )
3095+ try await waitUntil ( " no-deadline retry dispatched once " ) {
3096+ await freshProbe . count ( ) == 1
3097+ }
3098+ await freshProbe . release ( )
3099+
3100+ #expect ( await stale . value . ok == false )
3101+ #expect ( await replay . value . ok )
3102+ #expect ( await freshProbe . count ( ) == 1 )
3103+ }
3104+
30403105 @ Test
30413106 func `stale receipt retry preserves the original invoke deadline` ( ) async throws {
30423107 let gateway = GatewayNodeSession ( )
@@ -3074,22 +3139,13 @@ struct GatewayNodeSessionTests {
30743139 idempotencyKey : key ,
30753140 receiptScope : scope ) == 1
30763141 }
3077- let releaseStale = Task {
3078- try? await Task . sleep ( for : . milliseconds ( 60 ) )
3079- await staleGate . release ( )
3080- }
3081- let releaseFresh = Task {
3082- try? await Task . sleep ( for : . milliseconds ( 120 ) )
3083- await freshProbe . release ( )
3084- }
3085-
3142+ await staleGate . release ( )
30863143 let response = await replay . value
30873144 #expect ( ! response . ok )
30883145 #expect ( response . error ? . message == " node invoke timed out " )
3089- _ = await releaseStale . value
3090- _ = await releaseFresh . value
3146+ await freshProbe . release ( )
30913147 #expect ( await stale . value . ok == false )
3092- #expect ( await freshProbe . count ( ) = = 1 )
3148+ #expect ( await freshProbe . count ( ) < = 1 )
30933149 }
30943150
30953151 @ Test
0 commit comments