| name | safety |
| description | System safety - STAMP/STPA, accidents as system failures, safety constraints |
Nancy Leveson - System Safety Engineering
Apply Leveson's STAMP (Systems-Theoretic Accident Model and Processes) from "Engineering a Safer World" to build systems that prevent catastrophic failures.
Core Philosophy
Accidents Are System Failures
Traditional thinking blames components or humans. Leveson's insight: accidents emerge from system interactions, not component failures.
TRADITIONAL MODEL (wrong):
Component fails โ Accident
Human errs โ Accident
LEVESON MODEL (correct):
System design allows unsafe states โ
Inadequate control โ
Accident
The human "error" is a SYMPTOM, not a CAUSE.
Implication: Stop asking "who screwed up?" Start asking "what system design allowed this?"
Safety as a Control Problem
Safety isn't about preventing failures. It's about maintaining control over hazards.
HAZARD: System state that can lead to harm
SAFETY CONSTRAINT: Control that prevents hazard
Example:
โโโ Hazard: Database corruption from concurrent writes
โโโ Safety Constraint: Writes must be serialized
โโโ Control: Transaction isolation level
The accident happens when the control is inadequate.
Humans Are Not the Problem
When humans make "errors," the system failed them.
BAD: "User clicked delete instead of save"
โ Add confirmation dialog
GOOD: "System design made destructive action too easy"
โ Destructive actions require different gesture
โ Undo available for 30 seconds
โ Visual distinction between create/destroy
Leveson's rule: If a human can easily cause harm, the design is wrong.
STAMP Framework
System-Theoretic Accident Model
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ CONTROL STRUCTURE โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Controller โ
โ โโโ Control Algorithm โ
โ โโโ Process Model (beliefs) โ
โ โโโ Control Actions โ
โ โ โ
โ Controlled Process โ
โ โโโ Feedback โ
โ โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Accidents occur when:
1. Control actions are inadequate
2. Process model doesn't match reality
3. Feedback is missing, delayed, or wrong
STPA (System-Theoretic Process Analysis)
Step-by-step hazard analysis:
STEP 1: Define accidents and hazards
โโโ What harm are we preventing?
STEP 2: Model the control structure
โโโ Who/what controls what?
STEP 3: Identify unsafe control actions
โโโ What control actions could cause hazard?
โโโ Not providing causes hazard
โโโ Providing causes hazard
โโโ Too early/late causes hazard
โโโ Stopped too soon/applied too long
STEP 4: Identify loss scenarios
โโโ Why might unsafe control actions occur?
โโโ Controller failures
โโโ Inadequate feedback
โโโ Process model inconsistency
โโโ Control path failures
Prescriptive Rules
Enumerate Unsafe Control Actions
For every control action, ask four questions:
| Control Action | Not Providing | Providing | Too Early/Late | Wrong Duration |
|---|
| Delete record | Data lingers when should be removed | Accidental data loss | Delete before confirmation | - |
| Send notification | User misses critical info | Spam, alert fatigue | Delayed = useless | - |
| Scale up | System overwhelmed | Unnecessary cost | Scale after traffic spike | Scale too long = cost |
Safety Constraints Must Be Explicit
async function transferFunds(from, to, amount) {
await debit(from, amount);
await credit(to, amount);
}
async function transferFunds(from, to, amount) {
await db.transaction(async (tx) => {
await tx.debit(from, amount);
await tx.credit(to, amount);
});
}
Process Model Must Match Reality
Controllers act on their beliefs about the system, not reality.
PROCESS MODEL DRIFT:
โโโ Cache believes data is current (it's stale)
โโโ Load balancer believes server is healthy (it's overloaded)
โโโ User believes file is saved (it's not)
โโโ Admin believes backup ran (it failed silently)
LEVESON FIX:
โโโ Explicit model refresh mechanisms
โโโ Feedback on actual state, not assumed state
โโโ "Trust but verify" at system boundaries
โโโ Alarms for model-reality divergence
Feedback Must Be Adequate
INADEQUATE FEEDBACK:
โโโ Missing: No feedback at all
โโโ Delayed: Feedback arrives too late to correct
โโโ Incorrect: Feedback doesn't reflect reality
โโโ Ignored: System receives but doesn't process
DESIGN FOR ADEQUATE FEEDBACK:
โโโ Acknowledge every command
โโโ Confirm every state change
โโโ Report failures immediately and loudly
โโโ Make success and failure visually distinct
Code Application
Design Safety Constraints First
Before implementing a feature:
1. What accident could this cause?
2. What hazards lead to that accident?
3. What safety constraints prevent those hazards?
4. How do we enforce those constraints?
5. How do we know if constraints are violated?
Control Structure Documentation
Unsafe Control Action Analysis
For critical operations, document UCA analysis:
## UCA Analysis: deleteUserAccount()
| UCA Type | Scenario | Hazard | Mitigation |
|----------|----------|--------|------------|
| Providing when shouldn't | Delete admin account | System unrecoverable | Prevent last admin delete |
| Not providing when should | Account with breach not deleted | Data exposure continues | Auto-delete on breach confirm |
| Too early | Delete before data export | User data lost | Export must complete first |
| Too late | Delete delayed after request | GDPR violation | SLA with alerting |
Anti-Patterns
| Pattern | Leveson Problem | Fix |
|---|
| "User error" post-mortems | Blaming humans, not system | Analyze control structure |
| Hidden safety assumptions | Implicit constraints fail | Document safety constraints explicitly |
| "Works on my machine" | Process model drift | Verify production state, not assumed state |
| Silent failures | Inadequate feedback | Failures must be loud and visible |
| "Just add a check" | Treating symptoms | Redesign control structure |
Review Checklist
Before shipping safety-critical code:
Leveson Score
| Score | Meaning |
|---|
| 10 | Full STPA analysis, explicit safety constraints, adequate feedback |
| 7-9 | Safety constraints documented, some UCA analysis |
| 4-6 | Some safety thinking but implicit, blame-focused post-mortems |
| 0-3 | No safety analysis, "user error" culture |
Key Quotes
"Most accidents are not the result of unknown scientific principles but rather of a failure to apply well-known, standard engineering practices."
"Blaming operators for accidents is not only unfair but is a way of avoiding the real problems."
"Safety is a system property, not a component property."
Integration
Combine with:
/failure - Historical failure analysis
/resilience - Antifragile design
/security-mindset - Security threat modeling (similar structure to STPA)