@@ -111,6 +111,7 @@ public class ChatView extends ViewPart implements ChatProgressListener, MessageL
111111 private String conversationId = "" ;
112112 private String subagentConversationId = null ;
113113 private ConversationState conversationState = ConversationState .NEW_CONVERSATION ;
114+ private CompletableFuture <?> persistUserTurnFuture = CompletableFuture .completedFuture (null );
114115 private Set <CompletableFuture <?>> conversationFutures = new HashSet <>();
115116 private IEventBroker eventBroker = PlatformUI .getWorkbench ().getService (IEventBroker .class );
116117 private DragReferenceManager dragReferenceManager ;
@@ -796,9 +797,12 @@ public void onChatProgress(ChatProgressValue value) {
796797 // Cache conversation progress on begin
797798 persistenceManager .cacheConversationProgress (this .conversationId , value );
798799
799- // Set the CLS-assigned turnId on the last user turn that doesn't have one yet
800+ // Set the CLS-assigned turnId on the last user turn that doesn't have one yet.
801+ // Chain off persistUserTurnFuture to ensure the UserTurnData has been created first.
800802 if (StringUtils .isNotBlank (value .getTurnId ())) {
801- persistenceManager .setUserTurnId (this .conversationId , value .getTurnId ());
803+ final String convId = this .conversationId ;
804+ final String turnId = value .getTurnId ();
805+ persistUserTurnFuture .thenRun (() -> persistenceManager .setUserTurnId (convId , turnId ));
802806 }
803807
804808 // Hide handoff container when new turn starts
@@ -948,8 +952,8 @@ private void onSendInternal(String workDoneToken, String message, String agentSl
948952 if (conversationState == ConversationState .CONTINUED_CONVERSATION ) {
949953 // Continue existing conversation - persist user message and send to existing conversation
950954 if (persistenceManager != null ) {
951- persistenceManager .persistUserTurnInfo (conversationId , null , processedMessage , activeModel ,
952- chatModeName , customChatModeId , currentFile , references );
955+ this . persistUserTurnFuture = persistenceManager .persistUserTurnInfo (conversationId , null , processedMessage ,
956+ activeModel , chatModeName , customChatModeId , currentFile , references );
953957 }
954958
955959 // Get current todo list to sync with the server
@@ -994,11 +998,10 @@ private void onSendInternal(String workDoneToken, String message, String agentSl
994998 if (conversationState == ConversationState .NEW_HISTORY_BASED_CONVERSATION ) {
995999 // Load turns from the history conversation and persist user turn with current conversation ID
9961000 turns = persistenceManager .loadConversationTurns (this .conversationId );
997- persistenceManager .persistUserTurnInfo (this .conversationId , null , processedMessage , activeModel ,
998- chatModeName , customChatModeId , currentFile , references );
1001+ this . persistUserTurnFuture = persistenceManager .persistUserTurnInfo (this .conversationId , null ,
1002+ processedMessage , activeModel , chatModeName , customChatModeId , currentFile , references );
9991003
10001004 // Set conversationId and last completed turnId for CLS server-side session restoration.
1001- // Only use turnId from turns that have a response (completed turns), not cancelled ones.
10021005 restoredConversationId = this .conversationId ;
10031006 if (turns != null && !turns .isEmpty ()) {
10041007 for (int i = turns .size () - 1 ; i >= 0 ; i --) {
@@ -1018,8 +1021,8 @@ private void onSendInternal(String workDoneToken, String message, String agentSl
10181021 } else if (conversationState == ConversationState .NEW_CONVERSATION ) {
10191022 // Generate a temporary ID for brand new conversation and persist user turn
10201023 this .conversationId = UUID .randomUUID ().toString ();
1021- persistenceManager .persistUserTurnInfo (this .conversationId , null , processedMessage , activeModel ,
1022- chatModeName , customChatModeId , currentFile , references );
1024+ this . persistUserTurnFuture = persistenceManager .persistUserTurnInfo (this .conversationId , null ,
1025+ processedMessage , activeModel , chatModeName , customChatModeId , currentFile , references );
10231026 }
10241027
10251028 CompletableFuture <ChatCreateResult > createConversationFuture = null ;
0 commit comments