From 6d7de0b314cfabe64c8e3c029680c3f63d92c10e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 1 May 2026 14:45:43 -0700 Subject: [PATCH] Normalize timestamps across VCS and source control contracts - Switch freshness and PR update times to typed `DateTime`/`Option` values - Update GitHub and VCS adapters, tests, and sorting to use the new shapes --- apps/server/src/git/Layers/GitManager.ts | 30 +++++++++++-------- apps/server/src/git/githubPullRequests.ts | 9 +++--- apps/server/src/server.test.ts | 15 +++++++--- .../GitHubSourceControlProvider.test.ts | 9 ++++-- .../GitHubSourceControlProvider.ts | 4 +-- apps/server/src/vcs/GitVcsDriver.ts | 5 ++-- .../vcs/testing/VcsDriverContractHarness.ts | 7 +++-- packages/contracts/src/sourceControl.ts | 2 +- packages/contracts/src/vcs.ts | 4 +-- 9 files changed, 51 insertions(+), 34 deletions(-) diff --git a/apps/server/src/git/Layers/GitManager.ts b/apps/server/src/git/Layers/GitManager.ts index 1f993faeed..2ebeef820c 100644 --- a/apps/server/src/git/Layers/GitManager.ts +++ b/apps/server/src/git/Layers/GitManager.ts @@ -71,7 +71,7 @@ interface OpenPrInfo { interface PullRequestInfo extends OpenPrInfo, PullRequestHeadRemoteInfo { state: "open" | "closed" | "merged"; - updatedAt: string | null; + updatedAt: Option.Option; } interface ResolvedPullRequest { @@ -273,12 +273,18 @@ function toPullRequestInfo(summary: ChangeRequest): PullRequestInfo { }; } -function parseDateTimeMillis(value: string | null): number { - if (!value) { - return 0; - } - const parsed = DateTime.make(value); - return Option.isSome(parsed) ? DateTime.toEpochMillis(parsed.value) : 0; +function compareOptionalDateTimeDesc( + left: Option.Option, + right: Option.Option, +): number { + return Option.match(left, { + onNone: () => (Option.isNone(right) ? 0 : 1), + onSome: (leftValue) => + Option.match(right, { + onNone: () => -1, + onSome: (rightValue) => DateTime.Order(rightValue, leftValue), + }), + }); } function gitManagerError(operation: string, detail: string, cause?: unknown): GitManagerError { @@ -858,7 +864,7 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () { baseRefName: firstPullRequest.baseRefName, headRefName: firstPullRequest.headRefName, state: "open", - updatedAt: null, + updatedAt: Option.none(), } satisfies PullRequestInfo; } } @@ -889,11 +895,9 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () { } } - const parsed = Array.from(parsedByNumber.values()).toSorted((a, b) => { - const left = parseDateTimeMillis(a.updatedAt); - const right = parseDateTimeMillis(b.updatedAt); - return right - left; - }); + const parsed = Array.from(parsedByNumber.values()).toSorted((a, b) => + compareOptionalDateTimeDesc(a.updatedAt, b.updatedAt), + ); const latestOpenPr = parsed.find((pr) => pr.state === "open"); if (latestOpenPr) { diff --git a/apps/server/src/git/githubPullRequests.ts b/apps/server/src/git/githubPullRequests.ts index d137a46d6f..f0804dda8c 100644 --- a/apps/server/src/git/githubPullRequests.ts +++ b/apps/server/src/git/githubPullRequests.ts @@ -1,4 +1,4 @@ -import { Cause, Exit, Result, Schema } from "effect"; +import { Cause, DateTime, Exit, Option, Result, Schema } from "effect"; import { PositiveInt, TrimmedNonEmptyString } from "@t3tools/contracts"; import { decodeJsonResult, formatSchemaError } from "@t3tools/shared/schemaJson"; @@ -9,7 +9,7 @@ export interface NormalizedGitHubPullRequestRecord { readonly baseRefName: string; readonly headRefName: string; readonly state: "open" | "closed" | "merged"; - readonly updatedAt: string | null; + readonly updatedAt: Option.Option; readonly isCrossRepository?: boolean; readonly headRepositoryNameWithOwner?: string | null; readonly headRepositoryOwnerLogin?: string | null; @@ -23,7 +23,7 @@ const GitHubPullRequestSchema = Schema.Struct({ headRefName: TrimmedNonEmptyString, state: Schema.optional(Schema.NullOr(Schema.String)), mergedAt: Schema.optional(Schema.NullOr(Schema.String)), - updatedAt: Schema.optional(Schema.NullOr(Schema.String)), + updatedAt: Schema.optional(Schema.OptionFromNullOr(Schema.DateTimeUtcFromString)), isCrossRepository: Schema.optional(Schema.Boolean), headRepository: Schema.optional( Schema.NullOr( @@ -80,8 +80,7 @@ function normalizeGitHubPullRequestRecord( baseRefName: raw.baseRefName, headRefName: raw.headRefName, state: normalizeGitHubPullRequestState(raw), - updatedAt: - typeof raw.updatedAt === "string" && raw.updatedAt.trim().length > 0 ? raw.updatedAt : null, + updatedAt: raw.updatedAt ?? Option.none(), ...(typeof raw.isCrossRepository === "boolean" ? { isCrossRepository: raw.isCrossRepository } : {}), diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.ts index 487845d574..2096cf7691 100644 --- a/apps/server/src/server.test.ts +++ b/apps/server/src/server.test.ts @@ -29,6 +29,7 @@ import { assert, it } from "@effect/vitest"; import { assertFailure, assertInclude, assertTrue } from "@effect/vitest/utils"; import { Deferred, + DateTime, Duration, Effect, FileSystem, @@ -50,6 +51,8 @@ import { RpcClient, RpcSerialization } from "effect/unstable/rpc"; import * as Socket from "effect/unstable/socket/Socket"; import { vi } from "vitest"; +const TEST_EPOCH = DateTime.makeUnsafe("1970-01-01T00:00:00.000Z"); + import type { ServerConfigShape } from "./config.ts"; import { deriveServerPaths, ServerConfig } from "./config.ts"; import { makeRoutesLayer } from "./server.ts"; @@ -395,7 +398,8 @@ const buildAppUnderTest = (options?: { truncated: false, freshness: { source: "live-local", - observedAt: new Date(0).toISOString(), + observedAt: TEST_EPOCH, + expiresAt: Option.none(), }, }), filterIgnoredPaths: (_cwd, relativePaths) => Effect.succeed(relativePaths), @@ -416,7 +420,8 @@ const buildAppUnderTest = (options?: { metadataPath: null, freshness: { source: "live-local" as const, - observedAt: new Date(0).toISOString(), + observedAt: TEST_EPOCH, + expiresAt: Option.none(), }, } : null, @@ -444,7 +449,8 @@ const buildAppUnderTest = (options?: { metadataPath: null, freshness: { source: "live-local", - observedAt: new Date(0).toISOString(), + observedAt: TEST_EPOCH, + expiresAt: Option.none(), }, }, driver: defaultVcsDriver, @@ -2154,7 +2160,8 @@ it.layer(NodeServices.layer)("server router seam", (it) => { truncated: false, freshness: { source: "live-local", - observedAt: new Date(0).toISOString(), + observedAt: TEST_EPOCH, + expiresAt: Option.none(), }, }), filterIgnoredPaths: (_cwd, relativePaths) => diff --git a/apps/server/src/sourceControl/GitHubSourceControlProvider.test.ts b/apps/server/src/sourceControl/GitHubSourceControlProvider.test.ts index 485059d48a..7382d00066 100644 --- a/apps/server/src/sourceControl/GitHubSourceControlProvider.test.ts +++ b/apps/server/src/sourceControl/GitHubSourceControlProvider.test.ts @@ -1,5 +1,5 @@ import { assert, it } from "@effect/vitest"; -import { Effect, Layer } from "effect"; +import { DateTime, Effect, Layer, Option } from "effect"; import { GitHubCli, type GitHubCliShape } from "../git/Services/GitHubCli.ts"; import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts"; @@ -46,7 +46,7 @@ it.effect("maps GitHub PR summaries into provider-neutral change requests", () = baseRefName: "main", headRefName: "feature/source-control", state: "open", - updatedAt: null, + updatedAt: Option.none(), isCrossRepository: true, headRepositoryNameWithOwner: "fork/t3code", headRepositoryOwnerLogin: "fork", @@ -99,7 +99,10 @@ it.effect("uses gh json listing for non-open change request state queries", () = ]); assert.strictEqual(changeRequests[0]?.provider, "github"); assert.strictEqual(changeRequests[0]?.state, "merged"); - assert.strictEqual(changeRequests[0]?.updatedAt, "2026-01-02T00:00:00.000Z"); + assert.deepStrictEqual( + changeRequests[0]?.updatedAt, + Option.some(DateTime.makeUnsafe("2026-01-02T00:00:00.000Z")), + ); }), ); diff --git a/apps/server/src/sourceControl/GitHubSourceControlProvider.ts b/apps/server/src/sourceControl/GitHubSourceControlProvider.ts index 2cec4ac598..cba7bc7431 100644 --- a/apps/server/src/sourceControl/GitHubSourceControlProvider.ts +++ b/apps/server/src/sourceControl/GitHubSourceControlProvider.ts @@ -1,4 +1,4 @@ -import { Effect, Layer, Result, Schema } from "effect"; +import { Effect, Layer, Option, Result, Schema } from "effect"; import { SourceControlProviderError, type ChangeRequest, @@ -28,7 +28,7 @@ function toChangeRequest(summary: GitHubPullRequestSummary): ChangeRequest { baseRefName: summary.baseRefName, headRefName: summary.headRefName, state: summary.state ?? "open", - updatedAt: null, + updatedAt: Option.none(), ...(summary.isCrossRepository !== undefined ? { isCrossRepository: summary.isCrossRepository } : {}), diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.ts index 32619ccc4d..eb82f6d3ac 100644 --- a/apps/server/src/vcs/GitVcsDriver.ts +++ b/apps/server/src/vcs/GitVcsDriver.ts @@ -1,4 +1,4 @@ -import { Context, DateTime, Effect, Layer } from "effect"; +import { Context, DateTime, Effect, Layer, Option } from "effect"; import { GitCommandError, @@ -204,7 +204,8 @@ const nowFreshness = Effect.fn("GitVcsDriver.nowFreshness")(function* () { const now = yield* DateTime.now; return { source: "live-local" as const, - observedAt: DateTime.formatIso(now), + observedAt: now, + expiresAt: Option.none(), }; }); diff --git a/apps/server/src/vcs/testing/VcsDriverContractHarness.ts b/apps/server/src/vcs/testing/VcsDriverContractHarness.ts index b7108dc043..c0e195558b 100644 --- a/apps/server/src/vcs/testing/VcsDriverContractHarness.ts +++ b/apps/server/src/vcs/testing/VcsDriverContractHarness.ts @@ -1,4 +1,5 @@ import { assert, it } from "@effect/vitest"; +import { DateTime, Option } from "effect"; import { Effect, FileSystem, Layer, Path, type PlatformError, type Scope } from "effect"; import { describe } from "vitest"; @@ -59,7 +60,8 @@ export function runVcsDriverContractSuite(input: VcsDriverContractSuiteInp assert.equal(identity?.kind, input.kind); assert.isTrue(identity?.rootPath.endsWith(cwd)); assert.equal(identity?.freshness.source, "live-local"); - assert.equal(typeof identity?.freshness.observedAt, "string"); + assert.isTrue(DateTime.isDateTime(identity?.freshness.observedAt)); + assert.isTrue(Option.isNone(identity?.freshness.expiresAt ?? Option.none())); assert.equal(yield* driver.isInsideWorkTree(cwd), true); const path = yield* Path.Path; @@ -91,7 +93,8 @@ export function runVcsDriverContractSuite(input: VcsDriverContractSuiteInp assert.include(result.paths, "untracked.ts"); assert.equal(result.truncated, false); assert.equal(result.freshness.source, "live-local"); - assert.equal(typeof result.freshness.observedAt, "string"); + assert.isTrue(DateTime.isDateTime(result.freshness.observedAt)); + assert.isTrue(Option.isNone(result.freshness.expiresAt)); }), ); diff --git a/packages/contracts/src/sourceControl.ts b/packages/contracts/src/sourceControl.ts index 55e6710180..7fc437324a 100644 --- a/packages/contracts/src/sourceControl.ts +++ b/packages/contracts/src/sourceControl.ts @@ -20,7 +20,7 @@ export const ChangeRequest = Schema.Struct({ baseRefName: TrimmedNonEmptyString, headRefName: TrimmedNonEmptyString, state: ChangeRequestState, - updatedAt: Schema.NullOr(TrimmedNonEmptyString), + updatedAt: Schema.Option(Schema.DateTimeUtc), isCrossRepository: Schema.optional(Schema.Boolean), headRepositoryNameWithOwner: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)), headRepositoryOwnerLogin: Schema.optional(Schema.NullOr(TrimmedNonEmptyString)), diff --git a/packages/contracts/src/vcs.ts b/packages/contracts/src/vcs.ts index 3bef29a479..bd9a81f7e2 100644 --- a/packages/contracts/src/vcs.ts +++ b/packages/contracts/src/vcs.ts @@ -14,8 +14,8 @@ export type VcsFreshnessSource = typeof VcsFreshnessSource.Type; export const VcsFreshness = Schema.Struct({ source: VcsFreshnessSource, - observedAt: TrimmedNonEmptyString, - expiresAt: Schema.optional(TrimmedNonEmptyString), + observedAt: Schema.DateTimeUtc, + expiresAt: Schema.Option(Schema.DateTimeUtc), }); export type VcsFreshness = typeof VcsFreshness.Type;