Lawfare Group → Knowledge Institute → Lawfare Academy
Reproduce the worked examples
Copy the complete code below into a UTF-8 file named verify.py and run python verify.py with Python 3. It uses only the standard library and performs exact or rational synthetic calculations. It does not access a network or learner records. Assertions fail if the expected synthetic results change.
These checks verify the stated arithmetic and query behavior, not independent academic acceptance or production migration safety.
"""Isolated synthetic MIS fixtures. No network, production or learner records.
Serial SQLite transactions are a teaching model, not distributed assurance.
"""
import json,sqlite3
from fractions import Fraction as F
from datetime import datetime
results={}
def check(name,condition):
assert condition,name
results[name]=True
jobs=set(range(100));material=set(range(8));approval=set(range(8,13))
check('disjoint_missing_conditions',len(jobs-(material|approval))==87)
approval={0,1,2,8,9}
check('overlap_count',len(jobs-(material|approval))==90)
check('nested_conditions',len(jobs-(material|set(range(5))))==92)
def ready(approved,material,capacity,cancelled):
return approved is True and material is True and capacity is True and cancelled is False
check('unknown_is_not_approved',not ready(None,True,True,False))
check('cancellation_blocks_dispatch',not ready(True,True,True,True))
check('all_required_conditions',ready(True,True,True,False))
a=(9,3,8);b=(7,9,7)
def score(s,w):return sum(x*y for x,y in zip(s,w))
check('original_preference',score(a,(F(1,2),F(3,10),F(1,5)))==7 and score(b,(F(1,2),F(3,10),F(1,5)))==F(38,5))
check('changed_preference',score(a,(F(7,10),F(1,10),F(1,5)))==F(41,5) and score(b,(F(7,10),F(1,10),F(1,5)))==F(36,5))
check('export_gate_independent_of_score',a[1]<5<=b[1])
check('annual_expected_costs',20000+F(1,5)*50000==30000 and 26000+F(1,20)*50000==28500)
check('changed_risk_cost',20000+F(3,100)*50000==21500)
check('cost_equality_probability',F(28500-20000,50000)==F(17,100))
db=sqlite3.connect(':memory:')
db.executescript('CREATE TABLE events(id TEXT PRIMARY KEY,invoice TEXT NOT NULL,amount INTEGER NOT NULL);CREATE TABLE invoices(id TEXT PRIMARY KEY,balance INTEGER NOT NULL);')
def apply(event,invoice,amount,crash=False):
old=db.execute('SELECT invoice,amount FROM events WHERE id=?',(event,)).fetchone()
if old is not None:
if old!=(invoice,amount):raise ValueError('conflicting reuse')
return 'replay'
with db:
db.execute('INSERT INTO events VALUES(?,?,?)',(event,invoice,amount))
db.execute('INSERT INTO invoices VALUES(?,?) ON CONFLICT(id) DO UPDATE SET balance=balance+excluded.balance',(invoice,amount))
if crash:raise RuntimeError('synthetic interruption before commit')
return 'accepted'
check('first_operation',apply('E001','I001',12000)=='accepted')
check('acknowledgement_lost_exact_replay',apply('E001','I001',12000)=='replay')
apply('E002','I002',8000);apply('E003','I001',-1000)
check('accepted_count',db.execute('SELECT COUNT(*) FROM events').fetchone()[0]==3)
check('accepted_net',db.execute('SELECT SUM(balance) FROM invoices').fetchone()[0]==19000)
try:apply('E001','I001',13000)
except ValueError:pass
else:raise AssertionError('conflicting event accepted')
check('conflict_preserves_balance',db.execute('SELECT balance FROM invoices WHERE id=?',('I001',)).fetchone()[0]==11000)
try:apply('E004','I003',500,crash=True)
except RuntimeError:pass
else:raise AssertionError('crash fixture did not interrupt')
check('event_and_effect_rollback',not db.execute('SELECT id FROM events WHERE id=?',('E004',)).fetchone() and not db.execute('SELECT id FROM invoices WHERE id=?',('I003',)).fetchone())
check('retry_after_rollback',apply('E004','I003',500)=='accepted')
db.close()
def minutes(a,b):
return int((datetime.fromisoformat(a)-datetime.fromisoformat(b)).total_seconds()/60)
check('recovery_gap',minutes('2026-01-31T14:00','2026-01-31T13:15')==45)
check('recovery_time_misses_objective',minutes('2026-01-31T18:30','2026-01-31T14:00')==270>240)
check('changed_copy_misses_data_objective',minutes('2026-01-31T14:00','2026-01-31T12:30')==90>60)
tp,fn,fp,tn=80,20,90,810
check('confusion_population',tp+fn+fp+tn==1000 and tp+fn==100)
check('precision',F(tp,tp+fp)==F(8,17))
check('recall',F(tp,tp+fn)==F(4,5))
check('accuracy',F(tp+tn,1000)==F(89,100))
check('always_negative_accuracy',F(900,1000)==F(9,10) and F(900,1000)>F(tp+tn,1000))
check('changed_prevalence_counts',F(20)*F(4,5)==16 and F(980)*F(1,10)==98)
check('changed_precision',F(16,16+98)==F(8,57))
source={'I001':12000,'I002':8000,'I003':1000}
target={'I001':12000,'I002':9000}
check('grand_total_false_assurance',sum(source.values())==sum(target.values())==21000)
check('identity_control_detects_error',set(source)!=set(target))
check('entity_amount_control_detects_error',source['I002']!=target['I002'])
print(json.dumps({'passed':True,'checks':results,'scope':'Synthetic serial fixtures only; no production, distributed, restoration or observed learner validation.'},indent=2))
Related chapters
- From a business promise to an auditable state transition
- Choose systems through testable requirements and conditional economics
- Make repeated, late and partial integration events explainable
- Distinguish a backup claim from tested business recovery
- Trace a dashboard signal to the population and action it represents
- Make adoption a controlled change with explicit acceptance authority
- Cumulative clinic: defend and challenge an information-system acceptance decision