Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion apps/mobile/src/features/settings/SettingsThreadsRouteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ function AutoSettleSettingsRows() {
}

const writeToAll = (
patch: Partial<AutoSettleSettings> & { autoResumeLimitedThreads?: boolean },
patch: Partial<AutoSettleSettings> & {
autoResumeLimitedThreads?: boolean;
snoozeLimitedThreads?: boolean;
},
) => {
if (writeInFlight.current) return;
const writes = planMobileScopedSettingsPatch(syncTargets, projectSelected, patch);
Expand Down Expand Up @@ -175,6 +178,13 @@ function AutoSettleSettingsRows() {
disabled={disabled}
onValueChange={(value) => writeToAll({ autoResumeLimitedThreads: value })}
/>
<SettingsSwitchRow
icon="clock"
label="Snooze limited threads"
value={referenceSettings.snoozeLimitedThreads}
disabled={disabled}
onValueChange={(value) => writeToAll({ snoozeLimitedThreads: value })}
/>
</SettingsSection>
) : null}
<SettingsSection title="Auto-settle">
Expand Down
54 changes: 42 additions & 12 deletions apps/mobile/src/features/threads/UsageLimitRecoveryCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,32 @@ export function UsageLimitRecoveryCard({
!runId
)
return null;
async function toggle() {
const snoozed =
recovery?.snooze === true &&
recovery.runId === runId &&
recovery.resetAt === resetAt &&
resetAt !== null &&
thread.snoozedUntil !== null &&
Date.parse(thread.snoozedUntil) === Date.parse(resetAt);
async function toggle(action: "resume" | "snooze") {
if (!resetAt || !runId || !canSchedule) return;
if (action === "snooze" && !snoozed && Date.parse(resetAt) <= Date.now()) {
setError("The reset time has passed. Retry the thread manually.");
return;
}
setPending(true);
setError(null);
try {
const result = await updateMetadata({
environmentId,
input: { threadId: thread.id, limitRecovery: { runId, resetAt, autoResume: !scheduled } },
input: {
threadId: thread.id,
limitRecovery: {
runId,
resetAt,
...(action === "resume" ? { autoResume: !scheduled } : { snooze: !snoozed }),
},
},
});
if (result._tag === "Failure") throw squashAtomCommandFailure(result);
} catch (cause) {
Expand All @@ -56,16 +74,28 @@ export function UsageLimitRecoveryCard({
: "The provider did not report a reset time. Retry manually when your limit is available."}
</Text>
{canSchedule ? (
<Pressable
accessibilityRole="button"
disabled={pending}
onPress={() => void toggle()}
className="self-start rounded-lg bg-subtle px-3 py-2 active:opacity-70"
>
<Text className="text-sm text-foreground">
{scheduled ? "Cancel auto-resume" : "Resume at reset"}
</Text>
</Pressable>
<View className="flex-row flex-wrap gap-2">
<Pressable
accessibilityRole="button"
disabled={pending}
onPress={() => void toggle("resume")}
className="self-start rounded-lg bg-subtle px-3 py-2 active:opacity-70"
>
<Text className="text-sm text-foreground">
{scheduled ? "Cancel auto-resume" : "Resume at reset"}
</Text>
</Pressable>
<Pressable
accessibilityRole="button"
disabled={pending || (!snoozed && Date.parse(resetAt!) <= Date.now())}
onPress={() => void toggle("snooze")}
className="self-start rounded-lg bg-subtle px-3 py-2 active:opacity-70"
>
<Text className="text-sm text-foreground">
{snoozed ? "Wake now" : "Snooze until reset"}
</Text>
</Pressable>
</View>
) : null}
{error ? (
<Text accessibilityRole="alert" className="text-sm text-destructive">
Expand Down
58 changes: 47 additions & 11 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2231,14 +2231,22 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
const projection = yield* loadProjectionForCommand(command);
const run = projection.runs.at(-1) ?? null;
const failure = latestRootProviderFailure(run, projection.turnItems);
const resetMs = Date.parse(command.limitRecovery.resetAt);
if (command.limitRecovery.snooze === true && resetMs <= DateTime.toEpochMillis(now)) {
return yield* new OrchestratorDispatchError({
commandId: command.commandId,
commandType: command.type,
cause: "The reset time has passed. Retry the thread manually.",
});
}
if (
!Number.isFinite(resetMs) ||
thread.archivedAt !== null ||
thread.settledOverride === "settled" ||
run?.id !== command.limitRecovery.runId ||
failure?.class !== "usage_limit" ||
failure.resetAt !== command.limitRecovery.resetAt ||
Date.parse(command.limitRecovery.resetAt) <=
DateTime.toEpochMillis(run.completedAt ?? run.requestedAt) ||
resetMs <= DateTime.toEpochMillis(run.completedAt ?? run.requestedAt) ||
projection.runtimeRequests.some((request) => request.status === "pending") ||
projection.runs.some((candidate) => candidate.status === "queued")
) {
Expand Down Expand Up @@ -2334,6 +2342,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
return {
...thread,
snoozedUntil,
limitRecovery: thread.limitRecovery ? { ...thread.limitRecovery, snooze: false } : null,
snoozedAt: existingSnoozedAt ?? now,
updatedAt: existingSnoozedAt === null ? now : thread.updatedAt,
};
Expand Down Expand Up @@ -2402,18 +2411,44 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
}
case "thread.mark-unread":
return { ...thread, lastVisitedAt: markUnreadVisitedAt };
case "thread.metadata.update":
case "thread.metadata.update": {
const previousRecovery =
thread.limitRecovery?.runId === command.limitRecovery?.runId &&
thread.limitRecovery?.resetAt === command.limitRecovery?.resetAt
? thread.limitRecovery
: null;
const limitRecovery =
command.limitRecovery === undefined
? thread.limitRecovery
: command.limitRecovery === null
? null
: {
...command.limitRecovery,
autoResume:
command.limitRecovery.autoResume ?? previousRecovery?.autoResume ?? false,
snooze: command.limitRecovery.snooze ?? previousRecovery?.snooze ?? false,
requestId: command.commandId,
};
return {
...thread,
...(command.title === undefined ? {} : { title: command.title }),
...(command.limitRecovery === undefined
? {}
: {
limitRecovery:
command.limitRecovery === null
? null
: { ...command.limitRecovery, requestId: command.commandId },
}),
...(command.limitRecovery === undefined ? {} : { limitRecovery }),
...(command.limitRecovery !== undefined &&
limitRecovery?.snooze === true &&
Date.parse(limitRecovery.resetAt) > DateTime.toEpochMillis(now)
? {
snoozedUntil: DateTime.makeUnsafe(limitRecovery.resetAt),
// Recovery changes acknowledge the same stopped run; keep its
// metadata timestamp from appearing as a fresh failure wake.
snoozedAt: now,
}
: command.limitRecovery !== undefined &&
thread.limitRecovery?.snooze &&
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Resolved
thread.snoozedUntil != null &&
DateTime.toEpochMillis(thread.snoozedUntil) ===
Date.parse(thread.limitRecovery.resetAt)
? { snoozedUntil: null, snoozedAt: null }
: {}),
...(command.branch === undefined ? {} : { branch: command.branch }),
...(command.worktreePath === undefined ? {} : { worktreePath: command.worktreePath }),
...(command.linkedPullRequest === undefined
Expand Down Expand Up @@ -2461,6 +2496,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
: {}),
updatedAt: now,
};
}
case "thread.pull-request.link":
case "thread.pull-request.unlink":
case "thread.pull-request-link.sync": {
Expand Down
17 changes: 14 additions & 3 deletions apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function limitRecoveryCommand(
thread: OrchestrationV2ThreadShell,
autoResume: boolean,
nowMs: number,
snooze = false,
): OrchestrationV2Command | null {
if (
thread.status !== "failed" ||
Expand All @@ -38,12 +39,17 @@ export function limitRecoveryCommand(
const identity = `${thread.id}:${thread.latestRunId}:${resetMs}`;
const recovery = thread.limitRecovery;
if (recovery?.runId !== thread.latestRunId || recovery.resetAt !== thread.usageLimitResetAt) {
if (!autoResume) return null;
if (!autoResume && (!snooze || resetMs <= nowMs)) return null;
return {
type: "thread.metadata.update",
commandId: CommandId.make(`limit-arm:${identity}`),
threadId: thread.id,
limitRecovery: { runId: thread.latestRunId, resetAt: thread.usageLimitResetAt, autoResume },
limitRecovery: {
runId: thread.latestRunId,
resetAt: thread.usageLimitResetAt,
autoResume,
snooze: snooze && resetMs > nowMs,
},
};
}
if (
Expand Down Expand Up @@ -79,7 +85,12 @@ const makeSweep = Effect.gen(function* () {
const snapshot = yield* projections.getShellSnapshot();
const nowMs = DateTime.toEpochMillis(yield* DateTime.now);
for (const thread of snapshot.threads) {
const command = limitRecoveryCommand(thread, preferences.autoResumeLimitedThreads, nowMs);
const command = limitRecoveryCommand(
thread,
preferences.autoResumeLimitedThreads,
nowMs,
preferences.snoozeLimitedThreads,
);
if (command === null) continue;
yield* threads.dispatch(command).pipe(
Effect.catchCause((cause) =>
Expand Down
Loading
Loading