fix(clients): Working section keeps its order while agents finish and wake (#15418)

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Theo BrowneandClaude Opus 5.5 authored and GitHub committed 2026-10-04 00:28:10 -07:00
1 parent e19a48d220
commit 1302ccacbd
10 files changed
+127 -30

No files matched your search

@@ -15,6 +15,7 @@ import {
createInboxReturnTracker,
isThreadWorking,
sortInboxThreadsByReturn,
sortWorkingThreadsBySend,
} from "@t3tools/client-runtime/state/thread-inbox";
import {
sortActiveThreadsByOrderKey,
@@ -728,8 +729,8 @@ export function buildThreadListV2Items(input: {
const orderedActive = workingShelfEnabled
? sortInboxThreadsByReturn(active, input.inboxReturnAt)
: applyPendingThreadOrder(sortThreadsForListV2(active), "active", pending);
// Newest work first, by the same clock as the inbox.
const orderedWorking = sortInboxThreadsByReturn(working);
// Newest send first; finishing and waking again do not move a row.
const orderedWorking = sortWorkingThreadsBySend(working);
const orderedSnoozed = [...snoozed].sort(
(left, right) =>
parseTimestampMs(left.snoozedUntil ?? "") - parseTimestampMs(right.snoozedUntil ?? ""),
@@ -343,6 +343,14 @@ it.effect.each([
DateTime.formatIso(candidate.latestUserAuthoredMessageAt!),
DateTime.formatIso(written),
);
// The shell carries the same stamp, which orders the Working section.
const shellThread = (yield* store.getShellSnapshot()).threads.find(
(thread) => thread.id === threadId,
);
assert.equal(
DateTime.formatIso(shellThread!.latestUserAuthoredMessageAt!),
DateTime.formatIso(written),
);
}).pipe(Effect.provide(testLayer)),
);
@@ -161,10 +161,7 @@ export type ProjectionThreadPullRequests = Pick<
/**
* Thread activity needed by settlement, without transcript or fork history.
* `latestUserAuthoredMessageAt` is the last message the user wrote. Agent,
* provider, and server notifications also use the user role, so
* `latestUserMessageAt` moves when background work or a PR watch wakes the
* agent.
* Settlement always loads `latestUserAuthoredMessageAt`, so it is required here.
*/
export type ProjectionSettlementCandidate = Pick<
OrchestrationV2ThreadShell,
@@ -917,6 +914,7 @@ type ShellThreadRow = {
readonly blocking_failure_payload_json: string | null;
readonly pending_request_payload_json: string | null;
readonly latest_user_message_at: string | null;
readonly latest_user_authored_message_at: string | null;
readonly has_actionable_proposed_plan: number;
readonly item_count: number;
readonly runless_item_count: number;
@@ -938,7 +936,8 @@ type SettlementThreadRow = Pick<
| "latest_run_started_at"
| "latest_run_completed_at"
| "latest_user_message_at"
> & { readonly latest_user_authored_message_at: string | null };
| "latest_user_authored_message_at"
>;
type ShellRunItemCountRow = {
readonly thread_id: string;
@@ -1329,13 +1328,13 @@ export function threadShellFromProjection(
(left, right) =>
DateTime.toEpochMillis(right.createdAt) - DateTime.toEpochMillis(left.createdAt),
)[0] ?? null;
const latestUserMessage =
projection.messages
.filter((message) => message.role === "user")
.toSorted(
(left, right) =>
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
)[0] ?? null;
const userMessages = projection.messages
.filter((message) => message.role === "user")
.toSorted(
(left, right) =>
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt),
);
const latestUserMessage = userMessages[0] ?? null;
const pendingBackgroundTasks = derivePendingBackgroundWork({
latestRun,
providerThreads: projection.providerThreads,
@@ -1396,6 +1395,8 @@ export function threadShellFromProjection(
// initial hydration and streaming updates independent of transcript size.
latestVisibleMessage: null,
latestUserMessageAt: latestUserMessage?.updatedAt ?? null,
latestUserAuthoredMessageAt:
userMessages.find((message) => message.createdBy === "user")?.updatedAt ?? null,
hasActionableProposedPlan: projection.plans.some(
(plan) => plan.kind === "proposed_plan" && plan.status === "active",
),
@@ -1480,6 +1481,7 @@ type ShellThreadState = {
readonly usageLimitResetAt: OrchestrationV2ThreadShell["usageLimitResetAt"];
readonly pendingRuntimeRequest: OrchestrationV2ThreadProjection["runtimeRequests"][number] | null;
readonly latestUserMessageAt: DateTime.Utc | null;
readonly latestUserAuthoredMessageAt: DateTime.Utc | null;
readonly hasActionableProposedPlan: boolean;
readonly pendingBackgroundTasks: OrchestrationV2ThreadShell["pendingBackgroundTasks"];
readonly providerInstanceHistory: OrchestrationV2ThreadShell["providerInstanceHistory"];
@@ -1625,6 +1627,7 @@ function shellFromState(input: {
},
latestVisibleMessage: null,
latestUserMessageAt: input.state.latestUserMessageAt,
latestUserAuthoredMessageAt: input.state.latestUserAuthoredMessageAt,
hasActionableProposedPlan: input.state.hasActionableProposedPlan,
pendingBackgroundTasks: input.state.pendingBackgroundTasks,
providerInstanceHistory: input.state.providerInstanceHistory,
@@ -4887,6 +4890,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
ORDER BY message.updated_at DESC, message.message_id DESC
LIMIT 1
) AS latest_user_message_at,
(
SELECT message.updated_at
FROM orchestration_v2_projection_messages message
WHERE message.thread_id = t.thread_id
AND message.role = 'user'
AND json_extract(message.payload_json, '$.createdBy') = 'user'
ORDER BY message.updated_at DESC, message.message_id DESC
LIMIT 1
) AS latest_user_authored_message_at,
EXISTS (
SELECT 1
FROM orchestration_v2_projection_plans plan
@@ -5325,6 +5337,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
row.latest_user_message_at === null
? null
: DateTime.makeUnsafe(row.latest_user_message_at),
latestUserAuthoredMessageAt:
row.latest_user_authored_message_at === null
? null
: DateTime.makeUnsafe(row.latest_user_authored_message_at),
hasActionableProposedPlan: row.has_actionable_proposed_plan === 1,
pendingBackgroundTasks,
providerInstanceHistory: providerInstanceHistoryForShell({
@@ -5630,16 +5646,13 @@ export const layerMemory: Layer.Layer<ProjectionStoreV2> = Layer.effect(
!runs.some(isActivityRunForShell) &&
!runtimeRequests.some((request) => request.status === "pending"),
)
.map((projection) => ({
...threadShellFromProjection(projection),
latestUserAuthoredMessageAt:
projection.messages
.filter((message) => message.role === "user" && message.createdBy === "user")
.map((message) => message.updatedAt)
.toSorted(
(left, right) => DateTime.toEpochMillis(right) - DateTime.toEpochMillis(left),
)[0] ?? null,
}))
.map((projection) => {
const shell = threadShellFromProjection(projection);
return {
...shell,
latestUserAuthoredMessageAt: shell.latestUserAuthoredMessageAt ?? null,
};
})
.toSorted(
(left, right) =>
DateTime.toEpochMillis(left.updatedAt) - DateTime.toEpochMillis(right.updatedAt) ||
+1
View File
@@ -1055,6 +1055,7 @@ export { sortActiveThreadsByOrderKey as sortThreadsForSidebar } from "@t3tools/c
export {
isThreadWorking as isSidebarThreadWorking,
sortInboxThreadsByReturn,
sortWorkingThreadsBySend,
} from "@t3tools/client-runtime/state/thread-inbox";
// Pinned-reorder key math and the keyed sort live in client-runtime
+3 -2
View File
@@ -209,6 +209,7 @@ import {
sortPinnedThreadsForSidebar,
sortSidebarV2ProjectGroups,
sortThreadsForSidebar,
sortWorkingThreadsBySend,
useThreadJumpHintVisibility,
useRetainedValue,
useSidebarRowSubscriptionLease,
@@ -2794,8 +2795,8 @@ export default function Sidebar() {
preferredIds: optimisticDrop.order,
getId: (thread) => scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
}),
// Newest work first, by the same clock as the inbox.
workingThreads: sortInboxThreadsByReturn(working),
// Newest send first; finishing and waking again do not move a row.
workingThreads: sortWorkingThreadsBySend(working),
// Soonest wake first: "what comes back next" is the shelf's question.
snoozedThreads: snoozed.toSorted(
(left, right) =>
+3 -2
View File
@@ -117,8 +117,9 @@ open.
Turn on **Settings → General → Working section (beta)** on web and desktop, or **Settings →
Thread behavior → Working section** on iOS and Android, to move threads that are working or
monitoring into a collapsed **Working** section below the active list. A thread returns to the top
of the active list when it finishes, fails, or needs an approval or answer. Pinned threads stay in
the pinned section. Each device keeps its own choice.
of the active list when it finishes, fails, or needs an approval or answer. The Working section
lists the thread you last sent work to first. Pinned threads stay in the pinned section. Each
device keeps its own choice.
While this is on, the active list is ordered by when each thread last came back to you, so you
cannot drag or move threads within it. Your saved order returns when you turn it off.
@@ -102,6 +102,8 @@ export interface EnvironmentThreadShell {
readonly latestRun: ThreadRunSummary | null;
readonly runtime: ThreadRuntimeSummary | null;
readonly latestUserMessageAt: string | null;
/** The last message the user wrote. `undefined` means the server predates it. */
readonly latestUserAuthoredMessageAt?: string | null;
readonly hasPendingApprovals: boolean;
readonly hasPendingUserInput: boolean;
readonly hasActionableProposedPlan: boolean;
@@ -241,6 +243,9 @@ export function presentThreadShell(
latestRun,
runtime: shellRuntime(thread),
latestUserMessageAt: nullableIso(thread.latestUserMessageAt),
...(thread.latestUserAuthoredMessageAt === undefined
? {}
: { latestUserAuthoredMessageAt: nullableIso(thread.latestUserAuthoredMessageAt) }),
hasPendingApprovals:
thread.pendingRuntimeRequest !== null &&
thread.pendingRuntimeRequest.kind !== "user_input" &&
@@ -1,7 +1,7 @@
import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
import { EnvironmentId, ProviderInstanceId, RunId, ThreadId } from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";
import { createInboxReturnTracker } from "./threadInbox.ts";
import { createInboxReturnTracker, sortWorkingThreadsBySend } from "./threadInbox.ts";
const environmentId = EnvironmentId.make("environment-1");
@@ -57,3 +57,30 @@ describe("createInboxReturnTracker", () => {
expect(tracker.returnedAt(thread("b", false))).toBeDefined();
});
});
describe("sortWorkingThreadsBySend", () => {
it("orders by the last message the user sent, not by later runs", () => {
const sentFirst = {
...thread("sent-first", true),
latestUserAuthoredMessageAt: "2026-06-01T01:00:00.000Z",
// A wake run requested after the other thread's send.
latestRun: {
runId: RunId.make("run:wake"),
status: "running" as const,
requestedAt: "2026-06-01T04:00:00.000Z",
startedAt: "2026-06-01T04:00:00.000Z",
completedAt: null,
assistantMessageId: null,
},
};
const sentLast = {
...thread("sent-last", true),
latestUserAuthoredMessageAt: "2026-06-01T02:00:00.000Z",
};
// Launched by an agent: no user message, so creation time is the send.
const launched = { ...thread("launched", true), latestUserAuthoredMessageAt: null };
expect(
sortWorkingThreadsBySend([launched, sentFirst, sentLast]).map((thread) => thread.id),
).toEqual(["sent-last", "sent-first", "launched"]);
});
});
@@ -58,6 +58,40 @@ export function sortInboxThreadsByReturn<T extends InboxThreadInput>(
),
]),
);
return sortNewestFirst(threads, timestamps);
}
type WorkingSortInput = Pick<
EnvironmentThreadShell,
"id" | "environmentId" | "createdAt" | "latestRun" | "latestUserAuthoredMessageAt"
>;
/** The Working section lists threads newest first by the last message the
user sent. Runs ending and wakes (background results, delegated results,
PR watches) do not move a row, so the order stays put while agents finish
and resume. Servers without the authored stamp fall back to the latest
run's request time. */
export function sortWorkingThreadsBySend<T extends WorkingSortInput>(threads: readonly T[]): T[] {
const timestamps = new Map(
threads.map((thread) => [
thread,
Math.max(
toSortableTimestamp(thread.createdAt) ?? 0,
toSortableTimestamp(
(thread.latestUserAuthoredMessageAt === undefined
? thread.latestRun?.requestedAt
: thread.latestUserAuthoredMessageAt) ?? undefined,
) ?? 0,
),
]),
);
return sortNewestFirst(threads, timestamps);
}
function sortNewestFirst<T extends Pick<EnvironmentThreadShell, "id" | "environmentId">>(
threads: readonly T[],
timestamps: ReadonlyMap<T, number>,
): T[] {
return [...threads].sort(
(left, right) =>
timestamps.get(right)! - timestamps.get(left)! ||
@@ -1747,6 +1747,11 @@ export const OrchestrationV2ThreadShell = Schema.Struct({
pendingRuntimeRequest: Schema.NullOr(OrchestrationV2PendingRuntimeRequestSummary),
latestVisibleMessage: Schema.NullOr(OrchestrationV2LatestVisibleMessageSummary),
latestUserMessageAt: Schema.NullOr(Schema.DateTimeUtc),
/**
* The last message the user wrote. Wakes and agent messages also use the
* user role, so they move latestUserMessageAt but not this.
*/
latestUserAuthoredMessageAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
hasActionableProposedPlan: Schema.Boolean,
// Normalized post-settlement background work for sidebar Waiting pills.
// Empty when the latest root run is still active or no pending work remains.
@@ -2269,6 +2274,7 @@ export const OrchestrationV2ThreadShellJson = OrchestrationV2ThreadShell.mapFiel
pendingRuntimeRequest: Schema.NullOr(OrchestrationV2PendingRuntimeRequestSummaryJson),
latestVisibleMessage: Schema.NullOr(OrchestrationV2LatestVisibleMessageSummaryJson),
latestUserMessageAt: Schema.NullOr(Schema.DateTimeUtcFromString),
latestUserAuthoredMessageAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtcFromString)),
createdAt: Schema.DateTimeUtcFromString,
updatedAt: Schema.DateTimeUtcFromString,
archivedAt: Schema.NullOr(Schema.DateTimeUtcFromString),