Skip to content

Commit 65e5e38

Browse files
committed
fix: convert to BigInt before nanosecond multiplication in all instances
Fixes triggerdotdev#3292 Multiple functions compute nanosecond timestamps as BigInt(milliseconds * 1_000_000). When milliseconds exceed Number.MAX_SAFE_INTEGER / 1_000_000 (≈ 9.007e9, i.e. any date after ~1970-04-15), the multiplication overflows IEEE 754 precision before the BigInt conversion captures it. Fix all instances (including runEngineHandlers.server.ts:432 which was missed in the previous PR triggerdotdev#3378) by converting to BigInt first: BigInt(milliseconds) * BigInt(1_000_000) Made-with: Cursor
1 parent 73ea586 commit 65e5e38

3 files changed

Lines changed: 7 additions & 5 deletions

File tree

apps/webapp/app/v3/eventRepository/common.server.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function extractContextFromCarrier(carrier: Record<string, unknown>) {
2121
}
2222

2323
export function getNowInNanoseconds(): bigint {
24-
return BigInt(new Date().getTime() * 1_000_000);
24+
return BigInt(new Date().getTime()) * BigInt(1_000_000);
2525
}
2626

2727
export function getDateFromNanoseconds(nanoseconds: bigint): Date {
@@ -35,7 +35,7 @@ export function calculateDurationFromStart(
3535
) {
3636
const $endtime = typeof endTime === "string" ? new Date(endTime) : endTime;
3737

38-
const duration = Number(BigInt($endtime.getTime() * 1_000_000) - startTime);
38+
const duration = Number(BigInt($endtime.getTime()) * BigInt(1_000_000) - startTime);
3939

4040
if (minimumDuration && duration < minimumDuration) {
4141
return minimumDuration;
@@ -47,7 +47,9 @@ export function calculateDurationFromStart(
4747
export function calculateDurationFromStartJsDate(startTime: Date, endTime: Date = new Date()) {
4848
const $endtime = typeof endTime === "string" ? new Date(endTime) : endTime;
4949

50-
return ($endtime.getTime() - startTime.getTime()) * 1_000_000;
50+
return Number(
51+
(BigInt($endtime.getTime()) - BigInt(startTime.getTime())) * BigInt(1_000_000)
52+
);
5153
}
5254

5355
export function convertDateToNanoseconds(date: Date): bigint {

apps/webapp/app/v3/eventRepository/index.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ async function recordRunEvent(
215215
runId: foundRun.friendlyId,
216216
...attributes,
217217
},
218-
startTime: BigInt((startTime?.getTime() ?? Date.now()) * 1_000_000),
218+
startTime: BigInt(startTime?.getTime() ?? Date.now()) * BigInt(1_000_000),
219219
...optionsRest,
220220
});
221221

apps/webapp/app/v3/runEngineHandlers.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ export function registerRunEngineEventBusHandlers() {
429429
const eventRepository = resolveEventRepositoryForStore(run.taskEventStore);
430430

431431
await eventRepository.recordEvent(retryMessage, {
432-
startTime: BigInt(time.getTime() * 1000000),
432+
startTime: BigInt(time.getTime()) * BigInt(1_000_000),
433433
taskSlug: run.taskIdentifier,
434434
environment,
435435
attributes: {

0 commit comments

Comments
 (0)