Skip to content

.NET: Fix case-sensitive property mismatch in CosmosChatHistoryProvider queries#3485

Merged
westey-m merged 4 commits intomainfrom
copilot/fix-cosmoschatmessage-count-issue
Feb 23, 2026
Merged

.NET: Fix case-sensitive property mismatch in CosmosChatHistoryProvider queries#3485
westey-m merged 4 commits intomainfrom
copilot/fix-cosmoschatmessage-count-issue

Conversation

Copy link
Contributor

Copilot AI commented Jan 28, 2026

Motivation and Context

SQL queries in CosmosChatHistoryProvider used c.Type (uppercase) while the document model defines [JsonProperty("type")] (lowercase). Cosmos DB's case-sensitive queries failed to match any documents, causing GetMessageCountAsync() and ClearMessagesAsync() to always return 0.

Description

Changed queries:

  • GetMessageCountAsync() - line 555: c.Typec.type
  • ClearMessagesAsync() - line 585: c.Typec.type

Added tests:

  • GetMessageCountAsync_WithMessages_ShouldReturnCorrectCountAsync
  • GetMessageCountAsync_WithNoMessages_ShouldReturnZeroAsync
  • ClearMessagesAsync_WithMessages_ShouldDeleteAndReturnCountAsync
  • ClearMessagesAsync_WithNoMessages_ShouldReturnZeroAsync

Test updates:

  • Fixed IDE0090 style violations by using target-typed new expressions (new(...) instead of new ChatMessage(...))
  • Updated tests to work with current API: constructor now uses state factory pattern, methods require AgentSession parameter

Tests follow existing patterns (skip when Cosmos DB Emulator unavailable).

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.
Original prompt

This section details on the original issue you should resolve

<issue_title>.NET: [Bug]: CosmosChatMessageStore methods GetMessageCountAsync and ClearMessagesAsync always return 0 due to case-sensitivity in query</issue_title>
<issue_description>### Description

What happened?
When using CosmosChatMessageStore, calls to GetMessageCountAsync() and ClearMessagesAsync() always return 0, even when there are messages present in the container that can be retrieved via other methods.

What did you expect to happen?
GetMessageCountAsync() should return the actual number of messages for the given conversation, and ClearMessagesAsync() should delete the messages and return the count of removed items.

Steps to reproduce the issue:

  1. Initialize CosmosChatMessageStore with a valid connection string and container.
  2. Add messages to the store (or verify they exist).
  3. Call GetMessageCountAsync().
  4. Observe that it returns 0 regardless of the actual data.

Code Sample

var cosmosProvider = new CosmosChatMessageStore(
    "AccountEndpoint=xxxx;AccountKey=xxxx",
    "my-databaseid",
    "my-container",
    "my-conversationId");

// Retrieval works fine
var messages = await cosmosProvider.InvokingAsync(new ChatMessageStore.InvokingContext([]));

// Bug: Always returns 0
var count = await cosmosProvider.GetMessageCountAsync();

// Bug: No messages are deleted and returns 0
var removedMessagesCount1 = await cosmosProvider.ClearMessagesAsync();

Error Messages / Stack Traces

No exception is thrown; the methods silently fail to match any documents due to an incorrect property name in the underlying Cosmos DB SQL query.

Package Versions

Microsoft.Agents.AI.CosmosNoSql: 1.0.0-preview.260121.1

.NET Version

NET 10

Additional Context

The issue appears to be caused by a case-sensitivity mismatch in the query string.
In CosmosChatMessageStore.cs, the query uses c.Type = @type instead of c.type = @type (lowercase 't').

<agent_instructions>Analyse this error and add your findings to the issue</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 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.

Co-authored-by: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix case-sensitivity issue in CosmosChatMessageStore methods Fix case-sensitive property mismatch in CosmosChatHistoryProvider queries Jan 28, 2026
@github-actions github-actions bot changed the title Fix case-sensitive property mismatch in CosmosChatHistoryProvider queries .NET: Fix case-sensitive property mismatch in CosmosChatHistoryProvider queries Feb 9, 2026
alliscode pushed a commit to alliscode/agent-framework that referenced this pull request Feb 20, 2026
…c and ClearMessagesAsync

