Time Travel
Time travel allows you to re-execute a workflow starting from any specific step, using either stored snapshot data or custom context you provide. This is useful for debugging failed workflows, testing individual steps with different inputs, or recovering from errors without re-running the entire workflow. You can also use time travel to execute a workflow that has not been run yet, starting from any specific step.
How time travel worksDirect link to How time travel works
When you call timeTravel() on a workflow run:
- The workflow loads the existing snapshot from storage (if available)
- Step results before the target step are reconstructed from the snapshot or provided context
- Execution begins from the specified step with the provided or reconstructed input data
- The workflow continues to completion from that point forward
Time travel requires storage to be configured since it relies on persisted workflow snapshots.
Basic usageDirect link to Basic usage
Use run.timeTravel() to re-execute a workflow from a specific step:
import { mastra } from "./mastra";
const workflow = mastra.getWorkflow("myWorkflow");
const run = await workflow.createRunAsync();
const result = await run.timeTravel({
step: "step2",
inputData: { previousStepResult: "custom value" },
});
Specifying the target stepDirect link to Specifying the target step
You can specify the target step using either a step reference or a step ID:
Using step referenceDirect link to Using step reference
const result = await run.timeTravel({
step: step2,
inputData: { value: 10 },
});
Using step IDDirect link to Using step ID
const result = await run.timeTravel({
step: "step2",
inputData: { value: 10 },
});
Nested workflow stepsDirect link to Nested workflow steps
For steps inside nested workflows, use dot notation, an array of step IDS or an array of step references:
// Using dot notation
const result = await run.timeTravel({
step: "nestedWorkflow.step3",
inputData: { value: 10 },
});
// Using array of step IDs
const result = await run.timeTravel({
step: ["nestedWorkflow", "step3"],
inputData: { value: 10 },
});
// Using array of step references
const result = await run.timeTravel({
step: [nestedWorkflow, step3],
inputData: { value: 10 },
});
Providing execution contextDirect link to Providing execution context
You can provide context to specify the state of previous steps when time traveling:
const result = await run.timeTravel({
step: "step2",
context: {
step1: {
status: "success",
payload: { value: 0 },
output: { step1Result: 2 },
startedAt: Date.now(),
endedAt: Date.now(),
},
},
});
The context object contains step results keyed by step ID. Each step result includes:
status: The step's execution status (success,failed,suspended)payload: The input data passed to the stepoutput: The step's output data (for successful steps)startedAt: Timestamp when the step startedendedAt: Timestamp when the step ended (for completed steps)suspendPayload: Data passed tosuspend()(for suspended steps)resumePayload: Data passed toresume()(for resumed steps)
Re-running failed workflowsDirect link to Re-running failed workflows
Time travel is particularly useful for debugging and recovering from failed workflow executions:
const workflow = mastra.getWorkflow("myWorkflow");
const run = await workflow.createRunAsync();
// Initial run fails at step2
const failedResult = await run.start({
inputData: { value: 1 },
});
if (failedResult.status === "failed") {
// Re-run from step2 with corrected input
const recoveredResult = await run.timeTravel({
step: "step2",
inputData: { step1Result: 5 }, // Provide corrected input
});
}
Time travel with suspended workflowsDirect link to Time travel with suspended workflows
You can time travel to resume a suspended workflow from an earlier step:
const run = await workflow.createRunAsync();
// Start workflow - suspends at promptAgent step
const initialResult = await run.start({
inputData: { input: "test" },
});
if (initialResult.status === "suspended") {
// Time travel back to an earlier step with resume data
const result = await run.timeTravel({
step: "getUserInput",
resumeData: {
userInput: "corrected input",
},
});
}
Streaming time travel resultsDirect link to Streaming time travel results
Use timeTravelStream() to receive streaming events during time travel execution:
const run = await workflow.createRunAsync();
const output = run.timeTravelStream({
step: "step2",
inputData: { value: 10 },
});
// Access the stream
for await (const event of output.fullStream) {
console.log(event.type, event.payload);
}
// Get final result
const result = await output.result;
Time travel with initial stateDirect link to Time travel with initial state
You can provide initial state when time traveling to set workflow-level state:
const result = await run.timeTravel({
step: "step2",
inputData: { value: 10 },
initialState: {
counter: 5,
metadata: { source: "time-travel" },
},
});
Error handlingDirect link to Error handling
Time travel throws errors in specific situations:
Running workflowDirect link to Running workflow
You cannot time travel to a workflow that is currently running:
try {
await run.timeTravel({ step: "step2" });
} catch (error) {
// "This workflow run is still running, cannot time travel"
}
Invalid step IDDirect link to Invalid step ID
Time travel throws if the target step doesn't exist in the workflow:
try {
await run.timeTravel({ step: "nonExistentStep" });
} catch (error) {
// "Time travel target step not found in execution graph: 'nonExistentStep'. Verify the step id/path."
}
Invalid input dataDirect link to Invalid input data
When validateInputs is enabled, time travel validates the input data against the step's schema:
try {
await run.timeTravel({
step: "step2",
inputData: { invalidField: "value" },
});
} catch (error) {
// "Invalid inputData: \n- step1Result: Required"
}
Nested workflows contextDirect link to Nested workflows context
When time traveling into a nested workflow, you can provide context for both the parent and nested workflow steps:
const result = await run.timeTravel({
step: "nestedWorkflow.step3",
context: {
step1: {
status: "success",
payload: { value: 0 },
output: { step1Result: 2 },
startedAt: Date.now(),
endedAt: Date.now(),
},
nestedWorkflow: {
status: "running",
payload: { step1Result: 2 },
startedAt: Date.now(),
},
},
nestedStepsContext: {
nestedWorkflow: {
step2: {
status: "success",
payload: { step1Result: 2 },
output: { step2Result: 3 },
startedAt: Date.now(),
endedAt: Date.now(),
},
},
},
});
Use casesDirect link to Use cases
Debugging failed stepsDirect link to Debugging failed steps
Re-run a failed step with the same or modified input to diagnose issues:
const result = await run.timeTravel({
step: failedStepId,
context: originalContext, // Use context from the failed run
});
Testing step logic on new workflow runDirect link to Testing step logic on new workflow run
Test individual steps with specific inputs on a new workflow run, useful for testing a step logic without starting workflow execution from the beginning.
const result = await run.timeTravel({
step: "processData",
inputData: { testData: "specific test case" },
});
Recovering from transient failuresDirect link to Recovering from transient failures
Re-run steps that failed due to temporary issues (network errors, rate limits):
// After fixing the external service issue
const result = await run.timeTravel({
step: "callExternalApi",
inputData: savedInputData,
});