{"record":{"id":"21c4dc4d3fa4031e","repo":"mastra-ai/mastra","slug":"snapshot-context-not-found-for-runid-snapshot-r","errorCode":null,"errorMessage":"Snapshot context not found for runId ${snapshot?.runId}","messagePattern":"Snapshot context not found for runId (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/storage/workflow-snapshot.ts","lineNumber":69,"sourceCode":"    waitingPaths: {},\n    status: 'pending',\n    runId,\n  } as WorkflowRunState;\n}\n\nexport function mergeWorkflowStepResult({\n  snapshot,\n  stepId,\n  result,\n  requestContext,\n}: {\n  snapshot: WorkflowRunState;\n  stepId: string;\n  result: StepResult<any, any, any, any>;\n  requestContext: Record<string, any>;\n}): Record<string, StepResult<any, any, any, any>> {\n  if (!snapshot?.context) {\n    throw new Error(`Snapshot context not found for runId ${snapshot?.runId}`);\n  }\n\n  const existingResult = snapshot.context[stepId];\n  if (\n    existingResult &&\n    'output' in existingResult &&\n    Array.isArray(existingResult.output) &&\n    result &&\n    typeof result === 'object' &&\n    'output' in result &&\n    Array.isArray(result.output)\n  ) {\n    const existingOutput = existingResult.output as unknown[];\n    const newOutput = result.output as unknown[];\n    const mergedOutput = [...existingOutput];\n    const hasPendingMarker = newOutput.some(isPendingMarker);\n    for (let i = 0; i < Math.max(existingOutput.length, newOutput.length); i++) {\n      if (i < newOutput.length) {","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/storage/workflow-snapshot.ts#L51-L87","documentation":"mergeWorkflowStepResult mutates a WorkflowRunState snapshot to merge a step result, but requires snapshot.context to exist. When the snapshot is undefined, malformed (missing context), or loaded from storage in a legacy/partial shape, this error is thrown so the step result is not silently lost.","triggerScenarios":"Calling context/getWorkflowRunState paths where the loaded snapshot is undefined (run not found) or its context field is missing; merging results into a snapshot retrieved from an old storage format.","commonSituations":"Querying a workflow run by ID that was never persisted or was deleted; a snapshot written by an older Mastra version without context; concurrent runs where the wrong runId was used.","solutions":["Verify the runId is correct and the run exists in storage before merging","Re-create/initialize the snapshot so it has a context object before merging step results","Check the stored snapshot shape (e.g. log it) and migrate old records to include context","Wrap in try/catch and fall back to re-running the step or fetching a fresh snapshot"],"exampleFix":"// before\nawait workflow.commit({ runId: someId, results }); // someId not persisted\n// after\nconst snapshot = await storage.getWorkflowRunState(runId);\nif (!snapshot?.context) throw new Error(`Run ${runId} not found`);\nawait workflow.commit({ runId, results });","handlingStrategy":"try-catch","validationCode":"async function requireSnapshot(storage, runId) {\n  const snapshot = await storage.getWorkflowRunState(runId);\n  if (!snapshot?.context) throw new Error(`No persisted snapshot/context for run ${runId}`);\n  return snapshot;\n}","typeGuard":"function hasContext(s: WorkflowRunState | undefined | null): s is WorkflowRunState & { context: Record<string, StepResult<any, any, any, any>> } {\n  return !!s && typeof s === 'object' && !!s.context;\n}","tryCatchPattern":"try {\n  await mergeAndPersist(runId, stepId, result);\n} catch (e) {\n  if (String(e).includes('Snapshot context not found')) {\n    logger.error('Workflow run snapshot missing', { runId, stepId });\n    // re-create the run or surface a 404 to the caller\n  } else throw e;\n}","preventionTips":["Confirm the run exists in storage before merging step results","Ensure workflow runs are fully persisted (context included) — watch for legacy snapshot formats","Pass runIds from trusted state, not user input","Add an integrity check on snapshot load that validates the context field"],"tags":["workflow","storage","state"],"backgroundTag":"workflow-snapshot-missing","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}