@@ -41,6 +41,8 @@ type Mysis struct {
4141 // For runtime tracking
4242 lastError error
4343 currentAccountUsername string
44+ currentPassword string // Password for current account
45+ currentSessionID string // Active session_id from login/register
4446 activityState ActivityState
4547 activityUntil time.Time
4648 lastServerTick int64
@@ -194,6 +196,20 @@ func (m *Mysis) CurrentAccountUsername() string {
194196 return a.currentAccountUsername
195197 }
196198
199+// CurrentPassword returns the password for the current account.
200+func (m *Mysis) CurrentPassword() string {
201+ m.mu.RLock()
202+ defer m.mu.RUnlock()
203+ return m.currentPassword
204+}
205+
206+// CurrentSessionID returns the active session_id.
207+func (m *Mysis) CurrentSessionID() string {
208+ m.mu.RLock()
209+ defer m.mu.RUnlock()
210+ return m.currentSessionID
211+}
212+
197213 // ActivityState returns the mysis current activity.
198214 func (m *Mysis) ActivityState() ActivityState {
199215 a := m
@@ -871,46 +887,139 @@ func (m *Mysis) executeToolCall(ctx context.Context, mcpProxy *mcp.Proxy, tc pro
871887 if err == nil && result != nil && !result.IsError {
872888 switch tc.Name {
873889 case "login", "register":
890+ // Extract username from arguments
874891 var args struct {
875892 Username string `json:"username"`
893+ Password string `json:"password"`
894+ }
895+ json.Unmarshal(tc.Arguments, &args)
896+
897+ // Extract session_id and password from tool result
898+ sessionID := extractSessionID(result)
899+ password := args.Password
900+
901+ // For register, password might be in result instead of args
902+ if password == "" && tc.Name == "register" {
903+ password = extractPassword(result)
876904 }
877- if err := json.Unmarshal(tc.Arguments, &args); err == nil {
878- a.setCurrentAccount(args.Username)
905+
906+ // Extract actual username from result (may differ from args due to pool account substitution)
907+ actualUsername := extractUsername(result)
908+ if actualUsername == "" {
909+ actualUsername = args.Username // Fallback to requested username
910+ }
911+
912+ if actualUsername != "" && sessionID != "" {
913+ a.setCurrentAccount(actualUsername, password, sessionID)
914+
915+ // Rebuild system prompt with new credentials
916+ if err := a.rebuildSystemMemory(); err != nil {
917+ log.Error().Err(err).Msg("failed to rebuild system memory after login")
918+ }
879919 }
880920 case "logout":
881921 a.releaseCurrentAccount()
922+
923+ // Rebuild system prompt to clear credentials
924+ if err := a.rebuildSystemMemory(); err != nil {
925+ log.Error().Err(err).Msg("failed to rebuild system memory after logout")
926+ }
882927 }
883928 }
884929
885930 return result, err
886931 }
887932
888-func (m *Mysis) setCurrentAccount(username string) {
889- a := m
933+func (m *Mysis) setCurrentAccount(username, password, sessionID string) {
934+ m.mu.Lock()
935+ defer m.mu.Unlock()
936+
937+ // Ignore empty username (no-op)
890938 if username == "" {
891939 return
892940 }
893941
894- a.mu.Lock()
895- previous := a.currentAccountUsername
896- a.currentAccountUsername = username
897- a.mu.Unlock()
898-
899- if previous != "" && previous != username {
900- _ = a.store.ReleaseAccount(previous)
942+ if m.currentAccountUsername != "" && m.currentAccountUsername != username {
943+ if err := m.store.ReleaseAccount(m.currentAccountUsername); err != nil {
944+ log.Error().Err(err).Str("username", m.currentAccountUsername).Msg("failed to release previous account")
945+ }
901946 }
947+
948+ m.currentAccountUsername = username
949+ m.currentPassword = password
950+ m.currentSessionID = sessionID
902951 }
903952
904953 func (m *Mysis) releaseCurrentAccount() {
905- a := m
906- a.mu.Lock()
907- username := a.currentAccountUsername
908- a.currentAccountUsername = ""
909- a.mu.Unlock()
954+ m.mu.Lock()
955+ defer m.mu.Unlock()
956+
957+ if m.currentAccountUsername != "" {
958+ if err := m.store.ReleaseAccount(m.currentAccountUsername); err != nil {
959+ log.Error().Err(err).Str("username", m.currentAccountUsername).Msg("failed to release account")
960+ }
961+ m.currentAccountUsername = ""
962+ m.currentPassword = ""
963+ m.currentSessionID = ""
964+ }
965+}
966+
967+// extractSessionID extracts session_id from tool result content
968+func extractSessionID(result *mcp.ToolResult) string {
969+ if result == nil || result.IsError {
970+ return ""
971+ }
972+
973+ for _, block := range result.Content {
974+ if block.Type == "text" {
975+ // Parse JSON from text block
976+ var data map[string]interface{}
977+ if err := json.Unmarshal([]byte(block.Text), &data); err == nil {
978+ if sessionID, ok := data["session_id"].(string); ok {
979+ return sessionID
980+ }
981+ }
982+ }
983+ }
984+ return ""
985+}
986+
987+// extractPassword extracts password from tool result (for register responses)
988+func extractPassword(result *mcp.ToolResult) string {
989+ if result == nil || result.IsError {
990+ return ""
991+ }
910992
911- if username != "" {
912- _ = a.store.ReleaseAccount(username)
993+ for _, block := range result.Content {
994+ if block.Type == "text" {
995+ var data map[string]interface{}
996+ if err := json.Unmarshal([]byte(block.Text), &data); err == nil {
997+ if password, ok := data["password"].(string); ok {
998+ return password
999+ }
1000+ }
1001+ }
9131002 }
1003+ return ""
1004+}
1005+
1006+// extractUsername extracts username from tool result (actual username after pool substitution)
1007+func extractUsername(result *mcp.ToolResult) string {
1008+ if result == nil || result.IsError {
1009+ return ""
1010+ }
1011+
1012+ for _, block := range result.Content {
1013+ if block.Type == "text" {
1014+ var data map[string]interface{}
1015+ if err := json.Unmarshal([]byte(block.Text), &data); err == nil {
1016+ if username, ok := data["username"].(string); ok {
1017+ return username
1018+ }
1019+ }
1020+ }
1021+ }
1022+ return ""
9141023 }
9151024
9161025 // formatToolCallsForStorage formats tool calls for storage in memory.
@@ -1568,27 +1677,60 @@ func (m *Mysis) buildSystemPrompt() string {
15681677 // Get most recent commander broadcast (sender_id is empty for commander broadcasts)
15691678 broadcasts, err := a.store.GetRecentBroadcasts(10) // Get more to filter for commander broadcasts
15701679 if err != nil {
1571- return strings.Replace(base, "{{LATEST_BROADCAST}}", constants.BroadcastFallback, 1)
1572- }
1680+ base = strings.Replace(base, "{{LATEST_BROADCAST}}", constants.BroadcastFallback, 1)
1681+ } else {
1682+ // Find the most recent commander broadcast (empty sender_id)
1683+ var commanderBroadcast *store.BroadcastMessage
1684+ for _, b := range broadcasts {
1685+ if b.SenderID == "" {
1686+ commanderBroadcast = b
1687+ break
1688+ }
1689+ }
15731690
1574- // Find the most recent commander broadcast (empty sender_id)
1575- var commanderBroadcast *store.BroadcastMessage
1576- for _, b := range broadcasts {
1577- if b.SenderID == "" {
1578- commanderBroadcast = b
1579- break
1691+ if commanderBroadcast == nil {
1692+ // No commander broadcasts yet - show fallback
1693+ base = strings.Replace(base, "{{LATEST_BROADCAST}}", constants.BroadcastFallback, 1)
1694+ } else {
1695+ // Format broadcast section (only content, no sender name)
1696+ broadcastSection := fmt.Sprintf(constants.BroadcastSectionTemplate, commanderBroadcast.Content)
1697+ base = strings.Replace(base, "{{LATEST_BROADCAST}}", broadcastSection, 1)
15801698 }
15811699 }
15821700
1583- if commanderBroadcast == nil {
1584- // No commander broadcasts yet - show fallback
1585- return strings.Replace(base, "{{LATEST_BROADCAST}}", constants.BroadcastFallback, 1)
1701+ prompt := base
1702+
1703+ // Replace {{ACCOUNT_DETAILS}} with current credentials
1704+ accountDetails := constants.AccountDetailsFallback
1705+ username := m.CurrentAccountUsername()
1706+ password := m.CurrentPassword()
1707+ sessionID := m.CurrentSessionID()
1708+
1709+ if username != "" && sessionID != "" {
1710+ accountDetails = fmt.Sprintf(constants.AccountDetailsTemplate, username, password, sessionID)
1711+ }
1712+
1713+ prompt = strings.Replace(prompt, "{{ACCOUNT_DETAILS}}", accountDetails, 1)
1714+
1715+ return prompt
1716+}
1717+
1718+// rebuildSystemMemory rebuilds the system memory with current state
1719+func (m *Mysis) rebuildSystemMemory() error {
1720+ // Build new system prompt with current credentials and broadcasts
1721+ newPrompt := m.buildSystemPrompt()
1722+
1723+ // Delete old system memory
1724+ if err := m.store.DeleteSystemMemory(m.id); err != nil {
1725+ return fmt.Errorf("delete old system memory: %w", err)
15861726 }
15871727
1588- // Format broadcast section (only content, no sender name)
1589- broadcastSection := fmt.Sprintf(constants.BroadcastSectionTemplate, commanderBroadcast.Content)
1728+ // Store new system memory
1729+ if err := m.store.AddMemory(m.id, store.MemoryRoleSystem, store.MemorySourceSystem, newPrompt, "", ""); err != nil {
1730+ return fmt.Errorf("add new system memory: %w", err)
1731+ }
15901732
1591- return strings.Replace(base, "{{LATEST_BROADCAST}}", broadcastSection, 1)
1733+ return nil
15921734 }
15931735
15941736 func (m *Mysis) updateActivityFromToolResult(result *mcp.ToolResult, err error) {