Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 3, 2025

Motivation and Context

Addresses review feedback from #2505: #2505 (comment)

The ContextualFunctionProvider was adding failed invocation messages to its context history, polluting the conversation state with messages from unsuccessful operations.

Description

Modified InvokedAsync to check context.InvokeException and use an early return pattern when invocations fail:

public override ValueTask InvokedAsync(InvokedContext context, CancellationToken cancellationToken = default)
{
    Throw.IfNull(context);

    // Don't add messages to the recent messages queue if the invocation failed
    if (context.InvokeException is not null)
    {
        return default;
    }

    // Add the request and response messages...
    
    return default;
}

Implementation Details

  • Uses early return pattern to check for exceptions before processing messages
  • Skips adding request and response messages to the recent messages queue when InvokeException is not null
  • Maintains cleaner code structure with reduced nesting

Test Coverage

  • Added InvokedAsync_ShouldNotAddMessages_WhenExceptionIsPresent_Async to verify messages from failed invocations are excluded from context history

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Address feedback on message handling in ContextualFunctionProvider port Skip adding messages to context when invocation fails with exception Dec 3, 2025
Copilot AI requested a review from westey-m December 3, 2025 11:01
Copilot finished work on behalf of westey-m December 3, 2025 11:01
// Add the request and response messages to the recent messages queue
foreach (var message in context.RequestMessages)
// Only add messages to the recent messages queue if the invocation succeeded
if (context.InvokeException is null)
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot Instead of having an if around all the code, just have an if that checks if InvokeException is not null and return if so.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored to use early return pattern in commit 70b0e76.

Copy link
Contributor

@westey-m westey-m left a comment

Choose a reason for hiding this comment

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

Please fix requested item

@github-actions github-actions bot changed the title Skip adding messages to context when invocation fails with exception .NET: Skip adding messages to context when invocation fails with exception Dec 3, 2025
Copilot AI changed the title .NET: Skip adding messages to context when invocation fails with exception Skip adding messages to context when invocation fails with exception Dec 3, 2025
Copilot AI requested a review from westey-m December 3, 2025 11:30
Copilot finished work on behalf of westey-m December 3, 2025 11:30
Copy link
Contributor Author

Copilot AI commented Dec 3, 2025

Please fix requested item

Could you clarify what needs to be fixed? The early return pattern was implemented in commit 70b0e76 as requested. The current code checks if context.InvokeException is not null and returns early before adding any messages to the queue.

Copilot finished work on behalf of westey-m December 3, 2025 11:32
@westey-m westey-m marked this pull request as ready for review December 3, 2025 11:56
@westey-m westey-m merged commit 9008bb8 into contextual-function-provider-port Dec 3, 2025
4 checks passed
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.

3 participants