Replay Examples
Demonstrates different replay modes for testing, debugging, and policy optimization.
Overview
This example shows:
- Exact replay
- Policy-aware replay
- Memory-informed replay
- Comparing replay results
Code
import { Workflow, Execution } from "@mdwr/sdk";
// Create a simple workflow for replay testing
const replayWorkflow = Workflow.create("replay_test_v1")
.step("evaluate", async (ctx) => {
const { amount, userTier } = ctx.input;
// Query memory for similar decisions
const insights = await ctx.memory.similar({
situation: "evaluation",
context: { amount, userTier }
});
let decision = "approve";
let reason = "Standard approval";
let confidence = 0.8;
if (insights.length > 0) {
const best = insights[0];
decision = best.decision.decision;
reason = `Based on history: ${best.decision.reason}`;
confidence = best.decision.confidence || 0.8;
}
// Policy rule: amount threshold
const threshold = ctx.input.policyVersion === "v2" ? 5000 : 1000;
if (amount > threshold) {
decision = "review";
reason = `Amount ${amount} exceeds threshold ${threshold}`;
confidence = 0.9;
}
return {
decision,
reason,
confidence,
metadata: {
amount,
userTier,
threshold,
policyVersion: ctx.input.policyVersion || "v1"
}
};
}, true)
.step("process", async (ctx) => {
if (ctx.state.evaluate?.decision !== "approve") {
return {
decision: "skip",
reason: "Evaluation did not approve",
confidence: 1.0
};
}
return {
decision: "completed",
reason: "Processing completed",
confidence: 1.0
};
}, true);
/**
* Example 1: Exact Replay
* Replays the workflow with the exact same decisions
*/
export async function exactReplayExample() {
console.log("=== Exact Replay Example ===");
// Initial execution
const execution = await replayWorkflow.execute({
amount: 1500,
userTier: "gold",
policyVersion: "v1"
});
console.log("Initial execution:");
console.log(" Decision:", execution.getStepResults().get("evaluate")?.decision);
console.log(" Reason:", execution.getStepResults().get("evaluate")?.reason);
// Exact replay - same decisions
const replayed = await execution.replay({ mode: "exact" });
console.log("\nExact replay:");
console.log(" Decision:", replayed.getStepResults().get("evaluate")?.decision);
console.log(" Reason:", replayed.getStepResults().get("evaluate")?.reason);
console.log(" (Should match initial execution)");
}
/**
* Example 2: Policy-Aware Replay
* Replays with different policy parameters
*/
export async function policyAwareReplayExample() {
console.log("\n=== Policy-Aware Replay Example ===");
// Initial execution with v1 policy (threshold: 1000)
const execution = await replayWorkflow.execute({
amount: 1500,
userTier: "gold",
policyVersion: "v1"
});
console.log("Initial execution (v1 policy, threshold: 1000):");
console.log(" Decision:", execution.getStepResults().get("evaluate")?.decision);
console.log(" Reason:", execution.getStepResults().get("evaluate")?.reason);
// Replay with v2 policy (threshold: 5000)
const replayed = await execution.replay({
mode: "policy-aware",
overrides: {
policyVersion: "v2"
}
});
console.log("\nPolicy-aware replay (v2 policy, threshold: 5000):");
console.log(" Decision:", replayed.getStepResults().get("evaluate")?.decision);
console.log(" Reason:", replayed.getStepResults().get("evaluate")?.reason);
console.log(" (Should show different decision due to policy change)");
}
/**
* Example 3: Memory-Informed Replay
* Replays using historical insights
*/
export async function memoryInformedReplayExample() {
console.log("\n=== Memory-Informed Replay Example ===");
// Initial execution
const execution = await replayWorkflow.execute({
amount: 1500,
userTier: "gold",
policyVersion: "v1"
});
console.log("Initial execution:");
console.log(" Decision:", execution.getStepResults().get("evaluate")?.decision);
console.log(" Confidence:", execution.getStepResults().get("evaluate")?.confidence);
// Memory-informed replay - uses historical data
const replayed = await execution.replay({ mode: "memory-informed" });
console.log("\nMemory-informed replay:");
console.log(" Decision:", replayed.getStepResults().get("evaluate")?.decision);
console.log(" Confidence:", replayed.getStepResults().get("evaluate")?.confidence);
console.log(" (May differ based on accumulated memory)");
}
// Run all examples
export async function runAllReplayExamples() {
await exactReplayExample();
await policyAwareReplayExample();
await memoryInformedReplayExample();
}
export default replayWorkflow;
Running the Example
# Deploy the workflow
npx ts-node scripts/deploy.ts
# Run replay examples
npx ts-node examples/replay-examples.ts
Replay Modes
1. Exact Replay
Replays with the exact same decisions:
const replayed = await execution.replay({ mode: "exact" });
Use cases:
- Testing workflow logic
- Debugging issues
- Auditing decisions
2. Policy-Aware Replay
Replays with different policy parameters:
const replayed = await execution.replay({
mode: "policy-aware",
overrides: {
policyVersion: "v2"
}
});
Use cases:
- Testing policy changes
- Comparing policy versions
- Optimizing parameters
3. Memory-Informed Replay
Replays using accumulated historical insights:
const replayed = await execution.replay({
mode: "memory-informed"
});
Use cases:
- Learning from history
- Improving decisions over time
- Adapting to patterns
Comparing Replays
const original = await workflow.execute({ amount: 1500 });
const replayed = await original.replay({
mode: "policy-aware",
overrides: { policyVersion: "v2" }
});
// Compare decisions
const originalDecision = original.getStepResults().get("evaluate");
const replayedDecision = replayed.getStepResults().get("evaluate");
console.log("Original:", originalDecision?.decision);
console.log("Replayed:", replayedDecision?.decision);
Next Steps
- Replay Modes Guide - Learn more about replay
- Execution API - Complete API reference
- Examples Index - View all examples