From ab875a158f5f5a4b30ad9e9fb9bfb78305021d97 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 28 Sep 2026 20:12:15 +0000 Subject: [PATCH] Download history again when a device loses it but keeps its cursor (6.9.1) The sync cursor and the visited history live under separate storage keys. When a userscript manager lost only the history, the device kept asking the worker for rows after its cursor and never got the lost ones back. The history now carries an id that the sync config records alongside the cursor; a history without the matching id pulls from cursor 0. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BGAYvSDCqQt1R7AerUwSeT --- CHANGELOG.md | 5 ++ README.md | 2 +- javstore-full-layout-cleanup.user.js | 69 ++++++++++++++++++++--- tests/sync.test.mjs | 82 ++++++++++++++++++++++++++++ worker/src/worker.mjs | 2 +- 5 files changed, 150 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0273b9..969b190 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## 6.9.1 + +- Fix: a device whose userscript manager lost its visited history but kept its sync settings stayed short of history for good. The panel showed a handful of visits and "synced just now" while the worker still held hundreds. A device remembers how far it has downloaded from the worker, and that bookmark is stored apart from the history. When only the history went, the device kept asking for what was new since the bookmark, so nothing it had lost was ever sent back. The history now carries an id, and the bookmark records which history it belongs to. A history that has lost it (wiped, unreadable, or saved by an older version) is downloaded in full on the next sync, with a "Restored N visited URLs" note, and syncs pick up from the bookmark again after that. Devices updating from 6.9.0 download everything once. Visits recorded since the loss are kept and still go up. +- Change: the worker's version moves to 6.9.1 with the script. The worker itself is unchanged. + ## 6.9.0 - Change: a worker behind the userscript is now flagged on the page itself with a **⚠ Redeploy worker** button next to the JavStore controls, instead of only in the Cloud sync panel, where it was easy to miss. Tapping it shows both versions and points to the worker's Cloudflare build. The panel still lists the worker version. diff --git a/README.md b/README.md index 3ec55c7..ffba530 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Userscript-manager storage is local to one device and AdGuard has been known to Once it is deployed, open the `JVS` panel → **Cloud sync**, enter the worker URL and the access token you generated, tick the box, and save. Repeat on every device with the same URL and token. - Each device syncs on page load, when its tab regains focus, a few seconds after a visit, on the interval you choose, and whenever you press **Sync now**. The panel's status line reports the last sync or the reason the last one failed. -- Devices exchange only what changed: each remembers the sequence number it last saw and the moment it last pushed, so an idle page load costs about 150 bytes each way rather than the whole history. The first sync on a device transfers everything. +- Devices exchange only what changed: each remembers the sequence number it last saw and the moment it last pushed, so an idle page load costs about 150 bytes each way rather than the whole history. The first sync on a device transfers everything, and so does the next sync on a device whose userscript manager lost its history but kept the sync settings: the history carries an id that the saved position is tied to, so a history without it is downloaded again rather than left behind. - Tabs on one device share a history but not a sync. Before deciding what to send, a sync merges what the device has on disk, so a visit recorded in one tab is not passed over by another tab's sync; and an entry that turns up stamped before the last push—a click replayed from the previous page, a restored backup—pulls the mark back to itself rather than being left behind it. - The worker applies the same rules as the local merge—newest timestamp wins per URL, tombstones and the clear/retention horizons outrank stale entries—so a device that has been offline for a week cannot overwrite what the others recorded, and clearing history on one device clears it everywhere instead of being undone. A device that pushes a stale entry is handed the winning one back. - Because a Durable Object is single-threaded, each sync's read-merge-write is serialized and strongly consistent: two devices syncing in the same second queue behind one another rather than racing. diff --git a/javstore-full-layout-cleanup.user.js b/javstore-full-layout-cleanup.user.js index 86b7284..4feaaea 100644 --- a/javstore-full-layout-cleanup.user.js +++ b/javstore-full-layout-cleanup.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name JavStore Full Layout Cleanup - No Sidebars + Mosaic Overlay // @namespace http://tampermonkey.net/ -// @version 6.9.0 +// @version 6.9.1 // @description Clean up JavStore's layout, filter keyword-matched thumbnails, and track visited items with private, configurable controls. // @homepageURL https://github.com/jitdor/userscript-javstore // @supportURL https://github.com/jitdor/userscript-javstore/issues @@ -29,7 +29,7 @@ (function () { 'use strict'; - const SCRIPT_VERSION = '6.9.0'; + const SCRIPT_VERSION = '6.9.1'; // The same address as @downloadURL: opening it hands the newest release to the // userscript manager, which offers to install it. const INSTALL_URL = 'https://github.com/jitdor/userscript-javstore/releases/latest/download/javstore-full-layout-cleanup.user.js'; @@ -101,6 +101,8 @@ cursor: 0, pushedAt: 0, remoteKey: '', + // Which copy of the history the cursor was recorded against; see `historyId`. + historyId: '', }); const VALID_MODES = new Set(['tint', 'blur', 'hide']); @@ -114,6 +116,14 @@ let tombstones = new Map(); let resetAt = 0; let prunedBefore = 0; + // Names this device's stored history, and travels inside it. The sync cursor says + // "this device already holds every row up to here", which is only true of the history + // it was recorded against. The two live under separate storage keys, and a userscript + // manager can lose one and keep the other: then the device goes on asking only for + // rows after its cursor, and the history it lost is never offered back. The sync + // configuration records the id the cursor belongs to, so a history that no longer + // carries it—wiped, unreadable, or written before ids existed—is downloaded in full. + let historyId = ''; let settingsUpdatedAt = 0; // When each setting was last changed on purpose, keyed by setting name. Settings merge // field by field on these, so changing one setting on one device does not carry that @@ -629,6 +639,7 @@ settingTimes: { ...settingTimes }, resetAt, prunedBefore, + ...(historyId ? { historyId } : {}), settings: { ...settings }, visited: Object.fromEntries(visited), overrides: Object.fromEntries(overrides), @@ -799,7 +810,9 @@ try { const lowerings = journalLowerings; const snapshot = await readStorageSnapshot(); - mergeStoredState(combineSnapshot(snapshot)); + const combined = combineSnapshot(snapshot); + adoptHistoryId(combined); + mergeStoredState(combined); notePushBacklog(); pruneVisited(); const payload = serializeState(); @@ -1207,9 +1220,27 @@ cursor: clamp(candidate.cursor, 0, Number.MAX_SAFE_INTEGER, 0), pushedAt: parseTimestamp(candidate.pushedAt), remoteKey: String(candidate.remoteKey || '').slice(0, 500), + historyId: parseHistoryId(candidate.historyId), }; } + function parseHistoryId(value) { + return typeof value === 'string' ? value.slice(0, 100) : ''; + } + + function makeHistoryId() { + if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') return crypto.randomUUID(); + return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`; + } + + // Only ever taken from this device's own storage, never from the worker. What storage + // holds wins over what the tab remembers, so a tab opened before the id was made + // cannot write the history back without it. + function adoptHistoryId(stored) { + const id = parseHistoryId(stored?.historyId); + if (id) historyId = id; + } + function syncConfigured() { return syncConfig.enabled && Boolean(syncConfig.endpoint); } @@ -1459,13 +1490,21 @@ // everything older than now is upstream, a visit another tab recorded is // stranded on this device for good. So the stored document is merged in first: // what this sync is about to declare pushed, it has now actually seen. - applyStoredState(await readStoredState()); + const stored = await readStoredState(); + adoptHistoryId(stored); + applyStoredState(stored); pruneVisited(); // The cursor and the high-water mark only mean anything against the worker they // were recorded from, so pointing the panel at a different one starts over. const sameRemote = syncConfig.remoteKey === syncConfig.endpoint; - let cursor = sameRemote ? syncConfig.cursor : 0; + // And the cursor only means anything against the history it was recorded into. + // A history that has lost it pulls everything again; the worker settles each row + // on its timestamp, so rows the device still holds cost bandwidth and nothing else. + const sameHistory = Boolean(historyId) && syncConfig.historyId === historyId; + const recovering = sameRemote && syncConfig.cursor > 0 && !sameHistory; + const heldBefore = visited.size; + let cursor = sameRemote && sameHistory ? syncConfig.cursor : 0; const pushedAt = sameRemote ? syncConfig.pushedAt : 0; let outgoing = localChangesSince(pushedAt); @@ -1522,8 +1561,12 @@ // it advanced first and the state write were then interrupted by a navigation or // rejected outright, the next sync would ask only for rows after that cursor and // the merged ones would never be offered again. + // A history without an id gets one now, and it has to be in storage before the + // cursor is recorded against it, for the same reason as the rows above. + const naming = !legacy && !historyId; + if (naming) historyId = makeHistoryId(); applyingRemoteState = true; - const saved = (changed || absorbed) ? await persistState() : true; + const saved = (changed || absorbed || naming) ? await persistState() : true; applyingRemoteState = false; if (!saved) throw new Error('the merged history could not be saved'); @@ -1544,11 +1587,16 @@ cursor: legacy ? 0 : cursor, pushedAt: legacy ? 0 : Math.min(horizon, unsent || horizon, arrived), remoteKey: legacy ? '' : syncConfig.endpoint, + historyId: legacy ? '' : historyId, }); lastRemoteSyncAt = Date.now(); lastRemoteError = ''; - if (manual) { + const restored = visited.size - heldBefore; + if (recovering && restored > 0) { + console.info(`[JVS] Local history had lost its place in the sync; ${restored} visits were downloaded again.`); + showToast(`Restored ${restored} visited URLs from the worker.`); + } else if (manual) { // Both directions, because a device that is quietly failing to push looks // exactly like one with nothing to push when only the pull is reported. const report = []; @@ -2214,6 +2262,7 @@ cursor: syncConfig.cursor, pushedAt: syncConfig.pushedAt, remoteKey: syncConfig.remoteKey, + historyId: syncConfig.historyId, enabled: elements.syncEnabled.checked, endpoint: elements.syncEndpoint.value, // An empty box means "keep the token already stored"—it is deliberately never @@ -2650,6 +2699,7 @@ } function reloadRemoteState(newValue) { + adoptHistoryId(newValue); if (applyStoredState(newValue)) showToast('Settings synchronized from another tab.'); } @@ -2658,7 +2708,9 @@ // on a snapshot that other tabs have since moved past. async function refreshFromStorage() { if (document.visibilityState === 'hidden') return; - applyStoredState(await readStoredState()); + const stored = await readStoredState(); + adoptHistoryId(stored); + applyStoredState(stored); } function onReady() { @@ -2741,6 +2793,7 @@ tombstones = parseTimestamps(initialState?.tombstones); resetAt = parseTimestamp(initialState?.resetAt); prunedBefore = parseTimestamp(initialState?.prunedBefore); + historyId = parseHistoryId(initialState?.historyId); lastKnownUpdatedAt = parseTimestamp(initialState?.updatedAt); settingsUpdatedAt = parseTimestamp(initialState?.settingsUpdatedAt) || lastKnownUpdatedAt; settingTimes = documentSettingTimes(initialState); diff --git a/tests/sync.test.mjs b/tests/sync.test.mjs index 63dfccc..8f67a13 100644 --- a/tests/sync.test.mjs +++ b/tests/sync.test.mjs @@ -442,6 +442,88 @@ test('pointing the panel at a different worker starts over', async () => { assert.deepEqual(second.visited(), ['https://javstore.net/a.html']); }); +// What a userscript manager leaves behind when it loses the history but keeps the sync +// settings: the cursor still says every row up to it is already on this device. +function loseHistory(store) { + store.data.delete('javstore_cleanup_state_v2'); + store.journals().forEach(key => store.data.delete(key)); +} + +test('a device that lost its history but kept its cursor downloads it again', async () => { + const remote = makeRemote(); + const store = makeStore(); + const tab = await openDevice(remote, { store }); + CARDS.forEach(([href]) => clickCard(tab.window, href)); + await settle(); + await syncNow(tab); + assert.equal(remote.visited().length, 3); + assert.ok(store.read('javstore_sync_config_v1').cursor > 0); + closeTabs(); + + loseHistory(store); + const reopened = await openTab(store, { html: listing(), remote }); + await waitFor(() => visitedUrls(store).length === 3); + assert.ok(card(reopened.window, '/b.html').classList.contains('jvs-visited')); + assert.equal( + store.read('javstore_sync_config_v1').historyId, + store.state().historyId, + 'the cursor is recorded against the history that now holds the rows', + ); +}); + +test('a visit recorded after the history was lost does not hide what is missing', async () => { + const remote = makeRemote(); + const store = makeStore(); + const tab = await openDevice(remote, { store }); + clickCard(tab.window, '/a.html'); + clickCard(tab.window, '/b.html'); + await settle(); + await syncNow(tab); + closeTabs(); + + // Sync is off for this load, so the new visit is the only history the device has + // when it next talks to the worker—the state the device in the report was left in. + loseHistory(store); + const config = store.read('javstore_sync_config_v1'); + store.write('javstore_sync_config_v1', { ...config, enabled: false }); + const offline = await openTab(store, { html: listing(), remote }); + clickCard(offline.window, '/c.html'); + await settle(); + assert.deepEqual(visitedUrls(store), ['https://javstore.net/c.html']); + closeTabs(); + + store.write('javstore_sync_config_v1', { ...store.read('javstore_sync_config_v1'), enabled: true }); + await openTab(store, { html: listing(), remote }); + await waitFor(() => visitedUrls(store).length === 3); + assert.equal(remote.visited().length, 3); +}); + +test('a device synced by an older version downloads everything once', async () => { + const remote = makeRemote(); + const first = await openDevice(remote); + CARDS.forEach(([href]) => clickCard(first.window, href)); + await settle(); + await syncNow(first); + + // An older version's device: a cursor past every row, no history id on either side, + // and only one of the visits still in its history. + const seq = remote.rows().at(-1).seq; + const store = makeStore(); + enableSync(store, remote, { cursor: seq, pushedAt: Date.now(), remoteKey: remote.endpoint }); + store.write('javstore_cleanup_state_v2', { + version: 2, + updatedAt: Date.now(), + visited: { 'https://javstore.net/a.html': Date.now() - 1000 }, + }); + const tab = await openTab(store, { html: listing(), remote }); + await waitFor(() => visitedUrls(store).length === 3); + assert.equal(remote.requests.at(-1).body.cursor, 0); + + // Once the history carries the id, the next sync goes back to asking for what is new. + await syncNow(tab); + assert.equal(remote.requests.at(-1).body.cursor, seq); +}); + test('only what changed since the last push goes up', async () => { const remote = makeRemote(); const tab = await openDevice(remote); diff --git a/worker/src/worker.mjs b/worker/src/worker.mjs index beeb5b7..630325f 100644 --- a/worker/src/worker.mjs +++ b/worker/src/worker.mjs @@ -17,7 +17,7 @@ const STORAGE_VERSION = 3; // Reported in every answer so the userscript can show which worker it is talking to. It // moves in step with the userscript's own version; a test holds the two together. -export const WORKER_VERSION = '6.9.0'; +export const WORKER_VERSION = '6.9.1'; const OBJECT_NAME = 'default'; const MAX_BODY_BYTES = 8 * 1024 * 1024; const MAX_PAGE_ROWS = 2000;