validate_contract_fixtures.py
1"""Check Agent contract artifacts without importing or executing the platform.23Supports exactly the JSON Schema keywords used by the local Agent schema; rejects4unknown assertions. This is not a general Draft 2020-12 implementation, runtime5acceptance test, sandbox proof or provider integration test. Uses only stdlib.6"""78import json9import math10import re11from decimal import Decimal12from pathlib import Path131415ROOT = Path(__file__).resolve().parents[1]16ARTIFACTS = ROOT / 'tests' / 'fixtures' / 'agent-contracts'17KEYWORDS = {18 '$schema',19 '$id',20 '$defs',21 '$ref',22 'title',23 'description',24 'type',25 'enum',26 'const',27 'properties',28 'required',29 'additionalProperties',30 'items',31 'minItems',32 'maxItems',33 'uniqueItems',34 'minimum',35 'maximum',36 'minLength',37 'maxLength',38 'pattern',39 'oneOf',40 'anyOf',41}424344def object_pairs(pairs):45 result = {}46 for key, value in pairs:47 if key in result:48 raise ValueError('duplicate JSON key: ' + key)49 result[key] = value50 return result515253def load(path):54 def nonfinite(value):55 raise ValueError('nonfinite JSON: ' + value)5657 return json.loads(path.read_text(), object_pairs_hook=object_pairs, parse_constant=nonfinite)585960def inspect_schema(node, root):61 unknown = set(node) - KEYWORDS62 if unknown:63 raise ValueError('unsupported schema keyword: ' + repr(unknown))64 if '$ref' in node:65 name = node['$ref'][8:]66 assert node['$ref'] == '#/$defs/' + name and name in root['$defs']67 for key in ('$defs', 'properties'):68 for child in node.get(key, {}).values():69 inspect_schema(child, root)70 for key in ('oneOf', 'anyOf'):71 for child in node.get(key, []):72 inspect_schema(child, root)73 for key in ('items', 'additionalProperties'):74 if isinstance(node.get(key), dict):75 inspect_schema(node[key], root)767778def valid(value, node, root):79 if '$ref' in node:80 return valid(value, root['$defs'][node['$ref'][8:]], root)81 if 'oneOf' in node and sum(valid(value, sub, root) for sub in node['oneOf']) != 1:82 return False83 if 'anyOf' in node and not any(valid(value, sub, root) for sub in node['anyOf']):84 return False85 if 'const' in node and (type(value) is not type(node['const']) or value != node['const']):86 return False87 if 'enum' in node and not any(88 type(value) is type(item) and value == item for item in node['enum']89 ):90 return False91 expected = node.get('type')92 types = {93 'null': value is None,94 'boolean': type(value) is bool,95 'integer': type(value) is int,96 'number': type(value) in (int, float) and math.isfinite(value),97 'string': isinstance(value, str),98 'array': isinstance(value, list),99 'object': isinstance(value, dict),100 }101 if expected and not types[expected]:102 return False103 if isinstance(value, str):104 if not node.get('minLength', 0) <= len(value) <= node.get('maxLength', math.inf):105 return False106 if 'pattern' in node and re.search(node['pattern'], value) is None:107 return False108 if type(value) in (int, float):109 if not node.get('minimum', -math.inf) <= value <= node.get('maximum', math.inf):110 return False111 if isinstance(value, list):112 if not node.get('minItems', 0) <= len(value) <= node.get('maxItems', math.inf):113 return False114 if node.get('uniqueItems') and len({json.dumps(v, sort_keys=True) for v in value}) != len(115 value116 ):117 return False118 if 'items' in node and not all(valid(v, node['items'], root) for v in value):119 return False120 if isinstance(value, dict):121 if any(key not in value for key in node.get('required', [])):122 return False123 props = node.get('properties', {})124 for key, item in value.items():125 if key in props:126 if not valid(item, props[key], root):127 return False128 elif node.get('additionalProperties') is False:129 return False130 elif isinstance(node.get('additionalProperties'), dict):131 if not valid(item, node['additionalProperties'], root):132 return False133 return True134135136def check_formula(check):137 """Independent arithmetic/cutoff oracle, not the platform implementation."""138 kind, args, expected = check['kind'], check['input'], check['expected']139 if kind == 'interval':140 actual = args['finished'] + args['interval']141 elif kind == 'timeout_remaining':142 actual = args['timeout'] - args['active_elapsed']143 elif kind == 'partial_cleanup':144 actual = {145 'spent': args['filled'] * (args['price'] + args['fee']),146 'released': (args['quantity'] - args['filled']) * (args['limit'] + args['fee']),147 'held': args['filled'],148 }149 elif kind == 'available_cash':150 actual = args['cash'] - sum(q * (p + args['fee']) for q, p in args['open_orders'])151 elif kind == 'available_shares':152 actual = args['held'] - sum(args['reserved'])153 elif kind == 'shared_supply':154 remaining, actual = args['supply'], []155 for quantity in args['requested']:156 fill = min(remaining, quantity)157 actual.append(fill)158 remaining -= fill159 elif kind == 'effective_time':160 actual = max(args['receipt_target'], args['executed_target'])161 elif kind == 'source_prefix':162 stamps = args['timestamps']163 if any(a > b for a, b in zip(stamps, stamps[1:])):164 actual = 'SOURCE_ERROR'165 else:166 actual = [167 i + 1 for i, t in enumerate(stamps) if t <= args['cutoff'] and t < args['end']168 ]169 elif kind == 'bar_cutoff':170 observed = min(args['cutoff'], args['period_end'])171 actual = {'observed_through': observed, 'complete': observed == args['period_end']}172 elif kind == 'boundary_order':173 order = {174 'fault': 0,175 'end': 1,176 'stop': 2,177 'pause': 3,178 'market': 4,179 'model_timeout': 5,180 'model_reply': 6,181 'tool': 7,182 'wake': 8,183 }184 actual = sorted(args['events'], key=lambda event: (order[event['kind']], event['receipt']))185 elif kind == 'attempt_valid':186 actual = (187 args['reply_attempt'] == args['current_attempt']188 and args['reply_active'] < args['deadline']189 and not args['stopped']190 )191 elif kind == 'retry_delay':192 actual = min(args['cap'], args['base'] * 2 ** (args['failed_attempt'] - 1))193 elif kind == 'dedup':194 actual = (195 'original_result'196 if args['old_payload'] == args['new_payload']197 else 'REQUEST_ID_CONFLICT'198 )199 else:200 raise ValueError('unknown arithmetic check: ' + kind)201 assert actual == expected, (kind, actual, expected)202203204def semantic_error(value):205 """Check standalone invariants; owner-dependent checks remain runtime gates."""206 if 'integration' in value:207 if int(value['revision']) < 1:208 return 'INVALID_ARGUMENT'209 schedule = value.get('schedule', {})210 if schedule.get('mode') == 'interval' and Decimal(schedule['interval_seconds']) <= 0:211 return 'INVALID_ARGUMENT'212 elif 'definition' in value:213 if not 0 <= int(value['start_ns']) < int(value['end_ns']) < 86_400_000_000_000:214 return 'INVALID_ARGUMENT'215 if Decimal(value['timeout_seconds']) <= 0:216 return 'INVALID_ARGUMENT'217 retry = value['retry']218 if Decimal(retry['base_delay_seconds']) > Decimal(retry['max_delay_seconds']):219 return 'INVALID_ARGUMENT'220 if (221 Decimal(value['lag']['threshold_seconds']) <= 0222 or Decimal(value['lag']['window_seconds']) <= 0223 ):224 return 'INVALID_ARGUMENT'225 if not set(value['definition']['requires']) <= set(value['granted_tools']):226 return 'TOOL_UNAVAILABLE'227 return semantic_error(value['definition'])228 elif 'arguments' in value:229 if int(value['epoch']) < 1:230 return 'INVALID_ARGUMENT'231 tool, args = value['tool'], value['arguments']232 if 'symbol' in args and args['symbol'] != args['symbol'].strip():233 return 'INVALID_ARGUMENT'234 if tool == 'orders.submit_limit':235 price = Decimal(args['limit_price_usd']) * 10000236 if price <= 0 or price * args['quantity_shares'] > 2**63 - 1:237 return 'INVALID_ARGUMENT'238 if (239 tool == 'market.bars'240 and not 0 <= int(args['start_ns']) < int(args['end_ns']) < 86_400_000_000_000241 ):242 return 'INVALID_ARGUMENT'243 if tool == 'control.wait':244 if any(c['kind'] == 'after' and Decimal(c['seconds']) <= 0 for c in args['conditions']):245 return 'INVALID_ARGUMENT'246 elif value.get('status') == 'ok':247 data = value['data']248 if value['tool'] == 'orders.submit_limit':249 if (250 data['outcome'] == 'accepted'251 and (data['order'] is None or data['reason_code'] is not None)252 or data['outcome'] == 'rejected'253 and data['reason_code'] is None254 ):255 return 'INTERNAL_STATE_ERROR'256 orders = []257 if value['tool'] == 'orders.get':258 orders = [data]259 elif value['tool'] in ('orders.submit_limit', 'orders.cancel') and data.get('order'):260 orders = [data['order']]261 elif value['tool'] == 'orders.list':262 orders = data['items']263 for order in orders:264 if (265 order['filled_shares'] > order['quantity_shares']266 or order['remaining_shares'] != order['quantity_shares'] - order['filled_shares']267 ):268 return 'INTERNAL_STATE_ERROR'269 if order['status'] == 'filled' and order['remaining_shares'] != 0:270 return 'INTERNAL_STATE_ERROR'271 return None272273274def main():275 schema = load(ARTIFACTS / 'contracts.schema.json')276 inspect_schema(schema, schema)277 examples = load(ARTIFACTS / 'wire-examples.json')['examples']278 assert len({case['id'] for case in examples}) == len(examples)279 for case in examples:280 assert valid(case['value'], schema, schema) == case['valid'], case['id']281 if case['valid']:282 assert semantic_error(case['value']) == case.get('semantic_error'), case['id']283 # Negative controls demonstrate rejection rather than only successful parsing.284 for raw in ('{"x":1,"x":2}', '{"x":NaN}', '{"x":Infinity}'):285 try:286 json.loads(287 raw,288 object_pairs_hook=object_pairs,289 parse_constant=lambda value: (_ for _ in ()).throw(ValueError(value)),290 )291 except ValueError:292 continue293 raise AssertionError('invalid JSON was accepted')294 cases = load(ARTIFACTS / 'semantic-cases.json')['cases']295 ids = [case['id'] for case in cases]296 assert ids == ['C%02d' % i for i in range(1, 29)], ids297 count = 0298 for case in cases:299 assert case['events'] and case['expected'] and case['requirements']300 for event in case['events']:301 assert set(event) == {'at_active_ns', 'kind', 'payload'}302 assert re.fullmatch(r'0|[1-9][0-9]*', event['at_active_ns'])303 for check in case['checks']:304 check_formula(check)305 count += 1306 print(307 'PASS: %d wire examples; %d semantic case records; %d independent arithmetic/order checks; strict JSON negative controls'308 % (len(examples), len(cases), count)309 )310 print(311 'Not executed: runtime traces, provider, SQL/native faults, OS isolation, real-time pacing or Replay integration.'312 )313314315if __name__ == '__main__':316 main()