Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions browser/data-browser/src/handlers/errorHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ vi.mock('react-hot-toast', () => ({
}));
vi.mock('../helpers/loggingHandlers', () => ({
handleErrorBugsnag: () => undefined,
reportStoreError: () => undefined,
}));

const { errorHandler } = await import('./errorHandler');
Expand Down
6 changes: 5 additions & 1 deletion browser/data-browser/src/handlers/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -39,6 +42,7 @@ export const errorHandler = (thrown: unknown) => {

console.error(e);
handleErrorBugsnag(e);
reportStoreError(e);

toast.error(e.message);
};
9 changes: 8 additions & 1 deletion browser/data-browser/src/helpers/loggingHandlers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 17 additions & 2 deletions browser/data-browser/src/helpers/managed/cloudSync.setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 () => {
Expand Down
16 changes: 8 additions & 8 deletions browser/data-browser/src/helpers/managed/cloudSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
32 changes: 27 additions & 5 deletions browser/data-browser/src/routes/SyncRoute.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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());
Expand Down
13 changes: 13 additions & 0 deletions browser/lib/src/local-outbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down
9 changes: 9 additions & 0 deletions browser/lib/src/local-outbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,15 @@ export function isUnrecoverableCommitErrorMessage(message: string): boolean {
return true;
}

// Server emits: "Parent of <subject> (<parent>) 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;
}

Expand Down
74 changes: 74 additions & 0 deletions browser/lib/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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
Expand Down
51 changes: 45 additions & 6 deletions browser/react/src/useFileObjectUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, { db: unknown; url: string }>();

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
Expand Down Expand Up @@ -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);
Expand All @@ -47,24 +81,29 @@ 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 });
}
})();

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;
Expand Down