← System index

prepare-regeneration.mjs

Read saved feedback and checked source hashes into a revision brief.

Download original source

Factory source f102c8669271902fe60d3189b0a54a566f64b07c · SHA-256 2256de201237295c479224d2c9ed6768550cc1ff646e31a6fde2a17547280322

// Operator tool: prepare one source-bound revision. It never invokes a provider or changes art/ratings.
import fs from 'node:fs';
import path from 'node:path';
import {createHash} from 'node:crypto';
import {fileURLToPath} from 'node:url';
const sha=b=>createHash('sha256').update(b).digest('hex');
export function prepareRegeneration({manifest,root,ledger,asset,change='',assetPathMap=process.env.CREATIVE_ASSET_PATH_MAP}) {
  const desk=path.dirname(path.resolve(manifest));root=path.resolve(root);
  const m=JSON.parse(fs.readFileSync(manifest,'utf8'));
  const pair=m.pairs.find(p=>p.id===asset);if(!pair) throw Error('Unknown asset: '+asset);
  const receipt=JSON.parse(fs.readFileSync(path.join(desk,'comparison-receipt.json'),'utf8'));
  const current=receipt.pairs.find(p=>p.id===asset);if(!current) throw Error('Missing current receipt');
  if(receipt.brand!==m.brand) throw Error('Manifest/receipt brand mismatch');
  const frames=JSON.parse(fs.readFileSync(path.join(desk,'frames.json'),'utf8'));
  const frame=frames.frames.find(f=>f.id===current.ratingId);
  if(!frame||frame.client!==m.brand) throw Error('Missing current brand/version frame');
  const recipe=pair.recipe || {};
  const locations=assetPathMap?JSON.parse(fs.readFileSync(assetPathMap,'utf8')):null;
  if(locations&&(locations.schema!=='creative-asset-path-map/v1'||!locations.assets)) throw Error('Unsupported creative asset path map');
  const checked=(item,base,strict=true)=>{
    const provenancePath=item.path || item.file;
    const mapped=locations?.assets[item.sha256];
    if(mapped&&!path.isAbsolute(mapped)) throw Error('Mapped asset path must be absolute');
    const absolute=mapped || path.resolve(base,provenancePath);
    const bytes=fs.readFileSync(absolute),hash=sha(bytes);
    if(mapped&&hash!==item.sha256) throw Error('Mapped asset hash mismatch: '+absolute);
    if(strict&&item.sha256&&hash!==item.sha256) throw Error('Source drift: '+absolute);
    return {...item,path:absolute,provenancePath,currentSha256:hash,changed:!!item.sha256&&hash!==item.sha256};
  };
  // The original input order matters. Only fallback replacement slots use the displayed references.
  const sources=recipe.sources?.length?recipe.sources.map(s=>checked(s,root)):
    [pair.reference,...(pair.additionalReferences || [])].map(s=>checked(s,desk));
  const output=current.output?checked(current.output,desk):null;
  const master=recipe.master?checked(recipe.master,root):null;
  const brandInputs=(recipe.brandInputs || []).map(s=>checked(s,root,false));
  if(recipe.promptSha256&&sha(recipe.prompt || '')!==recipe.promptSha256) throw Error('Prompt hash mismatch');
  let feedback=null;
  for(const line of fs.readFileSync(ledger,'utf8').split(/\r?\n/).filter(Boolean)) {
    const row=JSON.parse(line);if(row.id===current.ratingId) feedback=row;
  }
  return {schema:'reference-output-regeneration-brief/v1',brand:m.brand,assetId:asset,
    fromRatingId:current.ratingId,fromVersionHash:frame.versionHash,
    originalRecipe:recipe,originalPrompt:recipe.prompt || null,reference:pair.reference,
    sources,currentOutput:output,previousMaster:master,brandInputs,exactCopy:pair.copy || '',
    feedback,requestedChange:change,
    steps:['Read the exact-version feedback and requested change. Preserve everything else in the source composition and client facts.',
      'Inspect the actual reference files, prior master and current brand inputs. Changed brand inputs require deliberate rebinding in the new recipe.',
      'Use image generation/editing with these real inputs in their recorded order. Record the actual final prompt and provider; a seeded run may still produce different pixels.',
      'Save a NEW versioned master in this machine’s offloaded storage; retain the old master and recipe. Inspect source parity, product identity, wording and room fidelity.',
      'Archive current web/PDF exports before replacement; export only this asset, update its prompt/recipe, then rebuild the shared comparison.',
      'Confirm the new rating ID differs and the prior feedback remains earlier-version feedback. A page-only rebuild must preserve all current IDs.']};
}
if(process.argv[1]&&path.resolve(process.argv[1])===fileURLToPath(import.meta.url)) {
  const args={};for(let i=2;i<process.argv.length;i+=2) args[process.argv[i].replace(/^--/,'')]=process.argv[i+1];
  if(args['asset-path-map']) args.assetPathMap=args['asset-path-map'];
  for(const key of ['manifest','root','ledger','asset','out']) if(!args[key]) throw Error('--'+key+' required');
  const brief=prepareRegeneration(args);fs.mkdirSync(path.dirname(path.resolve(args.out)),{recursive:true});
  fs.writeFileSync(args.out,JSON.stringify(brief,null,2)+'\n');console.log('Prepared '+brief.assetId+' from '+brief.fromRatingId+' → '+args.out);
}