Создание помощника по проверке кода на основе ИИ с помощью Google ADK

1. Ночной обзор кода

Сейчас 2 часа ночи.

Вы отлаживали код часами. Функция выглядит правильно, но что-то не так. Вам знакомо это чувство: когда код должен работать, но не работает, и вы уже не понимаете, почему, потому что слишком долго на него смотрите.

def dfs_search_v1(graph, start, target):
    """Find if target is reachable from start."""
    visited = set()
    stack = start  # Looks innocent enough...
   
    while stack:
        current = stack.pop()
       
        if current == target:
            return True
           
        if current not in visited:
            visited.add(current)
           
            for neighbor in graph[current]:
                if neighbor not in visited:
                    stack.append(neighbor)
   
    return False

Путь разработчика ИИ

Если вы читаете это, вы, вероятно, уже ощутили, как искусственный интеллект преобразует программирование. Такие инструменты, как Gemini Code Assist , Claude Code и Cursor, изменили наш подход к написанию кода. Они отлично подходят для создания шаблонов, предложения вариантов реализации и ускорения разработки.

Но вы здесь, потому что хотите копнуть глубже. Вы хотите понять, как создавать эти системы ИИ, а не просто использовать их. Вы хотите создать что-то, что:

  • Имеет предсказуемое, прослеживаемое поведение
  • Можно с уверенностью внедрить в производство
  • Обеспечивает стабильные результаты, на которые вы можете положиться
  • Показывает вам, как именно он принимает решения

От потребителя к создателю

архитектура.png

Сегодня вы перейдёте от использования инструментов ИИ к их созданию. Вы создадите многоагентную систему, которая:

  1. Детерминированный анализ структуры кода
  2. Выполняет реальные тесты для проверки поведения
  3. Проверяет соответствие стиля реальным линтерам
  4. Обобщает результаты в действенную обратную связь
  5. Развертывается в Google Cloud с полной видимостью

2. Ваше первое развертывание агента

Вопрос разработчика

«Я понимаю, что такое LLM, я использовал API, но как перейти от скрипта Python к масштабируемому производственному ИИ-агенту?»

Давайте ответим на этот вопрос, правильно настроив вашу среду, а затем создав простой агент, чтобы понять основы, прежде чем углубляться в шаблоны производства.

Сначала необходимая настройка

Прежде чем создавать агентов, давайте убедимся, что ваша среда Google Cloud готова.

Нужны кредиты Google Cloud?

Нажмите «Активировать Cloud Shell» в верхней части консоли Google Cloud (это значок в форме терминала в верхней части панели Cloud Shell).

альтернативный текст

Найдите идентификатор вашего проекта Google Cloud:

  • Откройте консоль Google Cloud: https://console.cloud.google.com
  • Выберите проект, который вы хотите использовать для этого семинара, из раскрывающегося списка проектов в верхней части страницы.
  • Идентификатор вашего проекта отображается на карточке информации о проекте на панели инструментов. альтернативный текст

Шаг 1: Задайте идентификатор вашего проекта

В Cloud Shell инструмент командной строки gcloud уже настроен. Выполните следующую команду, чтобы задать активный проект. При этом используется переменная окружения $GOOGLE_CLOUD_PROJECT , которая автоматически устанавливается в сеансе Cloud Shell.

gcloud config set project $GOOGLE_CLOUD_PROJECT

Шаг 2: Проверьте свои настройки

Затем выполните следующие команды, чтобы подтвердить, что ваш проект настроен правильно и вы прошли аутентификацию.

# Confirm project is set
echo "Current project: $(gcloud config get-value project)"

# Check authentication status
gcloud auth list

Вы должны увидеть распечатанный идентификатор вашего проекта, а также вашу учетную запись пользователя с пометкой (ACTIVE) рядом с ней.

Если ваша учетная запись не указана как активная или возникла ошибка аутентификации, выполните следующую команду для входа в систему:

gcloud auth application-default login

Шаг 3: включите основные API

Нам нужны как минимум следующие API для базового агента:

gcloud services enable \
    aiplatform.googleapis.com \
    compute.googleapis.com

Это может занять минуту или две. Вы увидите:

Operation "operations/..." finished successfully.

Шаг 4: Установка ADK

# Install the ADK CLI
pip install google-adk --upgrade

# Verify installation
adk --version

Вы должны увидеть номер версии, например 1.15.0 или выше.

Теперь создайте своего базового агента

Подготовив среду, давайте создадим простого агента.

Шаг 5: Используйте ADK Create

adk create my_first_agent

Следуйте интерактивным подсказкам:

Choose a model for the root agent:
1. gemini-2.5-flash
2. Other models (fill later)
Choose model (1, 2): 1

1. Google AI
2. Vertex AI
Choose a backend (1, 2): 2

Enter Google Cloud project ID [auto-detected-from-gcloud]:
Enter Google Cloud region [us-central1]:

Шаг 6: Изучите то, что было создано

cd my_first_agent
ls -la

Вы найдете три файла:

.env          # Configuration (auto-populated with your project)
__init__.py   # Package marker
agent.py      # Your agent definition

Шаг 7: Быстрая проверка конфигурации

# Verify the .env was created correctly
cat .env

# Should show something like:
# GOOGLE_CLOUD_PROJECT=your-project-id
# GOOGLE_CLOUD_LOCATION=us-central1
# GOOGLE_GENAI_USE_VERTEXAI=1

Если идентификатор проекта отсутствует или неверен, отредактируйте файл .env :

nano .env  # or use your preferred editor

Шаг 8: Посмотрите на код агента

cat agent.py
from google.adk.agents.llm_agent import Agent

root_agent = Agent(
    model='gemini-2.5-flash',
    name='root_agent',
    description='A helpful assistant for user questions.',
    instruction='Answer user questions to the best of your knowledge',
)

Просто, понятно, минималистично. Это ваш «Hello World» среди агентов.

Протестируйте своего базового агента

Шаг 9: Запустите своего агента

cd ..
adk run my_first_agent

Вы должны увидеть что-то вроде:

Log setup complete: /tmp/agents_log/agent.20250930_162430.log
To access latest log: tail -F /tmp/agents_log/agent.latest.log
[user]:

Шаг 10: Попробуйте выполнить несколько запросов

В терминале, где запущен adk run , вы увидите приглашение. Введите запросы:

[user]: Hey, what can you do?
[root_agent]: I can help answer questions, generate text, explain concepts, assist with problem-solving, and provide information on a wide range of topics. What would you like to know or discuss?

[user]: What's the current weather in Mountain View?
[root_agent]: I'm sorry, but I cannot provide real-time weather information as I don't have access to current data or weather services. To get the current weather in Mountain View, I recommend checking a weather website or app like weather.com or using a search engine.

Обратите внимание на ограничение — доступ к текущим данным невозможен. Давайте продолжим:

[user]: Review this Python code: def add(a, b): return a + b
[root_agent]: This is a simple Python function that adds two numbers. Here's my review:

**Strengths:**
- Clean and concise implementation
- Follows Python conventions for simple operations
- Clear function name that describes its purpose

**Suggestions for improvement:**
- Add a docstring to document the function
- Consider adding type hints for better code clarity
- You might want to handle edge cases

Here's an enhanced version:
def add(a: float, b: float) -> float:
    """Add two numbers and return their sum."""
    return a + b

Агент может обсуждать код, но может ли он:

  • На самом деле разобрать AST, чтобы понять структуру?
  • Провести тесты, чтобы убедиться, что это работает?
  • Проверить соответствие стилю?
  • Помните ваши предыдущие обзоры?

Нет. Здесь нам нужна архитектура .

🏃🚪 Выход с

Ctrl+C

когда закончите исследование.

3. Подготовка вашего производственного рабочего пространства

Решение: готовая к производству архитектура

Этот простой агент продемонстрировал отправную точку, но производственная система требует надёжной структуры. Теперь мы создадим полноценный проект, воплощающий принципы производства.

Создание фонда

Вы уже настроили свой проект Google Cloud для базового агента. Теперь давайте подготовим полноценное рабочее пространство со всеми инструментами, шаблонами и инфраструктурой, необходимыми для реальной системы.

Шаг 1: Получите структурированный проект

Сначала завершите работу adk run с помощью Ctrl+C и очистите:

# Clean up the basic agent
cd ~  # Make sure you're not inside my_first_agent
rm -rf my_first_agent

# Get the production scaffold
git clone https://github.com/ayoisio/adk-code-review-assistant.git
cd adk-code-review-assistant
git checkout codelab

Шаг 2: Создание и активация виртуальной среды

# Create the virtual environment
python -m venv .venv

# Activate it
# On macOS/Linux:
source .venv/bin/activate
# On Windows:
# .venv\Scripts\activate

Проверка : Теперь в начале вашего приглашения должно отображаться (.venv) .

Шаг 3: Установка зависимостей

pip install -r code_review_assistant/requirements.txt

# Install the package in editable mode (enables imports)
pip install -e .

Это устанавливает:

  • google-adk — фреймворк ADK
  • pycodestyle — для проверки PEP 8
  • vertexai — для облачного развертывания
  • Другие производственные зависимости

Флаг -e позволяет импортировать модули code_review_assistant из любой точки мира.

Шаг 4: Настройте свою среду

# Copy the example environment file
cp .env.example .env

# Edit .env and replace the placeholders:
# - GOOGLE_CLOUD_PROJECT=your-project-id → your actual project ID
# - Keep other defaults as-is

Проверка : Проверьте свою конфигурацию:

cat .env

Должны показать:

GOOGLE_CLOUD_PROJECT=your-actual-project-id
GOOGLE_CLOUD_LOCATION=us-central1
GOOGLE_GENAI_USE_VERTEXAI=TRUE

Шаг 5: Обеспечьте аутентификацию

Поскольку вы уже запустили gcloud auth ранее, давайте просто проверим:

# Check current authentication
gcloud auth list

# Should show your account with (ACTIVE)
# If not, run:
gcloud auth application-default login

Шаг 6: включите дополнительные производственные API

Мы уже включили базовые API. Теперь добавим рабочие:

gcloud services enable \
    sqladmin.googleapis.com \
    run.googleapis.com \
    cloudbuild.googleapis.com \
    artifactregistry.googleapis.com \
    storage.googleapis.com \
    cloudtrace.googleapis.com

Это позволяет:

  • SQL Admin : для Cloud SQL при использовании Cloud Run
  • Cloud Run : для бессерверного развертывания
  • Cloud Build : для автоматизированных развертываний
  • Реестр артефактов : для образов контейнеров
  • Облачное хранилище : для артефактов и подготовки
  • След облака : для обеспечения видимости

Шаг 7: Создание репозитория реестра артефактов

Наше развертывание создаст образы контейнеров, которым нужен дом:

gcloud artifacts repositories create code-review-assistant-repo \
    --repository-format=docker \
    --location=us-central1 \
    --description="Docker repository for Code Review Assistant"

Вы должны увидеть:

Created repository [code-review-assistant-repo].

Если он уже существует (возможно, с предыдущей попытки), ничего страшного — вы увидите сообщение об ошибке, которое можно игнорировать.

Шаг 8: Предоставьте разрешения IAM

# Get your project number
PROJECT_NUMBER=$(gcloud projects describe $GOOGLE_CLOUD_PROJECT \
    --format="value(projectNumber)")

# Define the service account
SERVICE_ACCOUNT="${PROJECT_NUMBER}@cloudbuild.gserviceaccount.com"

# Grant necessary roles
gcloud projects add-iam-policy-binding $GOOGLE_CLOUD_PROJECT \
    --member="serviceAccount:${SERVICE_ACCOUNT}" \
    --role="roles/run.admin"

gcloud projects add-iam-policy-binding $GOOGLE_CLOUD_PROJECT \
    --member="serviceAccount:${SERVICE_ACCOUNT}" \
    --role="roles/iam.serviceAccountUser"

gcloud projects add-iam-policy-binding $GOOGLE_CLOUD_PROJECT \
    --member="serviceAccount:${SERVICE_ACCOUNT}" \
    --role="roles/cloudsql.admin"

gcloud projects add-iam-policy-binding $GOOGLE_CLOUD_PROJECT \
    --member="serviceAccount:${SERVICE_ACCOUNT}" \
    --role="roles/storage.admin"

Каждая команда выведет:

Updated IAM policy for project [your-project-id].

Чего вы достигли

Ваше производственное рабочее пространство теперь полностью подготовлено:

✅ Проект Google Cloud настроен и аутентифицирован
✅ Базовый агент протестирован для понимания ограничений
✅ Код проекта со стратегическими заполнителями готов
✅ Зависимости изолированы в виртуальной среде
✅ Все необходимые API включены
✅ Реестр контейнеров готов к развертыванию
✅ Разрешения IAM настроены правильно
✅ Переменные среды установлены правильно

Теперь вы готовы построить настоящую систему ИИ с детерминированными инструментами, управлением состоянием и правильной архитектурой.

4. Создание вашего первого агента

создание-вашего-первого-агента-диаграмма.png

Чем инструменты отличаются от LLM

Когда вы спрашиваете LLM «Сколько функций в этом коде?», он использует сопоставление с образцом и оценку. Когда вы используете инструмент, который вызывает функцию Python ast.parse() , он анализирует фактическое синтаксическое дерево — никаких догадок, каждый раз один и тот же результат.

В этом разделе создается инструмент, который детерминированно анализирует структуру кода, а затем подключает его к агенту, который знает, когда его вызывать.

Шаг 1: Понимание структуры

Давайте рассмотрим структуру, которую вам предстоит заполнить.

👉 Открыто

code_review_assistant/tools.py

Вы увидите функцию analyze_code_structure с комментариями-заполнителями, отмечающими места для добавления кода. Функция уже имеет базовую структуру — вы будете улучшать её шаг за шагом.

Шаг 2: Добавьте хранилище состояний

Хранилище состояний позволяет другим агентам в конвейере получать доступ к результатам вашего инструмента без повторного запуска анализа.

👉 Найти:

        # MODULE_4_STEP_2_ADD_STATE_STORAGE

👉 Замените эту единственную строку на:

        # Store code and analysis for other agents to access
        tool_context.state[StateKeys.CODE_TO_REVIEW] = code
        tool_context.state[StateKeys.CODE_ANALYSIS] = analysis
        tool_context.state[StateKeys.CODE_LINE_COUNT] = len(code.splitlines())

Шаг 3: Добавьте асинхронный парсинг с помощью пулов потоков

Наш инструмент должен парсить AST, не блокируя другие операции. Добавим асинхронное выполнение с помощью пулов потоков.

👉 Найти:

        # MODULE_4_STEP_3_ADD_ASYNC

👉 Замените эту единственную строку на:

        # Parse in thread pool to avoid blocking the event loop
        loop = asyncio.get_event_loop()
        with ThreadPoolExecutor() as executor:
            tree = await loop.run_in_executor(executor, ast.parse, code)

Шаг 4: Извлечение полной информации

Теперь давайте извлечем классы, импорты и подробные метрики — все, что нам нужно для полного обзора кода.

👉 Найти:

        # MODULE_4_STEP_4_EXTRACT_DETAILS

👉 Замените эту единственную строку на:

        # Extract comprehensive structural information
        analysis = await loop.run_in_executor(
            executor, _extract_code_structure, tree, code
        )

👉 Проверьте: функция

analyze_code_structure

в

tools.py

имеет центральное тело, которое выглядит так:

# Parse in thread pool to avoid blocking the event loop
loop = asyncio.get_event_loop()
with ThreadPoolExecutor() as executor:
    tree = await loop.run_in_executor(executor, ast.parse, code)

    # Extract comprehensive structural information
    analysis = await loop.run_in_executor(
        executor, _extract_code_structure, tree, code
    )

# Store code and analysis for other agents to access
tool_context.state[StateKeys.CODE_TO_REVIEW] = code
tool_context.state[StateKeys.CODE_ANALYSIS] = analysis
tool_context.state[StateKeys.CODE_LINE_COUNT] = len(code.splitlines())

👉 Теперь прокрутите вниз

tools.py

и найдите:

# MODULE_4_STEP_4_HELPER_FUNCTION

👉 Замените эту единственную строку полной вспомогательной функцией:

def _extract_code_structure(tree: ast.AST, code: str) -> Dict[str, Any]:
    """
    Helper function to extract structural information from AST.
    Runs in thread pool for CPU-bound work.
    """
    functions = []
    classes = []
    imports = []
    docstrings = []

    for node in ast.walk(tree):
        if isinstance(node, ast.FunctionDef):
            func_info = {
                'name': node.name,
                'args': [arg.arg for arg in node.args.args],
                'lineno': node.lineno,
                'has_docstring': ast.get_docstring(node) is not None,
                'is_async': isinstance(node, ast.AsyncFunctionDef),
                'decorators': [d.id for d in node.decorator_list
                               if isinstance(d, ast.Name)]
            }
            functions.append(func_info)

            if func_info['has_docstring']:
                docstrings.append(f"{node.name}: {ast.get_docstring(node)[:50]}...")

        elif isinstance(node, ast.ClassDef):
            methods = []
            for item in node.body:
                if isinstance(item, ast.FunctionDef):
                    methods.append(item.name)

            class_info = {
                'name': node.name,
                'lineno': node.lineno,
                'methods': methods,
                'has_docstring': ast.get_docstring(node) is not None,
                'base_classes': [base.id for base in node.bases
                                 if isinstance(base, ast.Name)]
            }
            classes.append(class_info)

        elif isinstance(node, ast.Import):
            for alias in node.names:
                imports.append({
                    'module': alias.name,
                    'alias': alias.asname,
                    'type': 'import'
                })
        elif isinstance(node, ast.ImportFrom):
            imports.append({
                'module': node.module or '',
                'names': [alias.name for alias in node.names],
                'type': 'from_import',
                'level': node.level
            })

    return {
        'functions': functions,
        'classes': classes,
        'imports': imports,
        'docstrings': docstrings,
        'metrics': {
            'line_count': len(code.splitlines()),
            'function_count': len(functions),
            'class_count': len(classes),
            'import_count': len(imports),
            'has_main': any(f['name'] == 'main' for f in functions),
            'has_if_main': '__main__' in code,
            'avg_function_length': _calculate_avg_function_length(tree)
        }
    }


def _calculate_avg_function_length(tree: ast.AST) -> float:
    """Calculate average function length in lines."""
    function_lengths = []

    for node in ast.walk(tree):
        if isinstance(node, ast.FunctionDef):
            if hasattr(node, 'end_lineno') and hasattr(node, 'lineno'):
                length = node.end_lineno - node.lineno + 1
                function_lengths.append(length)

    if function_lengths:
        return sum(function_lengths) / len(function_lengths)
    return 0.0

Шаг 5: Подключитесь к агенту

Теперь мы подключаем инструмент к агенту, который знает, когда его использовать и как интерпретировать его результаты.

👉 Открыто

code_review_assistant/sub_agents/review_pipeline/code_analyzer.py

👉 Найти:

# MODULE_4_STEP_5_CREATE_AGENT

👉 Замените эту строку полным производственным агентом:

code_analyzer_agent = Agent(
    name="CodeAnalyzer",
    model=config.worker_model,
    description="Analyzes Python code structure and identifies components",
    instruction="""You are a code analysis specialist responsible for understanding code structure.

Your task:
1. Take the code submitted by the user (it will be provided in the user message)
2. Use the analyze_code_structure tool to parse and analyze it
3. Pass the EXACT code to your tool - do not modify, fix, or "improve" it
4. Identify all functions, classes, imports, and structural patterns
5. Note any syntax errors or structural issues
6. Store the analysis in state for other agents to use

CRITICAL:
- Pass the code EXACTLY as provided to the analyze_code_structure tool
- Do not fix syntax errors, even if obvious
- Do not add missing imports or fix indentation
- The goal is to analyze what IS there, not what SHOULD be there

When calling the tool, pass the code as a string to the 'code' parameter.
If the analysis fails due to syntax errors, clearly report the error location and type.

Provide a clear summary including:
- Number of functions and classes found
- Key structural observations
- Any syntax errors or issues detected
- Overall code organization assessment""",
    tools=[FunctionTool(func=analyze_code_structure)],
    output_key="structure_analysis_summary"
)

Проверьте свой анализатор кода

Теперь проверьте, что ваш анализатор работает правильно.

👉 Запустите тестовый скрипт:

python tests/test_code_analyzer.py

Тестовый скрипт автоматически загружает конфигурацию из вашего файла .env с помощью python-dotenv , поэтому ручная настройка переменных среды не требуется.

Ожидаемый результат:

INFO:code_review_assistant.config:Code Review Assistant Configuration Loaded:
INFO:code_review_assistant.config:  - GCP Project: your-project-id
INFO:code_review_assistant.config:  - Artifact Bucket: gs://your-project-artifacts
INFO:code_review_assistant.config:  - Models: worker=gemini-2.5-flash, critic=gemini-2.5-pro
Testing code analyzer...
INFO:code_review_assistant.tools:Tool: Analysis complete - 2 functions, 1 classes

=== Analyzer Response ===
The analysis of the provided code shows the following:

* **Functions Found:** 2
    * `add(a, b)`: A global function at line 2.
    * `multiply(self, x, y)`: A method within the `Calculator` class.

* **Classes Found:** 1
    * `Calculator`: A class defined at line 5. Contains one method, `multiply`.

* **Imports:** 0

* **Structural Patterns:** The code defines one global function and one class 
  with a single method. Both are simple, each with a single return statement.

* **Syntax Errors/Issues:** No syntax errors detected.

* **Overall Code Organization:** The code is well-organized for its small size, 
  clearly defining a function and a class with a method.

Что только что произошло:

  1. Тестовый скрипт автоматически загрузил вашу конфигурацию .env
  2. Ваш инструмент analyze_code_structure() проанализировал код с помощью AST Python.
  3. Вспомогательный метод _extract_code_structure() извлекает функции, классы и метрики
  4. Результаты были сохранены в состоянии сеанса с использованием констант StateKeys
  5. Агент анализатора кода интерпретировал результаты и предоставил сводку

Поиск неисправностей:

  • «Нет модуля с именем 'code_review_assistant'» : Запустите pip install -e . из корня проекта.
  • «Отсутствует аргумент key inputs» : убедитесь, что в вашем .env есть GOOGLE_CLOUD_PROJECT , GOOGLE_CLOUD_LOCATION и GOOGLE_GENAI_USE_VERTEXAI=true

Что вы построили

Теперь у вас есть готовый к использованию анализатор кода, который:

Анализирует фактическое AST Python — детерминировано, а не по шаблону
Сохраняет результаты в состоянии — другие агенты могут получить доступ к анализу
Работает асинхронно — не блокирует другие инструменты
Извлекает исчерпывающую информацию — функции, классы, импорт, метрики
Корректная обработка ошибок — сообщения об ошибках синтаксиса с номерами строк
Подключается к агенту — LLM знает, когда и как его использовать

Освоенные ключевые концепции

Инструменты против агентов:

  • Инструменты выполняют детерминированную работу (анализ AST)
  • Агенты решают, когда использовать инструменты и интерпретировать результаты

Возвращаемое значение и состояние:

  • Возврат: то, что сразу видит магистр права
  • Состояние: что сохраняется для других агентов

Константы ключей состояния:

  • Предотвращение опечаток в многоагентных системах
  • Действуют как контракты между агентами
  • Критически важно, когда агенты обмениваются данными

Асинхронные пулы потоков:

  • async def позволяет инструментам приостанавливать выполнение
  • Пулы потоков выполняют задачи, связанные с процессором, в фоновом режиме.
  • Вместе они обеспечивают отзывчивость цикла событий.

Вспомогательные функции:

  • Отделите помощников синхронизации от асинхронных инструментов
  • Делает код пригодным для тестирования и повторного использования

Инструкции агенту:

  • Подробные инструкции предотвращают распространенные ошибки LLM
  • Ясно укажите, чего НЕ следует делать (не исправляйте код)
  • Четкие шаги рабочего процесса для обеспечения последовательности

Что дальше?

В Модуле 5 вы добавите:

  • Средство проверки стиля , которое считывает код из состояния
  • Тестовый исполнитель , который фактически выполняет тесты
  • Синтезатор обратной связи , объединяющий все анализы

Вы увидите, как состояние передается по последовательному конвейеру, и почему шаблон констант имеет значение, когда несколько агентов считывают и записывают одни и те же данные.

5. Создание конвейера: совместная работа нескольких агентов

построение-конвейера-множество-агентов-работающих-вместе-диаграмма.png

Введение

В модуле 4 вы создали единый агент, анализирующий структуру кода. Но для комплексного анализа кода требуется больше, чем просто парсинг — вам нужны проверка стиля, выполнение тестов и интеллектуальный синтез обратной связи.

Этот модуль создает конвейер из 4 агентов , которые работают вместе последовательно, каждый из которых вносит свой вклад в специализированный анализ:

  1. Анализатор кода (из модуля 4) — анализирует структуру
  2. Проверка стиля — выявляет нарушения стиля.
  3. Test Runner — выполняет и проверяет тесты.
  4. Синтезатор обратной связи — объединяет все данные в действенную обратную связь.

Ключевая концепция: Состояние как канал связи. Каждый агент считывает данные, записанные предыдущими агентами в состояние, добавляет свой анализ и передаёт обогащённое состояние следующему агенту. Шаблон констант из Модуля 4 становится критически важным, когда несколько агентов совместно используют данные.

Предварительный просмотр того, что вы создадите: отправьте «грязный» код → наблюдайте за потоком состояний через 4 агентов → получите подробный отчет с персонализированной обратной связью на основе прошлых шаблонов.

Шаг 1: Добавьте инструмент проверки стилей + агент

Средство проверки стиля выявляет нарушения PEP 8 с помощью pycodestyle — детерминированного линтера, а не интерпретации на основе LLM.

Добавьте инструмент проверки стиля

👉 Открыто

code_review_assistant/tools.py

👉 Найти:

# MODULE_5_STEP_1_STYLE_CHECKER_TOOL

👉 Замените эту единственную строку на:

async def check_code_style(code: str, tool_context: ToolContext) -> Dict[str, Any]:
    """
    Checks code style compliance using pycodestyle (PEP 8).

    Args:
        code: Python source code to check (or will retrieve from state)
        tool_context: ADK tool context

    Returns:
        Dictionary containing style score and issues
    """
    logger.info("Tool: Checking code style...")

    try:
        # Retrieve code from state if not provided
        if not code:
            code = tool_context.state.get(StateKeys.CODE_TO_REVIEW, '')
            if not code:
                return {
                    "status": "error",
                    "message": "No code provided or found in state"
                }

        # Run style check in thread pool
        loop = asyncio.get_event_loop()
        with ThreadPoolExecutor() as executor:
            result = await loop.run_in_executor(
                executor, _perform_style_check, code
            )

        # Store results in state
        tool_context.state[StateKeys.STYLE_SCORE] = result['score']
        tool_context.state[StateKeys.STYLE_ISSUES] = result['issues']
        tool_context.state[StateKeys.STYLE_ISSUE_COUNT] = result['issue_count']

        logger.info(f"Tool: Style check complete - Score: {result['score']}/100, "
                    f"Issues: {result['issue_count']}")

        return result

    except Exception as e:
        error_msg = f"Style check failed: {str(e)}"
        logger.error(f"Tool: {error_msg}", exc_info=True)

        # Set default values on error
        tool_context.state[StateKeys.STYLE_SCORE] = 0
        tool_context.state[StateKeys.STYLE_ISSUES] = []

        return {
            "status": "error",
            "message": error_msg,
            "score": 0
        }

👉 Теперь прокрутите файл до конца и найдите:

# MODULE_5_STEP_1_STYLE_HELPERS

👉 Замените эту единственную строку вспомогательными функциями:

def _perform_style_check(code: str) -> Dict[str, Any]:
    """Helper to perform style check in thread pool."""
    import io
    import sys

    with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as tmp:
        tmp.write(code)
        tmp_path = tmp.name

    try:
        # Capture stdout to get pycodestyle output
        old_stdout = sys.stdout
        sys.stdout = captured_output = io.StringIO()

        style_guide = pycodestyle.StyleGuide(
            quiet=False,  # We want output
            max_line_length=100,
            ignore=['E501', 'W503']
        )

        result = style_guide.check_files([tmp_path])

        # Restore stdout
        sys.stdout = old_stdout

        # Parse captured output
        output = captured_output.getvalue()
        issues = []

        for line in output.strip().split('\n'):
            if line and ':' in line:
                parts = line.split(':', 4)
                if len(parts) >= 4:
                    try:
                        issues.append({
                            'line': int(parts[1]),
                            'column': int(parts[2]),
                            'code': parts[3].split()[0] if len(parts) > 3 else 'E000',
                            'message': parts[3].strip() if len(parts) > 3 else 'Unknown error'
                        })
                    except (ValueError, IndexError):
                        pass

        # Add naming convention checks
        try:
            tree = ast.parse(code)
            naming_issues = _check_naming_conventions(tree)
            issues.extend(naming_issues)
        except SyntaxError:
            pass  # Syntax errors will be caught elsewhere

        # Calculate weighted score
        score = _calculate_style_score(issues)

        return {
            "status": "success",
            "score": score,
            "issue_count": len(issues),
            "issues": issues[:10],  # First 10 issues
            "summary": f"Style score: {score}/100 with {len(issues)} violations"
        }

    finally:
        if os.path.exists(tmp_path):
            os.unlink(tmp_path)


def _check_naming_conventions(tree: ast.AST) -> List[Dict[str, Any]]:
    """Check PEP 8 naming conventions."""
    naming_issues = []

    for node in ast.walk(tree):
        if isinstance(node, ast.FunctionDef):
            # Skip private/protected methods and __main__
            if not node.name.startswith('_') and node.name != node.name.lower():
                naming_issues.append({
                    'line': node.lineno,
                    'column': node.col_offset,
                    'code': 'N802',
                    'message': f"N802 function name '{node.name}' should be lowercase"
                })
        elif isinstance(node, ast.ClassDef):
            # Check if class name follows CapWords convention
            if not node.name[0].isupper() or '_' in node.name:
                naming_issues.append({
                    'line': node.lineno,
                    'column': node.col_offset,
                    'code': 'N801',
                    'message': f"N801 class name '{node.name}' should use CapWords convention"
                })

    return naming_issues