The count and delete queries used c.Type (capital T) instead of c.type (lowercase t),
which doesn't match the stored document property name. Cosmos DB property names are
case-sensitive, so these queries would always return 0 results.

Fixes the same issue as microsoft#3485 but applied cleanly to current main.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@westey-m
Copy link
Contributor

@copilot, the build is failing with style issues, please fix.

/app/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs(897,13): error IDE0090: 'new' expression can be simplified [/app/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/Microsoft.Agents.AI.CosmosNoSql.UnitTests.csproj]
/app/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs(898,13): error IDE0090: 'new' expression can be simplified [/app/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/Microsoft.Agents.AI.CosmosNoSql.UnitTests.csproj]
/app/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs(899,13): error IDE0090: 'new' expression can be simplified [/app/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/Microsoft.Agents.AI.CosmosNoSql.UnitTests.csproj]
/app/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs(897,13): error IDE0090: 'new' expression can be simplified [/app/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/Microsoft.Agents.AI.CosmosNoSql.UnitTests.csproj]
/app/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs(898,13): error IDE0090: 'new' expression can be simplified [/app/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/Microsoft.Agents.AI.CosmosNoSql.UnitTests.csproj]
/app/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs(899,13): error IDE0090: 'new' expression can be simplified [/app/dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/Microsoft.Agents.AI.CosmosNoSql.UnitTests.csproj]

Co-authored-by: westey-m <164392973+westey-m@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 23, 2026

@copilot, the build is failing with style issues, please fix.

...

Fixed in 02a65ac. Applied target-typed new expressions and updated tests to work with the new API that was introduced after the merge.

@westey-m westey-m marked this pull request as ready for review February 23, 2026 15:14
Copilot AI review requested due to automatic review settings February 23, 2026 15:14
@westey-m westey-m enabled auto-merge February 23, 2026 15:16
Copy link
Contributor

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

Fixes a Cosmos DB SQL case-sensitivity bug in .NET where queries used c.Type even though the stored JSON field is type, causing GetMessageCountAsync() and ClearMessagesAsync() to return 0 incorrectly.

Changes:

  • Update CosmosChatHistoryProvider SQL queries to use c.type (matching [JsonProperty("type")]).
  • Add Cosmos emulator-backed unit tests covering message counting and clearing scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosChatHistoryProvider.cs Fixes case-sensitive Cosmos SQL queries for count and delete operations by querying c.type.
dotnet/tests/Microsoft.Agents.AI.CosmosNoSql.UnitTests/CosmosChatHistoryProviderTests.cs Adds new emulator-skippable tests validating correct counts and deletes for empty/non-empty conversations.

@westey-m westey-m added this pull request to the merge queue Feb 23, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Feb 23, 2026
github-merge-queue bot pushed a commit that referenced this pull request Feb 23, 2026
…ntAsyn… (#4139)

* Fix case-sensitivity bug in Cosmos DB queries for GetMessageCountAsync and ClearMessagesAsync

The count and delete queries used c.Type (capital T) instead of c.type (lowercase t),
which doesn't match the stored document property name. Cosmos DB property names are
case-sensitive, so these queries would always return 0 results.

Fixes the same issue as #3485 but applied cleanly to current main.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Add tests for GetMessageCountAsync and ClearMessagesAsync

Verifies that the count and clear queries correctly match stored documents
by using the proper lowercase 'type' property name in Cosmos DB queries.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: alliscode <bentho@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@westey-m westey-m added this pull request to the merge queue Feb 23, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Feb 23, 2026
@westey-m westey-m added this pull request to the merge queue Feb 23, 2026
Merged via the queue into main with commit 69eabcd Feb 23, 2026
18 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.

.NET: [Bug]: CosmosChatMessageStore methods GetMessageCountAsync and ClearMessagesAsync always return 0 due to case-sensitivity in query

5 participants