Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/active_agent/concerns/rescue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ module Rescue
include ActiveSupport::Rescuable

class_methods do
# Handles exceptions raised during GenerationJob execution.
#
# Called by GenerationJob#handle_exception_with_agent_class as a
# class-level fallback when no instance is available to handle the error.
#
# @param exception [Exception] the exception to handle
# @return [void]
def handle_exception(exception)
Rails.logger.error "[#{name}] #{exception.class}: #{exception.message}"
Rails.logger.error exception.backtrace&.first(10)&.join("\n") if exception.backtrace
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

As written, this default handle_exception implementation will swallow the original exception (ActiveJob rescue_from considers it handled), which can cause GenerationJob failures to be silently marked successful and prevent job-level retries/discard logic from running. Consider re-raising after logging (or making the default behavior opt-in) so failures still surface to the job framework.

Suggested change
Rails.logger.error exception.backtrace&.first(10)&.join("\n") if exception.backtrace
Rails.logger.error exception.backtrace&.first(10)&.join("\n") if exception.backtrace
raise exception

Copilot uses AI. Check for mistakes.
end
Comment on lines +16 to +26
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

This change introduces new behavior for background-job exception handling, but there doesn’t appear to be coverage asserting that GenerationJob calls the class-level handler and that the exception is logged/propagated as intended. Please add a test around GenerationJob#handle_exception_with_agent_class (and the default handle_exception) to prevent regressions.

Copilot uses AI. Check for mistakes.

Comment on lines +24 to +27
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

handle_exception logs via Rails.logger, which bypasses the framework’s configurable logger (ActiveAgent::Base.logger / config.logger) and can also raise NameError if ActiveAgent is used without Rails loaded. Prefer logging through the agent/logger accessor (with a safe fallback) so custom logger configuration is respected.

Suggested change
Rails.logger.error "[#{name}] #{exception.class}: #{exception.message}"
Rails.logger.error exception.backtrace&.first(10)&.join("\n") if exception.backtrace
end
agent_logger.error "[#{name}] #{exception.class}: #{exception.message}"
agent_logger.error exception.backtrace&.first(10)&.join("\n") if exception.backtrace
end
private
# Returns the logger to use for class-level exception handling.
#
# Prefers the including class's logger, then ActiveAgent::Base.logger,
# then Rails.logger if available, and finally falls back to a standard
# Ruby Logger to $stderr.
#
# @return [#error] a logger-like object responding to `#error`
def agent_logger
if respond_to?(:logger) && (current_logger = logger)
current_logger
elsif defined?(ActiveAgent::Base) &&
ActiveAgent::Base.respond_to?(:logger) &&
(base_logger = ActiveAgent::Base.logger)
base_logger
elsif defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
Rails.logger
else
require "logger"
@__active_agent_rescue_logger ||= Logger.new($stderr)
end
end

Copilot uses AI. Check for mistakes.
# Finds and instruments the rescue handler for an exception.
#
# @param exception [Exception] the exception to handle
Expand Down
Loading