def _calculate_style_score(issues: List[Dict[str, Any]]) -> int:
    """Calculate weighted style score based on violation severity."""
    if not issues:
        return 100

    # Define weights by error type
    weights = {
        'E1': 10,  # Indentation errors
        'E2': 3,  # Whitespace errors
        'E3': 5,  # Blank line errors
        'E4': 8,  # Import errors
        'E5': 5,  # Line length
        'E7': 7,  # Statement errors
        'E9': 10,  # Syntax errors
        'W2': 2,  # Whitespace warnings
        'W3': 2,  # Blank line warnings
        'W5': 3,  # Line break warnings
        'N8': 7,  # Naming conventions
    }

    total_deduction = 0
    for issue in issues:
        code_prefix = issue['code'][:2] if len(issue['code']) >= 2 else 'E2'
        weight = weights.get(code_prefix, 3)
        total_deduction += weight

    # Cap at 100 points deduction
    return max(0, 100 - min(total_deduction, 100))

Добавьте агент проверки стилей

👉 Открыто

code_review_assistant/sub_agents/review_pipeline/style_checker.py

👉 Найти:

# MODULE_5_STEP_1_INSTRUCTION_PROVIDER

👉 Замените эту единственную строку на:

async def style_checker_instruction_provider(context: ReadonlyContext) -> str:
    """Dynamic instruction provider that injects state variables."""
    template = """You are a code style expert focused on PEP 8 compliance.

Your task:
1. Use the check_code_style tool to validate PEP 8 compliance
2. The tool will retrieve the ORIGINAL code from state automatically
3. Report violations exactly as found
4. Present the results clearly and confidently

CRITICAL:
- The tool checks the code EXACTLY as provided by the user
- Do not suggest the code was modified or fixed
- Report actual violations found in the original code
- If there are style issues, they should be reported honestly

Call the check_code_style tool with an empty string for the code parameter,
as the tool will retrieve the code from state automatically.

When presenting results based on what the tool returns:
- State the exact score from the tool results
- If score >= 90: "Excellent style compliance!"
- If score 70-89: "Good style with minor improvements needed"
- If score 50-69: "Style needs attention"
- If score < 50: "Significant style improvements needed"

List the specific violations found (the tool will provide these):
- Show line numbers, error codes, and messages
- Focus on the top 10 most important issues

Previous analysis: {structure_analysis_summary}

Format your response as:
## Style Analysis Results
- Style Score: [exact score]/100
- Total Issues: [count]
- Assessment: [your assessment based on score]

## Top Style Issues
[List issues with line numbers and descriptions]

## Recommendations
[Specific fixes for the most critical issues]"""

    return await instructions_utils.inject_session_state(template, context)

👉 Найти:

# MODULE_5_STEP_1_STYLE_CHECKER_AGENT

👉 Замените эту единственную строку на:

style_checker_agent = Agent(
    name="StyleChecker",
    model=config.worker_model,
    description="Checks Python code style against PEP 8 guidelines",
    instruction=style_checker_instruction_provider,
    tools=[FunctionTool(func=check_code_style)],
    output_key="style_check_summary"
)

Шаг 2: Добавьте агента Test Runner

Тестовый исполнитель генерирует комплексные тесты и выполняет их с помощью встроенного исполнителя кода.

👉 Открыто

code_review_assistant/sub_agents/review_pipeline/test_runner.py

👉 Найти:

# MODULE_5_STEP_2_INSTRUCTION_PROVIDER

👉 Замените эту единственную строку на:

async def test_runner_instruction_provider(context: ReadonlyContext) -> str:
    """Dynamic instruction provider that injects the code_to_review directly."""
    template = """You are a testing specialist who creates and runs tests for Python code.

THE CODE TO TEST IS:
{code_to_review}

YOUR TASK:
1. Understand what the function appears to do based on its name and structure
2. Generate comprehensive tests (15-20 test cases)
3. Execute the tests using your code executor
4. Analyze results to identify bugs vs expected behavior
5. Output a detailed JSON analysis

TESTING METHODOLOGY:
- Test with the most natural interpretation first
- When something fails, determine if it's a bug or unusual design
- Test edge cases, boundaries, and error scenarios
- Document any surprising behavior

Execute your tests and output ONLY valid JSON with this structure:
- "test_summary": object with "total_tests_run", "tests_passed", "tests_failed", "tests_with_errors", "critical_issues_found"
- "critical_issues": array of objects, each with "type", "description", "example_input", "expected_behavior", "actual_behavior", "severity"
- "test_categories": object with "basic_functionality", "edge_cases", "error_handling" (each containing "passed", "failed", "errors" counts)
- "function_behavior": object with "apparent_purpose", "actual_interface", "unexpected_requirements"
- "verdict": object with "status" (WORKING/BUGGY/BROKEN), "confidence" (high/medium/low), "recommendation"

Do NOT output the test code itself, only the JSON analysis."""

    return await instructions_utils.inject_session_state(template, context)

👉 Найти:

# MODULE_5_STEP_2_TEST_RUNNER_AGENT

👉 Замените эту единственную строку на:

test_runner_agent = Agent(
    name="TestRunner",
    model=config.critic_model,
    description="Generates and runs tests for Python code using safe code execution",
    instruction=test_runner_instruction_provider,
    code_executor=BuiltInCodeExecutor(),
    output_key="test_execution_summary"
)

Шаг 3: Понимание памяти для межсессионного обучения

Прежде чем строить синтезатор с обратной связью, необходимо понять разницу между состоянием и памятью — двумя разными механизмами хранения для двух разных целей.

Состояние против памяти: ключевое различие

Давайте поясним это на конкретном примере из обзора кода:

Состояние (только текущий сеанс):

# Data from THIS review session
tool_context.state[StateKeys.STYLE_ISSUES] = [
    {"line": 5, "code": "E231", "message": "missing whitespace"},
    {"line": 12, "code": "E701", "message": "multiple statements"}
]
  • Область действия: только этот разговор
  • Цель: передача данных между агентами в текущем конвейере.
  • Проживает в: объект Session
  • Время жизни: удаляется после завершения сеанса

Память (все прошлые сеансы):

# Learned from 50 previous reviews
"User frequently forgets docstrings on helper functions"
"User tends to write long functions (avg 45 lines)"
"User improved error handling after feedback in session #23"
  • Область действия: все прошлые сеансы этого пользователя
  • Цель: изучение закономерностей, предоставление персонализированной обратной связи.
  • Проживает в: MemoryService
  • Время жизни: сохраняется между сеансами, доступен для поиска

Почему для обратной связи необходимо и то, и другое:

Представьте себе синтезатор, создающий обратную связь:

Используя только состояние (текущий обзор):

"Function `calculate_total` has no docstring."

Обычная механическая обратная связь.

Использование состояния + памяти (текущие + прошлые паттерны):

"Function `calculate_total` has no docstring. This is the 4th review
where helper functions lacked documentation. Consider adding docstrings
as you write functions, not afterwards - you mentioned in our last
session that you find it easier that way."

Персонализированные, контекстные, улучшающиеся с течением времени ссылки.

Для производственных развертываний у вас есть следующие варианты :

Вариант 1: VertexAiMemoryBankService (расширенный)

  • Что он делает: извлечение значимых фактов из разговоров с помощью LLM
  • Поиск: семантический поиск (понимает значение, а не только ключевые слова)
  • Управление памятью: автоматически объединяет и обновляет воспоминания с течением времени.
  • Требуется: настройка Google Cloud Project + Agent Engine
  • Используйте, когда: вам нужны сложные, развивающиеся, персонализированные воспоминания.
  • Пример: «Пользователь предпочитает функциональное программирование» (из 10 разговоров о стиле кода)

Вариант 2: продолжить с InMemoryMemoryService + Persistent Sessions

  • Что делает: сохраняет полную историю разговоров для поиска по ключевым словам.
  • Поиск: базовое соответствие ключевым словам за предыдущие сеансы
  • Управление памятью: вы контролируете, что именно сохраняется (через add_session_to_memory ).
  • Требуется: только постоянный SessionService (например, VertexAiSessionService или DatabaseSessionService )
  • Используйте, когда: вам нужен простой поиск по прошлым разговорам без обработки LLM.
  • Пример: Поиск «docstring» возвращает все сеансы, в которых упоминается это слово.

Как заполняется память

После завершения каждой проверки кода:

# At the end of a session (typically in your application code)
await memory_service.add_session_to_memory(session)

Что происходит:

  • InMemoryMemoryService: сохраняет полные события сеанса для поиска по ключевым словам.
  • VertexAiMemoryBankService: LLM извлекает ключевые факты и объединяет их с существующими воспоминаниями

Последующие сеансы могут затем запрашивать:

# In a tool, search for relevant past feedback
results = tool_context.search_memory("feedback about docstrings")

Шаг 4: Добавьте инструменты и агент синтезатора обратной связи

Синтезатор обратной связи — самый сложный агент в конвейере. Он управляет тремя инструментами, использует динамические инструкции и объединяет состояние, память и артефакты.

Добавьте три инструмента синтезатора

👉 Открыто

code_review_assistant/tools.py

👉 Найти:

# MODULE_5_STEP_4_SEARCH_PAST_FEEDBACK

👉 Заменить на Инструмент 1 — Поиск в памяти (производственная версия):

async def search_past_feedback(developer_id: str, tool_context: ToolContext) -> Dict[str, Any]:
    """
    Search for past feedback in memory service.

    Args:
        developer_id: ID of the developer (defaults to "default_user")
        tool_context: ADK tool context with potential memory service access

    Returns:
        Dictionary containing feedback search results
    """
    logger.info(f"Tool: Searching for past feedback for developer {developer_id}...")

    try:
        # Default developer ID if not provided
        if not developer_id:
            developer_id = tool_context.state.get(StateKeys.USER_ID, 'default_user')

        # Check if memory service is available
        if hasattr(tool_context, 'search_memory'):
            try:
                # Perform structured searches
                queries = [
                    f"developer:{developer_id} code review feedback",
                    f"developer:{developer_id} common issues",
                    f"developer:{developer_id} improvements"
                ]

                all_feedback = []
                patterns = {
                    'common_issues': [],
                    'improvements': [],
                    'strengths': []
                }

                for query in queries:
                    search_result = await tool_context.search_memory(query)

                    if search_result and hasattr(search_result, 'memories'):
                        for memory in search_result.memories[:5]:
                            memory_text = memory.text if hasattr(memory, 'text') else str(memory)
                            all_feedback.append(memory_text)

                            # Extract patterns
                            if 'style' in memory_text.lower():
                                patterns['common_issues'].append('style compliance')
                            if 'improved' in memory_text.lower():
                                patterns['improvements'].append('showing improvement')
                            if 'excellent' in memory_text.lower():
                                patterns['strengths'].append('consistent quality')

                # Store in state
                tool_context.state[StateKeys.PAST_FEEDBACK] = all_feedback
                tool_context.state[StateKeys.FEEDBACK_PATTERNS] = patterns

                logger.info(f"Tool: Found {len(all_feedback)} past feedback items")

                return {
                    "status": "success",
                    "feedback_found": True,
                    "count": len(all_feedback),
                    "summary": " | ".join(all_feedback[:3]) if all_feedback else "No feedback",
                    "patterns": patterns
                }

            except Exception as e:
                logger.warning(f"Tool: Memory search error: {e}")

        # Fallback: Check state for cached feedback
        cached_feedback = tool_context.state.get(StateKeys.USER_PAST_FEEDBACK_CACHE, [])
        if cached_feedback:
            tool_context.state[StateKeys.PAST_FEEDBACK] = cached_feedback
            return {
                "status": "success",
                "feedback_found": True,
                "count": len(cached_feedback),
                "summary": "Using cached feedback",
                "patterns": {}
            }

        # No feedback found
        tool_context.state[StateKeys.PAST_FEEDBACK] = []
        logger.info("Tool: No past feedback found")

        return {
            "status": "success",
            "feedback_found": False,
            "message": "No past feedback available - this appears to be a first submission",
            "patterns": {}
        }

    except Exception as e:
        error_msg = f"Feedback search error: {str(e)}"
        logger.error(f"Tool: {error_msg}", exc_info=True)

        tool_context.state[StateKeys.PAST_FEEDBACK] = []

        return {
            "status": "error",
            "message": error_msg,
            "feedback_found": False
        }

👉 Найти:

# MODULE_5_STEP_4_UPDATE_GRADING_PROGRESS

👉 Заменить на Инструмент 2 - Отслеживание оценок (производственная версия):

async def update_grading_progress(tool_context: ToolContext) -> Dict[str, Any]:
    """
    Updates grading progress counters and metrics in state.
    """
    logger.info("Tool: Updating grading progress...")

    try:
        current_time = datetime.now().isoformat()

        # Build all state changes
        state_updates = {}

        # Temporary (invocation-level) state
        state_updates[StateKeys.TEMP_PROCESSING_TIMESTAMP] = current_time

        # Session-level state
        attempts = tool_context.state.get(StateKeys.GRADING_ATTEMPTS, 0) + 1
        state_updates[StateKeys.GRADING_ATTEMPTS] = attempts
        state_updates[StateKeys.LAST_GRADING_TIME] = current_time

        # User-level persistent state
        lifetime_submissions = tool_context.state.get(StateKeys.USER_TOTAL_SUBMISSIONS, 0) + 1
        state_updates[StateKeys.USER_TOTAL_SUBMISSIONS] = lifetime_submissions
        state_updates[StateKeys.USER_LAST_SUBMISSION_TIME] = current_time

        # Calculate improvement metrics
        current_style_score = tool_context.state.get(StateKeys.STYLE_SCORE, 0)
        last_style_score = tool_context.state.get(StateKeys.USER_LAST_STYLE_SCORE, 0)
        score_improvement = current_style_score - last_style_score

        state_updates[StateKeys.USER_LAST_STYLE_SCORE] = current_style_score
        state_updates[StateKeys.SCORE_IMPROVEMENT] = score_improvement

        # Track test results if available
        test_results = tool_context.state.get(StateKeys.TEST_EXECUTION_SUMMARY, {})

        # Parse if it's a string
        if isinstance(test_results, str):
            try:
                test_results = json.loads(test_results)
            except:
                test_results = {}

        if test_results and test_results.get('test_summary', {}).get('total_tests_run', 0) > 0:
            summary = test_results['test_summary']
            total = summary.get('total_tests_run', 0)
            passed = summary.get('tests_passed', 0)
            if total > 0:
                pass_rate = (passed / total) * 100
                state_updates[StateKeys.USER_LAST_TEST_PASS_RATE] = pass_rate

        # Apply all updates atomically
        for key, value in state_updates.items():
            tool_context.state[key] = value

        logger.info(f"Tool: Progress updated - Attempt #{attempts}, "
                    f"Lifetime: {lifetime_submissions}")

        return {
            "status": "success",
            "session_attempts": attempts,
            "lifetime_submissions": lifetime_submissions,
            "timestamp": current_time,
            "improvement": {
                "style_score_change": score_improvement,
                "direction": "improved" if score_improvement > 0 else "declined"
            },
            "summary": f"Attempt #{attempts} recorded, {lifetime_submissions} total submissions"
        }

    except Exception as e:
        error_msg = f"Progress update error: {str(e)}"
        logger.error(f"Tool: {error_msg}", exc_info=True)

        return {
            "status": "error",
            "message": error_msg
        }

👉 Найти:

# MODULE_5_STEP_4_SAVE_GRADING_REPORT

👉 Заменить с помощью инструмента 3 - Artifact Saver (производственная версия):

