{"record":{"id":"ffc830e6a9b5a659","repo":"Budibase/budibase","slug":"workspace-context-is-required-ffc830","errorCode":null,"errorMessage":"Workspace context is required","messagePattern":"Workspace context is required","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/server/src/sdk/workspace/ai/agents/agentRuntime.ts","lineNumber":415,"sourceCode":"    sessionId,\n    span,\n    agentId\n  )\n  const routingDecision = await selectOperationForRun({\n    agent,\n    sessionId,\n    latestQuestion,\n    operationId,\n    llm,\n  })\n  const toolSecurityEnabled = await features.isEnabled(\n    FeatureFlag.AI_AGENT_TOOL_SECURITY\n  )\n  let executionContext: AgentExecutionContext | undefined\n  if (routingDecision.operation && requester) {\n    const workspaceId = context.getWorkspaceId()\n    if (!workspaceId) {\n      throw new Error(\"Workspace context is required\")\n    }\n    executionContext = {\n      tenantId: context.getTenantId(),\n      workspaceId,\n      agentId,\n      operationId: routingDecision.operation.id,\n      conversationId: sessionId,\n      requester,\n    }\n  }\n  const promptAndTools = await buildPromptAndTools(\n    agent,\n    routingDecision.operation,\n    {\n      ...buildPromptOptions,\n      fallbackPromptInstructions:\n        routingDecision.action === \"summarize_operations\"\n          ? buildOperationsSummaryPrompt(getLiveOperations(agent))","sourceCodeStart":397,"sourceCodeEnd":433,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/server/src/sdk/workspace/ai/agents/agentRuntime.ts#L397-L433","documentation":"prepareAgentRunContext builds an AgentExecutionContext (tenantId, workspaceId, agentId, operationId) when tool-level security routing has selected an operation and there is a requester. Since the execution context is what tools use to scope permissions, a missing workspace id makes it impossible to build a valid context, so the runtime throws immediately.","triggerScenarios":"Invoking the agent runtime with a routingDecision that has an operation and a non-null requester while context.getWorkspaceId() is undefined — e.g. an agent run triggered outside a workspace-scoped request (script, job, or un-scoped API call), or workspace context middleware not applied to the route.","commonSituations":"Hitting the agent endpoint without workspace scoping in the URL/headers; calling runtime functions from tests without context setup; running an agent via a background worker that forgot to restore workspace context; tenant/workspace resolution failing upstream (invalid app id).","solutions":["Run the agent within a workspace context (context.withWorkspace or the standard workspace-scoped route/middleware)","Check how the run was triggered; only workspace-scoped requests can execute secured operations","In tests, initialize workspace context before calling prepareAgentRunContext / the chat run","Verify workspace resolution upstream (app/workspace id parsing) if the request does include a workspace identifier"],"exampleFix":"// before\nconst run = await prepareAgentChatRun(params)\n// after\nawait context.doInWorkspaceContext(appId, async () => {\n  const run = await prepareAgentChatRun(params)\n})","handlingStrategy":"validation","validationCode":"import context from \"@budibase/backend-core/context\"\nif (!context.getWorkspaceId()) {\n  throw new Error(\"Agent runs require a workspace-scoped request\")\n}","typeGuard":null,"tryCatchPattern":"try {\n  await prepareAgentChatRun(params)\n} catch (err) {\n  if (err.message === \"Workspace context is required\") {\n    // re-invoke within a workspace context\n  } else { throw err }\n}","preventionTips":["Only trigger agent runs from workspace-scoped routes or wrapped contexts","Verify workspace context middleware covers the agent endpoints","In workers/jobs, restore workspace context before running agents"],"tags":["context","workspace","security"],"backgroundTag":"missing-workspace-context","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}