{"record":{"id":"7b9412a24df46bea","repo":"mastra-ai/mastra","slug":"cannot-build-stream-options-without-a-current-thre","errorCode":null,"errorMessage":"Cannot build stream options without a current thread","messagePattern":"Cannot build stream options without a current thread","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/agent-controller/agent-controller.ts","lineNumber":1893,"sourceCode":"      const category = this.getToolCategory({ toolName });\n      return !category || !deniedCategories.has(category);\n    });\n  }\n\n  private async buildAgentMessageStreamOptions({\n    session,\n    requestContext: requestContextInput,\n    tracingContext,\n    tracingOptions,\n  }: {\n    session: Session<TState>;\n    requestContext?: RequestContext;\n    tracingContext?: TracingContext;\n    tracingOptions?: TracingOptions;\n  }): Promise<Record<string, unknown>> {\n    const runThreadId = session.thread.getId();\n    if (!runThreadId) {\n      throw new Error('Cannot build stream options without a current thread');\n    }\n\n    session.run.clearAbortRequested();\n    // Reconcile the in-memory model selection with the persisted per-mode model\n    // before snapshotting it into the request context. In multiplayer\n    // deployments another process (or a freshly-created Session for an existing\n    // thread) may have persisted a different model; the per-instance cache would\n    // otherwise run with a stale selection. No-op in the single-player TUI.\n    await session.model.syncFromPersisted({ modeId: session.mode.get() });\n    const requestContext = await this.buildRequestContext(session, requestContextInput);\n    // Resolve mode-aware instructions at call time so the agent's own\n    // instructions are never mutated by the harness.\n    // When mode/harness instructions exist, combine them with the agent's\n    // own instructions so dynamic instructions (e.g. AGENTS.md, project\n    // context) aren't lost — the agent treats options.instructions as a\n    // full override.\n    let callTimeInstructions: string | undefined;\n    if (this.config.agent) {","sourceCodeStart":1875,"sourceCodeEnd":1911,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/agent-controller/agent-controller.ts#L1875-L1911","documentation":"Building stream options needs a currently selected thread on the Session. session.thread.getId() returned undefined, meaning no thread was set on the session, so the controller cannot snapshot thread context for the stream and throws.","triggerScenarios":"Invoking a run/stream-building API with a Session that was created without a thread (no overrides.threadId and no default thread created/set), typically a stateless session reused for a thread-scoped call.","commonSituations":"Reusing a session constructed in stateless mode for thread-scoped streaming; a createSession code path that skipped thread setup (e.g. thread lookup failed silently upstream); clearing the thread between calls.","solutions":["Set a thread on the session first: session.thread.set({ threadId }) or create the session with overrides.threadId","Create the session via a path that initializes a thread (e.g. with memory configured) before streaming","Check upstream logic that may unset or never set the thread id"],"exampleFix":"// before\nconst session = await controller.createSessionForResource({}); // no thread\nawait controller.stream({ session });\n// after\nconst session = await controller.createSessionForResource({ overrides: { threadId } });\nawait controller.stream({ session });","handlingStrategy":"validation","validationCode":"if (!session.thread.getId()) throw new Error('Set a thread on the session before streaming');","typeGuard":null,"tryCatchPattern":"try {\n  return await controller.stream({ session });\n} catch (e) {\n  if (e instanceof Error && e.message.includes('without a current thread')) {\n    session.thread.set({ threadId: await ensureThread(session) });\n    return controller.stream({ session });\n  }\n  throw e;\n}","preventionTips":["Always create thread-scoped sessions (overrides.threadId) for streaming calls","Do not reuse stateless sessions for thread-scoped APIs","Add a session invariant helper that asserts a thread exists before run/stream calls"],"tags":["session","thread","streaming"],"backgroundTag":"missing-thread-context","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}