async def save_grading_report(feedback_text: str, tool_context: ToolContext) -> Dict[str, Any]:
    """
    Saves a detailed grading report as an artifact.

    Args:
        feedback_text: The feedback text to include in the report
        tool_context: ADK tool context for state management

    Returns:
        Dictionary containing save status and details
    """
    logger.info("Tool: Saving grading report...")

    try:
        # Gather all relevant data from state
        code = tool_context.state.get(StateKeys.CODE_TO_REVIEW, '')
        analysis = tool_context.state.get(StateKeys.CODE_ANALYSIS, {})
        style_score = tool_context.state.get(StateKeys.STYLE_SCORE, 0)
        style_issues = tool_context.state.get(StateKeys.STYLE_ISSUES, [])

        # Get test results
        test_results = tool_context.state.get(StateKeys.TEST_EXECUTION_SUMMARY, {})

        # Parse if it's a string
        if isinstance(test_results, str):
            try:
                test_results = json.loads(test_results)
            except:
                test_results = {}

        timestamp = datetime.now().isoformat()

        # Create comprehensive report dictionary
        report = {
            'timestamp': timestamp,
            'grading_attempt': tool_context.state.get(StateKeys.GRADING_ATTEMPTS, 1),
            'code': {
                'content': code,
                'line_count': len(code.splitlines()),
                'hash': hashlib.md5(code.encode()).hexdigest()
            },
            'analysis': analysis,
            'style': {
                'score': style_score,
                'issues': style_issues[:5]  # First 5 issues
            },
            'tests': test_results,
            'feedback': feedback_text,
            'improvements': {
                'score_change': tool_context.state.get(StateKeys.SCORE_IMPROVEMENT, 0),
                'from_last_score': tool_context.state.get(StateKeys.USER_LAST_STYLE_SCORE, 0)
            }
        }

        # Convert report to JSON string
        report_json = json.dumps(report, indent=2)
        report_part = types.Part.from_text(text=report_json)

        # Try to save as artifact if the service is available
        if hasattr(tool_context, 'save_artifact'):
            try:
                # Generate filename with timestamp (replace colons for filesystem compatibility)
                filename = f"grading_report_{timestamp.replace(':', '-')}.json"

                # Save the main report
                version = await tool_context.save_artifact(filename, report_part)

                # Also save a "latest" version for easy access
                await tool_context.save_artifact("latest_grading_report.json", report_part)

                logger.info(f"Tool: Report saved as {filename} (version {version})")

                # Store report in state as well for redundancy
                tool_context.state[StateKeys.USER_LAST_GRADING_REPORT] = report

                return {
                    "status": "success",
                    "artifact_saved": True,
                    "filename": filename,
                    "version": str(version),
                    "size": len(report_json),
                    "summary": f"Report saved as {filename}"
                }

            except Exception as artifact_error:
                logger.warning(f"Artifact service error: {artifact_error}, falling back to state storage")
                # Continue to fallback below

        # Fallback: Store in state if artifact service is not available or failed
        tool_context.state[StateKeys.USER_LAST_GRADING_REPORT] = report
        logger.info("Tool: Report saved to state (artifact service not available)")

        return {
            "status": "success",
            "artifact_saved": False,
            "message": "Report saved to state only",
            "size": len(report_json),
            "summary": "Report saved to session state"
        }

    except Exception as e:
        error_msg = f"Report save error: {str(e)}"
        logger.error(f"Tool: {error_msg}", exc_info=True)

        # Still try to save minimal data to state
        try:
            tool_context.state[StateKeys.USER_LAST_GRADING_REPORT] = {
                'error': error_msg,
                'feedback': feedback_text,
                'timestamp': datetime.now().isoformat()
            }
        except:
            pass

        return {
            "status": "error",
            "message": error_msg,
            "artifact_saved": False,
            "summary": f"Failed to save report: {error_msg}"
        }

Создайте агент-синтезатор

👉 Открыто

code_review_assistant/sub_agents/review_pipeline/feedback_synthesizer.py

👉 Найти:

# MODULE_5_STEP_4_INSTRUCTION_PROVIDER

👉 Заменить поставщиком производственных инструкций:

async def feedback_instruction_provider(context: ReadonlyContext) -> str:
    """Dynamic instruction provider that injects state variables."""
    template = """You are an expert code reviewer and mentor providing constructive, educational feedback.

CONTEXT FROM PREVIOUS AGENTS:
- Structure analysis summary: {structure_analysis_summary}
- Style check summary: {style_check_summary}  
- Test execution summary: {test_execution_summary}

YOUR TASK requires these steps IN ORDER:
1. Call search_past_feedback tool with developer_id="default_user"
2. Call update_grading_progress tool with no parameters
3. Carefully analyze the test results to understand what really happened
4. Generate comprehensive feedback following the structure below
5. Call save_grading_report tool with the feedback_text parameter
6. Return the feedback as your final output

CRITICAL - Understanding Test Results:
The test_execution_summary contains structured JSON. Parse it carefully:
- tests_passed = Code worked correctly
- tests_failed = Code produced wrong output
- tests_with_errors = Code crashed
- critical_issues = Fundamental problems with the code

If critical_issues array contains items, these are serious bugs that need fixing.
Do NOT count discovering bugs as test successes.

FEEDBACK STRUCTURE TO FOLLOW:

## 📊 Summary
Provide an honest assessment. Be encouraging but truthful about problems found.

## ✅ Strengths  
List 2-3 things done well, referencing specific code elements.

## 📈 Code Quality Analysis

### Structure & Organization
Comment on code organization, readability, and documentation.

### Style Compliance
Report the actual style score and any specific issues.

### Test Results
Report the actual test results accurately:
- If critical_issues exist, report them as bugs to fix
- Be clear: "X tests passed, Y critical issues were found"
- List each critical issue
- Don't hide or minimize problems

## 💡 Recommendations for Improvement
Based on the analysis, provide specific actionable fixes.
If critical issues exist, fixing them is top priority.

## 🎯 Next Steps
Prioritized action list based on severity of issues.

## 💬 Encouragement
End with encouragement while being honest about what needs fixing.

Remember: Complete ALL steps including calling save_grading_report."""

    return await instructions_utils.inject_session_state(template, context)

👉 Найти:

# MODULE_5_STEP_4_SYNTHESIZER_AGENT

👉 Заменить на:

feedback_synthesizer_agent = Agent(
    name="FeedbackSynthesizer",
    model=config.critic_model,
    description="Synthesizes all analysis into constructive, personalized feedback",
    instruction=feedback_instruction_provider,
    tools=[
        FunctionTool(func=search_past_feedback),
        FunctionTool(func=update_grading_progress),
        FunctionTool(func=save_grading_report)
    ],
    output_key="final_feedback"
)

Шаг 5: Прокладка трубопровода

Теперь соедините все четыре агента в последовательный конвейер и создайте корневой агент.

👉 Открыто

code_review_assistant/agent.py

👉 Добавьте необходимые импорты в начало файла (после существующих импортов):

from google.adk.agents import Agent, SequentialAgent
from code_review_assistant.sub_agents.review_pipeline.code_analyzer import code_analyzer_agent
from code_review_assistant.sub_agents.review_pipeline.style_checker import style_checker_agent
from code_review_assistant.sub_agents.review_pipeline.test_runner import test_runner_agent
from code_review_assistant.sub_agents.review_pipeline.feedback_synthesizer import feedback_synthesizer_agent

Теперь ваш файл должен выглядеть так:

"""
Main agent orchestration for the Code Review Assistant.
"""

from google.adk.agents import Agent, SequentialAgent
from .config import config
from code_review_assistant.sub_agents.review_pipeline.code_analyzer import code_analyzer_agent
from code_review_assistant.sub_agents.review_pipeline.style_checker import style_checker_agent
from code_review_assistant.sub_agents.review_pipeline.test_runner import test_runner_agent
from code_review_assistant.sub_agents.review_pipeline.feedback_synthesizer import feedback_synthesizer_agent

# MODULE_5_STEP_5_CREATE_PIPELINE

# MODULE_6_STEP_5_CREATE_FIX_LOOP

# MODULE_6_STEP_5_UPDATE_ROOT_AGENT

👉 Найти:

# MODULE_5_STEP_5_CREATE_PIPELINE

👉 Замените эту единственную строку на:

# Create sequential pipeline
code_review_pipeline = SequentialAgent(
    name="CodeReviewPipeline",
    description="Complete code review pipeline with analysis, testing, and feedback",
    sub_agents=[
        code_analyzer_agent,
        style_checker_agent,
        test_runner_agent,
        feedback_synthesizer_agent
    ]
)

# Root agent - coordinates the review pipeline
root_agent = Agent(
    name="CodeReviewAssistant",
    model=config.worker_model,
    description="An intelligent code review assistant that analyzes Python code and provides educational feedback",
    instruction="""You are a specialized Python code review assistant focused on helping developers improve their code quality.

When a user provides Python code for review:
1. Immediately delegate to CodeReviewPipeline and pass the code EXACTLY as it was provided by the user.
2. The pipeline will handle all analysis and feedback
3. Return ONLY the final feedback from the pipeline - do not add any commentary

When a user asks what you can do or asks general questions:
- Explain your capabilities for code review
- Do NOT trigger the pipeline for non-code messages

The pipeline handles everything for code review - just pass through its final output.""",
    sub_agents=[code_review_pipeline],
    output_key="assistant_response"
)

Шаг 6: Тестирование всего конвейера

Настало время увидеть, как все четыре агента работают вместе.

👉 Запуск системы:

adk web code_review_assistant

После запуска команды adk web вы должны увидеть в терминале вывод, указывающий на то, что веб-сервер ADK запущен, примерно такой:

+-----------------------------------------------------------------------------+
| ADK Web Server started                                                      |
|                                                                             |
| For local testing, access at http://localhost:8000.                         |
+-----------------------------------------------------------------------------+

INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)

👉 Далее, чтобы получить доступ к интерфейсу разработчика ADK из браузера:

На значке веб-предпросмотра (часто он выглядит как глаз или квадрат со стрелкой) на панели инструментов Cloud Shell (обычно справа вверху) выберите «Изменить порт». Во всплывающем окне установите порт 8000 и нажмите «Изменить и просмотреть». Cloud Shell откроет новую вкладку или окно браузера с интерфейсом ADK Dev.

веб-превью

👉 Агент запущен. Интерфейс ADK Dev в вашем браузере — это прямое подключение к агенту.

  • Выберите цель: в раскрывающемся меню в верхней части пользовательского интерфейса выберите агента code_review_assistant .

выбор агента

👉 Тестовая подсказка:

Please analyze the following:
def dfs_search_v1(graph, start, target):
    """Find if target is reachable from start."""
    visited = set()
    stack = start
   
    while stack:
        current = stack.pop()
       
        if current == target:
            return True
           
        if current not in visited:
            visited.add(current)
           
            for neighbor in graph[current]:
                if neighbor not in visited:
                    stack.append(neighbor)
   
    return False

👉 Посмотрите на процесс проверки кода в действии:

Отправляя ошибочную функцию dfs_search_v1 , вы получаете не просто один ответ. Вы наблюдаете за работой многоагентного конвейера. Потоковый вывод, который вы видите, — это результат последовательного выполнения четырёх специализированных агентов, каждый из которых дополняет предыдущий.

Ниже приведен анализ вклада каждого агента в окончательный всесторонний обзор, превращающий необработанные данные в применимую на практике информацию.

конвейер-обзора-кода-в-действии

1. Структурный отчет анализатора кода

Сначала агент CodeAnalyzer получает исходный код. Он не пытается угадать , что делает код, а использует инструмент analyze_code_structure для детерминированного разбора абстрактного синтаксического дерева (AST).

Его вывод — чистые фактические данные о структуре кода:

The analysis of the provided code reveals the following:

Summary:
- Functions Found: 1
- Classes Found: 0

Key Structural Observations:
- A single function, dfs_search_v1, is defined.
- It includes a docstring: "Find if target is reachable from start."
- No syntax errors were detected.

Overall Code Organization Assessment:
- The code snippet is a well-defined, self-contained function.

Значение: Этот начальный шаг обеспечивает чистую и надежную основу для других агентов. Он подтверждает корректность кода Python и определяет точные компоненты, требующие проверки.

2. Аудит PEP 8 от Style Checker

Затем в дело вступает агент StyleChecker . Он считывает код из общего состояния и использует инструмент check_code_style , который использует линтер pycodestyle .

Его выход — количественная оценка качества и конкретные нарушения:

Style Analysis Results
- Style Score: 88/100
- Total Issues: 6
- Assessment: Good style with minor improvements needed

Top Style Issues
- Line 5, W293: blank line contains whitespace
- Line 19, W292: no newline at end of file

Ценность: Этот агент предоставляет объективную, не подлежащую обсуждению обратную связь, основанную на установленных стандартах сообщества (PEP 8). Система взвешенных оценок мгновенно сообщает пользователю о серьёзности проблем.

3. Обнаружение критической ошибки тестировщиком

Здесь система выходит за рамки поверхностного анализа. Агент TestRunner генерирует и выполняет комплексный набор тестов для проверки поведения кода.

Его вывод представляет собой структурированный JSON-объект, содержащий обвинительный вердикт:

{
  "critical_issues": [
    {
      "type": "Critical Bug",
      "description": "The function's initialization `stack = start` is incorrect... When a common input like a string... is provided... the function crashes with an AttributeError.",
      "severity": "Critical"
    }
  ],
  "verdict": {
    "status": "BROKEN",
    "confidence": "high",
    "recommendation": "The function is fundamentally broken... the stack initialization line `stack = start` must be changed to `stack = [start]`."
  }
}

Значение: Это самое важное открытие. Агент не просто предположил; он доказал , что код был неисправен, запустив его. Он обнаружил незначительную, но критическую ошибку выполнения, которую человек-рецензент мог легко пропустить, и точно определил причину и необходимое исправление.

4. Заключительный отчет синтезатора обратной связи

Наконец, агент FeedbackSynthesizer выступает в роли проводника. Он берёт структурированные данные от трёх предыдущих агентов и формирует единый, удобный для пользователя отчёт, который одновременно аналитичен и вдохновляет.

Результатом его работы является финальный, отполированный обзор, который вы видите:

📊 Summary
Great effort on implementing the Depth-First Search algorithm! ... However, a critical bug in the initialization of the stack prevents the function from working correctly...

 Strengths
- Good Algorithm Structure
- Correct Use of `visited` Set

📈 Code Quality Analysis
...
### Style Compliance
The style analysis returned a good score of 88/100.
...
### Test Results
The automated testing revealed a critical issue... The line `stack = start` directly assigns the input... which results in an `AttributeError`.

💡 Recommendations for Improvement
**Fix the Critical Stack Initialization Bug:**
- Incorrect Code: `stack = start`
- Correct Code: `stack = [start]`

💬 Encouragement
You are very close to a perfect implementation! The core logic of your DFS algorithm is sound, which is the hardest part.

Ценность: Этот агент преобразует технические данные в полезный обучающий опыт. Он выделяет наиболее важную проблему (ошибку), чётко её объясняет, предлагает точное решение и делает это в воодушевляющем тоне. Он успешно объединяет результаты всех предыдущих этапов в единое и ценное целое.

Этот многоэтапный процесс демонстрирует мощь агентного конвейера. Вместо единого, монолитного ответа вы получаете многоуровневый анализ, в котором каждый агент выполняет специализированную, поддающуюся проверке задачу. Это приводит к тому, что обзор становится не только содержательным, но и детерминированным, достоверным и крайне познавательным.

👉💻 После завершения тестирования вернитесь в терминал Cloud Shell Editor и нажмите Ctrl+C , чтобы остановить ADK Dev UI.

Что вы построили

