{"record":{"id":"9273efe29fda15dd","repo":"Budibase/budibase","slug":"file-does-not-belong-to-this-agent","errorCode":null,"errorMessage":"File does not belong to this agent","messagePattern":"File does not belong to this agent","errorType":"http","errorClass":"HTTPError","httpStatus":404,"severity":"error","filePath":"packages/server/src/sdk/workspace/ai/rag/files.ts","lineNumber":379,"sourceCode":"  filename: string\n  mimetype?: string\n  size?: number\n  buffer: Buffer\n  uploadedBy: string\n}\n\nexport const deleteFileForAgent = async (\n  agentId: string,\n  fileId: string\n): Promise<void> => {\n  const file = await knowledgeBaseSdk.getKnowledgeBaseFileOrThrow(fileId)\n  const fileKnowledgeBaseId = file.knowledgeBaseId\n  if (!fileKnowledgeBaseId) {\n    throw new HTTPError(\"Invalid knowledge base file id\", 400)\n  }\n  const knowledgeBaseIds = await getKnowledgeBaseIdsForAgent(agentId)\n  if (!knowledgeBaseIds.includes(fileKnowledgeBaseId)) {\n    throw new HTTPError(\"File does not belong to this agent\", 404)\n  }\n\n  const knowledgeBase = await knowledgeBaseSdk.find(fileKnowledgeBaseId)\n  if (!knowledgeBase) {\n    throw new HTTPError(\"Agent file storage not found\", 404)\n  }\n  await knowledgeBaseSdk.removeKnowledgeBaseFile(knowledgeBase, file)\n}\n\nexport const getFileUrlForAgent = async (\n  agentId: string,\n  fileId: string\n): Promise<string> => {\n  const file = await knowledgeBaseSdk.getKnowledgeBaseFileOrThrow(fileId)\n  const fileKnowledgeBaseId = file.knowledgeBaseId\n  if (!fileKnowledgeBaseId) {\n    throw new HTTPError(\"Invalid knowledge base file id\", 400)\n  }","sourceCodeStart":361,"sourceCodeEnd":397,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/server/src/sdk/workspace/ai/rag/files.ts#L361-L397","documentation":"Thrown by deleteFileForAgent when the file's knowledgeBaseId is not among the knowledge bases linked to the given agent. This is an authorization/tenancy guard preventing an agent from deleting files it does not own.","triggerScenarios":"Calling deleteFileForAgent(agentId, fileId) where fileId belongs to a knowledge base attached to a different agent or not attached to any agent.","commonSituations":"Passing a fileId cached from a previous agent configuration, mixing up IDs between agents, or the knowledge base was detached from the agent after the ID was obtained.","solutions":["Verify the knowledge base containing the file is actually attached to this agent's operation config","Re-fetch the agent and its current file list to get valid fileIds","Use the agentId that actually owns the knowledge base"],"exampleFix":"// before\nawait deleteFileForAgent(wrongAgentId, fileId) // 404\n// after\nconst agent = await agentsSdk.getOrThrow(agentId)\nconst kbIds = agent.operations?.flatMap(op => (op.knowledgeSources || []).map(s => s.knowledgeBaseId)) || []\n// ensure fileId resolves to a knowledge base in kbIds before deleting\nawait deleteFileForAgent(agentId, fileId)","handlingStrategy":"validation","validationCode":"const agent = await agentsSdk.getOrThrow(agentId)\nconst kbIds = (agent.operations || []).flatMap(op => (op.knowledgeSources || []).map(s => s.knowledgeBaseId))\nif (!kbIds.length) throw new Error(`Agent ${agentId} owns no knowledge bases`)","typeGuard":"const agentOwnsFile = async (agentId: string, fileId: string): Promise<boolean> => {\n  const file = await knowledgeBaseSdk.getKnowledgeBaseFileOrThrow(fileId)\n  const kbIds = await getKnowledgeBaseIdsForAgent(agentId)\n  return !!file.knowledgeBaseId && kbIds.includes(file.knowledgeBaseId)\n}","tryCatchPattern":"try {\n  await deleteFileForAgent(agentId, fileId)\n} catch (err) {\n  if (err instanceof HTTPError && err.status === 404) {\n    // file not owned by this agent - refresh file list before retrying\n  } else throw err\n}","preventionTips":["Always source fileIds from a fresh fetch of the owning agent","Never reuse fileIds across agents or workspaces","Refresh UI file lists after any agent configuration change"],"tags":["authorization","knowledge-base","ownership"],"backgroundTag":"file-not-owned-by-agent","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}