{"record":{"id":"5058e912e45fc6a8","repo":"toeverything/AFFiNE","slug":"failed-to-get-surface-block","errorCode":null,"errorMessage":"Failed to get surface block","messagePattern":"Failed to get surface block","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/frontend/core/src/blocksuite/ai/utils/extract.ts","lineNumber":96,"sourceCode":"    const collection = new WorkspaceImpl({\n      id: 'AI_EXTRACT',\n      rootDoc: new YDoc({ guid: 'AI_EXTRACT' }),\n    });\n    collection.meta.initialize();\n\n    let needSnapshot = false;\n    let needMarkdown = false;\n    try {\n      const fragmentDoc = collection.createDoc();\n      const fragment = fragmentDoc.getStore();\n      fragmentDoc.load();\n\n      const rootId = fragment.addBlock('affine:page');\n      fragment.addBlock('affine:surface', {}, rootId);\n      const noteId = fragment.addBlock('affine:note', {}, rootId);\n      const surface = getSurfaceBlock(fragment);\n      if (!surface) {\n        throw new Error('Failed to get surface block');\n      }\n\n      for (const element of selectedElements) {\n        if (element instanceof NoteBlockModel) {\n          needMarkdown = true;\n          for (const child of element.children) {\n            const props = getBlockProps(child);\n            fragment.addBlock(child.flavour, props, noteId);\n          }\n        } else if (isAttachment(element)) {\n          const { name, sourceId } = element.props;\n          if (name && sourceId) {\n            attachments.push({ name, sourceId });\n          }\n        } else if (isImage(element)) {\n          const { sourceId } = element.props;\n          if (sourceId) {\n            const blob = await host.store.blobSync.get(sourceId);","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/toeverything/AFFiNE/blob/b4c8548c09da21b2898443559a5b846f0ccf5dd8/packages/frontend/core/src/blocksuite/ai/utils/extract.ts#L78-L114","documentation":"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.","triggerScenarios":"Thrown while extracting selected edgeless elements when the temporary fragment document's affine:surface block cannot be retrieved via getSurfaceBlock.","commonSituations":"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.","solutions":["Verify the surface block exists in the doc before extraction.","Guard the call with a presence check for the surface block."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"b4c8548c09da21b2898443559a5b846f0ccf5dd8","analyzedAt":"2026-08-18T21:16:52.546Z","contentChangedAt":"2026-08-18T21:16:52.546Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}