Skip to content

Add debug logs for agent interactions#759

Merged
Bryan-Roe merged 4 commits intomainfrom
codex/add-logging-for-agent-interactions
Aug 6, 2025
Merged

Add debug logs for agent interactions#759
Bryan-Roe merged 4 commits intomainfrom
codex/add-logging-for-agent-interactions

Conversation

@Bryan-Roe
Copy link
Collaborator

@Bryan-Roe Bryan-Roe commented Jul 27, 2025

Summary

  • add detailed logging in AgentThread lifecycle events and message handling

Testing

  • ruff format semantic_kernel/agents/agent.py (fails: Failed to parse pyproject.toml)
  • ruff check semantic_kernel/agents/agent.py (fails: Failed to parse pyproject.toml)
  • pytest tests/unit -k "" (fails: Invalid initial character for a key part)

https://chatgpt.com/codex/tasks/task_e_6886a6e605808322b782c2286facece1

Summary by Sourcery

Introduce detailed debug logging for agent thread lifecycle and message handling.

Enhancements:

  • Add debug logs around agent thread creation and deletion events to trace lifecycle changes
  • Add debug logs for new message reception, thread construction, and message notification in agent threads

Copilot AI review requested due to automatic review settings July 27, 2025 22:31
@sourcery-ai
Copy link

sourcery-ai bot commented Jul 27, 2025

Reviewer's Guide

This pull request instrumentally augments the AgentThread lifecycle and message‐handling flows with logger.debug statements at key entry and exit points to enhance runtime visibility.

Sequence diagram for AgentThread lifecycle with debug logging

sequenceDiagram
    participant Caller
    participant AgentThread
    participant Logger
    Caller->>AgentThread: create()
    AgentThread->>Logger: debug("Creating agent thread")
    AgentThread->>AgentThread: _create()
    AgentThread->>Logger: debug("Created agent thread with id %s", self._id)
    Caller->>AgentThread: delete()
    AgentThread->>Logger: debug("Deleting agent thread %s", self._id)
    AgentThread->>AgentThread: _delete()
    AgentThread->>Logger: debug("Deleted agent thread %s", self._id)
Loading

Sequence diagram for AgentThread message handling with debug logging

sequenceDiagram
    participant Caller
    participant AgentThread
    participant Logger
    participant Message
    Caller->>AgentThread: on_new_message(new_message)
    AgentThread->>Logger: debug("Thread %s received new message from role %s", self._id, new_message.role)
    AgentThread->>AgentThread: _on_new_message(new_message)
Loading

Sequence diagram for thread creation and notification with debug logging

sequenceDiagram
    participant Agent
    participant Logger
    participant Thread
    participant Message
    Agent->>Thread: construct_thread()
    Agent->>Logger: debug("Created new thread for agent interaction")
    Agent->>Thread: create()
    Agent->>Logger: debug("Notifying thread %s of new message from role %s", thread.id, new_message.role)
    Agent->>Thread: on_new_message(new_message)
Loading

File-Level Changes

Change Details Files
Enhanced debug logging around thread creation and deletion
  • Log before creating a new agent thread
  • Log after successfully creating a thread with its assigned ID
  • Log before deleting an existing agent thread
  • Log after deleting the thread and clearing its ID
01-core-implementations/python/semantic_kernel/agents/agent.py
Added debug logs in message handling paths
  • Log when a thread receives a new message with its role
  • Log upon ensuring a thread exists before processing messages
  • Log when notifying the thread of a new incoming message
01-core-implementations/python/semantic_kernel/agents/agent.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds comprehensive debug logging to the AgentThread lifecycle and message handling operations to improve observability and debugging capabilities for agent interactions.

  • Added debug logs for thread creation and deletion operations
  • Added debug logs for message handling in both thread and agent contexts
  • Included thread IDs and message roles in log messages for better traceability

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Bryan-Roe - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `01-core-implementations/python/semantic_kernel/agents/agent.py:522` </location>
<code_context>

         if thread is None:
             thread = construct_thread()
+            logger.debug("Created new thread for agent interaction")
             await thread.create()

</code_context>

<issue_to_address>
Log message could include more context for easier tracing.

Consider adding a unique identifier or relevant parameters to the log message to improve traceability, especially in concurrent scenarios.

Suggested implementation:

```python
        if thread is None:
            thread = construct_thread()
            logger.debug(
                "Created new thread for agent interaction: thread_id=%s, params=%r",
                getattr(thread, "id", None),
                getattr(thread, "params", None) if hasattr(thread, "params") else {}
            )
            await thread.create()

```

If the `thread` object does not have an `id` or `params` attribute, replace or supplement these with other relevant identifiers or context available at this point in your codebase.
</issue_to_address>

### Comment 2
<location> `01-core-implementations/python/semantic_kernel/agents/agent.py:542` </location>
<code_context>
         new_message: ChatMessageContent,
     ) -> None:
         """Notify the thread of a new message."""
+        logger.debug(
+            "Notifying thread %s of new message from role %s", thread.id, new_message.role
+        )
         await thread.on_new_message(new_message)
</code_context>

<issue_to_address>
Consider handling the case where thread.id is None in the log.

Explicitly check if thread.id is None and adjust the log message to ensure clarity.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
        logger.debug(
            "Notifying thread %s of new message from role %s", thread.id, new_message.role
        )
=======
        if thread.id is None:
            logger.debug(
                "Notifying thread with no ID of new message from role %s", new_message.role
            )
        else:
            logger.debug(
                "Notifying thread %s of new message from role %s", thread.id, new_message.role
            )
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Bryan-Roe and others added 3 commits August 3, 2025 01:24
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Bryan <74067792+Bryan-Roe@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Signed-off-by: Bryan <74067792+Bryan-Roe@users.noreply.github.com>
Signed-off-by: Bryan <74067792+Bryan-Roe@users.noreply.github.com>
@Bryan-Roe Bryan-Roe merged commit 17a2cf9 into main Aug 6, 2025
7 of 11 checks passed
@Bryan-Roe Bryan-Roe deleted the codex/add-logging-for-agent-interactions branch August 6, 2025 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant