Files
moreminimore-chat/lib/captain_response_dequeued_logger.rb
Aakash Bakhle a4eae9710a test: Add focused Captain response lifecycle logs (#15364)
## What changed

This pull request adds three focused log emitters for Captain V2
response jobs:

* `job_dequeued` when Sidekiq fetches the job from Redis
* `job_skipped` when the conversation is not Pending at the first job
guard
* `response_discarded` when a newer customer message exists before model
generation

The dequeue middleware identifies V2 jobs by the triggering message ID
in the third serialized Active Job argument. It does not log Captain V1
or other Sidekiq jobs.

## Why

Recent production incidents have an enqueue record but no later job
record. Active Job `Performing` and `Performed` logs are now available
temporarily, but they do not show whether Sidekiq fetched a job before a
worker disappeared.

The dequeue log closes that gap. The two application logs explain the
early exits that otherwise produce no model trace.

The production root cause remains unresolved. This pull request adds
evidence for the next occurrence and does not change Captain response
behavior.

## Log volume

A Captain V2 response job adds one dequeue line. The other two lines
occur only on an early status skip or a pre-generation burst discard.
Existing Active Job, Langfuse, completion, failure, handoff, and usage
logs cover later stages.

## Validation

* Ruby syntax checks passed for the four implementation files.
* RuboCop found no offenses in the four implementation files.
* No new specs were added because this is temporary diagnostic logging
with no response behavior change.
2026-08-09 09:01:03 +05:30

27 lines
838 B
Ruby

# Records the Sidekiq fetch boundary for Captain response jobs without logging every job.
class CaptainResponseDequeuedLogger
JOB_CLASS = 'Captain::Conversation::ResponseBuilderJob'.freeze
def call(_worker, job, queue)
log_dequeued(job, queue) if captain_v2_response_job?(job)
yield
end
private
def log_dequeued(job, queue)
active_job_id = job.dig('args', 0, 'job_id')
responding_to_message_id = job.dig('args', 0, 'arguments', 2)
Sidekiq.logger.info(
"[CAPTAIN][ResponseLifecycle] event=job_dequeued active_job_id=#{active_job_id} provider_job_id=#{job['jid']} " \
"responding_to_message_id=#{responding_to_message_id} queue=#{queue}"
)
end
def captain_v2_response_job?(job)
return false unless job['wrapped'] == JOB_CLASS
!job.dig('args', 0, 'arguments', 2).nil?
end
end