|
1 | 1 | import type { |
2 | 2 | ModelSelection, |
| 3 | + OrchestrationV2ThreadLaunchWorkspaceStrategy, |
3 | 4 | ServerConfig, |
4 | 5 | ProjectId, |
5 | 6 | RuntimeMode, |
6 | 7 | ScheduledTask, |
| 8 | + ScheduledTaskUpdateInput, |
7 | 9 | ScheduledTaskUpsertSchedule, |
8 | 10 | } from "@t3tools/contracts"; |
9 | 11 |
|
10 | | -import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts"; |
| 12 | +import { DEFAULT_SERVER_SETTINGS, MIN_SCHEDULED_TASK_INTERVAL_MS } from "@t3tools/contracts"; |
11 | 13 | import { |
12 | 14 | resolveProjectSettings, |
13 | 15 | type LegacyProjectSettingsFields, |
@@ -179,3 +181,134 @@ export function editDraft(task: ScheduledTask): ScheduledTaskDraft { |
179 | 181 | runtimeMode: task.runtimeMode, |
180 | 182 | }; |
181 | 183 | } |
| 184 | + |
| 185 | +function sameSchedule(a: ScheduledTaskUpsertSchedule, b: ScheduledTaskUpsertSchedule): boolean { |
| 186 | + if (a.type === "interval") { |
| 187 | + return b.type === "interval" && a.everyMs === b.everyMs; |
| 188 | + } |
| 189 | + if (b.type !== "fixed_time") return false; |
| 190 | + const key = (weekdays: ReadonlyArray<number> | undefined) => { |
| 191 | + const unique = [...new Set(weekdays ?? [])].sort((x, y) => x - y); |
| 192 | + return unique.length === 0 || unique.length === 7 ? "daily" : unique.join(","); |
| 193 | + }; |
| 194 | + return a.timeOfDay === b.timeOfDay && key(a.weekdays) === key(b.weekdays); |
| 195 | +} |
| 196 | + |
| 197 | +function sameModelSelection(a: ModelSelection | null, b: ModelSelection | null): boolean { |
| 198 | + if (a === null || b === null) return a === b; |
| 199 | + const key = (selection: ModelSelection) => |
| 200 | + JSON.stringify([ |
| 201 | + selection.instanceId, |
| 202 | + selection.model, |
| 203 | + [...(selection.options ?? [])] |
| 204 | + .sort((x, y) => x.id.localeCompare(y.id)) |
| 205 | + .map((option) => [option.id, option.value]), |
| 206 | + ]); |
| 207 | + return key(a) === key(b); |
| 208 | +} |
| 209 | + |
| 210 | +function workspaceStrategyFromDraft( |
| 211 | + draft: ScheduledTaskDraft, |
| 212 | +): OrchestrationV2ThreadLaunchWorkspaceStrategy { |
| 213 | + if (draft.workspace === "root") return { type: "root" }; |
| 214 | + if (draft.workspace === "existing_worktree") { |
| 215 | + return { type: "existing_worktree", worktreePath: draft.checkoutPath.trim() }; |
| 216 | + } |
| 217 | + return { |
| 218 | + type: "worktree", |
| 219 | + baseRef: draft.baseRef.trim() || "main", |
| 220 | + startFromOrigin: draft.startFromOrigin, |
| 221 | + }; |
| 222 | +} |
| 223 | + |
| 224 | +/** |
| 225 | + * Dirty-field patch for an existing task, scoped to its live project. The |
| 226 | + * baseline is the task snapshot the editor opened with (`draft.task`), so a |
| 227 | + * concurrent commit landing mid-session is never read as a change the user |
| 228 | + * made — and `projectId` tracks the live row, so a save still targets the |
| 229 | + * task wherever it currently lives. Returns null when nothing changed. |
| 230 | + */ |
| 231 | +export function buildScheduledTaskUpdateInput( |
| 232 | + draft: ScheduledTaskDraft, |
| 233 | + liveTask: ScheduledTask, |
| 234 | +): ScheduledTaskUpdateInput | null { |
| 235 | + const opening = draft.task; |
| 236 | + if (opening === null) return null; |
| 237 | + const baseline = editDraft(opening); |
| 238 | + const patch: { |
| 239 | + -readonly [ |
| 240 | + K in Exclude<keyof ScheduledTaskUpdateInput, "id" | "projectId"> |
| 241 | + ]?: ScheduledTaskUpdateInput[K]; |
| 242 | + } = {}; |
| 243 | + const title = draft.title.trim(); |
| 244 | + if (title !== baseline.title.trim()) patch.title = title; |
| 245 | + const prompt = draft.prompt.trim(); |
| 246 | + if (prompt !== baseline.prompt.trim()) patch.prompt = prompt; |
| 247 | + if (draft.enabled !== baseline.enabled) patch.enabled = draft.enabled; |
| 248 | + const schedule = scheduleFromDraft(draft.schedule); |
| 249 | + const baselineSchedule = scheduleFromDraft(baseline.schedule); |
| 250 | + if ( |
| 251 | + schedule !== null && |
| 252 | + baselineSchedule !== null && |
| 253 | + (!sameSchedule(schedule, baselineSchedule) || |
| 254 | + // scheduleDraftForTask clamps a legacy sub-minute interval to one |
| 255 | + // minute, so the baseline diff cannot see the normalization the |
| 256 | + // editor promises. Emit the normalized schedule while the live task |
| 257 | + // still carries it — a concurrent write to a valid interval clears |
| 258 | + // this condition instead of being overwritten. |
| 259 | + (liveTask.schedule.type === "interval" && |
| 260 | + liveTask.schedule.everyMs < MIN_SCHEDULED_TASK_INTERVAL_MS)) |
| 261 | + ) { |
| 262 | + patch.schedule = schedule; |
| 263 | + } |
| 264 | + if (draft.runtimeMode !== baseline.runtimeMode) patch.runtimeMode = draft.runtimeMode; |
| 265 | + if ( |
| 266 | + draft.modelSelection !== null && |
| 267 | + !sameModelSelection(draft.modelSelection, baseline.modelSelection) |
| 268 | + ) { |
| 269 | + patch.modelSelection = draft.modelSelection; |
| 270 | + } |
| 271 | + const workspaceStrategy = workspaceStrategyFromDraft(draft); |
| 272 | + const baselineStrategy = workspaceStrategyFromDraft(baseline); |
| 273 | + if (JSON.stringify(workspaceStrategy) !== JSON.stringify(baselineStrategy)) { |
| 274 | + if (workspaceStrategy.type !== baselineStrategy.type) { |
| 275 | + patch.workspaceStrategy = workspaceStrategy; |
| 276 | + } else if ( |
| 277 | + workspaceStrategy.type === "worktree" && |
| 278 | + baselineStrategy.type === "worktree" && |
| 279 | + liveTask.workspaceStrategy.type === "worktree" |
| 280 | + ) { |
| 281 | + // Send only the controls the user changed: the server merges the |
| 282 | + // sparse patch into the live strategy inside the update transaction, |
| 283 | + // so a concurrent edit to an untouched control — or to a field the |
| 284 | + // editor cannot express (`branch`) — survives on both sides. |
| 285 | + patch.workspaceStrategyPatch = { |
| 286 | + ...(workspaceStrategy.baseRef !== baselineStrategy.baseRef |
| 287 | + ? { baseRef: workspaceStrategy.baseRef } |
| 288 | + : {}), |
| 289 | + ...(workspaceStrategy.startFromOrigin !== baselineStrategy.startFromOrigin |
| 290 | + ? { startFromOrigin: workspaceStrategy.startFromOrigin } |
| 291 | + : {}), |
| 292 | + }; |
| 293 | + } else if ( |
| 294 | + workspaceStrategy.type === "existing_worktree" && |
| 295 | + baselineStrategy.type === "existing_worktree" && |
| 296 | + liveTask.workspaceStrategy.type === "existing_worktree" |
| 297 | + ) { |
| 298 | + patch.workspaceStrategyPatch = { worktreePath: workspaceStrategy.worktreePath }; |
| 299 | + } |
| 300 | + // A live kind the draft no longer matches means a concurrent editor |
| 301 | + // switched it — drop the stale control edit rather than revert that. |
| 302 | + } |
| 303 | + if (draft.projectId !== null && draft.projectId !== baseline.projectId) { |
| 304 | + patch.nextProjectId = draft.projectId; |
| 305 | + // A binding cannot follow a real re-home — the thread stays in the old |
| 306 | + // project — so a patch that moves the task detaches it; a stale save |
| 307 | + // landing where the task already lives keeps a concurrent rebind. |
| 308 | + if (draft.projectId !== liveTask.projectId && liveTask.threadId !== null) { |
| 309 | + patch.threadId = null; |
| 310 | + } |
| 311 | + } |
| 312 | + if (Object.keys(patch).length === 0) return null; |
| 313 | + return { id: liveTask.id, projectId: liveTask.projectId, ...patch }; |
| 314 | +} |
0 commit comments