diff --git a/browser/data-browser/src/handlers/errorHandler.test.ts b/browser/data-browser/src/handlers/errorHandler.test.ts index 7b9bf32630..2afa26d75a 100644 --- a/browser/data-browser/src/handlers/errorHandler.test.ts +++ b/browser/data-browser/src/handlers/errorHandler.test.ts @@ -7,6 +7,7 @@ vi.mock('react-hot-toast', () => ({ })); vi.mock('../helpers/loggingHandlers', () => ({ handleErrorBugsnag: () => undefined, + reportStoreError: () => undefined, })); const { errorHandler } = await import('./errorHandler'); diff --git a/browser/data-browser/src/handlers/errorHandler.ts b/browser/data-browser/src/handlers/errorHandler.ts index 36a3209df4..67a1e34eaa 100644 --- a/browser/data-browser/src/handlers/errorHandler.ts +++ b/browser/data-browser/src/handlers/errorHandler.ts @@ -1,5 +1,8 @@ import toast from 'react-hot-toast'; -import { handleErrorBugsnag } from '../helpers/loggingHandlers'; +import { + handleErrorBugsnag, + reportStoreError, +} from '../helpers/loggingHandlers'; /** * Coerces whatever was thrown into an Error. @@ -39,6 +42,7 @@ export const errorHandler = (thrown: unknown) => { console.error(e); handleErrorBugsnag(e); + reportStoreError(e); toast.error(e.message); }; diff --git a/browser/data-browser/src/helpers/loggingHandlers.tsx b/browser/data-browser/src/helpers/loggingHandlers.tsx index b94a2626e5..96e5f078ee 100644 --- a/browser/data-browser/src/helpers/loggingHandlers.tsx +++ b/browser/data-browser/src/helpers/loggingHandlers.tsx @@ -4,14 +4,21 @@ import BugsnagPluginReact, { BugsnagErrorBoundary, } from '@bugsnag/plugin-react'; +import * as Sentry from '@sentry/react'; + import { isDev } from '../config'; export function handleErrorBugsnag(e: Error): void { - if (!isDev) { + if (!isDev()) { Bugsnag.notify(e); } } +/** No-op unless Sentry was initialised (a DSN is configured and not in dev). */ +export function reportStoreError(e: Error): void { + Sentry.captureException(e, { tags: { source: 'store' } }); +} + export function initBugsnag(apiKey: string): BugsnagErrorBoundary { Bugsnag.start({ apiKey, diff --git a/browser/data-browser/src/helpers/managed/cloudSync.setup.test.ts b/browser/data-browser/src/helpers/managed/cloudSync.setup.test.ts index e45a2b49a3..fd387d76bd 100644 --- a/browser/data-browser/src/helpers/managed/cloudSync.setup.test.ts +++ b/browser/data-browser/src/helpers/managed/cloudSync.setup.test.ts @@ -24,6 +24,7 @@ function setup(local = true) { getResource: vi.fn(async () => ({})), waitForServerConnected: vi.fn(async () => true), promoteLocalDrive: vi.fn(async () => {}), + syncDriveToServerAndVerify: vi.fn(async () => {}), getSyncStatus: vi.fn(() => ({ serverConnected: true })), }; const setServer = vi.fn(); @@ -78,14 +79,28 @@ describe('Cloud Server setup', () => { return true; }); await enableCloudSyncForDrive(args); - expect(store.promoteLocalDrive).toHaveBeenCalledWith(drive); + expect(store.syncDriveToServerAndVerify).toHaveBeenCalledWith(drive); + }); + + it('also syncs and verifies a drive that was already synced elsewhere', async () => { + const { args, store } = setup(false); + await enableCloudSyncForDrive(args); + expect(store.syncDriveToServerAndVerify).toHaveBeenCalledWith(drive); + }); + + it('does not report success when the node never receives the drive', async () => { + const { args, store } = setup(); + store.syncDriveToServerAndVerify.mockRejectedValue( + new Error('The server is still missing this workspace root.'), + ); + await expect(enableCloudSyncForDrive(args)).rejects.toThrow(/missing/); }); it('leaves a local drive unpromoted when connection fails', async () => { const { args, store } = setup(); store.waitForServerConnected.mockResolvedValue(false); await expect(enableCloudSyncForDrive(args)).rejects.toThrow(/Timed out/); - expect(store.promoteLocalDrive).not.toHaveBeenCalled(); + expect(store.syncDriveToServerAndVerify).not.toHaveBeenCalled(); }); it('does not claim success or promote against an unrelated server without placement', async () => { diff --git a/browser/data-browser/src/helpers/managed/cloudSync.ts b/browser/data-browser/src/helpers/managed/cloudSync.ts index 1a4907fd45..a1b3f17425 100644 --- a/browser/data-browser/src/helpers/managed/cloudSync.ts +++ b/browser/data-browser/src/helpers/managed/cloudSync.ts @@ -280,14 +280,14 @@ export async function enableCloudSyncForDrive(params: { ); } - await promoteLocalOnly(); - - async function promoteLocalOnly() { - // Agent first: the drive's commits are signed by it, and a node that can - // resolve the signer before the drive arrives has nothing to defer. - if (agentWasLocalOnly) await store.promoteLocalDrive(agentSubject); - if (wasLocalOnly) await store.promoteLocalDrive(drive); - } + // Agent first: the drive's commits are signed by it, and a node that can + // resolve the signer before the drive arrives has nothing to defer. + if (agentWasLocalOnly) await store.promoteLocalDrive(agentSubject); + + // Also for a drive that was already synced elsewhere: switching servers + // copies nothing, and reporting success without this left the node without + // the drive root, so every later commit was refused as "Parent not found". + await store.syncDriveToServerAndVerify(drive); return { ok: true, httpOrigin, replicated: false }; } diff --git a/browser/data-browser/src/routes/SyncRoute.tsx b/browser/data-browser/src/routes/SyncRoute.tsx index 3e3875fd34..3723da80f0 100644 --- a/browser/data-browser/src/routes/SyncRoute.tsx +++ b/browser/data-browser/src/routes/SyncRoute.tsx @@ -906,11 +906,27 @@ function SyncPage() { } }; - const onFocus = () => void refreshAccount(); + let lastRefresh = 0; + + const throttled = () => { + // Window focus fires constantly; the account rarely changes under us. + if (Date.now() - lastRefresh < 30_000) return; + lastRefresh = Date.now(); + void refreshAccount(); + }; + + const onFocus = () => throttled(); + + const onAgentChanged = () => { + lastRefresh = Date.now(); + void refreshAccount(); + }; + + lastRefresh = Date.now(); void refreshAccount(); window.addEventListener('focus', onFocus); - const unsubscribe = store.on(StoreEvents.AgentChanged, onFocus); + const unsubscribe = store.on(StoreEvents.AgentChanged, onAgentChanged); return () => { cancelled = true; @@ -932,7 +948,10 @@ function SyncPage() { const poll = () => fetchManagedInfo(serverUrl).then(info => { - if (!cancelled) setManagedInfo(info); + if (!cancelled) + setManagedInfo(prev => + JSON.stringify(prev) === JSON.stringify(info) ? prev : info, + ); }); void poll(); @@ -1063,13 +1082,16 @@ function SyncPage() { .catch(() => {}); }, []); + const cloudSyncAvailable = isCloudSyncAvailable(managedInfo); + const hasManagedAccount = managedAccount !== null; + // Does the active drive already have a Cloud Server enrollment? Drives the // Cloud Server CTA below. Skips entirely when no control plane is // reachable (pure self-hosted), so the CTA never shows there. useEffect(() => { const drive = status.drive; - if (!drive || !isCloudSyncAvailable(managedInfo)) { + if (!drive || !cloudSyncAvailable) { setCloudEnrollment(null); return; @@ -1088,7 +1110,7 @@ function SyncPage() { return () => { cancelled = true; }; - }, [status.drive, status.serverUrl, managedInfo, managedAccount]); + }, [status.drive, status.serverUrl, cloudSyncAvailable, hasManagedAccount]); useEffect(() => { const refresh = () => setStatus(store.getSyncStatus()); diff --git a/browser/lib/src/local-outbox.test.ts b/browser/lib/src/local-outbox.test.ts index d75841f37b..01dedca3e8 100644 --- a/browser/lib/src/local-outbox.test.ts +++ b/browser/lib/src/local-outbox.test.ts @@ -472,6 +472,19 @@ describe('a commit naming a class the server lacks', () => { }); }); +describe('a commit whose parent the server lacks', () => { + const MESSAGE = + 'Parent of atomic:kfyvO_ (atomic:5JhFDF6v) not found: Resource not found. ' + + 'DID Resource atomic:5JhFDF6v not found locally'; + + it('blocks but is not terminal, so the edit is kept and not re-sent', ({ + expect, + }) => { + expect(isUnrecoverableCommitError(MESSAGE, undefined)).toBe(true); + expect(isTerminalCommitError(MESSAGE, undefined)).toBe(false); + }); +}); + describe('a stale Loro write rejected by the causality guard', () => { const MESSAGE = "Commit's Loro update produced no state changes — its writes were silently dropped by LWW against stored state."; diff --git a/browser/lib/src/local-outbox.ts b/browser/lib/src/local-outbox.ts index ab74268694..46025bb2ff 100644 --- a/browser/lib/src/local-outbox.ts +++ b/browser/lib/src/local-outbox.ts @@ -283,6 +283,15 @@ export function isUnrecoverableCommitErrorMessage(message: string): boolean { return true; } + // Server emits: "Parent of () not found: ..." + // (`resources.rs` get_parent). The node does not hold the commit's parent, + // e.g. a drive that was switched to a node without its root ever being + // copied. Every retry fails identically, so park the entry (keep the edit) + // instead of spinning; a resync of the drive re-arms it. + if (/Parent of .+ not found/.test(message)) { + return true; + } + return false; } diff --git a/browser/lib/src/store.ts b/browser/lib/src/store.ts index 02937502d7..5696d6181d 100644 --- a/browser/lib/src/store.ts +++ b/browser/lib/src/store.ts @@ -1227,6 +1227,80 @@ export class Store { await ws?.resyncDrive(normalized); } + /** + * Reconcile `drive` with the connected server and wait until that server's + * inventory covers everything held locally (the drive root included). + * Unlike `promoteLocalDrive`, which only kicks the reconcile off, this + * resolves once the server provably has the drive, so a caller can report + * "connected" honestly. Applies to a drive that was already synced + * elsewhere too: switching servers alone never copies anything. + * Throws when the server does not catch up within `timeoutMs`. + */ + public async syncDriveToServerAndVerify( + drive: string, + timeoutMs = 60_000, + ): Promise { + const normalized = this.normalizeSubject(drive); + const open = (candidate: WSClient | undefined) => + candidate?.readyState === WebSocket.OPEN ? candidate : undefined; + const ws = + open(this.getDefaultWebSocket()) ?? + open(this.getWebSocketForSubject(normalized)); + const db = this.getClientDb(); + + if (!ws || !db) { + throw new AtomicError( + 'Connect to a server before syncing this workspace.', + ErrorType.Server, + ); + } + + this.unregisterLocalOnlyDrive(normalized); + this.subscribeWebSocket(normalized); + + const deadline = Date.now() + timeoutMs; + let lastProblem = 'The server has not received this workspace yet.'; + + while (Date.now() < deadline) { + await ws.resyncDrive(normalized); + await db.flush(); + + try { + const remote = await ws.driveInventory(drive, ''); + const local = await db.getVersionVectorsForDrive(drive); + const remoteBySubject = new Map(remote.map(i => [i.subject, i.vv])); + + if (!remoteBySubject.has(drive)) { + lastProblem = 'The server is still missing this workspace root.'; + } else { + const missing = Object.entries(local).filter(([subject, vv]) => { + const have = remoteBySubject.get(subject); + + return ( + !have || + Object.entries(vv).some( + ([peer, counter]) => (have[peer] ?? 0) < counter, + ) + ); + }); + + if (missing.length === 0) return; + + lastProblem = `The server is still missing ${missing.length} item(s).`; + } + } catch (e) { + lastProblem = e instanceof Error ? e.message : String(e); + } + + await new Promise(resolve => setTimeout(resolve, 2_000)); + } + + throw new AtomicError( + `Cloud Server did not finish receiving this workspace. ${lastProblem}`, + ErrorType.Server, + ); + } + /** * Whether a subject belongs to a local-only drive. Resolution mirrors * the server's rights check: the resource's stable `drive` propval diff --git a/browser/react/src/useFileObjectUrl.ts b/browser/react/src/useFileObjectUrl.ts index fc9968d125..1e9b04c7ac 100644 --- a/browser/react/src/useFileObjectUrl.ts +++ b/browser/react/src/useFileObjectUrl.ts @@ -9,6 +9,39 @@ import { useStore } from './hooks.js'; const BLOB = 'https://atomicdata.dev/properties/blob'; +// Object URLs are cached per (database, blob) so remounting an avatar-style +// image is instant instead of re-reading the blob and re-decoding a fresh URL +// each visit. Bounded, evicting (and revoking) the least recently used. +const MAX_CACHED_URLS = 64; +const urlCache = new Map(); + +function cachedUrl(db: unknown, blobDid: string): string | undefined { + const hit = urlCache.get(blobDid); + + if (!hit || hit.db !== db) return undefined; + + urlCache.delete(blobDid); + urlCache.set(blobDid, hit); + + return hit.url; +} + +function cacheUrl(db: unknown, blobDid: string, url: string): void { + const previous = urlCache.get(blobDid); + + if (previous) URL.revokeObjectURL(previous.url); + urlCache.delete(blobDid); + urlCache.set(blobDid, { db, url }); + + if (urlCache.size > MAX_CACHED_URLS) { + const oldest = urlCache.keys().next().value as string; + const evicted = urlCache.get(oldest); + + if (evicted) URL.revokeObjectURL(evicted.url); + urlCache.delete(oldest); + } +} + /** * Returns a `blob:` object URL for the file's bytes when they are available * locally in the WASM clientDb (e.g. just-uploaded files, or anything cached @@ -36,9 +69,10 @@ export function useFileObjectUrl( useEffect(() => { if (!blobDid || !isBlobSubject(blobDid) || !clientDb) return; - let revoked: string | undefined; let cancelled = false; + if (cachedUrl(clientDb, blobDid)) return; + (async () => { try { const hashHex = blobHashHex(blobDid); @@ -47,11 +81,13 @@ export function useFileObjectUrl( const bytes = await clientDb.getBlob(hash); if (cancelled) return; - if (bytes) { - revoked = URL.createObjectURL(new Blob([bytes as BlobPart])); - } + const url = bytes + ? URL.createObjectURL(new Blob([bytes as BlobPart])) + : undefined; + + if (url) cacheUrl(clientDb, blobDid, url); - setResolved({ blobDid, clientDb, url: revoked }); + setResolved({ blobDid, clientDb, url }); } catch { if (!cancelled) setResolved({ blobDid, clientDb }); } @@ -59,12 +95,15 @@ export function useFileObjectUrl( return () => { cancelled = true; - if (revoked) URL.revokeObjectURL(revoked); }; }, [blobDid, clientDb]); if (!blobDid || !isBlobSubject(blobDid) || !clientDb) return fallbackUrl; + const cached = cachedUrl(clientDb, blobDid); + + if (cached) return cached; + // A result for the previous resource/database must never leak into this render. if (resolved?.blobDid !== blobDid || resolved.clientDb !== clientDb) { return undefined;