← back to Mfr Review Viewer Corruption

scripts/diagnostics/probe-delete-context-matrix.mjs

43 lines

// STEP 2: ZERO-EFFECT delete probe. Deletes a recordId PROVEN not to exist, so the
// call can destroy nothing; we only read back FileMaker's error code.
//   110 = "Related tables are missing"  -> layout's TO context is broken for DELETE
//   101 = "Record is missing"           -> DELETE context is CLEAN (it got far enough
//                                           to look for the record and not find it)
import { readFileSync, existsSync, writeFileSync } from 'node:fs';
import path from 'node:path'; import os from 'node:os';
const SP='/private/tmp/claude-501/-Users-macstudio3-Projects-mfr-review-viewer-corruption/a26937d9-cc11-4299-9dfb-d661aa523a33/scratchpad';
const FM_PROJECT = path.join(os.homedir(),'Projects','filemaker-mcp');
for (const line of readFileSync(path.join(FM_PROJECT,'.env'),'utf8').split('\n')) { const m=line.match(/^([A-Z_]+)=(.*)$/); if(m&&!process.env[m[1]]) process.env[m[1]]=m[2]; }
const fm = await import('file://'+path.join(FM_PROJECT,'src','fm-client.js'));

const PROBE_RID = '99999999';   // must NOT exist
// --- HARD PRECONDITION: prove the probe id does not exist before any DELETE ---
let exists = null;
try { exists = await fm.getRecord('WALLPAPER','Basic List of Fields',PROBE_RID); }
catch(e){ exists = { _err: String(e.fmCode||'')+' '+e.message }; }
if (!exists || !exists._err) { console.error('ABORT: probe id may exist ->', JSON.stringify(exists).slice(0,300)); process.exit(2); }
console.log('precondition OK: probe id', PROBE_RID, 'does not exist ->', exists._err.slice(0,80));

const p1 = JSON.parse(readFileSync(SP+'/probe1.json','utf8'));
// same-base-table + zero related fields/portals
const cands = p1.filter(o=>o.ok && o.relatedFields.length===0 && o.portals.length===0 &&
                          (o.combo==='HSW51502' || o.mfr==='51502'));
console.log('delete-probe candidates:', cands.length);

const res=[]; let i=0;
async function worker(){
  while(true){ const k=i++; if(k>=cands.length) return; const lay=cands[k].layout;
    let code='?', msg='';
    try { await fm.deleteRecord('WALLPAPER', lay, PROBE_RID); code='DELETED?!'; }
    catch(e){ code=String(e.fmCode||''); msg=String(e.message).slice(0,120); }
    res.push({layout:lay, code, msg, nFields:cands[k].nFields});
  }
}
await Promise.all(Array.from({length:8},worker));
writeFileSync(SP+'/probe2.json', JSON.stringify(res,null,1));
const tally={}; for(const r of res) tally[r.code]=(tally[r.code]||0)+1;
console.log('ERROR-CODE TALLY:', JSON.stringify(tally));
const clean = res.filter(r=>r.code!=='110');
console.log('NON-110 layouts:', clean.length);
console.log(clean.map(c=>`  ${c.code} | ${c.layout} | f=${c.nFields} | ${c.msg}`).join('\n'));