Skip to content

.NET: fix CA1873 in GitHubCopilotAgent by using LoggerMessage source generator - #1

Draft
alliscode wants to merge 1 commit into
mainfrom
bentho/fix-ca1873-github-copilot-logger
Draft

alliscode wants to merge 1 commit into
mainfrom
bentho/fix-ca1873-github-copilot-logger

Conversation

@alliscode

Copy link
Copy Markdown
Owner

Motivation & Context

The release build was failing with CA1873 analyzer errors in GitHubCopilotAgent.cs:

error CA1873: Evaluation of this argument may be expensive and unnecessary if logging is disabled

The violation was caused by string.Join(", ", approvalRequiredToolNames) being passed as an argument to logger.LogWarning(). This string concatenation is evaluated eagerly at the call site, even when the Warning log level is disabled — exactly the scenario CA1873 guards against.

Description & Review Guide

  • What are the major changes?

    • Added GitHubCopilotAgentLogMessages.cs with a [LoggerMessage]-generated extension method LogApprovalGatingSkippedDueToCustomHook, following the established pattern used throughout the codebase (e.g. ChatClientAgentLogMessages.cs).
    • Replaced the direct logger.LogWarning(...) call in GitHubCopilotAgent.ConfigureApprovalHook with the new source-generated method, wrapped in an if (logger.IsEnabled(LogLevel.Warning)) guard so the expensive string.Join is only evaluated when the warning level is actually active.
  • What is the impact of these changes?
    The CA1873 build error is resolved across all target frameworks (net8.0, net9.0, net10.0). No behavioral change — the log message content and level are identical.

  • What do you want reviewers to focus on?
    The IsEnabled guard at the call site is necessary in addition to the [LoggerMessage] method because the source-generated method's internal IsEnabled check does not prevent eager argument evaluation at the call site. CA1873 fires on the argument expression itself.

Related Issue

Fixes #

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change.

…ce generator

Replace the direct logger.LogWarning() call (which eagerly evaluates
string.Join()) with a [LoggerMessage]-generated extension method in
GitHubCopilotAgentLogMessages.cs.

Fixes build error:
  GitHubCopilotAgent.cs(580,13): error CA1873: Evaluation of this argument
  may be expensive and unnecessary if logging is disabled

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@alliscode
alliscode force-pushed the bentho/fix-ca1873-github-copilot-logger branch from ef12b58 to 725e729 Compare June 30, 2026 02:05
alliscode pushed a commit that referenced this pull request Sep 30, 2026
…osoft#8201) (microsoft#8284)

* Python: add public TypedDict for AgentExecutor checkpoint state (microsoft#8201)

Expose AgentExecutorCheckpointState / AgentSessionCheckpointState, validate restore field types, and cover partial/malformed/forward-compatible payloads.

* fix(checkpoint): address microsoft#8284 review on session TypedDict ownership (#1)

- Define AgentSessionDict on AgentSession (to_dict return) so the shape is
  not duplicated in AgentExecutor; keep AgentSessionCheckpointState as alias.
- Include ServiceSessionId mapping in service_session_id.
- Type on_checkpoint_restore as AgentExecutorCheckpointState only.
- Sync root stub exports (__init__.pyi) with runtime __all__.

* fix(checkpoint): validate AgentExecutor restore element types (microsoft#8284)

Enforce Message/Content element types, string pending-request keys, and
required agent_session.session_id so malformed checkpoints fail at restore.

* fix(checkpoint): validate AgentSessionDict fields on restore

- Validate agent_session.state / service_session_id types
- Raise WorkflowCheckpointException when session restore fails
- Drop unshipped AgentSessionCheckpointState alias; use AgentSessionDict

* fix(checkpoint): keep session/executor checkpoint payloads as dict

TypedDict schemas remain public for documentation and validation, but to_dict and on_checkpoint_save/restore stay dict[str, Any] so subclasses, samples, and pyright stay compatible.

* fix(checkpoint): keep AgentSessionDict optional keys under postponed annotations

Use a required base plus total=False subclass, stop exporting AgentSessionDict from the public package root, and tighten AgentExecutor restore validation with walrus checks.

---------

Co-authored-by: minelhi <3417378192@qq.com>
Co-authored-by: LI <2484593937@qq.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

1 participant