36 lines
2.6 KiB
TypeScript
36 lines
2.6 KiB
TypeScript
import { BadRequestException } from '@nestjs/common';
|
|
import { createHash } from 'crypto';
|
|
export const TECH_PILOT_KIND = 'tech-dev-clamp-quantity-v1';
|
|
export function admittedTechTask(t: any): boolean {
|
|
return !!t && t.key === TECH_PILOT_KIND && t.department === 'TECH' &&
|
|
t.operationMode === 'PREPARE' && t.budgetLimitCents === 20 &&
|
|
t.metadata?.scope === TECH_PILOT_KIND && t.metadata?.workflowScope === 'STAGE_PREPARE' &&
|
|
t.metadata?.environment === 'staging' && /^[a-f0-9]{40}$/.test(t.metadata?.commitSha ?? '');
|
|
}
|
|
/** Separate bounded recognizer/interpreter. No Python, eval, subprocess or model QA claims. */
|
|
export function validateTechSource(source: string) {
|
|
const deny = (): never => { throw new BadRequestException('Only bounded clamp_quantity min/max fixture source passing QA9 accepted'); };
|
|
if(typeof source !== 'string' || Buffer.byteLength(source, 'utf8') > 2048) deny();
|
|
const body = /^def clamp_quantity\(quantity\):[ \t]*\r?\n[ \t]+return ([^\r\n]+)[ \t]*\r?\n?$/.exec(source);
|
|
if(!body) deny();
|
|
const text = body![1]; let pos=0, nodes=0;
|
|
type Expr = {kind:'q'} | {kind:'n';value:number} | {kind:'min'|'max';left:Expr;right:Expr};
|
|
const ws=()=>{while(text[pos]===' ' || text[pos]==='\t') pos++;};
|
|
function parse(depth:number): Expr {
|
|
if(depth>32 || ++nodes>128) return deny(); ws();
|
|
if(text.slice(pos,pos+8)==='quantity') {pos+=8;return {kind:'q'};}
|
|
const number=/^(0|[1-9][0-9]?)(?![0-9])/.exec(text.slice(pos));
|
|
if(number) {pos+=number[0].length;return {kind:'n',value:Number(number[0])};}
|
|
const name=text.slice(pos,pos+3); if(name!=='min' && name!=='max') return deny(); pos+=3;ws();
|
|
if(text[pos++]!=='(') return deny(); const left=parse(depth+1);ws();if(text[pos++]!==',') return deny();
|
|
const right=parse(depth+1);ws();if(text[pos++]!==')') return deny();return {kind:name,left,right};
|
|
}
|
|
const tree=parse(0);ws();if(pos!==text.length) deny();
|
|
function evaluate(n:Expr,q:number): number {if(n.kind==='q') return q;if(n.kind==='n') return n.value;const a=evaluate(n.left,q),b=evaluate(n.right,q);return n.kind==='min'?Math.min(a,b):Math.max(a,b);}
|
|
for(const q of [-1000,-1,0,1,50,98,99,100,1e30]) if(evaluate(tree,q)!==Math.max(0,Math.min(q,99))) deny();
|
|
return {passed:true,cases:9,validator:'bounded-minmax-v1',scope:'quantity clamp fixture only',sourceSha256:createHash('sha256').update(source).digest('hex')};
|
|
}
|
|
export function techArtifact(binding:{taskId:string;runId:string;commitSha:string},source:string) {
|
|
return JSON.stringify({pilotKind:TECH_PILOT_KIND,schemaVersion:1,...binding,environment:'staging',source,applicationQa:false,costStatus:'upper-bound-reserved',retainedReservationCents:20,qa:validateTechSource(source)});
|
|
}
|