// Offline check of a declared LLM routing plan. It reads one local JSON file. import {readFileSync,statSync} from 'node:fs'; import {pathToFileURL} from 'node:url'; const regionNames=new Intl.DisplayNames('en',{type:'region',fallback:'none'}); const isCountry=value=>typeof value==='string'&&/^[A-Z]{2}$/.test(value) && !['EU','UN','XA','XB','XK','ZZ'].includes(value)&&Boolean(regionNames.of(value)); const isObject=value=>value!==null&&typeof value==='object'&&!Array.isArray(value); const isDays=value=>Number.isSafeInteger(value)&&value>=0; const isEvidence=value=>{ if(typeof value!=='string')return false; try{ const url=new URL(value); return url.protocol==='https:'&&!url.username&&!url.password; }catch{return false;} }; export function checkRoutePlan(plan){ if(!isObject(plan)||plan.version!==1)throw Error('Use a version 1 routing plan object.'); const {policy,primary,fallbacks,telemetry,failureProbe}=plan; if(!isObject(policy)||!Array.isArray(policy.allowedCountries)||!policy.allowedCountries.length ||!policy.allowedCountries.every(isCountry)||!isDays(policy.maxContentRetentionDays)){ throw Error('Set allowedCountries to specific country codes (such as FR or DE, not EU) and maxContentRetentionDays to a whole number.'); } const issues=[]; const add=(field,kind,detail)=>issues.push({field,kind,detail}); const allowed=new Set(policy.allowedCountries); const check=(name,route)=>{ if(!isObject(route)){add(name,'missing_evidence','Record the declared route and its published or contractual evidence.');return;} if(!isCountry(route.country))add(name+'.country','missing_evidence','Name the country, not a broad region or an unknown value.'); else if(!allowed.has(route.country))add(name+'.country','policy_mismatch','The declared country is not permitted by this plan.'); if(!isDays(route.contentRetentionDays))add(name+'.contentRetentionDays','missing_evidence','Record the retention period for request or response content.'); else if(route.contentRetentionDays>policy.maxContentRetentionDays)add(name+'.contentRetentionDays','policy_mismatch','Declared retention exceeds the allowance.'); if(!isEvidence(route.evidenceUrl))add(name+'.evidenceUrl','missing_evidence','Add an HTTPS reference with no embedded credentials.'); }; check('primary',primary); for(const [name,items] of [['fallbacks',fallbacks],['telemetry',telemetry]]){ if(items===null||items===undefined)add(name,'missing_evidence','List every applicable route or use an empty array only if none exist.'); else if(!Array.isArray(items))throw Error(name+' must be an array or null.'); else items.forEach((route,i)=>check(name+'['+i+']',route)); } if(failureProbe==='outside_policy_retry')add('failureProbe','policy_mismatch','The recorded failure retried outside policy.'); else if(failureProbe!=='error_without_retry')add('failureProbe','missing_evidence','Record the fail-closed staging result; do not infer it from the primary route.'); return { status:issues.some(issue=>issue.kind==='policy_mismatch')?'policy_mismatch':issues.length?'needs_evidence':'matches_declared_policy', issues, scope:'Checks the information you supplied. It does not contact providers, inspect dashboards, validate evidence links, check metadata retention or certify residency or compliance.' }; } if(process.argv[1]&&import.meta.url===pathToFileURL(process.argv[1]).href){ try{ if(process.argv.length!==3)throw Error('Usage: node llm-route-check.mjs llm-route-plan.json'); const file=process.argv[2],info=statSync(file); if(!info.isFile()||info.size>65536)throw Error('Use a local JSON file no larger than 64 KiB.'); const result=checkRoutePlan(JSON.parse(readFileSync(file,'utf8').replace(/^\uFEFF/,''))); console.log(JSON.stringify(result,null,2)); process.exitCode=result.status==='matches_declared_policy'?0:1; }catch(error){ console.error(error instanceof SyntaxError?'Invalid routing plan JSON.':error.message); process.exitCode=2; } }