[codex] fix: tolerate invalid latest user message timestamps (#3521)

Co-authored-by: Codex <[email protected]>
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-07-17 23:24:52 +02:00
1 parent b655a9f183
commit 43e2aeab38
2 files changed
+75 -1

No files matched your search

@@ -0,0 +1,71 @@
import { describe, expect, it } from "vite-plus/test";
import { sortThreads, type ThreadSortInput } from "./threadSort.ts";
type TestThread = { readonly id: string } & ThreadSortInput;
function makeThread(overrides: Partial<TestThread> = {}): TestThread {
return {
id: "thread-1",
createdAt: "2026-03-09T10:00:00.000Z",
updatedAt: "2026-03-09T10:00:00.000Z",
messages: [],
latestUserMessageAt: null,
...overrides,
};
}
describe("sortThreads", () => {
it("falls back to updatedAt and createdAt when latestUserMessageAt is invalid and there are no messages", () => {
const sorted = sortThreads(
[
makeThread({
id: "thread-1",
latestUserMessageAt: "not-a-date",
createdAt: "2026-03-09T10:00:00.000Z",
updatedAt: "2026-03-09T10:05:00.000Z",
}),
makeThread({
id: "thread-2",
latestUserMessageAt: "still-not-a-date",
createdAt: "invalid-created-at",
updatedAt: "invalid-updated-at",
}),
makeThread({
id: "thread-3",
latestUserMessageAt: "invalid-latest-user-message-at",
createdAt: "2026-03-09T10:06:00.000Z",
updatedAt: "invalid-updated-at",
}),
],
"updated_at",
);
expect(sorted.map((thread) => thread.id)).toEqual(["thread-3", "thread-1", "thread-2"]);
});
it("falls back to the latest valid user message when latestUserMessageAt is invalid", () => {
const sorted = sortThreads(
[
makeThread({
id: "thread-1",
latestUserMessageAt: "invalid-latest-user-message-at",
updatedAt: "2026-03-09T10:00:00.000Z",
messages: [
{ role: "user", createdAt: "2026-03-09T10:05:00.000Z" },
{ role: "assistant", createdAt: "2026-03-09T10:30:00.000Z" },
{ role: "user", createdAt: "2026-03-09T10:20:00.000Z" },
],
}),
makeThread({
id: "thread-2",
createdAt: "2026-03-09T10:15:00.000Z",
updatedAt: "2026-03-09T10:15:00.000Z",
}),
],
"updated_at",
);
expect(sorted.map((thread) => thread.id)).toEqual(["thread-1", "thread-2"]);
});
});
@@ -32,7 +32,10 @@ function getFirstSortableTimestamp(...values: Array<string | null | undefined>):
function getLatestUserMessageTimestamp(thread: ThreadSortInput): number {
if (thread.latestUserMessageAt) {
return toSortableTimestamp(thread.latestUserMessageAt) ?? Number.NEGATIVE_INFINITY;
const latestUserMessageTimestamp = toSortableTimestamp(thread.latestUserMessageAt);
if (latestUserMessageTimestamp !== null) {
return latestUserMessageTimestamp;
}
}
let latestUserMessageTimestamp: number | null = null;