Skip to content
TypeScript

fix(source-control): compare check run timestamps by instant (#20094)

Simone6ae212e6dc25
diff --git a/packages/source-control-core/src/server/pullRequestChecks.test.ts b/packages/source-control-core/src/server/pullRequestChecks.test.ts
index 5033e719dec..34c8ed6fefd 100644
--- a/packages/source-control-core/src/server/pullRequestChecks.test.ts
+++ b/packages/source-control-core/src/server/pullRequestChecks.test.ts
@@ -20,6 +20,47 @@ function entry(
}
describe("dedupeChecks", () => {
+ it.each([
+ ["2026-10-16T12:00:00Z", "2026-10-16T12:00:00.500Z"],
+ ["2026-10-16T12:00:00+02:00", "2026-10-16T10:30:00Z"],
+ ["2026-10-16T12:00:00.0001Z", "2026-10-16T12:00:00.0002Z"],
+ ["2026-10-16T12:00:00.0001z", "2026-10-16T12:00:00.0002z"],
+ ["2026-10-16T12:00:00.00009Z", "2026-10-16T12:00:00.0001Z"],
+ ["2024-02-28T23:59:59Z", "2024-02-29T00:00:00.0001Z"],
+ ["2000-02-28T23:59:59Z", "2000-02-29T00:00:00Z"],
+ ["2024-03-01T01:00:00Z", "2024-02-29T23:30:00-02:00"],
+ ["2026-10-16t12:00:00z", "2026-10-16t12:00:00.0001z"],
+ ["+010000-01-01T00:00:00Z", "+010000-01-01T00:00:00.0001Z"],
+ ])("keeps the newer instant between %s and %s in either listing order", (older, newer) => {
+ const oldRun = entry("build", "success", { at: older });
+ const newRun = entry("build", "failure", { at: newer });
+
+ expect(
+ [
+ [oldRun, newRun],
+ [newRun, oldRun],
+ ].map((runs) => dedupeChecks(runs)[0]?.status),
+ ).toEqual(["failure", "failure"]);
+ });
+
+ it.each([
+ ["2026-10-16T12:00:00Z", "2026-10-16T12:00:00.000Z"],
+ ["2026-10-16T12:00:00Z", "2026-10-16T14:00:00+02:00"],
+ ["2026-10-16T24:00:00.000Z", "2026-10-17T00:00:00Z"],
+ ["2026-10-16T12:00Z", "2026-10-16T12:00:00Z"],
+ ])("takes the last copy for equal instants %s and %s", (first, second) => {
+ const forward = dedupeChecks([
+ entry("build", "success", { at: first }),
+ entry("build", "failure", { at: second }),
+ ]);
+ const reversed = dedupeChecks([
+ entry("build", "success", { at: second }),
+ entry("build", "failure", { at: first }),
+ ]);
+
+ expect([forward[0]?.status, reversed[0]?.status]).toEqual(["failure", "failure"]);
+ });
+
it("keeps the newest run of a check the host listed twice", () => {
const checks = dedupeChecks([
entry("Prepare PR size config", "success", {
@@ -37,6 +78,48 @@ describe("dedupeChecks", () => {
]);
});
+ it("preserves text ordering when a host supplies an unparseable timestamp", () => {
+ expect(
+ dedupeChecks([
+ entry("build", "success", { at: "invalid-a" }),
+ entry("build", "failure", { at: "invalid-b" }),
+ ]).map((check) => check.status),
+ ).toEqual(["failure"]);
+ });
+
+ it.each([
+ ["2026-10-16", "2026-10-15T23:00:00-02:00", "success"],
+ ["2026-10-16T00:00:00", "2026-10-15T23:00:00-02:00", "success"],
+ ["2026-10-16T24:00:00.0001Z", "2026-10-17T00:00:00Z", "failure"],
+ ])("uses text fallback for incomplete or invalid timestamp %s", (malformed, valid, expected) => {
+ const malformedRun = entry("build", "success", { at: malformed });
+ const validRun = entry("build", "failure", { at: valid });
+
+ expect(
+ [
+ [malformedRun, validRun],
+ [validRun, malformedRun],
+ ].map((runs) => dedupeChecks(runs)[0]?.status),
+ ).toEqual([expected, expected]);
+ });
+
+ it.each([
+ ["2026-02-30T00:00:00Z", "2026-03-01T00:00:00Z"],
+ ["2026-02-29T00:00:00Z", "2026-03-01T00:00:00Z"],
+ ["1900-02-29T00:00:00Z", "1900-03-01T00:00:00Z"],
+ ["2026-04-31T00:00:00.0001-02:00", "2026-05-01T00:00:00Z"],
+ ])("uses text fallback for impossible date %s", (malformed, valid) => {
+ const malformedRun = entry("build", "success", { at: malformed });
+ const validRun = entry("build", "failure", { at: valid });
+
+ expect(
+ [
+ [malformedRun, validRun],
+ [validRun, malformedRun],
+ ].map((runs) => dedupeChecks(runs)[0]?.status),
+ ).toEqual(["failure", "failure"]);
+ });
+
it("holds a check at the place it first appeared, so a re-run does not reshuffle the list", () => {
const checks = dedupeChecks([
entry("lint", "success", { workflowName: "CI", at: "2026-08-11T16:00:00Z" }),
diff --git a/packages/source-control-core/src/server/pullRequestChecks.ts b/packages/source-control-core/src/server/pullRequestChecks.ts
index ed5b571f942..9119240dd31 100644
--- a/packages/source-control-core/src/server/pullRequestChecks.ts
+++ b/packages/source-control-core/src/server/pullRequestChecks.ts
@@ -1,8 +1,39 @@
import type { PullRequestCheck } from "@t3tools/contracts";
+import * as DateTime from "effect/DateTime";
+import * as Option from "effect/Option";
+
+const zonedDateTimePattern =
+ /^(?:\d{4}|[+-]\d{6})-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d+)?)?|24:00(?::00(?:\.0+)?)?)(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)$/i;
+
+// Parse whole seconds separately so sub-millisecond fractions keep their precision.
+function timestampParts(value: string) {
+ if (!zonedDateTimePattern.test(value)) return { time: Number.NaN, fraction: "" };
+ const fraction = /\.(\d+)(?=Z$|[+-]\d{2}:\d{2}$)/i.exec(value);
+ // Date can normalize impossible days. Check the calendar date before applying the offset.
+ const datePart = value.split(/[Tt]/, 1)[0] ?? "";
+ const date = DateTime.make(`${datePart}T00:00:00Z`);
+ const parts = Option.isSome(date) ? DateTime.toPartsUtc(date.value) : null;
+ const validDate =
+ parts !== null &&
+ parts.month === Number(datePart.slice(-5, -3)) &&
+ parts.day === Number(datePart.slice(-2));
+ return {
+ time: validDate ? Date.parse(fraction ? value.replace(fraction[0], "") : value) : Number.NaN,
+ fraction: fraction?.[1] ?? "",
+ };
+}
function isAtLeastAsNew(candidate: string | null, kept: string | null): boolean {
if (candidate === null) return kept === null;
- return kept === null || candidate >= kept;
+ if (kept === null) return true;
+ const candidateParts = timestampParts(candidate);
+ const keptParts = timestampParts(kept);
+ if (Number.isNaN(candidateParts.time) || Number.isNaN(keptParts.time)) return candidate >= kept;
+ if (candidateParts.time !== keptParts.time) return candidateParts.time > keptParts.time;
+ const precision = Math.max(candidateParts.fraction.length, keptParts.fraction.length);
+ return (
+ candidateParts.fraction.padEnd(precision, "0") >= keptParts.fraction.padEnd(precision, "0")
+ );
}
/**
diff --git a/packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts b/packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts
index 3699b08ba67..60c95a2b786 100644
--- a/packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts
+++ b/packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts
@@ -122,7 +122,7 @@ it.effect("submits a Forgejo review without sending its summary in the prelimina
);
});
-it.effect("reads Forgejo checks without repository or viewer requests", () => {
+it.effect("reads the newest Forgejo checks without repository or viewer requests", () => {
const paths: string[] = [];
return Effect.gen(function* () {
const provider = yield* ForgejoPullRequestProvider.make;
@@ -154,7 +154,7 @@ it.effect("reads Forgejo checks without repository or viewer requests", () => {
"head":{"ref":"feature","sha":"head","repo":null},"base":{"ref":"main","sha":"base","repo":null},
"created_at":"2026-09-16T00:00:00Z","updated_at":"2026-09-16T00:00:00Z","closed_at":null,"merged_at":null,"labels":[]}`
: input.path.endsWith("page=1")
- ? `[{"context":"build","status":"failure","description":null,"target_url":null,"updated_at":"2026-09-16T00:00:00Z"}]`
+ ? `[{"context":"build","status":"failure","description":null,"target_url":null,"updated_at":"2026-09-16T00:00:00.500Z"},{"context":"build","status":"success","description":null,"target_url":null,"updated_at":"2026-09-16T00:00:00Z"}]`
: "[]",
),
);