Budibase/budibase · error · Error

workspaceId is required

Error message

workspaceId is required

What it means

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.

Source

Thrown at packages/server/src/sdk/workspace/ai/agentRequests/queue.ts:121

    role: "user" | "assistant"
    content: string
  }>
  operation?: {
    name: string
    prompt: string
  }
  userId: string
  existingRequestId?: string
}) {
  if (!(await features.isEnabled(FeatureFlag.AI_AGENT_ACTIVITY))) {
    return
  }
  if (!context.isProdWorkspace()) {
    return
  }
  const workspaceId = context.getWorkspaceId()
  if (!workspaceId) {
    throw new Error("workspaceId is required")
  }

  await enqueue({
    workspaceId,
    agentId,
    sessionId,
    latestUserPrompt,
    recentChatContext,
    operation,
    source: determineTrigger(sessionId),
    userId,
    existingRequestId,
  })
}

View on GitHub (pinned to a81a902e9a)

Solutions

  1. Ensure the caller runs inside a workspace-scoped context (e.g. context.withWorkspace / the Koa workspace middleware) before invoking startAgentRequestTracking
  2. Check getWorkspaceId() yourself before calling and skip/short-circuit tracking when absent if tracking is best-effort
  3. In tests, set up the workspace context fixture the same way server request tests do
  4. Confirm isProdWorkspace() and getWorkspaceId() derive from the same context state — a mismatch means context was partially initialized

Example fix

// before
await startAgentRequestTracking({ agentId, sessionId, prompt })
// after
await context.doInWorkspaceContext(workspaceId, () =>
  startAgentRequestTracking({ agentId, sessionId, prompt })
)
Defensive patterns

Strategy: validation

Validate before calling

import context from "@budibase/backend-core/context"
const workspaceId = context.getWorkspaceId()
if (!workspaceId) {
  // skip or defer tracking; do not call enqueueRequestTracking
}

Try / catch

try {
  await startAgentRequestTracking({...})
} catch (err) {
  if (err.message === "workspaceId is required") {
    // request ran outside a workspace context; tracking is best-effort
  } else { throw err }
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of Budibase/budibase@a81a902e9a (2026-08-29). Data as JSON: /api/errors/a1fc3ead8b6cb847. Report an issue: GitHub.