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