Download scripts/verify_crash_matrix.py from Gaurav711/HyperFlow: direct link, hf CLI and curl.
- Browser
- Download file 8.28 kB
-
https://huggingface.co/spaces/Gaurav711/HyperFlow/resolve/main/scripts/verify_crash_matrix.py
- Command line
-
hf download hf://spaces/Gaurav711/HyperFlow/scripts/verify_crash_matrix.py
-
curl -L -o verify_crash_matrix.py https://huggingface.co/spaces/Gaurav711/HyperFlow/resolve/main/scripts/verify_crash_matrix.py
8.28 kB
| #!/usr/bin/env python3 | |
| """Abrupt subprocess exits at six commerce boundaries; simulated effects only.""" | |
| import argparse | |
| import hashlib | |
| import json | |
| import os | |
| from pathlib import Path | |
| import subprocess | |
| import sys | |
| import time | |
| import uuid | |
| ROOT = Path(__file__).resolve().parents[1] | |
| sys.path.insert(0, str(ROOT)) | |
| from scripts.verify_runtime import isolated_database | |
| from backend.flagship.store import Store | |
| from backend.flagship.identity import Identity | |
| from backend.flagship.provider import Provider | |
| from backend.flagship.commerce import Commerce | |
| STAGES = ('proposal_persisted', 'approval_persisted', 'executing_before_provider', | |
| 'provider_committed_before_receipt', 'response_received_before_persistence', | |
| 'success_persisted_before_response') | |
| def context(store, actor, probe, **fields): | |
| row = store.get('crash_probe', probe, actor['id']) | |
| store.update('crash_probe', probe, actor['id'], {**row['data'], **fields}, row['version']) | |
| def child(stage, account_id, probe): | |
| store = Store() | |
| actor = store.account(account_id) | |
| provider = Provider(store, Identity(store)) | |
| commerce = Commerce(store, provider) | |
| proposal = commerce.propose(actor, {'provider': 'im', 'tool': 'checkout', 'mode': 'replay', | |
| 'arguments': {'addressId': 'scenario-address', 'paymentMethod': 'COD'}}) | |
| context(store, actor, probe, proposal_id=proposal['id']) | |
| if stage == 'proposal_persisted': | |
| os._exit(77) | |
| action = commerce.approve(actor, proposal['id'], proposal['version']) | |
| context(store, actor, probe, action_id=action['id']) | |
| if stage == 'approval_persisted': | |
| os._exit(77) | |
| if stage in ('executing_before_provider', 'response_received_before_persistence'): | |
| original = commerce._save | |
| def save(*args, **kwargs): | |
| if stage == 'response_received_before_persistence' and args[2] == 'SUCCEEDED': | |
| os._exit(77) | |
| result = original(*args, **kwargs) | |
| if stage == 'executing_before_provider' and args[2] == 'EXECUTING': | |
| os._exit(77) | |
| return result | |
| commerce._save = save | |
| if stage == 'provider_committed_before_receipt': | |
| original = provider.call | |
| def call(*args, **kwargs): | |
| result = original(*args, **kwargs) | |
| if args[2] == 'checkout': | |
| os._exit(77) | |
| return result | |
| provider.call = call | |
| result = commerce.execute(actor, action['id'], 'original-attempt') | |
| if stage == 'success_persisted_before_response' and result['data']['state'] == 'SUCCEEDED': | |
| os._exit(77) | |
| raise AssertionError('Expected crash boundary was not reached') | |
| def run(output, trials=10): | |
| report = {'schema_version': 1, 'source': 'controlled_simulation', 'protocol': 'commerce-crash-matrix-v2', | |
| 'trials_per_boundary': trials, 'expected_cases': trials*len(STAGES), 'passed': False, 'cases': [], | |
| 'limitations': ['Subprocess os._exit at declared boundaries, not a machine or disk failure.', | |
| 'Provider effects are persisted simulations; no upstream exactly-once claim.', | |
| 'No receipt after pre-provider crash remains UNKNOWN and requires human resolution.']} | |
| with isolated_database() as url: | |
| store = Store(url); store.migrate() | |
| report['database_backend'] = store.engine.dialect.name | |
| try: | |
| for stage in STAGES: | |
| for trial in range(trials): | |
| identity = Identity(store) | |
| actor = identity.provision('crash_' + uuid.uuid4().hex, 'synthetic-crash-password-123') | |
| commerce = Commerce(store, Provider(store, identity)) | |
| cart = commerce.propose(actor, {'provider': 'im', 'tool': 'update_cart', 'mode': 'replay', | |
| 'arguments': {'selectedAddressId': 'scenario-address', 'items': [ | |
| {'spinId': 'scenario-milk-0', 'skuId': 'scenario-sku-milk-0', 'quantity': 1}]}}) | |
| approved = commerce.approve(actor, cart['id'], cart['version']) | |
| assert commerce.execute(actor, approved['id'], 'prepare')['data']['state'] == 'SUCCEEDED' | |
| probe = store.create('crash_probe', actor['id'], {'stage': stage}) | |
| env = {**os.environ, 'DATABASE_URL': url, 'HYPERFLOW_ENABLE_LLM': '0', 'REDIS_URL': ''} | |
| started = time.perf_counter() | |
| process = subprocess.run([sys.executable, str(Path(__file__).resolve()), '--child', stage, | |
| '--account', actor['id'], '--probe', probe['id']], env=env, capture_output=True, timeout=30) | |
| assert process.returncode == 77, (stage, process.returncode, process.stderr.decode()[-800:]) | |
| data = store.get('crash_probe', probe['id'], actor['id'])['data'] | |
| if stage == 'proposal_persisted': | |
| proposal = store.get('proposal', data['proposal_id'], actor['id']) | |
| assert proposal['data']['state'] == 'PROPOSED' | |
| action = commerce.approve(actor, proposal['id'], proposal['version']) | |
| else: | |
| action = store.get('action', data['action_id'], actor['id']) | |
| initial_state = action['data']['state'] | |
| first = commerce.execute(actor, action['id'], 'original-attempt')['data']['state'] | |
| if stage in ('provider_committed_before_receipt', 'response_received_before_persistence'): | |
| assert first == 'UNKNOWN' | |
| final = commerce.reconcile(actor, action['id'])['data']['state'] | |
| elif stage == 'executing_before_provider': | |
| assert first == 'UNKNOWN' | |
| final = commerce.reconcile(actor, action['id'])['data']['state'] | |
| assert final == 'UNKNOWN' | |
| else: | |
| final = first | |
| again = commerce.execute(actor, action['id'], 'original-attempt')['data']['state'] | |
| receipts = len(store.list('simulation', actor['id'])[0]['data']['receipts']) | |
| expected_receipts = 0 if stage == 'executing_before_provider' else 1 | |
| assert receipts == expected_receipts and again == final | |
| assert final == ('UNKNOWN' if stage == 'executing_before_provider' else 'SUCCEEDED') | |
| report['cases'].append({'boundary': stage, 'trial': trial+1, 'abrupt_exit_code': process.returncode, | |
| 'initial_state': initial_state, 'retry_state': first, 'final_state': final, | |
| 'receipts': receipts, 'duplicate_receipts': 0, 'passed': True, | |
| 'elapsed_seconds': time.perf_counter()-started}) | |
| output.parent.mkdir(parents=True, exist_ok=True) | |
| output.write_text(json.dumps(report, indent=2)+'\n') | |
| finally: | |
| store.engine.dispose() | |
| report['passed'] = len(report['cases']) == trials*len(STAGES) and trials == 10 | |
| report['completed_cases'] = len(report['cases']) | |
| files = [Path(__file__), ROOT/'backend/flagship/commerce.py', ROOT/'backend/flagship/provider.py', ROOT/'backend/flagship/store.py'] | |
| report['source_sha256'] = {str(p.relative_to(ROOT)): hashlib.sha256(p.read_bytes()).hexdigest() for p in files} | |
| output.write_text(json.dumps(report, indent=2)+'\n') | |
| print(json.dumps({'passed': report['passed'], 'cases': len(report['cases']), 'database': report['database_backend']})) | |
| return report | |
| if __name__ == '__main__': | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('--output', type=Path) | |
| parser.add_argument('--trials', type=int, default=10, choices=range(1,11)) | |
| parser.add_argument('--child', choices=STAGES) | |
| parser.add_argument('--account'); parser.add_argument('--probe') | |
| args = parser.parse_args() | |
| if args.child: | |
| child(args.child, args.account, args.probe) | |
| elif args.output: | |
| result = run(args.output, args.trials) | |
| raise SystemExit(0 if result['passed'] or args.trials < 10 else 1) | |
| else: | |
| parser.error('--output is required for parent execution') | |