{"record":{"id":"a2e2153ea6f23fa7","repo":"mastra-ai/mastra","slug":"could-not-create-the-session-reload-the-page-and","errorCode":null,"errorMessage":"Could not create the session. Reload the page and try again.","messagePattern":"Could not create the session\\. Reload the page and try again\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/factory-ui/src/ui/domains/chat/hooks/useCreateUserSessionFromDraft.ts","lineNumber":26,"sourceCode":"import { useChatModels } from '../context/useChatModels';\nimport { useChatModes } from '../context/useChatModes';\nimport { useChatSessionContext } from '../context/useChatSessionContext';\nimport { AGENT_CONTROLLER_ID } from '../services/constants';\nimport { promptHandoffState } from './useHandoffPrompt';\n\nexport function useCreateUserSessionFromDraft() {\n  const { baseUrl, factorySessionState } = useChatSessionContext();\n  const { activeModeId } = useChatModes();\n  const { activeModelId, draftModelPackId, modelPacks } = useChatModels();\n  const { factoryId, draftSessionId } = useParams<{ factoryId: string; draftSessionId: string }>();\n  const queryClient = useQueryClient();\n  const navigate = useNavigate();\n\n  return useMutation({\n    mutationFn: async (prompt: string) => {\n      const projectRepositoryId = factorySessionState?.projectRepositoryId;\n      if (!draftSessionId || !factoryId || !projectRepositoryId) {\n        throw new Error('Could not create the session. Reload the page and try again.');\n      }\n      if (!activeModeId || !activeModelId) {\n        throw new Error('Session configuration is not ready. Try again.');\n      }\n\n      // Activating the pack applies its models server-side, so only hand off a\n      // model when the draft explicitly deviated from the selected pack.\n      const modeKey =\n        activeModeId === 'build' || activeModeId === 'plan' || activeModeId === 'fast' ? activeModeId : undefined;\n      const packModelId =\n        draftModelPackId && modeKey\n          ? modelPacks.find(pack => pack.id === draftModelPackId)?.models[modeKey]\n          : undefined;\n      const handoffModelId = activeModelId === packModelId ? undefined : activeModelId;\n\n      try {\n        const session = await createUserSession(baseUrl, projectRepositoryId, {\n          sessionId: draftSessionId,","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/factory-ui/src/ui/domains/chat/hooks/useCreateUserSessionFromDraft.ts#L8-L44","documentation":"useCreateUserSessionFromDraft promotes a draft chat into a real user session. Its mutationFn throws 'Could not create the session. Reload the page and try again.' when the required identifiers (draftSessionId, factoryId, or factorySessionState.projectRepositoryId) are missing at submit time, i.e. the draft cannot be anchored to a factory project repository.","triggerScenarios":"Submitting the first prompt of a draft chat while draftSessionId is absent (draft not yet initialized), factoryId is missing from the route, or the factory session state has no projectRepositoryId (no repository connected or session state not loaded).","commonSituations":"User sends a prompt immediately after page load before hydration completes; repository disconnected from the project while a stale draft was open; deep link into a draft URL missing factory/route params; HMR or navigation wiping draft session state.","solutions":["Reload the page so draft session and factory state re-hydrate, then resend the prompt (as the message suggests).","Confirm the project has a connected repository; projectRepositoryId is required to create a session.","Check the URL/route includes the factoryId and draft session identifiers before submitting.","In code, disable the submit button until draftSessionId, factoryId, and projectRepositoryId are all present."],"exampleFix":"// before\ncreateDraftSessionMutation.mutate(prompt); // throws when ids are missing\n\n// after\nconst ready = Boolean(draftSessionId && factoryId && factorySessionState?.projectRepositoryId);\n<button disabled={!ready} onClick={() => createDraftSessionMutation.mutate(prompt)}>Send</button>","handlingStrategy":"validation","validationCode":"const ready = Boolean(draftSessionId && factoryId && factorySessionState?.projectRepositoryId);\nif (!ready) return; // keep submit disabled and prompt a reload","typeGuard":"function canCreateSession(s: { draftSessionId?: string; factoryId?: string; projectRepositoryId?: string }): s is { draftSessionId: string; factoryId: string; projectRepositoryId: string } {\n  return Boolean(s.draftSessionId && s.factoryId && s.projectRepositoryId);\n}","tryCatchPattern":"try {\n  await createDraftSessionMutation.mutateAsync(prompt);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Could not create the session')) {\n    toast.error(e.message);\n    window.location.reload();\n  } else throw e;\n}","preventionTips":["Disable the chat submit control until draftSessionId, factoryId, and projectRepositoryId exist.","Hydrate draft/session state before enabling prompt input.","Reconnect the project repository before opening drafts.","Detect route param loss (navigation/HMR) and reinitialize the draft."],"tags":["react","react-query","state","missing-parameter","chat"],"backgroundTag":"missing-required-parameter","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}