You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix sidecar state restoration bug in SetCurrentState method
Add state inheritance options for sidecar conversations
Introduce SideCarOptions model for configuration control
Enhance SendMessage method with optional parameters
Changes diagram
flowchart LR
A["SideCarOptions"] --> B["SendMessage Method"]
B --> C["RestoreStates Logic"]
C --> D["State Inheritance"]
E["SetCurrentState Fix"] --> C
The RestoreStates method modifies the innerStates object directly, which is assigned from prevStates parameter. This could lead to unintended side effects as the original state object is being mutated instead of creating a proper copy.
The _sideCarOptions field is stored as an instance variable but could be accessed concurrently by multiple threads. This could lead to race conditions where options from one request affect another concurrent request.
In the RestoreStates method, there's potential for null reference exceptions when accessing pair.Value.Values.LastOrDefault() and subsequent operations on endNode without proper null checks on the collection itself.
The _sideCarOptions field is not reset after method execution, which could cause options from previous calls to persist. Reset the field in the AfterExecute method or use a local variable instead.
Why: The suggestion correctly identifies a state leakage bug where _sideCarOptions persists across calls, potentially causing incorrect behavior in subsequent executions. This is a critical fix for correctness.
High
Possible issue
Avoid mutating original state object
The method directly modifies prevStates by assigning it to innerStates, which can cause unintended side effects. Create a deep copy of prevStates to avoid mutating the original state object.
private void RestoreStates(ConversationState prevStates)
{
- var innerStates = prevStates;+ var innerStates = new ConversationState(prevStates.Values.ToList());
var state = _services.GetRequiredService<IConversationStateService>();
if (_sideCarOptions?.IsInheritStates == true)
{
var curStates = state.GetCurrentState();
foreach (var pair in curStates)
{
var endNode = pair.Value.Values.LastOrDefault();
if (endNode == null) continue;
if (_sideCarOptions?.InheritStateKeys?.Any() == true
&& !_sideCarOptions.InheritStateKeys.Contains(pair.Key))
{
continue;
}
if (innerStates.ContainsKey(pair.Key))
{
innerStates[pair.Key].Values.Add(endNode);
}
else
{
innerStates[pair.Key] = new StateKeyValue
{
Key = pair.Key,
Versioning = pair.Value.Versioning,
Readonly = pair.Value.Readonly,
Values = [endNode]
};
}
}
}
state.SetCurrentState(innerStates);
}
Apply / Chat
Suggestion importance[1-10]: 8
__
Why: The suggestion correctly identifies that prevStates is mutated, which can cause unintended side effects, and proposes creating a copy to ensure the method is pure regarding its input parameter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Type
Bug fix, Enhancement
Description
Fix sidecar state restoration bug in
SetCurrentStatemethodAdd state inheritance options for sidecar conversations
Introduce
SideCarOptionsmodel for configuration controlEnhance
SendMessagemethod with optional parametersChanges diagram
Changes walkthrough 📝
IConversationSideCar.cs
Update interface with SideCarOptions parametersrc/Infrastructure/BotSharp.Abstraction/SideCar/IConversationSideCar.cs
SideCarOptionsparameter toSendMessagemethodSideCar.ModelsnamespaceSideCarOptions.cs
Add SideCarOptions configuration modelsrc/Infrastructure/BotSharp.Abstraction/SideCar/Models/SideCarOptions.cs
SideCarOptionsclassIsInheritStatesandInheritStateKeyspropertiesEmpty()factory methodBotSharpConversationSideCar.cs
Implement state inheritance and restoration logicsrc/Infrastructure/BotSharp.Core.SideCar/Services/BotSharpConversationSideCar.cs
_sideCarOptionsfield for configuration storageRestoreStatesmethod with inheritance logicSendMessageto accept and store optionsUsing.cs
Add SideCar models namespace importsrc/Infrastructure/BotSharp.Core.SideCar/Using.cs
BotSharp.Abstraction.SideCar.ModelsConversationStateService.cs
Fix SetCurrentState parameter bugsrc/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs
SetCurrentStatemethod parameter usage_curStates.Valuestostate.Values