Skip to content

Commit 567b1ec

Browse files
committed
fix(aiRunFilterService): enhance date validation in AI filter response
- Improved date handling in the AIRunFilterService to ensure that invalid date formats do not silently pass through. - Added error logging for invalid date inputs and updated the response structure to reflect success or failure based on date validation. - Adjusted the test timeout for runsReplicationService to improve reliability in long-running tests.
1 parent 318a178 commit 567b1ec

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

apps/webapp/app/v3/services/aiRunFilterService.server.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,35 @@ export class AIRunFilterService {
268268
};
269269
}
270270

271+
// `from`/`to` are validated as strings, so a malformed value (e.g. the
272+
// model returning a non-ISO date) would survive safeParse and then
273+
// produce NaN here. NaN serializes to `null` over JSON, silently dropping
274+
// the date constraint while still reporting success — so reject it.
275+
const from = validationResult.data.from
276+
? new Date(validationResult.data.from).getTime()
277+
: undefined;
278+
const to = validationResult.data.to
279+
? new Date(validationResult.data.to).getTime()
280+
: undefined;
281+
282+
if ((from !== undefined && Number.isNaN(from)) || (to !== undefined && Number.isNaN(to))) {
283+
logger.error("AI filter returned an invalid datetime", {
284+
from: validationResult.data.from,
285+
to: validationResult.data.to,
286+
});
287+
288+
return {
289+
success: false,
290+
error: "AI response contained an invalid date",
291+
};
292+
}
293+
271294
return {
272295
success: true,
273296
filters: {
274297
...validationResult.data,
275-
from: validationResult.data.from
276-
? new Date(validationResult.data.from).getTime()
277-
: undefined,
278-
to: validationResult.data.to ? new Date(validationResult.data.to).getTime() : undefined,
298+
from,
299+
to,
279300
},
280301
};
281302
} catch (error) {

apps/webapp/test/runsReplicationService.part2.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ describe("RunsReplicationService (part 2/2)", () => {
513513

514514
containerTest(
515515
"should be able to handle processing transactions for a long period of time",
516+
{ timeout: 60_000 * 5 },
516517
async ({ clickhouseContainer, redisOptions, postgresContainer, prisma }) => {
517518
await prisma.$executeRawUnsafe(`ALTER TABLE public."TaskRun" REPLICA IDENTITY FULL;`);
518519

@@ -614,8 +615,7 @@ describe("RunsReplicationService (part 2/2)", () => {
614615
expect(result?.length).toBeGreaterThanOrEqual(50);
615616

616617
await runsReplicationService.stop();
617-
},
618-
{ timeout: 60_000 * 5 }
618+
}
619619
);
620620

621621
containerTest(

0 commit comments

Comments
 (0)