Skip to content
This repository was archived by the owner on Jul 7, 2026. It is now read-only.

Improves chat service logging and error handling - #55

Merged
magicxor merged 10 commits into
stagingfrom
feature/add-log-messages
Mar 23, 2026
Merged

Improves chat service logging and error handling#55
magicxor merged 10 commits into
stagingfrom
feature/add-log-messages

Conversation

@magicxor

Copy link
Copy Markdown
Owner

Enhances the application's resilience and diagnosability for chat services:

  • Adds an informational log message during service registration to clearly indicate which primary and fallback chat models are configured.
  • Expands the types of ApiException error codes (502, 503, 504) that trigger a fallback to an alternative chat model, making the system more robust against various service unavailability issues.
  • Improves error logging by including the specific ErrorCode in warnings when the primary chat model fails and a fallback is attempted or when no fallback is configured.

Copilot AI review requested due to automatic review settings March 23, 2026 15:18
@magicxor magicxor self-assigned this Mar 23, 2026
@magicxor magicxor added the enhancement New feature or request label Mar 23, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 improves observability and resilience of the chat flow by enhancing startup logging of configured chat models and broadening the set of transient API failures that trigger fallback from the primary to the secondary model.

Changes:

  • Add an informational log during ChatService construction to report configured primary and fallback model names.
  • Expand fallback-triggering ApiException status codes to include 502/503/504 and include the ErrorCode in warning logs.
  • Add explicit warning + rethrow behavior when transient failures occur but no fallback model is configured.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
Pacos/Services/ChatCommandHandlers/MentionHandler.cs Broaden fallback conditions and improve warning logs to include ErrorCode and “no fallback configured” path.
Pacos/Program.cs Log configured primary/fallback model names when ChatService is created via DI.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Pacos/Program.cs Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 23, 2026 15:22
@magicxor
magicxor merged commit 868f853 into staging Mar 23, 2026
4 checks passed
@magicxor
magicxor deleted the feature/add-log-messages branch March 23, 2026 15:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +69 to 70
catch (ApiException ex) when (ex.ErrorCode is 502 or 503 or 504)
{

Copilot AI Mar 23, 2026

Copy link

Choose a reason for hiding this comment

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

The transient/unavailable error code list (502/503/504) is now duplicated between the retry policy and this catch filter. To avoid the two drifting over time, consider extracting a single helper (e.g., IsModelUnavailable(ApiException)) or a shared static readonly int[]/pattern so both the retry handler and fallback switch use the same source of truth.

Copilot uses AI. Check for mistakes.
_logger.LogWarning(ex, "Primary chat model unavailable after retry, switching to fallback model");
if (!_chatService.HasFallback)
{
_logger.LogWarning(ex, "Primary chat model returned {ErrorCode} but no fallback model is configured", ex.ErrorCode);

Copilot AI Mar 23, 2026

Copy link

Choose a reason for hiding this comment

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

When no fallback is configured, this method logs a warning and then rethrows; the caller (HandleMentionAsync) also logs the same exception as an error. This can produce duplicated stack traces/noisy logs for the same failure. Consider either: (1) removing this warning in the no-fallback path and letting the caller handle logging, or (2) having the caller suppress/log differently for ApiException when this warning is emitted.

Suggested change
_logger.LogWarning(ex, "Primary chat model returned {ErrorCode} but no fallback model is configured", ex.ErrorCode);

Copilot uses AI. Check for mistakes.
Comment thread Pacos/Program.cs
Comment on lines +125 to 128
logger.LogInformation("Chat models configured — Primary: {PrimaryModel}, Fallback: {FallbackModel}",
options.ChatModel,
string.IsNullOrWhiteSpace(options.ChatModelFallback) ? "(none)" : options.ChatModelFallback);
return new ChatService(

Copilot AI Mar 23, 2026

Copy link

Choose a reason for hiding this comment

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

This log is inside the AddSingleton factory, so it runs when ChatService is first resolved (singleton creation), not when the service is registered. If the intent is a guaranteed startup log regardless of whether ChatService is ever resolved, consider moving this to host startup (e.g., after building the host, or via an IHostedService that logs the configured models once).

Copilot uses AI. Check for mistakes.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants