MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / claimTaskWithBusyCheck

Function claimTaskWithBusyCheck

source code/utils/tasks.ts:620–694  ·  view source on GitHub ↗

* Claims a task with an atomic check for agent busy status. * Uses a task-list-level lock to ensure the busy check and claim are atomic.

(
  taskListId: string,
  taskId: string,
  claimantAgentId: string,
)

Source from the content-addressed store, hash-verified

618 * Uses a task-list-level lock to ensure the busy check and claim are atomic.
619 */
620async function claimTaskWithBusyCheck(
621 taskListId: string,
622 taskId: string,
623 claimantAgentId: string,
624): Promise<ClaimTaskResult> {
625 const lockPath = await ensureTaskListLockFile(taskListId)
626
627 let release: (() => Promise<void>) | undefined
628 try {
629 // Acquire exclusive lock on the task list
630 release = await lockfile.lock(lockPath, LOCK_OPTIONS)
631
632 // Read all tasks to check agent status and task state atomically
633 const allTasks = await listTasks(taskListId)
634
635 // Find the task we want to claim
636 const task = allTasks.find(t => t.id === taskId)
637 if (!task) {
638 return { success: false, reason: 'task_not_found' }
639 }
640
641 // Check if already claimed by another agent
642 if (task.owner && task.owner !== claimantAgentId) {
643 return { success: false, reason: 'already_claimed', task }
644 }
645
646 // Check if already resolved
647 if (task.status === 'completed') {
648 return { success: false, reason: 'already_resolved', task }
649 }
650
651 // Check for unresolved blockers (open or in_progress tasks block)
652 const unresolvedTaskIds = new Set(
653 allTasks.filter(t => t.status !== 'completed').map(t => t.id),
654 )
655 const blockedByTasks = task.blockedBy.filter(id =>
656 unresolvedTaskIds.has(id),
657 )
658 if (blockedByTasks.length > 0) {
659 return { success: false, reason: 'blocked', task, blockedByTasks }
660 }
661
662 // Check if agent is busy with other unresolved tasks
663 const agentOpenTasks = allTasks.filter(
664 t =>
665 t.status !== 'completed' &&
666 t.owner === claimantAgentId &&
667 t.id !== taskId,
668 )
669 if (agentOpenTasks.length > 0) {
670 return {
671 success: false,
672 reason: 'agent_busy',
673 task,
674 busyWithTasks: agentOpenTasks.map(t => t.id),
675 }
676 }
677

Callers 1

claimTaskFunction · 0.85

Calls 8

ensureTaskListLockFileFunction · 0.85
listTasksFunction · 0.85
updateTaskFunction · 0.85
logForDebuggingFunction · 0.85
errorMessageFunction · 0.70
logErrorFunction · 0.70
releaseFunction · 0.50
hasMethod · 0.45

Tested by

no test coverage detected