Skip to content

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

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.

Copilot AI changed the title [WIP] Fix case-sensitivity issue in CosmosChatMessageStore methods Fix case-sensitive property mismatch in CosmosChatHistoryProvider queries Jan 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

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

2 participants