← back to Dw Yolo Loop

scripts/continue-masked-segmentation/continue-masked-segmentation.mjs

60 lines

// continue-masked-segmentation — READ-ONLY, $0. Cycle 90. Closes the c89 (b) residual: of the active
// products (all buyable), which are buyable via POSITIVE REAL STOCK (benign) vs ONLY because
// inventoryPolicy=CONTINUE oversells a qty<=0 variant (the soft onboarding-defect population riding
// oversell — "buyable because we never say no"). Classification per product:
//   maxQty = max variant inventoryQuantity
//   real-stock  : maxQty > 0  (benign; sub-split: has the 2026 sentinel vs other-positive-stock)
//   CONTINUE-masked-only : maxQty <= 0  -> buyable ONLY via CONTINUE oversell = the (b) set.
// Note: the 2026 sentinel is a qty>0, so CONTINUE-masked-only is by construction missing-2026 (the
// never-properly-onboarded signal). NEGATIVE CONTROL: the genuinely-stocked bulk makes the masked-at-zero
// set a distinct quantifiable population, not the norm. 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||r.status>=500){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 Q=`query($cursor:String){ products(first:50, after:$cursor, query:"status:active"){ pageInfo{hasNextPage endCursor}
  edges{node{ title vendor productType
    variants(first:12){ edges{ node{ availableForSale inventoryQuantity inventoryPolicy } } } } } } }`;
let cursor=null,pages=0,scanned=0,complete=true;
let realStock=0, realStock2026=0, realStockOther=0, maskedOnly=0, noVar=0, maskedButHasDenyBuyable=0;
const maskedByVendor={}, maskedEx=[];
while(true){ 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===0){ noVar++; continue; }
    const maxQty=Math.max(...vs.map(v=>(typeof v.inventoryQuantity==='number'?v.inventoryQuantity:-1)));
    const has2026=vs.some(v=>v.inventoryQuantity===2026);
    if(maxQty>0){ realStock++; if(has2026) realStock2026++; else realStockOther++; }
    else {
      // maxQty<=0 : buyable only via CONTINUE oversell (the (b) set). Sanity: is there a buyable variant?
      const buyableVia=vs.filter(v=>v.availableForSale===true);
      const allContinue=buyableVia.length>0 && buyableVia.every(v=>v.inventoryPolicy==='CONTINUE');
      if(buyableVia.some(v=>v.inventoryPolicy==='DENY')) maskedButHasDenyBuyable++; // DENY+qty<=0+buyable = oddity
      maskedOnly++;
      maskedByVendor[n.vendor]=(maskedByVendor[n.vendor]||0)+1;
      if(maskedEx.length<30){ const summ=vs.map(v=>`${v.inventoryPolicy}/${v.inventoryQuantity}`).join(','); maskedEx.push({t:(n.title||'').slice(0,42),vendor:n.vendor,type:n.productType,vars:vs.length,allCont:allContinue,summ:summ.slice(0,64)}); }
    }
  }
  pages++; if(!c.pageInfo.hasNextPage)break; cursor=c.pageInfo.endCursor;
  const cost=j.extensions?.cost?.throttleStatus; if(cost&&cost.currentlyAvailable<500) await sleep(1500); else await sleep(250);
  if(pages%100===0) console.log(`  ${pages}p scanned=${scanned} masked-only=${maskedOnly}`);
}
const pct=x=>scanned?(100*x/scanned).toFixed(2):'0';
console.log(`\n=== continue-masked-segmentation (READ-ONLY, $0) ===`);
console.log(`active scanned: ${scanned} (complete=${complete}, ${pages}p) [PJ excluded]`);
console.log(`\nHOW each active product is BUYABLE:`);
console.log(`  REAL-STOCK (>=1 variant qty>0, benign): ${realStock} (${pct(realStock)}%)`);
console.log(`     - of which AT the 2026 sentinel: ${realStock2026} (${pct(realStock2026)}%)`);
console.log(`     - other positive stock (no 2026 sentinel, benign drift): ${realStockOther} (${pct(realStockOther)}%)`);
console.log(`  CONTINUE-MASKED-ONLY (max qty<=0, buyable ONLY via oversell = the (b) soft-defect set): ${maskedOnly} (${pct(maskedOnly)}%)`);
console.log(`  no-variants oddity: ${noVar}`);
console.log(`  (sanity) masked-but-a-DENY-variant-is-buyable-at-qty<=0 (data oddity): ${maskedButHasDenyBuyable}`);
console.log(`\nNEGATIVE CONTROL: real-stock bulk ${pct(realStock)}% -> CONTINUE-masked-only is ${maskedOnly>0?'a distinct quantifiable population':'(none)'}.`);
console.log(`\nCONTINUE-MASKED-ONLY by vendor (top 20):`); Object.entries(maskedByVendor).sort((a,b)=>b[1]-a[1]).slice(0,20).forEach(([v,n])=>console.log(`  ${v}: ${n}`));
console.log(`\nCONTINUE-MASKED-ONLY examples (summ = policy/qty per variant):`); maskedEx.forEach(x=>console.log(`  "${x.t}" [${x.vendor}] vars=${x.vars} allContinue=${x.allCont} summ=${x.summ}`));
fs.writeFileSync('/tmp/continue-masked-segmentation.json',JSON.stringify({ts:new Date().toISOString(),scanned,complete,realStock,realStock2026,realStockOther,maskedOnly,noVar,maskedButHasDenyBuyable,maskedByVendor,maskedEx},null,2));
console.log(`\nwrote /tmp/continue-masked-segmentation.json`);