Теперь у вас есть полноценный конвейер проверки кода, который:

Анализирует структуру кода — детерминированный анализ AST с помощью вспомогательных функций
Стиль проверки — взвешенная оценка с правилами наименования
Запускает тесты — комплексная генерация тестов со структурированным выводом JSON
Synthesizes feedback - integrates state + memory + artifacts
Tracks progress - multi-tier state across invocations/sessions/users
Learns over time - memory service for cross-session patterns
Provides artifacts - downloadable JSON reports with complete audit trail

Освоенные ключевые концепции

Sequential Pipelines:

  • Four agents executing in strict order
  • Each enriches state for the next
  • Dependencies determine execution sequence

Production Patterns:

  • Helper function separation (sync in thread pools)
  • Graceful degradation (fallback strategies)
  • Multi-tier state management (temp/session/user)
  • Dynamic instruction providers (context-aware)
  • Dual storage (artifacts + state redundancy)

State as Communication:

  • Constants prevent typos across agents
  • output_key writes agent summaries to state
  • Later agents read via StateKeys
  • State flows linearly through pipeline

Memory vs State:

  • State: current session data
  • Memory: patterns across sessions
  • Different purposes, different lifetimes

Tool Orchestration:

  • Single-tool agents (analyzer, style_checker)
  • Built-in executors (test_runner)
  • Multi-tool coordination (synthesizer)

Model Selection Strategy:

  • Worker model: mechanical tasks (parsing, linting, routing)
  • Critic model: reasoning tasks (testing, synthesis)
  • Cost optimization through appropriate selection

Что дальше?

In Module 6, you'll build the fix pipeline :

  • LoopAgent architecture for iterative fixing
  • Exit conditions via escalation
  • State accumulation across iterations
  • Validation and retry logic
  • Integration with review pipeline to offer fixes

You'll see how the same state patterns scale to complex iterative workflows where agents attempt multiple times until success, and how to coordinate multiple pipelines in a single application.

6. Adding the Fix Pipeline: Loop Architecture

adding-the-fix-pipeline-loop-architecture-diagram.png

Введение

In Module 5, you built a sequential review pipeline that analyzes code and provides feedback. But identifying problems is only half the solution - developers need help fixing them.

This module builds an automated fix pipeline that:

  1. Generates fixes based on review results
  2. Validates fixes by running comprehensive tests
  3. Retries automatically if fixes don't work (up to 3 attempts)
  4. Reports results with before/after comparisons

Key concept: LoopAgent for automatic retry. Unlike sequential agents that run once, a LoopAgent repeats its sub-agents until an exit condition is met or max iterations reached. Tools signal success by setting tool_context.actions.escalate = True .

Preview of what you'll build: Submit buggy code → review identifies issues → fix loop generates corrections → tests validate → retries if needed → final comprehensive report.

Основные концепции: LoopAgent и Sequential

Sequential Pipeline (Module 5):

SequentialAgent(agents=[A, B, C])
# Executes: A → B → C → Done
  • One-way flow
  • Each agent runs exactly once
  • No retry logic

Loop Pipeline (Module 6):

LoopAgent(agents=[A, B, C], max_iterations=3)
# Executes: A → B → C → (check exit) → A → B → C → (check exit) → ...
  • Cyclic flow
  • Agents can run multiple times
  • Exits when:
    • A tool sets tool_context.actions.escalate = True (success)
    • max_iterations reached (safety limit)
    • Unhandled exception occurs (error)

Why loops for code fixing:

Code fixes often need multiple attempts:

  • First attempt : Fix obvious bugs (wrong variable types)
  • Second attempt : Fix secondary issues revealed by tests (edge cases)
  • Third attempt : Fine-tune and validate all tests pass

Without a loop, you'd need complex conditional logic in agent instructions. With LoopAgent , retry is automatic.

Architecture comparison:

Sequential (Module 5):
User → Review Pipeline → Feedback → Done

Loop (Module 6):
User → Review Pipeline → Feedback → Fix Pipeline
                                         ↓
                          ┌──────────────┴──────────────┐
                          │   Fix Attempt Loop (1-3x)   │
                          │  ┌─────────────────────┐    │
                          │  │ 1. Generate Fixes   │    │
                          │  │ 2. Test Fixes       │    │
                          │  │ 3. Validate & Exit? │────┼─→ If escalate=True
                          │  └─────────────────────┘    │      exit loop
                          │         ↓ If not            │
                          │    Try Again (max 3)        │
                          └─────────────────────────────┘
                                     ↓
                          4. Synthesize Final Report → Done

Шаг 1: Добавьте агент Code Fixer

The code fixer generates corrected Python code based on review results.

👉 Open

code_review_assistant/sub_agents/fix_pipeline/code_fixer.py

👉 Find:

# MODULE_6_STEP_1_CODE_FIXER_INSTRUCTION_PROVIDER

👉 Replace that single line with:

async def code_fixer_instruction_provider(context: ReadonlyContext) -> str:
    """Dynamic instruction provider that injects state variables."""
    template = """You are an expert code fixing specialist.

Original Code:
{code_to_review}

Analysis Results:
- Style Score: {style_score}/100
- Style Issues: {style_issues}
- Test Results: {test_execution_summary}

Based on the test results, identify and fix ALL issues including:
- Interface bugs (e.g., if start parameter expects wrong type)
- Logic errors (e.g., KeyError when accessing graph nodes)
- Style violations
- Missing documentation

YOUR TASK:
Generate the complete fixed Python code that addresses all identified issues.

CRITICAL INSTRUCTIONS:
- Output ONLY the corrected Python code
- Do NOT include markdown code blocks (```python)
- Do NOT include any explanations or commentary
- The output should be valid, executable Python code and nothing else

Common fixes to apply based on test results:
- If tests show AttributeError with 'pop', fix: stack = [start] instead of stack = start
- If tests show KeyError accessing graph, fix: use graph.get(current, [])
- Add docstrings if missing
- Fix any style violations identified

Output the complete fixed code now:"""

    return await instructions_utils.inject_session_state(template, context)

👉 Find:

# MODULE_6_STEP_1_CODE_FIXER_AGENT

👉 Replace that single line with:

code_fixer_agent = Agent(
    name="CodeFixer",
    model=config.worker_model,
    description="Generates comprehensive fixes for all identified code issues",
    instruction=code_fixer_instruction_provider,
    code_executor=BuiltInCodeExecutor(),
    output_key="code_fixes"
)

Шаг 2: Добавьте агент Fix Test Runner

The fix test runner validates corrections by executing comprehensive tests on the fixed code.

👉 Open

code_review_assistant/sub_agents/fix_pipeline/fix_test_runner.py

👉 Find:

# MODULE_6_STEP_2_FIX_TEST_RUNNER_INSTRUCTION_PROVIDER

👉 Replace that single line with:

async def fix_test_runner_instruction_provider(context: ReadonlyContext) -> str:
    """Dynamic instruction provider that uses the clean code from the previous step."""
    template = """You are responsible for validating the fixed code by running tests.

THE FIXED CODE TO TEST:
{code_fixes}

ORIGINAL TEST RESULTS: {test_execution_summary}

YOUR TASK:
1. Understand the fixes that were applied
2. Generate the same comprehensive tests (15-20 test cases)
3. Execute the tests on the FIXED code using your code executor
4. Compare results with original test results
5. Output a detailed JSON analysis

TESTING METHODOLOGY:
- Run the same tests that revealed issues in the original code
- Verify that previously failing tests now pass
- Ensure no regressions were introduced
- Document the improvement

Execute your tests and output ONLY valid JSON with this structure:
- "passed": number of tests that passed
- "failed": number of tests that failed  
- "total": total number of tests
- "pass_rate": percentage as a number
- "comparison": object with "original_pass_rate", "new_pass_rate", "improvement"
- "newly_passing_tests": array of test names that now pass
- "still_failing_tests": array of test names still failing

Do NOT output the test code itself, only the JSON analysis."""

    return await instructions_utils.inject_session_state(template, context)

👉 Find:

# MODULE_6_STEP_2_FIX_TEST_RUNNER_AGENT

👉 Replace that single line with:

fix_test_runner_agent = Agent(
    name="FixTestRunner",
    model=config.critic_model,
    description="Runs comprehensive tests on fixed code to verify all issues are resolved",
    instruction=fix_test_runner_instruction_provider,
    code_executor=BuiltInCodeExecutor(),
    output_key="fix_test_execution_summary"
)

Шаг 3: Добавьте агент проверки исправлений

The validator checks if fixes were successful and decides whether to exit the loop.

Understanding the Tools

First, add the three tools the validator needs.

👉 Open

code_review_assistant/tools.py

👉 Find:

# MODULE_6_STEP_3_VALIDATE_FIXED_STYLE

👉 Replace with Tool 1 - Style Validator:

async def validate_fixed_style(tool_context: ToolContext) -> Dict[str, Any]:
    """
    Validates style compliance of the fixed code.

    Args:
        tool_context: ADK tool context containing fixed code in state

    Returns:
        Dictionary with style validation results
    """
    logger.info("Tool: Validating style of fixed code...")

    try:
        # Get the fixed code from state
        code_fixes = tool_context.state.get(StateKeys.CODE_FIXES, '')
       
        # Try to extract from markdown if present
        if '```python' in code_fixes:
            start = code_fixes.rfind('```python') + 9
            end = code_fixes.rfind('```')
            if start < end:
                code_fixes = code_fixes[start:end].strip()

        if not code_fixes:
            return {
                "status": "error",
                "message": "No fixed code found in state"
            }

        # Store the extracted fixed code
        tool_context.state[StateKeys.CODE_FIXES] = code_fixes

        # Run style check on fixed code
        loop = asyncio.get_event_loop()
        with ThreadPoolExecutor() as executor:
            style_result = await loop.run_in_executor(
                executor, _perform_style_check, code_fixes
            )

        # Compare with original
        original_score = tool_context.state.get(StateKeys.STYLE_SCORE, 0)
        improvement = style_result['score'] - original_score

        # Store results
        tool_context.state[StateKeys.FIXED_STYLE_SCORE] = style_result['score']
        tool_context.state[StateKeys.FIXED_STYLE_ISSUES] = style_result['issues']

        logger.info(f"Tool: Fixed code style score: {style_result['score']}/100 "
                    f"(improvement: +{improvement})")

        return {
            "status": "success",
            "fixed_style_score": style_result['score'],
            "original_style_score": original_score,
            "improvement": improvement,
            "remaining_issues": style_result['issues'],
            "perfect_style": style_result['score'] == 100
        }

    except Exception as e:
        logger.error(f"Tool: Style validation failed: {e}", exc_info=True)
        return {
            "status": "error",
            "message": str(e)
        }

👉 Find:

# MODULE_6_STEP_3_COMPILE_FIX_REPORT

👉 Replace with Tool 2 - Report Compiler:

async def compile_fix_report(tool_context: ToolContext) -> Dict[str, Any]:
    """
    Compiles comprehensive report of the fix process.

    Args:
        tool_context: ADK tool context with all fix pipeline data

    Returns:
        Comprehensive fix report
    """
    logger.info("Tool: Compiling comprehensive fix report...")

    try:
        # Gather all data
        original_code = tool_context.state.get(StateKeys.CODE_TO_REVIEW, '')
        code_fixes = tool_context.state.get(StateKeys.CODE_FIXES, '')

        # Test results
        original_tests = tool_context.state.get(StateKeys.TEST_EXECUTION_SUMMARY, {})
        fixed_tests = tool_context.state.get(StateKeys.FIX_TEST_EXECUTION_SUMMARY, {})

        # Parse if strings
        if isinstance(original_tests, str):
            try:
                original_tests = json.loads(original_tests)
            except:
                original_tests = {}

        if isinstance(fixed_tests, str):
            try:
                fixed_tests = json.loads(fixed_tests)
            except:
                fixed_tests = {}

        # Extract pass rates
        original_pass_rate = 0
        if original_tests:
            if 'pass_rate' in original_tests:
                original_pass_rate = original_tests['pass_rate']
            elif 'test_summary' in original_tests:
                # Handle test_runner_agent's JSON structure
                summary = original_tests['test_summary']
                total = summary.get('total_tests_run', 0)
                passed = summary.get('tests_passed', 0)
                if total > 0:
                    original_pass_rate = (passed / total) * 100
            elif 'passed' in original_tests and 'total' in original_tests:
                if original_tests['total'] > 0:
                    original_pass_rate = (original_tests['passed'] / original_tests['total']) * 100

        fixed_pass_rate = 0
        all_tests_pass = False
        if fixed_tests:
            if 'pass_rate' in fixed_tests:
                fixed_pass_rate = fixed_tests['pass_rate']
                all_tests_pass = fixed_tests.get('failed', 1) == 0
            elif 'passed' in fixed_tests and 'total' in fixed_tests:
                if fixed_tests['total'] > 0:
                    fixed_pass_rate = (fixed_tests['passed'] / fixed_tests['total']) * 100
                all_tests_pass = fixed_tests.get('failed', 0) == 0

        # Style scores
        original_style = tool_context.state.get(StateKeys.STYLE_SCORE, 0)
        fixed_style = tool_context.state.get(StateKeys.FIXED_STYLE_SCORE, 0)

        # Calculate improvements
        test_improvement = {
            'original_pass_rate': original_pass_rate,
            'fixed_pass_rate': fixed_pass_rate,
            'improvement': fixed_pass_rate - original_pass_rate,
            'all_tests_pass': all_tests_pass
        }

        style_improvement = {
            'original_score': original_style,
            'fixed_score': fixed_style,
            'improvement': fixed_style - original_style,
            'perfect_style': fixed_style == 100
        }

        # Determine overall status
        if all_tests_pass and style_improvement['perfect_style']:
            fix_status = 'SUCCESSFUL'
            status_emoji = '✅'
        elif test_improvement['improvement'] > 0 or style_improvement['improvement'] > 0:
            fix_status = 'PARTIAL'
            status_emoji = '⚠️'
        else:
            fix_status = 'FAILED'
            status_emoji = '❌'

        # Build comprehensive report
        report = {
            'status': fix_status,
            'status_emoji': status_emoji,
            'timestamp': datetime.now().isoformat(),
            'original_code': original_code,
            'code_fixes': code_fixes,
            'improvements': {
                'tests': test_improvement,
                'style': style_improvement
            },
            'summary': f"{status_emoji} Fix Status: {fix_status}\n"
                      f"Tests: {original_pass_rate:.1f}% → {fixed_pass_rate:.1f}%\n"
                      f"Style: {original_style}/100 → {fixed_style}/100"
        }

        # Store report in state
        tool_context.state[StateKeys.FIX_REPORT] = report
        tool_context.state[StateKeys.FIX_STATUS] = fix_status

        logger.info(f"Tool: Fix report compiled - Status: {fix_status}")
        logger.info(f"Tool: Test improvement: {original_pass_rate:.1f}% → {fixed_pass_rate:.1f}%")
        logger.info(f"Tool: Style improvement: {original_style} → {fixed_style}")

        return {
            "status": "success",
            "fix_status": fix_status,
            "report": report
        }

    except Exception as e:
        logger.error(f"Tool: Failed to compile fix report: {e}", exc_info=True)
        return {
            "status": "error",
            "message": str(e)
        }

👉 Find:

# MODULE_6_STEP_3_EXIT_FIX_LOOP

👉 Replace with Tool 3 - Loop Exit Signal:

