// 결과와 함께 대기를 저장하고 제출 의도/반환 결과를 불변 추가한다. 불확실한 제출을 반복하지 않는다. // 설명 문서: docs/specs/batch-step-continuation.md (v0.7.0, R67 진행 중) const {hash,hex}=require('../services/bong-browser-resolution'),{same}=require('./batch-recovery-record'); const fail=()=>require('./batch-checkpoint-record').fail(),channels=['hud','ipc','native','warning']; function states(c){ const packets=new Map();for(const e of Object.values(c.loopPublications||{})){ if(e.kind==='ready')packets.set(e.publicationId,{ready:e,channels:{}}); else{const item=packets.get(e.publicationId);if(!item)fail();const prior=item.channels[e.channel];if(e.kind==='intent'?!!prior:prior?.kind!=='intent')fail();item.channels[e.channel]=e;} }return packets; } function validate(c){ if(c.publicationVersion!==undefined&&c.publicationVersion!==1)fail();if(c.loopPublications===undefined)return; const map=c.loopPublications;if(c.publicationVersion!==1||!c.definition.loop||!map||typeof map!=='object'||Array.isArray(map)||!Object.keys(map).length||Object.keys(map).length>8192)fail(); let prior;const ids=new Set();for(const [key,e]of Object.entries(map)){ const {eventHash,...body}=e||{},keys=['version','key','revision','recordedAt','previousHash','kind','publicationId','channel','data','eventHash']; if(e?.version!==1||key!==e.key||key!==String(e.revision)||!Number.isSafeInteger(e.revision)||e.revision<1||e.revision>c.revision||prior&&e.revision<=prior.revision||!Number.isFinite(Date.parse(e.recordedAt))||e.previousHash!==(prior?.eventHash||null) ||Object.keys(e).length!==keys.length||Object.keys(e).some(k=>!keys.includes(k))||!hex(eventHash)||hash(body)!==eventHash||!hex(e.publicationId)||!['ready','intent','submitted','unsupported','uncertain','superseded'].includes(e.kind))fail(); if(e.kind==='ready'){ const d=e.data;if(e.channel!==null||!d||Object.keys(d).length!==3||!d.source||!d.packet||!Array.isArray(d.channels)||!d.channels.length||new Set(d.channels).size!==d.channels.length||d.channels.some(k=>!channels.includes(k))||hash(d)!==e.publicationId||ids.has(e.publicationId))fail(); const s=d.source,p=d.packet;if(!['committed','stopped','warning'].includes(s.kind)||p.runId!==c.runId||p.loopRunId!=='looprun-'+c.runId||!Number.isSafeInteger(p.iteration)||p.iteration<(s.kind==='stopped'?0:1)||!Number.isFinite(p.cost)||p.cost<0)fail(); if(s.kind==='stopped'){if(Object.keys(s).length!==2||!hex(s.rowsHash)||!['failed','aborted'].includes(p.status)||!same(d.channels,['hud','ipc']))fail();} else{const event=c.loopEvents?.[s.eventKey];if(Object.keys(s).length!==3||!event||event.eventHash!==s.eventHash||event.iteration!==p.iteration||event.revision>=e.revision||event.phase!==(s.kind==='warning'?'evaluated':'committed')||s.kind==='warning'&&(!event.controller.hasWarnedBudget||Object.values(c.loopEvents).some(v=>v.phase==='evaluated'&&v.revisionv.kind==='ready'&&v.publicationId===e.publicationId);if(!ready||ready.revision>=e.revision||!ready.data.channels.includes(e.channel)||e.data!==null||e.kind==='superseded'&&!['hud','ipc'].includes(e.channel))fail(); } prior=e; }states(c); } function transition(next,previous){ const old=previous.loopPublications||{},map=next.loopPublications||{},added=Object.keys(map).filter(k=>!Object.hasOwn(old,k));for(const[k,e]of Object.entries(old))if(!same(e,map[k]))fail(); if(added.length){if(added.length!==1||map[added[0]].revision!==previous.revision+1)fail();return true;}if(!same(old,map))fail();return false; } module.exports={validate,transition,states};