@@ -581,6 +581,18 @@ func (m *Mysis) SendMessageFrom(content string, source store.MemorySource, sende
581581 Message: &MessageData{Role: "tool", Content: fmt.Sprintf("[%s] %s", tc.Name, a.formatToolResultDisplay(result, execErr))},
582582 Timestamp: time.Now(),
583583 })
584+
585+ if execErr != nil && isToolTimeout(execErr) {
586+ a.bus.Publish(Event{Type: EventNetworkIdle, MysisID: a.id, Timestamp: time.Now()})
587+ a.setError(execErr)
588+ return fmt.Errorf("tool call timed out: %w", execErr)
589+ }
590+
591+ if execErr != nil && isToolRetryExhausted(execErr) {
592+ a.bus.Publish(Event{Type: EventNetworkIdle, MysisID: a.id, Timestamp: time.Now()})
593+ a.setError(execErr)
594+ return fmt.Errorf("tool call failed after retries: %w", execErr)
595+ }
584596 }
585597
586598 // Continue loop to get next LLM response
@@ -858,6 +870,18 @@ func (m *Mysis) SendEphemeralMessage(content string, source store.MemorySource)
858870 Message: &MessageData{Role: "tool", Content: fmt.Sprintf("[%s] %s", tc.Name, a.formatToolResultDisplay(result, execErr))},
859871 Timestamp: time.Now(),
860872 })
873+
874+ if execErr != nil && isToolTimeout(execErr) {
875+ a.bus.Publish(Event{Type: EventNetworkIdle, MysisID: a.id, Timestamp: time.Now()})
876+ a.setError(execErr)
877+ return fmt.Errorf("tool call timed out: %w", execErr)
878+ }
879+
880+ if execErr != nil && isToolRetryExhausted(execErr) {
881+ a.bus.Publish(Event{Type: EventNetworkIdle, MysisID: a.id, Timestamp: time.Now()})
882+ a.setError(execErr)
883+ return fmt.Errorf("tool call failed after retries: %w", execErr)
884+ }
861885 }
862886
863887 // Continue loop to get next LLM response
@@ -1097,7 +1121,13 @@ func (m *Mysis) parseStoredToolCalls(stored string) []provider.ToolCall {
10971121 // formatToolResult formats a tool result for storage (includes ID for LLM context).
10981122 func (m *Mysis) formatToolResult(toolCallID, toolName string, result *mcp.ToolResult, err error) string {
10991123 if err != nil {
1100- return fmt.Sprintf("%s:Error calling %s: %v. Check the tool's required parameters and try again.", toolCallID, toolName, err)
1124+ if errors.Is(err, context.DeadlineExceeded) {
1125+ return fmt.Sprintf("%s%sError calling %s: tool call timed out: %v", toolCallID, constants.ToolCallStorageFieldDelimiter, toolName, err)
1126+ }
1127+ if errors.Is(err, mcp.ErrToolRetryExhausted) {
1128+ return fmt.Sprintf("%s%sError calling %s: MCP call failed after retries: %v", toolCallID, constants.ToolCallStorageFieldDelimiter, toolName, err)
1129+ }
1130+ return fmt.Sprintf("%s%sError calling %s: %v. Check the tool's required parameters and try again.", toolCallID, constants.ToolCallStorageFieldDelimiter, toolName, err)
11011131 }
11021132
11031133 var texts []string
@@ -1119,6 +1149,14 @@ func (m *Mysis) formatToolResult(toolCallID, toolName string, result *mcp.ToolRe
11191149 return fmt.Sprintf("%s%s%s", toolCallID, constants.ToolCallStorageFieldDelimiter, content)
11201150 }
11211151
1152+func isToolTimeout(err error) bool {
1153+ return errors.Is(err, context.DeadlineExceeded)
1154+}
1155+
1156+func isToolRetryExhausted(err error) bool {
1157+ return errors.Is(err, mcp.ErrToolRetryExhausted)
1158+}
1159+
11221160 // formatToolResultDisplay formats a tool result for UI display (human-readable).
11231161 func (m *Mysis) formatToolResultDisplay(result *mcp.ToolResult, err error) string {
11241162 if err != nil {
@@ -1192,6 +1230,40 @@ func (m *Mysis) setError(err error) {
11921230 })
11931231 }
11941232
1233+func (m *Mysis) setIdle(reason string) {
1234+ a := m
1235+ a.mu.Lock()
1236+ oldState := a.state
1237+
1238+ if oldState == MysisStateStopped {
1239+ a.mu.Unlock()
1240+ log.Debug().Str("mysis", a.name).Str("reason", reason).Msg("Ignoring idle transition - mysis was intentionally stopped")
1241+ return
1242+ }
1243+
1244+ if oldState == MysisStateIdle {
1245+ a.mu.Unlock()
1246+ return
1247+ }
1248+
1249+ log.Warn().
1250+ Str("mysis", a.name).
1251+ Str("old_state", string(oldState)).
1252+ Str("reason", reason).
1253+ Msg("Mysis transitioning to idle state")
1254+
1255+ a.state = MysisStateIdle
1256+ a.lastError = nil
1257+ a.mu.Unlock()
1258+
1259+ if updateErr := a.store.UpdateMysisState(a.id, store.MysisStateIdle); updateErr != nil {
1260+ log.Warn().Err(updateErr).Str("mysis", a.id).Msg("Failed to update state in store")
1261+ }
1262+
1263+ a.emitStateChange(oldState, MysisStateIdle)
1264+ a.releaseCurrentAccount()
1265+}
1266+
11951267 // getContextMemories returns memories for LLM context: system prompt + recent messages.
11961268 // This keeps context small for faster inference while preserving essential information.
11971269 // Compacts repeated snapshot tool results to prefer recent state.
@@ -1449,7 +1521,7 @@ func (m *Mysis) run(ctx context.Context) {
14491521
14501522 // If we've failed 3 times, transition to error state
14511523 if failCount >= 3 {
1452- a.setError(errors.New("Failed to respond after 3 nudges"))
1524+ a.setIdle("Failed to respond after 3 nudges")
14531525 return
14541526 }
14551527
@@ -1598,16 +1670,12 @@ func (m *Mysis) shouldNudge(now time.Time) bool {
15981670 a.mu.Lock()
15991671 defer a.mu.Unlock()
16001672
1601- if a.activityState != ActivityStateTraveling && a.activityState != ActivityStateCooldown {
1602- return true
1603- }
1604-
1605- if !a.activityUntil.IsZero() && now.Before(a.activityUntil) {
1606- return false
1673+ if a.activityState == ActivityStateTraveling || a.activityState == ActivityStateCooldown {
1674+ if !a.activityUntil.IsZero() && !now.Before(a.activityUntil) {
1675+ a.activityState = ActivityStateIdle
1676+ a.activityUntil = time.Time{}
1677+ }
16071678 }
1608-
1609- a.activityState = ActivityStateIdle
1610- a.activityUntil = time.Time{}
16111679 return true
16121680 }
16131681