@@ -1471,12 +1471,12 @@ func (m *Mysis) isSnapshotTool(toolName string) bool {
14711471 }
14721472
14731473 // run is the mysis main processing loop.
1474-// Processes one initial turn after Start(), then waits for external messages.
1474+// Takes autonomous turns until reaching idle state (after 3 encouragements with no user messages).
14751475 // According to MESSAGE_FORMAT_GUARANTEES.md lines 100-121:
1476-// - Iteration 1: getContextMemories() adds synthetic, counter=1, LLM responds
1477-// - Iteration 2: Still no user message, synthetic added, counter=2, LLM responds
1478-// - Iteration 3: Still no user message, synthetic added, counter=3, transitions to idle
1479-// External messages (broadcast/direct) call SendMessageFrom which handles subsequent turns.
1476+// - Turn 1: getContextMemories() adds synthetic, counter=1, LLM responds
1477+// - Turn 2: Still no user message, synthetic added, counter=2, LLM responds
1478+// - Turn 3: Still no user message, synthetic added, counter=3, transitions to idle
1479+// External messages (broadcast/direct) call SendMessageFrom directly, which handles subsequent turns.
14801480 func (m *Mysis) run(ctx context.Context) {
14811481 a := m
14821482 // Signal goroutine completion when exiting
@@ -1484,28 +1484,45 @@ func (m *Mysis) run(ctx context.Context) {
14841484 defer a.commander.wg.Done()
14851485 }
14861486
1487- // Process one initial turn using existing SendMessageFrom infrastructure
1488- // Use empty content - getContextMemories() will add synthetic encouragement if needed
1489- // This reuses all the complex LLM loop logic, tool calling, error handling, etc.
1490- if err := a.SendMessageFrom("", store.MemorySourceSystem, ""); err != nil {
1491- log.Debug().Err(err).Str("mysis", a.name).Msg("Error processing initial turn")
1492- // Error already handled by SendMessageFrom -> setError()
1493- return
1494- }
1487+ // Autonomous turn loop - continues until idle, stopped, errored, or context canceled
1488+ for {
1489+ // Process one turn using existing SendMessageFrom infrastructure
1490+ // Use empty content - getContextMemories() will add synthetic encouragement if needed
1491+ // This reuses all the complex LLM loop logic, tool calling, error handling, etc.
1492+ if err := a.SendMessageFrom("", store.MemorySourceSystem, ""); err != nil {
1493+ log.Debug().Err(err).Str("mysis", a.name).Msg("Error processing autonomous turn")
1494+ // Error already handled by SendMessageFrom -> setError()
1495+ return
1496+ }
14951497
1496- // Check if we transitioned to idle during the initial turn
1497- a.mu.RLock()
1498- state := a.state
1499- a.mu.RUnlock()
1498+ // Check state after turn completes
1499+ a.mu.RLock()
1500+ state := a.state
1501+ a.mu.RUnlock()
15001502
1501- if state == MysisStateIdle {
1502- // Mysis went idle (3 encouragements reached or other reason)
1503- return
1504- }
1503+ // Exit if mysis transitioned to terminal state
1504+ switch state {
1505+ case MysisStateIdle:
1506+ log.Debug().Str("mysis", a.name).Msg("Autonomous turn loop exiting - mysis idle")
1507+ return
1508+ case MysisStateStopped:
1509+ log.Debug().Str("mysis", a.name).Msg("Autonomous turn loop exiting - mysis stopped")
1510+ return
1511+ case MysisStateErrored:
1512+ log.Debug().Str("mysis", a.name).Msg("Autonomous turn loop exiting - mysis errored")
1513+ return
1514+ }
15051515
1506- // Wait for context cancellation
1507- // External messages call SendMessageFrom directly, which handles its own turn processing
1508- <-ctx.Done()
1516+ // Wait before next turn (2 seconds delay)
1517+ select {
1518+ case <-time.After(2 * time.Second):
1519+ // Continue to next turn
1520+ case <-ctx.Done():
1521+ // Context canceled (Stop() called)
1522+ log.Debug().Str("mysis", a.name).Msg("Autonomous turn loop exiting - context canceled")
1523+ return
1524+ }
1525+ }
15091526 }
15101527
15111528 // buildSystemPrompt creates the system prompt with the latest swarm broadcast injected.