Skip to content

Commit f25ba0b

Browse files
fix(clients): a settled usage-limited thread no longer offers recovery it refuses
The server refuses usage-limit recovery for a settled thread, but the web banner and mobile card still offered Resume at reset and Snooze until reset. Every press failed with "The provider limit changed before recovery could be configured", which named the wrong cause. Both clients now hide those actions on a settled thread and say to un-settle it, and the server rejects that case with its own message.
1 parent 54c5f25 commit f25ba0b

7 files changed

Lines changed: 82 additions & 10 deletions

File tree

‎apps/mobile/src/features/threads/UsageLimitRecoveryCard.test.tsx‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ import { UsageLimitRecoveryCard } from "./UsageLimitRecoveryCard";
2727

2828
const DAY_MS = 24 * 60 * 60 * 1000;
2929

30-
function failedThread(resetAt: string): EnvironmentThreadShell {
30+
function failedThread(resetAt: string, settled = false): EnvironmentThreadShell {
3131
return {
3232
id: "thread-1",
33+
settledOverride: settled ? "settled" : null,
3334
updatedAt: new Date(0).toISOString(),
3435
snoozedUntil: null,
3536
latestRun: { runId: "run-1", completedAt: new Date(0).toISOString() },
@@ -39,6 +40,24 @@ function failedThread(resetAt: string): EnvironmentThreadShell {
3940
}
4041

4142
describe("UsageLimitRecoveryCard", () => {
43+
it("offers no recovery on a settled thread and points at Un-settle", () => {
44+
using _timers = { [Symbol.dispose]: () => vi.useRealTimers() };
45+
vi.useFakeTimers({ now: 0 });
46+
const container = document.createElement("div");
47+
const root = createRoot(container);
48+
using _root = { [Symbol.dispose]: () => act(() => root.unmount()) };
49+
act(() =>
50+
root.render(
51+
<UsageLimitRecoveryCard
52+
thread={failedThread(new Date(DAY_MS).toISOString(), true)}
53+
environmentId={"env-1" as EnvironmentId}
54+
/>,
55+
),
56+
);
57+
expect(container.querySelectorAll("button")).toHaveLength(0);
58+
expect(container.textContent).toContain("Un-settle to resume at reset.");
59+
});
60+
4261
it("disables Snooze once a reset more than 24.8 days away passes", () => {
4362
using _timers = { [Symbol.dispose]: () => vi.useRealTimers() };
4463
vi.useFakeTimers({ now: 0 });

‎apps/mobile/src/features/threads/UsageLimitRecoveryCard.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ export function UsageLimitRecoveryCard({
2222
const [pending, setPending] = useState(false);
2323
const [error, setError] = useState<string | null>(null);
2424
const resetAt = thread.runtime?.usageLimitResetAt ?? null;
25-
const canSchedule =
25+
const resetsLater =
2626
resetAt !== null &&
2727
Date.parse(resetAt) > Date.parse(thread.latestRun?.completedAt ?? thread.updatedAt);
28+
// The server refuses recovery for settled threads until they are un-settled.
29+
const settled = thread.settledOverride === "settled";
30+
const canSchedule = resetsLater && !settled;
2831
const resetPassed = useDeadlinePassed(resetAt === null ? null : Date.parse(resetAt));
2932
const runId = thread.latestRun?.runId;
3033
const recovery = thread.limitRecovery;
@@ -72,7 +75,9 @@ export function UsageLimitRecoveryCard({
7275
<View className="mx-3 mb-2 gap-2 rounded-xl border border-warning-foreground/25 bg-screen p-3">
7376
<Text className="text-sm text-warning-foreground">
7477
{resetAt
75-
? `Usage limit resets ${DateTime.toDateUtc(DateTime.makeUnsafe(resetAt)).toLocaleString()}.`
78+
? `Usage limit resets ${DateTime.toDateUtc(DateTime.makeUnsafe(resetAt)).toLocaleString()}.${
79+
resetsLater && settled ? " Un-settle to resume at reset." : ""
80+
}`
7681
: "The provider did not report a reset time. Retry manually when your limit is available."}
7782
</Text>
7883
{canSchedule ? (

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3304,10 +3304,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
33043304
cause: "The reset time has passed. Retry the thread manually.",
33053305
});
33063306
}
3307+
if (thread.settledOverride === "settled") {
3308+
return yield* new OrchestratorDispatchError({
3309+
commandId: command.commandId,
3310+
commandType: command.type,
3311+
cause: "The thread is settled. Un-settle it to resume at the reset.",
3312+
});
3313+
}
33073314
if (
33083315
!Number.isFinite(resetMs) ||
33093316
thread.archivedAt !== null ||
3310-
thread.settledOverride === "settled" ||
33113317
run?.id !== command.limitRecovery.runId ||
33123318
failure?.class !== "usage_limit" ||
33133319
failure.resetAt !== command.limitRecovery.resetAt ||

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7715,6 +7715,7 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
77157715
"replacement",
77167716
"manual-snooze",
77177717
"invalid-snooze",
7718+
"settled",
77187719
"cancel-resume-keep-snooze",
77197720
"independent-patches",
77207721
"expired-snooze",
@@ -7868,6 +7869,27 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
78687869
assert.isNull(current.thread.snoozedUntil);
78697870
return;
78707871
}
7872+
if (scenario === "settled") {
7873+
yield* orchestrator.dispatch({
7874+
type: "thread.settle",
7875+
commandId: CommandId.make("recovery:settle:settled"),
7876+
threadId,
7877+
});
7878+
const error = yield* orchestrator
7879+
.dispatch({
7880+
type: "thread.metadata.update",
7881+
commandId: CommandId.make("recovery:arm:settled"),
7882+
threadId,
7883+
limitRecovery: { runId: run.id, resetAt, autoResume: true },
7884+
})
7885+
.pipe(Effect.flip);
7886+
assert(error._tag === "OrchestratorDispatchError");
7887+
assert.equal(error.cause, "The thread is settled. Un-settle it to resume at the reset.");
7888+
assert.isNull(
7889+
(yield* orchestrator.getThreadProjection(threadId)).thread.limitRecovery ?? null,
7890+
);
7891+
return;
7892+
}
78717893
const snooze = scenario === "wake" || scenario === "cancel-resume-keep-snooze";
78727894
const autoResume = scenario !== "wake";
78737895
const arm = limitRecoveryCommand(shell, autoResume, DateTime.toEpochMillis(now), snooze);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,6 +2551,7 @@ export default function ChatView(props: ChatViewProps) {
25512551
runId: activeThreadShell.latestRun.runId,
25522552
resetAt: serverRuntime.usageLimitResetAt ?? null,
25532553
stoppedAt: activeThreadShell.latestRun.completedAt ?? activeThreadShell.updatedAt,
2554+
settled: activeThreadShell.settledOverride === "settled",
25542555
recovery: activeThreadShell.limitRecovery ?? null,
25552556
snoozedUntil: activeThreadShell.snoozedUntil,
25562557
timestampFormat,

‎apps/web/src/components/chat/UsageLimitRecoveryBanner.test.tsx‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@ import { usageLimitRecoveryBannerItem } from "./UsageLimitRecoveryBanner";
66

77
const stoppedAt = new Date(2026, 9, 20, 12, 0);
88

9-
function bannerDescription(resetAt: Date, timestampFormat: TimestampFormat) {
9+
function banner(resetAt: Date, timestampFormat: TimestampFormat, settled = false) {
1010
return usageLimitRecoveryBannerItem({
1111
runId: RunId.make("run-1"),
1212
resetAt: resetAt.toISOString(),
1313
stoppedAt: stoppedAt.toISOString(),
14+
settled,
1415
snoozedUntil: null,
1516
recovery: null,
1617
timestampFormat,
1718
onChange: async () => {},
18-
}).description;
19+
});
20+
}
21+
22+
function bannerDescription(resetAt: Date, timestampFormat: TimestampFormat) {
23+
return banner(resetAt, timestampFormat).description;
1924
}
2025

2126
describe("usageLimitRecoveryBannerItem", () => {
@@ -27,6 +32,15 @@ describe("usageLimitRecoveryBannerItem", () => {
2732
expect(bannerDescription(new Date(2026, 9, 20, 17, 30), timestampFormat)).toBe(expected);
2833
});
2934

35+
it("points a settled thread at Un-settle instead of offering recovery the server refuses", () => {
36+
using _now = vi.spyOn(Date, "now").mockReturnValue(stoppedAt.getTime());
37+
const resetAt = new Date(2026, 9, 20, 17, 30);
38+
expect(banner(resetAt, "24-hour").actions).not.toBeNull();
39+
const settled = banner(resetAt, "24-hour", true);
40+
expect(settled.actions).toBeNull();
41+
expect(settled.description).toBe("Resets 17:30. Un-settle to resume at reset");
42+
});
43+
3044
it("names the day when the reset is tomorrow", () => {
3145
using _now = vi.spyOn(Date, "now").mockReturnValue(stoppedAt.getTime());
3246
expect(bannerDescription(new Date(2026, 9, 21, 17, 30), "24-hour")).toBe(

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,29 @@ type RecoveryProps = {
1717
runId: RunId;
1818
resetAt: string | null;
1919
stoppedAt: string;
20+
/** The server refuses recovery for settled threads until they are un-settled. */
21+
settled: boolean;
2022
snoozedUntil: string | null;
2123
recovery: OrchestrationV2LimitRecovery | null;
2224
timestampFormat: TimestampFormat;
2325
onChange: (recovery: OrchestrationV2LimitRecoveryUpdate) => Promise<void>;
2426
};
2527

2628
export function usageLimitRecoveryBannerItem(props: RecoveryProps): ComposerBannerStackItem {
27-
const { runId, resetAt, stoppedAt, timestampFormat } = props;
28-
const canSchedule = resetAt !== null && Date.parse(resetAt) > Date.parse(stoppedAt);
29+
const { runId, resetAt, stoppedAt, settled, timestampFormat } = props;
30+
const resetsLater = resetAt !== null && Date.parse(resetAt) > Date.parse(stoppedAt);
2931
return {
3032
id: `usage-limit-recovery:${runId}`,
3133
variant: "warning",
3234
icon: <GaugeIcon />,
3335
title: "Usage limit reached",
3436
description: resetAt
35-
? `Resets ${formatUpcomingTimestamp(resetAt, timestampFormat)}`
37+
? `Resets ${formatUpcomingTimestamp(resetAt, timestampFormat)}${
38+
resetsLater && settled ? ". Un-settle to resume at reset" : ""
39+
}`
3640
: "Reset time unavailable; retry manually",
37-
actions: canSchedule ? <RecoveryActions key={`${runId}:${resetAt}`} {...props} /> : null,
41+
actions:
42+
resetsLater && !settled ? <RecoveryActions key={`${runId}:${resetAt}`} {...props} /> : null,
3843
};
3944
}
4045

0 commit comments

Comments
 (0)