(taskId: string)
| 42 | * 7. Update task status |
| 43 | */ |
| 44 | export async function executeInboxTask(taskId: string): Promise<void> { |
| 45 | const [inboxTask] = await db |
| 46 | .select() |
| 47 | .from(mothershipInboxTask) |
| 48 | .where(eq(mothershipInboxTask.id, taskId)) |
| 49 | .limit(1) |
| 50 | |
| 51 | if (!inboxTask) { |
| 52 | logger.error('Inbox task not found', { taskId }) |
| 53 | return |
| 54 | } |
| 55 | |
| 56 | if (inboxTask.status === 'completed' || inboxTask.status === 'failed') { |
| 57 | logger.info('Inbox task already terminal, skipping', { taskId, status: inboxTask.status }) |
| 58 | return |
| 59 | } |
| 60 | |
| 61 | const [ws] = await db |
| 62 | .select({ |
| 63 | id: workspace.id, |
| 64 | ownerId: workspace.ownerId, |
| 65 | inboxProviderId: workspace.inboxProviderId, |
| 66 | }) |
| 67 | .from(workspace) |
| 68 | .where(eq(workspace.id, inboxTask.workspaceId)) |
| 69 | .limit(1) |
| 70 | |
| 71 | if (!ws) { |
| 72 | logger.error('Workspace not found for inbox task', { |
| 73 | taskId, |
| 74 | workspaceId: inboxTask.workspaceId, |
| 75 | }) |
| 76 | await markTaskFailed(taskId, 'Workspace not found') |
| 77 | return |
| 78 | } |
| 79 | |
| 80 | let chatId = inboxTask.chatId |
| 81 | let responseSent = false |
| 82 | |
| 83 | try { |
| 84 | const [[claimed], userId] = await Promise.all([ |
| 85 | db |
| 86 | .update(mothershipInboxTask) |
| 87 | .set({ status: 'processing', processingStartedAt: new Date() }) |
| 88 | .where(and(eq(mothershipInboxTask.id, taskId), eq(mothershipInboxTask.status, 'received'))) |
| 89 | .returning({ id: mothershipInboxTask.id }), |
| 90 | resolveUserId(inboxTask.fromEmail, ws), |
| 91 | ]) |
| 92 | |
| 93 | if (!claimed) { |
| 94 | logger.info('Task already claimed by another execution, skipping', { taskId }) |
| 95 | return |
| 96 | } |
| 97 | |
| 98 | // Blocked senders and banned accounts must not drive the agent; the sender |
| 99 | // email is checked directly (domain list + the sender's own account ban) |
| 100 | // because non-members resolve to the workspace owner, and the workspace |
| 101 | // billed account is checked to match preprocessExecution's gate. Fails |
no test coverage detected