diff --git a/apps/mobile/src/features/settings/ThreadSettingsSections.tsx b/apps/mobile/src/features/settings/ThreadSettingsSections.tsx index a23f0c98932ea..35258f6dff736 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 cabb9a34dbe9a..d82e5c6af8996 100644 --- a/apps/mobile/src/i18n/translations/de.json +++ b/apps/mobile/src/i18n/translations/de.json @@ -5301,5 +5301,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 fa572328fc871..f984654e9e4aa 100644 --- a/apps/mobile/src/i18n/translations/en.json +++ b/apps/mobile/src/i18n/translations/en.json @@ -5302,5 +5302,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 aaeb8230bc83e..72dce482a6a9d 100644 --- a/apps/mobile/src/i18n/translations/es.json +++ b/apps/mobile/src/i18n/translations/es.json @@ -5295,5 +5295,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 371ab3d7532dd..a81427de87e55 100644 --- a/apps/mobile/src/i18n/translations/fr.json +++ b/apps/mobile/src/i18n/translations/fr.json @@ -5295,5 +5295,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 32b5c4d8f9748..16810b026b87a 100644 --- a/apps/mobile/src/i18n/translations/ja.json +++ b/apps/mobile/src/i18n/translations/ja.json @@ -5277,5 +5277,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 0cd8713696dc5..8eabed00f4cf0 100644 --- a/apps/mobile/src/i18n/translations/ko.json +++ b/apps/mobile/src/i18n/translations/ko.json @@ -5281,5 +5281,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 caac483484c2e..a7907b561e865 100644 --- a/apps/mobile/src/i18n/translations/pt-BR.json +++ b/apps/mobile/src/i18n/translations/pt-BR.json @@ -5295,5 +5295,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 d5439f9698130..c0d5ed507e232 100644 --- a/apps/mobile/src/i18n/translations/ru.json +++ b/apps/mobile/src/i18n/translations/ru.json @@ -5375,5 +5375,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 7a634bcab134f..4da234a9ffb2a 100644 --- a/apps/mobile/src/i18n/translations/sv.json +++ b/apps/mobile/src/i18n/translations/sv.json @@ -5301,5 +5301,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 98b946c76f028..5c2adf97800f4 100644 --- a/apps/mobile/src/i18n/translations/zh.json +++ b/apps/mobile/src/i18n/translations/zh.json @@ -5286,5 +5286,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 4369220911d5b..78e2fbf87b0da 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, @@ -13,6 +14,7 @@ import { RunId, ThreadId, } 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"; @@ -21,12 +23,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; @@ -192,9 +199,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 }, @@ -206,10 +262,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, @@ -220,7 +277,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: { @@ -274,8 +333,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: [] }; }), @@ -288,10 +352,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 ? {} @@ -318,7 +407,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); }); }, ); @@ -397,6 +486,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 c4d586817551f..2ae609fcc0684 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, @@ -104,3 +111,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 e0a12417848fa..d89a7b9e55ab1 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")] : []), @@ -724,6 +727,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, @@ -2023,6 +2027,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 52dadd9c8fb29..247fcfea449ad 100644 --- a/apps/web/src/components/settings/settingsSearch.test.ts +++ b/apps/web/src/components/settings/settingsSearch.test.ts @@ -258,6 +258,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 b02f8d2f0b0c8..ca9c50fa3d787 100644 --- a/apps/web/src/components/settings/settingsSearch.ts +++ b/apps/web/src/components/settings/settingsSearch.ts @@ -475,6 +475,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 46df7424ab0f2..a43597fdedcaf 100644 --- a/apps/web/src/i18n/translations/de.json +++ b/apps/web/src/i18n/translations/de.json @@ -12140,5 +12140,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 fc971dba6e051..e788eb06f25bc 100644 --- a/apps/web/src/i18n/translations/en.json +++ b/apps/web/src/i18n/translations/en.json @@ -12149,5 +12149,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 797888016bd4a..ffe1629077054 100644 --- a/apps/web/src/i18n/translations/es.json +++ b/apps/web/src/i18n/translations/es.json @@ -12135,5 +12135,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 e06cde54989bd..5ab7deb386dc8 100644 --- a/apps/web/src/i18n/translations/fr.json +++ b/apps/web/src/i18n/translations/fr.json @@ -12140,5 +12140,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 4473bff3ddef1..f4197c2bd68d7 100644 --- a/apps/web/src/i18n/translations/ja.json +++ b/apps/web/src/i18n/translations/ja.json @@ -12088,5 +12088,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 68f0b20794362..ced88b59a36b5 100644 --- a/apps/web/src/i18n/translations/ko.json +++ b/apps/web/src/i18n/translations/ko.json @@ -12084,5 +12084,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 5a18a359ecb2a..8e1b9073316ea 100644 --- a/apps/web/src/i18n/translations/pt-BR.json +++ b/apps/web/src/i18n/translations/pt-BR.json @@ -12139,5 +12139,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 6c7fa00a15455..9c2b12d20e3a5 100644 --- a/apps/web/src/i18n/translations/ru.json +++ b/apps/web/src/i18n/translations/ru.json @@ -12139,5 +12139,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 d02de62557d73..0b42904444693 100644 --- a/apps/web/src/i18n/translations/sv.json +++ b/apps/web/src/i18n/translations/sv.json @@ -12147,5 +12147,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 acf5ce3045376..e97dc4f92c563 100644 --- a/apps/web/src/i18n/translations/zh.json +++ b/apps/web/src/i18n/translations/zh.json @@ -12165,5 +12165,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 4842885d9aa3f..30ebc1059fba0 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 678f58294c66a..5a59f99b47437 100644 --- a/packages/contracts/src/settings.ts +++ b/packages/contracts/src/settings.ts @@ -1460,6 +1460,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)), ), @@ -1912,6 +1914,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),