@@ -296,8 +296,11 @@ function _job_logs_newer!(
296296 isnothing (buffer. _stream) || return nothing
297297 # If there are existing logs in the buffer then we may not have to fetch anything because
298298 # we have enough logs already in the buffer.
299- if ! isnothing (count) && ! isempty (buffer. _logs) &&
299+ if (
300+ ! isnothing (count) &&
301+ ! isempty (buffer. _logs) &&
300302 buffer. _active_range. stop + count <= length (buffer. _logs)
303+ )
301304 _job_logs_update_active_range! (buffer; stop= buffer. _active_range. stop + count)
302305 return nothing
303306 end
@@ -334,8 +337,21 @@ function _job_logs_newer!(
334337 _job_logs_update_active_range! (buffer; start= 1 , stop= updated_stop)
335338 return nothing
336339 end
340+
337341 # Finally, assuming we do have some logs, but not enough, we keep fetching new logs
338342 # until we don't find any more, find the last message, or have enough.
343+ #
344+ # Before we start the loop though, let's update the active range _once_, in case we
345+ # exit the next loop early due to lack of new messages.
346+ _job_logs_update_active_range! (buffer; stop= length (buffer. _logs))
347+ if count != = nothing
348+ # The case where we have enough logs in the buffer is already handled
349+ # This initializes `count` for the case where we're still missing some logs
350+ count -= length (buffer. _logs)
351+ end
352+
353+ # At this point, the active range is set to the end of the buffer, but we still
354+ # need to fetch more messages (if available).
339355 while true
340356 reference_log = last (buffer. _logs)
341357 start_time = _log_legacy_datetime_to_ms (reference_log. timestamp)
0 commit comments