MCPcopy Create free account
hub / github.com/Noumena-Network/code / waitForNextPromptOrShutdown

Function waitForNextPromptOrShutdown

src/utils/swarm/inProcessRunner.ts:690–869  ·  view source on GitHub ↗

* Waits for new prompts or shutdown request. * Polls the teammate's mailbox every 500ms, checking for: * - Shutdown request from leader (returned to caller for model decision) * - New messages/prompts from leader * - Abort signal * * This keeps the teammate alive in 'idle' state instead of ter

(
  identity: TeammateIdentity,
  abortController: AbortController,
  taskId: string,
  getAppState: () => AppState,
  setAppState: SetAppStateFn,
  taskListId: string,
)

Source from the content-addressed store, hash-verified

688 * Does NOT auto-approve shutdown - the model should make that decision.
689 */
690async function waitForNextPromptOrShutdown(
691 identity: TeammateIdentity,
692 abortController: AbortController,
693 taskId: string,
694 getAppState: () => AppState,
695 setAppState: SetAppStateFn,
696 taskListId: string,
697): Promise<WaitResult> {
698 const POLL_INTERVAL_MS = 500
699
700 logForDebugging(
701 `[inProcessRunner] ${identity.agentName} starting poll loop (abort=${abortController.signal.aborted})`,
702 )
703
704 let pollCount = 0
705 while (!abortController.signal.aborted) {
706 // Check for in-memory pending messages on every iteration (from transcript viewing)
707 const appState = getAppState()
708 const task = appState.tasks[taskId]
709 if (
710 task &&
711 task.type === 'in_process_teammate' &&
712 task.pendingUserMessages.length > 0
713 ) {
714 const message = task.pendingUserMessages[0]! // Safe: checked length > 0
715 // Pop the message from the queue
716 setAppState(prev => {
717 const prevTask = prev.tasks[taskId]
718 if (!prevTask || prevTask.type !== 'in_process_teammate') {
719 return prev
720 }
721 return {
722 ...prev,
723 tasks: {
724 ...prev.tasks,
725 [taskId]: {
726 ...prevTask,
727 pendingUserMessages: prevTask.pendingUserMessages.slice(1),
728 },
729 },
730 }
731 })
732 logForDebugging(
733 `[inProcessRunner] ${identity.agentName} found pending user message (poll #${pollCount})`,
734 )
735 return {
736 type: 'new_message',
737 message,
738 from: 'user',
739 }
740 }
741
742 // Wait before next poll (skip on first iteration to check immediately)
743 if (pollCount > 0) {
744 await sleep(POLL_INTERVAL_MS)
745 }
746 pollCount++
747

Callers 1

runInProcessTeammateFunction · 0.85

Calls 9

readMailboxFunction · 0.85
isShutdownRequestFunction · 0.85
countFunction · 0.85
markMessageAsReadByIndexFunction · 0.85
tryClaimNextTaskFunction · 0.85
logForDebuggingFunction · 0.50
getAppStateFunction · 0.50
setAppStateFunction · 0.50
sleepFunction · 0.50

Tested by

no test coverage detected