Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-workflow-timers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@voltagent/core": patch
---

Keep workflow sleep steps pending for their requested duration when it exceeds the Node.js timer limit.
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
37 changes: 37 additions & 0 deletions packages/core/src/workflow/steps/signal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { waitWithSignal } from "./signal";

const MAX_TIMEOUT_MS = 2_147_483_647;

afterEach(() => {
vi.useRealTimers();
});

describe("waitWithSignal", () => {
it("waits for the full duration when it exceeds the timer limit", async () => {
vi.useFakeTimers();

const onComplete = vi.fn();
const wait = waitWithSignal(MAX_TIMEOUT_MS + 100).then(onComplete);

await vi.advanceTimersByTimeAsync(MAX_TIMEOUT_MS + 99);
expect(onComplete).not.toHaveBeenCalled();

await vi.advanceTimersByTimeAsync(1);
await wait;
expect(onComplete).toHaveBeenCalledOnce();
});

it("can cancel a long wait after the first timer elapses", async () => {
vi.useFakeTimers();

const controller = new AbortController();
const wait = waitWithSignal(MAX_TIMEOUT_MS + 100, controller.signal);

await vi.advanceTimersByTimeAsync(MAX_TIMEOUT_MS);
controller.abort({ type: "cancelled" });

await expect(wait).rejects.toThrow("WORKFLOW_CANCELLED");
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
expect(vi.getTimerCount()).toBe(0);
});
});
16 changes: 14 additions & 2 deletions packages/core/src/workflow/steps/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,21 @@ export const throwIfAborted = (signal?: AbortSignal) => {
}
};

export const waitWithSignal = (ms: number, signal?: AbortSignal) => {
const durationMs = Number.isFinite(ms) ? Math.max(0, ms) : 0;
// Node.js runs timers above the signed 32-bit limit after just 1 ms.
const MAX_TIMEOUT_MS = 2_147_483_647;

export const waitWithSignal = async (ms: number, signal?: AbortSignal) => {
let remainingMs = Number.isFinite(ms) ? Math.max(0, ms) : 0;

while (remainingMs > MAX_TIMEOUT_MS) {
await waitForTimer(MAX_TIMEOUT_MS, signal);
remainingMs -= MAX_TIMEOUT_MS;
}

await waitForTimer(remainingMs, signal);
};

const waitForTimer = (durationMs: number, signal?: AbortSignal) => {
if (!signal) {
return new Promise<void>((resolve) => {
setTimeout(resolve, durationMs);
Expand Down