|
| 1 | +"""Grade observable outcomes without prescribing a particular repair.""" |
| 2 | + |
| 3 | +import json |
| 4 | + |
| 5 | + |
| 6 | +def grade(observations, stack): |
| 7 | + criteria = [] |
| 8 | + |
| 9 | + def result(name, passed, evidence): |
| 10 | + criteria.append({'id': name, 'passed': bool(passed), 'evidence': evidence}) |
| 11 | + |
| 12 | + records, malformed = [], [] |
| 13 | + for stream in ('stdout', 'stderr'): |
| 14 | + for line in observations[stream].splitlines(): |
| 15 | + if not line.strip(): |
| 16 | + continue |
| 17 | + try: |
| 18 | + record = json.loads(line) |
| 19 | + if not isinstance(record, dict) or not isinstance(record.get('event'), str): |
| 20 | + raise ValueError('expected JSON object with string event') |
| 21 | + records.append(record) |
| 22 | + except (ValueError, TypeError): |
| 23 | + malformed.append(line) |
| 24 | + result('json_output', bool(records) and not malformed, |
| 25 | + {'records': len(records), 'malformed_lines': malformed}) |
| 26 | + |
| 27 | + responses = observations['responses'] |
| 28 | + labels = {'health', 'success', 'failure', 'invalid', 'parallel_success', 'parallel_failure'} |
| 29 | + business_errors = [] |
| 30 | + for response in responses: |
| 31 | + label = response['label'] |
| 32 | + expected_status = 200 if label == 'health' else 422 if label == 'invalid' else 503 if response['mode'] == 'timeout' else 201 |
| 33 | + expected_body = ({'status': 'ok'} if label == 'health' else |
| 34 | + {'detail': 'provider unavailable'} if expected_status == 503 else |
| 35 | + {'order_id': response['order_id'], 'status': 'created'}) |
| 36 | + if response['status'] != expected_status or (expected_status != 422 and response['body'] != expected_body): |
| 37 | + business_errors.append(response) |
| 38 | + if not response['request_id'] or (response['expected_request_id'] and response['request_id'] != response['expected_request_id']): |
| 39 | + business_errors.append(response) |
| 40 | + result('http_contract', len(responses) == len(labels) and {r['label'] for r in responses} == labels and not business_errors, |
| 41 | + business_errors or responses) |
| 42 | + provider = observations['provider'] |
| 43 | + result('provider_behavior', provider == { |
| 44 | + 'success': {'order_id': 'direct', 'status': 'created'}, |
| 45 | + 'failure': {'type': 'ProviderTimeout', |
| 46 | + 'message': 'provider timed out; credential=TEST_SECRET_PAYMENT'}, |
| 47 | + }, provider) |
| 48 | + |
| 49 | + successful = [r for r in responses if r['label'] in ('success', 'parallel_success')] |
| 50 | + failed = [r for r in responses if r['label'] in ('failure', 'parallel_failure')] |
| 51 | + created = [r for r in records if r['event'] == 'order.created'] |
| 52 | + failures = [r for r in records if r['event'] == 'order.failed'] |
| 53 | + completed = [r for r in records if r['event'] == 'request.completed'] |
| 54 | + result('event_contracts', len(created) == 2 and len(failures) >= 2 and len(completed) == 6, |
| 55 | + {'order.created': len(created), 'order.failed': len(failures), 'request.completed': len(completed)}) |
| 56 | + variable_names = [r['event'] for r in records if any( |
| 57 | + value and value in r['event'] for response in responses |
| 58 | + for value in (response['order_id'], response['request_id']))] |
| 59 | + result('stable_events', not variable_names, {'variable_events': variable_names}) |
| 60 | + |
| 61 | + field_errors = [] |
| 62 | + for response in successful: |
| 63 | + matches = [r for r in created if r.get('order_id') == response['order_id']] |
| 64 | + if len(matches) != 1 or matches[0].get('amount_cents') != response['amount_cents']: |
| 65 | + field_errors.append(response['label']) |
| 66 | + for response in failed: |
| 67 | + if not any(r.get('order_id') == response['order_id'] and r.get('retryable') is True for r in failures): |
| 68 | + field_errors.append(response['label']) |
| 69 | + result('structured_fields', not field_errors, {'missing_or_wrong_fields': field_errors}) |
| 70 | + |
| 71 | + # Count all ERROR/CRITICAL records, not only the contracted event name. |
| 72 | + error_records = [r for r in records if str(r.get('level', r.get('log_level', r.get('levelname', '')))).lower() in ('error', 'critical')] |
| 73 | + ownership_errors = [] |
| 74 | + for response in failed: |
| 75 | + matches = [r for r in error_records if r.get('order_id') == response['order_id']] |
| 76 | + if len(matches) != 1 or matches[0]['event'] != 'order.failed': |
| 77 | + ownership_errors.append(response['label']) |
| 78 | + elif 'ProviderTimeout' not in json.dumps(matches[0]) or 'Traceback' not in json.dumps(matches[0]): |
| 79 | + ownership_errors.append(response['label']) |
| 80 | + result('single_failure_traceback', len(error_records) == 2 and not ownership_errors, |
| 81 | + {'error_records': len(error_records), 'invalid_failures': ownership_errors}) |
| 82 | + |
| 83 | + raw = observations['stdout'] + observations['stderr'] |
| 84 | + markers = [marker for marker in ('TEST_SECRET_HEADER', 'TEST_SECRET_PAYMENT', 'TEST_SECRET_DEFAULT') if marker in raw] |
| 85 | + result('no_secrets', not markers, {'leaked_markers': markers}) |
| 86 | + |
| 87 | + correlation_errors = [] |
| 88 | + for response in responses: |
| 89 | + matches = [r for r in completed if r.get('request_id') == response['request_id']] |
| 90 | + if len(matches) != 1 or matches[0].get('status_code') != response['status']: |
| 91 | + correlation_errors.append(response['label']) |
| 92 | + if response in successful + failed: |
| 93 | + event = 'order.created' if response in successful else 'order.failed' |
| 94 | + matches = [r for r in records if r['event'] == event and r.get('order_id') == response['order_id']] |
| 95 | + if not matches or any(r.get('request_id') != response['request_id'] for r in matches): |
| 96 | + correlation_errors.append(response['label']) |
| 97 | + result('request_isolation', not correlation_errors, {'wrong_request_context': correlation_errors}) |
| 98 | + probes = [r for r in records if r['event'] == 'demo.probe'] |
| 99 | + result('context_cleanup', len(probes) == 6 and {r.get('probe_id') for r in probes} == labels and |
| 100 | + all(r.get('request_id') is None for r in probes), {'probes': probes}) |
| 101 | + usage = observations['backend_calls'] |
| 102 | + preserved = usage[stack] > 0 and (stack != 'stdlib' or usage['structlog'] == 0) |
| 103 | + result('original_stack', preserved, usage) |
| 104 | + return criteria |
0 commit comments