Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .server-changes/warm-start-delivery-verification.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
area: supervisor
type: feature
---

Verify warm-start dispatches were acted on and cold-start the run within seconds when a dispatch is silently lost (opt-in via TRIGGER_WARM_START_VERIFY_ENABLED).
10 changes: 10 additions & 0 deletions apps/supervisor/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ const Env = z
TRIGGER_CHECKPOINT_URL: z.string().optional(),
TRIGGER_METADATA_URL: z.string().optional(),

// Warm-start delivery verification: after a warm-start hit, probe the
// platform and cold-start the run if no runner acted on the dispatch
TRIGGER_WARM_START_VERIFY_ENABLED: BoolEnv.default(false),
TRIGGER_WARM_START_VERIFY_DELAY_MS: z.coerce
.number()
.int()
.min(1_000)
.max(60_000)
.default(10_000),

// Used by the resource monitor
RESOURCE_MONITOR_ENABLED: BoolEnv.default(false),
RESOURCE_MONITOR_OVERRIDE_CPU_TOTAL: z.coerce.number().optional(),
Expand Down
141 changes: 94 additions & 47 deletions apps/supervisor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import { PodCleaner } from "./services/podCleaner.js";
import { FailedPodHandler } from "./services/failedPodHandler.js";
import { getWorkerToken } from "./workerToken.js";
import { OtlpTraceService } from "./services/otlpTraceService.js";
import {
WarmStartVerificationService,
type WarmStartTimings,
} from "./services/warmStartVerificationService.js";
import { extractTraceparent, getRestoreRunnerId } from "./util.js";
import { Redis } from "ioredis";
import { BackpressureMonitor } from "./backpressure/backpressureMonitor.js";
Expand Down Expand Up @@ -54,6 +58,7 @@ class ManagedSupervisor {
private readonly logger = new SimpleStructuredLogger("managed-supervisor");
private readonly resourceMonitor: ResourceMonitor;
private readonly checkpointClient?: CheckpointClient;
private readonly warmStartVerifier?: WarmStartVerificationService;

private readonly podCleaner?: PodCleaner;
private readonly failedPodHandler?: FailedPodHandler;
Expand Down Expand Up @@ -299,6 +304,19 @@ class ManagedSupervisor {
});
}

if (env.TRIGGER_WARM_START_VERIFY_ENABLED && this.warmStartUrl) {
this.logger.log("Warm-start delivery verification enabled", {
delayMs: env.TRIGGER_WARM_START_VERIFY_DELAY_MS,
});

this.warmStartVerifier = new WarmStartVerificationService({
workerClient: this.workerSession.httpClient,
delayMs: env.TRIGGER_WARM_START_VERIFY_DELAY_MS,
createWorkload: (message, timings) => this.createWorkload(message, timings),
wideEventOpts: this.wideEventOpts,
});
}

