From a674615dffa66d8dee730e7cd92141d5cf98b84d Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 26 Aug 2026 02:39:50 -0700 Subject: [PATCH] refactor(logbook): consolidate batch enqueueing (#129996) Amp-Thread-ID: https://ampcode.com/threads/T-01a03a19-be71-77a7-a886-4c688012c709 Co-authored-by: Amp --- extensions/logbook/src/service.ts | 54 +++++++++++++------------------ 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/extensions/logbook/src/service.ts b/extensions/logbook/src/service.ts index eaa69c4d38c4..b478d8f640d2 100644 --- a/extensions/logbook/src/service.ts +++ b/extensions/logbook/src/service.ts @@ -362,23 +362,10 @@ export class LogbookService { // retries could loop model spend on a persistently failing batch. store.resetErrorBatches(); if (!store.nextPendingBatch()) { - const frames = store.unbatchedActiveFrames(2000); // Force-close the current window so "analyze now" needs no elapsed time. - const selection = selectBatchFrames({ - frames, - windowMs: this.config.analysisIntervalMinutes * 60_000, - nowMs: Date.now(), - force: true, - }); - if (!selection) { + if (!this.enqueueNextBatch(true)) { return { started: false, reason: "no unanalyzed activity captured yet" }; } - store.createBatch({ - day: dayKeyFor(selection.startMs), - startMs: selection.startMs, - endMs: selection.endMs, - frameIds: selection.frameIds, - }); } void this.analysisTick(); return { started: true }; @@ -416,26 +403,31 @@ export class LogbookService { } } - private enqueueElapsedWindow(): void { + private enqueueNextBatch(force = false): boolean { const store = this.requireStore(); + const selection = selectBatchFrames({ + frames: store.unbatchedActiveFrames(2000), + windowMs: this.config.analysisIntervalMinutes * 60_000, + nowMs: Date.now(), + force, + }); + if (!selection) { + return false; + } + store.createBatch({ + day: dayKeyFor(selection.startMs), + startMs: selection.startMs, + endMs: selection.endMs, + frameIds: selection.frameIds, + }); + return true; + } + + private enqueueElapsedWindow(): void { // Windows close on elapsed wall-clock or on a capture gap; both cases are // resolved by selectBatchFrames against the oldest unbatched frame. - while (true) { - const frames = store.unbatchedActiveFrames(2000); - const selection = selectBatchFrames({ - frames, - windowMs: this.config.analysisIntervalMinutes * 60_000, - nowMs: Date.now(), - }); - if (!selection) { - return; - } - store.createBatch({ - day: dayKeyFor(selection.startMs), - startMs: selection.startMs, - endMs: selection.endMs, - frameIds: selection.frameIds, - }); + while (this.enqueueNextBatch()) { + // Continue until all elapsed windows are queued. } }