Download src/services/batch-loop-ghost.js from VanyaJ/bada-terminal: direct link, hf CLI and curl.
- Browser
- Download file 4.63 kB
-
https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/services/batch-loop-ghost.js
- Command line
-
hf download hf://VanyaJ/bada-terminal/src/services/batch-loop-ghost.js
-
curl -L -o batch-loop-ghost.js https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/services/batch-loop-ghost.js
4.63 kB
| // 시작 phase 없는 다음 회차 하나를 실제 완료 prefix·요청/관측/비용과 대사한다. 원본은 grant에 남긴다. | |
| // 설명 문서: docs/specs/batch-loop-ghost.md v1.0.0 | |
| const{hash,hex}=require('./bong-browser-resolution'),{same}=require('../db/batch-recovery-record'); | |
| const fail=()=>{throw Object.assign(Error('BATCH_LOOP_CONTINUATION_BLOCKED'),{code:'BATCH_LOOP_CONTINUATION_BLOCKED',retryable:false});}; | |
| const output=v=>typeof v==='string'?JSON.parse(v):v; | |
| function inspect(db,parent,actual,event){ | |
| if(event?.phase!=='committed'||actual.iterations.length!==event.iteration+1)return null; | |
| const c=parent.checkpoint,u=parent.usage,ghost=actual.iterations.at(-1),r=actual.run,end=Date.parse(parent.row.completed_at||Object.values(c.recoveries||{}).at(-1)?.originalRun.completed_at),start=Date.parse(ghost.started_at),closed=ghost.completed_at===null?null:Date.parse(ghost.completed_at); | |
| if(event.result.shouldTerminate||!r||!['failed','aborted','running'].includes(r.status)||!['failed','aborted','running'].includes(ghost.status)||ghost.id!=='iter-looprun-'+c.runId+'-'+(event.iteration+1)||ghost.loop_run_id!==event.ledgerRun.run_id||ghost.iteration_number!==event.iteration+1 | |
| ||ghost.cost!==0||ghost.tokens_used!==0||ghost.goal_evaluation_result!==0||ghost.worktree_branch!==null||!Number.isFinite(start)||start<Date.parse(event.recordedAt)||!Number.isFinite(end)||start>end | |
| ||ghost.status==='running'&&(closed!==null||ghost.error_message!==null)||ghost.status!=='running'&&(!Number.isFinite(closed)||closed<start||closed>end)||ghost.error_message!==null&&(typeof ghost.error_message!=='string'||ghost.error_message.length>2000) | |
| ||![{},output(event.ledgerIteration.step_outputs)].some(v=>same(output(ghost.step_outputs),v))||Object.keys(ghost).length!==12 | |
| ||!same(actual.iterations.slice(0,-1),Object.values(c.loopEvents).filter(e=>e.phase==='committed').map(e=>e.ledgerIteration)) | |
| ||parent.entries.some(e=>e.iteration>event.iteration)||Object.values(u.workflowInvocations||{}).some(d=>d.kind!=='batch'||d.iteration>event.iteration) | |
| ||!same(event.usageIds,(u.usage.entries||[]).map(e=>e.id))||!same(event.requestKeys,Object.keys(u.requests||{}))||event.controller.cumulativeCost!==u.usage.costUsd||event.controller.cumulativeTokens!==(u.usage.totalTokens||0))fail(); | |
| const status=r.status==='aborted'?'aborted':'failed',expected={...event.ledgerRun,status,total_iterations:r.total_iterations,goal_met:0,break_reason:status==='aborted'?'CANCELLED':'EXECUTION_FAILED',completed_at:r.completed_at}; | |
| if(![event.iteration,event.iteration+1].includes(r.total_iterations)||![event.ledgerRun,expected].some(v=>same(r,v))||r.completed_at!==null&&(!Number.isFinite(Date.parse(r.completed_at))||Date.parse(r.completed_at)<start||Date.parse(r.completed_at)>end))fail(); | |
| if(db.getLoopMessages(event.ledgerRun.run_id).some(m=>m.iteration_number>event.iteration))fail(); | |
| const descendants=new Set();function visit(usage,depth){if(depth>16)fail();for(const d of Object.values(usage.workflowInvocations||{})){if(descendants.has(d.childRunId)||descendants.size>=128)fail();descendants.add(d.childRunId);const child=db.getRuntimeUsage(d.childRunId);if(!child)fail();visit(child,depth+1);}}visit(u,0); | |
| const browser=require('./bong-browser-lifecycle').inspectRun(c.runId,c.projectId);if(require('./bong-browser-lifecycle').unsafe(browser)||browser.cases.some(v=>!descendants.has(v.binding.runId)))fail(); | |
| require('./batch-loop-outbox').verify(db,c);const proof=require('./batch-loop-prefix-proof').verify(db,parent,{throughCommitted:true}); | |
| const normalized={run:{...event.ledgerRun,status,goal_met:0,break_reason:status==='aborted'?'CANCELLED':'EXECUTION_FAILED',completed_at:r.completed_at||ghost.completed_at||new Date(end).toISOString()},iterations:actual.iterations.slice(0,-1)}; | |
| return{rows:normalized,reconciliation:{version:1,kind:'unstarted_iteration',sourceHash:event.eventHash,prefixHash:proof.prefixHash,observationsHash:proof.observationsHash,ghostId:ghost.id,ghostHash:hash(ghost),normalizedHash:hash(normalized)}}; | |
| } | |
| function validate(v,event){if(!v||Object.keys(v).length!==8||Object.keys(v).some(k=>!['version','kind','sourceHash','prefixHash','observationsHash','ghostId','ghostHash','normalizedHash'].includes(k))||v.version!==1||v.kind!=='unstarted_iteration'||event.phase!=='committed'||event.result.shouldTerminate||v.sourceHash!==event.eventHash||v.ghostId!=='iter-'+event.ledgerRun.run_id+'-'+(event.iteration+1)||!['sourceHash','prefixHash','observationsHash','ghostHash','normalizedHash'].every(k=>hex(v[k])))fail();} | |
| module.exports={inspect,validate,fail}; | |