Skip to content

Commit 239091b

Browse files
feat(claude): stop threads at subscription usage limits (#376)
* feat(claude): stop threads at subscription usage limits * feat(settings): enforce Claude subscription guard across threads * feat(clients): toggle Stop at subscription limit from the thread menu Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Cursor <[email protected]>
1 parent e8af9f3 commit 239091b

30 files changed

Lines changed: 1108 additions & 16 deletions

‎apps/mobile/src/features/home/HomeRouteScreen.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export function HomeRouteScreen() {
102102
pinThread,
103103
unpinThread,
104104
setThreadAutoSettle,
105+
setThreadSubscriptionLimitGuard,
105106
setThreadHidden,
106107
setThreadGroup,
107108
moveThreadToNewGroup,
@@ -320,6 +321,7 @@ export function HomeRouteScreen() {
320321
onPinThread={pinThread}
321322
onUnpinThread={unpinThread}
322323
onSetThreadAutoSettle={setThreadAutoSettle}
324+
onSetThreadSubscriptionLimitGuard={setThreadSubscriptionLimitGuard}
323325
onSetThreadHidden={setThreadHidden}
324326
onSetThreadGroup={setThreadGroup}
325327
onMoveThreadToNewGroup={moveThreadToNewGroup}

‎apps/mobile/src/features/home/HomeScreen.tsx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ interface HomeScreenProps {
158158
thread: EnvironmentThreadShell,
159159
enabled: boolean,
160160
) => Promise<boolean>;
161+
readonly onSetThreadSubscriptionLimitGuard: (
162+
thread: EnvironmentThreadShell,
163+
enabled: boolean,
164+
) => Promise<boolean>;
161165
readonly onSetThreadHidden: (thread: EnvironmentThreadShell, hidden: boolean) => Promise<boolean>;
162166
readonly onSetThreadGroup: (
163167
thread: EnvironmentThreadShell,
@@ -486,6 +490,12 @@ export function HomeScreen(props: HomeScreenProps) {
486490
const handleSetThreadAutoSettle = (thread: EnvironmentThreadShell, enabled: boolean) => {
487491
void props.onSetThreadAutoSettle(thread, enabled);
488492
};
493+
const handleSetThreadSubscriptionLimitGuard = (
494+
thread: EnvironmentThreadShell,
495+
enabled: boolean,
496+
) => {
497+
void props.onSetThreadSubscriptionLimitGuard(thread, enabled);
498+
};
489499
const handleSetThreadHidden = (thread: EnvironmentThreadShell, hidden: boolean) => {
490500
void props.onSetThreadHidden(thread, hidden);
491501
};
@@ -783,6 +793,7 @@ export function HomeScreen(props: HomeScreenProps) {
783793
onPinThread={handlePinThread}
784794
onUnpinThread={handleUnpinThread}
785795
onSetThreadAutoSettle={handleSetThreadAutoSettle}
796+
onSetThreadSubscriptionLimitGuard={handleSetThreadSubscriptionLimitGuard}
786797
onSetThreadHidden={handleSetThreadHidden}
787798
onSetThreadGroup={handleSetThreadGroup}
788799
onMoveThreadToNewGroup={props.onMoveThreadToNewGroup}

‎apps/mobile/src/features/home/useThreadListActions.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ThreadMoveDestination } from "../threads/threadOrder";
22
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
3+
import { withSubscriptionLimitOption } from "@t3tools/client-runtime/state/subscription-limit-guard";
34
import { canSnooze, effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled";
45
import { AuthOrchestrationOperateScope, type ProjectId } from "@t3tools/contracts";
56
import * as Cause from "effect/Cause";
@@ -243,6 +244,10 @@ export function useThreadListActions(): {
243244
thread: EnvironmentThreadShell,
244245
enabled: boolean,
245246
) => Promise<boolean>;
247+
readonly setThreadSubscriptionLimitGuard: (
248+
thread: EnvironmentThreadShell,
249+
enabled: boolean,
250+
) => Promise<boolean>;
246251
/** Hides the thread from the main sections, or brings it back. */
247252
readonly setThreadHidden: (thread: EnvironmentThreadShell, hidden: boolean) => Promise<boolean>;
248253
/** Moves the thread into a named group, or out of its group with null. */
@@ -482,6 +487,30 @@ export function useThreadListActions(): {
482487
}
483488
return true;
484489
};
490+
const setThreadSubscriptionLimitGuard = async (
491+
thread: EnvironmentThreadShell,
492+
enabled: boolean,
493+
) => {
494+
selectionHaptic();
495+
const result = await updateThreadMetadata({
496+
environmentId: thread.environmentId,
497+
input: {
498+
threadId: thread.id,
499+
modelSelection: withSubscriptionLimitOption(thread.modelSelection, enabled),
500+
},
501+
});
502+
if (result._tag === "Failure") {
503+
const error = Cause.squash(result.cause);
504+
Dialog.alert(
505+
"Could not update subscription limit",
506+
error instanceof Error && error.message.trim().length > 0
507+
? error.message
508+
: "The subscription limit setting could not be changed.",
509+
);
510+
return false;
511+
}
512+
return true;
513+
};
485514
const setThreadHidden = async (thread: EnvironmentThreadShell, hidden: boolean) => {
486515
const title = hidden ? "Could not hide thread" : "Could not unhide thread";
487516
if (!environmentSupportsHiding(thread.environmentId)) {
@@ -851,6 +880,7 @@ export function useThreadListActions(): {
851880
pinThread,
852881
unpinThread,
853882
setThreadAutoSettle,
883+
setThreadSubscriptionLimitGuard,
854884
setThreadHidden,
855885
setThreadGroup,
856886
moveThreadToNewGroup,

‎apps/mobile/src/features/settings/SettingsThreadsRouteScreen.tsx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ function AutoSettleSettingsRows() {
8888

8989
const writeToAll = (
9090
patch: Partial<AutoSettleSettings> & {
91+
stopAtSubscriptionLimit?: boolean;
9192
autoResumeLimitedThreads?: boolean;
9293
snoozeLimitedThreads?: boolean;
9394
},
@@ -177,6 +178,14 @@ function AutoSettleSettingsRows() {
177178
) : null}
178179
{!projectSelected ? (
179180
<SettingsSection title="Usage limits">
181+
<SettingsSwitchRow
182+
icon="clock"
183+
label="Stop at subscription limit"
184+
subtitle="Stop all Claude threads before paid extra usage, overriding thread options. Rechecks running sessions when enabled. Provider reports can lag; this is not a billing cap."
185+
value={uniformMobileSetting(displayTargets, "stopAtSubscriptionLimit")}
186+
disabled={disabled}
187+
onValueChange={(value) => writeToAll({ stopAtSubscriptionLimit: value })}
188+
/>
180189
<SettingsSwitchRow
181190
icon="clock"
182191
label="Auto-resume limited threads"

‎apps/mobile/src/features/settings/settings-scoped-server.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const firstProject = "first-project" as ProjectId;
2020
const secondProject = "second-project" as ProjectId;
2121

2222
describe("mobile usage-limit settings across environments", () => {
23-
it.each(["autoResumeLimitedThreads", "snoozeLimitedThreads"] as const)(
23+
it.each(["stopAtSubscriptionLimit", "autoResumeLimitedThreads", "snoozeLimitedThreads"] as const)(
2424
"shows %s as mixed and can enable it everywhere without changing other settings",
2525
(key) => {
2626
const targets = resolveMobileSettingsTargets(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ function ThreadNavigationSidebarPane(
171171
pinThread,
172172
unpinThread,
173173
setThreadAutoSettle,
174+
setThreadSubscriptionLimitGuard,
174175
setThreadHidden,
175176
setThreadGroup,
176177
moveThreadToNewGroup,
@@ -806,6 +807,7 @@ function ThreadNavigationSidebarPane(
806807
onPinThread={pinThread}
807808
onUnpinThread={unpinThread}
808809
onSetThreadAutoSettle={setThreadAutoSettle}
810+
onSetThreadSubscriptionLimitGuard={setThreadSubscriptionLimitGuard}
809811
onSetThreadHidden={setThreadHidden}
810812
onSetThreadGroup={setThreadGroup}
811813
onMoveThreadToNewGroup={moveThreadToNewGroup}

‎apps/mobile/src/features/threads/thread-list-v2-items.tsx‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from "@t3tools/contracts";
2626
import { threadRuntimeCanArchive } from "@t3tools/client-runtime/state/models";
2727
import { threadMoveTargets } from "@t3tools/client-runtime/state/projects";
28+
import { resolveThreadSubscriptionLimitGuard } from "@t3tools/client-runtime/state/subscription-limit-guard";
2829
import { useProjects, useServerConfigs } from "../../state/entities";
2930
import { canSnooze, resolveSnoozePresets } from "@t3tools/client-runtime/state/thread-settled";
3031
import type { MenuAction } from "@react-native-menu/menu";
@@ -563,6 +564,10 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
563564
readonly onPinThread: (thread: EnvironmentThreadShell) => void;
564565
readonly onUnpinThread: (thread: EnvironmentThreadShell) => void;
565566
readonly onSetThreadAutoSettle: (thread: EnvironmentThreadShell, enabled: boolean) => void;
567+
readonly onSetThreadSubscriptionLimitGuard: (
568+
thread: EnvironmentThreadShell,
569+
enabled: boolean,
570+
) => void;
566571
readonly onSetThreadHidden: (thread: EnvironmentThreadShell, hidden: boolean) => void;
567572
readonly onSetThreadGroup: (thread: EnvironmentThreadShell, groupName: string | null) => void;
568573
readonly onMoveThreadToNewGroup: (thread: EnvironmentThreadShell) => void;
@@ -613,6 +618,7 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
613618
onPinThread,
614619
onUnpinThread,
615620
onSetThreadAutoSettle,
621+
onSetThreadSubscriptionLimitGuard,
616622
onSetThreadHidden,
617623
onSetThreadGroup,
618624
onMoveThreadToNewGroup,
@@ -778,6 +784,39 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
778784
} satisfies MenuAction,
779785
]
780786
: [])() as MenuAction[];
787+
const threadServerConfig = serverConfigs.get(thread.environmentId);
788+
const subscriptionLimit = resolveThreadSubscriptionLimitGuard({
789+
provider: threadServerConfig?.providers.find(
790+
(candidate) => candidate.instanceId === thread.modelSelection.instanceId,
791+
),
792+
selection: thread.modelSelection,
793+
appSettingEnabled: threadServerConfig?.settings.stopAtSubscriptionLimit === true,
794+
});
795+
const subscriptionLimitMenuItems = (() =>
796+
subscriptionLimit
797+
? [
798+
{
799+
id: "subscription-limit",
800+
title: "Stop at subscription limit",
801+
...(subscriptionLimit.enforcedByApp
802+
? { subtitle: "On for all threads in Settings" }
803+
: {}),
804+
image: "speedometer",
805+
subactions: [
806+
{
807+
id: "subscription-limit:on",
808+
title: "On",
809+
state: subscriptionLimit.enabled ? "on" : "off",
810+
},
811+
{
812+
id: "subscription-limit:off",
813+
title: "Off",
814+
state: subscriptionLimit.enabled ? "off" : "on",
815+
},
816+
],
817+
} satisfies MenuAction,
818+
]
819+
: [])() as MenuAction[];
781820
const hideMenuItems = (() =>
782821
props.hidingSupported
783822
? [{ id: "hide", title: "Hide thread", image: "eye.slash" }]
@@ -846,6 +885,7 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
846885
...arrangementMenuItems,
847886
...titleMenuItems,
848887
...autoSettleMenuItems,
888+
...subscriptionLimitMenuItems,
849889
...groupMenuItems,
850890
...projectMenuItems,
851891
...hideMenuItems,
@@ -856,6 +896,7 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
856896
...arrangementMenuItems,
857897
...titleMenuItems,
858898
...autoSettleMenuItems,
899+
...subscriptionLimitMenuItems,
859900
...groupMenuItems,
860901
...projectMenuItems,
861902
...hideMenuItems,
@@ -868,6 +909,7 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
868909
),
869910
...titleMenuItems,
870911
...autoSettleMenuItems,
912+
...subscriptionLimitMenuItems,
871913
...groupMenuItems,
872914
...projectMenuItems,
873915
...hideMenuItems,
@@ -877,6 +919,7 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
877919
SNOOZED_MENU_ACTIONS[0]!,
878920
...titleMenuItems,
879921
...autoSettleMenuItems,
922+
...subscriptionLimitMenuItems,
880923
...groupMenuItems,
881924
...projectMenuItems,
882925
...hideMenuItems,
@@ -888,6 +931,7 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
888931
HIDDEN_MENU_ACTIONS[0]!,
889932
...titleMenuItems,
890933
...autoSettleMenuItems,
934+
...subscriptionLimitMenuItems,
891935
...groupMenuItems,
892936
...projectMenuItems,
893937
HIDDEN_MENU_ACTIONS[1]!,
@@ -911,6 +955,11 @@ export const ThreadListV2Row = memo(function ThreadListV2Row(props: {
911955
if (nativeEvent.event === "unpin") handleUnpin();
912956
if (nativeEvent.event === "auto-settle:enabled") handleSetAutoSettle(true);
913957
if (nativeEvent.event === "auto-settle:disabled") handleSetAutoSettle(false);
958+
if (nativeEvent.event === "subscription-limit:on")
959+
onSetThreadSubscriptionLimitGuard(thread, true);
960+
if (nativeEvent.event === "subscription-limit:off") {
961+
onSetThreadSubscriptionLimitGuard(thread, false);
962+
}
914963
if (nativeEvent.event === "hide") handleHide();
915964
if (nativeEvent.event === "unhide") handleUnhide();
916965
if (nativeEvent.event === "group:new") handleNewGroup();

0 commit comments

Comments
 (0)