toeverything/AFFiNE · error

Failed to get surface block

Error message

Failed to get surface block

What it means

Guard inside extractEdgelessSelected: after adding a synthetic affine:surface block to a temporary AI extraction doc, getSurfaceBlock still returned null, meaning the surface block could not be retrieved from the scratch store — an internal invariant failure of the edgeless extraction pipeline.

Source

Thrown at packages/frontend/core/src/blocksuite/ai/utils/extract.ts:96

    const collection = new WorkspaceImpl({
      id: 'AI_EXTRACT',
      rootDoc: new YDoc({ guid: 'AI_EXTRACT' }),
    });
    collection.meta.initialize();

    let needSnapshot = false;
    let needMarkdown = false;
    try {
      const fragmentDoc = collection.createDoc();
      const fragment = fragmentDoc.getStore();
      fragmentDoc.load();

      const rootId = fragment.addBlock('affine:page');
      fragment.addBlock('affine:surface', {}, rootId);
      const noteId = fragment.addBlock('affine:note', {}, rootId);
      const surface = getSurfaceBlock(fragment);
      if (!surface) {
        throw new Error('Failed to get surface block');
      }

      for (const element of selectedElements) {
        if (element instanceof NoteBlockModel) {
          needMarkdown = true;
          for (const child of element.children) {
            const props = getBlockProps(child);
            fragment.addBlock(child.flavour, props, noteId);
          }
        } else if (isAttachment(element)) {
          const { name, sourceId } = element.props;
          if (name && sourceId) {
            attachments.push({ name, sourceId });
          }
        } else if (isImage(element)) {
          const { sourceId } = element.props;
          if (sourceId) {
            const blob = await host.store.blobSync.get(sourceId);

View on GitHub (pinned to b4c8548c09)

Solutions

  1. Verify the surface block exists in the doc before extraction.
  2. Guard the call with a presence check for the surface block.
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown while extracting selected edgeless elements when the temporary fragment document's affine:surface block cannot be retrieved via getSurfaceBlock.

Common situations: Occurs when running an AI action on selected canvas elements and the scratch document fails to initialize its surface block. Retry the action or reselect the elements.


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