diff --git a/pm-stack/aggregator/src/registry.ts b/pm-stack/aggregator/src/registry.ts index 43e03aebe6..6de0132d1a 100644 --- a/pm-stack/aggregator/src/registry.ts +++ b/pm-stack/aggregator/src/registry.ts @@ -8,6 +8,7 @@ // Loose v1 by design: identity is (source, session id); task linkage is // whatever refs the session's title/PR/branch or launch record names. import { parseRef, refsInText, type SessionFeedEntry, type SessionRecord } from "./model.ts"; +import { isIsoDate } from "./validate.ts"; function isValidTaskRef(value: unknown): value is string { if (typeof value !== "string") return false; @@ -170,12 +171,18 @@ export function isSessionRecord(v: unknown): v is SessionRecord { // this filter today and throws TypeError once the sort reaches it. // Timestamps must be string-or-absent; a poisoned record is rejected, // not coerced (the #2347 load-filter precedent). + // #5078: a string is not enough — mergeRecords string-compares + // lastSeenAt, so a non-ISO stamp ("March 2, 2026" sorts after every + // ISO stamp) wins recency forever, and impossible dates (2026-02-30) + // roll to other instants in registryActivityMs. Present timestamps + // must be real ISO dates via isIsoDate, as isSnapshotRecord and the + // charge/subscription row guards already require; absent stays fine. return ( typeof r.id === "string" && typeof r.source === "string" && typeof r.label === "string" && - (r.lastSeenAt === undefined || typeof r.lastSeenAt === "string") && - (r.startedAt === undefined || typeof r.startedAt === "string") && + (r.lastSeenAt === undefined || isIsoDate(r.lastSeenAt)) && + (r.startedAt === undefined || isIsoDate(r.startedAt)) && // #2408/#2485/#4284: mergeRecords spreads taskRefs as an array, and // querying/rendering dereferences it unconditionally (spread in // matchesText, .some in the ?task= filter and the item-page linked- diff --git a/pm-stack/aggregator/test/registry-isodate-5078.test.ts b/pm-stack/aggregator/test/registry-isodate-5078.test.ts new file mode 100644 index 0000000000..b3e80f0481 --- /dev/null +++ b/pm-stack/aggregator/test/registry-isodate-5078.test.ts @@ -0,0 +1,59 @@ +import { describe, expect, test } from "bun:test"; +import { isSessionRecord } from "../src/registry.ts"; +import type { SessionRecord } from "../src/model.ts"; + +// #5078: isSessionRecord gated lastSeenAt/startedAt on typeof string only +// (the #2376 type fix), never on date validity. Impossible dates +// (2026-02-30) roll to other instants in registryActivityMs, and a +// non-ISO stamp ("March 2, 2026") string-compares after every ISO stamp +// in mergeRecords, winning recency forever. Present timestamps must now +// be real ISO dates via the existing isIsoDate helper; absent timestamps +// stay allowed. + +function rec(partial: Partial & { id: string }): SessionRecord { + return { + source: "claude-code", + label: partial.id, + agentId: "claude-code", + agentLabel: "Muse", + parentId: null, + status: "succeeded", + taskRefs: [], + ...partial, + } as SessionRecord; +} + +const valid = rec({ + id: "ses_valid", + startedAt: "2026-10-31T00:00:00Z", + lastSeenAt: "2026-11-01T00:00:00Z", +}); + +const INVALID_STAMPS = ["2026-02-30", "2026-04-31", "March 2, 2026", "not-a-date"]; + +describe("isSessionRecord timestamp validity (#5078)", () => { + test("rejects impossible and non-ISO lastSeenAt", () => { + for (const stamp of INVALID_STAMPS) { + expect(isSessionRecord({ ...valid, lastSeenAt: stamp })).toBe(false); + } + }); + + test("rejects impossible and non-ISO startedAt", () => { + for (const stamp of INVALID_STAMPS) { + expect(isSessionRecord({ ...valid, startedAt: stamp })).toBe(false); + } + }); + + test("accepts valid ISO timestamps, including offsets and fractions", () => { + expect(isSessionRecord(valid)).toBe(true); + expect(isSessionRecord({ ...valid, lastSeenAt: "2026-11-01T00:00:00.123+05:30" })).toBe(true); + expect(isSessionRecord({ ...valid, startedAt: "2026-10-31T00:00:00-04:00" })).toBe(true); + expect(isSessionRecord({ ...valid, lastSeenAt: "2026-11-01" })).toBe(true); + }); + + test("accepts absent timestamps", () => { + expect(isSessionRecord({ id: "a", source: "s", label: "l", taskRefs: [] })).toBe(true); + const { lastSeenAt: _ls, startedAt: _st, ...absent } = valid; + expect(isSessionRecord(absent)).toBe(true); + }); +});