Skip to content

Commit 54ff24f

Browse files
t3dotggclaude
andcommitted
fix(web): judge the meter reserve from the thread shell
The loading thread built from the shell has no messages or session, and a thread whose latest turn was cleared has no latestTurn either, so the composer reserved nothing for exactly the threads that were loading. The shell records when the last user message landed, which every started thread has. Read that instead. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 59dd53d commit 54ff24f

4 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
resolveProactiveTurnDiffAction,
5858
resolveThreadMetadataUpdateForNextTurn,
5959
resolveSendEnvMode,
60+
threadShellHasStarted,
6061
resolveDraftHeroState,
6162
scheduleEnvironmentReconnectWarning,
6263
startNewThreadForProject,
@@ -1981,3 +1982,36 @@ describe("checkout Git memory", () => {
19811982
).toBeUndefined();
19821983
});
19831984
});
1985+
1986+
describe("threadShellHasStarted", () => {
1987+
it("counts a thread that has a user message but no latest turn", () => {
1988+
expect(
1989+
threadShellHasStarted({ latestTurn: null, latestUserMessageAt: now, session: null }),
1990+
).toBe(true);
1991+
});
1992+
1993+
it("counts a thread with a live session and nothing else", () => {
1994+
expect(
1995+
threadShellHasStarted({
1996+
latestTurn: null,
1997+
latestUserMessageAt: null,
1998+
session: {
1999+
threadId,
2000+
status: "starting",
2001+
providerName: "codex",
2002+
runtimeMode: "full-access",
2003+
activeTurnId: null,
2004+
lastError: null,
2005+
updatedAt: now,
2006+
},
2007+
}),
2008+
).toBe(true);
2009+
});
2010+
2011+
it("does not count a thread that never sent anything", () => {
2012+
expect(
2013+
threadShellHasStarted({ latestTurn: null, latestUserMessageAt: null, session: null }),
2014+
).toBe(false);
2015+
expect(threadShellHasStarted(null)).toBe(false);
2016+
});
2017+
});

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,22 @@ export function threadHasStarted(thread: Thread | null | undefined): boolean {
827827
);
828828
}
829829

830+
/**
831+
* Whether a thread ran at least one turn, judged from its shell alone.
832+
*
833+
* `threadHasStarted` needs the detail: a thread whose latest turn was cleared
834+
* still has messages, and the loading shell carries none. The shell records
835+
* when the last user message landed, which every started thread has.
836+
*/
837+
export function threadShellHasStarted(
838+
shell: Pick<ThreadShell, "latestTurn" | "latestUserMessageAt" | "session"> | null | undefined,
839+
): boolean {
840+
return Boolean(
841+
shell &&
842+
(shell.latestTurn !== null || shell.latestUserMessageAt !== null || shell.session !== null),
843+
);
844+
}
845+
830846
// Imported history has no session until its first prompt. Resolve its instance
831847
// through the environment's provider catalog before locking to a driver.
832848
export function deriveLockedProvider(input: {

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8352,6 +8352,7 @@ export default function ChatView(props: ChatViewProps) {
83528352
activeThreadId={activeThreadId}
83538353
activeThreadEnvironmentId={activeThread?.environmentId}
83548354
activeThread={activeThread}
8355+
activeThreadShell={routeServerThreadShell}
83558356
promptHistoryMessages={timelineMessages}
83568357
isServerThread={isServerThread}
83578358
isLocalDraftThread={isLocalDraftThread}

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ import {
6363
readFileAsDataUrl,
6464
resolveComposerInteractionMode,
6565
resolveComposerProviderSelection,
66-
threadHasStarted,
66+
threadShellHasStarted,
6767
} from "../ChatView.logic";
6868
import {
6969
dataTransferHasComposerMention,
@@ -868,7 +868,13 @@ import {
868868
} from "../../providerInstances";
869869
import { type AppModelOption, getAppModelOptionsForInstance } from "../../modelSelection";
870870
import type { UnifiedSettings } from "@t3tools/contracts/settings";
871-
import { type ChatMessage, type SessionPhase, type Thread, videoMimeType } from "../../types";
871+
import {
872+
type ChatMessage,
873+
type SessionPhase,
874+
type Thread,
875+
type ThreadShell,
876+
videoMimeType,
877+
} from "../../types";
872878
import {
873879
buildComposerPromptHistoryEntries,
874880
stepComposerPromptHistory,
@@ -1247,6 +1253,8 @@ export interface ChatComposerProps {
12471253
activeThreadId: ThreadId | null;
12481254
activeThreadEnvironmentId: EnvironmentId | undefined;
12491255
activeThread: Thread | undefined;
1256+
/** The routed server thread's shell, present before its detail loads. */
1257+
activeThreadShell: ThreadShell | null;
12501258
/** Timeline messages including optimistic sends, for ArrowUp prompt recall. */
12511259
promptHistoryMessages: ReadonlyArray<ChatMessage>;
12521260
isServerThread: boolean;
@@ -1904,7 +1912,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
19041912
const reserveContextWindowMeter = shouldReserveContextWindowMeter({
19051913
meterEnabled: settings.contextWindowMeterEnabled,
19061914
detailLoading: props.threadSyncPhase === "loading",
1907-
threadStarted: threadHasStarted(activeThread),
1915+
threadStarted: threadShellHasStarted(props.activeThreadShell),
19081916
providerReportsContextWindow: selectedProviderStatus?.reportsContextWindow === true,
19091917
});
19101918

0 commit comments

Comments
 (0)