Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pm-stack/aggregator/src/sources/opencode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,17 @@ async function getJson<T>(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;
Expand Down Expand Up @@ -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);
Expand Down
39 changes: 39 additions & 0 deletions pm-stack/aggregator/test/opencode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -82,6 +104,8 @@ const messages: Record<string, unknown[]> = {
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<typeof Bun.serve>;
Expand Down Expand Up @@ -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);
Expand Down