feat(claude): compact old threads before they burn through usage #8144
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 12 commits into
main fromrefs/pull/8144/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/web/src/components/chat/ChatComposer.tsx line 2020Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1982,3 +2009,12 @@19822009 shouldBlurMobileComposerOnSubmit,19832010 ],19842011 );2012+ const compactThreadContext = useCallback(() => {2013+ if (phase === "running" || isSendBusy || isConnecting || !activeThreadId) {2014+ return;2015+ }2016+2017+ if (resumeCompactionNoticeKey) {2018+ setDismissedResumeCompactionNoticeKey(resumeCompactionNoticeKey);2019+ }2020+ promptRef.current = "/compact";
- apps/web/src/components/chat/ChatComposer.tsx lines 2951 to 2976OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2901,0 +2956,21 @@2956+ <div className="min-w-0">2957+ <div className="text-sm font-medium text-foreground">Resume with less context</div>2958+ <div className="text-xs text-muted-foreground">2959+ {formatContextWindowTokens(activeContextWindow.usedTokens)} tokens from an older2960+ session2961+ </div>2962+ </div>2963+ <div className="flex shrink-0 items-center gap-1">2964+ <Button2965+ size="xs"2966+ variant="ghost-muted"2967+ onClick={() => setDismissedResumeCompactionNoticeKey(resumeCompactionNoticeKey)}2968+ >2969+ Keep2970+ </Button>2971+ <Button size="xs" onClick={compactThreadContext}>2972+ Compact2973+ </Button>2974+ </div>2975+ </div>2976+ ) : null}
- apps/web/src/components/chat/ChatComposer.tsx line 2045Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -1984,0 +2013,21 @@2013+ if (phase === "running" || isSendBusy || isConnecting || !activeThreadId) {2014+ return;2015+ }2016+2017+ if (resumeCompactionNoticeKey) {2018+ setDismissedResumeCompactionNoticeKey(resumeCompactionNoticeKey);2019+ }2020+ promptRef.current = "/compact";2021+ setComposerDraftPrompt(composerDraftTarget, "/compact");2022+ onSend();2023+ }, [2024+ activeThreadId,2025+ composerDraftTarget,2026+ isConnecting,2027+ isSendBusy,2028+ onSend,2029+ phase,2030+ promptRef,2031+ resumeCompactionNoticeKey,2032+ setComposerDraftPrompt,2033+ ]);apps/web/src/components/chat/ContextWindowMeter.logic.ts line 77Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -3,0 +7,21 @@7+export function shouldOfferResumeCompaction(input: {8+ readonly provider: string | null | undefined;9+ readonly usedTokens: number | null | undefined;10+ readonly updatedAt: string | null | undefined;11+ readonly now: string;12+}): boolean {13+ if (14+ input.provider !== "claudeAgent" ||15+ (input.usedTokens ?? 0) < CLAUDE_RESUME_COMPACTION_TOKENS16+ ) {17+ return false;18+ }19+20+ const updatedAt = Date.parse(input.updatedAt ?? "");21+ const now = Date.parse(input.now);22+ return (23+ Number.isFinite(updatedAt) &&24+ Number.isFinite(now) &&25+ now - updatedAt >= CLAUDE_RESUME_COMPACTION_MINUTES * 60_00026+ );27+}
- apps/web/src/components/chat/ContextWindowMeter.tsx lines 137 to 147OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -128,6 +131,17 @@128131 ) : null}129132 {usage.compactsAutomatically ? (130133 <div className="mt-1 text-pretty text-secondary-label text-[11px] font-medium">131- {formatContextWindowCompactionMessage(modelDisplayName)}134+ {formatContextWindowCompactionMessage(modelDisplayName, usage.autoCompactThreshold)}132135 </div>133136 ) : null}137+ {onCompact ? (138+ <Button139+ size="xs"140+ variant="outline"141+ className="mt-1 w-full justify-center"142+ disabled={compactDisabled}143+ onClick={onCompact}144+ >145+ <Minimize2Icon aria-hidden="true" />146+ Compact context147+ </Button>apps/web/src/components/ChatView.tsx lines 4820 to 4830OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -4727,0 +4810,21 @@4810+ }4811+4812+ const dismiss = () => setDismissedResumeCompactionKey(resumeCompactionKey);4813+ return {4814+ id: `resume-compaction:${resumeCompactionKey}`,4815+ variant: "info",4816+ icon: <Minimize2Icon />,4817+ title: "Resume with less context",4818+ description: `${formatContextWindowTokens(activeContextWindow.usedTokens)} tokens from an older session`,4819+ actions: (4820+ <Button4821+ size="xs"4822+ disabled={compactDisabled}4823+ onClick={() => {4824+ if (compactDisabled) return;4825+ composerRef.current?.compactContext();4826+ dismiss();4827+ }}4828+ >4829+ Compact4830+ </Button>
- apps/web/src/components/ChatView.tsx line 4827OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -4727,0 +4807,21 @@4807+ })4808+ ) {4809+ return null;4810+ }4811+4812+ const dismiss = () => setDismissedResumeCompactionKey(resumeCompactionKey);4813+ return {4814+ id: `resume-compaction:${resumeCompactionKey}`,4815+ variant: "info",4816+ icon: <Minimize2Icon />,4817+ title: "Resume with less context",4818+ description: `${formatContextWindowTokens(activeContextWindow.usedTokens)} tokens from an older session`,4819+ actions: (4820+ <Button4821+ size="xs"4822+ disabled={compactDisabled}4823+ onClick={() => {4824+ if (compactDisabled) return;4825+ composerRef.current?.compactContext();4826+ dismiss();4827+ }}
- apps/web/src/components/chat/ChatComposer.tsx line 1996Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1982,3 +1985,6 @@19821985 shouldBlurMobileComposerOnSubmit,19831986 ],19841987 );1988+ const compactThreadContext = useCallback(() => {1989+ if (1990+ compactDisabled ||apps/web/src/components/chat/ContextWindowMeter.logic.ts line 20OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1,3 +1,20 @@11 import type { ModelSelection, ProviderInstanceId } from "@t3tools/contracts";22 import { getTriggerDisplayModelName, type ModelEsque } from "./providerIconUtils";334+export const CLAUDE_RESUME_COMPACTION_MINUTES = 70;5+export const CLAUDE_RESUME_COMPACTION_TOKENS = 100_000;6+export const CLAUDE_RESUME_COMPACTION_NEVER_ANSWER = "Don't ask again";7+8+export function hasDismissedResumeCompaction(9+ activities: ReadonlyArray<{ readonly kind: string; readonly payload: unknown }>,10+): boolean {11+ return activities.some((activity) => {12+ if (activity.kind !== "user-input.resolved") return false;13+ const payload = activity.payload;14+ if (!payload || typeof payload !== "object") return false;15+ const answers = (payload as { readonly answers?: unknown }).answers;16+ if (!answers || typeof answers !== "object" || Array.isArray(answers)) return false;17+18+ return Object.entries(answers).some(19+ ([question, answer]) =>20+ question.endsWith("Compact it before continuing?") &&
- apps/web/src/components/ChatView.tsx line 4788Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -4725,3 +4767,11 @@47254767 isUnsnoozing,47264768 isUnsettling,47274769 ]);4770+ const [dismissedResumeCompactionKey, setDismissedResumeCompactionKey] = useState<string | null>(4771+ null,4772+ );4773+ const resumeCompactionKey =4774+ activeThread && activeContextWindow4775+ ? `${activeThread.id}:${activeContextWindow.updatedAt}`4776+ : null;4777+ const compactDisabled =
- apps/web/src/components/ChatView.tsx lines 4820 to 4823OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -4727,0 +4803,21 @@4803+ provider: selectedProvider,4804+ usedTokens: activeContextWindow.usedTokens,4805+ updatedAt: activeContextWindow.updatedAt,4806+ now: `${nowMinute}:00.000Z`,4807+ })4808+ ) {4809+ return null;4810+ }4811+4812+ const dismiss = () => setDismissedResumeCompactionKey(resumeCompactionKey);4813+ return {4814+ id: `resume-compaction:${resumeCompactionKey}`,4815+ variant: "info",4816+ icon: <Minimize2Icon />,4817+ title: "Resume with less context",4818+ description: `${formatContextWindowTokens(activeContextWindow.usedTokens)} tokens from an older session`,4819+ actions: (4820+ <Button4821+ size="xs"4822+ variant="outline"4823+ disabled={compactDisabled}
- apps/web/src/components/chat/ChatComposer.tsx line 2004OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -1982,3 +1985,14 @@19821985 shouldBlurMobileComposerOnSubmit,19831986 ],19841987 );1988+ const compactThreadContext = useCallback(() => {1989+ if (1990+ compactDisabled ||1991+ noProviderAvailable ||1992+ composerSendState.hasSendableContent ||1993+ activePendingApproval !== null ||1994+ pendingUserInputs.length > 0 ||1995+ phase === "running" ||1996+ isSendBusy ||1997+ isConnecting ||1998+ !activeThreadIdapps/web/src/components/chat/ChatComposer.tsx line 1994Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1982,3 +1985,4 @@19821985 shouldBlurMobileComposerOnSubmit,19831986 ],19841987 );1988+ const compactThreadContext = useCallback(() => {
- apps/web/src/components/ChatView.tsx line 4794Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -4725,3 +4767,17 @@47254767 isUnsnoozing,47264768 isUnsettling,47274769 ]);4770+ const [dismissedResumeCompactionKey, setDismissedResumeCompactionKey] = useState<string | null>(4771+ null,4772+ );4773+ const resumeCompactionKey =4774+ activeThread && activeContextWindow4775+ ? `${activeThread.id}:${activeContextWindow.updatedAt}`4776+ : null;4777+ const compactDisabled =4778+ !activeThread ||4779+ !isServerThread ||4780+ selectedProvider !== "claudeAgent" ||4781+ !activeProviderStatus?.enabled ||4782+ activeProviderStatus.availability === "unavailable" ||4783+ isWorking ||
- apps/web/src/components/chat/ContextWindowMeter.tsx lines 138 to 156Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -128,6 +132,19 @@128132 ) : null}129133 {usage.compactsAutomatically ? (130134 <div className="mt-1 text-pretty text-secondary-label text-[11px] font-medium">131- {formatContextWindowCompactionMessage(modelDisplayName)}135+ {formatContextWindowCompactionMessage(modelDisplayName, usage.autoCompactThreshold)}132136 </div>133137 ) : null}138+ {onCompact ? (139+ <Button140+ size="xs"141+ variant="outline"142+ className="mt-1 w-full justify-center"143+ disabled={compactDisabled}144+ aria-label={compactDisabledReason ?? "Compact context"}145+ onClick={onCompact}146+ >147+ <Minimize2Icon aria-hidden="true" />148+ Compact context149+ </Button>150+ ) : null}apps/web/src/components/ChatView.tsx lines 4851 to 4851OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -4727,0 +4831,21 @@4831+ usedTokens: activeContextWindow.usedTokens,4832+ updatedAt: activeContextWindow.updatedAt,4833+ now: `${nowMinute}:00.000Z`,4834+ })4835+ ) {4836+ return null;4837+ }4838+4839+ const dismiss = () => setDismissedResumeCompactionKey(resumeCompactionKey);4840+ return {4841+ id: `resume-compaction:${resumeCompactionKey}`,4842+ variant: "info",4843+ icon: <Minimize2Icon />,4844+ title: "Resume with less context",4845+ description: `${formatContextWindowTokens(activeContextWindow.usedTokens)} tokens from an older session`,4846+ actions: (4847+ <Button4848+ size="xs"4849+ variant="outline"4850+ disabled={compactDisabled}4851+ aria-label={compactDisabledReason ?? "Compact"}
- apps/web/src/components/ChatView.tsx line 2802OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2759,3 +2787,16 @@27592787 activeThread?.modelSelection.instanceId ??27602788 activeProject?.defaultModelSelection?.instanceId ??27612789 null;2790+ const compactionProviderAvailable = useMemo(() => {2791+ const claudeProviders = applyProviderInstanceSettings(2792+ deriveProviderInstanceEntries(providerStatuses),2793+ settings,2794+ ).filter((provider) => provider.driverKind === "claudeAgent");2795+2796+ return (2797+ resolveSelectableProviderInstanceEntry(2798+ claudeProviders,2799+ activeProviderInstanceId ?? undefined,2800+ ) !== undefined2801+ );2802+ }, [activeProviderInstanceId, providerStatuses, settings]);
- apps/web/src/components/ChatView.tsx line 4793Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -4725,3 +4765,16 @@47254765 isUnsnoozing,47264766 isUnsettling,47274767 ]);4768+ const [dismissedResumeCompactionKey, setDismissedResumeCompactionKey] = useState<string | null>(4769+ null,4770+ );4771+ const resumeCompactionKey =4772+ activeThread && activeContextWindow4773+ ? `${activeThread.id}:${activeContextWindow.updatedAt}`4774+ : null;4775+ const compactDisabled =4776+ !activeThread ||4777+ !activeProject ||4778+ !isServerThread ||4779+ selectedProvider !== "claudeAgent" ||4780+ !compactionProviderAvailable ||apps/web/src/components/ChatView.tsx line 4841Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -4727,0 +4807,21 @@4807+ pendingUserInputs.length > 0 ||4808+ phase === "running" ||4809+ !shouldOfferResumeCompaction({4810+ provider: selectedProvider,4811+ usedTokens: activeContextWindow.usedTokens,4812+ updatedAt: activeContextWindow.updatedAt,4813+ now: `${nowMinute}:00.000Z`,4814+ })4815+ ) {4816+ return null;4817+ }4818+4819+ const dismiss = () => setDismissedResumeCompactionKey(resumeCompactionKey);4820+ const compactAction = (4821+ <Button4822+ size="xs"4823+ variant="outline"4824+ disabled={compactDisabled}4825+ onClick={() => {4826+ if (compactDisabled) return;4827+ composerRef.current?.compactContext();
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labels📱 Native Change
- packages/shared/src/model.ts lines 369 to 369OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -362,4 +362,8 @@362362 if (!trimmed) {363363 return trimmed;364364 }365- if (effort !== "ultrathink") {365+ // Prefixing a slash command turns it into plain prose, so Claude never366+ // runs it. The pattern matches command-shaped first tokens ("/compact",367+ // "/security-review", "/plugin:skill args") without catching absolute368+ // paths like "/home/theo/app.ts", whose first token has more slashes.369+ if (effort !== "ultrathink" || /^\/[a-z0-9][\w:-]*(?:\s|$)/iu.test(trimmed)) {
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.






Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
af46b8d6-ea93-46eb-ad83-f81b058d3753You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share