{"record":{"id":"a1fc3ead8b6cb847","repo":"Budibase/budibase","slug":"workspaceid-is-required-a1fc3e","errorCode":null,"errorMessage":"workspaceId is required","messagePattern":"workspaceId is required","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/server/src/sdk/workspace/ai/agentRequests/queue.ts","lineNumber":121,"sourceCode":"    role: \"user\" | \"assistant\"\n    content: string\n  }>\n  operation?: {\n    name: string\n    prompt: string\n  }\n  userId: string\n  existingRequestId?: string\n}) {\n  if (!(await features.isEnabled(FeatureFlag.AI_AGENT_ACTIVITY))) {\n    return\n  }\n  if (!context.isProdWorkspace()) {\n    return\n  }\n  const workspaceId = context.getWorkspaceId()\n  if (!workspaceId) {\n    throw new Error(\"workspaceId is required\")\n  }\n\n  await enqueue({\n    workspaceId,\n    agentId,\n    sessionId,\n    latestUserPrompt,\n    recentChatContext,\n    operation,\n    source: determineTrigger(sessionId),\n    userId,\n    existingRequestId,\n  })\n}\n","sourceCodeStart":103,"sourceCodeEnd":136,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/server/src/sdk/workspace/ai/agentRequests/queue.ts#L103-L136","documentation":"enqueueRequestTracking only runs request tracking for production workspaces, and afterwards requires a workspace id from the request context in order to enqueue the tracking job. If context.getWorkspaceId() returns falsy (no workspace/app context on the request) the guard throws this plain Error rather than silently skipping tracking.","triggerScenarios":"Calling enqueueRequestTracking (directly or via startAgentRequestTracking) from a context where isProdWorkspace() is true but getWorkspaceId() returns undefined — i.e. a request scoped to no workspace, or context not initialized (e.g. running outside an HTTP request, background job, script, or tests without setContext).","commonSituations":"Calling the tracking helper in a unit/integration test without wrapping in context.withWorkspace; an automation or cron job lacking workspace context; calling before the context middleware has run; mixing dev/prod workspace checks so the early return no longer applies.","solutions":["Ensure the caller runs inside a workspace-scoped context (e.g. context.withWorkspace / the Koa workspace middleware) before invoking startAgentRequestTracking","Check getWorkspaceId() yourself before calling and skip/short-circuit tracking when absent if tracking is best-effort","In tests, set up the workspace context fixture the same way server request tests do","Confirm isProdWorkspace() and getWorkspaceId() derive from the same context state — a mismatch means context was partially initialized"],"exampleFix":"// before\nawait startAgentRequestTracking({ agentId, sessionId, prompt })\n// after\nawait context.doInWorkspaceContext(workspaceId, () =>\n  startAgentRequestTracking({ agentId, sessionId, prompt })\n)","handlingStrategy":"validation","validationCode":"import context from \"@budibase/backend-core/context\"\nconst workspaceId = context.getWorkspaceId()\nif (!workspaceId) {\n  // skip or defer tracking; do not call enqueueRequestTracking\n}","typeGuard":null,"tryCatchPattern":"try {\n  await startAgentRequestTracking({...})\n} catch (err) {\n  if (err.message === \"workspaceId is required\") {\n    // request ran outside a workspace context; tracking is best-effort\n  } else { throw err }\n}","preventionTips":["Always call tracking helpers inside workspace-scoped context (middleware or context.withWorkspace)","In tests, set up workspace context fixtures before invoking","Remember tracking only applies to prod workspaces; dev workspaces return early"],"tags":["context","workspace","validation"],"backgroundTag":"missing-workspace-context","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}