From 6753f42b8cb0a0fa54d93ff6540984b5fc45f464 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Tue, 28 Jul 2026 12:23:17 +0200 Subject: [PATCH] fix(orchestration): preserve legacy schedule compatibility Enforce the one-minute floor only on writes, clamp legacy execution intervals, and keep queued work protected from archive while reusing shared active-run derivation. Co-authored-by: codex --- .../src/features/home/useThreadListActions.ts | 8 ++- apps/mobile/src/state/use-thread-selection.ts | 41 +++++++---- .../src/scheduledTasks/Schedule.test.ts | 8 +++ apps/server/src/scheduledTasks/Schedule.ts | 8 ++- packages/contracts/src/orchestratorMcp.ts | 20 +++--- packages/contracts/src/scheduledTask.test.ts | 25 ++++++- packages/contracts/src/scheduledTask.ts | 68 +++++++++++++------ 7 files changed, 127 insertions(+), 51 deletions(-) diff --git a/apps/mobile/src/features/home/useThreadListActions.ts b/apps/mobile/src/features/home/useThreadListActions.ts index 5746509b9c..0f9a5f9247 100644 --- a/apps/mobile/src/features/home/useThreadListActions.ts +++ b/apps/mobile/src/features/home/useThreadListActions.ts @@ -1,5 +1,7 @@ -import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; -import { threadRuntimeHasInterruptibleRun } from "@t3tools/client-runtime/state/thread-execution"; +import { + threadRuntimeIsActive, + type EnvironmentThreadShell, +} from "@t3tools/client-runtime/state/shell"; import { canSettle } from "@t3tools/client-runtime/state/thread-settled"; import * as Cause from "effect/Cause"; import * as Haptics from "expo-haptics"; @@ -110,7 +112,7 @@ function useThreadActionExecutor( } // Archive keeps its original, narrower guard: never interrupt a // thread mid-turn. - if (action === "archive" && threadRuntimeHasInterruptibleRun(thread.runtime)) { + if (action === "archive" && threadRuntimeIsActive(thread.runtime)) { Alert.alert( actionFailureTitle(action), "This thread is working. Interrupt it first, then try again.", diff --git a/apps/mobile/src/state/use-thread-selection.ts b/apps/mobile/src/state/use-thread-selection.ts index d4c955ea31..c1fa82b3c3 100644 --- a/apps/mobile/src/state/use-thread-selection.ts +++ b/apps/mobile/src/state/use-thread-selection.ts @@ -7,9 +7,15 @@ import { type ScopedProjectRef, type ScopedThreadRef, } from "@t3tools/contracts"; -import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; +import { + presentThreadShell, + type EnvironmentThreadShell, +} from "@t3tools/client-runtime/state/shell"; +import { + deriveLatestThreadRun, + deriveThreadRuntime, +} from "@t3tools/client-runtime/state/thread-execution"; import * as Option from "effect/Option"; -import { copySorted } from "@t3tools/shared/Array"; import { useProject, useThreadShell } from "../state/entities"; import { useEnvironmentThread } from "../state/threads"; @@ -46,17 +52,8 @@ function threadDetailToShell( thread: OrchestrationThread, ): EnvironmentThreadShell { const thread = projection.thread; - const runsByOrdinal = copySorted(projection.runs, (left, right) => right.ordinal - left.ordinal); - const latestRun = runsByOrdinal[0] ?? null; - const activeRun = - runsByOrdinal.find( - (run) => - run.status === "preparing" || - run.status === "queued" || - run.status === "starting" || - run.status === "running" || - run.status === "waiting", - ) ?? null; + const latestRun = deriveLatestThreadRun(projection); + const runtime = deriveThreadRuntime(projection); const pendingRequest = projection.runtimeRequests.find((request) => request.status === "pending") ?? null; return presentThreadShell(environmentId, { @@ -68,7 +65,23 @@ function threadDetailToShell( interactionMode: thread.interactionMode, branch: thread.branch, worktreePath: thread.worktreePath, - latestTurn: thread.latestTurn, + activeProviderThreadId: thread.activeProviderThreadId, + lineage: thread.lineage, + forkedFrom: thread.forkedFrom, + createdBy: thread.createdBy, + creationSource: thread.creationSource, + latestRunId: latestRun?.runId ?? null, + activeRunId: runtime?.activeRunId ?? null, + status: runtime?.status ?? "idle", + pendingRuntimeRequest: + pendingRequest === null + ? null + : { id: pendingRequest.id, kind: pendingRequest.kind, createdAt: pendingRequest.createdAt }, + latestVisibleMessage: null, + latestUserMessageAt: latestUserMessageAt(projection), + hasActionableProposedPlan: false, + itemCount: projection.turnItems.length, + visibleItemCount: projection.visibleTurnItems.length, createdAt: thread.createdAt, updatedAt: thread.updatedAt, archivedAt: thread.archivedAt, diff --git a/apps/server/src/scheduledTasks/Schedule.test.ts b/apps/server/src/scheduledTasks/Schedule.test.ts index 1138301bc4..aa369975f2 100644 --- a/apps/server/src/scheduledTasks/Schedule.test.ts +++ b/apps/server/src/scheduledTasks/Schedule.test.ts @@ -23,6 +23,14 @@ describe("scheduled task schedule calculation", () => { expect(next ? DateTime.formatIso(DateTime.toUtc(next)) : null).toBe("2026-07-01T16:05:00.000Z"); }); + it("clamps legacy sub-minute intervals to the one-minute execution floor", () => { + const next = nextScheduledRunAt( + { type: "interval", everyMs: 1_000 }, + DateTime.makeUnsafe("2026-07-01T16:00:00.000Z"), + ); + expect(next ? DateTime.formatIso(DateTime.toUtc(next)) : null).toBe("2026-07-01T16:01:00.000Z"); + }); + it("skips to the next matching fixed-time weekday", () => { const next = nextScheduledRunAt( { type: "fixed_time", timeOfDay: "09:00", weekdays: [1, 2, 3, 4, 5] }, diff --git a/apps/server/src/scheduledTasks/Schedule.ts b/apps/server/src/scheduledTasks/Schedule.ts index e0ed26400f..952c7e6e8f 100644 --- a/apps/server/src/scheduledTasks/Schedule.ts +++ b/apps/server/src/scheduledTasks/Schedule.ts @@ -1,4 +1,4 @@ -import type { ScheduledTaskSchedule } from "@t3tools/contracts"; +import { MIN_SCHEDULED_TASK_INTERVAL_MS, type ScheduledTaskSchedule } from "@t3tools/contracts"; import * as DateTime from "effect/DateTime"; const MINUTE_MS = 60_000; @@ -14,7 +14,11 @@ export function nextScheduledRunAt( from: DateTime.DateTime, ): DateTime.DateTime | null { if (schedule.type === "interval") { - return DateTime.add(from, { milliseconds: schedule.everyMs }); + // Persisted rows created before the one-minute floor remain readable, but + // they must not retain their old high-frequency execution rate. + return DateTime.add(from, { + milliseconds: Math.max(schedule.everyMs, MIN_SCHEDULED_TASK_INTERVAL_MS), + }); } const time = parseTimeOfDay(schedule.timeOfDay); diff --git a/packages/contracts/src/orchestratorMcp.ts b/packages/contracts/src/orchestratorMcp.ts index 202601d7fb..7e62e9351d 100644 --- a/packages/contracts/src/orchestratorMcp.ts +++ b/packages/contracts/src/orchestratorMcp.ts @@ -16,7 +16,11 @@ import { TrimmedNonEmptyString, TurnItemId, } from "./baseSchemas.ts"; -import { ScheduledTaskRunStatus, ScheduledTaskSchedule } from "./scheduledTask.ts"; +import { + ScheduledTaskRunStatus, + ScheduledTaskSchedule, + ScheduledTaskUpsertSchedule, +} from "./scheduledTask.ts"; import { ProviderInteractionMode, RuntimeMode } from "./providerPolicy.ts"; import { OrchestrationV2Actor, @@ -46,15 +50,15 @@ const OrchestratorMcpClientRequestId = TrimmedNonEmptyString.check( * strings. Keep the structured object as the documented form while accepting * that compatibility shape at the tool boundary and decoding it immediately. */ -const OrchestratorMcpScheduleFromJsonString = Schema.fromJsonString(ScheduledTaskSchedule).annotate( - { - description: - "Compatibility-only JSON encoding of the schedule object. Prefer a structured object.", - }, -); +const OrchestratorMcpScheduleFromJsonString = Schema.fromJsonString( + ScheduledTaskUpsertSchedule, +).annotate({ + description: + "Compatibility-only JSON encoding of the schedule object. Prefer a structured object.", +}); const OrchestratorMcpSchedule = Schema.Union([ - ScheduledTaskSchedule, + ScheduledTaskUpsertSchedule, OrchestratorMcpScheduleFromJsonString, ]).annotate({ description: diff --git a/packages/contracts/src/scheduledTask.test.ts b/packages/contracts/src/scheduledTask.test.ts index be136944b4..fa79c912c1 100644 --- a/packages/contracts/src/scheduledTask.test.ts +++ b/packages/contracts/src/scheduledTask.test.ts @@ -1,14 +1,33 @@ import { describe, expect, it } from "vite-plus/test"; import * as Schema from "effect/Schema"; -import { MIN_SCHEDULED_TASK_INTERVAL_MS, ScheduledTaskSchedule } from "./scheduledTask.ts"; +import { + MIN_SCHEDULED_TASK_INTERVAL_MS, + ScheduledTaskSchedule, + ScheduledTaskUpsertSchedule, +} from "./scheduledTask.ts"; const decodeSchedule = Schema.decodeUnknownSync(ScheduledTaskSchedule); +const decodeUpsertSchedule = Schema.decodeUnknownSync(ScheduledTaskUpsertSchedule); describe("ScheduledTaskSchedule", () => { - it("accepts interval schedules at the one-minute minimum", () => { + it("keeps legacy sub-minute persisted schedules readable", () => { expect( decodeSchedule({ + type: "interval", + everyMs: MIN_SCHEDULED_TASK_INTERVAL_MS - 1, + }), + ).toEqual({ + type: "interval", + everyMs: MIN_SCHEDULED_TASK_INTERVAL_MS - 1, + }); + }); +}); + +describe("ScheduledTaskUpsertSchedule", () => { + it("accepts interval schedules at the one-minute minimum", () => { + expect( + decodeUpsertSchedule({ type: "interval", everyMs: MIN_SCHEDULED_TASK_INTERVAL_MS, }), @@ -20,7 +39,7 @@ describe("ScheduledTaskSchedule", () => { it("rejects interval schedules more frequent than once per minute", () => { expect(() => - decodeSchedule({ + decodeUpsertSchedule({ type: "interval", everyMs: MIN_SCHEDULED_TASK_INTERVAL_MS - 1, }), diff --git a/packages/contracts/src/scheduledTask.ts b/packages/contracts/src/scheduledTask.ts index 311b55c32f..958ca6e1a3 100644 --- a/packages/contracts/src/scheduledTask.ts +++ b/packages/contracts/src/scheduledTask.ts @@ -23,7 +23,50 @@ const TimeOfDay = TrimmedNonEmptyString.check( export const MIN_SCHEDULED_TASK_INTERVAL_MS = 60_000; +const ScheduledTaskIntervalSchedule = Schema.Struct({ + type: Schema.Literal("interval").annotate({ + description: "Select interval scheduling.", + }), + everyMs: Schema.Int.annotate({ + description: "Interval in milliseconds.", + }), +}).annotate({ + description: "Run repeatedly after a fixed number of milliseconds.", +}); + +const ScheduledTaskFixedTimeSchedule = Schema.Struct({ + type: Schema.Literal("fixed_time").annotate({ + description: "Select a fixed local wall-clock time.", + }), + timeOfDay: TimeOfDay, + weekdays: Schema.optional( + Schema.Array( + Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: 6 })).annotate({ + description: "Weekday number where 0 is Sunday and 6 is Saturday.", + }), + ).annotate({ + description: "Optional weekdays; omit to run every day.", + }), + ), +}).annotate({ + description: "Run at a fixed local wall-clock time on selected weekdays.", +}); + +/** + * Read model for persisted schedules. Keep accepting legacy sub-minute rows so + * users can list, disable, edit, or delete them after the write minimum changes. + */ export const ScheduledTaskSchedule = Schema.Union([ + ScheduledTaskIntervalSchedule, + ScheduledTaskFixedTimeSchedule, +]).annotate({ + description: + "Structured recurring schedule. Pass an object with type 'interval' or 'fixed_time'.", +}); +export type ScheduledTaskSchedule = typeof ScheduledTaskSchedule.Type; + +/** Mutation model: newly created or updated interval schedules run at most once per minute. */ +export const ScheduledTaskUpsertSchedule = Schema.Union([ Schema.Struct({ type: Schema.Literal("interval").annotate({ description: "Select interval scheduling.", @@ -36,28 +79,11 @@ export const ScheduledTaskSchedule = Schema.Union([ }).annotate({ description: "Run repeatedly after a fixed number of milliseconds.", }), - Schema.Struct({ - type: Schema.Literal("fixed_time").annotate({ - description: "Select a fixed local wall-clock time.", - }), - timeOfDay: TimeOfDay, - weekdays: Schema.optional( - Schema.Array( - Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: 6 })).annotate({ - description: "Weekday number where 0 is Sunday and 6 is Saturday.", - }), - ).annotate({ - description: "Optional weekdays; omit to run every day.", - }), - ), - }).annotate({ - description: "Run at a fixed local wall-clock time on selected weekdays.", - }), + ScheduledTaskFixedTimeSchedule, ]).annotate({ - description: - "Structured recurring schedule. Pass an object with type 'interval' or 'fixed_time'.", + description: "Writable recurring schedule. Pass an object with type 'interval' or 'fixed_time'.", }); -export type ScheduledTaskSchedule = typeof ScheduledTaskSchedule.Type; +export type ScheduledTaskUpsertSchedule = typeof ScheduledTaskUpsertSchedule.Type; export const ScheduledTaskRunStatus = Schema.Literals(["never", "running", "succeeded", "failed"]); export type ScheduledTaskRunStatus = typeof ScheduledTaskRunStatus.Type; @@ -100,7 +126,7 @@ export const ScheduledTaskUpsertInput = Schema.Struct({ title: TrimmedNonEmptyString, prompt: TrimmedNonEmptyString, enabled: Schema.Boolean, - schedule: ScheduledTaskSchedule, + schedule: ScheduledTaskUpsertSchedule, projectId: ProjectId, threadId: Schema.optional(Schema.NullOr(ThreadId)), workspaceStrategy: OrchestrationV2ThreadLaunchWorkspaceStrategy,