Files
triggerdotdev--trigger.dev/apps/webapp/app/v3/engineVersion.server.ts
Daniel Sutton 8465ac5ac3 feat(run-ops): webapp db topology, flags, and split-mode resolver wiring (#4117)
## What

Wires the run-ops split into the webapp: database topology, environment
flags, split-mode gating, and the control-plane resolver/cache layer
that the run-store and run-engine seams from the previous PR plug into.

- **DB topology & env** (`apps/webapp/app/db.server.ts`,
`env.server.ts`, `entry.server.tsx`): adds the run-ops database
clients/topology and the environment variables that configure and gate
the split.
- **runOpsMigration module** (new
`apps/webapp/app/v3/runOpsMigration/`): the webapp-side machinery —
`splitMode.server.ts`, `controlPlaneResolver.server.ts` +
`controlPlaneCache.server.ts`, `readThrough.server.ts`,
`crossSeamGuard.server.ts`, `distinctDbSentinel.server.ts`, id-minting
helpers (`mintBatchFriendlyId`, `runOpsMintKind`,
`resolveInheritedMintKind`), `runOpsCascadeCleanup.server.ts`, the split
read gate, and route/unblock catalogs.
- **Store/engine wiring** (`app/v3/runStore.server.ts`,
`runEngine.server.ts`, `runEngineHandlers.server.ts` + new
`runEngineHandlersShared.server.ts`): points the webapp's store/engine
construction at the resolver, and factors shared handler logic out so
both seams use one path.
- **Read-path touch-ups**: `runtimeEnvironment.server.ts`,
`eventRepository/index.server.ts`, `taskRunHeartbeatFailed.server.ts`,
`engineVersion.server.ts` route their run/environment lookups
read-through the resolver.
- `413a94511` — interlocks split mode against the native realtime
backend so the two aren't enabled in an incompatible combination (see
`.server-changes/run-ops-split-realtime-interlock.md`).
- `dc74c57fd` — drops the earlier "known-migrated" read layer; residency
is determined by id-shape only.

## Why

PR5 of the run-ops split stack. This is the webapp foundation layer: it
stands up the DB topology, flags, and resolver/cache the rest of the
stack depends on, and repoints webapp read paths through the resolver.
Additive when the split is not enabled (existing single-DB behavior
preserved behind flags); behavior-changing on the read-through paths and
the realtime interlock.

## Tests

New vitest coverage across `apps/webapp/test/` and colocated
`*.server.test.ts` files: db topology, split mode, split read gate,
cross-seam guard, mint cutover / flip latency, control-plane cache,
control-plane resolver, distinct-db sentinel, read-through loaders
(route loaders, run-detail loaders, `findEnvironmentFromRun`), and the
run-engine handlers. Testcontainers-backed; no mocks. `pnpm-lock.yaml`
synced for the two new webapp deps.

## Notes

Draft, **stacked on #4116** (`runops/pr04-store-engine`). Review that
first; this diff is against it.

Server-change / changeset note to be added at stack-assembly time.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-03 18:02:22 +01:00

82 lines
2.3 KiB
TypeScript

import { RunEngineVersion, type RuntimeEnvironmentType } from "@trigger.dev/database";
import { $replica } from "~/db.server";
import {
findCurrentWorkerFromEnvironment,
getCurrentWorkerDeploymentEngineVersion,
} from "./models/workerDeployment.server";
// Co-locate the per-env run-ops residency/mint decision next to the
// engine-version decision. determineEngineVersion is intentionally left untouched so its
// read-only callers (presenters, admin routes, pauseQueue) never pay the mint flag read.
export { resolveRunIdMintKind, type RunIdMintKind } from "./runOpsMigration/runOpsMintKind.server";
type Environment = {
id: string;
type: RuntimeEnvironmentType;
project: {
id: string;
engine: RunEngineVersion;
};
};
export async function determineEngineVersion({
environment,
workerVersion,
engineVersion: version,
}: {
environment: Environment;
workerVersion?: string;
engineVersion?: RunEngineVersion;
}): Promise<RunEngineVersion> {
if (version) {
return version;
}
// If the project is V1, then none of the background workers are running V2
if (environment.project.engine === RunEngineVersion.V1) {
return "V1";
}
/**
* The project has V2 enabled so it *could* be V2.
*/
// A specific worker version is requested
if (workerVersion) {
const worker = await $replica.backgroundWorker.findUnique({
select: {
engine: true,
},
where: {
projectId_runtimeEnvironmentId_version: {
projectId: environment.project.id,
runtimeEnvironmentId: environment.id,
version: workerVersion,
},
},
});
if (!worker) {
throw new Error(`Worker not found: environment: ${environment.id} version: ${workerVersion}`);
}
return worker.engine;
}
// Dev: use the latest BackgroundWorker
if (environment.type === "DEVELOPMENT") {
const backgroundWorker = await findCurrentWorkerFromEnvironment(environment);
return backgroundWorker?.engine ?? "V1";
}
// Deployed: use the latest deployed BackgroundWorker
const currentDeploymentEngineVersion = await getCurrentWorkerDeploymentEngineVersion(
environment.id
);
if (currentDeploymentEngineVersion) {
return currentDeploymentEngineVersion;
}
return environment.project.engine;
}