diff --git a/src/Managing.Application/Abstractions/Grains/IAgentGrain.cs b/src/Managing.Application/Abstractions/Grains/IAgentGrain.cs
index 6f9d1b06..c5bedcc4 100644
--- a/src/Managing.Application/Abstractions/Grains/IAgentGrain.cs
+++ b/src/Managing.Application/Abstractions/Grains/IAgentGrain.cs
@@ -11,6 +11,12 @@ namespace Managing.Application.Abstractions.Grains
/// The display name of the agent.
Task InitializeAsync(int userId, string agentName);
+ ///
+ /// Updates only the agent name without recalculating summary.
+ ///
+ /// The new agent name.
+ Task UpdateAgentNameAsync(string agentName);
+
///
/// Generates a summary of the agent's stats for the AgentRegistryGrain.
///
diff --git a/src/Managing.Application/Bots/Grains/AgentGrain.cs b/src/Managing.Application/Bots/Grains/AgentGrain.cs
index 4dd2dc1d..87ec31d9 100644
--- a/src/Managing.Application/Bots/Grains/AgentGrain.cs
+++ b/src/Managing.Application/Bots/Grains/AgentGrain.cs
@@ -51,8 +51,33 @@ public class AgentGrain : Grain, IAgentGrain
{
_state.State.AgentName = agentName;
await _state.WriteStateAsync();
- await UpdateSummary();
- _logger.LogInformation("Agent {UserId} initialized with name {AgentName}", userId, agentName);
+
+ // Create an empty AgentSummary for the new agent
+ var emptySummary = new AgentSummary
+ {
+ UserId = userId,
+ AgentName = agentName,
+ TotalPnL = 0,
+ TotalROI = 0,
+ Wins = 0,
+ Losses = 0,
+ Runtime = null,
+ CreatedAt = DateTime.UtcNow,
+ UpdatedAt = DateTime.UtcNow,
+ ActiveStrategiesCount = 0,
+ TotalVolume = 0,
+ TotalBalance = 0
+ };
+
+ await _agentService.SaveOrUpdateAgentSummary(emptySummary);
+ _logger.LogInformation("Agent {UserId} initialized with name {AgentName} and empty summary", userId, agentName);
+ }
+
+ public async Task UpdateAgentNameAsync(string agentName)
+ {
+ _state.State.AgentName = agentName;
+ await _state.WriteStateAsync();
+ _logger.LogInformation("Agent {UserId} updated with name {AgentName}", this.GetPrimaryKeyLong(), agentName);
}
public async Task OnAgentSummaryUpdateAsync(AgentSummaryUpdateEvent updateEvent)
diff --git a/src/Managing.Application/Users/UserService.cs b/src/Managing.Application/Users/UserService.cs
index 565deda4..bc5abd27 100644
--- a/src/Managing.Application/Users/UserService.cs
+++ b/src/Managing.Application/Users/UserService.cs
@@ -109,6 +109,20 @@ public class UserService : IUserService
{
account
};
+
+ // Initialize AgentGrain for new user (with empty agent name initially)
+ try
+ {
+ var agentGrain = _grainFactory.GetGrain(user.Id);
+ await agentGrain.InitializeAsync(user.Id, string.Empty);
+ _logger.LogInformation("AgentGrain initialized for new user {UserId}", user.Id);
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError(ex, "Failed to initialize AgentGrain for new user {UserId}", user.Id);
+ SentrySdk.CaptureException(ex);
+ // Don't throw here to avoid breaking the user creation process
+ }
}
return user;
@@ -165,17 +179,16 @@ public class UserService : IUserService
user.AgentName = agentName;
await _userRepository.SaveOrUpdateUserAsync(user);
- // Initialize the AgentGrain for this user
+ // Update the AgentGrain with the new agent name (lightweight operation)
try
{
var agentGrain = _grainFactory.GetGrain(user.Id);
- await agentGrain.InitializeAsync(user.Id, agentName);
- _logger.LogInformation("AgentGrain initialized for user {UserId} with agent name {AgentName}", user.Id,
- agentName);
+ await agentGrain.UpdateAgentNameAsync(agentName);
+ _logger.LogInformation("AgentGrain updated for user {UserId} with agent name {AgentName}", user.Id, agentName);
}
catch (Exception ex)
{
- _logger.LogError(ex, "Failed to initialize AgentGrain for user {UserId} with agent name {AgentName}",
+ _logger.LogError(ex, "Failed to update AgentGrain for user {UserId} with agent name {AgentName}",
user.Id, agentName);
// Don't throw here to avoid breaking the user update process
}