// 원래 phase의 상태·사용량 prefix에 고정한 순수 전이 재구성이다. 전체 복구 인증/승인은 별도다. // 설명 문서: docs/specs/batch-loop-observations.md v0.5.0; 메모리 출력 읽기 대사: docs/specs/batch-memory-continue.md v1.0.0 const{hash,same,fail}=require('./batch-observation-proof-common'),policy=require('./loop-controller-policy'); function evaluation(state,goals,reader,required,db){ if(!reader.peek()){if(required)fail('MISSING_DECISION_CLOCK');return null;} // 이전 기록의 시각을 현재 시각으로 보충하지 않는다. const{event,parent,config}=state,s=event.controller,{entries,requests}=require('./harness-evaluation-usage-proof').evaluationUsage(state,db); if(entries.some(e=>!['cli_reported','provider_reported','operator_reported','not_submitted'].includes(e.costSource)||!Number.isFinite(e.costUsd)||e.costUsd<0||e.totalTokens!==null&&e.totalTokens!==undefined&&(!Number.isSafeInteger(e.totalTokens)||e.totalTokens<0))||Object.values(requests).some(q=>!q||!['settled','not_submitted'].includes(q.state)||q.costKnown!==true))fail('UNKNOWN_EVALUATION_USAGE'); const cost=entries.reduce((n,e)=>n+e.costUsd,0),tokens=entries.reduce((n,e)=>n+(e.totalTokens||0),0); if(!Number.isFinite(cost)||!Number.isSafeInteger(tokens)||Math.abs(s.cumulativeCost-event.ledgerRun.total_cost)>1e-9||s.cumulativeTokens!==event.ledgerRun.total_tokens_used||costevent.entryKeys.includes(key)&&parent.checkpoint.entries[key].iteration===event.iteration); const input={iteration:event.iteration,iterationInfo:{cost:cost-event.ledgerRun.total_cost,tokens:tokens-event.ledgerRun.total_tokens_used,success},mockFallbacks:0}; const start=reader.begin('controller:evaluation_clock',input),end=reader.end(start),now=end.data; if(end.kind!=='returned'||!Number.isSafeInteger(now)||nowDate.parse(end.recordedAt))fail('DECISION_CLOCK'); const boundary=Object.values(parent.checkpoint.loopEvents).find(e=>e.iteration===event.iteration&&e.phase==='evaluated'); const controller={...structuredClone(s),currentIteration:s.currentIteration+1,cumulativeCost:s.cumulativeCost+input.iterationInfo.cost,cumulativeTokens:s.cumulativeTokens+input.iterationInfo.tokens,consecutiveFailures:success?0:s.consecutiveFailures+1,metricHistory:goals.metricHistory,iterationResults:[...s.iterationResults,{iteration:event.iteration,goalMet:goals.goalMet}].slice(-500)}; controller.hasWarnedBudget=s.hasWarnedBudget||controller.cumulativeCost>=config.token_budget*.8; const stopReason=goals.goalMet?null:policy.breakReason(controller,config,now),reason=goals.goalMet?'goal_achieved':stopReason?'circuit_broken':'continue'; const result={shouldTerminate:reason!=='continue',reason,...(stopReason?{breakReason:stopReason}:{}),iteration:event.iteration,goalMet:goals.goalMet}; if(result.shouldTerminate)result.receiptSummary=policy.receipt({runId:parent.row.run_id,config,controller,...result,now,mockFallbacks:0}); if(boundary&&(!same(boundary.controller,controller)||!same(boundary.result,result)||!same(boundary.history,event.history)||!same(boundary.meta,event.meta)))fail('EVALUATED_CONTROLLER_MISMATCH'); return{version:1,sourceEventHash:event.eventHash,observationsHash:state.observations.hash,decisionTime:now,controller,result,history:event.history,meta:event.meta,usageHash:hash({entries,requests})}; } module.exports={evaluation};