diff --git a/apps/mobile/src/features/settings/ThreadSettingsSections.tsx b/apps/mobile/src/features/settings/ThreadSettingsSections.tsx index 55c45eae31f01..9e9be33c80176 100644 --- a/apps/mobile/src/features/settings/ThreadSettingsSections.tsx +++ b/apps/mobile/src/features/settings/ThreadSettingsSections.tsx @@ -108,6 +108,7 @@ export function AutoSettleSettingsRows(props: { patch: Partial & { autoResumeLimitedThreads?: boolean; snoozeLimitedThreads?: boolean; + compactBeforeUsageLimit?: boolean; }, ) => props.write(patch); @@ -154,6 +155,13 @@ export function AutoSettleSettingsRows(props: { disabled={disabledFor("snoozeLimitedThreads")} onValueChange={(value) => writeToAll({ snoozeLimitedThreads: value })} /> + writeToAll({ compactBeforeUsageLimit: value })} + /> ) : null} diff --git a/apps/mobile/src/i18n/translations/de.json b/apps/mobile/src/i18n/translations/de.json index 039e1ddadd0bf..5cb4d90678f9a 100644 --- a/apps/mobile/src/i18n/translations/de.json +++ b/apps/mobile/src/i18n/translations/de.json @@ -5465,5 +5465,6 @@ "ad208e3e32c85029": "Alle ausblenden", "3328503592789bed": "Zen", "856812e7e1fc445e": "Tool-Aufrufe im Chat", - "8d545d54c0a9e950": "Die Befehle, Bearbeitungen und Suchen ausblenden, die Agenten ausführen. „Erfolgreiche ausblenden“ behält Fehler. „Zen“ zeigt nur endgültige Antworten. Genehmigungen, Fragen und Fehler werden immer angezeigt." + "8d545d54c0a9e950": "Die Befehle, Bearbeitungen und Suchen ausblenden, die Agenten ausführen. „Erfolgreiche ausblenden“ behält Fehler. „Zen“ zeigt nur endgültige Antworten. Genehmigungen, Fragen und Fehler werden immer angezeigt.", + "83c544176fa694bb": "Compact before usage limits" } diff --git a/apps/mobile/src/i18n/translations/en.json b/apps/mobile/src/i18n/translations/en.json index 91170171c1621..7f9489bd8f2f3 100644 --- a/apps/mobile/src/i18n/translations/en.json +++ b/apps/mobile/src/i18n/translations/en.json @@ -5467,5 +5467,6 @@ "ad208e3e32c85029": "Hide all", "3328503592789bed": "Zen", "856812e7e1fc445e": "Tool calls in chat", - "8d545d54c0a9e950": "Hide the commands, edits, and searches agents run. Hide successful keeps failures. Zen shows only final answers. Approvals, questions, and errors always show." + "8d545d54c0a9e950": "Hide the commands, edits, and searches agents run. Hide successful keeps failures. Zen shows only final answers. Approvals, questions, and errors always show.", + "83c544176fa694bb": "Compact before usage limits" } diff --git a/apps/mobile/src/i18n/translations/es.json b/apps/mobile/src/i18n/translations/es.json index 398c20ce17f1f..c62c6c94f07a8 100644 --- a/apps/mobile/src/i18n/translations/es.json +++ b/apps/mobile/src/i18n/translations/es.json @@ -5459,5 +5459,6 @@ "ad208e3e32c85029": "Ocultar todo", "3328503592789bed": "Zen", "856812e7e1fc445e": "Llamadas a herramientas en el chat", - "8d545d54c0a9e950": "Oculta los comandos, ediciones y búsquedas que ejecutan los agentes. Ocultar las correctas mantiene los fallos. Zen solo muestra las respuestas finales. Las aprobaciones, preguntas y errores siempre se muestran." + "8d545d54c0a9e950": "Oculta los comandos, ediciones y búsquedas que ejecutan los agentes. Ocultar las correctas mantiene los fallos. Zen solo muestra las respuestas finales. Las aprobaciones, preguntas y errores siempre se muestran.", + "83c544176fa694bb": "Compact before usage limits" } diff --git a/apps/mobile/src/i18n/translations/fr.json b/apps/mobile/src/i18n/translations/fr.json index 1734d91934a04..e4e848e800df2 100644 --- a/apps/mobile/src/i18n/translations/fr.json +++ b/apps/mobile/src/i18n/translations/fr.json @@ -5459,5 +5459,6 @@ "ad208e3e32c85029": "Tout masquer", "3328503592789bed": "Zen", "856812e7e1fc445e": "Appels d’outils dans la discussion", - "8d545d54c0a9e950": "Masque les commandes, modifications et recherches lancées par les agents. Masquer les réussis garde les échecs. Zen n’affiche que les réponses finales. Les approbations, questions et erreurs s’affichent toujours." + "8d545d54c0a9e950": "Masque les commandes, modifications et recherches lancées par les agents. Masquer les réussis garde les échecs. Zen n’affiche que les réponses finales. Les approbations, questions et erreurs s’affichent toujours.", + "83c544176fa694bb": "Compacter avant les limites d'utilisation" } diff --git a/apps/mobile/src/i18n/translations/ja.json b/apps/mobile/src/i18n/translations/ja.json index 113ada551f17d..f26adf102bb98 100644 --- a/apps/mobile/src/i18n/translations/ja.json +++ b/apps/mobile/src/i18n/translations/ja.json @@ -5440,5 +5440,6 @@ "ad208e3e32c85029": "すべて非表示", "3328503592789bed": "Zen", "856812e7e1fc445e": "チャット内のツール呼び出し", - "8d545d54c0a9e950": "エージェントが実行するコマンド、編集、検索を非表示にします。「成功したものを非表示」では失敗は表示されます。Zen では最終的な回答だけを表示します。承認、質問、エラーは常に表示されます。" + "8d545d54c0a9e950": "エージェントが実行するコマンド、編集、検索を非表示にします。「成功したものを非表示」では失敗は表示されます。Zen では最終的な回答だけを表示します。承認、質問、エラーは常に表示されます。", + "83c544176fa694bb": "Compact before usage limits" } diff --git a/apps/mobile/src/i18n/translations/ko.json b/apps/mobile/src/i18n/translations/ko.json index 7768bd410d434..8981559e960d2 100644 --- a/apps/mobile/src/i18n/translations/ko.json +++ b/apps/mobile/src/i18n/translations/ko.json @@ -5444,5 +5444,6 @@ "ad208e3e32c85029": "모두 숨기기", "3328503592789bed": "Zen", "856812e7e1fc445e": "채팅의 도구 호출", - "8d545d54c0a9e950": "에이전트가 실행하는 명령, 편집, 검색을 숨깁니다. 성공한 항목 숨기기는 실패를 남겨 둡니다. Zen은 최종 답변만 표시합니다. 승인, 질문, 오류는 항상 표시됩니다." + "8d545d54c0a9e950": "에이전트가 실행하는 명령, 편집, 검색을 숨깁니다. 성공한 항목 숨기기는 실패를 남겨 둡니다. Zen은 최종 답변만 표시합니다. 승인, 질문, 오류는 항상 표시됩니다.", + "83c544176fa694bb": "Compact before usage limits" } diff --git a/apps/mobile/src/i18n/translations/pt-BR.json b/apps/mobile/src/i18n/translations/pt-BR.json index 88dbb8da5cd87..d427aaebc4aad 100644 --- a/apps/mobile/src/i18n/translations/pt-BR.json +++ b/apps/mobile/src/i18n/translations/pt-BR.json @@ -5459,5 +5459,6 @@ "ad208e3e32c85029": "Ocultar todas", "3328503592789bed": "Zen", "856812e7e1fc445e": "Chamadas de ferramentas no chat", - "8d545d54c0a9e950": "Oculta os comandos, edições e pesquisas que os agentes executam. \"Ocultar bem-sucedidas\" mantém as falhas. Zen mostra só as respostas finais. Aprovações, perguntas e erros sempre aparecem." + "8d545d54c0a9e950": "Oculta os comandos, edições e pesquisas que os agentes executam. \"Ocultar bem-sucedidas\" mantém as falhas. Zen mostra só as respostas finais. Aprovações, perguntas e erros sempre aparecem.", + "83c544176fa694bb": "Compact before usage limits" } diff --git a/apps/mobile/src/i18n/translations/ru.json b/apps/mobile/src/i18n/translations/ru.json index 2d0fb55302031..abdc2f30e1f80 100644 --- a/apps/mobile/src/i18n/translations/ru.json +++ b/apps/mobile/src/i18n/translations/ru.json @@ -5539,5 +5539,6 @@ "ad208e3e32c85029": "Скрывать все", "3328503592789bed": "Дзен", "856812e7e1fc445e": "Вызовы инструментов в чате", - "8d545d54c0a9e950": "Скрывать команды, правки и поиски, которые выполняют агенты. «Скрывать успешные» оставляет ошибки. «Дзен» показывает только итоговые ответы. Запросы подтверждения, вопросы и ошибки показываются всегда." + "8d545d54c0a9e950": "Скрывать команды, правки и поиски, которые выполняют агенты. «Скрывать успешные» оставляет ошибки. «Дзен» показывает только итоговые ответы. Запросы подтверждения, вопросы и ошибки показываются всегда.", + "83c544176fa694bb": "Compact before usage limits" } diff --git a/apps/mobile/src/i18n/translations/sv.json b/apps/mobile/src/i18n/translations/sv.json index 70617afd183ca..1f80a2d812cb7 100644 --- a/apps/mobile/src/i18n/translations/sv.json +++ b/apps/mobile/src/i18n/translations/sv.json @@ -5465,5 +5465,6 @@ "ad208e3e32c85029": "Dölj alla", "3328503592789bed": "Zen", "856812e7e1fc445e": "Verktygsanrop i chatten", - "8d545d54c0a9e950": "Dölj kommandon, redigeringar och sökningar som agenter kör. Dölj lyckade behåller misslyckanden. Zen visar bara slutliga svar. Godkännanden, frågor och fel visas alltid." + "8d545d54c0a9e950": "Dölj kommandon, redigeringar och sökningar som agenter kör. Dölj lyckade behåller misslyckanden. Zen visar bara slutliga svar. Godkännanden, frågor och fel visas alltid.", + "83c544176fa694bb": "Compact before usage limits" } diff --git a/apps/mobile/src/i18n/translations/zh.json b/apps/mobile/src/i18n/translations/zh.json index 485d4206bf054..ea3d094c1c56e 100644 --- a/apps/mobile/src/i18n/translations/zh.json +++ b/apps/mobile/src/i18n/translations/zh.json @@ -5450,5 +5450,6 @@ "ad208e3e32c85029": "全部隐藏", "3328503592789bed": "专注模式", "856812e7e1fc445e": "聊天中的工具调用", - "8d545d54c0a9e950": "隐藏智能体执行的命令、编辑和搜索。“隐藏成功的调用”仍保留失败项;“专注模式”仅显示最终回答。审批、提问和错误始终显示。" + "8d545d54c0a9e950": "隐藏智能体执行的命令、编辑和搜索。“隐藏成功的调用”仍保留失败项;“专注模式”仅显示最终回答。审批、提问和错误始终显示。", + "83c544176fa694bb": "Compact before usage limits" } diff --git a/apps/server/src/orchestration-v2/IdleCompaction.test.ts b/apps/server/src/orchestration-v2/IdleCompaction.test.ts index 21d2158372e86..d95ded6b771eb 100644 --- a/apps/server/src/orchestration-v2/IdleCompaction.test.ts +++ b/apps/server/src/orchestration-v2/IdleCompaction.test.ts @@ -2,6 +2,7 @@ import { describe, expect, it } from "@effect/vitest"; import { DEFAULT_SERVER_SETTINGS, EventId, + IDLE_COMPACTION_TEXT, MessageId, type OrchestrationV2ThreadProjection, NodeId, @@ -14,6 +15,7 @@ import { ThreadId, TurnItemId, } from "@t3tools/contracts"; +import type { ProviderInstance } from "@t3tools/provider-core/server/driver"; import * as DateTime from "effect/DateTime"; import * as Deferred from "effect/Deferred"; import * as Effect from "effect/Effect"; @@ -22,12 +24,17 @@ import * as Stream from "effect/Stream"; import * as Tracer from "effect/Tracer"; import * as TestClock from "effect/testing/TestClock"; +import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts"; import * as ServerSettings from "../serverSettings.ts"; import * as IdleCompactionService from "./IdleCompactionService.ts"; import * as Orchestrator from "./Orchestrator.ts"; import * as ProjectionStore from "./ProjectionStore.ts"; -import { idleCompactionWindow, type IdleCompactionRecords } from "./IdleCompaction.ts"; +import { + idleCompactionAction, + idleCompactionWindow, + type IdleCompactionRecords, +} from "./IdleCompaction.ts"; const NOW_MS = Date.parse("2026-10-19T12:00:00.000Z"); const MINUTE_MS = 60_000; @@ -224,9 +231,58 @@ describe("idleCompactionWindow", () => { }); }); +describe("idleCompactionAction", () => { + it("compacts near a usage limit that outlasts the cache, unless set to never", () => { + const window = { startMs: NOW_MS + 58 * MINUTE_MS, endMs: NOW_MS + 60 * MINUTE_MS }; + const usage = ( + usedPercent: number, + resetsInMs?: number, + kind: "session" | "weekly" = "session", + ) => ({ + id: kind === "session" ? "five_hour" : "seven_day", + kind, + label: kind === "session" ? "Session" : "Weekly", + usedPercent, + ...(resetsInMs === undefined ? {} : { resetsAt: DateTime.formatIso(at(resetsInMs)) }), + }); + const action = ( + mode: "onReturn" | "beforeCacheExpires" | "off", + usageWindows: ReadonlyArray>, + nowMs = NOW_MS, + compactBeforeUsageLimit = true, + ) => idleCompactionAction({ mode, compactBeforeUsageLimit, window, usageWindows, nowMs }); + const nearLimit = [usage(95, 90 * MINUTE_MS)]; + + expect(action("onReturn", nearLimit)).toBe("compact"); + expect(action("beforeCacheExpires", nearLimit)).toBe("compact"); + expect(action("off", nearLimit)).toBe("skip"); + expect(action("onReturn", nearLimit, NOW_MS, false)).toBe("skip"); + expect(action("onReturn", [usage(94, 90 * MINUTE_MS)])).toBe("skip"); + expect(action("onReturn", [usage(95, 30 * MINUTE_MS)])).toBe("skip"); + expect(action("onReturn", [usage(95)])).toBe("skip"); + expect(action("onReturn", [usage(99, 3 * 24 * 60 * MINUTE_MS, "weekly")])).toBe("skip"); + // Inside the cache window, only the cache-expiry mode compacts without a usage reason. + expect(action("onReturn", [], window.startMs)).toBe("skip"); + expect(action("beforeCacheExpires", [])).toBe("wait"); + expect(action("beforeCacheExpires", [], window.startMs)).toBe("compact"); + }); +}); + describe("IdleCompactionService", () => { it.effect.each([ { mode: "onReturn", reads: 0 }, + { mode: "onReturn", usageLimit: true, reads: 0, dispatches: 0 }, + { mode: "onReturn", usageLimit: true, usedPercent: 94, reads: 0, dispatches: 0 }, + { mode: "onReturn", usageLimit: true, usedPercent: 95, reads: 1, dispatches: 1 }, + { mode: "off", usageLimit: true, usedPercent: 95, reads: 0, dispatches: 0 }, + { + mode: "onReturn", + override: "off", + usageLimit: true, + usedPercent: 95, + reads: 0, + dispatches: 0, + }, { mode: "off", reads: 0 }, { mode: "beforeCacheExpires", reads: 1 }, { mode: "beforeCacheExpires", override: "off", reads: 0 }, @@ -238,10 +294,11 @@ describe("IdleCompactionService", () => { { mode: "beforeCacheExpires", driver: "claudeAgent", instance: "custom-claude", reads: 1 }, { mode: "beforeCacheExpires", unconfigured: true, reads: 1 }, ] as const)( - "loads history only for eligible idle compaction: $mode/$override/$enabled/$projectDisabled/$instance/$unconfigured", + "loads history only for eligible idle compaction: $mode/$usageLimit/$override/$enabled/$projectDisabled/$instance/$unconfigured", (scenario) => { const test = { override: "default", + usageLimit: false, enabled: true, projectDisabled: false, ...scenario, @@ -252,7 +309,9 @@ describe("IdleCompactionService", () => { "instance" in test ? test.instance : "claudeAgent", ); const driver = ProviderDriverKind.make("driver" in test ? test.driver : "claudeAgent"); - const base = records({ cacheExpiresInMs: 2 * MINUTE_MS }); + const base = records({ + cacheExpiresInMs: (test.usageLimit ? 60 : 2) * MINUTE_MS, + }); const data: OrchestrationV2ThreadProjection = { ...base, thread: { @@ -306,8 +365,13 @@ describe("IdleCompactionService", () => { occurredAt: at(), payload: data.runs[0]!, }), - dispatch: () => + dispatch: (command) => Effect.sync(() => { + expect(command).toMatchObject({ + type: "message.dispatch", + text: IDLE_COMPACTION_TEXT, + threadId, + }); dispatches++; return { sequence: 1, storedEvents: [] }; }), @@ -320,10 +384,35 @@ describe("IdleCompactionService", () => { return data; }), }), + Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({ + getInstance: () => + Effect.succeed( + "usedPercent" in test + ? ({ + snapshot: { + getSnapshot: Effect.succeed({ + usageLimits: { + windows: [ + { + id: "five_hour", + kind: "session", + label: "Session", + usedPercent: test.usedPercent, + resetsAt: DateTime.formatIso(at(90 * MINUTE_MS)), + }, + ], + }, + }), + }, + } as unknown as ProviderInstance) + : undefined, + ), + }), Layer.mock(ServerSettings.ServerSettingsService)({ getSettings: Effect.succeed({ ...DEFAULT_SERVER_SETTINGS, idleCompaction: test.mode, + compactBeforeUsageLimit: test.usageLimit, providerInstances: "unconfigured" in test ? {} @@ -350,7 +439,7 @@ describe("IdleCompactionService", () => { yield* Deferred.await(done); }).pipe(Effect.provide(dependencies), Effect.withTracer(tracer)); expect(historyReads).toBe(test.reads); - expect(dispatches).toBe(test.reads); + expect(dispatches).toBe("dispatches" in test ? test.dispatches : test.reads); }); }, ); @@ -429,6 +518,9 @@ describe("IdleCompactionService", () => { ), }), }), + Layer.mock(ProviderInstanceRegistry.ProviderInstanceRegistry)({ + getInstance: () => Effect.undefined, + }), Layer.mock(ServerSettings.ServerSettingsService)({ getSettings: Effect.succeed({ ...DEFAULT_SERVER_SETTINGS, diff --git a/apps/server/src/orchestration-v2/IdleCompaction.ts b/apps/server/src/orchestration-v2/IdleCompaction.ts index 9aff19a244ead..f4afc95ae5174 100644 --- a/apps/server/src/orchestration-v2/IdleCompaction.ts +++ b/apps/server/src/orchestration-v2/IdleCompaction.ts @@ -1,4 +1,9 @@ -import { isListedThreadLineage, type OrchestrationV2ThreadProjection } from "@t3tools/contracts"; +import { + isListedThreadLineage, + type IdleCompactionMode, + type OrchestrationV2ThreadProjection, + type ServerProviderUsageWindow, +} from "@t3tools/contracts"; import { COMPACTION_SUGGESTION_MIN_TOKENS } from "@t3tools/shared/claudeCompaction"; import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork"; import * as DateTime from "effect/DateTime"; @@ -10,6 +15,8 @@ const MIN_CONTEXT_TOKENS = COMPACTION_SUGGESTION_MIN_TOKENS; // Start this long before the cache expires: enough for the session to resume // and send the compaction while the cache is still warm. const LEAD_MS = 2 * 60_000; +// Close enough to a usage limit that the next turn may well be the one it stops. +const NEAR_USAGE_LIMIT_PERCENT = 95; export type IdleCompactionRecords = Pick< OrchestrationV2ThreadProjection, @@ -107,3 +114,38 @@ export function idleCompactionWindow( if (!Number.isFinite(endMs) || nowMs >= endMs) return null; return { runId: latestRun.id, startMs: endMs - LEAD_MS, endMs }; } + +/** + * What to do with an idle compaction window. A nearly spent session window that + * resets after the cache expires compacts at once: a stop on that limit would + * resume against an expired cache. "Never" always wins. + */ +export function idleCompactionAction(input: { + readonly mode: IdleCompactionMode; + readonly compactBeforeUsageLimit: boolean; + readonly window: { readonly startMs: number; readonly endMs: number }; + readonly usageWindows: ReadonlyArray; + readonly nowMs: number; +}): "compact" | "wait" | "skip" { + const { mode, window, nowMs } = input; + if (mode === "off") return "skip"; + if (input.compactBeforeUsageLimit && usageLimitOutlastsCache(input.usageWindows, window.endMs)) + return "compact"; + if (mode !== "beforeCacheExpires") return "skip"; + return nowMs < window.startMs ? "wait" : "compact"; +} + +export function usageLimitOutlastsCache( + usageWindows: ReadonlyArray, + cacheExpiresAt: number, +): boolean { + return usageWindows.some( + (usage) => + // A weekly window can sit near its limit for days; compacting after every turn + // meanwhile would throw away context the user is still working with. + usage.kind === "session" && + usage.usedPercent >= NEAR_USAGE_LIMIT_PERCENT && + usage.resetsAt !== undefined && + Date.parse(usage.resetsAt) > cacheExpiresAt, + ); +} diff --git a/apps/server/src/orchestration-v2/IdleCompactionService.ts b/apps/server/src/orchestration-v2/IdleCompactionService.ts index c7035de4ec584..a1390faf59272 100644 --- a/apps/server/src/orchestration-v2/IdleCompactionService.ts +++ b/apps/server/src/orchestration-v2/IdleCompactionService.ts @@ -13,22 +13,27 @@ import * as Effect from "effect/Effect"; import * as FiberMap from "effect/FiberMap"; import * as Stream from "effect/Stream"; +import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts"; import { forkParked } from "../serverActivation.ts"; import * as ServerSettings from "../serverSettings.ts"; -import { idleCompactionWindow } from "./IdleCompaction.ts"; +import { + idleCompactionAction, + idleCompactionWindow, + usageLimitOutlastsCache, +} from "./IdleCompaction.ts"; import * as Orchestrator from "./Orchestrator.ts"; import * as ProjectionStore from "./ProjectionStore.ts"; /** - * Beta: when a run completes on a Claude instance set to compact before the - * cache expires, waits until shortly before its prompt cache expires and sends - * `/compact` as a visible T3 Code turn. Timers live in - * memory, so a server restart skips compactions that were still waiting. + * Beta: sends `/compact` shortly before a completed Claude run's prompt cache + * expires, or at once with `compactBeforeUsageLimit` when a nearly spent usage + * limit outlasts the cache. Timers live in memory; a restart skips waiting ones. */ export const make = Effect.gen(function* () { const orchestrator = yield* Orchestrator.OrchestratorV2; const projections = yield* ProjectionStore.ProjectionStoreV2; const settings = yield* ServerSettings.ServerSettingsService; + const providerInstances = yield* ProviderInstanceRegistry.ProviderInstanceRegistry; const compactWhenDue = Effect.fn("IdleCompactionService.compactWhenDue")( function* (threadId: ThreadId, completedRunId: RunId) { @@ -40,9 +45,11 @@ export const make = Effect.gen(function* () { const claudeInstance = Object.hasOwn(current.providerInstances, thread.providerInstanceId) ? current.providerInstances[thread.providerInstanceId]?.driver === "claudeAgent" : thread.providerInstanceId === "claudeAgent"; + const mode = resolveIdleCompaction(current, thread.providerInstanceId); if ( !claudeInstance || - resolveIdleCompaction(current, thread.providerInstanceId) !== "beforeCacheExpires" || + mode === "off" || + (mode !== "beforeCacheExpires" && !current.compactBeforeUsageLimit) || !resolveProjectProviderInstanceEnabled( current, thread.projectId, @@ -50,6 +57,17 @@ export const make = Effect.gen(function* () { ) ) return; + // The instance snapshot, not the registry list: the registry applies the + // run's last usage update asynchronously and can still be behind here. + const instance = current.compactBeforeUsageLimit + ? yield* providerInstances.getInstance(thread.providerInstanceId) + : undefined; + const usageWindows = + instance === undefined + ? [] + : ((yield* instance.snapshot.getSnapshot).usageLimits?.windows ?? []); + const nowMs = DateTime.toEpochMillis(yield* DateTime.now); + if (mode === "onReturn" && !usageLimitOutlastsCache(usageWindows, nowMs)) return; const activeProviderThreadIds = thread.activeProviderThreadId === null ? [] : [thread.activeProviderThreadId]; const records = yield* projections.getThreadRecords( @@ -79,8 +97,7 @@ export const make = Effect.gen(function* () { }, ); if ( - resolveIdleCompaction(current, records.thread.providerInstanceId) !== - "beforeCacheExpires" || + records.thread.providerInstanceId !== thread.providerInstanceId || !resolveProjectProviderInstanceEnabled( current, records.thread.projectId, @@ -99,10 +116,17 @@ export const make = Effect.gen(function* () { runId = latestRun.id; continue; } - const nowMs = DateTime.toEpochMillis(yield* DateTime.now); const window = idleCompactionWindow(records, nowMs); if (window?.runId !== runId) return; - if (nowMs < window.startMs) { + const action = idleCompactionAction({ + mode, + compactBeforeUsageLimit: current.compactBeforeUsageLimit, + window, + usageWindows, + nowMs, + }); + if (action === "skip") return; + if (action === "wait") { yield* Effect.sleep(Duration.millis(window.startMs - nowMs)); continue; } diff --git a/apps/web/src/components/settings/SettingsPanels.tsx b/apps/web/src/components/settings/SettingsPanels.tsx index 493d8a1e79565..fdb9a68406844 100644 --- a/apps/web/src/components/settings/SettingsPanels.tsx +++ b/apps/web/src/components/settings/SettingsPanels.tsx @@ -548,6 +548,9 @@ export function useSettingsRestore(onRestored?: () => void) { ...(settings.snoozeLimitedThreads !== DEFAULT_UNIFIED_SETTINGS.snoozeLimitedThreads ? [gt("Snooze until limits reset")] : []), + ...(settings.compactBeforeUsageLimit !== DEFAULT_UNIFIED_SETTINGS.compactBeforeUsageLimit + ? ["Compact before usage limits"] + : []), ...(!Equal.equals(settings.snoozePresets, DEFAULT_UNIFIED_SETTINGS.snoozePresets) ? [gt("Snooze options")] : []), @@ -728,6 +731,7 @@ export function useSettingsRestore(onRestored?: () => void) { sidebarAutoSettleOnMerge: DEFAULT_UNIFIED_SETTINGS.sidebarAutoSettleOnMerge, autoResumeLimitedThreads: DEFAULT_UNIFIED_SETTINGS.autoResumeLimitedThreads, snoozeLimitedThreads: DEFAULT_UNIFIED_SETTINGS.snoozeLimitedThreads, + compactBeforeUsageLimit: DEFAULT_UNIFIED_SETTINGS.compactBeforeUsageLimit, snoozePresets: DEFAULT_UNIFIED_SETTINGS.snoozePresets, responseStreamingMode: DEFAULT_UNIFIED_SETTINGS.responseStreamingMode, enableProviderUpdateChecks: DEFAULT_UNIFIED_SETTINGS.enableProviderUpdateChecks, @@ -2084,6 +2088,24 @@ export function ThreadsSettingsPanel() { /> } /> + + updateSettings({ compactBeforeUsageLimit: Boolean(checked) }) + } + aria-label={gt("Compact before usage limits")} + /> + } + /> {hasServerTargets ? ( diff --git a/apps/web/src/components/settings/settingsSearch.test.ts b/apps/web/src/components/settings/settingsSearch.test.ts index 4bc2c2a6317e5..c1f7e3d60b319 100644 --- a/apps/web/src/components/settings/settingsSearch.test.ts +++ b/apps/web/src/components/settings/settingsSearch.test.ts @@ -277,6 +277,10 @@ describe("settings search targets", () => { expect(isSettingsSearchScopeAvailable(fallback.scope, "environment")).toBe(true); expect(isSettingsSearchScopeAvailable(fallback.scope, "all")).toBe(true); expect(isSettingsSearchScopeAvailable(fallback.scope, "project")).toBe(false); + const compaction = getSettingsSearchTargetScope("compact-before-usage-limit")!; + expect(isSettingsSearchScopeAvailable(compaction.scope, "environment")).toBe(true); + expect(isSettingsSearchScopeAvailable(compaction.scope, "all")).toBe(true); + expect(isSettingsSearchScopeAvailable(compaction.scope, "project")).toBe(false); for (const id of ["response-streaming", "speech-project-dictionary"]) { expect(getSettingsSearchTargetScope(id)?.scope).toBe("project-defaults"); } diff --git a/apps/web/src/components/settings/settingsSearch.ts b/apps/web/src/components/settings/settingsSearch.ts index 4252fccc9c7d0..3add70aba5c55 100644 --- a/apps/web/src/components/settings/settingsSearch.ts +++ b/apps/web/src/components/settings/settingsSearch.ts @@ -486,6 +486,15 @@ export const SETTINGS_SEARCH_ITEMS = [ to: "/settings/threads", searchTerms: [msg("usage quota rate limit reset wake recover continue")], }, + { + id: "compact-before-usage-limit", + title: msg("Compact before usage limits"), + to: "/settings/threads", + scope: "environment-defaults", + searchTerms: [ + msg("usage quota rate limit compaction compact context prompt cache expire cost"), + ], + }, { id: "auto-resume-limited-threads", title: msg("Resume after usage limits"), diff --git a/apps/web/src/i18n/translations/de.json b/apps/web/src/i18n/translations/de.json index 3a71e92063446..3f8a24b1e658a 100644 --- a/apps/web/src/i18n/translations/de.json +++ b/apps/web/src/i18n/translations/de.json @@ -12457,5 +12457,8 @@ "27d76249d09eb69f": "Review-Leitfaden bereit", "faa3bb26abb39c93": "Review-Leitfaden fehlgeschlagen", "b7df38b7d8792d78": "Leitfaden öffnen", - "9e91e9a77b664e4e": "Review-Leitfaden konnte nicht gestartet werden" + "9e91e9a77b664e4e": "Review-Leitfaden konnte nicht gestartet werden", + "a4748476e3daa2d7": "Compact Claude threads near their session usage limit when it resets after the prompt cache expires, so resuming does not re-read the full context.", + "83c544176fa694bb": "Compact before usage limits", + "97b0a878d58c56d7": "usage quota rate limit compaction compact context prompt cache expire cost" } diff --git a/apps/web/src/i18n/translations/en.json b/apps/web/src/i18n/translations/en.json index 65ed2a4db1b89..0c04994d57e6c 100644 --- a/apps/web/src/i18n/translations/en.json +++ b/apps/web/src/i18n/translations/en.json @@ -12466,5 +12466,8 @@ "27d76249d09eb69f": "Review guide ready", "faa3bb26abb39c93": "Review guide failed", "b7df38b7d8792d78": "Open guide", - "9e91e9a77b664e4e": "Could not start a review guide" + "9e91e9a77b664e4e": "Could not start a review guide", + "a4748476e3daa2d7": "Compact Claude threads near their session usage limit when it resets after the prompt cache expires, so resuming does not re-read the full context.", + "83c544176fa694bb": "Compact before usage limits", + "97b0a878d58c56d7": "usage quota rate limit compaction compact context prompt cache expire cost" } diff --git a/apps/web/src/i18n/translations/es.json b/apps/web/src/i18n/translations/es.json index dae05eda74dd9..82e7461373d0f 100644 --- a/apps/web/src/i18n/translations/es.json +++ b/apps/web/src/i18n/translations/es.json @@ -12452,5 +12452,8 @@ "27d76249d09eb69f": "Guía de revisión lista", "faa3bb26abb39c93": "Falló la guía de revisión", "b7df38b7d8792d78": "Abrir guía", - "9e91e9a77b664e4e": "No se pudo iniciar una guía de revisión" + "9e91e9a77b664e4e": "No se pudo iniciar una guía de revisión", + "a4748476e3daa2d7": "Compact Claude threads near their session usage limit when it resets after the prompt cache expires, so resuming does not re-read the full context.", + "83c544176fa694bb": "Compact before usage limits", + "97b0a878d58c56d7": "usage quota rate limit compaction compact context prompt cache expire cost" } diff --git a/apps/web/src/i18n/translations/fr.json b/apps/web/src/i18n/translations/fr.json index 6054810c0c23d..508c231981d85 100644 --- a/apps/web/src/i18n/translations/fr.json +++ b/apps/web/src/i18n/translations/fr.json @@ -12457,5 +12457,8 @@ "27d76249d09eb69f": "Guide de revue prêt", "faa3bb26abb39c93": "Échec du guide de revue", "b7df38b7d8792d78": "Ouvrir le guide", - "9e91e9a77b664e4e": "Impossible de démarrer un guide de revue" + "9e91e9a77b664e4e": "Impossible de démarrer un guide de revue", + "a4748476e3daa2d7": "Compacte les threads Claude proches de leur limite d'utilisation de session lorsqu'elle se réinitialise après l'expiration du cache de prompt, pour que la reprise ne relise pas tout le contexte.", + "83c544176fa694bb": "Compacter avant les limites d'utilisation", + "97b0a878d58c56d7": "utilisation quota limite débit compaction compacter contexte cache prompt expiration coût" } diff --git a/apps/web/src/i18n/translations/ja.json b/apps/web/src/i18n/translations/ja.json index 188d63866545c..f632de8a04887 100644 --- a/apps/web/src/i18n/translations/ja.json +++ b/apps/web/src/i18n/translations/ja.json @@ -12406,5 +12406,8 @@ "27d76249d09eb69f": "レビューガイドの準備ができました", "faa3bb26abb39c93": "レビューガイドの作成に失敗しました", "b7df38b7d8792d78": "ガイドを開く", - "9e91e9a77b664e4e": "レビューガイドを開始できませんでした" + "9e91e9a77b664e4e": "レビューガイドを開始できませんでした", + "a4748476e3daa2d7": "Compact Claude threads near their session usage limit when it resets after the prompt cache expires, so resuming does not re-read the full context.", + "83c544176fa694bb": "Compact before usage limits", + "97b0a878d58c56d7": "usage quota rate limit compaction compact context prompt cache expire cost" } diff --git a/apps/web/src/i18n/translations/ko.json b/apps/web/src/i18n/translations/ko.json index fd4f1a5af2fc8..15a0260168ab8 100644 --- a/apps/web/src/i18n/translations/ko.json +++ b/apps/web/src/i18n/translations/ko.json @@ -12401,5 +12401,8 @@ "27d76249d09eb69f": "검토 가이드 준비됨", "faa3bb26abb39c93": "검토 가이드 실패", "b7df38b7d8792d78": "가이드 열기", - "9e91e9a77b664e4e": "검토 가이드를 시작할 수 없습니다" + "9e91e9a77b664e4e": "검토 가이드를 시작할 수 없습니다", + "a4748476e3daa2d7": "Compact Claude threads near their session usage limit when it resets after the prompt cache expires, so resuming does not re-read the full context.", + "83c544176fa694bb": "Compact before usage limits", + "97b0a878d58c56d7": "usage quota rate limit compaction compact context prompt cache expire cost" } diff --git a/apps/web/src/i18n/translations/pt-BR.json b/apps/web/src/i18n/translations/pt-BR.json index f8450e8032e5f..2e3882065946f 100644 --- a/apps/web/src/i18n/translations/pt-BR.json +++ b/apps/web/src/i18n/translations/pt-BR.json @@ -12457,5 +12457,8 @@ "27d76249d09eb69f": "Guia de revisão pronto", "faa3bb26abb39c93": "O guia de revisão falhou", "b7df38b7d8792d78": "Abrir guia", - "9e91e9a77b664e4e": "Não foi possível iniciar um guia de revisão" + "9e91e9a77b664e4e": "Não foi possível iniciar um guia de revisão", + "a4748476e3daa2d7": "Compact Claude threads near their session usage limit when it resets after the prompt cache expires, so resuming does not re-read the full context.", + "83c544176fa694bb": "Compact before usage limits", + "97b0a878d58c56d7": "usage quota rate limit compaction compact context prompt cache expire cost" } diff --git a/apps/web/src/i18n/translations/ru.json b/apps/web/src/i18n/translations/ru.json index 75b7db757cf04..e78e7147ce43a 100644 --- a/apps/web/src/i18n/translations/ru.json +++ b/apps/web/src/i18n/translations/ru.json @@ -12457,5 +12457,8 @@ "27d76249d09eb69f": "Гид по ревью готов", "faa3bb26abb39c93": "Не удалось создать гид по ревью", "b7df38b7d8792d78": "Открыть гид", - "9e91e9a77b664e4e": "Не удалось запустить гид по ревью" + "9e91e9a77b664e4e": "Не удалось запустить гид по ревью", + "a4748476e3daa2d7": "Compact Claude threads near their session usage limit when it resets after the prompt cache expires, so resuming does not re-read the full context.", + "83c544176fa694bb": "Compact before usage limits", + "97b0a878d58c56d7": "usage quota rate limit compaction compact context prompt cache expire cost" } diff --git a/apps/web/src/i18n/translations/sv.json b/apps/web/src/i18n/translations/sv.json index 6a3525acc8636..b4a4feac5a02f 100644 --- a/apps/web/src/i18n/translations/sv.json +++ b/apps/web/src/i18n/translations/sv.json @@ -12464,5 +12464,8 @@ "27d76249d09eb69f": "Granskningsguiden är klar", "faa3bb26abb39c93": "Granskningsguiden misslyckades", "b7df38b7d8792d78": "Öppna guiden", - "9e91e9a77b664e4e": "Kunde inte starta en granskningsguide" + "9e91e9a77b664e4e": "Kunde inte starta en granskningsguide", + "a4748476e3daa2d7": "Compact Claude threads near their session usage limit when it resets after the prompt cache expires, so resuming does not re-read the full context.", + "83c544176fa694bb": "Compact before usage limits", + "97b0a878d58c56d7": "usage quota rate limit compaction compact context prompt cache expire cost" } diff --git a/apps/web/src/i18n/translations/zh.json b/apps/web/src/i18n/translations/zh.json index af1ca50a064b3..5ca8a67fef8ef 100644 --- a/apps/web/src/i18n/translations/zh.json +++ b/apps/web/src/i18n/translations/zh.json @@ -12483,5 +12483,8 @@ "27d76249d09eb69f": "审查导览已就绪", "faa3bb26abb39c93": "审查导览生成失败", "b7df38b7d8792d78": "打开导览", - "9e91e9a77b664e4e": "无法开始生成审查导览" + "9e91e9a77b664e4e": "无法开始生成审查导览", + "a4748476e3daa2d7": "Compact Claude threads near their session usage limit when it resets after the prompt cache expires, so resuming does not re-read the full context.", + "83c544176fa694bb": "Compact before usage limits", + "97b0a878d58c56d7": "usage quota rate limit compaction compact context prompt cache expire cost" } diff --git a/docs/user/providers-claude.md b/docs/user/providers-claude.md index 00b27e02ae23d..ee77821871a64 100644 --- a/docs/user/providers-claude.md +++ b/docs/user/providers-claude.md @@ -89,6 +89,13 @@ Claude Code holds the turn until that window reopens, so it can keep showing as working. Wait for the reset, or stop the turn and continue later. The warning's timestamp shows when the displayed wait started. +A reset more than an hour away outlasts the prompt cache, so the continuation +reprocesses the whole conversation. Enable **Compact before usage limits** in +**Settings → Threads** to compact a thread over 200,000 tokens as soon as a turn +ends with the session usage window at 95% or more and resetting after the cache +expires. Weekly limits do not trigger it. +Providers whose **Compact idle threads** is **Never** are left alone. + ## Steering A steer waits until Claude finishes the tool calls it is running, then Claude reads it diff --git a/packages/client-runtime/src/state/sharedSettings.ts b/packages/client-runtime/src/state/sharedSettings.ts index 77dbddadb6bbb..569f284636653 100644 --- a/packages/client-runtime/src/state/sharedSettings.ts +++ b/packages/client-runtime/src/state/sharedSettings.ts @@ -10,6 +10,7 @@ const SHARED_SERVER_SETTING_KEYS = [ "sidebarAutoSettleOnMerge", "autoResumeLimitedThreads", "snoozeLimitedThreads", + "compactBeforeUsageLimit", "snoozePresets", "newWorktreesStartFromOrigin", "sourceControlWritingStyle", diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.ts index b0726b07174c9..23cadec76e745 100644 --- a/packages/contracts/src/settings.ts +++ b/packages/contracts/src/settings.ts @@ -1472,6 +1472,8 @@ export const ServerSettings = Schema.Struct({ idleCompaction: Schema.optionalKey(IdleCompactionMode), snoozeLimitedThreads: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))), autoResumeLimitedThreads: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))), + /** Compacts idle Claude threads near a usage limit that resets after the prompt cache expires. */ + compactBeforeUsageLimit: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))), snoozePresets: SnoozePresetSettings.pipe( Schema.withDecodingDefault(Effect.succeed(DEFAULT_SNOOZE_PRESETS)), ), @@ -1925,6 +1927,7 @@ export const ServerSettingsPatch = Schema.Struct({ sidebarAutoSettleOnMerge: Schema.optionalKey(Schema.Boolean), autoResumeLimitedThreads: Schema.optionalKey(Schema.Boolean), snoozeLimitedThreads: Schema.optionalKey(Schema.Boolean), + compactBeforeUsageLimit: Schema.optionalKey(Schema.Boolean), snoozePresets: Schema.optionalKey(SnoozePresetSettings), hiddenThreadTags: Schema.optionalKey(Schema.Array(Schema.String)), hideTaggedThreads: Schema.optionalKey(Schema.Boolean),