#!/usr/bin/env python3
import hashlib, json
run_id='INC-42'; evidence=[]; side_effects=0

def record(event, **data): evidence.append({"run_id":run_id,"event":event,**data})

def read_tool(name,result): record('read_tool',tool=name,result=result); return result
service=read_tool('get_service_status','stopped')
logs=read_tool('get_recent_logs','upstream connection failures')
deploy=read_tool('get_deployment_health','replicas available; service process stopped')
proposal={"tool":"restart_service","target":"prod-web-03","service":"checkout","reason":"service stopped with matching logs"}
proposal_hash=hashlib.sha256(json.dumps(proposal,sort_keys=True).encode()).hexdigest()[:16]
record('proposal',proposal=proposal,proposal_hash=proposal_hash)
approval={"decision":"approved","proposal_hash":proposal_hash,"approver":"human-operator"}
record('approval',**approval)
assert approval['proposal_hash']==proposal_hash and approval['decision']=='approved'
side_effects+=1; record('write_tool',tool='restart_service',operation_id='OP-900',status='ok')
health=read_tool('verify_service_health','healthy')
status='RESOLVED' if health=='healthy' else 'ESCALATE'
record('final',status=status,critical_policy_violations=0)
assert len([e for e in evidence if e['event']=='read_tool'])==4  # 3 investigation + verification
assert side_effects==1 and status=='RESOLVED'
print(json.dumps({"run_id":run_id,"side_effects":side_effects,"status":status,"events":evidence},indent=2))
print('PASS: capstone resolved incident with exact approval, one side effect and post-change verification')
