openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 3.94 kB
JavaScript
import{n as SUBAGENT_LANE_PREFIX,r as SWARM_LANE_PREFIX,t as STATIC_COMMAND_LANES}from"./lanes-EIjjRG8i.mjs";import{c as publishLaneConfiguration,d as getLaneGroup,f as getQueueState,r as getCommandLaneSnapshot,s as listCommandLaneTotals}from"./command-queue-fBrp5ERY.mjs";
//!src/process/background-work.ts
const BACKGROUND_WORK_GROUP=`background-work`;const BACKGROUND_WORK_MAX_CONCURRENT=3;function getBackgroundOwnerLane(e){const t=`${`background`}:${e}`;if(getLaneGroup(t)){if((getQueueState().lanes.get(t)?.maxConcurrent??1)!==1)throw new Error(`Background owner ${e} is already registered with different concurrency`)}else{const e=getQueueState().laneGroups.get(BACKGROUND_WORK_GROUP);publishLaneConfiguration({lanes:{[t]:1},groups:{[BACKGROUND_WORK_GROUP]:{budget:BACKGROUND_WORK_MAX_CONCURRENT,members:[...e?.members??[],t]}}})}return t}function isBackgroundWorkLane(e){return getLaneGroup(e)?.group===BACKGROUND_WORK_GROUP}function getBackgroundWorkSnapshot(){const{lanes:e,laneGroups:t}=getQueueState();const n=t.get(BACKGROUND_WORK_GROUP);const r={lane:`background`,activeCount:0,queuedCount:0,maxConcurrent:BACKGROUND_WORK_MAX_CONCURRENT,draining:false,generation:0,blockedBy:null};for(const t of n?.members??[]){const n=e.get(t);if(!n)continue;const i=n.activeTasks.size;r.activeCount+=i;r.queuedCount+=n.queue.length;r.draining||=n.draining;r.generation=Math.max(r.generation,n.generation);if(n.queue.length>0&&i>=n.maxConcurrent)r.blockedBy=`lane`}if(r.activeCount>=BACKGROUND_WORK_MAX_CONCURRENT)r.blockedBy=`group-budget`;return r}
//!src/process/command-lane-diagnostics.ts
const STATIC_COMMAND_LANE_SET=new Set(STATIC_COMMAND_LANES);const MAX_SWARM_LANE_SNAPSHOTS=32;function getSubagentLaneSnapshot(){const e={...getCommandLaneSnapshot(`subagent`),concurrencyScope:`session`,saturatedLaneCount:0};for(const t of getQueueState().lanes.values()){if(!t.lane.startsWith(`subagent:`)||t.lane.startsWith(`subagent:swarm:`))continue;const n=t.activeTasks.size;e.activeCount+=n;e.queuedCount+=t.queue.length;e.draining||=t.draining;e.generation=Math.max(e.generation,t.generation);if(n>=t.maxConcurrent){e.saturatedLaneCount=(e.saturatedLaneCount??0)+1;e.blockedBy=`lane`}}return e}function getCommandLaneDiagnostics(){const e=STATIC_COMMAND_LANES.toSorted().map(e=>{if(e===`background`)return getBackgroundWorkSnapshot();if(e===`subagent`)return getSubagentLaneSnapshot();return getCommandLaneSnapshot(e)}).filter(e=>e.maxConcurrent>0||e.activeCount>0||e.queuedCount>0);const t={laneCount:0,activeCount:0,queuedCount:0,queuedLaneCount:0};let n=0;for(const r of listCommandLaneTotals()){if(r.lane.startsWith(`subagent:swarm:`)){if(n<MAX_SWARM_LANE_SNAPSHOTS){e.push(getCommandLaneSnapshot(r.lane));n+=1;continue}}else if(STATIC_COMMAND_LANE_SET.has(r.lane)||isBackgroundWorkLane(r.lane)||r.lane.startsWith(`subagent:`))continue;t.laneCount+=1;t.activeCount+=r.activeCount;t.queuedCount+=r.queuedCount;if(r.queuedCount>0)t.queuedLaneCount+=1}return{lanes:e,dynamic:t.laneCount>0?t:null}}function formatAdmissionDiagnostics(e){const{phase:t,phaseStartedAt:n,laneMs:r,writerMs:i,placementMs:a,queueWait:o}=e;const s=o?.queued?getCommandLaneSnapshot(o.lane):void 0;const c=[];let l=0;if(s){const e=s.blockedBy===`lane`?[s.lane]:getLaneGroup(s.lane)?.members??[s.lane];for(const t of e){const e=getQueueState().lanes.get(t)?.activeTasks;l+=e?.size??0;for(const[n,r]of e??[]){if(c.length===64)break;c.push({lane:t,taskId:n,taskIdentity:r.taskIdentity,sessionTarget:r.sessionTarget,startedAt:r.startedAt})}}}return`admission=${JSON.stringify({phase:t,phaseElapsedMs:t?Math.round(performance.now()-n):void 0,laneMs:r,writerMs:i,placementMs:a,queue:o&&{lane:o.lane,queueAheadAtEnqueue:o.queuedAheadAtEnqueue,activeAheadAtEnqueue:o.activeAheadAtEnqueue,...s,...s?{holdersNow:c,holdersOmitted:l-c.length}:{}}})}`}export{isBackgroundWorkLane as i,getCommandLaneDiagnostics as n,getBackgroundOwnerLane as r,formatAdmissionDiagnostics as t};