Download src/services/batch-loop-first.js from VanyaJ/bada-terminal: direct link, hf CLI and curl.
- Browser
- Download file 4.92 kB
-
https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/services/batch-loop-first.js
- Command line
-
hf download hf://VanyaJ/bada-terminal/src/services/batch-loop-first.js
-
curl -L -o batch-loop-first.js https://huggingface.co/VanyaJ/bada-terminal/resolve/main/src/services/batch-loop-first.js
4.92 kB
| // ์ ์ฅ๋ ์ต์ด ๊ธฐ์ค๊ณผ ๋ชจ๋ ์์ ์์ฅ์ ๋์ฌํ๋ค. ๋น์ฉ0๋ง์ผ๋ก ์์ํ์ง ์์๋ค๊ณ ํ์ ํ์ง ์๋๋ค. | |
| // ์ค๋ช ๋ฌธ์: docs/specs/batch-loop-first.md v1.0.0, docs/specs/batch-worktree-continuation.md v1.0.0 | |
| const{hash,hex}=require('./bong-browser-resolution'),{same}=require('../db/batch-recovery-record'),fail=require('./batch-loop-ghost').fail; | |
| const isFirst=d=>d?.reconciliation?.kind==='unstarted_first_iteration'; | |
| const source=(c,d)=>isFirst(d)?c.loopOrigin:c.loopEvents?.[d?.eventKey]; | |
| const emptyUsage=u=>({entries:u.usage.entries||[],requests:u.requests||{},invocations:u.workflowInvocations||{},cost:u.usage.costUsd,tokens:u.usage.totalTokens||0,calls:u.usage.llmCalls||0,cumulativeCost:u.cumulativeCost,tokensUsed:u.tokensUsed||0}); | |
| function inspect(db,parent,actual){ | |
| const c=parent.checkpoint,o=c.loopOrigin,r=actual.run,u=emptyUsage(parent.usage),end=Date.parse(parent.row.completed_at||Object.values(c.recoveries||{}).at(-1)?.originalRun.completed_at),s=o?.controller,config=c.definition.loop; | |
| if(!o||Object.keys(c.loopEvents||{}).length)fail();require('../db/batch-loop-origin-record').validate(c); | |
| const browser=require('./bong-browser-lifecycle').inspectRun(c.runId,c.projectId); | |
| if(config.worktree_isolation&&!c.workspaceOrigin||parent.entries.length||c.currentKey!==null||c.continuations||Object.keys(u.entries).length||Object.keys(u.requests).length||Object.keys(u.invocations).length||u.cost!==0||u.tokens!==0||u.calls!==0||u.cumulativeCost!==0||u.tokensUsed!==0||hash(u)!==o.usageHash | |
| ||!Number.isFinite(end)||s.startTime<Date.parse(parent.row.started_at)||Date.parse(o.recordedAt)>end||Date.now()<s.startTime||Date.now()-s.startTime>=(config.max_wall_time??3600)*1000 | |
| ||actual.iterations.length>1||!r||db.getLoopMessages(r.run_id).length||db.hasBatchLoopObservations(c.runId)||require('./bong-browser-lifecycle').unsafe(browser)||browser.cases.length)fail(); | |
| const g=actual.iterations[0],start=Date.parse(g?.started_at),closed=g?.completed_at===null?null:Date.parse(g?.completed_at),body=v=>typeof v==='string'?JSON.parse(v):v; | |
| if(g&&(g.id!=='iter-looprun-'+c.runId+'-1'||g.loop_run_id!==r.run_id||g.iteration_number!==1||Object.keys(g).length!==12||!['running','failed','aborted'].includes(g.status)||!same(body(g.step_outputs),{})||g.cost!==0||g.tokens_used!==0||g.goal_evaluation_result!==0||g.worktree_branch!==null||!Number.isFinite(start)||start<Date.parse(o.recordedAt)||start>end | |
| ||g.status==='running'&&(closed!==null||g.error_message!==null)||g.status!=='running'&&(!Number.isFinite(closed)||closed<start||closed>end)||g.error_message!==null&&(typeof g.error_message!=='string'||g.error_message.length>2000)))fail(); | |
| const stopped={...o.ledgerRun,status:r.status,total_iterations:r.total_iterations,break_reason:r.status==='aborted'?'CANCELLED':'EXECUTION_FAILED',completed_at:r.completed_at}; | |
| if(!same(r,o.ledgerRun)&&(!['failed','aborted'].includes(r.status)||![0,...(g?[1]:[])].includes(r.total_iterations)||!Number.isFinite(Date.parse(r.completed_at))||Date.parse(r.completed_at)<(g?start:Date.parse(o.recordedAt))||Date.parse(r.completed_at)>end||!same(r,stopped)))fail(); | |
| require('./batch-loop-outbox').verify(db,c);const normalized={run:o.ledgerRun,iterations:[]};return{eventKey:o.key,eventHash:o.eventHash,rowsHash:hash(actual),originalRun:r,originalIterations:actual.iterations,reconciliation:{version:1,kind:'unstarted_first_iteration',sourceHash:o.eventHash,prefixHash:hash(o),observationsHash:hash({usage:u,browser,messages:[],observations:null}),ghostId:g?.id||null,ghostHash:hash(g||null),normalizedHash:hash(normalized)}}; | |
| } | |
| function validate(c,g){ | |
| const d=g.loopState,o=c.loopOrigin,v=d?.reconciliation;if(!o||!isFirst(d)||Object.keys(d).length!==6||Object.keys(d).some(k=>!['eventKey','eventHash','rowsHash','originalRun','originalIterations','reconciliation'].includes(k))||d.eventKey!==o.key||d.eventHash!==o.eventHash||d.originalRun?.run_id!==o.ledgerRun.run_id||!Array.isArray(d.originalIterations)||d.originalIterations.length>1||hash({run:d.originalRun,iterations:d.originalIterations})!==d.rowsHash||g.position.iteration!==1||g.position.stepIndex!==0||g.resolved||g.entryKeys.length||g.usageIds.length||g.requestKeys.length||g.invocationKeys.length||Object.values(c.loopEvents||{}).some(e=>e.revision<=g.checkpointRevision))fail(); | |
| 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.sourceHash!==o.eventHash||v.prefixHash!==hash(o)||v.normalizedHash!==hash({run:o.ledgerRun,iterations:[]})||v.ghostId!==(d.originalIterations[0]?.id||null)||v.ghostHash!==hash(d.originalIterations[0]||null)||!['sourceHash','prefixHash','observationsHash','ghostHash','normalizedHash'].every(k=>hex(v[k])))fail(); | |
| } | |
| module.exports={inspect,validate,isFirst,source,emptyUsage}; | |