this.workerSession.on("runNotification", async ({ time, run }) => {
this.logger.verbose("runNotification", { time, run });

Expand Down Expand Up @@ -455,58 +473,24 @@ class ManagedSupervisor {
if (didWarmStart) {
setExtra(fromContext(), "path_taken", "warm_start");
this.logger.debug("Warm start successful", { runId: message.run.id });
return;
}

setExtra(fromContext(), "path_taken", "cold_create");

const createStart = performance.now();
try {
if (!message.deployment.friendlyId) {
// mostly a type guard, deployments always exists for deployed environments
// a proper fix would be to use a discriminated union schema to differentiate between dequeued runs in dev and in deployed environments.
throw new Error("Deployment is missing");
}

await this.workloadManager.create({
dequeuedAt: message.dequeuedAt,
// A hit only means the response was written to the long-poll
// socket, not that the runner received it. Schedule a delivery
// verification that cold-starts the run if nobody acts on it.
this.warmStartVerifier?.schedule(message, {
dequeueResponseMs,
pollingIntervalMs,
warmStartCheckMs,
envId: message.environment.id,
envType: message.environment.type,
image: message.image,
machine: message.run.machine,
orgId: message.organization.id,
projectId: message.project.id,
deploymentFriendlyId: message.deployment.friendlyId,
deploymentVersion: message.backgroundWorker.version,
runId: message.run.id,
runFriendlyId: message.run.friendlyId,
version: message.version,
nextAttemptNumber: message.run.attemptNumber,
snapshotId: message.snapshot.id,
snapshotFriendlyId: message.snapshot.friendlyId,
placementTags: message.placementTags,
traceContext: message.run.traceContext,
annotations: message.run.annotations,
hasPrivateLink: message.organization.hasPrivateLink,
});
recordPhaseSince("workload_create", createStart, undefined);

// Disabled for now
// this.resourceMonitor.blockResources({
// cpu: message.run.machine.cpu,
// memory: message.run.machine.memory,
// });
} catch (error) {
recordPhaseSince(
"workload_create",
createStart,
error instanceof Error ? error : new Error(String(error))
);
this.logger.error("Failed to create workload", { error });
return;
}

setExtra(fromContext(), "path_taken", "cold_create");

await this.createWorkload(message, {
dequeueResponseMs,
pollingIntervalMs,
warmStartCheckMs,
});
}
);
}
Expand Down Expand Up @@ -541,6 +525,8 @@ class ManagedSupervisor {

async onRunConnected({ run }: { run: { friendlyId: string } }) {
this.logger.debug("Run connected", { run });
// The dispatched run reached a runner on this node - no fallback needed.
this.warmStartVerifier?.cancel(run.friendlyId);
this.workerSession.subscribeToRunNotifications([run.friendlyId]);
}

Expand All @@ -549,6 +535,64 @@ class ManagedSupervisor {
this.workerSession.unsubscribeFromRunNotifications([run.friendlyId]);
}

private async createWorkload(message: DequeuedMessage, timings: WarmStartTimings) {
const createStart = performance.now();
try {
if (!message.deployment.friendlyId) {
// mostly a type guard, deployments always exists for deployed environments
// a proper fix would be to use a discriminated union schema to differentiate between dequeued runs in dev and in deployed environments.
throw new Error("Deployment is missing");
}

if (!message.image) {
// same type-guard situation as deployment above
throw new Error("Image is missing");
}

await this.workloadManager.create({
dequeuedAt: message.dequeuedAt,
dequeueResponseMs: timings.dequeueResponseMs,
pollingIntervalMs: timings.pollingIntervalMs,
warmStartCheckMs: timings.warmStartCheckMs,
envId: message.environment.id,
envType: message.environment.type,
image: message.image,
machine: message.run.machine,
orgId: message.organization.id,
projectId: message.project.id,
deploymentFriendlyId: message.deployment.friendlyId,
deploymentVersion: message.backgroundWorker.version,
runId: message.run.id,
runFriendlyId: message.run.friendlyId,
version: message.version,
nextAttemptNumber: message.run.attemptNumber,
snapshotId: message.snapshot.id,
snapshotFriendlyId: message.snapshot.friendlyId,
placementTags: message.placementTags,
traceContext: message.run.traceContext,
annotations: message.run.annotations,
hasPrivateLink: message.organization.hasPrivateLink,
});
recordPhaseSince("workload_create", createStart, undefined);

// Disabled for now
// this.resourceMonitor.blockResources({
// cpu: message.run.machine.cpu,
// memory: message.run.machine.memory,
// });
} catch (error) {
recordPhaseSince(
"workload_create",
createStart,
error instanceof Error ? error : new Error(String(error))
);
Comment on lines +576 to +588

@devin-ai-integration devin-ai-integration Bot Jun 12, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Info: recordPhaseSince is a silent no-op when called from the verification fallback path

When createWorkload is invoked from the verification service's timer callback (the fallback cold-create path), it runs outside any runWideEvent context. The recordPhaseSince calls at apps/supervisor/src/index.ts:576 and apps/supervisor/src/index.ts:584-588 use fromContext() which returns null in this case, making recordPhase a no-op (apps/supervisor/src/wideEvents/record.ts:27). This means the workload_create phase timing data is silently dropped for fallback cold-creates. The separate emitOneShot call with outcome "fallback" in the verification service (warmStartVerificationService.ts:140) does capture the event, so there is observability — just not at the same phase-level granularity as the normal dequeue path.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition you flagged holds: recordPhaseSince -> recordPhase guards with if (!state) return (apps/supervisor/src/wideEvents/record.ts:27), and fromContext() returns null outside the ALS scope (wideEvents/context.ts:12) - so from the verifier's timer path it's a clean no-op, no throw, no corrupted phase data. The fallback path's observability instead comes from the warmstart.verify wide event plus the runId-attributed error log in createWorkload's catch.

this.logger.error("Failed to create workload", {
runId: message.run.friendlyId,
error,
});
}
}

private async tryWarmStart(
dequeuedMessage: DequeuedMessage,
traceparent: string | undefined
Expand Down Expand Up @@ -630,6 +674,9 @@ class ManagedSupervisor {

async stop() {
this.logger.log("Shutting down");
// Stop the verifier first: its timer can otherwise fire mid-shutdown and
// cold-create a workload on a node that is going down.
this.warmStartVerifier?.stop();
await this.workloadServer.stop();
await this.workerSession.stop();

Expand Down
132 changes: 132 additions & 0 deletions apps/supervisor/src/services/warmStartVerificationService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { describe, expect, it, vi } from "vitest";
import { setTimeout as sleep } from "node:timers/promises";
import { WarmStartVerificationService } from "./warmStartVerificationService.js";
import type { DequeuedMessage } from "@trigger.dev/core/v3";
import type { SupervisorHttpClient } from "@trigger.dev/core/v3/workers";

// The TimerWheel ticks every 100ms, so a 1000ms delay (the env minimum)
// fires within ~1.1s.
const DELAY_MS = 1_000;
// Long enough that a pending verification would certainly have fired.
const SETTLE_MS = 1_600;

const DEQUEUED_SNAPSHOT_ID = "snapshot_dequeued";

function makeMessage(runFriendlyId = "run_1"): DequeuedMessage {
return {
run: { friendlyId: runFriendlyId },
snapshot: { friendlyId: DEQUEUED_SNAPSHOT_ID },
} as unknown as DequeuedMessage;
}

function createService(opts: {
latestSnapshotId?: string;
probeError?: boolean;
}) {
const getLatestSnapshot = vi.fn(async (_runId: string) =>
opts.probeError
? { success: false as const, error: "connection refused" }
: {
success: true as const,
data: { execution: { snapshot: { friendlyId: opts.latestSnapshotId } } },
}
);

const createWorkload = vi.fn(async () => {});

const service = new WarmStartVerificationService({
workerClient: { getLatestSnapshot } as unknown as SupervisorHttpClient,
delayMs: DELAY_MS,
createWorkload,
wideEventOpts: { service: "supervisor-test", env: {}, enabled: false },
});

return { service, getLatestSnapshot, createWorkload };
}

describe("WarmStartVerificationService", () => {
it("falls back to a cold create when the snapshot is unchanged", async () => {
const { service, createWorkload } = createService({
latestSnapshotId: DEQUEUED_SNAPSHOT_ID,
});
try {
const message = makeMessage();
const timings = { warmStartCheckMs: 12 };
service.schedule(message, timings);

await vi.waitFor(() => expect(createWorkload).toHaveBeenCalledTimes(1), {
timeout: 3_000,
});
expect(createWorkload).toHaveBeenCalledWith(message, timings);
} finally {
service.stop();
}
});

it("does nothing when the snapshot has moved on (delivered)", async () => {
const { service, getLatestSnapshot, createWorkload } = createService({
latestSnapshotId: "snapshot_executing",
});
try {
service.schedule(makeMessage(), { warmStartCheckMs: 12 });

await vi.waitFor(() => expect(getLatestSnapshot).toHaveBeenCalledTimes(1), {
timeout: 3_000,
});
await sleep(100);
expect(createWorkload).not.toHaveBeenCalled();
} finally {
service.stop();
}
});

it("never falls back when the probe errors", async () => {
const { service, getLatestSnapshot, createWorkload } = createService({ probeError: true });
try {
service.schedule(makeMessage(), { warmStartCheckMs: 12 });

await vi.waitFor(() => expect(getLatestSnapshot).toHaveBeenCalledTimes(1), {
timeout: 3_000,
});
await sleep(100);
expect(createWorkload).not.toHaveBeenCalled();
} finally {
service.stop();
}
});

it("cancel before the delay prevents the probe entirely", async () => {
const { service, getLatestSnapshot, createWorkload } = createService({
latestSnapshotId: DEQUEUED_SNAPSHOT_ID,
});
try {
service.schedule(makeMessage(), { warmStartCheckMs: 12 });

expect(service.cancel("run_1")).toBe(true);

await sleep(SETTLE_MS);
expect(getLatestSnapshot).not.toHaveBeenCalled();
expect(createWorkload).not.toHaveBeenCalled();
} finally {
service.stop();
}
});

it("re-scheduling the same run replaces the pending verification", async () => {
const { service, getLatestSnapshot } = createService({
latestSnapshotId: "snapshot_executing",
});
try {
service.schedule(makeMessage(), { warmStartCheckMs: 1 });
service.schedule(makeMessage(), { warmStartCheckMs: 2 });

await vi.waitFor(() => expect(getLatestSnapshot).toHaveBeenCalledTimes(1), {
timeout: 3_000,
});
await sleep(SETTLE_MS);
expect(getLatestSnapshot).toHaveBeenCalledTimes(1);
} finally {
service.stop();
}
});
});
Loading
Loading