From 807370eb8aa056dd3118799fef97e2cf4b94e10d Mon Sep 17 00:00:00 2001
From: Mark Chao <mchao@gitlab.com>
Date: Tue, 22 Oct 2024 15:53:30 +0000
Subject: [PATCH] Log LLM ReAct turn count

---
 ee/lib/gitlab/duo/chat/react_executor.rb           | 8 ++++++--
 ee/lib/gitlab/llm/concerns/logger.rb               | 1 +
 ee/spec/lib/gitlab/duo/chat/react_executor_spec.rb | 9 +++++++++
 3 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/ee/lib/gitlab/duo/chat/react_executor.rb b/ee/lib/gitlab/duo/chat/react_executor.rb
index cdbc70dfade00..514bb9f133b4b 100644
--- a/ee/lib/gitlab/duo/chat/react_executor.rb
+++ b/ee/lib/gitlab/duo/chat/react_executor.rb
@@ -33,7 +33,7 @@ def initialize(user_input:, tools:, context:, response_handler:, stream_response
         end
 
         def execute
-          MAX_ITERATIONS.times do
+          MAX_ITERATIONS.times do |i|
             events = step_forward
 
             raise EmptyEventsError if events.empty?
@@ -42,7 +42,11 @@ def execute
               process_tool_action(events) ||
               process_unknown(events)
 
-            return answer if answer
+            next unless answer
+
+            log_info(message: "ReAct turn", react_turn: i, event_name: 'react_turn', ai_component: 'duo_chat')
+
+            return answer
           end
 
           raise ExhaustedLoopError
diff --git a/ee/lib/gitlab/llm/concerns/logger.rb b/ee/lib/gitlab/llm/concerns/logger.rb
index ecfc9e7aa0233..475b98d92dfdf 100644
--- a/ee/lib/gitlab/llm/concerns/logger.rb
+++ b/ee/lib/gitlab/llm/concerns/logger.rb
@@ -37,6 +37,7 @@ module Logger
             Attribute.new(:picked_tool, String),
             Attribute.new(:allowed, String),
             Attribute.new(:tool_name, String),
+            Attribute.new(:react_turn, Integer),
             Attribute.new(:ai_event, String),
             Attribute.new(:params, String),
             Attribute.new(:status, Integer),
diff --git a/ee/spec/lib/gitlab/duo/chat/react_executor_spec.rb b/ee/spec/lib/gitlab/duo/chat/react_executor_spec.rb
index 399710e509dd7..ea60218e6cc6d 100644
--- a/ee/spec/lib/gitlab/duo/chat/react_executor_spec.rb
+++ b/ee/spec/lib/gitlab/duo/chat/react_executor_spec.rb
@@ -107,6 +107,9 @@
       end
 
       it "streams final answer" do
+        expect(agent).to receive(:log_info).with(
+          message: "ReAct turn", react_turn: 0, event_name: 'react_turn', ai_component: 'duo_chat')
+
         expect(stream_response_service_double).to receive(:execute).with(
           response: first_response_double,
           options: { chunk_id: 1 }
@@ -144,6 +147,9 @@
       end
 
       it "returns tool answer" do
+        expect(agent).to receive(:log_info).with(
+          message: "ReAct turn", react_turn: 0, event_name: 'react_turn', ai_component: 'duo_chat')
+
         expect(answer.is_final?).to be_truthy
         expect(answer.content).to include("tool answer")
       end
@@ -220,6 +226,9 @@
       end
 
       it "returns unknown answer as is" do
+        expect(agent).to receive(:log_info).with(
+          message: "ReAct turn", react_turn: 0, event_name: 'react_turn', ai_component: 'duo_chat')
+
         expect(answer.content).to include('foo')
       end
     end
-- 
GitLab