From a484fb8f36825c4d0e0305e24bf0132d5ee38454 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 21 Jan 2026 22:07:23 +0000 Subject: [PATCH] improve watch history reliability - Reduced view threshold from 12s to 5s in watchHistoryTelemetry.js - Enabled immediate watch history republishing (0ms delay) for the first attempt in watchHistory.js - Updated watchHistoryService.js to recursively schedule queue flushes if items accumulate during an inflight sync --- js/nostr/watchHistory.js | 26 +++++++++++++++++++------- js/services/watchHistoryTelemetry.js | 2 +- js/watchHistoryService.js | 5 ++++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/js/nostr/watchHistory.js b/js/nostr/watchHistory.js index 6a1bd2d1..04e9ecfa 100644 --- a/js/nostr/watchHistory.js +++ b/js/nostr/watchHistory.js @@ -1101,13 +1101,25 @@ class WatchHistoryManager { if (attempt > WATCH_HISTORY_REPUBLISH_MAX_ATTEMPTS) { return; } - const exponentialDelay = WATCH_HISTORY_REPUBLISH_BASE_DELAY_MS * Math.pow(2, attempt); - const cappedDelay = Math.min(exponentialDelay, WATCH_HISTORY_REPUBLISH_MAX_DELAY_MS); - const jitter = Math.random() * cappedDelay * WATCH_HISTORY_REPUBLISH_JITTER; - const delay = Math.max( - WATCH_HISTORY_REPUBLISH_BASE_DELAY_MS, - Math.floor(cappedDelay + jitter), - ); + + let delay = 0; + if (attempt === 0) { + delay = 10; + } else { + const exponentialDelay = + WATCH_HISTORY_REPUBLISH_BASE_DELAY_MS * Math.pow(2, attempt); + const cappedDelay = Math.min( + exponentialDelay, + WATCH_HISTORY_REPUBLISH_MAX_DELAY_MS, + ); + const jitter = + Math.random() * cappedDelay * WATCH_HISTORY_REPUBLISH_JITTER; + delay = Math.max( + WATCH_HISTORY_REPUBLISH_BASE_DELAY_MS, + Math.floor(cappedDelay + jitter), + ); + } + if (onSchedule) { try { onSchedule({ taskId: key, attempt: attempt + 1, delay }); diff --git a/js/services/watchHistoryTelemetry.js b/js/services/watchHistoryTelemetry.js index 744fd681..2e3807b3 100644 --- a/js/services/watchHistoryTelemetry.js +++ b/js/services/watchHistoryTelemetry.js @@ -5,7 +5,7 @@ import { ingestLocalViewEvent as defaultIngestLocalViewEvent, } from "../viewCounter.js"; -const DEFAULT_VIEW_THRESHOLD_SECONDS = 12; +const DEFAULT_VIEW_THRESHOLD_SECONDS = 5; function normalizeString(value) { return typeof value === "string" ? value.trim() : ""; diff --git a/js/watchHistoryService.js b/js/watchHistoryService.js index dcc99d68..5174a230 100644 --- a/js/watchHistoryService.js +++ b/js/watchHistoryService.js @@ -615,12 +615,15 @@ function scheduleRepublishForQueue(actorKey) { actorKey, queue, keysToClear, - snapshotStartTime + snapshotStartTime, ); await updateFingerprintCache( actorKey, publishResult.items || latestItems, ); + if (queue.items.size > 0) { + scheduleRepublishForQueue(actorKey); + } } else if (!publishResult?.retryable) { // If not retryable, we clear the scheduled flag to allow new attempts later queue.republishScheduled = false;