← back to Dw Yolo Loop

scripts/draft-abnormal-decode/draft-abnormal-decode.mjs

33 lines

// draft-abnormal-decode — READ-ONLY, $0. Cycle 83. Closes the c82 completeness gap: decode
// the DRAFT abnormal-samples (single-variant "Sample" at non-$0/non-$4.25) the active scope
// (c68 532) didn't cover. Per-vendor: count, price-signature (MAGNITUDE-gated per c82 —
// flat-ROLL-price=disco, flat-SAMPLE-price=benign-uniform, varied=mislabel), SKU prefix, era.
// Draft = off-storefront (lower live-severity) BUT the latent-activation landmine (c78): a
// draft defect ships live the instant it's published. READ-ONLY Admin GraphQL. EXCLUDES PJ.
import fs from 'fs';
const ENV=fs.readFileSync('/Users/macstudio3/Projects/secrets-manager/.env','utf8');
const TOK=((ENV.match(/^SHOPIFY_ADMIN_TOKEN=(.+)$/m)||[])[1]||'').replace(/['"\r]/g,'').trim();
const GQL='https://designer-laboratory-sandbox.myshopify.com/admin/api/2024-10/graphql.json';
const PJ=/phillip[- ]?jeffries/i;
const sleep=ms=>new Promise(r=>setTimeout(r,ms));
async function gql(q,v){for(let a=0;a<6;a++){const r=await fetch(GQL,{method:'POST',headers:{'X-Shopify-Access-Token':TOK,'Content-Type':'application/json'},body:JSON.stringify({query:q,variables:v})});if(r.status===429){await sleep(2000*(a+1));continue;}const j=await r.json();if(j.errors&&JSON.stringify(j.errors).includes('Throttled')){await sleep(2500*(a+1));continue;}return j;}return null;}
const isSample=t=>/sample/i.test(t||'');
const Q=`query($cursor:String){ products(first:100, after:$cursor, query:"status:draft"){ pageInfo{hasNextPage endCursor} edges{node{vendor handle variants(first:5){edges{node{title price sku createdAt}}}}} } }`;
let cursor=null,pages=0,scanned=0,complete=true; const hits=[];
while(pages<90){ const j=await gql(Q,{cursor}); if(!j?.data){complete=false;break;} const c=j.data.products;
  for(const e of c.edges){ const n=e.node; if(PJ.test(n.vendor||''))continue; scanned++; const vs=(n.variants?.edges||[]).map(x=>x.node); if(vs.length!==1)continue; const v=vs[0]; const p=parseFloat(v.price);
    if(isSample(v.title)&&!isNaN(p)&&p!==0&&p!==4.25) hits.push({vendor:n.vendor,h:n.handle,price:p,sku:v.sku||'',vc:(v.createdAt||'').slice(0,4)}); }
  if(!c.pageInfo.hasNextPage)break; cursor=c.pageInfo.endCursor; pages++; await sleep(150); }
console.log(`=== draft-abnormal-decode (READ-ONLY, $0) ===\nDRAFT scanned ${scanned} (complete=${complete}) | abnormal-sample ${hits.length}\n`);
// per-vendor signature: magnitude-gated (c82). roll-scale >= ~20; sample-scale < 20.
const byV={};
for(const h of hits){ const v=h.vendor||'(none)'; (byV[v]=byV[v]||{n:0,prices:new Set(),min:1e9,max:0,skuPfx:{},years:{}}); const b=byV[v]; b.n++; b.prices.add(h.price); b.min=Math.min(b.min,h.price); b.max=Math.max(b.max,h.price); const pfx=(h.sku.match(/^([A-Z]+-?\d{0,2})/i)||[,'?'])[1]; b.skuPfx[pfx]=(b.skuPfx[pfx]||0)+1; b.years[h.vc]=(b.years[h.vc]||0)+1; }
for(const [v,b] of Object.entries(byV).sort((a,b)=>b[1].n-a[1].n)){
  const distinct=b.prices.size; const rollScale=b.min>=20;
  const sig = distinct<=2 ? (rollScale?'FLAT-ROLL = likely DISCO/placeholder':'FLAT-SAMPLE = benign-uniform') : (rollScale?'VARIED-ROLL = mislabel':'VARIED-SAMPLE = sample-price-variance');
  console.log(`${v}: ${b.n} | $${b.min}-$${b.max} | ${distinct} distinct → ${sig}`);
  console.log(`    sku-prefix: ${JSON.stringify(Object.entries(b.skuPfx).sort((x,y)=>y[1]-x[1]).slice(0,4))} | years: ${JSON.stringify(b.years)}`);
}
fs.writeFileSync('/tmp/draft-abnormal-decode.json',JSON.stringify({ts:new Date().toISOString(),scanned,complete,total:hits.length,byVendor:Object.fromEntries(Object.entries(byV).map(([v,b])=>[v,{n:b.n,min:b.min,max:b.max,distinct:b.prices.size,years:b.years,skuPfx:b.skuPfx}]))},null,2));
console.log(`\nwrote /tmp/draft-abnormal-decode.json`);