@devries-cameron20 / t3code-1 / commits / 898c22f990
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(scheduled-tasks): clearing a task's thread no longer keeps the old one (#39639)

Julius Marminge committed Nov 20, 2026 at 02:39 UTC 898c22f9900b90f81daece0895b7e4f4198355f8
6 files changed +33 -5
apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx
+1
@@ -884,6 +884,7 @@ function TaskForm({
884 schedule,
885 enabled: draft.enabled,
886 threadId: draft.threadId,
887 + ...(draft.threadId === null && draft.task?.threadId != null ? { unbindThread: true } : {}),
888 ...(offersThreadModes ? { threadMode: draft.threadMode } : {}),
889 workspaceStrategy:
890 draft.workspace === "root"
apps/server/src/scheduledTasks/ScheduledTaskService.test.ts
+16 -1
@@ -742,7 +742,12 @@ it.effect("a fresh-session task launches one thread, then starts each later run
742 ),
743 }),
744 Layer.mock(ThreadManagementService.ThreadManagementService)({
745 - getThreadShell: () => Effect.succeed({ deletedAt: null } as never),
745 + getThreadShell: () =>
746 + Effect.succeed({
747 + deletedAt: null,
748 + archivedAt: null,
749 + projectId: ProjectId.make("project:test"),
750 + } as never),
751 sendToThread: (input) =>
752 Ref.update(sends, (all) => [
753 ...all,
@@ -805,6 +810,16 @@ it.effect("a fresh-session task launches one thread, then starts each later run
810 assert.equal(moved.task.threadMode, "fresh_session");
811 const unbound = yield* service.upsert({ ...input, threadMode: "new_thread" });
812 assert.equal(unbound.task.threadId, null);
813 + // Clearing the thread keeps the mode, so the next run starts a thread for later runs.
814 + yield* service.upsert({ ...input, threadId: launched, threadMode: "continue" });
815 + const cleared = yield* service.upsert({
816 + ...input,
817 + threadId: null,
818 + unbindThread: true,
819 + threadMode: "continue",
820 + });
821 + assert.equal(cleared.task.threadId, null);
822 + assert.equal(cleared.task.threadMode, "continue");
823 }).pipe(Effect.provide(layer));
824 }).pipe(Effect.provide(SqlitePersistence.layerMemory)),
825 );
apps/server/src/scheduledTasks/ScheduledTaskService.ts
+8 -4
@@ -638,6 +638,8 @@ export const layer = Layer.effect(
638 requireExisting: boolean,
639 /** The schedule this save read, when it left the schedule unchanged. */
640 keptSchedule: ScheduledTask["schedule"] | null,
641 + /** The thread this save unbinds; any other stored thread is kept. */
642 + unboundThreadId: ThreadId | null,
643 webhook: {
644 readonly token: string | null;
645 readonly secret: string | null;
@@ -703,12 +705,12 @@ export const layer = Layer.effect(
705 enabled = excluded.enabled,
706 schedule_json = excluded.schedule_json,
707 project_id = excluded.project_id,
706 - -- A first run may bind the thread while this save is in flight; only a
707 - -- switch to new threads unbinds.
708 + -- A first run may bind the thread while this save is in flight, so a save
709 + -- without a thread keeps any thread but the one it unbinds.
710 thread_id = CASE
711 WHEN excluded.thread_mode = 'new_thread' THEN NULL
712 WHEN excluded.project_id != scheduled_tasks.project_id THEN excluded.thread_id
711 - ELSE COALESCE(excluded.thread_id, scheduled_tasks.thread_id)
713 + ELSE COALESCE(excluded.thread_id, NULLIF(scheduled_tasks.thread_id, ${unboundThreadId}))
714 END,
715 thread_mode = excluded.thread_mode,
716 workspace_strategy_json = excluded.workspace_strategy_json,
@@ -1478,7 +1480,8 @@ export const layer = Layer.effect(
1480 // of silently resetting an existing row.
1481 const existingTask = yield* findTask(id);
1482 const threadMode = resolveThreadMode(input, existingTask);
1481 - const keptThreadId = input.threadId ?? existingTask?.threadId ?? null;
1483 + const keptThreadId =
1484 + input.threadId ?? (input.unbindThread === true ? null : (existingTask?.threadId ?? null));
1485 // A bound thread belongs to its project, so moving the task leaves it for a new one.
1486 const movedOffThread =
1487 existingTask !== null &&
@@ -1610,6 +1613,7 @@ export const layer = Layer.effect(
1613 task,
1614 input.requireExisting === true,
1615 scheduleUnchanged ? existingTask.schedule : null,
1616 + threadId === null && input.unbindThread === true ? (existingTask?.threadId ?? null) : null,
1617 webhook,
1618 );
1619 yield* notifyChanged;
apps/web/src/components/settings/ScheduledTasksSettings.tsx
+2
@@ -194,6 +194,7 @@ const EMPTY_DRAFT: DraftState = {
194 weekdays: new Set([1, 2, 3, 4, 5]),
195 projectId: "",
196 threadId: "",
197 + savedThreadId: "",
198 threadMode: "new_thread",
199 workspaceMode: "worktree",
200 baseRef: "",
@@ -1679,6 +1680,7 @@ function ScheduledTaskEditorForm({
1680 schedule,
1681 projectId: selectedProjectId as ProjectId,
1682 threadId: draft.threadId ? (draft.threadId as ThreadId) : null,
1683 + ...(draft.threadId === "" && draft.savedThreadId !== "" ? { unbindThread: true } : {}),
1684 ...(offersThreadModes ? { threadMode: draft.threadMode } : {}),
1685 workspaceStrategy,
1686 modelSelection,
apps/web/src/components/settings/scheduledTasksSettings.logic.ts
+3
@@ -129,6 +129,8 @@ export interface DraftState {
129 readonly weekdays: ReadonlySet<number>;
130 readonly projectId: string;
131 readonly threadId: string;
132 + /** The thread the form opened with, so a save can tell a cleared destination from an unbound one. */
133 + readonly savedThreadId: string;
134 readonly threadMode: ScheduledTaskThreadMode;
135 readonly workspaceMode: WorkspaceMode;
136 readonly baseRef: string;
@@ -330,6 +332,7 @@ export function taskToDraft(task: ScheduledTask): DraftState {
332 weekdays,
333 projectId: task.projectId,
334 threadId: task.threadId ?? "",
335 + savedThreadId: task.threadId ?? "",
336 threadMode: scheduledTaskThreadMode(task),
337 workspaceMode: task.workspaceStrategy.type,
338 baseRef: task.workspaceStrategy.type === "worktree" ? task.workspaceStrategy.baseRef : "",
packages/contracts/src/scheduledTask.ts
+3
@@ -287,7 +287,10 @@ export const ScheduledTaskUpsertInput = Schema.Struct({
287 enabled: Schema.Boolean,
288 schedule: ScheduledTaskUpsertSchedule,
289 projectId: ProjectId,
290 + /** Omitted or null: keep the stored thread, which a first run may have bound since the form opened. */
291 threadId: Schema.optional(Schema.NullOr(ThreadId)),
292 + /** True with no `threadId`: the user cleared the thread, so the next run starts one. */
293 + unbindThread: Schema.optional(Schema.Boolean),
294 /** Omitted: keep the stored mode, or derive it from `threadId` for a new or rebound task. */
295 threadMode: Schema.optional(ScheduledTaskThreadMode),
296 workspaceStrategy: OrchestrationV2ThreadLaunchWorkspaceStrategy,