From 8de9397745b383945ff1de45fd4b12dc90b9e72d Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 09:27:27 -0500 Subject: [PATCH] fix(aggregator/opencode): #2286 gate iso() and freshness inclusion on Date-range validity --- pm-stack/aggregator/src/sources/opencode.ts | 13 +++++-- pm-stack/aggregator/test/opencode.test.ts | 39 +++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/pm-stack/aggregator/src/sources/opencode.ts b/pm-stack/aggregator/src/sources/opencode.ts index 268b71b98e..cb73fdb2c0 100644 --- a/pm-stack/aggregator/src/sources/opencode.ts +++ b/pm-stack/aggregator/src/sources/opencode.ts @@ -83,8 +83,17 @@ async function getJson(target: OpenCodeTarget, path: string, timeoutMs = 10_0 } } +// JS Dates cover +/-8.64e15 ms; a server-supplied stamp outside +// that range makes toISOString() throw RangeError, and one bad +// session used to drop every session from its server (#2286). +// One shared predicate gates both iso() and the freshness +// inclusion below: finite, positive, in Date range. +const MAX_DATE_MS = 8.64e15; +const validMs = (ms: number | undefined): boolean => + typeof ms === "number" && Number.isFinite(ms) && ms > 0 && ms <= MAX_DATE_MS; + const iso = (ms: number | undefined): string | null => - typeof ms === "number" && ms > 0 ? new Date(ms).toISOString() : null; + validMs(ms) ? new Date(ms as number).toISOString() : null; function errText(e: unknown): string | null { if (!e) return null; @@ -143,7 +152,7 @@ export async function mapServerSessions( (s) => activeIds.has(s.id) || pendingBySession.has(s.id) || - (typeof s.time?.updated === "number" && now - s.time.updated <= ATTENTION_WINDOW_MS), + (validMs(s.time?.updated) && now - (s.time?.updated as number) <= ATTENTION_WINDOW_MS), ) .sort((a, b) => (b.time?.updated ?? 0) - (a.time?.updated ?? 0)) .slice(0, opts.maxDetailed ?? 60); diff --git a/pm-stack/aggregator/test/opencode.test.ts b/pm-stack/aggregator/test/opencode.test.ts index 34d4623aec..71b3e7da3c 100644 --- a/pm-stack/aggregator/test/opencode.test.ts +++ b/pm-stack/aggregator/test/opencode.test.ts @@ -44,6 +44,28 @@ const sessions = [ time: { created: old, updated: old }, location: { directory: "/work/d" }, }, + { + // #2286: out-of-range updated stamp — must not earn freshness + // inclusion and must not throw the whole server away. + id: "ses_bad_updated", + title: "bad updated", + model: { id: "gpt-5.6-sol", providerID: "openai" }, + tokens: { input: 1, output: 1, reasoning: 0 }, + outcome: null, + time: { created: recent - 10_000, updated: 1e16 }, + location: { directory: "/work/e" }, + }, + { + // #2286: out-of-range created stamp, fresh updated — included + // via freshness, with a null createdAt instead of a throw. + id: "ses_bad_created", + title: "bad created", + model: { id: "gpt-5.6-sol", providerID: "openai" }, + tokens: { input: 1, output: 1, reasoning: 0 }, + outcome: null, + time: { created: 1e16, updated: recent }, + location: { directory: "/work/f" }, + }, { id: "ses_child", title: "explore child", @@ -82,6 +104,8 @@ const messages: Record = { ses_question: [{ id: "m1", type: "user", time: { created: recent }, text: "go" }], ses_stale: [{ id: "m1", type: "user", time: { created: old }, text: "go" }], ses_child: [{ id: "m1", type: "user", time: { created: recent }, text: "explore" }], + ses_bad_updated: [{ id: "m1", type: "user", time: { created: recent }, text: "go" }], + ses_bad_created: [{ id: "m1", type: "user", time: { created: 1e16 }, text: "go" }], }; let server: ReturnType; @@ -142,6 +166,21 @@ describe("opencode v2 mapper", () => { expect(child.createdAt).toBeTruthy(); }); + test("out-of-range stamps do not drop the server's sessions (#2286)", async () => { + const mapped = await mapServerSessions({ baseUrl, password: "x" }, NOW); + expect(mapped).not.toBeNull(); + const byId = new Map(mapped!.entries.map((e) => [e.sessionId, e])); + // The good sessions survive. + expect(byId.has("ses_active")).toBe(true); + // 1e16 updated earns no freshness inclusion. + expect(byId.has("ses_bad_updated")).toBe(false); + // 1e16 created maps with a null stamp, not a RangeError. + const badCreated = byId.get("ses_bad_created")!; + expect(badCreated).toBeTruthy(); + expect(badCreated.createdAt ?? null).toBeNull(); + expect(badCreated.lastActivityAt).toBeTruthy(); + }); + test("stamps the entry source when asked", async () => { const mapped = await mapServerSessions({ baseUrl, password: "x" }, NOW, { source: "opencode-laptop" }); expect(mapped!.entries.every((e) => e.source === "opencode-laptop")).toBe(true);