diff --git a/apps/web/src/components/ChatView.move-thread.browser.tsx b/apps/web/src/components/ChatView.move-thread.browser.tsx index 6a1f93230..99d3e86f0 100644 --- a/apps/web/src/components/ChatView.move-thread.browser.tsx +++ b/apps/web/src/components/ChatView.move-thread.browser.tsx @@ -2104,6 +2104,10 @@ describe("ChatView (full app): moving a thread to another computer", () => { title: titles.get(thread.id)!, parentThreadId: THREAD_ID, attachedToParent: true, + // Review was started first, though this app hears of it second. + ...(thread.id === reviewId + ? { createdAt: new Date(Date.parse(thread.createdAt) - 60_000).toISOString() } + : {}), } : thread, ), @@ -2149,12 +2153,12 @@ describe("ChatView (full app): moving a thread to another computer", () => { const rows = Array.from( document.querySelectorAll('[data-testid^="move-threads-row-"]'), ).map((row) => row.dataset.testid); - // The two children are listed in the order this app received them, - // which two streams race to set, so only their place under it is fixed. - expect(rows[0]).toBe(`move-threads-row-${THREAD_ID}`); - expect(rows.slice(1).toSorted()).toEqual([ - `move-threads-row-${fixTestsId}`, + // Its children oldest first, as the sidebar lists them, whatever order + // this app received them in. + expect(rows).toEqual([ + `move-threads-row-${THREAD_ID}`, `move-threads-row-${reviewId}`, + `move-threads-row-${fixTestsId}`, ]); for (const id of [THREAD_ID, fixTestsId, reviewId]) { await expect.element(dialog.getByTestId(`move-threads-tick-${id}`)).toBeChecked(); diff --git a/apps/web/src/components/Sidebar.logic.ts b/apps/web/src/components/Sidebar.logic.ts index 8f639ce42..f21fe401c 100644 --- a/apps/web/src/components/Sidebar.logic.ts +++ b/apps/web/src/components/Sidebar.logic.ts @@ -16,6 +16,7 @@ import { getThreadInFlightStatus, getThreadSortTimestamp, sortThreads, + compareFamilyChildren, toSortableTimestamp, type ThreadSortInput, } from "../lib/threadSort"; @@ -1832,12 +1833,7 @@ export function countThreadAttention( export function sortFamilyChildren< E extends { readonly thread: { readonly createdAt: string; readonly id: string } }, >(children: readonly E[]): E[] { - return children.toSorted( - (left, right) => - (toSortableTimestamp(left.thread.createdAt) ?? 0) - - (toSortableTimestamp(right.thread.createdAt) ?? 0) || - left.thread.id.localeCompare(right.thread.id), - ); + return children.toSorted((left, right) => compareFamilyChildren(left.thread, right.thread)); } /** diff --git a/apps/web/src/lib/threadSort.ts b/apps/web/src/lib/threadSort.ts index 02c85e918..e22fd4332 100644 --- a/apps/web/src/lib/threadSort.ts +++ b/apps/web/src/lib/threadSort.ts @@ -17,6 +17,21 @@ export function toSortableTimestamp(iso: string | undefined): number | null { return Number.isFinite(ms) ? ms : null; } +/** + * A parent's children in family order: oldest first, then by id, never + * re-sorted. The sidebar's families and the Move threads dialog both list + * them this way. + */ +export function compareFamilyChildren( + left: { readonly createdAt?: string | undefined; readonly id: string }, + right: { readonly createdAt?: string | undefined; readonly id: string }, +): number { + return ( + (toSortableTimestamp(left.createdAt) ?? 0) - (toSortableTimestamp(right.createdAt) ?? 0) || + left.id.localeCompare(right.id) + ); +} + function getLatestUserMessageTimestamp(thread: ThreadSortInput): number { if (thread.latestUserMessageAt) { return toSortableTimestamp(thread.latestUserMessageAt) ?? Number.NEGATIVE_INFINITY; diff --git a/apps/web/src/threadMove.test.ts b/apps/web/src/threadMove.test.ts index 4340900bf..4033b97e9 100644 --- a/apps/web/src/threadMove.test.ts +++ b/apps/web/src/threadMove.test.ts @@ -541,6 +541,21 @@ describe("a family in the Move threads and quit flows (Part 3f, (g))", () => { ]); }); + it("lists a family's children oldest first, whatever order they came in", () => { + const older = { ...other, createdAt: "2026-11-17T09:00:00.000Z" }; + const newer = { ...child, createdAt: "2026-11-17T10:00:00.000Z" }; + for (const listed of [ + [parent, newer, older], + [parent, older, newer], + ]) { + expect(groupMoveFamilies(listed).map((row) => row.candidate.threadId)).toEqual([ + "p", + "c2", + "c1", + ]); + } + }); + it("ticks a whole family from any member, and unticks one child or the whole family", () => { const candidates = [parent, child, other, alone]; const canGo = (id: string) => id !== "c2"; diff --git a/apps/web/src/threadMove.ts b/apps/web/src/threadMove.ts index 068b9dc0e..a03055bf7 100644 --- a/apps/web/src/threadMove.ts +++ b/apps/web/src/threadMove.ts @@ -31,6 +31,7 @@ import { } from "@threadlines/contracts"; import { formatDownloadProgress } from "./lib/formatBytes"; +import { compareFamilyChildren } from "./lib/threadSort"; import type { ThreadMoveStatus } from "./types"; /** @@ -712,6 +713,8 @@ export interface MoveCandidate { readonly working: boolean; /** The thread that started it, while it is in that thread's family. */ readonly parentThreadId?: TThreadId; + /** When it was made: a family's children are listed oldest first. */ + readonly createdAt?: string; } /** @@ -731,6 +734,7 @@ export function listMoveCandidates< readonly moveStatus?: ThreadMoveStatus | undefined; readonly parentThreadId?: string | null | undefined; readonly attachedToParent?: boolean | undefined; + readonly createdAt?: string | undefined; }, >( entries: ReadonlyArray<{ readonly thread: TThread; readonly isDone: boolean }>, @@ -749,6 +753,7 @@ export function listMoveCandidates< threadId: thread.id, title: thread.title, working: isWorking(thread), + ...(thread.createdAt !== undefined ? { createdAt: thread.createdAt } : {}), ...(thread.attachedToParent === true && typeof thread.parentThreadId === "string" ? { parentThreadId: thread.parentThreadId as TThread["id"] } : {}), @@ -902,24 +907,34 @@ function familyRootOf(candidates: ReadonlyArray): Array<{ readonly candidate: T; readonly depth: 0 | 1 }> { const grouped: Array<{ readonly candidate: T; readonly depth: 0 | 1 }> = []; for (const candidate of candidates) { if (familyRootOf(candidates, candidate) !== candidate.threadId) continue; grouped.push({ candidate, depth: 0 }); - for (const child of candidates) { - if ( - child.threadId !== candidate.threadId && - familyRootOf(candidates, child) === candidate.threadId - ) { - grouped.push({ candidate: child, depth: 1 }); - } - } + const children = candidates + .filter( + (child) => + child.threadId !== candidate.threadId && + familyRootOf(candidates, child) === candidate.threadId, + ) + .toSorted((left, right) => + compareFamilyChildren( + { createdAt: left.createdAt, id: left.threadId }, + { createdAt: right.createdAt, id: right.threadId }, + ), + ); + grouped.push(...children.map((child) => ({ candidate: child, depth: 1 as const }))); } return grouped; }