(input: {
taskId: string;
errorText?: string;
})
| 608 | } |
| 609 | |
| 610 | export function cancelQueuedTaskSync(input: { |
| 611 | taskId: string; |
| 612 | errorText?: string; |
| 613 | }): QueuedTaskRecord { |
| 614 | const db = getDatabase(); |
| 615 | const now = new Date().toISOString(); |
| 616 | const previous = readQueuedTaskSync(input.taskId); |
| 617 | db.prepare( |
| 618 | `UPDATE agent_task_queue |
| 619 | SET status = 'cancelled', |
| 620 | error_text = COALESCE(?, error_text), |
| 621 | finished_at = ?, |
| 622 | updated_at = ? |
| 623 | WHERE id = ? AND status = 'queued'`, |
| 624 | ).run(input.errorText ?? null, now, now, input.taskId); |
| 625 | |
| 626 | const task = readQueuedTaskSync(input.taskId); |
| 627 | if (!task) { |
| 628 | throw new Error(`Queued task "${input.taskId}" does not exist.`); |
| 629 | } |
| 630 | if (previous?.status !== "cancelled" && task.status === "cancelled") { |
| 631 | const attempt = readLatestAgentTaskAttemptForTaskSync(task.id); |
| 632 | if (attempt) { |
| 633 | updateAgentTaskAttemptSync({ |
| 634 | attemptId: attempt.id, |
| 635 | status: "cancelled", |
| 636 | errorText: input.errorText ?? "Task cancelled.", |
| 637 | }); |
| 638 | } |
| 639 | const runtime = readAgentRuntimeSync(task.runtimeId); |
| 640 | recordRouterLifecycleEvent(task, { |
| 641 | attemptId: attempt?.id, |
| 642 | type: "cancelled", |
| 643 | actorType: "system", |
| 644 | runtimeId: task.runtimeId, |
| 645 | provider: runtime?.provider, |
| 646 | summary: input.errorText ? truncateSummary(input.errorText) : "Task cancelled before execution.", |
| 647 | data: { |
| 648 | attemptId: attempt?.id, |
| 649 | }, |
| 650 | }); |
| 651 | recordQueueLifecycleEvent(task, { |
| 652 | type: "cancelled", |
| 653 | title: "Task cancelled", |
| 654 | summary: input.errorText ? truncateSummary(input.errorText) : "The queued task was cancelled before execution.", |
| 655 | severity: input.errorText ? "warning" : "info", |
| 656 | status: "failed", |
| 657 | }); |
| 658 | } |
| 659 | return task; |
| 660 | } |
| 661 | |
| 662 | function recordQueueLifecycleEvent( |
| 663 | task: QueuedTaskRecord, |
no test coverage detected