{"record":{"id":"0814eea2066d4345","repo":"unslothai/unsloth","slug":"document-not-found","errorCode":null,"errorMessage":"Document not found","messagePattern":"Document not found","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"studio/backend/routes/rag.py","lineNumber":359,"sourceCode":"            exists = store.get_kb(conn, scope_id) is not None\n        else:\n            owner_conn = _rag_connection()\n            try:\n                exists = store.get_kb(owner_conn, scope_id) is not None\n            finally:\n                owner_conn.close()\n        detail = \"Knowledge base not found\"\n    else:\n        from storage.studio_db import get_chat_project\n        exists = get_chat_project(scope_id) is not None\n        detail = \"Project not found\"\n    if not exists:\n        raise HTTPException(status_code = 404, detail = detail)\n\n\ndef _require_document_owner(conn: sqlite3.Connection, document: dict) -> None:\n    if document.get(\"kb_id\") and store.get_kb(conn, document[\"kb_id\"]) is None:\n        raise HTTPException(status_code = 404, detail = \"Document not found\")\n    if document.get(\"project_id\"):\n        from storage.studio_db import get_chat_project\n        if get_chat_project(document[\"project_id\"]) is None:\n            raise HTTPException(status_code = 404, detail = \"Document not found\")\n\n\ndef _create_linked_folder(scope_type: str, scope_id: str, payload: LinkFolderRequest) -> dict:\n    path, signed_identity = _resolve_linked_folder_path(payload.native_path_lease)\n    try:\n        with folder_sync.scope_lock(_scope_for_owner(scope_type, scope_id)):\n            _require_scope_owner(scope_type, scope_id)\n            folder, job_id = folder_sync.create_folder_with_sync(\n                scope_type = scope_type,\n                scope_id = scope_id,\n                path = path,\n                expected_identity = signed_identity,\n                name = payload.name,\n                auto_sync = payload.auto_sync,","sourceCodeStart":341,"sourceCodeEnd":377,"githubUrl":"https://github.com/unslothai/unsloth/blob/203007d19051dcd2ae33876786d117c99f6b0368/studio/backend/routes/rag.py#L341-L377","documentation":"_require_document_owner() verifies a document still belongs to a live scope: if document.kb_id is set but store.get_kb(conn, kb_id) is None, HTTP 404 'Document not found' is raised. It prevents acting on documents whose owning knowledge base has been deleted (even if the document row lingers).","triggerScenarios":"Requesting a document (download, delete, status) whose kb_id references a knowledge base that no longer exists — typically a KB deleted while the document list was still open in the UI.","commonSituations":"KB deleted in another tab/session after the document list loaded; orphaned document rows from an interrupted KB deletion; stale document ID from a previous workspace.","solutions":["Reload the documents from a live knowledge base and drop references to documents of the deleted one.","If the KB should still exist, check the KB list / deletion job logs (retire_and_delete_kb may have completed asynchronously).","Clean orphaned document rows if the KB deletion was itself interrupted."],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":"const docs = await api.listDocuments(kbId); // 404 here means the KB is gone\nconst live = docs.some(d => d.id === wantedId);\nif (!live) { removeFromUi(wantedId); }","typeGuard":null,"tryCatchPattern":"if (e.status === 404 && e.detail === 'Document not found') { removeDocumentRow(documentId); }","preventionTips":["Drop document references as soon as their owning KB disappears from the list.","After deleting a KB, clear every cached document of its scope.","Consider KB-deleted events to push-invalidate open document panels."],"tags":["http-404","document","knowledge-base","orphan","rag","studio-backend"],"backgroundTag":null,"analyzedSha":"203007d19051dcd2ae33876786d117c99f6b0368","analyzedAt":"2026-08-15T02:48:39.846Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}