fix(orchestration): harden scheduled task startup

Reject scheduled task intervals below one minute and tear down the effect worker when awareness relay startup fails.

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex committed 2026-07-28 11:34:04 +02:00
1 parent 44d48fd7df
commit 062f2753a3
4 files changed
+86 -7

No files matched your search

@@ -1,6 +1,7 @@
import { assert, it } from "@effect/vitest";
import { DEFAULT_MODEL, ProviderInstanceId } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as Ref from "effect/Ref";
@@ -57,6 +58,27 @@ it.effect("does not rebuild valid projections", () =>
}),
);
it.effect("interrupts the effect worker when awareness relay startup fails", () =>
Effect.scoped(
Effect.gen(function* () {
const workerInterrupted = yield* Ref.make(false);
const workerFiberRef = yield* Ref.make<Fiber.Fiber<void, never> | null>(null);
const exit = yield* ServerRuntimeStartup.startEffectWorkerWithRelay({
runWorker: Effect.never.pipe(Effect.ensuring(Ref.set(workerInterrupted, true))),
startRelay: Effect.yieldNow.pipe(
Effect.andThen(Effect.die("awareness relay startup failed")),
),
workerFiberRef,
}).pipe(Effect.exit);
assert.isTrue(Exit.isFailure(exit));
assert.isTrue(yield* Ref.get(workerInterrupted));
assert.isNull(yield* Ref.get(workerFiberRef));
}),
),
);
it.effect("queues commands until startup signals readiness", () =>
Effect.scoped(
Effect.gen(function* () {
+29 -4
View File
@@ -286,6 +286,31 @@ const runStartupPhase = <A, E, R>(phase: string, effect: Effect.Effect<A, E, R>)
Effect.withSpan(`server.startup.${phase}`),
);
export const startEffectWorkerWithRelay = Effect.fn(
"ServerRuntimeStartup.startEffectWorkerWithRelay",
)(function* <WorkerContext, RelayContext>(input: {
readonly runWorker: Effect.Effect<void, never, WorkerContext>;
readonly startRelay: Effect.Effect<void, never, RelayContext>;
readonly workerFiberRef: Ref.Ref<Fiber.Fiber<void, never> | null>;
}) {
const workerFiber = yield* input.runWorker.pipe(Effect.forkScoped);
yield* Ref.set(input.workerFiberRef, workerFiber);
yield* input.startRelay.pipe(
Effect.onExit((exit) => {
if (Exit.isSuccess(exit)) {
return Effect.void;
}
return Ref.getAndSet(input.workerFiberRef, null).pipe(
Effect.flatMap((ownedWorkerFiber) =>
ownedWorkerFiber === null
? Effect.void
: Fiber.interrupt(ownedWorkerFiber).pipe(Effect.asVoid),
),
);
}),
);
});
export function runOrderedV2StartupPhases<
Import,
Verification extends { readonly valid: boolean },
@@ -446,10 +471,10 @@ export const make = Effect.gen(function* () {
recover: runStartupPhase("orchestration-v2.recovery", providerRuntimeRecovery.recover),
startEffectWorker: runStartupPhase(
"orchestration-v2.effect-worker.start",
Effect.gen(function* () {
const workerFiber = yield* EffectWorker.runDaemon.pipe(Effect.forkScoped);
yield* Ref.set(effectWorkerFiber, workerFiber);
yield* agentAwarenessRelay.start();
startEffectWorkerWithRelay({
runWorker: EffectWorker.runDaemon,
startRelay: agentAwarenessRelay.start(),
workerFiberRef: effectWorkerFiber,
}),
),
autoBootstrap: (serverConfig.autoBootstrapProjectFromCwd
@@ -0,0 +1,29 @@
import { describe, expect, it } from "vite-plus/test";
import * as Schema from "effect/Schema";
import { MIN_SCHEDULED_TASK_INTERVAL_MS, ScheduledTaskSchedule } from "./scheduledTask.ts";
const decodeSchedule = Schema.decodeUnknownSync(ScheduledTaskSchedule);
describe("ScheduledTaskSchedule", () => {
it("accepts interval schedules at the one-minute minimum", () => {
expect(
decodeSchedule({
type: "interval",
everyMs: MIN_SCHEDULED_TASK_INTERVAL_MS,
}),
).toEqual({
type: "interval",
everyMs: MIN_SCHEDULED_TASK_INTERVAL_MS,
});
});
it("rejects interval schedules more frequent than once per minute", () => {
expect(() =>
decodeSchedule({
type: "interval",
everyMs: MIN_SCHEDULED_TASK_INTERVAL_MS - 1,
}),
).toThrow();
});
});
+6 -3
View File
@@ -3,7 +3,6 @@ import * as Schema from "effect/Schema";
import {
CommandId,
IsoDateTime,
PositiveInt,
ProjectId,
ScheduledTaskId,
ThreadId,
@@ -22,13 +21,17 @@ const TimeOfDay = TrimmedNonEmptyString.check(
Schema.isPattern(/^([01]?\d|2[0-3]):([0-5]\d)$/),
).annotate({ description: "Local wall-clock time in 24-hour HH:MM form, such as 09:30." });
export const MIN_SCHEDULED_TASK_INTERVAL_MS = 60_000;
export const ScheduledTaskSchedule = Schema.Union([
Schema.Struct({
type: Schema.Literal("interval").annotate({
description: "Select interval scheduling.",
}),
everyMs: PositiveInt.annotate({
description: "Positive interval in milliseconds; 3600000 means every hour.",
everyMs: Schema.Int.check(
Schema.isGreaterThanOrEqualTo(MIN_SCHEDULED_TASK_INTERVAL_MS),
).annotate({
description: "Interval in milliseconds, with a minimum of 60000 (one minute).",
}),
}).annotate({
description: "Run repeatedly after a fixed number of milliseconds.",