def exit_fix_loop(tool_context: ToolContext) -> Dict[str, Any]:
    """
    Signal that fixing is complete and should exit the loop.
   
    Args:
        tool_context: ADK tool context
       
    Returns:
        Confirmation message
    """
    logger.info("Tool: Setting escalate flag to exit fix loop")
   
    # This is the critical line that exits the LoopAgent
    tool_context.actions.escalate = True
   
    return {
        "status": "success",
        "message": "Fix complete, exiting loop"
    }

Create the Validator Agent

👉 Open

code_review_assistant/sub_agents/fix_pipeline/fix_validator.py

👉 Find:

# MODULE_6_STEP_3_FIX_VALIDATOR_INSTRUCTION_PROVIDER

👉 Replace that single line with:

async def fix_validator_instruction_provider(context: ReadonlyContext) -> str:
    """Dynamic instruction provider that injects state variables."""
    template = """You are the final validation specialist for code fixes.

You have access to:
- Original issues from initial review
- Applied fixes: {code_fixes}
- Test results after fix: {fix_test_execution_summary}
- All state data from the fix process

Your responsibilities:
1. Use validate_fixed_style tool to check style compliance of fixed code
   - Pass no arguments, it will retrieve fixed code from state
2. Use compile_fix_report tool to generate comprehensive report
   - Pass no arguments, it will gather all data from state
3. Based on the report, determine overall fix status:
   - ✅ SUCCESSFUL: All tests pass, style score 100
   - ⚠️ PARTIAL: Improvements made but issues remain
   - ❌ FAILED: Fix didn't work or made things worse

4. CRITICAL: If status is SUCCESSFUL, call the exit_fix_loop tool to stop iterations
   - This prevents unnecessary additional fix attempts
   - If not successful, the loop will continue for another attempt

5. Provide clear summary of:
   - What was fixed
   - What improvements were achieved
   - Any remaining issues requiring manual attention

Be precise and quantitative in your assessment.
"""
    return await instructions_utils.inject_session_state(template, context)

👉 Find:

# MODULE_6_STEP_3_FIX_VALIDATOR_AGENT

👉 Replace that single line with:

fix_validator_agent = Agent(
    name="FixValidator",
    model=config.worker_model,
    description="Validates fixes and generates final fix report",
    instruction=fix_validator_instruction_provider,
    tools=[
        FunctionTool(func=validate_fixed_style),
        FunctionTool(func=compile_fix_report),
        FunctionTool(func=exit_fix_loop)
    ],
    output_key="final_fix_report"
)

Step 4: Understanding LoopAgent Exit Conditions

The LoopAgent has three ways to exit:

1. Success Exit (via escalate)

# Inside any tool in the loop:
tool_context.actions.escalate = True

# Effect: Loop completes current iteration, then exits
# Use when: Fix is successful and no more attempts needed

Example flow:

Iteration 1:
  CodeFixer → generates fixes
  FixTestRunner → tests show 90% pass rate
  FixValidator → compiles report, sees PARTIAL status
  → Does NOT set escalate
  → Loop continues

Iteration 2:
  CodeFixer → refines fixes based on failures
  FixTestRunner → tests show 100% pass rate
  FixValidator → compiles report, sees SUCCESSFUL status
  → Calls exit_fix_loop() which sets escalate = True
  → Loop exits after this iteration

2. Max Iterations Exit

LoopAgent(
    name="FixAttemptLoop",
    sub_agents=[...],
    max_iterations=3  # Safety limit
)

# Effect: After 3 complete iterations, loop exits regardless of escalate
# Use when: Prevent infinite loops if fixes never succeed

Example flow:

Iteration 1: PARTIAL (continue)
Iteration 2: PARTIAL (continue)
Iteration 3: PARTIAL (but max reached)
→ Loop exits, synthesizer presents best attempt

3. Error Exit

# If any agent throws unhandled exception:
raise Exception("Unexpected error")

# Effect: Loop exits immediately with error state
# Use when: Critical failure that can't be recovered

State Evolution Across Iterations:

Each iteration sees updated state from the previous attempt:

# Before Iteration 1:
state = {
    "code_to_review": "def add(a,b):return a+b",  # Original
    "style_score": 40,
    "test_execution_summary": {...}
}

# After Iteration 1:
state = {
    "code_to_review": "def add(a,b):return a+b",  # Unchanged
    "code_fixes": "def add(a, b):\n    return a + b",  # NEW
    "style_score": 40,  # Unchanged
    "fixed_style_score": 100,  # NEW
    "test_execution_summary": {...},  # Unchanged
    "fix_test_execution_summary": {...}  # NEW
}

# Iteration 2 starts with all this state
# If fixes still not perfect, code_fixes gets overwritten

Почему

escalate

Instead of Return Values:

# Bad: Using return value to signal exit
def validator_agent():
    report = compile_report()
    if report['status'] == 'SUCCESSFUL':
        return {"exit": True}  # How does loop know?

# Good: Using escalate
def validator_tool(tool_context):
    report = compile_report()
    if report['status'] == 'SUCCESSFUL':
        tool_context.actions.escalate = True  # Loop knows immediately
    return {"report": report}

Преимущества:

  • Works from any tool, not just the last one
  • Doesn't interfere with return data
  • Clear semantic meaning
  • Framework handles the exit logic

Шаг 5: Подключите трубопровод для устранения неполадок

👉 Open

code_review_assistant/agent.py

👉 Add the fix pipeline imports (after the existing imports):

from google.adk.agents import LoopAgent  # Add this to the existing Agent, SequentialAgent line
from code_review_assistant.sub_agents.fix_pipeline.code_fixer import code_fixer_agent
from code_review_assistant.sub_agents.fix_pipeline.fix_test_runner import fix_test_runner_agent
from code_review_assistant.sub_agents.fix_pipeline.fix_validator import fix_validator_agent
from code_review_assistant.sub_agents.fix_pipeline.fix_synthesizer import fix_synthesizer_agent

Your imports should now be:

from google.adk.agents import Agent, SequentialAgent, LoopAgent
from .config import config
# Review pipeline imports (from Module 5)
from code_review_assistant.sub_agents.review_pipeline.code_analyzer import code_analyzer_agent
from code_review_assistant.sub_agents.review_pipeline.style_checker import style_checker_agent
from code_review_assistant.sub_agents.review_pipeline.test_runner import test_runner_agent
from code_review_assistant.sub_agents.review_pipeline.feedback_synthesizer import feedback_synthesizer_agent
# Fix pipeline imports (NEW)
from code_review_assistant.sub_agents.fix_pipeline.code_fixer import code_fixer_agent
from code_review_assistant.sub_agents.fix_pipeline.fix_test_runner import fix_test_runner_agent
from code_review_assistant.sub_agents.fix_pipeline.fix_validator import fix_validator_agent
from code_review_assistant.sub_agents.fix_pipeline.fix_synthesizer import fix_synthesizer_agent

👉 Find:

# MODULE_6_STEP_5_CREATE_FIX_LOOP

👉 Replace that single line with:

# Create the fix attempt loop (retries up to 3 times)
fix_attempt_loop = LoopAgent(
    name="FixAttemptLoop",
    sub_agents=[
        code_fixer_agent,      # Step 1: Generate fixes
        fix_test_runner_agent, # Step 2: Validate with tests
        fix_validator_agent    # Step 3: Check success & possibly exit
    ],
    max_iterations=3  # Try up to 3 times
)

# Wrap loop with synthesizer for final report
code_fix_pipeline = SequentialAgent(
    name="CodeFixPipeline",
    description="Automated code fixing pipeline with iterative validation",
    sub_agents=[
        fix_attempt_loop,      # Try to fix (1-3 times)
        fix_synthesizer_agent  # Present final results (always runs once)
    ]
)

👉 Remove the existing

root_agent

определение:

root_agent = Agent(...)

👉 Find:

# MODULE_6_STEP_5_UPDATE_ROOT_AGENT

👉 Replace that single line with:

# Update root agent to include both pipelines
root_agent = Agent(
    name="CodeReviewAssistant",
    model=config.worker_model,
    description="An intelligent code review assistant that analyzes Python code and provides educational feedback",
    instruction="""You are a specialized Python code review assistant focused on helping developers improve their code quality.

When a user provides Python code for review:
1. Immediately delegate to CodeReviewPipeline and pass the code EXACTLY as it was provided by the user.
2. The pipeline will handle all analysis and feedback
3. Return ONLY the final feedback from the pipeline - do not add any commentary

After completing a review, if significant issues were identified:
- If style score < 100 OR tests are failing OR critical issues exist:
  * Add at the end: "\n\n💡 I can fix these issues for you. Would you like me to do that?"
 
- If the user responds yes or requests fixes:
  * Delegate to CodeFixPipeline
  * Return the fix pipeline's complete output AS-IS

When a user asks what you can do or general questions:
- Explain your capabilities for code review and fixing
- Do NOT trigger the pipeline for non-code messages

The pipelines handle everything for code review and fixing - just pass through their final output.""",
    sub_agents=[code_review_pipeline, code_fix_pipeline],
    output_key="assistant_response"
)

Шаг 6: Добавьте агент синтезатора исправлений

The synthesizer creates a user-friendly presentation of fix results after the loop completes.

👉 Open

code_review_assistant/sub_agents/fix_pipeline/fix_synthesizer.py

👉 Find:

# MODULE_6_STEP_6_FIX_SYNTHESIZER_INSTRUCTION_PROVIDER

👉 Replace that single line with:

async def fix_synthesizer_instruction_provider(context: ReadonlyContext) -> str:
    """Dynamic instruction provider that injects state variables."""
    template = """You are responsible for presenting the fix results to the user.

Based on the validation report: {final_fix_report}
Fixed code from state: {code_fixes}
Fix status: {fix_status}

Create a comprehensive yet friendly response that includes:

## 🔧 Fix Summary
[Overall status and key improvements - be specific about what was achieved]

## 📊 Metrics
- Test Results: [original pass rate]% → [new pass rate]%
- Style Score: [original]/100 → [new]/100
- Issues Fixed: X of Y

## ✅ What Was Fixed
[List each fixed issue with brief explanation of the correction made]

## 📝 Complete Fixed Code
[Include the complete, corrected code from state - this is critical]

## 💡 Explanation of Key Changes
[Brief explanation of the most important changes made and why]

[If any issues remain]
## ⚠️ Remaining Issues
[List what still needs manual attention]

## 🎯 Next Steps
[Guidance on what to do next - either use the fixed code or address remaining issues]

Save the fix report using save_fix_report tool before presenting.
Call it with no parameters - it will retrieve the report from state automatically.

Be encouraging about improvements while being honest about any remaining issues.
Focus on the educational aspect - help the user understand what was wrong and how it was fixed.
"""
    return await instructions_utils.inject_session_state(template, context)

👉 Find:

# MODULE_6_STEP_6_FIX_SYNTHESIZER_AGENT

👉 Replace that single line with:

fix_synthesizer_agent = Agent(
    name="FixSynthesizer",
    model=config.critic_model,
    description="Creates comprehensive user-friendly fix report",
    instruction=fix_synthesizer_instruction_provider,
    tools=[FunctionTool(func=save_fix_report)],
    output_key="fix_summary"
)

👉 Add

save_fix_report

tool to

tools.py

:

👉 Find:

# MODULE_6_STEP_6_SAVE_FIX_REPORT

👉 Replace with:

async def save_fix_report(tool_context: ToolContext) -> Dict[str, Any]:
    """
    Saves the fix report as an artifact.

    Args:
        tool_context: ADK tool context

    Returns:
        Save status
    """
    logger.info("Tool: Saving fix report...")

    try:
        # Get the report from state
        fix_report = tool_context.state.get(StateKeys.FIX_REPORT, {})

        if not fix_report:
            return {
                "status": "error",
                "message": "No fix report found in state"
            }

        # Convert to JSON
        report_json = json.dumps(fix_report, indent=2)
        report_part = types.Part.from_text(text=report_json)

        # Generate filename
        timestamp = datetime.now().isoformat().replace(':', '-')
        filename = f"fix_report_{timestamp}.json"

        # Try to save as artifact
        if hasattr(tool_context, 'save_artifact'):
            try:
                version = await tool_context.save_artifact(filename, report_part)
                await tool_context.save_artifact("latest_fix_report.json", report_part)

                logger.info(f"Tool: Fix report saved as {filename}")

                return {
                    "status": "success",
                    "filename": filename,
                    "version": str(version),
                    "size": len(report_json)
                }
            except Exception as e:
                logger.warning(f"Could not save as artifact: {e}")

        # Fallback: store in state
        tool_context.state[StateKeys.LAST_FIX_REPORT] = fix_report

        return {
            "status": "success",
            "message": "Fix report saved to state",
            "size": len(report_json)
        }

    except Exception as e:
        logger.error(f"Tool: Failed to save fix report: {e}", exc_info=True)
        return {
            "status": "error",
            "message": str(e)
        }

Шаг 7: Тестирование полного конвейера исправлений

Time to see the entire loop in action.

👉 Start the system:

adk web code_review_assistant

After running the adk web command, you should see output in your terminal indicating that the ADK Web Server has started, similar to this:

+-----------------------------------------------------------------------------+
| ADK Web Server started                                                      |
|                                                                             |
| For local testing, access at http://localhost:8000.                         |
+-----------------------------------------------------------------------------+

INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)

👉 Test Prompt:

Please analyze the following:
def dfs_search_v1(graph, start, target):
    """Find if target is reachable from start."""
    visited = set()
    stack = start
   
    while stack:
        current = stack.pop()
       
        if current == target:
            return True
           
        if current not in visited:
            visited.add(current)
           
            for neighbor in graph[current]:
                if neighbor not in visited:
                    stack.append(neighbor)
   
    return False

First, submit the buggy code to trigger the review pipeline . After it identifies the flaws, you will ask the agent to "Please fix the code" which triggers the powerful, iterative fix pipeline .

fix-pipeline-in-action

1. Первоначальный обзор (выявление недостатков)

This is the first half of the process. The four-agent review pipeline analyzes the code, checks its style, and runs a generated test suite. It correctly identifies a critical AttributeError and other issues, delivering a verdict: the code is BROKEN , with a test pass rate of only 84.21% .

2. Автоматизированное исправление (цикл в действии)

This is the most impressive part. When you ask the agent to fix the code, it doesn't just make one change. It kicks off an iterative Fix and Validate Loop that works just like a diligent developer: it tries a fix, tests it thoroughly, and if it's not perfect, it tries again.

Iteration #1: The First Attempt (Partial Success)

  • The Fix: The CodeFixer agent reads the initial report and makes the most obvious corrections. It changes stack = start to stack = [start] and uses graph.get() to prevent KeyError exceptions.
  • The Validation: The TestRunner immediately re-runs the full test suite against this new code.
  • The Result: The pass rate improves significantly to 88.89% ! The critical bugs are gone. However, the tests are so comprehensive that they reveal two new, subtle bugs (regressions) related to handling None as a graph or non-list neighbor values. The system marks the fix as PARTIAL .

Iteration #2: The Final Polish (100% Success)

  • The Fix: Because the loop's exit condition (100% pass rate) was not met, it runs again. The CodeFixer now has more information—the two new regression failures. It generates a final, more robust version of the code that explicitly handles those edge cases.
  • The Validation: The TestRunner executes the test suite one last time against the final version of the code.
  • The Result: A perfect 100% pass rate . All original bugs and all regressions are resolved. The system marks the fix as SUCCESSFUL and the loop exits.

3. Итоговый отчет: высший балл

