// 설명 문서: /docs/specs/bada-bong-agent-sources.md (v0.3.0) // 배치 원본 소유자/원장을 변경하지 않고 검증된 현재 회차를 별도 읽기 뷰로 투영한다. const {createHash}=require('node:crypto'); const owners=new WeakMap(); const noop=()=>false,empty=Object.freeze({cycle:noop,observe:noop,activity:()=>null,assigned:noop,finish:noop}); function create(owner,task,steps,options={}){ try{return createOwned(owner,task,steps,options);}catch{return empty;} } function createOwned(owner,task,steps,{iteration=0,restored}={}){ if(!owner||![owner.projectId,owner.taskId,owner.runId].every(value=>typeof value==='string'&&value.length>0)||owner.taskId!==task?.id||owner.projectId!==task?.project_id||!Array.isArray(steps))return empty; let view,closed=false,reported=false,current=-1; const rig=require('./bong-rig-execution-capability').current(owner),origin=rig?'rig':'batch'; const labelText=[...(typeof task.name==='string'&&task.name.trim()?task.name:'Batch').replace(/[\x00-\x1f\x7f]/g,' ').trim()].slice(0,75).join(''); const definitions=steps.map(step=>({id:step.id,name:step.name,type:step.type})),nodes=new Map(); const safe=action=>{try{return action();}catch{if(!reported){reported=true;try{require('../utils/logger').makeLogger('batch-telemetry').warn('BATCH_TELEMETRY_UNAVAILABLE');}catch{/* 관측 실패는 실제 실행을 변경하지 않는다. */}}return false;}}; function plan(next,saved){ nodes.clear();const checkpoint=saved?.checkpoint; const trusted=checkpoint?.runId===owner.runId&&checkpoint?.taskId===owner.taskId&&checkpoint?.projectId===owner.projectId&&saved.iteration===next; const continued=new Set(trusted?require('./batch-step-output').continuedKeys(checkpoint):[]); return definitions.map((step,index)=>{ const id='iteration:'+next+':step:'+createHash('sha256').update(String(step.id)).digest('hex');let status='pending'; if(trusted&&indexentry.stepId===step.id&&entry.stepIndex===index&&entry.iteration===next); const entry=entries.sort((a,b)=>b.attempt-a.attempt)[0]; if(entry){if(continued.has(entry.key))status='failed';else if(entry.status==='completed'||require('../db/batch-recovery-record').resolved(checkpoint,entry.key))status='completed';else status='unknown';} } nodes.set(step.id,{...step,id});return{id,name:step.name||step.id,status}; }); } const api={ cycle(next,saved){return safe(()=>{if(closed||next===current||!Number.isSafeInteger(next)||next<0)return false;current=next;const list=plan(next,saved);if(view)view.setPlan(list);else view=require('./agent-telemetry-execution').createExecution({origin,label:(rig?'Rig · ':'배치 · ')+labelText,labelCode:rig?'run.rig':'run.batch',labelText,projectId:owner.projectId,taskId:rig?rig.qitemId:owner.taskId,runId:owner.runId,nodes:list});return true;});}, observe(event,stepId){return safe(()=>!closed&&view&&nodes.has(stepId)?view.observe(event,nodes.get(stepId).id):false);}, activity(profile){return safe(()=>{const context=require('../workflow/node-context').current(owner),step=nodes.get(context?.nodeId);if(closed||!view?.scope||!step||!['pty_agent','agent_call'].includes(step.type))return null;return view.activity(step.id,profile);})||null;}, assigned(profile){return safe(()=>{const context=require('../workflow/node-context').current(owner),step=nodes.get(context?.nodeId);return!closed&&view?.scope&&step&&['pty_agent','agent_call'].includes(step.type)?view.assigned(step.id,profile):false;});}, finish(event){return safe(()=>{if(closed)return false;closed=true;owners.delete(owner);return view?.finish(event==='unknown'?'unknown':owner.aborted?'cancelled':event);});} }; owners.set(owner,api);api.cycle(iteration,restored);return api; } function activity(owner,profile){return owners.get(owner)?.activity(profile)||null;} function assigned(owner,profile){return owners.get(owner)?.assigned(profile)||false;} module.exports={create,activity,assigned};