Skip to content

Commit c1b42c1

Browse files
fix(client-runtime): last user message sort ignores agent-posted messages (#33873)
1 parent 39f465a commit c1b42c1

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

‎packages/client-runtime/src/state/threadSearch.ts‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,7 @@ function buildThreadSearchSubject(
246246
projectTitle: string | null | undefined,
247247
environmentLabel: string | null | undefined,
248248
): ThreadSearchSubject {
249-
// What the user wrote, not agent wakes or task notices in the user role. A
250-
// thread the user never wrote in counts from its creation; an older server
251-
// omits the field, so the latest user-role message stands in.
252-
const authored = thread.latestUserAuthoredMessageAt;
253-
const activityAt =
254-
authored === undefined
255-
? getThreadSortTimestamp(thread, "updated_at")
256-
: Date.parse(authored ?? thread.createdAt);
249+
const activityAt = getThreadSortTimestamp(thread, "updated_at");
257250
const localOpenedAt = localThreadOpens.get(`${thread.environmentId}:${thread.id}`) ?? 0;
258251
return {
259252
id: thread.id,

‎packages/client-runtime/src/state/threadSort.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,26 @@ describe("sortThreads", () => {
172172

173173
expect(sorted.map((thread) => thread.id)).toEqual(["thread-1", "thread-2"]);
174174
});
175+
176+
it("orders last user message by what the user wrote, not agent messages in the user role", () => {
177+
const sorted = sortThreads(
178+
[
179+
makeThread({
180+
id: "agent-posted",
181+
latestUserAuthoredMessageAt: "2026-03-09T10:00:00.000Z",
182+
latestUserMessageAt: "2026-03-09T10:30:00.000Z",
183+
}),
184+
makeThread({
185+
id: "user-wrote",
186+
latestUserAuthoredMessageAt: "2026-03-09T10:10:00.000Z",
187+
latestUserMessageAt: "2026-03-09T10:10:00.000Z",
188+
}),
189+
],
190+
"updated_at",
191+
);
192+
193+
expect(sorted.map((thread) => thread.id)).toEqual(["user-wrote", "agent-posted"]);
194+
});
175195
});
176196

177197
describe("planPinnedReorder with hidden rows", () => {

‎packages/client-runtime/src/state/threadSort.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface ThreadSortInput {
66
readonly createdAt: string;
77
readonly updatedAt: string;
88
readonly latestUserMessageAt?: string | null;
9+
readonly latestUserAuthoredMessageAt?: string | null;
910
readonly messages?: ReadonlyArray<{
1011
readonly createdAt: string;
1112
readonly role: string;
@@ -80,6 +81,18 @@ function getFirstSortableTimestamp(...values: Array<string | null | undefined>):
8081
}
8182

8283
function getLatestUserMessageTimestamp(thread: ThreadSortInput): number {
84+
// Agents also post in the user role; only what the user wrote counts. A thread
85+
// the user never wrote in counts from its creation; older servers omit the field.
86+
if (thread.latestUserAuthoredMessageAt !== undefined) {
87+
return (
88+
getFirstSortableTimestamp(
89+
thread.latestUserAuthoredMessageAt,
90+
thread.createdAt,
91+
thread.updatedAt,
92+
) ?? Number.NEGATIVE_INFINITY
93+
);
94+
}
95+
8396
if (thread.latestUserMessageAt) {
8497
const latestUserMessageTimestamp = toSortableTimestamp(thread.latestUserMessageAt);
8598
if (latestUserMessageTimestamp !== null) {

0 commit comments

Comments
 (0)