With a fully validated fix, the FixSynthesizer agent takes over to present the final report, transforming the technical data into a clear, educational summary.

Метрическая

До

После

Улучшение

Test Pass Rate

84.21%

100%

▲ 15.79%

Оценка стиля

88 / 100

98 / 100

▲ 10 pts

Bugs Fixed

0 of 3

3 из 3

✅ Окончательный, проверенный код

Here is the complete, corrected code that now passes all 19 tests, demonstrating the successful fix:

def dfs_search_v1(graph, start, target):
    """Find if target is reachable from start."""
    # Handles 'None' graph input
    if graph is None:
        return False

    visited = set()
    # Fixes the critical AttributeError
    stack = [start]

    while stack:
        current = stack.pop()

        if current == target:
            return True

        if current not in visited:
            visited.add(current)
            
            # Safely gets neighbors to prevent KeyError
            neighbors = graph.get(current)

            if neighbors is None:
                continue
            
            # Validates that neighbors are iterable
            if not isinstance(neighbors, (list, set, tuple)):
                raise TypeError(
                    f"Graph value for node '{current}' is of type "
                    f"{type(neighbors).__name__}. Expected a list, set, or tuple."
                )
            
            for neighbor in neighbors:
                if neighbor not in visited:
                    stack.append(neighbor)

    return False

👉💻 Once you're done testing, return to your Cloud Shell Editor terminal and press Ctrl+C to stop the ADK Dev UI.

What You've Built

You now have a complete automated fix pipeline that:

Generates fixes - Based on review analysis
Validates iteratively - Tests after each fix attempt
Retries automatically - Up to 3 attempts for success
Exits intelligently - Via escalate when successful
Tracks improvements - Compares before/after metrics
Provides artifacts - Downloadable fix reports

Освоенные ключевые концепции

LoopAgent vs Sequential:

  • Sequential: One pass through agents
  • LoopAgent: Repeats until exit condition or max iterations
  • Exit via tool_context.actions.escalate = True

State Evolution Across Iterations:

  • CODE_FIXES updated each iteration
  • Test results show improvement over time
  • Validator sees cumulative changes

Multi-Pipeline Architecture:

  • Review pipeline: Read-only analysis (Module 5)
  • Fix loop: Iterative correction (Module 6 inner loop)
  • Fix pipeline: Loop + synthesizer (Module 6 outer)
  • Root agent: Orchestrates based on user intent

Tools Controlling Flow:

  • exit_fix_loop() sets escalate
  • Any tool can signal loop completion
  • Decouples exit logic from agent instructions

Max Iterations Safety:

  • Prevents infinite loops
  • Ensures system always responds
  • Presents best attempt even if not perfect

Что дальше?

In the final module, you'll learn how to deploy your agent to production :

  • Setting up persistent storage with VertexAiSessionService
  • Deploying to Agent Engine on Google Cloud
  • Monitoring and debugging production agents
  • Best practices for scaling and reliability

You've built a complete multi-agent system with sequential and loop architectures. The patterns you've learned - state management, dynamic instructions, tool orchestration, and iterative refinement - are production-ready techniques used in real agentic systems.

7. Deploying to Production

adk-deploy.png

Введение

Your code review assistant is now complete with review and fix pipelines working locally. The missing piece: it only runs on your machine. In this module, you'll deploy your agent to Google Cloud, making it accessible to your team with persistent sessions and production-grade infrastructure.

Что вы узнаете:

  • Three deployment paths: Local, Cloud Run, and Agent Engine
  • Automated infrastructure provisioning
  • Session persistence strategies
  • Testing deployed agents

Понимание вариантов развертывания

The ADK supports multiple deployment targets, each with different tradeoffs:

Deployment Paths

Factor

Local ( adk web )

Cloud Run ( adk deploy cloud_run )

Agent Engine ( adk deploy agent_engine )

Сложность

Минимальный

Середина

Низкий

Session Persistence

In-memory only (lost on restart)

Cloud SQL (PostgreSQL)

Vertex AI managed (automatic)

Инфраструктура

None (dev machine only)

Container + Database

Fully managed

Cold Start

Н/Д

100-2000ms

100-500ms

Scaling

Единичный экземпляр

Automatic (to zero)

Автоматический

Cost Model

Free (local compute)

Request-based + free tier

Compute-based

UI Support

Yes (via adk web )

Yes (via --with_ui )

No (API only)

Лучше всего подходит для

Development/testing

Variable traffic, cost control

Production agents

Additional deployment option: Google Kubernetes Engine (GKE) is available for advanced users requiring Kubernetes-level control, custom networking, or multi-service orchestration. GKE deployment is not covered in this codelab but is documented in the ADK deployment guide .

Что будет развернуто

When deploying to Cloud Run or Agent Engine, the following is packaged and deployed:

  • Your agent code ( agent.py , all sub-agents, tools)
  • Dependencies ( requirements.txt )
  • ADK API server (automatically included)
  • Web UI (Cloud Run only, when --with_ui specified)

Important differences:

  • Cloud Run : Uses adk deploy cloud_run CLI (builds container automatically) or gcloud run deploy (requires custom Dockerfile)
  • Agent Engine : Uses adk deploy agent_engine CLI (no container building needed, directly packages Python code)

Step 1: Configure Your Environment

Настройте свой файл .env

Your .env file (created in Module 3) needs updates for cloud deployment. Open .env and verify/update these settings:

Required for all cloud deployments:

# Your actual GCP Project ID (REQUIRED)
GOOGLE_CLOUD_PROJECT=your-project-id

# GCP region for deployments (REQUIRED)
GOOGLE_CLOUD_LOCATION=us-central1

# Use Vertex AI (REQUIRED)
GOOGLE_GENAI_USE_VERTEXAI=true

# Model configuration (already set)
WORKER_MODEL=gemini-2.5-flash
CRITIC_MODEL=gemini-2.5-pro

Set bucket names (REQUIRED before running deploy.sh):

The deployment script creates buckets based on these names. Set them now:

# Staging bucket for Agent Engine code uploads (REQUIRED for agent-engine)
STAGING_BUCKET=gs://your-project-id-staging

# Artifact storage for reports and fixed code (REQUIRED for both cloud-run and agent-engine)
ARTIFACT_BUCKET=gs://your-project-id-artifacts

Replace your-project-id with your actual project ID in both bucket names. The script will create these buckets if they don't exist.

Optional variables (created automatically if blank):

# Agent Engine ID (populated after first deployment)
AGENT_ENGINE_ID=

# Cloud Run Database credentials (created automatically if blank)
CLOUD_SQL_INSTANCE_NAME=
DB_USER=
DB_PASSWORD=
DB_NAME=

Проверка подлинности

If you encounter authentication errors during deployment:

gcloud auth application-default login
gcloud config set project $GOOGLE_CLOUD_PROJECT

Шаг 2: Понимание сценария развертывания

The deploy.sh script provides a unified interface for all deployment modes:

./deploy.sh {local|cloud-run|agent-engine}

Возможности скрипта

Infrastructure provisioning:

  • API enablement (AI Platform, Storage, Cloud Build, Cloud Trace, Cloud SQL)
  • IAM permission configuration (service accounts, roles)
  • Resource creation (buckets, databases, instances)
  • Deployment with proper flags
  • Post-deployment verification

Ключевые разделы сценария

  • Configuration (lines 1-35) : Project, region, service names, defaults
  • Helper Functions (lines 37-200) : API enablement, bucket creation, IAM setup
  • Main Logic (lines 202-400) : Mode-specific deployment orchestration

Шаг 3: Подготовка агента для Agent Engine

Before deploying to Agent Engine, an agent_engine_app.py file is needed that wraps your agent for the managed runtime. This has been created for you already.

Посмотреть code_review_assistant/agent_engine_app.py

👉 Open file:

"""
Agent Engine application wrapper.
This file prepares the agent for deployment to Vertex AI Agent Engine.
"""

from vertexai import agent_engines
from .agent import root_agent

# Wrap the agent in an AdkApp object for Agent Engine deployment
app = agent_engines.AdkApp(
    agent=root_agent,
    enable_tracing=True,
)

Step 4: Deploy to Agent Engine

Agent Engine is the recommended production deployment for ADK agents because it provides:

  • Fully managed infrastructure (no containers to build)
  • Built-in session persistence via VertexAiSessionService
  • Automatic scaling from zero
  • Cloud Trace integration enabled by default

How Agent Engine Differs from Other Deployments

Under the hood,

deploy.sh agent-engine

uses:

adk deploy agent_engine \
  --project=$GOOGLE_CLOUD_PROJECT \
  --region=$GOOGLE_CLOUD_LOCATION \
  --staging_bucket=$STAGING_BUCKET \
  --display_name="Code Review Assistant" \
  --trace_to_cloud \
  code_review_assistant

This command:

  • Packages your Python code directly (no Docker build)
  • Uploads to the staging bucket you specified in .env
  • Creates a managed Agent Engine instance
  • Enables Cloud Trace for observability
  • Uses agent_engine_app.py to configure the runtime

Unlike Cloud Run which containerizes your code, Agent Engine runs your Python code directly in a managed runtime environment, similar to serverless functions.

Запустить развертывание

From your project root:

./deploy.sh agent-engine

Фазы развертывания

Watch the script execute these phases:

Phase 1: API Enablement
   aiplatform.googleapis.com
   storage-api.googleapis.com
   cloudbuild.googleapis.com
   cloudtrace.googleapis.com

Phase 2: IAM Setup
   Getting project number
   Granting Storage Object Admin
   Granting AI Platform User
   Granting Cloud Trace Agent

Phase 3: Staging Bucket
   Creating gs://your-project-id-staging
   Setting permissions

Phase 4: Artifact Bucket
   Creating gs://your-project-id-artifacts
   Configuring access

Phase 5: Validation
   Checking agent.py exists
   Verifying root_agent defined
   Checking agent_engine_app.py exists
   Validating requirements.txt

Phase 6: Build & Deploy
   Packaging agent code
   Uploading to staging bucket
   Creating Agent Engine instance
   Configuring session persistence
   Setting up Cloud Trace integration
   Running health checks

This process takes 5-10 minutes as it packages the agent and deploys it to Vertex AI infrastructure.

Сохраните свой идентификатор агентской системы

Upon successful deployment:

✅ Deployment successful!
   Agent Engine ID: 7917477678498709504
   Resource Name: projects/123456789/locations/us-central1/reasoningEngines/7917477678498709504
   Endpoint: https://us-central1-aiplatform.googleapis.com/v1/...

⚠️  IMPORTANT: Save this in your .env file:
   AGENT_ENGINE_ID=7917477678498709504

Update your

.env

file immediately:

echo "AGENT_ENGINE_ID=7917477678498709504" >> .env

This ID is required for:

  • Testing the deployed agent
  • Updating the deployment later
  • Accessing logs and traces

Что было развернуто

Your Agent Engine deployment now includes:

✅ Complete review pipeline (4 agents)
✅ Complete fix pipeline (loop + synthesizer)
✅ All tools (AST analysis, style checking, artifact generation)
✅ Session persistence (automatic via VertexAiSessionService )
✅ State management (session/user/lifetime tiers)
✅ Observability (Cloud Trace enabled)
✅ Auto-scaling infrastructure

Шаг 5: Проверьте развернутый агент

Обновите ваш файл .env

After deployment, verify your .env includes:

AGENT_ENGINE_ID=7917477678498709504  # From deployment output
GOOGLE_CLOUD_PROJECT=your-project-id
GOOGLE_CLOUD_LOCATION=us-central1

Run the Test Script

The project includes tests/test_agent_engine.py specifically for testing Agent Engine deployments:

python tests/test_agent_engine.py

What the Test Does

  1. Authenticates with your Google Cloud project
  2. Creates a session with the deployed agent
  3. Sends a code review request (the DFS bug example)
  4. Streams the response back via Server-Sent Events (SSE)
  5. Verifies session persistence and state management

Ожидаемый результат

Authenticated with project: your-project-id
Targeting Agent Engine: projects/.../reasoningEngines/7917477678498709504

Creating new session...
Created session: 4857885913439920384

Sending query to agent and streaming response:
data: {"content": {"parts": [{"text": "I'll analyze your code..."}]}}
data: {"content": {"parts": [{"text": "**Code Structure Analysis**\n..."}]}}
data: {"content": {"parts": [{"text": "**Style Check Results**\n..."}]}}
data: {"content": {"parts": [{"text": "**Test Results**\n..."}]}}
data: {"content": {"parts": [{"text": "**Final Feedback**\n..."}]}}

Stream finished.

Контрольный список проверки

  • ✅ Full review pipeline executes (all 4 agents)
  • ✅ Streaming response shows progressive output
  • ✅ Session state persists across requests
  • ✅ No authentication or connection errors
  • ✅ Tool calls execute successfully (AST analysis, style checking)
  • ✅ Artifacts are saved (grading report accessible)

Альтернатива: Развертывание в Cloud Run

While Agent Engine is recommended for streamlined production deployment, Cloud Run offers more control and supports the ADK web UI. This section provides an overview.

Когда использовать Cloud Run

Choose Cloud Run if you need:

  • The ADK web UI for user interaction
  • Full control over the container environment
  • Custom database configurations
  • Integration with existing Cloud Run services

Как работает развертывание Cloud Run

Under the hood,

deploy.sh cloud-run

uses:

adk deploy cloud_run \
  --project=$GOOGLE_CLOUD_PROJECT \
  --region=$GOOGLE_CLOUD_LOCATION \
  --service_name="code-review-assistant" \
  --app_name="code_review_assistant" \
  --port=8080 \
  --with_ui \
  --artifact_service_uri="gs://$ARTIFACT_BUCKET" \
  --trace_to_cloud \
  code_review_assistant

This command:

  • Builds a Docker container with your agent code
  • Pushes to Google Artifact Registry
  • Deploys as a Cloud Run service
  • Includes the ADK web UI ( --with_ui )
  • Configures Cloud SQL connection (added by script after initial deployment)

The key difference from Agent Engine: Cloud Run containerizes your code and requires a database for session persistence, while Agent Engine handles both automatically.

Команда развертывания Cloud Run

./deploy.sh cloud-run

What's Different

Инфраструктура:

  • Containerized deployment (Docker built automatically by ADK)
  • Cloud SQL (PostgreSQL) for session persistence
  • Database auto-created by script or uses existing instance

Session Management:

  • Uses DatabaseSessionService instead of VertexAiSessionService
  • Requires database credentials in .env (or auto-generated)
  • State persists in PostgreSQL database

UI Support:

  • Web UI available via --with_ui flag (handled by script)
  • Access at: https://code-review-assistant-xyz.a.run.app

Чего вы достигли

Your production deployment includes:

Automated provisioning via deploy.sh script
Managed infrastructure (Agent Engine handles scaling, persistence, monitoring)
Persistent state across all memory tiers (session/user/lifetime)
Secure credential management (automatic generation and IAM setup)
Scalable architecture (zero to thousands of concurrent users)
Built-in observability (Cloud Trace integration enabled)
Production-grade error handling and recovery

Освоенные ключевые концепции

Deployment Preparation:

  • agent_engine_app.py : Wraps agent with AdkApp for Agent Engine
  • AdkApp automatically configures VertexAiSessionService for persistence
  • Tracing enabled via enable_tracing=True

Deployment Commands:

  • adk deploy agent_engine : Packages Python code, no containers
  • adk deploy cloud_run : Builds Docker container automatically
  • gcloud run deploy : Alternative with custom Dockerfile

