Getting Started

Fraud Detection with Human Review


For high-value transactions, you need a fraud detection pipeline that auto-approves low-risk payments, auto-rejects high-risk ones, and routes medium-risk transactions to a human reviewer with a configurable timeout. Orcher's wait_for_event_with_timeout makes the human-in-the-loop step trivial — the workflow pauses until a reviewer signals approval or the timeout expires.

How it works

One risk assessment, three routes. The two automatic routes finish in milliseconds; the third parks the workflow in front of a human — and only that route pays any waiting cost.

Risk Triage
assess_fraud_risk
ML model / rules engine → risk level
▼ triage
Low
auto-approve
→ charge
Medium
human review
→ event gate, 4h
High
auto-reject
→ workflow fails
Every route records its decision — auto or human, approve or reject — before acting on it.

Example: triage with an event gate

1
use orcher::prelude::*;
2
use anyhow::Result;
3
use std::time::Duration;
4
5
#[derive(Debug, Clone, Serialize, Deserialize)]
6
enum RiskLevel { Low, Medium, High }
7
8
#[derive(Debug, Clone, Serialize, Deserialize)]
9
struct FraudScore {
10
risk_level: RiskLevel,
11
score: f64,
12
reasons: Vec<String>,
13
}
14
15
#[derive(Debug, Clone, Serialize, Deserialize)]
16
struct ReviewDecision {
17
approved: bool,
18
reviewer: String,
19
notes: String,
20
}
21
22
#[task]
23
async fn assess_fraud_risk(input: PaymentInput) -> Result<FraudScore> {
24
// Call fraud detection service (ML model, rules engine, etc.)
25
let score = fraud::assess(&input).await?;
26
Ok(score)
27
}
28
29
#[task]
30
async fn notify_fraud_team(input: PaymentInput, score: FraudScore) -> Result<()> {
31
// Send to review queue with all context
32
fraud::create_review_case(&input, &score).await?;
33
Ok(())
34
}
35
36
#[task]
37
async fn record_fraud_decision(order_id: String, approved: bool, reason: String) -> Result<()> {
38
audit::log_fraud_decision(&order_id, approved, &reason).await?;
39
Ok(())
40
}
41
42
#[workflow(name = "fraud_checked_payment", version = "1.0.0")]
43
pub async fn fraud_checked_payment(
44
ctx: WorkflowContext,
45
input: PaymentInput,
46
) -> Result<PaymentResult> {
47
// Step 1: Assess fraud risk
48
let fraud_score = ctx.execute_task(assess_fraud_risk, input.clone()).await?;
49
50
match fraud_score.risk_level {
51
RiskLevel::Low => {
52
// Auto-approve: proceed directly to payment
53
ctx.execute_task(record_fraud_decision, (
54
input.order_id.clone(), true, "auto-approved: low risk".to_string()
55
)).await?;
56
}
57
RiskLevel::High => {
58
// Auto-reject: block the payment
59
ctx.execute_task(record_fraud_decision, (
60
input.order_id.clone(), false, format!("auto-rejected: {}", fraud_score.reasons.join(", "))
61
)).await?;
62
anyhow::bail!("Payment rejected: high fraud risk");
63
}
64
RiskLevel::Medium => {
65
// Route to human reviewer
66
ctx.execute_task(notify_fraud_team, (input.clone(), fraud_score)).await?;
67
68
// Wait for reviewer decision with 4-hour timeout
69
let decision: Option<ReviewDecision> = ctx
70
.wait_for_event_with_timeout("fraud_review_decision", Duration::from_secs(14400))
71
.await?;
72
73
match decision {
74
Some(d) if d.approved => {
75
ctx.execute_task(record_fraud_decision, (
76
input.order_id.clone(), true, format!("approved by {}", d.reviewer)
77
)).await?;
78
}
79
Some(d) => {
80
ctx.execute_task(record_fraud_decision, (
81
input.order_id.clone(), false, format!("rejected by {}: {}", d.reviewer, d.notes)
82
)).await?;
83
anyhow::bail!("Payment rejected by fraud reviewer");
84
}
85
None => {
86
// Timeout — auto-reject for safety
87
ctx.execute_task(record_fraud_decision, (
88
input.order_id.clone(), false, "auto-rejected: review timeout".to_string()
89
)).await?;
90
anyhow::bail!("Payment rejected: fraud review timeout");
91
}
92
}
93
}
94
}
95
96
// Fraud check passed — proceed to payment
97
let result = ctx.execute_task(charge_payment_gateway, (
98
input.amount, input.currency.clone(), input.idempotency_key.clone()
99
)).await?;
100
101
Ok(PaymentResult {
102
charge_id: result.charge_id,
103
status: "completed".to_string(),
104
order_id: input.order_id,
105
})
106
}

The review gate

wait_for_event_with_timeout is a race between a human and a clock. The workflow suspends — holding nothing — until either the reviewer's decision arrives as an event or the four-hour deadline fires, and the return value tells you which one won.

The Review Gate
✓
notify_fraud_teamReview case created with full context
⏸
wait_for_event_with_timeoutSuspended — zero resources, up to 4 hours
▼ whichever comes first
approved
reviewer says yes
→ charge proceeds
rejected
reviewer says no
→ workflow fails
timeout
no decision in 4h
→ auto-reject
The customer's card is never touched until the gate opens with an approval.

Note the ordering: the charge happens after the gate. A suspicious transaction waits as an intent, not as captured money — there is nothing to refund if the answer is no. The same suspension mechanics as durable timers apply: a deploy during the four-hour window changes nothing, and the reviewer's decision wakes the workflow wherever it lands.

Design notes

  • Timeout must fail safe. For payments, no answer means no — an expired review auto-rejects. If rejection is too costly for your risk profile, escalate instead: re-notify and wait again, but never silently approve.
  • Route the decision back with a correlation ID. notify_fraud_team should include the workflow or order ID in the review case, so the reviewer UI can address its fraud_review_decision event to this exact workflow instance. See Events for the delivery mechanics.
  • Record every decision, on every path. All five outcomes — auto-approve, auto-reject, human approve, human reject, timeout — pass through record_fraud_decision before the workflow acts. Combined with the journal, that's a complete who-decided-what audit trail with no extra logging code.
  • Keep thresholds out of the workflow. assess_fraud_risk returns the risk level; the scoring model and its thresholds live in the fraud service. Tuning them doesn't touch versioned workflow logic — and the journaled score keeps replay deterministic even as the model evolves.

Next Steps