"""Pilot input quality and agent-contract consistency. Does not certify semantics or execute tools.""" from pathlib import Path from jsonschema import Draft202012Validator from build_validate import load,need,schema_check def inspect_schema(schema): Draft202012Validator.check_schema(schema) def walk(x): if isinstance(x,dict): if '$ref' in x:need(x['$ref'].startswith('#/'),'Untrusted external schema reference') need('$dynamicRef' not in x,'Unsupported dynamic schema reference') for key,value in x.items(): if key not in ['const','enum','examples','default']:walk(value) elif isinstance(x,list): for value in x:walk(value) walk(schema) def check(bag,standard_root=None): root=Path(standard_root) if standard_root else Path(__file__).resolve().parents[1];data=Path(bag)/'data';c=load(data/'build/contract.json') a=load(data/'build/agent-runtime.json');plan=load(data/'build/pilot-plan.json');pkg=load(data/'package.json') need({'build_pilot_v1','agent_runtime_contracts_v1'}<=set(pkg['required_features']),'Missing required pilot feature declarations') schema_check(load(root/'schemas/agent_runtime.schema.json'),a);schema_check(load(root/'schemas/pilot_plan.schema.json'),plan) need(a['package_id']==plan['package_id']==pkg['package_id'],'Pilot package identity mismatch') reqs=set(c['scope']['requirement_ids']) groups=['contexts','skills','tools','brains','agents'];idx={} for name in groups: idx[name]={r['id']:r for r in a[name]};need(len(idx[name])==len(a[name]),'Duplicate agent-contract '+name) cats={load(data/p)['kind']:load(data/p)['records'] for p in pkg['canonical_catalogs']} original_agents={r['id']:r for r in cats.get('agents',[])};original_tools={r['id']:r for r in cats.get('tools',[])} registered={r['path'] for r in pkg['resources']} for ctx in a['contexts']: if ctx['source_kind']=='memory':need(ctx['trust_class']=='derived_memory' and ctx['memory_admission']=='never_authority','Memory promoted to authority') if ctx['source_kind']=='package':need(ctx['locator'].startswith('package:') and ctx['locator'][8:] in registered,'Invalid package context locator') if ctx['trust_class']=='normative':need(ctx['source_kind']=='package','Only the pinned package defines normative authority') for skill in a['skills']: need(set(skill['context_ids'])<=set(idx['contexts']) and set(skill['tool_ids'])<=set(idx['tools']),'Skill dependency unresolved') need(set(skill['requirement_ids'])<=reqs,'Skill requirement unresolved') inspect_schema(skill['output_schema']) for tool in a['tools']: for k in ['input_schema','output_schema']:inspect_schema(tool[k]) if tool['source_catalog_id'] is not None: need(tool['source_catalog_id'] in original_tools,'Runtime tool catalog binding missing') old=original_tools[tool['source_catalog_id']] need(tool['input_schema']==old['input_schema'] and tool['output_schema']==old['output_schema'] and tool['effect']==old['effect'],'Runtime tool differs from canonical catalog') need({t['source_catalog_id'] for t in a['tools'] if t['plane']=='product_runtime'}==set(original_tools),'Runtime tool coverage gap') need({r['id'] for r in a['agents'] if r['plane']=='product_runtime'}==set(original_agents),'Runtime agent coverage gap') for agent in a['agents']: need(agent['skill_ids'] and agent['context_ids'] and agent['tool_ids'],'Empty agent dependency set') need(agent['brain_id'] in idx['brains'],'Unknown brain binding');brain=idx['brains'][agent['brain_id']] need(brain['plane']==agent['plane'],'Cross-plane brain binding') for field,kind in [('skill_ids','skills'),('context_ids','contexts'),('tool_ids','tools')]: need(set(agent[field])<=set(idx[kind]),'Unknown agent dependency') need(all(idx[kind][x]['plane']==agent['plane'] for x in agent[field]),'Cross-plane dependency') need(set(agent['requirement_ids'])<=reqs,'Agent requirement unresolved') for sid in agent['skill_ids']: skill=idx['skills'][sid];need(set(skill['tool_ids'])<=set(agent['tool_ids']),'Skill expands agent tool authority') need(set(skill['context_ids'])<=set(agent['context_ids']),'Skill context not declared by agent') if agent['plane']=='product_runtime': old=original_agents[agent['id']];need(set(agent['tool_ids'])==set(old['tool_refs']),'Agent tool allowlist drift') need(brain['max_tool_calls_per_run']<=old['limits']['max_tool_calls'],'Brain budget exceeds canonical runtime policy') # Waivers are linked to concrete cases and bounded decisions, not free-text blanket exemptions. decisions={d['id'] for d in c['decisions']} for uc in c['use_cases']: cases={s['id']:s for s in uc['scenarios']} ops=[o for o in c['operations'] if o['id'] in uc['operation_ids']] for waiver in uc['coverage_decisions']: need(set(waiver['scenario_ids'])<=set(cases),'Coverage evidence points outside use case') if waiver['applies']: need(waiver['basis']=='present' and bool(waiver['scenario_ids']),'Applicable category without evidence') need(all(cases[x]['category']==waiver['category'] for x in waiver['scenario_ids']),'Wrong category evidence') elif waiver['basis']=='covered_elsewhere': need(bool(waiver['scenario_ids']) and waiver['decision_ref'] in decisions,'Unbound coverage exemption') else: need(waiver['basis']=='not_relevant' and waiver['decision_ref'] in decisions,'Unknown exemption basis') need(all(op['kind']=='verification' for op in ops),'HTTP/control category cannot be waived as irrelevant') for s in uc['scenarios']: if s['category']=='primary': meaningful=[e for e in s['expected'] if e['pointer'] not in ['/status','/code','/committed_effects']] need(bool(meaningful),'Primary scenario lacks domain observation') for phase in plan['phases']: need(set(phase['requirement_ids'])<=reqs,'Pilot phase scope drift') need(set(phase['release_ids'])<={r['id'] for r in c['releases']},'Pilot release unknown') need({x for p in plan['phases'] for x in p['requirement_ids']}==reqs,'Pilot omits product scope') return {'pilot_ready':True,'status':'pilot_candidate','agent_count':len(a['agents']),'runtime_agents':len(original_agents),'skills':len(a['skills']),'tool_contracts':len(a['tools']),'phases':len(plan['phases']),'semantic_validation':'Requires the recorded independent build pilot; not claimed by these checks.'} if __name__=='__main__': import sys,json try: from build_validate import check as check_build check_build(sys.argv[1]) print(json.dumps(check(sys.argv[1])));sys.exit(0) except Exception as e:print(json.dumps({'pilot_ready':False,'error':str(e)}));sys.exit(2)