{"record":{"id":"a765d720b1bd38b7","repo":"Budibase/budibase","slug":"agent-not-found-a765d7","errorCode":null,"errorMessage":"Agent not found","messagePattern":"Agent not found","errorType":"http","errorClass":"HTTPError","httpStatus":404,"severity":"error","filePath":"packages/server/src/sdk/workspace/ai/agents/crud.ts","lineNumber":453,"sourceCode":"  }\n\n  return resolved\n}\n\nexport async function fetch(): Promise<Agent[]> {\n  const agents = (await fetchRaw()).map(withAgentDefaults)\n  return withCurrentQueryToolReferences(agents)\n}\n\nexport async function getOrThrow(agentId: string | undefined): Promise<Agent> {\n  if (!agentId) {\n    throw new HTTPError(\"agentId is required\", 400)\n  }\n\n  const db = context.getWorkspaceDB()\n  const rawAgent = await db.tryGet<DeprecatedAgent>(agentId)\n  if (!rawAgent) {\n    throw new HTTPError(\"Agent not found\", 404)\n  }\n\n  const agent = withAgentDefaults(rawAgent)\n  const [resolvedAgent] = await withCurrentQueryToolReferences([agent])\n  return resolvedAgent\n}\n\nexport async function create(\n  request: Optional<\n    Omit<Agent, \"_id\" | \"_rev\" | \"createdAt\" | \"updatedAt\" | \"publishedAt\">,\n    \"aiconfig\"\n  >\n): Promise<Agent> {\n  const db = context.getWorkspaceDB()\n  const now = new Date().toISOString()\n\n  await guardName(request.name)\n","sourceCodeStart":435,"sourceCodeEnd":471,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/server/src/sdk/workspace/ai/agents/crud.ts#L435-L471","documentation":"getOrThrow fetches the raw agent document from the workspace DB (db.tryGet) and throws a 404 HTTPError \"Agent not found\" if no document exists for the given agentId. It then applies defaults and resolves current query tool references before returning, guaranteeing callers a fully-hydrated Agent or an error.","triggerScenarios":"Calling getOrThrow (or sdk.ai.agents.agent / getAgentId / sourceAgent / syncState paths) with a syntactically valid but non-existent agentId: agent deleted while a session/config still references it, wrong environment/workspace DB, typo in id, or a stale agentId persisted in another document (e.g. agentRequests, operations) after deletion.","commonSituations":"Agent deleted from the builder while automations or tracking records still hold its id; copy-pasting an agent id from another app/tenant; restoring a DB backup where agent docs were pruned; tests using hard-coded ids that don't exist in the test workspace.","solutions":["Verify the agentId exists in the current workspace DB (query the agents docs) — the id may be stale or from another workspace","If the agent was deleted, clean up references (config, operations, tracking records) or recreate the agent with a new id","Use sdk.ai.agents.fetch() to list valid ids and pick the correct one","Catch the 404 HTTPError and handle gracefully (skip resolution, prompt re-selection of the agent)"],"exampleFix":"// before\nconst agent = await sdk.ai.agents.getOrThrow(session.agentId)\n// after\nlet agent\ntry {\n  agent = await sdk.ai.agents.getOrThrow(session.agentId)\n} catch (err) {\n  if (err instanceof HTTPError && err.status === 404) {\n    // agent was deleted - rebind session to a valid agent\n    agent = await sdk.ai.agents.getOrThrow(defaultAgentId)\n  } else { throw err }\n}","handlingStrategy":"try-catch","validationCode":"const all = await sdk.ai.agents.fetch()\nif (!all.some(a => a._id === agentId)) {\n  // stale id - rebind to an existing agent before getOrThrow\n}","typeGuard":null,"tryCatchPattern":"try {\n  agent = await sdk.ai.agents.getOrThrow(agentId)\n} catch (err) {\n  if (err instanceof HTTPError && err.status === 404) {\n    // agent was deleted or id is from another workspace\n    agent = await sdk.ai.agents.getOrThrow(fallbackAgentId)\n  } else { throw err }\n}","preventionTips":["Clean up stored agentId references when an agent is deleted","Don't hard-code agent ids from other apps/tenants in tests","List agents with fetch() to confirm ids before lookups"],"tags":["not-found","http-404","crud","database"],"backgroundTag":"resource-not-found","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}