Skip to content

Commit 28a8d77

Browse files
sergiiopmclaude
andcommitted
fix(client-runtime): count only real returns in inbox send order
With the Working section on, the send order now also keeps a run that is waiting on you (approval or input) on top after a reload, through the stored request time, as the return order does. It also stops trusting a completion time the server did not report: older servers omit it and the shell falls back to `updatedAt`, so a metadata change could move an idle thread. The shell now keeps the reported value as `latestRunCompletedAt`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f21b5ad commit 28a8d77

3 files changed

Lines changed: 63 additions & 6 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ export interface EnvironmentThreadShell {
156156
readonly blockedOnThreadWait?: boolean;
157157
readonly latestUserMessageAt: string | null;
158158
readonly latestUserAuthoredMessageAt?: string | null;
159+
/** Set only when the server reports it; `latestRun.completedAt` can fall back to `updatedAt`. */
160+
readonly latestRunCompletedAt?: string | null;
159161
readonly hasPendingApprovals: boolean;
160162
readonly hasPendingUserInput: boolean;
161163
readonly pendingUserInputAt?: string | null;
@@ -307,6 +309,9 @@ export function presentThreadShell(
307309
...(thread.latestUserAuthoredMessageAt === undefined
308310
? {}
309311
: { latestUserAuthoredMessageAt: nullableIso(thread.latestUserAuthoredMessageAt) }),
312+
...(thread.latestRunCompletedAt === undefined
313+
? {}
314+
: { latestRunCompletedAt: nullableIso(thread.latestRunCompletedAt) }),
310315
hasPendingApprovals:
311316
thread.pendingRuntimeRequest !== null &&
312317
thread.pendingRuntimeRequest.kind !== "user_input" &&

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,11 @@ describe("sortInboxThreadsBySend", () => {
250250
overrides: Partial<
251251
Pick<
252252
EnvironmentThreadShell,
253-
"latestRun" | "latestUserMessageAt" | "latestUserAuthoredMessageAt" | "unsettledAt"
253+
| "latestRun"
254+
| "latestRunCompletedAt"
255+
| "latestUserMessageAt"
256+
| "latestUserAuthoredMessageAt"
257+
| "unsettledAt"
254258
>
255259
> = {},
256260
) => ({
@@ -318,6 +322,7 @@ describe("sortInboxThreadsBySend", () => {
318322
completedAt: "2026-06-01T03:00:00.000Z",
319323
assistantMessageId: null,
320324
},
325+
latestRunCompletedAt: "2026-06-01T03:00:00.000Z",
321326
});
322327
const sentLater = inboxThread("sent-later", {
323328
latestUserAuthoredMessageAt: "2026-06-01T02:00:00.000Z",
@@ -333,4 +338,45 @@ describe("sortInboxThreadsBySend", () => {
333338
"returned-earlier",
334339
]);
335340
});
341+
342+
it("keeps a run waiting on you on top after a reload", () => {
343+
const awaiting = inboxThread("awaiting", {
344+
latestUserAuthoredMessageAt: "2026-06-01T01:00:00.000Z",
345+
latestRun: {
346+
runId: RunId.make("run:approval"),
347+
status: "waiting" as const,
348+
requestedAt: "2026-06-01T03:00:00.000Z",
349+
startedAt: "2026-06-01T03:00:00.000Z",
350+
completedAt: null,
351+
assistantMessageId: null,
352+
},
353+
});
354+
const sentLater = inboxThread("sent-later", {
355+
latestUserAuthoredMessageAt: "2026-06-01T02:00:00.000Z",
356+
});
357+
expect(
358+
sortInboxThreadsBySend([sentLater, awaiting], () => undefined).map((entry) => entry.id),
359+
).toEqual(["awaiting", "sent-later"]);
360+
});
361+
362+
it("ignores a completion time the server did not report", () => {
363+
// Older servers omit it, so `latestRun.completedAt` is the thread's `updatedAt`.
364+
const touched = inboxThread("touched", {
365+
latestUserAuthoredMessageAt: "2026-06-01T01:00:00.000Z",
366+
latestRun: {
367+
runId: RunId.make("run:legacy"),
368+
status: "completed" as const,
369+
requestedAt: "2026-06-01T01:00:00.000Z",
370+
startedAt: "2026-06-01T01:00:00.000Z",
371+
completedAt: "2026-06-01T03:00:00.000Z",
372+
assistantMessageId: null,
373+
},
374+
});
375+
const sentLater = inboxThread("sent-later", {
376+
latestUserAuthoredMessageAt: "2026-06-01T02:00:00.000Z",
377+
});
378+
expect(
379+
sortInboxThreadsBySend([sentLater, touched], () => undefined).map((entry) => entry.id),
380+
).toEqual(["sent-later", "touched"]);
381+
});
336382
});

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ export function sortInboxThreadsByReturn<T extends InboxThreadInput>(
5555
}
5656

5757
type InboxSendSortInput = InboxThreadInput &
58-
Pick<EnvironmentThreadShell, "latestUserMessageAt" | "latestUserAuthoredMessageAt">;
58+
Pick<
59+
EnvironmentThreadShell,
60+
"latestUserMessageAt" | "latestUserAuthoredMessageAt" | "latestRunCompletedAt"
61+
>;
5962

6063
/**
6164
* Newest first by the last message the user sent. Agent activity never moves
@@ -77,10 +80,13 @@ export function sortInboxThreadsBySend<T extends InboxSendSortInput>(
7780
? thread.latestUserMessageAt
7881
: thread.latestUserAuthoredMessageAt) ?? undefined,
7982
) ?? 0,
80-
// With the Working section, a finished run is a return; the stored time survives reloads.
81-
observedReturnAt === undefined
82-
? 0
83-
: (toSortableTimestamp(thread.latestRun?.completedAt ?? undefined) ?? 0),
83+
// With the Working section, runs mark returns; the stored times survive reloads.
84+
...(observedReturnAt === undefined
85+
? []
86+
: [
87+
toSortableTimestamp(thread.latestRun?.requestedAt ?? undefined) ?? 0,
88+
toSortableTimestamp(thread.latestRunCompletedAt ?? undefined) ?? 0,
89+
]),
8490
observedReturnAt?.(thread) ?? 0,
8591
),
8692
]),

0 commit comments

Comments
 (0)