Skip to content

Commit f0e4b62

Browse files
feat: pause every scheduled task in an environment with one switch
A server setting holds timer, idle and webhook runs without touching which tasks are enabled, so resuming restores exactly what was running. Manual runs still work. The switch sits above the task list; agents can read and set it through the environment preferences tool. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3ba3b2b commit f0e4b62

6 files changed

Lines changed: 108 additions & 1 deletion

File tree

‎apps/server/src/mcp/toolkits/environment/handlers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export function preferences(settings: ServerSettings) {
1414
newWorktreesStartFromOrigin,
1515
enableProviderUpdateChecks,
1616
switchAccountAtUsageLimit,
17+
scheduledTasksPaused,
1718
backgroundActivity,
1819
sourceControlWritingStyle,
1920
} = settings;
@@ -23,6 +24,7 @@ export function preferences(settings: ServerSettings) {
2324
newWorktreesStartFromOrigin,
2425
enableProviderUpdateChecks,
2526
switchAccountAtUsageLimit,
27+
scheduledTasksPaused,
2628
backgroundActivity: { profile: backgroundActivity.profile },
2729
sourceControlWritingStyle: {
2830
...sourceControlWritingStyle,

‎apps/server/src/mcp/toolkits/environment/tools.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const PreferenceFields = {
1919
newWorktreesStartFromOrigin: ServerSettings.fields.newWorktreesStartFromOrigin,
2020
enableProviderUpdateChecks: ServerSettings.fields.enableProviderUpdateChecks,
2121
switchAccountAtUsageLimit: ServerSettings.fields.switchAccountAtUsageLimit,
22+
scheduledTasksPaused: ServerSettings.fields.scheduledTasksPaused,
2223
backgroundActivity: Schema.Struct({ profile: BackgroundActivityProfileSelection }),
2324
sourceControlWritingStyle: Schema.Struct({
2425
mode: Schema.String,
@@ -61,6 +62,7 @@ const EnvironmentPreferencesTool = Tool.make("t3_environment_preferences_update"
6162
newWorktreesStartFromOrigin: ServerSettingsPatch.fields.newWorktreesStartFromOrigin,
6263
enableProviderUpdateChecks: ServerSettingsPatch.fields.enableProviderUpdateChecks,
6364
switchAccountAtUsageLimit: ServerSettingsPatch.fields.switchAccountAtUsageLimit,
65+
scheduledTasksPaused: ServerSettingsPatch.fields.scheduledTasksPaused,
6466
backgroundActivity: Schema.optionalKey(Schema.Struct({ profile: BackgroundActivityProfile })),
6567
sourceControlWritingStyle: ServerSettingsPatch.fields.sourceControlWritingStyle,
6668
}),

‎apps/server/src/scheduledTasks/ScheduledTaskService.test.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import * as TestClock from "effect/testing/TestClock";
1818
import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts";
1919
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
2020
import * as SecretRequests from "../secrets/SecretRequests.ts";
21+
import * as ServerSettings from "../serverSettings.ts";
2122
import * as SqlitePersistence from "../persistence/Sqlite.ts";
2223
import * as ScheduledTaskService from "./ScheduledTaskService.ts";
2324

@@ -351,3 +352,57 @@ it.effect(
351352
);
352353
}).pipe(Effect.provide(SqlitePersistence.layerMemory)),
353354
);
355+
356+
it.effect("holds every due task while scheduled tasks are paused, and runs them once resumed", () =>
357+
Effect.gen(function* () {
358+
const sql = yield* SqlClient.SqlClient;
359+
const now = "2026-09-09T12:00:00.000Z";
360+
yield* insertRow(sql, { id: "due", next: now, status: "never", enabled: 1 }, now);
361+
yield* TestClock.setTime(Date.parse(now) + 1_000);
362+
363+
const dispatched = yield* Ref.make(0);
364+
const launched = yield* Deferred.make<void>();
365+
const serviceWith = (paused: boolean) =>
366+
Layer.provideMerge(
367+
ScheduledTaskService.layer,
368+
Layer.mergeAll(
369+
Layer.mock(ThreadLaunchService.ThreadLaunchService)({
370+
launch: () =>
371+
Ref.update(dispatched, (n) => n + 1).pipe(
372+
Effect.andThen(Deferred.succeed(launched, undefined)),
373+
Effect.andThen(Effect.die(new Error("test launch failure"))),
374+
),
375+
}),
376+
Layer.mock(ThreadManagementService.ThreadManagementService)({}),
377+
Layer.mock(SecretRequests.SecretRequests)({}),
378+
ServerSettings.layerTest({ scheduledTasksPaused: paused }),
379+
NodeCrypto.layer,
380+
Scheduler.layer,
381+
),
382+
);
383+
384+
yield* Effect.scoped(
385+
Effect.gen(function* () {
386+
yield* Layer.build(serviceWith(true));
387+
// Two polls pass; a paused environment dispatches nothing in either.
388+
yield* TestClock.adjust("6 seconds");
389+
yield* TestClock.adjust("6 seconds");
390+
assert.equal(yield* Ref.get(dispatched), 0);
391+
const rows = yield* sql<{ run_count: number; last_run_status: string }>`
392+
SELECT run_count, last_run_status FROM scheduled_tasks WHERE task_id = 'due'
393+
`;
394+
assert.equal(rows[0]?.run_count, 0);
395+
assert.equal(rows[0]?.last_run_status, "never");
396+
}),
397+
);
398+
399+
yield* Effect.scoped(
400+
Effect.gen(function* () {
401+
yield* Layer.build(serviceWith(false));
402+
yield* TestClock.adjust("6 seconds");
403+
yield* Deferred.await(launched);
404+
assert.equal(yield* Ref.get(dispatched), 1);
405+
}),
406+
);
407+
}).pipe(Effect.provide(SqlitePersistence.layerMemory)),
408+
);

‎apps/server/src/scheduledTasks/ScheduledTaskService.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts
4444
import * as Metrics from "../observability/Metrics.ts";
4545
import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts";
4646
import * as SecretRequests from "../secrets/SecretRequests.ts";
47+
import * as ServerSettings from "../serverSettings.ts";
4748
import * as Scheduler from "../scheduling/Scheduler.ts";
4849
import { decideIdleTrigger } from "./idleTrigger.ts";
4950
import { isMissedFixedTimeRun, isSameSchedule, nextScheduledRunAt } from "./Schedule.ts";
@@ -398,6 +399,15 @@ export const layer = Layer.effect(
398399
const threadManagement = yield* ThreadManagementService.ThreadManagementService;
399400
const secretRequests = yield* SecretRequests.SecretRequests;
400401
const scheduler = yield* Scheduler.Scheduler;
402+
// Optional, so harnesses that build this service without settings still work.
403+
const serverSettings = yield* Effect.serviceOption(ServerSettings.ServerSettingsService);
404+
// The environment-wide pause. A manual "run now" is the user asking, and still runs.
405+
const tasksPaused = Option.isNone(serverSettings)
406+
? Effect.succeed(false)
407+
: serverSettings.value.getSettings.pipe(
408+
Effect.map((settings) => settings.scheduledTasksPaused),
409+
Effect.orElseSucceed(() => false),
410+
);
401411
const readWebhookOrigin = yield* ScheduledTaskWebhookOrigin;
402412
// Webhook deliveries for one task dispatch in arrival order rather than
403413
// being dropped while an earlier delivery is still dispatching.
@@ -714,6 +724,12 @@ export const layer = Layer.effect(
714724
trigger: "scheduled" | "manual" | "webhook" | "idle",
715725
webhook?: { readonly deliveryId: string; readonly prompt: string },
716726
) {
727+
if (trigger !== "manual" && (yield* tasksPaused)) {
728+
if (webhook !== undefined) {
729+
return yield* new WebhookDeliverySkipped({ reason: "Scheduled tasks are paused." });
730+
}
731+
return task;
732+
}
717733
const reserved = yield* Ref.modify(activeRuns, (active) => {
718734
if (active.has(task.id)) return [false, active] as const;
719735
const next = new Set(active);
@@ -923,6 +939,7 @@ export const layer = Layer.effect(
923939
});
924940

925941
const runDueTasks = Effect.fn("ScheduledTaskService.runDueTasks")(function* () {
942+
if (yield* tasksPaused) return;
926943
const now = yield* localNow;
927944
const tasks = yield* listDueTasks(now).pipe(
928945
Effect.mapError((cause) => taskError("Could not list schedule tasks.", { cause })),
@@ -1006,6 +1023,7 @@ export const layer = Layer.effect(
10061023
// gone quiet. The thread is read here, on the server, right before the
10071024
// prompt is sent; a prompt that still meets a new run queues behind it.
10081025
const runIdleTasks = Effect.fn("ScheduledTaskService.runIdleTasks")(function* () {
1026+
if (yield* tasksPaused) return;
10091027
const rows = yield* sql<ScheduledTaskRow>`
10101028
SELECT * FROM scheduled_tasks
10111029
WHERE enabled = 1 AND next_run_at IS NULL AND thread_id IS NOT NULL

‎apps/web/src/components/settings/ScheduledTasksSettings.tsx‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
} from "@t3tools/client-runtime/state/runtime";
3737

3838
import { formatRelativeTime } from "../../timestampFormat";
39-
import { useEnvironmentSettings } from "../../hooks/useSettings";
39+
import { useEnvironmentSettings, useUpdateEnvironmentSettings } from "../../hooks/useSettings";
4040
import { getCustomModelOptionsByInstance } from "../../modelSelection";
4141
import {
4242
applyProviderInstanceSettings,
@@ -346,6 +346,12 @@ function ScheduledTaskEnvironmentSection({
346346
const tasks = tasksQuery.data?.tasks.filter((task) =>
347347
matchesScheduledTaskScope(scope, environment.environmentId, task.projectId),
348348
);
349+
const paused = useEnvironmentSettings(
350+
environment.environmentId,
351+
(settings) => settings.scheduledTasksPaused,
352+
);
353+
const updateEnvironmentSettings = useUpdateEnvironmentSettings(environment.environmentId);
354+
const enabledCount = tasks?.filter((task) => task.enabled).length ?? 0;
349355
const linkedTask = tasks?.find((task) => task.id === taskId);
350356
const openedLink = useRef(false);
351357
useEffect(() => {
@@ -384,6 +390,23 @@ function ScheduledTaskEnvironmentSection({
384390
role="status"
385391
/>
386392
) : null}
393+
<SettingsRow
394+
title={paused ? "All tasks are paused" : "Pause all tasks"}
395+
description={
396+
paused
397+
? `Nothing starts on a timer, on idle or from a webhook. ${enabledCount} ${enabledCount === 1 ? "task resumes" : "tasks resume"} when you turn this off.`
398+
: `Holds every task in this environment without changing which ones are enabled. ${enabledCount} of ${tasks.length} enabled.`
399+
}
400+
control={
401+
<Switch
402+
checked={paused}
403+
onCheckedChange={(checked) =>
404+
updateEnvironmentSettings({ scheduledTasksPaused: Boolean(checked) })
405+
}
406+
aria-label="Pause all scheduled tasks"
407+
/>
408+
}
409+
/>
387410
{tasks.length === 0 ? (
388411
<SettingsRow
389412
title="No scheduled tasks"

‎packages/contracts/src/settings.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,12 @@ export const ServerSettings = Schema.Struct({
11221122
* limit or spending paid credits.
11231123
*/
11241124
switchAccountAtUsageLimit: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
1125+
/**
1126+
* Holds every scheduled task in this environment: nothing starts on a timer,
1127+
* on idle or from a webhook until it is turned off again. Each task keeps
1128+
* its own enabled state, so resuming restores exactly what was running.
1129+
*/
1130+
scheduledTasksPaused: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
11251131
// Retain the update-era key; recovery now needs an environment-owned opt-in.
11261132
continueThreadsAfterServerUpdate: Schema.Boolean.pipe(
11271133
Schema.withDecodingDefault(Effect.succeed(false)),
@@ -1474,6 +1480,7 @@ export const ServerSettingsPatch = Schema.Struct({
14741480
responseStreamingMode: Schema.optionalKey(ResponseStreamingMode),
14751481
enableProviderUpdateChecks: Schema.optionalKey(Schema.Boolean),
14761482
switchAccountAtUsageLimit: Schema.optionalKey(Schema.Boolean),
1483+
scheduledTasksPaused: Schema.optionalKey(Schema.Boolean),
14771484
continueThreadsAfterServerUpdate: Schema.optionalKey(Schema.Boolean),
14781485
enableAgentBrowserAccess: Schema.optionalKey(Schema.Boolean),
14791486
projectAgentBrowserAccessOverrides: Schema.optionalKey(

0 commit comments

Comments
 (0)