feat(v2): resume limited threads when usage resets #12686
Mergedjuliusmarmingemerged 10 commits into
port-provider-limits-to-v2 fromrefs/pull/12686/headmerged by juliusmarminge 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
juliusmarminge changed the title
- apps/web/src/components/settings/settingsSearch.ts line 276Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -272,3 +272,5 @@272272 to: "/settings/general",273273 searchTerms: ["combine matching repositories environments sidebar"],274274 },275+ {276+ id: "auto-resume-limited-threads",
- apps/mobile/src/features/threads/UsageLimitRecoveryCard.tsx line 37OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +17,21 @@17+ const updateMetadata = useAtomCommand(threadEnvironment.updateMetadata);18+ const [pending, setPending] = useState(false);19+ const resetAt = thread.runtime?.usageLimitResetAt ?? null;20+ const canSchedule =21+ resetAt !== null &&22+ Date.parse(resetAt) > Date.parse(thread.latestRun?.completedAt ?? thread.updatedAt);23+ const runId = thread.latestRun?.runId;24+ const recovery = thread.limitRecovery;25+ const scheduled =26+ recovery?.runId === runId && recovery?.resetAt === resetAt && recovery?.autoResume;27+ if (28+ thread.runtime?.status !== "failed" ||29+ thread.runtime.lastErrorClass !== "usage_limit" ||30+ !runId31+ )32+ return null;33+ async function toggle() {34+ if (!resetAt || !runId || !canSchedule) return;35+ setPending(true);36+ try {37+ await updateMetadata({
- apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.ts lines 55 to 59Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +39,21 @@39+ const recovery = thread.limitRecovery;40+ if (recovery?.runId !== thread.latestRunId || recovery.resetAt !== thread.usageLimitResetAt) {41+ if (!autoResume) return null;42+ return {43+ type: "thread.metadata.update",44+ commandId: CommandId.make(`limit-arm:${identity}`),45+ threadId: thread.id,46+ limitRecovery: { runId: thread.latestRunId, resetAt: thread.usageLimitResetAt, autoResume },47+ };48+ }49+ if (50+ !recovery.autoResume ||51+ resetMs > nowMs ||52+ (thread.snoozedUntil != null && DateTime.toEpochMillis(thread.snoozedUntil) > nowMs)53+ )54+ return null;55+ const deliveryIdentity = `${identity}:${recovery.requestId ?? "legacy"}`;56+ return {57+ type: "message.dispatch",58+ commandId: CommandId.make(`limit-resume:${deliveryIdentity}`),59+ messageId: MessageId.make(`limit-resume:${deliveryIdentity}`),docs/user/thread-sidebar.md lines 141 to 148Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -138,3 +138,10 @@138138 keeps the provider's explanation. Retry after the limit resets, or switch to139139 another provider instance.140140141+When the provider reports a reset time, choose **Resume at reset** to schedule a142+continuation. You can cancel it from the thread. Enable **Auto-resume limited143+threads** in thread behavior settings to schedule limit stops by default.144+The environment must be running when the reset arrives; it resumes overdue145+continuations after a restart. Sending a new message, archiving, or settling the146+thread prevents a pending continuation from starting.147+
- apps/web/src/components/ChatView.tsx line 10384OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -10356,4 +10380,5 @@1035610380 onOpenProviderSetup={openProviderSetup}1035710381 />1035810382 <ThreadErrorBanner10359- error={visibleThreadError}10383+ error={10384+ limitRecoveryBanner !== null && visibleThreadError === serverRuntime?.lastError
- apps/web/src/components/chat/UsageLimitRecoveryBanner.tsx line 17Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,18 @@1+import { type OrchestrationV2LimitRecovery, type RunId } from "@t3tools/contracts";2+import { GaugeIcon } from "lucide-react";3+import { useState } from "react";4+import { Button } from "../ui/button";5+import type { ComposerBannerStackItem } from "./ComposerBannerStack";6+7+type RecoveryProps = {8+ runId: RunId;9+ resetAt: string | null;10+ stoppedAt: string;11+ recovery: OrchestrationV2LimitRecovery | null;12+ explanation: string | null;13+ onChange: (recovery: OrchestrationV2LimitRecovery) => Promise<void>;14+};15+16+export function usageLimitRecoveryBannerItem(props: RecoveryProps): ComposerBannerStackItem {17+ const { runId, resetAt, stoppedAt, recovery, explanation } = props;18+ const canSchedule = resetAt !== null && Date.parse(resetAt) > Date.parse(stoppedAt);
- @@ -0,0 +29,21 @@29+}30+31+function RecoveryActions({ runId, resetAt, recovery, onChange }: RecoveryProps) {32+ const [pending, setPending] = useState(false);33+ const [error, setError] = useState<string | null>(null);34+ const scheduled =35+ recovery?.runId === runId && recovery.resetAt === resetAt && recovery.autoResume;36+ async function toggle() {37+ if (resetAt === null) return;38+ setPending(true);39+ setError(null);40+ try {41+ await onChange({ runId, resetAt, autoResume: !scheduled });42+ } catch (cause) {43+ setError(cause instanceof Error ? cause.message : "Could not change limit recovery.");44+ } finally {45+ setPending(false);46+ }47+ }48+ return (49+ <div className="flex flex-wrap items-center gap-2">
- apps/server/src/orchestration-v2/runtimeLayer.ts lines 1 to 1OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1 @@1+import { workerLive as usageLimitRecoveryWorkerLive } from "./UsageLimitRecoveryService.ts";apps/server/src/orchestration-v2/UsageLimitRecoveryService.ts lines 11 to 13OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +1,13 @@1+import {2+ CommandId,3+ MessageId,4+ type OrchestrationV2ThreadShell,5+ type OrchestrationV2Command,6+} from "@t3tools/contracts";7+import * as DateTime from "effect/DateTime";8+import * as Effect from "effect/Effect";9+import * as Layer from "effect/Layer";10+import * as Schedule from "effect/Schedule";11+import { ServerSettingsService } from "../serverSettings.ts";12+import { ProjectionStoreV2 } from "./ProjectionStore.ts";13+import { ThreadManagementService } from "./ThreadManagementService.ts";
- apps/server/src/orchestration-v2/UsageLimitRecoveryService.ts lines 73 to 73OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +53,21 @@53+ )54+ return null;55+ const deliveryIdentity = `${identity}:${recovery.requestId ?? "legacy"}`;56+ return {57+ type: "message.dispatch",58+ commandId: CommandId.make(`limit-resume:${deliveryIdentity}`),59+ messageId: MessageId.make(`limit-resume:${deliveryIdentity}`),60+ threadId: thread.id,61+ usageLimitContinuationOfRunId: thread.latestRunId,62+ ...(recovery.requestId === undefined63+ ? {}64+ : { usageLimitRecoveryRequestId: recovery.requestId }),65+ text: "Continue where you left off.",66+ attachments: [],67+ dispatchMode: { type: "start_immediately" },68+ createdBy: "user",69+ creationSource: "server",70+ };71+}72+73+const make = Effect.gen(function* () {
juliusmarminge merged thisbe6b013
juliusmarminge closed this
Sign in to join this conversation.


macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed