MCPcopy Index your code
hub / github.com/claude-code-best/claude-code / tryClaimNextTask

Function tryClaimNextTask

src/utils/swarm/inProcessRunner.ts:633–666  ·  view source on GitHub ↗

* Try to claim an available task from the team's task list. * Returns the formatted prompt if a task was claimed, or undefined if none available.

(
  taskListId: string,
  agentName: string,
)

Source from the content-addressed store, hash-verified

631 * Returns the formatted prompt if a task was claimed, or undefined if none available.
632 */
633async function tryClaimNextTask(
634 taskListId: string,
635 agentName: string,
636): Promise<string | undefined> {
637 try {
638 const tasks = await listTasks(taskListId)
639 const availableTask = findAvailableTask(tasks)
640
641 if (!availableTask) {
642 return undefined
643 }
644
645 const result = await claimTask(taskListId, availableTask.id, agentName)
646
647 if (!result.success) {
648 logForDebugging(
649 `[inProcessRunner] Failed to claim task #${availableTask.id}: ${result.reason}`,
650 )
651 return undefined
652 }
653
654 // Also set status to in_progress so the UI reflects it immediately
655 await updateTask(taskListId, availableTask.id, { status: 'in_progress' })
656
657 logForDebugging(
658 `[inProcessRunner] Claimed task #${availableTask.id}: ${availableTask.subject}`,
659 )
660
661 return formatTaskAsPrompt(availableTask)
662 } catch (err) {
663 logForDebugging(`[inProcessRunner] Error checking task list: ${err}`)
664 return undefined
665 }
666}
667
668/**
669 * Result of waiting for messages.

Callers 2

runInProcessTeammateFunction · 0.85

Calls 6

listTasksFunction · 0.85
claimTaskFunction · 0.85
updateTaskFunction · 0.85
findAvailableTaskFunction · 0.70
formatTaskAsPromptFunction · 0.70
logForDebuggingFunction · 0.50

Tested by

no test coverage detected