{"record":{"id":"96e129035d2324fe","repo":"mastra-ai/mastra","slug":"no-model-available-this-run-started-without-a-con","errorCode":null,"errorMessage":"No model available: this run started without a controller session context, so no model selection could be resolved.","messagePattern":"No model available: this run started without a controller session context, so no model selection could be resolved\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/agents/model.ts","lineNumber":215,"sourceCode":"\n/**\n * Dynamic model function that reads the current model from controller state.\n * This allows runtime model switching via the /models picker.\n */\nexport function getDynamicModel(\n  { requestContext }: { requestContext: RequestContext },\n  settingsPath?: string,\n): ResolvedModel {\n  const agentControllerContext = requestContext.get('controller') as AgentControllerRequestContext<any> | undefined;\n\n  const modelId = agentControllerContext?.session?.modelId;\n  if (!modelId) {\n    // A missing controller context means the run was started without session\n    // request context at all (e.g. a signal delivered to an idle thread) —\n    // \"use /models\" would mislead there, the user's selection was never the\n    // problem.\n    if (!agentControllerContext) {\n      throw new Error(\n        'No model available: this run started without a controller session context, so no model selection could be resolved.',\n      );\n    }\n    throw new Error('No model selected. Use /models to select a model first.');\n  }\n\n  const thinkingLevel = resolveRequestThinkingLevel(agentControllerContext, settingsPath);\n\n  return resolveModel(modelId, { thinkingLevel, remapForCodexOAuth: true, requestContext });\n}\n\n/**\n * Goal judge model resolver for the agent's `goal.judge` config. Resolves the\n * configured goal judge model through mastracode's gateway so provider\n * credentials (stored in auth storage, not just env) are injected — a bare model\n * id handed to core's default model router would fail to find the API key.\n *\n * Returns `undefined` when no judge model is configured, which keeps the goal","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/agents/model.ts#L197-L233","documentation":"getDynamicModel throws this when the resolved modelId is missing AND the run has no agentControllerContext at all — the run started without session request context, so no model selection could ever have been made. The distinct message (vs 'No model selected') clarifies the user's /models choice was never the problem, e.g. a signal delivered to an idle thread.","triggerScenarios":"codeAgent → getDynamicModel finds `!modelId` and `!agentControllerContext` — the run began with no controller session context in request context (e.g. a signal/timer firing on an idle thread with no interactive session attached).","commonSituations":"Triggering a workflow/signal/cron on a thread that was never opened through the interactive session, running agents programmatically without establishing session request context, or replaying stored runs whose context is gone.","solutions":["Start the run through a controller session so agentControllerContext is populated (open the thread / send an initiating request)","Establish the session request context programmatically before invoking the agent","Guard scheduled/signal-driven runs to skip model-requiring steps when no session context exists","Configure a default model at the run level if you need headless runs without a session"],"exampleFix":"// before\nawait thread.signal('run-task'); // idle thread, no session context\n// after\nif (await thread.hasSessionContext()) {\n  await thread.signal('run-task');\n}","handlingStrategy":"validation","validationCode":"if (!agentControllerContext) {\n  throw new Error('run requires a controller session; open a session first');\n}","typeGuard":"const hasControllerContext = (ctx: unknown): ctx is AgentControllerContext =>\n  Boolean(ctx);","tryCatchPattern":"try {\n  await runAgent(task);\n} catch (err) {\n  if ((err as Error).message.includes('without a controller session context')) {\n    console.error('Start the run from an open session or establish request context first');\n  } else throw err;\n}","preventionTips":["Only trigger model-requiring runs from sessions with established request context","Guard signal/cron-driven code paths for missing session context","Establish session context programmatically for headless runs"],"tags":["configuration","request-context","model-resolution"],"backgroundTag":"missing-session-context","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}