Deployment Options:

  • Agent Engine: Fully managed, fastest to production
  • Cloud Run: More control, supports web UI
  • GKE: Advanced Kubernetes control (see GKE deployment guide )

Managed Services:

  • Agent Engine handles session persistence automatically
  • Cloud Run requires database setup (or auto-created)
  • Both support artifact storage via GCS

Session Management:

  • Agent Engine: VertexAiSessionService (automatic)
  • Cloud Run: DatabaseSessionService (Cloud SQL)
  • Local: InMemorySessionService (ephemeral)

Ваш агент в сети

Your code review assistant is now:

  • Accessible via HTTPS API endpoints
  • Persistent with state surviving restarts
  • Scalable to handle team growth automatically
  • Observable with complete request traces
  • Maintainable through scripted deployments

What's Next? In Module 8, you'll learn to use Cloud Trace to understand your agent's performance, identify bottlenecks in the review and fix pipelines, and optimize execution times.

8. Production Observability

cloud-trace-agent-engine.png

Введение

Your code review assistant is now deployed and running in production on Agent Engine. But how do you know it's working well? Can you answer these critical questions:

  • Is the agent responding quickly enough?
  • Which operations are slowest?
  • Are the fix loops completing efficiently?
  • Where are performance bottlenecks?

Without observability, you're operating blind. The --trace-to-cloud flag you used during deployment automatically enabled Cloud Trace, giving you complete visibility into every request your agent processes.

In this module, you'll learn to read traces, understand your agent's performance characteristics, and identify areas for optimization.

Understanding Traces and Spans

What is a Trace?

A trace is the complete timeline of your agent handling a single request. It captures everything from when a user sends a query until the final response is delivered. Each trace shows:

  • Total duration of the request
  • All operations that executed
  • How operations relate to each other (parent-child relationships)
  • When each operation started and ended

What is a Span?

A span represents a single unit of work within a trace. Common span types in your code review assistant:

  • agent_run : Execution of an agent (root agent or sub-agent)
  • call_llm : Request to a language model
  • execute_tool : Tool function execution
  • state_read / state_write : State management operations
  • code_executor : Running code with tests

Spans have:

  • Name : What operation this represents
  • Duration : How long it took
  • Attributes : Metadata like model name, token counts, inputs/outputs
  • Status : Success or failure
  • Parent/child relationships : Which operations triggered which

Automatic Instrumentation

When you deployed with --trace-to-cloud , ADK automatically instruments:

  • Every agent invocation and sub-agent call
  • All LLM requests with token counts
  • Tool executions with inputs/outputs
  • State operations (read/write)
  • Loop iterations in your fix pipeline
  • Error conditions and retries

No code changes required - tracing is built into ADK's runtime.

Шаг 1: Доступ к Cloud Trace Explorer

Open Cloud Trace in your Google Cloud Console:

  1. Navigate to Cloud Trace Explorer
  2. Select your project from the dropdown (should be pre-selected)
  3. You should see traces from your test in Module 7

If you don't see traces yet:

The test you ran in Module 7 should have generated traces. If the list is empty, generate some trace data:

python tests/test_agent_engine.py

Wait 1-2 minutes for traces to appear in the console.

На что вы смотрите

The Trace Explorer shows:

  • List of traces : Each row represents one complete request
  • Timeline : When requests occurred
  • Duration : How long each request took
  • Request details : Timestamp, latency, span count

This is your production traffic log - every interaction with your agent creates a trace.

Шаг 2: Проверьте трассировку конвейера обзора

Click on any trace in the list to open the waterfall view

You'll see a Gantt chart showing the complete execution timeline. Here's what a typical review pipeline trace looks like:

invocation (2.3s) ────────────────────────────────────────────►
├── agent_run: CodeReviewAssistant (2.2s) ──────────────────►
   ├── state_read: CODE_TO_REVIEW (0.01s) 
   ├── agent_run: CodeReviewPipeline (2.1s) ─────────────►
      ├── agent_run: CodeAnalyzer (0.3s) ──────►
         ├── execute_tool: analyze_code_structure (0.1s) ──►
         └── call_llm: gemini-2.5-flash (0.15s) ────►
      ├── agent_run: StyleChecker (0.2s) ──────►
         ├── execute_tool: check_code_style (0.1s) ──►
         └── call_llm: gemini-2.5-flash (0.08s) ──►
      ├── agent_run: TestRunner (1.2s) ─────────────►
         └── code_executor: BuiltInCodeExecutor (0.9s) ────►
      └── agent_run: FeedbackSynthesizer (0.4s) ────────►
          └── call_llm: gemini-2.5-flash (0.28s) ────►

Читая водопад

Each bar represents a span. The horizontal position shows when it started, and the length shows how long it took.

Key insights from this trace:

  • Total latency : 2.3 seconds from request to response
  • Critical path : TestRunner takes 1.2s (52% of total time)
  • Bottleneck : Code execution within TestRunner takes 0.9s (75% of TestRunner's time)
  • State operations : Very fast (10ms each) - not a concern
  • Pipeline structure : Sequential execution - CodeAnalyzer → StyleChecker → TestRunner → FeedbackSynthesizer

Проверка деталей пролета

Click on the

call_llm: gemini-2.5-flash

span under FeedbackSynthesizer

You'll see detailed attributes for this LLM call:

{
  "name": "call_llm",
  "span_kind": "LLM",
  "duration": "280ms",
  "attributes": {
    "llm.model": "models/gemini-2.5-flash",
    "llm.request_type": "GenerateContent",
    "llm.usage.prompt_tokens": 845,
    "llm.usage.completion_tokens": 234,
    "llm.usage.total_tokens": 1079,
    "llm.response.finish_reason": "STOP",
    "status_code": "OK"
  }
}

This shows:

  • Which model was used
  • How many tokens were consumed (input + output)
  • Request duration
  • Success/failure status
  • The full prompt is also visible in the attributes (scroll to see it)

Понимание потока трубопровода

Notice how the trace reveals your architecture:

  1. Root agent (CodeReviewAssistant) receives the request
  2. State read retrieves the code to review
  3. Review pipeline orchestrates four sub-agents sequentially
  4. Each sub-agent uses tools and LLM calls to complete its work
  5. Final response flows back up through the hierarchy

This visibility helps you understand exactly what happens during each request.

Шаг 3: Анализ трассировки конвейера исправлений

The fix pipeline is more complex because it includes loops. Let's examine how traces capture iterative behavior.

Find a trace that includes "CodeFixPipeline" in the span names

You may need to scroll through your traces or send a request that triggers the fix pipeline. If you don't have one, you can generate it:

# In your test script, respond "yes" when asked to fix issues
python tests/test_agent_engine.py

Examining Loop Structure

Here's what a fix pipeline trace with 2 iterations looks like:

agent_run: CodeFixPipeline (8.5s) ───────────────────────►
├── agent_run: FixAttemptLoop (7.8s) ───────────────────►
   ├── loop_iteration: 1 (3.2s) ──────────►
      ├── agent_run: CodeFixer (0.8s) ────►
         └── call_llm: gemini-2.5-flash (0.7s) ───►
      ├── agent_run: FixTestRunner (1.8s) ─────────►
         └── code_executor: BuiltInCodeExecutor (1.5s) ─────►
      └── agent_run: FixValidator (0.6s) ────►
          ├── execute_tool: validate_fixed_style (0.2s) ──►
          └── state_write: FIX_STATUS = "PARTIAL" 
   
   ├── loop_iteration: 2 (4.5s) ─────────────────►
      ├── agent_run: CodeFixer (1.0s) ──────►
         └── call_llm: gemini-2.5-flash (0.9s) ───►
      ├── agent_run: FixTestRunner (2.0s) ────────►
         └── code_executor: BuiltInCodeExecutor (1.7s) ─────►
      └── agent_run: FixValidator (1.5s) ──────►
          ├── execute_tool: compile_fix_report (0.3s) ──►
          └── state_write: FIX_STATUS = "SUCCESSFUL" 
   
   └── loop_exit: escalation_triggered 

└── agent_run: FixSynthesizer (0.7s) ────►
    ├── execute_tool: save_fix_report (0.2s) ──►
    └── call_llm: gemini-2.5 (0.4s) ────►

Ключевые наблюдения о циклах

Iteration patterns:

  • Two iterations : First attempt achieved partial success, second completed fully
  • Progressive cost : Iteration 2 takes longer (4.5s vs 3.2s)
  • State tracking : Each iteration writes FIX_STATUS to state
  • Exit mechanism : Loop terminates via escalation when FIX_STATUS = "SUCCESSFUL"

What this reveals:

  • Your loop architecture is working correctly
  • Most fixes complete in 1-2 iterations (good design)
  • Each iteration includes: fix generation → testing → validation
  • Code execution dominates each iteration (1.5-1.7s)
  • The loop properly exits when conditions are met

Разбивка затрат:

  • Iteration 1: 3.2s
  • Iteration 2: 4.5s (longer due to accumulated context)
  • Total loop: 7.8s
  • Synthesis: 0.7s
  • Total fix pipeline: 8.5s

Сравнение с конвейером обзоров

Review pipeline: ~2.3s
Fix pipeline: ~8.5s (with 2 iterations)

The fix pipeline takes ~3.7x longer, which makes sense:

  • It includes iterative refinement
  • It runs code multiple times (once per iteration)
  • It accumulates context from previous attempts

Шаг 4: Что вы обнаружили

Модели производительности

From examining traces, you now know:

Review pipeline:

  • Typical duration: 2-3 seconds
  • Main time consumer: TestRunner (code execution)
  • LLM calls: Fast (100-300ms each)
  • State operations: Negligible (10ms)

Fix pipeline:

  • Typical duration: 4-5 seconds per iteration
  • Most fixes: 1-2 iterations
  • Code execution: 1.5-2.0 seconds per iteration
  • Progressive cost: Later iterations take longer

What's fast:

  • State reads/writes (10ms)
  • Tool executions for analysis (100ms)
  • Individual LLM calls (100-300ms)

What's slow (but necessary):

  • Code execution with tests (0.9-2.0s)
  • Multiple loop iterations (cumulative)

Где искать проблемы

When reviewing traces in production, watch for:

  • Unusually long traces (>15 seconds) - investigate what went wrong
  • Failed spans (status != OK) - errors in execution
  • Excessive loop iterations (>2) - fix quality issues
  • Very high token counts - prompt optimization opportunities

Что вы узнали

Through Cloud Trace, you now understand:

Request flow : Complete execution path through your pipelines
Performance characteristics : What's fast, what's slow, and why
Loop behavior : How iterations execute and terminate
Span hierarchy : How operations nest within each other
Trace navigation : Reading waterfall charts effectively
Token visibility : Where LLM costs accumulate

Освоенные ключевые концепции

Traces and Spans:

  • Traces = complete request timelines
  • Spans = individual operations within traces
  • Waterfall view shows execution hierarchy
  • Automatic instrumentation via ADK

Performance Analysis:

  • Reading Gantt chart visualizations
  • Identifying critical paths
  • Understanding duration distributions
  • Spotting bottlenecks

Production Visibility:

  • Every operation traced automatically
  • Token usage captured per LLM call
  • State changes visible and traceable
  • Loop iterations individually tracked

Что дальше?

Continue exploring Cloud Trace:

  • Monitor traces regularly to catch issues early
  • Compare traces to identify performance regressions
  • Use trace data to inform optimization decisions
  • Filter by duration to find slow requests

Advanced observability (optional):

  • Export traces to BigQuery for complex analysis ( docs )
  • Create custom dashboards in Cloud Monitoring
  • Set up alerts for performance degradation
  • Correlate traces with application logs

9. Conclusion: From Prototype to Production

What You've Built

You started with just seven lines of code and built a production-grade AI agent system:

# Where we started (7 lines)
agent = Agent(
    model="gemini-2.5-flash",
    instruction="Review Python code for issues"
)

# Where we ended (production system)
- Two distinct multi-agent pipelines (review and fix) built from 8 specialized agents.
- An iterative fix loop architecture for automated validation and retries.
- Real AST-based code analysis tools for deterministic, accurate feedback.
- Robust state management using the "constants pattern" for type-safe communication.
- Fully automated deployment to a managed, scalable cloud infrastructure.
- Complete, built-in observability with Cloud Trace for production monitoring.

Освоены ключевые архитектурные шаблоны

Шаблон

Выполнение

Production Impact

Интеграция инструментов

AST analysis, style checking

Real validation, not just LLM opinions

Sequential Pipelines

Review → Fix workflows

Predictable, debuggable execution

Loop Architecture

Iterative fixing with exit conditions

Self-improving until success

Государственное управление

Constants pattern, three-tier memory

Type-safe, maintainable state handling

Production Deployment

Agent Engine via deploy.sh

Managed, scalable infrastructure

Наблюдаемость

Cloud Trace integration

Full visibility into production behavior

Производственная информация от Traces

Your Cloud Trace data revealed critical insights:
Bottleneck identified : TestRunner's LLM calls dominate latency
Tool performance : AST analysis executes in 100ms (excellent)
Success rate : Fix loops converge within 2-3 iterations
Token usage : ~600 tokens per review, ~1800 for fixes

These insights drive continuous improvement.

Ресурсы для очистки (необязательно)

If you're done experimenting and want to avoid charges:

Delete Agent Engine deployment:

import vertexai

client = vertexai.Client(  # For service interactions via client.agent_engines
    project="PROJECT_ID",
    location="LOCATION",
)

RESOURCE_NAME = "projects/{PROJECT_ID}/locations/{LOCATION}/reasoningEngines/{RESOURCE_ID}"

client.agent_engines.delete(
    name=RESOURCE_NAME,
    force=True, # Optional, if the agent has resources (e.g. sessions, memory)
)

Delete Cloud Run service (if created):

gcloud run services delete code-review-assistant \
    --region=$GOOGLE_CLOUD_LOCATION \
    --quiet

Delete Cloud SQL instance (if created):

gcloud sql instances delete your-project-db \
    --quiet

Clean up storage buckets:

gsutil -m rm -r gs://your-project-staging
gsutil -m rm -r gs://your-project-artifacts

Следующие шаги

With your foundation complete, consider these enhancements:

  1. Add more languages : Extend tools to support JavaScript, Go, Java
  2. Integrate with GitHub : Automatic PR reviews
  3. Implement caching : Reduce latency for common patterns
  4. Add specialized agents : Security scanning, performance analysis
  5. Enable A/B testing : Compare different models and prompts
  6. Export metrics : Send traces to specialized observability platforms

Ключевые выводы

  1. Start simple, iterate fast : Seven lines to production in manageable steps
  2. Tools over prompts : Real AST analysis beats "please check for bugs"
  3. State management matters : Constants pattern prevents typo bugs
  4. Loops need exit conditions : Always set max iterations and escalation
  5. Deploy with automation : deploy.sh handles all the complexity
  6. Observability is non-negotiable : You can't improve what you can't measure

Ресурсы для продолжения обучения

Ваше путешествие продолжается

You've built more than a code review assistant—you've mastered the patterns for building any production AI agent:
✅ Complex workflows with multiple specialized agents
✅ Real tool integration for genuine capabilities
✅ Production deployment with proper observability
✅ State management for maintainable systems

These patterns scale from simple assistants to complex autonomous systems. The foundation you've built here will serve you well as you tackle increasingly sophisticated agent architectures.

Welcome to production AI agent development. Your code review assistant is just the beginning.