mastra-ai/mastra · error

Could not create the session. Reload the page and try again.

Error message

Could not create the session. Reload the page and try again.

What it means

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.

Source

Thrown at mastracode/factory-ui/src/ui/domains/chat/hooks/useCreateUserSessionFromDraft.ts:26

import { useChatModels } from '../context/useChatModels';
import { useChatModes } from '../context/useChatModes';
import { useChatSessionContext } from '../context/useChatSessionContext';
import { AGENT_CONTROLLER_ID } from '../services/constants';
import { promptHandoffState } from './useHandoffPrompt';

export function useCreateUserSessionFromDraft() {
  const { baseUrl, factorySessionState } = useChatSessionContext();
  const { activeModeId } = useChatModes();
  const { activeModelId, draftModelPackId, modelPacks } = useChatModels();
  const { factoryId, draftSessionId } = useParams<{ factoryId: string; draftSessionId: string }>();
  const queryClient = useQueryClient();
  const navigate = useNavigate();

  return useMutation({
    mutationFn: async (prompt: string) => {
      const projectRepositoryId = factorySessionState?.projectRepositoryId;
      if (!draftSessionId || !factoryId || !projectRepositoryId) {
        throw new Error('Could not create the session. Reload the page and try again.');
      }
      if (!activeModeId || !activeModelId) {
        throw new Error('Session configuration is not ready. Try again.');
      }

      // Activating the pack applies its models server-side, so only hand off a
      // model when the draft explicitly deviated from the selected pack.
      const modeKey =
        activeModeId === 'build' || activeModeId === 'plan' || activeModeId === 'fast' ? activeModeId : undefined;
      const packModelId =
        draftModelPackId && modeKey
          ? modelPacks.find(pack => pack.id === draftModelPackId)?.models[modeKey]
          : undefined;
      const handoffModelId = activeModelId === packModelId ? undefined : activeModelId;

      try {
        const session = await createUserSession(baseUrl, projectRepositoryId, {
          sessionId: draftSessionId,

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Reload the page so draft session and factory state re-hydrate, then resend the prompt (as the message suggests).
  2. Confirm the project has a connected repository; projectRepositoryId is required to create a session.
  3. Check the URL/route includes the factoryId and draft session identifiers before submitting.
  4. In code, disable the submit button until draftSessionId, factoryId, and projectRepositoryId are all present.

Example fix

// before
createDraftSessionMutation.mutate(prompt); // throws when ids are missing

// after
const ready = Boolean(draftSessionId && factoryId && factorySessionState?.projectRepositoryId);
<button disabled={!ready} onClick={() => createDraftSessionMutation.mutate(prompt)}>Send</button>
Defensive patterns

Strategy: validation

Validate before calling

const ready = Boolean(draftSessionId && factoryId && factorySessionState?.projectRepositoryId);
if (!ready) return; // keep submit disabled and prompt a reload

Type guard

function canCreateSession(s: { draftSessionId?: string; factoryId?: string; projectRepositoryId?: string }): s is { draftSessionId: string; factoryId: string; projectRepositoryId: string } {
  return Boolean(s.draftSessionId && s.factoryId && s.projectRepositoryId);
}

Try / catch

try {
  await createDraftSessionMutation.mutateAsync(prompt);
} catch (e) {
  if (e instanceof Error && e.message.includes('Could not create the session')) {
    toast.error(e.message);
    window.location.reload();
  } else throw e;
}

Prevention

When it happens

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

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

Understand the failure class

Background: Missing required parameter errors: what 'X is required' and 'the required X param is missing' mean, and how to fix them — this error's family across 27 libraries.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/a2e2153ea6f23fa7. Report an issue: GitHub.