fix(frontend): preserve chronological order of thread history after context compression (#3354)

* fix(frontend): preserve chronological order of thread history after context compression

Iterate runs from newest to match backend `list_by_thread` (newest-first) and the prepend semantics of the history loader, so refreshed history renders in A→B→C→D→E→F order.

Fixes #3352

* fix(frontend): auto-continue loading runs with no visible messages after context compression
This commit is contained in:
Huixin615
2026-06-03 21:51:48 +08:00
committed by GitHub
parent 8fca56cf43
commit 9a53f9dfbb
2 changed files with 189 additions and 3 deletions
+27 -2
View File
@@ -106,11 +106,11 @@ function dedupeMessagesByIdentity(messages: Message[]): Message[] {
});
}
function findLatestUnloadedRunIndex(
export function findLatestUnloadedRunIndex(
runs: Run[],
loadedRunIds: ReadonlySet<string>,
): number {
for (let i = runs.length - 1; i >= 0; i--) {
for (let i = 0; i < runs.length; i++) {
const run = runs[i];
if (run && !loadedRunIds.has(run.run_id)) {
return i;
@@ -119,6 +119,19 @@ function findLatestUnloadedRunIndex(
return -1;
}
export const MAX_CONSECUTIVE_EMPTY_RUN_LOADS = 5;
export function shouldAutoContinueOnEmptyRun(
fetchedMessageCount: number,
consecutiveEmptyLoads: number,
maxConsecutiveEmptyLoads: number = MAX_CONSECUTIVE_EMPTY_RUN_LOADS,
): boolean {
return (
fetchedMessageCount === 0 &&
consecutiveEmptyLoads < maxConsecutiveEmptyLoads
);
}
type RunMessagesPageResponse = {
data: RunMessage[];
has_more?: boolean;
@@ -874,6 +887,7 @@ export function useThreadHistory(threadId: string) {
setLoading(true);
try {
let consecutiveEmptyLoads = 0;
do {
pendingLoadRef.current = false;
@@ -927,6 +941,17 @@ export function useThreadHistory(threadId: string) {
} else {
runBeforeSeqRef.current.delete(run.run_id);
loadedRunIdsRef.current.add(run.run_id);
if (
shouldAutoContinueOnEmptyRun(
_messages.length,
consecutiveEmptyLoads,
)
) {
consecutiveEmptyLoads += 1;
pendingLoadRef.current = true;
} else {
consecutiveEmptyLoads = 0;
}
}
indexRef.current = findLatestUnloadedRunIndex(
runsRef.current,