toeverything/AFFiNE · error

Can not find the current version of the doc.

Error message

Can not find the current version of the doc.

What it means

The second guard in rollbackDoc: after the target history snapshot was found, the CURRENT doc record (getDoc(docId)) must also exist to compute the revert update (generateRevertUpdate(fromSnapshot.bin, toSnapshot.bin) needs both ends). If the live doc is missing, rollback cannot proceed and throws 'Can not find the current version of the doc.'

Solutions

  1. Check getDoc(docId) before offering rollback; if absent, use a restore/create flow (create the doc from the snapshot bin) instead of rollbackDoc.
  2. Refresh/retry after sync completes if the doc exists on the server but not locally.
  3. Handle the error in UI as 'doc no longer exists' with an option to restore it as a new doc from the chosen version.

Example fix

// before
await history.rollbackDoc(docId, entry.timestamp); // throws if live doc deleted

// after
const current = await history.getDoc(docId);
if (!current) {
  await restoreAsNewDoc(entry); // create doc from snapshot bin
} else {
  await history.rollbackDoc(docId, entry.timestamp);
}
Defensive patterns

Strategy: validation

Validate before calling

const current = await history.getDoc(docId);
if (!current) throw new Error('doc missing — restore from snapshot instead of rollback');
await history.rollbackDoc(docId, ts);

Type guard

const docExists = async (docId: string): Promise<boolean> => (await history.getDoc(docId)) !== null;

Try / catch

try { await history.rollbackDoc(docId, ts); } catch (e) { if (e instanceof Error && e.message === 'Can not find the current version of the doc.') { /* offer restore-as-new-doc from snapshot */ } throw e; }

Prevention

When it happens

Trigger: Rolling back a docId whose live record was deleted (or never synced into this storage) while its history records still exist; local-first storage where the current doc hasn't been pulled yet; race where the doc is deleted between listing history and calling rollbackDoc.

Common situations: Doc moved/deleted in another client while a history tab stayed open; partial sync leaving history but not the current doc; deleting a doc and then attempting to restore content via rollback instead of a proper restore-from-history flow.

Related errors


AI-assisted analysis of toeverything/AFFiNE@b4c8548c09 (2026-08-18). Data as JSON: /api/errors/1a8a1abd2d8bbc60. Report an issue: GitHub.

Appendix: source

Thrown at packages/common/nbstore/src/storage/history.ts:85

    docId: string,
    filter?: HistoryFilter
  ): Promise<ListedHistory[]>;
  abstract getHistory(
    docId: string,
    timestamp: Date
  ): Promise<DocRecord | null>;
  abstract deleteHistory(docId: string, timestamp: Date): Promise<void>;

  async rollbackDoc(docId: string, timestamp: Date, editor?: string) {
    const toSnapshot = await this.getHistory(docId, timestamp);
    if (!toSnapshot) {
      throw new Error('Can not find the version to rollback to.');
    }

    const fromSnapshot = await this.getDoc(docId);

    if (!fromSnapshot) {
      throw new Error('Can not find the current version of the doc.');
    }

    const change = this.generateRevertUpdate(fromSnapshot.bin, toSnapshot.bin);
    await this.pushDocUpdate({ docId, bin: change, editor }, 'rollback');
    // force create a new history record after rollback
    await this.createHistory(docId, fromSnapshot);
  }

  // history can only be created upon update pushing.
  protected abstract createHistory(
    docId: string,
    snapshot: DocRecord
  ): Promise<void>;

  protected generateRevertUpdate(
    fromNewerBin: Uint8Array,
    toOlderBin: Uint8Array
  ): Uint8Array {

View on GitHub (pinned to b4c8548c09)