Skip to content

Commit abaa867

Browse files
rwspatinclaude
andcommitted
feat(settings): allow per-project override of usage-limit auto-resume
"Resume after usage limits" was environment-only, so a user could not enable it for one project or keep it off in another. It is now a project-scoped server setting, like continuing threads after restarts. The recovery sweep loads unarmed candidates whenever any scope enables auto-resume, and resolves the effective value per candidate from its project in resolveLimitRecoveryPolicy. Snooze stays environment-wide. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 47f2c8d commit abaa867

11 files changed

Lines changed: 378 additions & 33 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ const PAGE_PROJECT_KEYS: Record<SettingsPage, readonly ProjectScopedServerSettin
6464
"enableAgentBrowserAccess",
6565
"sidebarAutoSettleOnMerge",
6666
"sidebarAutoSettleAfterDays",
67+
"autoResumeLimitedThreads",
6768
],
6869
"source-control": ["defaultAutoPull", "removeAgentCreditsOnMerge"],
6970
worktrees: [

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,28 @@ export function AutoSettleSettingsRows(props: {
112112

113113
return (
114114
<View className="gap-6">
115-
{!projectSelected ? (
116-
<SettingsSection title="Usage limits">
117-
<SettingsSwitchRow
118-
icon="clock"
119-
label="Resume after usage limits"
120-
subtitle="Automatically resume threads when their usage limits reset. You can cancel auto-resume in each thread."
121-
value={uniformMobileSetting(displayTargets, "autoResumeLimitedThreads")}
122-
disabled={disabledFor("autoResumeLimitedThreads")}
123-
onValueChange={(value) => writeToAll({ autoResumeLimitedThreads: value })}
124-
/>
125-
<SettingsSwitchRow
126-
icon="clock"
127-
label="Snooze until limits reset"
128-
subtitle="Snooze threads until their usage limits reset. Enable auto-resume to continue the agent after the reset."
129-
value={uniformMobileSetting(displayTargets, "snoozeLimitedThreads")}
130-
disabled={disabledFor("snoozeLimitedThreads")}
131-
onValueChange={(value) => writeToAll({ snoozeLimitedThreads: value })}
132-
/>
133-
</SettingsSection>
134-
) : null}
115+
<SettingsSection title="Usage limits">
116+
<SettingsSwitchRow
117+
icon="clock"
118+
label="Resume after usage limits"
119+
subtitle="Automatically resume threads when their usage limits reset. You can cancel auto-resume in each thread."
120+
value={uniformMobileSetting(displayTargets, "autoResumeLimitedThreads")}
121+
disabled={disabledFor("autoResumeLimitedThreads")}
122+
onValueChange={(value) => writeToAll({ autoResumeLimitedThreads: value })}
123+
/>
124+
<SettingsSwitchRow
125+
icon="clock"
126+
label="Snooze until limits reset"
127+
subtitle={
128+
projectSelected
129+
? "Environment-wide setting. Select All projects to change it."
130+
: "Snooze threads until their usage limits reset. Enable auto-resume to continue the agent after the reset."
131+
}
132+
value={uniformMobileSetting(displayTargets, "snoozeLimitedThreads")}
133+
disabled={disabledFor("snoozeLimitedThreads") || projectSelected}
134+
onValueChange={(value) => writeToAll({ snoozeLimitedThreads: value })}
135+
/>
136+
</SettingsSection>
135137
<SettingsSection title="Auto-settle">
136138
<SettingsSwitchRow
137139
icon="arrow.triangle.branch"

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,29 @@ describe("mobile usage-limit settings across environments", () => {
5454
expect(uniformMobileSetting([], key)).toBeNull();
5555
},
5656
);
57+
58+
it("overrides resume per project while snooze stays environment-wide", () => {
59+
const settings: ServerSettings = {
60+
...DEFAULT_SERVER_SETTINGS,
61+
autoResumeLimitedThreads: false,
62+
};
63+
const targets = resolveMobileSettingsTargets(
64+
[environment(firstId, settings)],
65+
[{ environmentId: firstId, id: firstProject }],
66+
);
67+
68+
expect(
69+
planMobileScopedSettingsPatch(targets, true, { autoResumeLimitedThreads: true }),
70+
).toEqual([
71+
{
72+
environmentId: firstId,
73+
patch: { projectSettingsOverrides: { [firstProject]: { autoResumeLimitedThreads: true } } },
74+
},
75+
]);
76+
expect(planMobileScopedSettingsPatch(targets, true, { snoozeLimitedThreads: true })).toEqual(
77+
[],
78+
);
79+
});
5780
});
5881

5982
function environment(environmentId: EnvironmentId, settings: ServerSettings): SettingsTarget {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4811,6 +4811,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
48114811
if (lastErrorClass === "usage_limit") {
48124812
assert.deepEqual(candidate, {
48134813
id: sqlShell.id,
4814+
projectId: sqlShell.projectId,
48144815
status: sqlShell.status,
48154816
lastErrorClass: sqlShell.lastErrorClass,
48164817
usageLimitResetAt: sqlShell.usageLimitResetAt,

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ export type ProjectionRecoveryKind =
171171
export type ProjectionLimitRecoveryCandidate = Pick<
172172
OrchestrationV2ThreadShell,
173173
| "id"
174+
| "projectId"
174175
| "status"
175176
| "lastErrorClass"
176177
| "latestRunId"
@@ -505,7 +506,10 @@ export interface ProjectionStoreV2Shape {
505506
) => Effect.Effect<OrchestrationV2AppThread, ProjectionStoreV2Error>;
506507
readonly getLimitRecoveryCandidates: (options: {
507508
readonly now: DateTime.Utc;
509+
/** Arms unarmed candidates by default; the project lists flip it, and the worker still decides per candidate. */
508510
readonly autoResume: boolean;
511+
readonly autoResumeEnabledProjectIds?: ReadonlyArray<ProjectId>;
512+
readonly autoResumeDisabledProjectIds?: ReadonlyArray<ProjectId>;
509513
readonly snooze: boolean;
510514
}) => Effect.Effect<ReadonlyArray<ProjectionLimitRecoveryCandidate>, ProjectionStoreV2Error>;
511515
/** Epoch ms of the next armed reset or snooze end after `now`, when a waiting thread becomes a candidate. */
@@ -3916,7 +3920,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
39163920
OR json_extract(t.payload_json, '$.limitRecovery.resetAt') IS NOT json_extract(item.payload_json, '$.failure.resetAt')
39173921
)
39183922
AND (
3919-
${booleanInt(options.autoResume)}
3923+
CASE
3924+
WHEN t.project_id IN (SELECT value FROM json_each(${encodeIdList(options.autoResumeDisabledProjectIds ?? [])})) THEN 0
3925+
WHEN t.project_id IN (SELECT value FROM json_each(${encodeIdList(options.autoResumeEnabledProjectIds ?? [])})) THEN 1
3926+
ELSE ${booleanInt(options.autoResume)}
3927+
END
39203928
OR (${booleanInt(options.snooze)} AND julianday(json_extract(item.payload_json, '$.failure.resetAt')) > julianday(${DateTime.formatIso(options.now)}))
39213929
)
39223930
)
@@ -3938,6 +3946,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
39383946
if (summary.lastErrorClass !== "usage_limit") continue;
39393947
candidates.push({
39403948
id: thread.id,
3949+
projectId: thread.projectId,
39413950
status: "failed",
39423951
lastErrorClass: summary.lastErrorClass,
39433952
usageLimitResetAt: summary.usageLimitResetAt,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { assert, describe, it } from "@effect/vitest";
2-
import { RunId, ThreadId, type OrchestrationV2DomainEvent } from "@t3tools/contracts";
2+
import { ProjectId, RunId, ThreadId, type OrchestrationV2DomainEvent } from "@t3tools/contracts";
33
import * as Clock from "effect/Clock";
44
import * as DateTime from "effect/DateTime";
55
import * as Effect from "effect/Effect";
@@ -46,6 +46,7 @@ it.effect("idle sweeps skip SQL until the next wake time or a relevant event", (
4646
getSettings: Effect.succeed({
4747
autoResumeLimitedThreads: true,
4848
snoozeLimitedThreads: false,
49+
projectSettingsOverrides: {},
4950
} as never),
5051
streamChanges: Stream.never,
5152
}),
@@ -77,6 +78,7 @@ const failedThread = (failedAgoMs: number, resetAgoMs: number) => {
7778
const failedAt = DateTime.makeUnsafe(nowMs - failedAgoMs);
7879
const candidate: ProjectionStore.ProjectionLimitRecoveryCandidate = {
7980
id: ThreadId.make("thread:limited"),
81+
projectId: ProjectId.make("project:limited"),
8082
status: "failed",
8183
lastErrorClass: "usage_limit",
8284
usageLimitResetAt: DateTime.formatIso(DateTime.makeUnsafe(nowMs - resetAgoMs)),

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

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
import { CommandId, MessageId, type OrchestrationV2Command } from "@t3tools/contracts";
1+
import {
2+
CommandId,
3+
MessageId,
4+
type OrchestrationV2Command,
5+
type OrchestrationV2DomainEvent,
6+
ProjectId,
7+
type ServerSettings as ServerSettingsValue,
8+
} from "@t3tools/contracts";
9+
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
210
import * as DateTime from "effect/DateTime";
311
import * as Effect from "effect/Effect";
412
import * as Layer from "effect/Layer";
513
import * as Stream from "effect/Stream";
6-
import type { OrchestrationV2DomainEvent } from "@t3tools/contracts";
714
import * as DueHint from "../scheduling/DueHint.ts";
815
import * as Scheduler from "../scheduling/Scheduler.ts";
916
import { forkParked } from "../serverActivation.ts";
@@ -14,6 +21,33 @@ import * as ThreadManagement from "./ThreadManagementService.ts";
1421

1522
const MAX_OVERDUE_ARM_MS = 24 * 60 * 60 * 1000;
1623

24+
/** The one place a limited thread's recovery policy is resolved. */
25+
export function resolveLimitRecoveryPolicy(
26+
settings: ServerSettingsValue,
27+
thread: Pick<ProjectionStore.ProjectionLimitRecoveryCandidate, "projectId">,
28+
): { readonly autoResume: boolean; readonly snooze: boolean } {
29+
return {
30+
autoResume: resolveProjectSettings(settings, thread.projectId).settings
31+
.autoResumeLimitedThreads,
32+
snooze: settings.snoozeLimitedThreads,
33+
};
34+
}
35+
36+
/** Narrows the sweep's query to candidates whose project could auto-arm. */
37+
function limitAutoResumeScope(settings: ServerSettingsValue) {
38+
const enabled: Array<ProjectId> = [];
39+
const disabled: Array<ProjectId> = [];
40+
for (const [projectId, overrides] of Object.entries(settings.projectSettingsOverrides)) {
41+
if (overrides.autoResumeLimitedThreads === true) enabled.push(ProjectId.make(projectId));
42+
if (overrides.autoResumeLimitedThreads === false) disabled.push(ProjectId.make(projectId));
43+
}
44+
return {
45+
autoResume: settings.autoResumeLimitedThreads,
46+
autoResumeEnabledProjectIds: enabled,
47+
autoResumeDisabledProjectIds: disabled,
48+
};
49+
}
50+
1751
export function limitRecoveryCommand(
1852
thread: ProjectionStore.ProjectionLimitRecoveryCandidate,
1953
autoResume: boolean,
@@ -87,7 +121,7 @@ const affectsLimitRecovery = (event: OrchestrationV2DomainEvent) =>
87121
event.type.startsWith("provider-session.") ||
88122
(event.type === "turn-item.updated" && event.payload.type === "error");
89123

90-
const makeSweep = Effect.gen(function* () {
124+
export const makeSweep = Effect.gen(function* () {
91125
const projections = yield* ProjectionStore.ProjectionStoreV2;
92126
const threads = yield* ThreadManagement.ThreadManagementService;
93127
const orchestrator = yield* Orchestrator.OrchestratorV2;
@@ -107,17 +141,13 @@ const makeSweep = Effect.gen(function* () {
107141
const preferences = yield* settings.getSettings;
108142
const candidates = yield* projections.getLimitRecoveryCandidates({
109143
now,
110-
autoResume: preferences.autoResumeLimitedThreads,
144+
...limitAutoResumeScope(preferences),
111145
snooze: preferences.snoozeLimitedThreads,
112146
});
113147
let acted = false;
114148
for (const thread of candidates) {
115-
const command = limitRecoveryCommand(
116-
thread,
117-
preferences.autoResumeLimitedThreads,
118-
nowMs,
119-
preferences.snoozeLimitedThreads,
120-
);
149+
const policy = resolveLimitRecoveryPolicy(preferences, thread);
150+
const command = limitRecoveryCommand(thread, policy.autoResume, nowMs, policy.snooze);
121151
if (command === null) continue;
122152
acted = true;
123153
yield* Effect.gen(function* () {

0 commit comments

Comments
 (0)