{"record":{"id":"7008dc034f40f48a","repo":"mastra-ai/mastra","slug":"no-active-thread-on-this-session","errorCode":null,"errorMessage":"No active thread on this session","messagePattern":"No active thread on this session","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/agent-controller/session.ts","lineNumber":373,"sourceCode":"      throw new Error('SessionThread has not been connected to its session');\n    }\n    return this.#session;\n  }\n\n  /** The active thread id, or null when the session is not bound to a thread. */\n  getId(): string | null {\n    return this.#threadId;\n  }\n\n  /** Whether the session is currently bound to a thread. */\n  isSet(): boolean {\n    return this.#threadId !== null;\n  }\n\n  /** The active thread id, throwing when the session is not bound to a thread. */\n  requireId(): string {\n    if (this.#threadId === null) {\n      throw new Error('No active thread on this session');\n    }\n    return this.#threadId;\n  }\n\n  /** Bind the session to a thread. */\n  set({ threadId }: { threadId: string }): void {\n    this.#threadId = threadId;\n  }\n\n  /** Clear the session's thread binding. */\n  clear(): void {\n    this.#threadId = null;\n  }\n\n  /** Clear the session's thread binding and release its lock when one is held. */\n  async clearAndReleaseLock(): Promise<void> {\n    const threadId = this.#threadId;\n    this.#threadId = null;","sourceCodeStart":355,"sourceCodeEnd":391,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/agent-controller/session.ts#L355-L391","documentation":"requireId() is the accessor for the session's active thread id. The library throws this when the AgentController session has never been bound to a thread (#threadId is null) and code asks for an id that does not exist. It is a deliberate fail-fast guard instead of returning a null id that callers might misuse.","triggerScenarios":"Calling requireId() (directly or via the threadId/oldThreadId/aThreadId/currentThreadId accessors) before any bind/set() call, after an explicit unbind, or on a freshly constructed session with no --thread/--resume equivalent.","commonSituations":"Running the CLI without a resumed or created thread; reading currentThreadId in a hook that fires before thread selection; assuming a session auto-creates a thread when it defers binding until first prompt.","solutions":["Create or resume a thread before reading the id (e.g. call the session bind/set path with a threadId, or select a thread via promptForThreadSelection).","Use the non-throwing getId()/has-thread check and handle the null case instead of requireId().","If a thread should always exist, initialize it during session construction so #threadId is set early."],"exampleFix":"// before\nconst id = session.currentThreadId(); // throws when no thread\n// after\nconst id = session.thread.getId();\nif (id === null) {\n  await session.thread.create({ title: 'New session' });\n}\nconst threadId = session.requireId();","handlingStrategy":"type-guard","validationCode":"if (session.thread.getId() === null) { await session.thread.create({ title: 'New session' }); }","typeGuard":"function hasActiveThread(s: { thread: { getId(): string | null } }): s is typeof s & { thread: { getId(): string } } {\n  return s.thread.getId() !== null;\n}","tryCatchPattern":"let threadId: string;\ntry {\n  threadId = session.requireId();\n} catch (e) {\n  if (e instanceof Error && e.message === 'No active thread on this session') {\n    await session.thread.create({ title: 'New session' });\n    threadId = session.requireId();\n  } else throw e;\n}","preventionTips":["Check getId() for null instead of calling requireId() when absence is possible","Bind/create a thread immediately after session construction","Gate UI actions that need a thread on the has-thread check","Never assume sessions auto-create threads"],"tags":["state","lifecycle","threading"],"backgroundTag":"no-active-thread","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}