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

Function claimTaskWithBusyCheck

src/utils/tasks.ts:619–693  ·  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

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

Callers 1

claimTaskFunction · 0.85

Calls 8

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

Tested by

no test coverage detected