From cd87911af725010ba2a43128c1409e387f4a58bd Mon Sep 17 00:00:00 2001 From: jason chien <3936511+jason5545@users.noreply.github.com> Date: Mon, 26 Oct 2026 18:13:13 +0800 Subject: [PATCH] fix(web): manual compaction counts history beyond the loaded page Web decided whether a thread can be compacted from the loaded page alone. The first page of an agent-started thread or a long turn often holds no user message, so web skipped the "Compact next" offer on return and disabled Compact, while the server checks the full history and accepts /compact. Count unloaded history when the thread has a user message, as mobile already does. --- .../web/src/components/ChatView.logic.test.ts | 81 +++++++++++++++++++ apps/web/src/components/ChatView.logic.ts | 19 +++++ apps/web/src/components/ChatView.tsx | 11 +-- 3 files changed, 106 insertions(+), 5 deletions(-) diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index 426818dba7a24..c5b83497ee156 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -51,6 +51,7 @@ import { deriveCommittedServerUserMessageIds, deriveComposerSendState, deriveLockedProvider, + hasCompactableConversation, hasServerAcknowledgedLocalDispatch, resolveDraftPromotionNavigationTarget, waitForRevertedMessage, @@ -400,6 +401,86 @@ describe("deriveCommittedServerUserMessageIds", () => { }); }); +describe("hasCompactableConversation", () => { + const at = DateTime.makeUnsafe("2026-10-26T08:00:00.000Z"); + const runId = RunId.make("run:thread:thread-1:ordinal:1"); + const itemBase = (id: string, ordinal: number) => ({ + id: TurnItemId.make(id), + threadId, + runId, + nodeId: null, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal, + status: "completed" as const, + title: null, + startedAt: at, + completedAt: at, + updatedAt: at, + }); + const userMessage = (text: string): OrchestrationV2ProjectedTurnItem => ({ + position: 0, + visibility: "local", + sourceThreadId: threadId, + sourceItemId: TurnItemId.make("turn-item:user"), + item: { + ...itemBase("turn-item:user", 1), + createdBy: "user", + creationSource: "web", + type: "user_message", + messageId: MessageId.make("message-user"), + inputIntent: "turn_start", + text, + attachments: [], + }, + }); + const assistantMessage: OrchestrationV2ProjectedTurnItem = { + position: 1, + visibility: "local", + sourceThreadId: threadId, + sourceItemId: TurnItemId.make("turn-item:assistant"), + item: { + ...itemBase("turn-item:assistant", 2), + type: "assistant_message", + messageId: MessageId.make("message-assistant"), + text: "done", + streaming: false, + }, + }; + + it("counts a user message beyond the loaded page", () => { + expect( + hasCompactableConversation({ + visibleTurnItems: [assistantMessage], + hasMoreHistory: true, + latestUserMessageAt: "2026-10-26T08:00:00.000Z", + }), + ).toBe(true); + }); + + it("stays unavailable when the thread never had a user message", () => { + expect( + hasCompactableConversation({ + visibleTurnItems: [assistantMessage], + hasMoreHistory: true, + latestUserMessageAt: null, + }), + ).toBe(false); + }); + + it("does not count a lone /compact on a fully loaded thread", () => { + expect( + hasCompactableConversation({ + visibleTurnItems: [userMessage("/compact"), assistantMessage], + hasMoreHistory: false, + latestUserMessageAt: "2026-10-26T08:00:00.000Z", + }), + ).toBe(false); + }); +}); + describe("resolveThreadSwitchTimeline", () => { afterEach(() => { resetHeldThreadTimeline(); diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts index e14d296cdd071..e637b2f7e9b94 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -1183,6 +1183,25 @@ export function deriveCommittedServerUserMessageIds( ); } +/** + * The server checks the full history, but the first page of an + * agent-started thread or a long turn can hold no user message. + */ +export function hasCompactableConversation(input: { + readonly visibleTurnItems: ReadonlyArray; + readonly hasMoreHistory: boolean; + readonly latestUserMessageAt: string | null; +}): boolean { + return ( + input.visibleTurnItems.some( + ({ item }) => + item.type === "user_message" && + (item.text.trim().toLowerCase() !== "/compact" || item.attachments.length > 0), + ) || + (input.hasMoreHistory && input.latestUserMessageAt !== null) + ); +} + export function hasServerAcknowledgedLocalDispatch(input: { localDispatch: LocalDispatchSnapshot | null; phase: SessionPhase; diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index dae9caa8729ae..29dd04698cf67 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -335,6 +335,7 @@ import { startNewThreadForProject, codexArtifactTemplatePromptToAppend, shouldRefocusComposerOnWindowFocus, + hasCompactableConversation, } from "./ChatView.logic"; import { useLocalStorage } from "~/hooks/useLocalStorage"; import { useComposerHandleContext } from "../composerHandleContext"; @@ -2432,11 +2433,11 @@ export default function ChatView(props: ChatViewProps) { scrollToEnd, persistThreadSettingsForNextTurn, }); - const activeThreadHasCompactableConversation = serverVisibleTurnItems.some( - ({ item }) => - item.type === "user_message" && - (item.text.trim().toLowerCase() !== "/compact" || item.attachments.length > 0), - ); + const activeThreadHasCompactableConversation = hasCompactableConversation({ + visibleTurnItems: serverVisibleTurnItems, + hasMoreHistory: serverThreadHistory.hasMoreHistory, + latestUserMessageAt: serverThread?.latestUserMessageAt ?? null, + }); const pendingHandoffSelection = composerSelectedModelSelection ?? activeThread?.modelSelection; const pendingHandoffSelectionUsable = providerInstanceEntries.some( (entry) =>