{"record":{"id":"a71ea54c03fda54b","repo":"langgenius/dify","slug":"knowledge-creation-failed-during-policy","errorCode":null,"errorMessage":"Knowledge creation failed during policy","messagePattern":"Knowledge creation failed during policy","errorType":"exception","errorClass":"KnowledgeCreationError","httpStatus":null,"severity":"error","filePath":"web/features/new-rag/create-knowledge-workflow.ts","lineNumber":86,"sourceCode":"\n  try {\n    if (values.visibility === 'all_members') {\n      const policy = await consoleClient.knowledgeFs.getKnowledgeSpacesByIdAccessPolicy({\n        params: { id: created.id },\n      })\n      if (policy.visibility !== values.visibility) {\n        await consoleClient.knowledgeFs.patchKnowledgeSpacesByIdAccessPolicy({\n          body: {\n            expectedRevision: policy.revision,\n            partialMemberSubjectIds: [],\n            visibility: values.visibility,\n          },\n          params: { id: created.id },\n        })\n      }\n    }\n  } catch (error) {\n    throw new KnowledgeCreationError('policy', error, created)\n  }\n\n  return created\n}\n","sourceCodeStart":68,"sourceCodeEnd":91,"githubUrl":"https://github.com/langgenius/dify/blob/ef8544b173fd6cd7a8e71df2cab576e52bebbfbc/web/features/new-rag/create-knowledge-workflow.ts#L68-L91","documentation":"Wrapped KnowledgeCreationError thrown when the post-creation policy step fails. Stage is 'policy' and crucially the already-created knowledge space is attached as createdKnowledge so callers can recover or clean up. The policy step fetches the access policy and, if visibility differs, PATCHes it to the requested value (e.g. 'all_members').","triggerScenarios":"Fires when getKnowledgeSpacesByIdAccessPolicy or patchKnowledgeSpacesByIdAccessPolicy rejects, or when an optimistic-revision conflict occurs (expectedRevision mismatch). Also fires if values.visibility === 'all_members' and either call fails.","commonSituations":"Concurrent policy modification caused an expectedRevision conflict, the user lacks permission to change visibility, or the KnowledgeFS gateway had a transient error after the space was already created. The space exists in createdKnowledge and should not be re-created on retry.","solutions":["On retry, pass { existingKnowledge: error.createdKnowledge } so createKnowledge skips the create step and only re-attempts policy.","If the failure is a revision conflict, re-fetch the policy to obtain a fresh revision before patching.","Check error.originalError.status: 403 -> user cannot set all_members visibility; 409 -> revision conflict, refetch.","Warn the user that the knowledge space was created but visibility was not applied."],"exampleFix":"// before\ncatch (error) {\n  throw new KnowledgeCreationError('policy', error, created)\n}\n\n// after - recover using the created space on retry\ntry {\n  await createKnowledge(values)\n} catch (e) {\n  if (e instanceof KnowledgeCreationError && e.stage === 'policy' && e.createdKnowledge) {\n    await createKnowledge({ ...values, existingKnowledge: e.createdKnowledge })\n  }\n}","handlingStrategy":"retry","validationCode":"// Retry policy step using already-created space\nasync function createKnowledgeWithRecovery(values: CreateKnowledgeValues) {\n  try {\n    return await createKnowledge(values)\n  } catch (e) {\n    if (e instanceof KnowledgeCreationError && e.stage === 'policy' && e.createdKnowledge) {\n      return createKnowledge({ ...values, existingKnowledge: e.createdKnowledge })\n    }\n    throw e\n  }\n}","typeGuard":"function isPolicyStageError(e: unknown): e is KnowledgeCreationError {\n  return e instanceof KnowledgeCreationError && e.stage === 'policy' && !!e.createdKnowledge\n}","tryCatchPattern":"try {\n  await createKnowledge(values)\n} catch (e) {\n  if (e instanceof KnowledgeCreationError && e.stage === 'policy') {\n    notify(`Knowledge '${e.createdKnowledge?.name}' created, but visibility could not be applied.`)\n  }\n}","preventionTips":["On policy-stage retry, always pass existingKnowledge to avoid duplicate space creation.","Re-fetch the access policy for a fresh revision before patching on conflict.","Warn the user that the space exists even if visibility failed.","Idempotently retry only the policy step, not creation."],"tags":["frontend","knowledge","rag","api-error","partial-failure","optimistic-concurrency"],"backgroundTag":null,"analyzedSha":"ef8544b173fd6cd7a8e71df2cab576e52bebbfbc","analyzedAt":"2026-08-12T05:15:17.394Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}