/** Restore exact SHA-pinned REA skill exports; no original file access/execution. */ import fs from 'node:fs' import path from 'node:path' import crypto from 'node:crypto' const prefix='resources/plugins/workbuddy-builtin/skills/expert-manager/' const target=path.resolve('src/main/capabilities/assets/expert-manager') const pin='035254bc76310ea46317433e4840602094543ef323d2d5c5aff7c131729de918' const inputs=process.argv.slice(2) if(!inputs.length)throw new Error('Select retained REA exports') const manifest={targetVersion:'WorkBuddy 5.8.2',rootSha256:pin,scope:'exact retained expert-manager files; no original initialization or Python execution',files:[]} const bundle={files:{}} for(const input of inputs){ const d=JSON.parse(fs.readFileSync(input,'utf8')) if(d.normalized_result.root_sha256!==pin)throw new Error('Root mismatch') for(const f of d.normalized_result.files){ if(!f.path.startsWith(prefix))continue const relative=f.path.slice(prefix.length) if(relative.split('/').some(p=>!p||p==='.'||p==='..'))throw new Error('Noncanonical member') const sha=crypto.createHash('sha256').update(f.text).digest('hex') if(sha!==f.sha256||Buffer.byteLength(f.text)!==f.size_bytes)throw new Error('REA bytes mismatch') const destination=path.join(target,...relative.split('/')) fs.mkdirSync(path.dirname(destination),{recursive:true}) fs.writeFileSync(destination,f.text) if(crypto.createHash('sha256').update(fs.readFileSync(destination)).digest('hex')!==sha)throw new Error('Restored SHA mismatch') manifest.files.push({file:relative,originalMember:f.path,sha256:sha,bytes:f.size_bytes,evidenceId:d.evidence_id,retainedExport:input}) bundle.files[relative]=f.text } } fs.writeFileSync(path.join(target,'rea-source-manifest.json'),JSON.stringify(manifest,null,2)+'\n') fs.writeFileSync(path.join(path.dirname(target),'expert-manager-source.json'),JSON.stringify(bundle,null,2)+'\n') console.log(JSON.stringify({target,files:manifest.files.length,bytes:manifest.files.reduce((n,f)=>n+f.bytes,0)}))