Skip to content

Commit 24474d6

Browse files
committed
fix(CLI-BH): advance lastTimestamp using max across batch, not just first log
When the newest log in a poll batch lacks timestamp_precise, scanning only newLogs[0] fails to advance lastTimestamp, causing logs with timestamp_precise to be re-displayed on subsequent polls. Use a maxTimestamp() helper that finds the highest value across the entire batch for both initial fetch and poll updates.
1 parent 3177a81 commit 24474d6

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

src/commands/log/list.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -258,23 +258,38 @@ function executeFollowMode<T extends LogLike>(
258258
pendingTimer = setTimeout(poll, pollIntervalMs);
259259
}
260260

261-
function writeNewLogs(newLogs: T[]) {
262-
const newestLog = newLogs[0];
263-
if (newestLog) {
264-
if (!(flags.json || headerPrinted)) {
265-
stdout.write(table ? table.header() : formatLogsHeader());
266-
headerPrinted = true;
261+
/** Find the highest timestamp_precise in a batch, or undefined if none have it. */
262+
function maxTimestamp(logs: T[]): number | undefined {
263+
let max: number | undefined;
264+
for (const l of logs) {
265+
if (l.timestamp_precise !== undefined) {
266+
max =
267+
max === undefined
268+
? l.timestamp_precise
269+
: Math.max(max, l.timestamp_precise);
267270
}
268-
const chronological = [...newLogs].reverse();
269-
writeLogs({
270-
stdout,
271-
logs: chronological,
272-
asJson: flags.json,
273-
table,
274-
includeTrace: config.includeTrace,
275-
});
276-
lastTimestamp = newestLog.timestamp_precise ?? lastTimestamp;
277271
}
272+
return max;
273+
}
274+
275+
function writeNewLogs(newLogs: T[]) {
276+
if (newLogs.length === 0) {
277+
return;
278+
}
279+
280+
if (!(flags.json || headerPrinted)) {
281+
stdout.write(table ? table.header() : formatLogsHeader());
282+
headerPrinted = true;
283+
}
284+
const chronological = [...newLogs].reverse();
285+
writeLogs({
286+
stdout,
287+
logs: chronological,
288+
asJson: flags.json,
289+
table,
290+
includeTrace: config.includeTrace,
291+
});
292+
lastTimestamp = maxTimestamp(newLogs) ?? lastTimestamp;
278293
}
279294

280295
async function poll() {
@@ -317,9 +332,7 @@ function executeFollowMode<T extends LogLike>(
317332
table,
318333
includeTrace: config.includeTrace,
319334
});
320-
if (initialLogs[0]) {
321-
lastTimestamp = initialLogs[0].timestamp_precise ?? lastTimestamp;
322-
}
335+
lastTimestamp = maxTimestamp(initialLogs) ?? lastTimestamp;
323336
config.onInitialLogs?.(initialLogs);
324337
scheduleNextPoll();
325338
})

0 commit comments

Comments
 (0)