xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / core / mysis.go

73.3 KB
1package core
2
3import (
4	"context"
5	"encoding/json"
6	"errors"
7	"fmt"
8	"sort"
9	"strconv"
10	"strings"
11	"sync"
12	"time"
13
14	"github.com/rs/zerolog/log"
15	"github.com/xonecas/zoea-nova/internal/constants"
16	"github.com/xonecas/zoea-nova/internal/gamestate"
17	"github.com/xonecas/zoea-nova/internal/mcp"
18	"github.com/xonecas/zoea-nova/internal/provider"
19	"github.com/xonecas/zoea-nova/internal/store"
20)
21
22// Mysis represents a single AI mysis in the swarm.
23type Mysis struct {
24	mu sync.RWMutex
25
26	id          string
27	name        string
28	createdAt   time.Time
29	provider    provider.Provider
30	store       *store.Store
31	bus         *EventBus
32	mcpEndpoint string      // MCP upstream endpoint for creating own client
33	mcpClient   *mcp.Client // Per-mysis MCP client for session isolation
34	mcpProxy    *mcp.Proxy  // Per-mysis MCP proxy wrapping the client
35	commander   *Commander  // Reference to parent commander for WaitGroup
36
37	state  MysisState
38	ctx    context.Context
39	cancel context.CancelFunc
40
41	// turnMu ensures only one turn runs at a time.
42	turnMu sync.Mutex
43
44	// For runtime tracking
45	lastError              error
46	currentAccountUsername string
47	currentPassword        string // Password for current account
48	activityState          ActivityState
49	activityUntil          time.Time
50	lastServerTick         int64
51	lastServerTickAt       time.Time
52	tickDuration           time.Duration
53	encouragementCount     int // Counter for consecutive synthetic encouragements (limit: 3 before idle)
54}
55
56type contextStats struct {
57	MemoryCount    int
58	MessageCount   int
59	ContentBytes   int
60	ReasoningBytes int
61	RoleCounts     map[string]int
62	SourceCounts   map[string]int
63	ToolCallCount  int
64}
65
66// NewMysis creates a new mysis from stored data.
67func NewMysis(id, name string, createdAt time.Time, p provider.Provider, s *store.Store, bus *EventBus, mcpEndpoint string, cmd ...*Commander) *Mysis {
68	var commander *Commander
69	if len(cmd) > 0 {
70		commander = cmd[0]
71	}
72	return &Mysis{
73		id:            id,
74		name:          name,
75		createdAt:     createdAt,
76		provider:      p,
77		store:         s,
78		bus:           bus,
79		mcpEndpoint:   mcpEndpoint,
80		commander:     commander,
81		state:         MysisStateIdle,
82		activityState: ActivityStateIdle,
83	}
84}
85
86func (m *Mysis) computeMemoryStats(memories []*store.Memory) contextStats {
87	stats := contextStats{
88		MemoryCount:  len(memories),
89		RoleCounts:   make(map[string]int),
90		SourceCounts: make(map[string]int),
91	}
92
93	for _, m := range memories {
94		stats.ContentBytes += len(m.Content)
95		stats.ReasoningBytes += len(m.Reasoning)
96		stats.RoleCounts[string(m.Role)]++
97		stats.SourceCounts[string(m.Source)]++
98	}
99
100	return stats
101}
102
103func (m *Mysis) computeMessageStats(messages []provider.Message) contextStats {
104	stats := contextStats{
105		MessageCount: len(messages),
106	}
107
108	for _, msg := range messages {
109		stats.ContentBytes += len(msg.Content)
110		stats.ToolCallCount += len(msg.ToolCalls)
111	}
112
113	return stats
114}
115
116// ID returns the mysis unique identifier.
117func (m *Mysis) ID() string {
118	a := m
119	return a.id
120}
121
122// Name returns the mysis display name.
123func (m *Mysis) Name() string {
124	a := m
125	a.mu.RLock()
126	defer a.mu.RUnlock()
127	return a.name
128}
129
130// CreatedAt returns when the mysis was created.
131func (m *Mysis) CreatedAt() time.Time {
132	a := m
133	return a.createdAt
134}
135
136// State returns the mysis current state.
137func (m *Mysis) State() MysisState {
138	a := m
139	a.mu.RLock()
140	defer a.mu.RUnlock()
141	return a.state
142}
143
144// validateCanAcceptMessage checks if a mysis in the given state can accept messages.
145// Returns nil if messages are allowed, error with user-facing message if not.
146//
147// Valid states for accepting messages:
148//   - idle: Mysis is not running but can accept messages (will process when started)
149//   - running: Mysis is actively running and processing messages
150//
151// Invalid states for accepting messages:
152//   - stopped: User explicitly stopped the mysis, requires relaunch
153//   - errored: Mysis encountered an error, requires relaunch
154func validateCanAcceptMessage(state MysisState) error {
155	switch state {
156	case MysisStateIdle, MysisStateRunning:
157		return nil
158	case MysisStateStopped:
159		return fmt.Errorf("mysis stopped - press 'r' to relaunch")
160	case MysisStateErrored:
161		return fmt.Errorf("mysis errored - press 'r' to relaunch")
162	default:
163		return fmt.Errorf("unknown mysis state: %s", state)
164	}
165}
166
167// ProviderName returns the name of the mysis provider.
168func (m *Mysis) ProviderName() string {
169	a := m
170	a.mu.RLock()
171	defer a.mu.RUnlock()
172	if a.provider == nil {
173		return ""
174	}
175	return a.provider.Name()
176}
177
178// LastError returns the last error encountered by the mysis.
179func (m *Mysis) LastError() error {
180	a := m
181	a.mu.RLock()
182	defer a.mu.RUnlock()
183	return a.lastError
184}
185
186// CurrentAccountUsername returns the username of the account this mysis is using.
187func (m *Mysis) CurrentAccountUsername() string {
188	a := m
189	a.mu.RLock()
190	defer a.mu.RUnlock()
191	return a.currentAccountUsername
192}
193
194// CurrentPassword returns the password for the current account.
195func (m *Mysis) CurrentPassword() string {
196	m.mu.RLock()
197	defer m.mu.RUnlock()
198	return m.currentPassword
199}
200
201// ActivityState returns the mysis current activity.
202func (m *Mysis) ActivityState() ActivityState {
203	a := m
204	a.mu.RLock()
205	defer a.mu.RUnlock()
206	return a.activityState
207}
208
209// SetProvider updates the mysis provider.
210func (m *Mysis) SetProvider(p provider.Provider) {
211	a := m
212	a.mu.Lock()
213	defer a.mu.Unlock()
214	a.provider = p
215}
216
217// SetErrorState sets the mysis to errored state with the given error.
218// Used for testing error recovery scenarios.
219func (m *Mysis) SetErrorState(err error) {
220	m.setErrorState(err)
221}
222
223// Start begins the mysis processing loop.
224func (m *Mysis) Start() error {
225	a := m
226	a.mu.Lock()
227	if a.state == MysisStateRunning {
228		a.mu.Unlock()
229		return fmt.Errorf("mysis already running")
230	}
231
232	oldState := a.state
233
234	// If restarting from errored state, cleanup any existing context/goroutine
235	if oldState == MysisStateErrored && a.cancel != nil {
236		a.cancel() // Cancel old context
237		a.mu.Unlock()
238		// Wait for old goroutine to exit
239		done := make(chan struct{})
240		go func() {
241			a.turnMu.Lock()
242			close(done)
243			a.turnMu.Unlock()
244		}()
245		select {
246		case <-done:
247			// Old goroutine exited
248		case <-time.After(2 * time.Second):
249			log.Warn().Str("mysis", a.name).Msg("Timeout waiting for errored goroutine to exit")
250		}
251		a.mu.Lock()
252	}
253
254	a.mu.Unlock()
255
256	// Create context first (before any state changes)
257	ctx, cancel := context.WithCancel(context.Background())
258
259	// Update store BEFORE changing in-memory state
260	// This ensures we don't start the goroutine if persistence fails
261	if err := a.store.UpdateMysisState(a.id, store.MysisStateRunning); err != nil {
262		cancel() // Clean up context since we're not starting
263		return fmt.Errorf("failed to update state in store: %w", err)
264	}
265
266	// Now that store update succeeded, update in-memory state
267	a.mu.Lock()
268	a.state = MysisStateRunning
269	a.lastError = nil
270	a.activityState = ActivityStateIdle
271	a.activityUntil = time.Time{}
272	a.encouragementCount = 0 // Reset encouragement counter when starting/restarting
273	a.ctx = ctx
274	a.cancel = cancel
275	a.mu.Unlock()
276
277	// Add system prompt if it doesn't exist yet
278	system, err := a.store.GetSystemMemory(a.id)
279	if err != nil || system == nil {
280		systemPrompt := a.buildSystemPrompt()
281		a.store.AddMemory(a.id, store.MemoryRoleSystem, store.MemorySourceSystem, systemPrompt, "", "")
282	}
283
284	// Emit state change event
285	a.emitStateChange(oldState, MysisStateRunning)
286
287	// Track goroutine in WaitGroup (only after state change succeeded)
288	if a.commander != nil {
289		a.commander.wg.Add(1)
290	}
291
292	// Start the processing goroutine (only after all setup succeeded)
293	// Initial encouragement message (if needed) is added by getContextMemories() in run() loop
294	go a.run(ctx)
295
296	// Initialize MCP client in background (non-blocking)
297	go a.initializeMCP(ctx)
298
299	return nil
300}
301
302// initializeMCP creates and initializes a per-mysis MCP client for session isolation.
303// This runs in a goroutine to avoid blocking Start().
304func (m *Mysis) initializeMCP(ctx context.Context) {
305	a := m
306
307	// Skip if no MCP endpoint configured
308	if a.mcpEndpoint == "" {
309		log.Debug().Str("mysis", a.name).Msg("No MCP endpoint configured, skipping MCP initialization")
310		return
311	}
312
313	// Create MCP client
314	client := mcp.NewClient(a.mcpEndpoint)
315	proxy := mcp.NewProxy(client)
316
317	// Set account store and game state store
318	proxy.SetAccountStore(&accountStoreAdapter{a.store})
319	proxy.SetGameStateStore(a.store)
320
321	// Register orchestrator tools if commander is available
322	if a.commander != nil {
323		mcp.RegisterOrchestratorTools(proxy, &commanderAdapter{a.commander})
324	}
325
326	// Initialize with timeout
327	initCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
328	defer cancel()
329
330	if err := proxy.Initialize(initCtx); err != nil {
331		log.Error().
332			Err(err).
333			Str("mysis", a.name).
334			Str("endpoint", a.mcpEndpoint).
335			Msg("Failed to initialize MCP client - tools will be unavailable")
336		return
337	}
338
339	// List tools to verify connection
340	tools, err := proxy.ListTools(initCtx)
341	if err != nil {
342		log.Error().
343			Err(err).
344			Str("mysis", a.name).
345			Msg("Failed to list MCP tools")
346	} else {
347		log.Info().
348			Str("mysis", a.name).
349			Int("tool_count", len(tools)).
350			Msg("MCP client initialized successfully")
351	}
352
353	// Store client and proxy
354	a.mu.Lock()
355	a.mcpClient = client
356	a.mcpProxy = proxy
357	a.mu.Unlock()
358}
359
360// commanderAdapter adapts Commander to the mcp.Orchestrator interface.
361type commanderAdapter struct {
362	commander *Commander
363}
364
365func (a *commanderAdapter) MysisCount() int {
366	return a.commander.MysisCount()
367}
368
369func (a *commanderAdapter) MaxMyses() int {
370	return a.commander.MaxMyses()
371}
372
373func (a *commanderAdapter) GetStateCounts() map[string]int {
374	return a.commander.GetStateCounts()
375}
376
377func (a *commanderAdapter) SendMessageAsync(mysisID, message string) error {
378	return a.commander.SendMessageAsync(mysisID, message)
379}
380
381func (a *commanderAdapter) BroadcastAsync(message string) error {
382	return a.commander.BroadcastAsync(message)
383}
384
385func (a *commanderAdapter) BroadcastFrom(senderID, message string) error {
386	return a.commander.BroadcastFrom(senderID, message)
387}
388
389func (a *commanderAdapter) SearchMessages(mysisID, query string, limit int) ([]mcp.SearchResult, error) {
390	memories, err := a.commander.Store().SearchMemories(mysisID, query, limit)
391	if err != nil {
392		return nil, err
393	}
394
395	results := make([]mcp.SearchResult, len(memories))
396	for i, m := range memories {
397		results[i] = mcp.SearchResult{
398			Role:      string(m.Role),
399			Source:    string(m.Source),
400			Content:   m.Content,
401			CreatedAt: m.CreatedAt.Format("2006-01-02 15:04:05"),
402		}
403	}
404	return results, nil
405}
406
407func (a *commanderAdapter) SearchReasoning(mysisID, query string, limit int) ([]mcp.ReasoningResult, error) {
408	memories, err := a.commander.Store().SearchReasoning(mysisID, query, limit)
409	if err != nil {
410		return nil, err
411	}
412
413	results := make([]mcp.ReasoningResult, len(memories))
414	for i, m := range memories {
415		results[i] = mcp.ReasoningResult{
416			Role:      string(m.Role),
417			Source:    string(m.Source),
418			Content:   m.Content,
419			Reasoning: m.Reasoning,
420			CreatedAt: m.CreatedAt.Format("2006-01-02 15:04:05"),
421		}
422	}
423	return results, nil
424}
425
426// accountStoreAdapter adapts store.Store to mcp.AccountStore interface.
427type accountStoreAdapter struct {
428	store *store.Store
429}
430
431func (a *accountStoreAdapter) CreateAccount(username, password string, mysisID ...string) (*mcp.Account, error) {
432	acc, err := a.store.CreateAccount(username, password, mysisID...)
433	if err != nil {
434		return nil, err
435	}
436	return &mcp.Account{Username: acc.Username, Password: acc.Password}, nil
437}
438
439func (a *accountStoreAdapter) GetAccountByMysisID(mysisID string) (*mcp.Account, error) {
440	acc, err := a.store.GetAccountByMysisID(mysisID)
441	if err != nil {
442		return nil, err
443	}
444	return &mcp.Account{Username: acc.Username, Password: acc.Password}, nil
445}
446
447func (a *accountStoreAdapter) AssignAccount(username, mysisID string) error {
448	return a.store.AssignAccount(username, mysisID)
449}
450
451func (a *accountStoreAdapter) ReleaseAccount(username string) error {
452	return a.store.ReleaseAccount(username)
453}
454
455func (a *accountStoreAdapter) ReleaseAccountByMysisID(mysisID string) error {
456	return a.store.ReleaseAccountByMysisID(mysisID)
457}
458
459func (a *accountStoreAdapter) ReleaseAllAccounts() error {
460	return a.store.ReleaseAllAccounts()
461}
462
463func (a *accountStoreAdapter) ClaimAccount(mysisID string) (*mcp.Account, error) {
464	acc, err := a.store.ClaimAccount(mysisID)
465	if err != nil {
466		return nil, err
467	}
468	return &mcp.Account{Username: acc.Username, Password: acc.Password}, nil
469}
470
471// Stop halts the mysis processing loop.
472func (m *Mysis) Stop() error {
473	a := m
474	a.mu.Lock()
475	if a.state != MysisStateRunning {
476		a.mu.Unlock()
477		return nil
478	}
479
480	// Cancel context to signal goroutine to stop
481	if a.cancel != nil {
482		a.cancel()
483	}
484
485	// Set state to Stopped IMMEDIATELY (before waiting)
486	// This closes the race window where setError() could override with Errored
487	oldState := a.state
488	a.state = MysisStateStopped
489	a.mu.Unlock()
490
491	// Update store (if fails, in-memory state already correct)
492	if err := a.store.UpdateMysisState(a.id, store.MysisStateStopped); err != nil {
493		log.Warn().Err(err).Str("mysis", a.name).Msg("Failed to persist Stopped state")
494		// Continue anyway - in-memory state is authoritative
495	}
496
497	// Emit state change event (TUI will update immediately)
498	a.emitStateChange(oldState, MysisStateStopped)
499
500	// Wait for current turn to finish with timeout
501	// IMPORTANT: Don't clear ctx/cancel until AFTER turn completes
502	// Otherwise SendMessageFrom gets nil ctx and creates uncanceled Background context
503	done := make(chan struct{})
504	go func() {
505		a.turnMu.Lock()
506		close(done)
507		a.turnMu.Unlock()
508	}()
509
510	select {
511	case <-done:
512		// Turn finished successfully
513	case <-time.After(5 * time.Second):
514		log.Warn().Str("mysis", a.name).Msg("Stop timeout - forcing shutdown")
515		// Continue with cleanup even if turn didn't complete
516	}
517
518	// Clear context references AFTER turn completes
519	a.mu.Lock()
520	a.ctx = nil
521	a.cancel = nil
522	a.mu.Unlock()
523
524	// Close provider HTTP client
525	if a.provider != nil {
526		if err := a.provider.Close(); err != nil {
527			log.Warn().Err(err).Str("mysis", a.name).Msg("Failed to close provider")
528		}
529	}
530
531	// Close MCP client
532	a.mu.Lock()
533	mcpClient := a.mcpClient
534	a.mcpClient = nil
535	a.mcpProxy = nil
536	a.mu.Unlock()
537
538	if mcpClient != nil {
539		if err := mcpClient.Close(); err != nil {
540			log.Warn().Err(err).Str("mysis", a.name).Msg("Failed to close MCP client")
541		}
542	}
543
544	return nil
545}
546
547// SendMessageFrom sends a message to the mysis for processing with sender tracking.
548// The source parameter indicates whether this is a direct or broadcast message.
549func (m *Mysis) SendMessageFrom(content string, source store.MemorySource, senderID string) error {
550	a := m
551
552	// Check state BEFORE storing message to fail fast if mysis can't accept messages
553	a.mu.RLock()
554	state := a.state
555	a.mu.RUnlock()
556
557	if err := validateCanAcceptMessage(state); err != nil {
558		return err
559	}
560
561	// Determine role based on source
562	role := store.MemoryRoleUser
563	if source == store.MemorySourceSystem {
564		role = store.MemoryRoleSystem
565	}
566
567	// Store the message BEFORE acquiring turnMu (skip empty internal trigger messages from run loop)
568	// This ensures instant storage and immediate UI feedback, while turn processing happens later
569	if content != "" {
570		if err := a.store.AddMemory(a.id, role, source, content, "", senderID); err != nil {
571			return fmt.Errorf("store message: %w", err)
572		}
573
574		// Reset encouragement counter - we have a real user message now
575		a.mu.Lock()
576		a.encouragementCount = 0
577		a.mu.Unlock()
578
579		// Emit message event - UI will refresh and show the message immediately
580		a.bus.Publish(Event{
581			Type:      EventMysisMessage,
582			MysisID:   a.id,
583			MysisName: a.name,
584			Message:   &MessageData{Role: string(role), Content: content},
585			Timestamp: time.Now(),
586		})
587	}
588
589	// Now acquire turnMu for LLM processing - this may wait if another turn is in progress
590	a.turnMu.Lock()
591	defer a.turnMu.Unlock()
592
593	// Re-read state after acquiring lock (may have changed while waiting)
594	a.mu.RLock()
595	state = a.state
596	p := a.provider
597	mcpProxy := a.mcpProxy
598	a.mu.RUnlock()
599
600	// Validate again after acquiring lock - state may have changed while waiting
601	if err := validateCanAcceptMessage(state); err != nil {
602		// Message is already stored, but we can't process it
603		// This is okay - the message will remain in history but won't trigger LLM response
604		return err
605	}
606
607	// If mysis is idle, auto-start it (direct messages should wake idle myses)
608	// Note: Empty content means this is an internal trigger from run() loop
609	if state == MysisStateIdle && content != "" {
610		if err := a.Start(); err != nil {
611			// Log warning but don't fail the message send
612			// The message is already stored and will be processed when manually started
613			log.Warn().Err(err).Str("mysis", a.name).Msg("Failed to auto-start idle mysis on direct message")
614		}
615		// Return early - run() loop will process the message
616		return nil
617	}
618
619	// If this is an internal trigger but mysis is not running, skip processing
620	if content == "" && state != MysisStateRunning {
621		return nil
622	}
623
624	// Create context for the entire conversation turn
625	a.mu.RLock()
626	parentCtx := a.ctx
627	a.mu.RUnlock()
628
629	if parentCtx == nil {
630		parentCtx = context.Background()
631	}
632
633	ctx, cancel := context.WithTimeout(parentCtx, constants.LLMRequestTimeout)
634	defer cancel()
635
636	// Get available tools from MCP proxy
637	var tools []provider.Tool
638	if mcpProxy != nil {
639		mcpTools, err := mcpProxy.ListTools(ctx)
640		if err != nil {
641			if isMCPConnectionLost(err) {
642				log.Warn().
643					Str("mysis", a.name).
644					Err(err).
645					Msg("MCP connection lost - releasing account")
646				if rebuildErr := a.rebuildSystemMemory(); rebuildErr != nil {
647					log.Error().Err(rebuildErr).Msg("failed to rebuild system memory after MCP session loss")
648				}
649			}
650
651			// Check if this is a session expiration error
652			if strings.Contains(err.Error(), "Session not initialized") || strings.Contains(err.Error(), "-32600") {
653				log.Warn().
654					Str("mysis", a.name).
655					Err(err).
656					Msg("MCP session expired - attempting reinitialization")
657
658				// Trigger MCP reinitialization in background
659				go a.initializeMCP(parentCtx)
660			}
661
662			// Log error but continue - mysis can still chat without tools
663			a.publishCriticalEvent(Event{
664				Type:      EventMysisError,
665				MysisID:   a.id,
666				MysisName: a.name,
667				Error:     &ErrorData{Error: fmt.Sprintf("Failed to load tools: %v", err)},
668				Timestamp: time.Now(),
669			})
670		} else {
671			tools = make([]provider.Tool, len(mcpTools))
672			toolNames := make([]string, len(mcpTools))
673			for i, t := range mcpTools {
674				tools[i] = provider.Tool{
675					Name:        t.Name,
676					Description: t.Description,
677					Parameters:  t.InputSchema,
678				}
679				toolNames[i] = t.Name
680			}
681			// Log available tools (only on first message or if debugging)
682			if len(tools) > 0 {
683				a.bus.Publish(Event{
684					Type:      EventMysisMessage,
685					MysisID:   a.id,
686					MysisName: a.name,
687					Message:   &MessageData{Role: "system", Content: fmt.Sprintf("Tools available: %s", strings.Join(toolNames, ", "))},
688					Timestamp: time.Now(),
689				})
690			}
691		}
692	} else {
693		a.publishCriticalEvent(Event{
694			Type:      EventMysisError,
695			MysisID:   a.id,
696			MysisName: a.name,
697			Error:     &ErrorData{Error: "MCP proxy not configured - no tools available"},
698			Timestamp: time.Now(),
699		})
700	}
701
702	// Track if synthetic encouragement was added (for counter increment after turn completes)
703	var addedSyntheticEncouragement bool
704
705	// Loop: keep calling LLM until we get a final text response
706	for iteration := 0; iteration < constants.MaxToolIterations; iteration++ {
707		// Get recent conversation history (keeps context small for faster inference)
708		memories, addedSynthetic, err := a.getContextMemories()
709		if err != nil {
710			a.setError(err)
711			return fmt.Errorf("get memories: %w", err)
712		}
713
714		// Track if synthetic encouragement was added on first iteration
715		// (subsequent iterations reuse same context, so only first matters)
716		if iteration == 0 {
717			addedSyntheticEncouragement = addedSynthetic
718		}
719
720		// Check if encouragement limit reached (counter incremented after turn completes)
721		a.mu.RLock()
722		count := a.encouragementCount
723		a.mu.RUnlock()
724
725		if count >= 3 {
726			// No real user messages for 3 consecutive autonomous turns - go idle
727			a.setIdle("No user messages after 3 encouragements")
728			return nil
729		}
730
731		memoryStats := a.computeMemoryStats(memories)
732		log.Debug().
733			Str("mysis_id", a.id).
734			Str("mysis_name", a.name).
735			Str("stage", "context_memories").
736			Int("memory_count", memoryStats.MemoryCount).
737			Int("message_count", 0).
738			Int("content_bytes", memoryStats.ContentBytes).
739			Int("reasoning_bytes", memoryStats.ReasoningBytes).
740			Interface("role_counts", memoryStats.RoleCounts).
741			Interface("source_counts", memoryStats.SourceCounts).
742			Int("tool_call_count", 0).
743			Msg("Context stats")
744
745		// Convert to provider messages
746		messages := a.memoriesToMessages(memories)
747		messageStats := a.computeMessageStats(messages)
748		log.Debug().
749			Str("mysis_id", a.id).
750			Str("mysis_name", a.name).
751			Str("stage", "messages_converted").
752			Int("memory_count", memoryStats.MemoryCount).
753			Int("message_count", messageStats.MessageCount).
754			Int("content_bytes", messageStats.ContentBytes).
755			Int("reasoning_bytes", memoryStats.ReasoningBytes).
756			Interface("role_counts", memoryStats.RoleCounts).
757			Interface("source_counts", memoryStats.SourceCounts).
758			Int("tool_call_count", messageStats.ToolCallCount).
759			Msg("Context stats")
760
761		// Signal LLM activity start
762		a.bus.Publish(Event{
763			Type:      EventNetworkLLM,
764			MysisID:   a.id,
765			MysisName: a.name,
766			Timestamp: time.Now(),
767		})
768		log.Debug().
769			Str("mysis_id", a.id).
770			Str("mysis_name", a.name).
771			Str("stage", "before_llm_call").
772			Int("memory_count", memoryStats.MemoryCount).
773			Int("message_count", messageStats.MessageCount).
774			Int("content_bytes", messageStats.ContentBytes).
775			Int("reasoning_bytes", memoryStats.ReasoningBytes).
776			Interface("role_counts", memoryStats.RoleCounts).
777			Interface("source_counts", memoryStats.SourceCounts).
778			Int("tool_call_count", messageStats.ToolCallCount).
779			Msg("Context stats")
780
781		// Set activity state to indicate LLM call in progress
782		a.setActivity(ActivityStateLLMCall, time.Time{})
783
784		// Get response from provider
785		var response *provider.ChatResponse
786		if len(tools) > 0 {
787			response, err = p.ChatWithTools(ctx, messages, tools)
788		} else {
789			// No tools available, use simple chat
790			text, chatErr := p.Chat(ctx, messages)
791			if chatErr != nil {
792				err = chatErr
793			} else {
794				response = &provider.ChatResponse{Content: text}
795			}
796		}
797
798		// Clear LLM activity state after call completes (success or failure)
799		a.setActivity(ActivityStateIdle, time.Time{})
800
801		if err != nil {
802			log.Error().
803				Str("mysis", a.name).
804				Str("provider", p.Name()).
805				Err(err).
806				Msg("Provider returned error")
807			a.bus.Publish(Event{Type: EventNetworkIdle, MysisID: a.id, Timestamp: time.Now()})
808			a.setError(err)
809			return fmt.Errorf("provider chat: %w", err)
810		}
811
812		if response.Reasoning != "" {
813			log.Debug().Str("mysis", a.name).Int("reasoning_len", len(response.Reasoning)).Msg("LLM reasoning captured")
814		}
815
816		// If we have tool calls, execute them
817		if len(response.ToolCalls) > 0 {
818			// Store the assistant's tool call request
819			toolCallJSON := a.formatToolCallsForStorage(response.ToolCalls)
820			if err := a.store.AddMemory(a.id, store.MemoryRoleAssistant, store.MemorySourceLLM, toolCallJSON, response.Reasoning, ""); err != nil {
821				a.setError(err)
822				return fmt.Errorf("store tool call: %w", err)
823			}
824
825			// Emit event showing which tools are being called
826			toolNames := make([]string, len(response.ToolCalls))
827			for i, tc := range response.ToolCalls {
828				toolNames[i] = tc.Name
829			}
830			a.bus.Publish(Event{
831				Type:      EventMysisMessage,
832				MysisID:   a.id,
833				MysisName: a.name,
834				Message:   &MessageData{Role: "assistant", Content: fmt.Sprintf("Calling tools: %s", strings.Join(toolNames, ", "))},
835				Timestamp: time.Now(),
836			})
837
838			// Execute each tool call
839			for _, tc := range response.ToolCalls {
840				// Signal MCP activity
841				a.bus.Publish(Event{
842					Type:      EventNetworkMCP,
843					MysisID:   a.id,
844					MysisName: a.name,
845					Timestamp: time.Now(),
846				})
847
848				// Set activity state to indicate MCP call in progress
849				a.setActivity(ActivityStateMCPCall, time.Time{})
850
851				result, execErr := a.executeToolCall(ctx, mcpProxy, tc)
852
853				// Clear MCP activity state after call completes
854				a.setActivity(ActivityStateIdle, time.Time{})
855
856				// Signal MCP activity complete
857				a.bus.Publish(Event{Type: EventNetworkIdle, MysisID: a.id, Timestamp: time.Now()})
858
859				a.updateActivityFromToolResult(result, execErr)
860
861				// Cache snapshot tool results for game state summary
862				a.cacheSnapshotToolResult(tc.Name, result, execErr)
863
864				// Store the tool result
865				resultContent := a.formatToolResult(tc.ID, tc.Name, result, execErr)
866				if err := a.store.AddMemory(a.id, store.MemoryRoleTool, store.MemorySourceTool, resultContent, "", ""); err != nil {
867					a.setError(err)
868					return fmt.Errorf("store tool result: %w", err)
869				}
870
871				// Emit tool result event
872				a.bus.Publish(Event{
873					Type:      EventMysisMessage,
874					MysisID:   a.id,
875					MysisName: a.name,
876					Message:   &MessageData{Role: "tool", Content: fmt.Sprintf("[%s] %s", tc.Name, a.formatToolResultDisplay(result, execErr))},
877					Timestamp: time.Now(),
878				})
879
880				if execErr != nil && isToolTimeout(execErr) {
881					a.bus.Publish(Event{Type: EventNetworkIdle, MysisID: a.id, Timestamp: time.Now()})
882					a.setError(execErr)
883					return fmt.Errorf("tool call timed out: %w", execErr)
884				}
885
886				if execErr != nil && isToolRetryExhausted(execErr) {
887					a.bus.Publish(Event{Type: EventNetworkIdle, MysisID: a.id, Timestamp: time.Now()})
888					a.setError(execErr)
889					return fmt.Errorf("tool call failed after retries: %w", execErr)
890				}
891			}
892
893			// Continue loop to get next LLM response
894			continue
895		}
896
897		// No tool calls - we have a final response
898		finalResponse := response.Content
899		if finalResponse == "" && response.Reasoning == "" {
900			finalResponse = constants.FallbackLLMResponse
901		}
902
903		// Store the assistant response
904		if err := a.store.AddMemory(a.id, store.MemoryRoleAssistant, store.MemorySourceLLM, finalResponse, response.Reasoning, ""); err != nil {
905			a.setError(err)
906			return fmt.Errorf("store response: %w", err)
907		}
908
909		// Signal network idle
910		a.bus.Publish(Event{Type: EventNetworkIdle, MysisID: a.id, Timestamp: time.Now()})
911
912		// Emit response event
913		a.bus.Publish(Event{
914			Type:      EventMysisResponse,
915			MysisID:   a.id,
916			MysisName: a.name,
917			Message:   &MessageData{Role: "assistant", Content: finalResponse},
918			Timestamp: time.Now(),
919		})
920
921		// Increment encouragement counter if this was an autonomous turn (no user message)
922		// If synthetic encouragement was added, this means no real user message existed
923		if addedSyntheticEncouragement {
924			a.mu.Lock()
925			a.encouragementCount++
926			count := a.encouragementCount
927			a.mu.Unlock()
928
929			log.Debug().
930				Str("mysis", a.name).
931				Int("count", count).
932				Msg("Autonomous turn completed - incremented encouragement counter")
933
934			// If we've hit the limit, transition to idle immediately
935			if count >= 3 {
936				a.setIdle("No user messages after 3 encouragements")
937				return nil
938			}
939		}
940
941		return nil
942	}
943
944	// Max tool iterations reached - end this turn gracefully and continue to next turn
945	// This is NOT an error - the mysis made progress and should continue autonomous operation
946	log.Warn().
947		Str("mysis", a.name).
948		Int("max_iterations", constants.MaxToolIterations).
949		Msg("Max tool iterations reached - ending turn, will continue next turn")
950
951	// Signal network idle
952	a.bus.Publish(Event{Type: EventNetworkIdle, MysisID: a.id, Timestamp: time.Now()})
953
954	// Increment encouragement counter if this was an autonomous turn
955	// Same logic as successful turn completion (lines 697-713)
956	if addedSyntheticEncouragement {
957		a.mu.Lock()
958		a.encouragementCount++
959		count := a.encouragementCount
960		a.mu.Unlock()
961
962		log.Debug().
963			Str("mysis", a.name).
964			Int("count", count).
965			Msg("Autonomous turn completed (max iterations) - incremented encouragement counter")
966
967		// If we've hit the limit, transition to idle immediately
968		if count >= 3 {
969			a.setIdle("No user messages after 3 encouragements")
970			return nil
971		}
972	}
973
974	// Return success - turn completed, continue to next turn
975	return nil
976}
977
978// SendMessage sends a message to the mysis for processing.
979func (m *Mysis) SendMessage(content string, source store.MemorySource) error {
980	a := m
981	return a.SendMessageFrom(content, source, "")
982}
983
984// QueueBroadcast stores a broadcast message and triggers async processing.
985// Unlike SendMessage, this does not block waiting for the mysis to process.
986// Returns immediately after storing the message in the database.
987func (m *Mysis) QueueBroadcast(content string, senderID string) error {
988	a := m
989
990	a.mu.RLock()
991	state := a.state
992	a.mu.RUnlock()
993
994	if err := validateCanAcceptMessage(state); err != nil {
995		return err
996	}
997
998	// Store the message immediately (fast DB write, no LLM call)
999	if err := a.store.AddMemory(a.id, store.MemoryRoleUser, store.MemorySourceBroadcast, content, "", senderID); err != nil {
1000		return fmt.Errorf("store broadcast: %w", err)
1001	}
1002
1003	// Reset encouragement counter - we have a real user message now
1004	a.mu.Lock()
1005	a.encouragementCount = 0
1006	a.mu.Unlock()
1007
1008	// Emit message event
1009	a.bus.Publish(Event{
1010		Type:      EventMysisMessage,
1011		MysisID:   a.id,
1012		MysisName: a.name,
1013		Message:   &MessageData{Role: "user", Content: content},
1014		Timestamp: time.Now(),
1015	})
1016
1017	// If mysis is idle, auto-start it (broadcasts should wake idle myses)
1018	if state == MysisStateIdle {
1019		if err := a.Start(); err != nil {
1020			// Log warning but don't fail the broadcast
1021			// The message is already stored and will be processed when manually started
1022			log.Warn().Err(err).Str("mysis", a.name).Msg("Failed to auto-start idle mysis on broadcast")
1023		}
1024	}
1025
1026	return nil
1027}
1028
1029// memoriesToMessages converts stored memories to provider messages.
1030func (m *Mysis) memoriesToMessages(memories []*store.Memory) []provider.Message {
1031	a := m
1032	messages := make([]provider.Message, 0, len(memories))
1033
1034	for _, m := range memories {
1035		msg := provider.Message{
1036			Role:    string(m.Role),
1037			Content: m.Content,
1038		}
1039
1040		// Handle tool role - needs ToolCallID
1041		if m.Role == store.MemoryRoleTool {
1042			// Extract tool call ID from stored format: "tool_call_id:content"
1043			idx := strings.Index(m.Content, constants.ToolCallStorageFieldDelimiter)
1044			if idx <= 0 {
1045				log.Warn().
1046					Str("content", m.Content).
1047					Msg("Skipping malformed tool result - missing delimiter")
1048				continue // Skip this message
1049			}
1050			toolCallID := m.Content[:idx]
1051			if toolCallID == "" {
1052				log.Warn().
1053					Str("content", m.Content).
1054					Msg("Skipping tool result with empty tool_call_id")
1055				continue // Skip this message
1056			}
1057			msg.ToolCallID = toolCallID
1058			msg.Content = m.Content[idx+1:]
1059		}
1060
1061		// Handle assistant messages with tool calls
1062		if m.Role == store.MemoryRoleAssistant && strings.HasPrefix(m.Content, constants.ToolCallStoragePrefix) {
1063			// Parse tool calls from stored format
1064			msg.ToolCalls = a.parseStoredToolCalls(m.Content)
1065			if len(msg.ToolCalls) > 0 {
1066				msg.Content = "" // Only clear content if tool calls were parsed successfully
1067			} else {
1068				log.Warn().
1069					Str("content", m.Content).
1070					Msg("Failed to parse tool calls from assistant message - keeping original content")
1071				// Keep original content as fallback
1072			}
1073		}
1074
1075		messages = append(messages, msg)
1076	}
1077
1078	return messages
1079}
1080
1081// executeToolCall executes a single tool call via MCP proxy.
1082func (m *Mysis) executeToolCall(ctx context.Context, mcpProxy *mcp.Proxy, tc provider.ToolCall) (*mcp.ToolResult, error) {
1083	a := m
1084	if mcpProxy == nil {
1085		return &mcp.ToolResult{
1086			Content: []mcp.ContentBlock{{Type: "text", Text: "MCP not configured"}},
1087			IsError: true,
1088		}, nil
1089	}
1090
1091	caller := mcp.CallerContext{
1092		MysisID:   a.id,
1093		MysisName: a.name,
1094	}
1095
1096	result, err := mcpProxy.CallTool(ctx, caller, tc.Name, tc.Arguments)
1097	if err != nil && isMCPConnectionLost(err) {
1098		log.Warn().
1099			Str("mysis", a.name).
1100			Str("tool", tc.Name).
1101			Err(err).
1102			Msg("MCP connection lost during tool call - releasing account")
1103		if rebuildErr := a.rebuildSystemMemory(); rebuildErr != nil {
1104			log.Error().Err(rebuildErr).Msg("failed to rebuild system memory after MCP session loss")
1105		}
1106	}
1107	if err == nil && result != nil && !result.IsError {
1108		switch tc.Name {
1109		case "login", "register":
1110			// Extract username from arguments
1111			var args struct {
1112				Username string `json:"username"`
1113				Password string `json:"password"`
1114			}
1115			json.Unmarshal(tc.Arguments, &args)
1116
1117			// Extract password from tool result
1118			password := args.Password
1119
1120			// For register, password might be in result instead of args
1121			if password == "" && tc.Name == "register" {
1122				password = extractPassword(result)
1123			}
1124
1125			// Extract actual username from result (may differ from args due to pool account substitution)
1126			actualUsername := extractUsername(result)
1127			if actualUsername == "" {
1128				actualUsername = args.Username // Fallback to requested username
1129			}
1130
1131			if actualUsername != "" {
1132				a.setCurrentAccount(actualUsername, password)
1133
1134				// Rebuild system prompt with new credentials
1135				if err := a.rebuildSystemMemory(); err != nil {
1136					log.Error().Err(err).Msg("failed to rebuild system memory after login")
1137				}
1138			}
1139		}
1140	}
1141
1142	return result, err
1143}
1144
1145func (m *Mysis) setCurrentAccount(username, password string) {
1146	m.mu.Lock()
1147	storeRef := m.store
1148
1149	// Ignore empty username (no-op)
1150	if username == "" {
1151		m.mu.Unlock()
1152		return
1153	}
1154
1155	// Under permanent assignment model, accounts don't switch
1156	// If username differs from current, this is the first assignment
1157	// No release needed - accounts are permanently assigned
1158
1159	m.currentAccountUsername = username
1160	m.currentPassword = password
1161	m.mu.Unlock()
1162
1163	if storeRef != nil {
1164		if err := storeRef.AssignAccount(username, m.id); err != nil {
1165			log.Error().Err(err).Str("username", username).Msg("failed to assign account")
1166		}
1167	}
1168}
1169
1170func (m *Mysis) releaseCurrentAccount() {
1171	m.mu.Lock()
1172	defer m.mu.Unlock()
1173
1174	// Release the permanently assigned account for this mysis
1175	if err := m.store.ReleaseAccountByMysisID(m.id); err != nil {
1176		log.Error().Err(err).Str("mysis_id", m.id).Msg("failed to release account")
1177	}
1178	m.currentAccountUsername = ""
1179	m.currentPassword = ""
1180}
1181
1182// extractPassword extracts password from tool result (for register responses)
1183func extractPassword(result *mcp.ToolResult) string {
1184	if result == nil || result.IsError {
1185		return ""
1186	}
1187
1188	for _, block := range result.Content {
1189		if block.Type == "text" {
1190			var data map[string]interface{}
1191			if err := json.Unmarshal([]byte(block.Text), &data); err == nil {
1192				if password, ok := data["password"].(string); ok {
1193					return password
1194				}
1195			}
1196		}
1197	}
1198	return ""
1199}
1200
1201// extractUsername extracts username from tool result (actual username after pool substitution)
1202func extractUsername(result *mcp.ToolResult) string {
1203	if result == nil || result.IsError {
1204		return ""
1205	}
1206
1207	for _, block := range result.Content {
1208		if block.Type == "text" {
1209			var data map[string]interface{}
1210			if err := json.Unmarshal([]byte(block.Text), &data); err == nil {
1211				if username, ok := data["username"].(string); ok {
1212					return username
1213				}
1214			}
1215		}
1216	}
1217	return ""
1218}
1219
1220// formatToolCallsForStorage formats tool calls for storage in memory.
1221func (m *Mysis) formatToolCallsForStorage(calls []provider.ToolCall) string {
1222	var parts []string
1223	for _, tc := range calls {
1224		parts = append(parts, fmt.Sprintf("%s%s%s%s%s", tc.ID, constants.ToolCallStorageFieldDelimiter, tc.Name, constants.ToolCallStorageFieldDelimiter, string(tc.Arguments)))
1225	}
1226	return constants.ToolCallStoragePrefix + strings.Join(parts, constants.ToolCallStorageRecordDelimiter)
1227}
1228
1229// parseStoredToolCalls parses tool calls from stored format.
1230func (m *Mysis) parseStoredToolCalls(stored string) []provider.ToolCall {
1231	stored = strings.TrimPrefix(stored, constants.ToolCallStoragePrefix)
1232	if stored == "" {
1233		return nil
1234	}
1235
1236	var calls []provider.ToolCall
1237	parts := strings.Split(stored, constants.ToolCallStorageRecordDelimiter)
1238	for _, part := range parts {
1239		fields := strings.SplitN(part, constants.ToolCallStorageFieldDelimiter, constants.ToolCallStorageFieldCount)
1240		if len(fields) >= constants.ToolCallStorageFieldCount {
1241			args := json.RawMessage(fields[2])
1242			if !json.Valid(args) {
1243				log.Warn().
1244					Str("tool_call_id", fields[0]).
1245					Str("tool_name", fields[1]).
1246					Msg("Invalid JSON in tool call arguments - using empty object")
1247				args = json.RawMessage("{}")
1248			}
1249			calls = append(calls, provider.ToolCall{
1250				ID:        fields[0],
1251				Name:      fields[1],
1252				Arguments: args,
1253			})
1254		}
1255	}
1256	return calls
1257}
1258
1259// formatToolResult formats a tool result for storage (includes ID for LLM context).
1260func (m *Mysis) formatToolResult(toolCallID, toolName string, result *mcp.ToolResult, err error) string {
1261	if err != nil {
1262		if errors.Is(err, context.DeadlineExceeded) {
1263			return fmt.Sprintf("%s%sError calling %s: tool call timed out: %v", toolCallID, constants.ToolCallStorageFieldDelimiter, toolName, err)
1264		}
1265		if errors.Is(err, mcp.ErrToolRetryExhausted) {
1266			return fmt.Sprintf("%s%sError calling %s: MCP call failed after retries: %v", toolCallID, constants.ToolCallStorageFieldDelimiter, toolName, err)
1267		}
1268		return fmt.Sprintf("%s%sError calling %s: %v. Check the tool's required parameters and try again.", toolCallID, constants.ToolCallStorageFieldDelimiter, toolName, err)
1269	}
1270
1271	var texts []string
1272	for _, block := range result.Content {
1273		if block.Type == "text" {
1274			texts = append(texts, block.Text)
1275		}
1276	}
1277
1278	content := strings.Join(texts, "\n")
1279	if result.IsError {
1280		if strings.Contains(content, "empty_entry") {
1281			content = fmt.Sprintf("Error calling %s: %s. The entry field must contain non-empty text. Example: captains_log_add({\"entry\": \"Your message here\"})", toolName, content)
1282		} else {
1283			content = fmt.Sprintf("Error calling %s: %s", toolName, content)
1284		}
1285	}
1286
1287	return fmt.Sprintf("%s%s%s", toolCallID, constants.ToolCallStorageFieldDelimiter, content)
1288}
1289
1290func isToolTimeout(err error) bool {
1291	return errors.Is(err, context.DeadlineExceeded)
1292}
1293
1294func isToolRetryExhausted(err error) bool {
1295	return errors.Is(err, mcp.ErrToolRetryExhausted)
1296}
1297
1298func isMCPConnectionLost(err error) bool {
1299	if err == nil {
1300		return false
1301	}
1302	return isMCPConnectionLostMessage(err.Error())
1303}
1304
1305func isMCPConnectionLostMessage(message string) bool {
1306	if message == "" {
1307		return false
1308	}
1309	msg := strings.ToLower(message)
1310	return strings.Contains(msg, "session not initialized") ||
1311		strings.Contains(msg, "-32600") ||
1312		strings.Contains(msg, "connection refused") ||
1313		strings.Contains(msg, "connection reset") ||
1314		strings.Contains(msg, "no such host") ||
1315		strings.Contains(msg, "dial tcp") ||
1316		strings.Contains(msg, "unexpected eof") ||
1317		strings.Contains(msg, "eof")
1318}
1319
1320// formatToolResultDisplay formats a tool result for UI display (human-readable).
1321func (m *Mysis) formatToolResultDisplay(result *mcp.ToolResult, err error) string {
1322	if err != nil {
1323		return fmt.Sprintf("Error: %v", err)
1324	}
1325
1326	var texts []string
1327	for _, block := range result.Content {
1328		if block.Type == "text" {
1329			texts = append(texts, block.Text)
1330		}
1331	}
1332
1333	content := strings.Join(texts, "\n")
1334	if result.IsError {
1335		content = "Error: " + content
1336	}
1337
1338	// Truncate long results for display
1339	if len(content) > constants.ToolResultDisplayMaxChars {
1340		content = content[:constants.ToolResultDisplayTruncateTo] + constants.ToolResultDisplayEllipsis
1341	}
1342
1343	return content
1344}
1345
1346// setError sets the mysis last error, transitions to errored state, and emits events.
1347// This method ensures proper state machine compliance by transitioning to MysisStateErrored.
1348func (m *Mysis) setError(err error) {
1349	a := m
1350	a.mu.Lock()
1351
1352	// Check state AFTER acquiring lock to close race window
1353	// This prevents Stop() from being overridden by concurrent setError() calls
1354	oldState := a.state
1355
1356	// If mysis was intentionally stopped, don't override with error state
1357	// This prevents race where Stop() cancels context, causing in-flight turn
1358	// to error with "context canceled" and override the Stopped state
1359	if oldState == MysisStateStopped {
1360		a.mu.Unlock()
1361		log.Debug().Str("mysis", a.name).Err(err).Msg("Ignoring error - mysis was intentionally stopped")
1362		return
1363	}
1364
1365	log.Error().
1366		Str("mysis", a.name).
1367		Str("old_state", string(oldState)).
1368		Err(err).
1369		Msg("Mysis transitioning to errored state")
1370
1371	a.lastError = err
1372	a.state = MysisStateErrored
1373	a.mu.Unlock()
1374
1375	// Update store
1376	if updateErr := a.store.UpdateMysisState(a.id, store.MysisStateErrored); updateErr != nil {
1377		log.Warn().Err(updateErr).Str("mysis", a.id).Msg("Failed to update state in store")
1378	}
1379
1380	// Emit state change event
1381	a.emitStateChange(oldState, MysisStateErrored)
1382
1383	// Emit error event
1384	a.publishCriticalEvent(Event{
1385		Type:      EventMysisError,
1386		MysisID:   a.id,
1387		MysisName: a.name,
1388		Error:     &ErrorData{Error: err.Error()},
1389		Timestamp: time.Now(),
1390	})
1391}
1392
1393func (m *Mysis) setIdle(reason string) {
1394	a := m
1395	a.mu.Lock()
1396	oldState := a.state
1397
1398	if oldState == MysisStateStopped {
1399		a.mu.Unlock()
1400		log.Debug().Str("mysis", a.name).Str("reason", reason).Msg("Ignoring idle transition - mysis was intentionally stopped")
1401		return
1402	}
1403
1404	if oldState == MysisStateIdle {
1405		a.mu.Unlock()
1406		return
1407	}
1408
1409	log.Warn().
1410		Str("mysis", a.name).
1411		Str("old_state", string(oldState)).
1412		Str("reason", reason).
1413		Msg("Mysis transitioning to idle state")
1414
1415	// Cancel context to stop run loop goroutine (like Stop() does)
1416	if a.cancel != nil {
1417		a.cancel()
1418	}
1419
1420	a.state = MysisStateIdle
1421	a.lastError = nil
1422	a.mu.Unlock()
1423
1424	if updateErr := a.store.UpdateMysisState(a.id, store.MysisStateIdle); updateErr != nil {
1425		log.Warn().Err(updateErr).Str("mysis", a.id).Msg("Failed to update state in store")
1426	}
1427
1428	a.emitStateChange(oldState, MysisStateIdle)
1429	// NOTE: Don't release account when going idle - idle is a "sleeping" state
1430	// and the mysis should maintain its logged-in session for when it resumes.
1431	// Account is only released on: Stop(), setError(), or explicit logout tool call.
1432}
1433
1434// extractLatestToolLoop finds the most recent tool-call message (assistant role
1435// with tool_calls) and returns it plus all subsequent tool results.
1436//
1437// Parameters:
1438//   - memories: Slice of memories in chronological order (oldest first, newest last)
1439//     as returned by GetRecentMemories
1440//
1441// Returns:
1442//   - []*store.Memory: Slice containing [tool-call-message, result1, result2, ...]
1443//     in chronological order, or empty slice if no tool loop found
1444//
1445// Behavior:
1446//   - Scans backwards from the end to find the most recent assistant message
1447//     with content prefixed by constants.ToolCallStoragePrefix
1448//   - Collects all consecutive tool role messages that follow the tool call
1449//   - Stops at the first non-tool message (end of loop)
1450//   - Returns empty slice if no tool call is found
1451//
1452// Rationale:
1453//
1454//	By extracting only the latest tool loop, we ensure OpenAI-compatible providers
1455//	receive properly paired tool calls and results without orphaned tool messages.
1456//	This prevents API errors from malformed message sequences.
1457//
1458// Example:
1459//
1460//	Given memories: [user_msg, assistant_with_tools, tool_result1, tool_result2, user_msg2]
1461//	Returns: [assistant_with_tools, tool_result1, tool_result2]
1462func (m *Mysis) extractLatestToolLoop(memories []*store.Memory) []*store.Memory {
1463	if len(memories) == 0 {
1464		return nil
1465	}
1466
1467	// Scan backwards from the end (newest first) to find the most recent tool call
1468	toolCallIdx := -1
1469	for i := len(memories) - 1; i >= 0; i-- {
1470		mem := memories[i]
1471		if mem.Role == store.MemoryRoleAssistant &&
1472			strings.HasPrefix(mem.Content, constants.ToolCallStoragePrefix) {
1473			toolCallIdx = i
1474			break
1475		}
1476	}
1477
1478	// No tool calls found
1479	if toolCallIdx == -1 {
1480		return nil
1481	}
1482
1483	// Collect the tool call message and all subsequent tool results
1484	result := make([]*store.Memory, 0, 4) // Pre-allocate for typical case (1 call + 2-3 results)
1485	result = append(result, memories[toolCallIdx])
1486
1487	// Scan forward (later in time) for consecutive tool results
1488	for i := toolCallIdx + 1; i < len(memories); i++ {
1489		mem := memories[i]
1490
1491		// Only collect tool role messages
1492		if mem.Role == store.MemoryRoleTool {
1493			result = append(result, mem)
1494		} else {
1495			// Stop at first non-tool message (end of loop)
1496			break
1497		}
1498	}
1499
1500	return result
1501}
1502
1503// findLastUserPromptIndex finds the index of the most recent user-initiated prompt.
1504// User prompts include:
1505// - Direct messages (source: direct)
1506// - Broadcasts (source: broadcast)
1507// - System nudges (source: system, role: user)
1508//
1509// Returns -1 if no user prompt is found.
1510//
1511// This defines the "current turn" boundary - everything from this index onward
1512// is part of the current conversation turn and should be included in full context.
1513func (m *Mysis) findLastUserPromptIndex(memories []*store.Memory) int {
1514	// Scan backwards to find most recent user prompt
1515	for i := len(memories) - 1; i >= 0; i-- {
1516		mem := memories[i]
1517		if mem.Role == store.MemoryRoleUser {
1518			// User messages from direct, broadcast, or system (nudges) all count as prompts
1519			if mem.Source == store.MemorySourceDirect ||
1520				mem.Source == store.MemorySourceBroadcast ||
1521				mem.Source == store.MemorySourceSystem {
1522				return i
1523			}
1524		}
1525	}
1526	return -1
1527}
1528
1529// getContextMemories returns memories for LLM context with turn-aware composition.
1530// Composes context as: [system prompt] + [historical context] + [current turn].
1531//
1532// Returns:
1533//   - []*store.Memory: Composed context slice ready for provider conversion
1534//   - error: Database error from GetRecentMemories or GetSystemMemory
1535//
1536// Context Composition Strategy:
1537//  1. System prompt (if available) - provides mysis identity and mission
1538//  2. Historical context - memories before the current turn (compressed via extractLatestToolLoop)
1539//  3. Current turn - ALL memories from the last user prompt onward (uncompressed)
1540//
1541// Turn Boundary:
1542//   - "Current turn" starts at the most recent user-initiated prompt (direct message, broadcast, or synthetic encouragement)
1543//   - Everything from that prompt forward is included in full to preserve multi-step tool reasoning
1544//   - Historical turns (before current turn boundary) are compressed to save context
1545//
1546// Rationale:
1547//   - Multi-step tool calling (login → get_status → get_notifications) requires complete tool history
1548//   - Historical turns can be compressed without breaking current reasoning
1549//   - Preserves tool results from within the current turn
1550//   - Prevents orphaned tool calls by keeping complete tool loops together
1551//
1552// Example context for multi-step turn:
1553//
1554//	[
1555//	  {role: system, content: "You are Mysis Alpha..."},
1556//	  {role: user, source: direct, content: "Check ship status"},
1557//	  {role: assistant, content: "[TOOL_CALLS]call_1:login:{}"},
1558//	  {role: tool, content: "call_1:Login successful"},
1559//	  {role: assistant, content: "[TOOL_CALLS]call_2:get_status:{}"},
1560//	  {role: tool, content: "call_2:Ship health: 100%"}
1561//	]
1562func (m *Mysis) getContextMemories() ([]*store.Memory, bool, error) {
1563	// Track if synthetic encouragement was added this call
1564	addedSynthetic := false
1565
1566	// Get all recent memories
1567	allMemories, err := m.store.GetRecentMemories(m.id, constants.MaxContextMessages)
1568	if err != nil {
1569		return nil, false, err
1570	}
1571
1572	// Find current turn boundary
1573	turnBoundaryIdx := m.findLastUserPromptIndex(allMemories)
1574
1575	result := make([]*store.Memory, 0, 20) // Pre-allocate for typical size
1576
1577	// Step 1: Add system prompt (if available)
1578	system, err := m.store.GetSystemMemory(m.id)
1579	if err == nil && system != nil {
1580		result = append(result, system)
1581	}
1582
1583	// Step 2: Add historical context (before current turn)
1584	if turnBoundaryIdx > 0 {
1585		historicalMemories := allMemories[:turnBoundaryIdx]
1586		// Apply compression to historical turns - extract only latest tool loop
1587		historicalToolLoop := m.extractLatestToolLoop(historicalMemories)
1588		if len(historicalToolLoop) > 0 {
1589			result = append(result, historicalToolLoop...)
1590		}
1591	}
1592
1593	// Step 3: Add current turn (from last user prompt onward)
1594	if turnBoundaryIdx >= 0 {
1595		// Include ALL messages from current turn without compression
1596		currentTurnMemories := allMemories[turnBoundaryIdx:]
1597		result = append(result, currentTurnMemories...)
1598	} else {
1599		// No user prompt found in sliding window
1600		// Check if a broadcast exists in DB (even if outside the 20-message window)
1601		// This ensures myses stay running when they have mission directives
1602		broadcast, err := m.store.GetMostRecentBroadcast(m.id)
1603		if err != nil {
1604			// Database error - return error to caller rather than treating as "no broadcast"
1605			return nil, false, fmt.Errorf("check for broadcast: %w", err)
1606		}
1607
1608		if broadcast != nil {
1609			// Broadcast exists! Include it to keep mysis running
1610			// Reset encouragement counter since we have a real user message
1611			m.mu.Lock()
1612			m.encouragementCount = 0
1613			m.mu.Unlock()
1614
1615			// Include recent tool loop for continuity
1616			historicalToolLoop := m.extractLatestToolLoop(allMemories)
1617			if len(historicalToolLoop) > 0 {
1618				result = append(result, historicalToolLoop...)
1619			}
1620
1621			// Check if broadcast is already in allMemories (edge case protection)
1622			alreadyInContext := false
1623			for _, mem := range allMemories {
1624				if mem.ID == broadcast.ID {
1625					alreadyInContext = true
1626					break
1627				}
1628			}
1629
1630			// Add the broadcast as the user prompt (if not already present)
1631			if !alreadyInContext {
1632				result = append(result, broadcast)
1633			}
1634		} else {
1635			// No broadcast exists - add synthetic encouragement message
1636			// Include recent tool loop to maintain conversation continuity
1637			historicalToolLoop := m.extractLatestToolLoop(allMemories)
1638			if len(historicalToolLoop) > 0 {
1639				result = append(result, historicalToolLoop...)
1640			}
1641
1642			// Then add synthetic encouragement message
1643			nudgeContent := "Continue your mission. Check notifications and coordinate with the swarm."
1644			nudgeMemory := &store.Memory{
1645				Role:      store.MemoryRoleUser,
1646				Source:    store.MemorySourceSystem,
1647				Content:   nudgeContent,
1648				SenderID:  "",
1649				CreatedAt: time.Now(),
1650			}
1651			result = append(result, nudgeMemory)
1652
1653			// Mark that synthetic encouragement was added
1654			// Counter will be incremented once per turn by caller after turn completes
1655			addedSynthetic = true
1656		}
1657	}
1658
1659	// Apply compression to reduce context size and ensure API compliance
1660	result = m.compactSnapshots(result)
1661	result = m.removeOrphanedToolCalls(result)
1662
1663	return result, addedSynthetic, nil
1664}
1665
1666// compactSnapshots removes redundant snapshot tool results, keeping only the most recent
1667// result for each snapshot tool. This prevents state-heavy tools from crowding out
1668// conversation history while ensuring the latest state is available.
1669func (m *Mysis) compactSnapshots(memories []*store.Memory) []*store.Memory {
1670	a := m
1671	if len(memories) == 0 {
1672		return memories
1673	}
1674
1675	toolCallNames := a.toolCallNameIndex(memories)
1676
1677	// Track the most recent snapshot tool result for each tool
1678	latestSnapshot := make(map[string]int) // tool name -> index in memories
1679
1680	// First pass: identify tool results and track latest for each snapshot tool
1681	for i, m := range memories {
1682		if m.Role != store.MemoryRoleTool {
1683			continue
1684		}
1685
1686		// Extract tool name from stored format: "tool_call_id:content"
1687		toolName := a.extractToolNameFromResult(m.Content, toolCallNames)
1688		if toolName == "" {
1689			continue
1690		}
1691
1692		// If this is a snapshot tool, track its position
1693		if a.isSnapshotTool(toolName) {
1694			latestSnapshot[toolName] = i
1695		}
1696	}
1697
1698	// Second pass: build result, skipping older snapshot tool results
1699	result := make([]*store.Memory, 0, len(memories))
1700	for i, m := range memories {
1701		// Keep non-tool memories
1702		if m.Role != store.MemoryRoleTool {
1703			result = append(result, m)
1704			continue
1705		}
1706
1707		// Extract tool name
1708		toolName := a.extractToolNameFromResult(m.Content, toolCallNames)
1709		if toolName == "" {
1710			result = append(result, m)
1711			continue
1712		}
1713
1714		// If this is a snapshot tool, only keep if it's the latest
1715		if a.isSnapshotTool(toolName) {
1716			if latestSnapshot[toolName] == i {
1717				result = append(result, m)
1718			}
1719			// Skip older snapshots
1720			continue
1721		}
1722
1723		// Keep non-snapshot tool results
1724		result = append(result, m)
1725	}
1726
1727	return result
1728}
1729
1730// collectValidToolResultIDs extracts all tool call IDs from tool result messages.
1731// Used to validate that assistant tool calls have corresponding tool results.
1732func (m *Mysis) collectValidToolResultIDs(memories []*store.Memory) map[string]bool {
1733	validToolResults := make(map[string]bool)
1734
1735	for _, mem := range memories {
1736		if mem.Role == store.MemoryRoleTool {
1737			idx := strings.Index(mem.Content, constants.ToolCallStorageFieldDelimiter)
1738			if idx > 0 {
1739				toolCallID := mem.Content[:idx]
1740				validToolResults[toolCallID] = true
1741			}
1742		}
1743	}
1744
1745	return validToolResults
1746}
1747
1748// removeOrphanedToolCalls removes assistant messages with tool calls that don't have
1749// corresponding tool result messages. This can happen due to:
1750// 1. Context window cutting off tool results but keeping tool calls
1751// 2. Context compaction removing tool result messages
1752//
1753// OpenAI Chat Completions API may crash if assistant tool calls don't have matching results.
1754func (m *Mysis) removeOrphanedToolCalls(memories []*store.Memory) []*store.Memory {
1755	validToolResults := m.collectValidToolResultIDs(memories)
1756
1757	result := make([]*store.Memory, 0, len(memories))
1758	for _, mem := range memories {
1759		// Check if this is an assistant message with tool calls
1760		if mem.Role == store.MemoryRoleAssistant &&
1761			strings.HasPrefix(mem.Content, constants.ToolCallStoragePrefix) {
1762			calls := m.parseStoredToolCalls(mem.Content)
1763			allCallsHaveResults := true
1764			for _, call := range calls {
1765				if !validToolResults[call.ID] {
1766					log.Debug().
1767						Str("tool_call_id", call.ID).
1768						Str("tool_name", call.Name).
1769						Msg("Removing orphaned assistant tool call - no matching tool result")
1770					allCallsHaveResults = false
1771					break
1772				}
1773			}
1774			if !allCallsHaveResults {
1775				continue // Skip assistant message with orphaned tool calls
1776			}
1777		}
1778		result = append(result, mem)
1779	}
1780
1781	if len(result) < len(memories) {
1782		log.Debug().
1783			Int("removed", len(memories)-len(result)).
1784			Int("original", len(memories)).
1785			Int("final", len(result)).
1786			Msg("Removed orphaned assistant tool calls for OpenAI compliance")
1787	}
1788
1789	return result
1790}
1791
1792func (m *Mysis) extractToolNameFromResult(content string, toolCallNames map[string]string) string {
1793	idx := strings.Index(content, constants.ToolCallStorageFieldDelimiter)
1794	if idx <= 0 {
1795		return ""
1796	}
1797
1798	callID := content[:idx]
1799	return toolCallNames[callID]
1800}
1801
1802func (m *Mysis) toolCallNameIndex(memories []*store.Memory) map[string]string {
1803	index := make(map[string]string)
1804	for _, mem := range memories {
1805		if mem.Role != store.MemoryRoleAssistant {
1806			continue
1807		}
1808		if !strings.HasPrefix(mem.Content, constants.ToolCallStoragePrefix) {
1809			continue
1810		}
1811		calls := m.parseStoredToolCalls(mem.Content)
1812		for _, call := range calls {
1813			if call.ID == "" || call.Name == "" {
1814				continue
1815			}
1816			index[call.ID] = call.Name
1817		}
1818	}
1819
1820	return index
1821}
1822
1823func (m *Mysis) isSnapshotTool(toolName string) bool {
1824	if toolName == "" {
1825		return false
1826	}
1827	if strings.HasPrefix(toolName, "get_") {
1828		return true
1829	}
1830	return false
1831}
1832
1833// run is the mysis main processing loop.
1834// Takes autonomous turns until reaching idle state (after 3 encouragements with no user messages).
1835// According to MESSAGE_FORMAT_GUARANTEES.md lines 100-121:
1836// - Turn 1: getContextMemories() adds synthetic, counter=1, LLM responds
1837// - Turn 2: Still no user message, synthetic added, counter=2, LLM responds
1838// - Turn 3: Still no user message, synthetic added, counter=3, transitions to idle
1839// External messages (broadcast/direct) call SendMessageFrom directly, which handles subsequent turns.
1840func (m *Mysis) run(ctx context.Context) {
1841	a := m
1842	// Signal goroutine completion when exiting
1843	if a.commander != nil {
1844		defer a.commander.wg.Done()
1845	}
1846
1847	// Autonomous turn loop - continues until idle, stopped, errored, or context canceled
1848	for {
1849		// Process one turn using existing SendMessageFrom infrastructure
1850		// Use empty content - getContextMemories() will add synthetic encouragement if needed
1851		// This reuses all the complex LLM loop logic, tool calling, error handling, etc.
1852		if err := a.SendMessageFrom("", store.MemorySourceSystem, ""); err != nil {
1853			log.Debug().Err(err).Str("mysis", a.name).Msg("Error processing autonomous turn")
1854			// Error already handled by SendMessageFrom -> setError()
1855			return
1856		}
1857
1858		// Check state after turn completes
1859		a.mu.RLock()
1860		state := a.state
1861		a.mu.RUnlock()
1862
1863		// Exit if mysis transitioned to terminal state
1864		switch state {
1865		case MysisStateIdle:
1866			log.Debug().Str("mysis", a.name).Msg("Autonomous turn loop exiting - mysis idle")
1867			return
1868		case MysisStateStopped:
1869			log.Debug().Str("mysis", a.name).Msg("Autonomous turn loop exiting - mysis stopped")
1870			return
1871		case MysisStateErrored:
1872			log.Debug().Str("mysis", a.name).Msg("Autonomous turn loop exiting - mysis errored")
1873			return
1874		}
1875
1876		// Wait before next turn (2 seconds delay)
1877		select {
1878		case <-time.After(2 * time.Second):
1879			// Continue to next turn
1880		case <-ctx.Done():
1881			// Context canceled (Stop() called)
1882			log.Debug().Str("mysis", a.name).Msg("Autonomous turn loop exiting - context canceled")
1883			return
1884		}
1885	}
1886}
1887
1888// buildSystemPrompt creates the system prompt with the latest swarm broadcast injected.
1889func (m *Mysis) buildSystemPrompt() string {
1890	a := m
1891	base := constants.SystemPrompt
1892
1893	// Get most recent commander broadcast (sender_id is empty for commander broadcasts)
1894	broadcasts, err := a.store.GetRecentBroadcasts(10) // Get more to filter for commander broadcasts
1895	if err != nil {
1896		base = strings.Replace(base, "{{LATEST_BROADCAST}}", constants.BroadcastFallback, 1)
1897	} else {
1898		// Find the most recent commander broadcast (empty sender_id)
1899		var commanderBroadcast *store.BroadcastMessage
1900		for _, b := range broadcasts {
1901			if b.SenderID == "" {
1902				commanderBroadcast = b
1903				break
1904			}
1905		}
1906
1907		if commanderBroadcast == nil {
1908			// No commander broadcasts yet - show fallback
1909			base = strings.Replace(base, "{{LATEST_BROADCAST}}", constants.BroadcastFallback, 1)
1910		} else {
1911			// Format broadcast section (only content, no sender name)
1912			broadcastSection := fmt.Sprintf(constants.BroadcastSectionTemplate, commanderBroadcast.Content)
1913			base = strings.Replace(base, "{{LATEST_BROADCAST}}", broadcastSection, 1)
1914		}
1915	}
1916
1917	prompt := base
1918
1919	// Replace {{ACCOUNT_DETAILS}} with current credentials
1920	accountDetails := constants.AccountDetailsFallback
1921	username := m.CurrentAccountUsername()
1922	password := m.CurrentPassword()
1923
1924	if username != "" {
1925		accountDetails = fmt.Sprintf(constants.AccountDetailsTemplate, username, password)
1926	}
1927
1928	prompt = strings.Replace(prompt, "{{ACCOUNT_DETAILS}}", accountDetails, 1)
1929
1930	// Replace {{GAME_STATE_SUMMARY}} with cached game state
1931	gameStateSummary := m.buildGameStateSummary()
1932	prompt = strings.Replace(prompt, "{{GAME_STATE_SUMMARY}}", gameStateSummary, 1)
1933
1934	return prompt
1935}
1936
1937// buildGameStateSummary creates a compact summary of cached game state.
1938func (m *Mysis) buildGameStateSummary() string {
1939	username := m.CurrentAccountUsername()
1940	if username == "" {
1941		return constants.GameStateSummaryFallback
1942	}
1943
1944	snapshots, err := m.store.GetAllGameStateSnapshots(username)
1945	if err != nil || len(snapshots) == 0 {
1946		return constants.GameStateSummaryFallback
1947	}
1948
1949	// Get current tick for recency calculation
1950	currentTick := m.lastServerTick
1951
1952	var summary strings.Builder
1953	summary.WriteString("\n## Cached Game State\n")
1954	summary.WriteString(fmt.Sprintf("Account: %s\n\n", username))
1955
1956	// Group snapshots by category
1957	statusTools := []string{"get_status", "get_player"}
1958	shipTools := []string{"get_ship", "get_cargo"}
1959	locationTools := []string{"get_system", "get_location", "get_poi"}
1960	mapTools := []string{"get_map", "get_galaxy"}
1961
1962	writeSnapshotGroup := func(title string, toolNames []string) {
1963		for _, snapshot := range snapshots {
1964			for _, toolName := range toolNames {
1965				if snapshot.ToolName == toolName {
1966					recency := snapshot.RecencyMessage(currentTick)
1967					summary.WriteString(fmt.Sprintf("### %s (%s)\n", toolName, recency))
1968
1969					// Render a safe summary without schema assumptions
1970					lines := gamestate.SnapshotLines(snapshot.Content)
1971					for _, line := range lines {
1972						summary.WriteString("- ")
1973						summary.WriteString(line)
1974						summary.WriteString("\n")
1975					}
1976					summary.WriteString("\n")
1977				}
1978			}
1979		}
1980	}
1981
1982	writeSnapshotGroup("Status", statusTools)
1983	writeSnapshotGroup("Ship", shipTools)
1984	writeSnapshotGroup("Location", locationTools)
1985	writeSnapshotGroup("Map", mapTools)
1986
1987	// Add note about refreshing stale data
1988	summary.WriteString("Note: Call get_notifications to see events since last update.\n")
1989	summary.WriteString("Refresh any stale snapshots (>50 ticks old) before making decisions.\n")
1990
1991	return summary.String()
1992}
1993
1994// rebuildSystemMemory rebuilds the system memory with current state
1995func (m *Mysis) rebuildSystemMemory() error {
1996	// Build new system prompt with current credentials and broadcasts
1997	newPrompt := m.buildSystemPrompt()
1998
1999	// Delete old system memory
2000	if err := m.store.DeleteSystemMemory(m.id); err != nil {
2001		return fmt.Errorf("delete old system memory: %w", err)
2002	}
2003
2004	// Store new system memory
2005	if err := m.store.AddMemory(m.id, store.MemoryRoleSystem, store.MemorySourceSystem, newPrompt, "", ""); err != nil {
2006		return fmt.Errorf("add new system memory: %w", err)
2007	}
2008
2009	return nil
2010}
2011
2012func (m *Mysis) updateActivityFromToolResult(result *mcp.ToolResult, err error) {
2013	a := m
2014	if err != nil || result == nil || result.IsError {
2015		return
2016	}
2017
2018	now := time.Now()
2019	payload, ok := parseToolResultPayload(result)
2020	var currentTick int64
2021	var currentTickOK bool
2022	if ok {
2023		currentTick, currentTickOK = findCurrentTick(payload)
2024		if currentTickOK {
2025			a.updateServerTick(now, currentTick)
2026		}
2027	}
2028
2029	arrivalTick, found := findIntField(payload, "arrival_tick", "travel_arrival_tick")
2030	if found {
2031		if currentTickOK && arrivalTick <= currentTick {
2032			a.setActivity(ActivityStateIdle, time.Time{})
2033			return
2034		}
2035		until := a.estimateTravelUntil(now, arrivalTick, currentTick, currentTickOK)
2036		a.setActivity(ActivityStateTraveling, until)
2037		return
2038	}
2039
2040	if progress, ok := findFloatField(payload, "travel_progress"); ok {
2041		if progress >= 1 {
2042			a.setActivity(ActivityStateIdle, time.Time{})
2043			return
2044		}
2045		if progress > 0 {
2046			a.setActivity(ActivityStateTraveling, now.Add(constants.WaitStateNudgeInterval))
2047			return
2048		}
2049	}
2050
2051	if cooldownTicks, found := findIntField(payload, "cooldown_ticks", "cooldown_remaining"); found && cooldownTicks > 0 {
2052		until := a.estimateCooldownUntil(now, cooldownTicks)
2053		a.setActivity(ActivityStateCooldown, until)
2054		return
2055	}
2056
2057}
2058
2059// cacheSnapshotToolResult stores successful snapshot tool results in the game state cache.
2060func (m *Mysis) cacheSnapshotToolResult(toolName string, result *mcp.ToolResult, err error) {
2061	// Only cache successful snapshot tools
2062	if err != nil || result == nil || result.IsError {
2063		return
2064	}
2065
2066	// Only cache get_* tools
2067	if !strings.HasPrefix(toolName, "get_") {
2068		return
2069	}
2070
2071	// Extract game tick and username from result
2072	payload, ok := parseToolResultPayload(result)
2073	if !ok {
2074		return
2075	}
2076
2077	// Get username from current account
2078	username := m.CurrentAccountUsername()
2079	if username == "" {
2080		// Try to extract from result payload (nested under player.username)
2081		if payloadMap, ok := payload.(map[string]interface{}); ok {
2082			if player, ok := payloadMap["player"].(map[string]interface{}); ok {
2083				if extractedUsername, ok := player["username"].(string); ok && extractedUsername != "" {
2084					username = extractedUsername
2085					// NOTE: Don't call setCurrentAccount here - we don't have the password
2086					// and would overwrite it with empty string. The username extraction
2087					// is only for caching purposes when account isn't set yet.
2088					// If account needs to be set, it should be done during login/register
2089					// tool execution where we have the full credentials.
2090				}
2091			}
2092		}
2093	}
2094
2095	// Still no username - can't cache
2096	if username == "" {
2097		return
2098	}
2099
2100	gameTick, _ := findCurrentTick(payload)
2101
2102	// Get full result content
2103	var content strings.Builder
2104	for _, block := range result.Content {
2105		if block.Type == "text" {
2106			content.WriteString(block.Text)
2107		}
2108	}
2109
2110	if content.Len() == 0 {
2111		return
2112	}
2113
2114	// Store in cache (ignore errors - caching is best-effort)
2115	if err := m.store.StoreGameStateSnapshot(username, toolName, content.String(), gameTick); err == nil {
2116		if rebuildErr := m.rebuildSystemMemory(); rebuildErr != nil {
2117			log.Error().Err(rebuildErr).Msg("failed to rebuild system memory after snapshot update")
2118		}
2119	}
2120}
2121
2122func (m *Mysis) setActivity(state ActivityState, until time.Time) {
2123	a := m
2124	a.mu.Lock()
2125	a.activityState = state
2126	a.activityUntil = until
2127	a.mu.Unlock()
2128}
2129
2130func (m *Mysis) clearActivityIf(state ActivityState) {
2131	a := m
2132	a.mu.Lock()
2133	if a.activityState == state {
2134		a.activityState = ActivityStateIdle
2135		a.activityUntil = time.Time{}
2136	}
2137	a.mu.Unlock()
2138}
2139
2140func (m *Mysis) updateServerTick(now time.Time, tick int64) {
2141	a := m
2142	if tick <= 0 {
2143		return
2144	}
2145
2146	a.mu.Lock()
2147	defer a.mu.Unlock()
2148
2149	if a.lastServerTick > 0 && tick > a.lastServerTick && !a.lastServerTickAt.IsZero() {
2150		elapsed := now.Sub(a.lastServerTickAt)
2151		delta := tick - a.lastServerTick
2152		if elapsed > 0 && delta > 0 {
2153			a.tickDuration = elapsed / time.Duration(delta)
2154		}
2155	}
2156
2157	a.lastServerTick = tick
2158	a.lastServerTickAt = now
2159}
2160
2161func (m *Mysis) estimateTravelUntil(now time.Time, arrivalTick, currentTick int64, currentTickOK bool) time.Time {
2162	a := m
2163	if currentTickOK {
2164		a.mu.RLock()
2165		tickDuration := a.tickDuration
2166		a.mu.RUnlock()
2167		if tickDuration > 0 && arrivalTick > currentTick {
2168			return now.Add(time.Duration(arrivalTick-currentTick) * tickDuration)
2169		}
2170	}
2171
2172	return now.Add(constants.WaitStateNudgeInterval)
2173}
2174
2175func (m *Mysis) estimateCooldownUntil(now time.Time, cooldownTicks int64) time.Time {
2176	a := m
2177	a.mu.RLock()
2178	tickDuration := a.tickDuration
2179	a.mu.RUnlock()
2180
2181	if tickDuration > 0 && cooldownTicks > 0 {
2182		return now.Add(time.Duration(cooldownTicks) * tickDuration)
2183	}
2184
2185	return now.Add(constants.WaitStateNudgeInterval)
2186}
2187
2188func parseToolResultPayload(result *mcp.ToolResult) (interface{}, bool) {
2189	if result == nil {
2190		return nil, false
2191	}
2192
2193	var texts []string
2194	for _, block := range result.Content {
2195		if block.Type == "text" {
2196			texts = append(texts, block.Text)
2197		}
2198	}
2199
2200	content := strings.TrimSpace(strings.Join(texts, "\n"))
2201	if content == "" {
2202		return nil, false
2203	}
2204	if !strings.HasPrefix(content, "{") && !strings.HasPrefix(content, "[") {
2205		return nil, false
2206	}
2207
2208	decoder := json.NewDecoder(strings.NewReader(content))
2209	decoder.UseNumber()
2210	var payload interface{}
2211	if err := decoder.Decode(&payload); err != nil {
2212		return nil, false
2213	}
2214
2215	return payload, true
2216}
2217
2218func findIntField(payload interface{}, keys ...string) (int64, bool) {
2219	queue := []interface{}{payload}
2220
2221	for len(queue) > 0 {
2222		current := queue[0]
2223		queue = queue[1:]
2224
2225		switch value := current.(type) {
2226		case map[string]interface{}:
2227			for _, key := range keys {
2228				if raw, ok := value[key]; ok {
2229					if number, ok := normalizeInt(raw); ok {
2230						return number, true
2231					}
2232				}
2233			}
2234
2235			childKeys := make([]string, 0, len(value))
2236			for key := range value {
2237				childKeys = append(childKeys, key)
2238			}
2239			sort.Strings(childKeys)
2240			for _, key := range childKeys {
2241				queue = append(queue, value[key])
2242			}
2243		case []interface{}:
2244			queue = append(queue, value...)
2245		}
2246	}
2247
2248	return 0, false
2249}
2250
2251func findFloatField(payload interface{}, keys ...string) (float64, bool) {
2252	queue := []interface{}{payload}
2253
2254	for len(queue) > 0 {
2255		current := queue[0]
2256		queue = queue[1:]
2257
2258		switch value := current.(type) {
2259		case map[string]interface{}:
2260			for _, key := range keys {
2261				if raw, ok := value[key]; ok {
2262					if number, ok := normalizeFloat(raw); ok {
2263						return number, true
2264					}
2265				}
2266			}
2267
2268			childKeys := make([]string, 0, len(value))
2269			for key := range value {
2270				childKeys = append(childKeys, key)
2271			}
2272			sort.Strings(childKeys)
2273			for _, key := range childKeys {
2274				queue = append(queue, value[key])
2275			}
2276		case []interface{}:
2277			queue = append(queue, value...)
2278		}
2279	}
2280
2281	return 0, false
2282}
2283
2284func findStringField(payload interface{}, keys ...string) (string, bool) {
2285	queue := []interface{}{payload}
2286
2287	for len(queue) > 0 {
2288		current := queue[0]
2289		queue = queue[1:]
2290
2291		switch value := current.(type) {
2292		case map[string]interface{}:
2293			for _, key := range keys {
2294				if raw, ok := value[key]; ok {
2295					if str, ok := raw.(string); ok {
2296						return str, true
2297					}
2298				}
2299			}
2300
2301			childKeys := make([]string, 0, len(value))
2302			for key := range value {
2303				childKeys = append(childKeys, key)
2304			}
2305			sort.Strings(childKeys)
2306			for _, key := range childKeys {
2307				queue = append(queue, value[key])
2308			}
2309		case []interface{}:
2310			queue = append(queue, value...)
2311		}
2312	}
2313
2314	return "", false
2315}
2316
2317func findCurrentTick(payload interface{}) (int64, bool) {
2318	if number, ok := findIntFieldAtKeys(payload, "current_tick"); ok {
2319		return number, true
2320	}
2321	if number, ok := findIntFieldInWrappers(payload, "current_tick"); ok {
2322		return number, true
2323	}
2324	if number, ok := findIntField(payload, "current_tick"); ok {
2325		return number, true
2326	}
2327	if number, ok := findIntFieldAtKeys(payload, "tick"); ok {
2328		return number, true
2329	}
2330	if number, ok := findIntFieldInWrappers(payload, "tick"); ok {
2331		return number, true
2332	}
2333	if number, ok := findIntField(payload, "tick"); ok {
2334		return number, true
2335	}
2336	return 0, false
2337}
2338
2339func findIntFieldAtKeys(payload interface{}, keys ...string) (int64, bool) {
2340	value, ok := payload.(map[string]interface{})
2341	if !ok {
2342		return 0, false
2343	}
2344
2345	for _, key := range keys {
2346		if raw, ok := value[key]; ok {
2347			if number, ok := normalizeInt(raw); ok {
2348				return number, true
2349			}
2350		}
2351	}
2352
2353	return 0, false
2354}
2355
2356func findIntFieldInWrappers(payload interface{}, key string) (int64, bool) {
2357	value, ok := payload.(map[string]interface{})
2358	if !ok {
2359		return 0, false
2360	}
2361
2362	wrappers := []string{"data", "result", "payload", "response"}
2363	for _, wrapper := range wrappers {
2364		child, ok := value[wrapper]
2365		if !ok {
2366			continue
2367		}
2368		if number, ok := findIntField(child, key); ok {
2369			return number, true
2370		}
2371	}
2372
2373	return 0, false
2374}
2375
2376func normalizeInt(value interface{}) (int64, bool) {
2377	switch v := value.(type) {
2378	case int:
2379		return int64(v), true
2380	case int64:
2381		return v, true
2382	case float64:
2383		return int64(v), true
2384	case json.Number:
2385		number, err := v.Int64()
2386		if err != nil {
2387			return 0, false
2388		}
2389		return number, true
2390	case string:
2391		number, err := strconv.ParseInt(v, 10, 64)
2392		if err != nil {
2393			return 0, false
2394		}
2395		return number, true
2396	default:
2397		return 0, false
2398	}
2399}
2400
2401func normalizeFloat(value interface{}) (float64, bool) {
2402	switch v := value.(type) {
2403	case int:
2404		return float64(v), true
2405	case int64:
2406		return float64(v), true
2407	case float64:
2408		return v, true
2409	case json.Number:
2410		number, err := v.Float64()
2411		if err != nil {
2412			return 0, false
2413		}
2414		return number, true
2415	case string:
2416		number, err := strconv.ParseFloat(v, 64)
2417		if err != nil {
2418			return 0, false
2419		}
2420		return number, true
2421	default:
2422		return 0, false
2423	}
2424}
2425
2426func (m *Mysis) emitStateChange(oldState, newState MysisState) {
2427	a := m
2428	a.publishCriticalEvent(Event{
2429		Type:      EventMysisStateChanged,
2430		MysisID:   a.id,
2431		MysisName: a.name,
2432		State: &StateChangeData{
2433			OldState: oldState,
2434			NewState: newState,
2435		},
2436		Timestamp: time.Now(),
2437	})
2438}
2439
2440func (m *Mysis) setErrorState(err error) {
2441	a := m
2442	a.mu.Lock()
2443	oldState := a.state
2444
2445	// If mysis was intentionally stopped, don't override with error state
2446	if oldState == MysisStateStopped {
2447		a.mu.Unlock()
2448		log.Debug().Str("mysis", a.name).Err(err).Msg("Ignoring error - mysis was intentionally stopped")
2449		return
2450	}
2451
2452	a.state = MysisStateErrored
2453	a.lastError = err
2454	a.mu.Unlock()
2455
2456	if err := a.store.UpdateMysisState(a.id, store.MysisStateErrored); err != nil {
2457		log.Warn().
2458			Err(err).
2459			Str("mysis_id", a.id).
2460			Str("mysis_name", a.name).
2461			Msg("failed to update mysis state to errored")
2462	}
2463	a.emitStateChange(oldState, MysisStateErrored)
2464
2465	a.publishCriticalEvent(Event{
2466		Type:      EventMysisError,
2467		MysisID:   a.id,
2468		MysisName: a.name,
2469		Error:     &ErrorData{Error: err.Error()},
2470		Timestamp: time.Now(),
2471	})
2472}
2473
2474func (m *Mysis) publishCriticalEvent(event Event) {
2475	a := m
2476	if a.bus.PublishBlocking(event, constants.EventBusPublishTimeout) {
2477		return
2478	}
2479
2480	log.Warn().
2481		Str("event_type", string(event.Type)).
2482		Str("mysis_id", a.id).
2483		Str("mysis_name", a.name).
2484		Msg("event bus publish timeout")
2485}
2486
2487// collectValidToolCallIDs extracts all tool call IDs from assistant messages.
2488// Used to validate that tool result messages reference existing tool calls.
2489func (m *Mysis) collectValidToolCallIDs(memories []*store.Memory) map[string]bool {
2490	validToolCalls := make(map[string]bool)
2491
2492	for _, mem := range memories {
2493		if mem.Role == store.MemoryRoleAssistant &&
2494			strings.HasPrefix(mem.Content, constants.ToolCallStoragePrefix) {
2495			calls := m.parseStoredToolCalls(mem.Content)
2496			for _, call := range calls {
2497				validToolCalls[call.ID] = true
2498			}
2499		}
2500	}
2501
2502	return validToolCalls
2503}
2504
2505// removeOrphanedToolMessages removes tool result messages that don't have
2506// a corresponding assistant tool call message. This can happen due to:
2507// 1. Context window cutting off tool calls but keeping results
2508// 2. Context compaction removing tool call messages
2509//
2510// OpenAI Chat Completions API requires tool results to reference valid tool calls.
2511func (m *Mysis) removeOrphanedToolMessages(memories []*store.Memory) []*store.Memory {
2512	validToolCalls := m.collectValidToolCallIDs(memories)
2513
2514	result := make([]*store.Memory, 0, len(memories))
2515	for _, mem := range memories {
2516		// Check if this is a tool result message
2517		if mem.Role == store.MemoryRoleTool {
2518			idx := strings.Index(mem.Content, constants.ToolCallStorageFieldDelimiter)
2519			if idx > 0 {
2520				toolCallID := mem.Content[:idx]
2521				if !validToolCalls[toolCallID] {
2522					log.Debug().
2523						Str("tool_call_id", toolCallID).
2524						Msg("Removing orphaned tool result - no matching tool call")
2525					continue // Skip orphaned result
2526				}
2527			} else {
2528				// Malformed tool result - skip it
2529				log.Warn().
2530					Str("content", mem.Content).
2531					Msg("Skipping malformed tool result")
2532				continue
2533			}
2534		}
2535		result = append(result, mem)
2536	}
2537
2538	if len(result) < len(memories) {
2539		log.Debug().
2540			Int("removed", len(memories)-len(result)).
2541			Int("original", len(memories)).
2542			Int("final", len(result)).
2543			Msg("Removed orphaned tool messages for OpenAI compliance")
2544	}
2545
2546	return result
2547}