feat: scheduled tasks can run in one thread with a fresh session each run #30326
t3/fresh-session-clear fromrefs/pull/30326/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
juliusmarminge removed labelsvouch:trusted
juliusmarminge added labelsagent-context-increase
- apps/server/src/scheduledTasks/ScheduledTaskService.ts line 829OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -795,3 +822,8 @@795822 ),796823 );797824825+ // The run already started the thread, so a failed write only costs the binding.826+ const bindThread = (id: ScheduledTaskId, threadId: ThreadId) =>827+ sql`828+ UPDATE scheduled_tasks SET thread_id = ${threadId}829+ WHERE task_id = ${id} AND thread_id IS NULL AND thread_mode != 'new_thread'
- apps/server/src/mcp/OrchestratorMcpService.ts lines 2115 to 2123OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -2088,3 +2095,13 @@20882095 schedule: input.schedule ?? existing.schedule,20892096 projectId: existing.projectId,20902097 threadId,2098+ threadMode:2099+ input.freshSession === undefined && input.bindToCurrentThread === undefined2100+ ? scheduledTaskThreadMode(existing)2101+ : (input.freshSession ??2102+ (input.bindToCurrentThread !== false &&2103+ scheduledTaskThreadMode(existing) === "fresh_session"))2104+ ? "fresh_session"2105+ : threadId === null2106+ ? "new_thread"2107+ : "continue",apps/server/src/scheduledTasks/ScheduledTaskService.ts lines 828 to 829OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -795,3 +822,8 @@795822 ),796823 );797824825+ // The run already started the thread, so a failed write only costs the binding.826+ const bindThread = (id: ScheduledTaskId, threadId: ThreadId) =>827+ sql`828+ UPDATE scheduled_tasks SET thread_id = ${threadId}829+ WHERE task_id = ${id} AND thread_id IS NULL AND thread_mode != 'new_thread'
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
- apps/server/src/scheduledTasks/ScheduledTaskService.ts line 204OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -189,3 +191,14 @@189191 const encodeHeadersJson = Schema.encodeSync(HeadersJson);190192 const encodeMissingFieldsJson = Schema.encodeSync(MissingFieldsJson);191193194+/**195+ * Clients that predate thread modes send none. Clearing the thread of a bound task unbinds it, as196+ * before; an edit that leaves the binding alone keeps the stored mode. Current clients send it.197+ */198+function resolveThreadMode(199+ input: Pick<ScheduledTaskUpsertInput, "threadId" | "threadMode">,200+ existing: Pick<ScheduledTask, "threadId" | "threadMode"> | null,201+): ScheduledTaskThreadMode {202+ if (input.threadMode !== undefined) return input.threadMode;203+ const stored = existing === null ? "new_thread" : scheduledTaskThreadMode(existing);204+ if (input.threadId == null) return existing?.threadId == null ? stored : "new_thread";
- @@ -189,3 +191,14 @@189191 const encodeHeadersJson = Schema.encodeSync(HeadersJson);190192 const encodeMissingFieldsJson = Schema.encodeSync(MissingFieldsJson);191193194+/**195+ * Clients that predate thread modes send none. Clearing the thread of a bound task unbinds it, as196+ * before; an edit that leaves the binding alone keeps the stored mode. Current clients send it.197+ */198+function resolveThreadMode(199+ input: Pick<ScheduledTaskUpsertInput, "threadId" | "threadMode">,200+ existing: Pick<ScheduledTask, "threadId" | "threadMode"> | null,201+): ScheduledTaskThreadMode {202+ if (input.threadMode !== undefined) return input.threadMode;203+ const stored = existing === null ? "new_thread" : scheduledTaskThreadMode(existing);204+ if (input.threadId == null) return existing?.threadId == null ? stored : "new_thread";
- apps/server/src/scheduledTasks/ScheduledTaskService.ts line 1376OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1324,4 +1372,5 @@13241372 enabled: input.enabled,13251373 schedule,13261374 projectId: input.projectId,1327- threadId: input.threadId ?? null,1375+ threadId:1376+ threadMode === "new_thread" ? null : (input.threadId ?? existingTask?.threadId ?? null),
- apps/server/src/mcp/OrchestratorMcpService.ts lines 2115 to 2118OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -2088,3 +2095,10 @@20882095 schedule: input.schedule ?? existing.schedule,20892096 projectId: existing.projectId,20902097 threadId,2098+ threadMode:2099+ input.freshSession === undefined && input.bindToCurrentThread === undefined2100+ ? scheduledTaskThreadMode(existing)2101+ : (input.freshSession ??2102+ (input.bindToCurrentThread !== false &&2103+ scheduledTaskThreadMode(existing) === "fresh_session"))2104+ ? "fresh_session"
- apps/server/src/scheduledTasks/ScheduledTaskService.ts line 1278OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1236,3 +1273,6 @@12361273 // keep their run history, and so real load failures propagate instead12371274 // of silently resetting an existing row.12381275 const existingTask = yield* findTask(id);1276+ const threadMode = resolveThreadMode(input, existingTask);1277+ const threadId =1278+ threadMode === "new_thread" ? null : (input.threadId ?? existingTask?.threadId ?? null);
- @@ -2069,3 +2076,9 @@20692076 : "bindToCurrentThread binds to this thread, which belongs to a different project.",20702077 );20712078 }2079+ // A save keeps a bound thread unless the task goes back to new threads, so this pair2080+ // would leave runs in the old thread.2081+ if (2082+ input.bindToCurrentThread === false &&2083+ input.freshSession === true &&2084+ existing.threadId !== null
- apps/server/src/scheduledTasks/ScheduledTaskService.ts line 828OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -795,3 +822,7 @@795822 ),796823 );797824825+ // The run already started the thread, so a failed write only costs the binding.826+ const bindThread = (task: ScheduledTask, threadId: ThreadId) =>827+ sql`828+ UPDATE scheduled_tasks SET thread_id = ${threadId}
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/scheduledTasks/ScheduledTaskService.ts line 830OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -795,3 +823,8 @@795823 ),796824 );797825826+ // The run already started the thread, so a failed write only costs the binding.827+ const bindThread = (task: ScheduledTask, threadId: ThreadId) =>828+ sql`829+ UPDATE scheduled_tasks SET thread_id = ${threadId}830+ WHERE task_id = ${task.id} AND created_at = ${task.createdAt}
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:L
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XXL
- apps/server/src/scheduledTasks/ScheduledTaskService.ts line 829Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -798,3 +826,4 @@798826 ),799827 );800828829+ // The run already started the thread, so a failed write only costs the binding.
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:XXL
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
juliusmarminge merged this4aab23c
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed