diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts index c241522084415..6cf3ce762d434 100644 --- a/apps/web/src/components/ChatView.logic.test.ts +++ b/apps/web/src/components/ChatView.logic.test.ts @@ -61,6 +61,7 @@ import { deriveCommittedServerUserMessageIds, deriveComposerSendState, deriveLockedProvider, + hasCompactableConversation, hasServerAcknowledgedLocalDispatch, waitForRevertedMessage, } from "./ChatView.logic"; @@ -434,6 +435,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 14590bb0ff386..927a253ead2b8 100644 --- a/apps/web/src/components/ChatView.logic.ts +++ b/apps/web/src/components/ChatView.logic.ts @@ -1014,6 +1014,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 764ea2c6bf756..1110c730a722a 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -361,6 +361,7 @@ import { startNewThreadForProject, codexArtifactTemplatePromptToAppend, shouldRefocusComposerOnWindowFocus, + hasCompactableConversation, } from "./ChatView.logic"; import { useLocalStorage } from "~/hooks/useLocalStorage"; import { useComposerHandleContext } from "../composerHandleContext"; @@ -2485,11 +2486,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 = (composerModelSelectionApplies ? composerSelectedModelSelection : null) ?? activeThread?.modelSelection;