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

Function claimTask

source code/utils/tasks.ts:543–614  ·  view source on GitHub ↗
(
  taskListId: string,
  taskId: string,
  claimantAgentId: string,
  options: ClaimTaskOptions = {},
)

Source from the content-addressed store, hash-verified

541 * if the agent owns any other open tasks before claiming.
542 */
543export async function claimTask(
544 taskListId: string,
545 taskId: string,
546 claimantAgentId: string,
547 options: ClaimTaskOptions = {},
548): Promise<ClaimTaskResult> {
549 const taskPath = getTaskPath(taskListId, taskId)
550
551 // Check existence before locking — proper-lockfile.lock throws if the
552 // target file doesn't exist, and we want a clean task_not_found result.
553 const taskBeforeLock = await getTask(taskListId, taskId)
554 if (!taskBeforeLock) {
555 return { success: false, reason: 'task_not_found' }
556 }
557
558 // If we need to check agent busy status, use task-list-level lock
559 // to prevent TOCTOU race conditions
560 if (options.checkAgentBusy) {
561 return claimTaskWithBusyCheck(taskListId, taskId, claimantAgentId)
562 }
563
564 // Otherwise, use task-level lock (original behavior)
565 let release: (() => Promise<void>) | undefined
566 try {
567 // Acquire exclusive lock on the task file
568 release = await lockfile.lock(taskPath, LOCK_OPTIONS)
569
570 // Read current task state
571 const task = await getTask(taskListId, taskId)
572 if (!task) {
573 return { success: false, reason: 'task_not_found' }
574 }
575
576 // Check if already claimed by another agent
577 if (task.owner && task.owner !== claimantAgentId) {
578 return { success: false, reason: 'already_claimed', task }
579 }
580
581 // Check if already resolved
582 if (task.status === 'completed') {
583 return { success: false, reason: 'already_resolved', task }
584 }
585
586 // Check for unresolved blockers (open or in_progress tasks block)
587 const allTasks = await listTasks(taskListId)
588 const unresolvedTaskIds = new Set(
589 allTasks.filter(t => t.status !== 'completed').map(t => t.id),
590 )
591 const blockedByTasks = task.blockedBy.filter(id =>
592 unresolvedTaskIds.has(id),
593 )
594 if (blockedByTasks.length > 0) {
595 return { success: false, reason: 'blocked', task, blockedByTasks }
596 }
597
598 // Claim the task (already holding taskPath lock — use unsafe variant)
599 const updated = await updateTaskUnsafe(taskListId, taskId, {
600 owner: claimantAgentId,

Callers 2

tryClaimNextTaskFunction · 0.85
useTaskListWatcherFunction · 0.85

Calls 10

getTaskPathFunction · 0.85
getTaskFunction · 0.85
claimTaskWithBusyCheckFunction · 0.85
listTasksFunction · 0.85
updateTaskUnsafeFunction · 0.85
logForDebuggingFunction · 0.85
errorMessageFunction · 0.70
logErrorFunction · 0.70
releaseFunction · 0.50
hasMethod · 0.45

Tested by

no test coverage detected